Kernel: kmalloc now aligns everything by alignof(max_align_t)

kmalloc properly aligns everything now.

I also changed so that kmalloc addresses are stored as uintptr_t
instead of void* or uint8_t* for easier arithemetic and less casting
This commit is contained in:
Bananymous 2022-12-27 19:02:34 +02:00
parent 9169020ad2
commit 98fc72b0d0
1 changed files with 37 additions and 21 deletions

View File

@ -7,36 +7,38 @@
#define MB (1 << 20) #define MB (1 << 20)
#define ALIGN (alignof(max_align_t))
/* /*
#### KMALLOC ################ #### KMALLOC ################
*/ */
struct kmalloc_node struct kmalloc_node
{ {
uint8_t* addr = nullptr; uintptr_t addr;
size_t size : sizeof(size_t) * 8 - 1; size_t size : sizeof(size_t) * 8 - 1;
size_t free : 1; size_t free : 1;
}; };
static kmalloc_node* s_kmalloc_node_head = nullptr; static kmalloc_node* s_kmalloc_node_head = nullptr;
static size_t s_kmalloc_node_count; static size_t s_kmalloc_node_count;
static uint8_t* const s_kmalloc_node_base = (uint8_t*)0x00200000; static constexpr uintptr_t s_kmalloc_node_base = 0x00200000;
static constexpr size_t s_kmalloc_max_nodes = 1000; static constexpr size_t s_kmalloc_max_nodes = 1000;
static uint8_t* const s_kmalloc_base = s_kmalloc_node_base + s_kmalloc_max_nodes * sizeof(kmalloc_node); static constexpr uintptr_t s_kmalloc_base = s_kmalloc_node_base + s_kmalloc_max_nodes * sizeof(kmalloc_node);
static constexpr size_t s_kmalloc_size = 1 * MB; static constexpr size_t s_kmalloc_size = 1 * MB;
static uint8_t* const s_kmalloc_end = s_kmalloc_base + s_kmalloc_size; static constexpr uintptr_t s_kmalloc_end = s_kmalloc_base + s_kmalloc_size;
static size_t s_kmalloc_available = 0;
static size_t s_kmalloc_allocated = 0;
static size_t s_kmalloc_available = 0;
static size_t s_kmalloc_allocated = 0;
/* /*
#### KMALLOC ETERNAL ######## #### KMALLOC ETERNAL ########
*/ */
static uint8_t* s_kmalloc_eternal_ptr = nullptr; static uintptr_t s_kmalloc_eternal_ptr = 0;
static uint8_t* const s_kmalloc_eternal_base = s_kmalloc_end; static constexpr uintptr_t s_kmalloc_eternal_base = s_kmalloc_end;
static constexpr size_t s_kmalloc_eternal_size = 2 * MB; static constexpr size_t s_kmalloc_eternal_size = 2 * MB;
static uint8_t* const s_kmalloc_eternal_end = s_kmalloc_eternal_base + s_kmalloc_eternal_size; static constexpr uintptr_t s_kmalloc_eternal_end = s_kmalloc_eternal_base + s_kmalloc_eternal_size;
/* /*
############################# #############################
*/ */
@ -54,7 +56,7 @@ void kmalloc_initialize()
if (mmmt->type == 1) if (mmmt->type == 1)
{ {
if (mmmt->base_addr <= (uint64_t)s_kmalloc_base && (uint64_t)s_kmalloc_eternal_end <= mmmt->base_addr + mmmt->length) if (mmmt->base_addr <= s_kmalloc_base && s_kmalloc_eternal_end <= mmmt->base_addr + mmmt->length)
{ {
dprintln("Total usable RAM: {} MB", (float)mmmt->length / MB); dprintln("Total usable RAM: {} MB", (float)mmmt->length / MB);
valid = true; valid = true;
@ -68,6 +70,8 @@ void kmalloc_initialize()
if (!valid) if (!valid)
Kernel::panic("Kmalloc: Could not find {} MB of memory", (double)(s_kmalloc_eternal_end - s_kmalloc_base)); Kernel::panic("Kmalloc: Could not find {} MB of memory", (double)(s_kmalloc_eternal_end - s_kmalloc_base));
dprintln("Aligining everything to {} byte boundaries", ALIGN);
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;
@ -96,6 +100,12 @@ void kmalloc_dump_nodes()
void* kmalloc_eternal(size_t size) void* kmalloc_eternal(size_t size)
{ {
if (size % ALIGN)
size += ALIGN - (size % ALIGN);
if (s_kmalloc_eternal_ptr % ALIGN)
Kernel::panic("Unaligned ptr in kmalloc_eternal");
if (s_kmalloc_eternal_ptr + size > s_kmalloc_eternal_end) if (s_kmalloc_eternal_ptr + size > s_kmalloc_eternal_end)
{ {
dprintln("\e[33mKmalloc eternal: Could not allocate {} bytes\e[0m", size); dprintln("\e[33mKmalloc eternal: Could not allocate {} bytes\e[0m", size);
@ -109,6 +119,9 @@ void* kmalloc_eternal(size_t size)
void* kmalloc(size_t size) void* kmalloc(size_t size)
{ {
if (size % ALIGN)
size += ALIGN - (size % ALIGN);
// Search for node with free memory and big enough size // Search for node with free memory and big enough size
size_t valid_node_index = -1; size_t valid_node_index = -1;
for (size_t i = 0; i < s_kmalloc_node_count; i++) for (size_t i = 0; i < s_kmalloc_node_count; i++)
@ -134,7 +147,9 @@ void* kmalloc(size_t size)
if (valid_node.size == size) if (valid_node.size == size)
{ {
valid_node.free = false; valid_node.free = false;
return valid_node.addr; if (valid_node.addr % ALIGN)
Kernel::panic("Unaligned ptr in kmalloc");
return (void*)valid_node.addr;
} }
if (s_kmalloc_node_count == s_kmalloc_max_nodes) if (s_kmalloc_node_count == s_kmalloc_max_nodes)
@ -161,7 +176,9 @@ void* kmalloc(size_t size)
s_kmalloc_allocated += size; s_kmalloc_allocated += size;
s_kmalloc_available -= size; s_kmalloc_available -= size;
return valid_node.addr; if (valid_node.addr % ALIGN)
Kernel::panic("Unaligned ptr in kmalloc");
return (void*)valid_node.addr;
} }
void kfree(void* addr) void kfree(void* addr)
@ -174,7 +191,7 @@ void kfree(void* addr)
size_t node_index = -1; size_t node_index = -1;
for (size_t i = 0; i < s_kmalloc_node_count; i++) for (size_t i = 0; i < s_kmalloc_node_count; i++)
{ {
if (s_kmalloc_node_head[i].addr == addr) if (s_kmalloc_node_head[i].addr == (uintptr_t)addr)
{ {
node_index = i; node_index = i;
break; break;
@ -187,7 +204,6 @@ void kfree(void* addr)
return; return;
} }
// Mark this node as free // Mark this node as free
kmalloc_node* node = &s_kmalloc_node_head[node_index]; kmalloc_node* node = &s_kmalloc_node_head[node_index];
node->free = true; node->free = true;