X-Git-Url: https://git.proxmox.com/?a=blobdiff_plain;f=UefiCpuPkg%2FLibrary%2FMpInitLib%2FMpLib.c;h=8d1f24370a3636e6e936b5df1a4a42fddc5cdbee;hb=HEAD;hp=dc2a54aa31e894cac939e2b22ba2bc9252f4a11d;hpb=6933c78e4dc20cb5a633fa917e57158a6646e62e;p=mirror_edk2.git
diff --git a/UefiCpuPkg/Library/MpInitLib/MpLib.c b/UefiCpuPkg/Library/MpInitLib/MpLib.c
index dc2a54aa31..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 - 2021, 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,12 +9,34 @@
**/
#include "MpLib.h"
-#include
+#include
#include
#include
-EFI_GUID mCpuInitMpLibHobGuid = CPU_INIT_MP_LIB_HOB_GUID;
+EFI_GUID mCpuInitMpLibHobGuid = CPU_INIT_MP_LIB_HOB_GUID;
+/**
+ Save the volatile registers required to be restored following INIT IPI.
+
+ @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
+**/
+VOID
+RestoreVolatileRegisters (
+ IN CPU_VOLATILE_REGISTERS *VolatileRegisters,
+ IN BOOLEAN IsRestoreDr
+ );
/**
The function will check if BSP Execute Disable is enabled.
@@ -38,7 +60,7 @@ IsBspExecuteDisableEnabled (
BOOLEAN Enabled;
IA32_CR0 Cr0;
- Enabled = FALSE;
+ Enabled = FALSE;
Cr0.UintN = AsmReadCr0 ();
if (Cr0.Bits.PG != 0) {
//
@@ -78,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);
}
/**
@@ -96,7 +123,7 @@ FutureBSPProc (
**/
CPU_STATE
GetApState (
- IN CPU_AP_DATA *CpuData
+ IN CPU_AP_DATA *CpuData
)
{
return CpuData->State;
@@ -110,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);
@@ -126,7 +153,7 @@ SetApState (
**/
VOID
SaveLocalApicTimerSetting (
- IN CPU_MP_DATA *CpuMpData
+ IN CPU_MP_DATA *CpuMpData
)
{
//
@@ -148,7 +175,7 @@ SaveLocalApicTimerSetting (
**/
VOID
SyncLocalApicTimerSetting (
- IN CPU_MP_DATA *CpuMpData
+ IN CPU_MP_DATA *CpuMpData
)
{
//
@@ -173,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 ();
@@ -210,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);
@@ -239,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) {
@@ -261,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;
@@ -276,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);
@@ -295,10 +323,12 @@ GetApLoopMode (
ApLoopMode = ApInHltLoop;
}
- if (PcdGetBool (PcdSevEsIsEnabled)) {
+ if (ConfidentialComputingGuestHas (CCAttrAmdSevEs) &&
+ !ConfidentialComputingGuestHas (CCAttrAmdSevSnp))
+ {
//
- // For SEV-ES, force AP in Hlt-loop mode in order to use the GHCB
- // protocol for starting APs
+ // 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;
}
@@ -328,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;
@@ -355,6 +385,7 @@ SortApicId (
ApicId = CpuInfoInHob[Index2].ApicId;
}
}
+
if (Index3 != Index1) {
CopyMem (&CpuInfo, &CpuInfoInHob[Index3], sizeof (CPU_INFO_IN_HOB));
CopyMem (
@@ -367,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;
@@ -380,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;
}
}
@@ -416,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
@@ -440,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;
@@ -472,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
@@ -504,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;
@@ -525,6 +556,7 @@ CollectProcessorCount (
while (CpuMpData->FinishedCount < (CpuMpData->CpuCount - 1)) {
CpuPause ();
}
+
//
// Enable x2APIC on BSP
//
@@ -536,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
@@ -558,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;
@@ -580,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;
}
@@ -592,121 +632,10 @@ InitializeApData (
NULL
);
- InitializeSpinLock(&CpuMpData->CpuData[ProcessorNumber].ApLock);
+ InitializeSpinLock (&CpuMpData->CpuData[ProcessorNumber].ApLock);
SetApState (&CpuMpData->CpuData[ProcessorNumber], CpuStateIdle);
}
-/**
- Get Protected mode code segment with 16-bit default addressing
- from current GDT table.
-
- @return Protected mode 16-bit code segment value.
-**/
-STATIC
-UINT16
-GetProtectedMode16CS (
- VOID
- )
-{
- IA32_DESCRIPTOR GdtrDesc;
- IA32_SEGMENT_DESCRIPTOR *GdtEntry;
- UINTN GdtEntryCount;
- UINT16 Index;
-
- Index = (UINT16) -1;
- AsmReadGdtr (&GdtrDesc);
- GdtEntryCount = (GdtrDesc.Limit + 1) / sizeof (IA32_SEGMENT_DESCRIPTOR);
- GdtEntry = (IA32_SEGMENT_DESCRIPTOR *) GdtrDesc.Base;
- for (Index = 0; Index < GdtEntryCount; Index++) {
- if (GdtEntry->Bits.L == 0 &&
- GdtEntry->Bits.DB == 0 &&
- GdtEntry->Bits.Type > 8) {
- break;
- }
- GdtEntry++;
- }
- ASSERT (Index != GdtEntryCount);
- return Index * 8;
-}
-
-/**
- Get Protected mode code segment with 32-bit default addressing
- from current GDT table.
-
- @return Protected mode 32-bit code segment value.
-**/
-STATIC
-UINT16
-GetProtectedMode32CS (
- VOID
- )
-{
- IA32_DESCRIPTOR GdtrDesc;
- IA32_SEGMENT_DESCRIPTOR *GdtEntry;
- UINTN GdtEntryCount;
- UINT16 Index;
-
- Index = (UINT16) -1;
- AsmReadGdtr (&GdtrDesc);
- GdtEntryCount = (GdtrDesc.Limit + 1) / sizeof (IA32_SEGMENT_DESCRIPTOR);
- GdtEntry = (IA32_SEGMENT_DESCRIPTOR *) GdtrDesc.Base;
- for (Index = 0; Index < GdtEntryCount; Index++) {
- if (GdtEntry->Bits.L == 0 &&
- GdtEntry->Bits.DB == 1 &&
- GdtEntry->Bits.Type > 8) {
- break;
- }
- GdtEntry++;
- }
- ASSERT (Index != GdtEntryCount);
- return Index * 8;
-}
-
-/**
- Reset an AP when in SEV-ES mode.
-
- If successful, this function never returns.
-
- @param[in] Ghcb Pointer to the GHCB
- @param[in] CpuMpData Pointer to CPU MP Data
-
-**/
-STATIC
-VOID
-MpInitLibSevEsAPReset (
- IN GHCB *Ghcb,
- IN CPU_MP_DATA *CpuMpData
- )
-{
- EFI_STATUS Status;
- UINTN ProcessorNumber;
- UINT16 Code16, Code32;
- AP_RESET *APResetFn;
- UINTN BufferStart;
- UINTN StackStart;
-
- Status = GetProcessorNumber (CpuMpData, &ProcessorNumber);
- ASSERT_EFI_ERROR (Status);
-
- Code16 = GetProtectedMode16CS ();
- Code32 = GetProtectedMode32CS ();
-
- if (CpuMpData->WakeupBufferHigh != 0) {
- APResetFn = (AP_RESET *) (CpuMpData->WakeupBufferHigh + CpuMpData->AddressMap.SwitchToRealNoNxOffset);
- } else {
- APResetFn = (AP_RESET *) (CpuMpData->MpCpuExchangeInfo->BufferStart + CpuMpData->AddressMap.SwitchToRealOffset);
- }
-
- BufferStart = CpuMpData->MpCpuExchangeInfo->BufferStart;
- StackStart = CpuMpData->SevEsAPResetStackStart -
- (AP_RESET_STACK_SIZE * ProcessorNumber);
-
- //
- // This call never returns.
- //
- APResetFn (BufferStart, Code16, Code32, StackStart);
-}
-
/**
This function will be called from AP reset code if BSP uses WakeUpAP.
@@ -716,19 +645,20 @@ MpInitLibSevEsAPReset (
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
@@ -753,8 +683,10 @@ ApWakeupFunction (
//
// 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.
@@ -773,7 +705,7 @@ ApWakeupFunction (
//
ApStartupSignalBuffer = CpuMpData->CpuData[ProcessorNumber].StartupApSignal;
InterlockedCompareExchange32 (
- (UINT32 *) ApStartupSignalBuffer,
+ (UINT32 *)ApStartupSignalBuffer,
WAKEUP_AP_SIGNAL,
0
);
@@ -788,7 +720,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
@@ -806,7 +738,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);
//
@@ -817,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,
@@ -847,6 +780,7 @@ ApWakeupFunction (
}
}
}
+
SetApState (&CpuMpData->CpuData[ProcessorNumber], CpuStateFinished);
}
}
@@ -861,7 +795,7 @@ ApWakeupFunction (
//
// AP finished executing C code
//
- InterlockedIncrement ((UINT32 *) &CpuMpData->FinishedCount);
+ InterlockedIncrement ((UINT32 *)&CpuMpData->FinishedCount);
if (CpuMpData->InitFlag == ApInitConfig) {
//
@@ -869,8 +803,8 @@ ApWakeupFunction (
// 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);
+ if (!CpuMpData->UseSevEsAPMethod) {
+ InterlockedDecrement ((UINT32 *)&CpuMpData->MpCpuExchangeInfo->NumApsExecuting);
}
}
@@ -883,61 +817,23 @@ ApWakeupFunction (
//
while (TRUE) {
DisableInterrupts ();
- if (CpuMpData->SevEsIsEnabled) {
- MSR_SEV_ES_GHCB_REGISTER Msr;
- GHCB *Ghcb;
- UINT64 Status;
- BOOLEAN DoDecrement;
- BOOLEAN InterruptState;
-
- DoDecrement = (BOOLEAN) (CpuMpData->InitFlag == ApInitConfig);
-
- while (TRUE) {
- Msr.GhcbPhysicalAddress = AsmReadMsr64 (MSR_SEV_ES_GHCB);
- Ghcb = Msr.Ghcb;
-
- VmgInit (Ghcb, &InterruptState);
-
- if (DoDecrement) {
- DoDecrement = FALSE;
-
- //
- // Perform the delayed decrement just before issuing the first
- // VMGEXIT with AP_RESET_HOLD.
- //
- InterlockedDecrement ((UINT32 *) &CpuMpData->MpCpuExchangeInfo->NumApsExecuting);
- }
-
- Status = VmgExit (Ghcb, SVM_EXIT_AP_RESET_HOLD, 0, 0);
- if ((Status == 0) && (Ghcb->SaveArea.SwExitInfo2 != 0)) {
- VmgDone (Ghcb, InterruptState);
- break;
- }
-
- VmgDone (Ghcb, InterruptState);
- }
-
- //
- // Awakened in a new phase? Use the new CpuMpData
- //
- if (CpuMpData->NewCpuMpData != NULL) {
- CpuMpData = CpuMpData->NewCpuMpData;
- }
-
- MpInitLibSevEsAPReset (Ghcb, CpuMpData);
+ 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.
@@ -973,7 +869,7 @@ ApWakeupFunction (
**/
VOID
WaitApWakeup (
- IN volatile UINT32 *ApStartupSignalBuffer
+ IN volatile UINT32 *ApStartupSignalBuffer
)
{
//
@@ -981,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.
@@ -997,30 +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->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 ();
@@ -1035,58 +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->GhcbBase = (UINTN) CpuMpData->GhcbBase;
+ 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.SwitchToRealSize -
- 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;
}
@@ -1100,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
);
/**
@@ -1111,20 +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 +
- CpuMpData->AddressMap.SwitchToRealSize
+ (VOID *)CpuMpData->WakeupBuffer,
+ (VOID *)CpuMpData->AddressMap.RendezvousFunnelAddress,
+ CpuMpData->BackupBufferSize - sizeof (MP_CPU_EXCHANGE_INFO)
);
}
@@ -1134,83 +1044,81 @@ 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.
+ Allocate reset vector buffer.
- @return Total amount of memory required for the AP reset area
+ @param[in, out] CpuMpData The pointer to CPU MP Data structure.
**/
-STATIC
-UINTN
-GetApResetVectorSize (
- IN MP_ASSEMBLY_ADDRESS_MAP *AddressMap
+VOID
+AllocateResetVectorBelow1Mb (
+ IN OUT CPU_MP_DATA *CpuMpData
)
{
- UINTN Size;
+ UINTN ApResetStackSize;
- Size = AddressMap->RendezvousFunnelSize +
- AddressMap->SwitchToRealSize +
- sizeof (MP_CPU_EXCHANGE_INFO);
-
- //
- // The AP reset stack is only used by SEV-ES guests. Do not add to the
- // allocation if SEV-ES is not enabled.
- //
- if (PcdGetBool (PcdSevEsIsEnabled)) {
+ 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)
+ ));
//
- // Stack location is based on APIC ID, so use the total number of
- // processors for calculating the total stack area.
+ // 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.
//
- Size += AP_RESET_STACK_SIZE * PcdGet32 (PcdCpuMaxLogicalProcessorNumber);
-
- Size = ALIGN_VALUE (Size, CPU_STACK_ALIGNMENT);
- }
+ 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));
- return Size;
-}
+ //
+ // 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);
-/**
- Allocate reset vector buffer.
+ //
+ // 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"
+ ));
- @param[in, out] CpuMpData The pointer to CPU MP Data structure.
-**/
-VOID
-AllocateResetVector (
- IN OUT CPU_MP_DATA *CpuMpData
- )
-{
- UINTN ApResetVectorSize;
-
- 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->AddressMap.SwitchToRealSize);
- CpuMpData->WakeupBufferHigh = GetModeTransitionBuffer (
- CpuMpData->AddressMap.RendezvousFunnelSize +
- CpuMpData->AddressMap.SwitchToRealSize -
- CpuMpData->AddressMap.ModeTransitionOffset
- );
- //
- // The reset stack starts at the end of the buffer.
- //
- CpuMpData->SevEsAPResetStackStart = CpuMpData->WakeupBuffer + ApResetVectorSize;
+ ASSERT (FALSE);
+ CpuDeadLoop ();
+ }
+ }
}
+
BackupAndPrepareWakeupBuffer (CpuMpData);
}
@@ -1221,7 +1129,7 @@ AllocateResetVector (
**/
VOID
FreeResetVector (
- IN CPU_MP_DATA *CpuMpData
+ IN CPU_MP_DATA *CpuMpData
)
{
//
@@ -1230,76 +1138,11 @@ FreeResetVector (
// perform the restore as this will overwrite memory which has data
// needed by SEV-ES.
//
- if (!CpuMpData->SevEsIsEnabled) {
+ if (!CpuMpData->UseSevEsAPMethod) {
RestoreWakeupBuffer (CpuMpData);
}
}
-/**
- Allocate the SEV-ES AP jump table buffer.
-
- @param[in, out] CpuMpData The pointer to CPU MP Data structure.
-**/
-VOID
-AllocateSevEsAPMemory (
- IN OUT CPU_MP_DATA *CpuMpData
- )
-{
- if (CpuMpData->SevEsAPBuffer == (UINTN) -1) {
- CpuMpData->SevEsAPBuffer =
- CpuMpData->SevEsIsEnabled ? GetSevEsAPMemory () : 0;
- }
-}
-
-/**
- Program the SEV-ES AP jump table buffer.
-
- @param[in] SipiVector The SIPI vector used for the AP Reset
-**/
-VOID
-SetSevEsJumpTable (
- IN UINTN SipiVector
- )
-{
- SEV_ES_AP_JMP_FAR *JmpFar;
- UINT32 Offset, InsnByte;
- UINT8 LoNib, HiNib;
-
- JmpFar = (SEV_ES_AP_JMP_FAR *) (UINTN) FixedPcdGet32 (PcdSevEsWorkAreaBase);
- ASSERT (JmpFar != NULL);
-
- //
- // Obtain the address of the Segment/Rip location in the workarea.
- // This will be set to a value derived from the SIPI vector and will
- // be the memory address used for the far jump below.
- //
- Offset = FixedPcdGet32 (PcdSevEsWorkAreaBase);
- Offset += sizeof (JmpFar->InsnBuffer);
- LoNib = (UINT8) Offset;
- HiNib = (UINT8) (Offset >> 8);
-
- //
- // Program the workarea (which is the initial AP boot address) with
- // far jump to the SIPI vector (where XX and YY represent the
- // address of where the SIPI vector is stored.
- //
- // JMP FAR [CS:XXYY] => 2E FF 2E YY XX
- //
- InsnByte = 0;
- JmpFar->InsnBuffer[InsnByte++] = 0x2E; // CS override prefix
- JmpFar->InsnBuffer[InsnByte++] = 0xFF; // JMP (FAR)
- JmpFar->InsnBuffer[InsnByte++] = 0x2E; // ModRM (JMP memory location)
- JmpFar->InsnBuffer[InsnByte++] = LoNib; // YY offset ...
- JmpFar->InsnBuffer[InsnByte++] = HiNib; // XX offset ...
-
- //
- // Program the Segment/Rip based on the SIPI vector (always at least
- // 16-byte aligned, so Rip is set to 0).
- //
- JmpFar->Rip = 0;
- JmpFar->Segment = (UINT16) (SipiVector >> 4);
-}
-
/**
This function will be called by BSP to wakeup AP.
@@ -1313,27 +1156,28 @@ SetSevEsJumpTable (
**/
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);
@@ -1358,21 +1202,22 @@ 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
+ // 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.
//
@@ -1382,9 +1227,16 @@ WakeUpAP (
//
// 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) {
//
@@ -1445,7 +1297,7 @@ WakeUpAP (
);
while (CpuMpData->MpCpuExchangeInfo->NumApsExecuting != 0) {
- CpuPause();
+ CpuPause ();
}
}
} else {
@@ -1460,20 +1312,20 @@ 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
+ // 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.
//
@@ -1481,11 +1333,16 @@ WakeUpAP (
SetSevEsJumpTable (ExchangeInfo->BufferStart);
}
- SendInitSipiSipi (
- CpuInfoInHob[ProcessorNumber].ApicId,
- (UINT32) 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
//
@@ -1525,8 +1382,8 @@ CalculateTimeout (
OUT UINT64 *CurrentTime
)
{
- UINT64 TimeoutInSeconds;
- UINT64 TimestampCounterFreq;
+ UINT64 TimeoutInSeconds;
+ UINT64 TimestampCounterFreq;
//
// Read the current value of the performance counter
@@ -1611,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;
}
@@ -1643,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
)
{
//
@@ -1656,7 +1518,7 @@ TimedWaitForApFinish (
return;
}
- CpuMpData->TotalTime = 0;
+ CpuMpData->TotalTime = 0;
CpuMpData->ExpectedTime = CalculateTimeout (
TimeLimit,
&CpuMpData->CurrentTime
@@ -1666,7 +1528,8 @@ TimedWaitForApFinish (
&CpuMpData->CurrentTime,
&CpuMpData->TotalTime,
CpuMpData->ExpectedTime
- )) {
+ ))
+ {
CpuPause ();
}
@@ -1695,10 +1558,10 @@ TimedWaitForApFinish (
**/
VOID
ResetProcessorToIdleState (
- IN UINTN ProcessorNumber
+ IN UINTN ProcessorNumber
)
{
- CPU_MP_DATA *CpuMpData;
+ CPU_MP_DATA *CpuMpData;
CpuMpData = GetCpuMpData ();
@@ -1707,6 +1570,7 @@ ResetProcessorToIdleState (
while (CpuMpData->FinishedCount < 1) {
CpuPause ();
}
+
CpuMpData->InitFlag = ApInitDone;
SetApState (&CpuMpData->CpuData[ProcessorNumber], CpuStateIdle);
@@ -1725,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 ();
@@ -1756,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];
@@ -1773,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 {
@@ -1787,6 +1652,7 @@ CheckThisAP (
if (CpuData->Finished != NULL) {
*(CpuData->Finished) = FALSE;
}
+
//
// Reset failed AP to idle state
//
@@ -1795,6 +1661,7 @@ CheckThisAP (
return EFI_TIMEOUT;
}
}
+
return EFI_NOT_READY;
}
@@ -1813,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 ();
@@ -1838,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.
@@ -1853,12 +1720,12 @@ CheckAllAPs (
WakeUpAP (
CpuMpData,
FALSE,
- (UINT32) NextProcessorNumber,
+ (UINT32)NextProcessorNumber,
CpuMpData->Procedure,
CpuMpData->ProcArguments,
TRUE
);
- }
+ }
}
}
}
@@ -1874,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++) {
@@ -1903,11 +1773,14 @@ CheckAllAPs (
}
}
}
+
if (CpuMpData->FailedCpuList != NULL) {
(*CpuMpData->FailedCpuList)[ListIndex] = END_OF_CPU_LIST;
}
+
return EFI_TIMEOUT;
}
+
return EFI_NOT_READY;
}
@@ -1944,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 = GetApResetVectorSize (&AddressMap);
- 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);
@@ -1976,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)
@@ -1990,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
@@ -1999,31 +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->SevEsIsEnabled = PcdGetBool (PcdSevEsIsEnabled);
- CpuMpData->SevEsAPBuffer = (UINTN) -1;
- CpuMpData->GhcbBase = PcdGet64 (PcdGhcbBase);
+ 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.
@@ -2059,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.
//
@@ -2077,13 +1983,13 @@ MpInitLibInitialize (
// from HOB
//
OldCpuMpData->NewCpuMpData = CpuMpData;
- CpuMpData->CpuCount = OldCpuMpData->CpuCount;
- CpuMpData->BspNumber = OldCpuMpData->BspNumber;
- CpuMpData->CpuInfoInHob = OldCpuMpData->CpuInfoInHob;
- CpuInfoInHob = (CPU_INFO_IN_HOB *) (UINTN) CpuMpData->CpuInfoInHob;
+ 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;
}
}
@@ -2091,7 +1997,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
@@ -2120,6 +2027,7 @@ MpInitLibInitialize (
//
CpuMpData->InitFlag = ApInitReconfig;
}
+
WakeUpAP (CpuMpData, TRUE, 0, ApInitializeSync, CpuMpData, TRUE);
//
// Wait for all APs finished initialization
@@ -2127,9 +2035,11 @@ MpInitLibInitialize (
while (CpuMpData->FinishedCount < (CpuMpData->CpuCount - 1)) {
CpuPause ();
}
+
if (OldCpuMpData != NULL) {
CpuMpData->InitFlag = ApInitDone;
}
+
for (Index = 0; Index < CpuMpData->CpuCount; Index++) {
SetApState (&CpuMpData->CpuData[Index], CpuStateIdle);
}
@@ -2138,28 +2048,34 @@ MpInitLibInitialize (
//
// Dump the microcode revision for each core.
//
- DEBUG_CODE (
- 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_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
//
@@ -2193,19 +2109,19 @@ MpInitLibGetProcessorInfo (
OUT EFI_HEALTH_FLAGS *HealthData OPTIONAL
)
{
- CPU_MP_DATA *CpuMpData;
- UINTN CallerNumber;
- CPU_INFO_IN_HOB *CpuInfoInHob;
- UINTN OriginalProcessorNumber;
+ 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;
+ ProcessorNumber &= BIT24 - 1;
//
// Check whether caller processor is BSP
@@ -2223,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 {
@@ -2279,8 +2197,8 @@ MpInitLibGetProcessorInfo (
**/
EFI_STATUS
SwitchBSPWorker (
- IN UINTN ProcessorNumber,
- IN BOOLEAN EnableOldBSP
+ IN UINTN ProcessorNumber,
+ IN BOOLEAN EnableOldBSP
)
{
CPU_MP_DATA *CpuMpData;
@@ -2353,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);
@@ -2362,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 ();
@@ -2388,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.
@@ -2421,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 ();
@@ -2455,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;
@@ -2480,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;
@@ -2517,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 ();
@@ -2545,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;
}
@@ -2559,7 +2484,6 @@ MpInitLibGetNumberOfProcessors (
return EFI_SUCCESS;
}
-
/**
Worker function to execute a caller provided function on all enabled APs.
@@ -2594,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 ();
@@ -2618,7 +2542,7 @@ StartupAllCPUsWorker (
*FailedCpuList = NULL;
}
- if (CpuMpData->CpuCount == 1 && ExcludeBsp) {
+ if ((CpuMpData->CpuCount == 1) && ExcludeBsp) {
return EFI_NOT_STARTED;
}
@@ -2670,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) {
@@ -2692,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);
@@ -2702,6 +2626,7 @@ StartupAllCPUsWorker (
if (ProcessorNumber == CallerNumber) {
continue;
}
+
if (CpuMpData->CpuData[ProcessorNumber].Waiting) {
WakeUpAP (CpuMpData, FALSE, ProcessorNumber, Procedure, ProcedureArgument, TRUE);
break;
@@ -2751,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 ();
@@ -2816,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);
@@ -2848,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;
}
@@ -2890,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 (
@@ -2905,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);
+}