garage44.eu/jeroen/pinnacle

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

.. /boot/src/arm.rs

raw

//! Starting the kernel on ARM: its boot image read whole, a copy of the
//! device tree carrying the command line, the boot services left, the
//! caches cleaned to memory and the MMU off, then a jump to the image's
//! first byte with the tree in x0 and x1–x3 zero (Linux's `booting.rst`
//! sets the convention out; the kernel's `boot.rs` follows it).
//!
//! The kernel moves itself to its load address, which its header says
//! (offset 32): neither the tree nor the image may lie there. Its own
//! memory is handed out from its device tree's map; the firmware's is
//! not used again.

use crate::efi::{self, Firmware};
use crate::fdt;

const PAGE: usize = 4096;
const TWO_MIB: u64 = 2 << 20;

fn overlaps(a: (u64, u64), b: (u64, u64)) -> bool {
    a.0 < b.1 && b.0 < a.1
}

pub fn start(firmware: &Firmware, line: &str) -> &'static str {
    let Some((file, size)) = firmware.read_file("\\PINNACLE\\KERNEL", None) else {
        return "no kernel at PINNACLE/KERNEL";
    };
    // SAFETY: the pages the file was read into.
    let image = unsafe { core::slice::from_raw_parts(file as *const u8, size) };
    if size < 64 || &image[56..60] != b"ARM\x64" {
        return "PINNACLE/KERNEL is not an ARM64 boot image";
    }
    let word = |at: usize| u64::from_le_bytes(image[at..at + 8].try_into().unwrap());
    let (text_offset, image_size, load) = (word(8), word(16), word(32));
    // Where the kernel will be once it has moved itself: nothing of ours
    // may lie there.
    let kernel = (load, load + image_size.max(size as u64));

    // The image at a 2 MiB boundary plus its offset, as booti puts it.
    let pages = (size + (2 * TWO_MIB) as usize).div_ceil(PAGE);
    let Some(area) = firmware.allocate(pages, None) else {
        return "no memory for the kernel";
    };
    let placed = area.next_multiple_of(TWO_MIB) + text_offset;
    // SAFETY: the area allocated holds the image at `placed`.
    unsafe { core::ptr::copy_nonoverlapping(file as *const u8, placed as *mut u8, size) };

    let Some(tree) = firmware.configuration(efi::DEVICE_TREE) else {
        return "the firmware gave no device tree";
    };
    // SAFETY: the tree the firmware's table points at.
    let Some(tree_size) = (unsafe { fdt::size(tree as *const u8) }) else {
        return "the firmware's device tree is not one";
    };
    // SAFETY: as above, the whole tree.
    let tree = unsafe { core::slice::from_raw_parts(tree as *const u8, tree_size) };
    let room = tree_size + line.len() + 4096;
    // A place for the copy away from the kernel's: tried until one is.
    let mut copy = None;
    for _ in 0..8 {
        let Some(at) = firmware.allocate(room.div_ceil(PAGE), None) else {
            break;
        };
        if !overlaps((at, at + room as u64), kernel) {
            copy = Some(at);
            break;
        }
    }
    let Some(copy) = copy else {
        return "no memory for the device tree away from the kernel";
    };
    // SAFETY: the pages just allocated.
    let out = unsafe { core::slice::from_raw_parts_mut(copy as *mut u8, room) };
    let Some(copy_size) = fdt::with_bootargs(tree, line, out) else {
        return "the device tree could not be copied";
    };

    firmware.print("Pinnacle: starting the kernel\n");
    let mut map = [0u8; 16 * 1024];
    for _ in 0..4 {
        let Some((_, key, _)) = firmware.memory_map(&mut map) else {
            return "no memory map";
        };
        if firmware.exit(key) {
            // SAFETY: the boot services are left; the image and the tree
            // are whole in memory and the kernel takes it from here.
            unsafe { jump(placed, size, copy, copy_size) }
        }
    }
    "the firmware's boot services could not be left"
}

/// Cleans `len` bytes from `at` out of the data cache to memory.
fn clean(at: u64, len: usize) {
    let mut line = at & !63;
    while line < at + len as u64 {
        // SAFETY: cleaning and invalidating a line only moves data to memory.
        unsafe { core::arch::asm!("dc civac, {}", in(reg) line, options(nostack)) };
        line += 64;
    }
}

/// Turns the MMU and the caches off at the EL the firmware runs at and
/// jumps to the image at `image`, the tree at `tree` in x0.
///
/// # Safety
/// The boot services are left; the image and the tree are where they say,
/// and the code that runs is identity mapped, as UEFI maps all of it.
unsafe fn jump(image: u64, image_size: usize, tree: u64, tree_size: usize) -> ! {
    clean(image, image_size);
    clean(tree, tree_size);
    // SAFETY: as the caller promises; from the MMU's turning off on, the
    // code runs at the same, physical, addresses.
    unsafe {
        core::arch::asm!(
            "dsb sy",
            "msr daifset, #0xf",
            "mrs x9, CurrentEL",
            "cmp x9, #(2 << 2)",
            "b.ne 1f",
            "mrs x9, sctlr_el2",
            "bic x9, x9, #1",          // M
            "bic x9, x9, #(1 << 2)",   // C
            "bic x9, x9, #(1 << 12)",  // I
            "msr sctlr_el2, x9",
            "b 2f",
            "1:",
            "mrs x9, sctlr_el1",
            "bic x9, x9, #1",
            "bic x9, x9, #(1 << 2)",
            "bic x9, x9, #(1 << 12)",
            "msr sctlr_el1, x9",
            "2:",
            "isb",
            "ic iallu",
            "dsb sy",
            "isb",
            "mov x0, x10",
            "mov x1, xzr",
            "mov x2, xzr",
            "mov x3, xzr",
            "br x11",
            in("x10") tree,
            in("x11") image,
            options(noreturn),
        )
    }
}