From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from NAM12-DM6-obe.outbound.protection.outlook.com (NAM12-DM6-obe.outbound.protection.outlook.com [40.107.243.81]) by mx.groups.io with SMTP id smtpd.web12.225.1594829831888049247 for ; Wed, 15 Jul 2020 09:17:12 -0700 Authentication-Results: mx.groups.io; dkim=pass header.i=@amdcloud.onmicrosoft.com header.s=selector2-amdcloud-onmicrosoft-com header.b=gpWoyoaI; spf=none, err=SPF record not found (domain: amd.com, ip: 40.107.243.81, mailfrom: thomas.lendacky@amd.com) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=XSfOlJ+NRO85Cl67oR9sZIqN7yiMJ2o6t9J21SGI01K15Ak8vOj7vVVeOl9raFeCnjJKC2nKTCeuZJVHOO52vSuR+XxtIoxSMhSR6pUwP5ncnurP9nKDH+B852C7jfU1GntW8HQD0iVI7SYC7NWVSHf4QizhaSvMlzMS8MTsHsNtAf8TCff79SduT5Z1uSkOCSt5hyYpVjjzQFx4i6xIaluZJw6ap4SzofbrN50sWHO6PXu6OPJx3emIQWYFwDhiyRBb0p2vnqivChZbpMfNXhydN8/5aEX+YgvWy/OS1CSnbO3U8tgJx1Aw9FYeNgX6ESAt4l+EhtLg8YLYQm2vNA== 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=/Xqi54K9F61a6gr2P+UozeiKldbVjmD26xUtOfKVNZo=; b=BeJtyKO+6LzSROamhDQM+1p3bSFVj3g1xr1wzgJ/fs1yJshMrJxchHM1ZGc9E/Qk7BD6tIs8kxvZLUh6ApdE46fHCKsDmM26H4nWX0+xJZYpxwP0u19oCRKzTktFTmsHJ8H4b8mPrChMA47S1N1+ICL1KG5r0FDOEJw5VUCIzJ+iKTl/zSVOIR9cih4qDhtWrCTaFwnptltTi7+ifKD82obI4QVb49RuA6A84wATYlUtWa76LsKYBQ+Oav0zrDmmQ9HsPNNab+0cVpZ7s7kH5QbvTjp151DinxM9Xf6VSMhmi9ifPzULDFXC1N/p7a+rEuzJprrmZN84IH8zkm7s1A== 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=/Xqi54K9F61a6gr2P+UozeiKldbVjmD26xUtOfKVNZo=; b=gpWoyoaIWDYrMmzU4Axjs71kUuA8iOhZVjeRZiXg7w5oZFsIOmqwnQTrTKgiJVhp7cHNfFzgDn7iyxtbCADlBnDCzTjoXOylrR78vXoaOZrlRqs1ma5uoflP3HiMXyrpC0gKK5TsQToLsfwwMgCarUMsReYnLcVhH62F9JGZkY8= 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 DM6PR12MB3179.namprd12.prod.outlook.com (2603:10b6:5:183::18) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3174.22; Wed, 15 Jul 2020 16:17:10 +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.3174.026; Wed, 15 Jul 2020 16:17:10 +0000 Subject: Re: [edk2-devel] [PATCH v10 06/46] MdePkg/BaseLib: Add support for the XGETBV instruction To: Laszlo Ersek , devel@edk2.groups.io Cc: Brijesh Singh , Ard Biesheuvel , Eric Dong , Jordan Justen , Liming Gao , Michael D Kinney , Ray Ni References: <20ddae98568fa4379cf10a73baef1418f5de307f.1594736896.git.thomas.lendacky@amd.com> <15224b54-1914-2c88-b9a5-ce56ad3aad94@redhat.com> From: "Lendacky, Thomas" Message-ID: <9a5b6c82-c752-7676-0efd-9693d10e5a78@amd.com> Date: Wed, 15 Jul 2020 11:17:08 -0500 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.10.0 In-Reply-To: <15224b54-1914-2c88-b9a5-ce56ad3aad94@redhat.com> X-ClientProxiedBy: DM5PR15CA0028.namprd15.prod.outlook.com (2603:10b6:4:4b::14) 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 [10.236.30.118] (165.204.77.1) by DM5PR15CA0028.namprd15.prod.outlook.com (2603:10b6:4:4b::14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3195.17 via Frontend Transport; Wed, 15 Jul 2020 16:17:09 +0000 X-Originating-IP: [165.204.77.1] X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-HT: Tenant X-MS-Office365-Filtering-Correlation-Id: e39e827a-31d1-433d-cc6f-08d828da86b3 X-MS-TrafficTypeDiagnostic: DM6PR12MB3179: X-MS-Exchange-Transport-Forked: True X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:7691; X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 1y+qy0Mg9kL0RJW87ZUvfNJyuqIq7SwGHu78/mtzE4VC3hEGESCuPp+VTAYlmAjTt7FDIbHqniF84AoT5IwqhuPs95sdGR56e6TZwR0m3UmcAD9mmZlqQ+8HaWtH46Nv3D5FgJ7VV8fEl2Dy9+zfC/6H+w9xjOcj953CHXbZiTTgCaz9SoaMv3vJwueaQF65E6xElKVIC4IQoZ6uCjQg1QvzhuzvCMKQzsrUVR4pyLNMWJlO4CZR3paGxrtMjfWLydSIZwfITyJLqv0qYymCnN47YoZoe0WqD/hCWvYDzrEuuUC/DM7ryv+a6BriD30lrhKJjmQLTm8mGeZUtGCezm0BAgUNtlk4psgq1gMyKwN6HuERAMCbudGOcIpyTh8AFkLQ3hpEkXzkdGGG2xmh7Oo+0LdCdShTKgbqWxBliq5I86PBOhf4YEJXzm7ysLMisgl56FopdC9+U2YuynWooQ== 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)(346002)(396003)(376002)(366004)(39860400002)(136003)(66946007)(8676002)(8936002)(19627235002)(31686004)(54906003)(31696002)(316002)(956004)(2616005)(16576012)(478600001)(45080400002)(5660300002)(4326008)(966005)(86362001)(2906002)(66476007)(186003)(66556008)(36756003)(52116002)(26005)(83380400001)(16526019)(6486002)(53546011)(43740500002);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData: SqD0HQvvAMOCWBQXAoplaSRJwGCaievQQitPGluT9p8rJ6ns5ULRxQXpFm1kv2y53GDg8AQaDRY5HQBbt3WC5SPrkoemTn0kR4+lsFCAgkks5RCVguh07EvLERPQBhi8sZIfltxz1plc3teO32ChjQ7muuPGqI7RYQESQ62i4yvlsrjXL7DkKCokzZr3i1UVrDYC7VyWs2BwFgIh5LffO4Ogrpg45dugINfve9egsv7neFoFozU5TSOA6AwS7frVIT3kWv1la6bodcOAy2jqgxMWQ+kW02M0j+aXgdnz5O3Kp4yj3VmlqGCO5aES9hlmdCxTZ/pBqDrIsnjHAe5nZnZftg9sBYoETV9LaKq3/bY7FPCK6A1isqqDsTa4wQ6utBUIngZ70EDxv7NvEBOxpP3dlMl8hWg+h+sEeatuIDqmFHLOzw3qBY8CIUVOj9SJ7I+Gj0DaXtObOh93abRO7lD9/WOGhHS1z2fsOmWZ3cQ= X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-Network-Message-Id: e39e827a-31d1-433d-cc6f-08d828da86b3 X-MS-Exchange-CrossTenant-AuthSource: DM5PR12MB1355.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 15 Jul 2020 16:17:10.2946 (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: H5Hw//Zj5c15X+j5VpEDxwd2DusEQ0qNYJI2IRTTSCr5dp5UDkseTmll8+Tl3vkT/PBQ7T9nVw49OJgVuYzgOQ== X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM6PR12MB3179 Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 7bit On 7/15/20 10:55 AM, Laszlo Ersek wrote: > Hi Tom, > > On 07/14/20 16:27, Lendacky, Thomas wrote: >> From: Tom Lendacky >> >> BZ: https://nam11.safelinks.protection.outlook.com/?url=https%3A%2F%2Fbugzilla.tianocore.org%2Fshow_bug.cgi%3Fid%3D2198&data=02%7C01%7Cthomas.lendacky%40amd.com%7C00c7f97271bf4efd050608d828d79a13%7C3dd8961fe4884e608e11a82d994e183d%7C0%7C0%7C637304253757199823&sdata=hQ%2BP%2F5DintrtGi66sfqRI%2B9h4V1rTB1YlYdY1VX6sLM%3D&reserved=0 >> >> 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 > > This patch no longer applies, it conflicts with commit d3c9e40abcc8 > ("MdePkg/BaseLib: Break out IA32/X64 GCC inline privileged functions", > 2020-07-15). Ok, that looks like it landed just after I sent my patches. > > The conflict persists if I successfully apply the series on c7195b9ec3c5 > -- just preceding Mike's series "[Patch v3 00/16] UnitTestFrameworkPkg: > Enhancements and bug fixes" --, and then try to rebase to current master > (e77966b341b9) from there. > > Can you please investigate? Yup, I'll take a look. Thanks, Tom > > Thanks, > Laszlo > >> >> diff --git a/MdePkg/Library/BaseLib/BaseLib.inf b/MdePkg/Library/BaseLib/BaseLib.inf >> index a57ae2da31f3..da6bc22a3e2b 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 >> @@ -287,6 +288,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 register >> +**/ >> +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 5287200f8754..c962bcfa4617 100644 >> --- a/MdePkg/Library/BaseLib/Ia32/GccInline.c >> +++ b/MdePkg/Library/BaseLib/Ia32/GccInline.c >> @@ -1763,3 +1763,31 @@ AsmFlushCacheLine ( >> } >> >> >> +/** >> + 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 register >> +**/ >> +UINT64 >> +EFIAPI >> +AsmXGetBv ( >> + IN UINT32 Index >> + ) >> +{ >> + UINT64 Data; >> + >> + __asm__ __volatile__ ( >> + "xgetbv" >> + : "=A" (Data) >> + : "c" (Index) >> + ); >> + >> + return Data; >> +} >> + >> + >> diff --git a/MdePkg/Library/BaseLib/X64/GccInline.c b/MdePkg/Library/BaseLib/X64/GccInline.c >> index 154ce1f57e92..915555198f9f 100644 >> --- a/MdePkg/Library/BaseLib/X64/GccInline.c >> +++ b/MdePkg/Library/BaseLib/X64/GccInline.c >> @@ -1798,3 +1798,33 @@ AsmFlushCacheLine ( >> } >> >> >> +/** >> + 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 register >> +**/ >> +UINT64 >> +EFIAPI >> +AsmXGetBv ( >> + IN UINT32 Index >> + ) >> +{ >> + UINT32 LowData; >> + UINT32 HighData; >> + >> + __asm__ __volatile__ ( >> + "xgetbv" >> + : "=a" (LowData), >> + "=d" (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 >> + >> >