Skip to contentSkip to conversation

x86/Kconfig: introduce CENTAUR, HYGON & SHANGHAI config options

PATCH In review Sergiy Kibrik 4 participants 16 messages Aug 16, 2024 – Aug 30, 2024 1 message not in the archive

lore
Share & tools
lore
#1 lore raw

PATCH v2 1/5 x86/Kconfig: introduce CENTAUR, HYGON & SHANGHAI config options

In reply to a message not in the archive

These options aim to represent what's currently supported by Xen, and later to allow tuning for specific platform(s) only.

HYGON and SHANGHAI options depend on AMD and INTEL as there're build dependencies on support code for AMD and Intel CPUs respectively.

CC Alejandro Vallejo <[email protected]>
CC Jan Beulich <[email protected]>
---
 xen/arch/x86/Kconfig.cpu  | 29 +++++++++++++++++++++++++++++
 xen/arch/x86/cpu/Makefile |  6 +++---
 xen/arch/x86/cpu/common.c |  6 ++++++
 3 files changed, 38 insertions(+), 3 deletions(-)
xen/arch/x86/Kconfig.cpu +29 -0
index 5fb18db1aa..ac8f41d464 100644
@@ -10,6 +10,25 @@ config AMD
 	  May be turned off in builds targetting other vendors.  Otherwise,
 	  must be enabled for Xen to work suitably on AMD platforms.
 
+config CENTAUR
+	bool "Support Centaur CPUs"
+	default y
+	help
+	  Detection, tunings and quirks for VIA platforms.
+
+	  May be turned off in builds targeting other vendors. Otherwise, must
+          be enabled for Xen to work suitably on VIA platforms.
+
+config HYGON
+	bool "Support Hygon CPUs"
+	depends on AMD
+	default y
+	help
+	  Detection, tunings and quirks for Hygon platforms.
+
+	  May be turned off in builds targeting other vendors. Otherwise, must
+          be enabled for Xen to work suitably on Hygon platforms.
+
 config INTEL
 	bool "Support Intel CPUs"
 	default y
@@ -19,4 +38,14 @@ config INTEL
 	  May be turned off in builds targetting other vendors.  Otherwise,
 	  must be enabled for Xen to work suitably on Intel platforms.
 
+config SHANGHAI
+	bool "Support Shanghai CPUs"
+	depends on INTEL
+	default y
+	help
+	  Detection, tunings and quirks for Zhaoxin platforms.
+
+	  May be turned off in builds targeting other vendors. Otherwise, must
+          be enabled for Xen to work suitably on Zhaoxin platforms.
+
 endmenu
xen/arch/x86/cpu/Makefile +3 -3
index eafce5f204..80739d0256 100644
@@ -3,13 +3,13 @@ obj-y += microcode/
 obj-y += mtrr/
 
 obj-y += amd.o
-obj-y += centaur.o
+obj-$(CONFIG_CENTAUR) += centaur.o
 obj-y += common.o
-obj-y += hygon.o
+obj-$(CONFIG_HYGON) += hygon.o
 obj-y += intel.o
 obj-y += intel_cacheinfo.o
 obj-y += mwait-idle.o
-obj-y += shanghai.o
+obj-$(CONFIG_SHANGHAI) += shanghai.o
 obj-y += vpmu.o
 obj-$(CONFIG_AMD) += vpmu_amd.o
 obj-$(CONFIG_INTEL) += vpmu_intel.o
xen/arch/x86/cpu/common.c +6 -0
index ff4cd22897..dcc2753212 100644
@@ -339,9 +339,15 @@ void __init early_cpu_init(bool verbose)
 	case X86_VENDOR_INTEL:    intel_unlock_cpuid_leaves(c);
 				  actual_cpu = intel_cpu_dev;    break;
 	case X86_VENDOR_AMD:      actual_cpu = amd_cpu_dev;      break;
+#ifdef CONFIG_CENTAUR
 	case X86_VENDOR_CENTAUR:  actual_cpu = centaur_cpu_dev;  break;
+#endif
+#ifdef CONFIG_SHANGHAI
 	case X86_VENDOR_SHANGHAI: actual_cpu = shanghai_cpu_dev; break;
+#endif
+#ifdef CONFIG_HYGON
 	case X86_VENDOR_HYGON:    actual_cpu = hygon_cpu_dev;    break;
+#endif
 	default:
 		actual_cpu = default_cpu;
 		if (!verbose)
Signature · 1 line
2.25.1
#5 lore raw

PATCH v2 2/5 x86/amd: configurable handling of AMD-specific MSRs access

In reply to a message not in the archive

Do not compile handlers of guest access to AMD-specific MSRs when CONFIG_AMD=n.

---
 xen/arch/x86/msr.c | 4 ++++
 1 file changed, 4 insertions(+)
xen/arch/x86/msr.c +4 -0
index 289cf10b78..4567de7fc8 100644
@@ -219,6 +219,7 @@ int guest_rdmsr(struct vcpu *v, uint32_t msr, uint64_t *val)
         *val = msrs->tsc_aux;
         break;
 
+#ifdef CONFIG_AMD
     case MSR_K8_SYSCFG:
     case MSR_K8_TOP_MEM1:
     case MSR_K8_TOP_MEM2:
