code wiki / _hdl_build / nx_boot_stub_emit.nx

nx_boot_stub_emit.nx source

↩ module page · 130 lines · 6416 B

1// nx_boot_stub_emit.nx -- the BOOT_STUB emitter (K-R0 of the kernel-up ladder). 2// A NEW kernel boot stub IS A SPEC FILE: reads (banner, out) DATA and AUTHORS a 3// bare-metal rv64 flat binary for qemu-virt -- zero hand-written machine code per 4// stub. The emitter is a tiny rv64 encoder for the few instruction forms a boot 5// stub needs (LUI/ADDI/SB/SW/JAL); the ARTIFACT (the boot binary) is emitted from 6// the spec, byte-reproducibly. Run on qemu-system-riscv64 -machine virt -bios it 7// prints the banner over the NS16550A UART and exits clean via the SiFive finisher. 8// nx_boot_stub_emit <specpath> -> writes the flat binary to the spec's `out` 9// VERDICT log -> knowledge/status/boot_stub.log (BOOTEMIT rows, the gate's evidence). 10// Sovereign: syscalls only, no gcc/.sh. license_tier: ORIGINAL 11import "nx_syscalls.nx" 12const BS_MAGIC_4096: i64 = 4096 13 14// ---- qemu-virt platform map (the hardware's fixed MMIO addresses = a device-tree 15// as data, not magic; adding a machine = another const block + a route). ---- 16const BS_UART: i64 = 0x10000000 // NS16550A THR (write a byte = transmit) 17const BS_FIN: i64 = 0x100000 // SiFive test finisher (write to exit) 18const BS_PASS: i64 = 0x5555 // FINISHER_PASS low half -> qemu exits 0 19// rv64 register numbers used 20const RV_X0: i64 = 0 21const RV_T0: i64 = 5 22const RV_T1: i64 = 6 23const RV_T2: i64 = 7 24 25func bs_p(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 } 26func bs_fp(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 } 27func bs_fn(fd: i64, v: i64) -> i64 { let bb: *u8=sys_mmap(28); var m: i64=v; if m<0{m=0-m}; 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 } 28 29// ---- the rv64 mini-encoder (the only instruction forms a boot stub needs) ---- 30func bs_lui(rd: i64, imm20: i64) -> i64 { return ((imm20 & 0xFFFFF) << 12) | (rd << 7) | 0x37 } 31func bs_addi(rd: i64, rs1: i64, imm: i64) -> i64 { return ((imm & 0xFFF) << 20) | (rs1 << 15) | (rd << 7) | 0x13 } 32// store with imm=0; f3=0 -> SB, f3=2 -> SW 33func bs_store(rs2: i64, rs1: i64, f3: i64) -> i64 { return (rs2 << 20) | (rs1 << 15) | (f3 << 12) | 0x23 } 34 35func bs_w32(buf: *u8, off: i64, w: i64) -> i64 { 36 buf[off] = (w & 0xff) as u8 37 buf[off+1] = ((w >> 8) & 0xff) as u8 38 buf[off+2] = ((w >> 16) & 0xff) as u8 39 buf[off+3] = ((w >> 24) & 0xff) as u8 40 return off + 4 41} 42 43// emit one "print byte c over the UART" pair: addi t1,x0,c ; sb t1,0(t0) 44func bs_putc(buf: *u8, off: i64, c: i64) -> i64 { 45 var o: i64 = bs_w32(buf, off, bs_addi(RV_T1, RV_X0, c)) 46 o = bs_w32(buf, o, bs_store(RV_T1, RV_T0, 0)) 47 return o 48} 49 50// author the whole stub into buf; return byte length 51func bs_emit(buf: *u8, banner: *u8, blen: i64) -> i64 { 52 var o: i64 = 0 53 o = bs_w32(buf, o, bs_lui(RV_T0, BS_UART >> 12)) // t0 = UART base 54 var i: i64 = 0 55 while i < blen { o = bs_putc(buf, o, banner[i] as i64); i = i + 1 } 56 o = bs_putc(buf, o, 10) // trailing newline 57 o = bs_w32(buf, o, bs_lui(RV_T2, BS_FIN >> 12)) // t2 = finisher base 58 // li t1, BS_PASS (lui + addi, with addi-sign-extension correction) 59 var hi: i64 = BS_PASS >> 12 60 var lo: i64 = BS_PASS & 0xFFF 61 if lo >= 0x800 { lo = lo - 0x1000; hi = hi + 1 } 62 o = bs_w32(buf, o, bs_lui(RV_T1, hi)) 63 o = bs_w32(buf, o, bs_addi(RV_T1, RV_T1, lo)) 64 o = bs_w32(buf, o, bs_store(RV_T1, RV_T2, 2)) // sw t1,0(t2) -> exit 65 o = bs_w32(buf, o, 0x6F) // jal x0,0 (spin) 66 return o 67} 68 69// ---- spec field parse: `key value` lines ---- 70func bs_field(buf: *u8, ls: i64, le: i64, key: *u8, out: *u8) -> i64 { 71 var k: i64 = 0 72 while key[k] != (0 as u8) { 73 if ls + k >= le { return 0 - 1 } 74 if buf[ls + k] != key[k] { return 0 - 1 } 75 k = k + 1 76 } 77 var o: i64 = 0 78 var q: i64 = ls + k 79 while q < le { if buf[q] == (13 as u8) { q = le } else { out[o] = buf[q]; o = o + 1; q = q + 1 } } 80 out[o] = 0 as u8 81 return o 82} 83 84func bs_log(name: *u8, bytes: i64, verdict: *u8) -> i64 { 85 let lfd: i64 = sys_openat_append("knowledge/status/boot_stub.log" as *u8, 0x1a4) 86 if lfd < 0 { return 0 - 1 } 87 bs_fp(lfd, "BOOTEMIT name=" as *u8); bs_fp(lfd, name) 88 bs_fp(lfd, " machine=virt bytes=" as *u8); bs_fn(lfd, bytes) 89 bs_fp(lfd, " verdict=" as *u8); bs_fp(lfd, verdict); bs_fp(lfd, "\n" as *u8) 90 sys_close(lfd) 91 return 0 92} 93 94func main(argc: i64, argv: *i64) -> i64 { 95 if argc < 2 { bs_p("usage: nx_boot_stub_emit <specpath>\n" as *u8); sys_exit(2); return 2 } 96 let sp: *u8 = argv[1] as *u8 97 let lenp: *i64 = sys_mmap(16) as *i64 98 let spec: *u8 = sys_read_file(sp, lenp) 99 let sn: i64 = lenp[0] 100 if sn <= 0 { bs_p("BOOT-EMIT REFUSED: spec missing\n" as *u8); bs_log("(missing)" as *u8, 0, "REFUSED" as *u8); sys_exit(2); return 2 } 101 let banner: *u8 = sys_mmap(256) 102 let outp: *u8 = sys_mmap(256) 103 banner[0] = 0 as u8 104 outp[0] = 0 as u8 105 var ls: i64 = 0 106 while ls < sn { 107 var le: i64 = ls 108 var scan: i64 = 1 109 while scan == 1 { if le >= sn { scan = 0 } else { if spec[le] == (10 as u8) { scan = 0 } else { le = le + 1 } } } 110 if spec[ls] != (35 as u8) { 111 bs_field(spec, ls, le, "banner " as *u8, banner) 112 bs_field(spec, ls, le, "out " as *u8, outp) 113 } 114 ls = le + 1 115 } 116 var blen: i64 = 0 117 while banner[blen] != (0 as u8) { blen = blen + 1 } 118 if blen <= 0 { bs_p("BOOT-EMIT REFUSED: no banner row\n" as *u8); bs_log("(no-banner)" as *u8, 0, "REFUSED" as *u8); sys_exit(2); return 2 } 119 if outp[0] == (0 as u8) { bs_p("BOOT-EMIT REFUSED: no out row\n" as *u8); bs_log("(no-out)" as *u8, 0, "REFUSED" as *u8); sys_exit(2); return 2 } 120 let bin: *u8 = sys_mmap(BS_MAGIC_4096) 121 let sz: i64 = bs_emit(bin, banner, blen) 122 let ofd: i64 = sys_openat_wr(outp, 0x1a4) 123 if ofd < 0 { bs_p("BOOT-EMIT RED: cannot open out\n" as *u8); bs_log(outp, sz, "RED" as *u8); sys_exit(1); return 1 } 124 sys_write(ofd, bin, sz) 125 sys_close(ofd) 126 bs_p("BOOT-EMIT GREEN: authored " as *u8); bs_p(outp); bs_p(" bytes=" as *u8); bs_fn(1, sz); bs_p(" (spec in, bootable rv64 stub out)\n" as *u8) 127 bs_log(outp, sz, "GREEN" as *u8) 128 sys_exit(0) 129 return 0 130}