[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH v3 2/3] x86/amd: Drop cpu_has_amd_erratum()


  • To: <xen-devel@xxxxxxxxxxxxxxxxxxxx>
  • From: Alejandro Vallejo <alejandro.garciavallejo@xxxxxxx>
  • Date: Thu, 26 Nov 2026 16:54:00 +0100
  • Arc-authentication-results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=lists.xenproject.org smtp.mailfrom=amd.com; dmarc=pass (p=quarantine sp=quarantine pct=100) action=none header.from=amd.com; dkim=none (message not signed); arc=none (0)
  • Arc-message-signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=CsA1rY2ybE7CXQq2TRf9UTNOGF6YyME+cRh4YL5OUsU=; b=dr+jtQCXcgmeJMvyi0t/Jf42tm3J6WnZCh7IeGAUgapATSNm4EaDdp5RqvyWHnIb4GeKoRzz4MVacsbanBY5PGpSM79sB9DZQMIHZFHl3pNK5y2uBdu8Gq3k1XX9Y42F8/KsEMzyj60vnPpprVif57kUUyA+o3lgAvdwCcmZrfOA3pFVoYKJOe3b+mJGJfc5kRy0Jw2UMO2ZBgj4wRCNCdp1dSqT1QNKsN+UQPsTkpMzHkG9TI2g4c74C3WD6zCeunnI16NFIELHhcpvH009++ECVQKGh02V2tfJoLDAKWADmkR8j40X2xAhV9nDQOIDDyjtL9dHOwjRJsXyoYAlzg==
  • Arc-seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=TLrRyXAtrjzCw9Bhtwra/IwarclSdD66Ten9LTb+EX5HOes55YiaenNHoLOnOXnA3kh25WIknKt8b89zq7oFCWOCzgJyiIZACeozMx45ue+rJOUhD4M2yih24uoeeLkINVmSbu0JxXVZN+olVAjL/Pzsr64+KSkQhim+unqF2dA+Rxxo8erQr1cHWi3UXpWORAulUkk6TzmHG1JWPpNBF40TKc6ZC67YVF4Mve4ko181UMxyDWwDaZo/yepb4RG59aT5/jdPM6iSliD9h46tPIlDn2qgKuSu4EjHDPB7wk0Db+Qa6zXAQGDVyf6/DHUMkTj/3aD+oSYaP18KcR4tEA==
  • Authentication-results: eu.smtp.expurgate.cloud; dkim=pass header.s=selector1 header.d=amd.com header.i="@amd.com" header.h="From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck"
  • Cc: Alejandro Vallejo <alejandro.garciavallejo@xxxxxxx>, Jan Beulich <jbeulich@xxxxxxxx>, Andrew Cooper <andrew.cooper3@xxxxxxxxxx>, Roger Pau Monné <roger@xxxxxxxxxxxxxx>, Teddy Astie <teddy.astie@xxxxxxxxxx>, Jason Andryuk <jason.andryuk@xxxxxxx>
  • Delivery-date: Thu, 26 Nov 2026 15:54:37 +0000
  • List-id: Xen developer discussion <xen-devel.lists.xenproject.org>

cpu_has_amd_erratum() takes the family-model-stepping ranges of an
erratum as variadic arguments, which MISRA C Rule 17.1 forbids, and the
ranges themselves are packed into integers by AMD_MODEL_RANGE(), which
makes them hard to read.

Replace it with one static inline predicate per erratum in asm/amd.h,
stating the vendor, family and models it covers.

For errata 383 specifically, take all of Fam10h as affected.

Signed-off-by: Alejandro Vallejo <alejandro.garciavallejo@xxxxxxx>
Acked-by: Andrew Cooper <andrew.cooper3@xxxxxxxxxx>
---
v3:
  * Change the commit message to highlight Fam10h starts being
    universally filtered.
