# HG changeset patch
# User Jimi Xenidis <[EMAIL PROTECTED]>
# Node ID cae803af2d571e7d44001207da361327db71ced4
# Parent  fa3a95fd876c75a48582db2916b915f985cd52ca
Converting from various data types to byte-stream bitmaps.

Note: this patch was submitted upstream.

The following patches handle endian issues with converstion to
byte-stream bitmaps from: uint32_t, unint64_t, and long arrays.

Tried to be Solaris/Sun friendly

Build tested on x86.

Signed-off-by: Jimi Xenidis <[EMAIL PROTECTED]>
---
 tools/libxc/xc_byteorder.h |   33 +++++++++++++++++++
 tools/libxc/xc_domain.c    |   23 +++++++------
 tools/libxc/xc_private.c   |   54 ++++++++++++++++++++++++++++++++
 tools/libxc/xc_private.h   |    3 +
 tools/libxc/xc_tbuf.c      |   18 ++++++++--
 xen/common/bitmap.c        |   75 +++++++++++++++++++++++++++++++++++++++++++++
 xen/common/domctl.c        |   15 +++++----
 xen/include/xen/bitmap.h   |    3 +
 8 files changed, 204 insertions(+), 20 deletions(-)

diff -r fa3a95fd876c -r cae803af2d57 tools/libxc/xc_domain.c
--- a/tools/libxc/xc_domain.c   Wed Jan 17 17:42:39 2007 -0500
+++ b/tools/libxc/xc_domain.c   Wed Jan 17 19:28:29 2007 -0500
@@ -96,16 +96,19 @@ int xc_vcpu_setaffinity(int xc_handle,
 {
     DECLARE_DOMCTL;
     int ret = -1;
+    uint8_t local[sizeof (cpumap)];
 
     domctl.cmd = XEN_DOMCTL_setvcpuaffinity;
     domctl.domain = (domid_t)domid;
     domctl.u.vcpuaffinity.vcpu    = vcpu;
 
-    set_xen_guest_handle(domctl.u.vcpuaffinity.cpumap.bitmap,
-                         (uint8_t *)&cpumap);
+    bitmap_64_to_byte(local, &cpumap, sizeof (cpumap));
+
+    set_xen_guest_handle(domctl.u.vcpuaffinity.cpumap.bitmap, local);
+
     domctl.u.vcpuaffinity.cpumap.nr_cpus = sizeof(cpumap) * 8;
     
-    if ( lock_pages(&cpumap, sizeof(cpumap)) != 0 )
+    if ( lock_pages(local, sizeof(local)) != 0 )
     {
         PERROR("Could not lock memory for Xen hypercall");
         goto out;
@@ -113,7 +116,7 @@ int xc_vcpu_setaffinity(int xc_handle,
 
     ret = do_domctl(xc_handle, &domctl);
 
-    unlock_pages(&cpumap, sizeof(cpumap));
+    unlock_pages(local, sizeof(local));
 
  out:
     return ret;
@@ -127,16 +130,16 @@ int xc_vcpu_getaffinity(int xc_handle,
 {
     DECLARE_DOMCTL;
     int ret = -1;
+    uint8_t local[sizeof (cpumap)];
 
     domctl.cmd = XEN_DOMCTL_getvcpuaffinity;
     domctl.domain = (domid_t)domid;
     domctl.u.vcpuaffinity.vcpu = vcpu;
 
-    set_xen_guest_handle(domctl.u.vcpuaffinity.cpumap.bitmap,
-                         (uint8_t *)cpumap);
-    domctl.u.vcpuaffinity.cpumap.nr_cpus = sizeof(*cpumap) * 8;
+    set_xen_guest_handle(domctl.u.vcpuaffinity.cpumap.bitmap, local);
+    domctl.u.vcpuaffinity.cpumap.nr_cpus = sizeof(cpumap) * 8;
     
-    if ( lock_pages(cpumap, sizeof(*cpumap)) != 0 )
+    if ( lock_pages(local, sizeof(local)) != 0 )
     {
         PERROR("Could not lock memory for Xen hypercall");
         goto out;
@@ -144,8 +147,8 @@ int xc_vcpu_getaffinity(int xc_handle,
 
     ret = do_domctl(xc_handle, &domctl);
 
-    unlock_pages(cpumap, sizeof(*cpumap));
-
+    unlock_pages(local, sizeof (local));
+    bitmap_byte_to_64(cpumap, local, sizeof (local));
  out:
     return ret;
 }
diff -r fa3a95fd876c -r cae803af2d57 tools/libxc/xc_private.c
--- a/tools/libxc/xc_private.c  Wed Jan 17 17:42:39 2007 -0500
+++ b/tools/libxc/xc_private.c  Wed Jan 17 19:28:29 2007 -0500
@@ -9,6 +9,7 @@
 #include "xg_private.h"
 #include <stdarg.h>
 #include <pthread.h>
+#include "xc_byteorder.h"
 
 static __thread xc_error last_error = { XC_ERROR_NONE, ""};
 #if DEBUG
@@ -502,6 +503,59 @@ char *safe_strerror(int errcode)
     return errbuf;
 }
 
+void *bitmap_64_to_byte(uint8_t *bp, const uint64_t *lp, size_t n)
+{
+#ifdef XC_BIG_ENDIAN
+    uint64_t l;
+    int i = 0;
+    int b = 0;
+
+    for (;;) {
+        l = cpu_to_le64(lp[i]);
+
+        if (n < sizeof (l))
+            break;
+
+        ((uint64_t *)bp)[b] = l;
+        ++i;
+        n -= sizeof (l);
+        b += sizeof (l);
+    }
+    /* don't call memcpy for 0 */
+    if (n > 0)
+        memcpy(&bp[b], &l, n);
+#else
+    memcpy(bp, lp, n);
+#endif
+
+    return bp;
+}
+
+void *bitmap_byte_to_64(uint64_t *lp, const uint8_t *bp, size_t n)
+{
+#ifdef XC_BIG_ENDIAN
+    uint64_t l;
+    int i = 0;
+    int b = 0;
+
+    while (n > sizeof (l)) {
+        l = ((uint64_t *)bp)[b];
+        lp[i] = le64_to_cpu(l);
+        ++i;
+        n -= sizeof (l);
+        b += sizeof (l);
+    }
+    if (n > 0) {
+        l = 0;
+        memcpy(&l, &bp[b], n);
+        lp[i] = le64_to_cpu(l);
+    }
+#else
+    memcpy(lp, bp, n);
+#endif
+    return lp;
+}
+
 /*
  * Local variables:
  * mode: C
diff -r fa3a95fd876c -r cae803af2d57 tools/libxc/xc_private.h
--- a/tools/libxc/xc_private.h  Wed Jan 17 17:42:39 2007 -0500
+++ b/tools/libxc/xc_private.h  Wed Jan 17 19:28:29 2007 -0500
@@ -157,4 +157,7 @@ int xc_waitdomain_core(int xc_handle, in
 int xc_waitdomain_core(int xc_handle, int domain, int *status,
     int options, vcpu_guest_context_t *ctxt);
 
+extern void *bitmap_64_to_byte(uint8_t *bp, const uint64_t *lp, size_t n);
+extern void *bitmap_byte_to_64(uint64_t *lp, const uint8_t *bp, size_t n);
+
 #endif /* __XC_PRIVATE_H__ */
diff -r fa3a95fd876c -r cae803af2d57 tools/libxc/xc_tbuf.c
--- a/tools/libxc/xc_tbuf.c     Wed Jan 17 17:42:39 2007 -0500
+++ b/tools/libxc/xc_tbuf.c     Wed Jan 17 19:28:29 2007 -0500
@@ -96,15 +96,25 @@ int xc_tbuf_set_cpu_mask(int xc_handle, 
 {
     DECLARE_SYSCTL;
     int ret = -1;
+    uint64_t mask64;
+    uint8_t local[sizeof (mask64)];
 
     sysctl.cmd = XEN_SYSCTL_tbuf_op;
     sysctl.interface_version = XEN_SYSCTL_INTERFACE_VERSION;
     sysctl.u.tbuf_op.cmd  = XEN_SYSCTL_TBUFOP_set_cpu_mask;
 
-    set_xen_guest_handle(sysctl.u.tbuf_op.cpu_mask.bitmap, (uint8_t *)&mask);
-    sysctl.u.tbuf_op.cpu_mask.nr_cpus = sizeof(mask) * 8;
+#ifdef XC_BIG_ENDIAN
+    mask64 = mask;
+#else
+    mask64 = (uint64_t)mask << 32;
+#endif
 
-    if ( lock_pages(&mask, sizeof(mask)) != 0 )
+    bitmap_64_to_byte(local, &mask64, sizeof (mask64));
+
+    set_xen_guest_handle(sysctl.u.tbuf_op.cpu_mask.bitmap, local);
+    sysctl.u.tbuf_op.cpu_mask.nr_cpus = sizeof(local) * 8;
+
+    if ( lock_pages(&local, sizeof(local)) != 0 )
     {
         PERROR("Could not lock memory for Xen hypercall");
         goto out;
@@ -112,7 +122,7 @@ int xc_tbuf_set_cpu_mask(int xc_handle, 
 
     ret = do_sysctl(xc_handle, &sysctl);
 
-    unlock_pages(&mask, sizeof(mask));
+    unlock_pages(&local, sizeof(local));
 
  out:
     return ret;
diff -r fa3a95fd876c -r cae803af2d57 xen/common/bitmap.c
--- a/xen/common/bitmap.c       Wed Jan 17 17:42:39 2007 -0500
+++ b/xen/common/bitmap.c       Wed Jan 17 19:28:29 2007 -0500
@@ -10,6 +10,7 @@
 #include <xen/errno.h>
 #include <xen/bitmap.h>
 #include <xen/bitops.h>
+#include <asm/byteorder.h>
 
 /*
  * bitmaps provide an array of bits, implemented using an an
@@ -467,3 +468,77 @@ int bitmap_allocate_region(unsigned long
        return 0;
 }
 EXPORT_SYMBOL(bitmap_allocate_region);
+
+static inline ulong ulong_to_le(ulong l)
+{
+#if BITS_PER_LONG == 32
+    return cpu_to_le32(l);
+#else
+    return cpu_to_le64(l);
+#endif
+
+}
+
+static inline ulong le_to_ulong(ulong l)
+{
+#if BITS_PER_LONG == 32
+    return le32_to_cpu(l);
+#else
+    return le64_to_cpu(l);
+#endif
+}
+
+void *bitmap_long_to_byte(uint8_t *bp, const ulong *lp, size_t n)
+{
+#ifdef __BIG_ENDIAN
+    ulong l;
+    int i = 0;
+    int b = 0;
+
+    for (;;) {
+        l = ulong_to_le(lp[i]);
+
+        if (n < sizeof (l))
+            break;
+
+        ((ulong *)bp)[b] = l;
+        ++i;
+        n -= sizeof (l);
+        b += sizeof (l);
+    }
+    /* don't call memcpy for 0 */
+    if (n > 0)
+        memcpy(&bp[b], &l, n);
+#else
+    memcpy(bp, lp, n);
+#endif
+
+    return bp;
+}
+EXPORT_SYMBOL(bitmap_long_to_byte);
+
+void *bitmap_byte_to_long(ulong *lp, const uint8_t *bp, size_t n)
+{
+#ifdef __BIG_ENDIAN
+    ulong l;
+    int i = 0;
+    int b = 0;
+
+    while (n > sizeof (l)) {
+        l = ((ulong *)bp)[b];
+        lp[i] = le_to_ulong(l);
+        ++i;
+        n -= sizeof (l);
+        b += sizeof (l);
+    }
+    if (n > 0) {
+        l = 0;
+        memcpy(&l, &bp[b], n);
+        lp[i] = le_to_ulong(l);
+    }
+#else
+    memcpy(lp, bp, n);
+#endif
+    return lp;
+}
+EXPORT_SYMBOL(bitmap_byte_to_long);
diff -r fa3a95fd876c -r cae803af2d57 xen/common/domctl.c
--- a/xen/common/domctl.c       Wed Jan 17 17:42:39 2007 -0500
+++ b/xen/common/domctl.c       Wed Jan 17 19:28:29 2007 -0500
@@ -18,6 +18,7 @@
 #include <xen/console.h>
 #include <xen/iocap.h>
 #include <xen/guest_access.h>
+#include <xen/bitmap.h>
 #ifdef CONFIG_COMPAT
 #include <xen/compat.h>
 #endif
@@ -40,6 +41,7 @@ void cpumask_to_xenctl_cpumap(
 {
     unsigned int guest_bytes, copy_bytes, i;
     uint8_t zero = 0;
+    uint8_t local[sizeof (*cpumask)];
 
     if ( guest_handle_is_null(xenctl_cpumap->bitmap) )
         return;
@@ -47,9 +49,9 @@ void cpumask_to_xenctl_cpumap(
     guest_bytes = (xenctl_cpumap->nr_cpus + 7) / 8;
     copy_bytes  = min_t(unsigned int, guest_bytes, (NR_CPUS + 7) / 8);
 
-    copy_to_guest(xenctl_cpumap->bitmap,
-                  (uint8_t *)cpus_addr(*cpumask),
-                  copy_bytes);
+    bitmap_long_to_byte(local, cpus_addr(*cpumask), copy_bytes);
+
+    copy_to_guest(xenctl_cpumap->bitmap, &local[0], copy_bytes);
 
     for ( i = copy_bytes; i < guest_bytes; i++ )
         copy_to_guest_offset(xenctl_cpumap->bitmap, i, &zero, 1);
@@ -59,6 +61,7 @@ void xenctl_cpumap_to_cpumask(
     cpumask_t *cpumask, struct xenctl_cpumap *xenctl_cpumap)
 {
     unsigned int guest_bytes, copy_bytes;
+    uint8_t local[sizeof (*cpumask)];
 
     guest_bytes = (xenctl_cpumap->nr_cpus + 7) / 8;
     copy_bytes  = min_t(unsigned int, guest_bytes, (NR_CPUS + 7) / 8);
@@ -68,9 +71,9 @@ void xenctl_cpumap_to_cpumask(
     if ( guest_handle_is_null(xenctl_cpumap->bitmap) )
         return;
 
-    copy_from_guest((uint8_t *)cpus_addr(*cpumask),
-                    xenctl_cpumap->bitmap,
-                    copy_bytes);
+    copy_from_guest(&local[0], xenctl_cpumap->bitmap, copy_bytes);
+
+    bitmap_byte_to_long(cpus_addr(*cpumask), local, copy_bytes);
 }
 
 #endif /* COMPAT */
diff -r fa3a95fd876c -r cae803af2d57 xen/include/xen/bitmap.h
--- a/xen/include/xen/bitmap.h  Wed Jan 17 17:42:39 2007 -0500
+++ b/xen/include/xen/bitmap.h  Wed Jan 17 19:28:29 2007 -0500
@@ -251,6 +251,9 @@ static inline void bitmap_shift_left(uns
                __bitmap_shift_left(dst, src, n, nbits);
 }
 
+extern void *bitmap_long_to_byte(uint8_t *bp, const ulong *lp, size_t n);
+extern void *bitmap_byte_to_long(ulong *lp, const uint8_t *bp, size_t n);
+
 #endif /* __ASSEMBLY__ */
 
 #endif /* __XEN_BITMAP_H */
diff -r fa3a95fd876c -r cae803af2d57 tools/libxc/xc_byteorder.h
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/tools/libxc/xc_byteorder.h        Wed Jan 17 19:28:29 2007 -0500
@@ -0,0 +1,33 @@
+#ifdef __sun__
+#include <sys/byteorder.h>
+#define bswap_8  BSWAP_8
+#define bswap_16 BSWAP_16
+#define bswap_32 BSWAP_32
+#define bswap_64 BSWAP_64
+#else
+#include <endian.h>
+#include <byteswap.h>
+#endif
+
+/*
+ * define as needed
+ */
+
+#if (__BYTE_ORDER == __BIG_ENDIAN) || defined(_BIG_ENDIAN)
+#define XC_BIG_ENDIAN
+
+#define cpu_to_le64(x) bswap_64(x)
+#define le64_to_cpu(x) bswap_64(x)
+#define cpu_to_le32(x) bswap_32(x)
+#define le32_to_cpu(x) bswap_32(x)
+
+#elif (__BYTE_ORDER == __LITTLE_ENDIAN) || defined(_LITTLE_ENDIAN)
+
+#define XC_LITTLE_ENDIAN
+
+#define cpu_to_le64(x) (x)
+#define le64_to_cpu(x) (x)
+#define cpu_to_le32(x) (x)
+#define le32_to_cpu(x) (x)
+
+#endif

_______________________________________________
Xen-ppc-devel mailing list
Xen-ppc-devel@lists.xensource.com
http://lists.xensource.com/xen-ppc-devel

Reply via email to