[Bug] Problem with mapping

This commit is contained in:
Adrien Bourmault 2020-01-20 01:17:10 +01:00
parent d69e028f56
commit 8d839cd586
1 changed files with 54 additions and 121 deletions

View File

@ -217,55 +217,51 @@ void MmInitPaging(void)
//
// Get a page from an address
//
static ulong *MmGetPageDescriptorFromVirtual(void *virtualAddr)
ulong *MmGetPageDescriptorFromVirtual(void *virtualAddr)
{
volatile ulong virtAddrPage;
volatile pdpe_t *pdp;
volatile pde_t *pd;
volatile pte_t *pt;
volatile ulong *page;
volatile ulong index;
register ulong pml4Index = ((ulong)virtualAddr & 0xFF8000000000) >> 39; // Select bit from 39 to 48
register ulong pdpIndex = ((ulong)virtualAddr & 0x7FC0000000) >> 30; // Select bit from 39 to 48
register ulong pdIndex = ((ulong)virtualAddr & 0x3FE00000) >> 21; // Select bit from 39 to 48
register ulong ptIndex = ((ulong)virtualAddr & 0x1FF000) >> 12; // Select bit from 39 to 48
pdpe_t *pdp = NULL;
pde_t *pd = NULL;
pte_t *pt = NULL;
//DebugLog("Get virtual descriptor %p\n", virtualAddr);
while (virtualAddr) {
virtAddrPage = (ulong)virtualAddr & ( ~((KPAGESIZE - 1) | NX));
DebugLog("PML4[%d], PDP[%d], PD[%d], PT[%d]\n", pml4Index, pdpIndex, pdIndex, ptIndex);
index = (virtAddrPage / ((ulong)KPAGESIZE * 0x8000000)) % 512;
pdp = (pdpe_t*)((ulong)MmPageMapLevel4[index] & ( ~(KPAGESIZE - 1)) );
//DebugLog("pdp at %p\t: %p\n", &pdp, pdp);
if (!pdp) {
KalAllocMemoryEx((void**)&pdp, 512*sizeof(pdpe_t), M_ZEROED, KPAGESIZE);
MmPageMapLevel4[index] = (pdpe_t *)((ulong)pdp | PRESENT | READWRITE);
//DebugLog("Created pdp\t: %p\n", pdp);
continue;
}
index = (virtAddrPage / ((ulong)KPAGESIZE * 0x40000)) % 512;
pd = (pde_t*)( (ulong)pdp[index] & ( ~(KPAGESIZE - 1)) );
//DebugLog("pd at %p\t: %p\n", &pd, pd);
if (!pd) {
KalAllocMemoryEx((void**)&pd, 512*sizeof(pde_t), M_ZEROED, KPAGESIZE);
pdp[index] = (pde_t *)((ulong)pd | PRESENT | READWRITE);
//DebugLog("Created pd\t: %p\n", pd);
continue;
}
index = (virtAddrPage / ((ulong)KPAGESIZE * 0x200)) % 512;
pt = (pte_t*)( (ulong)pd[index] & ( ~(KPAGESIZE - 1)) );
DebugLog("pt at %p\t: %p\n", &pt, pt);
if (!pt) {
KalAllocMemoryEx((void**)&pt, 512*sizeof(pte_t), M_ZEROED, KPAGESIZE);
pd[index] = (pte_t *)((ulong)pt | PRESENT | READWRITE);
//DebugLog("Created pt\t: %p\n", pt);
continue;
}
break;
if (!((ulong)MmPageMapLevel4[pml4Index] & 0xFFFFFFFFFF000)) { // Select bit from 12 to 51
KalAllocMemoryEx((void**)&MmPageMapLevel4[pml4Index], 512*sizeof(pdpe_t), M_ZEROED, KPAGESIZE);
pdp = (pdpe_t *)((ulong)MmPageMapLevel4[pml4Index] & 0xFFFFFFFFFF000);
DebugLog("Create PDP\n");
} else {
pdp = (pdpe_t *)((ulong)MmPageMapLevel4[pml4Index] & 0xFFFFFFFFFF000);
}
index = ((ulong)virtualAddr / ((ulong)KPAGESIZE)) % 512;
page = &(pt[index]);
DebugLog("page (with flags): %p\n", *page);
return page;
DebugLog("PDP at %p = %p\n", &pdp[0], pdp[pdpIndex]);
if (!((ulong)pdp[pdpIndex] & 0xFFFFFFFFFF000)) { // Select bit from 12 to 51
KalAllocMemoryEx((void**)&pdp[pdpIndex], 512*sizeof(pde_t), M_ZEROED, KPAGESIZE);
pd = (pde_t *)((ulong)pdp[pdpIndex] & 0xFFFFFFFFFF000);
DebugLog("Create PD\n");
} else {
pd = (pde_t *)((ulong)pdp[pdpIndex] & 0xFFFFFFFFFF000);
}
DebugLog("PD at %p = %p\n", &pd[0], pd[pdIndex]);
if (!((ulong)pd[pdIndex] & 0xFFFFFFFFFF000)) { // Select bit from 12 to 51
KalAllocMemoryEx((void**)&pd[pdIndex], 512*sizeof(pte_t), M_ZEROED, KPAGESIZE);
pt = (pte_t *)((ulong)pd[pdIndex] & 0xFFFFFFFFFF000);
DebugLog("Create PT\n");
} else {
pt = (pte_t *)((ulong)pd[pdIndex] & 0xFFFFFFFFFF000);
}
DebugLog("PT at %p = %p\n", &pt[0], pt[ptIndex]);
MmLoadPML4((void *)MmPageMapLevel4);
return &pt[ptIndex];
}
//
@ -276,11 +272,11 @@ void *MmTransVirtToPhyAddr(void* virtualAddr)
ulong virtAddrPage = (ulong)virtualAddr & ( ~(KPAGESIZE - 1));
ulong *page = MmGetPageDescriptorFromVirtual(virtualAddr);
if (!(*page)) {
if (!(page)) {
return NULL;
}
return (void*)((*page & ~((KPAGESIZE - 1) | NX))+ ((ulong)virtualAddr - (ulong)virtAddrPage));
return (void*)(((ulong)*page & 0xFFFFFFFFFF000)+ ((ulong)virtualAddr - (ulong)virtAddrPage));
}
void *MmTransPhyToVirtAddr(void* physicalAddr)
@ -298,8 +294,6 @@ void MmSetPage(void* virtualAddr, ulong flags)
{
ulong *page = MmGetPageDescriptorFromVirtual(virtualAddr);
*page |= flags;
KeFlushTlbSingle(*page);
}
@ -310,8 +304,6 @@ void MmUnsetPage(void* virtualAddr, ulong flags)
{
ulong *page = MmGetPageDescriptorFromVirtual(virtualAddr);
*page &= (~flags);
KeFlushTlbSingle(*page);
}
@ -322,76 +314,15 @@ void MmMapPage(void* virtualAddr, void* physicalAddr, ulong flags)
{
//DebugLog("Request %p:%p with %lu\n", virtualAddr, physicalAddr, flags);
register ulong virtAddrPage;
volatile pdpe_t *pdp;
volatile pde_t *pd;
volatile pte_t *pt;
ulong *page = MmGetPageDescriptorFromVirtual(virtualAddr);
virtAddrPage = (ulong)virtualAddr & ( ~((KPAGESIZE - 1) | NX));
//DebugLog("Get virtual descriptor %p\n", virtualAddr);
while (virtAddrPage) {
pdp = (pdpe_t*)((ulong)MmPageMapLevel4[
(virtAddrPage / ((ulong)KPAGESIZE * 0x8000000)) % 512
] & ( ~(KPAGESIZE - 1)) );
//DebugLog("pdp at %p\t: %p\n", &pdp, pdp);
if (!pdp) {
KalAllocMemoryEx((void**)&pdp, 512*sizeof(pdpe_t), M_ZEROED, KPAGESIZE);
MmPageMapLevel4[
(virtAddrPage / ((ulong)KPAGESIZE * 0x8000000)) % 512
] = (pdpe_t *)((ulong)pdp | PRESENT | READWRITE);
//DebugLog("Created pdp\t: %p\n", pdp);
continue;
}
pd = (pde_t*)( (ulong)pdp[
(virtAddrPage / ((ulong)KPAGESIZE * 0x40000)) % 512
] & ( ~(KPAGESIZE - 1)) );
//DebugLog("pd at %p\t: %p\n", &pd, pd);
if (!pd) {
KalAllocMemoryEx((void**)&pd, 512*sizeof(pde_t), M_ZEROED, KPAGESIZE);
pdp[
(virtAddrPage / ((ulong)KPAGESIZE * 0x40000)) % 512
] = (pde_t *)((ulong)pd | PRESENT | READWRITE);
DebugLog("Created pd\t: %p\n", pd);
continue;
}
pt = (pte_t*)( (ulong)pd[
(virtAddrPage / ((ulong)KPAGESIZE * 0x200)) % 512
] & ( ~(KPAGESIZE - 1)) );
//DebugLog("pt at %p\t: %p\n", &pt, pt);
if (!pt) {
KalAllocMemoryEx((void**)&pt, 512*sizeof(pte_t), M_ZEROED, KPAGESIZE);
pd[
(virtAddrPage / ((ulong)KPAGESIZE * 0x200)) % 512
] = (pte_t *)((ulong)pt | PRESENT | READWRITE);
DebugLog("Created pt\t: %p\n", pt);
continue;
}
break;
}
pt[
(virtAddrPage / (ulong)KPAGESIZE) % 512
] = (ulong)physicalAddr | flags;
*page = (ulong)physicalAddr | flags;
MmPhysicalPageTable[(ulong)physicalAddr
/ ((ulong)KPAGESIZE)
] = (ulong)virtualAddr;
/ ((ulong)KPAGESIZE)
] = (ulong)virtualAddr;
KeFlushTlbSingle(
pt[
(virtAddrPage / (ulong)KPAGESIZE) % 512
] = (ulong)physicalAddr | flags
);
KeFlushTlbSingle(virtualAddr);
//DebugLog("Done %p at page %p\n", *page, page);
@ -406,13 +337,15 @@ void MmUnmapPage(void* virtualAddr)
{
ulong *page = MmGetPageDescriptorFromVirtual(virtualAddr);
MmPhysicalPageTable[(ulong)(MmTransVirtToPhyAddr(virtualAddr))
/ ((ulong)KPAGESIZE)
] = 0;
/* MmPhysicalPageTable[(ulong)(MmTransVirtToPhyAddr(virtualAddr)) */
/* / ((ulong)KPAGESIZE) */
/* ] = 0; */
*page = 0;
/* pt[ */
/* (virtualAddr / (ulong)KPAGESIZE) % 512 */
/* ] = 0; */
KeFlushTlbSingle(*page);
KeFlushTlbSingle(virtualAddr);
}
//-----------