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); size_t MmGetAvailZoneSize(void *start);
//
// Returns 1 if busy
//
bool MmIsBusyZone(void *start);
// //
// Returns the first available memory zone from the start address pointer // Returns the first available memory zone from the start address pointer
// //

View File

@ -130,6 +130,32 @@ size_t MmGetAvailZoneSize(void *start)
return 0; 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) bool MmIsFailingZoneSize(void *start)
{ {
uint i; uint i;

View File

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