code wiki / _hdl_build / _driver_gate.nx

_driver_gate.nx source

↩ module page · 113 lines · 6067 B

1// _driver_gate.nx -- gate for DRIVER-FROM-SPEC (X-DRV-W1). 2// NO mocks: runs the REAL nx_driver_emit to GENERATE a driver per vendor spec, has nx_cc COMPILE + 3// RUN each generated driver against the REAL hardware map (HWMAP / X-DRV-W0), reads each driver's 4// bound-device count, and asserts it equals an INDEPENDENT sovereign count of that vendor in the 5// map. Three vendors -> distinct correct counts proves the bind logic is SYNTHESIZED FROM THE SPEC 6// (a new device class = a new spec -> a new driver), not a fixed template. Evidence -> 7// knowledge/status/driver_spec.log. license_tier: ORIGINAL 8import "nx_syscalls.nx" 9 10func 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 } 11func 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 } 12func 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 } 13 14func g_run(prog: *u8, a1: *u8, a2: *u8, outpath: *u8) -> i64 { 15 let pid: i64 = sys_fork() 16 if pid == 0 { 17 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) } } 18 else { let dn: i64 = sys_openat_wr("/dev/null" as *u8, 0x1a4); if dn >= 0 { sys_dup3(dn, 1, 0) } } 19 let argv: *i64 = sys_mmap(32) as *i64 20 argv[0] = prog as i64 21 var k: i64 = 1 22 if a1 != (0 as *u8) { argv[k] = a1 as i64; k = k + 1 } 23 if a2 != (0 as *u8) { argv[k] = a2 as i64; k = k + 1 } 24 argv[k] = 0 25 let envp: *i64 = sys_mmap(16) as *i64 26 envp[0] = "PATH=/usr/bin:/bin" as *u8 as i64; envp[1] = 0 27 sys_execve(prog, argv, envp) 28 sys_exit(127) 29 } 30 let st: *i64 = sys_mmap(16) as *i64 31 sys_wait4(pid, st, 0) 32 return st[0] 33} 34 35func g_read(path: *u8, buf: *u8, cap: i64) -> i64 { 36 let fd: i64 = sys_openat_rd(path) 37 if fd < 0 { return 0 } 38 var n: i64 = 0 39 var go: i64 = 1 40 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 } } 41 sys_close(fd) 42 return n 43} 44 45// independent sovereign count of "\t<vendor>\t" in the hwmap buffer. 46func g_count(buf: *u8, n: i64, vendor: *u8) -> i64 { 47 // build pattern "\t<vendor>\t" 48 let pat: *u8 = sys_mmap(64) 49 var pl: i64 = 0 50 pat[pl] = 9 as u8; pl = pl + 1 51 var vi: i64 = 0 52 while vendor[vi] != (0 as u8) { pat[pl] = vendor[vi]; pl = pl + 1; vi = vi + 1 } 53 pat[pl] = 9 as u8; pl = pl + 1 54 var cnt: i64 = 0 55 var i: i64 = 0 56 while i + pl <= n { 57 var k: i64 = 0; var hit: i64 = 1 58 while k < pl { if buf[i+k] != pat[k] { hit = 0; k = pl } else { k = k + 1 } } 59 if hit == 1 { cnt = cnt + 1 } 60 i = i + 1 61 } 62 return cnt 63} 64 65// generate+compile+run a driver for vendor; return its emitted bound-device count byte. 66func g_driver_count(base: *u8, vendor: *u8, genout: *u8, runout: *u8) -> i64 { 67 let gst: i64 = g_run("/tmp/nx_driver_emit.sov.elf" as *u8, base, vendor, genout) 68 if gst != 0 { return 0 - 1 } 69 let rst: i64 = g_run("_offc/nx_sov_build_run.elf" as *u8, base, 0 as *u8, runout) 70 if rst != 0 { return 0 - 1 } 71 let rb: *u8 = sys_mmap(4096) 72 let rn: i64 = g_read(runout, rb, 4096) 73 if rn <= 0 { return 0 - 1 } 74 return rb[0] as i64 75} 76 77func main() -> i64 { 78 g_p("=== driver-from-spec gate (vendor spec -> emitted driver binds real HWMAP devices) ===\n" as *u8) 79 let lfd: i64 = sys_openat_append("knowledge/status/driver_spec.log" as *u8, 0x1a4) 80 81 let hw: *u8 = sys_mmap(262144) 82 let hn: i64 = g_read("knowledge/registry/hwmap.tsv" as *u8, hw, 262144) 83 84 // three device-class specs (vendors present/absent in the real map) 85 let d1: i64 = g_driver_count("_drv_v1414" as *u8, "0x1414" as *u8, "/tmp/_drv_g1.out" as *u8, "/tmp/_drv_r1.txt" as *u8) 86 let i1: i64 = g_count(hw, hn, "0x1414" as *u8) 87 let d2: i64 = g_driver_count("_drv_v1af4" as *u8, "0x1af4" as *u8, "/tmp/_drv_g2.out" as *u8, "/tmp/_drv_r2.txt" as *u8) 88 let i2: i64 = g_count(hw, hn, "0x1af4" as *u8) 89 let d3: i64 = g_driver_count("_drv_vdead" as *u8, "0xdead" as *u8, "/tmp/_drv_g3.out" as *u8, "/tmp/_drv_r3.txt" as *u8) 90 let i3: i64 = g_count(hw, hn, "0xdead" as *u8) 91 92 g_p(" vendor 0x1414: driver-bound="as *u8); g_fn(1,d1); g_p(" independent="as *u8); g_fn(1,i1) 93 g_p(" | 0x1af4: driver="as *u8); g_fn(1,d2); g_p(" indep="as *u8); g_fn(1,i2) 94 g_p(" | 0xdead: driver="as *u8); g_fn(1,d3); g_p(" indep="as *u8); g_fn(1,i3); g_p("\n"as *u8) 95 96 var match_ok: i64 = 0 97 if d1 == i1 { if d2 == i2 { if d3 == i3 { match_ok = 1 } } } 98 // distinct: a new spec binds a different device set (else it's a fixed template, not spec-driven) 99 var distinct: i64 = 0 100 if d1 != d2 { if d3 == 0 { if d1 > 0 { distinct = 1 } } } 101 102 var pass: i64 = 0 103 if match_ok == 1 { if distinct == 1 { pass = 1 } } 104 if pass == 1 { 105 g_p("DRIVERSPECGATE verdict=GREEN (vendor spec -> nx_driver_emit -> nx_cc -> driver binds EXACTLY the real HWMAP devices for its class; per-spec counts match independent count + are DISTINCT = bind logic synthesized from spec)\n" as *u8) 106 if lfd >= 0 { g_fp(lfd, "DRIVERSPECGATE verdict=GREEN keystone=driver-from-spec composes=HWMAP+emitter-of-emitters v1414=" as *u8); g_fn(lfd, d1); g_fp(lfd, " v1af4=" as *u8); g_fn(lfd, d2); g_fp(lfd, " vdead=" as *u8); g_fn(lfd, d3); g_fp(lfd, " match-independent=yes epoch=" as *u8); g_fn(lfd, sys_now_realtime_sec()); g_fp(lfd, "\n" as *u8); sys_close(lfd) } 107 sys_exit(0); return 0 108 } 109 g_p("DRIVERSPECGATE verdict=RED (match/distinct not both green)\n" as *u8) 110 if lfd >= 0 { g_fp(lfd, "DRIVERSPECGATE verdict=RED match_ok=" as *u8); g_fn(lfd, match_ok); g_fp(lfd, " distinct=" as *u8); g_fn(lfd, distinct); g_fp(lfd, "\n" as *u8); sys_close(lfd) } 111 sys_exit(1) 112 return 1 113}