hazel/kernel/init.asm

207 lines
3.1 KiB
NASM
Raw Normal View History

2024-06-25 21:43:07 -04:00
%define MAGIC 0x1BADB002
2024-06-27 22:31:22 -04:00
%define ALIGN 1<<0
%define MMAP 1<<1
%define VID 1<<2
%define FLAGS (ALIGN | MMAP | VID)
2024-06-25 15:28:44 -04:00
section .multiboot
dd MAGIC
2024-06-25 21:43:07 -04:00
dd FLAGS
dd -(MAGIC + FLAGS)
2024-06-25 15:28:44 -04:00
2024-06-27 22:31:22 -04:00
dd 0 ; header_addr
dd 0 ; load_addr
dd 0 ; load_end_addr
dd 0 ; bss_end_addr
dd 0 ; entry_addr
dd 1 ; mode_type
dd 80 ; width
dd 25 ; height
dd 0 ; depth
2024-06-25 15:28:44 -04:00
section .text
global _start
_start:
2024-06-27 14:49:11 -04:00
cli
2024-06-25 15:28:44 -04:00
2024-06-27 14:49:11 -04:00
; Check if EAX contains this magic value that our bootloader should've set
cmp eax, 0x2BADB002
jne halt
2024-06-25 22:47:21 -04:00
2024-06-27 14:49:11 -04:00
; Load GDT
2024-06-25 22:47:21 -04:00
lgdt [gdtp]
; Offset to kernel data descriptor
mov ax, 0x10
mov es, ax
mov ds, ax
mov fs, ax
mov gs, ax
mov ss, ax
2024-06-27 14:49:11 -04:00
; Offset to kernel code descriptor
2024-06-25 22:47:21 -04:00
jmp 0x8:.use_code_seg
.use_code_seg:
mov esp, stack_bottom
mov ebp, esp
push ebx
extern kernel
call kernel
2024-06-27 14:49:11 -04:00
cli
2024-06-25 22:47:21 -04:00
halt:
2024-06-27 14:49:11 -04:00
hlt
jmp halt
2024-06-25 22:47:21 -04:00
2024-07-01 19:28:45 -04:00
global pit_isr
pit_isr:
cli
extern pit_handler
call pit_handler
iret
2024-07-03 11:23:21 -04:00
global jmp_user_mode
jmp_user_mode:
mov ax, (4*8) | 3
mov ds, ax
mov es, ax
mov fs, ax
mov gs, ax ; SS is handled by iret
2024-07-03 12:17:32 -04:00
mov eax, [esp+4] ; esp arg
mov edx, [esp+8] ; eip arg
2024-07-03 11:23:21 -04:00
push (4*8) | 3
push eax
pushf
push (3*8) | 3
2024-07-03 15:42:01 -04:00
push edx
2024-07-03 11:23:21 -04:00
iret
2024-07-03 21:55:44 -04:00
global flush_tss
flush_tss:
ltr [esp+4]
ret
global enable_paging
enable_paging:
push eax
mov eax, [esp+4]
mov cr3, eax
mov eax, cr0
or eax, 0x80000000
mov cr0, eax
pop eax
ret
2024-07-01 19:28:45 -04:00
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-06-25 22:47:21 -04:00
section .data
; GDT for a flat memory layout
; We get access to all memory and can utilize paging
global gdt
gdt:
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
2024-07-03 11:23:21 -04:00
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
2024-07-03 21:55:44 -04:00
tss:
dq 0
2024-06-25 22:47:21 -04:00
gdt_end:
gdtp:
dw gdt_end - gdt - 1
dd gdt
section .bss
align 16
stack_top:
2024-06-27 14:49:11 -04:00
resb 16384
2024-06-25 22:47:21 -04:00
stack_bottom: