[BUG] Stack smashed during paging init

This commit is contained in:
Adrien Bourmault 2020-01-16 15:51:03 +01:00
parent de43801a48
commit d99e22fe41
2 changed files with 53 additions and 28 deletions

View File

@ -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

View File

@ -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;
}
//