diff mbox series

[09/13] libxl: change the type of libxl_cpuid_policy_list

Message ID 20230616131019.11476-10-roger.pau@citrix.com (mailing list archive)
State Superseded
Headers show
Series lib{xc,xl}: support for guest MSR features | expand

Commit Message

Roger Pau Monné June 16, 2023, 1:10 p.m. UTC
Currently libxl_cpuid_policy_list is an opaque type to the users of
libxl, and internally it's an array of xc_xend_cpuid objects.

Change the type to instead be a structure that contains one array for
CPUID policies, in preparation for it also holding another array for
MSR policies.

Signed-off-by: Roger Pau Monné <roger.pau@citrix.com>
---
 tools/include/libxl.h          |  6 +++--
 tools/libs/light/gentest.py    |  2 +-
 tools/libs/light/libxl_cpuid.c | 49 +++++++++++++++++++---------------
 3 files changed, 32 insertions(+), 25 deletions(-)

Comments

Andrew Cooper June 20, 2023, 12:14 p.m. UTC | #1
On 16/06/2023 2:10 pm, Roger Pau Monne wrote:
> Currently libxl_cpuid_policy_list is an opaque type to the users of
> libxl, and internally it's an array of xc_xend_cpuid objects.
>
> Change the type to instead be a structure that contains one array for
> CPUID policies, in preparation for it also holding another array for
> MSR policies.
>
> Signed-off-by: Roger Pau Monné <roger.pau@citrix.com>
> ---
>  tools/include/libxl.h          |  6 +++--
>  tools/libs/light/gentest.py    |  2 +-
>  tools/libs/light/libxl_cpuid.c | 49 +++++++++++++++++++---------------
>  3 files changed, 32 insertions(+), 25 deletions(-)
>
> diff --git a/tools/include/libxl.h b/tools/include/libxl.h
> index cac641a7eba2..41e19f2af7f5 100644
> --- a/tools/include/libxl.h
> +++ b/tools/include/libxl.h
> @@ -1459,8 +1459,10 @@ 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.
>   */
> -typedef struct xc_xend_cpuid libxl_cpuid_policy;
> -typedef libxl_cpuid_policy * libxl_cpuid_policy_list;
> +typedef struct libxl_cpu_policy {
> +    struct xc_xend_cpuid *cpuid;
> +} libxl_cpuid_policy;
> +typedef libxl_cpuid_policy libxl_cpuid_policy_list;

I don't think we can get away with doing this.  It makes the type
non-opaque, and you'll also break the libxl ABI in the next patch when
you change the size of this object.

For better or worse, I think

typedef libxl_cpuid_policy * libxl_cpuid_policy_list;

needs to stay here, and libxl_cpuid_policy get moved into
libxl_internal.h where it can then be altered.

~Andrew
diff mbox series

Patch

diff --git a/tools/include/libxl.h b/tools/include/libxl.h
index cac641a7eba2..41e19f2af7f5 100644
--- a/tools/include/libxl.h
+++ b/tools/include/libxl.h
@@ -1459,8 +1459,10 @@  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.
  */
-typedef struct xc_xend_cpuid libxl_cpuid_policy;
-typedef libxl_cpuid_policy * libxl_cpuid_policy_list;
+typedef struct libxl_cpu_policy {
+    struct xc_xend_cpuid *cpuid;
+} 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);
 void libxl_cpuid_policy_list_copy(libxl_ctx *ctx,
diff --git a/tools/libs/light/gentest.py b/tools/libs/light/gentest.py
index 1cc7eebc826d..207f988a741d 100644
--- a/tools/libs/light/gentest.py
+++ b/tools/libs/light/gentest.py
@@ -194,7 +194,7 @@  static void libxl_cpuid_policy_list_rand_init(libxl_cpuid_policy_list *pp)
     };
     const int nr_options = sizeof(options)/sizeof(options[0]);
     char buf[64];
