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