]> git.proxmox.com Git - mirror_edk2.git/commitdiff
MdeModule PeiCore: Support pre memory page allocation
authorStar Zeng <star.zeng@intel.com>
Thu, 23 Feb 2017 10:16:09 +0000 (18:16 +0800)
committerStar Zeng <star.zeng@intel.com>
Mon, 4 Sep 2017 05:45:46 +0000 (13:45 +0800)
Support pre memory page allocation.
Support FreePages.
Allocation made prior to permanent memory will be
migrated to permanent memory and the HOB updated.

Cc: Liming Gao <liming.gao@intel.com>
Cc: Ruiyu Ni <Ruiyu.Ni@intel.com>
Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Star Zeng <star.zeng@intel.com>
Reviewed-by: Liming Gao <liming.gao@intel.com>
MdeModulePkg/Core/Pei/Dispatcher/Dispatcher.c
MdeModulePkg/Core/Pei/Memory/MemoryServices.c
MdeModulePkg/Core/Pei/PeiMain.h
MdeModulePkg/Core/Pei/PeiMain/PeiMain.c
MdeModulePkg/Core/Pei/Ppi/Ppi.c

index ff43a90ba53c66543789e919183059d7f034ab8f..04ece9a627d5d2603279d93182c18760b0ade61e 100644 (file)
@@ -682,13 +682,13 @@ PeiCheckAndSwitchStack (
            && (*StackPointer == INIT_CAR_VALUE);\r
            StackPointer ++);\r
 \r
