From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from NAM10-DM6-obe.outbound.protection.outlook.com (NAM10-DM6-obe.outbound.protection.outlook.com [40.107.93.78]) by mx.groups.io with SMTP id smtpd.web10.13291.1595513934620926210 for ; Thu, 23 Jul 2020 07:18:54 -0700 Authentication-Results: mx.groups.io; dkim=fail reason="body hash did not verify" header.i=@amdcloud.onmicrosoft.com header.s=selector2-amdcloud-onmicrosoft-com header.b=xZJdZIiT; spf=none, err=SPF record not found (domain: amd.com, ip: 40.107.93.78, mailfrom: thomas.lendacky@amd.com) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=FzrF1B4mVknv9xRBAB8v+DiXRxHJN/KE1Z2OHViexFB1HdXPzV2yd4n3Yo4TSSiVth52NdL2cbT/99G70HLSiNRNUa68oZEthyoZnJ72p6Uh0Iin0cC4iKxbxtniAa0lPCgwBxJIY1lp8ffHduX3WPvkVtoab+Vb2uc/DzMZoh1uoCbqnqd7lN8JD11NllxrTl90REXl1vAHm7aPDBrBSCwzf+nTCSBBPHT73zKtXRl9Fd/AUzLAh5I1ajanXsarXsQQ+c/O6Pajn73DSt/xLIF3dnxGMKTvb5LG2S7FybltUMob7Sqc4dDw/FRJmhgC4rLZcHd9O9A3crEdOFI1dg== 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=74QF8wByw1MufARqHh4Ue4uZqUQWwxGvdslY3zF6Q3M=; b=IawMIxsQoPxXfvzQAkYUH9IA1HlzB1dX85gyHaW1R847n5kJJnRxzH+/tWkDhnp5E/VwOHsbk6SV/q0Cq3kU2pOW1MhWXOEdN7WX69soxudAeTytFlblPtvvFKmweLVJwLvzAbBM91skqADipYLufeId+4CQUek+5ZkRGZBHkna39OrIxbBEP+izSEPnQMsm+I6/XV/rbjtJWoB2BVtJZy7gEMTn+gsSFr4zo85kRPlFtOTKRffLZf5pKU+W7JySAWVfb++DzwrcVFbzwEKT78qCPn1xeDLYVn1v0YJCjI+EmWdQ+1WWHgbeztiiO3CLSuG7djFdKykxukR4yCGyVQ== 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=74QF8wByw1MufARqHh4Ue4uZqUQWwxGvdslY3zF6Q3M=; b=xZJdZIiTKl0bMmxLXbr+gSG5iiVj8GIi7pdNdkfq+nwwVfkxh2jnjIPM7e5fD48XdM/qjzS4WYRNe45a9TDCOO1dzKpRbHChzWK+FyAaqIVfbqUT3EyR6MXKB2+HiNPOEE8cIi+5lGplAhVBV/hAUkdkxbSIxs1yEyQy1+0skws= Authentication-Results: intel.com; dkim=none (message not signed) header.d=none;intel.com; dmarc=none action=none header.from=amd.com; Received: from DM5PR12MB1355.namprd12.prod.outlook.com (2603:10b6:3:6e::7) by DM5PR12MB2501.namprd12.prod.outlook.com (2603:10b6:4:b4::34) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3216.23; Thu, 23 Jul 2020 14:18:52 +0000 Received: from DM5PR12MB1355.namprd12.prod.outlook.com ([fe80::25ec:e6ba:197c:4eb0]) by DM5PR12MB1355.namprd12.prod.outlook.com ([fe80::25ec:e6ba:197c:4eb0%8]) with mapi id 15.20.3216.021; Thu, 23 Jul 2020 14:18:52 +0000 Subject: Re: [edk2-devel] [PATCH v11 06/46] MdePkg/BaseLib: Add support for the XGETBV instruction To: "Gao, Liming" , Laszlo Ersek , "Liu, Zhiguang" , "devel@edk2.groups.io" CC: Brijesh Singh , Ard Biesheuvel , "Dong, Eric" , "Justen, Jordan L" , "Kinney, Michael D" , "Ni, Ray" References: <0d091f43ae96ca4617f6a95a018bfaed6280eec2.1595366363.git.thomas.lendacky@amd.com> <59baa2d3-69e0-2f98-27b8-92010f393109@redhat.com> From: "Lendacky, Thomas" Message-ID: Date: Thu, 23 Jul 2020 09:18:50 -0500 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.10.0 In-Reply-To: X-ClientProxiedBy: SN1PR12CA0061.namprd12.prod.outlook.com (2603:10b6:802:20::32) To DM5PR12MB1355.namprd12.prod.outlook.com (2603:10b6:3:6e::7) Return-Path: thomas.lendacky@amd.com MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from office-linux.texastahm.com (67.79.209.213) by SN1PR12CA0061.namprd12.prod.outlook.com (2603:10b6:802:20::32) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3216.20 via Frontend Transport; Thu, 23 Jul 2020 14:18:51 +0000 X-Originating-IP: [67.79.209.213] X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-HT: Tenant X-MS-Office365-Filtering-Correlation-Id: 15eff924-ed06-49c9-7fdb-08d82f13537d X-MS-TrafficTypeDiagnostic: DM5PR12MB2501: X-MS-Exchange-Transport-Forked: True X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:9508; X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: GeaLhFd9/tjFw2aCOl2XYOKmbW25dOsGiOcQP4YmN7L+rW0NJ2fBgIaZ7Ph0zw/UB0PevLc+RNKSBGukONYWd/d/WI187T/JklRM3JDxkawILE0llUufg7fJCDZ9OGVemXp+a7wRMTJhQGgPPm6QeW6enEGrktQ3XkR5rksWwwAO/YLXmlGWIEso4C+ttuesWjawgqirfBoMVRrTMgHM02RdBo43hCIa0bRWBznDUH4bE5yx7zplXZ9zy79qm5nuxQEnA76gfguQ07rrcdLyiQNrfVJuUASkiz7vF/QbdPdlzLCBwWwnLCs8lP8kQpukJtUk7UEHDMeC0s/mwDi57v7O/k8xkUPQOhVSBLngSzxKlK88n/WVNzDNwGlJqyrrFL37MRZsTryCsAkfNCvgZxIYPUmDNiAoyifR+e4YstPXqy7CKpVfZet/i1052mBU+MwhjEPfD50aohhHcP8kDw== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:DM5PR12MB1355.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFTY:;SFS:(4636009)(396003)(366004)(39860400002)(346002)(376002)(136003)(8936002)(186003)(66946007)(86362001)(66476007)(66556008)(26005)(36756003)(45080400002)(16526019)(478600001)(19627235002)(966005)(31696002)(2906002)(54906003)(83380400001)(2616005)(110136005)(4326008)(956004)(52116002)(53546011)(6486002)(6506007)(8676002)(30864003)(31686004)(6512007)(316002)(5660300002)(43740500002);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData: FANewM2r5mk7yfAxbLuEBNMfe+Nb3A1DZLss/njJf5GkavB9ROXeD4O+nNeScKd3VoDo6ctm+LmabQAMM5IzpzPXrVY/fAWTrgxZXYLRo9bFjolvimINu6JzMIqtaBhJC3fUzPDGxzwBg3zJIoWM+x8psB7qGfzTeq4MqHHtf4ADfEUWFdXqdm1nbimj2+zo2Uxxye8AueUmiY9CvJKn7CCA6T3y2805TWqO417HKagEjbjC7CD0r0TFwlrYQY/x87jx7rhYEurkOd1E8KLC2hX7sQfkcdv4jQ6EHnyAMUFdnsaf9uekGJCX3Tw/xvlHcGZB184/9TCkULGhTZHZi3OfzUhmYsGjtGl66iKYTL06MO9xgT4LNA24otM19Bterhnf5tR6vvHKXjaUEgPfNvoNCI9o1Ft+izoDPn4DnxTcvuywh88MeO9Nr6P6Kv+cLhyvDgV1po0fyWaxuyTB9lNJxIwdGzFeDBiAFKpsqVA= X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-Network-Message-Id: 15eff924-ed06-49c9-7fdb-08d82f13537d X-MS-Exchange-CrossTenant-AuthSource: DM5PR12MB1355.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 23 Jul 2020 14:18:52.7231 (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: 6kA8xet6Wi5JEliLapD8y6ggUeAcXvKjK+GFjc63fKMyDUdnW2XigJnlMIfxcqy79CHIGuAGH0Orwm/+CYRXsQ== X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM5PR12MB2501 Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: quoted-printable On 7/22/20 8:16 PM, Gao, Liming wrote: > Laszlo: >=20 > -----Original Message----- > From: Laszlo Ersek > Sent: 2020=E5=B9=B47=E6=9C=8823=E6=97=A5 4:28 > To: Liu, Zhiguang ; devel@edk2.groups.io; thomas= .lendacky@amd.com > Cc: Brijesh Singh ; Ard Biesheuvel ; Dong, Eric ; Justen, Jordan L ; Gao, Liming ; Kinney, Michael D ; Ni, Ray > Subject: Re: [edk2-devel] [PATCH v11 06/46] MdePkg/BaseLib: Add support = for the XGETBV instruction >=20 > On 07/22/20 02:55, Liu, Zhiguang wrote: >> Hi Tom, >> Nasm is a cross-OS assembly code and can be used in Linux. >> So I think we don't need implement the same function in GccInline.c, we= can just use the nasm file in Linux. >=20 > I could agree, but this would create an inconsistency with the existent = functions (where both gcc inline assembly and NASM exists). > [Liming] Yes. This is clean up task to make the existing ones be consist= ent. The new one X86 assembly function (IA32 and X64) should follow nasm st= yle. I was following what I thought was convention. If you'd like, I can remove= = =20 the "| MSFT" from the .nasm entries in the .inf file and delete (not add)= =20 the functions in the GccInline.c file. Let me know what you would like done. Thanks, Tom >=20 > For example, consider AsmReadEflags(): > - inline assembly for MSFT IA32 ("Ia32/ReadEflags.c") > - NASM for MSFT X64 ("X64/ReadEflags.nasm") > - inline assembly for GCC IA32 ("Ia32/GccInline.c") > - inline assembly for GCC X64 ("X64/GccInline.c") >=20 > The source file "X64/ReadEflags.nasm" could be used with GCC X64 too, no= t just with MSFT X64. >=20 > So why do we have the gcc inline implementation for AsmReadEflags() in "= X64/GccInline.c", in the first place? > [Liming] This is the history. Nasm migration replaces .S and .asm. But, = the remaining one in C source is not replaced. >=20 > Thanks > Liming > The pattern that a contributor is supposed to follow is not clear to me. >=20 > Thanks, > Laszlo >=20 >>> -----Original Message----- >>> From: devel@edk2.groups.io On Behalf Of >>> Lendacky, Thomas >>> Sent: Wednesday, July 22, 2020 5:19 AM >>> To: devel@edk2.groups.io >>> Cc: Brijesh Singh ; Ard Biesheuvel >>> ; Dong, Eric ; Justen, >>> Jordan L ; Laszlo Ersek >>> ; Gao, Liming ; Kinney, >>> Michael D ; Ni, Ray >>> Subject: [edk2-devel] [PATCH v11 06/46] MdePkg/BaseLib: Add support >>> for the XGETBV instruction >>> >>> From: Tom Lendacky >>> >>> BZ: https://nam11.safelinks.protection.outlook.com/?url=3Dhttps%3A%2F%= 2Fbugzilla.tianocore.org%2Fshow_bug.cgi%3Fid%3D2198&data=3D02%7C01%7Cth= omas.lendacky%40amd.com%7C7cbbd5ae7ded4963ad1e08d82ea5ff27%7C3dd8961fe4884e= 608e11a82d994e183d%7C0%7C0%7C637310637778599307&sdata=3DGPutAlSGucRGFnj= U4rxWXfeiy4fJhZHHZe5YJ8hhPSQ%3D&reserved=3D0 >>> >>> 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/BaseLib.inf >>> index c740a819cacf..e26c0d8cb0ac 100644 >>> --- a/MdePkg/Library/BaseLib/BaseLib.inf >>> +++ b/MdePkg/Library/BaseLib/BaseLib.inf >>> @@ -153,6 +153,7 @@ [Sources.Ia32] >>> Ia32/ARShiftU64.c | MSFT >>> Ia32/EnableCache.c | MSFT >>> Ia32/DisableCache.c | MSFT >>> + Ia32/XGetBv.nasm | MSFT >>> >>> >>> Ia32/GccInline.c | GCC >>> @@ -288,6 +289,7 @@ [Sources.X64] >>> X64/ReadCr2.nasm| MSFT >>> X64/ReadCr0.nasm| MSFT >>> X64/ReadEflags.nasm| MSFT >>> + X64/XGetBv.nasm | MSFT >>> >>> >>> X64/Non-existing.c >>> diff --git a/MdePkg/Include/Library/BaseLib.h >>> b/MdePkg/Include/Library/BaseLib.h >>> index 8e7b87cbda4e..7edf0051a0a0 100644 >>> --- a/MdePkg/Include/Library/BaseLib.h >>> +++ b/MdePkg/Include/Library/BaseLib.h >>> @@ -7831,6 +7831,23 @@ AsmLfence ( >>> VOID >>> ); >>> >>> +/** >>> + 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 >>> + >>> + @return The current value of the extended control r= egister >>> +**/ >>> +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/BaseLib/Ia32/GccInline.c >>> index 6ed938187a08..c2565ab9a183 100644 >>> --- a/MdePkg/Library/BaseLib/Ia32/GccInline.c >>> +++ b/MdePkg/Library/BaseLib/Ia32/GccInline.c >>> @@ -584,3 +584,31 @@ AsmReadTsc ( >>> >>> return Data; >>> } >>> + >>> + >>> +/** >>> + 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 >>> + >>> + @return The current value of the extended control r= egister >>> +**/ >>> +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/BaseLib/X64/GccInline.c >>> index 40a208f1985f..65f864e35922 100644 >>> --- a/MdePkg/Library/BaseLib/X64/GccInline.c >>> +++ b/MdePkg/Library/BaseLib/X64/GccInline.c >>> @@ -560,3 +560,33 @@ AsmReadTsc ( >>> >>> return (((UINT64)HiData) << 32) | LowData; } >>> + >>> + >>> +/** >>> + 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 >>> + >>> + @return The current value of the extended control r= egister >>> +**/ >>> +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/BaseLib/Ia32/XGetBv.nasm >>> new file mode 100644 >>> index 000000000000..9f7b03bbff35 >>> --- /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/BaseLib/X64/XGetBv.nasm >>> new file mode 100644 >>> index 000000000000..09f3be8ae0a8 >>> --- /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 >>> + >>> -- >>> 2.27.0 >>> >>> >>>=20 >> >=20