@@ -281,6 +282,7 @@ int guest_rdmsr(struct vcpu *v, uint32_t msr, uint64_t *val)
                                    ? 0 : (msr - MSR_AMD64_DR1_ADDRESS_MASK + 1),
                                    ARRAY_SIZE(msrs->dr_mask))];
         break;
+#endif /* CONFIG_AMD */
 
         /*
          * TODO: Implement when we have better topology representation.
@@ -552,6 +554,7 @@ int guest_wrmsr(struct vcpu *v, uint32_t msr, uint64_t val)
             wrmsr_tsc_aux(val);
         break;
 
+#ifdef CONFIG_AMD
     case MSR_VIRT_SPEC_CTRL:
         if ( !cp->extd.virt_ssbd )
             goto gp_fault;
@@ -598,6 +601,7 @@ int guest_wrmsr(struct vcpu *v, uint32_t msr, uint64_t val)
         if ( v == curr && (curr->arch.dr7 & DR7_ACTIVE_MASK) )
             wrmsrl(msr, val);
         break;
+#endif /* CONFIG_AMD */
 
     default:
         return X86EMUL_UNHANDLEABLE;
Signature · 1 line
2.25.1
#7 lore raw

PATCH v2 3/5 x86/spec-ctrl: configurable Intlel/AMD-specific calculations

In reply to a message not in the archive

Put platforms-specific code under #ifdef CONFIG_{AMD,INTEL} so that when corresponding CPU support is disabled by configuration less dead code will end up in the build.

This includes re-ordering of calls to ibpb_calculations() & div_calculations(), but since they don't access common variables or feature bits it should be safe to do.

---
 xen/arch/x86/spec_ctrl.c | 17 +++++++++++++----
 1 file changed, 13 insertions(+), 4 deletions(-)
xen/arch/x86/spec_ctrl.c +13 -4
index 75a4177a75..ba6c3e80d2 100644
@@ -1012,6 +1012,7 @@ static bool __init should_use_eager_fpu(void)
     }
 }
 
+#ifdef CONFIG_AMD
 /*
  * https://www.amd.com/content/dam/amd/en/documents/corporate/cr/speculative-return-stack-overflow-whitepaper.pdf
  */
@@ -1110,6 +1111,7 @@ static void __init div_calculations(bool hw_smt_enabled)
             "enabled.  Please assess your configuration and choose an\n"
             "explicit 'smt=<bool>' setting.  See XSA-439.\n");
 }
+#endif /* CONFIG_AMD */
 
 static void __init ibpb_calculations(void)
 {
@@ -1319,6 +1321,7 @@ static __init void l1tf_calculations(void)
                                             : (3UL << (paddr_bits - 2))));
 }
 
+#ifdef CONFIG_INTEL
 /* Calculate whether this CPU is vulnerable to MDS. */
 static __init void mds_calculations(void)
 {
@@ -1730,6 +1733,8 @@ static void __init bhi_calculations(void)
     }
 }
 
+#endif /* CONFIG_INTEL */
+
 void spec_ctrl_init_domain(struct domain *d)
 {
     bool pv = is_pv_domain(d);
@@ -2025,11 +2030,13 @@ void __init init_speculation_mitigations(void)
             default_scf |= SCF_ist_rsb;
     }
 
+#ifdef CONFIG_AMD
     srso_calculations(hw_smt_enabled);
 
-    ibpb_calculations();
-
     div_calculations(hw_smt_enabled);
+#endif
+
+    ibpb_calculations();
 
     /* Check whether Eager FPU should be enabled by default. */
     if ( opt_eager_fpu == -1 )
@@ -2136,9 +2143,10 @@ void __init init_speculation_mitigations(void)
      * - March 2023, for RFDS.  Enumerate RFDS_CLEAR to mean that VERW now
      *   scrubs non-architectural entries from certain register files.
      */
+#ifdef CONFIG_INTEL
     mds_calculations();
     rfds_calculations();
