Statistics
| Branch: | Revision:

root / hw / vhost.c @ 2cae4119

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