code wiki / _hdl_build / nx_ifetch_emit.nx

nx_ifetch_emit.nx

buildroot/runtime/_hdl_build/nx_ifetch_emit.nx

11030 B190 linesdepth 2pulls 2 transitivereach 0 importersview sourcekind tool
docsdependenciesstructsconstsfunctions

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

nx_syscalls.nx nx_ifetch_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 ift_parse_num sys_mmap ift_emit_image ift_w32 ift_lui ift_li32u ift_li32 ift_w32 ↻ ift_lui ↻ ift_addi ift_w32 ↻ ift_slli ift_srli ift_csrrw ift_li32 ↻ ift_store ift_addi ↻ ift_slli ↻ ift_or ift_emit_str ift_w32 ↻ ift_addi ↻ ift_store ↻ ift_jal ift_csrrs ift_branch sys_openat_wr ift_p sys_write sys_write ↻ sys_close ift_fn sys_mmap ↻ sys_write ↻ sys_openat_append

structs

none

consts

21const IFT_MAGIC_8192: i64 = 8192
23const IFT_OUT: *u8 = "runtime/_hdl_build/_ifetch_virt.bin"
24const IFT_GOLD: *u8 = "runtime/_hdl_build/_ifetch_virt.bin.gold"
25const IFT_LOG: *u8 = "knowledge/status/paging.log"
27const IFT_UART: i64 = 0x10000000
28const IFT_FIN: i64 = 0x100000
29const IFT_PASS: i64 = 0x5555
30const IFT_MEM_BASE: i64 = 0x80000000
31const IFT_VBASE: i64 = 0xC0000000 // virtual code base -> PA 0x80000000 (gigapage root[VPN2=3])
32const IFT_CSR_SATP: i64 = 0x180
33const IFT_CSR_MSTATUS: i64 = 0x300
34const IFT_CSR_MTVEC: i64 = 0x305
35const IFT_CSR_MEPC: i64 = 0x341
36const IFT_CSR_MCAUSE: i64 = 0x342
37const IFT_MRET: i64 = 0x30200073
38const IFT_MPP_S: i64 = 0x800 // mstatus with MPP=01 (Supervisor)
40const RV_X0: i64 = 0
41const RV_T0: i64 = 5 // UART
42const RV_T1: i64 = 6 // value / satp scratch
43const RV_T2: i64 = 7 // mode scratch
44const RV_T3: i64 = 28 // mcause
45const RV_T4: i64 = 29 // expected cause
46const RV_T5: i64 = 30 // base scratch
48const IFT_PTE_ADDR: i64 = 0x80008000 // root page table
49const IFT_PTE_OFF: i64 = 0x18 // root[VPN2=3] = +3*8 (maps VA 0xC0000000..)
50const IFT_CODE_PTE: i64 = 0x2000000F // gigapage PPN 0x80000, V|R|W|X (leaf; X required to FETCH under perm enforcement)
51const IFT_SATP_PPN: i64 = 0x80008 // root >> 12
52const IFT_SV39: i64 = 8
53const IFT_INSTR_PF_CAUSE: i64 = 12 // instruction-page-fault mcause

functions

55func ift_lui(rd: i64, imm20: i64) -> i64 { return ((imm20 & 0xFFFFF) << 12) | (rd << 7) | 0x37 }
56func ift_addi(rd: i64, rs1: i64, imm: i64) -> i64 { return ((imm & 0xFFF) << 20) | (rs1 << 15) | (rd << 7) | 0x13 }
57func ift_store(rs2: i64, rs1: i64, f3: i64, imm: i64) -> i64
62func ift_branch(rs1: i64, rs2: i64, f3: i64, imm: i64) -> i64
called by 1: ift_emit_image
69func ift_jal(rd: i64, imm: i64) -> i64
called by 1: ift_emit_image
76func ift_slli(rd: i64, rs1: i64, shamt: i64) -> i64 { return ((shamt & 0x3f) << 20) | (rs1 << 15) | (1 << 12) | (rd << 7) | 0x13 }
77func 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
78func 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
79func 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
80func 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
82func ift_w32(buf: *u8, off: i64, w: i64) -> i64
86func ift_li32(buf: *u8, off: i64, rd: i64, val: i64) -> i64
94func ift_li32u(buf: *u8, off: i64, rd: i64, val: i64) -> i64
100func ift_emit_str(buf: *u8, off: i64, s: *u8, n: i64) -> i64
106func 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 }
called by 1: main calls 1: sys_write
107func 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 }
called by 1: main calls 2: sys_mmapsys_write
110func ift_emit_image(buf: *u8, cont_off: i64, handler_off: i64, notif_off: i64, halt_off: i64, pos_out: *i64, code_pte: i64) -> i64
152func ift_parse_num(s: *u8) -> i64
called by 1: main
165func main(argc: i64, argv: *i64) -> i64