mirror of
https://github.com/Fennix-Project/Kernel.git
synced 2025-07-11 07:19:20 +00:00
Updated memory mapping functions
This commit is contained in:
42
Core/Memory/PageDirectoryEntry.cpp
Normal file
42
Core/Memory/PageDirectoryEntry.cpp
Normal file
@ -0,0 +1,42 @@
|
||||
#include <memory.hpp>
|
||||
|
||||
namespace Memory
|
||||
{
|
||||
void PageDirectoryEntry::AddFlag(uint64_t Flag) { this->Value.raw |= Flag; }
|
||||
void PageDirectoryEntry::RemoveFlags(uint64_t Flag) { this->Value.raw &= ~Flag; }
|
||||
void PageDirectoryEntry::ClearFlags() { this->Value.raw = 0; }
|
||||
void PageDirectoryEntry::SetFlag(uint64_t Flag, bool Enabled)
|
||||
{
|
||||
this->Value.raw &= ~Flag;
|
||||
if (Enabled)
|
||||
this->Value.raw |= Flag;
|
||||
}
|
||||
bool PageDirectoryEntry::GetFlag(uint64_t Flag) { return (this->Value.raw & Flag) > 0 ? true : false; }
|
||||
uint64_t PageDirectoryEntry::GetFlag() { return this->Value.raw; }
|
||||
void PageDirectoryEntry::SetAddress(uint64_t Address)
|
||||
{
|
||||
#if defined(__amd64__)
|
||||
Address &= 0x000000FFFFFFFFFF;
|
||||
this->Value.raw &= 0xFFF0000000000FFF;
|
||||
this->Value.raw |= (Address << 12);
|
||||
#elif defined(__i386__)
|
||||
Address &= 0x000FFFFF;
|
||||
this->Value.raw &= 0xFFC00003;
|
||||
this->Value.raw |= (Address << 12);
|
||||
#elif defined(__aarch64__)
|
||||
Address &= 0x000000FFFFFFFFFF;
|
||||
this->Value.raw &= 0xFFF0000000000FFF;
|
||||
this->Value.raw |= (Address << 12);
|
||||
#endif
|
||||
}
|
||||
uint64_t PageDirectoryEntry::GetAddress()
|
||||
{
|
||||
#if defined(__amd64__)
|
||||
return (this->Value.raw & 0x000FFFFFFFFFF000) >> 12;
|
||||
#elif defined(__i386__)
|
||||
return (this->Value.raw & 0x003FFFFF000) >> 12;
|
||||
#elif defined(__aarch64__)
|
||||
return (this->Value.raw & 0x000FFFFFFFFFF000) >> 12;
|
||||
#endif
|
||||
}
|
||||
}
|
22
Core/Memory/PageMapIndexer.cpp
Normal file
22
Core/Memory/PageMapIndexer.cpp
Normal file
@ -0,0 +1,22 @@
|
||||
#include <memory.hpp>
|
||||
|
||||
namespace Memory
|
||||
{
|
||||
Virtual::PageMapIndexer::PageMapIndexer(uint64_t VirtualAddress)
|
||||
{
|
||||
#if defined(__amd64__)
|
||||
this->PDPIndex = (VirtualAddress & ((uint64_t)0x1FF << 39)) >> 39;
|
||||
this->PDIndex = (VirtualAddress & ((uint64_t)0x1FF << 30)) >> 30;
|
||||
this->PTIndex = (VirtualAddress & ((uint64_t)0x1FF << 21)) >> 21;
|
||||
this->PIndex = (VirtualAddress & ((uint64_t)0x1FF << 12)) >> 12;
|
||||
#elif defined(__i386__)
|
||||
this->PDIndex = (VirtualAddress & ((uint64_t)0x3FF << 22)) >> 22;
|
||||
this->PTIndex = (VirtualAddress & ((uint64_t)0x3FF << 12)) >> 12;
|
||||
this->PIndex = (VirtualAddress & ((uint64_t)0xFFF)) >> 0;
|
||||
#elif defined(__aarch64__)
|
||||
this->PDIndex = (VirtualAddress & ((uint64_t)0x1FF << 30)) >> 30;
|
||||
this->PTIndex = (VirtualAddress & ((uint64_t)0x1FF << 21)) >> 21;
|
||||
this->PIndex = (VirtualAddress & ((uint64_t)0x1FF << 12)) >> 12;
|
||||
#endif
|
||||
}
|
||||
}
|
@ -12,8 +12,8 @@ namespace Memory
|
||||
Address &= 0xFFFFFFFFFFFFF000;
|
||||
|
||||
PageMapIndexer Index = PageMapIndexer((uint64_t)Address);
|
||||
PageDirectoryEntry PDE = this->Table->Entries[Index.PDP_i];
|
||||
return PDE.GetFlag(Flag) ? true : false;
|
||||
PageDirectoryEntry PDE = this->Table->Entries[Index.PDPIndex];
|
||||
return PDE.GetFlag(Flag);
|
||||
}
|
||||
|
||||
void Virtual::Map(void *VirtualAddress, void *PhysicalAddress, uint64_t Flags)
|
||||
@ -25,8 +25,8 @@ namespace Memory
|
||||
return;
|
||||
}
|
||||
PageMapIndexer Index = PageMapIndexer((uint64_t)VirtualAddress);
|
||||
PageDirectoryEntry PDE = this->Table->Entries[Index.PDP_i];
|
||||
PageTable *PDP;
|
||||
PageDirectoryEntry PDE = this->Table->Entries[Index.PDPIndex];
|
||||
PageTable *PDP = nullptr;
|
||||
if (!PDE.GetFlag(PTFlag::P))
|
||||
{
|
||||
PDP = (PageTable *)KernelAllocator.RequestPage();
|
||||
@ -34,13 +34,13 @@ namespace Memory
|
||||
PDE.SetAddress((uint64_t)PDP >> 12);
|
||||
PDE.SetFlag(PTFlag::P, true);
|
||||
PDE.AddFlag(Flags);
|
||||
this->Table->Entries[Index.PDP_i] = PDE;
|
||||
this->Table->Entries[Index.PDPIndex] = PDE;
|
||||
}
|
||||
else
|
||||
PDP = (PageTable *)((uint64_t)PDE.GetAddress() << 12);
|
||||
|
||||
PDE = PDP->Entries[Index.PD_i];
|
||||
PageTable *PD;
|
||||
PDE = PDP->Entries[Index.PDIndex];
|
||||
PageTable *PD = nullptr;
|
||||
if (!PDE.GetFlag(PTFlag::P))
|
||||
{
|
||||
PD = (PageTable *)KernelAllocator.RequestPage();
|
||||
@ -48,13 +48,13 @@ namespace Memory
|
||||
PDE.SetAddress((uint64_t)PD >> 12);
|
||||
PDE.SetFlag(PTFlag::P, true);
|
||||
PDE.AddFlag(Flags);
|
||||
PDP->Entries[Index.PD_i] = PDE;
|
||||
PDP->Entries[Index.PDIndex] = PDE;
|
||||
}
|
||||
else
|
||||
PD = (PageTable *)((uint64_t)PDE.GetAddress() << 12);
|
||||
|
||||
PDE = PD->Entries[Index.PT_i];
|
||||
PageTable *PT;
|
||||
PDE = PD->Entries[Index.PTIndex];
|
||||
PageTable *PT = nullptr;
|
||||
if (!PDE.GetFlag(PTFlag::P))
|
||||
{
|
||||
PT = (PageTable *)KernelAllocator.RequestPage();
|
||||
@ -62,16 +62,16 @@ namespace Memory
|
||||
PDE.SetAddress((uint64_t)PT >> 12);
|
||||
PDE.SetFlag(PTFlag::P, true);
|
||||
PDE.AddFlag(Flags);
|
||||
PD->Entries[Index.PT_i] = PDE;
|
||||
PD->Entries[Index.PTIndex] = PDE;
|
||||
}
|
||||
else
|
||||
PT = (PageTable *)((uint64_t)PDE.GetAddress() << 12);
|
||||
|
||||
PDE = PT->Entries[Index.P_i];
|
||||
PDE = PT->Entries[Index.PIndex];
|
||||
PDE.SetAddress((uint64_t)PhysicalAddress >> 12);
|
||||
PDE.SetFlag(PTFlag::P, true);
|
||||
PDE.AddFlag(Flags);
|
||||
PT->Entries[Index.P_i] = PDE;
|
||||
PT->Entries[Index.PIndex] = PDE;
|
||||
#if defined(__amd64__)
|
||||
CPU::x64::invlpg(VirtualAddress);
|
||||
#elif defined(__i386__)
|
||||
@ -120,7 +120,7 @@ namespace Memory
|
||||
}
|
||||
|
||||
PageMapIndexer Index = PageMapIndexer((uint64_t)VirtualAddress);
|
||||
PageDirectoryEntry PDE = this->Table->Entries[Index.PDP_i];
|
||||
PageDirectoryEntry PDE = this->Table->Entries[Index.PDPIndex];
|
||||
PDE.ClearFlags();
|
||||
|
||||
#if defined(__amd64__) || defined(__i386__)
|
||||
|
Reference in New Issue
Block a user