From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from NAM10-DM6-obe.outbound.protection.outlook.com (NAM10-DM6-obe.outbound.protection.outlook.com [40.107.93.48]) by mx.groups.io with SMTP id smtpd.web08.612.1604685303779859290 for ; Fri, 06 Nov 2020 09:55:03 -0800 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=Lw/rCFAj; spf=none, err=SPF record not found (domain: amd.com, ip: 40.107.93.48, mailfrom: thomas.lendacky@amd.com) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=TPEnOM+yk/t1YrDJWy0RtDVgK8cltbtoj7/wWxPoafajSqRPRHtuwYc+FI6lMs5ET95U+mHENBI5JkRM3VU7SB0jDtr+g2tBI1iG6QvVUZ0TJZaKrBBwxI5Tu3b0RZ0698imSnkyqdcP9JU9OvgZu78PCwRuNMqtONaC5KaCBINBurrujuNbHUISDP/WHdQft+8BxabKytRDx/rW8Q78UC5xDdnJOm/xE1Pquvha2zV48Z0qEnzQlZqpKYVltm53cPG52BNlK1jMqkXwjEtCCdf6/pbw+aJOScEvbG9BJwA75ClSJs/HUusb/J1e2VGyGtSUq1gzBej1xnOnzM9hyQ== 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=ZOAW4gtJsrTR7If9+L+3zfwRpqTmJdt8GTVANlGg7Z4=; b=PbdD09Mrju8bh9igE2bfoPXb0j1IN90qE2k7+Lf+deOSJJU7w4CUHUhql1AGXhsoi3SdHfT0VoTbbamkI8/7KFGBLGTB9rniUm0YoKqlz8mc8rIk/xHEH+Xhf9aS+lsvNEL1v76gbVs4GmPzDBOchuMJQT18tdVMsgWfLEagRCqSrK+IkG1TCEbNkIz5wkNFXTXkpNjY4FdkisEO8rzijt+DBFrXRsfF7P7q+RFpstp11kpgnYgFS+C26RZcQHpibIHU4Sg7EuZcZH4RskvcXVdvJG7QrTh2TJktIuW48HGIMf6aeVr2HzTkFuyjcKgFnEnc4buks00AlKiK4TvJ7w== 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=ZOAW4gtJsrTR7If9+L+3zfwRpqTmJdt8GTVANlGg7Z4=; b=Lw/rCFAjkMxYyx13w/IOq9jdGMg26pz8aVk0OhueFIsl7lZpLus1CpasCy2u3kjpBmRRcCaT7J5dgfRfamBT1GG04h67EYkB6743b+zzqpW7JmLiwbSPmu4yMPdb6iWhF4OEdl7UQY9WqaCCQRLsIWCE8KKNeHMTcHcr8X/X7Wk= 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 DM5PR1201MB0122.namprd12.prod.outlook.com (2603:10b6:4:57::10) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3499.18; Fri, 6 Nov 2020 17:55:00 +0000 Received: from DM5PR12MB1355.namprd12.prod.outlook.com ([fe80::e442:c052:8a2c:5fba]) by DM5PR12MB1355.namprd12.prod.outlook.com ([fe80::e442:c052:8a2c:5fba%6]) with mapi id 15.20.3499.032; Fri, 6 Nov 2020 17:55:00 +0000 From: "Lendacky, Thomas" To: devel@edk2.groups.io CC: Brijesh Singh , Eric Dong , Ray Ni , Laszlo Ersek , Rahul Kumar , Jordan Justen , Ard Biesheuvel Subject: [PATCH v4 10/11] UefiCpuPkg, OvmfPkg: Disable interrupts when using the GHCB Date: Fri, 6 Nov 2020 11:53:12 -0600 Message-ID: X-Mailer: git-send-email 2.28.0 In-Reply-To: References: X-Originating-IP: [165.204.77.1] X-ClientProxiedBy: SA9PR13CA0231.namprd13.prod.outlook.com (2603:10b6:806:25::26) 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 SA9PR13CA0231.namprd13.prod.outlook.com (2603:10b6:806:25::26) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3564.13 via Frontend Transport; Fri, 6 Nov 2020 17:54:59 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-HT: Tenant X-MS-Office365-Filtering-Correlation-Id: 6875a97e-57b2-464a-9b06-08d8827d14c5 X-MS-TrafficTypeDiagnostic: DM5PR1201MB0122: X-MS-Exchange-Transport-Forked: True X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:9508; X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: /o+HjvaEkjhIL9tu9oY4KjL1A185xtah8/WEPXM1lF+InF+NYSYUSxOwTcSMVEkHEEEzNXIXYZJ1ML1hqK7uGfY7PnyoVpEvH3LvLJFpAkEXSch5KjA63e8CpuGChet9N+Ax4JnPxkdDYJ53KRmrgJyOzIugiGZ7tPs3bTbVJq6noraJLXLijzsCvXyQNUQn0wU0CLQ1O5e2SunZSzi/hbknlDf+N83OoTya1JORjU2QTV0nBC3/1lS2wEEeKHweS4Y6krFBqIVjRr4VUBL/TVVeiiNtAa6AJ8bH5Hw0dHnINULCuoIS+4qGbJrelmKA1ZNwcelICH0J7hMWVoUivTDACiviHT/+Sdp6e84QQSdpbcChav4sMz01HPlVK3zJz1MqYTqPHAj47jRZH9HGBg== 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;SFS:(4636009)(346002)(376002)(366004)(136003)(39860400002)(396003)(52116002)(5660300002)(2906002)(4326008)(83380400001)(8676002)(2616005)(956004)(7696005)(6916009)(66476007)(66946007)(26005)(16526019)(66556008)(86362001)(6486002)(8936002)(36756003)(54906003)(478600001)(19627235002)(6666004)(316002)(186003)(966005);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData: Qf3U4zL30+s5qccaFelegCrdMYJHHOoVxqATYmcNgJSymg3soxhhH+J4JrpZy0PUWdG9q6SPAZ/nvihbOTVZvmRWesuqYjZeAvajkjBwB7RBOUtD5H7N7sCtgJU2Vi4zOb+TmWxVZMmdjQsAIBzF29DazDqF3uVNbWlLNi/ETZ/mbM0wbbmgSf37B0Dxr+/2ITqWzqcjTo+ed7glqDiHkLxTxW/PiWOKMVxiDoJ6bVu+4YgfWAZU87qbGPXyj7rQJ4tRMOZ5g+Vg/vsc54h9DLOmg0l936T/cBEd11LTfytGRL58gn5iuy+YgYPD8jC9IOhb+QD2mP7pqgJrfFQ89RhvEneEJh7PX1Hb49GxnAhkdFVEnqnmh2gJMDmhgPlQw7sXG9IBBlCSP9wq39JO5AZWpd6Ek1zwGAXxnVP4mOZkMS8v7axNTiIPlo1i4AA3otpwJd9BVOPIhxZNsCOwxE5DyCCsFKA7LDWnDQFfzPbMOS9FgtjuJ9wMd3wN8Zz0ofcv+wqj13QbggpJ0ikn4G3xDKJMe30yiMNWm0lo/uHc8d1x9cvKTjhrq46RTYAlnQygUN2R5CpUPBn+NXqD1Y6EYKUQKwBozfitb5uHtqFch3D9rIVRyRr6Ka1KsmvwND65RDE6Tv8RX2qP76OzWg== X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-Network-Message-Id: 6875a97e-57b2-464a-9b06-08d8827d14c5 X-MS-Exchange-CrossTenant-AuthSource: DM5PR12MB1355.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 06 Nov 2020 17:55:00.5921 (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: CSpTEl0P4AZ547v/i2v8chtarjOGd7y50v7KnnmibqWpQxYyMvtPyZF3Z/JH9fqhGv0NtXFjr72O7t5exF1SWQ== X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM5PR1201MB0122 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: quoted-printable From: Tom Lendacky BZ: https://bugzilla.tianocore.org/show_bug.cgi?id=3D3008 The QemuFlashPtrWrite() flash services runtime uses the GHCB and VmgExit() directly to perform the flash write when running as an SEV-ES guest. If an interrupt arrives between VmgInit() and VmgExit(), the Dr7 read in the interrupt handler will generate a #VC, which can overwrite information in the GHCB that QemuFlashPtrWrite() has set. This has been seen with the timer interrupt firing and the CpuExceptionHandlerLib library code, UefiCpuPkg/Library/CpuExceptionHandlerLib/X64/ Xcode5ExceptionHandlerAsm.nasm and ExceptionHandlerAsm.nasm reading the Dr7 register while QemuFlashPtrWrite() is using the GHCB. In general, it is necessary to protect the GHCB whenever it is used, not just in QemuFlashPtrWrite(). Disable interrupts around the usage of the GHCB by modifying the VmgInit() and VmgDone() interfaces: - VmgInit() will take an extra parameter that is a pointer to a BOOLEAN that will hold the interrupt state at the time of invocation. VmgInit() will get and save this interrupt state before updating the GHCB. - VmgDone() will take an extra parameter that is used to indicate whether interrupts are to be (re)enabled. Before exiting, VmgDone() will enable interrupts if that is requested. Fixes: 437eb3f7a8db7681afe0e6064d3a8edb12abb766 Cc: Eric Dong Cc: Ray Ni Cc: Laszlo Ersek Cc: Rahul Kumar Cc: Jordan Justen Cc: Ard Biesheuvel Cc: Tom Lendacky Cc: Brijesh Singh Acked-by: Eric Dong Reviewed-by: Laszlo Ersek Signed-off-by: Tom Lendacky --- UefiCpuPkg/Include/Library/VmgExitLib.h | 14 ++++++++--- OvmfPkg/Library/VmgExitLib/VmgExitLib.c | 26 +++++++++++++++= ++--- OvmfPkg/Library/VmgExitLib/VmgExitVcHandler.c | 5 ++-- OvmfPkg/QemuFlashFvbServicesRuntimeDxe/QemuFlashDxe.c | 5 ++-- UefiCpuPkg/Library/MpInitLib/DxeMpLib.c | 5 ++-- UefiCpuPkg/Library/MpInitLib/MpLib.c | 7 +++--- UefiCpuPkg/Library/VmgExitLibNull/VmgExitLibNull.c | 18 ++++++++------ 7 files changed, 55 insertions(+), 25 deletions(-) diff --git a/UefiCpuPkg/Include/Library/VmgExitLib.h b/UefiCpuPkg/Include/L= ibrary/VmgExitLib.h index 07e8af6450b9..061948cf840d 100644 --- a/UefiCpuPkg/Include/Library/VmgExitLib.h +++ b/UefiCpuPkg/Include/Library/VmgExitLib.h @@ -50,13 +50,16 @@ VmgExit ( Performs the necessary steps in preparation for invoking VMGEXIT. Must b= e called before setting any fields within the GHCB. =20 - @param[in, out] Ghcb A pointer to the GHCB + @param[in, out] Ghcb A pointer to the GHCB + @param[in, out] InterruptState A pointer to hold the current interrupt + state, used for restoring in VmgDone () =20 **/ VOID EFIAPI VmgInit ( - IN OUT GHCB *Ghcb + IN OUT GHCB *Ghcb, + IN OUT BOOLEAN *InterruptState ); =20 /** @@ -65,13 +68,16 @@ VmgInit ( Performs the necessary steps to cleanup after invoking VMGEXIT. Must be called after obtaining needed fields within the GHCB. =20 - @param[in, out] Ghcb A pointer to the GHCB + @param[in, out] Ghcb A pointer to the GHCB + @param[in] InterruptState An indicator to conditionally (re)enabl= e + interrupts =20 **/ VOID EFIAPI VmgDone ( - IN OUT GHCB *Ghcb + IN OUT GHCB *Ghcb, + IN BOOLEAN InterruptState ); =20 /** diff --git a/OvmfPkg/Library/VmgExitLib/VmgExitLib.c b/OvmfPkg/Library/VmgE= xitLib/VmgExitLib.c index 0540df8a04d4..bc5cd61d751f 100644 --- a/OvmfPkg/Library/VmgExitLib/VmgExitLib.c +++ b/OvmfPkg/Library/VmgExitLib/VmgExitLib.c @@ -132,15 +132,27 @@ VmgExit ( Performs the necessary steps in preparation for invoking VMGEXIT. Must b= e called before setting any fields within the GHCB. =20 - @param[in, out] Ghcb A pointer to the GHCB + @param[in, out] Ghcb A pointer to the GHCB + @param[in, out] InterruptState A pointer to hold the current interrupt + state, used for restoring in VmgDone () =20 **/ VOID EFIAPI VmgInit ( - IN OUT GHCB *Ghcb + IN OUT GHCB *Ghcb, + IN OUT BOOLEAN *InterruptState ) { + // + // Be sure that an interrupt can't cause a #VC while the GHCB is + // being used. + // + *InterruptState =3D GetInterruptState (); + if (*InterruptState) { + DisableInterrupts (); + } + SetMem (&Ghcb->SaveArea, sizeof (Ghcb->SaveArea), 0); } =20 @@ -150,15 +162,21 @@ VmgInit ( Performs the necessary steps to cleanup after invoking VMGEXIT. Must be called after obtaining needed fields within the GHCB. =20 - @param[in, out] Ghcb A pointer to the GHCB + @param[in, out] Ghcb A pointer to the GHCB + @param[in] InterruptState An indicator to conditionally (re)enabl= e + interrupts =20 **/ VOID EFIAPI VmgDone ( - IN OUT GHCB *Ghcb + IN OUT GHCB *Ghcb, + IN BOOLEAN InterruptState ) { + if (InterruptState) { + EnableInterrupts (); + } } =20 /** diff --git a/OvmfPkg/Library/VmgExitLib/VmgExitVcHandler.c b/OvmfPkg/Librar= y/VmgExitLib/VmgExitVcHandler.c index 9bf9d160179c..1671db3a01b1 100644 --- a/OvmfPkg/Library/VmgExitLib/VmgExitVcHandler.c +++ b/OvmfPkg/Library/VmgExitLib/VmgExitVcHandler.c @@ -1568,6 +1568,7 @@ VmgExitHandleVc ( SEV_ES_INSTRUCTION_DATA InstructionData; UINT64 ExitCode, Status; EFI_STATUS VcRet; + BOOLEAN InterruptState; =20 VcRet =3D EFI_SUCCESS; =20 @@ -1578,7 +1579,7 @@ VmgExitHandleVc ( Regs =3D SystemContext.SystemContextX64; Ghcb =3D Msr.Ghcb; =20 - VmgInit (Ghcb); + VmgInit (Ghcb, &InterruptState); =20 ExitCode =3D Regs->ExceptionData; switch (ExitCode) { @@ -1662,7 +1663,7 @@ VmgExitHandleVc ( VcRet =3D EFI_PROTOCOL_ERROR; } =20 - VmgDone (Ghcb); + VmgDone (Ghcb, InterruptState); =20 return VcRet; } diff --git a/OvmfPkg/QemuFlashFvbServicesRuntimeDxe/QemuFlashDxe.c b/OvmfPk= g/QemuFlashFvbServicesRuntimeDxe/QemuFlashDxe.c index f9b21b54137d..1b0742967f71 100644 --- a/OvmfPkg/QemuFlashFvbServicesRuntimeDxe/QemuFlashDxe.c +++ b/OvmfPkg/QemuFlashFvbServicesRuntimeDxe/QemuFlashDxe.c @@ -52,6 +52,7 @@ QemuFlashPtrWrite ( if (MemEncryptSevEsIsEnabled ()) { MSR_SEV_ES_GHCB_REGISTER Msr; GHCB *Ghcb; + BOOLEAN InterruptState; =20 Msr.GhcbPhysicalAddress =3D AsmReadMsr64 (MSR_SEV_ES_GHCB); Ghcb =3D Msr.Ghcb; @@ -63,12 +64,12 @@ QemuFlashPtrWrite ( // #VC exception. Instead, use the the VMGEXIT MMIO write support dire= ctly // to perform the update. // - VmgInit (Ghcb); + VmgInit (Ghcb, &InterruptState); Ghcb->SharedBuffer[0] =3D Value; Ghcb->SaveArea.SwScratch =3D (UINT64) (UINTN) Ghcb->SharedBuffer; VmgSetOffsetValid (Ghcb, GhcbSwScratch); VmgExit (Ghcb, SVM_EXIT_MMIO_WRITE, (UINT64) (UINTN) Ptr, 1); - VmgDone (Ghcb); + VmgDone (Ghcb, InterruptState); } else { *Ptr =3D Value; } diff --git a/UefiCpuPkg/Library/MpInitLib/DxeMpLib.c b/UefiCpuPkg/Library/M= pInitLib/DxeMpLib.c index 2c00d72ddefe..7839c249760e 100644 --- a/UefiCpuPkg/Library/MpInitLib/DxeMpLib.c +++ b/UefiCpuPkg/Library/MpInitLib/DxeMpLib.c @@ -171,6 +171,7 @@ GetSevEsAPMemory ( EFI_PHYSICAL_ADDRESS StartAddress; MSR_SEV_ES_GHCB_REGISTER Msr; GHCB *Ghcb; + BOOLEAN InterruptState; =20 // // Allocate 1 page for AP jump table page @@ -192,9 +193,9 @@ GetSevEsAPMemory ( Msr.GhcbPhysicalAddress =3D AsmReadMsr64 (MSR_SEV_ES_GHCB); Ghcb =3D Msr.Ghcb; =20 - VmgInit (Ghcb); + VmgInit (Ghcb, &InterruptState); VmgExit (Ghcb, SVM_EXIT_AP_JUMP_TABLE, 0, (UINT64) (UINTN) StartAddress)= ; - VmgDone (Ghcb); + VmgDone (Ghcb, InterruptState); =20 return (UINTN) StartAddress; } diff --git a/UefiCpuPkg/Library/MpInitLib/MpLib.c b/UefiCpuPkg/Library/MpIn= itLib/MpLib.c index 6d977d45bcdd..1f47ff3f73b5 100644 --- a/UefiCpuPkg/Library/MpInitLib/MpLib.c +++ b/UefiCpuPkg/Library/MpInitLib/MpLib.c @@ -884,6 +884,7 @@ ApWakeupFunction ( GHCB *Ghcb; UINT64 Status; BOOLEAN DoDecrement; + BOOLEAN InterruptState; =20 DoDecrement =3D (BOOLEAN) (CpuMpData->InitFlag =3D=3D ApInitConf= ig); =20 @@ -891,7 +892,7 @@ ApWakeupFunction ( Msr.GhcbPhysicalAddress =3D AsmReadMsr64 (MSR_SEV_ES_GHCB); Ghcb =3D Msr.Ghcb; =20 - VmgInit (Ghcb); + VmgInit (Ghcb, &InterruptState); =20 if (DoDecrement) { DoDecrement =3D FALSE; @@ -905,11 +906,11 @@ ApWakeupFunction ( =20 Status =3D VmgExit (Ghcb, SVM_EXIT_AP_RESET_HOLD, 0, 0); if ((Status =3D=3D 0) && (Ghcb->SaveArea.SwExitInfo2 !=3D 0)) = { - VmgDone (Ghcb); + VmgDone (Ghcb, InterruptState); break; } =20 - VmgDone (Ghcb); + VmgDone (Ghcb, InterruptState); } =20 // diff --git a/UefiCpuPkg/Library/VmgExitLibNull/VmgExitLibNull.c b/UefiCpuPk= g/Library/VmgExitLibNull/VmgExitLibNull.c index b47e282aff82..89b065cb3ff3 100644 --- a/UefiCpuPkg/Library/VmgExitLibNull/VmgExitLibNull.c +++ b/UefiCpuPkg/Library/VmgExitLibNull/VmgExitLibNull.c @@ -57,15 +57,16 @@ VmgExit ( Performs the necessary steps in preparation for invoking VMGEXIT. Must b= e called before setting any fields within the GHCB. =20 - The base library function does nothing. - - @param[in, out] Ghcb A pointer to the GHCB + @param[in, out] Ghcb A pointer to the GHCB + @param[in, out] InterruptState A pointer to hold the current interrupt + state, used for restoring in VmgDone () =20 **/ VOID EFIAPI VmgInit ( - IN OUT GHCB *Ghcb + IN OUT GHCB *Ghcb, + IN OUT BOOLEAN *InterruptState ) { } @@ -76,15 +77,16 @@ VmgInit ( Performs the necessary steps to cleanup after invoking VMGEXIT. Must be called after obtaining needed fields within the GHCB. =20 - The base library function does nothing. - - @param[in, out] Ghcb A pointer to the GHCB + @param[in, out] Ghcb A pointer to the GHCB + @param[in] InterruptState An indicator to conditionally (re)enabl= e + interrupts =20 **/ VOID EFIAPI VmgDone ( - IN OUT GHCB *Ghcb + IN OUT GHCB *Ghcb, + IN BOOLEAN InterruptState ) { } --=20 2.28.0