From 678744f65db0dd381aff9d5dd0a8bf48aac208ab Mon Sep 17 00:00:00 2001 From: Alex Date: Mon, 27 Mar 2023 20:30:19 +0300 Subject: [PATCH] Fix kernel mapping --- Core/Memory/Memory.cpp | 11 ++++++++--- Core/Memory/VirtualMemoryManager.cpp | 2 ++ include/memory.hpp | 2 +- 3 files changed, 11 insertions(+), 4 deletions(-) diff --git a/Core/Memory/Memory.cpp b/Core/Memory/Memory.cpp index 628f216..2ae9cfb 100644 --- a/Core/Memory/Memory.cpp +++ b/Core/Memory/Memory.cpp @@ -136,6 +136,7 @@ NIF void MapKernel(PageTable4 *PT, BootInfo *Info) uintptr_t BaseKernelMapAddress = (uintptr_t)Info->Kernel.PhysicalBase; uintptr_t k; + /* Text section */ for (k = KernelStart; k < KernelTextEnd; k += PAGE_SIZE) { va.Map((void *)k, (void *)BaseKernelMapAddress, PTFlag::RW); @@ -143,6 +144,7 @@ NIF void MapKernel(PageTable4 *PT, BootInfo *Info) BaseKernelMapAddress += PAGE_SIZE; } + /* Data section */ for (k = KernelTextEnd; k < KernelDataEnd; k += PAGE_SIZE) { va.Map((void *)k, (void *)BaseKernelMapAddress, PTFlag::RW | PTFlag::G); @@ -150,13 +152,15 @@ NIF void MapKernel(PageTable4 *PT, BootInfo *Info) BaseKernelMapAddress += PAGE_SIZE; } + /* Read only data section */ for (k = KernelDataEnd; k < KernelRoDataEnd; k += PAGE_SIZE) { - va.Map((void *)k, (void *)BaseKernelMapAddress, PTFlag::P | PTFlag::G); + va.Map((void *)k, (void *)BaseKernelMapAddress, PTFlag::G); KernelAllocator.LockPage((void *)BaseKernelMapAddress); BaseKernelMapAddress += PAGE_SIZE; } + /* BSS section */ for (k = KernelRoDataEnd; k < KernelEnd; k += PAGE_SIZE) { va.Map((void *)k, (void *)BaseKernelMapAddress, PTFlag::RW | PTFlag::G); @@ -164,14 +168,15 @@ NIF void MapKernel(PageTable4 *PT, BootInfo *Info) BaseKernelMapAddress += PAGE_SIZE; } + /* Kernel file */ for (k = KernelFileStart; k < KernelFileEnd; k += PAGE_SIZE) { - va.Map((void *)k, (void *)k, PTFlag::RW | PTFlag::G); + va.Map((void *)k, (void *)k, PTFlag::G); KernelAllocator.LockPage((void *)k); } debug("\nStart: %#llx - Text End: %#llx - RoEnd: %#llx - End: %#llx\nStart Physical: %#llx - End Physical: %#llx", - KernelStart, KernelTextEnd, KernelRoDataEnd, KernelEnd, Info->Kernel.PhysicalBase, BaseKernelMapAddress - PAGE_SIZE); + KernelStart, KernelTextEnd, KernelRoDataEnd, KernelEnd, KernelFileStart, KernelFileEnd); #ifdef DEBUG if (EnableExternalMemoryTracer) diff --git a/Core/Memory/VirtualMemoryManager.cpp b/Core/Memory/VirtualMemoryManager.cpp index 904be28..590d9b5 100644 --- a/Core/Memory/VirtualMemoryManager.cpp +++ b/Core/Memory/VirtualMemoryManager.cpp @@ -49,6 +49,8 @@ namespace Memory return; } + Flags |= PTFlag::P; + PageMapIndexer Index = PageMapIndexer((uintptr_t)VirtualAddress); // Clear any flags that are not 1 << 0 (Present) - 1 << 5 (Accessed) because rest are for page table entries only uint64_t DirectoryFlags = Flags & 0x3F; diff --git a/include/memory.hpp b/include/memory.hpp index 6ea5bec..628c9c5 100644 --- a/include/memory.hpp +++ b/include/memory.hpp @@ -546,7 +546,7 @@ namespace Memory * @param PhysicalAddress Physical address of the page. * @param Flags Flags of the page. Check PTFlag enum. */ - void Map(void *VirtualAddress, void *PhysicalAddress, uint64_t Flags); + void Map(void *VirtualAddress, void *PhysicalAddress, uint64_t Flag = PTFlag::P); /** * @brief Map multiple pages.