]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdeModulePkg/Core/PiSmmCore/Pool.c
MdeModulePkg/PiSmmCore: AllocatePool should use MemoryType.
[mirror_edk2.git] / MdeModulePkg / Core / PiSmmCore / Pool.c
index 79847b4bb6d027b489361a7eff84ed2ad1c1defe..173650ae1dc820d5b2a839cc9547725f6133d4bc 100644 (file)
@@ -1,7 +1,7 @@
 /** @file\r
   SMM Memory pool management functions.\r
 \r
-  Copyright (c) 2009 - 2015, 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
-LIST_ENTRY  mSmmPoolLists[MAX_POOL_INDEX];\r
+LIST_ENTRY  mSmmPoolLists[SmmPoolTypeMax][MAX_POOL_INDEX];\r
 //\r
 // To cache the SMRAM base since when Loading modules At fixed address feature is enabled, \r
 // all module is assigned an offset relative the SMRAM base in build time.\r
 //\r
 GLOBAL_REMOVE_IF_UNREFERENCED  EFI_PHYSICAL_ADDRESS       gLoadModuleAtFixAddressSmramBase = 0;\r
 \r
+/**\r
+  Convert a UEFI memory type to SMM pool type.\r
+\r
+  @param[in]  MemoryType              Type of pool to allocate.\r
+\r
+  @return SMM pool type\r
+**/\r
+SMM_POOL_TYPE\r
+UefiMemoryTypeToSmmPoolType (\r
+  IN  EFI_MEMORY_TYPE   MemoryType\r
+  )\r
+{\r
+  ASSERT ((MemoryType == EfiRuntimeServicesCode) || (MemoryType == EfiRuntimeServicesData));\r
+  switch (MemoryType) {\r
+  case EfiRuntimeServicesCode:\r
+    return SmmPoolTypeCode;\r
+  case EfiRuntimeServicesData:\r
+    return SmmPoolTypeData;\r
+  default:\r
+    return SmmPoolTypeMax;\r
+  }\r
+}\r
+\r
+\r
 /**\r
   Called to initialize the memory service.\r
 \r
@@ -35,19 +59,22 @@ SmmInitializeMemoryServices (
   )\r
 {\r
   UINTN                  Index;\r
-       UINT64                 SmmCodeSize;\r
-       UINTN                  CurrentSmramRangesIndex;\r
-       UINT64                 MaxSize;\r
+  UINT64                 SmmCodeSize;\r
+  UINTN                  CurrentSmramRangesIndex;\r
+  UINT64                 MaxSize;\r
+  UINTN                  SmmPoolTypeIndex;\r
 \r
   //\r
   // Initialize Pool list\r
   //\r
-  for (Index = sizeof (mSmmPoolLists) / sizeof (*mSmmPoolLists); Index > 0;) {\r
-    InitializeListHead (&mSmmPoolLists[--Index]);\r
+  for (SmmPoolTypeIndex = 0; SmmPoolTypeIndex < SmmPoolTypeMax; SmmPoolTypeIndex++) {\r
+    for (Index = 0; Index < ARRAY_SIZE (mSmmPoolLists[SmmPoolTypeIndex]); Index++) {\r
+      InitializeListHead (&mSmmPoolLists[SmmPoolTypeIndex][Index]);\r
+    }\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
@@ -85,9 +112,28 @@ SmmInitializeMemoryServices (
     SmramRanges[CurrentSmramRangesIndex].PhysicalSize = SmramRanges[CurrentSmramRangesIndex].PhysicalSize - SmmCodeSize;\r
   }\r
   //\r
-  // Initialize free SMRAM regions\r
+  // Add 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
+  //\r
+  // Add the allocated SMRAM regions\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
@@ -101,6 +147,7 @@ SmmInitializeMemoryServices (
 /**\r
   Internal Function. Allocate a pool by specified PoolIndex.\r
 \r
+  @param  PoolType              Type of pool to allocate.\r
   @param  PoolIndex             Index which indicate the Pool size.\r
   @param  FreePoolHdr           The returned Free pool.\r
 \r
@@ -110,6 +157,7 @@ SmmInitializeMemoryServices (
 **/\r
 EFI_STATUS\r
 InternalAllocPoolByIndex (\r
+  IN  EFI_MEMORY_TYPE   PoolType,\r
   IN  UINTN             PoolIndex,\r
   OUT FREE_POOL_HEADER  **FreePoolHdr\r
   )\r
