user_space #4

Merged
mathieu merged 40 commits from user_space into master 2021-11-04 16:17:36 +01:00
4 changed files with 41 additions and 31 deletions
Showing only changes of commit 46e1bb0642 - Show all commits

View File

@ -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)

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);
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;
}

View File

@ -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,8 +75,7 @@ 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);
@ -94,16 +93,19 @@ vaddr_t areaAlloc(unsigned int nbPages)
insertSorted(&usedArea, newArea);
allocated = newArea;
}
if (flags & AREA_PHY_MAP) {
for (uint i = 0; i < nbPages; i++) {
paddr_t page = allocPhyPage(1);
if (page) {
pageMap(newArea->startAddr + i * PAGE_SIZE, page, PAGING_MEM_WRITE);
pageMap(allocated->startAddr + i * PAGE_SIZE, page, PAGING_MEM_WRITE);
} else {
// TODO
assert(1);
}
}
}
return allocated->startAddr;
}
@ -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);

View File

@ -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);