page table manager working

This commit is contained in:
2026-01-28 20:32:55 +11:00
parent 823b13234e
commit e4620f60c9
14 changed files with 110 additions and 14 deletions

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@@ -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;
}

View File

@@ -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;
}
}

View File

@@ -3,4 +3,5 @@
#include <stdint.h>
#include "efiMemory.h"
uint64_t GetMemorySize(EFI_MEMORY_DESCRIPTOR* mMap, uint64_t mMapEntries, uint64_t mMapDescSize);
uint64_t GetMemorySize(EFI_MEMORY_DESCRIPTOR* mMap, uint64_t mMapEntries, uint64_t mMapDescSize);
void memset(void* start, uint8_t value, uint64_t num);

View File

@@ -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;

View File

@@ -24,4 +24,6 @@ class PageFrameAllocator {
void ReservePages(void* address, uint64_t pageCount);
void UnreservePage(void* address);
void UnreservePages(void* address, uint64_t pageCount);
};
};
extern PageFrameAllocator GlobalAllocator;

View File

@@ -0,0 +1,65 @@
#include "PageTableManager.h"
#include "PageMapIndexer.h"
#include <stdint.h>
#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;
}

View File

@@ -0,0 +1,9 @@
#pragma once
#include "paging.h"
class PageTableManager {
public:
PageTableManager(PageTable* PML4Address);
PageTable* PML4;
void MapMemory(void* virtualMemory, void* physicalMemory);
};