code wiki / _hdl_build / nx_strap_emit.nx

nx_strap_emit.nx

buildroot/runtime/_hdl_build/nx_strap_emit.nx

9901 B176 linesdepth 2pulls 2 transitivereach 0 importersview sourcekind tool
docsdependenciesstructsconstsfunctions

about

nx_strap_emit.nx -- S-MODE TRAP DELEGATION + sret (X-PAGE-STRAP-001). The real-OS trap path: instead of every trap going to the M-mode monitor, an exception is DELEGATED (medeleg) to the S-mode kernel, which handles it at stvec and returns with sret. Sequence: M-mode installs mtvec(=M handler) + stvec(=S handler) + medeleg + drops to U-mode (mret, MPP=U); the U program ecalls; with the ecall-from-U bit set in medeleg the trap vectors to the S-MODE handler (stvec), which confirms scause==8, prints "STRAP", advances sepc+4, and sret's back to U; the U program then halts (finisher) -- reaching the finisher PROVES sret returned. With medeleg=0 (the gate's control) the trap goes to the M-mode handler instead -> "MTRAP", no STRAP. medeleg (argv[1]) -- default 0x100 (delegate ecall-from-U, cause 8). out-path (argv[2]). Sovereign, no gcc/.sh. license_tier: ORIGINAL

dependencies 1 imports · 0 importers

nx_syscalls.nx nx_strap_emit.nx

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

main st_parse_num sys_mmap nxa_die sys_write sys_exit nxa_lock_take nxa_lock_addr sys_write ↻ nxa_lock_give nxa_lock_addr ↻ nxa_report_overrun sys_write ↻ nxa_dump_printable sys_write ↻ nxa_dump_sizes sys_write ↻ st_emit_image st_w32 st_lui st_li32u st_li32 st_w32 ↻ st_lui ↻ st_addi st_w32 ↻ st_slli st_srli st_csrrw st_li32 ↻ st_jal st_csrrs st_branch st_emit_str st_w32 ↻ st_addi ↻ st_store st_addi ↻ st_fin st_li32 ↻

structs

none

consts

12const ST_MAGIC_8192: i64 = 8192
14const ST_OUT: *u8 = "runtime/_hdl_build/_strap_virt.bin"
15const ST_GOLD: *u8 = "runtime/_hdl_build/_strap_virt.bin.gold"
16const ST_LOG: *u8 = "knowledge/status/priv.log"
18const ST_UART: i64 = 0x10000000
19const ST_FIN: i64 = 0x100000
20const ST_PASS: i64 = 0x5555
21const ST_MEM_BASE: i64 = 0x80000000
22const ST_CSR_MSTATUS: i64 = 0x300
23const ST_CSR_MTVEC: i64 = 0x305
24const ST_CSR_MEPC: i64 = 0x341
25const ST_CSR_STVEC: i64 = 0x105
26const ST_CSR_SEPC: i64 = 0x141
27const ST_CSR_SCAUSE: i64 = 0x142
28const ST_CSR_MEDELEG: i64 = 0x302
29const ST_MRET: i64 = 0x30200073
30const ST_SRET: i64 = 0x10200073
31const ST_ECALL: i64 = 0x00000073
32const ST_MPP_U: i64 = 0
33const ST_ECALL_U_CAUSE: i64 = 8
35const RV_X0: i64 = 0
36const RV_T0: i64 = 5
37const RV_T1: i64 = 6
38const RV_T3: i64 = 28
39const RV_T4: i64 = 29
40const RV_T5: i64 = 30

functions

42func st_lui(rd: i64, imm20: i64) -> i64 { return ((imm20 & 0xFFFFF) << 12) | (rd << 7) | 0x37 }
43func st_addi(rd: i64, rs1: i64, imm: i64) -> i64 { return ((imm & 0xFFF) << 20) | (rs1 << 15) | (rd << 7) | 0x13 }
44func st_store(rs2: i64, rs1: i64, f3: i64, imm: i64) -> i64
called by 2: st_emit_strst_fin
49func st_branch(rs1: i64, rs2: i64, f3: i64, imm: i64) -> i64
called by 1: st_emit_image
56func st_jal(rd: i64, imm: i64) -> i64
called by 1: st_emit_image
63func st_slli(rd: i64, rs1: i64, shamt: i64) -> i64 { return ((shamt & 0x3f) << 20) | (rs1 << 15) | (1 << 12) | (rd << 7) | 0x13 }
called by 1: st_li32u
64func st_srli(rd: i64, rs1: i64, shamt: i64) -> i64 { return ((shamt & 0x3f) << 20) | (rs1 << 15) | (5 << 12) | (rd << 7) | 0x13 }
called by 1: st_li32u
65func st_csrrw(rd: i64, csr: i64, rs1: i64) -> i64 { return ((csr & 0xfff) << 20) | (rs1 << 15) | (1 << 12) | (rd << 7) | 0x73 }
called by 1: st_emit_image
66func st_csrrs(rd: i64, csr: i64, rs1: i64) -> i64 { return ((csr & 0xfff) << 20) | (rs1 << 15) | (2 << 12) | (rd << 7) | 0x73 }
called by 1: st_emit_image
67func st_w32(buf: *u8, off: i64, w: i64) -> i64 { buf[off]=(w&0xff) as u8; buf[off+1]=((w>>8)&0xff) as u8; buf[off+2]=((w>>16)&0xff) as u8; buf[off+3]=((w>>24)&0xff) as u8; return off+4 }
68func st_li32(buf: *u8, off: i64, rd: i64, val: i64) -> i64
76func st_li32u(buf: *u8, off: i64, rd: i64, val: i64) -> i64
82func st_emit_str(buf: *u8, off: i64, s: *u8, n: i64) -> i64
called by 1: st_emit_image calls 3: st_w32st_addist_store
88func st_fin(buf: *u8, off: i64) -> i64 { // li FIN; li PASS; sw; (caller adds jal)
called by 1: st_emit_image calls 3: st_li32st_w32st_store
94func st_p(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 }
called by 1: main calls 1: sys_write
95func st_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 }
called by 1: main calls 2: sys_mmapsys_write
98func st_emit_image(buf: *u8, user_off: i64, sh_off: i64, nots_off: i64, mh_off: i64, halt_off: i64, pos_out: *i64, medeleg: i64) -> i64
137func st_parse_num(s: *u8) -> i64
called by 1: main
150func main(argc: i64, argv: *i64) -> i64