From be080af906d104a6fd0a40038d465491775a3393 Mon Sep 17 00:00:00 2001 From: Mathieu Maret Date: Thu, 28 Oct 2021 18:19:35 +0200 Subject: [PATCH] Fix initial area setup The inital kernel stack was not page aligned and thus was found in 2 area --- arch/x86/boot/boot.asm | 2 +- core/allocArea.c | 30 +++++++++++++++++++----------- 2 files changed, 20 insertions(+), 12 deletions(-) diff --git a/arch/x86/boot/boot.asm b/arch/x86/boot/boot.asm index 5f9b676..7588d78 100644 --- a/arch/x86/boot/boot.asm +++ b/arch/x86/boot/boot.asm @@ -28,7 +28,7 @@ align 4 ; stack is properly aligned and failure to align the stack will result in ; undefined behavior. section .bss -align 16 +align 4096 stack_bottom: resb 16384 ; 16 KiB stack_top: diff --git a/core/allocArea.c b/core/allocArea.c index 4c5bb20..73df03a 100644 --- a/core/allocArea.c +++ b/core/allocArea.c @@ -6,14 +6,11 @@ #include "mem.h" #include "stdarg.h" - static struct memArea *freeArea; static struct memArea *usedArea; static int areaMergeFreeArea(struct memArea *prev, struct memArea *next); - -//Kernel stack is inside firstMemUsed-lastUsed void areaInit(vaddr_t firstMemUsed, vaddr_t lastUsed, vaddr_t stackBottom, vaddr_t stackTop) { list_init(freeArea); @@ -27,14 +24,23 @@ void areaInit(vaddr_t firstMemUsed, vaddr_t lastUsed, vaddr_t stackBottom, vaddr if (areaAddr != descAddr && areaAddr != entryAddr) areaAdd(areaAddr, 1, FALSE); - int nbPagesKernel = DIV_ROUND_UP((lastUsed - firstMemUsed), PAGE_SIZE); + //kernel bootstrap part + int nbPagesKernel = DIV_ROUND_UP((stackBottom - firstMemUsed), PAGE_SIZE); areaAdd(ALIGN_DOWN(firstMemUsed, PAGE_SIZE), nbPagesKernel, FALSE); - if(stackBottom > lastUsed){ - int nbPagesStack = DIV_ROUND_UP((stackTop - stackBottom), PAGE_SIZE); - areaAdd(ALIGN_DOWN(stackBottom, PAGE_SIZE), nbPagesStack, FALSE); - } + + //Initial kernel stack + int nbPagesStack = DIV_ROUND_UP((stackTop - stackBottom), PAGE_SIZE); + areaAdd(ALIGN_DOWN(stackBottom, PAGE_SIZE), nbPagesStack, FALSE); + + // Rest of kernel code + int nbPagesKernelCode = DIV_ROUND_UP((lastUsed - stackTop), PAGE_SIZE); + areaAdd(ALIGN_DOWN(stackTop, PAGE_SIZE), nbPagesKernelCode, FALSE); + + // Rest of virtual mem is free int nbFreePages = DIV_ROUND_UP((AREA_MEM_TOP - areaAddr), PAGE_SIZE); - areaAdd(ALIGN_DOWN(areaAddr + PAGE_SIZE, PAGE_SIZE), nbFreePages , TRUE); + areaAdd(ALIGN_DOWN(areaAddr + PAGE_SIZE, PAGE_SIZE), nbFreePages, TRUE); + + // Create allocBank for the rest of the system allocPopulate(); } @@ -183,9 +189,11 @@ int areaFree(vaddr_t addr) pr_info("Cannot find memArea associated to %p\n", addr); return -1; } - for(uint i = 0; i < area->nbPages; i++){ - pageUnmap( area->startAddr + i * PAGE_SIZE); + + for (uint i = 0; i < area->nbPages; i++) { + pageUnmap(area->startAddr + i * PAGE_SIZE); } + list_delete(usedArea, area); insertSorted(&freeArea, area);