code wiki / _hdl_build / _pagefault_gate.nx
_pagefault_gate.nx source
↩ module page · 90 lines · 5213 B
1// _pagefault_gate.nx -- gate for Sv39 PAGE-FAULT TRAPS (paging completion rung). NO mocks.
2//
3// (1) FAULT TRAPS -- emits the page-fault test (nx_pagefault_emit) + runs it: a mapped VA load
4// succeeds ("OK"), then an UNMAPPED VA load raises a load-page-fault trap, the CPU vectors to
5// mtvec, and the handler emits "PF". Serial must contain "OKPF" + a clean halt.
6// (2) CONTROL (fault is real) -- re-emit with the second VA MAPPED (no fault): control falls
7// through to "N" -> serial has "OK" but NOT "PF". Proves "PF" only appears via a genuine
8// page fault (the trap is fault-triggered, not always-emitted).
9//
10// Evidence -> knowledge/status/paging.log (PAGEFAULTGATE row). Sovereign. license_tier: ORIGINAL
11import "nx_syscalls.nx"
12
13const F_EMIT: *u8 = "_offc/nx_pagefault_emit.elf"
14const F_SOV: *u8 = "_offc/nx_boot_run_sov.elf"
15const F_BIN: *u8 = "runtime/_hdl_build/_pagefault_virt.bin"
16
17func g_p(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 }
18func g_fp(fd: i64, s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(fd,s,n); return 0 }
19func g_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 }
20
21func g_run(prog: *u8, a1: *u8, a2: *u8, outpath: *u8) -> i64 {
22 let pid: i64 = sys_fork()
23 if pid == 0 {
24 if outpath != (0 as *u8) { let ofd: i64 = sys_openat_wr(outpath, 0x1a4); if ofd >= 0 { sys_dup3(ofd, 1, 0); sys_dup3(ofd, 2, 0) } }
25 let argv: *i64 = sys_mmap(32) as *i64
26 argv[0] = prog as i64
27 var k: i64 = 1
28 if a1 != (0 as *u8) { argv[k] = a1 as i64; k = k + 1 }
29 if a2 != (0 as *u8) { argv[k] = a2 as i64; k = k + 1 }
30 argv[k] = 0
31 let envp: *i64 = sys_mmap(16) as *i64
32 envp[0] = "PATH=/usr/bin:/bin" as *u8 as i64; envp[1] = 0
33 sys_execve(prog, argv, envp)
34 sys_exit(127)
35 }
36 let st: *i64 = sys_mmap(16) as *i64
37 sys_wait4(pid, st, 0)
38 let sg: i64 = st[0] & 0x7f
39 if sg != 0 { return 128 + sg }
40 return (st[0] >> 8) & 0xff
41}
42func g_read(path: *u8, buf: *u8, cap: i64) -> i64 {
43 let fd: i64 = sys_openat_rd(path)
44 if fd < 0 { return 0 }
45 var n: i64 = 0
46 var go: i64 = 1
47 while go == 1 { let r: i64 = sys_read(fd, (buf as i64 + n) as *u8, cap - 1 - n); if r <= 0 { go = 0 } else { n = n + r } if n >= cap - 1 { go = 0 } }
48 sys_close(fd)
49 return n
50}
51func g_has(buf: *u8, n: i64, pat: *u8, pl: i64) -> i64 {
52 if pl <= 0 { return 0 }
53 var i: i64 = 0
54 while i + pl <= n { var k: i64=0; var hit: i64=1; while k<pl { if buf[i+k]!=pat[k]{hit=0;k=pl}else{k=k+1} } if hit==1 { return 1 } i=i+1 }
55 return 0
56}
57
58func main() -> i64 {
59 g_p("=== Sv39 page-fault trap gate (paging completion: unmapped VA -> trap -> handler) ===\n" as *u8)
60 let lfd: i64 = sys_openat_append("knowledge/status/paging.log" as *u8, 0x1a4)
61
62 // (1) fault traps: OKPF
63 g_run(F_EMIT, 0 as *u8, 0 as *u8, "/tmp/_pfg_emit.out" as *u8)
64 g_run(F_SOV, F_BIN, 0 as *u8, "/tmp/_pfg_main.txt" as *u8)
65 let mb: *u8 = sys_mmap(65536); let mn: i64 = g_read("/tmp/_pfg_main.txt" as *u8, mb, 65536)
66 var fault_ok: i64 = 0
67 if g_has(mb, mn, "OKPF" as *u8, 4) == 1 { if g_has(mb, mn, "BOOTSOV verdict=GREEN" as *u8, 21) == 1 { fault_ok = 1 } }
68
69 // (2) control: both VAs mapped -> no fault -> "OK" present, "PF" absent
70 g_run(F_EMIT, "0xC0009000" as *u8, "/tmp/_pfg_ctrl.bin" as *u8, "/tmp/_pfg_ctrl_emit.out" as *u8)
71 g_run(F_SOV, "/tmp/_pfg_ctrl.bin" as *u8, 0 as *u8, "/tmp/_pfg_ctrl.txt" as *u8)
72 let cb: *u8 = sys_mmap(65536); let cn: i64 = g_read("/tmp/_pfg_ctrl.txt" as *u8, cb, 65536)
73 var control_ok: i64 = 0
74 if g_has(cb, cn, "OK" as *u8, 2) == 1 { if g_has(cb, cn, "PF" as *u8, 2) == 0 { control_ok = 1 } }
75
76 g_p(" fault_traps=" as *u8); if fault_ok==1 { g_p("GREEN(OKPF: mapped-load OK, unmapped-load -> trap -> handler PF)" as *u8) } else { g_p("RED" as *u8) }
77 g_p(" control_no_false_pf=" as *u8); g_fn(1, control_ok); g_p("\n" as *u8)
78
79 var pass: i64 = 0
80 if fault_ok == 1 { if control_ok == 1 { pass = 1 } }
81 if pass == 1 {
82 g_p("PAGEFAULTGATE verdict=GREEN (Sv39 page-fault traps: an unmapped VA load raises a load-page-fault, the CPU aborts the instruction + vectors to mtvec, the handler runs [OKPF]; the both-mapped control emits no PF = the trap is genuinely fault-triggered. probe=page-fault-trap)\n" as *u8)
83 if lfd >= 0 { g_fp(lfd, "PAGEFAULTGATE verdict=GREEN keystone=sv39-page-fault-trap probe=page-fault-trap mcause=load-page-fault mtvec-dispatch=handler-PF control=no-false-pf epoch=" as *u8); g_fn(lfd, sys_now_realtime_sec()); g_fp(lfd, "\n" as *u8); sys_close(lfd) }
84 sys_exit(0); return 0
85 }
86 g_p("PAGEFAULTGATE verdict=RED (fault/control not all green)\n" as *u8)
87 if lfd >= 0 { g_fp(lfd, "PAGEFAULTGATE verdict=RED fault=" as *u8); g_fn(lfd, fault_ok); g_fp(lfd, " control=" as *u8); g_fn(lfd, control_ok); g_fp(lfd, "\n" as *u8); sys_close(lfd) }
88 sys_exit(1)
89 return 1
90}