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 "hw/virtio/vhost.h"
18 #include "qemu/atomic.h"
19 #include "qemu/range.h"
20 #include <linux/vhost.h>
21 #include "exec/address-spaces.h"
22 #include "hw/virtio/virtio-bus.h"
24 static void vhost_dev_sync_region(struct vhost_dev *dev,
25 MemoryRegionSection *section,
26 uint64_t mfirst, uint64_t mlast,
27 uint64_t rfirst, uint64_t rlast)
29 uint64_t start = MAX(mfirst, rfirst);
30 uint64_t end = MIN(mlast, rlast);
31 vhost_log_chunk_t *from = dev->log + start / VHOST_LOG_CHUNK;
32 vhost_log_chunk_t *to = dev->log + end / VHOST_LOG_CHUNK + 1;
33 uint64_t addr = (start / VHOST_LOG_CHUNK) * VHOST_LOG_CHUNK;
38 assert(end / VHOST_LOG_CHUNK < dev->log_size);
39 assert(start / VHOST_LOG_CHUNK < dev->log_size);
41 for (;from < to; ++from) {
42 vhost_log_chunk_t log;
43 /* We first check with non-atomic: much cheaper,
44 * and we expect non-dirty to be the common case. */
46 addr += VHOST_LOG_CHUNK;
49 /* Data must be read atomically. We don't really need barrier semantics
50 * but it's easier to use atomic_* than roll our own. */
51 log = atomic_xchg(from, 0);
55 hwaddr section_offset;
57 page_addr = addr + bit * VHOST_LOG_PAGE;
58 section_offset = page_addr - section->offset_within_address_space;
59 mr_offset = section_offset + section->offset_within_region;
60 memory_region_set_dirty(section->mr, mr_offset, VHOST_LOG_PAGE);
61 log &= ~(0x1ull << bit);
63 addr += VHOST_LOG_CHUNK;
67 static int vhost_sync_dirty_bitmap(struct vhost_dev *dev,
68 MemoryRegionSection *section,
76 if (!dev->log_enabled || !dev->started) {
79 start_addr = section->offset_within_address_space;
80 end_addr = range_get_last(start_addr, int128_get64(section->size));
81 start_addr = MAX(first, start_addr);
82 end_addr = MIN(last, end_addr);
84 for (i = 0; i < dev->mem->nregions; ++i) {
85 struct vhost_memory_region *reg = dev->mem->regions + i;
86 vhost_dev_sync_region(dev, section, start_addr, end_addr,
88 range_get_last(reg->guest_phys_addr,
91 for (i = 0; i < dev->nvqs; ++i) {
92 struct vhost_virtqueue *vq = dev->vqs + i;
93 vhost_dev_sync_region(dev, section, start_addr, end_addr, vq->used_phys,
94 range_get_last(vq->used_phys, vq->used_size));
99 static void vhost_log_sync(MemoryListener *listener,
100 MemoryRegionSection *section)
102 struct vhost_dev *dev = container_of(listener, struct vhost_dev,
104 vhost_sync_dirty_bitmap(dev, section, 0x0, ~0x0ULL);
107 static void vhost_log_sync_range(struct vhost_dev *dev,
108 hwaddr first, hwaddr last)
111 /* FIXME: this is N^2 in number of sections */
112 for (i = 0; i < dev->n_mem_sections; ++i) {
113 MemoryRegionSection *section = &dev->mem_sections[i];
114 vhost_sync_dirty_bitmap(dev, section, first, last);
118 /* Assign/unassign. Keep an unsorted array of non-overlapping
119 * memory regions in dev->mem. */
120 static void vhost_dev_unassign_memory(struct vhost_dev *dev,
124 int from, to, n = dev->mem->nregions;
125 /* Track overlapping/split regions for sanity checking. */
126 int overlap_start = 0, overlap_end = 0, overlap_middle = 0, split = 0;
128 for (from = 0, to = 0; from < n; ++from, ++to) {
129 struct vhost_memory_region *reg = dev->mem->regions + to;
134 /* clone old region */
136 memcpy(reg, dev->mem->regions + from, sizeof *reg);
139 /* No overlap is simple */
140 if (!ranges_overlap(reg->guest_phys_addr, reg->memory_size,
145 /* Split only happens if supplied region
146 * is in the middle of an existing one. Thus it can not
147 * overlap with any other existing region. */
150 reglast = range_get_last(reg->guest_phys_addr, reg->memory_size);
151 memlast = range_get_last(start_addr, size);
153 /* Remove whole region */
154 if (start_addr <= reg->guest_phys_addr && memlast >= reglast) {
155 --dev->mem->nregions;
162 if (memlast >= reglast) {
163 reg->memory_size = start_addr - reg->guest_phys_addr;
164 assert(reg->memory_size);
165 assert(!overlap_end);
171 if (start_addr <= reg->guest_phys_addr) {
172 change = memlast + 1 - reg->guest_phys_addr;
173 reg->memory_size -= change;
174 reg->guest_phys_addr += change;
175 reg->userspace_addr += change;
176 assert(reg->memory_size);
177 assert(!overlap_start);
182 /* This only happens if supplied region
183 * is in the middle of an existing one. Thus it can not
184 * overlap with any other existing region. */
185 assert(!overlap_start);
186 assert(!overlap_end);
187 assert(!overlap_middle);
188 /* Split region: shrink first part, shift second part. */
189 memcpy(dev->mem->regions + n, reg, sizeof *reg);
190 reg->memory_size = start_addr - reg->guest_phys_addr;
191 assert(reg->memory_size);
192 change = memlast + 1 - reg->guest_phys_addr;
193 reg = dev->mem->regions + n;
194 reg->memory_size -= change;
195 assert(reg->memory_size);
196 reg->guest_phys_addr += change;
197 reg->userspace_addr += change;
198 /* Never add more than 1 region */
199 assert(dev->mem->nregions == n);
200 ++dev->mem->nregions;
205 /* Called after unassign, so no regions overlap the given range. */
206 static void vhost_dev_assign_memory(struct vhost_dev *dev,
212 struct vhost_memory_region *merged = NULL;
213 for (from = 0, to = 0; from < dev->mem->nregions; ++from, ++to) {
214 struct vhost_memory_region *reg = dev->mem->regions + to;
215 uint64_t prlast, urlast;
216 uint64_t pmlast, umlast;
219 /* clone old region */
221 memcpy(reg, dev->mem->regions + from, sizeof *reg);
223 prlast = range_get_last(reg->guest_phys_addr, reg->memory_size);
224 pmlast = range_get_last(start_addr, size);
225 urlast = range_get_last(reg->userspace_addr, reg->memory_size);
226 umlast = range_get_last(uaddr, size);
228 /* check for overlapping regions: should never happen. */
229 assert(prlast < start_addr || pmlast < reg->guest_phys_addr);
230 /* Not an adjacent or overlapping region - do not merge. */
231 if ((prlast + 1 != start_addr || urlast + 1 != uaddr) &&
232 (pmlast + 1 != reg->guest_phys_addr ||
233 umlast + 1 != reg->userspace_addr)) {
243 u = MIN(uaddr, reg->userspace_addr);
244 s = MIN(start_addr, reg->guest_phys_addr);
245 e = MAX(pmlast, prlast);
246 uaddr = merged->userspace_addr = u;
247 start_addr = merged->guest_phys_addr = s;
248 size = merged->memory_size = e - s + 1;
249 assert(merged->memory_size);
253 struct vhost_memory_region *reg = dev->mem->regions + to;
254 memset(reg, 0, sizeof *reg);
255 reg->memory_size = size;
256 assert(reg->memory_size);
257 reg->guest_phys_addr = start_addr;
258 reg->userspace_addr = uaddr;
261 assert(to <= dev->mem->nregions + 1);
262 dev->mem->nregions = to;
265 static uint64_t vhost_get_log_size(struct vhost_dev *dev)
267 uint64_t log_size = 0;
269 for (i = 0; i < dev->mem->nregions; ++i) {
270 struct vhost_memory_region *reg = dev->mem->regions + i;
271 uint64_t last = range_get_last(reg->guest_phys_addr,
273 log_size = MAX(log_size, last / VHOST_LOG_CHUNK + 1);
275 for (i = 0; i < dev->nvqs; ++i) {
276 struct vhost_virtqueue *vq = dev->vqs + i;
277 uint64_t last = vq->used_phys + vq->used_size - 1;
278 log_size = MAX(log_size, last / VHOST_LOG_CHUNK + 1);
283 static inline void vhost_dev_log_resize(struct vhost_dev* dev, uint64_t size)
285 vhost_log_chunk_t *log;
289 log = g_malloc0(size * sizeof *log);
290 log_base = (uint64_t)(unsigned long)log;
291 r = dev->vhost_ops->vhost_call(dev, VHOST_SET_LOG_BASE, &log_base);
293 /* Sync only the range covered by the old log */
295 vhost_log_sync_range(dev, 0, dev->log_size * VHOST_LOG_CHUNK - 1);
299 dev->log_size = size;
302 static int vhost_verify_ring_mappings(struct vhost_dev *dev,
307 for (i = 0; i < dev->nvqs; ++i) {
308 struct vhost_virtqueue *vq = dev->vqs + i;
312 if (!ranges_overlap(start_addr, size, vq->ring_phys, vq->ring_size)) {
316 p = cpu_physical_memory_map(vq->ring_phys, &l, 1);
317 if (!p || l != vq->ring_size) {
318 fprintf(stderr, "Unable to map ring buffer for ring %d\n", i);
322 fprintf(stderr, "Ring buffer relocated for ring %d\n", i);
325 cpu_physical_memory_unmap(p, l, 0, 0);
330 static struct vhost_memory_region *vhost_dev_find_reg(struct vhost_dev *dev,
334 int i, n = dev->mem->nregions;
335 for (i = 0; i < n; ++i) {
336 struct vhost_memory_region *reg = dev->mem->regions + i;
337 if (ranges_overlap(reg->guest_phys_addr, reg->memory_size,
345 static bool vhost_dev_cmp_memory(struct vhost_dev *dev,
350 struct vhost_memory_region *reg = vhost_dev_find_reg(dev, start_addr, size);
358 reglast = range_get_last(reg->guest_phys_addr, reg->memory_size);
359 memlast = range_get_last(start_addr, size);
361 /* Need to extend region? */
362 if (start_addr < reg->guest_phys_addr || memlast > reglast) {
365 /* userspace_addr changed? */
366 return uaddr != reg->userspace_addr + start_addr - reg->guest_phys_addr;
369 static void vhost_set_memory(MemoryListener *listener,
370 MemoryRegionSection *section,
373 struct vhost_dev *dev = container_of(listener, struct vhost_dev,
375 hwaddr start_addr = section->offset_within_address_space;
376 ram_addr_t size = int128_get64(section->size);
377 bool log_dirty = memory_region_is_logging(section->mr);
378 int s = offsetof(struct vhost_memory, regions) +
379 (dev->mem->nregions + 1) * sizeof dev->mem->regions[0];
382 dev->mem = g_realloc(dev->mem, s);
390 /* Optimize no-change case. At least cirrus_vga does this a lot at this time. */
391 ram = memory_region_get_ram_ptr(section->mr) + section->offset_within_region;
393 if (!vhost_dev_cmp_memory(dev, start_addr, size, (uintptr_t)ram)) {
394 /* Region exists with same address. Nothing to do. */
398 if (!vhost_dev_find_reg(dev, start_addr, size)) {
399 /* Removing region that we don't access. Nothing to do. */
404 vhost_dev_unassign_memory(dev, start_addr, size);
406 /* Add given mapping, merging adjacent regions if any */
407 vhost_dev_assign_memory(dev, start_addr, size, (uintptr_t)ram);
409 /* Remove old mapping for this memory, if any. */
410 vhost_dev_unassign_memory(dev, start_addr, size);
412 dev->mem_changed_start_addr = MIN(dev->mem_changed_start_addr, start_addr);
413 dev->mem_changed_end_addr = MAX(dev->mem_changed_end_addr, start_addr + size - 1);
414 dev->memory_changed = true;
417 static bool vhost_section(MemoryRegionSection *section)
419 return memory_region_is_ram(section->mr);
422 static void vhost_begin(MemoryListener *listener)
424 struct vhost_dev *dev = container_of(listener, struct vhost_dev,
426 dev->mem_changed_end_addr = 0;
427 dev->mem_changed_start_addr = -1;
430 static void vhost_commit(MemoryListener *listener)
432 struct vhost_dev *dev = container_of(listener, struct vhost_dev,
434 hwaddr start_addr = 0;
439 if (!dev->memory_changed) {
445 if (dev->mem_changed_start_addr > dev->mem_changed_end_addr) {
450 start_addr = dev->mem_changed_start_addr;
451 size = dev->mem_changed_end_addr - dev->mem_changed_start_addr + 1;
453 r = vhost_verify_ring_mappings(dev, start_addr, size);
457 if (!dev->log_enabled) {
458 r = dev->vhost_ops->vhost_call(dev, VHOST_SET_MEM_TABLE, dev->mem);
460 dev->memory_changed = false;
463 log_size = vhost_get_log_size(dev);
464 /* We allocate an extra 4K bytes to log,
465 * to reduce the * number of reallocations. */
466 #define VHOST_LOG_BUFFER (0x1000 / sizeof *dev->log)
467 /* To log more, must increase log size before table update. */
468 if (dev->log_size < log_size) {
469 vhost_dev_log_resize(dev, log_size + VHOST_LOG_BUFFER);
471 r = dev->vhost_ops->vhost_call(dev, VHOST_SET_MEM_TABLE, dev->mem);
473 /* To log less, can only decrease log size after table update. */
474 if (dev->log_size > log_size + VHOST_LOG_BUFFER) {
475 vhost_dev_log_resize(dev, log_size);
477 dev->memory_changed = false;
480 static void vhost_region_add(MemoryListener *listener,
481 MemoryRegionSection *section)
483 struct vhost_dev *dev = container_of(listener, struct vhost_dev,
486 if (!vhost_section(section)) {
490 ++dev->n_mem_sections;
491 dev->mem_sections = g_renew(MemoryRegionSection, dev->mem_sections,
492 dev->n_mem_sections);
493 dev->mem_sections[dev->n_mem_sections - 1] = *section;
494 memory_region_ref(section->mr);
495 vhost_set_memory(listener, section, true);
498 static void vhost_region_del(MemoryListener *listener,
499 MemoryRegionSection *section)
501 struct vhost_dev *dev = container_of(listener, struct vhost_dev,
505 if (!vhost_section(section)) {
509 vhost_set_memory(listener, section, false);
510 memory_region_unref(section->mr);
511 for (i = 0; i < dev->n_mem_sections; ++i) {
512 if (dev->mem_sections[i].offset_within_address_space
513 == section->offset_within_address_space) {
514 --dev->n_mem_sections;
515 memmove(&dev->mem_sections[i], &dev->mem_sections[i+1],
516 (dev->n_mem_sections - i) * sizeof(*dev->mem_sections));
522 static void vhost_region_nop(MemoryListener *listener,
523 MemoryRegionSection *section)
527 static int vhost_virtqueue_set_addr(struct vhost_dev *dev,
528 struct vhost_virtqueue *vq,
529 unsigned idx, bool enable_log)
531 struct vhost_vring_addr addr = {
533 .desc_user_addr = (uint64_t)(unsigned long)vq->desc,
534 .avail_user_addr = (uint64_t)(unsigned long)vq->avail,
535 .used_user_addr = (uint64_t)(unsigned long)vq->used,
536 .log_guest_addr = vq->used_phys,
537 .flags = enable_log ? (1 << VHOST_VRING_F_LOG) : 0,
539 int r = dev->vhost_ops->vhost_call(dev, VHOST_SET_VRING_ADDR, &addr);
546 static int vhost_dev_set_features(struct vhost_dev *dev, bool enable_log)
548 uint64_t features = dev->acked_features;
551 features |= 0x1 << VHOST_F_LOG_ALL;
553 r = dev->vhost_ops->vhost_call(dev, VHOST_SET_FEATURES, &features);
554 return r < 0 ? -errno : 0;
557 static int vhost_dev_set_log(struct vhost_dev *dev, bool enable_log)
560 r = vhost_dev_set_features(dev, enable_log);
564 for (i = 0; i < dev->nvqs; ++i) {
565 r = vhost_virtqueue_set_addr(dev, dev->vqs + i, i,
573 for (; i >= 0; --i) {
574 t = vhost_virtqueue_set_addr(dev, dev->vqs + i, i,
578 t = vhost_dev_set_features(dev, dev->log_enabled);
584 static int vhost_migration_log(MemoryListener *listener, int enable)
586 struct vhost_dev *dev = container_of(listener, struct vhost_dev,
589 if (!!enable == dev->log_enabled) {
593 dev->log_enabled = enable;
597 r = vhost_dev_set_log(dev, false);
605 vhost_dev_log_resize(dev, vhost_get_log_size(dev));
606 r = vhost_dev_set_log(dev, true);
611 dev->log_enabled = enable;
615 static void vhost_log_global_start(MemoryListener *listener)
619 r = vhost_migration_log(listener, true);
625 static void vhost_log_global_stop(MemoryListener *listener)
629 r = vhost_migration_log(listener, false);
635 static void vhost_log_start(MemoryListener *listener,
636 MemoryRegionSection *section)
638 /* FIXME: implement */
641 static void vhost_log_stop(MemoryListener *listener,
642 MemoryRegionSection *section)
644 /* FIXME: implement */
647 static int vhost_virtqueue_start(struct vhost_dev *dev,
648 struct VirtIODevice *vdev,
649 struct vhost_virtqueue *vq,
654 int vhost_vq_index = idx - dev->vq_index;
655 struct vhost_vring_file file = {
656 .index = vhost_vq_index
658 struct vhost_vring_state state = {
659 .index = vhost_vq_index
661 struct VirtQueue *vvq = virtio_get_queue(vdev, idx);
663 assert(idx >= dev->vq_index && idx < dev->vq_index + dev->nvqs);
665 vq->num = state.num = virtio_queue_get_num(vdev, idx);
666 r = dev->vhost_ops->vhost_call(dev, VHOST_SET_VRING_NUM, &state);
671 state.num = virtio_queue_get_last_avail_idx(vdev, idx);
672 r = dev->vhost_ops->vhost_call(dev, VHOST_SET_VRING_BASE, &state);
677 s = l = virtio_queue_get_desc_size(vdev, idx);
678 a = virtio_queue_get_desc_addr(vdev, idx);
679 vq->desc = cpu_physical_memory_map(a, &l, 0);
680 if (!vq->desc || l != s) {
682 goto fail_alloc_desc;
684 s = l = virtio_queue_get_avail_size(vdev, idx);
685 a = virtio_queue_get_avail_addr(vdev, idx);
686 vq->avail = cpu_physical_memory_map(a, &l, 0);
687 if (!vq->avail || l != s) {
689 goto fail_alloc_avail;
691 vq->used_size = s = l = virtio_queue_get_used_size(vdev, idx);
692 vq->used_phys = a = virtio_queue_get_used_addr(vdev, idx);
693 vq->used = cpu_physical_memory_map(a, &l, 1);
694 if (!vq->used || l != s) {
696 goto fail_alloc_used;
699 vq->ring_size = s = l = virtio_queue_get_ring_size(vdev, idx);
700 vq->ring_phys = a = virtio_queue_get_ring_addr(vdev, idx);
701 vq->ring = cpu_physical_memory_map(a, &l, 1);
702 if (!vq->ring || l != s) {
704 goto fail_alloc_ring;
707 r = vhost_virtqueue_set_addr(dev, vq, vhost_vq_index, dev->log_enabled);
713 file.fd = event_notifier_get_fd(virtio_queue_get_host_notifier(vvq));
714 r = dev->vhost_ops->vhost_call(dev, VHOST_SET_VRING_KICK, &file);
720 /* Clear and discard previous events if any. */
721 event_notifier_test_and_clear(&vq->masked_notifier);
727 cpu_physical_memory_unmap(vq->ring, virtio_queue_get_ring_size(vdev, idx),
730 cpu_physical_memory_unmap(vq->used, virtio_queue_get_used_size(vdev, idx),
733 cpu_physical_memory_unmap(vq->avail, virtio_queue_get_avail_size(vdev, idx),
736 cpu_physical_memory_unmap(vq->desc, virtio_queue_get_desc_size(vdev, idx),
742 static void vhost_virtqueue_stop(struct vhost_dev *dev,
743 struct VirtIODevice *vdev,
744 struct vhost_virtqueue *vq,
747 struct vhost_vring_state state = {
748 .index = idx - dev->vq_index
751 assert(idx >= dev->vq_index && idx < dev->vq_index + dev->nvqs);
752 r = dev->vhost_ops->vhost_call(dev, VHOST_GET_VRING_BASE, &state);
754 fprintf(stderr, "vhost VQ %d ring restore failed: %d\n", idx, r);
757 virtio_queue_set_last_avail_idx(vdev, idx, state.num);
758 virtio_queue_invalidate_signalled_used(vdev, idx);
760 cpu_physical_memory_unmap(vq->ring, virtio_queue_get_ring_size(vdev, idx),
761 0, virtio_queue_get_ring_size(vdev, idx));
762 cpu_physical_memory_unmap(vq->used, virtio_queue_get_used_size(vdev, idx),
763 1, virtio_queue_get_used_size(vdev, idx));
764 cpu_physical_memory_unmap(vq->avail, virtio_queue_get_avail_size(vdev, idx),
765 0, virtio_queue_get_avail_size(vdev, idx));
766 cpu_physical_memory_unmap(vq->desc, virtio_queue_get_desc_size(vdev, idx),
767 0, virtio_queue_get_desc_size(vdev, idx));
770 static void vhost_eventfd_add(MemoryListener *listener,
771 MemoryRegionSection *section,
772 bool match_data, uint64_t data, EventNotifier *e)
776 static void vhost_eventfd_del(MemoryListener *listener,
777 MemoryRegionSection *section,
778 bool match_data, uint64_t data, EventNotifier *e)
782 static int vhost_virtqueue_init(struct vhost_dev *dev,
783 struct vhost_virtqueue *vq, int n)
785 struct vhost_vring_file file = {
788 int r = event_notifier_init(&vq->masked_notifier, 0);
793 file.fd = event_notifier_get_fd(&vq->masked_notifier);
794 r = dev->vhost_ops->vhost_call(dev, VHOST_SET_VRING_CALL, &file);
801 event_notifier_cleanup(&vq->masked_notifier);
805 static void vhost_virtqueue_cleanup(struct vhost_virtqueue *vq)
807 event_notifier_cleanup(&vq->masked_notifier);
810 int vhost_dev_init(struct vhost_dev *hdev, void *opaque,
811 VhostBackendType backend_type, bool force)
816 if (vhost_set_backend_type(hdev, backend_type) < 0) {
820 if (hdev->vhost_ops->vhost_backend_init(hdev, opaque) < 0) {
824 r = hdev->vhost_ops->vhost_call(hdev, VHOST_SET_OWNER, NULL);
829 r = hdev->vhost_ops->vhost_call(hdev, VHOST_GET_FEATURES, &features);
834 for (i = 0; i < hdev->nvqs; ++i) {
835 r = vhost_virtqueue_init(hdev, hdev->vqs + i, i);
840 hdev->features = features;
842 hdev->memory_listener = (MemoryListener) {
843 .begin = vhost_begin,
844 .commit = vhost_commit,
845 .region_add = vhost_region_add,
846 .region_del = vhost_region_del,
847 .region_nop = vhost_region_nop,
848 .log_start = vhost_log_start,
849 .log_stop = vhost_log_stop,
850 .log_sync = vhost_log_sync,
851 .log_global_start = vhost_log_global_start,
852 .log_global_stop = vhost_log_global_stop,
853 .eventfd_add = vhost_eventfd_add,
854 .eventfd_del = vhost_eventfd_del,
857 hdev->mem = g_malloc0(offsetof(struct vhost_memory, regions));
858 hdev->n_mem_sections = 0;
859 hdev->mem_sections = NULL;
862 hdev->log_enabled = false;
863 hdev->started = false;
864 hdev->memory_changed = false;
865 memory_listener_register(&hdev->memory_listener, &address_space_memory);
870 vhost_virtqueue_cleanup(hdev->vqs + i);
874 hdev->vhost_ops->vhost_backend_cleanup(hdev);
878 void vhost_dev_cleanup(struct vhost_dev *hdev)
881 for (i = 0; i < hdev->nvqs; ++i) {
882 vhost_virtqueue_cleanup(hdev->vqs + i);
884 memory_listener_unregister(&hdev->memory_listener);
886 g_free(hdev->mem_sections);
887 hdev->vhost_ops->vhost_backend_cleanup(hdev);
890 bool vhost_dev_query(struct vhost_dev *hdev, VirtIODevice *vdev)
892 BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(vdev)));
893 VirtioBusState *vbus = VIRTIO_BUS(qbus);
894 VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(vbus);
896 return !k->query_guest_notifiers ||
897 k->query_guest_notifiers(qbus->parent) ||
901 /* Stop processing guest IO notifications in qemu.
902 * Start processing them in vhost in kernel.
904 int vhost_dev_enable_notifiers(struct vhost_dev *hdev, VirtIODevice *vdev)
906 BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(vdev)));
907 VirtioBusState *vbus = VIRTIO_BUS(qbus);
908 VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(vbus);
910 if (!k->set_host_notifier) {
911 fprintf(stderr, "binding does not support host notifiers\n");
916 for (i = 0; i < hdev->nvqs; ++i) {
917 r = k->set_host_notifier(qbus->parent, hdev->vq_index + i, true);
919 fprintf(stderr, "vhost VQ %d notifier binding failed: %d\n", i, -r);
927 r = k->set_host_notifier(qbus->parent, hdev->vq_index + i, false);
929 fprintf(stderr, "vhost VQ %d notifier cleanup error: %d\n", i, -r);
938 /* Stop processing guest IO notifications in vhost.
939 * Start processing them in qemu.
940 * This might actually run the qemu handlers right away,
941 * so virtio in qemu must be completely setup when this is called.
943 void vhost_dev_disable_notifiers(struct vhost_dev *hdev, VirtIODevice *vdev)
945 BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(vdev)));
946 VirtioBusState *vbus = VIRTIO_BUS(qbus);
947 VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(vbus);
950 for (i = 0; i < hdev->nvqs; ++i) {
951 r = k->set_host_notifier(qbus->parent, hdev->vq_index + i, false);
953 fprintf(stderr, "vhost VQ %d notifier cleanup failed: %d\n", i, -r);
960 /* Test and clear event pending status.
961 * Should be called after unmask to avoid losing events.
963 bool vhost_virtqueue_pending(struct vhost_dev *hdev, int n)
965 struct vhost_virtqueue *vq = hdev->vqs + n - hdev->vq_index;
966 assert(hdev->started);
967 assert(n >= hdev->vq_index && n < hdev->vq_index + hdev->nvqs);
968 return event_notifier_test_and_clear(&vq->masked_notifier);
971 /* Mask/unmask events from this vq. */
972 void vhost_virtqueue_mask(struct vhost_dev *hdev, VirtIODevice *vdev, int n,
975 struct VirtQueue *vvq = virtio_get_queue(vdev, n);
976 int r, index = n - hdev->vq_index;
978 assert(hdev->started);
979 assert(n >= hdev->vq_index && n < hdev->vq_index + hdev->nvqs);
981 struct vhost_vring_file file = {
985 file.fd = event_notifier_get_fd(&hdev->vqs[index].masked_notifier);
987 file.fd = event_notifier_get_fd(virtio_queue_get_guest_notifier(vvq));
989 r = hdev->vhost_ops->vhost_call(hdev, VHOST_SET_VRING_CALL, &file);
993 unsigned vhost_get_features(struct vhost_dev *hdev, const int *feature_bits,
996 const int *bit = feature_bits;
997 while (*bit != VHOST_INVALID_FEATURE_BIT) {
998 unsigned bit_mask = (1 << *bit);
999 if (!(hdev->features & bit_mask)) {
1000 features &= ~bit_mask;
1007 void vhost_ack_features(struct vhost_dev *hdev, const int *feature_bits,
1010 const int *bit = feature_bits;
1011 while (*bit != VHOST_INVALID_FEATURE_BIT) {
1012 unsigned bit_mask = (1 << *bit);
1013 if (features & bit_mask) {
1014 hdev->acked_features |= bit_mask;
1020 /* Host notifiers must be enabled at this point. */
1021 int vhost_dev_start(struct vhost_dev *hdev, VirtIODevice *vdev)
1025 hdev->started = true;
1027 r = vhost_dev_set_features(hdev, hdev->log_enabled);
1031 r = hdev->vhost_ops->vhost_call(hdev, VHOST_SET_MEM_TABLE, hdev->mem);
1036 for (i = 0; i < hdev->nvqs; ++i) {
1037 r = vhost_virtqueue_start(hdev,
1040 hdev->vq_index + i);
1046 if (hdev->log_enabled) {
1047 hdev->log_size = vhost_get_log_size(hdev);
1048 hdev->log = hdev->log_size ?
1049 g_malloc0(hdev->log_size * sizeof *hdev->log) : NULL;
1050 r = hdev->vhost_ops->vhost_call(hdev, VHOST_SET_LOG_BASE, hdev->log);
1061 vhost_virtqueue_stop(hdev,
1064 hdev->vq_index + i);
1070 hdev->started = false;
1074 /* Host notifiers must be enabled at this point. */
1075 void vhost_dev_stop(struct vhost_dev *hdev, VirtIODevice *vdev)
1079 for (i = 0; i < hdev->nvqs; ++i) {
1080 vhost_virtqueue_stop(hdev,
1083 hdev->vq_index + i);
1085 vhost_log_sync_range(hdev, 0, ~0x0ull);
1087 hdev->started = false;