From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from NAM10-BN7-obe.outbound.protection.outlook.com (NAM10-BN7-obe.outbound.protection.outlook.com [40.107.92.79]) by mx.groups.io with SMTP id smtpd.web10.9862.1597325316236200983 for ; Thu, 13 Aug 2020 06:28:36 -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=JMwuzcw9; spf=none, err=SPF record not found (domain: amd.com, ip: 40.107.92.79, mailfrom: thomas.lendacky@amd.com) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=Ri4wvbBUIk9ULJy8UcTvKT/l8HhYXVlGIHQo03MtvRwUeheKSQfae3AvMabeRAD4k9WdVCiwU7MdEVw0LpjdVWhJ/8ra1h4lNGx3QMCr3BNEPv/NnDYvRccXtl8dKm01zck/vVl1WywaEqUgh+dxPehrIAMuMxP+WYdymepUp9n69wEbJ8tx1FYsFuCQGdkteeTHLuEl4Sb7Cid3tMjS8s3zsCwMZHWwIs1RRNJi15jOYqB+cYgHItsZi8UwtXE1H9GbUUtC8ImF+4M7Y2jd/gjpW7gIEkR8x3Niz0UozPj4QmS7WAyA1r5/YA3M3iR/6bzYzq0eITje+0TV7PjYfQ== 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=GdPNDT2pHskUkHJeEZyeq4nowRG9bvq6O+AZe1Xic8U=; b=CEtcShjU44uY5HvMsERg9f4KH4EOzZ726/mEwSt0F8NyDc+Dv+/Ht/cmeYNYwJAnh2fr5CL0yVziFvb+2pjsRvrPQsiLy5lR1TIYYDUxlUxdfzWA3ZQZH50tCN277QhK7JCMKxEpnXJ4ETsklxpfQIM+uSvQClpsbK1n1ZNoO6BUulNWvQHad/87epFXW3KShMO5q+YAxrGLqeRWzyDFoUqBA65Dydxlk/HqpYbd60pXa0xV62D7//T0zOqeYMwPMQwj9PYsi5/Q2XDgYSILnAtee+FlNEnU8naUrifw2h+yy/33NFd3wUP2BL8GIBzww8QI8phl3ouhYyj0OeZOqg== 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=GdPNDT2pHskUkHJeEZyeq4nowRG9bvq6O+AZe1Xic8U=; b=JMwuzcw9xS2zEswMd9kkXcjoyIW9VKK/2k10b4PKLssfm+0HPwb89R7nrnoNacRXH0J9eSj9c5FlSSCMDQ/lVElX0gZJaG9O2qNRi8tvtcgLt1zQ0RcYy8cf5/LAex/b0+BgF82uNqirA4GfrFkfAAILj4BrAjHCAJUcnGxAgEs= 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 DM6PR12MB4220.namprd12.prod.outlook.com (2603:10b6:5:21d::7) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3283.15; Thu, 13 Aug 2020 13:28:34 +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:28:34 +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 37/46] OvmfPkg/Sec: Add #VC exception handling for Sec phase Date: Thu, 13 Aug 2020 08:23:24 -0500 Message-ID: X-Mailer: git-send-email 2.28.0 In-Reply-To: References: X-ClientProxiedBy: DM6PR03CA0055.namprd03.prod.outlook.com (2603:10b6:5:100::32) 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 DM6PR03CA0055.namprd03.prod.outlook.com (2603:10b6:5:100::32) 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:28:33 +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: a31ec47b-34b8-4e69-bf31-08d83f8cc6f7 X-MS-TrafficTypeDiagnostic: DM6PR12MB4220: 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: KccKzrpJn06dxnaUElooLlBDgkjoGpNKMJ7QUUYz3B2C9d5EQvKZVAzZVAjKenHZTMtzP2h1aI/F9DH3pdwGh3smmPl5JrvKlFS0xEWVF/JHm8+aw1AZp8F/DxceMmEXfaXoC6bed5STAKMb+xHwOHWrNdl6Kp2CY5sScBo+3e/M0OD4adwsI3i029p8i/pKHC4KIwTpZZGo7Ox9WwASLtKMYDFTAhmTz0jy5/Qua3ctZWqQLaW61qrdmAmtuiYYx2aNHnGRBoa71J8+D/xxXudDAqtNv//zUUX3Mr3LPa+4gCiFvrvnZOl5E4+54/YEDQhVVO4um/ng6x9BuAUDMuOS+4tsvqubUsQ/imMkuGzJQBQWAg+udnDlvz9NMJp58X9FHQR4ulcRNPQpfSLBLH+9H3gY4GcHB7kmxVvkbDaOTAlZwXJO4LGeeDM+4kSFkM4A3Lh6DUaa9W9j67UDtQ== 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;SFS:(4636009)(376002)(39860400002)(346002)(396003)(136003)(366004)(7696005)(52116002)(6486002)(8936002)(83380400001)(4326008)(16526019)(26005)(66946007)(5660300002)(186003)(2616005)(966005)(36756003)(2906002)(956004)(86362001)(66556008)(54906003)(66476007)(8676002)(316002)(6666004)(478600001)(6916009)(136400200001);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData: RbJMYcvJ1eSGjyUlgPhE6ohZm7Ld+ebMiMUQCOAkbA7kUv6Cv1pqgjiCstqm+6xBMqj0c/c5EEp70tBNBrBKAhlyNN9Cffd3vPgsn0/K7324iYqTk/C4iy3KIX3phILJ5o+AWoy+JbejnnJqam+2fwVC7cGPCZDcc0Xg55C9Fqn7f/+/bus5yeQUs1qEZfOD9YOpX0awtUtSg38Og1py8sBs9ZrZB6fK8QIlcEGpQeGoWTrhdrRS81tM8UwDPUVX1WoP/75rE43hjj5hiUfEwCGIwbgCoNdp8BeWw/ruYjIGlD0g00Usc4fdVjqQZpwBcgDR4nZHGc0zooyjITEXS5ve8lsOxYxQt144l19+2Dg3zYN4QsqXzCfLV2q7V2YT9v3NGDkZTuIjgWOkAQ6zahB5lxFB9rU2uGGRKE03wwUu6lLTwLFQ31l3dORJaB1Yy2HZuYMtHRtNZlfGIHN7ohfwJ3Gh+ddait0UvHg+wNINoTGQ6DLyCt6tHq0ieY4rds0Uc8XMuLUZP1LgUGKj1+QpW28dA8tPL617HcBXeu+183Z76un69/J+7VPCeMA4mEt+892AleaoB6Mog7yMhYSEzK3Jq1IjPdYIcGsBJc4Rb0Qs4k4x16YPpa3xSRP9ImOz9CFKURyW+1djwzJq4g== X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-Network-Message-Id: a31ec47b-34b8-4e69-bf31-08d83f8cc6f7 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:28:34.1780 (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: JZz2yoibSGYtzZ6dHWQPOI9myJqbpht33iRb72efmDbazoI6xoUCvUbOP329IJvPBy36s02u/TUha4b0yjt87A== X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM6PR12MB4220 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 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 | 182 ++++++++++++++++++-- 2 files changed, 173 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 =20 [Ppis] gEfiTemporaryRamSupportPpiGuid # PPI ALWAYS_PRODUCED =20 [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..169c04b9cec7 100644 --- a/OvmfPkg/Sec/SecMain.c +++ b/OvmfPkg/Sec/SecMain.c @@ -24,6 +24,9 @@ #include #include #include +#include +#include +#include =20 #include =20 @@ -34,6 +37,10 @@ typedef struct _SEC_IDT_TABLE { IA32_IDT_GATE_DESCRIPTOR IdtTable[SEC_IDT_ENTRY_COUNT]; } SEC_IDT_TABLE; =20 +typedef struct _SEC_SEV_ES_WORK_AREA { + UINT8 SevEsEnabled; +} SEC_SEV_ES_WORK_AREA; + VOID EFIAPI SecStartupPhase2 ( @@ -712,6 +719,120 @@ FindAndReportEntryPoints ( return; } =20 +/** + Handle an SEV-ES/GHCB protocol check failure. + + Notify the hypervisor using the VMGEXIT instruction that the SEV-ES gues= t + 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 =3D 0; + Msr.GhcbTerminate.Function =3D GHCB_INFO_TERMINATE_REQUEST; + Msr.GhcbTerminate.ReasonCodeSet =3D GHCB_TERMINATE_GHCB; + Msr.GhcbTerminate.ReasonCode =3D 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 hyperviso= r + 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 =3D 0; + Msr.GhcbInfo.Function =3D GHCB_INFO_SEV_INFO_GET; + AsmWriteMsr64 (MSR_SEV_ES_GHCB, Msr.GhcbPhysicalAddress); + + AsmVmgExit (); + + Msr.GhcbPhysicalAddress =3D AsmReadMsr64 (MSR_SEV_ES_GHCB); + + if (Msr.GhcbInfo.Function !=3D GHCB_INFO_SEV_INFO) { + SevEsProtocolFailure (GHCB_TERMINATE_GHCB_GENERAL); + } + + if (Msr.GhcbProtocol.SevEsProtocolMin > Msr.GhcbProtocol.SevEsProtocolMa= x) { + 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 =3D FixedPcdGet32 (PcdOvmfSecGhcbBase); + AsmWriteMsr64 (MSR_SEV_ES_GHCB, Msr.GhcbPhysicalAddress); + + Ghcb =3D Msr.Ghcb; + SetMem (Ghcb, sizeof (*Ghcb), 0); + + // + // Set the version to the maximum that can be supported + // + Ghcb->ProtocolVersion =3D MIN (Msr.GhcbProtocol.SevEsProtocolMax, GHCB_V= ERSION_MAX); + Ghcb->GhcbUsage =3D GHCB_STANDARD_USAGE; +} + +/** + Determine if SEV-ES is active. + + During early booting, SEV-ES support code will set a flag to indicate th= at + SEV-ES is enabled. Return the value of this flag as an indicator that SE= V-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 =3D (SEC_SEV_ES_WORK_AREA *) FixedPcdGet32 (PcdSevEsWorkAr= eaBase); + + return ((SevEsWorkArea !=3D NULL) && (SevEsWorkArea->SevEsEnabled !=3D 0= )); +} + VOID EFIAPI SecCoreStartupWithStack ( @@ -737,8 +858,56 @@ SecCoreStartupWithStack ( Table[Index] =3D 0; } =20 + // + // Initialize IDT - Since this is before library constructors are called= , + // we use a loop rather than CopyMem. + // + IdtTableInStack.PeiService =3D NULL; + for (Index =3D 0; Index < SEC_IDT_ENTRY_COUNT; Index ++) { + UINT8 *Src; + UINT8 *Dst; + UINTN Byte; + + Src =3D (UINT8 *) &mIdtEntryTemplate; + Dst =3D (UINT8 *) &IdtTableInStack.IdtTable[Index]; + for (Byte =3D 0; Byte < sizeof (mIdtEntryTemplate); Byte++) { + Dst[Byte] =3D Src[Byte]; + } + } + + IdtDescriptor.Base =3D (UINTN)&IdtTableInStack.IdtTable; + IdtDescriptor.Limit =3D (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 constru= ctors + // perform some functions that result in #VC exceptions being generate= d. + // + // Due to this code executing before library constructors, *all* libra= ry + // 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, o= nly + // 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 an= d + // that the library instance not trigger any #VC exceptions. + // + AsmWriteIdtr (&IdtDescriptor); + InitializeCpuExceptionHandlers (NULL); + } + ProcessLibraryConstructorList (NULL, NULL); =20 + 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 +920,6 @@ SecCoreStartupWithStack ( // InitializeFloatingPointUnits (); =20 - // - // Initialize IDT - // - IdtTableInStack.PeiService =3D NULL; - for (Index =3D 0; Index < SEC_IDT_ENTRY_COUNT; Index ++) { - CopyMem (&IdtTableInStack.IdtTable[Index], &mIdtEntryTemplate, sizeof = (mIdtEntryTemplate)); - } - - IdtDescriptor.Base =3D (UINTN)&IdtTableInStack.IdtTable; - IdtDescriptor.Limit =3D (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 --=20 2.28.0