acompiled

The catalog contains seeded project examples. Your workspace stores your own projects and conversations.

kestrel-os/kestrel

Public
Star8

A small x86-64 kernel. Stage 0 goes from real mode to long mode inside the boot sector.

Code
Blame·3 commits across 50 linesoldernewer
Boot sector skeleton and signatureJul 23 1; boot.asm — Kestrel stage 0.
2;
3; The BIOS loads this sector at 0x7C00 and jumps to it in 16-bit real mode.
Identity-map the first 2 MiB with one huge pageAug 8 4; We enable A20 and identity-map the first 2 MiB with a single huge page,
5; ready for the switch to long mode.
7; nasm -f bin boot.asm -o boot.bin
8; qemu-system-x86_64 -drive format=raw,file=boot.bin
9
10 bits 16
11 org 0x7C00
12
Identity-map the first 2 MiB with one huge pageAug 8 13PML4 equ 0x1000 ; page tables live in free low memory
14PDPT equ 0x2000
15PD equ 0x3000
16PRESENT_WRITABLE equ 0x03
17HUGE equ 0x80
18
20 cli
Fast A20 through port 0x92Aug 1 21 xor ax, ax
22 mov ds, ax
23 mov es, ax
24 mov ss, ax
25 mov sp, 0x7C00
26 cld
27
28 in al, 0x92 ; fast A20 gate
29 or al, 2
30 out 0x92, al
31
Identity-map the first 2 MiB with one huge pageAug 8 32 mov di, PML4 ; clear the PML4
33 mov cx, 4096 / 2
34 xor ax, ax
35 rep stosw
36 mov di, PDPT ; clear the PDPT
37 mov cx, 4096 / 2
38 rep stosw
39 mov di, PD ; clear the PD
40 mov cx, 4096 / 2
41 rep stosw
42 mov word [PML4], PDPT | PRESENT_WRITABLE
43 mov word [PDPT], PD | PRESENT_WRITABLE
44 mov word [PD], HUGE | PRESENT_WRITABLE ; 0..2 MiB, identity
45
47 jmp .halt
48
49 times 510 - ($ - $$) db 0
50 dw 0xAA55 ; boot signature