std1.0.0-beta-7 SafeC docs

Module kernel::mmu

#include <std/kernel/mmu.h> source

MMU Context (real hardware page tables) Virtual-to-physical address translation for freestanding targets. Freestanding-safe; TLB/activate operations use inline asm inside unsafe{}.

MmuContext<F> builds the page tables the CPU itself walks. F is a compile-time page-table format type — every table walk step is a direct, inlinable call to F's methods, the same pattern as BST<K, V, C>'s ordering. One format per architecture:

MmuX86_64   x86-64 4-level paging (48-bit VA; PML4/PDPT/PD/PT, 8-byte entries)
MmuX86_32   x86 32-bit paging without PAE (2 levels, 4-byte entries)
MmuSv39     RISC-V Sv39 (RV64, 3 levels, 8-byte PTEs)
MmuSv32     RISC-V Sv32 (RV32, 2 levels, 4-byte PTEs)
MmuAArch64  AArch64 stage 1, 4 KiB granule, 48-bit VA (4 levels)
MmuArmV7    ARMv7-A short-descriptor (4096-entry L1 + 256-entry L2)

MmuNative names the format of the CPU being compiled for (none on Cortex-M, which has an MPU but no MMU, or on wasm). All formats can be built and walked on any target — e.g. on the host, for tests or for an image builder — but activate()/tlb_flush_*() only touch the CPU when the format matches the running bare-metal target (they return 0 otherwise, and always in a hosted process).

Mappings are 4 KiB pages. walk() also follows larger (block/superpage) leaves built by someone else.

Tables are read and written at (physical address + phys_offset): 0 for an identity-mapped kernel, the direct-map base for a higher-half one. New intermediate tables come from 'frames' (one 4 KiB frame each, zeroed); the root table is the caller's.

Structs

MmuX86_64Non-leaf entries carry P|RW|US so the leaf alone decides access; non-exec pages set NX (bit 63), which needs EFER.NXE enabled.
MmuX86_32No NX without PAE: MMU_EXEC is implied.
MmuSv39Leaves set A and D up front (no faults on hardware without A/D updates).
MmuSv32
MmuAArch64Leaves use MAIR attribute index 0 for normal memory and 1 for device memory (program MAIR_EL1 with Attr0 = 0xFF, Attr1 = 0x04), inner-shareable, AF set. TCR_EL1 must select a 4 KiB granule and T0SZ = 16.
MmuArmV7Small pages; the root L1 table is 16 KiB and must be 16 KiB aligned. Normal memory is TEX=001 C=1 B=1 (write-back), device is TEX=000 C=0 B=1.
MmuContext

Traits

MmuFormatEach format has the same methods (unused_ only gives the struct a size):

Type aliases

MmuNative

Macros

MMU_WRITEPortable mapping flags (map()'s 'flags'). Every mapping is readable.
MMU_EXECexecutable
MMU_USERreachable from user mode (x86 U/S, RISC-V U, ARM EL0/PL0)
MMU_DEVICEuncached device memory (x86 PCD|PWT, ARM device attributes; RISC-V: no effect — PMAs decide)