---
 xen/arch/x86/cpu/amd.c                 | 32 +-------------
 xen/arch/x86/domain.c                  |  2 +-
 xen/arch/x86/hvm/svm/asid.c            |  2 +-
 xen/arch/x86/hvm/svm/svm.c             |  2 +-
 xen/arch/x86/include/asm/amd.h         | 60 +++++++++++---------------
 xen/arch/x86/x86_emulate/fpu.c         |  7 ++-
 xen/arch/x86/x86_emulate/x86_emulate.c |  5 ---
 7 files changed, 32 insertions(+), 78 deletions(-)

diff --git a/xen/arch/x86/cpu/amd.c b/xen/arch/x86/cpu/amd.c
index c6d6913cfa..4944437e65 100644
--- a/xen/arch/x86/cpu/amd.c
+++ b/xen/arch/x86/cpu/amd.c
@@ -317,36 +317,6 @@ void amd_set_cpuid_user_dis(bool enable)
        wrmsrl(MSR_K8_HWCR, val);
 }
 
-/*
- * Check for the presence of an AMD erratum. Arguments are defined in amd.h 
- * for each known erratum. Return 1 if erratum is found.
- */
-int cpu_has_amd_erratum(const struct cpuinfo_x86 *cpu, ...)
-{
-       va_list ap;
-       u32 range;
-       u32 ms;
-       
-       if (cpu->vendor != X86_VENDOR_AMD)
-               return 0;
-
-       /* Match family-model-stepping range */
-       va_start(ap, cpu);
-
-       ms = (cpu->model << 4) | cpu->stepping;
-       while ((range = va_arg(ap, int))) {
-               if ((cpu->family == AMD_MODEL_RANGE_FAMILY(range)) &&
-                   (ms >= AMD_MODEL_RANGE_START(range)) &&
-                   (ms <= AMD_MODEL_RANGE_END(range))) {
-                       va_end(ap);
-                       return 1;
-               }
-       }
-
-       va_end(ap);
-       return 0;
-}
-
 /*
  * Disable C1-Clock ramping if enabled in PMM7.CpuLowPwrEnh on 8th-generation
  * cores only. Assume BIOS has setup all Northbridges equivalently.
@@ -1208,7 +1178,7 @@ static void cf_check init_amd(struct cpuinfo_x86 *c)
        if (c->family == 0x10)
                __clear_bit(X86_FEATURE_MONITOR, c->x86_capability);
 
-       if (!cpu_has_amd_erratum(c, AMD_ERRATUM_121))
+       if (!cpu_has_amd_erratum_121(c))
                opt_allow_unsafe = 1;
        else if (opt_allow_unsafe < 0)
                panic("Xen will not boot on this CPU for security reasons"
diff --git a/xen/arch/x86/domain.c b/xen/arch/x86/domain.c
index 2407c575d9..a8bd6b6364 100644
--- a/xen/arch/x86/domain.c
+++ b/xen/arch/x86/domain.c
@@ -845,7 +845,7 @@ int arch_domain_create(struct domain *d,
     if ( !is_hvm_domain(d) )
         d->options |= XEN_DOMCTL_CDF_oos_off;
 
-    if ( d->domain_id && cpu_has_amd_erratum(&boot_cpu_data, AMD_ERRATUM_121) )
+    if ( d->domain_id && cpu_has_amd_erratum_121(&boot_cpu_data) )
     {
         if ( !opt_allow_unsafe )
         {
diff --git a/xen/arch/x86/hvm/svm/asid.c b/xen/arch/x86/hvm/svm/asid.c
index c8d86938d1..e070d97ef8 100644
--- a/xen/arch/x86/hvm/svm/asid.c
+++ b/xen/arch/x86/hvm/svm/asid.c
@@ -16,7 +16,7 @@ void svm_asid_init(const struct cpuinfo_x86 *c)
     unsigned int nasids = 0;
 
     /* Check for erratum #170, and leave ASIDs disabled if it's present. */
-    if ( !cpu_has_amd_erratum(c, AMD_ERRATUM_170) )
+    if ( !cpu_has_amd_erratum_170(c) )
         nasids = cpuid_ebx(0x8000000aU);
 
     hvm_asid_init(nasids);
