This commit is contained in:
nub31
2025-08-31 20:23:52 +02:00
parent cc7b16ee1e
commit b3ceaa5eaf
4 changed files with 68 additions and 55 deletions

View File

@@ -1,5 +1,4 @@
global _start
extern kernel_main
%define FLAGS 0b10
%define MAGIC 0x1BADB002
@@ -11,21 +10,20 @@ section .multiboot
dd FLAGS
dd CHECKSUM
section .bss
align 4096
pml4_table:
resb 4096
pdpt_table:
resb 4096
pd_table:
resb 4096
section .bss
align 16
stack_bottom:
resb 16384
stack_top:
section .bss
align 4096
pml4:
resb 4096
pdpt:
resb 4096
pd:
resb 4096
section .data
align 8
gdt64:
@@ -48,7 +46,7 @@ section .text
cmp eax, 0x2BADB002
jne error
; Check if cpuid is available by flipping the 22-nth youngest bit
; Check if cpuid is available by flipping bit 21
; in the eflags register and checking if the cpu flipped it back
pushfd
pop eax
@@ -74,33 +72,51 @@ section .text
; Check if long mode is available by calling cpuid with 0x80000001
; this will place the extended features of the cpu in edx
; The 30-nth youngest bit tells us if long mode is supported or not
; Bit 29 tells us if long mode is supported or not
mov eax, 0x80000001
cpuid
test edx, 1 << 29
jz error
; todo(nub31): setup paging
; todo(nub31): enter long mode
; Enable PAE by setting bit 5 in cr4 to 1
mov eax, cr4
or eax, 1 << 5
mov cr4, eax
; todo(nub31): Set up page tables
; Load cr3 with the address of pml4
mov eax, pml4
mov cr3, eax
; Load global descriptor table which is set up for 64 bit
lgdt [gdt64.descriptor]
call kernel_main
jmp error
; Enable long mode by setting bit 8 to 1 in EFER (Extended Feature Enable Register)
mov ecx, 0xc0000080
rdmsr
or eax, 1 << 8
wrmsr
; Enable paging bt setting bit 31 in cr0 to 1
mov eax, cr0
or eax, 1 << 31
mov cr0, eax
jmp 0x8:long_mode
error:
cli
mov byte [0xb8000], 'B'
mov byte [0xb8002], 'O'
mov byte [0xb8004], 'O'
mov byte [0xb8006], 'T'
mov byte [0xb8008], ' '
mov byte [0xb800a], 'E'
mov byte [0xb800c], 'R'
mov byte [0xb800e], 'R'
mov byte [0xb8010], 'O'
mov byte [0xb8012], 'R'
hang:
mov byte [0xb8000], 'E'
mov byte [0xb8002], 'R'
mov byte [0xb8004], 'R'
.hang:
hlt
jmp hang
jmp .hang
section .text
bits 64
long_mode:
cli
.hang:
hlt
jmp .hang