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"
19 #include "qemu/atomic.h"
20 #include "qemu/range.h"
21 #include "qemu/error-report.h"
22 #include "qemu/memfd.h"
23 #include "standard-headers/linux/vhost_types.h"
24 #include "exec/address-spaces.h"
25 #include "hw/virtio/virtio-bus.h"
26 #include "hw/virtio/virtio-access.h"
27 #include "migration/blocker.h"
28 #include "migration/qemu-file-types.h"
29 #include "sysemu/dma.h"
30 #include "sysemu/tcg.h"
33 /* enabled until disconnected backend stabilizes */
34 #define _VHOST_DEBUG 1
37 #define VHOST_OPS_DEBUG(fmt, ...) \
38 do { error_report(fmt ": %s (%d)", ## __VA_ARGS__, \
39 strerror(errno), errno); } while (0)
41 #define VHOST_OPS_DEBUG(fmt, ...) \
45 static struct vhost_log
*vhost_log
;
46 static struct vhost_log
*vhost_log_shm
;
48 static unsigned int used_memslots
;
49 static QLIST_HEAD(, vhost_dev
) vhost_devices
=
50 QLIST_HEAD_INITIALIZER(vhost_devices
);
52 bool vhost_has_free_slot(void)
54 unsigned int slots_limit
= ~0U;
55 struct vhost_dev
*hdev
;
57 QLIST_FOREACH(hdev
, &vhost_devices
, entry
) {
58 unsigned int r
= hdev
->vhost_ops
->vhost_backend_memslots_limit(hdev
);
59 slots_limit
= MIN(slots_limit
, r
);
61 return slots_limit
> used_memslots
;
64 static void vhost_dev_sync_region(struct vhost_dev
*dev
,
65 MemoryRegionSection
*section
,
66 uint64_t mfirst
, uint64_t mlast
,
67 uint64_t rfirst
, uint64_t rlast
)
69 vhost_log_chunk_t
*log
= dev
->log
->log
;
71 uint64_t start
= MAX(mfirst
, rfirst
);
72 uint64_t end
= MIN(mlast
, rlast
);
73 vhost_log_chunk_t
*from
= log
+ start
/ VHOST_LOG_CHUNK
;
74 vhost_log_chunk_t
*to
= log
+ end
/ VHOST_LOG_CHUNK
+ 1;
75 uint64_t addr
= QEMU_ALIGN_DOWN(start
, VHOST_LOG_CHUNK
);
80 assert(end
/ VHOST_LOG_CHUNK
< dev
->log_size
);
81 assert(start
/ VHOST_LOG_CHUNK
< dev
->log_size
);
83 for (;from
< to
; ++from
) {
84 vhost_log_chunk_t log
;
85 /* We first check with non-atomic: much cheaper,
86 * and we expect non-dirty to be the common case. */
88 addr
+= VHOST_LOG_CHUNK
;
91 /* Data must be read atomically. We don't really need barrier semantics
92 * but it's easier to use atomic_* than roll our own. */
93 log
= qatomic_xchg(from
, 0);
97 hwaddr section_offset
;
99 page_addr
= addr
+ bit
* VHOST_LOG_PAGE
;
100 section_offset
= page_addr
- section
->offset_within_address_space
;
101 mr_offset
= section_offset
+ section
->offset_within_region
;
102 memory_region_set_dirty(section
->mr
, mr_offset
, VHOST_LOG_PAGE
);
103 log
&= ~(0x1ull
<< bit
);
105 addr
+= VHOST_LOG_CHUNK
;
109 static int vhost_sync_dirty_bitmap(struct vhost_dev
*dev
,
110 MemoryRegionSection
*section
,
118 if (!dev
->log_enabled
|| !dev
->started
) {
121 start_addr
= section
->offset_within_address_space
;
122 end_addr
= range_get_last(start_addr
, int128_get64(section
->size
));
123 start_addr
= MAX(first
, start_addr
);
124 end_addr
= MIN(last
, end_addr
);
126 for (i
= 0; i
< dev
->mem
->nregions
; ++i
) {
127 struct vhost_memory_region
*reg
= dev
->mem
->regions
+ i
;
128 vhost_dev_sync_region(dev
, section
, start_addr
, end_addr
,
129 reg
->guest_phys_addr
,
130 range_get_last(reg
->guest_phys_addr
,
133 for (i
= 0; i
< dev
->nvqs
; ++i
) {
134 struct vhost_virtqueue
*vq
= dev
->vqs
+ i
;
136 if (!vq
->used_phys
&& !vq
->used_size
) {
140 vhost_dev_sync_region(dev
, section
, start_addr
, end_addr
, vq
->used_phys
,
141 range_get_last(vq
->used_phys
, vq
->used_size
));
146 static void vhost_log_sync(MemoryListener
*listener
,
147 MemoryRegionSection
*section
)
149 struct vhost_dev
*dev
= container_of(listener
, struct vhost_dev
,
151 vhost_sync_dirty_bitmap(dev
, section
, 0x0, ~0x0ULL
);
154 static void vhost_log_sync_range(struct vhost_dev
*dev
,
155 hwaddr first
, hwaddr last
)
158 /* FIXME: this is N^2 in number of sections */
159 for (i
= 0; i
< dev
->n_mem_sections
; ++i
) {
160 MemoryRegionSection
*section
= &dev
->mem_sections
[i
];
161 vhost_sync_dirty_bitmap(dev
, section
, first
, last
);
165 static uint64_t vhost_get_log_size(struct vhost_dev
*dev
)
167 uint64_t log_size
= 0;
169 for (i
= 0; i
< dev
->mem
->nregions
; ++i
) {
170 struct vhost_memory_region
*reg
= dev
->mem
->regions
+ i
;
171 uint64_t last
= range_get_last(reg
->guest_phys_addr
,
173 log_size
= MAX(log_size
, last
/ VHOST_LOG_CHUNK
+ 1);
178 static struct vhost_log
*vhost_log_alloc(uint64_t size
, bool share
)
181 struct vhost_log
*log
;
182 uint64_t logsize
= size
* sizeof(*(log
->log
));
185 log
= g_new0(struct vhost_log
, 1);
187 log
->log
= qemu_memfd_alloc("vhost-log", logsize
,
188 F_SEAL_GROW
| F_SEAL_SHRINK
| F_SEAL_SEAL
,
191 error_report_err(err
);
195 memset(log
->log
, 0, logsize
);
197 log
->log
= g_malloc0(logsize
);
207 static struct vhost_log
*vhost_log_get(uint64_t size
, bool share
)
209 struct vhost_log
*log
= share
? vhost_log_shm
: vhost_log
;
211 if (!log
|| log
->size
!= size
) {
212 log
= vhost_log_alloc(size
, share
);
225 static void vhost_log_put(struct vhost_dev
*dev
, bool sync
)
227 struct vhost_log
*log
= dev
->log
;
234 if (log
->refcnt
== 0) {
235 /* Sync only the range covered by the old log */
236 if (dev
->log_size
&& sync
) {
237 vhost_log_sync_range(dev
, 0, dev
->log_size
* VHOST_LOG_CHUNK
- 1);
240 if (vhost_log
== log
) {
243 } else if (vhost_log_shm
== log
) {
244 qemu_memfd_free(log
->log
, log
->size
* sizeof(*(log
->log
)),
246 vhost_log_shm
= NULL
;
256 static bool vhost_dev_log_is_shared(struct vhost_dev
*dev
)
258 return dev
->vhost_ops
->vhost_requires_shm_log
&&
259 dev
->vhost_ops
->vhost_requires_shm_log(dev
);
262 static inline void vhost_dev_log_resize(struct vhost_dev
*dev
, uint64_t size
)
264 struct vhost_log
*log
= vhost_log_get(size
, vhost_dev_log_is_shared(dev
));
265 uint64_t log_base
= (uintptr_t)log
->log
;
268 /* inform backend of log switching, this must be done before
269 releasing the current log, to ensure no logging is lost */
270 r
= dev
->vhost_ops
->vhost_set_log_base(dev
, log_base
, log
);
272 VHOST_OPS_DEBUG("vhost_set_log_base failed");
275 vhost_log_put(dev
, true);
277 dev
->log_size
= size
;
280 static int vhost_dev_has_iommu(struct vhost_dev
*dev
)
282 VirtIODevice
*vdev
= dev
->vdev
;
285 * For vhost, VIRTIO_F_IOMMU_PLATFORM means the backend support
286 * incremental memory mapping API via IOTLB API. For platform that
287 * does not have IOMMU, there's no need to enable this feature
288 * which may cause unnecessary IOTLB miss/update trnasactions.
290 return vdev
->dma_as
!= &address_space_memory
&&
291 virtio_host_has_feature(vdev
, VIRTIO_F_IOMMU_PLATFORM
);
294 static void *vhost_memory_map(struct vhost_dev
*dev
, hwaddr addr
,
295 hwaddr
*plen
, bool is_write
)
297 if (!vhost_dev_has_iommu(dev
)) {
298 return cpu_physical_memory_map(addr
, plen
, is_write
);
300 return (void *)(uintptr_t)addr
;
304 static void vhost_memory_unmap(struct vhost_dev
*dev
, void *buffer
,
305 hwaddr len
, int is_write
,
308 if (!vhost_dev_has_iommu(dev
)) {
309 cpu_physical_memory_unmap(buffer
, len
, is_write
, access_len
);
313 static int vhost_verify_ring_part_mapping(void *ring_hva
,
320 uint64_t hva_ring_offset
;
321 uint64_t ring_last
= range_get_last(ring_gpa
, ring_size
);
322 uint64_t reg_last
= range_get_last(reg_gpa
, reg_size
);
324 if (ring_last
< reg_gpa
|| ring_gpa
> reg_last
) {
327 /* check that whole ring's is mapped */
328 if (ring_last
> reg_last
) {
331 /* check that ring's MemoryRegion wasn't replaced */
332 hva_ring_offset
= ring_gpa
- reg_gpa
;
333 if (ring_hva
!= reg_hva
+ hva_ring_offset
) {
340 static int vhost_verify_ring_mappings(struct vhost_dev
*dev
,
347 const char *part_name
[] = {
353 if (vhost_dev_has_iommu(dev
)) {
357 for (i
= 0; i
< dev
->nvqs
; ++i
) {
358 struct vhost_virtqueue
*vq
= dev
->vqs
+ i
;
360 if (vq
->desc_phys
== 0) {
365 r
= vhost_verify_ring_part_mapping(
366 vq
->desc
, vq
->desc_phys
, vq
->desc_size
,
367 reg_hva
, reg_gpa
, reg_size
);
373 r
= vhost_verify_ring_part_mapping(
374 vq
->avail
, vq
->avail_phys
, vq
->avail_size
,
375 reg_hva
, reg_gpa
, reg_size
);
381 r
= vhost_verify_ring_part_mapping(
382 vq
->used
, vq
->used_phys
, vq
->used_size
,
383 reg_hva
, reg_gpa
, reg_size
);
390 error_report("Unable to map %s for ring %d", part_name
[j
], i
);
391 } else if (r
== -EBUSY
) {
392 error_report("%s relocated for ring %d", part_name
[j
], i
);
398 * vhost_section: identify sections needed for vhost access
400 * We only care about RAM sections here (where virtqueue and guest
401 * internals accessed by virtio might live). If we find one we still
402 * allow the backend to potentially filter it out of our list.
404 static bool vhost_section(struct vhost_dev
*dev
, MemoryRegionSection
*section
)
406 MemoryRegion
*mr
= section
->mr
;
408 if (memory_region_is_ram(mr
) && !memory_region_is_rom(mr
)) {
409 uint8_t dirty_mask
= memory_region_get_dirty_log_mask(mr
);
410 uint8_t handled_dirty
;
413 * Kernel based vhost doesn't handle any block which is doing
414 * dirty-tracking other than migration for which it has
415 * specific logging support. However for TCG the kernel never
416 * gets involved anyway so we can also ignore it's
417 * self-modiying code detection flags. However a vhost-user
418 * client could still confuse a TCG guest if it re-writes
419 * executable memory that has already been translated.
421 handled_dirty
= (1 << DIRTY_MEMORY_MIGRATION
) |
422 (1 << DIRTY_MEMORY_CODE
);
424 if (dirty_mask
& ~handled_dirty
) {
425 trace_vhost_reject_section(mr
->name
, 1);
429 if (dev
->vhost_ops
->vhost_backend_mem_section_filter
&&
430 !dev
->vhost_ops
->vhost_backend_mem_section_filter(dev
, section
)) {
431 trace_vhost_reject_section(mr
->name
, 2);
435 trace_vhost_section(mr
->name
);
438 trace_vhost_reject_section(mr
->name
, 3);
443 static void vhost_begin(MemoryListener
*listener
)
445 struct vhost_dev
*dev
= container_of(listener
, struct vhost_dev
,
447 dev
->tmp_sections
= NULL
;
448 dev
->n_tmp_sections
= 0;
451 static void vhost_commit(MemoryListener
*listener
)
453 struct vhost_dev
*dev
= container_of(listener
, struct vhost_dev
,
455 MemoryRegionSection
*old_sections
;
461 bool changed
= false;
463 /* Note we can be called before the device is started, but then
464 * starting the device calls set_mem_table, so we need to have
465 * built the data structures.
467 old_sections
= dev
->mem_sections
;
468 n_old_sections
= dev
->n_mem_sections
;
469 dev
->mem_sections
= dev
->tmp_sections
;
470 dev
->n_mem_sections
= dev
->n_tmp_sections
;
472 if (dev
->n_mem_sections
!= n_old_sections
) {
475 /* Same size, lets check the contents */
476 for (int i
= 0; i
< n_old_sections
; i
++) {
477 if (!MemoryRegionSection_eq(&old_sections
[i
],
478 &dev
->mem_sections
[i
])) {
485 trace_vhost_commit(dev
->started
, changed
);
490 /* Rebuild the regions list from the new sections list */
491 regions_size
= offsetof(struct vhost_memory
, regions
) +
492 dev
->n_mem_sections
* sizeof dev
->mem
->regions
[0];
493 dev
->mem
= g_realloc(dev
->mem
, regions_size
);
494 dev
->mem
->nregions
= dev
->n_mem_sections
;
495 used_memslots
= dev
->mem
->nregions
;
496 for (i
= 0; i
< dev
->n_mem_sections
; i
++) {
497 struct vhost_memory_region
*cur_vmr
= dev
->mem
->regions
+ i
;
498 struct MemoryRegionSection
*mrs
= dev
->mem_sections
+ i
;
500 cur_vmr
->guest_phys_addr
= mrs
->offset_within_address_space
;
501 cur_vmr
->memory_size
= int128_get64(mrs
->size
);
502 cur_vmr
->userspace_addr
=
503 (uintptr_t)memory_region_get_ram_ptr(mrs
->mr
) +
504 mrs
->offset_within_region
;
505 cur_vmr
->flags_padding
= 0;
512 for (i
= 0; i
< dev
->mem
->nregions
; i
++) {
513 if (vhost_verify_ring_mappings(dev
,
514 (void *)(uintptr_t)dev
->mem
->regions
[i
].userspace_addr
,
515 dev
->mem
->regions
[i
].guest_phys_addr
,
516 dev
->mem
->regions
[i
].memory_size
)) {
517 error_report("Verify ring failure on region %d", i
);
522 if (!dev
->log_enabled
) {
523 r
= dev
->vhost_ops
->vhost_set_mem_table(dev
, dev
->mem
);
525 VHOST_OPS_DEBUG("vhost_set_mem_table failed");
529 log_size
= vhost_get_log_size(dev
);
530 /* We allocate an extra 4K bytes to log,
531 * to reduce the * number of reallocations. */
532 #define VHOST_LOG_BUFFER (0x1000 / sizeof *dev->log)
533 /* To log more, must increase log size before table update. */
534 if (dev
->log_size
< log_size
) {
535 vhost_dev_log_resize(dev
, log_size
+ VHOST_LOG_BUFFER
);
537 r
= dev
->vhost_ops
->vhost_set_mem_table(dev
, dev
->mem
);
539 VHOST_OPS_DEBUG("vhost_set_mem_table failed");
541 /* To log less, can only decrease log size after table update. */
542 if (dev
->log_size
> log_size
+ VHOST_LOG_BUFFER
) {
543 vhost_dev_log_resize(dev
, log_size
);
547 /* Deref the old list of sections, this must happen _after_ the
548 * vhost_set_mem_table to ensure the client isn't still using the
549 * section we're about to unref.
551 while (n_old_sections
--) {
552 memory_region_unref(old_sections
[n_old_sections
].mr
);
554 g_free(old_sections
);
558 /* Adds the section data to the tmp_section structure.
559 * It relies on the listener calling us in memory address order
560 * and for each region (via the _add and _nop methods) to
563 static void vhost_region_add_section(struct vhost_dev
*dev
,
564 MemoryRegionSection
*section
)
566 bool need_add
= true;
567 uint64_t mrs_size
= int128_get64(section
->size
);
568 uint64_t mrs_gpa
= section
->offset_within_address_space
;
569 uintptr_t mrs_host
= (uintptr_t)memory_region_get_ram_ptr(section
->mr
) +
570 section
->offset_within_region
;
571 RAMBlock
*mrs_rb
= section
->mr
->ram_block
;
573 trace_vhost_region_add_section(section
->mr
->name
, mrs_gpa
, mrs_size
,
576 if (dev
->vhost_ops
->backend_type
== VHOST_BACKEND_TYPE_USER
) {
577 /* Round the section to it's page size */
578 /* First align the start down to a page boundary */
579 size_t mrs_page
= qemu_ram_pagesize(mrs_rb
);
580 uint64_t alignage
= mrs_host
& (mrs_page
- 1);
582 mrs_host
-= alignage
;
583 mrs_size
+= alignage
;
586 /* Now align the size up to a page boundary */
587 alignage
= mrs_size
& (mrs_page
- 1);
589 mrs_size
+= mrs_page
- alignage
;
591 trace_vhost_region_add_section_aligned(section
->mr
->name
, mrs_gpa
,
595 if (dev
->n_tmp_sections
) {
596 /* Since we already have at least one section, lets see if
597 * this extends it; since we're scanning in order, we only
598 * have to look at the last one, and the FlatView that calls
599 * us shouldn't have overlaps.
601 MemoryRegionSection
*prev_sec
= dev
->tmp_sections
+
602 (dev
->n_tmp_sections
- 1);
603 uint64_t prev_gpa_start
= prev_sec
->offset_within_address_space
;
604 uint64_t prev_size
= int128_get64(prev_sec
->size
);
605 uint64_t prev_gpa_end
= range_get_last(prev_gpa_start
, prev_size
);
606 uint64_t prev_host_start
=
607 (uintptr_t)memory_region_get_ram_ptr(prev_sec
->mr
) +
608 prev_sec
->offset_within_region
;
609 uint64_t prev_host_end
= range_get_last(prev_host_start
, prev_size
);
611 if (mrs_gpa
<= (prev_gpa_end
+ 1)) {
612 /* OK, looks like overlapping/intersecting - it's possible that
613 * the rounding to page sizes has made them overlap, but they should
614 * match up in the same RAMBlock if they do.
616 if (mrs_gpa
< prev_gpa_start
) {
617 error_report("%s:Section '%s' rounded to %"PRIx64
618 " prior to previous '%s' %"PRIx64
,
619 __func__
, section
->mr
->name
, mrs_gpa
,
620 prev_sec
->mr
->name
, prev_gpa_start
);
621 /* A way to cleanly fail here would be better */
624 /* Offset from the start of the previous GPA to this GPA */
625 size_t offset
= mrs_gpa
- prev_gpa_start
;
627 if (prev_host_start
+ offset
== mrs_host
&&
628 section
->mr
== prev_sec
->mr
&&
629 (!dev
->vhost_ops
->vhost_backend_can_merge
||
630 dev
->vhost_ops
->vhost_backend_can_merge(dev
,
632 prev_host_start
, prev_size
))) {
633 uint64_t max_end
= MAX(prev_host_end
, mrs_host
+ mrs_size
);
635 prev_sec
->offset_within_address_space
=
636 MIN(prev_gpa_start
, mrs_gpa
);
637 prev_sec
->offset_within_region
=
638 MIN(prev_host_start
, mrs_host
) -
639 (uintptr_t)memory_region_get_ram_ptr(prev_sec
->mr
);
640 prev_sec
->size
= int128_make64(max_end
- MIN(prev_host_start
,
642 trace_vhost_region_add_section_merge(section
->mr
->name
,
643 int128_get64(prev_sec
->size
),
644 prev_sec
->offset_within_address_space
,
645 prev_sec
->offset_within_region
);
647 /* adjoining regions are fine, but overlapping ones with
648 * different blocks/offsets shouldn't happen
650 if (mrs_gpa
!= prev_gpa_end
+ 1) {
651 error_report("%s: Overlapping but not coherent sections "
661 ++dev
->n_tmp_sections
;
662 dev
->tmp_sections
= g_renew(MemoryRegionSection
, dev
->tmp_sections
,
663 dev
->n_tmp_sections
);
664 dev
->tmp_sections
[dev
->n_tmp_sections
- 1] = *section
;
665 /* The flatview isn't stable and we don't use it, making it NULL
666 * means we can memcmp the list.
668 dev
->tmp_sections
[dev
->n_tmp_sections
- 1].fv
= NULL
;
669 memory_region_ref(section
->mr
);
673 /* Used for both add and nop callbacks */
674 static void vhost_region_addnop(MemoryListener
*listener
,
675 MemoryRegionSection
*section
)
677 struct vhost_dev
*dev
= container_of(listener
, struct vhost_dev
,
680 if (!vhost_section(dev
, section
)) {
683 vhost_region_add_section(dev
, section
);
686 static void vhost_iommu_unmap_notify(IOMMUNotifier
*n
, IOMMUTLBEntry
*iotlb
)
688 struct vhost_iommu
*iommu
= container_of(n
, struct vhost_iommu
, n
);
689 struct vhost_dev
*hdev
= iommu
->hdev
;
690 hwaddr iova
= iotlb
->iova
+ iommu
->iommu_offset
;
692 if (vhost_backend_invalidate_device_iotlb(hdev
, iova
,
693 iotlb
->addr_mask
+ 1)) {
694 error_report("Fail to invalidate device iotlb");
698 static void vhost_iommu_region_add(MemoryListener
*listener
,
699 MemoryRegionSection
*section
)
701 struct vhost_dev
*dev
= container_of(listener
, struct vhost_dev
,
703 struct vhost_iommu
*iommu
;
706 IOMMUMemoryRegion
*iommu_mr
;
709 if (!memory_region_is_iommu(section
->mr
)) {
713 iommu_mr
= IOMMU_MEMORY_REGION(section
->mr
);
715 iommu
= g_malloc0(sizeof(*iommu
));
716 end
= int128_add(int128_make64(section
->offset_within_region
),
718 end
= int128_sub(end
, int128_one());
719 iommu_idx
= memory_region_iommu_attrs_to_index(iommu_mr
,
720 MEMTXATTRS_UNSPECIFIED
);
721 iommu_notifier_init(&iommu
->n
, vhost_iommu_unmap_notify
,
722 IOMMU_NOTIFIER_DEVIOTLB_UNMAP
,
723 section
->offset_within_region
,
726 iommu
->mr
= section
->mr
;
727 iommu
->iommu_offset
= section
->offset_within_address_space
-
728 section
->offset_within_region
;
730 ret
= memory_region_register_iommu_notifier(section
->mr
, &iommu
->n
, NULL
);
733 * Some vIOMMUs do not support dev-iotlb yet. If so, try to use the
734 * UNMAP legacy message
736 iommu
->n
.notifier_flags
= IOMMU_NOTIFIER_UNMAP
;
737 memory_region_register_iommu_notifier(section
->mr
, &iommu
->n
,
740 QLIST_INSERT_HEAD(&dev
->iommu_list
, iommu
, iommu_next
);
741 /* TODO: can replay help performance here? */
744 static void vhost_iommu_region_del(MemoryListener
*listener
,
745 MemoryRegionSection
*section
)
747 struct vhost_dev
*dev
= container_of(listener
, struct vhost_dev
,
749 struct vhost_iommu
*iommu
;
751 if (!memory_region_is_iommu(section
->mr
)) {
755 QLIST_FOREACH(iommu
, &dev
->iommu_list
, iommu_next
) {
756 if (iommu
->mr
== section
->mr
&&
757 iommu
->n
.start
== section
->offset_within_region
) {
758 memory_region_unregister_iommu_notifier(iommu
->mr
,
760 QLIST_REMOVE(iommu
, iommu_next
);
767 static int vhost_virtqueue_set_addr(struct vhost_dev
*dev
,
768 struct vhost_virtqueue
*vq
,
769 unsigned idx
, bool enable_log
)
771 struct vhost_vring_addr addr
;
773 memset(&addr
, 0, sizeof(struct vhost_vring_addr
));
775 if (dev
->vhost_ops
->vhost_vq_get_addr
) {
776 r
= dev
->vhost_ops
->vhost_vq_get_addr(dev
, &addr
, vq
);
778 VHOST_OPS_DEBUG("vhost_vq_get_addr failed");
782 addr
.desc_user_addr
= (uint64_t)(unsigned long)vq
->desc
;
783 addr
.avail_user_addr
= (uint64_t)(unsigned long)vq
->avail
;
784 addr
.used_user_addr
= (uint64_t)(unsigned long)vq
->used
;
787 addr
.log_guest_addr
= vq
->used_phys
;
788 addr
.flags
= enable_log
? (1 << VHOST_VRING_F_LOG
) : 0;
789 r
= dev
->vhost_ops
->vhost_set_vring_addr(dev
, &addr
);
791 VHOST_OPS_DEBUG("vhost_set_vring_addr failed");
797 static int vhost_dev_set_features(struct vhost_dev
*dev
,
800 uint64_t features
= dev
->acked_features
;
803 features
|= 0x1ULL
<< VHOST_F_LOG_ALL
;
805 if (!vhost_dev_has_iommu(dev
)) {
806 features
&= ~(0x1ULL
<< VIRTIO_F_IOMMU_PLATFORM
);
808 if (dev
->vhost_ops
->vhost_force_iommu
) {
809 if (dev
->vhost_ops
->vhost_force_iommu(dev
) == true) {
810 features
|= 0x1ULL
<< VIRTIO_F_IOMMU_PLATFORM
;
813 r
= dev
->vhost_ops
->vhost_set_features(dev
, features
);
815 VHOST_OPS_DEBUG("vhost_set_features failed");
818 if (dev
->vhost_ops
->vhost_set_backend_cap
) {
819 r
= dev
->vhost_ops
->vhost_set_backend_cap(dev
);
821 VHOST_OPS_DEBUG("vhost_set_backend_cap failed");
827 return r
< 0 ? -errno
: 0;
830 static int vhost_dev_set_log(struct vhost_dev
*dev
, bool enable_log
)
835 r
= vhost_dev_set_features(dev
, enable_log
);
839 for (i
= 0; i
< dev
->nvqs
; ++i
) {
840 idx
= dev
->vhost_ops
->vhost_get_vq_index(dev
, dev
->vq_index
+ i
);
841 addr
= virtio_queue_get_desc_addr(dev
->vdev
, idx
);
844 * The queue might not be ready for start. If this
845 * is the case there is no reason to continue the process.
846 * The similar logic is used by the vhost_virtqueue_start()
851 r
= vhost_virtqueue_set_addr(dev
, dev
->vqs
+ i
, idx
,
859 for (; i
>= 0; --i
) {
860 idx
= dev
->vhost_ops
->vhost_get_vq_index(dev
, dev
->vq_index
+ i
);
861 vhost_virtqueue_set_addr(dev
, dev
->vqs
+ i
, idx
,
864 vhost_dev_set_features(dev
, dev
->log_enabled
);
869 static int vhost_migration_log(MemoryListener
*listener
, bool enable
)
871 struct vhost_dev
*dev
= container_of(listener
, struct vhost_dev
,
874 if (enable
== dev
->log_enabled
) {
878 dev
->log_enabled
= enable
;
884 r
= vhost_dev_set_log(dev
, false);
886 goto check_dev_state
;
888 vhost_log_put(dev
, false);
890 vhost_dev_log_resize(dev
, vhost_get_log_size(dev
));
891 r
= vhost_dev_set_log(dev
, true);
893 goto check_dev_state
;
898 dev
->log_enabled
= enable
;
900 * vhost-user-* devices could change their state during log
901 * initialization due to disconnect. So check dev state after
902 * vhost communication.
906 * Since device is in the stopped state, it is okay for
907 * migration. Return success.
912 /* An error occurred. */
913 dev
->log_enabled
= false;
919 static void vhost_log_global_start(MemoryListener
*listener
)
923 r
= vhost_migration_log(listener
, true);
929 static void vhost_log_global_stop(MemoryListener
*listener
)
933 r
= vhost_migration_log(listener
, false);
939 static void vhost_log_start(MemoryListener
*listener
,
940 MemoryRegionSection
*section
,
943 /* FIXME: implement */
946 static void vhost_log_stop(MemoryListener
*listener
,
947 MemoryRegionSection
*section
,
950 /* FIXME: implement */
953 /* The vhost driver natively knows how to handle the vrings of non
954 * cross-endian legacy devices and modern devices. Only legacy devices
955 * exposed to a bi-endian guest may require the vhost driver to use a
956 * specific endianness.
958 static inline bool vhost_needs_vring_endian(VirtIODevice
*vdev
)
960 if (virtio_vdev_has_feature(vdev
, VIRTIO_F_VERSION_1
)) {
963 #ifdef HOST_WORDS_BIGENDIAN
964 return vdev
->device_endian
== VIRTIO_DEVICE_ENDIAN_LITTLE
;
966 return vdev
->device_endian
== VIRTIO_DEVICE_ENDIAN_BIG
;
970 static int vhost_virtqueue_set_vring_endian_legacy(struct vhost_dev
*dev
,
974 struct vhost_vring_state s
= {
975 .index
= vhost_vq_index
,
979 if (!dev
->vhost_ops
->vhost_set_vring_endian(dev
, &s
)) {
983 VHOST_OPS_DEBUG("vhost_set_vring_endian failed");
984 if (errno
== ENOTTY
) {
985 error_report("vhost does not support cross-endian");
992 static int vhost_memory_region_lookup(struct vhost_dev
*hdev
,
993 uint64_t gpa
, uint64_t *uaddr
,
998 for (i
= 0; i
< hdev
->mem
->nregions
; i
++) {
999 struct vhost_memory_region
*reg
= hdev
->mem
->regions
+ i
;
1001 if (gpa
>= reg
->guest_phys_addr
&&
1002 reg
->guest_phys_addr
+ reg
->memory_size
> gpa
) {
1003 *uaddr
= reg
->userspace_addr
+ gpa
- reg
->guest_phys_addr
;
1004 *len
= reg
->guest_phys_addr
+ reg
->memory_size
- gpa
;
1012 int vhost_device_iotlb_miss(struct vhost_dev
*dev
, uint64_t iova
, int write
)
1014 IOMMUTLBEntry iotlb
;
1015 uint64_t uaddr
, len
;
1018 RCU_READ_LOCK_GUARD();
1020 trace_vhost_iotlb_miss(dev
, 1);
1022 iotlb
= address_space_get_iotlb_entry(dev
->vdev
->dma_as
,
1024 MEMTXATTRS_UNSPECIFIED
);
1025 if (iotlb
.target_as
!= NULL
) {
1026 ret
= vhost_memory_region_lookup(dev
, iotlb
.translated_addr
,
1029 trace_vhost_iotlb_miss(dev
, 3);
1030 error_report("Fail to lookup the translated address "
1031 "%"PRIx64
, iotlb
.translated_addr
);
1035 len
= MIN(iotlb
.addr_mask
+ 1, len
);
1036 iova
= iova
& ~iotlb
.addr_mask
;
1038 ret
= vhost_backend_update_device_iotlb(dev
, iova
, uaddr
,
1041 trace_vhost_iotlb_miss(dev
, 4);
1042 error_report("Fail to update device iotlb");
1047 trace_vhost_iotlb_miss(dev
, 2);
1053 static int vhost_virtqueue_start(struct vhost_dev
*dev
,
1054 struct VirtIODevice
*vdev
,
1055 struct vhost_virtqueue
*vq
,
1058 BusState
*qbus
= BUS(qdev_get_parent_bus(DEVICE(vdev
)));
1059 VirtioBusState
*vbus
= VIRTIO_BUS(qbus
);
1060 VirtioBusClass
*k
= VIRTIO_BUS_GET_CLASS(vbus
);
1063 int vhost_vq_index
= dev
->vhost_ops
->vhost_get_vq_index(dev
, idx
);
1064 struct vhost_vring_file file
= {
1065 .index
= vhost_vq_index
1067 struct vhost_vring_state state
= {
1068 .index
= vhost_vq_index
1070 struct VirtQueue
*vvq
= virtio_get_queue(vdev
, idx
);
1072 a
= virtio_queue_get_desc_addr(vdev
, idx
);
1074 /* Queue might not be ready for start */
1078 vq
->num
= state
.num
= virtio_queue_get_num(vdev
, idx
);
1079 r
= dev
->vhost_ops
->vhost_set_vring_num(dev
, &state
);
1081 VHOST_OPS_DEBUG("vhost_set_vring_num failed");
1085 state
.num
= virtio_queue_get_last_avail_idx(vdev
, idx
);
1086 r
= dev
->vhost_ops
->vhost_set_vring_base(dev
, &state
);
1088 VHOST_OPS_DEBUG("vhost_set_vring_base failed");
1092 if (vhost_needs_vring_endian(vdev
)) {
1093 r
= vhost_virtqueue_set_vring_endian_legacy(dev
,
1094 virtio_is_big_endian(vdev
),
1101 vq
->desc_size
= s
= l
= virtio_queue_get_desc_size(vdev
, idx
);
1103 vq
->desc
= vhost_memory_map(dev
, a
, &l
, false);
1104 if (!vq
->desc
|| l
!= s
) {
1106 goto fail_alloc_desc
;
1108 vq
->avail_size
= s
= l
= virtio_queue_get_avail_size(vdev
, idx
);
1109 vq
->avail_phys
= a
= virtio_queue_get_avail_addr(vdev
, idx
);
1110 vq
->avail
= vhost_memory_map(dev
, a
, &l
, false);
1111 if (!vq
->avail
|| l
!= s
) {
1113 goto fail_alloc_avail
;
1115 vq
->used_size
= s
= l
= virtio_queue_get_used_size(vdev
, idx
);
1116 vq
->used_phys
= a
= virtio_queue_get_used_addr(vdev
, idx
);
1117 vq
->used
= vhost_memory_map(dev
, a
, &l
, true);
1118 if (!vq
->used
|| l
!= s
) {
1120 goto fail_alloc_used
;
1123 r
= vhost_virtqueue_set_addr(dev
, vq
, vhost_vq_index
, dev
->log_enabled
);
1129 file
.fd
= event_notifier_get_fd(virtio_queue_get_host_notifier(vvq
));
1130 r
= dev
->vhost_ops
->vhost_set_vring_kick(dev
, &file
);
1132 VHOST_OPS_DEBUG("vhost_set_vring_kick failed");
1137 /* Clear and discard previous events if any. */
1138 event_notifier_test_and_clear(&vq
->masked_notifier
);
1140 /* Init vring in unmasked state, unless guest_notifier_mask
1143 if (!vdev
->use_guest_notifier_mask
) {
1144 /* TODO: check and handle errors. */
1145 vhost_virtqueue_mask(dev
, vdev
, idx
, false);
1148 if (k
->query_guest_notifiers
&&
1149 k
->query_guest_notifiers(qbus
->parent
) &&
1150 virtio_queue_vector(vdev
, idx
) == VIRTIO_NO_VECTOR
) {
1152 r
= dev
->vhost_ops
->vhost_set_vring_call(dev
, &file
);
1163 vhost_memory_unmap(dev
, vq
->used
, virtio_queue_get_used_size(vdev
, idx
),
1166 vhost_memory_unmap(dev
, vq
->avail
, virtio_queue_get_avail_size(vdev
, idx
),
1169 vhost_memory_unmap(dev
, vq
->desc
, virtio_queue_get_desc_size(vdev
, idx
),
1175 static void vhost_virtqueue_stop(struct vhost_dev
*dev
,
1176 struct VirtIODevice
*vdev
,
1177 struct vhost_virtqueue
*vq
,
1180 int vhost_vq_index
= dev
->vhost_ops
->vhost_get_vq_index(dev
, idx
);
1181 struct vhost_vring_state state
= {
1182 .index
= vhost_vq_index
,
1186 if (virtio_queue_get_desc_addr(vdev
, idx
) == 0) {
1187 /* Don't stop the virtqueue which might have not been started */
1191 r
= dev
->vhost_ops
->vhost_get_vring_base(dev
, &state
);
1193 VHOST_OPS_DEBUG("vhost VQ %u ring restore failed: %d", idx
, r
);
1194 /* Connection to the backend is broken, so let's sync internal
1195 * last avail idx to the device used idx.
1197 virtio_queue_restore_last_avail_idx(vdev
, idx
);
1199 virtio_queue_set_last_avail_idx(vdev
, idx
, state
.num
);
1201 virtio_queue_invalidate_signalled_used(vdev
, idx
);
1202 virtio_queue_update_used_idx(vdev
, idx
);
1204 /* In the cross-endian case, we need to reset the vring endianness to
1205 * native as legacy devices expect so by default.
1207 if (vhost_needs_vring_endian(vdev
)) {
1208 vhost_virtqueue_set_vring_endian_legacy(dev
,
1209 !virtio_is_big_endian(vdev
),
1213 vhost_memory_unmap(dev
, vq
->used
, virtio_queue_get_used_size(vdev
, idx
),
1214 1, virtio_queue_get_used_size(vdev
, idx
));
1215 vhost_memory_unmap(dev
, vq
->avail
, virtio_queue_get_avail_size(vdev
, idx
),
1216 0, virtio_queue_get_avail_size(vdev
, idx
));
1217 vhost_memory_unmap(dev
, vq
->desc
, virtio_queue_get_desc_size(vdev
, idx
),
1218 0, virtio_queue_get_desc_size(vdev
, idx
));
1221 static void vhost_eventfd_add(MemoryListener
*listener
,
1222 MemoryRegionSection
*section
,
1223 bool match_data
, uint64_t data
, EventNotifier
*e
)
1227 static void vhost_eventfd_del(MemoryListener
*listener
,
1228 MemoryRegionSection
*section
,
1229 bool match_data
, uint64_t data
, EventNotifier
*e
)
1233 static int vhost_virtqueue_set_busyloop_timeout(struct vhost_dev
*dev
,
1234 int n
, uint32_t timeout
)
1236 int vhost_vq_index
= dev
->vhost_ops
->vhost_get_vq_index(dev
, n
);
1237 struct vhost_vring_state state
= {
1238 .index
= vhost_vq_index
,
1243 if (!dev
->vhost_ops
->vhost_set_vring_busyloop_timeout
) {
1247 r
= dev
->vhost_ops
->vhost_set_vring_busyloop_timeout(dev
, &state
);
1249 VHOST_OPS_DEBUG("vhost_set_vring_busyloop_timeout failed");
1256 static int vhost_virtqueue_init(struct vhost_dev
*dev
,
1257 struct vhost_virtqueue
*vq
, int n
)
1259 int vhost_vq_index
= dev
->vhost_ops
->vhost_get_vq_index(dev
, n
);
1260 struct vhost_vring_file file
= {
1261 .index
= vhost_vq_index
,
1263 int r
= event_notifier_init(&vq
->masked_notifier
, 0);
1268 file
.fd
= event_notifier_get_fd(&vq
->masked_notifier
);
1269 r
= dev
->vhost_ops
->vhost_set_vring_call(dev
, &file
);
1271 VHOST_OPS_DEBUG("vhost_set_vring_call failed");
1280 event_notifier_cleanup(&vq
->masked_notifier
);
1284 static void vhost_virtqueue_cleanup(struct vhost_virtqueue
*vq
)
1286 event_notifier_cleanup(&vq
->masked_notifier
);
1289 int vhost_dev_init(struct vhost_dev
*hdev
, void *opaque
,
1290 VhostBackendType backend_type
, uint32_t busyloop_timeout
)
1293 int i
, r
, n_initialized_vqs
= 0;
1294 Error
*local_err
= NULL
;
1297 hdev
->migration_blocker
= NULL
;
1299 r
= vhost_set_backend_type(hdev
, backend_type
);
1302 r
= hdev
->vhost_ops
->vhost_backend_init(hdev
, opaque
);
1307 r
= hdev
->vhost_ops
->vhost_set_owner(hdev
);
1309 VHOST_OPS_DEBUG("vhost_set_owner failed");
1313 r
= hdev
->vhost_ops
->vhost_get_features(hdev
, &features
);
1315 VHOST_OPS_DEBUG("vhost_get_features failed");
1319 for (i
= 0; i
< hdev
->nvqs
; ++i
, ++n_initialized_vqs
) {
1320 r
= vhost_virtqueue_init(hdev
, hdev
->vqs
+ i
, hdev
->vq_index
+ i
);
1326 if (busyloop_timeout
) {
1327 for (i
= 0; i
< hdev
->nvqs
; ++i
) {
1328 r
= vhost_virtqueue_set_busyloop_timeout(hdev
, hdev
->vq_index
+ i
,
1336 hdev
->features
= features
;
1338 hdev
->memory_listener
= (MemoryListener
) {
1339 .begin
= vhost_begin
,
1340 .commit
= vhost_commit
,
1341 .region_add
= vhost_region_addnop
,
1342 .region_nop
= vhost_region_addnop
,
1343 .log_start
= vhost_log_start
,
1344 .log_stop
= vhost_log_stop
,
1345 .log_sync
= vhost_log_sync
,
1346 .log_global_start
= vhost_log_global_start
,
1347 .log_global_stop
= vhost_log_global_stop
,
1348 .eventfd_add
= vhost_eventfd_add
,
1349 .eventfd_del
= vhost_eventfd_del
,
1353 hdev
->iommu_listener
= (MemoryListener
) {
1354 .region_add
= vhost_iommu_region_add
,
1355 .region_del
= vhost_iommu_region_del
,
1358 if (hdev
->migration_blocker
== NULL
) {
1359 if (!(hdev
->features
& (0x1ULL
<< VHOST_F_LOG_ALL
))) {
1360 error_setg(&hdev
->migration_blocker
,
1361 "Migration disabled: vhost lacks VHOST_F_LOG_ALL feature.");
1362 } else if (vhost_dev_log_is_shared(hdev
) && !qemu_memfd_alloc_check()) {
1363 error_setg(&hdev
->migration_blocker
,
1364 "Migration disabled: failed to allocate shared memory");
1368 if (hdev
->migration_blocker
!= NULL
) {
1369 r
= migrate_add_blocker(hdev
->migration_blocker
, &local_err
);
1371 error_report_err(local_err
);
1372 error_free(hdev
->migration_blocker
);
1377 hdev
->mem
= g_malloc0(offsetof(struct vhost_memory
, regions
));
1378 hdev
->n_mem_sections
= 0;
1379 hdev
->mem_sections
= NULL
;
1382 hdev
->log_enabled
= false;
1383 hdev
->started
= false;
1384 memory_listener_register(&hdev
->memory_listener
, &address_space_memory
);
1385 QLIST_INSERT_HEAD(&vhost_devices
, hdev
, entry
);
1387 if (used_memslots
> hdev
->vhost_ops
->vhost_backend_memslots_limit(hdev
)) {
1388 error_report("vhost backend memory slots limit is less"
1389 " than current number of present memory slots");
1397 if (busyloop_timeout
) {
1399 vhost_virtqueue_set_busyloop_timeout(hdev
, hdev
->vq_index
+ i
, 0);
1403 hdev
->nvqs
= n_initialized_vqs
;
1404 vhost_dev_cleanup(hdev
);
1408 void vhost_dev_cleanup(struct vhost_dev
*hdev
)
1412 for (i
= 0; i
< hdev
->nvqs
; ++i
) {
1413 vhost_virtqueue_cleanup(hdev
->vqs
+ i
);
1416 /* those are only safe after successful init */
1417 memory_listener_unregister(&hdev
->memory_listener
);
1418 QLIST_REMOVE(hdev
, entry
);
1420 if (hdev
->migration_blocker
) {
1421 migrate_del_blocker(hdev
->migration_blocker
);
1422 error_free(hdev
->migration_blocker
);
1425 g_free(hdev
->mem_sections
);
1426 if (hdev
->vhost_ops
) {
1427 hdev
->vhost_ops
->vhost_backend_cleanup(hdev
);
1431 memset(hdev
, 0, sizeof(struct vhost_dev
));
1434 /* Stop processing guest IO notifications in qemu.
1435 * Start processing them in vhost in kernel.
1437 int vhost_dev_enable_notifiers(struct vhost_dev
*hdev
, VirtIODevice
*vdev
)
1439 BusState
*qbus
= BUS(qdev_get_parent_bus(DEVICE(vdev
)));
1442 /* We will pass the notifiers to the kernel, make sure that QEMU
1443 * doesn't interfere.
1445 r
= virtio_device_grab_ioeventfd(vdev
);
1447 error_report("binding does not support host notifiers");
1451 for (i
= 0; i
< hdev
->nvqs
; ++i
) {
1452 r
= virtio_bus_set_host_notifier(VIRTIO_BUS(qbus
), hdev
->vq_index
+ i
,
1455 error_report("vhost VQ %d notifier binding failed: %d", i
, -r
);
1463 e
= virtio_bus_set_host_notifier(VIRTIO_BUS(qbus
), hdev
->vq_index
+ i
,
1466 error_report("vhost VQ %d notifier cleanup error: %d", i
, -r
);
1469 virtio_bus_cleanup_host_notifier(VIRTIO_BUS(qbus
), hdev
->vq_index
+ i
);
1471 virtio_device_release_ioeventfd(vdev
);
1476 /* Stop processing guest IO notifications in vhost.
1477 * Start processing them in qemu.
1478 * This might actually run the qemu handlers right away,
1479 * so virtio in qemu must be completely setup when this is called.
1481 void vhost_dev_disable_notifiers(struct vhost_dev
*hdev
, VirtIODevice
*vdev
)
1483 BusState
*qbus
= BUS(qdev_get_parent_bus(DEVICE(vdev
)));
1486 for (i
= 0; i
< hdev
->nvqs
; ++i
) {
1487 r
= virtio_bus_set_host_notifier(VIRTIO_BUS(qbus
), hdev
->vq_index
+ i
,
1490 error_report("vhost VQ %d notifier cleanup failed: %d", i
, -r
);
1493 virtio_bus_cleanup_host_notifier(VIRTIO_BUS(qbus
), hdev
->vq_index
+ i
);
1495 virtio_device_release_ioeventfd(vdev
);
1498 /* Test and clear event pending status.
1499 * Should be called after unmask to avoid losing events.
1501 bool vhost_virtqueue_pending(struct vhost_dev
*hdev
, int n
)
1503 struct vhost_virtqueue
*vq
= hdev
->vqs
+ n
- hdev
->vq_index
;
1504 assert(n
>= hdev
->vq_index
&& n
< hdev
->vq_index
+ hdev
->nvqs
);
1505 return event_notifier_test_and_clear(&vq
->masked_notifier
);
1508 /* Mask/unmask events from this vq. */
1509 void vhost_virtqueue_mask(struct vhost_dev
*hdev
, VirtIODevice
*vdev
, int n
,
1512 struct VirtQueue
*vvq
= virtio_get_queue(vdev
, n
);
1513 int r
, index
= n
- hdev
->vq_index
;
1514 struct vhost_vring_file file
;
1516 /* should only be called after backend is connected */
1517 assert(hdev
->vhost_ops
);
1520 assert(vdev
->use_guest_notifier_mask
);
1521 file
.fd
= event_notifier_get_fd(&hdev
->vqs
[index
].masked_notifier
);
1523 file
.fd
= event_notifier_get_fd(virtio_queue_get_guest_notifier(vvq
));
1526 file
.index
= hdev
->vhost_ops
->vhost_get_vq_index(hdev
, n
);
1527 r
= hdev
->vhost_ops
->vhost_set_vring_call(hdev
, &file
);
1529 VHOST_OPS_DEBUG("vhost_set_vring_call failed");
1533 uint64_t vhost_get_features(struct vhost_dev
*hdev
, const int *feature_bits
,
1536 const int *bit
= feature_bits
;
1537 while (*bit
!= VHOST_INVALID_FEATURE_BIT
) {
1538 uint64_t bit_mask
= (1ULL << *bit
);
1539 if (!(hdev
->features
& bit_mask
)) {
1540 features
&= ~bit_mask
;
1547 void vhost_ack_features(struct vhost_dev
*hdev
, const int *feature_bits
,
1550 const int *bit
= feature_bits
;
1551 while (*bit
!= VHOST_INVALID_FEATURE_BIT
) {
1552 uint64_t bit_mask
= (1ULL << *bit
);
1553 if (features
& bit_mask
) {
1554 hdev
->acked_features
|= bit_mask
;
1560 int vhost_dev_get_config(struct vhost_dev
*hdev
, uint8_t *config
,
1561 uint32_t config_len
)
1563 assert(hdev
->vhost_ops
);
1565 if (hdev
->vhost_ops
->vhost_get_config
) {
1566 return hdev
->vhost_ops
->vhost_get_config(hdev
, config
, config_len
);
1572 int vhost_dev_set_config(struct vhost_dev
*hdev
, const uint8_t *data
,
1573 uint32_t offset
, uint32_t size
, uint32_t flags
)
1575 assert(hdev
->vhost_ops
);
1577 if (hdev
->vhost_ops
->vhost_set_config
) {
1578 return hdev
->vhost_ops
->vhost_set_config(hdev
, data
, offset
,
1585 void vhost_dev_set_config_notifier(struct vhost_dev
*hdev
,
1586 const VhostDevConfigOps
*ops
)
1588 hdev
->config_ops
= ops
;
1591 void vhost_dev_free_inflight(struct vhost_inflight
*inflight
)
1593 if (inflight
&& inflight
->addr
) {
1594 qemu_memfd_free(inflight
->addr
, inflight
->size
, inflight
->fd
);
1595 inflight
->addr
= NULL
;
1600 static int vhost_dev_resize_inflight(struct vhost_inflight
*inflight
,
1605 void *addr
= qemu_memfd_alloc("vhost-inflight", new_size
,
1606 F_SEAL_GROW
| F_SEAL_SHRINK
| F_SEAL_SEAL
,
1610 error_report_err(err
);
1614 vhost_dev_free_inflight(inflight
);
1615 inflight
->offset
= 0;
1616 inflight
->addr
= addr
;
1618 inflight
->size
= new_size
;
1623 void vhost_dev_save_inflight(struct vhost_inflight
*inflight
, QEMUFile
*f
)
1625 if (inflight
->addr
) {
1626 qemu_put_be64(f
, inflight
->size
);
1627 qemu_put_be16(f
, inflight
->queue_size
);
1628 qemu_put_buffer(f
, inflight
->addr
, inflight
->size
);
1630 qemu_put_be64(f
, 0);
1634 int vhost_dev_load_inflight(struct vhost_inflight
*inflight
, QEMUFile
*f
)
1638 size
= qemu_get_be64(f
);
1643 if (inflight
->size
!= size
) {
1644 if (vhost_dev_resize_inflight(inflight
, size
)) {
1648 inflight
->queue_size
= qemu_get_be16(f
);
1650 qemu_get_buffer(f
, inflight
->addr
, size
);
1655 int vhost_dev_prepare_inflight(struct vhost_dev
*hdev
, VirtIODevice
*vdev
)
1659 if (hdev
->vhost_ops
->vhost_get_inflight_fd
== NULL
||
1660 hdev
->vhost_ops
->vhost_set_inflight_fd
== NULL
) {
1666 r
= vhost_dev_set_features(hdev
, hdev
->log_enabled
);
1668 VHOST_OPS_DEBUG("vhost_dev_prepare_inflight failed");
1675 int vhost_dev_set_inflight(struct vhost_dev
*dev
,
1676 struct vhost_inflight
*inflight
)
1680 if (dev
->vhost_ops
->vhost_set_inflight_fd
&& inflight
->addr
) {
1681 r
= dev
->vhost_ops
->vhost_set_inflight_fd(dev
, inflight
);
1683 VHOST_OPS_DEBUG("vhost_set_inflight_fd failed");
1691 int vhost_dev_get_inflight(struct vhost_dev
*dev
, uint16_t queue_size
,
1692 struct vhost_inflight
*inflight
)
1696 if (dev
->vhost_ops
->vhost_get_inflight_fd
) {
1697 r
= dev
->vhost_ops
->vhost_get_inflight_fd(dev
, queue_size
, inflight
);
1699 VHOST_OPS_DEBUG("vhost_get_inflight_fd failed");
1707 /* Host notifiers must be enabled at this point. */
1708 int vhost_dev_start(struct vhost_dev
*hdev
, VirtIODevice
*vdev
)
1712 /* should only be called after backend is connected */
1713 assert(hdev
->vhost_ops
);
1715 hdev
->started
= true;
1718 r
= vhost_dev_set_features(hdev
, hdev
->log_enabled
);
1723 if (vhost_dev_has_iommu(hdev
)) {
1724 memory_listener_register(&hdev
->iommu_listener
, vdev
->dma_as
);
1727 r
= hdev
->vhost_ops
->vhost_set_mem_table(hdev
, hdev
->mem
);
1729 VHOST_OPS_DEBUG("vhost_set_mem_table failed");
1733 for (i
= 0; i
< hdev
->nvqs
; ++i
) {
1734 r
= vhost_virtqueue_start(hdev
,
1737 hdev
->vq_index
+ i
);
1743 if (hdev
->log_enabled
) {
1746 hdev
->log_size
= vhost_get_log_size(hdev
);
1747 hdev
->log
= vhost_log_get(hdev
->log_size
,
1748 vhost_dev_log_is_shared(hdev
));
1749 log_base
= (uintptr_t)hdev
->log
->log
;
1750 r
= hdev
->vhost_ops
->vhost_set_log_base(hdev
,
1751 hdev
->log_size
? log_base
: 0,
1754 VHOST_OPS_DEBUG("vhost_set_log_base failed");
1759 if (hdev
->vhost_ops
->vhost_dev_start
) {
1760 r
= hdev
->vhost_ops
->vhost_dev_start(hdev
, true);
1765 if (vhost_dev_has_iommu(hdev
) &&
1766 hdev
->vhost_ops
->vhost_set_iotlb_callback
) {
1767 hdev
->vhost_ops
->vhost_set_iotlb_callback(hdev
, true);
1769 /* Update used ring information for IOTLB to work correctly,
1770 * vhost-kernel code requires for this.*/
1771 for (i
= 0; i
< hdev
->nvqs
; ++i
) {
1772 struct vhost_virtqueue
*vq
= hdev
->vqs
+ i
;
1773 vhost_device_iotlb_miss(hdev
, vq
->used_phys
, true);
1778 vhost_log_put(hdev
, false);
1781 vhost_virtqueue_stop(hdev
,
1784 hdev
->vq_index
+ i
);
1790 hdev
->started
= false;
1794 /* Host notifiers must be enabled at this point. */
1795 void vhost_dev_stop(struct vhost_dev
*hdev
, VirtIODevice
*vdev
)
1799 /* should only be called after backend is connected */
1800 assert(hdev
->vhost_ops
);
1802 if (hdev
->vhost_ops
->vhost_dev_start
) {
1803 hdev
->vhost_ops
->vhost_dev_start(hdev
, false);
1805 for (i
= 0; i
< hdev
->nvqs
; ++i
) {
1806 vhost_virtqueue_stop(hdev
,
1809 hdev
->vq_index
+ i
);
1812 if (vhost_dev_has_iommu(hdev
)) {
1813 if (hdev
->vhost_ops
->vhost_set_iotlb_callback
) {
1814 hdev
->vhost_ops
->vhost_set_iotlb_callback(hdev
, false);
1816 memory_listener_unregister(&hdev
->iommu_listener
);
1818 vhost_log_put(hdev
, true);
1819 hdev
->started
= false;
1823 int vhost_net_set_backend(struct vhost_dev
*hdev
,
1824 struct vhost_vring_file
*file
)
1826 if (hdev
->vhost_ops
->vhost_net_set_backend
) {
1827 return hdev
->vhost_ops
->vhost_net_set_backend(hdev
, file
);