|
| 1 | +# Based on code by Ring Zero and Lower: http://ringzeroandlower.com/2017/08/08/x86-64-kernel-boot.html |
| 2 | + |
| 3 | +#include "arch/x86_64/gdt.h" |
| 4 | +#include "arch/x86_64/mmu.h" |
| 5 | +#include "kernel.h" |
| 6 | +#include "sizes.h" |
| 7 | +#include "multiboot2.h" |
| 8 | +#include "arch/x86_64/msr.h" |
| 9 | + |
| 10 | +.SET HEADER_LENGTH, header_end - header_start |
| 11 | +.SET CHECKSUM, -(MULTIBOOT2_HEADER_MAGIC + MULTIBOOT_ARCHITECTURE_I386 + HEADER_LENGTH) |
| 12 | +.section .multiboot |
| 13 | +header_start: |
| 14 | + .long MULTIBOOT2_HEADER_MAGIC |
| 15 | + .long MULTIBOOT_ARCHITECTURE_I386 |
| 16 | + .long HEADER_LENGTH |
| 17 | + .long CHECKSUM |
| 18 | + |
| 19 | + # multiboot tags go here. |
| 20 | + |
| 21 | + .short MULTIBOOT_HEADER_TAG_END |
| 22 | + .short 0 # flags, none set |
| 23 | + .long 8 # size, including itself (short + short + long) |
| 24 | +header_end: |
| 25 | + |
| 26 | +.code32 |
| 27 | + |
| 28 | +.section .bss |
| 29 | +.comm pml4, PML4_SIZE, PML4_ALIGNMENT |
| 30 | +.comm low_pdpt, PDPT_SIZE, PDPT_ALIGNMENT |
| 31 | +.comm high_pdpt, PDPT_SIZE, PDPT_ALIGNMENT |
| 32 | +.comm low_page_directory_table, PAGE_DIRECTORY_SIZE, PAGE_DIRECTORY_ALIGNMENT |
| 33 | +.comm high_page_directory_table, PAGE_DIRECTORY_SIZE, PAGE_DIRECTORY_ALIGNMENT |
| 34 | +.comm tmp_stack, KERNEL_BOOT_STACK_SIZE, KERNEL_BOOT_STACK_ALIGNMENT |
| 35 | + |
| 36 | +.data |
| 37 | +.align GDT_TABLE_ALIGNMENT |
| 38 | +gdt_table: |
| 39 | + .8byte GDT_FIRST_ENTRY |
| 40 | + .8byte GDT_KERNEL_ENTRY |
| 41 | + |
| 42 | +gdt_table_end: |
| 43 | + .skip (GDT_TABLE_SIZE - (gdt_table_end - gdt_table)) |
| 44 | + |
| 45 | +gdt_ptr: |
| 46 | + .short GDT_TABLE_SIZE - 1 |
| 47 | + .long gdt_table |
| 48 | + |
| 49 | + |
| 50 | +.section .text |
| 51 | +.global _start |
| 52 | +.type _start, @function |
| 53 | +_start: |
| 54 | + movl $tmp_stack + KERNEL_BOOT_STACK_SIZE, %esp |
| 55 | + |
| 56 | + movl $low_pdpt, %eax |
| 57 | + or $(MMU_PRESENT | MMU_WRITABLE), %eax |
| 58 | + movl %eax, pml4 + (PML4_ADDR_TO_ENTRY_INDEX(KERNEL_PHYSICAL_START) * PML4_ENTRY_SIZE) |
| 59 | + |
| 60 | + movl $high_pdpt, %eax |
| 61 | + or $(MMU_PRESENT | MMU_WRITABLE), %eax |
| 62 | + movl %eax, pml4 + (PML4_ADDR_TO_ENTRY_INDEX(KERNEL_VIRTUAL_START) * PML4_ENTRY_SIZE) |
| 63 | + |
| 64 | + movl $low_page_directory_table, %eax |
| 65 | + or $(MMU_PRESENT | MMU_WRITABLE), %eax |
| 66 | + movl %eax, low_pdpt + (PDPT_ADDR_TO_ENTRY_INDEX(KERNEL_PHYSICAL_START) * PDPT_ENTRY_SIZE) |
| 67 | + |
| 68 | + movl $high_page_directory_table, %eax |
| 69 | + or $(MMU_PRESENT | MMU_WRITABLE), %eax |
| 70 | + movl %eax, high_pdpt + (PDPT_ADDR_TO_ENTRY_INDEX(KERNEL_VIRTUAL_START) * PDPT_ENTRY_SIZE) |
| 71 | + |
| 72 | + mov $0, %ecx |
| 73 | + |
| 74 | + movl $_kernel_physical_end, %esi |
| 75 | + shrl $TWO_MEGABYTES_SHIFT, %esi |
| 76 | + addl $1, %esi |
| 77 | + |
| 78 | +page_directory_table_loop: |
| 79 | + movl $TWO_MEGABYTES, %eax |
| 80 | + mul %ecx |
| 81 | + or $(MMU_PRESENT | MMU_WRITABLE | MMU_PDE_TWO_MB), %eax |
| 82 | + movl %eax, low_page_directory_table(, %ecx, PAGE_DIRECTORY_ENTRY_SIZE) |
| 83 | + movl %eax, high_page_directory_table(, %ecx, PAGE_DIRECTORY_ENTRY_SIZE) |
| 84 | + |
| 85 | + inc %ecx |
| 86 | + cmp %esi, %ecx |
| 87 | + jne page_directory_table_loop # If not equal, redo loop |
| 88 | + |
| 89 | + movl $pml4, %eax |
| 90 | + movl %eax, %cr3 |
| 91 | + |
| 92 | + movl $KERNEL_CR4, %eax |
| 93 | + movl %eax, %cr4 |
| 94 | + |
| 95 | + movl $MSR_EFER, %ecx |
| 96 | + rdmsr |
| 97 | + or $MSR_EFER_LME, %eax |
| 98 | + wrmsr |
| 99 | + |
| 100 | + movl $KERNEL_CR0, %eax |
| 101 | + movl %eax, %cr0 |
| 102 | + |
| 103 | + lgdt gdt_ptr |
| 104 | + |
| 105 | + ljmp $(KERNEL_GDT_ENTRY * GDT_ENTRY_SIZE), $_start64 |
| 106 | + |
| 107 | + cli |
| 108 | + hlt |
| 109 | + |
| 110 | +.code64 |
| 111 | + |
| 112 | +.global _start64 |
| 113 | +.type _start64, @function |
| 114 | +_start64: |
| 115 | + # Setup segment selectors |
| 116 | + movw $0, %ax |
| 117 | + movw %ax, %ds |
| 118 | + movw %ax, %es |
| 119 | + movw %ax, %fs |
| 120 | + movw %ax, %gs |
| 121 | + movw %ax, %ss |
| 122 | + |
| 123 | + call _Z11kernel_mainv |
| 124 | + |
| 125 | + # Should never reach here |
| 126 | + cli |
| 127 | + hlt |
| 128 | +1: |
| 129 | + jmp 1b |
0 commit comments