code wiki / _hdl_build / nx_nishi_usb_paging.nx
nx_nishi_usb_paging.nx
buildroot/runtime/_hdl_build/nx_nishi_usb_paging.nx
about
nx_nishi_usb_paging.nx -- x86 ladder R-KERN-2: the long-mode kernel sets up PAGING (CR3 + 4-level walk).
Second OS-internals rung at the long-mode entry. The kernel LOADED OFF the persisted image, after it
reaches long mode, BUILDS a 4-level page-table tree IN MEMORY with its own x86 stores (PML4 -> PDPT ->
PD -> PT), loads CR3 with `mov cr3, rax`, and HLTs. The emu then performs the long-mode 4-level page
walk (VA[47:39]->PML4, [38:30]->PDPT, [29:21]->PD, [20:12]->PT, +[11:0] offset; bit0=present) over the
tables the kernel built, and reports the translations -- proving the kernel's page tables are real and
walkable. pg_walk is reused verbatim from the proven nx_emu_x86_paging_test (the x86 twin of our rv64
SV39 MMU); the unified emu is extended with `mov cr3` (0F 22 /3).
KAT: (T1) booted off disk; (T2) LONG mode; (T3) CR3 loaded with the kernel's PML4 base; (T4) VA 0x1000
translates to PA 0x40000 through the kernel-built tables; (T5) offset preserved (0x1123->0x40123);
(T6) a second mapping (0x2000->0x50000); (T7) an unmapped VA faults. NEG/liar-kill (T8): a sibling
image whose CR3 is loaded with a WRONG base translates nothing -- so the translations depend on the
kernel having built the tables AND loaded the right CR3, not on a constant.
HONEST SCOPE: 4KB pages + present bit; long mode is modeled flat so the emu walks the kernel's tables
on demand but does not yet route every instruction fetch through the TLB (large pages / A-D bits /
permission faults / full MMU routing = refinements). NEVER-BRICK (Rule 26): writes a FILE; INT 13h
*reads* only; no /dev. expect_exit: 0 license_tier: ORIGINAL
dependencies 2 imports · 0 importers
imports: nx_syscalls.nxnx_itoa_lib.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
| 23 | const IMG_MAGIC_2000000: i64 = 2000000 |
| 24 | const IMG_MAGIC_32767: i64 = 32767 |
| 25 | const IMG_MAGIC_65536: i64 = 65536 |
| 129 | const IMG_SZ: i64 = 1024 |
functions
| 27 | func pg_puts(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 } |
| 32 | func pg_num(v: i64) -> i64 { nxi_out(v); return 0 } |
| 33 | func pg_mode(cr0: i64, efer: i64) -> i64 { let pe: i64=cr0&1; let pg: i64=(cr0>>31)&1; let lme: i64=(efer>>8)&1; if pe==0 { return 0 } if pg==1 { if lme==1 { return 2 } } return 1 } called by 1: emu_x86_boot_page |
| 34 | func pg_st64(mem: *u8, a: i64, v: i64) -> i64 { var i: i64=0; while i<8 { mem[a+i]=((v>>(i*8))&0xff) as u8; i=i+1 } return 0 } called by 1: emu_x86_boot_page |
| 35 | func pg_rd(mem: *u8, a: i64) -> i64 { var v: i64=0; var i: i64=0; while i<8 { v=v|((mem[a+i] as i64)<<(i*8)); i=i+1 } return v } called by 1: pg_walk |
| 38 | func pg_walk(mem: *u8, cr3: i64, va: i64) -> i64 |
| 60 | func emu_x86_boot_page(mem: *u8, disk: *u8, entry: i64, console: *u8, clen: *i64, st: *i64) -> i64 |
| 131 | func uf_movr(img: *u8, base: i64, o: i64, modrm: i64, imm: i64) -> i64 |
| 138 | func emit_pte(img: *u8, base: i64, o: i64, val: i64, addr: i64) -> i64 |
| 147 | func build_image(img: *u8, info: *i64) -> i64 |
| 201 | func boot_image(img: *u8, console: *u8, clen: *i64, st: *i64, loaded: *i64) -> i64 |
| 214 | func pg_read(path: *u8, out: *u8, cap: i64) -> i64 |
| 223 | func main() -> i64 |