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