libs/guest: new CPUID/MSR interface
v6 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 | 645 ++++++++--------------- 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, 946 insertions(+), 605 deletions(-)
Signature · 1 line
2.34.1
PATCH v6 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.34.1
Re: [PATCH v6 01/12] libs/guest: move cpu policy related prototypes to xenguest.h
Quoted 11 lines · Roger Pau Monne
Do this before adding any more stuff to
xg_cpuid_x86.c.The placement in
xenctrl.his wrong, as they are implemented by the xenguest library. Note thatxg_cpuid_x86.cneeds to includexg_private.h, and in turn also fixxg_private.hto includexc_bitops.h. The bitops definition ofBITS_PER_LONGneeds to be changed to not be an expression, so thatxxhash.hcan use it in a preprocessor if directive.As a result also modify xen-cpuid and the ocaml stubs to include
xenguest.h.
Adding xenguest.h to ocaml stub has been done so it isn't part of this patch anymore ;-).
Quoted 3 lines
Reported-by Andrew Cooper <[email protected]>Signed-off-by Roger Pau Monné <[email protected]>Acked-by Andrew Cooper <[email protected]>
Thanks,
Signature · 1 line
Anthony PERARD
Re: [PATCH v6 01/12] libs/guest: move cpu policy related prototypes to xenguest.h
Quoted 10 lines · Anthony PERARD
Quoted 9 lines · Roger Pau Monne
Do this before adding any more stuff to
xg_cpuid_x86.c.The placement in
xenctrl.his wrong, as they are implemented by the xenguest library. Note thatxg_cpuid_x86.cneeds to includexg_private.h, and in turn also fixxg_private.hto includexc_bitops.h. The bitops definition ofBITS_PER_LONGneeds to be changed to not be an expression, so thatxxhash.hcan use it in a preprocessor if directive.
Quoted 5 lines
Quoted 2 lines
As a result also modify xen-cpuid and the ocaml stubs to include
xenguest.h.Adding
xenguest.hto ocaml stub has been done so it isn't part of this patch anymore ;-).
I assume it was removed on rebase and I didn't even realize.
Quoted 5 lines
Quoted 3 lines
Reported-by Andrew Cooper <[email protected]>Signed-off-by Roger Pau Monné <[email protected]>Acked-by Andrew Cooper <[email protected]>Acked-by Anthony PERARD <[email protected]>
Thanks.
PATCH v6 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.34.1
Re: [PATCH v6 02/12] libx86: introduce helper to fetch cpuid leaf
Quoted 6 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.
Quoted 8 lines
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_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.
Filling an empty policy from scratch will be fine, because we always ascend through leaves. This also matches the chronology of how CPUID developed.
The most likely case to go wrong is enabling an optional feature above max_leaf, and getting the bump to max_leaf out of order. That said, I suspect such logic would be working on an object, rather than a list.
The important point is that x86_cpuid_copy_from_buffer() is deliberately invariant to the order of entries for compatibility reasons, even if we don't expect it to matter in practice.
Quoted 8 lines
Suggested-by Andrew Cooper <[email protected]>Signed-off-by Roger Pau Monné <[email protected]>Reviewed-by Jan Beulich <[email protected]>---
Changes since v4:
- Rename_x86_cpuid_get_leaftox86_cpuid_get_leaf_const.
Quoted 7 lines
Changes since v3:
- New in this version.
---
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.
It's a bit of a weird construct, and both GCC and Clang could generate better code, but it does look to be safe.
Quoted 73 lines
--- 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 +58
-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;
tools/tests/cpu-policy/test-cpu-policy.c static void test_cpuid_out_of_range_clearing(void) · line 640
Quoted 10 lines
tools/tests/cpu-policy/test-cpu-policy.c +10
-0+ +/* 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];
memdup(). It is important for tests which potentially reach out of bounds, so ASAN can work.
That said, you're only testing half of the boundary cases. Perhaps more important important is the case where max_leaf is really out of legal bounds. Further, it is also important to check the non-NULL cases too.
It would probably be better to have a single cpuid_policy object, and a list of pointers (perhaps offsets) to interesting max_leaf fields, along with their relevant compile time bounds. That way, you can try all the interesting max_leaf values (0, limit-1, limit, ~0) and check NULL/non-NULLness of the answer with a simple min() calculation.
Quoted 14 lines
xen/include/xen/lib/x86/cpuid.h +6
-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.
xen/include/xen/lib/x86/cpuid.h int x86_cpuid_copy_from_buffer(struct cpuid_policy *policy, · line 449
Quoted 10 lines
xen/include/xen/lib/x86/cpuid.h +10
-0+* @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);
Examples like this demonstrate obviously why
const struct cpuid_leaf *x86_cpuid_get_leaf_const(
const struct cpuid_policy *p, uint32_t leaf, uint32_t subleaf);
is a better layout.
xen/include/xen/lib/x86/cpuid.h int x86_cpuid_copy_from_buffer(struct cpuid_policy *policy, · line 451
Quoted 2 lines
xen/include/xen/lib/x86/cpuid.h +2
-0+#define x86_cpuid_get_leaf (p, l, s) \ + ((__typeof__(&(p)->basic.raw[0]))x86_cpuid_get_leaf_const(p, l, s))
You can drop the outermost brackets.
~Andrew
Re: [PATCH v6 02/12] libx86: introduce helper to fetch cpuid leaf
Quoted 5 lines · Andrew Cooper
xen/include/xen/lib/x86/cpuid.h int x86_cpuid_copy_from_buffer(struct cpuid_policy *policy, · line 451
Quoted 2 lines · Roger Pau Monne
xen/include/xen/lib/x86/cpuid.h +2
-0+#define x86_cpuid_get_leaf (p, l, s) \ + ((__typeof__(&(p)->basic.raw[0]))x86_cpuid_get_leaf_const(p, l, s))You can drop the outermost brackets.
How that? While people aren't expected to write x86_cpuid_get_leaf(p, l, s)[0], such a case shouldn't result in misleading compiler diagnostics. Afaict omitting outermost parentheses is only ever safe on postfix expressions.
Jan
Re: [PATCH v6 02/12] libx86: introduce helper to fetch cpuid leaf
In reply to Andrew CooperQuoted 16 lines · Andrew Cooper
Quoted 6 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.Quoted 8 lines
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_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.
Quoted 3 lines
Filling an empty policy from scratch will be fine, because we always ascend through leaves. This also matches the chronology of how CPUID developed.
I'm slightly confused by this. The main point I wanted to make here is that x86_cpuid_copy_from_buffer cannot be switched to use x86_cpuid_get_leaf because the later relies on accessing the different maximum leaf/subleaf fields in the policy object: basic.max_leaf, feat.max_subleaf and extd.max_leaf.
That would for example make the existing test_cpuid_deserialise_failure hit a page fault, since it passes NULL as a policy object.
I don't really felt like changing test_cpuid_deserialise_failure to cope with the new behavior of x86_cpuid_copy_from_buffer if it was switched to use x86_cpuid_get_leaf.
Let me know if that's OK, or if you would like x86_cpuid_copy_from_buffer switched to use x86_cpuid_get_leaf and consequently have the callers also adjusted.
Quoted 105 lines
The most likely case to go wrong is enabling an optional feature above
max_leaf, and getting the bump tomax_leafout of order. That said, I suspect such logic would be working on an object, rather than a list.The important point is that
x86_cpuid_copy_from_buffer()is deliberately invariant to the order of entries for compatibility reasons, even if we don't expect it to matter in practice.Quoted 8 lines
Suggested-by Andrew Cooper <[email protected]>Signed-off-by Roger Pau Monné <[email protected]>Reviewed-by Jan Beulich <[email protected]>---
Changes since v4:
- Rename_x86_cpuid_get_leaftox86_cpuid_get_leaf_const.Quoted 7 lines
Changes since v3:
- New in this version.
---
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.It's a bit of a weird construct, and both GCC and Clang could generate better code, but it does look to be safe.
Quoted 78 lines
--- 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 +63
-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 ); +
Quoted 8 lines
tools/tests/cpu-policy/test-cpu-policy.c static void test_cpuid_out_of_range_clearing(void) · line 640
Quoted 5 lines
tools/tests/cpu-policy/test-cpu-policy.c +5
-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];memdup(). It is important for tests which potentially reach out of bounds, so ASAN can work.
Done.
Quoted 4 lines
That said, you're only testing half of the boundary cases. Perhaps more important important is the case where
max_leafis really out of legal bounds.
I was attempting to test this in the first two tests, by setting max_leaf = CPUID_GUEST_NR_BASIC, max_subleaf = CPUID_GUEST_NR_FEAT and extd.max_leaf also to it's max value (ie: out of legal bounds). I could set them to ~0 if that's clearer.
Quoted 7 lines
Further, it is also important to check the non-NULL cases too.
It would probably be better to have a single
cpuid_policyobject, and a list of pointers (perhaps offsets) to interestingmax_leaffields, along with their relevant compile time bounds. That way, you can try all the interestingmax_leafvalues (0, limit-1, limit, ~0) and check NULL/non-NULLness of the answer with a simplemin()calculation.
Then it would make sense to rename to test_cpuid_get_leaf_boundary.
I'm not sure I get the part of having a single cpuid_policy object, as then we would have to go changing it's contents in order to do the different tests rather than just having a const one for each single test that's already setup as expected. Also I'm confused at the usage of min(), but that's likely because I don't really get the part of having a single cpuid_policy object.
I assume you would rather prefer your suggested testing to be implemented here rather than in a follow up patch?
Quoted 27 lines
Quoted 14 lines
xen/include/xen/lib/x86/cpuid.h +6
-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. xen/include/xen/lib/x86/cpuid.h int x86_cpuid_copy_from_buffer(struct cpuid_policy *policy, · line 449
Quoted 10 lines
xen/include/xen/lib/x86/cpuid.h +10
-0+* @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);Examples like this demonstrate obviously why
Quoted 4 lines
const struct
cpuid_leaf*x86_cpuid_get_leaf_const(
const structcpuid_policy*p,uint32_tleaf,uint32_tsubleaf);is a better layout.
I don't care that much in this case TBH. One way or another this is the style used currently on the file, so it's likely better to keep it that way.
Thanks, Roger.
PATCH v6 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.
---
Changes since v5:
- Zero out parameter.
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 | 25 +++++++++++++++++++++++++ 2 files changed, 28 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 +25 -0
@@ -855,6 +855,31 @@ 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; + + *out = (xen_cpuid_leaf_t ){}; + + 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.34.1
Re: [PATCH v6 03/12] libs/guest: allow fetching a specific CPUID leaf from a cpu policy
Quoted 9 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.
Signed-off-by Roger Pau Monné <[email protected]>
with a nit:
Quoted 20 lines
tools/libs/guest/xg_cpuid_x86.c +14
-0@@ -855,6 +855,31 @@ 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; + + *out = (xen_cpuid_leaf_t ){}; + + tmp =x86_cpuid_get_leaf (&policy->cpuid, leaf, subleaf); +if ( !tmp ) + { +/* Unable to find a matching leaf. */ + errno =ENOENT ; +return -1 ;
tools/libs/guest/xg_cpuid_x86.c int xc_cpu_policy_update_msrs(xc_interface *xch, xc_cpu_policy_t *policy, · line 881
Quoted 10 lines
tools/libs/guest/xg_cpuid_x86.c +10
-0+ } + + out->leaf = leaf; + out->subleaf = subleaf; + out->a = tmp->a; + out->b = tmp->b; + out->c = tmp->c; + out->d = tmp->d; +return 0 ; +}
Hypervisor style, which aiui also applies to libxc, would prefer a blank line before the main "return" of a function.
Jan
PATCH v6 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.34.1
PATCH v6 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
@@ -880,6 +880,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.34.1
Re: [PATCH v6 05/12] libs/guest: allow fetching a specific MSR entry from a cpu policy
Quoted 9 lines · Roger Pau Monne
Introduce an interface that returns a specific MSR entry from a cpu policy in
xen_msr_entry_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.
Signed-off-by Roger Pau Monné <[email protected]>
PATCH v6 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.34.1
PATCH v6 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.
---
Changes since v5:
- Keep using the host featureset.
- Fix copied comment typo.
Changes since v4:
- s/xc_cpu_policy_topology/xc_cpu_policy_legacy_topology/
--- tools/include/xenguest.h | 4 + tools/libs/guest/xg_cpuid_x86.c | 182 +++++++++++++++++--------------- 2 files changed, 101 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 +97 -85
207 lines not on this page · raw message
Signature · 1 line
2.34.1
PATCH v6 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.34.1
Re: [PATCH v6 08/12] libs/guest: rework xc_cpuid_xend_policy
Quoted 1 line · Roger Pau Monne
Rename
xc_cpuid_xend_policytoxc_cpu_policy_apply_cpuid
I'm not convinced by this name. 'xend' is important because it's the format of the data passed, which 'cpuid' is not.
It is a horribly inefficient format, and really doesn't want to survive cleanup to the internals of libxl.
Quoted 125 lines
tools/libs/guest/xg_cpuid_x86.c +16
-81@@ -254,144 +254,107 @@ int xc_set_domain_cpu_policy(xc_interface *xch, uint32_t domid,return ret; } -static int compare_leaves (const void *l,const void *r) -{ -const xen_cpuid_leaf_t *lhs = l; -const xen_cpuid_leaf_t *rhs = r; - -if ( lhs->leaf != rhs->leaf ) -return lhs->leaf < rhs->leaf ?-1 :1 ; - -if ( lhs->subleaf != rhs->subleaf ) -return lhs->subleaf < rhs->subleaf ?-1 :1 ; - -return 0 ; -} - -static xen_cpuid_leaf_t *find_leaf ( -xen_cpuid_leaf_t *leaves,unsigned int nr_leaves, -const struct xc_xend_cpuid *xend) -{ -const xen_cpuid_leaf_t key = { xend->leaf, xend->subleaf }; - -return bsearch (&key, leaves, nr_leaves,sizeof (*leaves), compare_leaves); -} - -static int xc_cpuid_xend_policy ( -xc_interface *xch,uint32_t domid,const struct xc_xend_cpuid *xend) +int xc_cpu_policy_apply_cpuid (xc_interface *xch,xc_cpu_policy_t *policy, +const struct xc_xend_cpuid *cpuid,bool hvm) {int rc; -xc_dominfo_t di; -unsigned int nr_leaves, nr_msrs; -uint32_t err_leaf =-1 , err_subleaf =-1 , err_msr =-1 ; -/* -* Three full policies. The host, default for the domain type, -* and domain current. -*/ -xen_cpuid_leaf_t *host =NULL , *def =NULL , *cur =NULL ; -unsigned int nr_host, nr_def, nr_cur; +xc_cpu_policy_t *host =NULL , *def =NULL ; -if (xc_domain_getinfo (xch, domid,1 , &di) !=1 || - di.domid != domid ) - { -ERROR ("Failed to obtain d%d info" , domid); - rc = -ESRCH ; - goto fail; - } - - rc =xc_cpu_policy_get_size (xch, &nr_leaves, &nr_msrs); -if ( rc ) - { -PERROR ("Failed to obtain policy info size" ); - rc = -errno; - goto fail; - } - - rc = -ENOMEM ; -if ( (host =calloc (nr_leaves,sizeof (*host))) ==NULL || - (def =calloc (nr_leaves,sizeof (*def))) ==NULL || - (cur =calloc (nr_leaves,sizeof (*cur))) ==NULL ) - { -ERROR ("Unable to allocate memory for %u CPUID leaves" , nr_leaves); - goto fail; - } - -/* Get the domain's current policy. */ - nr_msrs =0 ; - nr_cur = nr_leaves; - rc =get_domain_cpu_policy (xch, domid, &nr_cur, cur, &nr_msrs,NULL ); -if ( rc ) + host =xc_cpu_policy_init (); + def =xc_cpu_policy_init (); +if ( !host || !def ) { -PERROR ("Failed to obtain d%d current policy" , domid); - rc = -errno; - goto fail; +PERROR ("Failed to init policies" ); + rc = -ENOMEM ; + goto out; }/* Get the domain type's default policy. */ - nr_msrs =0 ; - nr_def = nr_leaves; - rc =get_system_cpu_policy (xch, di.hvm ? XEN_SYSCTL_cpu_policy_hvm_default + rc =xc_cpu_policy_get_system (xch, hvm ? XEN_SYSCTL_cpu_policy_hvm_default : XEN_SYSCTL_cpu_policy_pv_default, - &nr_def, def, &nr_msrs,NULL ); + def);if ( rc ) { -PERROR ("Failed to obtain %s def policy" , di.hvm ?"hvm" :"pv" ); - rc = -errno; - goto fail; +PERROR ("Failed to obtain %s def policy" , hvm ?"hvm" :"pv" ); + goto out; }/* Get the host policy. */ - nr_msrs =0 ; - nr_host = nr_leaves; - rc =get_system_cpu_policy (xch, XEN_SYSCTL_cpu_policy_host, - &nr_host, host, &nr_msrs,NULL ); + rc =xc_cpu_policy_get_system (xch, XEN_SYSCTL_cpu_policy_host, host);if ( rc ) {PERROR ("Failed to obtain host policy" ); - rc = -errno; - goto fail; + goto out; } rc = -EINVAL ; -for ( ; xend->leaf !=XEN_CPUID_INPUT_UNUSED ; ++xend ) +for ( ; cpuid->leaf !=XEN_CPUID_INPUT_UNUSED ; ++cpuid ) {
tools/libs/guest/xg_cpuid_x86.c int xc_set_domain_cpu_policy(xc_interface *xch, uint32_t domid, · line 298
Quoted 10 lines
tools/libs/guest/xg_cpuid_x86.c +5
-4-xen_cpuid_leaf_t *cur_leaf =find_leaf (cur, nr_cur, xend); -const xen_cpuid_leaf_t *def_leaf =find_leaf (def, nr_def, xend); -const xen_cpuid_leaf_t *host_leaf =find_leaf (host, nr_host, xend); +xen_cpuid_leaf_t cur_leaf; +xen_cpuid_leaf_t def_leaf; +xen_cpuid_leaf_t host_leaf; -if ( cur_leaf ==NULL || def_leaf ==NULL || host_leaf ==NULL ) + rc =xc_cpu_policy_get_cpuid (xch, policy, cpuid->leaf, cpuid->subleaf, + &cur_leaf);
This is a crazy increase in data shuffling.
Use x86_cpuid_get_leaf() from patch 2, which *is* find_leaf() for objects rather than lists, and removes the need to further-shuffle the data later now that you're not modifying cur in place.
It also removes the churn introduced by changing the indirection of these variables.
It also removes all callers of xc_cpu_policy_get_cpuid(), which don't have an obvious purpose to begin with. Relatedly, xc_cpu_policy_get_msr() has no users at all, that I can see.
~Andrew
Re: [PATCH v6 08/12] libs/guest: rework xc_cpuid_xend_policy
Quoted 5 lines · Andrew Cooper
Quoted 1 line · Roger Pau Monne
Rename
xc_cpuid_xend_policytoxc_cpu_policy_apply_cpuidI'm not convinced by this name. 'xend' is important because it's the format of the data passed, which 'cpuid' is not.
The format would be the libxc format now. Having xend here is a layering violation IMO.
Note this function goes away in patch 11, so I'm unsure there's a lot of value in discussing over the name of a function that's about to disappear.
Quoted 2 lines
It is a horribly inefficient format, and really doesn't want to survive cleanup to the internals of libxl.
Even when moved to the internals of libxl the format is not exposed to users of libxl (it's a libxl__cpuid_policy in libxl_internal.h), so we are free to modify it at our own will. I would defer the changes to the format to a separate series, there's enough churn here already. My aim was to provide a new interface while keeping the functional changes to a minimum.
Quoted 138 lines
Quoted 125 lines
tools/libs/guest/xg_cpuid_x86.c +16
-81@@ -254,144 +254,107 @@ int xc_set_domain_cpu_policy(xc_interface *xch, uint32_t domid,return ret; } -static int compare_leaves (const void *l,const void *r) -{ -const xen_cpuid_leaf_t *lhs = l; -const xen_cpuid_leaf_t *rhs = r; - -if ( lhs->leaf != rhs->leaf ) -return lhs->leaf < rhs->leaf ?-1 :1 ; - -if ( lhs->subleaf != rhs->subleaf ) -return lhs->subleaf < rhs->subleaf ?-1 :1 ; - -return 0 ; -} - -static xen_cpuid_leaf_t *find_leaf ( -xen_cpuid_leaf_t *leaves,unsigned int nr_leaves, -const struct xc_xend_cpuid *xend) -{ -const xen_cpuid_leaf_t key = { xend->leaf, xend->subleaf }; - -return bsearch (&key, leaves, nr_leaves,sizeof (*leaves), compare_leaves); -} - -static int xc_cpuid_xend_policy ( -xc_interface *xch,uint32_t domid,const struct xc_xend_cpuid *xend) +int xc_cpu_policy_apply_cpuid (xc_interface *xch,xc_cpu_policy_t *policy, +const struct xc_xend_cpuid *cpuid,bool hvm) {int rc; -xc_dominfo_t di; -unsigned int nr_leaves, nr_msrs; -uint32_t err_leaf =-1 , err_subleaf =-1 , err_msr =-1 ; -/* -* Three full policies. The host, default for the domain type, -* and domain current. -*/ -xen_cpuid_leaf_t *host =NULL , *def =NULL , *cur =NULL ; -unsigned int nr_host, nr_def, nr_cur; +xc_cpu_policy_t *host =NULL , *def =NULL ; -if (xc_domain_getinfo (xch, domid,1 , &di) !=1 || - di.domid != domid ) - { -ERROR ("Failed to obtain d%d info" , domid); - rc = -ESRCH ; - goto fail; - } - - rc =xc_cpu_policy_get_size (xch, &nr_leaves, &nr_msrs); -if ( rc ) - { -PERROR ("Failed to obtain policy info size" ); - rc = -errno; - goto fail; - } - - rc = -ENOMEM ; -if ( (host =calloc (nr_leaves,sizeof (*host))) ==NULL || - (def =calloc (nr_leaves,sizeof (*def))) ==NULL || - (cur =calloc (nr_leaves,sizeof (*cur))) ==NULL ) - { -ERROR ("Unable to allocate memory for %u CPUID leaves" , nr_leaves); - goto fail; - } - -/* Get the domain's current policy. */ - nr_msrs =0 ; - nr_cur = nr_leaves; - rc =get_domain_cpu_policy (xch, domid, &nr_cur, cur, &nr_msrs,NULL ); -if ( rc ) + host =xc_cpu_policy_init (); + def =xc_cpu_policy_init (); +if ( !host || !def ) { -PERROR ("Failed to obtain d%d current policy" , domid); - rc = -errno; - goto fail; +PERROR ("Failed to init policies" ); + rc = -ENOMEM ; + goto out; }/* Get the domain type's default policy. */ - nr_msrs =0 ; - nr_def = nr_leaves; - rc =get_system_cpu_policy (xch, di.hvm ? XEN_SYSCTL_cpu_policy_hvm_default + rc =xc_cpu_policy_get_system (xch, hvm ? XEN_SYSCTL_cpu_policy_hvm_default : XEN_SYSCTL_cpu_policy_pv_default, - &nr_def, def, &nr_msrs,NULL ); + def);if ( rc ) { -PERROR ("Failed to obtain %s def policy" , di.hvm ?"hvm" :"pv" ); - rc = -errno; - goto fail; +PERROR ("Failed to obtain %s def policy" , hvm ?"hvm" :"pv" ); + goto out; }/* Get the host policy. */ - nr_msrs =0 ; - nr_host = nr_leaves; - rc =get_system_cpu_policy (xch, XEN_SYSCTL_cpu_policy_host, - &nr_host, host, &nr_msrs,NULL ); + rc =xc_cpu_policy_get_system (xch, XEN_SYSCTL_cpu_policy_host, host);if ( rc ) {PERROR ("Failed to obtain host policy" ); - rc = -errno; - goto fail; + goto out; } rc = -EINVAL ; -for ( ; xend->leaf !=XEN_CPUID_INPUT_UNUSED ; ++xend ) +for ( ; cpuid->leaf !=XEN_CPUID_INPUT_UNUSED ; ++cpuid ) {tools/libs/guest/xg_cpuid_x86.c int xc_set_domain_cpu_policy(xc_interface *xch, uint32_t domid, · line 298
Quoted 10 lines
tools/libs/guest/xg_cpuid_x86.c +5
-4-xen_cpuid_leaf_t *cur_leaf =find_leaf (cur, nr_cur, xend); -const xen_cpuid_leaf_t *def_leaf =find_leaf (def, nr_def, xend); -const xen_cpuid_leaf_t *host_leaf =find_leaf (host, nr_host, xend); +xen_cpuid_leaf_t cur_leaf; +xen_cpuid_leaf_t def_leaf; +xen_cpuid_leaf_t host_leaf; -if ( cur_leaf ==NULL || def_leaf ==NULL || host_leaf ==NULL ) + rc =xc_cpu_policy_get_cpuid (xch, policy, cpuid->leaf, cpuid->subleaf, + &cur_leaf);This is a crazy increase in data shuffling.
Quoted 3 lines
Use
x86_cpuid_get_leaf()from patch 2, which *is*find_leaf()for objects rather than lists, and removes the need to further-shuffle the data later now that you're not modifying cur in place.
This function will get moved into libxl in patch 11, and then it would be a layering violation to call x86_cpuid_get_leaf, so it needs to use the xc wrapper.
Quoted 6 lines
It also removes the churn introduced by changing the indirection of these variables.
It also removes all callers of
xc_cpu_policy_get_cpuid(), which don't have an obvious purpose to begin with. Relatedly,xc_cpu_policy_get_msr()has no users at all, that I can see.
This was introduced in order to provide a sensible interface. It doesn't make sense to allow getting cpuid leafs but not MSRs from a policy IMO. I would expect other toolstacks, or libxl in the future to make use of it.
If you don't think that's enough justification we can leave that patch aside.
Thanks, Roger.
PATCH v6 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.34.1
PATCH v6 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.34.1
Re: [PATCH v6 10/12] libs/{light,guest}: implement xc_cpuid_apply_policy in libxl
Quoted 19 lines · Roger Pau Monne
tools/libs/light/libxl_cpuid.c +11
-0@@ -454,6 +456,41 @@ int libxl__cpuid_legacy(libxl_ctx *ctx, uint32_t domid, bool restore,*/ bool nested_virt = info->nested_hvm.val >0 ; + policy =xc_cpu_policy_init (); +if (!policy) { +LOGED (ERROR , domid,"Failed to init CPU policy" ); + rc =ERROR_FAIL ; + goto out; + } + + r =xc_cpu_policy_get_domain (ctx->xch, domid, policy); +if (r) { +LOGED (ERROR , domid,"Failed to fetch domain CPU policy" ); + rc =ERROR_FAIL ;
tools/libs/light/libxl_cpuid.c int libxl__cpuid_legacy(libxl_ctx *ctx, uint32_t domid, bool restore, · line 479
Quoted 10 lines
tools/libs/light/libxl_cpuid.c +10
-0+ goto out; + } + +if (restore) { +/* +* Make sure the policy is compatible with pre Xen 4.13. Note that +* newer Xen versions will pass policy data on the restore stream, so +* any adjustments done here will be superseded. +*/ + r =xc_cpu_policy_make_compat_4_12 (ctx->xch, policy, hvm);
One hidden host policy acquisition, and ...
tools/libs/light/libxl_cpuid.c int libxl__cpuid_legacy(libxl_ctx *ctx, uint32_t domid, bool restore, · line 487
Quoted 8 lines
tools/libs/light/libxl_cpuid.c +8
-0+if (r) { +LOGED (ERROR , domid,"Failed to setup compatible CPU policy" ); + rc =ERROR_FAIL ; + goto out; + } + } + + r =xc_cpu_policy_legacy_topology (ctx->xch, policy, hvm);
... one host featureset and ... (final comment)
Quoted 46 lines
tools/libs/light/libxl_cpuid.c +27
-4+if (r) { +LOGED (ERROR , domid,"Failed to setup CPU policy topology" ); + rc =ERROR_FAIL ; + goto out; + } +/* * For PV guests, PAE is Xen-controlled (it is the 'p' that differentiates * the xen-3.0-x86_32 and xen-3.0-x86_32p ABIs). It is mandatory as Xen @@ -466,6 +503,13 @@ int libxl__cpuid_legacy(libxl_ctx *ctx, uint32_t domid, bool restore,*/ if (info->type ==LIBXL_DOMAIN_TYPE_HVM ) pae =libxl_defbool_val (info->u.hvm.pae); + rc =libxl_cpuid_parse_config (&info->cpuid,GCSPRINTF ("pae=%d" , pae)); +if (rc) { +LOGD (ERROR , domid,"Failed to set PAE CPUID flag" ); + rc =ERROR_FAIL ; + goto out; + } +/* * Advertising Invariant TSC to a guest means that the TSC frequency won't @@ -481,14 +525,50 @@ int libxl__cpuid_legacy(libxl_ctx *ctx, uint32_t domid, bool restore,*/ itsc = (libxl_defbool_val (info->disable_migrate) || info->tsc_mode ==LIBXL_TSC_MODE_ALWAYS_EMULATE ); + rc =libxl_cpuid_parse_config (&info->cpuid,GCSPRINTF ("invtsc=%d" , itsc)); +if (rc) { +LOGD (ERROR , domid,"Failed to set Invariant TSC CPUID flag" ); + rc =ERROR_FAIL ; + goto out; + } - r =xc_cpuid_apply_policy (ctx->xch, domid, restore,NULL ,0 , - pae, itsc, nested_virt, info->cpuid); -if (r) -LOGEVD (ERROR , -r, domid,"Failed to apply CPUID policy" ); +/* Set Nested virt CPUID bits for HVM. */ +if (hvm) { + rc =libxl_cpuid_parse_config (&info->cpuid,GCSPRINTF ("vmx=%d" , + nested_virt)); +if (rc) { +LOGD (ERROR , domid,"Failed to set VMX CPUID flag" ); + rc =ERROR_FAIL ; + goto out;
tools/libs/light/libxl_cpuid.c int libxl__cpuid_legacy(libxl_ctx *ctx, uint32_t domid, bool restore, · line 552
Quoted 10 lines
tools/libs/light/libxl_cpuid.c +10
-0+ } + + rc =libxl_cpuid_parse_config (&info->cpuid,GCSPRINTF ("svm=%d" , + nested_virt)); +if (rc) { +LOGD (ERROR , domid,"Failed to set SVM CPUID flag" ); + rc =ERROR_FAIL ; + goto out; + } + }
libxl_cpuid_parse_config() is a large overhead, and cannot suffer errors because libxl crashes rather than failing memory allocations. The invasiveness would be reduced substantially by having:
str = GCSPRINTF("pae=%d,invtsc=%d", pae, itsc);
if ( hvm )
append GCSPRINTF("vmx=%d,svm=%d", nested_virt, nested_virt);
and a single call to libxl_cpuid_parse_config().
Depending on how much we care, these need inserting at the head of the info->cpuid list so they get processed in the same order as before.
tools/libs/light/libxl_cpuid.c int libxl__cpuid_legacy(libxl_ctx *ctx, uint32_t domid, bool restore, · line 554
Quoted 2 lines
tools/libs/light/libxl_cpuid.c +2
-0+ +/* Apply the bits from info->cpuid if any. */
'if any' is stale now.
tools/libs/light/libxl_cpuid.c int libxl__cpuid_legacy(libxl_ctx *ctx, uint32_t domid, bool restore, · line 555
Quoted 1 line
tools/libs/light/libxl_cpuid.c +1
-0+ r =xc_cpu_policy_apply_cpuid (ctx->xch, policy, info->cpuid, hvm);
... and both a host and default policy.
That is a lot of overhead added behind the scenes. It would be rather better to have this function obtain the host policy and pass it to all 3 helpers. Default policy is harder to judge, but it would avoid needing to pass an hvm boolean down into this helper.
~Andrew
Re: [PATCH v6 10/12] libs/{light,guest}: implement xc_cpuid_apply_policy in libxl
Quoted 120 lines · Andrew Cooper
Quoted 19 lines · Roger Pau Monne
tools/libs/light/libxl_cpuid.c +11
-0@@ -454,6 +456,41 @@ int libxl__cpuid_legacy(libxl_ctx *ctx, uint32_t domid, bool restore,*/ bool nested_virt = info->nested_hvm.val >0 ; + policy =xc_cpu_policy_init (); +if (!policy) { +LOGED (ERROR , domid,"Failed to init CPU policy" ); + rc =ERROR_FAIL ; + goto out; + } + + r =xc_cpu_policy_get_domain (ctx->xch, domid, policy); +if (r) { +LOGED (ERROR , domid,"Failed to fetch domain CPU policy" ); + rc =ERROR_FAIL ;tools/libs/light/libxl_cpuid.c int libxl__cpuid_legacy(libxl_ctx *ctx, uint32_t domid, bool restore, · line 479
Quoted 10 lines
tools/libs/light/libxl_cpuid.c +10
-0+ goto out; + } + +if (restore) { +/* +* Make sure the policy is compatible with pre Xen 4.13. Note that +* newer Xen versions will pass policy data on the restore stream, so +* any adjustments done here will be superseded. +*/ + r =xc_cpu_policy_make_compat_4_12 (ctx->xch, policy, hvm);One hidden host policy acquisition, and ...
tools/libs/light/libxl_cpuid.c int libxl__cpuid_legacy(libxl_ctx *ctx, uint32_t domid, bool restore, · line 487
Quoted 8 lines
tools/libs/light/libxl_cpuid.c +8
-0+if (r) { +LOGED (ERROR , domid,"Failed to setup compatible CPU policy" ); + rc =ERROR_FAIL ; + goto out; + } + } + + r =xc_cpu_policy_legacy_topology (ctx->xch, policy, hvm);... one host featureset and ... (final comment)
Quoted 46 lines
tools/libs/light/libxl_cpuid.c +27
-4+if (r) { +LOGED (ERROR , domid,"Failed to setup CPU policy topology" ); + rc =ERROR_FAIL ; + goto out; + } +/* * For PV guests, PAE is Xen-controlled (it is the 'p' that differentiates * the xen-3.0-x86_32 and xen-3.0-x86_32p ABIs). It is mandatory as Xen @@ -466,6 +503,13 @@ int libxl__cpuid_legacy(libxl_ctx *ctx, uint32_t domid, bool restore,*/ if (info->type ==LIBXL_DOMAIN_TYPE_HVM ) pae =libxl_defbool_val (info->u.hvm.pae); + rc =libxl_cpuid_parse_config (&info->cpuid,GCSPRINTF ("pae=%d" , pae)); +if (rc) { +LOGD (ERROR , domid,"Failed to set PAE CPUID flag" ); + rc =ERROR_FAIL ; + goto out; + } +/* * Advertising Invariant TSC to a guest means that the TSC frequency won't @@ -481,14 +525,50 @@ int libxl__cpuid_legacy(libxl_ctx *ctx, uint32_t domid, bool restore,*/ itsc = (libxl_defbool_val (info->disable_migrate) || info->tsc_mode ==LIBXL_TSC_MODE_ALWAYS_EMULATE ); + rc =libxl_cpuid_parse_config (&info->cpuid,GCSPRINTF ("invtsc=%d" , itsc)); +if (rc) { +LOGD (ERROR , domid,"Failed to set Invariant TSC CPUID flag" ); + rc =ERROR_FAIL ; + goto out; + } - r =xc_cpuid_apply_policy (ctx->xch, domid, restore,NULL ,0 , - pae, itsc, nested_virt, info->cpuid); -if (r) -LOGEVD (ERROR , -r, domid,"Failed to apply CPUID policy" ); +/* Set Nested virt CPUID bits for HVM. */ +if (hvm) { + rc =libxl_cpuid_parse_config (&info->cpuid,GCSPRINTF ("vmx=%d" , + nested_virt)); +if (rc) { +LOGD (ERROR , domid,"Failed to set VMX CPUID flag" ); + rc =ERROR_FAIL ; + goto out;tools/libs/light/libxl_cpuid.c int libxl__cpuid_legacy(libxl_ctx *ctx, uint32_t domid, bool restore, · line 552
Quoted 10 lines
tools/libs/light/libxl_cpuid.c +10
-0+ } + + rc =libxl_cpuid_parse_config (&info->cpuid,GCSPRINTF ("svm=%d" , + nested_virt)); +if (rc) { +LOGD (ERROR , domid,"Failed to set SVM CPUID flag" ); + rc =ERROR_FAIL ; + goto out; + } + }
libxl_cpuid_parse_config()is a large overhead, and cannot suffer errors because libxl crashes rather than failing memory allocations. The invasiveness would be reduced substantially by having:str =
GCSPRINTF("pae=%d,invtsc=%d", pae, itsc);
if ( hvm )
appendGCSPRINTF("vmx=%d,svm=%d", nested_virt, nested_virt);and a single call to
libxl_cpuid_parse_config().Depending on how much we care, these need inserting at the head of the
info->cpuidlist so they get processed in the same order as before.tools/libs/light/libxl_cpuid.c int libxl__cpuid_legacy(libxl_ctx *ctx, uint32_t domid, bool restore, · line 554
Quoted 2 lines
tools/libs/light/libxl_cpuid.c +2
-0+ +/* Apply the bits from info->cpuid if any. */ 'if any' is stale now.
Quoted 8 lines
tools/libs/light/libxl_cpuid.c int libxl__cpuid_legacy(libxl_ctx *ctx, uint32_t domid, bool restore, · line 555
Quoted 1 line
tools/libs/light/libxl_cpuid.c +1
-0+ r =xc_cpu_policy_apply_cpuid (ctx->xch, policy, info->cpuid, hvm);... and both a host and default policy.
That is a lot of overhead added behind the scenes. It would be rather better to have this function obtain the host policy and pass it to all 3 helpers. Default policy is harder to judge, but it would avoid needing to pass an
hvmboolean down into this helper.
I've fixed all the above, but haven't added a default policy parameter to xc_cpu_policy_apply_cpuid. Let me know how strongly you feel about that.
Thanks, Roger.
PATCH v6 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
@@ -2059,6 +2059,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.34.1
PATCH v6 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
@@ -688,3 +688,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.34.1