Fix initial area setup

The inital kernel stack was not page aligned and thus was found in 2
area
This commit is contained in:
Mathieu Maret 2021-10-28 18:19:35 +02:00
parent e658ea6e48
commit be080af906
2 changed files with 20 additions and 12 deletions

View File

@ -28,7 +28,7 @@ align 4
; stack is properly aligned and failure to align the stack will result in ; stack is properly aligned and failure to align the stack will result in
; undefined behavior. ; undefined behavior.
section .bss section .bss
align 16 align 4096
stack_bottom: stack_bottom:
resb 16384 ; 16 KiB resb 16384 ; 16 KiB
stack_top: stack_top:

View File

@ -6,14 +6,11 @@
#include "mem.h" #include "mem.h"
#include "stdarg.h" #include "stdarg.h"
static struct memArea *freeArea; static struct memArea *freeArea;
static struct memArea *usedArea; static struct memArea *usedArea;
static int areaMergeFreeArea(struct memArea *prev, struct memArea *next); 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) void areaInit(vaddr_t firstMemUsed, vaddr_t lastUsed, vaddr_t stackBottom, vaddr_t stackTop)
{ {
list_init(freeArea); list_init(freeArea);
@ -27,14 +24,23 @@ void areaInit(vaddr_t firstMemUsed, vaddr_t lastUsed, vaddr_t stackBottom, vaddr
if (areaAddr != descAddr && areaAddr != entryAddr) if (areaAddr != descAddr && areaAddr != entryAddr)
areaAdd(areaAddr, 1, FALSE); 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); areaAdd(ALIGN_DOWN(firstMemUsed, PAGE_SIZE), nbPagesKernel, FALSE);
if(stackBottom > lastUsed){
//Initial kernel stack
int nbPagesStack = DIV_ROUND_UP((stackTop - stackBottom), PAGE_SIZE); int nbPagesStack = DIV_ROUND_UP((stackTop - stackBottom), PAGE_SIZE);
areaAdd(ALIGN_DOWN(stackBottom, PAGE_SIZE), nbPagesStack, FALSE); 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); 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(); allocPopulate();
} }
@ -183,9 +189,11 @@ int areaFree(vaddr_t addr)
pr_info("Cannot find memArea associated to %p\n", addr); pr_info("Cannot find memArea associated to %p\n", addr);
return -1; return -1;
} }
for (uint i = 0; i < area->nbPages; i++) { for (uint i = 0; i < area->nbPages; i++) {
pageUnmap(area->startAddr + i * PAGE_SIZE); pageUnmap(area->startAddr + i * PAGE_SIZE);
} }
list_delete(usedArea, area); list_delete(usedArea, area);
insertSorted(&freeArea, area); insertSorted(&freeArea, area);