]> git.proxmox.com Git - mirror_edk2.git/commitdiff
UefiCpuPkg/PiSmmCpuDxeSmm: Consume SMM Base Hob for SmBase info
authorWu, Jiaxin <jiaxin.wu@intel.com>
Thu, 16 Feb 2023 06:16:31 +0000 (14:16 +0800)
committermergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
Mon, 6 Mar 2023 06:07:48 +0000 (06:07 +0000)
REF: https://bugzilla.tianocore.org/show_bug.cgi?id=4337

Existing SMBASE Relocation is in the PiSmmCpuDxeSmm driver, which
will relocate the SMBASE of each processor by setting the SMBASE
field in the saved state map (at offset 7EF8h) to a new value.
The RSM instruction reloads the internal SMBASE register with the
value in SMBASE field when each time it exits SMM. All subsequent
SMI requests will use the new SMBASE to find the starting address
for the SMI handler (at SMBASE + 8000h).

Due to the default SMBASE for all x86 processors is 0x30000, the
APs' 1st SMI for rebase has to be executed one by one to avoid
the processors over-writing each other's SMM Save State Area (see
existing SmmRelocateBases() function), which means the next AP has
to wait for the previous AP to finish its 1st SMI, then it can call
into its 1st SMI for rebase via Smi Ipi command, thus leading the
existing SMBASE Relocation has to be running in series. Besides, it
needs very complex code to handle the AP exit semaphore
(mRebased[Index]), which will hook return address of SMM Save State
so that semaphore code can be executed immediately after AP exits
SMM for SMBASE relocation (see existing SemaphoreHook() function).

With SMM Base Hob support, PiSmmCpuDxeSmm does not need the RSM
instruction to do the SMBASE Relocation. SMBASE Register for each
processors have already been programmed and all SMBASE address have
recorded in SMM Base Hob. So the same default SMBASE Address
(0x30000) will not be used, thus the processors over-writing each
other's SMM Save State Area will not happen in PiSmmCpuDxeSmm driver.
This way makes the first SMI init can be executed in parallel and
save boot time on multi-core system. Besides, Semaphore Hook code
logic is also not required, which will greatly simplify the SMBASE
Relocation flow.

Mainly changes as below:
* Assume the biggest possibility of tile size is 8k.
* Combine 2 SMIs (gcSmmInitTemplate & gcSmiHandlerTemplate) into one
(gcSmiHandlerTemplate), the new SMI handler needs to run to 2 paths:
one to SmmCpuFeaturesInitializeProcessor(), the other to SMM Core
Entry Point.
* Issue SMI IPI (All Excluding Self SMM IPI + BSP SMM IPI) for first
SMI init before normal SMI sources happen.
* Call SmmCpuFeaturesInitializeProcessor() in parallel.

Cc: Eric Dong <eric.dong@intel.com>
Cc: Ray Ni <ray.ni@intel.com>
Cc: Zeng Star <star.zeng@intel.com>
Cc: Laszlo Ersek <lersek@redhat.com>
Acked-by: Gerd Hoffmann <kraxel@redhat.com>
Cc: Rahul Kumar <rahul1.kumar@intel.com>
Signed-off-by: Jiaxin Wu <jiaxin.wu@intel.com>
Reviewed-by: Ray Ni <ray.ni@intel.com>
UefiCpuPkg/PiSmmCpuDxeSmm/CpuS3.c
UefiCpuPkg/PiSmmCpuDxeSmm/MpService.c
UefiCpuPkg/PiSmmCpuDxeSmm/PiSmmCpuDxeSmm.c
UefiCpuPkg/PiSmmCpuDxeSmm/PiSmmCpuDxeSmm.h
UefiCpuPkg/PiSmmCpuDxeSmm/PiSmmCpuDxeSmm.inf

index fb4a44eab69b585de666252573b34863e40325e8..d408b3f9f7a9c589a0cd97f048e02642097bd5c6 100644 (file)
@@ -1,7 +1,7 @@
 /** @file\r
 Code for Processor S3 restoration\r
 \r
-Copyright (c) 2006 - 2022, Intel Corporation. All rights reserved.<BR>\r
+Copyright (c) 2006 - 2023, Intel Corporation. All rights reserved.<BR>\r
 SPDX-License-Identifier: BSD-2-Clause-Patent\r
 \r
 **/\r
@@ -824,9 +824,34 @@ SmmRestoreCpu (
   }\r
 \r
   //\r
-  // Restore SMBASE for BSP and all APs\r
+  // Make sure the gSmmBaseHobGuid existence status is the same between normal and S3 boot.\r
   //\r