@@ -117,25 +165,29 @@ InternalAllocPoolByIndex (
   EFI_STATUS            Status;\r
   FREE_POOL_HEADER      *Hdr;\r
   EFI_PHYSICAL_ADDRESS  Address;\r
+  SMM_POOL_TYPE         SmmPoolType;\r
+\r
+  SmmPoolType = UefiMemoryTypeToSmmPoolType(PoolType);\r
 \r
   ASSERT (PoolIndex <= MAX_POOL_INDEX);\r
   Status = EFI_SUCCESS;\r
   Hdr = NULL;\r
   if (PoolIndex == MAX_POOL_INDEX) {\r
-    Status = SmmInternalAllocatePages (AllocateAnyPages, EfiRuntimeServicesData, EFI_SIZE_TO_PAGES (MAX_POOL_SIZE << 1), &Address);\r
+    Status = SmmInternalAllocatePages (AllocateAnyPages, PoolType, 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
+  } else if (!IsListEmpty (&mSmmPoolLists[SmmPoolType][PoolIndex])) {\r
+    Hdr = BASE_CR (GetFirstNode (&mSmmPoolLists[SmmPoolType][PoolIndex]), FREE_POOL_HEADER, Link);\r
     RemoveEntryList (&Hdr->Link);\r
   } else {\r
-    Status = InternalAllocPoolByIndex (PoolIndex + 1, &Hdr);\r
+    Status = InternalAllocPoolByIndex (PoolType, PoolIndex + 1, &Hdr);\r
     if (!EFI_ERROR (Status)) {\r
       Hdr->Header.Size >>= 1;\r
       Hdr->Header.Available = TRUE;\r
-      InsertHeadList (&mSmmPoolLists[PoolIndex], &Hdr->Link);\r
+      Hdr->Header.Type = PoolType;\r
+      InsertHeadList (&mSmmPoolLists[SmmPoolType][PoolIndex], &Hdr->Link);\r
       Hdr = (FREE_POOL_HEADER*)((UINT8*)Hdr + Hdr->Header.Size);\r
     }\r
   }\r
@@ -143,6 +195,7 @@ InternalAllocPoolByIndex (
   if (!EFI_ERROR (Status)) {\r
     Hdr->Header.Size = MIN_POOL_SIZE << PoolIndex;\r
     Hdr->Header.Available = FALSE;\r
+    Hdr->Header.Type = PoolType;\r
   }\r
 \r
   *FreePoolHdr = Hdr;\r
@@ -162,16 +215,19 @@ InternalFreePoolByIndex (
   IN FREE_POOL_HEADER  *FreePoolHdr\r
   )\r
 {\r
-  UINTN  PoolIndex;\r
+  UINTN                 PoolIndex;\r
+  SMM_POOL_TYPE         SmmPoolType;\r
 \r
   ASSERT ((FreePoolHdr->Header.Size & (FreePoolHdr->Header.Size - 1)) == 0);\r
   ASSERT (((UINTN)FreePoolHdr & (FreePoolHdr->Header.Size - 1)) == 0);\r
   ASSERT (FreePoolHdr->Header.Size >= MIN_POOL_SIZE);\r
 \r
+  SmmPoolType = UefiMemoryTypeToSmmPoolType(FreePoolHdr->Header.Type);\r
+\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
+  InsertHeadList (&mSmmPoolLists[SmmPoolType][PoolIndex], &FreePoolHdr->Link);\r
   return EFI_SUCCESS;\r
 }\r
 \r
@@ -218,6 +274,7 @@ SmmInternalAllocatePool (
     PoolHdr = (POOL_HEADER*)(UINTN)Address;\r
     PoolHdr->Size = EFI_PAGES_TO_SIZE (Size);\r
     PoolHdr->Available = FALSE;\r
+    PoolHdr->Type = PoolType;\r
     *Buffer = PoolHdr + 1;\r
     return Status;\r
   }\r
@@ -228,7 +285,7 @@ SmmInternalAllocatePool (
     PoolIndex++;\r
   }\r
 \r
-  Status = InternalAllocPoolByIndex (PoolIndex, &FreePoolHdr);\r
+  Status = InternalAllocPoolByIndex (PoolType, PoolIndex, &FreePoolHdr);\r
   if (!EFI_ERROR(Status)) {\r
     *Buffer = &FreePoolHdr->Header + 1;\r
   }\r
@@ -260,7 +317,14 @@ SmmAllocatePool (
 \r
   Status = SmmInternalAllocatePool (PoolType, Size, Buffer);\r
   if (!EFI_ERROR (Status)) {\r
-    SmmCoreUpdateProfile ((EFI_PHYSICAL_ADDRESS) (UINTN) RETURN_ADDRESS (0), MemoryProfileActionAllocatePool, PoolType, Size, *Buffer);\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
@@ -319,7 +383,14 @@ SmmFreePool (
 \r
   Status = SmmInternalFreePool (Buffer);\r
   if (!EFI_ERROR (Status)) {\r
-    SmmCoreUpdateProfile ((EFI_PHYSICAL_ADDRESS) (UINTN) RETURN_ADDRESS (0), MemoryProfileActionFreePool, 0, 0, Buffer);\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