code wiki / _hdl_build / nx_mmu_hw.nx

nx_mmu_hw.nx

buildroot/runtime/_hdl_build/nx_mmu_hw.nx

12758 B189 linesdepth 3pulls 3 transitivereach 0 importersview sourcekind tool
docsdependenciesstructsconstsfunctions

about

nx_mmu_hw.nx -- GATE: a FAITHFUL x86-64 4-LEVEL PAGE-TABLE WALK MODEL (deepens MEMORY-MGMT toward the real-hw-MMU gap). Where nx_paging is a flat vpn->pfn model, THIS models the ACTUAL x86-64 hardware structures the CPU's MMU walks: a 48-bit VA split 9/9/9/9/12 -> PML4 -> PDPT -> PD -> PT -> frame, CR3-rooted, with present/writable/user/ NX protection bits accumulated down the levels, page faults, and a TLB with invalidation + CR3-flush. This is the rung BEFORE loading a real CR3 -- the exact table layout the hardware expects. ★ NEVER-BRICK (cardinal 26) BY CONSTRUCTION: 100% in-memory integer model over an mmap'd 'simulated physical RAM'. It issues NO real CR3 write, NO privileged instruction, NO MMU/firmware write. The real-hardware activation path (mmu_activate_real) is a STUB that REFUSES by construction -- a real CR3 load requires a SEPARATELY-proven never-brick hardware capability that does not exist here. Proven mechanically in T5, not asserted. T1 4-level walk: map + translate a VA -> exact PA (frame<<12 | offset). T2 unmapped VA -> page fault. T3 protection: a read-only page faults on WRITE (ok on READ); an NX page faults on EXECUTE. T4 TLB: miss->fill->hit; invlpg evicts (miss again); CR3 reload flushes all. T5 NEVER-BRICK teeth: mmu_activate_real is REFUSED by construction (no real CR3 write path exists) + determinism. expect_exit: 0 Sovereign: nx_syscalls.

dependencies 2 imports · 0 importers

nx_syscalls.nx nx_itoa_lib.nx nx_mmu_hw.nx

imports: nx_syscalls.nxnx_itoa_lib.nx

imported by: nobody (leaf or entry point)

call flow from main pre-order; caps 40 nodes / depth 6 declared; ↻ = already shown

main g_puts sys_write mmu_new sys_mmap sys_mmap ↻ alloc_frame mmu_map sys_mmap ↻ idxL alloc_frame ↻ pte_make pte_frame mmu_translate tlb_lookup sys_mmap ↻ idxL ↻ pte_frame ↻ tlb_fill g_pn nxi_out nxi_fd sys_mmap ↻ ccz_cat_num sys_write ↻ sys_munmap ck g_puts ↻ mmu_invlpg mmu_set_cr3 mmu_activate_real sys_openat_append sys_write ↻ sys_close sys_exit

structs

41struct Mmu { pmem: *i64, next: i64, cr3: i64, tvpn: *i64, tpa: *i64, tfl: *i64, tvalid: *i64, tn: i64, tclk: i64 }

consts

17const F_MAGIC_4095: i64 = 4095
27const ENTRIES: i64 = 512 // 512 8-byte entries per 4KB table (2^9)
28const OFFB: i64 = 12 // page offset bits (4KB pages)
29const NFRAMES: i64 = 64
31const F_P: i64 = 1 // present
32const F_RW: i64 = 2 // writable
33const F_US: i64 = 4 // user-accessible
34const F_NX: i64 = 8 // no-execute (x86-64 XD)
36const FLT_NP: i64 = 0-1 // not present
37const FLT_W: i64 = 0-2 // write-protection
38const FLT_X: i64 = 0-3 // NX / execute violation

functions

19func g_puts(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 }
called by 2: ckmain calls 1: sys_write
24func g_pn(v: i64) -> i64 { nxi_out(v); return 0 }
called by 1: main calls 1: nxi_out
25func ck(name: *u8, c: i64) -> i64 { if c==1 { g_puts(" PASS " as *u8) } else { g_puts(" FAIL " as *u8) } g_puts(name); g_puts("\n" as *u8); return c }
called by 1: main calls 1: g_puts
43func mmu_new() -> *Mmu
called by 1: main calls 1: sys_mmap
55func alloc_frame(m: *Mmu) -> i64 { let f: i64=m.next; m.next=m.next+1; var i: i64=0; while i<ENTRIES { m.pmem[f*ENTRIES+i]=0; i=i+1 } return f }
called by 2: mmu_mapmain
56func pte_make(frame: i64, flags: i64) -> i64 { return (frame<<OFFB) | flags }
called by 1: mmu_map
57func pte_frame(pte: i64) -> i64 { return (pte>>OFFB) }
58func idxL(va: i64, shift: i64) -> i64 { return (va>>shift)&511 }
61func mmu_map(m: *Mmu, va: i64, data_frame: i64, leaf_flags: i64) -> i64
78func tlb_lookup(m: *Mmu, vpn: i64) -> i64 { var i: i64=0; while i<m.tn { if m.tvalid[i]==1 { if m.tvpn[i]==vpn { return i } } i=i+1 } return 0-1 }
called by 1: mmu_translate
79func tlb_fill(m: *Mmu, vpn: i64, paframe: i64, flags: i64) -> i64
called by 1: mmu_translate
85func mmu_invlpg(m: *Mmu, va: i64) -> i64 { let vpn: i64=va>>OFFB; var i: i64=0; while i<m.tn { if m.tvalid[i]==1 { if m.tvpn[i]==vpn { m.tvalid[i]=0 } } i=i+1 } return 0 }
called by 1: main
86func mmu_set_cr3(m: *Mmu, cr3: i64) -> i64 { m.cr3=cr3; var i: i64=0; while i<m.tn { m.tvalid[i]=0; i=i+1 } return 0 } // CR3 load flushes TLB
called by 1: main
89func mmu_translate(m: *Mmu, va: i64, want_w: i64, want_x: i64, out: *i64) -> i64
123func mmu_activate_real(neverbrick_hw_proof: i64) -> i64 { if neverbrick_hw_proof==0 { return 0-1 } return 0-1 }
called by 1: main
125func main() -> i64