X-Git-Url: https://git.proxmox.com/?a=blobdiff_plain;f=UefiCpuPkg%2FLibrary%2FMpInitLib%2FMpLib.c;h=64fddb497e1e7f59c93a95e56690cfe7719c408d;hb=b95908e04317a2b1e3641845ba36f673d5aebada;hp=ada3969b683c5e5ad5acabe5946d796b16a4d2dd;hpb=052aa07da46d96d7069d12f28fc31deb68a6d75e;p=mirror_edk2.git
diff --git a/UefiCpuPkg/Library/MpInitLib/MpLib.c b/UefiCpuPkg/Library/MpInitLib/MpLib.c
index ada3969b68..64fddb497e 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 - 2021, Intel Corporation. All rights reserved.
Copyright (c) 2020, AMD Inc. All rights reserved.
SPDX-License-Identifier: BSD-2-Clause-Patent
@@ -9,32 +9,11 @@
**/
#include "MpLib.h"
+#include
+#include
+#include
-EFI_GUID mCpuInitMpLibHobGuid = CPU_INIT_MP_LIB_HOB_GUID;
-
-
-/**
- Determine if the standard CPU signature is "AuthenticAMD".
-
- @retval TRUE The CPU signature matches.
- @retval FALSE The CPU signature does not match.
-
-**/
-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);
-}
+EFI_GUID mCpuInitMpLibHobGuid = CPU_INIT_MP_LIB_HOB_GUID;
/**
The function will check if BSP Execute Disable is enabled.
@@ -58,7 +37,7 @@ IsBspExecuteDisableEnabled (
BOOLEAN Enabled;
IA32_CR0 Cr0;
- Enabled = FALSE;
+ Enabled = FALSE;
Cr0.UintN = AsmReadCr0 ();
if (Cr0.Bits.PG != 0) {
//
@@ -98,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);
}
@@ -116,7 +95,7 @@ FutureBSPProc (
**/
CPU_STATE
GetApState (
- IN CPU_AP_DATA *CpuData
+ IN CPU_AP_DATA *CpuData
)
{
return CpuData->State;
@@ -130,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);
@@ -146,7 +125,7 @@ SetApState (
**/
VOID
SaveLocalApicTimerSetting (
- IN CPU_MP_DATA *CpuMpData
+ IN CPU_MP_DATA *CpuMpData
)
{
//
@@ -168,7 +147,7 @@ SaveLocalApicTimerSetting (
**/
VOID
SyncLocalApicTimerSetting (
- IN CPU_MP_DATA *CpuMpData
+ IN CPU_MP_DATA *CpuMpData
)
{
//
@@ -193,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 ();
@@ -230,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);
@@ -259,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) {
@@ -281,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;
@@ -296,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);
@@ -314,6 +294,14 @@ GetApLoopMode (
//
ApLoopMode = ApInHltLoop;
}
+
+ if (ConfidentialComputingGuestHas (CCAttrAmdSevEs)) {
+ //
+ // For 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 +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;
@@ -367,6 +355,7 @@ SortApicId (
ApicId = CpuInfoInHob[Index2].ApicId;
}
}
+
if (Index3 != Index1) {
CopyMem (&CpuInfo, &CpuInfoInHob[Index3], sizeof (CPU_INFO_IN_HOB));
CopyMem (
@@ -379,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;
@@ -392,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;
}
}
@@ -428,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
@@ -452,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;
@@ -484,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
@@ -497,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
@@ -518,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;
@@ -539,6 +526,7 @@ CollectProcessorCount (
while (CpuMpData->FinishedCount < (CpuMpData->CpuCount - 1)) {
CpuPause ();
}
+
//
// Enable x2APIC on BSP
//
@@ -550,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
@@ -572,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;
@@ -594,7 +583,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 +595,7 @@ InitializeApData (
NULL
);
- InitializeSpinLock(&CpuMpData->CpuData[ProcessorNumber].ApLock);
+ InitializeSpinLock (&CpuMpData->CpuData[ProcessorNumber].ApLock);
SetApState (&CpuMpData->CpuData[ProcessorNumber], CpuStateIdle);
}
@@ -619,19 +608,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
@@ -652,16 +641,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.
@@ -670,8 +655,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,7 +665,7 @@ ApWakeupFunction (
//
ApStartupSignalBuffer = CpuMpData->CpuData[ProcessorNumber].StartupApSignal;
InterlockedCompareExchange32 (
- (UINT32 *) ApStartupSignalBuffer,
+ (UINT32 *)ApStartupSignalBuffer,
WAKEUP_AP_SIGNAL,
0
);
@@ -697,7 +680,7 @@ ApWakeupFunction (
// different IDT shared by all APs.
//
RestoreVolatileRegisters (&CpuMpData->CpuData[0].VolatileRegisters, FALSE);
- } else {
+ } else {
if (CpuMpData->ApLoopMode == ApInHltLoop) {
//
// Restore AP's volatile registers saved before AP is halted
@@ -715,7 +698,7 @@ ApWakeupFunction (
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);
//
@@ -726,19 +709,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,
@@ -756,39 +740,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->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) {
+ 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.
@@ -824,7 +829,7 @@ ApWakeupFunction (
**/
VOID
WaitApWakeup (
- IN volatile UINT32 *ApStartupSignalBuffer
+ IN volatile UINT32 *ApStartupSignalBuffer
)
{
//
@@ -832,10 +837,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 ();
}
}
@@ -848,31 +854,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 ();
@@ -887,29 +892,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);
}
//
@@ -917,7 +926,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,
@@ -929,12 +939,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;
}
@@ -948,9 +958,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
);
/**
@@ -959,19 +969,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
);
}
@@ -981,17 +992,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.
@@ -999,23 +1032,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 (ConfidentialComputingGuestHas (CCAttrAmdSevEs)) {
+ //
+ // 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);
}
@@ -1026,10 +1097,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->SevEsIsEnabled) {
+ RestoreWakeupBuffer (CpuMpData);
+ }
}
/**
@@ -1045,27 +1124,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);
}
@@ -1089,24 +1170,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) {
//
@@ -1167,7 +1259,7 @@ WakeUpAP (
);
while (CpuMpData->MpCpuExchangeInfo->NumApsExecuting != 0) {
- CpuPause();
+ CpuPause ();
}
}
} else {
@@ -1182,22 +1274,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
//
@@ -1237,8 +1340,8 @@ CalculateTimeout (
OUT UINT64 *CurrentTime
)
{
- UINT64 TimeoutInSeconds;
- UINT64 TimestampCounterFreq;
+ UINT64 TimeoutInSeconds;
+ UINT64 TimestampCounterFreq;
//
// Read the current value of the performance counter
@@ -1323,25 +1426,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;
}
@@ -1355,9 +1463,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
)
{
//
@@ -1368,7 +1476,7 @@ TimedWaitForApFinish (
return;
}
- CpuMpData->TotalTime = 0;
+ CpuMpData->TotalTime = 0;
CpuMpData->ExpectedTime = CalculateTimeout (
TimeLimit,
&CpuMpData->CurrentTime
@@ -1378,7 +1486,8 @@ TimedWaitForApFinish (
&CpuMpData->CurrentTime,
&CpuMpData->TotalTime,
CpuMpData->ExpectedTime
- )) {
+ ))
+ {
CpuPause ();
}
@@ -1407,10 +1516,10 @@ TimedWaitForApFinish (
**/
VOID
ResetProcessorToIdleState (
- IN UINTN ProcessorNumber
+ IN UINTN ProcessorNumber
)
{
- CPU_MP_DATA *CpuMpData;
+ CPU_MP_DATA *CpuMpData;
CpuMpData = GetCpuMpData ();
@@ -1419,6 +1528,7 @@ ResetProcessorToIdleState (
while (CpuMpData->FinishedCount < 1) {
CpuPause ();
}
+
CpuMpData->InitFlag = ApInitDone;
SetApState (&CpuMpData->CpuData[ProcessorNumber], CpuStateIdle);
@@ -1437,11 +1547,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 ();
@@ -1468,11 +1578,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];
@@ -1485,10 +1595,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 {
@@ -1499,6 +1610,7 @@ CheckThisAP (
if (CpuData->Finished != NULL) {
*(CpuData->Finished) = FALSE;
}
+
//
// Reset failed AP to idle state
//
@@ -1507,6 +1619,7 @@ CheckThisAP (
return EFI_TIMEOUT;
}
}
+
return EFI_NOT_READY;
}
@@ -1525,12 +1638,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 ();
@@ -1550,10 +1663,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.
@@ -1565,12 +1678,12 @@ CheckAllAPs (
WakeUpAP (
CpuMpData,
FALSE,
- (UINT32) NextProcessorNumber,
+ (UINT32)NextProcessorNumber,
CpuMpData->Procedure,
CpuMpData->ProcArguments,
TRUE
);
- }
+ }
}
}
}
@@ -1586,18 +1699,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++) {
@@ -1615,11 +1731,14 @@ CheckAllAPs (
}
}
}
+
if (CpuMpData->FailedCpuList != NULL) {
(*CpuMpData->FailedCpuList)[ListIndex] = END_OF_CPU_LIST;
}
+
return EFI_TIMEOUT;
}
+
return EFI_NOT_READY;
}
@@ -1662,16 +1781,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.
@@ -1688,7 +1808,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:
@@ -1711,28 +1831,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 = ConfidentialComputingGuestHas (CCAttrAmdSevEs);
+ 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.
@@ -1768,6 +1893,7 @@ MpInitLibInitialize (
CpuMpData->CpuData[Index].StartupApSignal =
(UINT32 *)(MonitorBuffer + MonitorFilterSize * Index);
}
+
//
// Enable the local APIC for Virtual Wire Mode.
//
@@ -1785,13 +1911,14 @@ 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;
}
}
@@ -1799,7 +1926,8 @@ MpInitLibInitialize (
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
@@ -1820,7 +1948,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
@@ -1828,12 +1964,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
//
@@ -1867,12 +2038,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
@@ -1890,14 +2068,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 {
@@ -1914,6 +2094,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;
}
@@ -1934,8 +2126,8 @@ MpInitLibGetProcessorInfo (
**/
EFI_STATUS
SwitchBSPWorker (
- IN UINTN ProcessorNumber,
- IN BOOLEAN EnableOldBSP
+ IN UINTN ProcessorNumber,
+ IN BOOLEAN EnableOldBSP
)
{
CPU_MP_DATA *CpuMpData;
@@ -2008,7 +2200,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);
@@ -2022,7 +2214,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 ();
@@ -2043,10 +2235,11 @@ SwitchBSPWorker (
} else {
SetApState (&CpuMpData->CpuData[CallerNumber], CpuStateIdle);
}
+
//
// Save new BSP number
//
- CpuMpData->BspNumber = (UINT32) ProcessorNumber;
+ CpuMpData->BspNumber = (UINT32)ProcessorNumber;
//
// Restore interrupt state.
@@ -2076,13 +2269,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 ();
@@ -2110,7 +2303,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;
@@ -2135,10 +2328,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;
@@ -2172,15 +2365,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 ();
@@ -2200,13 +2393,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;
}
@@ -2214,7 +2408,6 @@ MpInitLibGetNumberOfProcessors (
return EFI_SUCCESS;
}
-
/**
Worker function to execute a caller provided function on all enabled APs.
@@ -2249,23 +2442,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 ();
@@ -2273,7 +2466,7 @@ StartupAllCPUsWorker (
*FailedCpuList = NULL;
}
- if (CpuMpData->CpuCount == 1 && ExcludeBsp) {
+ if ((CpuMpData->CpuCount == 1) && ExcludeBsp) {
return EFI_NOT_STARTED;
}
@@ -2325,7 +2518,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) {
@@ -2347,8 +2540,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);
@@ -2357,6 +2550,7 @@ StartupAllCPUsWorker (
if (ProcessorNumber == CallerNumber) {
continue;
}
+
if (CpuMpData->CpuData[ProcessorNumber].Waiting) {
WakeUpAP (CpuMpData, FALSE, ProcessorNumber, Procedure, ProcedureArgument, TRUE);
break;
@@ -2406,18 +2600,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 ();
@@ -2471,7 +2665,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);
@@ -2503,16 +2697,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;
}
@@ -2545,9 +2740,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 (
@@ -2560,3 +2755,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);
+}