Rename memDesc to phyMemDesc
This commit is contained in:
parent
4887984322
commit
865d3b811e
56
core/mem.c
56
core/mem.c
@ -5,9 +5,9 @@
|
||||
#include "list.h"
|
||||
#include "types.h"
|
||||
|
||||
static struct memDesc *pageDesc = (struct memDesc *)&__ld_kernel_end;
|
||||
static struct memDesc *freePage;
|
||||
static struct memDesc *usedPage;
|
||||
static struct phyMemDesc *pageDesc = (struct phyMemDesc *)&__ld_kernel_end;
|
||||
static struct phyMemDesc *phyFreePage;
|
||||
static struct phyMemDesc *phyUsedPage;
|
||||
paddr_t pageDescEnd;
|
||||
paddr_t upperMem;
|
||||
|
||||
@ -15,8 +15,8 @@ static unsigned long allocatedPage = 0;
|
||||
|
||||
int memSetup(paddr_t upperMemKB, paddr_t *firstUsed, paddr_t *lastMemUsedOut)
|
||||
{
|
||||
list_init(freePage);
|
||||
list_init(usedPage);
|
||||
list_init(phyFreePage);
|
||||
list_init(phyUsedPage);
|
||||
|
||||
// Align upper mem (in kB) on page size even if it does loose a page
|
||||
upperMemKB = ALIGN_DOWN(upperMemKB, PAGE_SIZE / 1024);
|
||||
@ -27,7 +27,7 @@ int memSetup(paddr_t upperMemKB, paddr_t *firstUsed, paddr_t *lastMemUsedOut)
|
||||
nbPage, PAGE_SIZE);
|
||||
|
||||
// Memory description is stored after the kernel. We need some place to store it
|
||||
pageDescEnd = (unsigned long)pageDesc + nbPage * sizeof(struct memDesc);
|
||||
pageDescEnd = (unsigned long)pageDesc + nbPage * sizeof(struct phyMemDesc);
|
||||
*lastMemUsedOut = ALIGN(pageDescEnd, PAGE_SIZE);
|
||||
upperMem = upperMemKB * 1024;
|
||||
*firstUsed = ALIGN_DOWN((paddr_t)&__ld_kernel_begin, PAGE_SIZE);
|
||||
@ -44,13 +44,13 @@ int memAddBank(paddr_t bottomMem, paddr_t topMem, int isFree)
|
||||
{
|
||||
topMem = min(topMem, upperMem);
|
||||
for (uint i = (bottomMem >> PAGE_SHIFT); i < (topMem >> PAGE_SHIFT); i++) {
|
||||
struct memDesc *mem = &pageDesc[i];
|
||||
struct phyMemDesc *mem = &pageDesc[i];
|
||||
if (isFree) {
|
||||
mem->ref = 0;
|
||||
list_add_tail(freePage, mem);
|
||||
list_add_tail(phyFreePage, mem);
|
||||
} else {
|
||||
mem->ref = 1;
|
||||
list_add_tail(usedPage, mem);
|
||||
list_add_tail(phyUsedPage, mem);
|
||||
}
|
||||
mem->phy_addr = i * PAGE_SIZE;
|
||||
}
|
||||
@ -61,39 +61,39 @@ int memAddBank(paddr_t bottomMem, paddr_t topMem, int isFree)
|
||||
void memGetStat(uint *free, uint *used)
|
||||
{
|
||||
uint idx;
|
||||
struct memDesc *mem;
|
||||
list_foreach(freePage, mem, idx)
|
||||
struct phyMemDesc *mem;
|
||||
list_foreach(phyFreePage, mem, idx)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
*free = idx;
|
||||
list_foreach(usedPage, mem, idx)
|
||||
list_foreach(phyUsedPage, mem, idx)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
*used = idx;
|
||||
}
|
||||
|
||||
struct memDesc *addr2memDesc(paddr_t addr)
|
||||
struct phyMemDesc *addr2memDesc(paddr_t addr)
|
||||
{
|
||||
int idx = addr >> PAGE_SHIFT;
|
||||
return pageDesc + idx;
|
||||
}
|
||||
|
||||
struct memDesc *memFindConsecutiveFreePage(uint nbPage)
|
||||
struct phyMemDesc *memFindConsecutiveFreePage(uint nbPage)
|
||||
{
|
||||
struct memDesc *mem, *head;
|
||||
struct phyMemDesc *mem, *head;
|
||||
uint memIdx, count;
|
||||
|
||||
if (list_is_empty(freePage)) {
|
||||
if (list_is_empty(phyFreePage)) {
|
||||
return NULL;
|
||||
}
|
||||
count = 1;
|
||||
memIdx = 0;
|
||||
head = freePage;
|
||||
mem = freePage;
|
||||
head = phyFreePage;
|
||||
mem = phyFreePage;
|
||||
|
||||
while (count < nbPage && (!memIdx || mem != freePage)) {
|
||||
while (count < nbPage && (!memIdx || mem != phyFreePage)) {
|
||||
memIdx++;
|
||||
mem = mem->next;
|
||||
if (mem->phy_addr == head->phy_addr + count * PAGE_SIZE) {
|
||||
@ -112,7 +112,7 @@ struct memDesc *memFindConsecutiveFreePage(uint nbPage)
|
||||
|
||||
paddr_t allocPhyPage(uint nbPage)
|
||||
{
|
||||
struct memDesc *mem, *head, *next;
|
||||
struct phyMemDesc *mem, *head, *next;
|
||||
uint count;
|
||||
|
||||
head = memFindConsecutiveFreePage(nbPage);
|
||||
@ -125,9 +125,9 @@ paddr_t allocPhyPage(uint nbPage)
|
||||
mem = head;
|
||||
next = head->next;
|
||||
for (count = 0; count < nbPage; count++) {
|
||||
list_delete(freePage, mem);
|
||||
list_delete(phyFreePage, mem);
|
||||
mem->ref = 1;
|
||||
list_add_tail(usedPage, mem);
|
||||
list_add_tail(phyUsedPage, mem);
|
||||
mem = next;
|
||||
next = mem->next;
|
||||
}
|
||||
@ -137,7 +137,7 @@ paddr_t allocPhyPage(uint nbPage)
|
||||
|
||||
int unrefPhyPage(paddr_t addr)
|
||||
{
|
||||
struct memDesc *mem = addr2memDesc(addr);
|
||||
struct phyMemDesc *mem = addr2memDesc(addr);
|
||||
if (!mem) {
|
||||
return -1;
|
||||
}
|
||||
@ -145,8 +145,8 @@ int unrefPhyPage(paddr_t addr)
|
||||
mem->ref--;
|
||||
if (mem->ref == 0) {
|
||||
allocatedPage--;
|
||||
list_delete(usedPage, mem);
|
||||
list_add_tail(freePage, mem); // TODO find the right place to keep free_page sorted;
|
||||
list_delete(phyUsedPage, mem);
|
||||
list_add_tail(phyFreePage, mem); // TODO find the right place to keep free_page sorted;
|
||||
}
|
||||
|
||||
return mem->ref;
|
||||
@ -154,15 +154,15 @@ int unrefPhyPage(paddr_t addr)
|
||||
|
||||
int refPhyPage(paddr_t addr)
|
||||
{
|
||||
struct memDesc *mem = addr2memDesc(addr);
|
||||
struct phyMemDesc *mem = addr2memDesc(addr);
|
||||
if (!mem) {
|
||||
return -1;
|
||||
}
|
||||
mem->ref++;
|
||||
if (mem->ref == 1) {
|
||||
allocatedPage++;
|
||||
list_add_tail(usedPage, mem);
|
||||
list_delete(freePage, mem);
|
||||
list_add_tail(phyUsedPage, mem);
|
||||
list_delete(phyFreePage, mem);
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
@ -2,6 +2,9 @@
|
||||
#include "stdarg.h"
|
||||
#include "types.h"
|
||||
|
||||
/** Physical Page related function
|
||||
*/
|
||||
|
||||
#define PAGE_SHIFT 12U
|
||||
#define PAGE_SIZE (1U << PAGE_SHIFT)
|
||||
|
||||
@ -9,10 +12,10 @@
|
||||
extern uint32_t __ld_kernel_begin;
|
||||
extern uint32_t __ld_kernel_end;
|
||||
|
||||
struct memDesc {
|
||||
struct phyMemDesc {
|
||||
paddr_t phy_addr;
|
||||
unsigned long ref;
|
||||
struct memDesc *next, *prev;
|
||||
struct phyMemDesc *next, *prev;
|
||||
};
|
||||
|
||||
int memSetup(paddr_t upperMem, paddr_t * firstUsed, paddr_t *lastUsed);
|
||||
|
@ -128,7 +128,7 @@ class PhyMemDescListDumpCmd(gdb.Command):
|
||||
if args:
|
||||
desc = gdb.parse_and_eval(args)
|
||||
else:
|
||||
desc = gdb.parse_and_eval("freePage")
|
||||
desc = gdb.parse_and_eval("phyFreePage")
|
||||
if str(desc.type) != "struct memDesc *":
|
||||
print("Expected pointer argument of type (struct memDesc *)")
|
||||
return
|
||||
|
16
tests/test.c
16
tests/test.c
@ -14,8 +14,8 @@
|
||||
void testPhymem(void)
|
||||
{
|
||||
printf("Testing memory PHY\n");
|
||||
struct memDesc *allocated_page_list;
|
||||
struct memDesc
|
||||
struct phyMemDesc *allocated_page_list;
|
||||
struct phyMemDesc
|
||||
*page; // Cast in mem_desc to use it. In fact it's the addr of 4K free memory
|
||||
list_init(allocated_page_list);
|
||||
int allocCount = 0;
|
||||
@ -27,7 +27,7 @@ void testPhymem(void)
|
||||
|
||||
memGetStat(&freePageStatBegin, &usedPageStatBegin);
|
||||
|
||||
while ((page = (struct memDesc *)allocPhyPage(1)) != NULL) {
|
||||
while ((page = (struct phyMemDesc *)allocPhyPage(1)) != NULL) {
|
||||
page->phy_addr = allocCount;
|
||||
allocCount++;
|
||||
list_add_tail(allocated_page_list, page);
|
||||
@ -47,7 +47,7 @@ void testPhymem(void)
|
||||
assert(freePageStatFree == freePageStatBegin);
|
||||
assert(usedPageStatFree == usedPageStatBegin);
|
||||
|
||||
assertmsg((page = (struct memDesc *)allocPhyPage(1)) != NULL, "Cannot allocate memory\n");
|
||||
assertmsg((page = (struct phyMemDesc *)allocPhyPage(1)) != NULL, "Cannot allocate memory\n");
|
||||
unrefPhyPage((ulong)page);
|
||||
}
|
||||
|
||||
@ -109,14 +109,14 @@ static void testAlloc(void)
|
||||
static void testPaging(void)
|
||||
{
|
||||
printf("Testing paging\n");
|
||||
struct memDesc *allocated_page_list;
|
||||
struct memDesc
|
||||
struct phyMemDesc *allocated_page_list;
|
||||
struct phyMemDesc
|
||||
*page; // Cast in mem_desc to use it. In fact it's the addr of 4K free memory
|
||||
list_init(allocated_page_list);
|
||||
int allocCount = 0;
|
||||
int freeCount = 0;
|
||||
|
||||
while ((page = (struct memDesc *)allocPhyPage(1)) != NULL) {
|
||||
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);
|
||||
memset(page, allocCount, PAGE_SIZE);
|
||||
@ -135,7 +135,7 @@ static void testPaging(void)
|
||||
}
|
||||
printf("%d pages freed\n", freeCount);
|
||||
|
||||
assertmsg((page = (struct memDesc *)allocPhyPage(1)) != NULL, "Cannot allocate memory\n");
|
||||
assertmsg((page = (struct phyMemDesc *)allocPhyPage(1)) != NULL, "Cannot allocate memory\n");
|
||||
unrefPhyPage((ulong)page);
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user