Statistics
| Branch: | Revision:

root / hw / vhost.c @ 7830cf78

History | View | Annotate | Download (31.5 kB)

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