A small x86-64 kernel. Stage 0 goes from real mode to long mode inside the boot sector.
Real mode straight to long modeAug 27
1; boot.asm — Kestrel stage 0: from real mode to long mode in one sector.
3; The BIOS loads this sector at 0x7C00 and jumps to it in 16-bit real mode.
Real mode straight to long modeAug 27
4; We enable A20, identity-map the first 2 MiB with a single huge page, switch
Print the banner from 64-bit codeSep 5
5; on PAE, long mode and paging in one step, and land in 64-bit code that
6; writes the banner straight into VGA text memory.
8; nasm -f bin boot.asm -o boot.bin
9; qemu-system-x86_64 -drive format=raw,file=boot.bin
11 bits 16
12 org 0x7C00
Identity-map the first 2 MiB with one huge pageAug 8
14PML4 equ 0x1000 ; page tables live in free low memory
15PDPT equ 0x2000
16PD equ 0x3000
17PRESENT_WRITABLE equ 0x03
18HUGE equ 0x80
21 cli
23 mov ds, ax
24 mov es, ax
25 mov ss, ax
26 mov sp, 0x7C00
27 cld
29 in al, 0x92 ; fast A20 gate
30 or al, 2
35 mov cx, 4096 / 2
36 xor ax, ax
37 rep stosw
39 mov cx, 4096 / 2
40 rep stosw
42 mov cx, 4096 / 2
43 rep stosw
44 mov word [PML4], PDPT | PRESENT_WRITABLE
45 mov word [PDPT], PD | PRESENT_WRITABLE
46 mov word [PD], HUGE | PRESENT_WRITABLE ; 0..2 MiB, identity
49 mov cr4, eax
51 mov cr3, edx
52 mov ecx, 0xC0000080 ; EFER
53 rdmsr
54 or eax, 1 << 8 ; EFER.LME
55 wrmsr
56 mov ebx, cr0
57 or ebx, 0x80000001 ; CR0.PG | CR0.PE, together
58 mov cr0, ebx
59 lgdt [gdt.pointer]
62 bits 64
63long_mode:
65 mov ds, ax
66 mov es, ax
67 mov ss, ax
69 mov edi, 0xB8000 + (12 * 80 + 36) * 2 ; row 12, centred
70 mov ah, 0x07 ; light grey on black
71.print: lodsb
72 test al, al
74 stosw
82gdt: dq 0
84 dq 0x00AF9A000000FFFF ; 64-bit code, ring 0
86 dq 0x00CF92000000FFFF ; data, ring 0
87.pointer:
92 dw 0xAA55 ; boot signature