From d99e22fe41c8bb2159052901e69a6d9b87dc24b2 Mon Sep 17 00:00:00 2001 From: Adrien Bourmault Date: Thu, 16 Jan 2020 15:51:03 +0100 Subject: [PATCH] =?UTF-8?q?[BUG]=C2=A0Stack=20smashed=20during=20paging=20?= =?UTF-8?q?init?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Makefile | 2 +- kaleid/kernel/mm/paging.c | 79 ++++++++++++++++++++++++++------------- 2 files changed, 53 insertions(+), 28 deletions(-) diff --git a/Makefile b/Makefile index 5e2b97f..bb5f2a7 100644 --- a/Makefile +++ b/Makefile @@ -46,7 +46,7 @@ CINCLUDES=-Iinclude CFLAGS1=-nostdlib -ffreestanding -mcmodel=large -std=gnu11 -fstack-protector-all -fdump-rtl-expand CFLAGS2= -c -mno-red-zone -mno-mmx -mno-sse -mno-sse2 CFLAGS= $(CFLAGS1) $(CFLAGS2) -CFLAGS_MATHS= $(CFLAGS1) -c -mno-red-zone -mno-mmx -mno-sse2 +CFLAGS_MATHS= $(CFLAGS1) -c -mno-red-zone -mno-mmx ifeq ($(mode), release) CFLAGS += -D_NO_DEBUG diff --git a/kaleid/kernel/mm/paging.c b/kaleid/kernel/mm/paging.c index 108911d..6a9bd1d 100644 --- a/kaleid/kernel/mm/paging.c +++ b/kaleid/kernel/mm/paging.c @@ -60,7 +60,12 @@ void MmInitPaging(void) pdpe_t *MmPDP = NULL; pde_t *MmPD = NULL; pte_t *MmPT = NULL; - register ulong index, xedni; + ulong index, xedni; + ulong curAddrPML4; + ulong curAddrPDP; + ulong curAddrPD; + ulong curAddrPT; + ulong firstDirectoryAddr = 0; ulong lastDirectoryAddr = 0; ulong phDirSize = 0; @@ -82,32 +87,50 @@ void MmInitPaging(void) // Alloc structures memzero((void *)&MmPageMapLevel4[0], 512*sizeof(ulong)); - KalAllocMemoryEx(&MmPhysicalPageTable, phDirSize, M_ZEROED, KPAGESIZE); + KalAllocMemoryEx((void**)&MmPhysicalPageTable, phDirSize, M_ZEROED, KPAGESIZE); - for (register ulong curAddrPML4 = 0; - curAddrPML4 < phRamSize; + DebugLog("PhDirSize : %d\n", phDirSize/sizeof(ulong)); + + for (curAddrPML4 = 0; + curAddrPML4 < 512 * KPAGESIZE * 0x8000000; curAddrPML4 += ((ulong)KPAGESIZE * 0x8000000)) { // Create an entry in PML4 each 512GB // 0x8000000 = 512 ^ 3 + index = (curAddrPML4 / ((ulong)KPAGESIZE * 0x8000000)) % 512; + + if (curAddrPML4 > phRamSize) { + MmPageMapLevel4[index] = (pdpe_t *)0; + //DebugLog("PML4 %d\n", index); + continue; + } + MmPDP = (pdpe_t *)malloc(512*sizeof(pde_t)); if (!firstDirectoryAddr) { firstDirectoryAddr = (ulong)MmPDP; } - index = (curAddrPML4 / ((ulong)KPAGESIZE * 0x8000000)) % 512; - //DebugLog("\t\t\t\tPDP %d : %p\n", index, MmPDP); MmPageMapLevel4[index] = (pdpe_t *)((ulong)MmPDP | PRESENT | READWRITE); - for (register ulong curAddrPDP = curAddrPML4; - curAddrPDP < (curAddrPML4 + ((ulong)KPAGESIZE * 0x8000000)) && - curAddrPDP < phRamSize; + for (curAddrPDP = curAddrPML4; + curAddrPDP < (curAddrPML4 + ((ulong)KPAGESIZE * 0x8000000)); curAddrPDP += ((ulong)KPAGESIZE * 0x40000)) { // Create an intry in PDP each 1GB // 0x40000 = 512 ^ 2 + index = (curAddrPDP / ((ulong)KPAGESIZE * 0x40000)) % 512; + + if (curAddrPDP > phRamSize) { + MmPDP[index] = (pde_t *)0; + //DebugLog("PDP %d\n", index); + continue; + } + + if (index == 0x447c0ffe4dbf9e55) + KeStartPanic("ERROR"); + MmPD = (pde_t *)malloc(512*sizeof(pde_t)); index = (curAddrPDP / ((ulong)KPAGESIZE * 0x40000)) % 512; @@ -115,23 +138,30 @@ void MmInitPaging(void) //DebugLog("\t\t\t\tPD %d : %p\n", index, MmPD); MmPDP[index] = (pde_t *)((ulong)MmPD | PRESENT | READWRITE); - for (register ulong curAddrPD = curAddrPDP; - curAddrPD < (curAddrPDP + ((ulong)KPAGESIZE * 0x40000)) && - curAddrPD < phRamSize; + for (curAddrPD = curAddrPDP; + curAddrPD < (curAddrPDP + ((ulong)KPAGESIZE * 0x40000)); curAddrPD += ((ulong)KPAGESIZE * 0x200)) { // Create an intry in PD each 2MB // 0x200 = 512 - MmPT = (pte_t *)malloc(512*sizeof(pte_t)); - index = (curAddrPD / ((ulong)KPAGESIZE * 0x200)) % 512; + if (curAddrPD > phRamSize) { + MmPD[index] = (pte_t *)0; + //DebugLog("PD %d\n", index); + continue; + } + + if (index == 0x447c0ffe4dbf9e55) + KeStartPanic("ERROR"); + + MmPT = (pte_t *)malloc(512*sizeof(pte_t)); + //DebugLog("\t\t\t\tPT %d : %p\n", index, MmPT); MmPD[index] = (pte_t *)((ulong)MmPT | PRESENT | READWRITE); - for (register ulong curAddrPT = curAddrPD; - curAddrPT < (curAddrPD + ((ulong)KPAGESIZE * 0x200)) && - curAddrPT < phRamSize; + for (curAddrPT = curAddrPD; + curAddrPT < (curAddrPD + ((ulong)KPAGESIZE * 0x200)); curAddrPT += (ulong)KPAGESIZE) { // Create an entry in PT each page of 4KB @@ -139,7 +169,7 @@ void MmInitPaging(void) xedni = (curAddrPT / ((ulong)KPAGESIZE)); if (curAddrPT == 0x973db000) - DebugLog("\t\t\t\tPage %d : %p\n", index, curAddrPT); + DebugLog("ERR : %p\n", &MmPhysicalPageTable[xedni]); // STACK GUARD PAGE */ if ((ulong)curAddrPT == (ulong)BtLoaderInfo.stackEndAddr) { @@ -176,18 +206,13 @@ void MmInitPaging(void) else if ((ulong)curAddrPT <= MmPhysLastKernAddress) { MmPT[index] = (ulong)curAddrPT | PRESENT | READWRITE; MmPhysicalPageTable[xedni] = (ulong)curAddrPT; - - if ((ulong)curAddrPT == MmPhysLastKernAddress) { - //DebugLog("\tLast page of kernel at %p\n", curAddrPT); - } } else { - MmPT[index] = 0; - MmPhysicalPageTable[xedni] = 0; + MmPT[index] = (ulong)0; + MmPhysicalPageTable[xedni] = (ulong)0; } KeFlushTlbSingle(curAddrPT); - asm (""); } } } @@ -287,8 +312,8 @@ void MmMapPage(void* virtualAddr, void* physicalAddr, ulong flags) KeFlushTlbSingle(*page); - if (virtualAddr > MmVirtLastAddress) - MmVirtLastAddress = virtualAddr; + if ((ulong)virtualAddr > MmVirtLastAddress) + MmVirtLastAddress = (ulong)virtualAddr; } //