switch from bitfields to enums

This commit is contained in:
2026-01-28 21:16:24 +11:00
parent 1ef6ed223e
commit a265558b98
8 changed files with 99 additions and 66 deletions

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

BIN
kernel/lib/paging/paging.o Normal file

Binary file not shown.

View File

@@ -8,58 +8,59 @@ PageTableManager::PageTableManager(PageTable* PML4Address) {
this->PML4 = PML4Address; this->PML4 = PML4Address;
} }
void PageTableManager::MapMemory(void* virtualMemory, void* physicalMemory) { void PageTableManager::MapMemory(void* virtualMemory, void* physicalMemory){
PageMapIndexer indexer = PageMapIndexer((uint64_t)virtualMemory); PageMapIndexer indexer = PageMapIndexer((uint64_t)virtualMemory);
PageDirectoryEntry PDE; PageDirectoryEntry PDE;
PDE = PML4->entries[indexer.PDP_i]; PDE = PML4->entries[indexer.PDP_i];
PageTable* PDP; PageTable* PDP;
if (!PDE.GetFlag(PT_Flag::Present)){
PDP = (PageTable*)GlobalAllocator.RequestPage();
memset(PDP, 0, 0x1000);
PDE.SetAddress((uint64_t)PDP >> 12);
PDE.SetFlag(PT_Flag::Present, true);
PDE.SetFlag(PT_Flag::ReadWrite, true);
PML4->entries[indexer.PDP_i] = PDE;
}
else
{
PDP = (PageTable*)((uint64_t)PDE.GetAddress() << 12);
}
if (!PDE.Present) {
PDP = (PageTable*)GlobalAllocator.RequestPage();
memset(PDP, 0, 4096); // zero out the new page
PDE.Address = (uint64_t)PDP >> 12; PDE = PDP->entries[indexer.PD_i];
PDE.Present = true; PageTable* PD;
PDE.ReadWrite = true; if (!PDE.GetFlag(PT_Flag::Present)){
PML4->entries[indexer.PDP_i] = PDE; PD = (PageTable*)GlobalAllocator.RequestPage();
} else { memset(PD, 0, 0x1000);
PDP = (PageTable*)((uint64_t)PDE.Address << 12); PDE.SetAddress((uint64_t)PD >> 12);
} PDE.SetFlag(PT_Flag::Present, true);
PDE.SetFlag(PT_Flag::ReadWrite, true);
PDP->entries[indexer.PD_i] = PDE;
}
else
{
PD = (PageTable*)((uint64_t)PDE.GetAddress() << 12);
}
PDE = PDP->entries[indexer.PD_i]; PDE = PD->entries[indexer.PT_i];
PageTable* PD; PageTable* PT;
if (!PDE.GetFlag(PT_Flag::Present)){
PT = (PageTable*)GlobalAllocator.RequestPage();
memset(PT, 0, 0x1000);
PDE.SetAddress((uint64_t)PT >> 12);
PDE.SetFlag(PT_Flag::Present, true);
PDE.SetFlag(PT_Flag::ReadWrite, true);
PD->entries[indexer.PT_i] = PDE;
}
else
{
PT = (PageTable*)((uint64_t)PDE.GetAddress() << 12);
}
if (!PDE.Present) { PDE = PT->entries[indexer.P_i];
PD = (PageTable*)GlobalAllocator.RequestPage(); PDE.SetAddress((uint64_t)physicalMemory >> 12);
memset(PD, 0, 4096); // zero out the new page PDE.SetFlag(PT_Flag::Present, true);
PDE.SetFlag(PT_Flag::ReadWrite, true);
PDE.Address = (uint64_t)PD >> 12; PT->entries[indexer.P_i] = PDE;
PDE.Present = true;
PDE.ReadWrite = true;
PDP->entries[indexer.PD_i] = PDE;
} else {
PD = (PageTable*)((uint64_t)PDE.Address << 12);
}
PDE = PD->entries[indexer.PT_i];
PageTable* PT;
if (!PDE.Present) {
PT = (PageTable*)GlobalAllocator.RequestPage();
memset(PT, 0, 4096); // zero out the new page
PDE.Address = (uint64_t)PT >> 12;
PDE.Present = true;
PDE.ReadWrite = true;
PD->entries[indexer.PT_i] = PDE;
} else {
PT = (PageTable*)((uint64_t)PDE.Address << 12);
}
PDE = PT->entries[indexer.P_i];
PDE.Address = (uint64_t)physicalMemory >> 12;
PDE.Present = true;
PDE.ReadWrite = true;
PT->entries[indexer.P_i] = PDE;
} }

View File

@@ -0,0 +1,25 @@
#include "paging.h"
void PageDirectoryEntry::SetFlag(PT_Flag flag, bool enabled) {
uint64_t bitSelector = (uint64_t)1 << flag;
Value %= ~bitSelector;
if (enabled) {
Value |= bitSelector;
}
}
bool PageDirectoryEntry::GetFlag(PT_Flag flag) {
uint64_t bitSelector = (uint64_t)1 << flag;
return Value & bitSelector > 0 ? true : false;
}
uint64_t PageDirectoryEntry::GetAddress() {
return (Value & 0x000FFFFFFFFFF000) >> 12;
}
void PageDirectoryEntry::SetAddress(uint64_t address) {
address &= 0x000000FFFFFFFFFF;
Value &= 0xFFF0000000000FFF;
Value |= (address << 12);
}

View File

@@ -1,21 +1,28 @@
#pragma once #pragma once
#include <stdint.h> #include <stdint.h>
struct PageDirectoryEntry { enum PT_Flag {
bool Present : 1; // memory exists and can be accessed Present = 0,
bool ReadWrite : 1; // ReadWrite = 1,
bool UserSuper : 1; // memory can only be accessed by supervisor or both user and supervisor UserSuper = 2,
bool WriteThrough : 1; // WriteThrough = 3,
bool CacheDisabled : 1; // CacheDisabled = 4,
bool Accessed : 1; // set when cpu accesses page Accessed = 5,
bool ignore0 : 1; // LargerPages = 7,
bool LargerPages : 1; // Custom0 = 9,
bool ignore1 : 1; // Custom1 = 10,
uint8_t Available : 3; // Custom2 = 11,
uint64_t Address : 52; // physical address NX = 63 // no-execute, only if supported
};
struct PageDirectoryEntry {
uint64_t Value;
void SetFlag(PT_Flag flag, bool enabled);
bool GetFlag(PT_Flag flag);
void SetAddress(uint64_t address);
uint64_t GetAddress();
}; };
struct PageTable { struct PageTable {
PageDirectoryEntry entries[512]; PageDirectoryEntry entries [512];
}__attribute__((aligned(0x1000))); }__attribute__((aligned(0x1000)));