[BUG] Stack smashed during paging init

This commit is contained in:
Adrien Bourmault 2020-01-17 14:09:45 +01:00
parent d99e22fe41
commit a22eb6669d
4 changed files with 36 additions and 33 deletions

View File

@ -81,9 +81,15 @@ noreturn void BtStartKern(multiboot_info_t *mbInfo, uint mbMagic, void *codeSeg)
KeGetCpuInfos(); KeGetCpuInfos();
IoEnableKeyb(); IoEnableKeyb();
for (ulong curAddrPT = 0x00971db000; curAddrPT <= 0x00971fc000; curAddrPT+= (ulong)KPAGESIZE) {
DebugLog("\t\t\t\tTable %p : %p\n", MmTransPhyToVirtAddr((void*)curAddrPT), MmTransVirtToPhyAddr((void*)curAddrPT));
}
// Command line (kernel mode) // Command line (kernel mode)
ShStartShell(); ShStartShell();
//KeCrashSystem();
// Exit ! // Exit !
PoShutdown(); PoShutdown();
} }

View File

@ -24,7 +24,7 @@
#include <kernel.h> #include <kernel.h>
ulong __stack_chk_guard = 0x447c0ffe4dbf9e55; ulong __stack_chk_guard = 0xec0ffec0ffec0ffe;
noreturn void __stack_chk_fail(void) noreturn void __stack_chk_fail(void)
{ {

View File

@ -34,8 +34,8 @@
//----------- //-----------
pml4_t MmPageMapLevel4[512] __attribute__((__aligned__(KPAGESIZE))); static pml4_t MmPageMapLevel4[512] __attribute__((__aligned__(KPAGESIZE)));
ulong *MmPhysicalPageTable; static ulong *MmPhysicalPageTable __attribute__((__aligned__(KPAGESIZE)));
extern ulong _text; extern ulong _text;
extern ulong _text_end; extern ulong _text_end;
@ -89,7 +89,7 @@ void MmInitPaging(void)
memzero((void *)&MmPageMapLevel4[0], 512*sizeof(ulong)); memzero((void *)&MmPageMapLevel4[0], 512*sizeof(ulong));
KalAllocMemoryEx((void**)&MmPhysicalPageTable, phDirSize, M_ZEROED, KPAGESIZE); KalAllocMemoryEx((void**)&MmPhysicalPageTable, phDirSize, M_ZEROED, KPAGESIZE);
DebugLog("PhDirSize : %d\n", phDirSize/sizeof(ulong)); DebugLog("\t\t\t\tPhysical map addr : %p\n", MmPhysicalPageTable);
for (curAddrPML4 = 0; for (curAddrPML4 = 0;
curAddrPML4 < 512 * KPAGESIZE * 0x8000000; curAddrPML4 < 512 * KPAGESIZE * 0x8000000;
@ -128,9 +128,6 @@ void MmInitPaging(void)
continue; continue;
} }
if (index == 0x447c0ffe4dbf9e55)
KeStartPanic("ERROR");
MmPD = (pde_t *)malloc(512*sizeof(pde_t)); MmPD = (pde_t *)malloc(512*sizeof(pde_t));
index = (curAddrPDP / ((ulong)KPAGESIZE * 0x40000)) % 512; index = (curAddrPDP / ((ulong)KPAGESIZE * 0x40000)) % 512;
@ -168,21 +165,18 @@ void MmInitPaging(void)
index = (curAddrPT / ((ulong)KPAGESIZE)) % 512; index = (curAddrPT / ((ulong)KPAGESIZE)) % 512;
xedni = (curAddrPT / ((ulong)KPAGESIZE)); xedni = (curAddrPT / ((ulong)KPAGESIZE));
if (curAddrPT == 0x973db000)
DebugLog("ERR : %p\n", &MmPhysicalPageTable[xedni]);
// 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) {
@ -207,12 +201,6 @@ void MmInitPaging(void)
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;
}
KeFlushTlbSingle(curAddrPT);
} }
} }
} }
@ -223,6 +211,11 @@ void MmInitPaging(void)
MmEnableWriteProtect(); MmEnableWriteProtect();
DebugLog("\tPage table size : %u MB\n", (lastDirectoryAddr - firstDirectoryAddr + phDirSize)/MB); DebugLog("\tPage table size : %u MB\n", (lastDirectoryAddr - firstDirectoryAddr + phDirSize)/MB);
for (ulong curAddrPT = 0x00971db000; curAddrPT <= 0x00971fc000; curAddrPT+= (ulong)KPAGESIZE) {
DebugLog("\t\t\t\tTable %p : %p\n", MmTransPhyToVirtAddr((void*)curAddrPT), MmTransVirtToPhyAddr((void*)curAddrPT));
}
DebugLog("\n");
} }
// //

View File

@ -249,24 +249,28 @@ error_t MmUnmapPageFrame(ulong id)
error_t MmTestBusyPage(void) error_t MmTestBusyPage(void)
{ {
ulong tab[2000] = {0}; for (ulong curAddrPT = 0x00971cb000; curAddrPT <= 0x00971fb000; curAddrPT+= (ulong)KPAGESIZE) {
int j = 0; DebugLog("\t\t\t\tTable %p : %p\n", MmTransPhyToVirtAddr((void*)curAddrPT), MmTransVirtToPhyAddr((void*)curAddrPT));
for (int i = 0; i < 2000; i++) {
if (rand() %2) {
if (rand() %2) {
tab[j++] = MmAllocPageFrame(rand()%65536, NORMAL);
} else {
tab[j++] = MmAllocPageFrame(rand()%65536, CONTIGUOUS);
}
} else {
MmFreePageFrame(tab[rand() % (j+1)]);
}
} }
//printBusyPages(); /* ulong tab[2000] = {0}; */
/* int j = 0; */
DebugLog("Alloc : %d; Free : %d\n", NSuccessfulAlloc, NSuccessfulFree); /* for (int i = 0; i < 2000; i++) { */
/* if (rand() %2) { */
/* if (rand() %2) { */
/* tab[j++] = MmAllocPageFrame(rand()%65536, NORMAL); */
/* } else { */
/* tab[j++] = MmAllocPageFrame(rand()%65536, CONTIGUOUS); */
/* } */
/* } else { */
/* MmFreePageFrame(tab[rand() % (j+1)]); */
/* } */
/* } */
/* //printBusyPages(); */
/* DebugLog("Alloc : %d; Free : %d\n", NSuccessfulAlloc, NSuccessfulFree); */
return EOK; return EOK;
} }