4 * Copyright Red Hat, Inc. 2010
7 * Michael S. Tsirkin <mst@redhat.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.
12 * Contributions after 2012-01-13 are licensed under the terms of the
13 * GNU GPL, version 2 or (at your option) any later version.
16 #include "qemu/osdep.h"
17 #include "qapi/error.h"
18 #include "hw/virtio/vhost.h"
20 #include "qemu/atomic.h"
21 #include "qemu/range.h"
22 #include "qemu/error-report.h"
23 #include "qemu/memfd.h"
24 #include <linux/vhost.h>
25 #include "exec/address-spaces.h"
26 #include "hw/virtio/virtio-bus.h"
27 #include "hw/virtio/virtio-access.h"
28 #include "migration/migration.h"
30 static struct vhost_log
*vhost_log
;
31 static struct vhost_log
*vhost_log_shm
;
33 static unsigned int used_memslots
;
34 static QLIST_HEAD(, vhost_dev
) vhost_devices
=
35 QLIST_HEAD_INITIALIZER(vhost_devices
);
37 bool vhost_has_free_slot(void)
39 unsigned int slots_limit
= ~0U;
40 struct vhost_dev
*hdev
;
42 QLIST_FOREACH(hdev
, &vhost_devices
, entry
) {
43 unsigned int r
= hdev
->vhost_ops
->vhost_backend_memslots_limit(hdev
);
44 slots_limit
= MIN(slots_limit
, r
);
46 return slots_limit
> used_memslots
;
49 static void vhost_dev_sync_region(struct vhost_dev
*dev
,
50 MemoryRegionSection
*section
,
51 uint64_t mfirst
, uint64_t mlast
,
52 uint64_t rfirst
, uint64_t rlast
)
54 vhost_log_chunk_t
*log
= dev
->log
->log
;
56 uint64_t start
= MAX(mfirst
, rfirst
);
57 uint64_t end
= MIN(mlast
, rlast
);
58 vhost_log_chunk_t
*from
= log
+ start
/ VHOST_LOG_CHUNK
;
59 vhost_log_chunk_t
*to
= log
+ end
/ VHOST_LOG_CHUNK
+ 1;
60 uint64_t addr
= (start
/ VHOST_LOG_CHUNK
) * VHOST_LOG_CHUNK
;
65 assert(end
/ VHOST_LOG_CHUNK
< dev
->log_size
);
66 assert(start
/ VHOST_LOG_CHUNK
< dev
->log_size
);
68 for (;from
< to
; ++from
) {
69 vhost_log_chunk_t log
;
70 /* We first check with non-atomic: much cheaper,
71 * and we expect non-dirty to be the common case. */
73 addr
+= VHOST_LOG_CHUNK
;
76 /* Data must be read atomically. We don't really need barrier semantics
77 * but it's easier to use atomic_* than roll our own. */
78 log
= atomic_xchg(from
, 0);
82 hwaddr section_offset
;
84 page_addr
= addr
+ bit
* VHOST_LOG_PAGE
;
85 section_offset
= page_addr
- section
->offset_within_address_space
;
86 mr_offset
= section_offset
+ section
->offset_within_region
;
87 memory_region_set_dirty(section
->mr
, mr_offset
, VHOST_LOG_PAGE
);
88 log
&= ~(0x1ull
<< bit
);
90 addr
+= VHOST_LOG_CHUNK
;
94 static int vhost_sync_dirty_bitmap(struct vhost_dev
*dev
,
95 MemoryRegionSection
*section
,
103 if (!dev
->log_enabled
|| !dev
->started
) {
106 start_addr
= section
->offset_within_address_space
;
107 end_addr
= range_get_last(start_addr
, int128_get64(section
->size
));
108 start_addr
= MAX(first
, start_addr
);
109 end_addr
= MIN(last
, end_addr
);
111 for (i
= 0; i
< dev
->mem
->nregions
; ++i
) {
112 struct vhost_memory_region
*reg
= dev
->mem
->regions
+ i
;
113 vhost_dev_sync_region(dev
, section
, start_addr
, end_addr
,
114 reg
->guest_phys_addr
,
115 range_get_last(reg
->guest_phys_addr
,
118 for (i
= 0; i
< dev
->nvqs
; ++i
) {
119 struct vhost_virtqueue
*vq
= dev
->vqs
+ i
;
120 vhost_dev_sync_region(dev
, section
, start_addr
, end_addr
, vq
->used_phys
,
121 range_get_last(vq
->used_phys
, vq
->used_size
));
126 static void vhost_log_sync(MemoryListener
*listener
,
127 MemoryRegionSection
*section
)
129 struct vhost_dev
*dev
= container_of(listener
, struct vhost_dev
,
131 vhost_sync_dirty_bitmap(dev
, section
, 0x0, ~0x0ULL
);
134 static void vhost_log_sync_range(struct vhost_dev
*dev
,
135 hwaddr first
, hwaddr last
)
138 /* FIXME: this is N^2 in number of sections */
139 for (i
= 0; i
< dev
->n_mem_sections
; ++i
) {
140 MemoryRegionSection
*section
= &dev
->mem_sections
[i
];
141 vhost_sync_dirty_bitmap(dev
, section
, first
, last
);
145 /* Assign/unassign. Keep an unsorted array of non-overlapping
146 * memory regions in dev->mem. */
147 static void vhost_dev_unassign_memory(struct vhost_dev
*dev
,
151 int from
, to
, n
= dev
->mem
->nregions
;
152 /* Track overlapping/split regions for sanity checking. */
153 int overlap_start
= 0, overlap_end
= 0, overlap_middle
= 0, split
= 0;
155 for (from
= 0, to
= 0; from
< n
; ++from
, ++to
) {
156 struct vhost_memory_region
*reg
= dev
->mem
->regions
+ to
;
161 /* clone old region */
163 memcpy(reg
, dev
->mem
->regions
+ from
, sizeof *reg
);
166 /* No overlap is simple */
167 if (!ranges_overlap(reg
->guest_phys_addr
, reg
->memory_size
,
172 /* Split only happens if supplied region
173 * is in the middle of an existing one. Thus it can not
174 * overlap with any other existing region. */
177 reglast
= range_get_last(reg
->guest_phys_addr
, reg
->memory_size
);
178 memlast
= range_get_last(start_addr
, size
);
180 /* Remove whole region */
181 if (start_addr
<= reg
->guest_phys_addr
&& memlast
>= reglast
) {
182 --dev
->mem
->nregions
;
189 if (memlast
>= reglast
) {
190 reg
->memory_size
= start_addr
- reg
->guest_phys_addr
;
191 assert(reg
->memory_size
);
192 assert(!overlap_end
);
198 if (start_addr
<= reg
->guest_phys_addr
) {
199 change
= memlast
+ 1 - reg
->guest_phys_addr
;
200 reg
->memory_size
-= change
;
201 reg
->guest_phys_addr
+= change
;
202 reg
->userspace_addr
+= change
;
203 assert(reg
->memory_size
);
204 assert(!overlap_start
);
209 /* This only happens if supplied region
210 * is in the middle of an existing one. Thus it can not
211 * overlap with any other existing region. */
212 assert(!overlap_start
);
213 assert(!overlap_end
);
214 assert(!overlap_middle
);
215 /* Split region: shrink first part, shift second part. */
216 memcpy(dev
->mem
->regions
+ n
, reg
, sizeof *reg
);
217 reg
->memory_size
= start_addr
- reg
->guest_phys_addr
;
218 assert(reg
->memory_size
);
219 change
= memlast
+ 1 - reg
->guest_phys_addr
;
220 reg
= dev
->mem
->regions
+ n
;
221 reg
->memory_size
-= change
;
222 assert(reg
->memory_size
);
223 reg
->guest_phys_addr
+= change
;
224 reg
->userspace_addr
+= change
;
225 /* Never add more than 1 region */
226 assert(dev
->mem
->nregions
== n
);
227 ++dev
->mem
->nregions
;
232 /* Called after unassign, so no regions overlap the given range. */
233 static void vhost_dev_assign_memory(struct vhost_dev
*dev
,
239 struct vhost_memory_region
*merged
= NULL
;
240 for (from
= 0, to
= 0; from
< dev
->mem
->nregions
; ++from
, ++to
) {
241 struct vhost_memory_region
*reg
= dev
->mem
->regions
+ to
;
242 uint64_t prlast
, urlast
;
243 uint64_t pmlast
, umlast
;
246 /* clone old region */
248 memcpy(reg
, dev
->mem
->regions
+ from
, sizeof *reg
);
250 prlast
= range_get_last(reg
->guest_phys_addr
, reg
->memory_size
);
251 pmlast
= range_get_last(start_addr
, size
);
252 urlast
= range_get_last(reg
->userspace_addr
, reg
->memory_size
);
253 umlast
= range_get_last(uaddr
, size
);
255 /* check for overlapping regions: should never happen. */
256 assert(prlast
< start_addr
|| pmlast
< reg
->guest_phys_addr
);
257 /* Not an adjacent or overlapping region - do not merge. */
258 if ((prlast
+ 1 != start_addr
|| urlast
+ 1 != uaddr
) &&
259 (pmlast
+ 1 != reg
->guest_phys_addr
||
260 umlast
+ 1 != reg
->userspace_addr
)) {
264 if (dev
->vhost_ops
->vhost_backend_can_merge
&&
265 !dev
->vhost_ops
->vhost_backend_can_merge(dev
, uaddr
, size
,
277 u
= MIN(uaddr
, reg
->userspace_addr
);
278 s
= MIN(start_addr
, reg
->guest_phys_addr
);
279 e
= MAX(pmlast
, prlast
);
280 uaddr
= merged
->userspace_addr
= u
;
281 start_addr
= merged
->guest_phys_addr
= s
;
282 size
= merged
->memory_size
= e
- s
+ 1;
283 assert(merged
->memory_size
);
287 struct vhost_memory_region
*reg
= dev
->mem
->regions
+ to
;
288 memset(reg
, 0, sizeof *reg
);
289 reg
->memory_size
= size
;
290 assert(reg
->memory_size
);
291 reg
->guest_phys_addr
= start_addr
;
292 reg
->userspace_addr
= uaddr
;
295 assert(to
<= dev
->mem
->nregions
+ 1);
296 dev
->mem
->nregions
= to
;
299 static uint64_t vhost_get_log_size(struct vhost_dev
*dev
)
301 uint64_t log_size
= 0;
303 for (i
= 0; i
< dev
->mem
->nregions
; ++i
) {
304 struct vhost_memory_region
*reg
= dev
->mem
->regions
+ i
;
305 uint64_t last
= range_get_last(reg
->guest_phys_addr
,
307 log_size
= MAX(log_size
, last
/ VHOST_LOG_CHUNK
+ 1);
309 for (i
= 0; i
< dev
->nvqs
; ++i
) {
310 struct vhost_virtqueue
*vq
= dev
->vqs
+ i
;
311 uint64_t last
= vq
->used_phys
+ vq
->used_size
- 1;
312 log_size
= MAX(log_size
, last
/ VHOST_LOG_CHUNK
+ 1);
317 static struct vhost_log
*vhost_log_alloc(uint64_t size
, bool share
)
319 struct vhost_log
*log
;
320 uint64_t logsize
= size
* sizeof(*(log
->log
));
323 log
= g_new0(struct vhost_log
, 1);
325 log
->log
= qemu_memfd_alloc("vhost-log", logsize
,
326 F_SEAL_GROW
| F_SEAL_SHRINK
| F_SEAL_SEAL
,
328 memset(log
->log
, 0, logsize
);
330 log
->log
= g_malloc0(logsize
);
340 static struct vhost_log
*vhost_log_get(uint64_t size
, bool share
)
342 struct vhost_log
*log
= share
? vhost_log_shm
: vhost_log
;
344 if (!log
|| log
->size
!= size
) {
345 log
= vhost_log_alloc(size
, share
);
358 static void vhost_log_put(struct vhost_dev
*dev
, bool sync
)
360 struct vhost_log
*log
= dev
->log
;
369 if (log
->refcnt
== 0) {
370 /* Sync only the range covered by the old log */
371 if (dev
->log_size
&& sync
) {
372 vhost_log_sync_range(dev
, 0, dev
->log_size
* VHOST_LOG_CHUNK
- 1);
375 if (vhost_log
== log
) {
378 } else if (vhost_log_shm
== log
) {
379 qemu_memfd_free(log
->log
, log
->size
* sizeof(*(log
->log
)),
381 vhost_log_shm
= NULL
;
388 static bool vhost_dev_log_is_shared(struct vhost_dev
*dev
)
390 return dev
->vhost_ops
->vhost_requires_shm_log
&&
391 dev
->vhost_ops
->vhost_requires_shm_log(dev
);
394 static inline void vhost_dev_log_resize(struct vhost_dev
*dev
, uint64_t size
)
396 struct vhost_log
*log
= vhost_log_get(size
, vhost_dev_log_is_shared(dev
));
397 uint64_t log_base
= (uintptr_t)log
->log
;
400 /* inform backend of log switching, this must be done before
401 releasing the current log, to ensure no logging is lost */
402 r
= dev
->vhost_ops
->vhost_set_log_base(dev
, log_base
, log
);
404 vhost_log_put(dev
, true);
406 dev
->log_size
= size
;
409 static int vhost_verify_ring_mappings(struct vhost_dev
*dev
,
416 for (i
= 0; !r
&& i
< dev
->nvqs
; ++i
) {
417 struct vhost_virtqueue
*vq
= dev
->vqs
+ i
;
421 if (!ranges_overlap(start_addr
, size
, vq
->ring_phys
, vq
->ring_size
)) {
425 p
= cpu_physical_memory_map(vq
->ring_phys
, &l
, 1);
426 if (!p
|| l
!= vq
->ring_size
) {
427 fprintf(stderr
, "Unable to map ring buffer for ring %d\n", i
);
431 fprintf(stderr
, "Ring buffer relocated for ring %d\n", i
);
434 cpu_physical_memory_unmap(p
, l
, 0, 0);
439 static struct vhost_memory_region
*vhost_dev_find_reg(struct vhost_dev
*dev
,
443 int i
, n
= dev
->mem
->nregions
;
444 for (i
= 0; i
< n
; ++i
) {
445 struct vhost_memory_region
*reg
= dev
->mem
->regions
+ i
;
446 if (ranges_overlap(reg
->guest_phys_addr
, reg
->memory_size
,
454 static bool vhost_dev_cmp_memory(struct vhost_dev
*dev
,
459 struct vhost_memory_region
*reg
= vhost_dev_find_reg(dev
, start_addr
, size
);
467 reglast
= range_get_last(reg
->guest_phys_addr
, reg
->memory_size
);
468 memlast
= range_get_last(start_addr
, size
);
470 /* Need to extend region? */
471 if (start_addr
< reg
->guest_phys_addr
|| memlast
> reglast
) {
474 /* userspace_addr changed? */
475 return uaddr
!= reg
->userspace_addr
+ start_addr
- reg
->guest_phys_addr
;
478 static void vhost_set_memory(MemoryListener
*listener
,
479 MemoryRegionSection
*section
,
482 struct vhost_dev
*dev
= container_of(listener
, struct vhost_dev
,
484 hwaddr start_addr
= section
->offset_within_address_space
;
485 ram_addr_t size
= int128_get64(section
->size
);
487 memory_region_get_dirty_log_mask(section
->mr
) & ~(1 << DIRTY_MEMORY_MIGRATION
);
488 int s
= offsetof(struct vhost_memory
, regions
) +
489 (dev
->mem
->nregions
+ 1) * sizeof dev
->mem
->regions
[0];
492 dev
->mem
= g_realloc(dev
->mem
, s
);
500 /* Optimize no-change case. At least cirrus_vga does this a lot at this time. */
501 ram
= memory_region_get_ram_ptr(section
->mr
) + section
->offset_within_region
;
503 if (!vhost_dev_cmp_memory(dev
, start_addr
, size
, (uintptr_t)ram
)) {
504 /* Region exists with same address. Nothing to do. */
508 if (!vhost_dev_find_reg(dev
, start_addr
, size
)) {
509 /* Removing region that we don't access. Nothing to do. */
514 vhost_dev_unassign_memory(dev
, start_addr
, size
);
516 /* Add given mapping, merging adjacent regions if any */
517 vhost_dev_assign_memory(dev
, start_addr
, size
, (uintptr_t)ram
);
519 /* Remove old mapping for this memory, if any. */
520 vhost_dev_unassign_memory(dev
, start_addr
, size
);
522 dev
->mem_changed_start_addr
= MIN(dev
->mem_changed_start_addr
, start_addr
);
523 dev
->mem_changed_end_addr
= MAX(dev
->mem_changed_end_addr
, start_addr
+ size
- 1);
524 dev
->memory_changed
= true;
525 used_memslots
= dev
->mem
->nregions
;
528 static bool vhost_section(MemoryRegionSection
*section
)
530 return memory_region_is_ram(section
->mr
);
533 static void vhost_begin(MemoryListener
*listener
)
535 struct vhost_dev
*dev
= container_of(listener
, struct vhost_dev
,
537 dev
->mem_changed_end_addr
= 0;
538 dev
->mem_changed_start_addr
= -1;
541 static void vhost_commit(MemoryListener
*listener
)
543 struct vhost_dev
*dev
= container_of(listener
, struct vhost_dev
,
545 hwaddr start_addr
= 0;
550 if (!dev
->memory_changed
) {
556 if (dev
->mem_changed_start_addr
> dev
->mem_changed_end_addr
) {
561 start_addr
= dev
->mem_changed_start_addr
;
562 size
= dev
->mem_changed_end_addr
- dev
->mem_changed_start_addr
+ 1;
564 r
= vhost_verify_ring_mappings(dev
, start_addr
, size
);
568 if (!dev
->log_enabled
) {
569 r
= dev
->vhost_ops
->vhost_set_mem_table(dev
, dev
->mem
);
571 dev
->memory_changed
= false;
574 log_size
= vhost_get_log_size(dev
);
575 /* We allocate an extra 4K bytes to log,
576 * to reduce the * number of reallocations. */
577 #define VHOST_LOG_BUFFER (0x1000 / sizeof *dev->log)
578 /* To log more, must increase log size before table update. */
579 if (dev
->log_size
< log_size
) {
580 vhost_dev_log_resize(dev
, log_size
+ VHOST_LOG_BUFFER
);
582 r
= dev
->vhost_ops
->vhost_set_mem_table(dev
, dev
->mem
);
584 /* To log less, can only decrease log size after table update. */
585 if (dev
->log_size
> log_size
+ VHOST_LOG_BUFFER
) {
586 vhost_dev_log_resize(dev
, log_size
);
588 dev
->memory_changed
= false;
591 static void vhost_region_add(MemoryListener
*listener
,
592 MemoryRegionSection
*section
)
594 struct vhost_dev
*dev
= container_of(listener
, struct vhost_dev
,
597 if (!vhost_section(section
)) {
601 ++dev
->n_mem_sections
;
602 dev
->mem_sections
= g_renew(MemoryRegionSection
, dev
->mem_sections
,
603 dev
->n_mem_sections
);
604 dev
->mem_sections
[dev
->n_mem_sections
- 1] = *section
;
605 memory_region_ref(section
->mr
);
606 vhost_set_memory(listener
, section
, true);
609 static void vhost_region_del(MemoryListener
*listener
,
610 MemoryRegionSection
*section
)
612 struct vhost_dev
*dev
= container_of(listener
, struct vhost_dev
,
616 if (!vhost_section(section
)) {
620 vhost_set_memory(listener
, section
, false);
621 memory_region_unref(section
->mr
);
622 for (i
= 0; i
< dev
->n_mem_sections
; ++i
) {
623 if (dev
->mem_sections
[i
].offset_within_address_space
624 == section
->offset_within_address_space
) {
625 --dev
->n_mem_sections
;
626 memmove(&dev
->mem_sections
[i
], &dev
->mem_sections
[i
+1],
627 (dev
->n_mem_sections
- i
) * sizeof(*dev
->mem_sections
));
633 static void vhost_region_nop(MemoryListener
*listener
,
634 MemoryRegionSection
*section
)
638 static int vhost_virtqueue_set_addr(struct vhost_dev
*dev
,
639 struct vhost_virtqueue
*vq
,
640 unsigned idx
, bool enable_log
)
642 struct vhost_vring_addr addr
= {
644 .desc_user_addr
= (uint64_t)(unsigned long)vq
->desc
,
645 .avail_user_addr
= (uint64_t)(unsigned long)vq
->avail
,
646 .used_user_addr
= (uint64_t)(unsigned long)vq
->used
,
647 .log_guest_addr
= vq
->used_phys
,
648 .flags
= enable_log
? (1 << VHOST_VRING_F_LOG
) : 0,
650 int r
= dev
->vhost_ops
->vhost_set_vring_addr(dev
, &addr
);
657 static int vhost_dev_set_features(struct vhost_dev
*dev
, bool enable_log
)
659 uint64_t features
= dev
->acked_features
;
662 features
|= 0x1ULL
<< VHOST_F_LOG_ALL
;
664 r
= dev
->vhost_ops
->vhost_set_features(dev
, features
);
665 return r
< 0 ? -errno
: 0;
668 static int vhost_dev_set_log(struct vhost_dev
*dev
, bool enable_log
)
671 r
= vhost_dev_set_features(dev
, enable_log
);
675 for (i
= 0; i
< dev
->nvqs
; ++i
) {
676 idx
= dev
->vhost_ops
->vhost_get_vq_index(dev
, dev
->vq_index
+ i
);
677 r
= vhost_virtqueue_set_addr(dev
, dev
->vqs
+ i
, idx
,
685 for (; i
>= 0; --i
) {
686 idx
= dev
->vhost_ops
->vhost_get_vq_index(dev
, dev
->vq_index
+ i
);
687 t
= vhost_virtqueue_set_addr(dev
, dev
->vqs
+ i
, idx
,
691 t
= vhost_dev_set_features(dev
, dev
->log_enabled
);
697 static int vhost_migration_log(MemoryListener
*listener
, int enable
)
699 struct vhost_dev
*dev
= container_of(listener
, struct vhost_dev
,
702 if (!!enable
== dev
->log_enabled
) {
706 dev
->log_enabled
= enable
;
710 r
= vhost_dev_set_log(dev
, false);
714 vhost_log_put(dev
, false);
716 vhost_dev_log_resize(dev
, vhost_get_log_size(dev
));
717 r
= vhost_dev_set_log(dev
, true);
722 dev
->log_enabled
= enable
;
726 static void vhost_log_global_start(MemoryListener
*listener
)
730 r
= vhost_migration_log(listener
, true);
736 static void vhost_log_global_stop(MemoryListener
*listener
)
740 r
= vhost_migration_log(listener
, false);
746 static void vhost_log_start(MemoryListener
*listener
,
747 MemoryRegionSection
*section
,
750 /* FIXME: implement */
753 static void vhost_log_stop(MemoryListener
*listener
,
754 MemoryRegionSection
*section
,
757 /* FIXME: implement */
760 /* The vhost driver natively knows how to handle the vrings of non
761 * cross-endian legacy devices and modern devices. Only legacy devices
762 * exposed to a bi-endian guest may require the vhost driver to use a
763 * specific endianness.
765 static inline bool vhost_needs_vring_endian(VirtIODevice
*vdev
)
767 if (virtio_vdev_has_feature(vdev
, VIRTIO_F_VERSION_1
)) {
770 #ifdef HOST_WORDS_BIGENDIAN
771 return vdev
->device_endian
== VIRTIO_DEVICE_ENDIAN_LITTLE
;
773 return vdev
->device_endian
== VIRTIO_DEVICE_ENDIAN_BIG
;
777 static int vhost_virtqueue_set_vring_endian_legacy(struct vhost_dev
*dev
,
781 struct vhost_vring_state s
= {
782 .index
= vhost_vq_index
,
786 if (!dev
->vhost_ops
->vhost_set_vring_endian(dev
, &s
)) {
790 if (errno
== ENOTTY
) {
791 error_report("vhost does not support cross-endian");
798 static int vhost_virtqueue_start(struct vhost_dev
*dev
,
799 struct VirtIODevice
*vdev
,
800 struct vhost_virtqueue
*vq
,
805 int vhost_vq_index
= dev
->vhost_ops
->vhost_get_vq_index(dev
, idx
);
806 struct vhost_vring_file file
= {
807 .index
= vhost_vq_index
809 struct vhost_vring_state state
= {
810 .index
= vhost_vq_index
812 struct VirtQueue
*vvq
= virtio_get_queue(vdev
, idx
);
815 vq
->num
= state
.num
= virtio_queue_get_num(vdev
, idx
);
816 r
= dev
->vhost_ops
->vhost_set_vring_num(dev
, &state
);
821 state
.num
= virtio_queue_get_last_avail_idx(vdev
, idx
);
822 r
= dev
->vhost_ops
->vhost_set_vring_base(dev
, &state
);
827 if (vhost_needs_vring_endian(vdev
)) {
828 r
= vhost_virtqueue_set_vring_endian_legacy(dev
,
829 virtio_is_big_endian(vdev
),
836 s
= l
= virtio_queue_get_desc_size(vdev
, idx
);
837 a
= virtio_queue_get_desc_addr(vdev
, idx
);
838 vq
->desc
= cpu_physical_memory_map(a
, &l
, 0);
839 if (!vq
->desc
|| l
!= s
) {
841 goto fail_alloc_desc
;
843 s
= l
= virtio_queue_get_avail_size(vdev
, idx
);
844 a
= virtio_queue_get_avail_addr(vdev
, idx
);
845 vq
->avail
= cpu_physical_memory_map(a
, &l
, 0);
846 if (!vq
->avail
|| l
!= s
) {
848 goto fail_alloc_avail
;
850 vq
->used_size
= s
= l
= virtio_queue_get_used_size(vdev
, idx
);
851 vq
->used_phys
= a
= virtio_queue_get_used_addr(vdev
, idx
);
852 vq
->used
= cpu_physical_memory_map(a
, &l
, 1);
853 if (!vq
->used
|| l
!= s
) {
855 goto fail_alloc_used
;
858 vq
->ring_size
= s
= l
= virtio_queue_get_ring_size(vdev
, idx
);
859 vq
->ring_phys
= a
= virtio_queue_get_ring_addr(vdev
, idx
);
860 vq
->ring
= cpu_physical_memory_map(a
, &l
, 1);
861 if (!vq
->ring
|| l
!= s
) {
863 goto fail_alloc_ring
;
866 r
= vhost_virtqueue_set_addr(dev
, vq
, vhost_vq_index
, dev
->log_enabled
);
872 file
.fd
= event_notifier_get_fd(virtio_queue_get_host_notifier(vvq
));
873 r
= dev
->vhost_ops
->vhost_set_vring_kick(dev
, &file
);
879 /* Clear and discard previous events if any. */
880 event_notifier_test_and_clear(&vq
->masked_notifier
);
882 /* Init vring in unmasked state, unless guest_notifier_mask
885 if (!vdev
->use_guest_notifier_mask
) {
886 /* TODO: check and handle errors. */
887 vhost_virtqueue_mask(dev
, vdev
, idx
, false);
894 cpu_physical_memory_unmap(vq
->ring
, virtio_queue_get_ring_size(vdev
, idx
),
897 cpu_physical_memory_unmap(vq
->used
, virtio_queue_get_used_size(vdev
, idx
),
900 cpu_physical_memory_unmap(vq
->avail
, virtio_queue_get_avail_size(vdev
, idx
),
903 cpu_physical_memory_unmap(vq
->desc
, virtio_queue_get_desc_size(vdev
, idx
),
909 static void vhost_virtqueue_stop(struct vhost_dev
*dev
,
910 struct VirtIODevice
*vdev
,
911 struct vhost_virtqueue
*vq
,
914 int vhost_vq_index
= dev
->vhost_ops
->vhost_get_vq_index(dev
, idx
);
915 struct vhost_vring_state state
= {
916 .index
= vhost_vq_index
,
920 r
= dev
->vhost_ops
->vhost_get_vring_base(dev
, &state
);
922 fprintf(stderr
, "vhost VQ %d ring restore failed: %d\n", idx
, r
);
925 virtio_queue_set_last_avail_idx(vdev
, idx
, state
.num
);
926 virtio_queue_invalidate_signalled_used(vdev
, idx
);
928 /* In the cross-endian case, we need to reset the vring endianness to
929 * native as legacy devices expect so by default.
931 if (vhost_needs_vring_endian(vdev
)) {
932 r
= vhost_virtqueue_set_vring_endian_legacy(dev
,
933 !virtio_is_big_endian(vdev
),
936 error_report("failed to reset vring endianness");
941 cpu_physical_memory_unmap(vq
->ring
, virtio_queue_get_ring_size(vdev
, idx
),
942 0, virtio_queue_get_ring_size(vdev
, idx
));
943 cpu_physical_memory_unmap(vq
->used
, virtio_queue_get_used_size(vdev
, idx
),
944 1, virtio_queue_get_used_size(vdev
, idx
));
945 cpu_physical_memory_unmap(vq
->avail
, virtio_queue_get_avail_size(vdev
, idx
),
946 0, virtio_queue_get_avail_size(vdev
, idx
));
947 cpu_physical_memory_unmap(vq
->desc
, virtio_queue_get_desc_size(vdev
, idx
),
948 0, virtio_queue_get_desc_size(vdev
, idx
));
951 static void vhost_eventfd_add(MemoryListener
*listener
,
952 MemoryRegionSection
*section
,
953 bool match_data
, uint64_t data
, EventNotifier
*e
)
957 static void vhost_eventfd_del(MemoryListener
*listener
,
958 MemoryRegionSection
*section
,
959 bool match_data
, uint64_t data
, EventNotifier
*e
)
963 static int vhost_virtqueue_set_busyloop_timeout(struct vhost_dev
*dev
,
964 int n
, uint32_t timeout
)
966 int vhost_vq_index
= dev
->vhost_ops
->vhost_get_vq_index(dev
, n
);
967 struct vhost_vring_state state
= {
968 .index
= vhost_vq_index
,
973 if (!dev
->vhost_ops
->vhost_set_vring_busyloop_timeout
) {
977 r
= dev
->vhost_ops
->vhost_set_vring_busyloop_timeout(dev
, &state
);
985 static int vhost_virtqueue_init(struct vhost_dev
*dev
,
986 struct vhost_virtqueue
*vq
, int n
)
988 int vhost_vq_index
= dev
->vhost_ops
->vhost_get_vq_index(dev
, n
);
989 struct vhost_vring_file file
= {
990 .index
= vhost_vq_index
,
992 int r
= event_notifier_init(&vq
->masked_notifier
, 0);
997 file
.fd
= event_notifier_get_fd(&vq
->masked_notifier
);
998 r
= dev
->vhost_ops
->vhost_set_vring_call(dev
, &file
);
1005 event_notifier_cleanup(&vq
->masked_notifier
);
1009 static void vhost_virtqueue_cleanup(struct vhost_virtqueue
*vq
)
1011 event_notifier_cleanup(&vq
->masked_notifier
);
1014 int vhost_dev_init(struct vhost_dev
*hdev
, void *opaque
,
1015 VhostBackendType backend_type
, uint32_t busyloop_timeout
)
1020 hdev
->migration_blocker
= NULL
;
1022 r
= vhost_set_backend_type(hdev
, backend_type
);
1025 r
= hdev
->vhost_ops
->vhost_backend_init(hdev
, opaque
);
1030 if (used_memslots
> hdev
->vhost_ops
->vhost_backend_memslots_limit(hdev
)) {
1031 fprintf(stderr
, "vhost backend memory slots limit is less"
1032 " than current number of present memory slots\n");
1036 QLIST_INSERT_HEAD(&vhost_devices
, hdev
, entry
);
1038 r
= hdev
->vhost_ops
->vhost_set_owner(hdev
);
1043 r
= hdev
->vhost_ops
->vhost_get_features(hdev
, &features
);
1048 for (i
= 0; i
< hdev
->nvqs
; ++i
) {
1049 r
= vhost_virtqueue_init(hdev
, hdev
->vqs
+ i
, hdev
->vq_index
+ i
);
1055 if (busyloop_timeout
) {
1056 for (i
= 0; i
< hdev
->nvqs
; ++i
) {
1057 r
= vhost_virtqueue_set_busyloop_timeout(hdev
, hdev
->vq_index
+ i
,
1065 hdev
->features
= features
;
1067 hdev
->memory_listener
= (MemoryListener
) {
1068 .begin
= vhost_begin
,
1069 .commit
= vhost_commit
,
1070 .region_add
= vhost_region_add
,
1071 .region_del
= vhost_region_del
,
1072 .region_nop
= vhost_region_nop
,
1073 .log_start
= vhost_log_start
,
1074 .log_stop
= vhost_log_stop
,
1075 .log_sync
= vhost_log_sync
,
1076 .log_global_start
= vhost_log_global_start
,
1077 .log_global_stop
= vhost_log_global_stop
,
1078 .eventfd_add
= vhost_eventfd_add
,
1079 .eventfd_del
= vhost_eventfd_del
,
1083 if (hdev
->migration_blocker
== NULL
) {
1084 if (!(hdev
->features
& (0x1ULL
<< VHOST_F_LOG_ALL
))) {
1085 error_setg(&hdev
->migration_blocker
,
1086 "Migration disabled: vhost lacks VHOST_F_LOG_ALL feature.");
1087 } else if (!qemu_memfd_check()) {
1088 error_setg(&hdev
->migration_blocker
,
1089 "Migration disabled: failed to allocate shared memory");
1093 if (hdev
->migration_blocker
!= NULL
) {
1094 migrate_add_blocker(hdev
->migration_blocker
);
1097 hdev
->mem
= g_malloc0(offsetof(struct vhost_memory
, regions
));
1098 hdev
->n_mem_sections
= 0;
1099 hdev
->mem_sections
= NULL
;
1102 hdev
->log_enabled
= false;
1103 hdev
->started
= false;
1104 hdev
->memory_changed
= false;
1105 memory_listener_register(&hdev
->memory_listener
, &address_space_memory
);
1109 vhost_virtqueue_set_busyloop_timeout(hdev
, hdev
->vq_index
+ i
, 0);
1114 vhost_virtqueue_cleanup(hdev
->vqs
+ i
);
1118 hdev
->vhost_ops
->vhost_backend_cleanup(hdev
);
1119 QLIST_REMOVE(hdev
, entry
);
1123 void vhost_dev_cleanup(struct vhost_dev
*hdev
)
1126 for (i
= 0; i
< hdev
->nvqs
; ++i
) {
1127 vhost_virtqueue_cleanup(hdev
->vqs
+ i
);
1129 memory_listener_unregister(&hdev
->memory_listener
);
1130 if (hdev
->migration_blocker
) {
1131 migrate_del_blocker(hdev
->migration_blocker
);
1132 error_free(hdev
->migration_blocker
);
1135 g_free(hdev
->mem_sections
);
1136 hdev
->vhost_ops
->vhost_backend_cleanup(hdev
);
1138 QLIST_REMOVE(hdev
, entry
);
1141 /* Stop processing guest IO notifications in qemu.
1142 * Start processing them in vhost in kernel.
1144 int vhost_dev_enable_notifiers(struct vhost_dev
*hdev
, VirtIODevice
*vdev
)
1146 BusState
*qbus
= BUS(qdev_get_parent_bus(DEVICE(vdev
)));
1147 VirtioBusState
*vbus
= VIRTIO_BUS(qbus
);
1148 VirtioBusClass
*k
= VIRTIO_BUS_GET_CLASS(vbus
);
1150 if (!k
->ioeventfd_started
) {
1151 fprintf(stderr
, "binding does not support host notifiers\n");
1156 for (i
= 0; i
< hdev
->nvqs
; ++i
) {
1157 r
= virtio_bus_set_host_notifier(VIRTIO_BUS(qbus
), hdev
->vq_index
+ i
,
1160 fprintf(stderr
, "vhost VQ %d notifier binding failed: %d\n", i
, -r
);
1168 e
= virtio_bus_set_host_notifier(VIRTIO_BUS(qbus
), hdev
->vq_index
+ i
,
1171 fprintf(stderr
, "vhost VQ %d notifier cleanup error: %d\n", i
, -r
);
1180 /* Stop processing guest IO notifications in vhost.
1181 * Start processing them in qemu.
1182 * This might actually run the qemu handlers right away,
1183 * so virtio in qemu must be completely setup when this is called.
1185 void vhost_dev_disable_notifiers(struct vhost_dev
*hdev
, VirtIODevice
*vdev
)
1187 BusState
*qbus
= BUS(qdev_get_parent_bus(DEVICE(vdev
)));
1190 for (i
= 0; i
< hdev
->nvqs
; ++i
) {
1191 r
= virtio_bus_set_host_notifier(VIRTIO_BUS(qbus
), hdev
->vq_index
+ i
,
1194 fprintf(stderr
, "vhost VQ %d notifier cleanup failed: %d\n", i
, -r
);
1201 /* Test and clear event pending status.
1202 * Should be called after unmask to avoid losing events.
1204 bool vhost_virtqueue_pending(struct vhost_dev
*hdev
, int n
)
1206 struct vhost_virtqueue
*vq
= hdev
->vqs
+ n
- hdev
->vq_index
;
1207 assert(n
>= hdev
->vq_index
&& n
< hdev
->vq_index
+ hdev
->nvqs
);
1208 return event_notifier_test_and_clear(&vq
->masked_notifier
);
1211 /* Mask/unmask events from this vq. */
1212 void vhost_virtqueue_mask(struct vhost_dev
*hdev
, VirtIODevice
*vdev
, int n
,
1215 struct VirtQueue
*vvq
= virtio_get_queue(vdev
, n
);
1216 int r
, index
= n
- hdev
->vq_index
;
1217 struct vhost_vring_file file
;
1220 assert(vdev
->use_guest_notifier_mask
);
1221 file
.fd
= event_notifier_get_fd(&hdev
->vqs
[index
].masked_notifier
);
1223 file
.fd
= event_notifier_get_fd(virtio_queue_get_guest_notifier(vvq
));
1226 file
.index
= hdev
->vhost_ops
->vhost_get_vq_index(hdev
, n
);
1227 r
= hdev
->vhost_ops
->vhost_set_vring_call(hdev
, &file
);
1231 uint64_t vhost_get_features(struct vhost_dev
*hdev
, const int *feature_bits
,
1234 const int *bit
= feature_bits
;
1235 while (*bit
!= VHOST_INVALID_FEATURE_BIT
) {
1236 uint64_t bit_mask
= (1ULL << *bit
);
1237 if (!(hdev
->features
& bit_mask
)) {
1238 features
&= ~bit_mask
;
1245 void vhost_ack_features(struct vhost_dev
*hdev
, const int *feature_bits
,
1248 const int *bit
= feature_bits
;
1249 while (*bit
!= VHOST_INVALID_FEATURE_BIT
) {
1250 uint64_t bit_mask
= (1ULL << *bit
);
1251 if (features
& bit_mask
) {
1252 hdev
->acked_features
|= bit_mask
;
1258 /* Host notifiers must be enabled at this point. */
1259 int vhost_dev_start(struct vhost_dev
*hdev
, VirtIODevice
*vdev
)
1263 hdev
->started
= true;
1265 r
= vhost_dev_set_features(hdev
, hdev
->log_enabled
);
1269 r
= hdev
->vhost_ops
->vhost_set_mem_table(hdev
, hdev
->mem
);
1274 for (i
= 0; i
< hdev
->nvqs
; ++i
) {
1275 r
= vhost_virtqueue_start(hdev
,
1278 hdev
->vq_index
+ i
);
1284 if (hdev
->log_enabled
) {
1287 hdev
->log_size
= vhost_get_log_size(hdev
);
1288 hdev
->log
= vhost_log_get(hdev
->log_size
,
1289 vhost_dev_log_is_shared(hdev
));
1290 log_base
= (uintptr_t)hdev
->log
->log
;
1291 r
= hdev
->vhost_ops
->vhost_set_log_base(hdev
,
1292 hdev
->log_size
? log_base
: 0,
1302 vhost_log_put(hdev
, false);
1305 vhost_virtqueue_stop(hdev
,
1308 hdev
->vq_index
+ i
);
1314 hdev
->started
= false;
1318 /* Host notifiers must be enabled at this point. */
1319 void vhost_dev_stop(struct vhost_dev
*hdev
, VirtIODevice
*vdev
)
1323 for (i
= 0; i
< hdev
->nvqs
; ++i
) {
1324 vhost_virtqueue_stop(hdev
,
1327 hdev
->vq_index
+ i
);
1330 vhost_log_put(hdev
, true);
1331 hdev
->started
= false;