From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mga17.intel.com (mga17.intel.com [192.55.52.151]) by mx.groups.io with SMTP id smtpd.web10.2530.1588986382897048031 for ; Fri, 08 May 2020 18:06:22 -0700 Authentication-Results: mx.groups.io; dkim=pass header.i=@intel.onmicrosoft.com header.s=selector2-intel-onmicrosoft-com header.b=WhZP1KYa; spf=pass (domain: intel.com, ip: 192.55.52.151, mailfrom: eric.dong@intel.com) IronPort-SDR: kFfYP7xBo2d57fnzZTwZWKAeKVSqcnypTewPg9eyMbcm3huHXyPQ2X7fNaxvwPyiVAxM0X9R5F Wgc+yH2s9Khw== X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from orsmga001.jf.intel.com ([10.7.209.18]) by fmsmga107.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 08 May 2020 18:06:22 -0700 IronPort-SDR: YDwK/QnE/vWr/151L/7skXMWdbDLUwkseTa+q8XcqELBGm8jWe6XF3h1M1Kob2dFekRhmG7dOc xJp1Dq1nYZ4g== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.73,369,1583222400"; d="scan'208";a="339971620" Received: from fmsmsx107.amr.corp.intel.com ([10.18.124.205]) by orsmga001.jf.intel.com with ESMTP; 08 May 2020 18:06:21 -0700 Received: from fmsmsx155.amr.corp.intel.com (10.18.116.71) by fmsmsx107.amr.corp.intel.com (10.18.124.205) with Microsoft SMTP Server (TLS) id 14.3.439.0; Fri, 8 May 2020 18:06:21 -0700 Received: from FMSEDG002.ED.cps.intel.com (10.1.192.134) by FMSMSX155.amr.corp.intel.com (10.18.116.71) with Microsoft SMTP Server (TLS) id 14.3.439.0; Fri, 8 May 2020 18:06:21 -0700 Received: from NAM11-CO1-obe.outbound.protection.outlook.com (104.47.56.177) by edgegateway.intel.com (192.55.55.69) with Microsoft SMTP Server (TLS) id 14.3.439.0; Fri, 8 May 2020 18:06:21 -0700 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=L2q3EvVsN3/iElK2SSLHHzXiSS+TfFoXkIhr8mJ0TgWPEL52BLyFyDiJP0LP/xtBbS/3LL0wwUNGSRYBOgCSTKVIRxgnlKZGgg23Nhw7M7JYue7JbDccSwykfxYQiXSMz8lN8pv/73mk7wys57Tjhwj84lsxOFFSe0Ch2whaTOvMFHwdDgVO5TBkoI5HJaNu2M1p/qcI9tqK7LCLvH4Qtj+yBZ8Xvcztw2Mp8nqlGPvJmBWntARE/YQNn+w1sXhakm058IrL6okfHSl6rkMK2Bse3Wrv4/FRDoIljwIaFtaRBcs5IGdMjrDtCr+vvYbLRaNP5IMh483AlE3UIsOPfw== 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=8Tex1CC0RrhzLsQlRwIVnFPyAXkdhIZ5/PR8LIFt9SM=; b=jbl+jI+XRy0vaYbgETbQFkokJwTHwA6EigsUryHg83IDUVcT+tg/y6G9OKe+nr3UHQZGaVib67d4EM9FhF0ToyWJznd/+V30SwI7vrP1X2+37wI+jkkyggd+NBL3YuPdk9dkKcFO94HTNQGaC6UtHuxZqLetLESlIwnVmlSv9ohXEsDXmHVIM7ykGgdSByKz1HQWLB/yGAQwb/bd+qas6R+16j+8hOCGCmTiAS523+W5Kh+tpwV6j3CI42O/MsqznC39be0/wOJIeCqpFL97TJm/CVX/z6Aq/LzoendkFEjsLnRqXYdG8Jud0xjSe/S482yZsxpLwQQDoBXE5INewQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=intel.com; dmarc=pass action=none header.from=intel.com; dkim=pass header.d=intel.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=intel.onmicrosoft.com; s=selector2-intel-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=8Tex1CC0RrhzLsQlRwIVnFPyAXkdhIZ5/PR8LIFt9SM=; b=WhZP1KYagJvoa4FueZGUq5krKhiWcXkGYib0nbj63yHMUZZ/6hUA23prKp5CtbQOgVeN7M1qn9BRptd+HD3XMEAvErZzfXFS1SagqCn/uIVS3DeUT+XM72jCGYZKVrBcfs9WcilI5ghbwQiU6+cDLWkOnAF+konOUKbnuWKeEzk= Received: from DM6PR11MB3274.namprd11.prod.outlook.com (2603:10b6:5:b::26) by DM6PR11MB3979.namprd11.prod.outlook.com (2603:10b6:5:197::33) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.2979.28; Sat, 9 May 2020 01:06:19 +0000 Received: from DM6PR11MB3274.namprd11.prod.outlook.com ([fe80::e1d1:a495:9826:f88b]) by DM6PR11MB3274.namprd11.prod.outlook.com ([fe80::e1d1:a495:9826:f88b%3]) with mapi id 15.20.2979.028; Sat, 9 May 2020 01:06:19 +0000 From: "Dong, Eric" To: Tom Lendacky , "devel@edk2.groups.io" CC: "Justen, Jordan L" , Laszlo Ersek , Ard Biesheuvel , "Kinney, Michael D" , "Gao, Liming" , "Ni, Ray" , Brijesh Singh Subject: Re: [PATCH v7 08/43] UefiCpuPkg: Implement library support for VMGEXIT Thread-Topic: [PATCH v7 08/43] UefiCpuPkg: Implement library support for VMGEXIT Thread-Index: AQHWGM1xbXom5gUBjECD2fGqHS8R36ifBn7Q Date: Sat, 9 May 2020 01:06:19 +0000 Message-ID: References: <555901ab0879836aa262f29db634a330208170fa.1587577317.git.thomas.lendacky@amd.com> In-Reply-To: <555901ab0879836aa262f29db634a330208170fa.1587577317.git.thomas.lendacky@amd.com> Accept-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: authentication-results: amd.com; dkim=none (message not signed) header.d=none;amd.com; dmarc=none action=none header.from=intel.com; x-originating-ip: [192.102.204.45] x-ms-publictraffictype: Email x-ms-office365-filtering-correlation-id: 9e1ca13d-e310-45f7-efb6-08d7f3b52e97 x-ms-traffictypediagnostic: DM6PR11MB3979: x-ld-processed: 46c98d88-e344-4ed4-8496-4ed7712e255d,ExtAddr x-ms-exchange-transport-forked: True x-microsoft-antispam-prvs: x-ms-oob-tlc-oobclassifiers: OLM:10000; x-forefront-prvs: 03982FDC1D x-ms-exchange-senderadcheck: 1 x-microsoft-antispam: BCL:0; x-microsoft-antispam-message-info: CDlo1Sr/v9VCayVeyXYzSireMj3RAq2CRSartomA7xqDHkkkUZN2yxejTvIuLSrgiXNqjoHU7KMztukHneDvmMWA3klFdVdL2ut+/hb7Tw+kXlh96Tv4XQHxe2aPTuJWIKKiq3gqhjiMq15spAmY22+p0R020Yp3/T07l6aQHwYTzXGzJNnIFWNC5/cL1N3ojh1dO+v6N+gzAHp8Ua7a/P7by+qLwvbahbrh7r8CH0m0X4kxB1GXe6tuuBaDyvuYmq4iVaRFzOR/oiqP8LKO5X23yputup0CKhprzb9IBqmb7idXmMJPTos4sbJh9GnmR4O+RPAQJ7dwyRGBZrrSNw1UsfmbLOYGLV7MbAqzIng4bk84YepHURSjMC0dbHN4o7lrud8MLCyw3ZHmpJss4aoJl25doD1AmwrI7SfObBlWt4Qv9rF6wuMzL4Xu4ntwKFyj3sRnw0v0KbVxeYeStDzyImc23HJdfO5OPfFyi3/tEqcov9LB/SpEpjuNR6wCFMYjt5gPYpjZiYMogO0nnTOF+uHeuwtsPiCjlF/o0Wy/QqXO1/wyi3Fa37ajCI14sYsiXo4DU4YWHekweP2RZe/pr8fwU6EeMLCIqNmYLwk= x-forefront-antispam-report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:DM6PR11MB3274.namprd11.prod.outlook.com;PTR:;CAT:NONE;SFTY:;SFS:(376002)(396003)(346002)(136003)(366004)(39860400002)(33430700001)(26005)(186003)(33656002)(478600001)(86362001)(9686003)(8676002)(2906002)(52536014)(55016002)(8936002)(76116006)(4326008)(33440700001)(66946007)(7696005)(71200400001)(66476007)(54906003)(64756008)(66446008)(110136005)(5660300002)(53546011)(66556008)(316002)(966005)(30864003)(6506007);DIR:OUT;SFP:1102; x-ms-exchange-antispam-messagedata: 7tWNMOPfMSDTnvjPpQ8FzdjZKWCO7KfL1Zo633HfVi8fxA598RV6gax7T9Ycx4s8/BMoRkt9HnrVMnssQWVr6AFFGCu+6vfSLvLe7kBMrbYNHrbaXYQ4OnpZ35XWEsBWBigz+ZYJBscE+MfIy035oxU9OUFPsOpe7GUm2x2+SH7teNFpvDMUIlg+JQBZUBX641RIYRFW6NR5hqbFJCjF5e8e6JmrMXaE+vivo/Uf+P5iYXuOGj/CVHVSthxwgO6SHEdBsklW6MBDYF0m/Q/P51NVsyRPzgyCWq0wNoqcv3xYf1dI+8z8tOKwagRK4wymHqYzb/4JCYUa1BYs9nREF/TbdKiuMBgOFn/PCfDlEAIdsDS8W0IK4xCF4Qbl81Lp0qKTzPfWRFWcEgZ+Krv+PVhD+GsoFg81Pd93+t5SSIOhV/1FqS23AzvXDkGnuk+DZDnL3uOU3UluI+/s5JJxVPDggyec8sn5FtT6Za6kdYSPzH4kbZpOA+gGoEFHJmMq MIME-Version: 1.0 X-MS-Exchange-CrossTenant-Network-Message-Id: 9e1ca13d-e310-45f7-efb6-08d7f3b52e97 X-MS-Exchange-CrossTenant-originalarrivaltime: 09 May 2020 01:06:19.0824 (UTC) X-MS-Exchange-CrossTenant-fromentityheader: Hosted X-MS-Exchange-CrossTenant-id: 46c98d88-e344-4ed4-8496-4ed7712e255d X-MS-Exchange-CrossTenant-mailboxtype: HOSTED X-MS-Exchange-CrossTenant-userprincipalname: CT7y/qzQHANrBstd55QMw/0S1likIu4h+kQVTeDQCF8uO4OJEfK2rQO2GRPQyjQPL4ogQR2U+1KLITgWJw00FA== X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM6PR11MB3979 Return-Path: eric.dong@intel.com X-OriginatorOrg: intel.com Content-Language: en-US Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: quoted-printable Hi Tom, > -----Original Message----- > From: Tom Lendacky > Sent: Thursday, April 23, 2020 1:41 AM > To: devel@edk2.groups.io > Cc: Justen, Jordan L ; Laszlo Ersek > ; Ard Biesheuvel ; Kinney, > Michael D ; Gao, Liming > ; Dong, Eric ; Ni, Ray > ; Brijesh Singh > Subject: [PATCH v7 08/43] UefiCpuPkg: Implement library support for > VMGEXIT >=20 > BZ: https://bugzilla.tianocore.org/show_bug.cgi?id=3D2198 >=20 > To support issuing a VMGEXIT instruction, create a library that can be us= ed to > perform GHCB and VMGEXIT related operations and to issue the actual > VMGEXIT instruction when using the GHCB. >=20 > Additionally, two VMGEXIT / MMIO related functions are created to support > flash emulation. Flash emulation currently is done by marking the flash a= rea > as read-only and taking a nested page fault to perform the emulation of t= he > instruction. However, emulation cannot be performed because there is no > instruction decode assist support when SEV-ES is enabled. Provide routine= s > to initiate an MMIO request to perform actual writes to flash. >=20 > Cc: Eric Dong > Cc: Ray Ni > Cc: Laszlo Ersek > Acked-by: Laszlo Ersek > Signed-off-by: Tom Lendacky > --- > UefiCpuPkg/UefiCpuPkg.dec | 3 + > UefiCpuPkg/UefiCpuPkg.dsc | 2 + > UefiCpuPkg/Library/VmgExitLib/VmgExitLib.inf | 33 +++ > UefiCpuPkg/Include/Library/VmgExitLib.h | 117 ++++++++ > UefiCpuPkg/Library/VmgExitLib/VmgExitLib.c | 293 > +++++++++++++++++++ > UefiCpuPkg/Library/VmgExitLib/VmgExitLib.uni | 15 + > 6 files changed, 463 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 >=20 > diff --git a/UefiCpuPkg/UefiCpuPkg.dec b/UefiCpuPkg/UefiCpuPkg.dec > index df5d02bae6b4..cb92f34b6f55 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, { 0xa= a, > 0xb8, 0xef, 0x7a, 0xe8, 0xf, 0x5c, 0xb0 }} > gMsegSmramGuid =3D { 0x5802bce4, 0xeeee, 0x4e33, { 0xa= 1, 0x30, > 0xeb, 0xad, 0x27, 0xf0, 0xe4, 0x39 }} > diff --git a/UefiCpuPkg/UefiCpuPkg.dsc b/UefiCpuPkg/UefiCpuPkg.dsc index > d28cb5cccb52..997840452218 100644 > --- a/UefiCpuPkg/UefiCpuPkg.dsc > +++ b/UefiCpuPkg/UefiCpuPkg.dsc > @@ -56,6 +56,7 @@ [LibraryClasses] >=20 > PeCoffGetEntryPointLib|MdePkg/Library/BasePeCoffGetEntryPointLib/Base > PeCoffGetEntryPointLib.inf >=20 > PeCoffExtraActionLib|MdePkg/Library/BasePeCoffExtraActionLibNull/BaseP > eCoffExtraActionLibNull.inf >=20 > TpmMeasurementLib|MdeModulePkg/Library/TpmMeasurementLibNull/Tp > mMeasurementLibNull.inf > + VmgExitLib|UefiCpuPkg/Library/VmgExitLib/VmgExitLib.inf >=20 > [LibraryClasses.common.SEC] >=20 > PlatformSecLib|UefiCpuPkg/Library/PlatformSecLibNull/PlatformSecLibNull.i > nf > @@ -136,6 +137,7 @@ [Components.IA32, Components.X64] >=20 > UefiCpuPkg/Library/SmmCpuPlatformHookLibNull/SmmCpuPlatformHookLib > Null.inf > UefiCpuPkg/Library/SmmCpuFeaturesLib/SmmCpuFeaturesLib.inf > UefiCpuPkg/Library/SmmCpuFeaturesLib/SmmCpuFeaturesLibStm.inf > + UefiCpuPkg/Library/VmgExitLib/VmgExitLib.inf > UefiCpuPkg/PiSmmCommunication/PiSmmCommunicationPei.inf > UefiCpuPkg/PiSmmCommunication/PiSmmCommunicationSmm.inf > UefiCpuPkg/SecCore/SecCore.inf > diff --git a/UefiCpuPkg/Library/VmgExitLib/VmgExitLib.inf > b/UefiCpuPkg/Library/VmgExitLib/VmgExitLib.inf > new file mode 100644 > index 000000000000..6acfa779e75a > --- /dev/null > +++ b/UefiCpuPkg/Library/VmgExitLib/VmgExitLib.inf > @@ -0,0 +1,33 @@ > +## @file > +# VMGEXIT Support Library. > +# > +# Copyright (c) 2019, Advanced Micro Devices, 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-2ed93813677= e > + MODULE_TYPE =3D BASE > + VERSION_STRING =3D 1.0 > + LIBRARY_CLASS =3D VmgExitLib > + > +# > +# The following information is for reference only and not required by th= e > 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/Library/VmgExitLib.h > new file mode 100644 > index 000000000000..3bf05bebd326 > --- /dev/null > +++ b/UefiCpuPkg/Include/Library/VmgExitLib.h > @@ -0,0 +1,117 @@ > +/** @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, Advanced Micro Devices, 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 and then handles the return actions. > + > + @param[in, out] Ghcb A pointer to the GHCB > + @param[in] ExitCode VMGEXIT code to be assigned to the SwExitC= ode > + field of the GHCB. > + @param[in] ExitInfo1 VMGEXIT information to be assigned to the > + SwExitInfo1 field of the GHCB. > + @param[in] ExitInfo2 VMGEXIT information to be assigned to the > + SwExitInfo2 field of the GHCB. > + > + @retval 0 VMGEXIT succeeded. > + @retval Others VMGEXIT processing did not succeed. Except= ion > + number to be propagated. > + > +**/ > +UINT64 > +EFIAPI > +VmgExit ( > + IN OUT GHCB *Ghcb, > + IN UINT64 ExitCode, > + IN UINT64 ExitInfo1, > + IN UINT64 ExitInfo2 > + ); > + > +/** > + Perform pre-VMGEXIT initialization/preparation. > + > + Performs the necessary steps in preparation for invoking VMGEXIT. > + Must be called before setting any fields within the GHCB. > + > + @param[in, out] Ghcb A pointer to the GHCB > + > +**/ > +VOID > +EFIAPI > +VmgInit ( > + IN OUT GHCB *Ghcb > + ); > + > +/** > + Perform post-VMGEXIT cleanup. > + > + Performs the necessary steps to cleanup after invoking VMGEXIT. Must > + be called after obtaining needed fields within the GHCB. > + > + @param[in, out] Ghcb A pointer to the GHCB > + > +**/ > +VOID > +EFIAPI > +VmgDone ( > + IN OUT GHCB *Ghcb > + ); > + > +#define VMGMMIO_READ False > +#define VMGMMIO_WRITE True > + > +/** > + Perform MMIO write of a buffer to a non-MMIO marked range. > + > + Performs an MMIO write without taking a #VC. This is useful for > + Flash devices, which are marked read-only. > + > + @param[in, out] Dest A pointer to the destination buffer > + @param[in] Src A pointer to the source data to be written > + @param[in] Bytes Number of bytes to write > + > +**/ > +VOID > +EFIAPI > +VmgMmioWrite ( > + IN OUT UINT8 *Dest, > + IN UINT8 *Src, > + IN UINTN Bytes > + ); > + > +/** > + Issue the GHCB set AP Jump Table VMGEXIT. > + > + Performs a VMGEXIT using the GHCB AP Jump Table exit code to save the > + AP Jump Table address with the hypervisor for retrieval at a later time= . > + > + @param[in] Address Physical address of the AP Jump Table > + > + @retval 0 VMGEXIT succeeded. > + @retval Others VMGEXIT processing did not succeed. Exception > + number to be propagated. > + > +**/ > +UINT64 > +EFIAPI > +VmgExitSetAPJumpTable ( > + IN EFI_PHYSICAL_ADDRESS Address > + ); I think above two APIs should not been added to this library, they are not = the basic actions for VmgExit.=20 Remove these two APIs will make the library more stable. Also, I check all the code in this patch series, only one caller for each A= PI, so I think we can directly move these codes to the caller. If later more and more callers need to use = these two APIs, we can create another service library to convenient the callers. I ignore all the coding style related issues in this patch because I assume= you have passed ECC checks in your new patches. Thanks, Eric > + > +#endif > diff --git a/UefiCpuPkg/Library/VmgExitLib/VmgExitLib.c > b/UefiCpuPkg/Library/VmgExitLib/VmgExitLib.c > new file mode 100644 > index 000000000000..6137b1a0eb64 > --- /dev/null > +++ b/UefiCpuPkg/Library/VmgExitLib/VmgExitLib.c > @@ -0,0 +1,293 @@ > +/** @file > + VMGEXIT Support Library. > + > + Copyright (c) 2019, Advanced Micro Devices, Inc. All rights > + reserved.
> + SPDX-License-Identifier: BSD-2-Clause-Patent > + > +**/ > + > +#include > +#include > +#include > +#include > +#include > + > +/** > + Check for VMGEXIT error > + > + Check if the hypervisor has returned an error after completion of the > + VMGEXIT by examining the SwExitInfo1 field of the GHCB. > + > + @param[in] Ghcb A pointer to the GHCB > + > + @retval 0 VMGEXIT succeeded. > + @retval Others VMGEXIT processing did not succeed. Exception > number to > + be propagated. > + > +**/ > +STATIC > +UINT64 > +VmgExitErrorCheck ( > + IN GHCB *Ghcb > + ) > +{ > + GHCB_EVENT_INJECTION Event; > + GHCB_EXIT_INFO ExitInfo; > + UINT64 Status; > + > + ExitInfo.Uint64 =3D Ghcb->SaveArea.SwExitInfo1; ASSERT > + ((ExitInfo.Elements.Lower32Bits =3D=3D 0) || > + (ExitInfo.Elements.Lower32Bits =3D=3D 1)); > + > + Status =3D 0; > + if (ExitInfo.Elements.Lower32Bits =3D=3D 0) { > + return Status; > + } > + > + if (ExitInfo.Elements.Lower32Bits =3D=3D 1) { > + ASSERT (Ghcb->SaveArea.SwExitInfo2 !=3D 0); > + > + // Check that the return event is valid > + Event.Uint64 =3D Ghcb->SaveArea.SwExitInfo2; > + if (Event.Elements.Valid && > + Event.Elements.Type =3D=3D GHCB_EVENT_INJECTION_TYPE_EXCEPTION) = { > + switch (Event.Elements.Vector) { > + case GP_EXCEPTION: > + case UD_EXCEPTION: > + // Use returned event as return code > + Status =3D Event.Uint64; > + } > + } > + } > + > + if (Status =3D=3D 0) { > + GHCB_EVENT_INJECTION Event; > + > + Event.Uint64 =3D 0; > + Event.Elements.Vector =3D GP_EXCEPTION; > + Event.Elements.Type =3D GHCB_EVENT_INJECTION_TYPE_EXCEPTION; > + Event.Elements.Valid =3D 1; > + > + Status =3D Event.Uint64; > + } > + > + return Status; > +} > + > +/** > + Perform VMGEXIT. > + > + Sets the necessary fields of the GHCB, invokes the VMGEXIT > + instruction and then handles the return actions. > + > + @param[in, out] Ghcb A pointer to the GHCB > + @param[in] ExitCode VMGEXIT code to be assigned to the SwExitC= ode > + field of the GHCB. > + @param[in] ExitInfo1 VMGEXIT information to be assigned to the > + SwExitInfo1 field of the GHCB. > + @param[in] ExitInfo2 VMGEXIT information to be assigned to the > + SwExitInfo2 field of the GHCB. > + > + @retval 0 VMGEXIT succeeded. > + @retval Others VMGEXIT processing did not succeed. Except= ion > + number to be propagated. > + > +**/ > +UINT64 > +EFIAPI > +VmgExit ( > + IN OUT GHCB *Ghcb, > + IN UINT64 ExitCode, > + IN UINT64 ExitInfo1, > + IN UINT64 ExitInfo2 > + ) > +{ > + Ghcb->SaveArea.SwExitCode =3D ExitCode; > + Ghcb->SaveArea.SwExitInfo1 =3D ExitInfo1; > + Ghcb->SaveArea.SwExitInfo2 =3D ExitInfo2; > + > + // > + // Guest memory is used for the guest-hypervisor communication, so > + fence // the invocation of the VMGEXIT instruction to ensure GHCB > + accesses are // synchronized properly. > + // > + MemoryFence (); > + AsmVmgExit (); > + MemoryFence (); > + > + return VmgExitErrorCheck (Ghcb); > +} > + > +/** > + Perform pre-VMGEXIT initialization/preparation. > + > + Performs the necessary steps in preparation for invoking VMGEXIT. > + Must be called before setting any fields within the GHCB. > + > + @param[in, out] Ghcb A pointer to the GHCB > + > +**/ > +VOID > +EFIAPI > +VmgInit ( > + IN OUT GHCB *Ghcb > + ) > +{ > + SetMem (&Ghcb->SaveArea, sizeof (Ghcb->SaveArea), 0); } > + > +/** > + Perform post-VMGEXIT cleanup. > + > + Performs the necessary steps to cleanup after invoking VMGEXIT. Must > + be called after obtaining needed fields within the GHCB. > + > + @param[in, out] Ghcb A pointer to the GHCB > + > +**/ > +VOID > +EFIAPI > +VmgDone ( > + IN OUT GHCB *Ghcb > + ) > +{ > +} > + > +/** > + Perform VMGEXIT MMIO read or write. > + > + Performs the requested MMIO read or write using the VMGEXIT > instruction. > + > + For an MMIO read, the data that has been read during the VMGEXIT is > + placed in the SharedBuffer area of the GHCB. This is then copied to > + the actual destination buffer within the guest. > + > + For an MMIO write, the data to be written is copied into the > + SharedBuffer area of the GHCB by the guest. This is then copied to > + the actual destination buffer by the hypervisor during the VMGEXIT. > + > + @param[in, out] MmioAddress A pointer to the MMIO buffer to be > read/written > + @param[in, out] Buffer A pointer to the buffer to hold the data= thas > + has been read or hold the data to be wri= tten > + @param[in] Bytes Number of bytes to read or write > + @param[in] Write If set, the request is for an MMIO write= , else > + it is an MMIO read. > + > + @retval 0 VMGEXIT succeeded. > + @retval Others VMGEXIT processing did not succeed. Exce= ption > + number to be propagated. > + > +**/ > +STATIC > +UINT64 > +EFIAPI > +VmgMmio ( > + IN OUT UINT8 *MmioAddress, > + IN OUT UINT8 *Buffer, > + IN UINTN Bytes, > + IN BOOLEAN Write > + ) > +{ > + UINT64 MmioOp, ExitInfo1, ExitInfo2, Status; > + GHCB *Ghcb; > + MSR_SEV_ES_GHCB_REGISTER Msr; > + > + Msr.GhcbPhysicalAddress =3D AsmReadMsr64 (MSR_SEV_ES_GHCB); Ghcb > =3D > + Msr.Ghcb; > + > + // > + // This function is about to set fields in the GHCB. Do not execute > + // anything that will cause a #VC before issuing the VmgExit(). Any > + #VC // will result in all GHCB settings being overwritten (this > + means, e.g., // do not add DEBUG() statements). > + // > + VmgInit (Ghcb); > + > + if (Write) { > + MmioOp =3D SvmExitMmioWrite; > + } else { > + MmioOp =3D SvmExitMmioRead; > + } > + > + ExitInfo1 =3D (UINT64) (UINTN) MmioAddress; > + ExitInfo2 =3D Bytes; > + > + if (Write) { > + CopyMem (Ghcb->SharedBuffer, Buffer, Bytes); } > + > + Ghcb->SaveArea.SwScratch =3D (UINT64) (UINTN) Ghcb->SharedBuffer; > + Status =3D VmgExit (Ghcb, MmioOp, ExitInfo1, ExitInfo2); if (Status != =3D > + 0) { > + return Status; > + } > + > + if (!Write) { > + CopyMem (Buffer, Ghcb->SharedBuffer, Bytes); } > + > + VmgDone (Ghcb); > + > + return 0; > +} > + > +/** > + Perform MMIO write of a buffer to a non-MMIO marked range. > + > + Performs an MMIO write without taking a #VC. This is useful for > + Flash devices, which are marked read-only. > + > + @param[in, out] Dest A pointer to the destination buffer > + @param[in] Src A pointer to the source data to be written > + @param[in] Bytes Number of bytes to write > + > +**/ > +VOID > +EFIAPI > +VmgMmioWrite ( > + IN OUT UINT8 *Dest, > + IN UINT8 *Src, > + IN UINTN Bytes > + ) > +{ > + VmgMmio (Dest, Src, Bytes, TRUE); > +} > + > +/** > + Issue the GHCB set AP Jump Table VMGEXIT. > + > + Performs a VMGEXIT using the GHCB AP Jump Table exit code to save the > + AP Jump Table address with the hypervisor for retrieval at a later time= . > + > + @param[in] Address Physical address of the AP Jump Table > + > + @retval 0 VMGEXIT succeeded. > + @retval Others VMGEXIT processing did not succeed. Exception > + number to be propagated. > + > +**/ > +UINT64 > +EFIAPI > +VmgExitSetAPJumpTable ( > + IN EFI_PHYSICAL_ADDRESS Address > + ) > +{ > + UINT64 ExitInfo1, ExitInfo2, Status; > + GHCB *Ghcb; > + MSR_SEV_ES_GHCB_REGISTER Msr; > + > + Msr.GhcbPhysicalAddress =3D AsmReadMsr64 (MSR_SEV_ES_GHCB); Ghcb > =3D > + Msr.Ghcb; > + > + VmgInit (Ghcb); > + > + ExitInfo1 =3D 0; > + ExitInfo2 =3D (UINT64) (UINTN) Address; > + > + Status =3D VmgExit (Ghcb, SvmExitApJumpTable, ExitInfo1, ExitInfo2); > + > + VmgDone (Ghcb); > + > + return Status; > +} > + > diff --git a/UefiCpuPkg/Library/VmgExitLib/VmgExitLib.uni > b/UefiCpuPkg/Library/VmgExitLib/VmgExitLib.uni > new file mode 100644 > index 000000000000..e8656aae4726 > --- /dev/null > +++ b/UefiCpuPkg/Library/VmgExitLib/VmgExitLib.uni > @@ -0,0 +1,15 @@ > +// /** @file > +// VMGEXIT support library instance. > +// > +// VMGEXIT support library instance. > +// > +// Copyright (c) 2019, Advanced Micro Devices, Inc. All rights > +reserved.
// SPDX-License-Identifier: BSD-2-Clause-Patent // // **/ > + > + > +#string STR_MODULE_ABSTRACT #language en-US "VMGEXIT > Support Library." > + > +#string STR_MODULE_DESCRIPTION #language en-US "VMGEXIT > Support Library." > + > -- > 2.17.1