code wiki / _hdl_build / nx_nishi_usb_longmode.nx
nx_nishi_usb_longmode.nx
buildroot/runtime/_hdl_build/nx_nishi_usb_longmode.nx
about
nx_nishi_usb_longmode.nx -- x86 ladder R9-HANDOFF: the loaded kernel boots THROUGH to LONG mode.
The previous rung (nx_nishi_usb_image) proved a persisted multi-sector image boots MBR->loader->
kernel, but every stage was 16-bit real-mode. THIS rung crosses the seam the OS ladder has been
stuck at: the kernel that the MBR loads OFF DISK now performs the real -> protected -> LONG mode
transition (GDT-less minimal model: CR0.PE, then EFER.LME via WRMSR + CR0.PG) and the emu tracks it.
It fuses two previously-separate emus into ONE unified boot-disk emu:
* the 16-bit real-mode + BIOS INT 13h/INT 10h loader (from nx_emu_x86_stage2 / nx_nishi_usb_image)
* the CR0/EFER mode-transition decode (from nx_emu_x86_boot_kernel / nx_nishios_boot_gui)
The two opcode sets are disjoint (0xBE/0xB8/0xCD... vs 0x48 0xC7 / 0x0F 0x22 / 0x0F 0x30), so one
flat decoder runs both and reports the final CPU mode.
Boot flow, executed off the PERSISTED on-disk bytes:
sector 0 (MBR) --INT 13h AH=02--> loads sector 1 (the kernel) to 0x8000, jmps to it
sector 1 (kernel) prints "NishiOS" over INT 10h, then: mov cr0,1 (->PROTECTED)
wrmsr EFER.LME=1 ; mov cr0,0x80000001 (PE|PG) -> LONG mode ; HLT
KAT: (T1) persisted byte-faithful + 0x55AA; (T2) INT 13h loaded the kernel off disk (0x8000 0->0xBE);
(T3) console banner == 'NishiOS'; (T4) the LOADED kernel reached LONG mode (mode==2); (T5) clean HLT;
(T6) NEG/liar-kill: a sibling image whose MBR loads ZERO sectors never runs the kernel, so it NEVER
reaches long mode and prints no banner -- the long-mode result MUST come from the loaded kernel.
HONEST SEAM (no overclaim, same as the boot-kernel organs): the emu EXECUTES the loader + the
CR0/EFER transition as real x86 and TRACKS the mode; it does not yet model a real GDT/IDT/paging
table walk, nor decode-width change by mode. The next rung is loading the real 64-bit desktop kernel
(nx_nishios_boot_gui) at the long-mode entry instead of a banner stub, then GDT/IDT/paging.
NEVER-BRICK (Rule 26): writes a FILE (knowledge/status/nishi_os_lm.img); models INT 13h *reads* only;
no /dev, no firmware. 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
| 32 | const IMG_MAGIC_200000: i64 = 200000 |
| 33 | const IMG_MAGIC_32767: i64 = 32767 |
| 34 | const IMG_MAGIC_65536: i64 = 65536 |
| 126 | const IMG_SZ: i64 = 1024 // 2 sectors: MBR + the long-mode kernel |
functions
| 36 | func ul_puts(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 } |
| 41 | func ul_num(v: i64) -> i64 { nxi_out(v); return 0 } |
| 42 | func ul_beq(a: *u8, b: *u8, n: i64) -> i64 { var i: i64=0; while i<n { if a[i]!=b[i] { return 0 } i=i+1 } return 1 } called by 1: main |
| 43 | func ul_contains(hay: *u8, hn: i64, ndl: *u8, nn: i64) -> i64 called by 1: main |
| 56 | func ul_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_disk |
| 60 | func emu_x86_boot_disk(mem: *u8, disk: *u8, entry: i64, console: *u8, clen: *i64, st: *i64) -> i64 |
| 129 | func ul_movrax(img: *u8, base: i64, o: i64, b0: i64, b1: i64, b2: i64, b3: i64) -> i64 called by 1: build_image |
| 135 | func build_image(img: *u8) -> i64 |
| 183 | func boot_image(img: *u8, console: *u8, clen: *i64, st: *i64, loaded: *i64) -> i64 |
| 196 | func ul_read(path: *u8, out: *u8, cap: i64) -> i64 |
| 209 | func main() -> i64 |