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(fmt, ...) \
37 do { error_report(fmt ": %s (%d)", ## __VA_ARGS__, \
38 strerror(errno), errno); } while (0)
40 #define VHOST_OPS_DEBUG(fmt, ...) \
44 static struct vhost_log *vhost_log;
45 static struct vhost_log *vhost_log_shm;
47 static unsigned int used_memslots;
48 static QLIST_HEAD(, vhost_dev) vhost_devices =
49 QLIST_HEAD_INITIALIZER(vhost_devices);
51 bool vhost_has_free_slot(void)
53 unsigned int slots_limit = ~0U;
54 struct vhost_dev *hdev;
56 QLIST_FOREACH(hdev, &vhost_devices, entry) {
57 unsigned int r = hdev->vhost_ops->vhost_backend_memslots_limit(hdev);
58 slots_limit = MIN(slots_limit, r);
60 return slots_limit > used_memslots;
63 static void vhost_dev_sync_region(struct vhost_dev *dev,
64 MemoryRegionSection *section,
65 uint64_t mfirst, uint64_t mlast,
66 uint64_t rfirst, uint64_t rlast)
68 vhost_log_chunk_t *log = dev->log->log;
70 uint64_t start = MAX(mfirst, rfirst);
71 uint64_t end = MIN(mlast, rlast);
72 vhost_log_chunk_t *from = log + start / VHOST_LOG_CHUNK;
73 vhost_log_chunk_t *to = log + end / VHOST_LOG_CHUNK + 1;
74 uint64_t addr = QEMU_ALIGN_DOWN(start, VHOST_LOG_CHUNK);
79 assert(end / VHOST_LOG_CHUNK < dev->log_size);
80 assert(start / VHOST_LOG_CHUNK < dev->log_size);
82 for (;from < to; ++from) {
83 vhost_log_chunk_t log;
84 /* We first check with non-atomic: much cheaper,
85 * and we expect non-dirty to be the common case. */
87 addr += VHOST_LOG_CHUNK;
90 /* Data must be read atomically. We don't really need barrier semantics
91 * but it's easier to use atomic_* than roll our own. */
92 log = qatomic_xchg(from, 0);
96 hwaddr section_offset;
98 page_addr = addr + bit * VHOST_LOG_PAGE;
99 section_offset = page_addr - section->offset_within_address_space;
100 mr_offset = section_offset + section->offset_within_region;
101 memory_region_set_dirty(section->mr, mr_offset, VHOST_LOG_PAGE);
102 log &= ~(0x1ull << bit);
104 addr += VHOST_LOG_CHUNK;
108 static int vhost_sync_dirty_bitmap(struct vhost_dev *dev,
109 MemoryRegionSection *section,
117 if (!dev->log_enabled || !dev->started) {
120 start_addr = section->offset_within_address_space;
121 end_addr = range_get_last(start_addr, int128_get64(section->size));
122 start_addr = MAX(first, start_addr);
123 end_addr = MIN(last, end_addr);
125 for (i = 0; i < dev->mem->nregions; ++i) {
126 struct vhost_memory_region *reg = dev->mem->regions + i;
127 vhost_dev_sync_region(dev, section, start_addr, end_addr,
128 reg->guest_phys_addr,
129 range_get_last(reg->guest_phys_addr,
132 for (i = 0; i < dev->nvqs; ++i) {
133 struct vhost_virtqueue *vq = dev->vqs + i;
135 if (!vq->used_phys && !vq->used_size) {
139 vhost_dev_sync_region(dev, section, start_addr, end_addr, vq->used_phys,
140 range_get_last(vq->used_phys, vq->used_size));
145 static void vhost_log_sync(MemoryListener *listener,
146 MemoryRegionSection *section)
148 struct vhost_dev *dev = container_of(listener, struct vhost_dev,
150 vhost_sync_dirty_bitmap(dev, section, 0x0, ~0x0ULL);
153 static void vhost_log_sync_range(struct vhost_dev *dev,
154 hwaddr first, hwaddr last)
157 /* FIXME: this is N^2 in number of sections */
158 for (i = 0; i < dev->n_mem_sections; ++i) {
159 MemoryRegionSection *section = &dev->mem_sections[i];
160 vhost_sync_dirty_bitmap(dev, section, first, last);
164 static uint64_t vhost_get_log_size(struct vhost_dev *dev)
166 uint64_t log_size = 0;
168 for (i = 0; i < dev->mem->nregions; ++i) {
169 struct vhost_memory_region *reg = dev->mem->regions + i;
170 uint64_t last = range_get_last(reg->guest_phys_addr,
172 log_size = MAX(log_size, last / VHOST_LOG_CHUNK + 1);
177 static struct vhost_log *vhost_log_alloc(uint64_t size, bool share)
180 struct vhost_log *log;
181 uint64_t logsize = size * sizeof(*(log->log));
184 log = g_new0(struct vhost_log, 1);
186 log->log = qemu_memfd_alloc("vhost-log", logsize,
187 F_SEAL_GROW | F_SEAL_SHRINK | F_SEAL_SEAL,
190 error_report_err(err);
194 memset(log->log, 0, logsize);
196 log->log = g_malloc0(logsize);
206 static struct vhost_log *vhost_log_get(uint64_t size, bool share)
208 struct vhost_log *log = share ? vhost_log_shm : vhost_log;
210 if (!log || log->size != size) {
211 log = vhost_log_alloc(size, share);
224 static void vhost_log_put(struct vhost_dev *dev, bool sync)
226 struct vhost_log *log = dev->log;
233 if (log->refcnt == 0) {
234 /* Sync only the range covered by the old log */
235 if (dev->log_size && sync) {
236 vhost_log_sync_range(dev, 0, dev->log_size * VHOST_LOG_CHUNK - 1);
239 if (vhost_log == log) {
242 } else if (vhost_log_shm == log) {
243 qemu_memfd_free(log->log, log->size * sizeof(*(log->log)),
245 vhost_log_shm = NULL;
255 static bool vhost_dev_log_is_shared(struct vhost_dev *dev)
257 return dev->vhost_ops->vhost_requires_shm_log &&
258 dev->vhost_ops->vhost_requires_shm_log(dev);
261 static inline void vhost_dev_log_resize(struct vhost_dev *dev, uint64_t size)
263 struct vhost_log *log = vhost_log_get(size, vhost_dev_log_is_shared(dev));
264 uint64_t log_base = (uintptr_t)log->log;
267 /* inform backend of log switching, this must be done before
268 releasing the current log, to ensure no logging is lost */
269 r = dev->vhost_ops->vhost_set_log_base(dev, log_base, log);
271 VHOST_OPS_DEBUG("vhost_set_log_base failed");
274 vhost_log_put(dev, true);
276 dev->log_size = size;
279 static int vhost_dev_has_iommu(struct vhost_dev *dev)
281 VirtIODevice *vdev = dev->vdev;
284 * For vhost, VIRTIO_F_IOMMU_PLATFORM means the backend support
285 * incremental memory mapping API via IOTLB API. For platform that
286 * does not have IOMMU, there's no need to enable this feature
287 * which may cause unnecessary IOTLB miss/update trnasactions.
289 return vdev->dma_as != &address_space_memory &&
290 virtio_host_has_feature(vdev, VIRTIO_F_IOMMU_PLATFORM);
293 static void *vhost_memory_map(struct vhost_dev *dev, hwaddr addr,
294 hwaddr *plen, bool is_write)
296 if (!vhost_dev_has_iommu(dev)) {
297 return cpu_physical_memory_map(addr, plen, is_write);
299 return (void *)(uintptr_t)addr;
303 static void vhost_memory_unmap(struct vhost_dev *dev, void *buffer,
304 hwaddr len, int is_write,
307 if (!vhost_dev_has_iommu(dev)) {
308 cpu_physical_memory_unmap(buffer, len, is_write, access_len);
312 static int vhost_verify_ring_part_mapping(void *ring_hva,
319 uint64_t hva_ring_offset;
320 uint64_t ring_last = range_get_last(ring_gpa, ring_size);
321 uint64_t reg_last = range_get_last(reg_gpa, reg_size);
323 if (ring_last < reg_gpa || ring_gpa > reg_last) {
326 /* check that whole ring's is mapped */
327 if (ring_last > reg_last) {
330 /* check that ring's MemoryRegion wasn't replaced */
331 hva_ring_offset = ring_gpa - reg_gpa;
332 if (ring_hva != reg_hva + hva_ring_offset) {
339 static int vhost_verify_ring_mappings(struct vhost_dev *dev,
346 const char *part_name[] = {
352 if (vhost_dev_has_iommu(dev)) {
356 for (i = 0; i < dev->nvqs; ++i) {
357 struct vhost_virtqueue *vq = dev->vqs + i;
359 if (vq->desc_phys == 0) {
364 r = vhost_verify_ring_part_mapping(
365 vq->desc, vq->desc_phys, vq->desc_size,
366 reg_hva, reg_gpa, reg_size);
372 r = vhost_verify_ring_part_mapping(
373 vq->avail, vq->avail_phys, vq->avail_size,
374 reg_hva, reg_gpa, reg_size);
380 r = vhost_verify_ring_part_mapping(
381 vq->used, vq->used_phys, vq->used_size,
382 reg_hva, reg_gpa, reg_size);
389 error_report("Unable to map %s for ring %d", part_name[j], i);
390 } else if (r == -EBUSY) {
391 error_report("%s relocated for ring %d", part_name[j], i);
397 * vhost_section: identify sections needed for vhost access
399 * We only care about RAM sections here (where virtqueue and guest
400 * internals accessed by virtio might live). If we find one we still
401 * allow the backend to potentially filter it out of our list.
403 static bool vhost_section(struct vhost_dev *dev, MemoryRegionSection *section)
405 MemoryRegion *mr = section->mr;
407 if (memory_region_is_ram(mr) && !memory_region_is_rom(mr)) {
408 uint8_t dirty_mask = memory_region_get_dirty_log_mask(mr);
409 uint8_t handled_dirty;
412 * Kernel based vhost doesn't handle any block which is doing
413 * dirty-tracking other than migration for which it has
414 * specific logging support. However for TCG the kernel never
415 * gets involved anyway so we can also ignore it's
416 * self-modiying code detection flags. However a vhost-user
417 * client could still confuse a TCG guest if it re-writes
418 * executable memory that has already been translated.
420 handled_dirty = (1 << DIRTY_MEMORY_MIGRATION) |
421 (1 << DIRTY_MEMORY_CODE);
423 if (dirty_mask & ~handled_dirty) {
424 trace_vhost_reject_section(mr->name, 1);
428 if (dev->vhost_ops->vhost_backend_mem_section_filter &&
429 !dev->vhost_ops->vhost_backend_mem_section_filter(dev, section)) {
430 trace_vhost_reject_section(mr->name, 2);
434 trace_vhost_section(mr->name);
437 trace_vhost_reject_section(mr->name, 3);
442 static void vhost_begin(MemoryListener *listener)
444 struct vhost_dev *dev = container_of(listener, struct vhost_dev,
446 dev->tmp_sections = NULL;
447 dev->n_tmp_sections = 0;
450 static void vhost_commit(MemoryListener *listener)
452 struct vhost_dev *dev = container_of(listener, struct vhost_dev,
454 MemoryRegionSection *old_sections;
460 bool changed = false;
462 /* Note we can be called before the device is started, but then
463 * starting the device calls set_mem_table, so we need to have
464 * built the data structures.
466 old_sections = dev->mem_sections;
467 n_old_sections = dev->n_mem_sections;
468 dev->mem_sections = dev->tmp_sections;
469 dev->n_mem_sections = dev->n_tmp_sections;
471 if (dev->n_mem_sections != n_old_sections) {
474 /* Same size, lets check the contents */
475 for (int i = 0; i < n_old_sections; i++) {
476 if (!MemoryRegionSection_eq(&old_sections[i],
477 &dev->mem_sections[i])) {
484 trace_vhost_commit(dev->started, changed);
489 /* Rebuild the regions list from the new sections list */
490 regions_size = offsetof(struct vhost_memory, regions) +
491 dev->n_mem_sections * sizeof dev->mem->regions[0];
492 dev->mem = g_realloc(dev->mem, regions_size);
493 dev->mem->nregions = dev->n_mem_sections;
494 used_memslots = dev->mem->nregions;
495 for (i = 0; i < dev->n_mem_sections; i++) {
496 struct vhost_memory_region *cur_vmr = dev->mem->regions + i;
497 struct MemoryRegionSection *mrs = dev->mem_sections + i;
499 cur_vmr->guest_phys_addr = mrs->offset_within_address_space;
500 cur_vmr->memory_size = int128_get64(mrs->size);
501 cur_vmr->userspace_addr =
502 (uintptr_t)memory_region_get_ram_ptr(mrs->mr) +
503 mrs->offset_within_region;
504 cur_vmr->flags_padding = 0;
511 for (i = 0; i < dev->mem->nregions; i++) {
512 if (vhost_verify_ring_mappings(dev,
513 (void *)(uintptr_t)dev->mem->regions[i].userspace_addr,
514 dev->mem->regions[i].guest_phys_addr,
515 dev->mem->regions[i].memory_size)) {
516 error_report("Verify ring failure on region %d", i);
521 if (!dev->log_enabled) {
522 r = dev->vhost_ops->vhost_set_mem_table(dev, dev->mem);
524 VHOST_OPS_DEBUG("vhost_set_mem_table failed");
528 log_size = vhost_get_log_size(dev);
529 /* We allocate an extra 4K bytes to log,
530 * to reduce the * number of reallocations. */
531 #define VHOST_LOG_BUFFER (0x1000 / sizeof *dev->log)
532 /* To log more, must increase log size before table update. */
533 if (dev->log_size < log_size) {
534 vhost_dev_log_resize(dev, log_size + VHOST_LOG_BUFFER);
536 r = dev->vhost_ops->vhost_set_mem_table(dev, dev->mem);
538 VHOST_OPS_DEBUG("vhost_set_mem_table failed");
540 /* To log less, can only decrease log size after table update. */
541 if (dev->log_size > log_size + VHOST_LOG_BUFFER) {
542 vhost_dev_log_resize(dev, log_size);
546 /* Deref the old list of sections, this must happen _after_ the
547 * vhost_set_mem_table to ensure the client isn't still using the
548 * section we're about to unref.
550 while (n_old_sections--) {
551 memory_region_unref(old_sections[n_old_sections].mr);
553 g_free(old_sections);
557 /* Adds the section data to the tmp_section structure.
558 * It relies on the listener calling us in memory address order
559 * and for each region (via the _add and _nop methods) to
562 static void vhost_region_add_section(struct vhost_dev *dev,
563 MemoryRegionSection *section)
565 bool need_add = true;
566 uint64_t mrs_size = int128_get64(section->size);
567 uint64_t mrs_gpa = section->offset_within_address_space;
568 uintptr_t mrs_host = (uintptr_t)memory_region_get_ram_ptr(section->mr) +
569 section->offset_within_region;
570 RAMBlock *mrs_rb = section->mr->ram_block;
572 trace_vhost_region_add_section(section->mr->name, mrs_gpa, mrs_size,
575 if (dev->vhost_ops->backend_type == VHOST_BACKEND_TYPE_USER) {
576 /* Round the section to it's page size */
577 /* First align the start down to a page boundary */
578 size_t mrs_page = qemu_ram_pagesize(mrs_rb);
579 uint64_t alignage = mrs_host & (mrs_page - 1);
581 mrs_host -= alignage;
582 mrs_size += alignage;
585 /* Now align the size up to a page boundary */
586 alignage = mrs_size & (mrs_page - 1);
588 mrs_size += mrs_page - alignage;
590 trace_vhost_region_add_section_aligned(section->mr->name, mrs_gpa,
594 if (dev->n_tmp_sections) {
595 /* Since we already have at least one section, lets see if
596 * this extends it; since we're scanning in order, we only
597 * have to look at the last one, and the FlatView that calls
598 * us shouldn't have overlaps.
600 MemoryRegionSection *prev_sec = dev->tmp_sections +
601 (dev->n_tmp_sections - 1);
602 uint64_t prev_gpa_start = prev_sec->offset_within_address_space;
603 uint64_t prev_size = int128_get64(prev_sec->size);
604 uint64_t prev_gpa_end = range_get_last(prev_gpa_start, prev_size);
605 uint64_t prev_host_start =
606 (uintptr_t)memory_region_get_ram_ptr(prev_sec->mr) +
607 prev_sec->offset_within_region;
608 uint64_t prev_host_end = range_get_last(prev_host_start, prev_size);
610 if (mrs_gpa <= (prev_gpa_end + 1)) {
611 /* OK, looks like overlapping/intersecting - it's possible that
612 * the rounding to page sizes has made them overlap, but they should
613 * match up in the same RAMBlock if they do.
615 if (mrs_gpa < prev_gpa_start) {
616 error_report("%s:Section '%s' rounded to %"PRIx64
617 " prior to previous '%s' %"PRIx64,
618 __func__, section->mr->name, mrs_gpa,
619 prev_sec->mr->name, prev_gpa_start);
620 /* A way to cleanly fail here would be better */
623 /* Offset from the start of the previous GPA to this GPA */
624 size_t offset = mrs_gpa - prev_gpa_start;
626 if (prev_host_start + offset == mrs_host &&
627 section->mr == prev_sec->mr &&
628 (!dev->vhost_ops->vhost_backend_can_merge ||
629 dev->vhost_ops->vhost_backend_can_merge(dev,
631 prev_host_start, prev_size))) {
632 uint64_t max_end = MAX(prev_host_end, mrs_host + mrs_size);
634 prev_sec->offset_within_address_space =
635 MIN(prev_gpa_start, mrs_gpa);
636 prev_sec->offset_within_region =
637 MIN(prev_host_start, mrs_host) -
638 (uintptr_t)memory_region_get_ram_ptr(prev_sec->mr);
639 prev_sec->size = int128_make64(max_end - MIN(prev_host_start,
641 trace_vhost_region_add_section_merge(section->mr->name,
642 int128_get64(prev_sec->size),
643 prev_sec->offset_within_address_space,
644 prev_sec->offset_within_region);
646 /* adjoining regions are fine, but overlapping ones with
647 * different blocks/offsets shouldn't happen
649 if (mrs_gpa != prev_gpa_end + 1) {
650 error_report("%s: Overlapping but not coherent sections "
660 ++dev->n_tmp_sections;
661 dev->tmp_sections = g_renew(MemoryRegionSection, dev->tmp_sections,
662 dev->n_tmp_sections);
663 dev->tmp_sections[dev->n_tmp_sections - 1] = *section;
664 /* The flatview isn't stable and we don't use it, making it NULL
665 * means we can memcmp the list.
667 dev->tmp_sections[dev->n_tmp_sections - 1].fv = NULL;
668 memory_region_ref(section->mr);
672 /* Used for both add and nop callbacks */
673 static void vhost_region_addnop(MemoryListener *listener,
674 MemoryRegionSection *section)
676 struct vhost_dev *dev = container_of(listener, struct vhost_dev,
679 if (!vhost_section(dev, section)) {
682 vhost_region_add_section(dev, section);
685 static void vhost_iommu_unmap_notify(IOMMUNotifier *n, IOMMUTLBEntry *iotlb)
687 struct vhost_iommu *iommu = container_of(n, struct vhost_iommu, n);
688 struct vhost_dev *hdev = iommu->hdev;
689 hwaddr iova = iotlb->iova + iommu->iommu_offset;
691 if (vhost_backend_invalidate_device_iotlb(hdev, iova,
692 iotlb->addr_mask + 1)) {
693 error_report("Fail to invalidate device iotlb");
697 static void vhost_iommu_region_add(MemoryListener *listener,
698 MemoryRegionSection *section)
700 struct vhost_dev *dev = container_of(listener, struct vhost_dev,
702 struct vhost_iommu *iommu;
705 IOMMUMemoryRegion *iommu_mr;
708 if (!memory_region_is_iommu(section->mr)) {
712 iommu_mr = IOMMU_MEMORY_REGION(section->mr);
714 iommu = g_malloc0(sizeof(*iommu));
715 end = int128_add(int128_make64(section->offset_within_region),
717 end = int128_sub(end, int128_one());
718 iommu_idx = memory_region_iommu_attrs_to_index(iommu_mr,
719 MEMTXATTRS_UNSPECIFIED);
720 iommu_notifier_init(&iommu->n, vhost_iommu_unmap_notify,
721 IOMMU_NOTIFIER_DEVIOTLB_UNMAP,
722 section->offset_within_region,
725 iommu->mr = section->mr;
726 iommu->iommu_offset = section->offset_within_address_space -
727 section->offset_within_region;
729 ret = memory_region_register_iommu_notifier(section->mr, &iommu->n, NULL);
732 * Some vIOMMUs do not support dev-iotlb yet. If so, try to use the
733 * UNMAP legacy message
735 iommu->n.notifier_flags = IOMMU_NOTIFIER_UNMAP;
736 memory_region_register_iommu_notifier(section->mr, &iommu->n,
739 QLIST_INSERT_HEAD(&dev->iommu_list, iommu, iommu_next);
740 /* TODO: can replay help performance here? */
743 static void vhost_iommu_region_del(MemoryListener *listener,
744 MemoryRegionSection *section)
746 struct vhost_dev *dev = container_of(listener, struct vhost_dev,
748 struct vhost_iommu *iommu;
750 if (!memory_region_is_iommu(section->mr)) {
754 QLIST_FOREACH(iommu, &dev->iommu_list, iommu_next) {
755 if (iommu->mr == section->mr &&
756 iommu->n.start == section->offset_within_region) {
757 memory_region_unregister_iommu_notifier(iommu->mr,
759 QLIST_REMOVE(iommu, iommu_next);
766 static int vhost_virtqueue_set_addr(struct vhost_dev *dev,
767 struct vhost_virtqueue *vq,
768 unsigned idx, bool enable_log)
770 struct vhost_vring_addr addr;
772 memset(&addr, 0, sizeof(struct vhost_vring_addr));
774 if (dev->vhost_ops->vhost_vq_get_addr) {
775 r = dev->vhost_ops->vhost_vq_get_addr(dev, &addr, vq);
777 VHOST_OPS_DEBUG("vhost_vq_get_addr failed");
781 addr.desc_user_addr = (uint64_t)(unsigned long)vq->desc;
782 addr.avail_user_addr = (uint64_t)(unsigned long)vq->avail;
783 addr.used_user_addr = (uint64_t)(unsigned long)vq->used;
786 addr.log_guest_addr = vq->used_phys;
787 addr.flags = enable_log ? (1 << VHOST_VRING_F_LOG) : 0;
788 r = dev->vhost_ops->vhost_set_vring_addr(dev, &addr);
790 VHOST_OPS_DEBUG("vhost_set_vring_addr failed");
796 static int vhost_dev_set_features(struct vhost_dev *dev,
799 uint64_t features = dev->acked_features;
802 features |= 0x1ULL << VHOST_F_LOG_ALL;
804 if (!vhost_dev_has_iommu(dev)) {
805 features &= ~(0x1ULL << VIRTIO_F_IOMMU_PLATFORM);
807 if (dev->vhost_ops->vhost_force_iommu) {
808 if (dev->vhost_ops->vhost_force_iommu(dev) == true) {
809 features |= 0x1ULL << VIRTIO_F_IOMMU_PLATFORM;
812 r = dev->vhost_ops->vhost_set_features(dev, features);
814 VHOST_OPS_DEBUG("vhost_set_features failed");
817 if (dev->vhost_ops->vhost_set_backend_cap) {
818 r = dev->vhost_ops->vhost_set_backend_cap(dev);
820 VHOST_OPS_DEBUG("vhost_set_backend_cap failed");
826 return r < 0 ? -errno : 0;
829 static int vhost_dev_set_log(struct vhost_dev *dev, bool enable_log)
834 r = vhost_dev_set_features(dev, enable_log);
838 for (i = 0; i < dev->nvqs; ++i) {
839 idx = dev->vhost_ops->vhost_get_vq_index(dev, dev->vq_index + i);
840 addr = virtio_queue_get_desc_addr(dev->vdev, idx);
843 * The queue might not be ready for start. If this
844 * is the case there is no reason to continue the process.
845 * The similar logic is used by the vhost_virtqueue_start()
850 r = vhost_virtqueue_set_addr(dev, dev->vqs + i, idx,
858 for (; i >= 0; --i) {
859 idx = dev->vhost_ops->vhost_get_vq_index(dev, dev->vq_index + i);
860 vhost_virtqueue_set_addr(dev, dev->vqs + i, idx,
863 vhost_dev_set_features(dev, dev->log_enabled);
868 static int vhost_migration_log(MemoryListener *listener, bool enable)
870 struct vhost_dev *dev = container_of(listener, struct vhost_dev,
873 if (enable == dev->log_enabled) {
877 dev->log_enabled = enable;
883 r = vhost_dev_set_log(dev, false);
885 goto check_dev_state;
887 vhost_log_put(dev, false);
889 vhost_dev_log_resize(dev, vhost_get_log_size(dev));
890 r = vhost_dev_set_log(dev, true);
892 goto check_dev_state;
897 dev->log_enabled = enable;
899 * vhost-user-* devices could change their state during log
900 * initialization due to disconnect. So check dev state after
901 * vhost communication.
905 * Since device is in the stopped state, it is okay for
906 * migration. Return success.
911 /* An error occurred. */
912 dev->log_enabled = false;
918 static void vhost_log_global_start(MemoryListener *listener)
922 r = vhost_migration_log(listener, true);
928 static void vhost_log_global_stop(MemoryListener *listener)
932 r = vhost_migration_log(listener, false);
938 static void vhost_log_start(MemoryListener *listener,
939 MemoryRegionSection *section,
942 /* FIXME: implement */
945 static void vhost_log_stop(MemoryListener *listener,
946 MemoryRegionSection *section,
949 /* FIXME: implement */
952 /* The vhost driver natively knows how to handle the vrings of non
953 * cross-endian legacy devices and modern devices. Only legacy devices
954 * exposed to a bi-endian guest may require the vhost driver to use a
955 * specific endianness.
957 static inline bool vhost_needs_vring_endian(VirtIODevice *vdev)
959 if (virtio_vdev_has_feature(vdev, VIRTIO_F_VERSION_1)) {
962 #ifdef HOST_WORDS_BIGENDIAN
963 return vdev->device_endian == VIRTIO_DEVICE_ENDIAN_LITTLE;
965 return vdev->device_endian == VIRTIO_DEVICE_ENDIAN_BIG;
969 static int vhost_virtqueue_set_vring_endian_legacy(struct vhost_dev *dev,
973 struct vhost_vring_state s = {
974 .index = vhost_vq_index,
978 if (!dev->vhost_ops->vhost_set_vring_endian(dev, &s)) {
982 VHOST_OPS_DEBUG("vhost_set_vring_endian failed");
983 if (errno == ENOTTY) {
984 error_report("vhost does not support cross-endian");
991 static int vhost_memory_region_lookup(struct vhost_dev *hdev,
992 uint64_t gpa, uint64_t *uaddr,
997 for (i = 0; i < hdev->mem->nregions; i++) {
998 struct vhost_memory_region *reg = hdev->mem->regions + i;
1000 if (gpa >= reg->guest_phys_addr &&
1001 reg->guest_phys_addr + reg->memory_size > gpa) {
1002 *uaddr = reg->userspace_addr + gpa - reg->guest_phys_addr;
1003 *len = reg->guest_phys_addr + reg->memory_size - gpa;
1011 int vhost_device_iotlb_miss(struct vhost_dev *dev, uint64_t iova, int write)
1013 IOMMUTLBEntry iotlb;
1014 uint64_t uaddr, len;
1017 RCU_READ_LOCK_GUARD();
1019 trace_vhost_iotlb_miss(dev, 1);
1021 iotlb = address_space_get_iotlb_entry(dev->vdev->dma_as,
1023 MEMTXATTRS_UNSPECIFIED);
1024 if (iotlb.target_as != NULL) {
1025 ret = vhost_memory_region_lookup(dev, iotlb.translated_addr,
1028 trace_vhost_iotlb_miss(dev, 3);
1029 error_report("Fail to lookup the translated address "
1030 "%"PRIx64, iotlb.translated_addr);
1034 len = MIN(iotlb.addr_mask + 1, len);
1035 iova = iova & ~iotlb.addr_mask;
1037 ret = vhost_backend_update_device_iotlb(dev, iova, uaddr,
1040 trace_vhost_iotlb_miss(dev, 4);
1041 error_report("Fail to update device iotlb");
1046 trace_vhost_iotlb_miss(dev, 2);
1052 static int vhost_virtqueue_start(struct vhost_dev *dev,
1053 struct VirtIODevice *vdev,
1054 struct vhost_virtqueue *vq,
1057 BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(vdev)));
1058 VirtioBusState *vbus = VIRTIO_BUS(qbus);
1059 VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(vbus);
1062 int vhost_vq_index = dev->vhost_ops->vhost_get_vq_index(dev, idx);
1063 struct vhost_vring_file file = {
1064 .index = vhost_vq_index
1066 struct vhost_vring_state state = {
1067 .index = vhost_vq_index
1069 struct VirtQueue *vvq = virtio_get_queue(vdev, idx);
1071 a = virtio_queue_get_desc_addr(vdev, idx);
1073 /* Queue might not be ready for start */
1077 vq->num = state.num = virtio_queue_get_num(vdev, idx);
1078 r = dev->vhost_ops->vhost_set_vring_num(dev, &state);
1080 VHOST_OPS_DEBUG("vhost_set_vring_num failed");
1084 state.num = virtio_queue_get_last_avail_idx(vdev, idx);
1085 r = dev->vhost_ops->vhost_set_vring_base(dev, &state);
1087 VHOST_OPS_DEBUG("vhost_set_vring_base failed");
1091 if (vhost_needs_vring_endian(vdev)) {
1092 r = vhost_virtqueue_set_vring_endian_legacy(dev,
1093 virtio_is_big_endian(vdev),
1100 vq->desc_size = s = l = virtio_queue_get_desc_size(vdev, idx);
1102 vq->desc = vhost_memory_map(dev, a, &l, false);
1103 if (!vq->desc || l != s) {
1105 goto fail_alloc_desc;
1107 vq->avail_size = s = l = virtio_queue_get_avail_size(vdev, idx);
1108 vq->avail_phys = a = virtio_queue_get_avail_addr(vdev, idx);
1109 vq->avail = vhost_memory_map(dev, a, &l, false);
1110 if (!vq->avail || l != s) {
1112 goto fail_alloc_avail;
1114 vq->used_size = s = l = virtio_queue_get_used_size(vdev, idx);
1115 vq->used_phys = a = virtio_queue_get_used_addr(vdev, idx);
1116 vq->used = vhost_memory_map(dev, a, &l, true);
1117 if (!vq->used || l != s) {
1119 goto fail_alloc_used;
1122 r = vhost_virtqueue_set_addr(dev, vq, vhost_vq_index, dev->log_enabled);
1128 file.fd = event_notifier_get_fd(virtio_queue_get_host_notifier(vvq));
1129 r = dev->vhost_ops->vhost_set_vring_kick(dev, &file);
1131 VHOST_OPS_DEBUG("vhost_set_vring_kick failed");
1136 /* Clear and discard previous events if any. */
1137 event_notifier_test_and_clear(&vq->masked_notifier);
1139 /* Init vring in unmasked state, unless guest_notifier_mask
1142 if (!vdev->use_guest_notifier_mask) {
1143 /* TODO: check and handle errors. */
1144 vhost_virtqueue_mask(dev, vdev, idx, false);
1147 if (k->query_guest_notifiers &&
1148 k->query_guest_notifiers(qbus->parent) &&
1149 virtio_queue_vector(vdev, idx) == VIRTIO_NO_VECTOR) {
1151 r = dev->vhost_ops->vhost_set_vring_call(dev, &file);
1162 vhost_memory_unmap(dev, vq->used, virtio_queue_get_used_size(vdev, idx),
1165 vhost_memory_unmap(dev, vq->avail, virtio_queue_get_avail_size(vdev, idx),
1168 vhost_memory_unmap(dev, vq->desc, virtio_queue_get_desc_size(vdev, idx),
1174 static void vhost_virtqueue_stop(struct vhost_dev *dev,
1175 struct VirtIODevice *vdev,
1176 struct vhost_virtqueue *vq,
1179 int vhost_vq_index = dev->vhost_ops->vhost_get_vq_index(dev, idx);
1180 struct vhost_vring_state state = {
1181 .index = vhost_vq_index,
1185 if (virtio_queue_get_desc_addr(vdev, idx) == 0) {
1186 /* Don't stop the virtqueue which might have not been started */
1190 r = dev->vhost_ops->vhost_get_vring_base(dev, &state);
1192 VHOST_OPS_DEBUG("vhost VQ %u ring restore failed: %d", idx, r);
1193 /* Connection to the backend is broken, so let's sync internal
1194 * last avail idx to the device used idx.
1196 virtio_queue_restore_last_avail_idx(vdev, idx);
1198 virtio_queue_set_last_avail_idx(vdev, idx, state.num);
1200 virtio_queue_invalidate_signalled_used(vdev, idx);
1201 virtio_queue_update_used_idx(vdev, idx);
1203 /* In the cross-endian case, we need to reset the vring endianness to
1204 * native as legacy devices expect so by default.
1206 if (vhost_needs_vring_endian(vdev)) {
1207 vhost_virtqueue_set_vring_endian_legacy(dev,
1208 !virtio_is_big_endian(vdev),
1212 vhost_memory_unmap(dev, vq->used, virtio_queue_get_used_size(vdev, idx),
1213 1, virtio_queue_get_used_size(vdev, idx));
1214 vhost_memory_unmap(dev, vq->avail, virtio_queue_get_avail_size(vdev, idx),
1215 0, virtio_queue_get_avail_size(vdev, idx));
1216 vhost_memory_unmap(dev, vq->desc, virtio_queue_get_desc_size(vdev, idx),
1217 0, virtio_queue_get_desc_size(vdev, idx));
1220 static void vhost_eventfd_add(MemoryListener *listener,
1221 MemoryRegionSection *section,
1222 bool match_data, uint64_t data, EventNotifier *e)
1226 static void vhost_eventfd_del(MemoryListener *listener,
1227 MemoryRegionSection *section,
1228 bool match_data, uint64_t data, EventNotifier *e)
1232 static int vhost_virtqueue_set_busyloop_timeout(struct vhost_dev *dev,
1233 int n, uint32_t timeout)
1235 int vhost_vq_index = dev->vhost_ops->vhost_get_vq_index(dev, n);
1236 struct vhost_vring_state state = {
1237 .index = vhost_vq_index,
1242 if (!dev->vhost_ops->vhost_set_vring_busyloop_timeout) {
1246 r = dev->vhost_ops->vhost_set_vring_busyloop_timeout(dev, &state);
1248 VHOST_OPS_DEBUG("vhost_set_vring_busyloop_timeout failed");
1255 static int vhost_virtqueue_init(struct vhost_dev *dev,
1256 struct vhost_virtqueue *vq, int n)
1258 int vhost_vq_index = dev->vhost_ops->vhost_get_vq_index(dev, n);
1259 struct vhost_vring_file file = {
1260 .index = vhost_vq_index,
1262 int r = event_notifier_init(&vq->masked_notifier, 0);
1267 file.fd = event_notifier_get_fd(&vq->masked_notifier);
1268 r = dev->vhost_ops->vhost_set_vring_call(dev, &file);
1270 VHOST_OPS_DEBUG("vhost_set_vring_call failed");
1279 event_notifier_cleanup(&vq->masked_notifier);
1283 static void vhost_virtqueue_cleanup(struct vhost_virtqueue *vq)
1285 event_notifier_cleanup(&vq->masked_notifier);
1288 int vhost_dev_init(struct vhost_dev *hdev, void *opaque,
1289 VhostBackendType backend_type, uint32_t busyloop_timeout,
1294 int i, r, n_initialized_vqs = 0;
1297 hdev->migration_blocker = NULL;
1299 r = vhost_set_backend_type(hdev, backend_type);
1302 r = hdev->vhost_ops->vhost_backend_init(hdev, opaque, errp);
1305 error_setg_errno(errp, -r, "vhost_backend_init failed");
1310 r = hdev->vhost_ops->vhost_set_owner(hdev);
1312 error_setg_errno(errp, -r, "vhost_set_owner failed");
1316 r = hdev->vhost_ops->vhost_get_features(hdev, &features);
1318 error_setg_errno(errp, -r, "vhost_get_features failed");
1322 for (i = 0; i < hdev->nvqs; ++i, ++n_initialized_vqs) {
1323 r = vhost_virtqueue_init(hdev, hdev->vqs + i, hdev->vq_index + i);
1325 error_setg_errno(errp, -r, "Failed to initialize virtqueue %d", i);
1330 if (busyloop_timeout) {
1331 for (i = 0; i < hdev->nvqs; ++i) {
1332 r = vhost_virtqueue_set_busyloop_timeout(hdev, hdev->vq_index + i,
1335 error_setg_errno(errp, -r, "Failed to set busyloop timeout");
1341 hdev->features = features;
1343 hdev->memory_listener = (MemoryListener) {
1344 .begin = vhost_begin,
1345 .commit = vhost_commit,
1346 .region_add = vhost_region_addnop,
1347 .region_nop = vhost_region_addnop,
1348 .log_start = vhost_log_start,
1349 .log_stop = vhost_log_stop,
1350 .log_sync = vhost_log_sync,
1351 .log_global_start = vhost_log_global_start,
1352 .log_global_stop = vhost_log_global_stop,
1353 .eventfd_add = vhost_eventfd_add,
1354 .eventfd_del = vhost_eventfd_del,
1358 hdev->iommu_listener = (MemoryListener) {
1359 .region_add = vhost_iommu_region_add,
1360 .region_del = vhost_iommu_region_del,
1363 if (hdev->migration_blocker == NULL) {
1364 if (!(hdev->features & (0x1ULL << VHOST_F_LOG_ALL))) {
1365 error_setg(&hdev->migration_blocker,
1366 "Migration disabled: vhost lacks VHOST_F_LOG_ALL feature.");
1367 } else if (vhost_dev_log_is_shared(hdev) && !qemu_memfd_alloc_check()) {
1368 error_setg(&hdev->migration_blocker,
1369 "Migration disabled: failed to allocate shared memory");
1373 if (hdev->migration_blocker != NULL) {
1374 r = migrate_add_blocker(hdev->migration_blocker, errp);
1376 error_free(hdev->migration_blocker);
1381 hdev->mem = g_malloc0(offsetof(struct vhost_memory, regions));
1382 hdev->n_mem_sections = 0;
1383 hdev->mem_sections = NULL;
1386 hdev->log_enabled = false;
1387 hdev->started = false;
1388 memory_listener_register(&hdev->memory_listener, &address_space_memory);
1389 QLIST_INSERT_HEAD(&vhost_devices, hdev, entry);
1391 if (used_memslots > hdev->vhost_ops->vhost_backend_memslots_limit(hdev)) {
1392 error_setg(errp, "vhost backend memory slots limit is less"
1393 " than current number of present memory slots");
1401 if (busyloop_timeout) {
1403 vhost_virtqueue_set_busyloop_timeout(hdev, hdev->vq_index + i, 0);
1407 hdev->nvqs = n_initialized_vqs;
1408 vhost_dev_cleanup(hdev);
1412 void vhost_dev_cleanup(struct vhost_dev *hdev)
1416 for (i = 0; i < hdev->nvqs; ++i) {
1417 vhost_virtqueue_cleanup(hdev->vqs + i);
1420 /* those are only safe after successful init */
1421 memory_listener_unregister(&hdev->memory_listener);
1422 QLIST_REMOVE(hdev, entry);
1424 if (hdev->migration_blocker) {
1425 migrate_del_blocker(hdev->migration_blocker);
1426 error_free(hdev->migration_blocker);
1429 g_free(hdev->mem_sections);
1430 if (hdev->vhost_ops) {
1431 hdev->vhost_ops->vhost_backend_cleanup(hdev);
1435 memset(hdev, 0, sizeof(struct vhost_dev));
1438 /* Stop processing guest IO notifications in qemu.
1439 * Start processing them in vhost in kernel.
1441 int vhost_dev_enable_notifiers(struct vhost_dev *hdev, VirtIODevice *vdev)
1443 BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(vdev)));
1446 /* We will pass the notifiers to the kernel, make sure that QEMU
1447 * doesn't interfere.
1449 r = virtio_device_grab_ioeventfd(vdev);
1451 error_report("binding does not support host notifiers");
1455 for (i = 0; i < hdev->nvqs; ++i) {
1456 r = virtio_bus_set_host_notifier(VIRTIO_BUS(qbus), hdev->vq_index + i,
1459 error_report("vhost VQ %d notifier binding failed: %d", i, -r);
1467 e = virtio_bus_set_host_notifier(VIRTIO_BUS(qbus), hdev->vq_index + i,
1470 error_report("vhost VQ %d notifier cleanup error: %d", i, -r);
1473 virtio_bus_cleanup_host_notifier(VIRTIO_BUS(qbus), hdev->vq_index + i);
1475 virtio_device_release_ioeventfd(vdev);
1480 /* Stop processing guest IO notifications in vhost.
1481 * Start processing them in qemu.
1482 * This might actually run the qemu handlers right away,
1483 * so virtio in qemu must be completely setup when this is called.
1485 void vhost_dev_disable_notifiers(struct vhost_dev *hdev, VirtIODevice *vdev)
1487 BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(vdev)));
1490 for (i = 0; i < hdev->nvqs; ++i) {
1491 r = virtio_bus_set_host_notifier(VIRTIO_BUS(qbus), hdev->vq_index + i,
1494 error_report("vhost VQ %d notifier cleanup failed: %d", i, -r);
1497 virtio_bus_cleanup_host_notifier(VIRTIO_BUS(qbus), hdev->vq_index + i);
1499 virtio_device_release_ioeventfd(vdev);
1502 /* Test and clear event pending status.
1503 * Should be called after unmask to avoid losing events.
1505 bool vhost_virtqueue_pending(struct vhost_dev *hdev, int n)
1507 struct vhost_virtqueue *vq = hdev->vqs + n - hdev->vq_index;
1508 assert(n >= hdev->vq_index && n < hdev->vq_index + hdev->nvqs);
1509 return event_notifier_test_and_clear(&vq->masked_notifier);
1512 /* Mask/unmask events from this vq. */
1513 void vhost_virtqueue_mask(struct vhost_dev *hdev, VirtIODevice *vdev, int n,
1516 struct VirtQueue *vvq = virtio_get_queue(vdev, n);
1517 int r, index = n - hdev->vq_index;
1518 struct vhost_vring_file file;
1520 /* should only be called after backend is connected */
1521 assert(hdev->vhost_ops);
1524 assert(vdev->use_guest_notifier_mask);
1525 file.fd = event_notifier_get_fd(&hdev->vqs[index].masked_notifier);
1527 file.fd = event_notifier_get_fd(virtio_queue_get_guest_notifier(vvq));
1530 file.index = hdev->vhost_ops->vhost_get_vq_index(hdev, n);
1531 r = hdev->vhost_ops->vhost_set_vring_call(hdev, &file);
1533 VHOST_OPS_DEBUG("vhost_set_vring_call failed");
1537 uint64_t vhost_get_features(struct vhost_dev *hdev, const int *feature_bits,
1540 const int *bit = feature_bits;
1541 while (*bit != VHOST_INVALID_FEATURE_BIT) {
1542 uint64_t bit_mask = (1ULL << *bit);
1543 if (!(hdev->features & bit_mask)) {
1544 features &= ~bit_mask;
1551 void vhost_ack_features(struct vhost_dev *hdev, const int *feature_bits,
1554 const int *bit = feature_bits;
1555 while (*bit != VHOST_INVALID_FEATURE_BIT) {
1556 uint64_t bit_mask = (1ULL << *bit);
1557 if (features & bit_mask) {
1558 hdev->acked_features |= bit_mask;
1564 int vhost_dev_get_config(struct vhost_dev *hdev, uint8_t *config,
1565 uint32_t config_len, Error **errp)
1570 assert(hdev->vhost_ops);
1572 if (hdev->vhost_ops->vhost_get_config) {
1573 ret = hdev->vhost_ops->vhost_get_config(hdev, config, config_len, errp);
1574 if (ret < 0 && !*errp) {
1575 error_setg_errno(errp, -ret, "vhost_get_config failed");
1580 error_setg(errp, "vhost_get_config not implemented");
1584 int vhost_dev_set_config(struct vhost_dev *hdev, const uint8_t *data,
1585 uint32_t offset, uint32_t size, uint32_t flags)
1587 assert(hdev->vhost_ops);
1589 if (hdev->vhost_ops->vhost_set_config) {
1590 return hdev->vhost_ops->vhost_set_config(hdev, data, offset,
1597 void vhost_dev_set_config_notifier(struct vhost_dev *hdev,
1598 const VhostDevConfigOps *ops)
1600 hdev->config_ops = ops;
1603 void vhost_dev_free_inflight(struct vhost_inflight *inflight)
1605 if (inflight && inflight->addr) {
1606 qemu_memfd_free(inflight->addr, inflight->size, inflight->fd);
1607 inflight->addr = NULL;
1612 static int vhost_dev_resize_inflight(struct vhost_inflight *inflight,
1617 void *addr = qemu_memfd_alloc("vhost-inflight", new_size,
1618 F_SEAL_GROW | F_SEAL_SHRINK | F_SEAL_SEAL,
1622 error_report_err(err);
1626 vhost_dev_free_inflight(inflight);
1627 inflight->offset = 0;
1628 inflight->addr = addr;
1630 inflight->size = new_size;
1635 void vhost_dev_save_inflight(struct vhost_inflight *inflight, QEMUFile *f)
1637 if (inflight->addr) {
1638 qemu_put_be64(f, inflight->size);
1639 qemu_put_be16(f, inflight->queue_size);
1640 qemu_put_buffer(f, inflight->addr, inflight->size);
1642 qemu_put_be64(f, 0);
1646 int vhost_dev_load_inflight(struct vhost_inflight *inflight, QEMUFile *f)
1650 size = qemu_get_be64(f);
1655 if (inflight->size != size) {
1656 if (vhost_dev_resize_inflight(inflight, size)) {
1660 inflight->queue_size = qemu_get_be16(f);
1662 qemu_get_buffer(f, inflight->addr, size);
1667 int vhost_dev_prepare_inflight(struct vhost_dev *hdev, VirtIODevice *vdev)
1671 if (hdev->vhost_ops->vhost_get_inflight_fd == NULL ||
1672 hdev->vhost_ops->vhost_set_inflight_fd == NULL) {
1678 r = vhost_dev_set_features(hdev, hdev->log_enabled);
1680 VHOST_OPS_DEBUG("vhost_dev_prepare_inflight failed");
1687 int vhost_dev_set_inflight(struct vhost_dev *dev,
1688 struct vhost_inflight *inflight)
1692 if (dev->vhost_ops->vhost_set_inflight_fd && inflight->addr) {
1693 r = dev->vhost_ops->vhost_set_inflight_fd(dev, inflight);
1695 VHOST_OPS_DEBUG("vhost_set_inflight_fd failed");
1703 int vhost_dev_get_inflight(struct vhost_dev *dev, uint16_t queue_size,
1704 struct vhost_inflight *inflight)
1708 if (dev->vhost_ops->vhost_get_inflight_fd) {
1709 r = dev->vhost_ops->vhost_get_inflight_fd(dev, queue_size, inflight);
1711 VHOST_OPS_DEBUG("vhost_get_inflight_fd failed");
1719 /* Host notifiers must be enabled at this point. */
1720 int vhost_dev_start(struct vhost_dev *hdev, VirtIODevice *vdev)
1724 /* should only be called after backend is connected */
1725 assert(hdev->vhost_ops);
1727 hdev->started = true;
1730 r = vhost_dev_set_features(hdev, hdev->log_enabled);
1735 if (vhost_dev_has_iommu(hdev)) {
1736 memory_listener_register(&hdev->iommu_listener, vdev->dma_as);
1739 r = hdev->vhost_ops->vhost_set_mem_table(hdev, hdev->mem);
1741 VHOST_OPS_DEBUG("vhost_set_mem_table failed");
1745 for (i = 0; i < hdev->nvqs; ++i) {
1746 r = vhost_virtqueue_start(hdev,
1749 hdev->vq_index + i);
1755 if (hdev->log_enabled) {
1758 hdev->log_size = vhost_get_log_size(hdev);
1759 hdev->log = vhost_log_get(hdev->log_size,
1760 vhost_dev_log_is_shared(hdev));
1761 log_base = (uintptr_t)hdev->log->log;
1762 r = hdev->vhost_ops->vhost_set_log_base(hdev,
1763 hdev->log_size ? log_base : 0,
1766 VHOST_OPS_DEBUG("vhost_set_log_base failed");
1771 if (hdev->vhost_ops->vhost_dev_start) {
1772 r = hdev->vhost_ops->vhost_dev_start(hdev, true);
1777 if (vhost_dev_has_iommu(hdev) &&
1778 hdev->vhost_ops->vhost_set_iotlb_callback) {
1779 hdev->vhost_ops->vhost_set_iotlb_callback(hdev, true);
1781 /* Update used ring information for IOTLB to work correctly,
1782 * vhost-kernel code requires for this.*/
1783 for (i = 0; i < hdev->nvqs; ++i) {
1784 struct vhost_virtqueue *vq = hdev->vqs + i;
1785 vhost_device_iotlb_miss(hdev, vq->used_phys, true);
1790 vhost_log_put(hdev, false);
1793 vhost_virtqueue_stop(hdev,
1796 hdev->vq_index + i);
1802 hdev->started = false;
1806 /* Host notifiers must be enabled at this point. */
1807 void vhost_dev_stop(struct vhost_dev *hdev, VirtIODevice *vdev)
1811 /* should only be called after backend is connected */
1812 assert(hdev->vhost_ops);
1814 if (hdev->vhost_ops->vhost_dev_start) {
1815 hdev->vhost_ops->vhost_dev_start(hdev, false);
1817 for (i = 0; i < hdev->nvqs; ++i) {
1818 vhost_virtqueue_stop(hdev,
1821 hdev->vq_index + i);
1824 if (vhost_dev_has_iommu(hdev)) {
1825 if (hdev->vhost_ops->vhost_set_iotlb_callback) {
1826 hdev->vhost_ops->vhost_set_iotlb_callback(hdev, false);
1828 memory_listener_unregister(&hdev->iommu_listener);
1830 vhost_log_put(hdev, true);
1831 hdev->started = false;
1835 int vhost_net_set_backend(struct vhost_dev *hdev,
1836 struct vhost_vring_file *file)
1838 if (hdev->vhost_ops->vhost_net_set_backend) {
1839 return hdev->vhost_ops->vhost_net_set_backend(hdev, file);