]> git.proxmox.com Git - mirror_edk2.git/blame - UefiCpuPkg/PiSmmCpuDxeSmm/PiSmmCpuDxeSmm.h
UefiCpuPkg/PiSmmCpuDxeSmm: Add paging protection.
[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
362typedef struct {\r
363 UINT64 Signature; // Offset 0x00\r
364 UINT16 Reserved1; // Offset 0x08\r
365 UINT16 Reserved2; // Offset 0x0A\r
366 UINT16 Reserved3; // Offset 0x0C\r
367 UINT16 SmmCs; // Offset 0x0E\r
368 UINT16 SmmDs; // Offset 0x10\r
369 UINT16 SmmSs; // Offset 0x12\r
370 UINT16 SmmOtherSegment; // Offset 0x14\r
371 UINT16 Reserved4; // Offset 0x16\r
372 UINT64 Reserved5; // Offset 0x18\r
373 UINT64 Reserved6; // Offset 0x20\r
374 UINT64 Reserved7; // Offset 0x28\r
375 UINT64 SmmGdtPtr; // Offset 0x30\r
376 UINT32 SmmGdtSize; // Offset 0x38\r
377 UINT32 Reserved8; // Offset 0x3C\r
378 UINT64 Reserved9; // Offset 0x40\r
379 UINT64 Reserved10; // Offset 0x48\r
380 UINT16 Reserved11; // Offset 0x50\r
381 UINT16 Reserved12; // Offset 0x52\r
382 UINT32 Reserved13; // Offset 0x54\r
383 UINT64 MtrrBaseMaskPtr; // Offset 0x58\r
384} PROCESSOR_SMM_DESCRIPTOR;\r
385\r
1d648531
JF
386\r
387///\r
388/// All global semaphores' pointer\r
389///\r
390typedef struct {\r
391 volatile UINT32 *Counter;\r
392 volatile BOOLEAN *InsideSmm;\r
393 volatile BOOLEAN *AllCpusInSync;\r
394 SPIN_LOCK *PFLock;\r
395 SPIN_LOCK *CodeAccessCheckLock;\r
6c4c15fa 396 SPIN_LOCK *MemoryMappedLock;\r
1d648531
JF
397} SMM_CPU_SEMAPHORE_GLOBAL;\r
398\r
4e920581
JF
399///\r
400/// All semaphores for each processor\r
401///\r
402typedef struct {\r
403 SPIN_LOCK *Busy;\r
404 volatile UINT32 *Run;\r
405 volatile BOOLEAN *Present;\r
406} SMM_CPU_SEMAPHORE_CPU;\r
407\r
695e62d1
JF
408///\r
409/// All MSRs semaphores' pointer and counter\r
410///\r
411typedef struct {\r
412 SPIN_LOCK *Msr;\r
413 UINTN AvailableCounter;\r
414} SMM_CPU_SEMAPHORE_MSR;\r
4e920581 415\r
1d648531
JF
416///\r
417/// All semaphores' information\r
418///\r
419typedef struct {\r
420 SMM_CPU_SEMAPHORE_GLOBAL SemaphoreGlobal;\r
4e920581 421 SMM_CPU_SEMAPHORE_CPU SemaphoreCpu;\r
695e62d1 422 SMM_CPU_SEMAPHORE_MSR SemaphoreMsr;\r
1d648531
JF
423} SMM_CPU_SEMAPHORES;\r
424\r
529a5a86 425extern IA32_DESCRIPTOR gcSmiGdtr;\r
717fb604
JY
426extern EFI_PHYSICAL_ADDRESS mGdtBuffer;\r
427extern UINTN mGdtBufferSize;\r
529a5a86
MK
428extern IA32_DESCRIPTOR gcSmiIdtr;\r
429extern VOID *gcSmiIdtrPtr;\r
430extern CONST PROCESSOR_SMM_DESCRIPTOR gcPsd;\r
431extern UINT64 gPhyMask;\r
529a5a86 432extern SMM_DISPATCHER_MP_SYNC_DATA *mSmmMpSyncData;\r
529a5a86
MK
433extern UINTN mSmmStackArrayBase;\r
434extern UINTN mSmmStackArrayEnd;\r
435extern UINTN mSmmStackSize;\r
436extern EFI_SMM_CPU_SERVICE_PROTOCOL mSmmCpuService;\r
437extern IA32_DESCRIPTOR gcSmiInitGdtr;\r
dc99315b
JF
438extern SMM_CPU_SEMAPHORES mSmmCpuSemaphores;\r
439extern UINTN mSemaphoreSize;\r
fe3a75bc
JF
440extern SPIN_LOCK *mPFLock;\r
441extern SPIN_LOCK *mConfigSmmCodeAccessCheckLock;\r
6c4c15fa 442extern SPIN_LOCK *mMemoryMappedLock;\r
529a5a86
MK
443\r
444/**\r
445 Create 4G PageTable in SMRAM.\r
446\r
717fb604 447 @param[in] Is32BitPageTable Whether the page table is 32-bit PAE\r
529a5a86
MK
448 @return PageTable Address\r
449\r
450**/\r
451UINT32\r
452Gen4GPageTable (\r
881520ea 453 IN BOOLEAN Is32BitPageTable\r
529a5a86
MK
454 );\r
455\r
456\r
457/**\r
458 Initialize global data for MP synchronization.\r
459\r
460 @param Stacks Base address of SMI stack buffer for all processors.\r
461 @param StackSize Stack size for each processor in SMM.\r
462\r
463**/\r
464UINT32\r
465InitializeMpServiceData (\r
466 IN VOID *Stacks,\r
467 IN UINTN StackSize\r
468 );\r
469\r
470/**\r
471 Initialize Timer for SMM AP Sync.\r
472\r
473**/\r
474VOID\r
475InitializeSmmTimer (\r
476 VOID\r
477 );\r
478\r
479/**\r
480 Start Timer for SMM AP Sync.\r
481\r
482**/\r
483UINT64\r
484EFIAPI\r
485StartSyncTimer (\r
486 VOID\r
487 );\r
488\r
489/**\r
490 Check if the SMM AP Sync timer is timeout.\r
491\r
492 @param Timer The start timer from the begin.\r
493\r
494**/\r
495BOOLEAN\r
496EFIAPI\r
497IsSyncTimerTimeout (\r
498 IN UINT64 Timer\r
499 );\r
500\r
501/**\r
502 Initialize IDT for SMM Stack Guard.\r
503\r
504**/\r
505VOID\r
506EFIAPI\r
507InitializeIDTSmmStackGuard (\r
508 VOID\r
509 );\r
510\r
fe5f1949
JY
511/**\r
512 Initialize Gdt for all processors.\r
717fb604 513\r
fe5f1949
JY
514 @param[in] Cr3 CR3 value.\r
515 @param[out] GdtStepSize The step size for GDT table.\r
516\r
517 @return GdtBase for processor 0.\r
518 GdtBase for processor X is: GdtBase + (GdtStepSize * X)\r
519**/\r
520VOID *\r
521InitGdt (\r
522 IN UINTN Cr3,\r
523 OUT UINTN *GdtStepSize\r
524 );\r
525\r
529a5a86
MK
526/**\r
527\r
528 Register the SMM Foundation entry point.\r
529\r
530 @param This Pointer to EFI_SMM_CONFIGURATION_PROTOCOL instance\r
531 @param SmmEntryPoint SMM Foundation EntryPoint\r
532\r
533 @retval EFI_SUCCESS Successfully to register SMM foundation entry point\r
534\r
535**/\r
536EFI_STATUS\r
537EFIAPI\r
538RegisterSmmEntry (\r
539 IN CONST EFI_SMM_CONFIGURATION_PROTOCOL *This,\r
540 IN EFI_SMM_ENTRY_POINT SmmEntryPoint\r
541 );\r
542\r
543/**\r
544 Create PageTable for SMM use.\r
545\r
546 @return PageTable Address\r
547\r
548**/\r
549UINT32\r
550SmmInitPageTable (\r
551 VOID\r
552 );\r
553\r
554/**\r
555 Schedule a procedure to run on the specified CPU.\r
556\r
557 @param Procedure The address of the procedure to run\r
558 @param CpuIndex Target CPU number\r
559 @param ProcArguments The parameter to pass to the procedure\r
560\r
561 @retval EFI_INVALID_PARAMETER CpuNumber not valid\r
562 @retval EFI_INVALID_PARAMETER CpuNumber specifying BSP\r
563 @retval EFI_INVALID_PARAMETER The AP specified by CpuNumber did not enter SMM\r
564 @retval EFI_INVALID_PARAMETER The AP specified by CpuNumber is busy\r
565 @retval EFI_SUCCESS - The procedure has been successfully scheduled\r
566\r
567**/\r
568EFI_STATUS\r
569EFIAPI\r
570SmmStartupThisAp (\r
571 IN EFI_AP_PROCEDURE Procedure,\r
572 IN UINTN CpuIndex,\r
573 IN OUT VOID *ProcArguments OPTIONAL\r
574 );\r
575\r
576/**\r
577 Schedule a procedure to run on the specified CPU in a blocking fashion.\r
578\r
579 @param Procedure The address of the procedure to run\r
580 @param CpuIndex Target CPU Index\r
581 @param ProcArguments The parameter to pass to the procedure\r
582\r
583 @retval EFI_INVALID_PARAMETER CpuNumber not valid\r
584 @retval EFI_INVALID_PARAMETER CpuNumber specifying BSP\r
585 @retval EFI_INVALID_PARAMETER The AP specified by CpuNumber did not enter SMM\r
586 @retval EFI_INVALID_PARAMETER The AP specified by CpuNumber is busy\r
587 @retval EFI_SUCCESS The procedure has been successfully scheduled\r
588\r
589**/\r
590EFI_STATUS\r
591EFIAPI\r
592SmmBlockingStartupThisAp (\r
593 IN EFI_AP_PROCEDURE Procedure,\r
594 IN UINTN CpuIndex,\r
595 IN OUT VOID *ProcArguments OPTIONAL\r
596 );\r
597\r
598/**\r
599 Initialize MP synchronization data.\r
600\r
601**/\r
602VOID\r
603EFIAPI\r
604InitializeMpSyncData (\r
605 VOID\r
606 );\r
607\r
608/**\r
609\r
610 Find out SMRAM information including SMRR base and SMRR size.\r
611\r
612 @param SmrrBase SMRR base\r
613 @param SmrrSize SMRR size\r
614\r
615**/\r
616VOID\r
617FindSmramInfo (\r
618 OUT UINT32 *SmrrBase,\r
619 OUT UINT32 *SmrrSize\r
620 );\r
621\r
622/**\r
0bdc9e75 623 Relocate SmmBases for each processor.\r
529a5a86 624\r
0bdc9e75 625 Execute on first boot and all S3 resumes\r
529a5a86
MK
626\r
627**/\r
628VOID\r
0bdc9e75
SZ
629EFIAPI\r
630SmmRelocateBases (\r
529a5a86
MK
631 VOID\r
632 );\r
633\r
634/**\r
635 Page Fault handler for SMM use.\r
636\r
637 @param InterruptType Defines the type of interrupt or exception that\r
638 occurred on the processor.This parameter is processor architecture specific.\r
639 @param SystemContext A pointer to the processor context when\r
640 the interrupt occurred on the processor.\r
641**/\r
642VOID\r
643EFIAPI\r
644SmiPFHandler (\r
645 IN EFI_EXCEPTION_TYPE InterruptType,\r
646 IN EFI_SYSTEM_CONTEXT SystemContext\r
647 );\r
648\r
649/**\r
650 Perform the remaining tasks.\r
651\r
652**/\r
653VOID\r
654PerformRemainingTasks (\r
655 VOID\r
656 );\r
657\r
9f419739
JY
658/**\r
659 Perform the pre tasks.\r
660\r
661**/\r
662VOID\r
663PerformPreTasks (\r
664 VOID\r
665 );\r
666\r
529a5a86
MK
667/**\r
668 Initialize MSR spin lock by MSR index.\r
669\r
670 @param MsrIndex MSR index value.\r
671\r
672**/\r
673VOID\r
674InitMsrSpinLockByIndex (\r
675 IN UINT32 MsrIndex\r
676 );\r
677\r
678/**\r
679 Hook return address of SMM Save State so that semaphore code\r
680 can be executed immediately after AP exits SMM to indicate to\r
681 the BSP that an AP has exited SMM after SMBASE relocation.\r
682\r
683 @param[in] CpuIndex The processor index.\r
684 @param[in] RebasedFlag A pointer to a flag that is set to TRUE\r
685 immediately after AP exits SMM.\r
686\r
687**/\r
688VOID\r
689SemaphoreHook (\r
690 IN UINTN CpuIndex,\r
691 IN volatile BOOLEAN *RebasedFlag\r
692 );\r
693\r
694/**\r
695Configure SMM Code Access Check feature for all processors.\r
696SMM Feature Control MSR will be locked after configuration.\r
697**/\r
698VOID\r
699ConfigSmmCodeAccessCheck (\r
700 VOID\r
701 );\r
702\r
703/**\r
704 Hook the code executed immediately after an RSM instruction on the currently\r
705 executing CPU. The mode of code executed immediately after RSM must be\r
706 detected, and the appropriate hook must be selected. Always clear the auto\r
707 HALT restart flag if it is set.\r
708\r
709 @param[in] CpuIndex The processor index for the currently\r
710 executing CPU.\r
711 @param[in] CpuState Pointer to SMRAM Save State Map for the\r
712 currently executing CPU.\r
713 @param[in] NewInstructionPointer32 Instruction pointer to use if resuming to\r
714 32-bit mode from 64-bit SMM.\r
715 @param[in] NewInstructionPointer Instruction pointer to use if resuming to\r
716 same mode as SMM.\r
717\r
718 @retval The value of the original instruction pointer before it was hooked.\r
719\r
720**/\r
721UINT64\r
722EFIAPI\r
723HookReturnFromSmm (\r
724 IN UINTN CpuIndex,\r
725 SMRAM_SAVE_STATE_MAP *CpuState,\r
726 UINT64 NewInstructionPointer32,\r
727 UINT64 NewInstructionPointer\r
728 );\r
729\r
730/**\r
731 Get the size of the SMI Handler in bytes.\r
732\r
733 @retval The size, in bytes, of the SMI Handler.\r
734\r
735**/\r
736UINTN\r
737EFIAPI\r
738GetSmiHandlerSize (\r
739 VOID\r
740 );\r
741\r
742/**\r
743 Install the SMI handler for the CPU specified by CpuIndex. This function\r
744 is called by the CPU that was elected as monarch during System Management\r
745 Mode initialization.\r
746\r
747 @param[in] CpuIndex The index of the CPU to install the custom SMI handler.\r
748 The value must be between 0 and the NumberOfCpus field\r
749 in the System Management System Table (SMST).\r
750 @param[in] SmBase The SMBASE address for the CPU specified by CpuIndex.\r
751 @param[in] SmiStack The stack to use when an SMI is processed by the\r
752 the CPU specified by CpuIndex.\r
753 @param[in] StackSize The size, in bytes, if the stack used when an SMI is\r
754 processed by the CPU specified by CpuIndex.\r
755 @param[in] GdtBase The base address of the GDT to use when an SMI is\r
756 processed by the CPU specified by CpuIndex.\r
757 @param[in] GdtSize The size, in bytes, of the GDT used when an SMI is\r
758 processed by the CPU specified by CpuIndex.\r
759 @param[in] IdtBase The base address of the IDT to use when an SMI is\r
760 processed by the CPU specified by CpuIndex.\r
761 @param[in] IdtSize The size, in bytes, of the IDT used when an SMI is\r
762 processed by the CPU specified by CpuIndex.\r
763 @param[in] Cr3 The base address of the page tables to use when an SMI\r
764 is processed by the CPU specified by CpuIndex.\r
765**/\r
766VOID\r
767EFIAPI\r
768InstallSmiHandler (\r
769 IN UINTN CpuIndex,\r
770 IN UINT32 SmBase,\r
771 IN VOID *SmiStack,\r
772 IN UINTN StackSize,\r
773 IN UINTN GdtBase,\r
774 IN UINTN GdtSize,\r
775 IN UINTN IdtBase,\r
776 IN UINTN IdtSize,\r
777 IN UINT32 Cr3\r
778 );\r
779\r
780/**\r
781 Search module name by input IP address and output it.\r
782\r
783 @param CallerIpAddress Caller instruction pointer.\r
784\r
785**/\r
786VOID\r
787DumpModuleInfoByIp (\r
788 IN UINTN CallerIpAddress\r
789 );\r
21c17193 790\r
717fb604
JY
791/**\r
792 This function sets memory attribute according to MemoryAttributesTable.\r
793**/\r
794VOID\r
795SetMemMapAttributes (\r
796 VOID\r
797 );\r
798\r
799/**\r
800 This function sets memory attribute for page table.\r
801**/\r
802VOID\r
803SetPageTableAttributes (\r
804 VOID\r
805 );\r
806\r
807/**\r
808 Return page table base.\r
809\r
810 @return page table base.\r
811**/\r
812UINTN\r
813GetPageTableBase (\r
814 VOID\r
815 );\r
816\r
817/**\r
818 This function sets the attributes for the memory region specified by BaseAddress and\r
819 Length from their current attributes to the attributes specified by Attributes.\r
820\r
821 @param[in] BaseAddress The physical address that is the start address of a memory region.\r
822 @param[in] Length The size in bytes of the memory region.\r
823 @param[in] Attributes The bit mask of attributes to set for the memory region.\r
824 @param[out] IsSplitted TRUE means page table splitted. FALSE means page table not splitted.\r
825\r
826 @retval EFI_SUCCESS The attributes were set for the memory region.\r
827 @retval EFI_ACCESS_DENIED The attributes for the memory resource range specified by\r
828 BaseAddress and Length cannot be modified.\r
829 @retval EFI_INVALID_PARAMETER Length is zero.\r
830 Attributes specified an illegal combination of attributes that\r
831 cannot be set together.\r
832 @retval EFI_OUT_OF_RESOURCES There are not enough system resources to modify the attributes of\r
833 the memory resource range.\r
834 @retval EFI_UNSUPPORTED The processor does not support one or more bytes of the memory\r
835 resource range specified by BaseAddress and Length.\r
836 The bit mask of attributes is not support for the memory resource\r
837 range specified by BaseAddress and Length.\r
838\r
839**/\r
840EFI_STATUS\r
841EFIAPI\r
842SmmSetMemoryAttributesEx (\r
843 IN EFI_PHYSICAL_ADDRESS BaseAddress,\r
844 IN UINT64 Length,\r
845 IN UINT64 Attributes,\r
846 OUT BOOLEAN *IsSplitted OPTIONAL\r
847 );\r
848\r
849/**\r
850 This function clears the attributes for the memory region specified by BaseAddress and\r
851 Length from their current attributes to the attributes specified by Attributes.\r
852\r
853 @param[in] BaseAddress The physical address that is the start address of a memory region.\r
854 @param[in] Length The size in bytes of the memory region.\r
855 @param[in] Attributes The bit mask of attributes to clear for the memory region.\r
856 @param[out] IsSplitted TRUE means page table splitted. FALSE means page table not splitted.\r
857\r
858 @retval EFI_SUCCESS The attributes were cleared for the memory region.\r
859 @retval EFI_ACCESS_DENIED The attributes for the memory resource range specified by\r
860 BaseAddress and Length cannot be modified.\r
861 @retval EFI_INVALID_PARAMETER Length is zero.\r
862 Attributes specified an illegal combination of attributes that\r
863 cannot be set together.\r
864 @retval EFI_OUT_OF_RESOURCES There are not enough system resources to modify the attributes of\r
865 the memory resource range.\r
866 @retval EFI_UNSUPPORTED The processor does not support one or more bytes of the memory\r
867 resource range specified by BaseAddress and Length.\r
868 The bit mask of attributes is not support for the memory resource\r
869 range specified by BaseAddress and Length.\r
870\r
871**/\r
872EFI_STATUS\r
873EFIAPI\r
874SmmClearMemoryAttributesEx (\r
875 IN EFI_PHYSICAL_ADDRESS BaseAddress,\r
876 IN UINT64 Length,\r
877 IN UINT64 Attributes,\r
878 OUT BOOLEAN *IsSplitted OPTIONAL\r
879 );\r
880\r
21c17193
JY
881/**\r
882 This API provides a way to allocate memory for page table.\r
883\r
884 This API can be called more once to allocate memory for page tables.\r
885\r
886 Allocates the number of 4KB pages of type EfiRuntimeServicesData and returns a pointer to the\r
887 allocated buffer. The buffer returned is aligned on a 4KB boundary. If Pages is 0, then NULL\r
888 is returned. If there is not enough memory remaining to satisfy the request, then NULL is\r
889 returned.\r
890\r
891 @param Pages The number of 4 KB pages to allocate.\r
892\r
893 @return A pointer to the allocated buffer or NULL if allocation fails.\r
894\r
895**/\r
896VOID *\r
897AllocatePageTableMemory (\r
898 IN UINTN Pages\r
899 );\r
900\r
717fb604
JY
901/**\r
902 Allocate pages for code.\r
903\r
904 @param[in] Pages Number of pages to be allocated.\r
905\r
906 @return Allocated memory.\r
907**/\r
908VOID *\r
909AllocateCodePages (\r
910 IN UINTN Pages\r
911 );\r
912\r
913/**\r
914 Allocate aligned pages for code.\r
915\r
916 @param[in] Pages Number of pages to be allocated.\r
917 @param[in] Alignment The requested alignment of the allocation.\r
918 Must be a power of two.\r
919 If Alignment is zero, then byte alignment is used.\r
920\r
921 @return Allocated memory.\r
922**/\r
923VOID *\r
924AllocateAlignedCodePages (\r
925 IN UINTN Pages,\r
926 IN UINTN Alignment\r
927 );\r
928\r
0bdc9e75
SZ
929\r
930//\r
931// S3 related global variable and function prototype.\r
932//\r
933\r
934extern BOOLEAN mSmmS3Flag;\r
935\r
936/**\r
937 Initialize SMM S3 resume state structure used during S3 Resume.\r
938\r
939 @param[in] Cr3 The base address of the page tables to use in SMM.\r
940\r
941**/\r
942VOID\r
943InitSmmS3ResumeState (\r
944 IN UINT32 Cr3\r
945 );\r
946\r
947/**\r
948 Get ACPI CPU data.\r
949\r
950**/\r
951VOID\r
952GetAcpiCpuData (\r
953 VOID\r
954 );\r
955\r
956/**\r
957 Restore SMM Configuration in S3 boot path.\r
958\r
959**/\r
960VOID\r
961RestoreSmmConfigurationInS3 (\r
962 VOID\r
963 );\r
964\r
b10d5ddc
SZ
965/**\r
966 Get ACPI S3 enable flag.\r
967\r
968**/\r
969VOID\r
970GetAcpiS3EnableFlag (\r
971 VOID\r
972 );\r
973\r
4a0f88dd
JF
974/**\r
975 Transfer AP to safe hlt-loop after it finished restore CPU features on S3 patch.\r
976\r
977 @param[in] ApHltLoopCode The 32-bit address of the safe hlt-loop function.\r
978 @param[in] TopOfStack A pointer to the new stack to use for the ApHltLoopCode.\r
ec8a3877 979 @param[in] NumberToFinish Semaphore of APs finish count.\r
4a0f88dd
JF
980\r
981**/\r
982VOID\r
983TransferApToSafeState (\r
984 IN UINT32 ApHltLoopCode,\r
ec8a3877
JF
985 IN UINT32 TopOfStack,\r
986 IN UINT32 *NumberToFinish\r
4a0f88dd
JF
987 );\r
988\r
529a5a86 989#endif\r