code wiki / _hdl_build / nx_x86_kat_gate.nx
nx_x86_kat_gate.nx source
↩ module page · 77 lines · 4145 B
1// nx_x86_kat_gate.nx -- SOVEREIGN x86_64 backend run-KAT gate (the gold per-ISA emit-proof tier,
2// hardware-rung-up, NO gcc / NO qemu / NO external binutils).
3//
4// AUTHOR=ORGAN, no-false-green: the proof that nxc2's x86_64 backend EMITS CORRECT NATIVE code is a
5// RUN, not a structural guess. This gate composes the team's own toolchain end to end -- it forks
6// _offc/nx_sov_build_run.elf (nx_cc_sovereign -> nxasm_x86 -> execute) on two KAT modules and reads
7// their real process exit codes:
8// POS nx_emit_kat -> 42 (all three distinct KATs 37/10/81 computed correctly by the backend)
9// NEG nx_emit_kat_raw -> 37 (raw kat(7,5); proves the gate reads the ACTUAL exit, not a baked 42)
10// GREEN iff pos==42 AND neg==37 AND pos!=neg (distinct => no constant rubber-stamp). This is the
11// SOVEREIGN replacement for the external bench-parity / qemu proof: the whole trust path is Nishi
12// organs. Other ISAs reuse this shape, swapping the native run for their sovereign emulator
13// (nx_emu_rv64/arm64/mips64/ppc64le/loongarch64/sparc64). Writes knowledge/status/x86_64_kat_gate.log
14// so nx_portability_proof_census can credit x86_64 as SOVEREIGN-run-proven. license_tier: ORIGINAL
15import "nx_syscalls.nx"
16import "nx_gate_verdict.nx"
17
18const SBR: *u8 = "_offc/nx_sov_build_run.elf"
19const KG_LOG: *u8 = "knowledge/status/x86_64_kat_gate.log"
20
21func kg_w(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 }
22func kg_wn(fd: i64, v: i64) -> i64 { let bb: *u8 = sys_mmap(28); var m: i64=v; if m<0 {m=0-m; sys_write(fd,"-" 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(fd, bb, k); return 0 }
23
24// fork + execve _offc/nx_sov_build_run.elf <name>, mute child stdout/stderr, return its run exit
25// (128+sig if it died to a signal -- a segfaulted toolchain must not decode as a false 0).
26func kg_run_mod(name: *u8) -> i64 {
27 let argv: *i64 = sys_mmap(8 * 4) as *i64
28 argv[0] = SBR as i64
29 argv[1] = name as i64
30 argv[2] = 0
31 let envp: *i64 = sys_mmap(8 * 4) as *i64
32 envp[0] = "PATH=/usr/bin:/bin" as *u8 as i64
33 envp[1] = 0
34 let dn: i64 = sys_openat_wr("/dev/null" as *u8, 0x1a4)
35 let pid: i64 = sys_fork()
36 if pid == 0 {
37 if dn >= 0 { sys_dup3(dn, 1, 0) }
38 if dn >= 0 { sys_dup3(dn, 2, 0) }
39 sys_execve(SBR, argv, envp)
40 sys_exit(127)
41 }
42 let st: *i64 = sys_mmap(16) as *i64
43 sys_wait4(pid, st, 0)
44 if dn >= 0 { sys_close(dn) }
45 let sig: i64 = st[0] & 0x7f
46 if sig != 0 { return 128 + sig }
47 return (st[0] >> 8) & 0xff
48}
49
50func kg_emit(fd: i64, pos: i64, neg: i64, ok: i64) -> i64 {
51 kg_w(fd, "X86_64GATE authored=organ target=x86_64 toolchain=nx_cc_sovereign+nxasm_x86(SOVEREIGN,no-gcc/qemu/binutils) kat=(a*b)+(a-b) pos_all3=" as *u8); kg_wn(fd, pos)
52 kg_w(fd, " neg_raw75=" as *u8); kg_wn(fd, neg)
53 kg_w(fd, " distinct=" as *u8); if pos != neg { kg_wn(fd, 1) } else { kg_wn(fd, 0) }
54 if ok == 1 { kg_w(fd, " verdict=GREEN\n" as *u8) } else { kg_w(fd, " verdict=RED reason=exit-mismatch-or-constant\n" as *u8) }
55 return 0
56}
57
58func main() -> i64 {
59 let pos: i64 = kg_run_mod("nx_emit_kat" as *u8)
60 let neg: i64 = kg_run_mod("nx_emit_kat_raw" as *u8)
61 var ok: i64 = 1
62 if pos != 42 { ok = 0 }
63 if neg != 37 { ok = 0 }
64 if pos == neg { ok = 0 }
65 kg_emit(1, pos, neg, ok)
66 let lf: i64 = sys_openat_append(KG_LOG, 420)
67 if lf >= 0 { kg_emit(lf, pos, neg, ok); sys_close(lf) }
68 // MIGRATED onto nx_gate_verdict by nx_gate_dry_apply (D001, minimal form): every check
69 // row above is untouched, so the PASS/FAIL vector cannot change; only the hand-rolled
70 // verdict emission is replaced by the ONE shared base class. Proven by nx_gate_migrate verify.
71 let ctr__dry: *i64 = gv_ctr()
72 ctr__dry[0] = ok
73 ctr__dry[1] = 1
74 let rc__dry: i64 = gv_verdict("X86-KAT-GATE" as *u8, ctr__dry, "teeth unchanged; verdict emission migrated onto the shared base class" as *u8)
75 sys_exit(rc__dry)
76 return rc__dry
77}