Minor modifs

This commit is contained in:
Adrien Bourmault 2020-01-20 20:05:02 +01:00
parent 3fc80a9a28
commit 067c9cf5a8
2 changed files with 62 additions and 40 deletions

View File

@ -112,7 +112,8 @@ void MmInitPaging(void)
} }
//DebugLog("\t\t\t\tPDP %d : %p\n", index, MmPDP); //DebugLog("\t\t\t\tPDP %d : %p\n", index, MmPDP);
MmPageMapLevel4[index] = (pdpe_t *)((ulong)MmPDP | PRESENT | READWRITE); MmPageMapLevel4[index] = (pdpe_t *)((ulong)MmPDP | PRESENT
| READWRITE);
for (curAddrPDP = curAddrPML4; for (curAddrPDP = curAddrPML4;
curAddrPDP < (curAddrPML4 + ((ulong)KPAGESIZE * 0x8000000)); curAddrPDP < (curAddrPML4 + ((ulong)KPAGESIZE * 0x8000000));
@ -141,7 +142,8 @@ void MmInitPaging(void)
// Create an intry in PD each 2MB // Create an intry in PD each 2MB
// 0x200 = 512 // 0x200 = 512
index = (curAddrPD / ((ulong)KPAGESIZE * 0x200)) % 512; index = (curAddrPD / ((ulong)KPAGESIZE * 0x200))
% 512;
if (curAddrPD > MmPhysLastKernAddress) { if (curAddrPD > MmPhysLastKernAddress) {
MmPD[index] = (pte_t *)0; MmPD[index] = (pte_t *)0;
@ -152,7 +154,8 @@ void MmInitPaging(void)
MmPT = memalign(512*sizeof(pte_t), KPAGESIZE); MmPT = memalign(512*sizeof(pte_t), KPAGESIZE);
//DebugLog("\t\t\t\tPT %d : %p\n", index, MmPT); //DebugLog("\t\t\t\tPT %d : %p\n", index, MmPT);
MmPD[index] = (pte_t *)((ulong)MmPT | PRESENT | READWRITE); MmPD[index] = (pte_t *)((ulong)MmPT | PRESENT
| READWRITE);
for (curAddrPT = curAddrPD; for (curAddrPT = curAddrPD;
curAddrPT < (curAddrPD + ((ulong)KPAGESIZE * 0x200)); curAddrPT < (curAddrPD + ((ulong)KPAGESIZE * 0x200));
@ -163,45 +166,50 @@ void MmInitPaging(void)
xedni = (curAddrPT / ((ulong)KPAGESIZE)); xedni = (curAddrPT / ((ulong)KPAGESIZE));
// STACK GUARD PAGE */ // STACK GUARD PAGE */
if ((ulong)curAddrPT == (ulong)BtLoaderInfo.stackEndAddr) { if ((ulong)curAddrPT ==
(ulong)BtLoaderInfo.stackEndAddr) {
MmPT[index] = (ulong)curAddrPT | PRESENT; MmPT[index] = (ulong)curAddrPT | PRESENT;
MmPhysicalPageTable[xedni] = (ulong)curAddrPT; MmPhysicalPageTable[xedni] = (ulong)curAddrPT;
MmStackGuards[0] = (ulong)curAddrPT; MmStackGuards[0] = (ulong)curAddrPT;
//DebugLog("\tStack Guard at %p\n", curAddrPT); //DebugLog("\tStack Guard at %p\n", curAddrPT);
} }
else if ((ulong)curAddrPT == (ulong)BtLoaderInfo.kernelEndAddr) { else if ((ulong)curAddrPT ==
(ulong)BtLoaderInfo.kernelEndAddr) {
MmPT[index] = (ulong)curAddrPT | PRESENT; MmPT[index] = (ulong)curAddrPT | PRESENT;
MmPhysicalPageTable[xedni] = (ulong)curAddrPT; MmPhysicalPageTable[xedni] = (ulong)curAddrPT;
MmStackGuards[1] = (ulong)curAddrPT; MmStackGuards[1] = (ulong)curAddrPT;
//DebugLog("\tStack Guard at %p\n", curAddrPT); //DebugLog("\tStack Guard at %p\n", curAddrPT);
} }
// SECTION .TEXT PROTECTION // SECTION .TEXT PROTECTION
else if ((ulong)curAddrPT >= (ulong)&_text && (ulong)curAddrPT <= (ulong)&_text_end) { else if ((ulong)curAddrPT >= (ulong)&_text
&& (ulong)curAddrPT <= (ulong)&_text_end) {
MmPT[index] = (ulong)curAddrPT | PRESENT; MmPT[index] = (ulong)curAddrPT | PRESENT;
MmPhysicalPageTable[xedni] = (ulong)curAddrPT; MmPhysicalPageTable[xedni] = (ulong)curAddrPT;
//DebugLog("\tSection .text at %p\n", curAddrPT); //DebugLog("\tSection .text at %p\n", curAddrPT);
} }
// SECTION .DATA PROTECTION // SECTION .DATA PROTECTION
else if ((ulong)curAddrPT >= (ulong)&_data && (ulong)curAddrPT <= (ulong)&_data_end) { else if ((ulong)curAddrPT >= (ulong)&_data
MmPT[index] = (ulong)curAddrPT | PRESENT | WRITETHR | READWRITE | NX; && (ulong)curAddrPT <= (ulong)&_data_end) {
MmPT[index] = (ulong)curAddrPT | PRESENT
| WRITETHR
| READWRITE
| NX;
MmPhysicalPageTable[xedni] = (ulong)curAddrPT; MmPhysicalPageTable[xedni] = (ulong)curAddrPT;
//DebugLog("\tSection .data at %p\n", curAddrPT); //DebugLog("\tSection .data at %p\n", curAddrPT);
} }
// SECTION .RODATA PROTECTION // SECTION .RODATA PROTECTION
else if ((ulong)curAddrPT >= (ulong)&_rodata && (ulong)curAddrPT <= (ulong)&_rodata_end) { else if ((ulong)curAddrPT >= (ulong)&_rodata
&& (ulong)curAddrPT <= (ulong)&_rodata_end) {
MmPT[index] = (ulong)curAddrPT | PRESENT | NX; MmPT[index] = (ulong)curAddrPT | PRESENT | NX;
MmPhysicalPageTable[xedni] = (ulong)curAddrPT; MmPhysicalPageTable[xedni] = (ulong)curAddrPT;
//DebugLog("\tSection .rodata at %p\n", curAddrPT); //DebugLog("\tSection .rodata at %p\n", curAddrPT);
} }
// While we're inside the kernel pages // While we're inside the kernel pages
else if ((ulong)curAddrPT <= MmPhysLastKernAddress) { else if ((ulong)curAddrPT <= MmPhysLastKernAddress) {
MmPT[index] = (ulong)curAddrPT | PRESENT | READWRITE; MmPT[index] = (ulong)curAddrPT | PRESENT
| READWRITE;
MmPhysicalPageTable[xedni] = (ulong)curAddrPT; MmPhysicalPageTable[xedni] = (ulong)curAddrPT;
} }
else {
MmPT[index] = (ulong)0;
MmPhysicalPageTable[xedni] = (ulong)0;
}
} }
} }
} }
@ -219,41 +227,50 @@ void MmInitPaging(void)
// //
ulong *MmGetPageDescriptorFromVirtual(void *virtualAddr) ulong *MmGetPageDescriptorFromVirtual(void *virtualAddr)
{ {
register ulong pml4Index = ((ulong)virtualAddr & 0xFF8000000000) >> 39; // Select bit from 39 to 48 // Select bit from 39 to 47
register ulong pdpIndex = ((ulong)virtualAddr & 0x7FC0000000) >> 30; // Select bit from 39 to 48 register ulong pml4Index = ((ulong)virtualAddr & 0xFF8000000000) >> 39;
register ulong pdIndex = ((ulong)virtualAddr & 0x3FE00000) >> 21; // Select bit from 39 to 48 // Select bit from 30 to 39
register ulong ptIndex = ((ulong)virtualAddr & 0x1FF000) >> 12; // Select bit from 39 to 48 register ulong pdpIndex = ((ulong)virtualAddr & 0x7FC0000000) >> 30;
// etc etc
register ulong pdIndex = ((ulong)virtualAddr & 0x3FE00000) >> 21;
// etc
register ulong ptIndex = ((ulong)virtualAddr & 0x1FF000) >> 12;
pdpe_t *pdp = NULL; pdpe_t *pdp = NULL;
pde_t *pd = NULL; pde_t *pd = NULL;
pte_t *pt = NULL; pte_t *pt = NULL;
DebugLog("PML4[%d], PDP[%d], PD[%d], PT[%d]\n", pml4Index, pdpIndex, pdIndex, ptIndex); //DebugLog("PML4[%d], PDP[%d], PD[%d], PT[%d]\n",
// pml4Index, pdpIndex, pdIndex, ptIndex);
if (!((ulong)MmPageMapLevel4[pml4Index] & 0xFFFFFFFFFF000)) { // Select bit from 12 to 51 // Select bit from 12 to 51
if (!((ulong)MmPageMapLevel4[pml4Index] & 0xFFFFFFFFFF000)) {
// Alloc space // Alloc space
MmPageMapLevel4[pml4Index] = memalign(512*sizeof(pdpe_t), KPAGESIZE); MmPageMapLevel4[pml4Index] = memalign(512*sizeof(pdpe_t), KPAGESIZE);
// Set present // Set present
MmPageMapLevel4[pml4Index] = (pml4_t)((ulong)MmPageMapLevel4[pml4Index] | PRESENT | READWRITE); MmPageMapLevel4[pml4Index] =
(pml4_t)((ulong)MmPageMapLevel4[pml4Index] | PRESENT | READWRITE);
pdp = (pdpe_t *)((ulong)MmPageMapLevel4[pml4Index] & 0xFFFFFFFFFF000); pdp = (pdpe_t *)((ulong)MmPageMapLevel4[pml4Index] & 0xFFFFFFFFFF000);
DebugLog("\tCreate PDP at %p\n", MmPageMapLevel4[pml4Index]); //DebugLog("\tCreate PDP at %p\n", MmPageMapLevel4[pml4Index]);
} else { } else {
pdp = (pdpe_t *)((ulong)MmPageMapLevel4[pml4Index] & 0xFFFFFFFFFF000); pdp = (pdpe_t *)((ulong)MmPageMapLevel4[pml4Index] & 0xFFFFFFFFFF000);
} }
DebugLog("\tPDP[%d] = %p\n", pdpIndex, pdp[pdpIndex]); //DebugLog("\tPDP[%d] = %p\n", pdpIndex, pdp[pdpIndex]);
if (!((ulong)pdp[pdpIndex] & 0xFFFFFFFFFF000)) { // Select bit from 12 to 51 // Select bit from 12 to 51
if (!((ulong)pdp[pdpIndex] & 0xFFFFFFFFFF000)) {
pdp[pdpIndex] = memalign(512*sizeof(pde_t), KPAGESIZE); pdp[pdpIndex] = memalign(512*sizeof(pde_t), KPAGESIZE);
pdp[pdpIndex] = (pdpe_t)((ulong)pdp[pdpIndex] | PRESENT | READWRITE); pdp[pdpIndex] = (pdpe_t)((ulong)pdp[pdpIndex] | PRESENT | READWRITE);
pd = (pde_t *)((ulong)pdp[pdpIndex] & 0xFFFFFFFFFF000); pd = (pde_t *)((ulong)pdp[pdpIndex] & 0xFFFFFFFFFF000);
DebugLog("\tCreate PD at %p\n", (ulong)pdp[pdpIndex]); //DebugLog("\tCreate PD at %p\n", (ulong)pdp[pdpIndex]);
} else { } else {
pd = (pde_t *)((ulong)pdp[pdpIndex] & 0xFFFFFFFFFF000); pd = (pde_t *)((ulong)pdp[pdpIndex] & 0xFFFFFFFFFF000);
@ -261,20 +278,21 @@ ulong *MmGetPageDescriptorFromVirtual(void *virtualAddr)
DebugLog("\tPD[%d] = %p\n", pdIndex, pd[pdIndex]); DebugLog("\tPD[%d] = %p\n", pdIndex, pd[pdIndex]);
if (!((ulong)pd[pdIndex] & 0xFFFFFFFFFF000)) { // Select bit from 12 to 51 // Select bit from 12 to 51
if (!((ulong)pd[pdIndex] & 0xFFFFFFFFFF000)) {
pd[pdIndex] = memalign(512*sizeof(pte_t), KPAGESIZE); pd[pdIndex] = memalign(512*sizeof(pte_t), KPAGESIZE);
pd[pdIndex] = (pde_t)((ulong)pd[pdIndex] | PRESENT | READWRITE); pd[pdIndex] = (pde_t)((ulong)pd[pdIndex] | PRESENT | READWRITE);
pt = (pte_t *)((ulong)pd[pdIndex] & 0xFFFFFFFFFF000); pt = (pte_t *)((ulong)pd[pdIndex] & 0xFFFFFFFFFF000);
DebugLog("\tCreate PT at %p\n", (ulong)pd[pdIndex]); //DebugLog("\tCreate PT at %p\n", (ulong)pd[pdIndex]);
} else { } else {
pt = (pte_t *)((ulong)pd[pdIndex] & 0xFFFFFFFFFF000); pt = (pte_t *)((ulong)pd[pdIndex] & 0xFFFFFFFFFF000);
} }
DebugLog("\tPT[%d] = %p\n", ptIndex, pt[ptIndex]); //DebugLog("\tPT[%d] = %p\n", ptIndex, pt[ptIndex]);
MmLoadPML4((void *)MmPageMapLevel4); MmLoadPML4((void *)MmPageMapLevel4);
@ -311,7 +329,9 @@ void MmSetPage(void* virtualAddr, ulong flags)
{ {
ulong *page = MmGetPageDescriptorFromVirtual(virtualAddr); ulong *page = MmGetPageDescriptorFromVirtual(virtualAddr);
KeFlushTlbSingle(*page); *page |= flags;
KeFlushTlbSingle(virtualAddr);
} }
// //
@ -321,7 +341,9 @@ void MmUnsetPage(void* virtualAddr, ulong flags)
{ {
ulong *page = MmGetPageDescriptorFromVirtual(virtualAddr); ulong *page = MmGetPageDescriptorFromVirtual(virtualAddr);
KeFlushTlbSingle(*page); *page |= flags;
KeFlushTlbSingle(virtualAddr);
} }
// //

View File

@ -115,7 +115,8 @@ static void addPageToBusyList(void *phyPageAddr, ulong id)
} }
} }
AllocatedPage_t *newBusyPage = (AllocatedPage_t*)malloc(sizeof(AllocatedPage_t)); AllocatedPage_t *newBusyPage =
(AllocatedPage_t*)malloc(sizeof(AllocatedPage_t));
newBusyPage->phyAddress = phyPageAddr; newBusyPage->phyAddress = phyPageAddr;
newBusyPage->id = id; newBusyPage->id = id;
newBusyPage->next = busyPage->next; newBusyPage->next = busyPage->next;
@ -142,7 +143,8 @@ static void removePageFromBusyList(void *phyPageAddr)
// //
// Returns an id to identify a page frame allocated (kernel) // Returns an id to identify a page frame allocated (kernel)
// //
ulong MmAllocPageFrameEx(void ***frameListPtr, size_t *pageNumber, size_t size, bool contiguous) ulong MmAllocPageFrameEx(void ***frameListPtr, size_t *pageNumber, size_t size,
bool contiguous)
{ {
static ulong id = 0; static ulong id = 0;
*pageNumber = (((ulong)size - 1) / KPAGESIZE) + 1; *pageNumber = (((ulong)size - 1) / KPAGESIZE) + 1;
@ -159,7 +161,8 @@ ulong MmAllocPageFrameEx(void ***frameListPtr, size_t *pageNumber, size_t size,
////DebugLog("Allocating %d pages...\n", *pageNumber); ////DebugLog("Allocating %d pages...\n", *pageNumber);
if (contiguous) { if (contiguous) {
for (void *curPage = (void*)(MmPhysLastKernAddress + KPAGESIZE); curPage < (void*)phRamSize; curPage += KPAGESIZE) { for (void *curPage = (void*)(MmPhysLastKernAddress + KPAGESIZE);
curPage < (void*)phRamSize; curPage += KPAGESIZE) {
if (!isPageBusy(curPage)) { if (!isPageBusy(curPage)) {
(*frameListPtr)[curNumber] = curPage; (*frameListPtr)[curNumber] = curPage;
inBlock = true; inBlock = true;
@ -175,7 +178,8 @@ ulong MmAllocPageFrameEx(void ***frameListPtr, size_t *pageNumber, size_t size,
curNumber = 0; curNumber = 0;
} }
} else { } else {
for (void *curPage = (void*)(MmPhysLastKernAddress + KPAGESIZE); curPage < (void*)phRamSize; curPage += KPAGESIZE) { for (void *curPage = (void*)(MmPhysLastKernAddress + KPAGESIZE);
curPage < (void*)phRamSize; curPage += KPAGESIZE) {
if (!isPageBusy(curPage)) { if (!isPageBusy(curPage)) {
(*frameListPtr)[curNumber] = curPage; (*frameListPtr)[curNumber] = curPage;
////DebugLog("Select page : %p\n", curPage); ////DebugLog("Select page : %p\n", curPage);
@ -264,10 +268,7 @@ error_t MmUnmapPageFrame(ulong id)
busyPage = busyPage->next; busyPage = busyPage->next;
actualPhys = MmTransPhyToVirtAddr(busyPage->phyAddress); actualPhys = MmTransPhyToVirtAddr(busyPage->phyAddress);
////DebugLog("Physical : %p is %p\n", busyPage->phyAddress, actualPhys);
if (actualPhys && id == busyPage->id) { if (actualPhys && id == busyPage->id) {
////DebugLog("Unmap %p from %p\n", busyPage->phyAddress, MmTransPhyToVirtAddr(busyPage->phyAddress));
MmUnmapPage(MmTransPhyToVirtAddr(busyPage->phyAddress)); MmUnmapPage(MmTransPhyToVirtAddr(busyPage->phyAddress));
} }
} }
@ -296,8 +297,7 @@ error_t MmTestBusyPage(void)
ulong a = KeGetTicks(); ulong a = KeGetTicks();
DebugLog("Start alloc 30 MB: %lu s\n", a/1000); DebugLog("Start alloc 30 MB: %lu s\n", a/1000);
tab[j++] = MmAllocPageFrame(5*MB, NORMAL); tab[0] = MmAllocPageFrame(100*MB, NORMAL);
tab[j++] = MmAllocPageFrame(5*MB, NORMAL);
ulong b = KeGetTicks(); ulong b = KeGetTicks();
DebugLog("End alloc : %lu s\n", b/1000); DebugLog("End alloc : %lu s\n", b/1000);
DebugLog("Alloc time : %lu s\n", (b-a)/1000); DebugLog("Alloc time : %lu s\n", (b-a)/1000);
@ -323,7 +323,7 @@ error_t MmTestBusyPage(void)
a = KeGetTicks(); a = KeGetTicks();
DebugLog("Start map at %p: %lu ms\n", USERSPACE, a); DebugLog("Start map at %p: %lu ms\n", USERSPACE, a);
MmMapPageFrame(tab[1], (void*)(USERSPACE), PRESENT | READWRITE); MmMapPageFrame(tab[0], (void*)(USERSPACE), PRESENT | READWRITE | USERSPACE);
b = KeGetTicks(); b = KeGetTicks();
DebugLog("End map : %lu ms\n", b); DebugLog("End map : %lu ms\n", b);
DebugLog("Map time : %lu ms\n", (b-a)); DebugLog("Map time : %lu ms\n", (b-a));