From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from NAM11-DM6-obe.outbound.protection.outlook.com (NAM11-DM6-obe.outbound.protection.outlook.com []) by mx.groups.io with SMTP id smtpd.web11.953.1589925096378722884 for ; Tue, 19 May 2020 14:51:36 -0700 Authentication-Results: mx.groups.io; dkim=pass header.i=@amdcloud.onmicrosoft.com header.s=selector2-amdcloud-onmicrosoft-com header.b=hVE9jI4A; spf=none, err=SPF record not found (domain: amd.com, ip: , mailfrom: thomas.lendacky@amd.com) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=d3vkOh8cedW/w6K6vT+A/8KBteoYx8YESFCaKfPx6lKpdeYtCYOrQtw9F1juyyDm0nS41iERstW8jKbFOVWyNEwCCzDilZ72eVf58TbEUPGCOLRSGB4Rcicayd42P4HakoVbQ/fjG45kQj9B4S205yQf1UeYOskhQ7jp9tFoLl5LDTlzFSkopnZqOWTSPq9ksJDozME/l4Vwj4mNs4ZYae3rOtYhAj4MVwhIAEEbcIvvQodtO0ujaPw+jVA/IorRoMwMeIaYgQ+VET16r4KUQqH46WysAYOhMk3c/AxHR7KwcfHJD7aT0fl52jjnovpOdAtFG1TZJh0lzkMpl/GrGg== 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=FQXvgh6i13tXJ8dQf2PIsIdb1vVr+hWSJKwgj4cvK+0=; b=gbvVshBp0BRHF64/V6ofQ6eSf4FskM8BOAQk/GY1Uowg1+4P5XeoccFVCmRoVgHn0VBuhNmsFuUgmXs4AUf2x0IK2DTKFB7gFrVTjJz18OhKz6agUOXU+PB8bcwIUiCYHMEa7wob3I+1v50DFepxW9f5BzlMUIQKacZA5CTczalvv3FqlfaUVvAkYjmtwAt+RlYVGnOxEABm3rxVR9CC8tnfbHpvCrifb2DLPOkuKXUq12niGyCV1On4IBel4AkowixhtC9zRwsqC6IzjduBRGUI8w3gEO4WdcHQJ2GsLNDyX1xw8BOsN6w5u8C5RLi0dHXyjs7WU4ZXfKxzdJPGzQ== 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=FQXvgh6i13tXJ8dQf2PIsIdb1vVr+hWSJKwgj4cvK+0=; b=hVE9jI4AujYol1PYCDqPwXeFeXEn8FuMbc/dNpRR4hfbtnAipgk7KJNLve1yjt9/l7O38xCmpvRrlTx8ExzXjz+88BqCliv2tvxtAKrUXcBfQdyPf5zjwhhm9fAr2Kv0b7wzDgBtpFtZ4VYf5rR9XluSguBWDgxTc1GHhiK3UXY= Authentication-Results: edk2.groups.io; dkim=none (message not signed) header.d=none;edk2.groups.io; dmarc=none action=none header.from=amd.com; Received: from DM5PR12MB1355.namprd12.prod.outlook.com (2603:10b6:3:6e::7) by DM5PR12MB2504.namprd12.prod.outlook.com (2603:10b6:4:b5::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3000.20; Tue, 19 May 2020 21:51:35 +0000 Received: from DM5PR12MB1355.namprd12.prod.outlook.com ([fe80::4ce1:9947:9681:c8b1]) by DM5PR12MB1355.namprd12.prod.outlook.com ([fe80::4ce1:9947:9681:c8b1%10]) with mapi id 15.20.3000.034; Tue, 19 May 2020 21:51:35 +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: [PATCH v8 06/46] MdePkg/BaseLib: Add support for the XGETBV instruction Date: Tue, 19 May 2020 16:50:34 -0500 Message-Id: <2e5aa32c158ac550e0b4b6481e4945258292f01a.1589925074.git.thomas.lendacky@amd.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: References: X-ClientProxiedBy: DM6PR11CA0048.namprd11.prod.outlook.com (2603:10b6:5:14c::25) 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 tlendack-t1.amd.com (165.204.77.1) by DM6PR11CA0048.namprd11.prod.outlook.com (2603:10b6:5:14c::25) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3000.25 via Frontend Transport; Tue, 19 May 2020 21:51:34 +0000 X-Mailer: git-send-email 2.17.1 X-Originating-IP: [165.204.77.1] X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-HT: Tenant X-MS-Office365-Filtering-Correlation-Id: 2c5c511a-d321-4b69-8d0c-08d7fc3eccc0 X-MS-TrafficTypeDiagnostic: DM5PR12MB2504: X-MS-Exchange-Transport-Forked: True X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:7219; X-Forefront-PRVS: 040866B734 X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: chzFxCNSjBjpxlANyrMasyVcWptFzl/l4O2dpWVSQZbVNLXTTNoUlV0zX76pj8v4I5enJyaqoaRvXvSt+7YrccQsdL02hYA5ZyoPjBdFtD65J2tq8fF2bX96PGwqRrU6Ak2n6KsfHAKQf9aHWyt0NZTXIfOKFWE8IYHKhUyk+ChyD/QiqTktW+S8+cUDk2eEoTPw0J68Dz4xQyVKUfXMofGpokYxZQVcjkuKGWuLSomnPGF16/rL2cbmBSL5Zb4HUGtV9pCgv8yIwG36PC3sQex4+zxpoiSW3QmxNPeW+fo/w4kQRuG4QP+3jmL8PAi7FpJwK15QZ49bwYwKgrgoYNGLpNSQanbDiWFZzkoL1xypmMBiA4gkTFkLH1Jqo1wXpU+8iuwzh6zdtPKjlQdT84fEJPY051MktYVKGklGMx+iZ6CuW2pSids2vi3LP3wm6/EFoR7GNjaQI9MJR9xddbbfd+dEJP52P3das00Py/z63zC/Y631mBGOReAqKZRdFvzio0Wejv2uUhpXKY87ONfV9BObtWulj9oatmTw65lXvuqziJ/zYW1VTdypuinU8TMMqfvfpAUoDOvxgVB4cA== 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)(376002)(39860400002)(136003)(346002)(396003)(366004)(316002)(86362001)(2906002)(54906003)(7696005)(52116002)(2616005)(956004)(66946007)(66476007)(19627235002)(66556008)(36756003)(26005)(16526019)(186003)(4326008)(8676002)(5660300002)(966005)(6486002)(8936002)(6916009)(478600001)(136400200001);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData: EnStyN9cyWk5tqVBHLO0jnKY6veKO2ELv+tFAcDkZ0IMuy1aeYovM2J00mMC/Za16OJK1XgslgMsNFbPKprb8N7g/QWx0wuoAH5h2iHf0x7lFCFGifNATGOKrDqxjyHC2WPq20w6M4a+GlwFx75I7KarwbyLAaApdnG/dcZ5en25ASvZkDt7L5SNqRvZk/WI17P/AiYwmJCwLkHUXfFhNLEh0dhsWcqMdi+iP+kiDgnCwnsHJN7tyigEoBgiaotGeTlJlpdy/jF4aw0a2oYl/bj0N1AV4Zcd8SSGLbpiHI4/nWcwv7MyvrCxvtKa1EUKvUPA/VdioZj09J61kjTmiWnvsBk1lAOvkoGXa9gMTy5EeBRPBc4uPRRUG6UUqCq6iXgpQvgGhNbvBFmUUMPVjjpD5g32HJtCefkFIMLfVDjjE8g8OBOk1tymC7FNrCkm/mxdG+I/eQn9QPjtkk6blwMXMJvNFlwlVp2+P63sLI8= X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-Network-Message-Id: 2c5c511a-d321-4b69-8d0c-08d7fc3eccc0 X-MS-Exchange-CrossTenant-OriginalArrivalTime: 19 May 2020 21:51:35.1116 (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: xvR2fhWAHQ6V8yWjZsLUvqBarerDZ0HWArlCHTrOwTWR9WJYTc7J9FNarat02b8VKW9AGQ5I8n57n0frVJfVuA== X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM5PR12MB2504 Content-Type: text/plain BZ: https://bugzilla.tianocore.org/show_bug.cgi?id=2198 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 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 b0bbe8cef834..049fd1b872b8 100644 --- a/MdePkg/Include/Library/BaseLib.h +++ b/MdePkg/Include/Library/BaseLib.h @@ -7942,6 +7942,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 + + @retval 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..591f0bb0e097 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 + + @retval 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..3eed1205adb2 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 + + @retval 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 + -- 2.17.1