diff mbox series

[3/7] tools/xg: Add self-(de)serialisation functions for cpu_policy

Message ID 20231107154921.54979-4-alejandro.vallejo@cloud.com (mailing list archive)
State New, archived
Headers show
Series Cleanup and code duplication removal in xenguest | expand

Commit Message

Alejandro Vallejo Nov. 7, 2023, 3:49 p.m. UTC
These allow a policy to internally (de)serialize itself, so that we don't
have to carry around serialization buffers when perfectly good ones are
present inside.

Both moved on top of the xend overrides as they are needed there in future
patches

Signed-off-by: Alejandro Vallejo <alejandro.vallejo@cloud.com>
---
 tools/libs/guest/xg_cpuid_x86.c | 90 ++++++++++++++++++---------------
 1 file changed, 49 insertions(+), 41 deletions(-)
diff mbox series

Patch

diff --git a/tools/libs/guest/xg_cpuid_x86.c b/tools/libs/guest/xg_cpuid_x86.c
index 3545f3e530..ac75ce2b4e 100644
--- a/tools/libs/guest/xg_cpuid_x86.c
+++ b/tools/libs/guest/xg_cpuid_x86.c
@@ -254,6 +254,50 @@  int xc_set_domain_cpu_policy(xc_interface *xch, uint32_t domid,
     return ret;
 }
 
+static int cpu_policy_deserialise_on_self(xc_interface *xch, xc_cpu_policy_t *p)
+{
+    uint32_t err_leaf = -1, err_subleaf = -1, err_msr = -1;
+    int rc;
+
+    rc = x86_cpuid_copy_from_buffer(&p->policy, p->leaves.buf, p->leaves.len,
+                                    &err_leaf, &err_subleaf);
+    if ( rc )
+    {
+        if ( err_leaf != -1 )
+            ERROR("Failed to deserialise CPUID (err leaf %#x, subleaf %#x) (%d = %s)",
+                  err_leaf, err_subleaf, -rc, strerror(-rc));
+        return rc;
+    }
+
+    rc = x86_msr_copy_from_buffer(&p->policy, p->msrs.buf, p->msrs.len, &err_msr);
+    if ( rc )
+    {
+        if ( err_msr != -1 )
+            ERROR("Failed to deserialise MSR (err MSR %#x) (%d = %s)",
+                  err_msr, -rc, strerror(-rc));
+        return rc;
+    }
+
+    return 0;
+}
+
+static int cpu_policy_serialise_on_self(xc_interface *xch, xc_cpu_policy_t *p)
+{
+    uint32_t nr_leaves = p->leaves.allocated;
+    uint32_t nr_msrs = p->msrs.allocated;
+    int rc = xc_cpu_policy_serialise(xch, p,
+                                     p->leaves.buf, &nr_leaves,
+                                     p->msrs.buf, &nr_msrs);
+
+    if ( !rc )
+    {
+        p->leaves.len = nr_leaves;
+        p->msrs.len = nr_msrs;
+    }
+
+    return rc;
+}
+
 static int compare_leaves(const void *l, const void *r)
 {
     const xen_cpuid_leaf_t *lhs = l;
@@ -883,35 +927,6 @@  void xc_cpu_policy_destroy(xc_cpu_policy_t *policy)
     errno = err;
 }
 
-static int deserialize_policy(xc_interface *xch, xc_cpu_policy_t *policy,
-                              unsigned int nr_leaves, unsigned int nr_entries)
-{
-    uint32_t err_leaf = -1, err_subleaf = -1, err_msr = -1;
-    int rc;
-
-    rc = x86_cpuid_copy_from_buffer(&policy->policy, policy->leaves.buf,
-                                    nr_leaves, &err_leaf, &err_subleaf);
-    if ( rc )
-    {
-        if ( err_leaf != -1 )
-            ERROR("Failed to deserialise CPUID (err leaf %#x, subleaf %#x) (%d = %s)",
-                  err_leaf, err_subleaf, -rc, strerror(-rc));
-        return rc;
-    }
-
-    rc = x86_msr_copy_from_buffer(&policy->policy, policy->msrs.buf,
-                                  nr_entries, &err_msr);
-    if ( rc )
-    {
-        if ( err_msr != -1 )
-            ERROR("Failed to deserialise MSR (err MSR %#x) (%d = %s)",
-                  err_msr, -rc, strerror(-rc));
-        return rc;
-    }
-
-    return 0;
-}
-
 int xc_cpu_policy_get_system(xc_interface *xch, unsigned int policy_idx,
                              xc_cpu_policy_t *policy)
 {
@@ -931,7 +946,7 @@  int xc_cpu_policy_get_system(xc_interface *xch, unsigned int policy_idx,
     policy->leaves.len = nr_leaves;
     policy->msrs.len = nr_msrs;
 
-    rc = deserialize_policy(xch, policy, nr_leaves, nr_msrs);
+    rc = cpu_policy_deserialise_on_self(xch, policy);
     if ( rc )
     {
         errno = -rc;
@@ -960,7 +975,7 @@  int xc_cpu_policy_get_domain(xc_interface *xch, uint32_t domid,
     policy->leaves.len = nr_leaves;
     policy->msrs.len = nr_msrs;
 
-    rc = deserialize_policy(xch, policy, nr_leaves, nr_msrs);
+    rc = cpu_policy_deserialise_on_self(xch, policy);
     if ( rc )
     {
         errno = -rc;
@@ -974,22 +989,15 @@  int xc_cpu_policy_set_domain(xc_interface *xch, uint32_t domid,
                              xc_cpu_policy_t *policy)
 {
     uint32_t err_leaf = -1, err_subleaf = -1, err_msr = -1;
-    unsigned int nr_leaves = policy->leaves.allocated;
-    unsigned int nr_msrs = policy->msrs.allocated;
     int rc;
 
-    rc = xc_cpu_policy_serialise(xch, policy,
-                                 policy->leaves.buf, &nr_leaves,
-                                 policy->msrs.buf, &nr_msrs);
+    rc = cpu_policy_serialise_on_self(xch, policy);
     if ( rc )
         return rc;
 
-    policy->leaves.len = nr_leaves;
-    policy->msrs.len = nr_msrs;
-
     rc = xc_set_domain_cpu_policy(xch, domid,
-                                  nr_leaves, policy->leaves.buf,
-                                  nr_msrs, policy->msrs.buf,
+                                  policy->leaves.len, policy->leaves.buf,
+                                  policy->msrs.len, policy->msrs.buf,
                                   &err_leaf, &err_subleaf, &err_msr);
     if ( rc )
     {