]> git.proxmox.com Git - mirror_qemu.git/blame - hw/virtio/vhost.c
Merge remote-tracking branch 'remotes/bonzini/tags/for-upstream' into staging
[mirror_qemu.git] / hw / virtio / vhost.c
CommitLineData
d5970055
MT
1/*
2 * vhost support
3 *
4 * Copyright Red Hat, Inc. 2010
5 *
6 * Authors:
7 * Michael S. Tsirkin <mst@redhat.com>
8 *
9 * This work is licensed under the terms of the GNU GPL, version 2. See
10 * the COPYING file in the top-level directory.
6b620ca3
PB
11 *
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.
d5970055
MT
14 */
15
9b8bfe21 16#include "qemu/osdep.h"
da34e65c 17#include "qapi/error.h"
0d09e41a 18#include "hw/virtio/vhost.h"
d5970055 19#include "hw/hw.h"
5444e768 20#include "qemu/atomic.h"
1de7afc9 21#include "qemu/range.h"
04b7a152 22#include "qemu/error-report.h"
15324404 23#include "qemu/memfd.h"
18658a3c 24#include "standard-headers/linux/vhost_types.h"
022c62cb 25#include "exec/address-spaces.h"
1c819449 26#include "hw/virtio/virtio-bus.h"
04b7a152 27#include "hw/virtio/virtio-access.h"
795c40b8 28#include "migration/blocker.h"
c471ad0e 29#include "sysemu/dma.h"
aa3c40f6 30#include "trace.h"
d5970055 31
162bba7f
MAL
32/* enabled until disconnected backend stabilizes */
33#define _VHOST_DEBUG 1
34
35#ifdef _VHOST_DEBUG
36#define VHOST_OPS_DEBUG(fmt, ...) \
37 do { error_report(fmt ": %s (%d)", ## __VA_ARGS__, \
38 strerror(errno), errno); } while (0)
39#else
40#define VHOST_OPS_DEBUG(fmt, ...) \
41 do { } while (0)
42#endif
43
309750fa 44static struct vhost_log *vhost_log;
15324404 45static struct vhost_log *vhost_log_shm;
309750fa 46
2ce68e4c
IM
47static unsigned int used_memslots;
48static QLIST_HEAD(, vhost_dev) vhost_devices =
49 QLIST_HEAD_INITIALIZER(vhost_devices);
50
51bool vhost_has_free_slot(void)
52{
53 unsigned int slots_limit = ~0U;
54 struct vhost_dev *hdev;
55
56 QLIST_FOREACH(hdev, &vhost_devices, entry) {
57 unsigned int r = hdev->vhost_ops->vhost_backend_memslots_limit(hdev);
58 slots_limit = MIN(slots_limit, r);
59 }
60 return slots_limit > used_memslots;
61}
62
d5970055 63static void vhost_dev_sync_region(struct vhost_dev *dev,
2817b260 64 MemoryRegionSection *section,
d5970055
MT
65 uint64_t mfirst, uint64_t mlast,
66 uint64_t rfirst, uint64_t rlast)
67{
309750fa
JW
68 vhost_log_chunk_t *log = dev->log->log;
69
d5970055
MT
70 uint64_t start = MAX(mfirst, rfirst);
71 uint64_t end = MIN(mlast, rlast);
309750fa
JW
72 vhost_log_chunk_t *from = log + start / VHOST_LOG_CHUNK;
73 vhost_log_chunk_t *to = log + end / VHOST_LOG_CHUNK + 1;
33c5793b 74 uint64_t addr = QEMU_ALIGN_DOWN(start, VHOST_LOG_CHUNK);
d5970055 75
d5970055
MT
76 if (end < start) {
77 return;
78 }
e314672a 79 assert(end / VHOST_LOG_CHUNK < dev->log_size);
fbbaf9ae 80 assert(start / VHOST_LOG_CHUNK < dev->log_size);
e314672a 81
d5970055
MT
82 for (;from < to; ++from) {
83 vhost_log_chunk_t log;
d5970055
MT
84 /* We first check with non-atomic: much cheaper,
85 * and we expect non-dirty to be the common case. */
86 if (!*from) {
0c600ce2 87 addr += VHOST_LOG_CHUNK;
d5970055
MT
88 continue;
89 }
5444e768
PB
90 /* Data must be read atomically. We don't really need barrier semantics
91 * but it's easier to use atomic_* than roll our own. */
92 log = atomic_xchg(from, 0);
747eb78b
NC
93 while (log) {
94 int bit = ctzl(log);
6b37a23d
MT
95 hwaddr page_addr;
96 hwaddr section_offset;
97 hwaddr mr_offset;
6b37a23d
MT
98 page_addr = addr + bit * VHOST_LOG_PAGE;
99 section_offset = page_addr - section->offset_within_address_space;
100 mr_offset = section_offset + section->offset_within_region;
101 memory_region_set_dirty(section->mr, mr_offset, VHOST_LOG_PAGE);
d5970055
MT
102 log &= ~(0x1ull << bit);
103 }
104 addr += VHOST_LOG_CHUNK;
105 }
106}
107
04097f7c 108static int vhost_sync_dirty_bitmap(struct vhost_dev *dev,
2817b260 109 MemoryRegionSection *section,
6b37a23d
MT
110 hwaddr first,
111 hwaddr last)
d5970055 112{
d5970055 113 int i;
6b37a23d
MT
114 hwaddr start_addr;
115 hwaddr end_addr;
04097f7c 116
d5970055
MT
117 if (!dev->log_enabled || !dev->started) {
118 return 0;
119 }
6b37a23d 120 start_addr = section->offset_within_address_space;
052e87b0 121 end_addr = range_get_last(start_addr, int128_get64(section->size));
6b37a23d
MT
122 start_addr = MAX(first, start_addr);
123 end_addr = MIN(last, end_addr);
124
d5970055
MT
125 for (i = 0; i < dev->mem->nregions; ++i) {
126 struct vhost_memory_region *reg = dev->mem->regions + i;
2817b260 127 vhost_dev_sync_region(dev, section, start_addr, end_addr,
d5970055
MT
128 reg->guest_phys_addr,
129 range_get_last(reg->guest_phys_addr,
130 reg->memory_size));
131 }
132 for (i = 0; i < dev->nvqs; ++i) {
133 struct vhost_virtqueue *vq = dev->vqs + i;
240e647a
LH
134
135 if (!vq->used_phys && !vq->used_size) {
136 continue;
137 }
138
2817b260 139 vhost_dev_sync_region(dev, section, start_addr, end_addr, vq->used_phys,
d5970055
MT
140 range_get_last(vq->used_phys, vq->used_size));
141 }
142 return 0;
143}
144
04097f7c
AK
145static void vhost_log_sync(MemoryListener *listener,
146 MemoryRegionSection *section)
147{
148 struct vhost_dev *dev = container_of(listener, struct vhost_dev,
149 memory_listener);
6b37a23d
MT
150 vhost_sync_dirty_bitmap(dev, section, 0x0, ~0x0ULL);
151}
04097f7c 152
6b37a23d
MT
153static void vhost_log_sync_range(struct vhost_dev *dev,
154 hwaddr first, hwaddr last)
155{
156 int i;
157 /* FIXME: this is N^2 in number of sections */
158 for (i = 0; i < dev->n_mem_sections; ++i) {
159 MemoryRegionSection *section = &dev->mem_sections[i];
160 vhost_sync_dirty_bitmap(dev, section, first, last);
161 }
04097f7c
AK
162}
163
d5970055
MT
164static uint64_t vhost_get_log_size(struct vhost_dev *dev)
165{
166 uint64_t log_size = 0;
167 int i;
168 for (i = 0; i < dev->mem->nregions; ++i) {
169 struct vhost_memory_region *reg = dev->mem->regions + i;
170 uint64_t last = range_get_last(reg->guest_phys_addr,
171 reg->memory_size);
172 log_size = MAX(log_size, last / VHOST_LOG_CHUNK + 1);
173 }
174 for (i = 0; i < dev->nvqs; ++i) {
175 struct vhost_virtqueue *vq = dev->vqs + i;
240e647a
LH
176
177 if (!vq->used_phys && !vq->used_size) {
178 continue;
179 }
180
d5970055
MT
181 uint64_t last = vq->used_phys + vq->used_size - 1;
182 log_size = MAX(log_size, last / VHOST_LOG_CHUNK + 1);
183 }
184 return log_size;
185}
15324404
MAL
186
187static struct vhost_log *vhost_log_alloc(uint64_t size, bool share)
309750fa 188{
0f2956f9 189 Error *err = NULL;
15324404
MAL
190 struct vhost_log *log;
191 uint64_t logsize = size * sizeof(*(log->log));
192 int fd = -1;
193
194 log = g_new0(struct vhost_log, 1);
195 if (share) {
196 log->log = qemu_memfd_alloc("vhost-log", logsize,
197 F_SEAL_GROW | F_SEAL_SHRINK | F_SEAL_SEAL,
0f2956f9
MAL
198 &fd, &err);
199 if (err) {
200 error_report_err(err);
201 g_free(log);
202 return NULL;
203 }
15324404
MAL
204 memset(log->log, 0, logsize);
205 } else {
206 log->log = g_malloc0(logsize);
207 }
309750fa
JW
208
209 log->size = size;
210 log->refcnt = 1;
15324404 211 log->fd = fd;
309750fa
JW
212
213 return log;
214}
215
15324404 216static struct vhost_log *vhost_log_get(uint64_t size, bool share)
309750fa 217{
15324404
MAL
218 struct vhost_log *log = share ? vhost_log_shm : vhost_log;
219
220 if (!log || log->size != size) {
221 log = vhost_log_alloc(size, share);
222 if (share) {
223 vhost_log_shm = log;
224 } else {
225 vhost_log = log;
226 }
309750fa 227 } else {
15324404 228 ++log->refcnt;
309750fa
JW
229 }
230
15324404 231 return log;
309750fa
JW
232}
233
234static void vhost_log_put(struct vhost_dev *dev, bool sync)
235{
236 struct vhost_log *log = dev->log;
237
238 if (!log) {
239 return;
240 }
241
242 --log->refcnt;
243 if (log->refcnt == 0) {
244 /* Sync only the range covered by the old log */
245 if (dev->log_size && sync) {
246 vhost_log_sync_range(dev, 0, dev->log_size * VHOST_LOG_CHUNK - 1);
247 }
15324404 248
309750fa 249 if (vhost_log == log) {
15324404 250 g_free(log->log);
309750fa 251 vhost_log = NULL;
15324404
MAL
252 } else if (vhost_log_shm == log) {
253 qemu_memfd_free(log->log, log->size * sizeof(*(log->log)),
254 log->fd);
255 vhost_log_shm = NULL;
309750fa 256 }
15324404 257
309750fa
JW
258 g_free(log);
259 }
5c0ba1be
FF
260
261 dev->log = NULL;
262 dev->log_size = 0;
309750fa 263}
d5970055 264
15324404
MAL
265static bool vhost_dev_log_is_shared(struct vhost_dev *dev)
266{
267 return dev->vhost_ops->vhost_requires_shm_log &&
268 dev->vhost_ops->vhost_requires_shm_log(dev);
269}
270
271static inline void vhost_dev_log_resize(struct vhost_dev *dev, uint64_t size)
d5970055 272{
15324404 273 struct vhost_log *log = vhost_log_get(size, vhost_dev_log_is_shared(dev));
309750fa 274 uint64_t log_base = (uintptr_t)log->log;
6b37a23d 275 int r;
6528499f 276
636f4ddd
MAL
277 /* inform backend of log switching, this must be done before
278 releasing the current log, to ensure no logging is lost */
9a78a5dd 279 r = dev->vhost_ops->vhost_set_log_base(dev, log_base, log);
162bba7f
MAL
280 if (r < 0) {
281 VHOST_OPS_DEBUG("vhost_set_log_base failed");
282 }
283
309750fa 284 vhost_log_put(dev, true);
d5970055
MT
285 dev->log = log;
286 dev->log_size = size;
287}
288
c471ad0e
JW
289static int vhost_dev_has_iommu(struct vhost_dev *dev)
290{
291 VirtIODevice *vdev = dev->vdev;
c471ad0e 292
375f74f4 293 return virtio_host_has_feature(vdev, VIRTIO_F_IOMMU_PLATFORM);
c471ad0e
JW
294}
295
296static void *vhost_memory_map(struct vhost_dev *dev, hwaddr addr,
297 hwaddr *plen, int is_write)
298{
299 if (!vhost_dev_has_iommu(dev)) {
300 return cpu_physical_memory_map(addr, plen, is_write);
301 } else {
302 return (void *)(uintptr_t)addr;
303 }
304}
305
306static void vhost_memory_unmap(struct vhost_dev *dev, void *buffer,
307 hwaddr len, int is_write,
308 hwaddr access_len)
309{
310 if (!vhost_dev_has_iommu(dev)) {
311 cpu_physical_memory_unmap(buffer, len, is_write, access_len);
312 }
313}
f1f9e6c5 314
0ca1fd2d
DDAG
315static int vhost_verify_ring_part_mapping(void *ring_hva,
316 uint64_t ring_gpa,
317 uint64_t ring_size,
318 void *reg_hva,
319 uint64_t reg_gpa,
320 uint64_t reg_size)
f1f9e6c5 321{
0ca1fd2d
DDAG
322 uint64_t hva_ring_offset;
323 uint64_t ring_last = range_get_last(ring_gpa, ring_size);
324 uint64_t reg_last = range_get_last(reg_gpa, reg_size);
f1f9e6c5 325
0ca1fd2d 326 if (ring_last < reg_gpa || ring_gpa > reg_last) {
f1f9e6c5
GK
327 return 0;
328 }
0ca1fd2d
DDAG
329 /* check that whole ring's is mapped */
330 if (ring_last > reg_last) {
331 return -ENOMEM;
f1f9e6c5 332 }
0ca1fd2d
DDAG
333 /* check that ring's MemoryRegion wasn't replaced */
334 hva_ring_offset = ring_gpa - reg_gpa;
335 if (ring_hva != reg_hva + hva_ring_offset) {
336 return -EBUSY;
f1f9e6c5 337 }
0ca1fd2d
DDAG
338
339 return 0;
f1f9e6c5
GK
340}
341
d5970055 342static int vhost_verify_ring_mappings(struct vhost_dev *dev,
0ca1fd2d
DDAG
343 void *reg_hva,
344 uint64_t reg_gpa,
345 uint64_t reg_size)
d5970055 346{
f1f9e6c5 347 int i, j;
8617343f 348 int r = 0;
f1f9e6c5
GK
349 const char *part_name[] = {
350 "descriptor table",
351 "available ring",
352 "used ring"
353 };
8617343f 354
aebbdbee
JW
355 if (vhost_dev_has_iommu(dev)) {
356 return 0;
357 }
358
f1f9e6c5 359 for (i = 0; i < dev->nvqs; ++i) {
d5970055 360 struct vhost_virtqueue *vq = dev->vqs + i;
d5970055 361
fb20fbb7
JH
362 if (vq->desc_phys == 0) {
363 continue;
364 }
365
f1f9e6c5 366 j = 0;
0ca1fd2d
DDAG
367 r = vhost_verify_ring_part_mapping(
368 vq->desc, vq->desc_phys, vq->desc_size,
369 reg_hva, reg_gpa, reg_size);
2fe45ec3 370 if (r) {
f1f9e6c5 371 break;
d5970055 372 }
f1f9e6c5
GK
373
374 j++;
0ca1fd2d 375 r = vhost_verify_ring_part_mapping(
9fac50c8 376 vq->avail, vq->avail_phys, vq->avail_size,
0ca1fd2d 377 reg_hva, reg_gpa, reg_size);
2fe45ec3 378 if (r) {
f1f9e6c5 379 break;
d5970055 380 }
f1f9e6c5
GK
381
382 j++;
0ca1fd2d 383 r = vhost_verify_ring_part_mapping(
9fac50c8 384 vq->used, vq->used_phys, vq->used_size,
0ca1fd2d 385 reg_hva, reg_gpa, reg_size);
2fe45ec3 386 if (r) {
f1f9e6c5 387 break;
d5970055 388 }
f1f9e6c5
GK
389 }
390
391 if (r == -ENOMEM) {
392 error_report("Unable to map %s for ring %d", part_name[j], i);
393 } else if (r == -EBUSY) {
394 error_report("%s relocated for ring %d", part_name[j], i);
d5970055 395 }
8617343f 396 return r;
d5970055
MT
397}
398
988a2775 399static bool vhost_section(struct vhost_dev *dev, MemoryRegionSection *section)
af603142 400{
aa3c40f6
DDAG
401 bool result;
402 bool log_dirty = memory_region_get_dirty_log_mask(section->mr) &
403 ~(1 << DIRTY_MEMORY_MIGRATION);
404 result = memory_region_is_ram(section->mr) &&
d56ec1e9 405 !memory_region_is_rom(section->mr);
aa3c40f6
DDAG
406
407 /* Vhost doesn't handle any block which is doing dirty-tracking other
408 * than migration; this typically fires on VGA areas.
409 */
410 result &= !log_dirty;
411
988a2775
TB
412 if (result && dev->vhost_ops->vhost_backend_mem_section_filter) {
413 result &=
414 dev->vhost_ops->vhost_backend_mem_section_filter(dev, section);
415 }
416
aa3c40f6
DDAG
417 trace_vhost_section(section->mr->name, result);
418 return result;
af603142
NB
419}
420
421static void vhost_begin(MemoryListener *listener)
422{
423 struct vhost_dev *dev = container_of(listener, struct vhost_dev,
424 memory_listener);
c44317ef
DDAG
425 dev->tmp_sections = NULL;
426 dev->n_tmp_sections = 0;
af603142 427}
d5970055 428
af603142
NB
429static void vhost_commit(MemoryListener *listener)
430{
431 struct vhost_dev *dev = container_of(listener, struct vhost_dev,
432 memory_listener);
c44317ef
DDAG
433 MemoryRegionSection *old_sections;
434 int n_old_sections;
af603142 435 uint64_t log_size;
ade6d081 436 size_t regions_size;
af603142 437 int r;
0ca1fd2d 438 int i;
ade6d081 439 bool changed = false;
af603142 440
ade6d081
DDAG
441 /* Note we can be called before the device is started, but then
442 * starting the device calls set_mem_table, so we need to have
443 * built the data structures.
444 */
c44317ef
DDAG
445 old_sections = dev->mem_sections;
446 n_old_sections = dev->n_mem_sections;
447 dev->mem_sections = dev->tmp_sections;
448 dev->n_mem_sections = dev->n_tmp_sections;
449
ade6d081
DDAG
450 if (dev->n_mem_sections != n_old_sections) {
451 changed = true;
452 } else {
453 /* Same size, lets check the contents */
454 changed = n_old_sections && memcmp(dev->mem_sections, old_sections,
455 n_old_sections * sizeof(old_sections[0])) != 0;
af603142 456 }
ade6d081
DDAG
457
458 trace_vhost_commit(dev->started, changed);
459 if (!changed) {
c44317ef 460 goto out;
d5970055 461 }
ade6d081
DDAG
462
463 /* Rebuild the regions list from the new sections list */
464 regions_size = offsetof(struct vhost_memory, regions) +
465 dev->n_mem_sections * sizeof dev->mem->regions[0];
466 dev->mem = g_realloc(dev->mem, regions_size);
467 dev->mem->nregions = dev->n_mem_sections;
468 used_memslots = dev->mem->nregions;
469 for (i = 0; i < dev->n_mem_sections; i++) {
470 struct vhost_memory_region *cur_vmr = dev->mem->regions + i;
471 struct MemoryRegionSection *mrs = dev->mem_sections + i;
472
473 cur_vmr->guest_phys_addr = mrs->offset_within_address_space;
474 cur_vmr->memory_size = int128_get64(mrs->size);
475 cur_vmr->userspace_addr =
476 (uintptr_t)memory_region_get_ram_ptr(mrs->mr) +
477 mrs->offset_within_region;
478 cur_vmr->flags_padding = 0;
479 }
480
481 if (!dev->started) {
c44317ef 482 goto out;
af603142 483 }
d5970055 484
0ca1fd2d
DDAG
485 for (i = 0; i < dev->mem->nregions; i++) {
486 if (vhost_verify_ring_mappings(dev,
487 (void *)(uintptr_t)dev->mem->regions[i].userspace_addr,
488 dev->mem->regions[i].guest_phys_addr,
489 dev->mem->regions[i].memory_size)) {
490 error_report("Verify ring failure on region %d", i);
491 abort();
492 }
d5970055
MT
493 }
494
495 if (!dev->log_enabled) {
21e70425 496 r = dev->vhost_ops->vhost_set_mem_table(dev, dev->mem);
162bba7f
MAL
497 if (r < 0) {
498 VHOST_OPS_DEBUG("vhost_set_mem_table failed");
499 }
c44317ef 500 goto out;
d5970055
MT
501 }
502 log_size = vhost_get_log_size(dev);
503 /* We allocate an extra 4K bytes to log,
504 * to reduce the * number of reallocations. */
505#define VHOST_LOG_BUFFER (0x1000 / sizeof *dev->log)
506 /* To log more, must increase log size before table update. */
507 if (dev->log_size < log_size) {
508 vhost_dev_log_resize(dev, log_size + VHOST_LOG_BUFFER);
509 }
21e70425 510 r = dev->vhost_ops->vhost_set_mem_table(dev, dev->mem);
162bba7f
MAL
511 if (r < 0) {
512 VHOST_OPS_DEBUG("vhost_set_mem_table failed");
513 }
d5970055
MT
514 /* To log less, can only decrease log size after table update. */
515 if (dev->log_size > log_size + VHOST_LOG_BUFFER) {
516 vhost_dev_log_resize(dev, log_size);
517 }
c44317ef
DDAG
518
519out:
520 /* Deref the old list of sections, this must happen _after_ the
521 * vhost_set_mem_table to ensure the client isn't still using the
522 * section we're about to unref.
523 */
524 while (n_old_sections--) {
525 memory_region_unref(old_sections[n_old_sections].mr);
526 }
527 g_free(old_sections);
528 return;
529}
530
48d7c975
DDAG
531/* Adds the section data to the tmp_section structure.
532 * It relies on the listener calling us in memory address order
533 * and for each region (via the _add and _nop methods) to
534 * join neighbours.
535 */
536static void vhost_region_add_section(struct vhost_dev *dev,
537 MemoryRegionSection *section)
c44317ef 538{
48d7c975
DDAG
539 bool need_add = true;
540 uint64_t mrs_size = int128_get64(section->size);
541 uint64_t mrs_gpa = section->offset_within_address_space;
542 uintptr_t mrs_host = (uintptr_t)memory_region_get_ram_ptr(section->mr) +
543 section->offset_within_region;
c1ece84e
DDAG
544 RAMBlock *mrs_rb = section->mr->ram_block;
545 size_t mrs_page = qemu_ram_pagesize(mrs_rb);
48d7c975
DDAG
546
547 trace_vhost_region_add_section(section->mr->name, mrs_gpa, mrs_size,
548 mrs_host);
549
c1ece84e
DDAG
550 /* Round the section to it's page size */
551 /* First align the start down to a page boundary */
552 uint64_t alignage = mrs_host & (mrs_page - 1);
553 if (alignage) {
554 mrs_host -= alignage;
555 mrs_size += alignage;
556 mrs_gpa -= alignage;
557 }
558 /* Now align the size up to a page boundary */
559 alignage = mrs_size & (mrs_page - 1);
560 if (alignage) {
561 mrs_size += mrs_page - alignage;
562 }
563 trace_vhost_region_add_section_aligned(section->mr->name, mrs_gpa, mrs_size,
564 mrs_host);
565
48d7c975
DDAG
566 if (dev->n_tmp_sections) {
567 /* Since we already have at least one section, lets see if
568 * this extends it; since we're scanning in order, we only
569 * have to look at the last one, and the FlatView that calls
570 * us shouldn't have overlaps.
571 */
572 MemoryRegionSection *prev_sec = dev->tmp_sections +
573 (dev->n_tmp_sections - 1);
574 uint64_t prev_gpa_start = prev_sec->offset_within_address_space;
575 uint64_t prev_size = int128_get64(prev_sec->size);
576 uint64_t prev_gpa_end = range_get_last(prev_gpa_start, prev_size);
577 uint64_t prev_host_start =
578 (uintptr_t)memory_region_get_ram_ptr(prev_sec->mr) +
579 prev_sec->offset_within_region;
580 uint64_t prev_host_end = range_get_last(prev_host_start, prev_size);
581
c1ece84e
DDAG
582 if (mrs_gpa <= (prev_gpa_end + 1)) {
583 /* OK, looks like overlapping/intersecting - it's possible that
584 * the rounding to page sizes has made them overlap, but they should
585 * match up in the same RAMBlock if they do.
586 */
587 if (mrs_gpa < prev_gpa_start) {
588 error_report("%s:Section rounded to %"PRIx64
589 " prior to previous %"PRIx64,
590 __func__, mrs_gpa, prev_gpa_start);
591 /* A way to cleanly fail here would be better */
592 return;
593 }
594 /* Offset from the start of the previous GPA to this GPA */
595 size_t offset = mrs_gpa - prev_gpa_start;
596
597 if (prev_host_start + offset == mrs_host &&
598 section->mr == prev_sec->mr &&
599 (!dev->vhost_ops->vhost_backend_can_merge ||
600 dev->vhost_ops->vhost_backend_can_merge(dev,
48d7c975
DDAG
601 mrs_host, mrs_size,
602 prev_host_start, prev_size))) {
c1ece84e
DDAG
603 uint64_t max_end = MAX(prev_host_end, mrs_host + mrs_size);
604 need_add = false;
605 prev_sec->offset_within_address_space =
606 MIN(prev_gpa_start, mrs_gpa);
607 prev_sec->offset_within_region =
608 MIN(prev_host_start, mrs_host) -
609 (uintptr_t)memory_region_get_ram_ptr(prev_sec->mr);
610 prev_sec->size = int128_make64(max_end - MIN(prev_host_start,
611 mrs_host));
612 trace_vhost_region_add_section_merge(section->mr->name,
613 int128_get64(prev_sec->size),
614 prev_sec->offset_within_address_space,
615 prev_sec->offset_within_region);
616 } else {
e7b94a84
DDAG
617 /* adjoining regions are fine, but overlapping ones with
618 * different blocks/offsets shouldn't happen
619 */
620 if (mrs_gpa != prev_gpa_end + 1) {
621 error_report("%s: Overlapping but not coherent sections "
622 "at %"PRIx64,
623 __func__, mrs_gpa);
624 return;
625 }
c1ece84e 626 }
48d7c975
DDAG
627 }
628 }
629
630 if (need_add) {
631 ++dev->n_tmp_sections;
632 dev->tmp_sections = g_renew(MemoryRegionSection, dev->tmp_sections,
633 dev->n_tmp_sections);
634 dev->tmp_sections[dev->n_tmp_sections - 1] = *section;
635 /* The flatview isn't stable and we don't use it, making it NULL
636 * means we can memcmp the list.
637 */
638 dev->tmp_sections[dev->n_tmp_sections - 1].fv = NULL;
639 memory_region_ref(section->mr);
640 }
50c1e149
AK
641}
642
938eeb64
DDAG
643/* Used for both add and nop callbacks */
644static void vhost_region_addnop(MemoryListener *listener,
645 MemoryRegionSection *section)
04097f7c 646{
2817b260
AK
647 struct vhost_dev *dev = container_of(listener, struct vhost_dev,
648 memory_listener);
649
988a2775 650 if (!vhost_section(dev, section)) {
c49450b9
AK
651 return;
652 }
48d7c975 653 vhost_region_add_section(dev, section);
04097f7c
AK
654}
655
375f74f4
JW
656static void vhost_iommu_unmap_notify(IOMMUNotifier *n, IOMMUTLBEntry *iotlb)
657{
658 struct vhost_iommu *iommu = container_of(n, struct vhost_iommu, n);
659 struct vhost_dev *hdev = iommu->hdev;
660 hwaddr iova = iotlb->iova + iommu->iommu_offset;
661
020e571b
MC
662 if (vhost_backend_invalidate_device_iotlb(hdev, iova,
663 iotlb->addr_mask + 1)) {
375f74f4
JW
664 error_report("Fail to invalidate device iotlb");
665 }
666}
667
668static void vhost_iommu_region_add(MemoryListener *listener,
669 MemoryRegionSection *section)
670{
671 struct vhost_dev *dev = container_of(listener, struct vhost_dev,
672 iommu_listener);
673 struct vhost_iommu *iommu;
698feb5e 674 Int128 end;
cb1efcf4 675 int iommu_idx;
388a86df 676 IOMMUMemoryRegion *iommu_mr;
375f74f4
JW
677
678 if (!memory_region_is_iommu(section->mr)) {
679 return;
680 }
681
388a86df
TB
682 iommu_mr = IOMMU_MEMORY_REGION(section->mr);
683
375f74f4 684 iommu = g_malloc0(sizeof(*iommu));
698feb5e
PX
685 end = int128_add(int128_make64(section->offset_within_region),
686 section->size);
687 end = int128_sub(end, int128_one());
cb1efcf4
PM
688 iommu_idx = memory_region_iommu_attrs_to_index(iommu_mr,
689 MEMTXATTRS_UNSPECIFIED);
698feb5e
PX
690 iommu_notifier_init(&iommu->n, vhost_iommu_unmap_notify,
691 IOMMU_NOTIFIER_UNMAP,
692 section->offset_within_region,
cb1efcf4
PM
693 int128_get64(end),
694 iommu_idx);
375f74f4
JW
695 iommu->mr = section->mr;
696 iommu->iommu_offset = section->offset_within_address_space -
697 section->offset_within_region;
698 iommu->hdev = dev;
699 memory_region_register_iommu_notifier(section->mr, &iommu->n);
700 QLIST_INSERT_HEAD(&dev->iommu_list, iommu, iommu_next);
701 /* TODO: can replay help performance here? */
702}
703
704static void vhost_iommu_region_del(MemoryListener *listener,
705 MemoryRegionSection *section)
706{
707 struct vhost_dev *dev = container_of(listener, struct vhost_dev,
708 iommu_listener);
709 struct vhost_iommu *iommu;
710
711 if (!memory_region_is_iommu(section->mr)) {
712 return;
713 }
714
715 QLIST_FOREACH(iommu, &dev->iommu_list, iommu_next) {
698feb5e
PX
716 if (iommu->mr == section->mr &&
717 iommu->n.start == section->offset_within_region) {
375f74f4
JW
718 memory_region_unregister_iommu_notifier(iommu->mr,
719 &iommu->n);
720 QLIST_REMOVE(iommu, iommu_next);
721 g_free(iommu);
722 break;
723 }
724 }
725}
726
d5970055
MT
727static int vhost_virtqueue_set_addr(struct vhost_dev *dev,
728 struct vhost_virtqueue *vq,
729 unsigned idx, bool enable_log)
730{
731 struct vhost_vring_addr addr = {
732 .index = idx,
2b3af999
SW
733 .desc_user_addr = (uint64_t)(unsigned long)vq->desc,
734 .avail_user_addr = (uint64_t)(unsigned long)vq->avail,
735 .used_user_addr = (uint64_t)(unsigned long)vq->used,
d5970055
MT
736 .log_guest_addr = vq->used_phys,
737 .flags = enable_log ? (1 << VHOST_VRING_F_LOG) : 0,
738 };
21e70425 739 int r = dev->vhost_ops->vhost_set_vring_addr(dev, &addr);
d5970055 740 if (r < 0) {
c6409692 741 VHOST_OPS_DEBUG("vhost_set_vring_addr failed");
d5970055
MT
742 return -errno;
743 }
744 return 0;
745}
746
c471ad0e
JW
747static int vhost_dev_set_features(struct vhost_dev *dev,
748 bool enable_log)
d5970055
MT
749{
750 uint64_t features = dev->acked_features;
751 int r;
752 if (enable_log) {
9a2ba823 753 features |= 0x1ULL << VHOST_F_LOG_ALL;
d5970055 754 }
21e70425 755 r = dev->vhost_ops->vhost_set_features(dev, features);
c6409692
MAL
756 if (r < 0) {
757 VHOST_OPS_DEBUG("vhost_set_features failed");
758 }
d5970055
MT
759 return r < 0 ? -errno : 0;
760}
761
762static int vhost_dev_set_log(struct vhost_dev *dev, bool enable_log)
763{
162bba7f 764 int r, i, idx;
d5970055
MT
765 r = vhost_dev_set_features(dev, enable_log);
766 if (r < 0) {
767 goto err_features;
768 }
769 for (i = 0; i < dev->nvqs; ++i) {
25a2a920
TC
770 idx = dev->vhost_ops->vhost_get_vq_index(dev, dev->vq_index + i);
771 r = vhost_virtqueue_set_addr(dev, dev->vqs + i, idx,
d5970055
MT
772 enable_log);
773 if (r < 0) {
774 goto err_vq;
775 }
776 }
777 return 0;
778err_vq:
779 for (; i >= 0; --i) {
25a2a920 780 idx = dev->vhost_ops->vhost_get_vq_index(dev, dev->vq_index + i);
162bba7f
MAL
781 vhost_virtqueue_set_addr(dev, dev->vqs + i, idx,
782 dev->log_enabled);
d5970055 783 }
162bba7f 784 vhost_dev_set_features(dev, dev->log_enabled);
d5970055
MT
785err_features:
786 return r;
787}
788
04097f7c 789static int vhost_migration_log(MemoryListener *listener, int enable)
d5970055 790{
04097f7c
AK
791 struct vhost_dev *dev = container_of(listener, struct vhost_dev,
792 memory_listener);
d5970055
MT
793 int r;
794 if (!!enable == dev->log_enabled) {
795 return 0;
796 }
797 if (!dev->started) {
798 dev->log_enabled = enable;
799 return 0;
800 }
801 if (!enable) {
802 r = vhost_dev_set_log(dev, false);
803 if (r < 0) {
804 return r;
805 }
309750fa 806 vhost_log_put(dev, false);
d5970055
MT
807 } else {
808 vhost_dev_log_resize(dev, vhost_get_log_size(dev));
809 r = vhost_dev_set_log(dev, true);
810 if (r < 0) {
811 return r;
812 }
813 }
814 dev->log_enabled = enable;
815 return 0;
816}
817
04097f7c
AK
818static void vhost_log_global_start(MemoryListener *listener)
819{
820 int r;
821
822 r = vhost_migration_log(listener, true);
823 if (r < 0) {
824 abort();
825 }
826}
827
828static void vhost_log_global_stop(MemoryListener *listener)
829{
830 int r;
831
832 r = vhost_migration_log(listener, false);
833 if (r < 0) {
834 abort();
835 }
836}
837
838static void vhost_log_start(MemoryListener *listener,
b2dfd71c
PB
839 MemoryRegionSection *section,
840 int old, int new)
04097f7c
AK
841{
842 /* FIXME: implement */
843}
844
845static void vhost_log_stop(MemoryListener *listener,
b2dfd71c
PB
846 MemoryRegionSection *section,
847 int old, int new)
04097f7c
AK
848{
849 /* FIXME: implement */
850}
851
46f70ff1
GK
852/* The vhost driver natively knows how to handle the vrings of non
853 * cross-endian legacy devices and modern devices. Only legacy devices
854 * exposed to a bi-endian guest may require the vhost driver to use a
855 * specific endianness.
856 */
a122ab24
GK
857static inline bool vhost_needs_vring_endian(VirtIODevice *vdev)
858{
e5848123
GK
859 if (virtio_vdev_has_feature(vdev, VIRTIO_F_VERSION_1)) {
860 return false;
861 }
a122ab24 862#ifdef HOST_WORDS_BIGENDIAN
46f70ff1 863 return vdev->device_endian == VIRTIO_DEVICE_ENDIAN_LITTLE;
a122ab24 864#else
46f70ff1 865 return vdev->device_endian == VIRTIO_DEVICE_ENDIAN_BIG;
a122ab24 866#endif
a122ab24
GK
867}
868
04b7a152
GK
869static int vhost_virtqueue_set_vring_endian_legacy(struct vhost_dev *dev,
870 bool is_big_endian,
871 int vhost_vq_index)
872{
873 struct vhost_vring_state s = {
874 .index = vhost_vq_index,
875 .num = is_big_endian
876 };
877
21e70425 878 if (!dev->vhost_ops->vhost_set_vring_endian(dev, &s)) {
04b7a152
GK
879 return 0;
880 }
881
c6409692 882 VHOST_OPS_DEBUG("vhost_set_vring_endian failed");
04b7a152
GK
883 if (errno == ENOTTY) {
884 error_report("vhost does not support cross-endian");
885 return -ENOSYS;
886 }
887
888 return -errno;
889}
890
c471ad0e
JW
891static int vhost_memory_region_lookup(struct vhost_dev *hdev,
892 uint64_t gpa, uint64_t *uaddr,
893 uint64_t *len)
894{
895 int i;
896
897 for (i = 0; i < hdev->mem->nregions; i++) {
898 struct vhost_memory_region *reg = hdev->mem->regions + i;
899
900 if (gpa >= reg->guest_phys_addr &&
901 reg->guest_phys_addr + reg->memory_size > gpa) {
902 *uaddr = reg->userspace_addr + gpa - reg->guest_phys_addr;
903 *len = reg->guest_phys_addr + reg->memory_size - gpa;
904 return 0;
905 }
906 }
907
908 return -EFAULT;
909}
910
fc58bd0d 911int vhost_device_iotlb_miss(struct vhost_dev *dev, uint64_t iova, int write)
c471ad0e
JW
912{
913 IOMMUTLBEntry iotlb;
914 uint64_t uaddr, len;
fc58bd0d 915 int ret = -EFAULT;
c471ad0e
JW
916
917 rcu_read_lock();
918
ffcbbe72
PX
919 trace_vhost_iotlb_miss(dev, 1);
920
c471ad0e 921 iotlb = address_space_get_iotlb_entry(dev->vdev->dma_as,
7446eb07
PM
922 iova, write,
923 MEMTXATTRS_UNSPECIFIED);
c471ad0e 924 if (iotlb.target_as != NULL) {
fc58bd0d
MC
925 ret = vhost_memory_region_lookup(dev, iotlb.translated_addr,
926 &uaddr, &len);
927 if (ret) {
ffcbbe72 928 trace_vhost_iotlb_miss(dev, 3);
c471ad0e
JW
929 error_report("Fail to lookup the translated address "
930 "%"PRIx64, iotlb.translated_addr);
931 goto out;
932 }
933
934 len = MIN(iotlb.addr_mask + 1, len);
935 iova = iova & ~iotlb.addr_mask;
936
020e571b
MC
937 ret = vhost_backend_update_device_iotlb(dev, iova, uaddr,
938 len, iotlb.perm);
fc58bd0d 939 if (ret) {
ffcbbe72 940 trace_vhost_iotlb_miss(dev, 4);
c471ad0e
JW
941 error_report("Fail to update device iotlb");
942 goto out;
943 }
944 }
ffcbbe72
PX
945
946 trace_vhost_iotlb_miss(dev, 2);
947
c471ad0e
JW
948out:
949 rcu_read_unlock();
fc58bd0d
MC
950
951 return ret;
c471ad0e
JW
952}
953
f56a1247 954static int vhost_virtqueue_start(struct vhost_dev *dev,
d5970055
MT
955 struct VirtIODevice *vdev,
956 struct vhost_virtqueue *vq,
957 unsigned idx)
958{
96a3d98d
JW
959 BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(vdev)));
960 VirtioBusState *vbus = VIRTIO_BUS(qbus);
961 VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(vbus);
a8170e5e 962 hwaddr s, l, a;
d5970055 963 int r;
21e70425 964 int vhost_vq_index = dev->vhost_ops->vhost_get_vq_index(dev, idx);
d5970055 965 struct vhost_vring_file file = {
a9f98bb5 966 .index = vhost_vq_index
d5970055
MT
967 };
968 struct vhost_vring_state state = {
a9f98bb5 969 .index = vhost_vq_index
d5970055
MT
970 };
971 struct VirtQueue *vvq = virtio_get_queue(vdev, idx);
972
fb20fbb7
JH
973 a = virtio_queue_get_desc_addr(vdev, idx);
974 if (a == 0) {
975 /* Queue might not be ready for start */
976 return 0;
977 }
a9f98bb5 978
d5970055 979 vq->num = state.num = virtio_queue_get_num(vdev, idx);
21e70425 980 r = dev->vhost_ops->vhost_set_vring_num(dev, &state);
d5970055 981 if (r) {
c6409692 982 VHOST_OPS_DEBUG("vhost_set_vring_num failed");
d5970055
MT
983 return -errno;
984 }
985
986 state.num = virtio_queue_get_last_avail_idx(vdev, idx);
21e70425 987 r = dev->vhost_ops->vhost_set_vring_base(dev, &state);
d5970055 988 if (r) {
c6409692 989 VHOST_OPS_DEBUG("vhost_set_vring_base failed");
d5970055
MT
990 return -errno;
991 }
992
e5848123 993 if (vhost_needs_vring_endian(vdev)) {
04b7a152
GK
994 r = vhost_virtqueue_set_vring_endian_legacy(dev,
995 virtio_is_big_endian(vdev),
996 vhost_vq_index);
997 if (r) {
998 return -errno;
999 }
1000 }
1001
f1f9e6c5 1002 vq->desc_size = s = l = virtio_queue_get_desc_size(vdev, idx);
fb20fbb7 1003 vq->desc_phys = a;
c471ad0e 1004 vq->desc = vhost_memory_map(dev, a, &l, 0);
d5970055
MT
1005 if (!vq->desc || l != s) {
1006 r = -ENOMEM;
1007 goto fail_alloc_desc;
1008 }
f1f9e6c5
GK
1009 vq->avail_size = s = l = virtio_queue_get_avail_size(vdev, idx);
1010 vq->avail_phys = a = virtio_queue_get_avail_addr(vdev, idx);
c471ad0e 1011 vq->avail = vhost_memory_map(dev, a, &l, 0);
d5970055
MT
1012 if (!vq->avail || l != s) {
1013 r = -ENOMEM;
1014 goto fail_alloc_avail;
1015 }
1016 vq->used_size = s = l = virtio_queue_get_used_size(vdev, idx);
1017 vq->used_phys = a = virtio_queue_get_used_addr(vdev, idx);
c471ad0e 1018 vq->used = vhost_memory_map(dev, a, &l, 1);
d5970055
MT
1019 if (!vq->used || l != s) {
1020 r = -ENOMEM;
1021 goto fail_alloc_used;
1022 }
1023
a9f98bb5 1024 r = vhost_virtqueue_set_addr(dev, vq, vhost_vq_index, dev->log_enabled);
d5970055
MT
1025 if (r < 0) {
1026 r = -errno;
1027 goto fail_alloc;
1028 }
a9f98bb5 1029
d5970055 1030 file.fd = event_notifier_get_fd(virtio_queue_get_host_notifier(vvq));
21e70425 1031 r = dev->vhost_ops->vhost_set_vring_kick(dev, &file);
d5970055 1032 if (r) {
c6409692 1033 VHOST_OPS_DEBUG("vhost_set_vring_kick failed");
c8852121 1034 r = -errno;
d5970055
MT
1035 goto fail_kick;
1036 }
1037
f56a1247
MT
1038 /* Clear and discard previous events if any. */
1039 event_notifier_test_and_clear(&vq->masked_notifier);
d5970055 1040
5669655a
VK
1041 /* Init vring in unmasked state, unless guest_notifier_mask
1042 * will do it later.
1043 */
1044 if (!vdev->use_guest_notifier_mask) {
1045 /* TODO: check and handle errors. */
1046 vhost_virtqueue_mask(dev, vdev, idx, false);
1047 }
1048
96a3d98d
JW
1049 if (k->query_guest_notifiers &&
1050 k->query_guest_notifiers(qbus->parent) &&
1051 virtio_queue_vector(vdev, idx) == VIRTIO_NO_VECTOR) {
1052 file.fd = -1;
1053 r = dev->vhost_ops->vhost_set_vring_call(dev, &file);
1054 if (r) {
1055 goto fail_vector;
1056 }
1057 }
1058
d5970055
MT
1059 return 0;
1060
96a3d98d 1061fail_vector:
d5970055 1062fail_kick:
d5970055 1063fail_alloc:
c471ad0e
JW
1064 vhost_memory_unmap(dev, vq->used, virtio_queue_get_used_size(vdev, idx),
1065 0, 0);
d5970055 1066fail_alloc_used:
c471ad0e
JW
1067 vhost_memory_unmap(dev, vq->avail, virtio_queue_get_avail_size(vdev, idx),
1068 0, 0);
d5970055 1069fail_alloc_avail:
c471ad0e
JW
1070 vhost_memory_unmap(dev, vq->desc, virtio_queue_get_desc_size(vdev, idx),
1071 0, 0);
d5970055
MT
1072fail_alloc_desc:
1073 return r;
1074}
1075
f56a1247 1076static void vhost_virtqueue_stop(struct vhost_dev *dev,
d5970055
MT
1077 struct VirtIODevice *vdev,
1078 struct vhost_virtqueue *vq,
1079 unsigned idx)
1080{
21e70425 1081 int vhost_vq_index = dev->vhost_ops->vhost_get_vq_index(dev, idx);
d5970055 1082 struct vhost_vring_state state = {
04b7a152 1083 .index = vhost_vq_index,
d5970055
MT
1084 };
1085 int r;
fb20fbb7 1086
fa4ae4be 1087 if (virtio_queue_get_desc_addr(vdev, idx) == 0) {
fb20fbb7
JH
1088 /* Don't stop the virtqueue which might have not been started */
1089 return;
1090 }
fc57fd99 1091
21e70425 1092 r = dev->vhost_ops->vhost_get_vring_base(dev, &state);
d5970055 1093 if (r < 0) {
31618958 1094 VHOST_OPS_DEBUG("vhost VQ %u ring restore failed: %d", idx, r);
2ae39a11
MC
1095 /* Connection to the backend is broken, so let's sync internal
1096 * last avail idx to the device used idx.
1097 */
1098 virtio_queue_restore_last_avail_idx(vdev, idx);
499c5579
MAL
1099 } else {
1100 virtio_queue_set_last_avail_idx(vdev, idx, state.num);
d5970055 1101 }
3561ba14 1102 virtio_queue_invalidate_signalled_used(vdev, idx);
aa94d521 1103 virtio_queue_update_used_idx(vdev, idx);
04b7a152
GK
1104
1105 /* In the cross-endian case, we need to reset the vring endianness to
1106 * native as legacy devices expect so by default.
1107 */
e5848123 1108 if (vhost_needs_vring_endian(vdev)) {
162bba7f
MAL
1109 vhost_virtqueue_set_vring_endian_legacy(dev,
1110 !virtio_is_big_endian(vdev),
1111 vhost_vq_index);
04b7a152
GK
1112 }
1113
c471ad0e
JW
1114 vhost_memory_unmap(dev, vq->used, virtio_queue_get_used_size(vdev, idx),
1115 1, virtio_queue_get_used_size(vdev, idx));
1116 vhost_memory_unmap(dev, vq->avail, virtio_queue_get_avail_size(vdev, idx),
1117 0, virtio_queue_get_avail_size(vdev, idx));
1118 vhost_memory_unmap(dev, vq->desc, virtio_queue_get_desc_size(vdev, idx),
1119 0, virtio_queue_get_desc_size(vdev, idx));
d5970055
MT
1120}
1121
80a1ea37
AK
1122static void vhost_eventfd_add(MemoryListener *listener,
1123 MemoryRegionSection *section,
753d5e14 1124 bool match_data, uint64_t data, EventNotifier *e)
80a1ea37
AK
1125{
1126}
1127
1128static void vhost_eventfd_del(MemoryListener *listener,
1129 MemoryRegionSection *section,
753d5e14 1130 bool match_data, uint64_t data, EventNotifier *e)
80a1ea37
AK
1131{
1132}
1133
69e87b32
JW
1134static int vhost_virtqueue_set_busyloop_timeout(struct vhost_dev *dev,
1135 int n, uint32_t timeout)
1136{
1137 int vhost_vq_index = dev->vhost_ops->vhost_get_vq_index(dev, n);
1138 struct vhost_vring_state state = {
1139 .index = vhost_vq_index,
1140 .num = timeout,
1141 };
1142 int r;
1143
1144 if (!dev->vhost_ops->vhost_set_vring_busyloop_timeout) {
1145 return -EINVAL;
1146 }
1147
1148 r = dev->vhost_ops->vhost_set_vring_busyloop_timeout(dev, &state);
1149 if (r) {
c6409692 1150 VHOST_OPS_DEBUG("vhost_set_vring_busyloop_timeout failed");
69e87b32
JW
1151 return r;
1152 }
1153
1154 return 0;
1155}
1156
f56a1247
MT
1157static int vhost_virtqueue_init(struct vhost_dev *dev,
1158 struct vhost_virtqueue *vq, int n)
1159{
21e70425 1160 int vhost_vq_index = dev->vhost_ops->vhost_get_vq_index(dev, n);
f56a1247 1161 struct vhost_vring_file file = {
b931bfbf 1162 .index = vhost_vq_index,
f56a1247
MT
1163 };
1164 int r = event_notifier_init(&vq->masked_notifier, 0);
1165 if (r < 0) {
1166 return r;
1167 }
1168
1169 file.fd = event_notifier_get_fd(&vq->masked_notifier);
21e70425 1170 r = dev->vhost_ops->vhost_set_vring_call(dev, &file);
f56a1247 1171 if (r) {
c6409692 1172 VHOST_OPS_DEBUG("vhost_set_vring_call failed");
f56a1247
MT
1173 r = -errno;
1174 goto fail_call;
1175 }
c471ad0e
JW
1176
1177 vq->dev = dev;
1178
f56a1247
MT
1179 return 0;
1180fail_call:
1181 event_notifier_cleanup(&vq->masked_notifier);
1182 return r;
1183}
1184
1185static void vhost_virtqueue_cleanup(struct vhost_virtqueue *vq)
1186{
1187 event_notifier_cleanup(&vq->masked_notifier);
1188}
1189
81647a65 1190int vhost_dev_init(struct vhost_dev *hdev, void *opaque,
69e87b32 1191 VhostBackendType backend_type, uint32_t busyloop_timeout)
d5970055
MT
1192{
1193 uint64_t features;
a06db3ec 1194 int i, r, n_initialized_vqs = 0;
fe44dc91 1195 Error *local_err = NULL;
81647a65 1196
c471ad0e 1197 hdev->vdev = NULL;
d2fc4402
MAL
1198 hdev->migration_blocker = NULL;
1199
7cb8a9b9
MAL
1200 r = vhost_set_backend_type(hdev, backend_type);
1201 assert(r >= 0);
1a1bfac9 1202
7cb8a9b9
MAL
1203 r = hdev->vhost_ops->vhost_backend_init(hdev, opaque);
1204 if (r < 0) {
1205 goto fail;
24d1eb33
NN
1206 }
1207
21e70425 1208 r = hdev->vhost_ops->vhost_set_owner(hdev);
d5970055 1209 if (r < 0) {
c6409692 1210 VHOST_OPS_DEBUG("vhost_set_owner failed");
d5970055
MT
1211 goto fail;
1212 }
1213
21e70425 1214 r = hdev->vhost_ops->vhost_get_features(hdev, &features);
d5970055 1215 if (r < 0) {
c6409692 1216 VHOST_OPS_DEBUG("vhost_get_features failed");
d5970055
MT
1217 goto fail;
1218 }
f56a1247 1219
a06db3ec 1220 for (i = 0; i < hdev->nvqs; ++i, ++n_initialized_vqs) {
b931bfbf 1221 r = vhost_virtqueue_init(hdev, hdev->vqs + i, hdev->vq_index + i);
f56a1247 1222 if (r < 0) {
a06db3ec 1223 goto fail;
f56a1247
MT
1224 }
1225 }
69e87b32
JW
1226
1227 if (busyloop_timeout) {
1228 for (i = 0; i < hdev->nvqs; ++i) {
1229 r = vhost_virtqueue_set_busyloop_timeout(hdev, hdev->vq_index + i,
1230 busyloop_timeout);
1231 if (r < 0) {
1232 goto fail_busyloop;
1233 }
1234 }
1235 }
1236
d5970055
MT
1237 hdev->features = features;
1238
04097f7c 1239 hdev->memory_listener = (MemoryListener) {
50c1e149
AK
1240 .begin = vhost_begin,
1241 .commit = vhost_commit,
938eeb64
DDAG
1242 .region_add = vhost_region_addnop,
1243 .region_nop = vhost_region_addnop,
04097f7c
AK
1244 .log_start = vhost_log_start,
1245 .log_stop = vhost_log_stop,
1246 .log_sync = vhost_log_sync,
1247 .log_global_start = vhost_log_global_start,
1248 .log_global_stop = vhost_log_global_stop,
80a1ea37
AK
1249 .eventfd_add = vhost_eventfd_add,
1250 .eventfd_del = vhost_eventfd_del,
72e22d2f 1251 .priority = 10
04097f7c 1252 };
d2fc4402 1253
375f74f4
JW
1254 hdev->iommu_listener = (MemoryListener) {
1255 .region_add = vhost_iommu_region_add,
1256 .region_del = vhost_iommu_region_del,
1257 };
c471ad0e 1258
d2fc4402
MAL
1259 if (hdev->migration_blocker == NULL) {
1260 if (!(hdev->features & (0x1ULL << VHOST_F_LOG_ALL))) {
1261 error_setg(&hdev->migration_blocker,
1262 "Migration disabled: vhost lacks VHOST_F_LOG_ALL feature.");
648abbfb 1263 } else if (vhost_dev_log_is_shared(hdev) && !qemu_memfd_alloc_check()) {
31190ed7
MAL
1264 error_setg(&hdev->migration_blocker,
1265 "Migration disabled: failed to allocate shared memory");
d2fc4402
MAL
1266 }
1267 }
1268
1269 if (hdev->migration_blocker != NULL) {
fe44dc91
AA
1270 r = migrate_add_blocker(hdev->migration_blocker, &local_err);
1271 if (local_err) {
1272 error_report_err(local_err);
1273 error_free(hdev->migration_blocker);
1274 goto fail_busyloop;
1275 }
7145872e 1276 }
d2fc4402 1277
7267c094 1278 hdev->mem = g_malloc0(offsetof(struct vhost_memory, regions));
2817b260
AK
1279 hdev->n_mem_sections = 0;
1280 hdev->mem_sections = NULL;
d5970055
MT
1281 hdev->log = NULL;
1282 hdev->log_size = 0;
1283 hdev->log_enabled = false;
1284 hdev->started = false;
f6790af6 1285 memory_listener_register(&hdev->memory_listener, &address_space_memory);
5be5f9be 1286 QLIST_INSERT_HEAD(&vhost_devices, hdev, entry);
9e2a2a3e
JZ
1287
1288 if (used_memslots > hdev->vhost_ops->vhost_backend_memslots_limit(hdev)) {
1289 error_report("vhost backend memory slots limit is less"
1290 " than current number of present memory slots");
1291 r = -1;
1292 if (busyloop_timeout) {
1293 goto fail_busyloop;
1294 } else {
1295 goto fail;
1296 }
1297 }
1298
d5970055 1299 return 0;
a06db3ec 1300
69e87b32
JW
1301fail_busyloop:
1302 while (--i >= 0) {
1303 vhost_virtqueue_set_busyloop_timeout(hdev, hdev->vq_index + i, 0);
1304 }
d5970055 1305fail:
a06db3ec
MAL
1306 hdev->nvqs = n_initialized_vqs;
1307 vhost_dev_cleanup(hdev);
d5970055
MT
1308 return r;
1309}
1310
1311void vhost_dev_cleanup(struct vhost_dev *hdev)
1312{
f56a1247 1313 int i;
e0547b59 1314
f56a1247
MT
1315 for (i = 0; i < hdev->nvqs; ++i) {
1316 vhost_virtqueue_cleanup(hdev->vqs + i);
1317 }
5be5f9be
MAL
1318 if (hdev->mem) {
1319 /* those are only safe after successful init */
1320 memory_listener_unregister(&hdev->memory_listener);
1321 QLIST_REMOVE(hdev, entry);
1322 }
7145872e
MT
1323 if (hdev->migration_blocker) {
1324 migrate_del_blocker(hdev->migration_blocker);
1325 error_free(hdev->migration_blocker);
1326 }
7267c094 1327 g_free(hdev->mem);
2817b260 1328 g_free(hdev->mem_sections);
e0547b59
MAL
1329 if (hdev->vhost_ops) {
1330 hdev->vhost_ops->vhost_backend_cleanup(hdev);
1331 }
7b527247 1332 assert(!hdev->log);
e0547b59
MAL
1333
1334 memset(hdev, 0, sizeof(struct vhost_dev));
d5970055
MT
1335}
1336
b0b3db79
MT
1337/* Stop processing guest IO notifications in qemu.
1338 * Start processing them in vhost in kernel.
1339 */
1340int vhost_dev_enable_notifiers(struct vhost_dev *hdev, VirtIODevice *vdev)
1341{
1c819449 1342 BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(vdev)));
16617e36 1343 int i, r, e;
4afba631 1344
310837de
PB
1345 /* We will pass the notifiers to the kernel, make sure that QEMU
1346 * doesn't interfere.
1347 */
1348 r = virtio_device_grab_ioeventfd(vdev);
1349 if (r < 0) {
4afba631 1350 error_report("binding does not support host notifiers");
b0b3db79
MT
1351 goto fail;
1352 }
1353
1354 for (i = 0; i < hdev->nvqs; ++i) {
b1f0a33d
CH
1355 r = virtio_bus_set_host_notifier(VIRTIO_BUS(qbus), hdev->vq_index + i,
1356 true);
b0b3db79 1357 if (r < 0) {
4afba631 1358 error_report("vhost VQ %d notifier binding failed: %d", i, -r);
b0b3db79
MT
1359 goto fail_vq;
1360 }
1361 }
1362
1363 return 0;
1364fail_vq:
1365 while (--i >= 0) {
b1f0a33d
CH
1366 e = virtio_bus_set_host_notifier(VIRTIO_BUS(qbus), hdev->vq_index + i,
1367 false);
16617e36 1368 if (e < 0) {
4afba631 1369 error_report("vhost VQ %d notifier cleanup error: %d", i, -r);
b0b3db79 1370 }
16617e36 1371 assert (e >= 0);
76143618 1372 virtio_bus_cleanup_host_notifier(VIRTIO_BUS(qbus), hdev->vq_index + i);
b0b3db79 1373 }
310837de 1374 virtio_device_release_ioeventfd(vdev);
b0b3db79
MT
1375fail:
1376 return r;
1377}
1378
1379/* Stop processing guest IO notifications in vhost.
1380 * Start processing them in qemu.
1381 * This might actually run the qemu handlers right away,
1382 * so virtio in qemu must be completely setup when this is called.
1383 */
1384void vhost_dev_disable_notifiers(struct vhost_dev *hdev, VirtIODevice *vdev)
1385{
1c819449 1386 BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(vdev)));
b0b3db79
MT
1387 int i, r;
1388
1389 for (i = 0; i < hdev->nvqs; ++i) {
b1f0a33d
CH
1390 r = virtio_bus_set_host_notifier(VIRTIO_BUS(qbus), hdev->vq_index + i,
1391 false);
b0b3db79 1392 if (r < 0) {
4afba631 1393 error_report("vhost VQ %d notifier cleanup failed: %d", i, -r);
b0b3db79
MT
1394 }
1395 assert (r >= 0);
76143618 1396 virtio_bus_cleanup_host_notifier(VIRTIO_BUS(qbus), hdev->vq_index + i);
b0b3db79 1397 }
310837de 1398 virtio_device_release_ioeventfd(vdev);
b0b3db79
MT
1399}
1400
f56a1247
MT
1401/* Test and clear event pending status.
1402 * Should be called after unmask to avoid losing events.
1403 */
1404bool vhost_virtqueue_pending(struct vhost_dev *hdev, int n)
1405{
a9f98bb5 1406 struct vhost_virtqueue *vq = hdev->vqs + n - hdev->vq_index;
a9f98bb5 1407 assert(n >= hdev->vq_index && n < hdev->vq_index + hdev->nvqs);
f56a1247
MT
1408 return event_notifier_test_and_clear(&vq->masked_notifier);
1409}
1410
1411/* Mask/unmask events from this vq. */
1412void vhost_virtqueue_mask(struct vhost_dev *hdev, VirtIODevice *vdev, int n,
1413 bool mask)
1414{
1415 struct VirtQueue *vvq = virtio_get_queue(vdev, n);
a9f98bb5 1416 int r, index = n - hdev->vq_index;
fc57fd99 1417 struct vhost_vring_file file;
f56a1247 1418
8695de0f
MAL
1419 /* should only be called after backend is connected */
1420 assert(hdev->vhost_ops);
1421
f56a1247 1422 if (mask) {
5669655a 1423 assert(vdev->use_guest_notifier_mask);
a9f98bb5 1424 file.fd = event_notifier_get_fd(&hdev->vqs[index].masked_notifier);
f56a1247
MT
1425 } else {
1426 file.fd = event_notifier_get_fd(virtio_queue_get_guest_notifier(vvq));
1427 }
fc57fd99 1428
21e70425
MAL
1429 file.index = hdev->vhost_ops->vhost_get_vq_index(hdev, n);
1430 r = hdev->vhost_ops->vhost_set_vring_call(hdev, &file);
162bba7f
MAL
1431 if (r < 0) {
1432 VHOST_OPS_DEBUG("vhost_set_vring_call failed");
1433 }
f56a1247
MT
1434}
1435
9a2ba823
CH
1436uint64_t vhost_get_features(struct vhost_dev *hdev, const int *feature_bits,
1437 uint64_t features)
2e6d46d7
NN
1438{
1439 const int *bit = feature_bits;
1440 while (*bit != VHOST_INVALID_FEATURE_BIT) {
9a2ba823 1441 uint64_t bit_mask = (1ULL << *bit);
2e6d46d7
NN
1442 if (!(hdev->features & bit_mask)) {
1443 features &= ~bit_mask;
1444 }
1445 bit++;
1446 }
1447 return features;
1448}
1449
1450void vhost_ack_features(struct vhost_dev *hdev, const int *feature_bits,
9a2ba823 1451 uint64_t features)
2e6d46d7
NN
1452{
1453 const int *bit = feature_bits;
1454 while (*bit != VHOST_INVALID_FEATURE_BIT) {
9a2ba823 1455 uint64_t bit_mask = (1ULL << *bit);
2e6d46d7
NN
1456 if (features & bit_mask) {
1457 hdev->acked_features |= bit_mask;
1458 }
1459 bit++;
1460 }
1461}
1462
4c3e257b
CL
1463int vhost_dev_get_config(struct vhost_dev *hdev, uint8_t *config,
1464 uint32_t config_len)
1465{
1466 assert(hdev->vhost_ops);
1467
1468 if (hdev->vhost_ops->vhost_get_config) {
1469 return hdev->vhost_ops->vhost_get_config(hdev, config, config_len);
1470 }
1471
1472 return -1;
1473}
1474
1475int vhost_dev_set_config(struct vhost_dev *hdev, const uint8_t *data,
1476 uint32_t offset, uint32_t size, uint32_t flags)
1477{
1478 assert(hdev->vhost_ops);
1479
1480 if (hdev->vhost_ops->vhost_set_config) {
1481 return hdev->vhost_ops->vhost_set_config(hdev, data, offset,
1482 size, flags);
1483 }
1484
1485 return -1;
1486}
1487
1488void vhost_dev_set_config_notifier(struct vhost_dev *hdev,
1489 const VhostDevConfigOps *ops)
1490{
4c3e257b
CL
1491 hdev->config_ops = ops;
1492}
1493
5ad204bf
XY
1494void vhost_dev_free_inflight(struct vhost_inflight *inflight)
1495{
1496 if (inflight->addr) {
1497 qemu_memfd_free(inflight->addr, inflight->size, inflight->fd);
1498 inflight->addr = NULL;
1499 inflight->fd = -1;
1500 }
1501}
1502
1503static int vhost_dev_resize_inflight(struct vhost_inflight *inflight,
1504 uint64_t new_size)
1505{
1506 Error *err = NULL;
1507 int fd = -1;
1508 void *addr = qemu_memfd_alloc("vhost-inflight", new_size,
1509 F_SEAL_GROW | F_SEAL_SHRINK | F_SEAL_SEAL,
1510 &fd, &err);
1511
1512 if (err) {
1513 error_report_err(err);
1514 return -1;
1515 }
1516
1517 vhost_dev_free_inflight(inflight);
1518 inflight->offset = 0;
1519 inflight->addr = addr;
1520 inflight->fd = fd;
1521 inflight->size = new_size;
1522
1523 return 0;
1524}
1525
1526void vhost_dev_save_inflight(struct vhost_inflight *inflight, QEMUFile *f)
1527{
1528 if (inflight->addr) {
1529 qemu_put_be64(f, inflight->size);
1530 qemu_put_be16(f, inflight->queue_size);
1531 qemu_put_buffer(f, inflight->addr, inflight->size);
1532 } else {
1533 qemu_put_be64(f, 0);
1534 }
1535}
1536
1537int vhost_dev_load_inflight(struct vhost_inflight *inflight, QEMUFile *f)
1538{
1539 uint64_t size;
1540
1541 size = qemu_get_be64(f);
1542 if (!size) {
1543 return 0;
1544 }
1545
1546 if (inflight->size != size) {
1547 if (vhost_dev_resize_inflight(inflight, size)) {
1548 return -1;
1549 }
1550 }
1551 inflight->queue_size = qemu_get_be16(f);
1552
1553 qemu_get_buffer(f, inflight->addr, size);
1554
1555 return 0;
1556}
1557
1558int vhost_dev_set_inflight(struct vhost_dev *dev,
1559 struct vhost_inflight *inflight)
1560{
1561 int r;
1562
1563 if (dev->vhost_ops->vhost_set_inflight_fd && inflight->addr) {
1564 r = dev->vhost_ops->vhost_set_inflight_fd(dev, inflight);
1565 if (r) {
1566 VHOST_OPS_DEBUG("vhost_set_inflight_fd failed");
1567 return -errno;
1568 }
1569 }
1570
1571 return 0;
1572}
1573
1574int vhost_dev_get_inflight(struct vhost_dev *dev, uint16_t queue_size,
1575 struct vhost_inflight *inflight)
1576{
1577 int r;
1578
1579 if (dev->vhost_ops->vhost_get_inflight_fd) {
1580 r = dev->vhost_ops->vhost_get_inflight_fd(dev, queue_size, inflight);
1581 if (r) {
1582 VHOST_OPS_DEBUG("vhost_get_inflight_fd failed");
1583 return -errno;
1584 }
1585 }
1586
1587 return 0;
1588}
1589
b0b3db79 1590/* Host notifiers must be enabled at this point. */
d5970055
MT
1591int vhost_dev_start(struct vhost_dev *hdev, VirtIODevice *vdev)
1592{
1593 int i, r;
24f4fe34 1594
8695de0f
MAL
1595 /* should only be called after backend is connected */
1596 assert(hdev->vhost_ops);
1597
24f4fe34 1598 hdev->started = true;
c471ad0e 1599 hdev->vdev = vdev;
24f4fe34 1600
d5970055
MT
1601 r = vhost_dev_set_features(hdev, hdev->log_enabled);
1602 if (r < 0) {
54dd9321 1603 goto fail_features;
d5970055 1604 }
c471ad0e
JW
1605
1606 if (vhost_dev_has_iommu(hdev)) {
375f74f4 1607 memory_listener_register(&hdev->iommu_listener, vdev->dma_as);
c471ad0e
JW
1608 }
1609
21e70425 1610 r = hdev->vhost_ops->vhost_set_mem_table(hdev, hdev->mem);
d5970055 1611 if (r < 0) {
c6409692 1612 VHOST_OPS_DEBUG("vhost_set_mem_table failed");
d5970055 1613 r = -errno;
54dd9321 1614 goto fail_mem;
d5970055 1615 }
d154e0ba 1616 for (i = 0; i < hdev->nvqs; ++i) {
f56a1247 1617 r = vhost_virtqueue_start(hdev,
a9f98bb5
JW
1618 vdev,
1619 hdev->vqs + i,
1620 hdev->vq_index + i);
d154e0ba
MT
1621 if (r < 0) {
1622 goto fail_vq;
1623 }
1624 }
1625
d5970055 1626 if (hdev->log_enabled) {
e05ca820
MT
1627 uint64_t log_base;
1628
d5970055 1629 hdev->log_size = vhost_get_log_size(hdev);
15324404
MAL
1630 hdev->log = vhost_log_get(hdev->log_size,
1631 vhost_dev_log_is_shared(hdev));
309750fa 1632 log_base = (uintptr_t)hdev->log->log;
c2bea314 1633 r = hdev->vhost_ops->vhost_set_log_base(hdev,
9a78a5dd
MAL
1634 hdev->log_size ? log_base : 0,
1635 hdev->log);
d5970055 1636 if (r < 0) {
c6409692 1637 VHOST_OPS_DEBUG("vhost_set_log_base failed");
d5970055 1638 r = -errno;
54dd9321 1639 goto fail_log;
d5970055
MT
1640 }
1641 }
d154e0ba 1642
c471ad0e
JW
1643 if (vhost_dev_has_iommu(hdev)) {
1644 hdev->vhost_ops->vhost_set_iotlb_callback(hdev, true);
1645
1646 /* Update used ring information for IOTLB to work correctly,
1647 * vhost-kernel code requires for this.*/
1648 for (i = 0; i < hdev->nvqs; ++i) {
1649 struct vhost_virtqueue *vq = hdev->vqs + i;
1650 vhost_device_iotlb_miss(hdev, vq->used_phys, true);
1651 }
1652 }
d5970055 1653 return 0;
54dd9321 1654fail_log:
24bfa207 1655 vhost_log_put(hdev, false);
d5970055
MT
1656fail_vq:
1657 while (--i >= 0) {
f56a1247 1658 vhost_virtqueue_stop(hdev,
a9f98bb5
JW
1659 vdev,
1660 hdev->vqs + i,
1661 hdev->vq_index + i);
d5970055 1662 }
c471ad0e 1663
54dd9321
MT
1664fail_mem:
1665fail_features:
24f4fe34
MT
1666
1667 hdev->started = false;
d5970055
MT
1668 return r;
1669}
1670
b0b3db79 1671/* Host notifiers must be enabled at this point. */
d5970055
MT
1672void vhost_dev_stop(struct vhost_dev *hdev, VirtIODevice *vdev)
1673{
a9f98bb5 1674 int i;
54dd9321 1675
8695de0f
MAL
1676 /* should only be called after backend is connected */
1677 assert(hdev->vhost_ops);
1678
d5970055 1679 for (i = 0; i < hdev->nvqs; ++i) {
f56a1247 1680 vhost_virtqueue_stop(hdev,
a9f98bb5
JW
1681 vdev,
1682 hdev->vqs + i,
1683 hdev->vq_index + i);
d5970055 1684 }
54dd9321 1685
c471ad0e
JW
1686 if (vhost_dev_has_iommu(hdev)) {
1687 hdev->vhost_ops->vhost_set_iotlb_callback(hdev, false);
375f74f4 1688 memory_listener_unregister(&hdev->iommu_listener);
c471ad0e 1689 }
309750fa 1690 vhost_log_put(hdev, true);
d5970055 1691 hdev->started = false;
c471ad0e 1692 hdev->vdev = NULL;
d5970055 1693}
950d94ba
MAL
1694
1695int vhost_net_set_backend(struct vhost_dev *hdev,
1696 struct vhost_vring_file *file)
1697{
1698 if (hdev->vhost_ops->vhost_net_set_backend) {
1699 return hdev->vhost_ops->vhost_net_set_backend(hdev, file);
1700 }
1701
1702 return -1;
1703}