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 "hw/virtio/vhost.h"
19 #include "qemu/atomic.h"
20 #include "qemu/range.h"
21 #include "qemu/error-report.h"
22 #include "qemu/memfd.h"
23 #include <linux/vhost.h>
24 #include "exec/address-spaces.h"
25 #include "hw/virtio/virtio-bus.h"
26 #include "hw/virtio/virtio-access.h"
27 #include "migration/migration.h"
29 static struct vhost_log
*vhost_log
;
30 static struct vhost_log
*vhost_log_shm
;
32 static unsigned int used_memslots
;
33 static QLIST_HEAD(, vhost_dev
) vhost_devices
=
34 QLIST_HEAD_INITIALIZER(vhost_devices
);
36 bool vhost_has_free_slot(void)
38 unsigned int slots_limit
= ~0U;
39 struct vhost_dev
*hdev
;
41 QLIST_FOREACH(hdev
, &vhost_devices
, entry
) {
42 unsigned int r
= hdev
->vhost_ops
->vhost_backend_memslots_limit(hdev
);
43 slots_limit
= MIN(slots_limit
, r
);
45 return slots_limit
> used_memslots
;
48 static void vhost_dev_sync_region(struct vhost_dev
*dev
,
49 MemoryRegionSection
*section
,
50 uint64_t mfirst
, uint64_t mlast
,
51 uint64_t rfirst
, uint64_t rlast
)
53 vhost_log_chunk_t
*log
= dev
->log
->log
;
55 uint64_t start
= MAX(mfirst
, rfirst
);
56 uint64_t end
= MIN(mlast
, rlast
);
57 vhost_log_chunk_t
*from
= log
+ start
/ VHOST_LOG_CHUNK
;
58 vhost_log_chunk_t
*to
= log
+ end
/ VHOST_LOG_CHUNK
+ 1;
59 uint64_t addr
= (start
/ VHOST_LOG_CHUNK
) * VHOST_LOG_CHUNK
;
64 assert(end
/ VHOST_LOG_CHUNK
< dev
->log_size
);
65 assert(start
/ VHOST_LOG_CHUNK
< dev
->log_size
);
67 for (;from
< to
; ++from
) {
68 vhost_log_chunk_t log
;
69 /* We first check with non-atomic: much cheaper,
70 * and we expect non-dirty to be the common case. */
72 addr
+= VHOST_LOG_CHUNK
;
75 /* Data must be read atomically. We don't really need barrier semantics
76 * but it's easier to use atomic_* than roll our own. */
77 log
= atomic_xchg(from
, 0);
81 hwaddr section_offset
;
83 page_addr
= addr
+ bit
* VHOST_LOG_PAGE
;
84 section_offset
= page_addr
- section
->offset_within_address_space
;
85 mr_offset
= section_offset
+ section
->offset_within_region
;
86 memory_region_set_dirty(section
->mr
, mr_offset
, VHOST_LOG_PAGE
);
87 log
&= ~(0x1ull
<< bit
);
89 addr
+= VHOST_LOG_CHUNK
;
93 static int vhost_sync_dirty_bitmap(struct vhost_dev
*dev
,
94 MemoryRegionSection
*section
,
102 if (!dev
->log_enabled
|| !dev
->started
) {
105 start_addr
= section
->offset_within_address_space
;
106 end_addr
= range_get_last(start_addr
, int128_get64(section
->size
));
107 start_addr
= MAX(first
, start_addr
);
108 end_addr
= MIN(last
, end_addr
);
110 for (i
= 0; i
< dev
->mem
->nregions
; ++i
) {
111 struct vhost_memory_region
*reg
= dev
->mem
->regions
+ i
;
112 vhost_dev_sync_region(dev
, section
, start_addr
, end_addr
,
113 reg
->guest_phys_addr
,
114 range_get_last(reg
->guest_phys_addr
,
117 for (i
= 0; i
< dev
->nvqs
; ++i
) {
118 struct vhost_virtqueue
*vq
= dev
->vqs
+ i
;
119 vhost_dev_sync_region(dev
, section
, start_addr
, end_addr
, vq
->used_phys
,
120 range_get_last(vq
->used_phys
, vq
->used_size
));
125 static void vhost_log_sync(MemoryListener
*listener
,
126 MemoryRegionSection
*section
)
128 struct vhost_dev
*dev
= container_of(listener
, struct vhost_dev
,
130 vhost_sync_dirty_bitmap(dev
, section
, 0x0, ~0x0ULL
);
133 static void vhost_log_sync_range(struct vhost_dev
*dev
,
134 hwaddr first
, hwaddr last
)
137 /* FIXME: this is N^2 in number of sections */
138 for (i
= 0; i
< dev
->n_mem_sections
; ++i
) {
139 MemoryRegionSection
*section
= &dev
->mem_sections
[i
];
140 vhost_sync_dirty_bitmap(dev
, section
, first
, last
);
144 /* Assign/unassign. Keep an unsorted array of non-overlapping
145 * memory regions in dev->mem. */
146 static void vhost_dev_unassign_memory(struct vhost_dev
*dev
,
150 int from
, to
, n
= dev
->mem
->nregions
;
151 /* Track overlapping/split regions for sanity checking. */
152 int overlap_start
= 0, overlap_end
= 0, overlap_middle
= 0, split
= 0;
154 for (from
= 0, to
= 0; from
< n
; ++from
, ++to
) {
155 struct vhost_memory_region
*reg
= dev
->mem
->regions
+ to
;
160 /* clone old region */
162 memcpy(reg
, dev
->mem
->regions
+ from
, sizeof *reg
);
165 /* No overlap is simple */
166 if (!ranges_overlap(reg
->guest_phys_addr
, reg
->memory_size
,
171 /* Split only happens if supplied region
172 * is in the middle of an existing one. Thus it can not
173 * overlap with any other existing region. */
176 reglast
= range_get_last(reg
->guest_phys_addr
, reg
->memory_size
);
177 memlast
= range_get_last(start_addr
, size
);
179 /* Remove whole region */
180 if (start_addr
<= reg
->guest_phys_addr
&& memlast
>= reglast
) {
181 --dev
->mem
->nregions
;
188 if (memlast
>= reglast
) {
189 reg
->memory_size
= start_addr
- reg
->guest_phys_addr
;
190 assert(reg
->memory_size
);
191 assert(!overlap_end
);
197 if (start_addr
<= reg
->guest_phys_addr
) {
198 change
= memlast
+ 1 - reg
->guest_phys_addr
;
199 reg
->memory_size
-= change
;
200 reg
->guest_phys_addr
+= change
;
201 reg
->userspace_addr
+= change
;
202 assert(reg
->memory_size
);
203 assert(!overlap_start
);
208 /* This only happens if supplied region
209 * is in the middle of an existing one. Thus it can not
210 * overlap with any other existing region. */
211 assert(!overlap_start
);
212 assert(!overlap_end
);
213 assert(!overlap_middle
);
214 /* Split region: shrink first part, shift second part. */
215 memcpy(dev
->mem
->regions
+ n
, reg
, sizeof *reg
);
216 reg
->memory_size
= start_addr
- reg
->guest_phys_addr
;
217 assert(reg
->memory_size
);
218 change
= memlast
+ 1 - reg
->guest_phys_addr
;
219 reg
= dev
->mem
->regions
+ n
;
220 reg
->memory_size
-= change
;
221 assert(reg
->memory_size
);
222 reg
->guest_phys_addr
+= change
;
223 reg
->userspace_addr
+= change
;
224 /* Never add more than 1 region */
225 assert(dev
->mem
->nregions
== n
);
226 ++dev
->mem
->nregions
;
231 /* Called after unassign, so no regions overlap the given range. */
232 static void vhost_dev_assign_memory(struct vhost_dev
*dev
,
238 struct vhost_memory_region
*merged
= NULL
;
239 for (from
= 0, to
= 0; from
< dev
->mem
->nregions
; ++from
, ++to
) {
240 struct vhost_memory_region
*reg
= dev
->mem
->regions
+ to
;
241 uint64_t prlast
, urlast
;
242 uint64_t pmlast
, umlast
;
245 /* clone old region */
247 memcpy(reg
, dev
->mem
->regions
+ from
, sizeof *reg
);
249 prlast
= range_get_last(reg
->guest_phys_addr
, reg
->memory_size
);
250 pmlast
= range_get_last(start_addr
, size
);
251 urlast
= range_get_last(reg
->userspace_addr
, reg
->memory_size
);
252 umlast
= range_get_last(uaddr
, size
);
254 /* check for overlapping regions: should never happen. */
255 assert(prlast
< start_addr
|| pmlast
< reg
->guest_phys_addr
);
256 /* Not an adjacent or overlapping region - do not merge. */
257 if ((prlast
+ 1 != start_addr
|| urlast
+ 1 != uaddr
) &&
258 (pmlast
+ 1 != reg
->guest_phys_addr
||
259 umlast
+ 1 != reg
->userspace_addr
)) {
269 u
= MIN(uaddr
, reg
->userspace_addr
);
270 s
= MIN(start_addr
, reg
->guest_phys_addr
);
271 e
= MAX(pmlast
, prlast
);
272 uaddr
= merged
->userspace_addr
= u
;
273 start_addr
= merged
->guest_phys_addr
= s
;
274 size
= merged
->memory_size
= e
- s
+ 1;
275 assert(merged
->memory_size
);
279 struct vhost_memory_region
*reg
= dev
->mem
->regions
+ to
;
280 memset(reg
, 0, sizeof *reg
);
281 reg
->memory_size
= size
;
282 assert(reg
->memory_size
);
283 reg
->guest_phys_addr
= start_addr
;
284 reg
->userspace_addr
= uaddr
;
287 assert(to
<= dev
->mem
->nregions
+ 1);
288 dev
->mem
->nregions
= to
;
291 static uint64_t vhost_get_log_size(struct vhost_dev
*dev
)
293 uint64_t log_size
= 0;
295 for (i
= 0; i
< dev
->mem
->nregions
; ++i
) {
296 struct vhost_memory_region
*reg
= dev
->mem
->regions
+ i
;
297 uint64_t last
= range_get_last(reg
->guest_phys_addr
,
299 log_size
= MAX(log_size
, last
/ VHOST_LOG_CHUNK
+ 1);
301 for (i
= 0; i
< dev
->nvqs
; ++i
) {
302 struct vhost_virtqueue
*vq
= dev
->vqs
+ i
;
303 uint64_t last
= vq
->used_phys
+ vq
->used_size
- 1;
304 log_size
= MAX(log_size
, last
/ VHOST_LOG_CHUNK
+ 1);
309 static struct vhost_log
*vhost_log_alloc(uint64_t size
, bool share
)
311 struct vhost_log
*log
;
312 uint64_t logsize
= size
* sizeof(*(log
->log
));
315 log
= g_new0(struct vhost_log
, 1);
317 log
->log
= qemu_memfd_alloc("vhost-log", logsize
,
318 F_SEAL_GROW
| F_SEAL_SHRINK
| F_SEAL_SEAL
,
320 memset(log
->log
, 0, logsize
);
322 log
->log
= g_malloc0(logsize
);
332 static struct vhost_log
*vhost_log_get(uint64_t size
, bool share
)
334 struct vhost_log
*log
= share
? vhost_log_shm
: vhost_log
;
336 if (!log
|| log
->size
!= size
) {
337 log
= vhost_log_alloc(size
, share
);
350 static void vhost_log_put(struct vhost_dev
*dev
, bool sync
)
352 struct vhost_log
*log
= dev
->log
;
359 if (log
->refcnt
== 0) {
360 /* Sync only the range covered by the old log */
361 if (dev
->log_size
&& sync
) {
362 vhost_log_sync_range(dev
, 0, dev
->log_size
* VHOST_LOG_CHUNK
- 1);
365 if (vhost_log
== log
) {
368 } else if (vhost_log_shm
== log
) {
369 qemu_memfd_free(log
->log
, log
->size
* sizeof(*(log
->log
)),
371 vhost_log_shm
= NULL
;
378 static bool vhost_dev_log_is_shared(struct vhost_dev
*dev
)
380 return dev
->vhost_ops
->vhost_requires_shm_log
&&
381 dev
->vhost_ops
->vhost_requires_shm_log(dev
);
384 static inline void vhost_dev_log_resize(struct vhost_dev
*dev
, uint64_t size
)
386 struct vhost_log
*log
= vhost_log_get(size
, vhost_dev_log_is_shared(dev
));
387 uint64_t log_base
= (uintptr_t)log
->log
;
390 /* inform backend of log switching, this must be done before
391 releasing the current log, to ensure no logging is lost */
392 r
= dev
->vhost_ops
->vhost_set_log_base(dev
, log_base
, log
);
394 vhost_log_put(dev
, true);
396 dev
->log_size
= size
;
399 static int vhost_verify_ring_mappings(struct vhost_dev
*dev
,
406 for (i
= 0; !r
&& i
< dev
->nvqs
; ++i
) {
407 struct vhost_virtqueue
*vq
= dev
->vqs
+ i
;
411 if (!ranges_overlap(start_addr
, size
, vq
->ring_phys
, vq
->ring_size
)) {
415 p
= cpu_physical_memory_map(vq
->ring_phys
, &l
, 1);
416 if (!p
|| l
!= vq
->ring_size
) {
417 fprintf(stderr
, "Unable to map ring buffer for ring %d\n", i
);
421 fprintf(stderr
, "Ring buffer relocated for ring %d\n", i
);
424 cpu_physical_memory_unmap(p
, l
, 0, 0);
429 static struct vhost_memory_region
*vhost_dev_find_reg(struct vhost_dev
*dev
,
433 int i
, n
= dev
->mem
->nregions
;
434 for (i
= 0; i
< n
; ++i
) {
435 struct vhost_memory_region
*reg
= dev
->mem
->regions
+ i
;
436 if (ranges_overlap(reg
->guest_phys_addr
, reg
->memory_size
,
444 static bool vhost_dev_cmp_memory(struct vhost_dev
*dev
,
449 struct vhost_memory_region
*reg
= vhost_dev_find_reg(dev
, start_addr
, size
);
457 reglast
= range_get_last(reg
->guest_phys_addr
, reg
->memory_size
);
458 memlast
= range_get_last(start_addr
, size
);
460 /* Need to extend region? */
461 if (start_addr
< reg
->guest_phys_addr
|| memlast
> reglast
) {
464 /* userspace_addr changed? */
465 return uaddr
!= reg
->userspace_addr
+ start_addr
- reg
->guest_phys_addr
;
468 static void vhost_set_memory(MemoryListener
*listener
,
469 MemoryRegionSection
*section
,
472 struct vhost_dev
*dev
= container_of(listener
, struct vhost_dev
,
474 hwaddr start_addr
= section
->offset_within_address_space
;
475 ram_addr_t size
= int128_get64(section
->size
);
477 memory_region_get_dirty_log_mask(section
->mr
) & ~(1 << DIRTY_MEMORY_MIGRATION
);
478 int s
= offsetof(struct vhost_memory
, regions
) +
479 (dev
->mem
->nregions
+ 1) * sizeof dev
->mem
->regions
[0];
482 dev
->mem
= g_realloc(dev
->mem
, s
);
490 /* Optimize no-change case. At least cirrus_vga does this a lot at this time. */
491 ram
= memory_region_get_ram_ptr(section
->mr
) + section
->offset_within_region
;
493 if (!vhost_dev_cmp_memory(dev
, start_addr
, size
, (uintptr_t)ram
)) {
494 /* Region exists with same address. Nothing to do. */
498 if (!vhost_dev_find_reg(dev
, start_addr
, size
)) {
499 /* Removing region that we don't access. Nothing to do. */
504 vhost_dev_unassign_memory(dev
, start_addr
, size
);
506 /* Add given mapping, merging adjacent regions if any */
507 vhost_dev_assign_memory(dev
, start_addr
, size
, (uintptr_t)ram
);
509 /* Remove old mapping for this memory, if any. */
510 vhost_dev_unassign_memory(dev
, start_addr
, size
);
512 dev
->mem_changed_start_addr
= MIN(dev
->mem_changed_start_addr
, start_addr
);
513 dev
->mem_changed_end_addr
= MAX(dev
->mem_changed_end_addr
, start_addr
+ size
- 1);
514 dev
->memory_changed
= true;
515 used_memslots
= dev
->mem
->nregions
;
518 static bool vhost_section(MemoryRegionSection
*section
)
520 return memory_region_is_ram(section
->mr
);
523 static void vhost_begin(MemoryListener
*listener
)
525 struct vhost_dev
*dev
= container_of(listener
, struct vhost_dev
,
527 dev
->mem_changed_end_addr
= 0;
528 dev
->mem_changed_start_addr
= -1;
531 static void vhost_commit(MemoryListener
*listener
)
533 struct vhost_dev
*dev
= container_of(listener
, struct vhost_dev
,
535 hwaddr start_addr
= 0;
540 if (!dev
->memory_changed
) {
546 if (dev
->mem_changed_start_addr
> dev
->mem_changed_end_addr
) {
551 start_addr
= dev
->mem_changed_start_addr
;
552 size
= dev
->mem_changed_end_addr
- dev
->mem_changed_start_addr
+ 1;
554 r
= vhost_verify_ring_mappings(dev
, start_addr
, size
);
558 if (!dev
->log_enabled
) {
559 r
= dev
->vhost_ops
->vhost_set_mem_table(dev
, dev
->mem
);
561 dev
->memory_changed
= false;
564 log_size
= vhost_get_log_size(dev
);
565 /* We allocate an extra 4K bytes to log,
566 * to reduce the * number of reallocations. */
567 #define VHOST_LOG_BUFFER (0x1000 / sizeof *dev->log)
568 /* To log more, must increase log size before table update. */
569 if (dev
->log_size
< log_size
) {
570 vhost_dev_log_resize(dev
, log_size
+ VHOST_LOG_BUFFER
);
572 r
= dev
->vhost_ops
->vhost_set_mem_table(dev
, dev
->mem
);
574 /* To log less, can only decrease log size after table update. */
575 if (dev
->log_size
> log_size
+ VHOST_LOG_BUFFER
) {
576 vhost_dev_log_resize(dev
, log_size
);
578 dev
->memory_changed
= false;
581 static void vhost_region_add(MemoryListener
*listener
,
582 MemoryRegionSection
*section
)
584 struct vhost_dev
*dev
= container_of(listener
, struct vhost_dev
,
587 if (!vhost_section(section
)) {
591 ++dev
->n_mem_sections
;
592 dev
->mem_sections
= g_renew(MemoryRegionSection
, dev
->mem_sections
,
593 dev
->n_mem_sections
);
594 dev
->mem_sections
[dev
->n_mem_sections
- 1] = *section
;
595 memory_region_ref(section
->mr
);
596 vhost_set_memory(listener
, section
, true);
599 static void vhost_region_del(MemoryListener
*listener
,
600 MemoryRegionSection
*section
)
602 struct vhost_dev
*dev
= container_of(listener
, struct vhost_dev
,
606 if (!vhost_section(section
)) {
610 vhost_set_memory(listener
, section
, false);
611 memory_region_unref(section
->mr
);
612 for (i
= 0; i
< dev
->n_mem_sections
; ++i
) {
613 if (dev
->mem_sections
[i
].offset_within_address_space
614 == section
->offset_within_address_space
) {
615 --dev
->n_mem_sections
;
616 memmove(&dev
->mem_sections
[i
], &dev
->mem_sections
[i
+1],
617 (dev
->n_mem_sections
- i
) * sizeof(*dev
->mem_sections
));
623 static void vhost_region_nop(MemoryListener
*listener
,
624 MemoryRegionSection
*section
)
628 static int vhost_virtqueue_set_addr(struct vhost_dev
*dev
,
629 struct vhost_virtqueue
*vq
,
630 unsigned idx
, bool enable_log
)
632 struct vhost_vring_addr addr
= {
634 .desc_user_addr
= (uint64_t)(unsigned long)vq
->desc
,
635 .avail_user_addr
= (uint64_t)(unsigned long)vq
->avail
,
636 .used_user_addr
= (uint64_t)(unsigned long)vq
->used
,
637 .log_guest_addr
= vq
->used_phys
,
638 .flags
= enable_log
? (1 << VHOST_VRING_F_LOG
) : 0,
640 int r
= dev
->vhost_ops
->vhost_set_vring_addr(dev
, &addr
);
647 static int vhost_dev_set_features(struct vhost_dev
*dev
, bool enable_log
)
649 uint64_t features
= dev
->acked_features
;
652 features
|= 0x1ULL
<< VHOST_F_LOG_ALL
;
654 r
= dev
->vhost_ops
->vhost_set_features(dev
, features
);
655 return r
< 0 ? -errno
: 0;
658 static int vhost_dev_set_log(struct vhost_dev
*dev
, bool enable_log
)
661 r
= vhost_dev_set_features(dev
, enable_log
);
665 for (i
= 0; i
< dev
->nvqs
; ++i
) {
666 idx
= dev
->vhost_ops
->vhost_get_vq_index(dev
, dev
->vq_index
+ i
);
667 r
= vhost_virtqueue_set_addr(dev
, dev
->vqs
+ i
, idx
,
675 for (; i
>= 0; --i
) {
676 idx
= dev
->vhost_ops
->vhost_get_vq_index(dev
, dev
->vq_index
+ i
);
677 t
= vhost_virtqueue_set_addr(dev
, dev
->vqs
+ i
, idx
,
681 t
= vhost_dev_set_features(dev
, dev
->log_enabled
);
687 static int vhost_migration_log(MemoryListener
*listener
, int enable
)
689 struct vhost_dev
*dev
= container_of(listener
, struct vhost_dev
,
692 if (!!enable
== dev
->log_enabled
) {
696 dev
->log_enabled
= enable
;
700 r
= vhost_dev_set_log(dev
, false);
704 vhost_log_put(dev
, false);
708 vhost_dev_log_resize(dev
, vhost_get_log_size(dev
));
709 r
= vhost_dev_set_log(dev
, true);
714 dev
->log_enabled
= enable
;
718 static void vhost_log_global_start(MemoryListener
*listener
)
722 r
= vhost_migration_log(listener
, true);
728 static void vhost_log_global_stop(MemoryListener
*listener
)
732 r
= vhost_migration_log(listener
, false);
738 static void vhost_log_start(MemoryListener
*listener
,
739 MemoryRegionSection
*section
,
742 /* FIXME: implement */
745 static void vhost_log_stop(MemoryListener
*listener
,
746 MemoryRegionSection
*section
,
749 /* FIXME: implement */
752 static int vhost_virtqueue_set_vring_endian_legacy(struct vhost_dev
*dev
,
756 struct vhost_vring_state s
= {
757 .index
= vhost_vq_index
,
761 if (!dev
->vhost_ops
->vhost_set_vring_endian(dev
, &s
)) {
765 if (errno
== ENOTTY
) {
766 error_report("vhost does not support cross-endian");
773 static int vhost_virtqueue_start(struct vhost_dev
*dev
,
774 struct VirtIODevice
*vdev
,
775 struct vhost_virtqueue
*vq
,
780 int vhost_vq_index
= dev
->vhost_ops
->vhost_get_vq_index(dev
, idx
);
781 struct vhost_vring_file file
= {
782 .index
= vhost_vq_index
784 struct vhost_vring_state state
= {
785 .index
= vhost_vq_index
787 struct VirtQueue
*vvq
= virtio_get_queue(vdev
, idx
);
790 vq
->num
= state
.num
= virtio_queue_get_num(vdev
, idx
);
791 r
= dev
->vhost_ops
->vhost_set_vring_num(dev
, &state
);
796 state
.num
= virtio_queue_get_last_avail_idx(vdev
, idx
);
797 r
= dev
->vhost_ops
->vhost_set_vring_base(dev
, &state
);
802 if (!virtio_vdev_has_feature(vdev
, VIRTIO_F_VERSION_1
) &&
803 virtio_legacy_is_cross_endian(vdev
)) {
804 r
= vhost_virtqueue_set_vring_endian_legacy(dev
,
805 virtio_is_big_endian(vdev
),
812 s
= l
= virtio_queue_get_desc_size(vdev
, idx
);
813 a
= virtio_queue_get_desc_addr(vdev
, idx
);
814 vq
->desc
= cpu_physical_memory_map(a
, &l
, 0);
815 if (!vq
->desc
|| l
!= s
) {
817 goto fail_alloc_desc
;
819 s
= l
= virtio_queue_get_avail_size(vdev
, idx
);
820 a
= virtio_queue_get_avail_addr(vdev
, idx
);
821 vq
->avail
= cpu_physical_memory_map(a
, &l
, 0);
822 if (!vq
->avail
|| l
!= s
) {
824 goto fail_alloc_avail
;
826 vq
->used_size
= s
= l
= virtio_queue_get_used_size(vdev
, idx
);
827 vq
->used_phys
= a
= virtio_queue_get_used_addr(vdev
, idx
);
828 vq
->used
= cpu_physical_memory_map(a
, &l
, 1);
829 if (!vq
->used
|| l
!= s
) {
831 goto fail_alloc_used
;
834 vq
->ring_size
= s
= l
= virtio_queue_get_ring_size(vdev
, idx
);
835 vq
->ring_phys
= a
= virtio_queue_get_ring_addr(vdev
, idx
);
836 vq
->ring
= cpu_physical_memory_map(a
, &l
, 1);
837 if (!vq
->ring
|| l
!= s
) {
839 goto fail_alloc_ring
;
842 r
= vhost_virtqueue_set_addr(dev
, vq
, vhost_vq_index
, dev
->log_enabled
);
848 file
.fd
= event_notifier_get_fd(virtio_queue_get_host_notifier(vvq
));
849 r
= dev
->vhost_ops
->vhost_set_vring_kick(dev
, &file
);
855 /* Clear and discard previous events if any. */
856 event_notifier_test_and_clear(&vq
->masked_notifier
);
862 cpu_physical_memory_unmap(vq
->ring
, virtio_queue_get_ring_size(vdev
, idx
),
865 cpu_physical_memory_unmap(vq
->used
, virtio_queue_get_used_size(vdev
, idx
),
868 cpu_physical_memory_unmap(vq
->avail
, virtio_queue_get_avail_size(vdev
, idx
),
871 cpu_physical_memory_unmap(vq
->desc
, virtio_queue_get_desc_size(vdev
, idx
),
877 static void vhost_virtqueue_stop(struct vhost_dev
*dev
,
878 struct VirtIODevice
*vdev
,
879 struct vhost_virtqueue
*vq
,
882 int vhost_vq_index
= dev
->vhost_ops
->vhost_get_vq_index(dev
, idx
);
883 struct vhost_vring_state state
= {
884 .index
= vhost_vq_index
,
888 r
= dev
->vhost_ops
->vhost_get_vring_base(dev
, &state
);
890 fprintf(stderr
, "vhost VQ %d ring restore failed: %d\n", idx
, r
);
893 virtio_queue_set_last_avail_idx(vdev
, idx
, state
.num
);
894 virtio_queue_invalidate_signalled_used(vdev
, idx
);
896 /* In the cross-endian case, we need to reset the vring endianness to
897 * native as legacy devices expect so by default.
899 if (!virtio_vdev_has_feature(vdev
, VIRTIO_F_VERSION_1
) &&
900 virtio_legacy_is_cross_endian(vdev
)) {
901 r
= vhost_virtqueue_set_vring_endian_legacy(dev
,
902 !virtio_is_big_endian(vdev
),
905 error_report("failed to reset vring endianness");
910 cpu_physical_memory_unmap(vq
->ring
, virtio_queue_get_ring_size(vdev
, idx
),
911 0, virtio_queue_get_ring_size(vdev
, idx
));
912 cpu_physical_memory_unmap(vq
->used
, virtio_queue_get_used_size(vdev
, idx
),
913 1, virtio_queue_get_used_size(vdev
, idx
));
914 cpu_physical_memory_unmap(vq
->avail
, virtio_queue_get_avail_size(vdev
, idx
),
915 0, virtio_queue_get_avail_size(vdev
, idx
));
916 cpu_physical_memory_unmap(vq
->desc
, virtio_queue_get_desc_size(vdev
, idx
),
917 0, virtio_queue_get_desc_size(vdev
, idx
));
920 static void vhost_eventfd_add(MemoryListener
*listener
,
921 MemoryRegionSection
*section
,
922 bool match_data
, uint64_t data
, EventNotifier
*e
)
926 static void vhost_eventfd_del(MemoryListener
*listener
,
927 MemoryRegionSection
*section
,
928 bool match_data
, uint64_t data
, EventNotifier
*e
)
932 static int vhost_virtqueue_init(struct vhost_dev
*dev
,
933 struct vhost_virtqueue
*vq
, int n
)
935 int vhost_vq_index
= dev
->vhost_ops
->vhost_get_vq_index(dev
, n
);
936 struct vhost_vring_file file
= {
937 .index
= vhost_vq_index
,
939 int r
= event_notifier_init(&vq
->masked_notifier
, 0);
944 file
.fd
= event_notifier_get_fd(&vq
->masked_notifier
);
945 r
= dev
->vhost_ops
->vhost_set_vring_call(dev
, &file
);
952 event_notifier_cleanup(&vq
->masked_notifier
);
956 static void vhost_virtqueue_cleanup(struct vhost_virtqueue
*vq
)
958 event_notifier_cleanup(&vq
->masked_notifier
);
961 int vhost_dev_init(struct vhost_dev
*hdev
, void *opaque
,
962 VhostBackendType backend_type
)
967 hdev
->migration_blocker
= NULL
;
969 if (vhost_set_backend_type(hdev
, backend_type
) < 0) {
970 close((uintptr_t)opaque
);
974 if (hdev
->vhost_ops
->vhost_backend_init(hdev
, opaque
) < 0) {
975 close((uintptr_t)opaque
);
979 if (used_memslots
> hdev
->vhost_ops
->vhost_backend_memslots_limit(hdev
)) {
980 fprintf(stderr
, "vhost backend memory slots limit is less"
981 " than current number of present memory slots\n");
982 close((uintptr_t)opaque
);
985 QLIST_INSERT_HEAD(&vhost_devices
, hdev
, entry
);
987 r
= hdev
->vhost_ops
->vhost_set_owner(hdev
);
992 r
= hdev
->vhost_ops
->vhost_get_features(hdev
, &features
);
997 for (i
= 0; i
< hdev
->nvqs
; ++i
) {
998 r
= vhost_virtqueue_init(hdev
, hdev
->vqs
+ i
, hdev
->vq_index
+ i
);
1003 hdev
->features
= features
;
1005 hdev
->memory_listener
= (MemoryListener
) {
1006 .begin
= vhost_begin
,
1007 .commit
= vhost_commit
,
1008 .region_add
= vhost_region_add
,
1009 .region_del
= vhost_region_del
,
1010 .region_nop
= vhost_region_nop
,
1011 .log_start
= vhost_log_start
,
1012 .log_stop
= vhost_log_stop
,
1013 .log_sync
= vhost_log_sync
,
1014 .log_global_start
= vhost_log_global_start
,
1015 .log_global_stop
= vhost_log_global_stop
,
1016 .eventfd_add
= vhost_eventfd_add
,
1017 .eventfd_del
= vhost_eventfd_del
,
1021 if (hdev
->migration_blocker
== NULL
) {
1022 if (!(hdev
->features
& (0x1ULL
<< VHOST_F_LOG_ALL
))) {
1023 error_setg(&hdev
->migration_blocker
,
1024 "Migration disabled: vhost lacks VHOST_F_LOG_ALL feature.");
1025 } else if (!qemu_memfd_check()) {
1026 error_setg(&hdev
->migration_blocker
,
1027 "Migration disabled: failed to allocate shared memory");
1031 if (hdev
->migration_blocker
!= NULL
) {
1032 migrate_add_blocker(hdev
->migration_blocker
);
1035 hdev
->mem
= g_malloc0(offsetof(struct vhost_memory
, regions
));
1036 hdev
->n_mem_sections
= 0;
1037 hdev
->mem_sections
= NULL
;
1040 hdev
->log_enabled
= false;
1041 hdev
->started
= false;
1042 hdev
->memory_changed
= false;
1043 memory_listener_register(&hdev
->memory_listener
, &address_space_memory
);
1047 vhost_virtqueue_cleanup(hdev
->vqs
+ i
);
1051 hdev
->vhost_ops
->vhost_backend_cleanup(hdev
);
1052 QLIST_REMOVE(hdev
, entry
);
1056 void vhost_dev_cleanup(struct vhost_dev
*hdev
)
1059 for (i
= 0; i
< hdev
->nvqs
; ++i
) {
1060 vhost_virtqueue_cleanup(hdev
->vqs
+ i
);
1062 memory_listener_unregister(&hdev
->memory_listener
);
1063 if (hdev
->migration_blocker
) {
1064 migrate_del_blocker(hdev
->migration_blocker
);
1065 error_free(hdev
->migration_blocker
);
1068 g_free(hdev
->mem_sections
);
1069 hdev
->vhost_ops
->vhost_backend_cleanup(hdev
);
1070 QLIST_REMOVE(hdev
, entry
);
1073 /* Stop processing guest IO notifications in qemu.
1074 * Start processing them in vhost in kernel.
1076 int vhost_dev_enable_notifiers(struct vhost_dev
*hdev
, VirtIODevice
*vdev
)
1078 BusState
*qbus
= BUS(qdev_get_parent_bus(DEVICE(vdev
)));
1079 VirtioBusState
*vbus
= VIRTIO_BUS(qbus
);
1080 VirtioBusClass
*k
= VIRTIO_BUS_GET_CLASS(vbus
);
1082 if (!k
->set_host_notifier
) {
1083 fprintf(stderr
, "binding does not support host notifiers\n");
1088 for (i
= 0; i
< hdev
->nvqs
; ++i
) {
1089 r
= k
->set_host_notifier(qbus
->parent
, hdev
->vq_index
+ i
, true);
1091 fprintf(stderr
, "vhost VQ %d notifier binding failed: %d\n", i
, -r
);
1099 e
= k
->set_host_notifier(qbus
->parent
, hdev
->vq_index
+ i
, false);
1101 fprintf(stderr
, "vhost VQ %d notifier cleanup error: %d\n", i
, -r
);
1110 /* Stop processing guest IO notifications in vhost.
1111 * Start processing them in qemu.
1112 * This might actually run the qemu handlers right away,
1113 * so virtio in qemu must be completely setup when this is called.
1115 void vhost_dev_disable_notifiers(struct vhost_dev
*hdev
, VirtIODevice
*vdev
)
1117 BusState
*qbus
= BUS(qdev_get_parent_bus(DEVICE(vdev
)));
1118 VirtioBusState
*vbus
= VIRTIO_BUS(qbus
);
1119 VirtioBusClass
*k
= VIRTIO_BUS_GET_CLASS(vbus
);
1122 for (i
= 0; i
< hdev
->nvqs
; ++i
) {
1123 r
= k
->set_host_notifier(qbus
->parent
, hdev
->vq_index
+ i
, false);
1125 fprintf(stderr
, "vhost VQ %d notifier cleanup failed: %d\n", i
, -r
);
1132 /* Test and clear event pending status.
1133 * Should be called after unmask to avoid losing events.
1135 bool vhost_virtqueue_pending(struct vhost_dev
*hdev
, int n
)
1137 struct vhost_virtqueue
*vq
= hdev
->vqs
+ n
- hdev
->vq_index
;
1138 assert(n
>= hdev
->vq_index
&& n
< hdev
->vq_index
+ hdev
->nvqs
);
1139 return event_notifier_test_and_clear(&vq
->masked_notifier
);
1142 /* Mask/unmask events from this vq. */
1143 void vhost_virtqueue_mask(struct vhost_dev
*hdev
, VirtIODevice
*vdev
, int n
,
1146 struct VirtQueue
*vvq
= virtio_get_queue(vdev
, n
);
1147 int r
, index
= n
- hdev
->vq_index
;
1148 struct vhost_vring_file file
;
1151 file
.fd
= event_notifier_get_fd(&hdev
->vqs
[index
].masked_notifier
);
1153 file
.fd
= event_notifier_get_fd(virtio_queue_get_guest_notifier(vvq
));
1156 file
.index
= hdev
->vhost_ops
->vhost_get_vq_index(hdev
, n
);
1157 r
= hdev
->vhost_ops
->vhost_set_vring_call(hdev
, &file
);
1161 uint64_t vhost_get_features(struct vhost_dev
*hdev
, const int *feature_bits
,
1164 const int *bit
= feature_bits
;
1165 while (*bit
!= VHOST_INVALID_FEATURE_BIT
) {
1166 uint64_t bit_mask
= (1ULL << *bit
);
1167 if (!(hdev
->features
& bit_mask
)) {
1168 features
&= ~bit_mask
;
1175 void vhost_ack_features(struct vhost_dev
*hdev
, const int *feature_bits
,
1178 const int *bit
= feature_bits
;
1179 while (*bit
!= VHOST_INVALID_FEATURE_BIT
) {
1180 uint64_t bit_mask
= (1ULL << *bit
);
1181 if (features
& bit_mask
) {
1182 hdev
->acked_features
|= bit_mask
;
1188 /* Host notifiers must be enabled at this point. */
1189 int vhost_dev_start(struct vhost_dev
*hdev
, VirtIODevice
*vdev
)
1193 hdev
->started
= true;
1195 r
= vhost_dev_set_features(hdev
, hdev
->log_enabled
);
1199 r
= hdev
->vhost_ops
->vhost_set_mem_table(hdev
, hdev
->mem
);
1204 for (i
= 0; i
< hdev
->nvqs
; ++i
) {
1205 r
= vhost_virtqueue_start(hdev
,
1208 hdev
->vq_index
+ i
);
1214 if (hdev
->log_enabled
) {
1217 hdev
->log_size
= vhost_get_log_size(hdev
);
1218 hdev
->log
= vhost_log_get(hdev
->log_size
,
1219 vhost_dev_log_is_shared(hdev
));
1220 log_base
= (uintptr_t)hdev
->log
->log
;
1221 r
= hdev
->vhost_ops
->vhost_set_log_base(hdev
,
1222 hdev
->log_size
? log_base
: 0,
1232 vhost_log_put(hdev
, false);
1235 vhost_virtqueue_stop(hdev
,
1238 hdev
->vq_index
+ i
);
1244 hdev
->started
= false;
1248 /* Host notifiers must be enabled at this point. */
1249 void vhost_dev_stop(struct vhost_dev
*hdev
, VirtIODevice
*vdev
)
1253 for (i
= 0; i
< hdev
->nvqs
; ++i
) {
1254 vhost_virtqueue_stop(hdev
,
1257 hdev
->vq_index
+ i
);
1260 vhost_log_put(hdev
, true);
1261 hdev
->started
= false;