From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail02.groups.io (mail02.groups.io [66.175.222.108]) by spool.mail.gandi.net (Postfix) with ESMTPS id EEA429416BF for ; Wed, 10 Apr 2024 13:57:39 +0000 (UTC) DKIM-Signature: a=rsa-sha256; bh=BJ0visfEaCKlLKTPRPNNZloN4Mh6jk2bh7qYDOmuV8Q=; 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=1712757458; v=1; b=4KrsZC5O01EQrS6nIa8zLZ+3mcGnDpnhRn8QLKgB7OjDR867SWgkEjzJAY5UwQr2SIu4L01q dVe8TpMqNrSZyi4Brh76lJ+zmYt02GiGOX0vxQ0SEz4QTpZI+wZfygBv4nWb6uwxo5H0mw6KD4j CXEJbvVPQhsH9HO3po0HWPTE6+0J/TSrN4Pa5xiJDmswReHrOLUEUuBA/YGSCFo83rV0QBG08pi l/y5v7V414YE5hZMek1fw4+zF44kQEw2LQNFD/oE98O9gThIbiqEGuH19Kq0XPJMumYGNtF2S6x 74553o12r/xXufVYXWtIq5axkYhnCKkSprGdyQEt5+oHA== X-Received: by 127.0.0.2 with SMTP id B4gsYY7687511xMpVdXkmqFi; Wed, 10 Apr 2024 06:57:38 -0700 X-Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.9]) by mx.groups.io with SMTP id smtpd.web10.306.1712757450770649687 for ; Wed, 10 Apr 2024 06:57:38 -0700 X-CSE-ConnectionGUID: BuFfRDfgR3ejQHLVtp8Xmw== X-CSE-MsgGUID: 0eWRxiMmTgyej8TzByLSig== X-IronPort-AV: E=McAfee;i="6600,9927,11039"; a="30602139" X-IronPort-AV: E=Sophos;i="6.07,190,1708416000"; d="scan'208";a="30602139" X-Received: from fmviesa001.fm.intel.com ([10.60.135.141]) by orvoesa101.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 10 Apr 2024 06:57:38 -0700 X-CSE-ConnectionGUID: z+4XLSTGTS6vmj8c4+mlwA== X-CSE-MsgGUID: W4CYrXf5Qx+lCnWilEWK3w== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.07,190,1708416000"; d="scan'208";a="51778164" X-Received: from sh1gapp1009.ccr.corp.intel.com ([10.239.189.219]) by fmviesa001.fm.intel.com with ESMTP; 10 Apr 2024 06:57:36 -0700 From: "Wu, Jiaxin" To: devel@edk2.groups.io Cc: Abdul Lateef Attar , Abner Chang , Ray Ni , Zeng Star , Gerd Hoffmann , Rahul Kumar Subject: [edk2-devel] [PATCH v1 04/13] UefiCpuPkg/SmmRelocationLib: Add library instance for AMD Date: Wed, 10 Apr 2024 21:57:15 +0800 Message-Id: <20240410135724.15344-5-jiaxin.wu@intel.com> In-Reply-To: <20240410135724.15344-1-jiaxin.wu@intel.com> References: <20240410135724.15344-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, 10 Apr 2024 06:57:38 -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: P7ZS0O7Z9WkGqmtFyuhzb9u9x7686176AA= X-GND-Status: LEGIT Authentication-Results: spool.mail.gandi.net; dkim=pass header.d=groups.io header.s=20240206 header.b=4KrsZC5O; 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 66.175.222.108 as permitted sender) smtp.mailfrom=bounce@groups.io Due to the definition difference of SMRAM Save State, SmmBase config in SMRAM Save State for AMD is also different. This patch provides the AmdSmmRelocationLib library instance to handle the SMRAM Save State difference. Cc: Abdul Lateef Attar Cc: Abner Chang Cc: Ray Ni Cc: Zeng Star Cc: Gerd Hoffmann Cc: Rahul Kumar Signed-off-by: Jiaxin Wu --- .../SmmRelocationLib/AmdSmmRelocationLib.inf | 61 ++++++++++++ .../SmmRelocationLib/AmdSmramSaveStateConfig.c | 109 +++++++++++++++++++++ 2 files changed, 170 insertions(+) create mode 100644 UefiCpuPkg/Library/SmmRelocationLib/AmdSmmRelocationLib.inf create mode 100644 UefiCpuPkg/Library/SmmRelocationLib/AmdSmramSaveStateConfig.c diff --git a/UefiCpuPkg/Library/SmmRelocationLib/AmdSmmRelocationLib.inf b/UefiCpuPkg/Library/SmmRelocationLib/AmdSmmRelocationLib.inf new file mode 100644 index 0000000000..710cd1948b --- /dev/null +++ b/UefiCpuPkg/Library/SmmRelocationLib/AmdSmmRelocationLib.inf @@ -0,0 +1,61 @@ +## @file +# SMM Relocation Lib for each processor. +# +# This Lib produces the SMM_BASE_HOB in HOB database which tells +# the PiSmmCpuDxeSmm driver (runs at a later phase) about the new +# SMBASE for each processor. PiSmmCpuDxeSmm driver installs the +# SMI handler at the SMM_BASE_HOB.SmBase[Index]+0x8000 for processor +# Index. +# +# Copyright (c) 2024, Intel Corporation. All rights reserved.
+# SPDX-License-Identifier: BSD-2-Clause-Patent +# +## + +[Defines] + INF_VERSION = 0x00010005 + BASE_NAME = SmmRelocationLib + FILE_GUID = 65C74DCD-0D09-494A-8BFF-A64226EB8054 + MODULE_TYPE = PEIM + VERSION_STRING = 1.0 + LIBRARY_CLASS = SmmRelocationLib + +[Sources] + InternalSmmRelocationLib.h + AmdSmramSaveStateConfig.c + SmmRelocationLib.c + +[Sources.Ia32] + Ia32/Semaphore.c + Ia32/SmmInit.nasm + +[Sources.X64] + X64/Semaphore.c + X64/SmmInit.nasm + +[Packages] + MdePkg/MdePkg.dec + MdeModulePkg/MdeModulePkg.dec + UefiCpuPkg/UefiCpuPkg.dec + +[LibraryClasses] + BaseLib + BaseMemoryLib + CpuExceptionHandlerLib + DebugLib + HobLib + LocalApicLib + MemoryAllocationLib + PcdLib + PeiServicesLib + +[Guids] + gSmmBaseHobGuid ## HOB ALWAYS_PRODUCED + gEfiSmmSmramMemoryGuid ## CONSUMES + +[Pcd] + gUefiCpuPkgTokenSpaceGuid.PcdCpuMaxLogicalProcessorNumber + gUefiCpuPkgTokenSpaceGuid.PcdCpuSmmStackSize ## CONSUMES + +[FeaturePcd] + gUefiCpuPkgTokenSpaceGuid.PcdCpuHotPlugSupport ## CONSUMES diff --git a/UefiCpuPkg/Library/SmmRelocationLib/AmdSmramSaveStateConfig.c b/UefiCpuPkg/Library/SmmRelocationLib/AmdSmramSaveStateConfig.c new file mode 100644 index 0000000000..fbcf347f9b --- /dev/null +++ b/UefiCpuPkg/Library/SmmRelocationLib/AmdSmramSaveStateConfig.c @@ -0,0 +1,109 @@ +/** @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 + +/** + This function configures the SmBase on the currently executing CPU. + + @param[in] CpuIndex The index of the CPU. + @param[in,out] CpuState Pointer to SMRAM Save State Map for the + currently executing CPU. On out, SmBase is + updated to the new value. + +**/ +VOID +EFIAPI +ConfigureSmBase ( + IN UINTN CpuIndex, + IN OUT SMRAM_SAVE_STATE_MAP *CpuState + ) +{ + AMD_SMRAM_SAVE_STATE_MAP *AmdCpuState; + + AmdCpuState = (AMD_SMRAM_SAVE_STATE_MAP *)CpuState; + + if (mSmmSaveStateRegisterLma == EFI_MM_SAVE_STATE_REGISTER_LMA_32BIT) { + AmdCpuState->x86.SMBASE = (UINT32)mSmBaseForAllCpus[CpuIndex]; + } else { + AmdCpuState->x64.SMBASE = (UINT32)mSmBaseForAllCpus[CpuIndex]; + } +} + +/** + This function updates the SMRAM save state on the currently executing CPU + to resume execution at a specific address after an RSM instruction. This + function must evaluate the SMRAM save state to determine the execution mode + the RSM instruction resumes and update the resume execution address with + either NewInstructionPointer32 or NewInstructionPoint. The auto HALT restart + flag in the SMRAM save state must always be cleared. This function returns + the value of the instruction pointer from the SMRAM save state that was + replaced. If this function returns 0, then the SMRAM save state was not + modified. + + This function is called during the very first SMI on each CPU after + SmmCpuFeaturesInitializeProcessor() to set a flag in normal execution mode + to signal that the SMBASE of each CPU has been updated before the default + SMBASE address is used for the first SMI to the next CPU. + + @param[in] CpuIndex The processor index for the currently + executing CPU. + @param[in,out] CpuState Pointer to SMRAM Save State Map for the + currently executing CPU. + @param[in] NewInstructionPointer32 Instruction pointer to use if resuming to + 32-bit mode from 64-bit SMM. + @param[in] NewInstructionPointer Instruction pointer to use if resuming to + same mode as SMM. + + @retval The value of the original instruction pointer before it was hooked. + +**/ +UINT64 +EFIAPI +HookReturnFromSmm ( + IN UINTN CpuIndex, + IN OUT SMRAM_SAVE_STATE_MAP *CpuState, + IN UINT64 NewInstructionPointer32, + IN UINT64 NewInstructionPointer + ) +{ + UINT64 OriginalInstructionPointer; + AMD_SMRAM_SAVE_STATE_MAP *AmdCpuState; + + AmdCpuState = (AMD_SMRAM_SAVE_STATE_MAP *)CpuState; + + if (mSmmSaveStateRegisterLma == EFI_MM_SAVE_STATE_REGISTER_LMA_32BIT) { + OriginalInstructionPointer = (UINT64)AmdCpuState->x86._EIP; + AmdCpuState->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; + } + } else { + OriginalInstructionPointer = AmdCpuState->x64._RIP; + if ((AmdCpuState->x64.EFER & LMA) == 0) { + AmdCpuState->x64._RIP = (UINT32)NewInstructionPointer32; + } else { + AmdCpuState->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; + } + } + + return OriginalInstructionPointer; +} -- 2.16.2.windows.1 -=-=-=-=-=-=-=-=-=-=-=- Groups.io Links: You receive all messages sent to this group. View/Reply Online (#117592): https://edk2.groups.io/g/devel/message/117592 Mute This Topic: https://groups.io/mt/105441993/7686176 Group Owner: devel+owner@edk2.groups.io Unsubscribe: https://edk2.groups.io/g/devel/unsub [rebecca@openfw.io] -=-=-=-=-=-=-=-=-=-=-=-