xref: /openbmc/qemu/hw/virtio/vhost.c (revision 8c95e1f20c18007f30bdba6a60e99aea37cc473b)
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.
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.
14  */
15 
16 #include "qemu/osdep.h"
17 #include "qapi/error.h"
18 #include "hw/virtio/vhost.h"
19 #include "hw/hw.h"
20 #include "qemu/atomic.h"
21 #include "qemu/range.h"
22 #include "qemu/error-report.h"
23 #include "qemu/memfd.h"
24 #include <linux/vhost.h>
25 #include "exec/address-spaces.h"
26 #include "hw/virtio/virtio-bus.h"
27 #include "hw/virtio/virtio-access.h"
28 #include "migration/migration.h"
29 #include "sysemu/dma.h"
30 
31 /* enabled until disconnected backend stabilizes */
32 #define _VHOST_DEBUG 1
33 
34 #ifdef _VHOST_DEBUG
35 #define VHOST_OPS_DEBUG(fmt, ...) \
36     do { error_report(fmt ": %s (%d)", ## __VA_ARGS__, \
37                       strerror(errno), errno); } while (0)
38 #else
39 #define VHOST_OPS_DEBUG(fmt, ...) \
40     do { } while (0)
41 #endif
42 
43 static struct vhost_log *vhost_log;
44 static struct vhost_log *vhost_log_shm;
45 
46 static unsigned int used_memslots;
47 static QLIST_HEAD(, vhost_dev) vhost_devices =
48     QLIST_HEAD_INITIALIZER(vhost_devices);
49 
50 bool vhost_has_free_slot(void)
51 {
52     unsigned int slots_limit = ~0U;
53     struct vhost_dev *hdev;
54 
55     QLIST_FOREACH(hdev, &vhost_devices, entry) {
56         unsigned int r = hdev->vhost_ops->vhost_backend_memslots_limit(hdev);
57         slots_limit = MIN(slots_limit, r);
58     }
59     return slots_limit > used_memslots;
60 }
61 
62 static void vhost_dev_sync_region(struct vhost_dev *dev,
63                                   MemoryRegionSection *section,
64                                   uint64_t mfirst, uint64_t mlast,
65                                   uint64_t rfirst, uint64_t rlast)
66 {
67     vhost_log_chunk_t *log = dev->log->log;
68 
69     uint64_t start = MAX(mfirst, rfirst);
70     uint64_t end = MIN(mlast, rlast);
71     vhost_log_chunk_t *from = log + start / VHOST_LOG_CHUNK;
72     vhost_log_chunk_t *to = log + end / VHOST_LOG_CHUNK + 1;
73     uint64_t addr = (start / VHOST_LOG_CHUNK) * VHOST_LOG_CHUNK;
74 
75     if (end < start) {
76         return;
77     }
78     assert(end / VHOST_LOG_CHUNK < dev->log_size);
79     assert(start / VHOST_LOG_CHUNK < dev->log_size);
80 
81     for (;from < to; ++from) {
82         vhost_log_chunk_t log;
83         /* We first check with non-atomic: much cheaper,
84          * and we expect non-dirty to be the common case. */
85         if (!*from) {
86             addr += VHOST_LOG_CHUNK;
87             continue;
88         }
89         /* Data must be read atomically. We don't really need barrier semantics
90          * but it's easier to use atomic_* than roll our own. */
91         log = atomic_xchg(from, 0);
92         while (log) {
93             int bit = ctzl(log);
94             hwaddr page_addr;
95             hwaddr section_offset;
96             hwaddr mr_offset;
97             page_addr = addr + bit * VHOST_LOG_PAGE;
98             section_offset = page_addr - section->offset_within_address_space;
99             mr_offset = section_offset + section->offset_within_region;
100             memory_region_set_dirty(section->mr, mr_offset, VHOST_LOG_PAGE);
101             log &= ~(0x1ull << bit);
102         }
103         addr += VHOST_LOG_CHUNK;
104     }
105 }
106 
107 static int vhost_sync_dirty_bitmap(struct vhost_dev *dev,
108                                    MemoryRegionSection *section,
109                                    hwaddr first,
110                                    hwaddr last)
111 {
112     int i;
113     hwaddr start_addr;
114     hwaddr end_addr;
115 
116     if (!dev->log_enabled || !dev->started) {
117         return 0;
118     }
119     start_addr = section->offset_within_address_space;
120     end_addr = range_get_last(start_addr, int128_get64(section->size));
121     start_addr = MAX(first, start_addr);
122     end_addr = MIN(last, end_addr);
123 
124     for (i = 0; i < dev->mem->nregions; ++i) {
125         struct vhost_memory_region *reg = dev->mem->regions + i;
126         vhost_dev_sync_region(dev, section, start_addr, end_addr,
127                               reg->guest_phys_addr,
128                               range_get_last(reg->guest_phys_addr,
129                                              reg->memory_size));
130     }
131     for (i = 0; i < dev->nvqs; ++i) {
132         struct vhost_virtqueue *vq = dev->vqs + i;
133         vhost_dev_sync_region(dev, section, start_addr, end_addr, vq->used_phys,
134                               range_get_last(vq->used_phys, vq->used_size));
135     }
136     return 0;
137 }
138 
139 static void vhost_log_sync(MemoryListener *listener,
140                           MemoryRegionSection *section)
141 {
142     struct vhost_dev *dev = container_of(listener, struct vhost_dev,
143                                          memory_listener);
144     vhost_sync_dirty_bitmap(dev, section, 0x0, ~0x0ULL);
145 }
146 
147 static void vhost_log_sync_range(struct vhost_dev *dev,
148                                  hwaddr first, hwaddr last)
149 {
150     int i;
151     /* FIXME: this is N^2 in number of sections */
152     for (i = 0; i < dev->n_mem_sections; ++i) {
153         MemoryRegionSection *section = &dev->mem_sections[i];
154         vhost_sync_dirty_bitmap(dev, section, first, last);
155     }
156 }
157 
158 /* Assign/unassign. Keep an unsorted array of non-overlapping
159  * memory regions in dev->mem. */
160 static void vhost_dev_unassign_memory(struct vhost_dev *dev,
161                                       uint64_t start_addr,
162                                       uint64_t size)
163 {
164     int from, to, n = dev->mem->nregions;
165     /* Track overlapping/split regions for sanity checking. */
166     int overlap_start = 0, overlap_end = 0, overlap_middle = 0, split = 0;
167 
168     for (from = 0, to = 0; from < n; ++from, ++to) {
169         struct vhost_memory_region *reg = dev->mem->regions + to;
170         uint64_t reglast;
171         uint64_t memlast;
172         uint64_t change;
173 
174         /* clone old region */
175         if (to != from) {
176             memcpy(reg, dev->mem->regions + from, sizeof *reg);
177         }
178 
179         /* No overlap is simple */
180         if (!ranges_overlap(reg->guest_phys_addr, reg->memory_size,
181                             start_addr, size)) {
182             continue;
183         }
184 
185         /* Split only happens if supplied region
186          * is in the middle of an existing one. Thus it can not
187          * overlap with any other existing region. */
188         assert(!split);
189 
190         reglast = range_get_last(reg->guest_phys_addr, reg->memory_size);
191         memlast = range_get_last(start_addr, size);
192 
193         /* Remove whole region */
194         if (start_addr <= reg->guest_phys_addr && memlast >= reglast) {
195             --dev->mem->nregions;
196             --to;
197             ++overlap_middle;
198             continue;
199         }
200 
201         /* Shrink region */
202         if (memlast >= reglast) {
203             reg->memory_size = start_addr - reg->guest_phys_addr;
204             assert(reg->memory_size);
205             assert(!overlap_end);
206             ++overlap_end;
207             continue;
208         }
209 
210         /* Shift region */
211         if (start_addr <= reg->guest_phys_addr) {
212             change = memlast + 1 - reg->guest_phys_addr;
213             reg->memory_size -= change;
214             reg->guest_phys_addr += change;
215             reg->userspace_addr += change;
216             assert(reg->memory_size);
217             assert(!overlap_start);
218             ++overlap_start;
219             continue;
220         }
221 
222         /* This only happens if supplied region
223          * is in the middle of an existing one. Thus it can not
224          * overlap with any other existing region. */
225         assert(!overlap_start);
226         assert(!overlap_end);
227         assert(!overlap_middle);
228         /* Split region: shrink first part, shift second part. */
229         memcpy(dev->mem->regions + n, reg, sizeof *reg);
230         reg->memory_size = start_addr - reg->guest_phys_addr;
231         assert(reg->memory_size);
232         change = memlast + 1 - reg->guest_phys_addr;
233         reg = dev->mem->regions + n;
234         reg->memory_size -= change;
235         assert(reg->memory_size);
236         reg->guest_phys_addr += change;
237         reg->userspace_addr += change;
238         /* Never add more than 1 region */
239         assert(dev->mem->nregions == n);
240         ++dev->mem->nregions;
241         ++split;
242     }
243 }
244 
245 /* Called after unassign, so no regions overlap the given range. */
246 static void vhost_dev_assign_memory(struct vhost_dev *dev,
247                                     uint64_t start_addr,
248                                     uint64_t size,
249                                     uint64_t uaddr)
250 {
251     int from, to;
252     struct vhost_memory_region *merged = NULL;
253     for (from = 0, to = 0; from < dev->mem->nregions; ++from, ++to) {
254         struct vhost_memory_region *reg = dev->mem->regions + to;
255         uint64_t prlast, urlast;
256         uint64_t pmlast, umlast;
257         uint64_t s, e, u;
258 
259         /* clone old region */
260         if (to != from) {
261             memcpy(reg, dev->mem->regions + from, sizeof *reg);
262         }
263         prlast = range_get_last(reg->guest_phys_addr, reg->memory_size);
264         pmlast = range_get_last(start_addr, size);
265         urlast = range_get_last(reg->userspace_addr, reg->memory_size);
266         umlast = range_get_last(uaddr, size);
267 
268         /* check for overlapping regions: should never happen. */
269         assert(prlast < start_addr || pmlast < reg->guest_phys_addr);
270         /* Not an adjacent or overlapping region - do not merge. */
271         if ((prlast + 1 != start_addr || urlast + 1 != uaddr) &&
272             (pmlast + 1 != reg->guest_phys_addr ||
273              umlast + 1 != reg->userspace_addr)) {
274             continue;
275         }
276 
277         if (dev->vhost_ops->vhost_backend_can_merge &&
278             !dev->vhost_ops->vhost_backend_can_merge(dev, uaddr, size,
279                                                      reg->userspace_addr,
280                                                      reg->memory_size)) {
281             continue;
282         }
283 
284         if (merged) {
285             --to;
286             assert(to >= 0);
287         } else {
288             merged = reg;
289         }
290         u = MIN(uaddr, reg->userspace_addr);
291         s = MIN(start_addr, reg->guest_phys_addr);
292         e = MAX(pmlast, prlast);
293         uaddr = merged->userspace_addr = u;
294         start_addr = merged->guest_phys_addr = s;
295         size = merged->memory_size = e - s + 1;
296         assert(merged->memory_size);
297     }
298 
299     if (!merged) {
300         struct vhost_memory_region *reg = dev->mem->regions + to;
301         memset(reg, 0, sizeof *reg);
302         reg->memory_size = size;
303         assert(reg->memory_size);
304         reg->guest_phys_addr = start_addr;
305         reg->userspace_addr = uaddr;
306         ++to;
307     }
308     assert(to <= dev->mem->nregions + 1);
309     dev->mem->nregions = to;
310 }
311 
312 static uint64_t vhost_get_log_size(struct vhost_dev *dev)
313 {
314     uint64_t log_size = 0;
315     int i;
316     for (i = 0; i < dev->mem->nregions; ++i) {
317         struct vhost_memory_region *reg = dev->mem->regions + i;
318         uint64_t last = range_get_last(reg->guest_phys_addr,
319                                        reg->memory_size);
320         log_size = MAX(log_size, last / VHOST_LOG_CHUNK + 1);
321     }
322     for (i = 0; i < dev->nvqs; ++i) {
323         struct vhost_virtqueue *vq = dev->vqs + i;
324         uint64_t last = vq->used_phys + vq->used_size - 1;
325         log_size = MAX(log_size, last / VHOST_LOG_CHUNK + 1);
326     }
327     return log_size;
328 }
329 
330 static struct vhost_log *vhost_log_alloc(uint64_t size, bool share)
331 {
332     struct vhost_log *log;
333     uint64_t logsize = size * sizeof(*(log->log));
334     int fd = -1;
335 
336     log = g_new0(struct vhost_log, 1);
337     if (share) {
338         log->log = qemu_memfd_alloc("vhost-log", logsize,
339                                     F_SEAL_GROW | F_SEAL_SHRINK | F_SEAL_SEAL,
340                                     &fd);
341         memset(log->log, 0, logsize);
342     } else {
343         log->log = g_malloc0(logsize);
344     }
345 
346     log->size = size;
347     log->refcnt = 1;
348     log->fd = fd;
349 
350     return log;
351 }
352 
353 static struct vhost_log *vhost_log_get(uint64_t size, bool share)
354 {
355     struct vhost_log *log = share ? vhost_log_shm : vhost_log;
356 
357     if (!log || log->size != size) {
358         log = vhost_log_alloc(size, share);
359         if (share) {
360             vhost_log_shm = log;
361         } else {
362             vhost_log = log;
363         }
364     } else {
365         ++log->refcnt;
366     }
367 
368     return log;
369 }
370 
371 static void vhost_log_put(struct vhost_dev *dev, bool sync)
372 {
373     struct vhost_log *log = dev->log;
374 
375     if (!log) {
376         return;
377     }
378     dev->log = NULL;
379     dev->log_size = 0;
380 
381     --log->refcnt;
382     if (log->refcnt == 0) {
383         /* Sync only the range covered by the old log */
384         if (dev->log_size && sync) {
385             vhost_log_sync_range(dev, 0, dev->log_size * VHOST_LOG_CHUNK - 1);
386         }
387 
388         if (vhost_log == log) {
389             g_free(log->log);
390             vhost_log = NULL;
391         } else if (vhost_log_shm == log) {
392             qemu_memfd_free(log->log, log->size * sizeof(*(log->log)),
393                             log->fd);
394             vhost_log_shm = NULL;
395         }
396 
397         g_free(log);
398     }
399 }
400 
401 static bool vhost_dev_log_is_shared(struct vhost_dev *dev)
402 {
403     return dev->vhost_ops->vhost_requires_shm_log &&
404            dev->vhost_ops->vhost_requires_shm_log(dev);
405 }
406 
407 static inline void vhost_dev_log_resize(struct vhost_dev *dev, uint64_t size)
408 {
409     struct vhost_log *log = vhost_log_get(size, vhost_dev_log_is_shared(dev));
410     uint64_t log_base = (uintptr_t)log->log;
411     int r;
412 
413     /* inform backend of log switching, this must be done before
414        releasing the current log, to ensure no logging is lost */
415     r = dev->vhost_ops->vhost_set_log_base(dev, log_base, log);
416     if (r < 0) {
417         VHOST_OPS_DEBUG("vhost_set_log_base failed");
418     }
419 
420     vhost_log_put(dev, true);
421     dev->log = log;
422     dev->log_size = size;
423 }
424 
425 static int vhost_dev_has_iommu(struct vhost_dev *dev)
426 {
427     VirtIODevice *vdev = dev->vdev;
428 
429     return virtio_host_has_feature(vdev, VIRTIO_F_IOMMU_PLATFORM);
430 }
431 
432 static void *vhost_memory_map(struct vhost_dev *dev, hwaddr addr,
433                               hwaddr *plen, int is_write)
434 {
435     if (!vhost_dev_has_iommu(dev)) {
436         return cpu_physical_memory_map(addr, plen, is_write);
437     } else {
438         return (void *)(uintptr_t)addr;
439     }
440 }
441 
442 static void vhost_memory_unmap(struct vhost_dev *dev, void *buffer,
443                                hwaddr len, int is_write,
444                                hwaddr access_len)
445 {
446     if (!vhost_dev_has_iommu(dev)) {
447         cpu_physical_memory_unmap(buffer, len, is_write, access_len);
448     }
449 }
450 
451 static int vhost_verify_ring_part_mapping(struct vhost_dev *dev,
452                                           void *part,
453                                           uint64_t part_addr,
454                                           uint64_t part_size,
455                                           uint64_t start_addr,
456                                           uint64_t size)
457 {
458     hwaddr l;
459     void *p;
460     int r = 0;
461 
462     if (!ranges_overlap(start_addr, size, part_addr, part_size)) {
463         return 0;
464     }
465     l = part_size;
466     p = vhost_memory_map(dev, part_addr, &l, 1);
467     if (!p || l != part_size) {
468         r = -ENOMEM;
469     }
470     if (p != part) {
471         r = -EBUSY;
472     }
473     vhost_memory_unmap(dev, p, l, 0, 0);
474     return r;
475 }
476 
477 static int vhost_verify_ring_mappings(struct vhost_dev *dev,
478                                       uint64_t start_addr,
479                                       uint64_t size)
480 {
481     int i, j;
482     int r = 0;
483     const char *part_name[] = {
484         "descriptor table",
485         "available ring",
486         "used ring"
487     };
488 
489     for (i = 0; i < dev->nvqs; ++i) {
490         struct vhost_virtqueue *vq = dev->vqs + i;
491 
492         j = 0;
493         r = vhost_verify_ring_part_mapping(dev, vq->desc, vq->desc_phys,
494                                            vq->desc_size, start_addr, size);
495         if (!r) {
496             break;
497         }
498 
499         j++;
500         r = vhost_verify_ring_part_mapping(dev, vq->avail, vq->avail_phys,
501                                            vq->avail_size, start_addr, size);
502         if (!r) {
503             break;
504         }
505 
506         j++;
507         r = vhost_verify_ring_part_mapping(dev, vq->used, vq->used_phys,
508                                            vq->used_size, start_addr, size);
509         if (!r) {
510             break;
511         }
512     }
513 
514     if (r == -ENOMEM) {
515         error_report("Unable to map %s for ring %d", part_name[j], i);
516     } else if (r == -EBUSY) {
517         error_report("%s relocated for ring %d", part_name[j], i);
518     }
519     return r;
520 }
521 
522 static struct vhost_memory_region *vhost_dev_find_reg(struct vhost_dev *dev,
523 						      uint64_t start_addr,
524 						      uint64_t size)
525 {
526     int i, n = dev->mem->nregions;
527     for (i = 0; i < n; ++i) {
528         struct vhost_memory_region *reg = dev->mem->regions + i;
529         if (ranges_overlap(reg->guest_phys_addr, reg->memory_size,
530                            start_addr, size)) {
531             return reg;
532         }
533     }
534     return NULL;
535 }
536 
537 static bool vhost_dev_cmp_memory(struct vhost_dev *dev,
538                                  uint64_t start_addr,
539                                  uint64_t size,
540                                  uint64_t uaddr)
541 {
542     struct vhost_memory_region *reg = vhost_dev_find_reg(dev, start_addr, size);
543     uint64_t reglast;
544     uint64_t memlast;
545 
546     if (!reg) {
547         return true;
548     }
549 
550     reglast = range_get_last(reg->guest_phys_addr, reg->memory_size);
551     memlast = range_get_last(start_addr, size);
552 
553     /* Need to extend region? */
554     if (start_addr < reg->guest_phys_addr || memlast > reglast) {
555         return true;
556     }
557     /* userspace_addr changed? */
558     return uaddr != reg->userspace_addr + start_addr - reg->guest_phys_addr;
559 }
560 
561 static void vhost_set_memory(MemoryListener *listener,
562                              MemoryRegionSection *section,
563                              bool add)
564 {
565     struct vhost_dev *dev = container_of(listener, struct vhost_dev,
566                                          memory_listener);
567     hwaddr start_addr = section->offset_within_address_space;
568     ram_addr_t size = int128_get64(section->size);
569     bool log_dirty =
570         memory_region_get_dirty_log_mask(section->mr) & ~(1 << DIRTY_MEMORY_MIGRATION);
571     int s = offsetof(struct vhost_memory, regions) +
572         (dev->mem->nregions + 1) * sizeof dev->mem->regions[0];
573     void *ram;
574 
575     dev->mem = g_realloc(dev->mem, s);
576 
577     if (log_dirty) {
578         add = false;
579     }
580 
581     assert(size);
582 
583     /* Optimize no-change case. At least cirrus_vga does this a lot at this time. */
584     ram = memory_region_get_ram_ptr(section->mr) + section->offset_within_region;
585     if (add) {
586         if (!vhost_dev_cmp_memory(dev, start_addr, size, (uintptr_t)ram)) {
587             /* Region exists with same address. Nothing to do. */
588             return;
589         }
590     } else {
591         if (!vhost_dev_find_reg(dev, start_addr, size)) {
592             /* Removing region that we don't access. Nothing to do. */
593             return;
594         }
595     }
596 
597     vhost_dev_unassign_memory(dev, start_addr, size);
598     if (add) {
599         /* Add given mapping, merging adjacent regions if any */
600         vhost_dev_assign_memory(dev, start_addr, size, (uintptr_t)ram);
601     } else {
602         /* Remove old mapping for this memory, if any. */
603         vhost_dev_unassign_memory(dev, start_addr, size);
604     }
605     dev->mem_changed_start_addr = MIN(dev->mem_changed_start_addr, start_addr);
606     dev->mem_changed_end_addr = MAX(dev->mem_changed_end_addr, start_addr + size - 1);
607     dev->memory_changed = true;
608     used_memslots = dev->mem->nregions;
609 }
610 
611 static bool vhost_section(MemoryRegionSection *section)
612 {
613     return memory_region_is_ram(section->mr) &&
614         !memory_region_is_rom(section->mr);
615 }
616 
617 static void vhost_begin(MemoryListener *listener)
618 {
619     struct vhost_dev *dev = container_of(listener, struct vhost_dev,
620                                          memory_listener);
621     dev->mem_changed_end_addr = 0;
622     dev->mem_changed_start_addr = -1;
623 }
624 
625 static void vhost_commit(MemoryListener *listener)
626 {
627     struct vhost_dev *dev = container_of(listener, struct vhost_dev,
628                                          memory_listener);
629     hwaddr start_addr = 0;
630     ram_addr_t size = 0;
631     uint64_t log_size;
632     int r;
633 
634     if (!dev->memory_changed) {
635         return;
636     }
637     if (!dev->started) {
638         return;
639     }
640     if (dev->mem_changed_start_addr > dev->mem_changed_end_addr) {
641         return;
642     }
643 
644     if (dev->started) {
645         start_addr = dev->mem_changed_start_addr;
646         size = dev->mem_changed_end_addr - dev->mem_changed_start_addr + 1;
647 
648         r = vhost_verify_ring_mappings(dev, start_addr, size);
649         assert(r >= 0);
650     }
651 
652     if (!dev->log_enabled) {
653         r = dev->vhost_ops->vhost_set_mem_table(dev, dev->mem);
654         if (r < 0) {
655             VHOST_OPS_DEBUG("vhost_set_mem_table failed");
656         }
657         dev->memory_changed = false;
658         return;
659     }
660     log_size = vhost_get_log_size(dev);
661     /* We allocate an extra 4K bytes to log,
662      * to reduce the * number of reallocations. */
663 #define VHOST_LOG_BUFFER (0x1000 / sizeof *dev->log)
664     /* To log more, must increase log size before table update. */
665     if (dev->log_size < log_size) {
666         vhost_dev_log_resize(dev, log_size + VHOST_LOG_BUFFER);
667     }
668     r = dev->vhost_ops->vhost_set_mem_table(dev, dev->mem);
669     if (r < 0) {
670         VHOST_OPS_DEBUG("vhost_set_mem_table failed");
671     }
672     /* To log less, can only decrease log size after table update. */
673     if (dev->log_size > log_size + VHOST_LOG_BUFFER) {
674         vhost_dev_log_resize(dev, log_size);
675     }
676     dev->memory_changed = false;
677 }
678 
679 static void vhost_region_add(MemoryListener *listener,
680                              MemoryRegionSection *section)
681 {
682     struct vhost_dev *dev = container_of(listener, struct vhost_dev,
683                                          memory_listener);
684 
685     if (!vhost_section(section)) {
686         return;
687     }
688 
689     ++dev->n_mem_sections;
690     dev->mem_sections = g_renew(MemoryRegionSection, dev->mem_sections,
691                                 dev->n_mem_sections);
692     dev->mem_sections[dev->n_mem_sections - 1] = *section;
693     memory_region_ref(section->mr);
694     vhost_set_memory(listener, section, true);
695 }
696 
697 static void vhost_region_del(MemoryListener *listener,
698                              MemoryRegionSection *section)
699 {
700     struct vhost_dev *dev = container_of(listener, struct vhost_dev,
701                                          memory_listener);
702     int i;
703 
704     if (!vhost_section(section)) {
705         return;
706     }
707 
708     vhost_set_memory(listener, section, false);
709     memory_region_unref(section->mr);
710     for (i = 0; i < dev->n_mem_sections; ++i) {
711         if (dev->mem_sections[i].offset_within_address_space
712             == section->offset_within_address_space) {
713             --dev->n_mem_sections;
714             memmove(&dev->mem_sections[i], &dev->mem_sections[i+1],
715                     (dev->n_mem_sections - i) * sizeof(*dev->mem_sections));
716             break;
717         }
718     }
719 }
720 
721 static void vhost_iommu_unmap_notify(IOMMUNotifier *n, IOMMUTLBEntry *iotlb)
722 {
723     struct vhost_iommu *iommu = container_of(n, struct vhost_iommu, n);
724     struct vhost_dev *hdev = iommu->hdev;
725     hwaddr iova = iotlb->iova + iommu->iommu_offset;
726 
727     if (hdev->vhost_ops->vhost_invalidate_device_iotlb(hdev, iova,
728                                                        iotlb->addr_mask + 1)) {
729         error_report("Fail to invalidate device iotlb");
730     }
731 }
732 
733 static void vhost_iommu_region_add(MemoryListener *listener,
734                                    MemoryRegionSection *section)
735 {
736     struct vhost_dev *dev = container_of(listener, struct vhost_dev,
737                                          iommu_listener);
738     struct vhost_iommu *iommu;
739 
740     if (!memory_region_is_iommu(section->mr)) {
741         return;
742     }
743 
744     iommu = g_malloc0(sizeof(*iommu));
745     iommu->n.notify = vhost_iommu_unmap_notify;
746     iommu->n.notifier_flags = IOMMU_NOTIFIER_UNMAP;
747     iommu->mr = section->mr;
748     iommu->iommu_offset = section->offset_within_address_space -
749                           section->offset_within_region;
750     iommu->hdev = dev;
751     memory_region_register_iommu_notifier(section->mr, &iommu->n);
752     QLIST_INSERT_HEAD(&dev->iommu_list, iommu, iommu_next);
753     /* TODO: can replay help performance here? */
754 }
755 
756 static void vhost_iommu_region_del(MemoryListener *listener,
757                                    MemoryRegionSection *section)
758 {
759     struct vhost_dev *dev = container_of(listener, struct vhost_dev,
760                                          iommu_listener);
761     struct vhost_iommu *iommu;
762 
763     if (!memory_region_is_iommu(section->mr)) {
764         return;
765     }
766 
767     QLIST_FOREACH(iommu, &dev->iommu_list, iommu_next) {
768         if (iommu->mr == section->mr) {
769             memory_region_unregister_iommu_notifier(iommu->mr,
770                                                     &iommu->n);
771             QLIST_REMOVE(iommu, iommu_next);
772             g_free(iommu);
773             break;
774         }
775     }
776 }
777 
778 static void vhost_region_nop(MemoryListener *listener,
779                              MemoryRegionSection *section)
780 {
781 }
782 
783 static int vhost_virtqueue_set_addr(struct vhost_dev *dev,
784                                     struct vhost_virtqueue *vq,
785                                     unsigned idx, bool enable_log)
786 {
787     struct vhost_vring_addr addr = {
788         .index = idx,
789         .desc_user_addr = (uint64_t)(unsigned long)vq->desc,
790         .avail_user_addr = (uint64_t)(unsigned long)vq->avail,
791         .used_user_addr = (uint64_t)(unsigned long)vq->used,
792         .log_guest_addr = vq->used_phys,
793         .flags = enable_log ? (1 << VHOST_VRING_F_LOG) : 0,
794     };
795     int r = dev->vhost_ops->vhost_set_vring_addr(dev, &addr);
796     if (r < 0) {
797         VHOST_OPS_DEBUG("vhost_set_vring_addr failed");
798         return -errno;
799     }
800     return 0;
801 }
802 
803 static int vhost_dev_set_features(struct vhost_dev *dev,
804                                   bool enable_log)
805 {
806     uint64_t features = dev->acked_features;
807     int r;
808     if (enable_log) {
809         features |= 0x1ULL << VHOST_F_LOG_ALL;
810     }
811     r = dev->vhost_ops->vhost_set_features(dev, features);
812     if (r < 0) {
813         VHOST_OPS_DEBUG("vhost_set_features failed");
814     }
815     return r < 0 ? -errno : 0;
816 }
817 
818 static int vhost_dev_set_log(struct vhost_dev *dev, bool enable_log)
819 {
820     int r, i, idx;
821     r = vhost_dev_set_features(dev, enable_log);
822     if (r < 0) {
823         goto err_features;
824     }
825     for (i = 0; i < dev->nvqs; ++i) {
826         idx = dev->vhost_ops->vhost_get_vq_index(dev, dev->vq_index + i);
827         r = vhost_virtqueue_set_addr(dev, dev->vqs + i, idx,
828                                      enable_log);
829         if (r < 0) {
830             goto err_vq;
831         }
832     }
833     return 0;
834 err_vq:
835     for (; i >= 0; --i) {
836         idx = dev->vhost_ops->vhost_get_vq_index(dev, dev->vq_index + i);
837         vhost_virtqueue_set_addr(dev, dev->vqs + i, idx,
838                                  dev->log_enabled);
839     }
840     vhost_dev_set_features(dev, dev->log_enabled);
841 err_features:
842     return r;
843 }
844 
845 static int vhost_migration_log(MemoryListener *listener, int enable)
846 {
847     struct vhost_dev *dev = container_of(listener, struct vhost_dev,
848                                          memory_listener);
849     int r;
850     if (!!enable == dev->log_enabled) {
851         return 0;
852     }
853     if (!dev->started) {
854         dev->log_enabled = enable;
855         return 0;
856     }
857     if (!enable) {
858         r = vhost_dev_set_log(dev, false);
859         if (r < 0) {
860             return r;
861         }
862         vhost_log_put(dev, false);
863     } else {
864         vhost_dev_log_resize(dev, vhost_get_log_size(dev));
865         r = vhost_dev_set_log(dev, true);
866         if (r < 0) {
867             return r;
868         }
869     }
870     dev->log_enabled = enable;
871     return 0;
872 }
873 
874 static void vhost_log_global_start(MemoryListener *listener)
875 {
876     int r;
877 
878     r = vhost_migration_log(listener, true);
879     if (r < 0) {
880         abort();
881     }
882 }
883 
884 static void vhost_log_global_stop(MemoryListener *listener)
885 {
886     int r;
887 
888     r = vhost_migration_log(listener, false);
889     if (r < 0) {
890         abort();
891     }
892 }
893 
894 static void vhost_log_start(MemoryListener *listener,
895                             MemoryRegionSection *section,
896                             int old, int new)
897 {
898     /* FIXME: implement */
899 }
900 
901 static void vhost_log_stop(MemoryListener *listener,
902                            MemoryRegionSection *section,
903                            int old, int new)
904 {
905     /* FIXME: implement */
906 }
907 
908 /* The vhost driver natively knows how to handle the vrings of non
909  * cross-endian legacy devices and modern devices. Only legacy devices
910  * exposed to a bi-endian guest may require the vhost driver to use a
911  * specific endianness.
912  */
913 static inline bool vhost_needs_vring_endian(VirtIODevice *vdev)
914 {
915     if (virtio_vdev_has_feature(vdev, VIRTIO_F_VERSION_1)) {
916         return false;
917     }
918 #ifdef HOST_WORDS_BIGENDIAN
919     return vdev->device_endian == VIRTIO_DEVICE_ENDIAN_LITTLE;
920 #else
921     return vdev->device_endian == VIRTIO_DEVICE_ENDIAN_BIG;
922 #endif
923 }
924 
925 static int vhost_virtqueue_set_vring_endian_legacy(struct vhost_dev *dev,
926                                                    bool is_big_endian,
927                                                    int vhost_vq_index)
928 {
929     struct vhost_vring_state s = {
930         .index = vhost_vq_index,
931         .num = is_big_endian
932     };
933 
934     if (!dev->vhost_ops->vhost_set_vring_endian(dev, &s)) {
935         return 0;
936     }
937 
938     VHOST_OPS_DEBUG("vhost_set_vring_endian failed");
939     if (errno == ENOTTY) {
940         error_report("vhost does not support cross-endian");
941         return -ENOSYS;
942     }
943 
944     return -errno;
945 }
946 
947 static int vhost_memory_region_lookup(struct vhost_dev *hdev,
948                                       uint64_t gpa, uint64_t *uaddr,
949                                       uint64_t *len)
950 {
951     int i;
952 
953     for (i = 0; i < hdev->mem->nregions; i++) {
954         struct vhost_memory_region *reg = hdev->mem->regions + i;
955 
956         if (gpa >= reg->guest_phys_addr &&
957             reg->guest_phys_addr + reg->memory_size > gpa) {
958             *uaddr = reg->userspace_addr + gpa - reg->guest_phys_addr;
959             *len = reg->guest_phys_addr + reg->memory_size - gpa;
960             return 0;
961         }
962     }
963 
964     return -EFAULT;
965 }
966 
967 void vhost_device_iotlb_miss(struct vhost_dev *dev, uint64_t iova, int write)
968 {
969     IOMMUTLBEntry iotlb;
970     uint64_t uaddr, len;
971 
972     rcu_read_lock();
973 
974     iotlb = address_space_get_iotlb_entry(dev->vdev->dma_as,
975                                           iova, write);
976     if (iotlb.target_as != NULL) {
977         if (vhost_memory_region_lookup(dev, iotlb.translated_addr,
978                                        &uaddr, &len)) {
979             error_report("Fail to lookup the translated address "
980                          "%"PRIx64, iotlb.translated_addr);
981             goto out;
982         }
983 
984         len = MIN(iotlb.addr_mask + 1, len);
985         iova = iova & ~iotlb.addr_mask;
986 
987         if (dev->vhost_ops->vhost_update_device_iotlb(dev, iova, uaddr,
988                                                       len, iotlb.perm)) {
989             error_report("Fail to update device iotlb");
990             goto out;
991         }
992     }
993 out:
994     rcu_read_unlock();
995 }
996 
997 static int vhost_virtqueue_start(struct vhost_dev *dev,
998                                 struct VirtIODevice *vdev,
999                                 struct vhost_virtqueue *vq,
1000                                 unsigned idx)
1001 {
1002     BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(vdev)));
1003     VirtioBusState *vbus = VIRTIO_BUS(qbus);
1004     VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(vbus);
1005     hwaddr s, l, a;
1006     int r;
1007     int vhost_vq_index = dev->vhost_ops->vhost_get_vq_index(dev, idx);
1008     struct vhost_vring_file file = {
1009         .index = vhost_vq_index
1010     };
1011     struct vhost_vring_state state = {
1012         .index = vhost_vq_index
1013     };
1014     struct VirtQueue *vvq = virtio_get_queue(vdev, idx);
1015 
1016 
1017     vq->num = state.num = virtio_queue_get_num(vdev, idx);
1018     r = dev->vhost_ops->vhost_set_vring_num(dev, &state);
1019     if (r) {
1020         VHOST_OPS_DEBUG("vhost_set_vring_num failed");
1021         return -errno;
1022     }
1023 
1024     state.num = virtio_queue_get_last_avail_idx(vdev, idx);
1025     r = dev->vhost_ops->vhost_set_vring_base(dev, &state);
1026     if (r) {
1027         VHOST_OPS_DEBUG("vhost_set_vring_base failed");
1028         return -errno;
1029     }
1030 
1031     if (vhost_needs_vring_endian(vdev)) {
1032         r = vhost_virtqueue_set_vring_endian_legacy(dev,
1033                                                     virtio_is_big_endian(vdev),
1034                                                     vhost_vq_index);
1035         if (r) {
1036             return -errno;
1037         }
1038     }
1039 
1040     vq->desc_size = s = l = virtio_queue_get_desc_size(vdev, idx);
1041     vq->desc_phys = a = virtio_queue_get_desc_addr(vdev, idx);
1042     vq->desc = vhost_memory_map(dev, a, &l, 0);
1043     if (!vq->desc || l != s) {
1044         r = -ENOMEM;
1045         goto fail_alloc_desc;
1046     }
1047     vq->avail_size = s = l = virtio_queue_get_avail_size(vdev, idx);
1048     vq->avail_phys = a = virtio_queue_get_avail_addr(vdev, idx);
1049     vq->avail = vhost_memory_map(dev, a, &l, 0);
1050     if (!vq->avail || l != s) {
1051         r = -ENOMEM;
1052         goto fail_alloc_avail;
1053     }
1054     vq->used_size = s = l = virtio_queue_get_used_size(vdev, idx);
1055     vq->used_phys = a = virtio_queue_get_used_addr(vdev, idx);
1056     vq->used = vhost_memory_map(dev, a, &l, 1);
1057     if (!vq->used || l != s) {
1058         r = -ENOMEM;
1059         goto fail_alloc_used;
1060     }
1061 
1062     r = vhost_virtqueue_set_addr(dev, vq, vhost_vq_index, dev->log_enabled);
1063     if (r < 0) {
1064         r = -errno;
1065         goto fail_alloc;
1066     }
1067 
1068     file.fd = event_notifier_get_fd(virtio_queue_get_host_notifier(vvq));
1069     r = dev->vhost_ops->vhost_set_vring_kick(dev, &file);
1070     if (r) {
1071         VHOST_OPS_DEBUG("vhost_set_vring_kick failed");
1072         r = -errno;
1073         goto fail_kick;
1074     }
1075 
1076     /* Clear and discard previous events if any. */
1077     event_notifier_test_and_clear(&vq->masked_notifier);
1078 
1079     /* Init vring in unmasked state, unless guest_notifier_mask
1080      * will do it later.
1081      */
1082     if (!vdev->use_guest_notifier_mask) {
1083         /* TODO: check and handle errors. */
1084         vhost_virtqueue_mask(dev, vdev, idx, false);
1085     }
1086 
1087     if (k->query_guest_notifiers &&
1088         k->query_guest_notifiers(qbus->parent) &&
1089         virtio_queue_vector(vdev, idx) == VIRTIO_NO_VECTOR) {
1090         file.fd = -1;
1091         r = dev->vhost_ops->vhost_set_vring_call(dev, &file);
1092         if (r) {
1093             goto fail_vector;
1094         }
1095     }
1096 
1097     return 0;
1098 
1099 fail_vector:
1100 fail_kick:
1101 fail_alloc:
1102     vhost_memory_unmap(dev, vq->used, virtio_queue_get_used_size(vdev, idx),
1103                        0, 0);
1104 fail_alloc_used:
1105     vhost_memory_unmap(dev, vq->avail, virtio_queue_get_avail_size(vdev, idx),
1106                        0, 0);
1107 fail_alloc_avail:
1108     vhost_memory_unmap(dev, vq->desc, virtio_queue_get_desc_size(vdev, idx),
1109                        0, 0);
1110 fail_alloc_desc:
1111     return r;
1112 }
1113 
1114 static void vhost_virtqueue_stop(struct vhost_dev *dev,
1115                                     struct VirtIODevice *vdev,
1116                                     struct vhost_virtqueue *vq,
1117                                     unsigned idx)
1118 {
1119     int vhost_vq_index = dev->vhost_ops->vhost_get_vq_index(dev, idx);
1120     struct vhost_vring_state state = {
1121         .index = vhost_vq_index,
1122     };
1123     int r;
1124 
1125     r = dev->vhost_ops->vhost_get_vring_base(dev, &state);
1126     if (r < 0) {
1127         VHOST_OPS_DEBUG("vhost VQ %d ring restore failed: %d", idx, r);
1128     } else {
1129         virtio_queue_set_last_avail_idx(vdev, idx, state.num);
1130     }
1131     virtio_queue_invalidate_signalled_used(vdev, idx);
1132     virtio_queue_update_used_idx(vdev, idx);
1133 
1134     /* In the cross-endian case, we need to reset the vring endianness to
1135      * native as legacy devices expect so by default.
1136      */
1137     if (vhost_needs_vring_endian(vdev)) {
1138         vhost_virtqueue_set_vring_endian_legacy(dev,
1139                                                 !virtio_is_big_endian(vdev),
1140                                                 vhost_vq_index);
1141     }
1142 
1143     vhost_memory_unmap(dev, vq->used, virtio_queue_get_used_size(vdev, idx),
1144                        1, virtio_queue_get_used_size(vdev, idx));
1145     vhost_memory_unmap(dev, vq->avail, virtio_queue_get_avail_size(vdev, idx),
1146                        0, virtio_queue_get_avail_size(vdev, idx));
1147     vhost_memory_unmap(dev, vq->desc, virtio_queue_get_desc_size(vdev, idx),
1148                        0, virtio_queue_get_desc_size(vdev, idx));
1149 }
1150 
1151 static void vhost_eventfd_add(MemoryListener *listener,
1152                               MemoryRegionSection *section,
1153                               bool match_data, uint64_t data, EventNotifier *e)
1154 {
1155 }
1156 
1157 static void vhost_eventfd_del(MemoryListener *listener,
1158                               MemoryRegionSection *section,
1159                               bool match_data, uint64_t data, EventNotifier *e)
1160 {
1161 }
1162 
1163 static int vhost_virtqueue_set_busyloop_timeout(struct vhost_dev *dev,
1164                                                 int n, uint32_t timeout)
1165 {
1166     int vhost_vq_index = dev->vhost_ops->vhost_get_vq_index(dev, n);
1167     struct vhost_vring_state state = {
1168         .index = vhost_vq_index,
1169         .num = timeout,
1170     };
1171     int r;
1172 
1173     if (!dev->vhost_ops->vhost_set_vring_busyloop_timeout) {
1174         return -EINVAL;
1175     }
1176 
1177     r = dev->vhost_ops->vhost_set_vring_busyloop_timeout(dev, &state);
1178     if (r) {
1179         VHOST_OPS_DEBUG("vhost_set_vring_busyloop_timeout failed");
1180         return r;
1181     }
1182 
1183     return 0;
1184 }
1185 
1186 static int vhost_virtqueue_init(struct vhost_dev *dev,
1187                                 struct vhost_virtqueue *vq, int n)
1188 {
1189     int vhost_vq_index = dev->vhost_ops->vhost_get_vq_index(dev, n);
1190     struct vhost_vring_file file = {
1191         .index = vhost_vq_index,
1192     };
1193     int r = event_notifier_init(&vq->masked_notifier, 0);
1194     if (r < 0) {
1195         return r;
1196     }
1197 
1198     file.fd = event_notifier_get_fd(&vq->masked_notifier);
1199     r = dev->vhost_ops->vhost_set_vring_call(dev, &file);
1200     if (r) {
1201         VHOST_OPS_DEBUG("vhost_set_vring_call failed");
1202         r = -errno;
1203         goto fail_call;
1204     }
1205 
1206     vq->dev = dev;
1207 
1208     return 0;
1209 fail_call:
1210     event_notifier_cleanup(&vq->masked_notifier);
1211     return r;
1212 }
1213 
1214 static void vhost_virtqueue_cleanup(struct vhost_virtqueue *vq)
1215 {
1216     event_notifier_cleanup(&vq->masked_notifier);
1217 }
1218 
1219 int vhost_dev_init(struct vhost_dev *hdev, void *opaque,
1220                    VhostBackendType backend_type, uint32_t busyloop_timeout)
1221 {
1222     uint64_t features;
1223     int i, r, n_initialized_vqs = 0;
1224     Error *local_err = NULL;
1225 
1226     hdev->vdev = NULL;
1227     hdev->migration_blocker = NULL;
1228 
1229     r = vhost_set_backend_type(hdev, backend_type);
1230     assert(r >= 0);
1231 
1232     r = hdev->vhost_ops->vhost_backend_init(hdev, opaque);
1233     if (r < 0) {
1234         goto fail;
1235     }
1236 
1237     if (used_memslots > hdev->vhost_ops->vhost_backend_memslots_limit(hdev)) {
1238         error_report("vhost backend memory slots limit is less"
1239                 " than current number of present memory slots");
1240         r = -1;
1241         goto fail;
1242     }
1243 
1244     r = hdev->vhost_ops->vhost_set_owner(hdev);
1245     if (r < 0) {
1246         VHOST_OPS_DEBUG("vhost_set_owner failed");
1247         goto fail;
1248     }
1249 
1250     r = hdev->vhost_ops->vhost_get_features(hdev, &features);
1251     if (r < 0) {
1252         VHOST_OPS_DEBUG("vhost_get_features failed");
1253         goto fail;
1254     }
1255 
1256     for (i = 0; i < hdev->nvqs; ++i, ++n_initialized_vqs) {
1257         r = vhost_virtqueue_init(hdev, hdev->vqs + i, hdev->vq_index + i);
1258         if (r < 0) {
1259             goto fail;
1260         }
1261     }
1262 
1263     if (busyloop_timeout) {
1264         for (i = 0; i < hdev->nvqs; ++i) {
1265             r = vhost_virtqueue_set_busyloop_timeout(hdev, hdev->vq_index + i,
1266                                                      busyloop_timeout);
1267             if (r < 0) {
1268                 goto fail_busyloop;
1269             }
1270         }
1271     }
1272 
1273     hdev->features = features;
1274 
1275     hdev->memory_listener = (MemoryListener) {
1276         .begin = vhost_begin,
1277         .commit = vhost_commit,
1278         .region_add = vhost_region_add,
1279         .region_del = vhost_region_del,
1280         .region_nop = vhost_region_nop,
1281         .log_start = vhost_log_start,
1282         .log_stop = vhost_log_stop,
1283         .log_sync = vhost_log_sync,
1284         .log_global_start = vhost_log_global_start,
1285         .log_global_stop = vhost_log_global_stop,
1286         .eventfd_add = vhost_eventfd_add,
1287         .eventfd_del = vhost_eventfd_del,
1288         .priority = 10
1289     };
1290 
1291     hdev->iommu_listener = (MemoryListener) {
1292         .region_add = vhost_iommu_region_add,
1293         .region_del = vhost_iommu_region_del,
1294     };
1295 
1296     if (hdev->migration_blocker == NULL) {
1297         if (!(hdev->features & (0x1ULL << VHOST_F_LOG_ALL))) {
1298             error_setg(&hdev->migration_blocker,
1299                        "Migration disabled: vhost lacks VHOST_F_LOG_ALL feature.");
1300         } else if (vhost_dev_log_is_shared(hdev) && !qemu_memfd_check()) {
1301             error_setg(&hdev->migration_blocker,
1302                        "Migration disabled: failed to allocate shared memory");
1303         }
1304     }
1305 
1306     if (hdev->migration_blocker != NULL) {
1307         r = migrate_add_blocker(hdev->migration_blocker, &local_err);
1308         if (local_err) {
1309             error_report_err(local_err);
1310             error_free(hdev->migration_blocker);
1311             goto fail_busyloop;
1312         }
1313     }
1314 
1315     hdev->mem = g_malloc0(offsetof(struct vhost_memory, regions));
1316     hdev->n_mem_sections = 0;
1317     hdev->mem_sections = NULL;
1318     hdev->log = NULL;
1319     hdev->log_size = 0;
1320     hdev->log_enabled = false;
1321     hdev->started = false;
1322     hdev->memory_changed = false;
1323     memory_listener_register(&hdev->memory_listener, &address_space_memory);
1324     QLIST_INSERT_HEAD(&vhost_devices, hdev, entry);
1325     return 0;
1326 
1327 fail_busyloop:
1328     while (--i >= 0) {
1329         vhost_virtqueue_set_busyloop_timeout(hdev, hdev->vq_index + i, 0);
1330     }
1331 fail:
1332     hdev->nvqs = n_initialized_vqs;
1333     vhost_dev_cleanup(hdev);
1334     return r;
1335 }
1336 
1337 void vhost_dev_cleanup(struct vhost_dev *hdev)
1338 {
1339     int i;
1340 
1341     for (i = 0; i < hdev->nvqs; ++i) {
1342         vhost_virtqueue_cleanup(hdev->vqs + i);
1343     }
1344     if (hdev->mem) {
1345         /* those are only safe after successful init */
1346         memory_listener_unregister(&hdev->memory_listener);
1347         QLIST_REMOVE(hdev, entry);
1348     }
1349     if (hdev->migration_blocker) {
1350         migrate_del_blocker(hdev->migration_blocker);
1351         error_free(hdev->migration_blocker);
1352     }
1353     g_free(hdev->mem);
1354     g_free(hdev->mem_sections);
1355     if (hdev->vhost_ops) {
1356         hdev->vhost_ops->vhost_backend_cleanup(hdev);
1357     }
1358     assert(!hdev->log);
1359 
1360     memset(hdev, 0, sizeof(struct vhost_dev));
1361 }
1362 
1363 /* Stop processing guest IO notifications in qemu.
1364  * Start processing them in vhost in kernel.
1365  */
1366 int vhost_dev_enable_notifiers(struct vhost_dev *hdev, VirtIODevice *vdev)
1367 {
1368     BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(vdev)));
1369     int i, r, e;
1370 
1371     /* We will pass the notifiers to the kernel, make sure that QEMU
1372      * doesn't interfere.
1373      */
1374     r = virtio_device_grab_ioeventfd(vdev);
1375     if (r < 0) {
1376         error_report("binding does not support host notifiers");
1377         goto fail;
1378     }
1379 
1380     for (i = 0; i < hdev->nvqs; ++i) {
1381         r = virtio_bus_set_host_notifier(VIRTIO_BUS(qbus), hdev->vq_index + i,
1382                                          true);
1383         if (r < 0) {
1384             error_report("vhost VQ %d notifier binding failed: %d", i, -r);
1385             goto fail_vq;
1386         }
1387     }
1388 
1389     return 0;
1390 fail_vq:
1391     while (--i >= 0) {
1392         e = virtio_bus_set_host_notifier(VIRTIO_BUS(qbus), hdev->vq_index + i,
1393                                          false);
1394         if (e < 0) {
1395             error_report("vhost VQ %d notifier cleanup error: %d", i, -r);
1396         }
1397         assert (e >= 0);
1398     }
1399     virtio_device_release_ioeventfd(vdev);
1400 fail:
1401     return r;
1402 }
1403 
1404 /* Stop processing guest IO notifications in vhost.
1405  * Start processing them in qemu.
1406  * This might actually run the qemu handlers right away,
1407  * so virtio in qemu must be completely setup when this is called.
1408  */
1409 void vhost_dev_disable_notifiers(struct vhost_dev *hdev, VirtIODevice *vdev)
1410 {
1411     BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(vdev)));
1412     int i, r;
1413 
1414     for (i = 0; i < hdev->nvqs; ++i) {
1415         r = virtio_bus_set_host_notifier(VIRTIO_BUS(qbus), hdev->vq_index + i,
1416                                          false);
1417         if (r < 0) {
1418             error_report("vhost VQ %d notifier cleanup failed: %d", i, -r);
1419         }
1420         assert (r >= 0);
1421     }
1422     virtio_device_release_ioeventfd(vdev);
1423 }
1424 
1425 /* Test and clear event pending status.
1426  * Should be called after unmask to avoid losing events.
1427  */
1428 bool vhost_virtqueue_pending(struct vhost_dev *hdev, int n)
1429 {
1430     struct vhost_virtqueue *vq = hdev->vqs + n - hdev->vq_index;
1431     assert(n >= hdev->vq_index && n < hdev->vq_index + hdev->nvqs);
1432     return event_notifier_test_and_clear(&vq->masked_notifier);
1433 }
1434 
1435 /* Mask/unmask events from this vq. */
1436 void vhost_virtqueue_mask(struct vhost_dev *hdev, VirtIODevice *vdev, int n,
1437                          bool mask)
1438 {
1439     struct VirtQueue *vvq = virtio_get_queue(vdev, n);
1440     int r, index = n - hdev->vq_index;
1441     struct vhost_vring_file file;
1442 
1443     /* should only be called after backend is connected */
1444     assert(hdev->vhost_ops);
1445 
1446     if (mask) {
1447         assert(vdev->use_guest_notifier_mask);
1448         file.fd = event_notifier_get_fd(&hdev->vqs[index].masked_notifier);
1449     } else {
1450         file.fd = event_notifier_get_fd(virtio_queue_get_guest_notifier(vvq));
1451     }
1452 
1453     file.index = hdev->vhost_ops->vhost_get_vq_index(hdev, n);
1454     r = hdev->vhost_ops->vhost_set_vring_call(hdev, &file);
1455     if (r < 0) {
1456         VHOST_OPS_DEBUG("vhost_set_vring_call failed");
1457     }
1458 }
1459 
1460 uint64_t vhost_get_features(struct vhost_dev *hdev, const int *feature_bits,
1461                             uint64_t features)
1462 {
1463     const int *bit = feature_bits;
1464     while (*bit != VHOST_INVALID_FEATURE_BIT) {
1465         uint64_t bit_mask = (1ULL << *bit);
1466         if (!(hdev->features & bit_mask)) {
1467             features &= ~bit_mask;
1468         }
1469         bit++;
1470     }
1471     return features;
1472 }
1473 
1474 void vhost_ack_features(struct vhost_dev *hdev, const int *feature_bits,
1475                         uint64_t features)
1476 {
1477     const int *bit = feature_bits;
1478     while (*bit != VHOST_INVALID_FEATURE_BIT) {
1479         uint64_t bit_mask = (1ULL << *bit);
1480         if (features & bit_mask) {
1481             hdev->acked_features |= bit_mask;
1482         }
1483         bit++;
1484     }
1485 }
1486 
1487 /* Host notifiers must be enabled at this point. */
1488 int vhost_dev_start(struct vhost_dev *hdev, VirtIODevice *vdev)
1489 {
1490     int i, r;
1491 
1492     /* should only be called after backend is connected */
1493     assert(hdev->vhost_ops);
1494 
1495     hdev->started = true;
1496     hdev->vdev = vdev;
1497 
1498     r = vhost_dev_set_features(hdev, hdev->log_enabled);
1499     if (r < 0) {
1500         goto fail_features;
1501     }
1502 
1503     if (vhost_dev_has_iommu(hdev)) {
1504         memory_listener_register(&hdev->iommu_listener, vdev->dma_as);
1505     }
1506 
1507     r = hdev->vhost_ops->vhost_set_mem_table(hdev, hdev->mem);
1508     if (r < 0) {
1509         VHOST_OPS_DEBUG("vhost_set_mem_table failed");
1510         r = -errno;
1511         goto fail_mem;
1512     }
1513     for (i = 0; i < hdev->nvqs; ++i) {
1514         r = vhost_virtqueue_start(hdev,
1515                                   vdev,
1516                                   hdev->vqs + i,
1517                                   hdev->vq_index + i);
1518         if (r < 0) {
1519             goto fail_vq;
1520         }
1521     }
1522 
1523     if (hdev->log_enabled) {
1524         uint64_t log_base;
1525 
1526         hdev->log_size = vhost_get_log_size(hdev);
1527         hdev->log = vhost_log_get(hdev->log_size,
1528                                   vhost_dev_log_is_shared(hdev));
1529         log_base = (uintptr_t)hdev->log->log;
1530         r = hdev->vhost_ops->vhost_set_log_base(hdev,
1531                                                 hdev->log_size ? log_base : 0,
1532                                                 hdev->log);
1533         if (r < 0) {
1534             VHOST_OPS_DEBUG("vhost_set_log_base failed");
1535             r = -errno;
1536             goto fail_log;
1537         }
1538     }
1539 
1540     if (vhost_dev_has_iommu(hdev)) {
1541         hdev->vhost_ops->vhost_set_iotlb_callback(hdev, true);
1542 
1543         /* Update used ring information for IOTLB to work correctly,
1544          * vhost-kernel code requires for this.*/
1545         for (i = 0; i < hdev->nvqs; ++i) {
1546             struct vhost_virtqueue *vq = hdev->vqs + i;
1547             vhost_device_iotlb_miss(hdev, vq->used_phys, true);
1548         }
1549     }
1550     return 0;
1551 fail_log:
1552     vhost_log_put(hdev, false);
1553 fail_vq:
1554     while (--i >= 0) {
1555         vhost_virtqueue_stop(hdev,
1556                              vdev,
1557                              hdev->vqs + i,
1558                              hdev->vq_index + i);
1559     }
1560     i = hdev->nvqs;
1561 
1562 fail_mem:
1563 fail_features:
1564 
1565     hdev->started = false;
1566     return r;
1567 }
1568 
1569 /* Host notifiers must be enabled at this point. */
1570 void vhost_dev_stop(struct vhost_dev *hdev, VirtIODevice *vdev)
1571 {
1572     int i;
1573 
1574     /* should only be called after backend is connected */
1575     assert(hdev->vhost_ops);
1576 
1577     for (i = 0; i < hdev->nvqs; ++i) {
1578         vhost_virtqueue_stop(hdev,
1579                              vdev,
1580                              hdev->vqs + i,
1581                              hdev->vq_index + i);
1582     }
1583 
1584     if (vhost_dev_has_iommu(hdev)) {
1585         hdev->vhost_ops->vhost_set_iotlb_callback(hdev, false);
1586         memory_listener_unregister(&hdev->iommu_listener);
1587     }
1588     vhost_log_put(hdev, true);
1589     hdev->started = false;
1590     hdev->vdev = NULL;
1591 }
1592 
1593 int vhost_net_set_backend(struct vhost_dev *hdev,
1594                           struct vhost_vring_file *file)
1595 {
1596     if (hdev->vhost_ops->vhost_net_set_backend) {
1597         return hdev->vhost_ops->vhost_net_set_backend(hdev, file);
1598     }
1599 
1600     return -1;
1601 }
1602