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.
19 #include "exec/memory.h"
20 #include "exec/address-spaces.h"
21 #include "hw/virtio/virtio-access.h"
22 #include "hw/virtio/dataplane/vring.h"
23 #include "hw/virtio/dataplane/vring-accessors.h"
24 #include "qemu/error-report.h"
26 /* vring_map can be coupled with vring_unmap or (if you still have the
27 * value returned in *mr) memory_region_unref.
29 static void *vring_map(MemoryRegion
**mr
, hwaddr phys
, hwaddr len
,
32 MemoryRegionSection section
= memory_region_find(get_system_memory(), phys
, len
);
34 if (!section
.mr
|| int128_get64(section
.size
) < len
) {
37 if (is_write
&& section
.readonly
) {
40 if (!memory_region_is_ram(section
.mr
)) {
44 /* Ignore regions with dirty logging, we cannot mark them dirty */
45 if (memory_region_is_logging(section
.mr
)) {
50 return memory_region_get_ram_ptr(section
.mr
) + section
.offset_within_region
;
53 memory_region_unref(section
.mr
);
58 static void vring_unmap(void *buffer
, bool is_write
)
63 mr
= qemu_ram_addr_from_host(buffer
, &addr
);
64 memory_region_unref(mr
);
67 /* Map the guest's vring to host memory */
68 bool vring_setup(Vring
*vring
, VirtIODevice
*vdev
, int n
)
70 hwaddr vring_addr
= virtio_queue_get_ring_addr(vdev
, n
);
71 hwaddr vring_size
= virtio_queue_get_ring_size(vdev
, n
);
74 vring
->broken
= false;
76 vring_ptr
= vring_map(&vring
->mr
, vring_addr
, vring_size
, true);
78 error_report("Failed to map vring "
79 "addr %#" HWADDR_PRIx
" size %" HWADDR_PRIu
,
80 vring_addr
, vring_size
);
85 vring_init(&vring
->vr
, virtio_queue_get_num(vdev
, n
), vring_ptr
, 4096);
87 vring
->last_avail_idx
= virtio_queue_get_last_avail_idx(vdev
, n
);
88 vring
->last_used_idx
= vring_get_used_idx(vdev
, vring
);
89 vring
->signalled_used
= 0;
90 vring
->signalled_used_valid
= false;
92 trace_vring_setup(virtio_queue_get_ring_addr(vdev
, n
),
93 vring
->vr
.desc
, vring
->vr
.avail
, vring
->vr
.used
);
97 void vring_teardown(Vring
*vring
, VirtIODevice
*vdev
, int n
)
99 virtio_queue_set_last_avail_idx(vdev
, n
, vring
->last_avail_idx
);
100 virtio_queue_invalidate_signalled_used(vdev
, n
);
102 memory_region_unref(vring
->mr
);
105 /* Disable guest->host notifies */
106 void vring_disable_notification(VirtIODevice
*vdev
, Vring
*vring
)
108 if (!virtio_has_feature(vdev
, VIRTIO_RING_F_EVENT_IDX
)) {
109 vring_set_used_flags(vdev
, vring
, VRING_USED_F_NO_NOTIFY
);
113 /* Enable guest->host notifies
115 * Return true if the vring is empty, false if there are more requests.
117 bool vring_enable_notification(VirtIODevice
*vdev
, Vring
*vring
)
119 if (virtio_has_feature(vdev
, VIRTIO_RING_F_EVENT_IDX
)) {
120 vring_avail_event(&vring
->vr
) = vring
->vr
.avail
->idx
;
122 vring_clear_used_flags(vdev
, vring
, VRING_USED_F_NO_NOTIFY
);
124 smp_mb(); /* ensure update is seen before reading avail_idx */
125 return !vring_more_avail(vdev
, vring
);
128 /* This is stolen from linux/drivers/vhost/vhost.c:vhost_notify() */
129 bool vring_should_notify(VirtIODevice
*vdev
, Vring
*vring
)
133 /* Flush out used index updates. This is paired
134 * with the barrier that the Guest executes when enabling
138 if (virtio_has_feature(vdev
, VIRTIO_F_NOTIFY_ON_EMPTY
) &&
139 unlikely(!vring_more_avail(vdev
, vring
))) {
143 if (!virtio_has_feature(vdev
, VIRTIO_RING_F_EVENT_IDX
)) {
144 return !(vring_get_avail_flags(vdev
, vring
) &
145 VRING_AVAIL_F_NO_INTERRUPT
);
147 old
= vring
->signalled_used
;
148 v
= vring
->signalled_used_valid
;
149 new = vring
->signalled_used
= vring
->last_used_idx
;
150 vring
->signalled_used_valid
= true;
156 return vring_need_event(vring_used_event(&vring
->vr
), new, old
);
160 static int get_desc(VirtIODevice
*vdev
, Vring
*vring
, VirtQueueElement
*elem
,
161 struct vring_desc
*desc
)
167 int is_write
= virtio_tswap16(vdev
, desc
->flags
) & VRING_DESC_F_WRITE
;
168 uint32_t len
= virtio_tswap32(vdev
, desc
->len
);
169 uint64_t desc_addr
= virtio_tswap64(vdev
, desc
->addr
);
173 iov
= &elem
->in_sg
[*num
];
174 addr
= &elem
->in_addr
[*num
];
176 num
= &elem
->out_num
;
177 iov
= &elem
->out_sg
[*num
];
178 addr
= &elem
->out_addr
[*num
];
180 /* If it's an output descriptor, they're all supposed
181 * to come before any input descriptors. */
182 if (unlikely(elem
->in_num
)) {
183 error_report("Descriptor has out after in");
188 /* Stop for now if there are not enough iovecs available. */
189 if (*num
>= VIRTQUEUE_MAX_SIZE
) {
190 error_report("Invalid SG num: %u", *num
);
194 /* TODO handle non-contiguous memory across region boundaries */
195 iov
->iov_base
= vring_map(&mr
, desc_addr
, len
, is_write
);
196 if (!iov
->iov_base
) {
197 error_report("Failed to map descriptor addr %#" PRIx64
" len %u",
198 (uint64_t)desc_addr
, len
);
202 /* The MemoryRegion is looked up again and unref'ed later, leave the
210 static void copy_in_vring_desc(VirtIODevice
*vdev
,
211 const struct vring_desc
*guest
,
212 struct vring_desc
*host
)
214 host
->addr
= virtio_ldq_p(vdev
, &guest
->addr
);
215 host
->len
= virtio_ldl_p(vdev
, &guest
->len
);
216 host
->flags
= virtio_lduw_p(vdev
, &guest
->flags
);
217 host
->next
= virtio_lduw_p(vdev
, &guest
->next
);
220 /* This is stolen from linux/drivers/vhost/vhost.c. */
221 static int get_indirect(VirtIODevice
*vdev
, Vring
*vring
,
222 VirtQueueElement
*elem
, struct vring_desc
*indirect
)
224 struct vring_desc desc
;
225 unsigned int i
= 0, count
, found
= 0;
227 uint32_t len
= virtio_tswap32(vdev
, indirect
->len
);
228 uint64_t addr
= virtio_tswap64(vdev
, indirect
->addr
);
231 if (unlikely(len
% sizeof(desc
))) {
232 error_report("Invalid length in indirect descriptor: "
233 "len %#x not multiple of %#zx",
235 vring
->broken
= true;
239 count
= len
/ sizeof(desc
);
240 /* Buffers are chained via a 16 bit next field, so
241 * we can have at most 2^16 of these. */
242 if (unlikely(count
> USHRT_MAX
+ 1)) {
243 error_report("Indirect buffer length too big: %d", len
);
244 vring
->broken
= true;
249 struct vring_desc
*desc_ptr
;
252 /* Translate indirect descriptor */
253 desc_ptr
= vring_map(&mr
,
254 addr
+ found
* sizeof(desc
),
255 sizeof(desc
), false);
257 error_report("Failed to map indirect descriptor "
258 "addr %#" PRIx64
" len %zu",
259 (uint64_t)addr
+ found
* sizeof(desc
),
261 vring
->broken
= true;
264 copy_in_vring_desc(vdev
, desc_ptr
, &desc
);
265 memory_region_unref(mr
);
267 /* Ensure descriptor has been loaded before accessing fields */
268 barrier(); /* read_barrier_depends(); */
270 if (unlikely(++found
> count
)) {
271 error_report("Loop detected: last one at %u "
272 "indirect size %u", i
, count
);
273 vring
->broken
= true;
277 if (unlikely(virtio_tswap16(vdev
, desc
.flags
)
278 & VRING_DESC_F_INDIRECT
)) {
279 error_report("Nested indirect descriptor");
280 vring
->broken
= true;
284 ret
= get_desc(vdev
, vring
, elem
, &desc
);
286 vring
->broken
|= (ret
== -EFAULT
);
289 i
= virtio_tswap16(vdev
, desc
.next
);
290 } while (virtio_tswap16(vdev
, desc
.flags
) & VRING_DESC_F_NEXT
);
294 static void vring_unmap_element(VirtQueueElement
*elem
)
298 /* This assumes that the iovecs, if changed, are never moved past
299 * the end of the valid area. This is true if iovec manipulations
300 * are done with iov_discard_front and iov_discard_back.
302 for (i
= 0; i
< elem
->out_num
; i
++) {
303 vring_unmap(elem
->out_sg
[i
].iov_base
, false);
306 for (i
= 0; i
< elem
->in_num
; i
++) {
307 vring_unmap(elem
->in_sg
[i
].iov_base
, true);
311 /* This looks in the virtqueue and for the first available buffer, and converts
312 * it to an iovec for convenient access. Since descriptors consist of some
313 * number of output then some number of input descriptors, it's actually two
314 * iovecs, but we pack them into one and note how many of each there were.
316 * This function returns the descriptor number found, or vq->num (which is
317 * never a valid descriptor number) if none was found. A negative code is
320 * Stolen from linux/drivers/vhost/vhost.c.
322 int vring_pop(VirtIODevice
*vdev
, Vring
*vring
,
323 VirtQueueElement
*elem
)
325 struct vring_desc desc
;
326 unsigned int i
, head
, found
= 0, num
= vring
->vr
.num
;
327 uint16_t avail_idx
, last_avail_idx
;
330 /* Initialize elem so it can be safely unmapped */
331 elem
->in_num
= elem
->out_num
= 0;
333 /* If there was a fatal error then refuse operation */
339 /* Check it isn't doing very strange things with descriptor numbers. */
340 last_avail_idx
= vring
->last_avail_idx
;
341 avail_idx
= vring_get_avail_idx(vdev
, vring
);
342 barrier(); /* load indices now and not again later */
344 if (unlikely((uint16_t)(avail_idx
- last_avail_idx
) > num
)) {
345 error_report("Guest moved used index from %u to %u",
346 last_avail_idx
, avail_idx
);
351 /* If there's nothing new since last we looked. */
352 if (avail_idx
== last_avail_idx
) {
357 /* Only get avail ring entries after they have been exposed by guest. */
360 /* Grab the next descriptor number they're advertising, and increment
361 * the index we've seen. */
362 head
= vring_get_avail_ring(vdev
, vring
, last_avail_idx
% num
);
366 /* If their number is silly, that's an error. */
367 if (unlikely(head
>= num
)) {
368 error_report("Guest says index %u > %u is available", head
, num
);
375 if (unlikely(i
>= num
)) {
376 error_report("Desc index is %u > %u, head = %u", i
, num
, head
);
380 if (unlikely(++found
> num
)) {
381 error_report("Loop detected: last one at %u vq size %u head %u",
386 copy_in_vring_desc(vdev
, &vring
->vr
.desc
[i
], &desc
);
388 /* Ensure descriptor is loaded before accessing fields */
391 if (virtio_tswap16(vdev
, desc
.flags
) & VRING_DESC_F_INDIRECT
) {
392 ret
= get_indirect(vdev
, vring
, elem
, &desc
);
399 ret
= get_desc(vdev
, vring
, elem
, &desc
);
404 i
= virtio_tswap16(vdev
, desc
.next
);
405 } while (virtio_tswap16(vdev
, desc
.flags
) & VRING_DESC_F_NEXT
);
407 /* On success, increment avail index. */
408 vring
->last_avail_idx
++;
409 if (virtio_has_feature(vdev
, VIRTIO_RING_F_EVENT_IDX
)) {
410 vring_avail_event(&vring
->vr
) = vring
->last_avail_idx
;
417 if (ret
== -EFAULT
) {
418 vring
->broken
= true;
420 vring_unmap_element(elem
);
424 /* After we've used one of their buffers, we tell them about it.
426 * Stolen from linux/drivers/vhost/vhost.c.
428 void vring_push(VirtIODevice
*vdev
, Vring
*vring
, VirtQueueElement
*elem
,
431 unsigned int head
= elem
->index
;
434 vring_unmap_element(elem
);
436 /* Don't touch vring if a fatal error occurred */
441 /* The virtqueue contains a ring of used buffers. Get a pointer to the
442 * next entry in that used ring. */
443 vring_set_used_ring_id(vdev
, vring
, vring
->last_used_idx
% vring
->vr
.num
,
445 vring_set_used_ring_len(vdev
, vring
, vring
->last_used_idx
% vring
->vr
.num
,
448 /* Make sure buffer is written before we update index. */
451 new = ++vring
->last_used_idx
;
452 vring_set_used_idx(vdev
, vring
, new);
453 if (unlikely((int16_t)(new - vring
->signalled_used
) < (uint16_t)1)) {
454 vring
->signalled_used_valid
= false;