A small x86-64 kernel. Stage 0 goes from real mode to long mode inside the boot sector.
Real mode straight to long mode
Browse filesCR4.PAE, CR3, EFER.LME, then CR0.PG and CR0.PE in a single write. QEMU takes the far jump without a stop in protected mode on the way.
failing
1 step in 1 ms
1 file changed
+33−3
@@ -1,8 +1,8 @@
1−; boot.asm — Kestrel stage 0.
1+; boot.asm — Kestrel stage 0: from real mode to long mode in one sector.
22 ;
33 ; The BIOS loads this sector at 0x7C00 and jumps to it in 16-bit real mode.
4−; We enable A20 and identity-map the first 2 MiB with a single huge page,
5−; ready for the switch to long mode.
4+; We enable A20, identity-map the first 2 MiB with a single huge page, switch
5+; on PAE, long mode and paging in one step, and land in 64-bit code.
66 ;
77 ; nasm -f bin boot.asm -o boot.bin
88 ; qemu-system-x86_64 -drive format=raw,file=boot.bin
@@ -44,8 +44,38 @@ start:
4444 mov word [PDPT], PD | PRESENT_WRITABLE
4545 mov word [PD], HUGE | PRESENT_WRITABLE ; 0..2 MiB, identity
4646
47+ mov eax, 0b10100000 ; CR4.PAE | CR4.PGE
48+ mov cr4, eax
49+ mov edx, PML4
50+ mov cr3, edx
51+ mov ecx, 0xC0000080 ; EFER
52+ rdmsr
53+ or eax, 1 << 8 ; EFER.LME
54+ wrmsr
55+ mov ebx, cr0
56+ or ebx, 0x80000001 ; CR0.PG | CR0.PE, together
57+ mov cr0, ebx
58+ lgdt [gdt.pointer]
59+ jmp gdt.code:long_mode ; far jump loads the 64-bit code segment
60+
61+ bits 64
62+long_mode:
63+ mov ax, gdt.data
64+ mov ds, ax
65+ mov es, ax
66+ mov ss, ax
4767 .halt: hlt
4868 jmp .halt
4969
70+ align 8
71+gdt: dq 0
72+.code: equ $ - gdt
73+ dq 0x00AF9A000000FFFF ; 64-bit code, ring 0
74+.data: equ $ - gdt
75+ dq 0x00CF92000000FFFF ; data, ring 0
76+.pointer:
77+ dw $ - gdt - 1
78+ dd gdt
79+
5080 times 510 - ($ - $$) db 0
5181 dw 0xAA55 ; boot signature