2015-10-19 21:12:53 +02:00
|
|
|
/** @file
|
|
|
|
Code for Processor S3 restoration
|
|
|
|
|
2022-12-16 13:46:26 +01:00
|
|
|
Copyright (c) 2006 - 2022, Intel Corporation. All rights reserved.<BR>
|
2019-04-04 01:07:22 +02:00
|
|
|
SPDX-License-Identifier: BSD-2-Clause-Patent
|
2015-10-19 21:12:53 +02:00
|
|
|
|
|
|
|
**/
|
|
|
|
|
|
|
|
#include "PiSmmCpuDxeSmm.h"
|
|
|
|
|
2018-01-11 10:05:15 +01:00
|
|
|
#pragma pack(1)
|
2015-10-19 21:12:53 +02:00
|
|
|
typedef struct {
|
2021-12-05 23:54:17 +01:00
|
|
|
UINTN Lock;
|
|
|
|
VOID *StackStart;
|
|
|
|
UINTN StackSize;
|
|
|
|
VOID *ApFunction;
|
|
|
|
IA32_DESCRIPTOR GdtrProfile;
|
|
|
|
IA32_DESCRIPTOR IdtrProfile;
|
|
|
|
UINT32 BufferStart;
|
|
|
|
UINT32 Cr3;
|
|
|
|
UINTN InitializeFloatingPointUnitsAddress;
|
2015-10-19 21:12:53 +02:00
|
|
|
} MP_CPU_EXCHANGE_INFO;
|
2018-01-11 10:05:15 +01:00
|
|
|
#pragma pack()
|
2015-10-19 21:12:53 +02:00
|
|
|
|
|
|
|
typedef struct {
|
2021-12-05 23:54:17 +01:00
|
|
|
UINT8 *RendezvousFunnelAddress;
|
|
|
|
UINTN PModeEntryOffset;
|
|
|
|
UINTN FlatJumpOffset;
|
|
|
|
UINTN Size;
|
|
|
|
UINTN LModeEntryOffset;
|
|
|
|
UINTN LongJumpOffset;
|
2015-10-19 21:12:53 +02:00
|
|
|
} MP_ASSEMBLY_ADDRESS_MAP;
|
|
|
|
|
2016-06-29 03:00:13 +02:00
|
|
|
//
|
2018-10-15 04:34:59 +02:00
|
|
|
// Flags used when program the register.
|
2016-06-29 03:00:13 +02:00
|
|
|
//
|
2018-10-15 04:34:59 +02:00
|
|
|
typedef struct {
|
2021-12-05 23:54:17 +01:00
|
|
|
volatile UINTN MemoryMappedLock; // Spinlock used to program mmio
|
|
|
|
volatile UINT32 *CoreSemaphoreCount; // Semaphore container used to program
|
UefiCpuPkg/PiSmmCpuDxeSmm: Separate semaphore container.
In current implementation, core and package level sync uses same semaphores.
Sharing the semaphore may cause wrong execution order.
For example:
1. Feature A has CPU_FEATURE_CORE_BEFORE dependency with Feature B.
2. Feature C has CPU_FEATURE_PACKAGE_AFTER dependency with Feature B.
The expected feature initialization order is A B C:
A ---- (Core Depends) ----> B ---- (Package Depends) ----> C
For a CPU has 1 package, 2 cores and 4 threads. The feature initialization
order may like below:
Thread#1 Thread#2 Thread#3 Thread#4
[A.Init] [A.Init] [A.Init]
Release(S1, S2) Release(S1, S2) Release(S3, S4)
Wait(S1) * 2 Wait(S2) * 2 <------------------------------- Core sync
[B.Init] [B.Init]
Release (S1,S2,S3,S4)
Wait (S1) * 4 <----------------------------------------------------- Package sync
Wait(S4 * 2) <- Core sync
[B.Init]
In above case, for thread#4, when it syncs in core level, Wait(S4) * 2 isn't
blocked and [B.Init] runs. But [A.Init] hasn't run in thread#3. It's wrong!
Thread#4 should execute [B.Init] after thread#3 executes [A.Init] because B
core level depends on A.
The reason of the wrong execution order is that S4 is released in thread#1
by calling Release (S1, S2, S3, S4) and in thread #4 by calling
Release (S3, S4).
To fix this issue, core level sync and package level sync should use separate
semaphores.
In above example, the S4 released in Release (S1, S2, S3, S4) should not be the
same semaphore as that in Release (S3, S4).
Related BZ: https://bugzilla.tianocore.org/show_bug.cgi?id=1311
Cc: Laszlo Ersek <lersek@redhat.com>
Cc: Ruiyu Ni <ruiyu.ni@intel.com>
Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Eric Dong <eric.dong@intel.com>
Reviewed-by: Ruiyu Ni <ruiyu.ni@intel.com>
Acked-by: Laszlo Ersek <lersek@redhat.com>
2018-11-10 03:53:41 +01:00
|
|
|
// core level semaphore.
|
2021-12-05 23:54:17 +01:00
|
|
|
volatile UINT32 *PackageSemaphoreCount; // Semaphore container used to program
|
UefiCpuPkg/PiSmmCpuDxeSmm: Separate semaphore container.
In current implementation, core and package level sync uses same semaphores.
Sharing the semaphore may cause wrong execution order.
For example:
1. Feature A has CPU_FEATURE_CORE_BEFORE dependency with Feature B.
2. Feature C has CPU_FEATURE_PACKAGE_AFTER dependency with Feature B.
The expected feature initialization order is A B C:
A ---- (Core Depends) ----> B ---- (Package Depends) ----> C
For a CPU has 1 package, 2 cores and 4 threads. The feature initialization
order may like below:
Thread#1 Thread#2 Thread#3 Thread#4
[A.Init] [A.Init] [A.Init]
Release(S1, S2) Release(S1, S2) Release(S3, S4)
Wait(S1) * 2 Wait(S2) * 2 <------------------------------- Core sync
[B.Init] [B.Init]
Release (S1,S2,S3,S4)
Wait (S1) * 4 <----------------------------------------------------- Package sync
Wait(S4 * 2) <- Core sync
[B.Init]
In above case, for thread#4, when it syncs in core level, Wait(S4) * 2 isn't
blocked and [B.Init] runs. But [A.Init] hasn't run in thread#3. It's wrong!
Thread#4 should execute [B.Init] after thread#3 executes [A.Init] because B
core level depends on A.
The reason of the wrong execution order is that S4 is released in thread#1
by calling Release (S1, S2, S3, S4) and in thread #4 by calling
Release (S3, S4).
To fix this issue, core level sync and package level sync should use separate
semaphores.
In above example, the S4 released in Release (S1, S2, S3, S4) should not be the
same semaphore as that in Release (S3, S4).
Related BZ: https://bugzilla.tianocore.org/show_bug.cgi?id=1311
Cc: Laszlo Ersek <lersek@redhat.com>
Cc: Ruiyu Ni <ruiyu.ni@intel.com>
Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Eric Dong <eric.dong@intel.com>
Reviewed-by: Ruiyu Ni <ruiyu.ni@intel.com>
Acked-by: Laszlo Ersek <lersek@redhat.com>
2018-11-10 03:53:41 +01:00
|
|
|
// package level semaphore.
|
2018-10-15 04:34:59 +02:00
|
|
|
} PROGRAM_CPU_REGISTER_FLAGS;
|
2016-06-29 03:00:13 +02:00
|
|
|
|
2017-09-28 10:57:35 +02:00
|
|
|
//
|
|
|
|
// Signal that SMM BASE relocation is complete.
|
|
|
|
//
|
2021-12-05 23:54:17 +01:00
|
|
|
volatile BOOLEAN mInitApsAfterSmmBaseReloc;
|
2017-09-28 10:57:35 +02:00
|
|
|
|
2015-10-19 21:12:53 +02:00
|
|
|
/**
|
|
|
|
Get starting address and size of the rendezvous entry for APs.
|
|
|
|
Information for fixing a jump instruction in the code is also returned.
|
|
|
|
|
|
|
|
@param AddressMap Output buffer for address map information.
|
|
|
|
**/
|
|
|
|
VOID *
|
|
|
|
EFIAPI
|
|
|
|
AsmGetAddressMap (
|
2021-12-05 23:54:17 +01:00
|
|
|
MP_ASSEMBLY_ADDRESS_MAP *AddressMap
|
2015-10-19 21:12:53 +02:00
|
|
|
);
|
|
|
|
|
2021-12-05 23:54:17 +01:00
|
|
|
#define LEGACY_REGION_SIZE (2 * 0x1000)
|
|
|
|
#define LEGACY_REGION_BASE (0xA0000 - LEGACY_REGION_SIZE)
|
2015-10-19 21:12:53 +02:00
|
|
|
|
2021-12-05 23:54:17 +01:00
|
|
|
PROGRAM_CPU_REGISTER_FLAGS mCpuFlags;
|
|
|
|
ACPI_CPU_DATA mAcpiCpuData;
|
|
|
|
volatile UINT32 mNumberToFinish;
|
|
|
|
MP_CPU_EXCHANGE_INFO *mExchangeInfo;
|
|
|
|
BOOLEAN mRestoreSmmConfigurationInS3 = FALSE;
|
2015-10-19 21:12:53 +02:00
|
|
|
|
2016-07-19 10:44:16 +02:00
|
|
|
//
|
|
|
|
// S3 boot flag
|
|
|
|
//
|
2021-12-05 23:54:17 +01:00
|
|
|
BOOLEAN mSmmS3Flag = FALSE;
|
2016-07-19 10:44:16 +02:00
|
|
|
|
|
|
|
//
|
|
|
|
// Pointer to structure used during S3 Resume
|
|
|
|
//
|
2021-12-05 23:54:17 +01:00
|
|
|
SMM_S3_RESUME_STATE *mSmmS3ResumeState = NULL;
|
2016-07-19 10:44:16 +02:00
|
|
|
|
2021-12-05 23:54:17 +01:00
|
|
|
BOOLEAN mAcpiS3Enable = TRUE;
|
2016-07-20 04:24:58 +02:00
|
|
|
|
2021-12-05 23:54:17 +01:00
|
|
|
UINT8 *mApHltLoopCode = NULL;
|
|
|
|
UINT8 mApHltLoopCodeTemplate[] = {
|
|
|
|
0x8B, 0x44, 0x24, 0x04, // mov eax, dword ptr [esp+4]
|
|
|
|
0xF0, 0xFF, 0x08, // lock dec dword ptr [eax]
|
|
|
|
0xFA, // cli
|
|
|
|
0xF4, // hlt
|
|
|
|
0xEB, 0xFC // jmp $-2
|
|
|
|
};
|
2016-11-10 06:40:12 +01:00
|
|
|
|
2015-10-19 21:12:53 +02:00
|
|
|
/**
|
|
|
|
Sync up the MTRR values for all processors.
|
|
|
|
|
|
|
|
@param MtrrTable Table holding fixed/variable MTRR values to be loaded.
|
|
|
|
**/
|
|
|
|
VOID
|
|
|
|
EFIAPI
|
|
|
|
LoadMtrrData (
|
2021-12-05 23:54:17 +01:00
|
|
|
EFI_PHYSICAL_ADDRESS MtrrTable
|
2015-10-19 21:12:53 +02:00
|
|
|
)
|
2021-12-05 23:54:17 +01:00
|
|
|
|
2015-10-19 21:12:53 +02:00
|
|
|
/*++
|
|
|
|
|
|
|
|
Routine Description:
|
|
|
|
|
|
|
|
Sync up the MTRR values for all processors.
|
|
|
|
|
|
|
|
Arguments:
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
None
|
|
|
|
|
|
|
|
--*/
|
|
|
|
{
|
2021-12-05 23:54:17 +01:00
|
|
|
MTRR_SETTINGS *MtrrSettings;
|
2015-10-19 21:12:53 +02:00
|
|
|
|
2021-12-05 23:54:17 +01:00
|
|
|
MtrrSettings = (MTRR_SETTINGS *)(UINTN)MtrrTable;
|
2015-10-19 21:12:53 +02:00
|
|
|
MtrrSetAllMtrrs (MtrrSettings);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-10-15 04:34:59 +02:00
|
|
|
Increment semaphore by 1.
|
2015-10-19 21:12:53 +02:00
|
|
|
|
2018-10-15 04:34:59 +02:00
|
|
|
@param Sem IN: 32-bit unsigned integer
|
2015-10-19 21:12:53 +02:00
|
|
|
|
2018-10-15 04:34:59 +02:00
|
|
|
**/
|
|
|
|
VOID
|
|
|
|
S3ReleaseSemaphore (
|
2021-12-05 23:54:17 +01:00
|
|
|
IN OUT volatile UINT32 *Sem
|
2018-10-15 04:34:59 +02:00
|
|
|
)
|
|
|
|
{
|
|
|
|
InterlockedIncrement (Sem);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
Decrement the semaphore by 1 if it is not zero.
|
2015-10-19 21:12:53 +02:00
|
|
|
|
2018-10-15 04:34:59 +02:00
|
|
|
Performs an atomic decrement operation for semaphore.
|
|
|
|
The compare exchange operation must be performed using
|
|
|
|
MP safe mechanisms.
|
|
|
|
|
|
|
|
@param Sem IN: 32-bit unsigned integer
|
|
|
|
|
|
|
|
**/
|
|
|
|
VOID
|
|
|
|
S3WaitForSemaphore (
|
2021-12-05 23:54:17 +01:00
|
|
|
IN OUT volatile UINT32 *Sem
|
2018-10-15 04:34:59 +02:00
|
|
|
)
|
|
|
|
{
|
|
|
|
UINT32 Value;
|
|
|
|
|
|
|
|
do {
|
|
|
|
Value = *Sem;
|
|
|
|
} while (Value == 0 ||
|
|
|
|
InterlockedCompareExchange32 (
|
|
|
|
Sem,
|
|
|
|
Value,
|
|
|
|
Value - 1
|
|
|
|
) != Value);
|
|
|
|
}
|
|
|
|
|
2019-08-16 05:57:26 +02:00
|
|
|
/**
|
|
|
|
Read / write CR value.
|
|
|
|
|
|
|
|
@param[in] CrIndex The CR index which need to read/write.
|
|
|
|
@param[in] Read Read or write. TRUE is read.
|
|
|
|
@param[in,out] CrValue CR value.
|
|
|
|
|
|
|
|
@retval EFI_SUCCESS means read/write success, else return EFI_UNSUPPORTED.
|
|
|
|
**/
|
|
|
|
UINTN
|
|
|
|
ReadWriteCr (
|
2021-12-05 23:54:17 +01:00
|
|
|
IN UINT32 CrIndex,
|
|
|
|
IN BOOLEAN Read,
|
|
|
|
IN OUT UINTN *CrValue
|
2019-08-16 05:57:26 +02:00
|
|
|
)
|
|
|
|
{
|
|
|
|
switch (CrIndex) {
|
2021-12-05 23:54:17 +01:00
|
|
|
case 0:
|
|
|
|
if (Read) {
|
|
|
|
*CrValue = AsmReadCr0 ();
|
|
|
|
} else {
|
|
|
|
AsmWriteCr0 (*CrValue);
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
if (Read) {
|
|
|
|
*CrValue = AsmReadCr2 ();
|
|
|
|
} else {
|
|
|
|
AsmWriteCr2 (*CrValue);
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
if (Read) {
|
|
|
|
*CrValue = AsmReadCr3 ();
|
|
|
|
} else {
|
|
|
|
AsmWriteCr3 (*CrValue);
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
if (Read) {
|
|
|
|
*CrValue = AsmReadCr4 ();
|
|
|
|
} else {
|
|
|
|
AsmWriteCr4 (*CrValue);
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return EFI_UNSUPPORTED;
|
2019-08-16 05:57:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return EFI_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2018-10-15 04:34:59 +02:00
|
|
|
/**
|
|
|
|
Initialize the CPU registers from a register table.
|
|
|
|
|
|
|
|
@param[in] RegisterTable The register table for this AP.
|
|
|
|
@param[in] ApLocation AP location info for this ap.
|
|
|
|
@param[in] CpuStatus CPU status info for this CPU.
|
|
|
|
@param[in] CpuFlags Flags data structure used when program the register.
|
|
|
|
|
|
|
|
@note This service could be called by BSP/APs.
|
2015-10-19 21:12:53 +02:00
|
|
|
**/
|
|
|
|
VOID
|
2018-10-15 04:34:59 +02:00
|
|
|
ProgramProcessorRegister (
|
2021-12-05 23:54:17 +01:00
|
|
|
IN CPU_REGISTER_TABLE *RegisterTable,
|
|
|
|
IN EFI_CPU_PHYSICAL_LOCATION *ApLocation,
|
|
|
|
IN CPU_STATUS_INFORMATION *CpuStatus,
|
|
|
|
IN PROGRAM_CPU_REGISTER_FLAGS *CpuFlags
|
2015-10-19 21:12:53 +02:00
|
|
|
)
|
|
|
|
{
|
|
|
|
CPU_REGISTER_TABLE_ENTRY *RegisterTableEntry;
|
|
|
|
UINTN Index;
|
|
|
|
UINTN Value;
|
2018-10-15 04:34:59 +02:00
|
|
|
CPU_REGISTER_TABLE_ENTRY *RegisterTableEntryHead;
|
|
|
|
volatile UINT32 *SemaphorePtr;
|
|
|
|
UINT32 FirstThread;
|
|
|
|
UINT32 CurrentThread;
|
2020-12-02 02:51:31 +01:00
|
|
|
UINT32 CurrentCore;
|
2018-10-15 04:34:59 +02:00
|
|
|
UINTN ProcessorIndex;
|
2020-12-02 02:51:31 +01:00
|
|
|
UINT32 *ThreadCountPerPackage;
|
|
|
|
UINT8 *ThreadCountPerCore;
|
2019-08-16 05:57:26 +02:00
|
|
|
EFI_STATUS Status;
|
2019-08-16 05:57:27 +02:00
|
|
|
UINT64 CurrentValue;
|
2015-10-19 21:12:53 +02:00
|
|
|
|
|
|
|
//
|
|
|
|
// Traverse Register Table of this logical processor
|
|
|
|
//
|
2021-12-05 23:54:17 +01:00
|
|
|
RegisterTableEntryHead = (CPU_REGISTER_TABLE_ENTRY *)(UINTN)RegisterTable->RegisterTableEntry;
|
2018-10-15 04:34:59 +02:00
|
|
|
|
|
|
|
for (Index = 0; Index < RegisterTable->TableLength; Index++) {
|
|
|
|
RegisterTableEntry = &RegisterTableEntryHead[Index];
|
|
|
|
|
2015-10-19 21:12:53 +02:00
|
|
|
//
|
|
|
|
// Check the type of specified register
|
|
|
|
//
|
|
|
|
switch (RegisterTableEntry->RegisterType) {
|
2021-12-05 23:54:17 +01:00
|
|
|
//
|
|
|
|
// The specified register is Control Register
|
|
|
|
//
|
|
|
|
case ControlRegister:
|
|
|
|
Status = ReadWriteCr (RegisterTableEntry->Index, TRUE, &Value);
|
|
|
|
if (EFI_ERROR (Status)) {
|
2019-08-16 05:57:27 +02:00
|
|
|
break;
|
|
|
|
}
|
2021-12-05 23:54:17 +01:00
|
|
|
|
|
|
|
if (RegisterTableEntry->TestThenWrite) {
|
2019-08-16 05:57:27 +02:00
|
|
|
CurrentValue = BitFieldRead64 (
|
|
|
|
Value,
|
|
|
|
RegisterTableEntry->ValidBitStart,
|
|
|
|
RegisterTableEntry->ValidBitStart + RegisterTableEntry->ValidBitLength - 1
|
|
|
|
);
|
|
|
|
if (CurrentValue == RegisterTableEntry->Value) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-05 23:54:17 +01:00
|
|
|
Value = (UINTN)BitFieldWrite64 (
|
|
|
|
Value,
|
|
|
|
RegisterTableEntry->ValidBitStart,
|
|
|
|
RegisterTableEntry->ValidBitStart + RegisterTableEntry->ValidBitLength - 1,
|
|
|
|
RegisterTableEntry->Value
|
|
|
|
);
|
|
|
|
ReadWriteCr (RegisterTableEntry->Index, FALSE, &Value);
|
|
|
|
break;
|
2015-10-19 21:12:53 +02:00
|
|
|
//
|
2021-12-05 23:54:17 +01:00
|
|
|
// The specified register is Model Specific Register
|
2015-10-19 21:12:53 +02:00
|
|
|
//
|
2021-12-05 23:54:17 +01:00
|
|
|
case Msr:
|
|
|
|
if (RegisterTableEntry->TestThenWrite) {
|
|
|
|
Value = (UINTN)AsmReadMsr64 (RegisterTableEntry->Index);
|
|
|
|
if (RegisterTableEntry->ValidBitLength >= 64) {
|
|
|
|
if (Value == RegisterTableEntry->Value) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
CurrentValue = BitFieldRead64 (
|
|
|
|
Value,
|
|
|
|
RegisterTableEntry->ValidBitStart,
|
|
|
|
RegisterTableEntry->ValidBitStart + RegisterTableEntry->ValidBitLength - 1
|
|
|
|
);
|
|
|
|
if (CurrentValue == RegisterTableEntry->Value) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-19 21:12:53 +02:00
|
|
|
//
|
2021-12-05 23:54:17 +01:00
|
|
|
// If this function is called to restore register setting after INIT signal,
|
|
|
|
// there is no need to restore MSRs in register table.
|
2015-10-19 21:12:53 +02:00
|
|
|
//
|
2021-12-05 23:54:17 +01:00
|
|
|
if (RegisterTableEntry->ValidBitLength >= 64) {
|
|
|
|
//
|
|
|
|
// If length is not less than 64 bits, then directly write without reading
|
|
|
|
//
|
|
|
|
AsmWriteMsr64 (
|
|
|
|
RegisterTableEntry->Index,
|
|
|
|
RegisterTableEntry->Value
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
//
|
|
|
|
// Set the bit section according to bit start and length
|
|
|
|
//
|
|
|
|
AsmMsrBitFieldWrite64 (
|
|
|
|
RegisterTableEntry->Index,
|
|
|
|
RegisterTableEntry->ValidBitStart,
|
|
|
|
RegisterTableEntry->ValidBitStart + RegisterTableEntry->ValidBitLength - 1,
|
|
|
|
RegisterTableEntry->Value
|
|
|
|
);
|
|
|
|
}
|
2015-10-19 21:12:53 +02:00
|
|
|
|
2021-12-05 23:54:17 +01:00
|
|
|
break;
|
2018-10-15 04:34:59 +02:00
|
|
|
//
|
2021-12-05 23:54:17 +01:00
|
|
|
// MemoryMapped operations
|
2018-10-15 04:34:59 +02:00
|
|
|
//
|
2021-12-05 23:54:17 +01:00
|
|
|
case MemoryMapped:
|
|
|
|
AcquireSpinLock (&CpuFlags->MemoryMappedLock);
|
|
|
|
MmioBitFieldWrite32 (
|
|
|
|
(UINTN)(RegisterTableEntry->Index | LShiftU64 (RegisterTableEntry->HighIndex, 32)),
|
|
|
|
RegisterTableEntry->ValidBitStart,
|
|
|
|
RegisterTableEntry->ValidBitStart + RegisterTableEntry->ValidBitLength - 1,
|
|
|
|
(UINT32)RegisterTableEntry->Value
|
|
|
|
);
|
|
|
|
ReleaseSpinLock (&CpuFlags->MemoryMappedLock);
|
|
|
|
break;
|
2018-10-15 04:34:59 +02:00
|
|
|
//
|
2021-12-05 23:54:17 +01:00
|
|
|
// Enable or disable cache
|
2018-10-15 04:34:59 +02:00
|
|
|
//
|
2021-12-05 23:54:17 +01:00
|
|
|
case CacheControl:
|
2018-10-15 04:34:59 +02:00
|
|
|
//
|
2021-12-05 23:54:17 +01:00
|
|
|
// If value of the entry is 0, then disable cache. Otherwise, enable cache.
|
2020-12-02 02:51:31 +01:00
|
|
|
//
|
2021-12-05 23:54:17 +01:00
|
|
|
if (RegisterTableEntry->Value == 0) {
|
|
|
|
AsmDisableCache ();
|
|
|
|
} else {
|
|
|
|
AsmEnableCache ();
|
2018-10-15 04:34:59 +02:00
|
|
|
}
|
2021-12-05 23:54:17 +01:00
|
|
|
|
2018-10-15 04:34:59 +02:00
|
|
|
break;
|
|
|
|
|
2021-12-05 23:54:17 +01:00
|
|
|
case Semaphore:
|
|
|
|
// Semaphore works logic like below:
|
2018-10-15 04:34:59 +02:00
|
|
|
//
|
2021-12-05 23:54:17 +01:00
|
|
|
// V(x) = LibReleaseSemaphore (Semaphore[FirstThread + x]);
|
|
|
|
// P(x) = LibWaitForSemaphore (Semaphore[FirstThread + x]);
|
2018-10-15 04:34:59 +02:00
|
|
|
//
|
2021-12-05 23:54:17 +01:00
|
|
|
// All threads (T0...Tn) waits in P() line and continues running
|
|
|
|
// together.
|
2018-10-15 04:34:59 +02:00
|
|
|
//
|
|
|
|
//
|
2021-12-05 23:54:17 +01:00
|
|
|
// T0 T1 ... Tn
|
2018-10-15 04:34:59 +02:00
|
|
|
//
|
2021-12-05 23:54:17 +01:00
|
|
|
// V(0...n) V(0...n) ... V(0...n)
|
|
|
|
// n * P(0) n * P(1) ... n * P(n)
|
2018-10-15 04:34:59 +02:00
|
|
|
//
|
2021-12-05 23:54:17 +01:00
|
|
|
ASSERT (
|
|
|
|
(ApLocation != NULL) &&
|
|
|
|
(CpuStatus->ThreadCountPerPackage != 0) &&
|
|
|
|
(CpuStatus->ThreadCountPerCore != 0) &&
|
|
|
|
(CpuFlags->CoreSemaphoreCount != NULL) &&
|
|
|
|
(CpuFlags->PackageSemaphoreCount != NULL)
|
|
|
|
);
|
|
|
|
switch (RegisterTableEntry->Value) {
|
|
|
|
case CoreDepType:
|
|
|
|
SemaphorePtr = CpuFlags->CoreSemaphoreCount;
|
|
|
|
ThreadCountPerCore = (UINT8 *)(UINTN)CpuStatus->ThreadCountPerCore;
|
|
|
|
|
|
|
|
CurrentCore = ApLocation->Package * CpuStatus->MaxCoreCount + ApLocation->Core;
|
|
|
|
//
|
|
|
|
// Get Offset info for the first thread in the core which current thread belongs to.
|
|
|
|
//
|
|
|
|
FirstThread = CurrentCore * CpuStatus->MaxThreadCount;
|
|
|
|
CurrentThread = FirstThread + ApLocation->Thread;
|
|
|
|
|
|
|
|
//
|
|
|
|
// Different cores may have different valid threads in them. If driver maintail clearly
|
|
|
|
// thread index in different cores, the logic will be much complicated.
|
|
|
|
// Here driver just simply records the max thread number in all cores and use it as expect
|
|
|
|
// thread number for all cores.
|
|
|
|
// In below two steps logic, first current thread will Release semaphore for each thread
|
|
|
|
// in current core. Maybe some threads are not valid in this core, but driver don't
|
|
|
|
// care. Second, driver will let current thread wait semaphore for all valid threads in
|
|
|
|
// current core. Because only the valid threads will do release semaphore for this
|
|
|
|
// thread, driver here only need to wait the valid thread count.
|
|
|
|
//
|
|
|
|
|
|
|
|
//
|
|
|
|
// First Notify ALL THREADs in current Core that this thread is ready.
|
|
|
|
//
|
|
|
|
for (ProcessorIndex = 0; ProcessorIndex < CpuStatus->MaxThreadCount; ProcessorIndex++) {
|
|
|
|
S3ReleaseSemaphore (&SemaphorePtr[FirstThread + ProcessorIndex]);
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Second, check whether all VALID THREADs (not all threads) in current core are ready.
|
|
|
|
//
|
|
|
|
for (ProcessorIndex = 0; ProcessorIndex < ThreadCountPerCore[CurrentCore]; ProcessorIndex++) {
|
|
|
|
S3WaitForSemaphore (&SemaphorePtr[CurrentThread]);
|
|
|
|
}
|
2018-10-15 04:34:59 +02:00
|
|
|
|
2021-12-05 23:54:17 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case PackageDepType:
|
|
|
|
SemaphorePtr = CpuFlags->PackageSemaphoreCount;
|
|
|
|
ThreadCountPerPackage = (UINT32 *)(UINTN)CpuStatus->ThreadCountPerPackage;
|
|
|
|
//
|
|
|
|
// Get Offset info for the first thread in the package which current thread belongs to.
|
|
|
|
//
|
|
|
|
FirstThread = ApLocation->Package * CpuStatus->MaxCoreCount * CpuStatus->MaxThreadCount;
|
|
|
|
//
|
|
|
|
// Get the possible threads count for current package.
|
|
|
|
//
|
|
|
|
CurrentThread = FirstThread + CpuStatus->MaxThreadCount * ApLocation->Core + ApLocation->Thread;
|
|
|
|
|
|
|
|
//
|
|
|
|
// Different packages may have different valid threads in them. If driver maintail clearly
|
|
|
|
// thread index in different packages, the logic will be much complicated.
|
|
|
|
// Here driver just simply records the max thread number in all packages and use it as expect
|
|
|
|
// thread number for all packages.
|
|
|
|
// In below two steps logic, first current thread will Release semaphore for each thread
|
|
|
|
// in current package. Maybe some threads are not valid in this package, but driver don't
|
|
|
|
// care. Second, driver will let current thread wait semaphore for all valid threads in
|
|
|
|
// current package. Because only the valid threads will do release semaphore for this
|
|
|
|
// thread, driver here only need to wait the valid thread count.
|
|
|
|
//
|
|
|
|
|
|
|
|
//
|
|
|
|
// First Notify ALL THREADS in current package that this thread is ready.
|
|
|
|
//
|
|
|
|
for (ProcessorIndex = 0; ProcessorIndex < CpuStatus->MaxThreadCount * CpuStatus->MaxCoreCount; ProcessorIndex++) {
|
|
|
|
S3ReleaseSemaphore (&SemaphorePtr[FirstThread + ProcessorIndex]);
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Second, check whether VALID THREADS (not all threads) in current package are ready.
|
|
|
|
//
|
|
|
|
for (ProcessorIndex = 0; ProcessorIndex < ThreadCountPerPackage[ApLocation->Package]; ProcessorIndex++) {
|
|
|
|
S3WaitForSemaphore (&SemaphorePtr[CurrentThread]);
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
2018-10-15 04:34:59 +02:00
|
|
|
}
|
2021-12-05 23:54:17 +01:00
|
|
|
|
2018-10-15 04:34:59 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
2015-10-19 21:12:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-15 04:34:59 +02:00
|
|
|
/**
|
|
|
|
|
|
|
|
Set Processor register for one AP.
|
2018-10-25 03:51:29 +02:00
|
|
|
|
2018-10-15 04:34:59 +02:00
|
|
|
@param PreSmmRegisterTable Use pre Smm register table or register table.
|
|
|
|
|
|
|
|
**/
|
|
|
|
VOID
|
|
|
|
SetRegister (
|
2021-12-05 23:54:17 +01:00
|
|
|
IN BOOLEAN PreSmmRegisterTable
|
2018-10-15 04:34:59 +02:00
|
|
|
)
|
|
|
|
{
|
2021-12-05 23:54:17 +01:00
|
|
|
CPU_FEATURE_INIT_DATA *FeatureInitData;
|
|
|
|
CPU_REGISTER_TABLE *RegisterTable;
|
|
|
|
CPU_REGISTER_TABLE *RegisterTables;
|
|
|
|
UINT32 InitApicId;
|
|
|
|
UINTN ProcIndex;
|
|
|
|
UINTN Index;
|
2018-10-15 04:34:59 +02:00
|
|
|
|
2021-09-16 11:27:11 +02:00
|
|
|
FeatureInitData = &mAcpiCpuData.CpuFeatureInitData;
|
|
|
|
|
2018-10-15 04:34:59 +02:00
|
|
|
if (PreSmmRegisterTable) {
|
2021-09-16 11:27:11 +02:00
|
|
|
RegisterTables = (CPU_REGISTER_TABLE *)(UINTN)FeatureInitData->PreSmmInitRegisterTable;
|
2018-10-15 04:34:59 +02:00
|
|
|
} else {
|
2021-09-16 11:27:11 +02:00
|
|
|
RegisterTables = (CPU_REGISTER_TABLE *)(UINTN)FeatureInitData->RegisterTable;
|
2018-10-15 04:34:59 +02:00
|
|
|
}
|
2021-12-05 23:54:17 +01:00
|
|
|
|
2021-01-11 02:54:19 +01:00
|
|
|
if (RegisterTables == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
2018-10-15 04:34:59 +02:00
|
|
|
|
2021-12-05 23:54:17 +01:00
|
|
|
InitApicId = GetInitialApicId ();
|
2018-10-15 04:34:59 +02:00
|
|
|
RegisterTable = NULL;
|
2021-12-05 23:54:17 +01:00
|
|
|
ProcIndex = (UINTN)-1;
|
2018-10-15 04:34:59 +02:00
|
|
|
for (Index = 0; Index < mAcpiCpuData.NumberOfCpus; Index++) {
|
|
|
|
if (RegisterTables[Index].InitialApicId == InitApicId) {
|
|
|
|
RegisterTable = &RegisterTables[Index];
|
2021-12-05 23:54:17 +01:00
|
|
|
ProcIndex = Index;
|
2018-10-15 04:34:59 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2021-12-05 23:54:17 +01:00
|
|
|
|
2018-10-15 04:34:59 +02:00
|
|
|
ASSERT (RegisterTable != NULL);
|
|
|
|
|
2021-09-16 11:27:11 +02:00
|
|
|
if (FeatureInitData->ApLocation != 0) {
|
2018-10-15 04:34:59 +02:00
|
|
|
ProgramProcessorRegister (
|
|
|
|
RegisterTable,
|
2021-09-16 11:27:11 +02:00
|
|
|
(EFI_CPU_PHYSICAL_LOCATION *)(UINTN)FeatureInitData->ApLocation + ProcIndex,
|
|
|
|
&FeatureInitData->CpuStatus,
|
2018-10-15 04:34:59 +02:00
|
|
|
&mCpuFlags
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
ProgramProcessorRegister (
|
|
|
|
RegisterTable,
|
|
|
|
NULL,
|
2021-09-16 11:27:11 +02:00
|
|
|
&FeatureInitData->CpuStatus,
|
2018-10-15 04:34:59 +02:00
|
|
|
&mCpuFlags
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-19 21:12:53 +02:00
|
|
|
/**
|
2017-09-28 10:57:35 +02:00
|
|
|
AP initialization before then after SMBASE relocation in the S3 boot path.
|
2015-10-19 21:12:53 +02:00
|
|
|
**/
|
|
|
|
VOID
|
2017-09-28 11:12:38 +02:00
|
|
|
InitializeAp (
|
2015-10-19 21:12:53 +02:00
|
|
|
VOID
|
|
|
|
)
|
|
|
|
{
|
2021-12-05 23:54:17 +01:00
|
|
|
UINTN TopOfStack;
|
|
|
|
UINT8 Stack[128];
|
2015-10-19 21:12:53 +02:00
|
|
|
|
|
|
|
LoadMtrrData (mAcpiCpuData.MtrrTable);
|
|
|
|
|
2018-10-15 04:34:59 +02:00
|
|
|
SetRegister (TRUE);
|
2017-09-28 10:57:35 +02:00
|
|
|
|
2015-10-19 21:12:53 +02:00
|
|
|
//
|
|
|
|
// Count down the number with lock mechanism.
|
|
|
|
//
|
|
|
|
InterlockedDecrement (&mNumberToFinish);
|
|
|
|
|
2017-09-28 10:57:35 +02:00
|
|
|
//
|
|
|
|
// Wait for BSP to signal SMM Base relocation done.
|
|
|
|
//
|
|
|
|
while (!mInitApsAfterSmmBaseReloc) {
|
|
|
|
CpuPause ();
|
|
|
|
}
|
2015-10-19 21:12:53 +02:00
|
|
|
|
|
|
|
ProgramVirtualWireMode ();
|
|
|
|
DisableLvtInterrupts ();
|
|
|
|
|
2018-10-15 04:34:59 +02:00
|
|
|
SetRegister (FALSE);
|
2015-10-19 21:12:53 +02:00
|
|
|
|
|
|
|
//
|
2016-11-11 06:25:51 +01:00
|
|
|
// Place AP into the safe code, count down the number with lock mechanism in the safe code.
|
2016-11-10 06:40:12 +01:00
|
|
|
//
|
2021-12-05 23:54:17 +01:00
|
|
|
TopOfStack = (UINTN)Stack + sizeof (Stack);
|
|
|
|
TopOfStack &= ~(UINTN)(CPU_STACK_ALIGNMENT - 1);
|
|
|
|
CopyMem ((VOID *)(UINTN)mApHltLoopCode, mApHltLoopCodeTemplate, sizeof (mApHltLoopCodeTemplate));
|
2016-11-17 21:41:35 +01:00
|
|
|
TransferApToSafeState ((UINTN)mApHltLoopCode, TopOfStack, (UINTN)&mNumberToFinish);
|
2015-10-19 21:12:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
Prepares startup vector for APs.
|
|
|
|
|
|
|
|
This function prepares startup vector for APs.
|
|
|
|
|
|
|
|
@param WorkingBuffer The address of the work buffer.
|
|
|
|
**/
|
|
|
|
VOID
|
|
|
|
PrepareApStartupVector (
|
|
|
|
EFI_PHYSICAL_ADDRESS WorkingBuffer
|
|
|
|
)
|
|
|
|
{
|
2021-12-05 23:54:17 +01:00
|
|
|
EFI_PHYSICAL_ADDRESS StartupVector;
|
|
|
|
MP_ASSEMBLY_ADDRESS_MAP AddressMap;
|
2015-10-19 21:12:53 +02:00
|
|
|
|
|
|
|
//
|
|
|
|
// Get the address map of startup code for AP,
|
|
|
|
// including code size, and offset of long jump instructions to redirect.
|
|
|
|
//
|
|
|
|
ZeroMem (&AddressMap, sizeof (AddressMap));
|
|
|
|
AsmGetAddressMap (&AddressMap);
|
|
|
|
|
|
|
|
StartupVector = WorkingBuffer;
|
|
|
|
|
|
|
|
//
|
|
|
|
// Copy AP startup code to startup vector, and then redirect the long jump
|
|
|
|
// instructions for mode switching.
|
|
|
|
//
|
2021-12-05 23:54:17 +01:00
|
|
|
CopyMem ((VOID *)(UINTN)StartupVector, AddressMap.RendezvousFunnelAddress, AddressMap.Size);
|
|
|
|
*(UINT32 *)(UINTN)(StartupVector + AddressMap.FlatJumpOffset + 3) = (UINT32)(StartupVector + AddressMap.PModeEntryOffset);
|
2015-10-19 21:12:53 +02:00
|
|
|
if (AddressMap.LongJumpOffset != 0) {
|
2021-12-05 23:54:17 +01:00
|
|
|
*(UINT32 *)(UINTN)(StartupVector + AddressMap.LongJumpOffset + 2) = (UINT32)(StartupVector + AddressMap.LModeEntryOffset);
|
2015-10-19 21:12:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Get the start address of exchange data between BSP and AP.
|
|
|
|
//
|
2021-12-05 23:54:17 +01:00
|
|
|
mExchangeInfo = (MP_CPU_EXCHANGE_INFO *)(UINTN)(StartupVector + AddressMap.Size);
|
|
|
|
ZeroMem ((VOID *)mExchangeInfo, sizeof (MP_CPU_EXCHANGE_INFO));
|
2015-10-19 21:12:53 +02:00
|
|
|
|
2021-12-05 23:54:17 +01:00
|
|
|
CopyMem ((VOID *)(UINTN)&mExchangeInfo->GdtrProfile, (VOID *)(UINTN)mAcpiCpuData.GdtrProfile, sizeof (IA32_DESCRIPTOR));
|
|
|
|
CopyMem ((VOID *)(UINTN)&mExchangeInfo->IdtrProfile, (VOID *)(UINTN)mAcpiCpuData.IdtrProfile, sizeof (IA32_DESCRIPTOR));
|
2015-10-19 21:12:53 +02:00
|
|
|
|
2021-12-05 23:54:17 +01:00
|
|
|
mExchangeInfo->StackStart = (VOID *)(UINTN)mAcpiCpuData.StackAddress;
|
|
|
|
mExchangeInfo->StackSize = mAcpiCpuData.StackSize;
|
|
|
|
mExchangeInfo->BufferStart = (UINT32)StartupVector;
|
|
|
|
mExchangeInfo->Cr3 = (UINT32)(AsmReadCr3 ());
|
2018-01-11 10:05:15 +01:00
|
|
|
mExchangeInfo->InitializeFloatingPointUnitsAddress = (UINTN)InitializeFloatingPointUnits;
|
2015-10-19 21:12:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
The function is invoked before SMBASE relocation in S3 path to restores CPU status.
|
|
|
|
|
|
|
|
The function is invoked before SMBASE relocation in S3 path. It does first time microcode load
|
|
|
|
and restores MTRRs for both BSP and APs.
|
|
|
|
|
|
|
|
**/
|
|
|
|
VOID
|
2017-09-28 11:12:38 +02:00
|
|
|
InitializeCpuBeforeRebase (
|
2015-10-19 21:12:53 +02:00
|
|
|
VOID
|
|
|
|
)
|
|
|
|
{
|
|
|
|
LoadMtrrData (mAcpiCpuData.MtrrTable);
|
|
|
|
|
2018-10-15 04:34:59 +02:00
|
|
|
SetRegister (TRUE);
|
2015-10-19 21:12:53 +02:00
|
|
|
|
|
|
|
ProgramVirtualWireMode ();
|
|
|
|
|
|
|
|
PrepareApStartupVector (mAcpiCpuData.StartupVector);
|
|
|
|
|
UefiCpuPkg/PiSmmCpuDxeSmm: fix S3 Resume for CPU hotplug
The "ACPI_CPU_DATA.NumberOfCpus" field is specified as follows, in
"UefiCpuPkg/Include/AcpiCpuData.h" (rewrapped for this commit message):
//
// The number of CPUs. If a platform does not support hot plug CPUs,
// then this is the number of CPUs detected when the platform is booted,
// regardless of being enabled or disabled. If a platform does support
// hot plug CPUs, then this is the maximum number of CPUs that the
// platform supports.
//
The InitializeCpuBeforeRebase() and InitializeCpuAfterRebase() functions
in "UefiCpuPkg/PiSmmCpuDxeSmm/CpuS3.c" try to restore CPU configuration on
the S3 Resume path for *all* CPUs accounted for in
"ACPI_CPU_DATA.NumberOfCpus". This is wrong, as with CPU hotplug, not all
of the possible CPUs may be present at the time of S3 Suspend / Resume.
The symptom is an infinite wait.
Instead, the "mNumberOfCpus" variable should be used, which is properly
maintained through the EFI_SMM_CPU_SERVICE_PROTOCOL implementation (see
SmmAddProcessor(), SmmRemoveProcessor(), SmmCpuUpdate() in
"UefiCpuPkg/PiSmmCpuDxeSmm/CpuService.c").
When CPU hotplug is disabled, "mNumberOfCpus" is constant, and equals
"ACPI_CPU_DATA.NumberOfCpus" at all times.
Cc: Ard Biesheuvel <ard.biesheuvel@linaro.org>
Cc: Eric Dong <eric.dong@intel.com>
Cc: Igor Mammedov <imammedo@redhat.com>
Cc: Jiewen Yao <jiewen.yao@intel.com>
Cc: Jordan Justen <jordan.l.justen@intel.com>
Cc: Michael Kinney <michael.d.kinney@intel.com>
Cc: Philippe Mathieu-Daudé <philmd@redhat.com>
Cc: Ray Ni <ray.ni@intel.com>
Ref: https://bugzilla.tianocore.org/show_bug.cgi?id=1512
Signed-off-by: Laszlo Ersek <lersek@redhat.com>
Message-Id: <20200226221156.29589-3-lersek@redhat.com>
Acked-by: Ard Biesheuvel <ard.biesheuvel@linaro.org>
Reviewed-by: Eric Dong <eric.dong@intel.com>
Tested-by: Boris Ostrovsky <boris.ostrovsky@oracle.com>
[lersek@redhat.com: shut up UINTN->UINT32 warning from Windows VS2019 PR]
2020-02-26 23:11:42 +01:00
|
|
|
if (FeaturePcdGet (PcdCpuHotPlugSupport)) {
|
|
|
|
ASSERT (mNumberOfCpus <= mAcpiCpuData.NumberOfCpus);
|
|
|
|
} else {
|
|
|
|
ASSERT (mNumberOfCpus == mAcpiCpuData.NumberOfCpus);
|
|
|
|
}
|
2021-12-05 23:54:17 +01:00
|
|
|
|
|
|
|
mNumberToFinish = (UINT32)(mNumberOfCpus - 1);
|
|
|
|
mExchangeInfo->ApFunction = (VOID *)(UINTN)InitializeAp;
|
2017-09-28 10:57:35 +02:00
|
|
|
|
|
|
|
//
|
|
|
|
// Execute code for before SmmBaseReloc. Note: This flag is maintained across S3 boots.
|
|
|
|
//
|
|
|
|
mInitApsAfterSmmBaseReloc = FALSE;
|
2015-10-19 21:12:53 +02:00
|
|
|
|
|
|
|
//
|
|
|
|
// Send INIT IPI - SIPI to all APs
|
|
|
|
//
|
|
|
|
SendInitSipiSipiAllExcludingSelf ((UINT32)mAcpiCpuData.StartupVector);
|
|
|
|
|
|
|
|
while (mNumberToFinish > 0) {
|
|
|
|
CpuPause ();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
The function is invoked after SMBASE relocation in S3 path to restores CPU status.
|
|
|
|
|
|
|
|
The function is invoked after SMBASE relocation in S3 path. It restores configuration according to
|
|
|
|
data saved by normal boot path for both BSP and APs.
|
|
|
|
|
|
|
|
**/
|
|
|
|
VOID
|
2017-09-28 11:12:38 +02:00
|
|
|
InitializeCpuAfterRebase (
|
2015-10-19 21:12:53 +02:00
|
|
|
VOID
|
|
|
|
)
|
|
|
|
{
|
UefiCpuPkg/PiSmmCpuDxeSmm: fix S3 Resume for CPU hotplug
The "ACPI_CPU_DATA.NumberOfCpus" field is specified as follows, in
"UefiCpuPkg/Include/AcpiCpuData.h" (rewrapped for this commit message):
//
// The number of CPUs. If a platform does not support hot plug CPUs,
// then this is the number of CPUs detected when the platform is booted,
// regardless of being enabled or disabled. If a platform does support
// hot plug CPUs, then this is the maximum number of CPUs that the
// platform supports.
//
The InitializeCpuBeforeRebase() and InitializeCpuAfterRebase() functions
in "UefiCpuPkg/PiSmmCpuDxeSmm/CpuS3.c" try to restore CPU configuration on
the S3 Resume path for *all* CPUs accounted for in
"ACPI_CPU_DATA.NumberOfCpus". This is wrong, as with CPU hotplug, not all
of the possible CPUs may be present at the time of S3 Suspend / Resume.
The symptom is an infinite wait.
Instead, the "mNumberOfCpus" variable should be used, which is properly
maintained through the EFI_SMM_CPU_SERVICE_PROTOCOL implementation (see
SmmAddProcessor(), SmmRemoveProcessor(), SmmCpuUpdate() in
"UefiCpuPkg/PiSmmCpuDxeSmm/CpuService.c").
When CPU hotplug is disabled, "mNumberOfCpus" is constant, and equals
"ACPI_CPU_DATA.NumberOfCpus" at all times.
Cc: Ard Biesheuvel <ard.biesheuvel@linaro.org>
Cc: Eric Dong <eric.dong@intel.com>
Cc: Igor Mammedov <imammedo@redhat.com>
Cc: Jiewen Yao <jiewen.yao@intel.com>
Cc: Jordan Justen <jordan.l.justen@intel.com>
Cc: Michael Kinney <michael.d.kinney@intel.com>
Cc: Philippe Mathieu-Daudé <philmd@redhat.com>
Cc: Ray Ni <ray.ni@intel.com>
Ref: https://bugzilla.tianocore.org/show_bug.cgi?id=1512
Signed-off-by: Laszlo Ersek <lersek@redhat.com>
Message-Id: <20200226221156.29589-3-lersek@redhat.com>
Acked-by: Ard Biesheuvel <ard.biesheuvel@linaro.org>
Reviewed-by: Eric Dong <eric.dong@intel.com>
Tested-by: Boris Ostrovsky <boris.ostrovsky@oracle.com>
[lersek@redhat.com: shut up UINTN->UINT32 warning from Windows VS2019 PR]
2020-02-26 23:11:42 +01:00
|
|
|
if (FeaturePcdGet (PcdCpuHotPlugSupport)) {
|
|
|
|
ASSERT (mNumberOfCpus <= mAcpiCpuData.NumberOfCpus);
|
|
|
|
} else {
|
|
|
|
ASSERT (mNumberOfCpus == mAcpiCpuData.NumberOfCpus);
|
|
|
|
}
|
2021-12-05 23:54:17 +01:00
|
|
|
|
UefiCpuPkg/PiSmmCpuDxeSmm: fix S3 Resume for CPU hotplug
The "ACPI_CPU_DATA.NumberOfCpus" field is specified as follows, in
"UefiCpuPkg/Include/AcpiCpuData.h" (rewrapped for this commit message):
//
// The number of CPUs. If a platform does not support hot plug CPUs,
// then this is the number of CPUs detected when the platform is booted,
// regardless of being enabled or disabled. If a platform does support
// hot plug CPUs, then this is the maximum number of CPUs that the
// platform supports.
//
The InitializeCpuBeforeRebase() and InitializeCpuAfterRebase() functions
in "UefiCpuPkg/PiSmmCpuDxeSmm/CpuS3.c" try to restore CPU configuration on
the S3 Resume path for *all* CPUs accounted for in
"ACPI_CPU_DATA.NumberOfCpus". This is wrong, as with CPU hotplug, not all
of the possible CPUs may be present at the time of S3 Suspend / Resume.
The symptom is an infinite wait.
Instead, the "mNumberOfCpus" variable should be used, which is properly
maintained through the EFI_SMM_CPU_SERVICE_PROTOCOL implementation (see
SmmAddProcessor(), SmmRemoveProcessor(), SmmCpuUpdate() in
"UefiCpuPkg/PiSmmCpuDxeSmm/CpuService.c").
When CPU hotplug is disabled, "mNumberOfCpus" is constant, and equals
"ACPI_CPU_DATA.NumberOfCpus" at all times.
Cc: Ard Biesheuvel <ard.biesheuvel@linaro.org>
Cc: Eric Dong <eric.dong@intel.com>
Cc: Igor Mammedov <imammedo@redhat.com>
Cc: Jiewen Yao <jiewen.yao@intel.com>
Cc: Jordan Justen <jordan.l.justen@intel.com>
Cc: Michael Kinney <michael.d.kinney@intel.com>
Cc: Philippe Mathieu-Daudé <philmd@redhat.com>
Cc: Ray Ni <ray.ni@intel.com>
Ref: https://bugzilla.tianocore.org/show_bug.cgi?id=1512
Signed-off-by: Laszlo Ersek <lersek@redhat.com>
Message-Id: <20200226221156.29589-3-lersek@redhat.com>
Acked-by: Ard Biesheuvel <ard.biesheuvel@linaro.org>
Reviewed-by: Eric Dong <eric.dong@intel.com>
Tested-by: Boris Ostrovsky <boris.ostrovsky@oracle.com>
[lersek@redhat.com: shut up UINTN->UINT32 warning from Windows VS2019 PR]
2020-02-26 23:11:42 +01:00
|
|
|
mNumberToFinish = (UINT32)(mNumberOfCpus - 1);
|
2015-10-19 21:12:53 +02:00
|
|
|
|
|
|
|
//
|
2018-10-15 04:34:59 +02:00
|
|
|
// Signal that SMM base relocation is complete and to continue initialization for all APs.
|
2015-10-19 21:12:53 +02:00
|
|
|
//
|
2017-09-28 10:57:35 +02:00
|
|
|
mInitApsAfterSmmBaseReloc = TRUE;
|
2015-10-19 21:12:53 +02:00
|
|
|
|
2018-10-15 04:34:59 +02:00
|
|
|
//
|
|
|
|
// Must begin set register after all APs have continue their initialization.
|
|
|
|
// This is a requirement to support semaphore mechanism in register table.
|
|
|
|
// Because if semaphore's dependence type is package type, semaphore will wait
|
|
|
|
// for all Aps in one package finishing their tasks before set next register
|
|
|
|
// for all APs. If the Aps not begin its task during BSP doing its task, the
|
|
|
|
// BSP thread will hang because it is waiting for other Aps in the same
|
|
|
|
// package finishing their task.
|
|
|
|
//
|
|
|
|
SetRegister (FALSE);
|
|
|
|
|
2015-10-19 21:12:53 +02:00
|
|
|
while (mNumberToFinish > 0) {
|
|
|
|
CpuPause ();
|
|
|
|
}
|
|
|
|
}
|
2016-07-19 10:44:16 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
Restore SMM Configuration in S3 boot path.
|
|
|
|
|
|
|
|
**/
|
|
|
|
VOID
|
|
|
|
RestoreSmmConfigurationInS3 (
|
|
|
|
VOID
|
|
|
|
)
|
|
|
|
{
|
2016-07-20 04:24:58 +02:00
|
|
|
if (!mAcpiS3Enable) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-07-19 10:44:16 +02:00
|
|
|
//
|
|
|
|
// Restore SMM Configuration in S3 boot path.
|
|
|
|
//
|
|
|
|
if (mRestoreSmmConfigurationInS3) {
|
|
|
|
//
|
|
|
|
// Need make sure gSmst is correct because below function may use them.
|
|
|
|
//
|
|
|
|
gSmst->SmmStartupThisAp = gSmmCpuPrivate->SmmCoreEntryContext.SmmStartupThisAp;
|
|
|
|
gSmst->CurrentlyExecutingCpu = gSmmCpuPrivate->SmmCoreEntryContext.CurrentlyExecutingCpu;
|
|
|
|
gSmst->NumberOfCpus = gSmmCpuPrivate->SmmCoreEntryContext.NumberOfCpus;
|
|
|
|
gSmst->CpuSaveStateSize = gSmmCpuPrivate->SmmCoreEntryContext.CpuSaveStateSize;
|
|
|
|
gSmst->CpuSaveState = gSmmCpuPrivate->SmmCoreEntryContext.CpuSaveState;
|
|
|
|
|
|
|
|
//
|
|
|
|
// Configure SMM Code Access Check feature if available.
|
|
|
|
//
|
|
|
|
ConfigSmmCodeAccessCheck ();
|
|
|
|
|
|
|
|
SmmCpuFeaturesCompleteSmmReadyToLock ();
|
|
|
|
|
|
|
|
mRestoreSmmConfigurationInS3 = FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
Perform SMM initialization for all processors in the S3 boot path.
|
|
|
|
|
|
|
|
For a native platform, MP initialization in the S3 boot path is also performed in this function.
|
|
|
|
**/
|
|
|
|
VOID
|
|
|
|
EFIAPI
|
|
|
|
SmmRestoreCpu (
|
|
|
|
VOID
|
|
|
|
)
|
|
|
|
{
|
2021-12-05 23:54:17 +01:00
|
|
|
SMM_S3_RESUME_STATE *SmmS3ResumeState;
|
|
|
|
IA32_DESCRIPTOR Ia32Idtr;
|
|
|
|
IA32_DESCRIPTOR X64Idtr;
|
|
|
|
IA32_IDT_GATE_DESCRIPTOR IdtEntryTable[EXCEPTION_VECTOR_NUMBER];
|
|
|
|
EFI_STATUS Status;
|
2016-07-19 10:44:16 +02:00
|
|
|
|
2021-11-17 04:21:42 +01:00
|
|
|
DEBUG ((DEBUG_INFO, "SmmRestoreCpu()\n"));
|
2016-07-19 10:44:16 +02:00
|
|
|
|
|
|
|
mSmmS3Flag = TRUE;
|
|
|
|
|
|
|
|
//
|
|
|
|
// See if there is enough context to resume PEI Phase
|
|
|
|
//
|
|
|
|
if (mSmmS3ResumeState == NULL) {
|
2021-11-17 04:21:42 +01:00
|
|
|
DEBUG ((DEBUG_ERROR, "No context to return to PEI Phase\n"));
|
2016-07-19 10:44:16 +02:00
|
|
|
CpuDeadLoop ();
|
|
|
|
}
|
|
|
|
|
|
|
|
SmmS3ResumeState = mSmmS3ResumeState;
|
|
|
|
ASSERT (SmmS3ResumeState != NULL);
|
|
|
|
|
2022-12-16 13:46:26 +01:00
|
|
|
//
|
|
|
|
// Setup 64bit IDT in 64bit SMM env when called from 32bit PEI.
|
|
|
|
// Note: 64bit PEI and 32bit DXE is not a supported combination.
|
|
|
|
//
|
|
|
|
if ((SmmS3ResumeState->Signature == SMM_S3_RESUME_SMM_64) && (FeaturePcdGet (PcdDxeIplSwitchToLongMode) == TRUE)) {
|
2016-07-19 10:44:16 +02:00
|
|
|
//
|
|
|
|
// Save the IA32 IDT Descriptor
|
|
|
|
//
|
2021-12-05 23:54:17 +01:00
|
|
|
AsmReadIdtr ((IA32_DESCRIPTOR *)&Ia32Idtr);
|
2016-07-19 10:44:16 +02:00
|
|
|
|
|
|
|
//
|
|
|
|
// Setup X64 IDT table
|
|
|
|
//
|
|
|
|
ZeroMem (IdtEntryTable, sizeof (IA32_IDT_GATE_DESCRIPTOR) * 32);
|
2021-12-05 23:54:17 +01:00
|
|
|
X64Idtr.Base = (UINTN)IdtEntryTable;
|
|
|
|
X64Idtr.Limit = (UINT16)(sizeof (IA32_IDT_GATE_DESCRIPTOR) * 32 - 1);
|
|
|
|
AsmWriteIdtr ((IA32_DESCRIPTOR *)&X64Idtr);
|
2016-07-19 10:44:16 +02:00
|
|
|
|
|
|
|
//
|
|
|
|
// Setup the default exception handler
|
|
|
|
//
|
|
|
|
Status = InitializeCpuExceptionHandlers (NULL);
|
|
|
|
ASSERT_EFI_ERROR (Status);
|
|
|
|
|
|
|
|
//
|
|
|
|
// Initialize Debug Agent to support source level debug
|
|
|
|
//
|
|
|
|
InitializeDebugAgent (DEBUG_AGENT_INIT_THUNK_PEI_IA32TOX64, (VOID *)&Ia32Idtr, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Skip initialization if mAcpiCpuData is not valid
|
|
|
|
//
|
|
|
|
if (mAcpiCpuData.NumberOfCpus > 0) {
|
|
|
|
//
|
|
|
|
// First time microcode load and restore MTRRs
|
|
|
|
//
|
2017-09-28 11:12:38 +02:00
|
|
|
InitializeCpuBeforeRebase ();
|
2016-07-19 10:44:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Restore SMBASE for BSP and all APs
|
|
|
|
//
|
|
|
|
SmmRelocateBases ();
|
|
|
|
|
|
|
|
//
|
|
|
|
// Skip initialization if mAcpiCpuData is not valid
|
|
|
|
//
|
|
|
|
if (mAcpiCpuData.NumberOfCpus > 0) {
|
|
|
|
//
|
|
|
|
// Restore MSRs for BSP and all APs
|
|
|
|
//
|
2017-09-28 11:12:38 +02:00
|
|
|
InitializeCpuAfterRebase ();
|
2016-07-19 10:44:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Set a flag to restore SMM configuration in S3 path.
|
|
|
|
//
|
|
|
|
mRestoreSmmConfigurationInS3 = TRUE;
|
|
|
|
|
2021-12-05 23:54:17 +01:00
|
|
|
DEBUG ((DEBUG_INFO, "SMM S3 Return CS = %x\n", SmmS3ResumeState->ReturnCs));
|
|
|
|
DEBUG ((DEBUG_INFO, "SMM S3 Return Entry Point = %x\n", SmmS3ResumeState->ReturnEntryPoint));
|
|
|
|
DEBUG ((DEBUG_INFO, "SMM S3 Return Context1 = %x\n", SmmS3ResumeState->ReturnContext1));
|
|
|
|
DEBUG ((DEBUG_INFO, "SMM S3 Return Context2 = %x\n", SmmS3ResumeState->ReturnContext2));
|
|
|
|
DEBUG ((DEBUG_INFO, "SMM S3 Return Stack Pointer = %x\n", SmmS3ResumeState->ReturnStackPointer));
|
2016-07-19 10:44:16 +02:00
|
|
|
|
|
|
|
//
|
2022-12-16 13:46:26 +01:00
|
|
|
// If SMM is in 32-bit mode or PcdDxeIplSwitchToLongMode is FALSE, then use SwitchStack() to resume PEI Phase.
|
|
|
|
// Note: 64bit PEI and 32bit DXE is not a supported combination.
|
2016-07-19 10:44:16 +02:00
|
|
|
//
|
2022-12-16 13:46:26 +01:00
|
|
|
if ((SmmS3ResumeState->Signature == SMM_S3_RESUME_SMM_32) || (FeaturePcdGet (PcdDxeIplSwitchToLongMode) == FALSE)) {
|
2021-11-17 04:21:42 +01:00
|
|
|
DEBUG ((DEBUG_INFO, "Call SwitchStack() to return to S3 Resume in PEI Phase\n"));
|
2016-07-19 10:44:16 +02:00
|
|
|
|
|
|
|
SwitchStack (
|
|
|
|
(SWITCH_STACK_ENTRY_POINT)(UINTN)SmmS3ResumeState->ReturnEntryPoint,
|
|
|
|
(VOID *)(UINTN)SmmS3ResumeState->ReturnContext1,
|
|
|
|
(VOID *)(UINTN)SmmS3ResumeState->ReturnContext2,
|
|
|
|
(VOID *)(UINTN)SmmS3ResumeState->ReturnStackPointer
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// If SMM is in 64-bit mode, then use AsmDisablePaging64() to resume PEI Phase
|
|
|
|
//
|
|
|
|
if (SmmS3ResumeState->Signature == SMM_S3_RESUME_SMM_64) {
|
2021-11-17 04:21:42 +01:00
|
|
|
DEBUG ((DEBUG_INFO, "Call AsmDisablePaging64() to return to S3 Resume in PEI Phase\n"));
|
2016-07-19 10:44:16 +02:00
|
|
|
//
|
|
|
|
// Disable interrupt of Debug timer, since new IDT table is for IA32 and will not work in long mode.
|
|
|
|
//
|
|
|
|
SaveAndSetDebugTimerInterrupt (FALSE);
|
|
|
|
//
|
|
|
|
// Restore IA32 IDT table
|
|
|
|
//
|
2021-12-05 23:54:17 +01:00
|
|
|
AsmWriteIdtr ((IA32_DESCRIPTOR *)&Ia32Idtr);
|
2016-07-19 10:44:16 +02:00
|
|
|
AsmDisablePaging64 (
|
|
|
|
SmmS3ResumeState->ReturnCs,
|
|
|
|
(UINT32)SmmS3ResumeState->ReturnEntryPoint,
|
|
|
|
(UINT32)SmmS3ResumeState->ReturnContext1,
|
|
|
|
(UINT32)SmmS3ResumeState->ReturnContext2,
|
|
|
|
(UINT32)SmmS3ResumeState->ReturnStackPointer
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Can not resume PEI Phase
|
|
|
|
//
|
2021-11-17 04:21:42 +01:00
|
|
|
DEBUG ((DEBUG_ERROR, "No context to return to PEI Phase\n"));
|
2016-07-19 10:44:16 +02:00
|
|
|
CpuDeadLoop ();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
Initialize SMM S3 resume state structure used during S3 Resume.
|
|
|
|
|
|
|
|
@param[in] Cr3 The base address of the page tables to use in SMM.
|
|
|
|
|
|
|
|
**/
|
|
|
|
VOID
|
|
|
|
InitSmmS3ResumeState (
|
|
|
|
IN UINT32 Cr3
|
|
|
|
)
|
|
|
|
{
|
2021-12-05 23:54:17 +01:00
|
|
|
VOID *GuidHob;
|
|
|
|
EFI_SMRAM_DESCRIPTOR *SmramDescriptor;
|
|
|
|
SMM_S3_RESUME_STATE *SmmS3ResumeState;
|
|
|
|
EFI_PHYSICAL_ADDRESS Address;
|
|
|
|
EFI_STATUS Status;
|
2016-07-19 10:44:16 +02:00
|
|
|
|
2016-07-20 04:24:58 +02:00
|
|
|
if (!mAcpiS3Enable) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-07-19 10:44:16 +02:00
|
|
|
GuidHob = GetFirstGuidHob (&gEfiAcpiVariableGuid);
|
2018-09-10 05:13:36 +02:00
|
|
|
if (GuidHob == NULL) {
|
|
|
|
DEBUG ((
|
|
|
|
DEBUG_ERROR,
|
|
|
|
"ERROR:%a(): HOB(gEfiAcpiVariableGuid=%g) needed by S3 resume doesn't exist!\n",
|
|
|
|
__FUNCTION__,
|
|
|
|
&gEfiAcpiVariableGuid
|
2021-12-05 23:54:17 +01:00
|
|
|
));
|
2018-09-10 05:13:36 +02:00
|
|
|
CpuDeadLoop ();
|
|
|
|
} else {
|
2021-12-05 23:54:17 +01:00
|
|
|
SmramDescriptor = (EFI_SMRAM_DESCRIPTOR *)GET_GUID_HOB_DATA (GuidHob);
|
2016-07-19 10:44:16 +02:00
|
|
|
|
2021-11-17 04:21:42 +01:00
|
|
|
DEBUG ((DEBUG_INFO, "SMM S3 SMRAM Structure = %x\n", SmramDescriptor));
|
|
|
|
DEBUG ((DEBUG_INFO, "SMM S3 Structure = %x\n", SmramDescriptor->CpuStart));
|
2016-07-19 10:44:16 +02:00
|
|
|
|
|
|
|
SmmS3ResumeState = (SMM_S3_RESUME_STATE *)(UINTN)SmramDescriptor->CpuStart;
|
|
|
|
ZeroMem (SmmS3ResumeState, sizeof (SMM_S3_RESUME_STATE));
|
|
|
|
|
2021-12-05 23:54:17 +01:00
|
|
|
mSmmS3ResumeState = SmmS3ResumeState;
|
2016-07-19 10:44:16 +02:00
|
|
|
SmmS3ResumeState->Smst = (EFI_PHYSICAL_ADDRESS)(UINTN)gSmst;
|
|
|
|
|
|
|
|
SmmS3ResumeState->SmmS3ResumeEntryPoint = (EFI_PHYSICAL_ADDRESS)(UINTN)SmmRestoreCpu;
|
|
|
|
|
|
|
|
SmmS3ResumeState->SmmS3StackSize = SIZE_32KB;
|
|
|
|
SmmS3ResumeState->SmmS3StackBase = (EFI_PHYSICAL_ADDRESS)(UINTN)AllocatePages (EFI_SIZE_TO_PAGES ((UINTN)SmmS3ResumeState->SmmS3StackSize));
|
|
|
|
if (SmmS3ResumeState->SmmS3StackBase == 0) {
|
|
|
|
SmmS3ResumeState->SmmS3StackSize = 0;
|
|
|
|
}
|
|
|
|
|
UefiCpuPkg/PiSmmCpuDxeSmm: patch "gSmmCr0" with PatchInstructionX86()
Like "gSmmCr4" in the previous patch, "gSmmCr0" is not only used for
machine code patching, but also as a means to communicate the initial CR0
value from SmmRelocateBases() to InitSmmS3ResumeState(). In other words,
the last four bytes of the "mov eax, Cr0Value" instruction's binary
representation are utilized as normal data too.
In order to get rid of the DB for "mov eax, Cr0Value", we have to split
both roles, patching and data flow. Introduce the "mSmmCr0" global (SMRAM)
variable for the data flow purpose. Rename the "gSmmCr0" variable to
"gPatchSmmCr0" so that its association with PatchInstructionX86() is clear
from the declaration, change its type to X86_ASSEMBLY_PATCH_LABEL, and
patch it with PatchInstructionX86(), to the value now contained in
"mSmmCr0".
This lets us remove the binary (DB) encoding of "mov eax, Cr0Value" in
"SmmInit.nasm".
Cc: Eric Dong <eric.dong@intel.com>
Cc: Michael D Kinney <michael.d.kinney@intel.com>
Ref: https://bugzilla.tianocore.org/show_bug.cgi?id=866
Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Laszlo Ersek <lersek@redhat.com>
Reviewed-by: Liming Gao <liming.gao@intel.com>
2018-02-02 02:10:05 +01:00
|
|
|
SmmS3ResumeState->SmmS3Cr0 = mSmmCr0;
|
2016-07-19 10:44:16 +02:00
|
|
|
SmmS3ResumeState->SmmS3Cr3 = Cr3;
|
UefiCpuPkg/PiSmmCpuDxeSmm: patch "gSmmCr4" with PatchInstructionX86()
Unlike "gSmmCr3" in the previous patch, "gSmmCr4" is not only used for
machine code patching, but also as a means to communicate the initial CR4
value from SmmRelocateBases() to InitSmmS3ResumeState(). In other words,
the last four bytes of the "mov eax, Cr4Value" instruction's binary
representation are utilized as normal data too.
In order to get rid of the DB for "mov eax, Cr4Value", we have to split
both roles, patching and data flow. Introduce the "mSmmCr4" global (SMRAM)
variable for the data flow purpose. Rename the "gSmmCr4" variable to
"gPatchSmmCr4" so that its association with PatchInstructionX86() is clear
from the declaration, change its type to X86_ASSEMBLY_PATCH_LABEL, and
patch it with PatchInstructionX86(), to the value now contained in
"mSmmCr4".
This lets us remove the binary (DB) encoding of "mov eax, Cr4Value" in
"SmmInit.nasm".
Cc: Eric Dong <eric.dong@intel.com>
Cc: Michael D Kinney <michael.d.kinney@intel.com>
Ref: https://bugzilla.tianocore.org/show_bug.cgi?id=866
Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Laszlo Ersek <lersek@redhat.com>
Reviewed-by: Liming Gao <liming.gao@intel.com>
2018-02-02 02:10:05 +01:00
|
|
|
SmmS3ResumeState->SmmS3Cr4 = mSmmCr4;
|
2016-07-19 10:44:16 +02:00
|
|
|
|
|
|
|
if (sizeof (UINTN) == sizeof (UINT64)) {
|
|
|
|
SmmS3ResumeState->Signature = SMM_S3_RESUME_SMM_64;
|
|
|
|
}
|
2021-12-05 23:54:17 +01:00
|
|
|
|
2016-07-19 10:44:16 +02:00
|
|
|
if (sizeof (UINTN) == sizeof (UINT32)) {
|
|
|
|
SmmS3ResumeState->Signature = SMM_S3_RESUME_SMM_32;
|
|
|
|
}
|
|
|
|
|
2018-09-14 07:40:37 +02:00
|
|
|
//
|
|
|
|
// Patch SmmS3ResumeState->SmmS3Cr3
|
|
|
|
//
|
|
|
|
InitSmmS3Cr3 ();
|
|
|
|
}
|
2016-11-10 06:40:12 +01:00
|
|
|
|
|
|
|
//
|
|
|
|
// Allocate safe memory in ACPI NVS for AP to execute hlt loop in
|
|
|
|
// protected mode on S3 path
|
|
|
|
//
|
|
|
|
Address = BASE_4GB - 1;
|
|
|
|
Status = gBS->AllocatePages (
|
|
|
|
AllocateMaxAddress,
|
|
|
|
EfiACPIMemoryNVS,
|
|
|
|
EFI_SIZE_TO_PAGES (sizeof (mApHltLoopCodeTemplate)),
|
|
|
|
&Address
|
|
|
|
);
|
|
|
|
ASSERT_EFI_ERROR (Status);
|
2021-12-05 23:54:17 +01:00
|
|
|
mApHltLoopCode = (UINT8 *)(UINTN)Address;
|
2016-07-19 10:44:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-01-11 02:54:19 +01:00
|
|
|
Copy register table from non-SMRAM into SMRAM.
|
2016-07-19 10:44:16 +02:00
|
|
|
|
|
|
|
@param[in] DestinationRegisterTableList Points to destination register table.
|
|
|
|
@param[in] SourceRegisterTableList Points to source register table.
|
|
|
|
@param[in] NumberOfCpus Number of CPUs.
|
|
|
|
|
|
|
|
**/
|
|
|
|
VOID
|
|
|
|
CopyRegisterTable (
|
2021-12-05 23:54:17 +01:00
|
|
|
IN CPU_REGISTER_TABLE *DestinationRegisterTableList,
|
|
|
|
IN CPU_REGISTER_TABLE *SourceRegisterTableList,
|
|
|
|
IN UINT32 NumberOfCpus
|
2016-07-19 10:44:16 +02:00
|
|
|
)
|
|
|
|
{
|
2021-12-05 23:54:17 +01:00
|
|
|
UINTN Index;
|
|
|
|
CPU_REGISTER_TABLE_ENTRY *RegisterTableEntry;
|
2016-07-19 10:44:16 +02:00
|
|
|
|
|
|
|
CopyMem (DestinationRegisterTableList, SourceRegisterTableList, NumberOfCpus * sizeof (CPU_REGISTER_TABLE));
|
|
|
|
for (Index = 0; Index < NumberOfCpus; Index++) {
|
2021-01-11 02:54:19 +01:00
|
|
|
if (DestinationRegisterTableList[Index].TableLength != 0) {
|
|
|
|
DestinationRegisterTableList[Index].AllocatedSize = DestinationRegisterTableList[Index].TableLength * sizeof (CPU_REGISTER_TABLE_ENTRY);
|
2021-12-05 23:54:17 +01:00
|
|
|
RegisterTableEntry = AllocateCopyPool (
|
|
|
|
DestinationRegisterTableList[Index].AllocatedSize,
|
|
|
|
(VOID *)(UINTN)SourceRegisterTableList[Index].RegisterTableEntry
|
|
|
|
);
|
2017-03-07 13:01:51 +01:00
|
|
|
ASSERT (RegisterTableEntry != NULL);
|
|
|
|
DestinationRegisterTableList[Index].RegisterTableEntry = (EFI_PHYSICAL_ADDRESS)(UINTN)RegisterTableEntry;
|
2016-07-19 10:44:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-11 02:54:19 +01:00
|
|
|
/**
|
|
|
|
Check whether the register table is empty or not.
|
|
|
|
|
|
|
|
@param[in] RegisterTable Point to the register table.
|
|
|
|
@param[in] NumberOfCpus Number of CPUs.
|
|
|
|
|
|
|
|
@retval TRUE The register table is empty.
|
|
|
|
@retval FALSE The register table is not empty.
|
|
|
|
**/
|
|
|
|
BOOLEAN
|
|
|
|
IsRegisterTableEmpty (
|
2021-12-05 23:54:17 +01:00
|
|
|
IN CPU_REGISTER_TABLE *RegisterTable,
|
|
|
|
IN UINT32 NumberOfCpus
|
2021-01-11 02:54:19 +01:00
|
|
|
)
|
|
|
|
{
|
2021-12-05 23:54:17 +01:00
|
|
|
UINTN Index;
|
2021-01-11 02:54:19 +01:00
|
|
|
|
|
|
|
if (RegisterTable != NULL) {
|
|
|
|
for (Index = 0; Index < NumberOfCpus; Index++) {
|
|
|
|
if (RegisterTable[Index].TableLength != 0) {
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2021-09-16 11:27:11 +02:00
|
|
|
/**
|
|
|
|
Copy the data used to initialize processor register into SMRAM.
|
|
|
|
|
|
|
|
@param[in,out] CpuFeatureInitDataDst Pointer to the destination CPU_FEATURE_INIT_DATA structure.
|
|
|
|
@param[in] CpuFeatureInitDataSrc Pointer to the source CPU_FEATURE_INIT_DATA structure.
|
|
|
|
|
|
|
|
**/
|
|
|
|
VOID
|
|
|
|
CopyCpuFeatureInitDatatoSmram (
|
2021-12-05 23:54:17 +01:00
|
|
|
IN OUT CPU_FEATURE_INIT_DATA *CpuFeatureInitDataDst,
|
|
|
|
IN CPU_FEATURE_INIT_DATA *CpuFeatureInitDataSrc
|
2021-09-16 11:27:11 +02:00
|
|
|
)
|
|
|
|
{
|
2021-12-05 23:54:17 +01:00
|
|
|
CPU_STATUS_INFORMATION *CpuStatus;
|
2021-09-16 11:27:11 +02:00
|
|
|
|
|
|
|
if (!IsRegisterTableEmpty ((CPU_REGISTER_TABLE *)(UINTN)CpuFeatureInitDataSrc->PreSmmInitRegisterTable, mAcpiCpuData.NumberOfCpus)) {
|
|
|
|
CpuFeatureInitDataDst->PreSmmInitRegisterTable = (EFI_PHYSICAL_ADDRESS)(UINTN)AllocatePool (mAcpiCpuData.NumberOfCpus * sizeof (CPU_REGISTER_TABLE));
|
|
|
|
ASSERT (CpuFeatureInitDataDst->PreSmmInitRegisterTable != 0);
|
|
|
|
|
|
|
|
CopyRegisterTable (
|
|
|
|
(CPU_REGISTER_TABLE *)(UINTN)CpuFeatureInitDataDst->PreSmmInitRegisterTable,
|
|
|
|
(CPU_REGISTER_TABLE *)(UINTN)CpuFeatureInitDataSrc->PreSmmInitRegisterTable,
|
|
|
|
mAcpiCpuData.NumberOfCpus
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!IsRegisterTableEmpty ((CPU_REGISTER_TABLE *)(UINTN)CpuFeatureInitDataSrc->RegisterTable, mAcpiCpuData.NumberOfCpus)) {
|
|
|
|
CpuFeatureInitDataDst->RegisterTable = (EFI_PHYSICAL_ADDRESS)(UINTN)AllocatePool (mAcpiCpuData.NumberOfCpus * sizeof (CPU_REGISTER_TABLE));
|
|
|
|
ASSERT (CpuFeatureInitDataDst->RegisterTable != 0);
|
|
|
|
|
|
|
|
CopyRegisterTable (
|
|
|
|
(CPU_REGISTER_TABLE *)(UINTN)CpuFeatureInitDataDst->RegisterTable,
|
|
|
|
(CPU_REGISTER_TABLE *)(UINTN)CpuFeatureInitDataSrc->RegisterTable,
|
|
|
|
mAcpiCpuData.NumberOfCpus
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
CpuStatus = &CpuFeatureInitDataDst->CpuStatus;
|
|
|
|
CopyMem (CpuStatus, &CpuFeatureInitDataSrc->CpuStatus, sizeof (CPU_STATUS_INFORMATION));
|
|
|
|
|
|
|
|
if (CpuFeatureInitDataSrc->CpuStatus.ThreadCountPerPackage != 0) {
|
|
|
|
CpuStatus->ThreadCountPerPackage = (EFI_PHYSICAL_ADDRESS)(UINTN)AllocateCopyPool (
|
2021-12-05 23:54:17 +01:00
|
|
|
sizeof (UINT32) * CpuStatus->PackageCount,
|
|
|
|
(UINT32 *)(UINTN)CpuFeatureInitDataSrc->CpuStatus.ThreadCountPerPackage
|
|
|
|
);
|
2021-09-16 11:27:11 +02:00
|
|
|
ASSERT (CpuStatus->ThreadCountPerPackage != 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (CpuFeatureInitDataSrc->CpuStatus.ThreadCountPerCore != 0) {
|
|
|
|
CpuStatus->ThreadCountPerCore = (EFI_PHYSICAL_ADDRESS)(UINTN)AllocateCopyPool (
|
2021-12-05 23:54:17 +01:00
|
|
|
sizeof (UINT8) * (CpuStatus->PackageCount * CpuStatus->MaxCoreCount),
|
|
|
|
(UINT32 *)(UINTN)CpuFeatureInitDataSrc->CpuStatus.ThreadCountPerCore
|
|
|
|
);
|
2021-09-16 11:27:11 +02:00
|
|
|
ASSERT (CpuStatus->ThreadCountPerCore != 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (CpuFeatureInitDataSrc->ApLocation != 0) {
|
|
|
|
CpuFeatureInitDataDst->ApLocation = (EFI_PHYSICAL_ADDRESS)(UINTN)AllocateCopyPool (
|
2021-12-05 23:54:17 +01:00
|
|
|
mAcpiCpuData.NumberOfCpus * sizeof (EFI_CPU_PHYSICAL_LOCATION),
|
|
|
|
(EFI_CPU_PHYSICAL_LOCATION *)(UINTN)CpuFeatureInitDataSrc->ApLocation
|
|
|
|
);
|
2021-09-16 11:27:11 +02:00
|
|
|
ASSERT (CpuFeatureInitDataDst->ApLocation != 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-19 10:44:16 +02:00
|
|
|
/**
|
|
|
|
Get ACPI CPU data.
|
|
|
|
|
|
|
|
**/
|
|
|
|
VOID
|
|
|
|
GetAcpiCpuData (
|
|
|
|
VOID
|
|
|
|
)
|
|
|
|
{
|
2021-12-05 23:54:17 +01:00
|
|
|
ACPI_CPU_DATA *AcpiCpuData;
|
|
|
|
IA32_DESCRIPTOR *Gdtr;
|
|
|
|
IA32_DESCRIPTOR *Idtr;
|
|
|
|
VOID *GdtForAp;
|
|
|
|
VOID *IdtForAp;
|
|
|
|
VOID *MachineCheckHandlerForAp;
|
|
|
|
CPU_STATUS_INFORMATION *CpuStatus;
|
2016-07-19 10:44:16 +02:00
|
|
|
|
2016-07-20 04:24:58 +02:00
|
|
|
if (!mAcpiS3Enable) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-07-19 10:44:16 +02:00
|
|
|
//
|
|
|
|
// Prevent use of mAcpiCpuData by initialize NumberOfCpus to 0
|
|
|
|
//
|
|
|
|
mAcpiCpuData.NumberOfCpus = 0;
|
|
|
|
|
|
|
|
//
|
|
|
|
// If PcdCpuS3DataAddress was never set, then do not copy CPU S3 Data into SMRAM
|
|
|
|
//
|
|
|
|
AcpiCpuData = (ACPI_CPU_DATA *)(UINTN)PcdGet64 (PcdCpuS3DataAddress);
|
|
|
|
if (AcpiCpuData == 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// For a native platform, copy the CPU S3 data into SMRAM for use on CPU S3 Resume.
|
|
|
|
//
|
|
|
|
CopyMem (&mAcpiCpuData, AcpiCpuData, sizeof (mAcpiCpuData));
|
|
|
|
|
|
|
|
mAcpiCpuData.MtrrTable = (EFI_PHYSICAL_ADDRESS)(UINTN)AllocatePool (sizeof (MTRR_SETTINGS));
|
|
|
|
ASSERT (mAcpiCpuData.MtrrTable != 0);
|
|
|
|
|
|
|
|
CopyMem ((VOID *)(UINTN)mAcpiCpuData.MtrrTable, (VOID *)(UINTN)AcpiCpuData->MtrrTable, sizeof (MTRR_SETTINGS));
|
|
|
|
|
|
|
|
mAcpiCpuData.GdtrProfile = (EFI_PHYSICAL_ADDRESS)(UINTN)AllocatePool (sizeof (IA32_DESCRIPTOR));
|
|
|
|
ASSERT (mAcpiCpuData.GdtrProfile != 0);
|
|
|
|
|
|
|
|
CopyMem ((VOID *)(UINTN)mAcpiCpuData.GdtrProfile, (VOID *)(UINTN)AcpiCpuData->GdtrProfile, sizeof (IA32_DESCRIPTOR));
|
|
|
|
|
|
|
|
mAcpiCpuData.IdtrProfile = (EFI_PHYSICAL_ADDRESS)(UINTN)AllocatePool (sizeof (IA32_DESCRIPTOR));
|
|
|
|
ASSERT (mAcpiCpuData.IdtrProfile != 0);
|
|
|
|
|
|
|
|
CopyMem ((VOID *)(UINTN)mAcpiCpuData.IdtrProfile, (VOID *)(UINTN)AcpiCpuData->IdtrProfile, sizeof (IA32_DESCRIPTOR));
|
|
|
|
|
|
|
|
//
|
|
|
|
// Copy AP's GDT, IDT and Machine Check handler into SMRAM.
|
|
|
|
//
|
|
|
|
Gdtr = (IA32_DESCRIPTOR *)(UINTN)mAcpiCpuData.GdtrProfile;
|
|
|
|
Idtr = (IA32_DESCRIPTOR *)(UINTN)mAcpiCpuData.IdtrProfile;
|
|
|
|
|
2021-09-16 11:27:11 +02:00
|
|
|
GdtForAp = AllocatePool ((Gdtr->Limit + 1) + (Idtr->Limit + 1) + mAcpiCpuData.ApMachineCheckHandlerSize);
|
2018-08-10 04:27:42 +02:00
|
|
|
ASSERT (GdtForAp != NULL);
|
2021-12-05 23:54:17 +01:00
|
|
|
IdtForAp = (VOID *)((UINTN)GdtForAp + (Gdtr->Limit + 1));
|
|
|
|
MachineCheckHandlerForAp = (VOID *)((UINTN)IdtForAp + (Idtr->Limit + 1));
|
2018-08-10 04:27:42 +02:00
|
|
|
|
|
|
|
CopyMem (GdtForAp, (VOID *)Gdtr->Base, Gdtr->Limit + 1);
|
|
|
|
CopyMem (IdtForAp, (VOID *)Idtr->Base, Idtr->Limit + 1);
|
|
|
|
CopyMem (MachineCheckHandlerForAp, (VOID *)(UINTN)mAcpiCpuData.ApMachineCheckHandlerBase, mAcpiCpuData.ApMachineCheckHandlerSize);
|
2016-07-19 10:44:16 +02:00
|
|
|
|
2021-12-05 23:54:17 +01:00
|
|
|
Gdtr->Base = (UINTN)GdtForAp;
|
|
|
|
Idtr->Base = (UINTN)IdtForAp;
|
2018-08-10 04:27:42 +02:00
|
|
|
mAcpiCpuData.ApMachineCheckHandlerBase = (EFI_PHYSICAL_ADDRESS)(UINTN)MachineCheckHandlerForAp;
|
2018-10-15 04:34:59 +02:00
|
|
|
|
2021-09-16 11:27:11 +02:00
|
|
|
ZeroMem (&mAcpiCpuData.CpuFeatureInitData, sizeof (CPU_FEATURE_INIT_DATA));
|
|
|
|
|
2021-09-16 11:27:12 +02:00
|
|
|
if (!PcdGetBool (PcdCpuFeaturesInitOnS3Resume)) {
|
|
|
|
//
|
|
|
|
// If the CPU features will not be initialized by CpuFeaturesPei module during
|
|
|
|
// next ACPI S3 resume, copy the CPU features initialization data into SMRAM,
|
|
|
|
// which will be consumed in SmmRestoreCpu during next S3 resume.
|
|
|
|
//
|
|
|
|
CopyCpuFeatureInitDatatoSmram (&mAcpiCpuData.CpuFeatureInitData, &AcpiCpuData->CpuFeatureInitData);
|
2021-09-16 11:27:11 +02:00
|
|
|
|
2021-09-16 11:27:12 +02:00
|
|
|
CpuStatus = &mAcpiCpuData.CpuFeatureInitData.CpuStatus;
|
2021-09-16 11:27:11 +02:00
|
|
|
|
2021-09-16 11:27:12 +02:00
|
|
|
mCpuFlags.CoreSemaphoreCount = AllocateZeroPool (
|
|
|
|
sizeof (UINT32) * CpuStatus->PackageCount *
|
|
|
|
CpuStatus->MaxCoreCount * CpuStatus->MaxThreadCount
|
|
|
|
);
|
|
|
|
ASSERT (mCpuFlags.CoreSemaphoreCount != NULL);
|
2021-09-16 11:27:11 +02:00
|
|
|
|
2021-09-16 11:27:12 +02:00
|
|
|
mCpuFlags.PackageSemaphoreCount = AllocateZeroPool (
|
|
|
|
sizeof (UINT32) * CpuStatus->PackageCount *
|
|
|
|
CpuStatus->MaxCoreCount * CpuStatus->MaxThreadCount
|
|
|
|
);
|
|
|
|
ASSERT (mCpuFlags.PackageSemaphoreCount != NULL);
|
|
|
|
|
2021-12-05 23:54:17 +01:00
|
|
|
InitializeSpinLock ((SPIN_LOCK *)&mCpuFlags.MemoryMappedLock);
|
2021-09-16 11:27:12 +02:00
|
|
|
}
|
2016-07-19 10:44:16 +02:00
|
|
|
}
|
2016-07-20 04:24:58 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
Get ACPI S3 enable flag.
|
|
|
|
|
|
|
|
**/
|
|
|
|
VOID
|
|
|
|
GetAcpiS3EnableFlag (
|
|
|
|
VOID
|
|
|
|
)
|
|
|
|
{
|
|
|
|
mAcpiS3Enable = PcdGetBool (PcdAcpiS3Enable);
|
|
|
|
}
|