X-Git-Url: https://git.proxmox.com/?a=blobdiff_plain;f=UefiCpuPkg%2FLibrary%2FMpInitLib%2FMpLib.c;h=2d8e9512ab98e261f8b6956aee3c98f76b6e7de8;hb=053e878bfb5c9d5eca779789b62891add30b14ba;hp=d0fbc17ce5516ae0ab07bde8460fa07f185b8426;hpb=348a34d984d5265ae91a6a56f0ccbc613210238d;p=mirror_edk2.git diff --git a/UefiCpuPkg/Library/MpInitLib/MpLib.c b/UefiCpuPkg/Library/MpInitLib/MpLib.c index d0fbc17ce5..2d8e9512ab 100644 --- a/UefiCpuPkg/Library/MpInitLib/MpLib.c +++ b/UefiCpuPkg/Library/MpInitLib/MpLib.c @@ -1,14 +1,19 @@ /** @file CPU MP Initialize Library common functions. - Copyright (c) 2016 - 2020, Intel Corporation. All rights reserved.
+ Copyright (c) 2016 - 2021, Intel Corporation. All rights reserved.
+ Copyright (c) 2020, AMD Inc. All rights reserved.
+ SPDX-License-Identifier: BSD-2-Clause-Patent **/ #include "MpLib.h" +#include +#include +#include -EFI_GUID mCpuInitMpLibHobGuid = CPU_INIT_MP_LIB_HOB_GUID; +EFI_GUID mCpuInitMpLibHobGuid = CPU_INIT_MP_LIB_HOB_GUID; /** The function will check if BSP Execute Disable is enabled. @@ -32,7 +37,7 @@ IsBspExecuteDisableEnabled ( BOOLEAN Enabled; IA32_CR0 Cr0; - Enabled = FALSE; + Enabled = FALSE; Cr0.UintN = AsmReadCr0 (); if (Cr0.Bits.PG != 0) { // @@ -72,12 +77,12 @@ IsBspExecuteDisableEnabled ( VOID EFIAPI FutureBSPProc ( - IN VOID *Buffer + IN VOID *Buffer ) { - CPU_MP_DATA *DataInHob; + CPU_MP_DATA *DataInHob; - DataInHob = (CPU_MP_DATA *) Buffer; + DataInHob = (CPU_MP_DATA *)Buffer; AsmExchangeRole (&DataInHob->APInfo, &DataInHob->BSPInfo); } @@ -90,7 +95,7 @@ FutureBSPProc ( **/ CPU_STATE GetApState ( - IN CPU_AP_DATA *CpuData + IN CPU_AP_DATA *CpuData ) { return CpuData->State; @@ -104,8 +109,8 @@ GetApState ( **/ VOID SetApState ( - IN CPU_AP_DATA *CpuData, - IN CPU_STATE State + IN CPU_AP_DATA *CpuData, + IN CPU_STATE State ) { AcquireSpinLock (&CpuData->ApLock); @@ -120,7 +125,7 @@ SetApState ( **/ VOID SaveLocalApicTimerSetting ( - IN CPU_MP_DATA *CpuMpData + IN CPU_MP_DATA *CpuMpData ) { // @@ -142,7 +147,7 @@ SaveLocalApicTimerSetting ( **/ VOID SyncLocalApicTimerSetting ( - IN CPU_MP_DATA *CpuMpData + IN CPU_MP_DATA *CpuMpData ) { // @@ -167,10 +172,10 @@ SyncLocalApicTimerSetting ( **/ VOID SaveVolatileRegisters ( - OUT CPU_VOLATILE_REGISTERS *VolatileRegisters + OUT CPU_VOLATILE_REGISTERS *VolatileRegisters ) { - CPUID_VERSION_INFO_EDX VersionInfoEdx; + CPUID_VERSION_INFO_EDX VersionInfoEdx; VolatileRegisters->Cr0 = AsmReadCr0 (); VolatileRegisters->Cr3 = AsmReadCr3 (); @@ -204,12 +209,12 @@ SaveVolatileRegisters ( **/ VOID RestoreVolatileRegisters ( - IN CPU_VOLATILE_REGISTERS *VolatileRegisters, - IN BOOLEAN IsRestoreDr + IN CPU_VOLATILE_REGISTERS *VolatileRegisters, + IN BOOLEAN IsRestoreDr ) { - CPUID_VERSION_INFO_EDX VersionInfoEdx; - IA32_TSS_DESCRIPTOR *Tss; + CPUID_VERSION_INFO_EDX VersionInfoEdx; + IA32_TSS_DESCRIPTOR *Tss; AsmWriteCr3 (VolatileRegisters->Cr3); AsmWriteCr4 (VolatileRegisters->Cr4); @@ -233,8 +238,9 @@ RestoreVolatileRegisters ( AsmWriteGdtr (&VolatileRegisters->Gdtr); AsmWriteIdtr (&VolatileRegisters->Idtr); - if (VolatileRegisters->Tr != 0 && - VolatileRegisters->Tr < VolatileRegisters->Gdtr.Limit) { + if ((VolatileRegisters->Tr != 0) && + (VolatileRegisters->Tr < VolatileRegisters->Gdtr.Limit)) + { Tss = (IA32_TSS_DESCRIPTOR *)(VolatileRegisters->Gdtr.Base + VolatileRegisters->Tr); if (Tss->Bits.P == 1) { @@ -255,7 +261,7 @@ IsMwaitSupport ( VOID ) { - CPUID_VERSION_INFO_ECX VersionInfoEcx; + CPUID_VERSION_INFO_ECX VersionInfoEcx; AsmCpuid (CPUID_VERSION_INFO, NULL, NULL, &VersionInfoEcx.Uint32, NULL); return (VersionInfoEcx.Bits.MONITOR == 1) ? TRUE : FALSE; @@ -270,11 +276,11 @@ IsMwaitSupport ( **/ UINT8 GetApLoopMode ( - OUT UINT32 *MonitorFilterSize + OUT UINT32 *MonitorFilterSize ) { - UINT8 ApLoopMode; - CPUID_MONITOR_MWAIT_EBX MonitorMwaitEbx; + UINT8 ApLoopMode; + CPUID_MONITOR_MWAIT_EBX MonitorMwaitEbx; ASSERT (MonitorFilterSize != NULL); @@ -288,6 +294,14 @@ GetApLoopMode ( // ApLoopMode = ApInHltLoop; } + + if (PcdGetBool (PcdSevEsIsEnabled)) { + // + // For SEV-ES, force AP in Hlt-loop mode in order to use the GHCB + // protocol for starting APs + // + ApLoopMode = ApInHltLoop; + } } if (ApLoopMode != ApInMwaitLoop) { @@ -314,20 +328,20 @@ GetApLoopMode ( **/ VOID SortApicId ( - IN CPU_MP_DATA *CpuMpData + IN CPU_MP_DATA *CpuMpData ) { - UINTN Index1; - UINTN Index2; - UINTN Index3; - UINT32 ApicId; - CPU_INFO_IN_HOB CpuInfo; - UINT32 ApCount; - CPU_INFO_IN_HOB *CpuInfoInHob; - volatile UINT32 *StartupApSignal; - - ApCount = CpuMpData->CpuCount - 1; - CpuInfoInHob = (CPU_INFO_IN_HOB *) (UINTN) CpuMpData->CpuInfoInHob; + UINTN Index1; + UINTN Index2; + UINTN Index3; + UINT32 ApicId; + CPU_INFO_IN_HOB CpuInfo; + UINT32 ApCount; + CPU_INFO_IN_HOB *CpuInfoInHob; + volatile UINT32 *StartupApSignal; + + ApCount = CpuMpData->CpuCount - 1; + CpuInfoInHob = (CPU_INFO_IN_HOB *)(UINTN)CpuMpData->CpuInfoInHob; if (ApCount != 0) { for (Index1 = 0; Index1 < ApCount; Index1++) { Index3 = Index1; @@ -341,6 +355,7 @@ SortApicId ( ApicId = CpuInfoInHob[Index2].ApicId; } } + if (Index3 != Index1) { CopyMem (&CpuInfo, &CpuInfoInHob[Index3], sizeof (CPU_INFO_IN_HOB)); CopyMem ( @@ -353,7 +368,7 @@ SortApicId ( // // Also exchange the StartupApSignal. // - StartupApSignal = CpuMpData->CpuData[Index3].StartupApSignal; + StartupApSignal = CpuMpData->CpuData[Index3].StartupApSignal; CpuMpData->CpuData[Index3].StartupApSignal = CpuMpData->CpuData[Index1].StartupApSignal; CpuMpData->CpuData[Index1].StartupApSignal = StartupApSignal; @@ -366,7 +381,7 @@ SortApicId ( ApicId = GetInitialApicId (); for (Index1 = 0; Index1 < CpuMpData->CpuCount; Index1++) { if (CpuInfoInHob[Index1].ApicId == ApicId) { - CpuMpData->BspNumber = (UINT32) Index1; + CpuMpData->BspNumber = (UINT32)Index1; break; } } @@ -402,8 +417,8 @@ ApInitializeSync ( UINTN ProcessorNumber; EFI_STATUS Status; - CpuMpData = (CPU_MP_DATA *) Buffer; - Status = GetProcessorNumber (CpuMpData, &ProcessorNumber); + CpuMpData = (CPU_MP_DATA *)Buffer; + Status = GetProcessorNumber (CpuMpData, &ProcessorNumber); ASSERT_EFI_ERROR (Status); // // Load microcode on AP @@ -426,20 +441,20 @@ ApInitializeSync ( **/ EFI_STATUS GetProcessorNumber ( - IN CPU_MP_DATA *CpuMpData, - OUT UINTN *ProcessorNumber + IN CPU_MP_DATA *CpuMpData, + OUT UINTN *ProcessorNumber ) { - UINTN TotalProcessorNumber; - UINTN Index; - CPU_INFO_IN_HOB *CpuInfoInHob; - UINT32 CurrentApicId; + UINTN TotalProcessorNumber; + UINTN Index; + CPU_INFO_IN_HOB *CpuInfoInHob; + UINT32 CurrentApicId; - CpuInfoInHob = (CPU_INFO_IN_HOB *) (UINTN) CpuMpData->CpuInfoInHob; + CpuInfoInHob = (CPU_INFO_IN_HOB *)(UINTN)CpuMpData->CpuInfoInHob; TotalProcessorNumber = CpuMpData->CpuCount; - CurrentApicId = GetApicId (); - for (Index = 0; Index < TotalProcessorNumber; Index ++) { + CurrentApicId = GetApicId (); + for (Index = 0; Index < TotalProcessorNumber; Index++) { if (CpuInfoInHob[Index].ApicId == CurrentApicId) { *ProcessorNumber = Index; return EFI_SUCCESS; @@ -458,12 +473,12 @@ GetProcessorNumber ( **/ UINTN CollectProcessorCount ( - IN CPU_MP_DATA *CpuMpData + IN CPU_MP_DATA *CpuMpData ) { - UINTN Index; - CPU_INFO_IN_HOB *CpuInfoInHob; - BOOLEAN X2Apic; + UINTN Index; + CPU_INFO_IN_HOB *CpuInfoInHob; + BOOLEAN X2Apic; // // Send 1st broadcast IPI to APs to wakeup APs @@ -471,14 +486,12 @@ CollectProcessorCount ( CpuMpData->InitFlag = ApInitConfig; WakeUpAP (CpuMpData, TRUE, 0, NULL, NULL, TRUE); CpuMpData->InitFlag = ApInitDone; - ASSERT (CpuMpData->CpuCount <= PcdGet32 (PcdCpuMaxLogicalProcessorNumber)); // - // Wait for all APs finished the initialization + // When InitFlag == ApInitConfig, WakeUpAP () guarantees all APs are checked in. + // FinishedCount is the number of check-in APs. // - while (CpuMpData->FinishedCount < (CpuMpData->CpuCount - 1)) { - CpuPause (); - } - + CpuMpData->CpuCount = CpuMpData->FinishedCount + 1; + ASSERT (CpuMpData->CpuCount <= PcdGet32 (PcdCpuMaxLogicalProcessorNumber)); // // Enable x2APIC mode if @@ -492,7 +505,7 @@ CollectProcessorCount ( // X2Apic = TRUE; } else { - CpuInfoInHob = (CPU_INFO_IN_HOB *) (UINTN) CpuMpData->CpuInfoInHob; + CpuInfoInHob = (CPU_INFO_IN_HOB *)(UINTN)CpuMpData->CpuInfoInHob; for (Index = 0; Index < CpuMpData->CpuCount; Index++) { if (CpuInfoInHob[Index].InitialApicId >= 0xFF) { X2Apic = TRUE; @@ -513,6 +526,7 @@ CollectProcessorCount ( while (CpuMpData->FinishedCount < (CpuMpData->CpuCount - 1)) { CpuPause (); } + // // Enable x2APIC on BSP // @@ -524,6 +538,7 @@ CollectProcessorCount ( SetApState (&CpuMpData->CpuData[Index], CpuStateIdle); } } + DEBUG ((DEBUG_INFO, "APIC MODE is %d\n", GetApicMode ())); // // Sort BSP/Aps by CPU APIC ID in ascending order @@ -546,16 +561,16 @@ CollectProcessorCount ( **/ VOID InitializeApData ( - IN OUT CPU_MP_DATA *CpuMpData, - IN UINTN ProcessorNumber, - IN UINT32 BistData, - IN UINT64 ApTopOfStack + IN OUT CPU_MP_DATA *CpuMpData, + IN UINTN ProcessorNumber, + IN UINT32 BistData, + IN UINT64 ApTopOfStack ) { - CPU_INFO_IN_HOB *CpuInfoInHob; - MSR_IA32_PLATFORM_ID_REGISTER PlatformIdMsr; + CPU_INFO_IN_HOB *CpuInfoInHob; + MSR_IA32_PLATFORM_ID_REGISTER PlatformIdMsr; - CpuInfoInHob = (CPU_INFO_IN_HOB *) (UINTN) CpuMpData->CpuInfoInHob; + CpuInfoInHob = (CPU_INFO_IN_HOB *)(UINTN)CpuMpData->CpuInfoInHob; CpuInfoInHob[ProcessorNumber].InitialApicId = GetInitialApicId (); CpuInfoInHob[ProcessorNumber].ApicId = GetApicId (); CpuInfoInHob[ProcessorNumber].Health = BistData; @@ -564,8 +579,13 @@ InitializeApData ( CpuMpData->CpuData[ProcessorNumber].Waiting = FALSE; CpuMpData->CpuData[ProcessorNumber].CpuHealthy = (BistData == 0) ? TRUE : FALSE; - PlatformIdMsr.Uint64 = AsmReadMsr64 (MSR_IA32_PLATFORM_ID); - CpuMpData->CpuData[ProcessorNumber].PlatformId = (UINT8) PlatformIdMsr.Bits.PlatformId; + // + // NOTE: PlatformId is not relevant on AMD platforms. + // + if (!StandardSignatureIsAuthenticAMD ()) { + PlatformIdMsr.Uint64 = AsmReadMsr64 (MSR_IA32_PLATFORM_ID); + CpuMpData->CpuData[ProcessorNumber].PlatformId = (UINT8)PlatformIdMsr.Bits.PlatformId; + } AsmCpuid ( CPUID_VERSION_INFO, @@ -575,10 +595,127 @@ InitializeApData ( NULL ); - InitializeSpinLock(&CpuMpData->CpuData[ProcessorNumber].ApLock); + InitializeSpinLock (&CpuMpData->CpuData[ProcessorNumber].ApLock); SetApState (&CpuMpData->CpuData[ProcessorNumber], CpuStateIdle); } +/** + Get Protected mode code segment with 16-bit default addressing + from current GDT table. + + @return Protected mode 16-bit code segment value. +**/ +STATIC +UINT16 +GetProtectedMode16CS ( + VOID + ) +{ + IA32_DESCRIPTOR GdtrDesc; + IA32_SEGMENT_DESCRIPTOR *GdtEntry; + UINTN GdtEntryCount; + UINT16 Index; + + Index = (UINT16)-1; + AsmReadGdtr (&GdtrDesc); + GdtEntryCount = (GdtrDesc.Limit + 1) / sizeof (IA32_SEGMENT_DESCRIPTOR); + GdtEntry = (IA32_SEGMENT_DESCRIPTOR *)GdtrDesc.Base; + for (Index = 0; Index < GdtEntryCount; Index++) { + if ((GdtEntry->Bits.L == 0) && + (GdtEntry->Bits.DB == 0) && + (GdtEntry->Bits.Type > 8)) + { + break; + } + + GdtEntry++; + } + + ASSERT (Index != GdtEntryCount); + return Index * 8; +} + +/** + Get Protected mode code segment with 32-bit default addressing + from current GDT table. + + @return Protected mode 32-bit code segment value. +**/ +STATIC +UINT16 +GetProtectedMode32CS ( + VOID + ) +{ + IA32_DESCRIPTOR GdtrDesc; + IA32_SEGMENT_DESCRIPTOR *GdtEntry; + UINTN GdtEntryCount; + UINT16 Index; + + Index = (UINT16)-1; + AsmReadGdtr (&GdtrDesc); + GdtEntryCount = (GdtrDesc.Limit + 1) / sizeof (IA32_SEGMENT_DESCRIPTOR); + GdtEntry = (IA32_SEGMENT_DESCRIPTOR *)GdtrDesc.Base; + for (Index = 0; Index < GdtEntryCount; Index++) { + if ((GdtEntry->Bits.L == 0) && + (GdtEntry->Bits.DB == 1) && + (GdtEntry->Bits.Type > 8)) + { + break; + } + + GdtEntry++; + } + + ASSERT (Index != GdtEntryCount); + return Index * 8; +} + +/** + Reset an AP when in SEV-ES mode. + + If successful, this function never returns. + + @param[in] Ghcb Pointer to the GHCB + @param[in] CpuMpData Pointer to CPU MP Data + +**/ +STATIC +VOID +MpInitLibSevEsAPReset ( + IN GHCB *Ghcb, + IN CPU_MP_DATA *CpuMpData + ) +{ + EFI_STATUS Status; + UINTN ProcessorNumber; + UINT16 Code16, Code32; + AP_RESET *APResetFn; + UINTN BufferStart; + UINTN StackStart; + + Status = GetProcessorNumber (CpuMpData, &ProcessorNumber); + ASSERT_EFI_ERROR (Status); + + Code16 = GetProtectedMode16CS (); + Code32 = GetProtectedMode32CS (); + + if (CpuMpData->WakeupBufferHigh != 0) { + APResetFn = (AP_RESET *)(CpuMpData->WakeupBufferHigh + CpuMpData->AddressMap.SwitchToRealNoNxOffset); + } else { + APResetFn = (AP_RESET *)(CpuMpData->MpCpuExchangeInfo->BufferStart + CpuMpData->AddressMap.SwitchToRealOffset); + } + + BufferStart = CpuMpData->MpCpuExchangeInfo->BufferStart; + StackStart = CpuMpData->SevEsAPResetStackStart - + (AP_RESET_STACK_SIZE * ProcessorNumber); + + // + // This call never returns. + // + APResetFn (BufferStart, Code16, Code32, StackStart); +} + /** This function will be called from AP reset code if BSP uses WakeUpAP. @@ -588,19 +725,19 @@ InitializeApData ( VOID EFIAPI ApWakeupFunction ( - IN MP_CPU_EXCHANGE_INFO *ExchangeInfo, - IN UINTN ApIndex + IN MP_CPU_EXCHANGE_INFO *ExchangeInfo, + IN UINTN ApIndex ) { - CPU_MP_DATA *CpuMpData; - UINTN ProcessorNumber; - EFI_AP_PROCEDURE Procedure; - VOID *Parameter; - UINT32 BistData; - volatile UINT32 *ApStartupSignalBuffer; - CPU_INFO_IN_HOB *CpuInfoInHob; - UINT64 ApTopOfStack; - UINTN CurrentApicMode; + CPU_MP_DATA *CpuMpData; + UINTN ProcessorNumber; + EFI_AP_PROCEDURE Procedure; + VOID *Parameter; + UINT32 BistData; + volatile UINT32 *ApStartupSignalBuffer; + CPU_INFO_IN_HOB *CpuInfoInHob; + UINT64 ApTopOfStack; + UINTN CurrentApicMode; // // AP finished assembly code and begin to execute C code @@ -621,16 +758,12 @@ ApWakeupFunction ( CurrentApicMode = GetApicMode (); while (TRUE) { if (CpuMpData->InitFlag == ApInitConfig) { - // - // Add CPU number - // - InterlockedIncrement ((UINT32 *) &CpuMpData->CpuCount); ProcessorNumber = ApIndex; // // This is first time AP wakeup, get BIST information from AP stack // - ApTopOfStack = CpuMpData->Buffer + (ProcessorNumber + 1) * CpuMpData->CpuApStackSize; - BistData = *(UINT32 *) ((UINTN) ApTopOfStack - sizeof (UINTN)); + ApTopOfStack = CpuMpData->Buffer + (ProcessorNumber + 1) * CpuMpData->CpuApStackSize; + BistData = *(UINT32 *)((UINTN)ApTopOfStack - sizeof (UINTN)); // // CpuMpData->CpuData[0].VolatileRegisters is initialized based on BSP environment, // to initialize AP in InitConfig path. @@ -639,8 +772,6 @@ ApWakeupFunction ( RestoreVolatileRegisters (&CpuMpData->CpuData[0].VolatileRegisters, FALSE); InitializeApData (CpuMpData, ProcessorNumber, BistData, ApTopOfStack); ApStartupSignalBuffer = CpuMpData->CpuData[ProcessorNumber].StartupApSignal; - - InterlockedDecrement ((UINT32 *) &CpuMpData->MpCpuExchangeInfo->NumApsExecuting); } else { // // Execute AP function if AP is ready @@ -651,27 +782,40 @@ ApWakeupFunction ( // ApStartupSignalBuffer = CpuMpData->CpuData[ProcessorNumber].StartupApSignal; InterlockedCompareExchange32 ( - (UINT32 *) ApStartupSignalBuffer, + (UINT32 *)ApStartupSignalBuffer, WAKEUP_AP_SIGNAL, 0 ); - if (CpuMpData->ApLoopMode == ApInHltLoop) { + + if (CpuMpData->InitFlag == ApInitReconfig) { // - // Restore AP's volatile registers saved + // ApInitReconfig happens when: + // 1. AP is re-enabled after it's disabled, in either PEI or DXE phase. + // 2. AP is initialized in DXE phase. + // In either case, use the volatile registers value derived from BSP. + // NOTE: IDTR.BASE stored in CpuMpData->CpuData[0].VolatileRegisters points to a + // different IDT shared by all APs. // - RestoreVolatileRegisters (&CpuMpData->CpuData[ProcessorNumber].VolatileRegisters, TRUE); + RestoreVolatileRegisters (&CpuMpData->CpuData[0].VolatileRegisters, FALSE); } else { - // - // The CPU driver might not flush TLB for APs on spot after updating - // page attributes. AP in mwait loop mode needs to take care of it when - // woken up. - // - CpuFlushTlb (); + if (CpuMpData->ApLoopMode == ApInHltLoop) { + // + // Restore AP's volatile registers saved before AP is halted + // + RestoreVolatileRegisters (&CpuMpData->CpuData[ProcessorNumber].VolatileRegisters, TRUE); + } else { + // + // The CPU driver might not flush TLB for APs on spot after updating + // page attributes. AP in mwait loop mode needs to take care of it when + // woken up. + // + CpuFlushTlb (); + } } if (GetApState (&CpuMpData->CpuData[ProcessorNumber]) == CpuStateReady) { Procedure = (EFI_AP_PROCEDURE)CpuMpData->CpuData[ProcessorNumber].ApFunction; - Parameter = (VOID *) CpuMpData->CpuData[ProcessorNumber].ApFunctionArgument; + Parameter = (VOID *)CpuMpData->CpuData[ProcessorNumber].ApFunctionArgument; if (Procedure != NULL) { SetApState (&CpuMpData->CpuData[ProcessorNumber], CpuStateBusy); // @@ -682,19 +826,20 @@ ApWakeupFunction ( // Invoke AP function here // Procedure (Parameter); - CpuInfoInHob = (CPU_INFO_IN_HOB *) (UINTN) CpuMpData->CpuInfoInHob; + CpuInfoInHob = (CPU_INFO_IN_HOB *)(UINTN)CpuMpData->CpuInfoInHob; if (CpuMpData->SwitchBspFlag) { // // Re-get the processor number due to BSP/AP maybe exchange in AP function // GetProcessorNumber (CpuMpData, &ProcessorNumber); - CpuMpData->CpuData[ProcessorNumber].ApFunction = 0; + CpuMpData->CpuData[ProcessorNumber].ApFunction = 0; CpuMpData->CpuData[ProcessorNumber].ApFunctionArgument = 0; - ApStartupSignalBuffer = CpuMpData->CpuData[ProcessorNumber].StartupApSignal; - CpuInfoInHob[ProcessorNumber].ApTopOfStack = CpuInfoInHob[CpuMpData->NewBspNumber].ApTopOfStack; + ApStartupSignalBuffer = CpuMpData->CpuData[ProcessorNumber].StartupApSignal; + CpuInfoInHob[ProcessorNumber].ApTopOfStack = CpuInfoInHob[CpuMpData->NewBspNumber].ApTopOfStack; } else { - if (CpuInfoInHob[ProcessorNumber].ApicId != GetApicId () || - CpuInfoInHob[ProcessorNumber].InitialApicId != GetInitialApicId ()) { + if ((CpuInfoInHob[ProcessorNumber].ApicId != GetApicId ()) || + (CpuInfoInHob[ProcessorNumber].InitialApicId != GetInitialApicId ())) + { if (CurrentApicMode != GetApicMode ()) { // // If APIC mode change happened during AP function execution, @@ -712,39 +857,100 @@ ApWakeupFunction ( } } } + SetApState (&CpuMpData->CpuData[ProcessorNumber], CpuStateFinished); } } + if (CpuMpData->ApLoopMode == ApInHltLoop) { + // + // Save AP volatile registers + // + SaveVolatileRegisters (&CpuMpData->CpuData[ProcessorNumber].VolatileRegisters); + } + // // AP finished executing C code // - InterlockedIncrement ((UINT32 *) &CpuMpData->FinishedCount); + InterlockedIncrement ((UINT32 *)&CpuMpData->FinishedCount); + + if (CpuMpData->InitFlag == ApInitConfig) { + // + // Delay decrementing the APs executing count when SEV-ES is enabled + // to allow the APs to issue an AP_RESET_HOLD before the BSP possibly + // performs another INIT-SIPI-SIPI sequence. + // + if (!CpuMpData->SevEsIsEnabled) { + InterlockedDecrement ((UINT32 *)&CpuMpData->MpCpuExchangeInfo->NumApsExecuting); + } + } // // Place AP is specified loop mode // if (CpuMpData->ApLoopMode == ApInHltLoop) { - // - // Save AP volatile registers - // - SaveVolatileRegisters (&CpuMpData->CpuData[ProcessorNumber].VolatileRegisters); // // Place AP in HLT-loop // while (TRUE) { DisableInterrupts (); - CpuSleep (); + if (CpuMpData->SevEsIsEnabled) { + MSR_SEV_ES_GHCB_REGISTER Msr; + GHCB *Ghcb; + UINT64 Status; + BOOLEAN DoDecrement; + BOOLEAN InterruptState; + + DoDecrement = (BOOLEAN)(CpuMpData->InitFlag == ApInitConfig); + + while (TRUE) { + Msr.GhcbPhysicalAddress = AsmReadMsr64 (MSR_SEV_ES_GHCB); + Ghcb = Msr.Ghcb; + + VmgInit (Ghcb, &InterruptState); + + if (DoDecrement) { + DoDecrement = FALSE; + + // + // Perform the delayed decrement just before issuing the first + // VMGEXIT with AP_RESET_HOLD. + // + InterlockedDecrement ((UINT32 *)&CpuMpData->MpCpuExchangeInfo->NumApsExecuting); + } + + Status = VmgExit (Ghcb, SVM_EXIT_AP_RESET_HOLD, 0, 0); + if ((Status == 0) && (Ghcb->SaveArea.SwExitInfo2 != 0)) { + VmgDone (Ghcb, InterruptState); + break; + } + + VmgDone (Ghcb, InterruptState); + } + + // + // Awakened in a new phase? Use the new CpuMpData + // + if (CpuMpData->NewCpuMpData != NULL) { + CpuMpData = CpuMpData->NewCpuMpData; + } + + MpInitLibSevEsAPReset (Ghcb, CpuMpData); + } else { + CpuSleep (); + } + CpuPause (); } } + while (TRUE) { DisableInterrupts (); if (CpuMpData->ApLoopMode == ApInMwaitLoop) { // // Place AP in MWAIT-loop // - AsmMonitor ((UINTN) ApStartupSignalBuffer, 0, 0); + AsmMonitor ((UINTN)ApStartupSignalBuffer, 0, 0); if (*ApStartupSignalBuffer != WAKEUP_AP_SIGNAL) { // // Check AP start-up signal again. @@ -780,7 +986,7 @@ ApWakeupFunction ( **/ VOID WaitApWakeup ( - IN volatile UINT32 *ApStartupSignalBuffer + IN volatile UINT32 *ApStartupSignalBuffer ) { // @@ -788,10 +994,11 @@ WaitApWakeup ( // Otherwise, write StartupApSignal again till AP waken up. // while (InterlockedCompareExchange32 ( - (UINT32 *) ApStartupSignalBuffer, - WAKEUP_AP_SIGNAL, - WAKEUP_AP_SIGNAL - ) != 0) { + (UINT32 *)ApStartupSignalBuffer, + WAKEUP_AP_SIGNAL, + WAKEUP_AP_SIGNAL + ) != 0) + { CpuPause (); } } @@ -804,31 +1011,30 @@ WaitApWakeup ( **/ VOID FillExchangeInfoData ( - IN CPU_MP_DATA *CpuMpData + IN CPU_MP_DATA *CpuMpData ) { - volatile MP_CPU_EXCHANGE_INFO *ExchangeInfo; - UINTN Size; - IA32_SEGMENT_DESCRIPTOR *Selector; - IA32_CR4 Cr4; + volatile MP_CPU_EXCHANGE_INFO *ExchangeInfo; + UINTN Size; + IA32_SEGMENT_DESCRIPTOR *Selector; + IA32_CR4 Cr4; - ExchangeInfo = CpuMpData->MpCpuExchangeInfo; - ExchangeInfo->Lock = 0; - ExchangeInfo->StackStart = CpuMpData->Buffer; - ExchangeInfo->StackSize = CpuMpData->CpuApStackSize; - ExchangeInfo->BufferStart = CpuMpData->WakeupBuffer; - ExchangeInfo->ModeOffset = CpuMpData->AddressMap.ModeEntryOffset; + ExchangeInfo = CpuMpData->MpCpuExchangeInfo; + ExchangeInfo->StackStart = CpuMpData->Buffer; + ExchangeInfo->StackSize = CpuMpData->CpuApStackSize; + ExchangeInfo->BufferStart = CpuMpData->WakeupBuffer; + ExchangeInfo->ModeOffset = CpuMpData->AddressMap.ModeEntryOffset; - ExchangeInfo->CodeSegment = AsmReadCs (); - ExchangeInfo->DataSegment = AsmReadDs (); + ExchangeInfo->CodeSegment = AsmReadCs (); + ExchangeInfo->DataSegment = AsmReadDs (); - ExchangeInfo->Cr3 = AsmReadCr3 (); + ExchangeInfo->Cr3 = AsmReadCr3 (); - ExchangeInfo->CFunction = (UINTN) ApWakeupFunction; + ExchangeInfo->CFunction = (UINTN)ApWakeupFunction; ExchangeInfo->ApIndex = 0; ExchangeInfo->NumApsExecuting = 0; - ExchangeInfo->InitFlag = (UINTN) CpuMpData->InitFlag; - ExchangeInfo->CpuInfo = (CPU_INFO_IN_HOB *) (UINTN) CpuMpData->CpuInfoInHob; + ExchangeInfo->InitFlag = (UINTN)CpuMpData->InitFlag; + ExchangeInfo->CpuInfo = (CPU_INFO_IN_HOB *)(UINTN)CpuMpData->CpuInfoInHob; ExchangeInfo->CpuMpData = CpuMpData; ExchangeInfo->EnableExecuteDisable = IsBspExecuteDisableEnabled (); @@ -843,29 +1049,33 @@ FillExchangeInfoData ( // Using latter way is simpler because it also eliminates the needs to // check whether platform wants to enable it. // - Cr4.UintN = AsmReadCr4 (); - ExchangeInfo->Enable5LevelPaging = (BOOLEAN) (Cr4.Bits.LA57 == 1); + Cr4.UintN = AsmReadCr4 (); + ExchangeInfo->Enable5LevelPaging = (BOOLEAN)(Cr4.Bits.LA57 == 1); DEBUG ((DEBUG_INFO, "%a: 5-Level Paging = %d\n", gEfiCallerBaseName, ExchangeInfo->Enable5LevelPaging)); + ExchangeInfo->SevEsIsEnabled = CpuMpData->SevEsIsEnabled; + ExchangeInfo->GhcbBase = (UINTN)CpuMpData->GhcbBase; + // // Get the BSP's data of GDT and IDT // - AsmReadGdtr ((IA32_DESCRIPTOR *) &ExchangeInfo->GdtrProfile); - AsmReadIdtr ((IA32_DESCRIPTOR *) &ExchangeInfo->IdtrProfile); + AsmReadGdtr ((IA32_DESCRIPTOR *)&ExchangeInfo->GdtrProfile); + AsmReadIdtr ((IA32_DESCRIPTOR *)&ExchangeInfo->IdtrProfile); // // Find a 32-bit code segment // Selector = (IA32_SEGMENT_DESCRIPTOR *)ExchangeInfo->GdtrProfile.Base; - Size = ExchangeInfo->GdtrProfile.Limit + 1; + Size = ExchangeInfo->GdtrProfile.Limit + 1; while (Size > 0) { - if (Selector->Bits.L == 0 && Selector->Bits.Type >= 8) { + if ((Selector->Bits.L == 0) && (Selector->Bits.Type >= 8)) { ExchangeInfo->ModeTransitionSegment = (UINT16)((UINTN)Selector - ExchangeInfo->GdtrProfile.Base); break; } + Selector += 1; - Size -= sizeof (IA32_SEGMENT_DESCRIPTOR); + Size -= sizeof (IA32_SEGMENT_DESCRIPTOR); } // @@ -873,7 +1083,8 @@ FillExchangeInfoData ( // EfiBootServicesCode to avoid page fault if NX memory protection is enabled. // if (CpuMpData->WakeupBufferHigh != 0) { - Size = CpuMpData->AddressMap.RendezvousFunnelSize - + Size = CpuMpData->AddressMap.RendezvousFunnelSize + + CpuMpData->AddressMap.SwitchToRealSize - CpuMpData->AddressMap.ModeTransitionOffset; CopyMem ( (VOID *)CpuMpData->WakeupBufferHigh, @@ -885,12 +1096,12 @@ FillExchangeInfoData ( ExchangeInfo->ModeTransitionMemory = (UINT32)CpuMpData->WakeupBufferHigh; } else { ExchangeInfo->ModeTransitionMemory = (UINT32) - (ExchangeInfo->BufferStart + CpuMpData->AddressMap.ModeTransitionOffset); + (ExchangeInfo->BufferStart + CpuMpData->AddressMap.ModeTransitionOffset); } ExchangeInfo->ModeHighMemory = ExchangeInfo->ModeTransitionMemory + - (UINT32)ExchangeInfo->ModeOffset - - (UINT32)CpuMpData->AddressMap.ModeTransitionOffset; + (UINT32)ExchangeInfo->ModeOffset - + (UINT32)CpuMpData->AddressMap.ModeTransitionOffset; ExchangeInfo->ModeHighSegment = (UINT16)ExchangeInfo->CodeSegment; } @@ -904,9 +1115,9 @@ FillExchangeInfoData ( **/ VOID TimedWaitForApFinish ( - IN CPU_MP_DATA *CpuMpData, - IN UINT32 FinishedApLimit, - IN UINT32 TimeLimit + IN CPU_MP_DATA *CpuMpData, + IN UINT32 FinishedApLimit, + IN UINT32 TimeLimit ); /** @@ -915,19 +1126,20 @@ TimedWaitForApFinish ( @param[in] CpuMpData The pointer to CPU MP Data structure. **/ VOID -BackupAndPrepareWakeupBuffer( - IN CPU_MP_DATA *CpuMpData +BackupAndPrepareWakeupBuffer ( + IN CPU_MP_DATA *CpuMpData ) { CopyMem ( - (VOID *) CpuMpData->BackupBuffer, - (VOID *) CpuMpData->WakeupBuffer, + (VOID *)CpuMpData->BackupBuffer, + (VOID *)CpuMpData->WakeupBuffer, CpuMpData->BackupBufferSize ); CopyMem ( - (VOID *) CpuMpData->WakeupBuffer, - (VOID *) CpuMpData->AddressMap.RendezvousFunnelAddress, - CpuMpData->AddressMap.RendezvousFunnelSize + (VOID *)CpuMpData->WakeupBuffer, + (VOID *)CpuMpData->AddressMap.RendezvousFunnelAddress, + CpuMpData->AddressMap.RendezvousFunnelSize + + CpuMpData->AddressMap.SwitchToRealSize ); } @@ -937,17 +1149,39 @@ BackupAndPrepareWakeupBuffer( @param[in] CpuMpData The pointer to CPU MP Data structure. **/ VOID -RestoreWakeupBuffer( - IN CPU_MP_DATA *CpuMpData +RestoreWakeupBuffer ( + IN CPU_MP_DATA *CpuMpData ) { CopyMem ( - (VOID *) CpuMpData->WakeupBuffer, - (VOID *) CpuMpData->BackupBuffer, + (VOID *)CpuMpData->WakeupBuffer, + (VOID *)CpuMpData->BackupBuffer, CpuMpData->BackupBufferSize ); } +/** + Calculate the size of the reset vector. + + @param[in] AddressMap The pointer to Address Map structure. + + @return Total amount of memory required for the AP reset area +**/ +STATIC +UINTN +GetApResetVectorSize ( + IN MP_ASSEMBLY_ADDRESS_MAP *AddressMap + ) +{ + UINTN Size; + + Size = AddressMap->RendezvousFunnelSize + + AddressMap->SwitchToRealSize + + sizeof (MP_CPU_EXCHANGE_INFO); + + return Size; +} + /** Allocate reset vector buffer. @@ -955,23 +1189,61 @@ RestoreWakeupBuffer( **/ VOID AllocateResetVector ( - IN OUT CPU_MP_DATA *CpuMpData + IN OUT CPU_MP_DATA *CpuMpData ) { - UINTN ApResetVectorSize; + UINTN ApResetVectorSize; + UINTN ApResetStackSize; - if (CpuMpData->WakeupBuffer == (UINTN) -1) { - ApResetVectorSize = CpuMpData->AddressMap.RendezvousFunnelSize + - sizeof (MP_CPU_EXCHANGE_INFO); + if (CpuMpData->WakeupBuffer == (UINTN)-1) { + ApResetVectorSize = GetApResetVectorSize (&CpuMpData->AddressMap); CpuMpData->WakeupBuffer = GetWakeupBuffer (ApResetVectorSize); - CpuMpData->MpCpuExchangeInfo = (MP_CPU_EXCHANGE_INFO *) (UINTN) - (CpuMpData->WakeupBuffer + CpuMpData->AddressMap.RendezvousFunnelSize); - CpuMpData->WakeupBufferHigh = GetModeTransitionBuffer ( - CpuMpData->AddressMap.RendezvousFunnelSize - + CpuMpData->MpCpuExchangeInfo = (MP_CPU_EXCHANGE_INFO *)(UINTN) + (CpuMpData->WakeupBuffer + + CpuMpData->AddressMap.RendezvousFunnelSize + + CpuMpData->AddressMap.SwitchToRealSize); + CpuMpData->WakeupBufferHigh = GetModeTransitionBuffer ( + CpuMpData->AddressMap.RendezvousFunnelSize + + CpuMpData->AddressMap.SwitchToRealSize - CpuMpData->AddressMap.ModeTransitionOffset ); + // + // The AP reset stack is only used by SEV-ES guests. Do not allocate it + // if SEV-ES is not enabled. + // + if (PcdGetBool (PcdSevEsIsEnabled)) { + // + // Stack location is based on ProcessorNumber, so use the total number + // of processors for calculating the total stack area. + // + ApResetStackSize = (AP_RESET_STACK_SIZE * + PcdGet32 (PcdCpuMaxLogicalProcessorNumber)); + + // + // Invoke GetWakeupBuffer a second time to allocate the stack area + // below 1MB. The returned buffer will be page aligned and sized and + // below the previously allocated buffer. + // + CpuMpData->SevEsAPResetStackStart = GetWakeupBuffer (ApResetStackSize); + + // + // Check to be sure that the "allocate below" behavior hasn't changed. + // This will also catch a failed allocation, as "-1" is returned on + // failure. + // + if (CpuMpData->SevEsAPResetStackStart >= CpuMpData->WakeupBuffer) { + DEBUG (( + DEBUG_ERROR, + "SEV-ES AP reset stack is not below wakeup buffer\n" + )); + + ASSERT (FALSE); + CpuDeadLoop (); + } + } } + BackupAndPrepareWakeupBuffer (CpuMpData); } @@ -982,10 +1254,83 @@ AllocateResetVector ( **/ VOID FreeResetVector ( - IN CPU_MP_DATA *CpuMpData + IN CPU_MP_DATA *CpuMpData ) { - RestoreWakeupBuffer (CpuMpData); + // + // If SEV-ES is enabled, the reset area is needed for AP parking and + // and AP startup in the OS, so the reset area is reserved. Do not + // perform the restore as this will overwrite memory which has data + // needed by SEV-ES. + // + if (!CpuMpData->SevEsIsEnabled) { + RestoreWakeupBuffer (CpuMpData); + } +} + +/** + Allocate the SEV-ES AP jump table buffer. + + @param[in, out] CpuMpData The pointer to CPU MP Data structure. +**/ +VOID +AllocateSevEsAPMemory ( + IN OUT CPU_MP_DATA *CpuMpData + ) +{ + if (CpuMpData->SevEsAPBuffer == (UINTN)-1) { + CpuMpData->SevEsAPBuffer = + CpuMpData->SevEsIsEnabled ? GetSevEsAPMemory () : 0; + } +} + +/** + Program the SEV-ES AP jump table buffer. + + @param[in] SipiVector The SIPI vector used for the AP Reset +**/ +VOID +SetSevEsJumpTable ( + IN UINTN SipiVector + ) +{ + SEV_ES_AP_JMP_FAR *JmpFar; + UINT32 Offset, InsnByte; + UINT8 LoNib, HiNib; + + JmpFar = (SEV_ES_AP_JMP_FAR *)(UINTN)FixedPcdGet32 (PcdSevEsWorkAreaBase); + ASSERT (JmpFar != NULL); + + // + // Obtain the address of the Segment/Rip location in the workarea. + // This will be set to a value derived from the SIPI vector and will + // be the memory address used for the far jump below. + // + Offset = FixedPcdGet32 (PcdSevEsWorkAreaBase); + Offset += sizeof (JmpFar->InsnBuffer); + LoNib = (UINT8)Offset; + HiNib = (UINT8)(Offset >> 8); + + // + // Program the workarea (which is the initial AP boot address) with + // far jump to the SIPI vector (where XX and YY represent the + // address of where the SIPI vector is stored. + // + // JMP FAR [CS:XXYY] => 2E FF 2E YY XX + // + InsnByte = 0; + JmpFar->InsnBuffer[InsnByte++] = 0x2E; // CS override prefix + JmpFar->InsnBuffer[InsnByte++] = 0xFF; // JMP (FAR) + JmpFar->InsnBuffer[InsnByte++] = 0x2E; // ModRM (JMP memory location) + JmpFar->InsnBuffer[InsnByte++] = LoNib; // YY offset ... + JmpFar->InsnBuffer[InsnByte++] = HiNib; // XX offset ... + + // + // Program the Segment/Rip based on the SIPI vector (always at least + // 16-byte aligned, so Rip is set to 0). + // + JmpFar->Rip = 0; + JmpFar->Segment = (UINT16)(SipiVector >> 4); } /** @@ -1001,27 +1346,29 @@ FreeResetVector ( **/ VOID WakeUpAP ( - IN CPU_MP_DATA *CpuMpData, - IN BOOLEAN Broadcast, - IN UINTN ProcessorNumber, - IN EFI_AP_PROCEDURE Procedure, OPTIONAL - IN VOID *ProcedureArgument, OPTIONAL - IN BOOLEAN WakeUpDisabledAps + IN CPU_MP_DATA *CpuMpData, + IN BOOLEAN Broadcast, + IN UINTN ProcessorNumber, + IN EFI_AP_PROCEDURE Procedure OPTIONAL, + IN VOID *ProcedureArgument OPTIONAL, + IN BOOLEAN WakeUpDisabledAps ) { - volatile MP_CPU_EXCHANGE_INFO *ExchangeInfo; - UINTN Index; - CPU_AP_DATA *CpuData; - BOOLEAN ResetVectorRequired; - CPU_INFO_IN_HOB *CpuInfoInHob; + volatile MP_CPU_EXCHANGE_INFO *ExchangeInfo; + UINTN Index; + CPU_AP_DATA *CpuData; + BOOLEAN ResetVectorRequired; + CPU_INFO_IN_HOB *CpuInfoInHob; CpuMpData->FinishedCount = 0; - ResetVectorRequired = FALSE; + ResetVectorRequired = FALSE; if (CpuMpData->WakeUpByInitSipiSipi || - CpuMpData->InitFlag != ApInitDone) { + (CpuMpData->InitFlag != ApInitDone)) + { ResetVectorRequired = TRUE; AllocateResetVector (CpuMpData); + AllocateSevEsAPMemory (CpuMpData); FillExchangeInfoData (CpuMpData); SaveLocalApicTimerSetting (CpuMpData); } @@ -1045,24 +1392,35 @@ WakeUpAP ( // the AP procedure will be skipped for disabled AP because AP state // is not CpuStateReady. // - if (GetApState (CpuData) == CpuStateDisabled && !WakeUpDisabledAps) { + if ((GetApState (CpuData) == CpuStateDisabled) && !WakeUpDisabledAps) { continue; } - CpuData->ApFunction = (UINTN) Procedure; - CpuData->ApFunctionArgument = (UINTN) ProcedureArgument; + CpuData->ApFunction = (UINTN)Procedure; + CpuData->ApFunctionArgument = (UINTN)ProcedureArgument; SetApState (CpuData, CpuStateReady); if (CpuMpData->InitFlag != ApInitConfig) { - *(UINT32 *) CpuData->StartupApSignal = WAKEUP_AP_SIGNAL; + *(UINT32 *)CpuData->StartupApSignal = WAKEUP_AP_SIGNAL; } } } + if (ResetVectorRequired) { + // + // For SEV-ES, the initial AP boot address will be defined by + // PcdSevEsWorkAreaBase. The Segment/Rip must be the jump address + // from the original INIT-SIPI-SIPI. + // + if (CpuMpData->SevEsIsEnabled) { + SetSevEsJumpTable (ExchangeInfo->BufferStart); + } + // // Wakeup all APs // - SendInitSipiSipiAllExcludingSelf ((UINT32) ExchangeInfo->BufferStart); + SendInitSipiSipiAllExcludingSelf ((UINT32)ExchangeInfo->BufferStart); } + if (CpuMpData->InitFlag == ApInitConfig) { if (PcdGet32 (PcdCpuBootLogicalProcessorNumber) > 0) { // @@ -1123,7 +1481,7 @@ WakeUpAP ( ); while (CpuMpData->MpCpuExchangeInfo->NumApsExecuting != 0) { - CpuPause(); + CpuPause (); } } } else { @@ -1138,22 +1496,33 @@ WakeUpAP ( } } } else { - CpuData = &CpuMpData->CpuData[ProcessorNumber]; - CpuData->ApFunction = (UINTN) Procedure; - CpuData->ApFunctionArgument = (UINTN) ProcedureArgument; + CpuData = &CpuMpData->CpuData[ProcessorNumber]; + CpuData->ApFunction = (UINTN)Procedure; + CpuData->ApFunctionArgument = (UINTN)ProcedureArgument; SetApState (CpuData, CpuStateReady); // // Wakeup specified AP // ASSERT (CpuMpData->InitFlag != ApInitConfig); - *(UINT32 *) CpuData->StartupApSignal = WAKEUP_AP_SIGNAL; + *(UINT32 *)CpuData->StartupApSignal = WAKEUP_AP_SIGNAL; if (ResetVectorRequired) { - CpuInfoInHob = (CPU_INFO_IN_HOB *) (UINTN) CpuMpData->CpuInfoInHob; + CpuInfoInHob = (CPU_INFO_IN_HOB *)(UINTN)CpuMpData->CpuInfoInHob; + + // + // For SEV-ES, the initial AP boot address will be defined by + // PcdSevEsWorkAreaBase. The Segment/Rip must be the jump address + // from the original INIT-SIPI-SIPI. + // + if (CpuMpData->SevEsIsEnabled) { + SetSevEsJumpTable (ExchangeInfo->BufferStart); + } + SendInitSipiSipi ( CpuInfoInHob[ProcessorNumber].ApicId, - (UINT32) ExchangeInfo->BufferStart + (UINT32)ExchangeInfo->BufferStart ); } + // // Wait specified AP waken up // @@ -1193,8 +1562,8 @@ CalculateTimeout ( OUT UINT64 *CurrentTime ) { - UINT64 TimeoutInSeconds; - UINT64 TimestampCounterFreq; + UINT64 TimeoutInSeconds; + UINT64 TimestampCounterFreq; // // Read the current value of the performance counter @@ -1279,25 +1648,30 @@ CheckTimeout ( if (Timeout == 0) { return FALSE; } + GetPerformanceCounterProperties (&Start, &End); Cycle = End - Start; if (Cycle < 0) { Cycle = -Cycle; } + Cycle++; - CurrentTime = GetPerformanceCounter(); - Delta = (INT64) (CurrentTime - *PreviousTime); + CurrentTime = GetPerformanceCounter (); + Delta = (INT64)(CurrentTime - *PreviousTime); if (Start > End) { Delta = -Delta; } + if (Delta < 0) { Delta += Cycle; } - *TotalTime += Delta; + + *TotalTime += Delta; *PreviousTime = CurrentTime; if (*TotalTime > Timeout) { return TRUE; } + return FALSE; } @@ -1311,9 +1685,9 @@ CheckTimeout ( **/ VOID TimedWaitForApFinish ( - IN CPU_MP_DATA *CpuMpData, - IN UINT32 FinishedApLimit, - IN UINT32 TimeLimit + IN CPU_MP_DATA *CpuMpData, + IN UINT32 FinishedApLimit, + IN UINT32 TimeLimit ) { // @@ -1324,7 +1698,7 @@ TimedWaitForApFinish ( return; } - CpuMpData->TotalTime = 0; + CpuMpData->TotalTime = 0; CpuMpData->ExpectedTime = CalculateTimeout ( TimeLimit, &CpuMpData->CurrentTime @@ -1334,7 +1708,8 @@ TimedWaitForApFinish ( &CpuMpData->CurrentTime, &CpuMpData->TotalTime, CpuMpData->ExpectedTime - )) { + )) + { CpuPause (); } @@ -1363,10 +1738,10 @@ TimedWaitForApFinish ( **/ VOID ResetProcessorToIdleState ( - IN UINTN ProcessorNumber + IN UINTN ProcessorNumber ) { - CPU_MP_DATA *CpuMpData; + CPU_MP_DATA *CpuMpData; CpuMpData = GetCpuMpData (); @@ -1375,6 +1750,7 @@ ResetProcessorToIdleState ( while (CpuMpData->FinishedCount < 1) { CpuPause (); } + CpuMpData->InitFlag = ApInitDone; SetApState (&CpuMpData->CpuData[ProcessorNumber], CpuStateIdle); @@ -1393,11 +1769,11 @@ ResetProcessorToIdleState ( **/ EFI_STATUS GetNextWaitingProcessorNumber ( - OUT UINTN *NextProcessorNumber + OUT UINTN *NextProcessorNumber ) { - UINTN ProcessorNumber; - CPU_MP_DATA *CpuMpData; + UINTN ProcessorNumber; + CPU_MP_DATA *CpuMpData; CpuMpData = GetCpuMpData (); @@ -1424,11 +1800,11 @@ GetNextWaitingProcessorNumber ( **/ EFI_STATUS CheckThisAP ( - IN UINTN ProcessorNumber + IN UINTN ProcessorNumber ) { - CPU_MP_DATA *CpuMpData; - CPU_AP_DATA *CpuData; + CPU_MP_DATA *CpuMpData; + CPU_AP_DATA *CpuData; CpuMpData = GetCpuMpData (); CpuData = &CpuMpData->CpuData[ProcessorNumber]; @@ -1441,10 +1817,11 @@ CheckThisAP ( // // If the AP finishes for StartupThisAP(), return EFI_SUCCESS. // - if (GetApState(CpuData) == CpuStateFinished) { + if (GetApState (CpuData) == CpuStateFinished) { if (CpuData->Finished != NULL) { *(CpuData->Finished) = TRUE; } + SetApState (CpuData, CpuStateIdle); return EFI_SUCCESS; } else { @@ -1455,6 +1832,7 @@ CheckThisAP ( if (CpuData->Finished != NULL) { *(CpuData->Finished) = FALSE; } + // // Reset failed AP to idle state // @@ -1463,6 +1841,7 @@ CheckThisAP ( return EFI_TIMEOUT; } } + return EFI_NOT_READY; } @@ -1481,12 +1860,12 @@ CheckAllAPs ( VOID ) { - UINTN ProcessorNumber; - UINTN NextProcessorNumber; - UINTN ListIndex; - EFI_STATUS Status; - CPU_MP_DATA *CpuMpData; - CPU_AP_DATA *CpuData; + UINTN ProcessorNumber; + UINTN NextProcessorNumber; + UINTN ListIndex; + EFI_STATUS Status; + CPU_MP_DATA *CpuMpData; + CPU_AP_DATA *CpuData; CpuMpData = GetCpuMpData (); @@ -1506,10 +1885,10 @@ CheckAllAPs ( // Only BSP and corresponding AP access this unit of CPU Data. This means the AP will not modify the // value of state after setting the it to CpuStateIdle, so BSP can safely make use of its value. // - if (GetApState(CpuData) == CpuStateFinished) { - CpuMpData->RunningCount --; + if (GetApState (CpuData) == CpuStateFinished) { + CpuMpData->RunningCount--; CpuMpData->CpuData[ProcessorNumber].Waiting = FALSE; - SetApState(CpuData, CpuStateIdle); + SetApState (CpuData, CpuStateIdle); // // If in Single Thread mode, then search for the next waiting AP for execution. @@ -1521,12 +1900,12 @@ CheckAllAPs ( WakeUpAP ( CpuMpData, FALSE, - (UINT32) NextProcessorNumber, + (UINT32)NextProcessorNumber, CpuMpData->Procedure, CpuMpData->ProcArguments, TRUE ); - } + } } } } @@ -1542,18 +1921,21 @@ CheckAllAPs ( // If timeout expires, report timeout. // if (CheckTimeout ( - &CpuMpData->CurrentTime, - &CpuMpData->TotalTime, - CpuMpData->ExpectedTime) - ) { + &CpuMpData->CurrentTime, + &CpuMpData->TotalTime, + CpuMpData->ExpectedTime + ) + ) + { // // If FailedCpuList is not NULL, record all failed APs in it. // if (CpuMpData->FailedCpuList != NULL) { *CpuMpData->FailedCpuList = - AllocatePool ((CpuMpData->RunningCount + 1) * sizeof (UINTN)); + AllocatePool ((CpuMpData->RunningCount + 1) * sizeof (UINTN)); ASSERT (*CpuMpData->FailedCpuList != NULL); } + ListIndex = 0; for (ProcessorNumber = 0; ProcessorNumber < CpuMpData->CpuCount; ProcessorNumber++) { @@ -1571,11 +1953,14 @@ CheckAllAPs ( } } } + if (CpuMpData->FailedCpuList != NULL) { (*CpuMpData->FailedCpuList)[ListIndex] = END_OF_CPU_LIST; } + return EFI_TIMEOUT; } + return EFI_NOT_READY; } @@ -1618,16 +2003,17 @@ MpInitLibInitialize ( OldCpuMpData = GetCpuMpDataFromGuidedHob (); if (OldCpuMpData == NULL) { - MaxLogicalProcessorNumber = PcdGet32(PcdCpuMaxLogicalProcessorNumber); + MaxLogicalProcessorNumber = PcdGet32 (PcdCpuMaxLogicalProcessorNumber); } else { MaxLogicalProcessorNumber = OldCpuMpData->CpuCount; } + ASSERT (MaxLogicalProcessorNumber != 0); AsmGetAddressMap (&AddressMap); - ApResetVectorSize = AddressMap.RendezvousFunnelSize + sizeof (MP_CPU_EXCHANGE_INFO); - ApStackSize = PcdGet32(PcdCpuApStackSize); - ApLoopMode = GetApLoopMode (&MonitorFilterSize); + ApResetVectorSize = GetApResetVectorSize (&AddressMap); + ApStackSize = PcdGet32 (PcdCpuApStackSize); + ApLoopMode = GetApLoopMode (&MonitorFilterSize); // // Save BSP's Control registers for APs. @@ -1644,7 +2030,7 @@ MpInitLibInitialize ( MpBuffer = AllocatePages (EFI_SIZE_TO_PAGES (BufferSize)); ASSERT (MpBuffer != NULL); ZeroMem (MpBuffer, BufferSize); - Buffer = (UINTN) MpBuffer; + Buffer = (UINTN)MpBuffer; // // The layout of the Buffer is as below: @@ -1667,28 +2053,33 @@ MpInitLibInitialize ( // CPU_INFO_IN_HOB (N) // +--------------------+ // - MonitorBuffer = (UINT8 *) (Buffer + ApStackSize * MaxLogicalProcessorNumber); - BackupBufferAddr = (UINTN) MonitorBuffer + MonitorFilterSize * MaxLogicalProcessorNumber; - ApIdtBase = ALIGN_VALUE (BackupBufferAddr + ApResetVectorSize, 8); - CpuMpData = (CPU_MP_DATA *) (ApIdtBase + VolatileRegisters.Idtr.Limit + 1); + MonitorBuffer = (UINT8 *)(Buffer + ApStackSize * MaxLogicalProcessorNumber); + BackupBufferAddr = (UINTN)MonitorBuffer + MonitorFilterSize * MaxLogicalProcessorNumber; + ApIdtBase = ALIGN_VALUE (BackupBufferAddr + ApResetVectorSize, 8); + CpuMpData = (CPU_MP_DATA *)(ApIdtBase + VolatileRegisters.Idtr.Limit + 1); CpuMpData->Buffer = Buffer; CpuMpData->CpuApStackSize = ApStackSize; CpuMpData->BackupBuffer = BackupBufferAddr; CpuMpData->BackupBufferSize = ApResetVectorSize; - CpuMpData->WakeupBuffer = (UINTN) -1; + CpuMpData->WakeupBuffer = (UINTN)-1; CpuMpData->CpuCount = 1; CpuMpData->BspNumber = 0; CpuMpData->WaitEvent = NULL; CpuMpData->SwitchBspFlag = FALSE; - CpuMpData->CpuData = (CPU_AP_DATA *) (CpuMpData + 1); - CpuMpData->CpuInfoInHob = (UINT64) (UINTN) (CpuMpData->CpuData + MaxLogicalProcessorNumber); - InitializeSpinLock(&CpuMpData->MpLock); + CpuMpData->CpuData = (CPU_AP_DATA *)(CpuMpData + 1); + CpuMpData->CpuInfoInHob = (UINT64)(UINTN)(CpuMpData->CpuData + MaxLogicalProcessorNumber); + InitializeSpinLock (&CpuMpData->MpLock); + CpuMpData->SevEsIsEnabled = PcdGetBool (PcdSevEsIsEnabled); + CpuMpData->SevEsAPBuffer = (UINTN)-1; + CpuMpData->GhcbBase = PcdGet64 (PcdGhcbBase); // // Make sure no memory usage outside of the allocated buffer. // - ASSERT ((CpuMpData->CpuInfoInHob + sizeof (CPU_INFO_IN_HOB) * MaxLogicalProcessorNumber) == - Buffer + BufferSize); + ASSERT ( + (CpuMpData->CpuInfoInHob + sizeof (CPU_INFO_IN_HOB) * MaxLogicalProcessorNumber) == + Buffer + BufferSize + ); // // Duplicate BSP's IDT to APs. @@ -1724,6 +2115,7 @@ MpInitLibInitialize ( CpuMpData->CpuData[Index].StartupApSignal = (UINT32 *)(MonitorBuffer + MonitorFilterSize * Index); } + // // Enable the local APIC for Virtual Wire Mode. // @@ -1741,22 +2133,23 @@ MpInitLibInitialize ( // APs have been wakeup before, just get the CPU Information // from HOB // - CpuMpData->CpuCount = OldCpuMpData->CpuCount; - CpuMpData->BspNumber = OldCpuMpData->BspNumber; - CpuMpData->CpuInfoInHob = OldCpuMpData->CpuInfoInHob; - CpuInfoInHob = (CPU_INFO_IN_HOB *) (UINTN) CpuMpData->CpuInfoInHob; + OldCpuMpData->NewCpuMpData = CpuMpData; + CpuMpData->CpuCount = OldCpuMpData->CpuCount; + CpuMpData->BspNumber = OldCpuMpData->BspNumber; + CpuMpData->CpuInfoInHob = OldCpuMpData->CpuInfoInHob; + CpuInfoInHob = (CPU_INFO_IN_HOB *)(UINTN)CpuMpData->CpuInfoInHob; for (Index = 0; Index < CpuMpData->CpuCount; Index++) { - InitializeSpinLock(&CpuMpData->CpuData[Index].ApLock); - CpuMpData->CpuData[Index].CpuHealthy = (CpuInfoInHob[Index].Health == 0)? TRUE:FALSE; + InitializeSpinLock (&CpuMpData->CpuData[Index].ApLock); + CpuMpData->CpuData[Index].CpuHealthy = (CpuInfoInHob[Index].Health == 0) ? TRUE : FALSE; CpuMpData->CpuData[Index].ApFunction = 0; - CopyMem (&CpuMpData->CpuData[Index].VolatileRegisters, &VolatileRegisters, sizeof (CPU_VOLATILE_REGISTERS)); } } if (!GetMicrocodePatchInfoFromHob ( &CpuMpData->MicrocodePatchAddress, &CpuMpData->MicrocodePatchRegionSize - )) { + )) + { // // The microcode patch information cache HOB does not exist, which means // the microcode patches data has not been loaded into memory yet @@ -1777,7 +2170,15 @@ MpInitLibInitialize ( // Wakeup APs to do some AP initialize sync (Microcode & MTRR) // if (CpuMpData->CpuCount > 1) { - CpuMpData->InitFlag = ApInitReconfig; + if (OldCpuMpData != NULL) { + // + // Only needs to use this flag for DXE phase to update the wake up + // buffer. Wakeup buffer allocated in PEI phase is no longer valid + // in DXE. + // + CpuMpData->InitFlag = ApInitReconfig; + } + WakeUpAP (CpuMpData, TRUE, 0, ApInitializeSync, CpuMpData, TRUE); // // Wait for all APs finished initialization @@ -1785,12 +2186,47 @@ MpInitLibInitialize ( while (CpuMpData->FinishedCount < (CpuMpData->CpuCount - 1)) { CpuPause (); } - CpuMpData->InitFlag = ApInitDone; + + if (OldCpuMpData != NULL) { + CpuMpData->InitFlag = ApInitDone; + } + for (Index = 0; Index < CpuMpData->CpuCount; Index++) { SetApState (&CpuMpData->CpuData[Index], CpuStateIdle); } } + // + // Dump the microcode revision for each core. + // + DEBUG_CODE_BEGIN (); + UINT32 ThreadId; + UINT32 ExpectedMicrocodeRevision; + + CpuInfoInHob = (CPU_INFO_IN_HOB *)(UINTN)CpuMpData->CpuInfoInHob; + for (Index = 0; Index < CpuMpData->CpuCount; Index++) { + GetProcessorLocationByApicId (CpuInfoInHob[Index].InitialApicId, NULL, NULL, &ThreadId); + if (ThreadId == 0) { + // + // MicrocodeDetect() loads microcode in first thread of each core, so, + // CpuMpData->CpuData[Index].MicrocodeEntryAddr is initialized only for first thread of each core. + // + ExpectedMicrocodeRevision = 0; + if (CpuMpData->CpuData[Index].MicrocodeEntryAddr != 0) { + ExpectedMicrocodeRevision = ((CPU_MICROCODE_HEADER *)(UINTN)CpuMpData->CpuData[Index].MicrocodeEntryAddr)->UpdateRevision; + } + + DEBUG (( + DEBUG_INFO, + "CPU[%04d]: Microcode revision = %08x, expected = %08x\n", + Index, + CpuMpData->CpuData[Index].MicrocodeRevision, + ExpectedMicrocodeRevision + )); + } + } + + DEBUG_CODE_END (); // // Initialize global data for MP support // @@ -1824,12 +2260,19 @@ MpInitLibGetProcessorInfo ( OUT EFI_HEALTH_FLAGS *HealthData OPTIONAL ) { - CPU_MP_DATA *CpuMpData; - UINTN CallerNumber; - CPU_INFO_IN_HOB *CpuInfoInHob; + CPU_MP_DATA *CpuMpData; + UINTN CallerNumber; + CPU_INFO_IN_HOB *CpuInfoInHob; + UINTN OriginalProcessorNumber; - CpuMpData = GetCpuMpData (); - CpuInfoInHob = (CPU_INFO_IN_HOB *) (UINTN) CpuMpData->CpuInfoInHob; + CpuMpData = GetCpuMpData (); + CpuInfoInHob = (CPU_INFO_IN_HOB *)(UINTN)CpuMpData->CpuInfoInHob; + + // + // Lower 24 bits contains the actual processor number. + // + OriginalProcessorNumber = ProcessorNumber; + ProcessorNumber &= BIT24 - 1; // // Check whether caller processor is BSP @@ -1847,14 +2290,16 @@ MpInitLibGetProcessorInfo ( return EFI_NOT_FOUND; } - ProcessorInfoBuffer->ProcessorId = (UINT64) CpuInfoInHob[ProcessorNumber].ApicId; + ProcessorInfoBuffer->ProcessorId = (UINT64)CpuInfoInHob[ProcessorNumber].ApicId; ProcessorInfoBuffer->StatusFlag = 0; if (ProcessorNumber == CpuMpData->BspNumber) { ProcessorInfoBuffer->StatusFlag |= PROCESSOR_AS_BSP_BIT; } + if (CpuMpData->CpuData[ProcessorNumber].CpuHealthy) { ProcessorInfoBuffer->StatusFlag |= PROCESSOR_HEALTH_STATUS_BIT; } + if (GetApState (&CpuMpData->CpuData[ProcessorNumber]) == CpuStateDisabled) { ProcessorInfoBuffer->StatusFlag &= ~PROCESSOR_ENABLED_BIT; } else { @@ -1871,6 +2316,18 @@ MpInitLibGetProcessorInfo ( &ProcessorInfoBuffer->Location.Thread ); + if ((OriginalProcessorNumber & CPU_V2_EXTENDED_TOPOLOGY) != 0) { + GetProcessorLocation2ByApicId ( + CpuInfoInHob[ProcessorNumber].ApicId, + &ProcessorInfoBuffer->ExtendedInformation.Location2.Package, + &ProcessorInfoBuffer->ExtendedInformation.Location2.Die, + &ProcessorInfoBuffer->ExtendedInformation.Location2.Tile, + &ProcessorInfoBuffer->ExtendedInformation.Location2.Module, + &ProcessorInfoBuffer->ExtendedInformation.Location2.Core, + &ProcessorInfoBuffer->ExtendedInformation.Location2.Thread + ); + } + if (HealthData != NULL) { HealthData->Uint32 = CpuInfoInHob[ProcessorNumber].Health; } @@ -1891,8 +2348,8 @@ MpInitLibGetProcessorInfo ( **/ EFI_STATUS SwitchBSPWorker ( - IN UINTN ProcessorNumber, - IN BOOLEAN EnableOldBSP + IN UINTN ProcessorNumber, + IN BOOLEAN EnableOldBSP ) { CPU_MP_DATA *CpuMpData; @@ -1965,7 +2422,7 @@ SwitchBSPWorker ( // // Clear the BSP bit of MSR_IA32_APIC_BASE // - ApicBaseMsr.Uint64 = AsmReadMsr64 (MSR_IA32_APIC_BASE); + ApicBaseMsr.Uint64 = AsmReadMsr64 (MSR_IA32_APIC_BASE); ApicBaseMsr.Bits.BSP = 0; AsmWriteMsr64 (MSR_IA32_APIC_BASE, ApicBaseMsr.Uint64); @@ -1979,7 +2436,7 @@ SwitchBSPWorker ( // // Set the BSP bit of MSR_IA32_APIC_BASE on new BSP // - ApicBaseMsr.Uint64 = AsmReadMsr64 (MSR_IA32_APIC_BASE); + ApicBaseMsr.Uint64 = AsmReadMsr64 (MSR_IA32_APIC_BASE); ApicBaseMsr.Bits.BSP = 1; AsmWriteMsr64 (MSR_IA32_APIC_BASE, ApicBaseMsr.Uint64); ProgramVirtualWireMode (); @@ -2000,10 +2457,11 @@ SwitchBSPWorker ( } else { SetApState (&CpuMpData->CpuData[CallerNumber], CpuStateIdle); } + // // Save new BSP number // - CpuMpData->BspNumber = (UINT32) ProcessorNumber; + CpuMpData->BspNumber = (UINT32)ProcessorNumber; // // Restore interrupt state. @@ -2033,13 +2491,13 @@ SwitchBSPWorker ( **/ EFI_STATUS EnableDisableApWorker ( - IN UINTN ProcessorNumber, - IN BOOLEAN EnableAP, - IN UINT32 *HealthFlag OPTIONAL + IN UINTN ProcessorNumber, + IN BOOLEAN EnableAP, + IN UINT32 *HealthFlag OPTIONAL ) { - CPU_MP_DATA *CpuMpData; - UINTN CallerNumber; + CPU_MP_DATA *CpuMpData; + UINTN CallerNumber; CpuMpData = GetCpuMpData (); @@ -2067,7 +2525,7 @@ EnableDisableApWorker ( if (HealthFlag != NULL) { CpuMpData->CpuData[ProcessorNumber].CpuHealthy = - (BOOLEAN) ((*HealthFlag & PROCESSOR_HEALTH_STATUS_BIT) != 0); + (BOOLEAN)((*HealthFlag & PROCESSOR_HEALTH_STATUS_BIT) != 0); } return EFI_SUCCESS; @@ -2092,10 +2550,10 @@ EnableDisableApWorker ( EFI_STATUS EFIAPI MpInitLibWhoAmI ( - OUT UINTN *ProcessorNumber + OUT UINTN *ProcessorNumber ) { - CPU_MP_DATA *CpuMpData; + CPU_MP_DATA *CpuMpData; if (ProcessorNumber == NULL) { return EFI_INVALID_PARAMETER; @@ -2129,15 +2587,15 @@ MpInitLibWhoAmI ( EFI_STATUS EFIAPI MpInitLibGetNumberOfProcessors ( - OUT UINTN *NumberOfProcessors, OPTIONAL - OUT UINTN *NumberOfEnabledProcessors OPTIONAL + OUT UINTN *NumberOfProcessors OPTIONAL, + OUT UINTN *NumberOfEnabledProcessors OPTIONAL ) { - CPU_MP_DATA *CpuMpData; - UINTN CallerNumber; - UINTN ProcessorNumber; - UINTN EnabledProcessorNumber; - UINTN Index; + CPU_MP_DATA *CpuMpData; + UINTN CallerNumber; + UINTN ProcessorNumber; + UINTN EnabledProcessorNumber; + UINTN Index; CpuMpData = GetCpuMpData (); @@ -2157,13 +2615,14 @@ MpInitLibGetNumberOfProcessors ( EnabledProcessorNumber = 0; for (Index = 0; Index < ProcessorNumber; Index++) { if (GetApState (&CpuMpData->CpuData[Index]) != CpuStateDisabled) { - EnabledProcessorNumber ++; + EnabledProcessorNumber++; } } if (NumberOfProcessors != NULL) { *NumberOfProcessors = ProcessorNumber; } + if (NumberOfEnabledProcessors != NULL) { *NumberOfEnabledProcessors = EnabledProcessorNumber; } @@ -2171,7 +2630,6 @@ MpInitLibGetNumberOfProcessors ( return EFI_SUCCESS; } - /** Worker function to execute a caller provided function on all enabled APs. @@ -2206,23 +2664,23 @@ MpInitLibGetNumberOfProcessors ( **/ EFI_STATUS StartupAllCPUsWorker ( - IN EFI_AP_PROCEDURE Procedure, - IN BOOLEAN SingleThread, - IN BOOLEAN ExcludeBsp, - IN EFI_EVENT WaitEvent OPTIONAL, - IN UINTN TimeoutInMicroseconds, - IN VOID *ProcedureArgument OPTIONAL, - OUT UINTN **FailedCpuList OPTIONAL + IN EFI_AP_PROCEDURE Procedure, + IN BOOLEAN SingleThread, + IN BOOLEAN ExcludeBsp, + IN EFI_EVENT WaitEvent OPTIONAL, + IN UINTN TimeoutInMicroseconds, + IN VOID *ProcedureArgument OPTIONAL, + OUT UINTN **FailedCpuList OPTIONAL ) { - EFI_STATUS Status; - CPU_MP_DATA *CpuMpData; - UINTN ProcessorCount; - UINTN ProcessorNumber; - UINTN CallerNumber; - CPU_AP_DATA *CpuData; - BOOLEAN HasEnabledAp; - CPU_STATE ApState; + EFI_STATUS Status; + CPU_MP_DATA *CpuMpData; + UINTN ProcessorCount; + UINTN ProcessorNumber; + UINTN CallerNumber; + CPU_AP_DATA *CpuData; + BOOLEAN HasEnabledAp; + CPU_STATE ApState; CpuMpData = GetCpuMpData (); @@ -2230,7 +2688,7 @@ StartupAllCPUsWorker ( *FailedCpuList = NULL; } - if (CpuMpData->CpuCount == 1 && ExcludeBsp) { + if ((CpuMpData->CpuCount == 1) && ExcludeBsp) { return EFI_NOT_STARTED; } @@ -2282,7 +2740,7 @@ StartupAllCPUsWorker ( CpuMpData->RunningCount = 0; for (ProcessorNumber = 0; ProcessorNumber < ProcessorCount; ProcessorNumber++) { - CpuData = &CpuMpData->CpuData[ProcessorNumber]; + CpuData = &CpuMpData->CpuData[ProcessorNumber]; CpuData->Waiting = FALSE; if (ProcessorNumber != CpuMpData->BspNumber) { if (CpuData->State == CpuStateIdle) { @@ -2304,8 +2762,8 @@ StartupAllCPUsWorker ( TimeoutInMicroseconds, &CpuMpData->CurrentTime ); - CpuMpData->TotalTime = 0; - CpuMpData->WaitEvent = WaitEvent; + CpuMpData->TotalTime = 0; + CpuMpData->WaitEvent = WaitEvent; if (!SingleThread) { WakeUpAP (CpuMpData, TRUE, 0, Procedure, ProcedureArgument, FALSE); @@ -2314,6 +2772,7 @@ StartupAllCPUsWorker ( if (ProcessorNumber == CallerNumber) { continue; } + if (CpuMpData->CpuData[ProcessorNumber].Waiting) { WakeUpAP (CpuMpData, FALSE, ProcessorNumber, Procedure, ProcedureArgument, TRUE); break; @@ -2363,18 +2822,18 @@ StartupAllCPUsWorker ( **/ EFI_STATUS StartupThisAPWorker ( - IN EFI_AP_PROCEDURE Procedure, - IN UINTN ProcessorNumber, - IN EFI_EVENT WaitEvent OPTIONAL, - IN UINTN TimeoutInMicroseconds, - IN VOID *ProcedureArgument OPTIONAL, - OUT BOOLEAN *Finished OPTIONAL + IN EFI_AP_PROCEDURE Procedure, + IN UINTN ProcessorNumber, + IN EFI_EVENT WaitEvent OPTIONAL, + IN UINTN TimeoutInMicroseconds, + IN VOID *ProcedureArgument OPTIONAL, + OUT BOOLEAN *Finished OPTIONAL ) { - EFI_STATUS Status; - CPU_MP_DATA *CpuMpData; - CPU_AP_DATA *CpuData; - UINTN CallerNumber; + EFI_STATUS Status; + CPU_MP_DATA *CpuMpData; + CPU_AP_DATA *CpuData; + UINTN CallerNumber; CpuMpData = GetCpuMpData (); @@ -2428,7 +2887,7 @@ StartupThisAPWorker ( // BSP saves data for CheckAPsStatus(), and returns EFI_SUCCESS. // CheckAPsStatus() will check completion and timeout periodically. // - CpuData = &CpuMpData->CpuData[ProcessorNumber]; + CpuData = &CpuMpData->CpuData[ProcessorNumber]; CpuData->WaitEvent = WaitEvent; CpuData->Finished = Finished; CpuData->ExpectedTime = CalculateTimeout (TimeoutInMicroseconds, &CpuData->CurrentTime); @@ -2460,16 +2919,17 @@ GetCpuMpDataFromGuidedHob ( VOID ) { - EFI_HOB_GUID_TYPE *GuidHob; - VOID *DataInHob; - CPU_MP_DATA *CpuMpData; + EFI_HOB_GUID_TYPE *GuidHob; + VOID *DataInHob; + CPU_MP_DATA *CpuMpData; CpuMpData = NULL; - GuidHob = GetFirstGuidHob (&mCpuInitMpLibHobGuid); + GuidHob = GetFirstGuidHob (&mCpuInitMpLibHobGuid); if (GuidHob != NULL) { DataInHob = GET_GUID_HOB_DATA (GuidHob); - CpuMpData = (CPU_MP_DATA *) (*(UINTN *) DataInHob); + CpuMpData = (CPU_MP_DATA *)(*(UINTN *)DataInHob); } + return CpuMpData; } @@ -2502,9 +2962,9 @@ GetCpuMpDataFromGuidedHob ( EFI_STATUS EFIAPI MpInitLibStartupAllCPUs ( - IN EFI_AP_PROCEDURE Procedure, - IN UINTN TimeoutInMicroseconds, - IN VOID *ProcedureArgument OPTIONAL + IN EFI_AP_PROCEDURE Procedure, + IN UINTN TimeoutInMicroseconds, + IN VOID *ProcedureArgument OPTIONAL ) { return StartupAllCPUsWorker (