code wiki / _hdl_build / nx_trap_syscall_emit.nx
nx_trap_syscall_emit.nx
buildroot/runtime/_hdl_build/nx_trap_syscall_emit.nx
about
nx_trap_syscall_emit.nx -- the TRAP+SYSCALL emitter (K-R1 of the kernel-up ladder).
A NEW kernel syscall surface IS A SPEC FILE: reads (banner, 8x `sys <name> <a7>
<emit>`, out) DATA and AUTHORS a bare-metal rv64 flat image for qemu-virt whose trap
vector dispatches an 8-syscall mini-surface. The trap dispatch IS a STATE_MACHINE
(shape 18 / pe13) instance: read a7, branch to the matching per-syscall handler row,
emit that syscall's transcript byte to the UART, advance mepc+4, mret. exit (a7=93)
writes the SiFive finisher = clean halt. Zero hand-written machine code per surface;
the emitter is a tiny rv64 encoder (the few forms a trap surface needs:
auipc/lui/addi/sb/sw + B-type branch + jal + csrrw/csrrs + ecall + mret) and the
IMAGE + the GOLDEN transcript are TABLE-COMPUTED from the spec, byte-reproducibly.
nx_trap_syscall_emit <specpath> -> writes the flat image to the spec's `out`
and the table-computed golden transcript to <out>.golden
VERDICT log -> knowledge/status/trap_syscall.log (TRAPEMIT rows, the gate's evidence).
Sovereign: syscalls only, no gcc/.sh. license_tier: ORIGINAL
dependencies 1 imports · 0 importers
imports: nx_syscalls.nx
imported by: nobody (leaf or entry point)
call flow from main pre-order; caps 40 nodes / depth 6 declared; ↻ = already shown
structs
| none |
consts
| 16 | const TS_MAGIC_8192: i64 = 8192 |
| 19 | const TS_UART: i64 = 0x10000000 // NS16550A THR (write a byte = transmit) |
| 20 | const TS_FIN: i64 = 0x100000 // SiFive test finisher (write to exit) |
| 21 | const TS_PASS: i64 = 0x5555 // FINISHER_PASS low half -> clean halt |
| 23 | const TS_MTVEC: i64 = 0x305 |
| 24 | const TS_MEPC: i64 = 0x341 |
| 26 | const RV_X0: i64 = 0 |
| 27 | const RV_T0: i64 = 5 |
| 28 | const RV_T1: i64 = 6 |
| 29 | const RV_T2: i64 = 7 |
| 30 | const RV_A7: i64 = 17 |
| 31 | const RV_T3: i64 = 28 |
| 32 | const RV_T4: i64 = 29 |
| 34 | const TS_NSYS: i64 = 8 |
functions
| 36 | func ts_p(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 } |
| 37 | func ts_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 } |
| 38 | func ts_fn(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 } |
| 41 | func ts_lui(rd: i64, imm20: i64) -> i64 { return ((imm20 & 0xFFFFF) << 12) | (rd << 7) | 0x37 } |
| 42 | func ts_auipc(rd: i64, imm20: i64) -> i64 { return ((imm20 & 0xFFFFF) << 12) | (rd << 7) | 0x17 } called by 1: ts_emit_boot |
| 43 | func ts_addi(rd: i64, rs1: i64, imm: i64) -> i64 { return ((imm & 0xFFF) << 20) | (rs1 << 15) | (rd << 7) | 0x13 } |
| 45 | func ts_store(rs2: i64, rs1: i64, f3: i64, imm: i64) -> i64 |
| 51 | func ts_branch(rs1: i64, rs2: i64, f3: i64, imm: i64) -> i64 called by 1: ts_emit_dispatch |
| 59 | func ts_jal(rd: i64, imm: i64) -> i64 |
| 67 | func ts_csrrw(rd: i64, csr: i64, rs1: i64) -> i64 { return ((csr & 0xFFF) << 20) | (rs1 << 15) | (1 << 12) | (rd << 7) | 0x73 } |
| 68 | func ts_csrrs(rd: i64, csr: i64, rs1: i64) -> i64 { return ((csr & 0xFFF) << 20) | (rs1 << 15) | (2 << 12) | (rd << 7) | 0x73 } called by 1: ts_emit_ret |
| 70 | func ts_w32(buf: *u8, off: i64, w: i64) -> i64 |
| 84 | func ts_trap_entry_byte(blen: i64) -> i64 |
| 89 | func ts_handler0_byte(blen: i64) -> i64 |
| 94 | func ts_handler_byte(blen: i64, idx: i64) -> i64 |
| 98 | func ts_trap_ret_byte(blen: i64) -> i64 |
| 105 | func ts_emit_boot(buf: *u8, off: i64, banner: *u8, blen: i64) -> i64 |
| 123 | func ts_emit_driver(buf: *u8, off: i64, nums: *i64) -> i64 |
| 136 | func ts_emit_dispatch(buf: *u8, off: i64, nums: *i64, blen: i64) -> i64 |
| 154 | func ts_emit_handler(buf: *u8, off: i64, ch: i64, blen: i64) -> i64 |
| 164 | func ts_emit_exit(buf: *u8, off: i64, ch: i64) -> i64 |
| 180 | func ts_emit_ret(buf: *u8, off: i64) -> i64 |
| 190 | func ts_emit_image(buf: *u8, banner: *u8, blen: i64, nums: *i64, emits: *u8) -> i64 called by 1: main calls 6: ts_emit_bootts_emit_driverts_emit_dispatchts_emit_exitts_emit_handlerts_emit_ret |
| 209 | func ts_field(buf: *u8, ls: i64, le: i64, key: *u8, out: *u8) -> i64 called by 1: main |
| 224 | func ts_skip_tok(buf: *u8, p: i64, le: i64) -> i64 called by 1: ts_parse_sys |
| 231 | func ts_skip_sp(buf: *u8, p: i64, le: i64) -> i64 called by 1: ts_parse_sys |
| 239 | func ts_parse_sys(buf: *u8, ls: i64, le: i64, outnum: *i64, outemit: *u8) -> i64 |
| 266 | func ts_log(name: *u8, bytes: i64, golden: *u8, verdict: *u8) -> i64 |
| 278 | func main(argc: i64, argv: *i64) -> i64 |