CoreInitializeMemoryServices() will essentially pickup a piece of memory out
of *any* memory descriptor HOB completely ignoring the memory allocation HOBs.
This change considers memory allocation hobs in this API.

v2 adds check the memory range in the resource HOB that includes PHIT range to
make sure the free memory range not include the memory allocation hob.

CC: Benjamin Herrenschmidt <b...@kernel.crashing.org>
Contributed-under: TianoCore Contribution Agreement 1.0
Signed-off-by: Liming Gao <liming....@intel.com>
---
 MdeModulePkg/Core/Dxe/Gcd/Gcd.c | 47 ++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 46 insertions(+), 1 deletion(-)

diff --git a/MdeModulePkg/Core/Dxe/Gcd/Gcd.c b/MdeModulePkg/Core/Dxe/Gcd/Gcd.c
index a50fda2..9d7c2df 100644
--- a/MdeModulePkg/Core/Dxe/Gcd/Gcd.c
+++ b/MdeModulePkg/Core/Dxe/Gcd/Gcd.c
@@ -2001,10 +2001,41 @@ 
CoreConvertResourceDescriptorHobAttributesToCapabilities (
   }
 
   return Capabilities;
 }
 
+/**
+  Check whether the memory allocation hob is in the specified memory range.
+  
+  @param  HobStart          The start address of the HOB.
+  @param  MemoryBaseAddress Start address of the memory region.
+  @param  MemoryLength      Length of the memory region.
+
+  @retval TRUE             Memory allocation hob is in the specified memory 
range.
+  @retval FALSE            No memory allocation hob is in the specified memory 
range.
+
+**/
+BOOLEAN
+OverlapWithMemoryAllocationHob (
+  IN  VOID                            **HobStart,
+  IN  EFI_PHYSICAL_ADDRESS            MemoryAddress,
+  IN  UINT64                          MemoryLength
+  )
+{
+  EFI_PEI_HOB_POINTERS               Hob;
+
+  for (Hob.Raw = *HobStart; !END_OF_HOB_LIST(Hob); Hob.Raw = 
GET_NEXT_HOB(Hob)) {
+    if (GET_HOB_TYPE (Hob) == EFI_HOB_TYPE_MEMORY_ALLOCATION) {
+      if (Hob.MemoryAllocation->AllocDescriptor.MemoryBaseAddress >= 
MemoryAddress &&
+         Hob.MemoryAllocation->AllocDescriptor.MemoryBaseAddress < 
MemoryAddress + MemoryLength) {
+        return TRUE;
+      }
+    }
+  }
+  
+  return FALSE;
+}
 
 /**
   External function. Initializes memory services based on the memory
   descriptor HOBs.  This function is responsible for priming the memory
   map, so memory allocations and resource allocations can be made.
@@ -2141,11 +2172,11 @@ CoreInitializeMemoryServices (
     // Compute range between PHIT EfiFreeMemoryTop and the end of the Resource 
Descriptor HOB
     //
     Attributes  = PhitResourceHob->ResourceAttribute;
     BaseAddress = PageAlignAddress (PhitHob->EfiMemoryTop);
     Length      = PageAlignLength  (ResourceHob->PhysicalStart + 
ResourceHob->ResourceLength - BaseAddress);
-    if (Length < MINIMUM_INITIAL_MEMORY_SIZE) {
+    if (Length < MINIMUM_INITIAL_MEMORY_SIZE || OverlapWithMemoryAllocationHob 
(HobStart, BaseAddress, Length)) {
       //
       // If that range is not large enough to intialize the DXE Core, then 
       // Compute range between PHIT EfiFreeMemoryBottom and PHIT 
EfiFreeMemoryTop
       //
       BaseAddress = PageAlignAddress (PhitHob->EfiFreeMemoryBottom);
@@ -2155,10 +2186,17 @@ CoreInitializeMemoryServices (
         // If that range is not large enough to intialize the DXE Core, then 
         // Compute range between the start of the Resource Descriptor HOB and 
the start of the HOB List
         //
         BaseAddress = PageAlignAddress (ResourceHob->PhysicalStart);
         Length      = PageAlignLength  ((UINT64)((UINTN)*HobStart - 
BaseAddress));
+        if (OverlapWithMemoryAllocationHob (HobStart, BaseAddress, Length)) {
+          //
+          // Use free range between PHIT EfiFreeMemoryBottom and PHIT 
EfiFreeMemoryTop.
+          //
+          BaseAddress = PageAlignAddress (PhitHob->EfiFreeMemoryBottom);
+          Length      = PageAlignLength  (PhitHob->EfiFreeMemoryTop - 
BaseAddress);
+        }
       }
     }
     break;
   }
 
@@ -2216,10 +2254,17 @@ CoreInitializeMemoryServices (
     if (TestedMemoryLength < MINIMUM_INITIAL_MEMORY_SIZE) {
       continue;
     }
     
     //
+    // Skip Resource Descriptor HOBs that some of resource have been allocated 
in PEI.
+    //
+    if (OverlapWithMemoryAllocationHob (HobStart, TestedMemoryBaseAddress, 
TestedMemoryLength)) {
+      continue;
+    }
+
+    //
     // Save the Resource Descriptor HOB context that is large enough to 
initilize the DXE Core
     //
     MaxMemoryBaseAddress = TestedMemoryBaseAddress;
     MaxMemoryLength      = TestedMemoryLength;
     MaxMemoryAttributes  = ResourceHob->ResourceAttribute; 
-- 
1.9.5.msysgit.0

_______________________________________________
edk2-devel mailing list
edk2-devel@lists.01.org
https://lists.01.org/mailman/listinfo/edk2-devel

Reply via email to