garage44.eu/jeroen/pinnacle

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

.. /kernel/src/arch.rs

raw

//! AArch64 registers and instructions the rest of the kernel asks for
//! (`x86_64/arch.rs` is the PC's).

/// The exception level this code runs at (0–3).
pub fn current_el() -> u64 {
    let el: u64;
    // SAFETY: reading CurrentEL has no side effects.
    unsafe { core::arch::asm!("mrs {}, CurrentEL", out(reg) el, options(nomem, nostack)) };
    (el >> 2) & 3
}

/// Whether the MMU and both caches are on (SCTLR_EL1 M, C and I).
pub fn caches_on() -> bool {
    let sctlr: u64;
    // SAFETY: reading SCTLR_EL1 has no side effects.
    unsafe { core::arch::asm!("mrs {}, sctlr_el1", out(reg) sctlr, options(nomem, nostack)) };
    let wanted = 1 | (1 << 2) | (1 << 12);
    sctlr & wanted == wanted
}

/// The syndrome of the exception being handled (ESR_EL1).
pub fn esr() -> u64 {
    let esr: u64;
    // SAFETY: reading ESR_EL1 has no side effects.
    unsafe { core::arch::asm!("mrs {}, esr_el1", out(reg) esr, options(nomem, nostack)) };
    esr
}

/// The faulting address of the exception being handled (FAR_EL1).
pub fn far() -> u64 {
    let far: u64;
    // SAFETY: reading FAR_EL1 has no side effects.
    unsafe { core::arch::asm!("mrs {}, far_el1", out(reg) far, options(nomem, nostack)) };
    far
}

/// Sleeps until an interrupt arrives. A compiler barrier (no `nomem`): the
/// interrupt's handler changes memory a waiting loop reads.
pub fn wait_for_interrupt() {
    // SAFETY: WFI only pauses the core.
    unsafe { core::arch::asm!("wfi", options(nostack)) };
}

/// ID_AA64ISAR0_EL1: which crypto instructions the core has.
pub fn isar0() -> u64 {
    let value: u64;
    // SAFETY: reading an ID register has no side effects.
    unsafe { core::arch::asm!("mrs {}, id_aa64isar0_el1", out(reg) value, options(nomem, nostack)) };
    value
}

/// This core's MPIDR affinity: Aff3, Aff2, Aff1, Aff0, as GICv3 uses it.
pub fn mpidr() -> u64 {
    let value: u64;
    // SAFETY: reading MPIDR_EL1 has no side effects.
    unsafe { core::arch::asm!("mrs {}, mpidr_el1", out(reg) value, options(nomem, nostack)) };
    value & 0xff_00ff_ffff
}

/// This core's index (0 boots, the others count up as they start), which
/// boot puts in TPIDR_EL1.
pub fn core_id() -> usize {
    let id: u64;
    // SAFETY: reading TPIDR_EL1 has no side effects.
    unsafe { core::arch::asm!("mrs {}, tpidr_el1", out(reg) id, options(nomem, nostack)) };
    id as usize
}

/// The machine a program's ELF must be built for: EM_AARCH64.
pub const ELF_MACHINE: u16 = 183;

/// Makes the kernel's writes to a program's tables visible to the walk.
pub fn tables_written() {
    // SAFETY: barriers only.
    unsafe { core::arch::asm!("dsb ishst", "isb", options(nostack)) };
}

/// Code written through the data cache must reach the point of
/// unification before the instruction cache can fetch it.
pub fn make_executable(start: usize, len: usize) {
    const LINE: usize = 64;
    // SAFETY: cache maintenance on memory the caller owns.
    unsafe {
        for line in (start..start + len).step_by(LINE) {
            core::arch::asm!("dc cvau, {}", in(reg) line, options(nostack));
        }
        core::arch::asm!("dsb ish", "ic ialluis", "dsb ish", "isb", options(nostack));
    }
}

/// A system call from kernel code (sleeping, yielding, ending a kernel
/// task): an SVC, which the vectors save and restore every register around.
pub fn kernel_call(call: u64, argument: u64) {
    // SAFETY: the kernel answers it like a program's, perhaps with a switch.
    unsafe { core::arch::asm!("svc #0", in("x8") call, inout("x0") argument => _, options(nostack)) };
}

/// The scheduler is about to run the task whose kernel stack ends at
/// `top`. Nothing to do here: exceptions from EL0 take SP_EL1, which is the
/// task's own already.
pub fn entering_task(_top: u64) {}

/// CPACR_EL1.FPEN: 0b11 lets EL0 use FP/SIMD, 0b01 traps it.
pub fn fp_access(open: bool) {
    let value: u64 = if open { 3 << 20 } else { 1 << 20 };
    // SAFETY: only FP/SIMD access changes, and the kernel never uses them.
    unsafe { core::arch::asm!("msr cpacr_el1, {}", "isb", in(reg) value, options(nostack)) };
}

/// Saves the core's FP/SIMD registers into `state`.
pub fn save_fp(state: &mut [u8; 528]) {
    // SAFETY: writes 528 bytes into `state`; the kernel's code never uses
    // these registers itself (it is built soft-float).
    unsafe {
        core::arch::asm!(
            ".arch_extension fp",
            ".arch_extension simd",
            "stp q0, q1, [{a}, #0]", "stp q2, q3, [{a}, #32]", "stp q4, q5, [{a}, #64]", "stp q6, q7, [{a}, #96]",
            "stp q8, q9, [{a}, #128]", "stp q10, q11, [{a}, #160]", "stp q12, q13, [{a}, #192]",
            "stp q14, q15, [{a}, #224]", "stp q16, q17, [{a}, #256]", "stp q18, q19, [{a}, #288]",
            "stp q20, q21, [{a}, #320]", "stp q22, q23, [{a}, #352]", "stp q24, q25, [{a}, #384]",
            "stp q26, q27, [{a}, #416]", "stp q28, q29, [{a}, #448]", "stp q30, q31, [{a}, #480]",
            "mrs {t}, fpcr", "str {t}, [{a}, #512]", "mrs {t}, fpsr", "str {t}, [{a}, #520]",
            a = in(reg) state.as_mut_ptr(),
            t = out(reg) _,
            options(nostack),
        );
    }
}

/// Loads the core's FP/SIMD registers from `state`.
pub fn load_fp(state: &[u8; 528]) {
    // SAFETY: reads 528 bytes from `state`, as `save_fp` wrote them.
    unsafe {
        core::arch::asm!(
            ".arch_extension fp",
            ".arch_extension simd",
            "ldp q0, q1, [{a}, #0]", "ldp q2, q3, [{a}, #32]", "ldp q4, q5, [{a}, #64]", "ldp q6, q7, [{a}, #96]",
            "ldp q8, q9, [{a}, #128]", "ldp q10, q11, [{a}, #160]", "ldp q12, q13, [{a}, #192]",
            "ldp q14, q15, [{a}, #224]", "ldp q16, q17, [{a}, #256]", "ldp q18, q19, [{a}, #288]",
            "ldp q20, q21, [{a}, #320]", "ldp q22, q23, [{a}, #352]", "ldp q24, q25, [{a}, #384]",
            "ldp q26, q27, [{a}, #416]", "ldp q28, q29, [{a}, #448]", "ldp q30, q31, [{a}, #480]",
            "ldr {t}, [{a}, #512]", "msr fpcr, {t}", "ldr {t}, [{a}, #520]", "msr fpsr, {t}",
            a = in(reg) state.as_ptr(),
            t = out(reg) _,
            options(nostack),
        );
    }
}

/// The virtual counter, read in order: the finest clock there is.
pub fn counter() -> u64 {
    let count: u64;
    // SAFETY: reading the virtual counter has no side effects.
    unsafe { core::arch::asm!("isb", "mrs {}, cntvct_el0", out(reg) count, options(nomem, nostack)) };
    count
}