57 lines
1.4 KiB
C++
57 lines
1.4 KiB
C++
|
#include "memory/VMM.h"
|
||
|
|
||
|
namespace Paging
|
||
|
{
|
||
|
void VirtualMemoryManager::Init()
|
||
|
{
|
||
|
asm volatile("mov %%cr3, %0" : "=r"(PML4));
|
||
|
}
|
||
|
|
||
|
void VirtualMemoryManager::Init(PageTable* PML4)
|
||
|
{
|
||
|
this->PML4 = PML4;
|
||
|
}
|
||
|
|
||
|
void VirtualMemoryManager::Unmap(uint64_t virtualAddress)
|
||
|
{
|
||
|
virtualAddress >>= 12;
|
||
|
uint64_t P_i = virtualAddress & 0x1ff;
|
||
|
virtualAddress >>= 9;
|
||
|
uint64_t PT_i = virtualAddress & 0x1ff;
|
||
|
virtualAddress >>= 9;
|
||
|
uint64_t PD_i = virtualAddress & 0x1ff;
|
||
|
virtualAddress >>= 9;
|
||
|
uint64_t PDP_i = virtualAddress & 0x1ff;
|
||
|
|
||
|
PageDirectoryEntry PDE;
|
||
|
|
||
|
PDE = PML4->entries[PDP_i];
|
||
|
PageTable* PDP;
|
||
|
if (!PDE.Present)
|
||
|
{
|
||
|
return; // Already unmapped
|
||
|
}
|
||
|
else { PDP = (PageTable*)((uint64_t)PDE.Address << 12); }
|
||
|
|
||
|
PDE = PDP->entries[PD_i];
|
||
|
PageTable* PD;
|
||
|
if (!PDE.Present)
|
||
|
{
|
||
|
return; // Already unmapped
|
||
|
}
|
||
|
else { PD = (PageTable*)((uint64_t)PDE.Address << 12); }
|
||
|
|
||
|
PDE = PD->entries[PT_i];
|
||
|
PageTable* PT;
|
||
|
if (!PDE.Present)
|
||
|
{
|
||
|
return; // Already unmapped
|
||
|
}
|
||
|
else { PT = (PageTable*)((uint64_t)PDE.Address << 12); }
|
||
|
|
||
|
PDE = PT->entries[P_i];
|
||
|
PDE.Present = false;
|
||
|
PDE.ReadWrite = false;
|
||
|
PT->entries[P_i] = PDE;
|
||
|
}
|
||
|
}
|