]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdeModulePkg/Core/PiSmmCore/PiSmmCore.c
MdeModulePkg: Change OPTIONAL keyword usage style
[mirror_edk2.git] / MdeModulePkg / Core / PiSmmCore / PiSmmCore.c
index 7245f201fbdf78ca965208c2563f67918bc69509..2e6d7913f453102423495c7408e2f37f2dac794d 100644 (file)
@@ -1,14 +1,8 @@
 /** @file\r
   SMM Core Main Entry Point\r
 \r
-  Copyright (c) 2009 - 2015, 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
-  http://opensource.org/licenses/bsd-license.php                                            \r
-\r
-  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,                     \r
-  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.             \r
+  Copyright (c) 2009 - 2019, Intel Corporation. All rights reserved.<BR>\r
+  SPDX-License-Identifier: BSD-2-Clause-Patent\r
 \r
 **/\r
 \r
@@ -65,28 +59,51 @@ EFI_SMM_SYSTEM_TABLE2  gSmmCoreSmst = {
 \r
 //\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
+// 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
 // does not touch of the code/data associated with the SMM IPL if this flag is TRUE.\r
 //\r
 BOOLEAN  mInLegacyBoot = FALSE;\r
 \r
+//\r
+// Flag to determine if it is during S3 resume.\r
+// It will be set in S3 entry callback and cleared at EndOfS3Resume.\r
+//\r
+BOOLEAN  mDuringS3Resume = FALSE;\r
+\r
+//\r
+// Flag to determine if platform enabled S3.\r
+// Get the value from PcdAcpiS3Enable.\r
+//\r
+BOOLEAN  mAcpiS3Enable = FALSE;\r
+\r
 //\r
 // 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
-  { SmmExitBootServicesHandler, &gEfiEventExitBootServicesGuid,     NULL, FALSE },\r
-  { SmmReadyToBootHandler,      &gEfiEventReadyToBootGuid,          NULL, FALSE },\r
-  { SmmEndOfDxeHandler,         &gEfiEndOfDxeEventGroupGuid,        NULL, TRUE },\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
+//\r
+// Table of SMI Handlers that are registered by the SMM Core when it is initialized\r
+//\r
+SMM_CORE_SMI_HANDLERS  mSmmCoreS3SmiHandlers[] = {\r
+  { SmmS3SmmInitDoneHandler,    &gEdkiiS3SmmInitDoneGuid,            NULL, FALSE },\r
+  { SmmEndOfS3ResumeHandler,    &gEdkiiEndOfS3ResumeGuid,            NULL, FALSE },\r
+  { NULL,                       NULL,                                NULL, FALSE }\r
 };\r
 \r
 UINTN                           mFullSmramRangeCount;\r
 EFI_SMRAM_DESCRIPTOR            *mFullSmramRanges;\r
 \r
+EFI_SMM_DRIVER_ENTRY            *mSmmCoreDriverEntry;\r
+\r
 EFI_LOADED_IMAGE_PROTOCOL       *mSmmCoreLoadedImage;\r
 \r
 /**\r
@@ -122,7 +139,7 @@ SmmEfiNotAvailableYetArg5 (
 \r
 /**\r
   Software SMI handler that is called when a Legacy Boot event is signalled.  The SMM\r
-  Core uses this signal to know that a Legacy Boot has been performed and that \r
+  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. Then the SMM Core also install SMM Legacy Boot protocol to notify SMM\r
@@ -141,13 +158,14 @@ EFI_STATUS
 EFIAPI\r
 SmmLegacyBootHandler (\r
   IN     EFI_HANDLE  DispatchHandle,\r
-  IN     CONST VOID  *Context,        OPTIONAL\r
-  IN OUT VOID        *CommBuffer,     OPTIONAL\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
+  UINTN         Index;\r
 \r
   //\r
   // Install SMM Legacy Boot protocol.\r
@@ -164,6 +182,16 @@ SmmLegacyBootHandler (
 \r
   SmiHandlerUnRegister (DispatchHandle);\r
 \r
+  //\r
+  // It is legacy boot, unregister ExitBootService SMI handler.\r
+  //\r
+  for (Index = 0; mSmmCoreSmiHandlers[Index].HandlerType != NULL; Index++) {\r
+    if (CompareGuid (mSmmCoreSmiHandlers[Index].HandlerType, &gEfiEventExitBootServicesGuid)) {\r
+      SmiHandlerUnRegister (mSmmCoreSmiHandlers[Index].DispatchHandle);\r
+      break;\r
+    }\r
+  }\r
+\r
   return Status;\r
 }\r
 \r
@@ -185,13 +213,14 @@ EFI_STATUS
 EFIAPI\r
 SmmExitBootServicesHandler (\r
   IN     EFI_HANDLE  DispatchHandle,\r
-  IN     CONST VOID  *Context,        OPTIONAL\r
-  IN OUT VOID        *CommBuffer,     OPTIONAL\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
+  UINTN         Index;\r
 \r
   //\r
   // Install SMM Exit Boot Services protocol.\r
@@ -206,9 +235,50 @@ SmmExitBootServicesHandler (
 \r
   SmiHandlerUnRegister (DispatchHandle);\r
 \r
+  //\r
+  // It is UEFI boot, unregister LegacyBoot SMI handler.\r
+  //\r
+  for (Index = 0; mSmmCoreSmiHandlers[Index].HandlerType != NULL; Index++) {\r
+    if (CompareGuid (mSmmCoreSmiHandlers[Index].HandlerType, &gEfiEventLegacyBootGuid)) {\r
+      SmiHandlerUnRegister (mSmmCoreSmiHandlers[Index].DispatchHandle);\r
+      break;\r
+    }\r
+  }\r
+\r
   return Status;\r
 }\r
 \r
+/**\r
+  Main entry point for an SMM handler dispatch or communicate-based callback.\r
+\r
+  @param[in]     DispatchHandle  The unique handle assigned to this handler by SmiHandlerRegister().\r
+  @param[in]     Context         Points to an optional handler context which was specified when the\r
+                                 handler was registered.\r
+  @param[in,out] 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[in,out] CommBufferSize  The size of the CommBuffer.\r
+\r
+  @retval EFI_SUCCESS                         The interrupt was handled and quiesced. No other handlers\r
+                                              should still be called.\r
+  @retval EFI_WARN_INTERRUPT_SOURCE_QUIESCED  The interrupt has been quiesced but other handlers should\r
+                                              still be called.\r
+  @retval EFI_WARN_INTERRUPT_SOURCE_PENDING   The interrupt is still pending and other handlers should still\r
+                                              be called.\r
+  @retval EFI_INTERRUPT_PENDING               The interrupt could not be quiesced.\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+SmmS3EntryCallBack (\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
+  mDuringS3Resume = TRUE;\r
+  return EFI_SUCCESS;\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
@@ -227,13 +297,13 @@ EFI_STATUS
 EFIAPI\r
 SmmReadyToBootHandler (\r
   IN     EFI_HANDLE  DispatchHandle,\r
-  IN     CONST VOID  *Context,        OPTIONAL\r
-  IN OUT VOID        *CommBuffer,     OPTIONAL\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
+  EFI_STATUS                        Status;\r
+  EFI_HANDLE                        SmmHandle;\r
 \r
   //\r
   // Install SMM Ready To Boot protocol.\r
@@ -253,10 +323,10 @@ SmmReadyToBootHandler (
 \r
 /**\r
   Software SMI handler that is called when the DxeSmmReadyToLock protocol is added\r
-  or if gEfiEventReadyToBootGuid is signalled.  This function unregisters the \r
-  Software SMIs that are nor required after SMRAM is locked and installs the \r
-  SMM Ready To Lock Protocol so SMM Drivers are informed that SMRAM is about \r
-  to be locked.  It also verifies the the SMM CPU I/O 2 Protocol has been installed\r
+  or if gEfiEventReadyToBootGuid is signalled.  This function unregisters the\r
+  Software SMIs that are nor required after SMRAM is locked and installs the\r
+  SMM Ready To Lock Protocol so SMM Drivers are informed that SMRAM is about\r
+  to be locked.  It also verifies the SMM CPU I/O 2 Protocol has been installed\r
   and NULLs gBS and gST because they can not longer be used after SMRAM is locked.\r
 \r
   @param  DispatchHandle  The unique handle assigned to this handler by SmiHandlerRegister().\r
@@ -272,8 +342,8 @@ EFI_STATUS
 EFIAPI\r
 SmmReadyToLockHandler (\r
   IN     EFI_HANDLE  DispatchHandle,\r
-  IN     CONST VOID  *Context,        OPTIONAL\r
-  IN OUT VOID        *CommBuffer,     OPTIONAL\r
+  IN     CONST VOID  *Context         OPTIONAL,\r
+  IN OUT VOID        *CommBuffer      OPTIONAL,\r
   IN OUT UINTN       *CommBufferSize  OPTIONAL\r
   )\r
 {\r
@@ -303,7 +373,7 @@ SmmReadyToLockHandler (
              );\r
 \r
   //\r
-  // Make sure SMM CPU I/O 2 Procol has been installed into the handle database\r
+  // Make sure SMM CPU I/O 2 Protocol has been installed into the handle database\r
   //\r
   Status = SmmLocateProtocol (&gEfiSmmCpuIo2ProtocolGuid, NULL, &Interface);\r
 \r
@@ -358,15 +428,19 @@ EFI_STATUS
 EFIAPI\r
 SmmEndOfDxeHandler (\r
   IN     EFI_HANDLE  DispatchHandle,\r
-  IN     CONST VOID  *Context,        OPTIONAL\r
-  IN OUT VOID        *CommBuffer,     OPTIONAL\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
+  EFI_STATUS                        Status;\r
+  EFI_HANDLE                        SmmHandle;\r
+  EFI_SMM_SX_DISPATCH2_PROTOCOL     *SxDispatch;\r
+  EFI_SMM_SX_REGISTER_CONTEXT       EntryRegisterContext;\r
+  EFI_HANDLE                        S3EntryHandle;\r
+\r
+  DEBUG ((DEBUG_INFO, "SmmEndOfDxeHandler\n"));\r
 \r
-  DEBUG ((EFI_D_INFO, "SmmEndOfDxeHandler\n"));\r
   //\r
   // Install SMM EndOfDxe protocol\r
   //\r
@@ -377,6 +451,152 @@ SmmEndOfDxeHandler (
              EFI_NATIVE_INTERFACE,\r
              NULL\r
              );\r
+\r
+  if (mAcpiS3Enable) {\r
+    //\r
+    // Locate SmmSxDispatch2 protocol.\r
+    //\r
+    Status = SmmLocateProtocol (\r
+               &gEfiSmmSxDispatch2ProtocolGuid,\r
+               NULL,\r
+               (VOID **)&SxDispatch\r
+               );\r
+    if (!EFI_ERROR (Status) && (SxDispatch != NULL)) {\r
+      //\r
+      // Register a S3 entry callback function to\r
+      // determine if it will be during S3 resume.\r
+      //\r
+      EntryRegisterContext.Type  = SxS3;\r
+      EntryRegisterContext.Phase = SxEntry;\r
+      Status = SxDispatch->Register (\r
+                             SxDispatch,\r
+                             SmmS3EntryCallBack,\r
+                             &EntryRegisterContext,\r
+                             &S3EntryHandle\r
+                             );\r
+      ASSERT_EFI_ERROR (Status);\r
+    }\r
+  }\r
+\r
+  return EFI_SUCCESS;\r
+}\r
+\r
+/**\r
+  Software SMI handler that is called when the S3SmmInitDone signal is triggered.\r
+  This function installs the SMM S3SmmInitDone Protocol so SMM Drivers are informed that\r
+  S3 SMM initialization has been done.\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
+SmmS3SmmInitDoneHandler (\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
+  DEBUG ((DEBUG_INFO, "SmmS3SmmInitDoneHandler\n"));\r
+\r
+  if (!mDuringS3Resume) {\r
+    DEBUG ((DEBUG_ERROR, "It is not during S3 resume\n"));\r
+    return EFI_SUCCESS;\r
+  }\r
+\r
+  //\r
+  // Install SMM S3SmmInitDone protocol\r
+  //\r
+  SmmHandle = NULL;\r
+  Status = SmmInstallProtocolInterface (\r
+             &SmmHandle,\r
+             &gEdkiiS3SmmInitDoneGuid,\r
+             EFI_NATIVE_INTERFACE,\r
+             NULL\r
+             );\r
+  ASSERT_EFI_ERROR (Status);\r
+\r
+  //\r
+  // Uninstall the protocol here because the comsumer just hook the\r
+  // installation event.\r
+  //\r
+  Status = SmmUninstallProtocolInterface (\r
+           SmmHandle,\r
+           &gEdkiiS3SmmInitDoneGuid,\r
+           NULL\r
+           );\r
+  ASSERT_EFI_ERROR (Status);\r
+\r
+  return Status;\r
+}\r
+\r
+/**\r
+  Software SMI handler that is called when the EndOfS3Resume signal is triggered.\r
+  This function installs the SMM EndOfS3Resume Protocol so SMM Drivers are informed that\r
+  S3 resume has finished.\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
+SmmEndOfS3ResumeHandler (\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
+  DEBUG ((DEBUG_INFO, "SmmEndOfS3ResumeHandler\n"));\r
+\r
+  if (!mDuringS3Resume) {\r
+    DEBUG ((DEBUG_ERROR, "It is not during S3 resume\n"));\r
+    return EFI_SUCCESS;\r
+  }\r
+\r
+  //\r
+  // Install SMM EndOfS3Resume protocol\r
+  //\r
+  SmmHandle = NULL;\r
+  Status = SmmInstallProtocolInterface (\r
+             &SmmHandle,\r
+             &gEdkiiEndOfS3ResumeGuid,\r
+             EFI_NATIVE_INTERFACE,\r
+             NULL\r
+             );\r
+  ASSERT_EFI_ERROR (Status);\r
+\r
+  //\r
+  // Uninstall the protocol here because the consumer just hook the\r
+  // installation event.\r
+  //\r
+  Status = SmmUninstallProtocolInterface (\r
+           SmmHandle,\r
+           &gEdkiiEndOfS3ResumeGuid,\r
+           NULL\r
+           );\r
+  ASSERT_EFI_ERROR (Status);\r
+\r
+  mDuringS3Resume = FALSE;\r
   return Status;\r
 }\r
 \r
@@ -430,8 +650,8 @@ SmmEntryPoint (
   EFI_SMM_COMMUNICATE_HEADER  *CommunicateHeader;\r
   BOOLEAN                     InLegacyBoot;\r
   BOOLEAN                     IsOverlapped;\r
-\r
-  PERF_START (NULL, "SMM", NULL, 0) ;\r
+  VOID                        *CommunicationBuffer;\r
+  UINTN                       BufferSize;\r
 \r
   //\r
   // Update SMST with contents of the SmmEntryContext structure\r
@@ -448,7 +668,12 @@ SmmEntryPoint (
   PlatformHookBeforeSmmDispatch ();\r
 \r
   //\r
-  // If a legacy boot has occured, then make sure gSmmCorePrivate is not accessed\r
+  // Call memory management hook function\r
+  //\r
+  SmmEntryPointMemoryManagementHook ();\r
+\r
+  //\r
+  // If a legacy boot has occurred, then make sure gSmmCorePrivate is not accessed\r
   //\r
   InLegacyBoot = mInLegacyBoot;\r
   if (!InLegacyBoot) {\r
@@ -458,41 +683,43 @@ SmmEntryPoint (
     gSmmCorePrivate->InSmm = TRUE;\r
 \r
     //\r
-    // Check to see if this is a Synchronous SMI sent through the SMM Communication \r
+    // Check to see if this is a Synchronous SMI sent through the SMM Communication\r
     // Protocol or an Asynchronous SMI\r
     //\r
-    if (gSmmCorePrivate->CommunicationBuffer != NULL) {\r
+    CommunicationBuffer = gSmmCorePrivate->CommunicationBuffer;\r
+    BufferSize          = gSmmCorePrivate->BufferSize;\r
+    if (CommunicationBuffer != NULL) {\r
       //\r
       // Synchronous SMI for SMM Core or request from Communicate protocol\r
       //\r
       IsOverlapped = InternalIsBufferOverlapped (\r
-                       (UINT8 *) gSmmCorePrivate->CommunicationBuffer,\r
-                       gSmmCorePrivate->BufferSize,\r
+                       (UINT8 *) CommunicationBuffer,\r
+                       BufferSize,\r
                        (UINT8 *) gSmmCorePrivate,\r
                        sizeof (*gSmmCorePrivate)\r
                        );\r
-      if (!SmmIsBufferOutsideSmmValid ((UINTN)gSmmCorePrivate->CommunicationBuffer, gSmmCorePrivate->BufferSize) || IsOverlapped) {\r
+      if (!SmmIsBufferOutsideSmmValid ((UINTN)CommunicationBuffer, 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
+        gSmmCorePrivate->ReturnStatus = EFI_ACCESS_DENIED;\r
       } else {\r
-        CommunicateHeader = (EFI_SMM_COMMUNICATE_HEADER *)gSmmCorePrivate->CommunicationBuffer;\r
-        gSmmCorePrivate->BufferSize -= OFFSET_OF (EFI_SMM_COMMUNICATE_HEADER, Data);\r
+        CommunicateHeader = (EFI_SMM_COMMUNICATE_HEADER *)CommunicationBuffer;\r
+        BufferSize -= OFFSET_OF (EFI_SMM_COMMUNICATE_HEADER, Data);\r
         Status = SmiManage (\r
-                   &CommunicateHeader->HeaderGuid, \r
-                   NULL, \r
-                   CommunicateHeader->Data, \r
-                   &gSmmCorePrivate->BufferSize\r
+                   &CommunicateHeader->HeaderGuid,\r
+                   NULL,\r
+                   CommunicateHeader->Data,\r
+                   &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->BufferSize = 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
@@ -503,14 +730,14 @@ SmmEntryPoint (
   // Process Asynchronous SMI sources\r
   //\r
   SmiManage (NULL, NULL, NULL, NULL);\r
-  \r
+\r
   //\r
   // Call platform hook after Smm Dispatch\r
   //\r
   PlatformHookAfterSmmDispatch ();\r
 \r
   //\r
-  // If a legacy boot has occured, then make sure gSmmCorePrivate is not accessed\r
+  // If a legacy boot has occurred, then make sure gSmmCorePrivate is not accessed\r
   //\r
   if (!InLegacyBoot) {\r
     //\r
@@ -518,8 +745,6 @@ SmmEntryPoint (
     //\r
     gSmmCorePrivate->InSmm = FALSE;\r
   }\r
-\r
-  PERF_END (NULL, "SMM", NULL, 0) ;\r
 }\r
 \r
 /**\r
@@ -564,13 +789,49 @@ SmmCoreInstallLoadedImage (
                   );\r
   ASSERT_EFI_ERROR (Status);\r
 \r
+  //\r
+  // Allocate a Loaded Image Protocol in SMM\r
+  //\r
+  Status = SmmAllocatePool (EfiRuntimeServicesData, sizeof(EFI_SMM_DRIVER_ENTRY), (VOID **)&mSmmCoreDriverEntry);\r
+  ASSERT_EFI_ERROR(Status);\r
+\r
+  ZeroMem (mSmmCoreDriverEntry, sizeof(EFI_SMM_DRIVER_ENTRY));\r
+  //\r
+  // Fill in the remaining fields of the Loaded Image Protocol instance.\r
+  //\r
+  mSmmCoreDriverEntry->Signature = EFI_SMM_DRIVER_ENTRY_SIGNATURE;\r
+  mSmmCoreDriverEntry->SmmLoadedImage.Revision = EFI_LOADED_IMAGE_PROTOCOL_REVISION;\r
+  mSmmCoreDriverEntry->SmmLoadedImage.ParentHandle = gSmmCorePrivate->SmmIplImageHandle;\r
+  mSmmCoreDriverEntry->SmmLoadedImage.SystemTable = gST;\r
+\r
+  mSmmCoreDriverEntry->SmmLoadedImage.ImageBase = (VOID *)(UINTN)gSmmCorePrivate->PiSmmCoreImageBase;\r
+  mSmmCoreDriverEntry->SmmLoadedImage.ImageSize = gSmmCorePrivate->PiSmmCoreImageSize;\r
+  mSmmCoreDriverEntry->SmmLoadedImage.ImageCodeType = EfiRuntimeServicesCode;\r
+  mSmmCoreDriverEntry->SmmLoadedImage.ImageDataType = EfiRuntimeServicesData;\r
+\r
+  mSmmCoreDriverEntry->ImageEntryPoint = gSmmCorePrivate->PiSmmCoreEntryPoint;\r
+  mSmmCoreDriverEntry->ImageBuffer     = gSmmCorePrivate->PiSmmCoreImageBase;\r
+  mSmmCoreDriverEntry->NumberOfPage    = EFI_SIZE_TO_PAGES((UINTN)gSmmCorePrivate->PiSmmCoreImageSize);\r
+\r
+  //\r
+  // Create a new image handle in the SMM handle database for the SMM Driver\r
+  //\r
+  mSmmCoreDriverEntry->SmmImageHandle = NULL;\r
+  Status = SmmInstallProtocolInterface (\r
+             &mSmmCoreDriverEntry->SmmImageHandle,\r
+             &gEfiLoadedImageProtocolGuid,\r
+             EFI_NATIVE_INTERFACE,\r
+             &mSmmCoreDriverEntry->SmmLoadedImage\r
+             );\r
+  ASSERT_EFI_ERROR(Status);\r
+\r
   return ;\r
 }\r
 \r
 /**\r
   The Entry Point for SMM Core\r
 \r
-  Install DXE Protocols and reload SMM Core into SMRAM and register SMM Core \r
+  Install DXE Protocols and reload SMM Core into SMRAM and register SMM Core\r
   EntryPoint on the SMI vector.\r
 \r
   Note: This function is called for both DXE invocation and SMRAM invocation.\r
@@ -602,11 +863,11 @@ SmmMain (
   //\r
   gSmmCorePrivate->Smst          = &gSmmCoreSmst;\r
   gSmmCorePrivate->SmmEntryPoint = SmmEntryPoint;\r
-  \r
+\r
   //\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
+  // so that the library linked with PiSmmCore can use AllocatePool() in constructor.\r
   //\r
 \r
   SmramProfileInit ();\r
@@ -631,9 +892,29 @@ SmmMain (
     ASSERT_EFI_ERROR (Status);\r
   }\r
 \r
+  mAcpiS3Enable = PcdGetBool (PcdAcpiS3Enable);\r
+  if (mAcpiS3Enable) {\r
+    //\r
+    // Register all S3 related SMI Handlers required by the SMM Core\r
+    //\r
+    for (Index = 0; mSmmCoreS3SmiHandlers[Index].HandlerType != NULL; Index++) {\r
+      Status = SmiHandlerRegister (\r
+                 mSmmCoreS3SmiHandlers[Index].Handler,\r
+                 mSmmCoreS3SmiHandlers[Index].HandlerType,\r
+                 &mSmmCoreS3SmiHandlers[Index].DispatchHandle\r
+                 );\r
+      ASSERT_EFI_ERROR (Status);\r
+    }\r
+  }\r
+\r
   RegisterSmramProfileHandler ();\r
+  SmramProfileInstallProtocol ();\r
 \r
   SmmCoreInstallLoadedImage ();\r
 \r
+  SmmCoreInitializeMemoryAttributesTable ();\r
+\r
+  SmmCoreInitializeSmiHandlerProfile ();\r
+\r
   return EFI_SUCCESS;\r
 }\r