- Use loader memory types in memory allocation bitmap instead of 0 for free memory and 1 for allocated

- Slightly change / add comments

svn path=/trunk/; revision=24509
This commit is contained in:
Aleksey Bragin
2006-10-14 13:36:21 +00:00
parent 201f1d750e
commit 8e756fbebc
3 changed files with 59 additions and 27 deletions
+3 -3
View File
@@ -50,8 +50,8 @@ typedef struct
typedef struct
{
ULONG PageAllocated; // Zero = free, non-zero = allocated
ULONG PageAllocationLength; // Number of pages allocated (or zero if this isn't the first page in the chain)
TYPE_OF_MEMORY PageAllocated; // Type of allocated memory (LoaderFree if this memory is free)
ULONG PageAllocationLength; // Number of pages allocated (or zero if this isn't the first page in the chain)
} PACKED PAGE_LOOKUP_TABLE_ITEM, *PPAGE_LOOKUP_TABLE_ITEM;
//
@@ -78,7 +78,7 @@ ULONG MmGetAddressablePageCountIncludingHoles(PBIOS_MEMORY_MAP BiosMemoryMap, U
PVOID MmFindLocationForPageLookupTable(PBIOS_MEMORY_MAP BiosMemoryMap, ULONG MapCount); // Returns the address for a memory chunk big enough to hold the page lookup table (starts search from end of memory)
VOID MmSortBiosMemoryMap(PBIOS_MEMORY_MAP BiosMemoryMap, ULONG MapCount); // Sorts the BIOS_MEMORY_MAP array so the first element corresponds to the first address in memory
VOID MmInitPageLookupTable(PVOID PageLookupTable, ULONG TotalPageCount, PBIOS_MEMORY_MAP BiosMemoryMap, ULONG MapCount); // Inits the page lookup table according to the memory types in the memory map
VOID MmMarkPagesInLookupTable(PVOID PageLookupTable, ULONG StartPage, ULONG PageCount, ULONG PageAllocated); // Marks the specified pages as allocated or free in the lookup table
VOID MmMarkPagesInLookupTable(PVOID PageLookupTable, ULONG StartPage, ULONG PageCount, TYPE_OF_MEMORY PageAllocated); // Marks the specified pages as allocated or free in the lookup table
VOID MmAllocatePagesInLookupTable(PVOID PageLookupTable, ULONG StartPage, ULONG PageCount); // Allocates the specified pages in the lookup table
ULONG MmCountFreePagesInLookupTable(PVOID PageLookupTable, ULONG TotalPageCount); // Returns the number of free pages in the lookup table
ULONG MmFindAvailablePages(PVOID PageLookupTable, ULONG TotalPageCount, ULONG PagesNeeded, BOOLEAN FromEnd); // Returns the page number of the first available page range from the beginning or end of memory
+15 -13
View File
@@ -77,6 +77,7 @@ BOOLEAN MmInitializeMemoryManager(VOID)
MmFixupSystemMemoryMap(BiosMemoryMap, &BiosMemoryMapEntryCount);
}
// Find address for the page lookup table
TotalPagesInLookupTable = MmGetAddressablePageCountIncludingHoles(BiosMemoryMap, BiosMemoryMapEntryCount);
PageLookupTableAddress = MmFindLocationForPageLookupTable(BiosMemoryMap, BiosMemoryMapEntryCount);
LastFreePageHint = TotalPagesInLookupTable;
@@ -90,6 +91,7 @@ BOOLEAN MmInitializeMemoryManager(VOID)
return FALSE;
}
// Initialize the page lookup table
MmInitPageLookupTable(PageLookupTableAddress, TotalPagesInLookupTable, BiosMemoryMap, BiosMemoryMapEntryCount);
MmUpdateLastFreePageHint(PageLookupTableAddress, TotalPagesInLookupTable);
@@ -248,23 +250,23 @@ VOID MmInitPageLookupTable(PVOID PageLookupTable, ULONG TotalPageCount, PBIOS_ME
MemoryMapStartPage = MmGetPageNumberFromAddress((PVOID)(ULONG)BiosMemoryMap[Index].BaseAddress);
MemoryMapEndPage = MmGetPageNumberFromAddress((PVOID)(ULONG)(BiosMemoryMap[Index].BaseAddress + BiosMemoryMap[Index].Length - 1));
MemoryMapPageCount = (MemoryMapEndPage - MemoryMapStartPage) + 1;
MemoryMapPageAllocated = (BiosMemoryMap[Index].Type == BiosMemoryUsable) ? 0 : BiosMemoryMap[Index].Type;
MemoryMapPageAllocated = (BiosMemoryMap[Index].Type == BiosMemoryUsable) ? LoaderFree : LoaderFirmwarePermanent;/*BiosMemoryMap[Index].Type*/;
DbgPrint((DPRINT_MEMORY, "Marking pages as type %d: StartPage: %d PageCount: %d\n", MemoryMapPageAllocated, MemoryMapStartPage, MemoryMapPageCount));
MmMarkPagesInLookupTable(PageLookupTable, MemoryMapStartPage, MemoryMapPageCount, MemoryMapPageAllocated);
}
// Mark the low memory region below 1MB as reserved (256 pages in region)
DbgPrint((DPRINT_MEMORY, "Marking the low 1MB region as reserved.\n"));
MmMarkPagesInLookupTable(PageLookupTable, 0, 256, BiosMemoryReserved);
MmMarkPagesInLookupTable(PageLookupTable, 0, 256, LoaderFirmwarePermanent);
// Mark the pages that the lookup tabel occupies as reserved
// Mark the pages that the lookup table occupies as reserved
PageLookupTableStartPage = MmGetPageNumberFromAddress(PageLookupTable);
PageLookupTablePageCount = MmGetPageNumberFromAddress((PVOID)((ULONG_PTR)PageLookupTable + ROUND_UP(TotalPageCount * sizeof(PAGE_LOOKUP_TABLE_ITEM), MM_PAGE_SIZE))) - PageLookupTableStartPage;
DbgPrint((DPRINT_MEMORY, "Marking the page lookup table pages as reserved StartPage: %d PageCount: %d\n", PageLookupTableStartPage, PageLookupTablePageCount));
MmMarkPagesInLookupTable(PageLookupTable, PageLookupTableStartPage, PageLookupTablePageCount, BiosMemoryReserved);
MmMarkPagesInLookupTable(PageLookupTable, PageLookupTableStartPage, PageLookupTablePageCount, LoaderFirmwareTemporary);
}
VOID MmMarkPagesInLookupTable(PVOID PageLookupTable, ULONG StartPage, ULONG PageCount, ULONG PageAllocated)
VOID MmMarkPagesInLookupTable(PVOID PageLookupTable, ULONG StartPage, ULONG PageCount, TYPE_OF_MEMORY PageAllocated)
{
PPAGE_LOOKUP_TABLE_ITEM RealPageLookupTable = (PPAGE_LOOKUP_TABLE_ITEM)PageLookupTable;
ULONG Index;
@@ -276,7 +278,7 @@ VOID MmMarkPagesInLookupTable(PVOID PageLookupTable, ULONG StartPage, ULONG Page
DbgPrint((DPRINT_MEMORY, "Index = %d StartPage = %d PageCount = %d\n", Index, StartPage, PageCount));
}
RealPageLookupTable[Index].PageAllocated = PageAllocated;
RealPageLookupTable[Index].PageAllocationLength = PageAllocated ? 1 : 0;
RealPageLookupTable[Index].PageAllocationLength = (PageAllocated != LoaderFree) ? 1 : 0;
}
DbgPrint((DPRINT_MEMORY, "MmMarkPagesInLookupTable() Done\n"));
}
@@ -288,7 +290,7 @@ VOID MmAllocatePagesInLookupTable(PVOID PageLookupTable, ULONG StartPage, ULONG
for (Index=StartPage; Index<(StartPage+PageCount); Index++)
{
RealPageLookupTable[Index].PageAllocated = 1;
RealPageLookupTable[Index].PageAllocated = LoaderSystemCode;
RealPageLookupTable[Index].PageAllocationLength = (Index == StartPage) ? PageCount : 0;
}
}
@@ -302,7 +304,7 @@ ULONG MmCountFreePagesInLookupTable(PVOID PageLookupTable, ULONG TotalPageCount)
FreePageCount = 0;
for (Index=0; Index<TotalPageCount; Index++)
{
if (RealPageLookupTable[Index].PageAllocated == 0)
if (RealPageLookupTable[Index].PageAllocated == LoaderFree)
{
FreePageCount++;
}
@@ -328,7 +330,7 @@ ULONG MmFindAvailablePages(PVOID PageLookupTable, ULONG TotalPageCount, ULONG Pa
/* Allocate "high" (from end) pages */
for (Index=LastFreePageHint-1; Index>0; Index--)
{
if (RealPageLookupTable[Index].PageAllocated != 0)
if (RealPageLookupTable[Index].PageAllocated != LoaderFree)
{
AvailablePagesSoFar = 0;
continue;
@@ -350,7 +352,7 @@ ULONG MmFindAvailablePages(PVOID PageLookupTable, ULONG TotalPageCount, ULONG Pa
/* Allocate "low" pages */
for (Index=1; Index < LastFreePageHint; Index++)
{
if (RealPageLookupTable[Index].PageAllocated != 0)
if (RealPageLookupTable[Index].PageAllocated != LoaderFree)
{
AvailablePagesSoFar = 0;
continue;
@@ -384,7 +386,7 @@ ULONG MmFindAvailablePagesBeforePage(PVOID PageLookupTable, ULONG TotalPageCount
AvailablePagesSoFar = 0;
for (Index=LastPage-1; Index>0; Index--)
{
if (RealPageLookupTable[Index].PageAllocated != 0)
if (RealPageLookupTable[Index].PageAllocated != LoaderFree)
{
AvailablePagesSoFar = 0;
continue;
@@ -434,7 +436,7 @@ VOID MmUpdateLastFreePageHint(PVOID PageLookupTable, ULONG TotalPageCount)
for (Index=TotalPageCount-1; Index>0; Index--)
{
if (RealPageLookupTable[Index].PageAllocated == 0)
if (RealPageLookupTable[Index].PageAllocated == LoaderFree)
{
LastFreePageHint = Index + 1;
break;
@@ -461,7 +463,7 @@ BOOLEAN MmAreMemoryPagesAvailable(PVOID PageLookupTable, ULONG TotalPageCount, P
{
// If this page is allocated then there obviously isn't
// memory availabe so return FALSE
if (RealPageLookupTable[Index].PageAllocated != 0)
if (RealPageLookupTable[Index].PageAllocated != LoaderFree)
{
return FALSE;
}
+41 -11
View File
@@ -271,7 +271,7 @@ VOID MmFreeMemory(PVOID MemoryPointer)
// to make sure they are allocated with a length of 0
for (Idx=PageNumber+1; Idx<(PageNumber + PageCount); Idx++)
{
if ((RealPageLookupTable[Idx].PageAllocated != 1) ||
if ((RealPageLookupTable[Idx].PageAllocated == LoaderFree) ||
(RealPageLookupTable[Idx].PageAllocationLength != 0))
{
BugCheck((DPRINT_MEMORY, "Invalid page entry in lookup table, PageAllocated should = 1 and PageAllocationLength should = 0 because this is not the first block in the run. PageLookupTable[%d].PageAllocated = %d PageLookupTable[%d].PageAllocationLength = %d\n", PageNumber, RealPageLookupTable[PageNumber].PageAllocated, PageNumber, RealPageLookupTable[PageNumber].PageAllocationLength));
@@ -291,7 +291,7 @@ VOID MmFreeMemory(PVOID MemoryPointer)
// blocks as free
for (Idx=PageNumber; Idx<(PageNumber + PageCount); Idx++)
{
RealPageLookupTable[Idx].PageAllocated = 0;
RealPageLookupTable[Idx].PageAllocated = LoaderFree;
RealPageLookupTable[Idx].PageAllocationLength = 0;
}
@@ -322,7 +322,7 @@ VOID VerifyHeap(VOID)
for (Idx=0; Idx<TotalPagesInLookupTable; Idx++)
{
// Check if this block is allocated
if (RealPageLookupTable[Idx].PageAllocated != 0)
if (RealPageLookupTable[Idx].PageAllocated != LoaderFree)
{
// This is the first block in the run so it
// had better have a length that is within range
@@ -339,7 +339,7 @@ VOID VerifyHeap(VOID)
for (Idx2=1; Idx2<Count; Idx2++)
{
// Make sure it's allocated
if (RealPageLookupTable[Idx + Idx2].PageAllocated == 0)
if (RealPageLookupTable[Idx + Idx2].PageAllocated == LoaderFree)
{
BugCheck((DPRINT_MEMORY, "Lookup table indicates hole in memory allocation. RealPageLookupTable[Idx + Idx2].PageAllocated == 0\n"));
}
@@ -386,23 +386,53 @@ VOID DumpMemoryAllocMap(VOID)
switch (RealPageLookupTable[Idx].PageAllocated)
{
case 0:
case LoaderFree:
DbgPrint((DPRINT_MEMORY, "*"));
break;
case 1:
DbgPrint((DPRINT_MEMORY, "A"));
case LoaderBad:
DbgPrint((DPRINT_MEMORY, "-"));
break;
case BiosMemoryReserved:
case LoaderLoadedProgram:
DbgPrint((DPRINT_MEMORY, "O"));
break;
case LoaderFirmwareTemporary:
DbgPrint((DPRINT_MEMORY, "T"));
break;
case LoaderFirmwarePermanent:
DbgPrint((DPRINT_MEMORY, "P"));
break;
case LoaderOsloaderHeap:
DbgPrint((DPRINT_MEMORY, "H"));
break;
case LoaderOsloaderStack:
DbgPrint((DPRINT_MEMORY, "S"));
break;
case LoaderSystemCode:
DbgPrint((DPRINT_MEMORY, "K"));
break;
case LoaderHalCode:
DbgPrint((DPRINT_MEMORY, "L"));
break;
case LoaderBootDriver:
DbgPrint((DPRINT_MEMORY, "B"));
break;
case LoaderStartupPcrPage:
DbgPrint((DPRINT_MEMORY, "G"));
break;
case LoaderRegistryData:
DbgPrint((DPRINT_MEMORY, "R"));
break;
case BiosMemoryAcpiReclaim:
case LoaderMemoryData:
DbgPrint((DPRINT_MEMORY, "M"));
break;
case BiosMemoryAcpiNvs:
case LoaderNlsData:
DbgPrint((DPRINT_MEMORY, "N"));
break;
case LoaderSpecialMemory:
DbgPrint((DPRINT_MEMORY, "C"));
break;
default:
DbgPrint((DPRINT_MEMORY, "X"));
DbgPrint((DPRINT_MEMORY, "?"));
break;
}
}