user_space #4
@ -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"
|
||||
@ -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;
|
||||
@ -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);
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -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);
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -178,6 +193,7 @@ int areaAdd(vaddr_t start, vaddr_t end, int isFree)
|
||||
struct memArea **area;
|
||||
|
||||
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(IS_ALIGNED(start, PAGE_SIZE));
|
||||
|
@ -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
|
||||
|
@ -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);
|
||||
|
Loading…
Reference in New Issue
Block a user