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 []) by mx.groups.io with SMTP id smtpd.web11.1675.1580857385855360254 for ; Tue, 04 Feb 2020 15:03:07 -0800 Authentication-Results: mx.groups.io; dkim=pass header.i=@amdcloud.onmicrosoft.com header.s=selector2-amdcloud-onmicrosoft-com header.b=GpodL1I0; 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=cyM8LlysJy32+seUojcG2h0okkhvTgThB7tlsyXCvssAoDQ3fjRUERNnI2VtocS47m5y1skOZlHJKrJVxA0VEahf7GyRI33jAiLSSYX1REnBKzE+xCvdb6DDYw9ROnGPigJrqol4LYYbvVwsChTqEkB1J5Ij7usK2u8KR4JNZo7mT+4QJAG1D+JQkNOrJLpMx+S+D8gLRm1NwZaVU2rVNEJlwL4gEZcLsjSxW1+TFUO8dXH5sz1kSZFqFitESymDM5c/RxsVHfy1ohQc9c/u47lv+yyV875JaJ0I+fxQI18xHIMXmZW6Hp/BXyXUOnJ12ZkLIZy9ow/P5BPIgDLAkQ== 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=FPJ+maiqB+MGO+G/y6fU6OXTtJFjducuKZXQ/HBbtTc=; b=n+U3Oa84GZHqbKUvuRSfBffkVijx2vSzZIhk3pv4fDWJgUAM0RJ+DTsZCZfR7BO2LUQHgZuxlYeCKl/oJCZeIsaXjZmMoATge2RNj/UmlQa8QcGzbNWHtkyhzZA8+lTmH4ueVbrXVCYxEVg6237ifdlwwxbP4TONtTjPRT1PnfeZnXoFWLRHeBpkuMhhh5rsmW/O3zBNFG+SeTyOATSrU5rxnix5xaRQMRFwodXoD90LAQlmOmq/eSWf0HwIGzuwNjDnQc0V/+Czvqs5bBTPdsOj8hcKWnbgbNBV5jwM/bBj8ADSvh6RipY0/TzqsI+7U7Gh3dU1fv/Cj3t4GrjbAw== 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=FPJ+maiqB+MGO+G/y6fU6OXTtJFjducuKZXQ/HBbtTc=; b=GpodL1I0DkYrOL0ek/62JQ+/lf9WgaZD6hngCDJVk2TfmXJjl05MDmKuH21ohhp2+7GZ2GF/n1J9KoZAuXP3jJBhPzOUJS6XYF+XqE4F8xcy656wyjrHqj4OAPq9Hy5pBO+xEtkMVJrrvKcTXNDiEu6c2xib67AD+sO0/8XW3Rs= Authentication-Results: spf=none (sender IP is ) smtp.mailfrom=Thomas.Lendacky@amd.com; Received: from DM6PR12MB3163.namprd12.prod.outlook.com (20.179.71.154) by DM6PR12MB3930.namprd12.prod.outlook.com (10.255.174.19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.2686.29; Tue, 4 Feb 2020 23:03:05 +0000 Received: from DM6PR12MB3163.namprd12.prod.outlook.com ([fe80::a0cd:463:f444:c270]) by DM6PR12MB3163.namprd12.prod.outlook.com ([fe80::a0cd:463:f444:c270%7]) with mapi id 15.20.2707.020; Tue, 4 Feb 2020 23:03:05 +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 v4 32/40] OvmfPkg/Sec: Add #VC exception handling for Sec phase Date: Tue, 4 Feb 2020 17:01:36 -0600 Message-Id: <2af37abfa288fb07caf3bdbfe694278caad203ac.1580857303.git.thomas.lendacky@amd.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: References: X-ClientProxiedBy: SN6PR16CA0062.namprd16.prod.outlook.com (2603:10b6:805:ca::39) To DM6PR12MB3163.namprd12.prod.outlook.com (2603:10b6:5:15e::26) Return-Path: thomas.lendacky@amd.com MIME-Version: 1.0 Received: from tlendack-t1.amd.com (165.204.77.1) by SN6PR16CA0062.namprd16.prod.outlook.com (2603:10b6:805:ca::39) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.2686.32 via Frontend Transport; Tue, 4 Feb 2020 23:02:35 +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: cef2e05f-f7dc-4b70-80d7-08d7a9c65362 X-MS-TrafficTypeDiagnostic: DM6PR12MB3930:|DM6PR12MB3930: X-MS-Exchange-Transport-Forked: True X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:10000; X-Forefront-PRVS: 03030B9493 X-Forefront-Antispam-Report: SFV:NSPM;SFS:(10009020)(4636009)(39860400002)(366004)(376002)(136003)(396003)(346002)(199004)(189003)(4326008)(478600001)(6916009)(81156014)(2616005)(956004)(81166006)(8676002)(966005)(316002)(54906003)(2906002)(6486002)(66476007)(66556008)(52116002)(7696005)(186003)(26005)(16526019)(5660300002)(86362001)(36756003)(66946007)(8936002);DIR:OUT;SFP:1101;SCL:1;SRVR:DM6PR12MB3930;H:DM6PR12MB3163.namprd12.prod.outlook.com;FPR:;SPF:None;LANG:en;PTR:InfoNoRecords;A:1;MX:1; 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: DdjiXqxAu2IIk7Hs8Dwq69NZwnOLZpqDlIJBI9o4g1zIcYqCYSTHIaeGRBv02XWbZKvj3xMfEXEGYoUGXfR+ELCQ4XneE+QqQ6e3mfK3kpvt9RMZ51LDgeUQintJwC+cz4swkDfsppik2andUW64h21WrmlYtORrlSFP04/O0UqNoIBDNYaHDNkqC3bAnwfu3zvLgaN6xPe3G86ZlNy9SQcNvGUPBHOnodWamrmh5TWLQuub1CyNvj6dtV5MqklHGpQnQFEGgTZQ4aXNNv1npYOqJIQC0ItLiBwSa31H6KMSdOSzuc7PrT/nJSj72CPV81udpmKCL0ltKW2jP0fmQaZ8CSJ0ienxgh3fbkTtnFQ0wnCb1OUakSJ71LMrnrq7O/q1ijc/ocNqyK8vN46G37q2msaRVCxS9MJ97LyMOp7/MoTn2jPyz13ZE+7n3iynuUCahYK1iqbSzios4hahZkqM+Wohh7K3rDsVCn87Sz8VBmXguTSxjNH6nMeapwwCqPFGIVlNabqTCv5cEq2GZg== X-MS-Exchange-AntiSpam-MessageData: 6tXWzl1AHnufQ5yc6MYivG0ffGH41GLeu8IWLyQ9IIetAcW9fPnkh28kHQgr0r8GKIF09+9xEF4X2aze2unHMnehJCrNe7t0aQXW+1gE79l50f0WUXfNk1rMm1PS8A4wQXjdPN11tLT6xIL7CszzLQ== X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-Network-Message-Id: cef2e05f-f7dc-4b70-80d7-08d7a9c65362 X-MS-Exchange-CrossTenant-OriginalArrivalTime: 04 Feb 2020 23:02:36.6456 (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: O4oqfwlzWBG8XIOuPc+lpB7nzAAsd1DM9To9TPFLpyJHw95AejMFAmO3OEB2f4w3JF3sINZogsUKKXZkHdMbtw== X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM6PR12MB3930 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. Cc: Jordan Justen Cc: Laszlo Ersek Cc: Ard Biesheuvel Signed-off-by: Tom Lendacky --- OvmfPkg/Sec/SecMain.inf | 2 ++ OvmfPkg/Sec/SecMain.c | 76 ++++++++++++++++++++++++++++++++++------- 2 files changed, 65 insertions(+), 13 deletions(-) diff --git a/OvmfPkg/Sec/SecMain.inf b/OvmfPkg/Sec/SecMain.inf index 63ba4cb555fb..401d06039dd3 100644 --- a/OvmfPkg/Sec/SecMain.inf +++ b/OvmfPkg/Sec/SecMain.inf @@ -50,11 +50,13 @@ [LibraryClasses] PeCoffExtraActionLib ExtractGuidedSectionLib LocalApicLib + CpuExceptionHandlerLib [Ppis] gEfiTemporaryRamSupportPpiGuid # PPI ALWAYS_PRODUCED [Pcd] + gUefiCpuPkgTokenSpaceGuid.PcdSevEsWorkAreaBase gUefiOvmfPkgTokenSpaceGuid.PcdOvmfPeiMemFvBase gUefiOvmfPkgTokenSpaceGuid.PcdOvmfPeiMemFvSize gUefiOvmfPkgTokenSpaceGuid.PcdOvmfDxeMemFvBase diff --git a/OvmfPkg/Sec/SecMain.c b/OvmfPkg/Sec/SecMain.c index bae9764577f0..2bab7128ade2 100644 --- a/OvmfPkg/Sec/SecMain.c +++ b/OvmfPkg/Sec/SecMain.c @@ -24,6 +24,7 @@ #include #include #include +#include #include @@ -34,6 +35,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 +717,19 @@ FindAndReportEntryPoints ( return; } +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 +755,53 @@ 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()) { + // + // 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 ((EFI_D_INFO, "SecCoreStartupWithStack(0x%x, 0x%x)\n", (UINT32)(UINTN)BootFv, @@ -751,19 +814,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