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.83]) by mx.groups.io with SMTP id smtpd.web11.960.1589925103650720677 for ; Tue, 19 May 2020 14:51:43 -0700 Authentication-Results: mx.groups.io; dkim=pass header.i=@amdcloud.onmicrosoft.com header.s=selector2-amdcloud-onmicrosoft-com header.b=a9LwU1Rz; spf=none, err=SPF record not found (domain: amd.com, ip: 40.107.220.83, mailfrom: thomas.lendacky@amd.com) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=Hni1piqDU/p1BFpSbX4islTbSW4G41g+9JIVWXJqbwx2KKP9n61ovGYpOvkzLH8mA2ZxpDOrWVmbmrQwjM6yE+z9y/gA3HOBukBwPPZJFehT3Ri0e6htjxYpUxomhj6oqiNKPLYCUplgGWJdSTNvav+lwV7+WaGkO0asXtDCZBFED8HBHaxXsEwsLH8WYI4STXtjTR5WArq+HP/3rFcgnTPuGXK+RGG30IGd7Eb4p3038YZ9ckE6dNtTakrhM7KRmFcyAlX9olE9DAw4m3UuwQEcn7wY8aHfoIPAG1JXBJuGDQfUkLy1OFUAW5NiSWugSqzi0LpC3Rru7GVeywotxw== 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=GMAaxZ8iyWh1OhrY9VuBCouZWkTWtrYqv9hp1ZRhXeU=; b=CejVzqoG8J9QNHxhqndOEnTqz5miG6QHkm+T4OlTbOkhfEkyYkBQH/7+Xzr73hGo/bceWItSAPplJk12dTZa68qYmLZoQvbZID9dpyY3zht1ZCj+6Xj1bj9W36m7gynoUipTy7yQ39cifXWqz4MM0Xa0HQ0mFS8Oj09C3jsm6A88rxUSTBtb4yGmecZDwu5GHRHYCSCzU5vTRMeS+q2465VBtmDlpQj2GUbZBWUoXK929a42JSj8F4ddRoIjV16Kw7eVF7RESq1SEopZ5GcbON2FsUMa8UeoGH1mPohRbLKZAk54I0356C/C0X4BhZR0duU7Dp6bonHPLSyquo6zAw== 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=GMAaxZ8iyWh1OhrY9VuBCouZWkTWtrYqv9hp1ZRhXeU=; b=a9LwU1RzxEbBxlQIzxus3VISyPDkFjlwnERkyClJhcbQaOFix9L+myGStAqlWBZ2+/RtydQH6aOrLi0uPznC75/uXT/Y24tQHgoTz0PT0tTqt1QHXUgFDecAbD/MSdfwx1A/6M8DrhKYY9EtA34hLHSy/UzUdtlW33xsn7vhwyk= 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 DM5PR12MB2504.namprd12.prod.outlook.com (2603:10b6:4:b5::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3000.20; Tue, 19 May 2020 21:51:41 +0000 Received: from DM5PR12MB1355.namprd12.prod.outlook.com ([fe80::4ce1:9947:9681:c8b1]) by DM5PR12MB1355.namprd12.prod.outlook.com ([fe80::4ce1:9947:9681:c8b1%10]) with mapi id 15.20.3000.034; Tue, 19 May 2020 21:51:41 +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 , Brijesh Singh , Ard Biesheuvel Subject: [PATCH v8 12/46] OvmfPkg/VmgExitLib: Implement library support for VmgExitLib in OVMF Date: Tue, 19 May 2020 16:50:40 -0500 Message-Id: X-Mailer: git-send-email 2.17.1 In-Reply-To: References: X-ClientProxiedBy: DM6PR11CA0048.namprd11.prod.outlook.com (2603:10b6:5:14c::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 DM6PR11CA0048.namprd11.prod.outlook.com (2603:10b6:5:14c::25) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3000.25 via Frontend Transport; Tue, 19 May 2020 21:51:40 +0000 X-Mailer: git-send-email 2.17.1 X-Originating-IP: [165.204.77.1] X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-HT: Tenant X-MS-Office365-Filtering-Correlation-Id: 4199a42f-83e9-4125-b359-08d7fc3ed085 X-MS-TrafficTypeDiagnostic: DM5PR12MB2504: X-MS-Exchange-Transport-Forked: True X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:10000; X-Forefront-PRVS: 040866B734 X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: ePz4vm9gYcL9E96JlfXBN+LJOle73FxtRSE8BBd0MP6DMYhASZvztR0O+8aJ1XnembEoU0quXsa4K0erK2PCY/Wxn5GOu/AhGDPy49pSzJgU40N9Ni02bdCWesZhOupCLvUfLD8RYZ2bmsv/fnwALFuCRSfuutsibdcKSoU/E6VhLxwZhuISdxKAjo63LHNA+kM72EqFXB2+xp/1ImL9SoJz/OFTeyXluFA/YopFf6WVZiujf/BdzS02WYExEEvj9DI4HY0y01OjsIxsmPpuVq+H+yMfpr4p2/wjDj9sMa0AzOvEVCD6e3icXAhtMI1zuWSx9t0SU5NVgBTX4kG8r852qPEtAvSf+o7pDK++9Vc+1FSaiH9/APr9Dua5qZN9dYITG5DnbirsP1X/u5dyClwN1gqSCXZy+dn8tVryfm5B4O9q4o2zQQbc0OQ7hJ206J1I91FFxIUGY6iW8uujzy4ThGasw5zuT4yay3ZAQmmM1PvqzDItSHwYwCq8tvFX 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)(39860400002)(136003)(346002)(396003)(366004)(316002)(86362001)(2906002)(54906003)(7696005)(52116002)(2616005)(956004)(66946007)(66476007)(19627235002)(66556008)(36756003)(26005)(16526019)(186003)(4326008)(8676002)(5660300002)(6486002)(8936002)(6916009)(478600001)(136400200001);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData: YGw+8518fGD74RO9TeoZ4B59JHQdBFP+6C05tAPg9JDrybGqbY2yCyoS35pD8Npu0uAkJp5gr8e1i/7xPGwmEGhoNIRfQ3/Ext/MVdjNwNll+pikmuqGD6UM/8WDobvIzaOLejVhJsn8UuaK9hq8EnvWLUQ0L2bqOIn/DzNUu4IPGbaA4+W3IpmCkYCMzytiI+cGke1QuDWWtyEuHpN/9sYEZLzGseqDkUQBj63ANyo1mO8EC6E6inEva1IVgdOyTUO1H0YK9s+T/mlU2AKmgGb9xI+WdEJUocQlmdLrWH0kGIHgArT9sRAiocEhU2jtHunqy2wwZ7v94M6RSXRlNk3OU8w7qAP1epOfu6Qm/Rrz3n2Df/XI49by49p0ejhleas1Tx2RAnLtA7c2zobCvCpxiJZTYDIUIbTdnl9NBZhZ6r9y17SpbePuf0fSylA5NUkHPAiQEkGyoIy1x4McmKWewIAkRnK27GwbQ7wGrok= X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-Network-Message-Id: 4199a42f-83e9-4125-b359-08d7fc3ed085 X-MS-Exchange-CrossTenant-OriginalArrivalTime: 19 May 2020 21:51:41.5558 (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: zfrOYCUk2id9oucm9WCyqQ5f74CZZ41z001AAlItr276gGoftTZEvzaj7eTWSeQtNma+O+yYJdg8q8X5xvrkhQ== X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM5PR12MB2504 Content-Type: text/plain 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 Signed-off-by: Tom Lendacky --- OvmfPkg/OvmfPkgX64.dsc | 2 +- OvmfPkg/Library/VmgExitLib/VmgExitLib.inf | 36 ++++ OvmfPkg/Library/VmgExitLib/VmgExitLib.c | 155 ++++++++++++++++++ .../Library/VmgExitLib/X64/VmgExitVcHandler.c | 81 +++++++++ OvmfPkg/Library/VmgExitLib/VmgExitLib.uni | 15 ++ 5 files changed, 288 insertions(+), 1 deletion(-) create mode 100644 OvmfPkg/Library/VmgExitLib/VmgExitLib.inf create mode 100644 OvmfPkg/Library/VmgExitLib/VmgExitLib.c create mode 100644 OvmfPkg/Library/VmgExitLib/X64/VmgExitVcHandler.c create mode 100644 OvmfPkg/Library/VmgExitLib/VmgExitLib.uni diff --git a/OvmfPkg/OvmfPkgX64.dsc b/OvmfPkg/OvmfPkgX64.dsc index 0b9189ab1e38..b5f3859420d0 100644 --- a/OvmfPkg/OvmfPkgX64.dsc +++ b/OvmfPkg/OvmfPkgX64.dsc @@ -232,7 +232,7 @@ [LibraryClasses] [LibraryClasses.common] BaseCryptLib|CryptoPkg/Library/BaseCryptLib/BaseCryptLib.inf - VmgExitLib|UefiCpuPkg/Library/VmgExitLibNull/VmgExitLibNull.inf + VmgExitLib|OvmfPkg/Library/VmgExitLib/VmgExitLib.inf [LibraryClasses.common.SEC] TimerLib|OvmfPkg/Library/AcpiTimerLib/BaseRomAcpiTimerLib.inf diff --git a/OvmfPkg/Library/VmgExitLib/VmgExitLib.inf b/OvmfPkg/Library/VmgExitLib/VmgExitLib.inf new file mode 100644 index 000000000000..0e6bc8432314 --- /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 = 0x00010005 + BASE_NAME = VmgExitLib + MODULE_UNI_FILE = VmgExitLib.uni + FILE_GUID = 0e923c25-13cd-430b-8714-ffe85652a97b + MODULE_TYPE = BASE + VERSION_STRING = 1.0 + LIBRARY_CLASS = VmgExitLib + +# +# The following information is for reference only and not required by the build tools. +# +# VALID_ARCHITECTURES = X64 +# + +[Sources.X64] + X64/VmgExitVcHandler.c + +[Sources.common] + VmgExitLib.c + +[Packages] + MdePkg/MdePkg.dec + UefiCpuPkg/UefiCpuPkg.dec + +[LibraryClasses] + BaseLib + diff --git a/OvmfPkg/Library/VmgExitLib/VmgExitLib.c b/OvmfPkg/Library/VmgExitLib/VmgExitLib.c new file mode 100644 index 000000000000..7b7ebea85256 --- /dev/null +++ b/OvmfPkg/Library/VmgExitLib/VmgExitLib.c @@ -0,0 +1,155 @@ +/** @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 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 = Ghcb->SaveArea.SwExitInfo1; + ASSERT ((ExitInfo.Elements.Lower32Bits == 0) || + (ExitInfo.Elements.Lower32Bits == 1)); + + Status = 0; + if (ExitInfo.Elements.Lower32Bits == 0) { + return Status; + } + + if (ExitInfo.Elements.Lower32Bits == 1) { + ASSERT (Ghcb->SaveArea.SwExitInfo2 != 0); + + // Check that the return event is valid + Event.Uint64 = Ghcb->SaveArea.SwExitInfo2; + if (Event.Elements.Valid && + Event.Elements.Type == GHCB_EVENT_INJECTION_TYPE_EXCEPTION) { + switch (Event.Elements.Vector) { + case GP_EXCEPTION: + case UD_EXCEPTION: + // Use returned event as return code + Status = Event.Uint64; + } + } + } + + if (Status == 0) { + GHCB_EVENT_INJECTION GpEvent; + + GpEvent.Uint64 = 0; + GpEvent.Elements.Vector = GP_EXCEPTION; + GpEvent.Elements.Type = GHCB_EVENT_INJECTION_TYPE_EXCEPTION; + GpEvent.Elements.Valid = 1; + + Status = GpEvent.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 SwExitCode + 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. Exception + event to be propagated. + +**/ +UINT64 +EFIAPI +VmgExit ( + IN OUT GHCB *Ghcb, + IN UINT64 ExitCode, + IN UINT64 ExitInfo1, + IN UINT64 ExitInfo2 + ) +{ + Ghcb->SaveArea.SwExitCode = ExitCode; + Ghcb->SaveArea.SwExitInfo1 = ExitInfo1; + Ghcb->SaveArea.SwExitInfo2 = 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 + ) +{ +} + diff --git a/OvmfPkg/Library/VmgExitLib/X64/VmgExitVcHandler.c b/OvmfPkg/Library/VmgExitLib/X64/VmgExitVcHandler.c new file mode 100644 index 000000000000..036f030d6b34 --- /dev/null +++ b/OvmfPkg/Library/VmgExitLib/X64/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 set + 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 value 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 = EFI_SUCCESS; + + Msr.GhcbPhysicalAddress = AsmReadMsr64 (MSR_SEV_ES_GHCB); + ASSERT (Msr.GhcbInfo.Function == 0); + ASSERT (Msr.Ghcb != 0); + + Regs = SystemContext.SystemContextX64; + Ghcb = Msr.Ghcb; + + VmgInit (Ghcb); + + ExitCode = Regs->ExceptionData; + switch (ExitCode) { + default: + Status = VmgExit (Ghcb, SVM_EXIT_UNSUPPORTED, ExitCode, 0); + if (Status == 0) { + Regs->ExceptionData = 0; + *ExceptionType = GP_EXCEPTION; + } else { + GHCB_EVENT_INJECTION Event; + + Event.Uint64 = Status; + if (Event.Elements.ErrorCodeValid) { + Regs->ExceptionData = Event.Elements.ErrorCode; + } else { + Regs->ExceptionData = 0; + } + + *ExceptionType = Event.Elements.Vector; + } + + VcRet = EFI_PROTOCOL_ERROR; + } + + VmgDone (Ghcb); + + return VcRet; +} diff --git a/OvmfPkg/Library/VmgExitLib/VmgExitLib.uni b/OvmfPkg/Library/VmgExitLib/VmgExitLib.uni new file mode 100644 index 000000000000..a919b484c319 --- /dev/null +++ b/OvmfPkg/Library/VmgExitLib/VmgExitLib.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 "OVMF VMGEXIT Support Library." + +#string STR_MODULE_DESCRIPTION #language en-US "OVMF VMGEXIT Support Library." + -- 2.17.1