xref: /openbmc/qemu/hw/virtio/vhost.c (revision 49946538)
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 "hw/virtio/vhost.h"
17 #include "hw/hw.h"
18 #include "qemu/atomic.h"
19 #include "qemu/range.h"
20 #include <linux/vhost.h>
21 #include "exec/address-spaces.h"
22 #include "hw/virtio/virtio-bus.h"
23 #include "migration/migration.h"
24 
25 static void vhost_dev_sync_region(struct vhost_dev *dev,
26                                   MemoryRegionSection *section,
27                                   uint64_t mfirst, uint64_t mlast,
28                                   uint64_t rfirst, uint64_t rlast)
29 {
30     uint64_t start = MAX(mfirst, rfirst);
31     uint64_t end = MIN(mlast, rlast);
32     vhost_log_chunk_t *from = dev->log + start / VHOST_LOG_CHUNK;
33     vhost_log_chunk_t *to = dev->log + end / VHOST_LOG_CHUNK + 1;
34     uint64_t addr = (start / VHOST_LOG_CHUNK) * VHOST_LOG_CHUNK;
35 
36     if (end < start) {
37         return;
38     }
39     assert(end / VHOST_LOG_CHUNK < dev->log_size);
40     assert(start / VHOST_LOG_CHUNK < dev->log_size);
41 
42     for (;from < to; ++from) {
43         vhost_log_chunk_t log;
44         /* We first check with non-atomic: much cheaper,
45          * and we expect non-dirty to be the common case. */
46         if (!*from) {
47             addr += VHOST_LOG_CHUNK;
48             continue;
49         }
50         /* Data must be read atomically. We don't really need barrier semantics
51          * but it's easier to use atomic_* than roll our own. */
52         log = atomic_xchg(from, 0);
53         while (log) {
54             int bit = ctzl(log);
55             hwaddr page_addr;
56             hwaddr section_offset;
57             hwaddr mr_offset;
58             page_addr = addr + bit * VHOST_LOG_PAGE;
59             section_offset = page_addr - section->offset_within_address_space;
60             mr_offset = section_offset + section->offset_within_region;
61             memory_region_set_dirty(section->mr, mr_offset, VHOST_LOG_PAGE);
62             log &= ~(0x1ull << bit);
63         }
64         addr += VHOST_LOG_CHUNK;
65     }
66 }
67 
68 static int vhost_sync_dirty_bitmap(struct vhost_dev *dev,
69                                    MemoryRegionSection *section,
70                                    hwaddr first,
71                                    hwaddr last)
72 {
73     int i;
74     hwaddr start_addr;
75     hwaddr end_addr;
76 
77     if (!dev->log_enabled || !dev->started) {
78         return 0;
79     }
80     start_addr = section->offset_within_address_space;
81     end_addr = range_get_last(start_addr, int128_get64(section->size));
82     start_addr = MAX(first, start_addr);
83     end_addr = MIN(last, end_addr);
84 
85     for (i = 0; i < dev->mem->nregions; ++i) {
86         struct vhost_memory_region *reg = dev->mem->regions + i;
87         vhost_dev_sync_region(dev, section, start_addr, end_addr,
88                               reg->guest_phys_addr,
89                               range_get_last(reg->guest_phys_addr,
90                                              reg->memory_size));
91     }
92     for (i = 0; i < dev->nvqs; ++i) {
93         struct vhost_virtqueue *vq = dev->vqs + i;
94         vhost_dev_sync_region(dev, section, start_addr, end_addr, vq->used_phys,
95                               range_get_last(vq->used_phys, vq->used_size));
96     }
97     return 0;
98 }
99 
100 static void vhost_log_sync(MemoryListener *listener,
101                           MemoryRegionSection *section)
102 {
103     struct vhost_dev *dev = container_of(listener, struct vhost_dev,
104                                          memory_listener);
105     vhost_sync_dirty_bitmap(dev, section, 0x0, ~0x0ULL);
106 }
107 
108 static void vhost_log_sync_range(struct vhost_dev *dev,
109                                  hwaddr first, hwaddr last)
110 {
111     int i;
112     /* FIXME: this is N^2 in number of sections */
113     for (i = 0; i < dev->n_mem_sections; ++i) {
114         MemoryRegionSection *section = &dev->mem_sections[i];
115         vhost_sync_dirty_bitmap(dev, section, first, last);
116     }
117 }
118 
119 /* Assign/unassign. Keep an unsorted array of non-overlapping
120  * memory regions in dev->mem. */
121 static void vhost_dev_unassign_memory(struct vhost_dev *dev,
122                                       uint64_t start_addr,
123                                       uint64_t size)
124 {
125     int from, to, n = dev->mem->nregions;
126     /* Track overlapping/split regions for sanity checking. */
127     int overlap_start = 0, overlap_end = 0, overlap_middle = 0, split = 0;
128 
129     for (from = 0, to = 0; from < n; ++from, ++to) {
130         struct vhost_memory_region *reg = dev->mem->regions + to;
131         uint64_t reglast;
132         uint64_t memlast;
133         uint64_t change;
134 
135         /* clone old region */
136         if (to != from) {
137             memcpy(reg, dev->mem->regions + from, sizeof *reg);
138         }
139 
140         /* No overlap is simple */
141         if (!ranges_overlap(reg->guest_phys_addr, reg->memory_size,
142                             start_addr, size)) {
143             continue;
144         }
145 
146         /* Split only happens if supplied region
147          * is in the middle of an existing one. Thus it can not
148          * overlap with any other existing region. */
149         assert(!split);
150 
151         reglast = range_get_last(reg->guest_phys_addr, reg->memory_size);
152         memlast = range_get_last(start_addr, size);
153 
154         /* Remove whole region */
155         if (start_addr <= reg->guest_phys_addr && memlast >= reglast) {
156             --dev->mem->nregions;
157             --to;
158             ++overlap_middle;
159             continue;
160         }
161 
162         /* Shrink region */
163         if (memlast >= reglast) {
164             reg->memory_size = start_addr - reg->guest_phys_addr;
165             assert(reg->memory_size);
166             assert(!overlap_end);
167             ++overlap_end;
168             continue;
169         }
170 
171         /* Shift region */
172         if (start_addr <= reg->guest_phys_addr) {
173             change = memlast + 1 - reg->guest_phys_addr;
174             reg->memory_size -= change;
175             reg->guest_phys_addr += change;
176             reg->userspace_addr += change;
177             assert(reg->memory_size);
178             assert(!overlap_start);
179             ++overlap_start;
180             continue;
181         }
182 
183         /* This only happens if supplied region
184          * is in the middle of an existing one. Thus it can not
185          * overlap with any other existing region. */
186         assert(!overlap_start);
187         assert(!overlap_end);
188         assert(!overlap_middle);
189         /* Split region: shrink first part, shift second part. */
190         memcpy(dev->mem->regions + n, reg, sizeof *reg);
191         reg->memory_size = start_addr - reg->guest_phys_addr;
192         assert(reg->memory_size);
193         change = memlast + 1 - reg->guest_phys_addr;
194         reg = dev->mem->regions + n;
195         reg->memory_size -= change;
196         assert(reg->memory_size);
197         reg->guest_phys_addr += change;
198         reg->userspace_addr += change;
199         /* Never add more than 1 region */
200         assert(dev->mem->nregions == n);
201         ++dev->mem->nregions;
202         ++split;
203     }
204 }
205 
206 /* Called after unassign, so no regions overlap the given range. */
207 static void vhost_dev_assign_memory(struct vhost_dev *dev,
208                                     uint64_t start_addr,
209                                     uint64_t size,
210                                     uint64_t uaddr)
211 {
212     int from, to;
213     struct vhost_memory_region *merged = NULL;
214     for (from = 0, to = 0; from < dev->mem->nregions; ++from, ++to) {
215         struct vhost_memory_region *reg = dev->mem->regions + to;
216         uint64_t prlast, urlast;
217         uint64_t pmlast, umlast;
218         uint64_t s, e, u;
219 
220         /* clone old region */
221         if (to != from) {
222             memcpy(reg, dev->mem->regions + from, sizeof *reg);
223         }
224         prlast = range_get_last(reg->guest_phys_addr, reg->memory_size);
225         pmlast = range_get_last(start_addr, size);
226         urlast = range_get_last(reg->userspace_addr, reg->memory_size);
227         umlast = range_get_last(uaddr, size);
228 
229         /* check for overlapping regions: should never happen. */
230         assert(prlast < start_addr || pmlast < reg->guest_phys_addr);
231         /* Not an adjacent or overlapping region - do not merge. */
232         if ((prlast + 1 != start_addr || urlast + 1 != uaddr) &&
233             (pmlast + 1 != reg->guest_phys_addr ||
234              umlast + 1 != reg->userspace_addr)) {
235             continue;
236         }
237 
238         if (merged) {
239             --to;
240             assert(to >= 0);
241         } else {
242             merged = reg;
243         }
244         u = MIN(uaddr, reg->userspace_addr);
245         s = MIN(start_addr, reg->guest_phys_addr);
246         e = MAX(pmlast, prlast);
247         uaddr = merged->userspace_addr = u;
248         start_addr = merged->guest_phys_addr = s;
249         size = merged->memory_size = e - s + 1;
250         assert(merged->memory_size);
251     }
252 
253     if (!merged) {
254         struct vhost_memory_region *reg = dev->mem->regions + to;
255         memset(reg, 0, sizeof *reg);
256         reg->memory_size = size;
257         assert(reg->memory_size);
258         reg->guest_phys_addr = start_addr;
259         reg->userspace_addr = uaddr;
260         ++to;
261     }
262     assert(to <= dev->mem->nregions + 1);
263     dev->mem->nregions = to;
264 }
265 
266 static uint64_t vhost_get_log_size(struct vhost_dev *dev)
267 {
268     uint64_t log_size = 0;
269     int i;
270     for (i = 0; i < dev->mem->nregions; ++i) {
271         struct vhost_memory_region *reg = dev->mem->regions + i;
272         uint64_t last = range_get_last(reg->guest_phys_addr,
273                                        reg->memory_size);
274         log_size = MAX(log_size, last / VHOST_LOG_CHUNK + 1);
275     }
276     for (i = 0; i < dev->nvqs; ++i) {
277         struct vhost_virtqueue *vq = dev->vqs + i;
278         uint64_t last = vq->used_phys + vq->used_size - 1;
279         log_size = MAX(log_size, last / VHOST_LOG_CHUNK + 1);
280     }
281     return log_size;
282 }
283 
284 static inline void vhost_dev_log_resize(struct vhost_dev* dev, uint64_t size)
285 {
286     vhost_log_chunk_t *log;
287     uint64_t log_base;
288     int r;
289 
290     log = g_malloc0(size * sizeof *log);
291     log_base = (uint64_t)(unsigned long)log;
292     r = dev->vhost_ops->vhost_call(dev, VHOST_SET_LOG_BASE, &log_base);
293     assert(r >= 0);
294     /* Sync only the range covered by the old log */
295     if (dev->log_size) {
296         vhost_log_sync_range(dev, 0, dev->log_size * VHOST_LOG_CHUNK - 1);
297     }
298     g_free(dev->log);
299     dev->log = log;
300     dev->log_size = size;
301 }
302 
303 static int vhost_verify_ring_mappings(struct vhost_dev *dev,
304                                       uint64_t start_addr,
305                                       uint64_t size)
306 {
307     int i;
308     int r = 0;
309 
310     for (i = 0; !r && i < dev->nvqs; ++i) {
311         struct vhost_virtqueue *vq = dev->vqs + i;
312         hwaddr l;
313         void *p;
314 
315         if (!ranges_overlap(start_addr, size, vq->ring_phys, vq->ring_size)) {
316             continue;
317         }
318         l = vq->ring_size;
319         p = cpu_physical_memory_map(vq->ring_phys, &l, 1);
320         if (!p || l != vq->ring_size) {
321             fprintf(stderr, "Unable to map ring buffer for ring %d\n", i);
322             r = -ENOMEM;
323         }
324         if (p != vq->ring) {
325             fprintf(stderr, "Ring buffer relocated for ring %d\n", i);
326             r = -EBUSY;
327         }
328         cpu_physical_memory_unmap(p, l, 0, 0);
329     }
330     return r;
331 }
332 
333 static struct vhost_memory_region *vhost_dev_find_reg(struct vhost_dev *dev,
334 						      uint64_t start_addr,
335 						      uint64_t size)
336 {
337     int i, n = dev->mem->nregions;
338     for (i = 0; i < n; ++i) {
339         struct vhost_memory_region *reg = dev->mem->regions + i;
340         if (ranges_overlap(reg->guest_phys_addr, reg->memory_size,
341                            start_addr, size)) {
342             return reg;
343         }
344     }
345     return NULL;
346 }
347 
348 static bool vhost_dev_cmp_memory(struct vhost_dev *dev,
349                                  uint64_t start_addr,
350                                  uint64_t size,
351                                  uint64_t uaddr)
352 {
353     struct vhost_memory_region *reg = vhost_dev_find_reg(dev, start_addr, size);
354     uint64_t reglast;
355     uint64_t memlast;
356 
357     if (!reg) {
358         return true;
359     }
360 
361     reglast = range_get_last(reg->guest_phys_addr, reg->memory_size);
362     memlast = range_get_last(start_addr, size);
363 
364     /* Need to extend region? */
365     if (start_addr < reg->guest_phys_addr || memlast > reglast) {
366         return true;
367     }
368     /* userspace_addr changed? */
369     return uaddr != reg->userspace_addr + start_addr - reg->guest_phys_addr;
370 }
371 
372 static void vhost_set_memory(MemoryListener *listener,
373                              MemoryRegionSection *section,
374                              bool add)
375 {
376     struct vhost_dev *dev = container_of(listener, struct vhost_dev,
377                                          memory_listener);
378     hwaddr start_addr = section->offset_within_address_space;
379     ram_addr_t size = int128_get64(section->size);
380     bool log_dirty = memory_region_is_logging(section->mr);
381     int s = offsetof(struct vhost_memory, regions) +
382         (dev->mem->nregions + 1) * sizeof dev->mem->regions[0];
383     void *ram;
384 
385     dev->mem = g_realloc(dev->mem, s);
386 
387     if (log_dirty) {
388         add = false;
389     }
390 
391     assert(size);
392 
393     /* Optimize no-change case. At least cirrus_vga does this a lot at this time. */
394     ram = memory_region_get_ram_ptr(section->mr) + section->offset_within_region;
395     if (add) {
396         if (!vhost_dev_cmp_memory(dev, start_addr, size, (uintptr_t)ram)) {
397             /* Region exists with same address. Nothing to do. */
398             return;
399         }
400     } else {
401         if (!vhost_dev_find_reg(dev, start_addr, size)) {
402             /* Removing region that we don't access. Nothing to do. */
403             return;
404         }
405     }
406 
407     vhost_dev_unassign_memory(dev, start_addr, size);
408     if (add) {
409         /* Add given mapping, merging adjacent regions if any */
410         vhost_dev_assign_memory(dev, start_addr, size, (uintptr_t)ram);
411     } else {
412         /* Remove old mapping for this memory, if any. */
413         vhost_dev_unassign_memory(dev, start_addr, size);
414     }
415     dev->mem_changed_start_addr = MIN(dev->mem_changed_start_addr, start_addr);
416     dev->mem_changed_end_addr = MAX(dev->mem_changed_end_addr, start_addr + size - 1);
417     dev->memory_changed = true;
418 }
419 
420 static bool vhost_section(MemoryRegionSection *section)
421 {
422     return memory_region_is_ram(section->mr);
423 }
424 
425 static void vhost_begin(MemoryListener *listener)
426 {
427     struct vhost_dev *dev = container_of(listener, struct vhost_dev,
428                                          memory_listener);
429     dev->mem_changed_end_addr = 0;
430     dev->mem_changed_start_addr = -1;
431 }
432 
433 static void vhost_commit(MemoryListener *listener)
434 {
435     struct vhost_dev *dev = container_of(listener, struct vhost_dev,
436                                          memory_listener);
437     hwaddr start_addr = 0;
438     ram_addr_t size = 0;
439     uint64_t log_size;
440     int r;
441 
442     if (!dev->memory_changed) {
443         return;
444     }
445     if (!dev->started) {
446         return;
447     }
448     if (dev->mem_changed_start_addr > dev->mem_changed_end_addr) {
449         return;
450     }
451 
452     if (dev->started) {
453         start_addr = dev->mem_changed_start_addr;
454         size = dev->mem_changed_end_addr - dev->mem_changed_start_addr + 1;
455 
456         r = vhost_verify_ring_mappings(dev, start_addr, size);
457         assert(r >= 0);
458     }
459 
460     if (!dev->log_enabled) {
461         r = dev->vhost_ops->vhost_call(dev, VHOST_SET_MEM_TABLE, dev->mem);
462         assert(r >= 0);
463         dev->memory_changed = false;
464         return;
465     }
466     log_size = vhost_get_log_size(dev);
467     /* We allocate an extra 4K bytes to log,
468      * to reduce the * number of reallocations. */
469 #define VHOST_LOG_BUFFER (0x1000 / sizeof *dev->log)
470     /* To log more, must increase log size before table update. */
471     if (dev->log_size < log_size) {
472         vhost_dev_log_resize(dev, log_size + VHOST_LOG_BUFFER);
473     }
474     r = dev->vhost_ops->vhost_call(dev, VHOST_SET_MEM_TABLE, dev->mem);
475     assert(r >= 0);
476     /* To log less, can only decrease log size after table update. */
477     if (dev->log_size > log_size + VHOST_LOG_BUFFER) {
478         vhost_dev_log_resize(dev, log_size);
479     }
480     dev->memory_changed = false;
481 }
482 
483 static void vhost_region_add(MemoryListener *listener,
484                              MemoryRegionSection *section)
485 {
486     struct vhost_dev *dev = container_of(listener, struct vhost_dev,
487                                          memory_listener);
488 
489     if (!vhost_section(section)) {
490         return;
491     }
492 
493     ++dev->n_mem_sections;
494     dev->mem_sections = g_renew(MemoryRegionSection, dev->mem_sections,
495                                 dev->n_mem_sections);
496     dev->mem_sections[dev->n_mem_sections - 1] = *section;
497     memory_region_ref(section->mr);
498     vhost_set_memory(listener, section, true);
499 }
500 
501 static void vhost_region_del(MemoryListener *listener,
502                              MemoryRegionSection *section)
503 {
504     struct vhost_dev *dev = container_of(listener, struct vhost_dev,
505                                          memory_listener);
506     int i;
507 
508     if (!vhost_section(section)) {
509         return;
510     }
511 
512     vhost_set_memory(listener, section, false);
513     memory_region_unref(section->mr);
514     for (i = 0; i < dev->n_mem_sections; ++i) {
515         if (dev->mem_sections[i].offset_within_address_space
516             == section->offset_within_address_space) {
517             --dev->n_mem_sections;
518             memmove(&dev->mem_sections[i], &dev->mem_sections[i+1],
519                     (dev->n_mem_sections - i) * sizeof(*dev->mem_sections));
520             break;
521         }
522     }
523 }
524 
525 static void vhost_region_nop(MemoryListener *listener,
526                              MemoryRegionSection *section)
527 {
528 }
529 
530 static int vhost_virtqueue_set_addr(struct vhost_dev *dev,
531                                     struct vhost_virtqueue *vq,
532                                     unsigned idx, bool enable_log)
533 {
534     struct vhost_vring_addr addr = {
535         .index = idx,
536         .desc_user_addr = (uint64_t)(unsigned long)vq->desc,
537         .avail_user_addr = (uint64_t)(unsigned long)vq->avail,
538         .used_user_addr = (uint64_t)(unsigned long)vq->used,
539         .log_guest_addr = vq->used_phys,
540         .flags = enable_log ? (1 << VHOST_VRING_F_LOG) : 0,
541     };
542     int r = dev->vhost_ops->vhost_call(dev, VHOST_SET_VRING_ADDR, &addr);
543     if (r < 0) {
544         return -errno;
545     }
546     return 0;
547 }
548 
549 static int vhost_dev_set_features(struct vhost_dev *dev, bool enable_log)
550 {
551     uint64_t features = dev->acked_features;
552     int r;
553     if (enable_log) {
554         features |= 0x1 << VHOST_F_LOG_ALL;
555     }
556     r = dev->vhost_ops->vhost_call(dev, VHOST_SET_FEATURES, &features);
557     return r < 0 ? -errno : 0;
558 }
559 
560 static int vhost_dev_set_log(struct vhost_dev *dev, bool enable_log)
561 {
562     int r, t, i;
563     r = vhost_dev_set_features(dev, enable_log);
564     if (r < 0) {
565         goto err_features;
566     }
567     for (i = 0; i < dev->nvqs; ++i) {
568         r = vhost_virtqueue_set_addr(dev, dev->vqs + i, i,
569                                      enable_log);
570         if (r < 0) {
571             goto err_vq;
572         }
573     }
574     return 0;
575 err_vq:
576     for (; i >= 0; --i) {
577         t = vhost_virtqueue_set_addr(dev, dev->vqs + i, i,
578                                      dev->log_enabled);
579         assert(t >= 0);
580     }
581     t = vhost_dev_set_features(dev, dev->log_enabled);
582     assert(t >= 0);
583 err_features:
584     return r;
585 }
586 
587 static int vhost_migration_log(MemoryListener *listener, int enable)
588 {
589     struct vhost_dev *dev = container_of(listener, struct vhost_dev,
590                                          memory_listener);
591     int r;
592     if (!!enable == dev->log_enabled) {
593         return 0;
594     }
595     if (!dev->started) {
596         dev->log_enabled = enable;
597         return 0;
598     }
599     if (!enable) {
600         r = vhost_dev_set_log(dev, false);
601         if (r < 0) {
602             return r;
603         }
604         g_free(dev->log);
605         dev->log = NULL;
606         dev->log_size = 0;
607     } else {
608         vhost_dev_log_resize(dev, vhost_get_log_size(dev));
609         r = vhost_dev_set_log(dev, true);
610         if (r < 0) {
611             return r;
612         }
613     }
614     dev->log_enabled = enable;
615     return 0;
616 }
617 
618 static void vhost_log_global_start(MemoryListener *listener)
619 {
620     int r;
621 
622     r = vhost_migration_log(listener, true);
623     if (r < 0) {
624         abort();
625     }
626 }
627 
628 static void vhost_log_global_stop(MemoryListener *listener)
629 {
630     int r;
631 
632     r = vhost_migration_log(listener, false);
633     if (r < 0) {
634         abort();
635     }
636 }
637 
638 static void vhost_log_start(MemoryListener *listener,
639                             MemoryRegionSection *section)
640 {
641     /* FIXME: implement */
642 }
643 
644 static void vhost_log_stop(MemoryListener *listener,
645                            MemoryRegionSection *section)
646 {
647     /* FIXME: implement */
648 }
649 
650 static int vhost_virtqueue_start(struct vhost_dev *dev,
651                                 struct VirtIODevice *vdev,
652                                 struct vhost_virtqueue *vq,
653                                 unsigned idx)
654 {
655     hwaddr s, l, a;
656     int r;
657     int vhost_vq_index = idx - dev->vq_index;
658     struct vhost_vring_file file = {
659         .index = vhost_vq_index
660     };
661     struct vhost_vring_state state = {
662         .index = vhost_vq_index
663     };
664     struct VirtQueue *vvq = virtio_get_queue(vdev, idx);
665 
666     assert(idx >= dev->vq_index && idx < dev->vq_index + dev->nvqs);
667 
668     vq->num = state.num = virtio_queue_get_num(vdev, idx);
669     r = dev->vhost_ops->vhost_call(dev, VHOST_SET_VRING_NUM, &state);
670     if (r) {
671         return -errno;
672     }
673 
674     state.num = virtio_queue_get_last_avail_idx(vdev, idx);
675     r = dev->vhost_ops->vhost_call(dev, VHOST_SET_VRING_BASE, &state);
676     if (r) {
677         return -errno;
678     }
679 
680     s = l = virtio_queue_get_desc_size(vdev, idx);
681     a = virtio_queue_get_desc_addr(vdev, idx);
682     vq->desc = cpu_physical_memory_map(a, &l, 0);
683     if (!vq->desc || l != s) {
684         r = -ENOMEM;
685         goto fail_alloc_desc;
686     }
687     s = l = virtio_queue_get_avail_size(vdev, idx);
688     a = virtio_queue_get_avail_addr(vdev, idx);
689     vq->avail = cpu_physical_memory_map(a, &l, 0);
690     if (!vq->avail || l != s) {
691         r = -ENOMEM;
692         goto fail_alloc_avail;
693     }
694     vq->used_size = s = l = virtio_queue_get_used_size(vdev, idx);
695     vq->used_phys = a = virtio_queue_get_used_addr(vdev, idx);
696     vq->used = cpu_physical_memory_map(a, &l, 1);
697     if (!vq->used || l != s) {
698         r = -ENOMEM;
699         goto fail_alloc_used;
700     }
701 
702     vq->ring_size = s = l = virtio_queue_get_ring_size(vdev, idx);
703     vq->ring_phys = a = virtio_queue_get_ring_addr(vdev, idx);
704     vq->ring = cpu_physical_memory_map(a, &l, 1);
705     if (!vq->ring || l != s) {
706         r = -ENOMEM;
707         goto fail_alloc_ring;
708     }
709 
710     r = vhost_virtqueue_set_addr(dev, vq, vhost_vq_index, dev->log_enabled);
711     if (r < 0) {
712         r = -errno;
713         goto fail_alloc;
714     }
715 
716     file.fd = event_notifier_get_fd(virtio_queue_get_host_notifier(vvq));
717     r = dev->vhost_ops->vhost_call(dev, VHOST_SET_VRING_KICK, &file);
718     if (r) {
719         r = -errno;
720         goto fail_kick;
721     }
722 
723     /* Clear and discard previous events if any. */
724     event_notifier_test_and_clear(&vq->masked_notifier);
725 
726     return 0;
727 
728 fail_kick:
729 fail_alloc:
730     cpu_physical_memory_unmap(vq->ring, virtio_queue_get_ring_size(vdev, idx),
731                               0, 0);
732 fail_alloc_ring:
733     cpu_physical_memory_unmap(vq->used, virtio_queue_get_used_size(vdev, idx),
734                               0, 0);
735 fail_alloc_used:
736     cpu_physical_memory_unmap(vq->avail, virtio_queue_get_avail_size(vdev, idx),
737                               0, 0);
738 fail_alloc_avail:
739     cpu_physical_memory_unmap(vq->desc, virtio_queue_get_desc_size(vdev, idx),
740                               0, 0);
741 fail_alloc_desc:
742     return r;
743 }
744 
745 static void vhost_virtqueue_stop(struct vhost_dev *dev,
746                                     struct VirtIODevice *vdev,
747                                     struct vhost_virtqueue *vq,
748                                     unsigned idx)
749 {
750     struct vhost_vring_state state = {
751         .index = idx - dev->vq_index
752     };
753     int r;
754     assert(idx >= dev->vq_index && idx < dev->vq_index + dev->nvqs);
755     r = dev->vhost_ops->vhost_call(dev, VHOST_GET_VRING_BASE, &state);
756     if (r < 0) {
757         fprintf(stderr, "vhost VQ %d ring restore failed: %d\n", idx, r);
758         fflush(stderr);
759     }
760     virtio_queue_set_last_avail_idx(vdev, idx, state.num);
761     virtio_queue_invalidate_signalled_used(vdev, idx);
762     assert (r >= 0);
763     cpu_physical_memory_unmap(vq->ring, virtio_queue_get_ring_size(vdev, idx),
764                               0, virtio_queue_get_ring_size(vdev, idx));
765     cpu_physical_memory_unmap(vq->used, virtio_queue_get_used_size(vdev, idx),
766                               1, virtio_queue_get_used_size(vdev, idx));
767     cpu_physical_memory_unmap(vq->avail, virtio_queue_get_avail_size(vdev, idx),
768                               0, virtio_queue_get_avail_size(vdev, idx));
769     cpu_physical_memory_unmap(vq->desc, virtio_queue_get_desc_size(vdev, idx),
770                               0, virtio_queue_get_desc_size(vdev, idx));
771 }
772 
773 static void vhost_eventfd_add(MemoryListener *listener,
774                               MemoryRegionSection *section,
775                               bool match_data, uint64_t data, EventNotifier *e)
776 {
777 }
778 
779 static void vhost_eventfd_del(MemoryListener *listener,
780                               MemoryRegionSection *section,
781                               bool match_data, uint64_t data, EventNotifier *e)
782 {
783 }
784 
785 static int vhost_virtqueue_init(struct vhost_dev *dev,
786                                 struct vhost_virtqueue *vq, int n)
787 {
788     struct vhost_vring_file file = {
789         .index = n,
790     };
791     int r = event_notifier_init(&vq->masked_notifier, 0);
792     if (r < 0) {
793         return r;
794     }
795 
796     file.fd = event_notifier_get_fd(&vq->masked_notifier);
797     r = dev->vhost_ops->vhost_call(dev, VHOST_SET_VRING_CALL, &file);
798     if (r) {
799         r = -errno;
800         goto fail_call;
801     }
802     return 0;
803 fail_call:
804     event_notifier_cleanup(&vq->masked_notifier);
805     return r;
806 }
807 
808 static void vhost_virtqueue_cleanup(struct vhost_virtqueue *vq)
809 {
810     event_notifier_cleanup(&vq->masked_notifier);
811 }
812 
813 int vhost_dev_init(struct vhost_dev *hdev, void *opaque,
814                    VhostBackendType backend_type, bool force)
815 {
816     uint64_t features;
817     int i, r;
818 
819     if (vhost_set_backend_type(hdev, backend_type) < 0) {
820         return -1;
821     }
822 
823     if (hdev->vhost_ops->vhost_backend_init(hdev, opaque) < 0) {
824         return -errno;
825     }
826 
827     r = hdev->vhost_ops->vhost_call(hdev, VHOST_SET_OWNER, NULL);
828     if (r < 0) {
829         goto fail;
830     }
831 
832     r = hdev->vhost_ops->vhost_call(hdev, VHOST_GET_FEATURES, &features);
833     if (r < 0) {
834         goto fail;
835     }
836 
837     for (i = 0; i < hdev->nvqs; ++i) {
838         r = vhost_virtqueue_init(hdev, hdev->vqs + i, i);
839         if (r < 0) {
840             goto fail_vq;
841         }
842     }
843     hdev->features = features;
844 
845     hdev->memory_listener = (MemoryListener) {
846         .begin = vhost_begin,
847         .commit = vhost_commit,
848         .region_add = vhost_region_add,
849         .region_del = vhost_region_del,
850         .region_nop = vhost_region_nop,
851         .log_start = vhost_log_start,
852         .log_stop = vhost_log_stop,
853         .log_sync = vhost_log_sync,
854         .log_global_start = vhost_log_global_start,
855         .log_global_stop = vhost_log_global_stop,
856         .eventfd_add = vhost_eventfd_add,
857         .eventfd_del = vhost_eventfd_del,
858         .priority = 10
859     };
860     hdev->migration_blocker = NULL;
861     if (!(hdev->features & (0x1 << VHOST_F_LOG_ALL))) {
862         error_setg(&hdev->migration_blocker,
863                    "Migration disabled: vhost lacks VHOST_F_LOG_ALL feature.");
864         migrate_add_blocker(hdev->migration_blocker);
865     }
866     hdev->mem = g_malloc0(offsetof(struct vhost_memory, regions));
867     hdev->n_mem_sections = 0;
868     hdev->mem_sections = NULL;
869     hdev->log = NULL;
870     hdev->log_size = 0;
871     hdev->log_enabled = false;
872     hdev->started = false;
873     hdev->memory_changed = false;
874     memory_listener_register(&hdev->memory_listener, &address_space_memory);
875     hdev->force = force;
876     return 0;
877 fail_vq:
878     while (--i >= 0) {
879         vhost_virtqueue_cleanup(hdev->vqs + i);
880     }
881 fail:
882     r = -errno;
883     hdev->vhost_ops->vhost_backend_cleanup(hdev);
884     return r;
885 }
886 
887 void vhost_dev_cleanup(struct vhost_dev *hdev)
888 {
889     int i;
890     for (i = 0; i < hdev->nvqs; ++i) {
891         vhost_virtqueue_cleanup(hdev->vqs + i);
892     }
893     memory_listener_unregister(&hdev->memory_listener);
894     if (hdev->migration_blocker) {
895         migrate_del_blocker(hdev->migration_blocker);
896         error_free(hdev->migration_blocker);
897     }
898     g_free(hdev->mem);
899     g_free(hdev->mem_sections);
900     hdev->vhost_ops->vhost_backend_cleanup(hdev);
901 }
902 
903 bool vhost_dev_query(struct vhost_dev *hdev, VirtIODevice *vdev)
904 {
905     BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(vdev)));
906     VirtioBusState *vbus = VIRTIO_BUS(qbus);
907     VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(vbus);
908 
909     return !k->query_guest_notifiers ||
910            k->query_guest_notifiers(qbus->parent) ||
911            hdev->force;
912 }
913 
914 /* Stop processing guest IO notifications in qemu.
915  * Start processing them in vhost in kernel.
916  */
917 int vhost_dev_enable_notifiers(struct vhost_dev *hdev, VirtIODevice *vdev)
918 {
919     BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(vdev)));
920     VirtioBusState *vbus = VIRTIO_BUS(qbus);
921     VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(vbus);
922     int i, r;
923     if (!k->set_host_notifier) {
924         fprintf(stderr, "binding does not support host notifiers\n");
925         r = -ENOSYS;
926         goto fail;
927     }
928 
929     for (i = 0; i < hdev->nvqs; ++i) {
930         r = k->set_host_notifier(qbus->parent, hdev->vq_index + i, true);
931         if (r < 0) {
932             fprintf(stderr, "vhost VQ %d notifier binding failed: %d\n", i, -r);
933             goto fail_vq;
934         }
935     }
936 
937     return 0;
938 fail_vq:
939     while (--i >= 0) {
940         r = k->set_host_notifier(qbus->parent, hdev->vq_index + i, false);
941         if (r < 0) {
942             fprintf(stderr, "vhost VQ %d notifier cleanup error: %d\n", i, -r);
943             fflush(stderr);
944         }
945         assert (r >= 0);
946     }
947 fail:
948     return r;
949 }
950 
951 /* Stop processing guest IO notifications in vhost.
952  * Start processing them in qemu.
953  * This might actually run the qemu handlers right away,
954  * so virtio in qemu must be completely setup when this is called.
955  */
956 void vhost_dev_disable_notifiers(struct vhost_dev *hdev, VirtIODevice *vdev)
957 {
958     BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(vdev)));
959     VirtioBusState *vbus = VIRTIO_BUS(qbus);
960     VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(vbus);
961     int i, r;
962 
963     for (i = 0; i < hdev->nvqs; ++i) {
964         r = k->set_host_notifier(qbus->parent, hdev->vq_index + i, false);
965         if (r < 0) {
966             fprintf(stderr, "vhost VQ %d notifier cleanup failed: %d\n", i, -r);
967             fflush(stderr);
968         }
969         assert (r >= 0);
970     }
971 }
972 
973 /* Test and clear event pending status.
974  * Should be called after unmask to avoid losing events.
975  */
976 bool vhost_virtqueue_pending(struct vhost_dev *hdev, int n)
977 {
978     struct vhost_virtqueue *vq = hdev->vqs + n - hdev->vq_index;
979     assert(n >= hdev->vq_index && n < hdev->vq_index + hdev->nvqs);
980     return event_notifier_test_and_clear(&vq->masked_notifier);
981 }
982 
983 /* Mask/unmask events from this vq. */
984 void vhost_virtqueue_mask(struct vhost_dev *hdev, VirtIODevice *vdev, int n,
985                          bool mask)
986 {
987     struct VirtQueue *vvq = virtio_get_queue(vdev, n);
988     int r, index = n - hdev->vq_index;
989 
990     assert(n >= hdev->vq_index && n < hdev->vq_index + hdev->nvqs);
991 
992     struct vhost_vring_file file = {
993         .index = index
994     };
995     if (mask) {
996         file.fd = event_notifier_get_fd(&hdev->vqs[index].masked_notifier);
997     } else {
998         file.fd = event_notifier_get_fd(virtio_queue_get_guest_notifier(vvq));
999     }
1000     r = hdev->vhost_ops->vhost_call(hdev, VHOST_SET_VRING_CALL, &file);
1001     assert(r >= 0);
1002 }
1003 
1004 unsigned vhost_get_features(struct vhost_dev *hdev, const int *feature_bits,
1005         unsigned features)
1006 {
1007     const int *bit = feature_bits;
1008     while (*bit != VHOST_INVALID_FEATURE_BIT) {
1009         unsigned bit_mask = (1 << *bit);
1010         if (!(hdev->features & bit_mask)) {
1011             features &= ~bit_mask;
1012         }
1013         bit++;
1014     }
1015     return features;
1016 }
1017 
1018 void vhost_ack_features(struct vhost_dev *hdev, const int *feature_bits,
1019         unsigned features)
1020 {
1021     const int *bit = feature_bits;
1022     while (*bit != VHOST_INVALID_FEATURE_BIT) {
1023         unsigned bit_mask = (1 << *bit);
1024         if (features & bit_mask) {
1025             hdev->acked_features |= bit_mask;
1026         }
1027         bit++;
1028     }
1029 }
1030 
1031 /* Host notifiers must be enabled at this point. */
1032 int vhost_dev_start(struct vhost_dev *hdev, VirtIODevice *vdev)
1033 {
1034     int i, r;
1035 
1036     hdev->started = true;
1037 
1038     r = vhost_dev_set_features(hdev, hdev->log_enabled);
1039     if (r < 0) {
1040         goto fail_features;
1041     }
1042     r = hdev->vhost_ops->vhost_call(hdev, VHOST_SET_MEM_TABLE, hdev->mem);
1043     if (r < 0) {
1044         r = -errno;
1045         goto fail_mem;
1046     }
1047     for (i = 0; i < hdev->nvqs; ++i) {
1048         r = vhost_virtqueue_start(hdev,
1049                                   vdev,
1050                                   hdev->vqs + i,
1051                                   hdev->vq_index + i);
1052         if (r < 0) {
1053             goto fail_vq;
1054         }
1055     }
1056 
1057     if (hdev->log_enabled) {
1058         hdev->log_size = vhost_get_log_size(hdev);
1059         hdev->log = hdev->log_size ?
1060             g_malloc0(hdev->log_size * sizeof *hdev->log) : NULL;
1061         r = hdev->vhost_ops->vhost_call(hdev, VHOST_SET_LOG_BASE, hdev->log);
1062         if (r < 0) {
1063             r = -errno;
1064             goto fail_log;
1065         }
1066     }
1067 
1068     return 0;
1069 fail_log:
1070 fail_vq:
1071     while (--i >= 0) {
1072         vhost_virtqueue_stop(hdev,
1073                              vdev,
1074                              hdev->vqs + i,
1075                              hdev->vq_index + i);
1076     }
1077     i = hdev->nvqs;
1078 fail_mem:
1079 fail_features:
1080 
1081     hdev->started = false;
1082     return r;
1083 }
1084 
1085 /* Host notifiers must be enabled at this point. */
1086 void vhost_dev_stop(struct vhost_dev *hdev, VirtIODevice *vdev)
1087 {
1088     int i;
1089 
1090     for (i = 0; i < hdev->nvqs; ++i) {
1091         vhost_virtqueue_stop(hdev,
1092                              vdev,
1093                              hdev->vqs + i,
1094                              hdev->vq_index + i);
1095     }
1096     vhost_log_sync_range(hdev, 0, ~0x0ull);
1097 
1098     hdev->started = false;
1099     g_free(hdev->log);
1100     hdev->log = NULL;
1101     hdev->log_size = 0;
1102 }
1103 
1104