Fix areaAlloc map failing and tests

This commit is contained in:
Mathieu Maret 2021-10-28 23:49:58 +02:00
parent d702c7b60e
commit 7081fea36d
3 changed files with 36 additions and 20 deletions

View File

@ -1,8 +1,8 @@
#include "allocArea.h"
#include "alloc.h"
#include "assert.h"
#include "kernel.h"
#include "irq.h"
#include "kernel.h"
#include "list.h"
#include "mem.h"
#include "stdarg.h"
@ -25,10 +25,10 @@ void areaInit(vaddr_t firstMemUsed, vaddr_t lastUsed, vaddr_t stackBottom, vaddr
if (areaAddr != descAddr && areaAddr != entryAddr)
areaAdd(areaAddr, areaAddr + PAGE_SIZE, FALSE);
//kernel bootstrap part
// kernel bootstrap part
areaAdd(ALIGN_DOWN(firstMemUsed, PAGE_SIZE), stackBottom, FALSE);
//Initial kernel stack
// Initial kernel stack
areaAdd(stackBottom, stackTop, FALSE);
// Rest of kernel code
@ -72,7 +72,8 @@ static void insertSorted(struct memArea **list, struct memArea *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;
vaddr_t allocated;
@ -81,7 +82,7 @@ vaddr_t areaBook(unsigned int nbPages, uint32_t flags){
disable_IRQs(irqFlags);
area = areaFindFit(nbPages);
if (!area){
if (!area) {
printf("NULL<\n");
restore_IRQs(irqFlags);
@ -97,18 +98,25 @@ vaddr_t areaBook(unsigned int nbPages, uint32_t flags){
area->nbPages -= nbPages;
area->startAddr += nbPages * PAGE_SIZE;
}
if (flags & AREA_PHY_MAP) {
for (uint i = 0; i < nbPages; i++) {
paddr_t page = allocPhyPage(1);
if (page) {
pageMap(allocated + i * PAGE_SIZE, page, PAGING_MEM_WRITE);
} else {
if (pageMap(allocated + i * PAGE_SIZE, page, PAGING_MEM_WRITE)) {
unrefPhyPage(page);
page = (paddr_t)NULL;
} else {
unrefPhyPage(page);
}
}
if (page == (paddr_t)NULL) {
printf("Ooops\n");
// TODO
assert(0);
// assert(0);
restore_IRQs(irqFlags);
return (vaddr_t)NULL;
}
}
}
@ -125,7 +133,7 @@ vaddr_t areaAlloc(unsigned int nbPages, uint32_t flags)
disable_IRQs(irqFlags);
area = areaFindFit(nbPages);
if (!area){
if (!area) {
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++) {
paddr_t page = allocPhyPage(1);
if (page) {
pageMap(allocated->startAddr + i * PAGE_SIZE, page, PAGING_MEM_WRITE);
} else {
if (pageMap(allocated->startAddr + i * PAGE_SIZE, page, PAGING_MEM_WRITE)) {
page = (paddr_t)NULL;
} else {
unrefPhyPage(page);
}
}
if (page == (paddr_t)NULL) {
printf("Ooops\n");
// TODO
assert(0);
// assert(0);
restore_IRQs(irqFlags);
return (vaddr_t)NULL;
}
}
}
@ -177,9 +192,10 @@ int areaAdd(vaddr_t start, vaddr_t end, int isFree)
{
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(end, PAGE_SIZE));

View File

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

View File

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