[Date Prev] | [Thread Prev] | [Thread Next] | [Date Next] -- [Date Index] | [Thread Index] | [List Home]
Subject: [PATCH 09/16] lguest: implement endian conversion for ring operations.
From: Rusty Russell <rusty@rustcorp.com.au>
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
---
tools/lguest/lguest.c | 51 +++++++++++++++++++++++++++++----------------------
1 file changed, 29 insertions(+), 22 deletions(-)
diff --git a/tools/lguest/lguest.c b/tools/lguest/lguest.c
index 68de1f7..cfc494e 100644
--- a/tools/lguest/lguest.c
+++ b/tools/lguest/lguest.c
@@ -646,11 +646,11 @@ static unsigned next_desc(struct vring_desc *desc,
unsigned int next;
/* If this descriptor says it doesn't chain, we're done. */
- if (!(desc[i].flags & VRING_DESC_F_NEXT))
+ if (!(desc[i].flags & cpu_to_virtio16(VRING_DESC_F_NEXT)))
return max;
/* Check they're not leading us off end of descriptors. */
- next = desc[i].next;
+ next = virtio_to_cpu16(desc[i].next);
/* Make sure compiler knows to grab that: we don't want it changing! */
wmb();
@@ -674,7 +674,7 @@ static void trigger_irq(struct virtqueue *vq)
vq->pending_used = 0;
/* If they don't want an interrupt, don't send one... */
- if (vq->vring.avail->flags & VRING_AVAIL_F_NO_INTERRUPT) {
+ if (vq->vring.avail->flags & cpu_to_virtio16(VRING_AVAIL_F_NO_INTERRUPT)) {
return;
}
@@ -700,7 +700,7 @@ static unsigned wait_for_vq_desc(struct virtqueue *vq,
u16 last_avail = lg_last_avail(vq);
/* There's nothing available? */
- while (last_avail == vq->vring.avail->idx) {
+ while (last_avail == virtio_to_cpu16(vq->vring.avail->idx)) {
u64 event;
/*
@@ -710,15 +710,16 @@ static unsigned wait_for_vq_desc(struct virtqueue *vq,
trigger_irq(vq);
/* OK, now we need to know about added descriptors. */
- vq->vring.used->flags &= ~VRING_USED_F_NO_NOTIFY;
+ vq->vring.used->flags &= ~cpu_to_virtio16(VRING_USED_F_NO_NOTIFY);
/*
* They could have slipped one in as we were doing that: make
* sure it's written, then check again.
*/
mb();
- if (last_avail != vq->vring.avail->idx) {
- vq->vring.used->flags |= VRING_USED_F_NO_NOTIFY;
+ if (last_avail != virtio_to_cpu16(vq->vring.avail->idx)) {
+ vq->vring.used->flags
+ |= cpu_to_virtio16(VRING_USED_F_NO_NOTIFY);
break;
}
@@ -727,13 +728,14 @@ static unsigned wait_for_vq_desc(struct virtqueue *vq,
errx(1, "Event read failed?");
/* We don't need to be notified again. */
- vq->vring.used->flags |= VRING_USED_F_NO_NOTIFY;
+ vq->vring.used->flags |= cpu_to_virtio16(VRING_USED_F_NO_NOTIFY);
}
/* Check it isn't doing very strange things with descriptor numbers. */
- if ((u16)(vq->vring.avail->idx - last_avail) > vq->vring.num)
+ if ((u16)(virtio_to_cpu16(vq->vring.avail->idx) - last_avail) >
+ vq->vring.num)
errx(1, "Guest moved used index from %u to %u",
- last_avail, vq->vring.avail->idx);
+ last_avail, virtio_to_cpu16(vq->vring.avail->idx));
/*
* Make sure we read the descriptor number *after* we read the ring
@@ -745,7 +747,8 @@ static unsigned wait_for_vq_desc(struct virtqueue *vq,
* Grab the next descriptor number they're advertising, and increment
* the index we've seen.
*/
- head = vq->vring.avail->ring[last_avail % vq->vring.num];
+ head = virtio_to_cpu16(vq->vring.avail->ring[last_avail %
+ vq->vring.num]);
lg_last_avail(vq)++;
/* If their number is silly, that's a fatal mistake. */
@@ -769,22 +772,24 @@ static unsigned wait_for_vq_desc(struct virtqueue *vq,
* If this is an indirect entry, then this buffer contains a descriptor
* table which we handle as if it's any normal descriptor chain.
*/
- if (desc[i].flags & VRING_DESC_F_INDIRECT) {
- if (desc[i].len % sizeof(struct vring_desc))
+ if (desc[i].flags & cpu_to_virtio16(VRING_DESC_F_INDIRECT)) {
+ if (virtio_to_cpu32(desc[i].len) % sizeof(struct vring_desc))
errx(1, "Invalid size for indirect buffer table");
- max = desc[i].len / sizeof(struct vring_desc);
- desc = check_pointer(desc[i].addr, desc[i].len);
+ max = virtio_to_cpu32(desc[i].len) / sizeof(struct vring_desc);
+ desc = check_pointer(virtio_to_cpu64(desc[i].addr),
+ virtio_to_cpu32(desc[i].len));
i = 0;
}
do {
/* Grab the first descriptor, and check it's OK. */
- iov[*out_num + *in_num].iov_len = desc[i].len;
+ iov[*out_num + *in_num].iov_len = virtio_to_cpu32(desc[i].len);
iov[*out_num + *in_num].iov_base
- = check_pointer(desc[i].addr, desc[i].len);
+ = check_pointer(virtio_to_cpu64(desc[i].addr),
+ virtio_to_cpu32(desc[i].len));
/* If this is an input descriptor, increment that count. */
- if (desc[i].flags & VRING_DESC_F_WRITE)
+ if (desc[i].flags & virtio_to_cpu16(VRING_DESC_F_WRITE))
(*in_num)++;
else {
/*
@@ -817,12 +822,14 @@ static void add_used(struct virtqueue *vq, unsigned int head, int len)
* The virtqueue contains a ring of used buffers. Get a pointer to the
* next entry in that used ring.
*/
- used = &vq->vring.used->ring[vq->vring.used->idx % vq->vring.num];
- used->id = head;
- used->len = len;
+ used = &vq->vring.used->ring[virtio_to_cpu16(vq->vring.used->idx)
+ % vq->vring.num];
+ used->id = cpu_to_virtio32(head);
+ used->len = cpu_to_virtio32(len);
/* Make sure buffer is written before we update index. */
wmb();
- vq->vring.used->idx++;
+ vq->vring.used->idx
+ = cpu_to_virtio16(virtio_to_cpu16(vq->vring.used->idx) + 1);
vq->pending_used++;
}
--
1.8.1.2
[Date Prev] | [Thread Prev] | [Thread Next] | [Date Next] -- [Date Index] | [Thread Index] | [List Home]