-    libxl_cpuid_policy_list p = NULL;
+    libxl_cpuid_policy_list p = { };
 
     for (i = 0; i < nr_policies; i++) {
         int opt = test_rand(nr_options);
diff --git a/tools/libs/light/libxl_cpuid.c b/tools/libs/light/libxl_cpuid.c
index c96aeb3bce46..ded0d0b8bc15 100644
--- a/tools/libs/light/libxl_cpuid.c
+++ b/tools/libs/light/libxl_cpuid.c
@@ -19,10 +19,10 @@  int libxl__cpuid_policy_is_empty(libxl_cpuid_policy_list *pl)
     return !libxl_cpuid_policy_list_length(pl);
 }
 
-void libxl_cpuid_dispose(libxl_cpuid_policy_list *p_cpuid_list)
+void libxl_cpuid_dispose(libxl_cpuid_policy_list *policy)
 {
     int i, j;
-    libxl_cpuid_policy_list cpuid_list = *p_cpuid_list;
+    struct xc_xend_cpuid *cpuid_list = policy->cpuid;
 
     if (cpuid_list == NULL)
         return;
@@ -33,8 +33,8 @@  void libxl_cpuid_dispose(libxl_cpuid_policy_list *p_cpuid_list)
                 cpuid_list[i].policy[j] = NULL;
             }
     }
-    free(cpuid_list);
-    *p_cpuid_list = NULL;
+    free(policy->cpuid);
+    policy->cpuid = NULL;
     return;
 }
 
