From nobody Fri Dec 19 22:07:53 2025 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) smtp.mailfrom=bounce+27952+101867+1787277+3901457@groups.io; arc=fail (BodyHash is different from the expected one) Received: from mail02.groups.io (mail02.groups.io [66.175.222.108]) by mx.zohomail.com with SMTPS id 1679722755068995.9847431681573; Fri, 24 Mar 2023 22:39:15 -0700 (PDT) Return-Path: X-Received: by 127.0.0.2 with SMTP id YZ47YY1788612xUAgra4zZxS; Fri, 24 Mar 2023 22:39:14 -0700 X-Received: from NAM10-BN7-obe.outbound.protection.outlook.com (NAM10-BN7-obe.outbound.protection.outlook.com [40.107.92.52]) by mx.groups.io with SMTP id smtpd.web11.16385.1679722754101063561 for ; Fri, 24 Mar 2023 22:39:14 -0700 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=kPQXUMvmeahgXau2MaT15xEZaZN1id/UfuO0+M0VLaaMheYiddkn2K5B4+F+pb3rRJ4v//gne2CdszPZ4rKeQPBpB21tNxf5kSuyCl672zCJ92W8muc/I9jcugq+dCihceuPX/ZAAuA5+eIPhpKFFda5bbJ8mwfXnvQpkcXSIk/1hSg4BZ5ShVmijt7tt82+kfaN4NJUYs68R+vE6jbBAT0KUUlOqY6W8dmSeIcxPR17V/G1gZUanWdFfO6IcYOu2QsdJfYIjAiJheaU/YuZppMqxN+44oPItMo2adyihxarqz9ZPzbYNycOsf9ow1PiraIjd94yrpt1lGMqtPxL5g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=msWDdxhvyL6I4fManKcVeJDaPEbJyqcLEkXz6/eNe0c=; b=TQ0QdYh0Ny/ncQi8qeWCNEFVBxPFzrAw90+DOQ6ZgQbyXoEE14MXJejz16cgEVxUlGIMs0UU+V3klfDl0A7mAXSifuyNCCOx5l+G1YGjQw8bURNrhoV5VfHDji4A0CcxWM7dg5HrHle8vXmCi8NhxRxokOpQdV94qn/48Y+OPvE7nZMHBilo4vSYZaeLGbBpLsju2bYgFFEdqCUCBemMp3V+YOxiSjsYbApcSZ4myZKigCxiWwUwXAw+Im3i39XsO60+u0ltU26UHU5ygd2hXKzMTIxnbphT+xx9XElKWBvmgO2oRqeAITCkjcck7keinuyr/uBvnrrihfQIlN1rEQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=edk2.groups.io smtp.mailfrom=amd.com; dmarc=pass (p=quarantine sp=quarantine pct=100) action=none header.from=amd.com; dkim=none (message not signed); arc=none X-Received: from BN9PR03CA0427.namprd03.prod.outlook.com (2603:10b6:408:113::12) by PH8PR12MB7278.namprd12.prod.outlook.com (2603:10b6:510:222::7) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6178.41; Sat, 25 Mar 2023 05:39:09 +0000 X-Received: from BN8NAM11FT059.eop-nam11.prod.protection.outlook.com (2603:10b6:408:113:cafe::f7) by BN9PR03CA0427.outlook.office365.com (2603:10b6:408:113::12) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6178.40 via Frontend Transport; Sat, 25 Mar 2023 05:39:08 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=amd.com; Received-SPF: pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) client-ip=66.175.222.108; envelope-from=bounce+27952+101867+1787277+3901457@groups.io; helo=mail02.groups.io; Received-SPF: Pass (protection.outlook.com: domain of amd.com designates 165.204.84.17 as permitted sender) receiver=protection.outlook.com; client-ip=165.204.84.17; helo=SATLEXMB03.amd.com; pr=C X-Received: from SATLEXMB03.amd.com (165.204.84.17) by BN8NAM11FT059.mail.protection.outlook.com (10.13.177.120) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.6222.22 via Frontend Transport; Sat, 25 Mar 2023 05:39:08 +0000 X-Received: from SATLEXMB08.amd.com (10.181.40.132) by SATLEXMB03.amd.com (10.181.40.144) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.34; Sat, 25 Mar 2023 00:39:08 -0500 X-Received: from SATLEXMB04.amd.com (10.181.40.145) by SATLEXMB08.amd.com (10.181.40.132) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.34; Fri, 24 Mar 2023 22:39:07 -0700 X-Received: from BLR-LAB-SFW01.amd.com (10.180.168.240) by SATLEXMB04.amd.com (10.181.40.145) with Microsoft SMTP Server id 15.1.2375.34 via Frontend Transport; Sat, 25 Mar 2023 00:39:05 -0500 From: "Abdul Lateef Attar via groups.io" To: CC: Abdul Lateef Attar , Paul Grimes , Garrett Kirkendall , Abner Chang , Eric Dong , Ray Ni , Rahul Kumar , Abdul Lateef Attar Subject: [edk2-devel] [PATCH v6 6/6] UefiCpuPkg: Implements SmmCpuFeaturesLib for AMD Family Date: Sat, 25 Mar 2023 11:08:45 +0530 Message-ID: <19f2778bccfc88ef2f47960a4ae8c7184df3a303.1679722393.git.abdattar@amd.com> In-Reply-To: References: MIME-Version: 1.0 X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: BN8NAM11FT059:EE_|PH8PR12MB7278:EE_ X-MS-Office365-Filtering-Correlation-Id: 96653fe4-f4e1-4437-1c3f-08db2cf3413d X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam-Message-Info: 75lmcoZ95wEOWXQ8ywimPOpT/0z/EOhg0UmZ21LA0p5/vfaxWMbTrqEoSU2c6sg2jqjC0SnMGHXeLYQqY8Nq1AS26SK6ai1rh8N+ZIlAChLBLfWQXbYLvlv7vpHYW0u5HxvgJGdDz97Hpe9fv/OsegyN0ka8btKhdGaxPx1hFkvLnLttBrRfUsbb+ujeRPm7Noew50NsI1yohdo2gEQCm2KbVetWtVmrMWB857PFKh8RInNtbH62AoyPJHXQlntQjQy9ktWgLv0VwztKngDDwrSuNJ0NxA+gW5U8XsUCDdU+RFYLyjX2tw2IiParLfl5mIwSPz4BqgJNWYKGDkWsgCPPeS8xIeKI3X5quiR7zmAaZK1O68Ux/NwDarO9DLM4gSIkjFTROA0fFuMfMct4WW62Lbh56m8oDSvsKH7T2WxVYIWshOAmQwGMuuUUHN9ig7+P6xEm8GzFHz3qZ3tDwzeZE3oU1n1LjkaMANw/c5/eAtiTbSk40sm+StNALvu3FHSecA4mgTQn6RhnymIrsimGTQaKrPPgxcbTunz5m61HAI7DQB4XEU29d2/qOoThJemcLjwLz6KLMGXnQmxZmTzvaMtJB8unUl/QiN1snHs97P7SbYk68agc3JqocXztowyJJB/bCnmy4uHlIVCkyLNdPqpOvuE0ew3kglX8ygH4LmPuSZKsAqAwA5n2sRgDI2g0yLANQ7+ZI1HYnQ5oSinyf66RzFWpiaQnyG7fbGs= X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 25 Mar 2023 05:39:08.5787 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 96653fe4-f4e1-4437-1c3f-08db2cf3413d X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=3dd8961f-e488-4e60-8e11-a82d994e183d;Ip=[165.204.84.17];Helo=[SATLEXMB03.amd.com] X-MS-Exchange-CrossTenant-AuthSource: BN8NAM11FT059.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: PH8PR12MB7278 Precedence: Bulk List-Unsubscribe: List-Subscribe: List-Help: Sender: devel@edk2.groups.io List-Id: Mailing-List: list devel@edk2.groups.io; contact devel+owner@edk2.groups.io Reply-To: devel@edk2.groups.io,abdattar@amd.com X-Gm-Message-State: 34fBLF6k01NYTQpqj7KvpYHXx1787277AA= Content-Transfer-Encoding: quoted-printable DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=groups.io; q=dns/txt; s=20140610; t=1679722754; bh=par2VW0thMK5/ZINIBp37jRowOeXcjGKom6yLBYNAo0=; h=CC:Content-Type:Date:From:Reply-To:Subject:To; b=sPivCjEH24sOwjyTRoFqIQnezeYxDOW4P2Zj+njfXbpSOIHbVjOi4aU6Jp6aSyU52J8 hywqbTOfFnm8o61oddDcXuw2l9N7A5F53RAmm5rZqrzR4qZOGDx5KQffX2QA+/95xvIGV oNLgo83XFgE9T/KdYICun7feOavi77LTmTQ= X-ZohoMail-DKIM: pass (identity @groups.io) X-ZM-MESSAGEID: 1679722756511100002 Content-Type: text/plain; charset="utf-8" From: Abdul Lateef Attar BZ: https://bugzilla.tianocore.org/show_bug.cgi?id=3D4182 Implements interfaces to read and write save state registers of AMD's processor family. Initializes processor SMMADDR and MASK depends on PcdSmrrEnable flag. Program or corrects the IP once control returns from SMM. Cc: Paul Grimes Cc: Garrett Kirkendall Cc: Abner Chang Cc: Eric Dong Cc: Ray Ni Cc: Rahul Kumar Signed-off-by: Abdul Lateef Attar Reviewed-by: Abner Chang --- .../AmdSmmCpuFeaturesLib.inf | 6 + .../SmmCpuFeaturesLib/AmdSmmCpuFeaturesLib.c | 106 +++++++++++++++++- 2 files changed, 109 insertions(+), 3 deletions(-) diff --git a/UefiCpuPkg/Library/SmmCpuFeaturesLib/AmdSmmCpuFeaturesLib.inf = b/UefiCpuPkg/Library/SmmCpuFeaturesLib/AmdSmmCpuFeaturesLib.inf index 4c77efc64462..9d5b8c2e972d 100644 --- a/UefiCpuPkg/Library/SmmCpuFeaturesLib/AmdSmmCpuFeaturesLib.inf +++ b/UefiCpuPkg/Library/SmmCpuFeaturesLib/AmdSmmCpuFeaturesLib.inf @@ -31,3 +31,9 @@ [LibraryClasses] PcdLib MemoryAllocationLib DebugLib + SmmSmramSaveStateLib + +[FeaturePcd] + gUefiCpuPkgTokenSpaceGuid.PcdSmrrEnable ## CONSUMES + gUefiCpuPkgTokenSpaceGuid.PcdSmmFeatureControlEnable ## CONSUMES + diff --git a/UefiCpuPkg/Library/SmmCpuFeaturesLib/AmdSmmCpuFeaturesLib.c b/= UefiCpuPkg/Library/SmmCpuFeaturesLib/AmdSmmCpuFeaturesLib.c index c74e1a0c0c5b..af45be3e265a 100644 --- a/UefiCpuPkg/Library/SmmCpuFeaturesLib/AmdSmmCpuFeaturesLib.c +++ b/UefiCpuPkg/Library/SmmCpuFeaturesLib/AmdSmmCpuFeaturesLib.c @@ -11,6 +11,21 @@ SPDX-License-Identifier: BSD-2-Clause-Patent =20 #include #include +#include +#include +#include +#include + +// EFER register LMA bit +#define LMA BIT10 + +// Machine Specific Registers (MSRs) +#define SMMADDR_ADDRESS 0xC0010112ul +#define SMMMASK_ADDRESS 0xC0010113ul +#define EFER_ADDRESS 0XC0000080ul + +// The mode of the CPU at the time an SMI occurs +STATIC UINT8 mSmmSaveStateRegisterLma; =20 /** Read an SMM Save State register on the target processor. If this functi= on @@ -39,7 +54,7 @@ SmmCpuFeaturesReadSaveStateRegister ( OUT VOID *Buffer ) { - return EFI_SUCCESS; + return SmramSaveStateReadRegister (CpuIndex, Register, Width, Buffer); } =20 /** @@ -67,7 +82,7 @@ SmmCpuFeaturesWriteSaveStateRegister ( IN CONST VOID *Buffer ) { - return EFI_SUCCESS; + return SmramSaveStateWriteRegister (CpuIndex, Register, Width, Buffer); } =20 /** @@ -82,6 +97,13 @@ CpuFeaturesLibInitialization ( VOID ) { + UINT32 LMAValue; + + LMAValue =3D (UINT32)AsmReadMsr64 (EFER_ADDRESS) & LMA; + mSmmSaveStateRegisterLma =3D EFI_SMM_SAVE_STATE_REGISTER_LMA_32BIT; + if (LMAValue) { + mSmmSaveStateRegisterLma =3D EFI_SMM_SAVE_STATE_REGISTER_LMA_64BIT; + } } =20 /** @@ -117,6 +139,52 @@ SmmCpuFeaturesInitializeProcessor ( IN CPU_HOT_PLUG_DATA *CpuHotPlugData ) { + AMD_SMRAM_SAVE_STATE_MAP *CpuState; + UINT32 LMAValue; + + // + // Configure SMBASE. + // + CpuState =3D (AMD_SMRAM_SAVE_STATE_MAP *)(UINTN)(SMM_DEFAULT= _SMBASE + SMRAM_SAVE_STATE_MAP_OFFSET); + CpuState->x64.SMBASE =3D (UINT32)CpuHotPlugData->SmBase[CpuIndex]; + + // Re-initialize the value of mSmmSaveStateRegisterLma flag which might = have been changed in PiCpuSmmDxeSmm Driver + // Entry point, to make sure correct value on AMD platform is assigned t= o be used by SmmCpuFeaturesLib. + LMAValue =3D (UINT32)AsmReadMsr64 (EFER_ADDRESS) & LMA; + mSmmSaveStateRegisterLma =3D EFI_SMM_SAVE_STATE_REGISTER_LMA_32BIT; + if (LMAValue) { + mSmmSaveStateRegisterLma =3D EFI_SMM_SAVE_STATE_REGISTER_LMA_64BIT; + } + + // + // If SMRR is supported, then program SMRR base/mask MSRs. + // The EFI_MSR_SMRR_PHYS_MASK_VALID bit is not set until the first norma= l SMI. + // The code that initializes SMM environment is running in normal mode + // from SMRAM region. If SMRR is enabled here, then the SMRAM region + // is protected and the normal mode code execution will fail. + // + if (FeaturePcdGet (PcdSmrrEnable)) { + // + // SMRR size cannot be less than 4-KBytes + // SMRR size must be of length 2^n + // SMRR base alignment cannot be less than SMRR length + // + if ((CpuHotPlugData->SmrrSize < SIZE_4KB) || + (CpuHotPlugData->SmrrSize !=3D GetPowerOfTwo32 (CpuHotPlugData->Sm= rrSize)) || + ((CpuHotPlugData->SmrrBase & ~(CpuHotPlugData->SmrrSize - 1)) !=3D= CpuHotPlugData->SmrrBase)) + { + // + // Print message and halt if CPU is Monarch + // + if (IsMonarch) { + DEBUG ((DEBUG_ERROR, "SMM Base/Size does not meet alignment/size r= equirement!\n")); + CpuDeadLoop (); + } + } else { + AsmWriteMsr64 (SMMADDR_ADDRESS, CpuHotPlugData->SmrrBase); + AsmWriteMsr64 (SMMMASK_ADDRESS, ((~(UINT64)(CpuHotPlugData->SmrrSize= - 1)) | 0x6600)); + } + } } =20 /** @@ -159,7 +227,39 @@ SmmCpuFeaturesHookReturnFromSmm ( IN UINT64 NewInstructionPointer ) { - return 0; + UINT64 OriginalInstructionPointer; + AMD_SMRAM_SAVE_STATE_MAP *AmdCpuState; + + AmdCpuState =3D (AMD_SMRAM_SAVE_STATE_MAP *)CpuState; + + if (mSmmSaveStateRegisterLma =3D=3D EFI_SMM_SAVE_STATE_REGISTER_LMA_32BI= T) { + OriginalInstructionPointer =3D (UINT64)AmdCpuState->x86._EIP; + AmdCpuState->x86._EIP =3D (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) !=3D 0) { + AmdCpuState->x86.AutoHALTRestart &=3D ~BIT0; + } + } else { + OriginalInstructionPointer =3D AmdCpuState->x64._RIP; + if ((AmdCpuState->x64.EFER & LMA) =3D=3D 0) { + AmdCpuState->x64._RIP =3D (UINT32)NewInstructionPointer32; + } else { + AmdCpuState->x64._RIP =3D (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) !=3D 0) { + AmdCpuState->x64.AutoHALTRestart &=3D ~BIT0; + } + } + + return OriginalInstructionPointer; } =20 /** --=20 2.25.1 -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D- Groups.io Links: You receive all messages sent to this group. View/Reply Online (#101867): https://edk2.groups.io/g/devel/message/101867 Mute This Topic: https://groups.io/mt/97839119/1787277 Group Owner: devel+owner@edk2.groups.io Unsubscribe: https://edk2.groups.io/g/devel/unsub [importer@patchew.org] -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-