From 9b22a6965579ea1867aea291d910c96f386b518b Mon Sep 17 00:00:00 2001 From: Brett Weiland Date: Tue, 24 Aug 2021 14:09:29 -0500 Subject: major backup 8.24.21 --- src/bootloader/enter_kernel.asm | 72 ++++++++++++++++++++++++----------------- 1 file changed, 43 insertions(+), 29 deletions(-) (limited to 'src/bootloader/enter_kernel.asm') diff --git a/src/bootloader/enter_kernel.asm b/src/bootloader/enter_kernel.asm index d3933cf..ef2cd71 100644 --- a/src/bootloader/enter_kernel.asm +++ b/src/bootloader/enter_kernel.asm @@ -1,14 +1,14 @@ [extern main] [extern _kernel_stack_loc] -[extern _stage1_pagetable] +[extern _kernel_page_size] enter_longmode: cli -; TODO check if a20 is already set mov al, 0x92 or al, 2 out 0x92, al +;enter 32 bit mode lgdt [protected_gdt.descriptor] mov eax, cr0 or eax, 0x1 @@ -30,44 +30,56 @@ mov es, ax mov fs, ax mov gs, ax -mov edi, _stage1_pagetable ; 0x3000 -mov cr3, edi -mov eax, 0 -mov ecx, 0xc00 ; 0x1000 -rep stosd -mov edi, cr3 +mov edi, 0x10000 +mov cr3, edi -mov DWORD [edi], _stage1_pagetable + 0x1003 ; pml4e[0] = pdpe -add edi, 0x1000 -mov DWORD [edi], _stage1_pagetable + 0x2003 ; pdpe[0] = pde -add edi, 0x1000 -mov DWORD [edi], 0x83 ; pde[0] = pte +mov eax, 0 +mov ecx, 0x1800 ; bzero 6 pages +rep stosd -mov eax, cr4 -or eax, 0x620 -mov cr4, eax +mov DWORD [0x10000], 0x11003 ; pml4e[0] = pdpe +mov DWORD [0x11000], 0x12003 ; pdpe[0] = pde +mov DWORD [0x12000], 0x83 ; pde[0] = pte + +mov DWORD [0x10ff8], 0x13003 +mov DWORD [0x13ff0], 0x14003 +mov DWORD [0x14000], 0x15003 + +mov eax, 0x100003 +mov ebx, 0 +mov ecx, _kernel_page_size +.kernel_load_loop: +mov DWORD [0x15000 + ebx], eax +add ebx, 8 +add eax, 0x1000 +loop .kernel_load_loop -;end of setting up pages -mov ecx, 0xc0000080 -rdmsr -or eax, 1 << 8 -wrmsr -mov eax, cr0 -or eax, 1 << 31 | 1 << 0 ; this is where we set paging and protected mode (respectively)! -mov cr0, eax +mov eax, cr4 +; PAE, OSFXSR, OSXMMEXCPT +or eax, 1 << 5 | 1 << 9 | 1 << 10 +mov cr4, eax +;end of setting up pages +;testing to see if NX bit is available. +;If it's not and we enable it, it will cause pagefaults on read +mov eax, 0x80000001 +cpuid +and edx, 1 << 20 +shr edx, 9 + mov ecx, 0xc0000080 rdmsr -or eax, 1 << 8 +or eax, 1 << 8 | 1 << 11 +or eax, edx wrmsr mov eax, cr0 -and ax, 0xfffb -or eax, 0x80000002 +or eax, 1 << 31 | 1 << 0; +and ax, ~(1 << 2) mov cr0, eax @@ -76,8 +88,10 @@ lgdt [long_gdt.descriptor] jmp LONG_CODE_SEGMENT:enter_kernel enter_kernel: bits 64 -mov rbp, _kernel_stack_loc +mov rbp, 0 mov rsp, _kernel_stack_loc -call main ; where we actually call the kernel +mov rax, QWORD main +;push QWORD 0 +jmp rax jmp $ ret -- cgit v1.2.3