From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from NAM02-CY1-obe.outbound.protection.outlook.com (NAM02-CY1-obe.outbound.protection.outlook.com [40.107.76.45]) by mx.groups.io with SMTP id smtpd.web12.476.1587577349670673050 for ; Wed, 22 Apr 2020 10:42:29 -0700 Authentication-Results: mx.groups.io; dkim=pass header.i=@amdcloud.onmicrosoft.com header.s=selector2-amdcloud-onmicrosoft-com header.b=XjZD1UPo; spf=none, err=SPF record not found (domain: amd.com, ip: 40.107.76.45, mailfrom: thomas.lendacky@amd.com) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=UMDOldhUKNKMOfAVmyyGjgzQzGBOFY3HQNrOQP9gFgCpf613VrqXbhlY/8ClBB7eBKmTv73xPyXqFXRu8Z6Zprn6Pv1abW6f5ptnZyH0uHomCytS2uLytPusDXxYRluoODjqt1XbEfVmKIV2IfHjkCFj2ooQoQhkM8gkYy4DkoUSsc6ZnCrh3sYXTBDke4kK2yT0kAzbdBPWkdqOYkcIka23xAeSMs7nlUCFL1KugEmN1X5E2KFs55ZMdxc5NZ4VQa1Ghjvmd8yIZL5AA319xfe/3f6Mkk5oxJKdYLp0I8+ib8F/+iAINQTIdVcmJshi4IjwIXVU7ga44WAq7JOrLg== 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=Fg+9FNTUorv77wF8spqYwZwQ2gl5HinrNZO9jtKtSSg=; b=LhQP83ycvkZQSeThf6Zygq47c30DrZdsJLBe2JEGUDBkpE8fDRM/SFH3ZKfoyG4cagm+fSUrL3rOYoEENdZzMCR9yQyKY7AzxxFezujTsKR70bzjlyTwky7q+VtnrAZDJhGVQ6wS/BmynL5pOPXqqnbw0Y8YtVq6VzlzGqm5kM17sa1A1TKnW36JFv1zOobH0wPEUu4fdMi3AcX6xi55A7E6ulGTGxJDEQF0YMW6cetzp74gvvFuZs/amNfMwr5PDDhv2fCWnMWBcejOeiHxZDg6Q3IYHczlv75Xw6SOLi3RpSyzO+TwIMLnLn+lxuye7o9SlClKxOA+0lxjGl8Rrw== 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=Fg+9FNTUorv77wF8spqYwZwQ2gl5HinrNZO9jtKtSSg=; b=XjZD1UPosLwf5tl+Izx/T67npTCkH4G/3fivJLKyBtVSCXrQWmvMVRbFvLONznCs/pz7YR31TvIi6xTUUMR18j2i50zl2fWjsXpbeAbxH+z+OkWmfSuTSLwdS26jfvzx6XSEOEm9rlGRPLnpEARDqFKFxtqlho3psFaYMiC04BI= Authentication-Results: spf=none (sender IP is ) smtp.mailfrom=Thomas.Lendacky@amd.com; Received: from DM6PR12MB3163.namprd12.prod.outlook.com (2603:10b6:5:15e::26) by DM6PR12MB3449.namprd12.prod.outlook.com (2603:10b6:5:3b::24) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.2921.25; Wed, 22 Apr 2020 17:42:27 +0000 Received: from DM6PR12MB3163.namprd12.prod.outlook.com ([fe80::9ae:cb95:c925:d5bf]) by DM6PR12MB3163.namprd12.prod.outlook.com ([fe80::9ae:cb95:c925:d5bf%4]) with mapi id 15.20.2921.030; Wed, 22 Apr 2020 17:42:27 +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 , Maurice Ma , Guo Dong , Benjamin You Subject: [PATCH v7 11/43] UefiCpuPkg/CpuExceptionHandler: Add base support for the #VC exception Date: Wed, 22 Apr 2020 12:41:26 -0500 Message-Id: X-Mailer: git-send-email 2.17.1 In-Reply-To: References: X-ClientProxiedBy: DM5PR04CA0060.namprd04.prod.outlook.com (2603:10b6:3:ef::22) To DM6PR12MB3163.namprd12.prod.outlook.com (2603:10b6:5:15e::26) 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 DM5PR04CA0060.namprd04.prod.outlook.com (2603:10b6:3:ef::22) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.2937.13 via Frontend Transport; Wed, 22 Apr 2020 17:42:26 +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: 8e057f86-932c-4a03-78fe-08d7e6e485d8 X-MS-TrafficTypeDiagnostic: DM6PR12MB3449:|DM6PR12MB3449: X-MS-Exchange-Transport-Forked: True X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:5236; X-Forefront-PRVS: 03818C953D X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:DM6PR12MB3163.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFTY:;SFS:(10009020)(4636009)(136003)(346002)(376002)(39860400002)(366004)(396003)(66556008)(16526019)(66476007)(186003)(5660300002)(4326008)(30864003)(86362001)(54906003)(8936002)(81156014)(966005)(6486002)(66946007)(316002)(19627235002)(52116002)(7696005)(956004)(7416002)(36756003)(6916009)(2906002)(2616005)(8676002)(478600001)(26005)(136400200001);DIR:OUT;SFP:1101; Received-SPF: None (protection.outlook.com: amd.com does not designate permitted sender hosts) X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: JziV/bO77x67tIsn2mLc1/S7IcpAJD9OqacHTBt2JvpysfnEdSn3W6CwzNay1z6rQM9r4SJCRhRGZviNoaJ+yFiiM1QuJlyTd0tsNZdz/Fw5IGmBxQO+emMunGZNYYqYguMSJXwnBvwWJPcehsYZeZCQ5/6SD+54sBkMh0i+cgGyxUJbLnQNfX/pxYDi8n0DDMFQp1hmUNu+17e9FElu7/S6k9/q2HwUqt8SMYk00ePQUnOjk3MKxD/vx+sJEh6t693VDPhAK9P3TgRjfIm+cP3ONlNwPsbazgVE6jXspXsOVLmVVqTZk4ohKnHk4eJrYzOrgxgo5eaLTS9/YcftJQhovqazElhvNotujR0Vfnz7qDJSo9pHVZbZCtQpLWOarPX/R8GFcMF1gJqB7mTEf14czlY4XQYRCTLZJUeVChGKCjZYM4CVxxPV/0C4UOq4Jt5GRvZkhCHOh0SUhzEenLoXocPbhZXuJc1iFjDRe89hda5n1YDH5Ors+AbjfPXm5v3s84sIsdbIC/8pANIVutgpdoukZFNqz59HN4uNmeAU/Fo/eHV+Al22YTzKhs5MWWKg5tgDwdZrTQEyIh2qHw== X-MS-Exchange-AntiSpam-MessageData: PiCFDvXTdjBR9pdF4PzXTo29EHPCWWt9OZuf3omPznDZ9S3y9v5yH5A8qKktcX1aQAIPGklch8PjUqP0MQ9uz5wth6U8Qz6Xi4yLt39edjyX1vFXkvMSWympIY0SMs8w92R99WNO54dGocYrynn/8Q== X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-Network-Message-Id: 8e057f86-932c-4a03-78fe-08d7e6e485d8 X-MS-Exchange-CrossTenant-OriginalArrivalTime: 22 Apr 2020 17:42:27.1571 (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: TA/R9jUqfMPIZGpf68Qd3go22DuslQy3hJnMGSeyNk8Of9ZPFcR7JR4lxcubewr1kEeqVBtKs5WbM6nhizEYvA== X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM6PR12MB3449 Content-Type: text/plain BZ: https://bugzilla.tianocore.org/show_bug.cgi?id=2198 Add base support to handle #VC exceptions. This includes a stub routine to invoke when a #VC exception occurs and special checks in the common exception handlers to invoke the #VC exception handler routine. Cc: Eric Dong Cc: Ray Ni Cc: Laszlo Ersek Cc: Jordan Justen Cc: Ard Biesheuvel Cc: Maurice Ma Cc: Guo Dong Cc: Benjamin You Signed-off-by: Tom Lendacky --- .../DxeCpuExceptionHandlerLib.inf | 5 ++ .../PeiCpuExceptionHandlerLib.inf | 5 ++ .../SecPeiCpuExceptionHandlerLib.inf | 5 ++ .../SmmCpuExceptionHandlerLib.inf | 5 ++ .../CpuExceptionHandlerLib/AMDSevVcCommon.h | 49 ++++++++++++++ .../CpuExceptionCommon.h | 2 + .../CpuExceptionHandlerLib/AMDSevVcHandler.c | 40 ++++++++++++ .../CpuExceptionCommon.c | 2 +- .../Ia32/ArchAMDSevVcHandler.c | 38 +++++++++++ .../PeiDxeSmmCpuException.c | 16 +++++ .../SecPeiCpuException.c | 16 +++++ .../X64/ArchAMDSevVcHandler.c | 64 +++++++++++++++++++ 12 files changed, 246 insertions(+), 1 deletion(-) create mode 100644 UefiCpuPkg/Library/CpuExceptionHandlerLib/AMDSevVcCommon.h create mode 100644 UefiCpuPkg/Library/CpuExceptionHandlerLib/AMDSevVcHandler.c create mode 100644 UefiCpuPkg/Library/CpuExceptionHandlerLib/Ia32/ArchAMDSevVcHandler.c create mode 100644 UefiCpuPkg/Library/CpuExceptionHandlerLib/X64/ArchAMDSevVcHandler.c diff --git a/UefiCpuPkg/Library/CpuExceptionHandlerLib/DxeCpuExceptionHandlerLib.inf b/UefiCpuPkg/Library/CpuExceptionHandlerLib/DxeCpuExceptionHandlerLib.inf index e41383573043..4d79c4910b18 100644 --- a/UefiCpuPkg/Library/CpuExceptionHandlerLib/DxeCpuExceptionHandlerLib.inf +++ b/UefiCpuPkg/Library/CpuExceptionHandlerLib/DxeCpuExceptionHandlerLib.inf @@ -26,17 +26,21 @@ [Sources.Ia32] Ia32/ExceptionTssEntryAsm.nasm Ia32/ArchExceptionHandler.c Ia32/ArchInterruptDefs.h + Ia32/ArchAMDSevVcHandler.c [Sources.X64] X64/ExceptionHandlerAsm.nasm X64/ArchExceptionHandler.c X64/ArchInterruptDefs.h + X64/ArchAMDSevVcHandler.c [Sources.common] CpuExceptionCommon.h CpuExceptionCommon.c PeiDxeSmmCpuException.c DxeException.c + AMDSevVcHandler.c + AMDSevVcCommon.h [Pcd] gEfiMdeModulePkgTokenSpaceGuid.PcdCpuStackGuard @@ -57,3 +61,4 @@ [LibraryClasses] PeCoffGetEntryPointLib MemoryAllocationLib DebugLib + VmgExitLib diff --git a/UefiCpuPkg/Library/CpuExceptionHandlerLib/PeiCpuExceptionHandlerLib.inf b/UefiCpuPkg/Library/CpuExceptionHandlerLib/PeiCpuExceptionHandlerLib.inf index f31423ac0f91..e8445e47eaa3 100644 --- a/UefiCpuPkg/Library/CpuExceptionHandlerLib/PeiCpuExceptionHandlerLib.inf +++ b/UefiCpuPkg/Library/CpuExceptionHandlerLib/PeiCpuExceptionHandlerLib.inf @@ -26,17 +26,21 @@ [Sources.Ia32] Ia32/ExceptionTssEntryAsm.nasm Ia32/ArchExceptionHandler.c Ia32/ArchInterruptDefs.h + Ia32/ArchAMDSevVcHandler.c [Sources.X64] X64/ExceptionHandlerAsm.nasm X64/ArchExceptionHandler.c X64/ArchInterruptDefs.h + X64/ArchAMDSevVcHandler.c [Sources.common] CpuExceptionCommon.h CpuExceptionCommon.c PeiCpuException.c PeiDxeSmmCpuException.c + AMDSevVcHandler.c + AMDSevVcCommon.h [Packages] MdePkg/MdePkg.dec @@ -52,6 +56,7 @@ [LibraryClasses] HobLib MemoryAllocationLib SynchronizationLib + VmgExitLib [Pcd] gEfiMdeModulePkgTokenSpaceGuid.PcdCpuStackGuard # CONSUMES diff --git a/UefiCpuPkg/Library/CpuExceptionHandlerLib/SecPeiCpuExceptionHandlerLib.inf b/UefiCpuPkg/Library/CpuExceptionHandlerLib/SecPeiCpuExceptionHandlerLib.inf index 6d25cafe2ca3..a15bf71b4052 100644 --- a/UefiCpuPkg/Library/CpuExceptionHandlerLib/SecPeiCpuExceptionHandlerLib.inf +++ b/UefiCpuPkg/Library/CpuExceptionHandlerLib/SecPeiCpuExceptionHandlerLib.inf @@ -26,16 +26,20 @@ [Sources.Ia32] Ia32/ExceptionTssEntryAsm.nasm Ia32/ArchExceptionHandler.c Ia32/ArchInterruptDefs.h + Ia32/ArchAMDSevVcHandler.c [Sources.X64] X64/ExceptionHandlerAsm.nasm X64/ArchExceptionHandler.c X64/ArchInterruptDefs.h + X64/ArchAMDSevVcHandler.c [Sources.common] CpuExceptionCommon.h CpuExceptionCommon.c SecPeiCpuException.c + AMDSevVcHandler.c + AMDSevVcCommon.h [Packages] MdePkg/MdePkg.dec @@ -48,3 +52,4 @@ [LibraryClasses] PrintLib LocalApicLib PeCoffGetEntryPointLib + VmgExitLib diff --git a/UefiCpuPkg/Library/CpuExceptionHandlerLib/SmmCpuExceptionHandlerLib.inf b/UefiCpuPkg/Library/CpuExceptionHandlerLib/SmmCpuExceptionHandlerLib.inf index 66c7f59e3c91..190d7dfcd42a 100644 --- a/UefiCpuPkg/Library/CpuExceptionHandlerLib/SmmCpuExceptionHandlerLib.inf +++ b/UefiCpuPkg/Library/CpuExceptionHandlerLib/SmmCpuExceptionHandlerLib.inf @@ -26,17 +26,21 @@ [Sources.Ia32] Ia32/ExceptionTssEntryAsm.nasm Ia32/ArchExceptionHandler.c Ia32/ArchInterruptDefs.h + Ia32/ArchAMDSevVcHandler.c [Sources.X64] X64/ExceptionHandlerAsm.nasm X64/ArchExceptionHandler.c X64/ArchInterruptDefs.h + X64/ArchAMDSevVcHandler.c [Sources.common] CpuExceptionCommon.h CpuExceptionCommon.c PeiDxeSmmCpuException.c SmmException.c + AMDSevVcHandler.c + AMDSevVcCommon.h [Packages] MdePkg/MdePkg.dec @@ -51,4 +55,5 @@ [LibraryClasses] LocalApicLib PeCoffGetEntryPointLib DebugLib + VmgExitLib diff --git a/UefiCpuPkg/Library/CpuExceptionHandlerLib/AMDSevVcCommon.h b/UefiCpuPkg/Library/CpuExceptionHandlerLib/AMDSevVcCommon.h new file mode 100644 index 000000000000..e4b8f3147add --- /dev/null +++ b/UefiCpuPkg/Library/CpuExceptionHandlerLib/AMDSevVcCommon.h @@ -0,0 +1,49 @@ +/** @file + Common header file for SEV-ES #VC Exception Handler Support. + + Copyright (c) 2020, Advanced Micro Devices, Inc. All rights reserved.
+ SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#ifndef _AMD_SEV_VC_COMMON_H_ +#define _AMD_SEV_VC_COMMON_H_ + +#include +#include + +/** + #VC exception handling routine. + + Called by the UefiCpuPkg exception handling support when a #VC is encountered. + + @param[in, out] Context Pointer to EFI_SYSTEM_CONTEXT. + + @retval 0 Exception handled + @retval Others New exception value to propagate + +**/ +UINTN +DoVcException ( + IN OUT EFI_SYSTEM_CONTEXT Context + ); + +/** + Common #VC exception handling routine. + + Used to bridge different phases of UEFI execution. + + @param[in, out] Ghcb Pointer to the Guest-Hypervisor Communication Block + @param[in, out] Context Pointer to EFI_SYSTEM_CONTEXT. + + @retval 0 Exception handled + @retval Others New exception value to propagate + +**/ +UINTN +DoVcCommon ( + IN OUT GHCB *Ghcb, + IN OUT EFI_SYSTEM_CONTEXT Context + ); + +#endif diff --git a/UefiCpuPkg/Library/CpuExceptionHandlerLib/CpuExceptionCommon.h b/UefiCpuPkg/Library/CpuExceptionHandlerLib/CpuExceptionCommon.h index 805dd9cbb4ff..0f274e7ea328 100644 --- a/UefiCpuPkg/Library/CpuExceptionHandlerLib/CpuExceptionCommon.h +++ b/UefiCpuPkg/Library/CpuExceptionHandlerLib/CpuExceptionCommon.h @@ -24,6 +24,8 @@ #define CPU_INTERRUPT_NUM 256 #define HOOKAFTER_STUB_SIZE 16 +#define VC_EXCEPTION 29 + // // Exception Error Code of Page-Fault Exception // diff --git a/UefiCpuPkg/Library/CpuExceptionHandlerLib/AMDSevVcHandler.c b/UefiCpuPkg/Library/CpuExceptionHandlerLib/AMDSevVcHandler.c new file mode 100644 index 000000000000..fc91cf119f3e --- /dev/null +++ b/UefiCpuPkg/Library/CpuExceptionHandlerLib/AMDSevVcHandler.c @@ -0,0 +1,40 @@ +/** @file + SEV-ES #VC Exception Handler functon. + + Copyright (c) 2020, Advanced Micro Devices, Inc. All rights reserved.
+ SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include +#include +#include "CpuExceptionCommon.h" +#include "AMDSevVcCommon.h" + +/** + #VC exception handling routine. + + Called by the UefiCpuPkg exception handling support when a #VC is encountered. + + @param[in, out] Context Pointer to EFI_SYSTEM_CONTEXT. + + @retval 0 Exception handled + @retval Others New exception value to propagate + +**/ +UINTN +DoVcException ( + IN OUT EFI_SYSTEM_CONTEXT Context + ) +{ + MSR_SEV_ES_GHCB_REGISTER Msr; + GHCB *Ghcb; + + Msr.GhcbPhysicalAddress = AsmReadMsr64 (MSR_SEV_ES_GHCB); + ASSERT (Msr.GhcbInfo.Function == 0); + ASSERT (Msr.Ghcb != 0); + + Ghcb = Msr.Ghcb; + + return DoVcCommon (Ghcb, Context); +} diff --git a/UefiCpuPkg/Library/CpuExceptionHandlerLib/CpuExceptionCommon.c b/UefiCpuPkg/Library/CpuExceptionHandlerLib/CpuExceptionCommon.c index 8adbd43fefb4..39e4dd9e9417 100644 --- a/UefiCpuPkg/Library/CpuExceptionHandlerLib/CpuExceptionCommon.c +++ b/UefiCpuPkg/Library/CpuExceptionHandlerLib/CpuExceptionCommon.c @@ -14,7 +14,7 @@ // // 1 means an error code will be pushed, otherwise 0 // -CONST UINT32 mErrorCodeFlag = 0x00227d00; +CONST UINT32 mErrorCodeFlag = 0x20227d00; // // Define the maximum message length diff --git a/UefiCpuPkg/Library/CpuExceptionHandlerLib/Ia32/ArchAMDSevVcHandler.c b/UefiCpuPkg/Library/CpuExceptionHandlerLib/Ia32/ArchAMDSevVcHandler.c new file mode 100644 index 000000000000..d988fe620d53 --- /dev/null +++ b/UefiCpuPkg/Library/CpuExceptionHandlerLib/Ia32/ArchAMDSevVcHandler.c @@ -0,0 +1,38 @@ +/** @file + IA32 SEV-ES #VC Exception Handler functons. + + Copyright (c) 2020, Advanced Micro Devices, Inc. All rights reserved.
+ SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include +#include +#include "AMDSevVcCommon.h" + +/** + Common #VC exception handling routine. + + Used to bridge different phases of UEFI execution. + + @param[in, out] Ghcb Pointer to the Guest-Hypervisor Communication Block + @param[in, out] Context Pointer to EFI_SYSTEM_CONTEXT. + + @retval 0 Exception handled + @retval Others New exception value to propagate + +**/ +UINTN +DoVcCommon ( + IN OUT GHCB *Ghcb, + IN OUT EFI_SYSTEM_CONTEXT Context + ) +{ + EFI_SYSTEM_CONTEXT_IA32 *Regs; + + Regs = Context.SystemContextIa32; + + Regs->ExceptionData = 0; + + return GP_EXCEPTION; +} diff --git a/UefiCpuPkg/Library/CpuExceptionHandlerLib/PeiDxeSmmCpuException.c b/UefiCpuPkg/Library/CpuExceptionHandlerLib/PeiDxeSmmCpuException.c index 6a2670d55918..d565db547808 100644 --- a/UefiCpuPkg/Library/CpuExceptionHandlerLib/PeiDxeSmmCpuException.c +++ b/UefiCpuPkg/Library/CpuExceptionHandlerLib/PeiDxeSmmCpuException.c @@ -7,6 +7,7 @@ SPDX-License-Identifier: BSD-2-Clause-Patent **/ #include "CpuExceptionCommon.h" +#include "AMDSevVcCommon.h" #include /** @@ -86,6 +87,21 @@ CommonExceptionHandlerWorker ( break; } + if (ExceptionType == VC_EXCEPTION) { + UINTN Status; + // + // #VC must be handled for an SEV-ES guest + // + Status = DoVcException (SystemContext); + if (Status) { + // Exception not handled - Status contains the desired exception now + ExceptionType = Status; + } else { + // Exception handled + return; + } + } + if (ExternalInterruptHandler != NULL && ExternalInterruptHandler[ExceptionType] != NULL) { (ExternalInterruptHandler[ExceptionType]) (ExceptionType, SystemContext); diff --git a/UefiCpuPkg/Library/CpuExceptionHandlerLib/SecPeiCpuException.c b/UefiCpuPkg/Library/CpuExceptionHandlerLib/SecPeiCpuException.c index 20148db74cf8..0716680d2f50 100644 --- a/UefiCpuPkg/Library/CpuExceptionHandlerLib/SecPeiCpuException.c +++ b/UefiCpuPkg/Library/CpuExceptionHandlerLib/SecPeiCpuException.c @@ -8,6 +8,7 @@ SPDX-License-Identifier: BSD-2-Clause-Patent #include #include "CpuExceptionCommon.h" +#include "AMDSevVcCommon.h" CONST UINTN mDoFarReturnFlag = 0; @@ -24,6 +25,21 @@ CommonExceptionHandler ( IN EFI_SYSTEM_CONTEXT SystemContext ) { + if (ExceptionType == VC_EXCEPTION) { + UINTN Status; + // + // #VC must be handled for an SEV-ES guest + // + Status = DoVcException (SystemContext); + if (Status) { + // Exception not handled - Status contains the desired exception now + ExceptionType = Status; + } else { + // Exception handled + return; + } + } + // // Initialize the serial port before dumping. // diff --git a/UefiCpuPkg/Library/CpuExceptionHandlerLib/X64/ArchAMDSevVcHandler.c b/UefiCpuPkg/Library/CpuExceptionHandlerLib/X64/ArchAMDSevVcHandler.c new file mode 100644 index 000000000000..c0a0eee319bf --- /dev/null +++ b/UefiCpuPkg/Library/CpuExceptionHandlerLib/X64/ArchAMDSevVcHandler.c @@ -0,0 +1,64 @@ +/** @file + X64 SEV-ES #VC Exception Handler functons. + + Copyright (c) 2020, Advanced Micro Devices, Inc. All rights reserved.
+ SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include +#include +#include +#include "AMDSevVcCommon.h" + +/** + Common #VC exception handling routine. + + Used to bridge different phases of UEFI execution. + + @param[in, out] Ghcb Pointer to the Guest-Hypervisor Communication Block + @param[in, out] Context Pointer to EFI_SYSTEM_CONTEXT. + + @retval 0 Exception handled + @retval Others New exception value to propagate + +**/ +UINTN +DoVcCommon ( + IN OUT GHCB *Ghcb, + IN OUT EFI_SYSTEM_CONTEXT Context + ) +{ + EFI_SYSTEM_CONTEXT_X64 *Regs; + UINT64 Status; + UINTN ExitCode, VcRet; + + Regs = Context.SystemContextX64; + + VmgInit (Ghcb); + + ExitCode = Regs->ExceptionData; + switch (ExitCode) { + default: + Status = VmgExit (Ghcb, SvmExitUnsupported, ExitCode, 0); + if (Status == 0) { + Regs->ExceptionData = 0; + VcRet = GP_EXCEPTION; + } else { + GHCB_EVENT_INJECTION Event; + + Event.Uint64 = Status; + if (Event.Elements.ErrorCodeValid) { + Regs->ExceptionData = Event.Elements.ErrorCode; + } else { + Regs->ExceptionData = 0; + } + + VcRet = Event.Elements.Vector; + } + } + + VmgDone (Ghcb); + + return VcRet; +} -- 2.17.1