diff --git a/Core/Memory/PageDirectoryEntry.cpp b/Core/Memory/PageDirectoryEntry.cpp new file mode 100644 index 0000000..1fdbddf --- /dev/null +++ b/Core/Memory/PageDirectoryEntry.cpp @@ -0,0 +1,42 @@ +#include + +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 + } +} diff --git a/Core/Memory/PageMapIndexer.cpp b/Core/Memory/PageMapIndexer.cpp new file mode 100644 index 0000000..8137291 --- /dev/null +++ b/Core/Memory/PageMapIndexer.cpp @@ -0,0 +1,22 @@ +#include + +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 + } +} diff --git a/Core/Memory/VirtualMemoryManager.cpp b/Core/Memory/VirtualMemoryManager.cpp index 9d5d460..fd32698 100644 --- a/Core/Memory/VirtualMemoryManager.cpp +++ b/Core/Memory/VirtualMemoryManager.cpp @@ -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__) diff --git a/include/memory.hpp b/include/memory.hpp index 54764e6..b92292c 100644 --- a/include/memory.hpp +++ b/include/memory.hpp @@ -185,43 +185,14 @@ namespace Memory struct __attribute__((packed)) PageDirectoryEntry { PDEData Value; - void AddFlag(uint64_t Flag) { this->Value.raw |= Flag; } - void RemoveFlags(uint64_t Flag) { this->Value.raw &= ~Flag; } - void ClearFlags() { this->Value.raw = 0; } - void SetFlag(uint64_t Flag, bool Enabled) - { - this->Value.raw &= ~Flag; - if (Enabled) - this->Value.raw |= Flag; - } - bool GetFlag(uint64_t Flag) { return (this->Value.raw & Flag) > 0 ? true : false; } - uint64_t GetFlag() { return this->Value.raw; } - void 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 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 - } + void AddFlag(uint64_t Flag); + void RemoveFlags(uint64_t Flag); + void ClearFlags(); + void SetFlag(uint64_t Flag, bool Enabled); + bool GetFlag(uint64_t Flag); + uint64_t GetFlag(); + void SetAddress(uint64_t Address); + uint64_t GetAddress(); }; struct PageTable @@ -364,28 +335,11 @@ namespace Memory class PageMapIndexer { public: - uint64_t PDP_i = 0; - uint64_t PD_i = 0; - uint64_t PT_i = 0; - uint64_t P_i = 0; - - PageMapIndexer(uint64_t VirtualAddress) - { -#if defined(__amd64__) - this->PDP_i = (VirtualAddress & ((uint64_t)0x1FF << 39)) >> 39; - this->PD_i = (VirtualAddress & ((uint64_t)0x1FF << 30)) >> 30; - this->PT_i = (VirtualAddress & ((uint64_t)0x1FF << 21)) >> 21; - this->P_i = (VirtualAddress & ((uint64_t)0x1FF << 12)) >> 12; -#elif defined(__i386__) - this->PD_i = (VirtualAddress & ((uint64_t)0x3FF << 22)) >> 22; - this->PT_i = (VirtualAddress & ((uint64_t)0x3FF << 12)) >> 12; - this->P_i = (VirtualAddress & ((uint64_t)0xFFF)) >> 0; -#elif defined(__aarch64__) - this->PD_i = (VirtualAddress & ((uint64_t)0x1FF << 30)) >> 30; - this->PT_i = (VirtualAddress & ((uint64_t)0x1FF << 21)) >> 21; - this->P_i = (VirtualAddress & ((uint64_t)0x1FF << 12)) >> 12; -#endif - } + uint64_t PDPIndex = 0; + uint64_t PDIndex = 0; + uint64_t PTIndex = 0; + uint64_t PIndex = 0; + PageMapIndexer(uint64_t VirtualAddress); }; public: