area alloc: mapping optionnal

This commit is contained in:
Mathieu Maret 2021-10-26 22:29:51 +02:00
parent 2c6ffe34a1
commit 46e1bb0642
4 changed files with 41 additions and 31 deletions

View File

@ -1,7 +1,7 @@
#pragma once #pragma once
#include "types.h" #include "types.h"
#define PAGING_MEM_USER 1 #define PAGING_MEM_USER (1U << 0)
#define PAGING_MEM_READ (1U << 1) #define PAGING_MEM_READ (1U << 1)
#define PAGING_MEM_WRITE (1U << 2) #define PAGING_MEM_WRITE (1U << 2)

View File

@ -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); nbPage = DIV_ROUND_UP(sizeSlab, PAGE_SIZE);
if (allocInitialized) { if (allocInitialized) {
alloc = areaAlloc(nbPage); alloc = areaAlloc(nbPage, AREA_PHY_MAP);
if (alloc == (paddr_t)NULL) if (alloc == (paddr_t)NULL)
return -ENOMEM; return -ENOMEM;
} else { } else {
@ -182,7 +182,7 @@ static int allocSlabEntry(struct slabEntry **desc, size_t size, size_t sizeSlab,
} }
nbPage = DIV_ROUND_UP(sizeSlab, PAGE_SIZE); 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) if (alloc == (paddr_t)NULL)
return -ENOMEM; return -ENOMEM;
@ -262,7 +262,7 @@ void *malloc(size_t size)
disable_IRQs(flags); disable_IRQs(flags);
if (size >= PAGE_SIZE){ 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; return (void *)area;
} }

View File

@ -63,7 +63,7 @@ static void insertSorted(struct memArea **list, struct memArea *item)
list_add_tail(*list, 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; struct memArea *area, *allocated;
@ -75,10 +75,9 @@ vaddr_t areaAlloc(unsigned int nbPages)
list_delete(freeArea, area); list_delete(freeArea, area);
insertSorted(&usedArea, area); insertSorted(&usedArea, area);
allocated = area; allocated = area;
} } else {
struct memArea *newArea = (struct memArea *)malloc(sizeof(struct memArea)); struct memArea *newArea = (struct memArea *)malloc(sizeof(struct memArea));
if (!newArea){ if (!newArea) {
pr_devel("Failed to allocated area of %d pages\n", nbPages); pr_devel("Failed to allocated area of %d pages\n", nbPages);
return (vaddr_t)NULL; return (vaddr_t)NULL;
} }
@ -94,16 +93,19 @@ vaddr_t areaAlloc(unsigned int nbPages)
insertSorted(&usedArea, newArea); insertSorted(&usedArea, newArea);
allocated = newArea; allocated = newArea;
}
if (flags & AREA_PHY_MAP) {
for (uint i = 0; i < nbPages; i++) { for (uint i = 0; i < nbPages; i++) {
paddr_t page = allocPhyPage(1); paddr_t page = allocPhyPage(1);
if (page) { if (page) {
pageMap(newArea->startAddr + i * PAGE_SIZE, page, PAGING_MEM_WRITE); pageMap(allocated->startAddr + i * PAGE_SIZE, page, PAGING_MEM_WRITE);
} else { } else {
// TODO // TODO
assert(1); assert(1);
} }
} }
}
return allocated->startAddr; return allocated->startAddr;
} }
@ -175,6 +177,9 @@ 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++){
pageUnmap( area->startAddr + i * PAGE_SIZE);
}
list_delete(usedArea, area); list_delete(usedArea, area);
insertSorted(&freeArea, area); insertSorted(&freeArea, area);

View File

@ -2,6 +2,11 @@
#include "paging.h" #include "paging.h"
#include "stdarg.h" #include "stdarg.h"
/* Pure Virtual Memory Allocation */
// areaAlloc map vmem to phy pages
#define AREA_PHY_MAP (1<<0)
struct memArea { struct memArea {
vaddr_t startAddr; vaddr_t startAddr;
uint nbPages; uint nbPages;
@ -11,6 +16,6 @@ struct memArea {
}; };
void areaInit(vaddr_t firstMemUsed, vaddr_t lastUsed); 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 areaFree(vaddr_t addr);
int areaAdd(vaddr_t addr, uint nbPages, int isFree); int areaAdd(vaddr_t addr, uint nbPages, int isFree);