code wiki / _hdl_build / nx_ifetch_emit.nx
nx_ifetch_emit.nx
buildroot/runtime/_hdl_build/nx_ifetch_emit.nx
about
nx_ifetch_emit.nx -- INSTRUCTION-FETCH translation test (virtual-memory-paging-mmu, CPU-datapath
completion rung). The data-load/store rung proved the CPU translates DATA addresses under Sv39;
this proves the CPU also translates the INSTRUCTION FETCH -- i.e. the PC itself is virtual in
S/U-mode and the next instruction is read through the page table.
Table-computes a bare-metal rv64 image:
1. M-mode (physical PC): install mtvec=&handler; lay a gigapage code PTE root[VPN2=3]
(VA 0xC0000000.. -> PA 0x80000000..); csrrw satp (Sv39 ON); mepc = the VIRTUAL address
0xC0000000+cont_off; mstatus.MPP = S; mret.
2. mret lands the PC at a VIRTUAL address -> the CPU FETCHES the continuation through the page
table (VA 0xC00000xx -> PA 0x800000xx) and runs it in S-mode -> emits "IOK". VA != PA, so
"IOK" can only print if the fetch was genuinely translated (not a physical read of the PC).
3. CONTROL (gate re-emits with the code PTE's Valid bit cleared, 0x20000006): the mret targets an
UNMAPPED virtual PC -> the FETCH walk faults -> instruction-page-fault (mcause=12) -> vectors
to mtvec (the handler is fetched physically in M-mode) -> the handler confirms mcause==12 and
emits "IPF". "IPF" (and the absence of "IOK") proves the fetch faults on a bad code page.
Zero hand-written machine code: a tiny rv64 encoder + two-pass forward-branch resolution.
nx_ifetch_emit -> runtime/_hdl_build/_ifetch_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
| 21 | const IFT_MAGIC_8192: i64 = 8192 |
| 23 | const IFT_OUT: *u8 = "runtime/_hdl_build/_ifetch_virt.bin" |
| 24 | const IFT_GOLD: *u8 = "runtime/_hdl_build/_ifetch_virt.bin.gold" |
| 25 | const IFT_LOG: *u8 = "knowledge/status/paging.log" |
| 27 | const IFT_UART: i64 = 0x10000000 |
| 28 | const IFT_FIN: i64 = 0x100000 |
| 29 | const IFT_PASS: i64 = 0x5555 |
| 30 | const IFT_MEM_BASE: i64 = 0x80000000 |
| 31 | const IFT_VBASE: i64 = 0xC0000000 // virtual code base -> PA 0x80000000 (gigapage root[VPN2=3]) |
| 32 | const IFT_CSR_SATP: i64 = 0x180 |
| 33 | const IFT_CSR_MSTATUS: i64 = 0x300 |
| 34 | const IFT_CSR_MTVEC: i64 = 0x305 |
| 35 | const IFT_CSR_MEPC: i64 = 0x341 |
| 36 | const IFT_CSR_MCAUSE: i64 = 0x342 |
| 37 | const IFT_MRET: i64 = 0x30200073 |
| 38 | const IFT_MPP_S: i64 = 0x800 // mstatus with MPP=01 (Supervisor) |
| 40 | const RV_X0: i64 = 0 |
| 41 | const RV_T0: i64 = 5 // UART |
| 42 | const RV_T1: i64 = 6 // value / satp scratch |
| 43 | const RV_T2: i64 = 7 // mode scratch |
| 44 | const RV_T3: i64 = 28 // mcause |
| 45 | const RV_T4: i64 = 29 // expected cause |
| 46 | const RV_T5: i64 = 30 // base scratch |
| 48 | const IFT_PTE_ADDR: i64 = 0x80008000 // root page table |
| 49 | const IFT_PTE_OFF: i64 = 0x18 // root[VPN2=3] = +3*8 (maps VA 0xC0000000..) |
| 50 | const IFT_CODE_PTE: i64 = 0x2000000F // gigapage PPN 0x80000, V|R|W|X (leaf; X required to FETCH under perm enforcement) |
| 51 | const IFT_SATP_PPN: i64 = 0x80008 // root >> 12 |
| 52 | const IFT_SV39: i64 = 8 |
| 53 | const IFT_INSTR_PF_CAUSE: i64 = 12 // instruction-page-fault mcause |
functions
| 55 | func ift_lui(rd: i64, imm20: i64) -> i64 { return ((imm20 & 0xFFFFF) << 12) | (rd << 7) | 0x37 } |
| 56 | func ift_addi(rd: i64, rs1: i64, imm: i64) -> i64 { return ((imm & 0xFFF) << 20) | (rs1 << 15) | (rd << 7) | 0x13 } |
| 57 | func ift_store(rs2: i64, rs1: i64, f3: i64, imm: i64) -> i64 |
| 62 | func ift_branch(rs1: i64, rs2: i64, f3: i64, imm: i64) -> i64 called by 1: ift_emit_image |
| 69 | func ift_jal(rd: i64, imm: i64) -> i64 called by 1: ift_emit_image |
| 76 | func ift_slli(rd: i64, rs1: i64, shamt: i64) -> i64 { return ((shamt & 0x3f) << 20) | (rs1 << 15) | (1 << 12) | (rd << 7) | 0x13 } |
| 77 | func ift_srli(rd: i64, rs1: i64, shamt: i64) -> i64 { return ((shamt & 0x3f) << 20) | (rs1 << 15) | (5 << 12) | (rd << 7) | 0x13 } called by 1: ift_li32u |
| 78 | func ift_or(rd: i64, rs1: i64, rs2: i64) -> i64 { return (rs2 << 20) | (rs1 << 15) | (6 << 12) | (rd << 7) | 0x33 } called by 1: ift_emit_image |
| 79 | func ift_csrrw(rd: i64, csr: i64, rs1: i64) -> i64 { return ((csr & 0xfff) << 20) | (rs1 << 15) | (1 << 12) | (rd << 7) | 0x73 } called by 1: ift_emit_image |
| 80 | func ift_csrrs(rd: i64, csr: i64, rs1: i64) -> i64 { return ((csr & 0xfff) << 20) | (rs1 << 15) | (2 << 12) | (rd << 7) | 0x73 } called by 1: ift_emit_image |
| 82 | func ift_w32(buf: *u8, off: i64, w: i64) -> i64 |
| 86 | func ift_li32(buf: *u8, off: i64, rd: i64, val: i64) -> i64 |
| 94 | func ift_li32u(buf: *u8, off: i64, rd: i64, val: i64) -> i64 |
| 100 | func ift_emit_str(buf: *u8, off: i64, s: *u8, n: i64) -> i64 |
| 106 | func ift_p(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 } |
| 107 | func ift_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 } |
| 110 | func ift_emit_image(buf: *u8, cont_off: i64, handler_off: i64, notif_off: i64, halt_off: i64, pos_out: *i64, code_pte: i64) -> i64 |
| 152 | func ift_parse_num(s: *u8) -> i64 called by 1: main |
| 165 | func main(argc: i64, argv: *i64) -> i64 |