]> git.proxmox.com Git - mirror_edk2.git/commitdiff
MdeModulePkg: use correct granularity when allocating pool pages
authorArd Biesheuvel <ard.biesheuvel@linaro.org>
Fri, 6 Mar 2015 02:54:05 +0000 (02:54 +0000)
committerlgao4 <lgao4@Edk2>
Fri, 6 Mar 2015 02:54:05 +0000 (02:54 +0000)
After fixing the sanity check on the alignment of the runtime regions
in SVN revision #16630 ("MdeModulePkg/DxeMain: Fix wrong sanity check
in CoreTerminateMemoryMap()"), it is no longer possible to define a
runtime allocation alignment that is different from the boot time
allocation alignment.

For instance, #defining the following in MdeModulePkg/Core/Dxe/Mem/Imem.h
will hit the ASSERT () in MdeModulePkg/Core/Dxe/Mem/Page.c:1798

  #define EFI_ACPI_RUNTIME_PAGE_ALLOCATION_ALIGNMENT  (SIZE_64KB)
  #define DEFAULT_PAGE_ALLOCATION                     (EFI_PAGE_SIZE)

(which is needed for 64-bit ARM to adhere to the Server Base Boot
Requirements [SBBR], which stipulates that all runtime memory regions
should be naturally aligned multiples of 64 KB)

This patch fixes this use case by ensuring that the backing for the memory
pools is allocated in appropriate chunks for the memory type.

Contributed-under: TianoCore Contribution Agreement 1.0
Signed-off-by: Ard Biesheuvel <ard.biesheuvel@linaro.org>
Reviewed-by: Liming Gao <liming.gao@intel.com>
git-svn-id: https://svn.code.sf.net/p/edk2/code/trunk/edk2@17011 6f19259b-4bc3-4df7-8a09-765794883524

MdeModulePkg/Core/Dxe/Mem/Pool.c

index a40ddd51bd13b26c3af736b6a81ecbbb0e8f39f3..f99293da5c51a2eb7dd04b565102f02e548658af 100644 (file)
@@ -274,9 +274,20 @@ CoreAllocatePoolI (
   UINTN       FSize;\r
   UINTN       Offset;\r
   UINTN       NoPages;\r
+  UINTN       Granularity;\r
 \r
   ASSERT_LOCKED (&gMemoryLock);\r
 \r
+  if  (PoolType == EfiACPIReclaimMemory   ||\r
+       PoolType == EfiACPIMemoryNVS       ||\r
+       PoolType == EfiRuntimeServicesCode ||\r
+       PoolType == EfiRuntimeServicesData) {\r
+\r
+    Granularity = EFI_ACPI_RUNTIME_PAGE_ALLOCATION_ALIGNMENT;\r
+  } else {\r
+    Granularity = DEFAULT_PAGE_ALLOCATION;\r
+  }\r
+\r
   //\r
   // Adjust the size by the pool header & tail overhead\r
   //\r
@@ -301,9 +312,9 @@ CoreAllocatePoolI (
   // (slow)\r
   //\r
   if (Index >= MAX_POOL_LIST) {\r
-    NoPages = EFI_SIZE_TO_PAGES(Size) + EFI_SIZE_TO_PAGES (DEFAULT_PAGE_ALLOCATION) - 1;\r
-    NoPages &= ~(UINTN)(EFI_SIZE_TO_PAGES (DEFAULT_PAGE_ALLOCATION) - 1);\r
-    Head = CoreAllocatePoolPages (PoolType, NoPages, DEFAULT_PAGE_ALLOCATION);\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
     goto Done;\r
   }\r
 \r
@@ -315,7 +326,7 @@ CoreAllocatePoolI (
     //\r
     // Get another page\r
     //\r
-    NewPage = CoreAllocatePoolPages(PoolType, EFI_SIZE_TO_PAGES (DEFAULT_PAGE_ALLOCATION), DEFAULT_PAGE_ALLOCATION);\r
+    NewPage = CoreAllocatePoolPages(PoolType, EFI_SIZE_TO_PAGES (Granularity), Granularity);\r
     if (NewPage == NULL) {\r
       goto Done;\r
     }\r
@@ -324,11 +335,11 @@ CoreAllocatePoolI (
     // Carve up new page into free pool blocks\r
     //\r
     Offset = 0;\r
-    while (Offset < DEFAULT_PAGE_ALLOCATION) {\r
+    while (Offset < Granularity) {\r
       ASSERT (Index < MAX_POOL_LIST);\r
       FSize = LIST_TO_SIZE(Index);\r
 \r
-      while (Offset + FSize <= DEFAULT_PAGE_ALLOCATION) {\r
+      while (Offset + FSize <= Granularity) {\r
         Free = (POOL_FREE *) &NewPage[Offset];\r
         Free->Signature = POOL_FREE_SIGNATURE;\r
         Free->Index     = (UINT32)Index;\r
@@ -339,7 +350,7 @@ CoreAllocatePoolI (
       Index -= 1;\r
     }\r
 \r
-    ASSERT (Offset == DEFAULT_PAGE_ALLOCATION);\r
+    ASSERT (Offset == Granularity);\r
     Index = SIZE_TO_LIST(Size);\r
   }\r
 \r
@@ -467,6 +478,7 @@ CoreFreePoolI (
   UINTN       FSize;\r
   UINTN       Offset;\r
   BOOLEAN     AllFree;\r
+  UINTN       Granularity;\r
 \r
   ASSERT(Buffer != NULL);\r
   //\r
@@ -508,6 +520,16 @@ CoreFreePoolI (
   Pool->Used -= Size;\r
   DEBUG ((DEBUG_POOL, "FreePool: %p (len %lx) %,ld\n", Head->Data, (UINT64)(Head->Size - POOL_OVERHEAD), (UINT64) Pool->Used));\r
 \r
+  if  (Head->Type == EfiACPIReclaimMemory   ||\r
+       Head->Type == EfiACPIMemoryNVS       ||\r
+       Head->Type == EfiRuntimeServicesCode ||\r
+       Head->Type == EfiRuntimeServicesData) {\r
+\r
+    Granularity = EFI_ACPI_RUNTIME_PAGE_ALLOCATION_ALIGNMENT;\r
+  } else {\r
+    Granularity = DEFAULT_PAGE_ALLOCATION;\r
+  }\r
+\r
   //\r
   // Determine the pool list\r
   //\r
@@ -522,8 +544,8 @@ CoreFreePoolI (
     //\r
     // Return the memory pages back to free memory\r
     //\r
-    NoPages = EFI_SIZE_TO_PAGES(Size) + EFI_SIZE_TO_PAGES (DEFAULT_PAGE_ALLOCATION) - 1;\r
-    NoPages &= ~(UINTN)(EFI_SIZE_TO_PAGES (DEFAULT_PAGE_ALLOCATION) - 1);\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
 \r
   } else {\r
@@ -541,7 +563,7 @@ CoreFreePoolI (
     // See if all the pool entries in the same page as Free are freed pool\r
     // entries\r
     //\r
-    NewPage = (CHAR8 *)((UINTN)Free & ~((DEFAULT_PAGE_ALLOCATION) -1));\r
+    NewPage = (CHAR8 *)((UINTN)Free & ~(Granularity - 1));\r
     Free = (POOL_FREE *) &NewPage[0];\r
     ASSERT(Free != NULL);\r
 \r
@@ -552,9 +574,9 @@ CoreFreePoolI (
       AllFree = TRUE;\r
       Offset = 0;\r
 \r
-      while ((Offset < DEFAULT_PAGE_ALLOCATION) && (AllFree)) {\r
+      while ((Offset < Granularity) && (AllFree)) {\r
         FSize = LIST_TO_SIZE(Index);\r
-        while (Offset + FSize <= DEFAULT_PAGE_ALLOCATION) {\r
+        while (Offset + FSize <= Granularity) {\r
           Free = (POOL_FREE *) &NewPage[Offset];\r
           ASSERT(Free != NULL);\r
           if (Free->Signature != POOL_FREE_SIGNATURE) {\r
@@ -577,9 +599,9 @@ CoreFreePoolI (
         Index = Free->Index;\r
         Offset = 0;\r
 \r
-        while (Offset < DEFAULT_PAGE_ALLOCATION) {\r
+        while (Offset < Granularity) {\r
           FSize = LIST_TO_SIZE(Index);\r
-          while (Offset + FSize <= DEFAULT_PAGE_ALLOCATION) {\r
+          while (Offset + FSize <= Granularity) {\r
             Free = (POOL_FREE *) &NewPage[Offset];\r
             ASSERT(Free != NULL);\r
             RemoveEntryList (&Free->Link);\r
@@ -591,7 +613,7 @@ CoreFreePoolI (
         //\r
         // Free the page\r
         //\r
-        CoreFreePoolPages ((EFI_PHYSICAL_ADDRESS) (UINTN)NewPage, EFI_SIZE_TO_PAGES (DEFAULT_PAGE_ALLOCATION));\r
+        CoreFreePoolPages ((EFI_PHYSICAL_ADDRESS) (UINTN)NewPage, EFI_SIZE_TO_PAGES (Granularity));\r
       }\r
     }\r
   }\r