free, lock, reserve and unreserve pages

This commit is contained in:
2026-01-28 18:06:16 +11:00
parent 7c94d21e31
commit 5b508f4a48
8 changed files with 114 additions and 1 deletions

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@@ -31,8 +31,16 @@ void PageFrameAllocator::ReadEFIMemoryMap(EFI_MEMORY_DESCRIPTOR* mMap, size_t mM
InitBitmap(bitmapSize, largestFreeMemSeg); InitBitmap(bitmapSize, largestFreeMemSeg);
//lock pages of bitmap // lock pages of bitmap
LockPages(&PageBitmap, PageBitmap.Size / 4096 + 1);
//reserve pages of unsable/reserverd memory //reserve pages of unsable/reserverd memory
for (int i = 0; i < mMapEntries; i++) {
EFI_MEMORY_DESCRIPTOR* desc = (EFI_MEMORY_DESCRIPTOR*)((uint64_t)mMap + (i * mMapDescSize));
if (desc->type != 7) { // not efiConventionalMemory
ReservePages(desc->physAddr, desc->numPages);
}
}
} }
void PageFrameAllocator::InitBitmap(size_t bitmapSize, void* bufferAddress){ void PageFrameAllocator::InitBitmap(size_t bitmapSize, void* bufferAddress){
@@ -41,4 +49,81 @@ void PageFrameAllocator::InitBitmap(size_t bitmapSize, void* bufferAddress){
for (int i = 0; i < bitmapSize; i++){ for (int i = 0; i < bitmapSize; i++){
*(uint8_t*)(PageBitmap.Buffer + i) = 0; *(uint8_t*)(PageBitmap.Buffer + i) = 0;
} }
}
void PageFrameAllocator::FreePage(void* address) {
uint64_t index = (uint64_t)address / 4096;
if (PageBitmap[index] == false) return; // page is already free
// free page in bitmap
PageBitmap.Set(index, false);
freeMemory += 4096;
usedMemory -= 4096;
}
void PageFrameAllocator::FreePages(void* address, uint64_t pageCount) {
for (int t = 0; t < pageCount; t++) {
FreePage((void*)((uint64_t)address + (t * 4096)));
}
}
void PageFrameAllocator::LockPage(void* address) {
uint64_t index = (uint64_t)address / 4096;
if (PageBitmap[index] == true) return; // page is already locked
// lock page in bitmap
PageBitmap.Set(index, true);
freeMemory -= 4096;
usedMemory += 4096;
}
void PageFrameAllocator::LockPages(void* address, uint64_t pageCount) {
for (int t = 0; t < pageCount; t++) {
LockPage((void*)((uint64_t)address + (t * 4096)));
}
}
void PageFrameAllocator::UnreservePage(void* address) {
uint64_t index = (uint64_t)address / 4096;
if (PageBitmap[index] == false) return; // page is already free
// free page in bitmap
PageBitmap.Set(index, false);
freeMemory += 4096;
reservedMemory -= 4096;
}
void PageFrameAllocator::UnreservePages(void* address, uint64_t pageCount) {
for (int t = 0; t < pageCount; t++) {
UnreservePage((void*)((uint64_t)address + (t * 4096)));
}
}
void PageFrameAllocator::ReservePage(void* address) {
uint64_t index = (uint64_t)address / 4096;
if (PageBitmap[index] == true) return; // page is already locked
// lock page in bitmap
PageBitmap.Set(index, true);
freeMemory -= 4096;
reservedMemory += 4096;
}
void PageFrameAllocator::ReservePages(void* address, uint64_t pageCount) {
for (int t = 0; t < pageCount; t++) {
ReservePage((void*)((uint64_t)address + (t * 4096)));
}
}
uint64_t PageFrameAllocator::GetFreeRAM() {
return freeMemory;
}
uint64_t PageFrameAllocator::GetUsedRAM() {
return usedMemory;
}
uint64_t PageFrameAllocator::GetReservedRAM() {
return reservedMemory;
} }

View File

@@ -9,7 +9,18 @@ class PageFrameAllocator {
public: public:
void ReadEFIMemoryMap(EFI_MEMORY_DESCRIPTOR* mMap, size_t mMapSize, size_t mMapDescSize); void ReadEFIMemoryMap(EFI_MEMORY_DESCRIPTOR* mMap, size_t mMapSize, size_t mMapDescSize);
Bitmap PageBitmap; Bitmap PageBitmap;
void FreePage(void* address);
void FreePages(void* address, uint64_t pageCount);
void LockPage(void* address);
void LockPages(void* address, uint64_t pageCount);
uint64_t GetFreeRAM();
uint64_t GetUsedRAM();
uint64_t GetReservedRAM();
private: private:
void InitBitmap(size_t bitmapSize, void* bufferAddress); void InitBitmap(size_t bitmapSize, void* bufferAddress);
void ReservePage(void* address);
void ReservePages(void* address, uint64_t pageCount);
void UnreservePage(void* address);
void UnreservePages(void* address, uint64_t pageCount);
}; };

View File

@@ -5,6 +5,7 @@
#include "efiMemory.h" #include "efiMemory.h"
#include "memory.h" #include "memory.h"
#include "Bitmap.h" #include "Bitmap.h"
#include "PageFrameAllocator.h"
struct BootInfo { struct BootInfo {
Framebuffer* framebuffer; Framebuffer* framebuffer;
@@ -27,6 +28,22 @@ extern "C" void _start(BootInfo* bootInfo) {
uint64_t mMapEntries = bootInfo->mMapSize / bootInfo->mMapDescriptorSize; uint64_t mMapEntries = bootInfo->mMapSize / bootInfo->mMapDescriptorSize;
PageFrameAllocator newAllocator;
newAllocator.ReadEFIMemoryMap(bootInfo->mMap, bootInfo->mMapSize, bootInfo->mMapDescriptorSize);
newRenderer.Print(0xFFFFFFFF, "Free RAM: ");
newRenderer.Print(0xFF00FFFF, to_string(newAllocator.GetFreeRAM() / 1024));
newRenderer.Print(0xFF00FFFF, " KB");
newRenderer.cursorPosition = {0, newRenderer.cursorPosition.y + 18};
newRenderer.Print(0xFFFFFFFF, "Used RAM: ");
newRenderer.Print(0xFF00FFFF, to_string(newAllocator.GetUsedRAM() / 1024));
newRenderer.Print(0xFF00FFFF, " KB");
newRenderer.cursorPosition = {0, newRenderer.cursorPosition.y + 18};
newRenderer.Print(0xFFFFFFFF, "Reserved RAM: ");
newRenderer.Print(0xFF00FFFF, to_string(newAllocator.GetReservedRAM() / 1024));
newRenderer.Print(0xFF00FFFF, " KB");
newRenderer.cursorPosition = {0, newRenderer.cursorPosition.y + 18};
//newRenderer.Print(0xFFFFFFFF, to_string(GetMemorySize(bootInfo->mMap, mMapEntries, bootInfo ->mMapDescriptorSize))); //newRenderer.Print(0xFFFFFFFF, to_string(GetMemorySize(bootInfo->mMap, mMapEntries, bootInfo ->mMapDescriptorSize)));