]> git.proxmox.com Git - mirror_edk2.git/blobdiff - StandaloneMmPkg/Library/StandaloneMmMemLib/StandaloneMmMemLib.c
UefiCpuPkg: Move AsmRelocateApLoopStart from Mpfuncs.nasm to AmdSev.nasm
[mirror_edk2.git] / StandaloneMmPkg / Library / StandaloneMmMemLib / StandaloneMmMemLib.c
index f82cdb3ba816a5a36d10fcdf13fc6bd73f76820f..814352369ed31ffff10b13cfb2f25df1daa70655 100644 (file)
@@ -1,27 +1,26 @@
 /** @file\r
   Instance of MM memory check library.\r
 \r
-  MM memory check library library implementation. This library consumes MM_ACCESS_PROTOCOL\r
+  MM memory check library implementation. This library consumes MM_ACCESS_PROTOCOL\r
   to get MMRAM information. In order to use this library instance, the platform should produce\r
   all MMRAM range via MM_ACCESS_PROTOCOL, including the range for firmware (like MM Core\r
   and MM driver) and/or specific dedicated hardware.\r
 \r
   Copyright (c) 2015, Intel Corporation. All rights reserved.<BR>\r
-  Copyright (c) 2016 - 2018, ARM Limited. All rights reserved.<BR>\r
+  Copyright (c) 2016 - 2021, Arm Limited. All rights reserved.<BR>\r
 \r
   SPDX-License-Identifier: BSD-2-Clause-Patent\r
 \r
 **/\r
 \r
-\r
 #include <PiMm.h>\r
 \r
 #include <Library/BaseLib.h>\r
 #include <Library/BaseMemoryLib.h>\r
 #include <Library/DebugLib.h>\r
 \r
-EFI_MMRAM_DESCRIPTOR *mMmMemLibInternalMmramRanges;\r
-UINTN                mMmMemLibInternalMmramCount;\r
+EFI_MMRAM_DESCRIPTOR  *mMmMemLibInternalMmramRanges;\r
+UINTN                 mMmMemLibInternalMmramCount;\r
 \r
 //\r
 // Maximum support address used to check input buffer\r
@@ -37,6 +36,27 @@ MmMemLibInternalCalculateMaximumSupportAddress (
   VOID\r
   );\r
 \r
+/**\r
+  Initialize cached Mmram Ranges from HOB.\r
+\r
+  @retval EFI_UNSUPPORTED   The routine is unable to extract MMRAM information.\r
+  @retval EFI_SUCCESS       MmRanges are populated successfully.\r
+\r
+**/\r
+EFI_STATUS\r
+MmMemLibInternalPopulateMmramRanges (\r
+  VOID\r
+  );\r
+\r
+/**\r
+  Deinitialize cached Mmram Ranges.\r
+\r
+**/\r
+VOID\r
+MmMemLibInternalFreeMmramRanges (\r
+  VOID\r
+  );\r
+\r
 /**\r
   This function check if the buffer is valid per processor architecture and not overlap with MMRAM.\r
 \r
@@ -61,7 +81,8 @@ MmIsBufferOutsideMmValid (
   //\r
   if ((Length > mMmMemLibInternalMaximumSupportAddress) ||\r
       (Buffer > mMmMemLibInternalMaximumSupportAddress) ||\r
-      ((Length != 0) && (Buffer > (mMmMemLibInternalMaximumSupportAddress - (Length - 1)))) ) {\r
+      ((Length != 0) && (Buffer > (mMmMemLibInternalMaximumSupportAddress - (Length - 1)))))\r
+  {\r
     //\r
     // Overflow happen\r
     //\r
@@ -75,11 +96,12 @@ MmIsBufferOutsideMmValid (
     return FALSE;\r
   }\r
 \r
-  for (Index = 0; Index < mMmMemLibInternalMmramCount; Index ++) {\r
+  for (Index = 0; Index < mMmMemLibInternalMmramCount; Index++) {\r
     if (((Buffer >= mMmMemLibInternalMmramRanges[Index].CpuStart) &&\r
          (Buffer < mMmMemLibInternalMmramRanges[Index].CpuStart + mMmMemLibInternalMmramRanges[Index].PhysicalSize)) ||\r
         ((mMmMemLibInternalMmramRanges[Index].CpuStart >= Buffer) &&\r
-         (mMmMemLibInternalMmramRanges[Index].CpuStart < Buffer + Length))) {\r
+         (mMmMemLibInternalMmramRanges[Index].CpuStart < Buffer + Length)))\r
+    {\r
       DEBUG ((\r
         DEBUG_ERROR,\r
         "MmIsBufferOutsideMmValid: Overlap: Buffer (0x%lx) - Length (0x%lx), ",\r
@@ -128,6 +150,7 @@ MmCopyMemToMmram (
     DEBUG ((DEBUG_ERROR, "MmCopyMemToMmram: Security Violation: Source (0x%x), Length (0x%x)\n", SourceBuffer, Length));\r
     return EFI_SECURITY_VIOLATION;\r
   }\r
+\r
   CopyMem (DestinationBuffer, SourceBuffer, Length);\r
   return EFI_SUCCESS;\r
 }\r
@@ -145,7 +168,7 @@ MmCopyMemToMmram (
   @param  SourceBuffer        The pointer to the source buffer of the memory copy.\r
   @param  Length              The number of bytes to copy from SourceBuffer to DestinationBuffer.\r
 \r
-  @retval EFI_SECURITY_VIOLATION The DesinationBuffer is invalid per processor architecture or overlap with MMRAM.\r
+  @retval EFI_SECURITY_VIOLATION The DestinationBuffer is invalid per processor architecture or overlap with MMRAM.\r
   @retval EFI_SUCCESS            Memory is copied.\r
 \r
 **/\r
