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.
13 #include <sys/ioctl.h>
14 #include <sys/eventfd.h>
17 /* For range_get_last */
19 #include <linux/vhost.h>
21 static void vhost_dev_sync_region(struct vhost_dev
*dev
,
22 uint64_t mfirst
, uint64_t mlast
,
23 uint64_t rfirst
, uint64_t rlast
)
25 uint64_t start
= MAX(mfirst
, rfirst
);
26 uint64_t end
= MIN(mlast
, rlast
);
27 vhost_log_chunk_t
*from
= dev
->log
+ start
/ VHOST_LOG_CHUNK
;
28 vhost_log_chunk_t
*to
= dev
->log
+ end
/ VHOST_LOG_CHUNK
+ 1;
29 uint64_t addr
= (start
/ VHOST_LOG_CHUNK
) * VHOST_LOG_CHUNK
;
31 assert(end
/ VHOST_LOG_CHUNK
< dev
->log_size
);
32 assert(start
/ VHOST_LOG_CHUNK
< dev
->log_size
);
36 for (;from
< to
; ++from
) {
37 vhost_log_chunk_t log
;
39 /* We first check with non-atomic: much cheaper,
40 * and we expect non-dirty to be the common case. */
44 /* Data must be read atomically. We don't really
45 * need the barrier semantics of __sync
46 * builtins, but it's easier to use them than
48 log
= __sync_fetch_and_and(from
, 0);
49 while ((bit
= sizeof(log
) > sizeof(int) ?
50 ffsll(log
) : ffs(log
))) {
52 cpu_physical_memory_set_dirty(addr
+ bit
* VHOST_LOG_PAGE
);
53 log
&= ~(0x1ull
<< bit
);
55 addr
+= VHOST_LOG_CHUNK
;
59 static int vhost_client_sync_dirty_bitmap(CPUPhysMemoryClient
*client
,
60 target_phys_addr_t start_addr
,
61 target_phys_addr_t end_addr
)
63 struct vhost_dev
*dev
= container_of(client
, struct vhost_dev
, client
);
65 if (!dev
->log_enabled
|| !dev
->started
) {
68 for (i
= 0; i
< dev
->mem
->nregions
; ++i
) {
69 struct vhost_memory_region
*reg
= dev
->mem
->regions
+ i
;
70 vhost_dev_sync_region(dev
, start_addr
, end_addr
,
72 range_get_last(reg
->guest_phys_addr
,
75 for (i
= 0; i
< dev
->nvqs
; ++i
) {
76 struct vhost_virtqueue
*vq
= dev
->vqs
+ i
;
77 vhost_dev_sync_region(dev
, start_addr
, end_addr
, vq
->used_phys
,
78 range_get_last(vq
->used_phys
, vq
->used_size
));
83 /* Assign/unassign. Keep an unsorted array of non-overlapping
84 * memory regions in dev->mem. */
85 static void vhost_dev_unassign_memory(struct vhost_dev
*dev
,
89 int from
, to
, n
= dev
->mem
->nregions
;
90 /* Track overlapping/split regions for sanity checking. */
91 int overlap_start
= 0, overlap_end
= 0, overlap_middle
= 0, split
= 0;
93 for (from
= 0, to
= 0; from
< n
; ++from
, ++to
) {
94 struct vhost_memory_region
*reg
= dev
->mem
->regions
+ to
;
99 /* clone old region */
101 memcpy(reg
, dev
->mem
->regions
+ from
, sizeof *reg
);
104 /* No overlap is simple */
105 if (!ranges_overlap(reg
->guest_phys_addr
, reg
->memory_size
,
110 /* Split only happens if supplied region
111 * is in the middle of an existing one. Thus it can not
112 * overlap with any other existing region. */
115 reglast
= range_get_last(reg
->guest_phys_addr
, reg
->memory_size
);
116 memlast
= range_get_last(start_addr
, size
);
118 /* Remove whole region */
119 if (start_addr
<= reg
->guest_phys_addr
&& memlast
>= reglast
) {
120 --dev
->mem
->nregions
;
128 if (memlast
>= reglast
) {
129 reg
->memory_size
= start_addr
- reg
->guest_phys_addr
;
130 assert(reg
->memory_size
);
131 assert(!overlap_end
);
137 if (start_addr
<= reg
->guest_phys_addr
) {
138 change
= memlast
+ 1 - reg
->guest_phys_addr
;
139 reg
->memory_size
-= change
;
140 reg
->guest_phys_addr
+= change
;
141 reg
->userspace_addr
+= change
;
142 assert(reg
->memory_size
);
143 assert(!overlap_start
);
148 /* This only happens if supplied region
149 * is in the middle of an existing one. Thus it can not
150 * overlap with any other existing region. */
151 assert(!overlap_start
);
152 assert(!overlap_end
);
153 assert(!overlap_middle
);
154 /* Split region: shrink first part, shift second part. */
155 memcpy(dev
->mem
->regions
+ n
, reg
, sizeof *reg
);
156 reg
->memory_size
= start_addr
- reg
->guest_phys_addr
;
157 assert(reg
->memory_size
);
158 change
= memlast
+ 1 - reg
->guest_phys_addr
;
159 reg
= dev
->mem
->regions
+ n
;
160 reg
->memory_size
-= change
;
161 assert(reg
->memory_size
);
162 reg
->guest_phys_addr
+= change
;
163 reg
->userspace_addr
+= change
;
164 /* Never add more than 1 region */
165 assert(dev
->mem
->nregions
== n
);
166 ++dev
->mem
->nregions
;
171 /* Called after unassign, so no regions overlap the given range. */
172 static void vhost_dev_assign_memory(struct vhost_dev
*dev
,
178 struct vhost_memory_region
*merged
= NULL
;
179 for (from
= 0, to
= 0; from
< dev
->mem
->nregions
; ++from
, ++to
) {
180 struct vhost_memory_region
*reg
= dev
->mem
->regions
+ to
;
181 uint64_t prlast
, urlast
;
182 uint64_t pmlast
, umlast
;
185 /* clone old region */
187 memcpy(reg
, dev
->mem
->regions
+ from
, sizeof *reg
);
189 prlast
= range_get_last(reg
->guest_phys_addr
, reg
->memory_size
);
190 pmlast
= range_get_last(start_addr
, size
);
191 urlast
= range_get_last(reg
->userspace_addr
, reg
->memory_size
);
192 umlast
= range_get_last(uaddr
, size
);
194 /* check for overlapping regions: should never happen. */
195 assert(prlast
< start_addr
|| pmlast
< reg
->guest_phys_addr
);
196 /* Not an adjacent or overlapping region - do not merge. */
197 if ((prlast
+ 1 != start_addr
|| urlast
+ 1 != uaddr
) &&
198 (pmlast
+ 1 != reg
->guest_phys_addr
||
199 umlast
+ 1 != reg
->userspace_addr
)) {
209 u
= MIN(uaddr
, reg
->userspace_addr
);
210 s
= MIN(start_addr
, reg
->guest_phys_addr
);
211 e
= MAX(pmlast
, prlast
);
212 uaddr
= merged
->userspace_addr
= u
;
213 start_addr
= merged
->guest_phys_addr
= s
;
214 size
= merged
->memory_size
= e
- s
+ 1;
215 assert(merged
->memory_size
);
219 struct vhost_memory_region
*reg
= dev
->mem
->regions
+ to
;
220 memset(reg
, 0, sizeof *reg
);
221 reg
->memory_size
= size
;
222 assert(reg
->memory_size
);
223 reg
->guest_phys_addr
= start_addr
;
224 reg
->userspace_addr
= uaddr
;
227 assert(to
<= dev
->mem
->nregions
+ 1);
228 dev
->mem
->nregions
= to
;
231 static uint64_t vhost_get_log_size(struct vhost_dev
*dev
)
233 uint64_t log_size
= 0;
235 for (i
= 0; i
< dev
->mem
->nregions
; ++i
) {
236 struct vhost_memory_region
*reg
= dev
->mem
->regions
+ i
;
237 uint64_t last
= range_get_last(reg
->guest_phys_addr
,
239 log_size
= MAX(log_size
, last
/ VHOST_LOG_CHUNK
+ 1);
241 for (i
= 0; i
< dev
->nvqs
; ++i
) {
242 struct vhost_virtqueue
*vq
= dev
->vqs
+ i
;
243 uint64_t last
= vq
->used_phys
+ vq
->used_size
- 1;
244 log_size
= MAX(log_size
, last
/ VHOST_LOG_CHUNK
+ 1);
249 static inline void vhost_dev_log_resize(struct vhost_dev
* dev
, uint64_t size
)
251 vhost_log_chunk_t
*log
;
255 log
= qemu_mallocz(size
* sizeof *log
);
259 log_base
= (uint64_t)(unsigned long)log
;
260 r
= ioctl(dev
->control
, VHOST_SET_LOG_BASE
, &log_base
);
262 vhost_client_sync_dirty_bitmap(&dev
->client
, 0,
263 (target_phys_addr_t
)~0x0ull
);
268 dev
->log_size
= size
;
271 static int vhost_verify_ring_mappings(struct vhost_dev
*dev
,
276 for (i
= 0; i
< dev
->nvqs
; ++i
) {
277 struct vhost_virtqueue
*vq
= dev
->vqs
+ i
;
278 target_phys_addr_t l
;
281 if (!ranges_overlap(start_addr
, size
, vq
->ring_phys
, vq
->ring_size
)) {
285 p
= cpu_physical_memory_map(vq
->ring_phys
, &l
, 1);
286 if (!p
|| l
!= vq
->ring_size
) {
287 fprintf(stderr
, "Unable to map ring buffer for ring %d\n", i
);
291 fprintf(stderr
, "Ring buffer relocated for ring %d\n", i
);
294 cpu_physical_memory_unmap(p
, l
, 0, 0);
299 static void vhost_client_set_memory(CPUPhysMemoryClient
*client
,
300 target_phys_addr_t start_addr
,
302 ram_addr_t phys_offset
)
304 struct vhost_dev
*dev
= container_of(client
, struct vhost_dev
, client
);
305 ram_addr_t flags
= phys_offset
& ~TARGET_PAGE_MASK
;
306 int s
= offsetof(struct vhost_memory
, regions
) +
307 (dev
->mem
->nregions
+ 1) * sizeof dev
->mem
->regions
[0];
310 dev
->mem
= qemu_realloc(dev
->mem
, s
);
314 vhost_dev_unassign_memory(dev
, start_addr
, size
);
315 if (flags
== IO_MEM_RAM
) {
316 /* Add given mapping, merging adjacent regions if any */
317 vhost_dev_assign_memory(dev
, start_addr
, size
,
318 (uintptr_t)qemu_get_ram_ptr(phys_offset
));
320 /* Remove old mapping for this memory, if any. */
321 vhost_dev_unassign_memory(dev
, start_addr
, size
);
329 r
= vhost_verify_ring_mappings(dev
, start_addr
, size
);
333 if (!dev
->log_enabled
) {
334 r
= ioctl(dev
->control
, VHOST_SET_MEM_TABLE
, dev
->mem
);
338 log_size
= vhost_get_log_size(dev
);
339 /* We allocate an extra 4K bytes to log,
340 * to reduce the * number of reallocations. */
341 #define VHOST_LOG_BUFFER (0x1000 / sizeof *dev->log)
342 /* To log more, must increase log size before table update. */
343 if (dev
->log_size
< log_size
) {
344 vhost_dev_log_resize(dev
, log_size
+ VHOST_LOG_BUFFER
);
346 r
= ioctl(dev
->control
, VHOST_SET_MEM_TABLE
, dev
->mem
);
348 /* To log less, can only decrease log size after table update. */
349 if (dev
->log_size
> log_size
+ VHOST_LOG_BUFFER
) {
350 vhost_dev_log_resize(dev
, log_size
);
354 static int vhost_virtqueue_set_addr(struct vhost_dev
*dev
,
355 struct vhost_virtqueue
*vq
,
356 unsigned idx
, bool enable_log
)
358 struct vhost_vring_addr addr
= {
360 .desc_user_addr
= (uint64_t)(unsigned long)vq
->desc
,
361 .avail_user_addr
= (uint64_t)(unsigned long)vq
->avail
,
362 .used_user_addr
= (uint64_t)(unsigned long)vq
->used
,
363 .log_guest_addr
= vq
->used_phys
,
364 .flags
= enable_log
? (1 << VHOST_VRING_F_LOG
) : 0,
366 int r
= ioctl(dev
->control
, VHOST_SET_VRING_ADDR
, &addr
);
373 static int vhost_dev_set_features(struct vhost_dev
*dev
, bool enable_log
)
375 uint64_t features
= dev
->acked_features
;
378 features
|= 0x1 << VHOST_F_LOG_ALL
;
380 r
= ioctl(dev
->control
, VHOST_SET_FEATURES
, &features
);
381 return r
< 0 ? -errno
: 0;
384 static int vhost_dev_set_log(struct vhost_dev
*dev
, bool enable_log
)
387 r
= vhost_dev_set_features(dev
, enable_log
);
391 for (i
= 0; i
< dev
->nvqs
; ++i
) {
392 r
= vhost_virtqueue_set_addr(dev
, dev
->vqs
+ i
, i
,
400 for (; i
>= 0; --i
) {
401 t
= vhost_virtqueue_set_addr(dev
, dev
->vqs
+ i
, i
,
405 t
= vhost_dev_set_features(dev
, dev
->log_enabled
);
411 static int vhost_client_migration_log(CPUPhysMemoryClient
*client
,
414 struct vhost_dev
*dev
= container_of(client
, struct vhost_dev
, client
);
416 if (!!enable
== dev
->log_enabled
) {
420 dev
->log_enabled
= enable
;
424 r
= vhost_dev_set_log(dev
, false);
434 vhost_dev_log_resize(dev
, vhost_get_log_size(dev
));
435 r
= vhost_dev_set_log(dev
, true);
440 dev
->log_enabled
= enable
;
444 static int vhost_virtqueue_init(struct vhost_dev
*dev
,
445 struct VirtIODevice
*vdev
,
446 struct vhost_virtqueue
*vq
,
449 target_phys_addr_t s
, l
, a
;
451 struct vhost_vring_file file
= {
454 struct vhost_vring_state state
= {
457 struct VirtQueue
*vvq
= virtio_get_queue(vdev
, idx
);
459 if (!vdev
->binding
->set_guest_notifier
) {
460 fprintf(stderr
, "binding does not support guest notifiers\n");
464 if (!vdev
->binding
->set_host_notifier
) {
465 fprintf(stderr
, "binding does not support host notifiers\n");
469 vq
->num
= state
.num
= virtio_queue_get_num(vdev
, idx
);
470 r
= ioctl(dev
->control
, VHOST_SET_VRING_NUM
, &state
);
475 state
.num
= virtio_queue_get_last_avail_idx(vdev
, idx
);
476 r
= ioctl(dev
->control
, VHOST_SET_VRING_BASE
, &state
);
481 s
= l
= virtio_queue_get_desc_size(vdev
, idx
);
482 a
= virtio_queue_get_desc_addr(vdev
, idx
);
483 vq
->desc
= cpu_physical_memory_map(a
, &l
, 0);
484 if (!vq
->desc
|| l
!= s
) {
486 goto fail_alloc_desc
;
488 s
= l
= virtio_queue_get_avail_size(vdev
, idx
);
489 a
= virtio_queue_get_avail_addr(vdev
, idx
);
490 vq
->avail
= cpu_physical_memory_map(a
, &l
, 0);
491 if (!vq
->avail
|| l
!= s
) {
493 goto fail_alloc_avail
;
495 vq
->used_size
= s
= l
= virtio_queue_get_used_size(vdev
, idx
);
496 vq
->used_phys
= a
= virtio_queue_get_used_addr(vdev
, idx
);
497 vq
->used
= cpu_physical_memory_map(a
, &l
, 1);
498 if (!vq
->used
|| l
!= s
) {
500 goto fail_alloc_used
;
503 vq
->ring_size
= s
= l
= virtio_queue_get_ring_size(vdev
, idx
);
504 vq
->ring_phys
= a
= virtio_queue_get_ring_addr(vdev
, idx
);
505 vq
->ring
= cpu_physical_memory_map(a
, &l
, 1);
506 if (!vq
->ring
|| l
!= s
) {
508 goto fail_alloc_ring
;
511 r
= vhost_virtqueue_set_addr(dev
, vq
, idx
, dev
->log_enabled
);
516 r
= vdev
->binding
->set_guest_notifier(vdev
->binding_opaque
, idx
, true);
518 fprintf(stderr
, "Error binding guest notifier: %d\n", -r
);
519 goto fail_guest_notifier
;
522 r
= vdev
->binding
->set_host_notifier(vdev
->binding_opaque
, idx
, true);
524 fprintf(stderr
, "Error binding host notifier: %d\n", -r
);
525 goto fail_host_notifier
;
528 file
.fd
= event_notifier_get_fd(virtio_queue_get_host_notifier(vvq
));
529 r
= ioctl(dev
->control
, VHOST_SET_VRING_KICK
, &file
);
534 file
.fd
= event_notifier_get_fd(virtio_queue_get_guest_notifier(vvq
));
535 r
= ioctl(dev
->control
, VHOST_SET_VRING_CALL
, &file
);
544 vdev
->binding
->set_host_notifier(vdev
->binding_opaque
, idx
, false);
546 vdev
->binding
->set_guest_notifier(vdev
->binding_opaque
, idx
, false);
549 cpu_physical_memory_unmap(vq
->ring
, virtio_queue_get_ring_size(vdev
, idx
),
552 cpu_physical_memory_unmap(vq
->used
, virtio_queue_get_used_size(vdev
, idx
),
555 cpu_physical_memory_unmap(vq
->avail
, virtio_queue_get_avail_size(vdev
, idx
),
558 cpu_physical_memory_unmap(vq
->desc
, virtio_queue_get_desc_size(vdev
, idx
),
564 static void vhost_virtqueue_cleanup(struct vhost_dev
*dev
,
565 struct VirtIODevice
*vdev
,
566 struct vhost_virtqueue
*vq
,
569 struct vhost_vring_state state
= {
573 r
= vdev
->binding
->set_guest_notifier(vdev
->binding_opaque
, idx
, false);
575 fprintf(stderr
, "vhost VQ %d guest cleanup failed: %d\n", idx
, r
);
580 r
= vdev
->binding
->set_host_notifier(vdev
->binding_opaque
, idx
, false);
582 fprintf(stderr
, "vhost VQ %d host cleanup failed: %d\n", idx
, r
);
586 r
= ioctl(dev
->control
, VHOST_GET_VRING_BASE
, &state
);
588 fprintf(stderr
, "vhost VQ %d ring restore failed: %d\n", idx
, r
);
591 virtio_queue_set_last_avail_idx(vdev
, idx
, state
.num
);
593 cpu_physical_memory_unmap(vq
->ring
, virtio_queue_get_ring_size(vdev
, idx
),
594 0, virtio_queue_get_ring_size(vdev
, idx
));
595 cpu_physical_memory_unmap(vq
->used
, virtio_queue_get_used_size(vdev
, idx
),
596 1, virtio_queue_get_used_size(vdev
, idx
));
597 cpu_physical_memory_unmap(vq
->avail
, virtio_queue_get_avail_size(vdev
, idx
),
598 0, virtio_queue_get_avail_size(vdev
, idx
));
599 cpu_physical_memory_unmap(vq
->desc
, virtio_queue_get_desc_size(vdev
, idx
),
600 0, virtio_queue_get_desc_size(vdev
, idx
));
603 int vhost_dev_init(struct vhost_dev
*hdev
, int devfd
)
608 hdev
->control
= devfd
;
610 hdev
->control
= open("/dev/vhost-net", O_RDWR
);
611 if (hdev
->control
< 0) {
615 r
= ioctl(hdev
->control
, VHOST_SET_OWNER
, NULL
);
620 r
= ioctl(hdev
->control
, VHOST_GET_FEATURES
, &features
);
624 hdev
->features
= features
;
626 hdev
->client
.set_memory
= vhost_client_set_memory
;
627 hdev
->client
.sync_dirty_bitmap
= vhost_client_sync_dirty_bitmap
;
628 hdev
->client
.migration_log
= vhost_client_migration_log
;
629 hdev
->mem
= qemu_mallocz(offsetof(struct vhost_memory
, regions
));
632 hdev
->log_enabled
= false;
633 hdev
->started
= false;
634 cpu_register_phys_memory_client(&hdev
->client
);
638 close(hdev
->control
);
642 void vhost_dev_cleanup(struct vhost_dev
*hdev
)
644 cpu_unregister_phys_memory_client(&hdev
->client
);
645 qemu_free(hdev
->mem
);
646 close(hdev
->control
);
649 int vhost_dev_start(struct vhost_dev
*hdev
, VirtIODevice
*vdev
)
653 r
= vhost_dev_set_features(hdev
, hdev
->log_enabled
);
657 r
= ioctl(hdev
->control
, VHOST_SET_MEM_TABLE
, hdev
->mem
);
662 for (i
= 0; i
< hdev
->nvqs
; ++i
) {
663 r
= vhost_virtqueue_init(hdev
,
672 if (hdev
->log_enabled
) {
673 hdev
->log_size
= vhost_get_log_size(hdev
);
674 hdev
->log
= hdev
->log_size
?
675 qemu_mallocz(hdev
->log_size
* sizeof *hdev
->log
) : NULL
;
676 r
= ioctl(hdev
->control
, VHOST_SET_LOG_BASE
,
677 (uint64_t)(unsigned long)hdev
->log
);
684 hdev
->started
= true;
689 vhost_virtqueue_cleanup(hdev
,
698 void vhost_dev_stop(struct vhost_dev
*hdev
, VirtIODevice
*vdev
)
701 for (i
= 0; i
< hdev
->nvqs
; ++i
) {
702 vhost_virtqueue_cleanup(hdev
,
707 vhost_client_sync_dirty_bitmap(&hdev
->client
, 0,
708 (target_phys_addr_t
)~0x0ull
);
709 hdev
->started
= false;
710 qemu_free(hdev
->log
);