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