[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Xen-devel] [PATCH 11/13] libx86: Introduce a helper to deserialise a cpuid_policy object



As with the serialise side, Xen's copy_from_guest API is used, with a
compatibility wrapper for the userspace build.

Signed-off-by: Andrew Cooper <andrew.cooper3@xxxxxxxxxx>
Signed-off-by: Sergey Dyasli <sergey.dyasli@xxxxxxxxxx>
Signed-off-by: Roger Pau Monné <roger.pau@xxxxxxxxxx>
---
CC: Jan Beulich <JBeulich@xxxxxxxx>
CC: Wei Liu <wei.liu2@xxxxxxxxxx>
CC: Roger Pau Monné <roger.pau@xxxxxxxxxx>
CC: Sergey Dyasli <sergey.dyasli@xxxxxxxxxx>
CC: Ian Jackson <Ian.Jackson@xxxxxxxxxxxxx>
---
 xen/common/libx86/cpuid.c          | 88 ++++++++++++++++++++++++++++++++++++++
 xen/common/libx86/libx86-private.h | 12 ++++++
 xen/include/xen/libx86/cpuid.h     |  4 ++
 3 files changed, 104 insertions(+)

diff --git a/xen/common/libx86/cpuid.c b/xen/common/libx86/cpuid.c
index d38c505..659cd6e 100644
--- a/xen/common/libx86/cpuid.c
+++ b/xen/common/libx86/cpuid.c
@@ -128,6 +128,94 @@ int x86_cpuid_copy_to_buffer(const struct cpuid_policy *p,
 }
 
 /*
+ * Copy CPUID data from a buffer, filling in a cpuid_policy object.  Performs
+ * boudary checking of the incomming leaves before filling the appropriate
+ * policy unions, but no content validation is performed.
+ */
+int x86_cpuid_copy_from_buffer(struct cpuid_policy *p,
+                               const cpuid_leaf_buffer_t leaves,
+                               uint32_t nr_leaves, uint32_t *err_leaf,
+                               uint32_t *err_subleaf)
+{
+    unsigned int i;
+    xen_cpuid_leaf_t data;
+    struct cpuid_leaf *l = (void *)&data.a;
+
+    if ( nr_leaves > CPUID_MAX_SERIALISED_LEAVES )
+        return -E2BIG;
+
+    for ( i = 0; i < nr_leaves; ++i )
+    {
+        if ( copy_from_buffer_offset(&data, leaves, i, 1) )
+            return -EFAULT;
+
+        switch ( data.leaf )
+        {
+        case 0 ... ARRAY_SIZE(p->basic.raw) - 1:
+            switch ( data.leaf )
+            {
+            case 0x4:
+                if ( data.subleaf >= ARRAY_SIZE(p->cache.raw) )
+                    goto out_of_range;
+
+                p->cache.raw[data.subleaf] = *l;
+                break;
+
+            case 0x7:
+                if ( data.subleaf >= ARRAY_SIZE(p->feat.raw) )
+                    goto out_of_range;
+
+                p->feat.raw[data.subleaf] = *l;
+                break;
+
+            case 0xb:
+                if ( data.subleaf >= ARRAY_SIZE(p->topo.raw) )
+                    goto out_of_range;
+
+                p->topo.raw[data.subleaf] = *l;
+                break;
+
+            case 0xd:
+                if ( data.subleaf >= ARRAY_SIZE(p->xstate.raw) )
+                    goto out_of_range;
+
+                p->xstate.raw[data.leaf] = *l;
+                break;
+
+            default:
+                p->basic.raw[data.leaf] = *l;
+                break;
+            }
+            break;
+
+        case 0x40000000:
+            p->hv_limit = l->a;
+            break;
+
+        case 0x40000100:
+            p->hv2_limit = l->a;
+            break;
+
+        case 0x80000000 ... 0x80000000 + ARRAY_SIZE(p->extd.raw) - 1:
+            p->extd.raw[data.leaf & 0xffff] = *l;
+            break;
+
+        default:
+            goto out_of_range;
+        }
+    }
+
+    return 0;
+
+ out_of_range:
+    if ( err_leaf )
+        *err_leaf = data.leaf;
+    if ( err_subleaf )
+        *err_subleaf = data.subleaf;
+    return -ERANGE;
+}
+
+/*
  * Local variables:
  * mode: C
  * c-file-style: "BSD"
diff --git a/xen/common/libx86/libx86-private.h 
b/xen/common/libx86/libx86-private.h
index e4ead1a..04a0865 100644
--- a/xen/common/libx86/libx86-private.h
+++ b/xen/common/libx86/libx86-private.h
@@ -12,6 +12,7 @@
 #include <asm/msr-index.h>
 
 #define copy_to_buffer_offset copy_to_guest_offset
+#define copy_from_buffer_offset copy_from_guest_offset
 
 #else
 
@@ -47,6 +48,17 @@ static inline bool test_bit(unsigned int bit, const void 
*vaddr)
     0;                                                  \
 })
 
+/* memcpy(), but with copy_from_guest_offset()'s API */
+#define copy_from_buffer_offset(dst, src, index, nr) ({ \
+    const typeof(*(dst)) *s = (src);                    \
+    unsigned int i;                                     \
+                                                        \
+    for ( i = 0; i < (nr); i++ )                        \
+        (dst)[i] = s[(index) + i];                      \
+                                                        \
+    0;                                                  \
+})
+
 #endif /* __XEN__ */
 
 #endif /* XEN_LIBX86_PRIVATE_H */
diff --git a/xen/include/xen/libx86/cpuid.h b/xen/include/xen/libx86/cpuid.h
index 48ce48d..0e71016 100644
--- a/xen/include/xen/libx86/cpuid.h
+++ b/xen/include/xen/libx86/cpuid.h
@@ -254,6 +254,10 @@ typedef xen_cpuid_leaf_t *cpuid_leaf_buffer_t;
 int x86_cpuid_copy_to_buffer(const struct cpuid_policy *p,
                              cpuid_leaf_buffer_t leaves,
                              uint32_t *nr_entries_p);
+int x86_cpuid_copy_from_buffer(struct cpuid_policy *p,
+                               const cpuid_leaf_buffer_t leaves,
+                               uint32_t nr_leaves, uint32_t *err_leaf,
+                               uint32_t *err_subleaf);
 
 #endif /* !XEN_LIBX86_CPUID_H */
 
-- 
2.1.4


_______________________________________________
Xen-devel mailing list
Xen-devel@xxxxxxxxxxxxxxxxxxxx
https://lists.xenproject.org/mailman/listinfo/xen-devel

 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.