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
= atomic_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);
175 for (i
= 0; i
< dev
->nvqs
; ++i
) {
176 struct vhost_virtqueue
*vq
= dev
->vqs
+ i
;
178 if (!vq
->used_phys
&& !vq
->used_size
) {
182 uint64_t last
= vq
->used_phys
+ vq
->used_size
- 1;
183 log_size
= MAX(log_size
, last
/ VHOST_LOG_CHUNK
+ 1);
188 static struct vhost_log
*vhost_log_alloc(uint64_t size
, bool share
)
191 struct vhost_log
*log
;
192 uint64_t logsize
= size
* sizeof(*(log
->log
));
195 log
= g_new0(struct vhost_log
, 1);
197 log
->log
= qemu_memfd_alloc("vhost-log", logsize
,
198 F_SEAL_GROW
| F_SEAL_SHRINK
| F_SEAL_SEAL
,
201 error_report_err(err
);
205 memset(log
->log
, 0, logsize
);
207 log
->log
= g_malloc0(logsize
);
217 static struct vhost_log
*vhost_log_get(uint64_t size
, bool share
)
219 struct vhost_log
*log
= share
? vhost_log_shm
: vhost_log
;
221 if (!log
|| log
->size
!= size
) {
222 log
= vhost_log_alloc(size
, share
);
235 static void vhost_log_put(struct vhost_dev
*dev
, bool sync
)
237 struct vhost_log
*log
= dev
->log
;
244 if (log
->refcnt
== 0) {
245 /* Sync only the range covered by the old log */
246 if (dev
->log_size
&& sync
) {
247 vhost_log_sync_range(dev
, 0, dev
->log_size
* VHOST_LOG_CHUNK
- 1);
250 if (vhost_log
== log
) {
253 } else if (vhost_log_shm
== log
) {
254 qemu_memfd_free(log
->log
, log
->size
* sizeof(*(log
->log
)),
256 vhost_log_shm
= NULL
;
266 static bool vhost_dev_log_is_shared(struct vhost_dev
*dev
)
268 return dev
->vhost_ops
->vhost_requires_shm_log
&&
269 dev
->vhost_ops
->vhost_requires_shm_log(dev
);
272 static inline void vhost_dev_log_resize(struct vhost_dev
*dev
, uint64_t size
)
274 struct vhost_log
*log
= vhost_log_get(size
, vhost_dev_log_is_shared(dev
));
275 uint64_t log_base
= (uintptr_t)log
->log
;
278 /* inform backend of log switching, this must be done before
279 releasing the current log, to ensure no logging is lost */
280 r
= dev
->vhost_ops
->vhost_set_log_base(dev
, log_base
, log
);
282 VHOST_OPS_DEBUG("vhost_set_log_base failed");
285 vhost_log_put(dev
, true);
287 dev
->log_size
= size
;
290 static int vhost_dev_has_iommu(struct vhost_dev
*dev
)
292 VirtIODevice
*vdev
= dev
->vdev
;
295 * For vhost, VIRTIO_F_IOMMU_PLATFORM means the backend support
296 * incremental memory mapping API via IOTLB API. For platform that
297 * does not have IOMMU, there's no need to enable this feature
298 * which may cause unnecessary IOTLB miss/update trnasactions.
300 return vdev
->dma_as
!= &address_space_memory
&&
301 virtio_host_has_feature(vdev
, VIRTIO_F_IOMMU_PLATFORM
);
304 static void *vhost_memory_map(struct vhost_dev
*dev
, hwaddr addr
,
305 hwaddr
*plen
, bool is_write
)
307 if (!vhost_dev_has_iommu(dev
)) {
308 return cpu_physical_memory_map(addr
, plen
, is_write
);
310 return (void *)(uintptr_t)addr
;
314 static void vhost_memory_unmap(struct vhost_dev
*dev
, void *buffer
,
315 hwaddr len
, int is_write
,
318 if (!vhost_dev_has_iommu(dev
)) {
319 cpu_physical_memory_unmap(buffer
, len
, is_write
, access_len
);
323 static int vhost_verify_ring_part_mapping(void *ring_hva
,
330 uint64_t hva_ring_offset
;
331 uint64_t ring_last
= range_get_last(ring_gpa
, ring_size
);
332 uint64_t reg_last
= range_get_last(reg_gpa
, reg_size
);
334 if (ring_last
< reg_gpa
|| ring_gpa
> reg_last
) {
337 /* check that whole ring's is mapped */
338 if (ring_last
> reg_last
) {
341 /* check that ring's MemoryRegion wasn't replaced */
342 hva_ring_offset
= ring_gpa
- reg_gpa
;
343 if (ring_hva
!= reg_hva
+ hva_ring_offset
) {
350 static int vhost_verify_ring_mappings(struct vhost_dev
*dev
,
357 const char *part_name
[] = {
363 if (vhost_dev_has_iommu(dev
)) {
367 for (i
= 0; i
< dev
->nvqs
; ++i
) {
368 struct vhost_virtqueue
*vq
= dev
->vqs
+ i
;
370 if (vq
->desc_phys
== 0) {
375 r
= vhost_verify_ring_part_mapping(
376 vq
->desc
, vq
->desc_phys
, vq
->desc_size
,
377 reg_hva
, reg_gpa
, reg_size
);
383 r
= vhost_verify_ring_part_mapping(
384 vq
->avail
, vq
->avail_phys
, vq
->avail_size
,
385 reg_hva
, reg_gpa
, reg_size
);
391 r
= vhost_verify_ring_part_mapping(
392 vq
->used
, vq
->used_phys
, vq
->used_size
,
393 reg_hva
, reg_gpa
, reg_size
);
400 error_report("Unable to map %s for ring %d", part_name
[j
], i
);
401 } else if (r
== -EBUSY
) {
402 error_report("%s relocated for ring %d", part_name
[j
], i
);
408 * vhost_section: identify sections needed for vhost access
410 * We only care about RAM sections here (where virtqueue and guest
411 * internals accessed by virtio might live). If we find one we still
412 * allow the backend to potentially filter it out of our list.
414 static bool vhost_section(struct vhost_dev
*dev
, MemoryRegionSection
*section
)
416 MemoryRegion
*mr
= section
->mr
;
418 if (memory_region_is_ram(mr
) && !memory_region_is_rom(mr
)) {
419 uint8_t dirty_mask
= memory_region_get_dirty_log_mask(mr
);
420 uint8_t handled_dirty
;
423 * Kernel based vhost doesn't handle any block which is doing
424 * dirty-tracking other than migration for which it has
425 * specific logging support. However for TCG the kernel never
426 * gets involved anyway so we can also ignore it's
427 * self-modiying code detection flags. However a vhost-user
428 * client could still confuse a TCG guest if it re-writes
429 * executable memory that has already been translated.
431 handled_dirty
= (1 << DIRTY_MEMORY_MIGRATION
) |
432 (1 << DIRTY_MEMORY_CODE
);
434 if (dirty_mask
& ~handled_dirty
) {
435 trace_vhost_reject_section(mr
->name
, 1);
439 if (dev
->vhost_ops
->vhost_backend_mem_section_filter
&&
440 !dev
->vhost_ops
->vhost_backend_mem_section_filter(dev
, section
)) {
441 trace_vhost_reject_section(mr
->name
, 2);
445 trace_vhost_section(mr
->name
);
448 trace_vhost_reject_section(mr
->name
, 3);
453 static void vhost_begin(MemoryListener
*listener
)
455 struct vhost_dev
*dev
= container_of(listener
, struct vhost_dev
,
457 dev
->tmp_sections
= NULL
;
458 dev
->n_tmp_sections
= 0;
461 static void vhost_commit(MemoryListener
*listener
)
463 struct vhost_dev
*dev
= container_of(listener
, struct vhost_dev
,
465 MemoryRegionSection
*old_sections
;
471 bool changed
= false;
473 /* Note we can be called before the device is started, but then
474 * starting the device calls set_mem_table, so we need to have
475 * built the data structures.
477 old_sections
= dev
->mem_sections
;
478 n_old_sections
= dev
->n_mem_sections
;
479 dev
->mem_sections
= dev
->tmp_sections
;
480 dev
->n_mem_sections
= dev
->n_tmp_sections
;
482 if (dev
->n_mem_sections
!= n_old_sections
) {
485 /* Same size, lets check the contents */
486 for (int i
= 0; i
< n_old_sections
; i
++) {
487 if (!MemoryRegionSection_eq(&old_sections
[i
],
488 &dev
->mem_sections
[i
])) {
495 trace_vhost_commit(dev
->started
, changed
);
500 /* Rebuild the regions list from the new sections list */
501 regions_size
= offsetof(struct vhost_memory
, regions
) +
502 dev
->n_mem_sections
* sizeof dev
->mem
->regions
[0];
503 dev
->mem
= g_realloc(dev
->mem
, regions_size
);
504 dev
->mem
->nregions
= dev
->n_mem_sections
;
505 used_memslots
= dev
->mem
->nregions
;
506 for (i
= 0; i
< dev
->n_mem_sections
; i
++) {
507 struct vhost_memory_region
*cur_vmr
= dev
->mem
->regions
+ i
;
508 struct MemoryRegionSection
*mrs
= dev
->mem_sections
+ i
;
510 cur_vmr
->guest_phys_addr
= mrs
->offset_within_address_space
;
511 cur_vmr
->memory_size
= int128_get64(mrs
->size
);
512 cur_vmr
->userspace_addr
=
513 (uintptr_t)memory_region_get_ram_ptr(mrs
->mr
) +
514 mrs
->offset_within_region
;
515 cur_vmr
->flags_padding
= 0;
522 for (i
= 0; i
< dev
->mem
->nregions
; i
++) {
523 if (vhost_verify_ring_mappings(dev
,
524 (void *)(uintptr_t)dev
->mem
->regions
[i
].userspace_addr
,
525 dev
->mem
->regions
[i
].guest_phys_addr
,
526 dev
->mem
->regions
[i
].memory_size
)) {
527 error_report("Verify ring failure on region %d", i
);
532 if (!dev
->log_enabled
) {
533 r
= dev
->vhost_ops
->vhost_set_mem_table(dev
, dev
->mem
);
535 VHOST_OPS_DEBUG("vhost_set_mem_table failed");
539 log_size
= vhost_get_log_size(dev
);
540 /* We allocate an extra 4K bytes to log,
541 * to reduce the * number of reallocations. */
542 #define VHOST_LOG_BUFFER (0x1000 / sizeof *dev->log)
543 /* To log more, must increase log size before table update. */
544 if (dev
->log_size
< log_size
) {
545 vhost_dev_log_resize(dev
, log_size
+ VHOST_LOG_BUFFER
);
547 r
= dev
->vhost_ops
->vhost_set_mem_table(dev
, dev
->mem
);
549 VHOST_OPS_DEBUG("vhost_set_mem_table failed");
551 /* To log less, can only decrease log size after table update. */
552 if (dev
->log_size
> log_size
+ VHOST_LOG_BUFFER
) {
553 vhost_dev_log_resize(dev
, log_size
);
557 /* Deref the old list of sections, this must happen _after_ the
558 * vhost_set_mem_table to ensure the client isn't still using the
559 * section we're about to unref.
561 while (n_old_sections
--) {
562 memory_region_unref(old_sections
[n_old_sections
].mr
);
564 g_free(old_sections
);
568 /* Adds the section data to the tmp_section structure.
569 * It relies on the listener calling us in memory address order
570 * and for each region (via the _add and _nop methods) to
573 static void vhost_region_add_section(struct vhost_dev
*dev
,
574 MemoryRegionSection
*section
)
576 bool need_add
= true;
577 uint64_t mrs_size
= int128_get64(section
->size
);
578 uint64_t mrs_gpa
= section
->offset_within_address_space
;
579 uintptr_t mrs_host
= (uintptr_t)memory_region_get_ram_ptr(section
->mr
) +
580 section
->offset_within_region
;
581 RAMBlock
*mrs_rb
= section
->mr
->ram_block
;
583 trace_vhost_region_add_section(section
->mr
->name
, mrs_gpa
, mrs_size
,
586 if (dev
->vhost_ops
->backend_type
== VHOST_BACKEND_TYPE_USER
) {
587 /* Round the section to it's page size */
588 /* First align the start down to a page boundary */
589 size_t mrs_page
= qemu_ram_pagesize(mrs_rb
);
590 uint64_t alignage
= mrs_host
& (mrs_page
- 1);
592 mrs_host
-= alignage
;
593 mrs_size
+= alignage
;
596 /* Now align the size up to a page boundary */
597 alignage
= mrs_size
& (mrs_page
- 1);
599 mrs_size
+= mrs_page
- alignage
;
601 trace_vhost_region_add_section_aligned(section
->mr
->name
, mrs_gpa
,
605 if (dev
->n_tmp_sections
) {
606 /* Since we already have at least one section, lets see if
607 * this extends it; since we're scanning in order, we only
608 * have to look at the last one, and the FlatView that calls
609 * us shouldn't have overlaps.
611 MemoryRegionSection
*prev_sec
= dev
->tmp_sections
+
612 (dev
->n_tmp_sections
- 1);
613 uint64_t prev_gpa_start
= prev_sec
->offset_within_address_space
;
614 uint64_t prev_size
= int128_get64(prev_sec
->size
);
615 uint64_t prev_gpa_end
= range_get_last(prev_gpa_start
, prev_size
);
616 uint64_t prev_host_start
=
617 (uintptr_t)memory_region_get_ram_ptr(prev_sec
->mr
) +
618 prev_sec
->offset_within_region
;
619 uint64_t prev_host_end
= range_get_last(prev_host_start
, prev_size
);
621 if (mrs_gpa
<= (prev_gpa_end
+ 1)) {
622 /* OK, looks like overlapping/intersecting - it's possible that
623 * the rounding to page sizes has made them overlap, but they should
624 * match up in the same RAMBlock if they do.
626 if (mrs_gpa
< prev_gpa_start
) {
627 error_report("%s:Section '%s' rounded to %"PRIx64
628 " prior to previous '%s' %"PRIx64
,
629 __func__
, section
->mr
->name
, mrs_gpa
,
630 prev_sec
->mr
->name
, prev_gpa_start
);
631 /* A way to cleanly fail here would be better */
634 /* Offset from the start of the previous GPA to this GPA */
635 size_t offset
= mrs_gpa
- prev_gpa_start
;
637 if (prev_host_start
+ offset
== mrs_host
&&
638 section
->mr
== prev_sec
->mr
&&
639 (!dev
->vhost_ops
->vhost_backend_can_merge
||
640 dev
->vhost_ops
->vhost_backend_can_merge(dev
,
642 prev_host_start
, prev_size
))) {
643 uint64_t max_end
= MAX(prev_host_end
, mrs_host
+ mrs_size
);
645 prev_sec
->offset_within_address_space
=
646 MIN(prev_gpa_start
, mrs_gpa
);
647 prev_sec
->offset_within_region
=
648 MIN(prev_host_start
, mrs_host
) -
649 (uintptr_t)memory_region_get_ram_ptr(prev_sec
->mr
);
650 prev_sec
->size
= int128_make64(max_end
- MIN(prev_host_start
,
652 trace_vhost_region_add_section_merge(section
->mr
->name
,
653 int128_get64(prev_sec
->size
),
654 prev_sec
->offset_within_address_space
,
655 prev_sec
->offset_within_region
);
657 /* adjoining regions are fine, but overlapping ones with
658 * different blocks/offsets shouldn't happen
660 if (mrs_gpa
!= prev_gpa_end
+ 1) {
661 error_report("%s: Overlapping but not coherent sections "
671 ++dev
->n_tmp_sections
;
672 dev
->tmp_sections
= g_renew(MemoryRegionSection
, dev
->tmp_sections
,
673 dev
->n_tmp_sections
);
674 dev
->tmp_sections
[dev
->n_tmp_sections
- 1] = *section
;
675 /* The flatview isn't stable and we don't use it, making it NULL
676 * means we can memcmp the list.
678 dev
->tmp_sections
[dev
->n_tmp_sections
- 1].fv
= NULL
;
679 memory_region_ref(section
->mr
);
683 /* Used for both add and nop callbacks */
684 static void vhost_region_addnop(MemoryListener
*listener
,
685 MemoryRegionSection
*section
)
687 struct vhost_dev
*dev
= container_of(listener
, struct vhost_dev
,
690 if (!vhost_section(dev
, section
)) {
693 vhost_region_add_section(dev
, section
);
696 static void vhost_iommu_unmap_notify(IOMMUNotifier
*n
, IOMMUTLBEntry
*iotlb
)
698 struct vhost_iommu
*iommu
= container_of(n
, struct vhost_iommu
, n
);
699 struct vhost_dev
*hdev
= iommu
->hdev
;
700 hwaddr iova
= iotlb
->iova
+ iommu
->iommu_offset
;
702 if (vhost_backend_invalidate_device_iotlb(hdev
, iova
,
703 iotlb
->addr_mask
+ 1)) {
704 error_report("Fail to invalidate device iotlb");
708 static void vhost_iommu_region_add(MemoryListener
*listener
,
709 MemoryRegionSection
*section
)
711 struct vhost_dev
*dev
= container_of(listener
, struct vhost_dev
,
713 struct vhost_iommu
*iommu
;
716 IOMMUMemoryRegion
*iommu_mr
;
719 if (!memory_region_is_iommu(section
->mr
)) {
723 iommu_mr
= IOMMU_MEMORY_REGION(section
->mr
);
725 iommu
= g_malloc0(sizeof(*iommu
));
726 end
= int128_add(int128_make64(section
->offset_within_region
),
728 end
= int128_sub(end
, int128_one());
729 iommu_idx
= memory_region_iommu_attrs_to_index(iommu_mr
,
730 MEMTXATTRS_UNSPECIFIED
);
731 iommu_notifier_init(&iommu
->n
, vhost_iommu_unmap_notify
,
732 IOMMU_NOTIFIER_UNMAP
,
733 section
->offset_within_region
,
736 iommu
->mr
= section
->mr
;
737 iommu
->iommu_offset
= section
->offset_within_address_space
-
738 section
->offset_within_region
;
740 ret
= memory_region_register_iommu_notifier(section
->mr
, &iommu
->n
, &err
);
742 error_report_err(err
);
745 QLIST_INSERT_HEAD(&dev
->iommu_list
, iommu
, iommu_next
);
746 /* TODO: can replay help performance here? */
749 static void vhost_iommu_region_del(MemoryListener
*listener
,
750 MemoryRegionSection
*section
)
752 struct vhost_dev
*dev
= container_of(listener
, struct vhost_dev
,
754 struct vhost_iommu
*iommu
;
756 if (!memory_region_is_iommu(section
->mr
)) {
760 QLIST_FOREACH(iommu
, &dev
->iommu_list
, iommu_next
) {
761 if (iommu
->mr
== section
->mr
&&
762 iommu
->n
.start
== section
->offset_within_region
) {
763 memory_region_unregister_iommu_notifier(iommu
->mr
,
765 QLIST_REMOVE(iommu
, iommu_next
);
772 static int vhost_virtqueue_set_addr(struct vhost_dev
*dev
,
773 struct vhost_virtqueue
*vq
,
774 unsigned idx
, bool enable_log
)
776 struct vhost_vring_addr addr
;
778 memset(&addr
, 0, sizeof(struct vhost_vring_addr
));
780 if (dev
->vhost_ops
->vhost_vq_get_addr
) {
781 r
= dev
->vhost_ops
->vhost_vq_get_addr(dev
, &addr
, vq
);
783 VHOST_OPS_DEBUG("vhost_vq_get_addr failed");
787 addr
.desc_user_addr
= (uint64_t)(unsigned long)vq
->desc
;
788 addr
.avail_user_addr
= (uint64_t)(unsigned long)vq
->avail
;
789 addr
.used_user_addr
= (uint64_t)(unsigned long)vq
->used
;
792 addr
.log_guest_addr
= vq
->used_phys
;
793 addr
.flags
= enable_log
? (1 << VHOST_VRING_F_LOG
) : 0;
794 r
= dev
->vhost_ops
->vhost_set_vring_addr(dev
, &addr
);
796 VHOST_OPS_DEBUG("vhost_set_vring_addr failed");
802 static int vhost_dev_set_features(struct vhost_dev
*dev
,
805 uint64_t features
= dev
->acked_features
;
808 features
|= 0x1ULL
<< VHOST_F_LOG_ALL
;
810 if (!vhost_dev_has_iommu(dev
)) {
811 features
&= ~(0x1ULL
<< VIRTIO_F_IOMMU_PLATFORM
);
813 if (dev
->vhost_ops
->vhost_force_iommu
) {
814 if (dev
->vhost_ops
->vhost_force_iommu(dev
) == true) {
815 features
|= 0x1ULL
<< VIRTIO_F_IOMMU_PLATFORM
;
818 r
= dev
->vhost_ops
->vhost_set_features(dev
, features
);
820 VHOST_OPS_DEBUG("vhost_set_features failed");
822 return r
< 0 ? -errno
: 0;
825 static int vhost_dev_set_log(struct vhost_dev
*dev
, bool enable_log
)
828 r
= vhost_dev_set_features(dev
, enable_log
);
832 for (i
= 0; i
< dev
->nvqs
; ++i
) {
833 idx
= dev
->vhost_ops
->vhost_get_vq_index(dev
, dev
->vq_index
+ i
);
834 r
= vhost_virtqueue_set_addr(dev
, dev
->vqs
+ i
, idx
,
842 for (; i
>= 0; --i
) {
843 idx
= dev
->vhost_ops
->vhost_get_vq_index(dev
, dev
->vq_index
+ i
);
844 vhost_virtqueue_set_addr(dev
, dev
->vqs
+ i
, idx
,
847 vhost_dev_set_features(dev
, dev
->log_enabled
);
852 static int vhost_migration_log(MemoryListener
*listener
, bool enable
)
854 struct vhost_dev
*dev
= container_of(listener
, struct vhost_dev
,
857 if (enable
== dev
->log_enabled
) {
861 dev
->log_enabled
= enable
;
865 r
= vhost_dev_set_log(dev
, false);
869 vhost_log_put(dev
, false);
871 vhost_dev_log_resize(dev
, vhost_get_log_size(dev
));
872 r
= vhost_dev_set_log(dev
, true);
877 dev
->log_enabled
= enable
;
881 static void vhost_log_global_start(MemoryListener
*listener
)
885 r
= vhost_migration_log(listener
, true);
891 static void vhost_log_global_stop(MemoryListener
*listener
)
895 r
= vhost_migration_log(listener
, false);
901 static void vhost_log_start(MemoryListener
*listener
,
902 MemoryRegionSection
*section
,
905 /* FIXME: implement */
908 static void vhost_log_stop(MemoryListener
*listener
,
909 MemoryRegionSection
*section
,
912 /* FIXME: implement */
915 /* The vhost driver natively knows how to handle the vrings of non
916 * cross-endian legacy devices and modern devices. Only legacy devices
917 * exposed to a bi-endian guest may require the vhost driver to use a
918 * specific endianness.
920 static inline bool vhost_needs_vring_endian(VirtIODevice
*vdev
)
922 if (virtio_vdev_has_feature(vdev
, VIRTIO_F_VERSION_1
)) {
925 #ifdef HOST_WORDS_BIGENDIAN
926 return vdev
->device_endian
== VIRTIO_DEVICE_ENDIAN_LITTLE
;
928 return vdev
->device_endian
== VIRTIO_DEVICE_ENDIAN_BIG
;
932 static int vhost_virtqueue_set_vring_endian_legacy(struct vhost_dev
*dev
,
936 struct vhost_vring_state s
= {
937 .index
= vhost_vq_index
,
941 if (!dev
->vhost_ops
->vhost_set_vring_endian(dev
, &s
)) {
945 VHOST_OPS_DEBUG("vhost_set_vring_endian failed");
946 if (errno
== ENOTTY
) {
947 error_report("vhost does not support cross-endian");
954 static int vhost_memory_region_lookup(struct vhost_dev
*hdev
,
955 uint64_t gpa
, uint64_t *uaddr
,
960 for (i
= 0; i
< hdev
->mem
->nregions
; i
++) {
961 struct vhost_memory_region
*reg
= hdev
->mem
->regions
+ i
;
963 if (gpa
>= reg
->guest_phys_addr
&&
964 reg
->guest_phys_addr
+ reg
->memory_size
> gpa
) {
965 *uaddr
= reg
->userspace_addr
+ gpa
- reg
->guest_phys_addr
;
966 *len
= reg
->guest_phys_addr
+ reg
->memory_size
- gpa
;
974 int vhost_device_iotlb_miss(struct vhost_dev
*dev
, uint64_t iova
, int write
)
980 RCU_READ_LOCK_GUARD();
982 trace_vhost_iotlb_miss(dev
, 1);
984 iotlb
= address_space_get_iotlb_entry(dev
->vdev
->dma_as
,
986 MEMTXATTRS_UNSPECIFIED
);
987 if (iotlb
.target_as
!= NULL
) {
988 ret
= vhost_memory_region_lookup(dev
, iotlb
.translated_addr
,
991 trace_vhost_iotlb_miss(dev
, 3);
992 error_report("Fail to lookup the translated address "
993 "%"PRIx64
, iotlb
.translated_addr
);
997 len
= MIN(iotlb
.addr_mask
+ 1, len
);
998 iova
= iova
& ~iotlb
.addr_mask
;
1000 ret
= vhost_backend_update_device_iotlb(dev
, iova
, uaddr
,
1003 trace_vhost_iotlb_miss(dev
, 4);
1004 error_report("Fail to update device iotlb");
1009 trace_vhost_iotlb_miss(dev
, 2);
1015 static int vhost_virtqueue_start(struct vhost_dev
*dev
,
1016 struct VirtIODevice
*vdev
,
1017 struct vhost_virtqueue
*vq
,
1020 BusState
*qbus
= BUS(qdev_get_parent_bus(DEVICE(vdev
)));
1021 VirtioBusState
*vbus
= VIRTIO_BUS(qbus
);
1022 VirtioBusClass
*k
= VIRTIO_BUS_GET_CLASS(vbus
);
1025 int vhost_vq_index
= dev
->vhost_ops
->vhost_get_vq_index(dev
, idx
);
1026 struct vhost_vring_file file
= {
1027 .index
= vhost_vq_index
1029 struct vhost_vring_state state
= {
1030 .index
= vhost_vq_index
1032 struct VirtQueue
*vvq
= virtio_get_queue(vdev
, idx
);
1034 a
= virtio_queue_get_desc_addr(vdev
, idx
);
1036 /* Queue might not be ready for start */
1040 vq
->num
= state
.num
= virtio_queue_get_num(vdev
, idx
);
1041 r
= dev
->vhost_ops
->vhost_set_vring_num(dev
, &state
);
1043 VHOST_OPS_DEBUG("vhost_set_vring_num failed");
1047 state
.num
= virtio_queue_get_last_avail_idx(vdev
, idx
);
1048 r
= dev
->vhost_ops
->vhost_set_vring_base(dev
, &state
);
1050 VHOST_OPS_DEBUG("vhost_set_vring_base failed");
1054 if (vhost_needs_vring_endian(vdev
)) {
1055 r
= vhost_virtqueue_set_vring_endian_legacy(dev
,
1056 virtio_is_big_endian(vdev
),
1063 vq
->desc_size
= s
= l
= virtio_queue_get_desc_size(vdev
, idx
);
1065 vq
->desc
= vhost_memory_map(dev
, a
, &l
, false);
1066 if (!vq
->desc
|| l
!= s
) {
1068 goto fail_alloc_desc
;
1070 vq
->avail_size
= s
= l
= virtio_queue_get_avail_size(vdev
, idx
);
1071 vq
->avail_phys
= a
= virtio_queue_get_avail_addr(vdev
, idx
);
1072 vq
->avail
= vhost_memory_map(dev
, a
, &l
, false);
1073 if (!vq
->avail
|| l
!= s
) {
1075 goto fail_alloc_avail
;
1077 vq
->used_size
= s
= l
= virtio_queue_get_used_size(vdev
, idx
);
1078 vq
->used_phys
= a
= virtio_queue_get_used_addr(vdev
, idx
);
1079 vq
->used
= vhost_memory_map(dev
, a
, &l
, true);
1080 if (!vq
->used
|| l
!= s
) {
1082 goto fail_alloc_used
;
1085 r
= vhost_virtqueue_set_addr(dev
, vq
, vhost_vq_index
, dev
->log_enabled
);
1091 file
.fd
= event_notifier_get_fd(virtio_queue_get_host_notifier(vvq
));
1092 r
= dev
->vhost_ops
->vhost_set_vring_kick(dev
, &file
);
1094 VHOST_OPS_DEBUG("vhost_set_vring_kick failed");
1099 /* Clear and discard previous events if any. */
1100 event_notifier_test_and_clear(&vq
->masked_notifier
);
1102 /* Init vring in unmasked state, unless guest_notifier_mask
1105 if (!vdev
->use_guest_notifier_mask
) {
1106 /* TODO: check and handle errors. */
1107 vhost_virtqueue_mask(dev
, vdev
, idx
, false);
1110 if (k
->query_guest_notifiers
&&
1111 k
->query_guest_notifiers(qbus
->parent
) &&
1112 virtio_queue_vector(vdev
, idx
) == VIRTIO_NO_VECTOR
) {
1114 r
= dev
->vhost_ops
->vhost_set_vring_call(dev
, &file
);
1125 vhost_memory_unmap(dev
, vq
->used
, virtio_queue_get_used_size(vdev
, idx
),
1128 vhost_memory_unmap(dev
, vq
->avail
, virtio_queue_get_avail_size(vdev
, idx
),
1131 vhost_memory_unmap(dev
, vq
->desc
, virtio_queue_get_desc_size(vdev
, idx
),
1137 static void vhost_virtqueue_stop(struct vhost_dev
*dev
,
1138 struct VirtIODevice
*vdev
,
1139 struct vhost_virtqueue
*vq
,
1142 int vhost_vq_index
= dev
->vhost_ops
->vhost_get_vq_index(dev
, idx
);
1143 struct vhost_vring_state state
= {
1144 .index
= vhost_vq_index
,
1148 if (virtio_queue_get_desc_addr(vdev
, idx
) == 0) {
1149 /* Don't stop the virtqueue which might have not been started */
1153 r
= dev
->vhost_ops
->vhost_get_vring_base(dev
, &state
);
1155 VHOST_OPS_DEBUG("vhost VQ %u ring restore failed: %d", idx
, r
);
1156 /* Connection to the backend is broken, so let's sync internal
1157 * last avail idx to the device used idx.
1159 virtio_queue_restore_last_avail_idx(vdev
, idx
);
1161 virtio_queue_set_last_avail_idx(vdev
, idx
, state
.num
);
1163 virtio_queue_invalidate_signalled_used(vdev
, idx
);
1164 virtio_queue_update_used_idx(vdev
, idx
);
1166 /* In the cross-endian case, we need to reset the vring endianness to
1167 * native as legacy devices expect so by default.
1169 if (vhost_needs_vring_endian(vdev
)) {
1170 vhost_virtqueue_set_vring_endian_legacy(dev
,
1171 !virtio_is_big_endian(vdev
),
1175 vhost_memory_unmap(dev
, vq
->used
, virtio_queue_get_used_size(vdev
, idx
),
1176 1, virtio_queue_get_used_size(vdev
, idx
));
1177 vhost_memory_unmap(dev
, vq
->avail
, virtio_queue_get_avail_size(vdev
, idx
),
1178 0, virtio_queue_get_avail_size(vdev
, idx
));
1179 vhost_memory_unmap(dev
, vq
->desc
, virtio_queue_get_desc_size(vdev
, idx
),
1180 0, virtio_queue_get_desc_size(vdev
, idx
));
1183 static void vhost_eventfd_add(MemoryListener
*listener
,
1184 MemoryRegionSection
*section
,
1185 bool match_data
, uint64_t data
, EventNotifier
*e
)
1189 static void vhost_eventfd_del(MemoryListener
*listener
,
1190 MemoryRegionSection
*section
,
1191 bool match_data
, uint64_t data
, EventNotifier
*e
)
1195 static int vhost_virtqueue_set_busyloop_timeout(struct vhost_dev
*dev
,
1196 int n
, uint32_t timeout
)
1198 int vhost_vq_index
= dev
->vhost_ops
->vhost_get_vq_index(dev
, n
);
1199 struct vhost_vring_state state
= {
1200 .index
= vhost_vq_index
,
1205 if (!dev
->vhost_ops
->vhost_set_vring_busyloop_timeout
) {
1209 r
= dev
->vhost_ops
->vhost_set_vring_busyloop_timeout(dev
, &state
);
1211 VHOST_OPS_DEBUG("vhost_set_vring_busyloop_timeout failed");
1218 static int vhost_virtqueue_init(struct vhost_dev
*dev
,
1219 struct vhost_virtqueue
*vq
, int n
)
1221 int vhost_vq_index
= dev
->vhost_ops
->vhost_get_vq_index(dev
, n
);
1222 struct vhost_vring_file file
= {
1223 .index
= vhost_vq_index
,
1225 int r
= event_notifier_init(&vq
->masked_notifier
, 0);
1230 file
.fd
= event_notifier_get_fd(&vq
->masked_notifier
);
1231 r
= dev
->vhost_ops
->vhost_set_vring_call(dev
, &file
);
1233 VHOST_OPS_DEBUG("vhost_set_vring_call failed");
1242 event_notifier_cleanup(&vq
->masked_notifier
);
1246 static void vhost_virtqueue_cleanup(struct vhost_virtqueue
*vq
)
1248 event_notifier_cleanup(&vq
->masked_notifier
);
1251 int vhost_dev_init(struct vhost_dev
*hdev
, void *opaque
,
1252 VhostBackendType backend_type
, uint32_t busyloop_timeout
)
1255 int i
, r
, n_initialized_vqs
= 0;
1256 Error
*local_err
= NULL
;
1259 hdev
->migration_blocker
= NULL
;
1261 r
= vhost_set_backend_type(hdev
, backend_type
);
1264 r
= hdev
->vhost_ops
->vhost_backend_init(hdev
, opaque
);
1269 r
= hdev
->vhost_ops
->vhost_set_owner(hdev
);
1271 VHOST_OPS_DEBUG("vhost_set_owner failed");
1275 r
= hdev
->vhost_ops
->vhost_get_features(hdev
, &features
);
1277 VHOST_OPS_DEBUG("vhost_get_features failed");
1281 for (i
= 0; i
< hdev
->nvqs
; ++i
, ++n_initialized_vqs
) {
1282 r
= vhost_virtqueue_init(hdev
, hdev
->vqs
+ i
, hdev
->vq_index
+ i
);
1288 if (busyloop_timeout
) {
1289 for (i
= 0; i
< hdev
->nvqs
; ++i
) {
1290 r
= vhost_virtqueue_set_busyloop_timeout(hdev
, hdev
->vq_index
+ i
,
1298 hdev
->features
= features
;
1300 hdev
->memory_listener
= (MemoryListener
) {
1301 .begin
= vhost_begin
,
1302 .commit
= vhost_commit
,
1303 .region_add
= vhost_region_addnop
,
1304 .region_nop
= vhost_region_addnop
,
1305 .log_start
= vhost_log_start
,
1306 .log_stop
= vhost_log_stop
,
1307 .log_sync
= vhost_log_sync
,
1308 .log_global_start
= vhost_log_global_start
,
1309 .log_global_stop
= vhost_log_global_stop
,
1310 .eventfd_add
= vhost_eventfd_add
,
1311 .eventfd_del
= vhost_eventfd_del
,
1315 hdev
->iommu_listener
= (MemoryListener
) {
1316 .region_add
= vhost_iommu_region_add
,
1317 .region_del
= vhost_iommu_region_del
,
1320 if (hdev
->migration_blocker
== NULL
) {
1321 if (!(hdev
->features
& (0x1ULL
<< VHOST_F_LOG_ALL
))) {
1322 error_setg(&hdev
->migration_blocker
,
1323 "Migration disabled: vhost lacks VHOST_F_LOG_ALL feature.");
1324 } else if (vhost_dev_log_is_shared(hdev
) && !qemu_memfd_alloc_check()) {
1325 error_setg(&hdev
->migration_blocker
,
1326 "Migration disabled: failed to allocate shared memory");
1330 if (hdev
->migration_blocker
!= NULL
) {
1331 r
= migrate_add_blocker(hdev
->migration_blocker
, &local_err
);
1333 error_report_err(local_err
);
1334 error_free(hdev
->migration_blocker
);
1339 hdev
->mem
= g_malloc0(offsetof(struct vhost_memory
, regions
));
1340 hdev
->n_mem_sections
= 0;
1341 hdev
->mem_sections
= NULL
;
1344 hdev
->log_enabled
= false;
1345 hdev
->started
= false;
1346 memory_listener_register(&hdev
->memory_listener
, &address_space_memory
);
1347 QLIST_INSERT_HEAD(&vhost_devices
, hdev
, entry
);
1349 if (used_memslots
> hdev
->vhost_ops
->vhost_backend_memslots_limit(hdev
)) {
1350 error_report("vhost backend memory slots limit is less"
1351 " than current number of present memory slots");
1353 if (busyloop_timeout
) {
1364 vhost_virtqueue_set_busyloop_timeout(hdev
, hdev
->vq_index
+ i
, 0);
1367 hdev
->nvqs
= n_initialized_vqs
;
1368 vhost_dev_cleanup(hdev
);
1372 void vhost_dev_cleanup(struct vhost_dev
*hdev
)
1376 for (i
= 0; i
< hdev
->nvqs
; ++i
) {
1377 vhost_virtqueue_cleanup(hdev
->vqs
+ i
);
1380 /* those are only safe after successful init */
1381 memory_listener_unregister(&hdev
->memory_listener
);
1382 QLIST_REMOVE(hdev
, entry
);
1384 if (hdev
->migration_blocker
) {
1385 migrate_del_blocker(hdev
->migration_blocker
);
1386 error_free(hdev
->migration_blocker
);
1389 g_free(hdev
->mem_sections
);
1390 if (hdev
->vhost_ops
) {
1391 hdev
->vhost_ops
->vhost_backend_cleanup(hdev
);
1395 memset(hdev
, 0, sizeof(struct vhost_dev
));
1398 /* Stop processing guest IO notifications in qemu.
1399 * Start processing them in vhost in kernel.
1401 int vhost_dev_enable_notifiers(struct vhost_dev
*hdev
, VirtIODevice
*vdev
)
1403 BusState
*qbus
= BUS(qdev_get_parent_bus(DEVICE(vdev
)));
1406 /* We will pass the notifiers to the kernel, make sure that QEMU
1407 * doesn't interfere.
1409 r
= virtio_device_grab_ioeventfd(vdev
);
1411 error_report("binding does not support host notifiers");
1415 for (i
= 0; i
< hdev
->nvqs
; ++i
) {
1416 r
= virtio_bus_set_host_notifier(VIRTIO_BUS(qbus
), hdev
->vq_index
+ i
,
1419 error_report("vhost VQ %d notifier binding failed: %d", i
, -r
);
1427 e
= virtio_bus_set_host_notifier(VIRTIO_BUS(qbus
), hdev
->vq_index
+ i
,
1430 error_report("vhost VQ %d notifier cleanup error: %d", i
, -r
);
1433 virtio_bus_cleanup_host_notifier(VIRTIO_BUS(qbus
), hdev
->vq_index
+ i
);
1435 virtio_device_release_ioeventfd(vdev
);
1440 /* Stop processing guest IO notifications in vhost.
1441 * Start processing them in qemu.
1442 * This might actually run the qemu handlers right away,
1443 * so virtio in qemu must be completely setup when this is called.
1445 void vhost_dev_disable_notifiers(struct vhost_dev
*hdev
, VirtIODevice
*vdev
)
1447 BusState
*qbus
= BUS(qdev_get_parent_bus(DEVICE(vdev
)));
1450 for (i
= 0; i
< hdev
->nvqs
; ++i
) {
1451 r
= virtio_bus_set_host_notifier(VIRTIO_BUS(qbus
), hdev
->vq_index
+ i
,
1454 error_report("vhost VQ %d notifier cleanup failed: %d", i
, -r
);
1457 virtio_bus_cleanup_host_notifier(VIRTIO_BUS(qbus
), hdev
->vq_index
+ i
);
1459 virtio_device_release_ioeventfd(vdev
);
1462 /* Test and clear event pending status.
1463 * Should be called after unmask to avoid losing events.
1465 bool vhost_virtqueue_pending(struct vhost_dev
*hdev
, int n
)
1467 struct vhost_virtqueue
*vq
= hdev
->vqs
+ n
- hdev
->vq_index
;
1468 assert(n
>= hdev
->vq_index
&& n
< hdev
->vq_index
+ hdev
->nvqs
);
1469 return event_notifier_test_and_clear(&vq
->masked_notifier
);
1472 /* Mask/unmask events from this vq. */
1473 void vhost_virtqueue_mask(struct vhost_dev
*hdev
, VirtIODevice
*vdev
, int n
,
1476 struct VirtQueue
*vvq
= virtio_get_queue(vdev
, n
);
1477 int r
, index
= n
- hdev
->vq_index
;
1478 struct vhost_vring_file file
;
1480 /* should only be called after backend is connected */
1481 assert(hdev
->vhost_ops
);
1484 assert(vdev
->use_guest_notifier_mask
);
1485 file
.fd
= event_notifier_get_fd(&hdev
->vqs
[index
].masked_notifier
);
1487 file
.fd
= event_notifier_get_fd(virtio_queue_get_guest_notifier(vvq
));
1490 file
.index
= hdev
->vhost_ops
->vhost_get_vq_index(hdev
, n
);
1491 r
= hdev
->vhost_ops
->vhost_set_vring_call(hdev
, &file
);
1493 VHOST_OPS_DEBUG("vhost_set_vring_call failed");
1497 uint64_t vhost_get_features(struct vhost_dev
*hdev
, const int *feature_bits
,
1500 const int *bit
= feature_bits
;
1501 while (*bit
!= VHOST_INVALID_FEATURE_BIT
) {
1502 uint64_t bit_mask
= (1ULL << *bit
);
1503 if (!(hdev
->features
& bit_mask
)) {
1504 features
&= ~bit_mask
;
1511 void vhost_ack_features(struct vhost_dev
*hdev
, const int *feature_bits
,
1514 const int *bit
= feature_bits
;
1515 while (*bit
!= VHOST_INVALID_FEATURE_BIT
) {
1516 uint64_t bit_mask
= (1ULL << *bit
);
1517 if (features
& bit_mask
) {
1518 hdev
->acked_features
|= bit_mask
;
1524 int vhost_dev_get_config(struct vhost_dev
*hdev
, uint8_t *config
,
1525 uint32_t config_len
)
1527 assert(hdev
->vhost_ops
);
1529 if (hdev
->vhost_ops
->vhost_get_config
) {
1530 return hdev
->vhost_ops
->vhost_get_config(hdev
, config
, config_len
);
1536 int vhost_dev_set_config(struct vhost_dev
*hdev
, const uint8_t *data
,
1537 uint32_t offset
, uint32_t size
, uint32_t flags
)
1539 assert(hdev
->vhost_ops
);
1541 if (hdev
->vhost_ops
->vhost_set_config
) {
1542 return hdev
->vhost_ops
->vhost_set_config(hdev
, data
, offset
,
1549 void vhost_dev_set_config_notifier(struct vhost_dev
*hdev
,
1550 const VhostDevConfigOps
*ops
)
1552 hdev
->config_ops
= ops
;
1555 void vhost_dev_free_inflight(struct vhost_inflight
*inflight
)
1557 if (inflight
&& inflight
->addr
) {
1558 qemu_memfd_free(inflight
->addr
, inflight
->size
, inflight
->fd
);
1559 inflight
->addr
= NULL
;
1564 static int vhost_dev_resize_inflight(struct vhost_inflight
*inflight
,
1569 void *addr
= qemu_memfd_alloc("vhost-inflight", new_size
,
1570 F_SEAL_GROW
| F_SEAL_SHRINK
| F_SEAL_SEAL
,
1574 error_report_err(err
);
1578 vhost_dev_free_inflight(inflight
);
1579 inflight
->offset
= 0;
1580 inflight
->addr
= addr
;
1582 inflight
->size
= new_size
;
1587 void vhost_dev_save_inflight(struct vhost_inflight
*inflight
, QEMUFile
*f
)
1589 if (inflight
->addr
) {
1590 qemu_put_be64(f
, inflight
->size
);
1591 qemu_put_be16(f
, inflight
->queue_size
);
1592 qemu_put_buffer(f
, inflight
->addr
, inflight
->size
);
1594 qemu_put_be64(f
, 0);
1598 int vhost_dev_load_inflight(struct vhost_inflight
*inflight
, QEMUFile
*f
)
1602 size
= qemu_get_be64(f
);
1607 if (inflight
->size
!= size
) {
1608 if (vhost_dev_resize_inflight(inflight
, size
)) {
1612 inflight
->queue_size
= qemu_get_be16(f
);
1614 qemu_get_buffer(f
, inflight
->addr
, size
);
1619 int vhost_dev_set_inflight(struct vhost_dev
*dev
,
1620 struct vhost_inflight
*inflight
)
1624 if (dev
->vhost_ops
->vhost_set_inflight_fd
&& inflight
->addr
) {
1625 r
= dev
->vhost_ops
->vhost_set_inflight_fd(dev
, inflight
);
1627 VHOST_OPS_DEBUG("vhost_set_inflight_fd failed");
1635 int vhost_dev_get_inflight(struct vhost_dev
*dev
, uint16_t queue_size
,
1636 struct vhost_inflight
*inflight
)
1640 if (dev
->vhost_ops
->vhost_get_inflight_fd
) {
1641 r
= dev
->vhost_ops
->vhost_get_inflight_fd(dev
, queue_size
, inflight
);
1643 VHOST_OPS_DEBUG("vhost_get_inflight_fd failed");
1651 /* Host notifiers must be enabled at this point. */
1652 int vhost_dev_start(struct vhost_dev
*hdev
, VirtIODevice
*vdev
)
1656 /* should only be called after backend is connected */
1657 assert(hdev
->vhost_ops
);
1659 hdev
->started
= true;
1662 r
= vhost_dev_set_features(hdev
, hdev
->log_enabled
);
1667 if (vhost_dev_has_iommu(hdev
)) {
1668 memory_listener_register(&hdev
->iommu_listener
, vdev
->dma_as
);
1671 r
= hdev
->vhost_ops
->vhost_set_mem_table(hdev
, hdev
->mem
);
1673 VHOST_OPS_DEBUG("vhost_set_mem_table failed");
1677 for (i
= 0; i
< hdev
->nvqs
; ++i
) {
1678 r
= vhost_virtqueue_start(hdev
,
1681 hdev
->vq_index
+ i
);
1687 if (hdev
->log_enabled
) {
1690 hdev
->log_size
= vhost_get_log_size(hdev
);
1691 hdev
->log
= vhost_log_get(hdev
->log_size
,
1692 vhost_dev_log_is_shared(hdev
));
1693 log_base
= (uintptr_t)hdev
->log
->log
;
1694 r
= hdev
->vhost_ops
->vhost_set_log_base(hdev
,
1695 hdev
->log_size
? log_base
: 0,
1698 VHOST_OPS_DEBUG("vhost_set_log_base failed");
1703 if (hdev
->vhost_ops
->vhost_dev_start
) {
1704 r
= hdev
->vhost_ops
->vhost_dev_start(hdev
, true);
1709 if (vhost_dev_has_iommu(hdev
) &&
1710 hdev
->vhost_ops
->vhost_set_iotlb_callback
) {
1711 hdev
->vhost_ops
->vhost_set_iotlb_callback(hdev
, true);
1713 /* Update used ring information for IOTLB to work correctly,
1714 * vhost-kernel code requires for this.*/
1715 for (i
= 0; i
< hdev
->nvqs
; ++i
) {
1716 struct vhost_virtqueue
*vq
= hdev
->vqs
+ i
;
1717 vhost_device_iotlb_miss(hdev
, vq
->used_phys
, true);
1722 vhost_log_put(hdev
, false);
1725 vhost_virtqueue_stop(hdev
,
1728 hdev
->vq_index
+ i
);
1734 hdev
->started
= false;
1738 /* Host notifiers must be enabled at this point. */
1739 void vhost_dev_stop(struct vhost_dev
*hdev
, VirtIODevice
*vdev
)
1743 /* should only be called after backend is connected */
1744 assert(hdev
->vhost_ops
);
1746 if (hdev
->vhost_ops
->vhost_dev_start
) {
1747 hdev
->vhost_ops
->vhost_dev_start(hdev
, false);
1749 for (i
= 0; i
< hdev
->nvqs
; ++i
) {
1750 vhost_virtqueue_stop(hdev
,
1753 hdev
->vq_index
+ i
);
1756 if (vhost_dev_has_iommu(hdev
)) {
1757 if (hdev
->vhost_ops
->vhost_set_iotlb_callback
) {
1758 hdev
->vhost_ops
->vhost_set_iotlb_callback(hdev
, false);
1760 memory_listener_unregister(&hdev
->iommu_listener
);
1762 vhost_log_put(hdev
, true);
1763 hdev
->started
= false;
1767 int vhost_net_set_backend(struct vhost_dev
*hdev
,
1768 struct vhost_vring_file
*file
)
1770 if (hdev
->vhost_ops
->vhost_net_set_backend
) {
1771 return hdev
->vhost_ops
->vhost_net_set_backend(hdev
, file
);