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/dataplane/vring.h"
20 /* Map the guest's vring to host memory */
21 bool vring_setup(Vring
*vring
, VirtIODevice
*vdev
, int n
)
23 hwaddr vring_addr
= virtio_queue_get_ring_addr(vdev
, n
);
24 hwaddr vring_size
= virtio_queue_get_ring_size(vdev
, n
);
27 vring
->broken
= false;
29 hostmem_init(&vring
->hostmem
);
30 vring_ptr
= hostmem_lookup(&vring
->hostmem
, vring_addr
, vring_size
, true);
32 error_report("Failed to map vring "
33 "addr %#" HWADDR_PRIx
" size %" HWADDR_PRIu
,
34 vring_addr
, vring_size
);
39 vring_init(&vring
->vr
, virtio_queue_get_num(vdev
, n
), vring_ptr
, 4096);
41 vring
->last_avail_idx
= 0;
42 vring
->last_used_idx
= 0;
43 vring
->signalled_used
= 0;
44 vring
->signalled_used_valid
= false;
46 trace_vring_setup(virtio_queue_get_ring_addr(vdev
, n
),
47 vring
->vr
.desc
, vring
->vr
.avail
, vring
->vr
.used
);
51 void vring_teardown(Vring
*vring
)
53 hostmem_finalize(&vring
->hostmem
);
56 /* Disable guest->host notifies */
57 void vring_disable_notification(VirtIODevice
*vdev
, Vring
*vring
)
59 if (!(vdev
->guest_features
& (1 << VIRTIO_RING_F_EVENT_IDX
))) {
60 vring
->vr
.used
->flags
|= VRING_USED_F_NO_NOTIFY
;
64 /* Enable guest->host notifies
66 * Return true if the vring is empty, false if there are more requests.
68 bool vring_enable_notification(VirtIODevice
*vdev
, Vring
*vring
)
70 if (vdev
->guest_features
& (1 << VIRTIO_RING_F_EVENT_IDX
)) {
71 vring_avail_event(&vring
->vr
) = vring
->vr
.avail
->idx
;
73 vring
->vr
.used
->flags
&= ~VRING_USED_F_NO_NOTIFY
;
75 smp_mb(); /* ensure update is seen before reading avail_idx */
76 return !vring_more_avail(vring
);
79 /* This is stolen from linux/drivers/vhost/vhost.c:vhost_notify() */
80 bool vring_should_notify(VirtIODevice
*vdev
, Vring
*vring
)
84 /* Flush out used index updates. This is paired
85 * with the barrier that the Guest executes when enabling
89 if ((vdev
->guest_features
& VIRTIO_F_NOTIFY_ON_EMPTY
) &&
90 unlikely(vring
->vr
.avail
->idx
== vring
->last_avail_idx
)) {
94 if (!(vdev
->guest_features
& VIRTIO_RING_F_EVENT_IDX
)) {
95 return !(vring
->vr
.avail
->flags
& VRING_AVAIL_F_NO_INTERRUPT
);
97 old
= vring
->signalled_used
;
98 v
= vring
->signalled_used_valid
;
99 new = vring
->signalled_used
= vring
->last_used_idx
;
100 vring
->signalled_used_valid
= true;
106 return vring_need_event(vring_used_event(&vring
->vr
), new, old
);
109 /* This is stolen from linux/drivers/vhost/vhost.c. */
110 static int get_indirect(Vring
*vring
,
111 struct iovec iov
[], struct iovec
*iov_end
,
112 unsigned int *out_num
, unsigned int *in_num
,
113 struct vring_desc
*indirect
)
115 struct vring_desc desc
;
116 unsigned int i
= 0, count
, found
= 0;
119 if (unlikely(indirect
->len
% sizeof(desc
))) {
120 error_report("Invalid length in indirect descriptor: "
121 "len %#x not multiple of %#zx",
122 indirect
->len
, sizeof(desc
));
123 vring
->broken
= true;
127 count
= indirect
->len
/ sizeof(desc
);
128 /* Buffers are chained via a 16 bit next field, so
129 * we can have at most 2^16 of these. */
130 if (unlikely(count
> USHRT_MAX
+ 1)) {
131 error_report("Indirect buffer length too big: %d", indirect
->len
);
132 vring
->broken
= true;
137 struct vring_desc
*desc_ptr
;
139 /* Translate indirect descriptor */
140 desc_ptr
= hostmem_lookup(&vring
->hostmem
,
141 indirect
->addr
+ found
* sizeof(desc
),
142 sizeof(desc
), false);
144 error_report("Failed to map indirect descriptor "
145 "addr %#" PRIx64
" len %zu",
146 (uint64_t)indirect
->addr
+ found
* sizeof(desc
),
148 vring
->broken
= true;
153 /* Ensure descriptor has been loaded before accessing fields */
154 barrier(); /* read_barrier_depends(); */
156 if (unlikely(++found
> count
)) {
157 error_report("Loop detected: last one at %u "
158 "indirect size %u", i
, count
);
159 vring
->broken
= true;
163 if (unlikely(desc
.flags
& VRING_DESC_F_INDIRECT
)) {
164 error_report("Nested indirect descriptor");
165 vring
->broken
= true;
169 /* Stop for now if there are not enough iovecs available. */
170 if (iov
>= iov_end
) {
174 iov
->iov_base
= hostmem_lookup(&vring
->hostmem
, desc
.addr
, desc
.len
,
175 desc
.flags
& VRING_DESC_F_WRITE
);
176 if (!iov
->iov_base
) {
177 error_report("Failed to map indirect descriptor"
178 "addr %#" PRIx64
" len %u",
179 (uint64_t)desc
.addr
, desc
.len
);
180 vring
->broken
= true;
183 iov
->iov_len
= desc
.len
;
186 /* If this is an input descriptor, increment that count. */
187 if (desc
.flags
& VRING_DESC_F_WRITE
) {
190 /* If it's an output descriptor, they're all supposed
191 * to come before any input descriptors. */
192 if (unlikely(*in_num
)) {
193 error_report("Indirect descriptor "
194 "has out after in: idx %u", i
);
195 vring
->broken
= true;
201 } while (desc
.flags
& VRING_DESC_F_NEXT
);
205 /* This looks in the virtqueue and for the first available buffer, and converts
206 * it to an iovec for convenient access. Since descriptors consist of some
207 * number of output then some number of input descriptors, it's actually two
208 * iovecs, but we pack them into one and note how many of each there were.
210 * This function returns the descriptor number found, or vq->num (which is
211 * never a valid descriptor number) if none was found. A negative code is
214 * Stolen from linux/drivers/vhost/vhost.c.
216 int vring_pop(VirtIODevice
*vdev
, Vring
*vring
,
217 struct iovec iov
[], struct iovec
*iov_end
,
218 unsigned int *out_num
, unsigned int *in_num
)
220 struct vring_desc desc
;
221 unsigned int i
, head
, found
= 0, num
= vring
->vr
.num
;
222 uint16_t avail_idx
, last_avail_idx
;
224 /* If there was a fatal error then refuse operation */
229 /* Check it isn't doing very strange things with descriptor numbers. */
230 last_avail_idx
= vring
->last_avail_idx
;
231 avail_idx
= vring
->vr
.avail
->idx
;
232 barrier(); /* load indices now and not again later */
234 if (unlikely((uint16_t)(avail_idx
- last_avail_idx
) > num
)) {
235 error_report("Guest moved used index from %u to %u",
236 last_avail_idx
, avail_idx
);
237 vring
->broken
= true;
241 /* If there's nothing new since last we looked. */
242 if (avail_idx
== last_avail_idx
) {
246 /* Only get avail ring entries after they have been exposed by guest. */
249 /* Grab the next descriptor number they're advertising, and increment
250 * the index we've seen. */
251 head
= vring
->vr
.avail
->ring
[last_avail_idx
% num
];
253 /* If their number is silly, that's an error. */
254 if (unlikely(head
>= num
)) {
255 error_report("Guest says index %u > %u is available", head
, num
);
256 vring
->broken
= true;
260 if (vdev
->guest_features
& (1 << VIRTIO_RING_F_EVENT_IDX
)) {
261 vring_avail_event(&vring
->vr
) = vring
->vr
.avail
->idx
;
264 /* When we start there are none of either input nor output. */
265 *out_num
= *in_num
= 0;
269 if (unlikely(i
>= num
)) {
270 error_report("Desc index is %u > %u, head = %u", i
, num
, head
);
271 vring
->broken
= true;
274 if (unlikely(++found
> num
)) {
275 error_report("Loop detected: last one at %u vq size %u head %u",
277 vring
->broken
= true;
280 desc
= vring
->vr
.desc
[i
];
282 /* Ensure descriptor is loaded before accessing fields */
285 if (desc
.flags
& VRING_DESC_F_INDIRECT
) {
286 int ret
= get_indirect(vring
, iov
, iov_end
, out_num
, in_num
, &desc
);
293 /* If there are not enough iovecs left, stop for now. The caller
294 * should check if there are more descs available once they have dealt
295 * with the current set.
297 if (iov
>= iov_end
) {
301 /* TODO handle non-contiguous memory across region boundaries */
302 iov
->iov_base
= hostmem_lookup(&vring
->hostmem
, desc
.addr
, desc
.len
,
303 desc
.flags
& VRING_DESC_F_WRITE
);
304 if (!iov
->iov_base
) {
305 error_report("Failed to map vring desc addr %#" PRIx64
" len %u",
306 (uint64_t)desc
.addr
, desc
.len
);
307 vring
->broken
= true;
310 iov
->iov_len
= desc
.len
;
313 if (desc
.flags
& VRING_DESC_F_WRITE
) {
314 /* If this is an input descriptor,
315 * increment that count. */
318 /* If it's an output descriptor, they're all supposed
319 * to come before any input descriptors. */
320 if (unlikely(*in_num
)) {
321 error_report("Descriptor has out after in: idx %d", i
);
322 vring
->broken
= true;
328 } while (desc
.flags
& VRING_DESC_F_NEXT
);
330 /* On success, increment avail index. */
331 vring
->last_avail_idx
++;
335 /* After we've used one of their buffers, we tell them about it.
337 * Stolen from linux/drivers/vhost/vhost.c.
339 void vring_push(Vring
*vring
, unsigned int head
, int len
)
341 struct vring_used_elem
*used
;
344 /* Don't touch vring if a fatal error occurred */
349 /* The virtqueue contains a ring of used buffers. Get a pointer to the
350 * next entry in that used ring. */
351 used
= &vring
->vr
.used
->ring
[vring
->last_used_idx
% vring
->vr
.num
];
355 /* Make sure buffer is written before we update index. */
358 new = vring
->vr
.used
->idx
= ++vring
->last_used_idx
;
359 if (unlikely((int16_t)(new - vring
->signalled_used
) < (uint16_t)1)) {
360 vring
->signalled_used_valid
= false;