From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from NAM11-CO1-obe.outbound.protection.outlook.com (NAM11-CO1-obe.outbound.protection.outlook.com []) by mx.groups.io with SMTP id smtpd.web11.983.1589925154464670122 for ; Tue, 19 May 2020 14:52:36 -0700 Authentication-Results: mx.groups.io; dkim=pass header.i=@amdcloud.onmicrosoft.com header.s=selector2-amdcloud-onmicrosoft-com header.b=2vigP/yN; 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=k7NRwn5ELm6qD7jf/Wxi+C0acWU++7mNwmNq6cHwJdZ72P9lpabxTEFDfeNBuZFFHEJ04QXnudUU0zOgCy0xTz3vbXHQsilsLXfxSRWCLwqCY/cPbo4DmtPXsQB4n9Gi+v1YZQrSGEV9IzncnAtuYPg2AqvwB2wIwhJxSCgoWnO1heYC5yj7pRGlD+bdUGXtDwTmrWZWvi/CPteBJ3w6y9qyOFcbQRFtbyvUgXlEUHC4pIfPaPv1+sgEEOfM93ST731dD2GMW2NXdRi+D3IlwxJ7DPUdm3fFHUcky3Nc0eF8yfROa53r6WU7uCFHr0PjVsTzGaPfnXYUQXT4ORrUKQ== 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=eIlMcsHOhx8qFXHaH8TGYjsdiaFluaWTXfgZbwVlRS8=; b=QKc6XCh6HnPFHN8i2OD3YeY77sq4AoxgIn/IhIj97VAWUhtDbz7tRoEilpPvjQqlBTSljwwT+sW/aBKYg43arpvE8Y0v4dI7qUpqjm2zLAawus5lKm4IzcY3tsQJF8I3CsmjWSeX2m5wJdZgRyHFQWr9QIVG8uVBxdErkf7vnC06AxXjy1YWBcw96NTZq4cgA9vlscSegwAjTBrvq3oB+ZPNHzGAck1tjSnOdRLDp4U3rvSNH41ZrwbcBCP7iuBLivk1QzwffqNLD4HrxFERMwtnXMXnuceFsnWsAf22k90I8k9QIT+UTkp4Wh9zMFFIs39f2NsW6TK/s4yzdaKebw== 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=eIlMcsHOhx8qFXHaH8TGYjsdiaFluaWTXfgZbwVlRS8=; b=2vigP/yNGnPq7VnEHmfQLFmFwS6OBDLY5aiE4wSRt2+ntKXsQgL1sqD/5LZB1+xjxd3/C6LMoGXP6FZqOMLBolnCH6I4EN5bMIr8X8lXiGYlM/+fKl1vM8fLZJL6x3LBB27el1HtqK0TM9zbwDizP+COimjCtVHpwQlmIDx8ckY= 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 DM5PR12MB2504.namprd12.prod.outlook.com (2603:10b6:4:b5::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3000.20; Tue, 19 May 2020 21:52:34 +0000 Received: from DM5PR12MB1355.namprd12.prod.outlook.com ([fe80::4ce1:9947:9681:c8b1]) by DM5PR12MB1355.namprd12.prod.outlook.com ([fe80::4ce1:9947:9681:c8b1%10]) with mapi id 15.20.3000.034; Tue, 19 May 2020 21:52:34 +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 v8 39/46] OvmfPkg/QemuFlashFvbServicesRuntimeDxe: Bypass flash detection with SEV-ES Date: Tue, 19 May 2020 16:51:07 -0500 Message-Id: <4d35621f7ac33a99cd1d473adc559aa4a5aad8c0.1589925074.git.thomas.lendacky@amd.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: References: X-ClientProxiedBy: DM6PR11CA0048.namprd11.prod.outlook.com (2603:10b6:5:14c::25) 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 DM6PR11CA0048.namprd11.prod.outlook.com (2603:10b6:5:14c::25) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3000.25 via Frontend Transport; Tue, 19 May 2020 21:52:06 +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: 7e6414e4-40d7-4633-80fa-08d7fc3ee007 X-MS-TrafficTypeDiagnostic: DM5PR12MB2504: X-MS-Exchange-Transport-Forked: True X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:10000; X-Forefront-PRVS: 040866B734 X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: bg6wVZp1jFj4FgR2saWANWZvvLAoocq2gR9pl9/Dxwx07L+Gx3NOUZZowXNFe/DUuhi18Lb9BkXu6tq2BESAtIEnIr3GzL+xL+0I//L1VcOBGUqlfC/LNWZmTL10AkyID41AxkfYhFNtT9+kjcjnCetnyELJExd9C3bl71xWK7cVUshteOPVn4K1YgXu8QRTEu2fpo3nFtc4Uw9Eyz+dqe8wGwBHAkyooXICEis6Wx+aonQv7DlymtHlqIiuUUKcnjVxKgWCNB6/rR9br3QRpB3/1Bq+mSFJWvMHrgYpC2w8A8ri9BTkU5rrIl/hJFO56bRsHy6u1o3jJdEu9RzUgnq5BBZPGwOIyfqVxWCN3i4zTKoyYnpFCPAGKcBW1N/dSAkNGuxlhfR8j74kYptg2UyvOj7M2ddRiA2t1r2MhA5yI4LDhXaKtgk4hdPiW4iHz3/TKp0eZb+Vi9pk+H+WBZpUMhy6ys95aTbxJo9lSW9gSApYG0ytrm5FgixjTIXhH5iDYF5f0ns7DhfsypfC/M2dDAthJKsjdmgs4Pyczt+7IASgeWQPkkxH2uOuvdCEEh8bPHgfpvZJFngPClVXwSjPf4bE2HFvjyftXFAE8rM= 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)(136003)(346002)(396003)(366004)(316002)(86362001)(2906002)(54906003)(7696005)(52116002)(2616005)(956004)(66946007)(66476007)(66556008)(36756003)(26005)(16526019)(186003)(4326008)(8676002)(5660300002)(966005)(6486002)(8936002)(6916009)(478600001)(136400200001)(213903007);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData: mWOq0wEZi1J/YRsNVPDiSHNUmtscsXm77U31Y9uAADOkRRwlAc5/CpRHQnclNi+vzaznHKUO2DhUXx9mSV5kC0E3rN1zf4eb5ipoRF9zHNk9kLaUNtEKdJLtWTMxUNEiCckZUJadYPg3qea/HE+VcutXql/m7F1GFaO5W1cdW/YwzDF905MQecjTwwvlQ4+khrN9OkRwIw1SKiM49gikVQP0BqhGINn+cyQM7u4bqMXZ1iJi8/YoHVs6mT2+6f8m/eoNEs3X01Jo8vUpduaNOKpXqAEGBsFOs5X3V+XDLCzB6mlBFGiiLpV50nlFfN7FAtLCOmLdl5EWrDSug0OD3atrdgKlqJQYCZmE7/U9zsTVzQDN3riIeLvKCRgV217XHiTzdyvUnInoN0SymOaTuyWzefVEvVTUGSEsbP/4fzJis3RnAmE3IIDR2bauoHnVsxZe/c++a9LPfLP+HqvPUoThl9LdNx3zVUjJUhlE1b8= X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-Network-Message-Id: 7e6414e4-40d7-4633-80fa-08d7fc3ee007 X-MS-Exchange-CrossTenant-OriginalArrivalTime: 19 May 2020 21:52:07.4773 (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: TQIbqm1bwGqIlhPKHuiJ37qruOAQYjYlD6EWS818gkjHANURi6rWGU/4/VTF1HDtRPaWmDg13/mXfhbrrINgSg== X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM5PR12MB2504 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 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.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 1b0d6c053f1a..0d29bf701aca 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 ((DEBUG_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..565383ee26d2 100644 --- a/OvmfPkg/QemuFlashFvbServicesRuntimeDxe/QemuFlashDxe.c +++ b/OvmfPkg/QemuFlashFvbServicesRuntimeDxe/QemuFlashDxe.c @@ -10,6 +10,9 @@ **/ #include +#include +#include +#include #include "QemuFlash.h" @@ -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 = AsmReadMsr64 (MSR_SEV_ES_GHCB); + Ghcb = Msr.Ghcb; + + // + // Writing to flash is emulated by the hypervisor through the use of write + // protection. This won't work for an SEV-ES guest because the write won't + // be recognized as a true MMIO write, which would result in the required + // #VC exception. Instead, use the the VMGEXIT MMIO write support directly + // to perform the update. + // + VmgInit (Ghcb); + Ghcb->SharedBuffer[0] = Value; + Ghcb->SaveArea.SwScratch = (UINT64) (UINTN) Ghcb->SharedBuffer; + VmgExit (Ghcb, SVM_EXIT_MMIO_WRITE, (UINT64) (UINTN) Ptr, 1); + VmgDone (Ghcb); + } 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