]> git.proxmox.com Git - mirror_edk2.git/blame - UefiCpuPkg/PiSmmCpuDxeSmm/PiSmmCpuDxeSmm.h
UefiCpuPkg/PiSmmCpuDxeSmm: patch "gSmmCr4" with PatchInstructionX86()
[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
6e601a41 4Copyright (c) 2009 - 2018, Intel Corporation. All rights reserved.<BR>\r
241f9149
LD
5Copyright (c) 2017, AMD Incorporated. All rights reserved.<BR>\r
6\r
529a5a86
MK
7This program and the accompanying materials\r
8are licensed and made available under the terms and conditions of the BSD License\r
9which accompanies this distribution. The full text of the license may be found at\r
10http://opensource.org/licenses/bsd-license.php\r
11\r
12THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
13WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
14\r
15**/\r
16\r
17#ifndef _CPU_PISMMCPUDXESMM_H_\r
18#define _CPU_PISMMCPUDXESMM_H_\r
19\r
20#include <PiSmm.h>\r
21\r
22#include <Protocol/MpService.h>\r
23#include <Protocol/SmmConfiguration.h>\r
24#include <Protocol/SmmCpu.h>\r
25#include <Protocol/SmmAccess2.h>\r
529a5a86
MK
26#include <Protocol/SmmReadyToLock.h>\r
27#include <Protocol/SmmCpuService.h>\r
827330cc 28#include <Protocol/SmmMemoryAttribute.h>\r
529a5a86
MK
29\r
30#include <Guid/AcpiS3Context.h>\r
717fb604 31#include <Guid/PiSmmMemoryAttributesTable.h>\r
529a5a86
MK
32\r
33#include <Library/BaseLib.h>\r
34#include <Library/IoLib.h>\r
35#include <Library/TimerLib.h>\r
529a5a86
MK
36#include <Library/SynchronizationLib.h>\r
37#include <Library/DebugLib.h>\r
38#include <Library/BaseMemoryLib.h>\r
39#include <Library/PcdLib.h>\r
40#include <Library/CacheMaintenanceLib.h>\r
41#include <Library/MtrrLib.h>\r
42#include <Library/SmmCpuPlatformHookLib.h>\r
43#include <Library/SmmServicesTableLib.h>\r
44#include <Library/MemoryAllocationLib.h>\r
45#include <Library/UefiBootServicesTableLib.h>\r
46#include <Library/UefiRuntimeServicesTableLib.h>\r
47#include <Library/DebugAgentLib.h>\r
48#include <Library/HobLib.h>\r
49#include <Library/LocalApicLib.h>\r
50#include <Library/UefiCpuLib.h>\r
51#include <Library/CpuExceptionHandlerLib.h>\r
52#include <Library/ReportStatusCodeLib.h>\r
53#include <Library/SmmCpuFeaturesLib.h>\r
54#include <Library/PeCoffGetEntryPointLib.h>\r
55\r
56#include <AcpiCpuData.h>\r
57#include <CpuHotPlugData.h>\r
58\r
59#include <Register/Cpuid.h>\r
f85d3ce2 60#include <Register/Msr.h>\r
529a5a86
MK
61\r
62#include "CpuService.h"\r
63#include "SmmProfile.h"\r
64\r
65//\r
66// MSRs required for configuration of SMM Code Access Check\r
67//\r
68#define EFI_MSR_SMM_MCA_CAP 0x17D\r
69#define SMM_CODE_ACCESS_CHK_BIT BIT58\r
70\r
71#define SMM_FEATURE_CONTROL_LOCK_BIT BIT0\r
72#define SMM_CODE_CHK_EN_BIT BIT2\r
73\r
74///\r
75/// Page Table Entry\r
76///\r
77#define IA32_PG_P BIT0\r
78#define IA32_PG_RW BIT1\r
881520ea 79#define IA32_PG_U BIT2\r
529a5a86
MK
80#define IA32_PG_WT BIT3\r
81#define IA32_PG_CD BIT4\r
82#define IA32_PG_A BIT5\r
881520ea 83#define IA32_PG_D BIT6\r
529a5a86
MK
84#define IA32_PG_PS BIT7\r
85#define IA32_PG_PAT_2M BIT12\r
86#define IA32_PG_PAT_4K IA32_PG_PS\r
87#define IA32_PG_PMNT BIT62\r
88#define IA32_PG_NX BIT63\r
89\r
717fb604 90#define PAGE_ATTRIBUTE_BITS (IA32_PG_D | IA32_PG_A | IA32_PG_U | IA32_PG_RW | IA32_PG_P)\r
881520ea
JY
91//\r
92// Bits 1, 2, 5, 6 are reserved in the IA32 PAE PDPTE\r
93// X64 PAE PDPTE does not have such restriction\r
94//\r
95#define IA32_PAE_PDPTE_ATTRIBUTE_BITS (IA32_PG_P)\r
96\r
717fb604
JY
97#define PAGE_PROGATE_BITS (IA32_PG_NX | PAGE_ATTRIBUTE_BITS)\r
98\r
99#define PAGING_4K_MASK 0xFFF\r
100#define PAGING_2M_MASK 0x1FFFFF\r
101#define PAGING_1G_MASK 0x3FFFFFFF\r
102\r
103#define PAGING_PAE_INDEX_MASK 0x1FF\r
104\r
105#define PAGING_4K_ADDRESS_MASK_64 0x000FFFFFFFFFF000ull\r
106#define PAGING_2M_ADDRESS_MASK_64 0x000FFFFFFFE00000ull\r
107#define PAGING_1G_ADDRESS_MASK_64 0x000FFFFFC0000000ull\r
108\r
7ed6f781
JF
109#define SMRR_MAX_ADDRESS BASE_4GB\r
110\r
717fb604
JY
111typedef enum {\r
112 PageNone,\r
113 Page4K,\r
114 Page2M,\r
115 Page1G,\r
116} PAGE_ATTRIBUTE;\r
117\r
118typedef struct {\r
119 PAGE_ATTRIBUTE Attribute;\r
120 UINT64 Length;\r
121 UINT64 AddressMask;\r
122} PAGE_ATTRIBUTE_TABLE;\r
123\r
529a5a86
MK
124//\r
125// Size of Task-State Segment defined in IA32 Manual\r
126//\r
127#define TSS_SIZE 104\r
128#define TSS_X64_IST1_OFFSET 36\r
129#define TSS_IA32_CR3_OFFSET 28\r
130#define TSS_IA32_ESP_OFFSET 56\r
131\r
717fb604
JY
132#define CR0_WP BIT16\r
133\r
529a5a86
MK
134//\r
135// Code select value\r
136//\r
137#define PROTECT_MODE_CODE_SEGMENT 0x08\r
138#define LONG_MODE_CODE_SEGMENT 0x38\r
139\r
140//\r
141// The size 0x20 must be bigger than\r
142// the size of template code of SmmInit. Currently,\r
143// the size of SmmInit requires the 0x16 Bytes buffer\r
144// at least.\r
145//\r
146#define BACK_BUF_SIZE 0x20\r
147\r
148#define EXCEPTION_VECTOR_NUMBER 0x20\r
149\r
150#define INVALID_APIC_ID 0xFFFFFFFFFFFFFFFFULL\r
151\r
152typedef UINT32 SMM_CPU_ARRIVAL_EXCEPTIONS;\r
153#define ARRIVAL_EXCEPTION_BLOCKED 0x1\r
154#define ARRIVAL_EXCEPTION_DELAYED 0x2\r
155#define ARRIVAL_EXCEPTION_SMI_DISABLED 0x4\r
156\r
157//\r
158// Private structure for the SMM CPU module that is stored in DXE Runtime memory\r
159// Contains the SMM Configuration Protocols that is produced.\r
160// Contains a mix of DXE and SMM contents. All the fields must be used properly.\r
161//\r
162#define SMM_CPU_PRIVATE_DATA_SIGNATURE SIGNATURE_32 ('s', 'c', 'p', 'u')\r
163\r
164typedef struct {\r
165 UINTN Signature;\r
166\r
167 EFI_HANDLE SmmCpuHandle;\r
168\r
169 EFI_PROCESSOR_INFORMATION *ProcessorInfo;\r
170 SMM_CPU_OPERATION *Operation;\r
171 UINTN *CpuSaveStateSize;\r
172 VOID **CpuSaveState;\r
173\r
174 EFI_SMM_RESERVED_SMRAM_REGION SmmReservedSmramRegion[1];\r
175 EFI_SMM_ENTRY_CONTEXT SmmCoreEntryContext;\r
176 EFI_SMM_ENTRY_POINT SmmCoreEntry;\r
177\r
178 EFI_SMM_CONFIGURATION_PROTOCOL SmmConfiguration;\r
179} SMM_CPU_PRIVATE_DATA;\r
180\r
181extern SMM_CPU_PRIVATE_DATA *gSmmCpuPrivate;\r
182extern CPU_HOT_PLUG_DATA mCpuHotPlugData;\r
183extern UINTN mMaxNumberOfCpus;\r
184extern UINTN mNumberOfCpus;\r
529a5a86
MK
185extern EFI_SMM_CPU_PROTOCOL mSmmCpu;\r
186\r
187///\r
188/// The mode of the CPU at the time an SMI occurs\r
189///\r
190extern UINT8 mSmmSaveStateRegisterLma;\r
191\r
529a5a86
MK
192//\r
193// SMM CPU Protocol function prototypes.\r
194//\r
195\r
196/**\r
197 Read information from the CPU save state.\r
198\r
199 @param This EFI_SMM_CPU_PROTOCOL instance\r
200 @param Width The number of bytes to read from the CPU save state.\r
201 @param Register Specifies the CPU register to read form the save state.\r
202 @param CpuIndex Specifies the zero-based index of the CPU save state\r
203 @param Buffer Upon return, this holds the CPU register value read from the save state.\r
204\r
205 @retval EFI_SUCCESS The register was read from Save State\r
206 @retval EFI_NOT_FOUND The register is not defined for the Save State of Processor\r
207 @retval EFI_INVALID_PARAMTER This or Buffer is NULL.\r
208\r
209**/\r
210EFI_STATUS\r
211EFIAPI\r
212SmmReadSaveState (\r
213 IN CONST EFI_SMM_CPU_PROTOCOL *This,\r
214 IN UINTN Width,\r
215 IN EFI_SMM_SAVE_STATE_REGISTER Register,\r
216 IN UINTN CpuIndex,\r
217 OUT VOID *Buffer\r
218 );\r
219\r
220/**\r
221 Write data to the CPU save state.\r
222\r
223 @param This EFI_SMM_CPU_PROTOCOL instance\r
224 @param Width The number of bytes to read from the CPU save state.\r
225 @param Register Specifies the CPU register to write to the save state.\r
226 @param CpuIndex Specifies the zero-based index of the CPU save state\r
227 @param Buffer Upon entry, this holds the new CPU register value.\r
228\r
229 @retval EFI_SUCCESS The register was written from Save State\r
230 @retval EFI_NOT_FOUND The register is not defined for the Save State of Processor\r
231 @retval EFI_INVALID_PARAMTER ProcessorIndex or Width is not correct\r
232\r
233**/\r
234EFI_STATUS\r
235EFIAPI\r
236SmmWriteSaveState (\r
237 IN CONST EFI_SMM_CPU_PROTOCOL *This,\r
238 IN UINTN Width,\r
239 IN EFI_SMM_SAVE_STATE_REGISTER Register,\r
240 IN UINTN CpuIndex,\r
241 IN CONST VOID *Buffer\r
242 );\r
243\r
244/**\r
245Read a CPU Save State register on the target processor.\r
246\r
247This function abstracts the differences that whether the CPU Save State register is in the\r
248IA32 CPU Save State Map or X64 CPU Save State Map.\r
249\r
250This function supports reading a CPU Save State register in SMBase relocation handler.\r
251\r
252@param[in] CpuIndex Specifies the zero-based index of the CPU save state.\r
253@param[in] RegisterIndex Index into mSmmCpuWidthOffset[] look up table.\r
254@param[in] Width The number of bytes to read from the CPU save state.\r
255@param[out] Buffer Upon return, this holds the CPU register value read from the save state.\r
256\r
257@retval EFI_SUCCESS The register was read from Save State.\r
258@retval EFI_NOT_FOUND The register is not defined for the Save State of Processor.\r
259@retval EFI_INVALID_PARAMTER This or Buffer is NULL.\r
260\r
261**/\r
262EFI_STATUS\r
263EFIAPI\r
264ReadSaveStateRegister (\r
265 IN UINTN CpuIndex,\r
266 IN EFI_SMM_SAVE_STATE_REGISTER Register,\r
267 IN UINTN Width,\r
268 OUT VOID *Buffer\r
269 );\r
270\r
271/**\r
272Write value to a CPU Save State register on the target processor.\r
273\r
274This function abstracts the differences that whether the CPU Save State register is in the\r
275IA32 CPU Save State Map or X64 CPU Save State Map.\r
276\r
277This function supports writing a CPU Save State register in SMBase relocation handler.\r
278\r
279@param[in] CpuIndex Specifies the zero-based index of the CPU save state.\r
280@param[in] RegisterIndex Index into mSmmCpuWidthOffset[] look up table.\r
281@param[in] Width The number of bytes to read from the CPU save state.\r
282@param[in] Buffer Upon entry, this holds the new CPU register value.\r
283\r
284@retval EFI_SUCCESS The register was written to Save State.\r
285@retval EFI_NOT_FOUND The register is not defined for the Save State of Processor.\r
286@retval EFI_INVALID_PARAMTER ProcessorIndex or Width is not correct.\r
287\r
288**/\r
289EFI_STATUS\r
290EFIAPI\r
291WriteSaveStateRegister (\r
292 IN UINTN CpuIndex,\r
293 IN EFI_SMM_SAVE_STATE_REGISTER Register,\r
294 IN UINTN Width,\r
295 IN CONST VOID *Buffer\r
296 );\r
297\r
298//\r
299//\r
300//\r
301typedef struct {\r
302 UINT32 Offset;\r
303 UINT16 Segment;\r
304 UINT16 Reserved;\r
305} IA32_FAR_ADDRESS;\r
306\r
307extern IA32_FAR_ADDRESS gSmmJmpAddr;\r
308\r
309extern CONST UINT8 gcSmmInitTemplate[];\r
310extern CONST UINT16 gcSmmInitSize;\r
311extern UINT32 gSmmCr0;\r
6b0841c1 312X86_ASSEMBLY_PATCH_LABEL gPatchSmmCr3;\r
351b49c1
LE
313extern UINT32 mSmmCr4;\r
314X86_ASSEMBLY_PATCH_LABEL gPatchSmmCr4;\r
529a5a86
MK
315extern UINTN gSmmInitStack;\r
316\r
317/**\r
318 Semaphore operation for all processor relocate SMMBase.\r
319**/\r
320VOID\r
321EFIAPI\r
322SmmRelocationSemaphoreComplete (\r
323 VOID\r
324 );\r
325\r
326///\r
327/// The type of SMM CPU Information\r
328///\r
329typedef struct {\r
ed3d5ecb 330 SPIN_LOCK *Busy;\r
529a5a86
MK
331 volatile EFI_AP_PROCEDURE Procedure;\r
332 volatile VOID *Parameter;\r
ed3d5ecb
JF
333 volatile UINT32 *Run;\r
334 volatile BOOLEAN *Present;\r
529a5a86
MK
335} SMM_CPU_DATA_BLOCK;\r
336\r
337typedef enum {\r
338 SmmCpuSyncModeTradition,\r
339 SmmCpuSyncModeRelaxedAp,\r
340 SmmCpuSyncModeMax\r
341} SMM_CPU_SYNC_MODE;\r
342\r
343typedef struct {\r
344 //\r
345 // Pointer to an array. The array should be located immediately after this structure\r
346 // so that UC cache-ability can be set together.\r
347 //\r
348 SMM_CPU_DATA_BLOCK *CpuData;\r
fe3a75bc 349 volatile UINT32 *Counter;\r
529a5a86 350 volatile UINT32 BspIndex;\r
fe3a75bc
JF
351 volatile BOOLEAN *InsideSmm;\r
352 volatile BOOLEAN *AllCpusInSync;\r
529a5a86
MK
353 volatile SMM_CPU_SYNC_MODE EffectiveSyncMode;\r
354 volatile BOOLEAN SwitchBsp;\r
355 volatile BOOLEAN *CandidateBsp;\r
356} SMM_DISPATCHER_MP_SYNC_DATA;\r
357\r
695e62d1
JF
358#define MSR_SPIN_LOCK_INIT_NUM 15\r
359\r
529a5a86 360typedef struct {\r
dc99315b 361 SPIN_LOCK *SpinLock;\r
529a5a86
MK
362 UINT32 MsrIndex;\r
363} MP_MSR_LOCK;\r
364\r
365#define SMM_PSD_OFFSET 0xfb00\r
366\r
1d648531
JF
367///\r
368/// All global semaphores' pointer\r
369///\r
370typedef struct {\r
371 volatile UINT32 *Counter;\r
372 volatile BOOLEAN *InsideSmm;\r
373 volatile BOOLEAN *AllCpusInSync;\r
374 SPIN_LOCK *PFLock;\r
375 SPIN_LOCK *CodeAccessCheckLock;\r
6c4c15fa 376 SPIN_LOCK *MemoryMappedLock;\r
1d648531
JF
377} SMM_CPU_SEMAPHORE_GLOBAL;\r
378\r
4e920581
JF
379///\r
380/// All semaphores for each processor\r
381///\r
382typedef struct {\r
383 SPIN_LOCK *Busy;\r
384 volatile UINT32 *Run;\r
385 volatile BOOLEAN *Present;\r
386} SMM_CPU_SEMAPHORE_CPU;\r
387\r
695e62d1
JF
388///\r
389/// All MSRs semaphores' pointer and counter\r
390///\r
391typedef struct {\r
392 SPIN_LOCK *Msr;\r
393 UINTN AvailableCounter;\r
394} SMM_CPU_SEMAPHORE_MSR;\r
4e920581 395\r
1d648531
JF
396///\r
397/// All semaphores' information\r
398///\r
399typedef struct {\r
400 SMM_CPU_SEMAPHORE_GLOBAL SemaphoreGlobal;\r
4e920581 401 SMM_CPU_SEMAPHORE_CPU SemaphoreCpu;\r
695e62d1 402 SMM_CPU_SEMAPHORE_MSR SemaphoreMsr;\r
1d648531
JF
403} SMM_CPU_SEMAPHORES;\r
404\r
529a5a86 405extern IA32_DESCRIPTOR gcSmiGdtr;\r
717fb604
JY
406extern EFI_PHYSICAL_ADDRESS mGdtBuffer;\r
407extern UINTN mGdtBufferSize;\r
529a5a86
MK
408extern IA32_DESCRIPTOR gcSmiIdtr;\r
409extern VOID *gcSmiIdtrPtr;\r
529a5a86 410extern UINT64 gPhyMask;\r
529a5a86 411extern SMM_DISPATCHER_MP_SYNC_DATA *mSmmMpSyncData;\r
529a5a86
MK
412extern UINTN mSmmStackArrayBase;\r
413extern UINTN mSmmStackArrayEnd;\r
414extern UINTN mSmmStackSize;\r
415extern EFI_SMM_CPU_SERVICE_PROTOCOL mSmmCpuService;\r
416extern IA32_DESCRIPTOR gcSmiInitGdtr;\r
dc99315b
JF
417extern SMM_CPU_SEMAPHORES mSmmCpuSemaphores;\r
418extern UINTN mSemaphoreSize;\r
fe3a75bc
JF
419extern SPIN_LOCK *mPFLock;\r
420extern SPIN_LOCK *mConfigSmmCodeAccessCheckLock;\r
6c4c15fa 421extern SPIN_LOCK *mMemoryMappedLock;\r
7ed6f781
JF
422extern EFI_SMRAM_DESCRIPTOR *mSmmCpuSmramRanges;\r
423extern UINTN mSmmCpuSmramRangeCount;\r
714c2603 424extern UINT8 mPhysicalAddressBits;\r
529a5a86 425\r
241f9149
LD
426//\r
427// Copy of the PcdPteMemoryEncryptionAddressOrMask\r
428//\r
429extern UINT64 mAddressEncMask;\r
430\r
529a5a86
MK
431/**\r
432 Create 4G PageTable in SMRAM.\r
433\r
717fb604 434 @param[in] Is32BitPageTable Whether the page table is 32-bit PAE\r
529a5a86
MK
435 @return PageTable Address\r
436\r
437**/\r
438UINT32\r
439Gen4GPageTable (\r
881520ea 440 IN BOOLEAN Is32BitPageTable\r
529a5a86
MK
441 );\r
442\r
443\r
444/**\r
445 Initialize global data for MP synchronization.\r
446\r
447 @param Stacks Base address of SMI stack buffer for all processors.\r
448 @param StackSize Stack size for each processor in SMM.\r
449\r
450**/\r
451UINT32\r
452InitializeMpServiceData (\r
453 IN VOID *Stacks,\r
454 IN UINTN StackSize\r
455 );\r
456\r
457/**\r
458 Initialize Timer for SMM AP Sync.\r
459\r
460**/\r
461VOID\r
462InitializeSmmTimer (\r
463 VOID\r
464 );\r
465\r
466/**\r
467 Start Timer for SMM AP Sync.\r
468\r
469**/\r
470UINT64\r
471EFIAPI\r
472StartSyncTimer (\r
473 VOID\r
474 );\r
475\r
476/**\r
477 Check if the SMM AP Sync timer is timeout.\r
478\r
479 @param Timer The start timer from the begin.\r
480\r
481**/\r
482BOOLEAN\r
483EFIAPI\r
484IsSyncTimerTimeout (\r
485 IN UINT64 Timer\r
486 );\r
487\r
488/**\r
489 Initialize IDT for SMM Stack Guard.\r
490\r
491**/\r
492VOID\r
493EFIAPI\r
494InitializeIDTSmmStackGuard (\r
495 VOID\r
496 );\r
497\r
fe5f1949
JY
498/**\r
499 Initialize Gdt for all processors.\r
717fb604 500\r
fe5f1949
JY
501 @param[in] Cr3 CR3 value.\r
502 @param[out] GdtStepSize The step size for GDT table.\r
503\r
504 @return GdtBase for processor 0.\r
505 GdtBase for processor X is: GdtBase + (GdtStepSize * X)\r
506**/\r
507VOID *\r
508InitGdt (\r
509 IN UINTN Cr3,\r
510 OUT UINTN *GdtStepSize\r
511 );\r
512\r
529a5a86
MK
513/**\r
514\r
515 Register the SMM Foundation entry point.\r
516\r
517 @param This Pointer to EFI_SMM_CONFIGURATION_PROTOCOL instance\r
518 @param SmmEntryPoint SMM Foundation EntryPoint\r
519\r
520 @retval EFI_SUCCESS Successfully to register SMM foundation entry point\r
521\r
522**/\r
523EFI_STATUS\r
524EFIAPI\r
525RegisterSmmEntry (\r
526 IN CONST EFI_SMM_CONFIGURATION_PROTOCOL *This,\r
527 IN EFI_SMM_ENTRY_POINT SmmEntryPoint\r
528 );\r
529\r
530/**\r
531 Create PageTable for SMM use.\r
532\r
533 @return PageTable Address\r
534\r
535**/\r
536UINT32\r
537SmmInitPageTable (\r
538 VOID\r
539 );\r
540\r
541/**\r
542 Schedule a procedure to run on the specified CPU.\r
543\r
544 @param Procedure The address of the procedure to run\r
545 @param CpuIndex Target CPU number\r
546 @param ProcArguments The parameter to pass to the procedure\r
547\r
548 @retval EFI_INVALID_PARAMETER CpuNumber not valid\r
549 @retval EFI_INVALID_PARAMETER CpuNumber specifying BSP\r
550 @retval EFI_INVALID_PARAMETER The AP specified by CpuNumber did not enter SMM\r
551 @retval EFI_INVALID_PARAMETER The AP specified by CpuNumber is busy\r
552 @retval EFI_SUCCESS - The procedure has been successfully scheduled\r
553\r
554**/\r
555EFI_STATUS\r
556EFIAPI\r
557SmmStartupThisAp (\r
558 IN EFI_AP_PROCEDURE Procedure,\r
559 IN UINTN CpuIndex,\r
560 IN OUT VOID *ProcArguments OPTIONAL\r
561 );\r
562\r
563/**\r
564 Schedule a procedure to run on the specified CPU in a blocking fashion.\r
565\r
566 @param Procedure The address of the procedure to run\r
567 @param CpuIndex Target CPU Index\r
568 @param ProcArguments The parameter to pass to the procedure\r
569\r
570 @retval EFI_INVALID_PARAMETER CpuNumber not valid\r
571 @retval EFI_INVALID_PARAMETER CpuNumber specifying BSP\r
572 @retval EFI_INVALID_PARAMETER The AP specified by CpuNumber did not enter SMM\r
573 @retval EFI_INVALID_PARAMETER The AP specified by CpuNumber is busy\r
574 @retval EFI_SUCCESS The procedure has been successfully scheduled\r
575\r
576**/\r
577EFI_STATUS\r
578EFIAPI\r
579SmmBlockingStartupThisAp (\r
580 IN EFI_AP_PROCEDURE Procedure,\r
581 IN UINTN CpuIndex,\r
582 IN OUT VOID *ProcArguments OPTIONAL\r
583 );\r
584\r
e4435f71
JY
585/**\r
586 This function sets the attributes for the memory region specified by BaseAddress and\r
587 Length from their current attributes to the attributes specified by Attributes.\r
588\r
589 @param[in] BaseAddress The physical address that is the start address of a memory region.\r
590 @param[in] Length The size in bytes of the memory region.\r
591 @param[in] Attributes The bit mask of attributes to set for the memory region.\r
592\r
593 @retval EFI_SUCCESS The attributes were set for the memory region.\r
594 @retval EFI_ACCESS_DENIED The attributes for the memory resource range specified by\r
595 BaseAddress and Length cannot be modified.\r
596 @retval EFI_INVALID_PARAMETER Length is zero.\r
597 Attributes specified an illegal combination of attributes that\r
598 cannot be set together.\r
599 @retval EFI_OUT_OF_RESOURCES There are not enough system resources to modify the attributes of\r
600 the memory resource range.\r
601 @retval EFI_UNSUPPORTED The processor does not support one or more bytes of the memory\r
602 resource range specified by BaseAddress and Length.\r
603 The bit mask of attributes is not support for the memory resource\r
604 range specified by BaseAddress and Length.\r
605\r
606**/\r
607EFI_STATUS\r
608EFIAPI\r
609SmmSetMemoryAttributes (\r
610 IN EFI_PHYSICAL_ADDRESS BaseAddress,\r
611 IN UINT64 Length,\r
612 IN UINT64 Attributes\r
613 );\r
614\r
615/**\r
616 This function clears the attributes for the memory region specified by BaseAddress and\r
617 Length from their current attributes to the attributes specified by Attributes.\r
618\r
619 @param[in] BaseAddress The physical address that is the start address of a memory region.\r
620 @param[in] Length The size in bytes of the memory region.\r
621 @param[in] Attributes The bit mask of attributes to clear for the memory region.\r
622\r
623 @retval EFI_SUCCESS The attributes were cleared for the memory region.\r
624 @retval EFI_ACCESS_DENIED The attributes for the memory resource range specified by\r
625 BaseAddress and Length cannot be modified.\r
626 @retval EFI_INVALID_PARAMETER Length is zero.\r
627 Attributes specified an illegal combination of attributes that\r
628 cannot be set together.\r
629 @retval EFI_OUT_OF_RESOURCES There are not enough system resources to modify the attributes of\r
630 the memory resource range.\r
631 @retval EFI_UNSUPPORTED The processor does not support one or more bytes of the memory\r
632 resource range specified by BaseAddress and Length.\r
633 The bit mask of attributes is not support for the memory resource\r
634 range specified by BaseAddress and Length.\r
635\r
636**/\r
637EFI_STATUS\r
638EFIAPI\r
639SmmClearMemoryAttributes (\r
640 IN EFI_PHYSICAL_ADDRESS BaseAddress,\r
641 IN UINT64 Length,\r
642 IN UINT64 Attributes\r
643 );\r
644\r
529a5a86
MK
645/**\r
646 Initialize MP synchronization data.\r
647\r
648**/\r
649VOID\r
650EFIAPI\r
651InitializeMpSyncData (\r
652 VOID\r
653 );\r
654\r
655/**\r
656\r
657 Find out SMRAM information including SMRR base and SMRR size.\r
658\r
659 @param SmrrBase SMRR base\r
660 @param SmrrSize SMRR size\r
661\r
662**/\r
663VOID\r
664FindSmramInfo (\r
665 OUT UINT32 *SmrrBase,\r
666 OUT UINT32 *SmrrSize\r
667 );\r
668\r
669/**\r
0bdc9e75 670 Relocate SmmBases for each processor.\r
529a5a86 671\r
0bdc9e75 672 Execute on first boot and all S3 resumes\r
529a5a86
MK
673\r
674**/\r
675VOID\r
0bdc9e75
SZ
676EFIAPI\r
677SmmRelocateBases (\r
529a5a86
MK
678 VOID\r
679 );\r
680\r
681/**\r
682 Page Fault handler for SMM use.\r
683\r
684 @param InterruptType Defines the type of interrupt or exception that\r
685 occurred on the processor.This parameter is processor architecture specific.\r
686 @param SystemContext A pointer to the processor context when\r
687 the interrupt occurred on the processor.\r
688**/\r
689VOID\r
690EFIAPI\r
691SmiPFHandler (\r
b8caae19
JF
692 IN EFI_EXCEPTION_TYPE InterruptType,\r
693 IN EFI_SYSTEM_CONTEXT SystemContext\r
529a5a86
MK
694 );\r
695\r
696/**\r
697 Perform the remaining tasks.\r
698\r
699**/\r
700VOID\r
701PerformRemainingTasks (\r
702 VOID\r
703 );\r
704\r
9f419739
JY
705/**\r
706 Perform the pre tasks.\r
707\r
708**/\r
709VOID\r
710PerformPreTasks (\r
711 VOID\r
712 );\r
713\r
529a5a86
MK
714/**\r
715 Initialize MSR spin lock by MSR index.\r
716\r
717 @param MsrIndex MSR index value.\r
718\r
719**/\r
720VOID\r
721InitMsrSpinLockByIndex (\r
722 IN UINT32 MsrIndex\r
723 );\r
724\r
725/**\r
726 Hook return address of SMM Save State so that semaphore code\r
727 can be executed immediately after AP exits SMM to indicate to\r
728 the BSP that an AP has exited SMM after SMBASE relocation.\r
729\r
730 @param[in] CpuIndex The processor index.\r
731 @param[in] RebasedFlag A pointer to a flag that is set to TRUE\r
732 immediately after AP exits SMM.\r
733\r
734**/\r
735VOID\r
736SemaphoreHook (\r
737 IN UINTN CpuIndex,\r
738 IN volatile BOOLEAN *RebasedFlag\r
739 );\r
740\r
741/**\r
742Configure SMM Code Access Check feature for all processors.\r
743SMM Feature Control MSR will be locked after configuration.\r
744**/\r
745VOID\r
746ConfigSmmCodeAccessCheck (\r
747 VOID\r
748 );\r
749\r
750/**\r
751 Hook the code executed immediately after an RSM instruction on the currently\r
752 executing CPU. The mode of code executed immediately after RSM must be\r
753 detected, and the appropriate hook must be selected. Always clear the auto\r
754 HALT restart flag if it is set.\r
755\r
756 @param[in] CpuIndex The processor index for the currently\r
757 executing CPU.\r
758 @param[in] CpuState Pointer to SMRAM Save State Map for the\r
759 currently executing CPU.\r
760 @param[in] NewInstructionPointer32 Instruction pointer to use if resuming to\r
761 32-bit mode from 64-bit SMM.\r
762 @param[in] NewInstructionPointer Instruction pointer to use if resuming to\r
763 same mode as SMM.\r
764\r
765 @retval The value of the original instruction pointer before it was hooked.\r
766\r
767**/\r
768UINT64\r
769EFIAPI\r
770HookReturnFromSmm (\r
771 IN UINTN CpuIndex,\r
772 SMRAM_SAVE_STATE_MAP *CpuState,\r
773 UINT64 NewInstructionPointer32,\r
774 UINT64 NewInstructionPointer\r
775 );\r
776\r
777/**\r
778 Get the size of the SMI Handler in bytes.\r
779\r
780 @retval The size, in bytes, of the SMI Handler.\r
781\r
782**/\r
783UINTN\r
784EFIAPI\r
785GetSmiHandlerSize (\r
786 VOID\r
787 );\r
788\r
789/**\r
790 Install the SMI handler for the CPU specified by CpuIndex. This function\r
791 is called by the CPU that was elected as monarch during System Management\r
792 Mode initialization.\r
793\r
794 @param[in] CpuIndex The index of the CPU to install the custom SMI handler.\r
795 The value must be between 0 and the NumberOfCpus field\r
796 in the System Management System Table (SMST).\r
797 @param[in] SmBase The SMBASE address for the CPU specified by CpuIndex.\r
798 @param[in] SmiStack The stack to use when an SMI is processed by the\r
799 the CPU specified by CpuIndex.\r
800 @param[in] StackSize The size, in bytes, if the stack used when an SMI is\r
801 processed by the CPU specified by CpuIndex.\r
802 @param[in] GdtBase The base address of the GDT to use when an SMI is\r
803 processed by the CPU specified by CpuIndex.\r
804 @param[in] GdtSize The size, in bytes, of the GDT used when an SMI is\r
805 processed by the CPU specified by CpuIndex.\r
806 @param[in] IdtBase The base address of the IDT to use when an SMI is\r
807 processed by the CPU specified by CpuIndex.\r
808 @param[in] IdtSize The size, in bytes, of the IDT used when an SMI is\r
809 processed by the CPU specified by CpuIndex.\r
810 @param[in] Cr3 The base address of the page tables to use when an SMI\r
811 is processed by the CPU specified by CpuIndex.\r
812**/\r
813VOID\r
814EFIAPI\r
815InstallSmiHandler (\r
816 IN UINTN CpuIndex,\r
817 IN UINT32 SmBase,\r
818 IN VOID *SmiStack,\r
819 IN UINTN StackSize,\r
820 IN UINTN GdtBase,\r
821 IN UINTN GdtSize,\r
822 IN UINTN IdtBase,\r
823 IN UINTN IdtSize,\r
824 IN UINT32 Cr3\r
825 );\r
826\r
827/**\r
828 Search module name by input IP address and output it.\r
829\r
830 @param CallerIpAddress Caller instruction pointer.\r
831\r
832**/\r
833VOID\r
834DumpModuleInfoByIp (\r
835 IN UINTN CallerIpAddress\r
836 );\r
21c17193 837\r
717fb604
JY
838/**\r
839 This function sets memory attribute according to MemoryAttributesTable.\r
840**/\r
841VOID\r
842SetMemMapAttributes (\r
843 VOID\r
844 );\r
845\r
d2fc7711
JY
846/**\r
847 This function sets UEFI memory attribute according to UEFI memory map.\r
848**/\r
849VOID\r
850SetUefiMemMapAttributes (\r
851 VOID\r
852 );\r
853\r
854/**\r
855 Return if the Address is forbidden as SMM communication buffer.\r
856\r
857 @param[in] Address the address to be checked\r
858\r
859 @return TRUE The address is forbidden as SMM communication buffer.\r
860 @return FALSE The address is allowed as SMM communication buffer.\r
861**/\r
862BOOLEAN\r
863IsSmmCommBufferForbiddenAddress (\r
864 IN UINT64 Address\r
865 );\r
866\r
867/**\r
868 This function caches the UEFI memory map information.\r
869**/\r
870VOID\r
871GetUefiMemoryMap (\r
872 VOID\r
873 );\r
874\r
717fb604
JY
875/**\r
876 This function sets memory attribute for page table.\r
877**/\r
878VOID\r
879SetPageTableAttributes (\r
880 VOID\r
881 );\r
882\r
883/**\r
884 Return page table base.\r
885\r
886 @return page table base.\r
887**/\r
888UINTN\r
889GetPageTableBase (\r
890 VOID\r
891 );\r
892\r
893/**\r
894 This function sets the attributes for the memory region specified by BaseAddress and\r
895 Length from their current attributes to the attributes specified by Attributes.\r
896\r
897 @param[in] BaseAddress The physical address that is the start address of a memory region.\r
898 @param[in] Length The size in bytes of the memory region.\r
899 @param[in] Attributes The bit mask of attributes to set for the memory region.\r
900 @param[out] IsSplitted TRUE means page table splitted. FALSE means page table not splitted.\r
901\r
902 @retval EFI_SUCCESS The attributes were set for the memory region.\r
903 @retval EFI_ACCESS_DENIED The attributes for the memory resource range specified by\r
904 BaseAddress and Length cannot be modified.\r
905 @retval EFI_INVALID_PARAMETER Length is zero.\r
906 Attributes specified an illegal combination of attributes that\r
907 cannot be set together.\r
908 @retval EFI_OUT_OF_RESOURCES There are not enough system resources to modify the attributes of\r
909 the memory resource range.\r
910 @retval EFI_UNSUPPORTED The processor does not support one or more bytes of the memory\r
911 resource range specified by BaseAddress and Length.\r
912 The bit mask of attributes is not support for the memory resource\r
913 range specified by BaseAddress and Length.\r
914\r
915**/\r
916EFI_STATUS\r
917EFIAPI\r
918SmmSetMemoryAttributesEx (\r
919 IN EFI_PHYSICAL_ADDRESS BaseAddress,\r
920 IN UINT64 Length,\r
921 IN UINT64 Attributes,\r
922 OUT BOOLEAN *IsSplitted OPTIONAL\r
923 );\r
924\r
925/**\r
926 This function clears the attributes for the memory region specified by BaseAddress and\r
927 Length from their current attributes to the attributes specified by Attributes.\r
928\r
929 @param[in] BaseAddress The physical address that is the start address of a memory region.\r
930 @param[in] Length The size in bytes of the memory region.\r
931 @param[in] Attributes The bit mask of attributes to clear for the memory region.\r
932 @param[out] IsSplitted TRUE means page table splitted. FALSE means page table not splitted.\r
933\r
934 @retval EFI_SUCCESS The attributes were cleared for the memory region.\r
935 @retval EFI_ACCESS_DENIED The attributes for the memory resource range specified by\r
936 BaseAddress and Length cannot be modified.\r
937 @retval EFI_INVALID_PARAMETER Length is zero.\r
938 Attributes specified an illegal combination of attributes that\r
939 cannot be set together.\r
940 @retval EFI_OUT_OF_RESOURCES There are not enough system resources to modify the attributes of\r
941 the memory resource range.\r
942 @retval EFI_UNSUPPORTED The processor does not support one or more bytes of the memory\r
943 resource range specified by BaseAddress and Length.\r
944 The bit mask of attributes is not support for the memory resource\r
945 range specified by BaseAddress and Length.\r
946\r
947**/\r
948EFI_STATUS\r
949EFIAPI\r
950SmmClearMemoryAttributesEx (\r
951 IN EFI_PHYSICAL_ADDRESS BaseAddress,\r
952 IN UINT64 Length,\r
953 IN UINT64 Attributes,\r
954 OUT BOOLEAN *IsSplitted OPTIONAL\r
955 );\r
956\r
21c17193
JY
957/**\r
958 This API provides a way to allocate memory for page table.\r
959\r
960 This API can be called more once to allocate memory for page tables.\r
961\r
962 Allocates the number of 4KB pages of type EfiRuntimeServicesData and returns a pointer to the\r
963 allocated buffer. The buffer returned is aligned on a 4KB boundary. If Pages is 0, then NULL\r
964 is returned. If there is not enough memory remaining to satisfy the request, then NULL is\r
965 returned.\r
966\r
967 @param Pages The number of 4 KB pages to allocate.\r
968\r
969 @return A pointer to the allocated buffer or NULL if allocation fails.\r
970\r
971**/\r
972VOID *\r
973AllocatePageTableMemory (\r
974 IN UINTN Pages\r
975 );\r
976\r
717fb604
JY
977/**\r
978 Allocate pages for code.\r
979\r
980 @param[in] Pages Number of pages to be allocated.\r
981\r
982 @return Allocated memory.\r
983**/\r
984VOID *\r
985AllocateCodePages (\r
986 IN UINTN Pages\r
987 );\r
988\r
989/**\r
990 Allocate aligned pages for code.\r
991\r
992 @param[in] Pages Number of pages to be allocated.\r
993 @param[in] Alignment The requested alignment of the allocation.\r
994 Must be a power of two.\r
995 If Alignment is zero, then byte alignment is used.\r
996\r
997 @return Allocated memory.\r
998**/\r
999VOID *\r
1000AllocateAlignedCodePages (\r
1001 IN UINTN Pages,\r
1002 IN UINTN Alignment\r
1003 );\r
1004\r
0bdc9e75
SZ
1005\r
1006//\r
1007// S3 related global variable and function prototype.\r
1008//\r
1009\r
1010extern BOOLEAN mSmmS3Flag;\r
1011\r
1012/**\r
1013 Initialize SMM S3 resume state structure used during S3 Resume.\r
1014\r
1015 @param[in] Cr3 The base address of the page tables to use in SMM.\r
1016\r
1017**/\r
1018VOID\r
1019InitSmmS3ResumeState (\r
1020 IN UINT32 Cr3\r
1021 );\r
1022\r
1023/**\r
1024 Get ACPI CPU data.\r
1025\r
1026**/\r
1027VOID\r
1028GetAcpiCpuData (\r
1029 VOID\r
1030 );\r
1031\r
1032/**\r
1033 Restore SMM Configuration in S3 boot path.\r
1034\r
1035**/\r
1036VOID\r
1037RestoreSmmConfigurationInS3 (\r
1038 VOID\r
1039 );\r
1040\r
b10d5ddc
SZ
1041/**\r
1042 Get ACPI S3 enable flag.\r
1043\r
1044**/\r
1045VOID\r
1046GetAcpiS3EnableFlag (\r
1047 VOID\r
1048 );\r
1049\r
4a0f88dd
JF
1050/**\r
1051 Transfer AP to safe hlt-loop after it finished restore CPU features on S3 patch.\r
1052\r
672b80c8
MK
1053 @param[in] ApHltLoopCode The address of the safe hlt-loop function.\r
1054 @param[in] TopOfStack A pointer to the new stack to use for the ApHltLoopCode.\r
1055 @param[in] NumberToFinishAddress Address of Semaphore of APs finish count.\r
4a0f88dd
JF
1056\r
1057**/\r
1058VOID\r
1059TransferApToSafeState (\r
672b80c8
MK
1060 IN UINTN ApHltLoopCode,\r
1061 IN UINTN TopOfStack,\r
1062 IN UINTN NumberToFinishAddress\r
4a0f88dd
JF
1063 );\r
1064\r
827330cc
JW
1065/**\r
1066 This function set given attributes of the memory region specified by\r
1067 BaseAddress and Length.\r
1068\r
1069 @param This The EDKII_SMM_MEMORY_ATTRIBUTE_PROTOCOL instance.\r
1070 @param BaseAddress The physical address that is the start address of\r
1071 a memory region.\r
1072 @param Length The size in bytes of the memory region.\r
1073 @param Attributes The bit mask of attributes to set for the memory\r
1074 region.\r
1075\r
1076 @retval EFI_SUCCESS The attributes were set for the memory region.\r
1077 @retval EFI_INVALID_PARAMETER Length is zero.\r
1078 Attributes specified an illegal combination of\r
1079 attributes that cannot be set together.\r
1080 @retval EFI_UNSUPPORTED The processor does not support one or more\r
1081 bytes of the memory resource range specified\r
1082 by BaseAddress and Length.\r
aae02dcc 1083 The bit mask of attributes is not supported for\r
827330cc
JW
1084 the memory resource range specified by\r
1085 BaseAddress and Length.\r
1086\r
1087**/\r
1088EFI_STATUS\r
1089EFIAPI\r
1090EdkiiSmmSetMemoryAttributes (\r
1091 IN EDKII_SMM_MEMORY_ATTRIBUTE_PROTOCOL *This,\r
1092 IN EFI_PHYSICAL_ADDRESS BaseAddress,\r
1093 IN UINT64 Length,\r
1094 IN UINT64 Attributes\r
1095 );\r
1096\r
1097/**\r
1098 This function clears given attributes of the memory region specified by\r
1099 BaseAddress and Length.\r
1100\r
1101 @param This The EDKII_SMM_MEMORY_ATTRIBUTE_PROTOCOL instance.\r
1102 @param BaseAddress The physical address that is the start address of\r
1103 a memory region.\r
1104 @param Length The size in bytes of the memory region.\r
aae02dcc 1105 @param Attributes The bit mask of attributes to clear for the memory\r
827330cc
JW
1106 region.\r
1107\r
aae02dcc 1108 @retval EFI_SUCCESS The attributes were cleared for the memory region.\r
827330cc
JW
1109 @retval EFI_INVALID_PARAMETER Length is zero.\r
1110 Attributes specified an illegal combination of\r
aae02dcc 1111 attributes that cannot be cleared together.\r
827330cc
JW
1112 @retval EFI_UNSUPPORTED The processor does not support one or more\r
1113 bytes of the memory resource range specified\r
1114 by BaseAddress and Length.\r
aae02dcc 1115 The bit mask of attributes is not supported for\r
827330cc
JW
1116 the memory resource range specified by\r
1117 BaseAddress and Length.\r
1118\r
1119**/\r
1120EFI_STATUS\r
1121EFIAPI\r
1122EdkiiSmmClearMemoryAttributes (\r
1123 IN EDKII_SMM_MEMORY_ATTRIBUTE_PROTOCOL *This,\r
1124 IN EFI_PHYSICAL_ADDRESS BaseAddress,\r
1125 IN UINT64 Length,\r
1126 IN UINT64 Attributes\r
1127 );\r
1128\r
1129/**\r
aae02dcc 1130 This function retrieves the attributes of the memory region specified by\r
827330cc
JW
1131 BaseAddress and Length. If different attributes are got from different part\r
1132 of the memory region, EFI_NO_MAPPING will be returned.\r
1133\r
1134 @param This The EDKII_SMM_MEMORY_ATTRIBUTE_PROTOCOL instance.\r
1135 @param BaseAddress The physical address that is the start address of\r
1136 a memory region.\r
1137 @param Length The size in bytes of the memory region.\r
1138 @param Attributes Pointer to attributes returned.\r
1139\r
1140 @retval EFI_SUCCESS The attributes got for the memory region.\r
1141 @retval EFI_INVALID_PARAMETER Length is zero.\r
1142 Attributes is NULL.\r
1143 @retval EFI_NO_MAPPING Attributes are not consistent cross the memory\r
1144 region.\r
1145 @retval EFI_UNSUPPORTED The processor does not support one or more\r
1146 bytes of the memory resource range specified\r
1147 by BaseAddress and Length.\r
827330cc
JW
1148\r
1149**/\r
1150EFI_STATUS\r
1151EFIAPI\r
1152EdkiiSmmGetMemoryAttributes (\r
1153 IN EDKII_SMM_MEMORY_ATTRIBUTE_PROTOCOL *This,\r
1154 IN EFI_PHYSICAL_ADDRESS BaseAddress,\r
1155 IN UINT64 Length,\r
1156 IN UINT64 *Attributes\r
1157 );\r
1158\r
e21e355e
LG
1159/**\r
1160 This function fixes up the address of the global variable or function\r
1161 referred in SmmInit assembly files to be the absoute address.\r
1162**/\r
1163VOID\r
1164EFIAPI\r
1165PiSmmCpuSmmInitFixupAddress (\r
1166 );\r
1167\r
1168/**\r
1169 This function fixes up the address of the global variable or function\r
1170 referred in SmiEntry assembly files to be the absoute address.\r
1171**/\r
1172VOID\r
1173EFIAPI\r
1174PiSmmCpuSmiEntryFixupAddress (\r
1175 );\r
1176\r
529a5a86 1177#endif\r