Page allocator fully functionnal #67
This commit is contained in:
parent
69b0a65a0f
commit
7a7d458bc6
|
@ -49,6 +49,19 @@ typedef pde_t* pdpe_t;
|
||||||
// Page directory L4 pointer offset
|
// Page directory L4 pointer offset
|
||||||
typedef pdpe_t* pml4_t;
|
typedef pdpe_t* pml4_t;
|
||||||
|
|
||||||
|
enum
|
||||||
|
{
|
||||||
|
PRESENT = 1 << 0,
|
||||||
|
READWRITE = 1 << 1,
|
||||||
|
USERMODE = 1 << 2,
|
||||||
|
WRITETHR = 1 << 3,
|
||||||
|
CACHEDIS = 1 << 4,
|
||||||
|
ACCESSED = 1 << 5,
|
||||||
|
DIRTY = 1 << 6,
|
||||||
|
HUGE = 1 << 7,
|
||||||
|
NX = 1UL << 63
|
||||||
|
};
|
||||||
|
|
||||||
//----------------------------------------------------------------------------//
|
//----------------------------------------------------------------------------//
|
||||||
|
|
||||||
//
|
//
|
||||||
|
|
|
@ -50,19 +50,6 @@ static ulong MmStackGuards[2] = { 0 };
|
||||||
ulong MmVirtLastAddress = 0;
|
ulong MmVirtLastAddress = 0;
|
||||||
ulong MmPhysLastKernAddress = 0;
|
ulong MmPhysLastKernAddress = 0;
|
||||||
|
|
||||||
enum
|
|
||||||
{
|
|
||||||
PRESENT = 1 << 0,
|
|
||||||
READWRITE = 1 << 1,
|
|
||||||
USERMODE = 1 << 2,
|
|
||||||
WRITETHR = 1 << 3,
|
|
||||||
CACHEDIS = 1 << 4,
|
|
||||||
ACCESSED = 1 << 5,
|
|
||||||
DIRTY = 1 << 6,
|
|
||||||
HUGE = 1 << 7,
|
|
||||||
NX = 1UL << 63
|
|
||||||
};
|
|
||||||
|
|
||||||
//-----------
|
//-----------
|
||||||
|
|
||||||
//
|
//
|
||||||
|
@ -302,6 +289,10 @@ void MmMapPage(void* virtualAddr, void* physicalAddr, ulong flags)
|
||||||
|
|
||||||
*page = ((ulong)physicalAddr & ~((KPAGESIZE - 1) | NX)) | flags;
|
*page = ((ulong)physicalAddr & ~((KPAGESIZE - 1) | NX)) | flags;
|
||||||
|
|
||||||
|
MmPhysicalPageTable[(ulong)physicalAddr
|
||||||
|
/ ((ulong)KPAGESIZE)
|
||||||
|
] = (ulong)virtualAddr;
|
||||||
|
|
||||||
KeFlushTlbSingle(*page);
|
KeFlushTlbSingle(*page);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -312,6 +303,10 @@ void MmUnmapPage(void* virtualAddr)
|
||||||
{
|
{
|
||||||
pte_t *page = MmGetPageDescriptorFromVirtual(virtualAddr);
|
pte_t *page = MmGetPageDescriptorFromVirtual(virtualAddr);
|
||||||
|
|
||||||
|
MmPhysicalPageTable[(ulong)(MmTransVirtToPhyAddr(virtualAddr))
|
||||||
|
/ ((ulong)KPAGESIZE)
|
||||||
|
] = 0;
|
||||||
|
|
||||||
*page = 0;
|
*page = 0;
|
||||||
|
|
||||||
KeFlushTlbSingle(*page);
|
KeFlushTlbSingle(*page);
|
||||||
|
|
|
@ -81,14 +81,22 @@ static void printBusyPages(void)
|
||||||
static void addPageToBusyList(void *phyPageAddr, ulong id)
|
static void addPageToBusyList(void *phyPageAddr, ulong id)
|
||||||
{
|
{
|
||||||
AllocatedPage_t *busyPage = &busyPagesList;
|
AllocatedPage_t *busyPage = &busyPagesList;
|
||||||
|
AllocatedPage_t *prevBusyPage = NULL;
|
||||||
|
|
||||||
while(busyPage->next) {
|
while(busyPage->next) {
|
||||||
|
prevBusyPage = busyPage;
|
||||||
busyPage = busyPage->next;
|
busyPage = busyPage->next;
|
||||||
|
|
||||||
|
if (busyPage->phyAddress > phyPageAddr) {
|
||||||
|
busyPage = prevBusyPage;
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
AllocatedPage_t *newBusyPage = (AllocatedPage_t*)malloc(sizeof(AllocatedPage_t));
|
AllocatedPage_t *newBusyPage = (AllocatedPage_t*)malloc(sizeof(AllocatedPage_t));
|
||||||
newBusyPage->phyAddress = phyPageAddr;
|
newBusyPage->phyAddress = phyPageAddr;
|
||||||
newBusyPage->id = id;
|
newBusyPage->id = id;
|
||||||
|
newBusyPage->next = busyPage->next;
|
||||||
busyPage->next = newBusyPage;
|
busyPage->next = newBusyPage;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -129,7 +137,7 @@ ulong MmAllocPageFrame(void ***frameListPtr, size_t *pageNumber, size_t size, bo
|
||||||
|
|
||||||
DebugLog("Allocating %d pages...\n", *pageNumber);
|
DebugLog("Allocating %d pages...\n", *pageNumber);
|
||||||
|
|
||||||
for (void *curPage = (void*)MmPhysLastKernAddress; curPage < (void*)phRamSize; curPage += KPAGESIZE) {
|
for (void *curPage = (void*)(MmPhysLastKernAddress + KPAGESIZE); curPage < (void*)phRamSize; curPage += KPAGESIZE) {
|
||||||
if (!isPageBusy(curPage)) {
|
if (!isPageBusy(curPage)) {
|
||||||
(*frameListPtr)[curNumber] = curPage;
|
(*frameListPtr)[curNumber] = curPage;
|
||||||
inBlock = true;
|
inBlock = true;
|
||||||
|
@ -169,6 +177,53 @@ void MmFreePageFrame(ulong id)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
// Maps an allocated page frame to the given address
|
||||||
|
//
|
||||||
|
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) {
|
||||||
|
MmMapPage((void*)((ulong)virtAddr + offset), busyPage->phyAddress, flags);
|
||||||
|
DebugLog("Map %p at %p\n", busyPage->phyAddress, virtAddr + offset);
|
||||||
|
offset += KPAGESIZE;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return EOK;
|
||||||
|
}
|
||||||
|
|
||||||
|
error_t MmUnmapPageFrame(ulong id)
|
||||||
|
{
|
||||||
|
AllocatedPage_t *busyPage = &busyPagesList;
|
||||||
|
void *actualPhys = 0;
|
||||||
|
|
||||||
|
while(busyPage->next) {
|
||||||
|
busyPage = busyPage->next;
|
||||||
|
actualPhys = MmTransPhyToVirtAddr(busyPage->phyAddress);
|
||||||
|
|
||||||
|
//DebugLog("Physical : %p is %p\n", busyPage->phyAddress, actualPhys);
|
||||||
|
|
||||||
|
if (actualPhys && id == busyPage->id) {
|
||||||
|
DebugLog("Unmap %p from %p\n", busyPage->phyAddress, MmTransPhyToVirtAddr(busyPage->phyAddress));
|
||||||
|
MmUnmapPage(MmTransPhyToVirtAddr(busyPage->phyAddress));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return EOK;
|
||||||
|
}
|
||||||
|
|
||||||
error_t MmTestBusyPage(void)
|
error_t MmTestBusyPage(void)
|
||||||
{
|
{
|
||||||
DebugLog("\nBusy pages\n");
|
DebugLog("\nBusy pages\n");
|
||||||
|
@ -178,49 +233,49 @@ error_t MmTestBusyPage(void)
|
||||||
void **ptr = NULL;
|
void **ptr = NULL;
|
||||||
size_t n = 0;
|
size_t n = 0;
|
||||||
ulong id1 = MmAllocPageFrame(&ptr, &n, 6677, NORMAL);
|
ulong id1 = MmAllocPageFrame(&ptr, &n, 6677, NORMAL);
|
||||||
DebugLog("\nAlloc 6677 bytes : %p, %d pages, first at %p\n", ptr, n, ptr[0]);
|
DebugLog("Alloc 6677 bytes : %p, %d pages, first at %p\n", ptr, n, ptr[0]);
|
||||||
|
|
||||||
void **ptr2 = NULL;
|
void **ptr2 = NULL;
|
||||||
size_t n2 = 0;
|
size_t n2 = 0;
|
||||||
ulong id2 = MmAllocPageFrame(&ptr2, &n2, 9045, NORMAL);
|
ulong id2 = MmAllocPageFrame(&ptr2, &n2, 9045, NORMAL);
|
||||||
DebugLog("\nAlloc 9045 bytes: %p, %d pages, first at %p\n", ptr2, n2, ptr2[0]);
|
DebugLog("Alloc 9045 bytes: %p, %d pages, first at %p\n", ptr2, n2, ptr2[0]);
|
||||||
|
|
||||||
void **ptr3 = NULL;
|
void **ptr3 = NULL;
|
||||||
size_t n3 = 0;
|
size_t n3 = 0;
|
||||||
ulong id3 = MmAllocPageFrame(&ptr3, &n3, 1200, NORMAL);
|
ulong id3 = MmAllocPageFrame(&ptr3, &n3, 1200, NORMAL);
|
||||||
DebugLog("\nAlloc 1200 bytes: %p, %d pages, first at %p\n", ptr3, n3, ptr3[0]);
|
DebugLog("Alloc 1200 bytes: %p, %d pages, first at %p\n", ptr3, n3, ptr3[0]);
|
||||||
|
|
||||||
void **ptr4 = NULL;
|
void **ptr4 = NULL;
|
||||||
size_t n4 = 0;
|
size_t n4 = 0;
|
||||||
ulong id4 = MmAllocPageFrame(&ptr4, &n4, 4096, NORMAL);
|
ulong id4 = MmAllocPageFrame(&ptr4, &n4, 4096, NORMAL);
|
||||||
DebugLog("\nAlloc 4096 bytes: %p, %d pages, first at %p\n", ptr4, n4, ptr4[0]);
|
DebugLog("Alloc 4096 bytes: %p, %d pages, first at %p\n", ptr4, n4, ptr4[0]);
|
||||||
void **ptr5 = NULL;
|
void **ptr5 = NULL;
|
||||||
size_t n5 = 0;
|
size_t n5 = 0;
|
||||||
ulong id5 = MmAllocPageFrame(&ptr5, &n5, 4097, NORMAL);
|
ulong id5 = MmAllocPageFrame(&ptr5, &n5, 4097, NORMAL);
|
||||||
DebugLog("\nAlloc 4097 bytes: %p, %d pages, first at %p\n", ptr5, n5, ptr5[0]);
|
DebugLog("Alloc 4097 bytes: %p, %d pages, first at %p\n", ptr5, n5, ptr5[0]);
|
||||||
|
|
||||||
printBusyPages();
|
printBusyPages();
|
||||||
|
|
||||||
MmFreePageFrame(id1);
|
MmFreePageFrame(id1);
|
||||||
MmFreePageFrame(id3);
|
MmFreePageFrame(id3);
|
||||||
DebugLog("\nFree 6677 and 1200 bytes\n");
|
DebugLog("Free 6677 and 1200 bytes\n");
|
||||||
|
|
||||||
void **ptr6 = NULL;
|
void **ptr6 = NULL;
|
||||||
size_t n6 = 0;
|
size_t n6 = 0;
|
||||||
ulong id6 = MmAllocPageFrame(&ptr6, &n6, 10000, NORMAL);
|
ulong id6 = MmAllocPageFrame(&ptr6, &n6, 10000, NORMAL);
|
||||||
DebugLog("\nAlloc 10000 bytes: %p, %d pages, first at %p\n", ptr6, n6, ptr6[0]);
|
DebugLog("Alloc 10000 bytes: %p, %d pages, first at %p\n", ptr6, n6, ptr6[0]);
|
||||||
|
|
||||||
printBusyPages();
|
printBusyPages();
|
||||||
|
|
||||||
MmFreePageFrame(id6);
|
MmFreePageFrame(id6);
|
||||||
DebugLog("\nFree 10000 bytes\n");
|
DebugLog("Free 10000 bytes\n");
|
||||||
|
|
||||||
printBusyPages();
|
printBusyPages();
|
||||||
|
|
||||||
void **ptr7 = NULL;
|
void **ptr7 = NULL;
|
||||||
size_t n7 = 0;
|
size_t n7 = 0;
|
||||||
ulong id7 = MmAllocPageFrame(&ptr7, &n7, 10000, CONTIGUOUS);
|
ulong id7 = MmAllocPageFrame(&ptr7, &n7, 10000, CONTIGUOUS);
|
||||||
DebugLog("\nAlloc 10000 bytes contiguous: %p, %d pages, first at %p\n", ptr7, n7, ptr7[0]);
|
DebugLog("Alloc 10000 bytes contiguous: %p, %d pages, first at %p\n", ptr7, n7, ptr7[0]);
|
||||||
|
|
||||||
printBusyPages();
|
printBusyPages();
|
||||||
|
|
||||||
|
@ -231,9 +286,19 @@ error_t MmTestBusyPage(void)
|
||||||
MmFreePageFrame(id5);
|
MmFreePageFrame(id5);
|
||||||
MmFreePageFrame(id6);
|
MmFreePageFrame(id6);
|
||||||
MmFreePageFrame(id7);
|
MmFreePageFrame(id7);
|
||||||
DebugLog("\nFree all bytes\n");
|
DebugLog("Free all bytes\n");
|
||||||
|
|
||||||
printBusyPages();
|
printBusyPages();
|
||||||
|
|
||||||
|
id1 = MmAllocPageFrame(&ptr, &n, 1*MB, NORMAL);
|
||||||
|
error_t err = MmMapPageFrame(id1, (void*)USERSPACE, PRESENT | USERMODE | READWRITE);
|
||||||
|
|
||||||
|
if (err == EOK)
|
||||||
|
DebugLog("Map status : OK\n");
|
||||||
|
if (err == EADDRINUSE)
|
||||||
|
DebugLog("Map status : NOK\n");
|
||||||
|
|
||||||
|
MmUnmapPageFrame(id1);
|
||||||
|
|
||||||
return EOK;
|
return EOK;
|
||||||
}
|
}
|
||||||
|
|
|
@ -220,19 +220,6 @@ error_t CmdPageTranslatePhyToVirt(int argc, char **argv, char *cmdline)
|
||||||
return EOK;
|
return EOK;
|
||||||
}
|
}
|
||||||
|
|
||||||
enum
|
|
||||||
{
|
|
||||||
PRESENT = 1 << 0,
|
|
||||||
READWRITE = 1 << 1,
|
|
||||||
USERMODE = 1 << 2,
|
|
||||||
WRITETHR = 1 << 3,
|
|
||||||
CACHEDIS = 1 << 4,
|
|
||||||
ACCESSED = 1 << 5,
|
|
||||||
DIRTY = 1 << 6,
|
|
||||||
HUGE = 1 << 7,
|
|
||||||
NX = 1UL << 63
|
|
||||||
};
|
|
||||||
|
|
||||||
error_t CmdPageMap(int argc, char **argv, char *cmdline)
|
error_t CmdPageMap(int argc, char **argv, char *cmdline)
|
||||||
{
|
{
|
||||||
void *virtual = (void*)strtoul(argv[1], NULL, 16);
|
void *virtual = (void*)strtoul(argv[1], NULL, 16);
|
||||||
|
|
Loading…
Reference in New Issue