x86/CPUID: leaf pruning
v5 cover x86/CPUID: leaf pruning
These three related patches were previously part of the AMX series. For AVX10 some of this is going to be needed (or at least wanted) too, though, hence I'm now refreshing these separately. Neither of the three is strictly a functional prereq, but two of the three introduce new functions which then are further altered while adding AVX10 (and AMX) logic. Presumably none of these would be necessary anymore once toolstack side work planned by Andrew gets carried out.
All taken together: Until it is clear that one or more of these are strictly not needed / wanted (in this or another form), having these ahead of the actual work seems preferable over accidentally losing adjustments done for AVX10 / AMX (and down the road perhaps others).
1: adjust extended leaves out of range clearing
2: shrink max_{,sub}leaf fields according to actual leaf contents 3: move bounding of max_{,sub}leaf fields to library code
Jan
PATCH v5 1/3 x86/CPUID: adjust extended leaves out of range clearing
A maximum extended leaf input value with the high half different from 0x8000 should not be considered valid - all leaves should be cleared in this case.
---
TBD: Andrew suggested to drop this patch, but that sub-thread still has
a loose end. Hence, until I'm convinced otherwise, I've retained this patch here. I don't think it conflicts with any of the subsequent ones.
---
v2: Integrate into series.
tools/tests/cpu-policy/test-cpu-policy.c +12 -1
@@ -528,11 +528,22 @@ static void test_cpuid_out_of_range_clea }, }, { + .name = "no extd", + .nr_markers = 0, + .p = { +/* Clears all markers. */ + .extd.max_leaf = 0, + + .extd.vendor_ebx = 0xc2, + .extd.raw_fms = 0xc2, + }, + }, + { .name = "extd", .nr_markers = 1, .p = {/* Retains marker in leaf 0. Clears others. */ - .extd.max_leaf = 0, + .extd.max_leaf = 0x80000000, .extd.vendor_ebx = 0xc2, .extd.raw_fms = 0xc2,
xen/lib/x86/cpuid.c +3 -1
@@ -285,7 +285,9 @@ void x86_cpu_policy_clear_out_of_range_l ARRAY_SIZE(p->xstate.raw) - 1); } -zero_leaves (p->extd.raw, (p->extd.max_leaf &0xffff ) +1 , +zero_leaves (p->extd.raw, + ((p->extd.max_leaf >>16 ) ==0x8000 + ? (p->extd.max_leaf &0xffff ) +1 :0 ),ARRAY_SIZE (p->extd.raw) -1 ); }
PATCH v5 2/3 x86/CPUID: shrink max_{,sub}leaf fields according to actual leaf contents
In reply to Jan BeulichZapping leaf data for out of range leaves is just one half of it: To avoid guests (bogusly or worse) inferring information from mere leaf presence, also shrink maximum indicators such that the respective trailing entry is not all blank (unless of course it's the initial subleaf of a leaf that's not the final one).
This is also in preparation of bumping the maximum basic leaf we support, to ensure guests not getting exposed related features won't observe a change in behavior.
---
TBD: The use in xc_cpuid_apply_policy() supposedly needs to further
respect user (or migration source) max (sub)leaf values (i.e. the function needs calling unconditionally and then higher inputs need putting back); question is where those original values would be recorded / come from. (Supposedly there was a patch from Roger under similar or even the same title, which should have eliminated the need for the patch here, but upon searching I can't find such a patch.)
---
v5: Re-base.
v4: Re-instate with default policy shrinking dropped, and with
81da2b544cbb folded in. Re-base.
v3: Record the actual non-empty subleaf in p->basic.raw[0x7], rather
than subleaf 0. Re-base over Viridian leaf 40000005 addition.
v2: New.
tools/libs/guest/xg_cpuid_x86.c +7 -0
@@ -783,6 +783,13 @@ int xc_cpuid_apply_policy(xc_interface * } } +/* +* Do not try to shrink the policy if restoring, as that could cause +* guest visible changes in the maximum leaf fields. +*/ +if ( !restore ) +x86_cpu_policy_shrink_max_leaves (&p->policy); + nr_leaves =ARRAY_SIZE (p->leaves); rc =x86_cpuid_copy_to_buffer (&p->policy, p->leaves, &nr_leaves);if ( rc )
tools/tests/cpu-policy/test-cpu-policy.c +101 -0
123 lines not on this page · raw message
xen/arch/x86/cpu-policy.c +18 -12
@@ -383,6 +383,8 @@ static void __init calculate_host_policyif ( vpmu_mode ==XENPMU_MODE_OFF ) p->basic.raw[0xa ] =EMPTY_LEAF ; +x86_cpu_policy_shrink_max_leaves (p); +/* 0x000000ce MSR_INTEL_PLATFORM_INFO */ /* probe_cpuid_faulting() sanity checks presence of MISC_FEATURES_ENABLES */ p->platform_info.cpuid_faulting = cpu_has_cpuid_faulting;
@@ -576,12 +578,6 @@ static void __init calculate_pv_max_poli *p = host_cpu_policy; -/* -* Some VMs may have a larger-than-necessary feat max_subleaf. Allow them -* to migrate in. -*/ - p->feat.max_subleaf =ARRAY_SIZE (p->feat.raw) -1 ; -x86_cpu_policy_to_featureset (p, fs);for ( i =0 ; i <ARRAY_SIZE (fs); ++i )
@@ -613,6 +609,14 @@ static void __init calculate_pv_max_polirecalculate_xstate (p); p->extd.raw[0xa ] =EMPTY_LEAF ;/* No SVM for PV guests. */ + +x86_cpu_policy_shrink_max_leaves (p); + +/* +* Some VMs may have a larger-than-necessary feat max_subleaf. Allow them +* to migrate in. +*/ + p->feat.max_subleaf =ARRAY_SIZE (p->feat.raw) -1 ; }static void __init calculate_pv_def_policy (void )
@@ -663,12 +667,6 @@ static void __init calculate_hvm_max_pol *p = host_cpu_policy; -/* -* Some VMs may have a larger-than-necessary feat max_subleaf. Allow them -* to migrate in. -*/ - p->feat.max_subleaf =ARRAY_SIZE (p->feat.raw) -1 ; -x86_cpu_policy_to_featureset (p, fs); mask = hvm_hap_supported() ?
@@ -774,6 +772,14 @@ static void __init calculate_hvm_max_polx86_cpu_featureset_to_policy (fs, p);recalculate_xstate (p); +x86_cpu_policy_shrink_max_leaves (p); + +/* +* Some VMs may have a larger-than-necessary feat max_subleaf. Allow them +* to migrate in. +*/ + p->feat.max_subleaf =ARRAY_SIZE (p->feat.raw) -1 ; +/* It's always possible to emulate CPUID faulting for HVM guests */ p->platform_info.cpuid_faulting = true; }
xen/arch/x86/traps.c +4 -2
@@ -1060,13 +1060,15 @@ void cpuid_hypervisor_leaves(const strucuint32_t base =is_viridian_domain (d) ?0x40000100 :0x40000000 ;uint32_t idx = leaf - base;unsigned int limit =is_viridian_domain (d) ? p->hv2_limit : p->hv_limit; +unsigned int dflt =is_pv_domain (d) ?XEN_CPUID_MAX_PV_NUM_LEAVES + :XEN_CPUID_MAX_HVM_NUM_LEAVES ;if ( limit ==0 )/* Default number of leaves */ - limit =XEN_CPUID_MAX_NUM_LEAVES ; + limit = dflt;else /* Clamp toolstack value between 2 and MAX_NUM_LEAVES. */ - limit =min (max (limit,2u ),XEN_CPUID_MAX_NUM_LEAVES +0u ); + limit =min (max (limit,2u ), dflt);if ( idx > limit )return ;
xen/include/public/arch-x86/cpuid.h +5 -1
@@ -117,6 +117,10 @@/* Max. address width in bits taking memory hotplug into account. */ #define XEN_CPUID_MACHINE_ADDRESS_WIDTH_MASK (0xffu << 0) -#define XEN_CPUID_MAX_NUM_LEAVES 5 +#define XEN_CPUID_MAX_PV_NUM_LEAVES 5 +#define XEN_CPUID_MAX_HVM_NUM_LEAVES 4 +#define XEN_CPUID_MAX_NUM_LEAVES \ + (XEN_CPUID_MAX_PV_NUM_LEAVES > XEN_CPUID_MAX_HVM_NUM_LEAVES ? \ + XEN_CPUID_MAX_PV_NUM_LEAVES : XEN_CPUID_MAX_HVM_NUM_LEAVES)#endif /* __XEN_PUBLIC_ARCH_X86_CPUID_H__ */
xen/include/xen/lib/x86/cpu-policy.h +7 -0
@@ -438,6 +438,13 @@ void x86_cpu_policy_fill_native(struct c*/ void x86_cpu_policy_clear_out_of_range_leaves (struct cpu_policy *p); +/** +* Shrink max leaf/subleaf values such that the last respective valid entry +* isn't all blank. While permitted by the spec, such extraneous leaves may +* provide undue "hints" to guests. +*/ +void x86_cpu_policy_shrink_max_leaves (struct cpu_policy *p); +#ifdef __XEN__#include <public/xen.h> typedef XEN_GUEST_HANDLE_64 (xen_cpuid_leaf_t)cpuid_leaf_buffer_t ;
xen/lib/x86/cpuid.c +39 -0
@@ -291,6 +291,45 @@ void x86_cpu_policy_clear_out_of_range_l ARRAY_SIZE(p->extd.raw) - 1); } +void x86_cpu_policy_shrink_max_leaves (struct cpu_policy *p) +{ +unsigned int i; + + p->basic.raw[0x4 ] = p->cache.raw[0 ]; + +for ( i = p->feat.max_subleaf; i; --i ) +if ( p->feat.raw[i].a | p->feat.raw[i].b | + p->feat.raw[i].c | p->feat.raw[i].d ) +break ; + p->feat.max_subleaf = i; + p->basic.raw[0x7 ] = p->feat.raw[i]; + + p->basic.raw[0xb ] = p->topo.raw[0 ]; + +/* +* Due to the way xstate gets handled in the hypervisor (see +* recalculate_xstate()) there is (for now at least) no need to fiddle +* with the xstate subleaves (IOW we assume they're already in consistent +* shape, for coming from either hardware or recalculate_xstate()). +*/ + p->basic.raw[0xd ] = p->xstate.raw[0 ]; + +for ( i = p->basic.max_leaf; i; --i ) +if ( p->basic.raw[i].a | p->basic.raw[i].b | + p->basic.raw[i].c | p->basic.raw[i].d ) +break ; + p->basic.max_leaf = i; + +for ( i = p->extd.max_leaf &0xffff ; i; --i ) +if ( p->extd.raw[i].a | p->extd.raw[i].b | + p->extd.raw[i].c | p->extd.raw[i].d ) +break ; +if ( i | p->extd.raw[0 ].b | p->extd.raw[0 ].c | p->extd.raw[0 ].d ) + p->extd.max_leaf =0x80000000U | i; +else + p->extd.max_leaf =0 ; +} +const uint32_t *x86_cpu_policy_lookup_deep_deps (uint32_t feature) {static const uint32_t deep_features[] =INIT_DEEP_FEATURES ;
PATCH v5 3/3 x86/CPUID: move bounding of max_{,sub}leaf fields to library code
In reply to Jan BeulichBreak out this logic from calculate_host_policy() to also use it in the x86 emulator harness, where subsequently we'll want to avoid open-coding AMX maximum palette bounding as well as AVX10 sub-feature pruning.
---
v4: Mark x86_cpu_policy_bound_max_leaves() __init. Add U suffixes.
Re-base.
v2: New.
tools/tests/x86_emulator/x86-emulate.c +3 -0
@@ -76,6 +76,7 @@ bool emul_test_init(void)unsigned long sp;x86_cpu_policy_fill_native (&cp); +x86_cpu_policy_bound_max_leaves (&cp);/* * The emulator doesn't use these instructions, so can always emulate
@@ -88,6 +89,8 @@ bool emul_test_init(void) cp.feat.wrmsrns = true; cp.extd.clzero = true; +x86_cpu_policy_shrink_max_leaves (&cp); +if ( cpu_has_xsave ) {unsigned int tmp, ebx;
xen/arch/x86/cpu-policy.c +3 -12
@@ -352,28 +352,19 @@ void calculate_raw_cpu_policy(void)static void __init calculate_host_policy (void ) {struct cpu_policy *p = &host_cpu_policy; -unsigned int max_extd_leaf; *p = raw_cpu_policy; - p->basic.max_leaf = -min_t (uint32_t, p->basic.max_leaf,ARRAY_SIZE (p->basic.raw) -1 ); - p->feat.max_subleaf = -min_t (uint32_t, p->feat.max_subleaf,ARRAY_SIZE (p->feat.raw) -1 ); - - max_extd_leaf = p->extd.max_leaf; -/* * For AMD/Hygon hardware before Zen3, we unilaterally modify LFENCE to be * dispatch serialising for Spectre mitigations. Extend max_extd_leaf * beyond what hardware supports, to include the feature leaf containing * this information. */ -if ( cpu_has_lfence_dispatch ) - max_extd_leaf =max (max_extd_leaf,0x80000021U ); +if ( cpu_has_lfence_dispatch && p->extd.max_leaf <0x80000021U ) + p->extd.max_leaf =0x80000021U ; - p->extd.max_leaf =0x80000000U |min_t (uint32_t, max_extd_leaf &0xffff , -ARRAY_SIZE (p->extd.raw) -1 ); +x86_cpu_policy_bound_max_leaves (p);x86_cpu_featureset_to_policy (boot_cpu_data.x86_capability, p);recalculate_xstate (p);
xen/include/xen/lib/x86/cpu-policy.h +6 -0
@@ -439,6 +439,12 @@ void x86_cpu_policy_fill_native(struct cvoid x86_cpu_policy_clear_out_of_range_leaves (struct cpu_policy *p);/** +* Bound max leaf/subleaf values according to the capacity of the respective +* arrays in struct cpu_policy. +*/ +void x86_cpu_policy_bound_max_leaves (struct cpu_policy *p); + +/** * Shrink max leaf/subleaf values such that the last respective valid entry * isn't all blank. While permitted by the spec, such extraneous leaves may * provide undue "hints" to guests.
xen/lib/x86/cpuid.c +10 -0
@@ -291,6 +291,16 @@ void x86_cpu_policy_clear_out_of_range_l ARRAY_SIZE(p->extd.raw) - 1); } +void __init x86_cpu_policy_bound_max_leaves (struct cpu_policy *p) +{ + p->basic.max_leaf = +min_t (uint32_t, p->basic.max_leaf,ARRAY_SIZE (p->basic.raw) -1 ); + p->feat.max_subleaf = +min_t (uint32_t, p->feat.max_subleaf,ARRAY_SIZE (p->feat.raw) -1 ); + p->extd.max_leaf =0x80000000U |min_t (uint32_t, p->extd.max_leaf &0xffff , +ARRAY_SIZE (p->extd.raw) -1 ); +} +void x86_cpu_policy_shrink_max_leaves (struct cpu_policy *p) {unsigned int i;
xen/lib/x86/private.h +6 -0
@@ -5,6 +5,7 @@#include <xen/bitops.h> #include <xen/guest_access.h> +#include <xen/init.h> #include <xen/kernel.h> #include <xen/lib.h> #include <xen/nospec.h>
@@ -28,6 +29,11 @@#include <xen-tools/common-macros.h> +#define __init +#define __initdata +#define __initconst +#define __initconstrel +static inline bool test_bit (unsigned int bit,const void *vaddr) {const char *addr = vaddr;