user_space #4
@ -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"
|
||||||
@ -25,10 +25,10 @@ void areaInit(vaddr_t firstMemUsed, vaddr_t lastUsed, vaddr_t stackBottom, vaddr
|
|||||||
if (areaAddr != descAddr && areaAddr != entryAddr)
|
if (areaAddr != descAddr && areaAddr != entryAddr)
|
||||||
areaAdd(areaAddr, areaAddr + PAGE_SIZE, FALSE);
|
areaAdd(areaAddr, areaAddr + PAGE_SIZE, FALSE);
|
||||||
|
|
||||||
//kernel bootstrap part
|
// kernel bootstrap part
|
||||||
areaAdd(ALIGN_DOWN(firstMemUsed, PAGE_SIZE), stackBottom, FALSE);
|
areaAdd(ALIGN_DOWN(firstMemUsed, PAGE_SIZE), stackBottom, FALSE);
|
||||||
|
|
||||||
//Initial kernel stack
|
// Initial kernel stack
|
||||||
areaAdd(stackBottom, stackTop, FALSE);
|
areaAdd(stackBottom, stackTop, FALSE);
|
||||||
|
|
||||||
// Rest of kernel code
|
// Rest of kernel code
|
||||||
@ -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;
|
||||||
@ -81,7 +82,7 @@ vaddr_t areaBook(unsigned int nbPages, uint32_t flags){
|
|||||||
disable_IRQs(irqFlags);
|
disable_IRQs(irqFlags);
|
||||||
|
|
||||||
area = areaFindFit(nbPages);
|
area = areaFindFit(nbPages);
|
||||||
if (!area){
|
if (!area) {
|
||||||
printf("NULL<\n");
|
printf("NULL<\n");
|
||||||
restore_IRQs(irqFlags);
|
restore_IRQs(irqFlags);
|
||||||
|
|
||||||
@ -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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -125,7 +133,7 @@ vaddr_t areaAlloc(unsigned int nbPages, uint32_t flags)
|
|||||||
disable_IRQs(irqFlags);
|
disable_IRQs(irqFlags);
|
||||||
|
|
||||||
area = areaFindFit(nbPages);
|
area = areaFindFit(nbPages);
|
||||||
if (!area){
|
if (!area) {
|
||||||
restore_IRQs(irqFlags);
|
restore_IRQs(irqFlags);
|
||||||
|
|
||||||
return (vaddr_t)NULL;
|
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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -177,9 +192,10 @@ 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));
|
||||||
assert(IS_ALIGNED(end, PAGE_SIZE));
|
assert(IS_ALIGNED(end, PAGE_SIZE));
|
||||||
|
|
||||||
|
@ -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
|
||||||
|
@ -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);
|
||||||
|
Loading…
Reference in New Issue
Block a user