alloc_stack ok (HHDM mapped from mmap)

This commit is contained in:
2026-02-05 21:18:21 +01:00
parent 7bb542d901
commit 7f997f6611
9 changed files with 110 additions and 43 deletions

View File

@@ -16,12 +16,24 @@ static uintptr_t end;
// Kernel root table (level 4)
extern uint64_t *kernel_pml4;
static void kheap_map_page()
static void kheap_grow(size_t size)
{
size_t pages = ALIGN_UP(size + sizeof(struct heap_block_t), PAGE_SIZE) / PAGE_SIZE;
if (pages == 0) pages = 1;
for (size_t i = 0; i < pages; i++)
{
kheap_map_page();
}
}
void kheap_map_page()
{
uintptr_t phys = pmm_alloc();
paging_map_page(kernel_pml4, end, phys, PTE_PRESENT | PTE_WRITABLE | PTE_NOEXEC);
end += PAGE_SIZE;
DEBUG("Mapped first kheap page");
//DEBUG("Mapped first kheap page");
}
void kheap_init()
@@ -44,6 +56,7 @@ void* kmalloc(size_t size)
{
// No size, no memory allocated!
if (!size) return NULL;
size = ALIGN(size);
struct heap_block_t* curr = head;
@@ -53,17 +66,16 @@ void* kmalloc(size_t size)
if (curr->free && curr->size >= size)
{
// We split the block if it is big enough
if (curr->size > size + sizeof(struct heap_block_t))
if (curr->size >= size + BLOCK_MIN_SIZE)
{
struct heap_block_t* new_block = (struct heap_block_t*)((uintptr_t)curr + sizeof(struct heap_block_t) + size);
// We have to subtract the size of our block struct
new_block->size = curr->size - size - sizeof(struct heap_block_t);
new_block->free = true;
// Then we chain up the block in the list
new_block->next = curr->next;
curr->next = new_block;
//struct heap_block_t* new_block = (struct heap_block_t*)((uintptr_t)curr + sizeof(struct heap_block_t) + size);
struct heap_block_t* split = (struct heap_block_t*)((uintptr_t)curr + sizeof(*curr) + size);
split->size = curr->size - size - sizeof(*curr);
split->free = true;
split->next = curr->next;
curr->next = split;
curr->size = size;
}
@@ -75,14 +87,14 @@ void* kmalloc(size_t size)
curr = curr->next;
}
// If we're hear it means we didn't have enough memory
// If we're here it means we didn't have enough memory
// for the block allocation. So we will allocate more..
uintptr_t old_end = end;
kheap_map_page();
kheap_grow(size + sizeof(struct heap_block_t));
struct heap_block_t* block = (struct heap_block_t*)old_end;
block->size = PAGE_SIZE - sizeof(struct heap_block_t);
block->free = false;
block->size = ALIGN_UP(end - old_end - sizeof(struct heap_block_t), 16);
block->free = true;
block->next = NULL;
// Put the block at the end of the list
@@ -93,7 +105,7 @@ void* kmalloc(size_t size)
}
curr->next = block;
return (void*)((uintptr_t)block + sizeof(struct heap_block_t));
return kmalloc(size);
}
void kfree(void* ptr)
@@ -104,12 +116,25 @@ void kfree(void* ptr)
// Set it free!
struct heap_block_t* block = (struct heap_block_t*)((uintptr_t)ptr - sizeof(struct heap_block_t));
block->free = true;
// merge adjacent free blocks (coalescing)
struct heap_block_t* curr = head;
while (curr && curr->next)
{
if (curr->free && curr->next->free)
{
curr->size += sizeof(*curr) + curr->next->size;
curr->next = curr->next->next;
continue;
}
curr = curr->next;
}
}
// Should alloc enough for a stack (at least 64kb) to be used for a process.
// Should return a pointer to top of the stack (as stack grows DOWNWARDS)
void* kalloc_stack()
{
void* ptr = kmalloc(PROCESS_STACK_SIZE);
return ptr+PROCESS_STACK_SIZE;
uint8_t* ptr = kmalloc(PROCESS_STACK_SIZE);
return ptr ? ptr+PROCESS_STACK_SIZE : NULL;
}