From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail05.groups.io (mail05.groups.io [45.79.224.7]) by spool.mail.gandi.net (Postfix) with ESMTPS id D73EE74004C for ; Fri, 26 Apr 2024 12:17:54 +0000 (UTC) DKIM-Signature: a=rsa-sha256; bh=g0z5pbJ6pEOYmzkXyTlzwGjlmJfWlGzo1187/AbLJEA=; c=relaxed/simple; d=groups.io; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References:Precedence:List-Subscribe:List-Help:Sender:List-Id:Mailing-List:Delivered-To:Resent-Date:Resent-From:Reply-To:List-Unsubscribe-Post:List-Unsubscribe; s=20240206; t=1714133873; v=1; b=VQIV+3k5K6eplfhHLoTpIRzWjuIhnoMUEdl67OZTC5iIhWuLa1tN8L6pG29eeJ5BMlRns18q ndWBK69wgLUIc++BIQElZJB++A9hid6S/8JmI50Wh3GKkAZE2x0eLFU7S5wajQ83o2dRDc7jUNT 0k6qzGECdnvF9b9MnhKtJd4ZL4f+pKWBfObbSmMe6ushO1sZdpSRu7dDrfGdjfRDPhT6RQRaOe3 Xack00INuDkwWVZp1YdR0Is74Wte62S1Zw+FhJocFul9hF39Dw213RzYdL14aS9n6iq/xWK2XJ9 iJt3Z1Hrpic7hbpXw5DycOosfMJZuy31VC8MRrq5uR5pw== X-Received: by 127.0.0.2 with SMTP id POBuYY7687511x12L2VYA58s; Fri, 26 Apr 2024 05:17:53 -0700 X-Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.18]) by mx.groups.io with SMTP id smtpd.web11.12554.1714133857432549514 for ; Fri, 26 Apr 2024 05:17:52 -0700 X-CSE-ConnectionGUID: LO+C7clzSk+y4me9KPH6oQ== X-CSE-MsgGUID: xShSTeZJSE+nKpo/xrW8tg== X-IronPort-AV: E=McAfee;i="6600,9927,11055"; a="10027152" X-IronPort-AV: E=Sophos;i="6.07,232,1708416000"; d="scan'208";a="10027152" X-Received: from orviesa001.jf.intel.com ([10.64.159.141]) by orvoesa110.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 26 Apr 2024 05:17:53 -0700 X-CSE-ConnectionGUID: qAXipnm3ShKnyXh2kdMgfQ== X-CSE-MsgGUID: hVF0viuFRWWiYCxSQ3ERng== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.07,232,1708416000"; d="scan'208";a="62879141" X-Received: from sh1gapp1009.ccr.corp.intel.com ([10.239.189.219]) by orviesa001.jf.intel.com with ESMTP; 26 Apr 2024 05:17:50 -0700 From: "Wu, Jiaxin" To: devel@edk2.groups.io Cc: Ray Ni , Zeng Star , Ard Biesheuvel , Jiewen Yao , Gerd Hoffmann , Rahul Kumar Subject: [edk2-devel] [PATCH v4 08/14] OvmfPkg/SmmRelocationLib: Add library instance for OVMF Date: Fri, 26 Apr 2024 20:17:14 +0800 Message-Id: <20240426121720.54308-9-jiaxin.wu@intel.com> In-Reply-To: <20240426121720.54308-1-jiaxin.wu@intel.com> References: <20240426121720.54308-1-jiaxin.wu@intel.com> Precedence: Bulk List-Subscribe: List-Help: Sender: devel@edk2.groups.io List-Id: Mailing-List: list devel@edk2.groups.io; contact devel+owner@edk2.groups.io Resent-Date: Fri, 26 Apr 2024 05:17:52 -0700 Resent-From: jiaxin.wu@intel.com Reply-To: devel@edk2.groups.io,jiaxin.wu@intel.com List-Unsubscribe-Post: List-Unsubscribe=One-Click List-Unsubscribe: X-Gm-Message-State: ouJrwAERQ3150giXD3CIWrYmx7686176AA= X-GND-Status: LEGIT Authentication-Results: spool.mail.gandi.net; dkim=pass header.d=groups.io header.s=20240206 header.b=VQIV+3k5; dmarc=fail reason="SPF not aligned (relaxed), DKIM not aligned (relaxed)" header.from=intel.com (policy=none); spf=pass (spool.mail.gandi.net: domain of bounce@groups.io designates 45.79.224.7 as permitted sender) smtp.mailfrom=bounce@groups.io 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 Cc: Zeng Star Cc: Ard Biesheuvel Cc: Jiewen Yao Cc: Gerd Hoffmann Cc: Rahul Kumar Signed-off-by: Jiaxin Wu --- 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.
Copyright (c) 2024, Intel Corporation. All rights reserved.
SPDX-License-Identifier: BSD-2-Clause-Patent **/ #include "InternalSmmRelocationLib.h" #include -#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 9db3ebd0e7..1be021be71 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 4337812292..d27a4c7278 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 157ae6c0e4..8f5cd23b2e 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 (#118332): https://edk2.groups.io/g/devel/message/118332 Mute This Topic: https://groups.io/mt/105748920/7686176 Group Owner: devel+owner@edk2.groups.io Unsubscribe: https://edk2.groups.io/g/devel/unsub [rebecca@openfw.io] -=-=-=-=-=-=-=-=-=-=-=-