]> git.proxmox.com Git - mirror_edk2.git/blame - UefiCpuPkg/PiSmmCpuDxeSmm/PiSmmCpuDxeSmm.h
UefiCpuPkg/PiSmmCpuDxeSmm: Remove mInternalCr3 in PiSmmCpuDxeSmm
[mirror_edk2.git] / UefiCpuPkg / PiSmmCpuDxeSmm / PiSmmCpuDxeSmm.h
CommitLineData
529a5a86
MK
1/** @file\r
2Agent Module to load other modules to deploy SMM Entry Vector for X86 CPU.\r
3\r
4a68176c 4Copyright (c) 2009 - 2022, Intel Corporation. All rights reserved.<BR>\r
241f9149
LD
5Copyright (c) 2017, AMD Incorporated. All rights reserved.<BR>\r
6\r
0acd8697 7SPDX-License-Identifier: BSD-2-Clause-Patent\r
529a5a86
MK
8\r
9**/\r
10\r
11#ifndef _CPU_PISMMCPUDXESMM_H_\r
12#define _CPU_PISMMCPUDXESMM_H_\r
13\r
14#include <PiSmm.h>\r
15\r
16#include <Protocol/MpService.h>\r
17#include <Protocol/SmmConfiguration.h>\r
18#include <Protocol/SmmCpu.h>\r
19#include <Protocol/SmmAccess2.h>\r
529a5a86
MK
20#include <Protocol/SmmReadyToLock.h>\r
21#include <Protocol/SmmCpuService.h>\r
827330cc 22#include <Protocol/SmmMemoryAttribute.h>\r
51dd408a 23#include <Protocol/MmMp.h>\r
529a5a86
MK
24\r
25#include <Guid/AcpiS3Context.h>\r
8a2e1a9d 26#include <Guid/MemoryAttributesTable.h>\r
717fb604 27#include <Guid/PiSmmMemoryAttributesTable.h>\r
529a5a86
MK
28\r
29#include <Library/BaseLib.h>\r
30#include <Library/IoLib.h>\r
31#include <Library/TimerLib.h>\r
529a5a86
MK
32#include <Library/SynchronizationLib.h>\r
33#include <Library/DebugLib.h>\r
34#include <Library/BaseMemoryLib.h>\r
35#include <Library/PcdLib.h>\r
529a5a86
MK
36#include <Library/MtrrLib.h>\r
37#include <Library/SmmCpuPlatformHookLib.h>\r
38#include <Library/SmmServicesTableLib.h>\r
39#include <Library/MemoryAllocationLib.h>\r
40#include <Library/UefiBootServicesTableLib.h>\r
41#include <Library/UefiRuntimeServicesTableLib.h>\r
42#include <Library/DebugAgentLib.h>\r
8a2e1a9d 43#include <Library/UefiLib.h>\r
529a5a86
MK
44#include <Library/HobLib.h>\r
45#include <Library/LocalApicLib.h>\r
86d41c07 46#include <Library/CpuLib.h>\r
529a5a86
MK
47#include <Library/UefiCpuLib.h>\r
48#include <Library/CpuExceptionHandlerLib.h>\r
49#include <Library/ReportStatusCodeLib.h>\r
50#include <Library/SmmCpuFeaturesLib.h>\r
51#include <Library/PeCoffGetEntryPointLib.h>\r
93324390 52#include <Library/RegisterCpuFeaturesLib.h>\r
529a5a86
MK
53\r
54#include <AcpiCpuData.h>\r
55#include <CpuHotPlugData.h>\r
56\r
01acb06c
RN
57#include <Register/Intel/Cpuid.h>\r
58#include <Register/Intel/Msr.h>\r
529a5a86
MK
59\r
60#include "CpuService.h"\r
61#include "SmmProfile.h"\r
62\r
3eb69b08
JY
63//\r
64// CET definition\r
65//\r
66#define CPUID_CET_SS BIT7\r
67#define CPUID_CET_IBT BIT20\r
68\r
69#define CR4_CET_ENABLE BIT23\r
70\r
71#define MSR_IA32_S_CET 0x6A2\r
72#define MSR_IA32_PL0_SSP 0x6A4\r
73#define MSR_IA32_INTERRUPT_SSP_TABLE_ADDR 0x6A8\r
74\r
75typedef union {\r
76 struct {\r
77 // enable shadow stacks\r
053e878b 78 UINT32 SH_STK_ENP : 1;\r
3eb69b08 79 // enable the WRSS{D,Q}W instructions.\r
053e878b 80 UINT32 WR_SHSTK_EN : 1;\r
3eb69b08 81 // enable tracking of indirect call/jmp targets to be ENDBRANCH instruction.\r
053e878b 82 UINT32 ENDBR_EN : 1;\r
3eb69b08 83 // enable legacy compatibility treatment for indirect call/jmp tracking.\r
053e878b 84 UINT32 LEG_IW_EN : 1;\r
3eb69b08 85 // enable use of no-track prefix on indirect call/jmp.\r
053e878b 86 UINT32 NO_TRACK_EN : 1;\r
3eb69b08 87 // disable suppression of CET indirect branch tracking on legacy compatibility.\r
053e878b
MK
88 UINT32 SUPPRESS_DIS : 1;\r
89 UINT32 RSVD : 4;\r
3eb69b08
JY
90 // indirect branch tracking is suppressed.\r
91 // This bit can be written to 1 only if TRACKER is written as IDLE.\r
053e878b 92 UINT32 SUPPRESS : 1;\r
3eb69b08
JY
93 // Value of the endbranch state machine\r
94 // Values: IDLE (0), WAIT_FOR_ENDBRANCH(1).\r
053e878b 95 UINT32 TRACKER : 1;\r
3eb69b08
JY
96 // linear address of a bitmap in memory indicating valid\r
97 // pages as target of CALL/JMP_indirect that do not land on ENDBRANCH when CET is enabled\r
98 // and not suppressed. Valid when ENDBR_EN is 1. Must be machine canonical when written on\r
99 // parts that support 64 bit mode. On parts that do not support 64 bit mode, the bits 63:32 are\r
100 // reserved and must be 0. This value is extended by 12 bits at the low end to form the base address\r
101 // (this automatically aligns the address on a 4-Kbyte boundary).\r
053e878b
MK
102 UINT32 EB_LEG_BITMAP_BASE_low : 12;\r
103 UINT32 EB_LEG_BITMAP_BASE_high : 32;\r
3eb69b08 104 } Bits;\r
053e878b 105 UINT64 Uint64;\r
3eb69b08
JY
106} MSR_IA32_CET;\r
107\r
529a5a86
MK
108//\r
109// MSRs required for configuration of SMM Code Access Check\r
110//\r
053e878b
MK
111#define EFI_MSR_SMM_MCA_CAP 0x17D\r
112#define SMM_CODE_ACCESS_CHK_BIT BIT58\r
529a5a86 113\r
053e878b
MK
114#define SMM_FEATURE_CONTROL_LOCK_BIT BIT0\r
115#define SMM_CODE_CHK_EN_BIT BIT2\r
529a5a86
MK
116\r
117///\r
118/// Page Table Entry\r
119///\r
053e878b
MK
120#define IA32_PG_P BIT0\r
121#define IA32_PG_RW BIT1\r
122#define IA32_PG_U BIT2\r
123#define IA32_PG_WT BIT3\r
124#define IA32_PG_CD BIT4\r
125#define IA32_PG_A BIT5\r
126#define IA32_PG_D BIT6\r
127#define IA32_PG_PS BIT7\r
128#define IA32_PG_PAT_2M BIT12\r
129#define IA32_PG_PAT_4K IA32_PG_PS\r
130#define IA32_PG_PMNT BIT62\r
131#define IA32_PG_NX BIT63\r
132\r
133#define PAGE_ATTRIBUTE_BITS (IA32_PG_D | IA32_PG_A | IA32_PG_U | IA32_PG_RW | IA32_PG_P)\r
881520ea
JY
134//\r
135// Bits 1, 2, 5, 6 are reserved in the IA32 PAE PDPTE\r
136// X64 PAE PDPTE does not have such restriction\r
137//\r
053e878b 138#define IA32_PAE_PDPTE_ATTRIBUTE_BITS (IA32_PG_P)\r
881520ea 139\r
053e878b 140#define PAGE_PROGATE_BITS (IA32_PG_NX | PAGE_ATTRIBUTE_BITS)\r
717fb604
JY
141\r
142#define PAGING_4K_MASK 0xFFF\r
143#define PAGING_2M_MASK 0x1FFFFF\r
144#define PAGING_1G_MASK 0x3FFFFFFF\r
145\r
146#define PAGING_PAE_INDEX_MASK 0x1FF\r
147\r
053e878b
MK
148#define PAGING_4K_ADDRESS_MASK_64 0x000FFFFFFFFFF000ull\r
149#define PAGING_2M_ADDRESS_MASK_64 0x000FFFFFFFE00000ull\r
150#define PAGING_1G_ADDRESS_MASK_64 0x000FFFFFC0000000ull\r
717fb604 151\r
053e878b 152#define SMRR_MAX_ADDRESS BASE_4GB\r
7ed6f781 153\r
717fb604
JY
154typedef enum {\r
155 PageNone,\r
156 Page4K,\r
157 Page2M,\r
158 Page1G,\r
159} PAGE_ATTRIBUTE;\r
160\r
161typedef struct {\r
053e878b
MK
162 PAGE_ATTRIBUTE Attribute;\r
163 UINT64 Length;\r
164 UINT64 AddressMask;\r
717fb604
JY
165} PAGE_ATTRIBUTE_TABLE;\r
166\r
529a5a86
MK
167//\r
168// Size of Task-State Segment defined in IA32 Manual\r
169//\r
053e878b
MK
170#define TSS_SIZE 104\r
171#define EXCEPTION_TSS_SIZE (TSS_SIZE + 4) // Add 4 bytes SSP\r
172#define TSS_X64_IST1_OFFSET 36\r
173#define TSS_IA32_CR3_OFFSET 28\r
174#define TSS_IA32_ESP_OFFSET 56\r
175#define TSS_IA32_SSP_OFFSET 104\r
529a5a86 176\r
053e878b 177#define CR0_WP BIT16\r
717fb604 178\r
529a5a86
MK
179//\r
180// Code select value\r
181//\r
053e878b
MK
182#define PROTECT_MODE_CODE_SEGMENT 0x08\r
183#define LONG_MODE_CODE_SEGMENT 0x38\r
529a5a86
MK
184\r
185//\r
186// The size 0x20 must be bigger than\r
187// the size of template code of SmmInit. Currently,\r
188// the size of SmmInit requires the 0x16 Bytes buffer\r
189// at least.\r
190//\r
191#define BACK_BUF_SIZE 0x20\r
192\r
053e878b 193#define EXCEPTION_VECTOR_NUMBER 0x20\r
529a5a86 194\r
053e878b 195#define INVALID_APIC_ID 0xFFFFFFFFFFFFFFFFULL\r
529a5a86 196\r
053e878b
MK
197typedef UINT32 SMM_CPU_ARRIVAL_EXCEPTIONS;\r
198#define ARRIVAL_EXCEPTION_BLOCKED 0x1\r
199#define ARRIVAL_EXCEPTION_DELAYED 0x2\r
200#define ARRIVAL_EXCEPTION_SMI_DISABLED 0x4\r
529a5a86 201\r
51dd408a
ED
202//\r
203// Wrapper used to convert EFI_AP_PROCEDURE2 and EFI_AP_PROCEDURE.\r
204//\r
205typedef struct {\r
053e878b
MK
206 EFI_AP_PROCEDURE Procedure;\r
207 VOID *ProcedureArgument;\r
51dd408a
ED
208} PROCEDURE_WRAPPER;\r
209\r
210#define PROCEDURE_TOKEN_SIGNATURE SIGNATURE_32 ('P', 'R', 'T', 'S')\r
211\r
212typedef struct {\r
053e878b
MK
213 UINTN Signature;\r
214 LIST_ENTRY Link;\r
51dd408a 215\r
053e878b
MK
216 SPIN_LOCK *SpinLock;\r
217 volatile UINT32 RunningApCount;\r
51dd408a
ED
218} PROCEDURE_TOKEN;\r
219\r
220#define PROCEDURE_TOKEN_FROM_LINK(a) CR (a, PROCEDURE_TOKEN, Link, PROCEDURE_TOKEN_SIGNATURE)\r
221\r
9caaa79d
ED
222#define TOKEN_BUFFER_SIGNATURE SIGNATURE_32 ('T', 'K', 'B', 'S')\r
223\r
224typedef struct {\r
053e878b
MK
225 UINTN Signature;\r
226 LIST_ENTRY Link;\r
9caaa79d 227\r
053e878b 228 UINT8 *Buffer;\r
9caaa79d
ED
229} TOKEN_BUFFER;\r
230\r
231#define TOKEN_BUFFER_FROM_LINK(a) CR (a, TOKEN_BUFFER, Link, TOKEN_BUFFER_SIGNATURE)\r
232\r
529a5a86
MK
233//\r
234// Private structure for the SMM CPU module that is stored in DXE Runtime memory\r
235// Contains the SMM Configuration Protocols that is produced.\r
236// Contains a mix of DXE and SMM contents. All the fields must be used properly.\r
237//\r
238#define SMM_CPU_PRIVATE_DATA_SIGNATURE SIGNATURE_32 ('s', 'c', 'p', 'u')\r
239\r
240typedef struct {\r
053e878b 241 UINTN Signature;\r
529a5a86 242\r
053e878b 243 EFI_HANDLE SmmCpuHandle;\r
529a5a86 244\r
053e878b
MK
245 EFI_PROCESSOR_INFORMATION *ProcessorInfo;\r
246 SMM_CPU_OPERATION *Operation;\r
247 UINTN *CpuSaveStateSize;\r
248 VOID **CpuSaveState;\r
529a5a86 249\r
053e878b
MK
250 EFI_SMM_RESERVED_SMRAM_REGION SmmReservedSmramRegion[1];\r
251 EFI_SMM_ENTRY_CONTEXT SmmCoreEntryContext;\r
252 EFI_SMM_ENTRY_POINT SmmCoreEntry;\r
529a5a86 253\r
053e878b 254 EFI_SMM_CONFIGURATION_PROTOCOL SmmConfiguration;\r
51dd408a 255\r
053e878b
MK
256 PROCEDURE_WRAPPER *ApWrapperFunc;\r
257 LIST_ENTRY TokenList;\r
258 LIST_ENTRY *FirstFreeToken;\r
529a5a86
MK
259} SMM_CPU_PRIVATE_DATA;\r
260\r
261extern SMM_CPU_PRIVATE_DATA *gSmmCpuPrivate;\r
053e878b
MK
262extern CPU_HOT_PLUG_DATA mCpuHotPlugData;\r
263extern UINTN mMaxNumberOfCpus;\r
264extern UINTN mNumberOfCpus;\r
265extern EFI_SMM_CPU_PROTOCOL mSmmCpu;\r
266extern EFI_MM_MP_PROTOCOL mSmmMp;\r
7b475490 267extern BOOLEAN m5LevelPagingNeeded;\r
529a5a86
MK
268\r
269///\r
270/// The mode of the CPU at the time an SMI occurs\r
271///\r
272extern UINT8 mSmmSaveStateRegisterLma;\r
273\r
529a5a86
MK
274//\r
275// SMM CPU Protocol function prototypes.\r
276//\r
277\r
278/**\r
279 Read information from the CPU save state.\r
280\r
281 @param This EFI_SMM_CPU_PROTOCOL instance\r
282 @param Width The number of bytes to read from the CPU save state.\r
283 @param Register Specifies the CPU register to read form the save state.\r
284 @param CpuIndex Specifies the zero-based index of the CPU save state\r
285 @param Buffer Upon return, this holds the CPU register value read from the save state.\r
286\r
287 @retval EFI_SUCCESS The register was read from Save State\r
288 @retval EFI_NOT_FOUND The register is not defined for the Save State of Processor\r
ef62da4f 289 @retval EFI_INVALID_PARAMETER This or Buffer is NULL.\r
529a5a86
MK
290\r
291**/\r
292EFI_STATUS\r
293EFIAPI\r
294SmmReadSaveState (\r
053e878b
MK
295 IN CONST EFI_SMM_CPU_PROTOCOL *This,\r
296 IN UINTN Width,\r
297 IN EFI_SMM_SAVE_STATE_REGISTER Register,\r
298 IN UINTN CpuIndex,\r
299 OUT VOID *Buffer\r
529a5a86
MK
300 );\r
301\r
302/**\r
303 Write data to the CPU save state.\r
304\r
305 @param This EFI_SMM_CPU_PROTOCOL instance\r
306 @param Width The number of bytes to read from the CPU save state.\r
307 @param Register Specifies the CPU register to write to the save state.\r
308 @param CpuIndex Specifies the zero-based index of the CPU save state\r
309 @param Buffer Upon entry, this holds the new CPU register value.\r
310\r
311 @retval EFI_SUCCESS The register was written from Save State\r
312 @retval EFI_NOT_FOUND The register is not defined for the Save State of Processor\r
ef62da4f 313 @retval EFI_INVALID_PARAMETER ProcessorIndex or Width is not correct\r
529a5a86
MK
314\r
315**/\r
316EFI_STATUS\r
317EFIAPI\r
318SmmWriteSaveState (\r
053e878b
MK
319 IN CONST EFI_SMM_CPU_PROTOCOL *This,\r
320 IN UINTN Width,\r
321 IN EFI_SMM_SAVE_STATE_REGISTER Register,\r
322 IN UINTN CpuIndex,\r
323 IN CONST VOID *Buffer\r
529a5a86
MK
324 );\r
325\r
326/**\r
327Read a CPU Save State register on the target processor.\r
328\r
329This function abstracts the differences that whether the CPU Save State register is in the\r
330IA32 CPU Save State Map or X64 CPU Save State Map.\r
331\r
332This function supports reading a CPU Save State register in SMBase relocation handler.\r
333\r
334@param[in] CpuIndex Specifies the zero-based index of the CPU save state.\r
335@param[in] RegisterIndex Index into mSmmCpuWidthOffset[] look up table.\r
336@param[in] Width The number of bytes to read from the CPU save state.\r
337@param[out] Buffer Upon return, this holds the CPU register value read from the save state.\r
338\r
339@retval EFI_SUCCESS The register was read from Save State.\r
340@retval EFI_NOT_FOUND The register is not defined for the Save State of Processor.\r
a7d8e28b 341@retval EFI_INVALID_PARAMETER Buffer is NULL, or Width does not meet requirement per Register type.\r
529a5a86
MK
342\r
343**/\r
344EFI_STATUS\r
345EFIAPI\r
346ReadSaveStateRegister (\r
347 IN UINTN CpuIndex,\r
348 IN EFI_SMM_SAVE_STATE_REGISTER Register,\r
349 IN UINTN Width,\r
350 OUT VOID *Buffer\r
351 );\r
352\r
353/**\r
354Write value to a CPU Save State register on the target processor.\r
355\r
356This function abstracts the differences that whether the CPU Save State register is in the\r
357IA32 CPU Save State Map or X64 CPU Save State Map.\r
358\r
359This function supports writing a CPU Save State register in SMBase relocation handler.\r
360\r
361@param[in] CpuIndex Specifies the zero-based index of the CPU save state.\r
362@param[in] RegisterIndex Index into mSmmCpuWidthOffset[] look up table.\r
363@param[in] Width The number of bytes to read from the CPU save state.\r
364@param[in] Buffer Upon entry, this holds the new CPU register value.\r
365\r
366@retval EFI_SUCCESS The register was written to Save State.\r
367@retval EFI_NOT_FOUND The register is not defined for the Save State of Processor.\r
ef62da4f 368@retval EFI_INVALID_PARAMETER ProcessorIndex or Width is not correct.\r
529a5a86
MK
369\r
370**/\r
371EFI_STATUS\r
372EFIAPI\r
373WriteSaveStateRegister (\r
374 IN UINTN CpuIndex,\r
375 IN EFI_SMM_SAVE_STATE_REGISTER Register,\r
376 IN UINTN Width,\r
377 IN CONST VOID *Buffer\r
378 );\r
379\r
053e878b
MK
380extern CONST UINT8 gcSmmInitTemplate[];\r
381extern CONST UINT16 gcSmmInitSize;\r
382X86_ASSEMBLY_PATCH_LABEL gPatchSmmCr0;\r
383extern UINT32 mSmmCr0;\r
384X86_ASSEMBLY_PATCH_LABEL gPatchSmmCr3;\r
385extern UINT32 mSmmCr4;\r
386X86_ASSEMBLY_PATCH_LABEL gPatchSmmCr4;\r
387X86_ASSEMBLY_PATCH_LABEL gPatchSmmInitStack;\r
388X86_ASSEMBLY_PATCH_LABEL mPatchCetSupported;\r
389extern BOOLEAN mCetSupported;\r
529a5a86
MK
390\r
391/**\r
392 Semaphore operation for all processor relocate SMMBase.\r
393**/\r
394VOID\r
395EFIAPI\r
396SmmRelocationSemaphoreComplete (\r
397 VOID\r
398 );\r
399\r
400///\r
401/// The type of SMM CPU Information\r
402///\r
403typedef struct {\r
053e878b
MK
404 SPIN_LOCK *Busy;\r
405 volatile EFI_AP_PROCEDURE2 Procedure;\r
406 volatile VOID *Parameter;\r
407 volatile UINT32 *Run;\r
408 volatile BOOLEAN *Present;\r
409 PROCEDURE_TOKEN *Token;\r
410 EFI_STATUS *Status;\r
529a5a86
MK
411} SMM_CPU_DATA_BLOCK;\r
412\r
413typedef enum {\r
414 SmmCpuSyncModeTradition,\r
415 SmmCpuSyncModeRelaxedAp,\r
416 SmmCpuSyncModeMax\r
417} SMM_CPU_SYNC_MODE;\r
418\r
419typedef struct {\r
420 //\r
421 // Pointer to an array. The array should be located immediately after this structure\r
422 // so that UC cache-ability can be set together.\r
423 //\r
424 SMM_CPU_DATA_BLOCK *CpuData;\r
fe3a75bc 425 volatile UINT32 *Counter;\r
529a5a86 426 volatile UINT32 BspIndex;\r
fe3a75bc
JF
427 volatile BOOLEAN *InsideSmm;\r
428 volatile BOOLEAN *AllCpusInSync;\r
529a5a86
MK
429 volatile SMM_CPU_SYNC_MODE EffectiveSyncMode;\r
430 volatile BOOLEAN SwitchBsp;\r
431 volatile BOOLEAN *CandidateBsp;\r
4a68176c 432 volatile BOOLEAN AllApArrivedWithException;\r
51dd408a
ED
433 EFI_AP_PROCEDURE StartupProcedure;\r
434 VOID *StartupProcArgs;\r
529a5a86
MK
435} SMM_DISPATCHER_MP_SYNC_DATA;\r
436\r
053e878b 437#define SMM_PSD_OFFSET 0xfb00\r
529a5a86 438\r
1d648531
JF
439///\r
440/// All global semaphores' pointer\r
441///\r
442typedef struct {\r
053e878b
MK
443 volatile UINT32 *Counter;\r
444 volatile BOOLEAN *InsideSmm;\r
445 volatile BOOLEAN *AllCpusInSync;\r
446 SPIN_LOCK *PFLock;\r
447 SPIN_LOCK *CodeAccessCheckLock;\r
1d648531
JF
448} SMM_CPU_SEMAPHORE_GLOBAL;\r
449\r
4e920581
JF
450///\r
451/// All semaphores for each processor\r
452///\r
453typedef struct {\r
053e878b
MK
454 SPIN_LOCK *Busy;\r
455 volatile UINT32 *Run;\r
456 volatile BOOLEAN *Present;\r
457 SPIN_LOCK *Token;\r
4e920581
JF
458} SMM_CPU_SEMAPHORE_CPU;\r
459\r
1d648531
JF
460///\r
461/// All semaphores' information\r
462///\r
463typedef struct {\r
053e878b
MK
464 SMM_CPU_SEMAPHORE_GLOBAL SemaphoreGlobal;\r
465 SMM_CPU_SEMAPHORE_CPU SemaphoreCpu;\r
1d648531
JF
466} SMM_CPU_SEMAPHORES;\r
467\r
053e878b
MK
468extern IA32_DESCRIPTOR gcSmiGdtr;\r
469extern EFI_PHYSICAL_ADDRESS mGdtBuffer;\r
470extern UINTN mGdtBufferSize;\r
471extern IA32_DESCRIPTOR gcSmiIdtr;\r
472extern VOID *gcSmiIdtrPtr;\r
473extern UINT64 gPhyMask;\r
474extern SMM_DISPATCHER_MP_SYNC_DATA *mSmmMpSyncData;\r
475extern UINTN mSmmStackArrayBase;\r
476extern UINTN mSmmStackArrayEnd;\r
477extern UINTN mSmmStackSize;\r
478extern EFI_SMM_CPU_SERVICE_PROTOCOL mSmmCpuService;\r
479extern IA32_DESCRIPTOR gcSmiInitGdtr;\r
480extern SMM_CPU_SEMAPHORES mSmmCpuSemaphores;\r
481extern UINTN mSemaphoreSize;\r
482extern SPIN_LOCK *mPFLock;\r
483extern SPIN_LOCK *mConfigSmmCodeAccessCheckLock;\r
484extern EFI_SMRAM_DESCRIPTOR *mSmmCpuSmramRanges;\r
485extern UINTN mSmmCpuSmramRangeCount;\r
486extern UINT8 mPhysicalAddressBits;\r
529a5a86 487\r
241f9149
LD
488//\r
489// Copy of the PcdPteMemoryEncryptionAddressOrMask\r
490//\r
491extern UINT64 mAddressEncMask;\r
492\r
529a5a86
MK
493/**\r
494 Create 4G PageTable in SMRAM.\r
495\r
717fb604 496 @param[in] Is32BitPageTable Whether the page table is 32-bit PAE\r
529a5a86
MK
497 @return PageTable Address\r
498\r
499**/\r
500UINT32\r
501Gen4GPageTable (\r
053e878b 502 IN BOOLEAN Is32BitPageTable\r
529a5a86
MK
503 );\r
504\r
529a5a86
MK
505/**\r
506 Initialize global data for MP synchronization.\r
507\r
3eb69b08
JY
508 @param Stacks Base address of SMI stack buffer for all processors.\r
509 @param StackSize Stack size for each processor in SMM.\r
510 @param ShadowStackSize Shadow Stack size for each processor in SMM.\r
529a5a86
MK
511\r
512**/\r
513UINT32\r
514InitializeMpServiceData (\r
053e878b
MK
515 IN VOID *Stacks,\r
516 IN UINTN StackSize,\r
517 IN UINTN ShadowStackSize\r
529a5a86
MK
518 );\r
519\r
520/**\r
521 Initialize Timer for SMM AP Sync.\r
522\r
523**/\r
524VOID\r
525InitializeSmmTimer (\r
526 VOID\r
527 );\r
528\r
529/**\r
530 Start Timer for SMM AP Sync.\r
531\r
532**/\r
533UINT64\r
534EFIAPI\r
535StartSyncTimer (\r
536 VOID\r
537 );\r
538\r
539/**\r
540 Check if the SMM AP Sync timer is timeout.\r
541\r
542 @param Timer The start timer from the begin.\r
543\r
544**/\r
545BOOLEAN\r
546EFIAPI\r
547IsSyncTimerTimeout (\r
053e878b 548 IN UINT64 Timer\r
529a5a86
MK
549 );\r
550\r
551/**\r
552 Initialize IDT for SMM Stack Guard.\r
553\r
554**/\r
555VOID\r
556EFIAPI\r
557InitializeIDTSmmStackGuard (\r
558 VOID\r
559 );\r
560\r
455b0347
S
561/**\r
562 Initialize IDT IST Field.\r
563\r
564 @param[in] ExceptionType Exception type.\r
565 @param[in] Ist IST value.\r
566\r
567**/\r
568VOID\r
569EFIAPI\r
570InitializeIdtIst (\r
053e878b
MK
571 IN EFI_EXCEPTION_TYPE ExceptionType,\r
572 IN UINT8 Ist\r
455b0347
S
573 );\r
574\r
fe5f1949
JY
575/**\r
576 Initialize Gdt for all processors.\r
717fb604 577\r
fe5f1949
JY
578 @param[in] Cr3 CR3 value.\r
579 @param[out] GdtStepSize The step size for GDT table.\r
580\r
581 @return GdtBase for processor 0.\r
582 GdtBase for processor X is: GdtBase + (GdtStepSize * X)\r
583**/\r
584VOID *\r
585InitGdt (\r
586 IN UINTN Cr3,\r
587 OUT UINTN *GdtStepSize\r
588 );\r
589\r
529a5a86
MK
590/**\r
591\r
592 Register the SMM Foundation entry point.\r
593\r
594 @param This Pointer to EFI_SMM_CONFIGURATION_PROTOCOL instance\r
595 @param SmmEntryPoint SMM Foundation EntryPoint\r
596\r
597 @retval EFI_SUCCESS Successfully to register SMM foundation entry point\r
598\r
599**/\r
600EFI_STATUS\r
601EFIAPI\r
602RegisterSmmEntry (\r
603 IN CONST EFI_SMM_CONFIGURATION_PROTOCOL *This,\r
604 IN EFI_SMM_ENTRY_POINT SmmEntryPoint\r
605 );\r
606\r
607/**\r
608 Create PageTable for SMM use.\r
609\r
610 @return PageTable Address\r
611\r
612**/\r
613UINT32\r
614SmmInitPageTable (\r
615 VOID\r
616 );\r
617\r
618/**\r
619 Schedule a procedure to run on the specified CPU.\r
620\r
621 @param Procedure The address of the procedure to run\r
622 @param CpuIndex Target CPU number\r
623 @param ProcArguments The parameter to pass to the procedure\r
624\r
625 @retval EFI_INVALID_PARAMETER CpuNumber not valid\r
626 @retval EFI_INVALID_PARAMETER CpuNumber specifying BSP\r
627 @retval EFI_INVALID_PARAMETER The AP specified by CpuNumber did not enter SMM\r
628 @retval EFI_INVALID_PARAMETER The AP specified by CpuNumber is busy\r
629 @retval EFI_SUCCESS - The procedure has been successfully scheduled\r
630\r
631**/\r
632EFI_STATUS\r
633EFIAPI\r
634SmmStartupThisAp (\r
053e878b
MK
635 IN EFI_AP_PROCEDURE Procedure,\r
636 IN UINTN CpuIndex,\r
637 IN OUT VOID *ProcArguments OPTIONAL\r
529a5a86
MK
638 );\r
639\r
640/**\r
641 Schedule a procedure to run on the specified CPU in a blocking fashion.\r
642\r
643 @param Procedure The address of the procedure to run\r
644 @param CpuIndex Target CPU Index\r
645 @param ProcArguments The parameter to pass to the procedure\r
646\r
647 @retval EFI_INVALID_PARAMETER CpuNumber not valid\r
648 @retval EFI_INVALID_PARAMETER CpuNumber specifying BSP\r
649 @retval EFI_INVALID_PARAMETER The AP specified by CpuNumber did not enter SMM\r
650 @retval EFI_INVALID_PARAMETER The AP specified by CpuNumber is busy\r
651 @retval EFI_SUCCESS The procedure has been successfully scheduled\r
652\r
653**/\r
654EFI_STATUS\r
655EFIAPI\r
656SmmBlockingStartupThisAp (\r
053e878b
MK
657 IN EFI_AP_PROCEDURE Procedure,\r
658 IN UINTN CpuIndex,\r
659 IN OUT VOID *ProcArguments OPTIONAL\r
529a5a86
MK
660 );\r
661\r
e4435f71
JY
662/**\r
663 This function sets the attributes for the memory region specified by BaseAddress and\r
664 Length from their current attributes to the attributes specified by Attributes.\r
665\r
666 @param[in] BaseAddress The physical address that is the start address of a memory region.\r
667 @param[in] Length The size in bytes of the memory region.\r
668 @param[in] Attributes The bit mask of attributes to set for the memory region.\r
669\r
670 @retval EFI_SUCCESS The attributes were set for the memory region.\r
671 @retval EFI_ACCESS_DENIED The attributes for the memory resource range specified by\r
672 BaseAddress and Length cannot be modified.\r
673 @retval EFI_INVALID_PARAMETER Length is zero.\r
674 Attributes specified an illegal combination of attributes that\r
675 cannot be set together.\r
676 @retval EFI_OUT_OF_RESOURCES There are not enough system resources to modify the attributes of\r
677 the memory resource range.\r
678 @retval EFI_UNSUPPORTED The processor does not support one or more bytes of the memory\r
679 resource range specified by BaseAddress and Length.\r
680 The bit mask of attributes is not support for the memory resource\r
681 range specified by BaseAddress and Length.\r
682\r
683**/\r
684EFI_STATUS\r
e4435f71 685SmmSetMemoryAttributes (\r
053e878b
MK
686 IN EFI_PHYSICAL_ADDRESS BaseAddress,\r
687 IN UINT64 Length,\r
688 IN UINT64 Attributes\r
e4435f71
JY
689 );\r
690\r
691/**\r
692 This function clears the attributes for the memory region specified by BaseAddress and\r
693 Length from their current attributes to the attributes specified by Attributes.\r
694\r
695 @param[in] BaseAddress The physical address that is the start address of a memory region.\r
696 @param[in] Length The size in bytes of the memory region.\r
697 @param[in] Attributes The bit mask of attributes to clear for the memory region.\r
698\r
699 @retval EFI_SUCCESS The attributes were cleared for the memory region.\r
700 @retval EFI_ACCESS_DENIED The attributes for the memory resource range specified by\r
701 BaseAddress and Length cannot be modified.\r
702 @retval EFI_INVALID_PARAMETER Length is zero.\r
703 Attributes specified an illegal combination of attributes that\r
704 cannot be set together.\r
705 @retval EFI_OUT_OF_RESOURCES There are not enough system resources to modify the attributes of\r
706 the memory resource range.\r
707 @retval EFI_UNSUPPORTED The processor does not support one or more bytes of the memory\r
708 resource range specified by BaseAddress and Length.\r
709 The bit mask of attributes is not support for the memory resource\r
710 range specified by BaseAddress and Length.\r
711\r
712**/\r
713EFI_STATUS\r
e4435f71 714SmmClearMemoryAttributes (\r
053e878b
MK
715 IN EFI_PHYSICAL_ADDRESS BaseAddress,\r
716 IN UINT64 Length,\r
717 IN UINT64 Attributes\r
e4435f71
JY
718 );\r
719\r
529a5a86
MK
720/**\r
721 Initialize MP synchronization data.\r
722\r
723**/\r
724VOID\r
725EFIAPI\r
726InitializeMpSyncData (\r
727 VOID\r
728 );\r
729\r
730/**\r
731\r
732 Find out SMRAM information including SMRR base and SMRR size.\r
733\r
734 @param SmrrBase SMRR base\r
735 @param SmrrSize SMRR size\r
736\r
737**/\r
738VOID\r
739FindSmramInfo (\r
053e878b
MK
740 OUT UINT32 *SmrrBase,\r
741 OUT UINT32 *SmrrSize\r
529a5a86
MK
742 );\r
743\r
744/**\r
0bdc9e75 745 Relocate SmmBases for each processor.\r
529a5a86 746\r
0bdc9e75 747 Execute on first boot and all S3 resumes\r
529a5a86
MK
748\r
749**/\r
750VOID\r
0bdc9e75
SZ
751EFIAPI\r
752SmmRelocateBases (\r
529a5a86
MK
753 VOID\r
754 );\r
755\r
756/**\r
757 Page Fault handler for SMM use.\r
758\r
759 @param InterruptType Defines the type of interrupt or exception that\r
760 occurred on the processor.This parameter is processor architecture specific.\r
761 @param SystemContext A pointer to the processor context when\r
762 the interrupt occurred on the processor.\r
763**/\r
764VOID\r
765EFIAPI\r
766SmiPFHandler (\r
053e878b
MK
767 IN EFI_EXCEPTION_TYPE InterruptType,\r
768 IN EFI_SYSTEM_CONTEXT SystemContext\r
529a5a86
MK
769 );\r
770\r
771/**\r
772 Perform the remaining tasks.\r
773\r
774**/\r
775VOID\r
776PerformRemainingTasks (\r
777 VOID\r
778 );\r
779\r
9f419739
JY
780/**\r
781 Perform the pre tasks.\r
782\r
783**/\r
784VOID\r
785PerformPreTasks (\r
786 VOID\r
787 );\r
788\r
529a5a86
MK
789/**\r
790 Initialize MSR spin lock by MSR index.\r
791\r
792 @param MsrIndex MSR index value.\r
793\r
794**/\r
795VOID\r
796InitMsrSpinLockByIndex (\r
053e878b 797 IN UINT32 MsrIndex\r
529a5a86
MK
798 );\r
799\r
800/**\r
801 Hook return address of SMM Save State so that semaphore code\r
802 can be executed immediately after AP exits SMM to indicate to\r
803 the BSP that an AP has exited SMM after SMBASE relocation.\r
804\r
805 @param[in] CpuIndex The processor index.\r
806 @param[in] RebasedFlag A pointer to a flag that is set to TRUE\r
807 immediately after AP exits SMM.\r
808\r
809**/\r
810VOID\r
811SemaphoreHook (\r
812 IN UINTN CpuIndex,\r
813 IN volatile BOOLEAN *RebasedFlag\r
814 );\r
815\r
816/**\r
817Configure SMM Code Access Check feature for all processors.\r
818SMM Feature Control MSR will be locked after configuration.\r
819**/\r
820VOID\r
821ConfigSmmCodeAccessCheck (\r
822 VOID\r
823 );\r
824\r
825/**\r
826 Hook the code executed immediately after an RSM instruction on the currently\r
827 executing CPU. The mode of code executed immediately after RSM must be\r
828 detected, and the appropriate hook must be selected. Always clear the auto\r
829 HALT restart flag if it is set.\r
830\r
831 @param[in] CpuIndex The processor index for the currently\r
832 executing CPU.\r
833 @param[in] CpuState Pointer to SMRAM Save State Map for the\r
834 currently executing CPU.\r
835 @param[in] NewInstructionPointer32 Instruction pointer to use if resuming to\r
836 32-bit mode from 64-bit SMM.\r
837 @param[in] NewInstructionPointer Instruction pointer to use if resuming to\r
838 same mode as SMM.\r
839\r
840 @retval The value of the original instruction pointer before it was hooked.\r
841\r
842**/\r
843UINT64\r
844EFIAPI\r
845HookReturnFromSmm (\r
846 IN UINTN CpuIndex,\r
847 SMRAM_SAVE_STATE_MAP *CpuState,\r
848 UINT64 NewInstructionPointer32,\r
849 UINT64 NewInstructionPointer\r
850 );\r
851\r
852/**\r
853 Get the size of the SMI Handler in bytes.\r
854\r
855 @retval The size, in bytes, of the SMI Handler.\r
856\r
857**/\r
858UINTN\r
859EFIAPI\r
860GetSmiHandlerSize (\r
861 VOID\r
862 );\r
863\r
864/**\r
865 Install the SMI handler for the CPU specified by CpuIndex. This function\r
866 is called by the CPU that was elected as monarch during System Management\r
867 Mode initialization.\r
868\r
869 @param[in] CpuIndex The index of the CPU to install the custom SMI handler.\r
870 The value must be between 0 and the NumberOfCpus field\r
871 in the System Management System Table (SMST).\r
872 @param[in] SmBase The SMBASE address for the CPU specified by CpuIndex.\r
873 @param[in] SmiStack The stack to use when an SMI is processed by the\r
874 the CPU specified by CpuIndex.\r
875 @param[in] StackSize The size, in bytes, if the stack used when an SMI is\r
876 processed by the CPU specified by CpuIndex.\r
877 @param[in] GdtBase The base address of the GDT to use when an SMI is\r
878 processed by the CPU specified by CpuIndex.\r
879 @param[in] GdtSize The size, in bytes, of the GDT used when an SMI is\r
880 processed by the CPU specified by CpuIndex.\r
881 @param[in] IdtBase The base address of the IDT to use when an SMI is\r
882 processed by the CPU specified by CpuIndex.\r
883 @param[in] IdtSize The size, in bytes, of the IDT used when an SMI is\r
884 processed by the CPU specified by CpuIndex.\r
885 @param[in] Cr3 The base address of the page tables to use when an SMI\r
886 is processed by the CPU specified by CpuIndex.\r
887**/\r
888VOID\r
889EFIAPI\r
890InstallSmiHandler (\r
891 IN UINTN CpuIndex,\r
892 IN UINT32 SmBase,\r
893 IN VOID *SmiStack,\r
894 IN UINTN StackSize,\r
895 IN UINTN GdtBase,\r
896 IN UINTN GdtSize,\r
897 IN UINTN IdtBase,\r
898 IN UINTN IdtSize,\r
899 IN UINT32 Cr3\r
900 );\r
901\r
902/**\r
903 Search module name by input IP address and output it.\r
904\r
905 @param CallerIpAddress Caller instruction pointer.\r
906\r
907**/\r
908VOID\r
909DumpModuleInfoByIp (\r
053e878b 910 IN UINTN CallerIpAddress\r
529a5a86 911 );\r
21c17193 912\r
717fb604
JY
913/**\r
914 This function sets memory attribute according to MemoryAttributesTable.\r
915**/\r
916VOID\r
917SetMemMapAttributes (\r
918 VOID\r
919 );\r
920\r
d2fc7711
JY
921/**\r
922 This function sets UEFI memory attribute according to UEFI memory map.\r
923**/\r
924VOID\r
925SetUefiMemMapAttributes (\r
926 VOID\r
927 );\r
928\r
929/**\r
930 Return if the Address is forbidden as SMM communication buffer.\r
931\r
932 @param[in] Address the address to be checked\r
933\r
934 @return TRUE The address is forbidden as SMM communication buffer.\r
935 @return FALSE The address is allowed as SMM communication buffer.\r
936**/\r
937BOOLEAN\r
938IsSmmCommBufferForbiddenAddress (\r
939 IN UINT64 Address\r
940 );\r
941\r
942/**\r
943 This function caches the UEFI memory map information.\r
944**/\r
945VOID\r
946GetUefiMemoryMap (\r
947 VOID\r
948 );\r
949\r
717fb604
JY
950/**\r
951 This function sets memory attribute for page table.\r
952**/\r
953VOID\r
954SetPageTableAttributes (\r
955 VOID\r
956 );\r
957\r
717fb604
JY
958/**\r
959 This function sets the attributes for the memory region specified by BaseAddress and\r
960 Length from their current attributes to the attributes specified by Attributes.\r
961\r
7b475490
DT
962 @param[in] PageTableBase The page table base.\r
963 @param[in] EnablePML5Paging If PML5 paging is enabled.\r
717fb604
JY
964 @param[in] BaseAddress The physical address that is the start address of a memory region.\r
965 @param[in] Length The size in bytes of the memory region.\r
966 @param[in] Attributes The bit mask of attributes to set for the memory region.\r
967 @param[out] IsSplitted TRUE means page table splitted. FALSE means page table not splitted.\r
968\r
969 @retval EFI_SUCCESS The attributes were set for the memory region.\r
970 @retval EFI_ACCESS_DENIED The attributes for the memory resource range specified by\r
971 BaseAddress and Length cannot be modified.\r
972 @retval EFI_INVALID_PARAMETER Length is zero.\r
973 Attributes specified an illegal combination of attributes that\r
974 cannot be set together.\r
975 @retval EFI_OUT_OF_RESOURCES There are not enough system resources to modify the attributes of\r
976 the memory resource range.\r
977 @retval EFI_UNSUPPORTED The processor does not support one or more bytes of the memory\r
978 resource range specified by BaseAddress and Length.\r
979 The bit mask of attributes is not support for the memory resource\r
980 range specified by BaseAddress and Length.\r
981\r
982**/\r
983EFI_STATUS\r
717fb604 984SmmSetMemoryAttributesEx (\r
7b475490
DT
985 IN UINTN PageTableBase,\r
986 IN BOOLEAN EnablePML5Paging,\r
053e878b
MK
987 IN EFI_PHYSICAL_ADDRESS BaseAddress,\r
988 IN UINT64 Length,\r
989 IN UINT64 Attributes,\r
990 OUT BOOLEAN *IsSplitted OPTIONAL\r
717fb604
JY
991 );\r
992\r
993/**\r
994 This function clears the attributes for the memory region specified by BaseAddress and\r
995 Length from their current attributes to the attributes specified by Attributes.\r
996\r
7b475490
DT
997 @param[in] PageTableBase The page table base.\r
998 @param[in] EnablePML5Paging If PML5 paging is enabled.\r
717fb604
JY
999 @param[in] BaseAddress The physical address that is the start address of a memory region.\r
1000 @param[in] Length The size in bytes of the memory region.\r
1001 @param[in] Attributes The bit mask of attributes to clear for the memory region.\r
1002 @param[out] IsSplitted TRUE means page table splitted. FALSE means page table not splitted.\r
1003\r
1004 @retval EFI_SUCCESS The attributes were cleared for the memory region.\r
1005 @retval EFI_ACCESS_DENIED The attributes for the memory resource range specified by\r
1006 BaseAddress and Length cannot be modified.\r
1007 @retval EFI_INVALID_PARAMETER Length is zero.\r
1008 Attributes specified an illegal combination of attributes that\r
1009 cannot be set together.\r
1010 @retval EFI_OUT_OF_RESOURCES There are not enough system resources to modify the attributes of\r
1011 the memory resource range.\r
1012 @retval EFI_UNSUPPORTED The processor does not support one or more bytes of the memory\r
1013 resource range specified by BaseAddress and Length.\r
1014 The bit mask of attributes is not support for the memory resource\r
1015 range specified by BaseAddress and Length.\r
1016\r
1017**/\r
1018EFI_STATUS\r
717fb604 1019SmmClearMemoryAttributesEx (\r
7b475490
DT
1020 IN UINTN PageTableBase,\r
1021 IN BOOLEAN EnablePML5Paging,\r
053e878b
MK
1022 IN EFI_PHYSICAL_ADDRESS BaseAddress,\r
1023 IN UINT64 Length,\r
1024 IN UINT64 Attributes,\r
1025 OUT BOOLEAN *IsSplitted OPTIONAL\r
717fb604
JY
1026 );\r
1027\r
21c17193
JY
1028/**\r
1029 This API provides a way to allocate memory for page table.\r
1030\r
1031 This API can be called more once to allocate memory for page tables.\r
1032\r
1033 Allocates the number of 4KB pages of type EfiRuntimeServicesData and returns a pointer to the\r
1034 allocated buffer. The buffer returned is aligned on a 4KB boundary. If Pages is 0, then NULL\r
1035 is returned. If there is not enough memory remaining to satisfy the request, then NULL is\r
1036 returned.\r
1037\r
1038 @param Pages The number of 4 KB pages to allocate.\r
1039\r
1040 @return A pointer to the allocated buffer or NULL if allocation fails.\r
1041\r
1042**/\r
1043VOID *\r
1044AllocatePageTableMemory (\r
053e878b 1045 IN UINTN Pages\r
21c17193
JY
1046 );\r
1047\r
717fb604
JY
1048/**\r
1049 Allocate pages for code.\r
1050\r
1051 @param[in] Pages Number of pages to be allocated.\r
1052\r
1053 @return Allocated memory.\r
1054**/\r
1055VOID *\r
1056AllocateCodePages (\r
053e878b 1057 IN UINTN Pages\r
717fb604
JY
1058 );\r
1059\r
1060/**\r
1061 Allocate aligned pages for code.\r
1062\r
1063 @param[in] Pages Number of pages to be allocated.\r
1064 @param[in] Alignment The requested alignment of the allocation.\r
1065 Must be a power of two.\r
1066 If Alignment is zero, then byte alignment is used.\r
1067\r
1068 @return Allocated memory.\r
1069**/\r
1070VOID *\r
1071AllocateAlignedCodePages (\r
053e878b
MK
1072 IN UINTN Pages,\r
1073 IN UINTN Alignment\r
717fb604
JY
1074 );\r
1075\r
0bdc9e75
SZ
1076//\r
1077// S3 related global variable and function prototype.\r
1078//\r
1079\r
053e878b 1080extern BOOLEAN mSmmS3Flag;\r
0bdc9e75
SZ
1081\r
1082/**\r
1083 Initialize SMM S3 resume state structure used during S3 Resume.\r
1084\r
1085 @param[in] Cr3 The base address of the page tables to use in SMM.\r
1086\r
1087**/\r
1088VOID\r
1089InitSmmS3ResumeState (\r
1090 IN UINT32 Cr3\r
1091 );\r
1092\r
1093/**\r
1094 Get ACPI CPU data.\r
1095\r
1096**/\r
1097VOID\r
1098GetAcpiCpuData (\r
1099 VOID\r
1100 );\r
1101\r
1102/**\r
1103 Restore SMM Configuration in S3 boot path.\r
1104\r
1105**/\r
1106VOID\r
1107RestoreSmmConfigurationInS3 (\r
1108 VOID\r
1109 );\r
1110\r
b10d5ddc
SZ
1111/**\r
1112 Get ACPI S3 enable flag.\r
1113\r
1114**/\r
1115VOID\r
1116GetAcpiS3EnableFlag (\r
1117 VOID\r
1118 );\r
1119\r
4a0f88dd
JF
1120/**\r
1121 Transfer AP to safe hlt-loop after it finished restore CPU features on S3 patch.\r
1122\r
672b80c8
MK
1123 @param[in] ApHltLoopCode The address of the safe hlt-loop function.\r
1124 @param[in] TopOfStack A pointer to the new stack to use for the ApHltLoopCode.\r
1125 @param[in] NumberToFinishAddress Address of Semaphore of APs finish count.\r
4a0f88dd
JF
1126\r
1127**/\r
1128VOID\r
1129TransferApToSafeState (\r
672b80c8
MK
1130 IN UINTN ApHltLoopCode,\r
1131 IN UINTN TopOfStack,\r
1132 IN UINTN NumberToFinishAddress\r
4a0f88dd
JF
1133 );\r
1134\r
3eb69b08
JY
1135/**\r
1136 Set ShadowStack memory.\r
1137\r
1138 @param[in] Cr3 The page table base address.\r
1139 @param[in] BaseAddress The physical address that is the start address of a memory region.\r
1140 @param[in] Length The size in bytes of the memory region.\r
1141\r
1142 @retval EFI_SUCCESS The shadow stack memory is set.\r
1143**/\r
1144EFI_STATUS\r
1145SetShadowStack (\r
053e878b
MK
1146 IN UINTN Cr3,\r
1147 IN EFI_PHYSICAL_ADDRESS BaseAddress,\r
1148 IN UINT64 Length\r
3eb69b08
JY
1149 );\r
1150\r
1151/**\r
1152 Set not present memory.\r
1153\r
1154 @param[in] Cr3 The page table base address.\r
1155 @param[in] BaseAddress The physical address that is the start address of a memory region.\r
1156 @param[in] Length The size in bytes of the memory region.\r
1157\r
1158 @retval EFI_SUCCESS The not present memory is set.\r
1159**/\r
1160EFI_STATUS\r
1161SetNotPresentPage (\r
053e878b
MK
1162 IN UINTN Cr3,\r
1163 IN EFI_PHYSICAL_ADDRESS BaseAddress,\r
1164 IN UINT64 Length\r
3eb69b08
JY
1165 );\r
1166\r
1167/**\r
1168 Initialize the shadow stack related data structure.\r
1169\r
1170 @param CpuIndex The index of CPU.\r
1171 @param ShadowStack The bottom of the shadow stack for this CPU.\r
1172**/\r
1173VOID\r
1174InitShadowStack (\r
1175 IN UINTN CpuIndex,\r
1176 IN VOID *ShadowStack\r
1177 );\r
1178\r
827330cc
JW
1179/**\r
1180 This function set given attributes of the memory region specified by\r
1181 BaseAddress and Length.\r
1182\r
1183 @param This The EDKII_SMM_MEMORY_ATTRIBUTE_PROTOCOL instance.\r
1184 @param BaseAddress The physical address that is the start address of\r
1185 a memory region.\r
1186 @param Length The size in bytes of the memory region.\r
1187 @param Attributes The bit mask of attributes to set for the memory\r
1188 region.\r
1189\r
1190 @retval EFI_SUCCESS The attributes were set for the memory region.\r
1191 @retval EFI_INVALID_PARAMETER Length is zero.\r
1192 Attributes specified an illegal combination of\r
1193 attributes that cannot be set together.\r
1194 @retval EFI_UNSUPPORTED The processor does not support one or more\r
1195 bytes of the memory resource range specified\r
1196 by BaseAddress and Length.\r
aae02dcc 1197 The bit mask of attributes is not supported for\r
827330cc
JW
1198 the memory resource range specified by\r
1199 BaseAddress and Length.\r
1200\r
1201**/\r
1202EFI_STATUS\r
1203EFIAPI\r
1204EdkiiSmmSetMemoryAttributes (\r
053e878b
MK
1205 IN EDKII_SMM_MEMORY_ATTRIBUTE_PROTOCOL *This,\r
1206 IN EFI_PHYSICAL_ADDRESS BaseAddress,\r
1207 IN UINT64 Length,\r
1208 IN UINT64 Attributes\r
827330cc
JW
1209 );\r
1210\r
1211/**\r
1212 This function clears given attributes of the memory region specified by\r
1213 BaseAddress and Length.\r
1214\r
1215 @param This The EDKII_SMM_MEMORY_ATTRIBUTE_PROTOCOL instance.\r
1216 @param BaseAddress The physical address that is the start address of\r
1217 a memory region.\r
1218 @param Length The size in bytes of the memory region.\r
aae02dcc 1219 @param Attributes The bit mask of attributes to clear for the memory\r
827330cc
JW
1220 region.\r
1221\r
aae02dcc 1222 @retval EFI_SUCCESS The attributes were cleared for the memory region.\r
827330cc
JW
1223 @retval EFI_INVALID_PARAMETER Length is zero.\r
1224 Attributes specified an illegal combination of\r
aae02dcc 1225 attributes that cannot be cleared together.\r
827330cc
JW
1226 @retval EFI_UNSUPPORTED The processor does not support one or more\r
1227 bytes of the memory resource range specified\r
1228 by BaseAddress and Length.\r
aae02dcc 1229 The bit mask of attributes is not supported for\r
827330cc
JW
1230 the memory resource range specified by\r
1231 BaseAddress and Length.\r
1232\r
1233**/\r
1234EFI_STATUS\r
1235EFIAPI\r
1236EdkiiSmmClearMemoryAttributes (\r
053e878b
MK
1237 IN EDKII_SMM_MEMORY_ATTRIBUTE_PROTOCOL *This,\r
1238 IN EFI_PHYSICAL_ADDRESS BaseAddress,\r
1239 IN UINT64 Length,\r
1240 IN UINT64 Attributes\r
827330cc
JW
1241 );\r
1242\r
1243/**\r
aae02dcc 1244 This function retrieves the attributes of the memory region specified by\r
827330cc
JW
1245 BaseAddress and Length. If different attributes are got from different part\r
1246 of the memory region, EFI_NO_MAPPING will be returned.\r
1247\r
1248 @param This The EDKII_SMM_MEMORY_ATTRIBUTE_PROTOCOL instance.\r
1249 @param BaseAddress The physical address that is the start address of\r
1250 a memory region.\r
1251 @param Length The size in bytes of the memory region.\r
1252 @param Attributes Pointer to attributes returned.\r
1253\r
1254 @retval EFI_SUCCESS The attributes got for the memory region.\r
1255 @retval EFI_INVALID_PARAMETER Length is zero.\r
1256 Attributes is NULL.\r
1257 @retval EFI_NO_MAPPING Attributes are not consistent cross the memory\r
1258 region.\r
1259 @retval EFI_UNSUPPORTED The processor does not support one or more\r
1260 bytes of the memory resource range specified\r
1261 by BaseAddress and Length.\r
827330cc
JW
1262\r
1263**/\r
1264EFI_STATUS\r
1265EFIAPI\r
1266EdkiiSmmGetMemoryAttributes (\r
053e878b
MK
1267 IN EDKII_SMM_MEMORY_ATTRIBUTE_PROTOCOL *This,\r
1268 IN EFI_PHYSICAL_ADDRESS BaseAddress,\r
1269 IN UINT64 Length,\r
1270 IN UINT64 *Attributes\r
827330cc
JW
1271 );\r
1272\r
e21e355e
LG
1273/**\r
1274 This function fixes up the address of the global variable or function\r
ef62da4f 1275 referred in SmmInit assembly files to be the absolute address.\r
e21e355e
LG
1276**/\r
1277VOID\r
1278EFIAPI\r
1279PiSmmCpuSmmInitFixupAddress (\r
053e878b 1280 );\r
e21e355e
LG
1281\r
1282/**\r
1283 This function fixes up the address of the global variable or function\r
ef62da4f 1284 referred in SmiEntry assembly files to be the absolute address.\r
e21e355e
LG
1285**/\r
1286VOID\r
1287EFIAPI\r
1288PiSmmCpuSmiEntryFixupAddress (\r
053e878b 1289 );\r
e21e355e 1290\r
37f9fea5
VN
1291/**\r
1292 This function reads CR2 register when on-demand paging is enabled\r
1293 for 64 bit and no action for 32 bit.\r
1294\r
1295 @param[out] *Cr2 Pointer to variable to hold CR2 register value.\r
1296**/\r
1297VOID\r
1298SaveCr2 (\r
1299 OUT UINTN *Cr2\r
1300 );\r
1301\r
1302/**\r
1303 This function writes into CR2 register when on-demand paging is enabled\r
1304 for 64 bit and no action for 32 bit.\r
1305\r
1306 @param[in] Cr2 Value to write into CR2 register.\r
1307**/\r
1308VOID\r
1309RestoreCr2 (\r
1310 IN UINTN Cr2\r
1311 );\r
1312\r
51dd408a
ED
1313/**\r
1314 Schedule a procedure to run on the specified CPU.\r
1315\r
1316 @param[in] Procedure The address of the procedure to run\r
1317 @param[in] CpuIndex Target CPU Index\r
1318 @param[in,out] ProcArguments The parameter to pass to the procedure\r
1319 @param[in,out] Token This is an optional parameter that allows the caller to execute the\r
1320 procedure in a blocking or non-blocking fashion. If it is NULL the\r
1321 call is blocking, and the call will not return until the AP has\r
1322 completed the procedure. If the token is not NULL, the call will\r
1323 return immediately. The caller can check whether the procedure has\r
1324 completed with CheckOnProcedure or WaitForProcedure.\r
1325 @param[in] TimeoutInMicroseconds Indicates the time limit in microseconds for the APs to finish\r
1326 execution of Procedure, either for blocking or non-blocking mode.\r
1327 Zero means infinity. If the timeout expires before all APs return\r
1328 from Procedure, then Procedure on the failed APs is terminated. If\r
1329 the timeout expires in blocking mode, the call returns EFI_TIMEOUT.\r
1330 If the timeout expires in non-blocking mode, the timeout determined\r
1331 can be through CheckOnProcedure or WaitForProcedure.\r
1332 Note that timeout support is optional. Whether an implementation\r
1333 supports this feature can be determined via the Attributes data\r
1334 member.\r
073f2ced 1335 @param[in,out] CpuStatus This optional pointer may be used to get the status code returned\r
51dd408a
ED
1336 by Procedure when it completes execution on the target AP, or with\r
1337 EFI_TIMEOUT if the Procedure fails to complete within the optional\r
1338 timeout. The implementation will update this variable with\r
1339 EFI_NOT_READY prior to starting Procedure on the target AP.\r
1340\r
1341 @retval EFI_INVALID_PARAMETER CpuNumber not valid\r
1342 @retval EFI_INVALID_PARAMETER CpuNumber specifying BSP\r
1343 @retval EFI_INVALID_PARAMETER The AP specified by CpuNumber did not enter SMM\r
1344 @retval EFI_INVALID_PARAMETER The AP specified by CpuNumber is busy\r
1345 @retval EFI_SUCCESS The procedure has been successfully scheduled\r
1346\r
1347**/\r
1348EFI_STATUS\r
1349InternalSmmStartupThisAp (\r
053e878b
MK
1350 IN EFI_AP_PROCEDURE2 Procedure,\r
1351 IN UINTN CpuIndex,\r
1352 IN OUT VOID *ProcArguments OPTIONAL,\r
1353 IN OUT MM_COMPLETION *Token,\r
1354 IN UINTN TimeoutInMicroseconds,\r
1355 IN OUT EFI_STATUS *CpuStatus\r
51dd408a
ED
1356 );\r
1357\r
1358/**\r
1359 Checks whether the input token is the current used token.\r
1360\r
1361 @param[in] Token This parameter describes the token that was passed into DispatchProcedure or\r
1362 BroadcastProcedure.\r
1363\r
1364 @retval TRUE The input token is the current used token.\r
1365 @retval FALSE The input token is not the current used token.\r
1366**/\r
1367BOOLEAN\r
1368IsTokenInUse (\r
053e878b 1369 IN SPIN_LOCK *Token\r
51dd408a
ED
1370 );\r
1371\r
1372/**\r
1373 Checks status of specified AP.\r
1374\r
1375 This function checks whether the specified AP has finished the task assigned\r
1376 by StartupThisAP(), and whether timeout expires.\r
1377\r
1378 @param[in] Token This parameter describes the token that was passed into DispatchProcedure or\r
1379 BroadcastProcedure.\r
1380\r
1381 @retval EFI_SUCCESS Specified AP has finished task assigned by StartupThisAPs().\r
1382 @retval EFI_NOT_READY Specified AP has not finished task and timeout has not expired.\r
1383**/\r
1384EFI_STATUS\r
1385IsApReady (\r
1386 IN SPIN_LOCK *Token\r
1387 );\r
1388\r
1389/**\r
1390 Check whether it is an present AP.\r
1391\r
1392 @param CpuIndex The AP index which calls this function.\r
1393\r
1394 @retval TRUE It's a present AP.\r
1395 @retval TRUE This is not an AP or it is not present.\r
1396\r
1397**/\r
1398BOOLEAN\r
1399IsPresentAp (\r
053e878b 1400 IN UINTN CpuIndex\r
51dd408a
ED
1401 );\r
1402\r
1403/**\r
1404 Worker function to execute a caller provided function on all enabled APs.\r
1405\r
1406 @param[in] Procedure A pointer to the function to be run on\r
1407 enabled APs of the system.\r
1408 @param[in] TimeoutInMicroseconds Indicates the time limit in microseconds for\r
1409 APs to return from Procedure, either for\r
1410 blocking or non-blocking mode.\r
5ed4c46f 1411 @param[in,out] ProcedureArguments The parameter passed into Procedure for\r
51dd408a
ED
1412 all APs.\r
1413 @param[in,out] Token This is an optional parameter that allows the caller to execute the\r
1414 procedure in a blocking or non-blocking fashion. If it is NULL the\r
1415 call is blocking, and the call will not return until the AP has\r
1416 completed the procedure. If the token is not NULL, the call will\r
1417 return immediately. The caller can check whether the procedure has\r
1418 completed with CheckOnProcedure or WaitForProcedure.\r
1419 @param[in,out] CPUStatus This optional pointer may be used to get the status code returned\r
1420 by Procedure when it completes execution on the target AP, or with\r
1421 EFI_TIMEOUT if the Procedure fails to complete within the optional\r
1422 timeout. The implementation will update this variable with\r
1423 EFI_NOT_READY prior to starting Procedure on the target AP.\r
1424\r
1425 @retval EFI_SUCCESS In blocking mode, all APs have finished before\r
1426 the timeout expired.\r
1427 @retval EFI_SUCCESS In non-blocking mode, function has been dispatched\r
1428 to all enabled APs.\r
1429 @retval others Failed to Startup all APs.\r
1430\r
1431**/\r
1432EFI_STATUS\r
1433InternalSmmStartupAllAPs (\r
053e878b
MK
1434 IN EFI_AP_PROCEDURE2 Procedure,\r
1435 IN UINTN TimeoutInMicroseconds,\r
1436 IN OUT VOID *ProcedureArguments OPTIONAL,\r
1437 IN OUT MM_COMPLETION *Token,\r
1438 IN OUT EFI_STATUS *CPUStatus\r
51dd408a
ED
1439 );\r
1440\r
1441/**\r
1442\r
1443 Register the SMM Foundation entry point.\r
1444\r
1445 @param[in] Procedure A pointer to the code stream to be run on the designated target AP\r
1446 of the system. Type EFI_AP_PROCEDURE is defined below in Volume 2\r
1447 with the related definitions of\r
1448 EFI_MP_SERVICES_PROTOCOL.StartupAllAPs.\r
1449 If caller may pass a value of NULL to deregister any existing\r
1450 startup procedure.\r
1451 @param[in,out] ProcedureArguments Allows the caller to pass a list of parameters to the code that is\r
1452 run by the AP. It is an optional common mailbox between APs and\r
1453 the caller to share information\r
1454\r
1455 @retval EFI_SUCCESS The Procedure has been set successfully.\r
1456 @retval EFI_INVALID_PARAMETER The Procedure is NULL but ProcedureArguments not NULL.\r
1457\r
1458**/\r
1459EFI_STATUS\r
1460RegisterStartupProcedure (\r
053e878b
MK
1461 IN EFI_AP_PROCEDURE Procedure,\r
1462 IN OUT VOID *ProcedureArguments OPTIONAL\r
51dd408a
ED
1463 );\r
1464\r
1465/**\r
1466 Allocate buffer for SpinLock and Wrapper function buffer.\r
1467\r
1468**/\r
1469VOID\r
1470InitializeDataForMmMp (\r
1471 VOID\r
1472 );\r
1473\r
79186ddc
RN
1474/**\r
1475 Return whether access to non-SMRAM is restricted.\r
1476\r
1477 @retval TRUE Access to non-SMRAM is restricted.\r
1478 @retval FALSE Access to non-SMRAM is not restricted.\r
9c33f16f 1479**/\r
79186ddc
RN
1480BOOLEAN\r
1481IsRestrictedMemoryAccess (\r
1482 VOID\r
1483 );\r
1484\r
4a68176c
LZ
1485/**\r
1486 Choose blocking or non-blocking mode to Wait for all APs.\r
1487\r
1488 @param[in] This A pointer to the EDKII_SMM_CPU_RENDEZVOUS_PROTOCOL instance.\r
1489 @param[in] BlockingMode Blocking or non-blocking mode.\r
1490\r
1491 @retval EFI_SUCCESS All APs have arrived SMM mode except SMI disabled APs.\r
1492 @retval EFI_TIMEOUT There are APs not in SMM mode in given timeout constraint.\r
1493\r
1494**/\r
1495EFI_STATUS\r
1496EFIAPI\r
1497SmmCpuRendezvous (\r
1498 IN EDKII_SMM_CPU_RENDEZVOUS_PROTOCOL *This,\r
1499 IN BOOLEAN BlockingMode\r
1500 );\r
1501\r
1502/**\r
1503 Insure when this function returns, no AP will execute normal mode code before entering SMM, except SMI disabled APs.\r
1504\r
1505**/\r
1506VOID\r
1507SmmWaitForApArrival (\r
1508 VOID\r
1509 );\r
1510\r
529a5a86 1511#endif\r