public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Borislav Petkov <bp@alien8.de>
To: X86 ML <x86@kernel.org>
Cc: LKML <linux-kernel@vger.kernel.org>,
	Boris Ostrovsky <boris.ostrovsky@oracle.com>,
	"Maciej S. Szmigiero" <maciej.szmigiero@oracle.com>
Subject: [PATCH 2/2] x86/CPU/AMD: Improve the erratum 1386 workaround
Date: Thu, 21 Mar 2024 13:05:48 +0100	[thread overview]
Message-ID: <20240321120548.22687-2-bp@alien8.de> (raw)
In-Reply-To: <20240321120548.22687-1-bp@alien8.de>

From: "Borislav Petkov (AMD)" <bp@alien8.de>

Disable XSAVES only on machines which haven't loaded the microcode
revision containing the erratum fix.

This will come in handy when running archaic OSes as guests. OSes whose
brilliant programmers thought that CPUID is overrated and one should not
query it but use features directly, ala shoot first, ask questions
later... but only if you're alive after the shooting.

Signed-off-by: Borislav Petkov (AMD) <bp@alien8.de>
Cc: Boris Ostrovsky <boris.ostrovsky@oracle.com>
Cc: "Maciej S. Szmigiero" <maciej.szmigiero@oracle.com>
---
 arch/x86/kernel/cpu/amd.c | 28 ++++++++++++++++++++++++++++
 1 file changed, 28 insertions(+)

diff --git a/arch/x86/kernel/cpu/amd.c b/arch/x86/kernel/cpu/amd.c
index 6d8677e80ddb..c02b07feff6e 100644
--- a/arch/x86/kernel/cpu/amd.c
+++ b/arch/x86/kernel/cpu/amd.c
@@ -15,6 +15,7 @@
 #include <asm/cpu.h>
 #include <asm/spec-ctrl.h>
 #include <asm/smp.h>
+#include <asm/microcode.h>
 #include <asm/numa.h>
 #include <asm/pci-direct.h>
 #include <asm/delay.h>
@@ -804,6 +805,16 @@ static void init_amd_bd(struct cpuinfo_x86 *c)
 
 static void fix_erratum_1386(struct cpuinfo_x86 *c)
 {
+	u8 fam, model, stpng, rev;
+	union zen_patch_rev p;
+
+	p.ucode_rev = c->microcode;
+
+	fam   = p.ext_fam + 0xf;
+	model = p.ext_model << 4 | p.model;
+	stpng = p.stepping;
+	rev   = p.rev;
+
 	/*
 	 * Work around Erratum 1386.  The XSAVES instruction malfunctions in
 	 * certain circumstances on Zen1/2 uarch, and not all parts have had
@@ -811,7 +822,24 @@ static void fix_erratum_1386(struct cpuinfo_x86 *c)
 	 *
 	 * Affected parts all have no supervisor XSAVE states, meaning that
 	 * the XSAVEC instruction (which works fine) is equivalent.
+	 *
+	 * Clear the feature flag only on microcode revisions which
+	 * don't have the fix.
 	 */
+	if (fam   == c->x86 &&
+	    model == c->x86_model &&
+	    stpng == c->x86_stepping) {
+		if (fam == 0x17) {
+			if (model == 0x1 && stpng == 0x2) {
+				if (rev >= 0x6e)
+					return;
+			} else if (model == 0x31 && stpng == 0x0) {
+				if (rev >= 0x52)
+					return;
+			}
+		}
+	}
+
 	clear_cpu_cap(c, X86_FEATURE_XSAVES);
 }
 
-- 
2.43.0


  reply	other threads:[~2024-03-21 12:06 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-03-21 12:05 [PATCH 1/2] x86/microcode/AMD: Add a patch revision number union Borislav Petkov
2024-03-21 12:05 ` Borislav Petkov [this message]
2024-03-21 15:21   ` [PATCH 2/2] x86/CPU/AMD: Improve the erratum 1386 workaround Maciej S. Szmigiero
2024-03-24 20:05     ` [PATCH -v2] " Borislav Petkov
2024-03-25 12:45       ` Maciej S. Szmigiero
2024-03-25 13:01         ` Borislav Petkov
2024-03-25 13:23       ` [tip: x86/cpu] " tip-bot2 for Borislav Petkov (AMD)
2024-03-21 19:26   ` [PATCH 2/2] " Ingo Molnar
2024-03-21 19:24 ` [PATCH 1/2] x86/microcode/AMD: Add a patch revision number union Ingo Molnar

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240321120548.22687-2-bp@alien8.de \
    --to=bp@alien8.de \
    --cc=boris.ostrovsky@oracle.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=maciej.szmigiero@oracle.com \
    --cc=x86@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox