Statistics
| Branch: | Revision:

root / hw / vhost.c @ 57c83dac

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