]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdeModulePkg/Core/Pei/Memory/MemoryServices.c
MdeModulePkg/PeiCore: honour minimal runtime allocation granularity
[mirror_edk2.git] / MdeModulePkg / Core / Pei / Memory / MemoryServices.c
index f848fbeccfb982c52dd2df5323253bc39e4c888a..719372e0612801176d6bd47d68c0c12b791b69f0 100644 (file)
@@ -1,7 +1,7 @@
 /** @file\r
   EFI PEI Core memory services\r
   \r
-Copyright (c) 2006 - 2010, Intel Corporation. All rights reserved.<BR>\r
+Copyright (c) 2006 - 2015, 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
@@ -96,7 +96,7 @@ PeiInstallPeiMemory (
   // \r
   if (PrivateData->PeiMemoryInstalled) {\r
     DEBUG ((EFI_D_ERROR, "ERROR: PeiInstallPeiMemory is called more than once!\n"));\r
-    ASSERT (PrivateData->PeiMemoryInstalled);\r
+    ASSERT (FALSE);\r
     return EFI_SUCCESS;\r
   }\r
   \r
@@ -123,7 +123,7 @@ PeiInstallPeiMemory (
   @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, or EfiACPIMemoryNVS.\r
+                                EfiACPIReclaimMemory, EfiReservedMemoryType, or EfiACPIMemoryNVS.\r
 \r
 **/\r
 EFI_STATUS\r
@@ -140,6 +140,8 @@ PeiAllocatePages (
   EFI_PHYSICAL_ADDRESS                    *FreeMemoryTop;\r
   EFI_PHYSICAL_ADDRESS                    *FreeMemoryBottom;\r
   UINTN                                   RemainingPages;\r
+  UINTN                                   Granularity;\r
+  UINTN                                   Padding;\r
 \r
   if ((MemoryType != EfiLoaderCode) &&\r
       (MemoryType != EfiLoaderData) &&\r
@@ -148,10 +150,25 @@ PeiAllocatePages (
       (MemoryType != EfiBootServicesCode) &&\r
       (MemoryType != EfiBootServicesData) &&\r
       (MemoryType != EfiACPIReclaimMemory) &&\r
+      (MemoryType != EfiReservedMemoryType) &&\r
       (MemoryType != EfiACPIMemoryNVS)) {\r
     return EFI_INVALID_PARAMETER;\r
   }\r
 \r
+  Granularity = DEFAULT_PAGE_ALLOCATION_GRANULARITY;\r
+\r
+  if  (RUNTIME_PAGE_ALLOCATION_GRANULARITY > DEFAULT_PAGE_ALLOCATION_GRANULARITY &&\r
+       (MemoryType == EfiACPIReclaimMemory   ||\r
+        MemoryType == EfiACPIMemoryNVS       ||\r
+        MemoryType == EfiRuntimeServicesCode ||\r
+        MemoryType == EfiRuntimeServicesData)) {\r
+\r
+    Granularity = RUNTIME_PAGE_ALLOCATION_GRANULARITY;\r
+\r
+    DEBUG ((DEBUG_INFO, "AllocatePages: aligning allocation to %d KB\n",\r
+      Granularity / SIZE_1KB));\r
+  }\r
+\r
   PrivateData = PEI_CORE_INSTANCE_FROM_PS_THIS (PeiServices);\r
   Hob.Raw     = PrivateData->HobList.Raw;\r
   \r
@@ -175,19 +192,42 @@ PeiAllocatePages (
   }\r
 \r
   //\r
-  // Check to see if on 4k boundary, If not aligned, make the allocation aligned.\r
+  // Check to see if on correct boundary for the memory type.\r
+  // If not aligned, make the allocation aligned.\r
   //\r
-  *(FreeMemoryTop) -= *(FreeMemoryTop) & 0xFFF;\r
-  \r
+  Padding = *(FreeMemoryTop) & (Granularity - 1);\r
+  if ((UINTN) (*FreeMemoryTop - *FreeMemoryBottom) < Padding) {\r
+    DEBUG ((DEBUG_ERROR, "AllocatePages failed: Out of space after padding.\n"));\r
+    return EFI_OUT_OF_RESOURCES;\r
+  }\r
+\r
+  *(FreeMemoryTop) -= Padding;\r
+  if (Padding >= EFI_PAGE_SIZE) {\r
+    //\r
+    // Create a memory allocation HOB to cover\r
+    // the pages that we will lose to rounding\r
+    //\r
+    BuildMemoryAllocationHob (\r
+      *(FreeMemoryTop),\r
+      Padding & ~(UINTN)EFI_PAGE_MASK,\r
+      EfiConventionalMemory\r
+      );\r
+  }\r
+\r
   //\r
-  // Verify that there is sufficient memory to satisfy the allocation\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
-  RemainingPages = EFI_SIZE_TO_PAGES ((UINTN) (*FreeMemoryTop - *FreeMemoryBottom));\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
   //\r
-  // For page allocation, the overhead sizeof (EFI_HOB_MEMORY_ALLOCATION) needs one extra page.\r
-  // So the number of remaining pages needs to be greater than that of the request pages.\r
+  // The number of remaining pages needs to be greater than or equal to that of the request pages.\r
   //\r
-  if (RemainingPages <= Pages) {\r
+  Pages = ALIGN_VALUE (Pages, EFI_SIZE_TO_PAGES (Granularity));\r
+  if (RemainingPages < Pages) {\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
@@ -217,7 +257,7 @@ PeiAllocatePages (
 \r
 /**\r
 \r
-  Pool allocation service. Before permenent memory is discoveried, the pool will \r
+  Pool allocation service. Before permanent memory is discoveried, the pool will\r
   be allocated the heap in the temporary memory. Genenrally, the size of heap in temporary \r
   memory does not exceed to 64K, so the biggest pool size could be allocated is \r
   64K.\r
@@ -249,9 +289,9 @@ PeiAllocatePool (
   \r
   //\r
   // Generally, the size of heap in temporary memory does not exceed to 64K,\r
-  // so the maxmium size of pool is 0x10000 - sizeof (EFI_HOB_MEMORY_POOL)\r
+  // HobLength is multiples of 8 bytes, so the maxmium size of pool is 0xFFF8 - sizeof (EFI_HOB_MEMORY_POOL)\r
   //\r
-  if (Size >= (0x10000 - sizeof (EFI_HOB_MEMORY_POOL))) {\r
+  if (Size > (0xFFF8 - sizeof (EFI_HOB_MEMORY_POOL))) {\r
     return EFI_OUT_OF_RESOURCES;\r
   }\r
   \r
@@ -260,6 +300,7 @@ PeiAllocatePool (
              (UINT16)(sizeof (EFI_HOB_MEMORY_POOL) + Size),\r
              (VOID **)&Hob\r
              );\r
+  ASSERT_EFI_ERROR (Status);\r
   *Buffer = Hob+1;  \r
 \r
   return Status;\r