-
+#endif
     /*
      * Parts which enumerate FB_CLEAR are those with now-updated microcode
      * which weren't susceptible to the original MFBDS (and therefore didn't
@@ -2255,7 +2263,6 @@ void __init init_speculation_mitigations(void)
         opt_tsx = 0;
         tsx_init();
     }
-#endif
 
     /*
      * On some SRBDS-affected hardware, it may be safe to relax srb-lock by
@@ -2286,6 +2293,8 @@ void __init init_speculation_mitigations(void)
 
     bhi_calculations();
 
+#endif /* CONFIG_INTEL */
+
     print_details(thunk);
 
     /*
Signature · 1 line
2.25.1
#11 lore raw

PATCH v2 4/5 x86/intel: optional build of intel.c

In reply to a message not in the archive

With specific config option INTEL in place and most of the code that depends on intel.c now can be optionally enabled/disabled it's now possible to put the whole intel.c under INTEL option as well. This will allow for a Xen build without Intel CPU support.

CC Alejandro Vallejo <[email protected]>
CC Jan Beulich <[email protected]>

---
changes in v2:
- drop set_in_mcu_opt_ctrl() stub

---
 xen/arch/x86/cpu/Makefile            | 4 ++--
 xen/arch/x86/cpu/common.c            | 2 ++
 xen/arch/x86/include/asm/processor.h | 3 ++-
 3 files changed, 6 insertions(+), 3 deletions(-)
xen/arch/x86/cpu/Makefile +2 -2
index 80739d0256..eeb9ebe562 100644
@@ -6,8 +6,8 @@ obj-y += amd.o
 obj-$(CONFIG_CENTAUR) += centaur.o
 obj-y += common.o
 obj-$(CONFIG_HYGON) += hygon.o
-obj-y += intel.o
-obj-y += intel_cacheinfo.o
+obj-$(CONFIG_INTEL) += intel.o
+obj-$(CONFIG_INTEL) += intel_cacheinfo.o
 obj-y += mwait-idle.o
 obj-$(CONFIG_SHANGHAI) += shanghai.o
 obj-y += vpmu.o
xen/arch/x86/cpu/common.c +2 -0
index dcc2753212..580b01d6d5 100644
@@ -336,8 +336,10 @@ void __init early_cpu_init(bool verbose)
 
 	c->x86_vendor = x86_cpuid_lookup_vendor(ebx, ecx, edx);
 	switch (c->x86_vendor) {
+#ifdef CONFIG_INTEL
 	case X86_VENDOR_INTEL:    intel_unlock_cpuid_leaves(c);
 				  actual_cpu = intel_cpu_dev;    break;
+#endif
 	case X86_VENDOR_AMD:      actual_cpu = amd_cpu_dev;      break;
 #ifdef CONFIG_CENTAUR
 	case X86_VENDOR_CENTAUR:  actual_cpu = centaur_cpu_dev;  break;
xen/arch/x86/include/asm/processor.h +2 -1
index 66463f6a6d..a52f8b0a83 100644
@@ -507,13 +507,14 @@ static inline uint8_t get_cpu_family(uint32_t raw, uint8_t *model,
 extern int8_t opt_tsx;
 extern bool rtm_disabled;
 void tsx_init(void);
+void update_mcu_opt_ctrl(void);
 #else
 #define opt_tsx      0     /* explicitly indicate TSX is off */
 #define rtm_disabled false /* RTM was not force-disabled */
 static inline void tsx_init(void) {}
+static inline void update_mcu_opt_ctrl(void) {}
 #endif
 
