]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdeModulePkg/Core/PiSmmCore/Pool.c
Patch from open source community for CryptoPkg to allow it to build for ARM using...
[mirror_edk2.git] / MdeModulePkg / Core / PiSmmCore / Pool.c
index d7f80f4f8c2448b776a5c2f84066607d23c55d6b..beb5cd965e0a157403649d1b90b01bf4070c9c0c 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 - 2011, 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
@@ -42,6 +42,11 @@ typedef struct {
 } 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
+// 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
   Called to initialize the memory service.\r
@@ -56,7 +61,10 @@ SmmInitializeMemoryServices (
   IN EFI_SMRAM_DESCRIPTOR  *SmramRanges\r
   )\r
 {\r
-  UINTN  Index;\r
+  UINTN                  Index;\r
+       UINT64                 SmmCodeSize;\r
+       UINTN                  CurrentSmramRangesIndex;\r
+       UINT64                 MaxSize;\r
 \r
   //\r
   // Initialize Pool list\r
@@ -64,7 +72,45 @@ SmmInitializeMemoryServices (
   for (Index = sizeof (mSmmPoolLists) / sizeof (*mSmmPoolLists); Index > 0;) {\r
     InitializeListHead (&mSmmPoolLists[--Index]);\r
   }\r
-\r
+  CurrentSmramRangesIndex = 0;\r
+  //\r
+  // If Loadding Module At fixed Address feature is enabled, cache the SMRAM base here\r
+  //\r
+  if (PcdGet64(PcdLoadModuleAtFixAddressEnable) != 0) {\r
+    //\r
+    // Build tool will calculate the smm code size and then patch the PcdLoadFixAddressSmmCodePageNumber\r
+    //\r
+    SmmCodeSize = LShiftU64 (PcdGet32(PcdLoadFixAddressSmmCodePageNumber), EFI_PAGE_SHIFT);\r
+    \r
+    //\r
+    // 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].PhysicalSize >= MaxSize) {\r
+            MaxSize = SmramRanges[Index].PhysicalSize;\r
+            CurrentSmramRangesIndex = Index;\r
+          }\r
+        }\r
+      }\r
+    }\r
+    gLoadModuleAtFixAddressSmramBase = SmramRanges[CurrentSmramRangesIndex].CpuStart;\r
+    \r
+    //\r
+    // cut out a memory range from this SMRAM range with the size SmmCodeSize to hold SMM driver code\r
+    // A notable thing is that SMM core is already loaded into this range.\r
+    //\r
+    SmramRanges[CurrentSmramRangesIndex].CpuStart     = SmramRanges[CurrentSmramRangesIndex].CpuStart + SmmCodeSize; \r
+    SmramRanges[CurrentSmramRangesIndex].PhysicalSize = SmramRanges[CurrentSmramRangesIndex].PhysicalSize - SmmCodeSize;\r
+  }\r
   //\r
   // Initialize free SMRAM regions\r
   //\r
@@ -76,6 +122,7 @@ SmmInitializeMemoryServices (
       SmramRanges[Index].RegionState\r
       );\r
   }\r
+\r
 }\r
 \r
 /**\r
@@ -145,7 +192,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
@@ -184,11 +231,6 @@ SmmAllocatePool (
     return EFI_INVALID_PARAMETER;\r
   }\r
 \r
-  if (Size == 0) {\r
-    *Buffer = NULL;\r
-    return EFI_SUCCESS;\r
-  }\r
-\r
   Size += sizeof (*PoolHdr);\r
   if (Size > MAX_POOL_SIZE) {\r
     Size = EFI_SIZE_TO_PAGES (Size);\r
@@ -205,7 +247,7 @@ 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