X-Git-Url: https://git.proxmox.com/?a=blobdiff_plain;f=UefiCpuPkg%2FLibrary%2FMpInitLib%2FMpLib.c;h=e5dc852ed95fd23a6fbd6f437cbc1e062e42e969;hb=HEAD;hp=64a4c3546e22f254b463a2b4ee279b6e118aba64;hpb=4c0f6e349d32cf27a7104ddd3e729d6ebc88ea70;p=mirror_edk2.git diff --git a/UefiCpuPkg/Library/MpInitLib/MpLib.c b/UefiCpuPkg/Library/MpInitLib/MpLib.c index 64a4c3546e..e5dc852ed9 100644 --- a/UefiCpuPkg/Library/MpInitLib/MpLib.c +++ b/UefiCpuPkg/Library/MpInitLib/MpLib.c @@ -1,7 +1,7 @@ /** @file CPU MP Initialize Library common functions. - Copyright (c) 2016 - 2020, Intel Corporation. All rights reserved.
+ Copyright (c) 2016 - 2022, Intel Corporation. All rights reserved.
Copyright (c) 2020, AMD Inc. All rights reserved.
SPDX-License-Identifier: BSD-2-Clause-Patent @@ -9,32 +9,34 @@ **/ #include "MpLib.h" +#include +#include +#include -EFI_GUID mCpuInitMpLibHobGuid = CPU_INIT_MP_LIB_HOB_GUID; - +EFI_GUID mCpuInitMpLibHobGuid = CPU_INIT_MP_LIB_HOB_GUID; /** - Determine if the standard CPU signature is "AuthenticAMD". + Save the volatile registers required to be restored following INIT IPI. - @retval TRUE The CPU signature matches. - @retval FALSE The CPU signature does not match. + @param[out] VolatileRegisters Returns buffer saved the volatile resisters +**/ +VOID +SaveVolatileRegisters ( + OUT CPU_VOLATILE_REGISTERS *VolatileRegisters + ); +/** + Restore the volatile registers following INIT IPI. + + @param[in] VolatileRegisters Pointer to volatile resisters + @param[in] IsRestoreDr TRUE: Restore DRx if supported + FALSE: Do not restore DRx **/ -STATIC -BOOLEAN -StandardSignatureIsAuthenticAMD ( - VOID - ) -{ - UINT32 RegEbx; - UINT32 RegEcx; - UINT32 RegEdx; - - AsmCpuid (CPUID_SIGNATURE, NULL, &RegEbx, &RegEcx, &RegEdx); - return (RegEbx == CPUID_SIGNATURE_AUTHENTIC_AMD_EBX && - RegEcx == CPUID_SIGNATURE_AUTHENTIC_AMD_ECX && - RegEdx == CPUID_SIGNATURE_AUTHENTIC_AMD_EDX); -} +VOID +RestoreVolatileRegisters ( + IN CPU_VOLATILE_REGISTERS *VolatileRegisters, + IN BOOLEAN IsRestoreDr + ); /** The function will check if BSP Execute Disable is enabled. @@ -58,7 +60,7 @@ IsBspExecuteDisableEnabled ( BOOLEAN Enabled; IA32_CR0 Cr0; - Enabled = FALSE; + Enabled = FALSE; Cr0.UintN = AsmReadCr0 (); if (Cr0.Bits.PG != 0) { // @@ -98,13 +100,18 @@ 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; + // + // Save and restore volatile registers when switch BSP + // + SaveVolatileRegisters (&DataInHob->APInfo.VolatileRegisters); AsmExchangeRole (&DataInHob->APInfo, &DataInHob->BSPInfo); + RestoreVolatileRegisters (&DataInHob->APInfo.VolatileRegisters, FALSE); } /** @@ -116,7 +123,7 @@ FutureBSPProc ( **/ CPU_STATE GetApState ( - IN CPU_AP_DATA *CpuData + IN CPU_AP_DATA *CpuData ) { return CpuData->State; @@ -130,8 +137,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); @@ -146,7 +153,7 @@ SetApState ( **/ VOID SaveLocalApicTimerSetting ( - IN CPU_MP_DATA *CpuMpData + IN CPU_MP_DATA *CpuMpData ) { // @@ -168,7 +175,7 @@ SaveLocalApicTimerSetting ( **/ VOID SyncLocalApicTimerSetting ( - IN CPU_MP_DATA *CpuMpData + IN CPU_MP_DATA *CpuMpData ) { // @@ -193,10 +200,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 (); @@ -230,12 +237,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); @@ -259,8 +266,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) { @@ -281,7 +289,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; @@ -296,11 +304,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); @@ -314,6 +322,16 @@ GetApLoopMode ( // ApLoopMode = ApInHltLoop; } + + if (ConfidentialComputingGuestHas (CCAttrAmdSevEs) && + !ConfidentialComputingGuestHas (CCAttrAmdSevSnp)) + { + // + // For SEV-ES (SEV-SNP is also considered SEV-ES), force AP in Hlt-loop + // mode in order to use the GHCB protocol for starting APs + // + ApLoopMode = ApInHltLoop; + } } if (ApLoopMode != ApInMwaitLoop) { @@ -340,20 +358,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; @@ -367,6 +385,7 @@ SortApicId ( ApicId = CpuInfoInHob[Index2].ApicId; } } + if (Index3 != Index1) { CopyMem (&CpuInfo, &CpuInfoInHob[Index3], sizeof (CPU_INFO_IN_HOB)); CopyMem ( @@ -379,7 +398,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; @@ -392,7 +411,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; } } @@ -428,8 +447,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 @@ -452,20 +471,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; @@ -484,12 +503,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 @@ -497,14 +516,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 @@ -518,7 +535,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; @@ -539,6 +556,7 @@ CollectProcessorCount ( while (CpuMpData->FinishedCount < (CpuMpData->CpuCount - 1)) { CpuPause (); } + // // Enable x2APIC on BSP // @@ -550,6 +568,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 @@ -572,21 +591,28 @@ 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; + AP_STACK_DATA *ApStackData; - 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; CpuInfoInHob[ProcessorNumber].ApTopOfStack = ApTopOfStack; + // + // AP_STACK_DATA is stored at the top of AP Stack + // + ApStackData = (AP_STACK_DATA *)((UINTN)ApTopOfStack - sizeof (AP_STACK_DATA)); + ApStackData->MpData = CpuMpData; + CpuMpData->CpuData[ProcessorNumber].Waiting = FALSE; CpuMpData->CpuData[ProcessorNumber].CpuHealthy = (BistData == 0) ? TRUE : FALSE; @@ -594,7 +620,7 @@ InitializeApData ( // NOTE: PlatformId is not relevant on AMD platforms. // if (!StandardSignatureIsAuthenticAMD ()) { - PlatformIdMsr.Uint64 = AsmReadMsr64 (MSR_IA32_PLATFORM_ID); + PlatformIdMsr.Uint64 = AsmReadMsr64 (MSR_IA32_PLATFORM_ID); CpuMpData->CpuData[ProcessorNumber].PlatformId = (UINT8)PlatformIdMsr.Bits.PlatformId; } @@ -606,7 +632,7 @@ InitializeApData ( NULL ); - InitializeSpinLock(&CpuMpData->CpuData[ProcessorNumber].ApLock); + InitializeSpinLock (&CpuMpData->CpuData[ProcessorNumber].ApLock); SetApState (&CpuMpData->CpuData[ProcessorNumber], CpuStateIdle); } @@ -619,19 +645,20 @@ 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_STACK_DATA *ApStackData; // // AP finished assembly code and begin to execute C code @@ -652,16 +679,14 @@ 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; + ApStackData = (AP_STACK_DATA *)((UINTN)ApTopOfStack - sizeof (AP_STACK_DATA)); + BistData = (UINT32)ApStackData->Bist; + // // CpuMpData->CpuData[0].VolatileRegisters is initialized based on BSP environment, // to initialize AP in InitConfig path. @@ -670,8 +695,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 @@ -682,27 +705,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); // @@ -713,19 +749,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, @@ -743,39 +780,60 @@ 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->UseSevEsAPMethod) { + 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->UseSevEsAPMethod) { + SevEsPlaceApHlt (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. @@ -811,7 +869,7 @@ ApWakeupFunction ( **/ VOID WaitApWakeup ( - IN volatile UINT32 *ApStartupSignalBuffer + IN volatile UINT32 *ApStartupSignalBuffer ) { // @@ -819,14 +877,39 @@ 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 (); } } +/** + Calculate the size of the reset vector. + + @param[in] AddressMap The pointer to Address Map structure. + @param[out] SizeBelow1Mb Return the size of below 1MB memory for AP reset area. + @param[out] SizeAbove1Mb Return the size of abvoe 1MB memory for AP reset area. +**/ +STATIC +VOID +GetApResetVectorSize ( + IN MP_ASSEMBLY_ADDRESS_MAP *AddressMap, + OUT UINTN *SizeBelow1Mb OPTIONAL, + OUT UINTN *SizeAbove1Mb OPTIONAL + ) +{ + if (SizeBelow1Mb != NULL) { + *SizeBelow1Mb = AddressMap->ModeTransitionOffset + sizeof (MP_CPU_EXCHANGE_INFO); + } + + if (SizeAbove1Mb != NULL) { + *SizeAbove1Mb = AddressMap->RendezvousFunnelSize - AddressMap->ModeTransitionOffset; + } +} + /** This function will fill the exchange info structure. @@ -835,31 +918,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 (); @@ -874,54 +956,48 @@ 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->SevSnpIsEnabled = CpuMpData->SevSnpIsEnabled; + ExchangeInfo->GhcbBase = (UINTN)CpuMpData->GhcbBase; + + // + // Populate SEV-ES specific exchange data. + // + if (ExchangeInfo->SevSnpIsEnabled) { + FillExchangeInfoDataSevEs (ExchangeInfo); + } + // // 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); } - // - // Copy all 32-bit code and 64-bit code into memory with type of - // EfiBootServicesCode to avoid page fault if NX memory protection is enabled. - // - if (CpuMpData->WakeupBufferHigh != 0) { - Size = CpuMpData->AddressMap.RendezvousFunnelSize - - CpuMpData->AddressMap.ModeTransitionOffset; - CopyMem ( - (VOID *)CpuMpData->WakeupBufferHigh, - CpuMpData->AddressMap.RendezvousFunnelAddress + - CpuMpData->AddressMap.ModeTransitionOffset, - Size - ); - - ExchangeInfo->ModeTransitionMemory = (UINT32)CpuMpData->WakeupBufferHigh; - } else { - ExchangeInfo->ModeTransitionMemory = (UINT32) - (ExchangeInfo->BufferStart + CpuMpData->AddressMap.ModeTransitionOffset); - } + ExchangeInfo->ModeTransitionMemory = (UINT32)CpuMpData->WakeupBufferHigh; ExchangeInfo->ModeHighMemory = ExchangeInfo->ModeTransitionMemory + - (UINT32)ExchangeInfo->ModeOffset - - (UINT32)CpuMpData->AddressMap.ModeTransitionOffset; + (UINT32)ExchangeInfo->ModeOffset - + (UINT32)CpuMpData->AddressMap.ModeTransitionOffset; ExchangeInfo->ModeHighSegment = (UINT16)ExchangeInfo->CodeSegment; } @@ -935,9 +1011,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 ); /** @@ -946,19 +1022,19 @@ 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->BackupBufferSize - sizeof (MP_CPU_EXCHANGE_INFO) ); } @@ -968,13 +1044,13 @@ 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 ); } @@ -985,24 +1061,64 @@ RestoreWakeupBuffer( @param[in, out] CpuMpData The pointer to CPU MP Data structure. **/ VOID -AllocateResetVector ( - IN OUT CPU_MP_DATA *CpuMpData +AllocateResetVectorBelow1Mb ( + IN OUT CPU_MP_DATA *CpuMpData ) { - UINTN ApResetVectorSize; + UINTN ApResetStackSize; + + if (CpuMpData->WakeupBuffer == (UINTN)-1) { + CpuMpData->WakeupBuffer = GetWakeupBuffer (CpuMpData->BackupBufferSize); + CpuMpData->MpCpuExchangeInfo = (MP_CPU_EXCHANGE_INFO *)(UINTN) + (CpuMpData->WakeupBuffer + CpuMpData->BackupBufferSize - sizeof (MP_CPU_EXCHANGE_INFO)); + DEBUG (( + DEBUG_INFO, + "AP Vector: 16-bit = %p/%x, ExchangeInfo = %p/%x\n", + CpuMpData->WakeupBuffer, + CpuMpData->BackupBufferSize - sizeof (MP_CPU_EXCHANGE_INFO), + CpuMpData->MpCpuExchangeInfo, + sizeof (MP_CPU_EXCHANGE_INFO) + )); + // + // The AP reset stack is only used by SEV-ES guests. Do not allocate it + // if SEV-ES is not enabled. An SEV-SNP guest is also considered + // an SEV-ES guest, but uses a different method of AP startup, eliminating + // the need for the allocation. + // + if (ConfidentialComputingGuestHas (CCAttrAmdSevEs) && + !ConfidentialComputingGuestHas (CCAttrAmdSevSnp)) + { + // + // 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)); - if (CpuMpData->WakeupBuffer == (UINTN) -1) { - ApResetVectorSize = CpuMpData->AddressMap.RendezvousFunnelSize + - sizeof (MP_CPU_EXCHANGE_INFO); + // + // 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" + )); - CpuMpData->WakeupBuffer = GetWakeupBuffer (ApResetVectorSize); - CpuMpData->MpCpuExchangeInfo = (MP_CPU_EXCHANGE_INFO *) (UINTN) - (CpuMpData->WakeupBuffer + CpuMpData->AddressMap.RendezvousFunnelSize); - CpuMpData->WakeupBufferHigh = GetModeTransitionBuffer ( - CpuMpData->AddressMap.RendezvousFunnelSize - - CpuMpData->AddressMap.ModeTransitionOffset - ); + ASSERT (FALSE); + CpuDeadLoop (); + } + } } + BackupAndPrepareWakeupBuffer (CpuMpData); } @@ -1013,10 +1129,18 @@ 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->UseSevEsAPMethod) { + RestoreWakeupBuffer (CpuMpData); + } } /** @@ -1032,27 +1156,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); + AllocateResetVectorBelow1Mb (CpuMpData); + AllocateSevEsAPMemory (CpuMpData); FillExchangeInfoData (CpuMpData); SaveLocalApicTimerSetting (CpuMpData); } @@ -1076,24 +1202,41 @@ 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 and SEV-SNP, 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 + // Must use the INIT-SIPI-SIPI method for initial configuration in + // order to obtain the APIC ID. // - SendInitSipiSipiAllExcludingSelf ((UINT32) ExchangeInfo->BufferStart); + if (CpuMpData->SevSnpIsEnabled && (CpuMpData->InitFlag != ApInitConfig)) { + SevSnpCreateAP (CpuMpData, -1); + } else { + SendInitSipiSipiAllExcludingSelf ((UINT32)ExchangeInfo->BufferStart); + } } + if (CpuMpData->InitFlag == ApInitConfig) { if (PcdGet32 (PcdCpuBootLogicalProcessorNumber) > 0) { // @@ -1154,7 +1297,7 @@ WakeUpAP ( ); while (CpuMpData->MpCpuExchangeInfo->NumApsExecuting != 0) { - CpuPause(); + CpuPause (); } } } else { @@ -1169,22 +1312,37 @@ 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; - SendInitSipiSipi ( - CpuInfoInHob[ProcessorNumber].ApicId, - (UINT32) ExchangeInfo->BufferStart - ); + CpuInfoInHob = (CPU_INFO_IN_HOB *)(UINTN)CpuMpData->CpuInfoInHob; + + // + // For SEV-ES and SEV-SNP, 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); + } + + if (CpuMpData->SevSnpIsEnabled && (CpuMpData->InitFlag != ApInitConfig)) { + SevSnpCreateAP (CpuMpData, (INTN)ProcessorNumber); + } else { + SendInitSipiSipi ( + CpuInfoInHob[ProcessorNumber].ApicId, + (UINT32)ExchangeInfo->BufferStart + ); + } } + // // Wait specified AP waken up // @@ -1224,8 +1382,8 @@ CalculateTimeout ( OUT UINT64 *CurrentTime ) { - UINT64 TimeoutInSeconds; - UINT64 TimestampCounterFreq; + UINT64 TimeoutInSeconds; + UINT64 TimestampCounterFreq; // // Read the current value of the performance counter @@ -1310,25 +1468,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; } @@ -1342,9 +1505,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 ) { // @@ -1355,7 +1518,7 @@ TimedWaitForApFinish ( return; } - CpuMpData->TotalTime = 0; + CpuMpData->TotalTime = 0; CpuMpData->ExpectedTime = CalculateTimeout ( TimeLimit, &CpuMpData->CurrentTime @@ -1365,7 +1528,8 @@ TimedWaitForApFinish ( &CpuMpData->CurrentTime, &CpuMpData->TotalTime, CpuMpData->ExpectedTime - )) { + )) + { CpuPause (); } @@ -1394,10 +1558,10 @@ TimedWaitForApFinish ( **/ VOID ResetProcessorToIdleState ( - IN UINTN ProcessorNumber + IN UINTN ProcessorNumber ) { - CPU_MP_DATA *CpuMpData; + CPU_MP_DATA *CpuMpData; CpuMpData = GetCpuMpData (); @@ -1406,6 +1570,7 @@ ResetProcessorToIdleState ( while (CpuMpData->FinishedCount < 1) { CpuPause (); } + CpuMpData->InitFlag = ApInitDone; SetApState (&CpuMpData->CpuData[ProcessorNumber], CpuStateIdle); @@ -1424,11 +1589,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 (); @@ -1455,11 +1620,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]; @@ -1472,10 +1637,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 { @@ -1486,6 +1652,7 @@ CheckThisAP ( if (CpuData->Finished != NULL) { *(CpuData->Finished) = FALSE; } + // // Reset failed AP to idle state // @@ -1494,6 +1661,7 @@ CheckThisAP ( return EFI_TIMEOUT; } } + return EFI_NOT_READY; } @@ -1512,12 +1680,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 (); @@ -1537,10 +1705,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. @@ -1552,12 +1720,12 @@ CheckAllAPs ( WakeUpAP ( CpuMpData, FALSE, - (UINT32) NextProcessorNumber, + (UINT32)NextProcessorNumber, CpuMpData->Procedure, CpuMpData->ProcArguments, TRUE ); - } + } } } } @@ -1573,18 +1741,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++) { @@ -1602,11 +1773,14 @@ CheckAllAPs ( } } } + if (CpuMpData->FailedCpuList != NULL) { (*CpuMpData->FailedCpuList)[ListIndex] = END_OF_CPU_LIST; } + return EFI_TIMEOUT; } + return EFI_NOT_READY; } @@ -1643,31 +1817,41 @@ MpInitLibInitialize ( UINT8 ApLoopMode; UINT8 *MonitorBuffer; UINTN Index; - UINTN ApResetVectorSize; + UINTN ApResetVectorSizeBelow1Mb; + UINTN ApResetVectorSizeAbove1Mb; UINTN BackupBufferAddr; UINTN ApIdtBase; 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); + GetApResetVectorSize (&AddressMap, &ApResetVectorSizeBelow1Mb, &ApResetVectorSizeAbove1Mb); + ApStackSize = PcdGet32 (PcdCpuApStackSize); + // + // ApStackSize must be power of 2 + // + ASSERT ((ApStackSize & (ApStackSize - 1)) == 0); + ApLoopMode = GetApLoopMode (&MonitorFilterSize); // // Save BSP's Control registers for APs. // SaveVolatileRegisters (&VolatileRegisters); - BufferSize = ApStackSize * MaxLogicalProcessorNumber; + BufferSize = ApStackSize * MaxLogicalProcessorNumber; + // + // Allocate extra ApStackSize to let AP stack align on ApStackSize bounday + // + BufferSize += ApStackSize; BufferSize += MonitorFilterSize * MaxLogicalProcessorNumber; - BufferSize += ApResetVectorSize; + BufferSize += ApResetVectorSizeBelow1Mb; BufferSize = ALIGN_VALUE (BufferSize, 8); BufferSize += VolatileRegisters.Idtr.Limit + 1; BufferSize += sizeof (CPU_MP_DATA); @@ -1675,13 +1859,13 @@ MpInitLibInitialize ( MpBuffer = AllocatePages (EFI_SIZE_TO_PAGES (BufferSize)); ASSERT (MpBuffer != NULL); ZeroMem (MpBuffer, BufferSize); - Buffer = (UINTN) MpBuffer; + Buffer = ALIGN_VALUE ((UINTN)MpBuffer, ApStackSize); // - // The layout of the Buffer is as below: + // The layout of the Buffer is as below (lower address on top): // - // +--------------------+ <-- Buffer - // AP Stacks (N) + // +--------------------+ <-- Buffer (Pointer of CpuMpData is stored in the top of each AP's stack.) + // AP Stacks (N) (StackTop = (RSP + ApStackSize) & ~ApStackSize)) // +--------------------+ <-- MonitorBuffer // AP Monitor Filters (N) // +--------------------+ <-- BackupBufferAddr (CpuMpData->BackupBuffer) @@ -1689,7 +1873,7 @@ MpInitLibInitialize ( // +--------------------+ // Padding // +--------------------+ <-- ApIdtBase (8-byte boundary) - // AP IDT All APs share one separate IDT. So AP can get address of CPU_MP_DATA from IDT Base. + // AP IDT All APs share one separate IDT. // +--------------------+ <-- CpuMpData // CPU_MP_DATA // +--------------------+ <-- CpuMpData->CpuData @@ -1698,28 +1882,40 @@ 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 + ApResetVectorSizeBelow1Mb, 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->BackupBufferSize = ApResetVectorSizeBelow1Mb; + 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 = ConfidentialComputingGuestHas (CCAttrAmdSevEs); + CpuMpData->SevSnpIsEnabled = ConfidentialComputingGuestHas (CCAttrAmdSevSnp); + CpuMpData->SevEsAPBuffer = (UINTN)-1; + CpuMpData->GhcbBase = PcdGet64 (PcdGhcbBase); + CpuMpData->UseSevEsAPMethod = CpuMpData->SevEsIsEnabled && !CpuMpData->SevSnpIsEnabled; + + if (CpuMpData->SevSnpIsEnabled) { + ASSERT ((PcdGet64 (PcdGhcbHypervisorFeatures) & GHCB_HV_FEATURES_SNP_AP_CREATE) == GHCB_HV_FEATURES_SNP_AP_CREATE); + } // // Make sure no memory usage outside of the allocated buffer. + // (ApStackSize - (Buffer - (UINTN)MpBuffer)) is the redundant caused by alignment // - ASSERT ((CpuMpData->CpuInfoInHob + sizeof (CPU_INFO_IN_HOB) * MaxLogicalProcessorNumber) == - Buffer + BufferSize); + ASSERT ( + (CpuMpData->CpuInfoInHob + sizeof (CPU_INFO_IN_HOB) * MaxLogicalProcessorNumber) == + (UINTN)MpBuffer + BufferSize - (ApStackSize - Buffer + (UINTN)MpBuffer) + ); // // Duplicate BSP's IDT to APs. @@ -1755,6 +1951,20 @@ MpInitLibInitialize ( CpuMpData->CpuData[Index].StartupApSignal = (UINT32 *)(MonitorBuffer + MonitorFilterSize * Index); } + + // + // Copy all 32-bit code and 64-bit code into memory with type of + // EfiBootServicesCode to avoid page fault if NX memory protection is enabled. + // + CpuMpData->WakeupBufferHigh = AllocateCodeBuffer (ApResetVectorSizeAbove1Mb); + CopyMem ( + (VOID *)CpuMpData->WakeupBufferHigh, + CpuMpData->AddressMap.RendezvousFunnelAddress + + CpuMpData->AddressMap.ModeTransitionOffset, + ApResetVectorSizeAbove1Mb + ); + DEBUG ((DEBUG_INFO, "AP Vector: non-16-bit = %p/%x\n", CpuMpData->WakeupBufferHigh, ApResetVectorSizeAbove1Mb)); + // // Enable the local APIC for Virtual Wire Mode. // @@ -1772,22 +1982,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 @@ -1808,7 +2019,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 @@ -1816,12 +2035,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 // @@ -1855,12 +2109,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 @@ -1878,14 +2139,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 { @@ -1902,6 +2165,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; } @@ -1922,8 +2197,8 @@ MpInitLibGetProcessorInfo ( **/ EFI_STATUS SwitchBSPWorker ( - IN UINTN ProcessorNumber, - IN BOOLEAN EnableOldBSP + IN UINTN ProcessorNumber, + IN BOOLEAN EnableOldBSP ) { CPU_MP_DATA *CpuMpData; @@ -1996,7 +2271,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); @@ -2005,12 +2280,17 @@ SwitchBSPWorker ( // WakeUpAP (CpuMpData, FALSE, ProcessorNumber, FutureBSPProc, CpuMpData, TRUE); + // + // Save and restore volatile registers when switch BSP + // + SaveVolatileRegisters (&CpuMpData->BSPInfo.VolatileRegisters); AsmExchangeRole (&CpuMpData->BSPInfo, &CpuMpData->APInfo); + RestoreVolatileRegisters (&CpuMpData->BSPInfo.VolatileRegisters, FALSE); // // 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 (); @@ -2031,10 +2311,11 @@ SwitchBSPWorker ( } else { SetApState (&CpuMpData->CpuData[CallerNumber], CpuStateIdle); } + // // Save new BSP number // - CpuMpData->BspNumber = (UINT32) ProcessorNumber; + CpuMpData->BspNumber = (UINT32)ProcessorNumber; // // Restore interrupt state. @@ -2064,13 +2345,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 (); @@ -2098,7 +2379,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; @@ -2123,10 +2404,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; @@ -2160,15 +2441,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 (); @@ -2188,13 +2469,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; } @@ -2202,7 +2484,6 @@ MpInitLibGetNumberOfProcessors ( return EFI_SUCCESS; } - /** Worker function to execute a caller provided function on all enabled APs. @@ -2237,23 +2518,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 (); @@ -2261,7 +2542,7 @@ StartupAllCPUsWorker ( *FailedCpuList = NULL; } - if (CpuMpData->CpuCount == 1 && ExcludeBsp) { + if ((CpuMpData->CpuCount == 1) && ExcludeBsp) { return EFI_NOT_STARTED; } @@ -2313,7 +2594,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) { @@ -2335,8 +2616,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); @@ -2345,6 +2626,7 @@ StartupAllCPUsWorker ( if (ProcessorNumber == CallerNumber) { continue; } + if (CpuMpData->CpuData[ProcessorNumber].Waiting) { WakeUpAP (CpuMpData, FALSE, ProcessorNumber, Procedure, ProcedureArgument, TRUE); break; @@ -2394,18 +2676,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 (); @@ -2459,7 +2741,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); @@ -2491,16 +2773,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; } @@ -2533,9 +2816,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 ( @@ -2548,3 +2831,70 @@ MpInitLibStartupAllCPUs ( NULL ); } + +/** + The function check if the specified Attr is set. + + @param[in] CurrentAttr The current attribute. + @param[in] Attr The attribute to check. + + @retval TRUE The specified Attr is set. + @retval FALSE The specified Attr is not set. + +**/ +STATIC +BOOLEAN +AmdMemEncryptionAttrCheck ( + IN UINT64 CurrentAttr, + IN CONFIDENTIAL_COMPUTING_GUEST_ATTR Attr + ) +{ + switch (Attr) { + case CCAttrAmdSev: + // + // SEV is automatically enabled if SEV-ES or SEV-SNP is active. + // + return CurrentAttr >= CCAttrAmdSev; + case CCAttrAmdSevEs: + // + // SEV-ES is automatically enabled if SEV-SNP is active. + // + return CurrentAttr >= CCAttrAmdSevEs; + case CCAttrAmdSevSnp: + return CurrentAttr == CCAttrAmdSevSnp; + default: + return FALSE; + } +} + +/** + Check if the specified confidential computing attribute is active. + + @param[in] Attr The attribute to check. + + @retval TRUE The specified Attr is active. + @retval FALSE The specified Attr is not active. + +**/ +BOOLEAN +EFIAPI +ConfidentialComputingGuestHas ( + IN CONFIDENTIAL_COMPUTING_GUEST_ATTR Attr + ) +{ + UINT64 CurrentAttr; + + // + // Get the current CC attribute. + // + CurrentAttr = PcdGet64 (PcdConfidentialComputingGuestAttr); + + // + // If attr is for the AMD group then call AMD specific checks. + // + if (((RShiftU64 (CurrentAttr, 8)) & 0xff) == 1) { + return AmdMemEncryptionAttrCheck (CurrentAttr, Attr); + } + + return (CurrentAttr == Attr); +}