-void update_mcu_opt_ctrl(void);
 void set_in_mcu_opt_ctrl(uint32_t mask, uint32_t val);
 
 enum ap_boot_method {
Signature · 1 line
2.25.1
#13 lore raw

PATCH v2 5/5 x86/amd: optional build of amd.c

In reply to a message not in the archive

Similar to making Intel CPU support optional -- as we've got CONFIG_AMD option now, we can put arch/x86/cpu/amd.c under it and make it possible to build Xen without AMD CPU support. One possible use case is to dispose of dead code in Intel-only systems.

---
changes in v2:
- drop routines-stubs in amd.h, handle call sites instead
- cpu_has_amd_erratum() return int instead of bool

---
 xen/arch/x86/cpu/Makefile      |  2 +-
 xen/arch/x86/cpu/common.c      |  4 +++-
 xen/arch/x86/hvm/svm/svm.c     |  6 ++++--
 xen/arch/x86/include/asm/amd.h | 20 ++++++++++++++++++--
 xen/arch/x86/spec_ctrl.c       |  2 ++
 5 files changed, 28 insertions(+), 6 deletions(-)
xen/arch/x86/cpu/Makefile +1 -1
index eeb9ebe562..2c34597136 100644
@@ -2,7 +2,7 @@ obj-y += mcheck/
 obj-y += microcode/
 obj-y += mtrr/
 
-obj-y += amd.o
+obj-$(CONFIG_AMD) += amd.o
 obj-$(CONFIG_CENTAUR) += centaur.o
 obj-y += common.o
 obj-$(CONFIG_HYGON) += hygon.o
xen/arch/x86/cpu/common.c +3 -1
index 580b01d6d5..5930b712bf 100644
@@ -194,7 +194,7 @@ void ctxt_switch_levelling(const struct vcpu *next)
 
 		if (cpu_has_cpuid_faulting)
 			set_cpuid_faulting(enable_cpuid_faulting);
-		else
+		else if ( IS_ENABLED(CONFIG_AMD) )
 			amd_set_cpuid_user_dis(enable_cpuid_faulting);
 
 		return;
@@ -340,7 +340,9 @@ void __init early_cpu_init(bool verbose)
 	case X86_VENDOR_INTEL:    intel_unlock_cpuid_leaves(c);
 				  actual_cpu = intel_cpu_dev;    break;
 #endif
+#ifdef CONFIG_AMD
 	case X86_VENDOR_AMD:      actual_cpu = amd_cpu_dev;      break;
+#endif
 #ifdef CONFIG_CENTAUR
 	case X86_VENDOR_CENTAUR:  actual_cpu = centaur_cpu_dev;  break;
 #endif
xen/arch/x86/hvm/svm/svm.c +4 -2
index 92bb10c504..88902e2d3a 100644
@@ -919,7 +919,8 @@ static void cf_check svm_ctxt_switch_from(struct vcpu *v)
      * Possibly clear previous guest selection of SSBD if set.  Note that
      * SPEC_CTRL.SSBD is already handled by svm_vmexit_spec_ctrl.
      */
-    if ( v->arch.msrs->virt_spec_ctrl.raw & SPEC_CTRL_SSBD )
+    if ( IS_ENABLED(CONFIG_AMD) &&
+         v->arch.msrs->virt_spec_ctrl.raw & SPEC_CTRL_SSBD )
     {
         ASSERT(v->domain->arch.cpuid->extd.virt_ssbd);
         amd_set_legacy_ssbd(false);
@@ -953,7 +954,8 @@ static void cf_check svm_ctxt_switch_to(struct vcpu *v)
         wrmsr_tsc_aux(v->arch.msrs->tsc_aux);
 
     /* Load SSBD if set by the guest. */
-    if ( v->arch.msrs->virt_spec_ctrl.raw & SPEC_CTRL_SSBD )
+    if ( IS_ENABLED(CONFIG_AMD) &&
+         v->arch.msrs->virt_spec_ctrl.raw & SPEC_CTRL_SSBD )
     {
         ASSERT(v->domain->arch.cpuid->extd.virt_ssbd);
         amd_set_legacy_ssbd(true);
xen/arch/x86/include/asm/amd.h +18 -2
index fa4e0fc766..da35b82d5a 100644
@@ -158,20 +158,36 @@
 #define is_zen4_uarch()   boot_cpu_has(X86_FEATURE_AUTO_IBRS)
 
 struct cpuinfo_x86;
+#ifdef CONFIG_AMD
 int cpu_has_amd_erratum(const struct cpuinfo_x86 *cpu, int osvw_id, ...);
+#else
+static inline int cpu_has_amd_erratum(const struct cpuinfo_x86 *cpu,
+                                      int osvw_id, ...)
+{
+    return 0;
+}
+#endif
 
 extern s8 opt_allow_unsafe;
 
 void fam10h_check_enable_mmcfg(void);
 void check_enable_amd_mmconf_dmi(void);
 
-extern bool amd_acpi_c1e_quirk;
 void amd_check_disable_c1e(unsigned int port, u8 value);
 
 extern bool amd_legacy_ssbd;
-extern bool amd_virt_spec_ctrl;
 bool amd_setup_legacy_ssbd(void);
 void amd_set_legacy_ssbd(bool enable);
 void amd_set_cpuid_user_dis(bool enable);
 
+#ifdef CONFIG_AMD
+extern bool amd_acpi_c1e_quirk;
+extern bool amd_virt_spec_ctrl;
+#else
+
+#define amd_acpi_c1e_quirk (false)
+#define amd_virt_spec_ctrl (false)
+
+#endif
+
 #endif /* __AMD_H__ */
xen/arch/x86/spec_ctrl.c +2 -0
index ba6c3e80d2..1964a417de 100644
@@ -1893,10 +1893,12 @@ void __init init_speculation_mitigations(void)
             setup_force_cpu_cap(X86_FEATURE_SC_MSR_HVM);
     }
 
+#ifdef CONFIG_AMD
     /* Support VIRT_SPEC_CTRL.SSBD if AMD_SSBD is not available. */
     if ( opt_msr_sc_hvm && !cpu_has_amd_ssbd &&
          (cpu_has_virt_ssbd || (amd_legacy_ssbd && amd_setup_legacy_ssbd())) )
         amd_virt_spec_ctrl = true;
+#endif
 
     /* Figure out default_xen_spec_ctrl. */
     if ( has_spec_ctrl && ibrs )
Signature · 1 line
2.25.1
Reviewed-by on v2 1/5
#2 lore raw
In reply to Sergiy Kibrik
Quoted 91 lines · Sergiy Kibrik

These options aim to represent what's currently supported by Xen, and later to allow tuning for specific platform(s) only.

HYGON and SHANGHAI options depend on AMD and INTEL as there're build dependencies on support code for AMD and Intel CPUs respectively.

CC Alejandro Vallejo <[email protected]>
CC Jan Beulich <[email protected]>
---
 xen/arch/x86/Kconfig.cpu  | 29 +++++++++++++++++++++++++++++
 xen/arch/x86/cpu/Makefile |  6 +++---
 xen/arch/x86/cpu/common.c |  6 ++++++
 3 files changed, 38 insertions(+), 3 deletions(-)
xen/arch/x86/Kconfig.cpu +29 -0
index 5fb18db1aa..ac8f41d464 100644
@@ -10,6 +10,25 @@ config AMD
 	  May be turned off in builds targetting other vendors.  Otherwise,
 	  must be enabled for Xen to work suitably on AMD platforms.
 
+config CENTAUR
+	bool "Support Centaur CPUs"
+	default y
+	help
+	  Detection, tunings and quirks for VIA platforms.
+
+	  May be turned off in builds targeting other vendors. Otherwise, must
+          be enabled for Xen to work suitably on VIA platforms.
+
+config HYGON
+	bool "Support Hygon CPUs"
+	depends on AMD
+	default y
+	help
+	  Detection, tunings and quirks for Hygon platforms.
+
+	  May be turned off in builds targeting other vendors. Otherwise, must
+          be enabled for Xen to work suitably on Hygon platforms.
+
 config INTEL
 	bool "Support Intel CPUs"
 	default y
@@ -19,4 +38,14 @@ config INTEL
 	  May be turned off in builds targetting other vendors.  Otherwise,
 	  must be enabled for Xen to work suitably on Intel platforms.
 
+config SHANGHAI
+	bool "Support Shanghai CPUs"
+	depends on INTEL
+	default y
+	help
+	  Detection, tunings and quirks for Zhaoxin platforms.
+
+	  May be turned off in builds targeting other vendors. Otherwise, must
+          be enabled for Xen to work suitably on Zhaoxin platforms.
+
 endmenu
xen/arch/x86/cpu/Makefile +3 -3
index eafce5f204..80739d0256 100644
@@ -3,13 +3,13 @@ obj-y += microcode/
 obj-y += mtrr/
 
 obj-y += amd.o
-obj-y += centaur.o
+obj-$(CONFIG_CENTAUR) += centaur.o
 obj-y += common.o
-obj-y += hygon.o
+obj-$(CONFIG_HYGON) += hygon.o
 obj-y += intel.o
 obj-y += intel_cacheinfo.o
 obj-y += mwait-idle.o
-obj-y += shanghai.o
+obj-$(CONFIG_SHANGHAI) += shanghai.o
 obj-y += vpmu.o
 obj-$(CONFIG_AMD) += vpmu_amd.o
 obj-$(CONFIG_INTEL) += vpmu_intel.o
xen/arch/x86/cpu/common.c +1 -0
index ff4cd22897..dcc2753212 100644
@@ -339,9 +339,15 @@ void __init early_cpu_init(bool verbose)
 	case X86_VENDOR_INTEL:    intel_unlock_cpuid_leaves(c);
 				  actual_cpu = intel_cpu_dev;    break;
 	case X86_VENDOR_AMD:      actual_cpu = amd_cpu_dev;      break;
+#ifdef CONFIG_CENTAUR
 	case X86_VENDOR_CENTAUR:  actual_cpu = centaur_cpu_dev;  break;

xen/arch/x86/cpu/common.c void __init early_cpu_init(bool verbose) · line 353

Quoted 10 lines
xen/arch/x86/cpu/common.c +5 -0
+#endif
+#ifdef CONFIG_SHANGHAI
 	case X86_VENDOR_SHANGHAI: actual_cpu = shanghai_cpu_dev; break;
+#endif
+#ifdef CONFIG_HYGON
 	case X86_VENDOR_HYGON:    actual_cpu = hygon_cpu_dev;    break;
+#endif
 	default:
 		actual_cpu = default_cpu;
 		if (!verbose)

Cheers,
Alejandro

#3 lore raw
In reply to Sergiy Kibrik
Quoted 33 lines · Sergiy Kibrik
xen/arch/x86/Kconfig.cpu +20 -0
@@ -10,6 +10,25 @@ config AMD
 	  May be turned off in builds targetting other vendors.  Otherwise,
 	  must be enabled for Xen to work suitably on AMD platforms.
 
+config CENTAUR
+	bool "Support Centaur CPUs"
+	default y
+	help
+	  Detection, tunings and quirks for VIA platforms.
+
+	  May be turned off in builds targeting other vendors. Otherwise, must
+          be enabled for Xen to work suitably on VIA platforms.
+
+config HYGON
+	bool "Support Hygon CPUs"
+	depends on AMD
+	default y
+	help
+	  Detection, tunings and quirks for Hygon platforms.
+
+	  May be turned off in builds targeting other vendors. Otherwise, must
+          be enabled for Xen to work suitably on Hygon platforms.
+
 config INTEL
 	bool "Support Intel CPUs"
 	default y
@@ -19,4 +38,14 @@ config INTEL
 	  May be turned off in builds targetting other vendors.  Otherwise,
 	  must be enabled for Xen to work suitably on Intel platforms.
 
+config SHANGHAI

xen/arch/x86/Kconfig.cpu config INTEL · line 51

Quoted 10 lines
xen/arch/x86/Kconfig.cpu +9 -0
+	bool "Support Shanghai CPUs"
+	depends on INTEL
+	default y
+	help
+	  Detection, tunings and quirks for Zhaoxin platforms.
+
+	  May be turned off in builds targeting other vendors. Otherwise, must
+          be enabled for Xen to work suitably on Zhaoxin platforms.
+
 endmenu

Imo this re-raises the question of whether it is a good idea to leave out "CPU" from the names: The more names there are, the more likely it'll become that going forward we'll run into a naming collision. Andrew, iirc you were the main proponent for omitting "CPU" - may I ask that you re-consider?

Furthermore I wonder whether "depends on" is appropriate here. This way one won't be offered e.g. SHANGHAI if earlier on one de-selected INTEL.

Plus it is mere luck that the alphabetic ordering ends up with the dependents after their dependencies (things would be somewhat odd the other way around).

Finally please check indentation of help text - there is one inconsistency repeated for all three entries being added.

Jan

#6 lore raw

Re: [XEN PATCH v2 2/5] x86/amd: configurable handling of AMD-specific MSRs access

In reply to Sergiy Kibrik
Quoted 1 line · Sergiy Kibrik

Do not compile handlers of guest access to AMD-specific MSRs when CONFIG_AMD=n.

What I'm missing in the description is clarification on how boundaries were drawn. In guest_rdmsr() there is, for example, also handling of MSR_AMD_PATCHLEVEL. Which I'm okay to leave aside for now, as long as it's clear why that is.

Quoted 27 lines
xen/arch/x86/msr.c +3 -0
@@ -219,6 +219,7 @@ int guest_rdmsr(struct vcpu *v, uint32_t msr, uint64_t *val)
         *val = msrs->tsc_aux;
         break;
 
+#ifdef CONFIG_AMD
     case MSR_K8_SYSCFG:
     case MSR_K8_TOP_MEM1:
     case MSR_K8_TOP_MEM2:
@@ -281,6 +282,7 @@ int guest_rdmsr(struct vcpu *v, uint32_t msr, uint64_t *val)
                                    ? 0 : (msr - MSR_AMD64_DR1_ADDRESS_MASK + 1),
                                    ARRAY_SIZE(msrs->dr_mask))];
         break;
