From 3df6011d7a262fbe85d03f3916e765e7cf69680b Mon Sep 17 00:00:00 2001 From: 0x35c <> Date: Fri, 28 Nov 2025 17:36:10 +0100 Subject: [PATCH] wip: user allocator --- headers/memory.h | 7 +- src/memory/kern/allocator.c | 3 +- src/memory/kern/page.c | 7 +- src/memory/memory.c | 2 +- src/memory/virt/allocator.c | 3 +- src/memory/virt/page.c | 136 ++++++++++++++++-------------------- src/multitasking/thread.c | 3 +- 7 files changed, 79 insertions(+), 82 deletions(-) diff --git a/headers/memory.h b/headers/memory.h index 6942c8e..19ca3b7 100644 --- a/headers/memory.h +++ b/headers/memory.h @@ -21,11 +21,16 @@ #define KERNEL_END ((uint32_t)&_kernel_end - VIRT_OFFSET) #define KERNEL_PT_END 1020 #define KERNEL_PT_START 769 +#define USER_PT_START 1 +#define USER_PT_END 768 #define PDE_VBE 1021 #define PDE_FRAME_ZONES 1022 #define PDE_MULTIBOOT 1020 +#define GET_PTE(pd_index, pt_index) \ + ((uint32_t *)(VIRT_PT_BASE + pd_index * 1024 + pt_index)) + #define PTE2VA(pd_index, pt_index) \ ((uint32_t *)((((uint32_t)pd_index * 1024) + (uint32_t)pt_index) * \ 4096)) @@ -69,6 +74,6 @@ void init_memory(void); void *alloc_frame(void); int free_frame(void *frame_ptr); void *kalloc_pages(size_t nb_pages); -void *valloc_pages(size_t size, void **phys_addr); +void *valloc_pages(size_t nb_pages); int kfree_pages(void *page_ptr, size_t size); int vfree_pages(void *page_ptr, size_t size); diff --git a/src/memory/kern/allocator.c b/src/memory/kern/allocator.c index c58448d..d582dcc 100644 --- a/src/memory/kern/allocator.c +++ b/src/memory/kern/allocator.c @@ -3,6 +3,7 @@ #include "kprintf.h" #include "memory.h" #include "string.h" +#include "utils.h" Zone *kzones[3]; @@ -49,7 +50,7 @@ static void new_block(Zone *zone, uint32_t zone_size) int new_kzone(block_type_t type, uint32_t size) { // assert(current_task->pid); - void *heap = kalloc_pages(size); + void *heap = kalloc_pages(CEIL(size, PAGE_SIZE)); if (heap == NULL) { kprintf(KERN_ERR "error: alloc_frame failed\n"); return (-1); diff --git a/src/memory/kern/page.c b/src/memory/kern/page.c index b7ec6c2..719afb1 100644 --- a/src/memory/kern/page.c +++ b/src/memory/kern/page.c @@ -12,7 +12,8 @@ static uint32_t *find_next_block(size_t nb_pages) { uint32_t count = 0; - for (uint32_t *pte = PTE2VA(1023, 0); pte < PTE2VA(1023, 1023); pte++) { + for (uint32_t *pte = PTE2VA(1023, KERNEL_PT_START); + pte < PTE2VA(1023, KERNEL_PT_END); pte++) { if (!*pte) { count = 0; continue; @@ -27,6 +28,8 @@ static uint32_t *find_next_block(size_t nb_pages) void *kalloc_pages(size_t nb_pages) { uint32_t *start = find_next_block(nb_pages); + if (!start) + return NULL; for (uint32_t i = 0; i < nb_pages; i++) { void *frame = alloc_frame(); if (!frame) { @@ -35,7 +38,7 @@ void *kalloc_pages(size_t nb_pages) PAGE_MASK)); return NULL; } - assert(!((uint32_t)frame & PAGE_MASK)); + assert((uint32_t)frame & PAGE_MASK); start[i] = (uint32_t)frame | INIT_FLAGS; } uint32_t page_index = start - PTE2VA(1023, 0); diff --git a/src/memory/memory.c b/src/memory/memory.c index 55ef6ea..4963c9e 100644 --- a/src/memory/memory.c +++ b/src/memory/memory.c @@ -123,7 +123,7 @@ void init_memory() uint32_t frame = (uint32_t)alloc_frame(); if (!frame) kpanic("Couldn't initialize kernel PTs\n"); - PD[i] = frame | RW; + PD[i] = frame | INIT_FLAGS; } // kalash kalash kalash sur la mélodie chez nous pas de félonie ça vient // de Sevran les R diff --git a/src/memory/virt/allocator.c b/src/memory/virt/allocator.c index 13f7652..18adbfe 100644 --- a/src/memory/virt/allocator.c +++ b/src/memory/virt/allocator.c @@ -4,6 +4,7 @@ #include "kprintf.h" #include "memory.h" #include "string.h" +#include "utils.h" Zone *vzones[3]; @@ -50,7 +51,7 @@ static void new_block(Zone *zone, uint32_t zone_size) int new_vzone(block_type_t type, uint32_t size) { // assert(current_task->pid); - void *heap = valloc_pages(size, NULL); + void *heap = valloc_pages(CEIL(size, PAGE_SIZE)); if (heap == NULL) { kprintf(KERN_ERR "error: alloc_pages failed\n"); return (-1); diff --git a/src/memory/virt/page.c b/src/memory/virt/page.c index a0d17cf..c4679a3 100644 --- a/src/memory/virt/page.c +++ b/src/memory/virt/page.c @@ -9,82 +9,68 @@ #include "string.h" #include "utils.h" -static int16_t find_next_block(size_t nb_pages, uint16_t *pd_index_ptr, - uint32_t **page_table_ptr) +static int8_t alloc_pagetable(uint16_t pd_index) { - for (*pd_index_ptr = 2; *pd_index_ptr < 768; (*pd_index_ptr)++) { - // if (current_pd[(*pd_index_ptr)] == 0x02) { - // if (add_page_table(*pd_index_ptr, 1) < 0) - // return -2; - // } - *page_table_ptr = (uint32_t *)PTE2VA(1, *pd_index_ptr); - for (uint16_t i = 0; i + nb_pages - 1 < 1024; i++) { - uint16_t j; - for (j = 0; (*page_table_ptr)[i + j] >> 12 == i + j && - j < nb_pages; - j++) - ; - if (j == nb_pages) - return i; - i += j; - } - } - return -1; -} - -void *valloc_pages(size_t size, void **phys_addr) -{ - const uint32_t nb_pages = CEIL(size, PAGE_SIZE); - uint16_t pd_index; - uint32_t *page_table; - const int16_t index = find_next_block(nb_pages, &pd_index, &page_table); - - if (index < 0) { - kprintf(KERN_CRIT "%d: Not enough pages (max: %d)\n", index, - 1024); - return NULL; - } - for (size_t i = index; i - (size_t)index < nb_pages; i++) { - void *frame = alloc_frame(); - if (!frame) { - for (size_t j = index; j < i; j++) - free_frame((void *)(page_table[j] & PAGE_MASK)); - return NULL; - } - if (phys_addr) - *phys_addr = frame; - page_table[i] = ((uint32_t)frame & PAGE_MASK) | INIT_FLAGS; - } - return (void *)PTE2VA(pd_index, index); -} - -int vfree_pages(void *page_ptr, size_t size) -{ - const uint32_t page_addr = (uint32_t)page_ptr; - const uint32_t nb_pages = CEIL(size, PAGE_SIZE); - const uint32_t page_index = page_addr / PAGE_SIZE; - const uint32_t pd_index = page_index / 1024; - const uint32_t pt_index = page_index % 1024; - - if ((uint32_t)pd_index > 0x300) { - kprintf(KERN_WARNING "Address out of range\n"); + uint32_t *pt = alloc_frame(); + if (!pt) return -1; - } else if (page_addr % PAGE_SIZE) { - kprintf(KERN_WARNING "Invalid address\n"); - return -1; - } else if (pt_index + nb_pages > 1024) { - kprintf(KERN_WARNING "Invalid number of frames\n"); - return -1; - } - uint32_t *page_table = (uint32_t *)PTE2VA(1, pd_index); - for (uint16_t i = pt_index; i < pt_index + nb_pages; i++) { - if (page_table[i] >> 12 == i) { - kprintf(KERN_WARNING "Page already freed\n"); - return -2; - } - free_frame((void *)(page_table[i] & PAGE_MASK)); - page_table[i] = i << 12; - } - + PD[pd_index] = (uint32_t)pt | INIT_FLAGS; + return 0; +} + +static uint32_t *find_next_block(size_t nb_pages) +{ + size_t count = 0; + for (size_t i = USER_PT_START; i < USER_PT_END; i++) { + if (!PD[i]) + if (alloc_pagetable(i) < 0) + return NULL; + for (size_t j = 0; j < 1024; j++) { + if (!*GET_PTE(i, j)) { + count = 0; + continue; + } + count++; + if (count == nb_pages) { + return GET_PTE(i, j) - count; + } + } + } + return NULL; +} + +void *valloc_pages(size_t nb_pages) +{ + uint32_t *start = find_next_block(nb_pages); + if (!start) + return NULL; + for (uint32_t i = 0; i < nb_pages; i++) { + void *frame = alloc_frame(); + if (!frame) { + for (uint32_t j = 0; j < i; j++) + free_frame((void *)(((uint32_t)(start + j)) & + PAGE_MASK)); + return NULL; + } + assert((uint32_t)frame & PAGE_MASK); + start[i] = (uint32_t)frame | INIT_FLAGS; + } + uint32_t page_index = start - PTE2VA(1023, 0); + return PTE2VA(page_index / 1024, page_index % 1024); +} + +int vfree_pages(void *page_ptr, size_t nb_pages) +{ + const uint32_t page_addr = (uint32_t)page_ptr; + if (page_addr % PAGE_SIZE) { + kprintf(KERN_WARNING "Invalid address\n"); + return -1; + } + + for (uint32_t *pte = VA2PTE(page_addr); + pte < VA2PTE(page_addr) + nb_pages; pte++) { + free_frame((void *)(*pte & PAGE_MASK)); + *pte = 0; + } return 0; } diff --git a/src/multitasking/thread.c b/src/multitasking/thread.c index 8a1abe9..123ebc9 100644 --- a/src/multitasking/thread.c +++ b/src/multitasking/thread.c @@ -7,6 +7,7 @@ #include "process.h" #include "string.h" #include "thread.h" +#include "utils.h" struct tcb *create_thread(struct pcb *process, void (*entry)(void)) { @@ -15,7 +16,7 @@ struct tcb *create_thread(struct pcb *process, void (*entry)(void)) return NULL; new_tcb->tid = process->tid++; - new_tcb->esp0 = valloc_pages(STACK_SIZE, NULL); + new_tcb->esp0 = valloc_pages(CEIL(STACK_SIZE, PAGE_SIZE)); if (!new_tcb->esp0) { vfree(new_tcb); return NULL;