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>
16 /* For range_get_last */
18 #include <linux/vhost.h>
20 static void vhost_dev_sync_region(struct vhost_dev
*dev
,
21 uint64_t mfirst
, uint64_t mlast
,
22 uint64_t rfirst
, uint64_t rlast
)
24 uint64_t start
= MAX(mfirst
, rfirst
);
25 uint64_t end
= MIN(mlast
, rlast
);
26 vhost_log_chunk_t
*from
= dev
->log
+ start
/ VHOST_LOG_CHUNK
;
27 vhost_log_chunk_t
*to
= dev
->log
+ end
/ VHOST_LOG_CHUNK
+ 1;
28 uint64_t addr
= (start
/ VHOST_LOG_CHUNK
) * VHOST_LOG_CHUNK
;
30 assert(end
/ VHOST_LOG_CHUNK
< dev
->log_size
);
31 assert(start
/ VHOST_LOG_CHUNK
< dev
->log_size
);
35 for (;from
< to
; ++from
) {
36 vhost_log_chunk_t log
;
38 /* We first check with non-atomic: much cheaper,
39 * and we expect non-dirty to be the common case. */
43 /* Data must be read atomically. We don't really
44 * need the barrier semantics of __sync
45 * builtins, but it's easier to use them than
47 log
= __sync_fetch_and_and(from
, 0);
48 while ((bit
= sizeof(log
) > sizeof(int) ?
49 ffsll(log
) : ffs(log
))) {
51 cpu_physical_memory_set_dirty(addr
+ bit
* VHOST_LOG_PAGE
);
52 log
&= ~(0x1ull
<< bit
);
54 addr
+= VHOST_LOG_CHUNK
;
58 static int vhost_client_sync_dirty_bitmap(CPUPhysMemoryClient
*client
,
59 target_phys_addr_t start_addr
,
60 target_phys_addr_t end_addr
)
62 struct vhost_dev
*dev
= container_of(client
, struct vhost_dev
, client
);
64 if (!dev
->log_enabled
|| !dev
->started
) {
67 for (i
= 0; i
< dev
->mem
->nregions
; ++i
) {
68 struct vhost_memory_region
*reg
= dev
->mem
->regions
+ i
;
69 vhost_dev_sync_region(dev
, start_addr
, end_addr
,
71 range_get_last(reg
->guest_phys_addr
,
74 for (i
= 0; i
< dev
->nvqs
; ++i
) {
75 struct vhost_virtqueue
*vq
= dev
->vqs
+ i
;
76 vhost_dev_sync_region(dev
, start_addr
, end_addr
, vq
->used_phys
,
77 range_get_last(vq
->used_phys
, vq
->used_size
));
82 /* Assign/unassign. Keep an unsorted array of non-overlapping
83 * memory regions in dev->mem. */
84 static void vhost_dev_unassign_memory(struct vhost_dev
*dev
,
88 int from
, to
, n
= dev
->mem
->nregions
;
89 /* Track overlapping/split regions for sanity checking. */
90 int overlap_start
= 0, overlap_end
= 0, overlap_middle
= 0, split
= 0;
92 for (from
= 0, to
= 0; from
< n
; ++from
, ++to
) {
93 struct vhost_memory_region
*reg
= dev
->mem
->regions
+ to
;
98 /* clone old region */
100 memcpy(reg
, dev
->mem
->regions
+ from
, sizeof *reg
);
103 /* No overlap is simple */
104 if (!ranges_overlap(reg
->guest_phys_addr
, reg
->memory_size
,
109 /* Split only happens if supplied region
110 * is in the middle of an existing one. Thus it can not
111 * overlap with any other existing region. */
114 reglast
= range_get_last(reg
->guest_phys_addr
, reg
->memory_size
);
115 memlast
= range_get_last(start_addr
, size
);
117 /* Remove whole region */
118 if (start_addr
<= reg
->guest_phys_addr
&& memlast
>= reglast
) {
119 --dev
->mem
->nregions
;
127 if (memlast
>= reglast
) {
128 reg
->memory_size
= start_addr
- reg
->guest_phys_addr
;
129 assert(reg
->memory_size
);
130 assert(!overlap_end
);
136 if (start_addr
<= reg
->guest_phys_addr
) {
137 change
= memlast
+ 1 - reg
->guest_phys_addr
;
138 reg
->memory_size
-= change
;
139 reg
->guest_phys_addr
+= change
;
140 reg
->userspace_addr
+= change
;
141 assert(reg
->memory_size
);
142 assert(!overlap_start
);
147 /* This only happens if supplied region
148 * is in the middle of an existing one. Thus it can not
149 * overlap with any other existing region. */
150 assert(!overlap_start
);
151 assert(!overlap_end
);
152 assert(!overlap_middle
);
153 /* Split region: shrink first part, shift second part. */
154 memcpy(dev
->mem
->regions
+ n
, reg
, sizeof *reg
);
155 reg
->memory_size
= start_addr
- reg
->guest_phys_addr
;
156 assert(reg
->memory_size
);
157 change
= memlast
+ 1 - reg
->guest_phys_addr
;
158 reg
= dev
->mem
->regions
+ n
;
159 reg
->memory_size
-= change
;
160 assert(reg
->memory_size
);
161 reg
->guest_phys_addr
+= change
;
162 reg
->userspace_addr
+= change
;
163 /* Never add more than 1 region */
164 assert(dev
->mem
->nregions
== n
);
165 ++dev
->mem
->nregions
;
170 /* Called after unassign, so no regions overlap the given range. */
171 static void vhost_dev_assign_memory(struct vhost_dev
*dev
,
177 struct vhost_memory_region
*merged
= NULL
;
178 for (from
= 0, to
= 0; from
< dev
->mem
->nregions
; ++from
, ++to
) {
179 struct vhost_memory_region
*reg
= dev
->mem
->regions
+ to
;
180 uint64_t prlast
, urlast
;
181 uint64_t pmlast
, umlast
;
184 /* clone old region */
186 memcpy(reg
, dev
->mem
->regions
+ from
, sizeof *reg
);
188 prlast
= range_get_last(reg
->guest_phys_addr
, reg
->memory_size
);
189 pmlast
= range_get_last(start_addr
, size
);
190 urlast
= range_get_last(reg
->userspace_addr
, reg
->memory_size
);
191 umlast
= range_get_last(uaddr
, size
);
193 /* check for overlapping regions: should never happen. */
194 assert(prlast
< start_addr
|| pmlast
< reg
->guest_phys_addr
);
195 /* Not an adjacent or overlapping region - do not merge. */
196 if ((prlast
+ 1 != start_addr
|| urlast
+ 1 != uaddr
) &&
197 (pmlast
+ 1 != reg
->guest_phys_addr
||
198 umlast
+ 1 != reg
->userspace_addr
)) {
208 u
= MIN(uaddr
, reg
->userspace_addr
);
209 s
= MIN(start_addr
, reg
->guest_phys_addr
);
210 e
= MAX(pmlast
, prlast
);
211 uaddr
= merged
->userspace_addr
= u
;
212 start_addr
= merged
->guest_phys_addr
= s
;
213 size
= merged
->memory_size
= e
- s
+ 1;
214 assert(merged
->memory_size
);
218 struct vhost_memory_region
*reg
= dev
->mem
->regions
+ to
;
219 memset(reg
, 0, sizeof *reg
);
220 reg
->memory_size
= size
;
221 assert(reg
->memory_size
);
222 reg
->guest_phys_addr
= start_addr
;
223 reg
->userspace_addr
= uaddr
;
226 assert(to
<= dev
->mem
->nregions
+ 1);
227 dev
->mem
->nregions
= to
;
230 static uint64_t vhost_get_log_size(struct vhost_dev
*dev
)
232 uint64_t log_size
= 0;
234 for (i
= 0; i
< dev
->mem
->nregions
; ++i
) {
235 struct vhost_memory_region
*reg
= dev
->mem
->regions
+ i
;
236 uint64_t last
= range_get_last(reg
->guest_phys_addr
,
238 log_size
= MAX(log_size
, last
/ VHOST_LOG_CHUNK
+ 1);
240 for (i
= 0; i
< dev
->nvqs
; ++i
) {
241 struct vhost_virtqueue
*vq
= dev
->vqs
+ i
;
242 uint64_t last
= vq
->used_phys
+ vq
->used_size
- 1;
243 log_size
= MAX(log_size
, last
/ VHOST_LOG_CHUNK
+ 1);
248 static inline void vhost_dev_log_resize(struct vhost_dev
* dev
, uint64_t size
)
250 vhost_log_chunk_t
*log
;
254 log
= qemu_mallocz(size
* sizeof *log
);
258 log_base
= (uint64_t)(unsigned long)log
;
259 r
= ioctl(dev
->control
, VHOST_SET_LOG_BASE
, &log_base
);
261 vhost_client_sync_dirty_bitmap(&dev
->client
, 0,
262 (target_phys_addr_t
)~0x0ull
);
267 dev
->log_size
= size
;
270 static int vhost_verify_ring_mappings(struct vhost_dev
*dev
,
275 for (i
= 0; i
< dev
->nvqs
; ++i
) {
276 struct vhost_virtqueue
*vq
= dev
->vqs
+ i
;
277 target_phys_addr_t l
;
280 if (!ranges_overlap(start_addr
, size
, vq
->ring_phys
, vq
->ring_size
)) {
284 p
= cpu_physical_memory_map(vq
->ring_phys
, &l
, 1);
285 if (!p
|| l
!= vq
->ring_size
) {
286 fprintf(stderr
, "Unable to map ring buffer for ring %d\n", i
);
290 fprintf(stderr
, "Ring buffer relocated for ring %d\n", i
);
293 cpu_physical_memory_unmap(p
, l
, 0, 0);
298 static void vhost_client_set_memory(CPUPhysMemoryClient
*client
,
299 target_phys_addr_t start_addr
,
301 ram_addr_t phys_offset
)
303 struct vhost_dev
*dev
= container_of(client
, struct vhost_dev
, client
);
304 ram_addr_t flags
= phys_offset
& ~TARGET_PAGE_MASK
;
305 int s
= offsetof(struct vhost_memory
, regions
) +
306 (dev
->mem
->nregions
+ 1) * sizeof dev
->mem
->regions
[0];
309 dev
->mem
= qemu_realloc(dev
->mem
, s
);
313 vhost_dev_unassign_memory(dev
, start_addr
, size
);
314 if (flags
== IO_MEM_RAM
) {
315 /* Add given mapping, merging adjacent regions if any */
316 vhost_dev_assign_memory(dev
, start_addr
, size
,
317 (uintptr_t)qemu_get_ram_ptr(phys_offset
));
319 /* Remove old mapping for this memory, if any. */
320 vhost_dev_unassign_memory(dev
, start_addr
, size
);
328 r
= vhost_verify_ring_mappings(dev
, start_addr
, size
);
332 if (!dev
->log_enabled
) {
333 r
= ioctl(dev
->control
, VHOST_SET_MEM_TABLE
, dev
->mem
);
337 log_size
= vhost_get_log_size(dev
);
338 /* We allocate an extra 4K bytes to log,
339 * to reduce the * number of reallocations. */
340 #define VHOST_LOG_BUFFER (0x1000 / sizeof *dev->log)
341 /* To log more, must increase log size before table update. */
342 if (dev
->log_size
< log_size
) {
343 vhost_dev_log_resize(dev
, log_size
+ VHOST_LOG_BUFFER
);
345 r
= ioctl(dev
->control
, VHOST_SET_MEM_TABLE
, dev
->mem
);
347 /* To log less, can only decrease log size after table update. */
348 if (dev
->log_size
> log_size
+ VHOST_LOG_BUFFER
) {
349 vhost_dev_log_resize(dev
, log_size
);
353 static int vhost_virtqueue_set_addr(struct vhost_dev
*dev
,
354 struct vhost_virtqueue
*vq
,
355 unsigned idx
, bool enable_log
)
357 struct vhost_vring_addr addr
= {
359 .desc_user_addr
= (uint64_t)(unsigned long)vq
->desc
,
360 .avail_user_addr
= (uint64_t)(unsigned long)vq
->avail
,
361 .used_user_addr
= (uint64_t)(unsigned long)vq
->used
,
362 .log_guest_addr
= vq
->used_phys
,
363 .flags
= enable_log
? (1 << VHOST_VRING_F_LOG
) : 0,
365 int r
= ioctl(dev
->control
, VHOST_SET_VRING_ADDR
, &addr
);
372 static int vhost_dev_set_features(struct vhost_dev
*dev
, bool enable_log
)
374 uint64_t features
= dev
->acked_features
;
377 features
|= 0x1 << VHOST_F_LOG_ALL
;
379 r
= ioctl(dev
->control
, VHOST_SET_FEATURES
, &features
);
380 return r
< 0 ? -errno
: 0;
383 static int vhost_dev_set_log(struct vhost_dev
*dev
, bool enable_log
)
386 r
= vhost_dev_set_features(dev
, enable_log
);
390 for (i
= 0; i
< dev
->nvqs
; ++i
) {
391 r
= vhost_virtqueue_set_addr(dev
, dev
->vqs
+ i
, i
,
399 for (; i
>= 0; --i
) {
400 t
= vhost_virtqueue_set_addr(dev
, dev
->vqs
+ i
, i
,
404 t
= vhost_dev_set_features(dev
, dev
->log_enabled
);
410 static int vhost_client_migration_log(CPUPhysMemoryClient
*client
,
413 struct vhost_dev
*dev
= container_of(client
, struct vhost_dev
, client
);
415 if (!!enable
== dev
->log_enabled
) {
419 dev
->log_enabled
= enable
;
423 r
= vhost_dev_set_log(dev
, false);
433 vhost_dev_log_resize(dev
, vhost_get_log_size(dev
));
434 r
= vhost_dev_set_log(dev
, true);
439 dev
->log_enabled
= enable
;
443 static int vhost_virtqueue_init(struct vhost_dev
*dev
,
444 struct VirtIODevice
*vdev
,
445 struct vhost_virtqueue
*vq
,
448 target_phys_addr_t s
, l
, a
;
450 struct vhost_vring_file file
= {
453 struct vhost_vring_state state
= {
456 struct VirtQueue
*vvq
= virtio_get_queue(vdev
, idx
);
458 if (!vdev
->binding
->set_guest_notifier
) {
459 fprintf(stderr
, "binding does not support guest notifiers\n");
463 if (!vdev
->binding
->set_host_notifier
) {
464 fprintf(stderr
, "binding does not support host notifiers\n");
468 vq
->num
= state
.num
= virtio_queue_get_num(vdev
, idx
);
469 r
= ioctl(dev
->control
, VHOST_SET_VRING_NUM
, &state
);
474 state
.num
= virtio_queue_get_last_avail_idx(vdev
, idx
);
475 r
= ioctl(dev
->control
, VHOST_SET_VRING_BASE
, &state
);
480 s
= l
= virtio_queue_get_desc_size(vdev
, idx
);
481 a
= virtio_queue_get_desc_addr(vdev
, idx
);
482 vq
->desc
= cpu_physical_memory_map(a
, &l
, 0);
483 if (!vq
->desc
|| l
!= s
) {
485 goto fail_alloc_desc
;
487 s
= l
= virtio_queue_get_avail_size(vdev
, idx
);
488 a
= virtio_queue_get_avail_addr(vdev
, idx
);
489 vq
->avail
= cpu_physical_memory_map(a
, &l
, 0);
490 if (!vq
->avail
|| l
!= s
) {
492 goto fail_alloc_avail
;
494 vq
->used_size
= s
= l
= virtio_queue_get_used_size(vdev
, idx
);
495 vq
->used_phys
= a
= virtio_queue_get_used_addr(vdev
, idx
);
496 vq
->used
= cpu_physical_memory_map(a
, &l
, 1);
497 if (!vq
->used
|| l
!= s
) {
499 goto fail_alloc_used
;
502 vq
->ring_size
= s
= l
= virtio_queue_get_ring_size(vdev
, idx
);
503 vq
->ring_phys
= a
= virtio_queue_get_ring_addr(vdev
, idx
);
504 vq
->ring
= cpu_physical_memory_map(a
, &l
, 1);
505 if (!vq
->ring
|| l
!= s
) {
507 goto fail_alloc_ring
;
510 r
= vhost_virtqueue_set_addr(dev
, vq
, idx
, dev
->log_enabled
);
515 r
= vdev
->binding
->set_guest_notifier(vdev
->binding_opaque
, idx
, true);
517 fprintf(stderr
, "Error binding guest notifier: %d\n", -r
);
518 goto fail_guest_notifier
;
521 r
= vdev
->binding
->set_host_notifier(vdev
->binding_opaque
, idx
, true);
523 fprintf(stderr
, "Error binding host notifier: %d\n", -r
);
524 goto fail_host_notifier
;
527 file
.fd
= event_notifier_get_fd(virtio_queue_get_host_notifier(vvq
));
528 r
= ioctl(dev
->control
, VHOST_SET_VRING_KICK
, &file
);
533 file
.fd
= event_notifier_get_fd(virtio_queue_get_guest_notifier(vvq
));
534 r
= ioctl(dev
->control
, VHOST_SET_VRING_CALL
, &file
);
543 vdev
->binding
->set_host_notifier(vdev
->binding_opaque
, idx
, false);
545 vdev
->binding
->set_guest_notifier(vdev
->binding_opaque
, idx
, false);
548 cpu_physical_memory_unmap(vq
->ring
, virtio_queue_get_ring_size(vdev
, idx
),
551 cpu_physical_memory_unmap(vq
->used
, virtio_queue_get_used_size(vdev
, idx
),
554 cpu_physical_memory_unmap(vq
->avail
, virtio_queue_get_avail_size(vdev
, idx
),
557 cpu_physical_memory_unmap(vq
->desc
, virtio_queue_get_desc_size(vdev
, idx
),
563 static void vhost_virtqueue_cleanup(struct vhost_dev
*dev
,
564 struct VirtIODevice
*vdev
,
565 struct vhost_virtqueue
*vq
,
568 struct vhost_vring_state state
= {
572 r
= vdev
->binding
->set_guest_notifier(vdev
->binding_opaque
, idx
, false);
574 fprintf(stderr
, "vhost VQ %d guest cleanup failed: %d\n", idx
, r
);
579 r
= vdev
->binding
->set_host_notifier(vdev
->binding_opaque
, idx
, false);
581 fprintf(stderr
, "vhost VQ %d host cleanup failed: %d\n", idx
, r
);
585 r
= ioctl(dev
->control
, VHOST_GET_VRING_BASE
, &state
);
587 fprintf(stderr
, "vhost VQ %d ring restore failed: %d\n", idx
, r
);
590 virtio_queue_set_last_avail_idx(vdev
, idx
, state
.num
);
592 cpu_physical_memory_unmap(vq
->ring
, virtio_queue_get_ring_size(vdev
, idx
),
593 0, virtio_queue_get_ring_size(vdev
, idx
));
594 cpu_physical_memory_unmap(vq
->used
, virtio_queue_get_used_size(vdev
, idx
),
595 1, virtio_queue_get_used_size(vdev
, idx
));
596 cpu_physical_memory_unmap(vq
->avail
, virtio_queue_get_avail_size(vdev
, idx
),
597 0, virtio_queue_get_avail_size(vdev
, idx
));
598 cpu_physical_memory_unmap(vq
->desc
, virtio_queue_get_desc_size(vdev
, idx
),
599 0, virtio_queue_get_desc_size(vdev
, idx
));
602 int vhost_dev_init(struct vhost_dev
*hdev
, int devfd
)
607 hdev
->control
= devfd
;
609 hdev
->control
= open("/dev/vhost-net", O_RDWR
);
610 if (hdev
->control
< 0) {
614 r
= ioctl(hdev
->control
, VHOST_SET_OWNER
, NULL
);
619 r
= ioctl(hdev
->control
, VHOST_GET_FEATURES
, &features
);
623 hdev
->features
= features
;
625 hdev
->client
.set_memory
= vhost_client_set_memory
;
626 hdev
->client
.sync_dirty_bitmap
= vhost_client_sync_dirty_bitmap
;
627 hdev
->client
.migration_log
= vhost_client_migration_log
;
628 hdev
->mem
= qemu_mallocz(offsetof(struct vhost_memory
, regions
));
631 hdev
->log_enabled
= false;
632 hdev
->started
= false;
633 cpu_register_phys_memory_client(&hdev
->client
);
637 close(hdev
->control
);
641 void vhost_dev_cleanup(struct vhost_dev
*hdev
)
643 cpu_unregister_phys_memory_client(&hdev
->client
);
644 qemu_free(hdev
->mem
);
645 close(hdev
->control
);
648 int vhost_dev_start(struct vhost_dev
*hdev
, VirtIODevice
*vdev
)
652 r
= vhost_dev_set_features(hdev
, hdev
->log_enabled
);
656 r
= ioctl(hdev
->control
, VHOST_SET_MEM_TABLE
, hdev
->mem
);
661 for (i
= 0; i
< hdev
->nvqs
; ++i
) {
662 r
= vhost_virtqueue_init(hdev
,
671 if (hdev
->log_enabled
) {
672 hdev
->log_size
= vhost_get_log_size(hdev
);
673 hdev
->log
= hdev
->log_size
?
674 qemu_mallocz(hdev
->log_size
* sizeof *hdev
->log
) : NULL
;
675 r
= ioctl(hdev
->control
, VHOST_SET_LOG_BASE
,
676 (uint64_t)(unsigned long)hdev
->log
);
683 hdev
->started
= true;
688 vhost_virtqueue_cleanup(hdev
,
697 void vhost_dev_stop(struct vhost_dev
*hdev
, VirtIODevice
*vdev
)
700 for (i
= 0; i
< hdev
->nvqs
; ++i
) {
701 vhost_virtqueue_cleanup(hdev
,
706 vhost_client_sync_dirty_bitmap(&hdev
->client
, 0,
707 (target_phys_addr_t
)~0x0ull
);
708 hdev
->started
= false;
709 qemu_free(hdev
->log
);