@@ -158,10 +181,15 @@ MmCopyMemFromMmram (
   )\r
 {\r
   if (!MmIsBufferOutsideMmValid ((EFI_PHYSICAL_ADDRESS)(UINTN)DestinationBuffer, Length)) {\r
-    DEBUG ((DEBUG_ERROR, "MmCopyMemFromMmram: Security Violation: Destination (0x%x), Length (0x%x)\n",\r
-            DestinationBuffer, Length));\r
+    DEBUG ((\r
+      DEBUG_ERROR,\r
+      "MmCopyMemFromMmram: Security Violation: Destination (0x%x), Length (0x%x)\n",\r
+      DestinationBuffer,\r
+      Length\r
+      ));\r
     return EFI_SECURITY_VIOLATION;\r
   }\r
+\r
   CopyMem (DestinationBuffer, SourceBuffer, Length);\r
   return EFI_SUCCESS;\r
 }\r
@@ -179,7 +207,7 @@ MmCopyMemFromMmram (
   @param  SourceBuffer        The pointer to the source buffer of the memory copy.\r
   @param  Length              The number of bytes to copy from SourceBuffer to DestinationBuffer.\r
 \r
-  @retval EFI_SECURITY_VIOLATION The DesinationBuffer is invalid per processor architecture or overlap with MMRAM.\r
+  @retval EFI_SECURITY_VIOLATION The DestinationBuffer is invalid per processor architecture or overlap with MMRAM.\r
   @retval EFI_SECURITY_VIOLATION The SourceBuffer is invalid per processor architecture or overlap with MMRAM.\r
   @retval EFI_SUCCESS            Memory is copied.\r
 \r
@@ -193,14 +221,20 @@ MmCopyMem (
   )\r
 {\r
   if (!MmIsBufferOutsideMmValid ((EFI_PHYSICAL_ADDRESS)(UINTN)DestinationBuffer, Length)) {\r
-    DEBUG ((DEBUG_ERROR, "MmCopyMem: Security Violation: Destination (0x%x), Length (0x%x)\n",\r
-            DestinationBuffer, Length));\r
+    DEBUG ((\r
+      DEBUG_ERROR,\r
+      "MmCopyMem: Security Violation: Destination (0x%x), Length (0x%x)\n",\r
+      DestinationBuffer,\r
+      Length\r
+      ));\r
     return EFI_SECURITY_VIOLATION;\r
   }\r
+\r
   if (!MmIsBufferOutsideMmValid ((EFI_PHYSICAL_ADDRESS)(UINTN)SourceBuffer, Length)) {\r
     DEBUG ((DEBUG_ERROR, "MmCopyMem: Security Violation: Source (0x%x), Length (0x%x)\n", SourceBuffer, Length));\r
     return EFI_SECURITY_VIOLATION;\r
   }\r
+\r
   CopyMem (DestinationBuffer, SourceBuffer, Length);\r
   return EFI_SUCCESS;\r
 }\r
@@ -233,6 +267,7 @@ MmSetMem (
     DEBUG ((DEBUG_ERROR, "MmSetMem: Security Violation: Source (0x%x), Length (0x%x)\n", Buffer, Length));\r
     return EFI_SECURITY_VIOLATION;\r
   }\r
+\r
   SetMem (Buffer, Length, Value);\r
   return EFI_SUCCESS;\r
 }\r
@@ -240,8 +275,8 @@ MmSetMem (
 /**\r
   The constructor function initializes the Mm Mem library\r
 \r
-  @param  ImageHandle   The firmware allocated handle for the EFI image.\r
-  @param  SystemTable   A pointer to the EFI System Table.\r
+  @param  [in]  ImageHandle     The firmware allocated handle for the EFI image.\r
+  @param  [in]  MmSystemTable   A pointer to the EFI System Table.\r
 \r
   @retval EFI_SUCCESS   The constructor always returns EFI_SUCCESS.\r
 \r
@@ -249,15 +284,45 @@ MmSetMem (
 EFI_STATUS\r
 EFIAPI\r
 MemLibConstructor (\r
-  IN EFI_HANDLE             ImageHandle,\r
-  IN EFI_MM_SYSTEM_TABLE    *MmSystemTable\r
+  IN EFI_HANDLE           ImageHandle,\r
+  IN EFI_MM_SYSTEM_TABLE  *MmSystemTable\r
   )\r
 {\r
+  EFI_STATUS  Status;\r
 \r
   //\r
   // Calculate and save maximum support address\r
   //\r
   MmMemLibInternalCalculateMaximumSupportAddress ();\r
 \r
+  //\r
+  // Initialize cached Mmram Ranges from HOB.\r
+  //\r
+  Status = MmMemLibInternalPopulateMmramRanges ();\r
+\r
+  return Status;\r
+}\r
+\r
+/**\r
+  Destructor for Mm Mem library.\r
+\r
+  @param ImageHandle    The image handle of the process.\r
+  @param MmSystemTable  The EFI System Table pointer.\r
+\r
+  @retval EFI_SUCCESS   The constructor always returns EFI_SUCCESS.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+MemLibDestructor (\r
+  IN EFI_HANDLE           ImageHandle,\r
+  IN EFI_MM_SYSTEM_TABLE  *MmSystemTable\r
+  )\r
+{\r
+  //\r
+  // Deinitialize cached Mmram Ranges.\r
+  //\r
+  MmMemLibInternalFreeMmramRanges ();\r
+\r
   return EFI_SUCCESS;\r
 }\r