]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdeModulePkg/Core/PiSmmCore/PiSmmIpl.c
MdeModulePkg: remove PE/COFF header workaround for ELILO on IPF
[mirror_edk2.git] / MdeModulePkg / Core / PiSmmCore / PiSmmIpl.c
index 50c3b34dfd34d65819ea6a5e6eaf47b55ace8784..f8cbe1704bfd78e715c42b983456fa25b1a1efbc 100644 (file)
@@ -1,14 +1,14 @@
 /** @file\r
   SMM IPL that produces SMM related runtime protocols and load the SMM Core into SMRAM\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
+  Copyright (c) 2009 - 2018, 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
+  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
 \r
 **/\r
 \r
 #include <Library/UefiLib.h>\r
 #include <Library/UefiRuntimeLib.h>\r
 #include <Library/PcdLib.h>\r
+#include <Library/ReportStatusCodeLib.h>\r
 \r
 #include "PiSmmCorePrivateData.h"\r
 \r
+#define SMRAM_CAPABILITIES  (EFI_MEMORY_WB | EFI_MEMORY_UC)\r
+\r
+#define MEMORY_CACHE_ATTRIBUTES (EFI_MEMORY_UC | EFI_MEMORY_WC | \\r
+                                 EFI_MEMORY_WT | EFI_MEMORY_WB | \\r
+                                 EFI_MEMORY_WP | EFI_MEMORY_UCE)\r
+\r
+#define MEMORY_PAGE_ATTRIBUTES  (EFI_MEMORY_XP | EFI_MEMORY_RP | EFI_MEMORY_RO)\r
+\r
 //\r
 // Function prototypes from produced protocols\r
 //\r
