From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from NAM04-SN1-obe.outbound.protection.outlook.com (NAM04-SN1-obe.outbound.protection.outlook.com [40.107.70.84]) by mx.groups.io with SMTP id smtpd.web11.58178.1595863912828154074 for ; Mon, 27 Jul 2020 08:31:52 -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=OyyqZNpd; spf=none, err=SPF record not found (domain: amd.com, ip: 40.107.70.84, mailfrom: thomas.lendacky@amd.com) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=kAfVZMmzK7Eyw0en4w9NwYWYn6CxtB7IxnH/u+ytpS/YUqto8pzTu8HftYenoPlOzLLB5251iwE+PHmAIfkP+IAtq9weqms6BsRa736/zLo287K0MiKHcAvVawZcKG8mVLvODQT9VWqd6tQo3XHySD4o+rlwO9Tw3+4A52v3d7OsPFFyfu7PV6YlwYxZwsm2gLHcj+Gv8ZUrd+uhsCZPQDG23qLp6o4yuebhA1w3CaO1yr7jGQI3hiYkS2HO+fMOPaS5YjZ8Ou7XuTb7hzgmuwspT3fdIckXb0T0jic5P6CJXkl8oPHjd7uH55nJV9s2w+TqsFC4fQ2iV6KTdd8efw== 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=9SL7ZuvVbtegSaUIJvRjeIedgAr9HaaB2JEMJds61HY=; b=m3xjgOCF05aOY88c0h7UWiIB7Ows26d0sAvjFbO3gVuhDtAysO4WJG2ifvQNF5F7m/B1x5WoC/q/8NOPCmuuzockDZRxJJg2o6o/EP4Ke1aSsZ8c6QV2Aa/yTF51bEKEO0amx+Z/xtqH0zAJ9BbcC51t9fxnBibzdp6imlclE7fzwqK9u3dNuz9gseiARbMcQbTa+kXQtQYi32Px1DvtPuUXLUia7PRQIe2/Yw6966/4kAFtn67qxkX7HoyVc/7ClaBcEJRkCr1wkAsLx4eomIMmOBooTb0R6OC1W2OWXa/HISfPRO4i9plLCrWFZgyyxS0IILDpnbXy6TjjJmjN8A== 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=9SL7ZuvVbtegSaUIJvRjeIedgAr9HaaB2JEMJds61HY=; b=OyyqZNpd1RyNwCZ5pki2TRvT/b18UrTwMFUELwzAB7/rUEzhM2k8kUxNQb4jAmdP0+S3DocX0VFuo/XQJRnUFmmMFPQPztG6Wbm+WvHeUuxEhqwK1xYhu8hEkuzP/gmt05pLB5ysY5P1OxFZ/erzt406NmLHELy70exZw3Eif4c= 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 DM5PR12MB1163.namprd12.prod.outlook.com (2603:10b6:3:7a::18) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3216.25; Mon, 27 Jul 2020 15:31:51 +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.3216.031; Mon, 27 Jul 2020 15:31:51 +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 v12 39/46] OvmfPkg/QemuFlashFvbServicesRuntimeDxe: Bypass flash detection with SEV-ES Date: Mon, 27 Jul 2020 10:26:20 -0500 Message-ID: <5ad304ebdff11d09e9219dc625613d5ffbb5d5ed.1595863587.git.thomas.lendacky@amd.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: References: X-ClientProxiedBy: SN4PR0501CA0089.namprd05.prod.outlook.com (2603:10b6:803:22::27) 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 SN4PR0501CA0089.namprd05.prod.outlook.com (2603:10b6:803:22::27) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3239.9 via Frontend Transport; Mon, 27 Jul 2020 15:31:50 +0000 X-Mailer: git-send-email 2.27.0 X-Originating-IP: [165.204.77.1] X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-HT: Tenant X-MS-Office365-Filtering-Correlation-Id: 2b1acdc9-a1c9-4ce8-2a19-08d832422f19 X-MS-TrafficTypeDiagnostic: DM5PR12MB1163: 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: 8GF/RSt+BGkupp9HIWfRXO/IrvnuYHjFvD48d8u15u0WARkW02p1S58xhK2BSpBYXg+CmKJWiXCUBroY9MYh4VCkhJQ3O7NJlHM1gHWMiqys8E3NPQp/zazO+NbL/Cf5KfyeaozmkDJs8V+O+vIww1KL6gEuj6XmdXtMlHrTkgykg9Oo9h1szpHYmyUnTwRj7ed2Rc4RinwLTIEmPsDIh1PVs1ilY2six+nsVjtBW7UvQcJfJ3s3vqnHBUrJyZkhqut8g2zJzVrqTs4rRoIr1DpRflLWWzg7tAwKjpMAqmylrVkj2zA8rzYDnhMzrqg0oU/pUiOjfNDyt60uCFv8RtKx/WI3RZ5WmWG+MjSrysH0c888LdI4XXv/L+fo4LeD8iCkUDRSwgZqfO2JgXFcBgstBE1irXWco923tVIKooqvj6nxJfx6zu+J3/G2xDzw2PLFg7S7d8YAjcSoRPwloc89Jr9QLym1kw9r/7mpDUrpdm/wJJi/02WRZT/Krblf 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)(346002)(396003)(136003)(366004)(26005)(7696005)(16526019)(52116002)(186003)(66946007)(2616005)(66556008)(316002)(36756003)(956004)(966005)(5660300002)(66476007)(54906003)(6666004)(478600001)(2906002)(4326008)(6486002)(8676002)(8936002)(6916009)(83380400001)(86362001)(213903007)(136400200001);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData: 9TFZzdFO8AO1gdvrr6ojZWnX+/+puMkFepmN4BmTH2dckGWjGcHPwu75jP2xWNmLm16dV4AugfmFlPcnxZTIAUv5HjhtFCFxDPjnWMK+d3M9OpkIpupRr6vH7Gb1M2WJnoYwEHA9sgJ9Qn0r/xsrzh4iB4lv0iwEd0jvAYpRo7VKtXjFScls99W5s+fyByrycBPbz0K5Rv/TDYismKP5QkhEslbeiR2Ldb3kURZT77Kt/cdO4Pu2857dSxd+K7N0oGIrG/4IJYMN+cRKlpgalkT2mxmAMI637hP79nd3jb8UakLnBlLa8eTkQfNzWZ4KshVlIxGKfjLfkIs/PVTGuVA6nV+T849HZPE5qMoTrq7uuNNcgaSP/HftJAREl3zdeIjPOrRcbgSBWdjoLqqeJ9UwMj7EvHFdpZmMe9e8e3xhth1JfPx5fKrB4U3n8k8i330Y5P+/87CKuwvGIs+PlWqtI7GC0NoZBIrNYUObGKA= X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-Network-Message-Id: 2b1acdc9-a1c9-4ce8-2a19-08d832422f19 X-MS-Exchange-CrossTenant-AuthSource: DM5PR12MB1355.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 27 Jul 2020 15:31:51.3963 (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: vMdEynGB6QKUxxohQPavNe7//MT7Q8AumGvylsqaJhDx7ERZGGvYdbtT7rKBhhqu4S7YivZE//2dP4IuJTVOaA== X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM5PR12MB1163 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 The flash detection routine will attempt to determine how the flash device behaves (e.g. ROM, RAM, Flash). But when SEV-ES is enabled and the flash device behaves as a ROM device (meaning it is marked read-only by the hypervisor), this check may result in an infinite nested page fault because of the attempted write. Since the instruction cannot be emulated when SEV-ES is enabled, the RIP is never advanced, resulting in repeated nested page faults. When SEV-ES is enabled, exit the flash detection early and assume that the FD behaves as Flash. This will result in QemuFlashWrite() being called to store EFI variables, which will also result in an infinite nested page fault when the write is performed. In this case, update QemuFlashWrite() to use the VMGEXIT MMIO write support to have the hypervisor perform the write without having to emulate the instruction. Cc: Jordan Justen Cc: Laszlo Ersek Cc: Ard Biesheuvel Reviewed-by: Laszlo Ersek Signed-off-by: Tom Lendacky --- .../FvbServicesRuntimeDxe.inf | 2 + .../QemuFlash.h | 13 ++++++ .../QemuFlash.c | 23 +++++++++-- .../QemuFlashDxe.c | 40 +++++++++++++++++++ .../QemuFlashSmm.c | 16 ++++++++ 5 files changed, 91 insertions(+), 3 deletions(-) diff --git a/OvmfPkg/QemuFlashFvbServicesRuntimeDxe/FvbServicesRuntimeDxe.i= nf b/OvmfPkg/QemuFlashFvbServicesRuntimeDxe/FvbServicesRuntimeDxe.inf index 72cabba4357d..8bb2325157ea 100644 --- a/OvmfPkg/QemuFlashFvbServicesRuntimeDxe/FvbServicesRuntimeDxe.inf +++ b/OvmfPkg/QemuFlashFvbServicesRuntimeDxe/FvbServicesRuntimeDxe.inf @@ -38,6 +38,7 @@ [Sources] [Packages] MdePkg/MdePkg.dec MdeModulePkg/MdeModulePkg.dec + UefiCpuPkg/UefiCpuPkg.dec OvmfPkg/OvmfPkg.dec =20 [LibraryClasses] @@ -52,6 +53,7 @@ [LibraryClasses] UefiBootServicesTableLib UefiDriverEntryPoint UefiRuntimeLib + VmgExitLib =20 [Guids] gEfiEventVirtualAddressChangeGuid # ALWAYS_CONSUMED diff --git a/OvmfPkg/QemuFlashFvbServicesRuntimeDxe/QemuFlash.h b/OvmfPkg/Q= emuFlashFvbServicesRuntimeDxe/QemuFlash.h index f1afabcbe6ae..219d0d6e83cf 100644 --- a/OvmfPkg/QemuFlashFvbServicesRuntimeDxe/QemuFlash.h +++ b/OvmfPkg/QemuFlashFvbServicesRuntimeDxe/QemuFlash.h @@ -89,5 +89,18 @@ QemuFlashBeforeProbe ( IN UINTN FdBlockCount ); =20 +/** + Write to QEMU Flash + + @param[in] Ptr Pointer to the location to write. + @param[in] Value The value to write. + +**/ +VOID +QemuFlashPtrWrite ( + IN volatile UINT8 *Ptr, + IN UINT8 Value + ); + #endif =20 diff --git a/OvmfPkg/QemuFlashFvbServicesRuntimeDxe/QemuFlash.c b/OvmfPkg/Q= emuFlashFvbServicesRuntimeDxe/QemuFlash.c index 1b0d6c053f1a..0d29bf701aca 100644 --- a/OvmfPkg/QemuFlashFvbServicesRuntimeDxe/QemuFlash.c +++ b/OvmfPkg/QemuFlashFvbServicesRuntimeDxe/QemuFlash.c @@ -9,6 +9,7 @@ =20 #include #include +#include #include =20 #include "QemuFlash.h" @@ -80,6 +81,21 @@ QemuFlashDetected ( =20 DEBUG ((DEBUG_INFO, "QEMU Flash: Attempting flash detection at %p\n", Pt= r)); =20 + if (MemEncryptSevEsIsEnabled ()) { + // + // When SEV-ES is enabled, the check below can result in an infinite + // loop with respect to a nested page fault. When the memslot is mappe= d + // read-only, the nested page table entry is read-only. The check belo= w + // will cause a nested page fault that cannot be emulated, causing + // the instruction to retried over and over. For SEV-ES, acknowledge t= hat + // the FD appears as ROM and not as FLASH, but report FLASH anyway bec= ause + // FLASH behavior can be simulated using VMGEXIT. + // + DEBUG ((DEBUG_INFO, + "QEMU Flash: SEV-ES enabled, assuming FD behaves as FLASH\n")); + return TRUE; + } + OriginalUint8 =3D *Ptr; *Ptr =3D CLEAR_STATUS_CMD; ProbeUint8 =3D *Ptr; @@ -181,8 +197,9 @@ QemuFlashWrite ( // Ptr =3D QemuFlashPtr (Lba, Offset); for (Loop =3D 0; Loop < *NumBytes; Loop++) { - *Ptr =3D WRITE_BYTE_CMD; - *Ptr =3D Buffer[Loop]; + QemuFlashPtrWrite (Ptr, WRITE_BYTE_CMD); + QemuFlashPtrWrite (Ptr, Buffer[Loop]); + Ptr++; } =20 @@ -190,7 +207,7 @@ QemuFlashWrite ( // Restore flash to read mode // if (*NumBytes > 0) { - *(Ptr - 1) =3D READ_ARRAY_CMD; + QemuFlashPtrWrite (Ptr - 1, READ_ARRAY_CMD); } =20 return EFI_SUCCESS; diff --git a/OvmfPkg/QemuFlashFvbServicesRuntimeDxe/QemuFlashDxe.c b/OvmfPk= g/QemuFlashFvbServicesRuntimeDxe/QemuFlashDxe.c index 5aabe9d7b59c..565383ee26d2 100644 --- a/OvmfPkg/QemuFlashFvbServicesRuntimeDxe/QemuFlashDxe.c +++ b/OvmfPkg/QemuFlashFvbServicesRuntimeDxe/QemuFlashDxe.c @@ -10,6 +10,9 @@ **/ =20 #include +#include +#include +#include =20 #include "QemuFlash.h" =20 @@ -32,3 +35,40 @@ QemuFlashBeforeProbe ( // Do nothing // } + +/** + Write to QEMU Flash + + @param[in] Ptr Pointer to the location to write. + @param[in] Value The value to write. + +**/ +VOID +QemuFlashPtrWrite ( + IN volatile UINT8 *Ptr, + IN UINT8 Value + ) +{ + if (MemEncryptSevEsIsEnabled ()) { + MSR_SEV_ES_GHCB_REGISTER Msr; + GHCB *Ghcb; + + Msr.GhcbPhysicalAddress =3D AsmReadMsr64 (MSR_SEV_ES_GHCB); + Ghcb =3D Msr.Ghcb; + + // + // Writing to flash is emulated by the hypervisor through the use of w= rite + // protection. This won't work for an SEV-ES guest because the write w= on't + // be recognized as a true MMIO write, which would result in the requi= red + // #VC exception. Instead, use the the VMGEXIT MMIO write support dire= ctly + // to perform the update. + // + VmgInit (Ghcb); + Ghcb->SharedBuffer[0] =3D Value; + Ghcb->SaveArea.SwScratch =3D (UINT64) (UINTN) Ghcb->SharedBuffer; + VmgExit (Ghcb, SVM_EXIT_MMIO_WRITE, (UINT64) (UINTN) Ptr, 1); + VmgDone (Ghcb); + } else { + *Ptr =3D Value; + } +} diff --git a/OvmfPkg/QemuFlashFvbServicesRuntimeDxe/QemuFlashSmm.c b/OvmfPk= g/QemuFlashFvbServicesRuntimeDxe/QemuFlashSmm.c index 7eb426e03855..7eb80bfeffae 100644 --- a/OvmfPkg/QemuFlashFvbServicesRuntimeDxe/QemuFlashSmm.c +++ b/OvmfPkg/QemuFlashFvbServicesRuntimeDxe/QemuFlashSmm.c @@ -46,3 +46,19 @@ QemuFlashBeforeProbe ( ); ASSERT_EFI_ERROR (Status); } + +/** + Write to QEMU Flash + + @param[in] Ptr Pointer to the location to write. + @param[in] Value The value to write. + +**/ +VOID +QemuFlashPtrWrite ( + IN volatile UINT8 *Ptr, + IN UINT8 Value + ) +{ + *Ptr =3D Value; +} --=20 2.27.0