From 0e52a0fa94bae866ffc2647cb30be4abb67e7370 Mon Sep 17 00:00:00 2001 From: Ox03bb Date: Sun, 8 Nov 2026 11:49:58 +0100 Subject: [PATCH] feat(heap, vmm): enhance memory management with range allocation and update heap functions --- Makefile | 4 ++-- includes/mm/heap.h | 8 +++++-- includes/mm/vmm.h | 1 + src/arch/x86/mm/heap/heap.c | 33 ++++++++++++++-------------- src/arch/x86/mm/virtual/vmm.c | 41 +++++++++++++++++++++++++++++++++++ src/kernel/kernel.c | 36 +++++------------------------- 6 files changed, 72 insertions(+), 51 deletions(-) diff --git a/Makefile b/Makefile index 77e9346..8c800ae 100644 --- a/Makefile +++ b/Makefile @@ -75,12 +75,12 @@ $(OS_BIN): $(BOOT_ASM) $(KERNEL_ASM_SRCS) $(KERNEL_C_SRCS) $(LINKER_SCRIPT) i686-elf-gcc $(CFLAGS) \ -T linker.ld \ -o build/kernel.elf \ - build/kernel-linked.o && \ + build/kernel-linked.o ; \ \ echo "== Creating raw kernel binary =="; \ i686-elf-objcopy -O binary \ build/kernel.elf \ - bin/kernel.bin && \ + bin/kernel.bin ; \ \ KERNEL_SIZE=$$(stat -c%s bin/kernel.bin); \ KERNEL_SECTORS=$$(( (KERNEL_SIZE + 511) / 512 )); \ diff --git a/includes/mm/heap.h b/includes/mm/heap.h index f5aa262..aba58d9 100644 --- a/includes/mm/heap.h +++ b/includes/mm/heap.h @@ -24,12 +24,16 @@ typedef struct { heap_block_t *head; } heap_t; +extern heap_t *current_heap; + int8_t init_heap(pde_t *directory, heap_t *heap); void *gmalloc(heap_t *heap, size_t size); int8_t gfree(heap_t *heap, void *ptr); -// void *kmalloc(size_t size); -// void kfree(void *ptr); +void *malloc(size_t size); +int8_t free(void *ptr); + +bool is_allocated(const void *ptr); #endif \ No newline at end of file diff --git a/includes/mm/vmm.h b/includes/mm/vmm.h index 043b315..33a7b2b 100644 --- a/includes/mm/vmm.h +++ b/includes/mm/vmm.h @@ -77,6 +77,7 @@ pte_t *vmm_create_page_table(); int8_t vmm_map_page(pde_t *directory, uint32_t virtual_addr, uint32_t physical_addr, uint32_t flags); int8_t vmm_allocate(uint32_t virtual_addr); +int8_t vmm_allocate_range(uint32_t start, uint32_t end); int8_t vmm_unmap_page(pde_t *directory, uint32_t virtual_addr); diff --git a/src/arch/x86/mm/heap/heap.c b/src/arch/x86/mm/heap/heap.c index bb1ad72..b0a9417 100644 --- a/src/arch/x86/mm/heap/heap.c +++ b/src/arch/x86/mm/heap/heap.c @@ -6,12 +6,10 @@ #include "panic.h" -#include "stdio.h" +heap_t *current_heap; int8_t init_heap(pde_t *directory, heap_t *heap) { - printf("cc\n"); if (directory == NULL) { - print("null"); return -1; } @@ -21,19 +19,8 @@ int8_t init_heap(pde_t *directory, heap_t *heap) { if (end > K_HEAP_MAX) return -1; - printf("cc\n"); - - print("\ninit heap\n"); - // Map the initial heap pages. - for (uintptr_t addr = start; addr < end; addr += PAGE_SIZE) { - print("a "); - if (vmm_allocate(addr) != 0) { - print("\n not a"); - return -1; - } - } - - printf("ff\n"); + if (vmm_allocate_range((uint32_t)start, (uint32_t)end) != 0) + return -1; // Initialize heap metadata. heap->start = start; @@ -115,4 +102,16 @@ int8_t gfree(heap_t *heap, void *ptr) { block->is_free = true; return 0; -} \ No newline at end of file +} + +void *malloc(size_t size) { return gmalloc(current_heap, size); } + +int8_t free(void *ptr) { return gfree(current_heap, ptr); } + +bool is_allocated(const void *ptr) { + if (ptr == NULL) + return false; + + const heap_block_t *block = (const heap_block_t *)ptr - 1; + return !block->is_free; +} diff --git a/src/arch/x86/mm/virtual/vmm.c b/src/arch/x86/mm/virtual/vmm.c index 6495fd2..3341f16 100644 --- a/src/arch/x86/mm/virtual/vmm.c +++ b/src/arch/x86/mm/virtual/vmm.c @@ -165,6 +165,47 @@ int8_t vmm_allocate(uint32_t virtual_addr) { return 0; } +int8_t vmm_allocate_range(uint32_t start, uint32_t end) { + if (current_directory == NULL || start >= end || (start & (PAGE_SIZE - 1)) != 0 || + (end & (PAGE_SIZE - 1)) != 0) + return -1; + + uint32_t page_count = (end - start) / PAGE_SIZE; + + /* + * Check the complete range before allocating frames so a failed request + * cannot leave a partially mapped virtual range. + */ + for (uint32_t i = 0; i < page_count; i++) { + vaddr_t va; + va.raw = start + (i * PAGE_SIZE); + + if (vmm_is_mapped(current_directory, va)) + return -1; + } + + uintptr_t physical_start = pmm_alloc_n_frame((int)page_count); + + if (physical_start == 0) + return -1; + + for (uint32_t i = 0; i < page_count; i++) { + uint32_t virtual_addr = start + (i * PAGE_SIZE); + uint32_t physical_addr = (uint32_t)(physical_start + (i * PAGE_SIZE)); + + if (vmm_map_page(current_directory, virtual_addr, physical_addr, + VMM_PRESENT | VMM_WRITABLE) != 0) { + for (uint32_t rollback = 0; rollback < i; rollback++) + vmm_unmap_page(current_directory, start + (rollback * PAGE_SIZE)); + + pmm_free_n_frame(physical_start, (int)page_count); + return -1; + } + } + + return 0; +} + int8_t vmm_unmap_page(pde_t *directory, uint32_t virtual_addr) { if (directory == NULL) return -1; diff --git a/src/kernel/kernel.c b/src/kernel/kernel.c index ea9521e..3390b1e 100644 --- a/src/kernel/kernel.c +++ b/src/kernel/kernel.c @@ -69,42 +69,18 @@ void kernel_main(boot_info_t *boot_info) { pmm_reserve((uintptr_t)&kernel_start_addr, (uintptr_t)&kernel_end_addr); KERNEL_INIT_P("Reserved kernel memory"); - // uintptr_t ptr = pmm_alloc_n_frame(100); - // printf("%x\n", ptr); - - // bool c = pmm_check_mm(ptr); - - // printf("%b\n", c); - - // tty_t tty0; - // tty_init(&tty0); - KERNEL_INIT("Virtual Memory & Paging ", vmm_init); - vmm_allocate(0x3e000); - // vmm_map_page(current_directory,0x3e000,0x3e000,VMM_PRESENT | VMM_WRITABLE); - - static volatile int *a = (volatile int *)0x3e000; - - *a = 5; - int b = *a; - printf("a %d", b); - - // int8_t err = init_heap(current_directory, &k_heap); - // if (err == -1) - // panic("heap init error"); - - // KERNEL_INIT_P("Kernal Heap"); - - // void *b = gmalloc(&k_heap, sizeof(int) * 5); - // b = 5; + current_heap = &k_heap; + init_heap(current_directory, current_heap); - // static volatile int *a = (volatile int *)0xfffffff; + KERNEL_INIT_P("Kernal Heap"); - // *a = 4; + tty_t tty0; + tty_init(&tty0); while (1) { - // tty_process_events(&tty0); + tty_process_events(&tty0); } print("[\033[02:00mready\033[0m] Kernel initialized successfully!\n");