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 <sys/ioctl.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
;
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
;
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
50 * need the barrier semantics of __sync
51 * builtins, but it's easier to use them than
53 log
= __sync_fetch_and_and(from
, 0);
54 while ((bit
= sizeof(log
) > sizeof(int) ?
55 ffsll(log
) : ffs(log
))) {
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
,
73 if (!dev
->log_enabled
|| !dev
->started
) {
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
,
80 range_get_last(reg
->guest_phys_addr
,
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
));
91 static void vhost_log_sync(MemoryListener
*listener
,
92 MemoryRegionSection
*section
)
94 struct vhost_dev
*dev
= container_of(listener
, struct vhost_dev
,
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
,
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
;
118 /* clone old region */
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
,
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. */
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
;
146 if (memlast
>= reglast
) {
147 reg
->memory_size
= start_addr
- reg
->guest_phys_addr
;
148 assert(reg
->memory_size
);
149 assert(!overlap_end
);
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
);
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
;
189 /* Called after unassign, so no regions overlap the given range. */
190 static void vhost_dev_assign_memory(struct vhost_dev
*dev
,
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
;
203 /* clone old region */
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
)) {
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
);
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
;
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;
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
,
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);
267 static inline void vhost_dev_log_resize(struct vhost_dev
* dev
, uint64_t size
)
269 vhost_log_chunk_t
*log
;
273 log
= g_malloc0(size
* sizeof *log
);
274 log_base
= (uint64_t)(unsigned long)log
;
275 r
= ioctl(dev
->control
, VHOST_SET_LOG_BASE
, &log_base
);
277 for (i
= 0; i
< dev
->n_mem_sections
; ++i
) {
278 /* Sync only the range covered by the old log */
279 vhost_sync_dirty_bitmap(dev
, &dev
->mem_sections
[i
], 0,
280 dev
->log_size
* VHOST_LOG_CHUNK
- 1);
286 dev
->log_size
= size
;
289 static int vhost_verify_ring_mappings(struct vhost_dev
*dev
,
294 for (i
= 0; i
< dev
->nvqs
; ++i
) {
295 struct vhost_virtqueue
*vq
= dev
->vqs
+ i
;
299 if (!ranges_overlap(start_addr
, size
, vq
->ring_phys
, vq
->ring_size
)) {
303 p
= cpu_physical_memory_map(vq
->ring_phys
, &l
, 1);
304 if (!p
|| l
!= vq
->ring_size
) {
305 fprintf(stderr
, "Unable to map ring buffer for ring %d\n", i
);
309 fprintf(stderr
, "Ring buffer relocated for ring %d\n", i
);
312 cpu_physical_memory_unmap(p
, l
, 0, 0);
317 static struct vhost_memory_region
*vhost_dev_find_reg(struct vhost_dev
*dev
,
321 int i
, n
= dev
->mem
->nregions
;
322 for (i
= 0; i
< n
; ++i
) {
323 struct vhost_memory_region
*reg
= dev
->mem
->regions
+ i
;
324 if (ranges_overlap(reg
->guest_phys_addr
, reg
->memory_size
,
332 static bool vhost_dev_cmp_memory(struct vhost_dev
*dev
,
337 struct vhost_memory_region
*reg
= vhost_dev_find_reg(dev
, start_addr
, size
);
345 reglast
= range_get_last(reg
->guest_phys_addr
, reg
->memory_size
);
346 memlast
= range_get_last(start_addr
, size
);
348 /* Need to extend region? */
349 if (start_addr
< reg
->guest_phys_addr
|| memlast
> reglast
) {
352 /* userspace_addr changed? */
353 return uaddr
!= reg
->userspace_addr
+ start_addr
- reg
->guest_phys_addr
;
356 static void vhost_set_memory(MemoryListener
*listener
,
357 MemoryRegionSection
*section
,
360 struct vhost_dev
*dev
= container_of(listener
, struct vhost_dev
,
362 hwaddr start_addr
= section
->offset_within_address_space
;
363 ram_addr_t size
= section
->size
;
364 bool log_dirty
= memory_region_is_logging(section
->mr
);
365 int s
= offsetof(struct vhost_memory
, regions
) +
366 (dev
->mem
->nregions
+ 1) * sizeof dev
->mem
->regions
[0];
371 dev
->mem
= g_realloc(dev
->mem
, s
);
379 /* Optimize no-change case. At least cirrus_vga does this a lot at this time. */
380 ram
= memory_region_get_ram_ptr(section
->mr
) + section
->offset_within_region
;
382 if (!vhost_dev_cmp_memory(dev
, start_addr
, size
, (uintptr_t)ram
)) {
383 /* Region exists with same address. Nothing to do. */
387 if (!vhost_dev_find_reg(dev
, start_addr
, size
)) {
388 /* Removing region that we don't access. Nothing to do. */
393 vhost_dev_unassign_memory(dev
, start_addr
, size
);
395 /* Add given mapping, merging adjacent regions if any */
396 vhost_dev_assign_memory(dev
, start_addr
, size
, (uintptr_t)ram
);
398 /* Remove old mapping for this memory, if any. */
399 vhost_dev_unassign_memory(dev
, start_addr
, size
);
407 r
= vhost_verify_ring_mappings(dev
, start_addr
, size
);
411 if (!dev
->log_enabled
) {
412 r
= ioctl(dev
->control
, VHOST_SET_MEM_TABLE
, dev
->mem
);
416 log_size
= vhost_get_log_size(dev
);
417 /* We allocate an extra 4K bytes to log,
418 * to reduce the * number of reallocations. */
419 #define VHOST_LOG_BUFFER (0x1000 / sizeof *dev->log)
420 /* To log more, must increase log size before table update. */
421 if (dev
->log_size
< log_size
) {
422 vhost_dev_log_resize(dev
, log_size
+ VHOST_LOG_BUFFER
);
424 r
= ioctl(dev
->control
, VHOST_SET_MEM_TABLE
, dev
->mem
);
426 /* To log less, can only decrease log size after table update. */
427 if (dev
->log_size
> log_size
+ VHOST_LOG_BUFFER
) {
428 vhost_dev_log_resize(dev
, log_size
);
432 static bool vhost_section(MemoryRegionSection
*section
)
434 return memory_region_is_ram(section
->mr
);
437 static void vhost_begin(MemoryListener
*listener
)
441 static void vhost_commit(MemoryListener
*listener
)
445 static void vhost_region_add(MemoryListener
*listener
,
446 MemoryRegionSection
*section
)
448 struct vhost_dev
*dev
= container_of(listener
, struct vhost_dev
,
451 if (!vhost_section(section
)) {
455 ++dev
->n_mem_sections
;
456 dev
->mem_sections
= g_renew(MemoryRegionSection
, dev
->mem_sections
,
457 dev
->n_mem_sections
);
458 dev
->mem_sections
[dev
->n_mem_sections
- 1] = *section
;
459 vhost_set_memory(listener
, section
, true);
462 static void vhost_region_del(MemoryListener
*listener
,
463 MemoryRegionSection
*section
)
465 struct vhost_dev
*dev
= container_of(listener
, struct vhost_dev
,
469 if (!vhost_section(section
)) {
473 vhost_set_memory(listener
, section
, false);
474 for (i
= 0; i
< dev
->n_mem_sections
; ++i
) {
475 if (dev
->mem_sections
[i
].offset_within_address_space
476 == section
->offset_within_address_space
) {
477 --dev
->n_mem_sections
;
478 memmove(&dev
->mem_sections
[i
], &dev
->mem_sections
[i
+1],
479 (dev
->n_mem_sections
- i
) * sizeof(*dev
->mem_sections
));
485 static void vhost_region_nop(MemoryListener
*listener
,
486 MemoryRegionSection
*section
)
490 static int vhost_virtqueue_set_addr(struct vhost_dev
*dev
,
491 struct vhost_virtqueue
*vq
,
492 unsigned idx
, bool enable_log
)
494 struct vhost_vring_addr addr
= {
496 .desc_user_addr
= (uint64_t)(unsigned long)vq
->desc
,
497 .avail_user_addr
= (uint64_t)(unsigned long)vq
->avail
,
498 .used_user_addr
= (uint64_t)(unsigned long)vq
->used
,
499 .log_guest_addr
= vq
->used_phys
,
500 .flags
= enable_log
? (1 << VHOST_VRING_F_LOG
) : 0,
502 int r
= ioctl(dev
->control
, VHOST_SET_VRING_ADDR
, &addr
);
509 static int vhost_dev_set_features(struct vhost_dev
*dev
, bool enable_log
)
511 uint64_t features
= dev
->acked_features
;
514 features
|= 0x1 << VHOST_F_LOG_ALL
;
516 r
= ioctl(dev
->control
, VHOST_SET_FEATURES
, &features
);
517 return r
< 0 ? -errno
: 0;
520 static int vhost_dev_set_log(struct vhost_dev
*dev
, bool enable_log
)
523 r
= vhost_dev_set_features(dev
, enable_log
);
527 for (i
= 0; i
< dev
->nvqs
; ++i
) {
528 r
= vhost_virtqueue_set_addr(dev
, dev
->vqs
+ i
, i
,
536 for (; i
>= 0; --i
) {
537 t
= vhost_virtqueue_set_addr(dev
, dev
->vqs
+ i
, i
,
541 t
= vhost_dev_set_features(dev
, dev
->log_enabled
);
547 static int vhost_migration_log(MemoryListener
*listener
, int enable
)
549 struct vhost_dev
*dev
= container_of(listener
, struct vhost_dev
,
552 if (!!enable
== dev
->log_enabled
) {
556 dev
->log_enabled
= enable
;
560 r
= vhost_dev_set_log(dev
, false);
570 vhost_dev_log_resize(dev
, vhost_get_log_size(dev
));
571 r
= vhost_dev_set_log(dev
, true);
576 dev
->log_enabled
= enable
;
580 static void vhost_log_global_start(MemoryListener
*listener
)
584 r
= vhost_migration_log(listener
, true);
590 static void vhost_log_global_stop(MemoryListener
*listener
)
594 r
= vhost_migration_log(listener
, false);
600 static void vhost_log_start(MemoryListener
*listener
,
601 MemoryRegionSection
*section
)
603 /* FIXME: implement */
606 static void vhost_log_stop(MemoryListener
*listener
,
607 MemoryRegionSection
*section
)
609 /* FIXME: implement */
612 static int vhost_virtqueue_start(struct vhost_dev
*dev
,
613 struct VirtIODevice
*vdev
,
614 struct vhost_virtqueue
*vq
,
619 struct vhost_vring_file file
= {
622 struct vhost_vring_state state
= {
625 struct VirtQueue
*vvq
= virtio_get_queue(vdev
, idx
);
627 vq
->num
= state
.num
= virtio_queue_get_num(vdev
, idx
);
628 r
= ioctl(dev
->control
, VHOST_SET_VRING_NUM
, &state
);
633 state
.num
= virtio_queue_get_last_avail_idx(vdev
, idx
);
634 r
= ioctl(dev
->control
, VHOST_SET_VRING_BASE
, &state
);
639 s
= l
= virtio_queue_get_desc_size(vdev
, idx
);
640 a
= virtio_queue_get_desc_addr(vdev
, idx
);
641 vq
->desc
= cpu_physical_memory_map(a
, &l
, 0);
642 if (!vq
->desc
|| l
!= s
) {
644 goto fail_alloc_desc
;
646 s
= l
= virtio_queue_get_avail_size(vdev
, idx
);
647 a
= virtio_queue_get_avail_addr(vdev
, idx
);
648 vq
->avail
= cpu_physical_memory_map(a
, &l
, 0);
649 if (!vq
->avail
|| l
!= s
) {
651 goto fail_alloc_avail
;
653 vq
->used_size
= s
= l
= virtio_queue_get_used_size(vdev
, idx
);
654 vq
->used_phys
= a
= virtio_queue_get_used_addr(vdev
, idx
);
655 vq
->used
= cpu_physical_memory_map(a
, &l
, 1);
656 if (!vq
->used
|| l
!= s
) {
658 goto fail_alloc_used
;
661 vq
->ring_size
= s
= l
= virtio_queue_get_ring_size(vdev
, idx
);
662 vq
->ring_phys
= a
= virtio_queue_get_ring_addr(vdev
, idx
);
663 vq
->ring
= cpu_physical_memory_map(a
, &l
, 1);
664 if (!vq
->ring
|| l
!= s
) {
666 goto fail_alloc_ring
;
669 r
= vhost_virtqueue_set_addr(dev
, vq
, idx
, dev
->log_enabled
);
674 file
.fd
= event_notifier_get_fd(virtio_queue_get_host_notifier(vvq
));
675 r
= ioctl(dev
->control
, VHOST_SET_VRING_KICK
, &file
);
681 /* Clear and discard previous events if any. */
682 event_notifier_test_and_clear(&vq
->masked_notifier
);
688 cpu_physical_memory_unmap(vq
->ring
, virtio_queue_get_ring_size(vdev
, idx
),
691 cpu_physical_memory_unmap(vq
->used
, virtio_queue_get_used_size(vdev
, idx
),
694 cpu_physical_memory_unmap(vq
->avail
, virtio_queue_get_avail_size(vdev
, idx
),
697 cpu_physical_memory_unmap(vq
->desc
, virtio_queue_get_desc_size(vdev
, idx
),
703 static void vhost_virtqueue_stop(struct vhost_dev
*dev
,
704 struct VirtIODevice
*vdev
,
705 struct vhost_virtqueue
*vq
,
708 struct vhost_vring_state state
= {
712 r
= ioctl(dev
->control
, VHOST_GET_VRING_BASE
, &state
);
714 fprintf(stderr
, "vhost VQ %d ring restore failed: %d\n", idx
, r
);
717 virtio_queue_set_last_avail_idx(vdev
, idx
, state
.num
);
719 cpu_physical_memory_unmap(vq
->ring
, virtio_queue_get_ring_size(vdev
, idx
),
720 0, virtio_queue_get_ring_size(vdev
, idx
));
721 cpu_physical_memory_unmap(vq
->used
, virtio_queue_get_used_size(vdev
, idx
),
722 1, virtio_queue_get_used_size(vdev
, idx
));
723 cpu_physical_memory_unmap(vq
->avail
, virtio_queue_get_avail_size(vdev
, idx
),
724 0, virtio_queue_get_avail_size(vdev
, idx
));
725 cpu_physical_memory_unmap(vq
->desc
, virtio_queue_get_desc_size(vdev
, idx
),
726 0, virtio_queue_get_desc_size(vdev
, idx
));
729 static void vhost_eventfd_add(MemoryListener
*listener
,
730 MemoryRegionSection
*section
,
731 bool match_data
, uint64_t data
, EventNotifier
*e
)
735 static void vhost_eventfd_del(MemoryListener
*listener
,
736 MemoryRegionSection
*section
,
737 bool match_data
, uint64_t data
, EventNotifier
*e
)
741 static int vhost_virtqueue_init(struct vhost_dev
*dev
,
742 struct vhost_virtqueue
*vq
, int n
)
744 struct vhost_vring_file file
= {
747 int r
= event_notifier_init(&vq
->masked_notifier
, 0);
752 file
.fd
= event_notifier_get_fd(&vq
->masked_notifier
);
753 r
= ioctl(dev
->control
, VHOST_SET_VRING_CALL
, &file
);
760 event_notifier_cleanup(&vq
->masked_notifier
);
764 static void vhost_virtqueue_cleanup(struct vhost_virtqueue
*vq
)
766 event_notifier_cleanup(&vq
->masked_notifier
);
769 int vhost_dev_init(struct vhost_dev
*hdev
, int devfd
, const char *devpath
,
775 hdev
->control
= devfd
;
777 hdev
->control
= open(devpath
, O_RDWR
);
778 if (hdev
->control
< 0) {
782 r
= ioctl(hdev
->control
, VHOST_SET_OWNER
, NULL
);
787 r
= ioctl(hdev
->control
, VHOST_GET_FEATURES
, &features
);
792 for (i
= 0; i
< hdev
->nvqs
; ++i
) {
793 r
= vhost_virtqueue_init(hdev
, hdev
->vqs
+ i
, i
);
798 hdev
->features
= features
;
800 hdev
->memory_listener
= (MemoryListener
) {
801 .begin
= vhost_begin
,
802 .commit
= vhost_commit
,
803 .region_add
= vhost_region_add
,
804 .region_del
= vhost_region_del
,
805 .region_nop
= vhost_region_nop
,
806 .log_start
= vhost_log_start
,
807 .log_stop
= vhost_log_stop
,
808 .log_sync
= vhost_log_sync
,
809 .log_global_start
= vhost_log_global_start
,
810 .log_global_stop
= vhost_log_global_stop
,
811 .eventfd_add
= vhost_eventfd_add
,
812 .eventfd_del
= vhost_eventfd_del
,
815 hdev
->mem
= g_malloc0(offsetof(struct vhost_memory
, regions
));
816 hdev
->n_mem_sections
= 0;
817 hdev
->mem_sections
= NULL
;
820 hdev
->log_enabled
= false;
821 hdev
->started
= false;
822 memory_listener_register(&hdev
->memory_listener
, &address_space_memory
);
827 vhost_virtqueue_cleanup(hdev
->vqs
+ i
);
831 close(hdev
->control
);
835 void vhost_dev_cleanup(struct vhost_dev
*hdev
)
838 for (i
= 0; i
< hdev
->nvqs
; ++i
) {
839 vhost_virtqueue_cleanup(hdev
->vqs
+ i
);
841 memory_listener_unregister(&hdev
->memory_listener
);
843 g_free(hdev
->mem_sections
);
844 close(hdev
->control
);
847 bool vhost_dev_query(struct vhost_dev
*hdev
, VirtIODevice
*vdev
)
849 return !vdev
->binding
->query_guest_notifiers
||
850 vdev
->binding
->query_guest_notifiers(vdev
->binding_opaque
) ||
854 /* Stop processing guest IO notifications in qemu.
855 * Start processing them in vhost in kernel.
857 int vhost_dev_enable_notifiers(struct vhost_dev
*hdev
, VirtIODevice
*vdev
)
860 if (!vdev
->binding
->set_host_notifier
) {
861 fprintf(stderr
, "binding does not support host notifiers\n");
866 for (i
= 0; i
< hdev
->nvqs
; ++i
) {
867 r
= vdev
->binding
->set_host_notifier(vdev
->binding_opaque
, i
, true);
869 fprintf(stderr
, "vhost VQ %d notifier binding failed: %d\n", i
, -r
);
877 r
= vdev
->binding
->set_host_notifier(vdev
->binding_opaque
, i
, false);
879 fprintf(stderr
, "vhost VQ %d notifier cleanup error: %d\n", i
, -r
);
888 /* Stop processing guest IO notifications in vhost.
889 * Start processing them in qemu.
890 * This might actually run the qemu handlers right away,
891 * so virtio in qemu must be completely setup when this is called.
893 void vhost_dev_disable_notifiers(struct vhost_dev
*hdev
, VirtIODevice
*vdev
)
897 for (i
= 0; i
< hdev
->nvqs
; ++i
) {
898 r
= vdev
->binding
->set_host_notifier(vdev
->binding_opaque
, i
, false);
900 fprintf(stderr
, "vhost VQ %d notifier cleanup failed: %d\n", i
, -r
);
907 /* Test and clear event pending status.
908 * Should be called after unmask to avoid losing events.
910 bool vhost_virtqueue_pending(struct vhost_dev
*hdev
, int n
)
912 struct vhost_virtqueue
*vq
= hdev
->vqs
+ n
;
913 assert(hdev
->started
);
914 return event_notifier_test_and_clear(&vq
->masked_notifier
);
917 /* Mask/unmask events from this vq. */
918 void vhost_virtqueue_mask(struct vhost_dev
*hdev
, VirtIODevice
*vdev
, int n
,
921 struct VirtQueue
*vvq
= virtio_get_queue(vdev
, n
);
924 assert(hdev
->started
);
926 struct vhost_vring_file file
= {
930 file
.fd
= event_notifier_get_fd(&hdev
->vqs
[n
].masked_notifier
);
932 file
.fd
= event_notifier_get_fd(virtio_queue_get_guest_notifier(vvq
));
934 r
= ioctl(hdev
->control
, VHOST_SET_VRING_CALL
, &file
);
938 /* Host notifiers must be enabled at this point. */
939 int vhost_dev_start(struct vhost_dev
*hdev
, VirtIODevice
*vdev
)
943 hdev
->started
= true;
945 if (!vdev
->binding
->set_guest_notifiers
) {
946 fprintf(stderr
, "binding does not support guest notifiers\n");
951 r
= vdev
->binding
->set_guest_notifiers(vdev
->binding_opaque
,
955 fprintf(stderr
, "Error binding guest notifier: %d\n", -r
);
959 r
= vhost_dev_set_features(hdev
, hdev
->log_enabled
);
963 r
= ioctl(hdev
->control
, VHOST_SET_MEM_TABLE
, hdev
->mem
);
968 for (i
= 0; i
< hdev
->nvqs
; ++i
) {
969 r
= vhost_virtqueue_start(hdev
,
978 if (hdev
->log_enabled
) {
979 hdev
->log_size
= vhost_get_log_size(hdev
);
980 hdev
->log
= hdev
->log_size
?
981 g_malloc0(hdev
->log_size
* sizeof *hdev
->log
) : NULL
;
982 r
= ioctl(hdev
->control
, VHOST_SET_LOG_BASE
,
983 (uint64_t)(unsigned long)hdev
->log
);
994 vhost_virtqueue_stop(hdev
,
1001 vdev
->binding
->set_guest_notifiers(vdev
->binding_opaque
, hdev
->nvqs
, false);
1005 hdev
->started
= false;
1009 /* Host notifiers must be enabled at this point. */
1010 void vhost_dev_stop(struct vhost_dev
*hdev
, VirtIODevice
*vdev
)
1014 for (i
= 0; i
< hdev
->nvqs
; ++i
) {
1015 vhost_virtqueue_stop(hdev
,
1020 for (i
= 0; i
< hdev
->n_mem_sections
; ++i
) {
1021 vhost_sync_dirty_bitmap(hdev
, &hdev
->mem_sections
[i
],
1022 0, (hwaddr
)~0x0ull
);
1024 r
= vdev
->binding
->set_guest_notifiers(vdev
->binding_opaque
,
1028 fprintf(stderr
, "vhost guest notifier cleanup failed: %d\n", r
);
1033 hdev
->started
= false;