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