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 [40.107.223.88]) by mx.groups.io with SMTP id smtpd.web10.5611.1595366431016897619 for ; Tue, 21 Jul 2020 14:20:31 -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=srkxockd; spf=none, err=SPF record not found (domain: amd.com, ip: 40.107.223.88, mailfrom: thomas.lendacky@amd.com) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=DHUxQ6MBxCgQmPfDcFLz25KwOCWyihJdWywSE5mgBnGqaj+/OlZ1UfcVKjPh5hWkRdFZbhIr04VZnGd6qx2YyrPmR+MS2TYlf/nxjB7K1GmQlTmqjX/RKaF1u4CsZWoznrJ1/Ep1GeXnRfJvYSmqc8QAIDm+8JrVpFg3xme8Fn7huhEZtxq8Tm1dOGJYnlHj/lZ3BJV8Tz1z6c9EKwla3jjWUCMs5M2YZWlpxpyvhJ/zZsbN1gHXXAmtdCpALCCH2FQsxheKrfxiKKPHlcgChA5hi0U9t10nOknbPWbxsDzae+QpnR9Iractf+aKZRZwhU1Y5Mj+w/q5l2M491Uq1g== 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=2ayQejRVDoGBP8sd1K4oywwjRrcrbY8KDkVKs4a/sBo=; b=Xsgc2R6/BuCluBpUD9I4z1nrjjYCEuvh4ezUa6FctmuTe2MvsQFlHkTN3uOCfeuTjPyiJK0YRyOa3N3r3o7KJgoFqZHhALVMJLu4g1DMwqJGH0qJqe2SyHmF7nOST6mtO++A53jHticrRCk/QlsneTfuzsmFvkNRr1fjYdb0wbSU1T+JrUTJpCkDm9Itjj/abmTer83NxtR+5/l1Gq8kMR2FQh06iJJ+WuwtEy47tEJdRQWNScnaBaQPEjodlT7rBH2EPlXoIELVS2OYoKQLF/CSpOapyFOAsa26SOg2wl9f8Ie1szo3xlGlczpegwICCstQ1nvPTERQtDbXzc7ZLw== 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=2ayQejRVDoGBP8sd1K4oywwjRrcrbY8KDkVKs4a/sBo=; b=srkxockdbE2tX3C97X7J09IFP9DgPTLgOEzWuLP3oDFNv1zkyGgDwhVbQlpTQk3LJq9Z1bfDqI61VI1zgubRmK2Bp8okFqDamjKDJ+I9thj3z22nhM5OB5PnjoFDTXsHCASGmiy80E55Z49NTdgz3HLfw1kW1XUrM/jLZqVgPEk= 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 CY4PR12MB1352.namprd12.prod.outlook.com (2603:10b6:903:3a::13) by CY4PR12MB1606.namprd12.prod.outlook.com (2603:10b6:910:10::15) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3195.23; Tue, 21 Jul 2020 21:20:29 +0000 Received: from CY4PR12MB1352.namprd12.prod.outlook.com ([fe80::9181:78bf:bf0:702b]) by CY4PR12MB1352.namprd12.prod.outlook.com ([fe80::9181:78bf:bf0:702b%5]) with mapi id 15.20.3216.020; Tue, 21 Jul 2020 21:20:28 +0000 From: "Lendacky, Thomas" To: devel@edk2.groups.io CC: Brijesh Singh , Ard Biesheuvel , Eric Dong , Jordan Justen , Laszlo Ersek , Liming Gao , Michael D Kinney , Ray Ni Subject: [PATCH v11 06/46] MdePkg/BaseLib: Add support for the XGETBV instruction Date: Tue, 21 Jul 2020 16:18:43 -0500 Message-ID: <0d091f43ae96ca4617f6a95a018bfaed6280eec2.1595366363.git.thomas.lendacky@amd.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: References: X-ClientProxiedBy: SN6PR04CA0091.namprd04.prod.outlook.com (2603:10b6:805:f2::32) To CY4PR12MB1352.namprd12.prod.outlook.com (2603:10b6:903:3a::13) 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 SN6PR04CA0091.namprd04.prod.outlook.com (2603:10b6:805:f2::32) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3195.18 via Frontend Transport; Tue, 21 Jul 2020 21:20:27 +0000 X-Mailer: git-send-email 2.27.0 X-Originating-IP: [165.204.77.1] X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-HT: Tenant X-MS-Office365-Filtering-Correlation-Id: c65bc314-037d-445c-e700-08d82dbbe42c X-MS-TrafficTypeDiagnostic: CY4PR12MB1606: X-MS-Exchange-Transport-Forked: True X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:7219; X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: bVITvdFlY2xzR8AYWwVlzrBIIRz8pTynaI+94g8Qd0s2ZvMMgTK+uYNBOHjSf4dHTZ5LgvQ/hU0ApLFhFCuckTaqK6uLTGEsWRrATZ+ovCw0oVuKhnwkcSHfyFHmitVFC8Vc/vZXjL9lZos02MjmPL7408bo3+d+2UY4C10bAcxCyCVpAxLz8/dona5KIWB1/otIO9kiM4dr8v8JrBfzQDmoZzNjjHD+BYyBEB/rT78QuR/0UU5ayjsFVAlzzhQ5xMDtG3x2SHBfBwSpjg8PzTXJD3R6HQ7T3wx1tdHjULQC/5AAvONjdj+1w49WPjNnFmFt2xRmJollpbGpSJlD9p8Gxc7EENKx2Pn/9h79TpKlPZw0R3is5JJOR7hlOiguUzIcpBPvd0PC+KHYOcz0qIa5ya6+CTjx4mHt8JYCot0GX/IGXbSr7/rO3BsEmuBwKzKQ5BcJq7Zt8lmuCanljQ== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:CY4PR12MB1352.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFTY:;SFS:(4636009)(136003)(39860400002)(346002)(396003)(366004)(376002)(6916009)(54906003)(2616005)(956004)(316002)(6666004)(26005)(4326008)(186003)(66476007)(19627235002)(36756003)(16526019)(66556008)(66946007)(2906002)(6486002)(8676002)(52116002)(83380400001)(966005)(5660300002)(478600001)(86362001)(8936002)(7696005)(136400200001);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData: DdP6T/QFsoc0TYbGC8wi9G9+Qf2XCtSdOtq8BSkDLauNwJ085mwYl8W9g37gTQQAEZ/ZW5Xy5lCML91fNcwtRy1UieXTAHSX0jSubYRuwwR1bUuGktriIuTFahCkNQHI6Odc8oivOZ6I2LnDWKMK9rn6fE/uKYIIH1rjFTfUab+dhe/HJeZTcwicxiGyuJg1yl6XFk3bo9i3MbokF/RjgHGz2DPrp2WZ8sOYNTIFufco4UsDklGdid4E9E+jkWnn7Iv1rjJkQmL03EQ35bqlUNqF3iYo2cjUwLH4Av58sgSNGpXNnCyoptipztEUW1mqeXosfrfyPxW1rLndjNyT1PKTMer4eWRzz6uZoy5J6yQSrWqDniWuJpZG/q9+8b3kXtfLXNMJJfFYEgxbJycYsHozrAiF8JC+0F53nQgd0uaJFnFy/UEYoiAj1ItAjK/AhrzhKJSIiKGgIU83PO8pQ4RiZoW7TnSpbu8eTEZK3Mg= X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-Network-Message-Id: c65bc314-037d-445c-e700-08d82dbbe42c X-MS-Exchange-CrossTenant-AuthSource: CY4PR12MB1352.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 21 Jul 2020 21:20:28.4946 (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: QLLO/Zbg8c4FXdsRWbROIPuR24trAEvEkJ8qvznho+1tupwDniSwszp8VNtL18qCVDqODgDwxW/puCRVhMbXIg== X-MS-Exchange-Transport-CrossTenantHeadersStamped: CY4PR12MB1606 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: quoted-printable From: Tom Lendacky 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 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 =20 =20 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 =20 =20 X64/Non-existing.c diff --git a/MdePkg/Include/Library/BaseLib.h b/MdePkg/Include/Library/Base= Lib.h index 8e7b87cbda4e..7edf0051a0a0 100644 --- a/MdePkg/Include/Library/BaseLib.h +++ b/MdePkg/Include/Library/BaseLib.h @@ -7831,6 +7831,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 + + @return 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 6ed938187a08..c2565ab9a183 100644 --- a/MdePkg/Library/BaseLib/Ia32/GccInline.c +++ b/MdePkg/Library/BaseLib/Ia32/GccInline.c @@ -584,3 +584,31 @@ AsmReadTsc ( =20 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 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 40a208f1985f..65f864e35922 100644 --- a/MdePkg/Library/BaseLib/X64/GccInline.c +++ b/MdePkg/Library/BaseLib/X64/GccInline.c @@ -560,3 +560,33 @@ AsmReadTsc ( =20 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 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..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/BaseLi= b/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 + --=20 2.27.0