ULONG TotalPagesInLookupTable = 0;
ULONG FreePagesInLookupTable = 0;
ULONG LastFreePageHint = 0;
+ULONG MmLowestPhysicalPage = 0xFFFFFFFF;
+ULONG MmHighestPhysicalPage = 0;
extern ULONG_PTR MmHeapPointer;
extern ULONG_PTR MmHeapStart;
BOOLEAN MmInitializeMemoryManager(VOID)
{
#if DBG
- MEMORY_DESCRIPTOR* MemoryDescriptor = NULL;
+ const MEMORY_DESCRIPTOR* MemoryDescriptor = NULL;
#endif
DPRINTM(DPRINT_MEMORY, "Initializing Memory Manager.\n");
// Find address for the page lookup table
TotalPagesInLookupTable = MmGetAddressablePageCountIncludingHoles();
PageLookupTableAddress = MmFindLocationForPageLookupTable(TotalPagesInLookupTable);
- LastFreePageHint = TotalPagesInLookupTable;
+ LastFreePageHint = MmHighestPhysicalPage;
if (PageLookupTableAddress == 0)
{
{
ULONG PagesNeeded;
ULONG HeapStart;
+#ifndef _M_ARM
MEMORY_TYPE Type;
PPAGE_LOOKUP_TABLE_ITEM RealPageLookupTable = (PPAGE_LOOKUP_TABLE_ITEM)PageLookupTable;
// HACK: Make it so it doesn't overlap kernel space
Type = RealPageLookupTable[0x100].PageAllocated;
MmMarkPagesInLookupTable(PageLookupTableAddress, 0x100, 0xFF, LoaderSystemCode);
-
+#endif
// Find contigious memory block for HEAP:STACK
PagesNeeded = HEAP_PAGES + STACK_PAGES;
HeapStart = MmFindAvailablePages(PageLookupTable, TotalPagesInLookupTable, PagesNeeded, FALSE);
-
+#ifndef _M_ARM
// Unapply the hack
MmMarkPagesInLookupTable(PageLookupTableAddress, 0x100, 0xFF, Type);
-
+#endif
if (HeapStart == 0)
{
UiMessageBox("Critical error: Can't allocate heap!");
ULONG MmGetAddressablePageCountIncludingHoles(VOID)
{
- MEMORY_DESCRIPTOR* MemoryDescriptor = NULL;
- ULONG EndPage = 0;
+ const MEMORY_DESCRIPTOR* MemoryDescriptor = NULL;
+ ULONG PageCount;
//
// Go through the whole memory map to get max address
//
// Check if we got a higher end page address
//
- if (MemoryDescriptor->BasePage + MemoryDescriptor->PageCount > EndPage)
+ if (MemoryDescriptor->BasePage + MemoryDescriptor->PageCount > MmHighestPhysicalPage)
+ {
+ //
+ // Yes, remember it if this is real memory
+ //
+ if (MemoryDescriptor->MemoryType == MemoryFree) MmHighestPhysicalPage = MemoryDescriptor->BasePage + MemoryDescriptor->PageCount;
+ }
+
+ //
+ // Check if we got a higher (usable) start page address
+ //
+ if (MemoryDescriptor->BasePage < MmLowestPhysicalPage)
{
//
- // Yes, remember it
+ // Yes, remember it if this is real memory
//
- EndPage = MemoryDescriptor->BasePage + MemoryDescriptor->PageCount;
+ if (MemoryDescriptor->MemoryType == MemoryFree) MmLowestPhysicalPage = MemoryDescriptor->BasePage;
}
}
-
- DPRINTM(DPRINT_MEMORY, "MmGetAddressablePageCountIncludingHoles() returning 0x%x\n", EndPage);
-
- return EndPage;
+
+ DPRINTM(DPRINT_MEMORY, "lo/hi %lx %lxn", MmLowestPhysicalPage, MmHighestPhysicalPage);
+ PageCount = MmHighestPhysicalPage - MmLowestPhysicalPage;
+ DPRINTM(DPRINT_MEMORY, "MmGetAddressablePageCountIncludingHoles() returning 0x%x\n", PageCount);
+ return PageCount;
}
PVOID MmFindLocationForPageLookupTable(ULONG TotalPageCount)
{
- MEMORY_DESCRIPTOR* MemoryDescriptor = NULL;
+ const MEMORY_DESCRIPTOR* MemoryDescriptor = NULL;
ULONG PageLookupTableSize;
ULONG PageLookupTablePages;
ULONG PageLookupTableStartPage = 0;
continue;
}
+ //
+ // Can we use this address?
+ //
+ if (MemoryDescriptor->BasePage >= MM_MAX_PAGE)
+ {
+ //
+ // No. Process next descriptor
+ //
+ continue;
+ }
+
//
// Memory block is more suitable than the previous one
//
VOID MmInitPageLookupTable(PVOID PageLookupTable, ULONG TotalPageCount)
{
- MEMORY_DESCRIPTOR* MemoryDescriptor = NULL;
+ const MEMORY_DESCRIPTOR* MemoryDescriptor = NULL;
TYPE_OF_MEMORY MemoryMapPageAllocated;
ULONG PageLookupTableStartPage;
ULONG PageLookupTablePageCount;
// We will go through and mark pages again according to the memory map
// But this will mark any holes not described in the map as allocated
//
- MmMarkPagesInLookupTable(PageLookupTable, 0, TotalPageCount, LoaderFirmwarePermanent);
+ MmMarkPagesInLookupTable(PageLookupTable, MmLowestPhysicalPage, TotalPageCount, LoaderFirmwarePermanent);
//
// Parse the whole memory map
case MemorySpecialMemory:
{
//
- // Special reserved memory
+ // OS Loader Stack
//
- MemoryMapPageAllocated = LoaderSpecialMemory;
+ MemoryMapPageAllocated = LoaderOsloaderStack;
break;
}
default:
PPAGE_LOOKUP_TABLE_ITEM RealPageLookupTable = (PPAGE_LOOKUP_TABLE_ITEM)PageLookupTable;
ULONG Index;
+ StartPage -= MmLowestPhysicalPage;
for (Index=StartPage; Index<(StartPage+PageCount); Index++)
{
#if 0
PPAGE_LOOKUP_TABLE_ITEM RealPageLookupTable = (PPAGE_LOOKUP_TABLE_ITEM)PageLookupTable;
ULONG Index;
+ StartPage -= MmLowestPhysicalPage;
for (Index=StartPage; Index<(StartPage+PageCount); Index++)
{
RealPageLookupTable[Index].PageAllocated = MemoryType;
if (AvailablePagesSoFar >= PagesNeeded)
{
- return Index;
+ return Index + MmLowestPhysicalPage;
}
}
}
if (AvailablePagesSoFar >= PagesNeeded)
{
- return Index - AvailablePagesSoFar + 1;
+ return Index - AvailablePagesSoFar + 1 + MmLowestPhysicalPage;
}
}
}
if (AvailablePagesSoFar >= PagesNeeded)
{
- return Index;
+ return Index + MmLowestPhysicalPage;
}
}
{
if (RealPageLookupTable[Index].PageAllocated == LoaderFree)
{
- LastFreePageHint = Index + 1;
+ LastFreePageHint = Index + 1 + MmLowestPhysicalPage;
break;
}
}
ULONG Index;
StartPage = MmGetPageNumberFromAddress(PageAddress);
+ StartPage -= MmLowestPhysicalPage;
// Make sure they aren't trying to go past the
// end of availabe memory