From nobody Wed Oct 1 20:45:30 2025 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zoho.com: domain of groups.io designates 66.175.222.12 as permitted sender) smtp.mailfrom=bounce+27952+47655+1787277+3901457@groups.io; arc=fail (BodyHash is different from the expected one) Received: from web01.groups.io (web01.groups.io [66.175.222.12]) by mx.zohomail.com with SMTPS id 1568922771075305.19276863105006; Thu, 19 Sep 2019 12:52:51 -0700 (PDT) Return-Path: X-Received: by 127.0.0.2 with SMTP id bjNXYY1788612x43FkzNbo7W; Thu, 19 Sep 2019 12:52:50 -0700 X-Received: from NAM03-BY2-obe.outbound.protection.outlook.com (NAM03-BY2-obe.outbound.protection.outlook.com [40.107.78.57]) by groups.io with SMTP; Thu, 19 Sep 2019 12:52:49 -0700 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=hxz2XhjsT5rxOgJDnpqb6XP1mYjBRF9XLhqcUc2E35RzG2TIIYdfadNxTVJCrAy5BSsjTvTuXXK8tWLOxYHC/2Dt9NsnhPT8zP7hr1smobHY9KHjvjCmJQn085gjhoLmeLOEJLhVASm5mouv+of1Zb+xI6YQ5hf6rojkYSvILYOThjV0i9j9wjg9GcWRKgW7abCmtF5F1tqwDxsnRaA9VzwWFMfSsjU8F2YHl4o7p8TK+qrZdk+VUJ0X2l2wg1dSYWjLVcUx6BZUbfSnuRS7Tl+yo0V603eZjTt0B62YXGiGPbKZ7fzxKr7c6VKt4LhAB/UOtjK656LtaTxUVIByKg== 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-SenderADCheck; bh=BFsWp4wKhdW5TFigaa6fEcY1iuCs7yqCoEvw/+B2Ukg=; b=j+O0bX7SqE9ztnVz53iEayr4aeRYiEGRaXrKqU80C38MqpmKSEAr+vzpmzK7DDs/S8J/ZMkRof+NxySUpCQUi4sLsWlOKwIa9igmwPGAJSUnf8w2905T8A8Mr+xO0Qsi3YyjGxVr3LgZCsyhsKuziGaBDwgz0IQOhessSmzeydF6ACm+PlefodaIGOWZQ/2Z7YYCahe+gYZ8mrzNDYVZ10k1LXxXPu+4uGQcO/G8P/NqfvO8tjYgxDo7Zbmfd4QcpUMT3km4kUl3x6SEdFfDLbHtJ/PfsVBSogbfWblECbRv8dsmktCIYWtJSYfTTZgHIad74sT+v54ie8lhvAfpHQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=amd.com; dmarc=pass action=none header.from=amd.com; dkim=pass header.d=amd.com; arc=none X-Received: from DM6PR12MB3163.namprd12.prod.outlook.com (20.179.104.150) by DM6PR12MB3228.namprd12.prod.outlook.com (20.179.105.96) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.2263.13; Thu, 19 Sep 2019 19:52:48 +0000 X-Received: from DM6PR12MB3163.namprd12.prod.outlook.com ([fe80::400e:f0c3:7ca:2fcc]) by DM6PR12MB3163.namprd12.prod.outlook.com ([fe80::400e:f0c3:7ca:2fcc%6]) with mapi id 15.20.2284.009; Thu, 19 Sep 2019 19:52:48 +0000 From: "Lendacky, Thomas" To: "devel@edk2.groups.io" CC: Jordan Justen , Laszlo Ersek , Ard Biesheuvel , Michael D Kinney , Liming Gao , Eric Dong , Ray Ni , "Singh, Brijesh" Subject: [edk2-devel] [RFC PATCH v2 21/44] MdePkg: Add support for the XGETBV instruction Thread-Topic: [RFC PATCH v2 21/44] MdePkg: Add support for the XGETBV instruction Thread-Index: AQHVbyPQn9lGvqvv6EGd5fRiEOXTKQ== Date: Thu, 19 Sep 2019 19:52:48 +0000 Message-ID: References: In-Reply-To: Accept-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-clientproxiedby: SN4PR0501CA0146.namprd05.prod.outlook.com (2603:10b6:803:2c::24) To DM6PR12MB3163.namprd12.prod.outlook.com (2603:10b6:5:182::22) x-ms-exchange-messagesentrepresentingtype: 1 x-originating-ip: [165.204.78.1] x-ms-publictraffictype: Email x-ms-office365-filtering-correlation-id: e45d9153-6019-41b4-9ba3-08d73d3af27a x-ms-office365-filtering-ht: Tenant x-ms-traffictypediagnostic: DM6PR12MB3228: x-ms-exchange-purlcount: 1 x-ms-exchange-transport-forked: True x-microsoft-antispam-prvs: x-ms-oob-tlc-oobclassifiers: OLM:6430; Received-SPF: pass (zoho.com: domain of groups.io designates 66.175.222.12 as permitted sender) client-ip=66.175.222.12; envelope-from=bounce+27952+47655+1787277+3901457@groups.io; helo=web01.groups.io; received-spf: None (protection.outlook.com: amd.com does not designate permitted sender hosts) x-ms-exchange-senderadcheck: 1 x-microsoft-antispam-message-info: Zhl/aWkU1KoqXVNmr21yGOf594mqyv0utbkoEFhSzLhxnmUML+bnZkWc0tMamiLKLGfHBFJ+OEx9m0OYJGdcSuWPLt5olBHe7VRqH/ldMU/7LyWIz23ld0n8LiDppjamUt9WW2EMZaMNqTqHTtNApNztj4bHjjAoFJzGzsXQGXq9Rii4wR/twTydstmXC1chScpFNkWmEZadajg6C+3n/+39HnMfGConVU2Ls7+jOW7suoMsnBU31TcCCled05jJO5kTwQzepiJ0Rlbbtcl6nyOthqmBKCORBhSNjWjfh/HnebTPROsJhfcszIMqDs0F/8vK8goUWSy0ab8dAyg2s3fs1xpIYk1U4d6JvBVu68/7o5E7ozZRDQBUFiCGH9RS9+1fNGsJ5qHaELpKQpfs3yCnbw80ZWMoOH25v9HMZXI= MIME-Version: 1.0 X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-Network-Message-Id: e45d9153-6019-41b4-9ba3-08d73d3af27a X-MS-Exchange-CrossTenant-originalarrivaltime: 19 Sep 2019 19:52:48.3982 (UTC) X-MS-Exchange-CrossTenant-fromentityheader: Hosted X-MS-Exchange-CrossTenant-id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-mailboxtype: HOSTED X-MS-Exchange-CrossTenant-userprincipalname: ko6KYhCqWRTW6kfEK/zygULt6RRkdLkDJRAw+W7MyO6HImafXKd9uo+1EzLI5KvbLK4cPCOv/V8iaeVPQDxx6w== X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM6PR12MB3228 Precedence: Bulk List-Unsubscribe: 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,thomas.lendacky@amd.com X-Gm-Message-State: Gos2Sd8TqEb3Dh2x0v95w2FHx1787277AA= Content-Language: en-US Content-ID: Content-Transfer-Encoding: quoted-printable DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=groups.io; q=dns/txt; s=20140610; t=1568922770; bh=ij6WwK4w3FThrRtmu5v4JaCUzYHEYmpSY1f/m+9K2cE=; h=CC:Content-Type:Date:From:Reply-To:Subject:To; b=k5aq6aK6oGigGFd55ihft9NTyI2RHCymAsaDVPa5SDN82pb1imnKLPU7DITph/+WtHv k6LhLHxA6a9h3PZkkFRCiCwU60A8y1N6CQOUw/9PQCcQz7FKjhfCYiDa9B5aTjmGtniI7 tzGo6yF39trCFqrHVTKma6PiMY7WYHU+NWg= X-ZohoMail-DKIM: pass (identity @groups.io) Content-Type: text/plain; charset="utf-8" From: Tom Lendacky BZ: https://bugzilla.tianocore.org/show_bug.cgi?id=3D2198 Under SEV-ES, a CPUID instruction requires the current value of the XCR0 register. In order to retrieve that value, the XGETBV instruction needs to be executed. Provide the necessary support to execute the XGETBV instruction. Cc: Michael D Kinney Cc: Liming Gao Signed-off-by: Tom Lendacky --- MdePkg/Library/BaseLib/BaseLib.inf | 2 ++ MdePkg/Include/Library/BaseLib.h | 16 ++++++++++++ MdePkg/Library/BaseLib/Ia32/GccInline.c | 28 ++++++++++++++++++++ MdePkg/Library/BaseLib/X64/GccInline.c | 30 ++++++++++++++++++++++ MdePkg/Library/BaseLib/Ia32/XGetBv.nasm | 31 ++++++++++++++++++++++ MdePkg/Library/BaseLib/X64/XGetBv.nasm | 34 +++++++++++++++++++++++++ 6 files changed, 141 insertions(+) create mode 100644 MdePkg/Library/BaseLib/Ia32/XGetBv.nasm create mode 100644 MdePkg/Library/BaseLib/X64/XGetBv.nasm diff --git a/MdePkg/Library/BaseLib/BaseLib.inf b/MdePkg/Library/BaseLib/Ba= seLib.inf index 14b3f5721101..ea3e284bf8bf 100644 --- a/MdePkg/Library/BaseLib/BaseLib.inf +++ b/MdePkg/Library/BaseLib/BaseLib.inf @@ -153,6 +153,7 @@ [Sources.Ia32] Ia32/EnableCache.c | MSFT Ia32/DisableCache.c | MSFT Ia32/VmgExit.nasm | MSFT + Ia32/XGetBv.nasm | MSFT =20 =20 Ia32/GccInline.c | GCC @@ -288,6 +289,7 @@ [Sources.X64] X64/ReadCr0.nasm| MSFT X64/ReadEflags.nasm| MSFT X64/VmgExit.nasm | MSFT + X64/XGetBv.nasm | MSFT =20 =20 X64/Non-existing.c diff --git a/MdePkg/Include/Library/BaseLib.h b/MdePkg/Include/Library/Base= Lib.h index 80bd5cf57a72..362bc2e05dbc 100644 --- a/MdePkg/Include/Library/BaseLib.h +++ b/MdePkg/Include/Library/BaseLib.h @@ -7893,6 +7893,22 @@ AsmVmgExit ( VOID ); =20 +/** + Executes a XGETBV instruction + + Executes a XGETBV instruction. This function is only available on IA-32 = and + x64. + + @param[in] Index Extended control register index + + @retval The current value of the extended control regist= er +**/ +UINT64 +EFIAPI +AsmXGetBv ( + IN UINT32 Index + ); + =20 /** Patch the immediate operand of an IA32 or X64 instruction such that the = byte, diff --git a/MdePkg/Library/BaseLib/Ia32/GccInline.c b/MdePkg/Library/BaseL= ib/Ia32/GccInline.c index 55d2e12bcdc9..9db5e48d9899 100644 --- a/MdePkg/Library/BaseLib/Ia32/GccInline.c +++ b/MdePkg/Library/BaseLib/Ia32/GccInline.c @@ -1780,3 +1780,31 @@ AsmVmgExit ( } =20 =20 +/** + Executes a XGETBV instruction + + Executes a XGETBV instruction. This function is only available on IA-32 = and + x64. + + @param[in] Index Extended control register index + + @retval The current value of the extended control regist= er +**/ +UINT64 +EFIAPI +AsmXGetBv ( + IN UINT32 Index + ) +{ + UINT64 Data; + + __asm__ __volatile__ ( + "xgetbv" + : "=3DA" (Data) + : "c" (Index) + ); + + return Data; +} + + diff --git a/MdePkg/Library/BaseLib/X64/GccInline.c b/MdePkg/Library/BaseLi= b/X64/GccInline.c index 17539caa0798..0169382978ac 100644 --- a/MdePkg/Library/BaseLib/X64/GccInline.c +++ b/MdePkg/Library/BaseLib/X64/GccInline.c @@ -1815,3 +1815,33 @@ AsmVmgExit ( } =20 =20 +/** + Executes a XGETBV instruction + + Executes a XGETBV instruction. This function is only available on IA-32 = and + x64. + + @param[in] Index Extended control register index + + @retval The current value of the extended control regist= er +**/ +UINT64 +EFIAPI +AsmXGetBv ( + IN UINT32 Index + ) +{ + UINT32 LowData; + UINT32 HighData; + + __asm__ __volatile__ ( + "xgetbv" + : "=3Da" (LowData), + "=3Dd" (HighData) + : "c" (Index) + ); + + return (((UINT64)HighData) << 32) | LowData; +} + + diff --git a/MdePkg/Library/BaseLib/Ia32/XGetBv.nasm b/MdePkg/Library/BaseL= ib/Ia32/XGetBv.nasm new file mode 100644 index 000000000000..8d14ba03d4a8 --- /dev/null +++ b/MdePkg/Library/BaseLib/Ia32/XGetBv.nasm @@ -0,0 +1,31 @@ +;-------------------------------------------------------------------------= ----- +; +; Copyright (c) 2019, Advanced Micro Device, Inc. All rights reserved.
+; SPDX-License-Identifier: BSD-2-Clause-Patent +; +; Module Name: +; +; XGetBv.Asm +; +; Abstract: +; +; AsmXgetBv function +; +; Notes: +; +;-------------------------------------------------------------------------= ----- + + SECTION .text + +;-------------------------------------------------------------------------= ----- +; UINT64 +; EFIAPI +; AsmXGetBv ( +; IN UINT32 Index +; ); +;-------------------------------------------------------------------------= ----- +global ASM_PFX(AsmXGetBv) +ASM_PFX(AsmXGetBv): + mov ecx, [esp + 4] + xgetbv + ret diff --git a/MdePkg/Library/BaseLib/X64/XGetBv.nasm b/MdePkg/Library/BaseLi= b/X64/XGetBv.nasm new file mode 100644 index 000000000000..239ecb58ab13 --- /dev/null +++ b/MdePkg/Library/BaseLib/X64/XGetBv.nasm @@ -0,0 +1,34 @@ +;-------------------------------------------------------------------------= ----- +; +; Copyright (c) 2019, Advanced Micro Device, Inc. All rights reserved.
+; SPDX-License-Identifier: BSD-2-Clause-Patent +; +; Module Name: +; +; XGetBv.Asm +; +; Abstract: +; +; AsmXgetBv function +; +; Notes: +; +;-------------------------------------------------------------------------= ----- + + DEFAULT REL + SECTION .text + +;-------------------------------------------------------------------------= ----- +; UINT64 +; EFIAPI +; AsmXGetBv ( +; IN UINT32 Index +; ); +;-------------------------------------------------------------------------= ----- +global ASM_PFX(AsmXGetBv) +ASM_PFX(AsmXGetBv): + xgetbv + shl rdx, 0x20 + or rax, rdx + ret + --=20 2.17.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 (#47655): https://edk2.groups.io/g/devel/message/47655 Mute This Topic: https://groups.io/mt/34203557/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-