Fix Paging Mapping
New PT entry should be set unsing the Vaddr, not the Paddr
This commit is contained in:
parent
bff505fb40
commit
32f1a8ba9d
@ -19,7 +19,7 @@
|
|||||||
#define PT_SHIFT 12
|
#define PT_SHIFT 12
|
||||||
#define PTE_MASK 0x3ff //10bits
|
#define PTE_MASK 0x3ff //10bits
|
||||||
#define PD_SHIFT 22
|
#define PD_SHIFT 22
|
||||||
#define PD_MIRROR_PAGE_IDX 1023
|
#define PD_MIRROR_PAGE_IDX 1023U
|
||||||
|
|
||||||
struct pde {
|
struct pde {
|
||||||
uint32_t present : 1;
|
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;
|
uint ptEntry = (vaddr >> PT_SHIFT) & PTE_MASK;
|
||||||
|
|
||||||
// Thank to mirroring, we can access the PD
|
// Thank to mirroring, we can access the PD
|
||||||
struct pde *pd = (struct pde *)(PD_MIRROR_PAGE_IDX * (1U << 22) +
|
struct pde *pd =
|
||||||
PD_MIRROR_PAGE_IDX * (1U << 12));
|
(struct pde *)((PD_MIRROR_PAGE_IDX << 22) + (PD_MIRROR_PAGE_IDX << 12));
|
||||||
|
|
||||||
struct pte *pt = (struct pte *)(PD_MIRROR_PAGE_IDX * (1U << 22) +
|
struct pte *pt = (struct pte *)((PD_MIRROR_PAGE_IDX << 22) + (pdEntry << 12));
|
||||||
pdEntry * (1U << 12));
|
|
||||||
|
|
||||||
if (!pd[pdEntry].present) {
|
if (!pd[pdEntry].present) {
|
||||||
paddr_t ptPhy = allocPhyPage();
|
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].user = (flags & PAGING_MEM_USER) ? 1:0;
|
||||||
pd[pdEntry].present = 1;
|
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);
|
pd[pdEntry].pt_addr = (ptPhy >> PT_SHIFT);
|
||||||
|
|
||||||
__native_flush_tlb_single((vaddr_t)pt);
|
__native_flush_tlb_single((vaddr_t)pt);
|
||||||
|
memset((void *)pt, 0, PAGE_SIZE);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Already mapped ? Remove old mapping
|
// Already mapped ? Remove old mapping
|
||||||
|
10
tests/test.c
10
tests/test.c
@ -25,7 +25,7 @@ void testPhymem(void)
|
|||||||
if (page->phy_addr != (ulong)freeCount) {
|
if (page->phy_addr != (ulong)freeCount) {
|
||||||
printf("Error page %d modified\n", (ulong)page);
|
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);
|
printf("Failed to free page %d\n", (ulong)page);
|
||||||
}
|
}
|
||||||
freeCount++;
|
freeCount++;
|
||||||
@ -50,7 +50,10 @@ static void testPaging(void)
|
|||||||
int freeCount = 0;
|
int freeCount = 0;
|
||||||
|
|
||||||
while ((page = (struct mem_desc *)allocPhyPage()) != NULL) {
|
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;
|
page->phy_addr = allocCount;
|
||||||
allocCount++;
|
allocCount++;
|
||||||
list_add_tail(allocated_page_list, page);
|
list_add_tail(allocated_page_list, page);
|
||||||
@ -61,7 +64,7 @@ static void testPaging(void)
|
|||||||
if (page->phy_addr != (ulong)freeCount) {
|
if (page->phy_addr != (ulong)freeCount) {
|
||||||
printf("Error page %d modified\n", (ulong)page);
|
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);
|
printf("Failed to free page %d\n", (ulong)page);
|
||||||
}
|
}
|
||||||
pageUnmap((vaddr_t)page);
|
pageUnmap((vaddr_t)page);
|
||||||
@ -73,7 +76,6 @@ static void testPaging(void)
|
|||||||
printf("Error ! Cannot allocate memory\n");
|
printf("Error ! Cannot allocate memory\n");
|
||||||
} else {
|
} else {
|
||||||
unrefPhyPage((ulong)page);
|
unrefPhyPage((ulong)page);
|
||||||
pageUnmap((vaddr_t)page);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user