From ca399ee782349ce099ed6ac6660860dac888a45c Mon Sep 17 00:00:00 2001 From: Mathieu Maret Date: Thu, 28 Oct 2021 18:33:02 +0200 Subject: [PATCH] Modify areaAdd api to be able to do more check --- core/allocArea.c | 28 +++++++++++++++------------- core/allocArea.h | 2 +- 2 files changed, 16 insertions(+), 14 deletions(-) diff --git a/core/allocArea.c b/core/allocArea.c index 73df03a..e18a0e5 100644 --- a/core/allocArea.c +++ b/core/allocArea.c @@ -18,27 +18,23 @@ void areaInit(vaddr_t firstMemUsed, vaddr_t lastUsed, vaddr_t stackBottom, vaddr vaddr_t areaAddr, descAddr, entryAddr; allocSetup(sizeof(struct memArea), &areaAddr, &descAddr, &entryAddr); - areaAdd(descAddr, 1, FALSE); + areaAdd(descAddr, descAddr + PAGE_SIZE, FALSE); if (entryAddr != descAddr) - areaAdd(entryAddr, 1, FALSE); + areaAdd(entryAddr, entryAddr + PAGE_SIZE, FALSE); if (areaAddr != descAddr && areaAddr != entryAddr) - areaAdd(areaAddr, 1, FALSE); + areaAdd(areaAddr, areaAddr + PAGE_SIZE, FALSE); //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), stackBottom, FALSE); //Initial kernel stack - int nbPagesStack = DIV_ROUND_UP((stackTop - stackBottom), PAGE_SIZE); - areaAdd(ALIGN_DOWN(stackBottom, PAGE_SIZE), nbPagesStack, FALSE); + areaAdd(stackBottom, stackTop, FALSE); // Rest of kernel code - int nbPagesKernelCode = DIV_ROUND_UP((lastUsed - stackTop), PAGE_SIZE); - areaAdd(ALIGN_DOWN(stackTop, PAGE_SIZE), nbPagesKernelCode, FALSE); + areaAdd(stackTop, lastUsed, 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(areaAddr + PAGE_SIZE, AREA_MEM_TOP, TRUE); // Create allocBank for the rest of the system allocPopulate(); @@ -122,16 +118,22 @@ vaddr_t areaAlloc(unsigned int nbPages, uint32_t flags) return allocated->startAddr; } -int areaAdd(vaddr_t addr, uint nbPages, int isFree) +int areaAdd(vaddr_t start, vaddr_t end, int isFree) { struct memArea **area; + int nbPages = (end-start)/PAGE_SIZE; + + assert(nbPages >0); + assert(IS_ALIGNED(start, PAGE_SIZE)); + assert(IS_ALIGNED(end, PAGE_SIZE)); + struct memArea *newArea = (struct memArea *)malloc(sizeof(struct memArea)); if (!newArea) return (vaddr_t)NULL; newArea->nbPages = nbPages; - newArea->startAddr = addr; + newArea->startAddr = start; if (isFree) { area = &freeArea; diff --git a/core/allocArea.h b/core/allocArea.h index afa8674..948e527 100644 --- a/core/allocArea.h +++ b/core/allocArea.h @@ -19,4 +19,4 @@ struct memArea { void areaInit(vaddr_t firstMemUsed, vaddr_t lastUsed, vaddr_t stack_bottom, vaddr_t stack_top); vaddr_t areaAlloc(unsigned int nbPages, uint32_t flags); int areaFree(vaddr_t addr); -int areaAdd(vaddr_t addr, uint nbPages, int isFree); +int areaAdd(vaddr_t begin, vaddr_t end, int isFree);