From nobody Sun Feb 8 21:06:22 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of groups.io designates 66.175.222.12 as permitted sender) smtp.mailfrom=bounce+27952+53752+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 158085732868724.714125676464846; Tue, 4 Feb 2020 15:02:08 -0800 (PST) Return-Path: X-Received: by 127.0.0.2 with SMTP id YjDqYY1788612xte4W9xlrMx; Tue, 04 Feb 2020 15:02:08 -0800 X-Received: from NAM10-BN7-obe.outbound.protection.outlook.com (NAM10-BN7-obe.outbound.protection.outlook.com []) by mx.groups.io with SMTP id smtpd.web09.1685.1580857326836339527 for ; Tue, 04 Feb 2020 15:02:07 -0800 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=cdDSi/Fi+0x149cJMipRpDueAnKYKP3Ui/bjg1fpcoKcdirUNEiqc95rnnI0InXQ5AsalPGuBpEqNxRUEYAZk4QzBsXXDZ67dt9YLeRArTi2CJZHM6yrCPGPiA6i803XAiaZoKZgP5ERFU9KUhZsVus8M1gsbcfPEPaCIyjW3FxKsYhYrkyZ0QWUl4vIMo74ioCzMZb3cVQdQ18fsjmPSIlM3wKVyxTYnuZpVy/N2ZBGq7eLr9waozzBH1X6r51N37OiASYl/Y40b8p+Fj6uCtLYZpsTC3Cf1ZpEWqTofIsf381eAAOVaHmoHsbhFLHWOxAb/Xif9X2RPbKbbOgBWw== 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=dJLAtSvxBEhMMaH044vhrl8h8HKGd+FxPghL2yN868s=; b=a5Sj2o7fw7G6HCw0/uCHNQWVf0ttAmzna4v5BvkjxsBcbHmbWa9NKygHS7BA1boNC4Y9+cThwsF7qUawt0iZqBLjpJmO8K9BcV4Nue70oTNGO32MvOSsRLYsiXMPh1PQ5R32BrkMUP+Zf8+HyfjeaPa+YJP2ZDzCVZyc2hPq/7E6ZjwCHs06fr+BtH6fdlcDSDLSqoRj6QguaJRaQOD1QsG8wrGbwJYBARXRpK/47dINn6auO6eBoxjVnJ9KuAvlh8UXGMva5/eSzKGFGIlr67KNvZDzBBOy1wxxtDLpSs4bLlOoE0ZvGJIMF0ammk2LdWNENFcn/7hbZ6TT23casA== 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.71.154) by DM6PR12MB3930.namprd12.prod.outlook.com (10.255.174.19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.2686.29; Tue, 4 Feb 2020 23:02:05 +0000 X-Received: from DM6PR12MB3163.namprd12.prod.outlook.com ([fe80::a0cd:463:f444:c270]) by DM6PR12MB3163.namprd12.prod.outlook.com ([fe80::a0cd:463:f444:c270%7]) with mapi id 15.20.2707.020; Tue, 4 Feb 2020 23:02:05 +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 , Brijesh Singh Subject: [edk2-devel] [PATCH v4 05/40] MdePkg/BaseLib: Add support for the XGETBV instruction Date: Tue, 4 Feb 2020 17:01:09 -0600 Message-Id: <0ca07425fe0a9b73d8394df0b5868d3173dc95aa.1580857303.git.thomas.lendacky@amd.com> In-Reply-To: References: X-ClientProxiedBy: SN6PR16CA0062.namprd16.prod.outlook.com (2603:10b6:805:ca::39) To DM6PR12MB3163.namprd12.prod.outlook.com (2603:10b6:5:15e::26) MIME-Version: 1.0 X-Received: from tlendack-t1.amd.com (165.204.77.1) by SN6PR16CA0062.namprd16.prod.outlook.com (2603:10b6:805:ca::39) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.2686.32 via Frontend Transport; Tue, 4 Feb 2020 23:02:04 +0000 X-Originating-IP: [165.204.77.1] X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-HT: Tenant X-MS-Office365-Filtering-Correlation-Id: 7a13416c-f32b-4dd0-3001-08d7a9c640f7 X-MS-TrafficTypeDiagnostic: DM6PR12MB3930:|DM6PR12MB3930: X-MS-Exchange-Transport-Forked: True X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:7219; Received-SPF: pass (zohomail.com: domain of groups.io designates 66.175.222.12 as permitted sender) client-ip=66.175.222.12; envelope-from=bounce+27952+53752+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: 1BsaGY/GXjvSKmZgyiqqDO7NNFL+qNRCZ1fWavVdQCHVirJlzq6mqioKG2GC4dZoeWouXxOJuIMAIu5ZXXDhHJ8rkl7wByYDwu2MVAXRnxMIYAYbqCcA4jLjlIGenVntaoVeva6aAccBWDQKVPep+H1j55Zr1l6aIyFK/GKmxFIlSSgvvMKiRNBakjpiQaxuM32ho+vaMwJdWqN/3Dsu+30wd9yUZdffEsGkZShN40QNej6hxi16hPZn/iwFt0p1grkoTqA5Fd45osfXp9b6S4iwy9vgCVGuzEsP9Pk1s40i5R2do97NxLew0uLuzzKT/kSfz3g868l6/RPp34Ko525JjFk8XaFsD3wqL8O/4LHGGXj2HAYI6AA2PjwHIsFSR+yn5qq16IIeqrGptaAUFujauWBmaY2SVgiv7wX/PMOIcELpDKP1l3CPetFOKuyygAHOxCk0WrdsEPsxV3Ml7F65Rd2nZYUlk2xRdSQOtGOjMVQG+xbEeejtwiNOfOtNmW0DvbQzSfhNQtaDXldPpQ== X-MS-Exchange-AntiSpam-MessageData: OQRSfMqW3vHG5KRaT9jbOJTTqrxiKwDzVBeqTJgc6v0oAeNnLbp+jE8FM1kRjH4BDxsAyNN0Ht77FlRByJnn+bmirrdK5aHza1rtpIZ8xkkuRNTxCNO3FtLfo4jpd1hwmtL7XuBG4ys7MzEf4CWI4w== X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-Network-Message-Id: 7a13416c-f32b-4dd0-3001-08d7a9c640f7 X-MS-Exchange-CrossTenant-OriginalArrivalTime: 04 Feb 2020 23:02:05.7383 (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: zLrsSS4bPcdzpb1CPSrUwBuBB0AWPOsHMA6CGLK8VZbxdzyyuP99YJvqWv33AaXKy6eoBdtJJB4wbqJgUWD+GQ== X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM6PR12MB3930 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: 7mWcQVnf615LufOMfxP4j5YQx1787277AA= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=groups.io; q=dns/txt; s=20140610; t=1580857328; bh=J2RUug0q17Xbmf2avzxcpxifWHrvTTC/Zw2CgKqzIVI=; h=Cc:Content-Type:Date:From:Reply-To:Subject:To; b=cFHapHwORfyhjSB+LZ58MRc5S8rjMiTDMW6TIolqc6UVuVyVHB18y+uw6B0nn2lTZJf C6kZ2tGhaqd1ir9iQP+hTt6kpjYRfFvVdz4wnt2iuJxRn8Xnz17xRTdRU0kvOl046Paa6 DcDr8j7hX6GvPQU9O2n1MhY0s6wEqh6ot28= X-ZohoMail-DKIM: pass (identity @groups.io) Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" 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 | 17 +++++++++++++ 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, 142 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 3586beb0ab5c..d7a1dd017e95 100644 --- a/MdePkg/Library/BaseLib/BaseLib.inf +++ b/MdePkg/Library/BaseLib/BaseLib.inf @@ -152,6 +152,7 @@ [Sources.Ia32] Ia32/ARShiftU64.c | MSFT Ia32/EnableCache.c | MSFT Ia32/DisableCache.c | MSFT + Ia32/XGetBv.nasm | MSFT =20 =20 Ia32/GccInline.c | GCC @@ -286,6 +287,7 @@ [Sources.X64] X64/ReadCr2.nasm| MSFT X64/ReadCr0.nasm| MSFT X64/ReadEflags.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 ecadff8b235e..d0cbb52ed8f9 100644 --- a/MdePkg/Include/Library/BaseLib.h +++ b/MdePkg/Include/Library/BaseLib.h @@ -7889,6 +7889,23 @@ AsmLfence ( 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 + ); + + /** Patch the immediate operand of an IA32 or X64 instruction such that the = byte, word, dword or qword operand is encoded at the end of the instruction's diff --git a/MdePkg/Library/BaseLib/Ia32/GccInline.c b/MdePkg/Library/BaseL= ib/Ia32/GccInline.c index 5287200f8754..591f0bb0e097 100644 --- a/MdePkg/Library/BaseLib/Ia32/GccInline.c +++ b/MdePkg/Library/BaseLib/Ia32/GccInline.c @@ -1763,3 +1763,31 @@ AsmFlushCacheLine ( } =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 154ce1f57e92..3eed1205adb2 100644 --- a/MdePkg/Library/BaseLib/X64/GccInline.c +++ b/MdePkg/Library/BaseLib/X64/GccInline.c @@ -1798,3 +1798,33 @@ AsmFlushCacheLine ( } =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..23ad38df0710 --- /dev/null +++ b/MdePkg/Library/BaseLib/Ia32/XGetBv.nasm @@ -0,0 +1,31 @@ +;-------------------------------------------------------------------------= ----- +; +; Copyright (c) 2019, Advanced Micro Devices, 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..cd73e972d31b --- /dev/null +++ b/MdePkg/Library/BaseLib/X64/XGetBv.nasm @@ -0,0 +1,34 @@ +;-------------------------------------------------------------------------= ----- +; +; Copyright (c) 2019, Advanced Micro Devices, 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, 32 + 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 (#53752): https://edk2.groups.io/g/devel/message/53752 Mute This Topic: https://groups.io/mt/70984924/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-