Started Page Frame Allocator

This commit is contained in:
2026-01-28 17:34:08 +11:00
parent 6ee9dbc0f2
commit 459972c176
7 changed files with 131 additions and 11 deletions

22
kernel/src/Bitmap.cpp Normal file
View File

@@ -0,0 +1,22 @@
#include "Bitmap.h"
bool Bitmap::operator[](uint64_t index){
uint64_t byteIndex = index / 8;
uint8_t bitIndex = index % 8;
uint8_t bitIndexer = 0b10000000 >> bitIndex;
if ((Buffer[byteIndex] & bitIndexer) > 0){
return true;
}
return false;
};
void Bitmap::Set(uint64_t index, bool value){
uint64_t byteIndex = index / 8;
uint8_t bitIndex = index % 8;
uint8_t bitIndexer = 0b10000000 >> bitIndex;
Buffer[byteIndex] &= ~bitIndexer;
if (value) {
Buffer[byteIndex] |= bitIndexer;
}
}

11
kernel/src/Bitmap.h Normal file
View File

@@ -0,0 +1,11 @@
#pragma once
#include <stddef.h>
#include <stdint.h>
class Bitmap{
public:
size_t Size;
uint8_t* Buffer;
bool operator[](uint64_t index);
void Set(uint64_t index, bool value);
};

View File

@@ -0,0 +1,44 @@
#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;
size_t largestFreeMemSegSize = 0
for (int i = 0; i < mMapEntries; i++){
EFI_MEMORY_DESCRIPTOR* desc = (EFI_MEMORY_DESCRIPTOR*)((uint64_t)mMap + (i * mMapDescSize));
if (desc->type == 7){
if (desc -> numPages * 4096 > largestFreeMemSegSize); {
largestFreeMemSeg = desc-> physAddr;
largestFreeMemSegSize = desc -> numPages * 4096;
}
}
}
uint64_t memorySize = GetMemorySize(mMap, mMapEntries, mMapDescSize)
freeMemory = memorySize;
uint64_t bitmap = memorySize / 4096 / 8 + 1
InitBitmap(bitmapSize, largestFreeMemSeg)
//lock pages of bitmap
//reserve pages of unsable/reserverd memory
}
void PageFrameAllocator::InitBitmap(size_t bitmap, void* bufferAddress){
PageBitmap.Size = bitmapSize;
PageBitmap.Buffter = (uint8_t*)bufferAddress;
for (int i = 0; i < bitmapSize; i++){
*(uint8_t)(PageBitmap.Buffer + t) = 0;
}
}

View File

@@ -0,0 +1,14 @@
#pragma once
#include "efiMemory.h"
#include <stdint.h>
#include "Bitmap.h"
#include "memory.h"
class pageFrameAllocator {
public:
void ReadEFIMemoryMap(EFI_MEMORY_DESCRIPTOR* mMap, size_t mMapSize, size_t mMapDescSize);
private:
void InitBitmap(size_t bitmap, void* bufferAddress);
}

View File

@@ -3,12 +3,13 @@
#include "BasicRenderer.h" #include "BasicRenderer.h"
#include "cstr.h" #include "cstr.h"
#include "efiMemory.h" #include "efiMemory.h"
#include "memory.h"
#include "Bitmap.h"
struct BootInfo { struct BootInfo {
Framebuffer* framebuffer; Framebuffer* framebuffer;
PSF1_FONT* psf1_Font; PSF1_FONT* psf1_Font;
void* mMap; EFI_MEMORY_DESCRIPTOR* mMap;
uint64_t mMapSize; uint64_t mMapSize;
uint64_t mMapDescriptorSize; uint64_t mMapDescriptorSize;
}; };
@@ -18,20 +19,25 @@ struct BootInfo {
extern "C" void __stack_chk_fail(void) { extern "C" void __stack_chk_fail(void) {
return; return;
} }
uint8_t testBuffer[20];
extern "C" void _start(BootInfo* bootInfo) { extern "C" void _start(BootInfo* bootInfo) {
BasicRenderer newRenderer = BasicRenderer(bootInfo->framebuffer, bootInfo->psf1_Font); BasicRenderer newRenderer = BasicRenderer(bootInfo->framebuffer, bootInfo->psf1_Font);
// display information of memory map // display information of memory map
uint64_t mMapEntries = bootInfo->mMapSize / bootInfo->mMapDescriptorSize; uint64_t mMapEntries = bootInfo->mMapSize / bootInfo->mMapDescriptorSize;
for (int i = 0; i < mMapEntries; i++) {
EFI_MEMORY_DESCRIPTOR* desc = (EFI_MEMORY_DESCRIPTOR*)((uint64_t)bootInfo->mMap + (i * bootInfo->mMapDescriptorSize));
newRenderer.Print(0xFFFFFFFF, EFI_MEMORY_TYPE_STRINGS[desc->type]); //newRenderer.Print(0xFFFFFFFF, to_string(GetMemorySize(bootInfo->mMap, mMapEntries, bootInfo ->mMapDescriptorSize)));
newRenderer.Print(0xFFFFFFFF, " ");
newRenderer.Print(0xFFFF00FF, to_string(desc->numPages * 4096 / 1024));
newRenderer.Print(0xFFFF00FF, " KB"); // for (int i = 0; i < mMapEntries; i++) {
newRenderer.cursorPosition = {0, newRenderer.cursorPosition.y + 18}; // EFI_MEMORY_DESCRIPTOR* desc = (EFI_MEMORY_DESCRIPTOR*)((uint64_t)bootInfo ->mMap (i * bootinfo->mMapDescriptorSize));
} // newRenderer.Print(0xFFFFFFFF, EFI_MEMORY_TYPE_STRINGS[desc->type]);
// newRenderer.Print(0xFFFFFFFF, " ");
// newRenderer.Print(0xFFFF00FF, to_string(desc->numPages * 4096 / 1024));
// newRenderer.Print(0xFFFF00FF, " KB");
// newRenderer.cursorPosition = {0, newRenderer.cursorPosition.y + 18};
// }
return; return;
} }

17
kernel/src/memory.cpp Normal file
View File

@@ -0,0 +1,17 @@
#include "memory.h"
uint64_t GetMemorySize(EFI_MEMORY_DESCRIPTOR* mMap, uint64_t mMapEntries, uint64_t mMapDescriptorSize){
static uint64_t memorySizeBytes = 0;
if (memorySizeBytes > 0) return memorySizeBytes;
for (int i = 0; i < mMapEntries; i++){
EFI_MEMORY_DESCRIPTOR* desc = (EFI_MEMORY_DESCRIPTOR*)((uint64_t)mMap + (i * mMapDescriptorSize));
memorySizeBytes += desc->numPages * 4096;
}
return memorySizeBytes;
}

6
kernel/src/memory.h Normal file
View File

@@ -0,0 +1,6 @@
#pragma once
#include <stdint.h>
#include "efiMemory.h"
uint64_t GetMemorySize(EFI_MEMORY_DESCRIPTOR* mMap, uint64_t mMapEntries, uint64_t mMapDescSize);