code wiki / _hdl_build / nx_cpuid.nx
nx_cpuid.nx source
↩ module page · 75 lines · 4332 B
1// nx_cpuid.nx -- THE R0 PRIMITIVE: what silicon are we actually on, and which code path may dispatch?
2//
3// WHY THIS IS RUNG ZERO: nx_prim_gap reported cpu-feature-detect MISSING across all 256 registry
4// primitives. Without it NO organ can adapt to the machine it runs on, so no future hardware purchase
5// is visible to the ecosystem and every SIMD/prefetch/cache rung above is unbuildable. It is literally
6// the first bit up.
7// ★THE GAP WAS DISCOVERY, NOT CAPABILITY: runtime/nx_cpu_features.nx already ran REAL cpuid via the
8// __cpuid_ebx intrinsic -- but exposed 3 bits (BMI2/ADX) out of a register that also carries AVX2, the
9// whole AVX-512 family, SHA, BMI1, RDSEED and CLFLUSHOPT, and it was registered nowhere. The data was
10// fetched and thrown away. LAW: a capability no registry can see is indistinguishable from one that
11// does not exist.
12//
13// TRIANGULATION IS THE POINT (evidence law): every bit is read TWICE from independent sources -- our own
14// cpuid(7,0):EBX, and the kernel's own decode in /proc/cpuinfo. Agreement is evidence; a DISAGREEMENT is
15// reported as a defect and drives the exit code, never averaged or hidden. If our bit map is wrong we
16// want to fail loudly here, not silently dispatch an AVX-512 kernel onto a Zen 1.
17//
18// Exit code = number of disagreements. 0 = both sources agree on every probed feature.
19// license_tier: ORIGINAL Read-only. No hw writes (Rule 26). expect_exit: 0
20import "nx_cpuid_lib.nx"
21import "nx_resmon_lib.nx"
22
23const CI_CPUBUF: i64 = 8192
24const CI_LEAF: i64 = 7
25const CI_SUBLEAF: i64 = 0
26
27// one probed feature: our cpuid bit vs the kernel's flag token. Prints the row, returns 1 on DISAGREEMENT.
28func ci_row(name: *u8, ours: i64, buf: *u8, n: i64, tok: *u8) -> i64 {
29 let theirs: i64 = cf_flag(buf, n, tok)
30 rm_puts(" " as *u8)
31 rm_puts(name)
32 rm_puts(": cpuid=" as *u8)
33 rm_num(ours)
34 rm_puts(" cpuinfo=" as *u8)
35 rm_num(theirs)
36 var bad: i64 = 0
37 if ours != theirs { bad = 1 }
38 if bad == 0 { rm_puts(" AGREE\n" as *u8) }
39 if bad == 1 { rm_puts(" ***DISAGREE*** (our bit map or the register read is WRONG -- do NOT dispatch on this)\n" as *u8) }
40 return bad
41}
42
43func main() -> i64 {
44 let ebx: i64 = __cpuid_ebx(CI_LEAF, CI_SUBLEAF)
45 let buf: *u8 = sys_mmap(CI_CPUBUF)
46 let n: i64 = rm_read("/proc/cpuinfo" as *u8, buf, CI_CPUBUF)
47
48 rm_puts("=== nx_cpuid -- R0: the silicon this ecosystem is actually running on ===\n" as *u8)
49 rm_puts("cpuid_leaf=7 subleaf=0 ebx=" as *u8); rm_num(ebx); rm_puts("\n" as *u8)
50 rm_puts("oracle=/proc/cpuinfo bytes=" as *u8); rm_num(n); rm_puts("\n" as *u8)
51
52 var bad: i64 = 0
53 bad = bad + ci_row("avx2 " as *u8, cf_avx2(ebx), buf, n, "avx2" as *u8)
54 bad = bad + ci_row("bmi1 " as *u8, cf_bmi1(ebx), buf, n, "bmi1" as *u8)
55 bad = bad + ci_row("bmi2 " as *u8, cf_bmi2(ebx), buf, n, "bmi2" as *u8)
56 bad = bad + ci_row("adx " as *u8, cf_adx(ebx), buf, n, "adx" as *u8)
57 bad = bad + ci_row("sha " as *u8, cf_sha(ebx), buf, n, "sha_ni" as *u8)
58 bad = bad + ci_row("rdseed " as *u8, cf_rdseed(ebx), buf, n, "rdseed" as *u8)
59 bad = bad + ci_row("clflushopt" as *u8, cf_clflushopt(ebx), buf, n, "clflushopt" as *u8)
60 bad = bad + ci_row("avx512f " as *u8, cf_avx512f(ebx), buf, n, "avx512f" as *u8)
61 bad = bad + ci_row("avx512dq " as *u8, cf_avx512dq(ebx), buf, n, "avx512dq" as *u8)
62 bad = bad + ci_row("avx512bw " as *u8, cf_avx512bw(ebx), buf, n, "avx512bw" as *u8)
63 bad = bad + ci_row("avx512vl " as *u8, cf_avx512vl(ebx), buf, n, "avx512vl" as *u8)
64
65 let tier: i64 = cf_simd_tier(ebx)
66 rm_puts("simd_dispatch_tier=" as *u8); rm_num(tier)
67 if tier == CF_TIER_SCALAR { rm_puts(" SCALAR -- no vector path is legal here\n" as *u8) }
68 if tier == CF_TIER_AVX2 { rm_puts(" AVX2 -- 256-bit kernels are legal TODAY on existing hardware; AVX-512 kernels are NOT\n" as *u8) }
69 if tier == CF_TIER_AVX512 { rm_puts(" AVX512 -- 512-bit kernels are legal (tier 1 remains a valid fallback)\n" as *u8) }
70
71 rm_puts("disagreements=" as *u8); rm_num(bad)
72 if bad == 0 { rm_puts(" verdict=TRIANGULATED (two independent readings of the same silicon agree)\n" as *u8) }
73 if bad > 0 { rm_puts(" verdict=CONFLICT -- a feature bit is misread; dispatch on this at your peril\n" as *u8) }
74 return bad
75}