WARNING - OLD ARCHIVES

This is an archived copy of the Xen.org mailing list, which we have preserved to ensure that existing links to archives are not broken. The live archive, which contains the latest emails, can be found at http://lists.xen.org/
   
 
 
Xen 
 
Home Products Support Community News
 
   
 

xen-devel

[Xen-devel] [PATCH 2/3] [XEN][LIBXC] Convert from bytestream to uint64_t

To: xen-devel@xxxxxxxxxxxxxxxxxxx
Subject: [Xen-devel] [PATCH 2/3] [XEN][LIBXC] Convert from bytestream to uint64_t and back
From: Jimi Xenidis <jimix@xxxxxxxxxxxxxx>
Date: Wed, 17 Jan 2007 17:46:05 -0500
Cc: levon@xxxxxxxxxxxxxxxxx, xen-ppc-devel@xxxxxxxxxxxxxxxxxxx
Delivery-date: Wed, 17 Jan 2007 14:50:05 -0800
Envelope-to: www-data@xxxxxxxxxxxxxxxxxx
List-help: <mailto:xen-devel-request@lists.xensource.com?subject=help>
List-id: Xen developer discussion <xen-devel.lists.xensource.com>
List-post: <mailto:xen-devel@lists.xensource.com>
List-subscribe: <http://lists.xensource.com/cgi-bin/mailman/listinfo/xen-devel>, <mailto:xen-devel-request@lists.xensource.com?subject=subscribe>
List-unsubscribe: <http://lists.xensource.com/cgi-bin/mailman/listinfo/xen-devel>, <mailto:xen-devel-request@lists.xensource.com?subject=unsubscribe>
Sender: xen-devel-bounces@xxxxxxxxxxxxxxxxxxx
Signed-off-by: Jimi Xenidis <jimix@xxxxxxxxxxxxxx>

---

diff -r 58d6c9cb95c6 tools/libxc/xc_byteorder.h
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/tools/libxc/xc_byteorder.h        Wed Jan 17 16:42:03 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
diff -r 58d6c9cb95c6 tools/libxc/xc_domain.c
--- a/tools/libxc/xc_domain.c   Wed Jan 17 14:57:04 2007 -0500
+++ b/tools/libxc/xc_domain.c   Wed Jan 17 16:52:25 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 58d6c9cb95c6 tools/libxc/xc_private.c
--- a/tools/libxc/xc_private.c  Wed Jan 17 14:57:04 2007 -0500
+++ b/tools/libxc/xc_private.c  Wed Jan 17 17:08:08 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 58d6c9cb95c6 tools/libxc/xc_private.h
--- a/tools/libxc/xc_private.h  Wed Jan 17 14:57:04 2007 -0500
+++ b/tools/libxc/xc_private.h  Wed Jan 17 16:50:54 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__ */


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

<Prev in Thread] Current Thread [Next in Thread>
  • [Xen-devel] [PATCH 2/3] [XEN][LIBXC] Convert from bytestream to uint64_t and back, Jimi Xenidis <=