Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp4368436pxj; Tue, 8 Jun 2021 12:35:07 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxrTE0LQhx/hX1mhB/v1JVHdl90ifOIWKYjN38E5W+7Q6fU+gO7Zi5dOG22sz0SI1zf4wBI X-Received: by 2002:a05:6402:1513:: with SMTP id f19mr28234300edw.235.1623180907322; Tue, 08 Jun 2021 12:35:07 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1623180907; cv=none; d=google.com; s=arc-20160816; b=0iNHE8T5o8m39L1l4L++fvOarnb5VXGGdbNe/51bvnaoXuk4TvkLBRfXbIDEz5+4s3 KlEDLhCw82iLuisGdqngvKbnz5VeZM4QCrlBRvZibgG4P1yC38PXONwvD9W1WV/m8Oa/ eLgqWxOdaqoixbduijRDjtHB12WXI/HRDbW6EIBAIGDSu9i370L4qsd4ZpZmN+M9CTE3 kXewkqwFkbkKNq0BiUQNGNx05lYLpVLIbFGWnQfHdICK+R4y2nal2zwcLbxzVtjOSQlm Ba4yKrfD5IEr6p04N6Fkm76iskGTCuqMI33rJeuysd6Q4UIb1fl/jilKv4biYD50ka/6 P6Aw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=ecbxJmaUef01dG5uXi992oaBY35JMpDAuQzTLNAAKmc=; b=GsGKk9wF4+lA6ii3oaowHTCn5cIHJRhxc4MuVdXx+aunqte6yqX/19Du5rziFcIAyX uueYxY2TawVZ5RsjuwIckW/z3TsJdG0fTzlczYm1ueReP7KwkqT1R4pcx3iAtctTGJ+l LO4qboVmNn3yp5kZNkWBD0Pz5Ee7K0PGVQHGweWUV9Ta/NZJ/GsJiML3sLro4dTt2YWe 7l3c2t+unVDrj0Qv7W+eqjQmpvOcYcJFjaXiPIBitdKQSyR/yW9EdCfhfB/sNb9Vr94V 1n9n/Li35SA455DOEW72DHkR24kKLAvrR3I+jmT4tEogPmN3xBp4F6yHJbMn3zf5kYJv Sk7Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=Ov9A4YTH; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id f25si536997ejh.456.2021.06.08.12.34.44; Tue, 08 Jun 2021 12:35:07 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=Ov9A4YTH; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238352AbhFHT3e (ORCPT + 99 others); Tue, 8 Jun 2021 15:29:34 -0400 Received: from mail.kernel.org ([198.145.29.99]:39610 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234614AbhFHTQI (ORCPT ); Tue, 8 Jun 2021 15:16:08 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 3D1AA61954; Tue, 8 Jun 2021 18:50:47 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1623178247; bh=IWzbsvazQ/Rv68RMwi2/R6ZolNNYvus/0NQQowmYP0o=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Ov9A4YTH0s/hNXeu8t2KQyZ84wP7Z1MVmLiLWo7+mjEh9TZXqCVWoiClUiIs3+j10 Ly2bvxJM3B0ozm8bXXdWFyQCOWsrDdQs+CjU2QmhBCgtMlNBbR9D2QwskC1lD7MPU5 4gsEoUNORArg0XVDQEpc0d1DeNkL6XkT736/8VbA= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Pu Wen , Borislav Petkov , Tom Lendacky Subject: [PATCH 5.12 132/161] x86/sev: Check SME/SEV support in CPUID first Date: Tue, 8 Jun 2021 20:27:42 +0200 Message-Id: <20210608175949.910197372@linuxfoundation.org> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20210608175945.476074951@linuxfoundation.org> References: <20210608175945.476074951@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Pu Wen commit 009767dbf42ac0dbe3cf48c1ee224f6b778aa85a upstream. The first two bits of the CPUID leaf 0x8000001F EAX indicate whether SEV or SME is supported, respectively. It's better to check whether SEV or SME is actually supported before accessing the MSR_AMD64_SEV to check whether SEV or SME is enabled. This is both a bare-metal issue and a guest/VM issue. Since the first generation Hygon Dhyana CPU doesn't support the MSR_AMD64_SEV, reading that MSR results in a #GP - either directly from hardware in the bare-metal case or via the hypervisor (because the RDMSR is actually intercepted) in the guest/VM case, resulting in a failed boot. And since this is very early in the boot phase, rdmsrl_safe()/native_read_msr_safe() can't be used. So check the CPUID bits first, before accessing the MSR. [ tlendacky: Expand and improve commit message. ] [ bp: Massage commit message. ] Fixes: eab696d8e8b9 ("x86/sev: Do not require Hypervisor CPUID bit for SEV guests") Signed-off-by: Pu Wen Signed-off-by: Borislav Petkov Acked-by: Tom Lendacky Cc: # v5.10+ Link: https://lkml.kernel.org/r/20210602070207.2480-1-puwen@hygon.cn Signed-off-by: Greg Kroah-Hartman --- arch/x86/mm/mem_encrypt_identity.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) --- a/arch/x86/mm/mem_encrypt_identity.c +++ b/arch/x86/mm/mem_encrypt_identity.c @@ -504,10 +504,6 @@ void __init sme_enable(struct boot_param #define AMD_SME_BIT BIT(0) #define AMD_SEV_BIT BIT(1) - /* Check the SEV MSR whether SEV or SME is enabled */ - sev_status = __rdmsr(MSR_AMD64_SEV); - feature_mask = (sev_status & MSR_AMD64_SEV_ENABLED) ? AMD_SEV_BIT : AMD_SME_BIT; - /* * Check for the SME/SEV feature: * CPUID Fn8000_001F[EAX] @@ -519,11 +515,16 @@ void __init sme_enable(struct boot_param eax = 0x8000001f; ecx = 0; native_cpuid(&eax, &ebx, &ecx, &edx); - if (!(eax & feature_mask)) + /* Check whether SEV or SME is supported */ + if (!(eax & (AMD_SEV_BIT | AMD_SME_BIT))) return; me_mask = 1UL << (ebx & 0x3f); + /* Check the SEV MSR whether SEV or SME is enabled */ + sev_status = __rdmsr(MSR_AMD64_SEV); + feature_mask = (sev_status & MSR_AMD64_SEV_ENABLED) ? AMD_SEV_BIT : AMD_SME_BIT; + /* Check if memory encryption is enabled */ if (feature_mask == AMD_SME_BIT) { /*