]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdePkg/Library/PeiMemoryAllocationLib/MemoryAllocationLib.c
MdePkg PeiMemoryAllocationLib: Update InternalAllocateAlignedPages
[mirror_edk2.git] / MdePkg / Library / PeiMemoryAllocationLib / MemoryAllocationLib.c
index d711aa79cf91d7c6d19a032b362db0c0515e8e68..5cd24a38a3c66c6526618ee326453897a267fdab 100644 (file)
@@ -2,11 +2,11 @@
   Support routines for memory allocation routines \r
   based on PeiService for PEI phase drivers.\r
 \r
-  Copyright (c) 2006 - 2008, 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
-  http://opensource.org/licenses/bsd-license.php                                            \r
+  http://opensource.org/licenses/bsd-license.php.                                            \r
 \r
   THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,                     \r
   WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.             \r
@@ -21,6 +21,7 @@
 #include <Library/PeiServicesLib.h>\r
 #include <Library/BaseMemoryLib.h>\r
 #include <Library/DebugLib.h>\r
+#include <Library/HobLib.h>\r
 \r
 \r
 /**\r
@@ -53,6 +54,7 @@ InternalAllocatePages (
   if (EFI_ERROR (Status)) {\r
     return NULL;\r
   }\r
+\r
   return (VOID *) (UINTN) Memory;\r
 }\r
 \r
@@ -135,7 +137,7 @@ AllocateReservedPages (
   then ASSERT().\r
   If Pages is zero, then ASSERT().\r
  \r
-  @param  Buffer                Pointer to the buffer of pages to free.\r
+  @param  Buffer                The pointer to the buffer of pages to free.\r
   @param  Pages                 The number of 4 KB pages to free.\r
 \r
 **/\r
@@ -146,10 +148,11 @@ FreePages (
   IN UINTN  Pages\r
   )\r
 {\r
+  EFI_STATUS  Status;\r
+\r
   ASSERT (Pages != 0);\r
-  //\r
-  // PEI phase does not support to free pages, so leave it as NOP.\r
-  //\r
+  Status = PeiServicesFreePages ((EFI_PHYSICAL_ADDRESS) (UINTN) Buffer, Pages);\r
+  ASSERT_EFI_ERROR (Status);\r
 }\r
 \r
 /**\r
@@ -160,10 +163,12 @@ FreePages (
   If there is not enough memory at the specified alignment remaining to satisfy the request, then\r
   NULL is returned.\r
   If Alignment is not a power of two and Alignment is not zero, then ASSERT().\r
+  If Pages plus EFI_SIZE_TO_PAGES (Alignment) overflows, then ASSERT().\r
 \r
   @param  MemoryType            The type of memory to allocate.\r
   @param  Pages                 The number of 4 KB pages to allocate.\r
-  @param  Alignment             The requested alignment of the allocation.  Must be a power of two.\r
+  @param  Alignment             The requested alignment of the allocation.  \r
+                                Must be a power of two.\r
                                 If Alignment is zero, then byte alignment is used.\r
 \r
   @return A pointer to the allocated buffer or NULL if allocation fails.\r
@@ -176,8 +181,12 @@ InternalAllocateAlignedPages (
   IN UINTN            Alignment\r
   )\r
 {\r
-  VOID    *Memory;\r
-  UINTN   AlignmentMask;\r
+  EFI_STATUS            Status;\r
+  EFI_PHYSICAL_ADDRESS  Memory;\r
+  UINTN                 AlignedMemory;\r
+  UINTN                 AlignmentMask;\r
+  UINTN                 UnalignedPages;\r
+  UINTN                 RealPages;\r
 \r
   //\r
   // Alignment must be a power of two or zero.\r
@@ -187,20 +196,50 @@ InternalAllocateAlignedPages (
   if (Pages == 0) {\r
     return NULL;\r
   }\r
-  //\r
-  // Make sure that Pages plus EFI_SIZE_TO_PAGES (Alignment) does not overflow.\r
-  //\r
-  ASSERT (Pages <= (MAX_ADDRESS - EFI_SIZE_TO_PAGES (Alignment)));\r
-  //\r
-  // We would rather waste some memory to save PEI code size.\r
-  //\r
-  Memory = InternalAllocatePages (MemoryType, Pages + EFI_SIZE_TO_PAGES (Alignment));\r
-  if (Alignment == 0) {\r
-    AlignmentMask = Alignment;\r
+  if (Alignment > EFI_PAGE_SIZE) {\r
+    //\r
+    // Calculate the total number of pages since alignment is larger than page size.\r
+    //\r
+    AlignmentMask  = Alignment - 1;\r
+    RealPages      = Pages + EFI_SIZE_TO_PAGES (Alignment);\r
+    //\r
+    // Make sure that Pages plus EFI_SIZE_TO_PAGES (Alignment) does not overflow.\r
+    //\r
+    ASSERT (RealPages > Pages);\r
+\r
+    Status         = PeiServicesAllocatePages (MemoryType, RealPages, &Memory);\r
+    if (EFI_ERROR (Status)) {\r
+      return NULL;\r
+    }\r
+    AlignedMemory  = ((UINTN) Memory + AlignmentMask) & ~AlignmentMask;\r
+    UnalignedPages = EFI_SIZE_TO_PAGES (AlignedMemory - (UINTN) Memory);\r
+    if (UnalignedPages > 0) {\r
+      //\r
+      // Free first unaligned page(s).\r
+      //\r
+      Status = PeiServicesFreePages (Memory, UnalignedPages);\r
+      ASSERT_EFI_ERROR (Status);\r
+    }\r
+    Memory         = AlignedMemory + EFI_PAGES_TO_SIZE (Pages);\r
+    UnalignedPages = RealPages - Pages - UnalignedPages;\r
+    if (UnalignedPages > 0) {\r
+      //\r
+      // Free last unaligned page(s).\r
+      //\r
+      Status = PeiServicesFreePages (Memory, UnalignedPages);\r
+      ASSERT_EFI_ERROR (Status);\r
+    }\r
   } else {\r
-    AlignmentMask = Alignment - 1;  \r
+    //\r
+    // Do not over-allocate pages in this case.\r
+    //\r
+    Status = PeiServicesAllocatePages (MemoryType, Pages, &Memory);\r
+    if (EFI_ERROR (Status)) {\r
+      return NULL;\r
+    }\r
+    AlignedMemory  = (UINTN) Memory;\r
   }\r
-  return (VOID *) (UINTN) (((UINTN) Memory + AlignmentMask) & ~AlignmentMask);\r
+  return (VOID *) AlignedMemory;\r
 }\r
 \r
 /**\r
@@ -212,9 +251,11 @@ InternalAllocateAlignedPages (
   request, then NULL is returned.\r
   \r
   If Alignment is not a power of two and Alignment is not zero, then ASSERT().\r
+  If Pages plus EFI_SIZE_TO_PAGES (Alignment) overflows, then ASSERT().\r
 \r
   @param  Pages                 The number of 4 KB pages to allocate.\r
-  @param  Alignment             The requested alignment of the allocation.  Must be a power of two.\r
+  @param  Alignment             The requested alignment of the allocation.  \r
+                                Must be a power of two.\r
                                 If Alignment is zero, then byte alignment is used.\r
 \r
   @return A pointer to the allocated buffer or NULL if allocation fails.\r
@@ -239,9 +280,11 @@ AllocateAlignedPages (
   request, then NULL is returned.\r
   \r
   If Alignment is not a power of two and Alignment is not zero, then ASSERT().\r
+  If Pages plus EFI_SIZE_TO_PAGES (Alignment) overflows, then ASSERT().\r
 \r
   @param  Pages                 The number of 4 KB pages to allocate.\r
-  @param  Alignment             The requested alignment of the allocation.  Must be a power of two.\r
+  @param  Alignment             The requested alignment of the allocation.  \r
+                                Must be a power of two.\r
                                 If Alignment is zero, then byte alignment is used.\r
 \r
   @return A pointer to the allocated buffer or NULL if allocation fails.\r
@@ -266,9 +309,11 @@ AllocateAlignedRuntimePages (
   request, then NULL is returned.\r
   \r
   If Alignment is not a power of two and Alignment is not zero, then ASSERT().\r
+  If Pages plus EFI_SIZE_TO_PAGES (Alignment) overflows, then ASSERT().\r
 \r
   @param  Pages                 The number of 4 KB pages to allocate.\r
-  @param  Alignment             The requested alignment of the allocation.  Must be a power of two.\r
+  @param  Alignment             The requested alignment of the allocation.  \r
+                                Must be a power of two.\r
                                 If Alignment is zero, then byte alignment is used.\r
 \r
   @return A pointer to the allocated buffer or NULL if allocation fails.\r
@@ -297,7 +342,7 @@ AllocateAlignedReservedPages (
   Library, then ASSERT().\r
   If Pages is zero, then ASSERT().\r
   \r
-  @param  Buffer                Pointer to the buffer of pages to free.\r
+  @param  Buffer                The pointer to the buffer of pages to free.\r
   @param  Pages                 The number of 4 KB pages to free.\r
 \r
 **/\r
