From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: mx.groups.io; dkim=pass header.i=@amdcloud.onmicrosoft.com header.s=selector2-amdcloud-onmicrosoft-com header.b=rC+ASnh5; spf=none, err=SPF record not found (domain: amd.com, ip: , mailfrom: thomas.lendacky@amd.com) Received: from NAM02-CY1-obe.outbound.protection.outlook.com (NAM02-CY1-obe.outbound.protection.outlook.com []) by groups.io with SMTP; Thu, 19 Sep 2019 12:52:45 -0700 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=G0/yZ1GdElcgllHm3Jvf3OaI0zJTRb7iHka3y9Y0MlqLlWy+s/KHeLz1+2B+oVBPemyNQvCQjR7qBDVOI8W39jRqeaEJ4jkibW5AG1AzRWiXOV5tieQrvr9VjCGL7zcFQeNuEprc9hVi/AOzy0lQLLwt7+/IAd51/KMzOVIEUtgy2rE+yJhPKAzWMjbnvTBEuu1TmmO0Lx5GlhmoypFGjeAVmu0EHPZ6Cvouka6Q/s6PfEJZd3ZxfwqWarfJLya4Jm5trpNfx2RQsVabF4cR7A8R6HSFnaJLCSTVii88jGNFEPJfDVwZHvpS5ltDNm4VjwCXN1cdwoTHj/gJgVn8KA== 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=MTAyrAzWFlcNou84MNXAc+SMlAOBdXSpfYlWY4d1TPs=; b=k7qm+nWgeUDrQeV1Lj8NilrAzWLzoXz8mZFikAP/njnKA8CKddrOnhroHpOpdQBwDUGk50b9RQ/h/6CLKbNBfHglJWQsbyt7Z1pcofNxlyeAZJ+VPoJwh9P7R4v4BiQxJzT9+tt0JmxnIrDxc2s82yGA3L/gKudELT1z6FXbx2ZObWpt12qpZXlbz9VxBr7gA7+1iu7wkWVJMUr0reAUeUzFwyTeo/skMTaeBGJys0inkMDnl4qKLijA/S19AA97zRcS/sJtbxzlZOYLOYjzs9Ay+PsP46rHB5GAkDl3eQeeILbodsqjfTBBixSHQsDmgoLCMr0bTpjXm3Ij9iMbnw== 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=MTAyrAzWFlcNou84MNXAc+SMlAOBdXSpfYlWY4d1TPs=; b=rC+ASnh5pKb3k4iWJ39cp1UbTmzwifWTH4wVeRjdXC5rw9wg8RE8YnAKrqXWQf+g8dHGa7R1U02w66X71ief/VxevdlD47WyqOItw1I5w+qbQ4or6g/yBK5w5y8T8+0UL8+1Qzt869hR1GgUDYwM0aEU0AsHDMavn1KQxBDS2FY= Received: from DM6PR12MB3163.namprd12.prod.outlook.com (20.179.104.150) by DM6PR12MB3228.namprd12.prod.outlook.com (20.179.105.96) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.2263.13; Thu, 19 Sep 2019 19:52:40 +0000 Received: from DM6PR12MB3163.namprd12.prod.outlook.com ([fe80::400e:f0c3:7ca:2fcc]) by DM6PR12MB3163.namprd12.prod.outlook.com ([fe80::400e:f0c3:7ca:2fcc%6]) with mapi id 15.20.2284.009; Thu, 19 Sep 2019 19:52:40 +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 , "Singh, Brijesh" Subject: [RFC PATCH v2 14/44] UefiCpuPkg: Implement library support for VMGEXIT Thread-Topic: [RFC PATCH v2 14/44] UefiCpuPkg: Implement library support for VMGEXIT Thread-Index: AQHVbyPLcIVKgoXR1EuouJUf+lbdSQ== Date: Thu, 19 Sep 2019 19:52:40 +0000 Message-ID: <5427110cedd114a56a77b2895f0261e18b11a455.1568922728.git.thomas.lendacky@amd.com> References: In-Reply-To: Accept-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-mailer: git-send-email 2.17.1 x-clientproxiedby: SN4PR0501CA0146.namprd05.prod.outlook.com (2603:10b6:803:2c::24) To DM6PR12MB3163.namprd12.prod.outlook.com (2603:10b6:5:182::22) authentication-results: spf=none (sender IP is ) smtp.mailfrom=Thomas.Lendacky@amd.com; x-ms-exchange-messagesentrepresentingtype: 1 x-originating-ip: [165.204.78.1] x-ms-publictraffictype: Email x-ms-office365-filtering-correlation-id: 52e13542-fb8c-4c53-54a1-08d73d3aeda9 x-ms-office365-filtering-ht: Tenant x-microsoft-antispam: BCL:0;PCL:0;RULEID:(2390118)(7020095)(4652040)(8989299)(5600167)(711020)(4605104)(1401327)(4618075)(4534185)(4627221)(201703031133081)(201702281549075)(8990200)(2017052603328)(7193020);SRVR:DM6PR12MB3228; x-ms-traffictypediagnostic: DM6PR12MB3228: x-ms-exchange-purlcount: 1 x-ms-exchange-transport-forked: True x-microsoft-antispam-prvs: x-ms-oob-tlc-oobclassifiers: OLM:9508; x-forefront-prvs: 016572D96D x-forefront-antispam-report: SFV:NSPM;SFS:(10009020)(4636009)(366004)(376002)(396003)(136003)(39860400002)(346002)(189003)(199004)(4326008)(26005)(446003)(25786009)(52116002)(71200400001)(71190400001)(8936002)(81156014)(81166006)(256004)(8676002)(186003)(50226002)(2351001)(2616005)(102836004)(486006)(11346002)(476003)(66066001)(386003)(6506007)(66446008)(14454004)(76176011)(99286004)(6486002)(36756003)(5640700003)(66556008)(118296001)(3846002)(316002)(7736002)(2501003)(1730700003)(6116002)(5660300002)(478600001)(54906003)(6916009)(966005)(6436002)(2906002)(64756008)(305945005)(86362001)(6512007)(6306002)(66476007)(66946007);DIR:OUT;SFP:1101;SCL:1;SRVR:DM6PR12MB3228;H:DM6PR12MB3163.namprd12.prod.outlook.com;FPR:;SPF:None;LANG:en;PTR:InfoNoRecords;MX:1;A:1; received-spf: None (protection.outlook.com: amd.com does not designate permitted sender hosts) x-ms-exchange-senderadcheck: 1 x-microsoft-antispam-message-info: STPABSmmJTLI0TgrCUs9yKELoPxrndylxm2Vtnm9FuP0cy7XKfPImaVl4YSO1bCD6g3WBBcLTwNiPZM1Bamc1TVy5Pq3EgOjajJli4uMG80m2qfp/SbxoiYwUOP79ylzD2880VKA+52DW9r4JnqgNEZBomzO268kyv02OStzj9N66liHdWCrEq0dSz8SjuprSdRZsAxh4VgYe9h8Ka17n3uKCpUx4VazCQ5C9Rq1bmTQRgRQ+IoKa05MgLoQkCnJpIRjr+/l1xaGbcbfu6aQkswrL38uX85qcSuOYHYN2IDQR+jgOMKSwyxfE91TtsUGU0TBaAJ+FgoACDLPSTiqV0ccmvNUYC5OYrRrJOHCn+YPIaGfcqkSWzdQBPOuHVCiY5wInk1sovOFgkL8MXfJUG7GJAHWuwUG4ffNt1zl4dU= MIME-Version: 1.0 X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-Network-Message-Id: 52e13542-fb8c-4c53-54a1-08d73d3aeda9 X-MS-Exchange-CrossTenant-originalarrivaltime: 19 Sep 2019 19:52:40.3478 (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: 7PEPbO7P7PDgXr6w53oaLpMs0zMYWaeCCG/fgNsSfDPRTizbHK9RQZAGbabZEDy4TmWAFPPCkho5EHngC4TcXw== X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM6PR12MB3228 Content-Language: en-US Content-Type: text/plain; charset="us-ascii" Content-ID: <317BE8DAF901014388C06418EB067019@namprd12.prod.outlook.com> Content-Transfer-Encoding: quoted-printable From: Tom Lendacky BZ: https://bugzilla.tianocore.org/show_bug.cgi?id=3D2198 To support issuing a VMGEXIT instruction, create a library that can be used to perform GHCB and VMGEXIT related operations and to issue the actual VMGEXIT instruction when using the GHCB. Cc: Eric Dong Cc: Ray Ni Cc: Laszlo Ersek Signed-off-by: Tom Lendacky --- UefiCpuPkg/UefiCpuPkg.dec | 3 + UefiCpuPkg/UefiCpuPkg.dsc | 5 ++ UefiCpuPkg/Library/VmgExitLib/VmgExitLib.inf | 33 +++++++++ UefiCpuPkg/Include/Library/VmgExitLib.h | 74 +++++++++++++++++++ UefiCpuPkg/Library/VmgExitLib/VmgExitLib.c | 75 ++++++++++++++++++++ UefiCpuPkg/Library/VmgExitLib/VmgExitLib.uni | 15 ++++ 6 files changed, 205 insertions(+) create mode 100644 UefiCpuPkg/Library/VmgExitLib/VmgExitLib.inf create mode 100644 UefiCpuPkg/Include/Library/VmgExitLib.h create mode 100644 UefiCpuPkg/Library/VmgExitLib/VmgExitLib.c create mode 100644 UefiCpuPkg/Library/VmgExitLib/VmgExitLib.uni diff --git a/UefiCpuPkg/UefiCpuPkg.dec b/UefiCpuPkg/UefiCpuPkg.dec index 14ddaa8633c6..5d2bd5b7c0eb 100644 --- a/UefiCpuPkg/UefiCpuPkg.dec +++ b/UefiCpuPkg/UefiCpuPkg.dec @@ -53,6 +53,9 @@ [LibraryClasses.IA32, LibraryClasses.X64] ## MpInitLib|Include/Library/MpInitLib.h =20 + ## @libraryclass Provides function to support VMGEXIT processing. + VmgExitLib|Include/Library/VmgExitLib.h + [Guids] gUefiCpuPkgTokenSpaceGuid =3D { 0xac05bf33, 0x995a, 0x4ed4, { 0xaa,= 0xb8, 0xef, 0x7a, 0xe8, 0xf, 0x5c, 0xb0 }} gMsegSmramGuid =3D { 0x5802bce4, 0xeeee, 0x4e33, { 0xa1,= 0x30, 0xeb, 0xad, 0x27, 0xf0, 0xe4, 0x39 }} diff --git a/UefiCpuPkg/UefiCpuPkg.dsc b/UefiCpuPkg/UefiCpuPkg.dsc index bf690d3978f1..3ab96f6f986f 100644 --- a/UefiCpuPkg/UefiCpuPkg.dsc +++ b/UefiCpuPkg/UefiCpuPkg.dsc @@ -63,6 +63,7 @@ [LibraryClasses.common.SEC] HobLib|MdePkg/Library/PeiHobLib/PeiHobLib.inf PeiServicesTablePointerLib|MdePkg/Library/PeiServicesTablePointerLibIdt/= PeiServicesTablePointerLibIdt.inf MemoryAllocationLib|MdePkg/Library/PeiMemoryAllocationLib/PeiMemoryAlloc= ationLib.inf + VmgExitLib|UefiCpuPkg/Library/VmgExitLib/VmgExitLib.inf =20 [LibraryClasses.common.PEIM] MemoryAllocationLib|MdePkg/Library/PeiMemoryAllocationLib/PeiMemoryAlloc= ationLib.inf @@ -74,6 +75,7 @@ [LibraryClasses.common.PEIM] [LibraryClasses.IA32.PEIM, LibraryClasses.X64.PEIM] PeiServicesTablePointerLib|MdePkg/Library/PeiServicesTablePointerLibIdt/= PeiServicesTablePointerLibIdt.inf CpuExceptionHandlerLib|UefiCpuPkg/Library/CpuExceptionHandlerLib/PeiCpuE= xceptionHandlerLib.inf + VmgExitLib|UefiCpuPkg/Library/VmgExitLib/VmgExitLib.inf =20 [LibraryClasses.common.DXE_DRIVER] MemoryAllocationLib|MdePkg/Library/UefiMemoryAllocationLib/UefiMemoryAll= ocationLib.inf @@ -81,12 +83,14 @@ [LibraryClasses.common.DXE_DRIVER] CpuExceptionHandlerLib|UefiCpuPkg/Library/CpuExceptionHandlerLib/DxeCpuE= xceptionHandlerLib.inf MpInitLib|UefiCpuPkg/Library/MpInitLib/DxeMpInitLib.inf RegisterCpuFeaturesLib|UefiCpuPkg/Library/RegisterCpuFeaturesLib/DxeRegi= sterCpuFeaturesLib.inf + VmgExitLib|UefiCpuPkg/Library/VmgExitLib/VmgExitLib.inf =20 [LibraryClasses.common.DXE_SMM_DRIVER] SmmServicesTableLib|MdePkg/Library/SmmServicesTableLib/SmmServicesTableL= ib.inf MemoryAllocationLib|MdePkg/Library/SmmMemoryAllocationLib/SmmMemoryAlloc= ationLib.inf HobLib|MdePkg/Library/DxeHobLib/DxeHobLib.inf CpuExceptionHandlerLib|UefiCpuPkg/Library/CpuExceptionHandlerLib/SmmCpuE= xceptionHandlerLib.inf + VmgExitLib|UefiCpuPkg/Library/VmgExitLib/VmgExitLib.inf =20 [LibraryClasses.common.UEFI_APPLICATION] UefiApplicationEntryPoint|MdePkg/Library/UefiApplicationEntryPoint/UefiA= pplicationEntryPoint.inf @@ -143,6 +147,7 @@ [Components.IA32, Components.X64] SmmCpuFeaturesLib|UefiCpuPkg/Library/SmmCpuFeaturesLib/SmmCpuFeature= sLibStm.inf } UefiCpuPkg/Universal/Acpi/S3Resume2Pei/S3Resume2Pei.inf + UefiCpuPkg/Library/VmgExitLib/VmgExitLib.inf =20 [BuildOptions] *_*_*_CC_FLAGS =3D -D DISABLE_NEW_DEPRECATED_INTERFACES diff --git a/UefiCpuPkg/Library/VmgExitLib/VmgExitLib.inf b/UefiCpuPkg/Libr= ary/VmgExitLib/VmgExitLib.inf new file mode 100644 index 000000000000..f1c1af93b297 --- /dev/null +++ b/UefiCpuPkg/Library/VmgExitLib/VmgExitLib.inf @@ -0,0 +1,33 @@ +## @file +# VMGEXIT Support Library. +# +# Copyright (c) 2019, AMD Inc. All rights reserved.
+# SPDX-License-Identifier: BSD-2-Clause-Patent +# +## + +[Defines] + INF_VERSION =3D 0x00010005 + BASE_NAME =3D VmgExitLib + MODULE_UNI_FILE =3D VmgExitLib.uni + FILE_GUID =3D 3cd7368f-ef9b-4a9b-9571-2ed93813677e + MODULE_TYPE =3D BASE + VERSION_STRING =3D 1.0 + LIBRARY_CLASS =3D VmgExitLib + +# +# The following information is for reference only and not required by the = build tools. +# +# VALID_ARCHITECTURES =3D IA32 X64 +# + +[Sources] + VmgExitLib.c + +[Packages] + MdePkg/MdePkg.dec + UefiCpuPkg/UefiCpuPkg.dec + +[LibraryClasses] + BaseLib + diff --git a/UefiCpuPkg/Include/Library/VmgExitLib.h b/UefiCpuPkg/Include/L= ibrary/VmgExitLib.h new file mode 100644 index 000000000000..4b3c6908f1c1 --- /dev/null +++ b/UefiCpuPkg/Include/Library/VmgExitLib.h @@ -0,0 +1,74 @@ +/** @file + Public header file for the VMGEXIT Support library class. + + This library class defines some routines used when invoking the VMGEXIT + instruction in support of SEV-ES. + + Copyright (c) 2019, AMD Inc. All rights reserved.
+ SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#ifndef __VMG_EXIT_LIB_H__ +#define __VMG_EXIT_LIB_H__ + +#include + + +/** + Perform VMGEXIT. + + Sets the necessary fields of the GHCB, invokes the VMGEXIT instruction a= nd + then handles the return actions. + + @param[in] GHCB A pointer to the GHCB + @param[in] ExitCode VMGEXIT code to be assigned to the SwExitCode fie= ld of + the GHCB. + @param[in] ExitInfo1 VMGEXIT information to be assigned to the SwExitI= nfo1 + field of the GHCB. + @param[in] ExitInfo2 VMGEXIT information to be assigned to the SwExitI= nfo2 + field of the GHCB. + + @retval 0 VMGEXIT succeeded. + @retval Others VMGEXIT processing did not succeed. Exception number to + be issued. + +**/ +UINTN +EFIAPI +VmgExit ( + GHCB *Ghcb, + UINT64 ExitCode, + UINT64 ExitInfo1, + UINT64 ExitInfo2 + ); + +/** + Perform pre-VMGEXIT initialization/preparation. + + Performs the necessary steps in preparation for invoking VMGEXIT. + + @param[in] GHCB A pointer to the GHCB + +**/ +VOID +EFIAPI +VmgInit ( + GHCB *Ghcb + ); + +/** + Perform post-VMGEXIT cleanup. + + Performs the necessary steps to cleanup after invoking VMGEXIT. + + @param[in] GHCB A pointer to the GHCB + +**/ +VOID +EFIAPI +VmgDone ( + GHCB *Ghcb + ); + +#endif diff --git a/UefiCpuPkg/Library/VmgExitLib/VmgExitLib.c b/UefiCpuPkg/Librar= y/VmgExitLib/VmgExitLib.c new file mode 100644 index 000000000000..f7e2571cd726 --- /dev/null +++ b/UefiCpuPkg/Library/VmgExitLib/VmgExitLib.c @@ -0,0 +1,75 @@ +/** @file + VMGEXIT Support Library. + + Copyright (c) 2019, AMD Inc. All rights reserved.
+ SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include +#include +#include + +STATIC +UINTN +VmgExitErrorCheck ( + GHCB *Ghcb + ) +{ + GHCB_EXIT_INFO ExitInfo; + UINTN Reason, Action; + + if (!Ghcb->SaveArea.SwExitInfo1) { + return 0; + } + + ExitInfo.Uint64 =3D Ghcb->SaveArea.SwExitInfo1; + Action =3D ExitInfo.Elements.Lower32Bits; + if (Action =3D=3D 1) { + Reason =3D ExitInfo.Elements.Upper32Bits; + + switch (Reason) { + case UD_EXCEPTION: + case GP_EXCEPTION: + return Reason; + } + } + + ASSERT (0); + return GP_EXCEPTION; +} + +UINTN +EFIAPI +VmgExit ( + GHCB *Ghcb, + UINT64 ExitCode, + UINT64 ExitInfo1, + UINT64 ExitInfo2 + ) +{ + Ghcb->SaveArea.SwExitCode =3D ExitCode; + Ghcb->SaveArea.SwExitInfo1 =3D ExitInfo1; + Ghcb->SaveArea.SwExitInfo2 =3D ExitInfo2; + AsmVmgExit (); + + return VmgExitErrorCheck (Ghcb); +} + +VOID +EFIAPI +VmgInit ( + GHCB *Ghcb + ) +{ + SetMem (&Ghcb->SaveArea, sizeof (Ghcb->SaveArea), 0); +} + +VOID +EFIAPI +VmgDone ( + GHCB *Ghcb + ) +{ +} + diff --git a/UefiCpuPkg/Library/VmgExitLib/VmgExitLib.uni b/UefiCpuPkg/Libr= ary/VmgExitLib/VmgExitLib.uni new file mode 100644 index 000000000000..65db52a05576 --- /dev/null +++ b/UefiCpuPkg/Library/VmgExitLib/VmgExitLib.uni @@ -0,0 +1,15 @@ +// /** @file +// VMGEXIT support library instance. +// +// VMGEXIT support library instance. +// +// Copyright (c) 2019, AMD Inc. All rights reserved.
+// SPDX-License-Identifier: BSD-2-Clause-Patent +// +// **/ + + +#string STR_MODULE_ABSTRACT #language en-US "VMGEXIT Support L= ibrary." + +#string STR_MODULE_DESCRIPTION #language en-US "VMGEXIT Support L= ibrary." + --=20 2.17.1