2 SMM MP service implementation
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 #include "PiSmmCpuDxeSmm.h"
14 // Slots for all MTRR( FIXED MTRR + VARIABLE MTRR + MTRR_LIB_IA32_MTRR_DEF_TYPE)
16 MTRR_SETTINGS gSmiMtrrs
;
18 SMM_DISPATCHER_MP_SYNC_DATA
*mSmmMpSyncData
= NULL
;
19 UINTN mSmmMpSyncDataSize
;
20 SMM_CPU_SEMAPHORES mSmmCpuSemaphores
;
22 SPIN_LOCK
*mPFLock
= NULL
;
23 SMM_CPU_SYNC_MODE mCpuSmmSyncMode
;
24 BOOLEAN mMachineCheckSupported
= FALSE
;
27 Performs an atomic compare exchange operation to get semaphore.
28 The compare exchange operation must be performed using
31 @param Sem IN: 32-bit unsigned integer
32 OUT: original integer - 1
33 @return Original integer - 1
38 IN OUT
volatile UINT32
*Sem
45 } while (Value
== 0 ||
46 InterlockedCompareExchange32 (
56 Performs an atomic compare exchange operation to release semaphore.
57 The compare exchange operation must be performed using
60 @param Sem IN: 32-bit unsigned integer
61 OUT: original integer + 1
62 @return Original integer + 1
67 IN OUT
volatile UINT32
*Sem
74 } while (Value
+ 1 != 0 &&
75 InterlockedCompareExchange32 (
84 Performs an atomic compare exchange operation to lock semaphore.
85 The compare exchange operation must be performed using
88 @param Sem IN: 32-bit unsigned integer
90 @return Original integer
95 IN OUT
volatile UINT32
*Sem
102 } while (InterlockedCompareExchange32 (
110 Wait all APs to performs an atomic compare exchange operation to release semaphore.
112 @param NumberOfAPs AP number
122 BspIndex
= mSmmMpSyncData
->BspIndex
;
123 while (NumberOfAPs
-- > 0) {
124 WaitForSemaphore (mSmmMpSyncData
->CpuData
[BspIndex
].Run
);
129 Performs an atomic compare exchange operation to release semaphore
140 for (Index
= mMaxNumberOfCpus
; Index
-- > 0;) {
141 if (IsPresentAp (Index
)) {
142 ReleaseSemaphore (mSmmMpSyncData
->CpuData
[Index
].Run
);
148 Checks if all CPUs (with certain exceptions) have checked in for this SMI run
150 @param Exceptions CPU Arrival exception flags.
152 @retval TRUE if all CPUs the have checked in.
153 @retval FALSE if at least one Normal AP hasn't checked in.
157 AllCpusInSmmWithExceptions (
158 SMM_CPU_ARRIVAL_EXCEPTIONS Exceptions
162 SMM_CPU_DATA_BLOCK
*CpuData
;
163 EFI_PROCESSOR_INFORMATION
*ProcessorInfo
;
165 ASSERT (*mSmmMpSyncData
->Counter
<= mNumberOfCpus
);
167 if (*mSmmMpSyncData
->Counter
== mNumberOfCpus
) {
171 CpuData
= mSmmMpSyncData
->CpuData
;
172 ProcessorInfo
= gSmmCpuPrivate
->ProcessorInfo
;
173 for (Index
= mMaxNumberOfCpus
; Index
-- > 0;) {
174 if (!(*(CpuData
[Index
].Present
)) && ProcessorInfo
[Index
].ProcessorId
!= INVALID_APIC_ID
) {
175 if (((Exceptions
& ARRIVAL_EXCEPTION_DELAYED
) != 0) && SmmCpuFeaturesGetSmmRegister (Index
, SmmRegSmmDelayed
) != 0) {
178 if (((Exceptions
& ARRIVAL_EXCEPTION_BLOCKED
) != 0) && SmmCpuFeaturesGetSmmRegister (Index
, SmmRegSmmBlocked
) != 0) {
181 if (((Exceptions
& ARRIVAL_EXCEPTION_SMI_DISABLED
) != 0) && SmmCpuFeaturesGetSmmRegister (Index
, SmmRegSmmEnable
) != 0) {
193 Has OS enabled Lmce in the MSR_IA32_MCG_EXT_CTL
195 @retval TRUE Os enable lmce.
196 @retval FALSE Os not enable lmce.
204 MSR_IA32_MCG_CAP_REGISTER McgCap
;
205 MSR_IA32_FEATURE_CONTROL_REGISTER FeatureCtrl
;
206 MSR_IA32_MCG_EXT_CTL_REGISTER McgExtCtrl
;
208 McgCap
.Uint64
= AsmReadMsr64 (MSR_IA32_MCG_CAP
);
209 if (McgCap
.Bits
.MCG_LMCE_P
== 0) {
213 FeatureCtrl
.Uint64
= AsmReadMsr64 (MSR_IA32_FEATURE_CONTROL
);
214 if (FeatureCtrl
.Bits
.LmceOn
== 0) {
218 McgExtCtrl
.Uint64
= AsmReadMsr64 (MSR_IA32_MCG_EXT_CTL
);
219 return (BOOLEAN
) (McgExtCtrl
.Bits
.LMCE_EN
== 1);
223 Return if Local machine check exception signaled.
225 Indicates (when set) that a local machine check exception was generated. This indicates that the current machine-check event was
226 delivered to only the logical processor.
228 @retval TRUE LMCE was signaled.
229 @retval FALSE LMCE was not signaled.
237 MSR_IA32_MCG_STATUS_REGISTER McgStatus
;
239 McgStatus
.Uint64
= AsmReadMsr64 (MSR_IA32_MCG_STATUS
);
240 return (BOOLEAN
) (McgStatus
.Bits
.LMCE_S
== 1);
244 Given timeout constraint, wait for all APs to arrive, and insure when this function returns, no AP will execute normal mode code before
245 entering SMM, except SMI disabled APs.
249 SmmWaitForApArrival (
258 ASSERT (*mSmmMpSyncData
->Counter
<= mNumberOfCpus
);
262 if (mMachineCheckSupported
) {
263 LmceEn
= IsLmceOsEnabled ();
264 LmceSignal
= IsLmceSignaled();
268 // Platform implementor should choose a timeout value appropriately:
269 // - The timeout value should balance the SMM time constrains and the likelihood that delayed CPUs are excluded in the SMM run. Note
270 // the SMI Handlers must ALWAYS take into account the cases that not all APs are available in an SMI run.
271 // - The timeout value must, in the case of 2nd timeout, be at least long enough to give time for all APs to receive the SMI IPI
272 // and either enter SMM or buffer the SMI, to insure there is no CPU running normal mode code when SMI handling starts. This will
273 // be TRUE even if a blocked CPU is brought out of the blocked state by a normal mode CPU (before the normal mode CPU received the
274 // SMI IPI), because with a buffered SMI, and CPU will enter SMM immediately after it is brought out of the blocked state.
275 // - The timeout value must be longer than longest possible IO operation in the system
279 // Sync with APs 1st timeout
281 for (Timer
= StartSyncTimer ();
282 !IsSyncTimerTimeout (Timer
) && !(LmceEn
&& LmceSignal
) &&
283 !AllCpusInSmmWithExceptions (ARRIVAL_EXCEPTION_BLOCKED
| ARRIVAL_EXCEPTION_SMI_DISABLED
);
289 // Not all APs have arrived, so we need 2nd round of timeout. IPIs should be sent to ALL none present APs,
291 // a) Delayed AP may have just come out of the delayed state. Blocked AP may have just been brought out of blocked state by some AP running
292 // normal mode code. These APs need to be guaranteed to have an SMI pending to insure that once they are out of delayed / blocked state, they
293 // enter SMI immediately without executing instructions in normal mode. Note traditional flow requires there are no APs doing normal mode
294 // work while SMI handling is on-going.
295 // b) As a consequence of SMI IPI sending, (spurious) SMI may occur after this SMM run.
296 // c) ** NOTE **: Use SMI disabling feature VERY CAREFULLY (if at all) for traditional flow, because a processor in SMI-disabled state
297 // will execute normal mode code, which breaks the traditional SMI handlers' assumption that no APs are doing normal
298 // mode work while SMI handling is on-going.
299 // d) We don't add code to check SMI disabling status to skip sending IPI to SMI disabled APs, because:
300 // - In traditional flow, SMI disabling is discouraged.
301 // - In relaxed flow, CheckApArrival() will check SMI disabling status before calling this function.
302 // In both cases, adding SMI-disabling checking code increases overhead.
304 if (*mSmmMpSyncData
->Counter
< mNumberOfCpus
) {
306 // Send SMI IPIs to bring outside processors in
308 for (Index
= mMaxNumberOfCpus
; Index
-- > 0;) {
309 if (!(*(mSmmMpSyncData
->CpuData
[Index
].Present
)) && gSmmCpuPrivate
->ProcessorInfo
[Index
].ProcessorId
!= INVALID_APIC_ID
) {
310 SendSmiIpi ((UINT32
)gSmmCpuPrivate
->ProcessorInfo
[Index
].ProcessorId
);
315 // Sync with APs 2nd timeout.
317 for (Timer
= StartSyncTimer ();
318 !IsSyncTimerTimeout (Timer
) &&
319 !AllCpusInSmmWithExceptions (ARRIVAL_EXCEPTION_BLOCKED
| ARRIVAL_EXCEPTION_SMI_DISABLED
);
330 Replace OS MTRR's with SMI MTRR's.
332 @param CpuIndex Processor Index
340 SmmCpuFeaturesDisableSmrr ();
343 // Replace all MTRRs registers
345 MtrrSetAllMtrrs (&gSmiMtrrs
);
349 Wheck whether task has been finished by all APs.
351 @param BlockMode Whether did it in block mode or non-block mode.
353 @retval TRUE Task has been finished by all APs.
354 @retval FALSE Task not has been finished by all APs.
358 WaitForAllAPsNotBusy (
364 for (Index
= mMaxNumberOfCpus
; Index
-- > 0;) {
366 // Ignore BSP and APs which not call in SMM.
368 if (!IsPresentAp(Index
)) {
373 AcquireSpinLock(mSmmMpSyncData
->CpuData
[Index
].Busy
);
374 ReleaseSpinLock(mSmmMpSyncData
->CpuData
[Index
].Busy
);
376 if (AcquireSpinLockOrFail (mSmmMpSyncData
->CpuData
[Index
].Busy
)) {
377 ReleaseSpinLock(mSmmMpSyncData
->CpuData
[Index
].Busy
);
388 Check whether it is an present AP.
390 @param CpuIndex The AP index which calls this function.
392 @retval TRUE It's a present AP.
393 @retval TRUE This is not an AP or it is not present.
401 return ((CpuIndex
!= gSmmCpuPrivate
->SmmCoreEntryContext
.CurrentlyExecutingCpu
) &&
402 *(mSmmMpSyncData
->CpuData
[CpuIndex
].Present
));
406 Check whether execute in single AP or all APs.
408 Compare two Tokens used by different APs to know whether in StartAllAps call.
410 Whether is an valid AP base on AP's Present flag.
412 @retval TRUE IN StartAllAps call.
413 @retval FALSE Not in StartAllAps call.
424 for (ApIndex
= mMaxNumberOfCpus
; ApIndex
-- > 0;) {
425 if (IsPresentAp (ApIndex
) && (mSmmMpSyncData
->CpuData
[ApIndex
].Token
!= NULL
)) {
426 for (ApIndex2
= ApIndex
; ApIndex2
-- > 0;) {
427 if (IsPresentAp (ApIndex2
) && (mSmmMpSyncData
->CpuData
[ApIndex2
].Token
!= NULL
)) {
428 return mSmmMpSyncData
->CpuData
[ApIndex2
].Token
== mSmmMpSyncData
->CpuData
[ApIndex
].Token
;
438 Clean up the status flags used during executing the procedure.
440 @param CpuIndex The AP index which calls this function.
451 if (InStartAllApsCall ()) {
453 // In Start All APs mode, make sure all APs have finished task.
455 if (WaitForAllAPsNotBusy (FALSE
)) {
457 // Clean the flags update in the function call.
460 for (Index
= mMaxNumberOfCpus
; Index
-- > 0;) {
462 // Only In SMM APs need to be clean up.
464 if (mSmmMpSyncData
->CpuData
[Index
].Present
&& mSmmMpSyncData
->CpuData
[Index
].Token
!= NULL
) {
466 ReleaseSpinLock (mSmmMpSyncData
->CpuData
[Index
].Token
);
469 mSmmMpSyncData
->CpuData
[Index
].Token
= NULL
;
475 // In single AP mode.
477 if (mSmmMpSyncData
->CpuData
[CpuIndex
].Token
!= NULL
) {
478 ReleaseSpinLock (mSmmMpSyncData
->CpuData
[CpuIndex
].Token
);
479 mSmmMpSyncData
->CpuData
[CpuIndex
].Token
= NULL
;
485 Free the tokens in the maintained list.
494 PROCEDURE_TOKEN
*ProcToken
;
496 while (!IsListEmpty (&gSmmCpuPrivate
->TokenList
)) {
497 Link
= GetFirstNode (&gSmmCpuPrivate
->TokenList
);
498 ProcToken
= PROCEDURE_TOKEN_FROM_LINK (Link
);
500 RemoveEntryList (&ProcToken
->Link
);
502 FreePool ((VOID
*)ProcToken
->ProcedureToken
);
503 FreePool (ProcToken
);
510 @param CpuIndex BSP processor Index
511 @param SyncMode SMM MP sync mode
517 IN SMM_CPU_SYNC_MODE SyncMode
523 BOOLEAN ClearTopLevelSmiResult
;
526 ASSERT (CpuIndex
== mSmmMpSyncData
->BspIndex
);
530 // Flag BSP's presence
532 *mSmmMpSyncData
->InsideSmm
= TRUE
;
535 // Initialize Debug Agent to start source level debug in BSP handler
537 InitializeDebugAgent (DEBUG_AGENT_INIT_ENTER_SMI
, NULL
, NULL
);
540 // Mark this processor's presence
542 *(mSmmMpSyncData
->CpuData
[CpuIndex
].Present
) = TRUE
;
545 // Clear platform top level SMI status bit before calling SMI handlers. If
546 // we cleared it after SMI handlers are run, we would miss the SMI that
547 // occurs after SMI handlers are done and before SMI status bit is cleared.
549 ClearTopLevelSmiResult
= ClearTopLevelSmiStatus();
550 ASSERT (ClearTopLevelSmiResult
== TRUE
);
553 // Set running processor index
555 gSmmCpuPrivate
->SmmCoreEntryContext
.CurrentlyExecutingCpu
= CpuIndex
;
558 // If Traditional Sync Mode or need to configure MTRRs: gather all available APs.
560 if (SyncMode
== SmmCpuSyncModeTradition
|| SmmCpuFeaturesNeedConfigureMtrrs()) {
563 // Wait for APs to arrive
565 SmmWaitForApArrival();
568 // Lock the counter down and retrieve the number of APs
570 *mSmmMpSyncData
->AllCpusInSync
= TRUE
;
571 ApCount
= LockdownSemaphore (mSmmMpSyncData
->Counter
) - 1;
574 // Wait for all APs to get ready for programming MTRRs
576 WaitForAllAPs (ApCount
);
578 if (SmmCpuFeaturesNeedConfigureMtrrs()) {
580 // Signal all APs it's time for backup MTRRs
585 // WaitForSemaphore() may wait for ever if an AP happens to enter SMM at
586 // exactly this point. Please make sure PcdCpuSmmMaxSyncLoops has been set
587 // to a large enough value to avoid this situation.
588 // Note: For HT capable CPUs, threads within a core share the same set of MTRRs.
589 // We do the backup first and then set MTRR to avoid race condition for threads
592 MtrrGetAllMtrrs(&Mtrrs
);
595 // Wait for all APs to complete their MTRR saving
597 WaitForAllAPs (ApCount
);
600 // Let all processors program SMM MTRRs together
605 // WaitForSemaphore() may wait for ever if an AP happens to enter SMM at
606 // exactly this point. Please make sure PcdCpuSmmMaxSyncLoops has been set
607 // to a large enough value to avoid this situation.
609 ReplaceOSMtrrs (CpuIndex
);
612 // Wait for all APs to complete their MTRR programming
614 WaitForAllAPs (ApCount
);
619 // The BUSY lock is initialized to Acquired state
621 AcquireSpinLock (mSmmMpSyncData
->CpuData
[CpuIndex
].Busy
);
624 // Perform the pre tasks
629 // Invoke SMM Foundation EntryPoint with the processor information context.
631 gSmmCpuPrivate
->SmmCoreEntry (&gSmmCpuPrivate
->SmmCoreEntryContext
);
634 // Make sure all APs have completed their pending none-block tasks
636 WaitForAllAPsNotBusy (TRUE
);
639 // Perform the remaining tasks
641 PerformRemainingTasks ();
644 // If Relaxed-AP Sync Mode: gather all available APs after BSP SMM handlers are done, and
645 // make those APs to exit SMI synchronously. APs which arrive later will be excluded and
646 // will run through freely.
648 if (SyncMode
!= SmmCpuSyncModeTradition
&& !SmmCpuFeaturesNeedConfigureMtrrs()) {
651 // Lock the counter down and retrieve the number of APs
653 *mSmmMpSyncData
->AllCpusInSync
= TRUE
;
654 ApCount
= LockdownSemaphore (mSmmMpSyncData
->Counter
) - 1;
656 // Make sure all APs have their Present flag set
660 for (Index
= mMaxNumberOfCpus
; Index
-- > 0;) {
661 if (*(mSmmMpSyncData
->CpuData
[Index
].Present
)) {
665 if (PresentCount
> ApCount
) {
672 // Notify all APs to exit
674 *mSmmMpSyncData
->InsideSmm
= FALSE
;
678 // Wait for all APs to complete their pending tasks
680 WaitForAllAPs (ApCount
);
682 if (SmmCpuFeaturesNeedConfigureMtrrs()) {
684 // Signal APs to restore MTRRs
691 SmmCpuFeaturesReenableSmrr ();
692 MtrrSetAllMtrrs(&Mtrrs
);
695 // Wait for all APs to complete MTRR programming
697 WaitForAllAPs (ApCount
);
701 // Stop source level debug in BSP handler, the code below will not be
704 InitializeDebugAgent (DEBUG_AGENT_INIT_EXIT_SMI
, NULL
, NULL
);
707 // Signal APs to Reset states/semaphore for this processor
712 // Perform pending operations for hot-plug
717 // Clear the Present flag of BSP
719 *(mSmmMpSyncData
->CpuData
[CpuIndex
].Present
) = FALSE
;
722 // Gather APs to exit SMM synchronously. Note the Present flag is cleared by now but
723 // WaitForAllAps does not depend on the Present flag.
725 WaitForAllAPs (ApCount
);
728 // Clean the tokens buffer.
733 // Reset BspIndex to -1, meaning BSP has not been elected.
735 if (FeaturePcdGet (PcdCpuSmmEnableBspElection
)) {
736 mSmmMpSyncData
->BspIndex
= (UINT32
)-1;
740 // Allow APs to check in from this point on
742 *mSmmMpSyncData
->Counter
= 0;
743 *mSmmMpSyncData
->AllCpusInSync
= FALSE
;
749 @param CpuIndex AP processor Index.
750 @param ValidSmi Indicates that current SMI is a valid SMI or not.
751 @param SyncMode SMM MP sync mode.
758 IN SMM_CPU_SYNC_MODE SyncMode
764 EFI_STATUS ProcedureStatus
;
769 for (Timer
= StartSyncTimer ();
770 !IsSyncTimerTimeout (Timer
) &&
771 !(*mSmmMpSyncData
->InsideSmm
);
776 if (!(*mSmmMpSyncData
->InsideSmm
)) {
778 // BSP timeout in the first round
780 if (mSmmMpSyncData
->BspIndex
!= -1) {
782 // BSP Index is known
784 BspIndex
= mSmmMpSyncData
->BspIndex
;
785 ASSERT (CpuIndex
!= BspIndex
);
788 // Send SMI IPI to bring BSP in
790 SendSmiIpi ((UINT32
)gSmmCpuPrivate
->ProcessorInfo
[BspIndex
].ProcessorId
);
793 // Now clock BSP for the 2nd time
795 for (Timer
= StartSyncTimer ();
796 !IsSyncTimerTimeout (Timer
) &&
797 !(*mSmmMpSyncData
->InsideSmm
);
802 if (!(*mSmmMpSyncData
->InsideSmm
)) {
804 // Give up since BSP is unable to enter SMM
805 // and signal the completion of this AP
806 WaitForSemaphore (mSmmMpSyncData
->Counter
);
811 // Don't know BSP index. Give up without sending IPI to BSP.
813 WaitForSemaphore (mSmmMpSyncData
->Counter
);
821 BspIndex
= mSmmMpSyncData
->BspIndex
;
822 ASSERT (CpuIndex
!= BspIndex
);
825 // Mark this processor's presence
827 *(mSmmMpSyncData
->CpuData
[CpuIndex
].Present
) = TRUE
;
829 if (SyncMode
== SmmCpuSyncModeTradition
|| SmmCpuFeaturesNeedConfigureMtrrs()) {
831 // Notify BSP of arrival at this point
833 ReleaseSemaphore (mSmmMpSyncData
->CpuData
[BspIndex
].Run
);
836 if (SmmCpuFeaturesNeedConfigureMtrrs()) {
838 // Wait for the signal from BSP to backup MTRRs
840 WaitForSemaphore (mSmmMpSyncData
->CpuData
[CpuIndex
].Run
);
845 MtrrGetAllMtrrs(&Mtrrs
);
848 // Signal BSP the completion of this AP
850 ReleaseSemaphore (mSmmMpSyncData
->CpuData
[BspIndex
].Run
);
853 // Wait for BSP's signal to program MTRRs
855 WaitForSemaphore (mSmmMpSyncData
->CpuData
[CpuIndex
].Run
);
858 // Replace OS MTRRs with SMI MTRRs
860 ReplaceOSMtrrs (CpuIndex
);
863 // Signal BSP the completion of this AP
865 ReleaseSemaphore (mSmmMpSyncData
->CpuData
[BspIndex
].Run
);
870 // Wait for something to happen
872 WaitForSemaphore (mSmmMpSyncData
->CpuData
[CpuIndex
].Run
);
875 // Check if BSP wants to exit SMM
877 if (!(*mSmmMpSyncData
->InsideSmm
)) {
882 // BUSY should be acquired by SmmStartupThisAp()
885 !AcquireSpinLockOrFail (mSmmMpSyncData
->CpuData
[CpuIndex
].Busy
)
889 // Invoke the scheduled procedure
891 ProcedureStatus
= (*mSmmMpSyncData
->CpuData
[CpuIndex
].Procedure
) (
892 (VOID
*)mSmmMpSyncData
->CpuData
[CpuIndex
].Parameter
894 if (mSmmMpSyncData
->CpuData
[CpuIndex
].Status
!= NULL
) {
895 *mSmmMpSyncData
->CpuData
[CpuIndex
].Status
= ProcedureStatus
;
901 ReleaseSpinLock (mSmmMpSyncData
->CpuData
[CpuIndex
].Busy
);
903 ReleaseToken (CpuIndex
);
906 if (SmmCpuFeaturesNeedConfigureMtrrs()) {
908 // Notify BSP the readiness of this AP to program MTRRs
910 ReleaseSemaphore (mSmmMpSyncData
->CpuData
[BspIndex
].Run
);
913 // Wait for the signal from BSP to program MTRRs
915 WaitForSemaphore (mSmmMpSyncData
->CpuData
[CpuIndex
].Run
);
920 SmmCpuFeaturesReenableSmrr ();
921 MtrrSetAllMtrrs(&Mtrrs
);
925 // Notify BSP the readiness of this AP to Reset states/semaphore for this processor
927 ReleaseSemaphore (mSmmMpSyncData
->CpuData
[BspIndex
].Run
);
930 // Wait for the signal from BSP to Reset states/semaphore for this processor
932 WaitForSemaphore (mSmmMpSyncData
->CpuData
[CpuIndex
].Run
);
935 // Reset states/semaphore for this processor
937 *(mSmmMpSyncData
->CpuData
[CpuIndex
].Present
) = FALSE
;
940 // Notify BSP the readiness of this AP to exit SMM
942 ReleaseSemaphore (mSmmMpSyncData
->CpuData
[BspIndex
].Run
);
947 Create 4G PageTable in SMRAM.
949 @param[in] Is32BitPageTable Whether the page table is 32-bit PAE
950 @return PageTable Address
955 IN BOOLEAN Is32BitPageTable
963 UINTN High2MBoundary
;
973 if (FeaturePcdGet (PcdCpuSmmStackGuard
)) {
975 // Add one more page for known good stack, then find the lower 2MB aligned address.
977 Low2MBoundary
= (mSmmStackArrayBase
+ EFI_PAGE_SIZE
) & ~(SIZE_2MB
-1);
979 // Add two more pages for known good stack and stack guard page,
980 // then find the lower 2MB aligned address.
982 High2MBoundary
= (mSmmStackArrayEnd
- mSmmStackSize
+ EFI_PAGE_SIZE
* 2) & ~(SIZE_2MB
-1);
983 PagesNeeded
= ((High2MBoundary
- Low2MBoundary
) / SIZE_2MB
) + 1;
986 // Allocate the page table
988 PageTable
= AllocatePageTableMemory (5 + PagesNeeded
);
989 ASSERT (PageTable
!= NULL
);
991 PageTable
= (VOID
*)((UINTN
)PageTable
);
992 Pte
= (UINT64
*)PageTable
;
995 // Zero out all page table entries first
997 ZeroMem (Pte
, EFI_PAGES_TO_SIZE (1));
1000 // Set Page Directory Pointers
1002 for (Index
= 0; Index
< 4; Index
++) {
1003 Pte
[Index
] = ((UINTN
)PageTable
+ EFI_PAGE_SIZE
* (Index
+ 1)) | mAddressEncMask
|
1004 (Is32BitPageTable
? IA32_PAE_PDPTE_ATTRIBUTE_BITS
: PAGE_ATTRIBUTE_BITS
);
1006 Pte
+= EFI_PAGE_SIZE
/ sizeof (*Pte
);
1009 // Fill in Page Directory Entries
1011 for (Index
= 0; Index
< EFI_PAGE_SIZE
* 4 / sizeof (*Pte
); Index
++) {
1012 Pte
[Index
] = (Index
<< 21) | mAddressEncMask
| IA32_PG_PS
| PAGE_ATTRIBUTE_BITS
;
1015 Pdpte
= (UINT64
*)PageTable
;
1016 if (FeaturePcdGet (PcdCpuSmmStackGuard
)) {
1017 Pages
= (UINTN
)PageTable
+ EFI_PAGES_TO_SIZE (5);
1018 GuardPage
= mSmmStackArrayBase
+ EFI_PAGE_SIZE
;
1019 for (PageIndex
= Low2MBoundary
; PageIndex
<= High2MBoundary
; PageIndex
+= SIZE_2MB
) {
1020 Pte
= (UINT64
*)(UINTN
)(Pdpte
[BitFieldRead32 ((UINT32
)PageIndex
, 30, 31)] & ~mAddressEncMask
& ~(EFI_PAGE_SIZE
- 1));
1021 Pte
[BitFieldRead32 ((UINT32
)PageIndex
, 21, 29)] = (UINT64
)Pages
| mAddressEncMask
| PAGE_ATTRIBUTE_BITS
;
1023 // Fill in Page Table Entries
1025 Pte
= (UINT64
*)Pages
;
1026 PageAddress
= PageIndex
;
1027 for (Index
= 0; Index
< EFI_PAGE_SIZE
/ sizeof (*Pte
); Index
++) {
1028 if (PageAddress
== GuardPage
) {
1030 // Mark the guard page as non-present
1032 Pte
[Index
] = PageAddress
| mAddressEncMask
;
1033 GuardPage
+= mSmmStackSize
;
1034 if (GuardPage
> mSmmStackArrayEnd
) {
1038 Pte
[Index
] = PageAddress
| mAddressEncMask
| PAGE_ATTRIBUTE_BITS
;
1040 PageAddress
+= EFI_PAGE_SIZE
;
1042 Pages
+= EFI_PAGE_SIZE
;
1046 if ((PcdGet8 (PcdNullPointerDetectionPropertyMask
) & BIT1
) != 0) {
1047 Pte
= (UINT64
*)(UINTN
)(Pdpte
[0] & ~mAddressEncMask
& ~(EFI_PAGE_SIZE
- 1));
1048 if ((Pte
[0] & IA32_PG_PS
) == 0) {
1049 // 4K-page entries are already mapped. Just hide the first one anyway.
1050 Pte
= (UINT64
*)(UINTN
)(Pte
[0] & ~mAddressEncMask
& ~(EFI_PAGE_SIZE
- 1));
1051 Pte
[0] &= ~(UINT64
)IA32_PG_P
; // Hide page 0
1053 // Create 4K-page entries
1054 Pages
= (UINTN
)AllocatePageTableMemory (1);
1055 ASSERT (Pages
!= 0);
1057 Pte
[0] = (UINT64
)(Pages
| mAddressEncMask
| PAGE_ATTRIBUTE_BITS
);
1059 Pte
= (UINT64
*)Pages
;
1061 Pte
[0] = PageAddress
| mAddressEncMask
; // Hide page 0 but present left
1062 for (Index
= 1; Index
< EFI_PAGE_SIZE
/ sizeof (*Pte
); Index
++) {
1063 PageAddress
+= EFI_PAGE_SIZE
;
1064 Pte
[Index
] = PageAddress
| mAddressEncMask
| PAGE_ATTRIBUTE_BITS
;
1069 return (UINT32
)(UINTN
)PageTable
;
1073 Checks whether the input token is the current used token.
1075 @param[in] Token This parameter describes the token that was passed into DispatchProcedure or
1078 @retval TRUE The input token is the current used token.
1079 @retval FALSE The input token is not the current used token.
1087 PROCEDURE_TOKEN
*ProcToken
;
1089 if (Token
== NULL
) {
1093 Link
= GetFirstNode (&gSmmCpuPrivate
->TokenList
);
1094 while (!IsNull (&gSmmCpuPrivate
->TokenList
, Link
)) {
1095 ProcToken
= PROCEDURE_TOKEN_FROM_LINK (Link
);
1097 if (ProcToken
->ProcedureToken
== Token
) {
1101 Link
= GetNextNode (&gSmmCpuPrivate
->TokenList
, Link
);
1108 create token and save it to the maintain list.
1110 @retval return the spin lock used as token.
1118 PROCEDURE_TOKEN
*ProcToken
;
1119 SPIN_LOCK
*CpuToken
;
1122 SpinLockSize
= GetSpinLockProperties ();
1123 CpuToken
= AllocatePool (SpinLockSize
);
1124 ASSERT (CpuToken
!= NULL
);
1125 InitializeSpinLock (CpuToken
);
1126 AcquireSpinLock (CpuToken
);
1128 ProcToken
= AllocatePool (sizeof (PROCEDURE_TOKEN
));
1129 ASSERT (ProcToken
!= NULL
);
1130 ProcToken
->Signature
= PROCEDURE_TOKEN_SIGNATURE
;
1131 ProcToken
->ProcedureToken
= CpuToken
;
1133 InsertTailList (&gSmmCpuPrivate
->TokenList
, &ProcToken
->Link
);
1139 Checks status of specified AP.
1141 This function checks whether the specified AP has finished the task assigned
1142 by StartupThisAP(), and whether timeout expires.
1144 @param[in] Token This parameter describes the token that was passed into DispatchProcedure or
1147 @retval EFI_SUCCESS Specified AP has finished task assigned by StartupThisAPs().
1148 @retval EFI_NOT_READY Specified AP has not finished task and timeout has not expired.
1155 if (AcquireSpinLockOrFail (Token
)) {
1156 ReleaseSpinLock (Token
);
1160 return EFI_NOT_READY
;
1164 Schedule a procedure to run on the specified CPU.
1166 @param[in] Procedure The address of the procedure to run
1167 @param[in] CpuIndex Target CPU Index
1168 @param[in,out] ProcArguments The parameter to pass to the procedure
1169 @param[in] Token This is an optional parameter that allows the caller to execute the
1170 procedure in a blocking or non-blocking fashion. If it is NULL the
1171 call is blocking, and the call will not return until the AP has
1172 completed the procedure. If the token is not NULL, the call will
1173 return immediately. The caller can check whether the procedure has
1174 completed with CheckOnProcedure or WaitForProcedure.
1175 @param[in] TimeoutInMicroseconds Indicates the time limit in microseconds for the APs to finish
1176 execution of Procedure, either for blocking or non-blocking mode.
1177 Zero means infinity. If the timeout expires before all APs return
1178 from Procedure, then Procedure on the failed APs is terminated. If
1179 the timeout expires in blocking mode, the call returns EFI_TIMEOUT.
1180 If the timeout expires in non-blocking mode, the timeout determined
1181 can be through CheckOnProcedure or WaitForProcedure.
1182 Note that timeout support is optional. Whether an implementation
1183 supports this feature can be determined via the Attributes data
1185 @param[in,out] CpuStatus This optional pointer may be used to get the status code returned
1186 by Procedure when it completes execution on the target AP, or with
1187 EFI_TIMEOUT if the Procedure fails to complete within the optional
1188 timeout. The implementation will update this variable with
1189 EFI_NOT_READY prior to starting Procedure on the target AP.
1191 @retval EFI_INVALID_PARAMETER CpuNumber not valid
1192 @retval EFI_INVALID_PARAMETER CpuNumber specifying BSP
1193 @retval EFI_INVALID_PARAMETER The AP specified by CpuNumber did not enter SMM
1194 @retval EFI_INVALID_PARAMETER The AP specified by CpuNumber is busy
1195 @retval EFI_SUCCESS The procedure has been successfully scheduled
1199 InternalSmmStartupThisAp (
1200 IN EFI_AP_PROCEDURE2 Procedure
,
1202 IN OUT VOID
*ProcArguments OPTIONAL
,
1203 IN MM_COMPLETION
*Token
,
1204 IN UINTN TimeoutInMicroseconds
,
1205 IN OUT EFI_STATUS
*CpuStatus
1208 if (CpuIndex
>= gSmmCpuPrivate
->SmmCoreEntryContext
.NumberOfCpus
) {
1209 DEBUG((DEBUG_ERROR
, "CpuIndex(%d) >= gSmmCpuPrivate->SmmCoreEntryContext.NumberOfCpus(%d)\n", CpuIndex
, gSmmCpuPrivate
->SmmCoreEntryContext
.NumberOfCpus
));
1210 return EFI_INVALID_PARAMETER
;
1212 if (CpuIndex
== gSmmCpuPrivate
->SmmCoreEntryContext
.CurrentlyExecutingCpu
) {
1213 DEBUG((DEBUG_ERROR
, "CpuIndex(%d) == gSmmCpuPrivate->SmmCoreEntryContext.CurrentlyExecutingCpu\n", CpuIndex
));
1214 return EFI_INVALID_PARAMETER
;
1216 if (gSmmCpuPrivate
->ProcessorInfo
[CpuIndex
].ProcessorId
== INVALID_APIC_ID
) {
1217 return EFI_INVALID_PARAMETER
;
1219 if (!(*(mSmmMpSyncData
->CpuData
[CpuIndex
].Present
))) {
1220 if (mSmmMpSyncData
->EffectiveSyncMode
== SmmCpuSyncModeTradition
) {
1221 DEBUG((DEBUG_ERROR
, "!mSmmMpSyncData->CpuData[%d].Present\n", CpuIndex
));
1223 return EFI_INVALID_PARAMETER
;
1225 if (gSmmCpuPrivate
->Operation
[CpuIndex
] == SmmCpuRemove
) {
1226 if (!FeaturePcdGet (PcdCpuHotPlugSupport
)) {
1227 DEBUG((DEBUG_ERROR
, "gSmmCpuPrivate->Operation[%d] == SmmCpuRemove\n", CpuIndex
));
1229 return EFI_INVALID_PARAMETER
;
1231 if ((TimeoutInMicroseconds
!= 0) && ((mSmmMp
.Attributes
& EFI_MM_MP_TIMEOUT_SUPPORTED
) == 0)) {
1232 return EFI_INVALID_PARAMETER
;
1234 if (Procedure
== NULL
) {
1235 return EFI_INVALID_PARAMETER
;
1238 if (Token
== NULL
) {
1239 AcquireSpinLock (mSmmMpSyncData
->CpuData
[CpuIndex
].Busy
);
1241 if (!AcquireSpinLockOrFail (mSmmMpSyncData
->CpuData
[CpuIndex
].Busy
)) {
1242 DEBUG((DEBUG_ERROR
, "Can't acquire mSmmMpSyncData->CpuData[%d].Busy\n", CpuIndex
));
1243 return EFI_NOT_READY
;
1246 *Token
= (MM_COMPLETION
) CreateToken ();
1249 mSmmMpSyncData
->CpuData
[CpuIndex
].Procedure
= Procedure
;
1250 mSmmMpSyncData
->CpuData
[CpuIndex
].Parameter
= ProcArguments
;
1251 if (Token
!= NULL
) {
1252 mSmmMpSyncData
->CpuData
[CpuIndex
].Token
= (SPIN_LOCK
*)(*Token
);
1254 mSmmMpSyncData
->CpuData
[CpuIndex
].Status
= CpuStatus
;
1255 if (mSmmMpSyncData
->CpuData
[CpuIndex
].Status
!= NULL
) {
1256 *mSmmMpSyncData
->CpuData
[CpuIndex
].Status
= EFI_NOT_READY
;
1259 ReleaseSemaphore (mSmmMpSyncData
->CpuData
[CpuIndex
].Run
);
1261 if (Token
== NULL
) {
1262 AcquireSpinLock (mSmmMpSyncData
->CpuData
[CpuIndex
].Busy
);
1263 ReleaseSpinLock (mSmmMpSyncData
->CpuData
[CpuIndex
].Busy
);
1270 Worker function to execute a caller provided function on all enabled APs.
1272 @param[in] Procedure A pointer to the function to be run on
1273 enabled APs of the system.
1274 @param[in] TimeoutInMicroseconds Indicates the time limit in microseconds for
1275 APs to return from Procedure, either for
1276 blocking or non-blocking mode.
1277 @param[in,out] ProcedureArguments The parameter passed into Procedure for
1279 @param[in,out] Token This is an optional parameter that allows the caller to execute the
1280 procedure in a blocking or non-blocking fashion. If it is NULL the
1281 call is blocking, and the call will not return until the AP has
1282 completed the procedure. If the token is not NULL, the call will
1283 return immediately. The caller can check whether the procedure has
1284 completed with CheckOnProcedure or WaitForProcedure.
1285 @param[in,out] CPUStatus This optional pointer may be used to get the status code returned
1286 by Procedure when it completes execution on the target AP, or with
1287 EFI_TIMEOUT if the Procedure fails to complete within the optional
1288 timeout. The implementation will update this variable with
1289 EFI_NOT_READY prior to starting Procedure on the target AP.
1292 @retval EFI_SUCCESS In blocking mode, all APs have finished before
1293 the timeout expired.
1294 @retval EFI_SUCCESS In non-blocking mode, function has been dispatched
1296 @retval others Failed to Startup all APs.
1300 InternalSmmStartupAllAPs (
1301 IN EFI_AP_PROCEDURE2 Procedure
,
1302 IN UINTN TimeoutInMicroseconds
,
1303 IN OUT VOID
*ProcedureArguments OPTIONAL
,
1304 IN OUT MM_COMPLETION
*Token
,
1305 IN OUT EFI_STATUS
*CPUStatus
1311 if ((TimeoutInMicroseconds
!= 0) && ((mSmmMp
.Attributes
& EFI_MM_MP_TIMEOUT_SUPPORTED
) == 0)) {
1312 return EFI_INVALID_PARAMETER
;
1314 if (Procedure
== NULL
) {
1315 return EFI_INVALID_PARAMETER
;
1319 for (Index
= mMaxNumberOfCpus
; Index
-- > 0;) {
1320 if (IsPresentAp (Index
)) {
1323 if (gSmmCpuPrivate
->Operation
[Index
] == SmmCpuRemove
) {
1324 return EFI_INVALID_PARAMETER
;
1327 if (!AcquireSpinLockOrFail(mSmmMpSyncData
->CpuData
[Index
].Busy
)) {
1328 return EFI_NOT_READY
;
1330 ReleaseSpinLock (mSmmMpSyncData
->CpuData
[Index
].Busy
);
1333 if (CpuCount
== 0) {
1334 return EFI_NOT_STARTED
;
1337 if (Token
!= NULL
) {
1338 *Token
= (MM_COMPLETION
) CreateToken ();
1342 // Make sure all BUSY should be acquired.
1344 // Because former code already check mSmmMpSyncData->CpuData[***].Busy for each AP.
1345 // Here code always use AcquireSpinLock instead of AcquireSpinLockOrFail for not
1348 for (Index
= mMaxNumberOfCpus
; Index
-- > 0;) {
1349 if (IsPresentAp (Index
)) {
1350 AcquireSpinLock (mSmmMpSyncData
->CpuData
[Index
].Busy
);
1354 for (Index
= mMaxNumberOfCpus
; Index
-- > 0;) {
1355 if (IsPresentAp (Index
)) {
1356 mSmmMpSyncData
->CpuData
[Index
].Procedure
= (EFI_AP_PROCEDURE2
) Procedure
;
1357 mSmmMpSyncData
->CpuData
[Index
].Parameter
= ProcedureArguments
;
1358 if (Token
!= NULL
) {
1359 mSmmMpSyncData
->CpuData
[Index
].Token
= (SPIN_LOCK
*)(*Token
);
1361 if (CPUStatus
!= NULL
) {
1362 mSmmMpSyncData
->CpuData
[Index
].Status
= &CPUStatus
[Index
];
1363 if (mSmmMpSyncData
->CpuData
[Index
].Status
!= NULL
) {
1364 *mSmmMpSyncData
->CpuData
[Index
].Status
= EFI_NOT_READY
;
1369 // PI spec requirement:
1370 // For every excluded processor, the array entry must contain a value of EFI_NOT_STARTED.
1372 if (CPUStatus
!= NULL
) {
1373 CPUStatus
[Index
] = EFI_NOT_STARTED
;
1380 if (Token
== NULL
) {
1382 // Make sure all APs have completed their tasks.
1384 WaitForAllAPsNotBusy (TRUE
);
1391 ISO C99 6.5.2.2 "Function calls", paragraph 9:
1392 If the function is defined with a type that is not compatible with
1393 the type (of the expression) pointed to by the expression that
1394 denotes the called function, the behavior is undefined.
1396 So add below wrapper function to convert between EFI_AP_PROCEDURE
1397 and EFI_AP_PROCEDURE2.
1399 Wrapper for Procedures.
1401 @param[in] Buffer Pointer to PROCEDURE_WRAPPER buffer.
1410 PROCEDURE_WRAPPER
*Wrapper
;
1413 Wrapper
->Procedure (Wrapper
->ProcedureArgument
);
1419 Schedule a procedure to run on the specified CPU in blocking mode.
1421 @param[in] Procedure The address of the procedure to run
1422 @param[in] CpuIndex Target CPU Index
1423 @param[in, out] ProcArguments The parameter to pass to the procedure
1425 @retval EFI_INVALID_PARAMETER CpuNumber not valid
1426 @retval EFI_INVALID_PARAMETER CpuNumber specifying BSP
1427 @retval EFI_INVALID_PARAMETER The AP specified by CpuNumber did not enter SMM
1428 @retval EFI_INVALID_PARAMETER The AP specified by CpuNumber is busy
1429 @retval EFI_SUCCESS The procedure has been successfully scheduled
1434 SmmBlockingStartupThisAp (
1435 IN EFI_AP_PROCEDURE Procedure
,
1437 IN OUT VOID
*ProcArguments OPTIONAL
1440 PROCEDURE_WRAPPER Wrapper
;
1442 Wrapper
.Procedure
= Procedure
;
1443 Wrapper
.ProcedureArgument
= ProcArguments
;
1446 // Use wrapper function to convert EFI_AP_PROCEDURE to EFI_AP_PROCEDURE2.
1448 return InternalSmmStartupThisAp (ProcedureWrapper
, CpuIndex
, &Wrapper
, NULL
, 0, NULL
);
1452 Schedule a procedure to run on the specified CPU.
1454 @param Procedure The address of the procedure to run
1455 @param CpuIndex Target CPU Index
1456 @param ProcArguments The parameter to pass to the procedure
1458 @retval EFI_INVALID_PARAMETER CpuNumber not valid
1459 @retval EFI_INVALID_PARAMETER CpuNumber specifying BSP
1460 @retval EFI_INVALID_PARAMETER The AP specified by CpuNumber did not enter SMM
1461 @retval EFI_INVALID_PARAMETER The AP specified by CpuNumber is busy
1462 @retval EFI_SUCCESS The procedure has been successfully scheduled
1468 IN EFI_AP_PROCEDURE Procedure
,
1470 IN OUT VOID
*ProcArguments OPTIONAL
1473 MM_COMPLETION Token
;
1475 gSmmCpuPrivate
->ApWrapperFunc
[CpuIndex
].Procedure
= Procedure
;
1476 gSmmCpuPrivate
->ApWrapperFunc
[CpuIndex
].ProcedureArgument
= ProcArguments
;
1479 // Use wrapper function to convert EFI_AP_PROCEDURE to EFI_AP_PROCEDURE2.
1481 return InternalSmmStartupThisAp (
1484 &gSmmCpuPrivate
->ApWrapperFunc
[CpuIndex
],
1485 FeaturePcdGet (PcdCpuSmmBlockStartupThisAp
) ? NULL
: &Token
,
1492 This function sets DR6 & DR7 according to SMM save state, before running SMM C code.
1493 They are useful when you want to enable hardware breakpoints in SMM without entry SMM mode.
1495 NOTE: It might not be appreciated in runtime since it might
1496 conflict with OS debugging facilities. Turn them off in RELEASE.
1498 @param CpuIndex CPU Index
1507 SMRAM_SAVE_STATE_MAP
*CpuSaveState
;
1509 if (FeaturePcdGet (PcdCpuSmmDebug
)) {
1510 ASSERT(CpuIndex
< mMaxNumberOfCpus
);
1511 CpuSaveState
= (SMRAM_SAVE_STATE_MAP
*)gSmmCpuPrivate
->CpuSaveState
[CpuIndex
];
1512 if (mSmmSaveStateRegisterLma
== EFI_SMM_SAVE_STATE_REGISTER_LMA_32BIT
) {
1513 AsmWriteDr6 (CpuSaveState
->x86
._DR6
);
1514 AsmWriteDr7 (CpuSaveState
->x86
._DR7
);
1516 AsmWriteDr6 ((UINTN
)CpuSaveState
->x64
._DR6
);
1517 AsmWriteDr7 ((UINTN
)CpuSaveState
->x64
._DR7
);
1523 This function restores DR6 & DR7 to SMM save state.
1525 NOTE: It might not be appreciated in runtime since it might
1526 conflict with OS debugging facilities. Turn them off in RELEASE.
1528 @param CpuIndex CPU Index
1537 SMRAM_SAVE_STATE_MAP
*CpuSaveState
;
1539 if (FeaturePcdGet (PcdCpuSmmDebug
)) {
1540 ASSERT(CpuIndex
< mMaxNumberOfCpus
);
1541 CpuSaveState
= (SMRAM_SAVE_STATE_MAP
*)gSmmCpuPrivate
->CpuSaveState
[CpuIndex
];
1542 if (mSmmSaveStateRegisterLma
== EFI_SMM_SAVE_STATE_REGISTER_LMA_32BIT
) {
1543 CpuSaveState
->x86
._DR7
= (UINT32
)AsmReadDr7 ();
1544 CpuSaveState
->x86
._DR6
= (UINT32
)AsmReadDr6 ();
1546 CpuSaveState
->x64
._DR7
= AsmReadDr7 ();
1547 CpuSaveState
->x64
._DR6
= AsmReadDr6 ();
1553 C function for SMI entry, each processor comes here upon SMI trigger.
1555 @param CpuIndex CPU Index
1567 BOOLEAN BspInProgress
;
1571 ASSERT(CpuIndex
< mMaxNumberOfCpus
);
1574 // Save Cr2 because Page Fault exception in SMM may override its value,
1575 // when using on-demand paging for above 4G memory.
1581 // Call the user register Startup function first.
1583 if (mSmmMpSyncData
->StartupProcedure
!= NULL
) {
1584 mSmmMpSyncData
->StartupProcedure (mSmmMpSyncData
->StartupProcArgs
);
1588 // Perform CPU specific entry hooks
1590 SmmCpuFeaturesRendezvousEntry (CpuIndex
);
1593 // Determine if this is a valid SMI
1595 ValidSmi
= PlatformValidSmi();
1598 // Determine if BSP has been already in progress. Note this must be checked after
1599 // ValidSmi because BSP may clear a valid SMI source after checking in.
1601 BspInProgress
= *mSmmMpSyncData
->InsideSmm
;
1603 if (!BspInProgress
&& !ValidSmi
) {
1605 // If we reach here, it means when we sampled the ValidSmi flag, SMI status had not
1606 // been cleared by BSP in a new SMI run (so we have a truly invalid SMI), or SMI
1607 // status had been cleared by BSP and an existing SMI run has almost ended. (Note
1608 // we sampled ValidSmi flag BEFORE judging BSP-in-progress status.) In both cases, there
1609 // is nothing we need to do.
1614 // Signal presence of this processor
1616 if (ReleaseSemaphore (mSmmMpSyncData
->Counter
) == 0) {
1618 // BSP has already ended the synchronization, so QUIT!!!
1622 // Wait for BSP's signal to finish SMI
1624 while (*mSmmMpSyncData
->AllCpusInSync
) {
1631 // The BUSY lock is initialized to Released state.
1632 // This needs to be done early enough to be ready for BSP's SmmStartupThisAp() call.
1633 // E.g., with Relaxed AP flow, SmmStartupThisAp() may be called immediately
1634 // after AP's present flag is detected.
1636 InitializeSpinLock (mSmmMpSyncData
->CpuData
[CpuIndex
].Busy
);
1639 if (FeaturePcdGet (PcdCpuSmmProfileEnable
)) {
1640 ActivateSmmProfile (CpuIndex
);
1643 if (BspInProgress
) {
1645 // BSP has been elected. Follow AP path, regardless of ValidSmi flag
1646 // as BSP may have cleared the SMI status
1648 APHandler (CpuIndex
, ValidSmi
, mSmmMpSyncData
->EffectiveSyncMode
);
1651 // We have a valid SMI
1658 if (FeaturePcdGet (PcdCpuSmmEnableBspElection
)) {
1659 if (!mSmmMpSyncData
->SwitchBsp
|| mSmmMpSyncData
->CandidateBsp
[CpuIndex
]) {
1661 // Call platform hook to do BSP election
1663 Status
= PlatformSmmBspElection (&IsBsp
);
1664 if (EFI_SUCCESS
== Status
) {
1666 // Platform hook determines successfully
1669 mSmmMpSyncData
->BspIndex
= (UINT32
)CpuIndex
;
1673 // Platform hook fails to determine, use default BSP election method
1675 InterlockedCompareExchange32 (
1676 (UINT32
*)&mSmmMpSyncData
->BspIndex
,
1685 // "mSmmMpSyncData->BspIndex == CpuIndex" means this is the BSP
1687 if (mSmmMpSyncData
->BspIndex
== CpuIndex
) {
1690 // Clear last request for SwitchBsp.
1692 if (mSmmMpSyncData
->SwitchBsp
) {
1693 mSmmMpSyncData
->SwitchBsp
= FALSE
;
1694 for (Index
= 0; Index
< mMaxNumberOfCpus
; Index
++) {
1695 mSmmMpSyncData
->CandidateBsp
[Index
] = FALSE
;
1699 if (FeaturePcdGet (PcdCpuSmmProfileEnable
)) {
1700 SmmProfileRecordSmiNum ();
1704 // BSP Handler is always called with a ValidSmi == TRUE
1706 BSPHandler (CpuIndex
, mSmmMpSyncData
->EffectiveSyncMode
);
1708 APHandler (CpuIndex
, ValidSmi
, mSmmMpSyncData
->EffectiveSyncMode
);
1712 ASSERT (*mSmmMpSyncData
->CpuData
[CpuIndex
].Run
== 0);
1715 // Wait for BSP's signal to exit SMI
1717 while (*mSmmMpSyncData
->AllCpusInSync
) {
1723 SmmCpuFeaturesRendezvousExit (CpuIndex
);
1732 Allocate buffer for SpinLock and Wrapper function buffer.
1736 InitializeDataForMmMp (
1740 gSmmCpuPrivate
->ApWrapperFunc
= AllocatePool (sizeof (PROCEDURE_WRAPPER
) * gSmmCpuPrivate
->SmmCoreEntryContext
.NumberOfCpus
);
1741 ASSERT (gSmmCpuPrivate
->ApWrapperFunc
!= NULL
);
1743 InitializeListHead (&gSmmCpuPrivate
->TokenList
);
1747 Allocate buffer for all semaphores and spin locks.
1751 InitializeSmmCpuSemaphores (
1755 UINTN ProcessorCount
;
1757 UINTN GlobalSemaphoresSize
;
1758 UINTN CpuSemaphoresSize
;
1759 UINTN SemaphoreSize
;
1761 UINTN
*SemaphoreBlock
;
1762 UINTN SemaphoreAddr
;
1764 SemaphoreSize
= GetSpinLockProperties ();
1765 ProcessorCount
= gSmmCpuPrivate
->SmmCoreEntryContext
.NumberOfCpus
;
1766 GlobalSemaphoresSize
= (sizeof (SMM_CPU_SEMAPHORE_GLOBAL
) / sizeof (VOID
*)) * SemaphoreSize
;
1767 CpuSemaphoresSize
= (sizeof (SMM_CPU_SEMAPHORE_CPU
) / sizeof (VOID
*)) * ProcessorCount
* SemaphoreSize
;
1768 TotalSize
= GlobalSemaphoresSize
+ CpuSemaphoresSize
;
1769 DEBUG((EFI_D_INFO
, "One Semaphore Size = 0x%x\n", SemaphoreSize
));
1770 DEBUG((EFI_D_INFO
, "Total Semaphores Size = 0x%x\n", TotalSize
));
1771 Pages
= EFI_SIZE_TO_PAGES (TotalSize
);
1772 SemaphoreBlock
= AllocatePages (Pages
);
1773 ASSERT (SemaphoreBlock
!= NULL
);
1774 ZeroMem (SemaphoreBlock
, TotalSize
);
1776 SemaphoreAddr
= (UINTN
)SemaphoreBlock
;
1777 mSmmCpuSemaphores
.SemaphoreGlobal
.Counter
= (UINT32
*)SemaphoreAddr
;
1778 SemaphoreAddr
+= SemaphoreSize
;
1779 mSmmCpuSemaphores
.SemaphoreGlobal
.InsideSmm
= (BOOLEAN
*)SemaphoreAddr
;
1780 SemaphoreAddr
+= SemaphoreSize
;
1781 mSmmCpuSemaphores
.SemaphoreGlobal
.AllCpusInSync
= (BOOLEAN
*)SemaphoreAddr
;
1782 SemaphoreAddr
+= SemaphoreSize
;
1783 mSmmCpuSemaphores
.SemaphoreGlobal
.PFLock
= (SPIN_LOCK
*)SemaphoreAddr
;
1784 SemaphoreAddr
+= SemaphoreSize
;
1785 mSmmCpuSemaphores
.SemaphoreGlobal
.CodeAccessCheckLock
1786 = (SPIN_LOCK
*)SemaphoreAddr
;
1787 SemaphoreAddr
+= SemaphoreSize
;
1789 SemaphoreAddr
= (UINTN
)SemaphoreBlock
+ GlobalSemaphoresSize
;
1790 mSmmCpuSemaphores
.SemaphoreCpu
.Busy
= (SPIN_LOCK
*)SemaphoreAddr
;
1791 SemaphoreAddr
+= ProcessorCount
* SemaphoreSize
;
1792 mSmmCpuSemaphores
.SemaphoreCpu
.Run
= (UINT32
*)SemaphoreAddr
;
1793 SemaphoreAddr
+= ProcessorCount
* SemaphoreSize
;
1794 mSmmCpuSemaphores
.SemaphoreCpu
.Present
= (BOOLEAN
*)SemaphoreAddr
;
1796 mPFLock
= mSmmCpuSemaphores
.SemaphoreGlobal
.PFLock
;
1797 mConfigSmmCodeAccessCheckLock
= mSmmCpuSemaphores
.SemaphoreGlobal
.CodeAccessCheckLock
;
1799 mSemaphoreSize
= SemaphoreSize
;
1803 Initialize un-cacheable data.
1808 InitializeMpSyncData (
1814 if (mSmmMpSyncData
!= NULL
) {
1816 // mSmmMpSyncDataSize includes one structure of SMM_DISPATCHER_MP_SYNC_DATA, one
1817 // CpuData array of SMM_CPU_DATA_BLOCK and one CandidateBsp array of BOOLEAN.
1819 ZeroMem (mSmmMpSyncData
, mSmmMpSyncDataSize
);
1820 mSmmMpSyncData
->CpuData
= (SMM_CPU_DATA_BLOCK
*)((UINT8
*)mSmmMpSyncData
+ sizeof (SMM_DISPATCHER_MP_SYNC_DATA
));
1821 mSmmMpSyncData
->CandidateBsp
= (BOOLEAN
*)(mSmmMpSyncData
->CpuData
+ gSmmCpuPrivate
->SmmCoreEntryContext
.NumberOfCpus
);
1822 if (FeaturePcdGet (PcdCpuSmmEnableBspElection
)) {
1824 // Enable BSP election by setting BspIndex to -1
1826 mSmmMpSyncData
->BspIndex
= (UINT32
)-1;
1828 mSmmMpSyncData
->EffectiveSyncMode
= mCpuSmmSyncMode
;
1830 mSmmMpSyncData
->Counter
= mSmmCpuSemaphores
.SemaphoreGlobal
.Counter
;
1831 mSmmMpSyncData
->InsideSmm
= mSmmCpuSemaphores
.SemaphoreGlobal
.InsideSmm
;
1832 mSmmMpSyncData
->AllCpusInSync
= mSmmCpuSemaphores
.SemaphoreGlobal
.AllCpusInSync
;
1833 ASSERT (mSmmMpSyncData
->Counter
!= NULL
&& mSmmMpSyncData
->InsideSmm
!= NULL
&&
1834 mSmmMpSyncData
->AllCpusInSync
!= NULL
);
1835 *mSmmMpSyncData
->Counter
= 0;
1836 *mSmmMpSyncData
->InsideSmm
= FALSE
;
1837 *mSmmMpSyncData
->AllCpusInSync
= FALSE
;
1839 for (CpuIndex
= 0; CpuIndex
< gSmmCpuPrivate
->SmmCoreEntryContext
.NumberOfCpus
; CpuIndex
++) {
1840 mSmmMpSyncData
->CpuData
[CpuIndex
].Busy
=
1841 (SPIN_LOCK
*)((UINTN
)mSmmCpuSemaphores
.SemaphoreCpu
.Busy
+ mSemaphoreSize
* CpuIndex
);
1842 mSmmMpSyncData
->CpuData
[CpuIndex
].Run
=
1843 (UINT32
*)((UINTN
)mSmmCpuSemaphores
.SemaphoreCpu
.Run
+ mSemaphoreSize
* CpuIndex
);
1844 mSmmMpSyncData
->CpuData
[CpuIndex
].Present
=
1845 (BOOLEAN
*)((UINTN
)mSmmCpuSemaphores
.SemaphoreCpu
.Present
+ mSemaphoreSize
* CpuIndex
);
1846 *(mSmmMpSyncData
->CpuData
[CpuIndex
].Busy
) = 0;
1847 *(mSmmMpSyncData
->CpuData
[CpuIndex
].Run
) = 0;
1848 *(mSmmMpSyncData
->CpuData
[CpuIndex
].Present
) = FALSE
;
1854 Initialize global data for MP synchronization.
1856 @param Stacks Base address of SMI stack buffer for all processors.
1857 @param StackSize Stack size for each processor in SMM.
1858 @param ShadowStackSize Shadow Stack size for each processor in SMM.
1862 InitializeMpServiceData (
1865 IN UINTN ShadowStackSize
1870 UINT8
*GdtTssTables
;
1871 UINTN GdtTableStepSize
;
1872 CPUID_VERSION_INFO_EDX RegEdx
;
1875 // Determine if this CPU supports machine check
1877 AsmCpuid (CPUID_VERSION_INFO
, NULL
, NULL
, NULL
, &RegEdx
.Uint32
);
1878 mMachineCheckSupported
= (BOOLEAN
)(RegEdx
.Bits
.MCA
== 1);
1881 // Allocate memory for all locks and semaphores
1883 InitializeSmmCpuSemaphores ();
1886 // Initialize mSmmMpSyncData
1888 mSmmMpSyncDataSize
= sizeof (SMM_DISPATCHER_MP_SYNC_DATA
) +
1889 (sizeof (SMM_CPU_DATA_BLOCK
) + sizeof (BOOLEAN
)) * gSmmCpuPrivate
->SmmCoreEntryContext
.NumberOfCpus
;
1890 mSmmMpSyncData
= (SMM_DISPATCHER_MP_SYNC_DATA
*) AllocatePages (EFI_SIZE_TO_PAGES (mSmmMpSyncDataSize
));
1891 ASSERT (mSmmMpSyncData
!= NULL
);
1892 mCpuSmmSyncMode
= (SMM_CPU_SYNC_MODE
)PcdGet8 (PcdCpuSmmSyncMode
);
1893 InitializeMpSyncData ();
1896 // Initialize physical address mask
1897 // NOTE: Physical memory above virtual address limit is not supported !!!
1899 AsmCpuid (0x80000008, (UINT32
*)&Index
, NULL
, NULL
, NULL
);
1900 gPhyMask
= LShiftU64 (1, (UINT8
)Index
) - 1;
1901 gPhyMask
&= (1ull << 48) - EFI_PAGE_SIZE
;
1904 // Create page tables
1906 Cr3
= SmmInitPageTable ();
1908 GdtTssTables
= InitGdt (Cr3
, &GdtTableStepSize
);
1911 // Install SMI handler for each CPU
1913 for (Index
= 0; Index
< mMaxNumberOfCpus
; Index
++) {
1916 (UINT32
)mCpuHotPlugData
.SmBase
[Index
],
1917 (VOID
*)((UINTN
)Stacks
+ (StackSize
+ ShadowStackSize
) * Index
),
1919 (UINTN
)(GdtTssTables
+ GdtTableStepSize
* Index
),
1920 gcSmiGdtr
.Limit
+ 1,
1922 gcSmiIdtr
.Limit
+ 1,
1928 // Record current MTRR settings
1930 ZeroMem (&gSmiMtrrs
, sizeof (gSmiMtrrs
));
1931 MtrrGetAllMtrrs (&gSmiMtrrs
);
1938 Register the SMM Foundation entry point.
1940 @param This Pointer to EFI_SMM_CONFIGURATION_PROTOCOL instance
1941 @param SmmEntryPoint SMM Foundation EntryPoint
1943 @retval EFI_SUCCESS Successfully to register SMM foundation entry point
1949 IN CONST EFI_SMM_CONFIGURATION_PROTOCOL
*This
,
1950 IN EFI_SMM_ENTRY_POINT SmmEntryPoint
1954 // Record SMM Foundation EntryPoint, later invoke it on SMI entry vector.
1956 gSmmCpuPrivate
->SmmCoreEntry
= SmmEntryPoint
;
1962 Register the SMM Foundation entry point.
1964 @param[in] Procedure A pointer to the code stream to be run on the designated target AP
1965 of the system. Type EFI_AP_PROCEDURE is defined below in Volume 2
1966 with the related definitions of
1967 EFI_MP_SERVICES_PROTOCOL.StartupAllAPs.
1968 If caller may pass a value of NULL to deregister any existing
1970 @param[in,out] ProcedureArguments Allows the caller to pass a list of parameters to the code that is
1971 run by the AP. It is an optional common mailbox between APs and
1972 the caller to share information
1974 @retval EFI_SUCCESS The Procedure has been set successfully.
1975 @retval EFI_INVALID_PARAMETER The Procedure is NULL but ProcedureArguments not NULL.
1979 RegisterStartupProcedure (
1980 IN EFI_AP_PROCEDURE Procedure
,
1981 IN OUT VOID
*ProcedureArguments OPTIONAL
1984 if (Procedure
== NULL
&& ProcedureArguments
!= NULL
) {
1985 return EFI_INVALID_PARAMETER
;
1987 if (mSmmMpSyncData
== NULL
) {
1988 return EFI_NOT_READY
;
1991 mSmmMpSyncData
->StartupProcedure
= Procedure
;
1992 mSmmMpSyncData
->StartupProcArgs
= ProcedureArguments
;