@@ -308,10 +353,11 @@ FreeAlignedPages (
   IN UINTN  Pages\r
   )\r
 {\r
+  EFI_STATUS  Status;\r
+\r
   ASSERT (Pages != 0);\r
-  //\r
-  // PEI phase does not support to free pages, so leave it as NOP.\r
-  //\r
+  Status = PeiServicesFreePages ((EFI_PHYSICAL_ADDRESS) (UINTN) Buffer, Pages);\r
+  ASSERT_EFI_ERROR (Status);\r
 }\r
 \r
 /**\r
@@ -654,8 +700,8 @@ AllocateReservedCopyPool (
   @param  PoolType       The type of pool to allocate.\r
   @param  OldSize        The size, in bytes, of OldBuffer.\r
   @param  NewSize        The size, in bytes, of the buffer to reallocate.\r
-  @param  OldBuffer      The buffer to copy to the allocated buffer.  This is an optional \r
-                         parameter that may be NULL.\r
+  @param  OldBuffer      The buffer to copy to the allocated buffer.  This is an \r
+                         optional parameter that may be NULL.\r
 \r
   @return A pointer to the allocated buffer or NULL if allocation fails.\r
 \r
@@ -757,8 +803,8 @@ ReallocateRuntimePool (
 \r
   @param  OldSize        The size, in bytes, of OldBuffer.\r
   @param  NewSize        The size, in bytes, of the buffer to reallocate.\r
-  @param  OldBuffer      The buffer to copy to the allocated buffer.  This is an optional \r
-                         parameter that may be NULL.\r
+  @param  OldBuffer      The buffer to copy to the allocated buffer.  This is an \r
+                         optional parameter that may be NULL.\r
 \r
   @return A pointer to the allocated buffer or NULL if allocation fails.\r
 \r
@@ -785,7 +831,7 @@ ReallocateReservedPool (
   If Buffer was not allocated with a pool allocation function in the Memory Allocation Library,\r
   then ASSERT().\r
 \r
-  @param  Buffer                Pointer to the buffer to free.\r
+  @param  Buffer                The pointer to the buffer to free.\r
 \r
 **/\r
 VOID\r