From nobody Sun Feb 8 18:43:50 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+56177+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 158507167756855.52802245501982; Tue, 24 Mar 2020 10:41:17 -0700 (PDT) Return-Path: X-Received: by 127.0.0.2 with SMTP id XXESYY1788612xNOEa9yIQWe; Tue, 24 Mar 2020 10:41:17 -0700 X-Received: from NAM12-DM6-obe.outbound.protection.outlook.com (NAM12-DM6-obe.outbound.protection.outlook.com []) by mx.groups.io with SMTP id smtpd.web11.1213.1585071675772552838 for ; Tue, 24 Mar 2020 10:41:16 -0700 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=HQblrTUvm3PiDFo8ofjYuCK0OZCUJ06mZtt9ExxT7pxEEMC3pmaf80o2mGw/bt+c/uStzIqe144ZLyjb1eQeYue3dzlyb/a3nIOZkOX9QU5u2W7sv4j9+7o8fgxi5kTMvYL1RjCgR9O8NKY++0PiQjx5exkDqIg/Z3xiImPffryjVGF0bjT4d6W9HRck20c0tS3RegFH4SB7uuWYJtzBEjOYqtZJ0vd6s3w7cunNhRiLXDJUqa3JsCuURwjRKzhVHvwamUlBp+FfprmmhQ0m5O4tqOIbCFeDnbezK4Gl9JW4iIbjMvJJshccqT/JM/m42qGtq72ObEaB2zEnWrvbxg== 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=W43wvRt3OirN7tyejLY1pb1Jv94yiIZnmMkphcrqlPI=; b=I01Yu/JFVfFB/Z65cezTNTyWZrm0yoiz2FDsFtBCBX4IbfmDo6LlS/yz1YpV5O/oK2qQRZidHATyKWGZsBJbkte7W0Cg4e8xig2q/cLFfHTlFxtmw4Sdrnvn6pFMxI/hJ9zJO0xEooQXaguEYH8qN177MME0wXGUpoIB6FrIhp6BbmhSdttXqeAAtjbRiJtmctOhWCFVQECG5GhPiglKNPnq8oKbR8INR1OpaGo9nxe2n9eDZZrY+NulOHTYGq6CT5HXFmDBvpfb/P3mRshfGjtxgcTxtwqMteS5Mq6FFIcvHG+WiQ1uq+/UzjvQPNkCOCwDhbq92Fr5HvWh27SH2Q== 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 (2603:10b6:5:15e::26) by DM6PR12MB3915.namprd12.prod.outlook.com (2603:10b6:5:1c4::20) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.2835.22; Tue, 24 Mar 2020 17:41:15 +0000 X-Received: from DM6PR12MB3163.namprd12.prod.outlook.com ([fe80::f0f9:a88f:f840:2733]) by DM6PR12MB3163.namprd12.prod.outlook.com ([fe80::f0f9:a88f:f840:2733%7]) with mapi id 15.20.2835.023; Tue, 24 Mar 2020 17:41:15 +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 v6 05/42] MdePkg/BaseLib: Add support for the XGETBV instruction Date: Tue, 24 Mar 2020 12:40:19 -0500 Message-Id: In-Reply-To: References: X-ClientProxiedBy: DM5PR06CA0025.namprd06.prod.outlook.com (2603:10b6:3:5d::11) To DM6PR12MB3163.namprd12.prod.outlook.com (2603:10b6:5:15e::26) MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 X-Received: from tlendack-t1.amd.com (165.204.77.1) by DM5PR06CA0025.namprd06.prod.outlook.com (2603:10b6:3:5d::11) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.2835.19 via Frontend Transport; Tue, 24 Mar 2020 17:41:14 +0000 X-Originating-IP: [165.204.77.1] X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-HT: Tenant X-MS-Office365-Filtering-Correlation-Id: 8ee1d995-b18e-40a5-9f82-08d7d01a8ce2 X-MS-TrafficTypeDiagnostic: DM6PR12MB3915:|DM6PR12MB3915: 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+56177+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: qYRfAnT3IfJ41qvgehtlIQniWMNIJw3rsJgMzH3n0SxPdE0W8u9fUY4jHi9HbPtbI0phWiHiDfMhi02ab2aQ2829M6nRn5d4Im50/YNTvRGfofuCYj41i/nJE8Uw0k7GrLEafqCV+y0wPyQ1MlTLrsx9rQY9JH2MTpdJO6bw6yXljEkp6HiXCtJEqlMxbA8bZHO9OJtKm377SpoZvxZcuK8e5PhihQctg/ex10KEXSY1drFddLnCABXCKlb8wIlTS49uf/ZfKmiMJaBML8Y/UCmU8iF+2sVOZHYHEtCMrx2FWwXX4NGY6AEDWNrmKu6qAmo6EXgsT+s/HZDu47txlZuXT8ZY+gLOZ0J7L1jdbLZoVy9P006evM7wzuAYpxy58oDYJzGqQF/2j6vcjXUxxEALqElbtd731yyh03CocE/a5/BxaPjxp5omIRlqCboW13kI6UE+ehRqHojxCrsssbjp/YHl9SuKpZf5vocUzmCp9gFOl949zZcNUzyzxk/38QpaZZAoX1RQpJSlKRuwbfQ8u5pYS5C+B/Zj/Te5F07KUtE2V0FRGohjBcTEUUAu39m+t+d7gFGwiUu4U4QL9A== X-MS-Exchange-AntiSpam-MessageData: 13P/2hXR/Q4mtgR6z3Md0vI4vfoTB/ouKW3WGHMbUumfQUyD8MpxVq89hn5AzBoLuDMirMrInYjyHgIOVIrJlYofDlZyeoEKIgttf8RIeolK+lTaHmn7y1Ll6ogBj1QsR/lCbtgxdhlUxsuYHzb5zg== X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-Network-Message-Id: 8ee1d995-b18e-40a5-9f82-08d7d01a8ce2 X-MS-Exchange-CrossTenant-OriginalArrivalTime: 24 Mar 2020 17:41:14.9702 (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: J3cRyCHo1sDcNSp0TldG2d2zXwWWyHvTu2uaub1NsDR2bYukhXXoU2srJuPkF9FM4ECSsPocyYrpIGvYXce1jA== X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM6PR12MB3915 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: g0yXa20eNrrQA0fXW6WieMpPx1787277AA= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=groups.io; q=dns/txt; s=20140610; t=1585071677; bh=PXqcu1G/rK00LygDVdNIWU31/Df3B276aBI6UhnlCCE=; h=Cc:Content-Type:Date:From:Reply-To:Subject:To; b=wq3u7OZJUL3coruXlNltONZ5RQUEUq3dKQmE7M6NVQlS+mZjbpju+/nG3/mNgsOg1Uu DhC32hkSBhA07AgQPHbv6iNJS6W2fyhcrbUl8jMbzJ1z5k+LACoOceMjxQbXh99Ci6Elz pJj4cjDguZSGQ1MDlv9RtOW+4T3h3MlYkWY= 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..b6dee38af029 --- /dev/null +++ b/MdePkg/Library/BaseLib/Ia32/XGetBv.nasm @@ -0,0 +1,31 @@ +;-------------------------------------------------------------------------= ----- +; +; Copyright (c) 2020, 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..f3aec43e6813 --- /dev/null +++ b/MdePkg/Library/BaseLib/X64/XGetBv.nasm @@ -0,0 +1,34 @@ +;-------------------------------------------------------------------------= ----- +; +; Copyright (c) 2020, 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 (#56177): https://edk2.groups.io/g/devel/message/56177 Mute This Topic: https://groups.io/mt/72522844/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-