2 Agent Module to load other modules to deploy SMM Entry Vector for X86 CPU.
4 Copyright (c) 2009 - 2016, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
15 #include "PiSmmCpuDxeSmm.h"
18 // SMM CPU Private Data structure that contains SMM Configuration Protocol
19 // along its supporting fields.
21 SMM_CPU_PRIVATE_DATA mSmmCpuPrivateData
= {
22 SMM_CPU_PRIVATE_DATA_SIGNATURE
, // Signature
24 NULL
, // Pointer to ProcessorInfo array
25 NULL
, // Pointer to Operation array
26 NULL
, // Pointer to CpuSaveStateSize array
27 NULL
, // Pointer to CpuSaveState array
28 { {0} }, // SmmReservedSmramRegion
30 SmmStartupThisAp
, // SmmCoreEntryContext.SmmStartupThisAp
31 0, // SmmCoreEntryContext.CurrentlyExecutingCpu
32 0, // SmmCoreEntryContext.NumberOfCpus
33 NULL
, // SmmCoreEntryContext.CpuSaveStateSize
34 NULL
// SmmCoreEntryContext.CpuSaveState
38 mSmmCpuPrivateData
.SmmReservedSmramRegion
, // SmmConfiguration.SmramReservedRegions
39 RegisterSmmEntry
// SmmConfiguration.RegisterSmmEntry
43 CPU_HOT_PLUG_DATA mCpuHotPlugData
= {
44 CPU_HOT_PLUG_DATA_REVISION_1
, // Revision
45 0, // Array Length of SmBase and APIC ID
46 NULL
, // Pointer to APIC ID array
47 NULL
, // Pointer to SMBASE array
54 // Global pointer used to access mSmmCpuPrivateData from outside and inside SMM
56 SMM_CPU_PRIVATE_DATA
*gSmmCpuPrivate
= &mSmmCpuPrivateData
;
59 // SMM Relocation variables
61 volatile BOOLEAN
*mRebased
;
62 volatile BOOLEAN mIsBsp
;
65 /// Handle for the SMM CPU Protocol
67 EFI_HANDLE mSmmCpuHandle
= NULL
;
70 /// SMM CPU Protocol instance
72 EFI_SMM_CPU_PROTOCOL mSmmCpu
= {
77 EFI_CPU_INTERRUPT_HANDLER mExternalVectorTable
[EXCEPTION_VECTOR_NUMBER
];
80 // SMM stack information
82 UINTN mSmmStackArrayBase
;
83 UINTN mSmmStackArrayEnd
;
87 // Pointer to structure used during S3 Resume
89 SMM_S3_RESUME_STATE
*mSmmS3ResumeState
= NULL
;
91 UINTN mMaxNumberOfCpus
= 1;
92 UINTN mNumberOfCpus
= 1;
95 // SMM ready to lock flag
97 BOOLEAN mSmmReadyToLock
= FALSE
;
102 BOOLEAN mSmmS3Flag
= FALSE
;
105 // Global used to cache PCD for SMM Code Access Check enable
107 BOOLEAN mSmmCodeAccessCheckEnable
= FALSE
;
110 // Spin lock used to serialize setting of SMM Code Access Check feature
112 SPIN_LOCK
*mConfigSmmCodeAccessCheckLock
= NULL
;
115 Initialize IDT to setup exception handlers for SMM.
124 BOOLEAN InterruptState
;
125 IA32_DESCRIPTOR DxeIdtr
;
127 // Disable Interrupt and save DXE IDT table
129 InterruptState
= SaveAndDisableInterrupts ();
130 AsmReadIdtr (&DxeIdtr
);
132 // Load SMM temporary IDT table
134 AsmWriteIdtr (&gcSmiIdtr
);
136 // Setup SMM default exception handlers, SMM IDT table
137 // will be updated and saved in gcSmiIdtr
139 Status
= InitializeCpuExceptionHandlers (NULL
);
140 ASSERT_EFI_ERROR (Status
);
142 // Restore DXE IDT table and CPU interrupt
144 AsmWriteIdtr ((IA32_DESCRIPTOR
*) &DxeIdtr
);
145 SetInterruptState (InterruptState
);
149 Search module name by input IP address and output it.
151 @param CallerIpAddress Caller instruction pointer.
156 IN UINTN CallerIpAddress
160 EFI_IMAGE_DOS_HEADER
*DosHdr
;
161 EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr
;
163 UINT64 DumpIpAddress
;
168 Pe32Data
= CallerIpAddress
& ~(SIZE_4KB
- 1);
169 while (Pe32Data
!= 0) {
170 DosHdr
= (EFI_IMAGE_DOS_HEADER
*) Pe32Data
;
171 if (DosHdr
->e_magic
== EFI_IMAGE_DOS_SIGNATURE
) {
173 // DOS image header is present, so read the PE header after the DOS image header.
175 Hdr
.Pe32
= (EFI_IMAGE_NT_HEADERS32
*)(Pe32Data
+ (UINTN
) ((DosHdr
->e_lfanew
) & 0x0ffff));
177 // Make sure PE header address does not overflow and is less than the initial address.
179 if (((UINTN
)Hdr
.Pe32
> Pe32Data
) && ((UINTN
)Hdr
.Pe32
< CallerIpAddress
)) {
180 if (Hdr
.Pe32
->Signature
== EFI_IMAGE_NT_SIGNATURE
) {
190 // Not found the image base, check the previous aligned address
192 Pe32Data
-= SIZE_4KB
;
195 DumpIpAddress
= CallerIpAddress
;
196 DEBUG ((EFI_D_ERROR
, "It is invoked from the instruction before IP(0x%lx)", DumpIpAddress
));
199 PdbPointer
= PeCoffLoaderGetPdbPointer ((VOID
*) Pe32Data
);
200 if (PdbPointer
!= NULL
) {
201 DEBUG ((EFI_D_ERROR
, " in module (%a)", PdbPointer
));
207 Read information from the CPU save state.
209 @param This EFI_SMM_CPU_PROTOCOL instance
210 @param Width The number of bytes to read from the CPU save state.
211 @param Register Specifies the CPU register to read form the save state.
212 @param CpuIndex Specifies the zero-based index of the CPU save state.
213 @param Buffer Upon return, this holds the CPU register value read from the save state.
215 @retval EFI_SUCCESS The register was read from Save State
216 @retval EFI_NOT_FOUND The register is not defined for the Save State of Processor
217 @retval EFI_INVALID_PARAMTER This or Buffer is NULL.
223 IN CONST EFI_SMM_CPU_PROTOCOL
*This
,
225 IN EFI_SMM_SAVE_STATE_REGISTER Register
,
233 // Retrieve pointer to the specified CPU's SMM Save State buffer
235 if ((CpuIndex
>= gSmst
->NumberOfCpus
) || (Buffer
== NULL
)) {
236 return EFI_INVALID_PARAMETER
;
240 // Check for special EFI_SMM_SAVE_STATE_REGISTER_PROCESSOR_ID
242 if (Register
== EFI_SMM_SAVE_STATE_REGISTER_PROCESSOR_ID
) {
244 // The pseudo-register only supports the 64-bit size specified by Width.
246 if (Width
!= sizeof (UINT64
)) {
247 return EFI_INVALID_PARAMETER
;
250 // If the processor is in SMM at the time the SMI occurred,
251 // the pseudo register value for EFI_SMM_SAVE_STATE_REGISTER_PROCESSOR_ID is returned in Buffer.
252 // Otherwise, EFI_NOT_FOUND is returned.
254 if (*(mSmmMpSyncData
->CpuData
[CpuIndex
].Present
)) {
255 *(UINT64
*)Buffer
= gSmmCpuPrivate
->ProcessorInfo
[CpuIndex
].ProcessorId
;
258 return EFI_NOT_FOUND
;
262 if (!(*(mSmmMpSyncData
->CpuData
[CpuIndex
].Present
))) {
263 return EFI_INVALID_PARAMETER
;
266 Status
= SmmCpuFeaturesReadSaveStateRegister (CpuIndex
, Register
, Width
, Buffer
);
267 if (Status
== EFI_UNSUPPORTED
) {
268 Status
= ReadSaveStateRegister (CpuIndex
, Register
, Width
, Buffer
);
274 Write data to the CPU save state.
276 @param This EFI_SMM_CPU_PROTOCOL instance
277 @param Width The number of bytes to read from the CPU save state.
278 @param Register Specifies the CPU register to write to the save state.
279 @param CpuIndex Specifies the zero-based index of the CPU save state
280 @param Buffer Upon entry, this holds the new CPU register value.
282 @retval EFI_SUCCESS The register was written from Save State
283 @retval EFI_NOT_FOUND The register is not defined for the Save State of Processor
284 @retval EFI_INVALID_PARAMTER ProcessorIndex or Width is not correct
290 IN CONST EFI_SMM_CPU_PROTOCOL
*This
,
292 IN EFI_SMM_SAVE_STATE_REGISTER Register
,
294 IN CONST VOID
*Buffer
300 // Retrieve pointer to the specified CPU's SMM Save State buffer
302 if ((CpuIndex
>= gSmst
->NumberOfCpus
) || (Buffer
== NULL
)) {
303 return EFI_INVALID_PARAMETER
;
307 // Writes to EFI_SMM_SAVE_STATE_REGISTER_PROCESSOR_ID are ignored
309 if (Register
== EFI_SMM_SAVE_STATE_REGISTER_PROCESSOR_ID
) {
313 if (!mSmmMpSyncData
->CpuData
[CpuIndex
].Present
) {
314 return EFI_INVALID_PARAMETER
;
317 Status
= SmmCpuFeaturesWriteSaveStateRegister (CpuIndex
, Register
, Width
, Buffer
);
318 if (Status
== EFI_UNSUPPORTED
) {
319 Status
= WriteSaveStateRegister (CpuIndex
, Register
, Width
, Buffer
);
326 C function for SMI handler. To change all processor's SMMBase Register.
339 // Update SMM IDT entries' code segment and load IDT
341 AsmWriteIdtr (&gcSmiIdtr
);
342 ApicId
= GetApicId ();
344 ASSERT (mNumberOfCpus
<= PcdGet32 (PcdCpuMaxLogicalProcessorNumber
));
346 for (Index
= 0; Index
< mNumberOfCpus
; Index
++) {
347 if (ApicId
== (UINT32
)gSmmCpuPrivate
->ProcessorInfo
[Index
].ProcessorId
) {
349 // Initialize SMM specific features on the currently executing CPU
351 SmmCpuFeaturesInitializeProcessor (
354 gSmmCpuPrivate
->ProcessorInfo
,
360 // BSP rebase is already done above.
361 // Initialize private data during S3 resume
363 InitializeMpSyncData ();
367 // Hook return after RSM to set SMM re-based flag
369 SemaphoreHook (Index
, &mRebased
[Index
]);
378 Relocate SmmBases for each processor.
380 Execute on first boot and all S3 resumes
389 UINT8 BakBuf
[BACK_BUF_SIZE
];
390 SMRAM_SAVE_STATE_MAP BakBuf2
;
391 SMRAM_SAVE_STATE_MAP
*CpuStatePtr
;
398 // Make sure the reserved size is large enough for procedure SmmInitTemplate.
400 ASSERT (sizeof (BakBuf
) >= gcSmmInitSize
);
403 // Patch ASM code template with current CR0, CR3, and CR4 values
405 gSmmCr0
= (UINT32
)AsmReadCr0 ();
406 gSmmCr3
= (UINT32
)AsmReadCr3 ();
407 gSmmCr4
= (UINT32
)AsmReadCr4 ();
410 // Patch GDTR for SMM base relocation
412 gcSmiInitGdtr
.Base
= gcSmiGdtr
.Base
;
413 gcSmiInitGdtr
.Limit
= gcSmiGdtr
.Limit
;
415 U8Ptr
= (UINT8
*)(UINTN
)(SMM_DEFAULT_SMBASE
+ SMM_HANDLER_OFFSET
);
416 CpuStatePtr
= (SMRAM_SAVE_STATE_MAP
*)(UINTN
)(SMM_DEFAULT_SMBASE
+ SMRAM_SAVE_STATE_MAP_OFFSET
);
419 // Backup original contents at address 0x38000
421 CopyMem (BakBuf
, U8Ptr
, sizeof (BakBuf
));
422 CopyMem (&BakBuf2
, CpuStatePtr
, sizeof (BakBuf2
));
425 // Load image for relocation
427 CopyMem (U8Ptr
, gcSmmInitTemplate
, gcSmmInitSize
);
430 // Retrieve the local APIC ID of current processor
432 ApicId
= GetApicId ();
435 // Relocate SM bases for all APs
436 // This is APs' 1st SMI - rebase will be done here, and APs' default SMI handler will be overridden by gcSmmInitTemplate
439 BspIndex
= (UINTN
)-1;
440 for (Index
= 0; Index
< mNumberOfCpus
; Index
++) {
441 mRebased
[Index
] = FALSE
;
442 if (ApicId
!= (UINT32
)gSmmCpuPrivate
->ProcessorInfo
[Index
].ProcessorId
) {
443 SendSmiIpi ((UINT32
)gSmmCpuPrivate
->ProcessorInfo
[Index
].ProcessorId
);
445 // Wait for this AP to finish its 1st SMI
447 while (!mRebased
[Index
]);
450 // BSP will be Relocated later
457 // Relocate BSP's SMM base
459 ASSERT (BspIndex
!= (UINTN
)-1);
463 // Wait for the BSP to finish its 1st SMI
465 while (!mRebased
[BspIndex
]);
468 // Restore contents at address 0x38000
470 CopyMem (CpuStatePtr
, &BakBuf2
, sizeof (BakBuf2
));
471 CopyMem (U8Ptr
, BakBuf
, sizeof (BakBuf
));
475 Perform SMM initialization for all processors in the S3 boot path.
477 For a native platform, MP initialization in the S3 boot path is also performed in this function.
485 SMM_S3_RESUME_STATE
*SmmS3ResumeState
;
486 IA32_DESCRIPTOR Ia32Idtr
;
487 IA32_DESCRIPTOR X64Idtr
;
488 IA32_IDT_GATE_DESCRIPTOR IdtEntryTable
[EXCEPTION_VECTOR_NUMBER
];
491 DEBUG ((EFI_D_INFO
, "SmmRestoreCpu()\n"));
495 InitializeSpinLock (mMemoryMappedLock
);
498 // See if there is enough context to resume PEI Phase
500 if (mSmmS3ResumeState
== NULL
) {
501 DEBUG ((EFI_D_ERROR
, "No context to return to PEI Phase\n"));
505 SmmS3ResumeState
= mSmmS3ResumeState
;
506 ASSERT (SmmS3ResumeState
!= NULL
);
508 if (SmmS3ResumeState
->Signature
== SMM_S3_RESUME_SMM_64
) {
510 // Save the IA32 IDT Descriptor
512 AsmReadIdtr ((IA32_DESCRIPTOR
*) &Ia32Idtr
);
515 // Setup X64 IDT table
517 ZeroMem (IdtEntryTable
, sizeof (IA32_IDT_GATE_DESCRIPTOR
) * 32);
518 X64Idtr
.Base
= (UINTN
) IdtEntryTable
;
519 X64Idtr
.Limit
= (UINT16
) (sizeof (IA32_IDT_GATE_DESCRIPTOR
) * 32 - 1);
520 AsmWriteIdtr ((IA32_DESCRIPTOR
*) &X64Idtr
);
523 // Setup the default exception handler
525 Status
= InitializeCpuExceptionHandlers (NULL
);
526 ASSERT_EFI_ERROR (Status
);
529 // Initialize Debug Agent to support source level debug
531 InitializeDebugAgent (DEBUG_AGENT_INIT_THUNK_PEI_IA32TOX64
, (VOID
*)&Ia32Idtr
, NULL
);
535 // Skip initialization if mAcpiCpuData is not valid
537 if (mAcpiCpuData
.NumberOfCpus
> 0) {
539 // First time microcode load and restore MTRRs
541 EarlyInitializeCpu ();
545 // Restore SMBASE for BSP and all APs
550 // Skip initialization if mAcpiCpuData is not valid
552 if (mAcpiCpuData
.NumberOfCpus
> 0) {
554 // Restore MSRs for BSP and all APs
560 // Set a flag to restore SMM configuration in S3 path.
562 mRestoreSmmConfigurationInS3
= TRUE
;
564 DEBUG (( EFI_D_INFO
, "SMM S3 Return CS = %x\n", SmmS3ResumeState
->ReturnCs
));
565 DEBUG (( EFI_D_INFO
, "SMM S3 Return Entry Point = %x\n", SmmS3ResumeState
->ReturnEntryPoint
));
566 DEBUG (( EFI_D_INFO
, "SMM S3 Return Context1 = %x\n", SmmS3ResumeState
->ReturnContext1
));
567 DEBUG (( EFI_D_INFO
, "SMM S3 Return Context2 = %x\n", SmmS3ResumeState
->ReturnContext2
));
568 DEBUG (( EFI_D_INFO
, "SMM S3 Return Stack Pointer = %x\n", SmmS3ResumeState
->ReturnStackPointer
));
571 // If SMM is in 32-bit mode, then use SwitchStack() to resume PEI Phase
573 if (SmmS3ResumeState
->Signature
== SMM_S3_RESUME_SMM_32
) {
574 DEBUG ((EFI_D_INFO
, "Call SwitchStack() to return to S3 Resume in PEI Phase\n"));
577 (SWITCH_STACK_ENTRY_POINT
)(UINTN
)SmmS3ResumeState
->ReturnEntryPoint
,
578 (VOID
*)(UINTN
)SmmS3ResumeState
->ReturnContext1
,
579 (VOID
*)(UINTN
)SmmS3ResumeState
->ReturnContext2
,
580 (VOID
*)(UINTN
)SmmS3ResumeState
->ReturnStackPointer
585 // If SMM is in 64-bit mode, then use AsmDisablePaging64() to resume PEI Phase
587 if (SmmS3ResumeState
->Signature
== SMM_S3_RESUME_SMM_64
) {
588 DEBUG ((EFI_D_INFO
, "Call AsmDisablePaging64() to return to S3 Resume in PEI Phase\n"));
590 // Disable interrupt of Debug timer, since new IDT table is for IA32 and will not work in long mode.
592 SaveAndSetDebugTimerInterrupt (FALSE
);
594 // Restore IA32 IDT table
596 AsmWriteIdtr ((IA32_DESCRIPTOR
*) &Ia32Idtr
);
598 SmmS3ResumeState
->ReturnCs
,
599 (UINT32
)SmmS3ResumeState
->ReturnEntryPoint
,
600 (UINT32
)SmmS3ResumeState
->ReturnContext1
,
601 (UINT32
)SmmS3ResumeState
->ReturnContext2
,
602 (UINT32
)SmmS3ResumeState
->ReturnStackPointer
607 // Can not resume PEI Phase
609 DEBUG ((EFI_D_ERROR
, "No context to return to PEI Phase\n"));
614 Copy register table from ACPI NVS memory into SMRAM.
616 @param[in] DestinationRegisterTableList Points to destination register table.
617 @param[in] SourceRegisterTableList Points to source register table.
618 @param[in] NumberOfCpus Number of CPUs.
623 IN CPU_REGISTER_TABLE
*DestinationRegisterTableList
,
624 IN CPU_REGISTER_TABLE
*SourceRegisterTableList
,
625 IN UINT32 NumberOfCpus
630 CPU_REGISTER_TABLE_ENTRY
*RegisterTableEntry
;
632 CopyMem (DestinationRegisterTableList
, SourceRegisterTableList
, NumberOfCpus
* sizeof (CPU_REGISTER_TABLE
));
633 for (Index
= 0; Index
< NumberOfCpus
; Index
++) {
634 DestinationRegisterTableList
[Index
].RegisterTableEntry
= AllocatePool (DestinationRegisterTableList
[Index
].AllocatedSize
);
635 ASSERT (DestinationRegisterTableList
[Index
].RegisterTableEntry
!= NULL
);
636 CopyMem (DestinationRegisterTableList
[Index
].RegisterTableEntry
, SourceRegisterTableList
[Index
].RegisterTableEntry
, DestinationRegisterTableList
[Index
].AllocatedSize
);
638 // Go though all MSRs in register table to initialize MSR spin lock
640 RegisterTableEntry
= DestinationRegisterTableList
[Index
].RegisterTableEntry
;
641 for (Index1
= 0; Index1
< DestinationRegisterTableList
[Index
].TableLength
; Index1
++, RegisterTableEntry
++) {
642 if ((RegisterTableEntry
->RegisterType
== Msr
) && (RegisterTableEntry
->ValidBitLength
< 64)) {
644 // Initialize MSR spin lock only for those MSRs need bit field writing
646 InitMsrSpinLockByIndex (RegisterTableEntry
->Index
);
653 SMM Ready To Lock event notification handler.
655 The CPU S3 data is copied to SMRAM for security and mSmmReadyToLock is set to
656 perform additional lock actions that must be performed from SMM on the next SMI.
658 @param[in] Protocol Points to the protocol's unique identifier.
659 @param[in] Interface Points to the interface instance.
660 @param[in] Handle The handle on which the interface was installed.
662 @retval EFI_SUCCESS Notification handler runs successfully.
666 SmmReadyToLockEventNotify (
667 IN CONST EFI_GUID
*Protocol
,
672 ACPI_CPU_DATA
*AcpiCpuData
;
673 IA32_DESCRIPTOR
*Gdtr
;
674 IA32_DESCRIPTOR
*Idtr
;
677 // Prevent use of mAcpiCpuData by initialize NumberOfCpus to 0
679 mAcpiCpuData
.NumberOfCpus
= 0;
682 // If PcdCpuS3DataAddress was never set, then do not copy CPU S3 Data into SMRAM
684 AcpiCpuData
= (ACPI_CPU_DATA
*)(UINTN
)PcdGet64 (PcdCpuS3DataAddress
);
685 if (AcpiCpuData
== 0) {
690 // For a native platform, copy the CPU S3 data into SMRAM for use on CPU S3 Resume.
692 CopyMem (&mAcpiCpuData
, AcpiCpuData
, sizeof (mAcpiCpuData
));
694 mAcpiCpuData
.MtrrTable
= (EFI_PHYSICAL_ADDRESS
)(UINTN
)AllocatePool (sizeof (MTRR_SETTINGS
));
695 ASSERT (mAcpiCpuData
.MtrrTable
!= 0);
697 CopyMem ((VOID
*)(UINTN
)mAcpiCpuData
.MtrrTable
, (VOID
*)(UINTN
)AcpiCpuData
->MtrrTable
, sizeof (MTRR_SETTINGS
));
699 mAcpiCpuData
.GdtrProfile
= (EFI_PHYSICAL_ADDRESS
)(UINTN
)AllocatePool (sizeof (IA32_DESCRIPTOR
));
700 ASSERT (mAcpiCpuData
.GdtrProfile
!= 0);
702 CopyMem ((VOID
*)(UINTN
)mAcpiCpuData
.GdtrProfile
, (VOID
*)(UINTN
)AcpiCpuData
->GdtrProfile
, sizeof (IA32_DESCRIPTOR
));
704 mAcpiCpuData
.IdtrProfile
= (EFI_PHYSICAL_ADDRESS
)(UINTN
)AllocatePool (sizeof (IA32_DESCRIPTOR
));
705 ASSERT (mAcpiCpuData
.IdtrProfile
!= 0);
707 CopyMem ((VOID
*)(UINTN
)mAcpiCpuData
.IdtrProfile
, (VOID
*)(UINTN
)AcpiCpuData
->IdtrProfile
, sizeof (IA32_DESCRIPTOR
));
709 mAcpiCpuData
.PreSmmInitRegisterTable
= (EFI_PHYSICAL_ADDRESS
)(UINTN
)AllocatePool (mAcpiCpuData
.NumberOfCpus
* sizeof (CPU_REGISTER_TABLE
));
710 ASSERT (mAcpiCpuData
.PreSmmInitRegisterTable
!= 0);
713 (CPU_REGISTER_TABLE
*)(UINTN
)mAcpiCpuData
.PreSmmInitRegisterTable
,
714 (CPU_REGISTER_TABLE
*)(UINTN
)AcpiCpuData
->PreSmmInitRegisterTable
,
715 mAcpiCpuData
.NumberOfCpus
718 mAcpiCpuData
.RegisterTable
= (EFI_PHYSICAL_ADDRESS
)(UINTN
)AllocatePool (mAcpiCpuData
.NumberOfCpus
* sizeof (CPU_REGISTER_TABLE
));
719 ASSERT (mAcpiCpuData
.RegisterTable
!= 0);
722 (CPU_REGISTER_TABLE
*)(UINTN
)mAcpiCpuData
.RegisterTable
,
723 (CPU_REGISTER_TABLE
*)(UINTN
)AcpiCpuData
->RegisterTable
,
724 mAcpiCpuData
.NumberOfCpus
728 // Copy AP's GDT, IDT and Machine Check handler into SMRAM.
730 Gdtr
= (IA32_DESCRIPTOR
*)(UINTN
)mAcpiCpuData
.GdtrProfile
;
731 Idtr
= (IA32_DESCRIPTOR
*)(UINTN
)mAcpiCpuData
.IdtrProfile
;
733 mGdtForAp
= AllocatePool ((Gdtr
->Limit
+ 1) + (Idtr
->Limit
+ 1) + mAcpiCpuData
.ApMachineCheckHandlerSize
);
734 ASSERT (mGdtForAp
!= NULL
);
735 mIdtForAp
= (VOID
*) ((UINTN
)mGdtForAp
+ (Gdtr
->Limit
+ 1));
736 mMachineCheckHandlerForAp
= (VOID
*) ((UINTN
)mIdtForAp
+ (Idtr
->Limit
+ 1));
738 CopyMem (mGdtForAp
, (VOID
*)Gdtr
->Base
, Gdtr
->Limit
+ 1);
739 CopyMem (mIdtForAp
, (VOID
*)Idtr
->Base
, Idtr
->Limit
+ 1);
740 CopyMem (mMachineCheckHandlerForAp
, (VOID
*)(UINTN
)mAcpiCpuData
.ApMachineCheckHandlerBase
, mAcpiCpuData
.ApMachineCheckHandlerSize
);
744 // Set SMM ready to lock flag and return
746 mSmmReadyToLock
= TRUE
;
751 The module Entry Point of the CPU SMM driver.
753 @param ImageHandle The firmware allocated handle for the EFI image.
754 @param SystemTable A pointer to the EFI System Table.
756 @retval EFI_SUCCESS The entry point is executed successfully.
757 @retval Other Some error occurs when executing this entry point.
763 IN EFI_HANDLE ImageHandle
,
764 IN EFI_SYSTEM_TABLE
*SystemTable
768 EFI_MP_SERVICES_PROTOCOL
*MpServices
;
769 UINTN NumberOfEnabledProcessors
;
777 EFI_SMRAM_DESCRIPTOR
*SmramDescriptor
;
778 SMM_S3_RESUME_STATE
*SmmS3ResumeState
;
788 // Initialize Debug Agent to support source level debug in SMM code
790 InitializeDebugAgent (DEBUG_AGENT_INIT_SMM
, NULL
, NULL
);
793 // Report the start of CPU SMM initialization.
797 EFI_COMPUTING_UNIT_HOST_PROCESSOR
| EFI_CU_HP_PC_SMM_INIT
801 // Fix segment address of the long-mode-switch jump
803 if (sizeof (UINTN
) == sizeof (UINT64
)) {
804 gSmmJmpAddr
.Segment
= LONG_MODE_CODE_SEGMENT
;
808 // Find out SMRR Base and SMRR Size
810 FindSmramInfo (&mCpuHotPlugData
.SmrrBase
, &mCpuHotPlugData
.SmrrSize
);
813 // Get MP Services Protocol
815 Status
= SystemTable
->BootServices
->LocateProtocol (&gEfiMpServiceProtocolGuid
, NULL
, (VOID
**)&MpServices
);
816 ASSERT_EFI_ERROR (Status
);
819 // Use MP Services Protocol to retrieve the number of processors and number of enabled processors
821 Status
= MpServices
->GetNumberOfProcessors (MpServices
, &mNumberOfCpus
, &NumberOfEnabledProcessors
);
822 ASSERT_EFI_ERROR (Status
);
823 ASSERT (mNumberOfCpus
<= PcdGet32 (PcdCpuMaxLogicalProcessorNumber
));
826 // If support CPU hot plug, PcdCpuSmmEnableBspElection should be set to TRUE.
827 // A constant BSP index makes no sense because it may be hot removed.
830 if (FeaturePcdGet (PcdCpuHotPlugSupport
)) {
832 ASSERT (FeaturePcdGet (PcdCpuSmmEnableBspElection
));
837 // Save the PcdCpuSmmCodeAccessCheckEnable value into a global variable.
839 mSmmCodeAccessCheckEnable
= PcdGetBool (PcdCpuSmmCodeAccessCheckEnable
);
840 DEBUG ((EFI_D_INFO
, "PcdCpuSmmCodeAccessCheckEnable = %d\n", mSmmCodeAccessCheckEnable
));
843 // If support CPU hot plug, we need to allocate resources for possibly hot-added processors
845 if (FeaturePcdGet (PcdCpuHotPlugSupport
)) {
846 mMaxNumberOfCpus
= PcdGet32 (PcdCpuMaxLogicalProcessorNumber
);
848 mMaxNumberOfCpus
= mNumberOfCpus
;
850 gSmmCpuPrivate
->SmmCoreEntryContext
.NumberOfCpus
= mMaxNumberOfCpus
;
853 // The CPU save state and code for the SMI entry point are tiled within an SMRAM
854 // allocated buffer. The minimum size of this buffer for a uniprocessor system
855 // is 32 KB, because the entry point is SMBASE + 32KB, and CPU save state area
856 // just below SMBASE + 64KB. If more than one CPU is present in the platform,
857 // then the SMI entry point and the CPU save state areas can be tiles to minimize
858 // the total amount SMRAM required for all the CPUs. The tile size can be computed
859 // by adding the // CPU save state size, any extra CPU specific context, and
860 // the size of code that must be placed at the SMI entry point to transfer
861 // control to a C function in the native SMM execution mode. This size is
862 // rounded up to the nearest power of 2 to give the tile size for a each CPU.
863 // The total amount of memory required is the maximum number of CPUs that
864 // platform supports times the tile size. The picture below shows the tiling,
865 // where m is the number of tiles that fit in 32KB.
867 // +-----------------------------+ <-- 2^n offset from Base of allocated buffer
868 // | CPU m+1 Save State |
869 // +-----------------------------+
870 // | CPU m+1 Extra Data |
871 // +-----------------------------+
873 // +-----------------------------+
874 // | CPU 2m SMI Entry |
875 // +#############################+ <-- Base of allocated buffer + 64 KB
876 // | CPU m-1 Save State |
877 // +-----------------------------+
878 // | CPU m-1 Extra Data |
879 // +-----------------------------+
881 // +-----------------------------+
882 // | CPU 2m-1 SMI Entry |
883 // +=============================+ <-- 2^n offset from Base of allocated buffer
884 // | . . . . . . . . . . . . |
885 // +=============================+ <-- 2^n offset from Base of allocated buffer
886 // | CPU 2 Save State |
887 // +-----------------------------+
888 // | CPU 2 Extra Data |
889 // +-----------------------------+
891 // +-----------------------------+
892 // | CPU m+1 SMI Entry |
893 // +=============================+ <-- Base of allocated buffer + 32 KB
894 // | CPU 1 Save State |
895 // +-----------------------------+
896 // | CPU 1 Extra Data |
897 // +-----------------------------+
899 // +-----------------------------+
900 // | CPU m SMI Entry |
901 // +#############################+ <-- Base of allocated buffer + 32 KB == CPU 0 SMBASE + 64 KB
902 // | CPU 0 Save State |
903 // +-----------------------------+
904 // | CPU 0 Extra Data |
905 // +-----------------------------+
907 // +-----------------------------+
908 // | CPU m-1 SMI Entry |
909 // +=============================+ <-- 2^n offset from Base of allocated buffer
910 // | . . . . . . . . . . . . |
911 // +=============================+ <-- 2^n offset from Base of allocated buffer
913 // +-----------------------------+
914 // | CPU 1 SMI Entry |
915 // +=============================+ <-- 2^n offset from Base of allocated buffer
917 // +-----------------------------+
918 // | CPU 0 SMI Entry |
919 // +#############################+ <-- Base of allocated buffer == CPU 0 SMBASE + 32 KB
923 // Retrieve CPU Family
925 AsmCpuid (CPUID_VERSION_INFO
, &RegEax
, NULL
, NULL
, NULL
);
926 FamilyId
= (RegEax
>> 8) & 0xf;
927 ModelId
= (RegEax
>> 4) & 0xf;
928 if (FamilyId
== 0x06 || FamilyId
== 0x0f) {
929 ModelId
= ModelId
| ((RegEax
>> 12) & 0xf0);
933 AsmCpuid (CPUID_EXTENDED_FUNCTION
, &RegEax
, NULL
, NULL
, NULL
);
934 if (RegEax
>= CPUID_EXTENDED_CPU_SIG
) {
935 AsmCpuid (CPUID_EXTENDED_CPU_SIG
, NULL
, NULL
, NULL
, &RegEdx
);
938 // Determine the mode of the CPU at the time an SMI occurs
939 // Intel(R) 64 and IA-32 Architectures Software Developer's Manual
940 // Volume 3C, Section 34.4.1.1
942 mSmmSaveStateRegisterLma
= EFI_SMM_SAVE_STATE_REGISTER_LMA_32BIT
;
943 if ((RegEdx
& BIT29
) != 0) {
944 mSmmSaveStateRegisterLma
= EFI_SMM_SAVE_STATE_REGISTER_LMA_64BIT
;
946 if (FamilyId
== 0x06) {
947 if (ModelId
== 0x17 || ModelId
== 0x0f || ModelId
== 0x1c) {
948 mSmmSaveStateRegisterLma
= EFI_SMM_SAVE_STATE_REGISTER_LMA_64BIT
;
953 // Compute tile size of buffer required to hold the CPU SMRAM Save State Map, extra CPU
954 // specific context in a PROCESSOR_SMM_DESCRIPTOR, and the SMI entry point. This size
955 // is rounded up to nearest power of 2.
957 TileCodeSize
= GetSmiHandlerSize ();
958 TileCodeSize
= ALIGN_VALUE(TileCodeSize
, SIZE_4KB
);
959 TileDataSize
= sizeof (SMRAM_SAVE_STATE_MAP
) + sizeof (PROCESSOR_SMM_DESCRIPTOR
);
960 TileDataSize
= ALIGN_VALUE(TileDataSize
, SIZE_4KB
);
961 TileSize
= TileDataSize
+ TileCodeSize
- 1;
962 TileSize
= 2 * GetPowerOfTwo32 ((UINT32
)TileSize
);
963 DEBUG ((EFI_D_INFO
, "SMRAM TileSize = 0x%08x (0x%08x, 0x%08x)\n", TileSize
, TileCodeSize
, TileDataSize
));
966 // If the TileSize is larger than space available for the SMI Handler of CPU[i],
967 // the PROCESSOR_SMM_DESCRIPTOR of CPU[i+1] and the SMRAM Save State Map of CPU[i+1],
968 // the ASSERT(). If this ASSERT() is triggered, then the SMI Handler size must be
971 ASSERT (TileSize
<= (SMRAM_SAVE_STATE_MAP_OFFSET
+ sizeof (SMRAM_SAVE_STATE_MAP
) - SMM_HANDLER_OFFSET
));
974 // Allocate buffer for all of the tiles.
976 // Intel(R) 64 and IA-32 Architectures Software Developer's Manual
977 // Volume 3C, Section 34.11 SMBASE Relocation
978 // For Pentium and Intel486 processors, the SMBASE values must be
979 // aligned on a 32-KByte boundary or the processor will enter shutdown
980 // state during the execution of a RSM instruction.
982 // Intel486 processors: FamilyId is 4
983 // Pentium processors : FamilyId is 5
985 BufferPages
= EFI_SIZE_TO_PAGES (SIZE_32KB
+ TileSize
* (mMaxNumberOfCpus
- 1));
986 if ((FamilyId
== 4) || (FamilyId
== 5)) {
987 Buffer
= AllocateAlignedPages (BufferPages
, SIZE_32KB
);
989 Buffer
= AllocateAlignedPages (BufferPages
, SIZE_4KB
);
991 ASSERT (Buffer
!= NULL
);
992 DEBUG ((EFI_D_INFO
, "SMRAM SaveState Buffer (0x%08x, 0x%08x)\n", Buffer
, EFI_PAGES_TO_SIZE(BufferPages
)));
995 // Allocate buffer for pointers to array in SMM_CPU_PRIVATE_DATA.
997 gSmmCpuPrivate
->ProcessorInfo
= (EFI_PROCESSOR_INFORMATION
*)AllocatePool (sizeof (EFI_PROCESSOR_INFORMATION
) * mMaxNumberOfCpus
);
998 ASSERT (gSmmCpuPrivate
->ProcessorInfo
!= NULL
);
1000 gSmmCpuPrivate
->Operation
= (SMM_CPU_OPERATION
*)AllocatePool (sizeof (SMM_CPU_OPERATION
) * mMaxNumberOfCpus
);
1001 ASSERT (gSmmCpuPrivate
->Operation
!= NULL
);
1003 gSmmCpuPrivate
->CpuSaveStateSize
= (UINTN
*)AllocatePool (sizeof (UINTN
) * mMaxNumberOfCpus
);
1004 ASSERT (gSmmCpuPrivate
->CpuSaveStateSize
!= NULL
);
1006 gSmmCpuPrivate
->CpuSaveState
= (VOID
**)AllocatePool (sizeof (VOID
*) * mMaxNumberOfCpus
);
1007 ASSERT (gSmmCpuPrivate
->CpuSaveState
!= NULL
);
1009 mSmmCpuPrivateData
.SmmCoreEntryContext
.CpuSaveStateSize
= gSmmCpuPrivate
->CpuSaveStateSize
;
1010 mSmmCpuPrivateData
.SmmCoreEntryContext
.CpuSaveState
= gSmmCpuPrivate
->CpuSaveState
;
1013 // Allocate buffer for pointers to array in CPU_HOT_PLUG_DATA.
1015 mCpuHotPlugData
.ApicId
= (UINT64
*)AllocatePool (sizeof (UINT64
) * mMaxNumberOfCpus
);
1016 ASSERT (mCpuHotPlugData
.ApicId
!= NULL
);
1017 mCpuHotPlugData
.SmBase
= (UINTN
*)AllocatePool (sizeof (UINTN
) * mMaxNumberOfCpus
);
1018 ASSERT (mCpuHotPlugData
.SmBase
!= NULL
);
1019 mCpuHotPlugData
.ArrayLength
= (UINT32
)mMaxNumberOfCpus
;
1022 // Retrieve APIC ID of each enabled processor from the MP Services protocol.
1023 // Also compute the SMBASE address, CPU Save State address, and CPU Save state
1024 // size for each CPU in the platform
1026 for (Index
= 0; Index
< mMaxNumberOfCpus
; Index
++) {
1027 mCpuHotPlugData
.SmBase
[Index
] = (UINTN
)Buffer
+ Index
* TileSize
- SMM_HANDLER_OFFSET
;
1028 gSmmCpuPrivate
->CpuSaveStateSize
[Index
] = sizeof(SMRAM_SAVE_STATE_MAP
);
1029 gSmmCpuPrivate
->CpuSaveState
[Index
] = (VOID
*)(mCpuHotPlugData
.SmBase
[Index
] + SMRAM_SAVE_STATE_MAP_OFFSET
);
1030 gSmmCpuPrivate
->Operation
[Index
] = SmmCpuNone
;
1032 if (Index
< mNumberOfCpus
) {
1033 Status
= MpServices
->GetProcessorInfo (MpServices
, Index
, &gSmmCpuPrivate
->ProcessorInfo
[Index
]);
1034 ASSERT_EFI_ERROR (Status
);
1035 mCpuHotPlugData
.ApicId
[Index
] = gSmmCpuPrivate
->ProcessorInfo
[Index
].ProcessorId
;
1037 DEBUG ((EFI_D_INFO
, "CPU[%03x] APIC ID=%04x SMBASE=%08x SaveState=%08x Size=%08x\n",
1039 (UINT32
)gSmmCpuPrivate
->ProcessorInfo
[Index
].ProcessorId
,
1040 mCpuHotPlugData
.SmBase
[Index
],
1041 gSmmCpuPrivate
->CpuSaveState
[Index
],
1042 gSmmCpuPrivate
->CpuSaveStateSize
[Index
]
1045 gSmmCpuPrivate
->ProcessorInfo
[Index
].ProcessorId
= INVALID_APIC_ID
;
1046 mCpuHotPlugData
.ApicId
[Index
] = INVALID_APIC_ID
;
1051 // Allocate SMI stacks for all processors.
1053 if (FeaturePcdGet (PcdCpuSmmStackGuard
)) {
1055 // 2 more pages is allocated for each processor.
1056 // one is guard page and the other is known good stack.
1058 // +-------------------------------------------+-----+-------------------------------------------+
1059 // | Known Good Stack | Guard Page | SMM Stack | ... | Known Good Stack | Guard Page | SMM Stack |
1060 // +-------------------------------------------+-----+-------------------------------------------+
1062 // |<-------------- Processor 0 -------------->| |<-------------- Processor n -------------->|
1064 mSmmStackSize
= EFI_PAGES_TO_SIZE (EFI_SIZE_TO_PAGES (PcdGet32 (PcdCpuSmmStackSize
)) + 2);
1065 Stacks
= (UINT8
*) AllocatePages (gSmmCpuPrivate
->SmmCoreEntryContext
.NumberOfCpus
* (EFI_SIZE_TO_PAGES (PcdGet32 (PcdCpuSmmStackSize
)) + 2));
1066 ASSERT (Stacks
!= NULL
);
1067 mSmmStackArrayBase
= (UINTN
)Stacks
;
1068 mSmmStackArrayEnd
= mSmmStackArrayBase
+ gSmmCpuPrivate
->SmmCoreEntryContext
.NumberOfCpus
* mSmmStackSize
- 1;
1070 mSmmStackSize
= PcdGet32 (PcdCpuSmmStackSize
);
1071 Stacks
= (UINT8
*) AllocatePages (EFI_SIZE_TO_PAGES (gSmmCpuPrivate
->SmmCoreEntryContext
.NumberOfCpus
* mSmmStackSize
));
1072 ASSERT (Stacks
!= NULL
);
1076 // Set SMI stack for SMM base relocation
1078 gSmmInitStack
= (UINTN
) (Stacks
+ mSmmStackSize
- sizeof (UINTN
));
1083 InitializeSmmIdt ();
1086 // Relocate SMM Base addresses to the ones allocated from SMRAM
1088 mRebased
= (BOOLEAN
*)AllocateZeroPool (sizeof (BOOLEAN
) * mMaxNumberOfCpus
);
1089 ASSERT (mRebased
!= NULL
);
1090 SmmRelocateBases ();
1093 // Call hook for BSP to perform extra actions in normal mode after all
1094 // SMM base addresses have been relocated on all CPUs
1096 SmmCpuFeaturesSmmRelocationComplete ();
1099 // SMM Time initialization
1101 InitializeSmmTimer ();
1104 // Initialize MP globals
1106 Cr3
= InitializeMpServiceData (Stacks
, mSmmStackSize
);
1109 // Fill in SMM Reserved Regions
1111 gSmmCpuPrivate
->SmmReservedSmramRegion
[0].SmramReservedStart
= 0;
1112 gSmmCpuPrivate
->SmmReservedSmramRegion
[0].SmramReservedSize
= 0;
1115 // Install the SMM Configuration Protocol onto a new handle on the handle database.
1116 // The entire SMM Configuration Protocol is allocated from SMRAM, so only a pointer
1117 // to an SMRAM address will be present in the handle database
1119 Status
= SystemTable
->BootServices
->InstallMultipleProtocolInterfaces (
1120 &gSmmCpuPrivate
->SmmCpuHandle
,
1121 &gEfiSmmConfigurationProtocolGuid
, &gSmmCpuPrivate
->SmmConfiguration
,
1124 ASSERT_EFI_ERROR (Status
);
1127 // Install the SMM CPU Protocol into SMM protocol database
1129 Status
= gSmst
->SmmInstallProtocolInterface (
1131 &gEfiSmmCpuProtocolGuid
,
1132 EFI_NATIVE_INTERFACE
,
1135 ASSERT_EFI_ERROR (Status
);
1138 // Expose address of CPU Hot Plug Data structure if CPU hot plug is supported.
1140 if (FeaturePcdGet (PcdCpuHotPlugSupport
)) {
1141 Status
= PcdSet64S (PcdCpuHotPlugDataAddress
, (UINT64
)(UINTN
)&mCpuHotPlugData
);
1142 ASSERT_EFI_ERROR (Status
);
1146 // Initialize SMM CPU Services Support
1148 Status
= InitializeSmmCpuServices (mSmmCpuHandle
);
1149 ASSERT_EFI_ERROR (Status
);
1152 // register SMM Ready To Lock Protocol notification
1154 Status
= gSmst
->SmmRegisterProtocolNotify (
1155 &gEfiSmmReadyToLockProtocolGuid
,
1156 SmmReadyToLockEventNotify
,
1159 ASSERT_EFI_ERROR (Status
);
1161 GuidHob
= GetFirstGuidHob (&gEfiAcpiVariableGuid
);
1162 if (GuidHob
!= NULL
) {
1163 SmramDescriptor
= (EFI_SMRAM_DESCRIPTOR
*) GET_GUID_HOB_DATA (GuidHob
);
1165 DEBUG ((EFI_D_INFO
, "SMM S3 SMRAM Structure = %x\n", SmramDescriptor
));
1166 DEBUG ((EFI_D_INFO
, "SMM S3 Structure = %x\n", SmramDescriptor
->CpuStart
));
1168 SmmS3ResumeState
= (SMM_S3_RESUME_STATE
*)(UINTN
)SmramDescriptor
->CpuStart
;
1169 ZeroMem (SmmS3ResumeState
, sizeof (SMM_S3_RESUME_STATE
));
1171 mSmmS3ResumeState
= SmmS3ResumeState
;
1172 SmmS3ResumeState
->Smst
= (EFI_PHYSICAL_ADDRESS
)(UINTN
)gSmst
;
1174 SmmS3ResumeState
->SmmS3ResumeEntryPoint
= (EFI_PHYSICAL_ADDRESS
)(UINTN
)SmmRestoreCpu
;
1176 SmmS3ResumeState
->SmmS3StackSize
= SIZE_32KB
;
1177 SmmS3ResumeState
->SmmS3StackBase
= (EFI_PHYSICAL_ADDRESS
)(UINTN
)AllocatePages (EFI_SIZE_TO_PAGES ((UINTN
)SmmS3ResumeState
->SmmS3StackSize
));
1178 if (SmmS3ResumeState
->SmmS3StackBase
== 0) {
1179 SmmS3ResumeState
->SmmS3StackSize
= 0;
1182 SmmS3ResumeState
->SmmS3Cr0
= gSmmCr0
;
1183 SmmS3ResumeState
->SmmS3Cr3
= Cr3
;
1184 SmmS3ResumeState
->SmmS3Cr4
= gSmmCr4
;
1186 if (sizeof (UINTN
) == sizeof (UINT64
)) {
1187 SmmS3ResumeState
->Signature
= SMM_S3_RESUME_SMM_64
;
1189 if (sizeof (UINTN
) == sizeof (UINT32
)) {
1190 SmmS3ResumeState
->Signature
= SMM_S3_RESUME_SMM_32
;
1195 // Check XD and BTS features
1197 CheckProcessorFeature ();
1200 // Initialize SMM Profile feature
1202 InitSmmProfile (Cr3
);
1205 // Patch SmmS3ResumeState->SmmS3Cr3
1209 DEBUG ((EFI_D_INFO
, "SMM CPU Module exit from SMRAM with EFI_SUCCESS\n"));
1216 Find out SMRAM information including SMRR base and SMRR size.
1218 @param SmrrBase SMRR base
1219 @param SmrrSize SMRR size
1224 OUT UINT32
*SmrrBase
,
1225 OUT UINT32
*SmrrSize
1230 EFI_SMM_ACCESS2_PROTOCOL
*SmmAccess
;
1231 EFI_SMRAM_DESCRIPTOR
*CurrentSmramRange
;
1232 EFI_SMRAM_DESCRIPTOR
*SmramRanges
;
1233 UINTN SmramRangeCount
;
1239 // Get SMM Access Protocol
1241 Status
= gBS
->LocateProtocol (&gEfiSmmAccess2ProtocolGuid
, NULL
, (VOID
**)&SmmAccess
);
1242 ASSERT_EFI_ERROR (Status
);
1245 // Get SMRAM information
1248 Status
= SmmAccess
->GetCapabilities (SmmAccess
, &Size
, NULL
);
1249 ASSERT (Status
== EFI_BUFFER_TOO_SMALL
);
1251 SmramRanges
= (EFI_SMRAM_DESCRIPTOR
*)AllocatePool (Size
);
1252 ASSERT (SmramRanges
!= NULL
);
1254 Status
= SmmAccess
->GetCapabilities (SmmAccess
, &Size
, SmramRanges
);
1255 ASSERT_EFI_ERROR (Status
);
1257 SmramRangeCount
= Size
/ sizeof (EFI_SMRAM_DESCRIPTOR
);
1260 // Find the largest SMRAM range between 1MB and 4GB that is at least 256K - 4K in size
1262 CurrentSmramRange
= NULL
;
1263 for (Index
= 0, MaxSize
= SIZE_256KB
- EFI_PAGE_SIZE
; Index
< SmramRangeCount
; Index
++) {
1265 // Skip any SMRAM region that is already allocated, needs testing, or needs ECC initialization
1267 if ((SmramRanges
[Index
].RegionState
& (EFI_ALLOCATED
| EFI_NEEDS_TESTING
| EFI_NEEDS_ECC_INITIALIZATION
)) != 0) {
1271 if (SmramRanges
[Index
].CpuStart
>= BASE_1MB
) {
1272 if ((SmramRanges
[Index
].CpuStart
+ SmramRanges
[Index
].PhysicalSize
) <= BASE_4GB
) {
1273 if (SmramRanges
[Index
].PhysicalSize
>= MaxSize
) {
1274 MaxSize
= SmramRanges
[Index
].PhysicalSize
;
1275 CurrentSmramRange
= &SmramRanges
[Index
];
1281 ASSERT (CurrentSmramRange
!= NULL
);
1283 *SmrrBase
= (UINT32
)CurrentSmramRange
->CpuStart
;
1284 *SmrrSize
= (UINT32
)CurrentSmramRange
->PhysicalSize
;
1288 for (Index
= 0; Index
< SmramRangeCount
; Index
++) {
1289 if (SmramRanges
[Index
].CpuStart
< *SmrrBase
&& *SmrrBase
== (SmramRanges
[Index
].CpuStart
+ SmramRanges
[Index
].PhysicalSize
)) {
1290 *SmrrBase
= (UINT32
)SmramRanges
[Index
].CpuStart
;
1291 *SmrrSize
= (UINT32
)(*SmrrSize
+ SmramRanges
[Index
].PhysicalSize
);
1293 } else if ((*SmrrBase
+ *SmrrSize
) == SmramRanges
[Index
].CpuStart
&& SmramRanges
[Index
].PhysicalSize
> 0) {
1294 *SmrrSize
= (UINT32
)(*SmrrSize
+ SmramRanges
[Index
].PhysicalSize
);
1300 DEBUG ((EFI_D_INFO
, "SMRR Base: 0x%x, SMRR Size: 0x%x\n", *SmrrBase
, *SmrrSize
));
1304 Configure SMM Code Access Check feature on an AP.
1305 SMM Feature Control MSR will be locked after configuration.
1307 @param[in,out] Buffer Pointer to private data buffer.
1311 ConfigSmmCodeAccessCheckOnCurrentProcessor (
1316 UINT64 SmmFeatureControlMsr
;
1317 UINT64 NewSmmFeatureControlMsr
;
1320 // Retrieve the CPU Index from the context passed in
1322 CpuIndex
= *(UINTN
*)Buffer
;
1325 // Get the current SMM Feature Control MSR value
1327 SmmFeatureControlMsr
= SmmCpuFeaturesGetSmmRegister (CpuIndex
, SmmRegFeatureControl
);
1330 // Compute the new SMM Feature Control MSR value
1332 NewSmmFeatureControlMsr
= SmmFeatureControlMsr
;
1333 if (mSmmCodeAccessCheckEnable
) {
1334 NewSmmFeatureControlMsr
|= SMM_CODE_CHK_EN_BIT
;
1335 if (FeaturePcdGet (PcdCpuSmmFeatureControlMsrLock
)) {
1336 NewSmmFeatureControlMsr
|= SMM_FEATURE_CONTROL_LOCK_BIT
;
1341 // Only set the SMM Feature Control MSR value if the new value is different than the current value
1343 if (NewSmmFeatureControlMsr
!= SmmFeatureControlMsr
) {
1344 SmmCpuFeaturesSetSmmRegister (CpuIndex
, SmmRegFeatureControl
, NewSmmFeatureControlMsr
);
1348 // Release the spin lock user to serialize the updates to the SMM Feature Control MSR
1350 ReleaseSpinLock (mConfigSmmCodeAccessCheckLock
);
1354 Configure SMM Code Access Check feature for all processors.
1355 SMM Feature Control MSR will be locked after configuration.
1358 ConfigSmmCodeAccessCheck (
1366 // Check to see if the Feature Control MSR is supported on this CPU
1368 Index
= gSmmCpuPrivate
->SmmCoreEntryContext
.CurrentlyExecutingCpu
;
1369 if (!SmmCpuFeaturesIsSmmRegisterSupported (Index
, SmmRegFeatureControl
)) {
1370 mSmmCodeAccessCheckEnable
= FALSE
;
1375 // Check to see if the CPU supports the SMM Code Access Check feature
1376 // Do not access this MSR unless the CPU supports the SmmRegFeatureControl
1378 if ((AsmReadMsr64 (EFI_MSR_SMM_MCA_CAP
) & SMM_CODE_ACCESS_CHK_BIT
) == 0) {
1379 mSmmCodeAccessCheckEnable
= FALSE
;
1384 // Initialize the lock used to serialize the MSR programming in BSP and all APs
1386 InitializeSpinLock (mConfigSmmCodeAccessCheckLock
);
1389 // Acquire Config SMM Code Access Check spin lock. The BSP will release the
1390 // spin lock when it is done executing ConfigSmmCodeAccessCheckOnCurrentProcessor().
1392 AcquireSpinLock (mConfigSmmCodeAccessCheckLock
);
1395 // Enable SMM Code Access Check feature on the BSP.
1397 ConfigSmmCodeAccessCheckOnCurrentProcessor (&Index
);
1400 // Enable SMM Code Access Check feature for the APs.
1402 for (Index
= 0; Index
< gSmst
->NumberOfCpus
; Index
++) {
1403 if (Index
!= gSmmCpuPrivate
->SmmCoreEntryContext
.CurrentlyExecutingCpu
) {
1406 // Acquire Config SMM Code Access Check spin lock. The AP will release the
1407 // spin lock when it is done executing ConfigSmmCodeAccessCheckOnCurrentProcessor().
1409 AcquireSpinLock (mConfigSmmCodeAccessCheckLock
);
1412 // Call SmmStartupThisAp() to enable SMM Code Access Check on an AP.
1414 Status
= gSmst
->SmmStartupThisAp (ConfigSmmCodeAccessCheckOnCurrentProcessor
, Index
, &Index
);
1415 ASSERT_EFI_ERROR (Status
);
1418 // Wait for the AP to release the Config SMM Code Access Check spin lock.
1420 while (!AcquireSpinLockOrFail (mConfigSmmCodeAccessCheckLock
)) {
1425 // Release the Config SMM Code Access Check spin lock.
1427 ReleaseSpinLock (mConfigSmmCodeAccessCheckLock
);
1433 This API provides a way to allocate memory for page table.
1435 This API can be called more once to allocate memory for page tables.
1437 Allocates the number of 4KB pages of type EfiRuntimeServicesData and returns a pointer to the
1438 allocated buffer. The buffer returned is aligned on a 4KB boundary. If Pages is 0, then NULL
1439 is returned. If there is not enough memory remaining to satisfy the request, then NULL is
1442 @param Pages The number of 4 KB pages to allocate.
1444 @return A pointer to the allocated buffer or NULL if allocation fails.
1448 AllocatePageTableMemory (
1454 Buffer
= SmmCpuFeaturesAllocatePageTableMemory (Pages
);
1455 if (Buffer
!= NULL
) {
1458 return AllocatePages (Pages
);
1462 Perform the remaining tasks.
1466 PerformRemainingTasks (
1470 if (mSmmReadyToLock
) {
1472 // Start SMM Profile feature
1474 if (FeaturePcdGet (PcdCpuSmmProfileEnable
)) {
1478 // Create a mix of 2MB and 4KB page table. Update some memory ranges absent and execute-disable.
1482 // Configure SMM Code Access Check feature if available.
1484 ConfigSmmCodeAccessCheck ();
1486 SmmCpuFeaturesCompleteSmmReadyToLock ();
1489 // Clean SMM ready to lock flag
1491 mSmmReadyToLock
= FALSE
;
1496 Perform the pre tasks.
1505 // Restore SMM Configuration in S3 boot path.
1507 if (mRestoreSmmConfigurationInS3
) {
1509 // Need make sure gSmst is correct because below function may use them.
1511 gSmst
->SmmStartupThisAp
= gSmmCpuPrivate
->SmmCoreEntryContext
.SmmStartupThisAp
;
1512 gSmst
->CurrentlyExecutingCpu
= gSmmCpuPrivate
->SmmCoreEntryContext
.CurrentlyExecutingCpu
;
1513 gSmst
->NumberOfCpus
= gSmmCpuPrivate
->SmmCoreEntryContext
.NumberOfCpus
;
1514 gSmst
->CpuSaveStateSize
= gSmmCpuPrivate
->SmmCoreEntryContext
.CpuSaveStateSize
;
1515 gSmst
->CpuSaveState
= gSmmCpuPrivate
->SmmCoreEntryContext
.CpuSaveState
;
1518 // Configure SMM Code Access Check feature if available.
1520 ConfigSmmCodeAccessCheck ();
1522 SmmCpuFeaturesCompleteSmmReadyToLock ();
1524 mRestoreSmmConfigurationInS3
= FALSE
;