code wiki / _hdl_build / nx_priv_emit.nx
nx_priv_emit.nx
buildroot/runtime/_hdl_build/nx_priv_emit.nx
about
nx_priv_emit.nx -- M/S/U PRIVILEGE + privilege-gated paging proof (privilege-levels capability).
Proves the riscv-privileged-spec rule that Sv39 translation applies ONLY in S/U mode (M-mode is
always Bare) AND that mret transitions M->S. Same VA, two privilege modes, two results:
1. M-mode: program a valid page table + satp(Sv39); load VA 0xC0009000 -> priv=M so Bare
(identity) -> reads physical 0xC0009000 (out of guest RAM) -> 0 != sentinel -> emit "MID"
(M-mode did NOT translate; if it HAD, beq -> "MX" = RED).
2. mret to S-mode (mepc=&cont, mstatus.MPP=S); in S-mode load the SAME VA 0xC0009000 -> priv=S
so it TRANSLATES -> PA 0x80009000 -> sentinel -> emit "SOK" (else "SX").
Success transcript "MIDSOK": the identical VA is identity in M and translated in S = translation
is privilege-gated, and mret M->S works. Zero hand-written machine code; two-pass branch resolve.
nx_priv_emit -> runtime/_hdl_build/_priv_virt.bin + .gold
Sovereign, 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
| 15 | const PV_MAGIC_8192: i64 = 8192 |
| 17 | const PV_OUT: *u8 = "runtime/_hdl_build/_priv_virt.bin" |
| 18 | const PV_GOLD: *u8 = "runtime/_hdl_build/_priv_virt.bin.gold" |
| 19 | const PV_LOG: *u8 = "knowledge/status/priv.log" |
| 21 | const PV_UART: i64 = 0x10000000 |
| 22 | const PV_FIN: i64 = 0x100000 |
| 23 | const PV_PASS: i64 = 0x5555 |
| 24 | const PV_MEM_BASE: i64 = 0x80000000 |
| 25 | const PV_CSR_SATP: i64 = 0x180 |
| 26 | const PV_CSR_MSTATUS: i64 = 0x300 |
| 27 | const PV_CSR_MEPC: i64 = 0x341 |
| 28 | const PV_MRET: i64 = 0x30200073 |
| 29 | const PV_MPP_S: i64 = 0x800 |
| 31 | const RV_X0: i64 = 0 |
| 32 | const RV_T0: i64 = 5 |
| 33 | const RV_T1: i64 = 6 |
| 34 | const RV_T2: i64 = 7 |
| 35 | const RV_T3: i64 = 28 |
| 36 | const RV_T4: i64 = 29 |
| 37 | const RV_T5: i64 = 30 |
| 39 | const PV_PTE_ADDR: i64 = 0x80008000 |
| 40 | const PV_PTE_OFF: i64 = 0x18 |
| 41 | const PV_PTE_VAL: i64 = 0x20000007 |
| 42 | const PV_SENT_PA: i64 = 0x80009000 |
| 43 | const PV_SENT_VAL: i64 = 0x5ECA1234 |
| 44 | const PV_VA: i64 = 0xC0009000 |
| 45 | const PV_SATP_PPN: i64 = 0x80008 |
| 46 | const PV_SV39: i64 = 8 |
functions
| 48 | func pv_lui(rd: i64, imm20: i64) -> i64 { return ((imm20 & 0xFFFFF) << 12) | (rd << 7) | 0x37 } |
| 49 | func pv_addi(rd: i64, rs1: i64, imm: i64) -> i64 { return ((imm & 0xFFF) << 20) | (rs1 << 15) | (rd << 7) | 0x13 } |
| 50 | func pv_load(rd: i64, rs1: i64, f3: i64, imm: i64) -> i64 { return ((imm & 0xFFF) << 20) | (rs1 << 15) | (f3 << 12) | (rd << 7) | 0x03 } called by 1: pv_emit_image |
| 51 | func pv_store(rs2: i64, rs1: i64, f3: i64, imm: i64) -> i64 |
| 56 | func pv_branch(rs1: i64, rs2: i64, f3: i64, imm: i64) -> i64 called by 1: pv_emit_image |
| 63 | func pv_jal(rd: i64, imm: i64) -> i64 called by 1: pv_emit_image |
| 70 | func pv_slli(rd: i64, rs1: i64, shamt: i64) -> i64 { return ((shamt & 0x3f) << 20) | (rs1 << 15) | (1 << 12) | (rd << 7) | 0x13 } |
| 71 | func pv_srli(rd: i64, rs1: i64, shamt: i64) -> i64 { return ((shamt & 0x3f) << 20) | (rs1 << 15) | (5 << 12) | (rd << 7) | 0x13 } called by 1: pv_li32u |
| 72 | func pv_or(rd: i64, rs1: i64, rs2: i64) -> i64 { return (rs2 << 20) | (rs1 << 15) | (6 << 12) | (rd << 7) | 0x33 } called by 1: pv_emit_image |
| 73 | func pv_csrrw(rd: i64, csr: i64, rs1: i64) -> i64 { return ((csr & 0xfff) << 20) | (rs1 << 15) | (1 << 12) | (rd << 7) | 0x73 } called by 1: pv_emit_image |
| 74 | func pv_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 } |
| 75 | func pv_li32(buf: *u8, off: i64, rd: i64, val: i64) -> i64 |
| 83 | func pv_li32u(buf: *u8, off: i64, rd: i64, val: i64) -> i64 |
| 89 | func pv_emit_str(buf: *u8, off: i64, s: *u8, n: i64) -> i64 |
| 95 | func pv_p(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 } |
| 96 | func pv_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 } |
| 99 | func pv_emit_image(buf: *u8, mfail_off: i64, sfail_off: i64, halt_off: i64, cont_off: i64, pos_out: *i64, mpp: i64) -> i64 |
| 155 | func pv_parse_num(s: *u8) -> i64 called by 1: main |
| 168 | func main(argc: i64, argv: *i64) -> i64 |