code wiki / _hdl_build / _k_r2_001b3_gate.nx
_k_r2_001b3_gate.nx source
↩ module page · 351 lines · 18478 B
1// _k_r2_001b3_gate.nx -- the K-R2-001b3 gate (virtio-MMIO SECTOR-DATA ROUND-TRIP layer; the
2// fifth slice of the K-R2-001b virtqueue epic, on top of b2c's status-byte writeback). Drives
3// the full author->handshake->queue-config->descriptor-DMA->avail/used->status-byte->sector-data
4// chain with NO mocks: runs the REAL nx_virtio_hs_emit (the team AUTHORS the rv64 image + the
5// TABLE-COMPUTED golden transcript from the sector spec), then RUNS the image on the SOVEREIGN
6// rv64 emulator (rv64im_min_sim + the rv64im_min_virtio device now, on the b2b QueueNotify kick,
7// after the used-ring + status-byte writeback, FOLLOWING the consumed descriptor's data pointer
8// (desc[head].addr) into guest RAM, DMA-READING the first 32-bit word of the data buffer the
9// driver placed the sector pattern into + latching it into the QueueSectPeek RO register -- the
10// PRIMARY, gating lane: Nishi owns the runtime) and asserts the captured serial transcript
11// CONTAINS the emitter's golden ("VIO ACK DRV FEAT OK VQ DESC USED STAT SECT\n" -- after the
12// used-ring + status writeback the device read the sector word the driver wrote, the driver READ
13// QueueSectPeek BACK and verified it == sect_data_expected: the sector-data round-trip binding
14// proof) AND the sovereign emu reports a clean SiFive-finisher halt.
15//
16// Then the ALIGNMENT lane: qemu-system-riscv64 -machine virt -global
17// virtio-mmio.force-legacy=true with a REAL virtio-blk-device backing. The gate authors a
18// SECOND image from a base-rewritten spec (the blk device lands at slot 0x10008000) and runs
19// the SAME driver against the REAL legacy virtio-blk transport: it completes the handshake +
20// queue config, lays the SAME descriptor + the SAME avail ring + the SAME sector word into qemu
21// guest RAM, and kicks the real QueueNotify -- the real legacy transport genuinely walks the
22// ring. The sovereign-only QueueDescPeek / QueueUsedIdxPeek / QueueStatPeek / QueueSectPeek
23// result registers are model instruments the real device does not expose (they read 0), so on
24// qemu the descriptor-DMA + used-ring + status-byte + sector-data verifies gracefully drop
25// " DESC" / " USED" / " STAT" / " SECT"; the gate asserts the qemu transcript contains the b1
26// prefix golden ("VIO ACK DRV FEAT OK VQ") -- proving the SAME driver lays the avail ring + the
27// sector word + kicks the real device -> lanes AGREE on every register the real transport can
28// observe.
29//
30// Finally a TAMPER test: corrupt the QueueSectPeek-EXPECTED constant in the sovereign image
31// (the driver loads its own QueueSectPeek read-back into t3 via lwu and compares against this
32// li-loaded expected in t4; bumping the expected's immediate makes t3 != t4) -> the read-back
33// verify branches PAST stage 9 straight to the finisher -> the transcript loses ONLY its
34// " SECT" canary (the "VIO ACK DRV FEAT OK VQ DESC USED STAT" tail survives) -> the gate MUST go
35// RED. Evidence -> knowledge/status/virtio_blk.log (SECTGATE row; the queue row's ||MARK=
36// reads it). Sovereign orchestration (fork/dup3/execve/wait4). license_tier: ORIGINAL
37import "nx_syscalls.nx"
38
39// the unique rv64 encoding of `lwu t3, 0x6C(t2)` -- the QueueSectPeek read-back load (f3=6,
40// zero-extend; the sector word is read unsigned, matching the lui-built expected). The
41// SectPeek-expected constant the driver compares against is the li that follows it (lui at +4,
42// addi at +8); corrupting the addi's high immediate byte (+11) is the sector-data tamper.
43const G_QSECTPEEK_LOAD_WORD: i64 = 0x06c3ee03 // lwu t3, 0x06C(t2) (QueueSectPeek read-back)
44
45func 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 }
46func 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 }
47func 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 }
48
49// run nx_virtio_hs_emit <spec>; return child wait status (0 = ok)
50func g_run_emit(spec: *u8) -> i64 {
51 let pid: i64 = sys_fork()
52 if pid == 0 {
53 let dn: i64 = sys_openat_wr("/dev/null" as *u8, 0x1a4)
54 if dn >= 0 { sys_dup3(dn, 1, 0) }
55 let argv: *i64 = sys_mmap(32) as *i64
56 argv[0] = "_offc/nx_virtio_hs_emit.elf" as *u8 as i64
57 argv[1] = spec as i64
58 argv[2] = 0
59 let envp: *i64 = sys_mmap(16) as *i64
60 envp[0] = "PATH=/usr/bin:/bin" as *u8 as i64
61 envp[1] = 0
62 sys_execve("_offc/nx_virtio_hs_emit.elf" as *u8, argv, envp)
63 sys_exit(127)
64 }
65 let st: *i64 = sys_mmap(16) as *i64
66 sys_wait4(pid, st, 0)
67 return st[0]
68}
69
70// run the SOVEREIGN rv64 emulator on binpath; serial -> outpath; return wait status
71func g_run_sov(binpath: *u8, outpath: *u8) -> i64 {
72 let pid: i64 = sys_fork()
73 if pid == 0 {
74 let ofd: i64 = sys_openat_wr(outpath, 0x1a4)
75 if ofd >= 0 { sys_dup3(ofd, 1, 0); sys_dup3(ofd, 2, 0) }
76 let argv: *i64 = sys_mmap(32) as *i64
77 argv[0] = "_offc/nx_boot_run_sov.elf" as *u8 as i64
78 argv[1] = binpath as i64
79 argv[2] = 0
80 let envp: *i64 = sys_mmap(16) as *i64
81 envp[0] = "PATH=/usr/bin:/bin" as *u8 as i64
82 envp[1] = 0
83 sys_execve("_offc/nx_boot_run_sov.elf" as *u8, argv, envp)
84 sys_exit(127)
85 }
86 let st: *i64 = sys_mmap(16) as *i64
87 sys_wait4(pid, st, 0)
88 return st[0]
89}
90
91// run qemu-system-riscv64 virt + legacy virtio-blk-device on binpath; serial -> outpath.
92func g_run_qemu(binpath: *u8, outpath: *u8, backing: *u8) -> i64 {
93 let pid: i64 = sys_fork()
94 if pid == 0 {
95 let ofd: i64 = sys_openat_wr(outpath, 0x1a4)
96 if ofd >= 0 { sys_dup3(ofd, 1, 0); sys_dup3(ofd, 2, 0) }
97 let argv: *i64 = sys_mmap(128) as *i64
98 argv[0] = "/usr/bin/qemu-system-riscv64" as *u8 as i64
99 argv[1] = "-machine" as *u8 as i64
100 argv[2] = "virt" as *u8 as i64
101 argv[3] = "-global" as *u8 as i64
102 argv[4] = "virtio-mmio.force-legacy=true" as *u8 as i64
103 argv[5] = "-nographic" as *u8 as i64
104 argv[6] = "-bios" as *u8 as i64
105 argv[7] = binpath as i64
106 argv[8] = "-drive" as *u8 as i64
107 argv[9] = "file=/tmp/_sectgate_backing.img,if=none,format=raw,id=hd0" as *u8 as i64
108 argv[10] = "-device" as *u8 as i64
109 argv[11] = "virtio-blk-device,drive=hd0" as *u8 as i64
110 argv[12] = 0
111 let envp: *i64 = sys_mmap(16) as *i64
112 envp[0] = "PATH=/usr/bin:/bin" as *u8 as i64
113 envp[1] = 0
114 sys_execve("/usr/bin/qemu-system-riscv64" as *u8, argv, envp)
115 sys_exit(127)
116 }
117 let st: *i64 = sys_mmap(16) as *i64
118 sys_wait4(pid, st, 0)
119 return st[0]
120}
121
122// create a small raw backing file for the qemu virtio-blk device (4 sectors of zeros).
123func g_make_backing(path: *u8) -> i64 {
124 let fd: i64 = sys_openat_wr(path, 0x1a4)
125 if fd < 0 { return 0 - 1 }
126 let z: *u8 = sys_mmap(2048)
127 var i: i64 = 0
128 while i < 2048 { z[i] = 0 as u8; i = i + 1 }
129 sys_write(fd, z, 2048)
130 sys_close(fd)
131 return 0
132}
133
134// read whole file into buf (cap-1 max); return byte count (0 if absent)
135func g_read(path: *u8, buf: *u8, cap: i64) -> i64 {
136 let fd: i64 = sys_openat_rd(path)
137 if fd < 0 { return 0 }
138 var n: i64 = 0
139 var go: i64 = 1
140 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 } }
141 sys_close(fd)
142 return n
143}
144
145// does buf[0,n) contain pat (length pl)? 1/0
146func g_buf_has(buf: *u8, n: i64, pat: *u8, pl: i64) -> i64 {
147 if pl <= 0 { return 0 }
148 var i: i64 = 0
149 while i + pl <= n {
150 var k: i64 = 0
151 var hit: i64 = 1
152 while k < pl { if buf[i+k] != pat[k] { hit = 0; k = pl } else { k = k + 1 } }
153 if hit == 1 { return 1 }
154 i = i + 1
155 }
156 return 0
157}
158
159// read a little-endian 32-bit word from buf at byte offset o.
160func g_w32(buf: *u8, o: i64) -> i64 {
161 let b0: i64 = buf[o] as i64
162 let b1: i64 = buf[o+1] as i64
163 let b2: i64 = buf[o+2] as i64
164 let b3: i64 = buf[o+3] as i64
165 return b0 | (b1 << 8) | (b2 << 16) | (b3 << 24)
166}
167
168// find the byte offset of the (unique) QueueSectPeek read-back load word in the image; -1
169// if absent. The SectPeek-expected li the driver compares against is at off+4 (lui) /
170// off+8 (addi); the addi's high immediate byte is at off+11.
171func g_find_sectpeek_load(buf: *u8, n: i64) -> i64 {
172 var o: i64 = 0
173 while o + 4 <= n {
174 if g_w32(buf, o) == G_QSECTPEEK_LOAD_WORD { return o }
175 o = o + 4
176 }
177 return 0 - 1
178}
179
180func g_strlen(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } return n }
181
182// does line [ls,le) of buf begin with key? 1/0
183func g_line_is(buf: *u8, ls: i64, le: i64, key: *u8) -> i64 {
184 var k: i64 = 0
185 while key[k] != (0 as u8) {
186 if ls + k >= le { return 0 }
187 if buf[ls + k] != key[k] { return 0 }
188 k = k + 1
189 }
190 return 1
191}
192
193// author the qemu-alignment spec: copy the main spec, but replace the `base ` line value
194// with the qemu blk slot and the `out ` line with the qemu image path. (The ring page +
195// data buffer + status buffer addresses are guest-RAM addresses valid on both lanes -- left
196// as is.)
197func g_write_qemu_spec(mainspec: *u8, derived: *u8, qemu_out: *u8) -> i64 {
198 let buf: *u8 = sys_mmap(16384)
199 let n: i64 = g_read(mainspec, buf, 16384)
200 if n <= 0 { return 0 - 1 }
201 let ofd: i64 = sys_openat_wr(derived, 0x1a4)
202 if ofd < 0 { return 0 - 1 }
203 var ls: i64 = 0
204 while ls < n {
205 var le: i64 = ls
206 var scan: i64 = 1
207 while scan == 1 { if le >= n { scan = 0 } else { if buf[le] == (10 as u8) { scan = 0 } else { le = le + 1 } } }
208 if g_line_is(buf, ls, le, "base " as *u8) == 1 {
209 g_fp(ofd, "base 0x10008000\n" as *u8)
210 } else {
211 if g_line_is(buf, ls, le, "out " as *u8) == 1 {
212 g_fp(ofd, "out " as *u8); g_fp(ofd, qemu_out); g_fp(ofd, "\n" as *u8)
213 } else {
214 sys_write(ofd, (buf as i64 + ls) as *u8, le - ls)
215 g_fp(ofd, "\n" as *u8)
216 }
217 }
218 ls = le + 1
219 }
220 sys_close(ofd)
221 return 0
222}
223
224func main() -> i64 {
225 let spec: *u8 = "knowledge/specs/virtio_blk_sector_virt.spec" as *u8
226 let binpath: *u8 = "runtime/_hdl_build/_virtio_sector_virt.bin" as *u8
227 let goldpath: *u8 = "runtime/_hdl_build/_virtio_sector_virt.bin.gold" as *u8
228 let qemu_spec: *u8 = "/tmp/_sectgate_qemu.spec" as *u8
229 let qemu_bin: *u8 = "/tmp/_sectgate_qemu.bin" as *u8
230 let backing: *u8 = "/tmp/_sectgate_backing.img" as *u8
231 let tamper_bin: *u8 = "/tmp/_sectgate_tamper.bin" as *u8
232 let sov_serial: *u8 = "/tmp/_sectgate_sov.txt" as *u8
233 let qemu_serial: *u8 = "/tmp/_sectgate_qemu.txt" as *u8
234 let sov_tamper: *u8 = "/tmp/_sectgate_sov_tamper.txt" as *u8
235 // the b1 prefix golden the qemu alignment lane asserts (the real device has none of
236 // QueueDescPeek / QueueUsedIdxPeek / QueueStatPeek / QueueSectPeek, so on qemu the
237 // descriptor-DMA + used-ring + status-byte + sector-data verifies drop " DESC" / " USED" /
238 // " STAT" / " SECT", leaving the b1 prefix the real transport can observe).
239 let prefix_gold: *u8 = "VIO ACK DRV FEAT OK VQ" as *u8
240 // the b2c "STAT" tail the tamper must KEEP (only " SECT" drops under the sector-data tamper).
241 let statonly_gold: *u8 = "VIO ACK DRV FEAT OK VQ DESC USED STAT" as *u8
242 g_p("=== virtio-MMIO sector-data gate (K-R2-001b3: SOVEREIGN rv64 device sector-data round-trip + QueueSectPeek read-back + qemu virtio-blk alignment) ===\n" as *u8)
243
244 let lfd: i64 = sys_openat_append("knowledge/status/virtio_blk.log" as *u8, 0x1a4)
245
246 // STEP 1: author the SOVEREIGN image (base 0x10001000) + the golden transcript.
247 let est: i64 = g_run_emit(spec)
248 if est != 0 {
249 g_p("SECTGATE verdict=RED reason=emit-failed\n" as *u8)
250 if lfd >= 0 { g_fp(lfd, "SECTGATE verdict=RED reason=emit-failed\n" as *u8); sys_close(lfd) }
251 sys_exit(1); return 1
252 }
253 let gold: *u8 = sys_mmap(512)
254 let gn: i64 = g_read(goldpath, gold, 512)
255 gold[gn] = 0 as u8
256 if gn <= 0 {
257 g_p("SECTGATE verdict=RED reason=golden-missing\n" as *u8)
258 if lfd >= 0 { g_fp(lfd, "SECTGATE verdict=RED reason=golden-missing\n" as *u8); sys_close(lfd) }
259 sys_exit(1); return 1
260 }
261
262 // STEP 2 (PRIMARY): the Nishi sovereign rv64 emulator RUNS the image -- handshake + queue
263 // config + descriptor lay (b2a) + avail-ring publish + used-ring writeback (b2b) + status-
264 // byte writeback (b2c) + sector-pattern write + sector-data read-back -> transcript must
265 // CONTAIN the full golden ("...USED STAT SECT\n"). On the b2b kick the device followed
266 // desc[head].addr into guest RAM, DMA-read the first data word the driver placed there +
267 // latched it into QueueSectPeek; the driver read QueueSectPeek back + verified == expected.
268 let sst: i64 = g_run_sov(binpath, sov_serial)
269 let sbuf: *u8 = sys_mmap(65536)
270 let sbn: i64 = g_read(sov_serial, sbuf, 65536)
271 let trans_ok: i64 = g_buf_has(sbuf, sbn, gold, gn)
272 let halt_ok: i64 = g_buf_has(sbuf, sbn, "BOOTSOV verdict=GREEN" as *u8, 21)
273 var sov_ok: i64 = 0
274 if sst == 0 { if trans_ok == 1 { if halt_ok == 1 { sov_ok = 1 } } }
275
276 // STEP 3 (ALIGNMENT): author the qemu-base image (slot 0x10008000) + run qemu with a REAL
277 // legacy virtio-blk-device; the SAME driver lays the SAME descriptor + the SAME avail ring +
278 // the SAME sector word into qemu guest RAM and kicks the real QueueNotify (the real transport
279 // walks the ring). The sovereign-only QueueDescPeek/QueueUsedIdxPeek/QueueStatPeek/
280 // QueueSectPeek registers read 0 on the real device, so the descriptor-DMA + used-ring +
281 // status-byte + sector-data verifies drop " DESC"/" USED"/" STAT"/" SECT" there; assert the
282 // b1 prefix golden ("VIO ACK DRV FEAT OK VQ") appears, proving the driver runs identically
283 // against the real transport up to the avail publish + kick.
284 g_make_backing(backing)
285 g_write_qemu_spec(spec, qemu_spec, qemu_bin)
286 let qest: i64 = g_run_emit(qemu_spec)
287 var align: i64 = 0
288 if qest == 0 {
289 let qst: i64 = g_run_qemu(qemu_bin, qemu_serial, backing)
290 let qbuf: *u8 = sys_mmap(65536)
291 let qbn: i64 = g_read(qemu_serial, qbuf, 65536)
292 let qtrans: i64 = g_buf_has(qbuf, qbn, prefix_gold, g_strlen(prefix_gold))
293 if qst == 0 { if qtrans == 1 { align = 1 } }
294 }
295
296 // STEP 4 (TAMPER): corrupt the QueueSectPeek-EXPECTED constant in the sovereign image.
297 // Scan for the unique QueueSectPeek read-back load word; the expected li the driver
298 // compares against is the addi at load+8. Bump that addi's high immediate byte (load+11)
299 // so the expected != the value the device latched -> the read-back verify branches to the
300 // finisher -> ONLY the " SECT" canary drops (the "...USED STAT" tail survives) -> gate MUST RED.
301 let ibuf: *u8 = sys_mmap(8192)
302 let ibn: i64 = g_read(binpath, ibuf, 8192)
303 let lpos: i64 = g_find_sectpeek_load(ibuf, ibn)
304 var tamper_built: i64 = 0
305 if lpos >= 0 {
306 let ebyte: i64 = lpos + 11 // high immediate byte of the expected addi
307 ibuf[ebyte] = (ibuf[ebyte] + 1) as u8
308 let tfd: i64 = sys_openat_wr(tamper_bin, 0x1a4)
309 if tfd >= 0 { sys_write(tfd, ibuf, ibn); sys_close(tfd); tamper_built = 1 }
310 }
311 var tamper_bites: i64 = 0
312 var tamper_keeps_stat: i64 = 0
313 if tamper_built == 1 {
314 let tst: i64 = g_run_sov(tamper_bin, sov_tamper)
315 let tbuf: *u8 = sys_mmap(65536)
316 let tbn: i64 = g_read(sov_tamper, tbuf, 65536)
317 let tamper_trans: i64 = g_buf_has(tbuf, tbn, gold, gn) // full golden should now MISS
318 let tamper_stat: i64 = g_buf_has(tbuf, tbn, statonly_gold, g_strlen(statonly_gold)) // STAT tail survives
319 if tamper_trans == 0 { tamper_bites = 1 }
320 if tamper_stat == 1 { tamper_keeps_stat = 1 }
321 }
322
323 g_p(" sovereign_emu=" as *u8)
324 if sov_ok == 1 { g_p("GREEN(transcript==golden+clean-halt)" as *u8) } else { g_p("RED" as *u8) }
325 g_p(" qemu_align=" as *u8)
326 if align == 1 { g_p("yes(real-virtio-blk @0x10008000, same descriptor+avail+sector laid + real kick, VQ prefix)" as *u8) } else { g_p("no" as *u8) }
327 g_p(" tamper_bites=" as *u8)
328 if tamper_bites == 1 { g_p("yes(SectPeek-expected corrupt -> SECT drops, STAT survives)\n" as *u8) } else { g_p("no\n" as *u8) }
329
330 var pass: i64 = 0
331 if sov_ok == 1 { if align == 1 { if tamper_bites == 1 { if tamper_keeps_stat == 1 { pass = 1 } } } }
332
333 if pass == 1 {
334 g_p("SECTGATE verdict=GREEN (sovereign rv64 emu drove the sector-data layer; the driver wrote a sector pattern into the descriptor's data buffer, and on the b2b kick the device followed desc[head].addr into guest RAM, DMA-READ that data word + latched it into QueueSectPeek; the driver read QueueSectPeek back + verified == sect_data_expected: REAL sector DATA round-tripped through the ring; transcript==golden; clean halt; qemu virtio-blk-device cross-check lays the SAME descriptor+avail+sector + kicks the real transport AGREEING on the VQ prefix; SectPeek tamper REJECTED -- SECT drops, STAT survives)\n" as *u8)
335 if lfd >= 0 {
336 g_fp(lfd, "SECTGATE verdict=GREEN runtime=sovereign-emu transport=legacy-virtio-mmio layer=sector-data roundtrip=guest-buf->device-dma->sectpeek->driver-readback sectpeek-readback=verified transcript==golden align_qemu=yes(virtio-blk-device,VQ-prefix) tamper=rejected(SECT-drops,STAT-survives) golden=" as *u8)
337 g_fp(lfd, gold)
338 g_fp(lfd, " epoch=" as *u8); g_fn(lfd, sys_now_realtime_sec()); g_fp(lfd, "\n" as *u8); sys_close(lfd)
339 }
340 sys_exit(0); return 0
341 }
342 g_p("SECTGATE verdict=RED (sov_ok/align/tamper not all green)\n" as *u8)
343 if lfd >= 0 {
344 g_fp(lfd, "SECTGATE verdict=RED sov_ok=" as *u8); g_fn(lfd, sov_ok)
345 g_fp(lfd, " align=" as *u8); g_fn(lfd, align)
346 g_fp(lfd, " tamper_bites=" as *u8); g_fn(lfd, tamper_bites)
347 g_fp(lfd, " tamper_keeps_stat=" as *u8); g_fn(lfd, tamper_keeps_stat); g_fp(lfd, "\n" as *u8); sys_close(lfd)
348 }
349 sys_exit(1)
350 return 1
351}