//! The identity map the kernel runs on from its first Rust instruction. //! //! The tables are computed at compile time from the board's memory map, so //! boot only points the MMU at them: the caches are on before any Rust runs, //! and with them off an AArch64 core runs tens of times slower. Where a whole //! GiB is one kind of memory it is one 1 GiB block, one TLB entry; only a GiB //! that mixes kinds is split into 2 MiB blocks. /// What a region of the physical address space holds. #[derive(Clone, Copy)] pub enum Kind { /// Registers: uncached, strictly ordered, never executed. Device, /// RAM: write-back cached. Normal, } /// A range of the physical address space, `start..end`, 2 MiB aligned. pub struct Region { pub start: u64, pub end: u64, pub kind: Kind, } const GIB: u64 = 1 << 30; const BLOCK_2M: u64 = 2 << 20; const ENTRIES: usize = 512; /// How many GiB from address 0 are mapped: both boards keep RAM and devices /// in the low 4 GiB. pub const MAPPED_GIB: usize = 4; /// MAIR_EL1: attribute 0 is Device-nGnRnE, 1 is Normal write-back /// read/write-allocate. pub const MAIR: u64 = 0xff << 8; const ATTR_DEVICE: u64 = 0 << 2; const ATTR_NORMAL: u64 = 1 << 2; /// Where the kernel's half of the address space starts: TTBR1 translates /// the top 512 GiB, and the kernel sees all physical memory there at this /// offset. The lower half (TTBR0) is left to programs. pub const KERNEL_OFFSET: usize = 0xffff_ff80_0000_0000; /// The kernel's address for a physical address. pub const fn virt(physical: usize) -> usize { physical + KERNEL_OFFSET } /// The physical address behind a kernel address. pub const fn physical(virt: usize) -> usize { virt - KERNEL_OFFSET } /// TCR_EL1 for both halves: 39-bit addresses (the walk starts at level 1), /// 4 KiB granules, table walks cached and inner shareable, 36-bit physical /// addresses. Boot translates the lower half through the same tables, the /// identity map it needs while it still runs at physical addresses. const TCR_HALVES: u64 = { let lower = 25 | (1 << 8) | (1 << 10) | (3 << 12); let upper = (25 << 16) | (1 << 24) | (1 << 26) | (3 << 28) | (0b10 << 30); lower | upper | (1 << 32) }; pub const TCR: u64 = TCR_HALVES; /// TCR_EL1.EPD0: no walks through TTBR0, so the lower half faults until a /// program's tables are there. pub const TCR_NO_LOWER: u64 = TCR_HALVES | (1 << 7); /// SCTLR_EL1: the MMU, the data cache and the instruction cache on, with the /// bits ARMv8.0 reserves as one. pub const SCTLR: u64 = (1 << 29) | (1 << 28) | (1 << 23) | (1 << 22) | (1 << 20) | (1 << 11) | (1 << 12) | (1 << 2) | 1; const BLOCK: u64 = 0b01; /// A level-1 table descriptor. Its address is the level-2 table's offset from /// [`TABLES`] until boot adds the tables' own address: a pointer cannot become /// an integer at compile time. pub const TABLE: u64 = 0b11; const ACCESSED: u64 = 1 << 10; const INNER_SHAREABLE: u64 = 0b11 << 8; const PRIVILEGED_NEVER_EXECUTE: u64 = 1 << 53; const USER_NEVER_EXECUTE: u64 = 1 << 54; #[repr(C, align(4096))] pub struct Tables { l1: [u64; ENTRIES], l2: [[u64; ENTRIES]; MAPPED_GIB], } /// The kernel's translation tables. Boot rewrites the level-1 table /// descriptors in place before turning the MMU on, and nothing else touches /// them. pub static mut TABLES: Tables = build(crate::board::MEMORY); const fn block(address: u64, kind: Kind) -> u64 { match kind { Kind::Normal => address | BLOCK | ATTR_NORMAL | INNER_SHAREABLE | ACCESSED | USER_NEVER_EXECUTE, Kind::Device => { address | BLOCK | ATTR_DEVICE | ACCESSED | PRIVILEGED_NEVER_EXECUTE | USER_NEVER_EXECUTE } } } /// The kind of the 2 MiB at `address`, `None` when unmapped. const fn kind_at(regions: &[Region], address: u64) -> Option { let mut i = 0; while i < regions.len() { let r = ®ions[i]; assert!( r.start.is_multiple_of(BLOCK_2M) && r.end.is_multiple_of(BLOCK_2M), "regions must be 2 MiB aligned" ); assert!( r.end <= MAPPED_GIB as u64 * GIB, "a region lies above the mapped GiB" ); if address >= r.start && address < r.end { return Some(r.kind); } i += 1; } None } const fn same(a: Option, b: Option) -> bool { matches!( (a, b), (None, None) | (Some(Kind::Device), Some(Kind::Device)) | (Some(Kind::Normal), Some(Kind::Normal)) ) } const fn build(regions: &[Region]) -> Tables { let mut tables = Tables { l1: [0; ENTRIES], l2: [[0; ENTRIES]; MAPPED_GIB], }; let mut gib = 0; while gib < MAPPED_GIB { let base = gib as u64 * GIB; let first = kind_at(regions, base); let mut uniform = true; let mut i = 0; while i < ENTRIES { let address = base + i as u64 * BLOCK_2M; let kind = kind_at(regions, address); if let Some(kind) = kind { tables.l2[gib][i] = block(address, kind); } uniform &= same(kind, first); i += 1; } tables.l1[gib] = match (uniform, first) { (true, Some(kind)) => block(base, kind), (true, None) => 0, (false, _) => (size_of::<[u64; ENTRIES]>() * (1 + gib)) as u64 | TABLE, }; gib += 1; } tables } /// The kernel's half has a table of its own (TTBR1): a core left on a freed /// program's TTBR0 only ever walks it for the lower half, which a kernel /// task never touches. pub const PROGRAM_TABLES_HOLD_THE_KERNEL: bool = false; /// Stops translating the lower half: boot's identity map is no longer /// needed once the kernel runs in the top half, and with it gone a stray /// physical address faults instead of reaching memory. pub fn close_lower_half() { // SAFETY: nothing in the kernel uses lower-half addresses after boot. unsafe { core::arch::asm!( "msr tcr_el1, {tcr}", "isb", "tlbi vmalle1", "dsb nsh", "isb", tcr = in(reg) TCR_NO_LOWER, options(nostack), ); } } /// Translates the lower half again, for a program's tables in TTBR0. pub fn open_lower_half() { // SAFETY: TTBR0 is written with a program's tables before anything uses // a lower-half address. unsafe { core::arch::asm!("msr tcr_el1, {tcr}", "isb", tcr = in(reg) TCR, options(nostack)) }; } /// Points the lower half at a program's tables (TTBR0's value, its ASID /// within): the ASID keeps the previous program's TLB entries apart. pub fn switch_lower_half(ttbr: u64) { // SAFETY: the tables are complete. unsafe { core::arch::asm!("msr ttbr0_el1, {}", "isb", in(reg) ttbr, options(nostack)) }; }