Skip to contentSkip to conversation

Convert from bytestream to uint64_t and back

PATCH New Jimi Xenidis 1 participant 1 message Jan 17, 2007

lore
Share & tools
lore
#0 lore raw

PATCH 2/3 Convert from bytestream to uint64_t and back

---

tools/libxc/xc_byteorder.h new file +33 -0
diff -r 58d6c9cb95c6 tools/libxc/xc_byteorder.h
@@ -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
tools/libxc/xc_domain.c +13 -10
diff -r 58d6c9cb95c6 tools/libxc/xc_domain.c
@@ -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;
 }
tools/libxc/xc_private.c +54 -0

66 lines not on this page · raw message

tools/libxc/xc_private.h +3 -0
diff -r 58d6c9cb95c6 tools/libxc/xc_private.h
@@ -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__ */