]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdeModulePkg/Core/PiSmmCore/Pool.c
MdeModulePkg/PiSmmCore: Cache CommunicationBuffer info before using it
[mirror_edk2.git] / MdeModulePkg / Core / PiSmmCore / Pool.c
index b38a3e0b7e190c4efe12fa23b33f266852508fa4..dcfd13e57536ceb411708749a867fcfbe0839881 100644 (file)
@@ -1,7 +1,7 @@
 /** @file\r
   SMM Memory pool management functions.\r
 \r
-  Copyright (c) 2009 - 2010, Intel Corporation.  All rights reserved.<BR>\r
+  Copyright (c) 2009 - 2016, Intel Corporation. All rights reserved.<BR>\r
   This program and the accompanying materials are licensed and made available \r
   under the terms and conditions of the BSD License which accompanies this \r
   distribution.  The full text of the license may be found at        \r
 \r
 #include "PiSmmCore.h"\r
 \r
-//\r
-// MIN_POOL_SHIFT must not be less than 5\r
-//\r
-#define MIN_POOL_SHIFT  6\r
-#define MIN_POOL_SIZE   (1 << MIN_POOL_SHIFT)\r
-\r
-//\r
-// MAX_POOL_SHIFT must not be less than EFI_PAGE_SHIFT - 1\r
-//\r
-#define MAX_POOL_SHIFT  (EFI_PAGE_SHIFT - 1)\r
-#define MAX_POOL_SIZE   (1 << MAX_POOL_SHIFT)\r
-\r
-//\r
-// MAX_POOL_INDEX are calculated by maximum and minimum pool sizes\r
-//\r
-#define MAX_POOL_INDEX  (MAX_POOL_SHIFT - MIN_POOL_SHIFT + 1)\r
-\r
-typedef struct {\r
-  UINTN        Size;\r
-  BOOLEAN      Available;\r
-} POOL_HEADER;\r
-\r
-typedef struct {\r
-  POOL_HEADER  Header;\r
-  LIST_ENTRY   Link;\r
-} FREE_POOL_HEADER;\r
-\r
 LIST_ENTRY  mSmmPoolLists[MAX_POOL_INDEX];\r
 //\r
 // To cache the SMRAM base since when Loading modules At fixed address feature is enabled, \r
@@ -69,12 +42,12 @@ SmmInitializeMemoryServices (
   //\r
   // Initialize Pool list\r
   //\r
-  for (Index = sizeof (mSmmPoolLists) / sizeof (*mSmmPoolLists); Index > 0;) {\r
+  for (Index = ARRAY_SIZE (mSmmPoolLists); Index > 0;) {\r
     InitializeListHead (&mSmmPoolLists[--Index]);\r
   }\r
   CurrentSmramRangesIndex = 0;\r
   //\r
-  // If Loadding Module At fixed Address feature is enabled, cache the SMRAM base here\r
+  // If Loading Module At fixed Address feature is enabled, cache the SMRAM base here\r
   //\r
   if (PcdGet64(PcdLoadModuleAtFixAddressEnable) != 0) {\r
     //\r
@@ -86,8 +59,15 @@ SmmInitializeMemoryServices (
     // Find the largest SMRAM range between 1MB and 4GB that is at least 256KB - 4K in size\r
     //\r
     for (Index = 0, MaxSize = SIZE_256KB - EFI_PAGE_SIZE; Index < SmramRangeCount; Index++) {\r
+      //\r
+      // Skip any SMRAM region that is already allocated, needs testing, or needs ECC initialization\r
+      //\r
+      if ((SmramRanges[Index].RegionState & (EFI_ALLOCATED | EFI_NEEDS_TESTING | EFI_NEEDS_ECC_INITIALIZATION)) != 0) {\r
+        continue;\r
+      }\r
+\r
       if (SmramRanges[Index].CpuStart >= BASE_1MB) {\r
-        if ((SmramRanges[Index].CpuStart + SmramRanges[Index].PhysicalSize) <= BASE_4GB) {\r
+        if ((SmramRanges[Index].CpuStart + SmramRanges[Index].PhysicalSize - 1) <= MAX_ADDRESS) {\r
           if (SmramRanges[Index].PhysicalSize >= MaxSize) {\r
             MaxSize = SmramRanges[Index].PhysicalSize;\r
             CurrentSmramRangesIndex = Index;\r
@@ -106,8 +86,24 @@ SmmInitializeMemoryServices (
   }\r
   //\r
   // Initialize free SMRAM regions\r
+  // Need add Free memory at first, to let gSmmMemoryMap record data\r
   //\r
   for (Index = 0; Index < SmramRangeCount; Index++) {\r
+    if ((SmramRanges[Index].RegionState & (EFI_ALLOCATED | EFI_NEEDS_TESTING | EFI_NEEDS_ECC_INITIALIZATION)) != 0) {\r
+      continue;\r
+    }\r
+    SmmAddMemoryRegion (\r
+      SmramRanges[Index].CpuStart,\r
+      SmramRanges[Index].PhysicalSize,\r
+      EfiConventionalMemory,\r
+      SmramRanges[Index].RegionState\r
+      );\r
+  }\r
+\r
+  for (Index = 0; Index < SmramRangeCount; Index++) {\r
+    if ((SmramRanges[Index].RegionState & (EFI_ALLOCATED | EFI_NEEDS_TESTING | EFI_NEEDS_ECC_INITIALIZATION)) == 0) {\r
+      continue;\r
+    }\r
     SmmAddMemoryRegion (\r
       SmramRanges[Index].CpuStart,\r
       SmramRanges[Index].PhysicalSize,\r
@@ -134,16 +130,19 @@ InternalAllocPoolByIndex (
   OUT FREE_POOL_HEADER  **FreePoolHdr\r
   )\r
 {\r
-  EFI_STATUS        Status;\r
-  FREE_POOL_HEADER  *Hdr;\r
+  EFI_STATUS            Status;\r
+  FREE_POOL_HEADER      *Hdr;\r
+  EFI_PHYSICAL_ADDRESS  Address;\r
 \r
   ASSERT (PoolIndex <= MAX_POOL_INDEX);\r
   Status = EFI_SUCCESS;\r
+  Hdr = NULL;\r
   if (PoolIndex == MAX_POOL_INDEX) {\r
-    Hdr = (FREE_POOL_HEADER *)AllocatePages (EFI_SIZE_TO_PAGES (MAX_POOL_SIZE << 1));\r
-    if (Hdr == NULL) {\r
+    Status = SmmInternalAllocatePages (AllocateAnyPages, EfiRuntimeServicesData, EFI_SIZE_TO_PAGES (MAX_POOL_SIZE << 1), &Address);\r
+    if (EFI_ERROR (Status)) {\r
       return EFI_OUT_OF_RESOURCES;\r
     }\r
+    Hdr = (FREE_POOL_HEADER *) (UINTN) Address;\r
   } else if (!IsListEmpty (&mSmmPoolLists[PoolIndex])) {\r
     Hdr = BASE_CR (GetFirstNode (&mSmmPoolLists[PoolIndex]), FREE_POOL_HEADER, Link);\r
     RemoveEntryList (&Hdr->Link);\r
@@ -185,7 +184,7 @@ InternalFreePoolByIndex (
   ASSERT (((UINTN)FreePoolHdr & (FreePoolHdr->Header.Size - 1)) == 0);\r
   ASSERT (FreePoolHdr->Header.Size >= MIN_POOL_SIZE);\r
 \r
-  PoolIndex = HighBitSet32 ((UINT32)FreePoolHdr->Header.Size) - MIN_POOL_SHIFT;\r
+  PoolIndex = (UINTN) (HighBitSet32 ((UINT32)FreePoolHdr->Header.Size) - MIN_POOL_SHIFT);\r
   FreePoolHdr->Header.Available = TRUE;\r
   ASSERT (PoolIndex < MAX_POOL_INDEX);\r
   InsertHeadList (&mSmmPoolLists[PoolIndex], &FreePoolHdr->Link);\r
@@ -207,7 +206,7 @@ InternalFreePoolByIndex (
 **/\r
 EFI_STATUS\r
 EFIAPI\r
