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