]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdeModulePkg/Library/PiSmmCoreMemoryAllocationLib/MemoryAllocationLib.c
NetworkPkg: Support DNS4/6 GeneralLookUp feature
[mirror_edk2.git] / MdeModulePkg / Library / PiSmmCoreMemoryAllocationLib / MemoryAllocationLib.c
index 7df6a1a73bf4dafa83a48af90522d9bfce10d371..5e13a3eda2035232d7bf6bb806f13cb86cca80b0 100644 (file)
@@ -1,7 +1,16 @@
 /** @file\r
   Support routines for memory allocation routines based on SMM Core internal functions.\r
-\r
-  Copyright (c) 2006 - 2010, Intel Corporation. All rights reserved.<BR>\r
+  \r
+  The PI System Management Mode Core Interface Specification only allows the use\r
+  of EfiRuntimeServicesCode and EfiRuntimeServicesData memory types for memory \r
+  allocations as the SMRAM space should be reserved after BDS phase.  The functions \r
+  in the Memory Allocation Library use EfiBootServicesData as the default memory \r
+  allocation type.  For this SMM specific instance of the Memory Allocation Library, \r
+  EfiRuntimeServicesData is used as the default memory type for all allocations. \r
+  In addition, allocation for the Reserved memory types are not supported and will \r
+  always return NULL.\r
+\r
+  Copyright (c) 2006 - 2015, 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
 \r
 #include <PiSmm.h>\r
 \r
+#include <Protocol/SmmAccess2.h>\r
 #include <Library/MemoryAllocationLib.h>\r
+#include <Library/UefiBootServicesTableLib.h>\r
 #include <Library/BaseMemoryLib.h>\r
 #include <Library/DebugLib.h>\r
 #include "PiSmmCoreMemoryAllocationServices.h"\r
 \r
+EFI_SMRAM_DESCRIPTOR  *mSmmCoreMemoryAllocLibSmramRanges    = NULL;\r
+UINTN                 mSmmCoreMemoryAllocLibSmramRangeCount = 0;\r
+\r
+/**\r
+  Check whether the start address of buffer is within any of the SMRAM ranges.\r
+\r
+  @param[in]  Buffer   The pointer to the buffer to be checked.\r
+\r
+  @retval     TURE     The buffer is in SMRAM ranges.\r
+  @retval     FALSE    The buffer is out of SMRAM ranges.\r
+**/\r
+BOOLEAN\r
+EFIAPI\r
+BufferInSmram (\r
+  IN VOID *Buffer\r
+  )\r
+{\r
+  UINTN  Index;\r
+\r
+  for (Index = 0; Index < mSmmCoreMemoryAllocLibSmramRangeCount; Index ++) {\r
+    if (((EFI_PHYSICAL_ADDRESS) (UINTN) Buffer >= mSmmCoreMemoryAllocLibSmramRanges[Index].CpuStart) && \r
+        ((EFI_PHYSICAL_ADDRESS) (UINTN) Buffer < (mSmmCoreMemoryAllocLibSmramRanges[Index].CpuStart + mSmmCoreMemoryAllocLibSmramRanges[Index].PhysicalSize))) {\r
+      return TRUE;\r
+    }\r
+  }\r
+\r
+  return FALSE;\r
+}\r
+\r
 /**\r
   Allocates one or more 4KB pages of a certain memory type.\r
 \r
@@ -53,9 +93,9 @@ InternalAllocatePages (
 }\r
 \r
 /**\r
-  Allocates one or more 4KB pages of type EfiBootServicesData.\r
+  Allocates one or more 4KB pages of type EfiRuntimeServicesData.\r
 \r
-  Allocates the number of 4KB pages of type EfiBootServicesData and returns a pointer to the\r
+  Allocates the number of 4KB pages of type EfiRuntimeServicesData and returns a pointer to the\r
   allocated buffer.  The buffer returned is aligned on a 4KB boundary.  If Pages is 0, then NULL\r
   is returned.  If there is not enough memory remaining to satisfy the request, then NULL is\r
   returned.\r
@@ -145,7 +185,19 @@ FreePages (
   EFI_STATUS  Status;\r
 \r
   ASSERT (Pages != 0);\r
-  Status = SmmFreePages ((EFI_PHYSICAL_ADDRESS) (UINTN) Buffer, Pages);\r
+  if (BufferInSmram (Buffer)) {\r
+    //\r
+    // When Buffer is in SMRAM range, it should be allocated by SmmAllocatePages() service.\r
+    // So, SmmFreePages() service is used to free it.\r
+    //\r
+    Status = SmmFreePages ((EFI_PHYSICAL_ADDRESS) (UINTN) Buffer, Pages);\r
+  } else {\r
+    //\r
+    // When Buffer is out of SMRAM range, it should be allocated by gBS->AllocatePages() service.\r
+    // So, gBS->FreePages() service is used to free it.\r
+    //\r
+    Status = gBS->FreePages ((EFI_PHYSICAL_ADDRESS) (UINTN) Buffer, Pages);\r
+  }\r
   ASSERT_EFI_ERROR (Status);\r
 }\r
 \r
@@ -157,6 +209,7 @@ FreePages (
   If there is not enough memory at the specified alignment remaining to satisfy the request, then\r
   NULL is returned.\r
   If Alignment is not a power of two and Alignment is not zero, then ASSERT().\r
+  If Pages plus EFI_SIZE_TO_PAGES (Alignment) overflows, then ASSERT().\r
 \r
   @param  MemoryType            The type of memory to allocate.\r
   @param  Pages                 The number of 4 KB pages to allocate.\r
@@ -190,7 +243,7 @@ InternalAllocateAlignedPages (
   }\r
   if (Alignment > EFI_PAGE_SIZE) {\r
     //\r
-    // Caculate the total number of pages since alignment is larger than page size.\r
+    // Calculate the total number of pages since alignment is larger than page size.\r
     //\r
     AlignmentMask  = Alignment - 1;\r
     RealPages      = Pages + EFI_SIZE_TO_PAGES (Alignment);\r
@@ -235,14 +288,15 @@ InternalAllocateAlignedPages (
 }\r
 \r
 /**\r
-  Allocates one or more 4KB pages of type EfiBootServicesData at a specified alignment.\r
+  Allocates one or more 4KB pages of type EfiRuntimeServicesData at a specified alignment.\r
 \r
-  Allocates the number of 4KB pages specified by Pages of type EfiBootServicesData with an\r
+  Allocates the number of 4KB pages specified by Pages of type EfiRuntimeServicesData with an\r
   alignment specified by Alignment.  The allocated buffer is returned.  If Pages is 0, then NULL is\r
   returned.  If there is not enough memory at the specified alignment remaining to satisfy the\r
   request, then NULL is returned.\r
   \r
   If Alignment is not a power of two and Alignment is not zero, then ASSERT().\r
+  If Pages plus EFI_SIZE_TO_PAGES (Alignment) overflows, then ASSERT().\r
 \r
   @param  Pages                 The number of 4 KB pages to allocate.\r
   @param  Alignment             The requested alignment of the allocation.  Must be a power of two.\r
@@ -270,6 +324,7 @@ AllocateAlignedPages (
   request, then NULL is returned.\r
   \r
   If Alignment is not a power of two and Alignment is not zero, then ASSERT().\r
+  If Pages plus EFI_SIZE_TO_PAGES (Alignment) overflows, then ASSERT().\r
 \r
   @param  Pages                 The number of 4 KB pages to allocate.\r
   @param  Alignment             The requested alignment of the allocation.  Must be a power of two.\r
@@ -297,6 +352,7 @@ AllocateAlignedRuntimePages (
   request, then NULL is returned.\r
   \r
   If Alignment is not a power of two and Alignment is not zero, then ASSERT().\r
+  If Pages plus EFI_SIZE_TO_PAGES (Alignment) overflows, then ASSERT().\r
 \r
   @param  Pages                 The number of 4 KB pages to allocate.\r
   @param  Alignment             The requested alignment of the allocation.  Must be a power of two.\r
@@ -342,7 +398,19 @@ FreeAlignedPages (
   EFI_STATUS  Status;\r
 \r
   ASSERT (Pages != 0);\r
-  Status = SmmFreePages ((EFI_PHYSICAL_ADDRESS) (UINTN) Buffer, Pages);\r
+  if (BufferInSmram (Buffer)) {\r
+    //\r
+    // When Buffer is in SMRAM range, it should be allocated by SmmAllocatePages() service.\r
+    // So, SmmFreePages() service is used to free it.\r
+    //\r
+    Status = SmmFreePages ((EFI_PHYSICAL_ADDRESS) (UINTN) Buffer, Pages);\r
+  } else {\r
+    //\r
+    // When Buffer is out of SMRAM range, it should be allocated by gBS->AllocatePages() service.\r
+    // So, gBS->FreePages() service is used to free it.\r
+    //\r
+    Status = gBS->FreePages ((EFI_PHYSICAL_ADDRESS) (UINTN) Buffer, Pages);\r
+  }\r
   ASSERT_EFI_ERROR (Status);\r
 }\r
 \r
@@ -368,6 +436,8 @@ InternalAllocatePool (
   EFI_STATUS  Status;\r
   VOID        *Memory;\r
 \r
+  Memory = NULL;\r
+\r
   Status = SmmAllocatePool (MemoryType, AllocationSize, &Memory);\r
   if (EFI_ERROR (Status)) {\r
     Memory = NULL;\r
@@ -376,9 +446,9 @@ InternalAllocatePool (
 }\r
 \r
 /**\r
-  Allocates a buffer of type EfiBootServicesData.\r
+  Allocates a buffer of type EfiRuntimeServicesData.\r
 \r
-  Allocates the number bytes specified by AllocationSize of type EfiBootServicesData and returns a\r
+  Allocates the number bytes specified by AllocationSize of type EfiRuntimeServicesData and returns a\r
   pointer to the allocated buffer.  If AllocationSize is 0, then a valid buffer of 0 size is\r
   returned.  If there is not enough memory remaining to satisfy the request, then NULL is returned.\r
 \r
@@ -468,9 +538,9 @@ InternalAllocateZeroPool (
 }\r
 \r
 /**\r
-  Allocates and zeros a buffer of type EfiBootServicesData.\r
+  Allocates and zeros a buffer of type EfiRuntimeServicesData.\r
 \r
-  Allocates the number bytes specified by AllocationSize of type EfiBootServicesData, clears the\r
+  Allocates the number bytes specified by AllocationSize of type EfiRuntimeServicesData, clears the\r
   buffer with zeros, and returns a pointer to the allocated buffer.  If AllocationSize is 0, then a\r
   valid buffer of 0 size is returned.  If there is not enough memory remaining to satisfy the\r
   request, then NULL is returned.\r
@@ -570,9 +640,9 @@ InternalAllocateCopyPool (
 } \r
 \r
 /**\r
-  Copies a buffer to an allocated buffer of type EfiBootServicesData.\r
+  Copies a buffer to an allocated buffer of type EfiRuntimeServicesData.\r
 \r
-  Allocates the number bytes specified by AllocationSize of type EfiBootServicesData, copies\r
+  Allocates the number bytes specified by AllocationSize of type EfiRuntimeServicesData, copies\r
   AllocationSize bytes from Buffer to the newly allocated buffer, and returns a pointer to the\r
   allocated buffer.  If AllocationSize is 0, then a valid buffer of 0 size is returned.  If there\r
   is not enough memory remaining to satisfy the request, then NULL is returned.\r
@@ -691,10 +761,10 @@ InternalReallocatePool (
 }\r
 \r
 /**\r
-  Reallocates a buffer of type EfiBootServicesData.\r
+  Reallocates a buffer of type EfiRuntimeServicesData.\r
 \r
   Allocates and zeros the number bytes specified by NewSize from memory of type\r
-  EfiBootServicesData.  If OldBuffer is not NULL, then the smaller of OldSize and \r
+  EfiRuntimeServicesData.  If OldBuffer is not NULL, then the smaller of OldSize and \r
   NewSize bytes are copied from OldBuffer to the newly allocated buffer, and \r
   OldBuffer is freed.  A pointer to the newly allocated buffer is returned.  \r
   If NewSize is 0, then a valid buffer of 0 size is  returned.  If there is not \r
@@ -808,7 +878,52 @@ FreePool (
 {\r
   EFI_STATUS    Status;\r
 \r
-  Status = SmmFreePool (Buffer);\r
+  if (BufferInSmram (Buffer)) {\r
+    //\r
+    // When Buffer is in SMRAM range, it should be allocated by SmmAllocatePool() service.\r
+    // So, SmmFreePool() service is used to free it.\r
+    //\r
+    Status = SmmFreePool (Buffer);\r
+  } else {\r
+    //\r
+    // When Buffer is out of SMRAM range, it should be allocated by gBS->AllocatePool() service.\r
+    // So, gBS->FreePool() service is used to free it.\r
+    //\r
+    Status = gBS->FreePool (Buffer);\r
+  }\r
   ASSERT_EFI_ERROR (Status);\r
 }\r
 \r
+/**\r
+  The constructor function calls SmmInitializeMemoryServices to initialize memory in SMRAM.\r
+\r
+  @param  ImageHandle   The firmware allocated handle for the EFI image.\r
+  @param  SystemTable   A pointer to the EFI System Table.\r
+\r
+  @retval EFI_SUCCESS   The constructor always returns EFI_SUCCESS.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+PiSmmCoreMemoryAllocationLibConstructor (\r
+  IN EFI_HANDLE        ImageHandle,\r
+  IN EFI_SYSTEM_TABLE  *SystemTable\r
+  )\r
+{\r
+  SMM_CORE_PRIVATE_DATA  *SmmCorePrivate;\r
+  UINTN                  Size;\r
+\r
+  SmmCorePrivate = (SMM_CORE_PRIVATE_DATA *)ImageHandle;\r
+  //\r
+  // Initialize memory service using free SMRAM\r
+  //\r
+  SmmInitializeMemoryServices (SmmCorePrivate->SmramRangeCount, SmmCorePrivate->SmramRanges);\r
+\r
+  mSmmCoreMemoryAllocLibSmramRangeCount = SmmCorePrivate->SmramRangeCount;\r
+  Size = mSmmCoreMemoryAllocLibSmramRangeCount * sizeof (EFI_SMRAM_DESCRIPTOR);\r
+  mSmmCoreMemoryAllocLibSmramRanges = (EFI_SMRAM_DESCRIPTOR *) AllocatePool (Size);\r
+  ASSERT (mSmmCoreMemoryAllocLibSmramRanges != NULL);\r
+  CopyMem (mSmmCoreMemoryAllocLibSmramRanges, SmmCorePrivate->SmramRanges, Size);\r
+\r
+  return EFI_SUCCESS;\r
+}\r