rk/kernel/multiboot.asm

214 lines
3.2 KiB
NASM
Raw Normal View History

2024-05-20 05:59:07 -04:00
%define MAGIC 0x1BADB002
%define FLAGS 0b110
2024-05-20 05:59:07 -04:00
; Multiboot v1 Specification
; https://www.gnu.org/software/grub/manual/multiboot/multiboot.html
section .multiboot
align 4
dd MAGIC
dd FLAGS
dd -(MAGIC + FLAGS)
2024-05-23 01:31:32 -04:00
dd 0
dd 0
dd 0
dd 0
dd 0
dd 1
dd 80
dd 25
dd 0
2024-05-20 05:59:07 -04:00
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
2024-05-21 22:07:04 -04:00
mov ebp, esp
2024-05-20 05:59:07 -04:00
push ebx
extern kmain
call kmain
; If the kernel function somehow returns, disable interrupts and hang
cli
halt:
hlt
jmp halt
extern exception_handler
%macro isr_err_stub 1
isr_stub_%+%1:
cli
push %1
pusha
call exception_handler
popa
add esp, 8
iret
%endmacro
%macro isr_no_err_stub 1
isr_stub_%+%1:
cli
push 0
push %1
pusha
call exception_handler
popa
add esp, 12
iret
%endmacro
isr_no_err_stub 0
isr_no_err_stub 1
isr_no_err_stub 2
isr_no_err_stub 3
isr_no_err_stub 4
isr_no_err_stub 5
isr_no_err_stub 6
isr_no_err_stub 7
isr_err_stub 8
isr_no_err_stub 9
isr_err_stub 10
isr_err_stub 11
isr_err_stub 12
isr_err_stub 13
isr_err_stub 14
isr_no_err_stub 15
isr_no_err_stub 16
isr_err_stub 17
isr_no_err_stub 18
isr_no_err_stub 19
isr_no_err_stub 20
isr_no_err_stub 21
isr_no_err_stub 22
isr_no_err_stub 23
isr_no_err_stub 24
isr_no_err_stub 25
isr_no_err_stub 26
isr_no_err_stub 27
isr_no_err_stub 28
isr_no_err_stub 29
isr_err_stub 30
isr_no_err_stub 31
global isr_stub_table
isr_stub_table:
%assign i 0
%rep 32
dd isr_stub_%+i
%assign i i+1
%endrep
2024-05-21 22:07:04 -04:00
global ps2_isr
ps2_isr:
extern ps2_handler
pushad
cld
call ps2_handler
popad
iret
2024-05-20 05:59:07 -04:00
global test
test:
push ebp
mov ebp, esp
mov eax, 1
mov edi, 0
mov esi, buf
int 0x80
jmp $
buf: db "Hello from stdout!", 0
global syscall_isr
syscall_isr:
xchg bx, bx
pushad
mov ecx, esp
sysenter
after_syscall:
popad
iret
global syscall
syscall:
imul eax, eax, 4
add eax, syscall_table
pushad
call [eax]
popad
mov edx, after_syscall
sysexit
2024-05-20 05:59:07 -04:00
; GDT for a flat memory layout
; We get access to all memory and can utilize paging
global gdt
gdt:
2024-05-20 05:59:07 -04:00
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_user_code:
dw 0xffff
dw 0x0000
db 0x00
db 0b11111010
db 0b11001111
db 0x00
gdt_user_data:
dw 0xffff
dw 0x0000
db 0x00
db 0b11110010
db 0b11001111
db 0x00
tss:
dq 0
2024-05-20 05:59:07 -04:00
gdt_end:
gdtp:
dw gdt_end - gdt - 1
dd gdt
2024-05-20 05:59:07 -04:00
global syscall_table
syscall_table:
extern syscall_read
dd syscall_read
extern syscall_write
dd syscall_write
2024-05-21 22:07:04 -04:00
2024-05-20 05:59:07 -04:00
; Reserve 16KiB of kernel stack space
section .bss
2024-05-21 22:07:04 -04:00
align 16
2024-05-20 05:59:07 -04:00
stack_top:
resb 16384
stack_bottom: