X-Git-Url: https://git.proxmox.com/?p=mirror_edk2.git;a=blobdiff_plain;f=UefiCpuPkg%2FPiSmmCpuDxeSmm%2FMpService.c;h=a1d16b45705259eda1fdb12d31d534049adcd8ee;hp=952cc87a202649b3fff71b5bd838e965175188c8;hb=e62a0eb61b5a37d46e559dc36b41c18fe9b1dc82;hpb=f85d3ce2efc21da5d874b3e4d880e5682c6fe7cb diff --git a/UefiCpuPkg/PiSmmCpuDxeSmm/MpService.c b/UefiCpuPkg/PiSmmCpuDxeSmm/MpService.c index 952cc87a20..a1d16b4570 100644 --- a/UefiCpuPkg/PiSmmCpuDxeSmm/MpService.c +++ b/UefiCpuPkg/PiSmmCpuDxeSmm/MpService.c @@ -1,7 +1,9 @@ /** @file SMM MP service implementation -Copyright (c) 2009 - 2016, Intel Corporation. All rights reserved.
+Copyright (c) 2009 - 2017, Intel Corporation. All rights reserved.
+Copyright (c) 2017, AMD Incorporated. All rights reserved.
+ This program and the accompanying materials are licensed and made available under the terms and conditions of the BSD License which accompanies this distribution. The full text of the license may be found at @@ -17,10 +19,14 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. // // Slots for all MTRR( FIXED MTRR + VARIABLE MTRR + MTRR_LIB_IA32_MTRR_DEF_TYPE) // -UINT64 gSmiMtrrs[MTRR_NUMBER_OF_FIXED_MTRR + 2 * MTRR_NUMBER_OF_VARIABLE_MTRR + 1]; +MTRR_SETTINGS gSmiMtrrs; UINT64 gPhyMask; SMM_DISPATCHER_MP_SYNC_DATA *mSmmMpSyncData = NULL; UINTN mSmmMpSyncDataSize; +SMM_CPU_SEMAPHORES mSmmCpuSemaphores; +UINTN mSemaphoreSize; +SPIN_LOCK *mPFLock = NULL; +SMM_CPU_SYNC_MODE mCpuSmmSyncMode; /** Performs an atomic compare exchange operation to get semaphore. @@ -120,7 +126,7 @@ WaitForAllAPs ( BspIndex = mSmmMpSyncData->BspIndex; while (NumberOfAPs-- > 0) { - WaitForSemaphore (&mSmmMpSyncData->CpuData[BspIndex].Run); + WaitForSemaphore (mSmmMpSyncData->CpuData[BspIndex].Run); } } @@ -139,8 +145,8 @@ ReleaseAllAPs ( BspIndex = mSmmMpSyncData->BspIndex; for (Index = mMaxNumberOfCpus; Index-- > 0;) { - if (Index != BspIndex && mSmmMpSyncData->CpuData[Index].Present) { - ReleaseSemaphore (&mSmmMpSyncData->CpuData[Index].Run); + if (Index != BspIndex && *(mSmmMpSyncData->CpuData[Index].Present)) { + ReleaseSemaphore (mSmmMpSyncData->CpuData[Index].Run); } } } @@ -163,16 +169,16 @@ AllCpusInSmmWithExceptions ( SMM_CPU_DATA_BLOCK *CpuData; EFI_PROCESSOR_INFORMATION *ProcessorInfo; - ASSERT (mSmmMpSyncData->Counter <= mNumberOfCpus); + ASSERT (*mSmmMpSyncData->Counter <= mNumberOfCpus); - if (mSmmMpSyncData->Counter == mNumberOfCpus) { + if (*mSmmMpSyncData->Counter == mNumberOfCpus) { return TRUE; } CpuData = mSmmMpSyncData->CpuData; ProcessorInfo = gSmmCpuPrivate->ProcessorInfo; for (Index = mMaxNumberOfCpus; Index-- > 0;) { - if (!CpuData[Index].Present && ProcessorInfo[Index].ProcessorId != INVALID_APIC_ID) { + if (!(*(CpuData[Index].Present)) && ProcessorInfo[Index].ProcessorId != INVALID_APIC_ID) { if (((Exceptions & ARRIVAL_EXCEPTION_DELAYED) != 0) && SmmCpuFeaturesGetSmmRegister (Index, SmmRegSmmDelayed) != 0) { continue; } @@ -204,7 +210,7 @@ SmmWaitForApArrival ( UINT64 Timer; UINTN Index; - ASSERT (mSmmMpSyncData->Counter <= mNumberOfCpus); + ASSERT (*mSmmMpSyncData->Counter <= mNumberOfCpus); // // Platform implementor should choose a timeout value appropriately: @@ -243,12 +249,12 @@ SmmWaitForApArrival ( // - In relaxed flow, CheckApArrival() will check SMI disabling status before calling this function. // In both cases, adding SMI-disabling checking code increases overhead. // - if (mSmmMpSyncData->Counter < mNumberOfCpus) { + if (*mSmmMpSyncData->Counter < mNumberOfCpus) { // // Send SMI IPIs to bring outside processors in // for (Index = mMaxNumberOfCpus; Index-- > 0;) { - if (!mSmmMpSyncData->CpuData[Index].Present && gSmmCpuPrivate->ProcessorInfo[Index].ProcessorId != INVALID_APIC_ID) { + if (!(*(mSmmMpSyncData->CpuData[Index].Present)) && gSmmCpuPrivate->ProcessorInfo[Index].ProcessorId != INVALID_APIC_ID) { SendSmiIpi ((UINT32)gSmmCpuPrivate->ProcessorInfo[Index].ProcessorId); } } @@ -279,20 +285,12 @@ ReplaceOSMtrrs ( IN UINTN CpuIndex ) { - PROCESSOR_SMM_DESCRIPTOR *Psd; - UINT64 *SmiMtrrs; - MTRR_SETTINGS *BiosMtrr; - - Psd = (PROCESSOR_SMM_DESCRIPTOR*)(mCpuHotPlugData.SmBase[CpuIndex] + SMM_PSD_OFFSET); - SmiMtrrs = (UINT64*)(UINTN)Psd->MtrrBaseMaskPtr; - SmmCpuFeaturesDisableSmrr (); // // Replace all MTRRs registers // - BiosMtrr = (MTRR_SETTINGS*)SmiMtrrs; - MtrrSetAllMtrrs(BiosMtrr); + MtrrSetAllMtrrs (&gSmiMtrrs); } /** @@ -320,7 +318,7 @@ BSPHandler ( // // Flag BSP's presence // - mSmmMpSyncData->InsideSmm = TRUE; + *mSmmMpSyncData->InsideSmm = TRUE; // // Initialize Debug Agent to start source level debug in BSP handler @@ -330,7 +328,7 @@ BSPHandler ( // // Mark this processor's presence // - mSmmMpSyncData->CpuData[CpuIndex].Present = TRUE; + *(mSmmMpSyncData->CpuData[CpuIndex].Present) = TRUE; // // Clear platform top level SMI status bit before calling SMI handlers. If @@ -358,8 +356,8 @@ BSPHandler ( // // Lock the counter down and retrieve the number of APs // - mSmmMpSyncData->AllCpusInSync = TRUE; - ApCount = LockdownSemaphore (&mSmmMpSyncData->Counter) - 1; + *mSmmMpSyncData->AllCpusInSync = TRUE; + ApCount = LockdownSemaphore (mSmmMpSyncData->Counter) - 1; // // Wait for all APs to get ready for programming MTRRs @@ -409,7 +407,7 @@ BSPHandler ( // // The BUSY lock is initialized to Acquired state // - AcquireSpinLockOrFail (&mSmmMpSyncData->CpuData[CpuIndex].Busy); + AcquireSpinLockOrFail (mSmmMpSyncData->CpuData[CpuIndex].Busy); // // Perform the pre tasks @@ -425,9 +423,9 @@ BSPHandler ( // Make sure all APs have completed their pending none-block tasks // for (Index = mMaxNumberOfCpus; Index-- > 0;) { - if (Index != CpuIndex && mSmmMpSyncData->CpuData[Index].Present) { - AcquireSpinLock (&mSmmMpSyncData->CpuData[Index].Busy); - ReleaseSpinLock (&mSmmMpSyncData->CpuData[Index].Busy);; + if (Index != CpuIndex && *(mSmmMpSyncData->CpuData[Index].Present)) { + AcquireSpinLock (mSmmMpSyncData->CpuData[Index].Busy); + ReleaseSpinLock (mSmmMpSyncData->CpuData[Index].Busy); } } @@ -446,15 +444,15 @@ BSPHandler ( // // Lock the counter down and retrieve the number of APs // - mSmmMpSyncData->AllCpusInSync = TRUE; - ApCount = LockdownSemaphore (&mSmmMpSyncData->Counter) - 1; + *mSmmMpSyncData->AllCpusInSync = TRUE; + ApCount = LockdownSemaphore (mSmmMpSyncData->Counter) - 1; // // Make sure all APs have their Present flag set // while (TRUE) { PresentCount = 0; for (Index = mMaxNumberOfCpus; Index-- > 0;) { - if (mSmmMpSyncData->CpuData[Index].Present) { + if (*(mSmmMpSyncData->CpuData[Index].Present)) { PresentCount ++; } } @@ -467,7 +465,7 @@ BSPHandler ( // // Notify all APs to exit // - mSmmMpSyncData->InsideSmm = FALSE; + *mSmmMpSyncData->InsideSmm = FALSE; ReleaseAllAPs (); // @@ -512,7 +510,7 @@ BSPHandler ( // // Clear the Present flag of BSP // - mSmmMpSyncData->CpuData[CpuIndex].Present = FALSE; + *(mSmmMpSyncData->CpuData[CpuIndex].Present) = FALSE; // // Gather APs to exit SMM synchronously. Note the Present flag is cleared by now but @@ -530,8 +528,8 @@ BSPHandler ( // // Allow APs to check in from this point on // - mSmmMpSyncData->Counter = 0; - mSmmMpSyncData->AllCpusInSync = FALSE; + *mSmmMpSyncData->Counter = 0; + *mSmmMpSyncData->AllCpusInSync = FALSE; } /** @@ -558,12 +556,12 @@ APHandler ( // for (Timer = StartSyncTimer (); !IsSyncTimerTimeout (Timer) && - !mSmmMpSyncData->InsideSmm; + !(*mSmmMpSyncData->InsideSmm); ) { CpuPause (); } - if (!mSmmMpSyncData->InsideSmm) { + if (!(*mSmmMpSyncData->InsideSmm)) { // // BSP timeout in the first round // @@ -584,23 +582,23 @@ APHandler ( // for (Timer = StartSyncTimer (); !IsSyncTimerTimeout (Timer) && - !mSmmMpSyncData->InsideSmm; + !(*mSmmMpSyncData->InsideSmm); ) { CpuPause (); } - if (!mSmmMpSyncData->InsideSmm) { + if (!(*mSmmMpSyncData->InsideSmm)) { // // Give up since BSP is unable to enter SMM // and signal the completion of this AP - WaitForSemaphore (&mSmmMpSyncData->Counter); + WaitForSemaphore (mSmmMpSyncData->Counter); return; } } else { // // Don't know BSP index. Give up without sending IPI to BSP. // - WaitForSemaphore (&mSmmMpSyncData->Counter); + WaitForSemaphore (mSmmMpSyncData->Counter); return; } } @@ -614,20 +612,20 @@ APHandler ( // // Mark this processor's presence // - mSmmMpSyncData->CpuData[CpuIndex].Present = TRUE; + *(mSmmMpSyncData->CpuData[CpuIndex].Present) = TRUE; if (SyncMode == SmmCpuSyncModeTradition || SmmCpuFeaturesNeedConfigureMtrrs()) { // // Notify BSP of arrival at this point // - ReleaseSemaphore (&mSmmMpSyncData->CpuData[BspIndex].Run); + ReleaseSemaphore (mSmmMpSyncData->CpuData[BspIndex].Run); } if (SmmCpuFeaturesNeedConfigureMtrrs()) { // // Wait for the signal from BSP to backup MTRRs // - WaitForSemaphore (&mSmmMpSyncData->CpuData[CpuIndex].Run); + WaitForSemaphore (mSmmMpSyncData->CpuData[CpuIndex].Run); // // Backup OS MTRRs @@ -637,12 +635,12 @@ APHandler ( // // Signal BSP the completion of this AP // - ReleaseSemaphore (&mSmmMpSyncData->CpuData[BspIndex].Run); + ReleaseSemaphore (mSmmMpSyncData->CpuData[BspIndex].Run); // // Wait for BSP's signal to program MTRRs // - WaitForSemaphore (&mSmmMpSyncData->CpuData[CpuIndex].Run); + WaitForSemaphore (mSmmMpSyncData->CpuData[CpuIndex].Run); // // Replace OS MTRRs with SMI MTRRs @@ -652,19 +650,19 @@ APHandler ( // // Signal BSP the completion of this AP // - ReleaseSemaphore (&mSmmMpSyncData->CpuData[BspIndex].Run); + ReleaseSemaphore (mSmmMpSyncData->CpuData[BspIndex].Run); } while (TRUE) { // // Wait for something to happen // - WaitForSemaphore (&mSmmMpSyncData->CpuData[CpuIndex].Run); + WaitForSemaphore (mSmmMpSyncData->CpuData[CpuIndex].Run); // // Check if BSP wants to exit SMM // - if (!mSmmMpSyncData->InsideSmm) { + if (!(*mSmmMpSyncData->InsideSmm)) { break; } @@ -672,7 +670,7 @@ APHandler ( // BUSY should be acquired by SmmStartupThisAp() // ASSERT ( - !AcquireSpinLockOrFail (&mSmmMpSyncData->CpuData[CpuIndex].Busy) + !AcquireSpinLockOrFail (mSmmMpSyncData->CpuData[CpuIndex].Busy) ); // @@ -685,19 +683,19 @@ APHandler ( // // Release BUSY // - ReleaseSpinLock (&mSmmMpSyncData->CpuData[CpuIndex].Busy); + ReleaseSpinLock (mSmmMpSyncData->CpuData[CpuIndex].Busy); } if (SmmCpuFeaturesNeedConfigureMtrrs()) { // // Notify BSP the readiness of this AP to program MTRRs // - ReleaseSemaphore (&mSmmMpSyncData->CpuData[BspIndex].Run); + ReleaseSemaphore (mSmmMpSyncData->CpuData[BspIndex].Run); // // Wait for the signal from BSP to program MTRRs // - WaitForSemaphore (&mSmmMpSyncData->CpuData[CpuIndex].Run); + WaitForSemaphore (mSmmMpSyncData->CpuData[CpuIndex].Run); // // Restore OS MTRRs @@ -709,36 +707,34 @@ APHandler ( // // Notify BSP the readiness of this AP to Reset states/semaphore for this processor // - ReleaseSemaphore (&mSmmMpSyncData->CpuData[BspIndex].Run); + ReleaseSemaphore (mSmmMpSyncData->CpuData[BspIndex].Run); // // Wait for the signal from BSP to Reset states/semaphore for this processor // - WaitForSemaphore (&mSmmMpSyncData->CpuData[CpuIndex].Run); + WaitForSemaphore (mSmmMpSyncData->CpuData[CpuIndex].Run); // // Reset states/semaphore for this processor // - mSmmMpSyncData->CpuData[CpuIndex].Present = FALSE; + *(mSmmMpSyncData->CpuData[CpuIndex].Present) = FALSE; // // Notify BSP the readiness of this AP to exit SMM // - ReleaseSemaphore (&mSmmMpSyncData->CpuData[BspIndex].Run); + ReleaseSemaphore (mSmmMpSyncData->CpuData[BspIndex].Run); } /** Create 4G PageTable in SMRAM. - @param ExtraPages Additional page numbers besides for 4G memory - @param Is32BitPageTable Whether the page table is 32-bit PAE + @param[in] Is32BitPageTable Whether the page table is 32-bit PAE @return PageTable Address **/ UINT32 Gen4GPageTable ( - IN UINTN ExtraPages, IN BOOLEAN Is32BitPageTable ) { @@ -772,10 +768,10 @@ Gen4GPageTable ( // // Allocate the page table // - PageTable = AllocatePageTableMemory (ExtraPages + 5 + PagesNeeded); + PageTable = AllocatePageTableMemory (5 + PagesNeeded); ASSERT (PageTable != NULL); - PageTable = (VOID *)((UINTN)PageTable + EFI_PAGES_TO_SIZE (ExtraPages)); + PageTable = (VOID *)((UINTN)PageTable); Pte = (UINT64*)PageTable; // @@ -787,7 +783,8 @@ Gen4GPageTable ( // Set Page Directory Pointers // for (Index = 0; Index < 4; Index++) { - Pte[Index] = (UINTN)PageTable + EFI_PAGE_SIZE * (Index + 1) + (Is32BitPageTable ? IA32_PAE_PDPTE_ATTRIBUTE_BITS : PAGE_ATTRIBUTE_BITS); + Pte[Index] = ((UINTN)PageTable + EFI_PAGE_SIZE * (Index + 1)) | mAddressEncMask | + (Is32BitPageTable ? IA32_PAE_PDPTE_ATTRIBUTE_BITS : PAGE_ATTRIBUTE_BITS); } Pte += EFI_PAGE_SIZE / sizeof (*Pte); @@ -795,7 +792,7 @@ Gen4GPageTable ( // Fill in Page Directory Entries // for (Index = 0; Index < EFI_PAGE_SIZE * 4 / sizeof (*Pte); Index++) { - Pte[Index] = (Index << 21) | IA32_PG_PS | PAGE_ATTRIBUTE_BITS; + Pte[Index] = (Index << 21) | mAddressEncMask | IA32_PG_PS | PAGE_ATTRIBUTE_BITS; } if (FeaturePcdGet (PcdCpuSmmStackGuard)) { @@ -803,8 +800,8 @@ Gen4GPageTable ( GuardPage = mSmmStackArrayBase + EFI_PAGE_SIZE; Pdpte = (UINT64*)PageTable; for (PageIndex = Low2MBoundary; PageIndex <= High2MBoundary; PageIndex += SIZE_2MB) { - Pte = (UINT64*)(UINTN)(Pdpte[BitFieldRead32 ((UINT32)PageIndex, 30, 31)] & ~(EFI_PAGE_SIZE - 1)); - Pte[BitFieldRead32 ((UINT32)PageIndex, 21, 29)] = (UINT64)Pages | PAGE_ATTRIBUTE_BITS; + Pte = (UINT64*)(UINTN)(Pdpte[BitFieldRead32 ((UINT32)PageIndex, 30, 31)] & ~mAddressEncMask & ~(EFI_PAGE_SIZE - 1)); + Pte[BitFieldRead32 ((UINT32)PageIndex, 21, 29)] = (UINT64)Pages | mAddressEncMask | PAGE_ATTRIBUTE_BITS; // // Fill in Page Table Entries // @@ -815,13 +812,13 @@ Gen4GPageTable ( // // Mark the guard page as non-present // - Pte[Index] = PageAddress; + Pte[Index] = PageAddress | mAddressEncMask; GuardPage += mSmmStackSize; if (GuardPage > mSmmStackArrayEnd) { GuardPage = 0; } } else { - Pte[Index] = PageAddress | PAGE_ATTRIBUTE_BITS; + Pte[Index] = PageAddress | mAddressEncMask | PAGE_ATTRIBUTE_BITS; } PageAddress+= EFI_PAGE_SIZE; } @@ -833,74 +830,94 @@ Gen4GPageTable ( } /** - Set memory cache ability. + Schedule a procedure to run on the specified CPU. - @param PageTable PageTable Address - @param Address Memory Address to change cache ability - @param Cacheability Cache ability to set + @param[in] Procedure The address of the procedure to run + @param[in] CpuIndex Target CPU Index + @param[in, out] ProcArguments The parameter to pass to the procedure + @param[in] BlockingMode Startup AP in blocking mode or not + + @retval EFI_INVALID_PARAMETER CpuNumber not valid + @retval EFI_INVALID_PARAMETER CpuNumber specifying BSP + @retval EFI_INVALID_PARAMETER The AP specified by CpuNumber did not enter SMM + @retval EFI_INVALID_PARAMETER The AP specified by CpuNumber is busy + @retval EFI_SUCCESS The procedure has been successfully scheduled **/ -VOID -SetCacheability ( - IN UINT64 *PageTable, - IN UINTN Address, - IN UINT8 Cacheability +EFI_STATUS +InternalSmmStartupThisAp ( + IN EFI_AP_PROCEDURE Procedure, + IN UINTN CpuIndex, + IN OUT VOID *ProcArguments OPTIONAL, + IN BOOLEAN BlockingMode ) { - UINTN PTIndex; - VOID *NewPageTableAddress; - UINT64 *NewPageTable; - UINTN Index; - - ASSERT ((Address & EFI_PAGE_MASK) == 0); - - if (sizeof (UINTN) == sizeof (UINT64)) { - PTIndex = (UINTN)RShiftU64 (Address, 39) & 0x1ff; - ASSERT (PageTable[PTIndex] & IA32_PG_P); - PageTable = (UINT64*)(UINTN)(PageTable[PTIndex] & gPhyMask); + if (CpuIndex >= gSmmCpuPrivate->SmmCoreEntryContext.NumberOfCpus) { + DEBUG((DEBUG_ERROR, "CpuIndex(%d) >= gSmmCpuPrivate->SmmCoreEntryContext.NumberOfCpus(%d)\n", CpuIndex, gSmmCpuPrivate->SmmCoreEntryContext.NumberOfCpus)); + return EFI_INVALID_PARAMETER; + } + if (CpuIndex == gSmmCpuPrivate->SmmCoreEntryContext.CurrentlyExecutingCpu) { + DEBUG((DEBUG_ERROR, "CpuIndex(%d) == gSmmCpuPrivate->SmmCoreEntryContext.CurrentlyExecutingCpu\n", CpuIndex)); + return EFI_INVALID_PARAMETER; + } + if (!(*(mSmmMpSyncData->CpuData[CpuIndex].Present))) { + if (mSmmMpSyncData->EffectiveSyncMode == SmmCpuSyncModeTradition) { + DEBUG((DEBUG_ERROR, "!mSmmMpSyncData->CpuData[%d].Present\n", CpuIndex)); + } + return EFI_INVALID_PARAMETER; + } + if (gSmmCpuPrivate->Operation[CpuIndex] == SmmCpuRemove) { + if (!FeaturePcdGet (PcdCpuHotPlugSupport)) { + DEBUG((DEBUG_ERROR, "gSmmCpuPrivate->Operation[%d] == SmmCpuRemove\n", CpuIndex)); + } + return EFI_INVALID_PARAMETER; } - PTIndex = (UINTN)RShiftU64 (Address, 30) & 0x1ff; - ASSERT (PageTable[PTIndex] & IA32_PG_P); - PageTable = (UINT64*)(UINTN)(PageTable[PTIndex] & gPhyMask); + if (BlockingMode) { + AcquireSpinLock (mSmmMpSyncData->CpuData[CpuIndex].Busy); + } else { + if (!AcquireSpinLockOrFail (mSmmMpSyncData->CpuData[CpuIndex].Busy)) { + DEBUG((DEBUG_ERROR, "mSmmMpSyncData->CpuData[%d].Busy\n", CpuIndex)); + return EFI_INVALID_PARAMETER; + } + } - // - // A perfect implementation should check the original cacheability with the - // one being set, and break a 2M page entry into pieces only when they - // disagreed. - // - PTIndex = (UINTN)RShiftU64 (Address, 21) & 0x1ff; - if ((PageTable[PTIndex] & IA32_PG_PS) != 0) { - // - // Allocate a page from SMRAM - // - NewPageTableAddress = AllocatePageTableMemory (1); - ASSERT (NewPageTableAddress != NULL); + mSmmMpSyncData->CpuData[CpuIndex].Procedure = Procedure; + mSmmMpSyncData->CpuData[CpuIndex].Parameter = ProcArguments; + ReleaseSemaphore (mSmmMpSyncData->CpuData[CpuIndex].Run); - NewPageTable = (UINT64 *)NewPageTableAddress; + if (BlockingMode) { + AcquireSpinLock (mSmmMpSyncData->CpuData[CpuIndex].Busy); + ReleaseSpinLock (mSmmMpSyncData->CpuData[CpuIndex].Busy); + } + return EFI_SUCCESS; +} - for (Index = 0; Index < 0x200; Index++) { - NewPageTable[Index] = PageTable[PTIndex]; - if ((NewPageTable[Index] & IA32_PG_PAT_2M) != 0) { - NewPageTable[Index] &= ~((UINT64)IA32_PG_PAT_2M); - NewPageTable[Index] |= (UINT64)IA32_PG_PAT_4K; - } - NewPageTable[Index] |= (UINT64)(Index << EFI_PAGE_SHIFT); - } +/** + Schedule a procedure to run on the specified CPU in blocking mode. - PageTable[PTIndex] = ((UINTN)NewPageTableAddress & gPhyMask) | PAGE_ATTRIBUTE_BITS; - } + @param[in] Procedure The address of the procedure to run + @param[in] CpuIndex Target CPU Index + @param[in, out] ProcArguments The parameter to pass to the procedure - ASSERT (PageTable[PTIndex] & IA32_PG_P); - PageTable = (UINT64*)(UINTN)(PageTable[PTIndex] & gPhyMask); + @retval EFI_INVALID_PARAMETER CpuNumber not valid + @retval EFI_INVALID_PARAMETER CpuNumber specifying BSP + @retval EFI_INVALID_PARAMETER The AP specified by CpuNumber did not enter SMM + @retval EFI_INVALID_PARAMETER The AP specified by CpuNumber is busy + @retval EFI_SUCCESS The procedure has been successfully scheduled - PTIndex = (UINTN)RShiftU64 (Address, 12) & 0x1ff; - ASSERT (PageTable[PTIndex] & IA32_PG_P); - PageTable[PTIndex] &= ~((UINT64)((IA32_PG_PAT_4K | IA32_PG_CD | IA32_PG_WT))); - PageTable[PTIndex] |= (UINT64)Cacheability; +**/ +EFI_STATUS +EFIAPI +SmmBlockingStartupThisAp ( + IN EFI_AP_PROCEDURE Procedure, + IN UINTN CpuIndex, + IN OUT VOID *ProcArguments OPTIONAL + ) +{ + return InternalSmmStartupThisAp(Procedure, CpuIndex, ProcArguments, TRUE); } - /** Schedule a procedure to run on the specified CPU. @@ -923,23 +940,7 @@ SmmStartupThisAp ( IN OUT VOID *ProcArguments OPTIONAL ) { - if (CpuIndex >= gSmmCpuPrivate->SmmCoreEntryContext.NumberOfCpus || - CpuIndex == gSmmCpuPrivate->SmmCoreEntryContext.CurrentlyExecutingCpu || - !mSmmMpSyncData->CpuData[CpuIndex].Present || - gSmmCpuPrivate->Operation[CpuIndex] == SmmCpuRemove || - !AcquireSpinLockOrFail (&mSmmMpSyncData->CpuData[CpuIndex].Busy)) { - return EFI_INVALID_PARAMETER; - } - - mSmmMpSyncData->CpuData[CpuIndex].Procedure = Procedure; - mSmmMpSyncData->CpuData[CpuIndex].Parameter = ProcArguments; - ReleaseSemaphore (&mSmmMpSyncData->CpuData[CpuIndex].Run); - - if (FeaturePcdGet (PcdCpuSmmBlockStartupThisAp)) { - AcquireSpinLock (&mSmmMpSyncData->CpuData[CpuIndex].Busy); - ReleaseSpinLock (&mSmmMpSyncData->CpuData[CpuIndex].Busy); - } - return EFI_SUCCESS; + return InternalSmmStartupThisAp(Procedure, CpuIndex, ProcArguments, FeaturePcdGet (PcdCpuSmmBlockStartupThisAp)); } /** @@ -961,6 +962,7 @@ CpuSmmDebugEntry ( SMRAM_SAVE_STATE_MAP *CpuSaveState; if (FeaturePcdGet (PcdCpuSmmDebug)) { + ASSERT(CpuIndex < mMaxNumberOfCpus); CpuSaveState = (SMRAM_SAVE_STATE_MAP *)gSmmCpuPrivate->CpuSaveState[CpuIndex]; if (mSmmSaveStateRegisterLma == EFI_SMM_SAVE_STATE_REGISTER_LMA_32BIT) { AsmWriteDr6 (CpuSaveState->x86._DR6); @@ -990,6 +992,7 @@ CpuSmmDebugExit ( SMRAM_SAVE_STATE_MAP *CpuSaveState; if (FeaturePcdGet (PcdCpuSmmDebug)) { + ASSERT(CpuIndex < mMaxNumberOfCpus); CpuSaveState = (SMRAM_SAVE_STATE_MAP *)gSmmCpuPrivate->CpuSaveState[CpuIndex]; if (mSmmSaveStateRegisterLma == EFI_SMM_SAVE_STATE_REGISTER_LMA_32BIT) { CpuSaveState->x86._DR7 = (UINT32)AsmReadDr7 (); @@ -1019,8 +1022,8 @@ SmiRendezvous ( BOOLEAN BspInProgress; UINTN Index; UINTN Cr2; - BOOLEAN XdDisableFlag; - MSR_IA32_MISC_ENABLE_REGISTER MiscEnableMsr; + + ASSERT(CpuIndex < mMaxNumberOfCpus); // // Save Cr2 because Page Fault exception in SMM may override its value @@ -1041,7 +1044,7 @@ SmiRendezvous ( // Determine if BSP has been already in progress. Note this must be checked after // ValidSmi because BSP may clear a valid SMI source after checking in. // - BspInProgress = mSmmMpSyncData->InsideSmm; + BspInProgress = *mSmmMpSyncData->InsideSmm; if (!BspInProgress && !ValidSmi) { // @@ -1056,7 +1059,7 @@ SmiRendezvous ( // // Signal presence of this processor // - if (ReleaseSemaphore (&mSmmMpSyncData->Counter) == 0) { + if (ReleaseSemaphore (mSmmMpSyncData->Counter) == 0) { // // BSP has already ended the synchronization, so QUIT!!! // @@ -1064,7 +1067,7 @@ SmiRendezvous ( // // Wait for BSP's signal to finish SMI // - while (mSmmMpSyncData->AllCpusInSync) { + while (*mSmmMpSyncData->AllCpusInSync) { CpuPause (); } goto Exit; @@ -1076,21 +1079,7 @@ SmiRendezvous ( // E.g., with Relaxed AP flow, SmmStartupThisAp() may be called immediately // after AP's present flag is detected. // - InitializeSpinLock (&mSmmMpSyncData->CpuData[CpuIndex].Busy); - } - - // - // Try to enable XD - // - XdDisableFlag = FALSE; - if (mXdSupported) { - MiscEnableMsr.Uint64 = AsmReadMsr64 (MSR_IA32_MISC_ENABLE); - if (MiscEnableMsr.Bits.XD == 1) { - XdDisableFlag = TRUE; - MiscEnableMsr.Bits.XD = 0; - AsmWriteMsr64 (MSR_IA32_MISC_ENABLE, MiscEnableMsr.Uint64); - } - ActivateXd (); + InitializeSpinLock (mSmmMpSyncData->CpuData[CpuIndex].Busy); } if (FeaturePcdGet (PcdCpuSmmProfileEnable)) { @@ -1166,22 +1155,13 @@ SmiRendezvous ( } } - ASSERT (mSmmMpSyncData->CpuData[CpuIndex].Run == 0); + ASSERT (*mSmmMpSyncData->CpuData[CpuIndex].Run == 0); // // Wait for BSP's signal to exit SMI // - while (mSmmMpSyncData->AllCpusInSync) { + while (*mSmmMpSyncData->AllCpusInSync) { CpuPause (); - } - - // - // Restore XD - // - if (XdDisableFlag) { - MiscEnableMsr.Uint64 = AsmReadMsr64 (MSR_IA32_MISC_ENABLE); - MiscEnableMsr.Bits.XD = 1; - AsmWriteMsr64 (MSR_IA32_MISC_ENABLE, MiscEnableMsr.Uint64); } } @@ -1193,6 +1173,72 @@ Exit: AsmWriteCr2 (Cr2); } +/** + Allocate buffer for all semaphores and spin locks. + +**/ +VOID +InitializeSmmCpuSemaphores ( + VOID + ) +{ + UINTN ProcessorCount; + UINTN TotalSize; + UINTN GlobalSemaphoresSize; + UINTN CpuSemaphoresSize; + UINTN MsrSemahporeSize; + UINTN SemaphoreSize; + UINTN Pages; + UINTN *SemaphoreBlock; + UINTN SemaphoreAddr; + + SemaphoreSize = GetSpinLockProperties (); + ProcessorCount = gSmmCpuPrivate->SmmCoreEntryContext.NumberOfCpus; + GlobalSemaphoresSize = (sizeof (SMM_CPU_SEMAPHORE_GLOBAL) / sizeof (VOID *)) * SemaphoreSize; + CpuSemaphoresSize = (sizeof (SMM_CPU_SEMAPHORE_CPU) / sizeof (VOID *)) * ProcessorCount * SemaphoreSize; + MsrSemahporeSize = MSR_SPIN_LOCK_INIT_NUM * SemaphoreSize; + TotalSize = GlobalSemaphoresSize + CpuSemaphoresSize + MsrSemahporeSize; + DEBUG((EFI_D_INFO, "One Semaphore Size = 0x%x\n", SemaphoreSize)); + DEBUG((EFI_D_INFO, "Total Semaphores Size = 0x%x\n", TotalSize)); + Pages = EFI_SIZE_TO_PAGES (TotalSize); + SemaphoreBlock = AllocatePages (Pages); + ASSERT (SemaphoreBlock != NULL); + ZeroMem (SemaphoreBlock, TotalSize); + + SemaphoreAddr = (UINTN)SemaphoreBlock; + mSmmCpuSemaphores.SemaphoreGlobal.Counter = (UINT32 *)SemaphoreAddr; + SemaphoreAddr += SemaphoreSize; + mSmmCpuSemaphores.SemaphoreGlobal.InsideSmm = (BOOLEAN *)SemaphoreAddr; + SemaphoreAddr += SemaphoreSize; + mSmmCpuSemaphores.SemaphoreGlobal.AllCpusInSync = (BOOLEAN *)SemaphoreAddr; + SemaphoreAddr += SemaphoreSize; + mSmmCpuSemaphores.SemaphoreGlobal.PFLock = (SPIN_LOCK *)SemaphoreAddr; + SemaphoreAddr += SemaphoreSize; + mSmmCpuSemaphores.SemaphoreGlobal.CodeAccessCheckLock + = (SPIN_LOCK *)SemaphoreAddr; + SemaphoreAddr += SemaphoreSize; + mSmmCpuSemaphores.SemaphoreGlobal.MemoryMappedLock + = (SPIN_LOCK *)SemaphoreAddr; + + SemaphoreAddr = (UINTN)SemaphoreBlock + GlobalSemaphoresSize; + mSmmCpuSemaphores.SemaphoreCpu.Busy = (SPIN_LOCK *)SemaphoreAddr; + SemaphoreAddr += ProcessorCount * SemaphoreSize; + mSmmCpuSemaphores.SemaphoreCpu.Run = (UINT32 *)SemaphoreAddr; + SemaphoreAddr += ProcessorCount * SemaphoreSize; + mSmmCpuSemaphores.SemaphoreCpu.Present = (BOOLEAN *)SemaphoreAddr; + + SemaphoreAddr = (UINTN)SemaphoreBlock + GlobalSemaphoresSize + CpuSemaphoresSize; + mSmmCpuSemaphores.SemaphoreMsr.Msr = (SPIN_LOCK *)SemaphoreAddr; + mSmmCpuSemaphores.SemaphoreMsr.AvailableCounter = + ((UINTN)SemaphoreBlock + Pages * SIZE_4KB - SemaphoreAddr) / SemaphoreSize; + ASSERT (mSmmCpuSemaphores.SemaphoreMsr.AvailableCounter >= MSR_SPIN_LOCK_INIT_NUM); + + mPFLock = mSmmCpuSemaphores.SemaphoreGlobal.PFLock; + mConfigSmmCodeAccessCheckLock = mSmmCpuSemaphores.SemaphoreGlobal.CodeAccessCheckLock; + mMemoryMappedLock = mSmmCpuSemaphores.SemaphoreGlobal.MemoryMappedLock; + + mSemaphoreSize = SemaphoreSize; +} /** Initialize un-cacheable data. @@ -1204,7 +1250,13 @@ InitializeMpSyncData ( VOID ) { + UINTN CpuIndex; + if (mSmmMpSyncData != NULL) { + // + // mSmmMpSyncDataSize includes one structure of SMM_DISPATCHER_MP_SYNC_DATA, one + // CpuData array of SMM_CPU_DATA_BLOCK and one CandidateBsp array of BOOLEAN. + // ZeroMem (mSmmMpSyncData, mSmmMpSyncDataSize); mSmmMpSyncData->CpuData = (SMM_CPU_DATA_BLOCK *)((UINT8 *)mSmmMpSyncData + sizeof (SMM_DISPATCHER_MP_SYNC_DATA)); mSmmMpSyncData->CandidateBsp = (BOOLEAN *)(mSmmMpSyncData->CpuData + gSmmCpuPrivate->SmmCoreEntryContext.NumberOfCpus); @@ -1214,7 +1266,28 @@ InitializeMpSyncData ( // mSmmMpSyncData->BspIndex = (UINT32)-1; } - mSmmMpSyncData->EffectiveSyncMode = (SMM_CPU_SYNC_MODE) PcdGet8 (PcdCpuSmmSyncMode); + mSmmMpSyncData->EffectiveSyncMode = mCpuSmmSyncMode; + + mSmmMpSyncData->Counter = mSmmCpuSemaphores.SemaphoreGlobal.Counter; + mSmmMpSyncData->InsideSmm = mSmmCpuSemaphores.SemaphoreGlobal.InsideSmm; + mSmmMpSyncData->AllCpusInSync = mSmmCpuSemaphores.SemaphoreGlobal.AllCpusInSync; + ASSERT (mSmmMpSyncData->Counter != NULL && mSmmMpSyncData->InsideSmm != NULL && + mSmmMpSyncData->AllCpusInSync != NULL); + *mSmmMpSyncData->Counter = 0; + *mSmmMpSyncData->InsideSmm = FALSE; + *mSmmMpSyncData->AllCpusInSync = FALSE; + + for (CpuIndex = 0; CpuIndex < gSmmCpuPrivate->SmmCoreEntryContext.NumberOfCpus; CpuIndex ++) { + mSmmMpSyncData->CpuData[CpuIndex].Busy = + (SPIN_LOCK *)((UINTN)mSmmCpuSemaphores.SemaphoreCpu.Busy + mSemaphoreSize * CpuIndex); + mSmmMpSyncData->CpuData[CpuIndex].Run = + (UINT32 *)((UINTN)mSmmCpuSemaphores.SemaphoreCpu.Run + mSemaphoreSize * CpuIndex); + mSmmMpSyncData->CpuData[CpuIndex].Present = + (BOOLEAN *)((UINTN)mSmmCpuSemaphores.SemaphoreCpu.Present + mSemaphoreSize * CpuIndex); + *(mSmmMpSyncData->CpuData[CpuIndex].Busy) = 0; + *(mSmmMpSyncData->CpuData[CpuIndex].Run) = 0; + *(mSmmMpSyncData->CpuData[CpuIndex].Present) = FALSE; + } } } @@ -1233,11 +1306,24 @@ InitializeMpServiceData ( { UINT32 Cr3; UINTN Index; - MTRR_SETTINGS *Mtrr; - PROCESSOR_SMM_DESCRIPTOR *Psd; UINT8 *GdtTssTables; UINTN GdtTableStepSize; + // + // Allocate memory for all locks and semaphores + // + InitializeSmmCpuSemaphores (); + + // + // Initialize mSmmMpSyncData + // + mSmmMpSyncDataSize = sizeof (SMM_DISPATCHER_MP_SYNC_DATA) + + (sizeof (SMM_CPU_DATA_BLOCK) + sizeof (BOOLEAN)) * gSmmCpuPrivate->SmmCoreEntryContext.NumberOfCpus; + mSmmMpSyncData = (SMM_DISPATCHER_MP_SYNC_DATA*) AllocatePages (EFI_SIZE_TO_PAGES (mSmmMpSyncDataSize)); + ASSERT (mSmmMpSyncData != NULL); + mCpuSmmSyncMode = (SMM_CPU_SYNC_MODE)PcdGet8 (PcdCpuSmmSyncMode); + InitializeMpSyncData (); + // // Initialize physical address mask // NOTE: Physical memory above virtual address limit is not supported !!! @@ -1254,45 +1340,27 @@ InitializeMpServiceData ( GdtTssTables = InitGdt (Cr3, &GdtTableStepSize); // - // Initialize PROCESSOR_SMM_DESCRIPTOR for each CPU + // Install SMI handler for each CPU // for (Index = 0; Index < mMaxNumberOfCpus; Index++) { - Psd = (PROCESSOR_SMM_DESCRIPTOR *)(VOID *)(UINTN)(mCpuHotPlugData.SmBase[Index] + SMM_PSD_OFFSET); - CopyMem (Psd, &gcPsd, sizeof (gcPsd)); - Psd->SmmGdtPtr = (UINT64)(UINTN)(GdtTssTables + GdtTableStepSize * Index); - Psd->SmmGdtSize = gcSmiGdtr.Limit + 1; - - // - // Install SMI handler - // InstallSmiHandler ( Index, (UINT32)mCpuHotPlugData.SmBase[Index], (VOID*)((UINTN)Stacks + (StackSize * Index)), StackSize, - (UINTN)Psd->SmmGdtPtr, - Psd->SmmGdtSize, + (UINTN)(GdtTssTables + GdtTableStepSize * Index), + gcSmiGdtr.Limit + 1, gcSmiIdtr.Base, gcSmiIdtr.Limit + 1, Cr3 ); } - // - // Initialize mSmmMpSyncData - // - mSmmMpSyncDataSize = sizeof (SMM_DISPATCHER_MP_SYNC_DATA) + - (sizeof (SMM_CPU_DATA_BLOCK) + sizeof (BOOLEAN)) * gSmmCpuPrivate->SmmCoreEntryContext.NumberOfCpus; - mSmmMpSyncData = (SMM_DISPATCHER_MP_SYNC_DATA*) AllocatePages (EFI_SIZE_TO_PAGES (mSmmMpSyncDataSize)); - ASSERT (mSmmMpSyncData != NULL); - InitializeMpSyncData (); - // // Record current MTRR settings // - ZeroMem(gSmiMtrrs, sizeof (gSmiMtrrs)); - Mtrr = (MTRR_SETTINGS*)gSmiMtrrs; - MtrrGetAllMtrrs (Mtrr); + ZeroMem (&gSmiMtrrs, sizeof (gSmiMtrrs)); + MtrrGetAllMtrrs (&gSmiMtrrs); return Cr3; }