Skip to contentSkip to conversation

x86: Add in-scope files under cpu/ to MISRA scans

PATCH Superseded Alejandro Vallejo 4 participants 13 messages Nov 18 – Nov 19

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

cover x86: Add in-scope files under cpu/ to MISRA scans

Hi,

The files under cpu/ are excluded from MISRA scans due to their Linux provenance but they aren't kept in close sync. At best they get semiregular updates (mwait_idle.c), but most often the files grow with Xen specifics bops that get a free pass on MISRA scans merely due to colocation with ancient Linux code. Some files just were never part of Linux (hygon.c) to begin with, which makes the blanket exclusion even less credible.

This series brings everything under the MISRA scan umbrella, with the surprisingly few fixes involved.

eclair pipeline: https://gitlab.com/xen-project/people/agvallejo/xen/-/jobs/18149225087
regular pipeline (still running): https://gitlab.com/xen-project/people/agvallejo/xen/-/pipelines/3085381461

Alejandro Vallejo (5):
x86/cpu: Add break to early_cpu_init() switch x86/mtrr: Address MISRA C Rules 5.3, 8.3 and 8.4 x86/cpu: Use U suffixes on integer constants x86/amd: Make cpu_has_amd_erratum() non-variadic eclair/x86: Scan in-scope files inside cpu/

 .../eclair_analysis/ECLAIR/deviations.ecl     |  3 -
 docs/misra/exclude-list.json                  | 36 -----------
 xen/arch/x86/cpu/amd.c                        | 62 +++++++++----------
 xen/arch/x86/cpu/centaur.c                    |  4 +-
 xen/arch/x86/cpu/common.c                     | 49 ++++++++-------
 xen/arch/x86/cpu/hygon.c                      |  8 +--
 xen/arch/x86/cpu/mtrr/generic.c               |  6 +-
 xen/arch/x86/cpu/mtrr/main.c                  |  4 +-
 xen/arch/x86/include/asm/amd.h                | 11 ++--
 9 files changed, 73 insertions(+), 110 deletions(-)
Signature · 1 line
2.43.0
#1 lore raw

PATCH 1/5 x86/cpu: Add break to early_cpu_init() switch

MISRA C Rule 16.3 requires every switch clause to be terminated by an unconditional break statement.

No functional change.

---
 xen/arch/x86/cpu/common.c | 1 +
 1 file changed, 1 insertion(+)
xen/arch/x86/cpu/common.c +1 -0
index 43785d19cc..54e3a02dd1 100644
@@ -341,6 +341,7 @@ void __init early_cpu_init(bool verbose)
 		printk(XENLOG_ERR
 		       "Unrecognised or unsupported CPU vendor '%.12s'\n",
 		       c->x86_vendor_id);
+		break;
 	}
 
 	cpuid(0x00000001, &eax, &ebx, &ecx, &edx);
Signature · 1 line
2.43.0
#3 lore raw

PATCH 2/5 x86/mtrr: Address MISRA C Rules 5.3, 8.3 and 8.4

In reply to Alejandro Vallejo

Define num_var_ranges with the type it is declared with in mtrr.h (Rule 8.3), make usage_table static as nothing outside main.c uses it (Rule 8.4), and rename the local variable in is_var_mtrr_overlapped() that hides the global num_var_ranges (Rule 5.3).

No functional change.

---
 xen/arch/x86/cpu/mtrr/generic.c | 6 +++---
 xen/arch/x86/cpu/mtrr/main.c    | 4 ++--
 2 files changed, 5 insertions(+), 5 deletions(-)
