code wiki / _hdl_build / nx_mmu_hw.nx
nx_mmu_hw.nx
buildroot/runtime/_hdl_build/nx_mmu_hw.nx
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
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
structs
| 41 | struct Mmu { pmem: *i64, next: i64, cr3: i64, tvpn: *i64, tpa: *i64, tfl: *i64, tvalid: *i64, tn: i64, tclk: i64 } |
consts
| 17 | const F_MAGIC_4095: i64 = 4095 |
| 27 | const ENTRIES: i64 = 512 // 512 8-byte entries per 4KB table (2^9) |
| 28 | const OFFB: i64 = 12 // page offset bits (4KB pages) |
| 29 | const NFRAMES: i64 = 64 |
| 31 | const F_P: i64 = 1 // present |
| 32 | const F_RW: i64 = 2 // writable |
| 33 | const F_US: i64 = 4 // user-accessible |
| 34 | const F_NX: i64 = 8 // no-execute (x86-64 XD) |
| 36 | const FLT_NP: i64 = 0-1 // not present |
| 37 | const FLT_W: i64 = 0-2 // write-protection |
| 38 | const FLT_X: i64 = 0-3 // NX / execute violation |
functions
| 19 | func 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 } |
| 24 | func g_pn(v: i64) -> i64 { nxi_out(v); return 0 } |
| 25 | func 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 } |
| 43 | func mmu_new() -> *Mmu |
| 55 | func 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 } |
| 56 | func pte_make(frame: i64, flags: i64) -> i64 { return (frame<<OFFB) | flags } called by 1: mmu_map |
| 57 | func pte_frame(pte: i64) -> i64 { return (pte>>OFFB) } |
| 58 | func idxL(va: i64, shift: i64) -> i64 { return (va>>shift)&511 } |
| 61 | func mmu_map(m: *Mmu, va: i64, data_frame: i64, leaf_flags: i64) -> i64 |
| 78 | func 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 |
| 79 | func tlb_fill(m: *Mmu, vpn: i64, paframe: i64, flags: i64) -> i64 called by 1: mmu_translate |
| 85 | func 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 |
| 86 | func 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 |
| 89 | func mmu_translate(m: *Mmu, va: i64, want_w: i64, want_x: i64, out: *i64) -> i64 |
| 123 | func mmu_activate_real(neverbrick_hw_proof: i64) -> i64 { if neverbrick_hw_proof==0 { return 0-1 } return 0-1 } called by 1: main |
| 125 | func main() -> i64 |