user_space #4

Merged
mathieu merged 40 commits from user_space into master 2021-11-04 16:17:36 +01:00
3 changed files with 36 additions and 20 deletions
Showing only changes of commit 7081fea36d - Show all commits

View File

@ -1,8 +1,8 @@
#include "allocArea.h" #include "allocArea.h"
#include "alloc.h" #include "alloc.h"
#include "assert.h" #include "assert.h"
#include "kernel.h"
#include "irq.h" #include "irq.h"
#include "kernel.h"
#include "list.h" #include "list.h"
#include "mem.h" #include "mem.h"
#include "stdarg.h" #include "stdarg.h"
@ -72,7 +72,8 @@ static void insertSorted(struct memArea **list, struct memArea *item)
list_add_tail(*list, item); list_add_tail(*list, item);
} }
vaddr_t areaBook(unsigned int nbPages, uint32_t flags){ vaddr_t areaBook(unsigned int nbPages, uint32_t flags)
{
struct memArea *area; struct memArea *area;
vaddr_t allocated; vaddr_t allocated;
@ -97,18 +98,25 @@ vaddr_t areaBook(unsigned int nbPages, uint32_t flags){
area->nbPages -= nbPages; area->nbPages -= nbPages;
area->startAddr += nbPages * PAGE_SIZE; area->startAddr += nbPages * PAGE_SIZE;
} }
if (flags & AREA_PHY_MAP) { 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(allocated + i * PAGE_SIZE, page, PAGING_MEM_WRITE); if (pageMap(allocated + i * PAGE_SIZE, page, PAGING_MEM_WRITE)) {
unrefPhyPage(page);
page = (paddr_t)NULL;
} else { } else {
unrefPhyPage(page);
}
}
if (page == (paddr_t)NULL) {
printf("Ooops\n"); printf("Ooops\n");
// TODO // TODO
assert(0); // assert(0);
restore_IRQs(irqFlags);
return (vaddr_t)NULL;
} }
} }
} }
@ -160,11 +168,18 @@ vaddr_t areaAlloc(unsigned int nbPages, uint32_t flags)
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(allocated->startAddr + i * PAGE_SIZE, page, PAGING_MEM_WRITE); if (pageMap(allocated->startAddr + i * PAGE_SIZE, page, PAGING_MEM_WRITE)) {
page = (paddr_t)NULL;
} else { } else {
unrefPhyPage(page);
}
}
if (page == (paddr_t)NULL) {
printf("Ooops\n"); printf("Ooops\n");
// TODO // TODO
assert(0); // assert(0);
restore_IRQs(irqFlags);
return (vaddr_t)NULL;
} }
} }
} }
@ -178,6 +193,7 @@ int areaAdd(vaddr_t start, vaddr_t end, int isFree)
struct memArea **area; struct memArea **area;
int nbPages = (end - start) / PAGE_SIZE; int nbPages = (end - start) / PAGE_SIZE;
pr_devel("Add %s area 0x%x->0x%x (%d)\n", isFree ? "free" : "used", start, end, nbPages);
assert(nbPages > 0); assert(nbPages > 0);
assert(IS_ALIGNED(start, PAGE_SIZE)); assert(IS_ALIGNED(start, PAGE_SIZE));

View File

@ -55,6 +55,8 @@ struct kthread *kthreadCreate(const char *name, cpu_kstate_function_arg1_t func,
return NULL; return NULL;
thread->stackAddr = (vaddr_t)malloc(KTHREAD_DEFAULT_STACK_SIZE); thread->stackAddr = (vaddr_t)malloc(KTHREAD_DEFAULT_STACK_SIZE);
if(!thread->stackAddr)
return NULL;
#ifdef DEBUG #ifdef DEBUG
printf("Alloc stack at 0x%x struct at 0x%x\n", thread->stackAddr, thread); printf("Alloc stack at 0x%x struct at 0x%x\n", thread->stackAddr, thread);
#endif #endif

View File

@ -154,9 +154,7 @@ static void testPaging(void)
int allocCount = 0; int allocCount = 0;
int freeCount = 0; int freeCount = 0;
while ((page = (struct phyMemDesc *)allocPhyPage(1)) != NULL) { while ((page = (struct phyMemDesc *)areaAlloc(1, AREA_PHY_MAP)) != NULL) {
assertmsg(pageMap((vaddr_t)page, (paddr_t)page, PAGING_MEM_WRITE) == 0,
"Fail to map page %d\n", allocCount);
memset(page, allocCount, PAGE_SIZE); memset(page, allocCount, PAGE_SIZE);
allocCount++; allocCount++;
list_add_tail(allocated_page_list, page); list_add_tail(allocated_page_list, page);
@ -167,12 +165,12 @@ static void testPaging(void)
(page = list_pop_head(allocated_page_list)) != NULL) { (page = list_pop_head(allocated_page_list)) != NULL) {
assertmsg((char)page->phy_addr == (char)freeCount, "page modified %d but is %d\n", assertmsg((char)page->phy_addr == (char)freeCount, "page modified %d but is %d\n",
freeCount, page->phy_addr); freeCount, page->phy_addr);
assertmsg(unrefPhyPage((ulong)page) >= 0, "Failed to free page %d\n", (ulong)page); areaFree((vaddr_t)page);
pageUnmap((vaddr_t)page);
freeCount++; freeCount++;
} }
printf("%d pages freed\n", freeCount); printf("%d pages freed\n", freeCount);
assert(freeCount == allocCount);
assertmsg((page = (struct phyMemDesc *)allocPhyPage(1)) != NULL, assertmsg((page = (struct phyMemDesc *)allocPhyPage(1)) != NULL,
"Cannot allocate memory\n"); "Cannot allocate memory\n");
unrefPhyPage((ulong)page); unrefPhyPage((ulong)page);