rk/multiboot.asm

72 lines
1.2 KiB
NASM
Raw Normal View History

2024-05-20 05:59:07 -04:00
%define MAGIC 0x1BADB002
%define FLAGS 0
; Multiboot v1 Specification
; https://www.gnu.org/software/grub/manual/multiboot/multiboot.html
section .multiboot
align 4
dd MAGIC
dd FLAGS
dd -(MAGIC + FLAGS)
section .text
global _start
_start:
cli
; Check if EAX contains the multiboot magic number the bootloader is supposed to give us
cmp eax, 0x2BADB002
jne halt
lgdt [gdtp]
mov ax, 0x10 ; Offset to kernel data descriptor
mov es, ax
mov ds, ax
mov fs, ax
mov gs, ax
mov ss, ax
jmp 0x8:.use_code_seg ; Offset to kernel code descriptor
.use_code_seg:
mov esp, stack_bottom
push ebx
extern kmain
call kmain
; If the kernel function somehow returns, disable interrupts and hang
cli
halt:
hlt
jmp halt
; GDT for a flat memory layout
; We get access to all memory and can utilize paging
gdt_start:
gdt_null:
dq 0
gdt_kern_code:
dw 0xffff
dw 0x0000
db 0x00
db 0b10011010
db 0b11001111
db 0x00
gdt_kern_data:
dw 0xffff
dw 0x0000
db 0x00
db 0b10010010
db 0b11001111
db 0x00
gdt_end:
gdtp:
dw gdt_end - gdt_start - 1
dd gdt_start
; Reserve 16KiB of kernel stack space
section .bss
align 4
stack_top:
resb 16384
stack_bottom: