Skip to contentSkip to conversation

xen/gntdev: add ioctl for grant copy

PATCH Superseded David Vrabel 2 participants 3 messages Nov 27, 2015 – Dec 1, 2015 Xen hypervisor interface

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 xen/gntdev: add ioctl for grant copy

Add IOCTL_GNTDEV_GRANT_COPY to allow applications to copy between user space buffers and grant references.

This interface is similar to the GNTTABOP_copy hypercall ABI except the local buffers are provided using a virtual address (instead of a GFN and offset). To avoid userspace from having to page align its buffers the driver will use two or more ops if required.

If the ioctl returns 0, the application must check the status of each segment with the segments status field. If the ioctl returns a -ve error code (EINVAL or EFAULT), the status of individual ops is undefined.

---
v3:
- Rewrite with different API that matches the capabilities of the hypervisor ABI and eliminates some of the size/alignment restrictions.

---
 drivers/xen/gntdev.c      | 193 ++++++++++++++++++++++++++++++++++++++++++++++
 include/uapi/xen/gntdev.h |  47 +++++++++++
 2 files changed, 240 insertions(+)
drivers/xen/gntdev.c +193 -0
index 2ea0b3b..5d78c79 100644
@@ -748,6 +748,196 @@ static long gntdev_ioctl_notify(struct gntdev_priv *priv, void __user *u)
 	return rc;
 }
 
+#define GNTDEV_COPY_BATCH 16
+
+struct gntdev_copy_batch {
+	struct gnttab_copy ops[GNTDEV_COPY_BATCH];
+	struct page *pages[2 * GNTDEV_COPY_BATCH];
+	s16 __user *status[GNTDEV_COPY_BATCH];
+	unsigned int nr_ops;
+	unsigned int nr_pages;
+};
+
+static int gntdev_get_page(struct gntdev_copy_batch *batch, void __user *virt,
+			   unsigned long *gfn)
+{
+	unsigned long addr = (unsigned long)virt;
+	struct page *page;
+	unsigned long xen_pfn;
+	int ret;
+
+	ret = get_user_pages_fast(addr, 1, 1, &page);
+	if (ret < 0)
+		return ret;
+
+	batch->pages[batch->nr_pages++] = page;
+
+	xen_pfn = page_to_xen_pfn(page) + XEN_PFN_DOWN(addr & ~PAGE_MASK);
+	*gfn = pfn_to_gfn(xen_pfn);
+
+	return 0;
+}
+
+static void gntdev_put_pages(struct gntdev_copy_batch *batch)
+{
+	unsigned int i;
+
+	for (i = 0; i < batch->nr_pages; i++)
+		put_page(batch->pages[i]);
+	batch->nr_pages = 0;
+}
+
+static int gntdev_copy(struct gntdev_copy_batch *batch)
+{
+	unsigned int i;
+
+	gnttab_batch_copy(batch->ops, batch->nr_ops);
+	gntdev_put_pages(batch);
+
+	/*
+	 * For each completed op, update the status if the op failed
+	 * and a previous failure for the segment hasn't been
+	 * recorded.
+	 */
+	for (i = 0; i < batch->nr_ops; i++) {
+		s16 status = batch->ops[i].status;
+		s16 old_status;
+
+		if (status == GNTST_okay)
+			continue;
+
+		if (__get_user(old_status, batch->status[i]))
+			return -EFAULT;
+
+		if (old_status != GNTST_okay)
+			continue;
+
+		if (__put_user(status, batch->status[i]))
+			return -EFAULT;
+	}
+
+	batch->nr_ops = 0;
+	return 0;
+}
+
+static int gntdev_grant_copy_seg(struct gntdev_copy_batch *batch,
+				 struct gntdev_grant_copy_segment *seg,
+				 s16 __user *status)
+{
+	uint16_t copied = 0;
+
+	/* Can't cross page if source/dest is a grant ref. */
+	if (seg->flags & GNTCOPY_source_gref) {
+		if (seg->source.foreign.offset + seg->len > XEN_PAGE_SIZE)
+			return -EINVAL;
+	}
+	if (seg->flags & GNTCOPY_dest_gref) {
+		if (seg->dest.foreign.offset + seg->len > XEN_PAGE_SIZE)
+			return -EINVAL;
+	}
+
+	if (put_user(GNTST_okay, status))
+		return -EFAULT;
+
+	while (copied < seg->len) {
+		struct gnttab_copy *op;
+		void __user *virt;
+		size_t len, off;
+		unsigned long gfn;
+		int ret;
+
+		if (batch->nr_ops >= GNTDEV_COPY_BATCH) {
+			ret = gntdev_copy(batch);
+			if (ret < 0)
+				return ret;
+		}
+
+		len = seg->len - copied;
+
+		op = &batch->ops[batch->nr_ops];
+		op->flags = 0;
+
+		if (seg->flags & GNTCOPY_source_gref) {
+			op->source.u.ref = seg->source.foreign.ref;
+			op->source.domid = seg->source.foreign.domid;
+			op->source.offset = seg->source.foreign.offset + copied;
+			op->flags |= GNTCOPY_source_gref;
+		} else {
+			virt = seg->source.virt + copied;
+			off = (unsigned long)virt & ~XEN_PAGE_MASK;
+			len = min(len, XEN_PAGE_SIZE - off);
+
+			ret = gntdev_get_page(batch, virt, &gfn);
+			if (ret < 0)
+				return ret;
+
+			op->source.u.gmfn = gfn;
+			op->source.domid = DOMID_SELF;
+			op->source.offset = off;
+		}
+
+		if (seg->flags & GNTCOPY_dest_gref) {
+			op->dest.u.ref = seg->dest.foreign.ref;
+			op->dest.domid = seg->dest.foreign.domid;
+			op->dest.offset = seg->dest.foreign.offset + copied;
+			op->flags |= GNTCOPY_dest_gref;
+		} else {
+			virt = seg->dest.virt + copied;
+			off = (unsigned long)virt & ~XEN_PAGE_MASK;
+			len = min(len, XEN_PAGE_SIZE - off);
+
+			ret = gntdev_get_page(batch, virt, &gfn);
+			if (ret < 0)
+				return ret;
+
+			op->dest.u.gmfn = gfn;
+			op->dest.domid = DOMID_SELF;
+			op->dest.offset = off;
+		}
+
+		op->len = len;
+		copied += len;
+
+		batch->status[batch->nr_ops] = status;
+		batch->nr_ops++;
+	}
+
+	return 0;
+}
+
+static long gntdev_ioctl_grant_copy(struct gntdev_priv *priv, void __user *u)
+{
+	struct ioctl_gntdev_grant_copy copy;
+	struct gntdev_copy_batch batch = { .nr_ops = 0, .nr_pages = 0, };
+	unsigned int i;
+	int ret = 0;
+
+	if (copy_from_user(&copy, u, sizeof(copy)))
+		return -EFAULT;
+
+	for (i = 0; i < copy.count; i++) {
+		struct gntdev_grant_copy_segment seg;
+
+		if (copy_from_user(&seg, &copy.segments[i], sizeof(seg))) {
+			ret = -EFAULT;
+			goto out;
+		}
+
+		ret = gntdev_grant_copy_seg(&batch, &seg, &copy.segments[i].status);
+		if (ret < 0)
+			goto out;
+
+		cond_resched();
+	}
+	if (batch.nr_ops)
+		ret = gntdev_copy(&batch);
+	return ret;
+
+  out:
+	gntdev_put_pages(&batch);
+	return ret;
+}
+
 static long gntdev_ioctl(struct file *flip,
 			 unsigned int cmd, unsigned long arg)
 {
@@ -767,6 +957,9 @@ static long gntdev_ioctl(struct file *flip,
 	case IOCTL_GNTDEV_SET_UNMAP_NOTIFY:
 		return gntdev_ioctl_notify(priv, ptr);
 
+	case IOCTL_GNTDEV_GRANT_COPY:
+		return gntdev_ioctl_grant_copy(priv, ptr);
+
 	default:
 		pr_debug("priv %p, unknown cmd %x\n", priv, cmd);
 		return -ENOIOCTLCMD;
include/uapi/xen/gntdev.h +47 -0

53 lines not on this page · raw message

Signature · 1 line
2.1.4
#1 lore raw
Quoted 8 lines · David Vrabel

Add IOCTL_GNTDEV_GRANT_COPY to allow applications to copy between user space buffers and grant references.

This interface is similar to the GNTTABOP_copy hypercall ABI except the local buffers are provided using a virtual address (instead of a GFN and offset). To avoid userspace from having to page align its buffers the driver will use two or more ops if required.

Quoted 4 lines

If the ioctl returns 0, the application must check the status of each segment with the segments status field. If the ioctl returns a -ve error code (EINVAL or EFAULT), the status of individual ops is undefined.

Are there any test tools that could be used for this? To make sure that regression wise this does not get broken?

Quoted 29 lines

---
v3:
- Rewrite with different API that matches the capabilities of the hypervisor ABI and eliminates some of the size/alignment restrictions.

---
 drivers/xen/gntdev.c      | 193 ++++++++++++++++++++++++++++++++++++++++++++++
 include/uapi/xen/gntdev.h |  47 +++++++++++
 2 files changed, 240 insertions(+)
drivers/xen/gntdev.c +9 -0
index 2ea0b3b..5d78c79 100644
@@ -748,6 +748,196 @@ static long gntdev_ioctl_notify(struct gntdev_priv *priv, void __user *u)
 	return rc;
 }
 
+#define GNTDEV_COPY_BATCH 16
+
+struct gntdev_copy_batch {
+	struct gnttab_copy ops[GNTDEV_COPY_BATCH];
+	struct page *pages[2 * GNTDEV_COPY_BATCH];
+	s16 __user *status[GNTDEV_COPY_BATCH];
+	unsigned int nr_ops;
+	unsigned int nr_pages;
+};

drivers/xen/gntdev.c static long gntdev_ioctl_notify(struct gntdev_priv *priv, void __user *u) · line 769

Quoted 10 lines
drivers/xen/gntdev.c +10 -0
+
+static int gntdev_get_page(struct gntdev_copy_batch *batch, void __user *virt,
+			   unsigned long *gfn)
+{
+	unsigned long addr = (unsigned long)virt;
+	struct page *page;
+	unsigned long xen_pfn;
+	int ret;
+
+	ret = get_user_pages_fast(addr, 1, 1, &page);

Could the '1,1' have a comment please?

Quoted 21 lines
drivers/xen/gntdev.c +21 -0
+	if (ret < 0)
+		return ret;
+
+	batch->pages[batch->nr_pages++] = page;
+
+	xen_pfn = page_to_xen_pfn(page) + XEN_PFN_DOWN(addr & ~PAGE_MASK);
+	*gfn = pfn_to_gfn(xen_pfn);
+
+	return 0;
+}
+
+static void gntdev_put_pages(struct gntdev_copy_batch *batch)
+{
+	unsigned int i;
+
+	for (i = 0; i < batch->nr_pages; i++)
+		put_page(batch->pages[i]);
+	batch->nr_pages = 0;
+}
+
+static int gntdev_copy(struct gntdev_copy_batch *batch)

drivers/xen/gntdev.c static long gntdev_ioctl_notify(struct gntdev_priv *priv, void __user *u) · line 800

Quoted 10 lines
drivers/xen/gntdev.c +10 -0
+{
+	unsigned int i;
+
+	gnttab_batch_copy(batch->ops, batch->nr_ops);
+	gntdev_put_pages(batch);
+
+	/*
+	 * For each completed op, update the status if the op failed
+	 * and a previous failure for the segment hasn't been
+	 * recorded.

How could an previous failure not be recorded? Could you mention that in this nice comment please?

Quoted 107 lines
drivers/xen/gntdev.c +107 -0
+	 */
+	for (i = 0; i < batch->nr_ops; i++) {
+		s16 status = batch->ops[i].status;
+		s16 old_status;
+
+		if (status == GNTST_okay)
+			continue;
+
+		if (__get_user(old_status, batch->status[i]))
+			return -EFAULT;
+
+		if (old_status != GNTST_okay)
+			continue;
+
+		if (__put_user(status, batch->status[i]))
+			return -EFAULT;
+	}
+
+	batch->nr_ops = 0;
+	return 0;
+}
+
+static int gntdev_grant_copy_seg(struct gntdev_copy_batch *batch,
+				 struct gntdev_grant_copy_segment *seg,
+				 s16 __user *status)
+{
+	uint16_t copied = 0;
+
+	/* Can't cross page if source/dest is a grant ref. */
+	if (seg->flags & GNTCOPY_source_gref) {
+		if (seg->source.foreign.offset + seg->len > XEN_PAGE_SIZE)
+			return -EINVAL;
+	}
+	if (seg->flags & GNTCOPY_dest_gref) {
+		if (seg->dest.foreign.offset + seg->len > XEN_PAGE_SIZE)
+			return -EINVAL;
+	}
+
+	if (put_user(GNTST_okay, status))
+		return -EFAULT;
+
+	while (copied < seg->len) {
+		struct gnttab_copy *op;
+		void __user *virt;
+		size_t len, off;
+		unsigned long gfn;
+		int ret;
+
+		if (batch->nr_ops >= GNTDEV_COPY_BATCH) {
+			ret = gntdev_copy(batch);
+			if (ret < 0)
+				return ret;
+		}
+
+		len = seg->len - copied;
+
+		op = &batch->ops[batch->nr_ops];
+		op->flags = 0;
+
+		if (seg->flags & GNTCOPY_source_gref) {
+			op->source.u.ref = seg->source.foreign.ref;
+			op->source.domid = seg->source.foreign.domid;
+			op->source.offset = seg->source.foreign.offset + copied;
+			op->flags |= GNTCOPY_source_gref;
+		} else {
+			virt = seg->source.virt + copied;
+			off = (unsigned long)virt & ~XEN_PAGE_MASK;
+			len = min(len, XEN_PAGE_SIZE - off);
+
+			ret = gntdev_get_page(batch, virt, &gfn);
+			if (ret < 0)
+				return ret;
+
+			op->source.u.gmfn = gfn;
+			op->source.domid = DOMID_SELF;
+			op->source.offset = off;
+		}
+
+		if (seg->flags & GNTCOPY_dest_gref) {
+			op->dest.u.ref = seg->dest.foreign.ref;
+			op->dest.domid = seg->dest.foreign.domid;
+			op->dest.offset = seg->dest.foreign.offset + copied;
+			op->flags |= GNTCOPY_dest_gref;
+		} else {
+			virt = seg->dest.virt + copied;
+			off = (unsigned long)virt & ~XEN_PAGE_MASK;
+			len = min(len, XEN_PAGE_SIZE - off);
+
+			ret = gntdev_get_page(batch, virt, &gfn);
+			if (ret < 0)
+				return ret;
+
+			op->dest.u.gmfn = gfn;
+			op->dest.domid = DOMID_SELF;
+			op->dest.offset = off;
+		}
+
+		op->len = len;
+		copied += len;
+
+		batch->status[batch->nr_ops] = status;
+		batch->nr_ops++;
+	}
+
+	return 0;
+}
+

drivers/xen/gntdev.c static long gntdev_ioctl_notify(struct gntdev_priv *priv, void __user *u)

Quoted 10 lines
drivers/xen/gntdev.c +9 -0
+static long gntdev_ioctl_grant_copy(struct gntdev_priv *priv, void __user *u)
+{
+	struct ioctl_gntdev_grant_copy copy;
+	struct gntdev_copy_batch batch = { .nr_ops = 0, .nr_pages = 0, };
+	unsigned int i;
+	int ret = 0;
+
+	if (copy_from_user(&copy, u, sizeof(copy)))
+		return -EFAULT;
 

+

No check on the .nr_pages' ? What if it is 0xfffffffffffffffffffffffff?

Ditto for .nr_ops?

Quoted 97 lines
drivers/xen/gntdev.c +26 -0
+	for (i = 0; i < copy.count; i++) {
+		struct gntdev_grant_copy_segment seg;
+
+		if (copy_from_user(&seg, &copy.segments[i], sizeof(seg))) {
+			ret = -EFAULT;
+			goto out;
+		}
+
+		ret = gntdev_grant_copy_seg(&batch, &seg, &copy.segments[i].status);
+		if (ret < 0)
+			goto out;
+
+		cond_resched();
+	}
+	if (batch.nr_ops)
+		ret = gntdev_copy(&batch);
+	return ret;
+
+  out:
+	gntdev_put_pages(&batch);
+	return ret;
+}
+
 static long gntdev_ioctl(struct file *flip,
 			 unsigned int cmd, unsigned long arg)
 {
@@ -767,6 +957,9 @@ static long gntdev_ioctl(struct file *flip,
 	case IOCTL_GNTDEV_SET_UNMAP_NOTIFY:
 		return gntdev_ioctl_notify(priv, ptr);
 
+	case IOCTL_GNTDEV_GRANT_COPY:
+		return gntdev_ioctl_grant_copy(priv, ptr);
+
 	default:
 		pr_debug("priv %p, unknown cmd %x\n", priv, cmd);
 		return -ENOIOCTLCMD;
include/uapi/xen/gntdev.h +47 -0
index aa7610a..4b02343 100644
@@ -144,6 +144,53 @@ struct ioctl_gntdev_unmap_notify {
 	__u32 event_channel_port;
 };
 
+struct gntdev_grant_copy_segment {
+	union {
+		void __user *virt;
+		struct {
+			grant_ref_t ref;
+			__u16 offset;
+			domid_t domid;
+		} foreign;
+	} source, dest;
+	__u16 len;
+
+	__u16 flags;  /* GNTCOPY_* */
+	__s16 status; /* GNTST_* */
+};
+
+/*
+ * Copy between grant references and local buffers.
+ *
+ * The copy is split into @count @segments, each of which can copy
+ * to/from one grant reference.
+ *
+ * Each segment is similar to struct gnttab_copy in the hypervisor ABI
+ * except the local buffer is specified using a virtual address
+ * (instead of a GFN and offset).
+ *
+ * The local buffer may cross a Xen page boundary -- the driver will
+ * split segments into multiple ops if required.
+ *
+ * Returns 0 if all segments have been processed and @status in each
+ * segment is valid.  Note that one or more segments may have failed
+ * (status != GNTST_okay).
+ *
+ * If the driver had to split a segment into two or more ops, @status
+ * includes the status of the first failed op for that segment (or
+ * GNTST_okay if all ops were successful).
+ *
+ * If -1 is returned, the status of all segments is undefined.
+ *
+ * - EINVAL: a segment crosses the boundary of a foreign page.
+ */
+#define IOCTL_GNTDEV_GRANT_COPY \
+	_IOC(_IOC_NONE, 'G', 8, sizeof(struct ioctl_gntdev_grant_copy))
+struct ioctl_gntdev_grant_copy {
+	unsigned int count;
+	struct gntdev_grant_copy_segment __user *segments;
+};
+
 /* Clear (set to zero) the byte specified by index */
 #define UNMAP_NOTIFY_CLEAR_BYTE 0x1
 /* Send an interrupt on the indicated event channel */
Signature · 1 line
2.1.4
#2 lore raw
Quoted 9 lines · Konrad Rzeszutek Wilk
Quoted 8 lines · David Vrabel

Add IOCTL_GNTDEV_GRANT_COPY to allow applications to copy between user space buffers and grant references.

This interface is similar to the GNTTABOP_copy hypercall ABI except the local buffers are provided using a virtual address (instead of a GFN and offset). To avoid userspace from having to page align its buffers the driver will use two or more ops if required.

Quoted 7 lines
Quoted 4 lines

If the ioctl returns 0, the application must check the status of each segment with the segments status field. If the ioctl returns a -ve error code (EINVAL or EFAULT), the status of individual ops is undefined.

Are there any test tools that could be used for this? To make sure that regression wise this does not get broken?

See attached.

Quoted 6 lines
Quoted 6 lines
drivers/xen/gntdev.c +6 -0
+static int gntdev_copy(struct gntdev_copy_batch *batch)
+{
+	unsigned int i;
+
+	gnttab_batch_copy(batch->ops, batch->nr_ops);
+	gntdev_put_pages(batch);
Quoted 8 lines

drivers/xen/gntdev.c static long gntdev_ioctl_notify(struct gntdev_priv *priv, void __user *u) · line 800

Quoted 5 lines
drivers/xen/gntdev.c +5 -0
+
+	/*
+	 * For each completed op, update the status if the op failed
+	 * and a previous failure for the segment hasn't been
+	 * recorded.

How could an previous failure not be recorded? Could you mention that in this nice comment please?

All the negatives in this sentence are confusing so I'll reword.

If we haven't recorded a failure for the previous op in the segment it's because it succeeded. The aim here is to record the first op failure for a segment. From the ioctl documentation:

+ * If the driver had to split a segment into two or more ops, @status
+ * includes the status of the first failed op for that segment (or
+ * GNTST_okay if all ops were successful).

Quoted 12 lines

drivers/xen/gntdev.c static long gntdev_ioctl_notify(struct gntdev_priv *priv, void __user *u) · line 916

Quoted 10 lines
drivers/xen/gntdev.c +9 -0
+static long gntdev_ioctl_grant_copy(struct gntdev_priv *priv, void __user *u)
+{
+	struct ioctl_gntdev_grant_copy copy;
+	struct gntdev_copy_batch batch = { .nr_ops = 0, .nr_pages = 0, };
+	unsigned int i;
+	int ret = 0;
+
+	if (copy_from_user(&copy, u, sizeof(copy)))
+		return -EFAULT;

+

Quoted 3 lines

No check on the .nr_pages' ? What if it is 0xfffffffffffffffffffffffff?

Ditto for .nr_ops?

batch.nr_ops and batch.nr_pages are internal, not supplied by the user and are limited by the batch size.

I guess you're really asking about the value of copy.count? This doesn't matter because we process the segments one by one and have a fixed batch size for the ops.

There's also a cond_resched() every segment so submitting a single ioctl with a crazy number of segments is really no different from userspace calling the ioctl a crazy number of times.

David

p.s. Please trim your replies when reviewing.

#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/ioctl.h>

#include <xenctrl.h>

struct gntdev_grant_copy_segment {
	union {
		void *virt;
		struct {
			grant_ref_t ref;
			uint16_t offset;
			domid_t domid;
		} foreign;
	} source, dest;
	uint16_t len;

	uint16_t flags;  /* GNTCOPY_* */
	int16_t status; /* GNTST_* */
};
#define IOCTL_GNTDEV_GRANT_COPY \
	_IOC(_IOC_NONE, 'G', 8, sizeof(struct ioctl_gntdev_grant_copy))
struct ioctl_gntdev_grant_copy {
	unsigned int count;
	struct gntdev_grant_copy_segment *segments;
};

#define NR_REFS 15

static void random_fill(char *buf, size_t len)

{
    int fd;
    int r = 0;

    fd = open("/dev/urandom", O_RDONLY);
    if (fd < 0) {
        perror("open(/dev/urandom)");
        exit(1);
    }

    while (r < len) {
        int ret;

        ret = read(fd, buf + r, len - r);
        if (ret < 0) {
            perror("read");
            exit(1);
        }
        r += ret;
    }
    close(fd);
}

static void do_copy(struct ioctl_gntdev_grant_copy *copy, char *a, char *b)

{
    int fd;
    unsigned int i;
    int ret;

    random_fill(a, NR_REFS * 4096);

    fd = open("/dev/xen/gntdev", O_RDWR);
    if (fd < 0) {
        perror("open(/dev/xen/gntdev)");
        exit(1);
    }

    ret = ioctl(fd, IOCTL_GNTDEV_GRANT_COPY, copy);
    if (ret < 0) {
        perror("ioctl(IOCTL_GNTDEV_GRANT_COPY)");
        exit(1);
    }

    close(fd);

    for (i = 0; i < NR_REFS; i++) {
        if (copy->segments[i].status != GNTST_okay) {
            fprintf(stderr, "[%u]: bad copy status: %d\n", i, copy->segments[i].status);
            exit(1);
        }
    }

    if (memcmp(a, b, NR_REFS * 4096) != 0) {
        fprintf(stderr, "a != b\n");
        exit(1);
    }
}

int main(void)

{
    xc_gntshr *gs;
    xc_gnttab *gt;
    uint32_t refs[NR_REFS];
    struct gntdev_grant_copy_segment seg[NR_REFS];
    struct ioctl_gntdev_grant_copy copy;
    char *shared;
    char local[4096 * NR_REFS];
    unsigned int i;

    gs = xc_gntshr_open(NULL, 0);
    if (!gs) {
        perror("xc_gntshr_open");
        exit(1);
    }

    gt = xc_gnttab_open(NULL, 0);
    if (!gt) {
        perror("xc_gnttab_open");
        exit(1);
    }

    shared = xc_gntshr_share_pages(gs, 0, NR_REFS, refs, 1);
    if (shared == NULL) {
        perror("xc_gntshr_share_pages");
        exit(1);
    }

    /*
     * 1. ref -> local
     */
    printf("ref -> local\n");

    for (i = 0; i < NR_REFS; i++) {
        seg[i].source.foreign.ref = refs[i];
        seg[i].source.foreign.offset = 0;
        seg[i].source.foreign.domid = 0;

        seg[i].dest.virt = local + i * 4096;

        seg[i].len = 4096;
        seg[i].flags = GNTCOPY_source_gref;
    }

    copy.count = NR_REFS;
    copy.segments = seg;

    do_copy(&copy, shared, local);

    printf("  ok\n");

    /*
     * 2. local -> ref
     */
    printf("local -> ref\n");

    for (i = 0; i < NR_REFS; i++) {
        seg[i].source.virt = local + i * 4096;

        seg[i].dest.foreign.ref = refs[i];
        seg[i].dest.foreign.offset = 0;
        seg[i].dest.foreign.domid = 0;

        seg[i].len = 4096;
        seg[i].flags = GNTCOPY_dest_gref;
    }

    copy.count = NR_REFS;
    copy.segments = seg;

    do_copy(&copy, local, shared);

    printf("  ok\n");

    return 0;    
}

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel