garage44.eu/jeroen/pinnacle

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

.. /kernel/src/boot.rs

raw

//! The first instructions of each core, from the loader's jump (core 0) or
//! PSCI's (the others) to Rust.
//!
//! Only these can be written without a stack, so they are the only part of
//! the kernel in assembly: leave EL2 for EL1 if entered there; turn on the
//! FPU, the MMU and the caches; move to the top half, where the kernel is
//! linked; set up a stack and the core's index in TPIDR_EL1. Core 0 first
//! moves the image to its load address if a loader put it elsewhere, and
//! finishes the translation tables and clears `.bss` before `kmain`; the
//! others find all that done and enter `smp::secondary_main`.

use crate::mmu;

unsafe extern "C" {
    static __stack_top: u8;
    static __bss_start: u8;
    static __bss_end: u8;
}

/// Leaves EL2 for EL1 when entered at EL2 (QEMU with virtualization=on,
/// U-Boot on the RK3399), setting x20 to the EL entered at. Clobbers x1.
macro_rules! enter_el1 {
    () => {
        concat!(
            "mov x20, #1\n",
            "mrs x1, CurrentEL\n",
            "cmp x1, #(2 << 2)\n",
            "b.ne 10f\n",
            "mov x20, #2\n",
            "mov x1, #(1 << 31)\n", // HCR_EL2.RW: EL1 runs AArch64
            "msr hcr_el2, x1\n",
            "mov x1, #3\n", // CNTHCTL_EL2: EL1 may use the physical timer
            "msr cnthctl_el2, x1\n",
            "msr cntvoff_el2, xzr\n",
            "mov x1, #0xf\n", // ICC_SRE_EL2: EL1 may use the GICv3 registers
            "msr icc_sre_el2, x1\n",
            "mov x1, #0x3c5\n", // EL1h, interrupts masked
            "msr spsr_el2, x1\n",
            "adr x1, 10f\n",
            "msr elr_el2, x1\n",
            "eret\n",
            "10:\n",
            "mov x1, #(1 << 20)\n", // CPACR_EL1.FPEN: FP/SIMD from EL0 traps (sched::fp_trap)
            "msr cpacr_el1, x1\n",
        )
    };
}

/// Turns on the MMU and caches with the tables at physical x1 behind both
/// halves, then jumps to the top half. Clobbers x2.
macro_rules! enable_mmu {
    () => {
        concat!(
            "ldr x2, ={mair}\n",
            "msr mair_el1, x2\n",
            "ldr x2, ={tcr}\n",
            "msr tcr_el1, x2\n",
            "msr ttbr0_el1, x1\n",
            "msr ttbr1_el1, x1\n",
            "dsb ish\n",
            "tlbi vmalle1\n",
            "dsb ish\n",
            "isb\n",
            "ldr x2, ={sctlr}\n",
            "msr sctlr_el1, x2\n",
            "isb\n",
            // From here on a symbol's address is its linked one.
            "ldr x2, =25f\n",
            "br x2\n",
            "25:\n",
        )
    };
}
/// The boot image header: the first 64 bytes of the kernel, in the format
/// ARM64 bootloaders take (U-Boot's `booti`, QEMU's `-kernel`; it was set
/// out for Linux, in its `Documentation/arch/arm64/booting.rst`, and holds
/// no code of it). They start the kernel at the first byte, MMU off, the
/// device tree in x0. Flags: little-endian, 4 KiB pages, placeable anywhere
/// (boot moves it).
#[unsafe(naked)]
#[unsafe(link_section = ".text.head")]
unsafe extern "C" fn _image_header() -> ! {
    core::arch::naked_asm!(
        "b {start}",                // code0: over the header
        ".word 0",                  // code1
        ".quad __text_offset",      // from a 2 MiB boundary
        ".quad __image_size",       // all of it, .bss and the boot stack too
        ".quad 0xa",                // flags
        // Reserved for Linux; Pinnacle's own loader (`boot/`) reads its
        // load address here, where nothing of its own may lie.
        ".quad __load_physical",
        ".quad 0",
        ".quad 0",
        ".ascii \"ARM\\x64\"",      // magic
        ".word 0",
        start = sym _start,
    )
}

#[unsafe(naked)]
#[unsafe(no_mangle)]
#[unsafe(link_section = ".text.boot")]
unsafe extern "C" fn _start() -> ! {
    // x0 holds the device tree's address throughout: kmain's first argument.
    core::arch::naked_asm!(
        // A loader leaves x1–x3 zero; a boot kernel handing over (`chain`)
        // puts its mark in x1, the EL it was entered at in x2 and the slot
        // in x3. Kept until kmain.
        "mov x21, x1",
        "mov x22, x2",
        "mov x23, x3",
        // Core 0 of cluster 0 boots; any other the loader started waits for
        // good. The others are started through PSCI at `_start_secondary`.
        "mrs x1, mpidr_el1",
        "and x1, x1, #0xffff",
        "cbnz x1, 90f",
        // A loader that put the image anywhere but its load address (booti
        // keeps only the 2 MiB alignment): move it there, then go on in the
        // copy. The device tree in x0 is left alone.
        "adr x1, {head}",
        "ldr x2, =__load_physical",
        "cmp x1, x2",
        "b.eq 45f",
        "ldr x3, =__image_file_size",
        "add x3, x3, #7",
        "and x3, x3, #~7",
        "cmp x2, x1",
        "b.hi 42f",
        "mov x4, #0",              // destination lower: copy forwards
        "40:",
        "ldr x5, [x1, x4]",
        "str x5, [x2, x4]",
        "add x4, x4, #8",
        "cmp x4, x3",
        "b.lo 40b",
        "b 44f",
        "42:",
        "mov x4, x3",              // destination higher: copy backwards
        "43:",
        "sub x4, x4, #8",
        "ldr x5, [x1, x4]",
        "str x5, [x2, x4]",
        "cbnz x4, 43b",
        "44:",
        "ic iallu",                // the instruction cache may be on
        "dsb sy",
        "isb",
        "adr x4, 45f",
        "sub x4, x4, x1",
        "add x4, x4, x2",
        "br x4",
        "45:",
        enter_el1!(),

        // Turn each level-1 table descriptor's offset into an address.
        "adrp x1, {tables}",
        "add x1, x1, :lo12:{tables}",
        "mov x2, #0",
        "20:",
        "ldr x3, [x1, x2, lsl #3]",
        "and x4, x3, #3",
        "cmp x4, #{table}",
        "b.ne 21f",
        "add x3, x3, x1",
        "str x3, [x1, x2, lsl #3]",
        "21:",
        "add x2, x2, #1",
        "cmp x2, #{mapped}",
        "b.lo 20b",
        enable_mmu!(),

        "ldr x1, ={stack_top}",
        "mov sp, x1",
        "msr tpidr_el1, xzr",
        "ldr x1, ={bss_start}",
        "ldr x2, ={bss_end}",
        "30:",
        "cmp x1, x2",
        "b.hs 31f",
        "stp xzr, xzr, [x1], #16",
        "b 30b",
        "31:",
        // Handed over to: the EL is the boot kernel's (this one entered at
        // EL1 whatever it was), and the slot kmain's third argument.
        "mov x2, #0",
        "ldr x4, ={handed}",
        "cmp x21, x4",
        "b.ne 32f",
        "mov x20, x22",
        "mov x2, x23",
        "32:",
        "mov x1, x20",
        "b {kmain}",

        "90:",
        "wfe",
        "b 90b",
        tables = sym mmu::TABLES,
        table = const mmu::TABLE,
        mapped = const mmu::MAPPED_GIB,
        mair = const mmu::MAIR,
        tcr = const mmu::TCR,
        sctlr = const mmu::SCTLR,
        stack_top = sym __stack_top,
        bss_start = sym __bss_start,
        bss_end = sym __bss_end,
        kmain = sym crate::kmain,
        head = sym _image_header,
        handed = const crate::chain::HANDED_OVER,
    )
}

/// Where PSCI CPU_ON starts the other cores, MMU off, with their index in
/// x0 (the context id `smp::start_cores` passes).
#[unsafe(naked)]
#[unsafe(link_section = ".text.boot")]
pub unsafe extern "C" fn _start_secondary() -> ! {
    core::arch::naked_asm!(
        "mov x19, x0",
        enter_el1!(),
        "adrp x1, {tables}",
        "add x1, x1, :lo12:{tables}",
        enable_mmu!(),
        // The core's stack is its idle task's: slot x19's.
        "ldr x1, ={stacks}",
        "add x1, x1, x19, lsl #{stack_shift}",
        "add x1, x1, #{stack_size}",
        "mov sp, x1",
        "msr tpidr_el1, x19",
        "mov x0, x19",
        "b {main}",
        tables = sym mmu::TABLES,
        mair = const mmu::MAIR,
        tcr = const mmu::TCR,
        sctlr = const mmu::SCTLR,
        stacks = sym crate::sched::STACKS,
        stack_shift = const crate::sched::STACK_SIZE.trailing_zeros(),
        stack_size = const crate::sched::STACK_SIZE,
        main = sym crate::smp::secondary_main,
    )
}