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
);
275 void virtqueue_fill(VirtQueue
*vq
, const VirtQueueElement
*elem
,
276 unsigned int len
, unsigned int idx
)
280 trace_virtqueue_fill(vq
, elem
, len
, idx
);
282 virtqueue_unmap_sg(vq
, elem
, len
);
284 idx
= (idx
+ vq
->used_idx
) % vq
->vring
.num
;
286 uelem
.id
= elem
->index
;
288 vring_used_write(vq
, &uelem
, idx
);
291 void virtqueue_flush(VirtQueue
*vq
, unsigned int count
)
294 /* Make sure buffer is written before we update index. */
296 trace_virtqueue_flush(vq
, count
);
299 vring_used_idx_set(vq
, new);
301 if (unlikely((int16_t)(new - vq
->signalled_used
) < (uint16_t)(new - old
)))
302 vq
->signalled_used_valid
= false;
305 void virtqueue_push(VirtQueue
*vq
, const VirtQueueElement
*elem
,
308 virtqueue_fill(vq
, elem
, len
, 0);
309 virtqueue_flush(vq
, 1);
312 static int virtqueue_num_heads(VirtQueue
*vq
, unsigned int idx
)
314 uint16_t num_heads
= vring_avail_idx(vq
) - idx
;
316 /* Check it isn't doing very strange things with descriptor numbers. */
317 if (num_heads
> vq
->vring
.num
) {
318 error_report("Guest moved used index from %u to %u",
319 idx
, vq
->shadow_avail_idx
);
322 /* On success, callers read a descriptor at vq->last_avail_idx.
323 * Make sure descriptor read does not bypass avail index read. */
331 static unsigned int virtqueue_get_head(VirtQueue
*vq
, unsigned int idx
)
335 /* Grab the next descriptor number they're advertising, and increment
336 * the index we've seen. */
337 head
= vring_avail_ring(vq
, idx
% vq
->vring
.num
);
339 /* If their number is silly, that's a fatal mistake. */
340 if (head
>= vq
->vring
.num
) {
341 error_report("Guest says index %u is available", head
);
348 static unsigned virtqueue_read_next_desc(VirtIODevice
*vdev
, VRingDesc
*desc
,
349 hwaddr desc_pa
, unsigned int max
)
353 /* If this descriptor says it doesn't chain, we're done. */
354 if (!(desc
->flags
& VRING_DESC_F_NEXT
)) {
358 /* Check they're not leading us off end of descriptors. */
360 /* Make sure compiler knows to grab that: we don't want it changing! */
364 error_report("Desc next is %u", next
);
368 vring_desc_read(vdev
, desc
, desc_pa
, next
);
372 void virtqueue_get_avail_bytes(VirtQueue
*vq
, unsigned int *in_bytes
,
373 unsigned int *out_bytes
,
374 unsigned max_in_bytes
, unsigned max_out_bytes
)
377 unsigned int total_bufs
, in_total
, out_total
;
379 idx
= vq
->last_avail_idx
;
381 total_bufs
= in_total
= out_total
= 0;
382 while (virtqueue_num_heads(vq
, idx
)) {
383 VirtIODevice
*vdev
= vq
->vdev
;
384 unsigned int max
, num_bufs
, indirect
= 0;
390 num_bufs
= total_bufs
;
391 i
= virtqueue_get_head(vq
, idx
++);
392 desc_pa
= vq
->vring
.desc
;
393 vring_desc_read(vdev
, &desc
, desc_pa
, i
);
395 if (desc
.flags
& VRING_DESC_F_INDIRECT
) {
396 if (desc
.len
% sizeof(VRingDesc
)) {
397 error_report("Invalid size for indirect buffer table");
401 /* If we've got too many, that implies a descriptor loop. */
402 if (num_bufs
>= max
) {
403 error_report("Looped descriptor");
407 /* loop over the indirect descriptor table */
409 max
= desc
.len
/ sizeof(VRingDesc
);
412 vring_desc_read(vdev
, &desc
, desc_pa
, i
);
416 /* If we've got too many, that implies a descriptor loop. */
417 if (++num_bufs
> max
) {
418 error_report("Looped descriptor");
422 if (desc
.flags
& VRING_DESC_F_WRITE
) {
423 in_total
+= desc
.len
;
425 out_total
+= desc
.len
;
427 if (in_total
>= max_in_bytes
&& out_total
>= max_out_bytes
) {
430 } while ((i
= virtqueue_read_next_desc(vdev
, &desc
, desc_pa
, max
)) != max
);
433 total_bufs
= num_bufs
;
439 *in_bytes
= in_total
;
442 *out_bytes
= out_total
;
446 int virtqueue_avail_bytes(VirtQueue
*vq
, unsigned int in_bytes
,
447 unsigned int out_bytes
)
449 unsigned int in_total
, out_total
;
451 virtqueue_get_avail_bytes(vq
, &in_total
, &out_total
, in_bytes
, out_bytes
);
452 return in_bytes
<= in_total
&& out_bytes
<= out_total
;
455 static void virtqueue_map_desc(unsigned int *p_num_sg
, hwaddr
*addr
, struct iovec
*iov
,
456 unsigned int max_num_sg
, bool is_write
,
457 hwaddr pa
, size_t sz
)
459 unsigned num_sg
= *p_num_sg
;
460 assert(num_sg
<= max_num_sg
);
463 error_report("virtio: zero sized buffers are not allowed");
470 if (num_sg
== max_num_sg
) {
471 error_report("virtio: too many write descriptors in indirect table");
475 iov
[num_sg
].iov_base
= cpu_physical_memory_map(pa
, &len
, is_write
);
476 iov
[num_sg
].iov_len
= len
;
486 static void virtqueue_map_iovec(struct iovec
*sg
, hwaddr
*addr
,
487 unsigned int *num_sg
, unsigned int max_size
,
493 /* Note: this function MUST validate input, some callers
494 * are passing in num_sg values received over the network.
496 /* TODO: teach all callers that this can fail, and return failure instead
498 * When we do, we might be able to re-enable NDEBUG below.
501 #error building with NDEBUG is not supported
503 assert(*num_sg
<= max_size
);
505 for (i
= 0; i
< *num_sg
; i
++) {
507 sg
[i
].iov_base
= cpu_physical_memory_map(addr
[i
], &len
, is_write
);
508 if (!sg
[i
].iov_base
) {
509 error_report("virtio: error trying to map MMIO memory");
512 if (len
!= sg
[i
].iov_len
) {
513 error_report("virtio: unexpected memory split");
519 void virtqueue_map(VirtQueueElement
*elem
)
521 virtqueue_map_iovec(elem
->in_sg
, elem
->in_addr
, &elem
->in_num
,
522 VIRTQUEUE_MAX_SIZE
, 1);
523 virtqueue_map_iovec(elem
->out_sg
, elem
->out_addr
, &elem
->out_num
,
524 VIRTQUEUE_MAX_SIZE
, 0);
527 void *virtqueue_alloc_element(size_t sz
, unsigned out_num
, unsigned in_num
)
529 VirtQueueElement
*elem
;
530 size_t in_addr_ofs
= QEMU_ALIGN_UP(sz
, __alignof__(elem
->in_addr
[0]));
531 size_t out_addr_ofs
= in_addr_ofs
+ in_num
* sizeof(elem
->in_addr
[0]);
532 size_t out_addr_end
= out_addr_ofs
+ out_num
* sizeof(elem
->out_addr
[0]);
533 size_t in_sg_ofs
= QEMU_ALIGN_UP(out_addr_end
, __alignof__(elem
->in_sg
[0]));
534 size_t out_sg_ofs
= in_sg_ofs
+ in_num
* sizeof(elem
->in_sg
[0]);
535 size_t out_sg_end
= out_sg_ofs
+ out_num
* sizeof(elem
->out_sg
[0]);
537 assert(sz
>= sizeof(VirtQueueElement
));
538 elem
= g_malloc(out_sg_end
);
539 elem
->out_num
= out_num
;
540 elem
->in_num
= in_num
;
541 elem
->in_addr
= (void *)elem
+ in_addr_ofs
;
542 elem
->out_addr
= (void *)elem
+ out_addr_ofs
;
543 elem
->in_sg
= (void *)elem
+ in_sg_ofs
;
544 elem
->out_sg
= (void *)elem
+ out_sg_ofs
;
548 void *virtqueue_pop(VirtQueue
*vq
, size_t sz
)
550 unsigned int i
, head
, max
;
551 hwaddr desc_pa
= vq
->vring
.desc
;
552 VirtIODevice
*vdev
= vq
->vdev
;
553 VirtQueueElement
*elem
;
554 unsigned out_num
, in_num
;
555 hwaddr addr
[VIRTQUEUE_MAX_SIZE
];
556 struct iovec iov
[VIRTQUEUE_MAX_SIZE
];
559 if (virtio_queue_empty(vq
)) {
562 /* Needed after virtio_queue_empty(), see comment in
563 * virtqueue_num_heads(). */
566 /* When we start there are none of either input nor output. */
567 out_num
= in_num
= 0;
571 if (vq
->inuse
>= vq
->vring
.num
) {
572 error_report("Virtqueue size exceeded");
576 i
= head
= virtqueue_get_head(vq
, vq
->last_avail_idx
++);
577 if (virtio_vdev_has_feature(vdev
, VIRTIO_RING_F_EVENT_IDX
)) {
578 vring_set_avail_event(vq
, vq
->last_avail_idx
);
581 vring_desc_read(vdev
, &desc
, desc_pa
, i
);
582 if (desc
.flags
& VRING_DESC_F_INDIRECT
) {
583 if (desc
.len
% sizeof(VRingDesc
)) {
584 error_report("Invalid size for indirect buffer table");
588 /* loop over the indirect descriptor table */
589 max
= desc
.len
/ sizeof(VRingDesc
);
592 vring_desc_read(vdev
, &desc
, desc_pa
, i
);
595 /* Collect all the descriptors */
597 if (desc
.flags
& VRING_DESC_F_WRITE
) {
598 virtqueue_map_desc(&in_num
, addr
+ out_num
, iov
+ out_num
,
599 VIRTQUEUE_MAX_SIZE
- out_num
, true, desc
.addr
, desc
.len
);
602 error_report("Incorrect order for descriptors");
605 virtqueue_map_desc(&out_num
, addr
, iov
,
606 VIRTQUEUE_MAX_SIZE
, false, desc
.addr
, desc
.len
);
609 /* If we've got too many, that implies a descriptor loop. */
610 if ((in_num
+ out_num
) > max
) {
611 error_report("Looped descriptor");
614 } while ((i
= virtqueue_read_next_desc(vdev
, &desc
, desc_pa
, max
)) != max
);
616 /* Now copy what we have collected and mapped */
617 elem
= virtqueue_alloc_element(sz
, out_num
, in_num
);
619 for (i
= 0; i
< out_num
; i
++) {
620 elem
->out_addr
[i
] = addr
[i
];
621 elem
->out_sg
[i
] = iov
[i
];
623 for (i
= 0; i
< in_num
; i
++) {
624 elem
->in_addr
[i
] = addr
[out_num
+ i
];
625 elem
->in_sg
[i
] = iov
[out_num
+ i
];
630 trace_virtqueue_pop(vq
, elem
, elem
->in_num
, elem
->out_num
);
634 /* Reading and writing a structure directly to QEMUFile is *awful*, but
635 * it is what QEMU has always done by mistake. We can change it sooner
636 * or later by bumping the version number of the affected vm states.
637 * In the meanwhile, since the in-memory layout of VirtQueueElement
638 * has changed, we need to marshal to and from the layout that was
639 * used before the change.
641 typedef struct VirtQueueElementOld
{
643 unsigned int out_num
;
645 hwaddr in_addr
[VIRTQUEUE_MAX_SIZE
];
646 hwaddr out_addr
[VIRTQUEUE_MAX_SIZE
];
647 struct iovec in_sg
[VIRTQUEUE_MAX_SIZE
];
648 struct iovec out_sg
[VIRTQUEUE_MAX_SIZE
];
649 } VirtQueueElementOld
;
651 void *qemu_get_virtqueue_element(QEMUFile
*f
, size_t sz
)
653 VirtQueueElement
*elem
;
654 VirtQueueElementOld data
;
657 qemu_get_buffer(f
, (uint8_t *)&data
, sizeof(VirtQueueElementOld
));
659 elem
= virtqueue_alloc_element(sz
, data
.out_num
, data
.in_num
);
660 elem
->index
= data
.index
;
662 for (i
= 0; i
< elem
->in_num
; i
++) {
663 elem
->in_addr
[i
] = data
.in_addr
[i
];
666 for (i
= 0; i
< elem
->out_num
; i
++) {
667 elem
->out_addr
[i
] = data
.out_addr
[i
];
670 for (i
= 0; i
< elem
->in_num
; i
++) {
671 /* Base is overwritten by virtqueue_map. */
672 elem
->in_sg
[i
].iov_base
= 0;
673 elem
->in_sg
[i
].iov_len
= data
.in_sg
[i
].iov_len
;
676 for (i
= 0; i
< elem
->out_num
; i
++) {
677 /* Base is overwritten by virtqueue_map. */
678 elem
->out_sg
[i
].iov_base
= 0;
679 elem
->out_sg
[i
].iov_len
= data
.out_sg
[i
].iov_len
;
686 void qemu_put_virtqueue_element(QEMUFile
*f
, VirtQueueElement
*elem
)
688 VirtQueueElementOld data
;
691 memset(&data
, 0, sizeof(data
));
692 data
.index
= elem
->index
;
693 data
.in_num
= elem
->in_num
;
694 data
.out_num
= elem
->out_num
;
696 for (i
= 0; i
< elem
->in_num
; i
++) {
697 data
.in_addr
[i
] = elem
->in_addr
[i
];
700 for (i
= 0; i
< elem
->out_num
; i
++) {
701 data
.out_addr
[i
] = elem
->out_addr
[i
];
704 for (i
= 0; i
< elem
->in_num
; i
++) {
705 /* Base is overwritten by virtqueue_map when loading. Do not
706 * save it, as it would leak the QEMU address space layout. */
707 data
.in_sg
[i
].iov_len
= elem
->in_sg
[i
].iov_len
;
710 for (i
= 0; i
< elem
->out_num
; i
++) {
711 /* Do not save iov_base as above. */
712 data
.out_sg
[i
].iov_len
= elem
->out_sg
[i
].iov_len
;
714 qemu_put_buffer(f
, (uint8_t *)&data
, sizeof(VirtQueueElementOld
));
718 static void virtio_notify_vector(VirtIODevice
*vdev
, uint16_t vector
)
720 BusState
*qbus
= qdev_get_parent_bus(DEVICE(vdev
));
721 VirtioBusClass
*k
= VIRTIO_BUS_GET_CLASS(qbus
);
724 k
->notify(qbus
->parent
, vector
);
728 void virtio_update_irq(VirtIODevice
*vdev
)
730 virtio_notify_vector(vdev
, VIRTIO_NO_VECTOR
);
733 static int virtio_validate_features(VirtIODevice
*vdev
)
735 VirtioDeviceClass
*k
= VIRTIO_DEVICE_GET_CLASS(vdev
);
737 if (k
->validate_features
) {
738 return k
->validate_features(vdev
);
744 int virtio_set_status(VirtIODevice
*vdev
, uint8_t val
)
746 VirtioDeviceClass
*k
= VIRTIO_DEVICE_GET_CLASS(vdev
);
747 trace_virtio_set_status(vdev
, val
);
749 if (virtio_vdev_has_feature(vdev
, VIRTIO_F_VERSION_1
)) {
750 if (!(vdev
->status
& VIRTIO_CONFIG_S_FEATURES_OK
) &&
751 val
& VIRTIO_CONFIG_S_FEATURES_OK
) {
752 int ret
= virtio_validate_features(vdev
);
760 k
->set_status(vdev
, val
);
766 bool target_words_bigendian(void);
767 static enum virtio_device_endian
virtio_default_endian(void)
769 if (target_words_bigendian()) {
770 return VIRTIO_DEVICE_ENDIAN_BIG
;
772 return VIRTIO_DEVICE_ENDIAN_LITTLE
;
776 static enum virtio_device_endian
virtio_current_cpu_endian(void)
778 CPUClass
*cc
= CPU_GET_CLASS(current_cpu
);
780 if (cc
->virtio_is_big_endian(current_cpu
)) {
781 return VIRTIO_DEVICE_ENDIAN_BIG
;
783 return VIRTIO_DEVICE_ENDIAN_LITTLE
;
787 void virtio_reset(void *opaque
)
789 VirtIODevice
*vdev
= opaque
;
790 VirtioDeviceClass
*k
= VIRTIO_DEVICE_GET_CLASS(vdev
);
793 virtio_set_status(vdev
, 0);
795 /* Guest initiated reset */
796 vdev
->device_endian
= virtio_current_cpu_endian();
799 vdev
->device_endian
= virtio_default_endian();
806 vdev
->guest_features
= 0;
810 vdev
->config_vector
= VIRTIO_NO_VECTOR
;
811 virtio_notify_vector(vdev
, vdev
->config_vector
);
813 for(i
= 0; i
< VIRTIO_QUEUE_MAX
; i
++) {
814 vdev
->vq
[i
].vring
.desc
= 0;
815 vdev
->vq
[i
].vring
.avail
= 0;
816 vdev
->vq
[i
].vring
.used
= 0;
817 vdev
->vq
[i
].last_avail_idx
= 0;
818 vdev
->vq
[i
].shadow_avail_idx
= 0;
819 vdev
->vq
[i
].used_idx
= 0;
820 virtio_queue_set_vector(vdev
, i
, VIRTIO_NO_VECTOR
);
821 vdev
->vq
[i
].signalled_used
= 0;
822 vdev
->vq
[i
].signalled_used_valid
= false;
823 vdev
->vq
[i
].notification
= true;
824 vdev
->vq
[i
].vring
.num
= vdev
->vq
[i
].vring
.num_default
;
828 uint32_t virtio_config_readb(VirtIODevice
*vdev
, uint32_t addr
)
830 VirtioDeviceClass
*k
= VIRTIO_DEVICE_GET_CLASS(vdev
);
833 if (addr
+ sizeof(val
) > vdev
->config_len
) {
837 k
->get_config(vdev
, vdev
->config
);
839 val
= ldub_p(vdev
->config
+ addr
);
843 uint32_t virtio_config_readw(VirtIODevice
*vdev
, uint32_t addr
)
845 VirtioDeviceClass
*k
= VIRTIO_DEVICE_GET_CLASS(vdev
);
848 if (addr
+ sizeof(val
) > vdev
->config_len
) {
852 k
->get_config(vdev
, vdev
->config
);
854 val
= lduw_p(vdev
->config
+ addr
);
858 uint32_t virtio_config_readl(VirtIODevice
*vdev
, uint32_t addr
)
860 VirtioDeviceClass
*k
= VIRTIO_DEVICE_GET_CLASS(vdev
);
863 if (addr
+ sizeof(val
) > vdev
->config_len
) {
867 k
->get_config(vdev
, vdev
->config
);
869 val
= ldl_p(vdev
->config
+ addr
);
873 void virtio_config_writeb(VirtIODevice
*vdev
, uint32_t addr
, uint32_t data
)
875 VirtioDeviceClass
*k
= VIRTIO_DEVICE_GET_CLASS(vdev
);
878 if (addr
+ sizeof(val
) > vdev
->config_len
) {
882 stb_p(vdev
->config
+ addr
, val
);
885 k
->set_config(vdev
, vdev
->config
);
889 void virtio_config_writew(VirtIODevice
*vdev
, uint32_t addr
, uint32_t data
)
891 VirtioDeviceClass
*k
= VIRTIO_DEVICE_GET_CLASS(vdev
);
894 if (addr
+ sizeof(val
) > vdev
->config_len
) {
898 stw_p(vdev
->config
+ addr
, val
);
901 k
->set_config(vdev
, vdev
->config
);
905 void virtio_config_writel(VirtIODevice
*vdev
, uint32_t addr
, uint32_t data
)
907 VirtioDeviceClass
*k
= VIRTIO_DEVICE_GET_CLASS(vdev
);
910 if (addr
+ sizeof(val
) > vdev
->config_len
) {
914 stl_p(vdev
->config
+ addr
, val
);
917 k
->set_config(vdev
, vdev
->config
);
921 uint32_t virtio_config_modern_readb(VirtIODevice
*vdev
, uint32_t addr
)
923 VirtioDeviceClass
*k
= VIRTIO_DEVICE_GET_CLASS(vdev
);
926 if (addr
+ sizeof(val
) > vdev
->config_len
) {
930 k
->get_config(vdev
, vdev
->config
);
932 val
= ldub_p(vdev
->config
+ addr
);
936 uint32_t virtio_config_modern_readw(VirtIODevice
*vdev
, uint32_t addr
)
938 VirtioDeviceClass
*k
= VIRTIO_DEVICE_GET_CLASS(vdev
);
941 if (addr
+ sizeof(val
) > vdev
->config_len
) {
945 k
->get_config(vdev
, vdev
->config
);
947 val
= lduw_le_p(vdev
->config
+ addr
);
951 uint32_t virtio_config_modern_readl(VirtIODevice
*vdev
, uint32_t addr
)
953 VirtioDeviceClass
*k
= VIRTIO_DEVICE_GET_CLASS(vdev
);
956 if (addr
+ sizeof(val
) > vdev
->config_len
) {
960 k
->get_config(vdev
, vdev
->config
);
962 val
= ldl_le_p(vdev
->config
+ addr
);
966 void virtio_config_modern_writeb(VirtIODevice
*vdev
,
967 uint32_t addr
, uint32_t data
)
969 VirtioDeviceClass
*k
= VIRTIO_DEVICE_GET_CLASS(vdev
);
972 if (addr
+ sizeof(val
) > vdev
->config_len
) {
976 stb_p(vdev
->config
+ addr
, val
);
979 k
->set_config(vdev
, vdev
->config
);
983 void virtio_config_modern_writew(VirtIODevice
*vdev
,
984 uint32_t addr
, uint32_t data
)
986 VirtioDeviceClass
*k
= VIRTIO_DEVICE_GET_CLASS(vdev
);
989 if (addr
+ sizeof(val
) > vdev
->config_len
) {
993 stw_le_p(vdev
->config
+ addr
, val
);
996 k
->set_config(vdev
, vdev
->config
);
1000 void virtio_config_modern_writel(VirtIODevice
*vdev
,
1001 uint32_t addr
, uint32_t data
)
1003 VirtioDeviceClass
*k
= VIRTIO_DEVICE_GET_CLASS(vdev
);
1004 uint32_t val
= data
;
1006 if (addr
+ sizeof(val
) > vdev
->config_len
) {
1010 stl_le_p(vdev
->config
+ addr
, val
);
1012 if (k
->set_config
) {
1013 k
->set_config(vdev
, vdev
->config
);
1017 void virtio_queue_set_addr(VirtIODevice
*vdev
, int n
, hwaddr addr
)
1019 vdev
->vq
[n
].vring
.desc
= addr
;
1020 virtio_queue_update_rings(vdev
, n
);
1023 hwaddr
virtio_queue_get_addr(VirtIODevice
*vdev
, int n
)
1025 return vdev
->vq
[n
].vring
.desc
;
1028 void virtio_queue_set_rings(VirtIODevice
*vdev
, int n
, hwaddr desc
,
1029 hwaddr avail
, hwaddr used
)
1031 vdev
->vq
[n
].vring
.desc
= desc
;
1032 vdev
->vq
[n
].vring
.avail
= avail
;
1033 vdev
->vq
[n
].vring
.used
= used
;
1036 void virtio_queue_set_num(VirtIODevice
*vdev
, int n
, int num
)
1038 /* Don't allow guest to flip queue between existent and
1039 * nonexistent states, or to set it to an invalid size.
1041 if (!!num
!= !!vdev
->vq
[n
].vring
.num
||
1042 num
> VIRTQUEUE_MAX_SIZE
||
1046 vdev
->vq
[n
].vring
.num
= num
;
1049 VirtQueue
*virtio_vector_first_queue(VirtIODevice
*vdev
, uint16_t vector
)
1051 return QLIST_FIRST(&vdev
->vector_queues
[vector
]);
1054 VirtQueue
*virtio_vector_next_queue(VirtQueue
*vq
)
1056 return QLIST_NEXT(vq
, node
);
1059 int virtio_queue_get_num(VirtIODevice
*vdev
, int n
)
1061 return vdev
->vq
[n
].vring
.num
;
1064 int virtio_get_num_queues(VirtIODevice
*vdev
)
1068 for (i
= 0; i
< VIRTIO_QUEUE_MAX
; i
++) {
1069 if (!virtio_queue_get_num(vdev
, i
)) {
1077 void virtio_queue_set_align(VirtIODevice
*vdev
, int n
, int align
)
1079 BusState
*qbus
= qdev_get_parent_bus(DEVICE(vdev
));
1080 VirtioBusClass
*k
= VIRTIO_BUS_GET_CLASS(qbus
);
1082 /* virtio-1 compliant devices cannot change the alignment */
1083 if (virtio_vdev_has_feature(vdev
, VIRTIO_F_VERSION_1
)) {
1084 error_report("tried to modify queue alignment for virtio-1 device");
1087 /* Check that the transport told us it was going to do this
1088 * (so a buggy transport will immediately assert rather than
1089 * silently failing to migrate this state)
1091 assert(k
->has_variable_vring_alignment
);
1093 vdev
->vq
[n
].vring
.align
= align
;
1094 virtio_queue_update_rings(vdev
, n
);
1097 static void virtio_queue_notify_aio_vq(VirtQueue
*vq
)
1099 if (vq
->vring
.desc
&& vq
->handle_aio_output
) {
1100 VirtIODevice
*vdev
= vq
->vdev
;
1102 trace_virtio_queue_notify(vdev
, vq
- vdev
->vq
, vq
);
1103 vq
->handle_aio_output(vdev
, vq
);
1107 static void virtio_queue_notify_vq(VirtQueue
*vq
)
1109 if (vq
->vring
.desc
&& vq
->handle_output
) {
1110 VirtIODevice
*vdev
= vq
->vdev
;
1112 trace_virtio_queue_notify(vdev
, vq
- vdev
->vq
, vq
);
1113 vq
->handle_output(vdev
, vq
);
1117 void virtio_queue_notify(VirtIODevice
*vdev
, int n
)
1119 virtio_queue_notify_vq(&vdev
->vq
[n
]);
1122 uint16_t virtio_queue_vector(VirtIODevice
*vdev
, int n
)
1124 return n
< VIRTIO_QUEUE_MAX
? vdev
->vq
[n
].vector
:
1128 void virtio_queue_set_vector(VirtIODevice
*vdev
, int n
, uint16_t vector
)
1130 VirtQueue
*vq
= &vdev
->vq
[n
];
1132 if (n
< VIRTIO_QUEUE_MAX
) {
1133 if (vdev
->vector_queues
&&
1134 vdev
->vq
[n
].vector
!= VIRTIO_NO_VECTOR
) {
1135 QLIST_REMOVE(vq
, node
);
1137 vdev
->vq
[n
].vector
= vector
;
1138 if (vdev
->vector_queues
&&
1139 vector
!= VIRTIO_NO_VECTOR
) {
1140 QLIST_INSERT_HEAD(&vdev
->vector_queues
[vector
], vq
, node
);
1145 static VirtQueue
*virtio_add_queue_internal(VirtIODevice
*vdev
, int queue_size
,
1146 VirtIOHandleOutput handle_output
,
1151 for (i
= 0; i
< VIRTIO_QUEUE_MAX
; i
++) {
1152 if (vdev
->vq
[i
].vring
.num
== 0)
1156 if (i
== VIRTIO_QUEUE_MAX
|| queue_size
> VIRTQUEUE_MAX_SIZE
)
1159 vdev
->vq
[i
].vring
.num
= queue_size
;
1160 vdev
->vq
[i
].vring
.num_default
= queue_size
;
1161 vdev
->vq
[i
].vring
.align
= VIRTIO_PCI_VRING_ALIGN
;
1162 vdev
->vq
[i
].handle_output
= handle_output
;
1163 vdev
->vq
[i
].handle_aio_output
= NULL
;
1164 vdev
->vq
[i
].use_aio
= use_aio
;
1166 return &vdev
->vq
[i
];
1169 /* Add a virt queue and mark AIO.
1170 * An AIO queue will use the AioContext based event interface instead of the
1171 * default IOHandler and EventNotifier interface.
1173 VirtQueue
*virtio_add_queue_aio(VirtIODevice
*vdev
, int queue_size
,
1174 VirtIOHandleOutput handle_output
)
1176 return virtio_add_queue_internal(vdev
, queue_size
, handle_output
, true);
1179 /* Add a normal virt queue (on the contrary to the AIO version above. */
1180 VirtQueue
*virtio_add_queue(VirtIODevice
*vdev
, int queue_size
,
1181 VirtIOHandleOutput handle_output
)
1183 return virtio_add_queue_internal(vdev
, queue_size
, handle_output
, false);
1186 void virtio_del_queue(VirtIODevice
*vdev
, int n
)
1188 if (n
< 0 || n
>= VIRTIO_QUEUE_MAX
) {
1192 vdev
->vq
[n
].vring
.num
= 0;
1193 vdev
->vq
[n
].vring
.num_default
= 0;
1196 void virtio_irq(VirtQueue
*vq
)
1198 trace_virtio_irq(vq
);
1199 vq
->vdev
->isr
|= 0x01;
1200 virtio_notify_vector(vq
->vdev
, vq
->vector
);
1203 bool virtio_should_notify(VirtIODevice
*vdev
, VirtQueue
*vq
)
1207 /* We need to expose used array entries before checking used event. */
1209 /* Always notify when queue is empty (when feature acknowledge) */
1210 if (virtio_vdev_has_feature(vdev
, VIRTIO_F_NOTIFY_ON_EMPTY
) &&
1211 !vq
->inuse
&& virtio_queue_empty(vq
)) {
1215 if (!virtio_vdev_has_feature(vdev
, VIRTIO_RING_F_EVENT_IDX
)) {
1216 return !(vring_avail_flags(vq
) & VRING_AVAIL_F_NO_INTERRUPT
);
1219 v
= vq
->signalled_used_valid
;
1220 vq
->signalled_used_valid
= true;
1221 old
= vq
->signalled_used
;
1222 new = vq
->signalled_used
= vq
->used_idx
;
1223 return !v
|| vring_need_event(vring_get_used_event(vq
), new, old
);
1226 void virtio_notify(VirtIODevice
*vdev
, VirtQueue
*vq
)
1228 if (!virtio_should_notify(vdev
, vq
)) {
1232 trace_virtio_notify(vdev
, vq
);
1234 virtio_notify_vector(vdev
, vq
->vector
);
1237 void virtio_notify_config(VirtIODevice
*vdev
)
1239 if (!(vdev
->status
& VIRTIO_CONFIG_S_DRIVER_OK
))
1244 virtio_notify_vector(vdev
, vdev
->config_vector
);
1247 static bool virtio_device_endian_needed(void *opaque
)
1249 VirtIODevice
*vdev
= opaque
;
1251 assert(vdev
->device_endian
!= VIRTIO_DEVICE_ENDIAN_UNKNOWN
);
1252 if (!virtio_vdev_has_feature(vdev
, VIRTIO_F_VERSION_1
)) {
1253 return vdev
->device_endian
!= virtio_default_endian();
1255 /* Devices conforming to VIRTIO 1.0 or later are always LE. */
1256 return vdev
->device_endian
!= VIRTIO_DEVICE_ENDIAN_LITTLE
;
1259 static bool virtio_64bit_features_needed(void *opaque
)
1261 VirtIODevice
*vdev
= opaque
;
1263 return (vdev
->host_features
>> 32) != 0;
1266 static bool virtio_virtqueue_needed(void *opaque
)
1268 VirtIODevice
*vdev
= opaque
;
1270 return virtio_host_has_feature(vdev
, VIRTIO_F_VERSION_1
);
1273 static bool virtio_ringsize_needed(void *opaque
)
1275 VirtIODevice
*vdev
= opaque
;
1278 for (i
= 0; i
< VIRTIO_QUEUE_MAX
; i
++) {
1279 if (vdev
->vq
[i
].vring
.num
!= vdev
->vq
[i
].vring
.num_default
) {
1286 static bool virtio_extra_state_needed(void *opaque
)
1288 VirtIODevice
*vdev
= opaque
;
1289 BusState
*qbus
= qdev_get_parent_bus(DEVICE(vdev
));
1290 VirtioBusClass
*k
= VIRTIO_BUS_GET_CLASS(qbus
);
1292 return k
->has_extra_state
&&
1293 k
->has_extra_state(qbus
->parent
);
1296 static const VMStateDescription vmstate_virtqueue
= {
1297 .name
= "virtqueue_state",
1299 .minimum_version_id
= 1,
1300 .fields
= (VMStateField
[]) {
1301 VMSTATE_UINT64(vring
.avail
, struct VirtQueue
),
1302 VMSTATE_UINT64(vring
.used
, struct VirtQueue
),
1303 VMSTATE_END_OF_LIST()
1307 static const VMStateDescription vmstate_virtio_virtqueues
= {
1308 .name
= "virtio/virtqueues",
1310 .minimum_version_id
= 1,
1311 .needed
= &virtio_virtqueue_needed
,
1312 .fields
= (VMStateField
[]) {
1313 VMSTATE_STRUCT_VARRAY_POINTER_KNOWN(vq
, struct VirtIODevice
,
1314 VIRTIO_QUEUE_MAX
, 0, vmstate_virtqueue
, VirtQueue
),
1315 VMSTATE_END_OF_LIST()
1319 static const VMStateDescription vmstate_ringsize
= {
1320 .name
= "ringsize_state",
1322 .minimum_version_id
= 1,
1323 .fields
= (VMStateField
[]) {
1324 VMSTATE_UINT32(vring
.num_default
, struct VirtQueue
),
1325 VMSTATE_END_OF_LIST()
1329 static const VMStateDescription vmstate_virtio_ringsize
= {
1330 .name
= "virtio/ringsize",
1332 .minimum_version_id
= 1,
1333 .needed
= &virtio_ringsize_needed
,
1334 .fields
= (VMStateField
[]) {
1335 VMSTATE_STRUCT_VARRAY_POINTER_KNOWN(vq
, struct VirtIODevice
,
1336 VIRTIO_QUEUE_MAX
, 0, vmstate_ringsize
, VirtQueue
),
1337 VMSTATE_END_OF_LIST()
1341 static int get_extra_state(QEMUFile
*f
, void *pv
, size_t size
)
1343 VirtIODevice
*vdev
= pv
;
1344 BusState
*qbus
= qdev_get_parent_bus(DEVICE(vdev
));
1345 VirtioBusClass
*k
= VIRTIO_BUS_GET_CLASS(qbus
);
1347 if (!k
->load_extra_state
) {
1350 return k
->load_extra_state(qbus
->parent
, f
);
1354 static void put_extra_state(QEMUFile
*f
, void *pv
, size_t size
)
1356 VirtIODevice
*vdev
= pv
;
1357 BusState
*qbus
= qdev_get_parent_bus(DEVICE(vdev
));
1358 VirtioBusClass
*k
= VIRTIO_BUS_GET_CLASS(qbus
);
1360 k
->save_extra_state(qbus
->parent
, f
);
1363 static const VMStateInfo vmstate_info_extra_state
= {
1364 .name
= "virtqueue_extra_state",
1365 .get
= get_extra_state
,
1366 .put
= put_extra_state
,
1369 static const VMStateDescription vmstate_virtio_extra_state
= {
1370 .name
= "virtio/extra_state",
1372 .minimum_version_id
= 1,
1373 .needed
= &virtio_extra_state_needed
,
1374 .fields
= (VMStateField
[]) {
1376 .name
= "extra_state",
1378 .field_exists
= NULL
,
1380 .info
= &vmstate_info_extra_state
,
1381 .flags
= VMS_SINGLE
,
1384 VMSTATE_END_OF_LIST()
1388 static const VMStateDescription vmstate_virtio_device_endian
= {
1389 .name
= "virtio/device_endian",
1391 .minimum_version_id
= 1,
1392 .needed
= &virtio_device_endian_needed
,
1393 .fields
= (VMStateField
[]) {
1394 VMSTATE_UINT8(device_endian
, VirtIODevice
),
1395 VMSTATE_END_OF_LIST()
1399 static const VMStateDescription vmstate_virtio_64bit_features
= {
1400 .name
= "virtio/64bit_features",
1402 .minimum_version_id
= 1,
1403 .needed
= &virtio_64bit_features_needed
,
1404 .fields
= (VMStateField
[]) {
1405 VMSTATE_UINT64(guest_features
, VirtIODevice
),
1406 VMSTATE_END_OF_LIST()
1410 static const VMStateDescription vmstate_virtio
= {
1413 .minimum_version_id
= 1,
1414 .minimum_version_id_old
= 1,
1415 .fields
= (VMStateField
[]) {
1416 VMSTATE_END_OF_LIST()
1418 .subsections
= (const VMStateDescription
*[]) {
1419 &vmstate_virtio_device_endian
,
1420 &vmstate_virtio_64bit_features
,
1421 &vmstate_virtio_virtqueues
,
1422 &vmstate_virtio_ringsize
,
1423 &vmstate_virtio_extra_state
,
1428 void virtio_save(VirtIODevice
*vdev
, QEMUFile
*f
)
1430 BusState
*qbus
= qdev_get_parent_bus(DEVICE(vdev
));
1431 VirtioBusClass
*k
= VIRTIO_BUS_GET_CLASS(qbus
);
1432 VirtioDeviceClass
*vdc
= VIRTIO_DEVICE_GET_CLASS(vdev
);
1433 uint32_t guest_features_lo
= (vdev
->guest_features
& 0xffffffff);
1436 if (k
->save_config
) {
1437 k
->save_config(qbus
->parent
, f
);
1440 qemu_put_8s(f
, &vdev
->status
);
1441 qemu_put_8s(f
, &vdev
->isr
);
1442 qemu_put_be16s(f
, &vdev
->queue_sel
);
1443 qemu_put_be32s(f
, &guest_features_lo
);
1444 qemu_put_be32(f
, vdev
->config_len
);
1445 qemu_put_buffer(f
, vdev
->config
, vdev
->config_len
);
1447 for (i
= 0; i
< VIRTIO_QUEUE_MAX
; i
++) {
1448 if (vdev
->vq
[i
].vring
.num
== 0)
1452 qemu_put_be32(f
, i
);
1454 for (i
= 0; i
< VIRTIO_QUEUE_MAX
; i
++) {
1455 if (vdev
->vq
[i
].vring
.num
== 0)
1458 qemu_put_be32(f
, vdev
->vq
[i
].vring
.num
);
1459 if (k
->has_variable_vring_alignment
) {
1460 qemu_put_be32(f
, vdev
->vq
[i
].vring
.align
);
1462 /* XXX virtio-1 devices */
1463 qemu_put_be64(f
, vdev
->vq
[i
].vring
.desc
);
1464 qemu_put_be16s(f
, &vdev
->vq
[i
].last_avail_idx
);
1465 if (k
->save_queue
) {
1466 k
->save_queue(qbus
->parent
, i
, f
);
1470 if (vdc
->save
!= NULL
) {
1475 vmstate_save_state(f
, &vmstate_virtio
, vdev
, NULL
);
1478 /* A wrapper for use as a VMState .put function */
1479 void virtio_vmstate_save(QEMUFile
*f
, void *opaque
, size_t size
)
1481 virtio_save(VIRTIO_DEVICE(opaque
), f
);
1484 static int virtio_set_features_nocheck(VirtIODevice
*vdev
, uint64_t val
)
1486 VirtioDeviceClass
*k
= VIRTIO_DEVICE_GET_CLASS(vdev
);
1487 bool bad
= (val
& ~(vdev
->host_features
)) != 0;
1489 val
&= vdev
->host_features
;
1490 if (k
->set_features
) {
1491 k
->set_features(vdev
, val
);
1493 vdev
->guest_features
= val
;
1494 return bad
? -1 : 0;
1497 int virtio_set_features(VirtIODevice
*vdev
, uint64_t val
)
1500 * The driver must not attempt to set features after feature negotiation
1503 if (vdev
->status
& VIRTIO_CONFIG_S_FEATURES_OK
) {
1506 return virtio_set_features_nocheck(vdev
, val
);
1509 int virtio_load(VirtIODevice
*vdev
, QEMUFile
*f
, int version_id
)
1515 BusState
*qbus
= qdev_get_parent_bus(DEVICE(vdev
));
1516 VirtioBusClass
*k
= VIRTIO_BUS_GET_CLASS(qbus
);
1517 VirtioDeviceClass
*vdc
= VIRTIO_DEVICE_GET_CLASS(vdev
);
1520 * We poison the endianness to ensure it does not get used before
1521 * subsections have been loaded.
1523 vdev
->device_endian
= VIRTIO_DEVICE_ENDIAN_UNKNOWN
;
1525 if (k
->load_config
) {
1526 ret
= k
->load_config(qbus
->parent
, f
);
1531 qemu_get_8s(f
, &vdev
->status
);
1532 qemu_get_8s(f
, &vdev
->isr
);
1533 qemu_get_be16s(f
, &vdev
->queue_sel
);
1534 if (vdev
->queue_sel
>= VIRTIO_QUEUE_MAX
) {
1537 qemu_get_be32s(f
, &features
);
1540 * Temporarily set guest_features low bits - needed by
1541 * virtio net load code testing for VIRTIO_NET_F_CTRL_GUEST_OFFLOADS
1542 * VIRTIO_NET_F_GUEST_ANNOUNCE and VIRTIO_NET_F_CTRL_VQ.
1544 * Note: devices should always test host features in future - don't create
1545 * new dependencies like this.
1547 vdev
->guest_features
= features
;
1549 config_len
= qemu_get_be32(f
);
1552 * There are cases where the incoming config can be bigger or smaller
1553 * than what we have; so load what we have space for, and skip
1554 * any excess that's in the stream.
1556 qemu_get_buffer(f
, vdev
->config
, MIN(config_len
, vdev
->config_len
));
1558 while (config_len
> vdev
->config_len
) {
1563 num
= qemu_get_be32(f
);
1565 if (num
> VIRTIO_QUEUE_MAX
) {
1566 error_report("Invalid number of virtqueues: 0x%x", num
);
1570 for (i
= 0; i
< num
; i
++) {
1571 vdev
->vq
[i
].vring
.num
= qemu_get_be32(f
);
1572 if (k
->has_variable_vring_alignment
) {
1573 vdev
->vq
[i
].vring
.align
= qemu_get_be32(f
);
1575 vdev
->vq
[i
].vring
.desc
= qemu_get_be64(f
);
1576 qemu_get_be16s(f
, &vdev
->vq
[i
].last_avail_idx
);
1577 vdev
->vq
[i
].signalled_used_valid
= false;
1578 vdev
->vq
[i
].notification
= true;
1580 if (vdev
->vq
[i
].vring
.desc
) {
1581 /* XXX virtio-1 devices */
1582 virtio_queue_update_rings(vdev
, i
);
1583 } else if (vdev
->vq
[i
].last_avail_idx
) {
1584 error_report("VQ %d address 0x0 "
1585 "inconsistent with Host index 0x%x",
1586 i
, vdev
->vq
[i
].last_avail_idx
);
1589 if (k
->load_queue
) {
1590 ret
= k
->load_queue(qbus
->parent
, i
, f
);
1596 virtio_notify_vector(vdev
, VIRTIO_NO_VECTOR
);
1598 if (vdc
->load
!= NULL
) {
1599 ret
= vdc
->load(vdev
, f
, version_id
);
1606 ret
= vmstate_load_state(f
, &vmstate_virtio
, vdev
, 1);
1611 if (vdev
->device_endian
== VIRTIO_DEVICE_ENDIAN_UNKNOWN
) {
1612 vdev
->device_endian
= virtio_default_endian();
1615 if (virtio_64bit_features_needed(vdev
)) {
1617 * Subsection load filled vdev->guest_features. Run them
1618 * through virtio_set_features to sanity-check them against
1621 uint64_t features64
= vdev
->guest_features
;
1622 if (virtio_set_features_nocheck(vdev
, features64
) < 0) {
1623 error_report("Features 0x%" PRIx64
" unsupported. "
1624 "Allowed features: 0x%" PRIx64
,
1625 features64
, vdev
->host_features
);
1629 if (virtio_set_features_nocheck(vdev
, features
) < 0) {
1630 error_report("Features 0x%x unsupported. "
1631 "Allowed features: 0x%" PRIx64
,
1632 features
, vdev
->host_features
);
1637 for (i
= 0; i
< num
; i
++) {
1638 if (vdev
->vq
[i
].vring
.desc
) {
1640 nheads
= vring_avail_idx(&vdev
->vq
[i
]) - vdev
->vq
[i
].last_avail_idx
;
1641 /* Check it isn't doing strange things with descriptor numbers. */
1642 if (nheads
> vdev
->vq
[i
].vring
.num
) {
1643 error_report("VQ %d size 0x%x Guest index 0x%x "
1644 "inconsistent with Host index 0x%x: delta 0x%x",
1645 i
, vdev
->vq
[i
].vring
.num
,
1646 vring_avail_idx(&vdev
->vq
[i
]),
1647 vdev
->vq
[i
].last_avail_idx
, nheads
);
1650 vdev
->vq
[i
].used_idx
= vring_used_idx(&vdev
->vq
[i
]);
1651 vdev
->vq
[i
].shadow_avail_idx
= vring_avail_idx(&vdev
->vq
[i
]);
1654 * Some devices migrate VirtQueueElements that have been popped
1655 * from the avail ring but not yet returned to the used ring.
1657 vdev
->vq
[i
].inuse
= vdev
->vq
[i
].last_avail_idx
-
1658 vdev
->vq
[i
].used_idx
;
1659 if (vdev
->vq
[i
].inuse
> vdev
->vq
[i
].vring
.num
) {
1660 error_report("VQ %d size 0x%x < last_avail_idx 0x%x - "
1662 i
, vdev
->vq
[i
].vring
.num
,
1663 vdev
->vq
[i
].last_avail_idx
,
1664 vdev
->vq
[i
].used_idx
);
1673 void virtio_cleanup(VirtIODevice
*vdev
)
1675 qemu_del_vm_change_state_handler(vdev
->vmstate
);
1676 g_free(vdev
->config
);
1678 g_free(vdev
->vector_queues
);
1681 static void virtio_vmstate_change(void *opaque
, int running
, RunState state
)
1683 VirtIODevice
*vdev
= opaque
;
1684 BusState
*qbus
= qdev_get_parent_bus(DEVICE(vdev
));
1685 VirtioBusClass
*k
= VIRTIO_BUS_GET_CLASS(qbus
);
1686 bool backend_run
= running
&& (vdev
->status
& VIRTIO_CONFIG_S_DRIVER_OK
);
1687 vdev
->vm_running
= running
;
1690 virtio_set_status(vdev
, vdev
->status
);
1693 if (k
->vmstate_change
) {
1694 k
->vmstate_change(qbus
->parent
, backend_run
);
1698 virtio_set_status(vdev
, vdev
->status
);
1702 void virtio_instance_init_common(Object
*proxy_obj
, void *data
,
1703 size_t vdev_size
, const char *vdev_name
)
1705 DeviceState
*vdev
= data
;
1707 object_initialize(vdev
, vdev_size
, vdev_name
);
1708 object_property_add_child(proxy_obj
, "virtio-backend", OBJECT(vdev
), NULL
);
1709 object_unref(OBJECT(vdev
));
1710 qdev_alias_all_properties(vdev
, proxy_obj
);
1713 void virtio_init(VirtIODevice
*vdev
, const char *name
,
1714 uint16_t device_id
, size_t config_size
)
1716 BusState
*qbus
= qdev_get_parent_bus(DEVICE(vdev
));
1717 VirtioBusClass
*k
= VIRTIO_BUS_GET_CLASS(qbus
);
1719 int nvectors
= k
->query_nvectors
? k
->query_nvectors(qbus
->parent
) : 0;
1722 vdev
->vector_queues
=
1723 g_malloc0(sizeof(*vdev
->vector_queues
) * nvectors
);
1726 vdev
->device_id
= device_id
;
1729 vdev
->queue_sel
= 0;
1730 vdev
->config_vector
= VIRTIO_NO_VECTOR
;
1731 vdev
->vq
= g_malloc0(sizeof(VirtQueue
) * VIRTIO_QUEUE_MAX
);
1732 vdev
->vm_running
= runstate_is_running();
1733 for (i
= 0; i
< VIRTIO_QUEUE_MAX
; i
++) {
1734 vdev
->vq
[i
].vector
= VIRTIO_NO_VECTOR
;
1735 vdev
->vq
[i
].vdev
= vdev
;
1736 vdev
->vq
[i
].queue_index
= i
;
1740 vdev
->config_len
= config_size
;
1741 if (vdev
->config_len
) {
1742 vdev
->config
= g_malloc0(config_size
);
1744 vdev
->config
= NULL
;
1746 vdev
->vmstate
= qemu_add_vm_change_state_handler(virtio_vmstate_change
,
1748 vdev
->device_endian
= virtio_default_endian();
1749 vdev
->use_guest_notifier_mask
= true;
1752 hwaddr
virtio_queue_get_desc_addr(VirtIODevice
*vdev
, int n
)
1754 return vdev
->vq
[n
].vring
.desc
;
1757 hwaddr
virtio_queue_get_avail_addr(VirtIODevice
*vdev
, int n
)
1759 return vdev
->vq
[n
].vring
.avail
;
1762 hwaddr
virtio_queue_get_used_addr(VirtIODevice
*vdev
, int n
)
1764 return vdev
->vq
[n
].vring
.used
;
1767 hwaddr
virtio_queue_get_ring_addr(VirtIODevice
*vdev
, int n
)
1769 return vdev
->vq
[n
].vring
.desc
;
1772 hwaddr
virtio_queue_get_desc_size(VirtIODevice
*vdev
, int n
)
1774 return sizeof(VRingDesc
) * vdev
->vq
[n
].vring
.num
;
1777 hwaddr
virtio_queue_get_avail_size(VirtIODevice
*vdev
, int n
)
1779 return offsetof(VRingAvail
, ring
) +
1780 sizeof(uint16_t) * vdev
->vq
[n
].vring
.num
;
1783 hwaddr
virtio_queue_get_used_size(VirtIODevice
*vdev
, int n
)
1785 return offsetof(VRingUsed
, ring
) +
1786 sizeof(VRingUsedElem
) * vdev
->vq
[n
].vring
.num
;
1789 hwaddr
virtio_queue_get_ring_size(VirtIODevice
*vdev
, int n
)
1791 return vdev
->vq
[n
].vring
.used
- vdev
->vq
[n
].vring
.desc
+
1792 virtio_queue_get_used_size(vdev
, n
);
1795 uint16_t virtio_queue_get_last_avail_idx(VirtIODevice
*vdev
, int n
)
1797 return vdev
->vq
[n
].last_avail_idx
;
1800 void virtio_queue_set_last_avail_idx(VirtIODevice
*vdev
, int n
, uint16_t idx
)
1802 vdev
->vq
[n
].last_avail_idx
= idx
;
1803 vdev
->vq
[n
].shadow_avail_idx
= idx
;
1806 void virtio_queue_invalidate_signalled_used(VirtIODevice
*vdev
, int n
)
1808 vdev
->vq
[n
].signalled_used_valid
= false;
1811 VirtQueue
*virtio_get_queue(VirtIODevice
*vdev
, int n
)
1813 return vdev
->vq
+ n
;
1816 uint16_t virtio_get_queue_index(VirtQueue
*vq
)
1818 return vq
->queue_index
;
1821 static void virtio_queue_guest_notifier_read(EventNotifier
*n
)
1823 VirtQueue
*vq
= container_of(n
, VirtQueue
, guest_notifier
);
1824 if (event_notifier_test_and_clear(n
)) {
1829 void virtio_queue_set_guest_notifier_fd_handler(VirtQueue
*vq
, bool assign
,
1832 if (assign
&& !with_irqfd
) {
1833 event_notifier_set_handler(&vq
->guest_notifier
, false,
1834 virtio_queue_guest_notifier_read
);
1836 event_notifier_set_handler(&vq
->guest_notifier
, false, NULL
);
1839 /* Test and clear notifier before closing it,
1840 * in case poll callback didn't have time to run. */
1841 virtio_queue_guest_notifier_read(&vq
->guest_notifier
);
1845 EventNotifier
*virtio_queue_get_guest_notifier(VirtQueue
*vq
)
1847 return &vq
->guest_notifier
;
1850 static void virtio_queue_host_notifier_aio_read(EventNotifier
*n
)
1852 VirtQueue
*vq
= container_of(n
, VirtQueue
, host_notifier
);
1853 if (event_notifier_test_and_clear(n
)) {
1854 virtio_queue_notify_aio_vq(vq
);
1858 void virtio_queue_aio_set_host_notifier_handler(VirtQueue
*vq
, AioContext
*ctx
,
1859 VirtIOHandleOutput handle_output
)
1861 if (handle_output
) {
1862 vq
->handle_aio_output
= handle_output
;
1863 aio_set_event_notifier(ctx
, &vq
->host_notifier
, true,
1864 virtio_queue_host_notifier_aio_read
);
1866 aio_set_event_notifier(ctx
, &vq
->host_notifier
, true, NULL
);
1867 /* Test and clear notifier before after disabling event,
1868 * in case poll callback didn't have time to run. */
1869 virtio_queue_host_notifier_aio_read(&vq
->host_notifier
);
1870 vq
->handle_aio_output
= NULL
;
1874 static void virtio_queue_host_notifier_read(EventNotifier
*n
)
1876 VirtQueue
*vq
= container_of(n
, VirtQueue
, host_notifier
);
1877 if (event_notifier_test_and_clear(n
)) {
1878 virtio_queue_notify_vq(vq
);
1882 void virtio_queue_set_host_notifier_fd_handler(VirtQueue
*vq
, bool assign
,
1885 AioContext
*ctx
= qemu_get_aio_context();
1886 if (assign
&& set_handler
) {
1888 aio_set_event_notifier(ctx
, &vq
->host_notifier
, true,
1889 virtio_queue_host_notifier_read
);
1891 event_notifier_set_handler(&vq
->host_notifier
, true,
1892 virtio_queue_host_notifier_read
);
1896 aio_set_event_notifier(ctx
, &vq
->host_notifier
, true, NULL
);
1898 event_notifier_set_handler(&vq
->host_notifier
, true, NULL
);
1902 /* Test and clear notifier before after disabling event,
1903 * in case poll callback didn't have time to run. */
1904 virtio_queue_host_notifier_read(&vq
->host_notifier
);
1908 EventNotifier
*virtio_queue_get_host_notifier(VirtQueue
*vq
)
1910 return &vq
->host_notifier
;
1913 void virtio_device_set_child_bus_name(VirtIODevice
*vdev
, char *bus_name
)
1915 g_free(vdev
->bus_name
);
1916 vdev
->bus_name
= g_strdup(bus_name
);
1919 static void virtio_device_realize(DeviceState
*dev
, Error
**errp
)
1921 VirtIODevice
*vdev
= VIRTIO_DEVICE(dev
);
1922 VirtioDeviceClass
*vdc
= VIRTIO_DEVICE_GET_CLASS(dev
);
1925 if (vdc
->realize
!= NULL
) {
1926 vdc
->realize(dev
, &err
);
1928 error_propagate(errp
, err
);
1933 virtio_bus_device_plugged(vdev
, &err
);
1935 error_propagate(errp
, err
);
1940 static void virtio_device_unrealize(DeviceState
*dev
, Error
**errp
)
1942 VirtIODevice
*vdev
= VIRTIO_DEVICE(dev
);
1943 VirtioDeviceClass
*vdc
= VIRTIO_DEVICE_GET_CLASS(dev
);
1946 virtio_bus_device_unplugged(vdev
);
1948 if (vdc
->unrealize
!= NULL
) {
1949 vdc
->unrealize(dev
, &err
);
1951 error_propagate(errp
, err
);
1956 g_free(vdev
->bus_name
);
1957 vdev
->bus_name
= NULL
;
1960 static Property virtio_properties
[] = {
1961 DEFINE_VIRTIO_COMMON_FEATURES(VirtIODevice
, host_features
),
1962 DEFINE_PROP_END_OF_LIST(),
1965 static void virtio_device_class_init(ObjectClass
*klass
, void *data
)
1967 /* Set the default value here. */
1968 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1970 dc
->realize
= virtio_device_realize
;
1971 dc
->unrealize
= virtio_device_unrealize
;
1972 dc
->bus_type
= TYPE_VIRTIO_BUS
;
1973 dc
->props
= virtio_properties
;
1976 static const TypeInfo virtio_device_info
= {
1977 .name
= TYPE_VIRTIO_DEVICE
,
1978 .parent
= TYPE_DEVICE
,
1979 .instance_size
= sizeof(VirtIODevice
),
1980 .class_init
= virtio_device_class_init
,
1982 .class_size
= sizeof(VirtioDeviceClass
),
1985 static void virtio_register_types(void)
1987 type_register_static(&virtio_device_info
);
1990 type_init(virtio_register_types
)