Bug resolved : alignment issue

This commit is contained in:
Adrien Bourmault 2020-01-10 19:25:33 +01:00
parent 102f3f0279
commit f10f53ff08
2 changed files with 24 additions and 11 deletions

View File

@ -11,8 +11,8 @@
//-----------
static pml4_t MmPageMapLevel4[512] __attribute__((__aligned__(KPAGESIZE)));
static ulong *MmPhysicalPageTable;
pml4_t MmPageMapLevel4[512] __attribute__((__aligned__(KPAGESIZE)));
ulong *MmPhysicalPageTable;
extern ulong _text;
extern ulong _text_end;
@ -48,8 +48,7 @@ void MmInitPaging(void)
pdpe_t *MmPDP = NULL;
pde_t *MmPD = NULL;
pte_t *MmPT = NULL;
ulong index;
ulong lastKernelAddr = (ulong)(_heap_start + _heap_max);
ulong index, xedni;
ulong firstDirectoryAddr = 0;
ulong lastDirectoryAddr = 0;
ulong phDirSize = 0;
@ -58,6 +57,7 @@ void MmInitPaging(void)
ulong phRamSize = memoryMap.freeRamSize + memoryMap.nonfreeRamSize;
// Difference between the end of kernel and the begin of userspace
ulong lastKernelAddr = (ulong)(_heap_start + _heap_max);
ulong diffKernUsr = (ulong)USERSPACE - lastKernelAddr - KPAGESIZE;
// Maximum VIRTUAL address in memory
@ -66,10 +66,10 @@ void MmInitPaging(void)
//DebugLog("\tPaging gap : %u MB (%p)\n\tLast virtual address %p\n", diffKernUsr / MB, diffKernUsr, MmVirtLastAddress);
memzero((void *)&MmPageMapLevel4[0], sizeof(MmPageMapLevel4));
phDirSize = (phRamSize / KPAGESIZE)*sizeof(ulong);
phDirSize = ((phRamSize / KPAGESIZE)*sizeof(ulong) + KPAGESIZE) & ( ~(KPAGESIZE - 1));
//MmPhysicalPageTable = (ulong*)malloc((phRamSize / KPAGESIZE)*sizeof(ulong));
DebugLog("\t\tRam %u MB, pagesize %u KB, size %u MB\n", phRamSize / MB, KPAGESIZE / KB, phDirSize / MB);
MmPhysicalPageTable = (ulong*)malloc(phDirSize);
//DebugLog("\t\tRam %u MB, pagesize %u KB, size %u MB\n", phRamSize / MB, KPAGESIZE / KB, phDirSize / MB);
for (ulong curAddrPML4 = 0;
curAddrPML4 < MmVirtLastAddress;
@ -123,38 +123,45 @@ void MmInitPaging(void)
// Create an entry in PT each page of 4KB
index = (curAddrPT / ((ulong)KPAGESIZE)) % 512;
xedni = (curAddrPT / ((ulong)KPAGESIZE));
//DebugLog("\t\t\t\tPage %d : %p\n", index, curAddrPT);
// STACK GUARD PAGE */
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) {
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) {
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;
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) {
MmPT[index] = (ulong)curAddrPT | PRESENT | WRITETHR | NX;
MmPhysicalPageTable[xedni] = (ulong)curAddrPT;
//DebugLog("\tSection .rodata at %p\n", curAddrPT);
}
// While we're inside the kernel pages
else if ((ulong)curAddrPT <= lastKernelAddr) {
MmPT[index] = (ulong)curAddrPT | PRESENT | READWRITE;
MmPhysicalPageTable[xedni] = (ulong)curAddrPT;
if ((ulong)curAddrPT == lastKernelAddr) {
//DebugLog("\tLast page of kernel at %p\n", curAddrPT);
@ -163,7 +170,8 @@ void MmInitPaging(void)
// While we're inside the userspace pages
else if ((ulong)curAddrPT >= USERSPACE) {
MmPT[index] = ((ulong)curAddrPT - diffKernUsr) | PRESENT; // Not present for instance
//MmPhysicalPageTable[(ulong)curAddrPT - diffKernUsr] = curAddrPT;
xedni = (((ulong)curAddrPT - diffKernUsr) / ((ulong)KPAGESIZE));
MmPhysicalPageTable[xedni] = (ulong)curAddrPT;
if ((ulong)curAddrPT == USERSPACE) {
DebugLog("\tUserspace at %p:%p\n", curAddrPT, curAddrPT - diffKernUsr);
@ -173,7 +181,7 @@ void MmInitPaging(void)
MmPT[index] = 0;
}
//KeFlushTlbSingle(curAddrPT);
KeFlushTlbSingle(curAddrPT);
}
}
}
@ -181,7 +189,7 @@ void MmInitPaging(void)
lastDirectoryAddr = (ulong)MmPT;
MmLoadPML4((void *)MmPageMapLevel4);
MmEnableWriteProtect();
//MmEnableWriteProtect();
DebugLog("\tPage table size : %u MB\n", (lastDirectoryAddr - firstDirectoryAddr + phDirSize)/MB);
}
@ -228,7 +236,10 @@ void *MmTransVirtToPhyAddr(void* virtualAddr)
void *MmTransPhyToVirtAddr(void* physicalAddr)
{
return (void*)MmPhysicalPageTable[(ulong)physicalAddr];
ulong phyAddrPage = (ulong)physicalAddr & ( ~(KPAGESIZE - 1));
return (void*)( MmPhysicalPageTable[(ulong)physicalAddr
/ ((ulong)KPAGESIZE)
+ ((ulong)physicalAddr - phyAddrPage) ] );
}
//

View File

@ -312,6 +312,8 @@ static Command_t testcmdtable[] =
{ "div", CmdFloatDiv, "Float div. Usage : div a b. Returns a/b"},
{ "transvtp", CmdPageTranslateVirtToPhy, "Translate a virtual to"
" physical address (paging)"},
{ "transptv", CmdPageTranslatePhyToVirt, "Translate a physical to"
" virtual address (paging)"},
{ "pmap", CmdPageMap, "Map a page to given physical addr" },
{ "punmap", CmdPageUnmap, "Unmap a page" },
{ "pf", CmdPF, "Provoke a PF. Usage: pfault <address>"},