s390: Drop set_bit usage in virtio_ccw.
[qemu.git] / hw / vhost.c
blobcee8aad4a140a0b5b60e5775692754824223ed7c
1 /*
2 * vhost support
4 * Copyright Red Hat, Inc. 2010
6 * Authors:
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 <sys/ioctl.h>
17 #include "vhost.h"
18 #include "hw/hw.h"
19 #include "qemu/range.h"
20 #include <linux/vhost.h>
21 #include "exec/address-spaces.h"
23 static void vhost_dev_sync_region(struct vhost_dev *dev,
24 MemoryRegionSection *section,
25 uint64_t mfirst, uint64_t mlast,
26 uint64_t rfirst, uint64_t rlast)
28 uint64_t start = MAX(mfirst, rfirst);
29 uint64_t end = MIN(mlast, rlast);
30 vhost_log_chunk_t *from = dev->log + start / VHOST_LOG_CHUNK;
31 vhost_log_chunk_t *to = dev->log + end / VHOST_LOG_CHUNK + 1;
32 uint64_t addr = (start / VHOST_LOG_CHUNK) * VHOST_LOG_CHUNK;
34 if (end < start) {
35 return;
37 assert(end / VHOST_LOG_CHUNK < dev->log_size);
38 assert(start / VHOST_LOG_CHUNK < dev->log_size);
40 for (;from < to; ++from) {
41 vhost_log_chunk_t log;
42 int bit;
43 /* We first check with non-atomic: much cheaper,
44 * and we expect non-dirty to be the common case. */
45 if (!*from) {
46 addr += VHOST_LOG_CHUNK;
47 continue;
49 /* Data must be read atomically. We don't really
50 * need the barrier semantics of __sync
51 * builtins, but it's easier to use them than
52 * roll our own. */
53 log = __sync_fetch_and_and(from, 0);
54 while ((bit = sizeof(log) > sizeof(int) ?
55 ffsll(log) : ffs(log))) {
56 ram_addr_t ram_addr;
57 bit -= 1;
58 ram_addr = section->offset_within_region + bit * VHOST_LOG_PAGE;
59 memory_region_set_dirty(section->mr, ram_addr, VHOST_LOG_PAGE);
60 log &= ~(0x1ull << bit);
62 addr += VHOST_LOG_CHUNK;
66 static int vhost_sync_dirty_bitmap(struct vhost_dev *dev,
67 MemoryRegionSection *section,
68 hwaddr start_addr,
69 hwaddr end_addr)
71 int i;
73 if (!dev->log_enabled || !dev->started) {
74 return 0;
76 for (i = 0; i < dev->mem->nregions; ++i) {
77 struct vhost_memory_region *reg = dev->mem->regions + i;
78 vhost_dev_sync_region(dev, section, start_addr, end_addr,
79 reg->guest_phys_addr,
80 range_get_last(reg->guest_phys_addr,
81 reg->memory_size));
83 for (i = 0; i < dev->nvqs; ++i) {
84 struct vhost_virtqueue *vq = dev->vqs + i;
85 vhost_dev_sync_region(dev, section, start_addr, end_addr, vq->used_phys,
86 range_get_last(vq->used_phys, vq->used_size));
88 return 0;
91 static void vhost_log_sync(MemoryListener *listener,
92 MemoryRegionSection *section)
94 struct vhost_dev *dev = container_of(listener, struct vhost_dev,
95 memory_listener);
96 hwaddr start_addr = section->offset_within_address_space;
97 hwaddr end_addr = start_addr + section->size;
99 vhost_sync_dirty_bitmap(dev, section, start_addr, end_addr);
102 /* Assign/unassign. Keep an unsorted array of non-overlapping
103 * memory regions in dev->mem. */
104 static void vhost_dev_unassign_memory(struct vhost_dev *dev,
105 uint64_t start_addr,
106 uint64_t size)
108 int from, to, n = dev->mem->nregions;
109 /* Track overlapping/split regions for sanity checking. */
110 int overlap_start = 0, overlap_end = 0, overlap_middle = 0, split = 0;
112 for (from = 0, to = 0; from < n; ++from, ++to) {
113 struct vhost_memory_region *reg = dev->mem->regions + to;
114 uint64_t reglast;
115 uint64_t memlast;
116 uint64_t change;
118 /* clone old region */
119 if (to != from) {
120 memcpy(reg, dev->mem->regions + from, sizeof *reg);
123 /* No overlap is simple */
124 if (!ranges_overlap(reg->guest_phys_addr, reg->memory_size,
125 start_addr, size)) {
126 continue;
129 /* Split only happens if supplied region
130 * is in the middle of an existing one. Thus it can not
131 * overlap with any other existing region. */
132 assert(!split);
134 reglast = range_get_last(reg->guest_phys_addr, reg->memory_size);
135 memlast = range_get_last(start_addr, size);
137 /* Remove whole region */
138 if (start_addr <= reg->guest_phys_addr && memlast >= reglast) {
139 --dev->mem->nregions;
140 --to;
141 ++overlap_middle;
142 continue;
145 /* Shrink region */
146 if (memlast >= reglast) {
147 reg->memory_size = start_addr - reg->guest_phys_addr;
148 assert(reg->memory_size);
149 assert(!overlap_end);
150 ++overlap_end;
151 continue;
154 /* Shift region */
155 if (start_addr <= reg->guest_phys_addr) {
156 change = memlast + 1 - reg->guest_phys_addr;
157 reg->memory_size -= change;
158 reg->guest_phys_addr += change;
159 reg->userspace_addr += change;
160 assert(reg->memory_size);
161 assert(!overlap_start);
162 ++overlap_start;
163 continue;
166 /* This only happens if supplied region
167 * is in the middle of an existing one. Thus it can not
168 * overlap with any other existing region. */
169 assert(!overlap_start);
170 assert(!overlap_end);
171 assert(!overlap_middle);
172 /* Split region: shrink first part, shift second part. */
173 memcpy(dev->mem->regions + n, reg, sizeof *reg);
174 reg->memory_size = start_addr - reg->guest_phys_addr;
175 assert(reg->memory_size);
176 change = memlast + 1 - reg->guest_phys_addr;
177 reg = dev->mem->regions + n;
178 reg->memory_size -= change;
179 assert(reg->memory_size);
180 reg->guest_phys_addr += change;
181 reg->userspace_addr += change;
182 /* Never add more than 1 region */
183 assert(dev->mem->nregions == n);
184 ++dev->mem->nregions;
185 ++split;
189 /* Called after unassign, so no regions overlap the given range. */
190 static void vhost_dev_assign_memory(struct vhost_dev *dev,
191 uint64_t start_addr,
192 uint64_t size,
193 uint64_t uaddr)
195 int from, to;
196 struct vhost_memory_region *merged = NULL;
197 for (from = 0, to = 0; from < dev->mem->nregions; ++from, ++to) {
198 struct vhost_memory_region *reg = dev->mem->regions + to;
199 uint64_t prlast, urlast;
200 uint64_t pmlast, umlast;
201 uint64_t s, e, u;
203 /* clone old region */
204 if (to != from) {
205 memcpy(reg, dev->mem->regions + from, sizeof *reg);
207 prlast = range_get_last(reg->guest_phys_addr, reg->memory_size);
208 pmlast = range_get_last(start_addr, size);
209 urlast = range_get_last(reg->userspace_addr, reg->memory_size);
210 umlast = range_get_last(uaddr, size);
212 /* check for overlapping regions: should never happen. */
213 assert(prlast < start_addr || pmlast < reg->guest_phys_addr);
214 /* Not an adjacent or overlapping region - do not merge. */
215 if ((prlast + 1 != start_addr || urlast + 1 != uaddr) &&
216 (pmlast + 1 != reg->guest_phys_addr ||
217 umlast + 1 != reg->userspace_addr)) {
218 continue;
221 if (merged) {
222 --to;
223 assert(to >= 0);
224 } else {
225 merged = reg;
227 u = MIN(uaddr, reg->userspace_addr);
228 s = MIN(start_addr, reg->guest_phys_addr);
229 e = MAX(pmlast, prlast);
230 uaddr = merged->userspace_addr = u;
231 start_addr = merged->guest_phys_addr = s;
232 size = merged->memory_size = e - s + 1;
233 assert(merged->memory_size);
236 if (!merged) {
237 struct vhost_memory_region *reg = dev->mem->regions + to;
238 memset(reg, 0, sizeof *reg);
239 reg->memory_size = size;
240 assert(reg->memory_size);
241 reg->guest_phys_addr = start_addr;
242 reg->userspace_addr = uaddr;
243 ++to;
245 assert(to <= dev->mem->nregions + 1);
246 dev->mem->nregions = to;
249 static uint64_t vhost_get_log_size(struct vhost_dev *dev)
251 uint64_t log_size = 0;
252 int i;
253 for (i = 0; i < dev->mem->nregions; ++i) {
254 struct vhost_memory_region *reg = dev->mem->regions + i;
255 uint64_t last = range_get_last(reg->guest_phys_addr,
256 reg->memory_size);
257 log_size = MAX(log_size, last / VHOST_LOG_CHUNK + 1);
259 for (i = 0; i < dev->nvqs; ++i) {
260 struct vhost_virtqueue *vq = dev->vqs + i;
261 uint64_t last = vq->used_phys + vq->used_size - 1;
262 log_size = MAX(log_size, last / VHOST_LOG_CHUNK + 1);
264 return log_size;
267 static inline void vhost_dev_log_resize(struct vhost_dev* dev, uint64_t size)
269 vhost_log_chunk_t *log;
270 uint64_t log_base;
271 int r, i;
272 if (size) {
273 log = g_malloc0(size * sizeof *log);
274 } else {
275 log = NULL;
277 log_base = (uint64_t)(unsigned long)log;
278 r = ioctl(dev->control, VHOST_SET_LOG_BASE, &log_base);
279 assert(r >= 0);
280 for (i = 0; i < dev->n_mem_sections; ++i) {
281 /* Sync only the range covered by the old log */
282 vhost_sync_dirty_bitmap(dev, &dev->mem_sections[i], 0,
283 dev->log_size * VHOST_LOG_CHUNK - 1);
285 if (dev->log) {
286 g_free(dev->log);
288 dev->log = log;
289 dev->log_size = size;
292 static int vhost_verify_ring_mappings(struct vhost_dev *dev,
293 uint64_t start_addr,
294 uint64_t size)
296 int i;
297 for (i = 0; i < dev->nvqs; ++i) {
298 struct vhost_virtqueue *vq = dev->vqs + i;
299 hwaddr l;
300 void *p;
302 if (!ranges_overlap(start_addr, size, vq->ring_phys, vq->ring_size)) {
303 continue;
305 l = vq->ring_size;
306 p = cpu_physical_memory_map(vq->ring_phys, &l, 1);
307 if (!p || l != vq->ring_size) {
308 fprintf(stderr, "Unable to map ring buffer for ring %d\n", i);
309 return -ENOMEM;
311 if (p != vq->ring) {
312 fprintf(stderr, "Ring buffer relocated for ring %d\n", i);
313 return -EBUSY;
315 cpu_physical_memory_unmap(p, l, 0, 0);
317 return 0;
320 static struct vhost_memory_region *vhost_dev_find_reg(struct vhost_dev *dev,
321 uint64_t start_addr,
322 uint64_t size)
324 int i, n = dev->mem->nregions;
325 for (i = 0; i < n; ++i) {
326 struct vhost_memory_region *reg = dev->mem->regions + i;
327 if (ranges_overlap(reg->guest_phys_addr, reg->memory_size,
328 start_addr, size)) {
329 return reg;
332 return NULL;
335 static bool vhost_dev_cmp_memory(struct vhost_dev *dev,
336 uint64_t start_addr,
337 uint64_t size,
338 uint64_t uaddr)
340 struct vhost_memory_region *reg = vhost_dev_find_reg(dev, start_addr, size);
341 uint64_t reglast;
342 uint64_t memlast;
344 if (!reg) {
345 return true;
348 reglast = range_get_last(reg->guest_phys_addr, reg->memory_size);
349 memlast = range_get_last(start_addr, size);
351 /* Need to extend region? */
352 if (start_addr < reg->guest_phys_addr || memlast > reglast) {
353 return true;
355 /* userspace_addr changed? */
356 return uaddr != reg->userspace_addr + start_addr - reg->guest_phys_addr;
359 static void vhost_set_memory(MemoryListener *listener,
360 MemoryRegionSection *section,
361 bool add)
363 struct vhost_dev *dev = container_of(listener, struct vhost_dev,
364 memory_listener);
365 hwaddr start_addr = section->offset_within_address_space;
366 ram_addr_t size = section->size;
367 bool log_dirty = memory_region_is_logging(section->mr);
368 int s = offsetof(struct vhost_memory, regions) +
369 (dev->mem->nregions + 1) * sizeof dev->mem->regions[0];
370 uint64_t log_size;
371 int r;
372 void *ram;
374 dev->mem = g_realloc(dev->mem, s);
376 if (log_dirty) {
377 add = false;
380 assert(size);
382 /* Optimize no-change case. At least cirrus_vga does this a lot at this time. */
383 ram = memory_region_get_ram_ptr(section->mr) + section->offset_within_region;
384 if (add) {
385 if (!vhost_dev_cmp_memory(dev, start_addr, size, (uintptr_t)ram)) {
386 /* Region exists with same address. Nothing to do. */
387 return;
389 } else {
390 if (!vhost_dev_find_reg(dev, start_addr, size)) {
391 /* Removing region that we don't access. Nothing to do. */
392 return;
396 vhost_dev_unassign_memory(dev, start_addr, size);
397 if (add) {
398 /* Add given mapping, merging adjacent regions if any */
399 vhost_dev_assign_memory(dev, start_addr, size, (uintptr_t)ram);
400 } else {
401 /* Remove old mapping for this memory, if any. */
402 vhost_dev_unassign_memory(dev, start_addr, size);
405 if (!dev->started) {
406 return;
409 if (dev->started) {
410 r = vhost_verify_ring_mappings(dev, start_addr, size);
411 assert(r >= 0);
414 if (!dev->log_enabled) {
415 r = ioctl(dev->control, VHOST_SET_MEM_TABLE, dev->mem);
416 assert(r >= 0);
417 return;
419 log_size = vhost_get_log_size(dev);
420 /* We allocate an extra 4K bytes to log,
421 * to reduce the * number of reallocations. */
422 #define VHOST_LOG_BUFFER (0x1000 / sizeof *dev->log)
423 /* To log more, must increase log size before table update. */
424 if (dev->log_size < log_size) {
425 vhost_dev_log_resize(dev, log_size + VHOST_LOG_BUFFER);
427 r = ioctl(dev->control, VHOST_SET_MEM_TABLE, dev->mem);
428 assert(r >= 0);
429 /* To log less, can only decrease log size after table update. */
430 if (dev->log_size > log_size + VHOST_LOG_BUFFER) {
431 vhost_dev_log_resize(dev, log_size);
435 static bool vhost_section(MemoryRegionSection *section)
437 return memory_region_is_ram(section->mr);
440 static void vhost_begin(MemoryListener *listener)
444 static void vhost_commit(MemoryListener *listener)
448 static void vhost_region_add(MemoryListener *listener,
449 MemoryRegionSection *section)
451 struct vhost_dev *dev = container_of(listener, struct vhost_dev,
452 memory_listener);
454 if (!vhost_section(section)) {
455 return;
458 ++dev->n_mem_sections;
459 dev->mem_sections = g_renew(MemoryRegionSection, dev->mem_sections,
460 dev->n_mem_sections);
461 dev->mem_sections[dev->n_mem_sections - 1] = *section;
462 vhost_set_memory(listener, section, true);
465 static void vhost_region_del(MemoryListener *listener,
466 MemoryRegionSection *section)
468 struct vhost_dev *dev = container_of(listener, struct vhost_dev,
469 memory_listener);
470 int i;
472 if (!vhost_section(section)) {
473 return;
476 vhost_set_memory(listener, section, false);
477 for (i = 0; i < dev->n_mem_sections; ++i) {
478 if (dev->mem_sections[i].offset_within_address_space
479 == section->offset_within_address_space) {
480 --dev->n_mem_sections;
481 memmove(&dev->mem_sections[i], &dev->mem_sections[i+1],
482 (dev->n_mem_sections - i) * sizeof(*dev->mem_sections));
483 break;
488 static void vhost_region_nop(MemoryListener *listener,
489 MemoryRegionSection *section)
493 static int vhost_virtqueue_set_addr(struct vhost_dev *dev,
494 struct vhost_virtqueue *vq,
495 unsigned idx, bool enable_log)
497 struct vhost_vring_addr addr = {
498 .index = idx,
499 .desc_user_addr = (uint64_t)(unsigned long)vq->desc,
500 .avail_user_addr = (uint64_t)(unsigned long)vq->avail,
501 .used_user_addr = (uint64_t)(unsigned long)vq->used,
502 .log_guest_addr = vq->used_phys,
503 .flags = enable_log ? (1 << VHOST_VRING_F_LOG) : 0,
505 int r = ioctl(dev->control, VHOST_SET_VRING_ADDR, &addr);
506 if (r < 0) {
507 return -errno;
509 return 0;
512 static int vhost_dev_set_features(struct vhost_dev *dev, bool enable_log)
514 uint64_t features = dev->acked_features;
515 int r;
516 if (enable_log) {
517 features |= 0x1 << VHOST_F_LOG_ALL;
519 r = ioctl(dev->control, VHOST_SET_FEATURES, &features);
520 return r < 0 ? -errno : 0;
523 static int vhost_dev_set_log(struct vhost_dev *dev, bool enable_log)
525 int r, t, i;
526 r = vhost_dev_set_features(dev, enable_log);
527 if (r < 0) {
528 goto err_features;
530 for (i = 0; i < dev->nvqs; ++i) {
531 r = vhost_virtqueue_set_addr(dev, dev->vqs + i, i,
532 enable_log);
533 if (r < 0) {
534 goto err_vq;
537 return 0;
538 err_vq:
539 for (; i >= 0; --i) {
540 t = vhost_virtqueue_set_addr(dev, dev->vqs + i, i,
541 dev->log_enabled);
542 assert(t >= 0);
544 t = vhost_dev_set_features(dev, dev->log_enabled);
545 assert(t >= 0);
546 err_features:
547 return r;
550 static int vhost_migration_log(MemoryListener *listener, int enable)
552 struct vhost_dev *dev = container_of(listener, struct vhost_dev,
553 memory_listener);
554 int r;
555 if (!!enable == dev->log_enabled) {
556 return 0;
558 if (!dev->started) {
559 dev->log_enabled = enable;
560 return 0;
562 if (!enable) {
563 r = vhost_dev_set_log(dev, false);
564 if (r < 0) {
565 return r;
567 if (dev->log) {
568 g_free(dev->log);
570 dev->log = NULL;
571 dev->log_size = 0;
572 } else {
573 vhost_dev_log_resize(dev, vhost_get_log_size(dev));
574 r = vhost_dev_set_log(dev, true);
575 if (r < 0) {
576 return r;
579 dev->log_enabled = enable;
580 return 0;
583 static void vhost_log_global_start(MemoryListener *listener)
585 int r;
587 r = vhost_migration_log(listener, true);
588 if (r < 0) {
589 abort();
593 static void vhost_log_global_stop(MemoryListener *listener)
595 int r;
597 r = vhost_migration_log(listener, false);
598 if (r < 0) {
599 abort();
603 static void vhost_log_start(MemoryListener *listener,
604 MemoryRegionSection *section)
606 /* FIXME: implement */
609 static void vhost_log_stop(MemoryListener *listener,
610 MemoryRegionSection *section)
612 /* FIXME: implement */
615 static int vhost_virtqueue_start(struct vhost_dev *dev,
616 struct VirtIODevice *vdev,
617 struct vhost_virtqueue *vq,
618 unsigned idx)
620 hwaddr s, l, a;
621 int r;
622 struct vhost_vring_file file = {
623 .index = idx,
625 struct vhost_vring_state state = {
626 .index = idx,
628 struct VirtQueue *vvq = virtio_get_queue(vdev, idx);
630 vq->num = state.num = virtio_queue_get_num(vdev, idx);
631 r = ioctl(dev->control, VHOST_SET_VRING_NUM, &state);
632 if (r) {
633 return -errno;
636 state.num = virtio_queue_get_last_avail_idx(vdev, idx);
637 r = ioctl(dev->control, VHOST_SET_VRING_BASE, &state);
638 if (r) {
639 return -errno;
642 s = l = virtio_queue_get_desc_size(vdev, idx);
643 a = virtio_queue_get_desc_addr(vdev, idx);
644 vq->desc = cpu_physical_memory_map(a, &l, 0);
645 if (!vq->desc || l != s) {
646 r = -ENOMEM;
647 goto fail_alloc_desc;
649 s = l = virtio_queue_get_avail_size(vdev, idx);
650 a = virtio_queue_get_avail_addr(vdev, idx);
651 vq->avail = cpu_physical_memory_map(a, &l, 0);
652 if (!vq->avail || l != s) {
653 r = -ENOMEM;
654 goto fail_alloc_avail;
656 vq->used_size = s = l = virtio_queue_get_used_size(vdev, idx);
657 vq->used_phys = a = virtio_queue_get_used_addr(vdev, idx);
658 vq->used = cpu_physical_memory_map(a, &l, 1);
659 if (!vq->used || l != s) {
660 r = -ENOMEM;
661 goto fail_alloc_used;
664 vq->ring_size = s = l = virtio_queue_get_ring_size(vdev, idx);
665 vq->ring_phys = a = virtio_queue_get_ring_addr(vdev, idx);
666 vq->ring = cpu_physical_memory_map(a, &l, 1);
667 if (!vq->ring || l != s) {
668 r = -ENOMEM;
669 goto fail_alloc_ring;
672 r = vhost_virtqueue_set_addr(dev, vq, idx, dev->log_enabled);
673 if (r < 0) {
674 r = -errno;
675 goto fail_alloc;
677 file.fd = event_notifier_get_fd(virtio_queue_get_host_notifier(vvq));
678 r = ioctl(dev->control, VHOST_SET_VRING_KICK, &file);
679 if (r) {
680 r = -errno;
681 goto fail_kick;
684 /* Clear and discard previous events if any. */
685 event_notifier_test_and_clear(&vq->masked_notifier);
687 return 0;
689 fail_kick:
690 fail_alloc:
691 cpu_physical_memory_unmap(vq->ring, virtio_queue_get_ring_size(vdev, idx),
692 0, 0);
693 fail_alloc_ring:
694 cpu_physical_memory_unmap(vq->used, virtio_queue_get_used_size(vdev, idx),
695 0, 0);
696 fail_alloc_used:
697 cpu_physical_memory_unmap(vq->avail, virtio_queue_get_avail_size(vdev, idx),
698 0, 0);
699 fail_alloc_avail:
700 cpu_physical_memory_unmap(vq->desc, virtio_queue_get_desc_size(vdev, idx),
701 0, 0);
702 fail_alloc_desc:
703 return r;
706 static void vhost_virtqueue_stop(struct vhost_dev *dev,
707 struct VirtIODevice *vdev,
708 struct vhost_virtqueue *vq,
709 unsigned idx)
711 struct vhost_vring_state state = {
712 .index = idx,
714 int r;
715 r = ioctl(dev->control, VHOST_GET_VRING_BASE, &state);
716 if (r < 0) {
717 fprintf(stderr, "vhost VQ %d ring restore failed: %d\n", idx, r);
718 fflush(stderr);
720 virtio_queue_set_last_avail_idx(vdev, idx, state.num);
721 assert (r >= 0);
722 cpu_physical_memory_unmap(vq->ring, virtio_queue_get_ring_size(vdev, idx),
723 0, virtio_queue_get_ring_size(vdev, idx));
724 cpu_physical_memory_unmap(vq->used, virtio_queue_get_used_size(vdev, idx),
725 1, virtio_queue_get_used_size(vdev, idx));
726 cpu_physical_memory_unmap(vq->avail, virtio_queue_get_avail_size(vdev, idx),
727 0, virtio_queue_get_avail_size(vdev, idx));
728 cpu_physical_memory_unmap(vq->desc, virtio_queue_get_desc_size(vdev, idx),
729 0, virtio_queue_get_desc_size(vdev, idx));
732 static void vhost_eventfd_add(MemoryListener *listener,
733 MemoryRegionSection *section,
734 bool match_data, uint64_t data, EventNotifier *e)
738 static void vhost_eventfd_del(MemoryListener *listener,
739 MemoryRegionSection *section,
740 bool match_data, uint64_t data, EventNotifier *e)
744 static int vhost_virtqueue_init(struct vhost_dev *dev,
745 struct vhost_virtqueue *vq, int n)
747 struct vhost_vring_file file = {
748 .index = n,
750 int r = event_notifier_init(&vq->masked_notifier, 0);
751 if (r < 0) {
752 return r;
755 file.fd = event_notifier_get_fd(&vq->masked_notifier);
756 r = ioctl(dev->control, VHOST_SET_VRING_CALL, &file);
757 if (r) {
758 r = -errno;
759 goto fail_call;
761 return 0;
762 fail_call:
763 event_notifier_cleanup(&vq->masked_notifier);
764 return r;
767 static void vhost_virtqueue_cleanup(struct vhost_virtqueue *vq)
769 event_notifier_cleanup(&vq->masked_notifier);
772 int vhost_dev_init(struct vhost_dev *hdev, int devfd, const char *devpath,
773 bool force)
775 uint64_t features;
776 int i, r;
777 if (devfd >= 0) {
778 hdev->control = devfd;
779 } else {
780 hdev->control = open(devpath, O_RDWR);
781 if (hdev->control < 0) {
782 return -errno;
785 r = ioctl(hdev->control, VHOST_SET_OWNER, NULL);
786 if (r < 0) {
787 goto fail;
790 r = ioctl(hdev->control, VHOST_GET_FEATURES, &features);
791 if (r < 0) {
792 goto fail;
795 for (i = 0; i < hdev->nvqs; ++i) {
796 r = vhost_virtqueue_init(hdev, hdev->vqs + i, i);
797 if (r < 0) {
798 goto fail_vq;
801 hdev->features = features;
803 hdev->memory_listener = (MemoryListener) {
804 .begin = vhost_begin,
805 .commit = vhost_commit,
806 .region_add = vhost_region_add,
807 .region_del = vhost_region_del,
808 .region_nop = vhost_region_nop,
809 .log_start = vhost_log_start,
810 .log_stop = vhost_log_stop,
811 .log_sync = vhost_log_sync,
812 .log_global_start = vhost_log_global_start,
813 .log_global_stop = vhost_log_global_stop,
814 .eventfd_add = vhost_eventfd_add,
815 .eventfd_del = vhost_eventfd_del,
816 .priority = 10
818 hdev->mem = g_malloc0(offsetof(struct vhost_memory, regions));
819 hdev->n_mem_sections = 0;
820 hdev->mem_sections = NULL;
821 hdev->log = NULL;
822 hdev->log_size = 0;
823 hdev->log_enabled = false;
824 hdev->started = false;
825 memory_listener_register(&hdev->memory_listener, &address_space_memory);
826 hdev->force = force;
827 return 0;
828 fail_vq:
829 while (--i >= 0) {
830 vhost_virtqueue_cleanup(hdev->vqs + i);
832 fail:
833 r = -errno;
834 close(hdev->control);
835 return r;
838 void vhost_dev_cleanup(struct vhost_dev *hdev)
840 int i;
841 for (i = 0; i < hdev->nvqs; ++i) {
842 vhost_virtqueue_cleanup(hdev->vqs + i);
844 memory_listener_unregister(&hdev->memory_listener);
845 g_free(hdev->mem);
846 g_free(hdev->mem_sections);
847 close(hdev->control);
850 bool vhost_dev_query(struct vhost_dev *hdev, VirtIODevice *vdev)
852 return !vdev->binding->query_guest_notifiers ||
853 vdev->binding->query_guest_notifiers(vdev->binding_opaque) ||
854 hdev->force;
857 /* Stop processing guest IO notifications in qemu.
858 * Start processing them in vhost in kernel.
860 int vhost_dev_enable_notifiers(struct vhost_dev *hdev, VirtIODevice *vdev)
862 int i, r;
863 if (!vdev->binding->set_host_notifier) {
864 fprintf(stderr, "binding does not support host notifiers\n");
865 r = -ENOSYS;
866 goto fail;
869 for (i = 0; i < hdev->nvqs; ++i) {
870 r = vdev->binding->set_host_notifier(vdev->binding_opaque, i, true);
871 if (r < 0) {
872 fprintf(stderr, "vhost VQ %d notifier binding failed: %d\n", i, -r);
873 goto fail_vq;
877 return 0;
878 fail_vq:
879 while (--i >= 0) {
880 r = vdev->binding->set_host_notifier(vdev->binding_opaque, i, false);
881 if (r < 0) {
882 fprintf(stderr, "vhost VQ %d notifier cleanup error: %d\n", i, -r);
883 fflush(stderr);
885 assert (r >= 0);
887 fail:
888 return r;
891 /* Stop processing guest IO notifications in vhost.
892 * Start processing them in qemu.
893 * This might actually run the qemu handlers right away,
894 * so virtio in qemu must be completely setup when this is called.
896 void vhost_dev_disable_notifiers(struct vhost_dev *hdev, VirtIODevice *vdev)
898 int i, r;
900 for (i = 0; i < hdev->nvqs; ++i) {
901 r = vdev->binding->set_host_notifier(vdev->binding_opaque, i, false);
902 if (r < 0) {
903 fprintf(stderr, "vhost VQ %d notifier cleanup failed: %d\n", i, -r);
904 fflush(stderr);
906 assert (r >= 0);
910 /* Test and clear event pending status.
911 * Should be called after unmask to avoid losing events.
913 bool vhost_virtqueue_pending(struct vhost_dev *hdev, int n)
915 struct vhost_virtqueue *vq = hdev->vqs + n;
916 assert(hdev->started);
917 return event_notifier_test_and_clear(&vq->masked_notifier);
920 /* Mask/unmask events from this vq. */
921 void vhost_virtqueue_mask(struct vhost_dev *hdev, VirtIODevice *vdev, int n,
922 bool mask)
924 struct VirtQueue *vvq = virtio_get_queue(vdev, n);
925 int r;
927 assert(hdev->started);
929 struct vhost_vring_file file = {
930 .index = n,
932 if (mask) {
933 file.fd = event_notifier_get_fd(&hdev->vqs[n].masked_notifier);
934 } else {
935 file.fd = event_notifier_get_fd(virtio_queue_get_guest_notifier(vvq));
937 r = ioctl(hdev->control, VHOST_SET_VRING_CALL, &file);
938 assert(r >= 0);
941 /* Host notifiers must be enabled at this point. */
942 int vhost_dev_start(struct vhost_dev *hdev, VirtIODevice *vdev)
944 int i, r;
946 hdev->started = true;
948 if (!vdev->binding->set_guest_notifiers) {
949 fprintf(stderr, "binding does not support guest notifiers\n");
950 r = -ENOSYS;
951 goto fail;
954 r = vdev->binding->set_guest_notifiers(vdev->binding_opaque,
955 hdev->nvqs,
956 true);
957 if (r < 0) {
958 fprintf(stderr, "Error binding guest notifier: %d\n", -r);
959 goto fail_notifiers;
962 r = vhost_dev_set_features(hdev, hdev->log_enabled);
963 if (r < 0) {
964 goto fail_features;
966 r = ioctl(hdev->control, VHOST_SET_MEM_TABLE, hdev->mem);
967 if (r < 0) {
968 r = -errno;
969 goto fail_mem;
971 for (i = 0; i < hdev->nvqs; ++i) {
972 r = vhost_virtqueue_start(hdev,
973 vdev,
974 hdev->vqs + i,
976 if (r < 0) {
977 goto fail_vq;
981 if (hdev->log_enabled) {
982 hdev->log_size = vhost_get_log_size(hdev);
983 hdev->log = hdev->log_size ?
984 g_malloc0(hdev->log_size * sizeof *hdev->log) : NULL;
985 r = ioctl(hdev->control, VHOST_SET_LOG_BASE,
986 (uint64_t)(unsigned long)hdev->log);
987 if (r < 0) {
988 r = -errno;
989 goto fail_log;
993 return 0;
994 fail_log:
995 fail_vq:
996 while (--i >= 0) {
997 vhost_virtqueue_stop(hdev,
998 vdev,
999 hdev->vqs + i,
1002 fail_mem:
1003 fail_features:
1004 vdev->binding->set_guest_notifiers(vdev->binding_opaque, hdev->nvqs, false);
1005 fail_notifiers:
1006 fail:
1008 hdev->started = false;
1009 return r;
1012 /* Host notifiers must be enabled at this point. */
1013 void vhost_dev_stop(struct vhost_dev *hdev, VirtIODevice *vdev)
1015 int i, r;
1017 for (i = 0; i < hdev->nvqs; ++i) {
1018 vhost_virtqueue_stop(hdev,
1019 vdev,
1020 hdev->vqs + i,
1023 for (i = 0; i < hdev->n_mem_sections; ++i) {
1024 vhost_sync_dirty_bitmap(hdev, &hdev->mem_sections[i],
1025 0, (hwaddr)~0x0ull);
1027 r = vdev->binding->set_guest_notifiers(vdev->binding_opaque,
1028 hdev->nvqs,
1029 false);
1030 if (r < 0) {
1031 fprintf(stderr, "vhost guest notifier cleanup failed: %d\n", r);
1032 fflush(stderr);
1034 assert (r >= 0);
1036 hdev->started = false;
1037 g_free(hdev->log);
1038 hdev->log = NULL;
1039 hdev->log_size = 0;