+#endif /* CONFIG_AMD */
 
         /*
          * TODO: Implement when we have better topology representation.
@@ -552,6 +554,7 @@ int guest_wrmsr(struct vcpu *v, uint32_t msr, uint64_t val)
             wrmsr_tsc_aux(val);
         break;
 
+#ifdef CONFIG_AMD
     case MSR_VIRT_SPEC_CTRL:
         if ( !cp->extd.virt_ssbd )
             goto gp_fault;

xen/arch/x86/msr.c int guest_wrmsr(struct vcpu *v, uint32_t msr, uint64_t val) · line 607

Quoted 7 lines
xen/arch/x86/msr.c +1 -0
@@ -598,6 +601,7 @@ int guest_wrmsr(struct vcpu *v, uint32_t msr, uint64_t val)
         if ( v == curr && (curr->arch.dr7 & DR7_ACTIVE_MASK) )
             wrmsrl(msr, val);
         break;
+#endif /* CONFIG_AMD */
 
     default:
         return X86EMUL_UNHANDLEABLE;

Is just adding #ifdef-s actually correct? That results in different behavior on e.g. Intel hardware, I think, depending on whether AMD=y or AMD=n. In the latter case the function will now return X86EMUL_UNHANDLEABLE, while in the former case it would return X86EMUL_EXCEPTION.

