Wrote physical memory manager

This commit is contained in:
2026-03-20 18:18:15 +01:00
parent 0214b4da31
commit 93ef197e58
7 changed files with 358 additions and 99 deletions
+12 -7
View File
@@ -2,13 +2,16 @@
#include <kernel.h>
#include <limine.h>
/*
* This defines our expected Limine version
*/
static volatile LIMINE_BASE_REVISION(4);
struct bootloader bootloader = { 0 };
static struct limine_paging_mode_request lim_paging_req = {
.id = LIMINE_PAGING_MODE_REQUEST,
.revision = 4,
.mode = LIMINE_PAGING_MODE_X86_64_4LVL,
};
static struct limine_paging_mode_request lim_paging_req =
{ .id = LIMINE_PAGING_MODE_REQUEST, .revision = 4,
.mode = LIMINE_PAGING_MODE_X86_64_4LVL, };
static struct limine_kernel_address_request lim_kaddr_req =
{ .id = LIMINE_KERNEL_ADDRESS_REQUEST, .revision = 4 };
@@ -23,6 +26,8 @@ static struct limine_framebuffer_request lim_fbuf_req =
{ .id = LIMINE_FRAMEBUFFER_REQUEST, .revision = 0 };
int boot_init(void) {
if (LIMINE_BASE_REVISION_SUPPORTED == false) panic("Unsupported Limine base revision.");
struct limine_paging_mode_response* lim_paging_res = lim_paging_req.response;
struct limine_kernel_address_response* lim_kaddr_res = lim_kaddr_req.response;
struct limine_hhdm_response* lim_hhdm_res = lim_hhdm_req.response;
@@ -36,10 +41,10 @@ int boot_init(void) {
bootloader.mm_entries = lim_memmap_res->entries;
bootloader.mm_entry_count = lim_memmap_res->entry_count;
bootloader.mm_total = 0;
bootloader.mm_total_memory = 0;
for (size_t i = 0; i < bootloader.mm_entry_count; i++) {
struct limine_memmap_entry* entry = bootloader.mm_entries[i];
if (entry->type != LIMINE_MEMMAP_USABLE) bootloader.mm_total += entry->length;
if (entry->type == LIMINE_MEMMAP_USABLE) bootloader.mm_total_memory += entry->length;
}
bootloader.fb_entries = lim_fbuf_res->framebuffers;
+23 -65
View File
@@ -1,86 +1,44 @@
#include "bitmap.h"
#include <bootloader.h>
#include <kernel.h>
#include <limine.h>
#include <paging.h>
#include <string.h>
#include <serial.h>
#include <pmm.h>
// Bitmap
bitmap_t pmm_bitmap;
uint64_t* pmm_head = NULL;
int pmm_init(void) {
pmm_bitmap.blocks = dceil(bootloader.mm_total, BLOCK_SIZE);
pmm_bitmap.bytes = dceil(pmm_bitmap.blocks, 8);
// # of pagetabs required to map the bitmap
size_t ptcount = dceil(dceil(pmm_bitmap.bytes, PAGE_SIZE), 1024);
// Find enough space for the bitmap
struct limine_memmap_entry* mm = NULL;
debugf("[pmm] Physical memory map:\n"); // We're gonna use the loop to also print the memmap
for (size_t i = 0; i < bootloader.mm_entry_count; i++) {
struct limine_memmap_entry* entry = bootloader.mm_entries[i];
debugf(
"\t0x%x - 0x%x (%d bytes), type %d\n",
entry->base, entry->base + entry->length, entry->length, entry->type
);
if (entry->type != LIMINE_MEMMAP_USABLE || entry->length < pmm_bitmap.bytes) continue;
mm = entry;
}
if (entry->type != LIMINE_MEMMAP_USABLE) continue;
if (!mm) {
debugf("[pmm] Not enough memory! Needs %d bytes.\n", pmm_bitmap.bytes);
return 1;
}
size_t bitmap_start_phys = mm->base;
pmm_bitmap.map = (void*)(bitmap_start_phys + bootloader.hhdm_offset);
// Set all blocks as used
bmap_set_all(&pmm_bitmap, 1);
debugf("[pmm] Memory map:\n"); // Use the loop to also print the memmap
for (size_t i = 0; i < bootloader.mm_entry_count; i++) {
struct limine_memmap_entry* entry = bootloader.mm_entries[i];
if (entry->length == 0) continue; // Ignore dummy entries
if (entry->type == LIMINE_MEMMAP_USABLE) {
bmap_set_area(&pmm_bitmap, entry->base, entry->length, 0);
for (offset_t offset = 0; offset < entry->length; offset += PAGE_SIZE) {
uint64_t* addr = (uint64_t*)(entry->base + offset + bootloader.hhdm_offset);
*addr = (uint64_t)pmm_head;
pmm_head = addr;
}
// Print entry
debugf("\tRegion %2d: 0x%08x - 0x%08x (%d bytes) %s\n",
i + 1, entry->base, entry->base + entry->length - 1, entry->length,
(entry->type == LIMINE_MEMMAP_USABLE) ? "AVAILABLE" : "RESERVED");
}
bmap_set_area(&pmm_bitmap, bitmap_start_phys, pmm_bitmap.bytes, 1);
pmm_bitmap.used = 0;
// Beatiful print statement :)
debugf(
"[pmm] Physical memory manager initialized.\n"
"\tAvailable memory: %d blocks free (%d bytes)\n"
"\tBitmap at 0x%08x in physical memory\n"
"\tBitmap is %d bytes contained in %d pages\n",
pmm_bitmap.blocks - pmm_bitmap.used,
(pmm_bitmap.blocks - pmm_bitmap.used) * BLOCK_SIZE,
bitmap_start_phys, pmm_bitmap.bytes
);
pmm_bitmap.ready = 1;
return 0;
}
size_t pmm_alloc(void) {
if (pmm_bitmap.blocks - pmm_bitmap.used == 0) return 0;
size_t base = bmap_get_area(&pmm_bitmap, 1, 0);
if (base == BMAP_NOT_FOUND) return 0;
bmap_set(&pmm_bitmap, base, 1);
return (size_t)bmap_ptr(&pmm_bitmap, base);
void* pmm_alloc() {
uint64_t* addr = pmm_head;
pmm_head = (uint64_t*)(*addr);
return addr - bootloader.hhdm_offset;
}
void pmm_free(size_t page) {
size_t base = page / BLOCK_SIZE;
bmap_set_area(&pmm_bitmap, base, 1, 0);
bmap_set(&pmm_bitmap, 0, 1);
void pmm_free(void* page) {
uint64_t* addr = (uint64_t*)(page - bootloader.hhdm_offset);
*addr = (uint64_t)pmm_head;
pmm_head = addr;
}