4 * Copyright Red Hat, Inc. 2010
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 "hw/virtio/virtio-bus.h"
25 #include "hw/virtio/virtio-access.h"
26 #include "migration/blocker.h"
27 #include "migration/qemu-file-types.h"
28 #include "sysemu/dma.h"
29 #include "sysemu/tcg.h"
32 /* enabled until disconnected backend stabilizes */
33 #define _VHOST_DEBUG 1
36 #define VHOST_OPS_DEBUG(retval, fmt, ...) \
38 error_report(fmt ": %s (%d)", ## __VA_ARGS__, \
39 strerror(-retval), -retval); \
42 #define VHOST_OPS_DEBUG(retval, fmt, ...) \
46 static struct vhost_log *vhost_log;
47 static struct vhost_log *vhost_log_shm;
49 static unsigned int used_memslots;
50 static QLIST_HEAD(, vhost_dev) vhost_devices =
51 QLIST_HEAD_INITIALIZER(vhost_devices);
53 bool vhost_has_free_slot(void)
55 unsigned int slots_limit = ~0U;
56 struct vhost_dev *hdev;
58 QLIST_FOREACH(hdev, &vhost_devices, entry) {
59 unsigned int r = hdev->vhost_ops->vhost_backend_memslots_limit(hdev);
60 slots_limit = MIN(slots_limit, r);
62 return slots_limit > used_memslots;
65 static void vhost_dev_sync_region(struct vhost_dev *dev,
66 MemoryRegionSection *section,
67 uint64_t mfirst, uint64_t mlast,
68 uint64_t rfirst, uint64_t rlast)
70 vhost_log_chunk_t *log = dev->log->log;
72 uint64_t start = MAX(mfirst, rfirst);
73 uint64_t end = MIN(mlast, rlast);
74 vhost_log_chunk_t *from = log + start / VHOST_LOG_CHUNK;
75 vhost_log_chunk_t *to = log + end / VHOST_LOG_CHUNK + 1;
76 uint64_t addr = QEMU_ALIGN_DOWN(start, VHOST_LOG_CHUNK);
81 assert(end / VHOST_LOG_CHUNK < dev->log_size);
82 assert(start / VHOST_LOG_CHUNK < dev->log_size);
84 for (;from < to; ++from) {
85 vhost_log_chunk_t log;
86 /* We first check with non-atomic: much cheaper,
87 * and we expect non-dirty to be the common case. */
89 addr += VHOST_LOG_CHUNK;
92 /* Data must be read atomically. We don't really need barrier semantics
93 * but it's easier to use atomic_* than roll our own. */
94 log = qatomic_xchg(from, 0);
98 hwaddr section_offset;
100 page_addr = addr + bit * VHOST_LOG_PAGE;
101 section_offset = page_addr - section->offset_within_address_space;
102 mr_offset = section_offset + section->offset_within_region;
103 memory_region_set_dirty(section->mr, mr_offset, VHOST_LOG_PAGE);
104 log &= ~(0x1ull << bit);
106 addr += VHOST_LOG_CHUNK;
110 static int vhost_sync_dirty_bitmap(struct vhost_dev *dev,
111 MemoryRegionSection *section,
119 if (!dev->log_enabled || !dev->started) {
122 start_addr = section->offset_within_address_space;
123 end_addr = range_get_last(start_addr, int128_get64(section->size));
124 start_addr = MAX(first, start_addr);
125 end_addr = MIN(last, end_addr);
127 for (i = 0; i < dev->mem->nregions; ++i) {
128 struct vhost_memory_region *reg = dev->mem->regions + i;
129 vhost_dev_sync_region(dev, section, start_addr, end_addr,
130 reg->guest_phys_addr,
131 range_get_last(reg->guest_phys_addr,
134 for (i = 0; i < dev->nvqs; ++i) {
135 struct vhost_virtqueue *vq = dev->vqs + i;
137 if (!vq->used_phys && !vq->used_size) {
141 vhost_dev_sync_region(dev, section, start_addr, end_addr, vq->used_phys,
142 range_get_last(vq->used_phys, vq->used_size));
147 static void vhost_log_sync(MemoryListener *listener,
148 MemoryRegionSection *section)
150 struct vhost_dev *dev = container_of(listener, struct vhost_dev,
152 vhost_sync_dirty_bitmap(dev, section, 0x0, ~0x0ULL);
155 static void vhost_log_sync_range(struct vhost_dev *dev,
156 hwaddr first, hwaddr last)
159 /* FIXME: this is N^2 in number of sections */
160 for (i = 0; i < dev->n_mem_sections; ++i) {
161 MemoryRegionSection *section = &dev->mem_sections[i];
162 vhost_sync_dirty_bitmap(dev, section, first, last);
166 static uint64_t vhost_get_log_size(struct vhost_dev *dev)
168 uint64_t log_size = 0;
170 for (i = 0; i < dev->mem->nregions; ++i) {
171 struct vhost_memory_region *reg = dev->mem->regions + i;
172 uint64_t last = range_get_last(reg->guest_phys_addr,
174 log_size = MAX(log_size, last / VHOST_LOG_CHUNK + 1);
179 static int vhost_set_backend_type(struct vhost_dev *dev,
180 VhostBackendType backend_type)
184 switch (backend_type) {
185 #ifdef CONFIG_VHOST_KERNEL
186 case VHOST_BACKEND_TYPE_KERNEL:
187 dev->vhost_ops = &kernel_ops;
190 #ifdef CONFIG_VHOST_USER
191 case VHOST_BACKEND_TYPE_USER:
192 dev->vhost_ops = &user_ops;
195 #ifdef CONFIG_VHOST_VDPA
196 case VHOST_BACKEND_TYPE_VDPA:
197 dev->vhost_ops = &vdpa_ops;
201 error_report("Unknown vhost backend type");
208 static struct vhost_log *vhost_log_alloc(uint64_t size, bool share)
211 struct vhost_log *log;
212 uint64_t logsize = size * sizeof(*(log->log));
215 log = g_new0(struct vhost_log, 1);
217 log->log = qemu_memfd_alloc("vhost-log", logsize,
218 F_SEAL_GROW | F_SEAL_SHRINK | F_SEAL_SEAL,
221 error_report_err(err);
225 memset(log->log, 0, logsize);
227 log->log = g_malloc0(logsize);
237 static struct vhost_log *vhost_log_get(uint64_t size, bool share)
239 struct vhost_log *log = share ? vhost_log_shm : vhost_log;
241 if (!log || log->size != size) {
242 log = vhost_log_alloc(size, share);
255 static void vhost_log_put(struct vhost_dev *dev, bool sync)
257 struct vhost_log *log = dev->log;
264 if (log->refcnt == 0) {
265 /* Sync only the range covered by the old log */
266 if (dev->log_size && sync) {
267 vhost_log_sync_range(dev, 0, dev->log_size * VHOST_LOG_CHUNK - 1);
270 if (vhost_log == log) {
273 } else if (vhost_log_shm == log) {
274 qemu_memfd_free(log->log, log->size * sizeof(*(log->log)),
276 vhost_log_shm = NULL;
286 static bool vhost_dev_log_is_shared(struct vhost_dev *dev)
288 return dev->vhost_ops->vhost_requires_shm_log &&
289 dev->vhost_ops->vhost_requires_shm_log(dev);
292 static inline void vhost_dev_log_resize(struct vhost_dev *dev, uint64_t size)
294 struct vhost_log *log = vhost_log_get(size, vhost_dev_log_is_shared(dev));
295 uint64_t log_base = (uintptr_t)log->log;
298 /* inform backend of log switching, this must be done before
299 releasing the current log, to ensure no logging is lost */
300 r = dev->vhost_ops->vhost_set_log_base(dev, log_base, log);
302 VHOST_OPS_DEBUG(r, "vhost_set_log_base failed");
305 vhost_log_put(dev, true);
307 dev->log_size = size;
310 static int vhost_dev_has_iommu(struct vhost_dev *dev)
312 VirtIODevice *vdev = dev->vdev;
315 * For vhost, VIRTIO_F_IOMMU_PLATFORM means the backend support
316 * incremental memory mapping API via IOTLB API. For platform that
317 * does not have IOMMU, there's no need to enable this feature
318 * which may cause unnecessary IOTLB miss/update transactions.
320 return virtio_bus_device_iommu_enabled(vdev) &&
321 virtio_host_has_feature(vdev, VIRTIO_F_IOMMU_PLATFORM);
324 static void *vhost_memory_map(struct vhost_dev *dev, hwaddr addr,
325 hwaddr *plen, bool is_write)
327 if (!vhost_dev_has_iommu(dev)) {
328 return cpu_physical_memory_map(addr, plen, is_write);
330 return (void *)(uintptr_t)addr;
334 static void vhost_memory_unmap(struct vhost_dev *dev, void *buffer,
335 hwaddr len, int is_write,
338 if (!vhost_dev_has_iommu(dev)) {
339 cpu_physical_memory_unmap(buffer, len, is_write, access_len);
343 static int vhost_verify_ring_part_mapping(void *ring_hva,
350 uint64_t hva_ring_offset;
351 uint64_t ring_last = range_get_last(ring_gpa, ring_size);
352 uint64_t reg_last = range_get_last(reg_gpa, reg_size);
354 if (ring_last < reg_gpa || ring_gpa > reg_last) {
357 /* check that whole ring's is mapped */
358 if (ring_last > reg_last) {
361 /* check that ring's MemoryRegion wasn't replaced */
362 hva_ring_offset = ring_gpa - reg_gpa;
363 if (ring_hva != reg_hva + hva_ring_offset) {
370 static int vhost_verify_ring_mappings(struct vhost_dev *dev,
377 const char *part_name[] = {
383 if (vhost_dev_has_iommu(dev)) {
387 for (i = 0; i < dev->nvqs; ++i) {
388 struct vhost_virtqueue *vq = dev->vqs + i;
390 if (vq->desc_phys == 0) {
395 r = vhost_verify_ring_part_mapping(
396 vq->desc, vq->desc_phys, vq->desc_size,
397 reg_hva, reg_gpa, reg_size);
403 r = vhost_verify_ring_part_mapping(
404 vq->avail, vq->avail_phys, vq->avail_size,
405 reg_hva, reg_gpa, reg_size);
411 r = vhost_verify_ring_part_mapping(
412 vq->used, vq->used_phys, vq->used_size,
413 reg_hva, reg_gpa, reg_size);
420 error_report("Unable to map %s for ring %d", part_name[j], i);
421 } else if (r == -EBUSY) {
422 error_report("%s relocated for ring %d", part_name[j], i);
428 * vhost_section: identify sections needed for vhost access
430 * We only care about RAM sections here (where virtqueue and guest
431 * internals accessed by virtio might live). If we find one we still
432 * allow the backend to potentially filter it out of our list.
434 static bool vhost_section(struct vhost_dev *dev, MemoryRegionSection *section)
436 MemoryRegion *mr = section->mr;
438 if (memory_region_is_ram(mr) && !memory_region_is_rom(mr)) {
439 uint8_t dirty_mask = memory_region_get_dirty_log_mask(mr);
440 uint8_t handled_dirty;
443 * Kernel based vhost doesn't handle any block which is doing
444 * dirty-tracking other than migration for which it has
445 * specific logging support. However for TCG the kernel never
446 * gets involved anyway so we can also ignore it's
447 * self-modiying code detection flags. However a vhost-user
448 * client could still confuse a TCG guest if it re-writes
449 * executable memory that has already been translated.
451 handled_dirty = (1 << DIRTY_MEMORY_MIGRATION) |
452 (1 << DIRTY_MEMORY_CODE);
454 if (dirty_mask & ~handled_dirty) {
455 trace_vhost_reject_section(mr->name, 1);
459 if (dev->vhost_ops->vhost_backend_mem_section_filter &&
460 !dev->vhost_ops->vhost_backend_mem_section_filter(dev, section)) {
461 trace_vhost_reject_section(mr->name, 2);
465 trace_vhost_section(mr->name);
468 trace_vhost_reject_section(mr->name, 3);
473 static void vhost_begin(MemoryListener *listener)
475 struct vhost_dev *dev = container_of(listener, struct vhost_dev,
477 dev->tmp_sections = NULL;
478 dev->n_tmp_sections = 0;
481 static void vhost_commit(MemoryListener *listener)
483 struct vhost_dev *dev = container_of(listener, struct vhost_dev,
485 MemoryRegionSection *old_sections;
491 bool changed = false;
493 /* Note we can be called before the device is started, but then
494 * starting the device calls set_mem_table, so we need to have
495 * built the data structures.
497 old_sections = dev->mem_sections;
498 n_old_sections = dev->n_mem_sections;
499 dev->mem_sections = dev->tmp_sections;
500 dev->n_mem_sections = dev->n_tmp_sections;
502 if (dev->n_mem_sections != n_old_sections) {
505 /* Same size, lets check the contents */
506 for (int i = 0; i < n_old_sections; i++) {
507 if (!MemoryRegionSection_eq(&old_sections[i],
508 &dev->mem_sections[i])) {
515 trace_vhost_commit(dev->started, changed);
520 /* Rebuild the regions list from the new sections list */
521 regions_size = offsetof(struct vhost_memory, regions) +
522 dev->n_mem_sections * sizeof dev->mem->regions[0];
523 dev->mem = g_realloc(dev->mem, regions_size);
524 dev->mem->nregions = dev->n_mem_sections;
525 used_memslots = dev->mem->nregions;
526 for (i = 0; i < dev->n_mem_sections; i++) {
527 struct vhost_memory_region *cur_vmr = dev->mem->regions + i;
528 struct MemoryRegionSection *mrs = dev->mem_sections + i;
530 cur_vmr->guest_phys_addr = mrs->offset_within_address_space;
531 cur_vmr->memory_size = int128_get64(mrs->size);
532 cur_vmr->userspace_addr =
533 (uintptr_t)memory_region_get_ram_ptr(mrs->mr) +
534 mrs->offset_within_region;
535 cur_vmr->flags_padding = 0;
542 for (i = 0; i < dev->mem->nregions; i++) {
543 if (vhost_verify_ring_mappings(dev,
544 (void *)(uintptr_t)dev->mem->regions[i].userspace_addr,
545 dev->mem->regions[i].guest_phys_addr,
546 dev->mem->regions[i].memory_size)) {
547 error_report("Verify ring failure on region %d", i);
552 if (!dev->log_enabled) {
553 r = dev->vhost_ops->vhost_set_mem_table(dev, dev->mem);
555 VHOST_OPS_DEBUG(r, "vhost_set_mem_table failed");
559 log_size = vhost_get_log_size(dev);
560 /* We allocate an extra 4K bytes to log,
561 * to reduce the * number of reallocations. */
562 #define VHOST_LOG_BUFFER (0x1000 / sizeof *dev->log)
563 /* To log more, must increase log size before table update. */
564 if (dev->log_size < log_size) {
565 vhost_dev_log_resize(dev, log_size + VHOST_LOG_BUFFER);
567 r = dev->vhost_ops->vhost_set_mem_table(dev, dev->mem);
569 VHOST_OPS_DEBUG(r, "vhost_set_mem_table failed");
571 /* To log less, can only decrease log size after table update. */
572 if (dev->log_size > log_size + VHOST_LOG_BUFFER) {
573 vhost_dev_log_resize(dev, log_size);
577 /* Deref the old list of sections, this must happen _after_ the
578 * vhost_set_mem_table to ensure the client isn't still using the
579 * section we're about to unref.
581 while (n_old_sections--) {
582 memory_region_unref(old_sections[n_old_sections].mr);
584 g_free(old_sections);
588 /* Adds the section data to the tmp_section structure.
589 * It relies on the listener calling us in memory address order
590 * and for each region (via the _add and _nop methods) to
593 static void vhost_region_add_section(struct vhost_dev *dev,
594 MemoryRegionSection *section)
596 bool need_add = true;
597 uint64_t mrs_size = int128_get64(section->size);
598 uint64_t mrs_gpa = section->offset_within_address_space;
599 uintptr_t mrs_host = (uintptr_t)memory_region_get_ram_ptr(section->mr) +
600 section->offset_within_region;
601 RAMBlock *mrs_rb = section->mr->ram_block;
603 trace_vhost_region_add_section(section->mr->name, mrs_gpa, mrs_size,
606 if (dev->vhost_ops->backend_type == VHOST_BACKEND_TYPE_USER) {
607 /* Round the section to it's page size */
608 /* First align the start down to a page boundary */
609 size_t mrs_page = qemu_ram_pagesize(mrs_rb);
610 uint64_t alignage = mrs_host & (mrs_page - 1);
612 mrs_host -= alignage;
613 mrs_size += alignage;
616 /* Now align the size up to a page boundary */
617 alignage = mrs_size & (mrs_page - 1);
619 mrs_size += mrs_page - alignage;
621 trace_vhost_region_add_section_aligned(section->mr->name, mrs_gpa,
625 if (dev->n_tmp_sections) {
626 /* Since we already have at least one section, lets see if
627 * this extends it; since we're scanning in order, we only
628 * have to look at the last one, and the FlatView that calls
629 * us shouldn't have overlaps.
631 MemoryRegionSection *prev_sec = dev->tmp_sections +
632 (dev->n_tmp_sections - 1);
633 uint64_t prev_gpa_start = prev_sec->offset_within_address_space;
634 uint64_t prev_size = int128_get64(prev_sec->size);
635 uint64_t prev_gpa_end = range_get_last(prev_gpa_start, prev_size);
636 uint64_t prev_host_start =
637 (uintptr_t)memory_region_get_ram_ptr(prev_sec->mr) +
638 prev_sec->offset_within_region;
639 uint64_t prev_host_end = range_get_last(prev_host_start, prev_size);
641 if (mrs_gpa <= (prev_gpa_end + 1)) {
642 /* OK, looks like overlapping/intersecting - it's possible that
643 * the rounding to page sizes has made them overlap, but they should
644 * match up in the same RAMBlock if they do.
646 if (mrs_gpa < prev_gpa_start) {
647 error_report("%s:Section '%s' rounded to %"PRIx64
648 " prior to previous '%s' %"PRIx64,
649 __func__, section->mr->name, mrs_gpa,
650 prev_sec->mr->name, prev_gpa_start);
651 /* A way to cleanly fail here would be better */
654 /* Offset from the start of the previous GPA to this GPA */
655 size_t offset = mrs_gpa - prev_gpa_start;
657 if (prev_host_start + offset == mrs_host &&
658 section->mr == prev_sec->mr &&
659 (!dev->vhost_ops->vhost_backend_can_merge ||
660 dev->vhost_ops->vhost_backend_can_merge(dev,
662 prev_host_start, prev_size))) {
663 uint64_t max_end = MAX(prev_host_end, mrs_host + mrs_size);
665 prev_sec->offset_within_address_space =
666 MIN(prev_gpa_start, mrs_gpa);
667 prev_sec->offset_within_region =
668 MIN(prev_host_start, mrs_host) -
669 (uintptr_t)memory_region_get_ram_ptr(prev_sec->mr);
670 prev_sec->size = int128_make64(max_end - MIN(prev_host_start,
672 trace_vhost_region_add_section_merge(section->mr->name,
673 int128_get64(prev_sec->size),
674 prev_sec->offset_within_address_space,
675 prev_sec->offset_within_region);
677 /* adjoining regions are fine, but overlapping ones with
678 * different blocks/offsets shouldn't happen
680 if (mrs_gpa != prev_gpa_end + 1) {
681 error_report("%s: Overlapping but not coherent sections "
691 ++dev->n_tmp_sections;
692 dev->tmp_sections = g_renew(MemoryRegionSection, dev->tmp_sections,
693 dev->n_tmp_sections);
694 dev->tmp_sections[dev->n_tmp_sections - 1] = *section;
695 /* The flatview isn't stable and we don't use it, making it NULL
696 * means we can memcmp the list.
698 dev->tmp_sections[dev->n_tmp_sections - 1].fv = NULL;
699 memory_region_ref(section->mr);
703 /* Used for both add and nop callbacks */
704 static void vhost_region_addnop(MemoryListener *listener,
705 MemoryRegionSection *section)
707 struct vhost_dev *dev = container_of(listener, struct vhost_dev,
710 if (!vhost_section(dev, section)) {
713 vhost_region_add_section(dev, section);
716 static void vhost_iommu_unmap_notify(IOMMUNotifier *n, IOMMUTLBEntry *iotlb)
718 struct vhost_iommu *iommu = container_of(n, struct vhost_iommu, n);
719 struct vhost_dev *hdev = iommu->hdev;
720 hwaddr iova = iotlb->iova + iommu->iommu_offset;
722 if (vhost_backend_invalidate_device_iotlb(hdev, iova,
723 iotlb->addr_mask + 1)) {
724 error_report("Fail to invalidate device iotlb");
728 static void vhost_iommu_region_add(MemoryListener *listener,
729 MemoryRegionSection *section)
731 struct vhost_dev *dev = container_of(listener, struct vhost_dev,
733 struct vhost_iommu *iommu;
736 IOMMUMemoryRegion *iommu_mr;
739 if (!memory_region_is_iommu(section->mr)) {
743 iommu_mr = IOMMU_MEMORY_REGION(section->mr);
745 iommu = g_malloc0(sizeof(*iommu));
746 end = int128_add(int128_make64(section->offset_within_region),
748 end = int128_sub(end, int128_one());
749 iommu_idx = memory_region_iommu_attrs_to_index(iommu_mr,
750 MEMTXATTRS_UNSPECIFIED);
751 iommu_notifier_init(&iommu->n, vhost_iommu_unmap_notify,
752 IOMMU_NOTIFIER_DEVIOTLB_UNMAP,
753 section->offset_within_region,
756 iommu->mr = section->mr;
757 iommu->iommu_offset = section->offset_within_address_space -
758 section->offset_within_region;
760 ret = memory_region_register_iommu_notifier(section->mr, &iommu->n, NULL);
763 * Some vIOMMUs do not support dev-iotlb yet. If so, try to use the
764 * UNMAP legacy message
766 iommu->n.notifier_flags = IOMMU_NOTIFIER_UNMAP;
767 memory_region_register_iommu_notifier(section->mr, &iommu->n,
770 QLIST_INSERT_HEAD(&dev->iommu_list, iommu, iommu_next);
771 /* TODO: can replay help performance here? */
774 static void vhost_iommu_region_del(MemoryListener *listener,
775 MemoryRegionSection *section)
777 struct vhost_dev *dev = container_of(listener, struct vhost_dev,
779 struct vhost_iommu *iommu;
781 if (!memory_region_is_iommu(section->mr)) {
785 QLIST_FOREACH(iommu, &dev->iommu_list, iommu_next) {
786 if (iommu->mr == section->mr &&
787 iommu->n.start == section->offset_within_region) {
788 memory_region_unregister_iommu_notifier(iommu->mr,
790 QLIST_REMOVE(iommu, iommu_next);
797 static int vhost_virtqueue_set_addr(struct vhost_dev *dev,
798 struct vhost_virtqueue *vq,
799 unsigned idx, bool enable_log)
801 struct vhost_vring_addr addr;
803 memset(&addr, 0, sizeof(struct vhost_vring_addr));
805 if (dev->vhost_ops->vhost_vq_get_addr) {
806 r = dev->vhost_ops->vhost_vq_get_addr(dev, &addr, vq);
808 VHOST_OPS_DEBUG(r, "vhost_vq_get_addr failed");
812 addr.desc_user_addr = (uint64_t)(unsigned long)vq->desc;
813 addr.avail_user_addr = (uint64_t)(unsigned long)vq->avail;
814 addr.used_user_addr = (uint64_t)(unsigned long)vq->used;
817 addr.log_guest_addr = vq->used_phys;
818 addr.flags = enable_log ? (1 << VHOST_VRING_F_LOG) : 0;
819 r = dev->vhost_ops->vhost_set_vring_addr(dev, &addr);
821 VHOST_OPS_DEBUG(r, "vhost_set_vring_addr failed");
826 static int vhost_dev_set_features(struct vhost_dev *dev,
829 uint64_t features = dev->acked_features;
832 features |= 0x1ULL << VHOST_F_LOG_ALL;
834 if (!vhost_dev_has_iommu(dev)) {
835 features &= ~(0x1ULL << VIRTIO_F_IOMMU_PLATFORM);
837 if (dev->vhost_ops->vhost_force_iommu) {
838 if (dev->vhost_ops->vhost_force_iommu(dev) == true) {
839 features |= 0x1ULL << VIRTIO_F_IOMMU_PLATFORM;
842 r = dev->vhost_ops->vhost_set_features(dev, features);
844 VHOST_OPS_DEBUG(r, "vhost_set_features failed");
847 if (dev->vhost_ops->vhost_set_backend_cap) {
848 r = dev->vhost_ops->vhost_set_backend_cap(dev);
850 VHOST_OPS_DEBUG(r, "vhost_set_backend_cap failed");
859 static int vhost_dev_set_log(struct vhost_dev *dev, bool enable_log)
864 r = vhost_dev_set_features(dev, enable_log);
868 for (i = 0; i < dev->nvqs; ++i) {
869 idx = dev->vhost_ops->vhost_get_vq_index(dev, dev->vq_index + i);
870 addr = virtio_queue_get_desc_addr(dev->vdev, idx);
873 * The queue might not be ready for start. If this
874 * is the case there is no reason to continue the process.
875 * The similar logic is used by the vhost_virtqueue_start()
880 r = vhost_virtqueue_set_addr(dev, dev->vqs + i, idx,
888 for (; i >= 0; --i) {
889 idx = dev->vhost_ops->vhost_get_vq_index(dev, dev->vq_index + i);
890 vhost_virtqueue_set_addr(dev, dev->vqs + i, idx,
893 vhost_dev_set_features(dev, dev->log_enabled);
898 static int vhost_migration_log(MemoryListener *listener, bool enable)
900 struct vhost_dev *dev = container_of(listener, struct vhost_dev,
903 if (enable == dev->log_enabled) {
907 dev->log_enabled = enable;
913 r = vhost_dev_set_log(dev, false);
915 goto check_dev_state;
917 vhost_log_put(dev, false);
919 vhost_dev_log_resize(dev, vhost_get_log_size(dev));
920 r = vhost_dev_set_log(dev, true);
922 goto check_dev_state;
927 dev->log_enabled = enable;
929 * vhost-user-* devices could change their state during log
930 * initialization due to disconnect. So check dev state after
931 * vhost communication.
935 * Since device is in the stopped state, it is okay for
936 * migration. Return success.
941 /* An error occurred. */
942 dev->log_enabled = false;
948 static void vhost_log_global_start(MemoryListener *listener)
952 r = vhost_migration_log(listener, true);
958 static void vhost_log_global_stop(MemoryListener *listener)
962 r = vhost_migration_log(listener, false);
968 static void vhost_log_start(MemoryListener *listener,
969 MemoryRegionSection *section,
972 /* FIXME: implement */
975 static void vhost_log_stop(MemoryListener *listener,
976 MemoryRegionSection *section,
979 /* FIXME: implement */
982 /* The vhost driver natively knows how to handle the vrings of non
983 * cross-endian legacy devices and modern devices. Only legacy devices
984 * exposed to a bi-endian guest may require the vhost driver to use a
985 * specific endianness.
987 static inline bool vhost_needs_vring_endian(VirtIODevice *vdev)
989 if (virtio_vdev_has_feature(vdev, VIRTIO_F_VERSION_1)) {
992 #ifdef HOST_WORDS_BIGENDIAN
993 return vdev->device_endian == VIRTIO_DEVICE_ENDIAN_LITTLE;
995 return vdev->device_endian == VIRTIO_DEVICE_ENDIAN_BIG;
999 static int vhost_virtqueue_set_vring_endian_legacy(struct vhost_dev *dev,
1004 struct vhost_vring_state s = {
1005 .index = vhost_vq_index,
1006 .num = is_big_endian
1009 r = dev->vhost_ops->vhost_set_vring_endian(dev, &s);
1011 VHOST_OPS_DEBUG(r, "vhost_set_vring_endian failed");
1016 static int vhost_memory_region_lookup(struct vhost_dev *hdev,
1017 uint64_t gpa, uint64_t *uaddr,
1022 for (i = 0; i < hdev->mem->nregions; i++) {
1023 struct vhost_memory_region *reg = hdev->mem->regions + i;
1025 if (gpa >= reg->guest_phys_addr &&
1026 reg->guest_phys_addr + reg->memory_size > gpa) {
1027 *uaddr = reg->userspace_addr + gpa - reg->guest_phys_addr;
1028 *len = reg->guest_phys_addr + reg->memory_size - gpa;
1036 int vhost_device_iotlb_miss(struct vhost_dev *dev, uint64_t iova, int write)
1038 IOMMUTLBEntry iotlb;
1039 uint64_t uaddr, len;
1042 RCU_READ_LOCK_GUARD();
1044 trace_vhost_iotlb_miss(dev, 1);
1046 iotlb = address_space_get_iotlb_entry(dev->vdev->dma_as,
1048 MEMTXATTRS_UNSPECIFIED);
1049 if (iotlb.target_as != NULL) {
1050 ret = vhost_memory_region_lookup(dev, iotlb.translated_addr,
1053 trace_vhost_iotlb_miss(dev, 3);
1054 error_report("Fail to lookup the translated address "
1055 "%"PRIx64, iotlb.translated_addr);
1059 len = MIN(iotlb.addr_mask + 1, len);
1060 iova = iova & ~iotlb.addr_mask;
1062 ret = vhost_backend_update_device_iotlb(dev, iova, uaddr,
1065 trace_vhost_iotlb_miss(dev, 4);
1066 error_report("Fail to update device iotlb");
1071 trace_vhost_iotlb_miss(dev, 2);
1077 static int vhost_virtqueue_start(struct vhost_dev *dev,
1078 struct VirtIODevice *vdev,
1079 struct vhost_virtqueue *vq,
1082 BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(vdev)));
1083 VirtioBusState *vbus = VIRTIO_BUS(qbus);
1084 VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(vbus);
1087 int vhost_vq_index = dev->vhost_ops->vhost_get_vq_index(dev, idx);
1088 struct vhost_vring_file file = {
1089 .index = vhost_vq_index
1091 struct vhost_vring_state state = {
1092 .index = vhost_vq_index
1094 struct VirtQueue *vvq = virtio_get_queue(vdev, idx);
1096 a = virtio_queue_get_desc_addr(vdev, idx);
1098 /* Queue might not be ready for start */
1102 vq->num = state.num = virtio_queue_get_num(vdev, idx);
1103 r = dev->vhost_ops->vhost_set_vring_num(dev, &state);
1105 VHOST_OPS_DEBUG(r, "vhost_set_vring_num failed");
1109 state.num = virtio_queue_get_last_avail_idx(vdev, idx);
1110 r = dev->vhost_ops->vhost_set_vring_base(dev, &state);
1112 VHOST_OPS_DEBUG(r, "vhost_set_vring_base failed");
1116 if (vhost_needs_vring_endian(vdev)) {
1117 r = vhost_virtqueue_set_vring_endian_legacy(dev,
1118 virtio_is_big_endian(vdev),
1125 vq->desc_size = s = l = virtio_queue_get_desc_size(vdev, idx);
1127 vq->desc = vhost_memory_map(dev, a, &l, false);
1128 if (!vq->desc || l != s) {
1130 goto fail_alloc_desc;
1132 vq->avail_size = s = l = virtio_queue_get_avail_size(vdev, idx);
1133 vq->avail_phys = a = virtio_queue_get_avail_addr(vdev, idx);
1134 vq->avail = vhost_memory_map(dev, a, &l, false);
1135 if (!vq->avail || l != s) {
1137 goto fail_alloc_avail;
1139 vq->used_size = s = l = virtio_queue_get_used_size(vdev, idx);
1140 vq->used_phys = a = virtio_queue_get_used_addr(vdev, idx);
1141 vq->used = vhost_memory_map(dev, a, &l, true);
1142 if (!vq->used || l != s) {
1144 goto fail_alloc_used;
1147 r = vhost_virtqueue_set_addr(dev, vq, vhost_vq_index, dev->log_enabled);
1152 file.fd = event_notifier_get_fd(virtio_queue_get_host_notifier(vvq));
1153 r = dev->vhost_ops->vhost_set_vring_kick(dev, &file);
1155 VHOST_OPS_DEBUG(r, "vhost_set_vring_kick failed");
1159 /* Clear and discard previous events if any. */
1160 event_notifier_test_and_clear(&vq->masked_notifier);
1162 /* Init vring in unmasked state, unless guest_notifier_mask
1165 if (!vdev->use_guest_notifier_mask) {
1166 /* TODO: check and handle errors. */
1167 vhost_virtqueue_mask(dev, vdev, idx, false);
1170 if (k->query_guest_notifiers &&
1171 k->query_guest_notifiers(qbus->parent) &&
1172 virtio_queue_vector(vdev, idx) == VIRTIO_NO_VECTOR) {
1174 r = dev->vhost_ops->vhost_set_vring_call(dev, &file);
1185 vhost_memory_unmap(dev, vq->used, virtio_queue_get_used_size(vdev, idx),
1188 vhost_memory_unmap(dev, vq->avail, virtio_queue_get_avail_size(vdev, idx),
1191 vhost_memory_unmap(dev, vq->desc, virtio_queue_get_desc_size(vdev, idx),
1197 static void vhost_virtqueue_stop(struct vhost_dev *dev,
1198 struct VirtIODevice *vdev,
1199 struct vhost_virtqueue *vq,
1202 int vhost_vq_index = dev->vhost_ops->vhost_get_vq_index(dev, idx);
1203 struct vhost_vring_state state = {
1204 .index = vhost_vq_index,
1208 if (virtio_queue_get_desc_addr(vdev, idx) == 0) {
1209 /* Don't stop the virtqueue which might have not been started */
1213 r = dev->vhost_ops->vhost_get_vring_base(dev, &state);
1215 VHOST_OPS_DEBUG(r, "vhost VQ %u ring restore failed: %d", idx, r);
1216 /* Connection to the backend is broken, so let's sync internal
1217 * last avail idx to the device used idx.
1219 virtio_queue_restore_last_avail_idx(vdev, idx);
1221 virtio_queue_set_last_avail_idx(vdev, idx, state.num);
1223 virtio_queue_invalidate_signalled_used(vdev, idx);
1224 virtio_queue_update_used_idx(vdev, idx);
1226 /* In the cross-endian case, we need to reset the vring endianness to
1227 * native as legacy devices expect so by default.
1229 if (vhost_needs_vring_endian(vdev)) {
1230 vhost_virtqueue_set_vring_endian_legacy(dev,
1231 !virtio_is_big_endian(vdev),
1235 vhost_memory_unmap(dev, vq->used, virtio_queue_get_used_size(vdev, idx),
1236 1, virtio_queue_get_used_size(vdev, idx));
1237 vhost_memory_unmap(dev, vq->avail, virtio_queue_get_avail_size(vdev, idx),
1238 0, virtio_queue_get_avail_size(vdev, idx));
1239 vhost_memory_unmap(dev, vq->desc, virtio_queue_get_desc_size(vdev, idx),
1240 0, virtio_queue_get_desc_size(vdev, idx));
1243 static void vhost_eventfd_add(MemoryListener *listener,
1244 MemoryRegionSection *section,
1245 bool match_data, uint64_t data, EventNotifier *e)
1249 static void vhost_eventfd_del(MemoryListener *listener,
1250 MemoryRegionSection *section,
1251 bool match_data, uint64_t data, EventNotifier *e)
1255 static int vhost_virtqueue_set_busyloop_timeout(struct vhost_dev *dev,
1256 int n, uint32_t timeout)
1258 int vhost_vq_index = dev->vhost_ops->vhost_get_vq_index(dev, n);
1259 struct vhost_vring_state state = {
1260 .index = vhost_vq_index,
1265 if (!dev->vhost_ops->vhost_set_vring_busyloop_timeout) {
1269 r = dev->vhost_ops->vhost_set_vring_busyloop_timeout(dev, &state);
1271 VHOST_OPS_DEBUG(r, "vhost_set_vring_busyloop_timeout failed");
1278 static int vhost_virtqueue_init(struct vhost_dev *dev,
1279 struct vhost_virtqueue *vq, int n)
1281 int vhost_vq_index = dev->vhost_ops->vhost_get_vq_index(dev, n);
1282 struct vhost_vring_file file = {
1283 .index = vhost_vq_index,
1285 int r = event_notifier_init(&vq->masked_notifier, 0);
1290 file.fd = event_notifier_get_fd(&vq->masked_notifier);
1291 r = dev->vhost_ops->vhost_set_vring_call(dev, &file);
1293 VHOST_OPS_DEBUG(r, "vhost_set_vring_call failed");
1301 event_notifier_cleanup(&vq->masked_notifier);
1305 static void vhost_virtqueue_cleanup(struct vhost_virtqueue *vq)
1307 event_notifier_cleanup(&vq->masked_notifier);
1310 int vhost_dev_init(struct vhost_dev *hdev, void *opaque,
1311 VhostBackendType backend_type, uint32_t busyloop_timeout,
1315 int i, r, n_initialized_vqs = 0;
1318 hdev->migration_blocker = NULL;
1320 r = vhost_set_backend_type(hdev, backend_type);
1323 r = hdev->vhost_ops->vhost_backend_init(hdev, opaque, errp);
1328 r = hdev->vhost_ops->vhost_set_owner(hdev);
1330 error_setg_errno(errp, -r, "vhost_set_owner failed");
1334 r = hdev->vhost_ops->vhost_get_features(hdev, &features);
1336 error_setg_errno(errp, -r, "vhost_get_features failed");
1340 for (i = 0; i < hdev->nvqs; ++i, ++n_initialized_vqs) {
1341 r = vhost_virtqueue_init(hdev, hdev->vqs + i, hdev->vq_index + i);
1343 error_setg_errno(errp, -r, "Failed to initialize virtqueue %d", i);
1348 if (busyloop_timeout) {
1349 for (i = 0; i < hdev->nvqs; ++i) {
1350 r = vhost_virtqueue_set_busyloop_timeout(hdev, hdev->vq_index + i,
1353 error_setg_errno(errp, -r, "Failed to set busyloop timeout");
1359 hdev->features = features;
1361 hdev->memory_listener = (MemoryListener) {
1363 .begin = vhost_begin,
1364 .commit = vhost_commit,
1365 .region_add = vhost_region_addnop,
1366 .region_nop = vhost_region_addnop,
1367 .log_start = vhost_log_start,
1368 .log_stop = vhost_log_stop,
1369 .log_sync = vhost_log_sync,
1370 .log_global_start = vhost_log_global_start,
1371 .log_global_stop = vhost_log_global_stop,
1372 .eventfd_add = vhost_eventfd_add,
1373 .eventfd_del = vhost_eventfd_del,
1377 hdev->iommu_listener = (MemoryListener) {
1378 .name = "vhost-iommu",
1379 .region_add = vhost_iommu_region_add,
1380 .region_del = vhost_iommu_region_del,
1383 if (hdev->migration_blocker == NULL) {
1384 if (!(hdev->features & (0x1ULL << VHOST_F_LOG_ALL))) {
1385 error_setg(&hdev->migration_blocker,
1386 "Migration disabled: vhost lacks VHOST_F_LOG_ALL feature.");
1387 } else if (vhost_dev_log_is_shared(hdev) && !qemu_memfd_alloc_check()) {
1388 error_setg(&hdev->migration_blocker,
1389 "Migration disabled: failed to allocate shared memory");
1393 if (hdev->migration_blocker != NULL) {
1394 r = migrate_add_blocker(hdev->migration_blocker, errp);
1396 error_free(hdev->migration_blocker);
1401 hdev->mem = g_malloc0(offsetof(struct vhost_memory, regions));
1402 hdev->n_mem_sections = 0;
1403 hdev->mem_sections = NULL;
1406 hdev->log_enabled = false;
1407 hdev->started = false;
1408 memory_listener_register(&hdev->memory_listener, &address_space_memory);
1409 QLIST_INSERT_HEAD(&vhost_devices, hdev, entry);
1411 if (used_memslots > hdev->vhost_ops->vhost_backend_memslots_limit(hdev)) {
1412 error_setg(errp, "vhost backend memory slots limit is less"
1413 " than current number of present memory slots");
1421 if (busyloop_timeout) {
1423 vhost_virtqueue_set_busyloop_timeout(hdev, hdev->vq_index + i, 0);
1427 hdev->nvqs = n_initialized_vqs;
1428 vhost_dev_cleanup(hdev);
1432 void vhost_dev_cleanup(struct vhost_dev *hdev)
1436 for (i = 0; i < hdev->nvqs; ++i) {
1437 vhost_virtqueue_cleanup(hdev->vqs + i);
1440 /* those are only safe after successful init */
1441 memory_listener_unregister(&hdev->memory_listener);
1442 QLIST_REMOVE(hdev, entry);
1444 if (hdev->migration_blocker) {
1445 migrate_del_blocker(hdev->migration_blocker);
1446 error_free(hdev->migration_blocker);
1449 g_free(hdev->mem_sections);
1450 if (hdev->vhost_ops) {
1451 hdev->vhost_ops->vhost_backend_cleanup(hdev);
1455 memset(hdev, 0, sizeof(struct vhost_dev));
1458 /* Stop processing guest IO notifications in qemu.
1459 * Start processing them in vhost in kernel.
1461 int vhost_dev_enable_notifiers(struct vhost_dev *hdev, VirtIODevice *vdev)
1463 BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(vdev)));
1466 /* We will pass the notifiers to the kernel, make sure that QEMU
1467 * doesn't interfere.
1469 r = virtio_device_grab_ioeventfd(vdev);
1471 error_report("binding does not support host notifiers");
1475 for (i = 0; i < hdev->nvqs; ++i) {
1476 r = virtio_bus_set_host_notifier(VIRTIO_BUS(qbus), hdev->vq_index + i,
1479 error_report("vhost VQ %d notifier binding failed: %d", i, -r);
1487 e = virtio_bus_set_host_notifier(VIRTIO_BUS(qbus), hdev->vq_index + i,
1490 error_report("vhost VQ %d notifier cleanup error: %d", i, -r);
1493 virtio_bus_cleanup_host_notifier(VIRTIO_BUS(qbus), hdev->vq_index + i);
1495 virtio_device_release_ioeventfd(vdev);
1500 /* Stop processing guest IO notifications in vhost.
1501 * Start processing them in qemu.
1502 * This might actually run the qemu handlers right away,
1503 * so virtio in qemu must be completely setup when this is called.
1505 void vhost_dev_disable_notifiers(struct vhost_dev *hdev, VirtIODevice *vdev)
1507 BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(vdev)));
1510 for (i = 0; i < hdev->nvqs; ++i) {
1511 r = virtio_bus_set_host_notifier(VIRTIO_BUS(qbus), hdev->vq_index + i,
1514 error_report("vhost VQ %d notifier cleanup failed: %d", i, -r);
1517 virtio_bus_cleanup_host_notifier(VIRTIO_BUS(qbus), hdev->vq_index + i);
1519 virtio_device_release_ioeventfd(vdev);
1522 /* Test and clear event pending status.
1523 * Should be called after unmask to avoid losing events.
1525 bool vhost_virtqueue_pending(struct vhost_dev *hdev, int n)
1527 struct vhost_virtqueue *vq = hdev->vqs + n - hdev->vq_index;
1528 assert(n >= hdev->vq_index && n < hdev->vq_index + hdev->nvqs);
1529 return event_notifier_test_and_clear(&vq->masked_notifier);
1532 /* Mask/unmask events from this vq. */
1533 void vhost_virtqueue_mask(struct vhost_dev *hdev, VirtIODevice *vdev, int n,
1536 struct VirtQueue *vvq = virtio_get_queue(vdev, n);
1537 int r, index = n - hdev->vq_index;
1538 struct vhost_vring_file file;
1540 /* should only be called after backend is connected */
1541 assert(hdev->vhost_ops);
1544 assert(vdev->use_guest_notifier_mask);
1545 file.fd = event_notifier_get_fd(&hdev->vqs[index].masked_notifier);
1547 file.fd = event_notifier_get_fd(virtio_queue_get_guest_notifier(vvq));
1550 file.index = hdev->vhost_ops->vhost_get_vq_index(hdev, n);
1551 r = hdev->vhost_ops->vhost_set_vring_call(hdev, &file);
1553 VHOST_OPS_DEBUG(r, "vhost_set_vring_call failed");
1557 uint64_t vhost_get_features(struct vhost_dev *hdev, const int *feature_bits,
1560 const int *bit = feature_bits;
1561 while (*bit != VHOST_INVALID_FEATURE_BIT) {
1562 uint64_t bit_mask = (1ULL << *bit);
1563 if (!(hdev->features & bit_mask)) {
1564 features &= ~bit_mask;
1571 void vhost_ack_features(struct vhost_dev *hdev, const int *feature_bits,
1574 const int *bit = feature_bits;
1575 while (*bit != VHOST_INVALID_FEATURE_BIT) {
1576 uint64_t bit_mask = (1ULL << *bit);
1577 if (features & bit_mask) {
1578 hdev->acked_features |= bit_mask;
1584 int vhost_dev_get_config(struct vhost_dev *hdev, uint8_t *config,
1585 uint32_t config_len, Error **errp)
1587 assert(hdev->vhost_ops);
1589 if (hdev->vhost_ops->vhost_get_config) {
1590 return hdev->vhost_ops->vhost_get_config(hdev, config, config_len,
1594 error_setg(errp, "vhost_get_config not implemented");
1598 int vhost_dev_set_config(struct vhost_dev *hdev, const uint8_t *data,
1599 uint32_t offset, uint32_t size, uint32_t flags)
1601 assert(hdev->vhost_ops);
1603 if (hdev->vhost_ops->vhost_set_config) {
1604 return hdev->vhost_ops->vhost_set_config(hdev, data, offset,
1611 void vhost_dev_set_config_notifier(struct vhost_dev *hdev,
1612 const VhostDevConfigOps *ops)
1614 hdev->config_ops = ops;
1617 void vhost_dev_free_inflight(struct vhost_inflight *inflight)
1619 if (inflight && inflight->addr) {
1620 qemu_memfd_free(inflight->addr, inflight->size, inflight->fd);
1621 inflight->addr = NULL;
1626 static int vhost_dev_resize_inflight(struct vhost_inflight *inflight,
1631 void *addr = qemu_memfd_alloc("vhost-inflight", new_size,
1632 F_SEAL_GROW | F_SEAL_SHRINK | F_SEAL_SEAL,
1636 error_report_err(err);
1640 vhost_dev_free_inflight(inflight);
1641 inflight->offset = 0;
1642 inflight->addr = addr;
1644 inflight->size = new_size;
1649 void vhost_dev_save_inflight(struct vhost_inflight *inflight, QEMUFile *f)
1651 if (inflight->addr) {
1652 qemu_put_be64(f, inflight->size);
1653 qemu_put_be16(f, inflight->queue_size);
1654 qemu_put_buffer(f, inflight->addr, inflight->size);
1656 qemu_put_be64(f, 0);
1660 int vhost_dev_load_inflight(struct vhost_inflight *inflight, QEMUFile *f)
1664 size = qemu_get_be64(f);
1669 if (inflight->size != size) {
1670 int ret = vhost_dev_resize_inflight(inflight, size);
1675 inflight->queue_size = qemu_get_be16(f);
1677 qemu_get_buffer(f, inflight->addr, size);
1682 int vhost_dev_prepare_inflight(struct vhost_dev *hdev, VirtIODevice *vdev)
1686 if (hdev->vhost_ops->vhost_get_inflight_fd == NULL ||
1687 hdev->vhost_ops->vhost_set_inflight_fd == NULL) {
1693 r = vhost_dev_set_features(hdev, hdev->log_enabled);
1695 VHOST_OPS_DEBUG(r, "vhost_dev_prepare_inflight failed");
1702 int vhost_dev_set_inflight(struct vhost_dev *dev,
1703 struct vhost_inflight *inflight)
1707 if (dev->vhost_ops->vhost_set_inflight_fd && inflight->addr) {
1708 r = dev->vhost_ops->vhost_set_inflight_fd(dev, inflight);
1710 VHOST_OPS_DEBUG(r, "vhost_set_inflight_fd failed");
1718 int vhost_dev_get_inflight(struct vhost_dev *dev, uint16_t queue_size,
1719 struct vhost_inflight *inflight)
1723 if (dev->vhost_ops->vhost_get_inflight_fd) {
1724 r = dev->vhost_ops->vhost_get_inflight_fd(dev, queue_size, inflight);
1726 VHOST_OPS_DEBUG(r, "vhost_get_inflight_fd failed");
1734 /* Host notifiers must be enabled at this point. */
1735 int vhost_dev_start(struct vhost_dev *hdev, VirtIODevice *vdev)
1739 /* should only be called after backend is connected */
1740 assert(hdev->vhost_ops);
1742 hdev->started = true;
1745 r = vhost_dev_set_features(hdev, hdev->log_enabled);
1750 if (vhost_dev_has_iommu(hdev)) {
1751 memory_listener_register(&hdev->iommu_listener, vdev->dma_as);
1754 r = hdev->vhost_ops->vhost_set_mem_table(hdev, hdev->mem);
1756 VHOST_OPS_DEBUG(r, "vhost_set_mem_table failed");
1759 for (i = 0; i < hdev->nvqs; ++i) {
1760 r = vhost_virtqueue_start(hdev,
1763 hdev->vq_index + i);
1769 if (hdev->log_enabled) {
1772 hdev->log_size = vhost_get_log_size(hdev);
1773 hdev->log = vhost_log_get(hdev->log_size,
1774 vhost_dev_log_is_shared(hdev));
1775 log_base = (uintptr_t)hdev->log->log;
1776 r = hdev->vhost_ops->vhost_set_log_base(hdev,
1777 hdev->log_size ? log_base : 0,
1780 VHOST_OPS_DEBUG(r, "vhost_set_log_base failed");
1784 if (hdev->vhost_ops->vhost_dev_start) {
1785 r = hdev->vhost_ops->vhost_dev_start(hdev, true);
1790 if (vhost_dev_has_iommu(hdev) &&
1791 hdev->vhost_ops->vhost_set_iotlb_callback) {
1792 hdev->vhost_ops->vhost_set_iotlb_callback(hdev, true);
1794 /* Update used ring information for IOTLB to work correctly,
1795 * vhost-kernel code requires for this.*/
1796 for (i = 0; i < hdev->nvqs; ++i) {
1797 struct vhost_virtqueue *vq = hdev->vqs + i;
1798 vhost_device_iotlb_miss(hdev, vq->used_phys, true);
1803 vhost_log_put(hdev, false);
1806 vhost_virtqueue_stop(hdev,
1809 hdev->vq_index + i);
1815 hdev->started = false;
1819 /* Host notifiers must be enabled at this point. */
1820 void vhost_dev_stop(struct vhost_dev *hdev, VirtIODevice *vdev)
1824 /* should only be called after backend is connected */
1825 assert(hdev->vhost_ops);
1827 if (hdev->vhost_ops->vhost_dev_start) {
1828 hdev->vhost_ops->vhost_dev_start(hdev, false);
1830 for (i = 0; i < hdev->nvqs; ++i) {
1831 vhost_virtqueue_stop(hdev,
1834 hdev->vq_index + i);
1837 if (vhost_dev_has_iommu(hdev)) {
1838 if (hdev->vhost_ops->vhost_set_iotlb_callback) {
1839 hdev->vhost_ops->vhost_set_iotlb_callback(hdev, false);
1841 memory_listener_unregister(&hdev->iommu_listener);
1843 vhost_log_put(hdev, true);
1844 hdev->started = false;
1848 int vhost_net_set_backend(struct vhost_dev *hdev,
1849 struct vhost_vring_file *file)
1851 if (hdev->vhost_ops->vhost_net_set_backend) {
1852 return hdev->vhost_ops->vhost_net_set_backend(hdev, file);