**/\r
\r
#include "DxeMain.h"\r
+#include "Imem.h"\r
\r
#define EFI_DEFAULT_PAGE_ALLOCATION_ALIGNMENT (EFI_PAGE_SIZE)\r
\r
//\r
UINTN mMemoryMapKey = 0;\r
\r
-//\r
-// mMapStack - space to use as temp storage to build new map descriptors\r
-// mMapDepth - depth of new descriptor stack\r
-//\r
-\r
#define MAX_MAP_DEPTH 6\r
+\r
+///\r
+/// mMapDepth - depth of new descriptor stack\r
+///\r
UINTN mMapDepth = 0;\r
+///\r
+/// mMapStack - space to use as temp storage to build new map descriptors\r
+///\r
MEMORY_MAP mMapStack[MAX_MAP_DEPTH];\r
UINTN mFreeMapStack = 0;\r
-//\r
-// This list maintain the free memory map list\r
-//\r
+///\r
+/// This list maintain the free memory map list\r
+///\r
LIST_ENTRY mFreeMemoryMapEntryList = INITIALIZE_LIST_HEAD_VARIABLE (mFreeMemoryMapEntryList);\r
BOOLEAN mMemoryTypeInformationInitialized = FALSE;\r
\r
{ EfiMaxMemoryType, 0 }\r
};\r
\r
-//\r
-// Internal prototypes\r
-//\r
+\r
/**\r
- Find untested but initialized memory regions in GCD map and convert them to be DXE allocatable.\r
+ Enter critical section by gaining lock on gMemoryLock.\r
\r
**/\r
VOID\r
-PromoteMemoryResource (\r
+CoreAcquireMemoryLock (\r
VOID\r
- );\r
+ )\r
+{\r
+ CoreAcquireLock (&gMemoryLock);\r
+}\r
+\r
+\r
+\r
+/**\r
+ Exit critical section by releasing lock on gMemoryLock.\r
+\r
+**/\r
+VOID\r
+CoreReleaseMemoryLock (\r
+ VOID\r
+ )\r
+{\r
+ CoreReleaseLock (&gMemoryLock);\r
+}\r
+\r
+\r
+\r
+\r
+/**\r
+ Internal function. Removes a descriptor entry.\r
+\r
+ @param Entry The entry to remove\r
+\r
+**/\r
+VOID\r
+RemoveMemoryMapEntry (\r
+ IN OUT MEMORY_MAP *Entry\r
+ )\r
+{\r
+ RemoveEntryList (&Entry->Link);\r
+ Entry->Link.ForwardLink = NULL;\r
+\r
+ if (Entry->FromPages) {\r
+ //\r
+ // Insert the free memory map descriptor to the end of mFreeMemoryMapEntryList\r
+ //\r
+ InsertTailList (&mFreeMemoryMapEntryList, &Entry->Link);\r
+ }\r
+}\r
\r
/**\r
Internal function. Adds a ranges to the memory map.\r
IN EFI_PHYSICAL_ADDRESS Start,\r
IN EFI_PHYSICAL_ADDRESS End,\r
IN UINT64 Attribute\r
- );\r
+ )\r
+{\r
+ LIST_ENTRY *Link;\r
+ MEMORY_MAP *Entry;\r
\r
-/**\r
- Internal function. Moves any memory descriptors that are on the\r
- temporary descriptor stack to heap.\r
+ ASSERT ((Start & EFI_PAGE_MASK) == 0);\r
+ ASSERT (End > Start) ;\r
\r
-**/\r
-VOID\r
-CoreFreeMemoryMapStack (\r
- VOID\r
- );\r
+ ASSERT_LOCKED (&gMemoryLock);\r
\r
-/**\r
- Internal function. Converts a memory range to the specified type.\r
- The range must exist in the memory map.\r
+ DEBUG ((DEBUG_PAGE, "AddRange: %lx-%lx to %d\n", Start, End, Type));\r
\r
- @param Start The first address of the range Must be page\r
- aligned\r
- @param NumberOfPages The number of pages to convert\r
- @param NewType The new type for the memory range\r
+ //\r
+ // Memory map being altered so updated key\r
+ //\r
+ mMemoryMapKey += 1;\r
\r
- @retval EFI_INVALID_PARAMETER Invalid parameter\r
- @retval EFI_NOT_FOUND Could not find a descriptor cover the specified\r
- range or convertion not allowed.\r
- @retval EFI_SUCCESS Successfully converts the memory range to the\r
- specified type.\r
+ //\r
+ // UEFI 2.0 added an event group for notificaiton on memory map changes.\r
+ // So we need to signal this Event Group every time the memory map changes.\r
+ // If we are in EFI 1.10 compatability mode no event groups will be\r
+ // found and nothing will happen we we call this function. These events\r
+ // will get signaled but since a lock is held around the call to this\r
+ // function the notificaiton events will only be called after this funciton\r
+ // returns and the lock is released.\r
+ //\r
+ CoreNotifySignalList (&gEfiEventMemoryMapChangeGuid);\r
\r
-**/\r
-EFI_STATUS\r
-CoreConvertPages (\r
- IN UINT64 Start,\r
- IN UINT64 NumberOfPages,\r
- IN EFI_MEMORY_TYPE NewType\r
- );\r
+ //\r
+ // Look for adjoining memory descriptor\r
+ //\r
\r
-/**\r
- Internal function. Removes a descriptor entry.\r
+ // Two memory descriptors can only be merged if they have the same Type\r
+ // and the same Attribute\r
+ //\r
\r
- @param Entry The entry to remove\r
+ Link = gMemoryMap.ForwardLink;\r
+ while (Link != &gMemoryMap) {\r
+ Entry = CR (Link, MEMORY_MAP, Link, MEMORY_MAP_SIGNATURE);\r
+ Link = Link->ForwardLink;\r
\r
-**/\r
-VOID\r
-RemoveMemoryMapEntry (\r
- IN OUT MEMORY_MAP *Entry\r
- );\r
+ if (Entry->Type != Type) {\r
+ continue;\r
+ }\r
+\r
+ if (Entry->Attribute != Attribute) {\r
+ continue;\r
+ }\r
+\r
+ if (Entry->End + 1 == Start) {\r
+\r
+ Start = Entry->Start;\r
+ RemoveMemoryMapEntry (Entry);\r
+\r
+ } else if (Entry->Start == End + 1) {\r
+\r
+ End = Entry->End;\r
+ RemoveMemoryMapEntry (Entry);\r
+ }\r
+ }\r
+\r
+ //\r
+ // Add descriptor\r
+ //\r
+\r
+ mMapStack[mMapDepth].Signature = MEMORY_MAP_SIGNATURE;\r
+ mMapStack[mMapDepth].FromPages = FALSE;\r
+ mMapStack[mMapDepth].Type = Type;\r
+ mMapStack[mMapDepth].Start = Start;\r
+ mMapStack[mMapDepth].End = End;\r
+ mMapStack[mMapDepth].VirtualStart = 0;\r
+ mMapStack[mMapDepth].Attribute = Attribute;\r
+ InsertTailList (&gMemoryMap, &mMapStack[mMapDepth].Link);\r
+\r
+ mMapDepth += 1;\r
+ ASSERT (mMapDepth < MAX_MAP_DEPTH);\r
+\r
+ return ;\r
+}\r
\r
/**\r
Internal function. Deque a descriptor entry from the mFreeMemoryMapEntryList.\r
MEMORY_MAP *\r
AllocateMemoryMapEntry (\r
VOID\r
- );\r
+ )\r
+{\r
+ MEMORY_MAP* FreeDescriptorEntries;\r
+ MEMORY_MAP* Entry;\r
+ UINTN Index;\r
+\r
+ if (IsListEmpty (&mFreeMemoryMapEntryList)) {\r
+ //\r
+ // The list is empty, to allocate one page to refuel the list\r
+ //\r
+ FreeDescriptorEntries = CoreAllocatePoolPages (EfiBootServicesData, EFI_SIZE_TO_PAGES(DEFAULT_PAGE_ALLOCATION), DEFAULT_PAGE_ALLOCATION);\r
+ if(FreeDescriptorEntries != NULL) {\r
+ //\r
+ // Enque the free memmory map entries into the list\r
+ //\r
+ for (Index = 0; Index< DEFAULT_PAGE_ALLOCATION / sizeof(MEMORY_MAP); Index++) {\r
+ FreeDescriptorEntries[Index].Signature = MEMORY_MAP_SIGNATURE;\r
+ InsertTailList (&mFreeMemoryMapEntryList, &FreeDescriptorEntries[Index].Link);\r
+ }\r
+ } else {\r
+ return NULL;\r
+ }\r
+ }\r
+ //\r
+ // dequeue the first descriptor from the list\r
+ //\r
+ Entry = CR (mFreeMemoryMapEntryList.ForwardLink, MEMORY_MAP, Link, MEMORY_MAP_SIGNATURE);\r
+ RemoveEntryList (&Entry->Link);\r
+\r
+ return Entry;\r
+}\r
\r
\r
/**\r
- Enter critical section by gaining lock on gMemoryLock.\r
+ Internal function. Moves any memory descriptors that are on the\r
+ temporary descriptor stack to heap.\r
\r
**/\r
VOID\r
-CoreAcquireMemoryLock (\r
+CoreFreeMemoryMapStack (\r
VOID\r
)\r
{\r
- CoreAcquireLock (&gMemoryLock);\r
-}\r
+ MEMORY_MAP *Entry;\r
+ MEMORY_MAP *Entry2;\r
+ LIST_ENTRY *Link2;\r
\r
+ ASSERT_LOCKED (&gMemoryLock);\r
\r
+ //\r
+ // If already freeing the map stack, then return\r
+ //\r
+ if (mFreeMapStack != 0) {\r
+ return ;\r
+ }\r
\r
-/**\r
- Exit critical section by releasing lock on gMemoryLock.\r
+ //\r
+ // Move the temporary memory descriptor stack into pool\r
+ //\r
+ mFreeMapStack += 1;\r
\r
-**/\r
-VOID\r
-CoreReleaseMemoryLock (\r
- VOID\r
- )\r
-{\r
- CoreReleaseLock (&gMemoryLock);\r
-}\r
+ while (mMapDepth != 0) {\r
+ //\r
+ // Deque an memory map entry from mFreeMemoryMapEntryList\r
+ //\r
+ Entry = AllocateMemoryMapEntry ();\r
+\r
+ ASSERT (Entry);\r
+\r
+ //\r
+ // Update to proper entry\r
+ //\r
+ mMapDepth -= 1;\r
+\r
+ if (mMapStack[mMapDepth].Link.ForwardLink != NULL) {\r
+\r
+ //\r
+ // Move this entry to general memory\r
+ //\r
+ RemoveEntryList (&mMapStack[mMapDepth].Link);\r
+ mMapStack[mMapDepth].Link.ForwardLink = NULL;\r
+\r
+ CopyMem (Entry , &mMapStack[mMapDepth], sizeof (MEMORY_MAP));\r
+ Entry->FromPages = TRUE;\r
+\r
+ //\r
+ // Find insertion location\r
+ //\r
+ for (Link2 = gMemoryMap.ForwardLink; Link2 != &gMemoryMap; Link2 = Link2->ForwardLink) {\r
+ Entry2 = CR (Link2, MEMORY_MAP, Link, MEMORY_MAP_SIGNATURE);\r
+ if (Entry2->FromPages && Entry2->Start > Entry->Start) {\r
+ break;\r
+ }\r
+ }\r
+\r
+ InsertTailList (Link2, &Entry->Link);\r
+\r
+ } else {\r
+ //\r
+ // This item of mMapStack[mMapDepth] has already been dequeued from gMemoryMap list,\r
+ // so here no need to move it to memory.\r
+ //\r
+ InsertTailList (&mFreeMemoryMapEntryList, &Entry->Link);\r
+ }\r
+ }\r
\r
+ mFreeMapStack -= 1;\r
+}\r
\r
/**\r
Find untested but initialized memory regions in GCD map and convert them to be DXE allocatable.\r
}\r
\r
\r
-\r
-/**\r
- Internal function. Adds a ranges to the memory map.\r
- The range must not already exist in the map.\r
-\r
- @param Type The type of memory range to add\r
- @param Start The starting address in the memory range Must be\r
- paged aligned\r
- @param End The last address in the range Must be the last\r
- byte of a page\r
- @param Attribute The attributes of the memory range to add\r
-\r
-**/\r
-VOID\r
-CoreAddRange (\r
- IN EFI_MEMORY_TYPE Type,\r
- IN EFI_PHYSICAL_ADDRESS Start,\r
- IN EFI_PHYSICAL_ADDRESS End,\r
- IN UINT64 Attribute\r
- )\r
-{\r
- LIST_ENTRY *Link;\r
- MEMORY_MAP *Entry;\r
-\r
- ASSERT ((Start & EFI_PAGE_MASK) == 0);\r
- ASSERT (End > Start) ;\r
-\r
- ASSERT_LOCKED (&gMemoryLock);\r
-\r
- DEBUG ((DEBUG_PAGE, "AddRange: %lx-%lx to %d\n", Start, End, Type));\r
-\r
- //\r
- // Memory map being altered so updated key\r
- //\r
- mMemoryMapKey += 1;\r
-\r
- //\r
- // UEFI 2.0 added an event group for notificaiton on memory map changes.\r
- // So we need to signal this Event Group every time the memory map changes.\r
- // If we are in EFI 1.10 compatability mode no event groups will be\r
- // found and nothing will happen we we call this function. These events\r
- // will get signaled but since a lock is held around the call to this\r
- // function the notificaiton events will only be called after this funciton\r
- // returns and the lock is released.\r
- //\r
- CoreNotifySignalList (&gEfiEventMemoryMapChangeGuid);\r
-\r
- //\r
- // Look for adjoining memory descriptor\r
- //\r
-\r
- // Two memory descriptors can only be merged if they have the same Type\r
- // and the same Attribute\r
- //\r
-\r
- Link = gMemoryMap.ForwardLink;\r
- while (Link != &gMemoryMap) {\r
- Entry = CR (Link, MEMORY_MAP, Link, MEMORY_MAP_SIGNATURE);\r
- Link = Link->ForwardLink;\r
-\r
- if (Entry->Type != Type) {\r
- continue;\r
- }\r
-\r
- if (Entry->Attribute != Attribute) {\r
- continue;\r
- }\r
-\r
- if (Entry->End + 1 == Start) {\r
-\r
- Start = Entry->Start;\r
- RemoveMemoryMapEntry (Entry);\r
-\r
- } else if (Entry->Start == End + 1) {\r
-\r
- End = Entry->End;\r
- RemoveMemoryMapEntry (Entry);\r
- }\r
- }\r
-\r
- //\r
- // Add descriptor\r
- //\r
-\r
- mMapStack[mMapDepth].Signature = MEMORY_MAP_SIGNATURE;\r
- mMapStack[mMapDepth].FromPages = FALSE;\r
- mMapStack[mMapDepth].Type = Type;\r
- mMapStack[mMapDepth].Start = Start;\r
- mMapStack[mMapDepth].End = End;\r
- mMapStack[mMapDepth].VirtualStart = 0;\r
- mMapStack[mMapDepth].Attribute = Attribute;\r
- InsertTailList (&gMemoryMap, &mMapStack[mMapDepth].Link);\r
-\r
- mMapDepth += 1;\r
- ASSERT (mMapDepth < MAX_MAP_DEPTH);\r
-\r
- return ;\r
-}\r
-\r
-\r
-/**\r
- Internal function. Moves any memory descriptors that are on the\r
- temporary descriptor stack to heap.\r
-\r
-**/\r
-VOID\r
-CoreFreeMemoryMapStack (\r
- VOID\r
- )\r
-{\r
- MEMORY_MAP *Entry;\r
- MEMORY_MAP *Entry2;\r
- LIST_ENTRY *Link2;\r
-\r
- ASSERT_LOCKED (&gMemoryLock);\r
-\r
- //\r
- // If already freeing the map stack, then return\r
- //\r
- if (mFreeMapStack != 0) {\r
- return ;\r
- }\r
-\r
- //\r
- // Move the temporary memory descriptor stack into pool\r
- //\r
- mFreeMapStack += 1;\r
-\r
- while (mMapDepth != 0) {\r
- //\r
- // Deque an memory map entry from mFreeMemoryMapEntryList\r
- //\r
- Entry = AllocateMemoryMapEntry ();\r
-\r
- ASSERT (Entry);\r
-\r
- //\r
- // Update to proper entry\r
- //\r
- mMapDepth -= 1;\r
-\r
- if (mMapStack[mMapDepth].Link.ForwardLink != NULL) {\r
-\r
- //\r
- // Move this entry to general memory\r
- //\r
- RemoveEntryList (&mMapStack[mMapDepth].Link);\r
- mMapStack[mMapDepth].Link.ForwardLink = NULL;\r
-\r
- CopyMem (Entry , &mMapStack[mMapDepth], sizeof (MEMORY_MAP));\r
- Entry->FromPages = TRUE;\r
-\r
- //\r
- // Find insertion location\r
- //\r
- for (Link2 = gMemoryMap.ForwardLink; Link2 != &gMemoryMap; Link2 = Link2->ForwardLink) {\r
- Entry2 = CR (Link2, MEMORY_MAP, Link, MEMORY_MAP_SIGNATURE);\r
- if (Entry2->FromPages && Entry2->Start > Entry->Start) {\r
- break;\r
- }\r
- }\r
-\r
- InsertTailList (Link2, &Entry->Link);\r
-\r
- } else {\r
- //\r
- // This item of mMapStack[mMapDepth] has already been dequeued from gMemoryMap list,\r
- // so here no need to move it to memory.\r
- //\r
- InsertTailList (&mFreeMemoryMapEntryList, &Entry->Link);\r
- }\r
- }\r
-\r
- mFreeMapStack -= 1;\r
-}\r
-\r
-\r
-/**\r
- Internal function. Removes a descriptor entry.\r
-\r
- @param Entry The entry to remove\r
-\r
-**/\r
-VOID\r
-RemoveMemoryMapEntry (\r
- IN OUT MEMORY_MAP *Entry\r
- )\r
-{\r
- RemoveEntryList (&Entry->Link);\r
- Entry->Link.ForwardLink = NULL;\r
-\r
- if (Entry->FromPages) {\r
- //\r
- // Insert the free memory map descriptor to the end of mFreeMemoryMapEntryList\r
- //\r
- InsertTailList (&mFreeMemoryMapEntryList, &Entry->Link);\r
- }\r
-}\r
-\r
-\r
-/**\r
- Internal function. Deque a descriptor entry from the mFreeMemoryMapEntryList.\r
- If the list is emtry, then allocate a new page to refuel the list.\r
- Please Note this algorithm to allocate the memory map descriptor has a property\r
- that the memory allocated for memory entries always grows, and will never really be freed\r
- For example, if the current boot uses 2000 memory map entries at the maximum point, but\r
- ends up with only 50 at the time the OS is booted, then the memory associated with the 1950\r
- memory map entries is still allocated from EfiBootServicesMemory.\r
-\r
-\r
- @return The Memory map descriptor dequed from the mFreeMemoryMapEntryList\r
-\r
-**/\r
-MEMORY_MAP *\r
-AllocateMemoryMapEntry (\r
- VOID\r
- )\r
-{\r
- MEMORY_MAP* FreeDescriptorEntries;\r
- MEMORY_MAP* Entry;\r
- UINTN Index;\r
-\r
- if (IsListEmpty (&mFreeMemoryMapEntryList)) {\r
- //\r
- // The list is empty, to allocate one page to refuel the list\r
- //\r
- FreeDescriptorEntries = CoreAllocatePoolPages (EfiBootServicesData, EFI_SIZE_TO_PAGES(DEFAULT_PAGE_ALLOCATION), DEFAULT_PAGE_ALLOCATION);\r
- if(FreeDescriptorEntries != NULL) {\r
- //\r
- // Enque the free memmory map entries into the list\r
- //\r
- for (Index = 0; Index< DEFAULT_PAGE_ALLOCATION / sizeof(MEMORY_MAP); Index++) {\r
- FreeDescriptorEntries[Index].Signature = MEMORY_MAP_SIGNATURE;\r
- InsertTailList (&mFreeMemoryMapEntryList, &FreeDescriptorEntries[Index].Link);\r
- }\r
- } else {\r
- return NULL;\r
- }\r
- }\r
- //\r
- // dequeue the first descriptor from the list\r
- //\r
- Entry = CR (mFreeMemoryMapEntryList.ForwardLink, MEMORY_MAP, Link, MEMORY_MAP_SIGNATURE);\r
- RemoveEntryList (&Entry->Link);\r
-\r
- return Entry;\r
-}\r
-\r
-\r
/**\r
Internal function. Converts a memory range to the specified type.\r
The range must exist in the memory map.\r
if ((GcdMapEntry->GcdMemoryType == EfiGcdMemoryTypeReserved) ||\r
(GcdMapEntry->GcdMemoryType == EfiGcdMemoryTypeMemoryMappedIo)) {\r
if ((GcdMapEntry->Attributes & EFI_MEMORY_RUNTIME) == EFI_MEMORY_RUNTIME) {\r
-\r
+ // \r
+ // Create EFI_MEMORY_DESCRIPTOR for every Reserved and MMIO GCD entries\r
+ // that are marked for runtime use\r
+ //\r
MemoryMap->PhysicalStart = GcdMapEntry->BaseAddress;\r
MemoryMap->VirtualStart = 0;\r
MemoryMap->NumberOfPages = RShiftU64 ((GcdMapEntry->EndAddress - GcdMapEntry->BaseAddress + 1), EFI_PAGE_SHIFT);\r