swissChili | d813792 | 2021-02-17 15:34:07 -0800 | [diff] [blame] | 1 | ;;; GRUB Multiboot header, calls main() in main.c |
| 2 | |
swissChili | d3a652e | 2021-02-21 22:16:06 -0800 | [diff] [blame^] | 3 | ;; Some constants |
swissChili | d813792 | 2021-02-17 15:34:07 -0800 | [diff] [blame] | 4 | MBOOT_PAGE_ALIGN equ 1<<0 |
| 5 | MBOOT_MEM_INFO equ 1<<1 |
| 6 | MBOOT_HEADER_MAGIC equ 0x1BADB002 |
| 7 | |
| 8 | MBOOT_HEADER_FLAGS equ MBOOT_PAGE_ALIGN | MBOOT_MEM_INFO |
| 9 | MBOOT_CHECKSUM equ -(MBOOT_HEADER_MAGIC + MBOOT_HEADER_FLAGS) |
| 10 | |
swissChili | d3a652e | 2021-02-21 22:16:06 -0800 | [diff] [blame^] | 11 | ;; Kernel memory start |
| 12 | KERNEL_VIRTUAL_BASE equ 0xC0000000 |
| 13 | ;; Index in page directory |
| 14 | KERNEL_PAGE_NUMBER equ (KERNEL_VIRTUAL_BASE >> 22) |
| 15 | |
| 16 | STACK_SIZE equ 0x4000 |
| 17 | |
| 18 | |
| 19 | [section .data align = 0x1000] |
| 20 | page_directory: |
| 21 | dd 0b010000011 ; Identity map first 4 megs |
| 22 | times (KERNEL_PAGE_NUMBER - 1) dd 0 |
| 23 | dd 0b010000011 ; Map kernel memory to zero page too |
| 24 | times (1024 - KERNEL_PAGE_NUMBER - 1) dd 0 |
swissChili | d813792 | 2021-02-17 15:34:07 -0800 | [diff] [blame] | 25 | |
| 26 | [bits 32] |
swissChili | d3a652e | 2021-02-21 22:16:06 -0800 | [diff] [blame^] | 27 | [section .text] |
swissChili | d813792 | 2021-02-17 15:34:07 -0800 | [diff] [blame] | 28 | [global mboot] |
| 29 | [extern code] |
| 30 | [extern bss] |
| 31 | [extern end] |
swissChili | d813792 | 2021-02-17 15:34:07 -0800 | [diff] [blame] | 32 | mboot: |
| 33 | dd MBOOT_HEADER_MAGIC ; This tells GRUB to start executing here: |
| 34 | dd MBOOT_HEADER_FLAGS |
| 35 | dd MBOOT_CHECKSUM |
| 36 | |
| 37 | dd mboot ; Current location |
| 38 | dd code ; .text section |
| 39 | dd bss ; End of .data |
| 40 | dd end ; End of kernel |
| 41 | dd start |
| 42 | |
| 43 | [global start] |
swissChili | 0b35bf2 | 2021-02-18 12:49:40 -0800 | [diff] [blame] | 44 | [extern kmain] ; C code |
swissChili | d813792 | 2021-02-17 15:34:07 -0800 | [diff] [blame] | 45 | |
swissChili | d3a652e | 2021-02-21 22:16:06 -0800 | [diff] [blame^] | 46 | start equ (_start - KERNEL_VIRTUAL_BASE) |
| 47 | |
| 48 | _start: |
| 49 | mov ecx, (page_directory - KERNEL_VIRTUAL_BASE) ; Physical address |
| 50 | mov cr3, ecx |
swissChili | d813792 | 2021-02-17 15:34:07 -0800 | [diff] [blame] | 51 | |
swissChili | d3a652e | 2021-02-21 22:16:06 -0800 | [diff] [blame^] | 52 | mov ecx, cr4 |
| 53 | or ecx, 0x00000010 ; Enable 4 meg pages |
| 54 | mov cr4, ecx |
| 55 | |
| 56 | mov ecx, cr0 |
| 57 | or ecx, 0x80000000 ; Enable paging |
| 58 | mov cr0, ecx |
| 59 | |
| 60 | lea ecx, [in_higher_half] ; Long jump into high memory |
| 61 | jmp ecx |
| 62 | |
| 63 | in_higher_half: |
| 64 | mov dword [page_directory], 0 |
| 65 | invlpg [0] ; Clear identity mapping |
| 66 | |
| 67 | mov esp, (stack + STACK_SIZE) |
| 68 | |
| 69 | push ebx ; Holds multiboot header location, PHYSICAL addr |
swissChili | 0b35bf2 | 2021-02-18 12:49:40 -0800 | [diff] [blame] | 70 | call kmain |
swissChili | d813792 | 2021-02-17 15:34:07 -0800 | [diff] [blame] | 71 | jmp $ |
swissChili | d3a652e | 2021-02-21 22:16:06 -0800 | [diff] [blame^] | 72 | |
| 73 | |
| 74 | [section .bss align = 32] |
| 75 | stack: |
| 76 | resb STACK_SIZE |