code wiki / _hdl_build / _ifetch_gate.nx
_ifetch_gate.nx source
↩ module page · 92 lines · 5687 B
1// _ifetch_gate.nx -- gate for INSTRUCTION-FETCH translation (virtual-memory-paging-mmu completion). NO mocks.
2//
3// (1) FETCH TRANSLATION -- emits the ifetch test (nx_ifetch_emit) + runs it on the sovereign emu:
4// after csrrw satp (MODE=Sv39), mret lands the PC at a VIRTUAL address 0xC0000000+cont_off; the
5// CPU FETCHES the continuation through the page table (VA 0xC00000xx -> PA 0x800000xx) and runs
6// it in S-mode -> "IOK". VA != PA, so "IOK" proves the fetch itself was translated.
7// (2) TAMPER bad code-PTE -- re-emit with the code page's Valid bit cleared (0x20000006): the mret
8// targets an unmapped virtual PC -> the FETCH walk faults -> instruction-page-fault (mcause=12)
9// -> the M-mode handler emits "IPF". "IPF" present + "IOK" gone = the fetch genuinely walks +
10// validates the page table on every instruction (translation is not a rubber stamp).
11//
12// Evidence -> knowledge/status/paging.log (IFETCHGATE row). Sovereign. license_tier: ORIGINAL
13import "nx_syscalls.nx"
14
15const IF_EMIT: *u8 = "_offc/nx_ifetch_emit.elf"
16const IF_SOV: *u8 = "_offc/nx_boot_run_sov.elf"
17const IF_BIN: *u8 = "runtime/_hdl_build/_ifetch_virt.bin"
18
19func 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 }
20func 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 }
21func 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 }
22
23func g_run(prog: *u8, a1: *u8, a2: *u8, outpath: *u8) -> i64 {
24 let pid: i64 = sys_fork()
25 if pid == 0 {
26 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) } }
27 let argv: *i64 = sys_mmap(32) as *i64
28 argv[0] = prog as i64
29 var k: i64 = 1
30 if a1 != (0 as *u8) { argv[k] = a1 as i64; k = k + 1 }
31 if a2 != (0 as *u8) { argv[k] = a2 as i64; k = k + 1 }
32 argv[k] = 0
33 let envp: *i64 = sys_mmap(16) as *i64
34 envp[0] = "PATH=/usr/bin:/bin" as *u8 as i64; envp[1] = 0
35 sys_execve(prog, argv, envp)
36 sys_exit(127)
37 }
38 let st: *i64 = sys_mmap(16) as *i64
39 sys_wait4(pid, st, 0)
40 let sg: i64 = st[0] & 0x7f
41 if sg != 0 { return 128 + sg }
42 return (st[0] >> 8) & 0xff
43}
44func g_read(path: *u8, buf: *u8, cap: i64) -> i64 {
45 let fd: i64 = sys_openat_rd(path)
46 if fd < 0 { return 0 }
47 var n: i64 = 0
48 var go: i64 = 1
49 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 } }
50 sys_close(fd)
51 return n
52}
53func g_has(buf: *u8, n: i64, pat: *u8, pl: i64) -> i64 {
54 if pl <= 0 { return 0 }
55 var i: i64 = 0
56 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 }
57 return 0
58}
59
60func main() -> i64 {
61 g_p("=== CPU-datapath Sv39 instruction-FETCH gate (virtual PC translation) ===\n" as *u8)
62 let lfd: i64 = sys_openat_append("knowledge/status/paging.log" as *u8, 0x1a4)
63
64 // (1) fetch translation: code runs from a virtual PC -> IOK
65 g_run(IF_EMIT, 0 as *u8, 0 as *u8, "/tmp/_if_emit.out" as *u8)
66 g_run(IF_SOV, IF_BIN, 0 as *u8, "/tmp/_if_main.txt" as *u8)
67 let mb: *u8 = sys_mmap(65536); let mn: i64 = g_read("/tmp/_if_main.txt" as *u8, mb, 65536)
68 var fetch_ok: i64 = 0
69 if g_has(mb, mn, "IOK" as *u8, 3) == 1 { if g_has(mb, mn, "BOOTSOV verdict=GREEN" as *u8, 21) == 1 { fetch_ok = 1 } }
70
71 // (2) tamper bad code-PTE (V=0): IOK must drop AND the fetch fault must raise mcause=12 -> IPF
72 g_run(IF_EMIT, "0x20000006" as *u8, "/tmp/_if_bad.bin" as *u8, "/tmp/_if_bad_emit.out" as *u8)
73 g_run(IF_SOV, "/tmp/_if_bad.bin" as *u8, 0 as *u8, "/tmp/_if_bad.txt" as *u8)
74 let tb: *u8 = sys_mmap(65536); let tn: i64 = g_read("/tmp/_if_bad.txt" as *u8, tb, 65536)
75 var tamper: i64 = 0
76 if g_has(tb, tn, "IOK" as *u8, 3) == 0 { if g_has(tb, tn, "IPF" as *u8, 3) == 1 { tamper = 1 } }
77
78 g_p(" fetch_translation=" as *u8); if fetch_ok==1 { g_p("GREEN(VA 0xC0000000+ -> PA 0x80000000+ via fetch -> IOK)" as *u8) } else { g_p("RED" as *u8) }
79 g_p(" tamper_instr_page_fault=" as *u8); g_fn(1, tamper); g_p("\n" as *u8)
80
81 var pass: i64 = 0
82 if fetch_ok == 1 { if tamper == 1 { pass = 1 } }
83 if pass == 1 {
84 g_p("IFETCHGATE verdict=GREEN (CPU-datapath Sv39 instruction fetch: after csrrw satp the CPU FETCHED its continuation through a VIRTUAL PC [VA 0xC0000000+ -> PA 0x80000000+ -> IOK; VA!=PA = real fetch translation]; a bad code PTE [V=0] raises an instruction-page-fault [mcause=12 -> IPF] and drops IOK = the datapath walks+validates the page table on every fetch. probe=ifetch-translation)\n" as *u8)
85 if lfd >= 0 { g_fp(lfd, "IFETCHGATE verdict=GREEN keystone=instruction-fetch-translation probe=ifetch-translation satp-csr=yes virtual-PC-fetch=IOK va=0xC0000000 pa=0x80000000 fetch-fault=instruction-page-fault(mcause=12) tamper=rejected(badpte->IPF) epoch=" as *u8); g_fn(lfd, sys_now_realtime_sec()); g_fp(lfd, "\n" as *u8); sys_close(lfd) }
86 sys_exit(0); return 0
87 }
88 g_p("IFETCHGATE verdict=RED (fetch-translation/tamper not all green)\n" as *u8)
89 if lfd >= 0 { g_fp(lfd, "IFETCHGATE verdict=RED fetch=" as *u8); g_fn(lfd, fetch_ok); g_fp(lfd, " tamper=" as *u8); g_fn(lfd, tamper); g_fp(lfd, "\n" as *u8); sys_close(lfd) }
90 sys_exit(1)
91 return 1
92}