code wiki / _hdl_build / _drv_bind_gate.nx
_drv_bind_gate.nx source
↩ module page · 133 lines · 7876 B
1// _drv_bind_gate.nx -- the gate for AUTONOMOUS DRIVER BRING-UP (X-DRV-W2). NO mocks.
2//
3// (1) AUTONOMY -- runs the REAL nx_drv_bind over the REAL emu device manifest + driver registry:
4// it must PROBE both devices, look each DeviceID up, emit the bound driver, run it on the
5// SOVEREIGN rv64 emu, and report probed=2 bound_working=2 unbound=0 failed=0 -- both real
6// devices brought up with NO per-device code.
7// (2) DISCRIMINATION -- emit the BLK driver spec BOUND to the NET device base (0x10002000) and
8// run it: the blk driver's identity verify (DeviceID==2) rejects the net device (DeviceID==1),
9// so its golden NEVER appears. Proves the registry's DeviceID->spec mapping is load-bearing:
10// you cannot bind any driver to any device (a fixed template would wrongly "work").
11// (3) NO-FALSE-BIND -- run nx_drv_bind over a scratch manifest with an UNKNOWN DeviceID (99):
12// the registry lookup misses, so the device is reported UNBOUND, never falsely bound. Proves
13// the binder refuses iron it has no driver for (honest, no fabricated bring-up).
14//
15// Evidence -> knowledge/status/driver_bind.log (DRVBINDGATE row; the queue row's ||MARK= reads
16// it). Sovereign orchestration (fork/dup3/execve/wait4), no gcc/.sh. license_tier: ORIGINAL
17import "nx_syscalls.nx"
18
19const G_BIND_ELF: *u8 = "_offc/nx_drv_bind.elf"
20const G_EMIT_ELF: *u8 = "_offc/nx_drv_proto_emit.elf"
21const G_SOV_ELF: *u8 = "_offc/nx_boot_run_sov.elf"
22
23func 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 }
24func 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 }
25func 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 }
26
27func g_run(prog: *u8, a1: *u8, a2: *u8, a3: *u8, outpath: *u8) -> i64 {
28 let pid: i64 = sys_fork()
29 if pid == 0 {
30 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) } }
31 let argv: *i64 = sys_mmap(64) as *i64
32 argv[0] = prog as i64
33 var k: i64 = 1
34 if a1 != (0 as *u8) { argv[k] = a1 as i64; k = k + 1 }
35 if a2 != (0 as *u8) { argv[k] = a2 as i64; k = k + 1 }
36 if a3 != (0 as *u8) { argv[k] = a3 as i64; k = k + 1 }
37 argv[k] = 0
38 let envp: *i64 = sys_mmap(16) as *i64
39 envp[0] = "PATH=/usr/bin:/bin" as *u8 as i64; envp[1] = 0
40 sys_execve(prog, argv, envp)
41 sys_exit(127)
42 }
43 let st: *i64 = sys_mmap(16) as *i64
44 sys_wait4(pid, st, 0)
45 let sig: i64 = st[0] & 0x7f
46 if sig != 0 { return 128 + sig }
47 return (st[0] >> 8) & 0xff
48}
49
50func g_read(path: *u8, buf: *u8, cap: i64) -> i64 {
51 let fd: i64 = sys_openat_rd(path)
52 if fd < 0 { return 0 }
53 var n: i64 = 0
54 var go: i64 = 1
55 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 } }
56 sys_close(fd)
57 return n
58}
59
60func g_has(buf: *u8, n: i64, pat: *u8, pl: i64) -> i64 {
61 if pl <= 0 { return 0 }
62 var i: i64 = 0
63 while i + pl <= n {
64 var k: i64 = 0; var hit: i64 = 1
65 while k < pl { if buf[i+k] != pat[k] { hit = 0; k = pl } else { k = k + 1 } }
66 if hit == 1 { return 1 }
67 i = i + 1
68 }
69 return 0
70}
71func g_strlen(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} return n }
72func g_hasz(buf: *u8, n: i64, pat: *u8) -> i64 { return g_has(buf, n, pat, g_strlen(pat)) }
73
74func main() -> i64 {
75 g_p("=== driver-registry autobind gate (X-DRV-W2: probe -> registry lookup -> emit-bound -> run) ===\n" as *u8)
76 let lfd: i64 = sys_openat_append("knowledge/status/driver_bind.log" as *u8, 0x1a4)
77
78 // (1) AUTONOMY: the real bring-up over the real manifest + registry.
79 let bst: i64 = g_run(G_BIND_ELF, 0 as *u8, 0 as *u8, 0 as *u8, "/tmp/_bindgate_run.txt" as *u8)
80 let rb: *u8 = sys_mmap(65536)
81 let rn: i64 = g_read("/tmp/_bindgate_run.txt" as *u8, rb, 65536)
82 var autonomy: i64 = 0
83 if bst == 0 { if g_hasz(rb, rn, "bound_working=2" as *u8) == 1 { if g_hasz(rb, rn, "unbound=0" as *u8) == 1 { if g_hasz(rb, rn, "failed=0" as *u8) == 1 { autonomy = 1 } } } }
84
85 // (2) DISCRIMINATION: blk driver spec BOUND to the NET base must NOT work (DeviceID verify rejects).
86 let dest: i64 = g_run(G_EMIT_ELF, "knowledge/specs/drv_proto_blk_virt.spec" as *u8, "0x10002000" as *u8, "/tmp/_disc.bin" as *u8, "/tmp/_disc_emit.out" as *u8)
87 let dgold: *u8 = sys_mmap(2048)
88 let dgn: i64 = g_read("/tmp/_disc.bin.gold" as *u8, dgold, 2048)
89 dgold[dgn] = 0 as u8
90 var discrim: i64 = 0
91 if dest == 0 { if dgn > 0 {
92 g_run(G_SOV_ELF, "/tmp/_disc.bin" as *u8, 0 as *u8, 0 as *u8, "/tmp/_disc_serial.txt" as *u8)
93 let db: *u8 = sys_mmap(65536)
94 let dn: i64 = g_read("/tmp/_disc_serial.txt" as *u8, db, 65536)
95 if g_has(db, dn, dgold, dgn) == 0 { discrim = 1 } // blk golden absent on the net device
96 } }
97
98 // (3) NO-FALSE-BIND: a scratch manifest with an UNKNOWN DeviceID (99) -> UNBOUND, never bound.
99 let tfd: i64 = sys_openat_wr("/tmp/_bindgate_unknown.tsv" as *u8, 0x1a4)
100 if tfd >= 0 { g_fp(tfd, "# scratch manifest: one device with a DeviceID absent from the registry\n0x10003000\t99\tunknown-iron\n" as *u8); sys_close(tfd) }
101 let ust: i64 = g_run(G_BIND_ELF, "/tmp/_bindgate_unknown.tsv" as *u8, 0 as *u8, 0 as *u8, "/tmp/_bindgate_unknown.txt" as *u8)
102 let ub: *u8 = sys_mmap(65536)
103 let un: i64 = g_read("/tmp/_bindgate_unknown.txt" as *u8, ub, 65536)
104 var nofalse: i64 = 0
105 if ust == 0 { if g_hasz(ub, un, "UNBOUND" as *u8) == 1 { if g_hasz(ub, un, "bound_working=0" as *u8) == 1 { nofalse = 1 } } }
106
107 g_p(" autonomy=" as *u8)
108 if autonomy == 1 { g_p("GREEN(probed=2 bound_working=2 unbound=0 failed=0, both devices brought up, no per-device code)" as *u8) } else { g_p("RED" as *u8) }
109 g_p(" discrimination=" as *u8)
110 if discrim == 1 { g_p("yes(blk driver @net base rejected by DeviceID verify, golden absent)" as *u8) } else { g_p("no" as *u8) }
111 g_p(" no_false_bind=" as *u8)
112 if nofalse == 1 { g_p("yes(unknown DeviceID=99 -> UNBOUND)\n" as *u8) } else { g_p("no\n" as *u8) }
113
114 var pass: i64 = 0
115 if autonomy == 1 { if discrim == 1 { if nofalse == 1 { pass = 1 } } }
116
117 if pass == 1 {
118 g_p("DRVBINDGATE verdict=GREEN (driver-registry-autobind: nx_drv_bind autonomously PROBED the emu device manifest, looked each DeviceID up in driver_registry.tsv, EMITTED each driver bound to the probed base via nx_drv_proto_emit, and RAN it WORKING on the sovereign emu -- both devices, NO per-device code; the DeviceID->spec mapping is load-bearing [blk driver rejected at the net base]; unknown iron is refused [UNBOUND, no false bind] -- bring up ANY iron, composes HWMAP-probe + driver-from-spec)\n" as *u8)
119 if lfd >= 0 {
120 g_fp(lfd, "DRVBINDGATE verdict=GREEN keystone=driver-registry-autobind composes=HWMAP-probe+driver-from-spec autonomy=probed2-boundworking2 discrimination=blk-rejected-at-net-base no-false-bind=unknown-deviceid-unbound epoch=" as *u8)
121 g_fn(lfd, sys_now_realtime_sec()); g_fp(lfd, "\n" as *u8); sys_close(lfd)
122 }
123 sys_exit(0); return 0
124 }
125 g_p("DRVBINDGATE verdict=RED (autonomy/discrimination/no-false-bind not all green)\n" as *u8)
126 if lfd >= 0 {
127 g_fp(lfd, "DRVBINDGATE verdict=RED autonomy=" as *u8); g_fn(lfd, autonomy)
128 g_fp(lfd, " discrimination=" as *u8); g_fn(lfd, discrim)
129 g_fp(lfd, " no_false_bind=" as *u8); g_fn(lfd, nofalse); g_fp(lfd, "\n" as *u8); sys_close(lfd)
130 }
131 sys_exit(1)
132 return 1
133}