code wiki / _hdl_build / nx_x86_kat_gate.nx
nx_x86_kat_gate.nx source
↩ module page · 69 lines · 3612 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"
16
17const SBR: *u8 = "_offc/nx_sov_build_run.elf"
18const KG_LOG: *u8 = "knowledge/status/x86_64_kat_gate.log"
19
20func 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 }
21func 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 }
22
23// fork + execve _offc/nx_sov_build_run.elf <name>, mute child stdout/stderr, return its run exit
24// (128+sig if it died to a signal -- a segfaulted toolchain must not decode as a false 0).
25func kg_run_mod(name: *u8) -> i64 {
26 let argv: *i64 = sys_mmap(8 * 4) as *i64
27 argv[0] = SBR as i64
28 argv[1] = name as i64
29 argv[2] = 0
30 let envp: *i64 = sys_mmap(8 * 4) as *i64
31 envp[0] = "PATH=/usr/bin:/bin" as *u8 as i64
32 envp[1] = 0
33 let dn: i64 = sys_openat_wr("/dev/null" as *u8, 0x1a4)
34 let pid: i64 = sys_fork()
35 if pid == 0 {
36 if dn >= 0 { sys_dup3(dn, 1, 0) }
37 if dn >= 0 { sys_dup3(dn, 2, 0) }
38 sys_execve(SBR, argv, envp)
39 sys_exit(127)
40 }
41 let st: *i64 = sys_mmap(16) as *i64
42 sys_wait4(pid, st, 0)
43 if dn >= 0 { sys_close(dn) }
44 let sig: i64 = st[0] & 0x7f
45 if sig != 0 { return 128 + sig }
46 return (st[0] >> 8) & 0xff
47}
48
49func kg_emit(fd: i64, pos: i64, neg: i64, ok: i64) -> i64 {
50 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)
51 kg_w(fd, " neg_raw75=" as *u8); kg_wn(fd, neg)
52 kg_w(fd, " distinct=" as *u8); if pos != neg { kg_wn(fd, 1) } else { kg_wn(fd, 0) }
53 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) }
54 return 0
55}
56
57func main() -> i64 {
58 let pos: i64 = kg_run_mod("nx_emit_kat" as *u8)
59 let neg: i64 = kg_run_mod("nx_emit_kat_raw" as *u8)
60 var ok: i64 = 1
61 if pos != 42 { ok = 0 }
62 if neg != 37 { ok = 0 }
63 if pos == neg { ok = 0 }
64 kg_emit(1, pos, neg, ok)
65 let lf: i64 = sys_openat_append(KG_LOG, 420)
66 if lf >= 0 { kg_emit(lf, pos, neg, ok); sys_close(lf) }
67 if ok == 1 { return 0 }
68 return 1
69}