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

@@ -14,52 +14,53 @@ void PageTableManager::MapMemory(void* virtualMemory, void* physicalMemory) {
PDE = PML4->entries[indexer.PDP_i];
PageTable* PDP;
if (!PDE.Present) {
if (!PDE.GetFlag(PT_Flag::Present)){
PDP = (PageTable*)GlobalAllocator.RequestPage();
memset(PDP, 0, 4096); // zero out the new page
PDE.Address = (uint64_t)PDP >> 12;
PDE.Present = true;
PDE.ReadWrite = true;
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.Address << 12);
}
else
{
PDP = (PageTable*)((uint64_t)PDE.GetAddress() << 12);
}
PDE = PDP->entries[indexer.PD_i];
PageTable* PD;
if (!PDE.Present) {
if (!PDE.GetFlag(PT_Flag::Present)){
PD = (PageTable*)GlobalAllocator.RequestPage();
memset(PD, 0, 4096); // zero out the new page
PDE.Address = (uint64_t)PD >> 12;
PDE.Present = true;
PDE.ReadWrite = true;
memset(PD, 0, 0x1000);
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.Address << 12);
}
else
{
PD = (PageTable*)((uint64_t)PDE.GetAddress() << 12);
}
PDE = PD->entries[indexer.PT_i];
PageTable* PT;
if (!PDE.Present) {
if (!PDE.GetFlag(PT_Flag::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;
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.Address << 12);
}
else
{
PT = (PageTable*)((uint64_t)PDE.GetAddress() << 12);
}
PDE = PT->entries[indexer.P_i];
PDE.Address = (uint64_t)physicalMemory >> 12;
PDE.Present = true;
PDE.ReadWrite = true;
PDE.SetAddress((uint64_t)physicalMemory >> 12);
PDE.SetFlag(PT_Flag::Present, true);
PDE.SetFlag(PT_Flag::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,19 +1,26 @@
#pragma once
#include <stdint.h>
struct PageDirectoryEntry {
bool Present : 1; // memory exists and can be accessed
bool ReadWrite : 1; //
bool UserSuper : 1; // memory can only be accessed by supervisor or both user and supervisor
bool WriteThrough : 1; //
bool CacheDisabled : 1; //
bool Accessed : 1; // set when cpu accesses page
bool ignore0 : 1; //
bool LargerPages : 1; //
bool ignore1 : 1; //
uint8_t Available : 3; //
uint64_t Address : 52; // physical address
enum PT_Flag {
Present = 0,
ReadWrite = 1,
UserSuper = 2,
WriteThrough = 3,
CacheDisabled = 4,
Accessed = 5,
LargerPages = 7,
Custom0 = 9,
Custom1 = 10,
Custom2 = 11,
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 {