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 686F474003D for ; Thu, 18 Apr 2024 06:56:17 +0000 (UTC) DKIM-Signature: a=rsa-sha256; bh=7mtsNvGQ0lGhQ76r0iBqppLJXV2lbt46wSRrxqwW9RQ=; 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=1713423376; v=1; b=XHmoOABU+cT9LqAS4OwfbtSfKX36dVv8dFuWNLio0aBwgikZBRVMl2RH6AcYwtuzppt538rU pKX2W+vIg9nUdJbdXY16I8xtz+xzUDuUA3kkty0J4uksGJNh+/+pLxfDYoRQYD3gT8neAAscd/i wgnYNT3QWA4MfUseZDpBAX8aFoeJPoOvjQs8cCn6f3G47aRrb6QKVMRcoTgfoh+xNK/156ry0qN d/72QPTsRxVASTBIGk5Pm7q4T7dv3udeqrRq4Ynhvb5Euuxh9dZYLETnA/t+7+67/BRvCYqPL+g HsFGa5L8UxycZtMT8H6QUGlvQAEtGcjXZ1vD794h++6hw== X-Received: by 127.0.0.2 with SMTP id ro1CYY7687511xDRSbSWIyyq; Wed, 17 Apr 2024 23:56:16 -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:15 -0700 X-CSE-ConnectionGUID: UWjI4kTcRTmOd5UDVVcm+g== X-CSE-MsgGUID: 5mA3Tg4STEeJGW8Hp90H3w== X-IronPort-AV: E=McAfee;i="6600,9927,11047"; a="8822225" X-IronPort-AV: E=Sophos;i="6.07,211,1708416000"; d="scan'208";a="8822225" 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:15 -0700 X-CSE-ConnectionGUID: ivOGg9dBR3SEr6FJraOpIw== X-CSE-MsgGUID: 5JUaczBoSPOBSrOH9JRZQg== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.07,211,1708416000"; d="scan'208";a="22755009" X-Received: from sh1gapp1009.ccr.corp.intel.com ([10.239.189.219]) by orviesa010.jf.intel.com with ESMTP; 17 Apr 2024 23:56:13 -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 v3 06/13] UefiCpuPkg/SmmRelocationLib: Add library instance for AMD Date: Thu, 18 Apr 2024 14:55:49 +0800 Message-Id: <20240418065556.5696-7-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:15 -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: LHmCvoddLZw8qm4b2MjSgEt9x7686176AA= X-GND-Status: LEGIT Authentication-Results: spool.mail.gandi.net; dkim=pass header.d=groups.io header.s=20240206 header.b=XHmoOABU; 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 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 Acked-by: Ray Ni --- ...mmRelocationLib.inf => AmdSmmRelocationLib.inf} | 5 +- ...SaveStateConfig.c => AmdSmramSaveStateConfig.c} | 91 ++++++++++------------ UefiCpuPkg/UefiCpuPkg.dsc | 1 + 3 files changed, 43 insertions(+), 54 deletions(-) copy UefiCpuPkg/Library/SmmRelocationLib/{SmmRelocationLib.inf => AmdSmmRelocationLib.inf} (89%) copy UefiCpuPkg/Library/SmmRelocationLib/{SmramSaveStateConfig.c => AmdSmramSaveStateConfig.c} (47%) diff --git a/UefiCpuPkg/Library/SmmRelocationLib/SmmRelocationLib.inf b/UefiCpuPkg/Library/SmmRelocationLib/AmdSmmRelocationLib.inf similarity index 89% copy from UefiCpuPkg/Library/SmmRelocationLib/SmmRelocationLib.inf copy to UefiCpuPkg/Library/SmmRelocationLib/AmdSmmRelocationLib.inf index 45fdaf35bc..e69079fa02 100644 --- a/UefiCpuPkg/Library/SmmRelocationLib/SmmRelocationLib.inf +++ b/UefiCpuPkg/Library/SmmRelocationLib/AmdSmmRelocationLib.inf @@ -13,18 +13,18 @@ ## [Defines] INF_VERSION = 0x00010005 BASE_NAME = SmmRelocationLib - FILE_GUID = 853E97B3-790C-4EA3-945C-8F622FC47FE8 + FILE_GUID = 65C74DCD-0D09-494A-8BFF-A64226EB8054 MODULE_TYPE = PEIM VERSION_STRING = 1.0 LIBRARY_CLASS = SmmRelocationLib [Sources] InternalSmmRelocationLib.h - SmramSaveStateConfig.c + AmdSmramSaveStateConfig.c SmmRelocationLib.c [Sources.Ia32] Ia32/Semaphore.c Ia32/SmmInit.nasm @@ -40,11 +40,10 @@ [LibraryClasses] BaseLib BaseMemoryLib CpuExceptionHandlerLib - CpuLib DebugLib HobLib LocalApicLib MemoryAllocationLib PcdLib diff --git a/UefiCpuPkg/Library/SmmRelocationLib/SmramSaveStateConfig.c b/UefiCpuPkg/Library/SmmRelocationLib/AmdSmramSaveStateConfig.c similarity index 47% copy from UefiCpuPkg/Library/SmmRelocationLib/SmramSaveStateConfig.c copy to UefiCpuPkg/Library/SmmRelocationLib/AmdSmramSaveStateConfig.c index 76d798aba5..068ae60887 100644 --- a/UefiCpuPkg/Library/SmmRelocationLib/SmramSaveStateConfig.c +++ b/UefiCpuPkg/Library/SmmRelocationLib/AmdSmramSaveStateConfig.c @@ -1,14 +1,17 @@ /** @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 +#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. @@ -18,44 +21,18 @@ UINT8 GetMmSaveStateRegisterLma ( VOID ) { - CPUID_VERSION_INFO_EAX RegEax; - CPUID_EXTENDED_CPU_SIG_EDX RegEdx; - UINTN FamilyId; - UINTN ModelId; - UINT32 Eax; - UINT8 SmmSaveStateRegisterLma; - - // - // Determine the mode of the CPU at the time an SMI occurs - // Intel(R) 64 and IA-32 Architectures Software Developer's Manual - // Volume 3C, Section 34.4.1.1 - // - RegEax.Uint32 = GetCpuFamilyModel (); - FamilyId = RegEax.Bits.FamilyId; - ModelId = RegEax.Bits.Model; - if ((FamilyId == 0x06) || (FamilyId == 0x0f)) { - ModelId = ModelId | RegEax.Bits.ExtendedModelId << 4; - } - - RegEdx.Uint32 = 0; - AsmCpuid (CPUID_EXTENDED_FUNCTION, &Eax, NULL, NULL, NULL); - if (Eax >= CPUID_EXTENDED_CPU_SIG) { - AsmCpuid (CPUID_EXTENDED_CPU_SIG, NULL, NULL, NULL, &(RegEdx.Uint32)); - } + UINT8 SmmSaveStateRegisterLma; + UINT32 LMAValue; SmmSaveStateRegisterLma = (UINT8)EFI_MM_SAVE_STATE_REGISTER_LMA_32BIT; - if (RegEdx.Bits.LM) { - SmmSaveStateRegisterLma = (UINT8)EFI_MM_SAVE_STATE_REGISTER_LMA_64BIT; - } - if (FamilyId == 0x06) { - if ((ModelId == 0x17) || (ModelId == 0x0f) || (ModelId == 0x1c)) { - SmmSaveStateRegisterLma = (UINT8)EFI_MM_SAVE_STATE_REGISTER_LMA_64BIT; - } + LMAValue = (UINT32)AsmReadMsr64 (EFER_ADDRESS) & LMA; + if (LMAValue) { + SmmSaveStateRegisterLma = (UINT8)EFI_MM_SAVE_STATE_REGISTER_LMA_64BIT; } return SmmSaveStateRegisterLma; } @@ -69,22 +46,32 @@ VOID EFIAPI ConfigureSmBase ( IN UINT64 SmBase ) { - SMRAM_SAVE_STATE_MAP *CpuState; + AMD_SMRAM_SAVE_STATE_MAP *AmdCpuState; - CpuState = (SMRAM_SAVE_STATE_MAP *)(UINTN)(SMM_DEFAULT_SMBASE + SMRAM_SAVE_STATE_MAP_OFFSET); + AmdCpuState = (AMD_SMRAM_SAVE_STATE_MAP *)(UINTN)(SMM_DEFAULT_SMBASE + SMRAM_SAVE_STATE_MAP_OFFSET); - CpuState->x86.SMBASE = (UINT32)SmBase; + AmdCpuState->x64.SMBASE = (UINT32)SmBase; } /** - Hook the code executed immediately after an RSM instruction on the currently - executing CPU. The mode of code executed immediately after RSM must be - detected, and the appropriate hook must be selected. Always clear the auto - HALT restart flag if it is set. + 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,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. @@ -100,37 +87,39 @@ HookReturnFromSmm ( IN OUT SMRAM_SAVE_STATE_MAP *CpuState, IN UINT64 NewInstructionPointer32, IN UINT64 NewInstructionPointer ) { - UINT64 OriginalInstructionPointer; + UINT64 OriginalInstructionPointer; + AMD_SMRAM_SAVE_STATE_MAP *AmdCpuState; - if (GetMmSaveStateRegisterLma () == EFI_MM_SAVE_STATE_REGISTER_LMA_32BIT) { - OriginalInstructionPointer = (UINT64)CpuState->x86._EIP; - CpuState->x86._EIP = (UINT32)NewInstructionPointer; + AmdCpuState = (AMD_SMRAM_SAVE_STATE_MAP *)CpuState; + if (GetMmSaveStateRegisterLma () == 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 ((CpuState->x86.AutoHALTRestart & BIT0) != 0) { - CpuState->x86.AutoHALTRestart &= ~BIT0; + if ((AmdCpuState->x86.AutoHALTRestart & BIT0) != 0) { + AmdCpuState->x86.AutoHALTRestart &= ~BIT0; } } else { - OriginalInstructionPointer = CpuState->x64._RIP; - if ((CpuState->x64.IA32_EFER & LMA) == 0) { - CpuState->x64._RIP = (UINT32)NewInstructionPointer32; + OriginalInstructionPointer = AmdCpuState->x64._RIP; + if ((AmdCpuState->x64.EFER & LMA) == 0) { + AmdCpuState->x64._RIP = (UINT32)NewInstructionPointer32; } else { - CpuState->x64._RIP = (UINT32)NewInstructionPointer; + 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 ((CpuState->x64.AutoHALTRestart & BIT0) != 0) { - CpuState->x64.AutoHALTRestart &= ~BIT0; + if ((AmdCpuState->x64.AutoHALTRestart & BIT0) != 0) { + AmdCpuState->x64.AutoHALTRestart &= ~BIT0; } } return OriginalInstructionPointer; } diff --git a/UefiCpuPkg/UefiCpuPkg.dsc b/UefiCpuPkg/UefiCpuPkg.dsc index 8f67587e91..3d49f72588 100644 --- a/UefiCpuPkg/UefiCpuPkg.dsc +++ b/UefiCpuPkg/UefiCpuPkg.dsc @@ -200,10 +200,11 @@ } UefiCpuPkg/Library/MmSaveStateLib/AmdMmSaveStateLib.inf UefiCpuPkg/Library/MmSaveStateLib/IntelMmSaveStateLib.inf UefiCpuPkg/Library/SmmCpuFeaturesLib/AmdSmmCpuFeaturesLib.inf UefiCpuPkg/Library/SmmRelocationLib/SmmRelocationLib.inf + UefiCpuPkg/Library/SmmRelocationLib/AmdSmmRelocationLib.inf [Components.X64] UefiCpuPkg/Library/CpuExceptionHandlerLib/UnitTest/DxeCpuExceptionHandlerLibUnitTest.inf [Components.RISCV64] -- 2.16.2.windows.1 -=-=-=-=-=-=-=-=-=-=-=- Groups.io Links: You receive all messages sent to this group. View/Reply Online (#117951): https://edk2.groups.io/g/devel/message/117951 Mute This Topic: https://groups.io/mt/105593574/7686176 Group Owner: devel+owner@edk2.groups.io Unsubscribe: https://edk2.groups.io/g/devel/unsub [rebecca@openfw.io] -=-=-=-=-=-=-=-=-=-=-=-