first commit

This commit is contained in:
Justus Wolff
2026-02-26 00:06:36 +01:00
commit 115b4c88a1
16 changed files with 750 additions and 0 deletions

28
src/bootload/GDT.asm Normal file
View File

@@ -0,0 +1,28 @@
gdt_start:
dd 0x0 ; 4 byte
dd 0x0 ; 4 byte
gdt_code:
dw 0xffff ; segment length, bits 0-15
dw 0x0 ; segment base, bits 0-15
db 0x0 ; segment base, bits 16-23
db 10011010b ; flags (8 bits)
db 11001111b ; flags (4 bits) + segment length, bits 16-19
db 0x0 ; segment base, bits 24-31
gdt_data:
dw 0xffff
dw 0x0
db 0x0
db 10010010b
db 11001111b
db 0x0
gdt_end:
gdt_descriptor:
dw gdt_end - gdt_start - 1
dd gdt_start
CODE_SEG equ gdt_code - gdt_start
DATA_SEG equ gdt_data - gdt_start

69
src/bootload/boot.asm Normal file
View File

@@ -0,0 +1,69 @@
; constants for multiboot
MAGIC equ 0xE85250D6
ARCH equ 0 ; 0 for i386, 4 for MIPS
HDRLEN equ 24 ; Total length: 4 fields + end tag
CHECKSUM equ -(MAGIC + ARCH + HDRLEN)
section .multiboot_header
align 8
header_start:
dd 0xE85250D6 ; Magic
dd 0 ; Architecture (i386)
dd header_end - header_start ; Length
dd -(0xE85250D6 + 0 + (header_end - header_start)) ; Checksum
; End tag
dw 0 ; Type
dw 0 ; Flags
dd 8 ; Size
header_end:
section .bss
align 16
stack_bottom:
resb 16384 ; 16 KiB is reserved for stack
stack_top:
section .rodata
%include "src/bootload/GDT.asm"
section .text ; entrypoint for bootloader
[BITS 32]
global _start:function (_start.end - _start)
_start:
; To set up a stack, we set the esp register to point to the top of our
; stack (as it grows downwards on x86 systems). This is necessarily done
; in assembly as languages such as C cannot function without a stack.
mov esp, stack_top
mov ebp, esp
mov ecx, eax ; move multiboot magic number to ecx
; TODO:
; load GDT [x]
; enter long m. [ ]
; init FPU [ ]
; load GDT
cli
lgdt [gdt_descriptor]
jmp CODE_SEG:.final ; if I get problems, check this line here, future me!
.final:
mov AX, DATA_SEG ; data segment selectors
mov DS, AX
mov ES, AX
mov FS, AX
mov GS, AX
mov SS, AX
push ecx ; magic number
push ebx ; info structure
extern kernel_main
call kernel_main
add esp, 8 ; remove arguments
cli ; the infamous infinite loop
.hang: hlt
jmp .hang
.end:

55
src/bootload/longm.asm Normal file
View File

@@ -0,0 +1,55 @@
; DEPRECATED!
CPUID_EXTENSIONS equ 0x80000000
CPUID_EXT_FEATURES equ 0x80000001
EFLAGS_ID equ 1 << 21
CPUID_EDX_EXT_FEAT_LM equ 1 << 29
enterlong:
call .queryLongMode
.NoLongMode
cli
hlt
jmp .NoLongMode
.checkCPUID:
pushfd
pop eax
; The original value should be saved for comparison and restoration later
mov ecx, eax
xor eax, EFLAGS_ID
; storing the eflags and then retrieving it again will show whether or not
; the bit could successfully be flipped
push eax ; save to eflags
popfd
pushfd ; restore from eflags
pop eax
; Restore EFLAGS to its original value
push ecx
popfd
; if the bit in eax was successfully flipped (eax != ecx), CPUID is supported.
xor eax, ecx
jnz .NoLongMode
ret
.queryLongMode:
call .checkCPUID
mov eax, CPUID_EXTENSIONS
cpuid
cmp eax, CPUID_FEATURES
jb .NoLongMode
mov eax, CPUID_EXT_FEATURES
cpuid
test edx, CPUID_EDX_EXT_FEAT_LM
jz .NoLongMode
ret