There are below 2 differences between AMD & OVMF according existing implementation: 1.The mode of the CPU check is different between the AMD & OVMF. OVMF: CpuSaveState->x86.SMMRevId & 0Xffff
AMD: LMAValue = (UINT32)AsmReadMsr64 (EFER_ADDRESS) & LMA 2.Existing SmBase configuration is different between the AMD & OVMF. OVMF: if ((CpuSaveState->x86.SMMRevId & 0xFFFF) == 0) { CpuSaveState->x86.SMBASE = mSmBaseForAllCpus[CpuIndex]; } else { CpuSaveState->x64.SMBASE = mSmBaseForAllCpus[CpuIndex]; } AMD: AmdCpuState->x64.SMBASE = mSmBaseForAllCpus[CpuIndex]; This patch provides the SmmRelocationLib library instance for OVMF to handle the logic difference, and it won't change the existing implementation code logic. Cc: Ray Ni <ray...@intel.com> Cc: Zeng Star <star.z...@intel.com> Cc: Ard Biesheuvel <ardb+tianoc...@kernel.org> Cc: Jiewen Yao <jiewen....@intel.com> Cc: Gerd Hoffmann <kra...@redhat.com> Cc: Rahul Kumar <rahul1.ku...@intel.com> Signed-off-by: Jiaxin Wu <jiaxin...@intel.com> --- OvmfPkg/AmdSev/AmdSevX64.dsc | 1 + OvmfPkg/CloudHv/CloudHvX64.dsc | 1 + .../Library/SmmRelocationLib/Ia32/Semaphore.c | 0 .../Library/SmmRelocationLib/Ia32/SmmInit.nasm | 0 .../SmmRelocationLib/InternalSmmRelocationLib.h | 0 .../Library/SmmRelocationLib/SmmRelocationLib.c | 0 .../Library/SmmRelocationLib/SmmRelocationLib.inf | 3 +- .../SmmRelocationLib/SmramSaveStateConfig.c | 65 +++++++--------------- .../Library/SmmRelocationLib/X64/Semaphore.c | 0 .../Library/SmmRelocationLib/X64/SmmInit.nasm | 0 OvmfPkg/Microvm/MicrovmX64.dsc | 1 + OvmfPkg/OvmfPkgIa32.dsc | 1 + OvmfPkg/OvmfPkgIa32X64.dsc | 1 + OvmfPkg/OvmfPkgX64.dsc | 1 + 14 files changed, 27 insertions(+), 47 deletions(-) copy {UefiCpuPkg => OvmfPkg}/Library/SmmRelocationLib/Ia32/Semaphore.c (100%) copy {UefiCpuPkg => OvmfPkg}/Library/SmmRelocationLib/Ia32/SmmInit.nasm (100%) copy {UefiCpuPkg => OvmfPkg}/Library/SmmRelocationLib/InternalSmmRelocationLib.h (100%) copy {UefiCpuPkg => OvmfPkg}/Library/SmmRelocationLib/SmmRelocationLib.c (100%) copy {UefiCpuPkg => OvmfPkg}/Library/SmmRelocationLib/SmmRelocationLib.inf (90%) copy UefiCpuPkg/Library/SmmRelocationLib/AmdSmramSaveStateConfig.c => OvmfPkg/Library/SmmRelocationLib/SmramSaveStateConfig.c (60%) copy {UefiCpuPkg => OvmfPkg}/Library/SmmRelocationLib/X64/Semaphore.c (100%) copy {UefiCpuPkg => OvmfPkg}/Library/SmmRelocationLib/X64/SmmInit.nasm (100%) diff --git a/OvmfPkg/AmdSev/AmdSevX64.dsc b/OvmfPkg/AmdSev/AmdSevX64.dsc index a7540bb636..8eb6f4f24f 100644 --- a/OvmfPkg/AmdSev/AmdSevX64.dsc +++ b/OvmfPkg/AmdSev/AmdSevX64.dsc @@ -108,10 +108,11 @@ ################################################################################ !include MdePkg/MdeLibs.dsc.inc [LibraryClasses] + SmmRelocationLib|OvmfPkg/Library/SmmRelocationLib/SmmRelocationLib.inf PcdLib|MdePkg/Library/BasePcdLibNull/BasePcdLibNull.inf TimerLib|OvmfPkg/Library/AcpiTimerLib/BaseAcpiTimerLib.inf ResetSystemLib|OvmfPkg/Library/ResetSystemLib/BaseResetSystemLib.inf PrintLib|MdePkg/Library/BasePrintLib/BasePrintLib.inf BaseMemoryLib|MdePkg/Library/BaseMemoryLibRepStr/BaseMemoryLibRepStr.inf diff --git a/OvmfPkg/CloudHv/CloudHvX64.dsc b/OvmfPkg/CloudHv/CloudHvX64.dsc index b1911d6ab4..4996885301 100644 --- a/OvmfPkg/CloudHv/CloudHvX64.dsc +++ b/OvmfPkg/CloudHv/CloudHvX64.dsc @@ -124,10 +124,11 @@ ################################################################################ !include MdePkg/MdeLibs.dsc.inc [LibraryClasses] + SmmRelocationLib|OvmfPkg/Library/SmmRelocationLib/SmmRelocationLib.inf PcdLib|MdePkg/Library/BasePcdLibNull/BasePcdLibNull.inf TimerLib|OvmfPkg/Library/AcpiTimerLib/BaseAcpiTimerLib.inf ResetSystemLib|OvmfPkg/Library/ResetSystemLib/BaseResetSystemLib.inf PrintLib|MdePkg/Library/BasePrintLib/BasePrintLib.inf BaseMemoryLib|MdePkg/Library/BaseMemoryLibRepStr/BaseMemoryLibRepStr.inf diff --git a/UefiCpuPkg/Library/SmmRelocationLib/Ia32/Semaphore.c b/OvmfPkg/Library/SmmRelocationLib/Ia32/Semaphore.c similarity index 100% copy from UefiCpuPkg/Library/SmmRelocationLib/Ia32/Semaphore.c copy to OvmfPkg/Library/SmmRelocationLib/Ia32/Semaphore.c diff --git a/UefiCpuPkg/Library/SmmRelocationLib/Ia32/SmmInit.nasm b/OvmfPkg/Library/SmmRelocationLib/Ia32/SmmInit.nasm similarity index 100% copy from UefiCpuPkg/Library/SmmRelocationLib/Ia32/SmmInit.nasm copy to OvmfPkg/Library/SmmRelocationLib/Ia32/SmmInit.nasm diff --git a/UefiCpuPkg/Library/SmmRelocationLib/InternalSmmRelocationLib.h b/OvmfPkg/Library/SmmRelocationLib/InternalSmmRelocationLib.h similarity index 100% copy from UefiCpuPkg/Library/SmmRelocationLib/InternalSmmRelocationLib.h copy to OvmfPkg/Library/SmmRelocationLib/InternalSmmRelocationLib.h diff --git a/UefiCpuPkg/Library/SmmRelocationLib/SmmRelocationLib.c b/OvmfPkg/Library/SmmRelocationLib/SmmRelocationLib.c similarity index 100% copy from UefiCpuPkg/Library/SmmRelocationLib/SmmRelocationLib.c copy to OvmfPkg/Library/SmmRelocationLib/SmmRelocationLib.c diff --git a/UefiCpuPkg/Library/SmmRelocationLib/SmmRelocationLib.inf b/OvmfPkg/Library/SmmRelocationLib/SmmRelocationLib.inf similarity index 90% copy from UefiCpuPkg/Library/SmmRelocationLib/SmmRelocationLib.inf copy to OvmfPkg/Library/SmmRelocationLib/SmmRelocationLib.inf index 45fdaf35bc..3ea6a1a0bc 100644 --- a/UefiCpuPkg/Library/SmmRelocationLib/SmmRelocationLib.inf +++ b/OvmfPkg/Library/SmmRelocationLib/SmmRelocationLib.inf @@ -13,11 +13,11 @@ ## [Defines] INF_VERSION = 0x00010005 BASE_NAME = SmmRelocationLib - FILE_GUID = 853E97B3-790C-4EA3-945C-8F622FC47FE8 + FILE_GUID = 51834F51-CCE0-4743-B553-935D0C8A53FF MODULE_TYPE = PEIM VERSION_STRING = 1.0 LIBRARY_CLASS = SmmRelocationLib [Sources] @@ -40,11 +40,10 @@ [LibraryClasses] BaseLib BaseMemoryLib CpuExceptionHandlerLib - CpuLib DebugLib HobLib LocalApicLib MemoryAllocationLib PcdLib diff --git a/UefiCpuPkg/Library/SmmRelocationLib/AmdSmramSaveStateConfig.c b/OvmfPkg/Library/SmmRelocationLib/SmramSaveStateConfig.c similarity index 60% copy from UefiCpuPkg/Library/SmmRelocationLib/AmdSmramSaveStateConfig.c copy to OvmfPkg/Library/SmmRelocationLib/SmramSaveStateConfig.c index 068ae60887..146e6d54d9 100644 --- a/UefiCpuPkg/Library/SmmRelocationLib/AmdSmramSaveStateConfig.c +++ b/OvmfPkg/Library/SmmRelocationLib/SmramSaveStateConfig.c @@ -1,43 +1,15 @@ /** @file Config SMRAM Save State for SmmBases Relocation. - Copyright (C) 2023 Advanced Micro Devices, Inc. All rights reserved.<BR> Copyright (c) 2024, Intel Corporation. All rights reserved.<BR> SPDX-License-Identifier: BSD-2-Clause-Patent **/ #include "InternalSmmRelocationLib.h" #include <Register/Amd/SmramSaveStateMap.h> -#define EFER_ADDRESS 0XC0000080ul - -/** - Get the mode of the CPU at the time an SMI occurs - - @retval EFI_MM_SAVE_STATE_REGISTER_LMA_32BIT 32 bit. - @retval EFI_MM_SAVE_STATE_REGISTER_LMA_64BIT 64 bit. - -**/ -UINT8 -GetMmSaveStateRegisterLma ( - VOID - ) -{ - UINT8 SmmSaveStateRegisterLma; - UINT32 LMAValue; - - SmmSaveStateRegisterLma = (UINT8)EFI_MM_SAVE_STATE_REGISTER_LMA_32BIT; - - LMAValue = (UINT32)AsmReadMsr64 (EFER_ADDRESS) & LMA; - if (LMAValue) { - SmmSaveStateRegisterLma = (UINT8)EFI_MM_SAVE_STATE_REGISTER_LMA_64BIT; - } - - return SmmSaveStateRegisterLma; -} - /** This function configures the SmBase on the currently executing CPU. @param[in] SmBase The SmBase on the currently executing CPU. @@ -46,15 +18,19 @@ VOID EFIAPI ConfigureSmBase ( IN UINT64 SmBase ) { - AMD_SMRAM_SAVE_STATE_MAP *AmdCpuState; + AMD_SMRAM_SAVE_STATE_MAP *CpuSaveState; - AmdCpuState = (AMD_SMRAM_SAVE_STATE_MAP *)(UINTN)(SMM_DEFAULT_SMBASE + SMRAM_SAVE_STATE_MAP_OFFSET); + CpuSaveState = (AMD_SMRAM_SAVE_STATE_MAP *)(UINTN)(SMM_DEFAULT_SMBASE + SMRAM_SAVE_STATE_MAP_OFFSET); - AmdCpuState->x64.SMBASE = (UINT32)SmBase; + if ((CpuSaveState->x86.SMMRevId & 0xFFFF) == 0) { + CpuSaveState->x86.SMBASE = (UINT32)SmBase; + } else { + CpuSaveState->x64.SMBASE = (UINT32)SmBase; + } } /** This function updates the SMRAM save state on the currently executing CPU to resume execution at a specific address after an RSM instruction. This @@ -88,38 +64,37 @@ HookReturnFromSmm ( IN UINT64 NewInstructionPointer32, IN UINT64 NewInstructionPointer ) { UINT64 OriginalInstructionPointer; - AMD_SMRAM_SAVE_STATE_MAP *AmdCpuState; - - AmdCpuState = (AMD_SMRAM_SAVE_STATE_MAP *)CpuState; + AMD_SMRAM_SAVE_STATE_MAP *CpuSaveState; - if (GetMmSaveStateRegisterLma () == EFI_MM_SAVE_STATE_REGISTER_LMA_32BIT) { - OriginalInstructionPointer = (UINT64)AmdCpuState->x86._EIP; - AmdCpuState->x86._EIP = (UINT32)NewInstructionPointer; + CpuSaveState = (AMD_SMRAM_SAVE_STATE_MAP *)CpuState; + if ((CpuSaveState->x86.SMMRevId & 0xFFFF) == 0) { + OriginalInstructionPointer = (UINT64)CpuSaveState->x86._EIP; + CpuSaveState->x86._EIP = (UINT32)NewInstructionPointer; // // Clear the auto HALT restart flag so the RSM instruction returns // program control to the instruction following the HLT instruction. // - if ((AmdCpuState->x86.AutoHALTRestart & BIT0) != 0) { - AmdCpuState->x86.AutoHALTRestart &= ~BIT0; + if ((CpuSaveState->x86.AutoHALTRestart & BIT0) != 0) { + CpuSaveState->x86.AutoHALTRestart &= ~BIT0; } } else { - OriginalInstructionPointer = AmdCpuState->x64._RIP; - if ((AmdCpuState->x64.EFER & LMA) == 0) { - AmdCpuState->x64._RIP = (UINT32)NewInstructionPointer32; + OriginalInstructionPointer = CpuSaveState->x64._RIP; + if ((CpuSaveState->x64.EFER & LMA) == 0) { + CpuSaveState->x64._RIP = (UINT32)NewInstructionPointer32; } else { - AmdCpuState->x64._RIP = (UINT32)NewInstructionPointer; + CpuSaveState->x64._RIP = (UINT32)NewInstructionPointer; } // // Clear the auto HALT restart flag so the RSM instruction returns // program control to the instruction following the HLT instruction. // - if ((AmdCpuState->x64.AutoHALTRestart & BIT0) != 0) { - AmdCpuState->x64.AutoHALTRestart &= ~BIT0; + if ((CpuSaveState->x64.AutoHALTRestart & BIT0) != 0) { + CpuSaveState->x64.AutoHALTRestart &= ~BIT0; } } return OriginalInstructionPointer; } diff --git a/UefiCpuPkg/Library/SmmRelocationLib/X64/Semaphore.c b/OvmfPkg/Library/SmmRelocationLib/X64/Semaphore.c similarity index 100% copy from UefiCpuPkg/Library/SmmRelocationLib/X64/Semaphore.c copy to OvmfPkg/Library/SmmRelocationLib/X64/Semaphore.c diff --git a/UefiCpuPkg/Library/SmmRelocationLib/X64/SmmInit.nasm b/OvmfPkg/Library/SmmRelocationLib/X64/SmmInit.nasm similarity index 100% copy from UefiCpuPkg/Library/SmmRelocationLib/X64/SmmInit.nasm copy to OvmfPkg/Library/SmmRelocationLib/X64/SmmInit.nasm diff --git a/OvmfPkg/Microvm/MicrovmX64.dsc b/OvmfPkg/Microvm/MicrovmX64.dsc index 1f2f8b5bb6..69de4dd3f1 100644 --- a/OvmfPkg/Microvm/MicrovmX64.dsc +++ b/OvmfPkg/Microvm/MicrovmX64.dsc @@ -127,10 +127,11 @@ ################################################################################ !include MdePkg/MdeLibs.dsc.inc [LibraryClasses] + SmmRelocationLib|OvmfPkg/Library/SmmRelocationLib/SmmRelocationLib.inf PcdLib|MdePkg/Library/BasePcdLibNull/BasePcdLibNull.inf TimerLib|MdePkg/Library/SecPeiDxeTimerLibCpu/SecPeiDxeTimerLibCpu.inf ResetSystemLib|OvmfPkg/Library/ResetSystemLib/BaseResetSystemLibMicrovm.inf PrintLib|MdePkg/Library/BasePrintLib/BasePrintLib.inf BaseMemoryLib|MdePkg/Library/BaseMemoryLibRepStr/BaseMemoryLibRepStr.inf diff --git a/OvmfPkg/OvmfPkgIa32.dsc b/OvmfPkg/OvmfPkgIa32.dsc index 15fadc2fdc..78a5aefb6d 100644 --- a/OvmfPkg/OvmfPkgIa32.dsc +++ b/OvmfPkg/OvmfPkgIa32.dsc @@ -127,10 +127,11 @@ ################################################################################ !include MdePkg/MdeLibs.dsc.inc [LibraryClasses] + SmmRelocationLib|OvmfPkg/Library/SmmRelocationLib/SmmRelocationLib.inf PcdLib|MdePkg/Library/BasePcdLibNull/BasePcdLibNull.inf TimerLib|OvmfPkg/Library/AcpiTimerLib/BaseAcpiTimerLib.inf ResetSystemLib|OvmfPkg/Library/ResetSystemLib/BaseResetSystemLib.inf PrintLib|MdePkg/Library/BasePrintLib/BasePrintLib.inf BaseMemoryLib|MdePkg/Library/BaseMemoryLibRepStr/BaseMemoryLibRepStr.inf diff --git a/OvmfPkg/OvmfPkgIa32X64.dsc b/OvmfPkg/OvmfPkgIa32X64.dsc index 6e55b50a96..8258129baf 100644 --- a/OvmfPkg/OvmfPkgIa32X64.dsc +++ b/OvmfPkg/OvmfPkgIa32X64.dsc @@ -132,10 +132,11 @@ ################################################################################ !include MdePkg/MdeLibs.dsc.inc [LibraryClasses] + SmmRelocationLib|OvmfPkg/Library/SmmRelocationLib/SmmRelocationLib.inf PcdLib|MdePkg/Library/BasePcdLibNull/BasePcdLibNull.inf TimerLib|OvmfPkg/Library/AcpiTimerLib/BaseAcpiTimerLib.inf ResetSystemLib|OvmfPkg/Library/ResetSystemLib/BaseResetSystemLib.inf PrintLib|MdePkg/Library/BasePrintLib/BasePrintLib.inf BaseMemoryLib|MdePkg/Library/BaseMemoryLibRepStr/BaseMemoryLibRepStr.inf diff --git a/OvmfPkg/OvmfPkgX64.dsc b/OvmfPkg/OvmfPkgX64.dsc index f2edd3bbc0..240fc18081 100644 --- a/OvmfPkg/OvmfPkgX64.dsc +++ b/OvmfPkg/OvmfPkgX64.dsc @@ -145,10 +145,11 @@ ################################################################################ !include MdePkg/MdeLibs.dsc.inc [LibraryClasses] + SmmRelocationLib|OvmfPkg/Library/SmmRelocationLib/SmmRelocationLib.inf PcdLib|MdePkg/Library/BasePcdLibNull/BasePcdLibNull.inf TimerLib|OvmfPkg/Library/AcpiTimerLib/BaseAcpiTimerLib.inf ResetSystemLib|OvmfPkg/Library/ResetSystemLib/BaseResetSystemLib.inf PrintLib|MdePkg/Library/BasePrintLib/BasePrintLib.inf BaseMemoryLib|MdePkg/Library/BaseMemoryLibRepStr/BaseMemoryLibRepStr.inf -- 2.16.2.windows.1 -=-=-=-=-=-=-=-=-=-=-=- Groups.io Links: You receive all messages sent to this group. View/Reply Online (#117952): https://edk2.groups.io/g/devel/message/117952 Mute This Topic: https://groups.io/mt/105593575/21656 Group Owner: devel+ow...@edk2.groups.io Unsubscribe: https://edk2.groups.io/g/devel/unsub [arch...@mail-archive.com] -=-=-=-=-=-=-=-=-=-=-=-