]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdeModulePkg/Core/Dxe/Mem/Pool.c
MdeModulePkg/DxeCore: switch to MdePkg allocation granularity macros
[mirror_edk2.git] / MdeModulePkg / Core / Dxe / Mem / Pool.c
index e5fee1abfc56dec170008f9075d14771c1b7e1e0..5248ee2e6c0221cc2fbd713078b2a4a621c03da0 100644 (file)
@@ -1,7 +1,7 @@
 /** @file\r
   UEFI Memory pool management functions.\r
 \r
-Copyright (c) 2006 - 2015, Intel Corporation. All rights reserved.<BR>\r
+Copyright (c) 2006 - 2016, 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
@@ -15,6 +15,8 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
 #include "DxeMain.h"\r
 #include "Imem.h"\r
 \r
+STATIC EFI_LOCK mPoolMemoryLock = EFI_INITIALIZE_LOCK_VARIABLE (TPL_NOTIFY);\r
+\r
 #define POOL_FREE_SIGNATURE   SIGNATURE_32('p','f','r','0')\r
 typedef struct {\r
   UINT32          Signature;\r
@@ -52,13 +54,13 @@ typedef struct {
 // as we would in a strict power-of-2 sequence\r
 //\r
 STATIC CONST UINT16 mPoolSizeTable[] = {\r
-  64, 128, 192, 320, 512, 832, 1344, 2176, 3520, 5696, 9216, 14912, 24128\r
+  128, 256, 384, 640, 1024, 1664, 2688, 4352, 7040, 11392, 18432, 29824\r
 };\r
 \r
 #define SIZE_TO_LIST(a)   (GetPoolIndexFromSize (a))\r
 #define LIST_TO_SIZE(a)   (mPoolSizeTable [a])\r
 \r
-#define MAX_POOL_LIST     (sizeof (mPoolSizeTable) / sizeof (mPoolSizeTable[0]))\r
+#define MAX_POOL_LIST     (ARRAY_SIZE (mPoolSizeTable))\r
 \r
 #define MAX_POOL_SIZE     (MAX_ADDRESS - POOL_OVERHEAD)\r
 \r
@@ -197,8 +199,9 @@ LookupPoolHead (
   @param  Buffer                 The address to return a pointer to the allocated\r
                                  pool\r
 \r
-  @retval EFI_INVALID_PARAMETER  PoolType not valid or Buffer is NULL. \r
-                                 PoolType was EfiPersistentMemory.\r
+  @retval EFI_INVALID_PARAMETER  Buffer is NULL.\r
+                                 PoolType is in the range EfiMaxMemoryType..0x6FFFFFFF.\r
+                                 PoolType is EfiPersistentMemory.\r
   @retval EFI_OUT_OF_RESOURCES   Size exceeds max pool size or allocation failed.\r
   @retval EFI_SUCCESS            Pool successfully allocated.\r
 \r
@@ -238,13 +241,13 @@ CoreInternalAllocatePool (
   //\r
   // Acquire the memory lock and make the allocation\r
   //\r
-  Status = CoreAcquireLockOrFail (&gMemoryLock);\r
+  Status = CoreAcquireLockOrFail (&mPoolMemoryLock);\r
   if (EFI_ERROR (Status)) {\r
     return EFI_OUT_OF_RESOURCES;\r
   }\r
 \r
   *Buffer = CoreAllocatePoolI (PoolType, Size);\r
-  CoreReleaseMemoryLock ();\r
+  CoreReleaseLock (&mPoolMemoryLock);\r
   return (*Buffer != NULL) ? EFI_SUCCESS : EFI_OUT_OF_RESOURCES;\r
 }\r
 \r
@@ -256,7 +259,9 @@ CoreInternalAllocatePool (
   @param  Buffer                 The address to return a pointer to the allocated\r
                                  pool\r
 \r
-  @retval EFI_INVALID_PARAMETER  PoolType not valid or Buffer is NULL. \r
+  @retval EFI_INVALID_PARAMETER  Buffer is NULL.\r
+                                 PoolType is in the range EfiMaxMemoryType..0x6FFFFFFF.\r
+                                 PoolType is EfiPersistentMemory.\r
   @retval EFI_OUT_OF_RESOURCES   Size exceeds max pool size or allocation failed.\r
   @retval EFI_SUCCESS            Pool successfully allocated.\r
 \r
@@ -273,11 +278,45 @@ CoreAllocatePool (
 \r
   Status = CoreInternalAllocatePool (PoolType, Size, Buffer);\r
   if (!EFI_ERROR (Status)) {\r
-    CoreUpdateProfile ((EFI_PHYSICAL_ADDRESS) (UINTN) RETURN_ADDRESS (0), MemoryProfileActionAllocatePool, PoolType, Size, *Buffer);\r
+    CoreUpdateProfile (\r
+      (EFI_PHYSICAL_ADDRESS) (UINTN) RETURN_ADDRESS (0),\r
+      MemoryProfileActionAllocatePool,\r
+      PoolType,\r
+      Size,\r
+      *Buffer,\r
+      NULL\r
+      );\r
+    InstallMemoryAttributesTableOnMemoryAllocation (PoolType);\r
   }\r
   return Status;\r
 }\r
 \r
+STATIC\r
+VOID *\r
+CoreAllocatePoolPagesI (\r
+  IN EFI_MEMORY_TYPE    PoolType,\r
+  IN UINTN              NoPages,\r
+  IN UINTN              Granularity\r
+  )\r
+{\r
+  VOID        *Buffer;\r
+  EFI_STATUS  Status;\r
+\r
+  Status = CoreAcquireLockOrFail (&gMemoryLock);\r
+  if (EFI_ERROR (Status)) {\r
+    return NULL;\r
+  }\r
+\r
+  Buffer = CoreAllocatePoolPages (PoolType, NoPages, Granularity);\r
+  CoreReleaseMemoryLock ();\r
+\r
+  if (Buffer != NULL) {\r
+    ApplyMemoryProtectionPolicy (EfiConventionalMemory, PoolType,\r
+      (EFI_PHYSICAL_ADDRESS)(UINTN)Buffer, EFI_PAGES_TO_SIZE (NoPages));\r
+  }\r
+  return Buffer;\r
+}\r
+\r
 /**\r
   Internal function to allocate pool of a particular type.\r
   Caller must have the memory lock held\r
@@ -306,16 +345,16 @@ CoreAllocatePoolI (
   UINTN       NoPages;\r
   UINTN       Granularity;\r
 \r
-  ASSERT_LOCKED (&gMemoryLock);\r
+  ASSERT_LOCKED (&mPoolMemoryLock);\r
 \r
   if  (PoolType == EfiACPIReclaimMemory   ||\r
        PoolType == EfiACPIMemoryNVS       ||\r
        PoolType == EfiRuntimeServicesCode ||\r
        PoolType == EfiRuntimeServicesData) {\r
 \r
-    Granularity = EFI_ACPI_RUNTIME_PAGE_ALLOCATION_ALIGNMENT;\r
+    Granularity = RUNTIME_PAGE_ALLOCATION_GRANULARITY;\r
   } else {\r
-    Granularity = DEFAULT_PAGE_ALLOCATION;\r
+    Granularity = DEFAULT_PAGE_ALLOCATION_GRANULARITY;\r
   }\r
 \r
   //\r
@@ -344,7 +383,7 @@ CoreAllocatePoolI (
   if (Index >= SIZE_TO_LIST (Granularity)) {\r
     NoPages = EFI_SIZE_TO_PAGES(Size) + EFI_SIZE_TO_PAGES (Granularity) - 1;\r
     NoPages &= ~(UINTN)(EFI_SIZE_TO_PAGES (Granularity) - 1);\r
-    Head = CoreAllocatePoolPages (PoolType, NoPages, Granularity);\r
+    Head = CoreAllocatePoolPagesI (PoolType, NoPages, Granularity);\r
     goto Done;\r
   }\r
 \r
@@ -372,7 +411,7 @@ CoreAllocatePoolI (
     //\r
     // Get another page\r
     //\r
-    NewPage = CoreAllocatePoolPages(PoolType, EFI_SIZE_TO_PAGES (Granularity), Granularity);\r
+    NewPage = CoreAllocatePoolPages(PoolType, EFI_SIZE_TO_PAGES (Granularity), Granularity);\r
     if (NewPage == NULL) {\r
       goto Done;\r
     }\r
@@ -456,6 +495,7 @@ Done:
   Frees pool.\r
 \r
   @param  Buffer                 The allocated pool entry to free\r
+  @param  PoolType               Pointer to pool type\r
 \r
   @retval EFI_INVALID_PARAMETER  Buffer is not a valid value.\r
   @retval EFI_SUCCESS            Pool successfully freed.\r
@@ -464,7 +504,8 @@ Done:
 EFI_STATUS\r
 EFIAPI\r
 CoreInternalFreePool (\r
-  IN VOID        *Buffer\r
+  IN VOID               *Buffer,\r
+  OUT EFI_MEMORY_TYPE   *PoolType OPTIONAL\r
   )\r
 {\r
   EFI_STATUS Status;\r
@@ -473,9 +514,9 @@ CoreInternalFreePool (
     return EFI_INVALID_PARAMETER;\r
   }\r
 \r
-  CoreAcquireMemoryLock ();\r
-  Status = CoreFreePoolI (Buffer);\r
-  CoreReleaseMemoryLock ();\r
+  CoreAcquireLock (&mPoolMemoryLock);\r
+  Status = CoreFreePoolI (Buffer, PoolType);\r
+  CoreReleaseLock (&mPoolMemoryLock);\r
   return Status;\r
 }\r
 \r
@@ -494,20 +535,46 @@ CoreFreePool (
   IN VOID  *Buffer\r
   )\r
 {\r
-  EFI_STATUS  Status;\r
+  EFI_STATUS        Status;\r
+  EFI_MEMORY_TYPE   PoolType;\r
 \r
-  Status = CoreInternalFreePool (Buffer);\r
+  Status = CoreInternalFreePool (Buffer, &PoolType);\r
   if (!EFI_ERROR (Status)) {\r
-    CoreUpdateProfile ((EFI_PHYSICAL_ADDRESS) (UINTN) RETURN_ADDRESS (0), MemoryProfileActionFreePool, (EFI_MEMORY_TYPE) 0, 0, Buffer);\r
+    CoreUpdateProfile (\r
+      (EFI_PHYSICAL_ADDRESS) (UINTN) RETURN_ADDRESS (0),\r
+      MemoryProfileActionFreePool,\r
+      PoolType,\r
+      0,\r
+      Buffer,\r
+      NULL\r
+      );\r
+    InstallMemoryAttributesTableOnMemoryAllocation (PoolType);\r
   }\r
   return Status;\r
 }\r
 \r
+STATIC\r
+VOID\r
+CoreFreePoolPagesI (\r
+  IN EFI_MEMORY_TYPE        PoolType,\r
+  IN EFI_PHYSICAL_ADDRESS   Memory,\r
+  IN UINTN                  NoPages\r
+  )\r
+{\r
+  CoreAcquireMemoryLock ();\r
+  CoreFreePoolPages (Memory, NoPages);\r
+  CoreReleaseMemoryLock ();\r
+\r
+  ApplyMemoryProtectionPolicy (PoolType, EfiConventionalMemory,\r
+    (EFI_PHYSICAL_ADDRESS)(UINTN)Memory, EFI_PAGES_TO_SIZE (NoPages));\r
+}\r
+\r
 /**\r
   Internal function to free a pool entry.\r
   Caller must have the memory lock held\r
 \r
   @param  Buffer                 The allocated pool entry to free\r
+  @param  PoolType               Pointer to pool type\r
 \r
   @retval EFI_INVALID_PARAMETER  Buffer not valid\r
   @retval EFI_SUCCESS            Buffer successfully freed.\r
@@ -515,7 +582,8 @@ CoreFreePool (
 **/\r
 EFI_STATUS\r
 CoreFreePoolI (\r
-  IN VOID       *Buffer\r
+  IN VOID               *Buffer,\r
+  OUT EFI_MEMORY_TYPE   *PoolType OPTIONAL\r
   )\r
 {\r
   POOL        *Pool;\r
@@ -549,7 +617,7 @@ CoreFreePoolI (
   //\r
   ASSERT (Tail->Signature == POOL_TAIL_SIGNATURE);\r
   ASSERT (Head->Size == Tail->Size);\r
-  ASSERT_LOCKED (&gMemoryLock);\r
+  ASSERT_LOCKED (&mPoolMemoryLock);\r
 \r
   if (Tail->Signature != POOL_TAIL_SIGNATURE) {\r
     return EFI_INVALID_PARAMETER;\r
@@ -575,9 +643,13 @@ CoreFreePoolI (
        Head->Type == EfiRuntimeServicesCode ||\r
        Head->Type == EfiRuntimeServicesData) {\r
 \r
-    Granularity = EFI_ACPI_RUNTIME_PAGE_ALLOCATION_ALIGNMENT;\r
+    Granularity = RUNTIME_PAGE_ALLOCATION_GRANULARITY;\r
   } else {\r
-    Granularity = DEFAULT_PAGE_ALLOCATION;\r
+    Granularity = DEFAULT_PAGE_ALLOCATION_GRANULARITY;\r
+  }\r
+\r
+  if (PoolType != NULL) {\r
+    *PoolType = Head->Type;\r
   }\r
 \r
   //\r
@@ -596,7 +668,7 @@ CoreFreePoolI (
     //\r
     NoPages = EFI_SIZE_TO_PAGES(Size) + EFI_SIZE_TO_PAGES (Granularity) - 1;\r
     NoPages &= ~(UINTN)(EFI_SIZE_TO_PAGES (Granularity) - 1);\r
-    CoreFreePoolPages ((EFI_PHYSICAL_ADDRESS) (UINTN) Head, NoPages);\r
+    CoreFreePoolPagesI (Pool->MemoryType, (EFI_PHYSICAL_ADDRESS) (UINTN) Head, NoPages);\r
 \r
   } else {\r
 \r
@@ -652,19 +724,20 @@ CoreFreePoolI (
         //\r
         // Free the page\r
         //\r
-        CoreFreePoolPages ((EFI_PHYSICAL_ADDRESS) (UINTN)NewPage, EFI_SIZE_TO_PAGES (Granularity));\r
+        CoreFreePoolPagesI (Pool->MemoryType, (EFI_PHYSICAL_ADDRESS) (UINTN)NewPage,\r
+          EFI_SIZE_TO_PAGES (Granularity));\r
       }\r
     }\r
   }\r
 \r
   //\r
-  // If this is an OS specific memory type, then check to see if the last\r
+  // If this is an OS/OEM specific memory type, then check to see if the last\r
   // portion of that memory type has been freed.  If it has, then free the\r
   // list entry for that memory type\r
   //\r
-  if ((INT32)Pool->MemoryType < 0 && Pool->Used == 0) {\r
+  if (((UINT32) Pool->MemoryType >= MEMORY_TYPE_OEM_RESERVED_MIN) && Pool->Used == 0) {\r
     RemoveEntryList (&Pool->Link);\r
-    CoreFreePoolI (Pool);\r
+    CoreFreePoolI (Pool, NULL);\r
   }\r
 \r
   return EFI_SUCCESS;\r