Better busines computation, better size calculation and asserts

This commit is contained in:
Adrien Bourmault 2020-01-27 15:28:44 +01:00
parent eecabef0a0
commit 45bffd5cf9
4 changed files with 56 additions and 17 deletions

View File

@ -75,6 +75,11 @@ void MmPrintMemoryMap(void);
//
size_t MmGetAvailZoneSize(void *start);
//
// Returns 1 if busy
//
bool MmIsBusyZone(void *start);
//
// Returns the first available memory zone from the start address pointer
//

View File

@ -130,6 +130,32 @@ size_t MmGetAvailZoneSize(void *start)
return 0;
}
bool MmIsBusyZone(void *start)
{
uint i;
// Because the kernel is the kernel
if (start < BtLoaderInfo.stackEndAddr + 16)
return 0;
// Search the zone where the start address is
for (i = 0; i < memoryMap.length; i++) {
// if the address is in an available zone, we can return the length
if (
memoryMap.entry[i].type != AVAILABLE_ZONE &&
(ulong)start >= (ulong)memoryMap.entry[i].addr &&
(ulong)start < ((ulong)memoryMap.entry[i].addr +
(ulong)memoryMap.entry[i].length)
) {
//KernLog("Non free because %d type : %p\n", memoryMap.entry[i].type, start);
return 1;
}
}
// The zone is free
return 0;
}
bool MmIsFailingZoneSize(void *start)
{
uint i;

View File

@ -56,8 +56,9 @@ static ulong isPageBusy(void *phyPageAddr)
ulong isBusy = 0;
// In case of NVS, ACPI or BADRAM zone, considered busy
if (!MmGetAvailZoneSize(phyPageAddr))
return true;
if (MmIsBusyZone(phyPageAddr)) {
return 1;
}
// Search in the busylist if the phy addr is here
while(busyPage->next) {
@ -106,8 +107,9 @@ ulong MmGetBusyPageSize(void)
ulong MmGetTotalPageSize(void)
{
// Maximum PHYSICAL address in memory
ulong phRamSize = memoryMap.freeRamSize + memoryMap.nonfreeRamSize;
return (phRamSize - (MmPhysLastKernAddress + KPAGESIZE));
ulong phRamSize = memoryMap.freeRamSize;
return ((phRamSize & 0xFFFFFFFFFF000) -
((MmPhysLastKernAddress + KPAGESIZE) & 0xFFFFFFFFFF000));
}
static void addPageToBusyList(void *phyPageAddrBegin, void *phyPageAddrEnd, ulong id)
@ -156,10 +158,12 @@ static void removePageFromBusyList(void *phyPageAddrBegin)
//
ulong MmAllocPageFrame(size_t size, bool contiguous)
{
static ulong id = 0;
ulong pageNumber = (((ulong)size - 1) / KPAGESIZE) + 1;
assert(size);
size_t curNumber = 0;
static ulong id = 0;
ulong pageNumber = (((ulong)size) / KPAGESIZE) + 1;
ulong curNumber = 0;
void *futureBegin = 0;
void *futureEnd = 0;
@ -172,13 +176,14 @@ ulong MmAllocPageFrame(size_t size, bool contiguous)
// Maximum PHYSICAL address in memory
ulong phRamSize = memoryMap.freeRamSize + memoryMap.nonfreeRamSize;
DebugLog("Allocating %d pages...\n", pageNumber);
for (void *curPage = (void*)(MmPhysLastKernAddress + KPAGESIZE);
curPage < (void*)phRamSize; curPage += KPAGESIZE) {
DebugLog("Allocating %d pages (%u o)...\n", pageNumber, size);
// Through the pages in physical memory...
for (void *curPage = (void*)((MmPhysLastKernAddress + KPAGESIZE) & 0xFFFFFFFFFF000);
curPage < (void*)(phRamSize & 0xFFFFFFFFFF000); curPage += KPAGESIZE) {
busyLastAddr = isPageBusy(curPage);
if (!busyLastAddr) {
if (!futureBegin) {
futureBegin = curPage;
//DebugLog("Select begin : %p\n", curPage);
@ -202,13 +207,13 @@ ulong MmAllocPageFrame(size_t size, bool contiguous)
}
}
if (curNumber != pageNumber) {
KeStartPanic("MmAllocPageFrame() : No more free pages to allocate");
}
if (futureBegin && futureEnd)
addPageToBusyList(futureBegin, futureEnd, id);
if (curNumber != pageNumber) {
return 0;
}
NSuccessfulAlloc++;
return id;
@ -219,6 +224,7 @@ ulong MmAllocPageFrame(size_t size, bool contiguous)
//
void MmFreePageFrame(ulong id)
{
assert(id);
AllocatedPage_t *busyPage = &busyPagesList;
bool success = false;
@ -240,6 +246,7 @@ void MmFreePageFrame(ulong id)
//
error_t MmMapPageFrame(ulong id, void *virtAddr, ulong flags)
{
assert(id);
AllocatedPage_t *busyPage = &busyPagesList;
while(busyPage->next) {
@ -262,6 +269,7 @@ error_t MmMapPageFrame(ulong id, void *virtAddr, ulong flags)
error_t MmUnmapPageFrame(ulong id)
{
assert(id);
AllocatedPage_t *busyPage = &busyPagesList;
void *actualPhys = 0;

View File

@ -250,7 +250,7 @@ error_t CmdPageAlloc(int argc, char **argv, char *cmdline)
void *virtaddr = (void *)strtol(argv[2], NULL, 16);
ulong flags = (ulong)atol(argv[3]);
KernLog("Allocating %d o...\n", size);
KernLog("Allocating %u o...\n", size);
ulong id = MmAllocPageFrame(size, false);
KernLog("Allocated with id : %lu\n", id);