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