code wiki / _hdl_build / _ad_gate.nx

_ad_gate.nx source

↩ module page · 88 lines · 4621 B

1// _ad_gate.nx -- LOCK for Sv39 ACCESSED/DIRTY bit tracking (X-PAGE-AD-001). NO mocks. 2// Proves the hardware sets the leaf PTE's A bit on any access + D bit on a store (the demand-paging 3// foundation). Drives nx_ad_emit (the M-mode kernel reads the leaf PTE physically + checks A/D): 4// T1 access : S-mode load (sets A) + store (sets D) -> kernel sees A=1 && D=1 -> "ADOK" 5// T2 control : NO access -> A/D stay 0 -> "ADN" (proves the bits are SET BY THE ACCESS, not pre-set) 6// T1 vs T2 differ ONLY in whether the page is touched. GREEN only if T1 && T2. 7// Evidence -> knowledge/status/paging.log (ADGATE). license_tier: ORIGINAL 8import "nx_syscalls.nx" 9 10const AD_EMIT: *u8 = "_offc/nx_ad_emit.elf" 11const AD_SOV: *u8 = "_offc/nx_boot_run_sov.elf" 12 13func 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 } 14func 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 } 15func 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 } 16 17func g_run(prog: *u8, a1: *u8, a2: *u8, outpath: *u8) -> i64 { 18 let pid: i64 = sys_fork() 19 if pid == 0 { 20 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) } } 21 let argv: *i64 = sys_mmap(32) as *i64 22 argv[0] = prog as i64 23 var k: i64 = 1 24 if a1 != (0 as *u8) { argv[k] = a1 as i64; k = k + 1 } 25 if a2 != (0 as *u8) { argv[k] = a2 as i64; k = k + 1 } 26 argv[k] = 0 27 let envp: *i64 = sys_mmap(16) as *i64 28 envp[0] = "PATH=/usr/bin:/bin" as *u8 as i64; envp[1] = 0 29 sys_execve(prog, argv, envp) 30 sys_exit(127) 31 } 32 let st: *i64 = sys_mmap(16) as *i64 33 sys_wait4(pid, st, 0) 34 return (st[0] >> 8) & 0xff 35} 36func g_read(path: *u8, buf: *u8, cap: i64) -> i64 { 37 let fd: i64 = sys_openat_rd(path) 38 if fd < 0 { return 0 } 39 var n: i64 = 0 40 var go: i64 = 1 41 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 } } 42 sys_close(fd) 43 return n 44} 45func g_has(buf: *u8, n: i64, pat: *u8, pl: i64) -> i64 { 46 if pl <= 0 { return 0 } 47 var i: i64 = 0 48 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 } 49 return 0 50} 51func g_case(acc: *u8, binp: *u8, txt: *u8, buf: *u8) -> i64 { 52 g_run(AD_EMIT, acc, binp, "/tmp/_ad_emit.out" as *u8) 53 g_run(AD_SOV, binp, 0 as *u8, txt) 54 return g_read(txt, buf, 65536) 55} 56func g_row(name: *u8, pass: i64) -> i64 { g_p(" " as *u8); g_p(name); if pass==1 { g_p(" PASS\n" as *u8) } else { g_p(" FAIL\n" as *u8) } return 0 } 57 58func main() -> i64 { 59 let b: *u8 = sys_mmap(65536) 60 61 // T1 access: load sets A, store sets D -> kernel confirms A=1 && D=1 -> ADOK 62 var n: i64 = g_case("1" as *u8, "/tmp/_ad_acc.bin" as *u8, "/tmp/_ad_acc.txt" as *u8, b) 63 var t1: i64 = 0 64 if g_has(b, n, "ADOK" as *u8, 4) == 1 { if g_has(b, n, "BOOTSOV verdict=GREEN" as *u8, 21) == 1 { t1 = 1 } } 65 66 // T2 control: no access -> A/D stay 0 -> ADN, no ADOK 67 n = g_case("0" as *u8, "/tmp/_ad_no.bin" as *u8, "/tmp/_ad_no.txt" as *u8, b) 68 var t2: i64 = 0 69 if g_has(b, n, "ADOK" as *u8, 4) == 0 { if g_has(b, n, "ADN" as *u8, 3) == 1 { t2 = 1 } } 70 71 var ok: i64 = 0 72 if t1 == 1 { if t2 == 1 { ok = 1 } } 73 74 g_p("AD gate (Sv39 Accessed/Dirty bit tracking)\n" as *u8) 75 g_row("T1 access->A=1,D=1 (ADOK) " as *u8, t1) 76 g_row("T2 no-access->A/D=0 (ADN) " as *u8, t2) 77 78 let lf: i64 = sys_openat_append("knowledge/status/paging.log" as *u8, 0x1a4) 79 if ok == 1 { 80 g_p("ADGATE verdict=GREEN keystone=sv39-accessed-dirty-bits probe=ad A-set-on-access D-set-on-store control=no-access->A/D-0\n" as *u8) 81 if lf >= 0 { g_fp(lf, "ADGATE verdict=GREEN keystone=sv39-accessed-dirty-bits probe=ad A-on-access+D-on-store + no-access-control epoch=" as *u8); g_fn(lf, sys_now_realtime_sec()); g_fp(lf, "\n" as *u8); sys_close(lf) } 82 sys_exit(0); return 0 83 } 84 g_p("ADGATE verdict=RED (t1=" as *u8); g_fn(1, t1); g_p(" t2=" as *u8); g_fn(1, t2); g_p(")\n" as *u8) 85 if lf >= 0 { g_fp(lf, "ADGATE verdict=RED t1=" as *u8); g_fn(lf, t1); g_fp(lf, " t2=" as *u8); g_fn(lf, t2); g_fp(lf, "\n" as *u8); sys_close(lf) } 86 sys_exit(1) 87 return 1 88}