1 /* Copyright 2012 Red Hat, Inc.
2 * Copyright IBM, Corp. 2012
4 * Based on Linux 2.6.39 vhost code:
5 * Copyright (C) 2009 Red Hat, Inc.
6 * Copyright (C) 2006 Rusty Russell IBM Corporation
8 * Author: Michael S. Tsirkin <mst@redhat.com>
9 * Stefan Hajnoczi <stefanha@redhat.com>
11 * Inspiration, some code, and most witty comments come from
12 * Documentation/virtual/lguest/lguest.c, by Rusty Russell
14 * This work is licensed under the terms of the GNU GPL, version 2.
18 #include "hw/virtio/dataplane/vring.h"
19 #include "qemu/error-report.h"
21 /* Map the guest's vring to host memory */
22 bool vring_setup(Vring
*vring
, VirtIODevice
*vdev
, int n
)
24 hwaddr vring_addr
= virtio_queue_get_ring_addr(vdev
, n
);
25 hwaddr vring_size
= virtio_queue_get_ring_size(vdev
, n
);
28 vring
->broken
= false;
30 hostmem_init(&vring
->hostmem
);
31 vring_ptr
= hostmem_lookup(&vring
->hostmem
, vring_addr
, vring_size
, true);
33 error_report("Failed to map vring "
34 "addr %#" HWADDR_PRIx
" size %" HWADDR_PRIu
,
35 vring_addr
, vring_size
);
40 vring_init(&vring
->vr
, virtio_queue_get_num(vdev
, n
), vring_ptr
, 4096);
42 vring
->last_avail_idx
= virtio_queue_get_last_avail_idx(vdev
, n
);
43 vring
->last_used_idx
= vring
->vr
.used
->idx
;
44 vring
->signalled_used
= 0;
45 vring
->signalled_used_valid
= false;
47 trace_vring_setup(virtio_queue_get_ring_addr(vdev
, n
),
48 vring
->vr
.desc
, vring
->vr
.avail
, vring
->vr
.used
);
52 void vring_teardown(Vring
*vring
, VirtIODevice
*vdev
, int n
)
54 virtio_queue_set_last_avail_idx(vdev
, n
, vring
->last_avail_idx
);
55 virtio_queue_invalidate_signalled_used(vdev
, n
);
57 hostmem_finalize(&vring
->hostmem
);
60 /* Disable guest->host notifies */
61 void vring_disable_notification(VirtIODevice
*vdev
, Vring
*vring
)
63 if (!(vdev
->guest_features
& (1 << VIRTIO_RING_F_EVENT_IDX
))) {
64 vring
->vr
.used
->flags
|= VRING_USED_F_NO_NOTIFY
;
68 /* Enable guest->host notifies
70 * Return true if the vring is empty, false if there are more requests.
72 bool vring_enable_notification(VirtIODevice
*vdev
, Vring
*vring
)
74 if (vdev
->guest_features
& (1 << VIRTIO_RING_F_EVENT_IDX
)) {
75 vring_avail_event(&vring
->vr
) = vring
->vr
.avail
->idx
;
77 vring
->vr
.used
->flags
&= ~VRING_USED_F_NO_NOTIFY
;
79 smp_mb(); /* ensure update is seen before reading avail_idx */
80 return !vring_more_avail(vring
);
83 /* This is stolen from linux/drivers/vhost/vhost.c:vhost_notify() */
84 bool vring_should_notify(VirtIODevice
*vdev
, Vring
*vring
)
88 /* Flush out used index updates. This is paired
89 * with the barrier that the Guest executes when enabling
93 if ((vdev
->guest_features
& VIRTIO_F_NOTIFY_ON_EMPTY
) &&
94 unlikely(vring
->vr
.avail
->idx
== vring
->last_avail_idx
)) {
98 if (!(vdev
->guest_features
& VIRTIO_RING_F_EVENT_IDX
)) {
99 return !(vring
->vr
.avail
->flags
& VRING_AVAIL_F_NO_INTERRUPT
);
101 old
= vring
->signalled_used
;
102 v
= vring
->signalled_used_valid
;
103 new = vring
->signalled_used
= vring
->last_used_idx
;
104 vring
->signalled_used_valid
= true;
110 return vring_need_event(vring_used_event(&vring
->vr
), new, old
);
114 static int get_desc(Vring
*vring
, VirtQueueElement
*elem
,
115 struct vring_desc
*desc
)
121 if (desc
->flags
& VRING_DESC_F_WRITE
) {
123 iov
= &elem
->in_sg
[*num
];
124 addr
= &elem
->in_addr
[*num
];
126 num
= &elem
->out_num
;
127 iov
= &elem
->out_sg
[*num
];
128 addr
= &elem
->out_addr
[*num
];
130 /* If it's an output descriptor, they're all supposed
131 * to come before any input descriptors. */
132 if (unlikely(elem
->in_num
)) {
133 error_report("Descriptor has out after in");
138 /* Stop for now if there are not enough iovecs available. */
139 if (*num
>= VIRTQUEUE_MAX_SIZE
) {
143 /* TODO handle non-contiguous memory across region boundaries */
144 iov
->iov_base
= hostmem_lookup(&vring
->hostmem
, desc
->addr
, desc
->len
,
145 desc
->flags
& VRING_DESC_F_WRITE
);
146 if (!iov
->iov_base
) {
147 error_report("Failed to map descriptor addr %#" PRIx64
" len %u",
148 (uint64_t)desc
->addr
, desc
->len
);
152 iov
->iov_len
= desc
->len
;
158 /* This is stolen from linux/drivers/vhost/vhost.c. */
159 static int get_indirect(Vring
*vring
, VirtQueueElement
*elem
,
160 struct vring_desc
*indirect
)
162 struct vring_desc desc
;
163 unsigned int i
= 0, count
, found
= 0;
167 if (unlikely(indirect
->len
% sizeof(desc
))) {
168 error_report("Invalid length in indirect descriptor: "
169 "len %#x not multiple of %#zx",
170 indirect
->len
, sizeof(desc
));
171 vring
->broken
= true;
175 count
= indirect
->len
/ sizeof(desc
);
176 /* Buffers are chained via a 16 bit next field, so
177 * we can have at most 2^16 of these. */
178 if (unlikely(count
> USHRT_MAX
+ 1)) {
179 error_report("Indirect buffer length too big: %d", indirect
->len
);
180 vring
->broken
= true;
185 struct vring_desc
*desc_ptr
;
187 /* Translate indirect descriptor */
188 desc_ptr
= hostmem_lookup(&vring
->hostmem
,
189 indirect
->addr
+ found
* sizeof(desc
),
190 sizeof(desc
), false);
192 error_report("Failed to map indirect descriptor "
193 "addr %#" PRIx64
" len %zu",
194 (uint64_t)indirect
->addr
+ found
* sizeof(desc
),
196 vring
->broken
= true;
201 /* Ensure descriptor has been loaded before accessing fields */
202 barrier(); /* read_barrier_depends(); */
204 if (unlikely(++found
> count
)) {
205 error_report("Loop detected: last one at %u "
206 "indirect size %u", i
, count
);
207 vring
->broken
= true;
211 if (unlikely(desc
.flags
& VRING_DESC_F_INDIRECT
)) {
212 error_report("Nested indirect descriptor");
213 vring
->broken
= true;
217 ret
= get_desc(vring
, elem
, &desc
);
219 vring
->broken
|= (ret
== -EFAULT
);
223 } while (desc
.flags
& VRING_DESC_F_NEXT
);
227 void vring_free_element(VirtQueueElement
*elem
)
229 g_slice_free(VirtQueueElement
, elem
);
232 /* This looks in the virtqueue and for the first available buffer, and converts
233 * it to an iovec for convenient access. Since descriptors consist of some
234 * number of output then some number of input descriptors, it's actually two
235 * iovecs, but we pack them into one and note how many of each there were.
237 * This function returns the descriptor number found, or vq->num (which is
238 * never a valid descriptor number) if none was found. A negative code is
241 * Stolen from linux/drivers/vhost/vhost.c.
243 int vring_pop(VirtIODevice
*vdev
, Vring
*vring
,
244 VirtQueueElement
**p_elem
)
246 struct vring_desc desc
;
247 unsigned int i
, head
, found
= 0, num
= vring
->vr
.num
;
248 uint16_t avail_idx
, last_avail_idx
;
249 VirtQueueElement
*elem
= NULL
;
252 /* If there was a fatal error then refuse operation */
258 /* Check it isn't doing very strange things with descriptor numbers. */
259 last_avail_idx
= vring
->last_avail_idx
;
260 avail_idx
= vring
->vr
.avail
->idx
;
261 barrier(); /* load indices now and not again later */
263 if (unlikely((uint16_t)(avail_idx
- last_avail_idx
) > num
)) {
264 error_report("Guest moved used index from %u to %u",
265 last_avail_idx
, avail_idx
);
270 /* If there's nothing new since last we looked. */
271 if (avail_idx
== last_avail_idx
) {
276 /* Only get avail ring entries after they have been exposed by guest. */
279 /* Grab the next descriptor number they're advertising, and increment
280 * the index we've seen. */
281 head
= vring
->vr
.avail
->ring
[last_avail_idx
% num
];
283 elem
= g_slice_new(VirtQueueElement
);
285 elem
->in_num
= elem
->out_num
= 0;
287 /* If their number is silly, that's an error. */
288 if (unlikely(head
>= num
)) {
289 error_report("Guest says index %u > %u is available", head
, num
);
294 if (vdev
->guest_features
& (1 << VIRTIO_RING_F_EVENT_IDX
)) {
295 vring_avail_event(&vring
->vr
) = vring
->vr
.avail
->idx
;
300 if (unlikely(i
>= num
)) {
301 error_report("Desc index is %u > %u, head = %u", i
, num
, head
);
305 if (unlikely(++found
> num
)) {
306 error_report("Loop detected: last one at %u vq size %u head %u",
311 desc
= vring
->vr
.desc
[i
];
313 /* Ensure descriptor is loaded before accessing fields */
316 if (desc
.flags
& VRING_DESC_F_INDIRECT
) {
317 int ret
= get_indirect(vring
, elem
, &desc
);
324 ret
= get_desc(vring
, elem
, &desc
);
330 } while (desc
.flags
& VRING_DESC_F_NEXT
);
332 /* On success, increment avail index. */
333 vring
->last_avail_idx
++;
339 if (ret
== -EFAULT
) {
340 vring
->broken
= true;
343 vring_free_element(elem
);
349 /* After we've used one of their buffers, we tell them about it.
351 * Stolen from linux/drivers/vhost/vhost.c.
353 void vring_push(Vring
*vring
, VirtQueueElement
*elem
, int len
)
355 struct vring_used_elem
*used
;
356 unsigned int head
= elem
->index
;
359 vring_free_element(elem
);
361 /* Don't touch vring if a fatal error occurred */
366 /* The virtqueue contains a ring of used buffers. Get a pointer to the
367 * next entry in that used ring. */
368 used
= &vring
->vr
.used
->ring
[vring
->last_used_idx
% vring
->vr
.num
];
372 /* Make sure buffer is written before we update index. */
375 new = vring
->vr
.used
->idx
= ++vring
->last_used_idx
;
376 if (unlikely((int16_t)(new - vring
->signalled_used
) < (uint16_t)1)) {
377 vring
->signalled_used_valid
= false;