From nobody Fri Dec 19 19:20:21 2025 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of groups.io designates 66.175.222.12 as permitted sender) smtp.mailfrom=bounce+27952+57868+1787277+3901457@groups.io; arc=fail (BodyHash is different from the expected one) Received: from web01.groups.io (web01.groups.io [66.175.222.12]) by mx.zohomail.com with SMTPS id 1587577402956954.3339563206376; Wed, 22 Apr 2020 10:43:22 -0700 (PDT) Return-Path: X-Received: by 127.0.0.2 with SMTP id bls6YY1788612xMEkmlRXlGK; Wed, 22 Apr 2020 10:43:22 -0700 X-Received: from NAM11-BN8-obe.outbound.protection.outlook.com (NAM11-BN8-obe.outbound.protection.outlook.com []) by mx.groups.io with SMTP id smtpd.web12.504.1587577397852627360 for ; Wed, 22 Apr 2020 10:43:21 -0700 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=Ccan+UjTnyaLS64C5PWtMD2FaUJzPU4AhIo2aGjIHGLtpSo2vWkJRyC1WAm9g7q4+AdWJ6yunV6nZ4GddfJRqsAb6yf3tTXpE80DjTd8v3Ja4bNE7wuJrfZiQoaR2sQW4IrugFtvF+hD+T278TCezLahoZ3xXOC6QNBuDQknHz2ymmgVgRVpENjXBQvPeUsXbTaJet5UnRgpDXdZat00HJoQ4C1Xto8wNvWRvk3+T4ZxYY+f55U5G7FX5gO2uJxVLl57TebYN2FGVmgXZjwVd8B6z064vQP7lHb28RTWg/bvBjJklu1HgV3hosyF9IF9Qc4Y5t6LdldP9vr0iTBWtg== 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=8mREBz8F2IASBuNJ4eUdTIiZR9kO+U7JXwcBf56acoI=; b=E4m5xRixzQPhGadY0X1cS5iNCniFqJ7ZvanoYk3HTL7vx+CwxpGhRLN6q/ILuq07QlrnCYmMQLoBnU5d2H7IqdFPt3d4ZSDuj4aDSNv+A+tooV9yUlYIiegD+Man6x856qChofzSOG/kTmTU58Gu3MnJod/xndTXp591fr3k5ApmQRq155rBZD6LVCOXNzGvExd5luhX4WVTL+IxuYn398D8gYXaGCmeu2nHjkvTRZApWXGonaJ7VkoIwlWLpz+RrV4oFSjuOFlI9E1p104032iUXUcuAEyFD00YWEE9xOj/zCiuFIdAmCE8TZibpQTOG61wQDKsM/2AGJDgYlrkuA== 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 X-Received: from DM6PR12MB3163.namprd12.prod.outlook.com (2603:10b6:5:15e::26) by DM6PR12MB3673.namprd12.prod.outlook.com (2603:10b6:5:1c5::11) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.2921.29; Wed, 22 Apr 2020 17:43:19 +0000 X-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:43:19 +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: [edk2-devel] [PATCH v7 37/43] OvmfPkg/QemuFlashFvbServicesRuntimeDxe: Bypass flash detection with SEV-ES is enabled Date: Wed, 22 Apr 2020 12:41:52 -0500 Message-Id: <402576b2e77d27c58d7567b2a6c1466f49c3e0bb.1587577317.git.thomas.lendacky@amd.com> 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) MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 X-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:51 +0000 X-Originating-IP: [165.204.77.1] X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-HT: Tenant X-MS-Office365-Filtering-Correlation-Id: f546b058-e203-401a-f1d8-08d7e6e49501 X-MS-TrafficTypeDiagnostic: DM6PR12MB3673:|DM6PR12MB3673: X-MS-Exchange-Transport-Forked: True X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:10000; Received-SPF: pass (zohomail.com: domain of groups.io designates 66.175.222.12 as permitted sender) client-ip=66.175.222.12; envelope-from=bounce+27952+57868+1787277+3901457@groups.io; helo=web01.groups.io; Received-SPF: None (protection.outlook.com: amd.com does not designate permitted sender hosts) X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam-Message-Info: b7Ipx7UOcMfU5CXwpl23y1w96AZxQ8yTnqsx5Jm6pSlgA2LAwXMVdQj8eNz1jjbO/Y6OPw0DlMrTT6m8AvE/hr615LWk796UxmMJdhgPZzIbSVz3B074nq/d/YrUPr3Gk01QDxFAr0Gwp+6viNlShXSmr5sfFICBBfBVwzJTOoXem+lSa0ZD5fapEFqxfxIZUe7RJUDGltScyK4YfNQ9Jx4aS0HwpyVb9ITlBWseKSVL7QfNrYm+VQYcofO3AmcblD+InUkeRR11Viu3oRkZja9DiYLEbhfxkLUbp/N7JamJltCS0FpYxjpUF2O2uKfsc07T43uOQI4SR1B8kfDXQOiwKFfrMqvwxVsIm5/ZMQ9cSUcFS4rp4iIQMlVkOwqjD7KeqH//djxRw+wfJ/hgCK5KsgiioBVdDnsNzdrckXJRHEW/73r7n0IOtBJocvBiJX47bzmAVnQTrzPjZmF5LNNbCzf8HQ8ofDarseqt+ajeAGl02rhfPgmoK2BQ1baJn4HpR2nJArihLCQDlw8791UEspSj+xIta250OV0QfWJecpzhDeR/UhfqWBUumrhNnE5FLdtsb3xzr6ySlVrvGM74VBIsGi/gnXGsnnH8gec= X-MS-Exchange-AntiSpam-MessageData: iiANcFcuL9D86FnoKmE5fuoabfSusU/SKAj/gfyvqRC6iaLtiJ/QtsUjIQ4eF6VdSM/2AFyx464KAah04HvtJG4GhguZWhL7bzDujRFkFSsw/jO6LsbgtzMKPOhn5ZSjyilyjnmMptf5xdXj18ZBNw== X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-Network-Message-Id: f546b058-e203-401a-f1d8-08d7e6e49501 X-MS-Exchange-CrossTenant-OriginalArrivalTime: 22 Apr 2020 17:42:52.4568 (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: XLYXolYJvsZFB3J8nlE6xHn9hCW0MT66mdlLItQD+neBqzqZrn8DXs6cCIidz4+2Qog9eTNGRLh6QBQFEv6sxQ== X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM6PR12MB3673 Precedence: Bulk List-Unsubscribe: Sender: devel@edk2.groups.io List-Id: Mailing-List: list devel@edk2.groups.io; contact devel+owner@edk2.groups.io Reply-To: devel@edk2.groups.io,thomas.lendacky@amd.com X-Gm-Message-State: 260M4lga16wPGAK0UqLQtVGgx1787277AA= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=groups.io; q=dns/txt; s=20140610; t=1587577402; bh=mfQDOnVBc8S+JBpOlvPLrbG07eBQILmCjp+SKvAOsSU=; h=Cc:Content-Type:Date:From:Reply-To:Subject:To; b=g+6vwkT4QtRgeyDM2wE1xuNWfOcd6Ysj3+9LYI+9em4UB2KoVDUJuCgwmTqNfUo56/G 9iTI4hmkeg9Xdixdl6pdig4j+2Nt549H20veXAG20sG/jLTOxz2nNexEGDF1LMLftPNnK /vPqr3vvhaMul/yaXpjkwa9wZNJRBKjiSwk= X-ZohoMail-DKIM: pass (identity @groups.io) Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" 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 VmgMmioWrite function from the VmgExitLib library 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 | 22 ++++++++++++++++++ .../QemuFlashSmm.c | 16 +++++++++++++ 5 files changed, 73 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 c81c58972bf2..358bce3336f2 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 ((EFI_D_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 mapped + // read-only, the nested page table entry is read-only. The check below + // 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..4d51183fc956 100644 --- a/OvmfPkg/QemuFlashFvbServicesRuntimeDxe/QemuFlashDxe.c +++ b/OvmfPkg/QemuFlashFvbServicesRuntimeDxe/QemuFlashDxe.c @@ -10,6 +10,8 @@ **/ =20 #include +#include +#include =20 #include "QemuFlash.h" =20 @@ -32,3 +34,23 @@ 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 ()) { + VmgMmioWrite ((UINT8 *) Ptr, &Value, 1); + } 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.17.1 -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D- Groups.io Links: You receive all messages sent to this group. View/Reply Online (#57868): https://edk2.groups.io/g/devel/message/57868 Mute This Topic: https://groups.io/mt/73201947/1787277 Group Owner: devel+owner@edk2.groups.io Unsubscribe: https://edk2.groups.io/g/devel/unsub [importer@patchew.org] -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-