Kernel: Improve kmalloc error messages

This commit is contained in:
Bananymous 2022-12-10 01:08:01 +02:00
parent 3d3f12bd30
commit 862b32d735
1 changed files with 13 additions and 14 deletions

View File

@ -1,7 +1,7 @@
#include <kernel/multiboot.h> #include <kernel/multiboot.h>
#include <kernel/kmalloc.h> #include <kernel/kmalloc.h>
#include <kernel/panic.h> #include <kernel/panic.h>
#include <kernel/kprint.h> #include <kernel/Serial.h>
#include <stdint.h> #include <stdint.h>
@ -29,7 +29,7 @@ static size_t s_kmalloc_allocated = 0;
void kmalloc_initialize() void kmalloc_initialize()
{ {
if (!(s_multiboot_info->flags & (1 << 6))) if (!(s_multiboot_info->flags & (1 << 6)))
Kernel::panic("Bootloader didn't give a memory map"); Kernel::panic("Kmalloc: Bootloader didn't give a memory map");
// Validate kmalloc memory // Validate kmalloc memory
bool valid = false; bool valid = false;
@ -39,13 +39,9 @@ void kmalloc_initialize()
if (mmmt->type == 1) if (mmmt->type == 1)
{ {
uint8_t* ptr1 = (uint8_t*)mmmt->base_addr; if (mmmt->base_addr <= (uint64_t)s_kmalloc_base && (uint64_t)s_kmalloc_end <= mmmt->base_addr + mmmt->length)
uint8_t* ptr2 = (uint8_t*)s_kmalloc_base;
size_t len1 = mmmt->length;
size_t len2 = s_kmalloc_size;
if (ptr1 <= ptr2 && ptr1 + len1 >= ptr2 + len2)
{ {
dprintln("Total usable RAM: {} MB", (float)mmmt->length / MB);
valid = true; valid = true;
break; break;
} }
@ -55,7 +51,7 @@ void kmalloc_initialize()
} }
if (!valid) if (!valid)
Kernel::panic("Could not find enough space for kmalloc"); Kernel::panic("Kmalloc: Could not find 1 MB of memory");
s_kmalloc_node_count = 1; s_kmalloc_node_count = 1;
s_kmalloc_node_head = (kmalloc_node*)s_kmalloc_node_base; s_kmalloc_node_head = (kmalloc_node*)s_kmalloc_node_base;
@ -71,11 +67,14 @@ void kmalloc_initialize()
void kmalloc_dump_nodes() void kmalloc_dump_nodes()
{ {
dprintln("Kmalloc memory available {} MB", (float)s_kmalloc_available / MB);
dprintln("Kmalloc memory allocated {} MB", (float)s_kmalloc_allocated / MB);
dprintln("Using {}/{} nodes", s_kmalloc_node_count, s_kmalloc_max_nodes);
for (size_t i = 0; i < s_kmalloc_node_count; i++) for (size_t i = 0; i < s_kmalloc_node_count; i++)
{ {
kmalloc_node& node = s_kmalloc_node_head[i]; kmalloc_node& node = s_kmalloc_node_head[i];
if (i < 10) kprint(" "); if (i < 10) dprint(" ");
kprint(" ({}) {}, node at {}, free: {}, size: {}\n", i, (void*)&node, (void*)node.addr, node.free, node.size); dprintln(" ({}) {}, node at {}, free: {}, size: {}", i, (void*)&node, (void*)node.addr, node.free, node.size);
} }
} }
@ -95,7 +94,7 @@ void* kmalloc(size_t size)
if (valid_node_index == size_t(-1)) if (valid_node_index == size_t(-1))
{ {
kprint("Could not allocate {} bytes\n", size); dprintln("\e[33mKmalloc: Could not allocate {} bytes\e[0m", size);
return nullptr; return nullptr;
} }
@ -111,7 +110,7 @@ void* kmalloc(size_t size)
if (s_kmalloc_node_count == s_kmalloc_max_nodes) if (s_kmalloc_node_count == s_kmalloc_max_nodes)
{ {
kprint("Out of kmalloc nodes\n"); dprintln("\e[33mKmalloc: Out of kmalloc nodes\e[0m");
return nullptr; return nullptr;
} }
@ -152,7 +151,7 @@ void kfree(void* addr)
if (node_index == size_t(-1)) if (node_index == size_t(-1))
{ {
kprint("Attempting to free unallocated pointer {}\n", addr); dprintln("\e[33mKmalloc: Attempting to free unallocated pointer {}\e[0m", addr);
return; return;
} }