TianoCore EDK2 master
Loading...
Searching...
No Matches
AmdSmmCpuFeaturesLib.c
Go to the documentation of this file.
1
12#include <Guid/SmmBaseHob.h>
14#include <Uefi/UefiBaseType.h>
16#include <Library/BaseLib.h>
17#include <Library/DebugLib.h>
19#include <Library/HobLib.h>
20#include <Register/Amd/Msr.h>
21
22// EFER register LMA bit
23#define LMA BIT10
24
25// The mode of the CPU at the time an SMI occurs
27
28//
29// Indicate SmBase for each Processors has been relocated or not. If TRUE,
30// means no need to do the relocation in SmmCpuFeaturesInitializeProcessor().
31//
32BOOLEAN mSmmCpuFeaturesSmmRelocated;
33
41VOID
43 VOID
44 )
45{
46 //
47 // AMD64 processors support EFI_SMM_SAVE_STATE_REGISTER_LMA_64BIT only
48 //
49 mSmmSaveStateRegisterLma = EFI_SMM_SAVE_STATE_REGISTER_LMA_64BIT;
50
51 //
52 // If gSmmBaseHobGuid found, means SmBase info has been relocated and recorded
53 // in the SmBase array.
54 //
55 mSmmCpuFeaturesSmmRelocated = (BOOLEAN)(GetFirstGuidHob (&gSmmBaseHobGuid) != NULL);
56}
57
82VOID
83EFIAPI
85 IN UINTN CpuIndex,
86 IN BOOLEAN IsMonarch,
87 IN EFI_PROCESSOR_INFORMATION *ProcessorInfo,
88 IN CPU_HOT_PLUG_DATA *CpuHotPlugData
89 )
90{
92
93 //
94 // No need to configure SMBASE if SmBase relocation has been done.
95 //
96 if (!mSmmCpuFeaturesSmmRelocated) {
97 //
98 // Configure SMBASE.
99 //
101 CpuState->x64.SMBASE = (UINT32)CpuHotPlugData->SmBase[CpuIndex];
102 }
103
104 // Re-initialize the value of mSmmSaveStateRegisterLma flag which might have been changed in PiCpuSmmDxeSmm Driver
105 // Entry point, to make sure correct value on AMD platform is assigned to be used by SmmCpuFeaturesLib.
106 mSmmSaveStateRegisterLma = EFI_SMM_SAVE_STATE_REGISTER_LMA_64BIT;
107
108 //
109 // If SMRR is supported, then program SMRR base/mask MSRs.
110 // The EFI_MSR_SMRR_PHYS_MASK_VALID bit is not set until the first normal SMI.
111 // The code that initializes SMM environment is running in normal mode
112 // from SMRAM region. If SMRR is enabled here, then the SMRAM region
113 // is protected and the normal mode code execution will fail.
114 //
115 if (FeaturePcdGet (PcdSmrrEnable)) {
116 //
117 // SMRR size cannot be less than 4-KBytes
118 // SMRR size must be of length 2^n
119 // SMRR base alignment cannot be less than SMRR length
120 //
121 if ((CpuHotPlugData->SmrrSize < SIZE_4KB) ||
122 (CpuHotPlugData->SmrrSize != GetPowerOfTwo32 (CpuHotPlugData->SmrrSize)) ||
123 ((CpuHotPlugData->SmrrBase & ~(CpuHotPlugData->SmrrSize - 1)) != CpuHotPlugData->SmrrBase))
124 {
125 //
126 // Print message and halt if CPU is Monarch
127 //
128 if (IsMonarch) {
129 DEBUG ((DEBUG_ERROR, "SMM Base/Size does not meet alignment/size requirement!\n"));
130 CpuDeadLoop ();
131 }
132 } else {
133 AsmWriteMsr64 (AMD_64_SMM_ADDR, CpuHotPlugData->SmrrBase);
134 AsmWriteMsr64 (AMD_64_SMM_MASK, ((~(UINT64)(CpuHotPlugData->SmrrSize - 1)) | 0x6600));
135 }
136 }
137}
138
170UINT64
171EFIAPI
173 IN UINTN CpuIndex,
174 IN SMRAM_SAVE_STATE_MAP *CpuState,
175 IN UINT64 NewInstructionPointer32,
176 IN UINT64 NewInstructionPointer
177 )
178{
179 UINT64 OriginalInstructionPointer;
180 AMD_SMRAM_SAVE_STATE_MAP *AmdCpuState;
181
182 AmdCpuState = (AMD_SMRAM_SAVE_STATE_MAP *)CpuState;
183
184 OriginalInstructionPointer = AmdCpuState->x64._RIP;
185
186 if ((AmdCpuState->x64.EFER & LMA) == 0) {
187 AmdCpuState->x64._RIP = NewInstructionPointer32;
188 } else {
189 AmdCpuState->x64._RIP = NewInstructionPointer;
190 }
191
192 //
193 // Clear the auto HALT restart flag so the RSM instruction returns
194 // program control to the instruction following the HLT instruction.
195 //
196 if ((AmdCpuState->x64.AutoHALTRestart & BIT0) != 0) {
197 AmdCpuState->x64.AutoHALTRestart &= ~BIT0;
198 }
199
200 return OriginalInstructionPointer;
201}
202
213UINTN
214EFIAPI
216 VOID
217 )
218{
219 return 0;
220}
221
247VOID
248EFIAPI
250 IN UINTN CpuIndex,
251 IN UINT32 SmBase,
252 IN VOID *SmiStack,
253 IN UINTN StackSize,
254 IN UINTN GdtBase,
255 IN UINTN GdtSize,
256 IN UINTN IdtBase,
257 IN UINTN IdtSize,
258 IN UINT32 Cr3
259 )
260{
261}
262
271BOOLEAN
272EFIAPI
274 VOID
275 )
276{
277 return FALSE;
278}
279
284VOID
285EFIAPI
287 VOID
288 )
289{
290}
291
296VOID
297EFIAPI
299 VOID
300 )
301{
302}
303
311VOID
312EFIAPI
314 IN UINTN CpuIndex
315 )
316{
317}
318
331UINT64
332EFIAPI
334 IN UINTN CpuIndex,
335 IN SMM_REG_NAME RegName
336 )
337{
338 return 0;
339}
340
352VOID
353EFIAPI
355 IN UINTN CpuIndex,
356 IN SMM_REG_NAME RegName,
357 IN UINT64 Value
358 )
359{
360}
361
375BOOLEAN
376EFIAPI
378 IN UINTN CpuIndex,
379 IN SMM_REG_NAME RegName
380 )
381{
382 return FALSE;
383}
UINT64 UINTN
UINT64 EFIAPI SmmCpuFeaturesGetSmmRegister(IN UINTN CpuIndex, IN SMM_REG_NAME RegName)
UINT64 EFIAPI SmmCpuFeaturesHookReturnFromSmm(IN UINTN CpuIndex, IN SMRAM_SAVE_STATE_MAP *CpuState, IN UINT64 NewInstructionPointer32, IN UINT64 NewInstructionPointer)
STATIC UINT8 mSmmSaveStateRegisterLma
VOID EFIAPI SmmCpuFeaturesInitializeProcessor(IN UINTN CpuIndex, IN BOOLEAN IsMonarch, IN EFI_PROCESSOR_INFORMATION *ProcessorInfo, IN CPU_HOT_PLUG_DATA *CpuHotPlugData)
VOID EFIAPI SmmCpuFeaturesRendezvousEntry(IN UINTN CpuIndex)
VOID EFIAPI SmmCpuFeaturesReenableSmrr(VOID)
BOOLEAN EFIAPI SmmCpuFeaturesIsSmmRegisterSupported(IN UINTN CpuIndex, IN SMM_REG_NAME RegName)
VOID EFIAPI SmmCpuFeaturesSetSmmRegister(IN UINTN CpuIndex, IN SMM_REG_NAME RegName, IN UINT64 Value)
BOOLEAN EFIAPI SmmCpuFeaturesNeedConfigureMtrrs(VOID)
VOID EFIAPI SmmCpuFeaturesInstallSmiHandler(IN UINTN CpuIndex, IN UINT32 SmBase, IN VOID *SmiStack, IN UINTN StackSize, IN UINTN GdtBase, IN UINTN GdtSize, IN UINTN IdtBase, IN UINTN IdtSize, IN UINT32 Cr3)
VOID CpuFeaturesLibInitialization(VOID)
VOID EFIAPI SmmCpuFeaturesDisableSmrr(VOID)
UINTN EFIAPI SmmCpuFeaturesGetSmiHandlerSize(VOID)
VOID *EFIAPI GetFirstGuidHob(IN CONST EFI_GUID *Guid)
Definition: HobLib.c:215
UINT32 EFIAPI GetPowerOfTwo32(IN UINT32 Operand)
VOID EFIAPI CpuDeadLoop(VOID)
Definition: CpuDeadLoop.c:25
UINT64 EFIAPI AsmWriteMsr64(IN UINT32 Index, IN UINT64 Value)
#define NULL
Definition: Base.h:319
#define STATIC
Definition: Base.h:264
#define FALSE
Definition: Base.h:307
#define IN
Definition: Base.h:279
#define DEBUG(Expression)
Definition: DebugLib.h:434
#define SMM_DEFAULT_SMBASE
#define SMRAM_SAVE_STATE_MAP_OFFSET
#define FeaturePcdGet(TokenName)
Definition: PcdLib.h:50
SMM_REG_NAME