A real-mode → long-mode boot chain written entirely in Forge — no inline
assembly anywhere. It is the reference implementation of the
x86_64 raw kernel target and lives in
examples/os64/.
| Stage | Source | Output | Sectors (LBA) | Loaded at |
|---|---|---|---|---|
| 1 — boot sector | src/boot/boot.dev |
build/boot.bin |
0 | 0x7C00 (BIOS) |
| 2 — loader | src/boot/loader.dev |
build/loader.raw |
1–2 | 0x9000 |
| 3 — kernel | src/kernel/kernel.dev |
build/kernel.raw |
3–10 | 0x100000 (physical) |
_dev_bios_disk_read (INT 13h CHS)
and far-jumps to the loader with _dev_jump.0x8000 via
INT 13h AH=42h (_dev_bios_disk_read_lba) — the same low-1 MiB limitation
as ForgeOS32._dev_enter_long_mode(lo, hi) runs the entire 16→32→64 switch as a single
codegen16 builtin:
CR0.PE, far jump to a 32-bit trampoline0x8000 to
0x100000 and stashes the entry at 0x8FF8PDPT[0] = 0x83, which also satisfies the PAE 3-level transition
window)CR3 / CR4.PAE / EFER.LME via wrmsr, 64-bit GDT + lgdt,
CR0.PG, far jmp to a 64-bit code segmentmov rax,0x90000; mov rax,[0x8FF8]; jmp rax
— landing at the kernel’s _start (linked at 0x100000)Before switching, the builtin probes CPUID for long-mode support (leaf
0x80000001’s LM bit, guarded by the max-extended-leaf check) and, on a
32-bit-only CPU, prints No 64-bit CPU over COM1 and halts instead of
triple-faulting.
cargo build --release
./examples/os64/build.sh # assemble build/os64.img
./examples/os64/build.sh run # ...and boot it in QEMU
qemu-system-x86_64 -accel tcg,thread=multi \
-drive file=examples/os64/build/os64.img,format=raw,if=ide -nographic
Expected output:
ForgeOS64: loading kernel: ok
ForgeOS64: hello from 64-bit long mode
The kernel then halts.
os64-loader.fld — ARCH x86_16 / FORMAT raw / LOAD 0x9000os64-kernel.fld — ARCH x86_64 / FORMAT raw / ENTRY _start /
LOAD 0x100000 (the kernel link base)The kernel alone can be built with:
./target/release/forgec kernel.dev -o kernel.raw --linker examples/os64/os64-kernel.fld
The kernel uses std.hal for its serial and VGA
setup — port I/O through typed wrappers, no extern declarations.
QEMU integration tests cover the full chain:
os64_boot_chain_enters_long_mode boots the three-stage image and asserts
the 64-bit banner; os64_32bit_cpu_guard_halts verifies the CPUID guard
falls back gracefully on a 32-bit-only CPU. They need
qemu-system-x86_64 on PATH.