Jan

#8 lore raw

Re: [XEN PATCH v2 3/5] x86/spec-ctrl: configurable Intlel/AMD-specific calculations

In reply to Sergiy Kibrik
Quoted 10 lines · Sergiy Kibrik

Put platforms-specific code under #ifdef CONFIG_{AMD,INTEL} so that when corresponding CPU support is disabled by configuration less dead code will end up in the build.

This includes re-ordering of calls to ibpb_calculations() & div_calculations(), but since they don't access common variables or feature bits it should be safe to do.

For one please consider adding Requested-by: or Suggested-by: tags.

Quoted 11 lines
xen/arch/x86/spec_ctrl.c +1 -0
@@ -1012,6 +1012,7 @@ static bool __init should_use_eager_fpu(void)
     }
 }
 
+#ifdef CONFIG_AMD
 /*
  * https://www.amd.com/content/dam/amd/en/documents/corporate/cr/speculative-return-stack-overflow-whitepaper.pdf
  */

xen/arch/x86/spec_ctrl.c static void __init div_calculations(bool hw_smt_enabled) · line 1114

Quoted 4 lines
xen/arch/x86/spec_ctrl.c +1 -0
@@ -1110,6 +1111,7 @@ static void __init div_calculations(bool hw_smt_enabled)
             "enabled.  Please assess your configuration and choose an\n"
             "explicit 'smt=<bool>' setting.  See XSA-439.\n");
 }
+#endif /* CONFIG_AMD */

And then no, I don't think we want to use #ifdef-ary here. IS_ENABLED() inside the functions (where the vendor checks are) is not only making sure the compiler will still parse all the code even when either vendor's support was turned off, but will also help review (by having in context what the actual vendor checks are in each function).

Jan

Acked-by on v2 4/5
#12 lore raw

Re: [XEN PATCH v2 4/5] x86/intel: optional build of intel.c

In reply to Sergiy Kibrik
Quoted 6 lines · Sergiy Kibrik

With specific config option INTEL in place and most of the code that depends on intel.c now can be optionally enabled/disabled it's now possible to put the whole intel.c under INTEL option as well. This will allow for a Xen build without Intel CPU support.

#14 lore raw

Re: [XEN PATCH v2 5/5] x86/amd: optional build of amd.c

In reply to Sergiy Kibrik
Quoted 13 lines · Sergiy Kibrik
xen/arch/x86/hvm/svm/svm.c +2 -1
@@ -919,7 +919,8 @@ static void cf_check svm_ctxt_switch_from(struct vcpu *v)
      * Possibly clear previous guest selection of SSBD if set.  Note that
      * SPEC_CTRL.SSBD is already handled by svm_vmexit_spec_ctrl.
      */
