2026-08-19
Stack Overflow: View Question
Tags: assembly, operating-system, x86-64, bootloader
Score: 1 | Views: 101
The asker is writing a school-project x64 bootloader in NASM that transitions CPU from Real Mode → Protected Mode → Long Mode, sets up 4-level paging (PML4/PDPT/PD), loads a C kernel at 0x100000, and jumps to it. The kernel never runs — control transfer fails silently.
This is one of the most unforgiving problems in systems programming because everything must be correct simultaneously before you get any feedback. There's no debugger, no printf, no exception trace — just a hung machine or a triple-fault reboot loop.
CR4.PAE=1), (3) load CR3 with a valid PML4, (4) set EFER.LME=1 via MSR 0xC0000080, (5) enable paging + protection (CR0.PG=1, CR0.PE=1) in the same MOV, then (6) far-jump through a 64-bit code segment descriptor. Skip a step or reorder → #GP or triple fault.jmp 0x100000 works, but any kernel code touching addresses beyond that page-faults with no IDT installed → triple fault.kernel_main compiled with -ffreestanding -mno-red-zone -mcmodel=kernel and linked with a custom linker script placing .text at 0x100000 is required. GCC's default assumes a hosted environment with a red zone — interrupts will clobber stack.INT 13h disk-read (or ATA PIO) that copies kernel sectors to 0x100000 while still in real mode. If the asker jumps to 0x100000 without loading anything there, they execute zeros (ADD [RAX], AL repeatedly) until fault.-d int,cpu_reset -no-reboot -no-shutdown. This dumps the register state at the moment of triple fault — often revealing exactly which instruction faulted and in which mode.qemu-system-x86_64 -s -S then target remote :1234, set architecture i386:x86-64. Step through the mode transition and inspect CR0/CR3/CR4/EFER after each write.0x100000 before the far jump (x/16bx 0x100000 in GDB).Gotcha: A 512-byte MBR bootloader cannot fit the disk-read code, GDT, paging tables, and mode-switch logic. Most working designs use a stage 2 loader that the stage-1 MBR reads off disk first.
