diff --git a/include/mm/map.h b/include/mm/map.h index d7c9ea7..5aa1c5e 100644 --- a/include/mm/map.h +++ b/include/mm/map.h @@ -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 // diff --git a/kaleid/kernel/mm/map.c b/kaleid/kernel/mm/map.c index 08868de..bf2fd4e 100644 --- a/kaleid/kernel/mm/map.c +++ b/kaleid/kernel/mm/map.c @@ -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; diff --git a/kaleid/kernel/mm/palloc.c b/kaleid/kernel/mm/palloc.c index 5d37d7b..2b4255e 100644 --- a/kaleid/kernel/mm/palloc.c +++ b/kaleid/kernel/mm/palloc.c @@ -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,12 +207,12 @@ 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); + addPageToBusyList(futureBegin, futureEnd, id); + + if (curNumber != pageNumber) { + return 0; + } NSuccessfulAlloc++; @@ -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; diff --git a/kaleid/kernel/sh/testcmds.c b/kaleid/kernel/sh/testcmds.c index 08cf4a7..2e2309b 100644 --- a/kaleid/kernel/sh/testcmds.c +++ b/kaleid/kernel/sh/testcmds.c @@ -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);