From a232e0cd2fe2bb882a5dd0cafe322a2899a44d51 Mon Sep 17 00:00:00 2001 From: Jiaxin Wu Date: Sat, 12 Oct 2024 14:32:40 +0800 Subject: [PATCH] UefiCpuPkg/PiSmmCpuDxeSmm: Save and restore CR2 only if SmiProfile enable A page fault (#PF) that triggers an update to the page table only occurs if SmiProfile is enabled. Therefore, it is necessary to save and restore the CR2 register if SmiProfile is configured to be enabled. Signed-off-by: Jiaxin Wu --- UefiCpuPkg/PiSmmCpuDxeSmm/X64/PageTbl.c | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/UefiCpuPkg/PiSmmCpuDxeSmm/X64/PageTbl.c b/UefiCpuPkg/PiSmmCpuDxeSmm/X64/PageTbl.c index 160e33b4ed..f4aa267984 100644 --- a/UefiCpuPkg/PiSmmCpuDxeSmm/X64/PageTbl.c +++ b/UefiCpuPkg/PiSmmCpuDxeSmm/X64/PageTbl.c @@ -851,7 +851,15 @@ SaveCr2 ( OUT UINTN *Cr2 ) { - *Cr2 = AsmReadCr2 (); + // + // A page fault (#PF) that triggers an update to the page + // table only occurs if SmiProfile is enabled. Therefore, it is + // necessary to save the CR2 register if SmiProfile is + // configured to be enabled. + // + if (mSmmProfileEnabled) { + *Cr2 = AsmReadCr2 (); + } } /** @@ -864,5 +872,13 @@ RestoreCr2 ( IN UINTN Cr2 ) { - AsmWriteCr2 (Cr2); + // + // A page fault (#PF) that triggers an update to the page + // table only occurs if SmiProfile is enabled. Therefore, it is + // necessary to restore the CR2 register if SmiProfile is + // configured to be enabled. + // + if (mSmmProfileEnabled) { + AsmWriteCr2 (Cr2); + } }