2026-01-28 17:34:08 +11:00
|
|
|
#include "PageFrameAllocator.h"
|
|
|
|
|
|
|
|
|
|
uint64_t freeMemory;
|
|
|
|
|
uint64_t reservedMemory;
|
|
|
|
|
uint64_t usedMemory;
|
|
|
|
|
bool Initialized = false;
|
|
|
|
|
|
|
|
|
|
void PageFrameAllocator::ReadEFIMemoryMap(EFI_MEMORY_DESCRIPTOR* mMap, size_t mMapSize, size_t mMapDescSize){
|
|
|
|
|
if (Initialized) return;
|
|
|
|
|
|
|
|
|
|
Initialized = true;
|
|
|
|
|
|
|
|
|
|
uint64_t mMapEntries = mMapSize / mMapDescSize;
|
|
|
|
|
|
|
|
|
|
void* largestFreeMemSeg = NULL;
|
2026-01-28 17:46:17 +11:00
|
|
|
size_t largestFreeMemSegSize = 0;
|
2026-01-28 17:34:08 +11:00
|
|
|
|
2026-01-28 17:46:17 +11:00
|
|
|
for (int i = 0; i < mMapEntries; i++) {
|
2026-01-28 17:34:08 +11:00
|
|
|
EFI_MEMORY_DESCRIPTOR* desc = (EFI_MEMORY_DESCRIPTOR*)((uint64_t)mMap + (i * mMapDescSize));
|
2026-01-28 17:46:17 +11:00
|
|
|
if (desc->type == 7) {
|
|
|
|
|
if (desc -> numPages * 4096 > largestFreeMemSegSize) {
|
2026-01-28 17:34:08 +11:00
|
|
|
largestFreeMemSeg = desc-> physAddr;
|
|
|
|
|
largestFreeMemSegSize = desc -> numPages * 4096;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-28 17:46:17 +11:00
|
|
|
uint64_t memorySize = GetMemorySize(mMap, mMapEntries, mMapDescSize);
|
2026-01-28 17:34:08 +11:00
|
|
|
freeMemory = memorySize;
|
2026-01-28 17:46:17 +11:00
|
|
|
uint64_t bitmapSize = memorySize / 4096 / 8 + 1;
|
2026-01-28 17:34:08 +11:00
|
|
|
|
2026-01-28 17:46:17 +11:00
|
|
|
InitBitmap(bitmapSize, largestFreeMemSeg);
|
2026-01-28 17:34:08 +11:00
|
|
|
|
|
|
|
|
//lock pages of bitmap
|
|
|
|
|
//reserve pages of unsable/reserverd memory
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-28 17:46:17 +11:00
|
|
|
void PageFrameAllocator::InitBitmap(size_t bitmapSize, void* bufferAddress){
|
2026-01-28 17:34:08 +11:00
|
|
|
PageBitmap.Size = bitmapSize;
|
2026-01-28 17:46:17 +11:00
|
|
|
PageBitmap.Buffer = (uint8_t*)bufferAddress;
|
2026-01-28 17:34:08 +11:00
|
|
|
for (int i = 0; i < bitmapSize; i++){
|
2026-01-28 17:46:17 +11:00
|
|
|
*(uint8_t*)(PageBitmap.Buffer + i) = 0;
|
2026-01-28 17:34:08 +11:00
|
|
|
}
|
|
|
|
|
}
|