code wiki / _hdl_build / _gpu_bm_doorbell_gate.nx
_gpu_bm_doorbell_gate.nx source
↩ module page · 128 lines · 9520 B
1// _gpu_bm_doorbell_gate.nx -- BARE-METAL SOVEREIGN-GPU rung BM-GPU-5: the FULL doorbell->execute path.
2// Composes BM-GPU-4 (GPFIFO channel + entry) + BM-GPU-1 (pushbuffer execution): the driver writes pushbuffers +
3// GPFIFO entries, advances GP_PUT, and RINGS THE DOORBELL; the modeled host FIFO then walks the ring GP_GET..GP_PUT,
4// decodes each entry, fetches+executes each pushbuffer, and the semaphores land in GPU memory. THIS IS THE EXACT
5// SOVEREIGN SUBMIT->EXECUTE LOOP WSL'S HOST-TRANSLATION BLOCKED -- closed end-to-end in a spec-faithful model.
6// NISHI-ECOSYSTEM-ONLY: our channel + our ring + our pushbuffer + our FIFO model. HONEST SCOPE: full path LOGIC vs
7// spec; real doorbell MMIO + real channel in VRAM = BM-GPU-6 on native Linux.
8//
9// GREEN iff: before the doorbell both semaphores read 0; after the doorbell BOTH land their distinct payloads and
10// GP_GET advances to GP_PUT (FIFO consumed the ring); AND tampers (no doorbell => nothing executes; corrupt
11// pushbuffer => its semaphore doesn't land) hold.
12// Marker -> knowledge/status/gpu_baremetal.log (BMDOORBELLGATE). license_tier: ORIGINAL
13import "nx_syscalls.nx"
14
15func p(s: *u8) -> i64 { var nn: i64=0; while s[nn]!=(0 as u8){nn=nn+1} sys_write(1,s,nn); return 0 }
16func fp(fd: i64, s: *u8) -> i64 { var nn: i64=0; while s[nn]!=(0 as u8){nn=nn+1} sys_write(fd,s,nn); return 0 }
17func n(v: i64) -> i64 { let bb: *u8=sys_mmap(28); var m: i64=v; if m<0{m=0-m;sys_write(1,"-" as *u8,1)}; 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(1,bb,k); return 0 }
18func x(v: i64) -> i64 { p("0x" as *u8); let bb:*u8=sys_mmap(20); var k:i64=0; var m:i64=v; if m==0{bb[0]=48;k=1}; while m>0{ let d:i64=m&15; if d<10{bb[k]=(48+d) as u8}else{bb[k]=(87+d) as u8}; m=(m>>4); k=k+1 } var i:i64=0; let o:*u8=sys_mmap(20); while i<k{o[i]=bb[k-1-i];i=i+1} sys_write(1,o,k); return 0 }
19func fx(fd: i64, v: i64) -> i64 { let bb:*u8=sys_mmap(20); var k:i64=0; var m:i64=v; if m==0{bb[0]=48;k=1}; while m>0{ let d:i64=m&15; if d<10{bb[k]=(48+d) as u8}else{bb[k]=(87+d) as u8}; m=(m>>4); k=k+1 } let o:*u8=sys_mmap(20); var i:i64=0; while i<k{o[i]=bb[k-1-i];i=i+1} fp(fd,"0x" as *u8); sys_write(fd,o,k); return 0 }
20func rd32(b: *u8, o: i64) -> i64 { return (b[o] as i64)|((b[o+1] as i64)<<8)|((b[o+2] as i64)<<16)|((b[o+3] as i64)<<24) }
21func wr32(b: *u8, o: i64, v: i64) -> i64 { b[o]=(v&0xff) as u8; b[o+1]=((v>>8)&0xff) as u8; b[o+2]=((v>>16)&0xff) as u8; b[o+3]=((v>>24)&0xff) as u8; return 0 }
22
23// ===== BM-GPU-1: spec-faithful NVIDIA GPFIFO pushbuffer interpreter (GF100 header + NVC56F semaphore release) =====
24func gpfifo_run(mem: *u8, pb_off: i64, pb_dwords: i64) -> i64 {
25 var i: i64 = 0; var s_lo: i64 = 0; var s_hi: i64 = 0; var s_pay: i64 = 0
26 while i < pb_dwords {
27 let hdr: i64 = rd32(mem, pb_off + i*4); i = i + 1
28 let mode: i64 = (hdr >> 29) & 0x7; let count: i64 = (hdr >> 16) & 0x1fff; let method0: i64 = (hdr & 0xfff) << 2
29 var j: i64 = 0
30 while j < count {
31 if i >= pb_dwords { j = count } else {
32 let data: i64 = rd32(mem, pb_off + i*4); i = i + 1
33 var m: i64 = method0; if mode == 1 { m = method0 + j*4 }
34 if m == 0x5c { s_lo = data }
35 if m == 0x60 { s_hi = data }
36 if m == 0x64 { s_pay = data }
37 if m == 0x6c { if (data & 0x7) == 1 { wr32(mem, (s_lo & 0xffffffff) | ((s_hi & 0xffffffff) << 32), s_pay) } }
38 j = j + 1
39 }
40 }
41 }
42 return 0
43}
44// BM-GPU-4: GF100 GPFIFO entry decode
45func gp_decode_va(e0: i64, e1: i64) -> i64 { return (e0 & 0xFFFFFFFC) | ((e1 & 0xFF) << 32) }
46func gp_decode_len(e1: i64) -> i64 { return (e1 >> 10) & 0x1FFFFF }
47func gp_encode0(va: i64) -> i64 { return va & 0xFFFFFFFC }
48func gp_encode1(va: i64, length: i64) -> i64 { return ((va >> 32) & 0xFF) | ((length & 0x1FFFFF) << 10) }
49
50// emit a clean NVC56F semaphore-release pushbuffer (6 dwords) at mem+pb_off; corrupt!=0 -> bad method offset.
51func emit_pb(mem: *u8, pb_off: i64, sem_addr: i64, payload: i64, corrupt: i64) -> i64 {
52 var method: i64 = 0x5c; if corrupt == 1 { method = 0x40 }
53 wr32(mem, pb_off + 0, (1 << 29) | (5 << 16) | (method >> 2))
54 wr32(mem, pb_off + 4, sem_addr & 0xffffffff); wr32(mem, pb_off + 8, (sem_addr >> 32) & 0xffffffff)
55 wr32(mem, pb_off + 12, payload); wr32(mem, pb_off + 16, 0); wr32(mem, pb_off + 20, 0x1)
56 return 0
57}
58
59// ===== the modeled host FIFO: on doorbell, walk the ring GP_GET..GP_PUT, decode+execute each pushbuffer =====
60// ch: [0]=ring_va [1]=ring_entries [2]=gp_put [3]=gp_get
61func fifo_doorbell(gmem: *u8, ch: *i64) -> i64 {
62 var get: i64 = ch[3]; let put: i64 = ch[2]
63 while get < put {
64 let eo: i64 = ch[0] + get*8
65 let e0: i64 = rd32(gmem, eo); let e1: i64 = rd32(gmem, eo + 4)
66 gpfifo_run(gmem, gp_decode_va(e0, e1), gp_decode_len(e1))
67 get = get + 1
68 }
69 ch[3] = get // FIFO advances GP_GET (consumed the ring)
70 return get
71}
72
73func main() -> i64 {
74 p("=== BARE-METAL SOVEREIGN-GPU BM-GPU-5: doorbell -> FIFO fetches ring entries -> executes pushbuffers ===\n" as *u8)
75 let gmem: *u8 = sys_mmap(8388608) // 8 MiB modeled GPU memory (VAs = offsets)
76 let SEM1: i64 = 0x1000; let SEM2: i64 = 0x1100
77 let PB1: i64 = 0x2000; let PB2: i64 = 0x2100
78 let RING: i64 = 0x4000
79 let PAY1: i64 = 0x1111aaaa; let PAY2: i64 = 0x2222bbbb
80 let ch: *i64 = sys_mmap(128)
81
82 // ---- driver: emit 2 pushbuffers, build channel + 2 ring entries, advance GP_PUT ----
83 emit_pb(gmem, PB1, SEM1, PAY1, 0); emit_pb(gmem, PB2, SEM2, PAY2, 0)
84 ch[0] = RING; ch[1] = 512; ch[2] = 0; ch[3] = 0
85 wr32(gmem, RING + 0, gp_encode0(PB1)); wr32(gmem, RING + 4, gp_encode1(PB1, 6))
86 wr32(gmem, RING + 8, gp_encode0(PB2)); wr32(gmem, RING + 12, gp_encode1(PB2, 6))
87 ch[2] = 2 // GP_PUT = 2 (two entries pending)
88
89 let before1: i64 = rd32(gmem, SEM1); let before2: i64 = rd32(gmem, SEM2)
90 p(" before doorbell: sem1=" as *u8); x(before1); p(" sem2=" as *u8); x(before2); p(" gp_put=" as *u8); n(ch[2]); p(" gp_get=" as *u8); n(ch[3]); p("\n" as *u8)
91 // ---- RING THE DOORBELL ----
92 fifo_doorbell(gmem, ch)
93 let after1: i64 = rd32(gmem, SEM1); let after2: i64 = rd32(gmem, SEM2)
94 p(" after doorbell: sem1=" as *u8); x(after1); p(" sem2=" as *u8); x(after2); p(" gp_get=" as *u8); n(ch[3]); p(" (expected sem1=" as *u8); x(PAY1); p(" sem2=" as *u8); x(PAY2); p(")\n" as *u8)
95
96 // ---- TAMPERS ----
97 // T1: do NOT ring the doorbell (GP_PUT stays 0) -> nothing executes -> semaphore stays 0 (doorbell is causal)
98 let ch2: *i64 = sys_mmap(128); wr32(gmem, SEM1, 0); emit_pb(gmem, PB1, SEM1, PAY1, 0)
99 ch2[0] = RING; ch2[1] = 512; ch2[2] = 0; ch2[3] = 0 // gp_put stays 0 = no kick
100 fifo_doorbell(gmem, ch2); let t_nodoor: i64 = rd32(gmem, SEM1)
101 // T2: corrupt PB1 -> doorbell rung but the semaphore must NOT land
102 let ch3: *i64 = sys_mmap(128); wr32(gmem, SEM1, 0); emit_pb(gmem, PB1, SEM1, PAY1, 1)
103 wr32(gmem, RING + 0, gp_encode0(PB1)); wr32(gmem, RING + 4, gp_encode1(PB1, 6))
104 ch3[0] = RING; ch3[1] = 512; ch3[2] = 1; ch3[3] = 0
105 fifo_doorbell(gmem, ch3); let t_corrupt: i64 = rd32(gmem, SEM1)
106 p(" [tamper] no-doorbell sem=" as *u8); x(t_nodoor); p(" corrupt-pb sem=" as *u8); x(t_corrupt); p(" (both must be 0)\n" as *u8)
107
108 var pass: i64 = 0
109 if before1 == 0 { if before2 == 0 { if after1 == PAY1 { if after2 == PAY2 { if ch[3] == 2 {
110 if t_nodoor == 0 { if t_corrupt == 0 { pass = 1 } } } } } } }
111 p(" checks: both_zero_before=" as *u8); var bz: i64=0; if before1==0 { if before2==0 { bz=1 } } n(bz)
112 p(" both_landed_after=" as *u8); var ba: i64=0; if after1==PAY1 { if after2==PAY2 { ba=1 } } n(ba); p(" gp_get==gp_put=" as *u8); n(ch[3]==2); p(" doorbell-causal=" as *u8); n(t_nodoor==0); p(" corrupt-rejected=" as *u8); n(t_corrupt==0); p("\n" as *u8)
113
114 let lfd: i64 = sys_openat_append("knowledge/status/gpu_baremetal.log" as *u8, 0x1a4)
115 if pass == 1 {
116 p("BMDOORBELLGATE verdict=GREEN reason=FULL-sovereign-submit->execute-path-CLOSED (doorbell rings -> modeled FIFO walks GPFIFO ring GP_GET..GP_PUT -> decodes entries -> executes BOTH pushbuffers -> distinct semaphores 0x1111aaaa+0x2222bbbb land, GP_GET advances to GP_PUT; no-doorbell=nothing executes [doorbell causal]; corrupt-pushbuffer=semaphore-doesnt-land) = THE EXACT LOOP WSL BLOCKED, sovereign+spec-faithful. SCOPE=full-path-LOGIC-vs-spec-NOT-real-silicon; real-doorbell/VRAM=BM-GPU-6\n" as *u8)
117 if lfd >= 0 {
118 fp(lfd, "BMDOORBELLGATE verdict=GREEN rung=BM-GPU-5-doorbell-execute device=sovereign-NVIDIA-channel+FIFO-model two-pushbuffers->two-semaphores sem1=" as *u8); fx(lfd, after1)
119 fp(lfd, " sem2=" as *u8); fx(lfd, after2); fp(lfd, " gp_get=2(consumed-ring) tampers=held(no-doorbell-nothing-executes+corrupt-pb-no-land) =CLOSES THE SOVEREIGN SUBMIT->EXECUTE LOOP WSL BLOCKED scope=full-path-logic-vs-spec-NOT-real-silicon next=BM-GPU-6-REAL-bare-metal-NATIVE-LINUX(PCIe/BAR0/VRAM/doorbell)\n" as *u8)
120 sys_close(lfd)
121 }
122 sys_exit(0); return 0
123 }
124 p("BMDOORBELLGATE verdict=RED (execute/ring-consume/tamper not satisfied)\n" as *u8)
125 if lfd >= 0 { fp(lfd, "BMDOORBELLGATE verdict=RED see-console\n" as *u8); sys_close(lfd) }
126 sys_exit(1)
127 return 1
128}