x86: Add in-scope files under cpu/ to MISRA scans
v2 cover x86: Add in-scope files under cpu/ to MISRA scans
Hi,
v1: https://lore.kernel.org/xen-devel/[email protected]/
pipeline: https://gitlab.com/xen-project/people/agvallejo/xen/-/pipelines/3089539179
(slightly different. fpu.c has different macros, but functionally identical)
v2:
I've reworked the cpu_has_amd_erratum() function into small helpers and brought the 383 erratum in line with Linux. It turns out a mistake was made when merging it originally. OSVW is gone and bloat-o-meter reports 200 bytes of net code reduction.
Original cover letter:
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.
Alejandro Vallejo (3):
x86/amd: Consider only Fam10h affected by erratum 383 x86/amd: Drop cpu_has_amd_erratum() 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 | 47 +------------- 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 | 62 ++++++------------- xen/arch/x86/x86_emulate/fpu.c | 7 +-- xen/arch/x86/x86_emulate/x86_emulate.c | 5 -- 9 files changed, 27 insertions(+), 139 deletions(-)
base-commit: 92ddcda17fb00020c6215d51df4c6a25d5eb0b5e
Signature · 1 line
2.43.0
PATCH v2 1/3 x86/amd: Consider only Fam10h affected by erratum 383
2604adda4f81 introduced the cpu_has_amd_erratum() machinary while adding erratum 383. The commit message claims it was based on Linux's, but in addition to cosmetic changes it also added Fam12h as affected. It is not acknowledged as so the relevant Fam12h revision guide, and Linux itself never considered it. My best guess is that OSVW always returns early and doesn't let the "legacy" mechanism ever match Fam12h (the relevant bit is always 0, according to its Revision Guide).
Seeing how all K10 is affected, and Xen only matches OSVW for this erratum, drop OSVW altogether while trimming the list to K10.
---
Fam12h CPUs where firmware wasn't exposing OSVW would change by not (wrongly) applying the workaround. I'm not sure if there may be any such machines. I'm also not sure anyone cares. I wouldn't backport this, thus why I subsumed the Fam12h removal into the OSVW removal.
--- xen/arch/x86/cpu/amd.c | 21 +++------------------ xen/arch/x86/include/asm/amd.h | 21 +++++++-------------- 2 files changed, 10 insertions(+), 32 deletions(-)
xen/arch/x86/cpu/amd.c +3 -18
@@ -321,7 +321,7 @@ 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, ...) {va_list ap;u32 range;
@@ -330,23 +330,8 @@ int cpu_has_amd_erratum(const struct cpuinfo_x86 *cpu, int osvw_id, ...)if (cpu->vendor !=X86_VENDOR_AMD )return 0 ; -if (osvw_id >=0 &&cpu_has (cpu,X86_FEATURE_OSVW )) { -u64 osvw_len; - -rdmsrl (MSR_AMD_OSVW_ID_LENGTH , osvw_len); - -if (osvw_id < osvw_len) { -u64 osvw_bits; - -rdmsrl (MSR_AMD_OSVW_STATUS + (osvw_id >>6 ), - osvw_bits); - -return (osvw_bits >> (osvw_id &0x3f )) &1 ; - } - } - -/* OSVW unavailable or ID unknown, match family-model-stepping range */ -va_start (ap, osvw_id); +/* Match family-model-stepping range */ +va_start (ap, cpu); ms = (cpu->model <<4 ) | cpu->stepping;while ((range =va_arg (ap, int))) {
xen/arch/x86/include/asm/amd.h +7 -14
@@ -9,25 +9,19 @@/* AMD errata checking * -* 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(). +* 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 1: +* 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)) -* Example 2: -* #define AMD_ERRATUM_400 \ -* AMD_OSVW_ERRATUM(1, AMD_MODEL_RANGE(0xf, 0x41, 0x2, 0xff, 0xf), \ -* AMD_MODEL_RANGE(0x10, 0x2, 0x1, 0xff, 0xf)) * */ -#define AMD_LEGACY_ERRATUM(...) -1 /* legacy */, __VA_ARGS__, 0 -#define AMD_OSVW_ERRATUM (osvw_id, ...) osvw_id, __VA_ARGS__, 0 +#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))
@@ -42,8 +36,7 @@ AMD_LEGACY_ERRATUM(AMD_MODEL_RANGE(0x0f, 0x0, 0x0, 0x67, 0xf))#define AMD_ERRATUM_383 \ - AMD_OSVW_ERRATUM(3, AMD_MODEL_RANGE(0x10, 0x2, 0x1, 0xff, 0xf), \ - AMD_MODEL_RANGE(0x12, 0x0, 0x0, 0x1, 0x0)) + AMD_LEGACY_ERRATUM(AMD_MODEL_RANGE(0x10, 0x0, 0x0, 0xff, 0xf))#define AMD_ERRATUM_573 \ AMD_LEGACY_ERRATUM(AMD_MODEL_RANGE(0x0f, 0x0, 0x0, 0xff, 0xf), \
@@ -72,7 +65,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, ...);extern int8_t opt_allow_unsafe;
Signature · 1 line
2.43.0
Re: [PATCH v2 1/3] x86/amd: Consider only Fam10h affected by erratum 383
Quoted 5 lines · Alejandro Vallejo
2604adda4f81introduced thecpu_has_amd_erratum()machinary while adding erratum 383. The commit message claims it was based on Linux's, but in addition to cosmetic changes it also added Fam12h as affected. It is not acknowledged as so the relevant Fam12h revision guide, and Linux itself never considered it.
You need the NDA revision guide, not the public one.
Fam12h, Model 0 Steppings 0 and 1 are affected. It was fixed starting in Model 1.
Quoted 3 lines
My best guess is that OSVW always returns early and
doesn't let the "legacy" mechanism ever match Fam12h (the relevant bit is always 0, according to its Revision Guide).
The OSVW MSRs are fully writeable, with a note saying "OS/VMM should treat them as read-only".
Getting helpful OSVW information requires a firmware update, which means OS/VMM software needs the fallback list irrespective.
This is why we have the legacy list as well, but it also means it suffices to only have the legacy list. (Yes, technically we could be levelled and OSVW says vulnerable outside of the range, but for the sake of one 20-year-old errata, I'm not overly bothered about this corner case).
Anyway, this can be given as the justification for getting rid of OSVW, with the Fam12 range reinstated.
~Andrew
Re: [PATCH v2 1/3] x86/amd: Consider only Fam10h affected by erratum 383
Quoted 11 lines · Andrew Cooper
Quoted 5 lines · Alejandro Vallejo
2604adda4f81introduced thecpu_has_amd_erratum()machinary while adding erratum 383. The commit message claims it was based on Linux's, but in addition to cosmetic changes it also added Fam12h as affected. It is not acknowledged as so the relevant Fam12h revision guide, and Linux itself never considered it.You need the NDA revision guide, not the public one.
Fam12h, Model 0 Steppings 0 and 1 are affected. It was fixed starting in Model 1.
They are 15yo engineering samples...
More interestingly, the fallback list is wrong. It considers model1/stepping0 affected when it shouldn't. THAT's actual silicon people could buy. The range is inclusive, but it was written as exclusive.
I'll rewrite the patch keeping Fam12h, even if it's silly to do so. with the last item being model0/steppingF.
Cheers,
Alejandro
Quoted 21 lines
Quoted 5 lines
My best guess is that OSVW always returns early and
doesn't let the "legacy" mechanism ever match Fam12h (the relevant bit is always 0, according to its Revision Guide).The OSVW MSRs are fully writeable, with a note saying "OS/VMM should treat them as read-only".
Getting helpful OSVW information requires a firmware update, which means OS/VMM software needs the fallback list irrespective.
This is why we have the legacy list as well, but it also means it suffices to only have the legacy list. (Yes, technically we could be levelled and OSVW says vulnerable outside of the range, but for the sake of one 20-year-old errata, I'm not overly bothered about this corner case).
Anyway, this can be given as the justification for getting rid of OSVW, with the Fam12 range reinstated.
~Andrew
PATCH v2 2/3 x86/amd: Drop cpu_has_amd_erratum()
In reply to Alejandro Vallejocpu_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.
No functional change.
--- 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 | 55 ++++++++++---------------- xen/arch/x86/x86_emulate/fpu.c | 7 ++-- xen/arch/x86/x86_emulate/x86_emulate.c | 5 --- 7 files changed, 27 insertions(+), 78 deletions(-)
xen/arch/x86/cpu/amd.c +1 -31
@@ -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"
xen/arch/x86/domain.c +1 -1
@@ -833,7 +833,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 ) {
xen/arch/x86/hvm/svm/asid.c +1 -1
@@ -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);
xen/arch/x86/hvm/svm/svm.c +1 -1
@@ -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 */
xen/arch/x86/include/asm/amd.h +20 -35
72 lines not on this page · raw message
xen/arch/x86/x86_emulate/fpu.c +3 -4
@@ -12,10 +12,9 @@#ifdef __XEN__# include <asm/amd.h> -# define cpu_has_amd_erratum (nr) \ - cpu_has_amd_erratum(¤t_cpu_data, AMD_ERRATUM_##nr) +# define cpu_has_amd_erratum_573 () cpu_has_amd_erratum_573(¤t_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 */
xen/arch/x86/x86_emulate/x86_emulate.c +0 -5
@@ -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(¤t_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)
Signature · 1 line
2.43.0
Re: [PATCH v2 2/3] x86/amd: Drop cpu_has_amd_erratum()
Quoted 16 lines · Alejandro Vallejo
xen/arch/x86/include/asm/amd.h +1
-6@@ -6,43 +6,31 @@#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(). -*
xen/arch/x86/include/asm/amd.h line 14
Quoted 10 lines
xen/arch/x86/include/asm/amd.h +3
-6-* 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. */
These could really do with some information about what they are.
/* Sequential execution across the non-canonical boundary causes hang. */
xen/arch/x86/include/asm/amd.h line 30
Quoted 12 lines
xen/arch/x86/include/asm/amd.h +4
-7+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_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)
/* Incorrect code bytes fetched after a world switch. */
xen/arch/x86/include/asm/amd.h line 33
Quoted 7 lines
xen/arch/x86/include/asm/amd.h +4
-2+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_121 \ - AMD_LEGACY_ERRATUM(AMD_MODEL_RANGE(0x0f, 0x0, 0x0, 0x3f, 0xf))
/* L1 TLB multi-hit causes #MC. */
Quoted 6 lines
xen/arch/x86/include/asm/amd.h +4
-1+static inline bool cpu_has_amd_erratum_383 (const struct cpuinfo_x86 *c) +{ +return c->vendor ==X86_VENDOR_AMD && c->family ==0x10 ; +} -#define AMD_ERRATUM_170 \
xen/arch/x86/include/asm/amd.h line 45
Quoted 10 lines
xen/arch/x86/include/asm/amd.h +0
-10- AMD_LEGACY_ERRATUM(AMD_MODEL_RANGE(0x0f, 0x0, 0x0, 0x67, 0xf)) - -#define AMD_ERRATUM_383 \ - AMD_LEGACY_ERRATUM(AMD_MODEL_RANGE(0x10, 0x0, 0x0, 0xff, 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. */
xen/arch/x86/include/asm/amd.h line 34
Quoted 5 lines
xen/arch/x86/include/asm/amd.h +4
-0+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 ; +}
over the correction in patch 1.
~Andrew
PATCH v2 3/3 eclair/x86: Scan in-scope files inside cpu/
In reply to Alejandro VallejoRemove 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
@@ -189,9 +189,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
@@ -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