code wiki / (root) / nxc_native_wrap.nx

nxc_native_wrap.nx source

↩ module page · 173 lines · 5264 B

1// nxc_native_wrap.nx -- sovereign bootstrap-verification driver. 2// 3// One-shot main() that proves the off-C runtime path works: 4// 5// 1. Read an RV64 ELF from disk (built via stage 1's gcc-anchor 6// OR via the in-NishiLang nxc.elf compiler). 7// 2. Validate the ELF via nx_elf_read. 8// 3. Allocate a 16 MB flat memory region. 9// 4. Load segments into memory via nx_loader. 10// 5. Set sp to the top of memory minus 64 bytes. 11// 6. Run via nx_rv64_sim with a 100M-instruction budget. 12// 7. Report exit code + instruction count to stderr. 13// 14// Usage: 15// nxc_native_wrap path/to/program.elf 16// 17// Returns: 18// 0 = program executed cleanly via simulator 19// 1 = invalid ELF 20// 2 = load failed (e.g. segment OOB) 21// 3 = simulator hit illegal instruction 22// 4 = budget expired 23// N = the simulated program exited with code N (passed through) 24// 25// Pairs with bootstrap_native.sh. Once this lands + works, we have 26// the full off-C verification: produce ELF in NishiLang, run ELF in 27// NishiLang, compare exit code with stage 1. Sovereign stage 2 is 28// achieved. 29 30import "syscalls.nx" 31import "nx_elf_read.nx" 32import "nx_loader.nx" 33import "nx_rv64_sim.nx" 34 35const NX_WRAP_MEM_SIZE: i64 = 16777216 // 16 MB sim memory 36const NX_WRAP_BUDGET: i64 = 100000000 // 100M instructions 37 38// Read the entire file at `path` into a fresh mmap'd buffer. 39// Returns null + zero len on error. 40func nx_wrap_read_file(path: *u8, out_len: *i64) -> *u8 { 41 let fd: i64 = sys_openat_rd(path) 42 if fd < 0 { 43 *out_len = 0 44 return 0 as *u8 45 } 46 let cap: i64 = 4194304 // 4 MB cap (covers nxc.elf + room) 47 let buf: *u8 = sys_mmap(cap) 48 var total: i64 = 0 49 var go: i64 = 1 50 while go == 1 { 51 let base: i64 = buf as i64 52 let tail: *u8 = (base + total) as *u8 53 let n: i64 = sys_read(fd, tail, cap - total) 54 if n <= 0 { go = 0 } 55 if n > 0 { total = total + n } 56 if total >= cap { go = 0 } 57 } 58 sys_close(fd) 59 *out_len = total 60 return buf 61} 62 63// Print "wrap: " + a NUL-terminated string + "\n" to stderr. 64func nx_wrap_log(s: *u8) -> i64 { 65 sys_write(2, "wrap: " as *u8, 6) 66 var n: i64 = 0 67 while s[n] != 0 { n = n + 1 } 68 sys_write(2, s, n) 69 sys_write(2, "\n" as *u8, 1) 70 return 0 71} 72 73// Print "wrap: " + label + " = " + decimal value + "\n". 74func nx_wrap_logn(label: *u8, v: i64) -> i64 { 75 sys_write(2, "wrap: " as *u8, 6) 76 var nl: i64 = 0 77 while label[nl] != 0 { nl = nl + 1 } 78 sys_write(2, label, nl) 79 sys_write(2, " = " as *u8, 3) 80 81 let buf: *u8 = sys_mmap(32) 82 var n: i64 = v 83 if n < 0 { 84 sys_write(2, "-" as *u8, 1) 85 n = 0 - n 86 } 87 if n == 0 { 88 buf[0] = 0x30 89 sys_write(2, buf, 1) 90 } else { 91 var k: i64 = 0 92 while n > 0 { 93 buf[k] = 0x30 + (n - (n / 10) * 10) 94 n = n / 10 95 k = k + 1 96 } 97 var j: i64 = 0 98 var r: i64 = k - 1 99 while j < r { 100 let t: i64 = buf[j] 101 buf[j] = buf[r] 102 buf[r] = t 103 j = j + 1 104 r = r - 1 105 } 106 sys_write(2, buf, k) 107 } 108 sys_write(2, "\n" as *u8, 1) 109 return 0 110} 111 112func main(argc: i64, argv: *i64) -> i64 { 113 if argc < 2 { 114 nx_wrap_log("usage: nxc_native_wrap <path.elf>" as *u8) 115 return 1 116 } 117 let path: *u8 = (argv[1]) as *u8 118 119 // Stage 1: read the ELF. 120 let elf_len_raw: *u8 = sys_mmap(16) 121 let elf_len: *i64 = elf_len_raw as *i64 122 *elf_len = 0 123 let elf_buf: *u8 = nx_wrap_read_file(path, elf_len) 124 if elf_buf == (0 as *u8) { 125 nx_wrap_log("read failed" as *u8) 126 return 1 127 } 128 nx_wrap_logn("elf bytes" as *u8, *elf_len) 129 130 // Stage 2: validate header. 131 let h: *NxElfHeader = nx_elf_parse_header(elf_buf, *elf_len) 132 if h.valid != 1 { 133 nx_wrap_log("invalid ELF" as *u8) 134 return 1 135 } 136 if h.is_64bit != 1 { 137 nx_wrap_log("not ELF64" as *u8) 138 return 1 139 } 140 if h.e_machine != 0xF3 { 141 nx_wrap_log("not RISC-V" as *u8) 142 return 1 143 } 144 nx_wrap_logn("entry vaddr (hex)" as *u8, h.e_entry) 145 nx_wrap_logn("phnum" as *u8, h.e_phnum) 146 147 // Stage 3: allocate sim memory + load segments. 148 let mem: *u8 = sys_mmap(NX_WRAP_MEM_SIZE) 149 let entry: i64 = nx_loader_load(elf_buf, *elf_len, mem, NX_WRAP_MEM_SIZE) 150 if entry < 0 { 151 nx_wrap_log("load failed" as *u8) 152 return 2 153 } 154 nx_wrap_logn("loaded; entry" as *u8, entry) 155 156 // Stage 4: spin up the CPU + run. 157 let cpu: *NxRv64Cpu = nx_rv64_cpu_new(mem, NX_WRAP_MEM_SIZE, entry) 158 nx_rv64_cpu_set_sp(cpu, NX_WRAP_MEM_SIZE - 64) 159 nx_wrap_log("starting simulator" as *u8) 160 let r: i64 = nx_rv64_run(cpu, NX_WRAP_BUDGET) 161 nx_wrap_logn("instructions executed" as *u8, cpu.insn_count) 162 163 if r == 0 { 164 nx_wrap_logn("program exit code" as *u8, cpu.exit_code) 165 return cpu.exit_code 166 } 167 if r > 0 { 168 nx_wrap_log("BUDGET EXPIRED" as *u8) 169 return 4 170 } 171 nx_wrap_log("ILLEGAL INSTRUCTION" as *u8) 172 return 3 173}