]> git.proxmox.com Git - mirror_edk2.git/commitdiff
MdePkg-SmmMemLib: Enhance SmmIsBufferOutsideSmmValid() check for fixed comm buffer.
authorJiewen Yao <jiewen.yao@intel.com>
Fri, 22 Apr 2016 07:49:28 +0000 (15:49 +0800)
committerJiewen Yao <jiewen.yao@intel.com>
Fri, 29 Apr 2016 04:49:26 +0000 (12:49 +0800)
This patch adds more check in SmmIsBufferOutsideSmmValid(), to make sure that
SMM communication buffer is only EfiReservedMemoryType/EfiRuntimeServicesCode/
EfiRuntimeServicesData/EfiACPIMemoryNVS. So that the communication buffer will
not touch any OS memory.

The assumption is that a platform reports valid SMM communication buffer at
EndOfDxe, because EndOfDxe is last hook point that SMM code can call-out to
get memory map information.
A platform MUST finish SMM communication buffer allocation before EndOfDxe.
If a DXE or OS driver need do communication after EndOfDxe, it can either
allocate SMM communication buffer before EndOfDxe and save it, or consume
EDKII_PI_SMM_COMMUNICATION_REGION_TABLE table to get general fixed comm buffer.

This is designed to meet Microsoft WSMT table definition on FIXED_COMM_BUFFERS
requirement.

Cc: Liming Gao <liming.gao@intel.com>
Cc: Laszlo Ersek <lersek@redhat.com>
Contributed-under: TianoCore Contribution Agreement 1.0
Signed-off-by: Jiewen Yao <jiewen.yao@intel.com>
Reviewed-by: Liming Gao <liming.gao@intel.com>
Regression-tested-by: Laszlo Ersek <lersek@redhat.com>
MdePkg/Library/SmmMemLib/SmmMemLib.c
MdePkg/Library/SmmMemLib/SmmMemLib.inf

index 420c5a755f90b7149c181daaa14ab711b6ace117..b4e3156cb42a747feccc656a1119c116a8066b80 100644 (file)
@@ -6,7 +6,7 @@
   all SMRAM range via SMM_ACCESS2_PROTOCOL, including the range for firmware (like SMM Core\r
   and SMM driver) and/or specific dedicated hardware.\r
 \r
   all SMRAM range via SMM_ACCESS2_PROTOCOL, including the range for firmware (like SMM Core\r
   and SMM driver) and/or specific dedicated hardware.\r
 \r
-  Copyright (c) 2015, Intel Corporation. All rights reserved.<BR>\r
+  Copyright (c) 2015 - 2016, 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
   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
 #include <Library/SmmServicesTableLib.h>\r
 #include <Library/HobLib.h>\r
 #include <Protocol/SmmAccess2.h>\r
 #include <Library/SmmServicesTableLib.h>\r
 #include <Library/HobLib.h>\r
 #include <Protocol/SmmAccess2.h>\r
+#include <Protocol/SmmReadyToLock.h>\r
+#include <Protocol/SmmEndOfDxe.h>\r
+\r
+#define NEXT_MEMORY_DESCRIPTOR(MemoryDescriptor, Size) \\r
+  ((EFI_MEMORY_DESCRIPTOR *)((UINT8 *)(MemoryDescriptor) + (Size)))\r
 \r
 EFI_SMRAM_DESCRIPTOR *mSmmMemLibInternalSmramRanges;\r
 UINTN                mSmmMemLibInternalSmramCount;\r
 \r
 EFI_SMRAM_DESCRIPTOR *mSmmMemLibInternalSmramRanges;\r
 UINTN                mSmmMemLibInternalSmramCount;\r
@@ -37,6 +42,15 @@ UINTN                mSmmMemLibInternalSmramCount;
 //\r
 EFI_PHYSICAL_ADDRESS  mSmmMemLibInternalMaximumSupportAddress = 0;\r
 \r
 //\r
 EFI_PHYSICAL_ADDRESS  mSmmMemLibInternalMaximumSupportAddress = 0;\r
 \r
