From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from NAM11-CO1-obe.outbound.protection.outlook.com (NAM11-CO1-obe.outbound.protection.outlook.com [40.107.220.46]) by mx.groups.io with SMTP id smtpd.web10.9787.1597325089627685795 for ; Thu, 13 Aug 2020 06:24:49 -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=BcaUScJ1; spf=none, err=SPF record not found (domain: amd.com, ip: 40.107.220.46, mailfrom: thomas.lendacky@amd.com) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=JjCPIcznFJKqi6484H3+x8kaSVVbWgQHd0Aprm8KA6iicUSVfjIJMOTVxQKmMwxr1kmVITmdhL9fzT9PEokLXnQHNBUz1lCMqazLee5eZjihsh+GCBnjIuNC+ii9eHAgbuoqCutzREhTMw3piSNLDwaJf55j7ZsjxOJCSG1oCMWc+7GE6FnsHoR3QK4BbvDD7YngOZbCdNLuWJoFQDhcJ2VWxf/LzWjL8FZb7ntYbsaxkQ1nifGOti8wvGX1IiXYTg1wKIpIgC8RdG2m5bQughUMP58h7IruJH3dSBqIq7YXQ2Xt7GptZoE9u2uaWAfSjY7U3oqG3PyumK/BTiz/4w== 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=eZC2AeLcLu+jnX1I/7s8jle3ua3q6wHqmmvaQDch97U=; b=N3tFcIIzjffQauZvHOXwwo8EmGnjV+79IIHLoyDjlQaEfc0zZ0jnXTBpDo+wVQcVb0aY6RWQDj6QxMew1n9MSVOJDFBqTYw0/yW2sH2FR6zIwZ77U+8nba30FZBkj2AQHslOkuzXo/t6gQo94CmxBqyGom1obuv416pHL+X+/JPPTDMxrFHM0BWXuDfvWfRr5W4y8AobpRCMtMAd0y7MIJGQo1wmbUkjySfG0qoeKGOnzs0E87uIur6AfBooWa8aQLc7nkvEOpIxvhwve4blMI60w3GWutVfVRhE6r/qJDwykzFk54LXEAqp0xT1r6KEW2rm0z0hsjar+e0FocR6VA== 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=eZC2AeLcLu+jnX1I/7s8jle3ua3q6wHqmmvaQDch97U=; b=BcaUScJ11bYKGC7QbM2ZDXr3v+xXiXlxaI4nhKif5FHDYBIIQZnrUR6r2M5NivYZRsVbftc4nS+1Bw1Lt69fFHFzWHrjer8OftBqr4BTuo6dRtGIOjzF6dU9//ROOgE+EPzKMcBfQ/gXV6+ciee3egRwgJUT+y9bqUL87QF2TiI= 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 DM5PR1201MB0026.namprd12.prod.outlook.com (2603:10b6:4:52::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3261.20; Thu, 13 Aug 2020 13:24:48 +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.3261.025; Thu, 13 Aug 2020 13:24:48 +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 v15 08/46] UefiCpuPkg: Implement library support for VMGEXIT Date: Thu, 13 Aug 2020 08:22:55 -0500 Message-ID: X-Mailer: git-send-email 2.28.0 In-Reply-To: References: X-ClientProxiedBy: DM5PR18CA0087.namprd18.prod.outlook.com (2603:10b6:3:3::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 DM5PR18CA0087.namprd18.prod.outlook.com (2603:10b6:3:3::25) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3283.16 via Frontend Transport; Thu, 13 Aug 2020 13:24:47 +0000 X-Mailer: git-send-email 2.28.0 X-Originating-IP: [165.204.77.1] X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-HT: Tenant X-MS-Office365-Filtering-Correlation-Id: 67d7bd04-404a-401f-3b3e-08d83f8c4044 X-MS-TrafficTypeDiagnostic: DM5PR1201MB0026: X-MS-Exchange-Transport-Forked: True X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:8882; X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: YwqBrn+tWFxFQZE6wDeQAwDdFyGpENZy5khsS5buM9hnGhqadFH6Rt9/zMDm9EYfyiQJS28tBXb0ei3RSQBFyXB2Bv+/Kb+Ib1sBoSEufqy3rpxJw0RPKm6i2xq1SBuO5jy9IcLZFMMgPGLaUeISrtPVQPjiaSKgGUXNbcrgRztiep4U86V6wtjB5qasODTi+wMvJtE6IfELRrxuXJvMurjgzXxyz4ttBSDREzPIKlCQVUaubpI/GI/b134hXbdZc6wq93Xz03meQNurpWe3OEhfnAtHEC8V4nDE/sD0sTZMtUHkBjapYnteAZESRK4TE4+vKkpXByf/qX2+1KF8/wP6KVdst/LaV92+Pst7fPMSwURqVejh0uP5y/QBI23TjUlMxqC9nH0ZtBeC3gjtIi61zKgz4L+O1Wn9qQquzpLhEsjk7VkyL5r8Yfof0YqtmyiRhYGbU43Fwt2cNVA0Cg== 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)(136003)(346002)(396003)(366004)(39860400002)(316002)(8936002)(5660300002)(30864003)(54906003)(8676002)(6486002)(2906002)(6666004)(6916009)(956004)(2616005)(4326008)(66476007)(26005)(52116002)(7696005)(66556008)(16526019)(36756003)(966005)(478600001)(66946007)(86362001)(186003)(136400200001);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData: OWGKTnWeRmP4GJ/3K0wUmALQIi2LlRTjzc3gqmQZwgIpI4fVfNT5JfrQ5XB/V4JMKcLOwIg7lVxwB1puW8u7hJpvoN4mdMPWgwTM8eqa65P+8dMgL7hOuXIFrU9hKNLd2H6Ds8Xd9GPI6fe0wZ7YzTDvdcD+dTOkHTzivTP5omrdT+RdKKvKTzUlEtm4up21yvm/N/iLUoggdHC3+7lyO7J4O2tulPEuOCQpC2AzpWu/CL5ojhi3vz7U6j79DdSu56tEDfAWU78CoCUhO+cUnIXn9enfLksyId51UjJEZ4RNBTt4YofCh4oW0tdeeg0RnAMnd51yZ04fX64jjEF9LLC+XDuxnyXV6VtrF9YFo98KxFJ3Y/NMyMQZEKj2g/Ed8IzC7etybLtkCTwCAjvdgrs/Nd9R7kEUn+4OorOtQUJA6ht5tKyOPHP0BhEU3MESXPkzwa7wQN/LLDNDaHqdiUQOVcApAwXJYW/vsPBzZiVg0wGcjfdmSB95BIaGKsuihkCSL4glWRKtpiPVreFKPS/76MdtiazNg+iUpTVNPdGPe8kqaO93d8HTGwe+xmgflqB+BUG4jq210MSZ4kPHFwZYjrO6tMz79ltfP6Mk0Hy7w8wF2ir3BfAoJnANl52pFlpTniFt5QSmtSS5IMLPeQ== X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-Network-Message-Id: 67d7bd04-404a-401f-3b3e-08d83f8c4044 X-MS-Exchange-CrossTenant-AuthSource: DM5PR12MB1355.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 13 Aug 2020 13:24:48.1249 (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: nYuLwxYt+dNzz7rVHB0ahFfL3AKQzcMSLMQ90rYjiRD0hFQfAXuEyRqKp8Apr5Bmvrpmj7M90oI1+vtBuiOeEg== X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM5PR1201MB0026 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 To support handling #VC exceptions and issuing VMGEXIT instructions, create a library with functions that can be used to perform these #VC/VMGEXIT related operations. This includes functions for: - Handling #VC exceptions - Preparing for and issuing a VMGEXIT - Performing MMIO-related write operations to support flash emulation - Performing AP related boot opeations The base functions in this driver will not do anything and will return an error if a return value is required. It is expected that other packages (like OvmfPkg) will create a version of the library to fully support an SEV-ES guest. Cc: Eric Dong Cc: Ray Ni Cc: Laszlo Ersek Reviewed-by: Eric Dong Signed-off-by: Tom Lendacky --- UefiCpuPkg/UefiCpuPkg.dec | 3 + UefiCpuPkg/UefiCpuPkg.dsc | 2 + UefiCpuPkg/Library/VmgExitLibNull/VmgExitLibNull.inf | 27 +++++ UefiCpuPkg/Include/Library/VmgExitLib.h | 103 +++++++++++++++= ++ UefiCpuPkg/Library/VmgExitLibNull/VmgExitLibNull.c | 121 +++++++++++++++= +++++ UefiCpuPkg/Library/VmgExitLibNull/VmgExitLibNull.uni | 15 +++ 6 files changed, 271 insertions(+) diff --git a/UefiCpuPkg/UefiCpuPkg.dec b/UefiCpuPkg/UefiCpuPkg.dec index d52912ddca1f..17228cb5a84f 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 964720048dd7..b2b6d78a71b0 100644 --- a/UefiCpuPkg/UefiCpuPkg.dsc +++ b/UefiCpuPkg/UefiCpuPkg.dsc @@ -56,6 +56,7 @@ [LibraryClasses] PeCoffGetEntryPointLib|MdePkg/Library/BasePeCoffGetEntryPointLib/BasePeC= offGetEntryPointLib.inf PeCoffExtraActionLib|MdePkg/Library/BasePeCoffExtraActionLibNull/BasePeC= offExtraActionLibNull.inf TpmMeasurementLib|MdeModulePkg/Library/TpmMeasurementLibNull/TpmMeasurem= entLibNull.inf + VmgExitLib|UefiCpuPkg/Library/VmgExitLibNull/VmgExitLibNull.inf =20 [LibraryClasses.common.SEC] PlatformSecLib|UefiCpuPkg/Library/PlatformSecLibNull/PlatformSecLibNull.= inf @@ -143,6 +144,7 @@ [Components.IA32, Components.X64] UefiCpuPkg/Library/SmmCpuPlatformHookLibNull/SmmCpuPlatformHookLibNull.i= nf UefiCpuPkg/Library/SmmCpuFeaturesLib/SmmCpuFeaturesLib.inf UefiCpuPkg/Library/SmmCpuFeaturesLib/SmmCpuFeaturesLibStm.inf + UefiCpuPkg/Library/VmgExitLibNull/VmgExitLibNull.inf UefiCpuPkg/PiSmmCommunication/PiSmmCommunicationPei.inf UefiCpuPkg/PiSmmCommunication/PiSmmCommunicationSmm.inf UefiCpuPkg/SecCore/SecCore.inf diff --git a/UefiCpuPkg/Library/VmgExitLibNull/VmgExitLibNull.inf b/UefiCpu= Pkg/Library/VmgExitLibNull/VmgExitLibNull.inf new file mode 100644 index 000000000000..d8770a21c355 --- /dev/null +++ b/UefiCpuPkg/Library/VmgExitLibNull/VmgExitLibNull.inf @@ -0,0 +1,27 @@ +## @file +# VMGEXIT Support Library. +# +# Copyright (C) 2020, Advanced Micro Devices, Inc. All rights reserved. +# SPDX-License-Identifier: BSD-2-Clause-Patent +# +## + +[Defines] + INF_VERSION =3D 0x00010005 + BASE_NAME =3D VmgExitLibNull + MODULE_UNI_FILE =3D VmgExitLibNull.uni + FILE_GUID =3D 3cd7368f-ef9b-4a9b-9571-2ed93813677e + MODULE_TYPE =3D BASE + VERSION_STRING =3D 1.0 + LIBRARY_CLASS =3D VmgExitLib + +[Sources.common] + VmgExitLibNull.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..45fc27d35e29 --- /dev/null +++ b/UefiCpuPkg/Include/Library/VmgExitLib.h @@ -0,0 +1,103 @@ +/** @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 and to handle #VC exceptions. + + Copyright (C) 2020, 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 +#include + + +/** + Perform VMGEXIT. + + Sets the necessary fields of the GHCB, invokes the VMGEXIT instruction a= nd + then handles the return actions. + + @param[in, out] Ghcb A pointer to the GHCB + @param[in] ExitCode VMGEXIT code to be assigned to the SwExitCod= e + 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. + @return Exception number to be propagated, VMGEXIT + processing did not succeed. + +**/ +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 b= e + 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 + ); + +/** + Handle a #VC exception. + + Performs the necessary processing to handle a #VC exception. + + The base library function returns an error equal to VC_EXCEPTION, + to be propagated to the standard exception handling stack. + + @param[in, out] ExceptionType Pointer to an EFI_EXCEPTION_TYPE to be s= et + as value to use on error. + @param[in, out] SystemContext Pointer to EFI_SYSTEM_CONTEXT + + @retval EFI_SUCCESS Exception handled + @retval EFI_UNSUPPORTED #VC not supported, (new) exception value= to + propagate provided + @retval EFI_PROTOCOL_ERROR #VC handling failed, (new) exception val= ue to + propagate provided + +**/ +EFI_STATUS +EFIAPI +VmgExitHandleVc ( + IN OUT EFI_EXCEPTION_TYPE *ExceptionType, + IN OUT EFI_SYSTEM_CONTEXT SystemContext + ); + +#endif diff --git a/UefiCpuPkg/Library/VmgExitLibNull/VmgExitLibNull.c b/UefiCpuPk= g/Library/VmgExitLibNull/VmgExitLibNull.c new file mode 100644 index 000000000000..bb265e1700d2 --- /dev/null +++ b/UefiCpuPkg/Library/VmgExitLibNull/VmgExitLibNull.c @@ -0,0 +1,121 @@ +/** @file + VMGEXIT Base Support Library. + + Copyright (C) 2020, Advanced Micro Devices, Inc. All rights reserved. + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include +#include +#include + +/** + Perform VMGEXIT. + + Sets the necessary fields of the GHCB, invokes the VMGEXIT instruction a= nd + then handles the return actions. + + The base library function returns an error in the form of a + GHCB_EVENT_INJECTION representing a GP_EXCEPTION. + + @param[in, out] Ghcb A pointer to the GHCB + @param[in] ExitCode VMGEXIT code to be assigned to the SwExitCod= e + 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. + @return Exception number to be propagated, VMGEXIT + processing did not succeed. + +**/ +UINT64 +EFIAPI +VmgExit ( + IN OUT GHCB *Ghcb, + IN UINT64 ExitCode, + IN UINT64 ExitInfo1, + IN UINT64 ExitInfo2 + ) +{ + 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; + + return Event.Uint64; +} + +/** + Perform pre-VMGEXIT initialization/preparation. + + Performs the necessary steps in preparation for invoking VMGEXIT. Must b= e + called before setting any fields within the GHCB. + + The base library function does nothing. + + @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. + + The base library function does nothing. + + @param[in, out] Ghcb A pointer to the GHCB + +**/ +VOID +EFIAPI +VmgDone ( + IN OUT GHCB *Ghcb + ) +{ +} + +/** + Handle a #VC exception. + + Performs the necessary processing to handle a #VC exception. + + The base library function returns an error equal to VC_EXCEPTION, + to be propagated to the standard exception handling stack. + + @param[in, out] ExceptionType Pointer to an EFI_EXCEPTION_TYPE to be s= et + as value to use on error. + @param[in, out] SystemContext Pointer to EFI_SYSTEM_CONTEXT + + @retval EFI_SUCCESS Exception handled + @retval EFI_UNSUPPORTED #VC not supported, (new) exception value= to + propagate provided + @retval EFI_PROTOCOL_ERROR #VC handling failed, (new) exception val= ue to + propagate provided + +**/ +EFI_STATUS +EFIAPI +VmgExitHandleVc ( + IN OUT EFI_EXCEPTION_TYPE *ExceptionType, + IN OUT EFI_SYSTEM_CONTEXT SystemContext + ) +{ + *ExceptionType =3D VC_EXCEPTION; + + return EFI_UNSUPPORTED; +} diff --git a/UefiCpuPkg/Library/VmgExitLibNull/VmgExitLibNull.uni b/UefiCpu= Pkg/Library/VmgExitLibNull/VmgExitLibNull.uni new file mode 100644 index 000000000000..8639bc0e8ce9 --- /dev/null +++ b/UefiCpuPkg/Library/VmgExitLibNull/VmgExitLibNull.uni @@ -0,0 +1,15 @@ +// /** @file +// VMGEXIT support library instance. +// +// VMGEXIT support library instance. +// +// Copyright (C) 2020, Advanced Micro Devices, Inc. All rights reserved. +// SPDX-License-Identifier: BSD-2-Clause-Patent +// +// **/ + + +#string STR_MODULE_ABSTRACT #language en-US "VMGEXIT support N= ULL library instance" + +#string STR_MODULE_DESCRIPTION #language en-US "VMGEXIT support N= ULL library instance." + --=20 2.28.0