libs/guest: new CPUID/MSR interface
v5 cover libs/guest: new CPUID/MSR interface
Hello,
The following series introduces a new CPUID/MSR interface for the xenguest library. Such interface handles both CPUID and MSRs using the same opaque object, and provides some helpers for the user to peek or modify such data without exposing the backing type. This is useful for future development as CPUID and MSRs are closely related, so it makes handling those much easier if they are inside the same object (ie: a change to a CPUID bit might expose or hide an MSR).
In this patch series libxl and other in tree users have been switched to use the new interface, so it shouldn't result in any functional change from a user point of view.
Note there are still some missing pieces likely. The way to modify CPUID data is not ideal, as it requires fetching a leaf and modifying it directly. We might want some kind of interface in order to set specific CPUID features more easily, but that's to be discussed, and would be done as a follow up series.
The addition of a helper to generate compatible policies given two inputs has been removed from this iteration, sine Andrew Cooper has posted a patch to set the foundation for that, and further work should be done against that baseline.
Thanks, Roger.
Jan Beulich (1):
x86/CPUID: shrink max_{,sub}leaf fields according to actual leaf
contents
Roger Pau Monne (11):
libs/guest: move cpu policy related prototypes to xenguest.h
libx86: introduce helper to fetch cpuid leaf libs/guest: allow fetching a specific CPUID leaf from a cpu policy
libx86: introduce helper to fetch msr entry
libs/guest: allow fetching a specific MSR entry from a cpu policy libs/guest: make a cpu policy compatible with older Xen versions libs/guest: introduce helper set cpu topology in cpu policy libs/guest: rework xc_cpuid_xend_policy
libs/guest: apply a featureset into a cpu policy libs/{light,guest}: implement xc_cpuid_apply_policy in libxl libs/guest: (re)move xc_cpu_policy_apply_cpuid
tools/include/libxl.h | 6 +- tools/include/xenctrl.h | 99 ---- tools/include/xenguest.h | 77 +++ tools/libs/ctrl/xc_bitops.h | 6 +- tools/libs/guest/xg_cpuid_x86.c | 647 ++++++++--------------- tools/libs/guest/xg_private.h | 1 + tools/libs/light/libxl_cpuid.c | 233 +++++++- tools/libs/light/libxl_internal.h | 26 + tools/misc/xen-cpuid.c | 1 + tools/tests/cpu-policy/test-cpu-policy.c | 224 +++++++- xen/arch/x86/cpuid.c | 55 +- xen/include/xen/lib/x86/cpuid.h | 26 + xen/include/xen/lib/x86/msr.h | 20 +- xen/lib/x86/cpuid.c | 91 ++++ xen/lib/x86/msr.c | 41 +- 15 files changed, 948 insertions(+), 605 deletions(-)
Signature · 1 line
2.33.0
PATCH v5 1/12 libs/guest: move cpu policy related prototypes to xenguest.h
Do this before adding any more stuff to xg_cpuid_x86.c.
The placement in xenctrl.h is wrong, as they are implemented by the xenguest library. Note that xg_cpuid_x86.c needs to include xg_private.h, and in turn also fix xg_private.h to include xc_bitops.h. The bitops definition of BITS_PER_LONG needs to be changed to not be an expression, so that xxhash.h can use it in a preprocessor if directive.
As a result also modify xen-cpuid and the ocaml stubs to include xenguest.h.
---
Changes since v1:
- Include xenguest.h in ocaml stubs.
- Change BITS_PER_LONG definition in xc_bitops.h.
--- tools/include/xenctrl.h | 55 -------------------------------- tools/include/xenguest.h | 56 +++++++++++++++++++++++++++++++++ tools/libs/ctrl/xc_bitops.h | 6 +++- tools/libs/guest/xg_cpuid_x86.c | 1 - tools/libs/guest/xg_private.h | 1 + tools/misc/xen-cpuid.c | 1 + 6 files changed, 63 insertions(+), 57 deletions(-)
tools/include/xenctrl.h +0 -55
@@ -2528,61 +2528,6 @@ int xc_psr_get_domain_data(xc_interface *xch, uint32_t domid, uint64_t *data);int xc_psr_get_hw_info (xc_interface *xch,uint32_t socket,xc_psr_feat_type type,xc_psr_hw_info *hw_info); - -typedef struct xc_cpu_policy xc_cpu_policy_t ; - -/* Create and free a xc_cpu_policy object. */ -xc_cpu_policy_t *xc_cpu_policy_init (void ); -void xc_cpu_policy_destroy (xc_cpu_policy_t *policy); - -/* Retrieve a system policy, or get/set a domains policy. */ -int xc_cpu_policy_get_system (xc_interface *xch,unsigned int policy_idx, -xc_cpu_policy_t *policy); -int xc_cpu_policy_get_domain (xc_interface *xch,uint32_t domid, -xc_cpu_policy_t *policy); -int xc_cpu_policy_set_domain (xc_interface *xch,uint32_t domid, -xc_cpu_policy_t *policy); - -/* Manipulate a policy via architectural representations. */ -int xc_cpu_policy_serialise (xc_interface *xch,const xc_cpu_policy_t *policy, -xen_cpuid_leaf_t *leaves,uint32_t *nr_leaves, -xen_msr_entry_t *msrs,uint32_t *nr_msrs); -int xc_cpu_policy_update_cpuid (xc_interface *xch,xc_cpu_policy_t *policy, -const xen_cpuid_leaf_t *leaves, -uint32_t nr); -int xc_cpu_policy_update_msrs (xc_interface *xch,xc_cpu_policy_t *policy, -const xen_msr_entry_t *msrs,uint32_t nr); - -/* Compatibility calculations. */ -bool xc_cpu_policy_is_compatible (xc_interface *xch,xc_cpu_policy_t *host, -xc_cpu_policy_t *guest); - -int xc_get_cpu_levelling_caps (xc_interface *xch,uint32_t *caps); -int xc_get_cpu_featureset (xc_interface *xch,uint32_t index, -uint32_t *nr_features,uint32_t *featureset); - -int xc_cpu_policy_get_size (xc_interface *xch,uint32_t *nr_leaves, -uint32_t *nr_msrs); -int xc_set_domain_cpu_policy (xc_interface *xch,uint32_t domid, -uint32_t nr_leaves,xen_cpuid_leaf_t *leaves, -uint32_t nr_msrs,xen_msr_entry_t *msrs, -uint32_t *err_leaf_p,uint32_t *err_subleaf_p, -uint32_t *err_msr_p); - -uint32_t xc_get_cpu_featureset_size (void ); - -enum xc_static_cpu_featuremask { -XC_FEATUREMASK_KNOWN , -XC_FEATUREMASK_SPECIAL , -XC_FEATUREMASK_PV_MAX , -XC_FEATUREMASK_PV_DEF , -XC_FEATUREMASK_HVM_SHADOW_MAX , -XC_FEATUREMASK_HVM_SHADOW_DEF , -XC_FEATUREMASK_HVM_HAP_MAX , -XC_FEATUREMASK_HVM_HAP_DEF , -}; -const uint32_t *xc_get_static_cpu_featuremask (enum xc_static_cpu_featuremask ); -#endif int xc_livepatch_upload(xc_interface *xch,
tools/include/xenguest.h +56 -0
60 lines not on this page · raw message
tools/libs/ctrl/xc_bitops.h +5 -1
@@ -6,7 +6,11 @@#include <stdlib.h> #include <string.h> -#define BITS_PER_LONG (sizeof(unsigned long) * 8) +#ifdef __LP64__ +#define BITS_PER_LONG 64 +#else +#define BITS_PER_LONG 32 +#endif #define BITMAP_ENTRY (_nr,_bmap) ((_bmap))[(_nr) / 8]#define BITMAP_SHIFT (_nr) ((_nr) % 8)
tools/libs/guest/xg_cpuid_x86.c +0 -1
@@ -23,7 +23,6 @@#include <stdbool.h> #include <limits.h> #include "xg_private.h" -#include "xc_bitops.h" #include <xen/hvm/params.h> #include <xen-tools/libs.h>
tools/libs/guest/xg_private.h +1 -0
@@ -27,6 +27,7 @@#include <sys/stat.h> #include "xc_private.h" +#include "xc_bitops.h" #include "xenguest.h" #include <xen/memory.h>
tools/misc/xen-cpuid.c +1 -0
@@ -8,6 +8,7 @@#include <inttypes.h> #include <xenctrl.h> +#include <xenguest.h> #include <xen-tools/libs.h>
Signature · 1 line
2.33.0
PATCH v5 2/12 libx86: introduce helper to fetch cpuid leaf
In reply to Roger Pau MonneIntroduce a helper based on the current Xen guest_cpuid code in order to fetch a cpuid leaf from a policy. The newly introduced function in cpuid.c should not be directly called and instead the provided x86_cpuid_get_leaf macro should be used that will properly deal with const and non-const inputs.
Also add a test to check that the introduced helper doesn't go over the bounds of the policy.
Note the code in x86_cpuid_copy_from_buffer is not switched to use the new function because of the boundary checks against the max fields of the policy, which might not be properly set at the point where x86_cpuid_copy_from_buffer get called, for example when filling an empty policy from scratch.
---
Changes since v4:
- Rename _x86_cpuid_get_leaf to x86_cpuid_get_leaf_const.
Changes since v3:
- New in this version.
---
Regarding safety of the usage of array_access_nospec to obtain a pointer to an element of an array, there are already other instances of this usage, for example in viridian_time_wrmsr, so I would assume this is fine.
--- tools/tests/cpu-policy/test-cpu-policy.c | 75 ++++++++++++++++++++++++ xen/arch/x86/cpuid.c | 55 +++-------------- xen/include/xen/lib/x86/cpuid.h | 19 ++++++ xen/lib/x86/cpuid.c | 52 ++++++++++++++++ 4 files changed, 153 insertions(+), 48 deletions(-)
tools/tests/cpu-policy/test-cpu-policy.c +75 -0
@@ -570,6 +570,80 @@ static void test_cpuid_out_of_range_clearing(void) } } +static void test_cpuid_get_leaf_failure (void ) +{ +static const struct test { +struct cpuid_policy p; +const char *name; +uint32_t leaf, subleaf; + } tests[] = { +/* Bound checking logic. */ + { + .name ="Basic max leaf >= array size" , + .p = { + .basic.max_leaf =CPUID_GUEST_NR_BASIC , + }, + }, + { + .name ="Feature max leaf >= array size" , + .p = { + .basic.max_leaf =CPUID_GUEST_NR_BASIC -1 , + .feat.max_subleaf =CPUID_GUEST_NR_FEAT , + }, + .leaf =0x00000007 , + }, + { + .name ="Extended max leaf >= array size" , + .p = { + .extd.max_leaf =0x80000000 +CPUID_GUEST_NR_EXTD , + }, + .leaf =0x80000000 , + }, + + { + .name ="Basic leaf >= max leaf" , + .p = { + .basic.max_leaf =CPUID_GUEST_NR_BASIC -1 , + }, + .leaf =CPUID_GUEST_NR_BASIC , + }, + { + .name ="Feature leaf >= max leaf" , + .p = { + .basic.max_leaf =CPUID_GUEST_NR_BASIC -1 , + .feat.max_subleaf =CPUID_GUEST_NR_FEAT -1 , + }, + .leaf =0x00000007 , + .subleaf =CPUID_GUEST_NR_FEAT , + }, + { + .name ="Extended leaf >= max leaf" , + .p = { + .extd.max_leaf =0x80000000 +CPUID_GUEST_NR_EXTD -1 , + }, + .leaf =0x80000000 +CPUID_GUEST_NR_EXTD , + }, + }; +const struct cpuid_policy pc; +const struct cpuid_leaf *lc; +struct cpuid_policy p; +struct cpuid_leaf *l; + +/* Constness build test. */ + lc =x86_cpuid_get_leaf (&pc,0 ,0 ); + l =x86_cpuid_get_leaf (&p,0 ,0 ); + +printf ("Testing CPUID get leaf bound checking:\n" ); + +for (size_t i =0 ; i <ARRAY_SIZE (tests); ++i ) + { +const struct test *t = &tests[i]; + +if (x86_cpuid_get_leaf (&t->p, t->leaf, t->subleaf) ) +fail (" Test %s get leaf fail\n" , t->name); + } +} +static void test_is_compatible_success (void ) { static struct test {
@@ -685,6 +759,7 @@ int main(int argc, char **argv)test_cpuid_serialise_success ();test_cpuid_deserialise_failure ();test_cpuid_out_of_range_clearing (); +test_cpuid_get_leaf_failure ();test_msr_serialise_success ();test_msr_deserialise_failure ();
xen/arch/x86/cpuid.c +7 -48
70 lines not on this page · raw message
xen/include/xen/lib/x86/cpuid.h +19 -0
@@ -431,6 +431,25 @@ int x86_cpuid_copy_from_buffer(struct cpuid_policy *policy, uint32_t nr_entries, uint32_t *err_leaf, uint32_t *err_subleaf); +/** +* Get a cpuid leaf from a policy object. +* +* @param policy The cpuid_policy object. +* @param leaf The leaf index. +* @param subleaf The subleaf index. +* @returns a pointer to the requested leaf or NULL in case of error. +* +* The function will perform out of bound checks. Do not call this function +* directly and instead use x86_cpuid_get_leaf that will deal with both const +* and non-const policies returning a pointer with constness matching that of +* the input. +*/ +const struct cpuid_leaf *x86_cpuid_get_leaf_const (const struct cpuid_policy *p, +uint32_t leaf, +uint32_t subleaf); +#define x86_cpuid_get_leaf (p, l, s) \ + ((__typeof__(&(p)->basic.raw[0]))x86_cpuid_get_leaf_const(p, l, s)) +#endif /* !XEN_LIB_X86_CPUID_H */ /*
xen/lib/x86/cpuid.c +52 -0
58 lines not on this page · raw message
Signature · 1 line
2.33.0
Re: [PATCH v5 02/12] libx86: introduce helper to fetch cpuid leaf
Quoted 9 lines · Roger Pau Monne
Introduce a helper based on the current Xen
guest_cpuidcode in order to fetch a cpuid leaf from a policy. The newly introduced function incpuid.cshould not be directly called and instead the providedx86_cpuid_get_leafmacro should be used that will properly deal with const and non-const inputs.Also add a test to check that the introduced helper doesn't go over the bounds of the policy.
Quoted 8 lines
Note the code in
x86_cpuid_copy_from_bufferis not switched to use the new function because of the boundary checks against the max fields of the policy, which might not be properly set at the point wherex86_cpuid_copy_from_bufferget called, for example when filling an empty policy from scratch.Suggested-by Andrew Cooper <[email protected]>Signed-off-by Roger Pau Monné <[email protected]>
Quoted 5 lines
---
Regarding safety of the usage ofarray_access_nospecto obtain a pointer to an element of an array, there are already other instances of this usage, for example inviridian_time_wrmsr, so I would assume this is fine.
I wonder what it is that you think one might be concerned about. Such pointer retrieval is no different from an actual array access imo.
Jan
PATCH v5 3/12 libs/guest: allow fetching a specific CPUID leaf from a cpu policy
In reply to Roger Pau MonneIntroduce an interface that returns a specific leaf/subleaf from a cpu policy in xen_cpuid_leaf_t format.
This is useful to callers can peek data from the opaque xc_cpu_policy_t type.
No caller of the interface introduced on this patch.
Note that callers of find_leaf need to be slightly adjusted to use the new helper parameters.
---
Changes since v3:
- Use x86_cpuid_get_leaf.
Changes since v1:
- Use find leaf.
--- tools/include/xenguest.h | 3 +++ tools/libs/guest/xg_cpuid_x86.c | 23 +++++++++++++++++++++++ 2 files changed, 26 insertions(+)
tools/include/xenguest.h +3 -0
@@ -807,6 +807,9 @@ int xc_cpu_policy_update_cpuid(xc_interface *xch, xc_cpu_policy_t *policy, uint32_t nr);int xc_cpu_policy_update_msrs (xc_interface *xch,xc_cpu_policy_t *policy,const xen_msr_entry_t *msrs,uint32_t nr); +int xc_cpu_policy_get_cpuid (xc_interface *xch,const xc_cpu_policy_t *policy, +uint32_t leaf,uint32_t subleaf, +xen_cpuid_leaf_t *out);/* Compatibility calculations. */ bool xc_cpu_policy_is_compatible(xc_interface *xch, xc_cpu_policy_t *host,
tools/libs/guest/xg_cpuid_x86.c +23 -0
@@ -855,6 +855,29 @@ int xc_cpu_policy_update_msrs(xc_interface *xch, xc_cpu_policy_t *policy,return rc; } +int xc_cpu_policy_get_cpuid (xc_interface *xch,const xc_cpu_policy_t *policy, +uint32_t leaf,uint32_t subleaf, +xen_cpuid_leaf_t *out) +{ +const struct cpuid_leaf *tmp; + + tmp =x86_cpuid_get_leaf (&policy->cpuid, leaf, subleaf); +if ( !tmp ) + { +/* Unable to find a matching leaf. */ + errno =ENOENT ; +return -1 ; + } + + out->leaf = leaf; + out->subleaf = subleaf; + out->a = tmp->a; + out->b = tmp->b; + out->c = tmp->c; + out->d = tmp->d; +return 0 ; +} +bool xc_cpu_policy_is_compatible (xc_interface *xch,xc_cpu_policy_t *host,xc_cpu_policy_t *guest) {
Signature · 1 line
2.33.0
Re: [PATCH v5 03/12] libs/guest: allow fetching a specific CPUID leaf from a cpu policy
Quoted 10 lines · Roger Pau Monne
Introduce an interface that returns a specific leaf/subleaf from a cpu policy in
xen_cpuid_leaf_tformat.This is useful to callers can peek data from the opaque
xc_cpu_policy_ttype.No caller of the interface introduced on this patch.
Note that callers of
find_leafneed to be slightly adjusted to use the new helper parameters.
Is this sentence a leftover from an earlier version? I can't associate it with anything.
tools/libs/guest/xg_cpuid_x86.c int xc_cpu_policy_update_msrs(xc_interface *xch, xc_cpu_policy_t *policy, · line 860
Quoted 9 lines
tools/libs/guest/xg_cpuid_x86.c +3
-0@@ -855,6 +855,29 @@ int xc_cpu_policy_update_msrs(xc_interface *xch, xc_cpu_policy_t *policy,return rc; } +int xc_cpu_policy_get_cpuid (xc_interface *xch,const xc_cpu_policy_t *policy, +uint32_t leaf,uint32_t subleaf, +xen_cpuid_leaf_t *out)
Is it common practice in libxc / libxg to have xch parameters even if they're unused?
Jan
Re: [PATCH v5 03/12] libs/guest: allow fetching a specific CPUID leaf from a cpu policy
Quoted 7 lines · Jan Beulich
Quoted 6 lines · Roger Pau Monne
Introduce an interface that returns a specific leaf/subleaf from a cpu policy in
xen_cpuid_leaf_tformat.This is useful to callers can peek data from the opaque
xc_cpu_policy_ttype.
Quoted 7 lines
Quoted 4 lines
No caller of the interface introduced on this patch.
Note that callers of
find_leafneed to be slightly adjusted to use the new helper parameters.Is this sentence a leftover from an earlier version? I can't associate it with anything.
Yes, looks like. Will remove it.
Quoted 6 lines
Quoted 6 lines
tools/libs/guest/xg_cpuid_x86.c +0
-0@@ -855,6 +855,29 @@ int xc_cpu_policy_update_msrs(xc_interface *xch, xc_cpu_policy_t *policy,return rc; }
Quoted 6 lines
tools/libs/guest/xg_cpuid_x86.c int xc_cpu_policy_update_msrs(xc_interface *xch, xc_cpu_policy_t *policy, · line 860
Quoted 3 lines
tools/libs/guest/xg_cpuid_x86.c +3
-0+int xc_cpu_policy_get_cpuid (xc_interface *xch,const xc_cpu_policy_t *policy, +uint32_t leaf,uint32_t subleaf, +xen_cpuid_leaf_t *out)Is it common practice in libxc / libxg to have xch parameters even if they're unused?
I think it's good practice, as if we ever need to add to use ERROR or similar from those functions we would require xch, and it's better to avoid having to change the interface later.
Thanks, Roger.
PATCH v5 4/12 libx86: introduce helper to fetch msr entry
In reply to Roger Pau MonneUse such helper in order to replace the code in x86_msr_copy_from_buffer. Note the introduced helper should not be directly called and instead x86_msr_get_entry should be used that will properly deal with const and non-const inputs.
Note this requires making the raw fields uint64_t so that it can accommodate the maximum size of MSRs values, and in turn removing the truncation tests.
---
Changes since v4:
- Rename _x86_msr_get_entry to x86_msr_get_entry_const.
- Add newline before endif.
Changes since v3:
- New in this version.
--- tools/tests/cpu-policy/test-cpu-policy.c | 48 +++++++++++++++++++----- xen/include/xen/lib/x86/msr.h | 20 +++++++++- xen/lib/x86/msr.c | 41 ++++++++++---------- 3 files changed, 76 insertions(+), 33 deletions(-)
tools/tests/cpu-policy/test-cpu-policy.c +38 -10
@@ -386,16 +386,6 @@ static void test_msr_deserialise_failure(void) .msr = { .idx = 0xce, .flags = 1 }, .rc = -EINVAL, }, - { - .name = "truncated val", - .msr = { .idx = 0xce, .val = ~0ull }, - .rc = -EOVERFLOW, - }, - { - .name = "truncated val", - .msr = { .idx = 0x10a, .val = ~0ull }, - .rc = -EOVERFLOW, - }, };printf ("Testing MSR deserialise failure:\n" );
@@ -644,6 +634,43 @@ static void test_cpuid_get_leaf_failure(void) } } +static void test_msr_get_entry (void ) +{ +static const struct test { +const char *name; +unsigned int idx; +bool success; + } tests[] = { + { + .name ="bad msr index" , + .idx =-1 , + }, + { + .name ="good msr index" , + .idx =0xce , + .success = true, + }, + }; +const struct msr_policy pc; +const uint64_t *ec; +struct msr_policy p; +uint64_t *e; + +/* Constness build test. */ + ec =x86_msr_get_entry (&pc,0 ); + e =x86_msr_get_entry (&p,0 ); + +printf ("Testing MSR get leaf:\n" ); + +for (size_t i =0 ; i <ARRAY_SIZE (tests); ++i ) + { +const struct test *t = &tests[i]; + +if ( !!x86_msr_get_entry (&pc, t->idx) != t->success ) +fail (" Test %s failed\n" , t->name); + } +} +static void test_is_compatible_success (void ) { static struct test {
@@ -763,6 +790,7 @@ int main(int argc, char **argv)test_msr_serialise_success ();test_msr_deserialise_failure (); +test_msr_get_entry ();test_is_compatible_success ();test_is_compatible_failure ();
xen/include/xen/lib/x86/msr.h +18 -2
@@ -17,7 +17,7 @@ struct msr_policy* is dependent on real hardware support. */ union { -uint32_t raw; +uint64_t raw;struct { uint32_t :31;bool cpuid_faulting:1 ;
@@ -32,7 +32,7 @@ struct msr_policy* fixed in hardware. */ union { -uint32_t raw; +uint64_t raw;struct {bool rdcl_no:1 ;bool ibrs_all:1 ;
@@ -91,6 +91,22 @@ int x86_msr_copy_from_buffer(struct msr_policy *policy, const msr_entry_buffer_t msrs, uint32_t nr_entries, uint32_t *err_msr); +/** +* Get a MSR entry from a policy object. +* +* @param policy The msr_policy object. +* @param idx The index. +* @returns a pointer to the requested leaf or NULL in case of error. +* +* Do not call this function directly and instead use x86_msr_get_entry that +* will deal with both const and non-const policies returning a pointer with +* constness matching that of the input. +*/ +const uint64_t *x86_msr_get_entry_const (const struct msr_policy *policy, +uint32_t idx); +#define x86_msr_get_entry (p, i) \ + ((__typeof__(&(p)->platform_info.raw))x86_msr_get_entry_const(p, i)) +#endif /* !XEN_LIB_X86_MSR_H */ /*
xen/lib/x86/msr.c +20 -21
63 lines not on this page · raw message
Signature · 1 line
2.33.0
Re: [PATCH v5 04/12] libx86: introduce helper to fetch msr entry
Quoted 7 lines · Roger Pau Monne
tools/tests/cpu-policy/test-cpu-policy.c +0
-1@@ -386,16 +386,6 @@ static void test_msr_deserialise_failure(void) .msr = { .idx = 0xce, .flags = 1 }, .rc = -EINVAL, }, - {
tools/tests/cpu-policy/test-cpu-policy.c static void test_msr_deserialise_failure(void) · line 389
Quoted 10 lines
tools/tests/cpu-policy/test-cpu-policy.c +0
-9- .name = "truncated val", - .msr = { .idx = 0xce, .val = ~0ull }, - .rc = -EOVERFLOW, - }, - { - .name = "truncated val", - .msr = { .idx = 0x10a, .val = ~0ull }, - .rc = -EOVERFLOW, - }, };
Why is this removal necessary? I don't think any of the additions further down actually replaces these.
Jan
Re: [PATCH v5 04/12] libx86: introduce helper to fetch msr entry
Quoted 13 lines · Jan Beulich
Quoted 12 lines · Roger Pau Monne
tools/tests/cpu-policy/test-cpu-policy.c +0
-6@@ -386,16 +386,6 @@ static void test_msr_deserialise_failure(void) .msr = { .idx = 0xce, .flags = 1 }, .rc = -EINVAL, }, - { - .name = "truncated val", - .msr = { .idx = 0xce, .val = ~0ull }, - .rc = -EOVERFLOW, - }, - {
Quoted 8 lines
tools/tests/cpu-policy/test-cpu-policy.c static void test_msr_deserialise_failure(void) · line 389
Quoted 5 lines
tools/tests/cpu-policy/test-cpu-policy.c +0
-4- .name = "truncated val", - .msr = { .idx = 0x10a, .val = ~0ull }, - .rc = -EOVERFLOW, - }, };Why is this removal necessary? I don't think any of the additions further down actually replaces these.
There are no longer overflow errors, since the field in msr_policy for both MSRs is now 64bits after this change. I've tried to explain this in the commit message:
"Note this requires making the raw fields uint64_t so that it can accommodate the maximum size of MSRs values, and in turn removing the truncation tests."
Thanks, Roger.
PATCH v5 5/12 libs/guest: allow fetching a specific MSR entry from a cpu policy
In reply to Roger Pau MonneIntroduce an interface that returns a specific MSR entry from a cpu policy in xen_msr_entry_t format.
This is useful to callers can peek data from the opaque xc_cpu_policy_t type.
No caller of the interface introduced on this patch.
---
Changes since v3:
- Use x86_msr_get_entry.
Changes since v1:
- Introduce a helper to perform a binary search of the MSR entries array.
--- tools/include/xenguest.h | 2 ++ tools/libs/guest/xg_cpuid_x86.c | 20 ++++++++++++++++++++ 2 files changed, 22 insertions(+)
tools/include/xenguest.h +2 -0
@@ -810,6 +810,8 @@ int xc_cpu_policy_update_msrs(xc_interface *xch, xc_cpu_policy_t *policy,int xc_cpu_policy_get_cpuid (xc_interface *xch,const xc_cpu_policy_t *policy,uint32_t leaf,uint32_t subleaf,xen_cpuid_leaf_t *out); +int xc_cpu_policy_get_msr (xc_interface *xch,const xc_cpu_policy_t *policy, +uint32_t msr,xen_msr_entry_t *out);/* Compatibility calculations. */ bool xc_cpu_policy_is_compatible(xc_interface *xch, xc_cpu_policy_t *host,
tools/libs/guest/xg_cpuid_x86.c +20 -0
@@ -878,6 +878,26 @@ int xc_cpu_policy_get_cpuid(xc_interface *xch, const xc_cpu_policy_t *policy,return 0 ; } +int xc_cpu_policy_get_msr (xc_interface *xch,const xc_cpu_policy_t *policy, +uint32_t msr,xen_msr_entry_t *out) +{ +const uint64_t *val; + + *out = (xen_msr_entry_t ){}; + + val =x86_msr_get_entry (&policy->msr, msr); +if ( !val ) + { + errno =ENOENT ; +return -1 ; + } + + out->idx = msr; + out->val = *val; + +return 0 ; +} +bool xc_cpu_policy_is_compatible (xc_interface *xch,xc_cpu_policy_t *host,xc_cpu_policy_t *guest) {
Signature · 1 line
2.33.0
Re: [PATCH v5 05/12] libs/guest: allow fetching a specific MSR entry from a cpu policy
tools/libs/guest/xg_cpuid_x86.c int xc_cpu_policy_get_cpuid(xc_interface *xch, const xc_cpu_policy_t *policy, · line 886
Quoted 12 lines · Roger Pau Monne
tools/libs/guest/xg_cpuid_x86.c +6
-0@@ -878,6 +878,26 @@ int xc_cpu_policy_get_cpuid(xc_interface *xch, const xc_cpu_policy_t *policy,return 0 ; } +int xc_cpu_policy_get_msr (xc_interface *xch,const xc_cpu_policy_t *policy, +uint32_t msr,xen_msr_entry_t *out) +{ +const uint64_t *val; + + *out = (xen_msr_entry_t ){};
Unless there's a specific reason for the divergence, this just-in- case initialization should imo be consistently there (or absent) in both the MSR and the CPUID function.
Jan
Re: [PATCH v5 05/12] libs/guest: allow fetching a specific MSR entry from a cpu policy
Quoted 7 lines · Jan Beulich
Quoted 6 lines · Roger Pau Monne
tools/libs/guest/xg_cpuid_x86.c +0
-0@@ -878,6 +878,26 @@ int xc_cpu_policy_get_cpuid(xc_interface *xch, const xc_cpu_policy_t *policy,return 0 ; }
Quoted 10 lines
tools/libs/guest/xg_cpuid_x86.c int xc_cpu_policy_get_cpuid(xc_interface *xch, const xc_cpu_policy_t *policy, · line 886
Quoted 6 lines
tools/libs/guest/xg_cpuid_x86.c +6
-0+int xc_cpu_policy_get_msr (xc_interface *xch,const xc_cpu_policy_t *policy, +uint32_t msr,xen_msr_entry_t *out) +{ +const uint64_t *val; + + *out = (xen_msr_entry_t ){};Unless there's a specific reason for the divergence, this just-in- case initialization should imo be consistently there (or absent) in both the MSR and the CPUID function.
Right - will add the initialization to the CPUID function.
Thanks, Roger.
PATCH v5 6/12 libs/guest: make a cpu policy compatible with older Xen versions
In reply to Roger Pau MonneOlder Xen versions used to expose some CPUID bits which are no longer exposed by default. In order to keep a compatible behavior with guests migrated from versions of Xen that don't encode the CPUID data on the migration stream introduce a function that sets the same bits as older Xen versions.
This is pulled out from xc_cpuid_apply_policy which already has this logic present.
No functional change intended.
---
Changes since v3:
- Rename function to xc_cpu_policy_make_compat_4_12.
Changes since v1:
- Move comments and explicitly mention pre-4.13 Xen.
--- tools/include/xenguest.h | 4 +++ tools/libs/guest/xg_cpuid_x86.c | 62 ++++++++++++++++++++++++--------- 2 files changed, 49 insertions(+), 17 deletions(-)
tools/include/xenguest.h +4 -0
@@ -817,6 +817,10 @@ int xc_cpu_policy_get_msr(xc_interface *xch, const xc_cpu_policy_t *policy,bool xc_cpu_policy_is_compatible (xc_interface *xch,xc_cpu_policy_t *host,xc_cpu_policy_t *guest); +/* Make a policy compatible with pre-4.13 Xen versions. */ +int xc_cpu_policy_make_compat_4_12 (xc_interface *xch,xc_cpu_policy_t *policy, +bool hvm); +int xc_get_cpu_levelling_caps (xc_interface *xch,uint32_t *caps);int xc_get_cpu_featureset (xc_interface *xch,uint32_t index,uint32_t *nr_features,uint32_t *featureset);
tools/libs/guest/xg_cpuid_x86.c +45 -17
77 lines not on this page · raw message
Signature · 1 line
2.33.0
PATCH v5 7/12 libs/guest: introduce helper set cpu topology in cpu policy
In reply to Roger Pau MonneThis logic is pulled out from xc_cpuid_apply_policy and placed into a separate helper. Note the legacy part of the introduced function, as long term Xen will require a proper topology setter function capable of expressing a more diverse set of topologies.
No functional change intended.
---
- s/xc_cpu_policy_topology/xc_cpu_policy_legacy_topology/
--- tools/include/xenguest.h | 4 + tools/libs/guest/xg_cpuid_x86.c | 186 +++++++++++++++++--------------- 2 files changed, 105 insertions(+), 85 deletions(-)
tools/include/xenguest.h +4 -0
@@ -821,6 +821,10 @@ bool xc_cpu_policy_is_compatible(xc_interface *xch, xc_cpu_policy_t *host,int xc_cpu_policy_make_compat_4_12 (xc_interface *xch,xc_cpu_policy_t *policy,bool hvm); +/* Setup the legacy policy topology. */ +int xc_cpu_policy_legacy_topology (xc_interface *xch,xc_cpu_policy_t *policy, +bool hvm); +int xc_get_cpu_levelling_caps (xc_interface *xch,uint32_t *caps);int xc_get_cpu_featureset (xc_interface *xch,uint32_t index,uint32_t *nr_features,uint32_t *featureset);
tools/libs/guest/xg_cpuid_x86.c +101 -85
211 lines not on this page · raw message
Signature · 1 line
2.33.0
Re: [PATCH v5 07/12] libs/guest: introduce helper set cpu topology in cpu policy
tools/libs/guest/xg_cpuid_x86.c int xc_cpuid_apply_policy(xc_interface *xch, uint32_t domid, bool restore, · line 463
Quoted 7 lines · Roger Pau Monne
tools/libs/guest/xg_cpuid_x86.c +0
-3@@ -458,22 +456,6 @@ int xc_cpuid_apply_policy(xc_interface *xch, uint32_t domid, bool restore, (p = calloc(1, sizeof(*p))) == NULL ) goto out; -/* Get the host policy. */ - rc =xc_get_cpu_featureset (xch, XEN_SYSCTL_cpu_featureset_host, - &len, host_featureset);
You go from retrieving the host featureset to ...
Quoted 11 lines
tools/libs/guest/xg_cpuid_x86.c +7
-0@@ -944,3 +865,98 @@ int xc_cpu_policy_make_compat_4_12(xc_interface *xch, xc_cpu_policy_t *policy,xc_cpu_policy_destroy (host);return rc; } + +int xc_cpu_policy_legacy_topology (xc_interface *xch,xc_cpu_policy_t *policy, +bool hvm) +{ +if ( !hvm ) + { +xc_cpu_policy_t *host;
tools/libs/guest/xg_cpuid_x86.c int xc_cpu_policy_make_compat_4_12(xc_interface *xch, xc_cpu_policy_t *policy, · line 884
Quoted 10 lines
tools/libs/guest/xg_cpuid_x86.c +10
-0+int rc; + + host =xc_cpu_policy_init (); +if ( !host ) + { + errno =ENOMEM ; +return -1 ; + } + + rc =xc_cpu_policy_get_system (xch, XEN_SYSCTL_cpu_policy_host, host);
... retrieving the host policy, which afaict is a larger blob of data. Is there a particular reason for doing so?
Quoted 50 lines
tools/libs/guest/xg_cpuid_x86.c +50
-0+if ( rc ) + { +ERROR ("Failed to get host policy" ); +xc_cpu_policy_destroy (host); +return rc; + } + + +/* +* On hardware without CPUID Faulting, PV guests see real topology. +* As a consequence, they also need to see the host htt/cmp fields. +*/ + policy->cpuid.basic.htt = host->cpuid.basic.htt; + policy->cpuid.extd.cmp_legacy = host->cpuid.extd.cmp_legacy; + } +else + { +unsigned int i; + +/* +* Topology for HVM guests is entirely controlled by Xen. For now, we +* hardcode APIC_ID = vcpu_id * 2 to give the illusion of no SMT. +*/ + policy->cpuid.basic.htt = true; + policy->cpuid.extd.cmp_legacy = false; + +/* +* Leaf 1 EBX[23:16] is Maximum Logical Processors Per Package. +* Update to reflect vLAPIC_ID = vCPU_ID * 2, but make sure to avoid +* overflow. +*/ +if ( !policy->cpuid.basic.lppp ) + policy->cpuid.basic.lppp =2 ; +else if ( !(policy->cpuid.basic.lppp &0x80 ) ) + policy->cpuid.basic.lppp *=2 ; + +switch ( policy->cpuid.x86_vendor ) + { +case X86_VENDOR_INTEL : +for ( i =0 ; (policy->cpuid.cache.subleaf[i].type && + i <ARRAY_SIZE (policy->cpuid.cache.raw)); ++i ) + { + policy->cpuid.cache.subleaf[i].cores_per_package = + (policy->cpuid.cache.subleaf[i].cores_per_package <<1 ) |1 ; + policy->cpuid.cache.subleaf[i].threads_per_cache =0 ; + } +break ; + +case X86_VENDOR_AMD : +case X86_VENDOR_HYGON :
tools/libs/guest/xg_cpuid_x86.c int xc_cpu_policy_make_compat_4_12(xc_interface *xch, xc_cpu_policy_t *policy, · line 944
Quoted 10 lines
tools/libs/guest/xg_cpuid_x86.c +10
-0+/* +* Leaf 0x80000008 ECX[15:12] is ApicIdCoreSize. +* Leaf 0x80000008 ECX[7:0] is NumberOfCores (minus one). +* Update to reflect vLAPIC_ID = vCPU_ID * 2. But avoid +* - overflow, +* - going out of sync with leaf 1 EBX[23:16], +* - incrementing ApicIdCoreSize when it's zero (which changes the +* meaning of bits 7:0). +* +* UPDATE: I addition to avoiding overflow, some
Nit: Would you mind switching "I" to "In" at this occasion?
Jan
Re: [PATCH v5 07/12] libs/guest: introduce helper set cpu topology in cpu policy
Quoted 27 lines · Jan Beulich
tools/libs/guest/xg_cpuid_x86.c int xc_cpuid_apply_policy(xc_interface *xch, uint32_t domid, bool restore, · line 463
Quoted 7 lines · Roger Pau Monne
tools/libs/guest/xg_cpuid_x86.c +0
-3@@ -458,22 +456,6 @@ int xc_cpuid_apply_policy(xc_interface *xch, uint32_t domid, bool restore, (p = calloc(1, sizeof(*p))) == NULL ) goto out; -/* Get the host policy. */ - rc =xc_get_cpu_featureset (xch, XEN_SYSCTL_cpu_featureset_host, - &len, host_featureset);You go from retrieving the host featureset to ...
Quoted 16 lines
tools/libs/guest/xg_cpuid_x86.c +12
-0@@ -944,3 +865,98 @@ int xc_cpu_policy_make_compat_4_12(xc_interface *xch, xc_cpu_policy_t *policy,xc_cpu_policy_destroy (host);return rc; } + +int xc_cpu_policy_legacy_topology (xc_interface *xch,xc_cpu_policy_t *policy, +bool hvm) +{ +if ( !hvm ) + { +xc_cpu_policy_t *host; +int rc; + + host =xc_cpu_policy_init (); +if ( !host ) + {
Quoted 8 lines
tools/libs/guest/xg_cpuid_x86.c int xc_cpu_policy_make_compat_4_12(xc_interface *xch, xc_cpu_policy_t *policy, · line 884
Quoted 5 lines
tools/libs/guest/xg_cpuid_x86.c +5
-0+ errno =ENOMEM ; +return -1 ; + } + + rc =xc_cpu_policy_get_system (xch, XEN_SYSCTL_cpu_policy_host, host);... retrieving the host policy, which afaict is a larger blob of data. Is there a particular reason for doing so?
I did that so I could assign from one CPUID policy to another, but will revert back to using a featureset since it's indeed smaller.
Quoted 54 lines
Quoted 54 lines
tools/libs/guest/xg_cpuid_x86.c +54
-0+if ( rc ) + { +ERROR ("Failed to get host policy" ); +xc_cpu_policy_destroy (host); +return rc; + } + + +/* +* On hardware without CPUID Faulting, PV guests see real topology. +* As a consequence, they also need to see the host htt/cmp fields. +*/ + policy->cpuid.basic.htt = host->cpuid.basic.htt; + policy->cpuid.extd.cmp_legacy = host->cpuid.extd.cmp_legacy; + } +else + { +unsigned int i; + +/* +* Topology for HVM guests is entirely controlled by Xen. For now, we +* hardcode APIC_ID = vcpu_id * 2 to give the illusion of no SMT. +*/ + policy->cpuid.basic.htt = true; + policy->cpuid.extd.cmp_legacy = false; + +/* +* Leaf 1 EBX[23:16] is Maximum Logical Processors Per Package. +* Update to reflect vLAPIC_ID = vCPU_ID * 2, but make sure to avoid +* overflow. +*/ +if ( !policy->cpuid.basic.lppp ) + policy->cpuid.basic.lppp =2 ; +else if ( !(policy->cpuid.basic.lppp &0x80 ) ) + policy->cpuid.basic.lppp *=2 ; + +switch ( policy->cpuid.x86_vendor ) + { +case X86_VENDOR_INTEL : +for ( i =0 ; (policy->cpuid.cache.subleaf[i].type && + i <ARRAY_SIZE (policy->cpuid.cache.raw)); ++i ) + { + policy->cpuid.cache.subleaf[i].cores_per_package = + (policy->cpuid.cache.subleaf[i].cores_per_package <<1 ) |1 ; + policy->cpuid.cache.subleaf[i].threads_per_cache =0 ; + } +break ; + +case X86_VENDOR_AMD : +case X86_VENDOR_HYGON : +/* +* Leaf 0x80000008 ECX[15:12] is ApicIdCoreSize. +* Leaf 0x80000008 ECX[7:0] is NumberOfCores (minus one). +* Update to reflect vLAPIC_ID = vCPU_ID * 2. But avoid
Quoted 8 lines
tools/libs/guest/xg_cpuid_x86.c int xc_cpu_policy_make_compat_4_12(xc_interface *xch, xc_cpu_policy_t *policy, · line 944
Quoted 6 lines
tools/libs/guest/xg_cpuid_x86.c +6
-0+* - overflow, +* - going out of sync with leaf 1 EBX[23:16], +* - incrementing ApicIdCoreSize when it's zero (which changes the +* meaning of bits 7:0). +* +* UPDATE: I addition to avoiding overflow, some Nit: Would you mind switching "I" to "In" at this occasion?
Will do.
Thanks, Roger.
PATCH v5 8/12 libs/guest: rework xc_cpuid_xend_policy
In reply to Roger Pau MonneRename xc_cpuid_xend_policy to xc_cpu_policy_apply_cpuid and make it public. Modify the function internally to use the new xc_cpu_policy_* set of functions. Also don't apply the passed policy to a domain directly, and instead modify the provided xc_cpu_policy_t. The caller will be responsible of applying the modified cpu policy to the domain.
Note that further patches will end up removing this function, as the callers should have the necessary helpers to modify an xc_cpu_policy_t themselves.
The find_leaf helper and related comparison function is also removed, as it's no longer needed to search for cpuid leafs as finding the matching leaves is now done using xc_cpu_policy_get_cpuid.
No functional change intended.
---
Changes since v3:
- Drop find_leaf and comparison helper.
--- tools/include/xenguest.h | 4 + tools/libs/guest/xg_cpuid_x86.c | 200 +++++++++++++------------------- 2 files changed, 83 insertions(+), 121 deletions(-)
tools/include/xenguest.h +4 -0
@@ -825,6 +825,10 @@ int xc_cpu_policy_make_compat_4_12(xc_interface *xch, xc_cpu_policy_t *policy,int xc_cpu_policy_legacy_topology (xc_interface *xch,xc_cpu_policy_t *policy,bool hvm); +/* Apply an xc_xend_cpuid object to the policy. */ +int xc_cpu_policy_apply_cpuid (xc_interface *xch,xc_cpu_policy_t *policy, +const struct xc_xend_cpuid *cpuid,bool hvm); +int xc_get_cpu_levelling_caps (xc_interface *xch,uint32_t *caps);int xc_get_cpu_featureset (xc_interface *xch,uint32_t index,uint32_t *nr_features,uint32_t *featureset);
tools/libs/guest/xg_cpuid_x86.c +79 -121
270 lines not on this page · raw message
Signature · 1 line
2.33.0
PATCH v5 9/12 libs/guest: apply a featureset into a cpu policy
In reply to Roger Pau MonnePull out the code from xc_cpuid_apply_policy that applies a featureset to a cpu policy and place it on it's own standalone function that's part of the public interface.
No functional change intended.
--- tools/include/xenguest.h | 5 ++ tools/libs/guest/xg_cpuid_x86.c | 95 ++++++++++++++++++++------------- 2 files changed, 62 insertions(+), 38 deletions(-)
tools/include/xenguest.h +5 -0
@@ -829,6 +829,11 @@ int xc_cpu_policy_legacy_topology(xc_interface *xch, xc_cpu_policy_t *policy,int xc_cpu_policy_apply_cpuid (xc_interface *xch,xc_cpu_policy_t *policy,const struct xc_xend_cpuid *cpuid,bool hvm); +/* Apply a featureset to the policy. */ +int xc_cpu_policy_apply_featureset (xc_interface *xch,xc_cpu_policy_t *policy, +const uint32_t *featureset, +unsigned int nr_features); +int xc_get_cpu_levelling_caps (xc_interface *xch,uint32_t *caps);int xc_get_cpu_featureset (xc_interface *xch,uint32_t index,uint32_t *nr_features,uint32_t *featureset);
tools/libs/guest/xg_cpuid_x86.c +57 -38
106 lines not on this page · raw message
Signature · 1 line
2.33.0
Re: [PATCH v5 09/12] libs/guest: apply a featureset into a cpu policy
Quoted 7 lines · Roger Pau Monne
Pull out the code from
xc_cpuid_apply_policythat applies a featureset to a cpu policy and place it on it's own standalone function that's part of the public interface.No functional change intended.
Signed-off-by Roger Pau Monné <[email protected]>
PATCH v5 10/12 libs/{light,guest}: implement xc_cpuid_apply_policy in libxl
In reply to Roger Pau MonneWith the addition of the xc_cpu_policy_* now libxl can have better control over the cpu policy, this allows removing the xc_cpuid_apply_policy function and instead coding the required bits by libxl in libxl__cpuid_legacy directly.
Remove xc_cpuid_apply_policy.
---
Changes since v4:
- Correctly account for PVH guests being HVM in libxl__cpuid_legacy.
- PAE option is only available to HVM guests (_not_ including PVH).
Changes since v2:
- Use 'r' for libxc return values.
- Fix comment about making a cpu policy compatible.
- Use LOG*D macros.
--- tools/include/xenctrl.h | 18 ----- tools/libs/guest/xg_cpuid_x86.c | 122 -------------------------------- tools/libs/light/libxl_cpuid.c | 92 ++++++++++++++++++++++-- 3 files changed, 86 insertions(+), 146 deletions(-)
tools/include/xenctrl.h +0 -18
@@ -1829,24 +1829,6 @@ struct xc_xend_cpuid {char *policy[4 ]; }; -/* -* Make adjustments to the CPUID settings for a domain. -* -* This path is used in two cases. First, for fresh boots of the domain, and -* secondly for migrate-in/restore of pre-4.14 guests (where CPUID data was -* missing from the stream). The @restore parameter distinguishes these -* cases, and the generated policy must be compatible with a 4.13. -* -* Either pass a full new @featureset (and @nr_features), or adjust individual -* features (@pae, @itsc, @nested_virt). -* -* Then (optionally) apply legacy XEND overrides (@xend) to the result. -*/ -int xc_cpuid_apply_policy (xc_interface *xch, -uint32_t domid,bool restore, -const uint32_t *featureset, -unsigned int nr_features,bool pae,bool itsc, -bool nested_virt,const struct xc_xend_cpuid *xend);int xc_mca_op (xc_interface *xch,struct xen_mc *mc);int xc_mca_op_inject_v2 (xc_interface *xch,unsigned int flags,xc_cpumap_t cpumap,unsigned int nr_cpus);
tools/libs/guest/xg_cpuid_x86.c +0 -122
128 lines not on this page · raw message
tools/libs/light/libxl_cpuid.c +86 -6
121 lines not on this page · raw message
Signature · 1 line
2.33.0
PATCH v5 11/12 libs/guest: (re)move xc_cpu_policy_apply_cpuid
In reply to Roger Pau MonneMove the logic from xc_cpu_policy_apply_cpuid into libxl, now that the xc_cpu_policy_* helpers allow modifying a cpu policy. By moving such parsing into libxl directly we can get rid of xc_xend_cpuid, as libxl will now implement it's own private type for storing CPUID information, which currently matches xc_xend_cpuid.
Note the function logic is moved as-is, but requires adapting to the libxl coding style.
No functional change intended.
---
Changes since v2:
- Use LOG*D.
- Pass a gc to apply_policy.
- Use 'r' for libxc return values.
--- tools/include/libxl.h | 6 +- tools/include/xenctrl.h | 26 ------ tools/include/xenguest.h | 4 - tools/libs/guest/xg_cpuid_x86.c | 125 -------------------------- tools/libs/light/libxl_cpuid.c | 142 ++++++++++++++++++++++++++++-- tools/libs/light/libxl_internal.h | 26 ++++++ 6 files changed, 165 insertions(+), 164 deletions(-)
tools/include/libxl.h +3 -3
@@ -1420,10 +1420,10 @@ void libxl_bitmap_init(libxl_bitmap *map);void libxl_bitmap_dispose (libxl_bitmap *map);/* -* libxl_cpuid_policy is opaque in the libxl ABI. Users of both libxl and -* libxc may not make assumptions about xc_xend_cpuid. +* libxl_cpuid_policy is opaque in the libxl ABI. Users of libxl may not make +* assumptions about libxl__cpuid_policy. */ -typedef struct xc_xend_cpuid libxl_cpuid_policy ; +typedef struct libxl__cpuid_policy libxl_cpuid_policy ;typedef libxl_cpuid_policy *libxl_cpuid_policy_list ;void libxl_cpuid_dispose (libxl_cpuid_policy_list *cpuid_list);int libxl_cpuid_policy_list_length (const libxl_cpuid_policy_list *l);
tools/include/xenctrl.h +0 -26
@@ -1803,32 +1803,6 @@ int xc_domain_debug_control(xc_interface *xch,#if defined(__i386__) || defined(__x86_64__) -/* -* CPUID policy data, expressed in the legacy XEND format. -* -* Policy is an array of strings, 32 chars long: -* policy[0] = eax -* policy[1] = ebx -* policy[2] = ecx -* policy[3] = edx -* -* The format of the string is the following: -* '1' -> force to 1 -* '0' -> force to 0 -* 'x' -> we don't care (use default) -* 'k' -> pass through host value -* 's' -> legacy alias for 'k' -*/ -struct xc_xend_cpuid { -union { -struct { -uint32_t leaf, subleaf; - }; -uint32_t input[2 ]; - }; -char *policy[4 ]; -}; -int xc_mca_op (xc_interface *xch,struct xen_mc *mc);int xc_mca_op_inject_v2 (xc_interface *xch,unsigned int flags,xc_cpumap_t cpumap,unsigned int nr_cpus);
tools/include/xenguest.h +0 -4
@@ -825,10 +825,6 @@ int xc_cpu_policy_make_compat_4_12(xc_interface *xch, xc_cpu_policy_t *policy,int xc_cpu_policy_legacy_topology (xc_interface *xch,xc_cpu_policy_t *policy,bool hvm); -/* Apply an xc_xend_cpuid object to the policy. */ -int xc_cpu_policy_apply_cpuid (xc_interface *xch,xc_cpu_policy_t *policy, -const struct xc_xend_cpuid *cpuid,bool hvm); -/* Apply a featureset to the policy. */ int xc_cpu_policy_apply_featureset(xc_interface *xch, xc_cpu_policy_t *policy, const uint32_t *featureset,
tools/libs/guest/xg_cpuid_x86.c +0 -125
131 lines not on this page · raw message
tools/libs/light/libxl_cpuid.c +136 -6
166 lines not on this page · raw message
tools/libs/light/libxl_internal.h +26 -0
@@ -2060,6 +2060,32 @@ typedef yajl_gen_status (*libxl__gen_json_callback)(yajl_gen hand, void *); _hidden char *libxl__object_to_json(libxl_ctx *ctx, const char *type,libxl__gen_json_callback gen,void *p); +/* +* CPUID policy data, expressed in the internal libxl format. +* +* Policy is an array of strings, 32 chars long: +* policy[0] = eax +* policy[1] = ebx +* policy[2] = ecx +* policy[3] = edx +* +* The format of the string is the following: +* '1' -> force to 1 +* '0' -> force to 0 +* 'x' -> we don't care (use default) +* 'k' -> pass through host value +* 's' -> legacy alias for 'k' +*/ +struct libxl__cpuid_policy { +union { +struct { +uint32_t leaf, subleaf; + }; +uint32_t input[2 ]; + }; +char *policy[4 ]; +}; + _hidden int libxl__cpuid_legacy(libxl_ctx *ctx, uint32_t domid, bool retore,libxl_domain_build_info *info);
Signature · 1 line
2.33.0
PATCH v5 12/12 x86/CPUID: shrink max_{,sub}leaf fields according to actual leaf contents
In reply to Roger Pau MonneFrom: Jan Beulich <[email protected]>
Zapping 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.
Note that such shrinking is only done when creating a policy for a domain from scratch. Migrated in domains keep their previous policy if present untouched, and for migrated in domains not having CPUID data the crafted Xen pre-4.13 policy is not trimmed to keep a behavior compatible with those older Xen versions.
---
Changes since v4:
- New in this version, picked up from 540d911c2813.
- Only shrink policies for newly created domains.
--- tools/include/xenguest.h | 3 + tools/libs/guest/xg_cpuid_x86.c | 5 ++ tools/libs/light/libxl_cpuid.c | 7 ++ tools/tests/cpu-policy/test-cpu-policy.c | 101 +++++++++++++++++++++++ xen/include/xen/lib/x86/cpuid.h | 7 ++ xen/lib/x86/cpuid.c | 39 +++++++++ 6 files changed, 162 insertions(+)
tools/include/xenguest.h +3 -0
@@ -830,6 +830,9 @@ int xc_cpu_policy_apply_featureset(xc_interface *xch, xc_cpu_policy_t *policy,const uint32_t *featureset, unsigned int nr_features); +/* Sanitize a policy: can change the contents of the passed policy. */ +void xc_cpu_policy_sanitize (xc_interface *xch,xc_cpu_policy_t *policy); +int xc_get_cpu_levelling_caps (xc_interface *xch,uint32_t *caps);int xc_get_cpu_featureset (xc_interface *xch,uint32_t index,uint32_t *nr_features,uint32_t *featureset);
tools/libs/guest/xg_cpuid_x86.c +5 -0
@@ -690,3 +690,8 @@ int xc_cpu_policy_apply_featureset(xc_interface *xch, xc_cpu_policy_t *policy,return 0 ; } + +void xc_cpu_policy_sanitize (xc_interface *xch,xc_cpu_policy_t *policy) +{ +x86_cpuid_policy_shrink_max_leaves (&policy->cpuid); +}
tools/libs/light/libxl_cpuid.c +7 -0
@@ -689,6 +689,13 @@ int libxl__cpuid_legacy(libxl_ctx *ctx, uint32_t domid, bool restore, goto out; } +/* +* Do not attempt any modifications if creating a policy that aims to be +* compatible with pre-4.13 Xen versions. +*/ +if (!restore) +xc_cpu_policy_sanitize (ctx->xch, policy); + r =xc_cpu_policy_set_domain (ctx->xch, domid, policy);if (r) {LOGED (ERROR , domid,"Failed to set domain CPUID policy" );
tools/tests/cpu-policy/test-cpu-policy.c +101 -0
123 lines not on this page · raw message
xen/include/xen/lib/x86/cpuid.h +7 -0
@@ -386,6 +386,13 @@ void x86_cpuid_policy_fill_native(struct cpuid_policy *p);*/ void x86_cpuid_policy_clear_out_of_range_leaves (struct cpuid_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_cpuid_policy_shrink_max_leaves (struct cpuid_policy *p); +#ifdef __XEN__#include <public/arch-x86/xen.h> typedef XEN_GUEST_HANDLE_64 (xen_cpuid_leaf_t)cpuid_leaf_buffer_t ;
xen/lib/x86/cpuid.c +39 -0
@@ -236,6 +236,45 @@ void x86_cpuid_policy_clear_out_of_range_leaves(struct cpuid_policy *p) ARRAY_SIZE(p->extd.raw) - 1); } +void x86_cpuid_policy_shrink_max_leaves (struct cpuid_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 =0x80000000 | i; +else + p->extd.max_leaf =0 ; +} +const uint32_t *x86_cpuid_lookup_deep_deps (uint32_t feature) {static const uint32_t deep_features[] =INIT_DEEP_FEATURES ;
Signature · 1 line
2.33.0