4 * Copyright IBM, Corp. 2007
7 * Anthony Liguori <aliguori@us.ibm.com>
9 * This work is licensed under the terms of the GNU GPL, version 2. See
10 * the COPYING file in the top-level directory.
14 #include "qemu/osdep.h"
15 #include "qapi/error.h"
16 #include "qemu-common.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"
28 * The alignment to use between consumer and producer parts of vring.
29 * x86 pagesize again. This is the default, used by transports like PCI
30 * which don't provide a means for the guest to tell the host the alignment.
32 #define VIRTIO_PCI_VRING_ALIGN 4096
34 typedef struct VRingDesc
42 typedef struct VRingAvail
49 typedef struct VRingUsedElem
55 typedef struct VRingUsed
59 VRingUsedElem ring
[0];
65 unsigned int num_default
;
76 /* Next head to pop */
77 uint16_t last_avail_idx
;
79 /* Last avail_idx read from VQ. */
80 uint16_t shadow_avail_idx
;
84 /* Last used index value we have signalled on */
85 uint16_t signalled_used
;
87 /* Last used index value we have signalled on */
88 bool signalled_used_valid
;
90 /* Notification enabled? */
98 VirtIOHandleOutput handle_output
;
99 VirtIOHandleOutput handle_aio_output
;
102 EventNotifier guest_notifier
;
103 EventNotifier host_notifier
;
104 QLIST_ENTRY(VirtQueue
) node
;
107 /* virt queue functions */
108 void virtio_queue_update_rings(VirtIODevice
*vdev
, int n
)
110 VRing
*vring
= &vdev
->vq
[n
].vring
;
113 /* not yet setup -> nothing to do */
116 vring
->avail
= vring
->desc
+ vring
->num
* sizeof(VRingDesc
);
117 vring
->used
= vring_align(vring
->avail
+
118 offsetof(VRingAvail
, ring
[vring
->num
]),
122 static void vring_desc_read(VirtIODevice
*vdev
, VRingDesc
*desc
,
123 hwaddr desc_pa
, int i
)
125 address_space_read(&address_space_memory
, 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
);
133 static inline uint16_t vring_avail_flags(VirtQueue
*vq
)
136 pa
= vq
->vring
.avail
+ offsetof(VRingAvail
, flags
);
137 return virtio_lduw_phys(vq
->vdev
, pa
);
140 static inline uint16_t vring_avail_idx(VirtQueue
*vq
)
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
;
148 static inline uint16_t vring_avail_ring(VirtQueue
*vq
, int i
)
151 pa
= vq
->vring
.avail
+ offsetof(VRingAvail
, ring
[i
]);
152 return virtio_lduw_phys(vq
->vdev
, pa
);
155 static inline uint16_t vring_get_used_event(VirtQueue
*vq
)
157 return vring_avail_ring(vq
, vq
->vring
.num
);
160 static inline void vring_used_write(VirtQueue
*vq
, VRingUsedElem
*uelem
,
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(&address_space_memory
, pa
, MEMTXATTRS_UNSPECIFIED
,
168 (void *)uelem
, sizeof(VRingUsedElem
));
171 static uint16_t vring_used_idx(VirtQueue
*vq
)
174 pa
= vq
->vring
.used
+ offsetof(VRingUsed
, idx
);
175 return virtio_lduw_phys(vq
->vdev
, pa
);
178 static inline void vring_used_idx_set(VirtQueue
*vq
, uint16_t val
)
181 pa
= vq
->vring
.used
+ offsetof(VRingUsed
, idx
);
182 virtio_stw_phys(vq
->vdev
, pa
, val
);
186 static inline void vring_used_flags_set_bit(VirtQueue
*vq
, int mask
)
188 VirtIODevice
*vdev
= vq
->vdev
;
190 pa
= vq
->vring
.used
+ offsetof(VRingUsed
, flags
);
191 virtio_stw_phys(vdev
, pa
, virtio_lduw_phys(vdev
, pa
) | mask
);
194 static inline void vring_used_flags_unset_bit(VirtQueue
*vq
, int mask
)
196 VirtIODevice
*vdev
= vq
->vdev
;
198 pa
= vq
->vring
.used
+ offsetof(VRingUsed
, flags
);
199 virtio_stw_phys(vdev
, pa
, virtio_lduw_phys(vdev
, pa
) & ~mask
);
202 static inline void vring_set_avail_event(VirtQueue
*vq
, uint16_t val
)
205 if (!vq
->notification
) {
208 pa
= vq
->vring
.used
+ offsetof(VRingUsed
, ring
[vq
->vring
.num
]);
209 virtio_stw_phys(vq
->vdev
, pa
, val
);
212 void virtio_queue_set_notification(VirtQueue
*vq
, int enable
)
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
));
218 vring_used_flags_unset_bit(vq
, VRING_USED_F_NO_NOTIFY
);
220 vring_used_flags_set_bit(vq
, VRING_USED_F_NO_NOTIFY
);
223 /* Expose avail event/used flags before caller checks the avail idx. */
228 int virtio_queue_ready(VirtQueue
*vq
)
230 return vq
->vring
.avail
!= 0;
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
)
237 if (vq
->shadow_avail_idx
!= vq
->last_avail_idx
) {
241 return vring_avail_idx(vq
) == vq
->last_avail_idx
;
244 static void virtqueue_unmap_sg(VirtQueue
*vq
, const VirtQueueElement
*elem
,
251 for (i
= 0; i
< elem
->in_num
; i
++) {
252 size_t size
= MIN(len
- offset
, elem
->in_sg
[i
].iov_len
);
254 cpu_physical_memory_unmap(elem
->in_sg
[i
].iov_base
,
255 elem
->in_sg
[i
].iov_len
,
261 for (i
= 0; i
< elem
->out_num
; i
++)
262 cpu_physical_memory_unmap(elem
->out_sg
[i
].iov_base
,
263 elem
->out_sg
[i
].iov_len
,
264 0, elem
->out_sg
[i
].iov_len
);
267 void virtqueue_discard(VirtQueue
*vq
, const VirtQueueElement
*elem
,
270 vq
->last_avail_idx
--;
272 virtqueue_unmap_sg(vq
, elem
, len
);
276 * @vq: The #VirtQueue
277 * @num: Number of elements to push back
279 * Pretend that elements weren't popped from the virtqueue. The next
280 * virtqueue_pop() will refetch the oldest element.
282 * Use virtqueue_discard() instead if you have a VirtQueueElement.
284 * Returns: true on success, false if @num is greater than the number of in use
287 bool virtqueue_rewind(VirtQueue
*vq
, unsigned int num
)
289 if (num
> vq
->inuse
) {
292 vq
->last_avail_idx
-= num
;
297 void virtqueue_fill(VirtQueue
*vq
, const VirtQueueElement
*elem
,
298 unsigned int len
, unsigned int idx
)
302 trace_virtqueue_fill(vq
, elem
, len
, idx
);
304 virtqueue_unmap_sg(vq
, elem
, len
);
306 if (unlikely(vq
->vdev
->broken
)) {
310 idx
= (idx
+ vq
->used_idx
) % vq
->vring
.num
;
312 uelem
.id
= elem
->index
;
314 vring_used_write(vq
, &uelem
, idx
);
317 void virtqueue_flush(VirtQueue
*vq
, unsigned int count
)
321 if (unlikely(vq
->vdev
->broken
)) {
326 /* Make sure buffer is written before we update index. */
328 trace_virtqueue_flush(vq
, count
);
331 vring_used_idx_set(vq
, new);
333 if (unlikely((int16_t)(new - vq
->signalled_used
) < (uint16_t)(new - old
)))
334 vq
->signalled_used_valid
= false;
337 void virtqueue_push(VirtQueue
*vq
, const VirtQueueElement
*elem
,
340 virtqueue_fill(vq
, elem
, len
, 0);
341 virtqueue_flush(vq
, 1);
344 static int virtqueue_num_heads(VirtQueue
*vq
, unsigned int idx
)
346 uint16_t num_heads
= vring_avail_idx(vq
) - idx
;
348 /* Check it isn't doing very strange things with descriptor numbers. */
349 if (num_heads
> vq
->vring
.num
) {
350 error_report("Guest moved used index from %u to %u",
351 idx
, vq
->shadow_avail_idx
);
354 /* On success, callers read a descriptor at vq->last_avail_idx.
355 * Make sure descriptor read does not bypass avail index read. */
363 static unsigned int virtqueue_get_head(VirtQueue
*vq
, unsigned int idx
)
367 /* Grab the next descriptor number they're advertising, and increment
368 * the index we've seen. */
369 head
= vring_avail_ring(vq
, idx
% vq
->vring
.num
);
371 /* If their number is silly, that's a fatal mistake. */
372 if (head
>= vq
->vring
.num
) {
373 error_report("Guest says index %u is available", head
);
381 VIRTQUEUE_READ_DESC_ERROR
= -1,
382 VIRTQUEUE_READ_DESC_DONE
= 0, /* end of chain */
383 VIRTQUEUE_READ_DESC_MORE
= 1, /* more buffers in chain */
386 static int virtqueue_read_next_desc(VirtIODevice
*vdev
, VRingDesc
*desc
,
387 hwaddr desc_pa
, unsigned int max
,
390 /* If this descriptor says it doesn't chain, we're done. */
391 if (!(desc
->flags
& VRING_DESC_F_NEXT
)) {
392 return VIRTQUEUE_READ_DESC_DONE
;
395 /* Check they're not leading us off end of descriptors. */
397 /* Make sure compiler knows to grab that: we don't want it changing! */
401 virtio_error(vdev
, "Desc next is %u", *next
);
402 return VIRTQUEUE_READ_DESC_ERROR
;
405 vring_desc_read(vdev
, desc
, desc_pa
, *next
);
406 return VIRTQUEUE_READ_DESC_MORE
;
409 void virtqueue_get_avail_bytes(VirtQueue
*vq
, unsigned int *in_bytes
,
410 unsigned int *out_bytes
,
411 unsigned max_in_bytes
, unsigned max_out_bytes
)
414 unsigned int total_bufs
, in_total
, out_total
;
417 idx
= vq
->last_avail_idx
;
419 total_bufs
= in_total
= out_total
= 0;
420 while (virtqueue_num_heads(vq
, idx
)) {
421 VirtIODevice
*vdev
= vq
->vdev
;
422 unsigned int max
, num_bufs
, indirect
= 0;
428 num_bufs
= total_bufs
;
429 i
= virtqueue_get_head(vq
, idx
++);
430 desc_pa
= vq
->vring
.desc
;
431 vring_desc_read(vdev
, &desc
, desc_pa
, i
);
433 if (desc
.flags
& VRING_DESC_F_INDIRECT
) {
434 if (desc
.len
% sizeof(VRingDesc
)) {
435 virtio_error(vdev
, "Invalid size for indirect buffer table");
439 /* If we've got too many, that implies a descriptor loop. */
440 if (num_bufs
>= max
) {
441 virtio_error(vdev
, "Looped descriptor");
445 /* loop over the indirect descriptor table */
447 max
= desc
.len
/ sizeof(VRingDesc
);
450 vring_desc_read(vdev
, &desc
, desc_pa
, i
);
454 /* If we've got too many, that implies a descriptor loop. */
455 if (++num_bufs
> max
) {
456 virtio_error(vdev
, "Looped descriptor");
460 if (desc
.flags
& VRING_DESC_F_WRITE
) {
461 in_total
+= desc
.len
;
463 out_total
+= desc
.len
;
465 if (in_total
>= max_in_bytes
&& out_total
>= max_out_bytes
) {
469 rc
= virtqueue_read_next_desc(vdev
, &desc
, desc_pa
, max
, &i
);
470 } while (rc
== VIRTQUEUE_READ_DESC_MORE
);
472 if (rc
== VIRTQUEUE_READ_DESC_ERROR
) {
477 total_bufs
= num_bufs
;
483 *in_bytes
= in_total
;
486 *out_bytes
= out_total
;
491 in_total
= out_total
= 0;
495 int virtqueue_avail_bytes(VirtQueue
*vq
, unsigned int in_bytes
,
496 unsigned int out_bytes
)
498 unsigned int in_total
, out_total
;
500 virtqueue_get_avail_bytes(vq
, &in_total
, &out_total
, in_bytes
, out_bytes
);
501 return in_bytes
<= in_total
&& out_bytes
<= out_total
;
504 static bool virtqueue_map_desc(VirtIODevice
*vdev
, unsigned int *p_num_sg
,
505 hwaddr
*addr
, struct iovec
*iov
,
506 unsigned int max_num_sg
, bool is_write
,
507 hwaddr pa
, size_t sz
)
510 unsigned num_sg
= *p_num_sg
;
511 assert(num_sg
<= max_num_sg
);
514 virtio_error(vdev
, "virtio: zero sized buffers are not allowed");
521 if (num_sg
== max_num_sg
) {
522 virtio_error(vdev
, "virtio: too many write descriptors in "
527 iov
[num_sg
].iov_base
= cpu_physical_memory_map(pa
, &len
, is_write
);
528 if (!iov
[num_sg
].iov_base
) {
529 virtio_error(vdev
, "virtio: bogus descriptor or out of resources");
533 iov
[num_sg
].iov_len
= len
;
547 /* Only used by error code paths before we have a VirtQueueElement (therefore
548 * virtqueue_unmap_sg() can't be used). Assumes buffers weren't written to
551 static void virtqueue_undo_map_desc(unsigned int out_num
, unsigned int in_num
,
556 for (i
= 0; i
< out_num
+ in_num
; i
++) {
557 int is_write
= i
>= out_num
;
559 cpu_physical_memory_unmap(iov
->iov_base
, iov
->iov_len
, is_write
, 0);
564 static void virtqueue_map_iovec(struct iovec
*sg
, hwaddr
*addr
,
565 unsigned int *num_sg
, unsigned int max_size
,
571 /* Note: this function MUST validate input, some callers
572 * are passing in num_sg values received over the network.
574 /* TODO: teach all callers that this can fail, and return failure instead
576 * When we do, we might be able to re-enable NDEBUG below.
579 #error building with NDEBUG is not supported
581 assert(*num_sg
<= max_size
);
583 for (i
= 0; i
< *num_sg
; i
++) {
585 sg
[i
].iov_base
= cpu_physical_memory_map(addr
[i
], &len
, is_write
);
586 if (!sg
[i
].iov_base
) {
587 error_report("virtio: error trying to map MMIO memory");
590 if (len
!= sg
[i
].iov_len
) {
591 error_report("virtio: unexpected memory split");
597 void virtqueue_map(VirtQueueElement
*elem
)
599 virtqueue_map_iovec(elem
->in_sg
, elem
->in_addr
, &elem
->in_num
,
600 VIRTQUEUE_MAX_SIZE
, 1);
601 virtqueue_map_iovec(elem
->out_sg
, elem
->out_addr
, &elem
->out_num
,
602 VIRTQUEUE_MAX_SIZE
, 0);
605 void *virtqueue_alloc_element(size_t sz
, unsigned out_num
, unsigned in_num
)
607 VirtQueueElement
*elem
;
608 size_t in_addr_ofs
= QEMU_ALIGN_UP(sz
, __alignof__(elem
->in_addr
[0]));
609 size_t out_addr_ofs
= in_addr_ofs
+ in_num
* sizeof(elem
->in_addr
[0]);
610 size_t out_addr_end
= out_addr_ofs
+ out_num
* sizeof(elem
->out_addr
[0]);
611 size_t in_sg_ofs
= QEMU_ALIGN_UP(out_addr_end
, __alignof__(elem
->in_sg
[0]));
612 size_t out_sg_ofs
= in_sg_ofs
+ in_num
* sizeof(elem
->in_sg
[0]);
613 size_t out_sg_end
= out_sg_ofs
+ out_num
* sizeof(elem
->out_sg
[0]);
615 assert(sz
>= sizeof(VirtQueueElement
));
616 elem
= g_malloc(out_sg_end
);
617 elem
->out_num
= out_num
;
618 elem
->in_num
= in_num
;
619 elem
->in_addr
= (void *)elem
+ in_addr_ofs
;
620 elem
->out_addr
= (void *)elem
+ out_addr_ofs
;
621 elem
->in_sg
= (void *)elem
+ in_sg_ofs
;
622 elem
->out_sg
= (void *)elem
+ out_sg_ofs
;
626 void *virtqueue_pop(VirtQueue
*vq
, size_t sz
)
628 unsigned int i
, head
, max
;
629 hwaddr desc_pa
= vq
->vring
.desc
;
630 VirtIODevice
*vdev
= vq
->vdev
;
631 VirtQueueElement
*elem
;
632 unsigned out_num
, in_num
;
633 hwaddr addr
[VIRTQUEUE_MAX_SIZE
];
634 struct iovec iov
[VIRTQUEUE_MAX_SIZE
];
638 if (unlikely(vdev
->broken
)) {
641 if (virtio_queue_empty(vq
)) {
644 /* Needed after virtio_queue_empty(), see comment in
645 * virtqueue_num_heads(). */
648 /* When we start there are none of either input nor output. */
649 out_num
= in_num
= 0;
653 if (vq
->inuse
>= vq
->vring
.num
) {
654 virtio_error(vdev
, "Virtqueue size exceeded");
658 i
= head
= virtqueue_get_head(vq
, vq
->last_avail_idx
++);
659 if (virtio_vdev_has_feature(vdev
, VIRTIO_RING_F_EVENT_IDX
)) {
660 vring_set_avail_event(vq
, vq
->last_avail_idx
);
663 vring_desc_read(vdev
, &desc
, desc_pa
, i
);
664 if (desc
.flags
& VRING_DESC_F_INDIRECT
) {
665 if (desc
.len
% sizeof(VRingDesc
)) {
666 virtio_error(vdev
, "Invalid size for indirect buffer table");
670 /* loop over the indirect descriptor table */
671 max
= desc
.len
/ sizeof(VRingDesc
);
674 vring_desc_read(vdev
, &desc
, desc_pa
, i
);
677 /* Collect all the descriptors */
681 if (desc
.flags
& VRING_DESC_F_WRITE
) {
682 map_ok
= virtqueue_map_desc(vdev
, &in_num
, addr
+ out_num
,
684 VIRTQUEUE_MAX_SIZE
- out_num
, true,
685 desc
.addr
, desc
.len
);
688 virtio_error(vdev
, "Incorrect order for descriptors");
691 map_ok
= virtqueue_map_desc(vdev
, &out_num
, addr
, iov
,
692 VIRTQUEUE_MAX_SIZE
, false,
693 desc
.addr
, desc
.len
);
699 /* If we've got too many, that implies a descriptor loop. */
700 if ((in_num
+ out_num
) > max
) {
701 virtio_error(vdev
, "Looped descriptor");
705 rc
= virtqueue_read_next_desc(vdev
, &desc
, desc_pa
, max
, &i
);
706 } while (rc
== VIRTQUEUE_READ_DESC_MORE
);
708 if (rc
== VIRTQUEUE_READ_DESC_ERROR
) {
712 /* Now copy what we have collected and mapped */
713 elem
= virtqueue_alloc_element(sz
, out_num
, in_num
);
715 for (i
= 0; i
< out_num
; i
++) {
716 elem
->out_addr
[i
] = addr
[i
];
717 elem
->out_sg
[i
] = iov
[i
];
719 for (i
= 0; i
< in_num
; i
++) {
720 elem
->in_addr
[i
] = addr
[out_num
+ i
];
721 elem
->in_sg
[i
] = iov
[out_num
+ i
];
726 trace_virtqueue_pop(vq
, elem
, elem
->in_num
, elem
->out_num
);
730 virtqueue_undo_map_desc(out_num
, in_num
, iov
);
734 /* Reading and writing a structure directly to QEMUFile is *awful*, but
735 * it is what QEMU has always done by mistake. We can change it sooner
736 * or later by bumping the version number of the affected vm states.
737 * In the meanwhile, since the in-memory layout of VirtQueueElement
738 * has changed, we need to marshal to and from the layout that was
739 * used before the change.
741 typedef struct VirtQueueElementOld
{
743 unsigned int out_num
;
745 hwaddr in_addr
[VIRTQUEUE_MAX_SIZE
];
746 hwaddr out_addr
[VIRTQUEUE_MAX_SIZE
];
747 struct iovec in_sg
[VIRTQUEUE_MAX_SIZE
];
748 struct iovec out_sg
[VIRTQUEUE_MAX_SIZE
];
749 } VirtQueueElementOld
;
751 void *qemu_get_virtqueue_element(QEMUFile
*f
, size_t sz
)
753 VirtQueueElement
*elem
;
754 VirtQueueElementOld data
;
757 qemu_get_buffer(f
, (uint8_t *)&data
, sizeof(VirtQueueElementOld
));
759 elem
= virtqueue_alloc_element(sz
, data
.out_num
, data
.in_num
);
760 elem
->index
= data
.index
;
762 for (i
= 0; i
< elem
->in_num
; i
++) {
763 elem
->in_addr
[i
] = data
.in_addr
[i
];
766 for (i
= 0; i
< elem
->out_num
; i
++) {
767 elem
->out_addr
[i
] = data
.out_addr
[i
];
770 for (i
= 0; i
< elem
->in_num
; i
++) {
771 /* Base is overwritten by virtqueue_map. */
772 elem
->in_sg
[i
].iov_base
= 0;
773 elem
->in_sg
[i
].iov_len
= data
.in_sg
[i
].iov_len
;
776 for (i
= 0; i
< elem
->out_num
; i
++) {
777 /* Base is overwritten by virtqueue_map. */
778 elem
->out_sg
[i
].iov_base
= 0;
779 elem
->out_sg
[i
].iov_len
= data
.out_sg
[i
].iov_len
;
786 void qemu_put_virtqueue_element(QEMUFile
*f
, VirtQueueElement
*elem
)
788 VirtQueueElementOld data
;
791 memset(&data
, 0, sizeof(data
));
792 data
.index
= elem
->index
;
793 data
.in_num
= elem
->in_num
;
794 data
.out_num
= elem
->out_num
;
796 for (i
= 0; i
< elem
->in_num
; i
++) {
797 data
.in_addr
[i
] = elem
->in_addr
[i
];
800 for (i
= 0; i
< elem
->out_num
; i
++) {
801 data
.out_addr
[i
] = elem
->out_addr
[i
];
804 for (i
= 0; i
< elem
->in_num
; i
++) {
805 /* Base is overwritten by virtqueue_map when loading. Do not
806 * save it, as it would leak the QEMU address space layout. */
807 data
.in_sg
[i
].iov_len
= elem
->in_sg
[i
].iov_len
;
810 for (i
= 0; i
< elem
->out_num
; i
++) {
811 /* Do not save iov_base as above. */
812 data
.out_sg
[i
].iov_len
= elem
->out_sg
[i
].iov_len
;
814 qemu_put_buffer(f
, (uint8_t *)&data
, sizeof(VirtQueueElementOld
));
818 static void virtio_notify_vector(VirtIODevice
*vdev
, uint16_t vector
)
820 BusState
*qbus
= qdev_get_parent_bus(DEVICE(vdev
));
821 VirtioBusClass
*k
= VIRTIO_BUS_GET_CLASS(qbus
);
823 if (unlikely(vdev
->broken
)) {
828 k
->notify(qbus
->parent
, vector
);
832 void virtio_update_irq(VirtIODevice
*vdev
)
834 virtio_notify_vector(vdev
, VIRTIO_NO_VECTOR
);
837 static int virtio_validate_features(VirtIODevice
*vdev
)
839 VirtioDeviceClass
*k
= VIRTIO_DEVICE_GET_CLASS(vdev
);
841 if (k
->validate_features
) {
842 return k
->validate_features(vdev
);
848 int virtio_set_status(VirtIODevice
*vdev
, uint8_t val
)
850 VirtioDeviceClass
*k
= VIRTIO_DEVICE_GET_CLASS(vdev
);
851 trace_virtio_set_status(vdev
, val
);
853 if (virtio_vdev_has_feature(vdev
, VIRTIO_F_VERSION_1
)) {
854 if (!(vdev
->status
& VIRTIO_CONFIG_S_FEATURES_OK
) &&
855 val
& VIRTIO_CONFIG_S_FEATURES_OK
) {
856 int ret
= virtio_validate_features(vdev
);
864 k
->set_status(vdev
, val
);
870 bool target_words_bigendian(void);
871 static enum virtio_device_endian
virtio_default_endian(void)
873 if (target_words_bigendian()) {
874 return VIRTIO_DEVICE_ENDIAN_BIG
;
876 return VIRTIO_DEVICE_ENDIAN_LITTLE
;
880 static enum virtio_device_endian
virtio_current_cpu_endian(void)
882 CPUClass
*cc
= CPU_GET_CLASS(current_cpu
);
884 if (cc
->virtio_is_big_endian(current_cpu
)) {
885 return VIRTIO_DEVICE_ENDIAN_BIG
;
887 return VIRTIO_DEVICE_ENDIAN_LITTLE
;
891 void virtio_reset(void *opaque
)
893 VirtIODevice
*vdev
= opaque
;
894 VirtioDeviceClass
*k
= VIRTIO_DEVICE_GET_CLASS(vdev
);
897 virtio_set_status(vdev
, 0);
899 /* Guest initiated reset */
900 vdev
->device_endian
= virtio_current_cpu_endian();
903 vdev
->device_endian
= virtio_default_endian();
910 vdev
->broken
= false;
911 vdev
->guest_features
= 0;
915 vdev
->config_vector
= VIRTIO_NO_VECTOR
;
916 virtio_notify_vector(vdev
, vdev
->config_vector
);
918 for(i
= 0; i
< VIRTIO_QUEUE_MAX
; i
++) {
919 vdev
->vq
[i
].vring
.desc
= 0;
920 vdev
->vq
[i
].vring
.avail
= 0;
921 vdev
->vq
[i
].vring
.used
= 0;
922 vdev
->vq
[i
].last_avail_idx
= 0;
923 vdev
->vq
[i
].shadow_avail_idx
= 0;
924 vdev
->vq
[i
].used_idx
= 0;
925 virtio_queue_set_vector(vdev
, i
, VIRTIO_NO_VECTOR
);
926 vdev
->vq
[i
].signalled_used
= 0;
927 vdev
->vq
[i
].signalled_used_valid
= false;
928 vdev
->vq
[i
].notification
= true;
929 vdev
->vq
[i
].vring
.num
= vdev
->vq
[i
].vring
.num_default
;
930 vdev
->vq
[i
].inuse
= 0;
934 uint32_t virtio_config_readb(VirtIODevice
*vdev
, uint32_t addr
)
936 VirtioDeviceClass
*k
= VIRTIO_DEVICE_GET_CLASS(vdev
);
939 if (addr
+ sizeof(val
) > vdev
->config_len
) {
943 k
->get_config(vdev
, vdev
->config
);
945 val
= ldub_p(vdev
->config
+ addr
);
949 uint32_t virtio_config_readw(VirtIODevice
*vdev
, uint32_t addr
)
951 VirtioDeviceClass
*k
= VIRTIO_DEVICE_GET_CLASS(vdev
);
954 if (addr
+ sizeof(val
) > vdev
->config_len
) {
958 k
->get_config(vdev
, vdev
->config
);
960 val
= lduw_p(vdev
->config
+ addr
);
964 uint32_t virtio_config_readl(VirtIODevice
*vdev
, uint32_t addr
)
966 VirtioDeviceClass
*k
= VIRTIO_DEVICE_GET_CLASS(vdev
);
969 if (addr
+ sizeof(val
) > vdev
->config_len
) {
973 k
->get_config(vdev
, vdev
->config
);
975 val
= ldl_p(vdev
->config
+ addr
);
979 void virtio_config_writeb(VirtIODevice
*vdev
, uint32_t addr
, uint32_t data
)
981 VirtioDeviceClass
*k
= VIRTIO_DEVICE_GET_CLASS(vdev
);
984 if (addr
+ sizeof(val
) > vdev
->config_len
) {
988 stb_p(vdev
->config
+ addr
, val
);
991 k
->set_config(vdev
, vdev
->config
);
995 void virtio_config_writew(VirtIODevice
*vdev
, uint32_t addr
, uint32_t data
)
997 VirtioDeviceClass
*k
= VIRTIO_DEVICE_GET_CLASS(vdev
);
1000 if (addr
+ sizeof(val
) > vdev
->config_len
) {
1004 stw_p(vdev
->config
+ addr
, val
);
1006 if (k
->set_config
) {
1007 k
->set_config(vdev
, vdev
->config
);
1011 void virtio_config_writel(VirtIODevice
*vdev
, uint32_t addr
, uint32_t data
)
1013 VirtioDeviceClass
*k
= VIRTIO_DEVICE_GET_CLASS(vdev
);
1014 uint32_t val
= data
;
1016 if (addr
+ sizeof(val
) > vdev
->config_len
) {
1020 stl_p(vdev
->config
+ addr
, val
);
1022 if (k
->set_config
) {
1023 k
->set_config(vdev
, vdev
->config
);
1027 uint32_t virtio_config_modern_readb(VirtIODevice
*vdev
, uint32_t addr
)
1029 VirtioDeviceClass
*k
= VIRTIO_DEVICE_GET_CLASS(vdev
);
1032 if (addr
+ sizeof(val
) > vdev
->config_len
) {
1033 return (uint32_t)-1;
1036 k
->get_config(vdev
, vdev
->config
);
1038 val
= ldub_p(vdev
->config
+ addr
);
1042 uint32_t virtio_config_modern_readw(VirtIODevice
*vdev
, uint32_t addr
)
1044 VirtioDeviceClass
*k
= VIRTIO_DEVICE_GET_CLASS(vdev
);
1047 if (addr
+ sizeof(val
) > vdev
->config_len
) {
1048 return (uint32_t)-1;
1051 k
->get_config(vdev
, vdev
->config
);
1053 val
= lduw_le_p(vdev
->config
+ addr
);
1057 uint32_t virtio_config_modern_readl(VirtIODevice
*vdev
, uint32_t addr
)
1059 VirtioDeviceClass
*k
= VIRTIO_DEVICE_GET_CLASS(vdev
);
1062 if (addr
+ sizeof(val
) > vdev
->config_len
) {
1063 return (uint32_t)-1;
1066 k
->get_config(vdev
, vdev
->config
);
1068 val
= ldl_le_p(vdev
->config
+ addr
);
1072 void virtio_config_modern_writeb(VirtIODevice
*vdev
,
1073 uint32_t addr
, uint32_t data
)
1075 VirtioDeviceClass
*k
= VIRTIO_DEVICE_GET_CLASS(vdev
);
1078 if (addr
+ sizeof(val
) > vdev
->config_len
) {
1082 stb_p(vdev
->config
+ addr
, val
);
1084 if (k
->set_config
) {
1085 k
->set_config(vdev
, vdev
->config
);
1089 void virtio_config_modern_writew(VirtIODevice
*vdev
,
1090 uint32_t addr
, uint32_t data
)
1092 VirtioDeviceClass
*k
= VIRTIO_DEVICE_GET_CLASS(vdev
);
1093 uint16_t val
= data
;
1095 if (addr
+ sizeof(val
) > vdev
->config_len
) {
1099 stw_le_p(vdev
->config
+ addr
, val
);
1101 if (k
->set_config
) {
1102 k
->set_config(vdev
, vdev
->config
);
1106 void virtio_config_modern_writel(VirtIODevice
*vdev
,
1107 uint32_t addr
, uint32_t data
)
1109 VirtioDeviceClass
*k
= VIRTIO_DEVICE_GET_CLASS(vdev
);
1110 uint32_t val
= data
;
1112 if (addr
+ sizeof(val
) > vdev
->config_len
) {
1116 stl_le_p(vdev
->config
+ addr
, val
);
1118 if (k
->set_config
) {
1119 k
->set_config(vdev
, vdev
->config
);
1123 void virtio_queue_set_addr(VirtIODevice
*vdev
, int n
, hwaddr addr
)
1125 vdev
->vq
[n
].vring
.desc
= addr
;
1126 virtio_queue_update_rings(vdev
, n
);
1129 hwaddr
virtio_queue_get_addr(VirtIODevice
*vdev
, int n
)
1131 return vdev
->vq
[n
].vring
.desc
;
1134 void virtio_queue_set_rings(VirtIODevice
*vdev
, int n
, hwaddr desc
,
1135 hwaddr avail
, hwaddr used
)
1137 vdev
->vq
[n
].vring
.desc
= desc
;
1138 vdev
->vq
[n
].vring
.avail
= avail
;
1139 vdev
->vq
[n
].vring
.used
= used
;
1142 void virtio_queue_set_num(VirtIODevice
*vdev
, int n
, int num
)
1144 /* Don't allow guest to flip queue between existent and
1145 * nonexistent states, or to set it to an invalid size.
1147 if (!!num
!= !!vdev
->vq
[n
].vring
.num
||
1148 num
> VIRTQUEUE_MAX_SIZE
||
1152 vdev
->vq
[n
].vring
.num
= num
;
1155 VirtQueue
*virtio_vector_first_queue(VirtIODevice
*vdev
, uint16_t vector
)
1157 return QLIST_FIRST(&vdev
->vector_queues
[vector
]);
1160 VirtQueue
*virtio_vector_next_queue(VirtQueue
*vq
)
1162 return QLIST_NEXT(vq
, node
);
1165 int virtio_queue_get_num(VirtIODevice
*vdev
, int n
)
1167 return vdev
->vq
[n
].vring
.num
;
1170 int virtio_get_num_queues(VirtIODevice
*vdev
)
1174 for (i
= 0; i
< VIRTIO_QUEUE_MAX
; i
++) {
1175 if (!virtio_queue_get_num(vdev
, i
)) {
1183 void virtio_queue_set_align(VirtIODevice
*vdev
, int n
, int align
)
1185 BusState
*qbus
= qdev_get_parent_bus(DEVICE(vdev
));
1186 VirtioBusClass
*k
= VIRTIO_BUS_GET_CLASS(qbus
);
1188 /* virtio-1 compliant devices cannot change the alignment */
1189 if (virtio_vdev_has_feature(vdev
, VIRTIO_F_VERSION_1
)) {
1190 error_report("tried to modify queue alignment for virtio-1 device");
1193 /* Check that the transport told us it was going to do this
1194 * (so a buggy transport will immediately assert rather than
1195 * silently failing to migrate this state)
1197 assert(k
->has_variable_vring_alignment
);
1199 vdev
->vq
[n
].vring
.align
= align
;
1200 virtio_queue_update_rings(vdev
, n
);
1203 static void virtio_queue_notify_aio_vq(VirtQueue
*vq
)
1205 if (vq
->vring
.desc
&& vq
->handle_aio_output
) {
1206 VirtIODevice
*vdev
= vq
->vdev
;
1208 trace_virtio_queue_notify(vdev
, vq
- vdev
->vq
, vq
);
1209 vq
->handle_aio_output(vdev
, vq
);
1213 static void virtio_queue_notify_vq(VirtQueue
*vq
)
1215 if (vq
->vring
.desc
&& vq
->handle_output
) {
1216 VirtIODevice
*vdev
= vq
->vdev
;
1218 if (unlikely(vdev
->broken
)) {
1222 trace_virtio_queue_notify(vdev
, vq
- vdev
->vq
, vq
);
1223 vq
->handle_output(vdev
, vq
);
1227 void virtio_queue_notify(VirtIODevice
*vdev
, int n
)
1229 virtio_queue_notify_vq(&vdev
->vq
[n
]);
1232 uint16_t virtio_queue_vector(VirtIODevice
*vdev
, int n
)
1234 return n
< VIRTIO_QUEUE_MAX
? vdev
->vq
[n
].vector
:
1238 void virtio_queue_set_vector(VirtIODevice
*vdev
, int n
, uint16_t vector
)
1240 VirtQueue
*vq
= &vdev
->vq
[n
];
1242 if (n
< VIRTIO_QUEUE_MAX
) {
1243 if (vdev
->vector_queues
&&
1244 vdev
->vq
[n
].vector
!= VIRTIO_NO_VECTOR
) {
1245 QLIST_REMOVE(vq
, node
);
1247 vdev
->vq
[n
].vector
= vector
;
1248 if (vdev
->vector_queues
&&
1249 vector
!= VIRTIO_NO_VECTOR
) {
1250 QLIST_INSERT_HEAD(&vdev
->vector_queues
[vector
], vq
, node
);
1255 static VirtQueue
*virtio_add_queue_internal(VirtIODevice
*vdev
, int queue_size
,
1256 VirtIOHandleOutput handle_output
,
1261 for (i
= 0; i
< VIRTIO_QUEUE_MAX
; i
++) {
1262 if (vdev
->vq
[i
].vring
.num
== 0)
1266 if (i
== VIRTIO_QUEUE_MAX
|| queue_size
> VIRTQUEUE_MAX_SIZE
)
1269 vdev
->vq
[i
].vring
.num
= queue_size
;
1270 vdev
->vq
[i
].vring
.num_default
= queue_size
;
1271 vdev
->vq
[i
].vring
.align
= VIRTIO_PCI_VRING_ALIGN
;
1272 vdev
->vq
[i
].handle_output
= handle_output
;
1273 vdev
->vq
[i
].handle_aio_output
= NULL
;
1274 vdev
->vq
[i
].use_aio
= use_aio
;
1276 return &vdev
->vq
[i
];
1279 /* Add a virt queue and mark AIO.
1280 * An AIO queue will use the AioContext based event interface instead of the
1281 * default IOHandler and EventNotifier interface.
1283 VirtQueue
*virtio_add_queue_aio(VirtIODevice
*vdev
, int queue_size
,
1284 VirtIOHandleOutput handle_output
)
1286 return virtio_add_queue_internal(vdev
, queue_size
, handle_output
, true);
1289 /* Add a normal virt queue (on the contrary to the AIO version above. */
1290 VirtQueue
*virtio_add_queue(VirtIODevice
*vdev
, int queue_size
,
1291 VirtIOHandleOutput handle_output
)
1293 return virtio_add_queue_internal(vdev
, queue_size
, handle_output
, false);
1296 void virtio_del_queue(VirtIODevice
*vdev
, int n
)
1298 if (n
< 0 || n
>= VIRTIO_QUEUE_MAX
) {
1302 vdev
->vq
[n
].vring
.num
= 0;
1303 vdev
->vq
[n
].vring
.num_default
= 0;
1306 void virtio_irq(VirtQueue
*vq
)
1308 trace_virtio_irq(vq
);
1309 vq
->vdev
->isr
|= 0x01;
1310 virtio_notify_vector(vq
->vdev
, vq
->vector
);
1313 bool virtio_should_notify(VirtIODevice
*vdev
, VirtQueue
*vq
)
1317 /* We need to expose used array entries before checking used event. */
1319 /* Always notify when queue is empty (when feature acknowledge) */
1320 if (virtio_vdev_has_feature(vdev
, VIRTIO_F_NOTIFY_ON_EMPTY
) &&
1321 !vq
->inuse
&& virtio_queue_empty(vq
)) {
1325 if (!virtio_vdev_has_feature(vdev
, VIRTIO_RING_F_EVENT_IDX
)) {
1326 return !(vring_avail_flags(vq
) & VRING_AVAIL_F_NO_INTERRUPT
);
1329 v
= vq
->signalled_used_valid
;
1330 vq
->signalled_used_valid
= true;
1331 old
= vq
->signalled_used
;
1332 new = vq
->signalled_used
= vq
->used_idx
;
1333 return !v
|| vring_need_event(vring_get_used_event(vq
), new, old
);
1336 void virtio_notify(VirtIODevice
*vdev
, VirtQueue
*vq
)
1338 if (!virtio_should_notify(vdev
, vq
)) {
1342 trace_virtio_notify(vdev
, vq
);
1344 virtio_notify_vector(vdev
, vq
->vector
);
1347 void virtio_notify_config(VirtIODevice
*vdev
)
1349 if (!(vdev
->status
& VIRTIO_CONFIG_S_DRIVER_OK
))
1354 virtio_notify_vector(vdev
, vdev
->config_vector
);
1357 static bool virtio_device_endian_needed(void *opaque
)
1359 VirtIODevice
*vdev
= opaque
;
1361 assert(vdev
->device_endian
!= VIRTIO_DEVICE_ENDIAN_UNKNOWN
);
1362 if (!virtio_vdev_has_feature(vdev
, VIRTIO_F_VERSION_1
)) {
1363 return vdev
->device_endian
!= virtio_default_endian();
1365 /* Devices conforming to VIRTIO 1.0 or later are always LE. */
1366 return vdev
->device_endian
!= VIRTIO_DEVICE_ENDIAN_LITTLE
;
1369 static bool virtio_64bit_features_needed(void *opaque
)
1371 VirtIODevice
*vdev
= opaque
;
1373 return (vdev
->host_features
>> 32) != 0;
1376 static bool virtio_virtqueue_needed(void *opaque
)
1378 VirtIODevice
*vdev
= opaque
;
1380 return virtio_host_has_feature(vdev
, VIRTIO_F_VERSION_1
);
1383 static bool virtio_ringsize_needed(void *opaque
)
1385 VirtIODevice
*vdev
= opaque
;
1388 for (i
= 0; i
< VIRTIO_QUEUE_MAX
; i
++) {
1389 if (vdev
->vq
[i
].vring
.num
!= vdev
->vq
[i
].vring
.num_default
) {
1396 static bool virtio_extra_state_needed(void *opaque
)
1398 VirtIODevice
*vdev
= opaque
;
1399 BusState
*qbus
= qdev_get_parent_bus(DEVICE(vdev
));
1400 VirtioBusClass
*k
= VIRTIO_BUS_GET_CLASS(qbus
);
1402 return k
->has_extra_state
&&
1403 k
->has_extra_state(qbus
->parent
);
1406 static bool virtio_broken_needed(void *opaque
)
1408 VirtIODevice
*vdev
= opaque
;
1410 return vdev
->broken
;
1413 static const VMStateDescription vmstate_virtqueue
= {
1414 .name
= "virtqueue_state",
1416 .minimum_version_id
= 1,
1417 .fields
= (VMStateField
[]) {
1418 VMSTATE_UINT64(vring
.avail
, struct VirtQueue
),
1419 VMSTATE_UINT64(vring
.used
, struct VirtQueue
),
1420 VMSTATE_END_OF_LIST()
1424 static const VMStateDescription vmstate_virtio_virtqueues
= {
1425 .name
= "virtio/virtqueues",
1427 .minimum_version_id
= 1,
1428 .needed
= &virtio_virtqueue_needed
,
1429 .fields
= (VMStateField
[]) {
1430 VMSTATE_STRUCT_VARRAY_POINTER_KNOWN(vq
, struct VirtIODevice
,
1431 VIRTIO_QUEUE_MAX
, 0, vmstate_virtqueue
, VirtQueue
),
1432 VMSTATE_END_OF_LIST()
1436 static const VMStateDescription vmstate_ringsize
= {
1437 .name
= "ringsize_state",
1439 .minimum_version_id
= 1,
1440 .fields
= (VMStateField
[]) {
1441 VMSTATE_UINT32(vring
.num_default
, struct VirtQueue
),
1442 VMSTATE_END_OF_LIST()
1446 static const VMStateDescription vmstate_virtio_ringsize
= {
1447 .name
= "virtio/ringsize",
1449 .minimum_version_id
= 1,
1450 .needed
= &virtio_ringsize_needed
,
1451 .fields
= (VMStateField
[]) {
1452 VMSTATE_STRUCT_VARRAY_POINTER_KNOWN(vq
, struct VirtIODevice
,
1453 VIRTIO_QUEUE_MAX
, 0, vmstate_ringsize
, VirtQueue
),
1454 VMSTATE_END_OF_LIST()
1458 static int get_extra_state(QEMUFile
*f
, void *pv
, size_t size
)
1460 VirtIODevice
*vdev
= pv
;
1461 BusState
*qbus
= qdev_get_parent_bus(DEVICE(vdev
));
1462 VirtioBusClass
*k
= VIRTIO_BUS_GET_CLASS(qbus
);
1464 if (!k
->load_extra_state
) {
1467 return k
->load_extra_state(qbus
->parent
, f
);
1471 static void put_extra_state(QEMUFile
*f
, void *pv
, size_t size
)
1473 VirtIODevice
*vdev
= pv
;
1474 BusState
*qbus
= qdev_get_parent_bus(DEVICE(vdev
));
1475 VirtioBusClass
*k
= VIRTIO_BUS_GET_CLASS(qbus
);
1477 k
->save_extra_state(qbus
->parent
, f
);
1480 static const VMStateInfo vmstate_info_extra_state
= {
1481 .name
= "virtqueue_extra_state",
1482 .get
= get_extra_state
,
1483 .put
= put_extra_state
,
1486 static const VMStateDescription vmstate_virtio_extra_state
= {
1487 .name
= "virtio/extra_state",
1489 .minimum_version_id
= 1,
1490 .needed
= &virtio_extra_state_needed
,
1491 .fields
= (VMStateField
[]) {
1493 .name
= "extra_state",
1495 .field_exists
= NULL
,
1497 .info
= &vmstate_info_extra_state
,
1498 .flags
= VMS_SINGLE
,
1501 VMSTATE_END_OF_LIST()
1505 static const VMStateDescription vmstate_virtio_device_endian
= {
1506 .name
= "virtio/device_endian",
1508 .minimum_version_id
= 1,
1509 .needed
= &virtio_device_endian_needed
,
1510 .fields
= (VMStateField
[]) {
1511 VMSTATE_UINT8(device_endian
, VirtIODevice
),
1512 VMSTATE_END_OF_LIST()
1516 static const VMStateDescription vmstate_virtio_64bit_features
= {
1517 .name
= "virtio/64bit_features",
1519 .minimum_version_id
= 1,
1520 .needed
= &virtio_64bit_features_needed
,
1521 .fields
= (VMStateField
[]) {
1522 VMSTATE_UINT64(guest_features
, VirtIODevice
),
1523 VMSTATE_END_OF_LIST()
1527 static const VMStateDescription vmstate_virtio_broken
= {
1528 .name
= "virtio/broken",
1530 .minimum_version_id
= 1,
1531 .needed
= &virtio_broken_needed
,
1532 .fields
= (VMStateField
[]) {
1533 VMSTATE_BOOL(broken
, VirtIODevice
),
1534 VMSTATE_END_OF_LIST()
1538 static const VMStateDescription vmstate_virtio
= {
1541 .minimum_version_id
= 1,
1542 .minimum_version_id_old
= 1,
1543 .fields
= (VMStateField
[]) {
1544 VMSTATE_END_OF_LIST()
1546 .subsections
= (const VMStateDescription
*[]) {
1547 &vmstate_virtio_device_endian
,
1548 &vmstate_virtio_64bit_features
,
1549 &vmstate_virtio_virtqueues
,
1550 &vmstate_virtio_ringsize
,
1551 &vmstate_virtio_broken
,
1552 &vmstate_virtio_extra_state
,
1557 void virtio_save(VirtIODevice
*vdev
, QEMUFile
*f
)
1559 BusState
*qbus
= qdev_get_parent_bus(DEVICE(vdev
));
1560 VirtioBusClass
*k
= VIRTIO_BUS_GET_CLASS(qbus
);
1561 VirtioDeviceClass
*vdc
= VIRTIO_DEVICE_GET_CLASS(vdev
);
1562 uint32_t guest_features_lo
= (vdev
->guest_features
& 0xffffffff);
1565 if (k
->save_config
) {
1566 k
->save_config(qbus
->parent
, f
);
1569 qemu_put_8s(f
, &vdev
->status
);
1570 qemu_put_8s(f
, &vdev
->isr
);
1571 qemu_put_be16s(f
, &vdev
->queue_sel
);
1572 qemu_put_be32s(f
, &guest_features_lo
);
1573 qemu_put_be32(f
, vdev
->config_len
);
1574 qemu_put_buffer(f
, vdev
->config
, vdev
->config_len
);
1576 for (i
= 0; i
< VIRTIO_QUEUE_MAX
; i
++) {
1577 if (vdev
->vq
[i
].vring
.num
== 0)
1581 qemu_put_be32(f
, i
);
1583 for (i
= 0; i
< VIRTIO_QUEUE_MAX
; i
++) {
1584 if (vdev
->vq
[i
].vring
.num
== 0)
1587 qemu_put_be32(f
, vdev
->vq
[i
].vring
.num
);
1588 if (k
->has_variable_vring_alignment
) {
1589 qemu_put_be32(f
, vdev
->vq
[i
].vring
.align
);
1591 /* XXX virtio-1 devices */
1592 qemu_put_be64(f
, vdev
->vq
[i
].vring
.desc
);
1593 qemu_put_be16s(f
, &vdev
->vq
[i
].last_avail_idx
);
1594 if (k
->save_queue
) {
1595 k
->save_queue(qbus
->parent
, i
, f
);
1599 if (vdc
->save
!= NULL
) {
1604 vmstate_save_state(f
, &vmstate_virtio
, vdev
, NULL
);
1607 /* A wrapper for use as a VMState .put function */
1608 void virtio_vmstate_save(QEMUFile
*f
, void *opaque
, size_t size
)
1610 virtio_save(VIRTIO_DEVICE(opaque
), f
);
1613 static int virtio_set_features_nocheck(VirtIODevice
*vdev
, uint64_t val
)
1615 VirtioDeviceClass
*k
= VIRTIO_DEVICE_GET_CLASS(vdev
);
1616 bool bad
= (val
& ~(vdev
->host_features
)) != 0;
1618 val
&= vdev
->host_features
;
1619 if (k
->set_features
) {
1620 k
->set_features(vdev
, val
);
1622 vdev
->guest_features
= val
;
1623 return bad
? -1 : 0;
1626 int virtio_set_features(VirtIODevice
*vdev
, uint64_t val
)
1629 * The driver must not attempt to set features after feature negotiation
1632 if (vdev
->status
& VIRTIO_CONFIG_S_FEATURES_OK
) {
1635 return virtio_set_features_nocheck(vdev
, val
);
1638 int virtio_load(VirtIODevice
*vdev
, QEMUFile
*f
, int version_id
)
1644 BusState
*qbus
= qdev_get_parent_bus(DEVICE(vdev
));
1645 VirtioBusClass
*k
= VIRTIO_BUS_GET_CLASS(qbus
);
1646 VirtioDeviceClass
*vdc
= VIRTIO_DEVICE_GET_CLASS(vdev
);
1649 * We poison the endianness to ensure it does not get used before
1650 * subsections have been loaded.
1652 vdev
->device_endian
= VIRTIO_DEVICE_ENDIAN_UNKNOWN
;
1654 if (k
->load_config
) {
1655 ret
= k
->load_config(qbus
->parent
, f
);
1660 qemu_get_8s(f
, &vdev
->status
);
1661 qemu_get_8s(f
, &vdev
->isr
);
1662 qemu_get_be16s(f
, &vdev
->queue_sel
);
1663 if (vdev
->queue_sel
>= VIRTIO_QUEUE_MAX
) {
1666 qemu_get_be32s(f
, &features
);
1669 * Temporarily set guest_features low bits - needed by
1670 * virtio net load code testing for VIRTIO_NET_F_CTRL_GUEST_OFFLOADS
1671 * VIRTIO_NET_F_GUEST_ANNOUNCE and VIRTIO_NET_F_CTRL_VQ.
1673 * Note: devices should always test host features in future - don't create
1674 * new dependencies like this.
1676 vdev
->guest_features
= features
;
1678 config_len
= qemu_get_be32(f
);
1681 * There are cases where the incoming config can be bigger or smaller
1682 * than what we have; so load what we have space for, and skip
1683 * any excess that's in the stream.
1685 qemu_get_buffer(f
, vdev
->config
, MIN(config_len
, vdev
->config_len
));
1687 while (config_len
> vdev
->config_len
) {
1692 num
= qemu_get_be32(f
);
1694 if (num
> VIRTIO_QUEUE_MAX
) {
1695 error_report("Invalid number of virtqueues: 0x%x", num
);
1699 for (i
= 0; i
< num
; i
++) {
1700 vdev
->vq
[i
].vring
.num
= qemu_get_be32(f
);
1701 if (k
->has_variable_vring_alignment
) {
1702 vdev
->vq
[i
].vring
.align
= qemu_get_be32(f
);
1704 vdev
->vq
[i
].vring
.desc
= qemu_get_be64(f
);
1705 qemu_get_be16s(f
, &vdev
->vq
[i
].last_avail_idx
);
1706 vdev
->vq
[i
].signalled_used_valid
= false;
1707 vdev
->vq
[i
].notification
= true;
1709 if (vdev
->vq
[i
].vring
.desc
) {
1710 /* XXX virtio-1 devices */
1711 virtio_queue_update_rings(vdev
, i
);
1712 } else if (vdev
->vq
[i
].last_avail_idx
) {
1713 error_report("VQ %d address 0x0 "
1714 "inconsistent with Host index 0x%x",
1715 i
, vdev
->vq
[i
].last_avail_idx
);
1718 if (k
->load_queue
) {
1719 ret
= k
->load_queue(qbus
->parent
, i
, f
);
1725 virtio_notify_vector(vdev
, VIRTIO_NO_VECTOR
);
1727 if (vdc
->load
!= NULL
) {
1728 ret
= vdc
->load(vdev
, f
, version_id
);
1735 ret
= vmstate_load_state(f
, &vmstate_virtio
, vdev
, 1);
1740 if (vdev
->device_endian
== VIRTIO_DEVICE_ENDIAN_UNKNOWN
) {
1741 vdev
->device_endian
= virtio_default_endian();
1744 if (virtio_64bit_features_needed(vdev
)) {
1746 * Subsection load filled vdev->guest_features. Run them
1747 * through virtio_set_features to sanity-check them against
1750 uint64_t features64
= vdev
->guest_features
;
1751 if (virtio_set_features_nocheck(vdev
, features64
) < 0) {
1752 error_report("Features 0x%" PRIx64
" unsupported. "
1753 "Allowed features: 0x%" PRIx64
,
1754 features64
, vdev
->host_features
);
1758 if (virtio_set_features_nocheck(vdev
, features
) < 0) {
1759 error_report("Features 0x%x unsupported. "
1760 "Allowed features: 0x%" PRIx64
,
1761 features
, vdev
->host_features
);
1766 for (i
= 0; i
< num
; i
++) {
1767 if (vdev
->vq
[i
].vring
.desc
) {
1769 nheads
= vring_avail_idx(&vdev
->vq
[i
]) - vdev
->vq
[i
].last_avail_idx
;
1770 /* Check it isn't doing strange things with descriptor numbers. */
1771 if (nheads
> vdev
->vq
[i
].vring
.num
) {
1772 error_report("VQ %d size 0x%x Guest index 0x%x "
1773 "inconsistent with Host index 0x%x: delta 0x%x",
1774 i
, vdev
->vq
[i
].vring
.num
,
1775 vring_avail_idx(&vdev
->vq
[i
]),
1776 vdev
->vq
[i
].last_avail_idx
, nheads
);
1779 vdev
->vq
[i
].used_idx
= vring_used_idx(&vdev
->vq
[i
]);
1780 vdev
->vq
[i
].shadow_avail_idx
= vring_avail_idx(&vdev
->vq
[i
]);
1783 * Some devices migrate VirtQueueElements that have been popped
1784 * from the avail ring but not yet returned to the used ring.
1786 vdev
->vq
[i
].inuse
= vdev
->vq
[i
].last_avail_idx
-
1787 vdev
->vq
[i
].used_idx
;
1788 if (vdev
->vq
[i
].inuse
> vdev
->vq
[i
].vring
.num
) {
1789 error_report("VQ %d size 0x%x < last_avail_idx 0x%x - "
1791 i
, vdev
->vq
[i
].vring
.num
,
1792 vdev
->vq
[i
].last_avail_idx
,
1793 vdev
->vq
[i
].used_idx
);
1802 void virtio_cleanup(VirtIODevice
*vdev
)
1804 qemu_del_vm_change_state_handler(vdev
->vmstate
);
1805 g_free(vdev
->config
);
1807 g_free(vdev
->vector_queues
);
1810 static void virtio_vmstate_change(void *opaque
, int running
, RunState state
)
1812 VirtIODevice
*vdev
= opaque
;
1813 BusState
*qbus
= qdev_get_parent_bus(DEVICE(vdev
));
1814 VirtioBusClass
*k
= VIRTIO_BUS_GET_CLASS(qbus
);
1815 bool backend_run
= running
&& (vdev
->status
& VIRTIO_CONFIG_S_DRIVER_OK
);
1816 vdev
->vm_running
= running
;
1819 virtio_set_status(vdev
, vdev
->status
);
1822 if (k
->vmstate_change
) {
1823 k
->vmstate_change(qbus
->parent
, backend_run
);
1827 virtio_set_status(vdev
, vdev
->status
);
1831 void virtio_instance_init_common(Object
*proxy_obj
, void *data
,
1832 size_t vdev_size
, const char *vdev_name
)
1834 DeviceState
*vdev
= data
;
1836 object_initialize(vdev
, vdev_size
, vdev_name
);
1837 object_property_add_child(proxy_obj
, "virtio-backend", OBJECT(vdev
), NULL
);
1838 object_unref(OBJECT(vdev
));
1839 qdev_alias_all_properties(vdev
, proxy_obj
);
1842 void virtio_init(VirtIODevice
*vdev
, const char *name
,
1843 uint16_t device_id
, size_t config_size
)
1845 BusState
*qbus
= qdev_get_parent_bus(DEVICE(vdev
));
1846 VirtioBusClass
*k
= VIRTIO_BUS_GET_CLASS(qbus
);
1848 int nvectors
= k
->query_nvectors
? k
->query_nvectors(qbus
->parent
) : 0;
1851 vdev
->vector_queues
=
1852 g_malloc0(sizeof(*vdev
->vector_queues
) * nvectors
);
1855 vdev
->device_id
= device_id
;
1858 vdev
->queue_sel
= 0;
1859 vdev
->config_vector
= VIRTIO_NO_VECTOR
;
1860 vdev
->vq
= g_malloc0(sizeof(VirtQueue
) * VIRTIO_QUEUE_MAX
);
1861 vdev
->vm_running
= runstate_is_running();
1862 vdev
->broken
= false;
1863 for (i
= 0; i
< VIRTIO_QUEUE_MAX
; i
++) {
1864 vdev
->vq
[i
].vector
= VIRTIO_NO_VECTOR
;
1865 vdev
->vq
[i
].vdev
= vdev
;
1866 vdev
->vq
[i
].queue_index
= i
;
1870 vdev
->config_len
= config_size
;
1871 if (vdev
->config_len
) {
1872 vdev
->config
= g_malloc0(config_size
);
1874 vdev
->config
= NULL
;
1876 vdev
->vmstate
= qemu_add_vm_change_state_handler(virtio_vmstate_change
,
1878 vdev
->device_endian
= virtio_default_endian();
1879 vdev
->use_guest_notifier_mask
= true;
1882 hwaddr
virtio_queue_get_desc_addr(VirtIODevice
*vdev
, int n
)
1884 return vdev
->vq
[n
].vring
.desc
;
1887 hwaddr
virtio_queue_get_avail_addr(VirtIODevice
*vdev
, int n
)
1889 return vdev
->vq
[n
].vring
.avail
;
1892 hwaddr
virtio_queue_get_used_addr(VirtIODevice
*vdev
, int n
)
1894 return vdev
->vq
[n
].vring
.used
;
1897 hwaddr
virtio_queue_get_ring_addr(VirtIODevice
*vdev
, int n
)
1899 return vdev
->vq
[n
].vring
.desc
;
1902 hwaddr
virtio_queue_get_desc_size(VirtIODevice
*vdev
, int n
)
1904 return sizeof(VRingDesc
) * vdev
->vq
[n
].vring
.num
;
1907 hwaddr
virtio_queue_get_avail_size(VirtIODevice
*vdev
, int n
)
1909 return offsetof(VRingAvail
, ring
) +
1910 sizeof(uint16_t) * vdev
->vq
[n
].vring
.num
;
1913 hwaddr
virtio_queue_get_used_size(VirtIODevice
*vdev
, int n
)
1915 return offsetof(VRingUsed
, ring
) +
1916 sizeof(VRingUsedElem
) * vdev
->vq
[n
].vring
.num
;
1919 hwaddr
virtio_queue_get_ring_size(VirtIODevice
*vdev
, int n
)
1921 return vdev
->vq
[n
].vring
.used
- vdev
->vq
[n
].vring
.desc
+
1922 virtio_queue_get_used_size(vdev
, n
);
1925 uint16_t virtio_queue_get_last_avail_idx(VirtIODevice
*vdev
, int n
)
1927 return vdev
->vq
[n
].last_avail_idx
;
1930 void virtio_queue_set_last_avail_idx(VirtIODevice
*vdev
, int n
, uint16_t idx
)
1932 vdev
->vq
[n
].last_avail_idx
= idx
;
1933 vdev
->vq
[n
].shadow_avail_idx
= idx
;
1936 void virtio_queue_invalidate_signalled_used(VirtIODevice
*vdev
, int n
)
1938 vdev
->vq
[n
].signalled_used_valid
= false;
1941 VirtQueue
*virtio_get_queue(VirtIODevice
*vdev
, int n
)
1943 return vdev
->vq
+ n
;
1946 uint16_t virtio_get_queue_index(VirtQueue
*vq
)
1948 return vq
->queue_index
;
1951 static void virtio_queue_guest_notifier_read(EventNotifier
*n
)
1953 VirtQueue
*vq
= container_of(n
, VirtQueue
, guest_notifier
);
1954 if (event_notifier_test_and_clear(n
)) {
1959 void virtio_queue_set_guest_notifier_fd_handler(VirtQueue
*vq
, bool assign
,
1962 if (assign
&& !with_irqfd
) {
1963 event_notifier_set_handler(&vq
->guest_notifier
, false,
1964 virtio_queue_guest_notifier_read
);
1966 event_notifier_set_handler(&vq
->guest_notifier
, false, NULL
);
1969 /* Test and clear notifier before closing it,
1970 * in case poll callback didn't have time to run. */
1971 virtio_queue_guest_notifier_read(&vq
->guest_notifier
);
1975 EventNotifier
*virtio_queue_get_guest_notifier(VirtQueue
*vq
)
1977 return &vq
->guest_notifier
;
1980 static void virtio_queue_host_notifier_aio_read(EventNotifier
*n
)
1982 VirtQueue
*vq
= container_of(n
, VirtQueue
, host_notifier
);
1983 if (event_notifier_test_and_clear(n
)) {
1984 virtio_queue_notify_aio_vq(vq
);
1988 void virtio_queue_aio_set_host_notifier_handler(VirtQueue
*vq
, AioContext
*ctx
,
1989 VirtIOHandleOutput handle_output
)
1991 if (handle_output
) {
1992 vq
->handle_aio_output
= handle_output
;
1993 aio_set_event_notifier(ctx
, &vq
->host_notifier
, true,
1994 virtio_queue_host_notifier_aio_read
);
1996 aio_set_event_notifier(ctx
, &vq
->host_notifier
, true, NULL
);
1997 /* Test and clear notifier before after disabling event,
1998 * in case poll callback didn't have time to run. */
1999 virtio_queue_host_notifier_aio_read(&vq
->host_notifier
);
2000 vq
->handle_aio_output
= NULL
;
2004 static void virtio_queue_host_notifier_read(EventNotifier
*n
)
2006 VirtQueue
*vq
= container_of(n
, VirtQueue
, host_notifier
);
2007 if (event_notifier_test_and_clear(n
)) {
2008 virtio_queue_notify_vq(vq
);
2012 void virtio_queue_set_host_notifier_fd_handler(VirtQueue
*vq
, bool assign
,
2015 AioContext
*ctx
= qemu_get_aio_context();
2016 if (assign
&& set_handler
) {
2018 aio_set_event_notifier(ctx
, &vq
->host_notifier
, true,
2019 virtio_queue_host_notifier_read
);
2021 event_notifier_set_handler(&vq
->host_notifier
, true,
2022 virtio_queue_host_notifier_read
);
2026 aio_set_event_notifier(ctx
, &vq
->host_notifier
, true, NULL
);
2028 event_notifier_set_handler(&vq
->host_notifier
, true, NULL
);
2032 /* Test and clear notifier before after disabling event,
2033 * in case poll callback didn't have time to run. */
2034 virtio_queue_host_notifier_read(&vq
->host_notifier
);
2038 EventNotifier
*virtio_queue_get_host_notifier(VirtQueue
*vq
)
2040 return &vq
->host_notifier
;
2043 void virtio_device_set_child_bus_name(VirtIODevice
*vdev
, char *bus_name
)
2045 g_free(vdev
->bus_name
);
2046 vdev
->bus_name
= g_strdup(bus_name
);
2049 void GCC_FMT_ATTR(2, 3) virtio_error(VirtIODevice
*vdev
, const char *fmt
, ...)
2054 error_vreport(fmt
, ap
);
2057 vdev
->broken
= true;
2059 if (virtio_vdev_has_feature(vdev
, VIRTIO_F_VERSION_1
)) {
2060 virtio_set_status(vdev
, vdev
->status
| VIRTIO_CONFIG_S_NEEDS_RESET
);
2061 virtio_notify_config(vdev
);
2065 static void virtio_device_realize(DeviceState
*dev
, Error
**errp
)
2067 VirtIODevice
*vdev
= VIRTIO_DEVICE(dev
);
2068 VirtioDeviceClass
*vdc
= VIRTIO_DEVICE_GET_CLASS(dev
);
2071 if (vdc
->realize
!= NULL
) {
2072 vdc
->realize(dev
, &err
);
2074 error_propagate(errp
, err
);
2079 virtio_bus_device_plugged(vdev
, &err
);
2081 error_propagate(errp
, err
);
2086 static void virtio_device_unrealize(DeviceState
*dev
, Error
**errp
)
2088 VirtIODevice
*vdev
= VIRTIO_DEVICE(dev
);
2089 VirtioDeviceClass
*vdc
= VIRTIO_DEVICE_GET_CLASS(dev
);
2092 virtio_bus_device_unplugged(vdev
);
2094 if (vdc
->unrealize
!= NULL
) {
2095 vdc
->unrealize(dev
, &err
);
2097 error_propagate(errp
, err
);
2102 g_free(vdev
->bus_name
);
2103 vdev
->bus_name
= NULL
;
2106 static Property virtio_properties
[] = {
2107 DEFINE_VIRTIO_COMMON_FEATURES(VirtIODevice
, host_features
),
2108 DEFINE_PROP_END_OF_LIST(),
2111 static void virtio_device_class_init(ObjectClass
*klass
, void *data
)
2113 /* Set the default value here. */
2114 DeviceClass
*dc
= DEVICE_CLASS(klass
);
2116 dc
->realize
= virtio_device_realize
;
2117 dc
->unrealize
= virtio_device_unrealize
;
2118 dc
->bus_type
= TYPE_VIRTIO_BUS
;
2119 dc
->props
= virtio_properties
;
2122 static const TypeInfo virtio_device_info
= {
2123 .name
= TYPE_VIRTIO_DEVICE
,
2124 .parent
= TYPE_DEVICE
,
2125 .instance_size
= sizeof(VirtIODevice
),
2126 .class_init
= virtio_device_class_init
,
2128 .class_size
= sizeof(VirtioDeviceClass
),
2131 static void virtio_register_types(void)
2133 type_register_static(&virtio_device_info
);
2136 type_init(virtio_register_types
)