]> git.ameliathe1st.gay Git - voyage-au-centre-des-fichiers.git/commitdiff
Moved the GDT to the higher half (added a second lgdt after jump to 64 bits), to...
authorAmelia Coutard <eliottulio.coutard@gmail.com>
Tue, 11 Oct 2022 06:38:45 +0000 (08:38 +0200)
committerAmelia Coutard <eliottulio.coutard@gmail.com>
Tue, 11 Oct 2022 06:38:45 +0000 (08:38 +0200)
src/boot.S
src/kernel.cpp
src/paging.cpp
src/paging.hpp

index 9faf67a8891587cd09c580a19f360a3ae5c6ccec..d26574870ec14064b3e04d9517c1eb6fcc9a1f9a 100644 (file)
@@ -188,11 +188,21 @@ _start:
        mov %eax, %cr0
 
        # Jump to 64 bits:
-       lgdt (GDT.PTR - KERNEL_VMA)
+       lgdt GDT.PTR - KERNEL_VMA
        jmp $GDT.KERNEL_CODE, $.trampoline
 
 .code64
 .trampoline:
+       mov %esp, %esp
+
+       movq $GDT, GDT.PTR + 2
+       lgdt GDT.PTR # Reload GDT in higher half.
+       pushq $GDT.KERNEL_CODE
+       movabsq $.trampoline2, %rax
+       pushq %rax
+       lretq # Jump to new GDT.
+.trampoline2:
+
        mov $GDT.KERNEL_DATA, %ax  # Set the A-register to the data descriptor.
        mov %ax, %ds        # Set the data segment to the A-register.
        mov %ax, %es        # Set the extra segment to the A-register.
index a52ecab6796cacfd8a443194724e3debf42b9a72..baf2dc78f1590d4a221389342f4625afaf691df0 100644 (file)
@@ -5,6 +5,7 @@
 #include "interrupts.hpp"
 
 os::idt<32> idt;
+extern "C" os::paging::PML4T PML4T;
 
 extern "C" void kmain(unsigned long magic, os::phys_ptr<const multiboot2::info_start> info) {
        os::assert(magic == 0x36D76289, "Incorrect magic number: wasn't booted with multiboot2.");
@@ -129,8 +130,9 @@ extern "C" void kmain(unsigned long magic, os::phys_ptr<const multiboot2::info_s
                os::enable_interrupts(isr_info, idt);
        }
 
-       os::phys_ptr<os::paging::PML4T> PML4T = nullptr;
-       asm ("movq $PML4T - 0xFFFFFFFF80000000,%0" : "=ri"(PML4T) : );
+       PML4T.contents[0].base_address()->contents[0].P(false);
+       PML4T.contents[0].base_address()->contents[1].P(false);
+       PML4T.contents[0].P(false);
        os::print("Mapping:\n");
        os::paging::print_mapping(PML4T);
 
index 9c2f13365cab575aba5ebce83f98f2bf19bf1220..7d0f9ff3ba01445b19f1b75a94a9e8f06994cae8 100644 (file)
@@ -3,9 +3,9 @@
 
 // void os::paging::unmap(phys_ptr<PML4T> PLM4T, page* virt, std::uint64_t length) { }
 // void os::paging::map(phys_ptr<PML4T> PLM4T, page* virt, std::uint64_t length, phys_ptr<page> phys) { }
-template<int order> void print_mapping(os::phys_ptr<os::paging::paging_table<order>> table, std::uint64_t virt_address, bool show_higher_half) {
+template<int order> void print_mapping(const os::paging::paging_table<order>& table, std::uint64_t virt_address, bool show_higher_half) {
        for (std::size_t i = 0; i < (order == 3 && !show_higher_half ? 256 : 512); i++) {
-               const auto& page = table->contents[i];
+               const auto& page = table.contents[i];
                if (!page.P()) { continue; }
                std::uint64_t new_virt = virt_address + i * (4096ul << (order * 9));
                new_virt |= ((new_virt & 0x0000800000000000) != 0 ? 0xFFFF000000000000 : 0);
@@ -18,12 +18,12 @@ template<int order> void print_mapping(os::phys_ptr<os::paging::paging_table<ord
                        os::print(")\n");
                } else {
                        if constexpr (order > 0) { // Will never be false when !page.is_page().
-                               print_mapping<order - 1>(page.base_address(), new_virt, show_higher_half);
+                               print_mapping<order - 1>(*page.base_address(), new_virt, show_higher_half);
                        }
                }
        }
 }
-void os::paging::print_mapping(phys_ptr<PML4T> PML4T, bool show_higher_half) {
+void os::paging::print_mapping(const PML4T& PML4T, bool show_higher_half) {
        ::print_mapping(PML4T, 0, show_higher_half);
 }
 
index 83a556f9b84a51849350e16648a30ab2f82d945a..347733bd78745fec7c2a9ed98b950cf336aa626e 100644 (file)
@@ -152,7 +152,7 @@ using page = paging_table<-1>;
 
 void unmap(phys_ptr<PML4T> PLM4T, page* virt, std::uint64_t length);
 void map(phys_ptr<PML4T> PLM4T, page* virt, std::uint64_t length, phys_ptr<page> phys);
-void print_mapping(phys_ptr<PML4T> PML4T, bool show_higher_half = false);
+void print_mapping(const PML4T& PML4T, bool show_higher_half = false);
 
 class page_allocator_t;
 extern page_allocator_t page_allocator;