diff --git a/kaleid/kernel/mm/paging.c b/kaleid/kernel/mm/paging.c index ad253d6..7a42a1a 100644 --- a/kaleid/kernel/mm/paging.c +++ b/kaleid/kernel/mm/paging.c @@ -112,7 +112,8 @@ void MmInitPaging(void) } //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; curAddrPDP < (curAddrPML4 + ((ulong)KPAGESIZE * 0x8000000)); @@ -141,7 +142,8 @@ void MmInitPaging(void) // Create an intry in PD each 2MB // 0x200 = 512 - index = (curAddrPD / ((ulong)KPAGESIZE * 0x200)) % 512; + index = (curAddrPD / ((ulong)KPAGESIZE * 0x200)) + % 512; if (curAddrPD > MmPhysLastKernAddress) { MmPD[index] = (pte_t *)0; @@ -152,7 +154,8 @@ void MmInitPaging(void) MmPT = memalign(512*sizeof(pte_t), KPAGESIZE); //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; curAddrPT < (curAddrPD + ((ulong)KPAGESIZE * 0x200)); @@ -163,45 +166,50 @@ void MmInitPaging(void) xedni = (curAddrPT / ((ulong)KPAGESIZE)); // STACK GUARD PAGE */ - if ((ulong)curAddrPT == (ulong)BtLoaderInfo.stackEndAddr) { + if ((ulong)curAddrPT == + (ulong)BtLoaderInfo.stackEndAddr) { MmPT[index] = (ulong)curAddrPT | PRESENT; MmPhysicalPageTable[xedni] = (ulong)curAddrPT; MmStackGuards[0] = (ulong)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; MmPhysicalPageTable[xedni] = (ulong)curAddrPT; MmStackGuards[1] = (ulong)curAddrPT; //DebugLog("\tStack Guard at %p\n", curAddrPT); } // 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; MmPhysicalPageTable[xedni] = (ulong)curAddrPT; //DebugLog("\tSection .text at %p\n", curAddrPT); } // SECTION .DATA PROTECTION - else if ((ulong)curAddrPT >= (ulong)&_data && (ulong)curAddrPT <= (ulong)&_data_end) { - MmPT[index] = (ulong)curAddrPT | PRESENT | WRITETHR | READWRITE | NX; + else if ((ulong)curAddrPT >= (ulong)&_data + && (ulong)curAddrPT <= (ulong)&_data_end) { + MmPT[index] = (ulong)curAddrPT | PRESENT + | WRITETHR + | READWRITE + | NX; MmPhysicalPageTable[xedni] = (ulong)curAddrPT; //DebugLog("\tSection .data at %p\n", curAddrPT); } // 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; MmPhysicalPageTable[xedni] = (ulong)curAddrPT; //DebugLog("\tSection .rodata at %p\n", curAddrPT); } // While we're inside the kernel pages else if ((ulong)curAddrPT <= MmPhysLastKernAddress) { - MmPT[index] = (ulong)curAddrPT | PRESENT | READWRITE; + MmPT[index] = (ulong)curAddrPT | PRESENT + | READWRITE; MmPhysicalPageTable[xedni] = (ulong)curAddrPT; } - else { - MmPT[index] = (ulong)0; - MmPhysicalPageTable[xedni] = (ulong)0; - } } } } @@ -219,41 +227,50 @@ void MmInitPaging(void) // ulong *MmGetPageDescriptorFromVirtual(void *virtualAddr) { - 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 + // Select bit from 39 to 47 + register ulong pml4Index = ((ulong)virtualAddr & 0xFF8000000000) >> 39; + // Select bit from 30 to 39 + 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; pde_t *pd = 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 MmPageMapLevel4[pml4Index] = memalign(512*sizeof(pdpe_t), KPAGESIZE); // 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); - DebugLog("\tCreate PDP at %p\n", MmPageMapLevel4[pml4Index]); + //DebugLog("\tCreate PDP at %p\n", MmPageMapLevel4[pml4Index]); } else { 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] = (pdpe_t)((ulong)pdp[pdpIndex] | PRESENT | READWRITE); 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 { pd = (pde_t *)((ulong)pdp[pdpIndex] & 0xFFFFFFFFFF000); @@ -261,20 +278,21 @@ ulong *MmGetPageDescriptorFromVirtual(void *virtualAddr) 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] = (pde_t)((ulong)pd[pdIndex] | PRESENT | READWRITE); 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 { 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); @@ -311,7 +329,9 @@ void MmSetPage(void* virtualAddr, ulong flags) { ulong *page = MmGetPageDescriptorFromVirtual(virtualAddr); - KeFlushTlbSingle(*page); + *page |= flags; + + KeFlushTlbSingle(virtualAddr); } // @@ -321,7 +341,9 @@ void MmUnsetPage(void* virtualAddr, ulong flags) { ulong *page = MmGetPageDescriptorFromVirtual(virtualAddr); - KeFlushTlbSingle(*page); + *page |= flags; + + KeFlushTlbSingle(virtualAddr); } // diff --git a/kaleid/kernel/mm/palloc.c b/kaleid/kernel/mm/palloc.c index 31fb17a..b9e3761 100644 --- a/kaleid/kernel/mm/palloc.c +++ b/kaleid/kernel/mm/palloc.c @@ -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->id = id; newBusyPage->next = busyPage->next; @@ -142,7 +143,8 @@ static void removePageFromBusyList(void *phyPageAddr) // // 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; *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); 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)) { (*frameListPtr)[curNumber] = curPage; inBlock = true; @@ -175,7 +178,8 @@ ulong MmAllocPageFrameEx(void ***frameListPtr, size_t *pageNumber, size_t size, curNumber = 0; } } 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)) { (*frameListPtr)[curNumber] = curPage; ////DebugLog("Select page : %p\n", curPage); @@ -264,10 +268,7 @@ error_t MmUnmapPageFrame(ulong id) busyPage = busyPage->next; actualPhys = MmTransPhyToVirtAddr(busyPage->phyAddress); - ////DebugLog("Physical : %p is %p\n", busyPage->phyAddress, actualPhys); - if (actualPhys && id == busyPage->id) { - ////DebugLog("Unmap %p from %p\n", busyPage->phyAddress, MmTransPhyToVirtAddr(busyPage->phyAddress)); MmUnmapPage(MmTransPhyToVirtAddr(busyPage->phyAddress)); } } @@ -296,8 +297,7 @@ error_t MmTestBusyPage(void) ulong a = KeGetTicks(); DebugLog("Start alloc 30 MB: %lu s\n", a/1000); - tab[j++] = MmAllocPageFrame(5*MB, NORMAL); - tab[j++] = MmAllocPageFrame(5*MB, NORMAL); + tab[0] = MmAllocPageFrame(100*MB, NORMAL); ulong b = KeGetTicks(); DebugLog("End alloc : %lu s\n", b/1000); DebugLog("Alloc time : %lu s\n", (b-a)/1000); @@ -323,7 +323,7 @@ error_t MmTestBusyPage(void) a = KeGetTicks(); 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(); DebugLog("End map : %lu ms\n", b); DebugLog("Map time : %lu ms\n", (b-a));