]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdeModulePkg/Core/Dxe/Mem/Page.c
Clean up to update the reference of the these macros:
[mirror_edk2.git] / MdeModulePkg / Core / Dxe / Mem / Page.c
index aeaa48da80f4357bec5b1b9cf99fd5856518fb53..fa54e7f0e63f9b6e7b37e6a75bfe42d87feaf83b 100644 (file)
@@ -13,6 +13,7 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
 **/\r
 \r
 #include "DxeMain.h"\r
+#include "Imem.h"\r
 \r
 #define EFI_DEFAULT_PAGE_ALLOCATION_ALIGNMENT  (EFI_PAGE_SIZE)\r
 \r
@@ -34,40 +35,42 @@ typedef struct {
 //\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
 EFI_MEMORY_TYPE_STAISTICS mMemoryTypeStatistics[EfiMaxMemoryType + 1] = {\r
-  { 0, EFI_MAX_ADDRESS, 0, 0, EfiMaxMemoryType, TRUE,  FALSE },  // EfiReservedMemoryType\r
-  { 0, EFI_MAX_ADDRESS, 0, 0, EfiMaxMemoryType, FALSE, FALSE },  // EfiLoaderCode\r
-  { 0, EFI_MAX_ADDRESS, 0, 0, EfiMaxMemoryType, FALSE, FALSE },  // EfiLoaderData\r
-  { 0, EFI_MAX_ADDRESS, 0, 0, EfiMaxMemoryType, FALSE, FALSE },  // EfiBootServicesCode\r
-  { 0, EFI_MAX_ADDRESS, 0, 0, EfiMaxMemoryType, FALSE, FALSE },  // EfiBootServicesData\r
-  { 0, EFI_MAX_ADDRESS, 0, 0, EfiMaxMemoryType, TRUE,  TRUE  },  // EfiRuntimeServicesCode\r
-  { 0, EFI_MAX_ADDRESS, 0, 0, EfiMaxMemoryType, TRUE,  TRUE  },  // EfiRuntimeServicesData\r
-  { 0, EFI_MAX_ADDRESS, 0, 0, EfiMaxMemoryType, FALSE, FALSE },  // EfiConventionalMemory\r
-  { 0, EFI_MAX_ADDRESS, 0, 0, EfiMaxMemoryType, FALSE, FALSE },  // EfiUnusableMemory\r
-  { 0, EFI_MAX_ADDRESS, 0, 0, EfiMaxMemoryType, TRUE,  FALSE },  // EfiACPIReclaimMemory\r
-  { 0, EFI_MAX_ADDRESS, 0, 0, EfiMaxMemoryType, TRUE,  FALSE },  // EfiACPIMemoryNVS\r
-  { 0, EFI_MAX_ADDRESS, 0, 0, EfiMaxMemoryType, FALSE, FALSE },  // EfiMemoryMappedIO\r
-  { 0, EFI_MAX_ADDRESS, 0, 0, EfiMaxMemoryType, FALSE, FALSE },  // EfiMemoryMappedIOPortSpace\r
-  { 0, EFI_MAX_ADDRESS, 0, 0, EfiMaxMemoryType, TRUE,  TRUE  },  // EfiPalCode\r
-  { 0, EFI_MAX_ADDRESS, 0, 0, EfiMaxMemoryType, FALSE, FALSE }   // EfiMaxMemoryType\r
+  { 0, MAX_ADDRESS, 0, 0, EfiMaxMemoryType, TRUE,  FALSE },  // EfiReservedMemoryType\r
+  { 0, MAX_ADDRESS, 0, 0, EfiMaxMemoryType, FALSE, FALSE },  // EfiLoaderCode\r
+  { 0, MAX_ADDRESS, 0, 0, EfiMaxMemoryType, FALSE, FALSE },  // EfiLoaderData\r
+  { 0, MAX_ADDRESS, 0, 0, EfiMaxMemoryType, FALSE, FALSE },  // EfiBootServicesCode\r
+  { 0, MAX_ADDRESS, 0, 0, EfiMaxMemoryType, FALSE, FALSE },  // EfiBootServicesData\r
+  { 0, MAX_ADDRESS, 0, 0, EfiMaxMemoryType, TRUE,  TRUE  },  // EfiRuntimeServicesCode\r
+  { 0, MAX_ADDRESS, 0, 0, EfiMaxMemoryType, TRUE,  TRUE  },  // EfiRuntimeServicesData\r
+  { 0, MAX_ADDRESS, 0, 0, EfiMaxMemoryType, FALSE, FALSE },  // EfiConventionalMemory\r
+  { 0, MAX_ADDRESS, 0, 0, EfiMaxMemoryType, FALSE, FALSE },  // EfiUnusableMemory\r
+  { 0, MAX_ADDRESS, 0, 0, EfiMaxMemoryType, TRUE,  FALSE },  // EfiACPIReclaimMemory\r
+  { 0, MAX_ADDRESS, 0, 0, EfiMaxMemoryType, TRUE,  FALSE },  // EfiACPIMemoryNVS\r
+  { 0, MAX_ADDRESS, 0, 0, EfiMaxMemoryType, FALSE, FALSE },  // EfiMemoryMappedIO\r
+  { 0, MAX_ADDRESS, 0, 0, EfiMaxMemoryType, FALSE, FALSE },  // EfiMemoryMappedIOPortSpace\r
+  { 0, MAX_ADDRESS, 0, 0, EfiMaxMemoryType, TRUE,  TRUE  },  // EfiPalCode\r
+  { 0, MAX_ADDRESS, 0, 0, EfiMaxMemoryType, FALSE, FALSE }   // EfiMaxMemoryType\r
 };\r
 \r
-EFI_PHYSICAL_ADDRESS mDefaultMaximumAddress = EFI_MAX_ADDRESS;\r
+EFI_PHYSICAL_ADDRESS mDefaultMaximumAddress = MAX_ADDRESS;\r
 \r
 EFI_MEMORY_TYPE_INFORMATION gMemoryTypeInformation[EfiMaxMemoryType + 1] = {\r
   { EfiReservedMemoryType,      0 },\r
@@ -87,17 +90,57 @@ EFI_MEMORY_TYPE_INFORMATION gMemoryTypeInformation[EfiMaxMemoryType + 1] = {
   { 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
@@ -117,51 +160,85 @@ CoreAddRange (
   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
@@ -179,35 +256,114 @@ RemoveMemoryMapEntry (
 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
@@ -231,7 +387,7 @@ PromoteMemoryResource (
     Entry = CR (Link, EFI_GCD_MAP_ENTRY, Link, EFI_GCD_MAP_SIGNATURE);\r
 \r
     if (Entry->GcdMemoryType == EfiGcdMemoryTypeReserved &&\r
-        Entry->EndAddress < EFI_MAX_ADDRESS &&\r
+        Entry->EndAddress < MAX_ADDRESS &&\r
         (Entry->Capabilities & (EFI_MEMORY_PRESENT | EFI_MEMORY_INITIALIZED | EFI_MEMORY_TESTED)) ==\r
           (EFI_MEMORY_PRESENT | EFI_MEMORY_INITIALIZED)) {\r
       //\r
@@ -357,7 +513,7 @@ CoreAddMemoryDescriptor (
               gMemoryTypeInformation[FreeIndex].NumberOfPages\r
               );\r
             mMemoryTypeStatistics[Type].BaseAddress    = 0;\r
-            mMemoryTypeStatistics[Type].MaximumAddress = EFI_MAX_ADDRESS;\r
+            mMemoryTypeStatistics[Type].MaximumAddress = MAX_ADDRESS;\r
           }\r
         }\r
         return;\r
@@ -415,7 +571,7 @@ CoreAddMemoryDescriptor (
       }\r
     }\r
     mMemoryTypeStatistics[Type].CurrentNumberOfPages = 0;\r
-    if (mMemoryTypeStatistics[Type].MaximumAddress == EFI_MAX_ADDRESS) {\r
+    if (mMemoryTypeStatistics[Type].MaximumAddress == MAX_ADDRESS) {\r
       mMemoryTypeStatistics[Type].MaximumAddress = mDefaultMaximumAddress;\r
     }\r
   }\r
@@ -424,255 +580,6 @@ CoreAddMemoryDescriptor (
 }\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
@@ -1112,7 +1019,7 @@ CoreAllocatePages (
   //\r
   // The max address is the max natively addressable address for the processor\r
   //\r
-  MaxAddress = EFI_MAX_ADDRESS;\r
+  MaxAddress = MAX_ADDRESS;\r
 \r
   if (Type == AllocateMaxAddress) {\r
     MaxAddress = Start;\r
@@ -1220,7 +1127,7 @@ CoreFreePages (
   //\r
   // Destroy the contents\r
   //\r
-  if (Memory < EFI_MAX_ADDRESS) {\r
+  if (Memory < MAX_ADDRESS) {\r
     DEBUG_CLEAR_MEMORY ((VOID *)(UINTN)Memory, NumberOfPages << EFI_PAGE_SHIFT);\r
   }\r
 \r
@@ -1374,7 +1281,7 @@ CoreGetMemoryMap (
       MemoryMap->Attribute |= EFI_MEMORY_RUNTIME;\r
     }\r
 \r
-    MemoryMap = NextMemoryDescriptor (MemoryMap, Size);\r
+    MemoryMap = NEXT_MEMORY_DESCRIPTOR (MemoryMap, Size);\r
   }\r
 \r
   for (Link = mGcdMemorySpaceMap.ForwardLink; Link != &mGcdMemorySpaceMap; Link = Link->ForwardLink) {\r
@@ -1382,7 +1289,10 @@ CoreGetMemoryMap (
     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
@@ -1398,7 +1308,7 @@ CoreGetMemoryMap (
           }\r
         }\r
 \r
-        MemoryMap = NextMemoryDescriptor (MemoryMap, Size);\r
+        MemoryMap = NEXT_MEMORY_DESCRIPTOR (MemoryMap, Size);\r
       }\r
     }\r
   }\r
@@ -1447,7 +1357,7 @@ CoreAllocatePoolPages (
   //\r
   // Find the pages to convert\r
   //\r
-  Start = FindFreePages (EFI_MAX_ADDRESS, NumberOfPages, PoolType, Alignment);\r
+  Start = FindFreePages (MAX_ADDRESS, NumberOfPages, PoolType, Alignment);\r
 \r
   //\r
   // Convert it to boot services data\r
@@ -1514,18 +1424,18 @@ CoreTerminateMemoryMap (
 \r
     for (Link = gMemoryMap.ForwardLink; Link != &gMemoryMap; Link = Link->ForwardLink) {\r
       Entry = CR(Link, MEMORY_MAP, Link, MEMORY_MAP_SIGNATURE);\r
-      if (Entry->Attribute & EFI_MEMORY_RUNTIME) {\r
+      if ((Entry->Attribute & EFI_MEMORY_RUNTIME) != 0) {\r
         if (Entry->Type == EfiACPIReclaimMemory || Entry->Type == EfiACPIMemoryNVS) {\r
           DEBUG((DEBUG_ERROR | DEBUG_PAGE, "ExitBootServices: ACPI memory entry has RUNTIME attribute set.\n"));\r
           Status =  EFI_INVALID_PARAMETER;\r
           goto Done;\r
         }\r
-        if (Entry->Start & (EFI_ACPI_RUNTIME_PAGE_ALLOCATION_ALIGNMENT - 1)) {\r
+        if ((Entry->Start & (EFI_ACPI_RUNTIME_PAGE_ALLOCATION_ALIGNMENT - 1)) != 0) {\r
           DEBUG((DEBUG_ERROR | DEBUG_PAGE, "ExitBootServices: A RUNTIME memory entry is not on a proper alignment.\n"));\r
           Status =  EFI_INVALID_PARAMETER;\r
           goto Done;\r
         }\r
-        if ((Entry->End + 1) & (EFI_ACPI_RUNTIME_PAGE_ALLOCATION_ALIGNMENT - 1)) {\r
+        if (((Entry->End + 1) & (EFI_ACPI_RUNTIME_PAGE_ALLOCATION_ALIGNMENT - 1)) != 0) {\r
           DEBUG((DEBUG_ERROR | DEBUG_PAGE, "ExitBootServices: A RUNTIME memory entry is not on a proper alignment.\n"));\r
           Status =  EFI_INVALID_PARAMETER;\r
           goto Done;\r