diff --git a/OVMFbin/OVMF_VARS-pure-efi.fd b/OVMFbin/OVMF_VARS-pure-efi.fd index d40d20e..e1e2fa3 100644 Binary files a/OVMFbin/OVMF_VARS-pure-efi.fd and b/OVMFbin/OVMF_VARS-pure-efi.fd differ diff --git a/kernel/bin/CustomOS.img b/kernel/bin/CustomOS.img index 4cbb54c..b1a0cba 100644 Binary files a/kernel/bin/CustomOS.img and b/kernel/bin/CustomOS.img differ diff --git a/kernel/bin/kernel.elf b/kernel/bin/kernel.elf index 9ec57c4..0fcb592 100755 Binary files a/kernel/bin/kernel.elf and b/kernel/bin/kernel.elf differ diff --git a/kernel/lib/kernel.o b/kernel/lib/kernel.o index c49a104..d0cd876 100644 Binary files a/kernel/lib/kernel.o and b/kernel/lib/kernel.o differ diff --git a/kernel/lib/memory.o b/kernel/lib/memory.o index f775d83..7bd77d6 100644 Binary files a/kernel/lib/memory.o and b/kernel/lib/memory.o differ diff --git a/kernel/lib/paging/PageFrameAllocator.o b/kernel/lib/paging/PageFrameAllocator.o index c0e72b0..8e51d5b 100644 Binary files a/kernel/lib/paging/PageFrameAllocator.o and b/kernel/lib/paging/PageFrameAllocator.o differ diff --git a/kernel/lib/paging/PageTableManager.o b/kernel/lib/paging/PageTableManager.o new file mode 100644 index 0000000..8027176 Binary files /dev/null and b/kernel/lib/paging/PageTableManager.o differ diff --git a/kernel/src/kernel.cpp b/kernel/src/kernel.cpp index 10b1506..d0cbcf6 100644 --- a/kernel/src/kernel.cpp +++ b/kernel/src/kernel.cpp @@ -7,6 +7,8 @@ #include "Bitmap.h" #include "paging/PageFrameAllocator.h" #include "paging/PageMapIndexer.h" +#include "paging/paging.h" +#include "paging/PageTableManager.h" struct BootInfo { Framebuffer* framebuffer; @@ -31,25 +33,34 @@ extern "C" void _start(BootInfo* bootInfo) { // display information of memory map uint64_t mMapEntries = bootInfo->mMapSize / bootInfo->mMapDescriptorSize; - - PageFrameAllocator newAllocator; - newAllocator.ReadEFIMemoryMap(bootInfo->mMap, bootInfo->mMapSize, bootInfo->mMapDescriptorSize); + GlobalAllocator = PageFrameAllocator(); + GlobalAllocator.ReadEFIMemoryMap(bootInfo->mMap, bootInfo->mMapSize, bootInfo->mMapDescriptorSize); // ensure we don't absolutely fuck shit up uint64_t kernelSize = (uint64_t)&_KernelEnd - (uint64_t)&_KernelStart; uint64_t kernelPages = (uint64_t)kernelSize / 4096 + 1; + GlobalAllocator.LockPages(&_KernelStart, kernelPages); - newAllocator.LockPages(&_KernelStart, kernelPages); + // create page map + PageTable* PML4 = (PageTable*)GlobalAllocator.RequestPage(); + memset(PML4, 0, 0x1000); - PageMapIndexer pageIndexer = PageMapIndexer(0x1000); + PageTableManager pageTableManager = PageTableManager(PML4); - newRenderer.Print(to_string(pageIndexer.P_i)); - newRenderer.Print(" - "); - newRenderer.Print(to_string(pageIndexer.PT_i)); - newRenderer.Print(" - "); - newRenderer.Print(to_string(pageIndexer.PD_i)); - newRenderer.Print(" - "); - newRenderer.Print(to_string(pageIndexer.PDP_i)); + for (uint64_t t = 0; t < GetMemorySize(bootInfo->mMap, mMapEntries, bootInfo->mMapDescriptorSize); t += 0x1000) { + pageTableManager.MapMemory((void*)t, (void*)t); + } + + uint64_t fbBase = (uint64_t)bootInfo->framebuffer->BaseAddress; + uint64_t fbSize = (uint64_t)bootInfo->framebuffer->BufferSize + 0x1000; + + for (uint64_t t = fbBase; t < fbBase + fbSize; t += 4096) { + pageTableManager.MapMemory((void*)t, (void*)t); + } + + asm("mov %0, %%cr3" :: "r" (PML4)); + + newRenderer.Print("WE'RE USING THE NEW PAGE MAP!!!!"); return; } \ No newline at end of file diff --git a/kernel/src/memory.cpp b/kernel/src/memory.cpp index 9118a4e..4c9b61a 100644 --- a/kernel/src/memory.cpp +++ b/kernel/src/memory.cpp @@ -15,3 +15,10 @@ uint64_t GetMemorySize(EFI_MEMORY_DESCRIPTOR* mMap, uint64_t mMapEntries, uint64 } + + +void memset(void* start, uint8_t value, uint64_t num) { + for (uint64_t i = 0; i < num; i++) { + *(uint8_t*)((uint64_t)start + i) = value; + } +} \ No newline at end of file diff --git a/kernel/src/memory.h b/kernel/src/memory.h index 63251b0..c882933 100644 --- a/kernel/src/memory.h +++ b/kernel/src/memory.h @@ -3,4 +3,5 @@ #include #include "efiMemory.h" -uint64_t GetMemorySize(EFI_MEMORY_DESCRIPTOR* mMap, uint64_t mMapEntries, uint64_t mMapDescSize); \ No newline at end of file +uint64_t GetMemorySize(EFI_MEMORY_DESCRIPTOR* mMap, uint64_t mMapEntries, uint64_t mMapDescSize); +void memset(void* start, uint8_t value, uint64_t num); \ No newline at end of file diff --git a/kernel/src/paging/PageFrameAllocator.cpp b/kernel/src/paging/PageFrameAllocator.cpp index 03c8d44..674b377 100644 --- a/kernel/src/paging/PageFrameAllocator.cpp +++ b/kernel/src/paging/PageFrameAllocator.cpp @@ -4,6 +4,7 @@ uint64_t freeMemory; uint64_t reservedMemory; uint64_t usedMemory; bool Initialized = false; +PageFrameAllocator GlobalAllocator; void PageFrameAllocator::ReadEFIMemoryMap(EFI_MEMORY_DESCRIPTOR* mMap, size_t mMapSize, size_t mMapDescSize){ if (Initialized) return; diff --git a/kernel/src/paging/PageFrameAllocator.h b/kernel/src/paging/PageFrameAllocator.h index 7551b7a..19221f0 100644 --- a/kernel/src/paging/PageFrameAllocator.h +++ b/kernel/src/paging/PageFrameAllocator.h @@ -24,4 +24,6 @@ class PageFrameAllocator { void ReservePages(void* address, uint64_t pageCount); void UnreservePage(void* address); void UnreservePages(void* address, uint64_t pageCount); -}; \ No newline at end of file +}; + +extern PageFrameAllocator GlobalAllocator; \ No newline at end of file diff --git a/kernel/src/paging/PageTableManager.cpp b/kernel/src/paging/PageTableManager.cpp new file mode 100644 index 0000000..7cb4078 --- /dev/null +++ b/kernel/src/paging/PageTableManager.cpp @@ -0,0 +1,65 @@ +#include "PageTableManager.h" +#include "PageMapIndexer.h" +#include +#include "PageFrameAllocator.h" +#include "../memory.h" + +PageTableManager::PageTableManager(PageTable* PML4Address) { + this->PML4 = PML4Address; +} + +void PageTableManager::MapMemory(void* virtualMemory, void* physicalMemory) { + PageMapIndexer indexer = PageMapIndexer((uint64_t)virtualMemory); + PageDirectoryEntry PDE; + + PDE = PML4->entries[indexer.PDP_i]; + PageTable* PDP; + + if (!PDE.Present) { + PDP = (PageTable*)GlobalAllocator.RequestPage(); + memset(PDP, 0, 4096); // zero out the new page + + PDE.Address = (uint64_t)PDP >> 12; + PDE.Present = true; + PDE.ReadWrite = true; + PML4->entries[indexer.PDP_i] = PDE; + } else { + PDP = (PageTable*)((uint64_t)PDE.Address << 12); + } + + PDE = PDP->entries[indexer.PD_i]; + PageTable* PD; + + if (!PDE.Present) { + PD = (PageTable*)GlobalAllocator.RequestPage(); + memset(PD, 0, 4096); // zero out the new page + + PDE.Address = (uint64_t)PD >> 12; + PDE.Present = true; + PDE.ReadWrite = true; + PDP->entries[indexer.PD_i] = PDE; + } else { + PD = (PageTable*)((uint64_t)PDE.Address << 12); + } + + PDE = PD->entries[indexer.PT_i]; + PageTable* PT; + + if (!PDE.Present) { + PT = (PageTable*)GlobalAllocator.RequestPage(); + memset(PT, 0, 4096); // zero out the new page + + PDE.Address = (uint64_t)PT >> 12; + PDE.Present = true; + PDE.ReadWrite = true; + PD->entries[indexer.PT_i] = PDE; + } else { + PT = (PageTable*)((uint64_t)PDE.Address << 12); + } + + PDE = PT->entries[indexer.P_i]; + PDE.Address = (uint64_t)physicalMemory >> 12; + PDE.Present = true; + PDE.ReadWrite = true; + PT->entries[indexer.P_i] = PDE; +} \ No newline at end of file diff --git a/kernel/src/paging/PageTableManager.h b/kernel/src/paging/PageTableManager.h new file mode 100644 index 0000000..7e9bea2 --- /dev/null +++ b/kernel/src/paging/PageTableManager.h @@ -0,0 +1,9 @@ +#pragma once +#include "paging.h" + +class PageTableManager { + public: + PageTableManager(PageTable* PML4Address); + PageTable* PML4; + void MapMemory(void* virtualMemory, void* physicalMemory); +}; \ No newline at end of file