From mboxrd@z Thu Jan 1 00:00:00 1970 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 Authentication-Results: mx.groups.io; dkim=pass header.i=@amdcloud.onmicrosoft.com header.s=selector2-amdcloud-onmicrosoft-com header.b=ynvkkKdV; 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=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 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=8mREBz8F2IASBuNJ4eUdTIiZR9kO+U7JXwcBf56acoI=; b=ynvkkKdVEr/Cu3IT4Gh5/biFsgMOe+oFAarZ2MnFdb4wLdY1j7a5lrV1m6Q9awAQ51IT8uyRuSCpxb5tN9+9NRQPFpuTMMlN14u7WPJUdrmozq2xULCBpi9JIFwVGH5/17QM7MAN5ZQrtlxPLtvYfmQDU+hVNiyjWGeqv0F+eiI= Authentication-Results: spf=none (sender IP is ) smtp.mailfrom=Thomas.Lendacky@amd.com; 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 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: [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> X-Mailer: git-send-email 2.17.1 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) 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 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-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: 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; X-Forefront-PRVS: 03818C953D X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:DM6PR12MB3163.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFTY:;SFS:(10009020)(4636009)(346002)(136003)(396003)(366004)(39860400002)(376002)(16526019)(186003)(8936002)(8676002)(956004)(4326008)(316002)(6916009)(86362001)(2616005)(36756003)(26005)(52116002)(7696005)(5660300002)(2906002)(478600001)(81156014)(966005)(66556008)(66946007)(66476007)(6486002)(54906003)(136400200001)(213903007);DIR:OUT;SFP:1101; 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: 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 Content-Type: text/plain BZ: https://bugzilla.tianocore.org/show_bug.cgi?id=2198 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.inf 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 [LibraryClasses] @@ -52,6 +53,7 @@ [LibraryClasses] UefiBootServicesTableLib UefiDriverEntryPoint UefiRuntimeLib + VmgExitLib [Guids] gEfiEventVirtualAddressChangeGuid # ALWAYS_CONSUMED diff --git a/OvmfPkg/QemuFlashFvbServicesRuntimeDxe/QemuFlash.h b/OvmfPkg/QemuFlashFvbServicesRuntimeDxe/QemuFlash.h index f1afabcbe6ae..219d0d6e83cf 100644 --- a/OvmfPkg/QemuFlashFvbServicesRuntimeDxe/QemuFlash.h +++ b/OvmfPkg/QemuFlashFvbServicesRuntimeDxe/QemuFlash.h @@ -89,5 +89,18 @@ QemuFlashBeforeProbe ( IN UINTN FdBlockCount ); +/** + 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 diff --git a/OvmfPkg/QemuFlashFvbServicesRuntimeDxe/QemuFlash.c b/OvmfPkg/QemuFlashFvbServicesRuntimeDxe/QemuFlash.c index c81c58972bf2..358bce3336f2 100644 --- a/OvmfPkg/QemuFlashFvbServicesRuntimeDxe/QemuFlash.c +++ b/OvmfPkg/QemuFlashFvbServicesRuntimeDxe/QemuFlash.c @@ -9,6 +9,7 @@ #include #include +#include #include #include "QemuFlash.h" @@ -80,6 +81,21 @@ QemuFlashDetected ( DEBUG ((EFI_D_INFO, "QEMU Flash: Attempting flash detection at %p\n", Ptr)); + 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 that + // the FD appears as ROM and not as FLASH, but report FLASH anyway because + // FLASH behavior can be simulated using VMGEXIT. + // + DEBUG ((DEBUG_INFO, + "QEMU Flash: SEV-ES enabled, assuming FD behaves as FLASH\n")); + return TRUE; + } + OriginalUint8 = *Ptr; *Ptr = CLEAR_STATUS_CMD; ProbeUint8 = *Ptr; @@ -181,8 +197,9 @@ QemuFlashWrite ( // Ptr = QemuFlashPtr (Lba, Offset); for (Loop = 0; Loop < *NumBytes; Loop++) { - *Ptr = WRITE_BYTE_CMD; - *Ptr = Buffer[Loop]; + QemuFlashPtrWrite (Ptr, WRITE_BYTE_CMD); + QemuFlashPtrWrite (Ptr, Buffer[Loop]); + Ptr++; } @@ -190,7 +207,7 @@ QemuFlashWrite ( // Restore flash to read mode // if (*NumBytes > 0) { - *(Ptr - 1) = READ_ARRAY_CMD; + QemuFlashPtrWrite (Ptr - 1, READ_ARRAY_CMD); } return EFI_SUCCESS; diff --git a/OvmfPkg/QemuFlashFvbServicesRuntimeDxe/QemuFlashDxe.c b/OvmfPkg/QemuFlashFvbServicesRuntimeDxe/QemuFlashDxe.c index 5aabe9d7b59c..4d51183fc956 100644 --- a/OvmfPkg/QemuFlashFvbServicesRuntimeDxe/QemuFlashDxe.c +++ b/OvmfPkg/QemuFlashFvbServicesRuntimeDxe/QemuFlashDxe.c @@ -10,6 +10,8 @@ **/ #include +#include +#include #include "QemuFlash.h" @@ -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 = Value; + } +} diff --git a/OvmfPkg/QemuFlashFvbServicesRuntimeDxe/QemuFlashSmm.c b/OvmfPkg/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 = Value; +} -- 2.17.1