-      DEBUG ((EFI_D_INFO, "Temp Stack : BaseAddress=0x%p Length=0x%X\n", SecCoreData->StackBase, (UINT32)SecCoreData->StackSize));\r
-      DEBUG ((EFI_D_INFO, "Temp Heap  : BaseAddress=0x%p Length=0x%X\n", Private->HobList.Raw, (UINT32)((UINTN) Private->HobList.HandoffInformationTable->EfiFreeMemoryTop - (UINTN) Private->HobList.Raw)));\r
-      DEBUG ((EFI_D_INFO, "Total temporary memory:    %d bytes.\n", (UINT32)SecCoreData->TemporaryRamSize));\r
-      DEBUG ((EFI_D_INFO, "  temporary memory stack ever used: %d bytes.\n",\r
+      DEBUG ((DEBUG_INFO, "Temp Stack : BaseAddress=0x%p Length=0x%X\n", SecCoreData->StackBase, (UINT32)SecCoreData->StackSize));\r
+      DEBUG ((DEBUG_INFO, "Temp Heap  : BaseAddress=0x%p Length=0x%X\n", SecCoreData->PeiTemporaryRamBase, (UINT32)SecCoreData->PeiTemporaryRamSize));\r
+      DEBUG ((DEBUG_INFO, "Total temporary memory:    %d bytes.\n", (UINT32)SecCoreData->TemporaryRamSize));\r
+      DEBUG ((DEBUG_INFO, "  temporary memory stack ever used:       %d bytes.\n",\r
              (UINT32)(SecCoreData->StackSize - ((UINTN) StackPointer - (UINTN)SecCoreData->StackBase))\r
             ));\r
-      DEBUG ((EFI_D_INFO, "  temporary memory heap used:       %d bytes.\n",\r
+      DEBUG ((DEBUG_INFO, "  temporary memory heap used for HobList: %d bytes.\n",\r
              (UINT32)((UINTN)Private->HobList.HandoffInformationTable->EfiFreeMemoryBottom - (UINTN)Private->HobList.Raw)\r
             ));\r
     DEBUG_CODE_END ();\r
@@ -800,16 +800,28 @@ PeiCheckAndSwitchStack (
                                 TemporaryRamSize\r
                                 );\r
 \r
+      //\r
+      // Migrate memory pages allocated in pre-memory phase.\r
+      // It could not be called before calling TemporaryRamSupportPpi->TemporaryRamMigration()\r
+      // as the migrated memory pages may be overridden by TemporaryRamSupportPpi->TemporaryRamMigration().\r
+      //\r
+      MigrateMemoryPages (Private, TRUE);\r
+\r
       //\r
       // Entry PEI Phase 2\r
       //\r
       PeiCore (SecCoreData, NULL, Private);\r
     } else {\r
+      //\r
+      // Migrate memory pages allocated in pre-memory phase.\r
+      //\r
+      MigrateMemoryPages (Private, FALSE);\r
+\r
       //\r
       // Migrate the PEI Services Table pointer from temporary RAM to permanent RAM.\r
       //\r
       MigratePeiServicesTablePointer ();\r
-                \r
+\r
       //\r
       // Heap Offset\r
       //\r
@@ -837,7 +849,7 @@ PeiCheckAndSwitchStack (
       //\r
       HeapTemporaryRamSize = (UINTN) (Private->HobList.HandoffInformationTable->EfiFreeMemoryBottom - Private->HobList.HandoffInformationTable->EfiMemoryBottom);\r
       ASSERT (BaseOfNewHeap + HeapTemporaryRamSize <= Private->FreePhysicalMemoryTop);\r
-      CopyMem ((UINT8 *) (UINTN) BaseOfNewHeap, (UINT8 *) PeiTemporaryRamBase, HeapTemporaryRamSize);\r
+      CopyMem ((UINT8 *) (UINTN) BaseOfNewHeap, PeiTemporaryRamBase, HeapTemporaryRamSize);\r
 \r
       //\r
       // Migrate Stack\r
@@ -846,7 +858,6 @@ PeiCheckAndSwitchStack (
 \r
       //\r
       // Copy Hole Range Data\r
-      // Convert PPI from Hole. \r
       //\r
       if (HoleMemSize != 0) {\r
         //\r
index 719372e0612801176d6bd47d68c0c12b791b69f0..516311d09189db05f7507aac18d49a906b5f1068 100644 (file)
@@ -1,7 +1,7 @@
 /** @file\r
   EFI PEI Core memory services\r
   \r
-Copyright (c) 2006 - 2015, Intel Corporation. All rights reserved.<BR>\r
+Copyright (c) 2006 - 2017, Intel Corporation. All rights reserved.<BR>\r
 This program and the accompanying materials                          \r
 are licensed and made available under the terms and conditions of the BSD License         \r
 which accompanies this distribution.  The full text of the license may be found at        \r
@@ -110,18 +110,387 @@ PeiInstallPeiMemory (
 }\r
 \r
 /**\r
-  The purpose of the service is to publish an interface that allows \r
+  Migrate memory pages allocated in pre-memory phase.\r
+  Copy memory pages at temporary heap top to permanent heap top.\r
+\r
+  @param[in] Private                Pointer to the private data passed in from caller.\r
+  @param[in] TemporaryRamMigrated   Temporary memory has been migrated to permanent memory.\r
+\r
+**/\r
+VOID\r
+MigrateMemoryPages (\r
+  IN PEI_CORE_INSTANCE      *Private,\r
+  IN BOOLEAN                TemporaryRamMigrated\r
+  )\r
+{\r
+  EFI_PHYSICAL_ADDRESS      NewMemPagesBase;\r
+  EFI_PHYSICAL_ADDRESS      MemPagesBase;\r
+\r
+  Private->MemoryPages.Size = (UINTN) (Private->HobList.HandoffInformationTable->EfiMemoryTop -\r
+                                       Private->HobList.HandoffInformationTable->EfiFreeMemoryTop);\r
+  if (Private->MemoryPages.Size == 0) {\r
+    //\r
+    // No any memory page allocated in pre-memory phase.\r
+    //\r
+    return;\r
+  }\r
+  Private->MemoryPages.Base = Private->HobList.HandoffInformationTable->EfiFreeMemoryTop;\r
+\r
+  ASSERT (Private->MemoryPages.Size <= Private->FreePhysicalMemoryTop);\r
+  NewMemPagesBase = Private->FreePhysicalMemoryTop - Private->MemoryPages.Size;\r
+  NewMemPagesBase &= ~(UINT64)EFI_PAGE_MASK;\r
+  ASSERT (NewMemPagesBase >= Private->PhysicalMemoryBegin);\r
+  //\r
+  // Copy memory pages at temporary heap top to permanent heap top.\r
+  //\r
+  if (TemporaryRamMigrated) {\r
+    //\r
+    // Memory pages at temporary heap top has been migrated to permanent heap,\r
+    // Here still needs to copy them from permanent heap to permanent heap top.\r
+    //\r
+    MemPagesBase = Private->MemoryPages.Base;\r
+    if (Private->HeapOffsetPositive) {\r
+      MemPagesBase += Private->HeapOffset;\r
+    } else {\r
+      MemPagesBase -= Private->HeapOffset;\r
+    }\r
+    CopyMem ((VOID *)(UINTN)NewMemPagesBase, (VOID *)(UINTN)MemPagesBase, Private->MemoryPages.Size);\r
+  } else {\r
+    CopyMem ((VOID *)(UINTN)NewMemPagesBase, (VOID *)(UINTN)Private->MemoryPages.Base, Private->MemoryPages.Size);\r
+  }\r
+\r
+  if (NewMemPagesBase >= Private->MemoryPages.Base) {\r
+    Private->MemoryPages.OffsetPositive = TRUE;\r
+    Private->MemoryPages.Offset = (UINTN)(NewMemPagesBase - Private->MemoryPages.Base);\r
+  } else {\r
+    Private->MemoryPages.OffsetPositive = FALSE;\r
+    Private->MemoryPages.Offset = (UINTN)(Private->MemoryPages.Base - NewMemPagesBase);\r
+  }\r
+\r
+  DEBUG ((DEBUG_INFO, "Pages Offset = 0x%lX\n", (UINT64) Private->MemoryPages.Offset));\r
+\r
+  Private->FreePhysicalMemoryTop = NewMemPagesBase;\r
+}\r
+\r
+/**\r
+  Migrate MemoryBaseAddress in memory allocation HOBs\r
+  from the temporary memory to PEI installed memory.\r
+\r
+  @param[in] PrivateData        Pointer to PeiCore's private data structure.\r
+\r
+**/\r
+VOID\r
+ConvertMemoryAllocationHobs (\r
+  IN PEI_CORE_INSTANCE          *PrivateData\r
+  )\r
+{\r
+  EFI_PEI_HOB_POINTERS          Hob;\r
+  EFI_HOB_MEMORY_ALLOCATION     *MemoryAllocationHob;\r
+  EFI_PHYSICAL_ADDRESS          OldMemPagesBase;\r
+  UINTN                         OldMemPagesSize;\r
+\r
+  if (PrivateData->MemoryPages.Size == 0) {\r
+    //\r
+    // No any memory page allocated in pre-memory phase.\r
+    //\r
+    return;\r
+  }\r
+\r
+  OldMemPagesBase = PrivateData->MemoryPages.Base;\r
+  OldMemPagesSize = PrivateData->MemoryPages.Size;\r
+\r
+  MemoryAllocationHob = NULL;\r
+  Hob.Raw = GetFirstHob (EFI_HOB_TYPE_MEMORY_ALLOCATION);\r
+  while (Hob.Raw != NULL) {\r
+    MemoryAllocationHob = (EFI_HOB_MEMORY_ALLOCATION *) Hob.Raw;\r
+    if ((MemoryAllocationHob->AllocDescriptor.MemoryBaseAddress >= OldMemPagesBase) &&\r
+        (MemoryAllocationHob->AllocDescriptor.MemoryBaseAddress < (OldMemPagesBase + OldMemPagesSize))\r
+        ) {\r
+      if (PrivateData->MemoryPages.OffsetPositive) {\r
+        MemoryAllocationHob->AllocDescriptor.MemoryBaseAddress += PrivateData->MemoryPages.Offset;\r
+      } else {\r
+        MemoryAllocationHob->AllocDescriptor.MemoryBaseAddress -= PrivateData->MemoryPages.Offset;\r
+      }\r
+    }\r
+\r
+    Hob.Raw = GET_NEXT_HOB (Hob);\r
+    Hob.Raw = GetNextHob (EFI_HOB_TYPE_MEMORY_ALLOCATION, Hob.Raw);\r
+  }\r
+}\r
+\r
+/**\r
+  Internal function to build a HOB for the memory allocation.\r
+  It will search and reuse the unused(freed) memory allocation HOB,\r
+  or build memory allocation HOB normally if no unused(freed) memory allocation HOB found.\r
+\r
+  @param[in] BaseAddress        The 64 bit physical address of the memory.\r
+  @param[in] Length             The length of the memory allocation in bytes.\r
+  @param[in] MemoryType         The type of memory allocated by this HOB.\r
+\r
+**/\r
+VOID\r
+InternalBuildMemoryAllocationHob (\r
+  IN EFI_PHYSICAL_ADDRESS       BaseAddress,\r
+  IN UINT64                     Length,\r
+  IN EFI_MEMORY_TYPE            MemoryType\r
+  )\r
+{\r
+  EFI_PEI_HOB_POINTERS          Hob;\r
+  EFI_HOB_MEMORY_ALLOCATION     *MemoryAllocationHob;\r
+\r
+  //\r
+  // Search unused(freed) memory allocation HOB.\r
+  //\r
+  MemoryAllocationHob = NULL;\r
+  Hob.Raw = GetFirstHob (EFI_HOB_TYPE_UNUSED);\r
+  while (Hob.Raw != NULL) {\r
+    if (Hob.Header->HobLength == sizeof (EFI_HOB_MEMORY_ALLOCATION)) {\r
+      MemoryAllocationHob = (EFI_HOB_MEMORY_ALLOCATION *) Hob.Raw;\r
+      break;\r
+    }\r
+\r
+    Hob.Raw = GET_NEXT_HOB (Hob);\r
+    Hob.Raw = GetNextHob (EFI_HOB_TYPE_UNUSED, Hob.Raw);\r
+  }\r
+\r
+  if (MemoryAllocationHob != NULL) {\r
+    //\r
+    // Reuse the unused(freed) memory allocation HOB.\r
+    //\r
+    MemoryAllocationHob->Header.HobType = EFI_HOB_TYPE_MEMORY_ALLOCATION;\r
+    ZeroMem (&(MemoryAllocationHob->AllocDescriptor.Name), sizeof (EFI_GUID));\r
+    MemoryAllocationHob->AllocDescriptor.MemoryBaseAddress = BaseAddress;\r
+    MemoryAllocationHob->AllocDescriptor.MemoryLength      = Length;\r
+    MemoryAllocationHob->AllocDescriptor.MemoryType        = MemoryType;\r
+    //\r
+    // Zero the reserved space to match HOB spec\r
+    //\r
+    ZeroMem (MemoryAllocationHob->AllocDescriptor.Reserved, sizeof (MemoryAllocationHob->AllocDescriptor.Reserved));\r
+  } else {\r
+    //\r
+    // No unused(freed) memory allocation HOB found.\r
+    // Build memory allocation HOB normally.\r
+    //\r
+    BuildMemoryAllocationHob (\r
+      BaseAddress,\r
+      Length,\r
+      MemoryType\r
+      );\r
+  }\r
+}\r
+\r
+/**\r
+  Update or split memory allocation HOB for memory pages allocate and free.\r
+\r
+  @param[in, out] MemoryAllocationHob   Pointer to the memory allocation HOB\r
+                                        that needs to be updated or split.\r
+                                        On output, it will be filled with\r
+                                        the input Memory, Bytes and MemoryType.\r
+  @param[in]      Memory                Memory to allocate or free.\r
+  @param[in]      Bytes                 Bytes to allocate or free.\r
+  @param[in]      MemoryType            EfiConventionalMemory for pages free,\r
+                                        others for pages allocate.\r
+\r
+**/\r
+VOID\r
+UpdateOrSplitMemoryAllocationHob (\r
+  IN OUT EFI_HOB_MEMORY_ALLOCATION      *MemoryAllocationHob,\r
+  IN EFI_PHYSICAL_ADDRESS               Memory,\r
+  IN UINT64                             Bytes,\r
+  IN EFI_MEMORY_TYPE                    MemoryType\r
+  )\r
+{\r
+  if ((Memory + Bytes) <\r
+      (MemoryAllocationHob->AllocDescriptor.MemoryBaseAddress + MemoryAllocationHob->AllocDescriptor.MemoryLength)) {\r
+    //\r
+    // Last pages need to be split out.\r
+    //\r
+    InternalBuildMemoryAllocationHob (\r
+      Memory + Bytes,\r
+      (MemoryAllocationHob->AllocDescriptor.MemoryBaseAddress + MemoryAllocationHob->AllocDescriptor.MemoryLength) - (Memory + Bytes),\r
+      MemoryAllocationHob->AllocDescriptor.MemoryType\r
+      );\r
+  }\r
+\r
+  if (Memory > MemoryAllocationHob->AllocDescriptor.MemoryBaseAddress) {\r
+    //\r
+    // First pages need to be split out.\r
+    //\r
+    InternalBuildMemoryAllocationHob (\r
+      MemoryAllocationHob->AllocDescriptor.MemoryBaseAddress,\r
+      Memory - MemoryAllocationHob->AllocDescriptor.MemoryBaseAddress,\r
+      MemoryAllocationHob->AllocDescriptor.MemoryType\r
+      );\r
+  }\r
+\r
+  //\r
+  // Update the memory allocation HOB.\r
+  //\r
+  MemoryAllocationHob->AllocDescriptor.MemoryBaseAddress = Memory;\r
+  MemoryAllocationHob->AllocDescriptor.MemoryLength = Bytes;\r
+  MemoryAllocationHob->AllocDescriptor.MemoryType = MemoryType;\r
+}\r
+\r
+/**\r
+  Merge adjacent free memory ranges in memory allocation HOBs.\r
+\r
+  @retval TRUE          There are free memory ranges merged.\r
+  @retval FALSE         No free memory ranges merged.\r
+\r
+**/\r
+BOOLEAN\r
+MergeFreeMemoryInMemoryAllocationHob (\r
+  VOID\r
+  )\r
+{\r
+  EFI_PEI_HOB_POINTERS          Hob;\r
+  EFI_PEI_HOB_POINTERS          Hob2;\r
+  EFI_HOB_MEMORY_ALLOCATION     *MemoryHob;\r
+  EFI_HOB_MEMORY_ALLOCATION     *MemoryHob2;\r
+  UINT64                        Start;\r
+  UINT64                        End;\r
+  BOOLEAN                       Merged;\r
+\r
+  Merged = FALSE;\r
+\r
+  Hob.Raw = GetFirstHob (EFI_HOB_TYPE_MEMORY_ALLOCATION);\r
+  while (Hob.Raw != NULL) {\r
+    if (Hob.MemoryAllocation->AllocDescriptor.MemoryType == EfiConventionalMemory) {\r
+      MemoryHob = (EFI_HOB_MEMORY_ALLOCATION *) Hob.Raw;\r
+      Start = MemoryHob->AllocDescriptor.MemoryBaseAddress;\r
+      End = MemoryHob->AllocDescriptor.MemoryBaseAddress + MemoryHob->AllocDescriptor.MemoryLength;\r
+\r
+      Hob2.Raw = GET_NEXT_HOB (Hob);\r
+      Hob2.Raw = GetNextHob (EFI_HOB_TYPE_MEMORY_ALLOCATION, Hob.Raw);\r
+      while (Hob2.Raw != NULL) {\r
+        if (Hob2.MemoryAllocation->AllocDescriptor.MemoryType == EfiConventionalMemory) {\r
+          MemoryHob2 = (EFI_HOB_MEMORY_ALLOCATION *) Hob2.Raw;\r
+          if (Start == (MemoryHob2->AllocDescriptor.MemoryBaseAddress + MemoryHob2->AllocDescriptor.MemoryLength)) {\r
+            //\r
+            // Merge adjacent two free memory ranges.\r
+            //\r
+            MemoryHob2->AllocDescriptor.MemoryLength += MemoryHob->AllocDescriptor.MemoryLength;\r
+            Merged = TRUE;\r
+            //\r
+            // Mark MemoryHob to be unused(freed).\r
+            //\r
+            MemoryHob->Header.HobType = EFI_HOB_TYPE_UNUSED;\r
+            break;\r
+          } else if (End == MemoryHob2->AllocDescriptor.MemoryBaseAddress) {\r
+            //\r
+            // Merge adjacent two free memory ranges.\r
+            //\r
+            MemoryHob2->AllocDescriptor.MemoryBaseAddress = MemoryHob->AllocDescriptor.MemoryBaseAddress;\r
+            MemoryHob2->AllocDescriptor.MemoryLength += MemoryHob->AllocDescriptor.MemoryLength;\r
+            Merged = TRUE;\r
+            //\r
+            // Mark MemoryHob to be unused(freed).\r
+            //\r
+            MemoryHob->Header.HobType = EFI_HOB_TYPE_UNUSED;\r
+            break;\r
+          }\r
+        }\r
+        Hob2.Raw = GET_NEXT_HOB (Hob2);\r
+        Hob2.Raw = GetNextHob (EFI_HOB_TYPE_MEMORY_ALLOCATION, Hob2.Raw);\r
+      }\r
+    }\r
+    Hob.Raw = GET_NEXT_HOB (Hob);\r
+    Hob.Raw = GetNextHob (EFI_HOB_TYPE_MEMORY_ALLOCATION, Hob.Raw);\r
+  }\r
+\r
+  return Merged;\r
+}\r
+\r
+/**\r
+  Find free memory by searching memory allocation HOBs.\r
+\r
+  @param[in]  MemoryType        The type of memory to allocate.\r
+  @param[in]  Pages             The number of contiguous 4 KB pages to allocate.\r
+  @param[in]  Granularity       Page allocation granularity.\r
+  @param[out] Memory            Pointer to a physical address. On output, the address is set to the base\r
+                                of the page range that was allocated.\r
+\r
+  @retval EFI_SUCCESS           The memory range was successfully allocated.\r
+  @retval EFI_NOT_FOUND         No memory allocation HOB with big enough free memory found.\r
+\r
+**/\r
+EFI_STATUS\r
+FindFreeMemoryFromMemoryAllocationHob (\r
+  IN  EFI_MEMORY_TYPE           MemoryType,\r
+  IN  UINTN                     Pages,\r
+  IN  UINTN                     Granularity,\r
+  OUT EFI_PHYSICAL_ADDRESS      *Memory\r
+  )\r
+{\r
+  EFI_PEI_HOB_POINTERS          Hob;\r
+  EFI_HOB_MEMORY_ALLOCATION     *MemoryAllocationHob;\r
+  UINT64                        Bytes;\r
+  EFI_PHYSICAL_ADDRESS          BaseAddress;\r
+\r
+  Bytes = LShiftU64 (Pages, EFI_PAGE_SHIFT);\r
+\r
+  BaseAddress = 0;\r
+  MemoryAllocationHob = NULL;\r
+  Hob.Raw = GetFirstHob (EFI_HOB_TYPE_MEMORY_ALLOCATION);\r
+  while (Hob.Raw != NULL) {\r
+    if ((Hob.MemoryAllocation->AllocDescriptor.MemoryType == EfiConventionalMemory) &&\r
+        (Hob.MemoryAllocation->AllocDescriptor.MemoryLength >= Bytes)) {\r
+      //\r
+      // Found one memory allocation HOB with big enough free memory.\r
+      //\r
+      MemoryAllocationHob = (EFI_HOB_MEMORY_ALLOCATION *) Hob.Raw;\r
+      BaseAddress = MemoryAllocationHob->AllocDescriptor.MemoryBaseAddress +\r
+                    MemoryAllocationHob->AllocDescriptor.MemoryLength - Bytes;\r
+      //\r
+      // Make sure the granularity could be satisfied.\r
+      //\r
+      BaseAddress &= ~((EFI_PHYSICAL_ADDRESS) Granularity - 1);\r
+      if (BaseAddress >= MemoryAllocationHob->AllocDescriptor.MemoryBaseAddress) {\r
+        break;\r
+      }\r
+      BaseAddress = 0;\r
+      MemoryAllocationHob = NULL;\r
+    }\r
+    //\r
+    // Continue to find.\r
+    //\r
+    Hob.Raw = GET_NEXT_HOB (Hob);\r
+    Hob.Raw = GetNextHob (EFI_HOB_TYPE_MEMORY_ALLOCATION, Hob.Raw);\r
+  }\r
+\r
+  if (MemoryAllocationHob != NULL) {\r
+    UpdateOrSplitMemoryAllocationHob (MemoryAllocationHob, BaseAddress, Bytes, MemoryType);\r
+    *Memory = BaseAddress;\r
+    return EFI_SUCCESS;\r
+  } else {\r
+    if (MergeFreeMemoryInMemoryAllocationHob ()) {\r
+      //\r
+      // Retry if there are free memory ranges merged.\r
+      //\r
+      return FindFreeMemoryFromMemoryAllocationHob (MemoryType, Pages, Granularity, Memory);\r
+    }\r
+    return EFI_NOT_FOUND;\r
+  }\r
+}\r
+\r
+/**\r
+  The purpose of the service is to publish an interface that allows\r
   PEIMs to allocate memory ranges that are managed by the PEI Foundation.\r
 \r
+  Prior to InstallPeiMemory() being called, PEI will allocate pages from the heap.\r
+  After InstallPeiMemory() is called, PEI will allocate pages within the region\r
+  of memory provided by InstallPeiMemory() service in a best-effort fashion.\r
+  Location-specific allocations are not managed by the PEI foundation code.\r
+\r
   @param  PeiServices      An indirect pointer to the EFI_PEI_SERVICES table published by the PEI Foundation.\r
   @param  MemoryType       The type of memory to allocate.\r
   @param  Pages            The number of contiguous 4 KB pages to allocate.\r
-  @param  Memory           Pointer to a physical address. On output, the address is set to the base \r
+  @param  Memory           Pointer to a physical address. On output, the address is set to the base\r
                            of the page range that was allocated.\r
 \r
   @retval EFI_SUCCESS           The memory range was successfully allocated.\r
   @retval EFI_OUT_OF_RESOURCES  The pages could not be allocated.\r
-  @retval EFI_INVALID_PARAMETER Type is not equal to EfiLoaderCode, EfiLoaderData, EfiRuntimeServicesCode, \r
+  @retval EFI_INVALID_PARAMETER Type is not equal to EfiLoaderCode, EfiLoaderData, EfiRuntimeServicesCode,\r
                                 EfiRuntimeServicesData, EfiBootServicesCode, EfiBootServicesData,\r
                                 EfiACPIReclaimMemory, EfiReservedMemoryType, or EfiACPIMemoryNVS.\r
 \r
@@ -135,6 +504,7 @@ PeiAllocatePages (
   OUT      EFI_PHYSICAL_ADDRESS *Memory\r
   )\r
 {\r
+  EFI_STATUS                              Status;\r
   PEI_CORE_INSTANCE                       *PrivateData;\r
   EFI_PEI_HOB_POINTERS                    Hob;\r
   EFI_PHYSICAL_ADDRESS                    *FreeMemoryTop;\r
@@ -157,6 +527,16 @@ PeiAllocatePages (
 \r
   Granularity = DEFAULT_PAGE_ALLOCATION_GRANULARITY;\r
 \r
+  PrivateData = PEI_CORE_INSTANCE_FROM_PS_THIS (PeiServices);\r
+  Hob.Raw     = PrivateData->HobList.Raw;\r
+\r
+  if (Hob.Raw == NULL) {\r
+    //\r
+    // HOB is not initialized yet.\r
+    //\r
+    return EFI_NOT_AVAILABLE_YET;\r
+  }\r
+\r
   if  (RUNTIME_PAGE_ALLOCATION_GRANULARITY > DEFAULT_PAGE_ALLOCATION_GRANULARITY &&\r
        (MemoryType == EfiACPIReclaimMemory   ||\r
         MemoryType == EfiACPIMemoryNVS       ||\r
@@ -169,23 +549,13 @@ PeiAllocatePages (
       Granularity / SIZE_1KB));\r
   }\r
 \r
-  PrivateData = PEI_CORE_INSTANCE_FROM_PS_THIS (PeiServices);\r
-  Hob.Raw     = PrivateData->HobList.Raw;\r
-  \r
-  //\r
-  // Check if Hob already available\r
-  //\r
-  if (!PrivateData->PeiMemoryInstalled) {\r
+  if (!PrivateData->PeiMemoryInstalled && PrivateData->SwitchStackSignal) {\r
     //\r
-    // When PeiInstallMemory is called but temporary memory has *not* been moved to temporary memory,\r
+    // When PeiInstallMemory is called but temporary memory has *not* been moved to permanent memory,\r
     // the AllocatePage will depend on the field of PEI_CORE_INSTANCE structure.\r
     //\r
-    if (!PrivateData->SwitchStackSignal) {\r
-      return EFI_NOT_AVAILABLE_YET;\r
-    } else {\r
-      FreeMemoryTop     = &(PrivateData->FreePhysicalMemoryTop);\r
-      FreeMemoryBottom  = &(PrivateData->PhysicalMemoryBegin);\r
-    }\r
+    FreeMemoryTop     = &(PrivateData->FreePhysicalMemoryTop);\r
+    FreeMemoryBottom  = &(PrivateData->PhysicalMemoryBegin);\r
   } else {\r
     FreeMemoryTop     = &(Hob.HandoffInformationTable->EfiFreeMemoryTop);\r
     FreeMemoryBottom  = &(Hob.HandoffInformationTable->EfiFreeMemoryBottom);\r
@@ -207,7 +577,7 @@ PeiAllocatePages (
     // Create a memory allocation HOB to cover\r
     // the pages that we will lose to rounding\r
     //\r
-    BuildMemoryAllocationHob (\r
+    InternalBuildMemoryAllocationHob (\r
       *(FreeMemoryTop),\r
       Padding & ~(UINTN)EFI_PAGE_MASK,\r
       EfiConventionalMemory\r
@@ -216,18 +586,20 @@ PeiAllocatePages (
 \r
   //\r
   // Verify that there is sufficient memory to satisfy the allocation.\r
-  // For page allocation, the overhead sizeof (EFI_HOB_MEMORY_ALLOCATION) needs to be considered.\r
   //\r
-  if ((UINTN) (*FreeMemoryTop - *FreeMemoryBottom) < (UINTN) ALIGN_VALUE (sizeof (EFI_HOB_MEMORY_ALLOCATION), 8)) {\r
-    DEBUG ((EFI_D_ERROR, "AllocatePages failed: No space to build memory allocation hob.\n"));\r
-    return  EFI_OUT_OF_RESOURCES;\r
-  }\r
-  RemainingPages = (UINTN)(*FreeMemoryTop - *FreeMemoryBottom - ALIGN_VALUE (sizeof (EFI_HOB_MEMORY_ALLOCATION), 8)) >> EFI_PAGE_SHIFT;\r
+  RemainingPages = (UINTN)(*FreeMemoryTop - *FreeMemoryBottom) >> EFI_PAGE_SHIFT;\r
   //\r
   // The number of remaining pages needs to be greater than or equal to that of the request pages.\r
   //\r
   Pages = ALIGN_VALUE (Pages, EFI_SIZE_TO_PAGES (Granularity));\r
   if (RemainingPages < Pages) {\r
+    //\r
+    // Try to find free memory by searching memory allocation HOBs.\r
+    //\r
+    Status = FindFreeMemoryFromMemoryAllocationHob (MemoryType, Pages, Granularity, Memory);\r
+    if (!EFI_ERROR (Status)) {\r
+      return Status;\r
+    }\r
     DEBUG ((EFI_D_ERROR, "AllocatePages failed: No 0x%lx Pages is available.\n", (UINT64) Pages));\r
     DEBUG ((EFI_D_ERROR, "There is only left 0x%lx pages memory resource to be allocated.\n", (UINT64) RemainingPages));\r
     return  EFI_OUT_OF_RESOURCES;\r
@@ -245,7 +617,7 @@ PeiAllocatePages (
     //\r
     // Create a memory allocation HOB.\r
     //\r
-    BuildMemoryAllocationHob (\r
+    InternalBuildMemoryAllocationHob (\r
       *(FreeMemoryTop),\r
       Pages * EFI_PAGE_SIZE,\r
       MemoryType\r
@@ -255,6 +627,141 @@ PeiAllocatePages (
   }\r
 }\r
 \r
+/**\r
+  Mark the memory allocation HOB to be unused(freed) and update *FreeMemoryTop\r
+  if MemoryBaseAddress == *FreeMemoryTop.\r
+\r
+  @param[in]      PrivateData                   Pointer to PeiCore's private data structure.\r
+  @param[in, out] MemoryAllocationHobToFree     Pointer to memory allocation HOB to be freed.\r
+\r
+**/\r
+VOID\r
+FreeMemoryAllocationHob (\r
+  IN PEI_CORE_INSTANCE                  *PrivateData,\r
+  IN OUT EFI_HOB_MEMORY_ALLOCATION      *MemoryAllocationHobToFree\r
+  )\r
+{\r
+  EFI_PEI_HOB_POINTERS                  Hob;\r
+  EFI_PHYSICAL_ADDRESS                  *FreeMemoryTop;\r
+  EFI_HOB_MEMORY_ALLOCATION             *MemoryAllocationHob;\r
+\r
+  Hob.Raw = PrivateData->HobList.Raw;\r
+\r
+  if (!PrivateData->PeiMemoryInstalled && PrivateData->SwitchStackSignal) {\r
+    //\r
+    // When PeiInstallMemory is called but temporary memory has *not* been moved to permanent memory,\r
+    // use the FreePhysicalMemoryTop field of PEI_CORE_INSTANCE structure.\r
+    //\r
+    FreeMemoryTop = &(PrivateData->FreePhysicalMemoryTop);\r
+  } else {\r
+    FreeMemoryTop = &(Hob.HandoffInformationTable->EfiFreeMemoryTop);\r
+  }\r
+\r
+  if (MemoryAllocationHobToFree->AllocDescriptor.MemoryBaseAddress == *FreeMemoryTop) {\r
+    //\r
+    // Update *FreeMemoryTop.\r
+    //\r
+    *FreeMemoryTop += MemoryAllocationHobToFree->AllocDescriptor.MemoryLength;\r
+    //\r
+    // Mark the memory allocation HOB to be unused(freed).\r
+    //\r
+    MemoryAllocationHobToFree->Header.HobType = EFI_HOB_TYPE_UNUSED;\r
+\r
+    MemoryAllocationHob = NULL;\r
+    Hob.Raw = GetFirstHob (EFI_HOB_TYPE_MEMORY_ALLOCATION);\r
+    while (Hob.Raw != NULL) {\r
+      if ((Hob.MemoryAllocation->AllocDescriptor.MemoryType == EfiConventionalMemory) &&\r
+          (Hob.MemoryAllocation->AllocDescriptor.MemoryBaseAddress == *FreeMemoryTop)) {\r
+        //\r
+        // Found memory allocation HOB that has EfiConventionalMemory MemoryType and\r
+        // MemoryBaseAddress == new *FreeMemoryTop.\r
+        //\r
+        MemoryAllocationHob = (EFI_HOB_MEMORY_ALLOCATION *) Hob.Raw;\r
+        break;\r
+      }\r
+      Hob.Raw = GET_NEXT_HOB (Hob);\r
+      Hob.Raw = GetNextHob (EFI_HOB_TYPE_MEMORY_ALLOCATION, Hob.Raw);\r
+    }\r
+    //\r
+    // Free memory allocation HOB iteratively.\r
+    //\r
+    if (MemoryAllocationHob != NULL) {\r
+      FreeMemoryAllocationHob (PrivateData, MemoryAllocationHob);\r
+    }\r
+  }\r
+}\r
+\r
+/**\r
+  Frees memory pages.\r
+\r
+  @param[in] PeiServices        An indirect pointer to the EFI_PEI_SERVICES table published by the PEI Foundation.\r
+  @param[in] Memory             The base physical address of the pages to be freed.\r
+  @param[in] Pages              The number of contiguous 4 KB pages to free.\r
+\r
+  @retval EFI_SUCCESS           The requested pages were freed.\r
+  @retval EFI_INVALID_PARAMETER Memory is not a page-aligned address or Pages is invalid.\r
+  @retval EFI_NOT_FOUND         The requested memory pages were not allocated with\r
+                                AllocatePages().\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+PeiFreePages (\r
+  IN CONST EFI_PEI_SERVICES     **PeiServices,\r
+  IN EFI_PHYSICAL_ADDRESS       Memory,\r
+  IN UINTN                      Pages\r
+  )\r
+{\r
+  PEI_CORE_INSTANCE             *PrivateData;\r
+  UINT64                        Bytes;\r
+  UINT64                        Start;\r
+  UINT64                        End;\r
+  EFI_PEI_HOB_POINTERS          Hob;\r
+  EFI_HOB_MEMORY_ALLOCATION     *MemoryAllocationHob;\r
+\r
+  Bytes = LShiftU64 (Pages, EFI_PAGE_SHIFT);\r
+  Start = Memory;\r
+  End = Start + Bytes - 1;\r
+\r
+  if (Pages == 0 || ((Start & EFI_PAGE_MASK) != 0) || (Start >= End)) {\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
+\r
+  PrivateData = PEI_CORE_INSTANCE_FROM_PS_THIS (PeiServices);\r
+  Hob.Raw     = PrivateData->HobList.Raw;\r
+\r
+  if (Hob.Raw == NULL) {\r
+    //\r
+    // HOB is not initialized yet.\r
+    //\r
+    return EFI_NOT_AVAILABLE_YET;\r
+  }\r
+\r
+  MemoryAllocationHob = NULL;\r
+  Hob.Raw = GetFirstHob (EFI_HOB_TYPE_MEMORY_ALLOCATION);\r
+  while (Hob.Raw != NULL) {\r
+    if ((Hob.MemoryAllocation->AllocDescriptor.MemoryType != EfiConventionalMemory) &&\r
+        (Memory >= Hob.MemoryAllocation->AllocDescriptor.MemoryBaseAddress) &&\r
+        ((Memory + Bytes) <= (Hob.MemoryAllocation->AllocDescriptor.MemoryBaseAddress + Hob.MemoryAllocation->AllocDescriptor.MemoryLength))) {\r
+      //\r
+      // Found the memory allocation HOB that includes the memory pages to be freed.\r
+      //\r
+      MemoryAllocationHob = (EFI_HOB_MEMORY_ALLOCATION *) Hob.Raw;\r
+      break;\r
+    }\r
+    Hob.Raw = GET_NEXT_HOB (Hob);\r
+    Hob.Raw = GetNextHob (EFI_HOB_TYPE_MEMORY_ALLOCATION, Hob.Raw);\r
+  }\r
+\r
+  if (MemoryAllocationHob != NULL) {\r
+    UpdateOrSplitMemoryAllocationHob (MemoryAllocationHob, Memory, Bytes, EfiConventionalMemory);\r
+    FreeMemoryAllocationHob (PrivateData, MemoryAllocationHob);\r
+    return EFI_SUCCESS;\r
+  } else {\r
+    return EFI_NOT_FOUND;\r
+  }\r
+}\r
+\r
 /**\r
 \r
   Pool allocation service. Before permanent memory is discoveried, the pool will\r
index 277f54a0c6ada0d37ec4e01ee6e807aff6d89e37..fef3753e4b3b3c4b1473360eef16e0a217b5484e 100644 (file)
@@ -233,6 +233,10 @@ struct _PEI_CORE_INSTANCE {
   BOOLEAN                            HeapOffsetPositive;\r
   UINTN                              StackOffset;\r
   BOOLEAN                            StackOffsetPositive;\r
+  //\r
+  // Information for migrating memory pages allocated in pre-memory phase.\r
+  //\r
+  HOLE_MEMORY_DATA                   MemoryPages;\r
   PEICORE_FUNCTION_POINTER           ShadowedPeiCore;\r
   CACHE_SECTION_DATA                 CacheSection;\r
   //\r
@@ -263,7 +267,7 @@ struct _PEI_CORE_INSTANCE {
 \r
   //\r
   // Temp Memory Range is not covered by PeiTempMem and Stack.\r
-  // Those Memory Range will be migrated into phisical memory. \r
+  // Those Memory Range will be migrated into physical memory.\r
   //\r
   HOLE_MEMORY_DATA                  HoleData[HOLE_MAX_NUMBER];\r
 };\r
@@ -423,7 +427,7 @@ InitializePpiServices (
 \r
 /**\r
 \r
-  Migrate the Hob list from the temporary memory stack to PEI installed memory.\r
+  Migrate the Hob list from the temporary memory to PEI installed memory.\r
 \r
   @param SecCoreData     Points to a data structure containing SEC to PEI handoff data, such as the size \r
                          and location of temporary RAM, the stack location and the BFV location.\r
@@ -877,30 +881,81 @@ PeiInstallPeiMemory (
   );\r
 \r
 /**\r
+  Migrate memory pages allocated in pre-memory phase.\r
+  Copy memory pages at temporary heap top to permanent heap top.\r
 \r
-  Memory allocation service on permanent memory,\r
-  not usable prior to the memory installation.\r
+  @param[in] Private                Pointer to the private data passed in from caller.\r
+  @param[in] TemporaryRamMigrated   Temporary memory has been migrated to permanent memory.\r
 \r
+**/\r
+VOID\r
+MigrateMemoryPages (\r
+  IN PEI_CORE_INSTANCE      *Private,\r
+  IN BOOLEAN                TemporaryRamMigrated\r
+  );\r
 \r
-  @param PeiServices               An indirect pointer to the EFI_PEI_SERVICES table published by the PEI Foundation.\r
-  @param MemoryType                Type of memory to allocate.\r
-  @param Pages                     Number of pages to allocate.\r
-  @param Memory                    Pointer of memory allocated.\r
+/**\r
+  Migrate MemoryBaseAddress in memory allocation HOBs\r
+  from the temporary memory to PEI installed memory.\r
 \r
-  @retval EFI_SUCCESS              The allocation was successful\r
-  @retval EFI_INVALID_PARAMETER    Only AllocateAnyAddress is supported.\r
-  @retval EFI_NOT_AVAILABLE_YET    Called with permanent memory not available\r
-  @retval EFI_OUT_OF_RESOURCES     There is not enough HOB heap to satisfy the requirement\r
-                                   to allocate the number of pages.\r
+  @param[in] PrivateData        Pointer to PeiCore's private data structure.\r
+\r
+**/\r
+VOID\r
+ConvertMemoryAllocationHobs (\r
+  IN PEI_CORE_INSTANCE          *PrivateData\r
+  );\r
+\r
+/**\r
+  The purpose of the service is to publish an interface that allows\r
+  PEIMs to allocate memory ranges that are managed by the PEI Foundation.\r
+\r
+  Prior to InstallPeiMemory() being called, PEI will allocate pages from the heap.\r
+  After InstallPeiMemory() is called, PEI will allocate pages within the region\r
+  of memory provided by InstallPeiMemory() service in a best-effort fashion.\r
+  Location-specific allocations are not managed by the PEI foundation code.\r
+\r
+  @param  PeiServices      An indirect pointer to the EFI_PEI_SERVICES table published by the PEI Foundation.\r
+  @param  MemoryType       The type of memory to allocate.\r
+  @param  Pages            The number of contiguous 4 KB pages to allocate.\r
+  @param  Memory           Pointer to a physical address. On output, the address is set to the base\r
+                           of the page range that was allocated.\r
+\r
+  @retval EFI_SUCCESS           The memory range was successfully allocated.\r
+  @retval EFI_OUT_OF_RESOURCES  The pages could not be allocated.\r
+  @retval EFI_INVALID_PARAMETER Type is not equal to EfiLoaderCode, EfiLoaderData, EfiRuntimeServicesCode,\r
+                                EfiRuntimeServicesData, EfiBootServicesCode, EfiBootServicesData,\r
+                                EfiACPIReclaimMemory, EfiReservedMemoryType, or EfiACPIMemoryNVS.\r
 \r
 **/\r
 EFI_STATUS\r
 EFIAPI\r
 PeiAllocatePages (\r
-  IN CONST EFI_PEI_SERVICES           **PeiServices,\r
-  IN EFI_MEMORY_TYPE            MemoryType,\r
-  IN UINTN                      Pages,\r
-  OUT EFI_PHYSICAL_ADDRESS      *Memory\r
+  IN CONST EFI_PEI_SERVICES     **PeiServices,\r
+  IN       EFI_MEMORY_TYPE      MemoryType,\r
+  IN       UINTN                Pages,\r
+  OUT      EFI_PHYSICAL_ADDRESS *Memory\r
+  );\r
+\r
+/**\r
+  Frees memory pages.\r
+\r
+  @param[in] PeiServices        An indirect pointer to the EFI_PEI_SERVICES table published by the PEI Foundation.\r
+  @param[in] Memory             The base physical address of the pages to be freed.\r
+  @param[in] Pages              The number of contiguous 4 KB pages to free.\r
+\r
+  @retval EFI_SUCCESS           The requested pages were freed.\r
+  @retval EFI_INVALID_PARAMETER Memory is not a page-aligned address or Pages is invalid.\r
+  @retval EFI_NOT_FOUND         The requested memory pages were not allocated with\r
+                                AllocatePages().\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+PeiFreePages (\r
+  IN CONST EFI_PEI_SERVICES     **PeiServices,\r
+  IN EFI_PHYSICAL_ADDRESS       Memory,\r
+  IN UINTN                      Pages\r
   );\r
 \r
 /**\r
index d2897437d776b3bfb8e24d1699850004cbb7d1f1..3cd61906c3dff8a7166980d668e457e7af51ab86 100644 (file)
@@ -64,7 +64,8 @@ EFI_PEI_SERVICES  gPs = {
   PeiRegisterForShadow,\r
   PeiFfsFindSectionData3,\r
   PeiFfsGetFileInfo2,\r
-  PeiResetSystem2\r
+  PeiResetSystem2,\r
+  PeiFreePages,\r
 };\r
 \r
 /**\r
@@ -231,6 +232,11 @@ PeiCore (
       HandoffInformationTable->EfiFreeMemoryTop    = OldCoreData->FreePhysicalMemoryTop;\r
       HandoffInformationTable->EfiFreeMemoryBottom = HandoffInformationTable->EfiEndOfHobList + sizeof (EFI_HOB_GENERIC_HEADER);\r
 \r
+      //\r
+      // We need convert MemoryBaseAddress in memory allocation HOBs\r
+      //\r
+      ConvertMemoryAllocationHobs (OldCoreData);\r
+\r
       //\r
       // We need convert the PPI descriptor's pointer\r
       //\r
index 36b8c235b2358bc28fb450cb877c668048c48d55..082c37945802f405422d10f0cccd3d3aca87809c 100644 (file)
@@ -103,7 +103,7 @@ ConvertSinglePpiPointer (
 \r
 /**\r
 \r
-  Migrate PPI Pointers from the temporary memory stack to PEI installed memory.\r
+  Migrate PPI Pointers from the temporary memory to PEI installed memory.\r
 \r
   @param SecCoreData     Points to a data structure containing SEC to PEI handoff data, such as the size \r
                          and location of temporary RAM, the stack location and the BFV location.\r
@@ -121,6 +121,20 @@ ConvertPpiPointers (
 \r
   for (Index = 0; Index < PcdGet32 (PcdPeiCoreMaxPpiSupported); Index++) {\r
     if (Index < PrivateData->PpiData.PpiListEnd || Index > PrivateData->PpiData.NotifyListEnd) {\r
+      if (PrivateData->MemoryPages.Size != 0) {\r
+        //\r
+        // Convert PPI pointer in old memory pages\r
+        // It needs to be done before Convert PPI pointer in old Heap\r
+        //\r
+        ConvertSinglePpiPointer (\r
+          &PrivateData->PpiData.PpiListPtrs[Index],\r
+          (UINTN)PrivateData->MemoryPages.Base,\r
+          (UINTN)PrivateData->MemoryPages.Base + PrivateData->MemoryPages.Size,\r
+          PrivateData->MemoryPages.Offset,\r
+          PrivateData->MemoryPages.OffsetPositive\r
+          );\r
+      }\r
+\r
       //\r
       // Convert PPI pointer in old Heap\r
       //\r