code wiki / _hdl_build / nx_pagefault_emit.nx
nx_pagefault_emit.nx
buildroot/runtime/_hdl_build/nx_pagefault_emit.nx
about
nx_pagefault_emit.nx -- PAGE-FAULT TRAP test (Sv39 paging completion rung).
Table-computes a bare-metal rv64 image that proves the CPU raises a LOAD-PAGE-FAULT trap on an
unmapped virtual address and vectors to the handler (mtvec) -- the foundation of demand paging /
memory protection. Sequence (satp = Sv39 throughout after it is set):
1. install mtvec = &handler (physical; fetch is untranslated so the handler runs physically)
2. lay a valid gigapage PTE + a sentinel; csrrw satp (Sv39 ON)
3. load the MAPPED VA 0xC0009000 -> sentinel -> verify -> emit "OK"
4. load the UNMAPPED VA 0x00009000 -> the walk faults -> the CPU takes a load-page-fault trap
-> PC = mtvec -> the handler emits "PF" and halts. (If the fault did NOT fire, control
falls through to emit "N" -> the gate sees no "PF" = RED.)
Success transcript = "OKPF". The handler's UART/finisher writes hit device MMIO (xlate-exempt),
so it works with paging on. Zero hand-written machine code; two-pass forward-ref resolution.
nx_pagefault_emit -> runtime/_hdl_build/_pagefault_virt.bin + .gold
Sovereign, no gcc/.sh. license_tier: ORIGINAL
dependencies 1 imports · 0 importers
imports: nx_syscalls.nx
imported by: nobody (leaf or entry point)
call flow from main pre-order; caps 40 nodes / depth 6 declared; ↻ = already shown
structs
| none |
consts
| 17 | const PF_MAGIC_8192: i64 = 8192 |
| 19 | const PF_OUT: *u8 = "runtime/_hdl_build/_pagefault_virt.bin" |
| 20 | const PF_GOLD: *u8 = "runtime/_hdl_build/_pagefault_virt.bin.gold" |
| 21 | const PF_LOG: *u8 = "knowledge/status/paging.log" |
| 23 | const PF_UART: i64 = 0x10000000 |
| 24 | const PF_FIN: i64 = 0x100000 |
| 25 | const PF_PASS: i64 = 0x5555 |
| 26 | const PF_MEM_BASE: i64 = 0x80000000 |
| 27 | const PF_CSR_SATP: i64 = 0x180 |
| 28 | const PF_CSR_MTVEC: i64 = 0x305 |
| 29 | const PF_CSR_MSTATUS: i64 = 0x300 |
| 30 | const PF_CSR_MEPC: i64 = 0x341 |
| 31 | const PF_MRET: i64 = 0x30200073 |
| 32 | const PF_MPP_S: i64 = 0x800 |
| 34 | const RV_X0: i64 = 0 |
| 35 | const RV_T0: i64 = 5 |
| 36 | const RV_T1: i64 = 6 |
| 37 | const RV_T2: i64 = 7 |
| 38 | const RV_T3: i64 = 28 |
| 39 | const RV_T4: i64 = 29 |
| 40 | const RV_T5: i64 = 30 |
| 42 | const PF_PTE_ADDR: i64 = 0x80008000 |
| 43 | const PF_PTE_OFF: i64 = 0x18 |
| 44 | const PF_PTE_VAL: i64 = 0x20000007 |
| 45 | const PF_SENT_PA: i64 = 0x80009000 |
| 46 | const PF_SENT_VAL: i64 = 0x5ECA1234 |
| 47 | const PF_VA_MAP: i64 = 0xC0009000 |
| 48 | const PF_VA_UNMAP: i64 = 0x00009000 |
| 49 | const PF_SATP_PPN: i64 = 0x80008 |
| 50 | const PF_SV39: i64 = 8 |
functions
| 52 | func pf_lui(rd: i64, imm20: i64) -> i64 { return ((imm20 & 0xFFFFF) << 12) | (rd << 7) | 0x37 } |
| 53 | func pf_addi(rd: i64, rs1: i64, imm: i64) -> i64 { return ((imm & 0xFFF) << 20) | (rs1 << 15) | (rd << 7) | 0x13 } |
| 54 | func pf_load(rd: i64, rs1: i64, f3: i64, imm: i64) -> i64 { return ((imm & 0xFFF) << 20) | (rs1 << 15) | (f3 << 12) | (rd << 7) | 0x03 } called by 1: pf_emit_image |
| 55 | func pf_store(rs2: i64, rs1: i64, f3: i64, imm: i64) -> i64 |
| 60 | func pf_branch(rs1: i64, rs2: i64, f3: i64, imm: i64) -> i64 called by 1: pf_emit_image |
| 67 | func pf_jal(rd: i64, imm: i64) -> i64 called by 1: pf_emit_image |
| 74 | func pf_slli(rd: i64, rs1: i64, shamt: i64) -> i64 { return ((shamt & 0x3f) << 20) | (rs1 << 15) | (1 << 12) | (rd << 7) | 0x13 } |
| 75 | func pf_srli(rd: i64, rs1: i64, shamt: i64) -> i64 { return ((shamt & 0x3f) << 20) | (rs1 << 15) | (5 << 12) | (rd << 7) | 0x13 } called by 1: pf_li32u |
| 76 | func pf_or(rd: i64, rs1: i64, rs2: i64) -> i64 { return (rs2 << 20) | (rs1 << 15) | (6 << 12) | (rd << 7) | 0x33 } called by 1: pf_emit_image |
| 77 | func pf_csrrw(rd: i64, csr: i64, rs1: i64) -> i64 { return ((csr & 0xfff) << 20) | (rs1 << 15) | (1 << 12) | (rd << 7) | 0x73 } called by 1: pf_emit_image |
| 78 | func pf_w32(buf: *u8, off: i64, w: i64) -> i64 { buf[off]=(w&0xff) as u8; buf[off+1]=((w>>8)&0xff) as u8; buf[off+2]=((w>>16)&0xff) as u8; buf[off+3]=((w>>24)&0xff) as u8; return off+4 } |
| 79 | func pf_li32(buf: *u8, off: i64, rd: i64, val: i64) -> i64 |
| 87 | func pf_li32u(buf: *u8, off: i64, rd: i64, val: i64) -> i64 |
| 93 | func pf_emit_str(buf: *u8, off: i64, s: *u8, n: i64) -> i64 |
| 99 | func pf_p(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 } |
| 100 | func pf_fn(fd: i64, v: i64) -> i64 { let bb: *u8=sys_mmap(28); var m: i64=v; if m<0{m=0-m}; let t: *u8=sys_mmap(28); var k: i64=0; if m==0{t[0]=48;k=1}; while m>0{t[k]=(48+(m%10)) as u8;m=m/10;k=k+1}; var i: i64=0; while i<k{bb[i]=t[k-1-i];i=i+1}; sys_write(fd,bb,k); return 0 } |
| 103 | func pf_emit_image(buf: *u8, fail_off: i64, halt_off: i64, handler_off: i64, pos_out: *i64, unmap_va: i64, cont_off: i64) -> i64 |
| 161 | func pf_parse_num(s: *u8) -> i64 called by 1: main |
| 174 | func main(argc: i64, argv: *i64) -> i64 |