]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdeModulePkg/Core/PiSmmCore/PiSmmCore.c
Merge branch 'master' of https://github.com/tianocore/edk2
[mirror_edk2.git] / MdeModulePkg / Core / PiSmmCore / PiSmmCore.c
index a7220e4235ab87847f8533d3791c2651239ca15f..551560b1fce1e92466e271065c83855f39d14565 100644 (file)
@@ -1,7 +1,7 @@
 /** @file\r
   SMM Core Main Entry Point\r
 \r
-  Copyright (c) 2009 - 2012, 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
@@ -66,7 +66,7 @@ EFI_SMM_SYSTEM_TABLE2  gSmmCoreSmst = {
 //\r
 // Flag to determine if the platform has performed a legacy boot.\r
 // If this flag is TRUE, then the runtime code and runtime data associated with the \r
-// SMM IPL are converted to free memory, so the SMM COre must guarantee that is\r
+// SMM IPL are converted to free memory, so the SMM Core must guarantee that is\r
 // does not touch of the code/data associated with the SMM IPL if this flag is TRUE.\r
 //\r
 BOOLEAN  mInLegacyBoot = FALSE;\r
@@ -75,13 +75,20 @@ BOOLEAN  mInLegacyBoot = FALSE;
 // Table of SMI Handlers that are registered by the SMM Core when it is initialized\r
 //\r
 SMM_CORE_SMI_HANDLERS  mSmmCoreSmiHandlers[] = {\r
-  { SmmDriverDispatchHandler, &gEfiEventDxeDispatchGuid,          NULL, TRUE  },\r
-  { SmmReadyToLockHandler,    &gEfiDxeSmmReadyToLockProtocolGuid, NULL, TRUE }, \r
-  { SmmLegacyBootHandler,     &gEfiEventLegacyBootGuid,           NULL, FALSE },\r
-  { SmmEndOfDxeHandler,       &gEfiEndOfDxeEventGroupGuid,        NULL, FALSE },\r
-  { NULL,                     NULL,                               NULL, FALSE }\r
+  { SmmDriverDispatchHandler,   &gEfiEventDxeDispatchGuid,          NULL, TRUE  },\r
+  { SmmReadyToLockHandler,      &gEfiDxeSmmReadyToLockProtocolGuid, NULL, TRUE }, \r
+  { SmmLegacyBootHandler,       &gEfiEventLegacyBootGuid,           NULL, FALSE },\r
+  { SmmExitBootServicesHandler, &gEfiEventExitBootServicesGuid,     NULL, FALSE },\r
+  { SmmReadyToBootHandler,      &gEfiEventReadyToBootGuid,          NULL, FALSE },\r
+  { SmmEndOfDxeHandler,         &gEfiEndOfDxeEventGroupGuid,        NULL, TRUE },\r
+  { NULL,                       NULL,                               NULL, FALSE }\r
 };\r
 \r
+UINTN                           mFullSmramRangeCount;\r
+EFI_SMRAM_DESCRIPTOR            *mFullSmramRanges;\r
+\r
+EFI_LOADED_IMAGE_PROTOCOL       *mSmmCoreLoadedImage;\r
+\r
 /**\r
   Place holder function until all the SMM System Table Service are available.\r
 \r
@@ -118,7 +125,8 @@ SmmEfiNotAvailableYetArg5 (
   Core uses this signal to know that a Legacy Boot has been performed and that \r
   gSmmCorePrivate that is shared between the UEFI and SMM execution environments can\r
   not be accessed from SMM anymore since that structure is considered free memory by\r
-  a legacy OS.\r
+  a legacy OS. Then the SMM Core also install SMM Legacy Boot protocol to notify SMM\r
+  driver that system enter legacy boot.\r
 \r
   @param  DispatchHandle  The unique handle assigned to this handler by SmiHandlerRegister().\r
   @param  Context         Points to an optional handler context which was specified when the handler was registered.\r
@@ -138,8 +146,109 @@ SmmLegacyBootHandler (
   IN OUT UINTN       *CommBufferSize  OPTIONAL\r
   )\r
 {\r
+  EFI_STATUS    Status;\r
+  EFI_HANDLE    SmmHandle;\r
+\r
+  //\r
+  // Install SMM Legacy Boot protocol.\r
+  //\r
+  SmmHandle = NULL;\r
+  Status = SmmInstallProtocolInterface (\r
+             &SmmHandle,\r
+             &gEdkiiSmmLegacyBootProtocolGuid,\r
+             EFI_NATIVE_INTERFACE,\r
+             NULL\r
+             );\r
+\r
   mInLegacyBoot = TRUE;\r
-  return EFI_SUCCESS;\r
+\r
+  SmiHandlerUnRegister (DispatchHandle);\r
+\r
+  return Status;\r
+}\r
+\r
+/**\r
+  Software SMI handler that is called when an Exit Boot Services event is signalled.\r
+  Then the SMM Core also install SMM Exit Boot Services protocol to notify SMM driver\r
+  that system enter exit boot services.\r
+\r
+  @param  DispatchHandle  The unique handle assigned to this handler by SmiHandlerRegister().\r
+  @param  Context         Points to an optional handler context which was specified when the handler was registered.\r
+  @param  CommBuffer      A pointer to a collection of data in memory that will\r
+                          be conveyed from a non-SMM environment into an SMM environment.\r
+  @param  CommBufferSize  The size of the CommBuffer.\r
+\r
+  @return Status Code\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+SmmExitBootServicesHandler (\r
+  IN     EFI_HANDLE  DispatchHandle,\r
+  IN     CONST VOID  *Context,        OPTIONAL\r
+  IN OUT VOID        *CommBuffer,     OPTIONAL\r
+  IN OUT UINTN       *CommBufferSize  OPTIONAL\r
+  )\r
+{\r
+  EFI_STATUS    Status;\r
+  EFI_HANDLE    SmmHandle;\r
+\r
+  //\r
+  // Install SMM Exit Boot Services protocol.\r
+  //\r
+  SmmHandle = NULL;\r
+  Status = SmmInstallProtocolInterface (\r
+             &SmmHandle,\r
+             &gEdkiiSmmExitBootServicesProtocolGuid,\r
+             EFI_NATIVE_INTERFACE,\r
+             NULL\r
+             );\r
+\r
+  SmiHandlerUnRegister (DispatchHandle);\r
+\r
+  return Status;\r
+}\r
+\r
+/**\r
+  Software SMI handler that is called when an Ready To Boot event is signalled.\r
+  Then the SMM Core also install SMM Ready To Boot protocol to notify SMM driver\r
+  that system enter ready to boot.\r
+\r
+  @param  DispatchHandle  The unique handle assigned to this handler by SmiHandlerRegister().\r
+  @param  Context         Points to an optional handler context which was specified when the handler was registered.\r
+  @param  CommBuffer      A pointer to a collection of data in memory that will\r
+                          be conveyed from a non-SMM environment into an SMM environment.\r
+  @param  CommBufferSize  The size of the CommBuffer.\r
+\r
+  @return Status Code\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+SmmReadyToBootHandler (\r
+  IN     EFI_HANDLE  DispatchHandle,\r
+  IN     CONST VOID  *Context,        OPTIONAL\r
+  IN OUT VOID        *CommBuffer,     OPTIONAL\r
+  IN OUT UINTN       *CommBufferSize  OPTIONAL\r
+  )\r
+{\r
+  EFI_STATUS    Status;\r
+  EFI_HANDLE    SmmHandle;\r
+\r
+  //\r
+  // Install SMM Ready To Boot protocol.\r
+  //\r
+  SmmHandle = NULL;\r
+  Status = SmmInstallProtocolInterface (\r
+             &SmmHandle,\r
+             &gEdkiiSmmReadyToBootProtocolGuid,\r
+             EFI_NATIVE_INTERFACE,\r
+             NULL\r
+             );\r
+\r
+  SmiHandlerUnRegister (DispatchHandle);\r
+\r
+  return Status;\r
 }\r
 \r
 /**\r
@@ -226,6 +335,8 @@ SmmReadyToLockHandler (
   gST = NULL;\r
   gBS = NULL;\r
 \r
+  SmramProfileReadyToLock ();\r
+\r
   return Status;\r
 }\r
 \r
@@ -266,7 +377,38 @@ SmmEndOfDxeHandler (
              EFI_NATIVE_INTERFACE,\r
              NULL\r
              );\r
-  return EFI_SUCCESS;\r
+  return Status;\r
+}\r
+\r
+/**\r
+  Determine if two buffers overlap in memory.\r
+\r
+  @param[in] Buff1  Pointer to first buffer\r
+  @param[in] Size1  Size of Buff1\r
+  @param[in] Buff2  Pointer to second buffer\r
+  @param[in] Size2  Size of Buff2\r
+\r
+  @retval TRUE      Buffers overlap in memory.\r
+  @retval FALSE     Buffer doesn't overlap.\r
+\r
+**/\r
+BOOLEAN\r
+InternalIsBufferOverlapped (\r
+  IN UINT8      *Buff1,\r
+  IN UINTN      Size1,\r
+  IN UINT8      *Buff2,\r
+  IN UINTN      Size2\r
+  )\r
+{\r
+  //\r
+  // If buff1's end is less than the start of buff2, then it's ok.\r
+  // Also, if buff1's start is beyond buff2's end, then it's ok.\r
+  //\r
+  if (((Buff1 + Size1) <= Buff2) || (Buff1 >= (Buff2 + Size2))) {\r
+    return FALSE;\r
+  }\r
+\r
+  return TRUE;\r
 }\r
 \r
 /**\r
@@ -287,13 +429,18 @@ SmmEntryPoint (
   EFI_STATUS                  Status;\r
   EFI_SMM_COMMUNICATE_HEADER  *CommunicateHeader;\r
   BOOLEAN                     InLegacyBoot;\r
+  BOOLEAN                     IsOverlapped;\r
 \r
   PERF_START (NULL, "SMM", NULL, 0) ;\r
 \r
   //\r
-  // Update SMST using the context\r
+  // Update SMST with contents of the SmmEntryContext structure\r
   //\r
-  CopyMem (&gSmmCoreSmst.SmmStartupThisAp, SmmEntryContext, sizeof (EFI_SMM_ENTRY_CONTEXT));\r
+  gSmmCoreSmst.SmmStartupThisAp      = SmmEntryContext->SmmStartupThisAp;\r
+  gSmmCoreSmst.CurrentlyExecutingCpu = SmmEntryContext->CurrentlyExecutingCpu;\r
+  gSmmCoreSmst.NumberOfCpus          = SmmEntryContext->NumberOfCpus;\r
+  gSmmCoreSmst.CpuSaveStateSize      = SmmEntryContext->CpuSaveStateSize;\r
+  gSmmCoreSmst.CpuSaveState          = SmmEntryContext->CpuSaveState;\r
 \r
   //\r
   // Call platform hook before Smm Dispatch\r
@@ -318,22 +465,37 @@ SmmEntryPoint (
       //\r
       // Synchronous SMI for SMM Core or request from Communicate protocol\r
       //\r
-      CommunicateHeader = (EFI_SMM_COMMUNICATE_HEADER *)gSmmCorePrivate->CommunicationBuffer;\r
-      gSmmCorePrivate->BufferSize -= OFFSET_OF (EFI_SMM_COMMUNICATE_HEADER, Data);\r
-      Status = SmiManage (\r
-                 &CommunicateHeader->HeaderGuid, \r
-                 NULL, \r
-                 CommunicateHeader->Data, \r
-                 &gSmmCorePrivate->BufferSize\r
-                 );\r
-\r
-      //\r
-      // Update CommunicationBuffer, BufferSize and ReturnStatus\r
-      // Communicate service finished, reset the pointer to CommBuffer to NULL\r
-      //\r
-      gSmmCorePrivate->BufferSize += OFFSET_OF (EFI_SMM_COMMUNICATE_HEADER, Data);\r
-      gSmmCorePrivate->CommunicationBuffer = NULL;\r
-      gSmmCorePrivate->ReturnStatus = (Status == EFI_SUCCESS) ? EFI_SUCCESS : EFI_NOT_FOUND;\r
+      IsOverlapped = InternalIsBufferOverlapped (\r
+                       (UINT8 *) gSmmCorePrivate->CommunicationBuffer,\r
+                       gSmmCorePrivate->BufferSize,\r
+                       (UINT8 *) gSmmCorePrivate,\r
+                       sizeof (*gSmmCorePrivate)\r
+                       );\r
+      if (!SmmIsBufferOutsideSmmValid ((UINTN)gSmmCorePrivate->CommunicationBuffer, gSmmCorePrivate->BufferSize) || IsOverlapped) {\r
+        //\r
+        // If CommunicationBuffer is not in valid address scope,\r
+        // or there is overlap between gSmmCorePrivate and CommunicationBuffer,\r
+        // return EFI_INVALID_PARAMETER\r
+        //\r
+        gSmmCorePrivate->CommunicationBuffer = NULL;\r
+        gSmmCorePrivate->ReturnStatus = EFI_INVALID_PARAMETER;\r
+      } else {\r
+        CommunicateHeader = (EFI_SMM_COMMUNICATE_HEADER *)gSmmCorePrivate->CommunicationBuffer;\r
+        gSmmCorePrivate->BufferSize -= OFFSET_OF (EFI_SMM_COMMUNICATE_HEADER, Data);\r
+        Status = SmiManage (\r
+                   &CommunicateHeader->HeaderGuid, \r
+                   NULL, \r
+                   CommunicateHeader->Data, \r
+                   &gSmmCorePrivate->BufferSize\r
+                   );\r
+        //\r
+        // Update CommunicationBuffer, BufferSize and ReturnStatus\r
+        // Communicate service finished, reset the pointer to CommBuffer to NULL\r
+        //\r
+        gSmmCorePrivate->BufferSize += OFFSET_OF (EFI_SMM_COMMUNICATE_HEADER, Data);\r
+        gSmmCorePrivate->CommunicationBuffer = NULL;\r
+        gSmmCorePrivate->ReturnStatus = (Status == EFI_SUCCESS) ? EFI_SUCCESS : EFI_NOT_FOUND;\r
+      }\r
     }\r
   }\r
 \r
@@ -360,6 +522,51 @@ SmmEntryPoint (
   PERF_END (NULL, "SMM", NULL, 0) ;\r
 }\r
 \r
+/**\r
+  Install LoadedImage protocol for SMM Core.\r
+**/\r
+VOID\r
+SmmCoreInstallLoadedImage (\r
+  VOID\r
+  )\r
+{\r
+  EFI_STATUS                 Status;\r
+  EFI_HANDLE                 Handle;\r
+\r
+  //\r
+  // Allocate a Loaded Image Protocol in EfiBootServicesData\r
+  //\r
+  Status = gBS->AllocatePool (EfiBootServicesData, sizeof(EFI_LOADED_IMAGE_PROTOCOL), (VOID **)&mSmmCoreLoadedImage);\r
+  ASSERT_EFI_ERROR (Status);\r
+\r
+  ZeroMem (mSmmCoreLoadedImage, sizeof (EFI_LOADED_IMAGE_PROTOCOL));\r
+  //\r
+  // Fill in the remaining fields of the Loaded Image Protocol instance.\r
+  // Note: ImageBase is an SMRAM address that can not be accessed outside of SMRAM if SMRAM window is closed.\r
+  //\r
+  mSmmCoreLoadedImage->Revision      = EFI_LOADED_IMAGE_PROTOCOL_REVISION;\r
+  mSmmCoreLoadedImage->ParentHandle  = gSmmCorePrivate->SmmIplImageHandle;\r
+  mSmmCoreLoadedImage->SystemTable   = gST;\r
+\r
+  mSmmCoreLoadedImage->ImageBase     = (VOID *)(UINTN)gSmmCorePrivate->PiSmmCoreImageBase;\r
+  mSmmCoreLoadedImage->ImageSize     = gSmmCorePrivate->PiSmmCoreImageSize;\r
+  mSmmCoreLoadedImage->ImageCodeType = EfiRuntimeServicesCode;\r
+  mSmmCoreLoadedImage->ImageDataType = EfiRuntimeServicesData;\r
+\r
+  //\r
+  // Create a new image handle in the UEFI handle database for the SMM Driver\r
+  //\r
+  Handle = NULL;\r
+  Status = gBS->InstallMultipleProtocolInterfaces (\r
+                  &Handle,\r
+                  &gEfiLoadedImageProtocolGuid, mSmmCoreLoadedImage,\r
+                  NULL\r
+                  );\r
+  ASSERT_EFI_ERROR (Status);\r
+\r
+  return ;\r
+}\r
+\r
 /**\r
   The Entry Point for SMM Core\r
 \r
@@ -397,9 +604,20 @@ SmmMain (
   gSmmCorePrivate->SmmEntryPoint = SmmEntryPoint;\r
   \r
   //\r
-  // Initialize memory service using free SMRAM\r
+  // No need to initialize memory service.\r
+  // It is done in constructor of PiSmmCoreMemoryAllocationLib(),\r
+  // so that the library linked with PiSmmCore can use AllocatePool() in constuctor.\r
   //\r
-  SmmInitializeMemoryServices (gSmmCorePrivate->SmramRangeCount, gSmmCorePrivate->SmramRanges);\r
+\r
+  SmramProfileInit ();\r
+\r
+  //\r
+  // Copy FullSmramRanges to SMRAM\r
+  //\r
+  mFullSmramRangeCount = gSmmCorePrivate->SmramRangeCount;\r
+  mFullSmramRanges = AllocatePool (mFullSmramRangeCount * sizeof (EFI_SMRAM_DESCRIPTOR));\r
+  ASSERT (mFullSmramRanges != NULL);\r
+  CopyMem (mFullSmramRanges, gSmmCorePrivate->SmramRanges, mFullSmramRangeCount * sizeof (EFI_SMRAM_DESCRIPTOR));\r
 \r
   //\r
   // Register all SMI Handlers required by the SMM Core\r
@@ -412,6 +630,11 @@ SmmMain (
                );\r
     ASSERT_EFI_ERROR (Status);\r
   }\r
-  \r
+\r
+  RegisterSmramProfileHandler ();\r
+  SmramProfileInstallProtocol ();\r
+\r
+  SmmCoreInstallLoadedImage ();\r
+\r
   return EFI_SUCCESS;\r
 }\r