diff --git a/arch/x86/paging.h b/arch/x86/paging.h index 11490de..d43e0a3 100644 --- a/arch/x86/paging.h +++ b/arch/x86/paging.h @@ -1,7 +1,7 @@ #pragma once #include "types.h" -#define PAGING_MEM_USER 1 +#define PAGING_MEM_USER (1U << 0) #define PAGING_MEM_READ (1U << 1) #define PAGING_MEM_WRITE (1U << 2) diff --git a/core/alloc.c b/core/alloc.c index 491ac24..e9058c5 100644 --- a/core/alloc.c +++ b/core/alloc.c @@ -127,7 +127,7 @@ static int allocSlab(struct slabDesc **desc, size_t size, size_t sizeSlab, int s nbPage = DIV_ROUND_UP(sizeSlab, PAGE_SIZE); if (allocInitialized) { - alloc = areaAlloc(nbPage); + alloc = areaAlloc(nbPage, AREA_PHY_MAP); if (alloc == (paddr_t)NULL) return -ENOMEM; } else { @@ -182,7 +182,7 @@ static int allocSlabEntry(struct slabEntry **desc, size_t size, size_t sizeSlab, } nbPage = DIV_ROUND_UP(sizeSlab, PAGE_SIZE); - vaddr_t alloc = areaAlloc(nbPage); + vaddr_t alloc = areaAlloc(nbPage, AREA_PHY_MAP); if (alloc == (paddr_t)NULL) return -ENOMEM; @@ -262,7 +262,7 @@ void *malloc(size_t size) disable_IRQs(flags); if (size >= PAGE_SIZE){ - vaddr_t area = areaAlloc(DIV_ROUND_UP(size, PAGE_SIZE)); + vaddr_t area = areaAlloc(DIV_ROUND_UP(size, PAGE_SIZE), AREA_PHY_MAP); return (void *)area; } diff --git a/core/allocArea.c b/core/allocArea.c index 4e47264..6294df7 100644 --- a/core/allocArea.c +++ b/core/allocArea.c @@ -63,7 +63,7 @@ static void insertSorted(struct memArea **list, struct memArea *item) list_add_tail(*list, item); } -vaddr_t areaAlloc(unsigned int nbPages) +vaddr_t areaAlloc(unsigned int nbPages, uint32_t flags) { struct memArea *area, *allocated; @@ -75,33 +75,35 @@ vaddr_t areaAlloc(unsigned int nbPages) list_delete(freeArea, area); insertSorted(&usedArea, area); allocated = area; + } else { + struct memArea *newArea = (struct memArea *)malloc(sizeof(struct memArea)); + if (!newArea) { + pr_devel("Failed to allocated area of %d pages\n", nbPages); + return (vaddr_t)NULL; + } + + // Avoid insertSorted(freeArea, newArea) call by modifying area + + newArea->nbPages = nbPages; + newArea->startAddr = area->startAddr; + + area->nbPages -= nbPages; + area->startAddr += nbPages * PAGE_SIZE; + + insertSorted(&usedArea, newArea); + + allocated = newArea; } - struct memArea *newArea = (struct memArea *)malloc(sizeof(struct memArea)); - if (!newArea){ - pr_devel("Failed to allocated area of %d pages\n", nbPages); - return (vaddr_t)NULL; - } - - // Avoid insertSorted(freeArea, newArea) call by modifying area - - newArea->nbPages = nbPages; - newArea->startAddr = area->startAddr; - - area->nbPages -= nbPages; - area->startAddr += nbPages * PAGE_SIZE; - - insertSorted(&usedArea, newArea); - - allocated = newArea; - - for (uint i = 0; i < nbPages; i++) { - paddr_t page = allocPhyPage(1); - if (page) { - pageMap(newArea->startAddr + i * PAGE_SIZE, page, PAGING_MEM_WRITE); - } else { - // TODO - assert(1); + if (flags & AREA_PHY_MAP) { + for (uint i = 0; i < nbPages; i++) { + paddr_t page = allocPhyPage(1); + if (page) { + pageMap(allocated->startAddr + i * PAGE_SIZE, page, PAGING_MEM_WRITE); + } else { + // TODO + assert(1); + } } } @@ -175,6 +177,9 @@ 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); + } list_delete(usedArea, area); insertSorted(&freeArea, area); diff --git a/core/allocArea.h b/core/allocArea.h index 292190b..a2030c0 100644 --- a/core/allocArea.h +++ b/core/allocArea.h @@ -2,6 +2,11 @@ #include "paging.h" #include "stdarg.h" +/* Pure Virtual Memory Allocation */ + +// areaAlloc map vmem to phy pages +#define AREA_PHY_MAP (1<<0) + struct memArea { vaddr_t startAddr; uint nbPages; @@ -11,6 +16,6 @@ struct memArea { }; void areaInit(vaddr_t firstMemUsed, vaddr_t lastUsed); -vaddr_t areaAlloc(unsigned int nbPages); +vaddr_t areaAlloc(unsigned int nbPages, uint32_t flags); int areaFree(vaddr_t addr); int areaAdd(vaddr_t addr, uint nbPages, int isFree);