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 | |
swissChili | dc25b2b | 2021-02-23 17:07:13 -0800 | [diff] [blame] | 19 | |
| 20 | ;;;;;;;;;;;;;;;;;;;;; DATA ;;;;;;;;;;;;;;;;;;;;; |
| 21 | |
swissChili | d3a652e | 2021-02-21 22:16:06 -0800 | [diff] [blame] | 22 | [section .data align = 0x1000] |
| 23 | page_directory: |
swissChili | e0a79bb | 2021-02-22 19:54:48 -0800 | [diff] [blame] | 24 | dd 0b010000011 ; Identity map first 4 megs |
swissChili | d3a652e | 2021-02-21 22:16:06 -0800 | [diff] [blame] | 25 | times (KERNEL_PAGE_NUMBER - 1) dd 0 |
swissChili | e0a79bb | 2021-02-22 19:54:48 -0800 | [diff] [blame] | 26 | dd 0b010000011 ; Map kernel memory to zero page too |
swissChili | d3a652e | 2021-02-21 22:16:06 -0800 | [diff] [blame] | 27 | times (1024 - KERNEL_PAGE_NUMBER - 1) dd 0 |
swissChili | d813792 | 2021-02-17 15:34:07 -0800 | [diff] [blame] | 28 | |
swissChili | dc25b2b | 2021-02-23 17:07:13 -0800 | [diff] [blame] | 29 | gdt: |
| 30 | ;; First entry, null segment |
| 31 | dw 0 ; zero limit |
| 32 | dw 0 ; base low |
| 33 | |
| 34 | db 0 ; base middle |
| 35 | db 0 ; access |
| 36 | db 0 ; granularity |
| 37 | db 0 ; base high |
| 38 | |
| 39 | ;; Second entry, code segment |
swissChili | 1b83922 | 2021-06-03 13:54:40 -0700 | [diff] [blame] | 40 | dw 0xffff ; max limit |
swissChili | dc25b2b | 2021-02-23 17:07:13 -0800 | [diff] [blame] | 41 | dw 0 |
| 42 | |
| 43 | db 0 |
| 44 | db 0x9a ; access |
| 45 | db 0xcf ; granularity |
| 46 | db 0 |
| 47 | |
| 48 | ;; Third entry, data segment |
swissChili | 1b83922 | 2021-06-03 13:54:40 -0700 | [diff] [blame] | 49 | dw 0xffff ; max limit |
swissChili | dc25b2b | 2021-02-23 17:07:13 -0800 | [diff] [blame] | 50 | dw 0 |
| 51 | |
| 52 | db 0 |
| 53 | db 0x92 ; access |
| 54 | db 0xcf ; granularity |
| 55 | db 0 |
| 56 | |
| 57 | ;; Fourth entry, user code segment |
swissChili | 1b83922 | 2021-06-03 13:54:40 -0700 | [diff] [blame] | 58 | dw 0xffff ; max limit |
swissChili | dc25b2b | 2021-02-23 17:07:13 -0800 | [diff] [blame] | 59 | dw 0 |
| 60 | |
| 61 | db 0 |
| 62 | db 0xfa ; access |
| 63 | db 0xcf ; granularity |
| 64 | db 0 |
| 65 | |
| 66 | ;; Fifth entry, user data segment |
swissChili | 1b83922 | 2021-06-03 13:54:40 -0700 | [diff] [blame] | 67 | dw 0xffff ; max limit |
swissChili | dc25b2b | 2021-02-23 17:07:13 -0800 | [diff] [blame] | 68 | dw 0 |
| 69 | |
| 70 | db 0 |
| 71 | db 0xf2 ; access |
| 72 | db 0xcf ; granularity |
| 73 | db 0 |
| 74 | |
| 75 | gdt_pointer: |
| 76 | dw (8 * 5 - 1) ; sizeof(gdt entry) * 5 - 1 |
| 77 | dd (gdt - KERNEL_VIRTUAL_BASE) ; Remember, PHYSICAL address |
| 78 | |
| 79 | |
| 80 | ;;;;;;;;;;;;;;;;;;;;; CODE ;;;;;;;;;;;;;;;;;;;;; |
| 81 | |
swissChili | d813792 | 2021-02-17 15:34:07 -0800 | [diff] [blame] | 82 | [bits 32] |
swissChili | d3a652e | 2021-02-21 22:16:06 -0800 | [diff] [blame] | 83 | [section .text] |
swissChili | d813792 | 2021-02-17 15:34:07 -0800 | [diff] [blame] | 84 | [global mboot] |
| 85 | [extern code] |
| 86 | [extern bss] |
| 87 | [extern end] |
swissChili | d813792 | 2021-02-17 15:34:07 -0800 | [diff] [blame] | 88 | mboot: |
| 89 | dd MBOOT_HEADER_MAGIC ; This tells GRUB to start executing here: |
| 90 | dd MBOOT_HEADER_FLAGS |
| 91 | dd MBOOT_CHECKSUM |
| 92 | |
| 93 | dd mboot ; Current location |
| 94 | dd code ; .text section |
| 95 | dd bss ; End of .data |
| 96 | dd end ; End of kernel |
| 97 | dd start |
| 98 | |
| 99 | [global start] |
swissChili | 0b35bf2 | 2021-02-18 12:49:40 -0800 | [diff] [blame] | 100 | [extern kmain] ; C code |
swissChili | d813792 | 2021-02-17 15:34:07 -0800 | [diff] [blame] | 101 | |
swissChili | dc25b2b | 2021-02-23 17:07:13 -0800 | [diff] [blame] | 102 | start equ (_start) |
swissChili | 4418ca5 | 2021-06-14 17:36:00 -0700 | [diff] [blame] | 103 | |
swissChili | d3a652e | 2021-02-21 22:16:06 -0800 | [diff] [blame] | 104 | _start: |
swissChili | dc25b2b | 2021-02-23 17:07:13 -0800 | [diff] [blame] | 105 | ;; First set up GDT |
| 106 | mov eax, (gdt_pointer - KERNEL_VIRTUAL_BASE) |
| 107 | lgdt [eax] ; Load GDT |
| 108 | |
| 109 | mov ax, 0x10 ; Offset of data segment |
| 110 | mov ds, ax |
| 111 | mov es, ax |
| 112 | mov fs, ax |
| 113 | mov gs, ax |
| 114 | mov ss, ax |
| 115 | |
| 116 | enable_paging: |
swissChili | d3a652e | 2021-02-21 22:16:06 -0800 | [diff] [blame] | 117 | mov ecx, (page_directory - KERNEL_VIRTUAL_BASE) ; Physical address |
| 118 | mov cr3, ecx |
swissChili | d813792 | 2021-02-17 15:34:07 -0800 | [diff] [blame] | 119 | |
swissChili | d3a652e | 2021-02-21 22:16:06 -0800 | [diff] [blame] | 120 | mov ecx, cr4 |
| 121 | or ecx, 0x00000010 ; Enable 4 meg pages |
| 122 | mov cr4, ecx |
| 123 | |
| 124 | mov ecx, cr0 |
| 125 | or ecx, 0x80000000 ; Enable paging |
| 126 | mov cr0, ecx |
| 127 | |
| 128 | lea ecx, [in_higher_half] ; Long jump into high memory |
| 129 | jmp ecx |
| 130 | |
| 131 | in_higher_half: |
| 132 | mov dword [page_directory], 0 |
| 133 | invlpg [0] ; Clear identity mapping |
| 134 | |
| 135 | mov esp, (stack + STACK_SIZE) |
| 136 | |
swissChili | e0a79bb | 2021-02-22 19:54:48 -0800 | [diff] [blame] | 137 | add ebx, 0xC0000000 ; Translate to virtual address |
| 138 | push ebx ; Holds multiboot header location |
swissChili | e9289ee | 2021-03-20 21:54:28 -0700 | [diff] [blame] | 139 | push esp ; Initial kernel stack |
swissChili | 0b35bf2 | 2021-02-18 12:49:40 -0800 | [diff] [blame] | 140 | call kmain |
swissChili | d3a652e | 2021-02-21 22:16:06 -0800 | [diff] [blame] | 141 | |
swissChili | e0a79bb | 2021-02-22 19:54:48 -0800 | [diff] [blame] | 142 | .end: |
| 143 | hlt |
| 144 | jmp .end |
| 145 | |
swissChili | d3a652e | 2021-02-21 22:16:06 -0800 | [diff] [blame] | 146 | |
| 147 | [section .bss align = 32] |
| 148 | stack: |
| 149 | resb STACK_SIZE |