@@ -62,9 +62,10 @@  struct cpuid_flags {
 /* go through the dynamic array finding the entry for a specified leaf.
  * if no entry exists, allocate one and return that.
  */
-static libxl_cpuid_policy_list cpuid_find_match(libxl_cpuid_policy_list *list,
-                                          uint32_t leaf, uint32_t subleaf)
+static struct xc_xend_cpuid *cpuid_find_match(libxl_cpuid_policy *policy,
+                                              uint32_t leaf, uint32_t subleaf)
 {
+    struct xc_xend_cpuid **list = &policy->cpuid;
     int i = 0;
 
     if (*list != NULL) {
@@ -86,7 +87,7 @@  static libxl_cpuid_policy_list cpuid_find_match(libxl_cpuid_policy_list *list,
  * Will overwrite earlier entries and thus can be called multiple
  * times.
  */
-int libxl_cpuid_parse_config(libxl_cpuid_policy_list *cpuid, const char* str)
+int libxl_cpuid_parse_config(libxl_cpuid_policy_list *policy, const char* str)
 {
 #define NA XEN_CPUID_INPUT_UNUSED
     static const struct cpuid_flags cpuid_flags[] = {
@@ -345,7 +346,7 @@  int libxl_cpuid_parse_config(libxl_cpuid_policy_list *cpuid, const char* str)
     if (flag->name == NULL) {
         return 2;
     }
-    entry = cpuid_find_match(cpuid, flag->leaf, flag->subleaf);
+    entry = cpuid_find_match(policy, flag->leaf, flag->subleaf);
     resstr = entry->policy[flag->reg - 1];
     num = strtoull(val, &endptr, 0);
     flags[flag->length] = 0;
@@ -400,7 +401,7 @@  int libxl_cpuid_parse_config(libxl_cpuid_policy_list *cpuid, const char* str)
  * the strings for each register were directly exposed to the user.
  * Used for maintaining compatibility with older config files
  */
-int libxl_cpuid_parse_config_xend(libxl_cpuid_policy_list *cpuid,
+int libxl_cpuid_parse_config_xend(libxl_cpuid_policy_list *policy,
                                   const char* str)
 {
     char *endptr;
@@ -427,7 +428,7 @@  int libxl_cpuid_parse_config_xend(libxl_cpuid_policy_list *cpuid,
         return 3;
     }
     str = endptr + 1;
-    entry = cpuid_find_match(cpuid, leaf, subleaf);
+    entry = cpuid_find_match(policy, leaf, subleaf);
     for (str = endptr + 1; *str != 0;) {
         if (str[0] != 'e' || str[2] != 'x') {
             return 4;
@@ -502,7 +503,7 @@  int libxl__cpuid_legacy(libxl_ctx *ctx, uint32_t domid, bool restore,
             info->tsc_mode == LIBXL_TSC_MODE_ALWAYS_EMULATE);
 
     r = xc_cpuid_apply_policy(ctx->xch, domid, restore, NULL, 0,
-                              pae, itsc, nested_virt, info->cpuid, NULL);
+                              pae, itsc, nested_virt, info->cpuid.cpuid, NULL);
     if (r)
         LOGEVD(ERROR, -r, domid, "Failed to apply CPUID policy");
 
@@ -527,9 +528,9 @@  static const char *policy_names[4] = { "eax", "ebx", "ecx", "edx" };
  */
 
 yajl_gen_status libxl_cpuid_policy_list_gen_json(yajl_gen hand,
-                                libxl_cpuid_policy_list *pcpuid)
+                                libxl_cpuid_policy_list *policy)
 {
-    libxl_cpuid_policy_list cpuid = *pcpuid;
+    struct xc_xend_cpuid *cpuid = policy->cpuid;
     yajl_gen_status s;
     int i, j;
 
@@ -556,7 +557,8 @@  yajl_gen_status libxl_cpuid_policy_list_gen_json(yajl_gen hand,
                 s = libxl__yajl_gen_asciiz(hand, policy_names[j]);
                 if (s != yajl_gen_status_ok) goto out;
                 s = yajl_gen_string(hand,
-                               (const unsigned char *)cpuid[i].policy[j], 32);
+                               (const unsigned char *)cpuid[i].policy[j],
+                               32);
                 if (s != yajl_gen_status_ok) goto out;
             }
         }
@@ -575,7 +577,7 @@  int libxl__cpuid_policy_list_parse_json(libxl__gc *gc,
                                         libxl_cpuid_policy_list *p)
 {
     int i, size;
-    libxl_cpuid_policy_list l;
+    struct xc_xend_cpuid *l;
     flexarray_t *array;
 
     if (!libxl__json_object_is_array(o))
@@ -587,7 +589,8 @@  int libxl__cpuid_policy_list_parse_json(libxl__gc *gc,
 
     size = array->count;
     /* need one extra slot as sentinel */
-    l = *p = libxl__calloc(NOGC, size + 1, sizeof(libxl_cpuid_policy));
+    p->cpuid = libxl__calloc(NOGC, size + 1, sizeof(struct xc_xend_cpuid));
+    l = p->cpuid;
 
     l[size].input[0] = XEN_CPUID_INPUT_UNUSED;
     l[size].input[1] = XEN_CPUID_INPUT_UNUSED;
@@ -627,10 +630,10 @@  int libxl__cpuid_policy_list_parse_json(libxl__gc *gc,
     return 0;
 }
 
-int libxl_cpuid_policy_list_length(const libxl_cpuid_policy_list *pl)
+int libxl_cpuid_policy_list_length(const libxl_cpuid_policy_list *policy)
 {
     int i = 0;
-    libxl_cpuid_policy_list l = *pl;
+    struct xc_xend_cpuid *l = policy->cpuid;
 
     if (l) {
         while (l[i].input[0] != XEN_CPUID_INPUT_UNUSED)
@@ -641,9 +644,11 @@  int libxl_cpuid_policy_list_length(const libxl_cpuid_policy_list *pl)
 }
 
 void libxl_cpuid_policy_list_copy(libxl_ctx *ctx,
-                                  libxl_cpuid_policy_list *dst,
-                                  const libxl_cpuid_policy_list *src)
+                                  libxl_cpuid_policy_list *pdst,
+                                  const libxl_cpuid_policy_list *psrc)
 {
+    struct xc_xend_cpuid **dst = &pdst->cpuid;
+    struct xc_xend_cpuid *const *src = &psrc->cpuid;
     GC_INIT(ctx);
     int i, j, len;
 
@@ -652,9 +657,9 @@  void libxl_cpuid_policy_list_copy(libxl_ctx *ctx,
         goto out;
     }
 
-    len = libxl_cpuid_policy_list_length(src);
+    len = libxl_cpuid_policy_list_length(psrc);
     /* one extra slot for sentinel */
-    *dst = libxl__calloc(NOGC, len + 1, sizeof(libxl_cpuid_policy));
+    *dst = libxl__calloc(NOGC, len + 1, sizeof(struct xc_xend_cpuid));
     (*dst)[len].input[0] = XEN_CPUID_INPUT_UNUSED;
     (*dst)[len].input[1] = XEN_CPUID_INPUT_UNUSED;