Why does my program run perfectly fine in QEMU, but crashes in bare metal?

2026-06-03

Stack Overflow: View Question

Tags: assembly, operating-system, kernel, bare-metal

Score: 0 | Views: 167

The asker has a hobby kernel that boots cleanly under QEMU but triple-faults on real hardware right around mov cr0, eax — the instruction that flips the CPU into protected mode. They're loading the kernel at 0x7E00 and chained through Ventoy from a custom ISO.

Why this is the canonical hobby-OS trap. QEMU is forgiving in ways real silicon is not. It tends to start with sane defaults: A20 already enabled, segment caches in a reasonable state, BIOS data areas untouched, and a GDT that "just works" even if your descriptors are slightly wrong. Real hardware, especially via a UEFI-CSM path that Ventoy uses, hands control off in a state that is technically legal but full of footguns.

Likely root causes, ranked by how often I've seen them bite people:

Sketch of a debugging plan:

Gotcha: "It works in QEMU" is not evidence of correctness — it's evidence that QEMU's defaults masked your bug. Always test under Bochs with -q and on at least one real machine before declaring a boot sequence done.

The challenge: Bare-metal boot code lives or dies on assumptions emulators silently grant — diagnosing the gap means treating QEMU as a debugger, not as ground truth.

All newsletters