@@ -83,26 +92,36 @@ SmmBase2GetSmstLocation (
 \r
 /**\r
   Communicates with a registered handler.\r
-  \r
-  This function provides a service to send and receive messages from a registered \r
-  UEFI service.  This function is part of the SMM Communication Protocol that may \r
-  be called in physical mode prior to SetVirtualAddressMap() and in virtual mode \r
+\r
+  This function provides a service to send and receive messages from a registered\r
+  UEFI service.  This function is part of the SMM Communication Protocol that may\r
+  be called in physical mode prior to SetVirtualAddressMap() and in virtual mode\r
   after SetVirtualAddressMap().\r
 \r
-  @param[in]     This                The EFI_SMM_COMMUNICATION_PROTOCOL instance.\r
-  @param[in, out] CommBuffer          A pointer to the buffer to convey into SMRAM.\r
-  @param[in, out] CommSize            The size of the data buffer being passed in.On exit, the size of data\r
-                                     being returned. Zero if the handler does not wish to reply with any data.\r
+  @param[in] This                The EFI_SMM_COMMUNICATION_PROTOCOL instance.\r
+  @param[in, out] CommBuffer     A pointer to the buffer to convey into SMRAM.\r
+  @param[in, out] CommSize       The size of the data buffer being passed in. On exit, the size of data\r
+                                 being returned. Zero if the handler does not wish to reply with any data.\r
+                                 This parameter is optional and may be NULL.\r
+\r
+  @retval EFI_SUCCESS            The message was successfully posted.\r
+  @retval EFI_INVALID_PARAMETER  The CommBuffer was NULL.\r
+  @retval EFI_BAD_BUFFER_SIZE    The buffer is too large for the MM implementation.\r
+                                 If this error is returned, the MessageLength field\r
+                                 in the CommBuffer header or the integer pointed by\r
+                                 CommSize, are updated to reflect the maximum payload\r
+                                 size the implementation can accommodate.\r
+  @retval EFI_ACCESS_DENIED      The CommunicateBuffer parameter or CommSize parameter,\r
+                                 if not omitted, are in address range that cannot be\r
+                                 accessed by the MM environment.\r
 \r
-  @retval EFI_SUCCESS                The message was successfully posted.\r
-  @retval EFI_INVALID_PARAMETER      The CommBuffer was NULL.\r
 **/\r
 EFI_STATUS\r
 EFIAPI\r
 SmmCommunicationCommunicate (\r
   IN CONST EFI_SMM_COMMUNICATION_PROTOCOL  *This,\r
   IN OUT VOID                              *CommBuffer,\r
-  IN OUT UINTN                             *CommSize\r
+  IN OUT UINTN                             *CommSize OPTIONAL\r
   );\r
 \r
 /**\r
@@ -162,6 +181,20 @@ SmmIplGuidedEventNotify (
   IN VOID       *Context\r
   );\r
 \r
+/**\r
+  Event notification that is fired when EndOfDxe Event Group is signaled.\r
+\r
+  @param  Event                 The Event that is being processed, not used.\r
+  @param  Context               Event Context, not used.\r
+\r
+**/\r
+VOID\r
+EFIAPI\r
+SmmIplEndOfDxeEventNotify (\r
+  IN EFI_EVENT  Event,\r
+  IN VOID       *Context\r
+  );\r
+\r
 /**\r
   Notification function of EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE.\r
 \r
@@ -180,7 +213,7 @@ SmmIplSetVirtualAddressNotify (
   );\r
 \r
 //\r
-// Data structure used to declare a table of protocol notifications and event \r
+// Data structure used to declare a table of protocol notifications and event\r
 // notifications required by the SMM IPL\r
 //\r
 typedef struct {\r
@@ -243,34 +276,40 @@ EFI_SMM_CONTROL2_PROTOCOL  *mSmmControl2;
 EFI_SMM_ACCESS2_PROTOCOL   *mSmmAccess;\r
 EFI_SMRAM_DESCRIPTOR       *mCurrentSmramRange;\r
 BOOLEAN                    mSmmLocked = FALSE;\r
+BOOLEAN                    mEndOfDxe  = FALSE;\r
 EFI_PHYSICAL_ADDRESS       mSmramCacheBase;\r
 UINT64                     mSmramCacheSize;\r
 \r
 EFI_SMM_COMMUNICATE_HEADER mCommunicateHeader;\r
+EFI_LOAD_FIXED_ADDRESS_CONFIGURATION_TABLE    *mLMFAConfigurationTable = NULL;\r
 \r
 //\r
 // Table of Protocol notification and GUIDed Event notifications that the SMM IPL requires\r
 //\r
 SMM_IPL_EVENT_NOTIFICATION  mSmmIplEvents[] = {\r
   //\r
-  // Declare protocol notification on the SMM Configuration protocol.  When this notification is etablished, \r
-  // the associated event is immediately signalled, so the notification function will be executed and the \r
+  // Declare protocol notification on the SMM Configuration protocol.  When this notification is established,\r
+  // the associated event is immediately signalled, so the notification function will be executed and the\r
   // SMM Configuration Protocol will be found if it is already in the handle database.\r
   //\r
   { TRUE,  FALSE, &gEfiSmmConfigurationProtocolGuid,  SmmIplSmmConfigurationEventNotify, &gEfiSmmConfigurationProtocolGuid,  TPL_NOTIFY,   NULL },\r
   //\r
-  // Declare protocol notification on DxeSmmReadyToLock protocols.  When this notification is established, \r
-  // the associated event is immediately signalled, so the notification function will be executed and the \r
+  // Declare protocol notification on DxeSmmReadyToLock protocols.  When this notification is established,\r
+  // the associated event is immediately signalled, so the notification function will be executed and the\r
   // DXE SMM Ready To Lock Protocol will be found if it is already in the handle database.\r
   //\r
   { TRUE,  TRUE,  &gEfiDxeSmmReadyToLockProtocolGuid, SmmIplReadyToLockEventNotify,      &gEfiDxeSmmReadyToLockProtocolGuid, TPL_CALLBACK, NULL },\r
   //\r
-  // Declare event notification on EndOfDxe event.  When this notification is etablished, \r
-  // the associated event is immediately signalled, so the notification function will be executed and the \r
+  // Declare event notification on EndOfDxe event.  When this notification is established,\r
+  // the associated event is immediately signalled, so the notification function will be executed and the\r
   // SMM End Of Dxe Protocol will be found if it is already in the handle database.\r
   //\r
   { FALSE, TRUE,  &gEfiEndOfDxeEventGroupGuid,        SmmIplGuidedEventNotify,           &gEfiEndOfDxeEventGroupGuid,        TPL_CALLBACK, NULL },\r
   //\r
+  // Declare event notification on EndOfDxe event.  This is used to set EndOfDxe event signaled flag.\r
+  //\r
+  { FALSE, TRUE,  &gEfiEndOfDxeEventGroupGuid,        SmmIplEndOfDxeEventNotify,         &gEfiEndOfDxeEventGroupGuid,        TPL_CALLBACK, NULL },\r
+  //\r
   // Declare event notification on the DXE Dispatch Event Group.  This event is signaled by the DXE Core\r
   // each time the DXE Core dispatcher has completed its work.  When this event is signalled, the SMM Core\r
   // if notified, so the SMM Core can dispatch SMM drivers.\r
@@ -282,8 +321,8 @@ SMM_IPL_EVENT_NOTIFICATION  mSmmIplEvents[] = {
   //\r
   { FALSE, TRUE,  &gEfiEventReadyToBootGuid,          SmmIplReadyToLockEventNotify,      &gEfiEventReadyToBootGuid,          TPL_CALLBACK, NULL },\r
   //\r
-  // Declare event notification on Legacy Boot Event Group.  This is used to inform the SMM Core that the platform \r
-  // is performing a legacy boot operation, and that the UEFI environment is no longer available and the SMM Core \r
+  // Declare event notification on Legacy Boot Event Group.  This is used to inform the SMM Core that the platform\r
+  // is performing a legacy boot operation, and that the UEFI environment is no longer available and the SMM Core\r
   // must guarantee that it does not access any UEFI related structures outside of SMRAM.\r
   // It is also to inform the SMM Core to notify SMM driver that system enter legacy boot.\r
   //\r
@@ -299,7 +338,7 @@ SMM_IPL_EVENT_NOTIFICATION  mSmmIplEvents[] = {
   //\r
   { FALSE, FALSE, &gEfiEventReadyToBootGuid,          SmmIplGuidedEventNotify,           &gEfiEventReadyToBootGuid,          TPL_CALLBACK, NULL },\r
   //\r
-  // Declare event notification on SetVirtualAddressMap() Event Group.  This is used to convert gSmmCorePrivate \r
+  // Declare event notification on SetVirtualAddressMap() Event Group.  This is used to convert gSmmCorePrivate\r
   // and mSmmControl2 from physical addresses to virtual addresses.\r
   //\r
   { FALSE, FALSE, &gEfiEventVirtualAddressChangeGuid, SmmIplSetVirtualAddressNotify,     NULL,                               TPL_CALLBACK, NULL },\r
@@ -311,7 +350,7 @@ SMM_IPL_EVENT_NOTIFICATION  mSmmIplEvents[] = {
 \r
 /**\r
   Find the maximum SMRAM cache range that covers the range specified by SmramRange.\r
-  \r
+\r
   This function searches and joins all adjacent ranges of SmramRange into a range to be cached.\r
 \r
   @param   SmramRange       The SMRAM range to search from.\r
@@ -349,7 +388,7 @@ GetSmramCacheRange (
       }\r
     }\r
   } while (FoundAjacentRange);\r
-  \r
+\r
 }\r
 \r
 /**\r
@@ -400,11 +439,11 @@ SmmBase2GetSmstLocation (
   if ((This == NULL) ||(Smst == NULL)) {\r
     return EFI_INVALID_PARAMETER;\r
   }\r
-  \r
+\r
   if (!gSmmCorePrivate->InSmm) {\r
     return EFI_UNSUPPORTED;\r
   }\r
-  \r
+\r
   *Smst = gSmmCorePrivate->Smst;\r
 \r
   return EFI_SUCCESS;\r
@@ -412,44 +451,62 @@ SmmBase2GetSmstLocation (
 \r
 /**\r
   Communicates with a registered handler.\r
-  \r
-  This function provides a service to send and receive messages from a registered \r
-  UEFI service.  This function is part of the SMM Communication Protocol that may \r
-  be called in physical mode prior to SetVirtualAddressMap() and in virtual mode \r
+\r
+  This function provides a service to send and receive messages from a registered\r
+  UEFI service.  This function is part of the SMM Communication Protocol that may\r
+  be called in physical mode prior to SetVirtualAddressMap() and in virtual mode\r
   after SetVirtualAddressMap().\r
 \r
   @param[in] This                The EFI_SMM_COMMUNICATION_PROTOCOL instance.\r
-  @param[in, out] CommBuffer          A pointer to the buffer to convey into SMRAM.\r
-  @param[in, out] CommSize            The size of the data buffer being passed in.On exit, the size of data\r
+  @param[in, out] CommBuffer     A pointer to the buffer to convey into SMRAM.\r
+  @param[in, out] CommSize       The size of the data buffer being passed in. On exit, the size of data\r
                                  being returned. Zero if the handler does not wish to reply with any data.\r
+                                 This parameter is optional and may be NULL.\r
 \r
   @retval EFI_SUCCESS            The message was successfully posted.\r
   @retval EFI_INVALID_PARAMETER  The CommBuffer was NULL.\r
+  @retval EFI_BAD_BUFFER_SIZE    The buffer is too large for the MM implementation.\r
+                                 If this error is returned, the MessageLength field\r
+                                 in the CommBuffer header or the integer pointed by\r
+                                 CommSize, are updated to reflect the maximum payload\r
+                                 size the implementation can accommodate.\r
+  @retval EFI_ACCESS_DENIED      The CommunicateBuffer parameter or CommSize parameter,\r
+                                 if not omitted, are in address range that cannot be\r
+                                 accessed by the MM environment.\r
+\r
 **/\r
 EFI_STATUS\r
 EFIAPI\r
 SmmCommunicationCommunicate (\r
   IN CONST EFI_SMM_COMMUNICATION_PROTOCOL  *This,\r
   IN OUT VOID                              *CommBuffer,\r
-  IN OUT UINTN                             *CommSize\r
+  IN OUT UINTN                             *CommSize OPTIONAL\r
   )\r
 {\r
   EFI_STATUS                  Status;\r
   EFI_SMM_COMMUNICATE_HEADER  *CommunicateHeader;\r
   BOOLEAN                     OldInSmm;\r
+  UINTN                       TempCommSize;\r
 \r
   //\r
   // Check parameters\r
   //\r
-  if ((CommBuffer == NULL) || (CommSize == NULL)) {\r
+  if (CommBuffer == NULL) {\r
     return EFI_INVALID_PARAMETER;\r
   }\r
 \r
-  //\r
-  // CommSize must hold HeaderGuid and MessageLength\r
-  //\r
-  if (*CommSize < OFFSET_OF (EFI_SMM_COMMUNICATE_HEADER, Data)) {\r
-    return EFI_INVALID_PARAMETER;\r
+  CommunicateHeader = (EFI_SMM_COMMUNICATE_HEADER *) CommBuffer;\r
+\r
+  if (CommSize == NULL) {\r
+    TempCommSize = OFFSET_OF (EFI_SMM_COMMUNICATE_HEADER, Data) + CommunicateHeader->MessageLength;\r
+  } else {\r
+    TempCommSize = *CommSize;\r
+    //\r
+    // CommSize must hold HeaderGuid and MessageLength\r
+    //\r
+    if (TempCommSize < OFFSET_OF (EFI_SMM_COMMUNICATE_HEADER, Data)) {\r
+      return EFI_INVALID_PARAMETER;\r
+    }\r
   }\r
 \r
   //\r
@@ -460,7 +517,7 @@ SmmCommunicationCommunicate (
     // Put arguments for Software SMI in gSmmCorePrivate\r
     //\r
     gSmmCorePrivate->CommunicationBuffer = CommBuffer;\r
-    gSmmCorePrivate->BufferSize          = *CommSize;\r
+    gSmmCorePrivate->BufferSize          = TempCommSize;\r
 \r
     //\r
     // Generate Software SMI\r
@@ -471,17 +528,19 @@ SmmCommunicationCommunicate (
     }\r
 \r
     //\r
-    // Return status from software SMI \r
+    // Return status from software SMI\r
     //\r
-    *CommSize = gSmmCorePrivate->BufferSize;\r
+    if (CommSize != NULL) {\r
+      *CommSize = gSmmCorePrivate->BufferSize;\r
+    }\r
     return gSmmCorePrivate->ReturnStatus;\r
   }\r
 \r
   //\r
   // If we are in SMM, then the execution mode must be physical, which means that\r
   // OS established virtual addresses can not be used.  If SetVirtualAddressMap()\r
-  // has been called, then a direct invocation of the Software SMI is not \r
-  // not allowed so return EFI_INVALID_PARAMETER.\r
+  // has been called, then a direct invocation of the Software SMI is not allowed,\r
+  // so return EFI_INVALID_PARAMETER.\r
   //\r
   if (EfiGoneVirtual()) {\r
     return EFI_INVALID_PARAMETER;\r
@@ -493,7 +552,7 @@ SmmCommunicationCommunicate (
   if ((!gSmmCorePrivate->InSmm) && (!mSmmAccess->OpenState || mSmmAccess->LockState)) {\r
     return EFI_INVALID_PARAMETER;\r
   }\r
\r
+\r
   //\r
   // Save current InSmm state and set InSmm state to TRUE\r
   //\r
@@ -501,22 +560,19 @@ SmmCommunicationCommunicate (
   gSmmCorePrivate->InSmm = TRUE;\r
 \r
   //\r
-  // Already in SMM and before SetVirtualAddressMap(), so call SmiManage() directly.\r
+  // Before SetVirtualAddressMap(), we are in SMM or SMRAM is open and unlocked, call SmiManage() directly.\r
   //\r
-  CommunicateHeader = (EFI_SMM_COMMUNICATE_HEADER *)CommBuffer;\r
-  *CommSize -= OFFSET_OF (EFI_SMM_COMMUNICATE_HEADER, Data);\r
+  TempCommSize -= OFFSET_OF (EFI_SMM_COMMUNICATE_HEADER, Data);\r
   Status = gSmmCorePrivate->Smst->SmiManage (\r
-                                    &CommunicateHeader->HeaderGuid, \r
-                                    NULL, \r
-                                    CommunicateHeader->Data, \r
-                                    CommSize\r
+                                    &CommunicateHeader->HeaderGuid,\r
+                                    NULL,\r
+                                    CommunicateHeader->Data,\r
+                                    &TempCommSize\r
                                     );\r
-\r
-  //\r
-  // Update CommunicationBuffer, BufferSize and ReturnStatus\r
-  // Communicate service finished, reset the pointer to CommBuffer to NULL\r
-  //\r
-  *CommSize += OFFSET_OF (EFI_SMM_COMMUNICATE_HEADER, Data);\r
+  TempCommSize += OFFSET_OF (EFI_SMM_COMMUNICATE_HEADER, Data);\r
+  if (CommSize != NULL) {\r
+    *CommSize = TempCommSize;\r
+  }\r
 \r
   //\r
   // Restore original InSmm state\r
@@ -543,7 +599,7 @@ SmmIplGuidedEventNotify (
   UINTN                       Size;\r
 \r
   //\r
-  // Use Guid to initialize EFI_SMM_COMMUNICATE_HEADER structure \r
+  // Use Guid to initialize EFI_SMM_COMMUNICATE_HEADER structure\r
   //\r
   CopyGuid (&mCommunicateHeader.HeaderGuid, (EFI_GUID *)Context);\r
   mCommunicateHeader.MessageLength = 1;\r
@@ -556,6 +612,23 @@ SmmIplGuidedEventNotify (
   SmmCommunicationCommunicate (&mSmmCommunication, &mCommunicateHeader, &Size);\r
 }\r
 \r
+/**\r
+  Event notification that is fired when EndOfDxe Event Group is signaled.\r
+\r
+  @param  Event                 The Event that is being processed, not used.\r
+  @param  Context               Event Context, not used.\r
+\r
+**/\r
+VOID\r
+EFIAPI\r
+SmmIplEndOfDxeEventNotify (\r
+  IN EFI_EVENT  Event,\r
+  IN VOID       *Context\r
+  )\r
+{\r
+  mEndOfDxe = TRUE;\r
+}\r
+\r
 /**\r
   Event notification that is fired when DxeDispatch Event Group is signaled.\r
 \r
@@ -591,26 +664,26 @@ SmmIplDxeDispatchEventNotify (
     //\r
     Size = sizeof (mCommunicateHeader);\r
     SmmCommunicationCommunicate (&mSmmCommunication, &mCommunicateHeader, &Size);\r
-    \r
+\r
     //\r
     // Return if there is no request to restart the SMM Core Dispatcher\r
     //\r
     if (mCommunicateHeader.Data[0] != COMM_BUFFER_SMM_DISPATCH_RESTART) {\r
       return;\r
     }\r
-      \r
+\r
     //\r
     // Attempt to reset SMRAM cacheability to UC\r
     // Assume CPU AP is available at this time\r
     //\r
     Status = gDS->SetMemorySpaceAttributes(\r
-                    mSmramCacheBase, \r
+                    mSmramCacheBase,\r
                     mSmramCacheSize,\r
                     EFI_MEMORY_UC\r
                     );\r
     if (EFI_ERROR (Status)) {\r
       DEBUG ((DEBUG_WARN, "SMM IPL failed to reset SMRAM window to EFI_MEMORY_UC\n"));\r
-    }  \r
+    }\r
 \r
     //\r
     // Close all SMRAM ranges to protect SMRAM\r
@@ -657,7 +730,7 @@ SmmIplSmmConfigurationEventNotify (
   ASSERT_EFI_ERROR (Status);\r
 \r
   //\r
-  // Set flag to indicate that the SMM Entry Point has been registered which \r
+  // Set flag to indicate that the SMM Entry Point has been registered which\r
   // means that SMIs are now fully operational.\r
   //\r
   gSmmCorePrivate->SmmEntryPointRegistered = TRUE;\r
@@ -693,7 +766,7 @@ SmmIplReadyToLockEventNotify (
   if (mSmmLocked) {\r
     return;\r
   }\r
-  \r
+\r
   //\r
   // Make sure this notification is for this handler\r
   //\r
@@ -704,21 +777,30 @@ SmmIplReadyToLockEventNotify (
     }\r
   } else {\r
     //\r
-    // If SMM is not locked yet and we got here from gEfiEventReadyToBootGuid being \r
+    // If SMM is not locked yet and we got here from gEfiEventReadyToBootGuid being\r
     // signaled, then gEfiDxeSmmReadyToLockProtocolGuid was not installed as expected.\r
     // Print a warning on debug builds.\r
     //\r
     DEBUG ((DEBUG_WARN, "SMM IPL!  DXE SMM Ready To Lock Protocol not installed before Ready To Boot signal\n"));\r
   }\r
 \r
+  if (!mEndOfDxe) {\r
+    DEBUG ((DEBUG_ERROR, "EndOfDxe Event must be signaled before DxeSmmReadyToLock Protocol installation!\n"));\r
+    REPORT_STATUS_CODE (\r
+      EFI_ERROR_CODE | EFI_ERROR_UNRECOVERED,\r
+      (EFI_SOFTWARE_SMM_DRIVER | EFI_SW_EC_ILLEGAL_SOFTWARE_STATE)\r
+      );\r
+    ASSERT (FALSE);\r
+  }\r
+\r
   //\r
   // Lock the SMRAM (Note: Locking SMRAM may not be supported on all platforms)\r
   //\r
   mSmmAccess->Lock (mSmmAccess);\r
-  \r
+\r
   //\r
-  // Close protocol and event notification events that do not apply after the \r
-  // DXE SMM Ready To Lock Protocol has been installed or the Ready To Boot \r
+  // Close protocol and event notification events that do not apply after the\r
+  // DXE SMM Ready To Lock Protocol has been installed or the Ready To Boot\r
   // event has been signalled.\r
   //\r
   for (Index = 0; mSmmIplEvents[Index].NotifyFunction != NULL; Index++) {\r
@@ -736,7 +818,7 @@ SmmIplReadyToLockEventNotify (
   // Print debug message that the SMRAM window is now locked.\r
   //\r
   DEBUG ((DEBUG_INFO, "SMM IPL locked SMRAM window\n"));\r
-  \r
+\r
   //\r
   // Set flag so this operation will not be performed again\r
   //\r
@@ -764,13 +846,13 @@ SmmIplSetVirtualAddressNotify (
 }\r
 \r
 /**\r
-  Get the fixed loadding address from image header assigned by build tool. This function only be called\r
+  Get the fixed loading address from image header assigned by build tool. This function only be called\r
   when Loading module at Fixed address feature enabled.\r
 \r
   @param  ImageContext              Pointer to the image context structure that describes the PE/COFF\r
                                     image that needs to be examined by this function.\r
   @retval EFI_SUCCESS               An fixed loading address is assigned to this image by build tools .\r
-  @retval EFI_NOT_FOUND             The image has no assigned fixed loadding address.\r
+  @retval EFI_NOT_FOUND             The image has no assigned fixed loading address.\r
 **/\r
 EFI_STATUS\r
 GetPeCoffImageFixLoadingAssignedAddress(\r
@@ -781,7 +863,7 @@ GetPeCoffImageFixLoadingAssignedAddress(
    EFI_STATUS                         Status;\r
    EFI_IMAGE_SECTION_HEADER           SectionHeader;\r
    EFI_IMAGE_OPTIONAL_HEADER_UNION    *ImgHdr;\r
-   EFI_PHYSICAL_ADDRESS               FixLoaddingAddress;\r
+   EFI_PHYSICAL_ADDRESS               FixLoadingAddress;\r
    UINT16                             Index;\r
    UINTN                              Size;\r
    UINT16                             NumberOfSections;\r
@@ -792,20 +874,18 @@ GetPeCoffImageFixLoadingAssignedAddress(
    // Build tool will calculate the smm code size and then patch the PcdLoadFixAddressSmmCodePageNumber\r
    //\r
    SmmCodeSize = EFI_PAGES_TO_SIZE (PcdGet32(PcdLoadFixAddressSmmCodePageNumber));\r
\r
-   FixLoaddingAddress = 0;\r
+\r
+   FixLoadingAddress = 0;\r
    Status = EFI_NOT_FOUND;\r
-   SmramBase = mCurrentSmramRange->CpuStart;\r
+   SmramBase = mLMFAConfigurationTable->SmramBase;\r
    //\r
    // Get PeHeader pointer\r
    //\r
    ImgHdr = (EFI_IMAGE_OPTIONAL_HEADER_UNION *)((CHAR8* )ImageContext->Handle + ImageContext->PeCoffHeaderOffset);\r
-   SectionHeaderOffset = (UINTN)(\r
-                                 ImageContext->PeCoffHeaderOffset +\r
-                                 sizeof (UINT32) +\r
-                                 sizeof (EFI_IMAGE_FILE_HEADER) +\r
-                                 ImgHdr->Pe32.FileHeader.SizeOfOptionalHeader\r
-                                 );\r
+   SectionHeaderOffset = ImageContext->PeCoffHeaderOffset +\r
+                         sizeof (UINT32) +\r
+                         sizeof (EFI_IMAGE_FILE_HEADER) +\r
+                         ImgHdr->Pe32.FileHeader.SizeOfOptionalHeader;\r
    NumberOfSections = ImgHdr->Pe32.FileHeader.NumberOfSections;\r
 \r
    //\r
@@ -825,29 +905,29 @@ GetPeCoffImageFixLoadingAssignedAddress(
      if (EFI_ERROR (Status)) {\r
        return Status;\r
      }\r
-     \r
+\r
      Status = EFI_NOT_FOUND;\r
-     \r
+\r
      if ((SectionHeader.Characteristics & EFI_IMAGE_SCN_CNT_CODE) == 0) {\r
        //\r
        // Build tool saves the offset to SMRAM base as image base in PointerToRelocations & PointerToLineNumbers fields in the\r
        // first section header that doesn't point to code section in image header. And there is an assumption that when the\r
        // feature is enabled, if a module is assigned a loading address by tools, PointerToRelocations & PointerToLineNumbers\r
-       // fields should NOT be Zero, or else, these 2 fileds should be set to Zero\r
+       // fields should NOT be Zero, or else, these 2 fields should be set to Zero\r
        //\r
        ValueInSectionHeader = ReadUnaligned64((UINT64*)&SectionHeader.PointerToRelocations);\r
        if (ValueInSectionHeader != 0) {\r
          //\r
-         // Found first section header that doesn't point to code section in which uild tool saves the\r
+         // Found first section header that doesn't point to code section in which build tool saves the\r
          // offset to SMRAM base as image base in PointerToRelocations & PointerToLineNumbers fields\r
          //\r
-         FixLoaddingAddress = (EFI_PHYSICAL_ADDRESS)(SmramBase + (INT64)ValueInSectionHeader);\r
+         FixLoadingAddress = (EFI_PHYSICAL_ADDRESS)(SmramBase + (INT64)ValueInSectionHeader);\r
 \r
-         if (SmramBase + SmmCodeSize > FixLoaddingAddress && SmramBase <=  FixLoaddingAddress) {\r
+         if (SmramBase + SmmCodeSize > FixLoadingAddress && SmramBase <=  FixLoadingAddress) {\r
            //\r
-           // The assigned address is valid. Return the specified loadding address\r
+           // The assigned address is valid. Return the specified loading address\r
            //\r
-           ImageContext->ImageAddress = FixLoaddingAddress;\r
+           ImageContext->ImageAddress = FixLoadingAddress;\r
            Status = EFI_SUCCESS;\r
          }\r
        }\r
@@ -855,13 +935,13 @@ GetPeCoffImageFixLoadingAssignedAddress(
      }\r
      SectionHeaderOffset += sizeof (EFI_IMAGE_SECTION_HEADER);\r
    }\r
-   DEBUG ((EFI_D_INFO|EFI_D_LOAD, "LOADING MODULE FIXED INFO: Loading module at fixed address %x, Status = %r \n", FixLoaddingAddress, Status));\r
+   DEBUG ((EFI_D_INFO|EFI_D_LOAD, "LOADING MODULE FIXED INFO: Loading module at fixed address %x, Status = %r \n", FixLoadingAddress, Status));\r
    return Status;\r
 }\r
 /**\r
   Load the SMM Core image into SMRAM and executes the SMM Core from SMRAM.\r
 \r
-  @param[in, out] SmramRange            Descriptor for the range of SMRAM to reload the \r
+  @param[in, out] SmramRange            Descriptor for the range of SMRAM to reload the\r
                                         currently executing image, the rang of SMRAM to\r
                                         hold SMM Core will be excluded.\r
   @param[in, out] SmramRangeSmmCore     Descriptor for the range of SMRAM to hold SMM Core.\r
@@ -887,19 +967,19 @@ ExecuteSmmCoreFromSmram (
 \r
   //\r
   // Search all Firmware Volumes for a PE/COFF image in a file of type SMM_CORE\r
-  //  \r
+  //\r
   Status = GetSectionFromAnyFvByFileType (\r
-             EFI_FV_FILETYPE_SMM_CORE, \r
+             EFI_FV_FILETYPE_SMM_CORE,\r
              0,\r
-             EFI_SECTION_PE32, \r
+             EFI_SECTION_PE32,\r
              0,\r
-             &SourceBuffer, \r
+             &SourceBuffer,\r
              &SourceSize\r
              );\r
   if (EFI_ERROR (Status)) {\r
     return Status;\r
   }\r
-  \r
+\r
   //\r
   // Initilize ImageContext\r
   //\r
@@ -914,7 +994,7 @@ ExecuteSmmCoreFromSmram (
     return Status;\r
   }\r
   //\r
-  // if Loading module at Fixed Address feature is enabled, the SMM core driver will be loaded to \r
+  // if Loading module at Fixed Address feature is enabled, the SMM core driver will be loaded to\r
   // the address assigned by build tool.\r
   //\r
   if (PcdGet64(PcdLoadModuleAtFixAddressEnable) != 0) {\r
@@ -927,10 +1007,14 @@ ExecuteSmmCoreFromSmram (
       // Since the memory range to load SMM CORE will be cut out in SMM core, so no need to allocate and free this range\r
       //\r
       PageCount = 0;\r
+      //\r
+      // Reserved Smram Region for SmmCore is not used, and remove it from SmramRangeCount.\r
+      //\r
+      gSmmCorePrivate->SmramRangeCount --;\r
     } else {\r
       DEBUG ((EFI_D_INFO, "LOADING MODULE FIXED ERROR: Loading module at fixed address at address failed\n"));\r
       //\r
-      // Allocate memory for the image being loaded from the EFI_SRAM_DESCRIPTOR \r
+      // Allocate memory for the image being loaded from the EFI_SRAM_DESCRIPTOR\r
       // specified by SmramRange\r
       //\r
       PageCount = (UINTN)EFI_SIZE_TO_PAGES((UINTN)ImageContext.ImageSize + ImageContext.SectionAlignment);\r
@@ -951,7 +1035,7 @@ ExecuteSmmCoreFromSmram (
     }\r
   } else {\r
     //\r
-    // Allocate memory for the image being loaded from the EFI_SRAM_DESCRIPTOR \r
+    // Allocate memory for the image being loaded from the EFI_SRAM_DESCRIPTOR\r
     // specified by SmramRange\r
     //\r
     PageCount = (UINTN)EFI_SIZE_TO_PAGES((UINTN)ImageContext.ImageSize + ImageContext.SectionAlignment);\r
@@ -970,9 +1054,9 @@ ExecuteSmmCoreFromSmram (
     //\r
     ImageContext.ImageAddress = SmramRangeSmmCore->CpuStart;\r
   }\r
-  \r
+\r
   ImageContext.ImageAddress += ImageContext.SectionAlignment - 1;\r
-  ImageContext.ImageAddress &= ~((EFI_PHYSICAL_ADDRESS)(ImageContext.SectionAlignment - 1));\r
+  ImageContext.ImageAddress &= ~((EFI_PHYSICAL_ADDRESS)ImageContext.SectionAlignment - 1);\r
 \r
   //\r
   // Print debug message showing SMM Core load address.\r
@@ -1253,6 +1337,7 @@ GetFullSmramRanges (
   UINTN                             Index2;\r
   EFI_SMRAM_DESCRIPTOR              *FullSmramRanges;\r
   UINTN                             TempSmramRangeCount;\r
+  UINTN                             AdditionSmramRangeCount;\r
   EFI_SMRAM_DESCRIPTOR              *TempSmramRanges;\r
   UINTN                             SmramRangeCount;\r
   EFI_SMRAM_DESCRIPTOR              *SmramRanges;\r
@@ -1286,12 +1371,22 @@ GetFullSmramRanges (
     }\r
   }\r
 \r
+  //\r
+  // Reserve one entry for SMM Core in the full SMRAM ranges.\r
+  //\r
+  AdditionSmramRangeCount = 1;\r
+  if (PcdGet64(PcdLoadModuleAtFixAddressEnable) != 0) {\r
+    //\r
+    // Reserve two entries for all SMM drivers and SMM Core in the full SMRAM ranges.\r
+    //\r
+    AdditionSmramRangeCount = 2;\r
+  }\r
+\r
   if (SmramReservedCount == 0) {\r
     //\r
     // No reserved SMRAM entry from SMM Configuration Protocol.\r
-    // Reserve one entry for SMM Core in the full SMRAM ranges.\r
     //\r
-    *FullSmramRangeCount = SmramRangeCount + 1;\r
+    *FullSmramRangeCount = SmramRangeCount + AdditionSmramRangeCount;\r
     Size = (*FullSmramRangeCount) * sizeof (EFI_SMRAM_DESCRIPTOR);\r
     FullSmramRanges = (EFI_SMRAM_DESCRIPTOR *) AllocateZeroPool (Size);\r
     ASSERT (FullSmramRanges != NULL);\r
@@ -1403,10 +1498,9 @@ GetFullSmramRanges (
   ASSERT (TempSmramRangeCount <= MaxCount);\r
 \r
   //\r
-  // Sort the entries,\r
-  // and reserve one entry for SMM Core in the full SMRAM ranges.\r
+  // Sort the entries\r
   //\r
-  FullSmramRanges = AllocateZeroPool ((TempSmramRangeCount + 1) * sizeof (EFI_SMRAM_DESCRIPTOR));\r
+  FullSmramRanges = AllocateZeroPool ((TempSmramRangeCount + AdditionSmramRangeCount) * sizeof (EFI_SMRAM_DESCRIPTOR));\r
   ASSERT (FullSmramRanges != NULL);\r
   *FullSmramRangeCount = 0;\r
   do {\r
@@ -1426,7 +1520,7 @@ GetFullSmramRanges (
     TempSmramRanges[Index].PhysicalSize = 0;\r
   } while (*FullSmramRangeCount < TempSmramRangeCount);\r
   ASSERT (*FullSmramRangeCount == TempSmramRangeCount);\r
-  *FullSmramRangeCount += 1;\r
+  *FullSmramRangeCount += AdditionSmramRangeCount;\r
 \r
   FreePool (SmramRanges);\r
   FreePool (SmramReservedRanges);\r
@@ -1438,10 +1532,10 @@ GetFullSmramRanges (
 /**\r
   The Entry Point for SMM IPL\r
 \r
-  Load SMM Core into SMRAM, register SMM Core entry point for SMIs, install \r
-  SMM Base 2 Protocol and SMM Communication Protocol, and register for the \r
+  Load SMM Core into SMRAM, register SMM Core entry point for SMIs, install\r
+  SMM Base 2 Protocol and SMM Communication Protocol, and register for the\r
   critical events required to coordinate between DXE and SMM environments.\r
-  \r
+\r
   @param  ImageHandle    The firmware allocated handle for the EFI image.\r
   @param  SystemTable    A pointer to the EFI System Table.\r
 \r
@@ -1461,13 +1555,14 @@ SmmIplEntry (
   UINT64                          MaxSize;\r
   VOID                            *Registration;\r
   UINT64                          SmmCodeSize;\r
-  EFI_LOAD_FIXED_ADDRESS_CONFIGURATION_TABLE    *LMFAConfigurationTable;\r
   EFI_CPU_ARCH_PROTOCOL           *CpuArch;\r
   EFI_STATUS                      SetAttrStatus;\r
+  EFI_SMRAM_DESCRIPTOR            *SmramRangeSmmDriver;\r
+  EFI_GCD_MEMORY_SPACE_DESCRIPTOR MemDesc;\r
 \r
   //\r
-  // Fill in the image handle of the SMM IPL so the SMM Core can use this as the \r
-  // ParentImageHandle field of the Load Image Protocol for all SMM Drivers loaded \r
+  // Fill in the image handle of the SMM IPL so the SMM Core can use this as the\r
+  // ParentImageHandle field of the Load Image Protocol for all SMM Drivers loaded\r
   // by the SMM Core\r
   //\r
   mSmmCorePrivateData.SmmIplImageHandle = ImageHandle;\r
@@ -1496,7 +1591,7 @@ SmmIplEntry (
   // Print debug message that the SMRAM window is now open.\r
   //\r
   DEBUG ((DEBUG_INFO, "SMM IPL opened SMRAM window\n"));\r
-  \r
+\r
   //\r
   // Find the largest SMRAM range between 1MB and 4GB that is at least 256KB - 4K in size\r
   //\r
@@ -1510,7 +1605,7 @@ SmmIplEntry (
     }\r
 \r
     if (gSmmCorePrivate->SmramRanges[Index].CpuStart >= BASE_1MB) {\r
-      if ((gSmmCorePrivate->SmramRanges[Index].CpuStart + gSmmCorePrivate->SmramRanges[Index].PhysicalSize) <= BASE_4GB) {\r
+      if ((gSmmCorePrivate->SmramRanges[Index].CpuStart + gSmmCorePrivate->SmramRanges[Index].PhysicalSize - 1) <= MAX_ADDRESS) {\r
         if (gSmmCorePrivate->SmramRanges[Index].PhysicalSize >= MaxSize) {\r
           MaxSize = gSmmCorePrivate->SmramRanges[Index].PhysicalSize;\r
           mCurrentSmramRange = &gSmmCorePrivate->SmramRanges[Index];\r
@@ -1523,28 +1618,55 @@ SmmIplEntry (
     //\r
     // Print debug message showing SMRAM window that will be used by SMM IPL and SMM Core\r
     //\r
-    DEBUG ((DEBUG_INFO, "SMM IPL found SMRAM window %p - %p\n", \r
-      (VOID *)(UINTN)mCurrentSmramRange->CpuStart, \r
+    DEBUG ((DEBUG_INFO, "SMM IPL found SMRAM window %p - %p\n",\r
+      (VOID *)(UINTN)mCurrentSmramRange->CpuStart,\r
       (VOID *)(UINTN)(mCurrentSmramRange->CpuStart + mCurrentSmramRange->PhysicalSize - 1)\r
       ));\r
 \r
     GetSmramCacheRange (mCurrentSmramRange, &mSmramCacheBase, &mSmramCacheSize);\r
     //\r
-    // If CPU AP is present, attempt to set SMRAM cacheability to WB\r
+    // Make sure we can change the desired memory attributes.\r
+    //\r
+    Status = gDS->GetMemorySpaceDescriptor (\r
+                    mSmramCacheBase,\r
+                    &MemDesc\r
+                    );\r
+    ASSERT_EFI_ERROR (Status);\r
+    if ((MemDesc.Capabilities & SMRAM_CAPABILITIES) != SMRAM_CAPABILITIES) {\r
+      gDS->SetMemorySpaceCapabilities (\r
+             mSmramCacheBase,\r
+             mSmramCacheSize,\r
+             MemDesc.Capabilities | SMRAM_CAPABILITIES\r
+             );\r
+    }\r
+    //\r
+    // If CPU AP is present, attempt to set SMRAM cacheability to WB and clear\r
+    // all paging attributes.\r
     // Note that it is expected that cacheability of SMRAM has been set to WB if CPU AP\r
     // is not available here.\r
     //\r
     CpuArch = NULL;\r
     Status = gBS->LocateProtocol (&gEfiCpuArchProtocolGuid, NULL, (VOID **)&CpuArch);\r
     if (!EFI_ERROR (Status)) {\r
-      Status = gDS->SetMemorySpaceAttributes(\r
-                      mSmramCacheBase, \r
+      MemDesc.Attributes &= ~(MEMORY_CACHE_ATTRIBUTES | MEMORY_PAGE_ATTRIBUTES);\r
+      MemDesc.Attributes |= EFI_MEMORY_WB;\r
+      Status = gDS->SetMemorySpaceAttributes (\r
+                      mSmramCacheBase,\r
                       mSmramCacheSize,\r
-                      EFI_MEMORY_WB\r
+                      MemDesc.Attributes\r
                       );\r
       if (EFI_ERROR (Status)) {\r
         DEBUG ((DEBUG_WARN, "SMM IPL failed to set SMRAM window to EFI_MEMORY_WB\n"));\r
-      }  \r
+      }\r
+\r
+      DEBUG_CODE (\r
+        gDS->GetMemorySpaceDescriptor (\r
+               mSmramCacheBase,\r
+               &MemDesc\r
+               );\r
+        DEBUG ((DEBUG_INFO, "SMRAM attributes: %016lx\n", MemDesc.Attributes));\r
+        ASSERT ((MemDesc.Attributes & MEMORY_PAGE_ATTRIBUTES) == 0);\r
+      );\r
     }\r
     //\r
     // if Loading module at Fixed Address feature is enabled, save the SMRAM base to Load\r
@@ -1564,15 +1686,28 @@ SmmIplEntry (
       //\r
       Status = EfiGetSystemConfigurationTable (\r
                 &gLoadFixedAddressConfigurationTableGuid,\r
-               (VOID **) &LMFAConfigurationTable\r
+               (VOID **) &mLMFAConfigurationTable\r
                );\r
-      if (!EFI_ERROR (Status) && LMFAConfigurationTable != NULL) {\r
-        LMFAConfigurationTable->SmramBase = mCurrentSmramRange->CpuStart;\r
+      if (!EFI_ERROR (Status) && mLMFAConfigurationTable != NULL) {\r
+        mLMFAConfigurationTable->SmramBase = mCurrentSmramRange->CpuStart;\r
         //\r
         // Print the SMRAM base\r
         //\r
-        DEBUG ((EFI_D_INFO, "LOADING MODULE FIXED INFO: TSEG BASE is %x. \n", LMFAConfigurationTable->SmramBase));\r
+        DEBUG ((EFI_D_INFO, "LOADING MODULE FIXED INFO: TSEG BASE is %x. \n", mLMFAConfigurationTable->SmramBase));\r
       }\r
+\r
+      //\r
+      // Fill the Smram range for all SMM code\r
+      //\r
+      SmramRangeSmmDriver = &gSmmCorePrivate->SmramRanges[gSmmCorePrivate->SmramRangeCount - 2];\r
+      SmramRangeSmmDriver->CpuStart      = mCurrentSmramRange->CpuStart;\r
+      SmramRangeSmmDriver->PhysicalStart = mCurrentSmramRange->PhysicalStart;\r
+      SmramRangeSmmDriver->RegionState   = mCurrentSmramRange->RegionState | EFI_ALLOCATED;\r
+      SmramRangeSmmDriver->PhysicalSize  = SmmCodeSize;\r
+\r
+      mCurrentSmramRange->PhysicalSize  -= SmmCodeSize;\r
+      mCurrentSmramRange->CpuStart       = mCurrentSmramRange->CpuStart + SmmCodeSize;\r
+      mCurrentSmramRange->PhysicalStart  = mCurrentSmramRange->PhysicalStart + SmmCodeSize;\r
     }\r
     //\r
     // Load SMM Core into SMRAM and execute it from SMRAM\r
@@ -1593,13 +1728,13 @@ SmmIplEntry (
       //\r
       if (CpuArch != NULL) {\r
         SetAttrStatus = gDS->SetMemorySpaceAttributes(\r
-                               mSmramCacheBase, \r
+                               mSmramCacheBase,\r
                                mSmramCacheSize,\r
                                EFI_MEMORY_UC\r
                                );\r
         if (EFI_ERROR (SetAttrStatus)) {\r
           DEBUG ((DEBUG_WARN, "SMM IPL failed to reset SMRAM window to EFI_MEMORY_UC\n"));\r
-        }  \r
+        }\r
       }\r
     }\r
   } else {\r
@@ -1610,7 +1745,7 @@ SmmIplEntry (
   }\r
 \r
   //\r
-  // If the SMM Core could not be loaded then close SMRAM window, free allocated \r
+  // If the SMM Core could not be loaded then close SMRAM window, free allocated\r
   // resources, and return an error so SMM IPL will be unloaded.\r
   //\r
   if (mCurrentSmramRange == NULL || EFI_ERROR (Status)) {\r
@@ -1632,7 +1767,7 @@ SmmIplEntry (
 \r
     return EFI_UNSUPPORTED;\r
   }\r
-  \r
+\r
   //\r
   // Install SMM Base2 Protocol and SMM Communication Protocol\r
   //\r