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 BD403AC0CFD for ; Thu, 18 Apr 2024 06:56:19 +0000 (UTC) DKIM-Signature: a=rsa-sha256; bh=liRhPtMNXYgFRrbtagQz7TkAfw3kTD2mJV54yLbLzXI=; 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=1713423378; v=1; b=EdgLccm7K8FeWJmeAsIVJmVCmu3HK87PUVyXj11rPNCALgmQjitKPMD3S7JIs/gpNPHk4Gwf bja7afoL9ZYzExWZ84SiZyi5BAcbppAKcERVhUohJVoisZGrfWFIJnIKb0Y7gIs70fLgb226i0N 7JxnJzY/T3JE/LXGXvK1JBBXas+p97w0FV7xwuM27lj8z/0Y2vgaP4yfEL0XItWprk4lRlE8m4i 4DuQuw3oRZdZxPbVQo2vhWfcrkse0d2YtIotElxTMv3LRe/Rr00SKD7ku77NklCicNwlu5B0bFI xYeVa8YAsldEAlPGZ8x5TEfpZwMIQFAt0ASWKWa0KdtOw== X-Received: by 127.0.0.2 with SMTP id vVxdYY7687511xNzjGnxjJWF; Wed, 17 Apr 2024 23:56:18 -0700 X-Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.19]) by mx.groups.io with SMTP id smtpd.web10.7041.1713423363789669653 for ; Wed, 17 Apr 2024 23:56:17 -0700 X-CSE-ConnectionGUID: qnQgVXMoSzSJmqFgGOAOHA== X-CSE-MsgGUID: l1rzR6U5R6yP0avnFaJMUg== X-IronPort-AV: E=McAfee;i="6600,9927,11047"; a="8822234" X-IronPort-AV: E=Sophos;i="6.07,211,1708416000"; d="scan'208";a="8822234" X-Received: from orviesa010.jf.intel.com ([10.64.159.150]) by orvoesa111.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 17 Apr 2024 23:56:18 -0700 X-CSE-ConnectionGUID: 9NT9ufFWTB+I62sweK+JOg== X-CSE-MsgGUID: I/ia8iofRFC9Y8m4DWRlQw== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.07,211,1708416000"; d="scan'208";a="22755062" X-Received: from sh1gapp1009.ccr.corp.intel.com ([10.239.189.219]) by orviesa010.jf.intel.com with ESMTP; 17 Apr 2024 23:56:16 -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 v3 07/13] OvmfPkg/SmmRelocationLib: Add library instance for OVMF Date: Thu, 18 Apr 2024 14:55:50 +0800 Message-Id: <20240418065556.5696-8-jiaxin.wu@intel.com> In-Reply-To: <20240418065556.5696-1-jiaxin.wu@intel.com> References: <20240418065556.5696-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: Wed, 17 Apr 2024 23:56:17 -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: oYySetX0qMLvrLM29itIqozmx7686176AA= X-GND-Status: LEGIT Authentication-Results: spool.mail.gandi.net; dkim=pass header.d=groups.io header.s=20240206 header.b=EdgLccm7; 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 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/7686176 Group Owner: devel+owner@edk2.groups.io Unsubscribe: https://edk2.groups.io/g/devel/unsub [rebecca@openfw.io] -=-=-=-=-=-=-=-=-=-=-=-