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