Page allocator functionnal #67

This commit is contained in:
Adrien Bourmault 2020-01-14 16:54:47 +01:00
parent 1de0d05ad2
commit 2df15a1488
2 changed files with 126 additions and 36 deletions

View File

@ -46,7 +46,7 @@ extern ulong _data_end;
extern MemoryMap_t memoryMap; extern MemoryMap_t memoryMap;
ulong MmStackGuards[2] = { 0 }; static ulong MmStackGuards[2] = { 0 };
ulong MmVirtLastAddress = 0; ulong MmVirtLastAddress = 0;
ulong MmPhysLastKernAddress = 0; ulong MmPhysLastKernAddress = 0;

View File

@ -27,6 +27,7 @@
#include <ex/malloc.h> #include <ex/malloc.h>
#include <mm/paging.h> #include <mm/paging.h>
#include <mm/palloc.h> #include <mm/palloc.h>
#include <mm/map.h>
#include <io/vga.h> #include <io/vga.h>
//--------- //---------
@ -38,27 +39,37 @@ enum
}; };
static AllocatedPage_t busyPagesList = { (void*)0, 0, (AllocatedPage_t*)0 }; static AllocatedPage_t busyPagesList = { (void*)0, 0, (AllocatedPage_t*)0 };
extern MemoryMap_t memoryMap;
extern ulong MmPhysLastKernAddress;
//--------- //---------
static bool isPageBusy(void *phyPageAddr) { static bool isPageBusy(void *phyPageAddr)
{
AllocatedPage_t *busyPage = &busyPagesList; AllocatedPage_t *busyPage = &busyPagesList;
bool isBusy = false; bool isBusy = false;
while(busyPage->next) { while(busyPage->next) {
busyPage = busyPage->next; busyPage = busyPage->next;
DebugLog("Busy page at %p\n", busyPage->phyAddress);
if (phyPageAddr == busyPage->phyAddress) { if (phyPageAddr == busyPage->phyAddress) {
isBusy = true; isBusy = true;
break; break;
} }
} }
return isBusy; return isBusy;
} }
static void addPageToBusyList(void *phyPageAddr, ulong id) { static void printBusyPages(void)
{
AllocatedPage_t *busyPage = &busyPagesList;
while(busyPage->next) {
busyPage = busyPage->next;
KernLog("Busy page at %p\n", busyPage->phyAddress);
}
}
static void addPageToBusyList(void *phyPageAddr, ulong id)
{
AllocatedPage_t *busyPage = &busyPagesList; AllocatedPage_t *busyPage = &busyPagesList;
while(busyPage->next) { while(busyPage->next) {
@ -71,7 +82,8 @@ static void addPageToBusyList(void *phyPageAddr, ulong id) {
busyPage->next = newBusyPage; busyPage->next = newBusyPage;
} }
static void removePageFromBusyList(void *phyPageAddr) { static void removePageFromBusyList(void *phyPageAddr)
{
AllocatedPage_t *busyPage = &busyPagesList; AllocatedPage_t *busyPage = &busyPagesList;
AllocatedPage_t *prevBusyPage = NULL; AllocatedPage_t *prevBusyPage = NULL;
@ -88,38 +100,116 @@ static void removePageFromBusyList(void *phyPageAddr) {
} }
// //
// Returns a structure that describes a pageframe // Returns an id to identify a page frame allocated
// //
error_t MmGetFreePageFrame(void **framePtr, size_t *pageNumber, size_t size) { ulong MmAllocKernelPageFrame(void **frameListPtr, size_t *pageNumber, size_t size, bool contiguous)
*pageNumber = (ulong)size / KPAGESIZE; {
static ulong id = 0;
*pageNumber = (((ulong)size - 1) / KPAGESIZE) + 1;
frameListPtr = (void**)malloc(sizeof(void*)*(*pageNumber));
size_t curNumber = 0;
bool inBlock = false;
// Incrementing id
return EOK; id++;
}
// Maximum PHYSICAL address in memory
error_t MmTestBusyPage(void) { ulong phRamSize = memoryMap.freeRamSize + memoryMap.nonfreeRamSize;
DebugLog("Adding pages \n"); DebugLog("Allocating %d pages...\n", *pageNumber);
addPageToBusyList((void*)0x123456789, 56);
addPageToBusyList((void*)0x555666666, 69); for (void *curPage = (void*)MmPhysLastKernAddress; curPage < (void*)phRamSize; curPage += KPAGESIZE) {
addPageToBusyList((void*)0x454545454, 5); if (!isPageBusy(curPage)) {
frameListPtr[curNumber] = curPage;
DebugLog("\nVerifying busy 1\n"); inBlock = true;
isPageBusy((void*)0x123456789); //DebugLog("Select page : %p\n", curPage);
DebugLog("\nVerifying busy 2\n"); if (++curNumber >= *pageNumber) {
isPageBusy((void*)0); break;
DebugLog("\nVerifying busy 3\n"); }
isPageBusy((void*)0x1234567894); } else {
inBlock = false;
DebugLog("\nRemoving first\n"); }
removePageFromBusyList((void*)0x123456789); if (contiguous)
isPageBusy((void*)0x1234567894); if (!inBlock)
DebugLog("\nRemoving others\n"); curNumber = 0;
removePageFromBusyList((void*)0x555666666); }
removePageFromBusyList((void*)0x454545454);
for (size_t i = 0; i < *pageNumber; i++) {
isPageBusy((void*)0x1234567894); addPageToBusyList(frameListPtr[i], id);
//DebugLog("Allocating page : %p\n", frameListPtr[i]);
}
return id;
}
void MmFreeKernelPageFrame(ulong id)
{
AllocatedPage_t *busyPage = &busyPagesList;
while(busyPage->next) {
busyPage = busyPage->next;
if (id == busyPage->id) {
removePageFromBusyList(busyPage->phyAddress);
}
}
}
error_t MmTestBusyPage(void)
{
DebugLog("\nBusy pages\n");
printBusyPages();
DebugLog("\nAlloc 6677 bytes\n");
void **ptr = NULL;
size_t n = 0;
ulong id1 = MmAllocKernelPageFrame (ptr, &n, 6677, false);
DebugLog("\nAlloc 9045 bytes\n");
void **ptr2 = NULL;
size_t n2 = 0;
ulong id2 = MmAllocKernelPageFrame (ptr2, &n2, 9045, false);
DebugLog("\nAlloc 1200 bytes\n");
void **ptr3 = NULL;
size_t n3 = 0;
ulong id3 = MmAllocKernelPageFrame (ptr3, &n3, 1200, false);
DebugLog("\nAlloc 4096 bytes\n");
void **ptr4 = NULL;
size_t n4 = 0;
ulong id4 = MmAllocKernelPageFrame (ptr3, &n3, 4096, false);
DebugLog("\nAlloc 4097 bytes\n");
void **ptr5 = NULL;
size_t n5 = 0;
ulong id5 = MmAllocKernelPageFrame (ptr3, &n3, 4097, false);
printBusyPages();
DebugLog("\nFree 6677 and 1200 bytes\n");
MmFreeKernelPageFrame(id1);
MmFreeKernelPageFrame(id3);
DebugLog("\nAlloc 10000 bytes\n");
void **ptr6 = NULL;
size_t n6 = 0;
ulong id6 = MmAllocKernelPageFrame (ptr3, &n3, 10000, false);
printBusyPages();
DebugLog("\nFree 10000 bytes\n");
MmFreeKernelPageFrame(id6);
printBusyPages();
DebugLog("\nAlloc 10000 bytes contiguous\n");
void **ptr7 = NULL;
size_t n7 = 0;
ulong id7 = MmAllocKernelPageFrame (ptr3, &n3, 10000, true);
printBusyPages();
return EOK; return EOK;
} }