xref: /openbmc/qemu/hw/virtio/virtio.c (revision baf2d5bfbac015b27f4db74feab235e167df0c84)
1 /*
2  * Virtio Support
3  *
4  * Copyright IBM, Corp. 2007
5  *
6  * Authors:
7  *  Anthony Liguori   <aliguori@us.ibm.com>
8  *
9  * This work is licensed under the terms of the GNU GPL, version 2.  See
10  * the COPYING file in the top-level directory.
11  *
12  */
13 
14 #include "qemu/osdep.h"
15 #include "qapi/error.h"
16 #include "qemu-common.h"
17 #include "cpu.h"
18 #include "trace.h"
19 #include "exec/address-spaces.h"
20 #include "qemu/error-report.h"
21 #include "hw/virtio/virtio.h"
22 #include "qemu/atomic.h"
23 #include "hw/virtio/virtio-bus.h"
24 #include "migration/migration.h"
25 #include "hw/virtio/virtio-access.h"
26 #include "sysemu/dma.h"
27 
28 /*
29  * The alignment to use between consumer and producer parts of vring.
30  * x86 pagesize again. This is the default, used by transports like PCI
31  * which don't provide a means for the guest to tell the host the alignment.
32  */
33 #define VIRTIO_PCI_VRING_ALIGN         4096
34 
35 typedef struct VRingDesc
36 {
37     uint64_t addr;
38     uint32_t len;
39     uint16_t flags;
40     uint16_t next;
41 } VRingDesc;
42 
43 typedef struct VRingAvail
44 {
45     uint16_t flags;
46     uint16_t idx;
47     uint16_t ring[0];
48 } VRingAvail;
49 
50 typedef struct VRingUsedElem
51 {
52     uint32_t id;
53     uint32_t len;
54 } VRingUsedElem;
55 
56 typedef struct VRingUsed
57 {
58     uint16_t flags;
59     uint16_t idx;
60     VRingUsedElem ring[0];
61 } VRingUsed;
62 
63 typedef struct VRing
64 {
65     unsigned int num;
66     unsigned int num_default;
67     unsigned int align;
68     hwaddr desc;
69     hwaddr avail;
70     hwaddr used;
71 } VRing;
72 
73 struct VirtQueue
74 {
75     VRing vring;
76 
77     /* Next head to pop */
78     uint16_t last_avail_idx;
79 
80     /* Last avail_idx read from VQ. */
81     uint16_t shadow_avail_idx;
82 
83     uint16_t used_idx;
84 
85     /* Last used index value we have signalled on */
86     uint16_t signalled_used;
87 
88     /* Last used index value we have signalled on */
89     bool signalled_used_valid;
90 
91     /* Notification enabled? */
92     bool notification;
93 
94     uint16_t queue_index;
95 
96     unsigned int inuse;
97 
98     uint16_t vector;
99     VirtIOHandleOutput handle_output;
100     VirtIOHandleOutput handle_aio_output;
101     VirtIODevice *vdev;
102     EventNotifier guest_notifier;
103     EventNotifier host_notifier;
104     QLIST_ENTRY(VirtQueue) node;
105 };
106 
107 /* virt queue functions */
108 void virtio_queue_update_rings(VirtIODevice *vdev, int n)
109 {
110     VRing *vring = &vdev->vq[n].vring;
111 
112     if (!vring->desc) {
113         /* not yet setup -> nothing to do */
114         return;
115     }
116     vring->avail = vring->desc + vring->num * sizeof(VRingDesc);
117     vring->used = vring_align(vring->avail +
118                               offsetof(VRingAvail, ring[vring->num]),
119                               vring->align);
120 }
121 
122 static void vring_desc_read(VirtIODevice *vdev, VRingDesc *desc,
123                             hwaddr desc_pa, int i)
124 {
125     address_space_read(vdev->dma_as, desc_pa + i * sizeof(VRingDesc),
126                        MEMTXATTRS_UNSPECIFIED, (void *)desc, sizeof(VRingDesc));
127     virtio_tswap64s(vdev, &desc->addr);
128     virtio_tswap32s(vdev, &desc->len);
129     virtio_tswap16s(vdev, &desc->flags);
130     virtio_tswap16s(vdev, &desc->next);
131 }
132 
133 static inline uint16_t vring_avail_flags(VirtQueue *vq)
134 {
135     hwaddr pa;
136     pa = vq->vring.avail + offsetof(VRingAvail, flags);
137     return virtio_lduw_phys(vq->vdev, pa);
138 }
139 
140 static inline uint16_t vring_avail_idx(VirtQueue *vq)
141 {
142     hwaddr pa;
143     pa = vq->vring.avail + offsetof(VRingAvail, idx);
144     vq->shadow_avail_idx = virtio_lduw_phys(vq->vdev, pa);
145     return vq->shadow_avail_idx;
146 }
147 
148 static inline uint16_t vring_avail_ring(VirtQueue *vq, int i)
149 {
150     hwaddr pa;
151     pa = vq->vring.avail + offsetof(VRingAvail, ring[i]);
152     return virtio_lduw_phys(vq->vdev, pa);
153 }
154 
155 static inline uint16_t vring_get_used_event(VirtQueue *vq)
156 {
157     return vring_avail_ring(vq, vq->vring.num);
158 }
159 
160 static inline void vring_used_write(VirtQueue *vq, VRingUsedElem *uelem,
161                                     int i)
162 {
163     hwaddr pa;
164     virtio_tswap32s(vq->vdev, &uelem->id);
165     virtio_tswap32s(vq->vdev, &uelem->len);
166     pa = vq->vring.used + offsetof(VRingUsed, ring[i]);
167     address_space_write(vq->vdev->dma_as, pa, MEMTXATTRS_UNSPECIFIED,
168                        (void *)uelem, sizeof(VRingUsedElem));
169 }
170 
171 static uint16_t vring_used_idx(VirtQueue *vq)
172 {
173     hwaddr pa;
174     pa = vq->vring.used + offsetof(VRingUsed, idx);
175     return virtio_lduw_phys(vq->vdev, pa);
176 }
177 
178 static inline void vring_used_idx_set(VirtQueue *vq, uint16_t val)
179 {
180     hwaddr pa;
181     pa = vq->vring.used + offsetof(VRingUsed, idx);
182     virtio_stw_phys(vq->vdev, pa, val);
183     vq->used_idx = val;
184 }
185 
186 static inline void vring_used_flags_set_bit(VirtQueue *vq, int mask)
187 {
188     VirtIODevice *vdev = vq->vdev;
189     hwaddr pa;
190     pa = vq->vring.used + offsetof(VRingUsed, flags);
191     virtio_stw_phys(vdev, pa, virtio_lduw_phys(vdev, pa) | mask);
192 }
193 
194 static inline void vring_used_flags_unset_bit(VirtQueue *vq, int mask)
195 {
196     VirtIODevice *vdev = vq->vdev;
197     hwaddr pa;
198     pa = vq->vring.used + offsetof(VRingUsed, flags);
199     virtio_stw_phys(vdev, pa, virtio_lduw_phys(vdev, pa) & ~mask);
200 }
201 
202 static inline void vring_set_avail_event(VirtQueue *vq, uint16_t val)
203 {
204     hwaddr pa;
205     if (!vq->notification) {
206         return;
207     }
208     pa = vq->vring.used + offsetof(VRingUsed, ring[vq->vring.num]);
209     virtio_stw_phys(vq->vdev, pa, val);
210 }
211 
212 void virtio_queue_set_notification(VirtQueue *vq, int enable)
213 {
214     vq->notification = enable;
215     if (virtio_vdev_has_feature(vq->vdev, VIRTIO_RING_F_EVENT_IDX)) {
216         vring_set_avail_event(vq, vring_avail_idx(vq));
217     } else if (enable) {
218         vring_used_flags_unset_bit(vq, VRING_USED_F_NO_NOTIFY);
219     } else {
220         vring_used_flags_set_bit(vq, VRING_USED_F_NO_NOTIFY);
221     }
222     if (enable) {
223         /* Expose avail event/used flags before caller checks the avail idx. */
224         smp_mb();
225     }
226 }
227 
228 int virtio_queue_ready(VirtQueue *vq)
229 {
230     return vq->vring.avail != 0;
231 }
232 
233 /* Fetch avail_idx from VQ memory only when we really need to know if
234  * guest has added some buffers. */
235 int virtio_queue_empty(VirtQueue *vq)
236 {
237     if (vq->shadow_avail_idx != vq->last_avail_idx) {
238         return 0;
239     }
240 
241     return vring_avail_idx(vq) == vq->last_avail_idx;
242 }
243 
244 static void virtqueue_unmap_sg(VirtQueue *vq, const VirtQueueElement *elem,
245                                unsigned int len)
246 {
247     AddressSpace *dma_as = vq->vdev->dma_as;
248     unsigned int offset;
249     int i;
250 
251     offset = 0;
252     for (i = 0; i < elem->in_num; i++) {
253         size_t size = MIN(len - offset, elem->in_sg[i].iov_len);
254 
255         dma_memory_unmap(dma_as, elem->in_sg[i].iov_base,
256                          elem->in_sg[i].iov_len,
257                          DMA_DIRECTION_FROM_DEVICE, size);
258 
259         offset += size;
260     }
261 
262     for (i = 0; i < elem->out_num; i++)
263         dma_memory_unmap(dma_as, elem->out_sg[i].iov_base,
264                          elem->out_sg[i].iov_len,
265                          DMA_DIRECTION_TO_DEVICE,
266                          elem->out_sg[i].iov_len);
267 }
268 
269 /* virtqueue_detach_element:
270  * @vq: The #VirtQueue
271  * @elem: The #VirtQueueElement
272  * @len: number of bytes written
273  *
274  * Detach the element from the virtqueue.  This function is suitable for device
275  * reset or other situations where a #VirtQueueElement is simply freed and will
276  * not be pushed or discarded.
277  */
278 void virtqueue_detach_element(VirtQueue *vq, const VirtQueueElement *elem,
279                               unsigned int len)
280 {
281     vq->inuse--;
282     virtqueue_unmap_sg(vq, elem, len);
283 }
284 
285 /* virtqueue_unpop:
286  * @vq: The #VirtQueue
287  * @elem: The #VirtQueueElement
288  * @len: number of bytes written
289  *
290  * Pretend the most recent element wasn't popped from the virtqueue.  The next
291  * call to virtqueue_pop() will refetch the element.
292  */
293 void virtqueue_unpop(VirtQueue *vq, const VirtQueueElement *elem,
294                      unsigned int len)
295 {
296     vq->last_avail_idx--;
297     virtqueue_detach_element(vq, elem, len);
298 }
299 
300 /* virtqueue_rewind:
301  * @vq: The #VirtQueue
302  * @num: Number of elements to push back
303  *
304  * Pretend that elements weren't popped from the virtqueue.  The next
305  * virtqueue_pop() will refetch the oldest element.
306  *
307  * Use virtqueue_unpop() instead if you have a VirtQueueElement.
308  *
309  * Returns: true on success, false if @num is greater than the number of in use
310  * elements.
311  */
312 bool virtqueue_rewind(VirtQueue *vq, unsigned int num)
313 {
314     if (num > vq->inuse) {
315         return false;
316     }
317     vq->last_avail_idx -= num;
318     vq->inuse -= num;
319     return true;
320 }
321 
322 void virtqueue_fill(VirtQueue *vq, const VirtQueueElement *elem,
323                     unsigned int len, unsigned int idx)
324 {
325     VRingUsedElem uelem;
326 
327     trace_virtqueue_fill(vq, elem, len, idx);
328 
329     virtqueue_unmap_sg(vq, elem, len);
330 
331     if (unlikely(vq->vdev->broken)) {
332         return;
333     }
334 
335     idx = (idx + vq->used_idx) % vq->vring.num;
336 
337     uelem.id = elem->index;
338     uelem.len = len;
339     vring_used_write(vq, &uelem, idx);
340 }
341 
342 void virtqueue_flush(VirtQueue *vq, unsigned int count)
343 {
344     uint16_t old, new;
345 
346     if (unlikely(vq->vdev->broken)) {
347         vq->inuse -= count;
348         return;
349     }
350 
351     /* Make sure buffer is written before we update index. */
352     smp_wmb();
353     trace_virtqueue_flush(vq, count);
354     old = vq->used_idx;
355     new = old + count;
356     vring_used_idx_set(vq, new);
357     vq->inuse -= count;
358     if (unlikely((int16_t)(new - vq->signalled_used) < (uint16_t)(new - old)))
359         vq->signalled_used_valid = false;
360 }
361 
362 void virtqueue_push(VirtQueue *vq, const VirtQueueElement *elem,
363                     unsigned int len)
364 {
365     virtqueue_fill(vq, elem, len, 0);
366     virtqueue_flush(vq, 1);
367 }
368 
369 static int virtqueue_num_heads(VirtQueue *vq, unsigned int idx)
370 {
371     uint16_t num_heads = vring_avail_idx(vq) - idx;
372 
373     /* Check it isn't doing very strange things with descriptor numbers. */
374     if (num_heads > vq->vring.num) {
375         virtio_error(vq->vdev, "Guest moved used index from %u to %u",
376                      idx, vq->shadow_avail_idx);
377         return -EINVAL;
378     }
379     /* On success, callers read a descriptor at vq->last_avail_idx.
380      * Make sure descriptor read does not bypass avail index read. */
381     if (num_heads) {
382         smp_rmb();
383     }
384 
385     return num_heads;
386 }
387 
388 static bool virtqueue_get_head(VirtQueue *vq, unsigned int idx,
389                                unsigned int *head)
390 {
391     /* Grab the next descriptor number they're advertising, and increment
392      * the index we've seen. */
393     *head = vring_avail_ring(vq, idx % vq->vring.num);
394 
395     /* If their number is silly, that's a fatal mistake. */
396     if (*head >= vq->vring.num) {
397         virtio_error(vq->vdev, "Guest says index %u is available", *head);
398         return false;
399     }
400 
401     return true;
402 }
403 
404 enum {
405     VIRTQUEUE_READ_DESC_ERROR = -1,
406     VIRTQUEUE_READ_DESC_DONE = 0,   /* end of chain */
407     VIRTQUEUE_READ_DESC_MORE = 1,   /* more buffers in chain */
408 };
409 
410 static int virtqueue_read_next_desc(VirtIODevice *vdev, VRingDesc *desc,
411                                     hwaddr desc_pa, unsigned int max,
412                                     unsigned int *next)
413 {
414     /* If this descriptor says it doesn't chain, we're done. */
415     if (!(desc->flags & VRING_DESC_F_NEXT)) {
416         return VIRTQUEUE_READ_DESC_DONE;
417     }
418 
419     /* Check they're not leading us off end of descriptors. */
420     *next = desc->next;
421     /* Make sure compiler knows to grab that: we don't want it changing! */
422     smp_wmb();
423 
424     if (*next >= max) {
425         virtio_error(vdev, "Desc next is %u", *next);
426         return VIRTQUEUE_READ_DESC_ERROR;
427     }
428 
429     vring_desc_read(vdev, desc, desc_pa, *next);
430     return VIRTQUEUE_READ_DESC_MORE;
431 }
432 
433 void virtqueue_get_avail_bytes(VirtQueue *vq, unsigned int *in_bytes,
434                                unsigned int *out_bytes,
435                                unsigned max_in_bytes, unsigned max_out_bytes)
436 {
437     unsigned int idx;
438     unsigned int total_bufs, in_total, out_total;
439     int rc;
440 
441     idx = vq->last_avail_idx;
442 
443     total_bufs = in_total = out_total = 0;
444     while ((rc = virtqueue_num_heads(vq, idx)) > 0) {
445         VirtIODevice *vdev = vq->vdev;
446         unsigned int max, num_bufs, indirect = 0;
447         VRingDesc desc;
448         hwaddr desc_pa;
449         unsigned int i;
450 
451         max = vq->vring.num;
452         num_bufs = total_bufs;
453 
454         if (!virtqueue_get_head(vq, idx++, &i)) {
455             goto err;
456         }
457 
458         desc_pa = vq->vring.desc;
459         vring_desc_read(vdev, &desc, desc_pa, i);
460 
461         if (desc.flags & VRING_DESC_F_INDIRECT) {
462             if (desc.len % sizeof(VRingDesc)) {
463                 virtio_error(vdev, "Invalid size for indirect buffer table");
464                 goto err;
465             }
466 
467             /* If we've got too many, that implies a descriptor loop. */
468             if (num_bufs >= max) {
469                 virtio_error(vdev, "Looped descriptor");
470                 goto err;
471             }
472 
473             /* loop over the indirect descriptor table */
474             indirect = 1;
475             max = desc.len / sizeof(VRingDesc);
476             desc_pa = desc.addr;
477             num_bufs = i = 0;
478             vring_desc_read(vdev, &desc, desc_pa, i);
479         }
480 
481         do {
482             /* If we've got too many, that implies a descriptor loop. */
483             if (++num_bufs > max) {
484                 virtio_error(vdev, "Looped descriptor");
485                 goto err;
486             }
487 
488             if (desc.flags & VRING_DESC_F_WRITE) {
489                 in_total += desc.len;
490             } else {
491                 out_total += desc.len;
492             }
493             if (in_total >= max_in_bytes && out_total >= max_out_bytes) {
494                 goto done;
495             }
496 
497             rc = virtqueue_read_next_desc(vdev, &desc, desc_pa, max, &i);
498         } while (rc == VIRTQUEUE_READ_DESC_MORE);
499 
500         if (rc == VIRTQUEUE_READ_DESC_ERROR) {
501             goto err;
502         }
503 
504         if (!indirect)
505             total_bufs = num_bufs;
506         else
507             total_bufs++;
508     }
509 
510     if (rc < 0) {
511         goto err;
512     }
513 
514 done:
515     if (in_bytes) {
516         *in_bytes = in_total;
517     }
518     if (out_bytes) {
519         *out_bytes = out_total;
520     }
521     return;
522 
523 err:
524     in_total = out_total = 0;
525     goto done;
526 }
527 
528 int virtqueue_avail_bytes(VirtQueue *vq, unsigned int in_bytes,
529                           unsigned int out_bytes)
530 {
531     unsigned int in_total, out_total;
532 
533     virtqueue_get_avail_bytes(vq, &in_total, &out_total, in_bytes, out_bytes);
534     return in_bytes <= in_total && out_bytes <= out_total;
535 }
536 
537 static bool virtqueue_map_desc(VirtIODevice *vdev, unsigned int *p_num_sg,
538                                hwaddr *addr, struct iovec *iov,
539                                unsigned int max_num_sg, bool is_write,
540                                hwaddr pa, size_t sz)
541 {
542     bool ok = false;
543     unsigned num_sg = *p_num_sg;
544     assert(num_sg <= max_num_sg);
545 
546     if (!sz) {
547         virtio_error(vdev, "virtio: zero sized buffers are not allowed");
548         goto out;
549     }
550 
551     while (sz) {
552         hwaddr len = sz;
553 
554         if (num_sg == max_num_sg) {
555             virtio_error(vdev, "virtio: too many write descriptors in "
556                                "indirect table");
557             goto out;
558         }
559 
560         iov[num_sg].iov_base = dma_memory_map(vdev->dma_as, pa, &len,
561                                               is_write ?
562                                               DMA_DIRECTION_FROM_DEVICE :
563                                               DMA_DIRECTION_TO_DEVICE);
564         if (!iov[num_sg].iov_base) {
565             virtio_error(vdev, "virtio: bogus descriptor or out of resources");
566             goto out;
567         }
568 
569         iov[num_sg].iov_len = len;
570         addr[num_sg] = pa;
571 
572         sz -= len;
573         pa += len;
574         num_sg++;
575     }
576     ok = true;
577 
578 out:
579     *p_num_sg = num_sg;
580     return ok;
581 }
582 
583 /* Only used by error code paths before we have a VirtQueueElement (therefore
584  * virtqueue_unmap_sg() can't be used).  Assumes buffers weren't written to
585  * yet.
586  */
587 static void virtqueue_undo_map_desc(unsigned int out_num, unsigned int in_num,
588                                     struct iovec *iov)
589 {
590     unsigned int i;
591 
592     for (i = 0; i < out_num + in_num; i++) {
593         int is_write = i >= out_num;
594 
595         cpu_physical_memory_unmap(iov->iov_base, iov->iov_len, is_write, 0);
596         iov++;
597     }
598 }
599 
600 static void virtqueue_map_iovec(VirtIODevice *vdev, struct iovec *sg,
601                                 hwaddr *addr, unsigned int *num_sg,
602                                 unsigned int max_size, int is_write)
603 {
604     unsigned int i;
605     hwaddr len;
606 
607     /* Note: this function MUST validate input, some callers
608      * are passing in num_sg values received over the network.
609      */
610     /* TODO: teach all callers that this can fail, and return failure instead
611      * of asserting here.
612      * When we do, we might be able to re-enable NDEBUG below.
613      */
614 #ifdef NDEBUG
615 #error building with NDEBUG is not supported
616 #endif
617     assert(*num_sg <= max_size);
618 
619     for (i = 0; i < *num_sg; i++) {
620         len = sg[i].iov_len;
621         sg[i].iov_base = dma_memory_map(vdev->dma_as,
622                                         addr[i], &len, is_write ?
623                                         DMA_DIRECTION_FROM_DEVICE :
624                                         DMA_DIRECTION_TO_DEVICE);
625         if (!sg[i].iov_base) {
626             error_report("virtio: error trying to map MMIO memory");
627             exit(1);
628         }
629         if (len != sg[i].iov_len) {
630             error_report("virtio: unexpected memory split");
631             exit(1);
632         }
633     }
634 }
635 
636 void virtqueue_map(VirtIODevice *vdev, VirtQueueElement *elem)
637 {
638     virtqueue_map_iovec(vdev, elem->in_sg, elem->in_addr, &elem->in_num,
639                         MIN(ARRAY_SIZE(elem->in_sg), ARRAY_SIZE(elem->in_addr)),
640                         1);
641     virtqueue_map_iovec(vdev, elem->out_sg, elem->out_addr, &elem->out_num,
642                         MIN(ARRAY_SIZE(elem->out_sg),
643                         ARRAY_SIZE(elem->out_addr)),
644                         0);
645 }
646 
647 static void *virtqueue_alloc_element(size_t sz, unsigned out_num, unsigned in_num)
648 {
649     VirtQueueElement *elem;
650     size_t in_addr_ofs = QEMU_ALIGN_UP(sz, __alignof__(elem->in_addr[0]));
651     size_t out_addr_ofs = in_addr_ofs + in_num * sizeof(elem->in_addr[0]);
652     size_t out_addr_end = out_addr_ofs + out_num * sizeof(elem->out_addr[0]);
653     size_t in_sg_ofs = QEMU_ALIGN_UP(out_addr_end, __alignof__(elem->in_sg[0]));
654     size_t out_sg_ofs = in_sg_ofs + in_num * sizeof(elem->in_sg[0]);
655     size_t out_sg_end = out_sg_ofs + out_num * sizeof(elem->out_sg[0]);
656 
657     assert(sz >= sizeof(VirtQueueElement));
658     elem = g_malloc(out_sg_end);
659     elem->out_num = out_num;
660     elem->in_num = in_num;
661     elem->in_addr = (void *)elem + in_addr_ofs;
662     elem->out_addr = (void *)elem + out_addr_ofs;
663     elem->in_sg = (void *)elem + in_sg_ofs;
664     elem->out_sg = (void *)elem + out_sg_ofs;
665     return elem;
666 }
667 
668 void *virtqueue_pop(VirtQueue *vq, size_t sz)
669 {
670     unsigned int i, head, max;
671     hwaddr desc_pa = vq->vring.desc;
672     VirtIODevice *vdev = vq->vdev;
673     VirtQueueElement *elem;
674     unsigned out_num, in_num;
675     hwaddr addr[VIRTQUEUE_MAX_SIZE];
676     struct iovec iov[VIRTQUEUE_MAX_SIZE];
677     VRingDesc desc;
678     int rc;
679 
680     if (unlikely(vdev->broken)) {
681         return NULL;
682     }
683     if (virtio_queue_empty(vq)) {
684         return NULL;
685     }
686     /* Needed after virtio_queue_empty(), see comment in
687      * virtqueue_num_heads(). */
688     smp_rmb();
689 
690     /* When we start there are none of either input nor output. */
691     out_num = in_num = 0;
692 
693     max = vq->vring.num;
694 
695     if (vq->inuse >= vq->vring.num) {
696         virtio_error(vdev, "Virtqueue size exceeded");
697         return NULL;
698     }
699 
700     if (!virtqueue_get_head(vq, vq->last_avail_idx++, &head)) {
701         return NULL;
702     }
703 
704     if (virtio_vdev_has_feature(vdev, VIRTIO_RING_F_EVENT_IDX)) {
705         vring_set_avail_event(vq, vq->last_avail_idx);
706     }
707 
708     i = head;
709     vring_desc_read(vdev, &desc, desc_pa, i);
710     if (desc.flags & VRING_DESC_F_INDIRECT) {
711         if (desc.len % sizeof(VRingDesc)) {
712             virtio_error(vdev, "Invalid size for indirect buffer table");
713             return NULL;
714         }
715 
716         /* loop over the indirect descriptor table */
717         max = desc.len / sizeof(VRingDesc);
718         desc_pa = desc.addr;
719         i = 0;
720         vring_desc_read(vdev, &desc, desc_pa, i);
721     }
722 
723     /* Collect all the descriptors */
724     do {
725         bool map_ok;
726 
727         if (desc.flags & VRING_DESC_F_WRITE) {
728             map_ok = virtqueue_map_desc(vdev, &in_num, addr + out_num,
729                                         iov + out_num,
730                                         VIRTQUEUE_MAX_SIZE - out_num, true,
731                                         desc.addr, desc.len);
732         } else {
733             if (in_num) {
734                 virtio_error(vdev, "Incorrect order for descriptors");
735                 goto err_undo_map;
736             }
737             map_ok = virtqueue_map_desc(vdev, &out_num, addr, iov,
738                                         VIRTQUEUE_MAX_SIZE, false,
739                                         desc.addr, desc.len);
740         }
741         if (!map_ok) {
742             goto err_undo_map;
743         }
744 
745         /* If we've got too many, that implies a descriptor loop. */
746         if ((in_num + out_num) > max) {
747             virtio_error(vdev, "Looped descriptor");
748             goto err_undo_map;
749         }
750 
751         rc = virtqueue_read_next_desc(vdev, &desc, desc_pa, max, &i);
752     } while (rc == VIRTQUEUE_READ_DESC_MORE);
753 
754     if (rc == VIRTQUEUE_READ_DESC_ERROR) {
755         goto err_undo_map;
756     }
757 
758     /* Now copy what we have collected and mapped */
759     elem = virtqueue_alloc_element(sz, out_num, in_num);
760     elem->index = head;
761     for (i = 0; i < out_num; i++) {
762         elem->out_addr[i] = addr[i];
763         elem->out_sg[i] = iov[i];
764     }
765     for (i = 0; i < in_num; i++) {
766         elem->in_addr[i] = addr[out_num + i];
767         elem->in_sg[i] = iov[out_num + i];
768     }
769 
770     vq->inuse++;
771 
772     trace_virtqueue_pop(vq, elem, elem->in_num, elem->out_num);
773     return elem;
774 
775 err_undo_map:
776     virtqueue_undo_map_desc(out_num, in_num, iov);
777     return NULL;
778 }
779 
780 /* virtqueue_drop_all:
781  * @vq: The #VirtQueue
782  * Drops all queued buffers and indicates them to the guest
783  * as if they are done. Useful when buffers can not be
784  * processed but must be returned to the guest.
785  */
786 unsigned int virtqueue_drop_all(VirtQueue *vq)
787 {
788     unsigned int dropped = 0;
789     VirtQueueElement elem = {};
790     VirtIODevice *vdev = vq->vdev;
791     bool fEventIdx = virtio_vdev_has_feature(vdev, VIRTIO_RING_F_EVENT_IDX);
792 
793     if (unlikely(vdev->broken)) {
794         return 0;
795     }
796 
797     while (!virtio_queue_empty(vq) && vq->inuse < vq->vring.num) {
798         /* works similar to virtqueue_pop but does not map buffers
799         * and does not allocate any memory */
800         smp_rmb();
801         if (!virtqueue_get_head(vq, vq->last_avail_idx, &elem.index)) {
802             break;
803         }
804         vq->inuse++;
805         vq->last_avail_idx++;
806         if (fEventIdx) {
807             vring_set_avail_event(vq, vq->last_avail_idx);
808         }
809         /* immediately push the element, nothing to unmap
810          * as both in_num and out_num are set to 0 */
811         virtqueue_push(vq, &elem, 0);
812         dropped++;
813     }
814 
815     return dropped;
816 }
817 
818 /* Reading and writing a structure directly to QEMUFile is *awful*, but
819  * it is what QEMU has always done by mistake.  We can change it sooner
820  * or later by bumping the version number of the affected vm states.
821  * In the meanwhile, since the in-memory layout of VirtQueueElement
822  * has changed, we need to marshal to and from the layout that was
823  * used before the change.
824  */
825 typedef struct VirtQueueElementOld {
826     unsigned int index;
827     unsigned int out_num;
828     unsigned int in_num;
829     hwaddr in_addr[VIRTQUEUE_MAX_SIZE];
830     hwaddr out_addr[VIRTQUEUE_MAX_SIZE];
831     struct iovec in_sg[VIRTQUEUE_MAX_SIZE];
832     struct iovec out_sg[VIRTQUEUE_MAX_SIZE];
833 } VirtQueueElementOld;
834 
835 void *qemu_get_virtqueue_element(VirtIODevice *vdev, QEMUFile *f, size_t sz)
836 {
837     VirtQueueElement *elem;
838     VirtQueueElementOld data;
839     int i;
840 
841     qemu_get_buffer(f, (uint8_t *)&data, sizeof(VirtQueueElementOld));
842 
843     elem = virtqueue_alloc_element(sz, data.out_num, data.in_num);
844     elem->index = data.index;
845 
846     for (i = 0; i < elem->in_num; i++) {
847         elem->in_addr[i] = data.in_addr[i];
848     }
849 
850     for (i = 0; i < elem->out_num; i++) {
851         elem->out_addr[i] = data.out_addr[i];
852     }
853 
854     for (i = 0; i < elem->in_num; i++) {
855         /* Base is overwritten by virtqueue_map.  */
856         elem->in_sg[i].iov_base = 0;
857         elem->in_sg[i].iov_len = data.in_sg[i].iov_len;
858     }
859 
860     for (i = 0; i < elem->out_num; i++) {
861         /* Base is overwritten by virtqueue_map.  */
862         elem->out_sg[i].iov_base = 0;
863         elem->out_sg[i].iov_len = data.out_sg[i].iov_len;
864     }
865 
866     virtqueue_map(vdev, elem);
867     return elem;
868 }
869 
870 void qemu_put_virtqueue_element(QEMUFile *f, VirtQueueElement *elem)
871 {
872     VirtQueueElementOld data;
873     int i;
874 
875     memset(&data, 0, sizeof(data));
876     data.index = elem->index;
877     data.in_num = elem->in_num;
878     data.out_num = elem->out_num;
879 
880     for (i = 0; i < elem->in_num; i++) {
881         data.in_addr[i] = elem->in_addr[i];
882     }
883 
884     for (i = 0; i < elem->out_num; i++) {
885         data.out_addr[i] = elem->out_addr[i];
886     }
887 
888     for (i = 0; i < elem->in_num; i++) {
889         /* Base is overwritten by virtqueue_map when loading.  Do not
890          * save it, as it would leak the QEMU address space layout.  */
891         data.in_sg[i].iov_len = elem->in_sg[i].iov_len;
892     }
893 
894     for (i = 0; i < elem->out_num; i++) {
895         /* Do not save iov_base as above.  */
896         data.out_sg[i].iov_len = elem->out_sg[i].iov_len;
897     }
898     qemu_put_buffer(f, (uint8_t *)&data, sizeof(VirtQueueElementOld));
899 }
900 
901 /* virtio device */
902 static void virtio_notify_vector(VirtIODevice *vdev, uint16_t vector)
903 {
904     BusState *qbus = qdev_get_parent_bus(DEVICE(vdev));
905     VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
906 
907     if (unlikely(vdev->broken)) {
908         return;
909     }
910 
911     if (k->notify) {
912         k->notify(qbus->parent, vector);
913     }
914 }
915 
916 void virtio_update_irq(VirtIODevice *vdev)
917 {
918     virtio_notify_vector(vdev, VIRTIO_NO_VECTOR);
919 }
920 
921 static int virtio_validate_features(VirtIODevice *vdev)
922 {
923     VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
924 
925     if (virtio_host_has_feature(vdev, VIRTIO_F_IOMMU_PLATFORM) &&
926         !virtio_vdev_has_feature(vdev, VIRTIO_F_IOMMU_PLATFORM)) {
927         return -EFAULT;
928     }
929 
930     if (k->validate_features) {
931         return k->validate_features(vdev);
932     } else {
933         return 0;
934     }
935 }
936 
937 int virtio_set_status(VirtIODevice *vdev, uint8_t val)
938 {
939     VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
940     trace_virtio_set_status(vdev, val);
941 
942     if (virtio_vdev_has_feature(vdev, VIRTIO_F_VERSION_1)) {
943         if (!(vdev->status & VIRTIO_CONFIG_S_FEATURES_OK) &&
944             val & VIRTIO_CONFIG_S_FEATURES_OK) {
945             int ret = virtio_validate_features(vdev);
946 
947             if (ret) {
948                 return ret;
949             }
950         }
951     }
952     if (k->set_status) {
953         k->set_status(vdev, val);
954     }
955     vdev->status = val;
956     return 0;
957 }
958 
959 bool target_words_bigendian(void);
960 static enum virtio_device_endian virtio_default_endian(void)
961 {
962     if (target_words_bigendian()) {
963         return VIRTIO_DEVICE_ENDIAN_BIG;
964     } else {
965         return VIRTIO_DEVICE_ENDIAN_LITTLE;
966     }
967 }
968 
969 static enum virtio_device_endian virtio_current_cpu_endian(void)
970 {
971     CPUClass *cc = CPU_GET_CLASS(current_cpu);
972 
973     if (cc->virtio_is_big_endian(current_cpu)) {
974         return VIRTIO_DEVICE_ENDIAN_BIG;
975     } else {
976         return VIRTIO_DEVICE_ENDIAN_LITTLE;
977     }
978 }
979 
980 void virtio_reset(void *opaque)
981 {
982     VirtIODevice *vdev = opaque;
983     VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
984     int i;
985 
986     virtio_set_status(vdev, 0);
987     if (current_cpu) {
988         /* Guest initiated reset */
989         vdev->device_endian = virtio_current_cpu_endian();
990     } else {
991         /* System reset */
992         vdev->device_endian = virtio_default_endian();
993     }
994 
995     if (k->reset) {
996         k->reset(vdev);
997     }
998 
999     vdev->broken = false;
1000     vdev->guest_features = 0;
1001     vdev->queue_sel = 0;
1002     vdev->status = 0;
1003     atomic_set(&vdev->isr, 0);
1004     vdev->config_vector = VIRTIO_NO_VECTOR;
1005     virtio_notify_vector(vdev, vdev->config_vector);
1006 
1007     for(i = 0; i < VIRTIO_QUEUE_MAX; i++) {
1008         vdev->vq[i].vring.desc = 0;
1009         vdev->vq[i].vring.avail = 0;
1010         vdev->vq[i].vring.used = 0;
1011         vdev->vq[i].last_avail_idx = 0;
1012         vdev->vq[i].shadow_avail_idx = 0;
1013         vdev->vq[i].used_idx = 0;
1014         virtio_queue_set_vector(vdev, i, VIRTIO_NO_VECTOR);
1015         vdev->vq[i].signalled_used = 0;
1016         vdev->vq[i].signalled_used_valid = false;
1017         vdev->vq[i].notification = true;
1018         vdev->vq[i].vring.num = vdev->vq[i].vring.num_default;
1019         vdev->vq[i].inuse = 0;
1020     }
1021 }
1022 
1023 uint32_t virtio_config_readb(VirtIODevice *vdev, uint32_t addr)
1024 {
1025     VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
1026     uint8_t val;
1027 
1028     if (addr + sizeof(val) > vdev->config_len) {
1029         return (uint32_t)-1;
1030     }
1031 
1032     k->get_config(vdev, vdev->config);
1033 
1034     val = ldub_p(vdev->config + addr);
1035     return val;
1036 }
1037 
1038 uint32_t virtio_config_readw(VirtIODevice *vdev, uint32_t addr)
1039 {
1040     VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
1041     uint16_t val;
1042 
1043     if (addr + sizeof(val) > vdev->config_len) {
1044         return (uint32_t)-1;
1045     }
1046 
1047     k->get_config(vdev, vdev->config);
1048 
1049     val = lduw_p(vdev->config + addr);
1050     return val;
1051 }
1052 
1053 uint32_t virtio_config_readl(VirtIODevice *vdev, uint32_t addr)
1054 {
1055     VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
1056     uint32_t val;
1057 
1058     if (addr + sizeof(val) > vdev->config_len) {
1059         return (uint32_t)-1;
1060     }
1061 
1062     k->get_config(vdev, vdev->config);
1063 
1064     val = ldl_p(vdev->config + addr);
1065     return val;
1066 }
1067 
1068 void virtio_config_writeb(VirtIODevice *vdev, uint32_t addr, uint32_t data)
1069 {
1070     VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
1071     uint8_t val = data;
1072 
1073     if (addr + sizeof(val) > vdev->config_len) {
1074         return;
1075     }
1076 
1077     stb_p(vdev->config + addr, val);
1078 
1079     if (k->set_config) {
1080         k->set_config(vdev, vdev->config);
1081     }
1082 }
1083 
1084 void virtio_config_writew(VirtIODevice *vdev, uint32_t addr, uint32_t data)
1085 {
1086     VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
1087     uint16_t val = data;
1088 
1089     if (addr + sizeof(val) > vdev->config_len) {
1090         return;
1091     }
1092 
1093     stw_p(vdev->config + addr, val);
1094 
1095     if (k->set_config) {
1096         k->set_config(vdev, vdev->config);
1097     }
1098 }
1099 
1100 void virtio_config_writel(VirtIODevice *vdev, uint32_t addr, uint32_t data)
1101 {
1102     VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
1103     uint32_t val = data;
1104 
1105     if (addr + sizeof(val) > vdev->config_len) {
1106         return;
1107     }
1108 
1109     stl_p(vdev->config + addr, val);
1110 
1111     if (k->set_config) {
1112         k->set_config(vdev, vdev->config);
1113     }
1114 }
1115 
1116 uint32_t virtio_config_modern_readb(VirtIODevice *vdev, uint32_t addr)
1117 {
1118     VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
1119     uint8_t val;
1120 
1121     if (addr + sizeof(val) > vdev->config_len) {
1122         return (uint32_t)-1;
1123     }
1124 
1125     k->get_config(vdev, vdev->config);
1126 
1127     val = ldub_p(vdev->config + addr);
1128     return val;
1129 }
1130 
1131 uint32_t virtio_config_modern_readw(VirtIODevice *vdev, uint32_t addr)
1132 {
1133     VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
1134     uint16_t val;
1135 
1136     if (addr + sizeof(val) > vdev->config_len) {
1137         return (uint32_t)-1;
1138     }
1139 
1140     k->get_config(vdev, vdev->config);
1141 
1142     val = lduw_le_p(vdev->config + addr);
1143     return val;
1144 }
1145 
1146 uint32_t virtio_config_modern_readl(VirtIODevice *vdev, uint32_t addr)
1147 {
1148     VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
1149     uint32_t val;
1150 
1151     if (addr + sizeof(val) > vdev->config_len) {
1152         return (uint32_t)-1;
1153     }
1154 
1155     k->get_config(vdev, vdev->config);
1156 
1157     val = ldl_le_p(vdev->config + addr);
1158     return val;
1159 }
1160 
1161 void virtio_config_modern_writeb(VirtIODevice *vdev,
1162                                  uint32_t addr, uint32_t data)
1163 {
1164     VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
1165     uint8_t val = data;
1166 
1167     if (addr + sizeof(val) > vdev->config_len) {
1168         return;
1169     }
1170 
1171     stb_p(vdev->config + addr, val);
1172 
1173     if (k->set_config) {
1174         k->set_config(vdev, vdev->config);
1175     }
1176 }
1177 
1178 void virtio_config_modern_writew(VirtIODevice *vdev,
1179                                  uint32_t addr, uint32_t data)
1180 {
1181     VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
1182     uint16_t val = data;
1183 
1184     if (addr + sizeof(val) > vdev->config_len) {
1185         return;
1186     }
1187 
1188     stw_le_p(vdev->config + addr, val);
1189 
1190     if (k->set_config) {
1191         k->set_config(vdev, vdev->config);
1192     }
1193 }
1194 
1195 void virtio_config_modern_writel(VirtIODevice *vdev,
1196                                  uint32_t addr, uint32_t data)
1197 {
1198     VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
1199     uint32_t val = data;
1200 
1201     if (addr + sizeof(val) > vdev->config_len) {
1202         return;
1203     }
1204 
1205     stl_le_p(vdev->config + addr, val);
1206 
1207     if (k->set_config) {
1208         k->set_config(vdev, vdev->config);
1209     }
1210 }
1211 
1212 void virtio_queue_set_addr(VirtIODevice *vdev, int n, hwaddr addr)
1213 {
1214     vdev->vq[n].vring.desc = addr;
1215     virtio_queue_update_rings(vdev, n);
1216 }
1217 
1218 hwaddr virtio_queue_get_addr(VirtIODevice *vdev, int n)
1219 {
1220     return vdev->vq[n].vring.desc;
1221 }
1222 
1223 void virtio_queue_set_rings(VirtIODevice *vdev, int n, hwaddr desc,
1224                             hwaddr avail, hwaddr used)
1225 {
1226     vdev->vq[n].vring.desc = desc;
1227     vdev->vq[n].vring.avail = avail;
1228     vdev->vq[n].vring.used = used;
1229 }
1230 
1231 void virtio_queue_set_num(VirtIODevice *vdev, int n, int num)
1232 {
1233     /* Don't allow guest to flip queue between existent and
1234      * nonexistent states, or to set it to an invalid size.
1235      */
1236     if (!!num != !!vdev->vq[n].vring.num ||
1237         num > VIRTQUEUE_MAX_SIZE ||
1238         num < 0) {
1239         return;
1240     }
1241     vdev->vq[n].vring.num = num;
1242 }
1243 
1244 VirtQueue *virtio_vector_first_queue(VirtIODevice *vdev, uint16_t vector)
1245 {
1246     return QLIST_FIRST(&vdev->vector_queues[vector]);
1247 }
1248 
1249 VirtQueue *virtio_vector_next_queue(VirtQueue *vq)
1250 {
1251     return QLIST_NEXT(vq, node);
1252 }
1253 
1254 int virtio_queue_get_num(VirtIODevice *vdev, int n)
1255 {
1256     return vdev->vq[n].vring.num;
1257 }
1258 
1259 int virtio_get_num_queues(VirtIODevice *vdev)
1260 {
1261     int i;
1262 
1263     for (i = 0; i < VIRTIO_QUEUE_MAX; i++) {
1264         if (!virtio_queue_get_num(vdev, i)) {
1265             break;
1266         }
1267     }
1268 
1269     return i;
1270 }
1271 
1272 void virtio_queue_set_align(VirtIODevice *vdev, int n, int align)
1273 {
1274     BusState *qbus = qdev_get_parent_bus(DEVICE(vdev));
1275     VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
1276 
1277     /* virtio-1 compliant devices cannot change the alignment */
1278     if (virtio_vdev_has_feature(vdev, VIRTIO_F_VERSION_1)) {
1279         error_report("tried to modify queue alignment for virtio-1 device");
1280         return;
1281     }
1282     /* Check that the transport told us it was going to do this
1283      * (so a buggy transport will immediately assert rather than
1284      * silently failing to migrate this state)
1285      */
1286     assert(k->has_variable_vring_alignment);
1287 
1288     vdev->vq[n].vring.align = align;
1289     virtio_queue_update_rings(vdev, n);
1290 }
1291 
1292 static void virtio_queue_notify_aio_vq(VirtQueue *vq)
1293 {
1294     if (vq->vring.desc && vq->handle_aio_output) {
1295         VirtIODevice *vdev = vq->vdev;
1296 
1297         trace_virtio_queue_notify(vdev, vq - vdev->vq, vq);
1298         vq->handle_aio_output(vdev, vq);
1299     }
1300 }
1301 
1302 static void virtio_queue_notify_vq(VirtQueue *vq)
1303 {
1304     if (vq->vring.desc && vq->handle_output) {
1305         VirtIODevice *vdev = vq->vdev;
1306 
1307         if (unlikely(vdev->broken)) {
1308             return;
1309         }
1310 
1311         trace_virtio_queue_notify(vdev, vq - vdev->vq, vq);
1312         vq->handle_output(vdev, vq);
1313     }
1314 }
1315 
1316 void virtio_queue_notify(VirtIODevice *vdev, int n)
1317 {
1318     virtio_queue_notify_vq(&vdev->vq[n]);
1319 }
1320 
1321 uint16_t virtio_queue_vector(VirtIODevice *vdev, int n)
1322 {
1323     return n < VIRTIO_QUEUE_MAX ? vdev->vq[n].vector :
1324         VIRTIO_NO_VECTOR;
1325 }
1326 
1327 void virtio_queue_set_vector(VirtIODevice *vdev, int n, uint16_t vector)
1328 {
1329     VirtQueue *vq = &vdev->vq[n];
1330 
1331     if (n < VIRTIO_QUEUE_MAX) {
1332         if (vdev->vector_queues &&
1333             vdev->vq[n].vector != VIRTIO_NO_VECTOR) {
1334             QLIST_REMOVE(vq, node);
1335         }
1336         vdev->vq[n].vector = vector;
1337         if (vdev->vector_queues &&
1338             vector != VIRTIO_NO_VECTOR) {
1339             QLIST_INSERT_HEAD(&vdev->vector_queues[vector], vq, node);
1340         }
1341     }
1342 }
1343 
1344 VirtQueue *virtio_add_queue(VirtIODevice *vdev, int queue_size,
1345                             VirtIOHandleOutput handle_output)
1346 {
1347     int i;
1348 
1349     for (i = 0; i < VIRTIO_QUEUE_MAX; i++) {
1350         if (vdev->vq[i].vring.num == 0)
1351             break;
1352     }
1353 
1354     if (i == VIRTIO_QUEUE_MAX || queue_size > VIRTQUEUE_MAX_SIZE)
1355         abort();
1356 
1357     vdev->vq[i].vring.num = queue_size;
1358     vdev->vq[i].vring.num_default = queue_size;
1359     vdev->vq[i].vring.align = VIRTIO_PCI_VRING_ALIGN;
1360     vdev->vq[i].handle_output = handle_output;
1361     vdev->vq[i].handle_aio_output = NULL;
1362 
1363     return &vdev->vq[i];
1364 }
1365 
1366 void virtio_del_queue(VirtIODevice *vdev, int n)
1367 {
1368     if (n < 0 || n >= VIRTIO_QUEUE_MAX) {
1369         abort();
1370     }
1371 
1372     vdev->vq[n].vring.num = 0;
1373     vdev->vq[n].vring.num_default = 0;
1374 }
1375 
1376 static void virtio_set_isr(VirtIODevice *vdev, int value)
1377 {
1378     uint8_t old = atomic_read(&vdev->isr);
1379 
1380     /* Do not write ISR if it does not change, so that its cacheline remains
1381      * shared in the common case where the guest does not read it.
1382      */
1383     if ((old & value) != value) {
1384         atomic_or(&vdev->isr, value);
1385     }
1386 }
1387 
1388 bool virtio_should_notify(VirtIODevice *vdev, VirtQueue *vq)
1389 {
1390     uint16_t old, new;
1391     bool v;
1392     /* We need to expose used array entries before checking used event. */
1393     smp_mb();
1394     /* Always notify when queue is empty (when feature acknowledge) */
1395     if (virtio_vdev_has_feature(vdev, VIRTIO_F_NOTIFY_ON_EMPTY) &&
1396         !vq->inuse && virtio_queue_empty(vq)) {
1397         return true;
1398     }
1399 
1400     if (!virtio_vdev_has_feature(vdev, VIRTIO_RING_F_EVENT_IDX)) {
1401         return !(vring_avail_flags(vq) & VRING_AVAIL_F_NO_INTERRUPT);
1402     }
1403 
1404     v = vq->signalled_used_valid;
1405     vq->signalled_used_valid = true;
1406     old = vq->signalled_used;
1407     new = vq->signalled_used = vq->used_idx;
1408     return !v || vring_need_event(vring_get_used_event(vq), new, old);
1409 }
1410 
1411 void virtio_notify_irqfd(VirtIODevice *vdev, VirtQueue *vq)
1412 {
1413     if (!virtio_should_notify(vdev, vq)) {
1414         return;
1415     }
1416 
1417     trace_virtio_notify_irqfd(vdev, vq);
1418 
1419     /*
1420      * virtio spec 1.0 says ISR bit 0 should be ignored with MSI, but
1421      * windows drivers included in virtio-win 1.8.0 (circa 2015) are
1422      * incorrectly polling this bit during crashdump and hibernation
1423      * in MSI mode, causing a hang if this bit is never updated.
1424      * Recent releases of Windows do not really shut down, but rather
1425      * log out and hibernate to make the next startup faster.  Hence,
1426      * this manifested as a more serious hang during shutdown with
1427      *
1428      * Next driver release from 2016 fixed this problem, so working around it
1429      * is not a must, but it's easy to do so let's do it here.
1430      *
1431      * Note: it's safe to update ISR from any thread as it was switched
1432      * to an atomic operation.
1433      */
1434     virtio_set_isr(vq->vdev, 0x1);
1435     event_notifier_set(&vq->guest_notifier);
1436 }
1437 
1438 void virtio_notify(VirtIODevice *vdev, VirtQueue *vq)
1439 {
1440     if (!virtio_should_notify(vdev, vq)) {
1441         return;
1442     }
1443 
1444     trace_virtio_notify(vdev, vq);
1445     virtio_set_isr(vq->vdev, 0x1);
1446     virtio_notify_vector(vdev, vq->vector);
1447 }
1448 
1449 void virtio_notify_config(VirtIODevice *vdev)
1450 {
1451     if (!(vdev->status & VIRTIO_CONFIG_S_DRIVER_OK))
1452         return;
1453 
1454     virtio_set_isr(vdev, 0x3);
1455     vdev->generation++;
1456     virtio_notify_vector(vdev, vdev->config_vector);
1457 }
1458 
1459 static bool virtio_device_endian_needed(void *opaque)
1460 {
1461     VirtIODevice *vdev = opaque;
1462 
1463     assert(vdev->device_endian != VIRTIO_DEVICE_ENDIAN_UNKNOWN);
1464     if (!virtio_vdev_has_feature(vdev, VIRTIO_F_VERSION_1)) {
1465         return vdev->device_endian != virtio_default_endian();
1466     }
1467     /* Devices conforming to VIRTIO 1.0 or later are always LE. */
1468     return vdev->device_endian != VIRTIO_DEVICE_ENDIAN_LITTLE;
1469 }
1470 
1471 static bool virtio_64bit_features_needed(void *opaque)
1472 {
1473     VirtIODevice *vdev = opaque;
1474 
1475     return (vdev->host_features >> 32) != 0;
1476 }
1477 
1478 static bool virtio_virtqueue_needed(void *opaque)
1479 {
1480     VirtIODevice *vdev = opaque;
1481 
1482     return virtio_host_has_feature(vdev, VIRTIO_F_VERSION_1);
1483 }
1484 
1485 static bool virtio_ringsize_needed(void *opaque)
1486 {
1487     VirtIODevice *vdev = opaque;
1488     int i;
1489 
1490     for (i = 0; i < VIRTIO_QUEUE_MAX; i++) {
1491         if (vdev->vq[i].vring.num != vdev->vq[i].vring.num_default) {
1492             return true;
1493         }
1494     }
1495     return false;
1496 }
1497 
1498 static bool virtio_extra_state_needed(void *opaque)
1499 {
1500     VirtIODevice *vdev = opaque;
1501     BusState *qbus = qdev_get_parent_bus(DEVICE(vdev));
1502     VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
1503 
1504     return k->has_extra_state &&
1505         k->has_extra_state(qbus->parent);
1506 }
1507 
1508 static bool virtio_broken_needed(void *opaque)
1509 {
1510     VirtIODevice *vdev = opaque;
1511 
1512     return vdev->broken;
1513 }
1514 
1515 static const VMStateDescription vmstate_virtqueue = {
1516     .name = "virtqueue_state",
1517     .version_id = 1,
1518     .minimum_version_id = 1,
1519     .fields = (VMStateField[]) {
1520         VMSTATE_UINT64(vring.avail, struct VirtQueue),
1521         VMSTATE_UINT64(vring.used, struct VirtQueue),
1522         VMSTATE_END_OF_LIST()
1523     }
1524 };
1525 
1526 static const VMStateDescription vmstate_virtio_virtqueues = {
1527     .name = "virtio/virtqueues",
1528     .version_id = 1,
1529     .minimum_version_id = 1,
1530     .needed = &virtio_virtqueue_needed,
1531     .fields = (VMStateField[]) {
1532         VMSTATE_STRUCT_VARRAY_POINTER_KNOWN(vq, struct VirtIODevice,
1533                       VIRTIO_QUEUE_MAX, 0, vmstate_virtqueue, VirtQueue),
1534         VMSTATE_END_OF_LIST()
1535     }
1536 };
1537 
1538 static const VMStateDescription vmstate_ringsize = {
1539     .name = "ringsize_state",
1540     .version_id = 1,
1541     .minimum_version_id = 1,
1542     .fields = (VMStateField[]) {
1543         VMSTATE_UINT32(vring.num_default, struct VirtQueue),
1544         VMSTATE_END_OF_LIST()
1545     }
1546 };
1547 
1548 static const VMStateDescription vmstate_virtio_ringsize = {
1549     .name = "virtio/ringsize",
1550     .version_id = 1,
1551     .minimum_version_id = 1,
1552     .needed = &virtio_ringsize_needed,
1553     .fields = (VMStateField[]) {
1554         VMSTATE_STRUCT_VARRAY_POINTER_KNOWN(vq, struct VirtIODevice,
1555                       VIRTIO_QUEUE_MAX, 0, vmstate_ringsize, VirtQueue),
1556         VMSTATE_END_OF_LIST()
1557     }
1558 };
1559 
1560 static int get_extra_state(QEMUFile *f, void *pv, size_t size)
1561 {
1562     VirtIODevice *vdev = pv;
1563     BusState *qbus = qdev_get_parent_bus(DEVICE(vdev));
1564     VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
1565 
1566     if (!k->load_extra_state) {
1567         return -1;
1568     } else {
1569         return k->load_extra_state(qbus->parent, f);
1570     }
1571 }
1572 
1573 static void put_extra_state(QEMUFile *f, void *pv, size_t size)
1574 {
1575     VirtIODevice *vdev = pv;
1576     BusState *qbus = qdev_get_parent_bus(DEVICE(vdev));
1577     VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
1578 
1579     k->save_extra_state(qbus->parent, f);
1580 }
1581 
1582 static const VMStateInfo vmstate_info_extra_state = {
1583     .name = "virtqueue_extra_state",
1584     .get = get_extra_state,
1585     .put = put_extra_state,
1586 };
1587 
1588 static const VMStateDescription vmstate_virtio_extra_state = {
1589     .name = "virtio/extra_state",
1590     .version_id = 1,
1591     .minimum_version_id = 1,
1592     .needed = &virtio_extra_state_needed,
1593     .fields = (VMStateField[]) {
1594         {
1595             .name         = "extra_state",
1596             .version_id   = 0,
1597             .field_exists = NULL,
1598             .size         = 0,
1599             .info         = &vmstate_info_extra_state,
1600             .flags        = VMS_SINGLE,
1601             .offset       = 0,
1602         },
1603         VMSTATE_END_OF_LIST()
1604     }
1605 };
1606 
1607 static const VMStateDescription vmstate_virtio_device_endian = {
1608     .name = "virtio/device_endian",
1609     .version_id = 1,
1610     .minimum_version_id = 1,
1611     .needed = &virtio_device_endian_needed,
1612     .fields = (VMStateField[]) {
1613         VMSTATE_UINT8(device_endian, VirtIODevice),
1614         VMSTATE_END_OF_LIST()
1615     }
1616 };
1617 
1618 static const VMStateDescription vmstate_virtio_64bit_features = {
1619     .name = "virtio/64bit_features",
1620     .version_id = 1,
1621     .minimum_version_id = 1,
1622     .needed = &virtio_64bit_features_needed,
1623     .fields = (VMStateField[]) {
1624         VMSTATE_UINT64(guest_features, VirtIODevice),
1625         VMSTATE_END_OF_LIST()
1626     }
1627 };
1628 
1629 static const VMStateDescription vmstate_virtio_broken = {
1630     .name = "virtio/broken",
1631     .version_id = 1,
1632     .minimum_version_id = 1,
1633     .needed = &virtio_broken_needed,
1634     .fields = (VMStateField[]) {
1635         VMSTATE_BOOL(broken, VirtIODevice),
1636         VMSTATE_END_OF_LIST()
1637     }
1638 };
1639 
1640 static const VMStateDescription vmstate_virtio = {
1641     .name = "virtio",
1642     .version_id = 1,
1643     .minimum_version_id = 1,
1644     .minimum_version_id_old = 1,
1645     .fields = (VMStateField[]) {
1646         VMSTATE_END_OF_LIST()
1647     },
1648     .subsections = (const VMStateDescription*[]) {
1649         &vmstate_virtio_device_endian,
1650         &vmstate_virtio_64bit_features,
1651         &vmstate_virtio_virtqueues,
1652         &vmstate_virtio_ringsize,
1653         &vmstate_virtio_broken,
1654         &vmstate_virtio_extra_state,
1655         NULL
1656     }
1657 };
1658 
1659 void virtio_save(VirtIODevice *vdev, QEMUFile *f)
1660 {
1661     BusState *qbus = qdev_get_parent_bus(DEVICE(vdev));
1662     VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
1663     VirtioDeviceClass *vdc = VIRTIO_DEVICE_GET_CLASS(vdev);
1664     uint32_t guest_features_lo = (vdev->guest_features & 0xffffffff);
1665     int i;
1666 
1667     if (k->save_config) {
1668         k->save_config(qbus->parent, f);
1669     }
1670 
1671     qemu_put_8s(f, &vdev->status);
1672     qemu_put_8s(f, &vdev->isr);
1673     qemu_put_be16s(f, &vdev->queue_sel);
1674     qemu_put_be32s(f, &guest_features_lo);
1675     qemu_put_be32(f, vdev->config_len);
1676     qemu_put_buffer(f, vdev->config, vdev->config_len);
1677 
1678     for (i = 0; i < VIRTIO_QUEUE_MAX; i++) {
1679         if (vdev->vq[i].vring.num == 0)
1680             break;
1681     }
1682 
1683     qemu_put_be32(f, i);
1684 
1685     for (i = 0; i < VIRTIO_QUEUE_MAX; i++) {
1686         if (vdev->vq[i].vring.num == 0)
1687             break;
1688 
1689         qemu_put_be32(f, vdev->vq[i].vring.num);
1690         if (k->has_variable_vring_alignment) {
1691             qemu_put_be32(f, vdev->vq[i].vring.align);
1692         }
1693         /* XXX virtio-1 devices */
1694         qemu_put_be64(f, vdev->vq[i].vring.desc);
1695         qemu_put_be16s(f, &vdev->vq[i].last_avail_idx);
1696         if (k->save_queue) {
1697             k->save_queue(qbus->parent, i, f);
1698         }
1699     }
1700 
1701     if (vdc->save != NULL) {
1702         vdc->save(vdev, f);
1703     }
1704 
1705     if (vdc->vmsd) {
1706         vmstate_save_state(f, vdc->vmsd, vdev, NULL);
1707     }
1708 
1709     /* Subsections */
1710     vmstate_save_state(f, &vmstate_virtio, vdev, NULL);
1711 }
1712 
1713 /* A wrapper for use as a VMState .put function */
1714 static void virtio_device_put(QEMUFile *f, void *opaque, size_t size)
1715 {
1716     virtio_save(VIRTIO_DEVICE(opaque), f);
1717 }
1718 
1719 /* A wrapper for use as a VMState .get function */
1720 static int virtio_device_get(QEMUFile *f, void *opaque, size_t size)
1721 {
1722     VirtIODevice *vdev = VIRTIO_DEVICE(opaque);
1723     DeviceClass *dc = DEVICE_CLASS(VIRTIO_DEVICE_GET_CLASS(vdev));
1724 
1725     return virtio_load(vdev, f, dc->vmsd->version_id);
1726 }
1727 
1728 const VMStateInfo  virtio_vmstate_info = {
1729     .name = "virtio",
1730     .get = virtio_device_get,
1731     .put = virtio_device_put,
1732 };
1733 
1734 static int virtio_set_features_nocheck(VirtIODevice *vdev, uint64_t val)
1735 {
1736     VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
1737     bool bad = (val & ~(vdev->host_features)) != 0;
1738 
1739     val &= vdev->host_features;
1740     if (k->set_features) {
1741         k->set_features(vdev, val);
1742     }
1743     vdev->guest_features = val;
1744     return bad ? -1 : 0;
1745 }
1746 
1747 int virtio_set_features(VirtIODevice *vdev, uint64_t val)
1748 {
1749    /*
1750      * The driver must not attempt to set features after feature negotiation
1751      * has finished.
1752      */
1753     if (vdev->status & VIRTIO_CONFIG_S_FEATURES_OK) {
1754         return -EINVAL;
1755     }
1756     return virtio_set_features_nocheck(vdev, val);
1757 }
1758 
1759 int virtio_load(VirtIODevice *vdev, QEMUFile *f, int version_id)
1760 {
1761     int i, ret;
1762     int32_t config_len;
1763     uint32_t num;
1764     uint32_t features;
1765     BusState *qbus = qdev_get_parent_bus(DEVICE(vdev));
1766     VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
1767     VirtioDeviceClass *vdc = VIRTIO_DEVICE_GET_CLASS(vdev);
1768 
1769     /*
1770      * We poison the endianness to ensure it does not get used before
1771      * subsections have been loaded.
1772      */
1773     vdev->device_endian = VIRTIO_DEVICE_ENDIAN_UNKNOWN;
1774 
1775     if (k->load_config) {
1776         ret = k->load_config(qbus->parent, f);
1777         if (ret)
1778             return ret;
1779     }
1780 
1781     qemu_get_8s(f, &vdev->status);
1782     qemu_get_8s(f, &vdev->isr);
1783     qemu_get_be16s(f, &vdev->queue_sel);
1784     if (vdev->queue_sel >= VIRTIO_QUEUE_MAX) {
1785         return -1;
1786     }
1787     qemu_get_be32s(f, &features);
1788 
1789     /*
1790      * Temporarily set guest_features low bits - needed by
1791      * virtio net load code testing for VIRTIO_NET_F_CTRL_GUEST_OFFLOADS
1792      * VIRTIO_NET_F_GUEST_ANNOUNCE and VIRTIO_NET_F_CTRL_VQ.
1793      *
1794      * Note: devices should always test host features in future - don't create
1795      * new dependencies like this.
1796      */
1797     vdev->guest_features = features;
1798 
1799     config_len = qemu_get_be32(f);
1800 
1801     /*
1802      * There are cases where the incoming config can be bigger or smaller
1803      * than what we have; so load what we have space for, and skip
1804      * any excess that's in the stream.
1805      */
1806     qemu_get_buffer(f, vdev->config, MIN(config_len, vdev->config_len));
1807 
1808     while (config_len > vdev->config_len) {
1809         qemu_get_byte(f);
1810         config_len--;
1811     }
1812 
1813     num = qemu_get_be32(f);
1814 
1815     if (num > VIRTIO_QUEUE_MAX) {
1816         error_report("Invalid number of virtqueues: 0x%x", num);
1817         return -1;
1818     }
1819 
1820     for (i = 0; i < num; i++) {
1821         vdev->vq[i].vring.num = qemu_get_be32(f);
1822         if (k->has_variable_vring_alignment) {
1823             vdev->vq[i].vring.align = qemu_get_be32(f);
1824         }
1825         vdev->vq[i].vring.desc = qemu_get_be64(f);
1826         qemu_get_be16s(f, &vdev->vq[i].last_avail_idx);
1827         vdev->vq[i].signalled_used_valid = false;
1828         vdev->vq[i].notification = true;
1829 
1830         if (vdev->vq[i].vring.desc) {
1831             /* XXX virtio-1 devices */
1832             virtio_queue_update_rings(vdev, i);
1833         } else if (vdev->vq[i].last_avail_idx) {
1834             error_report("VQ %d address 0x0 "
1835                          "inconsistent with Host index 0x%x",
1836                          i, vdev->vq[i].last_avail_idx);
1837                 return -1;
1838         }
1839         if (k->load_queue) {
1840             ret = k->load_queue(qbus->parent, i, f);
1841             if (ret)
1842                 return ret;
1843         }
1844     }
1845 
1846     virtio_notify_vector(vdev, VIRTIO_NO_VECTOR);
1847 
1848     if (vdc->load != NULL) {
1849         ret = vdc->load(vdev, f, version_id);
1850         if (ret) {
1851             return ret;
1852         }
1853     }
1854 
1855     if (vdc->vmsd) {
1856         ret = vmstate_load_state(f, vdc->vmsd, vdev, version_id);
1857         if (ret) {
1858             return ret;
1859         }
1860     }
1861 
1862     /* Subsections */
1863     ret = vmstate_load_state(f, &vmstate_virtio, vdev, 1);
1864     if (ret) {
1865         return ret;
1866     }
1867 
1868     if (vdev->device_endian == VIRTIO_DEVICE_ENDIAN_UNKNOWN) {
1869         vdev->device_endian = virtio_default_endian();
1870     }
1871 
1872     if (virtio_64bit_features_needed(vdev)) {
1873         /*
1874          * Subsection load filled vdev->guest_features.  Run them
1875          * through virtio_set_features to sanity-check them against
1876          * host_features.
1877          */
1878         uint64_t features64 = vdev->guest_features;
1879         if (virtio_set_features_nocheck(vdev, features64) < 0) {
1880             error_report("Features 0x%" PRIx64 " unsupported. "
1881                          "Allowed features: 0x%" PRIx64,
1882                          features64, vdev->host_features);
1883             return -1;
1884         }
1885     } else {
1886         if (virtio_set_features_nocheck(vdev, features) < 0) {
1887             error_report("Features 0x%x unsupported. "
1888                          "Allowed features: 0x%" PRIx64,
1889                          features, vdev->host_features);
1890             return -1;
1891         }
1892     }
1893 
1894     for (i = 0; i < num; i++) {
1895         if (vdev->vq[i].vring.desc) {
1896             uint16_t nheads;
1897             nheads = vring_avail_idx(&vdev->vq[i]) - vdev->vq[i].last_avail_idx;
1898             /* Check it isn't doing strange things with descriptor numbers. */
1899             if (nheads > vdev->vq[i].vring.num) {
1900                 error_report("VQ %d size 0x%x Guest index 0x%x "
1901                              "inconsistent with Host index 0x%x: delta 0x%x",
1902                              i, vdev->vq[i].vring.num,
1903                              vring_avail_idx(&vdev->vq[i]),
1904                              vdev->vq[i].last_avail_idx, nheads);
1905                 return -1;
1906             }
1907             vdev->vq[i].used_idx = vring_used_idx(&vdev->vq[i]);
1908             vdev->vq[i].shadow_avail_idx = vring_avail_idx(&vdev->vq[i]);
1909 
1910             /*
1911              * Some devices migrate VirtQueueElements that have been popped
1912              * from the avail ring but not yet returned to the used ring.
1913              * Since max ring size < UINT16_MAX it's safe to use modulo
1914              * UINT16_MAX + 1 subtraction.
1915              */
1916             vdev->vq[i].inuse = (uint16_t)(vdev->vq[i].last_avail_idx -
1917                                 vdev->vq[i].used_idx);
1918             if (vdev->vq[i].inuse > vdev->vq[i].vring.num) {
1919                 error_report("VQ %d size 0x%x < last_avail_idx 0x%x - "
1920                              "used_idx 0x%x",
1921                              i, vdev->vq[i].vring.num,
1922                              vdev->vq[i].last_avail_idx,
1923                              vdev->vq[i].used_idx);
1924                 return -1;
1925             }
1926         }
1927     }
1928 
1929     return 0;
1930 }
1931 
1932 void virtio_cleanup(VirtIODevice *vdev)
1933 {
1934     qemu_del_vm_change_state_handler(vdev->vmstate);
1935     g_free(vdev->config);
1936     g_free(vdev->vq);
1937     g_free(vdev->vector_queues);
1938 }
1939 
1940 static void virtio_vmstate_change(void *opaque, int running, RunState state)
1941 {
1942     VirtIODevice *vdev = opaque;
1943     BusState *qbus = qdev_get_parent_bus(DEVICE(vdev));
1944     VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
1945     bool backend_run = running && (vdev->status & VIRTIO_CONFIG_S_DRIVER_OK);
1946     vdev->vm_running = running;
1947 
1948     if (backend_run) {
1949         virtio_set_status(vdev, vdev->status);
1950     }
1951 
1952     if (k->vmstate_change) {
1953         k->vmstate_change(qbus->parent, backend_run);
1954     }
1955 
1956     if (!backend_run) {
1957         virtio_set_status(vdev, vdev->status);
1958     }
1959 }
1960 
1961 void virtio_instance_init_common(Object *proxy_obj, void *data,
1962                                  size_t vdev_size, const char *vdev_name)
1963 {
1964     DeviceState *vdev = data;
1965 
1966     object_initialize(vdev, vdev_size, vdev_name);
1967     object_property_add_child(proxy_obj, "virtio-backend", OBJECT(vdev), NULL);
1968     object_unref(OBJECT(vdev));
1969     qdev_alias_all_properties(vdev, proxy_obj);
1970 }
1971 
1972 void virtio_init(VirtIODevice *vdev, const char *name,
1973                  uint16_t device_id, size_t config_size)
1974 {
1975     BusState *qbus = qdev_get_parent_bus(DEVICE(vdev));
1976     VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
1977     int i;
1978     int nvectors = k->query_nvectors ? k->query_nvectors(qbus->parent) : 0;
1979 
1980     if (nvectors) {
1981         vdev->vector_queues =
1982             g_malloc0(sizeof(*vdev->vector_queues) * nvectors);
1983     }
1984 
1985     vdev->device_id = device_id;
1986     vdev->status = 0;
1987     atomic_set(&vdev->isr, 0);
1988     vdev->queue_sel = 0;
1989     vdev->config_vector = VIRTIO_NO_VECTOR;
1990     vdev->vq = g_malloc0(sizeof(VirtQueue) * VIRTIO_QUEUE_MAX);
1991     vdev->vm_running = runstate_is_running();
1992     vdev->broken = false;
1993     for (i = 0; i < VIRTIO_QUEUE_MAX; i++) {
1994         vdev->vq[i].vector = VIRTIO_NO_VECTOR;
1995         vdev->vq[i].vdev = vdev;
1996         vdev->vq[i].queue_index = i;
1997     }
1998 
1999     vdev->name = name;
2000     vdev->config_len = config_size;
2001     if (vdev->config_len) {
2002         vdev->config = g_malloc0(config_size);
2003     } else {
2004         vdev->config = NULL;
2005     }
2006     vdev->vmstate = qemu_add_vm_change_state_handler(virtio_vmstate_change,
2007                                                      vdev);
2008     vdev->device_endian = virtio_default_endian();
2009     vdev->use_guest_notifier_mask = true;
2010 }
2011 
2012 hwaddr virtio_queue_get_desc_addr(VirtIODevice *vdev, int n)
2013 {
2014     return vdev->vq[n].vring.desc;
2015 }
2016 
2017 hwaddr virtio_queue_get_avail_addr(VirtIODevice *vdev, int n)
2018 {
2019     return vdev->vq[n].vring.avail;
2020 }
2021 
2022 hwaddr virtio_queue_get_used_addr(VirtIODevice *vdev, int n)
2023 {
2024     return vdev->vq[n].vring.used;
2025 }
2026 
2027 hwaddr virtio_queue_get_desc_size(VirtIODevice *vdev, int n)
2028 {
2029     return sizeof(VRingDesc) * vdev->vq[n].vring.num;
2030 }
2031 
2032 hwaddr virtio_queue_get_avail_size(VirtIODevice *vdev, int n)
2033 {
2034     return offsetof(VRingAvail, ring) +
2035         sizeof(uint16_t) * vdev->vq[n].vring.num;
2036 }
2037 
2038 hwaddr virtio_queue_get_used_size(VirtIODevice *vdev, int n)
2039 {
2040     return offsetof(VRingUsed, ring) +
2041         sizeof(VRingUsedElem) * vdev->vq[n].vring.num;
2042 }
2043 
2044 uint16_t virtio_queue_get_last_avail_idx(VirtIODevice *vdev, int n)
2045 {
2046     return vdev->vq[n].last_avail_idx;
2047 }
2048 
2049 void virtio_queue_set_last_avail_idx(VirtIODevice *vdev, int n, uint16_t idx)
2050 {
2051     vdev->vq[n].last_avail_idx = idx;
2052     vdev->vq[n].shadow_avail_idx = idx;
2053 }
2054 
2055 void virtio_queue_update_used_idx(VirtIODevice *vdev, int n)
2056 {
2057     vdev->vq[n].used_idx = vring_used_idx(&vdev->vq[n]);
2058 }
2059 
2060 void virtio_queue_invalidate_signalled_used(VirtIODevice *vdev, int n)
2061 {
2062     vdev->vq[n].signalled_used_valid = false;
2063 }
2064 
2065 VirtQueue *virtio_get_queue(VirtIODevice *vdev, int n)
2066 {
2067     return vdev->vq + n;
2068 }
2069 
2070 uint16_t virtio_get_queue_index(VirtQueue *vq)
2071 {
2072     return vq->queue_index;
2073 }
2074 
2075 static void virtio_queue_guest_notifier_read(EventNotifier *n)
2076 {
2077     VirtQueue *vq = container_of(n, VirtQueue, guest_notifier);
2078     if (event_notifier_test_and_clear(n)) {
2079         virtio_notify_vector(vq->vdev, vq->vector);
2080     }
2081 }
2082 
2083 void virtio_queue_set_guest_notifier_fd_handler(VirtQueue *vq, bool assign,
2084                                                 bool with_irqfd)
2085 {
2086     if (assign && !with_irqfd) {
2087         event_notifier_set_handler(&vq->guest_notifier, false,
2088                                    virtio_queue_guest_notifier_read);
2089     } else {
2090         event_notifier_set_handler(&vq->guest_notifier, false, NULL);
2091     }
2092     if (!assign) {
2093         /* Test and clear notifier before closing it,
2094          * in case poll callback didn't have time to run. */
2095         virtio_queue_guest_notifier_read(&vq->guest_notifier);
2096     }
2097 }
2098 
2099 EventNotifier *virtio_queue_get_guest_notifier(VirtQueue *vq)
2100 {
2101     return &vq->guest_notifier;
2102 }
2103 
2104 static void virtio_queue_host_notifier_aio_read(EventNotifier *n)
2105 {
2106     VirtQueue *vq = container_of(n, VirtQueue, host_notifier);
2107     if (event_notifier_test_and_clear(n)) {
2108         virtio_queue_notify_aio_vq(vq);
2109     }
2110 }
2111 
2112 static void virtio_queue_host_notifier_aio_poll_begin(EventNotifier *n)
2113 {
2114     VirtQueue *vq = container_of(n, VirtQueue, host_notifier);
2115 
2116     virtio_queue_set_notification(vq, 0);
2117 }
2118 
2119 static bool virtio_queue_host_notifier_aio_poll(void *opaque)
2120 {
2121     EventNotifier *n = opaque;
2122     VirtQueue *vq = container_of(n, VirtQueue, host_notifier);
2123 
2124     if (virtio_queue_empty(vq)) {
2125         return false;
2126     }
2127 
2128     virtio_queue_notify_aio_vq(vq);
2129 
2130     /* In case the handler function re-enabled notifications */
2131     virtio_queue_set_notification(vq, 0);
2132     return true;
2133 }
2134 
2135 static void virtio_queue_host_notifier_aio_poll_end(EventNotifier *n)
2136 {
2137     VirtQueue *vq = container_of(n, VirtQueue, host_notifier);
2138 
2139     /* Caller polls once more after this to catch requests that race with us */
2140     virtio_queue_set_notification(vq, 1);
2141 }
2142 
2143 void virtio_queue_aio_set_host_notifier_handler(VirtQueue *vq, AioContext *ctx,
2144                                                 VirtIOHandleOutput handle_output)
2145 {
2146     if (handle_output) {
2147         vq->handle_aio_output = handle_output;
2148         aio_set_event_notifier(ctx, &vq->host_notifier, true,
2149                                virtio_queue_host_notifier_aio_read,
2150                                virtio_queue_host_notifier_aio_poll);
2151         aio_set_event_notifier_poll(ctx, &vq->host_notifier,
2152                                     virtio_queue_host_notifier_aio_poll_begin,
2153                                     virtio_queue_host_notifier_aio_poll_end);
2154     } else {
2155         aio_set_event_notifier(ctx, &vq->host_notifier, true, NULL, NULL);
2156         /* Test and clear notifier before after disabling event,
2157          * in case poll callback didn't have time to run. */
2158         virtio_queue_host_notifier_aio_read(&vq->host_notifier);
2159         vq->handle_aio_output = NULL;
2160     }
2161 }
2162 
2163 void virtio_queue_host_notifier_read(EventNotifier *n)
2164 {
2165     VirtQueue *vq = container_of(n, VirtQueue, host_notifier);
2166     if (event_notifier_test_and_clear(n)) {
2167         virtio_queue_notify_vq(vq);
2168     }
2169 }
2170 
2171 EventNotifier *virtio_queue_get_host_notifier(VirtQueue *vq)
2172 {
2173     return &vq->host_notifier;
2174 }
2175 
2176 void virtio_device_set_child_bus_name(VirtIODevice *vdev, char *bus_name)
2177 {
2178     g_free(vdev->bus_name);
2179     vdev->bus_name = g_strdup(bus_name);
2180 }
2181 
2182 void GCC_FMT_ATTR(2, 3) virtio_error(VirtIODevice *vdev, const char *fmt, ...)
2183 {
2184     va_list ap;
2185 
2186     va_start(ap, fmt);
2187     error_vreport(fmt, ap);
2188     va_end(ap);
2189 
2190     vdev->broken = true;
2191 
2192     if (virtio_vdev_has_feature(vdev, VIRTIO_F_VERSION_1)) {
2193         virtio_set_status(vdev, vdev->status | VIRTIO_CONFIG_S_NEEDS_RESET);
2194         virtio_notify_config(vdev);
2195     }
2196 }
2197 
2198 static void virtio_device_realize(DeviceState *dev, Error **errp)
2199 {
2200     VirtIODevice *vdev = VIRTIO_DEVICE(dev);
2201     VirtioDeviceClass *vdc = VIRTIO_DEVICE_GET_CLASS(dev);
2202     Error *err = NULL;
2203 
2204     /* Devices should either use vmsd or the load/save methods */
2205     assert(!vdc->vmsd || !vdc->load);
2206 
2207     if (vdc->realize != NULL) {
2208         vdc->realize(dev, &err);
2209         if (err != NULL) {
2210             error_propagate(errp, err);
2211             return;
2212         }
2213     }
2214 
2215     virtio_bus_device_plugged(vdev, &err);
2216     if (err != NULL) {
2217         error_propagate(errp, err);
2218         return;
2219     }
2220 }
2221 
2222 static void virtio_device_unrealize(DeviceState *dev, Error **errp)
2223 {
2224     VirtIODevice *vdev = VIRTIO_DEVICE(dev);
2225     VirtioDeviceClass *vdc = VIRTIO_DEVICE_GET_CLASS(dev);
2226     Error *err = NULL;
2227 
2228     virtio_bus_device_unplugged(vdev);
2229 
2230     if (vdc->unrealize != NULL) {
2231         vdc->unrealize(dev, &err);
2232         if (err != NULL) {
2233             error_propagate(errp, err);
2234             return;
2235         }
2236     }
2237 
2238     g_free(vdev->bus_name);
2239     vdev->bus_name = NULL;
2240 }
2241 
2242 static Property virtio_properties[] = {
2243     DEFINE_VIRTIO_COMMON_FEATURES(VirtIODevice, host_features),
2244     DEFINE_PROP_END_OF_LIST(),
2245 };
2246 
2247 static int virtio_device_start_ioeventfd_impl(VirtIODevice *vdev)
2248 {
2249     VirtioBusState *qbus = VIRTIO_BUS(qdev_get_parent_bus(DEVICE(vdev)));
2250     int n, r, err;
2251 
2252     for (n = 0; n < VIRTIO_QUEUE_MAX; n++) {
2253         VirtQueue *vq = &vdev->vq[n];
2254         if (!virtio_queue_get_num(vdev, n)) {
2255             continue;
2256         }
2257         r = virtio_bus_set_host_notifier(qbus, n, true);
2258         if (r < 0) {
2259             err = r;
2260             goto assign_error;
2261         }
2262         event_notifier_set_handler(&vq->host_notifier, true,
2263                                    virtio_queue_host_notifier_read);
2264     }
2265 
2266     for (n = 0; n < VIRTIO_QUEUE_MAX; n++) {
2267         /* Kick right away to begin processing requests already in vring */
2268         VirtQueue *vq = &vdev->vq[n];
2269         if (!vq->vring.num) {
2270             continue;
2271         }
2272         event_notifier_set(&vq->host_notifier);
2273     }
2274     return 0;
2275 
2276 assign_error:
2277     while (--n >= 0) {
2278         VirtQueue *vq = &vdev->vq[n];
2279         if (!virtio_queue_get_num(vdev, n)) {
2280             continue;
2281         }
2282 
2283         event_notifier_set_handler(&vq->host_notifier, true, NULL);
2284         r = virtio_bus_set_host_notifier(qbus, n, false);
2285         assert(r >= 0);
2286     }
2287     return err;
2288 }
2289 
2290 int virtio_device_start_ioeventfd(VirtIODevice *vdev)
2291 {
2292     BusState *qbus = qdev_get_parent_bus(DEVICE(vdev));
2293     VirtioBusState *vbus = VIRTIO_BUS(qbus);
2294 
2295     return virtio_bus_start_ioeventfd(vbus);
2296 }
2297 
2298 static void virtio_device_stop_ioeventfd_impl(VirtIODevice *vdev)
2299 {
2300     VirtioBusState *qbus = VIRTIO_BUS(qdev_get_parent_bus(DEVICE(vdev)));
2301     int n, r;
2302 
2303     for (n = 0; n < VIRTIO_QUEUE_MAX; n++) {
2304         VirtQueue *vq = &vdev->vq[n];
2305 
2306         if (!virtio_queue_get_num(vdev, n)) {
2307             continue;
2308         }
2309         event_notifier_set_handler(&vq->host_notifier, true, NULL);
2310         r = virtio_bus_set_host_notifier(qbus, n, false);
2311         assert(r >= 0);
2312     }
2313 }
2314 
2315 void virtio_device_stop_ioeventfd(VirtIODevice *vdev)
2316 {
2317     BusState *qbus = qdev_get_parent_bus(DEVICE(vdev));
2318     VirtioBusState *vbus = VIRTIO_BUS(qbus);
2319 
2320     virtio_bus_stop_ioeventfd(vbus);
2321 }
2322 
2323 int virtio_device_grab_ioeventfd(VirtIODevice *vdev)
2324 {
2325     BusState *qbus = qdev_get_parent_bus(DEVICE(vdev));
2326     VirtioBusState *vbus = VIRTIO_BUS(qbus);
2327 
2328     return virtio_bus_grab_ioeventfd(vbus);
2329 }
2330 
2331 void virtio_device_release_ioeventfd(VirtIODevice *vdev)
2332 {
2333     BusState *qbus = qdev_get_parent_bus(DEVICE(vdev));
2334     VirtioBusState *vbus = VIRTIO_BUS(qbus);
2335 
2336     virtio_bus_release_ioeventfd(vbus);
2337 }
2338 
2339 static void virtio_device_class_init(ObjectClass *klass, void *data)
2340 {
2341     /* Set the default value here. */
2342     VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass);
2343     DeviceClass *dc = DEVICE_CLASS(klass);
2344 
2345     dc->realize = virtio_device_realize;
2346     dc->unrealize = virtio_device_unrealize;
2347     dc->bus_type = TYPE_VIRTIO_BUS;
2348     dc->props = virtio_properties;
2349     vdc->start_ioeventfd = virtio_device_start_ioeventfd_impl;
2350     vdc->stop_ioeventfd = virtio_device_stop_ioeventfd_impl;
2351 
2352     vdc->legacy_features |= VIRTIO_LEGACY_FEATURES;
2353 }
2354 
2355 bool virtio_device_ioeventfd_enabled(VirtIODevice *vdev)
2356 {
2357     BusState *qbus = qdev_get_parent_bus(DEVICE(vdev));
2358     VirtioBusState *vbus = VIRTIO_BUS(qbus);
2359 
2360     return virtio_bus_ioeventfd_enabled(vbus);
2361 }
2362 
2363 static const TypeInfo virtio_device_info = {
2364     .name = TYPE_VIRTIO_DEVICE,
2365     .parent = TYPE_DEVICE,
2366     .instance_size = sizeof(VirtIODevice),
2367     .class_init = virtio_device_class_init,
2368     .abstract = true,
2369     .class_size = sizeof(VirtioDeviceClass),
2370 };
2371 
2372 static void virtio_register_types(void)
2373 {
2374     type_register_static(&virtio_device_info);
2375 }
2376 
2377 type_init(virtio_register_types)
2378