code wiki / _hdl_build / nx_genesis_live.nx

nx_genesis_live.nx source

↩ module page · 250 lines · 12878 B

1// nx_genesis_live.nx -- the genesis tree, LIVE per host (operator early ask: "dynamic and living by 2// what system the nishi os is on ... the spore to seed depends on it"). 3// 4// module: nishi-core.genealogy.genesis_live 5// capability: CORE_COMPUTE (reconcile the genesis driver axis against THIS machine's real devices) 6// 7// The genesis lineage is the PORTABLE model. This organ INGESTS the host PCI probe (hwmap.tsv, 8// authored by nx_hwmap from sysfs) and the PCI class map (PCI base class -> genesis driver-axis 9// sovereignty), and computes the LIVE per-host census: of the devices actually present here, how 10// many have a SOVEREIGN Nishi driver, how many are a THIRD-PARTY lock (worklist), how many are 11// FUTURE, how many UNCOVERED (a real device with no genealogy node = a gap to fill). Run it on a 12// different machine (different hwmap) and the census changes -- the tree's coverage re-derives per 13// host. 14// 15// The static PCI class map is now AUTHORED BY THIS ORGAN into the NATIVE content-addressed seg_store 16// (ncfg / nx_native_config) -- NOT a borrowed knowledge/registry/pci_class_map.tsv (operator 2026-06-20: 17// "stop using tsv, nishi ecosystem only, all rungs everywhere"). gl_load_map seeds the 11-row map into 18// a fresh per-call store then reads it BY COLUMN NAME, so the census has ZERO dependency on that .tsv 19// (migrated off + removed). The host probe (hwmap.tsv) stays a live sysfs-derived artifact (nx_hwmap's 20// output, dynamic per host -- a separate unit, not config). Sovereign: nx_cc->nxasm. license_tier: ORIGINAL 21import "nx_syscalls.nx" 22import "nx_itoa_lib.nx" // shared MSB-first emitter (zero-alloc) 23import "nx_native_config.nx" 24const GL_MAGIC_1048592: i64 = 1048592 25const GL_MAGIC_1048576: i64 = 1048576 26const GL_MAGIC_1048575: i64 = 1048575 27 28const GL_MAP_PREFIX: *u8 = "/tmp/pci-class-map-" 29const GL_HWMAP: *u8 = "knowledge/registry/hwmap.tsv" 30const GL_MAXMAP: i64 = 64 31 32func gl_puts(s: *u8) -> i64 { var n: i64 = 0; while s[n] != 0 as u8 { n = n + 1 } sys_write(1, s, n); return 0 } 33// MIGRATED to the shared emitter (debt 1785563586). The old body mmapped a scratch buffer 34// per call and never freed it. At PAGE granularity that is 4096B leaked PER CALL -- the 35// defect that took 28.5GB of a 36GB host in nx_ts_lumadiff (2MB input, ~3.66M calls). 36// nxi_* is MSB-first, allocates NOTHING, and emits identical bytes including the sign. 37func gl_putn(v: i64) -> i64 { nxi_out(v); return 0 } 38func gl_streq(a: *u8, b: *u8) -> i64 { 39 var i: i64 = 0 40 while a[i] != 0 as u8 { if a[i] != b[i] { return 0 } i = i + 1 } 41 if b[i] != 0 as u8 { return 0 } 42 return 1 43} 44func gl_read(path: *u8, lb: *i64) -> *u8 { 45 let fd: i64 = sys_openat_rd(path) 46 if fd < 0 { lb[0] = 0; return 0 as *u8 } 47 let buf: *u8 = sys_mmap(GL_MAGIC_1048592) 48 var n: i64 = 0; var go: i64 = 1 49 while go == 1 { 50 let r: i64 = sys_read(fd, ((buf as i64) + n) as *u8, GL_MAGIC_1048576 - n) 51 if r <= 0 { go = 0 } else { n = n + r } 52 if n >= GL_MAGIC_1048576 { go = 0 } 53 } 54 sys_close(fd); buf[n] = 0 as u8; lb[0] = n 55 return buf 56} 57func gl_field_dup(b: *u8, ls: i64, le: i64, f: i64) -> *u8 { 58 var cur: i64 = 0; var i: i64 = ls 59 while cur < f { 60 if i >= le { let e: *u8 = sys_mmap(2); e[0] = 0 as u8; return e } 61 if b[i] == 9 as u8 { cur = cur + 1 } 62 i = i + 1 63 } 64 let out: *u8 = sys_mmap(64); var o: i64 = 0 65 while i < le { 66 if b[i] == 9 as u8 { i = le } else { if o < 63 { out[o] = b[i]; o = o + 1 } i = i + 1 } 67 } 68 out[o] = 0 as u8 69 return out 70} 71// PCI class triplet "0x030200" -> base class "03" 72func gl_class_hi(cf: *u8) -> *u8 { 73 let out: *u8 = sys_mmap(3) 74 if cf[0] == 48 as u8 { if cf[1] == 120 as u8 { 75 out[0] = cf[2]; out[1] = cf[3]; out[2] = 0 as u8; return out 76 } } 77 out[0] = 63 as u8; out[1] = 63 as u8; out[2] = 0 as u8; return out 78} 79// copy a NUL-terminated string into a fresh buffer (null-safe) -- keeps map field copies stable. 80func gl_dupz(s: *u8) -> *u8 { 81 if (s as i64) == 0 { let e: *u8 = sys_mmap(2); e[0] = 0 as u8; return e } 82 var l: i64 = 0; while s[l] != (0 as u8) { l = l + 1 } 83 let d: *u8 = sys_mmap(l + 1) 84 var i: i64 = 0; while i < l { d[i] = s[i]; i = i + 1 } d[l] = 0 as u8 85 return d 86} 87// fresh unique native store prefix per call (sys_now_ms + a fresh-mmap-address salt so repeated calls 88// within the same millisecond -- the gate calls gl_analyze 4x -- never collide on one /tmp store). 89func gl_fresh_prefix(out: *u8, base: *u8) -> i64 { 90 var po: i64 = 0 91 while base[po] != (0 as u8) { out[po] = base[po]; po = po + 1 } 92 let salt: *u8 = sys_mmap(8) 93 var m: i64 = sys_now_ms() + ((salt as i64) & GL_MAGIC_1048575) 94 let ds: *u8 = sys_mmap(28) 95 var k: i64 = 0 96 if m == 0 { ds[0] = 48 as u8; k = 1 } 97 while m > 0 { ds[k] = (48 + (m % 10)) as u8; m = m / 10; k = k + 1 } 98 var j: i64 = 0 99 while j < k { out[po] = ds[k - 1 - j]; po = po + 1; j = j + 1 } 100 out[po] = 45 as u8; po = po + 1 101 out[po] = 0 as u8 102 return po 103} 104// author one class-map row (class_hi -> category/sovereignty/genesis-node) into the native store. 105func gl_map_row(w: *i64, idx: i64, chi: *u8, cat: *u8, sov: *u8, node: *u8) -> i64 { 106 let keys: *i64 = sys_mmap(8 * 8) as *i64 107 let vals: *i64 = sys_mmap(8 * 8) as *i64 108 keys[0] = ("class_hi\x00") as i64; vals[0] = (chi as i64) 109 keys[1] = ("category\x00") as i64; vals[1] = (cat as i64) 110 keys[2] = ("sovereignty\x00") as i64; vals[2] = (sov as i64) 111 keys[3] = ("node\x00") as i64; vals[3] = (node as i64) 112 return ncfg_add_row(w, "pcm\x00" as *u8, idx, keys, vals, 4) 113} 114// seed the 11-row PCI class map FAITHFULLY (values verbatim from the migrated pci_class_map.tsv). 115func gl_seed_map(prefix: *u8) -> i64 { 116 let w: *i64 = ncfg_begin() 117 gl_map_row(w, 0, "01\x00" as *u8, "storage\x00" as *u8, "sovereign\x00" as *u8, "VAR-DRV-BLK\x00" as *u8) 118 gl_map_row(w, 1, "02\x00" as *u8, "network\x00" as *u8, "sovereign\x00" as *u8, "VAR-DRV-NET\x00" as *u8) 119 gl_map_row(w, 2, "03\x00" as *u8, "display-gpu\x00" as *u8, "third-party\x00" as *u8, "VAR-DRV-GPU\x00" as *u8) 120 gl_map_row(w, 3, "04\x00" as *u8, "multimedia\x00" as *u8, "uncovered\x00" as *u8, "-\x00" as *u8) 121 gl_map_row(w, 4, "05\x00" as *u8, "memory-controller\x00" as *u8, "uncovered\x00" as *u8, "-\x00" as *u8) 122 gl_map_row(w, 5, "06\x00" as *u8, "bridge\x00" as *u8, "infra\x00" as *u8, "-\x00" as *u8) 123 gl_map_row(w, 6, "07\x00" as *u8, "comm-controller\x00" as *u8, "uncovered\x00" as *u8, "-\x00" as *u8) 124 gl_map_row(w, 7, "08\x00" as *u8, "system-peripheral\x00" as *u8, "uncovered\x00" as *u8, "-\x00" as *u8) 125 gl_map_row(w, 8, "09\x00" as *u8, "input\x00" as *u8, "uncovered\x00" as *u8, "-\x00" as *u8) 126 gl_map_row(w, 9, "0c\x00" as *u8, "serial-bus-usb\x00" as *u8, "future\x00" as *u8, "VAR-DRV-USB\x00" as *u8) 127 gl_map_row(w, 10, "0d\x00" as *u8, "wireless\x00" as *u8, "uncovered\x00" as *u8, "-\x00" as *u8) 128 ncfg_set_count(w, "pcm\x00" as *u8, 11) 129 return ncfg_commit(prefix, w) 130} 131// seed + open + load the class map into cmh/cms/cmc parallel arrays (copies); returns row count or -1. 132func gl_load_map(cmh: *i64, cms: *i64, cmc: *i64) -> i64 { 133 let prefix: *u8 = sys_mmap(256) 134 gl_fresh_prefix(prefix, GL_MAP_PREFIX) 135 if gl_seed_map(prefix) != 0 { return 0 - 1 } 136 let h: *i64 = ncfg_open(prefix) 137 if (h as i64) == 0 { return 0 - 1 } 138 let cnt: i64 = ncfg_count(h, "pcm\x00" as *u8) 139 let rk: *i64 = sys_mmap(8 * 16) as *i64 140 let rv: *i64 = sys_mmap(8 * 16) as *i64 141 var ncm: i64 = 0 142 var i: i64 = 0 143 while i < cnt { 144 let rf: i64 = ncfg_row(h, "pcm\x00" as *u8, i, rk, rv, 16) 145 if rf > 0 { if ncm < GL_MAXMAP { 146 cmh[ncm] = gl_dupz(ncfg_field(rk, rv, rf, "class_hi\x00" as *u8)) as i64 147 cmc[ncm] = gl_dupz(ncfg_field(rk, rv, rf, "category\x00" as *u8)) as i64 148 cms[ncm] = gl_dupz(ncfg_field(rk, rv, rf, "sovereignty\x00" as *u8)) as i64 149 ncm = ncm + 1 150 } } 151 i = i + 1 152 } 153 return ncm 154} 155 156// ingest the class map + the host probe; tally per-host driver sovereignty into outs. 157// outs[0]=present outs[1]=sovereign outs[2]=third_party outs[3]=future outs[4]=uncovered outs[5]=infra 158func gl_analyze(hwmap_path: *u8, outs: *i64) -> i64 { 159 // ---- load the class map from the NATIVE store (no TSV) ---- 160 let cmh: *i64 = sys_mmap(8 * GL_MAXMAP) as *i64 // class_hi strings 161 let cms: *i64 = sys_mmap(8 * GL_MAXMAP) as *i64 // sovereignty strings 162 let cmc: *i64 = sys_mmap(8 * GL_MAXMAP) as *i64 // category strings (unused here; kept for one loader) 163 let ncm: i64 = gl_load_map(cmh, cms, cmc) 164 if ncm < 0 { return 0 - 1 } 165 // ---- ingest the host probe ---- 166 let hlb: *i64 = sys_mmap(16) as *i64; hlb[0] = 0 167 let hb: *u8 = gl_read(hwmap_path, hlb) 168 if (hb as i64) == 0 { return 0 - 2 } 169 let hn: i64 = hlb[0] 170 var present: i64 = 0; var sov: i64 = 0; var tp: i64 = 0; var fut: i64 = 0; var unc: i64 = 0; var infra: i64 = 0 171 var ls: i64 = 0; var i: i64 = 0 172 while i <= hn { 173 var atend: i64 = 0 174 if i == hn { atend = 1 } 175 if i < hn { if hb[i] == 10 as u8 { atend = 1 } } 176 if atend == 1 { 177 if i > ls { if hb[ls] != 35 as u8 { 178 present = present + 1 179 let cf: *u8 = gl_field_dup(hb, ls, i, 3) 180 let chi: *u8 = gl_class_hi(cf) 181 // look up sovereignty (default uncovered if class not mapped) 182 var found: i64 = 0 - 1 183 var j: i64 = 0 184 while j < ncm { if gl_streq(cmh[j] as *u8, chi) == 1 { found = j; j = ncm } else { j = j + 1 } } 185 var sv: *u8 = "uncovered" as *u8 186 if found >= 0 { sv = cms[found] as *u8 } 187 if gl_streq(sv, "sovereign" as *u8) == 1 { sov = sov + 1 } 188 else { if gl_streq(sv, "third-party" as *u8) == 1 { tp = tp + 1 } 189 else { if gl_streq(sv, "future" as *u8) == 1 { fut = fut + 1 } 190 else { if gl_streq(sv, "infra" as *u8) == 1 { infra = infra + 1 } 191 else { unc = unc + 1 } } } } 192 } } 193 ls = i + 1 194 } 195 i = i + 1 196 } 197 outs[0] = present; outs[1] = sov; outs[2] = tp; outs[3] = fut; outs[4] = unc; outs[5] = infra 198 return 0 199} 200 201// print one line per present device (class -> category/sovereignty), for the real probe. 202func gl_print_devices(hwmap_path: *u8) -> i64 { 203 let cmh: *i64 = sys_mmap(8 * GL_MAXMAP) as *i64 204 let cms: *i64 = sys_mmap(8 * GL_MAXMAP) as *i64 205 let cmc: *i64 = sys_mmap(8 * GL_MAXMAP) as *i64 206 let ncm: i64 = gl_load_map(cmh, cms, cmc) 207 if ncm < 0 { return 0 - 1 } 208 let hlb: *i64 = sys_mmap(16) as *i64; hlb[0] = 0 209 let hb: *u8 = gl_read(hwmap_path, hlb); let hn: i64 = hlb[0] 210 var ls: i64 = 0; var i: i64 = 0 211 while i <= hn { 212 var atend: i64 = 0 213 if i == hn { atend = 1 } 214 if i < hn { if hb[i] == 10 as u8 { atend = 1 } } 215 if atend == 1 { 216 if i > ls { if hb[ls] != 35 as u8 { 217 let cf: *u8 = gl_field_dup(hb, ls, i, 3) 218 let chi: *u8 = gl_class_hi(cf) 219 var cat: *u8 = "(unmapped)" as *u8; var sv: *u8 = "uncovered" as *u8 220 var j: i64 = 0 221 while j < ncm { if gl_streq(cmh[j] as *u8, chi) == 1 { cat = cmc[j] as *u8; sv = cms[j] as *u8; j = ncm } else { j = j + 1 } } 222 gl_puts(" class 0x"); gl_puts(chi); gl_puts(" "); gl_puts(cat); gl_puts(" -> "); gl_puts(sv); gl_puts("\n") 223 } } 224 ls = i + 1 225 } 226 i = i + 1 227 } 228 return 0 229} 230 231func main() -> i64 { 232 gl_puts("=== NISHI GENESIS LIVE -- driver-sovereignty census for THIS host (ingests the PCI probe) ===\n") 233 let outs: *i64 = sys_mmap(128) as *i64 234 if gl_analyze(GL_HWMAP, outs) != 0 { 235 gl_puts("FATAL: cannot seed the native class map or read "); gl_puts(GL_HWMAP); gl_puts("\n") 236 sys_exit(1); return 1 237 } 238 gl_print_devices(GL_HWMAP) 239 gl_puts(" LIVE CENSUS: present="); gl_putn(outs[0]) 240 gl_puts(" sovereign="); gl_putn(outs[1]) 241 gl_puts(" third-party(lock worklist)="); gl_putn(outs[2]) 242 gl_puts(" future="); gl_putn(outs[3]) 243 gl_puts(" uncovered(gap)="); gl_putn(outs[4]) 244 gl_puts(" infra="); gl_putn(outs[5]); gl_puts("\n") 245 var acc: i64 = outs[1] + outs[2] + outs[3] + outs[4] + outs[5] 246 if acc != outs[0] { gl_puts(" VERDICT: tally inconsistent\n"); sys_exit(1); return 1 } 247 if outs[0] == 0 { gl_puts(" VERDICT: no devices probed (run nx_hwmap first)\n"); sys_exit(1); return 1 } 248 gl_puts(" VERDICT: ingested live probe; coverage re-derived for THIS host (different host -> different census)\n") 249 sys_exit(0); return 0 250}