Skip to contentSkip to conversation

libs/guest: new CPUID/MSR interface

PATCH Superseded Roger Pau Monne 4 participants 38 messages Apr 30, 2021 – May 6, 2021

v3 of 7 Superseded Latest is v7 →

lore
Share & tools
lore
#0 lore raw

v3 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.

Thanks, Roger.

Roger Pau Monne (13):
libxl: don't ignore the return value from xc_cpuid_apply_policy libs/guest: allow fetching a specific CPUID leaf from a cpu policy libs/guest: allow fetching a specific MSR entry from a cpu policy libs/guest: allow updating a cpu policy CPUID data libs/guest: allow updating a cpu policy MSR data libs/guest: introduce helper to check cpu policy compatibility libs/guest: obtain a compatible cpu policy from two input ones 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/xen-tools/libs.h    |   5 +
 tools/include/xenctrl.h           |  75 ++-
 tools/libs/guest/Makefile         |   2 +-
 tools/libs/guest/xg_cpuid_x86.c   | 832 ++++++++++++++++--------------
 tools/libs/light/libxl_cpuid.c    | 231 ++++++++-
 tools/libs/light/libxl_create.c   |   5 +-
 tools/libs/light/libxl_dom.c      |   2 +-
 tools/libs/light/libxl_internal.h |  32 +-
 tools/libs/light/libxl_nocpuid.c  |   5 +-
 10 files changed, 744 insertions(+), 451 deletions(-)
Signature · 1 line
2.31.1
#1 lore raw

PATCH v3 1/13 libxl: don't ignore the return value from xc_cpuid_apply_policy

Also change libxl__cpuid_legacy to propagate the error from xc_cpuid_apply_policy into callers.

---
Changes since v2:
- Use 'r' for xc_cpuid_apply_policy return value.
- Use LOGEVD to print error message.

Changes since v1:
- Return ERROR_FAIL on error.

---
 tools/libs/light/libxl_cpuid.c    | 15 +++++++++++----
 tools/libs/light/libxl_create.c   |  5 +++--
 tools/libs/light/libxl_dom.c      |  2 +-
 tools/libs/light/libxl_internal.h |  4 ++--
 tools/libs/light/libxl_nocpuid.c  |  5 +++--
 5 files changed, 20 insertions(+), 11 deletions(-)
tools/libs/light/libxl_cpuid.c +11 -4
index f32c5d3a6f6..eb6feaa96d1 100644
@@ -426,11 +426,13 @@ int libxl_cpuid_parse_config_xend(libxl_cpuid_policy_list *cpuid,
     return 0;
 }
 
-void libxl__cpuid_legacy(libxl_ctx *ctx, uint32_t domid, bool restore,
-                         libxl_domain_build_info *info)
+int libxl__cpuid_legacy(libxl_ctx *ctx, uint32_t domid, bool restore,
+                        libxl_domain_build_info *info)
 {
+    GC_INIT(ctx);
     bool pae = true;
     bool itsc;
+    int r;
 
     /*
      * Gross hack.  Using libxl_defbool_val() here causes libvirt to crash in
@@ -469,8 +471,13 @@ void 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);
 
-    xc_cpuid_apply_policy(ctx->xch, domid, restore, NULL, 0,
-                          pae, itsc, nested_virt, info->cpuid);
+    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");
+
+    GC_FREE;
+    return r ? ERROR_FAIL : 0;
 }
 
 static const char *input_names[2] = { "leaf", "subleaf" };
tools/libs/light/libxl_create.c +3 -2
index 43e9ba9c634..e356b2106d4 100644
@@ -1434,6 +1434,7 @@ int libxl__srm_callout_callback_static_data_done(unsigned int missing,
 
     libxl_domain_config *d_config = dcs->guest_config;
     libxl_domain_build_info *info = &d_config->b_info;
+    int rc = 0;
 
     /*
      * CPUID/MSR information is not present in pre Xen-4.14 streams.
@@ -1443,9 +1444,9 @@ int libxl__srm_callout_callback_static_data_done(unsigned int missing,
      * stream doesn't contain any CPUID data.
      */
     if (missing & XGR_SDD_MISSING_CPUID)
-        libxl__cpuid_legacy(ctx, dcs->guest_domid, true, info);
+        rc = libxl__cpuid_legacy(ctx, dcs->guest_domid, true, info);
 
-    return 0;
+    return rc;
 }
 
 void libxl__srm_callout_callback_restore_results(xen_pfn_t store_mfn,
tools/libs/light/libxl_dom.c +1 -1
index 842a51c86cb..e9f58ee4b2b 100644
@@ -384,7 +384,7 @@ int libxl__build_pre(libxl__gc *gc, uint32_t domid,
      * being migrated-in/restored have CPUID handled during the
      * static_data_done() callback. */
     if (!state->restore)
-        libxl__cpuid_legacy(ctx, domid, false, info);
+        rc = libxl__cpuid_legacy(ctx, domid, false, info);
 
     return rc;
 }
tools/libs/light/libxl_internal.h +2 -2
index c6a4a187f5b..44a2f3c8fe3 100644
@@ -2052,8 +2052,8 @@ 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);
 
-_hidden void libxl__cpuid_legacy(libxl_ctx *ctx, uint32_t domid, bool retore,
-                                 libxl_domain_build_info *info);
+_hidden int libxl__cpuid_legacy(libxl_ctx *ctx, uint32_t domid, bool retore,
+                                libxl_domain_build_info *info);
 
 /* Calls poll() again - useful to check whether a signaled condition
  * is still true.  Cannot fail.  Returns currently-true revents. */
tools/libs/light/libxl_nocpuid.c +3 -2
index f47336565b9..0630959e760 100644
@@ -34,9 +34,10 @@ int libxl_cpuid_parse_config_xend(libxl_cpuid_policy_list *cpuid,
     return 0;
 }
 
-void libxl__cpuid_legacy(libxl_ctx *ctx, uint32_t domid, bool restore,
-                         libxl_domain_build_info *info)
+int libxl__cpuid_legacy(libxl_ctx *ctx, uint32_t domid, bool restore,
+                        libxl_domain_build_info *info)
 {
+    return 0;
 }
 
 yajl_gen_status libxl_cpuid_policy_list_gen_json(yajl_gen hand,
Signature · 1 line
2.31.1
Reviewed-by on v3 1/13
#2 lore raw

Re: [PATCH v3 01/13] libxl: don't ignore the return value from xc_cpuid_apply_policy

Quoted 5 lines · Roger Pau Monne

Also change libxl__cpuid_legacy to propagate the error from xc_cpuid_apply_policy into callers.

Quoted 8 lines

---
Changes since v2:
- Use 'r' for xc_cpuid_apply_policy return value.
- Use LOGEVD to print error message.

Changes since v1:
- Return ERROR_FAIL on error.
---

Thanks,

Signature · 1 line
Anthony PERARD
#3 lore raw

PATCH v3 2/13 libs/guest: allow fetching a specific CPUID leaf from a cpu policy

In reply to Roger Pau Monne

Introduce 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 v1:
- Use find leaf.

---
 tools/include/xenctrl.h         |  3 +++
 tools/libs/guest/xg_cpuid_x86.c | 38 ++++++++++++++++++++++++++++-----
 2 files changed, 36 insertions(+), 5 deletions(-)
tools/include/xenctrl.h +3 -0
index 27cec1b93ff..cbca7209e34 100644
@@ -2608,6 +2608,9 @@ int xc_cpu_policy_set_domain(xc_interface *xch, uint32_t domid,
 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_get_cpuid(xc_interface *xch, const xc_cpu_policy_t policy,
+                            uint32_t leaf, uint32_t subleaf,
+                            xen_cpuid_leaf_t *out);
 
 int xc_get_cpu_levelling_caps(xc_interface *xch, uint32_t *caps);
 int xc_get_cpu_featureset(xc_interface *xch, uint32_t index,
tools/libs/guest/xg_cpuid_x86.c +33 -5
index 0c9b3a960f0..de27826f415 100644
@@ -280,9 +280,9 @@ static int compare_leaves(const void *l, const void *r)
 
 static xen_cpuid_leaf_t *find_leaf(
     xen_cpuid_leaf_t *leaves, unsigned int nr_leaves,
-    const struct xc_xend_cpuid *xend)
+    unsigned int leaf, unsigned int subleaf)
 {
-    const xen_cpuid_leaf_t key = { xend->leaf, xend->subleaf };
+    const xen_cpuid_leaf_t key = { leaf, subleaf };
 
     return bsearch(&key, leaves, nr_leaves, sizeof(*leaves), compare_leaves);
 }
@@ -365,9 +365,12 @@ static int xc_cpuid_xend_policy(
     rc = -EINVAL;
     for ( ; xend->leaf != XEN_CPUID_INPUT_UNUSED; ++xend )
     {
-        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 = find_leaf(cur, nr_cur,
+                                               xend->leaf, xend->subleaf);
+        const xen_cpuid_leaf_t *def_leaf = find_leaf(def, nr_def,
+                                                     xend->leaf, xend->subleaf);
+        const xen_cpuid_leaf_t *host_leaf = find_leaf(host, nr_host, xend->leaf,
+                                                      xend->subleaf);
 
         if ( cur_leaf == NULL || def_leaf == NULL || host_leaf == NULL )
         {
@@ -822,3 +825,28 @@ int xc_cpu_policy_serialise(xc_interface *xch, const xc_cpu_policy_t p,
     errno = 0;
     return 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)
+{
+    unsigned int nr_leaves = ARRAY_SIZE(policy->leaves);
+    xen_cpuid_leaf_t *tmp;
+    int rc;
+
+    rc = xc_cpu_policy_serialise(xch, policy, policy->leaves, &nr_leaves,
+                                 NULL, 0);
+    if ( rc )
+        return rc;
+
+    tmp = find_leaf(policy->leaves, nr_leaves, leaf, subleaf);
+    if ( !tmp )
+    {
+        /* Unable to find a matching leaf. */
+        errno = ENOENT;
+        return -1;
+    }
+
+    *out = *tmp;
+    return 0;
+}
Signature · 1 line
2.31.1
#4 lore raw

Re: [PATCH v3 02/13] libs/guest: allow fetching a specific CPUID leaf from a cpu policy

Quoted 7 lines · Roger Pau Monne
tools/libs/guest/xg_cpuid_x86.c +3 -0
@@ -822,3 +825,28 @@ int xc_cpu_policy_serialise(xc_interface *xch, const xc_cpu_policy_t p,
     errno = 0;
     return 0;
 }
+
+int xc_cpu_policy_get_cpuid(xc_interface *xch, const xc_cpu_policy_t policy,
+                            uint32_t leaf, uint32_t subleaf,

tools/libs/guest/xg_cpuid_x86.c int xc_cpu_policy_serialise(xc_interface *xch, const xc_cpu_policy_t p, · line 840

Quoted 10 lines
tools/libs/guest/xg_cpuid_x86.c +10 -0
+                            xen_cpuid_leaf_t *out)
+{
+    unsigned int nr_leaves = ARRAY_SIZE(policy->leaves);
+    xen_cpuid_leaf_t *tmp;
+    int rc;
+
+    rc = xc_cpu_policy_serialise(xch, policy, policy->leaves, &nr_leaves,
+                                 NULL, 0);
+    if ( rc )
+        return rc;

Sorry for not spotting this last time.

You don't need to serialise.  You can look up leaf/subleaf in O(1) time from cpuid_policy, which was a design goal of the structure originally.

It is probably best to adapt most of the first switch statement in guest_cpuid() to be a libx86 function.  The asserts aren't massively interesting to keep, and instead of messing around with nospec, just have the function return a pointer into the cpuid_policy (or NULL), and have a single block_speculation() in Xen.  We'll also want a unit test to go with this new function to check that out-of-range leaves don't result in out-of-bounds reads.

~Andrew

#5 lore raw

Re: [PATCH v3 02/13] libs/guest: allow fetching a specific CPUID leaf from a cpu policy

Quoted 21 lines · Andrew Cooper
Quoted 7 lines · Roger Pau Monne
tools/libs/guest/xg_cpuid_x86.c +3 -0
@@ -822,3 +825,28 @@ int xc_cpu_policy_serialise(xc_interface *xch, const xc_cpu_policy_t p,
     errno = 0;
     return 0;
 }
+
+int xc_cpu_policy_get_cpuid(xc_interface *xch, const xc_cpu_policy_t policy,
+                            uint32_t leaf, uint32_t subleaf,

tools/libs/guest/xg_cpuid_x86.c int xc_cpu_policy_serialise(xc_interface *xch, const xc_cpu_policy_t p, · line 840

Quoted 10 lines
tools/libs/guest/xg_cpuid_x86.c +10 -0
+                            xen_cpuid_leaf_t *out)
+{
+    unsigned int nr_leaves = ARRAY_SIZE(policy->leaves);
+    xen_cpuid_leaf_t *tmp;
+    int rc;
+
+    rc = xc_cpu_policy_serialise(xch, policy, policy->leaves, &nr_leaves,
+                                 NULL, 0);
+    if ( rc )
+        return rc;

Sorry for not spotting this last time.

Quoted 8 lines

You don't need to serialise.  You can look up leaf/subleaf in O(1) time from cpuid_policy, which was a design goal of the structure originally.

It is probably best to adapt most of the first switch statement in guest_cpuid() to be a libx86 function.  The asserts aren't massively interesting to keep, and instead of messing around with nospec, just have the function return a pointer into the cpuid_policy (or NULL), and have a single block_speculation() in Xen.

libx86 already has array_access_nospec, so I think it's fine to just leave the code as-is instead of adding a block_speculation in Xen and dropping the array_access_nospec accessors?

Quoted 3 lines

We'll also want a unit test
to go with this new function to check that out-of-range leaves don't result in out-of-bounds reads.

Sure.

Also, whats your opinion regarding xc_cpu_policy_get_msr, should I also split part of guest_rdmsr and place it in libx86 in order to fetch the MSRs present in msr_policy?

Thanks, Roger.

#6 lore raw

Re: [PATCH v3 02/13] libs/guest: allow fetching a specific CPUID leaf from a cpu policy

Quoted 24 lines · Roger Pau Monné
Quoted 23 lines · Andrew Cooper
Quoted 7 lines · Roger Pau Monne
tools/libs/guest/xg_cpuid_x86.c +3 -0
@@ -822,3 +825,28 @@ int xc_cpu_policy_serialise(xc_interface *xch, const xc_cpu_policy_t p,
     errno = 0;
     return 0;
 }
+
+int xc_cpu_policy_get_cpuid(xc_interface *xch, const xc_cpu_policy_t policy,
+                            uint32_t leaf, uint32_t subleaf,

tools/libs/guest/xg_cpuid_x86.c int xc_cpu_policy_serialise(xc_interface *xch, const xc_cpu_policy_t p, · line 840

Quoted 10 lines
tools/libs/guest/xg_cpuid_x86.c +10 -0
+                            xen_cpuid_leaf_t *out)
+{
+    unsigned int nr_leaves = ARRAY_SIZE(policy->leaves);
+    xen_cpuid_leaf_t *tmp;
+    int rc;
+
+    rc = xc_cpu_policy_serialise(xch, policy, policy->leaves, &nr_leaves,
+                                 NULL, 0);
+    if ( rc )
+        return rc;

Sorry for not spotting this last time.

You don't need to serialise.  You can look up leaf/subleaf in O(1) time from cpuid_policy, which was a design goal of the structure originally.

Quoted 8 lines
Quoted 5 lines

It is probably best to adapt most of the first switch statement in guest_cpuid() to be a libx86 function.  The asserts aren't massively interesting to keep, and instead of messing around with nospec, just have the function return a pointer into the cpuid_policy (or NULL), and have a single block_speculation() in Xen.

libx86 already has array_access_nospec, so I think it's fine to just leave the code as-is instead of adding a block_speculation in Xen and dropping the array_access_nospec accessors?

The same libx86 function should be used to simplify x86_cpuid_copy_from_buffer(), which has a similar opencoded construct for looking up the leaf/subleaf.

You might need some macro trickery to make a const and non-const versions, or have the main version non-const, and a const-qualified inline helper which casts away constness on the input but replaces it on the output.

The new code can't use array_access_nospec() because it is no longer accessing the array - merely returning a pointer.  array_index_nospec() might be an acceptable alternative.

Quoted 8 lines
Quoted 3 lines

We'll also want a unit test
to go with this new function to check that out-of-range leaves don't result in out-of-bounds reads.

Sure.

Also, whats your opinion regarding xc_cpu_policy_get_msr, should I also split part of guest_rdmsr and place it in libx86 in order to fetch the MSRs present in msr_policy?

That's harder to say.  I'd like to avoid the serialise call, but the current msr_policy structure currently uses uint32_t for space reasons, so you can't just create a uint64_t pointer to it.

Perhaps we should bite the bullet and use uint64_t unilaterally, so we can create a lookup_msr_by_index() or equiv.  The next big block of MSRs going into the policy are the VT-x ones, and they'll need to be 64 bits wide.

~Andrew

#7 lore raw

PATCH v3 3/13 libs/guest: allow fetching a specific MSR entry from a cpu policy

In reply to Roger Pau Monne

Introduce an interface that returns a specific MSR entry from a cpu policy in xen_msr_entry_t format. Provide a helper to perform a binary search against an array of MSR entries.

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 v1:
- Introduce a helper to perform a binary search of the MSR entries array.

---
 tools/include/xenctrl.h         |  2 ++
 tools/libs/guest/xg_cpuid_x86.c | 42 +++++++++++++++++++++++++++++++++
 2 files changed, 44 insertions(+)
tools/include/xenctrl.h +2 -0
index cbca7209e34..605c632cf30 100644
@@ -2611,6 +2611,8 @@ int xc_cpu_policy_serialise(xc_interface *xch, const 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);
 
 int xc_get_cpu_levelling_caps(xc_interface *xch, uint32_t *caps);
 int xc_get_cpu_featureset(xc_interface *xch, uint32_t index,
tools/libs/guest/xg_cpuid_x86.c +42 -0

45 lines not on this page · raw message

Signature · 1 line
2.31.1
#8 lore raw

Re: [PATCH v3 03/13] libs/guest: allow fetching a specific MSR entry from a cpu policy

Quoted 48 lines · Roger Pau Monne

Introduce an interface that returns a specific MSR entry from a cpu policy in xen_msr_entry_t format. Provide a helper to perform a binary search against an array of MSR entries.

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 v1:
- Introduce a helper to perform a binary search of the MSR entries array.

---
 tools/include/xenctrl.h         |  2 ++
 tools/libs/guest/xg_cpuid_x86.c | 42 +++++++++++++++++++++++++++++++++
 2 files changed, 44 insertions(+)
tools/include/xenctrl.h +2 -0
index cbca7209e34..605c632cf30 100644
@@ -2611,6 +2611,8 @@ int xc_cpu_policy_serialise(xc_interface *xch, const 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);
 
 int xc_get_cpu_levelling_caps(xc_interface *xch, uint32_t *caps);
 int xc_get_cpu_featureset(xc_interface *xch, uint32_t index,
tools/libs/guest/xg_cpuid_x86.c +8 -0
index de27826f415..9e83daca0e6 100644
@@ -850,3 +850,45 @@ int xc_cpu_policy_get_cpuid(xc_interface *xch, const xc_cpu_policy_t policy,
     *out = *tmp;
     return 0;
 }
+
+static int compare_entries(const void *l, const void *r)
+{
+    const xen_msr_entry_t *lhs = l;
+    const xen_msr_entry_t *rhs = r;
+
+    if ( lhs->idx == rhs->idx )
+        return 0;

tools/libs/guest/xg_cpuid_x86.c int xc_cpu_policy_get_cpuid(xc_interface *xch, const xc_cpu_policy_t policy, · line 870

Quoted 10 lines
tools/libs/guest/xg_cpuid_x86.c +10 -0
+    return lhs->idx < rhs->idx ? -1 : 1;
+}
+
+static xen_msr_entry_t *find_entry(xen_msr_entry_t *entries,
+                                   unsigned int nr_entries, unsigned int index)
+{
+    const xen_msr_entry_t key = { index };
+
+    return bsearch(&key, entries, nr_entries, sizeof(*entries), compare_entries);
+}

Isn't "entries" / "entry" a little too generic a name here, considering the CPUID equivalents use "leaves" / "leaf"? (Noticed really while looking at patch 7.)

Jan

#9 lore raw

Re: [PATCH v3 03/13] libs/guest: allow fetching a specific MSR entry from a cpu policy

Quoted 55 lines · Jan Beulich
Quoted 54 lines · Roger Pau Monne

Introduce an interface that returns a specific MSR entry from a cpu policy in xen_msr_entry_t format. Provide a helper to perform a binary search against an array of MSR entries.

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 v1:
- Introduce a helper to perform a binary search of the MSR entries array.

---
 tools/include/xenctrl.h         |  2 ++
 tools/libs/guest/xg_cpuid_x86.c | 42 +++++++++++++++++++++++++++++++++
 2 files changed, 44 insertions(+)
tools/include/xenctrl.h +2 -0
index cbca7209e34..605c632cf30 100644
@@ -2611,6 +2611,8 @@ int xc_cpu_policy_serialise(xc_interface *xch, const 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);
 
 int xc_get_cpu_levelling_caps(xc_interface *xch, uint32_t *caps);
 int xc_get_cpu_featureset(xc_interface *xch, uint32_t index,
tools/libs/guest/xg_cpuid_x86.c +14 -0
index de27826f415..9e83daca0e6 100644
@@ -850,3 +850,45 @@ int xc_cpu_policy_get_cpuid(xc_interface *xch, const xc_cpu_policy_t policy,
     *out = *tmp;
     return 0;
 }
+
+static int compare_entries(const void *l, const void *r)
+{
+    const xen_msr_entry_t *lhs = l;
+    const xen_msr_entry_t *rhs = r;
+
+    if ( lhs->idx == rhs->idx )
+        return 0;
+    return lhs->idx < rhs->idx ? -1 : 1;
+}
+
+static xen_msr_entry_t *find_entry(xen_msr_entry_t *entries,
+                                   unsigned int nr_entries, unsigned int index)
+{
Quoted 8 lines

tools/libs/guest/xg_cpuid_x86.c int xc_cpu_policy_get_cpuid(xc_interface *xch, const xc_cpu_policy_t policy, · line 870

Quoted 4 lines
tools/libs/guest/xg_cpuid_x86.c +4 -0
+    const xen_msr_entry_t key = { index };
+
+    return bsearch(&key, entries, nr_entries, sizeof(*entries), compare_entries);
+}

Isn't "entries" / "entry" a little too generic a name here, considering the CPUID equivalents use "leaves" / "leaf"? (Noticed really while looking at patch 7.)

Would you be fine with naming the function find_msr and leaving the rest of the parameters names as-is?

Thanks, Roger.

#10 lore raw

Re: [PATCH v3 03/13] libs/guest: allow fetching a specific MSR entry from a cpu policy

Quoted 31 lines · Roger Pau Monné
Quoted 21 lines · Jan Beulich
Quoted 20 lines · Roger Pau Monne
tools/libs/guest/xg_cpuid_x86.c +14 -0
@@ -850,3 +850,45 @@ int xc_cpu_policy_get_cpuid(xc_interface *xch, const xc_cpu_policy_t policy,
     *out = *tmp;
     return 0;
 }
+
+static int compare_entries(const void *l, const void *r)
+{
+    const xen_msr_entry_t *lhs = l;
+    const xen_msr_entry_t *rhs = r;
+
+    if ( lhs->idx == rhs->idx )
+        return 0;
+    return lhs->idx < rhs->idx ? -1 : 1;
+}
+
+static xen_msr_entry_t *find_entry(xen_msr_entry_t *entries,
+                                   unsigned int nr_entries, unsigned int index)
+{
Quoted 8 lines

tools/libs/guest/xg_cpuid_x86.c int xc_cpu_policy_get_cpuid(xc_interface *xch, const xc_cpu_policy_t policy, · line 870

Quoted 4 lines
tools/libs/guest/xg_cpuid_x86.c +4 -0
+    const xen_msr_entry_t key = { index };
+
+    return bsearch(&key, entries, nr_entries, sizeof(*entries), compare_entries);
+}

Isn't "entries" / "entry" a little too generic a name here, considering the CPUID equivalents use "leaves" / "leaf"? (Noticed really while looking at patch 7.)

Quoted 2 lines

Would you be fine with naming the function find_msr and leaving the rest of the parameters names as-is?

Yes. But recall I'm not the maintainer of this code anyway.

Jan

#11 lore raw

Re: [PATCH v3 03/13] libs/guest: allow fetching a specific MSR entry from a cpu policy

Quoted 32 lines · Jan Beulich
Quoted 31 lines · Roger Pau Monné
Quoted 29 lines · Jan Beulich
Quoted 14 lines · Roger Pau Monne
tools/libs/guest/xg_cpuid_x86.c +8 -0
@@ -850,3 +850,45 @@ int xc_cpu_policy_get_cpuid(xc_interface *xch, const xc_cpu_policy_t policy,
     *out = *tmp;
     return 0;
 }
+
+static int compare_entries(const void *l, const void *r)
+{
+    const xen_msr_entry_t *lhs = l;
+    const xen_msr_entry_t *rhs = r;
+
+    if ( lhs->idx == rhs->idx )
+        return 0;

tools/libs/guest/xg_cpuid_x86.c int xc_cpu_policy_get_cpuid(xc_interface *xch, const xc_cpu_policy_t policy, · line 870

Quoted 10 lines
tools/libs/guest/xg_cpuid_x86.c +10 -0
+    return lhs->idx < rhs->idx ? -1 : 1;
+}
+
+static xen_msr_entry_t *find_entry(xen_msr_entry_t *entries,
+                                   unsigned int nr_entries, unsigned int index)
+{
+    const xen_msr_entry_t key = { index };
+
+    return bsearch(&key, entries, nr_entries, sizeof(*entries), compare_entries);
+}

Isn't "entries" / "entry" a little too generic a name here, considering the CPUID equivalents use "leaves" / "leaf"? (Noticed really while looking at patch 7.)

Quoted 4 lines
Quoted 2 lines

Would you be fine with naming the function find_msr and leaving the rest of the parameters names as-is?

Yes. But recall I'm not the maintainer of this code anyway.

You cared to provide feedback, and I'm happy to make the change.

Thanks, Roger.

#12 lore raw

Re: [PATCH v3 03/13] libs/guest: allow fetching a specific MSR entry from a cpu policy

Quoted 31 lines · Roger Pau Monné
Quoted 30 lines · Jan Beulich
Quoted 29 lines · Roger Pau Monné
Quoted 28 lines · Jan Beulich
Quoted 14 lines · Roger Pau Monne
tools/libs/guest/xg_cpuid_x86.c +8 -0
@@ -850,3 +850,45 @@ int xc_cpu_policy_get_cpuid(xc_interface *xch, const xc_cpu_policy_t policy,
     *out = *tmp;
     return 0;
 }
+
+static int compare_entries(const void *l, const void *r)
+{
+    const xen_msr_entry_t *lhs = l;
+    const xen_msr_entry_t *rhs = r;
+
+    if ( lhs->idx == rhs->idx )
+        return 0;

tools/libs/guest/xg_cpuid_x86.c int xc_cpu_policy_get_cpuid(xc_interface *xch, const xc_cpu_policy_t policy, · line 870

Quoted 10 lines
tools/libs/guest/xg_cpuid_x86.c +10 -0
+    return lhs->idx < rhs->idx ? -1 : 1;
+}
+
+static xen_msr_entry_t *find_entry(xen_msr_entry_t *entries,
+                                   unsigned int nr_entries, unsigned int index)
+{
+    const xen_msr_entry_t key = { index };
+
+    return bsearch(&key, entries, nr_entries, sizeof(*entries), compare_entries);
+}

Isn't "entries" / "entry" a little too generic a name here, considering the CPUID equivalents use "leaves" / "leaf"? (Noticed really while looking at patch 7.)

Quoted 3 lines
Quoted 3 lines
Quoted 2 lines

Would you be fine with naming the function find_msr and leaving the rest of the parameters names as-is?

Yes. But recall I'm not the maintainer of this code anyway.

This file in particular has been entirely within the x86 remit for multiple years now, as have the other cpuid bits in misc/ and libxl.

Quoted 1 line

You cared to provide feedback, and I'm happy to make the change.

find_msr() would be a better name.  As for entries and nr_entries, suggestions welcome.  I couldn't think of anything better for the low level helpers.

~Andrew

#13 lore raw

Re: [PATCH v3 03/13] libs/guest: allow fetching a specific MSR entry from a cpu policy

Quoted 32 lines · Andrew Cooper
Quoted 31 lines · Roger Pau Monné
Quoted 30 lines · Jan Beulich
Quoted 29 lines · Roger Pau Monné
Quoted 28 lines · Jan Beulich
Quoted 14 lines · Roger Pau Monne
tools/libs/guest/xg_cpuid_x86.c +8 -0
@@ -850,3 +850,45 @@ int xc_cpu_policy_get_cpuid(xc_interface *xch, const xc_cpu_policy_t policy,
     *out = *tmp;
     return 0;
 }
+
+static int compare_entries(const void *l, const void *r)
+{
+    const xen_msr_entry_t *lhs = l;
+    const xen_msr_entry_t *rhs = r;
+
+    if ( lhs->idx == rhs->idx )
+        return 0;

tools/libs/guest/xg_cpuid_x86.c int xc_cpu_policy_get_cpuid(xc_interface *xch, const xc_cpu_policy_t policy, · line 870

Quoted 10 lines
tools/libs/guest/xg_cpuid_x86.c +10 -0
+    return lhs->idx < rhs->idx ? -1 : 1;
+}
+
+static xen_msr_entry_t *find_entry(xen_msr_entry_t *entries,
+                                   unsigned int nr_entries, unsigned int index)
+{
+    const xen_msr_entry_t key = { index };
+
+    return bsearch(&key, entries, nr_entries, sizeof(*entries), compare_entries);
+}

Isn't "entries" / "entry" a little too generic a name here, considering the CPUID equivalents use "leaves" / "leaf"? (Noticed really while looking at patch 7.)

Quoted 6 lines
Quoted 3 lines
Quoted 3 lines
Quoted 2 lines

Would you be fine with naming the function find_msr and leaving the rest of the parameters names as-is?

Yes. But recall I'm not the maintainer of this code anyway.

This file in particular has been entirely within the x86 remit for multiple years now, as have the other cpuid bits in misc/ and libxl.

Well, it's somewhere in the middle imo: Its maintainers have kind of permanently delegated to the x86 maintainers. Which still doesn't make it part of "X86 ARCHITECTURE" until such time as the file gets actually named there. Note how e.g. tools/misc/xen-cpuid.c already is.

Jan

#14 lore raw

PATCH v3 4/13 libs/guest: allow updating a cpu policy CPUID data

In reply to Roger Pau Monne

Introduce a helper to update the CPUID policy using an array of xen_cpuid_leaf_t entries. Note the leaves present in the input xen_cpuid_leaf_t array will replace any existing leaves on the policy.

No user of the interface introduced on this patch.

---
Changes since v1:
- Don't use memcpy.
- Drop logic to update the leaf manually - x86_cpuid_copy_from_buffer already does it.
- Only print a failure message if err_leaf != -1.

---
 tools/include/xenctrl.h         |  3 +++
 tools/libs/guest/xg_cpuid_x86.c | 20 ++++++++++++++++++++
 2 files changed, 23 insertions(+)
tools/include/xenctrl.h +3 -0
index 605c632cf30..49f919f16a7 100644
@@ -2613,6 +2613,9 @@ int xc_cpu_policy_get_cpuid(xc_interface *xch, const xc_cpu_policy_t policy,
                             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);
+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_get_cpu_levelling_caps(xc_interface *xch, uint32_t *caps);
 int xc_get_cpu_featureset(xc_interface *xch, uint32_t index,
tools/libs/guest/xg_cpuid_x86.c +20 -0
index 9e83daca0e6..a38e75f8fb1 100644
@@ -892,3 +892,23 @@ int xc_cpu_policy_get_msr(xc_interface *xch, const xc_cpu_policy_t policy,
     *out = *tmp;
     return 0;
 }
+
+int xc_cpu_policy_update_cpuid(xc_interface *xch, xc_cpu_policy_t policy,
+                               const xen_cpuid_leaf_t *leaves,
+                               uint32_t nr)
+{
+    unsigned int err_leaf = -1, err_subleaf = -1;
+    int rc = x86_cpuid_copy_from_buffer(&policy->cpuid, leaves, nr,
+                                        &err_leaf, &err_subleaf);
+
+    if ( rc )
+    {
+        if ( err_leaf != -1 )
+            ERROR("Failed to update CPUID (err leaf %#x, subleaf %#x) (%d = %s)",
+                  err_leaf, err_subleaf, -rc, strerror(-rc));
+        errno = -rc;
+        rc = -1;
+    }
+
+    return rc;
+}
Signature · 1 line
2.31.1
Acked-by on v3 4/13
#15 lore raw

Re: [PATCH v3 04/13] libs/guest: allow updating a cpu policy CPUID data

Quoted 7 lines · Roger Pau Monne

Introduce a helper to update the CPUID policy using an array of xen_cpuid_leaf_t entries. Note the leaves present in the input xen_cpuid_leaf_t array will replace any existing leaves on the policy.

No user of the interface introduced on this patch.

#16 lore raw

PATCH v3 5/13 libs/guest: allow updating a cpu policy MSR data

In reply to Roger Pau Monne

Introduce a helper to update the MSR policy using an array of xen_msr_entry_t entries. Note the MSRs present in the input xen_msr_entry_t array will replace any existing entries on the policy.

No user of the interface introduced on this patch.

---
Changes since v1:
- Drop logic to update entries manually.
- Only print failure message if err_msr != -1.

---
 tools/include/xenctrl.h         |  2 ++
 tools/libs/guest/xg_cpuid_x86.c | 18 ++++++++++++++++++
 2 files changed, 20 insertions(+)
tools/include/xenctrl.h +2 -0
index 49f919f16a7..9a6d1b126d8 100644
@@ -2616,6 +2616,8 @@ int xc_cpu_policy_get_msr(xc_interface *xch, const xc_cpu_policy_t policy,
 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);
 
 int xc_get_cpu_levelling_caps(xc_interface *xch, uint32_t *caps);
 int xc_get_cpu_featureset(xc_interface *xch, uint32_t index,
tools/libs/guest/xg_cpuid_x86.c +18 -0
index a38e75f8fb1..37e55279ffe 100644
@@ -912,3 +912,21 @@ int xc_cpu_policy_update_cpuid(xc_interface *xch, xc_cpu_policy_t policy,
 
     return rc;
 }
+
+int xc_cpu_policy_update_msrs(xc_interface *xch, xc_cpu_policy_t policy,
+                              const xen_msr_entry_t *msrs, uint32_t nr)
+{
+    unsigned int err_msr = -1;
+    int rc = x86_msr_copy_from_buffer(&policy->msr, msrs, nr, &err_msr);
+
+    if ( rc )
+    {
+        if ( err_msr != -1 )
+            ERROR("Failed to deserialise MSRS (err index %#x) (%d = %s)",
+                  err_msr, -rc, strerror(-rc));
+        errno = -rc;
+        rc = -1;
+    }
+
+    return rc;
+}
Signature · 1 line
2.31.1
Acked-by on v3 5/13
#17 lore raw

Re: [PATCH v3 05/13] libs/guest: allow updating a cpu policy MSR data

Quoted 8 lines · Roger Pau Monne

Introduce a helper to update the MSR policy using an array of xen_msr_entry_t entries. Note the MSRs present in the input xen_msr_entry_t array will replace any existing entries on the policy.

No user of the interface introduced on this patch.

#18 lore raw

PATCH v3 6/13 libs/guest: introduce helper to check cpu policy compatibility

In reply to Roger Pau Monne

Such helpers is just a wrapper to the existing x86_cpu_policies_are_compatible function. This requires building policy.c from libx86 on user land also.

No user of the interface introduced.

---
Changes since v1:
- Initialize err.
- Explicitly name parameters as host and guest.

---
 tools/include/xenctrl.h         |  4 ++++
 tools/libs/guest/Makefile       |  2 +-
 tools/libs/guest/xg_cpuid_x86.c | 19 +++++++++++++++++++
 3 files changed, 24 insertions(+), 1 deletion(-)
tools/include/xenctrl.h +4 -0
index 9a6d1b126d8..5f699c09509 100644
@@ -2619,6 +2619,10 @@ int xc_cpu_policy_update_cpuid(xc_interface *xch, xc_cpu_policy_t policy,
 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, const xc_cpu_policy_t host,
+                                 const 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);
tools/libs/guest/Makefile +1 -1
index 604e1695d6a..6d2a1d5bbc0 100644
@@ -40,7 +40,7 @@ $(patsubst %.c,%.opic,$(ELF_SRCS-y)): CFLAGS += -Wno-pointer-sign
 ifeq ($(CONFIG_X86),y) # Add libx86 to the build
 vpath %.c ../../../xen/lib/x86
 
-SRCS-y                 += cpuid.c msr.c
+SRCS-y                 += cpuid.c msr.c policy.c
 endif
 
 # new domain builder
tools/libs/guest/xg_cpuid_x86.c +19 -0
index 37e55279ffe..6b8bae00334 100644
@@ -930,3 +930,22 @@ int xc_cpu_policy_update_msrs(xc_interface *xch, xc_cpu_policy_t policy,
 
     return rc;
 }
+
+bool xc_cpu_policy_is_compatible(xc_interface *xch, const xc_cpu_policy_t host,
+                                 const xc_cpu_policy_t guest)
+{
+    struct cpu_policy_errors err = INIT_CPU_POLICY_ERRORS;
+    struct cpu_policy h = { &host->cpuid, &host->msr };
+    struct cpu_policy g = { &guest->cpuid, &guest->msr };
+    int rc = x86_cpu_policies_are_compatible(&h, &g, &err);
+
+    if ( !rc )
+        return true;
+
+    if ( err.leaf != -1 )
+        ERROR("Leaf %#x subleaf %#x is not compatible", err.leaf, err.subleaf);
+    if ( err.msr != -1 )
+        ERROR("MSR index %#x is not compatible", err.msr);
+
+    return false;
+}
Signature · 1 line
2.31.1
#20 lore raw

PATCH v3 7/13 libs/guest: obtain a compatible cpu policy from two input ones

In reply to Roger Pau Monne

Introduce a helper to obtain a compatible cpu policy based on two input cpu policies. Currently this is done by and'ing all CPUID feature leaves and MSR entries, except for MSR_ARCH_CAPABILITIES which has the RSBA bit or'ed.

The _AC macro is pulled from libxl_internal.h into xen-tools/libs.h since it's required in order to use the msr-index.h header.

Note there's no need to place this helper in libx86, since the calculation of a compatible policy shouldn't be done from the hypervisor.

No callers of the interface introduced.

---
Changes since v2:
- Add some comments.
- Remove stray double semicolon.
- AND all 0x7 subleaves (except 0.EAX).
- Explicitly handle MSR indexes in a switch statement.
- Error out when an unhandled MSR is found.
- Add handling of leaf 0x80000021.

Changes since v1:
- Only AND the feature parts of cpuid.
- Use a binary search to find the matching leaves and msr entries.
- Remove default case from MSR level function.

---
 tools/include/xen-tools/libs.h    |   5 ++
 tools/include/xenctrl.h           |   4 +
 tools/libs/guest/xg_cpuid_x86.c   | 137 ++++++++++++++++++++++++++++++
 tools/libs/light/libxl_internal.h |   2 -
 4 files changed, 146 insertions(+), 2 deletions(-)
tools/include/xen-tools/libs.h +5 -0
index a16e0c38070..b9e89f9a711 100644
@@ -63,4 +63,9 @@
 #define ROUNDUP(_x,_w) (((unsigned long)(_x)+(1UL<<(_w))-1) & ~((1UL<<(_w))-1))
 #endif
 
+#ifndef _AC
+#define __AC(X,Y)   (X##Y)
+#define _AC(X,Y)    __AC(X,Y)
+#endif
+
 #endif	/* __XEN_TOOLS_LIBS__ */
tools/include/xenctrl.h +4 -0
index 5f699c09509..c41d794683c 100644
@@ -2622,6 +2622,10 @@ int xc_cpu_policy_update_msrs(xc_interface *xch, xc_cpu_policy_t policy,
 /* Compatibility calculations. */
 bool xc_cpu_policy_is_compatible(xc_interface *xch, const xc_cpu_policy_t host,
                                  const xc_cpu_policy_t guest);
+int xc_cpu_policy_calc_compatible(xc_interface *xch,
+                                  const xc_cpu_policy_t p1,
+                                  const xc_cpu_policy_t p2,
+                                  xc_cpu_policy_t out);
 
 int xc_get_cpu_levelling_caps(xc_interface *xch, uint32_t *caps);
 int xc_get_cpu_featureset(xc_interface *xch, uint32_t index,
tools/libs/guest/xg_cpuid_x86.c +137 -0

146 lines not on this page · raw message

tools/libs/light/libxl_internal.h +0 -2
index 44a2f3c8fe3..5709bcb93fa 100644
@@ -126,8 +126,6 @@
 #define PVSHIM_CMDLINE "pv-shim console=xen,pv"
 
 /* Size macros. */
-#define __AC(X,Y)   (X##Y)
-#define _AC(X,Y)    __AC(X,Y)
 #define MB(_mb)     (_AC(_mb, ULL) << 20)
 #define GB(_gb)     (_AC(_gb, ULL) << 30)
 
Signature · 1 line
2.31.1
#21 lore raw

Re: [PATCH v3 07/13] libs/guest: obtain a compatible cpu policy from two input ones

Quoted 96 lines · Roger Pau Monne

Introduce a helper to obtain a compatible cpu policy based on two input cpu policies. Currently this is done by and'ing all CPUID feature leaves and MSR entries, except for MSR_ARCH_CAPABILITIES which has the RSBA bit or'ed.

The _AC macro is pulled from libxl_internal.h into xen-tools/libs.h since it's required in order to use the msr-index.h header.

Note there's no need to place this helper in libx86, since the calculation of a compatible policy shouldn't be done from the hypervisor.

No callers of the interface introduced.

---
Changes since v2:
- Add some comments.
- Remove stray double semicolon.
- AND all 0x7 subleaves (except 0.EAX).
- Explicitly handle MSR indexes in a switch statement.
- Error out when an unhandled MSR is found.
- Add handling of leaf 0x80000021.

Changes since v1:
- Only AND the feature parts of cpuid.
- Use a binary search to find the matching leaves and msr entries.
- Remove default case from MSR level function.

---
 tools/include/xen-tools/libs.h    |   5 ++
 tools/include/xenctrl.h           |   4 +
 tools/libs/guest/xg_cpuid_x86.c   | 137 ++++++++++++++++++++++++++++++
 tools/libs/light/libxl_internal.h |   2 -
 4 files changed, 146 insertions(+), 2 deletions(-)
tools/include/xen-tools/libs.h +5 -0
index a16e0c38070..b9e89f9a711 100644
@@ -63,4 +63,9 @@
 #define ROUNDUP(_x,_w) (((unsigned long)(_x)+(1UL<<(_w))-1) & ~((1UL<<(_w))-1))
 #endif
 
+#ifndef _AC
+#define __AC(X,Y)   (X##Y)
+#define _AC(X,Y)    __AC(X,Y)
+#endif
+
 #endif	/* __XEN_TOOLS_LIBS__ */
tools/include/xenctrl.h +4 -0
index 5f699c09509..c41d794683c 100644
@@ -2622,6 +2622,10 @@ int xc_cpu_policy_update_msrs(xc_interface *xch, xc_cpu_policy_t policy,
 /* Compatibility calculations. */
 bool xc_cpu_policy_is_compatible(xc_interface *xch, const xc_cpu_policy_t host,
                                  const xc_cpu_policy_t guest);
+int xc_cpu_policy_calc_compatible(xc_interface *xch,
+                                  const xc_cpu_policy_t p1,
+                                  const xc_cpu_policy_t p2,
+                                  xc_cpu_policy_t out);
 
 int xc_get_cpu_levelling_caps(xc_interface *xch, uint32_t *caps);
 int xc_get_cpu_featureset(xc_interface *xch, uint32_t index,
tools/libs/guest/xg_cpuid_x86.c +17 -0
index 6b8bae00334..be2056469aa 100644
@@ -32,6 +32,7 @@ enum {
 #include <xen/arch-x86/cpufeatureset.h>
 };
 
+#include <xen/asm/msr-index.h>
 #include <xen/asm/x86-vendors.h>
 
 #include <xen/lib/x86/cpu-policy.h>
@@ -949,3 +950,139 @@ bool xc_cpu_policy_is_compatible(xc_interface *xch, const xc_cpu_policy_t host,
 
     return false;
 }
+
+static bool level_msr(const xen_msr_entry_t *e1, const xen_msr_entry_t *e2,
+                      xen_msr_entry_t *out)
+{
+    *out = (xen_msr_entry_t){ .idx = e1->idx };
+
+    switch ( e1->idx )
+    {
+    case MSR_INTEL_PLATFORM_INFO:
+        out->val = e1->val & e2->val;
+        return true;
+
+    case MSR_ARCH_CAPABILITIES:
+        out->val = e1->val & e2->val;
+        /*
+         * Set RSBA if present on any of the input values to notice the guest

tools/libs/guest/xg_cpuid_x86.c bool xc_cpu_policy_is_compatible(xc_interface *xch, const xc_cpu_policy_t host, · line 978

Quoted 10 lines
tools/libs/guest/xg_cpuid_x86.c +10 -0
+         * might run on vulnerable hardware at some point.
+         */
+        out->val |= (e1->val | e2->val) & ARCH_CAPS_RSBA;
+        return true;
+    }
+
+    return false;
+}
+
+/* Only level featuresets so far. */

I have to admit that I don't think I see all the implications from this implementation restriction. All other leaves get dropped by the caller, but it's not clear to me what this means wrt what the guest is ultimately going to get to see.

tools/libs/guest/xg_cpuid_x86.c bool xc_cpu_policy_is_compatible(xc_interface *xch, const xc_cpu_policy_t host, · line 984

Quoted 6 lines
tools/libs/guest/xg_cpuid_x86.c +6 -0
+static bool level_leaf(const xen_cpuid_leaf_t *l1, const xen_cpuid_leaf_t *l2,
+                       xen_cpuid_leaf_t *out)
+{
+    *out = (xen_cpuid_leaf_t){
+        .leaf = l1->leaf,
+        .subleaf = l2->subleaf,

Since ->leaf and ->subleaf ought to match anyway, I think it would look less odd if both initializers were taken from consistent source.

Quoted 14 lines
tools/libs/guest/xg_cpuid_x86.c +14 -0
+    };
+
+    switch ( l1->leaf )
+    {
+    case 0x1:
+    case 0x80000001:
+        out->c = l1->c & l2->c;
+        out->d = l1->d & l2->d;
+        return true;
+
+    case 0xd:
+        if ( l1->subleaf != 1 )
+            break;
+        /*

tools/libs/guest/xg_cpuid_x86.c bool xc_cpu_policy_is_compatible(xc_interface *xch, const xc_cpu_policy_t host, · line 1008

Quoted 10 lines
tools/libs/guest/xg_cpuid_x86.c +10 -0
+         * Only take Da1 into account, the rest of subleaves will be dropped
+         * and recalculated by recalculate_xstate.
+         */
+        out->a = l1->a & l2->a;
+        return true;
+
+    case 0x7:
+        if ( l1->subleaf )
+            /* subleaf 0 EAX contains the max subleaf count. */
+            out->a = l1->a & l2->a;
        else
            out->a = min(l1->a, l2->a);

? Or is the result from here then further passed to x86_cpuid_policy_shrink_max_leaves() (not visible from this patch)? (If not, the same would apply to all other multi-subleaf leaves.)

Quoted 15 lines
tools/libs/guest/xg_cpuid_x86.c +15 -0
+        out->b = l1->b & l2->b;
+        out->c = l1->c & l2->c;
+        out->d = l1->d & l2->d;
+        return true;
+
+    case 0x80000007:
+        out->d = l1->d & l2->d;
+        return true;
+
+    case 0x80000008:
+        out->b = l1->b & l2->b;
+        return true;
+
+    case 0x80000021:
+        out->a = l1->a & l2->a;

tools/libs/guest/xg_cpuid_x86.c bool xc_cpu_policy_is_compatible(xc_interface *xch, const xc_cpu_policy_t host, · line 1033

Quoted 10 lines
tools/libs/guest/xg_cpuid_x86.c +10 -0
+        return true;
+    }
+
+    return false;
+}
+
+int xc_cpu_policy_calc_compatible(xc_interface *xch,
+                                  const xc_cpu_policy_t p1,
+                                  const xc_cpu_policy_t p2,
+                                  xc_cpu_policy_t out)

I have to admit that I find these two "const" misleading here. You don't equally constify the other two parameters (which would e.g. be xc_interface *const xch), and I don't think doing so is common practice elsewhere. And what p1 and p2 point to is specifically non- const (and cannot be const), due to ...

Quoted 5 lines
tools/libs/guest/xg_cpuid_x86.c +5 -0
+{
+    unsigned int nr_leaves, nr_msrs, i, index;
+    unsigned int p1_nr_leaves, p2_nr_leaves;
+    unsigned int p1_nr_entries, p2_nr_entries;
+    int rc;

tools/libs/guest/xg_cpuid_x86.c bool xc_cpu_policy_is_compatible(xc_interface *xch, const xc_cpu_policy_t host, · line 1048

Quoted 10 lines
tools/libs/guest/xg_cpuid_x86.c +10 -0
+
+    p1_nr_leaves = p2_nr_leaves = ARRAY_SIZE(p1->leaves);
+    p1_nr_entries = p2_nr_entries = ARRAY_SIZE(p1->entries);
+
+    rc = xc_cpu_policy_serialise(xch, p1, p1->leaves, &p1_nr_leaves,
+                                 p1->entries, &p1_nr_entries);
+    if ( rc )
+        return rc;
+    rc = xc_cpu_policy_serialise(xch, p2, p2->leaves, &p2_nr_leaves,
+                                 p2->entries, &p2_nr_entries);

... these two calls.

Jan

#22 lore raw

Re: [PATCH v3 07/13] libs/guest: obtain a compatible cpu policy from two input ones

Quoted 104 lines · Jan Beulich
Quoted 103 lines · Roger Pau Monne

Introduce a helper to obtain a compatible cpu policy based on two input cpu policies. Currently this is done by and'ing all CPUID feature leaves and MSR entries, except for MSR_ARCH_CAPABILITIES which has the RSBA bit or'ed.

The _AC macro is pulled from libxl_internal.h into xen-tools/libs.h since it's required in order to use the msr-index.h header.

Note there's no need to place this helper in libx86, since the calculation of a compatible policy shouldn't be done from the hypervisor.

No callers of the interface introduced.

---
Changes since v2:
- Add some comments.
- Remove stray double semicolon.
- AND all 0x7 subleaves (except 0.EAX).
- Explicitly handle MSR indexes in a switch statement.
- Error out when an unhandled MSR is found.
- Add handling of leaf 0x80000021.

Changes since v1:
- Only AND the feature parts of cpuid.
- Use a binary search to find the matching leaves and msr entries.
- Remove default case from MSR level function.

---
 tools/include/xen-tools/libs.h    |   5 ++
 tools/include/xenctrl.h           |   4 +
 tools/libs/guest/xg_cpuid_x86.c   | 137 ++++++++++++++++++++++++++++++
 tools/libs/light/libxl_internal.h |   2 -
 4 files changed, 146 insertions(+), 2 deletions(-)
tools/include/xen-tools/libs.h +5 -0
index a16e0c38070..b9e89f9a711 100644
@@ -63,4 +63,9 @@
 #define ROUNDUP(_x,_w) (((unsigned long)(_x)+(1UL<<(_w))-1) & ~((1UL<<(_w))-1))
 #endif
 
+#ifndef _AC
+#define __AC(X,Y)   (X##Y)
+#define _AC(X,Y)    __AC(X,Y)
+#endif
+
 #endif	/* __XEN_TOOLS_LIBS__ */
tools/include/xenctrl.h +4 -0
index 5f699c09509..c41d794683c 100644
@@ -2622,6 +2622,10 @@ int xc_cpu_policy_update_msrs(xc_interface *xch, xc_cpu_policy_t policy,
 /* Compatibility calculations. */
 bool xc_cpu_policy_is_compatible(xc_interface *xch, const xc_cpu_policy_t host,
                                  const xc_cpu_policy_t guest);
+int xc_cpu_policy_calc_compatible(xc_interface *xch,
+                                  const xc_cpu_policy_t p1,
+                                  const xc_cpu_policy_t p2,
+                                  xc_cpu_policy_t out);
 
 int xc_get_cpu_levelling_caps(xc_interface *xch, uint32_t *caps);
 int xc_get_cpu_featureset(xc_interface *xch, uint32_t index,
tools/libs/guest/xg_cpuid_x86.c +24 -0
index 6b8bae00334..be2056469aa 100644
@@ -32,6 +32,7 @@ enum {
 #include <xen/arch-x86/cpufeatureset.h>
 };
 
+#include <xen/asm/msr-index.h>
 #include <xen/asm/x86-vendors.h>
 
 #include <xen/lib/x86/cpu-policy.h>
@@ -949,3 +950,139 @@ bool xc_cpu_policy_is_compatible(xc_interface *xch, const xc_cpu_policy_t host,
 
     return false;
 }
+
+static bool level_msr(const xen_msr_entry_t *e1, const xen_msr_entry_t *e2,
+                      xen_msr_entry_t *out)
+{
+    *out = (xen_msr_entry_t){ .idx = e1->idx };
+
+    switch ( e1->idx )
+    {
+    case MSR_INTEL_PLATFORM_INFO:
+        out->val = e1->val & e2->val;
+        return true;
+
+    case MSR_ARCH_CAPABILITIES:
+        out->val = e1->val & e2->val;
+        /*
+         * Set RSBA if present on any of the input values to notice the guest
+         * might run on vulnerable hardware at some point.
+         */
+        out->val |= (e1->val | e2->val) & ARCH_CAPS_RSBA;
+        return true;
+    }
+
+    return false;
Quoted 8 lines

tools/libs/guest/xg_cpuid_x86.c bool xc_cpu_policy_is_compatible(xc_interface *xch, const xc_cpu_policy_t host, · line 978

Quoted 3 lines
tools/libs/guest/xg_cpuid_x86.c +3 -0
+}
+
+/* Only level featuresets so far. */

I have to admit that I don't think I see all the implications from this implementation restriction. All other leaves get dropped by the caller, but it's not clear to me what this means wrt what the guest is ultimately going to get to see.

This aims to be based on what XenServer does, which I was told is to level the featuresets. I think the caller of the function will have to fill the part of the policy that cannot be leveled. It's likely new helpers will be added to do that as required.

One option would be to get the default policy for the guest and then use xc_cpu_policy_update_cpuid to apply the leveled one.

Quoted 9 lines

tools/libs/guest/xg_cpuid_x86.c bool xc_cpu_policy_is_compatible(xc_interface *xch, const xc_cpu_policy_t host, · line 984

Quoted 6 lines
tools/libs/guest/xg_cpuid_x86.c +6 -0
+static bool level_leaf(const xen_cpuid_leaf_t *l1, const xen_cpuid_leaf_t *l2,
+                       xen_cpuid_leaf_t *out)
+{
+    *out = (xen_cpuid_leaf_t){
+        .leaf = l1->leaf,
+        .subleaf = l2->subleaf,

Since ->leaf and ->subleaf ought to match anyway, I think it would look less odd if both initializers were taken from consistent source.

Sure, my bad.

Quoted 28 lines
Quoted 14 lines
tools/libs/guest/xg_cpuid_x86.c +14 -0
+    };
+
+    switch ( l1->leaf )
+    {
+    case 0x1:
+    case 0x80000001:
+        out->c = l1->c & l2->c;
+        out->d = l1->d & l2->d;
+        return true;
+
+    case 0xd:
+        if ( l1->subleaf != 1 )
+            break;
+        /*

tools/libs/guest/xg_cpuid_x86.c bool xc_cpu_policy_is_compatible(xc_interface *xch, const xc_cpu_policy_t host, · line 1008

Quoted 10 lines
tools/libs/guest/xg_cpuid_x86.c +10 -0
+         * Only take Da1 into account, the rest of subleaves will be dropped
+         * and recalculated by recalculate_xstate.
+         */
+        out->a = l1->a & l2->a;
+        return true;
+
+    case 0x7:
+        if ( l1->subleaf )
+            /* subleaf 0 EAX contains the max subleaf count. */
+            out->a = l1->a & l2->a;
        else
            out->a = min(l1->a, l2->a);
Quoted 3 lines

? Or is the result from here then further passed to x86_cpuid_policy_shrink_max_leaves() (not visible from this patch)? (If not, the same would apply to all other multi-subleaf leaves.)

Hm, might be worth to set all the max fields directly in xc_cpu_policy_calc_compatible and also add a call to x86_cpuid_policy_shrink_max_leaves after the leveling is done.

Quoted 41 lines
Quoted 15 lines
tools/libs/guest/xg_cpuid_x86.c +15 -0
+        out->b = l1->b & l2->b;
+        out->c = l1->c & l2->c;
+        out->d = l1->d & l2->d;
+        return true;
+
+    case 0x80000007:
+        out->d = l1->d & l2->d;
+        return true;
+
+    case 0x80000008:
+        out->b = l1->b & l2->b;
+        return true;
+
+    case 0x80000021:
+        out->a = l1->a & l2->a;

tools/libs/guest/xg_cpuid_x86.c bool xc_cpu_policy_is_compatible(xc_interface *xch, const xc_cpu_policy_t host, · line 1033

Quoted 10 lines
tools/libs/guest/xg_cpuid_x86.c +10 -0
+        return true;
+    }
+
+    return false;
+}
+
+int xc_cpu_policy_calc_compatible(xc_interface *xch,
+                                  const xc_cpu_policy_t p1,
+                                  const xc_cpu_policy_t p2,
+                                  xc_cpu_policy_t out)

I have to admit that I find these two "const" misleading here. You don't equally constify the other two parameters (which would e.g. be xc_interface *const xch), and I don't think doing so is common practice elsewhere. And what p1 and p2 point to is specifically non- const (and cannot be const), due to ...

Quoted 9 lines
tools/libs/guest/xg_cpuid_x86.c +9 -0
+{
+    unsigned int nr_leaves, nr_msrs, i, index;
+    unsigned int p1_nr_leaves, p2_nr_leaves;
+    unsigned int p1_nr_entries, p2_nr_entries;
+    int rc;
+
+    p1_nr_leaves = p2_nr_leaves = ARRAY_SIZE(p1->leaves);
+    p1_nr_entries = p2_nr_entries = ARRAY_SIZE(p1->entries);
+
Quoted 8 lines

tools/libs/guest/xg_cpuid_x86.c bool xc_cpu_policy_is_compatible(xc_interface *xch, const xc_cpu_policy_t host, · line 1048

Quoted 6 lines
tools/libs/guest/xg_cpuid_x86.c +6 -0
+    rc = xc_cpu_policy_serialise(xch, p1, p1->leaves, &p1_nr_leaves,
+                                 p1->entries, &p1_nr_entries);
+    if ( rc )
+        return rc;
+    rc = xc_cpu_policy_serialise(xch, p2, p2->leaves, &p2_nr_leaves,
+                                 p2->entries, &p2_nr_entries);

... these two calls.

Right, that's a leftover from previously, where xc_cpu_policy_t didn't also contain a buffer to store the serialized version.

Thanks, Roger.

#23 lore raw

Re: [PATCH v3 07/13] libs/guest: obtain a compatible cpu policy from two input ones

Quoted 9 lines · Roger Pau Monné
Quoted 7 lines · Jan Beulich
Quoted 1 line · Roger Pau Monne

+/* Only level featuresets so far. */

I have to admit that I don't think I see all the implications from this implementation restriction. All other leaves get dropped by the caller, but it's not clear to me what this means wrt what the guest is ultimately going to get to see.

Quoted 7 lines

This aims to be based on what XenServer does, which I was told is to level the featuresets. I think the caller of the function will have to fill the part of the policy that cannot be leveled. It's likely new helpers will be added to do that as required.

One option would be to get the default policy for the guest and then use xc_cpu_policy_update_cpuid to apply the leveled one.

Could such further plans perhaps be outlined (to a degree at least) in the description?

Jan

#24 lore raw

Re: [PATCH v3 07/13] libs/guest: obtain a compatible cpu policy from two input ones

In reply to Roger Pau Monne
Quoted 28 lines · Roger Pau Monne

Introduce a helper to obtain a compatible cpu policy based on two input cpu policies. Currently this is done by and'ing all CPUID feature leaves and MSR entries, except for MSR_ARCH_CAPABILITIES which has the RSBA bit or'ed.

The _AC macro is pulled from libxl_internal.h into xen-tools/libs.h since it's required in order to use the msr-index.h header.

Note there's no need to place this helper in libx86, since the calculation of a compatible policy shouldn't be done from the hypervisor.

No callers of the interface introduced.

---
Changes since v2:
- Add some comments.
- Remove stray double semicolon.
- AND all 0x7 subleaves (except 0.EAX).
- Explicitly handle MSR indexes in a switch statement.
- Error out when an unhandled MSR is found.
- Add handling of leaf 0x80000021.

Changes since v1:
- Only AND the feature parts of cpuid.
- Use a binary search to find the matching leaves and msr entries.
- Remove default case from MSR level function.

Quoted 5 lines
---
 tools/include/xen-tools/libs.h    |   5 ++
 tools/include/xenctrl.h           |   4 +
 tools/libs/guest/xg_cpuid_x86.c   | 137 ++++++++++++++++++++++++++++++
 tools/libs/light/libxl_internal.h |   2 -

This *needs* to be in libx86.  I don't particularly mind if you start with it behind #ifdef __XEN__ (I'm still sure we'll need it in the hypervisor), but this, more than just about anything else, needs to be covered by the unit tests.

Next, you need to follow the same structure as Xen's cpuid.c for deriving policies.  You can't just loop through the two serialised forms like this.

To start with, you want to calculate the min of a/b->max_leaf, then loop over that pulling information sideways from a/b.

For MSRs, all but MSR_INTEL_PLATFORM_INFO are CPUID qualified, so need to look like:

if ( out.cpuid.feat.arch_caps ) out.msr.arch_caps.raw = ((a.msr.arch_caps.raw ^ INV_MASK) & (b.msr.arch_caps.raw ^ INV_MASK)) ^ INV_MASK;

Where INV_MASK is the mask of arch caps bits which want inverted polarity.  (Name subject to change - perhap ARCH_CAPS_POLARITY_MASK ?)

I'm sure I had some work starting this somewhere.  I'll see if I can locate it.

~Andrew

#25 lore raw

PATCH v3 8/13 libs/guest: make a cpu policy compatible with older Xen versions

In reply to Roger Pau Monne

Older 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 v1:
- Move comments and explicitly mention pre-4.14 Xen.

---
 tools/include/xenctrl.h         |  4 +++
 tools/libs/guest/xg_cpuid_x86.c | 58 ++++++++++++++++++++++++---------
 2 files changed, 47 insertions(+), 15 deletions(-)
tools/include/xenctrl.h +4 -0
index c41d794683c..89a73fd6823 100644
@@ -2627,6 +2627,10 @@ int xc_cpu_policy_calc_compatible(xc_interface *xch,
                                   const xc_cpu_policy_t p2,
                                   xc_cpu_policy_t out);
 
+/* Make a policy compatible with pre-4.14 Xen versions. */
+int xc_cpu_policy_make_compatible(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 +43 -15

73 lines not on this page · raw message

Signature · 1 line
2.31.1
#26 lore raw

Re: [PATCH v3 08/13] libs/guest: make a cpu policy compatible with older Xen versions

tools/libs/guest/xg_cpuid_x86.c int xc_cpu_policy_calc_compatible(xc_interface *xch, · line 1080

Quoted 7 lines · Roger Pau Monne
tools/libs/guest/xg_cpuid_x86.c +3 -0
@@ -1086,3 +1075,42 @@ int xc_cpu_policy_calc_compatible(xc_interface *xch,
 
     return rc;
 }
+
+int xc_cpu_policy_make_compatible(xc_interface *xch, xc_cpu_policy_t policy,
+                                  bool hvm)

I'm concerned of the naming, and in particular the two very different meanings of "compatible" for xc_cpu_policy_calc_compatible() and this new one. I'm afraid I don't have a good suggestion though, short of making the name even longer and inserting "backwards".

Jan

Quoted 37 lines
tools/libs/guest/xg_cpuid_x86.c +36 -0
+{
+    xc_cpu_policy_t host;
+    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);
+    if ( rc )
+    {
+        ERROR("Failed to get host policy");
+        goto out;
+    }
+
+    /*
+     * Account for features which have been disabled by default since Xen 4.13,
+     * so migrated-in VM's don't risk seeing features disappearing.
+     */
+    policy->cpuid.basic.rdrand = host->cpuid.basic.rdrand;
+
+    if ( hvm )
+        policy->cpuid.feat.mpx = host->cpuid.feat.mpx;
+
+    /* Clamp maximum leaves to the ones supported on 4.12. */
+    policy->cpuid.basic.max_leaf = min(policy->cpuid.basic.max_leaf, 0xdu);
+    policy->cpuid.feat.max_subleaf = 0;
+    policy->cpuid.extd.max_leaf = min(policy->cpuid.extd.max_leaf, 0x1cu);
+
+ out:
+    xc_cpu_policy_destroy(host);
+    return rc;
+}
#27 lore raw

Re: [PATCH v3 08/13] libs/guest: make a cpu policy compatible with older Xen versions

Quoted 13 lines · Jan Beulich

tools/libs/guest/xg_cpuid_x86.c int xc_cpu_policy_calc_compatible(xc_interface *xch, · line 1080

Quoted 7 lines · Roger Pau Monne
tools/libs/guest/xg_cpuid_x86.c +3 -0
@@ -1086,3 +1075,42 @@ int xc_cpu_policy_calc_compatible(xc_interface *xch,
 
     return rc;
 }
+
+int xc_cpu_policy_make_compatible(xc_interface *xch, xc_cpu_policy_t policy,
+                                  bool hvm)

I'm concerned of the naming, and in particular the two very different meanings of "compatible" for xc_cpu_policy_calc_compatible() and this new one. I'm afraid I don't have a good suggestion though, short of making the name even longer and inserting "backwards".

Would xc_cpu_policy_make_compat_412 be acceptable?

That's the more concise one I can think of.

Thanks, Roger.

#28 lore raw

Re: [PATCH v3 08/13] libs/guest: make a cpu policy compatible with older Xen versions

Quoted 15 lines · Roger Pau Monné
Quoted 13 lines · Jan Beulich

tools/libs/guest/xg_cpuid_x86.c int xc_cpu_policy_calc_compatible(xc_interface *xch, · line 1080

Quoted 7 lines · Roger Pau Monne
tools/libs/guest/xg_cpuid_x86.c +3 -0
@@ -1086,3 +1075,42 @@ int xc_cpu_policy_calc_compatible(xc_interface *xch,
 
     return rc;
 }
+
+int xc_cpu_policy_make_compatible(xc_interface *xch, xc_cpu_policy_t policy,
+                                  bool hvm)

I'm concerned of the naming, and in particular the two very different meanings of "compatible" for xc_cpu_policy_calc_compatible() and this new one. I'm afraid I don't have a good suggestion though, short of making the name even longer and inserting "backwards".

Quoted 3 lines

Would xc_cpu_policy_make_compat_412 be acceptable?

That's the more concise one I can think of.

Hmm, maybe (perhaps with an underscore inserted between 4 and 12). Yet (sorry) a comment in the function says "since Xen 4.13", which includes changes that have happened later. Therefore it's not really clear to me whether the function really _only_ deals with the 4.12 / 4.13 boundary.

Jan

#29 lore raw

Re: [PATCH v3 08/13] libs/guest: make a cpu policy compatible with older Xen versions

Quoted 16 lines · Jan Beulich
Quoted 15 lines · Roger Pau Monné
Quoted 13 lines · Jan Beulich

tools/libs/guest/xg_cpuid_x86.c int xc_cpu_policy_calc_compatible(xc_interface *xch, · line 1080

Quoted 7 lines · Roger Pau Monne
tools/libs/guest/xg_cpuid_x86.c +3 -0
@@ -1086,3 +1075,42 @@ int xc_cpu_policy_calc_compatible(xc_interface *xch,
 
     return rc;
 }
+
+int xc_cpu_policy_make_compatible(xc_interface *xch, xc_cpu_policy_t policy,
+                                  bool hvm)

I'm concerned of the naming, and in particular the two very different meanings of "compatible" for xc_cpu_policy_calc_compatible() and this new one. I'm afraid I don't have a good suggestion though, short of making the name even longer and inserting "backwards".

Quoted 8 lines
Quoted 3 lines

Would xc_cpu_policy_make_compat_412 be acceptable?

That's the more concise one I can think of.

Hmm, maybe (perhaps with an underscore inserted between 4 and 12). Yet (sorry) a comment in the function says "since Xen 4.13", which includes changes that have happened later. Therefore it's not really clear to me whether the function really _only_ deals with the 4.12 / 4.13 boundary.

It should deal with any changes in the default cpuid policy that happened after (and not including) Xen 4.12. So resulting policy is compatible with the behaviour that Xen 4.12 had. Any changes made in Xen 4.13 and further versions should be accounted for here.

Thanks, Roger.

#30 lore raw

Re: [PATCH v3 08/13] libs/guest: make a cpu policy compatible with older Xen versions

Quoted 26 lines · Roger Pau Monné
Quoted 16 lines · Jan Beulich
Quoted 15 lines · Roger Pau Monné
Quoted 13 lines · Jan Beulich

tools/libs/guest/xg_cpuid_x86.c int xc_cpu_policy_calc_compatible(xc_interface *xch, · line 1080

Quoted 7 lines · Roger Pau Monne
tools/libs/guest/xg_cpuid_x86.c +3 -0
@@ -1086,3 +1075,42 @@ int xc_cpu_policy_calc_compatible(xc_interface *xch,
 
     return rc;
 }
+
+int xc_cpu_policy_make_compatible(xc_interface *xch, xc_cpu_policy_t policy,
+                                  bool hvm)

I'm concerned of the naming, and in particular the two very different meanings of "compatible" for xc_cpu_policy_calc_compatible() and this new one. I'm afraid I don't have a good suggestion though, short of making the name even longer and inserting "backwards".

Quoted 8 lines
Quoted 3 lines

Would xc_cpu_policy_make_compat_412 be acceptable?

That's the more concise one I can think of.

Hmm, maybe (perhaps with an underscore inserted between 4 and 12). Yet (sorry) a comment in the function says "since Xen 4.13", which includes changes that have happened later. Therefore it's not really clear to me whether the function really _only_ deals with the 4.12 / 4.13 boundary.

Quoted 4 lines

It should deal with any changes in the default cpuid policy that happened after (and not including) Xen 4.12. So resulting policy is compatible with the behaviour that Xen 4.12 had. Any changes made in Xen 4.13 and further versions should be accounted for here.

I see.

Jan

#31 lore raw

PATCH v3 9/13 libs/guest: introduce helper set cpu topology in cpu policy

In reply to Roger Pau Monne

This 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/xenctrl.h         |   4 +
 tools/libs/guest/xg_cpuid_x86.c | 182 +++++++++++++++++---------------
 2 files changed, 103 insertions(+), 83 deletions(-)
tools/include/xenctrl.h +4 -0
index 89a73fd6823..ec184bccd3f 100644
@@ -2631,6 +2631,10 @@ int xc_cpu_policy_calc_compatible(xc_interface *xch,
 int xc_cpu_policy_make_compatible(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 +99 -83

207 lines not on this page · raw message

Signature · 1 line
2.31.1
#32 lore raw

PATCH v3 10/13 libs/guest: rework xc_cpuid_xend_policy

In reply to Roger Pau Monne

Rename 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.

No functional change intended.

---
 tools/include/xenctrl.h         |   4 +
 tools/libs/guest/xg_cpuid_x86.c | 182 ++++++++++++++------------------
 2 files changed, 84 insertions(+), 102 deletions(-)
tools/include/xenctrl.h +4 -0
index ec184bccd3f..ce0785f7654 100644
@@ -2635,6 +2635,10 @@ int xc_cpu_policy_make_compatible(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 +80 -102

251 lines not on this page · raw message

Signature · 1 line
2.31.1
#33 lore raw

PATCH v3 11/13 libs/guest: apply a featureset into a cpu policy

In reply to Roger Pau Monne

Pull 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/xenctrl.h         |  5 ++
 tools/libs/guest/xg_cpuid_x86.c | 95 ++++++++++++++++++++-------------
 2 files changed, 62 insertions(+), 38 deletions(-)
tools/include/xenctrl.h +5 -0
index ce0785f7654..3fef954d1d1 100644
@@ -2639,6 +2639,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.31.1
#34 lore raw

PATCH v3 12/13 libs/{light,guest}: implement xc_cpuid_apply_policy in libxl

In reply to Roger Pau Monne

With 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 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  |  94 ++++++++++++++++++++++--
 3 files changed, 87 insertions(+), 147 deletions(-)
tools/include/xenctrl.h +0 -18
index 3fef954d1d1..c6ee1142e8e 100644
@@ -1890,24 +1890,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 +87 -7

124 lines not on this page · raw message

Signature · 1 line
2.31.1
Reviewed-by on v3 12/13
#35 lore raw

Re: [PATCH v3 12/13] libs/{light,guest}: implement xc_cpuid_apply_policy in libxl

Quoted 7 lines · Roger Pau Monne

With 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.

Quoted 7 lines

---
Changes since v2:
- Use 'r' for libxc return values.
- Fix comment about making a cpu policy compatible.
- Use LOG*D macros.
---

Thanks,

Signature · 1 line
Anthony PERARD
#36 lore raw

PATCH v3 13/13 libs/guest: (re)move xc_cpu_policy_apply_cpuid

In reply to Roger Pau Monne

Move 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           |  30 -------
 tools/libs/guest/xg_cpuid_x86.c   | 125 --------------------------
 tools/libs/light/libxl_cpuid.c    | 142 ++++++++++++++++++++++++++++--
 tools/libs/light/libxl_internal.h |  26 ++++++
 5 files changed, 165 insertions(+), 164 deletions(-)
tools/include/libxl.h +3 -3
index ae7fe27c1f2..150b7ba85ac 100644
@@ -1375,10 +1375,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 -30
index c6ee1142e8e..044d05321a3 100644
@@ -1864,32 +1864,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);
@@ -2617,10 +2591,6 @@ int xc_cpu_policy_make_compatible(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
index 5709bcb93fa..d130c073ac4 100644
@@ -2050,6 +2050,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.31.1
Reviewed-by on v3 13/13
#37 lore raw

Re: [PATCH v3 13/13] libs/guest: (re)move xc_cpu_policy_apply_cpuid

Quoted 11 lines · Roger Pau Monne

Move 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.

Quoted 7 lines

---
Changes since v2:
- Use LOG*D.
- Pass a gc to apply_policy.
- Use 'r' for libxc return values.
---

Thanks,

Signature · 1 line
Anthony PERARD