Fixed #GP from pmm_free()

This commit is contained in:
2026-03-22 14:27:30 +01:00
parent e8d5a8be35
commit 74d1edbf9f
+17 -4
View File
@@ -9,35 +9,48 @@
uint64_t* pmm_head = NULL; uint64_t* pmm_head = NULL;
int pmm_init(void) { int pmm_init(void) {
size_t total_mem = 0;
debugf("[pmm] Physical memory map:\n"); // We're gonna use the loop to also print the memmap 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++) { for (size_t i = 0; i < bootloader.mm_entry_count; i++) {
struct limine_memmap_entry* entry = bootloader.mm_entries[i]; struct limine_memmap_entry* entry = bootloader.mm_entries[i];
debugf( debugf(
"\t0x%x - 0x%x (%d bytes), type %d\n", "\t%p - %p (%d bytes), type %d\n",
entry->base, entry->base + entry->length, entry->length, entry->type entry->base, entry->base + entry->length, entry->length, entry->type
); );
if (entry->type != LIMINE_MEMMAP_USABLE) continue; if (entry->type != LIMINE_MEMMAP_USABLE) continue;
// Limine spec says that it'll be aligned, but just as a sanity check
if (entry->base % PAGE_SIZE != 0) {
size_t diff = PAGE_SIZE - (entry->base % PAGE_SIZE);
debugf("\t Not 4 kiB aligned, shifting %p -> %p (%d bytes).\n", entry->base, entry->base + diff, diff);
entry->base += diff;
entry->length -= diff;
}
for (offset_t offset = 0; offset < entry->length; offset += PAGE_SIZE) { for (offset_t offset = 0; offset < entry->length; offset += PAGE_SIZE) {
uint64_t* addr = (uint64_t*)(entry->base + offset + bootloader.hhdm_offset); uint64_t* addr = TO_VIRT(entry->base) + offset;
*addr = (uint64_t)pmm_head; *addr = (uint64_t)pmm_head;
pmm_head = addr; pmm_head = addr;
total_mem += PAGE_SIZE;
} }
} }
debugf("[pmm] %d bytes of available memory.\n", total_mem);
return 0; return 0;
} }
void* pmm_alloc() { void* pmm_alloc() {
uint64_t* addr = pmm_head; uint64_t* addr = pmm_head;
pmm_head = (uint64_t*)(*addr); pmm_head = (uint64_t*)(*addr);
return addr - bootloader.hhdm_offset; return TO_PHYS(addr);
} }
void pmm_free(void* page) { void pmm_free(void* page) {
uint64_t* addr = (uint64_t*)(page - bootloader.hhdm_offset); uint64_t* addr = TO_VIRT(page);
*addr = (uint64_t)pmm_head; *addr = (uint64_t)pmm_head;
pmm_head = addr; pmm_head = addr;
} }