xen/arch/x86/cpu/mtrr/generic.c +3 -3
index 4cc7c8bd5d..5df3cf491c 100644
@@ -57,9 +57,9 @@ get_fixed_ranges(mtrr_type * frs)
 bool is_var_mtrr_overlapped(const struct mtrr_state *m)
 {
     unsigned int seg, i;
-    unsigned int num_var_ranges = MASK_EXTR(m->mtrr_cap, MTRRcap_VCNT);
+    unsigned int nr_ranges = MASK_EXTR(m->mtrr_cap, MTRRcap_VCNT);
 
-    for ( i = 0; i < num_var_ranges; i++ )
+    for ( i = 0; i < nr_ranges; i++ )
     {
         uint64_t base1 = m->var_ranges[i].base >> PAGE_SHIFT;
         uint64_t mask1 = m->var_ranges[i].mask >> PAGE_SHIFT;
@@ -67,7 +67,7 @@ bool is_var_mtrr_overlapped(const struct mtrr_state *m)
         if ( !(m->var_ranges[i].mask & MTRR_PHYSMASK_VALID) )
             continue;
 
-        for ( seg = i + 1; seg < num_var_ranges; seg++ )
+        for ( seg = i + 1; seg < nr_ranges; seg++ )
         {
             uint64_t base2 = m->var_ranges[seg].base >> PAGE_SHIFT;
             uint64_t mask2 = m->var_ranges[seg].mask >> PAGE_SHIFT;
xen/arch/x86/cpu/mtrr/main.c +2 -2
index eff0500f0d..e49ac1b508 100644
@@ -50,9 +50,9 @@
 #define	get_cpu()	smp_processor_id()
 #define put_cpu()	do {} while(0)
 
-u32 __read_mostly num_var_ranges = 0;
+unsigned int __read_mostly num_var_ranges = 0;
 
-unsigned int *__read_mostly usage_table;
+static unsigned int *__read_mostly usage_table;
 static DEFINE_MUTEX(mtrr_mutex);
 
 u64 __read_mostly size_or_mask;
Signature · 1 line
2.43.0
Acked-by on v1 2/5
#4 lore raw

Re: [PATCH 2/5] x86/mtrr: Address MISRA C Rules 5.3, 8.3 and 8.4

xen/arch/x86/cpu/mtrr/main.c line 53

Quoted 10 lines · Alejandro Vallejo
xen/arch/x86/cpu/mtrr/main.c +1 -1
index eff0500f0d..e49ac1b508 100644
@@ -50,9 +50,9 @@
 #define	get_cpu()	smp_processor_id()
 #define put_cpu()	do {} while(0)
 
-u32 __read_mostly num_var_ranges = 0;
+unsigned int __read_mostly num_var_ranges = 0;

As you're editing the line anyway, the = 0 can be dropped.  Can be fixed on commit.

#5 lore raw

PATCH 3/5 x86/cpu: Use U suffixes on integer constants

In reply to Alejandro Vallejo

MISRA C Rule 7.2 requires a 'U' suffix on integer constants of unsigned type, and Rule 7.3 forbids the lowercase 'l' in literal suffixes.

Add the 'U' suffix to the CPUID leaves, MSR indices and other constants above INT_MAX in amd.c, centaur.c, common.c and hygon.c, and uppercase the existing "ul" and "ull" suffixes. While at it, use MSR_K8_PSTATE0 rather than open-coding its value.

No functional change.

---
 xen/arch/x86/cpu/amd.c     | 48 +++++++++++++++++++-------------------
 xen/arch/x86/cpu/centaur.c |  4 ++--
 xen/arch/x86/cpu/common.c  | 48 +++++++++++++++++++-------------------
 xen/arch/x86/cpu/hygon.c   |  8 +++----
 4 files changed, 54 insertions(+), 54 deletions(-)
xen/arch/x86/cpu/amd.c +24 -24
index 21f82990db..fbbba0005c 100644
@@ -58,7 +58,7 @@ static inline int rdmsr_amd_safe(unsigned int msr, unsigned int *lo,
         ".previous\n"
         _ASM_EXTABLE(1b, 3b)
         : "=a" (*lo), "=d" (*hi), "=r" (err)
-        : "c" (msr), "D" (0x9c5a203a), "2" (0), "i" (-EFAULT) );
+        : "c" (msr), "D" (0x9c5a203aU), "2" (0), "i" (-EFAULT) );
 
     return err;
 #endif
@@ -71,7 +71,7 @@ static inline int wrmsr_amd_safe(unsigned int msr, unsigned int lo,
         "1: wrmsr\n\t"
         _ASM_EXTABLE(1b, %l[fault])
         :
-        : "c" (msr), "a" (lo), "d" (hi), "D" (0x9c5a203a)
+        : "c" (msr), "a" (lo), "d" (hi), "D" (0x9c5a203aU)
         :
         : fault );
 
@@ -85,7 +85,7 @@ static void wrmsr_amd(unsigned int msr, uint64_t val)
 {
 	asm volatile("wrmsr" ::
 		     "c" (msr), "a" ((uint32_t)val),
-		     "d" (val >> 32), "D" (0x9c5a203a));
+		     "d" (val >> 32), "D" (0x9c5a203aU));
 }
 
 /*
@@ -263,7 +263,7 @@ static void __init noinline amd_init_levelling(void)
 	if (levelling_caps & LCAP_e1cd) {
 		uint32_t ecx, edx, tmp;
 
-		cpuid(0x80000001, &tmp, &tmp, &ecx, &edx);
+		cpuid(0x80000001U, &tmp, &tmp, &ecx, &edx);
 
 		/* Fast-forward bits - Must be set. */
 		edx |= cpufeat_mask(X86_FEATURE_APIC);
@@ -444,7 +444,7 @@ static void amd_get_topology(struct cpuinfo_x86 *c)
          * distingush the cores.
          */
         cpu = smp_processor_id();
-        bits = (cpuid_ecx(0x80000008) >> 12) & 0xf;
+        bits = (cpuid_ecx(0x80000008U) >> 12) & 0xf;
 
         if (bits == 0) {
                 while ((1 << bits) < c->x86_max_cores)
@@ -459,7 +459,7 @@ static void amd_get_topology(struct cpuinfo_x86 *c)
         if (cpu_has(c, X86_FEATURE_TOPOEXT)) {
                 u32 eax, ebx, ecx, edx;
 
-                cpuid(0x8000001e, &eax, &ebx, &ecx, &edx);
+                cpuid(0x8000001eU, &eax, &ebx, &ecx, &edx);
                 c->x86_num_siblings = ((ebx >> 8) & 0xff) + 1;
 
                 if (c->family < 0x17)
@@ -531,13 +531,13 @@ void amd_process_freq(const struct cpuinfo_x86 *c,
 		 * Make an attempt at determining the node ID, but assume
 		 * symmetric setup (using node 0) if this fails.
 		 */
-		if (c->extended_cpuid_level >= 0x8000001e &&
+		if (c->extended_cpuid_level >= 0x8000001eU &&
 		    cpu_has(c, X86_FEATURE_TOPOEXT)) {
-			node = cpuid_ecx(0x8000001e) & 0xff;
+			node = cpuid_ecx(0x8000001eU) & 0xff;
 			if (node > 7)
 				node = 0;
 		} else if (cpu_has(c, X86_FEATURE_NODEID_MSR)) {
-			rdmsrl(0xC001100C, val);
+			rdmsrl(0xC001100CU, val);
 			node = val & 7;
 		}
 
@@ -549,14 +549,14 @@ void amd_process_freq(const struct cpuinfo_x86 *c,
 		wrmsrl(MSR_AMD64_NB_CFG,
 		       nbcfg | (1ULL << AMD64_NB_CFG_CF8_EXT_ENABLE_BIT));
 #define PCI_ECS_ADDRESS(sbdf, reg) \
-    (0x80000000 | ((sbdf).bdf << 8) | ((reg) & 0xfc) | (((reg) & 0xf00) << 16))
+    (0x80000000U | ((sbdf).bdf << 8) | ((reg) & 0xfc) | (((reg) & 0xf00) << 16))
 
 		for ( ; ; ) {
 			pci_sbdf_t sbdf = PCI_SBDF(0, 0, 0x18 | node, 4);
 
 			switch (pci_conf_read32(sbdf, PCI_VENDOR_ID)) {
 			case 0x00000000:
-			case 0xffffffff:
+			case 0xffffffffU:
 				/* No device at this SBDF. */
 				if (!node)
 					break;
@@ -594,21 +594,21 @@ void amd_process_freq(const struct cpuinfo_x86 *c,
 
 	lo = 0; /* gcc may not recognize the loop having at least 5 iterations */
 	for (h = c->family == 0x10 ? 5 : 8; h--; )
-		if (!rdmsr_safe(0xC0010064 + h, &lo) && (lo >> 63))
+		if (!rdmsr_safe(MSR_K8_PSTATE0 + h, &lo) && (lo >> 63))
 			break;
 	if (!(lo >> 63))
 		return;
 
 	if (idx && idx < h &&
-	    !rdmsr_safe(0xC0010064 + idx, &val) && (val >> 63) &&
-	    !rdmsr_safe(0xC0010064, &hi) && (hi >> 63)) {
+	    !rdmsr_safe(MSR_K8_PSTATE0 + idx, &val) && (val >> 63) &&
+	    !rdmsr_safe(MSR_K8_PSTATE0, &hi) && (hi >> 63)) {
 		if (nom_mhz)
 			*nom_mhz = amd_parse_freq(c->family, val);
 		if (low_mhz)
 			*low_mhz = amd_parse_freq(c->family, lo);
 		if (hi_mhz)
 			*hi_mhz = amd_parse_freq(c->family, hi);
-	} else if (h && !rdmsr_safe(0xC0010064, &hi) && (hi >> 63)) {
+	} else if (h && !rdmsr_safe(MSR_K8_PSTATE0, &hi) && (hi >> 63)) {
 		if (low_mhz)
 			*low_mhz = amd_parse_freq(c->family, lo);
 		if (hi_mhz)
@@ -662,7 +662,7 @@ static bool set_legacy_ssbd(const struct cpuinfo_x86 *c, bool enable)
 	}
 
 	if (bit >= 0) {
-		uint64_t val, mask = 1ull << bit;
+		uint64_t val, mask = 1ULL << bit;
 
 		if (rdmsr_safe(MSR_AMD64_LS_CFG, &val) ||
 		    ({
@@ -728,8 +728,8 @@ bool __init amd_setup_legacy_ssbd(void)
 	 * c->cpu_core_id is sparse on all but the top end CPUs.  Derive
 	 * max_cpus from ApicIdCoreIdSize which will cover any sparseness.
 	 */
-	if (boot_cpu_data.extended_cpuid_level >= 0x80000008) {
-		ssbd_max_cores = 1u << MASK_EXTR(cpuid_ecx(0x80000008), 0xf000);
+	if (boot_cpu_data.extended_cpuid_level >= 0x80000008U) {
+		ssbd_max_cores = 1u << MASK_EXTR(cpuid_ecx(0x80000008U), 0xf000);
 		ssbd_max_cores /= boot_cpu_data.x86_num_siblings;
 	}
 	if (!ssbd_max_cores)
@@ -838,7 +838,7 @@ void __init detect_zen2_null_seg_behaviour(void)
 static void cf_check fam17_disable_c6(void *arg)
 {
 	/* Disable C6 by clearing the CCR{0,1,2}_CC6EN bits. */
-	const uint64_t mask = ~((1ul << 6) | (1ul << 14) | (1ul << 22));
+	const uint64_t mask = ~((1UL << 6) | (1UL << 14) | (1UL << 22));
 	uint64_t val;
 
 	if (!fam17_c6_disabled) {
@@ -1095,8 +1095,8 @@ static void cf_check init_amd(struct cpuinfo_x86 *c)
 		 * (AMD Erratum #110, docId: 25759).
 		 */
 		__clear_bit(X86_FEATURE_LAHF_LM, c->x86_capability);
-		if (!rdmsr_amd_safe(0xc001100d, &l, &h))
-			wrmsr_amd_safe(0xc001100d, l, h & ~1);
+		if (!rdmsr_amd_safe(0xc001100dU, &l, &h))
+			wrmsr_amd_safe(0xc001100dU, l, h & ~1);
 	}
 
 	/*
@@ -1180,11 +1180,11 @@ static void cf_check init_amd(struct cpuinfo_x86 *c)
 
 	display_cacheinfo(c);
 
-	if (c->extended_cpuid_level >= 0x80000008) {
-		c->x86_max_cores = (cpuid_ecx(0x80000008) & 0xff) + 1;
+	if (c->extended_cpuid_level >= 0x80000008U) {
+		c->x86_max_cores = (cpuid_ecx(0x80000008U) & 0xff) + 1;
 	}
 
-	if (c->extended_cpuid_level >= 0x80000007) {
+	if (c->extended_cpuid_level >= 0x80000007U) {
 		if (cpu_has(c, X86_FEATURE_ITSC)) {
 			__set_bit(X86_FEATURE_CONSTANT_TSC, c->x86_capability);
 			__set_bit(X86_FEATURE_NONSTOP_TSC, c->x86_capability);
xen/arch/x86/cpu/centaur.c +2 -2
index 9123b05dc1..1e756d6c36 100644
@@ -21,8 +21,8 @@ static void init_c3(struct cpuinfo_x86 *c)
 	uint64_t msr_content;
 
 	/* Test for Centaur Extended Feature Flags presence */
-	if (cpuid_eax(0xC0000000) >= 0xC0000001) {
-		u32 tmp = cpuid_edx(0xC0000001);
+	if (cpuid_eax(0xC0000000U) >= 0xC0000001U) {
+		u32 tmp = cpuid_edx(0xC0000001U);
 
 		/* enable ACE unit, if present and disabled */
 		if ((tmp & (ACE_PRESENT | ACE_ENABLED)) == ACE_PRESENT) {
xen/arch/x86/cpu/common.c +24 -24

99 lines not on this page · raw message

xen/arch/x86/cpu/hygon.c +4 -4
index ef19c2b367..1f88275ce2 100644
@@ -17,7 +17,7 @@ static void hygon_get_topology(struct cpuinfo_x86 *c)
 	/* Socket ID is ApicId[6] for Hygon processors. */
 	c->phys_proc_id >>= APICID_SOCKET_ID_BIT;
 
-	ebx = cpuid_ebx(0x8000001e);
+	ebx = cpuid_ebx(0x8000001eU);
 	c->x86_num_siblings = ((ebx >> 8) & 0x3) + 1;
 	c->x86_max_cores /= c->x86_num_siblings;
 	c->cpu_core_id = ebx & 0xff;
@@ -66,10 +66,10 @@ static void cf_check init_hygon(struct cpuinfo_x86 *c)
 
 	display_cacheinfo(c);
 
-	if (c->extended_cpuid_level >= 0x80000008)
-		c->x86_max_cores = (cpuid_ecx(0x80000008) & 0xff) + 1;
+	if (c->extended_cpuid_level >= 0x80000008U)
+		c->x86_max_cores = (cpuid_ecx(0x80000008U) & 0xff) + 1;
 
-	if (c->extended_cpuid_level >= 0x80000007) {
+	if (c->extended_cpuid_level >= 0x80000007U) {
 		if (cpu_has(c, X86_FEATURE_ITSC)) {
 			__set_bit(X86_FEATURE_CONSTANT_TSC, c->x86_capability);
 			__set_bit(X86_FEATURE_NONSTOP_TSC, c->x86_capability);
Signature · 1 line
2.43.0
#6 lore raw

Re: [PATCH 3/5] x86/cpu: Use U suffixes on integer constants

xen/arch/x86/cpu/amd.c bool __init amd_setup_legacy_ssbd(void) · line 732

Quoted 12 lines · Alejandro Vallejo
xen/arch/x86/cpu/amd.c +2 -2
index 21f82990db..fbbba0005c 100644
@@ -728,8 +728,8 @@ bool __init amd_setup_legacy_ssbd(void)
 	 * c->cpu_core_id is sparse on all but the top end CPUs.  Derive
 	 * max_cpus from ApicIdCoreIdSize which will cover any sparseness.
 	 */
-	if (boot_cpu_data.extended_cpuid_level >= 0x80000008) {
-		ssbd_max_cores = 1u << MASK_EXTR(cpuid_ecx(0x80000008), 0xf000);
+	if (boot_cpu_data.extended_cpuid_level >= 0x80000008U) {
+		ssbd_max_cores = 1u << MASK_EXTR(cpuid_ecx(0x80000008U), 0xf000);

1U.

I guess the case of u/U isn't blocking in Eclair?

Can be fixed on commit, if this is the only example.

~Andrew

#7 lore raw

Re: [PATCH 3/5] x86/cpu: Use U suffixes on integer constants

Quoted 16 lines · Andrew Cooper

xen/arch/x86/cpu/amd.c bool __init amd_setup_legacy_ssbd(void) · line 732

Quoted 12 lines · Alejandro Vallejo
xen/arch/x86/cpu/amd.c +2 -2
index 21f82990db..fbbba0005c 100644
@@ -728,8 +728,8 @@ bool __init amd_setup_legacy_ssbd(void)
 	 * c->cpu_core_id is sparse on all but the top end CPUs.  Derive
 	 * max_cpus from ApicIdCoreIdSize which will cover any sparseness.
 	 */
-	if (boot_cpu_data.extended_cpuid_level >= 0x80000008) {
-		ssbd_max_cores = 1u << MASK_EXTR(cpuid_ecx(0x80000008), 0xf000);
+	if (boot_cpu_data.extended_cpuid_level >= 0x80000008U) {
+		ssbd_max_cores = 1u << MASK_EXTR(cpuid_ecx(0x80000008U), 0xf000);

1U.

Quoted 1 line

I guess the case of u/U isn't blocking in Eclair?

Iirc Misra only objects to l, not u. Us also wanting U (alongside L) is therefore purely a style decision.

Jan

#8 lore raw

PATCH 4/5 x86/amd: Make cpu_has_amd_erratum() non-variadic

In reply to Alejandro Vallejo

cpu_has_amd_erratum() takes the OSVW id and the family-model-stepping ranges of an erratum as variadic arguments, which MISRA C Rule 17.1 forbids.

Have AMD_LEGACY_ERRATUM() and AMD_OSVW_ERRATUM() expand to a zero-terminated array instead, and have cpu_has_amd_erratum() walk it. Callers pass the AMD_ERRATUM_* macros, so they are unaffected.

No functional change.

---
 xen/arch/x86/cpu/amd.c         | 14 ++++++--------
 xen/arch/x86/include/asm/amd.h | 11 +++++++----
 2 files changed, 13 insertions(+), 12 deletions(-)
xen/arch/x86/cpu/amd.c +6 -8
index fbbba0005c..5e22fff81c 100644
@@ -321,10 +321,10 @@ void amd_set_cpuid_user_dis(bool enable)
  * 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, int osvw_id, ...)
+int cpu_has_amd_erratum(const struct cpuinfo_x86 *cpu, const int *erratum)
 {
-	va_list ap;
-	u32 range;
+	int osvw_id = erratum[0];
+	const int *p;
 	u32 ms;
 	
 	if (cpu->vendor != X86_VENDOR_AMD)
@@ -346,19 +346,17 @@ int cpu_has_amd_erratum(const struct cpuinfo_x86 *cpu, int osvw_id, ...)
 	}
 
 	/* OSVW unavailable or ID unknown, match family-model-stepping range */
-	va_start(ap, osvw_id);
-
 	ms = (cpu->model << 4) | cpu->stepping;
-	while ((range = va_arg(ap, int))) {
+	for (p = &erratum[1]; *p; p++) {
+		u32 range = *p;
+
 		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;
 }
 
xen/arch/x86/include/asm/amd.h +7 -4
index d21df0741a..d897943210 100644
@@ -12,7 +12,8 @@
  * Errata are defined using the AMD_LEGACY_ERRATUM() or AMD_OSVW_ERRATUM()
  * macros. The latter is intended for newer errata that have an OSVW id
  * assigned, which it takes as first argument. Both take a variable number
- * of family-specific model-stepping ranges created by AMD_MODEL_RANGE().
+ * of family-specific model-stepping ranges created by AMD_MODEL_RANGE(),
+ * and yield a zero-terminated array to pass to cpu_has_amd_erratum().
  *
  * Example 1:
  * #define AMD_ERRATUM_319                                              \
@@ -26,8 +27,10 @@
  *   
  */
 
-#define AMD_LEGACY_ERRATUM(...)         -1 /* legacy */, __VA_ARGS__, 0
-#define AMD_OSVW_ERRATUM(osvw_id, ...)  osvw_id, __VA_ARGS__, 0
+#define AMD_LEGACY_ERRATUM(...) \
+    ((const int[]){ -1 /* legacy */, __VA_ARGS__, 0 })
+#define AMD_OSVW_ERRATUM(osvw_id, ...) \
+    ((const int[]){ osvw_id, __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))
@@ -72,7 +75,7 @@
 #define is_zen6_uarch()   boot_cpu_has(X86_FEATURE_FRED)
 
 struct cpuinfo_x86;
-int cpu_has_amd_erratum(const struct cpuinfo_x86 *cpu, int osvw_id, ...);
+int cpu_has_amd_erratum(const struct cpuinfo_x86 *cpu, const int *erratum);
 
 extern int8_t opt_allow_unsafe;
 
Signature · 1 line
2.43.0
#9 lore raw

Re: [PATCH 4/5] x86/amd: Make cpu_has_amd_erratum() non-variadic

Quoted 85 lines · Alejandro Vallejo

cpu_has_amd_erratum() takes the OSVW id and the family-model-stepping ranges of an erratum as variadic arguments, which MISRA C Rule 17.1 forbids.

Have AMD_LEGACY_ERRATUM() and AMD_OSVW_ERRATUM() expand to a zero-terminated array instead, and have cpu_has_amd_erratum() walk it. Callers pass the AMD_ERRATUM_* macros, so they are unaffected.

No functional change.

---
 xen/arch/x86/cpu/amd.c         | 14 ++++++--------
 xen/arch/x86/include/asm/amd.h | 11 +++++++----
 2 files changed, 13 insertions(+), 12 deletions(-)
xen/arch/x86/cpu/amd.c +6 -8
index fbbba0005c..5e22fff81c 100644
@@ -321,10 +321,10 @@ void amd_set_cpuid_user_dis(bool enable)
  * 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, int osvw_id, ...)
+int cpu_has_amd_erratum(const struct cpuinfo_x86 *cpu, const int *erratum)
 {
-	va_list ap;
-	u32 range;
+	int osvw_id = erratum[0];
+	const int *p;
 	u32 ms;
 
 	if (cpu->vendor != X86_VENDOR_AMD)
@@ -346,19 +346,17 @@ int cpu_has_amd_erratum(const struct cpuinfo_x86 *cpu, int osvw_id, ...)
 	}
 
 	/* OSVW unavailable or ID unknown, match family-model-stepping range */
-	va_start(ap, osvw_id);
-
 	ms = (cpu->model << 4) | cpu->stepping;
-	while ((range = va_arg(ap, int))) {
+	for (p = &erratum[1]; *p; p++) {
+		u32 range = *p;
+
 		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;
 }
 
xen/arch/x86/include/asm/amd.h +6 -3
index d21df0741a..d897943210 100644
@@ -12,7 +12,8 @@
  * Errata are defined using the AMD_LEGACY_ERRATUM() or AMD_OSVW_ERRATUM()
  * macros. The latter is intended for newer errata that have an OSVW id
  * assigned, which it takes as first argument. Both take a variable number
- * of family-specific model-stepping ranges created by AMD_MODEL_RANGE().
+ * of family-specific model-stepping ranges created by AMD_MODEL_RANGE(),
+ * and yield a zero-terminated array to pass to cpu_has_amd_erratum().
  *
  * Example 1:
  * #define AMD_ERRATUM_319                                              \
@@ -26,8 +27,10 @@
  *   
  */
 
-#define AMD_LEGACY_ERRATUM(...)         -1 /* legacy */, __VA_ARGS__, 0
-#define AMD_OSVW_ERRATUM(osvw_id, ...)  osvw_id, __VA_ARGS__, 0
+#define AMD_LEGACY_ERRATUM(...) \
+    ((const int[]){ -1 /* legacy */, __VA_ARGS__, 0 })
+#define AMD_OSVW_ERRATUM(osvw_id, ...) \
+    ((const int[]){ osvw_id, __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))

xen/arch/x86/include/asm/amd.h line 81

Quoted 8 lines
xen/arch/x86/include/asm/amd.h +1 -1
@@ -72,7 +75,7 @@
 #define is_zen6_uarch()   boot_cpu_has(X86_FEATURE_FRED)
 
 struct cpuinfo_x86;
-int cpu_has_amd_erratum(const struct cpuinfo_x86 *cpu, int osvw_id, ...);
+int cpu_has_amd_erratum(const struct cpuinfo_x86 *cpu, const int *erratum);
 
 extern int8_t opt_allow_unsafe;
 

This whole infrastructure is garbage.  I believe Linux dropped it a long time back.

If you see, we only ever encode LEGACY errata with it, meaning the osvw_id is always -1, meaning the OSVW MSR accesses is cpu_has_amd_erratum() are truly unreachable even in safety builds.

Next, have you seen what a disaster of code generation this actually causes at callers?  It's an anonymous array on the stack, which means it's (re)initialised every time it's called.  One of these is even in domain_create().

OSVW was a short-sighted and now abandoned idea of AMDs.  There are something like 4(?) bits defined, but there's no index of what they are so you have to search all the revision guides to find them.

Could I at least talk you into dropping the osvw_id part?  Xen does nothing with OSVW at all, and if we ever feel the need to gain some use, it will not be like this.

But, if you were to see about moving the arrays out to being static objects too, I think the result would be a whole lot less unpalatable.

~Andrew

#10 lore raw

Re: [PATCH 4/5] x86/amd: Make cpu_has_amd_erratum() non-variadic

Quoted 92 lines · Andrew Cooper
Quoted 91 lines · Alejandro Vallejo

cpu_has_amd_erratum() takes the OSVW id and the family-model-stepping ranges of an erratum as variadic arguments, which MISRA C Rule 17.1 forbids.

Have AMD_LEGACY_ERRATUM() and AMD_OSVW_ERRATUM() expand to a zero-terminated array instead, and have cpu_has_amd_erratum() walk it. Callers pass the AMD_ERRATUM_* macros, so they are unaffected.

No functional change.

---
 xen/arch/x86/cpu/amd.c         | 14 ++++++--------
 xen/arch/x86/include/asm/amd.h | 11 +++++++----
 2 files changed, 13 insertions(+), 12 deletions(-)
xen/arch/x86/cpu/amd.c +6 -8
index fbbba0005c..5e22fff81c 100644
@@ -321,10 +321,10 @@ void amd_set_cpuid_user_dis(bool enable)
  * 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, int osvw_id, ...)
+int cpu_has_amd_erratum(const struct cpuinfo_x86 *cpu, const int *erratum)
 {
-	va_list ap;
-	u32 range;
+	int osvw_id = erratum[0];
+	const int *p;
 	u32 ms;
 
 	if (cpu->vendor != X86_VENDOR_AMD)
@@ -346,19 +346,17 @@ int cpu_has_amd_erratum(const struct cpuinfo_x86 *cpu, int osvw_id, ...)
 	}
 
 	/* OSVW unavailable or ID unknown, match family-model-stepping range */
-	va_start(ap, osvw_id);
-
 	ms = (cpu->model << 4) | cpu->stepping;
-	while ((range = va_arg(ap, int))) {
+	for (p = &erratum[1]; *p; p++) {
+		u32 range = *p;
+
 		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;
 }
 
xen/arch/x86/include/asm/amd.h +7 -4
index d21df0741a..d897943210 100644
@@ -12,7 +12,8 @@
  * Errata are defined using the AMD_LEGACY_ERRATUM() or AMD_OSVW_ERRATUM()
  * macros. The latter is intended for newer errata that have an OSVW id
  * assigned, which it takes as first argument. Both take a variable number
- * of family-specific model-stepping ranges created by AMD_MODEL_RANGE().
+ * of family-specific model-stepping ranges created by AMD_MODEL_RANGE(),
+ * and yield a zero-terminated array to pass to cpu_has_amd_erratum().
  *
  * Example 1:
  * #define AMD_ERRATUM_319                                              \
@@ -26,8 +27,10 @@
  *   
  */
 
-#define AMD_LEGACY_ERRATUM(...)         -1 /* legacy */, __VA_ARGS__, 0
-#define AMD_OSVW_ERRATUM(osvw_id, ...)  osvw_id, __VA_ARGS__, 0
+#define AMD_LEGACY_ERRATUM(...) \
+    ((const int[]){ -1 /* legacy */, __VA_ARGS__, 0 })
+#define AMD_OSVW_ERRATUM(osvw_id, ...) \
+    ((const int[]){ osvw_id, __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))
@@ -72,7 +75,7 @@
 #define is_zen6_uarch()   boot_cpu_has(X86_FEATURE_FRED)
 
 struct cpuinfo_x86;
-int cpu_has_amd_erratum(const struct cpuinfo_x86 *cpu, int osvw_id, ...);
+int cpu_has_amd_erratum(const struct cpuinfo_x86 *cpu, const int *erratum);
 
Quoted 5 lines

xen/arch/x86/include/asm/amd.h line 81

Quoted 2 lines
xen/arch/x86/include/asm/amd.h +0 -0
 extern int8_t opt_allow_unsafe;
 

This whole infrastructure is garbage.  I believe Linux dropped it a long time back.

I agree. I can only describe it as an overcomplicated mess.

Quoted 4 lines

If you see, we only ever encode LEGACY errata with it, meaning the osvw_id is always -1, meaning the OSVW MSR accesses is cpu_has_amd_erratum() are truly unreachable even in safety builds.

errata 383 (out of context) matches on OSVW before attempting the fam/model match.

Quoted 5 lines

Next, have you seen what a disaster of code generation this actually causes at callers?  It's an anonymous array on the stack, which means it's (re)initialised every time it's called.  One of these is even in domain_create().

Not to say any of this is GOOD. But it is very definitely not worse. A variadic function is... pretty much that. They are largely equivalent.

Debug builds are slightly bigger with the anonymous arrays due to optimisation quirks, but release builds are slightly smaller (See bloat-o-meter):

add/remove: 0/0 grow/shrink: 4/2 up/down: 61/-113 (-52)
Function old new delta

svm_asid_init                                 39      60     +21
init_amd                                    2419    2435     +16
arch_domain_create                          1043    1059     +16

_svm_cpu_up 444 452 +8
x86emul_fpu 7369 7321 -48
cpu_has_amd_erratum 260 195 -65
Total: Before=3607126, After=3607074, chg -0.00%

Quoted 11 lines

OSVW was a short-sighted and now abandoned idea of AMDs.  There are something like 4(?) bits defined, but there's no index of what they are so you have to search all the revision guides to find them.

Could I at least talk you into dropping the osvw_id part?  Xen does nothing with OSVW at all, and if we ever feel the need to gain some use, it will not be like this.

But, if you were to see about moving the arrays out to being static objects too, I think the result would be a whole lot less unpalatable.

I don't mind refactoring this mess a bit more. I may try removing the arrays altogether and integrating them into the function itself.

Note cpu_has_amd_erratum() does perform the OSVW lookup for errata 383. Did you _really_ mean to drop that? It's not immediately obvious to me whether the fam/models listed cover everything OSVW does.

Cheers,
Alejandro

#11 lore raw

PATCH 5/5 eclair/x86: Scan in-scope files inside cpu/

In reply to Alejandro Vallejo

Remove the exclusions now that all in-scope files inside are clean on the regression subset.

---
 .../eclair_analysis/ECLAIR/deviations.ecl     |  3 --
 docs/misra/exclude-list.json                  | 36 -------------------
 2 files changed, 39 deletions(-)
automation/eclair_analysis/ECLAIR/deviations.ecl +0 -3
index d49af53055..9fcba4df16 100644
@@ -186,9 +186,6 @@ context of R7.2."
 -file_tag+={adopted_r7_2,"^xen/include/efi/efiapi\\.h$"}
 -file_tag+={adopted_r7_2,"^xen/include/efi/efidef\\.h$"}
 -file_tag+={adopted_r7_2,"^xen/include/efi/efiprot\\.h$"}
--file_tag+={adopted_r7_2,"^xen/arch/x86/cpu/intel\\.c$"}
--file_tag+={adopted_r7_2,"^xen/arch/x86/cpu/amd\\.c$"}
--file_tag+={adopted_r7_2,"^xen/arch/x86/cpu/common\\.c$"}
 -config=MC3A2.R7.2,reports+={deliberate,"any_area(any_loc(file(adopted_r7_2)))"}
 -doc_end
 
docs/misra/exclude-list.json +0 -36
index 2b874dfd3b..a52f0d6429 100644
@@ -33,42 +33,6 @@
             "rel_path": "arch/x86/acpi/lib.c",
             "comment": "Imported from Linux, ignore for now"
         },
-        {
-            "rel_path": "arch/x86/cpu/amd.c",
-            "comment": "Imported from Linux, ignore for now"
-        },
-        {
-            "rel_path": "arch/x86/cpu/centaur.c",
-            "comment": "Imported from Linux, ignore for now"
-        },
-        {
-            "rel_path": "arch/x86/cpu/common.c",
-            "comment": "Imported from Linux, ignore for now"
-        },
-        {
-            "rel_path": "arch/x86/cpu/hygon.c",
-            "comment": "Imported from Linux, ignore for now"
-        },
-        {
-            "rel_path": "arch/x86/cpu/intel.c",
-            "comment": "Imported from Linux, ignore for now"
-        },
-        {
-            "rel_path": "arch/x86/cpu/intel_cacheinfo.c",
-            "comment": "Imported from Linux, ignore for now"
-        },
-        {
-            "rel_path": "arch/x86/cpu/mcheck/non-fatal.c",
-            "comment": "Imported from Linux, ignore for now"
-        },
-        {
-            "rel_path": "arch/x86/cpu/mtrr/*",
-            "comment": "Imported from Linux, ignore for now"
-        },
-        {
-            "rel_path": "arch/x86/cpu/mwait-idle.c",
-            "comment": "Imported from Linux, ignore for now"
-        },
         {
             "rel_path": "arch/x86/delay.c",
             "comment": "Imported from Linux, ignore for now"
Signature · 1 line
2.43.0
Acked-by on v1 5/5
#12 lore raw

Re: [PATCH 5/5] eclair/x86: Scan in-scope files inside cpu/

Quoted 4 lines · Alejandro Vallejo

Remove the exclusions now that all in-scope files inside are clean on the regression subset.

Thanks for taking care of this. I think further cleanup can be done, looking at the output of

git grep out_of_scope

but probably it's better in a follow-up patch

Quoted 67 lines
---
 .../eclair_analysis/ECLAIR/deviations.ecl     |  3 --
 docs/misra/exclude-list.json                  | 36 -------------------
 2 files changed, 39 deletions(-)
automation/eclair_analysis/ECLAIR/deviations.ecl +0 -4
index d49af53055..9fcba4df16 100644
@@ -186,9 +186,6 @@ context of R7.2."
 -file_tag+={adopted_r7_2,"^xen/include/efi/efiapi\\.h$"}
 -file_tag+={adopted_r7_2,"^xen/include/efi/efidef\\.h$"}
 -file_tag+={adopted_r7_2,"^xen/include/efi/efiprot\\.h$"}
--file_tag+={adopted_r7_2,"^xen/arch/x86/cpu/intel\\.c$"}
--file_tag+={adopted_r7_2,"^xen/arch/x86/cpu/amd\\.c$"}
--file_tag+={adopted_r7_2,"^xen/arch/x86/cpu/common\\.c$"}
 
-config=MC3A2.R7.2,reports+={deliberate,"any_area(any_loc(file(adopted_r7_2)))"}
 -doc_end
docs/misra/exclude-list.json +0 -36
index 2b874dfd3b..a52f0d6429 100644
@@ -33,42 +33,6 @@
             "rel_path": "arch/x86/acpi/lib.c",
             "comment": "Imported from Linux, ignore for now"
         },
-        {
-            "rel_path": "arch/x86/cpu/amd.c",
-            "comment": "Imported from Linux, ignore for now"
-        },
-        {
-            "rel_path": "arch/x86/cpu/centaur.c",
-            "comment": "Imported from Linux, ignore for now"
-        },
-        {
-            "rel_path": "arch/x86/cpu/common.c",
-            "comment": "Imported from Linux, ignore for now"
-        },
-        {
-            "rel_path": "arch/x86/cpu/hygon.c",
-            "comment": "Imported from Linux, ignore for now"
-        },
-        {
-            "rel_path": "arch/x86/cpu/intel.c",
-            "comment": "Imported from Linux, ignore for now"
-        },
-        {
-            "rel_path": "arch/x86/cpu/intel_cacheinfo.c",
-            "comment": "Imported from Linux, ignore for now"
-        },
-        {
-            "rel_path": "arch/x86/cpu/mcheck/non-fatal.c",
-            "comment": "Imported from Linux, ignore for now"
-        },
-        {
-            "rel_path": "arch/x86/cpu/mtrr/*",
-            "comment": "Imported from Linux, ignore for now"
-        },
-        {
-            "rel_path": "arch/x86/cpu/mwait-idle.c",
-            "comment": "Imported from Linux, ignore for now"
-        },
         {
             "rel_path": "arch/x86/delay.c",
             "comment": "Imported from Linux, ignore for now"
Signature · 4 lines
Nicola Vetrini, B.Sc.
Software Engineer
BUGSENG (https://bugseng.com)
LinkedIn: https://www.linkedin.com/in/nicola-vetrini-a42471253