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 []) by mx.groups.io with SMTP id smtpd.web11.983.1589925154464670122 for ; Tue, 19 May 2020 14:52:35 -0700 Authentication-Results: mx.groups.io; dkim=pass header.i=@amdcloud.onmicrosoft.com header.s=selector2-amdcloud-onmicrosoft-com header.b=XU4Nc77Z; spf=none, err=SPF record not found (domain: amd.com, ip: , mailfrom: thomas.lendacky@amd.com) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=eTk9CjUJN2Wk73B7P8VPJkJjcek5MMTEHDAGMD8g9DeNXSpfUd9k6/wSR4ohUY3XvJAyGIHp2pWTawzxZXH9PiWmMyHA/HENjChzKp7dYGZccGC0jvu+4Ekb0XY2NSUoS63m6Y7Q1oMOCQqh7hl8TTtZ3okZPEn76NHuyKUZVOjMw4CPh4U++Xkx7gXio6kxK/OddGZyyDIqdk6KkmDX/kHgWtzxmjEA8mGvZ67/oXOPAqWIINK0zeljdl6gdhxsgWOa7AFYiXvqK0h4Z2qOJqG/OM8qLIB2fj/ZzZuc8EjurG5Q8w/KDCNFBPj5CaNsXqQjE6u4AGz6DaySliWgEA== 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=eKEFA2uuqO/N/ZKjYVCrCpPRVfH/BqoPSF1WMOT5bpw=; b=GI03EM70DIs+dgnBZmwSqYEkhQpbbpomFwEU2fhYL3Mco9WX31MzHRZP1DiH0DKgWcZAWfe8s9l+ecUUy7tbSVZ9V7W9J3S5ESGDsupbBWY7Ok459rRbldvqj4c9tatzL4a1cW3n8S7T4nHCQ5+qCNnSjhMdmZl36bF7BjlpHPj1wimURxAq+GaXAMqYRXMsB19lm6LAJKx/rblmF8c6qlRyU/DDUTC873seOpkl8BHWGU7WFUpGwt0e+LOR6wN/BICzcWMhWHE3EoQnwC+Y9X5EpkMU7sh5Lg34Sf6Bf5tHTR/zmeCB3fMwqCTyPjAqeGG3VUJGvnJNKXMX+E9zNA== 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=eKEFA2uuqO/N/ZKjYVCrCpPRVfH/BqoPSF1WMOT5bpw=; b=XU4Nc77ZJLZJ+9vdnNEFQjsVrNIabzRqEKnCr3gANr2DPnop/xoP4ye+QIO2HLEty9LOW8kwej9podVWhdgyEOliHusIMs2ODSxx0RvkD3yHtLcCYvVmGz4Pp6lPUPyuuTA9zgHiAM6Fp2bwyYHwjQtNlmYD+Kw66RzT+f9/HwE= 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:52:33 +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:52:33 +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 Subject: [PATCH v8 37/46] OvmfPkg/Sec: Add #VC exception handling for Sec phase Date: Tue, 19 May 2020 16:51:05 -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:52:05 +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: fd46a6c9-4036-4ee2-f73b-08d7fc3edefd 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: aNGZ91mxWYwqD365hzgeMehRlzklaaDKmN54e4dTuOYRDQl4yNzofJy7vYBuDWilqtnthIjXt30Lfr7ftkphds6LB4oBc7dDA1Bzjy/sOkYCgf6iSorCfNS3B30tl2KRS02141NDBPTHqE7TFJF8TgyjHpuoiXA6X0AKtQNzPcIzHbToRJPmCKgP+0krl/oyMC3rSsH9WxDHvwiSeH28vXhDT3gVXVso+qRF4KH77txRtw32g8XHxz4J2XxHlckXDbNtFU5AR4HoqCPXmeai7xK/n0jCrE8ykuUGwbpI+zP9IHGbeYojV8bFxdXnM30LFDfrp0OlHrpUu9QhZDaj1WHDIyxHPXCWtx5x1GcYkymxI7lUYos6IymURBRwHyNgB3EjHzP6z/ATB3yh/Vv26yhaudml2y83i4d04d8Mm+o8P0zo6B+O6xoIV6iUsq+XimsETS62SKlHYhv5qXM9ABo/aZSp9NH7AwjGSiXRWFBWwQm8uCxzj+1+VoEilXQJ491HafD3x6s1Qj6jhuOomyIQsC9Nxu3RHNXFSbpbR8vc0hcBRcXrgyle42w72LVv69TmHvFDRLdZ/AqMB61RuA== 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)(66556008)(36756003)(26005)(16526019)(186003)(4326008)(8676002)(5660300002)(966005)(6486002)(8936002)(6916009)(478600001)(136400200001);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData: DRoFakKTD3m1Ij9BJXb7zW69ANeCmg1+b7yA2mmmMbkNWSMtWpHuQyyh1eejMzgV5zrGgWzxosmg1M2fzaBcLy83CI/iLXiCglLLxZJtx4BnfNxZmPtBmeMGIwVlyNXvLD6CPlz9LIGOAV1Fd/p5wIoGxJPDH0UKt6eFkjt5FnsKPnkdyP6cWXA8hcRDMHuaXjH+U1/eiHk5az/H6K6GeVU8lCy1uFcOVLAD1e76YYBakIptr68HOCoBCRp4UmJKhXBqn6xc1/3LTEaZt5VwMP4z9Bx9/0XYqM4MADVF+IGJ1vz4cHGEqvemW/cy5tdnMgMOQ4G783bxmQjLnDGYFajLBH8a4IkS4StPEFXCIqZb9Rj8xiB002yZzWwr7C/cnPv5CjYhEQiJjkCJff1tFUbSo4tujApBBNeYsF578H+t1+LfF/CQ6UP0fIiHFHahbsaI+PszHobNr9+NiGkDf/DcWgzlyONWZHHYMHxpA5E= X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-Network-Message-Id: fd46a6c9-4036-4ee2-f73b-08d7fc3edefd X-MS-Exchange-CrossTenant-OriginalArrivalTime: 19 May 2020 21:52:05.7431 (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: XVfrEBPpkzDE1Pehbga5mSm9TN5mBp6JKqQIcJIu6OkiYvPChVVoppZegH8yNddf//lCqFaZpwrDoqXfcLy5iA== X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM5PR12MB2504 Content-Type: text/plain BZ: https://bugzilla.tianocore.org/show_bug.cgi?id=2198 An SEV-ES guest will generate a #VC exception when it encounters a non-automatic exit (NAE) event. It is expected that the #VC exception handler will communicate with the hypervisor using the GHCB to handle the NAE event. NAE events can occur during the Sec phase, so initialize exception handling early in the OVMF Sec support. Before establishing the exception handling, validate that the supported version of the SEV-ES protocol in OVMF is supported by the hypervisor. Cc: Jordan Justen Cc: Laszlo Ersek Cc: Ard Biesheuvel Reviewed-by: Laszlo Ersek Signed-off-by: Tom Lendacky --- OvmfPkg/Sec/SecMain.inf | 4 + OvmfPkg/Sec/SecMain.c | 181 +++++++++++++++++++++++++++++++++++++--- 2 files changed, 172 insertions(+), 13 deletions(-) diff --git a/OvmfPkg/Sec/SecMain.inf b/OvmfPkg/Sec/SecMain.inf index 63ba4cb555fb..7f78dcee2772 100644 --- a/OvmfPkg/Sec/SecMain.inf +++ b/OvmfPkg/Sec/SecMain.inf @@ -50,15 +50,19 @@ [LibraryClasses] PeCoffExtraActionLib ExtractGuidedSectionLib LocalApicLib + CpuExceptionHandlerLib [Ppis] gEfiTemporaryRamSupportPpiGuid # PPI ALWAYS_PRODUCED [Pcd] + gUefiCpuPkgTokenSpaceGuid.PcdSevEsWorkAreaBase gUefiOvmfPkgTokenSpaceGuid.PcdOvmfPeiMemFvBase gUefiOvmfPkgTokenSpaceGuid.PcdOvmfPeiMemFvSize gUefiOvmfPkgTokenSpaceGuid.PcdOvmfDxeMemFvBase gUefiOvmfPkgTokenSpaceGuid.PcdOvmfDxeMemFvSize + gUefiOvmfPkgTokenSpaceGuid.PcdOvmfSecGhcbBase + gUefiOvmfPkgTokenSpaceGuid.PcdOvmfSecGhcbSize gUefiOvmfPkgTokenSpaceGuid.PcdOvmfSecPageTablesBase gUefiOvmfPkgTokenSpaceGuid.PcdOvmfSecPeiTempRamBase gUefiOvmfPkgTokenSpaceGuid.PcdOvmfSecPeiTempRamSize diff --git a/OvmfPkg/Sec/SecMain.c b/OvmfPkg/Sec/SecMain.c index 6dea6e771a29..c2a35463dce4 100644 --- a/OvmfPkg/Sec/SecMain.c +++ b/OvmfPkg/Sec/SecMain.c @@ -24,6 +24,9 @@ #include #include #include +#include +#include +#include #include @@ -34,6 +37,10 @@ typedef struct _SEC_IDT_TABLE { IA32_IDT_GATE_DESCRIPTOR IdtTable[SEC_IDT_ENTRY_COUNT]; } SEC_IDT_TABLE; +typedef struct _SEC_SEV_ES_WORK_AREA { + UINT8 SevEsEnabled; +} SEC_SEV_ES_WORK_AREA; + VOID EFIAPI SecStartupPhase2 ( @@ -712,6 +719,120 @@ FindAndReportEntryPoints ( return; } +/** + Handle an SEV-ES/GHCB protocol check failure. + + Notify the hypervisor using the VMGEXIT instruction that the SEV-ES guest + wishes to be terminated. + + @param[in] ReasonCode Reason code to provide to the hypervisor for the + termination request. + +**/ +STATIC +VOID +SevEsProtocolFailure ( + IN UINT8 ReasonCode + ) +{ + MSR_SEV_ES_GHCB_REGISTER Msr; + + // + // Use the GHCB MSR Protocol to request termination by the hypervisor + // + Msr.GhcbPhysicalAddress = 0; + Msr.GhcbTerminate.Function = GHCB_INFO_TERMINATE_REQUEST; + Msr.GhcbTerminate.ReasonCodeSet = GHCB_TERMINATE_GHCB; + Msr.GhcbTerminate.ReasonCode = ReasonCode; + AsmWriteMsr64 (MSR_SEV_ES_GHCB, Msr.GhcbPhysicalAddress); + + AsmVmgExit (); + + ASSERT (FALSE); + CpuDeadLoop (); +} + +/** + Validate the SEV-ES/GHCB protocol level. + + Verify that the level of SEV-ES/GHCB protocol supported by the hypervisor + and the guest intersect. If they don't intersect, request termination. + +**/ +STATIC +VOID +SevEsProtocolCheck ( + VOID + ) +{ + MSR_SEV_ES_GHCB_REGISTER Msr; + GHCB *Ghcb; + + // + // Use the GHCB MSR Protocol to obtain the GHCB SEV-ES Information for + // protocol checking + // + Msr.GhcbPhysicalAddress = 0; + Msr.GhcbInfo.Function = GHCB_INFO_SEV_INFO_GET; + AsmWriteMsr64 (MSR_SEV_ES_GHCB, Msr.GhcbPhysicalAddress); + + AsmVmgExit (); + + Msr.GhcbPhysicalAddress = AsmReadMsr64 (MSR_SEV_ES_GHCB); + + if (Msr.GhcbInfo.Function != GHCB_INFO_SEV_INFO) { + SevEsProtocolFailure (GHCB_TERMINATE_GHCB_GENERAL); + } + + if (Msr.GhcbProtocol.SevEsProtocolMin > Msr.GhcbProtocol.SevEsProtocolMax) { + SevEsProtocolFailure (GHCB_TERMINATE_GHCB_PROTOCOL); + } + + if ((Msr.GhcbProtocol.SevEsProtocolMin > GHCB_VERSION_MAX) || + (Msr.GhcbProtocol.SevEsProtocolMax < GHCB_VERSION_MIN)) { + SevEsProtocolFailure (GHCB_TERMINATE_GHCB_PROTOCOL); + } + + // + // SEV-ES protocol checking succeeded, set the initial GHCB address + // + Msr.GhcbPhysicalAddress = FixedPcdGet32 (PcdOvmfSecGhcbBase); + AsmWriteMsr64 (MSR_SEV_ES_GHCB, Msr.GhcbPhysicalAddress); + + Ghcb = Msr.Ghcb; + SetMem (Ghcb, sizeof (*Ghcb), 0); + + // + // Set the version to the maximum that can be supported + // + Ghcb->ProtocolVersion = MIN (Msr.GhcbProtocol.SevEsProtocolMax, GHCB_VERSION_MAX); + Ghcb->GhcbUsage = GHCB_STANDARD_USAGE; +} + +/** + Determine if SEV-ES is active. + + During early booting, SEV-ES support code will set a flag to indicate that + SEV-ES is enabled. Return the value of this flag as an indicator that SEV-ES + is enabled. + + @retval TRUE SEV-ES is enabled + @retval FALSE SEV-ES is not enabled + +**/ +STATIC +BOOLEAN +SevEsIsEnabled ( + VOID + ) +{ + SEC_SEV_ES_WORK_AREA *SevEsWorkArea; + + SevEsWorkArea = (SEC_SEV_ES_WORK_AREA *) FixedPcdGet32 (PcdSevEsWorkAreaBase); + + return ((SevEsWorkArea != NULL) && (SevEsWorkArea->SevEsEnabled != 0)); +} + VOID EFIAPI SecCoreStartupWithStack ( @@ -737,8 +858,55 @@ SecCoreStartupWithStack ( Table[Index] = 0; } + // + // Initialize IDT - Since this is before library constructors are called, + // we use a loop rather than CopyMem. + // + IdtTableInStack.PeiService = NULL; + for (Index = 0; Index < SEC_IDT_ENTRY_COUNT; Index ++) { + UINT8 *Src, *Dst; + UINTN Byte; + + Src = (UINT8 *) &mIdtEntryTemplate; + Dst = (UINT8 *) &IdtTableInStack.IdtTable[Index]; + for (Byte = 0; Byte < sizeof (mIdtEntryTemplate); Byte++) { + Dst[Byte] = Src[Byte]; + } + } + + IdtDescriptor.Base = (UINTN)&IdtTableInStack.IdtTable; + IdtDescriptor.Limit = (UINT16)(sizeof (IdtTableInStack.IdtTable) - 1); + + if (SevEsIsEnabled ()) { + SevEsProtocolCheck (); + + // + // For SEV-ES guests, the exception handler is needed before calling + // ProcessLibraryConstructorList() because some of the library constructors + // perform some functions that result in #VC exceptions being generated. + // + // Due to this code executing before library constructors, *all* library + // API calls are theoretically interface contract violations. However, + // because this is SEC (executing in flash), those constructors cannot + // write variables with static storage duration anyway. Furthermore, only + // a small, restricted set of APIs, such as AsmWriteIdtr() and + // InitializeCpuExceptionHandlers(), are called, where we require that the + // underlying library not require constructors to have been invoked and + // that the library instance not trigger any #VC exceptions. + // + AsmWriteIdtr (&IdtDescriptor); + InitializeCpuExceptionHandlers (NULL); + } + ProcessLibraryConstructorList (NULL, NULL); + if (!SevEsIsEnabled ()) { + // + // For non SEV-ES guests, just load the IDTR. + // + AsmWriteIdtr (&IdtDescriptor); + } + DEBUG ((DEBUG_INFO, "SecCoreStartupWithStack(0x%x, 0x%x)\n", (UINT32)(UINTN)BootFv, @@ -751,19 +919,6 @@ SecCoreStartupWithStack ( // InitializeFloatingPointUnits (); - // - // Initialize IDT - // - IdtTableInStack.PeiService = NULL; - for (Index = 0; Index < SEC_IDT_ENTRY_COUNT; Index ++) { - CopyMem (&IdtTableInStack.IdtTable[Index], &mIdtEntryTemplate, sizeof (mIdtEntryTemplate)); - } - - IdtDescriptor.Base = (UINTN)&IdtTableInStack.IdtTable; - IdtDescriptor.Limit = (UINT16)(sizeof (IdtTableInStack.IdtTable) - 1); - - AsmWriteIdtr (&IdtDescriptor); - #if defined (MDE_CPU_X64) // // ASSERT that the Page Tables were set by the reset vector code to -- 2.17.1