mirror of
https://github.com/Fennix-Project/Kernel.git
synced 2025-05-28 15:34:33 +00:00
Updated memory mapping functions
This commit is contained in:
parent
6ae0e9db26
commit
52ef1e3b3b
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;
|
Address &= 0xFFFFFFFFFFFFF000;
|
||||||
|
|
||||||
PageMapIndexer Index = PageMapIndexer((uint64_t)Address);
|
PageMapIndexer Index = PageMapIndexer((uint64_t)Address);
|
||||||
PageDirectoryEntry PDE = this->Table->Entries[Index.PDP_i];
|
PageDirectoryEntry PDE = this->Table->Entries[Index.PDPIndex];
|
||||||
return PDE.GetFlag(Flag) ? true : false;
|
return PDE.GetFlag(Flag);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Virtual::Map(void *VirtualAddress, void *PhysicalAddress, uint64_t Flags)
|
void Virtual::Map(void *VirtualAddress, void *PhysicalAddress, uint64_t Flags)
|
||||||
@ -25,8 +25,8 @@ namespace Memory
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
PageMapIndexer Index = PageMapIndexer((uint64_t)VirtualAddress);
|
PageMapIndexer Index = PageMapIndexer((uint64_t)VirtualAddress);
|
||||||
PageDirectoryEntry PDE = this->Table->Entries[Index.PDP_i];
|
PageDirectoryEntry PDE = this->Table->Entries[Index.PDPIndex];
|
||||||
PageTable *PDP;
|
PageTable *PDP = nullptr;
|
||||||
if (!PDE.GetFlag(PTFlag::P))
|
if (!PDE.GetFlag(PTFlag::P))
|
||||||
{
|
{
|
||||||
PDP = (PageTable *)KernelAllocator.RequestPage();
|
PDP = (PageTable *)KernelAllocator.RequestPage();
|
||||||
@ -34,13 +34,13 @@ namespace Memory
|
|||||||
PDE.SetAddress((uint64_t)PDP >> 12);
|
PDE.SetAddress((uint64_t)PDP >> 12);
|
||||||
PDE.SetFlag(PTFlag::P, true);
|
PDE.SetFlag(PTFlag::P, true);
|
||||||
PDE.AddFlag(Flags);
|
PDE.AddFlag(Flags);
|
||||||
this->Table->Entries[Index.PDP_i] = PDE;
|
this->Table->Entries[Index.PDPIndex] = PDE;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
PDP = (PageTable *)((uint64_t)PDE.GetAddress() << 12);
|
PDP = (PageTable *)((uint64_t)PDE.GetAddress() << 12);
|
||||||
|
|
||||||
PDE = PDP->Entries[Index.PD_i];
|
PDE = PDP->Entries[Index.PDIndex];
|
||||||
PageTable *PD;
|
PageTable *PD = nullptr;
|
||||||
if (!PDE.GetFlag(PTFlag::P))
|
if (!PDE.GetFlag(PTFlag::P))
|
||||||
{
|
{
|
||||||
PD = (PageTable *)KernelAllocator.RequestPage();
|
PD = (PageTable *)KernelAllocator.RequestPage();
|
||||||
@ -48,13 +48,13 @@ namespace Memory
|
|||||||
PDE.SetAddress((uint64_t)PD >> 12);
|
PDE.SetAddress((uint64_t)PD >> 12);
|
||||||
PDE.SetFlag(PTFlag::P, true);
|
PDE.SetFlag(PTFlag::P, true);
|
||||||
PDE.AddFlag(Flags);
|
PDE.AddFlag(Flags);
|
||||||
PDP->Entries[Index.PD_i] = PDE;
|
PDP->Entries[Index.PDIndex] = PDE;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
PD = (PageTable *)((uint64_t)PDE.GetAddress() << 12);
|
PD = (PageTable *)((uint64_t)PDE.GetAddress() << 12);
|
||||||
|
|
||||||
PDE = PD->Entries[Index.PT_i];
|
PDE = PD->Entries[Index.PTIndex];
|
||||||
PageTable *PT;
|
PageTable *PT = nullptr;
|
||||||
if (!PDE.GetFlag(PTFlag::P))
|
if (!PDE.GetFlag(PTFlag::P))
|
||||||
{
|
{
|
||||||
PT = (PageTable *)KernelAllocator.RequestPage();
|
PT = (PageTable *)KernelAllocator.RequestPage();
|
||||||
@ -62,16 +62,16 @@ namespace Memory
|
|||||||
PDE.SetAddress((uint64_t)PT >> 12);
|
PDE.SetAddress((uint64_t)PT >> 12);
|
||||||
PDE.SetFlag(PTFlag::P, true);
|
PDE.SetFlag(PTFlag::P, true);
|
||||||
PDE.AddFlag(Flags);
|
PDE.AddFlag(Flags);
|
||||||
PD->Entries[Index.PT_i] = PDE;
|
PD->Entries[Index.PTIndex] = PDE;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
PT = (PageTable *)((uint64_t)PDE.GetAddress() << 12);
|
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.SetAddress((uint64_t)PhysicalAddress >> 12);
|
||||||
PDE.SetFlag(PTFlag::P, true);
|
PDE.SetFlag(PTFlag::P, true);
|
||||||
PDE.AddFlag(Flags);
|
PDE.AddFlag(Flags);
|
||||||
PT->Entries[Index.P_i] = PDE;
|
PT->Entries[Index.PIndex] = PDE;
|
||||||
#if defined(__amd64__)
|
#if defined(__amd64__)
|
||||||
CPU::x64::invlpg(VirtualAddress);
|
CPU::x64::invlpg(VirtualAddress);
|
||||||
#elif defined(__i386__)
|
#elif defined(__i386__)
|
||||||
@ -120,7 +120,7 @@ namespace Memory
|
|||||||
}
|
}
|
||||||
|
|
||||||
PageMapIndexer Index = PageMapIndexer((uint64_t)VirtualAddress);
|
PageMapIndexer Index = PageMapIndexer((uint64_t)VirtualAddress);
|
||||||
PageDirectoryEntry PDE = this->Table->Entries[Index.PDP_i];
|
PageDirectoryEntry PDE = this->Table->Entries[Index.PDPIndex];
|
||||||
PDE.ClearFlags();
|
PDE.ClearFlags();
|
||||||
|
|
||||||
#if defined(__amd64__) || defined(__i386__)
|
#if defined(__amd64__) || defined(__i386__)
|
||||||
|
@ -185,43 +185,14 @@ namespace Memory
|
|||||||
struct __attribute__((packed)) PageDirectoryEntry
|
struct __attribute__((packed)) PageDirectoryEntry
|
||||||
{
|
{
|
||||||
PDEData Value;
|
PDEData Value;
|
||||||
void AddFlag(uint64_t Flag) { this->Value.raw |= Flag; }
|
void AddFlag(uint64_t Flag);
|
||||||
void RemoveFlags(uint64_t Flag) { this->Value.raw &= ~Flag; }
|
void RemoveFlags(uint64_t Flag);
|
||||||
void ClearFlags() { this->Value.raw = 0; }
|
void ClearFlags();
|
||||||
void SetFlag(uint64_t Flag, bool Enabled)
|
void SetFlag(uint64_t Flag, bool Enabled);
|
||||||
{
|
bool GetFlag(uint64_t Flag);
|
||||||
this->Value.raw &= ~Flag;
|
uint64_t GetFlag();
|
||||||
if (Enabled)
|
void SetAddress(uint64_t Address);
|
||||||
this->Value.raw |= Flag;
|
uint64_t GetAddress();
|
||||||
}
|
|
||||||
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
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
struct PageTable
|
struct PageTable
|
||||||
@ -364,28 +335,11 @@ namespace Memory
|
|||||||
class PageMapIndexer
|
class PageMapIndexer
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
uint64_t PDP_i = 0;
|
uint64_t PDPIndex = 0;
|
||||||
uint64_t PD_i = 0;
|
uint64_t PDIndex = 0;
|
||||||
uint64_t PT_i = 0;
|
uint64_t PTIndex = 0;
|
||||||
uint64_t P_i = 0;
|
uint64_t PIndex = 0;
|
||||||
|
PageMapIndexer(uint64_t VirtualAddress);
|
||||||
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
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user