1
0
mirror of https://gitlab.os-k.eu/os-k-team/os-k.git synced 2023-08-25 14:03:10 +02:00

[Bug] Problem with mapping

This commit is contained in:
Adrien Bourmault 2020-01-20 01:17:10 +01:00
parent d69e028f56
commit 8d839cd586

View File

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