diff --git a/xen/arch/x86/hvm/svm/svm.c b/xen/arch/x86/hvm/svm/svm.c
index ef42d5eb98..f89f5e8dc9 100644
--- a/xen/arch/x86/hvm/svm/svm.c
+++ b/xen/arch/x86/hvm/svm/svm.c
@@ -1439,7 +1439,7 @@ static void svm_init_erratum_383(const struct cpuinfo_x86 
*c)
     uint64_t msr_content;
 
     /* check whether CPU is affected */
-    if ( !cpu_has_amd_erratum(c, AMD_ERRATUM_383) )
+    if ( !cpu_has_amd_erratum_383(c) )
         return;
 
     /* use safe methods to be compatible with nested virtualization */
diff --git a/xen/arch/x86/include/asm/amd.h b/xen/arch/x86/include/asm/amd.h
index d87a57f7a5..6c24d2494e 100644
--- a/xen/arch/x86/include/asm/amd.h
+++ b/xen/arch/x86/include/asm/amd.h
@@ -6,44 +6,37 @@
 #define __AMD_H__
 
 #include <asm/cpufeature.h>
+#include <asm/x86-vendors.h>
 
-/* AMD errata checking
- *
- * Errata are defined using the AMD_LEGACY_ERRATUM() macro, which takes a
- * variable number of family-specific model-stepping ranges created by
- * AMD_MODEL_RANGE().
- *
- * Example:
- * #define AMD_ERRATUM_319                                              \
- *   AMD_LEGACY_ERRATUM(AMD_MODEL_RANGE(0x10, 0x2, 0x1, 0x4, 0x2),      \
- *                      AMD_MODEL_RANGE(0x10, 0x8, 0x0, 0x8, 0x0),      \
- *                      AMD_MODEL_RANGE(0x10, 0x9, 0x0, 0x9, 0x0))
- *   
+/*
+ * AMD errata Xen works around, each named after its number in the Revision
+ * Guide for the families it affects.
  */
 
-#define AMD_LEGACY_ERRATUM(...)         __VA_ARGS__, 0
-#define AMD_MODEL_RANGE(f, m_start, s_start, m_end, s_end)              \
-    (((f) << 24) | ((m_start) << 16) | ((s_start) << 12) | \
-     ((m_end) << 4) | (s_end))
-#define AMD_MODEL_RANGE_FAMILY(range)   (((range) >> 24) & 0xff)
-#define AMD_MODEL_RANGE_START(range)    (((range) >> 12) & 0xfff)
-#define AMD_MODEL_RANGE_END(range)      ((range) & 0xfff)
+/* Sequential execution across the non-canonical boundary causes hang. */
+static inline bool cpu_has_amd_erratum_121(const struct cpuinfo_x86 *c)
+{
+    return c->vendor == X86_VENDOR_AMD && c->family == 0xf && c->model <= 0x3f;
+}
 
-#define AMD_ERRATUM_121                                                 \
-    AMD_LEGACY_ERRATUM(AMD_MODEL_RANGE(0x0f, 0x0, 0x0, 0x3f, 0xf))
+/* Incorrect code bytes fetched after a world switch. */
+static inline bool cpu_has_amd_erratum_170(const struct cpuinfo_x86 *c)
+{
+    return c->vendor == X86_VENDOR_AMD && c->family == 0xf && c->model <= 0x67;
+}
 
-#define AMD_ERRATUM_170                                                 \
-    AMD_LEGACY_ERRATUM(AMD_MODEL_RANGE(0x0f, 0x0, 0x0, 0x67, 0xf))
+/* L1 TLB multi-hit causes #MC. */
+static inline bool cpu_has_amd_erratum_383(const struct cpuinfo_x86 *c)
+{
+    return c->vendor == X86_VENDOR_AMD &&
+           (c->family == 0x10 || (c->family == 0x12 && c->model == 0));
+}
 
