Rename memDesc to phyMemDesc

This commit is contained in:
Mathieu Maret 2021-04-10 21:53:03 +02:00
parent 4887984322
commit 865d3b811e
4 changed files with 42 additions and 39 deletions

View File

@ -5,9 +5,9 @@
#include "list.h" #include "list.h"
#include "types.h" #include "types.h"
static struct memDesc *pageDesc = (struct memDesc *)&__ld_kernel_end; static struct phyMemDesc *pageDesc = (struct phyMemDesc *)&__ld_kernel_end;
static struct memDesc *freePage; static struct phyMemDesc *phyFreePage;
static struct memDesc *usedPage; static struct phyMemDesc *phyUsedPage;
paddr_t pageDescEnd; paddr_t pageDescEnd;
paddr_t upperMem; paddr_t upperMem;
@ -15,8 +15,8 @@ static unsigned long allocatedPage = 0;
int memSetup(paddr_t upperMemKB, paddr_t *firstUsed, paddr_t *lastMemUsedOut) int memSetup(paddr_t upperMemKB, paddr_t *firstUsed, paddr_t *lastMemUsedOut)
{ {
list_init(freePage); list_init(phyFreePage);
list_init(usedPage); list_init(phyUsedPage);
// Align upper mem (in kB) on page size even if it does loose a page // Align upper mem (in kB) on page size even if it does loose a page
upperMemKB = ALIGN_DOWN(upperMemKB, PAGE_SIZE / 1024); 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); nbPage, PAGE_SIZE);
// Memory description is stored after the kernel. We need some place to store it // 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); *lastMemUsedOut = ALIGN(pageDescEnd, PAGE_SIZE);
upperMem = upperMemKB * 1024; upperMem = upperMemKB * 1024;
*firstUsed = ALIGN_DOWN((paddr_t)&__ld_kernel_begin, PAGE_SIZE); *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); topMem = min(topMem, upperMem);
for (uint i = (bottomMem >> PAGE_SHIFT); i < (topMem >> PAGE_SHIFT); i++) { for (uint i = (bottomMem >> PAGE_SHIFT); i < (topMem >> PAGE_SHIFT); i++) {
struct memDesc *mem = &pageDesc[i]; struct phyMemDesc *mem = &pageDesc[i];
if (isFree) { if (isFree) {
mem->ref = 0; mem->ref = 0;
list_add_tail(freePage, mem); list_add_tail(phyFreePage, mem);
} else { } else {
mem->ref = 1; mem->ref = 1;
list_add_tail(usedPage, mem); list_add_tail(phyUsedPage, mem);
} }
mem->phy_addr = i * PAGE_SIZE; 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) void memGetStat(uint *free, uint *used)
{ {
uint idx; uint idx;
struct memDesc *mem; struct phyMemDesc *mem;
list_foreach(freePage, mem, idx) list_foreach(phyFreePage, mem, idx)
{ {
continue; continue;
} }
*free = idx; *free = idx;
list_foreach(usedPage, mem, idx) list_foreach(phyUsedPage, mem, idx)
{ {
continue; continue;
} }
*used = idx; *used = idx;
} }
struct memDesc *addr2memDesc(paddr_t addr) struct phyMemDesc *addr2memDesc(paddr_t addr)
{ {
int idx = addr >> PAGE_SHIFT; int idx = addr >> PAGE_SHIFT;
return pageDesc + idx; return pageDesc + idx;
} }
struct memDesc *memFindConsecutiveFreePage(uint nbPage) struct phyMemDesc *memFindConsecutiveFreePage(uint nbPage)
{ {
struct memDesc *mem, *head; struct phyMemDesc *mem, *head;
uint memIdx, count; uint memIdx, count;
if (list_is_empty(freePage)) { if (list_is_empty(phyFreePage)) {
return NULL; return NULL;
} }
count = 1; count = 1;
memIdx = 0; memIdx = 0;
head = freePage; head = phyFreePage;
mem = freePage; mem = phyFreePage;
while (count < nbPage && (!memIdx || mem != freePage)) { while (count < nbPage && (!memIdx || mem != phyFreePage)) {
memIdx++; memIdx++;
mem = mem->next; mem = mem->next;
if (mem->phy_addr == head->phy_addr + count * PAGE_SIZE) { if (mem->phy_addr == head->phy_addr + count * PAGE_SIZE) {
@ -112,7 +112,7 @@ struct memDesc *memFindConsecutiveFreePage(uint nbPage)
paddr_t allocPhyPage(uint nbPage) paddr_t allocPhyPage(uint nbPage)
{ {
struct memDesc *mem, *head, *next; struct phyMemDesc *mem, *head, *next;
uint count; uint count;
head = memFindConsecutiveFreePage(nbPage); head = memFindConsecutiveFreePage(nbPage);
@ -125,9 +125,9 @@ paddr_t allocPhyPage(uint nbPage)
mem = head; mem = head;
next = head->next; next = head->next;
for (count = 0; count < nbPage; count++) { for (count = 0; count < nbPage; count++) {
list_delete(freePage, mem); list_delete(phyFreePage, mem);
mem->ref = 1; mem->ref = 1;
list_add_tail(usedPage, mem); list_add_tail(phyUsedPage, mem);
mem = next; mem = next;
next = mem->next; next = mem->next;
} }
@ -137,7 +137,7 @@ paddr_t allocPhyPage(uint nbPage)
int unrefPhyPage(paddr_t addr) int unrefPhyPage(paddr_t addr)
{ {
struct memDesc *mem = addr2memDesc(addr); struct phyMemDesc *mem = addr2memDesc(addr);
if (!mem) { if (!mem) {
return -1; return -1;
} }
@ -145,8 +145,8 @@ int unrefPhyPage(paddr_t addr)
mem->ref--; mem->ref--;
if (mem->ref == 0) { if (mem->ref == 0) {
allocatedPage--; allocatedPage--;
list_delete(usedPage, mem); list_delete(phyUsedPage, mem);
list_add_tail(freePage, mem); // TODO find the right place to keep free_page sorted; list_add_tail(phyFreePage, mem); // TODO find the right place to keep free_page sorted;
} }
return mem->ref; return mem->ref;
@ -154,15 +154,15 @@ int unrefPhyPage(paddr_t addr)
int refPhyPage(paddr_t addr) int refPhyPage(paddr_t addr)
{ {
struct memDesc *mem = addr2memDesc(addr); struct phyMemDesc *mem = addr2memDesc(addr);
if (!mem) { if (!mem) {
return -1; return -1;
} }
mem->ref++; mem->ref++;
if (mem->ref == 1) { if (mem->ref == 1) {
allocatedPage++; allocatedPage++;
list_add_tail(usedPage, mem); list_add_tail(phyUsedPage, mem);
list_delete(freePage, mem); list_delete(phyFreePage, mem);
} }
return 0; return 0;

View File

@ -2,6 +2,9 @@
#include "stdarg.h" #include "stdarg.h"
#include "types.h" #include "types.h"
/** Physical Page related function
*/
#define PAGE_SHIFT 12U #define PAGE_SHIFT 12U
#define PAGE_SIZE (1U << PAGE_SHIFT) #define PAGE_SIZE (1U << PAGE_SHIFT)
@ -9,10 +12,10 @@
extern uint32_t __ld_kernel_begin; extern uint32_t __ld_kernel_begin;
extern uint32_t __ld_kernel_end; extern uint32_t __ld_kernel_end;
struct memDesc { struct phyMemDesc {
paddr_t phy_addr; paddr_t phy_addr;
unsigned long ref; unsigned long ref;
struct memDesc *next, *prev; struct phyMemDesc *next, *prev;
}; };
int memSetup(paddr_t upperMem, paddr_t * firstUsed, paddr_t *lastUsed); int memSetup(paddr_t upperMem, paddr_t * firstUsed, paddr_t *lastUsed);

View File

@ -128,7 +128,7 @@ class PhyMemDescListDumpCmd(gdb.Command):
if args: if args:
desc = gdb.parse_and_eval(args) desc = gdb.parse_and_eval(args)
else: else:
desc = gdb.parse_and_eval("freePage") desc = gdb.parse_and_eval("phyFreePage")
if str(desc.type) != "struct memDesc *": if str(desc.type) != "struct memDesc *":
print("Expected pointer argument of type (struct memDesc *)") print("Expected pointer argument of type (struct memDesc *)")
return return

View File

@ -14,8 +14,8 @@
void testPhymem(void) void testPhymem(void)
{ {
printf("Testing memory PHY\n"); printf("Testing memory PHY\n");
struct memDesc *allocated_page_list; struct phyMemDesc *allocated_page_list;
struct memDesc struct phyMemDesc
*page; // Cast in mem_desc to use it. In fact it's the addr of 4K free memory *page; // Cast in mem_desc to use it. In fact it's the addr of 4K free memory
list_init(allocated_page_list); list_init(allocated_page_list);
int allocCount = 0; int allocCount = 0;
@ -27,7 +27,7 @@ void testPhymem(void)
memGetStat(&freePageStatBegin, &usedPageStatBegin); memGetStat(&freePageStatBegin, &usedPageStatBegin);
while ((page = (struct memDesc *)allocPhyPage(1)) != NULL) { while ((page = (struct phyMemDesc *)allocPhyPage(1)) != NULL) {
page->phy_addr = allocCount; page->phy_addr = allocCount;
allocCount++; allocCount++;
list_add_tail(allocated_page_list, page); list_add_tail(allocated_page_list, page);
@ -47,7 +47,7 @@ void testPhymem(void)
assert(freePageStatFree == freePageStatBegin); assert(freePageStatFree == freePageStatBegin);
assert(usedPageStatFree == usedPageStatBegin); 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); unrefPhyPage((ulong)page);
} }
@ -109,14 +109,14 @@ static void testAlloc(void)
static void testPaging(void) static void testPaging(void)
{ {
printf("Testing paging\n"); printf("Testing paging\n");
struct memDesc *allocated_page_list; struct phyMemDesc *allocated_page_list;
struct memDesc struct phyMemDesc
*page; // Cast in mem_desc to use it. In fact it's the addr of 4K free memory *page; // Cast in mem_desc to use it. In fact it's the addr of 4K free memory
list_init(allocated_page_list); list_init(allocated_page_list);
int allocCount = 0; int allocCount = 0;
int freeCount = 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, assertmsg(pageMap((vaddr_t)page, (paddr_t)page, PAGING_MEM_WRITE) == 0,
"Fail to map page %d\n", allocCount); "Fail to map page %d\n", allocCount);
memset(page, allocCount, PAGE_SIZE); memset(page, allocCount, PAGE_SIZE);
@ -135,7 +135,7 @@ static void testPaging(void)
} }
printf("%d pages freed\n", freeCount); 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); unrefPhyPage((ulong)page);
} }