Browse Source

Fix Paging Mapping

New PT entry should be set unsing the Vaddr, not the Paddr
simpleList
Mathieu Maret 1 year ago
parent
commit
32f1a8ba9d
2 changed files with 14 additions and 11 deletions
  1. +8
    -7
      core/paging.c
  2. +6
    -4
      tests/test.c

+ 8
- 7
core/paging.c View File

@@ -19,7 +19,7 @@
#define PT_SHIFT 12
#define PTE_MASK 0x3ff //10bits
#define PD_SHIFT 22
#define PD_MIRROR_PAGE_IDX 1023
#define PD_MIRROR_PAGE_IDX 1023U

struct pde {
uint32_t present : 1;
@@ -128,15 +128,15 @@ int pageMap(vaddr_t vaddr, paddr_t paddr, int flags)
uint ptEntry = (vaddr >> PT_SHIFT) & PTE_MASK;

// Thank to mirroring, we can access the PD
struct pde *pd = (struct pde *)(PD_MIRROR_PAGE_IDX * (1U << 22) +
PD_MIRROR_PAGE_IDX * (1U << 12));
struct pde *pd =
(struct pde *)((PD_MIRROR_PAGE_IDX << 22) + (PD_MIRROR_PAGE_IDX << 12));

struct pte *pt = (struct pte *)(PD_MIRROR_PAGE_IDX * (1U << 22) +
pdEntry * (1U << 12));
struct pte *pt = (struct pte *)((PD_MIRROR_PAGE_IDX << 22) + (pdEntry << 12));

if(!pd[pdEntry].present){
if (!pd[pdEntry].present) {
paddr_t ptPhy = allocPhyPage();
memset((void *)ptPhy, 0, PAGE_SIZE);
if (ptPhy == (vaddr_t)NULL)
return ENOMEM;

pd[pdEntry].user = (flags & PAGING_MEM_USER) ? 1:0;
pd[pdEntry].present = 1;
@@ -144,6 +144,7 @@ int pageMap(vaddr_t vaddr, paddr_t paddr, int flags)
pd[pdEntry].pt_addr = (ptPhy >> PT_SHIFT);

__native_flush_tlb_single((vaddr_t)pt);
memset((void *)pt, 0, PAGE_SIZE);
}

// Already mapped ? Remove old mapping


+ 6
- 4
tests/test.c View File

@@ -25,7 +25,7 @@ void testPhymem(void)
if (page->phy_addr != (ulong)freeCount) {
printf("Error page %d modified\n", (ulong)page);
}
if (unrefPhyPage((ulong)page)) {
if (unrefPhyPage((ulong)page) < 0) {
printf("Failed to free page %d\n", (ulong)page);
}
freeCount++;
@@ -50,7 +50,10 @@ static void testPaging(void)
int freeCount = 0;

while ((page = (struct mem_desc *)allocPhyPage()) != NULL) {
pageMap((vaddr_t)page, (paddr_t)page, 0);
if (pageMap((vaddr_t)page, (paddr_t)page, PAGING_MEM_WRITE)){
printf("Fail to map page %d\n", allocCount);
break;
}
page->phy_addr = allocCount;
allocCount++;
list_add_tail(allocated_page_list, page);
@@ -61,7 +64,7 @@ static void testPaging(void)
if (page->phy_addr != (ulong)freeCount) {
printf("Error page %d modified\n", (ulong)page);
}
if (unrefPhyPage((ulong)page)) {
if (unrefPhyPage((ulong)page) < 0) {
printf("Failed to free page %d\n", (ulong)page);
}
pageUnmap((vaddr_t)page);
@@ -73,7 +76,6 @@ static void testPaging(void)
printf("Error ! Cannot allocate memory\n");
} else {
unrefPhyPage((ulong)page);
pageUnmap((vaddr_t)page);
}
}



Loading…
Cancel
Save