From eae6006d25eac170f375f6d4421c2588d04d2232 Mon Sep 17 00:00:00 2001 From: Alex Date: Fri, 31 Mar 2023 17:30:33 +0300 Subject: [PATCH] Add Virtual::GetPhysical() --- Core/Memory/VirtualMemoryManager.cpp | 35 ++++++++++++++++++++++++++++ include/memory.hpp | 7 ++++++ 2 files changed, 42 insertions(+) diff --git a/Core/Memory/VirtualMemoryManager.cpp b/Core/Memory/VirtualMemoryManager.cpp index 590d9b5..0b44e73 100644 --- a/Core/Memory/VirtualMemoryManager.cpp +++ b/Core/Memory/VirtualMemoryManager.cpp @@ -40,6 +40,41 @@ namespace Memory return false; } + void *Virtual::GetPhysical(void *VirtualAddress) + { + // 0x1000 aligned + uintptr_t Address = (uintptr_t)VirtualAddress; + Address &= 0xFFFFFFFFFFFFF000; + + PageMapIndexer Index = PageMapIndexer(Address); + PageMapLevel4 PML4 = this->Table->Entries[Index.PMLIndex]; + + PageDirectoryPointerTableEntryPtr *PDPTE = nullptr; + PageDirectoryEntryPtr *PDE = nullptr; + PageTableEntryPtr *PTE = nullptr; + + if (PML4.Present) + { + PDPTE = (PageDirectoryPointerTableEntryPtr *)((uintptr_t)PML4.GetAddress() << 12); + if (PDPTE) + if (PDPTE->Entries[Index.PDPTEIndex].Present) + { + PDE = (PageDirectoryEntryPtr *)((uintptr_t)PDPTE->Entries[Index.PDPTEIndex].GetAddress() << 12); + if (PDE) + if (PDE->Entries[Index.PDEIndex].Present) + { + PTE = (PageTableEntryPtr *)((uintptr_t)PDE->Entries[Index.PDEIndex].GetAddress() << 12); + if (PTE) + if (PTE->Entries[Index.PTEIndex].Present) + { + return (void *)((uintptr_t)PTE->Entries[Index.PTEIndex].GetAddress() << 12); + } + } + } + } + return nullptr; + } + void Virtual::Map(void *VirtualAddress, void *PhysicalAddress, uint64_t Flags) { SmartLock(this->MemoryLock); diff --git a/include/memory.hpp b/include/memory.hpp index 628c9c5..64c8b43 100644 --- a/include/memory.hpp +++ b/include/memory.hpp @@ -539,6 +539,13 @@ namespace Memory */ bool Check(void *VirtualAddress, PTFlag Flag = PTFlag::P); + /** + * @brief Get physical address of the page. + * @param VirtualAddress Virtual address of the page. + * @return Physical address of the page. + */ + void *GetPhysical(void *VirtualAddress); + /** * @brief Map page. *