From 2e6ca59e332f691195be869e87a8737c20bc25a8 Mon Sep 17 00:00:00 2001 From: Jiaxin Wu Date: Mon, 8 Jul 2024 12:48:32 +0800 Subject: [PATCH] UefiCpuPkg/PiSmmCpuDxeSmm: Avoid PcdCpuSmmProfileEnable check in MM For MM, gMmProfileDataHobGuid Memory Allocation HOB is defined to indicate SMM profile feature enabled or not. If the HOB exist, SMM profile base address and size will be returned in the HOB, so no need to consume the PcdCpuSmmProfileEnable feature PCD to check enable or disable. To achieve above purpose, Add the IsSmmProfileEnabled () function. With this change, Both MM and SMM can use the new function for SMM profile feature check. Signed-off-by: Jiaxin Wu Cc: Ray Ni Cc: Rahul Kumar Cc: Gerd Hoffmann Cc: Star Zeng Cc: Dun Tan Cc: Hongbin1 Zhang Cc: Wei6 Xu Cc: Yuanhao Xie --- UefiCpuPkg/PiSmmCpuDxeSmm/Ia32/PageTbl.c | 4 ++-- .../PiSmmCpuDxeSmm/Ia32/SmiException.nasm | 1 - UefiCpuPkg/PiSmmCpuDxeSmm/MpService.c | 4 ++-- .../PiSmmCpuDxeSmm/NonMmramMapStandaloneMm.c | 6 ++++- UefiCpuPkg/PiSmmCpuDxeSmm/PiSmmCpuCommon.c | 5 ++++ UefiCpuPkg/PiSmmCpuDxeSmm/PiSmmCpuCommon.h | 12 ++++++++++ UefiCpuPkg/PiSmmCpuDxeSmm/PiSmmCpuDxeSmm.c | 21 +++++++++++++--- .../PiSmmCpuDxeSmm/PiSmmCpuStandaloneMm.c | 24 ++++++++++++++++++- .../PiSmmCpuDxeSmm/SmmCpuMemoryManagement.c | 4 ++-- UefiCpuPkg/PiSmmCpuDxeSmm/SmmProfile.c | 9 +++++-- UefiCpuPkg/PiSmmCpuDxeSmm/SmmProfile.h | 4 ++++ UefiCpuPkg/PiSmmCpuDxeSmm/X64/PageTbl.c | 6 ++--- 12 files changed, 83 insertions(+), 17 deletions(-) diff --git a/UefiCpuPkg/PiSmmCpuDxeSmm/Ia32/PageTbl.c b/UefiCpuPkg/PiSmmCpuDxeSmm/Ia32/PageTbl.c index 2fc55ea3c2..1294485c60 100644 --- a/UefiCpuPkg/PiSmmCpuDxeSmm/Ia32/PageTbl.c +++ b/UefiCpuPkg/PiSmmCpuDxeSmm/Ia32/PageTbl.c @@ -33,7 +33,7 @@ SmmInitPageTable ( mPhysicalAddressBits = 32; mPagingMode = PagingPae; - if (FeaturePcdGet (PcdCpuSmmProfileEnable) || + if (mSmmProfileEnabled || HEAP_GUARD_NONSTOP_MODE || NULL_DETECTION_NONSTOP_MODE) { @@ -187,7 +187,7 @@ SmiPFHandler ( } } - if (FeaturePcdGet (PcdCpuSmmProfileEnable)) { + if (mSmmProfileEnabled) { SmmProfilePFHandler ( SystemContext.SystemContextIa32->Eip, SystemContext.SystemContextIa32->ExceptionData diff --git a/UefiCpuPkg/PiSmmCpuDxeSmm/Ia32/SmiException.nasm b/UefiCpuPkg/PiSmmCpuDxeSmm/Ia32/SmiException.nasm index e7b85a9949..208d6bc2ee 100644 --- a/UefiCpuPkg/PiSmmCpuDxeSmm/Ia32/SmiException.nasm +++ b/UefiCpuPkg/PiSmmCpuDxeSmm/Ia32/SmiException.nasm @@ -12,7 +12,6 @@ ; ;------------------------------------------------------------------------------- -extern ASM_PFX(FeaturePcdGet (PcdCpuSmmProfileEnable)) extern ASM_PFX(SmiPFHandler) extern ASM_PFX(mSetupDebugTrap) diff --git a/UefiCpuPkg/PiSmmCpuDxeSmm/MpService.c b/UefiCpuPkg/PiSmmCpuDxeSmm/MpService.c index 819b35a35d..210b23af21 100644 --- a/UefiCpuPkg/PiSmmCpuDxeSmm/MpService.c +++ b/UefiCpuPkg/PiSmmCpuDxeSmm/MpService.c @@ -1616,7 +1616,7 @@ SmiRendezvous ( InitializeSpinLock (mSmmMpSyncData->CpuData[CpuIndex].Busy); } - if (FeaturePcdGet (PcdCpuSmmProfileEnable)) { + if (mSmmProfileEnabled) { ActivateSmmProfile (CpuIndex); } @@ -1677,7 +1677,7 @@ SmiRendezvous ( } } - if (FeaturePcdGet (PcdCpuSmmProfileEnable)) { + if (mSmmProfileEnabled) { SmmProfileRecordSmiNum (); } diff --git a/UefiCpuPkg/PiSmmCpuDxeSmm/NonMmramMapStandaloneMm.c b/UefiCpuPkg/PiSmmCpuDxeSmm/NonMmramMapStandaloneMm.c index 70e94a6b76..dba7db0d00 100644 --- a/UefiCpuPkg/PiSmmCpuDxeSmm/NonMmramMapStandaloneMm.c +++ b/UefiCpuPkg/PiSmmCpuDxeSmm/NonMmramMapStandaloneMm.c @@ -11,6 +11,7 @@ SPDX-License-Identifier: BSD-2-Clause-Patent Get SmmProfileData. @param[in, out] Size Return Size of SmmProfileData. + 0 means the gMmProfileDataHobGuid does not exist. @return Address of SmmProfileData @@ -39,7 +40,10 @@ GetSmmProfileData ( SmmProfileDataHob.Raw = GetNextHob (EFI_HOB_TYPE_MEMORY_ALLOCATION, GET_NEXT_HOB (SmmProfileDataHob)); } - ASSERT (SmmProfileDataHob.Raw != NULL); + if (SmmProfileDataHob.Raw == NULL) { + *Size = 0; + return 0; + } *Size = SmmProfileDataHob.MemoryAllocation->AllocDescriptor.MemoryLength; diff --git a/UefiCpuPkg/PiSmmCpuDxeSmm/PiSmmCpuCommon.c b/UefiCpuPkg/PiSmmCpuDxeSmm/PiSmmCpuCommon.c index c5921a6de8..c6df9358eb 100644 --- a/UefiCpuPkg/PiSmmCpuDxeSmm/PiSmmCpuCommon.c +++ b/UefiCpuPkg/PiSmmCpuDxeSmm/PiSmmCpuCommon.c @@ -1153,6 +1153,11 @@ PiSmmCpuEntryCommon ( // InitializeSmmTimer (); + // + // Initialize mSmmProfileEnabled + // + mSmmProfileEnabled = IsSmmProfileEnabled (); + // // Initialize MP globals // diff --git a/UefiCpuPkg/PiSmmCpuDxeSmm/PiSmmCpuCommon.h b/UefiCpuPkg/PiSmmCpuDxeSmm/PiSmmCpuCommon.h index d7a645fb5b..0dccf7ca65 100644 --- a/UefiCpuPkg/PiSmmCpuDxeSmm/PiSmmCpuCommon.h +++ b/UefiCpuPkg/PiSmmCpuDxeSmm/PiSmmCpuCommon.h @@ -830,6 +830,18 @@ SmiPFHandler ( IN EFI_SYSTEM_CONTEXT SystemContext ); +/** + Check SmmProfile is enabled or not. + + @return TRUE SmmProfile is enabled. + FALSE SmmProfile is not enabled. + +**/ +BOOLEAN +IsSmmProfileEnabled ( + VOID + ); + /** Perform the remaining tasks. diff --git a/UefiCpuPkg/PiSmmCpuDxeSmm/PiSmmCpuDxeSmm.c b/UefiCpuPkg/PiSmmCpuDxeSmm/PiSmmCpuDxeSmm.c index f544858c19..50aeefb948 100644 --- a/UefiCpuPkg/PiSmmCpuDxeSmm/PiSmmCpuDxeSmm.c +++ b/UefiCpuPkg/PiSmmCpuDxeSmm/PiSmmCpuDxeSmm.c @@ -23,6 +23,21 @@ const BOOLEAN mIsStandaloneMm = FALSE; // BOOLEAN mSmmReadyToLock = FALSE; +/** + Check SmmProfile is enabled or not. + + @return TRUE SmmProfile is enabled. + FALSE SmmProfile is not enabled. + +**/ +BOOLEAN +IsSmmProfileEnabled ( + VOID + ) +{ + return FeaturePcdGet (PcdCpuSmmProfileEnable); +} + /** Perform the remaining tasks. @@ -40,7 +55,7 @@ PerformRemainingTasks ( // // Start SMM Profile feature // - if (FeaturePcdGet (PcdCpuSmmProfileEnable)) { + if (mSmmProfileEnabled) { SmmProfileStart (); } @@ -60,7 +75,7 @@ PerformRemainingTasks ( // // Update Page Table for outside SMRAM. // - if (FeaturePcdGet (PcdCpuSmmProfileEnable)) { + if (mSmmProfileEnabled) { SmmProfileUpdateMemoryAttributes (); } else { UpdateUefiMemMapAttributes (); @@ -157,7 +172,7 @@ SmmReadyToLockEventNotify ( // // Skip SMM profile initialization if feature is disabled // - if (FeaturePcdGet (PcdCpuSmmProfileEnable)) { + if (mSmmProfileEnabled) { // // Get Software SMI from FADT // diff --git a/UefiCpuPkg/PiSmmCpuDxeSmm/PiSmmCpuStandaloneMm.c b/UefiCpuPkg/PiSmmCpuDxeSmm/PiSmmCpuStandaloneMm.c index d8aaff811c..f81389463f 100644 --- a/UefiCpuPkg/PiSmmCpuDxeSmm/PiSmmCpuStandaloneMm.c +++ b/UefiCpuPkg/PiSmmCpuDxeSmm/PiSmmCpuStandaloneMm.c @@ -20,6 +20,28 @@ const BOOLEAN mIsStandaloneMm = TRUE; // BOOLEAN mRemainingTasksDone = FALSE; +/** + Check SmmProfile is enabled or not. + + @return TRUE SmmProfile is enabled. + FALSE SmmProfile is not enabled. + +**/ +BOOLEAN +IsSmmProfileEnabled ( + VOID + ) +{ + UINT64 SmmProfileSize; + + GetSmmProfileData (&SmmProfileSize); + if (SmmProfileSize == 0) { + return FALSE; + } + + return TRUE; +} + /** Perform the remaining tasks. @@ -216,7 +238,7 @@ PiCpuStandaloneMmEntry ( ASSERT_EFI_ERROR (Status); - if (FeaturePcdGet (PcdCpuSmmProfileEnable)) { + if (mSmmProfileEnabled) { // // Get Software SMI // diff --git a/UefiCpuPkg/PiSmmCpuDxeSmm/SmmCpuMemoryManagement.c b/UefiCpuPkg/PiSmmCpuDxeSmm/SmmCpuMemoryManagement.c index 4022c45065..d1cfd72106 100644 --- a/UefiCpuPkg/PiSmmCpuDxeSmm/SmmCpuMemoryManagement.c +++ b/UefiCpuPkg/PiSmmCpuDxeSmm/SmmCpuMemoryManagement.c @@ -1494,7 +1494,7 @@ IfReadOnlyPageTableNeeded ( // if (!IsRestrictedMemoryAccess () || ((PcdGet8 (PcdHeapGuardPropertyMask) & (BIT3 | BIT2)) != 0) || - FeaturePcdGet (PcdCpuSmmProfileEnable)) + mSmmProfileEnabled) { if (sizeof (UINTN) == sizeof (UINT64)) { // @@ -1508,7 +1508,7 @@ IfReadOnlyPageTableNeeded ( // // Restriction on access to non-SMRAM memory and SMM profile could not be enabled at the same time. // - ASSERT (!(IsRestrictedMemoryAccess () && FeaturePcdGet (PcdCpuSmmProfileEnable))); + ASSERT (!(IsRestrictedMemoryAccess () && mSmmProfileEnabled)); } return FALSE; diff --git a/UefiCpuPkg/PiSmmCpuDxeSmm/SmmProfile.c b/UefiCpuPkg/PiSmmCpuDxeSmm/SmmProfile.c index 44f67cc38b..164af20a4c 100644 --- a/UefiCpuPkg/PiSmmCpuDxeSmm/SmmProfile.c +++ b/UefiCpuPkg/PiSmmCpuDxeSmm/SmmProfile.c @@ -40,6 +40,11 @@ BOOLEAN mXdEnabled = FALSE; // BOOLEAN mBtsSupported = TRUE; +// +// The flag indicates if SMM profile is enabled. +// +BOOLEAN mSmmProfileEnabled = FALSE; + // // The flag indicates if SMM profile starts to record data. // @@ -342,7 +347,7 @@ IsAddressSplit ( { UINTN Index; - if (FeaturePcdGet (PcdCpuSmmProfileEnable)) { + if (mSmmProfileEnabled) { // // Check configuration // @@ -1018,7 +1023,7 @@ InitSmmProfile ( // // Skip SMM profile initialization if feature is disabled // - if (!FeaturePcdGet (PcdCpuSmmProfileEnable) && + if (!mSmmProfileEnabled && !HEAP_GUARD_NONSTOP_MODE && !NULL_DETECTION_NONSTOP_MODE) { diff --git a/UefiCpuPkg/PiSmmCpuDxeSmm/SmmProfile.h b/UefiCpuPkg/PiSmmCpuDxeSmm/SmmProfile.h index feddf6eec3..2726840c0e 100644 --- a/UefiCpuPkg/PiSmmCpuDxeSmm/SmmProfile.h +++ b/UefiCpuPkg/PiSmmCpuDxeSmm/SmmProfile.h @@ -137,6 +137,10 @@ extern BOOLEAN mXdSupported; // extern BOOLEAN mXdEnabled; // +// The flag indicates if SMM profile is enabled. +// +extern BOOLEAN mSmmProfileEnabled; +// // The flag indicates if #DB will be setup in #PF handler. // extern BOOLEAN mSetupDebugTrap; diff --git a/UefiCpuPkg/PiSmmCpuDxeSmm/X64/PageTbl.c b/UefiCpuPkg/PiSmmCpuDxeSmm/X64/PageTbl.c index f56d2849de..15d53b1411 100644 --- a/UefiCpuPkg/PiSmmCpuDxeSmm/X64/PageTbl.c +++ b/UefiCpuPkg/PiSmmCpuDxeSmm/X64/PageTbl.c @@ -228,7 +228,7 @@ SmmInitPageTable ( // PageTable = GenSmmPageTable (mPagingMode, mPhysicalAddressBits); - if (FeaturePcdGet (PcdCpuSmmProfileEnable)) { + if (mSmmProfileEnabled) { if (m5LevelPagingNeeded) { Pml5Entry = (UINT64 *)PageTable; // @@ -264,7 +264,7 @@ SmmInitPageTable ( } } - if (FeaturePcdGet (PcdCpuSmmProfileEnable) || + if (mSmmProfileEnabled || HEAP_GUARD_NONSTOP_MODE || NULL_DETECTION_NONSTOP_MODE) { @@ -820,7 +820,7 @@ SmiPFHandler ( } } - if (FeaturePcdGet (PcdCpuSmmProfileEnable)) { + if (mSmmProfileEnabled) { if (mIsStandaloneMm) { // // Only logging ranges shall run here in MM env.