page table manager working
This commit is contained in:
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
BIN
kernel/lib/paging/PageTableManager.o
Normal file
BIN
kernel/lib/paging/PageTableManager.o
Normal file
Binary file not shown.
@@ -7,6 +7,8 @@
|
|||||||
#include "Bitmap.h"
|
#include "Bitmap.h"
|
||||||
#include "paging/PageFrameAllocator.h"
|
#include "paging/PageFrameAllocator.h"
|
||||||
#include "paging/PageMapIndexer.h"
|
#include "paging/PageMapIndexer.h"
|
||||||
|
#include "paging/paging.h"
|
||||||
|
#include "paging/PageTableManager.h"
|
||||||
|
|
||||||
struct BootInfo {
|
struct BootInfo {
|
||||||
Framebuffer* framebuffer;
|
Framebuffer* framebuffer;
|
||||||
@@ -31,25 +33,34 @@ extern "C" void _start(BootInfo* bootInfo) {
|
|||||||
// display information of memory map
|
// display information of memory map
|
||||||
uint64_t mMapEntries = bootInfo->mMapSize / bootInfo->mMapDescriptorSize;
|
uint64_t mMapEntries = bootInfo->mMapSize / bootInfo->mMapDescriptorSize;
|
||||||
|
|
||||||
|
GlobalAllocator = PageFrameAllocator();
|
||||||
PageFrameAllocator newAllocator;
|
GlobalAllocator.ReadEFIMemoryMap(bootInfo->mMap, bootInfo->mMapSize, bootInfo->mMapDescriptorSize);
|
||||||
newAllocator.ReadEFIMemoryMap(bootInfo->mMap, bootInfo->mMapSize, bootInfo->mMapDescriptorSize);
|
|
||||||
|
|
||||||
// ensure we don't absolutely fuck shit up
|
// ensure we don't absolutely fuck shit up
|
||||||
uint64_t kernelSize = (uint64_t)&_KernelEnd - (uint64_t)&_KernelStart;
|
uint64_t kernelSize = (uint64_t)&_KernelEnd - (uint64_t)&_KernelStart;
|
||||||
uint64_t kernelPages = (uint64_t)kernelSize / 4096 + 1;
|
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));
|
for (uint64_t t = 0; t < GetMemorySize(bootInfo->mMap, mMapEntries, bootInfo->mMapDescriptorSize); t += 0x1000) {
|
||||||
newRenderer.Print(" - ");
|
pageTableManager.MapMemory((void*)t, (void*)t);
|
||||||
newRenderer.Print(to_string(pageIndexer.PT_i));
|
}
|
||||||
newRenderer.Print(" - ");
|
|
||||||
newRenderer.Print(to_string(pageIndexer.PD_i));
|
uint64_t fbBase = (uint64_t)bootInfo->framebuffer->BaseAddress;
|
||||||
newRenderer.Print(" - ");
|
uint64_t fbSize = (uint64_t)bootInfo->framebuffer->BufferSize + 0x1000;
|
||||||
newRenderer.Print(to_string(pageIndexer.PDP_i));
|
|
||||||
|
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;
|
return;
|
||||||
}
|
}
|
||||||
@@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -3,4 +3,5 @@
|
|||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include "efiMemory.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);
|
||||||
@@ -4,6 +4,7 @@ uint64_t freeMemory;
|
|||||||
uint64_t reservedMemory;
|
uint64_t reservedMemory;
|
||||||
uint64_t usedMemory;
|
uint64_t usedMemory;
|
||||||
bool Initialized = false;
|
bool Initialized = false;
|
||||||
|
PageFrameAllocator GlobalAllocator;
|
||||||
|
|
||||||
void PageFrameAllocator::ReadEFIMemoryMap(EFI_MEMORY_DESCRIPTOR* mMap, size_t mMapSize, size_t mMapDescSize){
|
void PageFrameAllocator::ReadEFIMemoryMap(EFI_MEMORY_DESCRIPTOR* mMap, size_t mMapSize, size_t mMapDescSize){
|
||||||
if (Initialized) return;
|
if (Initialized) return;
|
||||||
|
|||||||
@@ -24,4 +24,6 @@ class PageFrameAllocator {
|
|||||||
void ReservePages(void* address, uint64_t pageCount);
|
void ReservePages(void* address, uint64_t pageCount);
|
||||||
void UnreservePage(void* address);
|
void UnreservePage(void* address);
|
||||||
void UnreservePages(void* address, uint64_t pageCount);
|
void UnreservePages(void* address, uint64_t pageCount);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
extern PageFrameAllocator GlobalAllocator;
|
||||||
65
kernel/src/paging/PageTableManager.cpp
Normal file
65
kernel/src/paging/PageTableManager.cpp
Normal 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;
|
||||||
|
}
|
||||||
9
kernel/src/paging/PageTableManager.h
Normal file
9
kernel/src/paging/PageTableManager.h
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
#pragma once
|
||||||
|
#include "paging.h"
|
||||||
|
|
||||||
|
class PageTableManager {
|
||||||
|
public:
|
||||||
|
PageTableManager(PageTable* PML4Address);
|
||||||
|
PageTable* PML4;
|
||||||
|
void MapMemory(void* virtualMemory, void* physicalMemory);
|
||||||
|
};
|
||||||
Reference in New Issue
Block a user