-    if ( v->arch.msrs->virt_spec_ctrl.raw & SPEC_CTRL_SSBD )
+    if ( IS_ENABLED(CONFIG_AMD) &&
+         v->arch.msrs->virt_spec_ctrl.raw & SPEC_CTRL_SSBD )
     {
         ASSERT(v->domain->arch.cpuid->extd.virt_ssbd);
         amd_set_legacy_ssbd(false);

xen/arch/x86/hvm/svm/svm.c static void cf_check svm_ctxt_switch_to(struct vcpu *v) · line 961

Quoted 9 lines
xen/arch/x86/hvm/svm/svm.c +2 -1
@@ -953,7 +954,8 @@ static void cf_check svm_ctxt_switch_to(struct vcpu *v)
         wrmsr_tsc_aux(v->arch.msrs->tsc_aux);
 
     /* Load SSBD if set by the guest. */
-    if ( v->arch.msrs->virt_spec_ctrl.raw & SPEC_CTRL_SSBD )
+    if ( IS_ENABLED(CONFIG_AMD) &&
+         v->arch.msrs->virt_spec_ctrl.raw & SPEC_CTRL_SSBD )
     {
         ASSERT(v->domain->arch.cpuid->extd.virt_ssbd);
         amd_set_legacy_ssbd(true);

Instead of these changes, shouldn't AMD_SVM become dependent upon AMD in Kconfig?

xen/arch/x86/hvm/svm/svm.c

Quoted 7 lines
xen/arch/x86/hvm/svm/svm.c +7 -0
+#ifdef CONFIG_AMD
+extern bool amd_acpi_c1e_quirk;
+extern bool amd_virt_spec_ctrl;
+#else
+
+#define amd_acpi_c1e_quirk (false)
+#define amd_virt_spec_ctrl (false)

As a remark, while there's nothing wrong with parenthesizing "false" here, it also isn't really necessary. Omitting unnecessary parentheses generally aids readability imo.

Jan

#4 lore raw
In reply to Jan Beulich

19.08.24 11:53, Jan Beulich:

Quoted 49 lines · Sergiy Kibrik
Quoted 33 lines
xen/arch/x86/Kconfig.cpu +20 -0
@@ -10,6 +10,25 @@ config AMD
  	  May be turned off in builds targetting other vendors.  Otherwise,
  	  must be enabled for Xen to work suitably on AMD platforms.
 
+config CENTAUR
+	bool "Support Centaur CPUs"
+	default y
+	help
+	  Detection, tunings and quirks for VIA platforms.
+
+	  May be turned off in builds targeting other vendors. Otherwise, must
+          be enabled for Xen to work suitably on VIA platforms.
+
+config HYGON
+	bool "Support Hygon CPUs"
+	depends on AMD
+	default y
+	help
+	  Detection, tunings and quirks for Hygon platforms.
+
+	  May be turned off in builds targeting other vendors. Otherwise, must
+          be enabled for Xen to work suitably on Hygon platforms.
+
  config INTEL
  	bool "Support Intel CPUs"
  	default y
@@ -19,4 +38,14 @@ config INTEL
  	  May be turned off in builds targetting other vendors.  Otherwise,
  	  must be enabled for Xen to work suitably on Intel platforms.
 
+config SHANGHAI

xen/arch/x86/Kconfig.cpu config INTEL · line 51

Quoted 10 lines
xen/arch/x86/Kconfig.cpu +9 -0
+	bool "Support Shanghai CPUs"
+	depends on INTEL
+	default y
+	help
+	  Detection, tunings and quirks for Zhaoxin platforms.
+
+	  May be turned off in builds targeting other vendors. Otherwise, must
+          be enabled for Xen to work suitably on Zhaoxin platforms.
+
  endmenu

Imo this re-raises the question of whether it is a good idea to leave out "CPU" from the names: The more names there are, the more likely it'll become that going forward we'll run into a naming collision. Andrew, iirc you were the main proponent for omitting "CPU" - may I ask that you re-consider?

Quoted 2 lines

Furthermore I wonder whether "depends on" is appropriate here. This way one won't be offered e.g. SHANGHAI if earlier on one de-selected INTEL.

I see, then probably I'll use 'select INTEL' here. And also 'select AMD' for HYGON.

-Sergiy

#15 lore raw

Re: [XEN PATCH v2 5/5] x86/amd: optional build of amd.c

In reply to Jan Beulich

19.08.24 15:36, Jan Beulich:

Quoted 16 lines · Sergiy Kibrik
Quoted 15 lines
xen/arch/x86/hvm/svm/svm.c +2 -1
@@ -919,7 +919,8 @@ static void cf_check svm_ctxt_switch_from(struct vcpu *v)
       * Possibly clear previous guest selection of SSBD if set.  Note that
       * SPEC_CTRL.SSBD is already handled by svm_vmexit_spec_ctrl.
       */
-    if ( v->arch.msrs->virt_spec_ctrl.raw & SPEC_CTRL_SSBD )
+    if ( IS_ENABLED(CONFIG_AMD) &&
+         v->arch.msrs->virt_spec_ctrl.raw & SPEC_CTRL_SSBD )
      {
          ASSERT(v->domain->arch.cpuid->extd.virt_ssbd);
          amd_set_legacy_ssbd(false);
@@ -953,7 +954,8 @@ static void cf_check svm_ctxt_switch_to(struct vcpu *v)
          wrmsr_tsc_aux(v->arch.msrs->tsc_aux);
 
Quoted 9 lines

xen/arch/x86/hvm/svm/svm.c static void cf_check svm_ctxt_switch_to(struct vcpu *v) · line 961

Quoted 7 lines
xen/arch/x86/hvm/svm/svm.c +2 -1
      /* Load SSBD if set by the guest. */
-    if ( v->arch.msrs->virt_spec_ctrl.raw & SPEC_CTRL_SSBD )
+    if ( IS_ENABLED(CONFIG_AMD) &&
+         v->arch.msrs->virt_spec_ctrl.raw & SPEC_CTRL_SSBD )
      {
          ASSERT(v->domain->arch.cpuid->extd.virt_ssbd);
          amd_set_legacy_ssbd(true);

Instead of these changes, shouldn't AMD_SVM become dependent upon AMD in Kconfig?

It could be done earlier, yet I haven't done so since we briefly touched this before and decided not to link {AMD,INTEL} with {AMD_SVM,INTEL_VMX} then:

https://lore.kernel.org/xen-devel/[email protected]/

-Sergiy

#16 lore raw

Re: [XEN PATCH v2 5/5] x86/amd: optional build of amd.c

Quoted 27 lines · Sergiy Kibrik

19.08.24 15:36, Jan Beulich:

Quoted 16 lines · Sergiy Kibrik
Quoted 15 lines
xen/arch/x86/hvm/svm/svm.c +2 -1
@@ -919,7 +919,8 @@ static void cf_check svm_ctxt_switch_from(struct vcpu *v)
       * Possibly clear previous guest selection of SSBD if set.  Note that
       * SPEC_CTRL.SSBD is already handled by svm_vmexit_spec_ctrl.
       */
-    if ( v->arch.msrs->virt_spec_ctrl.raw & SPEC_CTRL_SSBD )
+    if ( IS_ENABLED(CONFIG_AMD) &&
+         v->arch.msrs->virt_spec_ctrl.raw & SPEC_CTRL_SSBD )
      {
          ASSERT(v->domain->arch.cpuid->extd.virt_ssbd);
          amd_set_legacy_ssbd(false);
@@ -953,7 +954,8 @@ static void cf_check svm_ctxt_switch_to(struct vcpu *v)
          wrmsr_tsc_aux(v->arch.msrs->tsc_aux);
 
Quoted 9 lines

xen/arch/x86/hvm/svm/svm.c static void cf_check svm_ctxt_switch_to(struct vcpu *v) · line 961

Quoted 7 lines
xen/arch/x86/hvm/svm/svm.c +2 -1
      /* Load SSBD if set by the guest. */
-    if ( v->arch.msrs->virt_spec_ctrl.raw & SPEC_CTRL_SSBD )
+    if ( IS_ENABLED(CONFIG_AMD) &&
+         v->arch.msrs->virt_spec_ctrl.raw & SPEC_CTRL_SSBD )
      {
          ASSERT(v->domain->arch.cpuid->extd.virt_ssbd);
          amd_set_legacy_ssbd(true);

Instead of these changes, shouldn't AMD_SVM become dependent upon AMD in Kconfig?

Quoted 5 lines

It could be done earlier, yet I haven't done so since we briefly touched this before and decided not to link {AMD,INTEL} with {AMD_SVM,INTEL_VMX} then:

https://lore.kernel.org/xen-devel/[email protected]/

Yet that only suggests that e.g HYGON also ought to select AMD_SVM. Which will happen transitively with HYGON selecting AMD (in the earlier patch).

Jan

#9 lore raw

Re: [XEN PATCH v2 3/5] x86/spec-ctrl: configurable Intlel/AMD-specific calculations

In reply to Sergiy Kibrik
Quoted 10 lines · Sergiy Kibrik

Put platforms-specific code under #ifdef CONFIG_{AMD,INTEL} so that when corresponding CPU support is disabled by configuration less dead code will end up in the build.

This includes re-ordering of calls to ibpb_calculations() & div_calculations(), but since they don't access common variables or feature bits it should be safe to do.

Sorry, but no.

This logic is security critical, highly fragile, gets chopped/changed multiple times a year (as researchers keep on finding new things), and all major work is done to it under embargo.

Just look at the history of the file.

The ifdefary around the tsx_init() call is bad enough and I intend to revert it and do that differently.  I would have objected if I'd got to the patch in time.

The only relevant cost in this file is whether I (and the other security team members) can edit it correctly or not in staging and all prior in-support branches.  You really don't want to know how many times there's been a bug in backports...

Saving 451 lines from certification is not cheaper than the problems/risks you're introducing with this change.

~Andrew

#10 lore raw

Re: [XEN PATCH v2 3/5] x86/spec-ctrl: configurable Intlel/AMD-specific calculations

Quoted 25 lines · Andrew Cooper
Quoted 10 lines · Sergiy Kibrik

Put platforms-specific code under #ifdef CONFIG_{AMD,INTEL} so that when corresponding CPU support is disabled by configuration less dead code will end up in the build.

This includes re-ordering of calls to ibpb_calculations() & div_calculations(), but since they don't access common variables or feature bits it should be safe to do.

Sorry, but no.

This logic is security critical, highly fragile, gets chopped/changed multiple times a year (as researchers keep on finding new things), and all major work is done to it under embargo.

Just look at the history of the file.

The ifdefary around the tsx_init() call is bad enough and I intend to revert it and do that differently.  I would have objected if I'd got to the patch in time.

Quoted 7 lines

The only relevant cost in this file is whether I (and the other security team members) can edit it correctly or not in staging and all prior in-support branches.  You really don't want to know how many times there's been a bug in backports...

Saving 451 lines from certification is not cheaper than the problems/risks you're introducing with this change.

Did you see my earlier reply? I don't think the issue is with hiding source lines. We want to have the compiler DCE stuff wherever possible, hence why I did respond asking to switch to IS_ENABLED(). That imo fits pretty well with the vendor checks we have there already anyway.

Jan