87 lines
1.5 KiB
NASM
87 lines
1.5 KiB
NASM
%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
|
|
mov ebp, esp
|
|
push ebx
|
|
extern kmain
|
|
call kmain
|
|
|
|
; If the kernel function somehow returns, disable interrupts and hang
|
|
cli
|
|
halt:
|
|
hlt
|
|
jmp halt
|
|
global exception_handler
|
|
exception_handler:
|
|
add esp, 4
|
|
mov byte [0xb8000], 'X'
|
|
iret
|
|
global ps2_isr
|
|
ps2_isr:
|
|
extern ps2_handler
|
|
pushad
|
|
cld
|
|
call ps2_handler
|
|
popad
|
|
iret
|
|
|
|
; 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 16
|
|
stack_top:
|
|
resb 16384
|
|
stack_bottom:
|