2 Agent Module to load other modules to deploy SMM Entry Vector for X86 CPU.
4 Copyright (c) 2009 - 2019, Intel Corporation. All rights reserved.<BR>
5 Copyright (c) 2017, AMD Incorporated. All rights reserved.<BR>
7 SPDX-License-Identifier: BSD-2-Clause-Patent
11 #ifndef _CPU_PISMMCPUDXESMM_H_
12 #define _CPU_PISMMCPUDXESMM_H_
16 #include <Protocol/MpService.h>
17 #include <Protocol/SmmConfiguration.h>
18 #include <Protocol/SmmCpu.h>
19 #include <Protocol/SmmAccess2.h>
20 #include <Protocol/SmmReadyToLock.h>
21 #include <Protocol/SmmCpuService.h>
22 #include <Protocol/SmmMemoryAttribute.h>
23 #include <Protocol/MmMp.h>
25 #include <Guid/AcpiS3Context.h>
26 #include <Guid/MemoryAttributesTable.h>
27 #include <Guid/PiSmmMemoryAttributesTable.h>
29 #include <Library/BaseLib.h>
30 #include <Library/IoLib.h>
31 #include <Library/TimerLib.h>
32 #include <Library/SynchronizationLib.h>
33 #include <Library/DebugLib.h>
34 #include <Library/BaseMemoryLib.h>
35 #include <Library/PcdLib.h>
36 #include <Library/MtrrLib.h>
37 #include <Library/SmmCpuPlatformHookLib.h>
38 #include <Library/SmmServicesTableLib.h>
39 #include <Library/MemoryAllocationLib.h>
40 #include <Library/UefiBootServicesTableLib.h>
41 #include <Library/UefiRuntimeServicesTableLib.h>
42 #include <Library/DebugAgentLib.h>
43 #include <Library/UefiLib.h>
44 #include <Library/HobLib.h>
45 #include <Library/LocalApicLib.h>
46 #include <Library/UefiCpuLib.h>
47 #include <Library/CpuExceptionHandlerLib.h>
48 #include <Library/ReportStatusCodeLib.h>
49 #include <Library/SmmCpuFeaturesLib.h>
50 #include <Library/PeCoffGetEntryPointLib.h>
51 #include <Library/RegisterCpuFeaturesLib.h>
53 #include <AcpiCpuData.h>
54 #include <CpuHotPlugData.h>
56 #include <Register/Intel/Cpuid.h>
57 #include <Register/Intel/Msr.h>
59 #include "CpuService.h"
60 #include "SmmProfile.h"
65 #define CPUID_CET_SS BIT7
66 #define CPUID_CET_IBT BIT20
68 #define CR4_CET_ENABLE BIT23
70 #define MSR_IA32_S_CET 0x6A2
71 #define MSR_IA32_PL0_SSP 0x6A4
72 #define MSR_IA32_INTERRUPT_SSP_TABLE_ADDR 0x6A8
76 // enable shadow stacks
78 // enable the WRSS{D,Q}W instructions.
80 // enable tracking of indirect call/jmp targets to be ENDBRANCH instruction.
82 // enable legacy compatibility treatment for indirect call/jmp tracking.
84 // enable use of no-track prefix on indirect call/jmp.
86 // disable suppression of CET indirect branch tracking on legacy compatibility.
87 UINT32 SUPPRESS_DIS
:1;
89 // indirect branch tracking is suppressed.
90 // This bit can be written to 1 only if TRACKER is written as IDLE.
92 // Value of the endbranch state machine
93 // Values: IDLE (0), WAIT_FOR_ENDBRANCH(1).
95 // linear address of a bitmap in memory indicating valid
96 // pages as target of CALL/JMP_indirect that do not land on ENDBRANCH when CET is enabled
97 // and not suppressed. Valid when ENDBR_EN is 1. Must be machine canonical when written on
98 // parts that support 64 bit mode. On parts that do not support 64 bit mode, the bits 63:32 are
99 // reserved and must be 0. This value is extended by 12 bits at the low end to form the base address
100 // (this automatically aligns the address on a 4-Kbyte boundary).
101 UINT32 EB_LEG_BITMAP_BASE_low
:12;
102 UINT32 EB_LEG_BITMAP_BASE_high
:32;
108 // MSRs required for configuration of SMM Code Access Check
110 #define EFI_MSR_SMM_MCA_CAP 0x17D
111 #define SMM_CODE_ACCESS_CHK_BIT BIT58
113 #define SMM_FEATURE_CONTROL_LOCK_BIT BIT0
114 #define SMM_CODE_CHK_EN_BIT BIT2
119 #define IA32_PG_P BIT0
120 #define IA32_PG_RW BIT1
121 #define IA32_PG_U BIT2
122 #define IA32_PG_WT BIT3
123 #define IA32_PG_CD BIT4
124 #define IA32_PG_A BIT5
125 #define IA32_PG_D BIT6
126 #define IA32_PG_PS BIT7
127 #define IA32_PG_PAT_2M BIT12
128 #define IA32_PG_PAT_4K IA32_PG_PS
129 #define IA32_PG_PMNT BIT62
130 #define IA32_PG_NX BIT63
132 #define PAGE_ATTRIBUTE_BITS (IA32_PG_D | IA32_PG_A | IA32_PG_U | IA32_PG_RW | IA32_PG_P)
134 // Bits 1, 2, 5, 6 are reserved in the IA32 PAE PDPTE
135 // X64 PAE PDPTE does not have such restriction
137 #define IA32_PAE_PDPTE_ATTRIBUTE_BITS (IA32_PG_P)
139 #define PAGE_PROGATE_BITS (IA32_PG_NX | PAGE_ATTRIBUTE_BITS)
141 #define PAGING_4K_MASK 0xFFF
142 #define PAGING_2M_MASK 0x1FFFFF
143 #define PAGING_1G_MASK 0x3FFFFFFF
145 #define PAGING_PAE_INDEX_MASK 0x1FF
147 #define PAGING_4K_ADDRESS_MASK_64 0x000FFFFFFFFFF000ull
148 #define PAGING_2M_ADDRESS_MASK_64 0x000FFFFFFFE00000ull
149 #define PAGING_1G_ADDRESS_MASK_64 0x000FFFFFC0000000ull
151 #define SMRR_MAX_ADDRESS BASE_4GB
161 PAGE_ATTRIBUTE Attribute
;
164 } PAGE_ATTRIBUTE_TABLE
;
167 // Size of Task-State Segment defined in IA32 Manual
170 #define EXCEPTION_TSS_SIZE (TSS_SIZE + 4) // Add 4 bytes SSP
171 #define TSS_X64_IST1_OFFSET 36
172 #define TSS_IA32_CR3_OFFSET 28
173 #define TSS_IA32_ESP_OFFSET 56
174 #define TSS_IA32_SSP_OFFSET 104
181 #define PROTECT_MODE_CODE_SEGMENT 0x08
182 #define LONG_MODE_CODE_SEGMENT 0x38
185 // The size 0x20 must be bigger than
186 // the size of template code of SmmInit. Currently,
187 // the size of SmmInit requires the 0x16 Bytes buffer
190 #define BACK_BUF_SIZE 0x20
192 #define EXCEPTION_VECTOR_NUMBER 0x20
194 #define INVALID_APIC_ID 0xFFFFFFFFFFFFFFFFULL
196 typedef UINT32 SMM_CPU_ARRIVAL_EXCEPTIONS
;
197 #define ARRIVAL_EXCEPTION_BLOCKED 0x1
198 #define ARRIVAL_EXCEPTION_DELAYED 0x2
199 #define ARRIVAL_EXCEPTION_SMI_DISABLED 0x4
202 // Wrapper used to convert EFI_AP_PROCEDURE2 and EFI_AP_PROCEDURE.
205 EFI_AP_PROCEDURE Procedure
;
206 VOID
*ProcedureArgument
;
209 #define PROCEDURE_TOKEN_SIGNATURE SIGNATURE_32 ('P', 'R', 'T', 'S')
216 volatile UINT32 RunningApCount
;
219 #define PROCEDURE_TOKEN_FROM_LINK(a) CR (a, PROCEDURE_TOKEN, Link, PROCEDURE_TOKEN_SIGNATURE)
221 #define TOKEN_BUFFER_SIGNATURE SIGNATURE_32 ('T', 'K', 'B', 'S')
230 #define TOKEN_BUFFER_FROM_LINK(a) CR (a, TOKEN_BUFFER, Link, TOKEN_BUFFER_SIGNATURE)
233 // Private structure for the SMM CPU module that is stored in DXE Runtime memory
234 // Contains the SMM Configuration Protocols that is produced.
235 // Contains a mix of DXE and SMM contents. All the fields must be used properly.
237 #define SMM_CPU_PRIVATE_DATA_SIGNATURE SIGNATURE_32 ('s', 'c', 'p', 'u')
242 EFI_HANDLE SmmCpuHandle
;
244 EFI_PROCESSOR_INFORMATION
*ProcessorInfo
;
245 SMM_CPU_OPERATION
*Operation
;
246 UINTN
*CpuSaveStateSize
;
249 EFI_SMM_RESERVED_SMRAM_REGION SmmReservedSmramRegion
[1];
250 EFI_SMM_ENTRY_CONTEXT SmmCoreEntryContext
;
251 EFI_SMM_ENTRY_POINT SmmCoreEntry
;
253 EFI_SMM_CONFIGURATION_PROTOCOL SmmConfiguration
;
255 PROCEDURE_WRAPPER
*ApWrapperFunc
;
256 LIST_ENTRY TokenList
;
258 LIST_ENTRY OldTokenBufList
;
260 UINT8
*CurrentTokenBuf
;
261 UINT32 UsedTokenNum
; // Only record tokens used in CurrentTokenBuf.
262 } SMM_CPU_PRIVATE_DATA
;
264 extern SMM_CPU_PRIVATE_DATA
*gSmmCpuPrivate
;
265 extern CPU_HOT_PLUG_DATA mCpuHotPlugData
;
266 extern UINTN mMaxNumberOfCpus
;
267 extern UINTN mNumberOfCpus
;
268 extern EFI_SMM_CPU_PROTOCOL mSmmCpu
;
269 extern EFI_MM_MP_PROTOCOL mSmmMp
;
272 /// The mode of the CPU at the time an SMI occurs
274 extern UINT8 mSmmSaveStateRegisterLma
;
277 // SMM CPU Protocol function prototypes.
281 Read information from the CPU save state.
283 @param This EFI_SMM_CPU_PROTOCOL instance
284 @param Width The number of bytes to read from the CPU save state.
285 @param Register Specifies the CPU register to read form the save state.
286 @param CpuIndex Specifies the zero-based index of the CPU save state
287 @param Buffer Upon return, this holds the CPU register value read from the save state.
289 @retval EFI_SUCCESS The register was read from Save State
290 @retval EFI_NOT_FOUND The register is not defined for the Save State of Processor
291 @retval EFI_INVALID_PARAMTER This or Buffer is NULL.
297 IN CONST EFI_SMM_CPU_PROTOCOL
*This
,
299 IN EFI_SMM_SAVE_STATE_REGISTER Register
,
305 Write data to the CPU save state.
307 @param This EFI_SMM_CPU_PROTOCOL instance
308 @param Width The number of bytes to read from the CPU save state.
309 @param Register Specifies the CPU register to write to the save state.
310 @param CpuIndex Specifies the zero-based index of the CPU save state
311 @param Buffer Upon entry, this holds the new CPU register value.
313 @retval EFI_SUCCESS The register was written from Save State
314 @retval EFI_NOT_FOUND The register is not defined for the Save State of Processor
315 @retval EFI_INVALID_PARAMTER ProcessorIndex or Width is not correct
321 IN CONST EFI_SMM_CPU_PROTOCOL
*This
,
323 IN EFI_SMM_SAVE_STATE_REGISTER Register
,
325 IN CONST VOID
*Buffer
329 Read a CPU Save State register on the target processor.
331 This function abstracts the differences that whether the CPU Save State register is in the
332 IA32 CPU Save State Map or X64 CPU Save State Map.
334 This function supports reading a CPU Save State register in SMBase relocation handler.
336 @param[in] CpuIndex Specifies the zero-based index of the CPU save state.
337 @param[in] RegisterIndex Index into mSmmCpuWidthOffset[] look up table.
338 @param[in] Width The number of bytes to read from the CPU save state.
339 @param[out] Buffer Upon return, this holds the CPU register value read from the save state.
341 @retval EFI_SUCCESS The register was read from Save State.
342 @retval EFI_NOT_FOUND The register is not defined for the Save State of Processor.
343 @retval EFI_INVALID_PARAMTER This or Buffer is NULL.
348 ReadSaveStateRegister (
350 IN EFI_SMM_SAVE_STATE_REGISTER Register
,
356 Write value to a CPU Save State register on the target processor.
358 This function abstracts the differences that whether the CPU Save State register is in the
359 IA32 CPU Save State Map or X64 CPU Save State Map.
361 This function supports writing a CPU Save State register in SMBase relocation handler.
363 @param[in] CpuIndex Specifies the zero-based index of the CPU save state.
364 @param[in] RegisterIndex Index into mSmmCpuWidthOffset[] look up table.
365 @param[in] Width The number of bytes to read from the CPU save state.
366 @param[in] Buffer Upon entry, this holds the new CPU register value.
368 @retval EFI_SUCCESS The register was written to Save State.
369 @retval EFI_NOT_FOUND The register is not defined for the Save State of Processor.
370 @retval EFI_INVALID_PARAMTER ProcessorIndex or Width is not correct.
375 WriteSaveStateRegister (
377 IN EFI_SMM_SAVE_STATE_REGISTER Register
,
379 IN CONST VOID
*Buffer
382 extern CONST UINT8 gcSmmInitTemplate
[];
383 extern CONST UINT16 gcSmmInitSize
;
384 X86_ASSEMBLY_PATCH_LABEL gPatchSmmCr0
;
385 extern UINT32 mSmmCr0
;
386 X86_ASSEMBLY_PATCH_LABEL gPatchSmmCr3
;
387 extern UINT32 mSmmCr4
;
388 X86_ASSEMBLY_PATCH_LABEL gPatchSmmCr4
;
389 X86_ASSEMBLY_PATCH_LABEL gPatchSmmInitStack
;
390 X86_ASSEMBLY_PATCH_LABEL mPatchCetSupported
;
391 extern BOOLEAN mCetSupported
;
394 Semaphore operation for all processor relocate SMMBase.
398 SmmRelocationSemaphoreComplete (
403 /// The type of SMM CPU Information
407 volatile EFI_AP_PROCEDURE2 Procedure
;
408 volatile VOID
*Parameter
;
409 volatile UINT32
*Run
;
410 volatile BOOLEAN
*Present
;
411 PROCEDURE_TOKEN
*Token
;
413 } SMM_CPU_DATA_BLOCK
;
416 SmmCpuSyncModeTradition
,
417 SmmCpuSyncModeRelaxedAp
,
423 // Pointer to an array. The array should be located immediately after this structure
424 // so that UC cache-ability can be set together.
426 SMM_CPU_DATA_BLOCK
*CpuData
;
427 volatile UINT32
*Counter
;
428 volatile UINT32 BspIndex
;
429 volatile BOOLEAN
*InsideSmm
;
430 volatile BOOLEAN
*AllCpusInSync
;
431 volatile SMM_CPU_SYNC_MODE EffectiveSyncMode
;
432 volatile BOOLEAN SwitchBsp
;
433 volatile BOOLEAN
*CandidateBsp
;
434 EFI_AP_PROCEDURE StartupProcedure
;
435 VOID
*StartupProcArgs
;
436 } SMM_DISPATCHER_MP_SYNC_DATA
;
438 #define SMM_PSD_OFFSET 0xfb00
441 /// All global semaphores' pointer
444 volatile UINT32
*Counter
;
445 volatile BOOLEAN
*InsideSmm
;
446 volatile BOOLEAN
*AllCpusInSync
;
448 SPIN_LOCK
*CodeAccessCheckLock
;
449 } SMM_CPU_SEMAPHORE_GLOBAL
;
452 /// All semaphores for each processor
456 volatile UINT32
*Run
;
457 volatile BOOLEAN
*Present
;
459 } SMM_CPU_SEMAPHORE_CPU
;
462 /// All semaphores' information
465 SMM_CPU_SEMAPHORE_GLOBAL SemaphoreGlobal
;
466 SMM_CPU_SEMAPHORE_CPU SemaphoreCpu
;
467 } SMM_CPU_SEMAPHORES
;
469 extern IA32_DESCRIPTOR gcSmiGdtr
;
470 extern EFI_PHYSICAL_ADDRESS mGdtBuffer
;
471 extern UINTN mGdtBufferSize
;
472 extern IA32_DESCRIPTOR gcSmiIdtr
;
473 extern VOID
*gcSmiIdtrPtr
;
474 extern UINT64 gPhyMask
;
475 extern SMM_DISPATCHER_MP_SYNC_DATA
*mSmmMpSyncData
;
476 extern UINTN mSmmStackArrayBase
;
477 extern UINTN mSmmStackArrayEnd
;
478 extern UINTN mSmmStackSize
;
479 extern EFI_SMM_CPU_SERVICE_PROTOCOL mSmmCpuService
;
480 extern IA32_DESCRIPTOR gcSmiInitGdtr
;
481 extern SMM_CPU_SEMAPHORES mSmmCpuSemaphores
;
482 extern UINTN mSemaphoreSize
;
483 extern SPIN_LOCK
*mPFLock
;
484 extern SPIN_LOCK
*mConfigSmmCodeAccessCheckLock
;
485 extern EFI_SMRAM_DESCRIPTOR
*mSmmCpuSmramRanges
;
486 extern UINTN mSmmCpuSmramRangeCount
;
487 extern UINT8 mPhysicalAddressBits
;
490 // Copy of the PcdPteMemoryEncryptionAddressOrMask
492 extern UINT64 mAddressEncMask
;
495 Create 4G PageTable in SMRAM.
497 @param[in] Is32BitPageTable Whether the page table is 32-bit PAE
498 @return PageTable Address
503 IN BOOLEAN Is32BitPageTable
508 Initialize global data for MP synchronization.
510 @param Stacks Base address of SMI stack buffer for all processors.
511 @param StackSize Stack size for each processor in SMM.
512 @param ShadowStackSize Shadow Stack size for each processor in SMM.
516 InitializeMpServiceData (
519 IN UINTN ShadowStackSize
523 Initialize Timer for SMM AP Sync.
532 Start Timer for SMM AP Sync.
542 Check if the SMM AP Sync timer is timeout.
544 @param Timer The start timer from the begin.
554 Initialize IDT for SMM Stack Guard.
559 InitializeIDTSmmStackGuard (
564 Initialize Gdt for all processors.
566 @param[in] Cr3 CR3 value.
567 @param[out] GdtStepSize The step size for GDT table.
569 @return GdtBase for processor 0.
570 GdtBase for processor X is: GdtBase + (GdtStepSize * X)
575 OUT UINTN
*GdtStepSize
580 Register the SMM Foundation entry point.
582 @param This Pointer to EFI_SMM_CONFIGURATION_PROTOCOL instance
583 @param SmmEntryPoint SMM Foundation EntryPoint
585 @retval EFI_SUCCESS Successfully to register SMM foundation entry point
591 IN CONST EFI_SMM_CONFIGURATION_PROTOCOL
*This
,
592 IN EFI_SMM_ENTRY_POINT SmmEntryPoint
596 Create PageTable for SMM use.
598 @return PageTable Address
607 Schedule a procedure to run on the specified CPU.
609 @param Procedure The address of the procedure to run
610 @param CpuIndex Target CPU number
611 @param ProcArguments The parameter to pass to the procedure
613 @retval EFI_INVALID_PARAMETER CpuNumber not valid
614 @retval EFI_INVALID_PARAMETER CpuNumber specifying BSP
615 @retval EFI_INVALID_PARAMETER The AP specified by CpuNumber did not enter SMM
616 @retval EFI_INVALID_PARAMETER The AP specified by CpuNumber is busy
617 @retval EFI_SUCCESS - The procedure has been successfully scheduled
623 IN EFI_AP_PROCEDURE Procedure
,
625 IN OUT VOID
*ProcArguments OPTIONAL
629 Schedule a procedure to run on the specified CPU in a blocking fashion.
631 @param Procedure The address of the procedure to run
632 @param CpuIndex Target CPU Index
633 @param ProcArguments The parameter to pass to the procedure
635 @retval EFI_INVALID_PARAMETER CpuNumber not valid
636 @retval EFI_INVALID_PARAMETER CpuNumber specifying BSP
637 @retval EFI_INVALID_PARAMETER The AP specified by CpuNumber did not enter SMM
638 @retval EFI_INVALID_PARAMETER The AP specified by CpuNumber is busy
639 @retval EFI_SUCCESS The procedure has been successfully scheduled
644 SmmBlockingStartupThisAp (
645 IN EFI_AP_PROCEDURE Procedure
,
647 IN OUT VOID
*ProcArguments OPTIONAL
651 This function sets the attributes for the memory region specified by BaseAddress and
652 Length from their current attributes to the attributes specified by Attributes.
654 @param[in] BaseAddress The physical address that is the start address of a memory region.
655 @param[in] Length The size in bytes of the memory region.
656 @param[in] Attributes The bit mask of attributes to set for the memory region.
658 @retval EFI_SUCCESS The attributes were set for the memory region.
659 @retval EFI_ACCESS_DENIED The attributes for the memory resource range specified by
660 BaseAddress and Length cannot be modified.
661 @retval EFI_INVALID_PARAMETER Length is zero.
662 Attributes specified an illegal combination of attributes that
663 cannot be set together.
664 @retval EFI_OUT_OF_RESOURCES There are not enough system resources to modify the attributes of
665 the memory resource range.
666 @retval EFI_UNSUPPORTED The processor does not support one or more bytes of the memory
667 resource range specified by BaseAddress and Length.
668 The bit mask of attributes is not support for the memory resource
669 range specified by BaseAddress and Length.
674 SmmSetMemoryAttributes (
675 IN EFI_PHYSICAL_ADDRESS BaseAddress
,
681 This function clears the attributes for the memory region specified by BaseAddress and
682 Length from their current attributes to the attributes specified by Attributes.
684 @param[in] BaseAddress The physical address that is the start address of a memory region.
685 @param[in] Length The size in bytes of the memory region.
686 @param[in] Attributes The bit mask of attributes to clear for the memory region.
688 @retval EFI_SUCCESS The attributes were cleared for the memory region.
689 @retval EFI_ACCESS_DENIED The attributes for the memory resource range specified by
690 BaseAddress and Length cannot be modified.
691 @retval EFI_INVALID_PARAMETER Length is zero.
692 Attributes specified an illegal combination of attributes that
693 cannot be set together.
694 @retval EFI_OUT_OF_RESOURCES There are not enough system resources to modify the attributes of
695 the memory resource range.
696 @retval EFI_UNSUPPORTED The processor does not support one or more bytes of the memory
697 resource range specified by BaseAddress and Length.
698 The bit mask of attributes is not support for the memory resource
699 range specified by BaseAddress and Length.
704 SmmClearMemoryAttributes (
705 IN EFI_PHYSICAL_ADDRESS BaseAddress
,
711 Initialize MP synchronization data.
716 InitializeMpSyncData (
722 Find out SMRAM information including SMRR base and SMRR size.
724 @param SmrrBase SMRR base
725 @param SmrrSize SMRR size
730 OUT UINT32
*SmrrBase
,
735 Relocate SmmBases for each processor.
737 Execute on first boot and all S3 resumes
747 Page Fault handler for SMM use.
749 @param InterruptType Defines the type of interrupt or exception that
750 occurred on the processor.This parameter is processor architecture specific.
751 @param SystemContext A pointer to the processor context when
752 the interrupt occurred on the processor.
757 IN EFI_EXCEPTION_TYPE InterruptType
,
758 IN EFI_SYSTEM_CONTEXT SystemContext
762 Perform the remaining tasks.
766 PerformRemainingTasks (
771 Perform the pre tasks.
780 Initialize MSR spin lock by MSR index.
782 @param MsrIndex MSR index value.
786 InitMsrSpinLockByIndex (
791 Hook return address of SMM Save State so that semaphore code
792 can be executed immediately after AP exits SMM to indicate to
793 the BSP that an AP has exited SMM after SMBASE relocation.
795 @param[in] CpuIndex The processor index.
796 @param[in] RebasedFlag A pointer to a flag that is set to TRUE
797 immediately after AP exits SMM.
803 IN
volatile BOOLEAN
*RebasedFlag
807 Configure SMM Code Access Check feature for all processors.
808 SMM Feature Control MSR will be locked after configuration.
811 ConfigSmmCodeAccessCheck (
816 Hook the code executed immediately after an RSM instruction on the currently
817 executing CPU. The mode of code executed immediately after RSM must be
818 detected, and the appropriate hook must be selected. Always clear the auto
819 HALT restart flag if it is set.
821 @param[in] CpuIndex The processor index for the currently
823 @param[in] CpuState Pointer to SMRAM Save State Map for the
824 currently executing CPU.
825 @param[in] NewInstructionPointer32 Instruction pointer to use if resuming to
826 32-bit mode from 64-bit SMM.
827 @param[in] NewInstructionPointer Instruction pointer to use if resuming to
830 @retval The value of the original instruction pointer before it was hooked.
837 SMRAM_SAVE_STATE_MAP
*CpuState
,
838 UINT64 NewInstructionPointer32
,
839 UINT64 NewInstructionPointer
843 Get the size of the SMI Handler in bytes.
845 @retval The size, in bytes, of the SMI Handler.
855 Install the SMI handler for the CPU specified by CpuIndex. This function
856 is called by the CPU that was elected as monarch during System Management
859 @param[in] CpuIndex The index of the CPU to install the custom SMI handler.
860 The value must be between 0 and the NumberOfCpus field
861 in the System Management System Table (SMST).
862 @param[in] SmBase The SMBASE address for the CPU specified by CpuIndex.
863 @param[in] SmiStack The stack to use when an SMI is processed by the
864 the CPU specified by CpuIndex.
865 @param[in] StackSize The size, in bytes, if the stack used when an SMI is
866 processed by the CPU specified by CpuIndex.
867 @param[in] GdtBase The base address of the GDT to use when an SMI is
868 processed by the CPU specified by CpuIndex.
869 @param[in] GdtSize The size, in bytes, of the GDT used when an SMI is
870 processed by the CPU specified by CpuIndex.
871 @param[in] IdtBase The base address of the IDT to use when an SMI is
872 processed by the CPU specified by CpuIndex.
873 @param[in] IdtSize The size, in bytes, of the IDT used when an SMI is
874 processed by the CPU specified by CpuIndex.
875 @param[in] Cr3 The base address of the page tables to use when an SMI
876 is processed by the CPU specified by CpuIndex.
893 Search module name by input IP address and output it.
895 @param CallerIpAddress Caller instruction pointer.
900 IN UINTN CallerIpAddress
904 This function sets memory attribute according to MemoryAttributesTable.
907 SetMemMapAttributes (
912 This function sets UEFI memory attribute according to UEFI memory map.
915 SetUefiMemMapAttributes (
920 Return if the Address is forbidden as SMM communication buffer.
922 @param[in] Address the address to be checked
924 @return TRUE The address is forbidden as SMM communication buffer.
925 @return FALSE The address is allowed as SMM communication buffer.
928 IsSmmCommBufferForbiddenAddress (
933 This function caches the UEFI memory map information.
941 This function sets memory attribute for page table.
944 SetPageTableAttributes (
949 Return page table base.
951 @return page table base.
959 This function sets the attributes for the memory region specified by BaseAddress and
960 Length from their current attributes to the attributes specified by Attributes.
962 @param[in] BaseAddress The physical address that is the start address of a memory region.
963 @param[in] Length The size in bytes of the memory region.
964 @param[in] Attributes The bit mask of attributes to set for the memory region.
965 @param[out] IsSplitted TRUE means page table splitted. FALSE means page table not splitted.
967 @retval EFI_SUCCESS The attributes were set for the memory region.
968 @retval EFI_ACCESS_DENIED The attributes for the memory resource range specified by
969 BaseAddress and Length cannot be modified.
970 @retval EFI_INVALID_PARAMETER Length is zero.
971 Attributes specified an illegal combination of attributes that
972 cannot be set together.
973 @retval EFI_OUT_OF_RESOURCES There are not enough system resources to modify the attributes of
974 the memory resource range.
975 @retval EFI_UNSUPPORTED The processor does not support one or more bytes of the memory
976 resource range specified by BaseAddress and Length.
977 The bit mask of attributes is not support for the memory resource
978 range specified by BaseAddress and Length.
983 SmmSetMemoryAttributesEx (
984 IN EFI_PHYSICAL_ADDRESS BaseAddress
,
986 IN UINT64 Attributes
,
987 OUT BOOLEAN
*IsSplitted OPTIONAL
991 This function clears the attributes for the memory region specified by BaseAddress and
992 Length from their current attributes to the attributes specified by Attributes.
994 @param[in] BaseAddress The physical address that is the start address of a memory region.
995 @param[in] Length The size in bytes of the memory region.
996 @param[in] Attributes The bit mask of attributes to clear for the memory region.
997 @param[out] IsSplitted TRUE means page table splitted. FALSE means page table not splitted.
999 @retval EFI_SUCCESS The attributes were cleared for the memory region.
1000 @retval EFI_ACCESS_DENIED The attributes for the memory resource range specified by
1001 BaseAddress and Length cannot be modified.
1002 @retval EFI_INVALID_PARAMETER Length is zero.
1003 Attributes specified an illegal combination of attributes that
1004 cannot be set together.
1005 @retval EFI_OUT_OF_RESOURCES There are not enough system resources to modify the attributes of
1006 the memory resource range.
1007 @retval EFI_UNSUPPORTED The processor does not support one or more bytes of the memory
1008 resource range specified by BaseAddress and Length.
1009 The bit mask of attributes is not support for the memory resource
1010 range specified by BaseAddress and Length.
1015 SmmClearMemoryAttributesEx (
1016 IN EFI_PHYSICAL_ADDRESS BaseAddress
,
1018 IN UINT64 Attributes
,
1019 OUT BOOLEAN
*IsSplitted OPTIONAL
1023 This API provides a way to allocate memory for page table.
1025 This API can be called more once to allocate memory for page tables.
1027 Allocates the number of 4KB pages of type EfiRuntimeServicesData and returns a pointer to the
1028 allocated buffer. The buffer returned is aligned on a 4KB boundary. If Pages is 0, then NULL
1029 is returned. If there is not enough memory remaining to satisfy the request, then NULL is
1032 @param Pages The number of 4 KB pages to allocate.
1034 @return A pointer to the allocated buffer or NULL if allocation fails.
1038 AllocatePageTableMemory (
1043 Allocate pages for code.
1045 @param[in] Pages Number of pages to be allocated.
1047 @return Allocated memory.
1055 Allocate aligned pages for code.
1057 @param[in] Pages Number of pages to be allocated.
1058 @param[in] Alignment The requested alignment of the allocation.
1059 Must be a power of two.
1060 If Alignment is zero, then byte alignment is used.
1062 @return Allocated memory.
1065 AllocateAlignedCodePages (
1072 // S3 related global variable and function prototype.
1075 extern BOOLEAN mSmmS3Flag
;
1078 Initialize SMM S3 resume state structure used during S3 Resume.
1080 @param[in] Cr3 The base address of the page tables to use in SMM.
1084 InitSmmS3ResumeState (
1098 Restore SMM Configuration in S3 boot path.
1102 RestoreSmmConfigurationInS3 (
1107 Get ACPI S3 enable flag.
1111 GetAcpiS3EnableFlag (
1116 Transfer AP to safe hlt-loop after it finished restore CPU features on S3 patch.
1118 @param[in] ApHltLoopCode The address of the safe hlt-loop function.
1119 @param[in] TopOfStack A pointer to the new stack to use for the ApHltLoopCode.
1120 @param[in] NumberToFinishAddress Address of Semaphore of APs finish count.
1124 TransferApToSafeState (
1125 IN UINTN ApHltLoopCode
,
1126 IN UINTN TopOfStack
,
1127 IN UINTN NumberToFinishAddress
1131 Set ShadowStack memory.
1133 @param[in] Cr3 The page table base address.
1134 @param[in] BaseAddress The physical address that is the start address of a memory region.
1135 @param[in] Length The size in bytes of the memory region.
1137 @retval EFI_SUCCESS The shadow stack memory is set.
1142 IN EFI_PHYSICAL_ADDRESS BaseAddress
,
1147 Set not present memory.
1149 @param[in] Cr3 The page table base address.
1150 @param[in] BaseAddress The physical address that is the start address of a memory region.
1151 @param[in] Length The size in bytes of the memory region.
1153 @retval EFI_SUCCESS The not present memory is set.
1158 IN EFI_PHYSICAL_ADDRESS BaseAddress
,
1163 Initialize the shadow stack related data structure.
1165 @param CpuIndex The index of CPU.
1166 @param ShadowStack The bottom of the shadow stack for this CPU.
1171 IN VOID
*ShadowStack
1175 This function set given attributes of the memory region specified by
1176 BaseAddress and Length.
1178 @param This The EDKII_SMM_MEMORY_ATTRIBUTE_PROTOCOL instance.
1179 @param BaseAddress The physical address that is the start address of
1181 @param Length The size in bytes of the memory region.
1182 @param Attributes The bit mask of attributes to set for the memory
1185 @retval EFI_SUCCESS The attributes were set for the memory region.
1186 @retval EFI_INVALID_PARAMETER Length is zero.
1187 Attributes specified an illegal combination of
1188 attributes that cannot be set together.
1189 @retval EFI_UNSUPPORTED The processor does not support one or more
1190 bytes of the memory resource range specified
1191 by BaseAddress and Length.
1192 The bit mask of attributes is not supported for
1193 the memory resource range specified by
1194 BaseAddress and Length.
1199 EdkiiSmmSetMemoryAttributes (
1200 IN EDKII_SMM_MEMORY_ATTRIBUTE_PROTOCOL
*This
,
1201 IN EFI_PHYSICAL_ADDRESS BaseAddress
,
1203 IN UINT64 Attributes
1207 This function clears given attributes of the memory region specified by
1208 BaseAddress and Length.
1210 @param This The EDKII_SMM_MEMORY_ATTRIBUTE_PROTOCOL instance.
1211 @param BaseAddress The physical address that is the start address of
1213 @param Length The size in bytes of the memory region.
1214 @param Attributes The bit mask of attributes to clear for the memory
1217 @retval EFI_SUCCESS The attributes were cleared for the memory region.
1218 @retval EFI_INVALID_PARAMETER Length is zero.
1219 Attributes specified an illegal combination of
1220 attributes that cannot be cleared together.
1221 @retval EFI_UNSUPPORTED The processor does not support one or more
1222 bytes of the memory resource range specified
1223 by BaseAddress and Length.
1224 The bit mask of attributes is not supported for
1225 the memory resource range specified by
1226 BaseAddress and Length.
1231 EdkiiSmmClearMemoryAttributes (
1232 IN EDKII_SMM_MEMORY_ATTRIBUTE_PROTOCOL
*This
,
1233 IN EFI_PHYSICAL_ADDRESS BaseAddress
,
1235 IN UINT64 Attributes
1239 This function retrieves the attributes of the memory region specified by
1240 BaseAddress and Length. If different attributes are got from different part
1241 of the memory region, EFI_NO_MAPPING will be returned.
1243 @param This The EDKII_SMM_MEMORY_ATTRIBUTE_PROTOCOL instance.
1244 @param BaseAddress The physical address that is the start address of
1246 @param Length The size in bytes of the memory region.
1247 @param Attributes Pointer to attributes returned.
1249 @retval EFI_SUCCESS The attributes got for the memory region.
1250 @retval EFI_INVALID_PARAMETER Length is zero.
1252 @retval EFI_NO_MAPPING Attributes are not consistent cross the memory
1254 @retval EFI_UNSUPPORTED The processor does not support one or more
1255 bytes of the memory resource range specified
1256 by BaseAddress and Length.
1261 EdkiiSmmGetMemoryAttributes (
1262 IN EDKII_SMM_MEMORY_ATTRIBUTE_PROTOCOL
*This
,
1263 IN EFI_PHYSICAL_ADDRESS BaseAddress
,
1265 IN UINT64
*Attributes
1269 This function fixes up the address of the global variable or function
1270 referred in SmmInit assembly files to be the absoute address.
1274 PiSmmCpuSmmInitFixupAddress (
1278 This function fixes up the address of the global variable or function
1279 referred in SmiEntry assembly files to be the absoute address.
1283 PiSmmCpuSmiEntryFixupAddress (
1287 This function reads CR2 register when on-demand paging is enabled
1288 for 64 bit and no action for 32 bit.
1290 @param[out] *Cr2 Pointer to variable to hold CR2 register value.
1298 This function writes into CR2 register when on-demand paging is enabled
1299 for 64 bit and no action for 32 bit.
1301 @param[in] Cr2 Value to write into CR2 register.
1309 Schedule a procedure to run on the specified CPU.
1311 @param[in] Procedure The address of the procedure to run
1312 @param[in] CpuIndex Target CPU Index
1313 @param[in,out] ProcArguments The parameter to pass to the procedure
1314 @param[in,out] Token This is an optional parameter that allows the caller to execute the
1315 procedure in a blocking or non-blocking fashion. If it is NULL the
1316 call is blocking, and the call will not return until the AP has
1317 completed the procedure. If the token is not NULL, the call will
1318 return immediately. The caller can check whether the procedure has
1319 completed with CheckOnProcedure or WaitForProcedure.
1320 @param[in] TimeoutInMicroseconds Indicates the time limit in microseconds for the APs to finish
1321 execution of Procedure, either for blocking or non-blocking mode.
1322 Zero means infinity. If the timeout expires before all APs return
1323 from Procedure, then Procedure on the failed APs is terminated. If
1324 the timeout expires in blocking mode, the call returns EFI_TIMEOUT.
1325 If the timeout expires in non-blocking mode, the timeout determined
1326 can be through CheckOnProcedure or WaitForProcedure.
1327 Note that timeout support is optional. Whether an implementation
1328 supports this feature can be determined via the Attributes data
1330 @param[in,out] CpuStatus This optional pointer may be used to get the status code returned
1331 by Procedure when it completes execution on the target AP, or with
1332 EFI_TIMEOUT if the Procedure fails to complete within the optional
1333 timeout. The implementation will update this variable with
1334 EFI_NOT_READY prior to starting Procedure on the target AP.
1336 @retval EFI_INVALID_PARAMETER CpuNumber not valid
1337 @retval EFI_INVALID_PARAMETER CpuNumber specifying BSP
1338 @retval EFI_INVALID_PARAMETER The AP specified by CpuNumber did not enter SMM
1339 @retval EFI_INVALID_PARAMETER The AP specified by CpuNumber is busy
1340 @retval EFI_SUCCESS The procedure has been successfully scheduled
1344 InternalSmmStartupThisAp (
1345 IN EFI_AP_PROCEDURE2 Procedure
,
1347 IN OUT VOID
*ProcArguments OPTIONAL
,
1348 IN OUT MM_COMPLETION
*Token
,
1349 IN UINTN TimeoutInMicroseconds
,
1350 IN OUT EFI_STATUS
*CpuStatus
1354 Checks whether the input token is the current used token.
1356 @param[in] Token This parameter describes the token that was passed into DispatchProcedure or
1359 @retval TRUE The input token is the current used token.
1360 @retval FALSE The input token is not the current used token.
1368 Checks status of specified AP.
1370 This function checks whether the specified AP has finished the task assigned
1371 by StartupThisAP(), and whether timeout expires.
1373 @param[in] Token This parameter describes the token that was passed into DispatchProcedure or
1376 @retval EFI_SUCCESS Specified AP has finished task assigned by StartupThisAPs().
1377 @retval EFI_NOT_READY Specified AP has not finished task and timeout has not expired.
1385 Check whether it is an present AP.
1387 @param CpuIndex The AP index which calls this function.
1389 @retval TRUE It's a present AP.
1390 @retval TRUE This is not an AP or it is not present.
1399 Worker function to execute a caller provided function on all enabled APs.
1401 @param[in] Procedure A pointer to the function to be run on
1402 enabled APs of the system.
1403 @param[in] TimeoutInMicroseconds Indicates the time limit in microseconds for
1404 APs to return from Procedure, either for
1405 blocking or non-blocking mode.
1406 @param[in,out] ProcedureArguments The parameter passed into Procedure for
1408 @param[in,out] Token This is an optional parameter that allows the caller to execute the
1409 procedure in a blocking or non-blocking fashion. If it is NULL the
1410 call is blocking, and the call will not return until the AP has
1411 completed the procedure. If the token is not NULL, the call will
1412 return immediately. The caller can check whether the procedure has
1413 completed with CheckOnProcedure or WaitForProcedure.
1414 @param[in,out] CPUStatus This optional pointer may be used to get the status code returned
1415 by Procedure when it completes execution on the target AP, or with
1416 EFI_TIMEOUT if the Procedure fails to complete within the optional
1417 timeout. The implementation will update this variable with
1418 EFI_NOT_READY prior to starting Procedure on the target AP.
1420 @retval EFI_SUCCESS In blocking mode, all APs have finished before
1421 the timeout expired.
1422 @retval EFI_SUCCESS In non-blocking mode, function has been dispatched
1424 @retval others Failed to Startup all APs.
1428 InternalSmmStartupAllAPs (
1429 IN EFI_AP_PROCEDURE2 Procedure
,
1430 IN UINTN TimeoutInMicroseconds
,
1431 IN OUT VOID
*ProcedureArguments OPTIONAL
,
1432 IN OUT MM_COMPLETION
*Token
,
1433 IN OUT EFI_STATUS
*CPUStatus
1438 Register the SMM Foundation entry point.
1440 @param[in] Procedure A pointer to the code stream to be run on the designated target AP
1441 of the system. Type EFI_AP_PROCEDURE is defined below in Volume 2
1442 with the related definitions of
1443 EFI_MP_SERVICES_PROTOCOL.StartupAllAPs.
1444 If caller may pass a value of NULL to deregister any existing
1446 @param[in,out] ProcedureArguments Allows the caller to pass a list of parameters to the code that is
1447 run by the AP. It is an optional common mailbox between APs and
1448 the caller to share information
1450 @retval EFI_SUCCESS The Procedure has been set successfully.
1451 @retval EFI_INVALID_PARAMETER The Procedure is NULL but ProcedureArguments not NULL.
1455 RegisterStartupProcedure (
1456 IN EFI_AP_PROCEDURE Procedure
,
1457 IN OUT VOID
*ProcedureArguments OPTIONAL
1461 Allocate buffer for SpinLock and Wrapper function buffer.
1465 InitializeDataForMmMp (
1470 Return whether access to non-SMRAM is restricted.
1472 @retval TRUE Access to non-SMRAM is restricted.
1473 @retval FALSE Access to non-SMRAM is not restricted.
1476 IsRestrictedMemoryAccess (