]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdeModulePkg/Core/Dxe/Mem/Pool.c
MdeModulePkg: Replace BSD License with BSD+Patent License
[mirror_edk2.git] / MdeModulePkg / Core / Dxe / Mem / Pool.c
index df9a1d28dff50006bb4128238e09ffdb92d2f9de..734fc94bf6129ad20f3e8846c9cf13aaee7266ab 100644 (file)
@@ -1,14 +1,8 @@
 /** @file\r
   UEFI Memory pool management functions.\r
 \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
-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
+Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>\r
+SPDX-License-Identifier: BSD-2-Clause-Patent\r
 \r
 **/\r
 \r
@@ -26,7 +20,8 @@ typedef struct {
 } POOL_FREE;\r
 \r
 \r
-#define POOL_HEAD_SIGNATURE   SIGNATURE_32('p','h','d','0')\r
+#define POOL_HEAD_SIGNATURE       SIGNATURE_32('p','h','d','0')\r
+#define POOLPAGE_HEAD_SIGNATURE   SIGNATURE_32('p','h','d','1')\r
 typedef struct {\r
   UINT32          Signature;\r
   UINT32          Reserved;\r
@@ -367,6 +362,7 @@ CoreAllocatePoolI (
   UINTN       NoPages;\r
   UINTN       Granularity;\r
   BOOLEAN     HasPoolTail;\r
+  BOOLEAN     PageAsPool;\r
 \r
   ASSERT_LOCKED (&mPoolMemoryLock);\r
 \r
@@ -386,6 +382,7 @@ CoreAllocatePoolI (
 \r
   HasPoolTail  = !(NeedGuard &&\r
                    ((PcdGet8 (PcdHeapGuardPropertyMask) & BIT7) == 0));\r
+  PageAsPool = (IsHeapGuardEnabled (GUARD_HEAP_TYPE_FREED) && !mOnGuarding);\r
 \r
   //\r
   // Adjusting the Size to be of proper alignment so that\r
@@ -406,7 +403,7 @@ CoreAllocatePoolI (
   // If allocation is over max size, just allocate pages for the request\r
   // (slow)\r
   //\r
-  if (Index >= SIZE_TO_LIST (Granularity) || NeedGuard) {\r
+  if (Index >= SIZE_TO_LIST (Granularity) || NeedGuard || PageAsPool) {\r
     if (!HasPoolTail) {\r
       Size -= sizeof (POOL_TAIL);\r
     }\r
@@ -498,7 +495,7 @@ Done:
     //\r
     // If we have a pool buffer, fill in the header & tail info\r
     //\r
-    Head->Signature = POOL_HEAD_SIGNATURE;\r
+    Head->Signature = (PageAsPool) ? POOLPAGE_HEAD_SIGNATURE : POOL_HEAD_SIGNATURE;\r
     Head->Size      = Size;\r
     Head->Type      = (EFI_MEMORY_TYPE) PoolType;\r
     Buffer          = Head->Data;\r
@@ -615,6 +612,7 @@ CoreFreePoolPagesI (
   CoreFreePoolPages (Memory, NoPages);\r
   CoreReleaseMemoryLock ();\r
 \r
+  GuardFreedPagesChecked (Memory, NoPages);\r
   ApplyMemoryProtectionPolicy (PoolType, EfiConventionalMemory,\r
     (EFI_PHYSICAL_ADDRESS)(UINTN)Memory, EFI_PAGES_TO_SIZE (NoPages));\r
 }\r
@@ -642,15 +640,15 @@ CoreFreePoolPagesWithGuard (
   NoPagesGuarded = NoPages;\r
 \r
   AdjustMemoryF (&Memory, &NoPages);\r
+  //\r
+  // It's safe to unset Guard page inside memory lock because there should\r
+  // be no memory allocation occurred in updating memory page attribute at\r
+  // this point. And unsetting Guard page before free will prevent Guard\r
+  // page just freed back to pool from being allocated right away before\r
+  // marking it usable (from non-present to present).\r
+  //\r
+  UnsetGuardForMemory (MemoryGuarded, NoPagesGuarded);\r
   if (NoPages > 0) {\r
-    //\r
-    // It's safe to unset Guard page inside memory lock because there should\r
-    // be no memory allocation occurred in updating memory page attribute at\r
-    // this point. And unsetting Guard page before free will prevent Guard\r
-    // page just freed back to pool from being allocated right away before\r
-    // marking it usable (from non-present to present).\r
-    //\r
-    UnsetGuardForMemory (MemoryGuarded, NoPagesGuarded);\r
     CoreFreePoolPagesI (PoolType, Memory, NoPages);\r
   }\r
 }\r
@@ -685,15 +683,19 @@ CoreFreePoolI (
   UINTN       Granularity;\r
   BOOLEAN     IsGuarded;\r
   BOOLEAN     HasPoolTail;\r
+  BOOLEAN     PageAsPool;\r
 \r
   ASSERT(Buffer != NULL);\r
   //\r
   // Get the head & tail of the pool entry\r
   //\r
-  Head = CR (Buffer, POOL_HEAD, Data, POOL_HEAD_SIGNATURE);\r
+  Head = BASE_CR (Buffer, POOL_HEAD, Data);\r
   ASSERT(Head != NULL);\r
 \r
-  if (Head->Signature != POOL_HEAD_SIGNATURE) {\r
+  if (Head->Signature != POOL_HEAD_SIGNATURE &&\r
+      Head->Signature != POOLPAGE_HEAD_SIGNATURE) {\r
+    ASSERT (Head->Signature == POOL_HEAD_SIGNATURE ||\r
+            Head->Signature == POOLPAGE_HEAD_SIGNATURE);\r
     return EFI_INVALID_PARAMETER;\r
   }\r
 \r
@@ -701,6 +703,7 @@ CoreFreePoolI (
                 IsMemoryGuarded ((EFI_PHYSICAL_ADDRESS)(UINTN)Head);\r
   HasPoolTail = !(IsGuarded &&\r
                   ((PcdGet8 (PcdHeapGuardPropertyMask) & BIT7) == 0));\r
+  PageAsPool = (Head->Signature == POOLPAGE_HEAD_SIGNATURE);\r
 \r
   if (HasPoolTail) {\r
     Tail = HEAD_TO_TAIL (Head);\r
@@ -757,7 +760,7 @@ CoreFreePoolI (
   //\r
   // If it's not on the list, it must be pool pages\r
   //\r
-  if (Index >= SIZE_TO_LIST (Granularity) || IsGuarded) {\r
+  if (Index >= SIZE_TO_LIST (Granularity) || IsGuarded || PageAsPool) {\r
 \r
     //\r
     // Return the memory pages back to free memory\r