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 Clean up the status flags used during executing the procedure.
408 @param CpuIndex The AP index which calls this function.
416 PROCEDURE_TOKEN
*Token
;
418 Token
= mSmmMpSyncData
->CpuData
[CpuIndex
].Token
;
420 if (InterlockedDecrement (&Token
->RunningApCount
) == 0) {
421 ReleaseSpinLock (Token
->SpinLock
);
424 mSmmMpSyncData
->CpuData
[CpuIndex
].Token
= NULL
;
428 Free the tokens in the maintained list.
437 PROCEDURE_TOKEN
*ProcToken
;
439 Link
= GetFirstNode (&gSmmCpuPrivate
->TokenList
);
440 while (!IsNull (&gSmmCpuPrivate
->TokenList
, Link
)) {
441 ProcToken
= PROCEDURE_TOKEN_FROM_LINK (Link
);
443 ProcToken
->RunningApCount
= 0;
444 ProcToken
->Used
= FALSE
;
447 // Check the spinlock status and release it if not released yet.
449 if (!AcquireSpinLockOrFail(ProcToken
->SpinLock
)) {
450 DEBUG((DEBUG_ERROR
, "Risk::SpinLock still not released!"));
452 ReleaseSpinLock (ProcToken
->SpinLock
);
454 Link
= GetNextNode (&gSmmCpuPrivate
->TokenList
, Link
);
461 @param CpuIndex BSP processor Index
462 @param SyncMode SMM MP sync mode
468 IN SMM_CPU_SYNC_MODE SyncMode
474 BOOLEAN ClearTopLevelSmiResult
;
477 ASSERT (CpuIndex
== mSmmMpSyncData
->BspIndex
);
481 // Flag BSP's presence
483 *mSmmMpSyncData
->InsideSmm
= TRUE
;
486 // Initialize Debug Agent to start source level debug in BSP handler
488 InitializeDebugAgent (DEBUG_AGENT_INIT_ENTER_SMI
, NULL
, NULL
);
491 // Mark this processor's presence
493 *(mSmmMpSyncData
->CpuData
[CpuIndex
].Present
) = TRUE
;
496 // Clear platform top level SMI status bit before calling SMI handlers. If
497 // we cleared it after SMI handlers are run, we would miss the SMI that
498 // occurs after SMI handlers are done and before SMI status bit is cleared.
500 ClearTopLevelSmiResult
= ClearTopLevelSmiStatus();
501 ASSERT (ClearTopLevelSmiResult
== TRUE
);
504 // Set running processor index
506 gSmmCpuPrivate
->SmmCoreEntryContext
.CurrentlyExecutingCpu
= CpuIndex
;
509 // If Traditional Sync Mode or need to configure MTRRs: gather all available APs.
511 if (SyncMode
== SmmCpuSyncModeTradition
|| SmmCpuFeaturesNeedConfigureMtrrs()) {
514 // Wait for APs to arrive
516 SmmWaitForApArrival();
519 // Lock the counter down and retrieve the number of APs
521 *mSmmMpSyncData
->AllCpusInSync
= TRUE
;
522 ApCount
= LockdownSemaphore (mSmmMpSyncData
->Counter
) - 1;
525 // Wait for all APs to get ready for programming MTRRs
527 WaitForAllAPs (ApCount
);
529 if (SmmCpuFeaturesNeedConfigureMtrrs()) {
531 // Signal all APs it's time for backup MTRRs
536 // WaitForSemaphore() may wait for ever if an AP happens to enter SMM at
537 // exactly this point. Please make sure PcdCpuSmmMaxSyncLoops has been set
538 // to a large enough value to avoid this situation.
539 // Note: For HT capable CPUs, threads within a core share the same set of MTRRs.
540 // We do the backup first and then set MTRR to avoid race condition for threads
543 MtrrGetAllMtrrs(&Mtrrs
);
546 // Wait for all APs to complete their MTRR saving
548 WaitForAllAPs (ApCount
);
551 // Let all processors program SMM MTRRs together
556 // WaitForSemaphore() may wait for ever if an AP happens to enter SMM at
557 // exactly this point. Please make sure PcdCpuSmmMaxSyncLoops has been set
558 // to a large enough value to avoid this situation.
560 ReplaceOSMtrrs (CpuIndex
);
563 // Wait for all APs to complete their MTRR programming
565 WaitForAllAPs (ApCount
);
570 // The BUSY lock is initialized to Acquired state
572 AcquireSpinLock (mSmmMpSyncData
->CpuData
[CpuIndex
].Busy
);
575 // Perform the pre tasks
580 // Invoke SMM Foundation EntryPoint with the processor information context.
582 gSmmCpuPrivate
->SmmCoreEntry (&gSmmCpuPrivate
->SmmCoreEntryContext
);
585 // Make sure all APs have completed their pending none-block tasks
587 WaitForAllAPsNotBusy (TRUE
);
590 // Perform the remaining tasks
592 PerformRemainingTasks ();
595 // If Relaxed-AP Sync Mode: gather all available APs after BSP SMM handlers are done, and
596 // make those APs to exit SMI synchronously. APs which arrive later will be excluded and
597 // will run through freely.
599 if (SyncMode
!= SmmCpuSyncModeTradition
&& !SmmCpuFeaturesNeedConfigureMtrrs()) {
602 // Lock the counter down and retrieve the number of APs
604 *mSmmMpSyncData
->AllCpusInSync
= TRUE
;
605 ApCount
= LockdownSemaphore (mSmmMpSyncData
->Counter
) - 1;
607 // Make sure all APs have their Present flag set
611 for (Index
= mMaxNumberOfCpus
; Index
-- > 0;) {
612 if (*(mSmmMpSyncData
->CpuData
[Index
].Present
)) {
616 if (PresentCount
> ApCount
) {
623 // Notify all APs to exit
625 *mSmmMpSyncData
->InsideSmm
= FALSE
;
629 // Wait for all APs to complete their pending tasks
631 WaitForAllAPs (ApCount
);
633 if (SmmCpuFeaturesNeedConfigureMtrrs()) {
635 // Signal APs to restore MTRRs
642 SmmCpuFeaturesReenableSmrr ();
643 MtrrSetAllMtrrs(&Mtrrs
);
646 // Wait for all APs to complete MTRR programming
648 WaitForAllAPs (ApCount
);
652 // Stop source level debug in BSP handler, the code below will not be
655 InitializeDebugAgent (DEBUG_AGENT_INIT_EXIT_SMI
, NULL
, NULL
);
658 // Signal APs to Reset states/semaphore for this processor
663 // Perform pending operations for hot-plug
668 // Clear the Present flag of BSP
670 *(mSmmMpSyncData
->CpuData
[CpuIndex
].Present
) = FALSE
;
673 // Gather APs to exit SMM synchronously. Note the Present flag is cleared by now but
674 // WaitForAllAps does not depend on the Present flag.
676 WaitForAllAPs (ApCount
);
679 // Reset the tokens buffer.
684 // Reset BspIndex to -1, meaning BSP has not been elected.
686 if (FeaturePcdGet (PcdCpuSmmEnableBspElection
)) {
687 mSmmMpSyncData
->BspIndex
= (UINT32
)-1;
691 // Allow APs to check in from this point on
693 *mSmmMpSyncData
->Counter
= 0;
694 *mSmmMpSyncData
->AllCpusInSync
= FALSE
;
700 @param CpuIndex AP processor Index.
701 @param ValidSmi Indicates that current SMI is a valid SMI or not.
702 @param SyncMode SMM MP sync mode.
709 IN SMM_CPU_SYNC_MODE SyncMode
715 EFI_STATUS ProcedureStatus
;
720 for (Timer
= StartSyncTimer ();
721 !IsSyncTimerTimeout (Timer
) &&
722 !(*mSmmMpSyncData
->InsideSmm
);
727 if (!(*mSmmMpSyncData
->InsideSmm
)) {
729 // BSP timeout in the first round
731 if (mSmmMpSyncData
->BspIndex
!= -1) {
733 // BSP Index is known
735 BspIndex
= mSmmMpSyncData
->BspIndex
;
736 ASSERT (CpuIndex
!= BspIndex
);
739 // Send SMI IPI to bring BSP in
741 SendSmiIpi ((UINT32
)gSmmCpuPrivate
->ProcessorInfo
[BspIndex
].ProcessorId
);
744 // Now clock BSP for the 2nd time
746 for (Timer
= StartSyncTimer ();
747 !IsSyncTimerTimeout (Timer
) &&
748 !(*mSmmMpSyncData
->InsideSmm
);
753 if (!(*mSmmMpSyncData
->InsideSmm
)) {
755 // Give up since BSP is unable to enter SMM
756 // and signal the completion of this AP
757 WaitForSemaphore (mSmmMpSyncData
->Counter
);
762 // Don't know BSP index. Give up without sending IPI to BSP.
764 WaitForSemaphore (mSmmMpSyncData
->Counter
);
772 BspIndex
= mSmmMpSyncData
->BspIndex
;
773 ASSERT (CpuIndex
!= BspIndex
);
776 // Mark this processor's presence
778 *(mSmmMpSyncData
->CpuData
[CpuIndex
].Present
) = TRUE
;
780 if (SyncMode
== SmmCpuSyncModeTradition
|| SmmCpuFeaturesNeedConfigureMtrrs()) {
782 // Notify BSP of arrival at this point
784 ReleaseSemaphore (mSmmMpSyncData
->CpuData
[BspIndex
].Run
);
787 if (SmmCpuFeaturesNeedConfigureMtrrs()) {
789 // Wait for the signal from BSP to backup MTRRs
791 WaitForSemaphore (mSmmMpSyncData
->CpuData
[CpuIndex
].Run
);
796 MtrrGetAllMtrrs(&Mtrrs
);
799 // Signal BSP the completion of this AP
801 ReleaseSemaphore (mSmmMpSyncData
->CpuData
[BspIndex
].Run
);
804 // Wait for BSP's signal to program MTRRs
806 WaitForSemaphore (mSmmMpSyncData
->CpuData
[CpuIndex
].Run
);
809 // Replace OS MTRRs with SMI MTRRs
811 ReplaceOSMtrrs (CpuIndex
);
814 // Signal BSP the completion of this AP
816 ReleaseSemaphore (mSmmMpSyncData
->CpuData
[BspIndex
].Run
);
821 // Wait for something to happen
823 WaitForSemaphore (mSmmMpSyncData
->CpuData
[CpuIndex
].Run
);
826 // Check if BSP wants to exit SMM
828 if (!(*mSmmMpSyncData
->InsideSmm
)) {
833 // BUSY should be acquired by SmmStartupThisAp()
836 !AcquireSpinLockOrFail (mSmmMpSyncData
->CpuData
[CpuIndex
].Busy
)
840 // Invoke the scheduled procedure
842 ProcedureStatus
= (*mSmmMpSyncData
->CpuData
[CpuIndex
].Procedure
) (
843 (VOID
*)mSmmMpSyncData
->CpuData
[CpuIndex
].Parameter
845 if (mSmmMpSyncData
->CpuData
[CpuIndex
].Status
!= NULL
) {
846 *mSmmMpSyncData
->CpuData
[CpuIndex
].Status
= ProcedureStatus
;
849 if (mSmmMpSyncData
->CpuData
[CpuIndex
].Token
!= NULL
) {
850 ReleaseToken (CpuIndex
);
856 ReleaseSpinLock (mSmmMpSyncData
->CpuData
[CpuIndex
].Busy
);
859 if (SmmCpuFeaturesNeedConfigureMtrrs()) {
861 // Notify BSP the readiness of this AP to program MTRRs
863 ReleaseSemaphore (mSmmMpSyncData
->CpuData
[BspIndex
].Run
);
866 // Wait for the signal from BSP to program MTRRs
868 WaitForSemaphore (mSmmMpSyncData
->CpuData
[CpuIndex
].Run
);
873 SmmCpuFeaturesReenableSmrr ();
874 MtrrSetAllMtrrs(&Mtrrs
);
878 // Notify BSP the readiness of this AP to Reset states/semaphore for this processor
880 ReleaseSemaphore (mSmmMpSyncData
->CpuData
[BspIndex
].Run
);
883 // Wait for the signal from BSP to Reset states/semaphore for this processor
885 WaitForSemaphore (mSmmMpSyncData
->CpuData
[CpuIndex
].Run
);
888 // Reset states/semaphore for this processor
890 *(mSmmMpSyncData
->CpuData
[CpuIndex
].Present
) = FALSE
;
893 // Notify BSP the readiness of this AP to exit SMM
895 ReleaseSemaphore (mSmmMpSyncData
->CpuData
[BspIndex
].Run
);
900 Create 4G PageTable in SMRAM.
902 @param[in] Is32BitPageTable Whether the page table is 32-bit PAE
903 @return PageTable Address
908 IN BOOLEAN Is32BitPageTable
916 UINTN High2MBoundary
;
926 if (FeaturePcdGet (PcdCpuSmmStackGuard
)) {
928 // Add one more page for known good stack, then find the lower 2MB aligned address.
930 Low2MBoundary
= (mSmmStackArrayBase
+ EFI_PAGE_SIZE
) & ~(SIZE_2MB
-1);
932 // Add two more pages for known good stack and stack guard page,
933 // then find the lower 2MB aligned address.
935 High2MBoundary
= (mSmmStackArrayEnd
- mSmmStackSize
+ EFI_PAGE_SIZE
* 2) & ~(SIZE_2MB
-1);
936 PagesNeeded
= ((High2MBoundary
- Low2MBoundary
) / SIZE_2MB
) + 1;
939 // Allocate the page table
941 PageTable
= AllocatePageTableMemory (5 + PagesNeeded
);
942 ASSERT (PageTable
!= NULL
);
944 PageTable
= (VOID
*)((UINTN
)PageTable
);
945 Pte
= (UINT64
*)PageTable
;
948 // Zero out all page table entries first
950 ZeroMem (Pte
, EFI_PAGES_TO_SIZE (1));
953 // Set Page Directory Pointers
955 for (Index
= 0; Index
< 4; Index
++) {
956 Pte
[Index
] = ((UINTN
)PageTable
+ EFI_PAGE_SIZE
* (Index
+ 1)) | mAddressEncMask
|
957 (Is32BitPageTable
? IA32_PAE_PDPTE_ATTRIBUTE_BITS
: PAGE_ATTRIBUTE_BITS
);
959 Pte
+= EFI_PAGE_SIZE
/ sizeof (*Pte
);
962 // Fill in Page Directory Entries
964 for (Index
= 0; Index
< EFI_PAGE_SIZE
* 4 / sizeof (*Pte
); Index
++) {
965 Pte
[Index
] = (Index
<< 21) | mAddressEncMask
| IA32_PG_PS
| PAGE_ATTRIBUTE_BITS
;
968 Pdpte
= (UINT64
*)PageTable
;
969 if (FeaturePcdGet (PcdCpuSmmStackGuard
)) {
970 Pages
= (UINTN
)PageTable
+ EFI_PAGES_TO_SIZE (5);
971 GuardPage
= mSmmStackArrayBase
+ EFI_PAGE_SIZE
;
972 for (PageIndex
= Low2MBoundary
; PageIndex
<= High2MBoundary
; PageIndex
+= SIZE_2MB
) {
973 Pte
= (UINT64
*)(UINTN
)(Pdpte
[BitFieldRead32 ((UINT32
)PageIndex
, 30, 31)] & ~mAddressEncMask
& ~(EFI_PAGE_SIZE
- 1));
974 Pte
[BitFieldRead32 ((UINT32
)PageIndex
, 21, 29)] = (UINT64
)Pages
| mAddressEncMask
| PAGE_ATTRIBUTE_BITS
;
976 // Fill in Page Table Entries
978 Pte
= (UINT64
*)Pages
;
979 PageAddress
= PageIndex
;
980 for (Index
= 0; Index
< EFI_PAGE_SIZE
/ sizeof (*Pte
); Index
++) {
981 if (PageAddress
== GuardPage
) {
983 // Mark the guard page as non-present
985 Pte
[Index
] = PageAddress
| mAddressEncMask
;
986 GuardPage
+= mSmmStackSize
;
987 if (GuardPage
> mSmmStackArrayEnd
) {
991 Pte
[Index
] = PageAddress
| mAddressEncMask
| PAGE_ATTRIBUTE_BITS
;
993 PageAddress
+= EFI_PAGE_SIZE
;
995 Pages
+= EFI_PAGE_SIZE
;
999 if ((PcdGet8 (PcdNullPointerDetectionPropertyMask
) & BIT1
) != 0) {
1000 Pte
= (UINT64
*)(UINTN
)(Pdpte
[0] & ~mAddressEncMask
& ~(EFI_PAGE_SIZE
- 1));
1001 if ((Pte
[0] & IA32_PG_PS
) == 0) {
1002 // 4K-page entries are already mapped. Just hide the first one anyway.
1003 Pte
= (UINT64
*)(UINTN
)(Pte
[0] & ~mAddressEncMask
& ~(EFI_PAGE_SIZE
- 1));
1004 Pte
[0] &= ~(UINT64
)IA32_PG_P
; // Hide page 0
1006 // Create 4K-page entries
1007 Pages
= (UINTN
)AllocatePageTableMemory (1);
1008 ASSERT (Pages
!= 0);
1010 Pte
[0] = (UINT64
)(Pages
| mAddressEncMask
| PAGE_ATTRIBUTE_BITS
);
1012 Pte
= (UINT64
*)Pages
;
1014 Pte
[0] = PageAddress
| mAddressEncMask
; // Hide page 0 but present left
1015 for (Index
= 1; Index
< EFI_PAGE_SIZE
/ sizeof (*Pte
); Index
++) {
1016 PageAddress
+= EFI_PAGE_SIZE
;
1017 Pte
[Index
] = PageAddress
| mAddressEncMask
| PAGE_ATTRIBUTE_BITS
;
1022 return (UINT32
)(UINTN
)PageTable
;
1026 Checks whether the input token is the current used token.
1028 @param[in] Token This parameter describes the token that was passed into DispatchProcedure or
1031 @retval TRUE The input token is the current used token.
1032 @retval FALSE The input token is not the current used token.
1040 PROCEDURE_TOKEN
*ProcToken
;
1042 if (Token
== NULL
) {
1046 Link
= GetFirstNode (&gSmmCpuPrivate
->TokenList
);
1047 while (!IsNull (&gSmmCpuPrivate
->TokenList
, Link
)) {
1048 ProcToken
= PROCEDURE_TOKEN_FROM_LINK (Link
);
1050 if (ProcToken
->Used
&& ProcToken
->SpinLock
== Token
) {
1054 Link
= GetNextNode (&gSmmCpuPrivate
->TokenList
, Link
);
1061 Allocate buffer for the SPIN_LOCK and PROCEDURE_TOKEN.
1065 AllocateTokenBuffer (
1070 UINT32 TokenCountPerChunk
;
1071 UINTN ProcTokenSize
;
1073 PROCEDURE_TOKEN
*ProcToken
;
1074 SPIN_LOCK
*SpinLock
;
1075 UINT8
*SpinLockBuffer
;
1076 UINT8
*ProcTokenBuffer
;
1078 SpinLockSize
= GetSpinLockProperties ();
1079 ProcTokenSize
= sizeof (PROCEDURE_TOKEN
);
1081 TokenCountPerChunk
= FixedPcdGet32 (PcdCpuSmmMpTokenCountPerChunk
);
1082 ASSERT (TokenCountPerChunk
!= 0);
1083 if (TokenCountPerChunk
== 0) {
1084 DEBUG ((DEBUG_ERROR
, "PcdCpuSmmMpTokenCountPerChunk should not be Zero!\n"));
1087 DEBUG ((DEBUG_INFO
, "CpuSmm: SpinLock Size = 0x%x, PcdCpuSmmMpTokenCountPerChunk = 0x%x\n", SpinLockSize
, TokenCountPerChunk
));
1090 // Separate the Spin_lock and Proc_token because the alignment requires by Spin_Lock.
1092 SpinLockBuffer
= AllocatePool (SpinLockSize
* TokenCountPerChunk
);
1093 ASSERT (SpinLockBuffer
!= NULL
);
1095 ProcTokenBuffer
= AllocatePool (ProcTokenSize
* TokenCountPerChunk
);
1096 ASSERT (ProcTokenBuffer
!= NULL
);
1098 for (Index
= 0; Index
< TokenCountPerChunk
; Index
++) {
1099 SpinLock
= (SPIN_LOCK
*)(SpinLockBuffer
+ SpinLockSize
* Index
);
1100 InitializeSpinLock (SpinLock
);
1102 ProcToken
= (PROCEDURE_TOKEN
*)(ProcTokenBuffer
+ ProcTokenSize
* Index
);
1103 ProcToken
->Signature
= PROCEDURE_TOKEN_SIGNATURE
;
1104 ProcToken
->SpinLock
= SpinLock
;
1105 ProcToken
->Used
= FALSE
;
1106 ProcToken
->RunningApCount
= 0;
1108 InsertTailList (&gSmmCpuPrivate
->TokenList
, &ProcToken
->Link
);
1113 Find first free token in the allocated token list.
1115 @retval return the first free PROCEDURE_TOKEN.
1119 FindFirstFreeToken (
1124 PROCEDURE_TOKEN
*ProcToken
;
1126 Link
= GetFirstNode (&gSmmCpuPrivate
->TokenList
);
1127 while (!IsNull (&gSmmCpuPrivate
->TokenList
, Link
)) {
1128 ProcToken
= PROCEDURE_TOKEN_FROM_LINK (Link
);
1130 if (!ProcToken
->Used
) {
1134 Link
= GetNextNode (&gSmmCpuPrivate
->TokenList
, Link
);
1143 If no free token, allocate new tokens then return the free one.
1145 @retval return the first free PROCEDURE_TOKEN.
1150 IN UINT32 RunningApsCount
1153 PROCEDURE_TOKEN
*NewToken
;
1155 NewToken
= FindFirstFreeToken ();
1156 if (NewToken
== NULL
) {
1157 AllocateTokenBuffer ();
1158 NewToken
= FindFirstFreeToken ();
1160 ASSERT (NewToken
!= NULL
);
1162 NewToken
->Used
= TRUE
;
1163 NewToken
->RunningApCount
= RunningApsCount
;
1164 AcquireSpinLock (NewToken
->SpinLock
);
1170 Checks status of specified AP.
1172 This function checks whether the specified AP has finished the task assigned
1173 by StartupThisAP(), and whether timeout expires.
1175 @param[in] Token This parameter describes the token that was passed into DispatchProcedure or
1178 @retval EFI_SUCCESS Specified AP has finished task assigned by StartupThisAPs().
1179 @retval EFI_NOT_READY Specified AP has not finished task and timeout has not expired.
1186 if (AcquireSpinLockOrFail (Token
)) {
1187 ReleaseSpinLock (Token
);
1191 return EFI_NOT_READY
;
1195 Schedule a procedure to run on the specified CPU.
1197 @param[in] Procedure The address of the procedure to run
1198 @param[in] CpuIndex Target CPU Index
1199 @param[in,out] ProcArguments The parameter to pass to the procedure
1200 @param[in] Token This is an optional parameter that allows the caller to execute the
1201 procedure in a blocking or non-blocking fashion. If it is NULL the
1202 call is blocking, and the call will not return until the AP has
1203 completed the procedure. If the token is not NULL, the call will
1204 return immediately. The caller can check whether the procedure has
1205 completed with CheckOnProcedure or WaitForProcedure.
1206 @param[in] TimeoutInMicroseconds Indicates the time limit in microseconds for the APs to finish
1207 execution of Procedure, either for blocking or non-blocking mode.
1208 Zero means infinity. If the timeout expires before all APs return
1209 from Procedure, then Procedure on the failed APs is terminated. If
1210 the timeout expires in blocking mode, the call returns EFI_TIMEOUT.
1211 If the timeout expires in non-blocking mode, the timeout determined
1212 can be through CheckOnProcedure or WaitForProcedure.
1213 Note that timeout support is optional. Whether an implementation
1214 supports this feature can be determined via the Attributes data
1216 @param[in,out] CpuStatus This optional pointer may be used to get the status code returned
1217 by Procedure when it completes execution on the target AP, or with
1218 EFI_TIMEOUT if the Procedure fails to complete within the optional
1219 timeout. The implementation will update this variable with
1220 EFI_NOT_READY prior to starting Procedure on the target AP.
1222 @retval EFI_INVALID_PARAMETER CpuNumber not valid
1223 @retval EFI_INVALID_PARAMETER CpuNumber specifying BSP
1224 @retval EFI_INVALID_PARAMETER The AP specified by CpuNumber did not enter SMM
1225 @retval EFI_INVALID_PARAMETER The AP specified by CpuNumber is busy
1226 @retval EFI_SUCCESS The procedure has been successfully scheduled
1230 InternalSmmStartupThisAp (
1231 IN EFI_AP_PROCEDURE2 Procedure
,
1233 IN OUT VOID
*ProcArguments OPTIONAL
,
1234 IN MM_COMPLETION
*Token
,
1235 IN UINTN TimeoutInMicroseconds
,
1236 IN OUT EFI_STATUS
*CpuStatus
1239 PROCEDURE_TOKEN
*ProcToken
;
1241 if (CpuIndex
>= gSmmCpuPrivate
->SmmCoreEntryContext
.NumberOfCpus
) {
1242 DEBUG((DEBUG_ERROR
, "CpuIndex(%d) >= gSmmCpuPrivate->SmmCoreEntryContext.NumberOfCpus(%d)\n", CpuIndex
, gSmmCpuPrivate
->SmmCoreEntryContext
.NumberOfCpus
));
1243 return EFI_INVALID_PARAMETER
;
1245 if (CpuIndex
== gSmmCpuPrivate
->SmmCoreEntryContext
.CurrentlyExecutingCpu
) {
1246 DEBUG((DEBUG_ERROR
, "CpuIndex(%d) == gSmmCpuPrivate->SmmCoreEntryContext.CurrentlyExecutingCpu\n", CpuIndex
));
1247 return EFI_INVALID_PARAMETER
;
1249 if (gSmmCpuPrivate
->ProcessorInfo
[CpuIndex
].ProcessorId
== INVALID_APIC_ID
) {
1250 return EFI_INVALID_PARAMETER
;
1252 if (!(*(mSmmMpSyncData
->CpuData
[CpuIndex
].Present
))) {
1253 if (mSmmMpSyncData
->EffectiveSyncMode
== SmmCpuSyncModeTradition
) {
1254 DEBUG((DEBUG_ERROR
, "!mSmmMpSyncData->CpuData[%d].Present\n", CpuIndex
));
1256 return EFI_INVALID_PARAMETER
;
1258 if (gSmmCpuPrivate
->Operation
[CpuIndex
] == SmmCpuRemove
) {
1259 if (!FeaturePcdGet (PcdCpuHotPlugSupport
)) {
1260 DEBUG((DEBUG_ERROR
, "gSmmCpuPrivate->Operation[%d] == SmmCpuRemove\n", CpuIndex
));
1262 return EFI_INVALID_PARAMETER
;
1264 if ((TimeoutInMicroseconds
!= 0) && ((mSmmMp
.Attributes
& EFI_MM_MP_TIMEOUT_SUPPORTED
) == 0)) {
1265 return EFI_INVALID_PARAMETER
;
1267 if (Procedure
== NULL
) {
1268 return EFI_INVALID_PARAMETER
;
1271 AcquireSpinLock (mSmmMpSyncData
->CpuData
[CpuIndex
].Busy
);
1273 mSmmMpSyncData
->CpuData
[CpuIndex
].Procedure
= Procedure
;
1274 mSmmMpSyncData
->CpuData
[CpuIndex
].Parameter
= ProcArguments
;
1275 if (Token
!= NULL
) {
1276 ProcToken
= GetFreeToken (1);
1277 mSmmMpSyncData
->CpuData
[CpuIndex
].Token
= ProcToken
;
1278 *Token
= (MM_COMPLETION
)ProcToken
->SpinLock
;
1280 mSmmMpSyncData
->CpuData
[CpuIndex
].Status
= CpuStatus
;
1281 if (mSmmMpSyncData
->CpuData
[CpuIndex
].Status
!= NULL
) {
1282 *mSmmMpSyncData
->CpuData
[CpuIndex
].Status
= EFI_NOT_READY
;
1285 ReleaseSemaphore (mSmmMpSyncData
->CpuData
[CpuIndex
].Run
);
1287 if (Token
== NULL
) {
1288 AcquireSpinLock (mSmmMpSyncData
->CpuData
[CpuIndex
].Busy
);
1289 ReleaseSpinLock (mSmmMpSyncData
->CpuData
[CpuIndex
].Busy
);
1296 Worker function to execute a caller provided function on all enabled APs.
1298 @param[in] Procedure A pointer to the function to be run on
1299 enabled APs of the system.
1300 @param[in] TimeoutInMicroseconds Indicates the time limit in microseconds for
1301 APs to return from Procedure, either for
1302 blocking or non-blocking mode.
1303 @param[in,out] ProcedureArguments The parameter passed into Procedure for
1305 @param[in,out] Token This is an optional parameter that allows the caller to execute the
1306 procedure in a blocking or non-blocking fashion. If it is NULL the
1307 call is blocking, and the call will not return until the AP has
1308 completed the procedure. If the token is not NULL, the call will
1309 return immediately. The caller can check whether the procedure has
1310 completed with CheckOnProcedure or WaitForProcedure.
1311 @param[in,out] CPUStatus This optional pointer may be used to get the status code returned
1312 by Procedure when it completes execution on the target AP, or with
1313 EFI_TIMEOUT if the Procedure fails to complete within the optional
1314 timeout. The implementation will update this variable with
1315 EFI_NOT_READY prior to starting Procedure on the target AP.
1318 @retval EFI_SUCCESS In blocking mode, all APs have finished before
1319 the timeout expired.
1320 @retval EFI_SUCCESS In non-blocking mode, function has been dispatched
1322 @retval others Failed to Startup all APs.
1326 InternalSmmStartupAllAPs (
1327 IN EFI_AP_PROCEDURE2 Procedure
,
1328 IN UINTN TimeoutInMicroseconds
,
1329 IN OUT VOID
*ProcedureArguments OPTIONAL
,
1330 IN OUT MM_COMPLETION
*Token
,
1331 IN OUT EFI_STATUS
*CPUStatus
1336 PROCEDURE_TOKEN
*ProcToken
;
1338 if ((TimeoutInMicroseconds
!= 0) && ((mSmmMp
.Attributes
& EFI_MM_MP_TIMEOUT_SUPPORTED
) == 0)) {
1339 return EFI_INVALID_PARAMETER
;
1341 if (Procedure
== NULL
) {
1342 return EFI_INVALID_PARAMETER
;
1346 for (Index
= mMaxNumberOfCpus
; Index
-- > 0;) {
1347 if (IsPresentAp (Index
)) {
1350 if (gSmmCpuPrivate
->Operation
[Index
] == SmmCpuRemove
) {
1351 return EFI_INVALID_PARAMETER
;
1354 if (!AcquireSpinLockOrFail(mSmmMpSyncData
->CpuData
[Index
].Busy
)) {
1355 return EFI_NOT_READY
;
1357 ReleaseSpinLock (mSmmMpSyncData
->CpuData
[Index
].Busy
);
1360 if (CpuCount
== 0) {
1361 return EFI_NOT_STARTED
;
1364 if (Token
!= NULL
) {
1365 ProcToken
= GetFreeToken ((UINT32
)mMaxNumberOfCpus
);
1366 *Token
= (MM_COMPLETION
)ProcToken
->SpinLock
;
1372 // Make sure all BUSY should be acquired.
1374 // Because former code already check mSmmMpSyncData->CpuData[***].Busy for each AP.
1375 // Here code always use AcquireSpinLock instead of AcquireSpinLockOrFail for not
1378 for (Index
= mMaxNumberOfCpus
; Index
-- > 0;) {
1379 if (IsPresentAp (Index
)) {
1380 AcquireSpinLock (mSmmMpSyncData
->CpuData
[Index
].Busy
);
1384 for (Index
= mMaxNumberOfCpus
; Index
-- > 0;) {
1385 if (IsPresentAp (Index
)) {
1386 mSmmMpSyncData
->CpuData
[Index
].Procedure
= (EFI_AP_PROCEDURE2
) Procedure
;
1387 mSmmMpSyncData
->CpuData
[Index
].Parameter
= ProcedureArguments
;
1388 if (ProcToken
!= NULL
) {
1389 mSmmMpSyncData
->CpuData
[Index
].Token
= ProcToken
;
1391 if (CPUStatus
!= NULL
) {
1392 mSmmMpSyncData
->CpuData
[Index
].Status
= &CPUStatus
[Index
];
1393 if (mSmmMpSyncData
->CpuData
[Index
].Status
!= NULL
) {
1394 *mSmmMpSyncData
->CpuData
[Index
].Status
= EFI_NOT_READY
;
1399 // PI spec requirement:
1400 // For every excluded processor, the array entry must contain a value of EFI_NOT_STARTED.
1402 if (CPUStatus
!= NULL
) {
1403 CPUStatus
[Index
] = EFI_NOT_STARTED
;
1407 // Decrease the count to mark this processor(AP or BSP) as finished.
1409 if (ProcToken
!= NULL
) {
1410 WaitForSemaphore (&ProcToken
->RunningApCount
);
1417 if (Token
== NULL
) {
1419 // Make sure all APs have completed their tasks.
1421 WaitForAllAPsNotBusy (TRUE
);
1428 ISO C99 6.5.2.2 "Function calls", paragraph 9:
1429 If the function is defined with a type that is not compatible with
1430 the type (of the expression) pointed to by the expression that
1431 denotes the called function, the behavior is undefined.
1433 So add below wrapper function to convert between EFI_AP_PROCEDURE
1434 and EFI_AP_PROCEDURE2.
1436 Wrapper for Procedures.
1438 @param[in] Buffer Pointer to PROCEDURE_WRAPPER buffer.
1447 PROCEDURE_WRAPPER
*Wrapper
;
1450 Wrapper
->Procedure (Wrapper
->ProcedureArgument
);
1456 Schedule a procedure to run on the specified CPU in blocking mode.
1458 @param[in] Procedure The address of the procedure to run
1459 @param[in] CpuIndex Target CPU Index
1460 @param[in, out] ProcArguments The parameter to pass to the procedure
1462 @retval EFI_INVALID_PARAMETER CpuNumber not valid
1463 @retval EFI_INVALID_PARAMETER CpuNumber specifying BSP
1464 @retval EFI_INVALID_PARAMETER The AP specified by CpuNumber did not enter SMM
1465 @retval EFI_INVALID_PARAMETER The AP specified by CpuNumber is busy
1466 @retval EFI_SUCCESS The procedure has been successfully scheduled
1471 SmmBlockingStartupThisAp (
1472 IN EFI_AP_PROCEDURE Procedure
,
1474 IN OUT VOID
*ProcArguments OPTIONAL
1477 PROCEDURE_WRAPPER Wrapper
;
1479 Wrapper
.Procedure
= Procedure
;
1480 Wrapper
.ProcedureArgument
= ProcArguments
;
1483 // Use wrapper function to convert EFI_AP_PROCEDURE to EFI_AP_PROCEDURE2.
1485 return InternalSmmStartupThisAp (ProcedureWrapper
, CpuIndex
, &Wrapper
, NULL
, 0, NULL
);
1489 Schedule a procedure to run on the specified CPU.
1491 @param Procedure The address of the procedure to run
1492 @param CpuIndex Target CPU Index
1493 @param ProcArguments The parameter to pass to the procedure
1495 @retval EFI_INVALID_PARAMETER CpuNumber not valid
1496 @retval EFI_INVALID_PARAMETER CpuNumber specifying BSP
1497 @retval EFI_INVALID_PARAMETER The AP specified by CpuNumber did not enter SMM
1498 @retval EFI_INVALID_PARAMETER The AP specified by CpuNumber is busy
1499 @retval EFI_SUCCESS The procedure has been successfully scheduled
1505 IN EFI_AP_PROCEDURE Procedure
,
1507 IN OUT VOID
*ProcArguments OPTIONAL
1510 MM_COMPLETION Token
;
1512 gSmmCpuPrivate
->ApWrapperFunc
[CpuIndex
].Procedure
= Procedure
;
1513 gSmmCpuPrivate
->ApWrapperFunc
[CpuIndex
].ProcedureArgument
= ProcArguments
;
1516 // Use wrapper function to convert EFI_AP_PROCEDURE to EFI_AP_PROCEDURE2.
1518 return InternalSmmStartupThisAp (
1521 &gSmmCpuPrivate
->ApWrapperFunc
[CpuIndex
],
1522 FeaturePcdGet (PcdCpuSmmBlockStartupThisAp
) ? NULL
: &Token
,
1529 This function sets DR6 & DR7 according to SMM save state, before running SMM C code.
1530 They are useful when you want to enable hardware breakpoints in SMM without entry SMM mode.
1532 NOTE: It might not be appreciated in runtime since it might
1533 conflict with OS debugging facilities. Turn them off in RELEASE.
1535 @param CpuIndex CPU Index
1544 SMRAM_SAVE_STATE_MAP
*CpuSaveState
;
1546 if (FeaturePcdGet (PcdCpuSmmDebug
)) {
1547 ASSERT(CpuIndex
< mMaxNumberOfCpus
);
1548 CpuSaveState
= (SMRAM_SAVE_STATE_MAP
*)gSmmCpuPrivate
->CpuSaveState
[CpuIndex
];
1549 if (mSmmSaveStateRegisterLma
== EFI_SMM_SAVE_STATE_REGISTER_LMA_32BIT
) {
1550 AsmWriteDr6 (CpuSaveState
->x86
._DR6
);
1551 AsmWriteDr7 (CpuSaveState
->x86
._DR7
);
1553 AsmWriteDr6 ((UINTN
)CpuSaveState
->x64
._DR6
);
1554 AsmWriteDr7 ((UINTN
)CpuSaveState
->x64
._DR7
);
1560 This function restores DR6 & DR7 to SMM save state.
1562 NOTE: It might not be appreciated in runtime since it might
1563 conflict with OS debugging facilities. Turn them off in RELEASE.
1565 @param CpuIndex CPU Index
1574 SMRAM_SAVE_STATE_MAP
*CpuSaveState
;
1576 if (FeaturePcdGet (PcdCpuSmmDebug
)) {
1577 ASSERT(CpuIndex
< mMaxNumberOfCpus
);
1578 CpuSaveState
= (SMRAM_SAVE_STATE_MAP
*)gSmmCpuPrivate
->CpuSaveState
[CpuIndex
];
1579 if (mSmmSaveStateRegisterLma
== EFI_SMM_SAVE_STATE_REGISTER_LMA_32BIT
) {
1580 CpuSaveState
->x86
._DR7
= (UINT32
)AsmReadDr7 ();
1581 CpuSaveState
->x86
._DR6
= (UINT32
)AsmReadDr6 ();
1583 CpuSaveState
->x64
._DR7
= AsmReadDr7 ();
1584 CpuSaveState
->x64
._DR6
= AsmReadDr6 ();
1590 C function for SMI entry, each processor comes here upon SMI trigger.
1592 @param CpuIndex CPU Index
1604 BOOLEAN BspInProgress
;
1608 ASSERT(CpuIndex
< mMaxNumberOfCpus
);
1611 // Save Cr2 because Page Fault exception in SMM may override its value,
1612 // when using on-demand paging for above 4G memory.
1618 // Call the user register Startup function first.
1620 if (mSmmMpSyncData
->StartupProcedure
!= NULL
) {
1621 mSmmMpSyncData
->StartupProcedure (mSmmMpSyncData
->StartupProcArgs
);
1625 // Perform CPU specific entry hooks
1627 SmmCpuFeaturesRendezvousEntry (CpuIndex
);
1630 // Determine if this is a valid SMI
1632 ValidSmi
= PlatformValidSmi();
1635 // Determine if BSP has been already in progress. Note this must be checked after
1636 // ValidSmi because BSP may clear a valid SMI source after checking in.
1638 BspInProgress
= *mSmmMpSyncData
->InsideSmm
;
1640 if (!BspInProgress
&& !ValidSmi
) {
1642 // If we reach here, it means when we sampled the ValidSmi flag, SMI status had not
1643 // been cleared by BSP in a new SMI run (so we have a truly invalid SMI), or SMI
1644 // status had been cleared by BSP and an existing SMI run has almost ended. (Note
1645 // we sampled ValidSmi flag BEFORE judging BSP-in-progress status.) In both cases, there
1646 // is nothing we need to do.
1651 // Signal presence of this processor
1653 if (ReleaseSemaphore (mSmmMpSyncData
->Counter
) == 0) {
1655 // BSP has already ended the synchronization, so QUIT!!!
1659 // Wait for BSP's signal to finish SMI
1661 while (*mSmmMpSyncData
->AllCpusInSync
) {
1668 // The BUSY lock is initialized to Released state.
1669 // This needs to be done early enough to be ready for BSP's SmmStartupThisAp() call.
1670 // E.g., with Relaxed AP flow, SmmStartupThisAp() may be called immediately
1671 // after AP's present flag is detected.
1673 InitializeSpinLock (mSmmMpSyncData
->CpuData
[CpuIndex
].Busy
);
1676 if (FeaturePcdGet (PcdCpuSmmProfileEnable
)) {
1677 ActivateSmmProfile (CpuIndex
);
1680 if (BspInProgress
) {
1682 // BSP has been elected. Follow AP path, regardless of ValidSmi flag
1683 // as BSP may have cleared the SMI status
1685 APHandler (CpuIndex
, ValidSmi
, mSmmMpSyncData
->EffectiveSyncMode
);
1688 // We have a valid SMI
1695 if (FeaturePcdGet (PcdCpuSmmEnableBspElection
)) {
1696 if (!mSmmMpSyncData
->SwitchBsp
|| mSmmMpSyncData
->CandidateBsp
[CpuIndex
]) {
1698 // Call platform hook to do BSP election
1700 Status
= PlatformSmmBspElection (&IsBsp
);
1701 if (EFI_SUCCESS
== Status
) {
1703 // Platform hook determines successfully
1706 mSmmMpSyncData
->BspIndex
= (UINT32
)CpuIndex
;
1710 // Platform hook fails to determine, use default BSP election method
1712 InterlockedCompareExchange32 (
1713 (UINT32
*)&mSmmMpSyncData
->BspIndex
,
1722 // "mSmmMpSyncData->BspIndex == CpuIndex" means this is the BSP
1724 if (mSmmMpSyncData
->BspIndex
== CpuIndex
) {
1727 // Clear last request for SwitchBsp.
1729 if (mSmmMpSyncData
->SwitchBsp
) {
1730 mSmmMpSyncData
->SwitchBsp
= FALSE
;
1731 for (Index
= 0; Index
< mMaxNumberOfCpus
; Index
++) {
1732 mSmmMpSyncData
->CandidateBsp
[Index
] = FALSE
;
1736 if (FeaturePcdGet (PcdCpuSmmProfileEnable
)) {
1737 SmmProfileRecordSmiNum ();
1741 // BSP Handler is always called with a ValidSmi == TRUE
1743 BSPHandler (CpuIndex
, mSmmMpSyncData
->EffectiveSyncMode
);
1745 APHandler (CpuIndex
, ValidSmi
, mSmmMpSyncData
->EffectiveSyncMode
);
1749 ASSERT (*mSmmMpSyncData
->CpuData
[CpuIndex
].Run
== 0);
1752 // Wait for BSP's signal to exit SMI
1754 while (*mSmmMpSyncData
->AllCpusInSync
) {
1760 SmmCpuFeaturesRendezvousExit (CpuIndex
);
1769 Allocate buffer for SpinLock and Wrapper function buffer.
1773 InitializeDataForMmMp (
1777 gSmmCpuPrivate
->ApWrapperFunc
= AllocatePool (sizeof (PROCEDURE_WRAPPER
) * gSmmCpuPrivate
->SmmCoreEntryContext
.NumberOfCpus
);
1778 ASSERT (gSmmCpuPrivate
->ApWrapperFunc
!= NULL
);
1780 InitializeListHead (&gSmmCpuPrivate
->TokenList
);
1782 AllocateTokenBuffer ();
1786 Allocate buffer for all semaphores and spin locks.
1790 InitializeSmmCpuSemaphores (
1794 UINTN ProcessorCount
;
1796 UINTN GlobalSemaphoresSize
;
1797 UINTN CpuSemaphoresSize
;
1798 UINTN SemaphoreSize
;
1800 UINTN
*SemaphoreBlock
;
1801 UINTN SemaphoreAddr
;
1803 SemaphoreSize
= GetSpinLockProperties ();
1804 ProcessorCount
= gSmmCpuPrivate
->SmmCoreEntryContext
.NumberOfCpus
;
1805 GlobalSemaphoresSize
= (sizeof (SMM_CPU_SEMAPHORE_GLOBAL
) / sizeof (VOID
*)) * SemaphoreSize
;
1806 CpuSemaphoresSize
= (sizeof (SMM_CPU_SEMAPHORE_CPU
) / sizeof (VOID
*)) * ProcessorCount
* SemaphoreSize
;
1807 TotalSize
= GlobalSemaphoresSize
+ CpuSemaphoresSize
;
1808 DEBUG((EFI_D_INFO
, "One Semaphore Size = 0x%x\n", SemaphoreSize
));
1809 DEBUG((EFI_D_INFO
, "Total Semaphores Size = 0x%x\n", TotalSize
));
1810 Pages
= EFI_SIZE_TO_PAGES (TotalSize
);
1811 SemaphoreBlock
= AllocatePages (Pages
);
1812 ASSERT (SemaphoreBlock
!= NULL
);
1813 ZeroMem (SemaphoreBlock
, TotalSize
);
1815 SemaphoreAddr
= (UINTN
)SemaphoreBlock
;
1816 mSmmCpuSemaphores
.SemaphoreGlobal
.Counter
= (UINT32
*)SemaphoreAddr
;
1817 SemaphoreAddr
+= SemaphoreSize
;
1818 mSmmCpuSemaphores
.SemaphoreGlobal
.InsideSmm
= (BOOLEAN
*)SemaphoreAddr
;
1819 SemaphoreAddr
+= SemaphoreSize
;
1820 mSmmCpuSemaphores
.SemaphoreGlobal
.AllCpusInSync
= (BOOLEAN
*)SemaphoreAddr
;
1821 SemaphoreAddr
+= SemaphoreSize
;
1822 mSmmCpuSemaphores
.SemaphoreGlobal
.PFLock
= (SPIN_LOCK
*)SemaphoreAddr
;
1823 SemaphoreAddr
+= SemaphoreSize
;
1824 mSmmCpuSemaphores
.SemaphoreGlobal
.CodeAccessCheckLock
1825 = (SPIN_LOCK
*)SemaphoreAddr
;
1826 SemaphoreAddr
+= SemaphoreSize
;
1828 SemaphoreAddr
= (UINTN
)SemaphoreBlock
+ GlobalSemaphoresSize
;
1829 mSmmCpuSemaphores
.SemaphoreCpu
.Busy
= (SPIN_LOCK
*)SemaphoreAddr
;
1830 SemaphoreAddr
+= ProcessorCount
* SemaphoreSize
;
1831 mSmmCpuSemaphores
.SemaphoreCpu
.Run
= (UINT32
*)SemaphoreAddr
;
1832 SemaphoreAddr
+= ProcessorCount
* SemaphoreSize
;
1833 mSmmCpuSemaphores
.SemaphoreCpu
.Present
= (BOOLEAN
*)SemaphoreAddr
;
1835 mPFLock
= mSmmCpuSemaphores
.SemaphoreGlobal
.PFLock
;
1836 mConfigSmmCodeAccessCheckLock
= mSmmCpuSemaphores
.SemaphoreGlobal
.CodeAccessCheckLock
;
1838 mSemaphoreSize
= SemaphoreSize
;
1842 Initialize un-cacheable data.
1847 InitializeMpSyncData (
1853 if (mSmmMpSyncData
!= NULL
) {
1855 // mSmmMpSyncDataSize includes one structure of SMM_DISPATCHER_MP_SYNC_DATA, one
1856 // CpuData array of SMM_CPU_DATA_BLOCK and one CandidateBsp array of BOOLEAN.
1858 ZeroMem (mSmmMpSyncData
, mSmmMpSyncDataSize
);
1859 mSmmMpSyncData
->CpuData
= (SMM_CPU_DATA_BLOCK
*)((UINT8
*)mSmmMpSyncData
+ sizeof (SMM_DISPATCHER_MP_SYNC_DATA
));
1860 mSmmMpSyncData
->CandidateBsp
= (BOOLEAN
*)(mSmmMpSyncData
->CpuData
+ gSmmCpuPrivate
->SmmCoreEntryContext
.NumberOfCpus
);
1861 if (FeaturePcdGet (PcdCpuSmmEnableBspElection
)) {
1863 // Enable BSP election by setting BspIndex to -1
1865 mSmmMpSyncData
->BspIndex
= (UINT32
)-1;
1867 mSmmMpSyncData
->EffectiveSyncMode
= mCpuSmmSyncMode
;
1869 mSmmMpSyncData
->Counter
= mSmmCpuSemaphores
.SemaphoreGlobal
.Counter
;
1870 mSmmMpSyncData
->InsideSmm
= mSmmCpuSemaphores
.SemaphoreGlobal
.InsideSmm
;
1871 mSmmMpSyncData
->AllCpusInSync
= mSmmCpuSemaphores
.SemaphoreGlobal
.AllCpusInSync
;
1872 ASSERT (mSmmMpSyncData
->Counter
!= NULL
&& mSmmMpSyncData
->InsideSmm
!= NULL
&&
1873 mSmmMpSyncData
->AllCpusInSync
!= NULL
);
1874 *mSmmMpSyncData
->Counter
= 0;
1875 *mSmmMpSyncData
->InsideSmm
= FALSE
;
1876 *mSmmMpSyncData
->AllCpusInSync
= FALSE
;
1878 for (CpuIndex
= 0; CpuIndex
< gSmmCpuPrivate
->SmmCoreEntryContext
.NumberOfCpus
; CpuIndex
++) {
1879 mSmmMpSyncData
->CpuData
[CpuIndex
].Busy
=
1880 (SPIN_LOCK
*)((UINTN
)mSmmCpuSemaphores
.SemaphoreCpu
.Busy
+ mSemaphoreSize
* CpuIndex
);
1881 mSmmMpSyncData
->CpuData
[CpuIndex
].Run
=
1882 (UINT32
*)((UINTN
)mSmmCpuSemaphores
.SemaphoreCpu
.Run
+ mSemaphoreSize
* CpuIndex
);
1883 mSmmMpSyncData
->CpuData
[CpuIndex
].Present
=
1884 (BOOLEAN
*)((UINTN
)mSmmCpuSemaphores
.SemaphoreCpu
.Present
+ mSemaphoreSize
* CpuIndex
);
1885 *(mSmmMpSyncData
->CpuData
[CpuIndex
].Busy
) = 0;
1886 *(mSmmMpSyncData
->CpuData
[CpuIndex
].Run
) = 0;
1887 *(mSmmMpSyncData
->CpuData
[CpuIndex
].Present
) = FALSE
;
1893 Initialize global data for MP synchronization.
1895 @param Stacks Base address of SMI stack buffer for all processors.
1896 @param StackSize Stack size for each processor in SMM.
1897 @param ShadowStackSize Shadow Stack size for each processor in SMM.
1901 InitializeMpServiceData (
1904 IN UINTN ShadowStackSize
1909 UINT8
*GdtTssTables
;
1910 UINTN GdtTableStepSize
;
1911 CPUID_VERSION_INFO_EDX RegEdx
;
1914 // Determine if this CPU supports machine check
1916 AsmCpuid (CPUID_VERSION_INFO
, NULL
, NULL
, NULL
, &RegEdx
.Uint32
);
1917 mMachineCheckSupported
= (BOOLEAN
)(RegEdx
.Bits
.MCA
== 1);
1920 // Allocate memory for all locks and semaphores
1922 InitializeSmmCpuSemaphores ();
1925 // Initialize mSmmMpSyncData
1927 mSmmMpSyncDataSize
= sizeof (SMM_DISPATCHER_MP_SYNC_DATA
) +
1928 (sizeof (SMM_CPU_DATA_BLOCK
) + sizeof (BOOLEAN
)) * gSmmCpuPrivate
->SmmCoreEntryContext
.NumberOfCpus
;
1929 mSmmMpSyncData
= (SMM_DISPATCHER_MP_SYNC_DATA
*) AllocatePages (EFI_SIZE_TO_PAGES (mSmmMpSyncDataSize
));
1930 ASSERT (mSmmMpSyncData
!= NULL
);
1931 mCpuSmmSyncMode
= (SMM_CPU_SYNC_MODE
)PcdGet8 (PcdCpuSmmSyncMode
);
1932 InitializeMpSyncData ();
1935 // Initialize physical address mask
1936 // NOTE: Physical memory above virtual address limit is not supported !!!
1938 AsmCpuid (0x80000008, (UINT32
*)&Index
, NULL
, NULL
, NULL
);
1939 gPhyMask
= LShiftU64 (1, (UINT8
)Index
) - 1;
1940 gPhyMask
&= (1ull << 48) - EFI_PAGE_SIZE
;
1943 // Create page tables
1945 Cr3
= SmmInitPageTable ();
1947 GdtTssTables
= InitGdt (Cr3
, &GdtTableStepSize
);
1950 // Install SMI handler for each CPU
1952 for (Index
= 0; Index
< mMaxNumberOfCpus
; Index
++) {
1955 (UINT32
)mCpuHotPlugData
.SmBase
[Index
],
1956 (VOID
*)((UINTN
)Stacks
+ (StackSize
+ ShadowStackSize
) * Index
),
1958 (UINTN
)(GdtTssTables
+ GdtTableStepSize
* Index
),
1959 gcSmiGdtr
.Limit
+ 1,
1961 gcSmiIdtr
.Limit
+ 1,
1967 // Record current MTRR settings
1969 ZeroMem (&gSmiMtrrs
, sizeof (gSmiMtrrs
));
1970 MtrrGetAllMtrrs (&gSmiMtrrs
);
1977 Register the SMM Foundation entry point.
1979 @param This Pointer to EFI_SMM_CONFIGURATION_PROTOCOL instance
1980 @param SmmEntryPoint SMM Foundation EntryPoint
1982 @retval EFI_SUCCESS Successfully to register SMM foundation entry point
1988 IN CONST EFI_SMM_CONFIGURATION_PROTOCOL
*This
,
1989 IN EFI_SMM_ENTRY_POINT SmmEntryPoint
1993 // Record SMM Foundation EntryPoint, later invoke it on SMI entry vector.
1995 gSmmCpuPrivate
->SmmCoreEntry
= SmmEntryPoint
;
2001 Register the SMM Foundation entry point.
2003 @param[in] Procedure A pointer to the code stream to be run on the designated target AP
2004 of the system. Type EFI_AP_PROCEDURE is defined below in Volume 2
2005 with the related definitions of
2006 EFI_MP_SERVICES_PROTOCOL.StartupAllAPs.
2007 If caller may pass a value of NULL to deregister any existing
2009 @param[in,out] ProcedureArguments Allows the caller to pass a list of parameters to the code that is
2010 run by the AP. It is an optional common mailbox between APs and
2011 the caller to share information
2013 @retval EFI_SUCCESS The Procedure has been set successfully.
2014 @retval EFI_INVALID_PARAMETER The Procedure is NULL but ProcedureArguments not NULL.
2018 RegisterStartupProcedure (
2019 IN EFI_AP_PROCEDURE Procedure
,
2020 IN OUT VOID
*ProcedureArguments OPTIONAL
2023 if (Procedure
== NULL
&& ProcedureArguments
!= NULL
) {
2024 return EFI_INVALID_PARAMETER
;
2026 if (mSmmMpSyncData
== NULL
) {
2027 return EFI_NOT_READY
;
2030 mSmmMpSyncData
->StartupProcedure
= Procedure
;
2031 mSmmMpSyncData
->StartupProcArgs
= ProcedureArguments
;