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