-  SmmRelocateBases ();\r
+  ASSERT (mSmmRelocated == (BOOLEAN)(GetFirstGuidHob (&gSmmBaseHobGuid) != NULL));\r
+  if (mSmmRelocated != (BOOLEAN)(GetFirstGuidHob (&gSmmBaseHobGuid) != NULL)) {\r
+    DEBUG ((\r
+      DEBUG_ERROR,\r
+      "gSmmBaseHobGuid %a produced in normal boot but %a in S3 boot!",\r
+      mSmmRelocated ? "is" : "is not",\r
+      mSmmRelocated ? "is not" : "is"\r
+      ));\r
+    CpuDeadLoop ();\r
+  }\r
+\r
+  //\r
+  // Check whether Smm Relocation is done or not.\r
+  // If not, will do the SmmBases Relocation here!!!\r
+  //\r
+  if (!mSmmRelocated) {\r
+    //\r
+    // Restore SMBASE for BSP and all APs\r
+    //\r
+    SmmRelocateBases ();\r
+  } else {\r
+    //\r
+    // Issue SMI IPI (All Excluding  Self SMM IPI + BSP SMM IPI) to execute first SMI init.\r
+    //\r
+    ExecuteFirstSmiInit ();\r
+  }\r
 \r
   //\r
   // Skip initialization if mAcpiCpuData is not valid\r
