Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp2814684rwd; Fri, 2 Jun 2023 15:26:19 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ4e4OI4m5hOSdkR2iLgwvCcl+TDT4DtomIEfKo9CzQDkPpD/iDKOqEs2hqm/qlWP4SUM0LN X-Received: by 2002:a17:902:cec9:b0:1b0:f31:a386 with SMTP id d9-20020a170902cec900b001b00f31a386mr1308567plg.26.1685744778748; Fri, 02 Jun 2023 15:26:18 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1685744778; cv=none; d=google.com; s=arc-20160816; b=mN1Ll4DbSWU7F7Mv7j48z98AekDWEh5g3ADgRtYj4+aI+MwryAJSNXqBn7qs5/KuSo HOmp0yBA3s437TTCSvLiizwt/p+OuSSlXIuLRFzmy6Rvxrb33TE6uG0qsrVjKdm1EUtX jNVgMXDTPlbyNb8WGNz/K4u/iCBkrSJ4GkEL/A+3IPWq7XiyghRFbMNDaYTVCt/hksb+ BtHR15QKQsQwyYBn63Ir2lHZ38QJFuJM0KvygJsguLTmN2yYZ5lCcWYVcWd2YPa55N/O hNVWxUPee+OzrWhTkcKRJp7OqWVeBIwiYJZV4L/K1zougZoLwpelhs5/GIkNCraVM3C9 9V4w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=DXVjh2eNwzzexw3cq8ebkwH8G1pscnb+UTG38cKYOgE=; b=yK62yvOsdGMid0bRqEcZfNRcdfLVMyhgmoFlGoFp4rTwOJkQkwvWGUKlJdbaeXJqKc JDIWg6SkIBeifq4J+sX+bApaoppuoIgRrZtwiRfBBN1JXPT/H883+GY+QvbVLqQjvfse 0qkziCoSJk40cgahfeM2moZmVtH9kJQ3RYyuGUIt3/XL+agqfAeDO+QVhyBAWMAP2TuS 6L6aXgD/7qy49G5hgj6uZyI9m3xUR1bKGOUUqt0+Qv231ar7rzt6LptrmgHAWsiwS9bk OQRJbSnjtw/6XaWaaF8cmALR4RiIBpdigJ51zyUG3fSS1bPw6rgG/LGQDR6STx0MlDIY RsBQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=ZKncJzQJ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id b1-20020a170902d50100b001adbb45eae6si1566298plg.331.2023.06.02.15.26.05; Fri, 02 Jun 2023 15:26:18 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=ZKncJzQJ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236567AbjFBWXJ (ORCPT + 99 others); Fri, 2 Jun 2023 18:23:09 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33072 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235208AbjFBWXI (ORCPT ); Fri, 2 Jun 2023 18:23:08 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 703CF9D; Fri, 2 Jun 2023 15:23:07 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id EC67961F43; Fri, 2 Jun 2023 22:23:06 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 4EA25C433A0; Fri, 2 Jun 2023 22:23:06 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1685744586; bh=A1Kw1w9zlma9Dn71EHkV9SDkh8QdOzzV3cw53rOgE4I=; h=References:In-Reply-To:From:Date:Subject:To:Cc:From; b=ZKncJzQJkW+BpcITXz1GTptBBDth+aU6Iz/qUnCw3No4SIH+0VQrCsa+j3Gpu42EH bryDaAyVxTIJYjRcbZ/Vg39cZzhFmSQhijY4Q+Kkp9w9EHj4v7L1oxnTO23ikLSH1r httLYzWdTdglPTZcmT2U4Fq68oENnVxJDCqZlCs/uW2xUcbjEBD4sq6KpO/4awP8ix SMc7DqXqBze2qxwFZERVaUT3Tfp8432Cc+izD5ByF86D1jxI5tgaBnVF1SFMvnyr9s epHNAxidhNryMXrIAjsQxHpk9CE4tbXBBprQPkDGpOOZpJIM/xQYD7A5QWeplhEev5 /7/ON/RwA48/A== Received: by mail-lf1-f51.google.com with SMTP id 2adb3069b0e04-4f4bdcde899so3458086e87.0; Fri, 02 Jun 2023 15:23:06 -0700 (PDT) X-Gm-Message-State: AC+VfDzc4K6rGRloEDDAi6S1n7jSlpGXpbf7FeCz8Kax7drj84KeD4+6 5S2Uqry7C3xGeZFExE5sX+FD8VR9B+C5GUNTL6M= X-Received: by 2002:ac2:5d6c:0:b0:4f2:61c8:fd0d with SMTP id h12-20020ac25d6c000000b004f261c8fd0dmr2203790lft.48.1685744584251; Fri, 02 Jun 2023 15:23:04 -0700 (PDT) MIME-Version: 1.0 References: <20230602101313.3557775-1-ardb@kernel.org> <20230602101313.3557775-21-ardb@kernel.org> <849a65c8-a320-a8a8-8784-0ee3737eee9e@amd.com> <16830132-f270-5c3d-37ae-6bfbc0e7c13d@amd.com> In-Reply-To: <16830132-f270-5c3d-37ae-6bfbc0e7c13d@amd.com> From: Ard Biesheuvel Date: Sat, 3 Jun 2023 00:22:52 +0200 X-Gmail-Original-Message-ID: Message-ID: Subject: Re: [PATCH v4 20/21] x86/efistub: Perform SNP feature test while running in the firmware To: Tom Lendacky Cc: linux-efi@vger.kernel.org, linux-kernel@vger.kernel.org, Evgeniy Baskov , Borislav Petkov , Andy Lutomirski , Dave Hansen , Ingo Molnar , Peter Zijlstra , Thomas Gleixner , Alexey Khoroshilov , Peter Jones , Gerd Hoffmann , Dave Young , Mario Limonciello , Kees Cook , "Kirill A . Shutemov" , Linus Torvalds , Joerg Roedel Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-4.6 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Sat, 3 Jun 2023 at 00:01, Tom Lendacky wrote: > > On 6/2/23 16:29, Ard Biesheuvel wrote: > > On Fri, 2 Jun 2023 at 22:39, Tom Lendacky wrote: > >> > >> On 6/2/23 15:38, Tom Lendacky wrote: > >>> On 6/2/23 05:13, Ard Biesheuvel wrote: > >>>> Before refactoring the EFI stub boot flow to avoid the legacy bare metal > >>>> decompressor, duplicate the SNP feature check in the EFI stub before > >>>> handing over to the kernel proper. > >>>> > >>>> The SNP feature check can be performed while running under the EFI boot > >>>> services, which means we can fail gracefully and return an error to the > >>>> bootloader if the loaded kernel does not implement support for all the > >>>> features that the hypervisor enabled. > >>>> > >>>> Signed-off-by: Ard Biesheuvel > >>>> --- > >>>> arch/x86/boot/compressed/sev.c | 74 ++++++++++++-------- > >>>> arch/x86/include/asm/sev.h | 4 ++ > >>>> drivers/firmware/efi/libstub/x86-stub.c | 17 +++++ > >>>> 3 files changed, 67 insertions(+), 28 deletions(-) > >>>> > >>>> diff --git a/arch/x86/boot/compressed/sev.c > >>>> b/arch/x86/boot/compressed/sev.c > >>>> index 014b89c890887b9a..be021e24f1ece421 100644 > >>>> --- a/arch/x86/boot/compressed/sev.c > >>>> +++ b/arch/x86/boot/compressed/sev.c > >>> > >>> > >>>> +void sev_enable(struct boot_params *bp) > >>>> +{ > >>>> + unsigned int eax, ebx, ecx, edx; > >>>> bool snp; > >>>> /* > >>>> @@ -358,37 +391,14 @@ void sev_enable(struct boot_params *bp) > >>>> */ > >>>> snp = snp_init(bp); > >>>> - /* Check for the SME/SEV support leaf */ > >>>> - eax = 0x80000000; > >>>> - ecx = 0; > >>>> - native_cpuid(&eax, &ebx, &ecx, &edx); > >>>> - if (eax < 0x8000001f) > >>>> - return; > >>>> - > >>>> - /* > >>>> - * Check for the SME/SEV feature: > >>>> - * CPUID Fn8000_001F[EAX] > >>>> - * - Bit 0 - Secure Memory Encryption support > >>>> - * - Bit 1 - Secure Encrypted Virtualization support > >>>> - * CPUID Fn8000_001F[EBX] > >>>> - * - Bits 5:0 - Pagetable bit position used to indicate encryption > >>>> - */ > >>>> - eax = 0x8000001f; > >>>> - ecx = 0; > >>>> - native_cpuid(&eax, &ebx, &ecx, &edx); > >>>> - /* Check whether SEV is supported */ > >>>> - if (!(eax & BIT(1))) { > >>>> + /* Set the SME mask if this is an SEV guest. */ > >>>> + sev_status = sev_get_status(); > >>>> + if (!(sev_status & MSR_AMD64_SEV_ENABLED)) { > >>>> if (snp) > >>>> error("SEV-SNP support indicated by CC blob, but not > >>>> CPUID."); > >>>> return; > >>>> } > >>>> - /* Set the SME mask if this is an SEV guest. */ > >>>> - boot_rdmsr(MSR_AMD64_SEV, &m); > >>>> - sev_status = m.q; > >>>> - if (!(sev_status & MSR_AMD64_SEV_ENABLED)) > >>>> - return; > >>>> - > >>>> /* Negotiate the GHCB protocol version. */ > >>>> if (sev_status & MSR_AMD64_SEV_ES_ENABLED) { > >>>> if (!sev_es_negotiate_protocol()) > >>>> @@ -409,6 +419,14 @@ void sev_enable(struct boot_params *bp) > >>>> if (snp && !(sev_status & MSR_AMD64_SEV_SNP_ENABLED)) > >>>> error("SEV-SNP supported indicated by CC blob, but not SEV > >>>> status MSR."); > >>>> + /* > >>>> + * Check for the SME/SEV feature: > >>>> + * CPUID Fn8000_001F[EBX] > >>>> + * - Bits 5:0 - Pagetable bit position used to indicate encryption > >>>> + */ > >>>> + eax = 0x8000001f; > >>>> + ecx = 0; > >>>> + native_cpuid(&eax, &ebx, &ecx, &edx); > >>> > >>> This causes SEV-ES / SEV-SNP to crash. > >>> > >>> This goes back to a previous comment where calling either > >>> sev_es_negotiate_protocol() or get_hv_features() blows away the GHCB value > >>> in the GHCB MSR and as soon as the CPUID instruction is executed the boot > >>> blows up. > >>> > >>> Even if we move this up to be done earlier, we can complete this function > >>> successfully but then blow up further on. > >>> > >>> So you probably have to modify the routines in question to save and > >>> restore the GHCB MSR value. > >> > >> I should clarify that it doesn't in fact cause a problem until the final > >> patch is applied and this path is taken. > >> > > > > Could we just move the CPUID call to the start of the function? > > I tried that and it allowed sev_enable() to complete successfully, but > then it blew up after that. > > But I noticed that the patch to apply the kernel CS earlier is no longer > part of this series. When I applied the patch to move the setting of the > kernel CS directly after the call to startup_64_setup_env() in > arch/x86/kernel/head_64.S, everything worked again. > > That patch hasn't been pulled into tip, yet, and since you dropped your > version, the CS value wrong and the IRET from the #VC blows up. > > So as long as you pre-req that patch, unless Boris or Dave would prefer it > to go in with this series, moving the call to native_cpuid() up to just > before the GHCB protocol version negotiation, works. > OK, thanks for confirming. I dropped that patch because I was assuming that your fix would be picked up.