[Bug] Problem with mapping

This commit is contained in:
Adrien Bourmault 2020-01-19 18:54:33 +01:00
parent 9e033fa441
commit d69e028f56
3 changed files with 93 additions and 31 deletions

View File

@ -31,7 +31,7 @@ global newStackEnd
global GDT64
[section .text]
KERNEL_STACK equ 64 * 1024 ; 64KB of stack
KERNEL_STACK equ 16 * 1024 * 1024 ; 16MB of stack
newKernelEnd dq 0x0
newStackEnd dq 0x0

View File

@ -226,13 +226,13 @@ static ulong *MmGetPageDescriptorFromVirtual(void *virtualAddr)
volatile ulong *page;
volatile ulong index;
DebugLog("Get virtual descriptor %p\n", virtualAddr);
//DebugLog("Get virtual descriptor %p\n", virtualAddr);
while (virtualAddr) {
virtAddrPage = (ulong)virtualAddr & ( ~((KPAGESIZE - 1) | NX));
index = (virtAddrPage / ((ulong)KPAGESIZE * 0x8000000)) % 512;
pdp = (pdpe_t*)((ulong)MmPageMapLevel4[index] & ( ~(KPAGESIZE - 1)) );
DebugLog("pdp at %p\t: %p\n", &pdp, pdp);
//DebugLog("pdp at %p\t: %p\n", &pdp, pdp);
if (!pdp) {
KalAllocMemoryEx((void**)&pdp, 512*sizeof(pdpe_t), M_ZEROED, KPAGESIZE);
MmPageMapLevel4[index] = (pdpe_t *)((ulong)pdp | PRESENT | READWRITE);
@ -242,7 +242,7 @@ static ulong *MmGetPageDescriptorFromVirtual(void *virtualAddr)
index = (virtAddrPage / ((ulong)KPAGESIZE * 0x40000)) % 512;
pd = (pde_t*)( (ulong)pdp[index] & ( ~(KPAGESIZE - 1)) );
DebugLog("pd at %p\t: %p\n", &pd, pd);
//DebugLog("pd at %p\t: %p\n", &pd, pd);
if (!pd) {
KalAllocMemoryEx((void**)&pd, 512*sizeof(pde_t), M_ZEROED, KPAGESIZE);
pdp[index] = (pde_t *)((ulong)pd | PRESENT | READWRITE);
@ -320,17 +320,78 @@ void MmUnsetPage(void* virtualAddr, ulong flags)
//
void MmMapPage(void* virtualAddr, void* physicalAddr, ulong flags)
{
ulong *page = MmGetPageDescriptorFromVirtual(virtualAddr);
//DebugLog("Request %p:%p with %lu\n", virtualAddr, physicalAddr, flags);
//DebugLog("Request %p:%p with %lu, at page %p\n", virtualAddr, physicalAddr, flags, page);
register ulong virtAddrPage;
volatile pdpe_t *pdp;
volatile pde_t *pd;
volatile pte_t *pt;
*page = (ulong)physicalAddr | flags;
virtAddrPage = (ulong)virtualAddr & ( ~((KPAGESIZE - 1) | NX));
//DebugLog("Get virtual descriptor %p\n", virtualAddr);
while (virtAddrPage) {
pdp = (pdpe_t*)((ulong)MmPageMapLevel4[
(virtAddrPage / ((ulong)KPAGESIZE * 0x8000000)) % 512
] & ( ~(KPAGESIZE - 1)) );
//DebugLog("pdp at %p\t: %p\n", &pdp, pdp);
if (!pdp) {
KalAllocMemoryEx((void**)&pdp, 512*sizeof(pdpe_t), M_ZEROED, KPAGESIZE);
MmPageMapLevel4[
(virtAddrPage / ((ulong)KPAGESIZE * 0x8000000)) % 512
] = (pdpe_t *)((ulong)pdp | PRESENT | READWRITE);
//DebugLog("Created pdp\t: %p\n", pdp);
continue;
}
pd = (pde_t*)( (ulong)pdp[
(virtAddrPage / ((ulong)KPAGESIZE * 0x40000)) % 512
] & ( ~(KPAGESIZE - 1)) );
//DebugLog("pd at %p\t: %p\n", &pd, pd);
if (!pd) {
KalAllocMemoryEx((void**)&pd, 512*sizeof(pde_t), M_ZEROED, KPAGESIZE);
pdp[
(virtAddrPage / ((ulong)KPAGESIZE * 0x40000)) % 512
] = (pde_t *)((ulong)pd | PRESENT | READWRITE);
DebugLog("Created pd\t: %p\n", pd);
continue;
}
pt = (pte_t*)( (ulong)pd[
(virtAddrPage / ((ulong)KPAGESIZE * 0x200)) % 512
] & ( ~(KPAGESIZE - 1)) );
//DebugLog("pt at %p\t: %p\n", &pt, pt);
if (!pt) {
KalAllocMemoryEx((void**)&pt, 512*sizeof(pte_t), M_ZEROED, KPAGESIZE);
pd[
(virtAddrPage / ((ulong)KPAGESIZE * 0x200)) % 512
] = (pte_t *)((ulong)pt | PRESENT | READWRITE);
DebugLog("Created pt\t: %p\n", pt);
continue;
}
break;
}
pt[
(virtAddrPage / (ulong)KPAGESIZE) % 512
] = (ulong)physicalAddr | flags;
MmPhysicalPageTable[(ulong)physicalAddr
/ ((ulong)KPAGESIZE)
] = (ulong)virtualAddr;
KeFlushTlbSingle(*page);
KeFlushTlbSingle(
pt[
(virtAddrPage / (ulong)KPAGESIZE) % 512
] = (ulong)physicalAddr | flags
);
//DebugLog("Done %p at page %p\n", *page, page);

View File

@ -205,6 +205,8 @@ ulong MmAllocPageFrame(size_t size, bool contiguous)
void **ptr = NULL;
ulong d = 0;
return MmAllocPageFrameEx(&ptr, &d, size, contiguous);
(void)ptr;
(void)d;
}
@ -235,21 +237,18 @@ void MmFreePageFrame(ulong id)
error_t MmMapPageFrame(ulong id, void *virtAddr, ulong flags)
{
AllocatedPage_t *busyPage = &busyPagesList;
ulong offset = 0;
while(busyPage->next) {
busyPage = busyPage->next;
////DebugLog("Physical : %p is %p\n", busyPage->phyAddress, MmTransPhyToVirtAddr(busyPage->phyAddress));
if (MmTransPhyToVirtAddr(busyPage->phyAddress)) {
return EADDRINUSE;
}
if (id == busyPage->id) {
//DebugLog("Map %p at %p\n", busyPage->phyAddress, virtAddr + offset);
MmMapPage((void*)((ulong)virtAddr + offset), busyPage->phyAddress, flags);
offset += KPAGESIZE;
DebugLog("Map %p at %p\n", busyPage->phyAddress, virtAddr);
MmMapPage((void*)((ulong)virtAddr), busyPage->phyAddress, flags);
virtAddr += KPAGESIZE;
}
}
@ -297,36 +296,38 @@ error_t MmTestBusyPage(void)
ulong a = KeGetTicks();
DebugLog("Start alloc 30 MB: %lu s\n", a/1000);
tab[j++] = MmAllocPageFrame(30*MB, NORMAL);
tab[j++] = MmAllocPageFrame(5*MB, NORMAL);
tab[j++] = MmAllocPageFrame(8*KB, NORMAL);
ulong b = KeGetTicks();
DebugLog("End alloc : %lu s\n", b/1000);
DebugLog("Alloc time : %lu s\n", (b-a)/1000);
DebugLog("Alloc : %d; Free : %d; Count : %lu Mo\n", NSuccessfulAlloc, NSuccessfulFree, MmBusyPagesSpace() / MB);
a = KeGetTicks();
DebugLog("Start alloc 30MB : %lu s\n", a/1000);
tab[j++] = MmAllocPageFrame(5*MB, NORMAL);
b = KeGetTicks();
DebugLog("End alloc : %lu s\n", b/1000);
DebugLog("Alloc time : %lu s\n", (b-a)/1000);
DebugLog("Alloc : %d; Free : %d; Count : %lu Mo\n", NSuccessfulAlloc, NSuccessfulFree, MmBusyPagesSpace() / MB);
/* a = KeGetTicks(); */
/* DebugLog("Start alloc 30MB : %lu s\n", a/1000); */
/* tab[j++] = MmAllocPageFrame(5*MB, NORMAL); */
/* b = KeGetTicks(); */
/* DebugLog("End alloc : %lu s\n", b/1000); */
/* DebugLog("Alloc time : %lu s\n", (b-a)/1000); */
/* DebugLog("Alloc : %d; Free : %d; Count : %lu Mo\n", NSuccessfulAlloc, NSuccessfulFree, MmBusyPagesSpace() / MB); */
j = 0;
/* j = 0; */
a = KeGetTicks();
DebugLog("Start free : %lu ms\n", a);
MmFreePageFrame(tab[j++]);
b = KeGetTicks();
DebugLog("End free : %lu ms\n", b);
DebugLog("Free time : %lu ms\n", (b-a));
DebugLog("Alloc : %d; Free : %d; Count : %lu Mo\n", NSuccessfulAlloc, NSuccessfulFree, MmBusyPagesSpace() / MB);
/* a = KeGetTicks(); */
/* DebugLog("Start free : %lu ms\n", a); */
/* MmFreePageFrame(tab[j++]); */
/* b = KeGetTicks(); */
/* DebugLog("End free : %lu ms\n", b); */
/* DebugLog("Free time : %lu ms\n", (b-a)); */
/* DebugLog("Alloc : %d; Free : %d; Count : %lu Mo\n", NSuccessfulAlloc, NSuccessfulFree, MmBusyPagesSpace() / MB); */
a = KeGetTicks();
DebugLog("Start map at %p: %lu ms\n", USERSPACE, a);
MmMapPageFrame(tab[1], (void*)USERSPACE, PRESENT | READWRITE);
MmMapPageFrame(tab[1], (void*)(USERSPACE), PRESENT | READWRITE);
b = KeGetTicks();
DebugLog("End map : %lu ms\n", b);
DebugLog("Map time : %lu ms\n", (b-a));
//printBusyPages();
//DebugLog("Finished !\n");