code wiki / _hdl_build / _boot_stub_gate.nx
_boot_stub_gate.nx source
↩ module page · 152 lines · 6708 B
1// _boot_stub_gate.nx -- the K-R0 gate (kernel-up ladder rung 0). Drives the full
2// author->boot chain with NO mocks: runs the REAL nx_boot_stub_emit (the team authors
3// the stub from the spec), then runs the REAL qemu-system-riscv64 -machine virt (the
4// declared hardware-oracle lane, per the ladder), captures the serial output, and
5// asserts the K-R0 acceptance: banner "NISHI" on serial + clean exit (qemu status 0,
6// the SiFive finisher pass). Evidence -> knowledge/status/boot_stub.log (BOOTGATE row,
7// the row's ||MARK= reads it). Sovereign orchestration (fork/dup3/execve/wait4).
8// NOTE: the emitted stub ALWAYS writes the finisher, so qemu self-exits; no watchdog
9// needed for our own artifact (a watchdog is a hardening follow-up if arbitrary stubs
10// are ever gated). license_tier: ORIGINAL
11import "nx_syscalls.nx"
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
17// run nx_boot_stub_emit <spec>; return child wait status (0 = ok)
18func g_run_emit(spec: *u8) -> i64 {
19 let pid: i64 = sys_fork()
20 if pid == 0 {
21 let dn: i64 = sys_openat_wr("/dev/null" as *u8, 0x1a4)
22 if dn >= 0 { sys_dup3(dn, 1, 0) }
23 let argv: *i64 = sys_mmap(32) as *i64
24 argv[0] = "_offc/nx_boot_stub_emit.elf" as *u8 as i64
25 argv[1] = spec as i64
26 argv[2] = 0
27 let envp: *i64 = sys_mmap(16) as *i64
28 envp[0] = "PATH=/usr/bin:/bin" as *u8 as i64
29 envp[1] = 0
30 sys_execve("_offc/nx_boot_stub_emit.elf" as *u8, argv, envp)
31 sys_exit(127)
32 }
33 let st: *i64 = sys_mmap(16) as *i64
34 sys_wait4(pid, st, 0)
35 return st[0]
36}
37
38// run qemu-system-riscv64 on binpath; serial -> outpath; return child wait status
39func g_run_qemu(binpath: *u8, outpath: *u8) -> i64 {
40 let pid: i64 = sys_fork()
41 if pid == 0 {
42 let ofd: i64 = sys_openat_wr(outpath, 0x1a4)
43 if ofd >= 0 { sys_dup3(ofd, 1, 0); sys_dup3(ofd, 2, 0) }
44 let argv: *i64 = sys_mmap(64) as *i64
45 argv[0] = "/usr/bin/qemu-system-riscv64" as *u8 as i64
46 argv[1] = "-machine" as *u8 as i64
47 argv[2] = "virt" as *u8 as i64
48 argv[3] = "-nographic" as *u8 as i64
49 argv[4] = "-bios" as *u8 as i64
50 argv[5] = binpath as i64
51 argv[6] = 0
52 let envp: *i64 = sys_mmap(16) as *i64
53 envp[0] = "PATH=/usr/bin:/bin" as *u8 as i64
54 envp[1] = 0
55 sys_execve("/usr/bin/qemu-system-riscv64" as *u8, argv, envp)
56 sys_exit(127)
57 }
58 let st: *i64 = sys_mmap(16) as *i64
59 sys_wait4(pid, st, 0)
60 return st[0]
61}
62
63// run the SOVEREIGN rv64 emulator on binpath; serial -> outpath; return wait status
64func g_run_sov(binpath: *u8, outpath: *u8) -> i64 {
65 let pid: i64 = sys_fork()
66 if pid == 0 {
67 let ofd: i64 = sys_openat_wr(outpath, 0x1a4)
68 if ofd >= 0 { sys_dup3(ofd, 1, 0); sys_dup3(ofd, 2, 0) }
69 let argv: *i64 = sys_mmap(32) as *i64
70 argv[0] = "_offc/nx_boot_run_sov.elf" as *u8 as i64
71 argv[1] = binpath as i64
72 argv[2] = 0
73 let envp: *i64 = sys_mmap(16) as *i64
74 envp[0] = "PATH=/usr/bin:/bin" as *u8 as i64
75 envp[1] = 0
76 sys_execve("_offc/nx_boot_run_sov.elf" as *u8, argv, envp)
77 sys_exit(127)
78 }
79 let st: *i64 = sys_mmap(16) as *i64
80 sys_wait4(pid, st, 0)
81 return st[0]
82}
83
84// does the file at path contain pat? 1/0
85func g_file_has(path: *u8, pat: *u8) -> i64 {
86 let buf: *u8 = sys_mmap(65536)
87 let fd: i64 = sys_openat_rd(path)
88 if fd < 0 { return 0 }
89 var n: i64 = 0
90 var go: i64 = 1
91 while go == 1 { let r: i64 = sys_read(fd, (buf as i64 + n) as *u8, 65535 - n); if r <= 0 { go = 0 } else { n = n + r } if n >= 65535 { go = 0 } }
92 sys_close(fd)
93 var pl: i64 = 0
94 while pat[pl] != (0 as u8) { pl = pl + 1 }
95 var i: i64 = 0
96 while i + pl <= n {
97 var k: i64 = 0
98 var hit: i64 = 1
99 while k < pl { if buf[i+k] != pat[k] { hit = 0; k = pl } else { k = k + 1 } }
100 if hit == 1 { return 1 }
101 i = i + 1
102 }
103 return 0
104}
105
106func main() -> i64 {
107 let spec: *u8 = "knowledge/specs/boot_stub_virt.spec" as *u8
108 let binpath: *u8 = "runtime/_hdl_build/_boot_nishi_virt.bin" as *u8
109 let sov_serial: *u8 = "/tmp/_bootgate_sov.txt" as *u8
110 let qemu_serial: *u8 = "/tmp/_bootgate_qemu.txt" as *u8
111 g_p("=== boot-stub gate (K-R0: SOVEREIGN rv64 runtime + qemu alignment diff-lane) ===\n" as *u8)
112
113 let est: i64 = g_run_emit(spec)
114 let lfd: i64 = sys_openat_append("knowledge/status/boot_stub.log" as *u8, 0x1a4)
115 if est != 0 {
116 g_p("BOOTGATE verdict=RED reason=emit-failed\n" as *u8)
117 if lfd >= 0 { g_fp(lfd, "BOOTGATE verdict=RED reason=emit-failed\n" as *u8); sys_close(lfd) }
118 sys_exit(1); return 1
119 }
120
121 // PRIMARY: the Nishi sovereign rv64 emulator RUNS the stub (Nishi owns the runtime)
122 let sst: i64 = g_run_sov(binpath, sov_serial)
123 let shas: i64 = g_file_has(sov_serial, "NISHI" as *u8)
124 var sov_ok: i64 = 0
125 if sst == 0 { if shas == 1 { sov_ok = 1 } }
126
127 // ALIGNMENT: qemu cross-check only (NOT the runtime -- the diff-lane signal)
128 let qst: i64 = g_run_qemu(binpath, qemu_serial)
129 let qhas: i64 = g_file_has(qemu_serial, "NISHI" as *u8)
130 var align: i64 = 0
131 if qst == 0 { if qhas == 1 { align = 1 } }
132
133 g_p(" sovereign_emu=" as *u8)
134 if sov_ok == 1 { g_p("GREEN(NISHI+clean-halt)" as *u8) } else { g_p("RED" as *u8) }
135 g_p(" qemu_align=" as *u8)
136 if align == 1 { g_p("yes\n" as *u8) } else { g_p("no\n" as *u8) }
137
138 if sov_ok == 1 {
139 g_p("BOOTGATE verdict=GREEN (sovereign rv64 emu booted; qemu cross-check " as *u8)
140 if align == 1 { g_p("AGREES)\n" as *u8) } else { g_p("DIVERGES -- investigate)\n" as *u8) }
141 if lfd >= 0 {
142 g_fp(lfd, "BOOTGATE verdict=GREEN runtime=sovereign-emu align_qemu=" as *u8)
143 if align == 1 { g_fp(lfd, "yes" as *u8) } else { g_fp(lfd, "no" as *u8) }
144 g_fp(lfd, " epoch=" as *u8); g_fn(lfd, sys_now_realtime_sec()); g_fp(lfd, "\n" as *u8); sys_close(lfd)
145 }
146 sys_exit(0); return 0
147 }
148 g_p("BOOTGATE verdict=RED (sovereign emu did not boot clean)\n" as *u8)
149 if lfd >= 0 { g_fp(lfd, "BOOTGATE verdict=RED reason=sovereign-emu\n" as *u8); sys_close(lfd) }
150 sys_exit(1)
151 return 1
152}