code wiki / _hdl_build / _paging_gate.nx
_paging_gate.nx source
↩ module page · 91 lines · 5287 B
1// _paging_gate.nx -- gate for CPU-DATAPATH Sv39 PAGING (virtual-memory-paging-mmu integration). NO mocks.
2//
3// (1) TRANSPARENT TRANSLATION -- emits the paging test (nx_paging_emit) + runs it on the sovereign
4// emu: after csrrw satp (MODE=Sv39), a NORMAL lw from VIRTUAL 0xC0009000 is transparently
5// translated by the CPU through the page table to PHYSICAL 0x80009000 and reads the sentinel
6// -> "VOK". VA (0xC0009000) != PA (0x80009000), so this is a real translation, not identity.
7// (2) TAMPER bad-PTE -- re-emit with the leaf PTE's Valid bit cleared (0x20000006): the VA walk
8// fails -> the load no longer reaches the sentinel -> "VOK" drops -> the CPU datapath genuinely
9// walks + validates the page table on every access (translation is not a rubber stamp).
10//
11// Evidence -> knowledge/status/paging.log (PAGINGGATE row). Sovereign. license_tier: ORIGINAL
12import "nx_syscalls.nx"
13
14const P_EMIT: *u8 = "_offc/nx_paging_emit.elf"
15const P_SOV: *u8 = "_offc/nx_boot_run_sov.elf"
16const P_BIN: *u8 = "runtime/_hdl_build/_paging_virt.bin"
17
18func 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 }
19func 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 }
20func 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 }
21
22func g_run(prog: *u8, a1: *u8, a2: *u8, outpath: *u8) -> i64 {
23 let pid: i64 = sys_fork()
24 if pid == 0 {
25 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) } }
26 let argv: *i64 = sys_mmap(32) as *i64
27 argv[0] = prog as i64
28 var k: i64 = 1
29 if a1 != (0 as *u8) { argv[k] = a1 as i64; k = k + 1 }
30 if a2 != (0 as *u8) { argv[k] = a2 as i64; k = k + 1 }
31 argv[k] = 0
32 let envp: *i64 = sys_mmap(16) as *i64
33 envp[0] = "PATH=/usr/bin:/bin" as *u8 as i64; envp[1] = 0
34 sys_execve(prog, argv, envp)
35 sys_exit(127)
36 }
37 let st: *i64 = sys_mmap(16) as *i64
38 sys_wait4(pid, st, 0)
39 let sg: i64 = st[0] & 0x7f
40 if sg != 0 { return 128 + sg }
41 return (st[0] >> 8) & 0xff
42}
43func g_read(path: *u8, buf: *u8, cap: i64) -> i64 {
44 let fd: i64 = sys_openat_rd(path)
45 if fd < 0 { return 0 }
46 var n: i64 = 0
47 var go: i64 = 1
48 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 } }
49 sys_close(fd)
50 return n
51}
52func g_has(buf: *u8, n: i64, pat: *u8, pl: i64) -> i64 {
53 if pl <= 0 { return 0 }
54 var i: i64 = 0
55 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 }
56 return 0
57}
58
59func main() -> i64 {
60 g_p("=== CPU-datapath Sv39 paging gate (transparent VA load translation) ===\n" as *u8)
61 let lfd: i64 = sys_openat_append("knowledge/status/paging.log" as *u8, 0x1a4)
62
63 // (1) transparent translation
64 g_run(P_EMIT, 0 as *u8, 0 as *u8, "/tmp/_pg_emit.out" as *u8)
65 g_run(P_SOV, P_BIN, 0 as *u8, "/tmp/_pg_main.txt" as *u8)
66 let mb: *u8 = sys_mmap(65536); let mn: i64 = g_read("/tmp/_pg_main.txt" as *u8, mb, 65536)
67 var xlate_ok: i64 = 0
68 if g_has(mb, mn, "VOK" as *u8, 3) == 1 { if g_has(mb, mn, "BOOTSOV verdict=GREEN" as *u8, 21) == 1 { xlate_ok = 1 } }
69
70 // (2) tamper bad-PTE (V=0): VOK must drop
71 g_run(P_EMIT, "0x20000006" as *u8, "/tmp/_pg_bad.bin" as *u8, "/tmp/_pg_bad_emit.out" as *u8)
72 g_run(P_SOV, "/tmp/_pg_bad.bin" as *u8, 0 as *u8, "/tmp/_pg_bad.txt" as *u8)
73 let tb: *u8 = sys_mmap(65536); let tn: i64 = g_read("/tmp/_pg_bad.txt" as *u8, tb, 65536)
74 var tamper: i64 = 0
75 if g_has(tb, tn, "VOK" as *u8, 3) == 0 { tamper = 1 }
76
77 g_p(" transparent_translation=" as *u8); if xlate_ok==1 { g_p("GREEN(VA 0xC0009000 -> PA 0x80009000 via lw)" as *u8) } else { g_p("RED" as *u8) }
78 g_p(" tamper_badpte=" as *u8); g_fn(1, tamper); g_p("\n" as *u8)
79
80 var pass: i64 = 0
81 if xlate_ok == 1 { if tamper == 1 { pass = 1 } }
82 if pass == 1 {
83 g_p("PAGINGGATE verdict=GREEN (CPU-datapath Sv39 paging: after csrrw satp the CPU TRANSPARENTLY translated a normal lw from VA 0xC0009000 -> PA 0x80009000 [VOK; VA!=PA = real translation]; a bad PTE [V=0] drops VOK = the datapath walks+validates the page table on every access. probe=cpu-datapath-paging)\n" as *u8)
84 if lfd >= 0 { g_fp(lfd, "PAGINGGATE verdict=GREEN keystone=cpu-datapath-sv39-paging probe=cpu-datapath-paging satp-csr=yes transparent-VA-load=VOK va=0xC0009000 pa=0x80009000 tamper=rejected(badpte) epoch=" as *u8); g_fn(lfd, sys_now_realtime_sec()); g_fp(lfd, "\n" as *u8); sys_close(lfd) }
85 sys_exit(0); return 0
86 }
87 g_p("PAGINGGATE verdict=RED (translation/tamper not all green)\n" as *u8)
88 if lfd >= 0 { g_fp(lfd, "PAGINGGATE verdict=RED xlate=" as *u8); g_fn(lfd, xlate_ok); g_fp(lfd, " tamper=" as *u8); g_fn(lfd, tamper); g_fp(lfd, "\n" as *u8); sys_close(lfd) }
89 sys_exit(1)
90 return 1
91}