+UINTN                 mMemoryMapEntryCount;\r
+EFI_MEMORY_DESCRIPTOR *mMemoryMap;\r
+UINTN                 mDescriptorSize;\r
+\r
+VOID                  *mRegistrationEndOfDxe;\r
+VOID                  *mRegistrationReadyToLock;\r
+\r
+BOOLEAN               mSmmReadyToLock = FALSE;\r
+\r
 /**\r
   Calculate and save the maximum support address.\r
 \r
 /**\r
   Calculate and save the maximum support address.\r
 \r
@@ -137,6 +151,34 @@ SmmIsBufferOutsideSmmValid (
     }\r
   }\r
 \r
     }\r
   }\r
 \r
+  //\r
+  // Check override for Valid Communication Region\r
+  //\r
+  if (mSmmReadyToLock) {\r
+    EFI_MEMORY_DESCRIPTOR          *MemoryMap;\r
+    BOOLEAN                        InValidCommunicationRegion;\r
+    \r
+    InValidCommunicationRegion = FALSE;\r
+    MemoryMap = mMemoryMap;\r
+    for (Index = 0; Index < mMemoryMapEntryCount; Index++) {\r
+      if ((Buffer >= MemoryMap->PhysicalStart) &&\r
+          (Buffer + Length <= MemoryMap->PhysicalStart + LShiftU64 (MemoryMap->NumberOfPages, EFI_PAGE_SHIFT))) {\r
+        InValidCommunicationRegion = TRUE;\r
+      }\r
+      MemoryMap = NEXT_MEMORY_DESCRIPTOR(MemoryMap, mDescriptorSize);\r
+    }\r
+\r
+    if (!InValidCommunicationRegion) {\r
+      DEBUG ((\r
+        EFI_D_ERROR,\r
+        "SmmIsBufferOutsideSmmValid: Not in ValidCommunicationRegion: Buffer (0x%lx) - Length (0x%lx), ",\r
+        Buffer,\r
+        Length\r
+        ));\r
+      ASSERT (FALSE);\r
+      return FALSE;\r
+    }\r
+  }\r
   return TRUE;\r
 }\r
 \r
   return TRUE;\r
 }\r
 \r
@@ -276,6 +318,128 @@ SmmSetMem (
   return EFI_SUCCESS;\r
 }\r
 \r
   return EFI_SUCCESS;\r
 }\r
 \r
+/**\r
+  Notification for SMM EndOfDxe protocol.\r
+\r
+  @param[in] Protocol   Points to the protocol's unique identifier.\r
+  @param[in] Interface  Points to the interface instance.\r
+  @param[in] Handle     The handle on which the interface was installed.\r
+\r
+  @retval EFI_SUCCESS   Notification runs successfully.\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+SmmLibInternalEndOfDxeNotify (\r
+  IN CONST EFI_GUID  *Protocol,\r
+  IN VOID            *Interface,\r
+  IN EFI_HANDLE      Handle\r
+  )\r
+{\r
+  EFI_STATUS            Status;\r
+  UINTN                 MapKey;\r
+  UINTN                 MemoryMapSize;\r
+  EFI_MEMORY_DESCRIPTOR *MemoryMap;\r
+  EFI_MEMORY_DESCRIPTOR *MemoryMapStart;\r
+  EFI_MEMORY_DESCRIPTOR *SmmMemoryMapStart;\r
+  UINTN                 MemoryMapEntryCount;\r
+  UINTN                 DescriptorSize;\r
+  UINT32                DescriptorVersion;\r
+  UINTN                 Index;\r
+\r
+  MemoryMapSize = 0;\r
+  MemoryMap = NULL;\r
+  Status = gBS->GetMemoryMap (\r
+             &MemoryMapSize,\r
+             MemoryMap,\r
+             &MapKey,\r
+             &DescriptorSize,\r
+             &DescriptorVersion\r
+             );\r
+  ASSERT (Status == EFI_BUFFER_TOO_SMALL);\r
+\r
+  do {\r
+    Status = gBS->AllocatePool (EfiBootServicesData, MemoryMapSize, (VOID **)&MemoryMap);\r
+    ASSERT (MemoryMap != NULL);\r
+  \r
+    Status = gBS->GetMemoryMap (\r
+               &MemoryMapSize,\r
+               MemoryMap,\r
+               &MapKey,\r
+               &DescriptorSize,\r
+               &DescriptorVersion\r
+               );\r
+    if (EFI_ERROR (Status)) {\r
+      gBS->FreePool (MemoryMap);\r
+    }\r
+  } while (Status == EFI_BUFFER_TOO_SMALL);\r
+\r
+  //\r
+  // Get Count\r
+  //\r
+  mDescriptorSize = DescriptorSize;\r
+  MemoryMapEntryCount = MemoryMapSize/DescriptorSize;\r
+  MemoryMapStart = MemoryMap;\r
+  mMemoryMapEntryCount = 0;\r
+  for (Index = 0; Index < MemoryMapEntryCount; Index++) {\r
+    switch (MemoryMap->Type) {\r
+    case EfiReservedMemoryType:\r
+    case EfiRuntimeServicesCode:\r
+    case EfiRuntimeServicesData:\r
+    case EfiACPIMemoryNVS:\r
+      mMemoryMapEntryCount++;\r
+      break;\r
+    }\r
+    MemoryMap = NEXT_MEMORY_DESCRIPTOR(MemoryMap, DescriptorSize);\r
+  }\r
+  MemoryMap = MemoryMapStart;\r
+  \r
+  //\r
+  // Get Data\r
+  //\r
+  mMemoryMap = AllocatePool (mMemoryMapEntryCount*DescriptorSize);\r
+  ASSERT (mMemoryMap != NULL);\r
+  SmmMemoryMapStart = mMemoryMap;\r
+  for (Index = 0; Index < MemoryMapEntryCount; Index++) {\r
+    switch (MemoryMap->Type) {\r
+    case EfiReservedMemoryType:\r
+    case EfiRuntimeServicesCode:\r
+    case EfiRuntimeServicesData:\r
+    case EfiACPIMemoryNVS:\r
+      CopyMem (mMemoryMap, MemoryMap, DescriptorSize);\r
+      mMemoryMap = NEXT_MEMORY_DESCRIPTOR(mMemoryMap, DescriptorSize);\r
+      break;\r
+    }\r
+    MemoryMap = NEXT_MEMORY_DESCRIPTOR(MemoryMap, DescriptorSize);\r
+  }\r
+  mMemoryMap = SmmMemoryMapStart;\r
+  MemoryMap = MemoryMapStart;\r
+  \r
+  gBS->FreePool (MemoryMap);\r
+\r
+  return EFI_SUCCESS;\r
+}\r
+\r
+\r
+/**\r
+  Notification for SMM ReadyToLock protocol.\r
+\r
+  @param[in] Protocol   Points to the protocol's unique identifier.\r
+  @param[in] Interface  Points to the interface instance.\r
+  @param[in] Handle     The handle on which the interface was installed.\r
+\r
+  @retval EFI_SUCCESS   Notification runs successfully.\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+SmmLibInternalReadyToLockNotify (\r
+  IN CONST EFI_GUID  *Protocol,\r
+  IN VOID            *Interface,\r
+  IN EFI_HANDLE      Handle\r
+  )\r
+{\r
+  mSmmReadyToLock = TRUE;\r
+  return EFI_SUCCESS;\r
+}\r
 /**\r
   The constructor function initializes the Smm Mem library\r
 \r
 /**\r
   The constructor function initializes the Smm Mem library\r
 \r
@@ -319,6 +483,18 @@ SmmMemLibConstructor (
   //\r
   SmmMemLibInternalCalculateMaximumSupportAddress ();\r
 \r
   //\r
   SmmMemLibInternalCalculateMaximumSupportAddress ();\r
 \r
+  //\r
+  // Register EndOfDxe to get UEFI memory map\r
+  //\r
+  Status = gSmst->SmmRegisterProtocolNotify (&gEfiSmmEndOfDxeProtocolGuid, SmmLibInternalEndOfDxeNotify, &mRegistrationEndOfDxe);\r
+  ASSERT_EFI_ERROR (Status);\r
+\r
+  //\r
+  // Register ready to lock so that we can know when to check valid SMRAM region\r
+  //\r
+  Status = gSmst->SmmRegisterProtocolNotify (&gEfiSmmReadyToLockProtocolGuid, SmmLibInternalReadyToLockNotify, &mRegistrationReadyToLock);\r
+  ASSERT_EFI_ERROR (Status);\r
+\r
   return EFI_SUCCESS;\r
 }\r
 \r
   return EFI_SUCCESS;\r
 }\r
 \r
@@ -339,5 +515,7 @@ SmmMemLibDestructor (
 {\r
   FreePool (mSmmMemLibInternalSmramRanges);\r
 \r
 {\r
   FreePool (mSmmMemLibInternalSmramRanges);\r
 \r
+  gSmst->SmmRegisterProtocolNotify (&gEfiSmmEndOfDxeProtocolGuid, NULL, &mRegistrationEndOfDxe);\r
+  gSmst->SmmRegisterProtocolNotify (&gEfiSmmReadyToLockProtocolGuid, NULL, &mRegistrationReadyToLock);\r
   return EFI_SUCCESS;\r
 }\r
   return EFI_SUCCESS;\r
 }\r
index a9d47cc90a59e5f2f21a8ae23952b49a7e314036..b1691cc2778820045d8941f53e26d72c9628def0 100644 (file)
@@ -6,7 +6,7 @@
 #  all SMRAM range via SMM_ACCESS2_PROTOCOL, including the range for firmware (like SMM Core\r
 #  and SMM driver) and/or specific dedicated hardware.\r
 #  \r
 #  all SMRAM range via SMM_ACCESS2_PROTOCOL, including the range for firmware (like SMM Core\r
 #  and SMM driver) and/or specific dedicated hardware.\r
 #  \r
-#  Copyright (c) 2015, Intel Corporation. All rights reserved.<BR>\r
+#  Copyright (c) 2015 - 2016, 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
 #  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
@@ -49,7 +49,9 @@
   MemoryAllocationLib\r
 \r
 [Protocols]\r
   MemoryAllocationLib\r
 \r
 [Protocols]\r
-  gEfiSmmAccess2ProtocolGuid    ## CONSUMES\r
+  gEfiSmmAccess2ProtocolGuid     ## CONSUMES\r
+  gEfiSmmReadyToLockProtocolGuid ## CONSUMES\r
+  gEfiSmmEndOfDxeProtocolGuid    ## CONSUMES\r
   \r
 [Depex]\r
   gEfiSmmAccess2ProtocolGuid\r
   \r
 [Depex]\r
   gEfiSmmAccess2ProtocolGuid\r