]> git.proxmox.com Git - mirror_edk2.git/blobdiff - UefiCpuPkg/PiSmmCpuDxeSmm/CpuS3.c
UefiCpuPkg/PiSmmCpuDxeSmm: Put AP into safe hlt-loop code on S3 path
[mirror_edk2.git] / UefiCpuPkg / PiSmmCpuDxeSmm / CpuS3.c
index 4c995ec27068e92d7a4ac19335fe6c929641ba44..ad7dc412fb40af19881d58485fedf5f481888028 100644 (file)
@@ -65,6 +65,25 @@ MP_MSR_LOCK                  *mMsrSpinLocks = NULL;
 UINTN                        mMsrSpinLockCount;\r
 UINTN                        mMsrCount = 0;\r
 \r
+//\r
+// S3 boot flag\r
+//\r
+BOOLEAN                      mSmmS3Flag = FALSE;\r
+\r
+//\r
+// Pointer to structure used during S3 Resume\r
+//\r
+SMM_S3_RESUME_STATE          *mSmmS3ResumeState = NULL;\r
+\r
+BOOLEAN                      mAcpiS3Enable = TRUE;\r
+\r
+UINT8                        *mApHltLoopCode = NULL;\r
+UINT8                        mApHltLoopCodeTemplate[] = {\r
+                               0xFA,        // cli\r
+                               0xF4,        // hlt\r
+                               0xEB, 0xFC   // jmp $-2\r
+                               };\r
+\r
 /**\r
   Get MSR spin lock by MSR index.\r
 \r
@@ -364,6 +383,8 @@ MPRendezvousProcedure (
   CPU_REGISTER_TABLE         *RegisterTableList;\r
   UINT32                     InitApicId;\r
   UINTN                      Index;\r
+  UINT32                     TopOfStack;\r
+  UINT8                      Stack[128];\r
 \r
   ProgramVirtualWireMode ();\r
   DisableLvtInterrupts ();\r
@@ -381,6 +402,14 @@ MPRendezvousProcedure (
   // Count down the number with lock mechanism.\r
   //\r
   InterlockedDecrement (&mNumberToFinish);\r
+\r
+  //\r
+  // Place AP into the safe code\r
+  //\r
+  TopOfStack  = (UINT32) (UINTN) Stack + sizeof (Stack);\r
+  TopOfStack &= ~(UINT32) (CPU_STACK_ALIGNMENT - 1);\r
+  CopyMem ((VOID *) (UINTN) mApHltLoopCode, mApHltLoopCodeTemplate, sizeof (mApHltLoopCodeTemplate));\r
+  TransferApToSafeState ((UINT32) (UINTN) mApHltLoopCode, TopOfStack);\r
 }\r
 \r
 /**\r
@@ -528,3 +557,388 @@ InitializeCpu (
     CpuPause ();\r
   }\r
 }\r
+\r
+/**\r
+  Restore SMM Configuration in S3 boot path.\r
+\r
+**/\r
+VOID\r
+RestoreSmmConfigurationInS3 (\r
+  VOID\r
+  )\r
+{\r
+  if (!mAcpiS3Enable) {\r
+    return;\r
+  }\r
+\r
+  //\r
+  // Restore SMM Configuration in S3 boot path.\r
+  //\r
+  if (mRestoreSmmConfigurationInS3) {\r
+    //\r
+    // Need make sure gSmst is correct because below function may use them.\r
+    //\r
+    gSmst->SmmStartupThisAp      = gSmmCpuPrivate->SmmCoreEntryContext.SmmStartupThisAp;\r
+    gSmst->CurrentlyExecutingCpu = gSmmCpuPrivate->SmmCoreEntryContext.CurrentlyExecutingCpu;\r
+    gSmst->NumberOfCpus          = gSmmCpuPrivate->SmmCoreEntryContext.NumberOfCpus;\r
+    gSmst->CpuSaveStateSize      = gSmmCpuPrivate->SmmCoreEntryContext.CpuSaveStateSize;\r
+    gSmst->CpuSaveState          = gSmmCpuPrivate->SmmCoreEntryContext.CpuSaveState;\r
+\r
+    //\r
+    // Configure SMM Code Access Check feature if available.\r
+    //\r
+    ConfigSmmCodeAccessCheck ();\r
+\r
+    SmmCpuFeaturesCompleteSmmReadyToLock ();\r
+\r
+    mRestoreSmmConfigurationInS3 = FALSE;\r
+  }\r
+}\r
+\r
+/**\r
+  Perform SMM initialization for all processors in the S3 boot path.\r
+\r
+  For a native platform, MP initialization in the S3 boot path is also performed in this function.\r
+**/\r
+VOID\r
+EFIAPI\r
+SmmRestoreCpu (\r
+  VOID\r
+  )\r
+{\r
+  SMM_S3_RESUME_STATE           *SmmS3ResumeState;\r
+  IA32_DESCRIPTOR               Ia32Idtr;\r
+  IA32_DESCRIPTOR               X64Idtr;\r
+  IA32_IDT_GATE_DESCRIPTOR      IdtEntryTable[EXCEPTION_VECTOR_NUMBER];\r
+  EFI_STATUS                    Status;\r
+\r
+  DEBUG ((EFI_D_INFO, "SmmRestoreCpu()\n"));\r
+\r
+  mSmmS3Flag = TRUE;\r
+\r
+  InitializeSpinLock (mMemoryMappedLock);\r
+\r
+  //\r
+  // See if there is enough context to resume PEI Phase\r
+  //\r
+  if (mSmmS3ResumeState == NULL) {\r
+    DEBUG ((EFI_D_ERROR, "No context to return to PEI Phase\n"));\r
+    CpuDeadLoop ();\r
+  }\r
+\r
+  SmmS3ResumeState = mSmmS3ResumeState;\r
+  ASSERT (SmmS3ResumeState != NULL);\r
+\r
+  if (SmmS3ResumeState->Signature == SMM_S3_RESUME_SMM_64) {\r
+    //\r
+    // Save the IA32 IDT Descriptor\r
+    //\r
+    AsmReadIdtr ((IA32_DESCRIPTOR *) &Ia32Idtr);\r
+\r
+    //\r
+    // Setup X64 IDT table\r
+    //\r
+    ZeroMem (IdtEntryTable, sizeof (IA32_IDT_GATE_DESCRIPTOR) * 32);\r
+    X64Idtr.Base = (UINTN) IdtEntryTable;\r
+    X64Idtr.Limit = (UINT16) (sizeof (IA32_IDT_GATE_DESCRIPTOR) * 32 - 1);\r
+    AsmWriteIdtr ((IA32_DESCRIPTOR *) &X64Idtr);\r
+\r
+    //\r
+    // Setup the default exception handler\r
+    //\r
+    Status = InitializeCpuExceptionHandlers (NULL);\r
+    ASSERT_EFI_ERROR (Status);\r
+\r
+    //\r
+    // Initialize Debug Agent to support source level debug\r
+    //\r
+    InitializeDebugAgent (DEBUG_AGENT_INIT_THUNK_PEI_IA32TOX64, (VOID *)&Ia32Idtr, NULL);\r
+  }\r
+\r
+  //\r
+  // Skip initialization if mAcpiCpuData is not valid\r
+  //\r
+  if (mAcpiCpuData.NumberOfCpus > 0) {\r
+    //\r
+    // First time microcode load and restore MTRRs\r
+    //\r
+    EarlyInitializeCpu ();\r
+  }\r
+\r
+  //\r
+  // Restore SMBASE for BSP and all APs\r
+  //\r
+  SmmRelocateBases ();\r
+\r
+  //\r
+  // Skip initialization if mAcpiCpuData is not valid\r
+  //\r
+  if (mAcpiCpuData.NumberOfCpus > 0) {\r
+    //\r
+    // Restore MSRs for BSP and all APs\r
+    //\r
+    InitializeCpu ();\r
+  }\r
+\r
+  //\r
+  // Set a flag to restore SMM configuration in S3 path.\r
+  //\r
+  mRestoreSmmConfigurationInS3 = TRUE;\r
+\r
+  DEBUG (( EFI_D_INFO, "SMM S3 Return CS                = %x\n", SmmS3ResumeState->ReturnCs));\r
+  DEBUG (( EFI_D_INFO, "SMM S3 Return Entry Point       = %x\n", SmmS3ResumeState->ReturnEntryPoint));\r
+  DEBUG (( EFI_D_INFO, "SMM S3 Return Context1          = %x\n", SmmS3ResumeState->ReturnContext1));\r
+  DEBUG (( EFI_D_INFO, "SMM S3 Return Context2          = %x\n", SmmS3ResumeState->ReturnContext2));\r
+  DEBUG (( EFI_D_INFO, "SMM S3 Return Stack Pointer     = %x\n", SmmS3ResumeState->ReturnStackPointer));\r
+\r
+  //\r
+  // If SMM is in 32-bit mode, then use SwitchStack() to resume PEI Phase\r
+  //\r
+  if (SmmS3ResumeState->Signature == SMM_S3_RESUME_SMM_32) {\r
+    DEBUG ((EFI_D_INFO, "Call SwitchStack() to return to S3 Resume in PEI Phase\n"));\r
+\r
+    SwitchStack (\r
+      (SWITCH_STACK_ENTRY_POINT)(UINTN)SmmS3ResumeState->ReturnEntryPoint,\r
+      (VOID *)(UINTN)SmmS3ResumeState->ReturnContext1,\r
+      (VOID *)(UINTN)SmmS3ResumeState->ReturnContext2,\r
+      (VOID *)(UINTN)SmmS3ResumeState->ReturnStackPointer\r
+      );\r
+  }\r
+\r
+  //\r
+  // If SMM is in 64-bit mode, then use AsmDisablePaging64() to resume PEI Phase\r
+  //\r
+  if (SmmS3ResumeState->Signature == SMM_S3_RESUME_SMM_64) {\r
+    DEBUG ((EFI_D_INFO, "Call AsmDisablePaging64() to return to S3 Resume in PEI Phase\n"));\r
+    //\r
+    // Disable interrupt of Debug timer, since new IDT table is for IA32 and will not work in long mode.\r
+    //\r
+    SaveAndSetDebugTimerInterrupt (FALSE);\r
+    //\r
+    // Restore IA32 IDT table\r
+    //\r
+    AsmWriteIdtr ((IA32_DESCRIPTOR *) &Ia32Idtr);\r
+    AsmDisablePaging64 (\r
+      SmmS3ResumeState->ReturnCs,\r
+      (UINT32)SmmS3ResumeState->ReturnEntryPoint,\r
+      (UINT32)SmmS3ResumeState->ReturnContext1,\r
+      (UINT32)SmmS3ResumeState->ReturnContext2,\r
+      (UINT32)SmmS3ResumeState->ReturnStackPointer\r
+      );\r
+  }\r
+\r
+  //\r
+  // Can not resume PEI Phase\r
+  //\r
+  DEBUG ((EFI_D_ERROR, "No context to return to PEI Phase\n"));\r
+  CpuDeadLoop ();\r
+}\r
+\r
+/**\r
+  Initialize SMM S3 resume state structure used during S3 Resume.\r
+\r
+  @param[in] Cr3    The base address of the page tables to use in SMM.\r
+\r
+**/\r
+VOID\r
+InitSmmS3ResumeState (\r
+  IN UINT32  Cr3\r
+  )\r
+{\r
+  VOID                       *GuidHob;\r
+  EFI_SMRAM_DESCRIPTOR       *SmramDescriptor;\r
+  SMM_S3_RESUME_STATE        *SmmS3ResumeState;\r
+  EFI_PHYSICAL_ADDRESS       Address;\r
+  EFI_STATUS                 Status;\r
+\r
+  if (!mAcpiS3Enable) {\r
+    return;\r
+  }\r
+\r
+  GuidHob = GetFirstGuidHob (&gEfiAcpiVariableGuid);\r
+  if (GuidHob != NULL) {\r
+    SmramDescriptor = (EFI_SMRAM_DESCRIPTOR *) GET_GUID_HOB_DATA (GuidHob);\r
+\r
+    DEBUG ((EFI_D_INFO, "SMM S3 SMRAM Structure = %x\n", SmramDescriptor));\r
+    DEBUG ((EFI_D_INFO, "SMM S3 Structure = %x\n", SmramDescriptor->CpuStart));\r
+\r
+    SmmS3ResumeState = (SMM_S3_RESUME_STATE *)(UINTN)SmramDescriptor->CpuStart;\r
+    ZeroMem (SmmS3ResumeState, sizeof (SMM_S3_RESUME_STATE));\r
+\r
+    mSmmS3ResumeState = SmmS3ResumeState;\r
+    SmmS3ResumeState->Smst = (EFI_PHYSICAL_ADDRESS)(UINTN)gSmst;\r
+\r
+    SmmS3ResumeState->SmmS3ResumeEntryPoint = (EFI_PHYSICAL_ADDRESS)(UINTN)SmmRestoreCpu;\r
+\r
+    SmmS3ResumeState->SmmS3StackSize = SIZE_32KB;\r
+    SmmS3ResumeState->SmmS3StackBase = (EFI_PHYSICAL_ADDRESS)(UINTN)AllocatePages (EFI_SIZE_TO_PAGES ((UINTN)SmmS3ResumeState->SmmS3StackSize));\r
+    if (SmmS3ResumeState->SmmS3StackBase == 0) {\r
+      SmmS3ResumeState->SmmS3StackSize = 0;\r
+    }\r
+\r
+    SmmS3ResumeState->SmmS3Cr0 = gSmmCr0;\r
+    SmmS3ResumeState->SmmS3Cr3 = Cr3;\r
+    SmmS3ResumeState->SmmS3Cr4 = gSmmCr4;\r
+\r
+    if (sizeof (UINTN) == sizeof (UINT64)) {\r
+      SmmS3ResumeState->Signature = SMM_S3_RESUME_SMM_64;\r
+    }\r
+    if (sizeof (UINTN) == sizeof (UINT32)) {\r
+      SmmS3ResumeState->Signature = SMM_S3_RESUME_SMM_32;\r
+    }\r
+  }\r
+\r
+  //\r
+  // Patch SmmS3ResumeState->SmmS3Cr3\r
+  //\r
+  InitSmmS3Cr3 ();\r
+\r
+  //\r
+  // Allocate safe memory in ACPI NVS for AP to execute hlt loop in\r
+  // protected mode on S3 path\r
+  //\r
+  Address = BASE_4GB - 1;\r
+  Status  = gBS->AllocatePages (\r
+                   AllocateMaxAddress,\r
+                   EfiACPIMemoryNVS,\r
+                   EFI_SIZE_TO_PAGES (sizeof (mApHltLoopCodeTemplate)),\r
+                   &Address\r
+                   );\r
+  ASSERT_EFI_ERROR (Status);\r
+  mApHltLoopCode = (UINT8 *) (UINTN) Address;\r
+}\r
+\r
+/**\r
+  Copy register table from ACPI NVS memory into SMRAM.\r
+\r
+  @param[in] DestinationRegisterTableList  Points to destination register table.\r
+  @param[in] SourceRegisterTableList       Points to source register table.\r
+  @param[in] NumberOfCpus                  Number of CPUs.\r
+\r
+**/\r
+VOID\r
+CopyRegisterTable (\r
+  IN CPU_REGISTER_TABLE         *DestinationRegisterTableList,\r
+  IN CPU_REGISTER_TABLE         *SourceRegisterTableList,\r
+  IN UINT32                     NumberOfCpus\r
+  )\r
+{\r
+  UINTN                      Index;\r
+  UINTN                      Index1;\r
+  CPU_REGISTER_TABLE_ENTRY   *RegisterTableEntry;\r
+\r
+  CopyMem (DestinationRegisterTableList, SourceRegisterTableList, NumberOfCpus * sizeof (CPU_REGISTER_TABLE));\r
+  for (Index = 0; Index < NumberOfCpus; Index++) {\r
+    DestinationRegisterTableList[Index].RegisterTableEntry = AllocatePool (DestinationRegisterTableList[Index].AllocatedSize);\r
+    ASSERT (DestinationRegisterTableList[Index].RegisterTableEntry != NULL);\r
+    CopyMem (DestinationRegisterTableList[Index].RegisterTableEntry, SourceRegisterTableList[Index].RegisterTableEntry, DestinationRegisterTableList[Index].AllocatedSize);\r
+    //\r
+    // Go though all MSRs in register table to initialize MSR spin lock\r
+    //\r
+    RegisterTableEntry = DestinationRegisterTableList[Index].RegisterTableEntry;\r
+    for (Index1 = 0; Index1 < DestinationRegisterTableList[Index].TableLength; Index1++, RegisterTableEntry++) {\r
+      if ((RegisterTableEntry->RegisterType == Msr) && (RegisterTableEntry->ValidBitLength < 64)) {\r
+        //\r
+        // Initialize MSR spin lock only for those MSRs need bit field writing\r
+        //\r
+        InitMsrSpinLockByIndex (RegisterTableEntry->Index);\r
+      }\r
+    }\r
+  }\r
+}\r
+\r
+/**\r
+  Get ACPI CPU data.\r
+\r
+**/\r
+VOID\r
+GetAcpiCpuData (\r
+  VOID\r
+  )\r
+{\r
+  ACPI_CPU_DATA              *AcpiCpuData;\r
+  IA32_DESCRIPTOR            *Gdtr;\r
+  IA32_DESCRIPTOR            *Idtr;\r
+\r
+  if (!mAcpiS3Enable) {\r
+    return;\r
+  }\r
+\r
+  //\r
+  // Prevent use of mAcpiCpuData by initialize NumberOfCpus to 0\r
+  //\r
+  mAcpiCpuData.NumberOfCpus = 0;\r
+\r
+  //\r
+  // If PcdCpuS3DataAddress was never set, then do not copy CPU S3 Data into SMRAM\r
+  //\r
+  AcpiCpuData = (ACPI_CPU_DATA *)(UINTN)PcdGet64 (PcdCpuS3DataAddress);\r
+  if (AcpiCpuData == 0) {\r
+    return;\r
+  }\r
+\r
+  //\r
+  // For a native platform, copy the CPU S3 data into SMRAM for use on CPU S3 Resume.\r
+  //\r
+  CopyMem (&mAcpiCpuData, AcpiCpuData, sizeof (mAcpiCpuData));\r
+\r
+  mAcpiCpuData.MtrrTable = (EFI_PHYSICAL_ADDRESS)(UINTN)AllocatePool (sizeof (MTRR_SETTINGS));\r
+  ASSERT (mAcpiCpuData.MtrrTable != 0);\r
+\r
+  CopyMem ((VOID *)(UINTN)mAcpiCpuData.MtrrTable, (VOID *)(UINTN)AcpiCpuData->MtrrTable, sizeof (MTRR_SETTINGS));\r
+\r
+  mAcpiCpuData.GdtrProfile = (EFI_PHYSICAL_ADDRESS)(UINTN)AllocatePool (sizeof (IA32_DESCRIPTOR));\r
+  ASSERT (mAcpiCpuData.GdtrProfile != 0);\r
+\r
+  CopyMem ((VOID *)(UINTN)mAcpiCpuData.GdtrProfile, (VOID *)(UINTN)AcpiCpuData->GdtrProfile, sizeof (IA32_DESCRIPTOR));\r
+\r
+  mAcpiCpuData.IdtrProfile = (EFI_PHYSICAL_ADDRESS)(UINTN)AllocatePool (sizeof (IA32_DESCRIPTOR));\r
+  ASSERT (mAcpiCpuData.IdtrProfile != 0);\r
+\r
+  CopyMem ((VOID *)(UINTN)mAcpiCpuData.IdtrProfile, (VOID *)(UINTN)AcpiCpuData->IdtrProfile, sizeof (IA32_DESCRIPTOR));\r
+\r
+  mAcpiCpuData.PreSmmInitRegisterTable = (EFI_PHYSICAL_ADDRESS)(UINTN)AllocatePool (mAcpiCpuData.NumberOfCpus * sizeof (CPU_REGISTER_TABLE));\r
+  ASSERT (mAcpiCpuData.PreSmmInitRegisterTable != 0);\r
+\r
+  CopyRegisterTable (\r
+    (CPU_REGISTER_TABLE *)(UINTN)mAcpiCpuData.PreSmmInitRegisterTable,\r
+    (CPU_REGISTER_TABLE *)(UINTN)AcpiCpuData->PreSmmInitRegisterTable,\r
+    mAcpiCpuData.NumberOfCpus\r
+    );\r
+\r
+  mAcpiCpuData.RegisterTable = (EFI_PHYSICAL_ADDRESS)(UINTN)AllocatePool (mAcpiCpuData.NumberOfCpus * sizeof (CPU_REGISTER_TABLE));\r
+  ASSERT (mAcpiCpuData.RegisterTable != 0);\r
+\r
+  CopyRegisterTable (\r
+    (CPU_REGISTER_TABLE *)(UINTN)mAcpiCpuData.RegisterTable,\r
+    (CPU_REGISTER_TABLE *)(UINTN)AcpiCpuData->RegisterTable,\r
+    mAcpiCpuData.NumberOfCpus\r
+    );\r
+\r
+  //\r
+  // Copy AP's GDT, IDT and Machine Check handler into SMRAM.\r
+  //\r
+  Gdtr = (IA32_DESCRIPTOR *)(UINTN)mAcpiCpuData.GdtrProfile;\r
+  Idtr = (IA32_DESCRIPTOR *)(UINTN)mAcpiCpuData.IdtrProfile;\r
+\r
+  mGdtForAp = AllocatePool ((Gdtr->Limit + 1) + (Idtr->Limit + 1) +  mAcpiCpuData.ApMachineCheckHandlerSize);\r
+  ASSERT (mGdtForAp != NULL);\r
+  mIdtForAp = (VOID *) ((UINTN)mGdtForAp + (Gdtr->Limit + 1));\r
+  mMachineCheckHandlerForAp = (VOID *) ((UINTN)mIdtForAp + (Idtr->Limit + 1));\r
+\r
+  CopyMem (mGdtForAp, (VOID *)Gdtr->Base, Gdtr->Limit + 1);\r
+  CopyMem (mIdtForAp, (VOID *)Idtr->Base, Idtr->Limit + 1);\r
+  CopyMem (mMachineCheckHandlerForAp, (VOID *)(UINTN)mAcpiCpuData.ApMachineCheckHandlerBase, mAcpiCpuData.ApMachineCheckHandlerSize);\r
+}\r
+\r
+/**\r
+  Get ACPI S3 enable flag.\r
+\r
+**/\r
+VOID\r
+GetAcpiS3EnableFlag (\r
+  VOID\r
+  )\r
+{\r
+  mAcpiS3Enable = PcdGetBool (PcdAcpiS3Enable);\r
+}\r