-#define AMD_ERRATUM_383                                                 \
-    AMD_LEGACY_ERRATUM(AMD_MODEL_RANGE(0x10, 0x2, 0x1, 0xff, 0xf),      \
-                       AMD_MODEL_RANGE(0x12, 0x0, 0x0, 0x00, 0xf))
-
-#define AMD_ERRATUM_573                                                        
\
-    AMD_LEGACY_ERRATUM(AMD_MODEL_RANGE(0x0f, 0x0, 0x0, 0xff, 0xf),     \
-                       AMD_MODEL_RANGE(0x10, 0x0, 0x0, 0xff, 0xf),     \
-                       AMD_MODEL_RANGE(0x11, 0x0, 0x0, 0xff, 0xf),     \
-                       AMD_MODEL_RANGE(0x12, 0x0, 0x0, 0xff, 0xf))
+/* FSINCOS instruction may corrupt %rip. */
+static inline bool cpu_has_amd_erratum_573(const struct cpuinfo_x86 *c)
+{
+    return c->vendor == X86_VENDOR_AMD && c->family >= 0xf && c->family <= 
0x12;
+}
 
 /*
  * The Zen1 and Zen2 microarchitectures are implemented by AMD (Fam17h) and
@@ -65,9 +58,6 @@
 #define is_zen5_uarch() (!boot_cpu_has(X86_FEATURE_FRED))
 #define is_zen6_uarch()   boot_cpu_has(X86_FEATURE_FRED)
 
-struct cpuinfo_x86;
-int cpu_has_amd_erratum(const struct cpuinfo_x86 *cpu, ...);
-
 extern int8_t opt_allow_unsafe;
 
 void fam10h_check_enable_mmcfg(void);
diff --git a/xen/arch/x86/x86_emulate/fpu.c b/xen/arch/x86/x86_emulate/fpu.c
index 88df238981..c024f3c601 100644
--- a/xen/arch/x86/x86_emulate/fpu.c
+++ b/xen/arch/x86/x86_emulate/fpu.c
@@ -12,10 +12,9 @@
 
 #ifdef __XEN__
 # include <asm/amd.h>
-# define cpu_has_amd_erratum(nr) \
-         cpu_has_amd_erratum(&current_cpu_data, AMD_ERRATUM_##nr)
+# define cpu_has_amd_erratum_573() cpu_has_amd_erratum_573(&current_cpu_data)
 #else
-# define cpu_has_amd_erratum(nr) 0
+# define cpu_has_amd_erratum_573() false
 #endif
 
 /* Floating point status word definitions. */
@@ -133,7 +132,7 @@ int x86emul_fpu(struct x86_emulate_state *s,
         switch ( s->modrm )
         {
         case 0xfb: /* fsincos */
-            fail_if(cpu_has_amd_erratum(573));
+            fail_if(cpu_has_amd_erratum_573());
             /* fall through */
         case 0xc0 ... 0xc7: /* fld %stN */
         case 0xc8 ... 0xcf: /* fxch %stN */
diff --git a/xen/arch/x86/x86_emulate/x86_emulate.c 
b/xen/arch/x86/x86_emulate/x86_emulate.c
index 5cd97eca8a..d893ecb296 100644
--- a/xen/arch/x86/x86_emulate/x86_emulate.c
+++ b/xen/arch/x86/x86_emulate/x86_emulate.c
@@ -12,7 +12,6 @@
 
 # include <xen/event.h>
 
-# include <asm/amd.h> /* cpu_has_amd_erratum() */
 # include <asm/processor.h> /* current_cpu_info */
 # include <asm/x86_emulate.h>
 # include <asm/xstate.h>
@@ -22,9 +21,6 @@
 # undef cpuid
 # undef wbinvd
 
-# define cpu_has_amd_erratum(nr) \
-         cpu_has_amd_erratum(&current_cpu_data, AMD_ERRATUM_##nr)
-
 #else /* !__XEN__ */
 
 /* See gcc bug 100680, but here don't bother making this version dependent. */
@@ -34,7 +30,6 @@
      (typeof(x))x_;                        \
 })
 
-# define cpu_has_amd_erratum(nr) 0
 # define cpu_has_mpx false
 # define read_bndcfgu() 0
 # define xstate_set_init(what)
-- 
2.43.0




 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.