-SmmAllocatePool (\r
+SmmInternalAllocatePool (\r
   IN   EFI_MEMORY_TYPE  PoolType,\r
   IN   UINTN            Size,\r
   OUT  VOID             **Buffer\r
@@ -227,7 +226,7 @@ SmmAllocatePool (
   Size += sizeof (*PoolHdr);\r
   if (Size > MAX_POOL_SIZE) {\r
     Size = EFI_SIZE_TO_PAGES (Size);\r
-    Status = SmmAllocatePages (AllocateAnyPages, PoolType, Size, &Address);\r
+    Status = SmmInternalAllocatePages (AllocateAnyPages, PoolType, Size, &Address);\r
     if (EFI_ERROR (Status)) {\r
       return Status;\r
     }\r
@@ -240,13 +239,52 @@ SmmAllocatePool (
   }\r
 \r
   Size = (Size + MIN_POOL_SIZE - 1) >> MIN_POOL_SHIFT;\r
-  PoolIndex = HighBitSet32 ((UINT32)Size);\r
+  PoolIndex = (UINTN) HighBitSet32 ((UINT32)Size);\r
   if ((Size & (Size - 1)) != 0) {\r
     PoolIndex++;\r
   }\r
 \r
   Status = InternalAllocPoolByIndex (PoolIndex, &FreePoolHdr);\r
-  *Buffer = &FreePoolHdr->Header + 1;\r
+  if (!EFI_ERROR(Status)) {\r
+    *Buffer = &FreePoolHdr->Header + 1;\r
+  }\r
+  return Status;\r
+}\r
+\r
+/**\r
+  Allocate pool of a particular type.\r
+\r
+  @param  PoolType               Type of pool to allocate.\r
+  @param  Size                   The amount of pool to allocate.\r
+  @param  Buffer                 The address to return a pointer to the allocated\r
+                                 pool.\r
+\r
+  @retval EFI_INVALID_PARAMETER  PoolType not valid.\r
+  @retval EFI_OUT_OF_RESOURCES   Size exceeds max pool size or allocation failed.\r
+  @retval EFI_SUCCESS            Pool successfully allocated.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+SmmAllocatePool (\r
+  IN   EFI_MEMORY_TYPE  PoolType,\r
+  IN   UINTN            Size,\r
+  OUT  VOID             **Buffer\r
+  )\r
+{\r
+  EFI_STATUS  Status;\r
+\r
+  Status = SmmInternalAllocatePool (PoolType, Size, Buffer);\r
+  if (!EFI_ERROR (Status)) {\r
+    SmmCoreUpdateProfile (\r
+      (EFI_PHYSICAL_ADDRESS) (UINTN) RETURN_ADDRESS (0),\r
+      MemoryProfileActionAllocatePool,\r
+      PoolType,\r
+      Size,\r
+      *Buffer,\r
+      NULL\r
+      );\r
+  }\r
   return Status;\r
 }\r
 \r
@@ -261,7 +299,7 @@ SmmAllocatePool (
 **/\r
 EFI_STATUS\r
 EFIAPI\r
-SmmFreePool (\r
+SmmInternalFreePool (\r
   IN VOID  *Buffer\r
   )\r
 {\r
@@ -277,10 +315,41 @@ SmmFreePool (
   if (FreePoolHdr->Header.Size > MAX_POOL_SIZE) {\r
     ASSERT (((UINTN)FreePoolHdr & EFI_PAGE_MASK) == 0);\r
     ASSERT ((FreePoolHdr->Header.Size & EFI_PAGE_MASK) == 0);\r
-    return SmmFreePages (\r
+    return SmmInternalFreePages (\r
              (EFI_PHYSICAL_ADDRESS)(UINTN)FreePoolHdr,\r
              EFI_SIZE_TO_PAGES (FreePoolHdr->Header.Size)\r
              );\r
   }\r
   return InternalFreePoolByIndex (FreePoolHdr);\r
 }\r
+\r
+/**\r
+  Frees pool.\r
+\r
+  @param  Buffer                 The allocated pool entry to free.\r
+\r
+  @retval EFI_INVALID_PARAMETER  Buffer is not a valid value.\r
+  @retval EFI_SUCCESS            Pool successfully freed.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+SmmFreePool (\r
+  IN VOID  *Buffer\r
+  )\r
+{\r
+  EFI_STATUS  Status;\r
+\r
+  Status = SmmInternalFreePool (Buffer);\r
+  if (!EFI_ERROR (Status)) {\r
+    SmmCoreUpdateProfile (\r
+      (EFI_PHYSICAL_ADDRESS) (UINTN) RETURN_ADDRESS (0),\r
+      MemoryProfileActionFreePool,\r
+      EfiMaxMemoryType,\r
+      0,\r
+      Buffer,\r
+      NULL\r
+      );\r
+  }\r
+  return Status;\r
+}\r