garage44.eu/jeroen/pinnacle

logclone: git clone https://garage44.eu/jeroen/pinnacle.git

.. /kernel/src/x86_64/apic.rs

raw

//! The local APIC: each core's interrupt controller and its timer. In
//! x2APIC mode where the core has it (every Intel core since 2008), its
//! registers model-specific registers from 0x800; else (xAPIC) where
//! ACPI's MADT puts them (0xfee00000 on every PC so far), in the uncached
//! fourth GiB. A firmware may hand over in x2APIC mode already, which
//! cannot be left without turning the APIC off: so x2APIC is taken
//! whenever it is there. The legacy 8259 PICs are masked: every interrupt
//! comes through an APIC.

use core::sync::atomic::{AtomicBool, AtomicU32, AtomicUsize, Ordering};

/// The timer's vector, and the one the APIC sends when an interrupt it
/// raised went away (no end of interrupt for it).
pub const TIMER: u64 = 32;
pub const SPURIOUS: u64 = 255;

const ID: usize = 0x20;
const ICR_LOW: usize = 0x300;
const ICR_HIGH: usize = 0x310;
const ICR_PENDING: u32 = 1 << 12;
const INIT: u32 = 0b101 << 8;
const STARTUP: u32 = 0b110 << 8;
const ASSERT: u32 = 1 << 14;
const LEVEL: u32 = 1 << 15;
const EOI: usize = 0xb0;
const SPURIOUS_VECTOR: usize = 0xf0;
const LVT_TIMER: usize = 0x320;
const TIMER_INITIAL: usize = 0x380;
const TIMER_CURRENT: usize = 0x390;
const TIMER_DIVIDE: usize = 0x3e0;

const ENABLE: u32 = 1 << 8;
const PERIODIC: u32 = 1 << 17;
const MASKED: u32 = 1 << 16;
/// Divide the bus clock by 16.
const DIVIDE_16: u32 = 0b0011;

static BASE: AtomicUsize = AtomicUsize::new(0);
/// The timer's counts in a millisecond, measured once.
static PER_MS: AtomicU32 = AtomicU32::new(0);
/// Every core's APIC in x2APIC mode (chosen by the boot core for all).
static X2: AtomicBool = AtomicBool::new(false);

/// IA32_APIC_BASE: the APIC on (EN), and in x2APIC mode (EXTD).
const APIC_BASE_MSR: u32 = 0x1b;
const APIC_EN: u64 = 1 << 11;
const APIC_EXTD: u64 = 1 << 10;
/// x2APIC's registers: xAPIC's offsets over 16, from this MSR.
const X2_MSRS: u32 = 0x800;

fn read(register: usize) -> u32 {
    if X2.load(Ordering::Relaxed) {
        return crate::arch::read_msr(X2_MSRS + (register >> 4) as u32) as u32;
    }
    // SAFETY: an APIC register, mapped uncached.
    unsafe { ((BASE.load(Ordering::Relaxed) + register) as *const u32).read_volatile() }
}

fn write(register: usize, value: u32) {
    if X2.load(Ordering::Relaxed) {
        crate::arch::write_msr(X2_MSRS + (register >> 4) as u32, u64::from(value));
        return;
    }
    // SAFETY: as in `read`.
    unsafe { ((BASE.load(Ordering::Relaxed) + register) as *mut u32).write_volatile(value) }
}

/// This core's APIC ID: 32 bits of it in x2APIC mode, 8 in xAPIC mode.
pub fn id() -> u32 {
    if X2.load(Ordering::Relaxed) {
        read(ID)
    } else {
        read(ID) >> 24
    }
}

/// Whether the APICs run in x2APIC mode.
pub fn x2apic() -> bool {
    X2.load(Ordering::Relaxed)
}

/// Puts this core's APIC in x2APIC mode where [`X2`] says so (from xAPIC
/// mode the way there is EN then EN | EXTD).
fn choose_mode() {
    let base = crate::arch::read_msr(APIC_BASE_MSR);
    if X2.load(Ordering::Relaxed) && base & APIC_EXTD == 0 {
        crate::arch::write_msr(APIC_BASE_MSR, base | APIC_EN | APIC_EXTD);
    }
}

