mirror of
https://github.com/Tom-on64/kern.git
synced 2026-11-03 18:22:22 +00:00
Fixed #GP from pmm_free()
This commit is contained in:
+17
-4
@@ -9,35 +9,48 @@
|
||||
uint64_t* pmm_head = NULL;
|
||||
|
||||
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
|
||||
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",
|
||||
"\t%p - %p (%d bytes), type %d\n",
|
||||
entry->base, entry->base + entry->length, entry->length, entry->type
|
||||
);
|
||||
|
||||
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) {
|
||||
uint64_t* addr = (uint64_t*)(entry->base + offset + bootloader.hhdm_offset);
|
||||
uint64_t* addr = TO_VIRT(entry->base) + offset;
|
||||
*addr = (uint64_t)pmm_head;
|
||||
pmm_head = addr;
|
||||
total_mem += PAGE_SIZE;
|
||||
}
|
||||
}
|
||||
|
||||
debugf("[pmm] %d bytes of available memory.\n", total_mem);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void* pmm_alloc() {
|
||||
uint64_t* addr = pmm_head;
|
||||
pmm_head = (uint64_t*)(*addr);
|
||||
return addr - bootloader.hhdm_offset;
|
||||
return TO_PHYS(addr);
|
||||
}
|
||||
|
||||
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;
|
||||
pmm_head = addr;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user