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.71]) by mx.groups.io with SMTP id smtpd.web12.9725.1597325121015035436 for ; Thu, 13 Aug 2020 06:25:21 -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=E8tV3EyR; spf=none, err=SPF record not found (domain: amd.com, ip: 40.107.220.71, mailfrom: thomas.lendacky@amd.com) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=PdfWMS3Sght7ynfumGMbC/ZwY9iAMbmq0q0M6qbaoD4XG/yoay5JQkN5FvJC5LAE77U2Sp0uJkOyTiTdPQoOMr0ZzyJNd68Bf8y0KSQG/oMsOvFk+AAUjlu+5plbq8e942CkpMjVs3w9pZpitc9pHo5oNBvZZTsVknSi57V5OQSSLT4htnLqODnPgEdY5ke5k3VWAT5vG7R8Vs8qopoTk44GV1eHNHDihXGTiKSiBMqC0utb2sQGcy3pFo/ZDK4eOxZmFiaBd+Gj62r4IaCRrCliUko2UqObLSJZRiv+eBnI+MJTAHukFLZm3IKQG9bf6Qo9jkbBApBTN9VbOH0ueQ== 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=ik9L+z4l6sIwqpUN3Bh5/I3nn9iNNA42ntxBIiLn74g=; b=Se87TAlUNZ4Wall3kD+5pu94b9UTW28U6Yl9o4yHsOFYGHVmi5WsT84oMsG8xHIDIqIMNjVhQfHZaLNyV82kxVR6KUqWXG/diK2K4CXJC2nCuJCT9/a0WY2LPGBjmd8KJb65apyIYbD27YA9Q3YMd1DTsE+LHcPrtCsfHjqJPJ0WK+F4iucRKnOlQCmbS+aeQ5dV9tjb1gbVwof4Krox8Pvvt9y8aTJq1a55dUHOqOzdhMEh+aBxQ615cnZfOIL3A4IW4FAoMMQ+SRm/iYDtuojjIgt1+AgK3lzVf9d4TB8U9DUQOUgRSJypUuZC59rBjWRQPHlVNziNsKza5j7Syg== 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=ik9L+z4l6sIwqpUN3Bh5/I3nn9iNNA42ntxBIiLn74g=; b=E8tV3EyR/SOAAqtW4O81f2RgFV3deXqLOHOhyrx2+ojlwVEjzM9Y/HrZvJABtSEzNsBd3fmtmaAGcah89+XJiebez42I7AkXKO0MDfVnUA3yFdClRazH+DNET+6H5TbN2gBomglXmfes/QQaLbV60sUGhqG76HfWTM2VYQQi4Dk= 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:25:19 +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:25:19 +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 12/46] OvmfPkg/VmgExitLib: Implement library support for VmgExitLib in OVMF Date: Thu, 13 Aug 2020 08:22:59 -0500 Message-ID: X-Mailer: git-send-email 2.28.0 In-Reply-To: References: X-ClientProxiedBy: DM6PR01CA0021.prod.exchangelabs.com (2603:10b6:5:296::26) 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 DM6PR01CA0021.prod.exchangelabs.com (2603:10b6:5:296::26) 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:25:18 +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: 81d1c6c2-c96a-413d-4c08-08d83f8c5305 X-MS-TrafficTypeDiagnostic: DM5PR1201MB0026: X-MS-Exchange-Transport-Forked: True X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:10000; X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: YY2gICcy4gsmLWmt3F6URQW2pflcIMjv08ueiCMom7qATKUqYaNweNimADTMYGqTNiKUEHIaeqc8C3ps43CkZfEKoLWVDHU4qj2+M8gwPvOyFrk5ERk0nLvHMLi/8OH2jbaV91+CLG2RvRvb6cCGrI6OCLzpTMIgp3PflrLPhIaCIw0BuMONNVYQa0fLwx0Kazj1QD5PlO0tPX8Bso1snedj12icCukk1QPAMo83iRG9KffPFGukxtK/ehZ7GypqBeWawlC7K23G0bFo+tcEmwroB8emplBbk9SyPhkh+nJ+TurRDhI4aRB/NmYW7LFycmNHMzj9BDM69mlDvEWj4l538HjqXnwDx8loGLJGecUAXttJzsEKfSU6rkYdWcyA 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)(83380400001)(54906003)(8676002)(6486002)(2906002)(6666004)(6916009)(956004)(2616005)(4326008)(66476007)(26005)(52116002)(7696005)(66556008)(16526019)(36756003)(19627235002)(478600001)(66946007)(86362001)(186003)(136400200001);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData: jUeKbIGo6CufVB/GJEzm9UoHOPkN+KgP4IaPmpkkN9Zywd9QNFiQetB6lNeC+1WF+ZCvlL49xINffbNG2p5gN9GDf+1b+CtxawgBcLSiiLkgKYdMJUf3eKj0nRmLYJY/VlGXt+MOwEnuTECPGqtsKu9HQqeAnM/Hr0b8OVMeXOywa5jjrK6kT/XPJApx11k//ElNVj8gkH8jYVa9eLTKCIhWTR/maV9ZvWMBv4L08efnyT0dUmVUd41wfOkZ6McbgsRc8Hl93oVrqJflRem8DHH/hutiflrecfDD3K+tMLOI12C3xas6egveeUPiAxtxqL4L68yftMXxZnbP8B9WH7OW48uX9zIKwa9WIpezRP5bU3j7KZeqO2rP2vnKXpDwhk6M1Xa6/iC3EURquMDsAKAhoCGznmzSTCJyEN+S6htBtrtQocVqEbPis7T9BBhFSWFg3pI+QUa1FqgfYGeWLWtcNGVqqEkfCmyvuJyBnqvekQAW6CVXVC8rJJ94UQgyx08l+XSncMsaOebGI8ljHX4OGflBxyX7jew0RjMewfwtPFIZsvaH/xA+Q5Ksb1jIP/O416KVRpFHaAsQLxPJVUUmetKeN9VBApPp6VyMz/mrwNYkTihs1pIdMYG8K0XsNnCeGGs7Z38/s0VAyLzgBQ== X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-Network-Message-Id: 81d1c6c2-c96a-413d-4c08-08d83f8c5305 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:25:19.6641 (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: /WSS1BedPShQAhpxXKo5Hj+rSK+oLW02tHLa2fqggYWDCr9GAFCzHZzZVcS/2aciiESJV/XKDCPL/8xCUtao9w== X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM5PR1201MB0026 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: quoted-printable From: Tom Lendacky The base VmgExitLib library provides a default limited interface. As it does not provide full support, create an OVMF version of this library to begin the process of providing full support of SEV-ES within OVMF. SEV-ES support is only provided for X64 builds, so only OvmfPkgX64.dsc is updated to make use of the OvmfPkg version of the library. Cc: Jordan Justen Cc: Laszlo Ersek Cc: Ard Biesheuvel Acked-by: Laszlo Ersek Signed-off-by: Tom Lendacky --- OvmfPkg/OvmfPkgX64.dsc | 2 +- OvmfPkg/Library/VmgExitLib/VmgExitLib.inf | 36 +++++ OvmfPkg/Library/VmgExitLib/VmgExitLib.c | 159 ++++++++++++++++++++ OvmfPkg/Library/VmgExitLib/VmgExitVcHandler.c | 81 ++++++++++ 4 files changed, 277 insertions(+), 1 deletion(-) diff --git a/OvmfPkg/OvmfPkgX64.dsc b/OvmfPkg/OvmfPkgX64.dsc index 27f5225fc281..60be5eae3d2b 100644 --- a/OvmfPkg/OvmfPkgX64.dsc +++ b/OvmfPkg/OvmfPkgX64.dsc @@ -233,7 +233,7 @@ [LibraryClasses] =20 [LibraryClasses.common] BaseCryptLib|CryptoPkg/Library/BaseCryptLib/BaseCryptLib.inf - VmgExitLib|UefiCpuPkg/Library/VmgExitLibNull/VmgExitLibNull.inf + VmgExitLib|OvmfPkg/Library/VmgExitLib/VmgExitLib.inf =20 [LibraryClasses.common.SEC] TimerLib|OvmfPkg/Library/AcpiTimerLib/BaseRomAcpiTimerLib.inf diff --git a/OvmfPkg/Library/VmgExitLib/VmgExitLib.inf b/OvmfPkg/Library/Vm= gExitLib/VmgExitLib.inf new file mode 100644 index 000000000000..d003ac63173e --- /dev/null +++ b/OvmfPkg/Library/VmgExitLib/VmgExitLib.inf @@ -0,0 +1,36 @@ +## @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 VmgExitLib + FILE_GUID =3D 0e923c25-13cd-430b-8714-ffe85652a97b + 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 X64 +# + +[Sources.common] + VmgExitLib.c + VmgExitVcHandler.c + +[Packages] + MdePkg/MdePkg.dec + OvmfPkg/OvmfPkg.dec + UefiCpuPkg/UefiCpuPkg.dec + +[LibraryClasses] + BaseLib + BaseMemoryLib + DebugLib + diff --git a/OvmfPkg/Library/VmgExitLib/VmgExitLib.c b/OvmfPkg/Library/VmgE= xitLib/VmgExitLib.c new file mode 100644 index 000000000000..53040cc6f649 --- /dev/null +++ b/OvmfPkg/Library/VmgExitLib/VmgExitLib.c @@ -0,0 +1,159 @@ +/** @file + VMGEXIT Support Library. + + Copyright (C) 2020, 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 VM= GEXIT + by examining the SwExitInfo1 field of the GHCB. + + @param[in] Ghcb A pointer to the GHCB + + @retval 0 VMGEXIT succeeded. + @return Exception number to be propagated, VMGEXIT proces= sing + did not succeed. + +**/ +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 GpEvent; + + GpEvent.Uint64 =3D 0; + GpEvent.Elements.Vector =3D GP_EXCEPTION; + GpEvent.Elements.Type =3D GHCB_EVENT_INJECTION_TYPE_EXCEPTION; + GpEvent.Elements.Valid =3D 1; + + Status =3D GpEvent.Uint64; + } + + return Status; +} + +/** + 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 + ) +{ + 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 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 + ) +{ + 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 + ) +{ +} + diff --git a/OvmfPkg/Library/VmgExitLib/VmgExitVcHandler.c b/OvmfPkg/Librar= y/VmgExitLib/VmgExitVcHandler.c new file mode 100644 index 000000000000..b6a955ed8088 --- /dev/null +++ b/OvmfPkg/Library/VmgExitLib/VmgExitVcHandler.c @@ -0,0 +1,81 @@ +/** @file + X64 #VC Exception Handler functon. + + Copyright (C) 2020, Advanced Micro Devices, Inc. All rights reserved. + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include +#include +#include +#include +#include + +/** + Handle a #VC exception. + + Performs the necessary processing to handle a #VC exception. + + @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 + ) +{ + MSR_SEV_ES_GHCB_REGISTER Msr; + EFI_SYSTEM_CONTEXT_X64 *Regs; + GHCB *Ghcb; + UINT64 ExitCode, Status; + EFI_STATUS VcRet; + + VcRet =3D EFI_SUCCESS; + + Msr.GhcbPhysicalAddress =3D AsmReadMsr64 (MSR_SEV_ES_GHCB); + ASSERT (Msr.GhcbInfo.Function =3D=3D 0); + ASSERT (Msr.Ghcb !=3D 0); + + Regs =3D SystemContext.SystemContextX64; + Ghcb =3D Msr.Ghcb; + + VmgInit (Ghcb); + + ExitCode =3D Regs->ExceptionData; + switch (ExitCode) { + default: + Status =3D VmgExit (Ghcb, SVM_EXIT_UNSUPPORTED, ExitCode, 0); + if (Status =3D=3D 0) { + Regs->ExceptionData =3D 0; + *ExceptionType =3D GP_EXCEPTION; + } else { + GHCB_EVENT_INJECTION Event; + + Event.Uint64 =3D Status; + if (Event.Elements.ErrorCodeValid !=3D 0) { + Regs->ExceptionData =3D Event.Elements.ErrorCode; + } else { + Regs->ExceptionData =3D 0; + } + + *ExceptionType =3D Event.Elements.Vector; + } + + VcRet =3D EFI_PROTOCOL_ERROR; + } + + VmgDone (Ghcb); + + return VcRet; +} --=20 2.28.0