From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: mx.groups.io; dkim=pass header.i=@amdcloud.onmicrosoft.com header.s=selector2-amdcloud-onmicrosoft-com header.b=KSU+bza5; spf=none, err=SPF record not found (domain: amd.com, ip: 40.107.78.57, mailfrom: thomas.lendacky@amd.com) 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 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amdcloud.onmicrosoft.com; s=selector2-amdcloud-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=BFsWp4wKhdW5TFigaa6fEcY1iuCs7yqCoEvw/+B2Ukg=; b=KSU+bza5PqSqyUPcEKpdPN2XnEZaIA6d+1cOyLw4P7Rj6I+pppmVfG4D+vvW1JqWqYUsC7W7cjM7amn6984KFhQ/s981ikwFEchfqRlLYWiRirDO/NtE/TMxPdgHcdQrC8urT9YKUlxXakYoe6qTIqxJO117f7P32Y6ip0pkKy4= 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 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: [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-mailer: git-send-email 2.17.1 x-clientproxiedby: SN4PR0501CA0146.namprd05.prod.outlook.com (2603:10b6:803:2c::24) To DM6PR12MB3163.namprd12.prod.outlook.com (2603:10b6:5:182::22) authentication-results: spf=none (sender IP is ) smtp.mailfrom=Thomas.Lendacky@amd.com; 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-microsoft-antispam: BCL:0;PCL:0;RULEID:(2390118)(7020095)(4652040)(8989299)(5600167)(711020)(4605104)(1401327)(4618075)(4534185)(4627221)(201703031133081)(201702281549075)(8990200)(2017052603328)(7193020);SRVR:DM6PR12MB3228; 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; x-forefront-prvs: 016572D96D x-forefront-antispam-report: SFV:NSPM;SFS:(10009020)(4636009)(1496009)(366004)(376002)(396003)(136003)(39860400002)(346002)(189003)(199004)(4326008)(26005)(446003)(25786009)(52116002)(14444005)(19627235002)(71200400001)(71190400001)(8936002)(81156014)(81166006)(256004)(8676002)(186003)(50226002)(2351001)(2616005)(102836004)(486006)(11346002)(476003)(66066001)(386003)(6506007)(66446008)(14454004)(76176011)(99286004)(6486002)(36756003)(5640700003)(66556008)(118296001)(3846002)(316002)(7736002)(2501003)(1730700003)(6116002)(5660300002)(478600001)(54906003)(6916009)(966005)(6436002)(2906002)(64756008)(305945005)(86362001)(6512007)(6306002)(66476007)(66946007);DIR:OUT;SFP:1101;SCL:1;SRVR:DM6PR12MB3228;H:DM6PR12MB3163.namprd12.prod.outlook.com;FPR:;SPF:None;LANG:en;PTR:InfoNoRecords;MX:1;A:1; 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 Content-Language: en-US Content-Type: text/plain; charset="us-ascii" Content-ID: Content-Transfer-Encoding: quoted-printable 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