/// Turns on this core's APIC with its registers at physical `base` (in
/// xAPIC mode), the 8259s masked. `xapic`: keep xAPIC mode even where the
/// core has x2APIC (the command line's `xapic`), unless the firmware left
/// x2APIC on.
pub fn init(base: u64, xapic: bool) {
    let has_x2 = crate::arch::cpuid(1)[2] & (1 << 21) != 0;
    let left_on = crate::arch::read_msr(APIC_BASE_MSR) & APIC_EXTD != 0;
    X2.store(left_on || (has_x2 && !xapic), Ordering::Relaxed);
    BASE.store(crate::mmu::virt(base as usize), Ordering::Relaxed);
    choose_mode();
    crate::arch::out8(0x21, 0xff);
    crate::arch::out8(0xa1, 0xff);
    write(SPURIOUS_VECTOR, ENABLE | SPURIOUS as u32);
}

/// Turns on a core's APIC, the boot core's having found where it is and
/// chosen its mode.
pub fn init_core() {
    choose_mode();
    write(SPURIOUS_VECTOR, ENABLE | SPURIOUS as u32);
    // The timer counts as the boot core's, whose rate was measured.
    write(TIMER_DIVIDE, DIVIDE_16);
}

/// Sends interrupt `vector` to the core with APIC ID `id`.
pub fn send_ipi(id: u32, vector: u32) {
    let _masked = crate::irq::Masked::new();
    send(id, vector);
}

/// Sends `command` to the core with APIC ID `id`, once the last one left.
fn send(id: u32, command: u32) {
    if X2.load(Ordering::Relaxed) {
        // One 64-bit register, the destination in its top half; nothing
        // to wait for.
        crate::arch::write_msr(
            X2_MSRS + (ICR_LOW >> 4) as u32,
            u64::from(id) << 32 | u64::from(command),
        );
        return;
    }
    write(ICR_HIGH, id << 24);
    write(ICR_LOW, command);
    while read(ICR_LOW) & ICR_PENDING != 0 {
        core::hint::spin_loop();
    }
}

/// Waits `us` microseconds by the time-stamp counter.
pub fn wait_us(us: u64) {
    let hz = crate::timer::frequency().max(1);
    let end = crate::arch::tsc() + hz * us / 1_000_000;
    while crate::arch::tsc() < end {
        core::hint::spin_loop();
    }
}

/// Wakes the core with APIC ID `id` at physical page `page`: INIT, 10 ms,
/// then STARTUP twice, 200 µs apart, as the MP specification has it.
pub fn wake(id: u32, page: u8) {
    send(id, INIT | ASSERT | LEVEL);
    send(id, INIT | LEVEL);
    wait_us(10_000);
    for _ in 0..2 {
        send(id, STARTUP | u32::from(page));
        wait_us(200);
    }
}

/// Starts the timer at `hz` interrupts a second, its rate measured first
/// against the time-stamp counter over 10 ms.
pub fn start_timer(hz: u64) {
    write(TIMER_DIVIDE, DIVIDE_16);
    write(LVT_TIMER, MASKED);
    write(TIMER_INITIAL, u32::MAX);
    let start = crate::timer::now_ms();
    while crate::timer::now_ms() < start + 10 {}
    let counted = u32::MAX - read(TIMER_CURRENT);
    PER_MS.store(counted / 10, Ordering::Relaxed);
    write(LVT_TIMER, TIMER as u32 | PERIODIC);
    write(TIMER_INITIAL, ((u64::from(counted) / 10) * 1000 / hz) as u32);
}

/// Sets this core's timer to fire once, `ms` milliseconds from now (at
/// once for 0), or stops it.
pub fn one_shot(ms: Option<u64>) {
    match ms {
        Some(ms) => {
            let counts = (ms.max(1) * u64::from(PER_MS.load(Ordering::Relaxed))).min(u64::from(u32::MAX));
            write(LVT_TIMER, TIMER as u32);
            write(TIMER_INITIAL, counts as u32);
        }
        None => write(TIMER_INITIAL, 0),
    }
}

/// The timer's rate, in counts a millisecond.
pub fn timer_rate() -> u32 {
    PER_MS.load(Ordering::Relaxed)
}

/// Ends the interrupt being handled.
pub fn end() {
    write(EOI, 0);
}