index a0967eb69c71de1c97d714e38c47b9f7ecb20106..baf827cf9dc088188d0b229dd8e1bb1cd2be2e35 100644 (file)
@@ -1,7 +1,7 @@
 /** @file\r
 SMM MP service implementation\r
 \r
-Copyright (c) 2009 - 2022, Intel Corporation. All rights reserved.<BR>\r
+Copyright (c) 2009 - 2023, Intel Corporation. All rights reserved.<BR>\r
 Copyright (c) 2017, AMD Incorporated. All rights reserved.<BR>\r
 \r
 SPDX-License-Identifier: BSD-2-Clause-Patent\r
@@ -1723,6 +1723,10 @@ SmiRendezvous (
 \r
   ASSERT (CpuIndex < mMaxNumberOfCpus);\r
 \r
+  if (mSmmRelocated) {\r
+    ASSERT (mSmmInitialized != NULL);\r
+  }\r
+\r
   //\r
   // Save Cr2 because Page Fault exception in SMM may override its value,\r
   // when using on-demand paging for above 4G memory.\r
@@ -1730,6 +1734,25 @@ SmiRendezvous (
   Cr2 = 0;\r
   SaveCr2 (&Cr2);\r
 \r
+  if (mSmmRelocated && !mSmmInitialized[CpuIndex]) {\r
+    //\r
+    // Perform SmmInitHandler for CpuIndex\r
+    //\r
+    SmmInitHandler ();\r
+\r
+    //\r
+    // Restore Cr2\r
+    //\r
+    RestoreCr2 (Cr2);\r
+\r
+    //\r
+    // Mark the first SMI init for CpuIndex has been done so as to avoid the reentry.\r
+    //\r
+    mSmmInitialized[CpuIndex] = TRUE;\r
+\r
+    return;\r
+  }\r
+\r
   //\r
   // Call the user register Startup function first.\r
   //\r
index 6e795d1756525037bdd0aa8f4afa99ffe33995f4..d2d0950f3b42cc4a94fa184e205a155d5ab5dfd3 100644 (file)
@@ -84,6 +84,8 @@ EDKII_SMM_MEMORY_ATTRIBUTE_PROTOCOL  mSmmMemoryAttribute = {
 \r
 EFI_CPU_INTERRUPT_HANDLER  mExternalVectorTable[EXCEPTION_VECTOR_NUMBER];\r
 \r
+BOOLEAN           mSmmRelocated    = FALSE;\r
+volatile BOOLEAN  *mSmmInitialized = NULL;\r
 UINT32            mBspApicId       = 0;\r
 \r
 //\r
@@ -383,10 +385,12 @@ SmmInitHandler (
         InitializeMpSyncData ();\r
       }\r
 \r
-      //\r
-      // Hook return after RSM to set SMM re-based flag\r
-      //\r
-      SemaphoreHook (Index, &mRebased[Index]);\r
+      if (!mSmmRelocated) {\r
+        //\r
+        // Hook return after RSM to set SMM re-based flag\r
+        //\r
+        SemaphoreHook (Index, &mRebased[Index]);\r
+      }\r
 \r
       return;\r
     }\r
@@ -395,6 +399,51 @@ SmmInitHandler (
   ASSERT (FALSE);\r
 }\r
 \r
+/**\r
+  Issue SMI IPI (All Excluding Self SMM IPI + BSP SMM IPI) to execute first SMI init.\r
+\r
+**/\r
+VOID\r
+ExecuteFirstSmiInit (\r
+  VOID\r
+  )\r
+{\r
+  UINTN  Index;\r
+\r
+  if (mSmmInitialized == NULL) {\r
+    mSmmInitialized = (BOOLEAN *)AllocatePool (sizeof (BOOLEAN) * mMaxNumberOfCpus);\r
+  }\r
+\r
+  ASSERT (mSmmInitialized != NULL);\r
+  if (mSmmInitialized == NULL) {\r
+    return;\r
+  }\r
+\r
+  //\r
+  // Reset the mSmmInitialized to false.\r
+  //\r
+  ZeroMem ((VOID *)mSmmInitialized, sizeof (BOOLEAN) * mMaxNumberOfCpus);\r
+\r
+  //\r
+  // Get the BSP ApicId.\r
+  //\r
+  mBspApicId = GetApicId ();\r
+\r
+  //\r
+  // Issue SMI IPI (All Excluding Self SMM IPI + BSP SMM IPI) for SMM init\r
+  //\r
+  SendSmiIpi (mBspApicId);\r
+  SendSmiIpiAllExcludingSelf ();\r
+\r
+  //\r
+  // Wait for all processors to finish its 1st SMI\r
+  //\r
+  for (Index = 0; Index < mNumberOfCpus; Index++) {\r
+    while (!(BOOLEAN)mSmmInitialized[Index]) {\r
+    }\r
+  }\r
+}\r
+\r
 /**\r
   Relocate SmmBases for each processor.\r
 \r
@@ -562,6 +611,11 @@ PiCpuSmmEntry (
   UINTN                     FamilyId;\r
   UINTN                     ModelId;\r
   UINT32                    Cr3;\r
+  EFI_HOB_GUID_TYPE         *GuidHob;\r
+  SMM_BASE_HOB_DATA         *SmmBaseHobData;\r
+\r
+  GuidHob        = NULL;\r
+  SmmBaseHobData = NULL;\r
 \r
   //\r
   // Initialize address fixup\r
@@ -790,26 +844,54 @@ PiCpuSmmEntry (
   ASSERT (TileSize <= (SMRAM_SAVE_STATE_MAP_OFFSET + sizeof (SMRAM_SAVE_STATE_MAP) - SMM_HANDLER_OFFSET));\r
 \r
   //\r
-  // Allocate buffer for all of the tiles.\r
-  //\r
-  // Intel(R) 64 and IA-32 Architectures Software Developer's Manual\r
-  // Volume 3C, Section 34.11 SMBASE Relocation\r
-  //   For Pentium and Intel486 processors, the SMBASE values must be\r
-  //   aligned on a 32-KByte boundary or the processor will enter shutdown\r
-  //   state during the execution of a RSM instruction.\r
-  //\r
-  // Intel486 processors: FamilyId is 4\r
-  // Pentium processors : FamilyId is 5\r
+  // Retrive the allocated SmmBase from gSmmBaseHobGuid. If found,\r
+  // means the SmBase relocation has been done.\r
   //\r
-  BufferPages = EFI_SIZE_TO_PAGES (SIZE_32KB + TileSize * (mMaxNumberOfCpus - 1));\r
-  if ((FamilyId == 4) || (FamilyId == 5)) {\r
-    Buffer = AllocateAlignedCodePages (BufferPages, SIZE_32KB);\r
+  GuidHob = GetFirstGuidHob (&gSmmBaseHobGuid);\r
+  if (GuidHob != NULL) {\r
+    //\r
+    // Check whether the Required TileSize is enough.\r
+    //\r
+    if (TileSize > SIZE_8KB) {\r
+      DEBUG ((DEBUG_ERROR, "The Range of Smbase in SMRAM is not enough -- Required TileSize = 0x%08x, Actual TileSize = 0x%08x\n", TileSize, SIZE_8KB));\r
+      CpuDeadLoop ();\r
+      return RETURN_BUFFER_TOO_SMALL;\r
+    }\r
+\r
+    SmmBaseHobData = GET_GUID_HOB_DATA (GuidHob);\r
+\r
+    //\r
+    // Assume single instance of HOB produced, expect the HOB.NumberOfProcessors equals to the mMaxNumberOfCpus.\r
+    //\r
+    ASSERT (SmmBaseHobData->NumberOfProcessors == (UINT32)mMaxNumberOfCpus && SmmBaseHobData->ProcessorIndex == 0);\r
+    mSmmRelocated = TRUE;\r
   } else {\r
-    Buffer = AllocateAlignedCodePages (BufferPages, SIZE_4KB);\r
-  }\r
+    //\r
+    // When the HOB doesn't exist, allocate new SMBASE itself.\r
+    //\r
+    DEBUG ((DEBUG_INFO, "PiCpuSmmEntry: gSmmBaseHobGuid not found!\n"));\r
+    //\r
+    // Allocate buffer for all of the tiles.\r
+    //\r
+    // Intel(R) 64 and IA-32 Architectures Software Developer's Manual\r
+    // Volume 3C, Section 34.11 SMBASE Relocation\r
+    //   For Pentium and Intel486 processors, the SMBASE values must be\r
+    //   aligned on a 32-KByte boundary or the processor will enter shutdown\r
+    //   state during the execution of a RSM instruction.\r
+    //\r
+    // Intel486 processors: FamilyId is 4\r
+    // Pentium processors : FamilyId is 5\r
+    //\r
+    BufferPages = EFI_SIZE_TO_PAGES (SIZE_32KB + TileSize * (mMaxNumberOfCpus - 1));\r
+    if ((FamilyId == 4) || (FamilyId == 5)) {\r
+      Buffer = AllocateAlignedCodePages (BufferPages, SIZE_32KB);\r
+    } else {\r
+      Buffer = AllocateAlignedCodePages (BufferPages, SIZE_4KB);\r
+    }\r
 \r
-  ASSERT (Buffer != NULL);\r
-  DEBUG ((DEBUG_INFO, "SMRAM SaveState Buffer (0x%08x, 0x%08x)\n", Buffer, EFI_PAGES_TO_SIZE (BufferPages)));\r
+    ASSERT (Buffer != NULL);\r
+    DEBUG ((DEBUG_INFO, "New Allcoated SMRAM SaveState Buffer (0x%08x, 0x%08x)\n", Buffer, EFI_PAGES_TO_SIZE (BufferPages)));\r
+  }\r
 \r
   //\r
   // Allocate buffer for pointers to array in  SMM_CPU_PRIVATE_DATA.\r
@@ -844,7 +926,8 @@ PiCpuSmmEntry (
   // size for each CPU in the platform\r
   //\r
   for (Index = 0; Index < mMaxNumberOfCpus; Index++) {\r
-    mCpuHotPlugData.SmBase[Index]           = (UINTN)Buffer + Index * TileSize - SMM_HANDLER_OFFSET;\r
+    mCpuHotPlugData.SmBase[Index] = mSmmRelocated ? (UINTN)SmmBaseHobData->SmBase[Index] : (UINTN)Buffer + Index * TileSize - SMM_HANDLER_OFFSET;\r
+\r
     gSmmCpuPrivate->CpuSaveStateSize[Index] = sizeof (SMRAM_SAVE_STATE_MAP);\r
     gSmmCpuPrivate->CpuSaveState[Index]     = (VOID *)(mCpuHotPlugData.SmBase[Index] + SMRAM_SAVE_STATE_MAP_OFFSET);\r
     gSmmCpuPrivate->Operation[Index]        = SmmCpuNone;\r
@@ -957,17 +1040,23 @@ PiCpuSmmEntry (
   InitializeSmmIdt ();\r
 \r
   //\r
-  // Relocate SMM Base addresses to the ones allocated from SMRAM\r
+  // Check whether Smm Relocation is done or not.\r
+  // If not, will do the SmmBases Relocation here!!!\r
   //\r
-  mRebased = (BOOLEAN *)AllocateZeroPool (sizeof (BOOLEAN) * mMaxNumberOfCpus);\r
-  ASSERT (mRebased != NULL);\r
-  SmmRelocateBases ();\r
+  if (!mSmmRelocated) {\r
+    //\r
+    // Relocate SMM Base addresses to the ones allocated from SMRAM\r
+    //\r
+    mRebased = (BOOLEAN *)AllocateZeroPool (sizeof (BOOLEAN) * mMaxNumberOfCpus);\r
+    ASSERT (mRebased != NULL);\r
+    SmmRelocateBases ();\r
 \r
-  //\r
-  // Call hook for BSP to perform extra actions in normal mode after all\r
-  // SMM base addresses have been relocated on all CPUs\r
-  //\r
-  SmmCpuFeaturesSmmRelocationComplete ();\r
+    //\r
+    // Call hook for BSP to perform extra actions in normal mode after all\r
+    // SMM base addresses have been relocated on all CPUs\r
+    //\r
+    SmmCpuFeaturesSmmRelocationComplete ();\r
+  }\r
 \r
   DEBUG ((DEBUG_INFO, "mXdSupported - 0x%x\n", mXdSupported));\r
 \r
@@ -998,6 +1087,21 @@ PiCpuSmmEntry (
     }\r
   }\r
 \r
+  //\r
+  // For relocated SMBASE, some MSRs & CSRs are still required to be configured in SMM Mode for SMM Initialization.\r
+  // Those MSRs & CSRs must be configured before normal SMI sources happen.\r
+  // So, here is to issue SMI IPI (All Excluding  Self SMM IPI + BSP SMM IPI) to execute first SMI init.\r
+  //\r
+  if (mSmmRelocated) {\r
+    ExecuteFirstSmiInit ();\r
+\r
+    //\r
+    // Call hook for BSP to perform extra actions in normal mode after all\r
+    // SMM base addresses have been relocated on all CPUs\r
+    //\r
+    SmmCpuFeaturesSmmRelocationComplete ();\r
+  }\r
+\r
   //\r
   // Fill in SMM Reserved Regions\r
   //\r
index 5f0a38e4002d33e46c7712f05633600c66e090a8..c3731f174be814e9f57c1fdb56a9fe7d7b73c6fe 100644 (file)
@@ -1,7 +1,7 @@
 /** @file\r
 Agent Module to load other modules to deploy SMM Entry Vector for X86 CPU.\r
 \r
-Copyright (c) 2009 - 2022, Intel Corporation. All rights reserved.<BR>\r
+Copyright (c) 2009 - 2023, Intel Corporation. All rights reserved.<BR>\r
 Copyright (c) 2017, AMD Incorporated. All rights reserved.<BR>\r
 \r
 SPDX-License-Identifier: BSD-2-Clause-Patent\r
@@ -25,6 +25,7 @@ SPDX-License-Identifier: BSD-2-Clause-Patent
 #include <Guid/AcpiS3Context.h>\r
 #include <Guid/MemoryAttributesTable.h>\r
 #include <Guid/PiSmmMemoryAttributesTable.h>\r
+#include <Guid/SmmBaseHob.h>\r
 \r
 #include <Library/BaseLib.h>\r
 #include <Library/IoLib.h>\r
@@ -348,6 +349,25 @@ SmmWriteSaveState (
   IN CONST VOID                   *Buffer\r
   );\r
 \r
+/**\r
+  C function for SMI handler. To change all processor's SMMBase Register.\r
+\r
+**/\r
+VOID\r
+EFIAPI\r
+SmmInitHandler (\r
+  VOID\r
+  );\r
+\r
+/**\r
+  Issue SMI IPI (All Excluding Self SMM IPI + BSP SMM IPI) to execute first SMI init.\r
+\r
+**/\r
+VOID\r
+ExecuteFirstSmiInit (\r
+  VOID\r
+  );\r
+\r
 /**\r
 Read a CPU Save State register on the target processor.\r
 \r
@@ -402,6 +422,10 @@ WriteSaveStateRegister (
   IN CONST VOID                   *Buffer\r
   );\r
 \r
+extern BOOLEAN            mSmmRelocated;\r
+extern volatile  BOOLEAN  *mSmmInitialized;\r
+extern UINT32             mBspApicId;\r
+\r
 extern CONST UINT8        gcSmmInitTemplate[];\r
 extern CONST UINT16       gcSmmInitSize;\r
 X86_ASSEMBLY_PATCH_LABEL  gPatchSmmCr0;\r
index b4b327f60c817d1dcf48ac5185652272fa331832..9bfa8c1a7607a8088d052ea7923266cfd1d96818 100644 (file)
@@ -4,7 +4,7 @@
 # This SMM driver performs SMM initialization, deploy SMM Entry Vector,\r
 # provides CPU specific services in SMM.\r
 #\r
-# Copyright (c) 2009 - 2022, Intel Corporation. All rights reserved.<BR>\r
+# Copyright (c) 2009 - 2023, Intel Corporation. All rights reserved.<BR>\r
 # Copyright (c) 2017, AMD Incorporated. All rights reserved.<BR>\r
 #\r
 # SPDX-License-Identifier: BSD-2-Clause-Patent\r
   gEfiAcpiVariableGuid                     ## SOMETIMES_CONSUMES ## HOB # it is used for S3 boot.\r
   gEdkiiPiSmmMemoryAttributesTableGuid     ## CONSUMES ## SystemTable\r
   gEfiMemoryAttributesTableGuid            ## CONSUMES ## SystemTable\r
+  gSmmBaseHobGuid                          ## CONSUMES\r
 \r
 [FeaturePcd]\r
   gUefiCpuPkgTokenSpaceGuid.PcdCpuSmmDebug                         ## CONSUMES\r