Statistics
| Branch: | Revision:

root / hw / vhost.c @ af5374aa

History | View | Annotate | Download (24.5 kB)

1 d5970055 Michael S. Tsirkin
/*
2 d5970055 Michael S. Tsirkin
 * vhost support
3 d5970055 Michael S. Tsirkin
 *
4 d5970055 Michael S. Tsirkin
 * Copyright Red Hat, Inc. 2010
5 d5970055 Michael S. Tsirkin
 *
6 d5970055 Michael S. Tsirkin
 * Authors:
7 d5970055 Michael S. Tsirkin
 *  Michael S. Tsirkin <mst@redhat.com>
8 d5970055 Michael S. Tsirkin
 *
9 d5970055 Michael S. Tsirkin
 * This work is licensed under the terms of the GNU GPL, version 2.  See
10 d5970055 Michael S. Tsirkin
 * the COPYING file in the top-level directory.
11 d5970055 Michael S. Tsirkin
 */
12 d5970055 Michael S. Tsirkin
13 d5970055 Michael S. Tsirkin
#include <sys/ioctl.h>
14 d5970055 Michael S. Tsirkin
#include "vhost.h"
15 d5970055 Michael S. Tsirkin
#include "hw/hw.h"
16 bf1b0071 Blue Swirl
#include "range.h"
17 11078ae3 Marcelo Tosatti
#include <linux/vhost.h>
18 d5970055 Michael S. Tsirkin
19 d5970055 Michael S. Tsirkin
static void vhost_dev_sync_region(struct vhost_dev *dev,
20 d5970055 Michael S. Tsirkin
                                  uint64_t mfirst, uint64_t mlast,
21 d5970055 Michael S. Tsirkin
                                  uint64_t rfirst, uint64_t rlast)
22 d5970055 Michael S. Tsirkin
{
23 d5970055 Michael S. Tsirkin
    uint64_t start = MAX(mfirst, rfirst);
24 d5970055 Michael S. Tsirkin
    uint64_t end = MIN(mlast, rlast);
25 d5970055 Michael S. Tsirkin
    vhost_log_chunk_t *from = dev->log + start / VHOST_LOG_CHUNK;
26 d5970055 Michael S. Tsirkin
    vhost_log_chunk_t *to = dev->log + end / VHOST_LOG_CHUNK + 1;
27 d5970055 Michael S. Tsirkin
    uint64_t addr = (start / VHOST_LOG_CHUNK) * VHOST_LOG_CHUNK;
28 d5970055 Michael S. Tsirkin
29 d5970055 Michael S. Tsirkin
    assert(end / VHOST_LOG_CHUNK < dev->log_size);
30 d5970055 Michael S. Tsirkin
    assert(start / VHOST_LOG_CHUNK < dev->log_size);
31 d5970055 Michael S. Tsirkin
    if (end < start) {
32 d5970055 Michael S. Tsirkin
        return;
33 d5970055 Michael S. Tsirkin
    }
34 d5970055 Michael S. Tsirkin
    for (;from < to; ++from) {
35 d5970055 Michael S. Tsirkin
        vhost_log_chunk_t log;
36 d5970055 Michael S. Tsirkin
        int bit;
37 d5970055 Michael S. Tsirkin
        /* We first check with non-atomic: much cheaper,
38 d5970055 Michael S. Tsirkin
         * and we expect non-dirty to be the common case. */
39 d5970055 Michael S. Tsirkin
        if (!*from) {
40 0c600ce2 Jason Wang
            addr += VHOST_LOG_CHUNK;
41 d5970055 Michael S. Tsirkin
            continue;
42 d5970055 Michael S. Tsirkin
        }
43 d5970055 Michael S. Tsirkin
        /* Data must be read atomically. We don't really
44 d5970055 Michael S. Tsirkin
         * need the barrier semantics of __sync
45 d5970055 Michael S. Tsirkin
         * builtins, but it's easier to use them than
46 d5970055 Michael S. Tsirkin
         * roll our own. */
47 d5970055 Michael S. Tsirkin
        log = __sync_fetch_and_and(from, 0);
48 d5970055 Michael S. Tsirkin
        while ((bit = sizeof(log) > sizeof(int) ?
49 d5970055 Michael S. Tsirkin
                ffsll(log) : ffs(log))) {
50 7b67b18a Michael S. Tsirkin
            ram_addr_t ram_addr;
51 d5970055 Michael S. Tsirkin
            bit -= 1;
52 7b67b18a Michael S. Tsirkin
            ram_addr = cpu_get_physical_page_desc(addr + bit * VHOST_LOG_PAGE);
53 7b67b18a Michael S. Tsirkin
            cpu_physical_memory_set_dirty(ram_addr);
54 d5970055 Michael S. Tsirkin
            log &= ~(0x1ull << bit);
55 d5970055 Michael S. Tsirkin
        }
56 d5970055 Michael S. Tsirkin
        addr += VHOST_LOG_CHUNK;
57 d5970055 Michael S. Tsirkin
    }
58 d5970055 Michael S. Tsirkin
}
59 d5970055 Michael S. Tsirkin
60 d5970055 Michael S. Tsirkin
static int vhost_client_sync_dirty_bitmap(CPUPhysMemoryClient *client,
61 d5970055 Michael S. Tsirkin
                                          target_phys_addr_t start_addr,
62 d5970055 Michael S. Tsirkin
                                          target_phys_addr_t end_addr)
63 d5970055 Michael S. Tsirkin
{
64 d5970055 Michael S. Tsirkin
    struct vhost_dev *dev = container_of(client, struct vhost_dev, client);
65 d5970055 Michael S. Tsirkin
    int i;
66 d5970055 Michael S. Tsirkin
    if (!dev->log_enabled || !dev->started) {
67 d5970055 Michael S. Tsirkin
        return 0;
68 d5970055 Michael S. Tsirkin
    }
69 d5970055 Michael S. Tsirkin
    for (i = 0; i < dev->mem->nregions; ++i) {
70 d5970055 Michael S. Tsirkin
        struct vhost_memory_region *reg = dev->mem->regions + i;
71 d5970055 Michael S. Tsirkin
        vhost_dev_sync_region(dev, start_addr, end_addr,
72 d5970055 Michael S. Tsirkin
                              reg->guest_phys_addr,
73 d5970055 Michael S. Tsirkin
                              range_get_last(reg->guest_phys_addr,
74 d5970055 Michael S. Tsirkin
                                             reg->memory_size));
75 d5970055 Michael S. Tsirkin
    }
76 d5970055 Michael S. Tsirkin
    for (i = 0; i < dev->nvqs; ++i) {
77 d5970055 Michael S. Tsirkin
        struct vhost_virtqueue *vq = dev->vqs + i;
78 d5970055 Michael S. Tsirkin
        vhost_dev_sync_region(dev, start_addr, end_addr, vq->used_phys,
79 d5970055 Michael S. Tsirkin
                              range_get_last(vq->used_phys, vq->used_size));
80 d5970055 Michael S. Tsirkin
    }
81 d5970055 Michael S. Tsirkin
    return 0;
82 d5970055 Michael S. Tsirkin
}
83 d5970055 Michael S. Tsirkin
84 d5970055 Michael S. Tsirkin
/* Assign/unassign. Keep an unsorted array of non-overlapping
85 d5970055 Michael S. Tsirkin
 * memory regions in dev->mem. */
86 d5970055 Michael S. Tsirkin
static void vhost_dev_unassign_memory(struct vhost_dev *dev,
87 d5970055 Michael S. Tsirkin
                                      uint64_t start_addr,
88 d5970055 Michael S. Tsirkin
                                      uint64_t size)
89 d5970055 Michael S. Tsirkin
{
90 d5970055 Michael S. Tsirkin
    int from, to, n = dev->mem->nregions;
91 d5970055 Michael S. Tsirkin
    /* Track overlapping/split regions for sanity checking. */
92 d5970055 Michael S. Tsirkin
    int overlap_start = 0, overlap_end = 0, overlap_middle = 0, split = 0;
93 d5970055 Michael S. Tsirkin
94 d5970055 Michael S. Tsirkin
    for (from = 0, to = 0; from < n; ++from, ++to) {
95 d5970055 Michael S. Tsirkin
        struct vhost_memory_region *reg = dev->mem->regions + to;
96 d5970055 Michael S. Tsirkin
        uint64_t reglast;
97 d5970055 Michael S. Tsirkin
        uint64_t memlast;
98 d5970055 Michael S. Tsirkin
        uint64_t change;
99 d5970055 Michael S. Tsirkin
100 d5970055 Michael S. Tsirkin
        /* clone old region */
101 d5970055 Michael S. Tsirkin
        if (to != from) {
102 d5970055 Michael S. Tsirkin
            memcpy(reg, dev->mem->regions + from, sizeof *reg);
103 d5970055 Michael S. Tsirkin
        }
104 d5970055 Michael S. Tsirkin
105 d5970055 Michael S. Tsirkin
        /* No overlap is simple */
106 d5970055 Michael S. Tsirkin
        if (!ranges_overlap(reg->guest_phys_addr, reg->memory_size,
107 d5970055 Michael S. Tsirkin
                            start_addr, size)) {
108 d5970055 Michael S. Tsirkin
            continue;
109 d5970055 Michael S. Tsirkin
        }
110 d5970055 Michael S. Tsirkin
111 d5970055 Michael S. Tsirkin
        /* Split only happens if supplied region
112 d5970055 Michael S. Tsirkin
         * is in the middle of an existing one. Thus it can not
113 d5970055 Michael S. Tsirkin
         * overlap with any other existing region. */
114 d5970055 Michael S. Tsirkin
        assert(!split);
115 d5970055 Michael S. Tsirkin
116 d5970055 Michael S. Tsirkin
        reglast = range_get_last(reg->guest_phys_addr, reg->memory_size);
117 d5970055 Michael S. Tsirkin
        memlast = range_get_last(start_addr, size);
118 d5970055 Michael S. Tsirkin
119 d5970055 Michael S. Tsirkin
        /* Remove whole region */
120 d5970055 Michael S. Tsirkin
        if (start_addr <= reg->guest_phys_addr && memlast >= reglast) {
121 d5970055 Michael S. Tsirkin
            --dev->mem->nregions;
122 d5970055 Michael S. Tsirkin
            --to;
123 d5970055 Michael S. Tsirkin
            assert(to >= 0);
124 d5970055 Michael S. Tsirkin
            ++overlap_middle;
125 d5970055 Michael S. Tsirkin
            continue;
126 d5970055 Michael S. Tsirkin
        }
127 d5970055 Michael S. Tsirkin
128 d5970055 Michael S. Tsirkin
        /* Shrink region */
129 d5970055 Michael S. Tsirkin
        if (memlast >= reglast) {
130 d5970055 Michael S. Tsirkin
            reg->memory_size = start_addr - reg->guest_phys_addr;
131 d5970055 Michael S. Tsirkin
            assert(reg->memory_size);
132 d5970055 Michael S. Tsirkin
            assert(!overlap_end);
133 d5970055 Michael S. Tsirkin
            ++overlap_end;
134 d5970055 Michael S. Tsirkin
            continue;
135 d5970055 Michael S. Tsirkin
        }
136 d5970055 Michael S. Tsirkin
137 d5970055 Michael S. Tsirkin
        /* Shift region */
138 d5970055 Michael S. Tsirkin
        if (start_addr <= reg->guest_phys_addr) {
139 d5970055 Michael S. Tsirkin
            change = memlast + 1 - reg->guest_phys_addr;
140 d5970055 Michael S. Tsirkin
            reg->memory_size -= change;
141 d5970055 Michael S. Tsirkin
            reg->guest_phys_addr += change;
142 d5970055 Michael S. Tsirkin
            reg->userspace_addr += change;
143 d5970055 Michael S. Tsirkin
            assert(reg->memory_size);
144 d5970055 Michael S. Tsirkin
            assert(!overlap_start);
145 d5970055 Michael S. Tsirkin
            ++overlap_start;
146 d5970055 Michael S. Tsirkin
            continue;
147 d5970055 Michael S. Tsirkin
        }
148 d5970055 Michael S. Tsirkin
149 d5970055 Michael S. Tsirkin
        /* This only happens if supplied region
150 d5970055 Michael S. Tsirkin
         * is in the middle of an existing one. Thus it can not
151 d5970055 Michael S. Tsirkin
         * overlap with any other existing region. */
152 d5970055 Michael S. Tsirkin
        assert(!overlap_start);
153 d5970055 Michael S. Tsirkin
        assert(!overlap_end);
154 d5970055 Michael S. Tsirkin
        assert(!overlap_middle);
155 d5970055 Michael S. Tsirkin
        /* Split region: shrink first part, shift second part. */
156 d5970055 Michael S. Tsirkin
        memcpy(dev->mem->regions + n, reg, sizeof *reg);
157 d5970055 Michael S. Tsirkin
        reg->memory_size = start_addr - reg->guest_phys_addr;
158 d5970055 Michael S. Tsirkin
        assert(reg->memory_size);
159 d5970055 Michael S. Tsirkin
        change = memlast + 1 - reg->guest_phys_addr;
160 d5970055 Michael S. Tsirkin
        reg = dev->mem->regions + n;
161 d5970055 Michael S. Tsirkin
        reg->memory_size -= change;
162 d5970055 Michael S. Tsirkin
        assert(reg->memory_size);
163 d5970055 Michael S. Tsirkin
        reg->guest_phys_addr += change;
164 d5970055 Michael S. Tsirkin
        reg->userspace_addr += change;
165 d5970055 Michael S. Tsirkin
        /* Never add more than 1 region */
166 d5970055 Michael S. Tsirkin
        assert(dev->mem->nregions == n);
167 d5970055 Michael S. Tsirkin
        ++dev->mem->nregions;
168 d5970055 Michael S. Tsirkin
        ++split;
169 d5970055 Michael S. Tsirkin
    }
170 d5970055 Michael S. Tsirkin
}
171 d5970055 Michael S. Tsirkin
172 d5970055 Michael S. Tsirkin
/* Called after unassign, so no regions overlap the given range. */
173 d5970055 Michael S. Tsirkin
static void vhost_dev_assign_memory(struct vhost_dev *dev,
174 d5970055 Michael S. Tsirkin
                                    uint64_t start_addr,
175 d5970055 Michael S. Tsirkin
                                    uint64_t size,
176 d5970055 Michael S. Tsirkin
                                    uint64_t uaddr)
177 d5970055 Michael S. Tsirkin
{
178 d5970055 Michael S. Tsirkin
    int from, to;
179 d5970055 Michael S. Tsirkin
    struct vhost_memory_region *merged = NULL;
180 d5970055 Michael S. Tsirkin
    for (from = 0, to = 0; from < dev->mem->nregions; ++from, ++to) {
181 d5970055 Michael S. Tsirkin
        struct vhost_memory_region *reg = dev->mem->regions + to;
182 d5970055 Michael S. Tsirkin
        uint64_t prlast, urlast;
183 d5970055 Michael S. Tsirkin
        uint64_t pmlast, umlast;
184 d5970055 Michael S. Tsirkin
        uint64_t s, e, u;
185 d5970055 Michael S. Tsirkin
186 d5970055 Michael S. Tsirkin
        /* clone old region */
187 d5970055 Michael S. Tsirkin
        if (to != from) {
188 d5970055 Michael S. Tsirkin
            memcpy(reg, dev->mem->regions + from, sizeof *reg);
189 d5970055 Michael S. Tsirkin
        }
190 d5970055 Michael S. Tsirkin
        prlast = range_get_last(reg->guest_phys_addr, reg->memory_size);
191 d5970055 Michael S. Tsirkin
        pmlast = range_get_last(start_addr, size);
192 d5970055 Michael S. Tsirkin
        urlast = range_get_last(reg->userspace_addr, reg->memory_size);
193 d5970055 Michael S. Tsirkin
        umlast = range_get_last(uaddr, size);
194 d5970055 Michael S. Tsirkin
195 d5970055 Michael S. Tsirkin
        /* check for overlapping regions: should never happen. */
196 d5970055 Michael S. Tsirkin
        assert(prlast < start_addr || pmlast < reg->guest_phys_addr);
197 d5970055 Michael S. Tsirkin
        /* Not an adjacent or overlapping region - do not merge. */
198 d5970055 Michael S. Tsirkin
        if ((prlast + 1 != start_addr || urlast + 1 != uaddr) &&
199 d5970055 Michael S. Tsirkin
            (pmlast + 1 != reg->guest_phys_addr ||
200 d5970055 Michael S. Tsirkin
             umlast + 1 != reg->userspace_addr)) {
201 d5970055 Michael S. Tsirkin
            continue;
202 d5970055 Michael S. Tsirkin
        }
203 d5970055 Michael S. Tsirkin
204 d5970055 Michael S. Tsirkin
        if (merged) {
205 d5970055 Michael S. Tsirkin
            --to;
206 d5970055 Michael S. Tsirkin
            assert(to >= 0);
207 d5970055 Michael S. Tsirkin
        } else {
208 d5970055 Michael S. Tsirkin
            merged = reg;
209 d5970055 Michael S. Tsirkin
        }
210 d5970055 Michael S. Tsirkin
        u = MIN(uaddr, reg->userspace_addr);
211 d5970055 Michael S. Tsirkin
        s = MIN(start_addr, reg->guest_phys_addr);
212 d5970055 Michael S. Tsirkin
        e = MAX(pmlast, prlast);
213 d5970055 Michael S. Tsirkin
        uaddr = merged->userspace_addr = u;
214 d5970055 Michael S. Tsirkin
        start_addr = merged->guest_phys_addr = s;
215 d5970055 Michael S. Tsirkin
        size = merged->memory_size = e - s + 1;
216 d5970055 Michael S. Tsirkin
        assert(merged->memory_size);
217 d5970055 Michael S. Tsirkin
    }
218 d5970055 Michael S. Tsirkin
219 d5970055 Michael S. Tsirkin
    if (!merged) {
220 d5970055 Michael S. Tsirkin
        struct vhost_memory_region *reg = dev->mem->regions + to;
221 d5970055 Michael S. Tsirkin
        memset(reg, 0, sizeof *reg);
222 d5970055 Michael S. Tsirkin
        reg->memory_size = size;
223 d5970055 Michael S. Tsirkin
        assert(reg->memory_size);
224 d5970055 Michael S. Tsirkin
        reg->guest_phys_addr = start_addr;
225 d5970055 Michael S. Tsirkin
        reg->userspace_addr = uaddr;
226 d5970055 Michael S. Tsirkin
        ++to;
227 d5970055 Michael S. Tsirkin
    }
228 d5970055 Michael S. Tsirkin
    assert(to <= dev->mem->nregions + 1);
229 d5970055 Michael S. Tsirkin
    dev->mem->nregions = to;
230 d5970055 Michael S. Tsirkin
}
231 d5970055 Michael S. Tsirkin
232 d5970055 Michael S. Tsirkin
static uint64_t vhost_get_log_size(struct vhost_dev *dev)
233 d5970055 Michael S. Tsirkin
{
234 d5970055 Michael S. Tsirkin
    uint64_t log_size = 0;
235 d5970055 Michael S. Tsirkin
    int i;
236 d5970055 Michael S. Tsirkin
    for (i = 0; i < dev->mem->nregions; ++i) {
237 d5970055 Michael S. Tsirkin
        struct vhost_memory_region *reg = dev->mem->regions + i;
238 d5970055 Michael S. Tsirkin
        uint64_t last = range_get_last(reg->guest_phys_addr,
239 d5970055 Michael S. Tsirkin
                                       reg->memory_size);
240 d5970055 Michael S. Tsirkin
        log_size = MAX(log_size, last / VHOST_LOG_CHUNK + 1);
241 d5970055 Michael S. Tsirkin
    }
242 d5970055 Michael S. Tsirkin
    for (i = 0; i < dev->nvqs; ++i) {
243 d5970055 Michael S. Tsirkin
        struct vhost_virtqueue *vq = dev->vqs + i;
244 d5970055 Michael S. Tsirkin
        uint64_t last = vq->used_phys + vq->used_size - 1;
245 d5970055 Michael S. Tsirkin
        log_size = MAX(log_size, last / VHOST_LOG_CHUNK + 1);
246 d5970055 Michael S. Tsirkin
    }
247 d5970055 Michael S. Tsirkin
    return log_size;
248 d5970055 Michael S. Tsirkin
}
249 d5970055 Michael S. Tsirkin
250 d5970055 Michael S. Tsirkin
static inline void vhost_dev_log_resize(struct vhost_dev* dev, uint64_t size)
251 d5970055 Michael S. Tsirkin
{
252 d5970055 Michael S. Tsirkin
    vhost_log_chunk_t *log;
253 d5970055 Michael S. Tsirkin
    uint64_t log_base;
254 d5970055 Michael S. Tsirkin
    int r;
255 d5970055 Michael S. Tsirkin
    if (size) {
256 d5970055 Michael S. Tsirkin
        log = qemu_mallocz(size * sizeof *log);
257 d5970055 Michael S. Tsirkin
    } else {
258 d5970055 Michael S. Tsirkin
        log = NULL;
259 d5970055 Michael S. Tsirkin
    }
260 d5970055 Michael S. Tsirkin
    log_base = (uint64_t)(unsigned long)log;
261 d5970055 Michael S. Tsirkin
    r = ioctl(dev->control, VHOST_SET_LOG_BASE, &log_base);
262 d5970055 Michael S. Tsirkin
    assert(r >= 0);
263 d5970055 Michael S. Tsirkin
    vhost_client_sync_dirty_bitmap(&dev->client, 0,
264 d5970055 Michael S. Tsirkin
                                   (target_phys_addr_t)~0x0ull);
265 d5970055 Michael S. Tsirkin
    if (dev->log) {
266 d5970055 Michael S. Tsirkin
        qemu_free(dev->log);
267 d5970055 Michael S. Tsirkin
    }
268 d5970055 Michael S. Tsirkin
    dev->log = log;
269 d5970055 Michael S. Tsirkin
    dev->log_size = size;
270 d5970055 Michael S. Tsirkin
}
271 d5970055 Michael S. Tsirkin
272 d5970055 Michael S. Tsirkin
static int vhost_verify_ring_mappings(struct vhost_dev *dev,
273 d5970055 Michael S. Tsirkin
                                      uint64_t start_addr,
274 d5970055 Michael S. Tsirkin
                                      uint64_t size)
275 d5970055 Michael S. Tsirkin
{
276 d5970055 Michael S. Tsirkin
    int i;
277 d5970055 Michael S. Tsirkin
    for (i = 0; i < dev->nvqs; ++i) {
278 d5970055 Michael S. Tsirkin
        struct vhost_virtqueue *vq = dev->vqs + i;
279 d5970055 Michael S. Tsirkin
        target_phys_addr_t l;
280 d5970055 Michael S. Tsirkin
        void *p;
281 d5970055 Michael S. Tsirkin
282 d5970055 Michael S. Tsirkin
        if (!ranges_overlap(start_addr, size, vq->ring_phys, vq->ring_size)) {
283 d5970055 Michael S. Tsirkin
            continue;
284 d5970055 Michael S. Tsirkin
        }
285 d5970055 Michael S. Tsirkin
        l = vq->ring_size;
286 d5970055 Michael S. Tsirkin
        p = cpu_physical_memory_map(vq->ring_phys, &l, 1);
287 d5970055 Michael S. Tsirkin
        if (!p || l != vq->ring_size) {
288 d5970055 Michael S. Tsirkin
            fprintf(stderr, "Unable to map ring buffer for ring %d\n", i);
289 d5970055 Michael S. Tsirkin
            return -ENOMEM;
290 d5970055 Michael S. Tsirkin
        }
291 d5970055 Michael S. Tsirkin
        if (p != vq->ring) {
292 d5970055 Michael S. Tsirkin
            fprintf(stderr, "Ring buffer relocated for ring %d\n", i);
293 d5970055 Michael S. Tsirkin
            return -EBUSY;
294 d5970055 Michael S. Tsirkin
        }
295 d5970055 Michael S. Tsirkin
        cpu_physical_memory_unmap(p, l, 0, 0);
296 d5970055 Michael S. Tsirkin
    }
297 d5970055 Michael S. Tsirkin
    return 0;
298 d5970055 Michael S. Tsirkin
}
299 d5970055 Michael S. Tsirkin
300 4e789564 Michael S. Tsirkin
static struct vhost_memory_region *vhost_dev_find_reg(struct vhost_dev *dev,
301 4e789564 Michael S. Tsirkin
                                                      uint64_t start_addr,
302 4e789564 Michael S. Tsirkin
                                                      uint64_t size)
303 4e789564 Michael S. Tsirkin
{
304 4e789564 Michael S. Tsirkin
    int i, n = dev->mem->nregions;
305 4e789564 Michael S. Tsirkin
    for (i = 0; i < n; ++i) {
306 4e789564 Michael S. Tsirkin
        struct vhost_memory_region *reg = dev->mem->regions + i;
307 4e789564 Michael S. Tsirkin
        if (ranges_overlap(reg->guest_phys_addr, reg->memory_size,
308 4e789564 Michael S. Tsirkin
                           start_addr, size)) {
309 4e789564 Michael S. Tsirkin
            return reg;
310 4e789564 Michael S. Tsirkin
        }
311 4e789564 Michael S. Tsirkin
    }
312 4e789564 Michael S. Tsirkin
    return NULL;
313 4e789564 Michael S. Tsirkin
}
314 4e789564 Michael S. Tsirkin
315 4e789564 Michael S. Tsirkin
static bool vhost_dev_cmp_memory(struct vhost_dev *dev,
316 4e789564 Michael S. Tsirkin
                                 uint64_t start_addr,
317 4e789564 Michael S. Tsirkin
                                 uint64_t size,
318 4e789564 Michael S. Tsirkin
                                 uint64_t uaddr)
319 4e789564 Michael S. Tsirkin
{
320 4e789564 Michael S. Tsirkin
    struct vhost_memory_region *reg = vhost_dev_find_reg(dev, start_addr, size);
321 4e789564 Michael S. Tsirkin
    uint64_t reglast;
322 4e789564 Michael S. Tsirkin
    uint64_t memlast;
323 4e789564 Michael S. Tsirkin
324 4e789564 Michael S. Tsirkin
    if (!reg) {
325 4e789564 Michael S. Tsirkin
        return true;
326 4e789564 Michael S. Tsirkin
    }
327 4e789564 Michael S. Tsirkin
328 4e789564 Michael S. Tsirkin
    reglast = range_get_last(reg->guest_phys_addr, reg->memory_size);
329 4e789564 Michael S. Tsirkin
    memlast = range_get_last(start_addr, size);
330 4e789564 Michael S. Tsirkin
331 4e789564 Michael S. Tsirkin
    /* Need to extend region? */
332 4e789564 Michael S. Tsirkin
    if (start_addr < reg->guest_phys_addr || memlast > reglast) {
333 4e789564 Michael S. Tsirkin
        return true;
334 4e789564 Michael S. Tsirkin
    }
335 4e789564 Michael S. Tsirkin
    /* userspace_addr changed? */
336 4e789564 Michael S. Tsirkin
    return uaddr != reg->userspace_addr + start_addr - reg->guest_phys_addr;
337 4e789564 Michael S. Tsirkin
}
338 4e789564 Michael S. Tsirkin
339 d5970055 Michael S. Tsirkin
static void vhost_client_set_memory(CPUPhysMemoryClient *client,
340 d5970055 Michael S. Tsirkin
                                    target_phys_addr_t start_addr,
341 d5970055 Michael S. Tsirkin
                                    ram_addr_t size,
342 0fd542fb Michael S. Tsirkin
                                    ram_addr_t phys_offset,
343 0fd542fb Michael S. Tsirkin
                                    bool log_dirty)
344 d5970055 Michael S. Tsirkin
{
345 d5970055 Michael S. Tsirkin
    struct vhost_dev *dev = container_of(client, struct vhost_dev, client);
346 d5970055 Michael S. Tsirkin
    ram_addr_t flags = phys_offset & ~TARGET_PAGE_MASK;
347 d5970055 Michael S. Tsirkin
    int s = offsetof(struct vhost_memory, regions) +
348 d5970055 Michael S. Tsirkin
        (dev->mem->nregions + 1) * sizeof dev->mem->regions[0];
349 d5970055 Michael S. Tsirkin
    uint64_t log_size;
350 d5970055 Michael S. Tsirkin
    int r;
351 4e789564 Michael S. Tsirkin
352 d5970055 Michael S. Tsirkin
    dev->mem = qemu_realloc(dev->mem, s);
353 d5970055 Michael S. Tsirkin
354 f5a4e64f Michael S. Tsirkin
    if (log_dirty) {
355 f5a4e64f Michael S. Tsirkin
        flags = IO_MEM_UNASSIGNED;
356 f5a4e64f Michael S. Tsirkin
    }
357 f5a4e64f Michael S. Tsirkin
358 d5970055 Michael S. Tsirkin
    assert(size);
359 d5970055 Michael S. Tsirkin
360 4e789564 Michael S. Tsirkin
    /* Optimize no-change case. At least cirrus_vga does this a lot at this time. */
361 4e789564 Michael S. Tsirkin
    if (flags == IO_MEM_RAM) {
362 4e789564 Michael S. Tsirkin
        if (!vhost_dev_cmp_memory(dev, start_addr, size,
363 4e789564 Michael S. Tsirkin
                                  (uintptr_t)qemu_get_ram_ptr(phys_offset))) {
364 4e789564 Michael S. Tsirkin
            /* Region exists with same address. Nothing to do. */
365 4e789564 Michael S. Tsirkin
            return;
366 4e789564 Michael S. Tsirkin
        }
367 4e789564 Michael S. Tsirkin
    } else {
368 4e789564 Michael S. Tsirkin
        if (!vhost_dev_find_reg(dev, start_addr, size)) {
369 4e789564 Michael S. Tsirkin
            /* Removing region that we don't access. Nothing to do. */
370 4e789564 Michael S. Tsirkin
            return;
371 4e789564 Michael S. Tsirkin
        }
372 4e789564 Michael S. Tsirkin
    }
373 4e789564 Michael S. Tsirkin
374 d5970055 Michael S. Tsirkin
    vhost_dev_unassign_memory(dev, start_addr, size);
375 d5970055 Michael S. Tsirkin
    if (flags == IO_MEM_RAM) {
376 d5970055 Michael S. Tsirkin
        /* Add given mapping, merging adjacent regions if any */
377 d5970055 Michael S. Tsirkin
        vhost_dev_assign_memory(dev, start_addr, size,
378 d5970055 Michael S. Tsirkin
                                (uintptr_t)qemu_get_ram_ptr(phys_offset));
379 d5970055 Michael S. Tsirkin
    } else {
380 d5970055 Michael S. Tsirkin
        /* Remove old mapping for this memory, if any. */
381 d5970055 Michael S. Tsirkin
        vhost_dev_unassign_memory(dev, start_addr, size);
382 d5970055 Michael S. Tsirkin
    }
383 d5970055 Michael S. Tsirkin
384 d5970055 Michael S. Tsirkin
    if (!dev->started) {
385 d5970055 Michael S. Tsirkin
        return;
386 d5970055 Michael S. Tsirkin
    }
387 d5970055 Michael S. Tsirkin
388 d5970055 Michael S. Tsirkin
    if (dev->started) {
389 d5970055 Michael S. Tsirkin
        r = vhost_verify_ring_mappings(dev, start_addr, size);
390 d5970055 Michael S. Tsirkin
        assert(r >= 0);
391 d5970055 Michael S. Tsirkin
    }
392 d5970055 Michael S. Tsirkin
393 d5970055 Michael S. Tsirkin
    if (!dev->log_enabled) {
394 d5970055 Michael S. Tsirkin
        r = ioctl(dev->control, VHOST_SET_MEM_TABLE, dev->mem);
395 d5970055 Michael S. Tsirkin
        assert(r >= 0);
396 d5970055 Michael S. Tsirkin
        return;
397 d5970055 Michael S. Tsirkin
    }
398 d5970055 Michael S. Tsirkin
    log_size = vhost_get_log_size(dev);
399 d5970055 Michael S. Tsirkin
    /* We allocate an extra 4K bytes to log,
400 d5970055 Michael S. Tsirkin
     * to reduce the * number of reallocations. */
401 d5970055 Michael S. Tsirkin
#define VHOST_LOG_BUFFER (0x1000 / sizeof *dev->log)
402 d5970055 Michael S. Tsirkin
    /* To log more, must increase log size before table update. */
403 d5970055 Michael S. Tsirkin
    if (dev->log_size < log_size) {
404 d5970055 Michael S. Tsirkin
        vhost_dev_log_resize(dev, log_size + VHOST_LOG_BUFFER);
405 d5970055 Michael S. Tsirkin
    }
406 d5970055 Michael S. Tsirkin
    r = ioctl(dev->control, VHOST_SET_MEM_TABLE, dev->mem);
407 d5970055 Michael S. Tsirkin
    assert(r >= 0);
408 d5970055 Michael S. Tsirkin
    /* To log less, can only decrease log size after table update. */
409 d5970055 Michael S. Tsirkin
    if (dev->log_size > log_size + VHOST_LOG_BUFFER) {
410 d5970055 Michael S. Tsirkin
        vhost_dev_log_resize(dev, log_size);
411 d5970055 Michael S. Tsirkin
    }
412 d5970055 Michael S. Tsirkin
}
413 d5970055 Michael S. Tsirkin
414 d5970055 Michael S. Tsirkin
static int vhost_virtqueue_set_addr(struct vhost_dev *dev,
415 d5970055 Michael S. Tsirkin
                                    struct vhost_virtqueue *vq,
416 d5970055 Michael S. Tsirkin
                                    unsigned idx, bool enable_log)
417 d5970055 Michael S. Tsirkin
{
418 d5970055 Michael S. Tsirkin
    struct vhost_vring_addr addr = {
419 d5970055 Michael S. Tsirkin
        .index = idx,
420 2b3af999 Stefan Weil
        .desc_user_addr = (uint64_t)(unsigned long)vq->desc,
421 2b3af999 Stefan Weil
        .avail_user_addr = (uint64_t)(unsigned long)vq->avail,
422 2b3af999 Stefan Weil
        .used_user_addr = (uint64_t)(unsigned long)vq->used,
423 d5970055 Michael S. Tsirkin
        .log_guest_addr = vq->used_phys,
424 d5970055 Michael S. Tsirkin
        .flags = enable_log ? (1 << VHOST_VRING_F_LOG) : 0,
425 d5970055 Michael S. Tsirkin
    };
426 d5970055 Michael S. Tsirkin
    int r = ioctl(dev->control, VHOST_SET_VRING_ADDR, &addr);
427 d5970055 Michael S. Tsirkin
    if (r < 0) {
428 d5970055 Michael S. Tsirkin
        return -errno;
429 d5970055 Michael S. Tsirkin
    }
430 d5970055 Michael S. Tsirkin
    return 0;
431 d5970055 Michael S. Tsirkin
}
432 d5970055 Michael S. Tsirkin
433 d5970055 Michael S. Tsirkin
static int vhost_dev_set_features(struct vhost_dev *dev, bool enable_log)
434 d5970055 Michael S. Tsirkin
{
435 d5970055 Michael S. Tsirkin
    uint64_t features = dev->acked_features;
436 d5970055 Michael S. Tsirkin
    int r;
437 d5970055 Michael S. Tsirkin
    if (enable_log) {
438 d5970055 Michael S. Tsirkin
        features |= 0x1 << VHOST_F_LOG_ALL;
439 d5970055 Michael S. Tsirkin
    }
440 d5970055 Michael S. Tsirkin
    r = ioctl(dev->control, VHOST_SET_FEATURES, &features);
441 d5970055 Michael S. Tsirkin
    return r < 0 ? -errno : 0;
442 d5970055 Michael S. Tsirkin
}
443 d5970055 Michael S. Tsirkin
444 d5970055 Michael S. Tsirkin
static int vhost_dev_set_log(struct vhost_dev *dev, bool enable_log)
445 d5970055 Michael S. Tsirkin
{
446 d5970055 Michael S. Tsirkin
    int r, t, i;
447 d5970055 Michael S. Tsirkin
    r = vhost_dev_set_features(dev, enable_log);
448 d5970055 Michael S. Tsirkin
    if (r < 0) {
449 d5970055 Michael S. Tsirkin
        goto err_features;
450 d5970055 Michael S. Tsirkin
    }
451 d5970055 Michael S. Tsirkin
    for (i = 0; i < dev->nvqs; ++i) {
452 d5970055 Michael S. Tsirkin
        r = vhost_virtqueue_set_addr(dev, dev->vqs + i, i,
453 d5970055 Michael S. Tsirkin
                                     enable_log);
454 d5970055 Michael S. Tsirkin
        if (r < 0) {
455 d5970055 Michael S. Tsirkin
            goto err_vq;
456 d5970055 Michael S. Tsirkin
        }
457 d5970055 Michael S. Tsirkin
    }
458 d5970055 Michael S. Tsirkin
    return 0;
459 d5970055 Michael S. Tsirkin
err_vq:
460 d5970055 Michael S. Tsirkin
    for (; i >= 0; --i) {
461 d5970055 Michael S. Tsirkin
        t = vhost_virtqueue_set_addr(dev, dev->vqs + i, i,
462 d5970055 Michael S. Tsirkin
                                     dev->log_enabled);
463 d5970055 Michael S. Tsirkin
        assert(t >= 0);
464 d5970055 Michael S. Tsirkin
    }
465 d5970055 Michael S. Tsirkin
    t = vhost_dev_set_features(dev, dev->log_enabled);
466 d5970055 Michael S. Tsirkin
    assert(t >= 0);
467 d5970055 Michael S. Tsirkin
err_features:
468 d5970055 Michael S. Tsirkin
    return r;
469 d5970055 Michael S. Tsirkin
}
470 d5970055 Michael S. Tsirkin
471 d5970055 Michael S. Tsirkin
static int vhost_client_migration_log(CPUPhysMemoryClient *client,
472 d5970055 Michael S. Tsirkin
                                      int enable)
473 d5970055 Michael S. Tsirkin
{
474 d5970055 Michael S. Tsirkin
    struct vhost_dev *dev = container_of(client, struct vhost_dev, client);
475 d5970055 Michael S. Tsirkin
    int r;
476 d5970055 Michael S. Tsirkin
    if (!!enable == dev->log_enabled) {
477 d5970055 Michael S. Tsirkin
        return 0;
478 d5970055 Michael S. Tsirkin
    }
479 d5970055 Michael S. Tsirkin
    if (!dev->started) {
480 d5970055 Michael S. Tsirkin
        dev->log_enabled = enable;
481 d5970055 Michael S. Tsirkin
        return 0;
482 d5970055 Michael S. Tsirkin
    }
483 d5970055 Michael S. Tsirkin
    if (!enable) {
484 d5970055 Michael S. Tsirkin
        r = vhost_dev_set_log(dev, false);
485 d5970055 Michael S. Tsirkin
        if (r < 0) {
486 d5970055 Michael S. Tsirkin
            return r;
487 d5970055 Michael S. Tsirkin
        }
488 d5970055 Michael S. Tsirkin
        if (dev->log) {
489 d5970055 Michael S. Tsirkin
            qemu_free(dev->log);
490 d5970055 Michael S. Tsirkin
        }
491 d5970055 Michael S. Tsirkin
        dev->log = NULL;
492 d5970055 Michael S. Tsirkin
        dev->log_size = 0;
493 d5970055 Michael S. Tsirkin
    } else {
494 d5970055 Michael S. Tsirkin
        vhost_dev_log_resize(dev, vhost_get_log_size(dev));
495 d5970055 Michael S. Tsirkin
        r = vhost_dev_set_log(dev, true);
496 d5970055 Michael S. Tsirkin
        if (r < 0) {
497 d5970055 Michael S. Tsirkin
            return r;
498 d5970055 Michael S. Tsirkin
        }
499 d5970055 Michael S. Tsirkin
    }
500 d5970055 Michael S. Tsirkin
    dev->log_enabled = enable;
501 d5970055 Michael S. Tsirkin
    return 0;
502 d5970055 Michael S. Tsirkin
}
503 d5970055 Michael S. Tsirkin
504 d5970055 Michael S. Tsirkin
static int vhost_virtqueue_init(struct vhost_dev *dev,
505 d5970055 Michael S. Tsirkin
                                struct VirtIODevice *vdev,
506 d5970055 Michael S. Tsirkin
                                struct vhost_virtqueue *vq,
507 d5970055 Michael S. Tsirkin
                                unsigned idx)
508 d5970055 Michael S. Tsirkin
{
509 d5970055 Michael S. Tsirkin
    target_phys_addr_t s, l, a;
510 d5970055 Michael S. Tsirkin
    int r;
511 d5970055 Michael S. Tsirkin
    struct vhost_vring_file file = {
512 d5970055 Michael S. Tsirkin
        .index = idx,
513 d5970055 Michael S. Tsirkin
    };
514 d5970055 Michael S. Tsirkin
    struct vhost_vring_state state = {
515 d5970055 Michael S. Tsirkin
        .index = idx,
516 d5970055 Michael S. Tsirkin
    };
517 d5970055 Michael S. Tsirkin
    struct VirtQueue *vvq = virtio_get_queue(vdev, idx);
518 d5970055 Michael S. Tsirkin
519 d5970055 Michael S. Tsirkin
    if (!vdev->binding->set_host_notifier) {
520 d5970055 Michael S. Tsirkin
        fprintf(stderr, "binding does not support host notifiers\n");
521 d5970055 Michael S. Tsirkin
        return -ENOSYS;
522 d5970055 Michael S. Tsirkin
    }
523 d5970055 Michael S. Tsirkin
524 d5970055 Michael S. Tsirkin
    vq->num = state.num = virtio_queue_get_num(vdev, idx);
525 d5970055 Michael S. Tsirkin
    r = ioctl(dev->control, VHOST_SET_VRING_NUM, &state);
526 d5970055 Michael S. Tsirkin
    if (r) {
527 d5970055 Michael S. Tsirkin
        return -errno;
528 d5970055 Michael S. Tsirkin
    }
529 d5970055 Michael S. Tsirkin
530 d5970055 Michael S. Tsirkin
    state.num = virtio_queue_get_last_avail_idx(vdev, idx);
531 d5970055 Michael S. Tsirkin
    r = ioctl(dev->control, VHOST_SET_VRING_BASE, &state);
532 d5970055 Michael S. Tsirkin
    if (r) {
533 d5970055 Michael S. Tsirkin
        return -errno;
534 d5970055 Michael S. Tsirkin
    }
535 d5970055 Michael S. Tsirkin
536 d5970055 Michael S. Tsirkin
    s = l = virtio_queue_get_desc_size(vdev, idx);
537 d5970055 Michael S. Tsirkin
    a = virtio_queue_get_desc_addr(vdev, idx);
538 d5970055 Michael S. Tsirkin
    vq->desc = cpu_physical_memory_map(a, &l, 0);
539 d5970055 Michael S. Tsirkin
    if (!vq->desc || l != s) {
540 d5970055 Michael S. Tsirkin
        r = -ENOMEM;
541 d5970055 Michael S. Tsirkin
        goto fail_alloc_desc;
542 d5970055 Michael S. Tsirkin
    }
543 d5970055 Michael S. Tsirkin
    s = l = virtio_queue_get_avail_size(vdev, idx);
544 d5970055 Michael S. Tsirkin
    a = virtio_queue_get_avail_addr(vdev, idx);
545 d5970055 Michael S. Tsirkin
    vq->avail = cpu_physical_memory_map(a, &l, 0);
546 d5970055 Michael S. Tsirkin
    if (!vq->avail || l != s) {
547 d5970055 Michael S. Tsirkin
        r = -ENOMEM;
548 d5970055 Michael S. Tsirkin
        goto fail_alloc_avail;
549 d5970055 Michael S. Tsirkin
    }
550 d5970055 Michael S. Tsirkin
    vq->used_size = s = l = virtio_queue_get_used_size(vdev, idx);
551 d5970055 Michael S. Tsirkin
    vq->used_phys = a = virtio_queue_get_used_addr(vdev, idx);
552 d5970055 Michael S. Tsirkin
    vq->used = cpu_physical_memory_map(a, &l, 1);
553 d5970055 Michael S. Tsirkin
    if (!vq->used || l != s) {
554 d5970055 Michael S. Tsirkin
        r = -ENOMEM;
555 d5970055 Michael S. Tsirkin
        goto fail_alloc_used;
556 d5970055 Michael S. Tsirkin
    }
557 d5970055 Michael S. Tsirkin
558 d5970055 Michael S. Tsirkin
    vq->ring_size = s = l = virtio_queue_get_ring_size(vdev, idx);
559 d5970055 Michael S. Tsirkin
    vq->ring_phys = a = virtio_queue_get_ring_addr(vdev, idx);
560 d5970055 Michael S. Tsirkin
    vq->ring = cpu_physical_memory_map(a, &l, 1);
561 d5970055 Michael S. Tsirkin
    if (!vq->ring || l != s) {
562 d5970055 Michael S. Tsirkin
        r = -ENOMEM;
563 d5970055 Michael S. Tsirkin
        goto fail_alloc_ring;
564 d5970055 Michael S. Tsirkin
    }
565 d5970055 Michael S. Tsirkin
566 d5970055 Michael S. Tsirkin
    r = vhost_virtqueue_set_addr(dev, vq, idx, dev->log_enabled);
567 d5970055 Michael S. Tsirkin
    if (r < 0) {
568 d5970055 Michael S. Tsirkin
        r = -errno;
569 d5970055 Michael S. Tsirkin
        goto fail_alloc;
570 d5970055 Michael S. Tsirkin
    }
571 d5970055 Michael S. Tsirkin
    r = vdev->binding->set_host_notifier(vdev->binding_opaque, idx, true);
572 d5970055 Michael S. Tsirkin
    if (r < 0) {
573 d5970055 Michael S. Tsirkin
        fprintf(stderr, "Error binding host notifier: %d\n", -r);
574 d5970055 Michael S. Tsirkin
        goto fail_host_notifier;
575 d5970055 Michael S. Tsirkin
    }
576 d5970055 Michael S. Tsirkin
577 d5970055 Michael S. Tsirkin
    file.fd = event_notifier_get_fd(virtio_queue_get_host_notifier(vvq));
578 d5970055 Michael S. Tsirkin
    r = ioctl(dev->control, VHOST_SET_VRING_KICK, &file);
579 d5970055 Michael S. Tsirkin
    if (r) {
580 c8852121 Michael S. Tsirkin
        r = -errno;
581 d5970055 Michael S. Tsirkin
        goto fail_kick;
582 d5970055 Michael S. Tsirkin
    }
583 d5970055 Michael S. Tsirkin
584 d5970055 Michael S. Tsirkin
    file.fd = event_notifier_get_fd(virtio_queue_get_guest_notifier(vvq));
585 d5970055 Michael S. Tsirkin
    r = ioctl(dev->control, VHOST_SET_VRING_CALL, &file);
586 d5970055 Michael S. Tsirkin
    if (r) {
587 c8852121 Michael S. Tsirkin
        r = -errno;
588 d5970055 Michael S. Tsirkin
        goto fail_call;
589 d5970055 Michael S. Tsirkin
    }
590 d5970055 Michael S. Tsirkin
591 d5970055 Michael S. Tsirkin
    return 0;
592 d5970055 Michael S. Tsirkin
593 d5970055 Michael S. Tsirkin
fail_call:
594 d5970055 Michael S. Tsirkin
fail_kick:
595 d5970055 Michael S. Tsirkin
    vdev->binding->set_host_notifier(vdev->binding_opaque, idx, false);
596 d5970055 Michael S. Tsirkin
fail_host_notifier:
597 d5970055 Michael S. Tsirkin
fail_alloc:
598 d5970055 Michael S. Tsirkin
    cpu_physical_memory_unmap(vq->ring, virtio_queue_get_ring_size(vdev, idx),
599 d5970055 Michael S. Tsirkin
                              0, 0);
600 d5970055 Michael S. Tsirkin
fail_alloc_ring:
601 d5970055 Michael S. Tsirkin
    cpu_physical_memory_unmap(vq->used, virtio_queue_get_used_size(vdev, idx),
602 d5970055 Michael S. Tsirkin
                              0, 0);
603 d5970055 Michael S. Tsirkin
fail_alloc_used:
604 d5970055 Michael S. Tsirkin
    cpu_physical_memory_unmap(vq->avail, virtio_queue_get_avail_size(vdev, idx),
605 d5970055 Michael S. Tsirkin
                              0, 0);
606 d5970055 Michael S. Tsirkin
fail_alloc_avail:
607 d5970055 Michael S. Tsirkin
    cpu_physical_memory_unmap(vq->desc, virtio_queue_get_desc_size(vdev, idx),
608 d5970055 Michael S. Tsirkin
                              0, 0);
609 d5970055 Michael S. Tsirkin
fail_alloc_desc:
610 d5970055 Michael S. Tsirkin
    return r;
611 d5970055 Michael S. Tsirkin
}
612 d5970055 Michael S. Tsirkin
613 d5970055 Michael S. Tsirkin
static void vhost_virtqueue_cleanup(struct vhost_dev *dev,
614 d5970055 Michael S. Tsirkin
                                    struct VirtIODevice *vdev,
615 d5970055 Michael S. Tsirkin
                                    struct vhost_virtqueue *vq,
616 d5970055 Michael S. Tsirkin
                                    unsigned idx)
617 d5970055 Michael S. Tsirkin
{
618 d5970055 Michael S. Tsirkin
    struct vhost_vring_state state = {
619 d5970055 Michael S. Tsirkin
        .index = idx,
620 d5970055 Michael S. Tsirkin
    };
621 d5970055 Michael S. Tsirkin
    int r;
622 d5970055 Michael S. Tsirkin
    r = vdev->binding->set_host_notifier(vdev->binding_opaque, idx, false);
623 d5970055 Michael S. Tsirkin
    if (r < 0) {
624 d5970055 Michael S. Tsirkin
        fprintf(stderr, "vhost VQ %d host cleanup failed: %d\n", idx, r);
625 d5970055 Michael S. Tsirkin
        fflush(stderr);
626 d5970055 Michael S. Tsirkin
    }
627 d5970055 Michael S. Tsirkin
    assert (r >= 0);
628 d5970055 Michael S. Tsirkin
    r = ioctl(dev->control, VHOST_GET_VRING_BASE, &state);
629 d5970055 Michael S. Tsirkin
    if (r < 0) {
630 d5970055 Michael S. Tsirkin
        fprintf(stderr, "vhost VQ %d ring restore failed: %d\n", idx, r);
631 d5970055 Michael S. Tsirkin
        fflush(stderr);
632 d5970055 Michael S. Tsirkin
    }
633 d5970055 Michael S. Tsirkin
    virtio_queue_set_last_avail_idx(vdev, idx, state.num);
634 d5970055 Michael S. Tsirkin
    assert (r >= 0);
635 d5970055 Michael S. Tsirkin
    cpu_physical_memory_unmap(vq->ring, virtio_queue_get_ring_size(vdev, idx),
636 d5970055 Michael S. Tsirkin
                              0, virtio_queue_get_ring_size(vdev, idx));
637 d5970055 Michael S. Tsirkin
    cpu_physical_memory_unmap(vq->used, virtio_queue_get_used_size(vdev, idx),
638 d5970055 Michael S. Tsirkin
                              1, virtio_queue_get_used_size(vdev, idx));
639 d5970055 Michael S. Tsirkin
    cpu_physical_memory_unmap(vq->avail, virtio_queue_get_avail_size(vdev, idx),
640 d5970055 Michael S. Tsirkin
                              0, virtio_queue_get_avail_size(vdev, idx));
641 d5970055 Michael S. Tsirkin
    cpu_physical_memory_unmap(vq->desc, virtio_queue_get_desc_size(vdev, idx),
642 d5970055 Michael S. Tsirkin
                              0, virtio_queue_get_desc_size(vdev, idx));
643 d5970055 Michael S. Tsirkin
}
644 d5970055 Michael S. Tsirkin
645 5430a28f mst@redhat.com
int vhost_dev_init(struct vhost_dev *hdev, int devfd, bool force)
646 d5970055 Michael S. Tsirkin
{
647 d5970055 Michael S. Tsirkin
    uint64_t features;
648 d5970055 Michael S. Tsirkin
    int r;
649 d5970055 Michael S. Tsirkin
    if (devfd >= 0) {
650 d5970055 Michael S. Tsirkin
        hdev->control = devfd;
651 d5970055 Michael S. Tsirkin
    } else {
652 d5970055 Michael S. Tsirkin
        hdev->control = open("/dev/vhost-net", O_RDWR);
653 d5970055 Michael S. Tsirkin
        if (hdev->control < 0) {
654 d5970055 Michael S. Tsirkin
            return -errno;
655 d5970055 Michael S. Tsirkin
        }
656 d5970055 Michael S. Tsirkin
    }
657 d5970055 Michael S. Tsirkin
    r = ioctl(hdev->control, VHOST_SET_OWNER, NULL);
658 d5970055 Michael S. Tsirkin
    if (r < 0) {
659 d5970055 Michael S. Tsirkin
        goto fail;
660 d5970055 Michael S. Tsirkin
    }
661 d5970055 Michael S. Tsirkin
662 d5970055 Michael S. Tsirkin
    r = ioctl(hdev->control, VHOST_GET_FEATURES, &features);
663 d5970055 Michael S. Tsirkin
    if (r < 0) {
664 d5970055 Michael S. Tsirkin
        goto fail;
665 d5970055 Michael S. Tsirkin
    }
666 d5970055 Michael S. Tsirkin
    hdev->features = features;
667 d5970055 Michael S. Tsirkin
668 d5970055 Michael S. Tsirkin
    hdev->client.set_memory = vhost_client_set_memory;
669 d5970055 Michael S. Tsirkin
    hdev->client.sync_dirty_bitmap = vhost_client_sync_dirty_bitmap;
670 d5970055 Michael S. Tsirkin
    hdev->client.migration_log = vhost_client_migration_log;
671 e5896b12 Anthony PERARD
    hdev->client.log_start = NULL;
672 e5896b12 Anthony PERARD
    hdev->client.log_stop = NULL;
673 d5970055 Michael S. Tsirkin
    hdev->mem = qemu_mallocz(offsetof(struct vhost_memory, regions));
674 d5970055 Michael S. Tsirkin
    hdev->log = NULL;
675 d5970055 Michael S. Tsirkin
    hdev->log_size = 0;
676 d5970055 Michael S. Tsirkin
    hdev->log_enabled = false;
677 d5970055 Michael S. Tsirkin
    hdev->started = false;
678 d5970055 Michael S. Tsirkin
    cpu_register_phys_memory_client(&hdev->client);
679 5430a28f mst@redhat.com
    hdev->force = force;
680 d5970055 Michael S. Tsirkin
    return 0;
681 d5970055 Michael S. Tsirkin
fail:
682 d5970055 Michael S. Tsirkin
    r = -errno;
683 d5970055 Michael S. Tsirkin
    close(hdev->control);
684 d5970055 Michael S. Tsirkin
    return r;
685 d5970055 Michael S. Tsirkin
}
686 d5970055 Michael S. Tsirkin
687 d5970055 Michael S. Tsirkin
void vhost_dev_cleanup(struct vhost_dev *hdev)
688 d5970055 Michael S. Tsirkin
{
689 d5970055 Michael S. Tsirkin
    cpu_unregister_phys_memory_client(&hdev->client);
690 d5970055 Michael S. Tsirkin
    qemu_free(hdev->mem);
691 d5970055 Michael S. Tsirkin
    close(hdev->control);
692 d5970055 Michael S. Tsirkin
}
693 d5970055 Michael S. Tsirkin
694 5430a28f mst@redhat.com
bool vhost_dev_query(struct vhost_dev *hdev, VirtIODevice *vdev)
695 5430a28f mst@redhat.com
{
696 5430a28f mst@redhat.com
    return !vdev->binding->query_guest_notifiers ||
697 5430a28f mst@redhat.com
        vdev->binding->query_guest_notifiers(vdev->binding_opaque) ||
698 5430a28f mst@redhat.com
        hdev->force;
699 5430a28f mst@redhat.com
}
700 5430a28f mst@redhat.com
701 d5970055 Michael S. Tsirkin
int vhost_dev_start(struct vhost_dev *hdev, VirtIODevice *vdev)
702 d5970055 Michael S. Tsirkin
{
703 d5970055 Michael S. Tsirkin
    int i, r;
704 54dd9321 Michael S. Tsirkin
    if (!vdev->binding->set_guest_notifiers) {
705 54dd9321 Michael S. Tsirkin
        fprintf(stderr, "binding does not support guest notifiers\n");
706 54dd9321 Michael S. Tsirkin
        r = -ENOSYS;
707 54dd9321 Michael S. Tsirkin
        goto fail;
708 54dd9321 Michael S. Tsirkin
    }
709 54dd9321 Michael S. Tsirkin
710 54dd9321 Michael S. Tsirkin
    r = vdev->binding->set_guest_notifiers(vdev->binding_opaque, true);
711 54dd9321 Michael S. Tsirkin
    if (r < 0) {
712 54dd9321 Michael S. Tsirkin
        fprintf(stderr, "Error binding guest notifier: %d\n", -r);
713 54dd9321 Michael S. Tsirkin
        goto fail_notifiers;
714 54dd9321 Michael S. Tsirkin
    }
715 d5970055 Michael S. Tsirkin
716 d5970055 Michael S. Tsirkin
    r = vhost_dev_set_features(hdev, hdev->log_enabled);
717 d5970055 Michael S. Tsirkin
    if (r < 0) {
718 54dd9321 Michael S. Tsirkin
        goto fail_features;
719 d5970055 Michael S. Tsirkin
    }
720 d5970055 Michael S. Tsirkin
    r = ioctl(hdev->control, VHOST_SET_MEM_TABLE, hdev->mem);
721 d5970055 Michael S. Tsirkin
    if (r < 0) {
722 d5970055 Michael S. Tsirkin
        r = -errno;
723 54dd9321 Michael S. Tsirkin
        goto fail_mem;
724 d5970055 Michael S. Tsirkin
    }
725 d154e0ba Michael S. Tsirkin
    for (i = 0; i < hdev->nvqs; ++i) {
726 d154e0ba Michael S. Tsirkin
        r = vhost_virtqueue_init(hdev,
727 d154e0ba Michael S. Tsirkin
                                 vdev,
728 d154e0ba Michael S. Tsirkin
                                 hdev->vqs + i,
729 d154e0ba Michael S. Tsirkin
                                 i);
730 d154e0ba Michael S. Tsirkin
        if (r < 0) {
731 d154e0ba Michael S. Tsirkin
            goto fail_vq;
732 d154e0ba Michael S. Tsirkin
        }
733 d154e0ba Michael S. Tsirkin
    }
734 d154e0ba Michael S. Tsirkin
735 d5970055 Michael S. Tsirkin
    if (hdev->log_enabled) {
736 d5970055 Michael S. Tsirkin
        hdev->log_size = vhost_get_log_size(hdev);
737 d5970055 Michael S. Tsirkin
        hdev->log = hdev->log_size ?
738 d5970055 Michael S. Tsirkin
            qemu_mallocz(hdev->log_size * sizeof *hdev->log) : NULL;
739 d5970055 Michael S. Tsirkin
        r = ioctl(hdev->control, VHOST_SET_LOG_BASE,
740 d5970055 Michael S. Tsirkin
                  (uint64_t)(unsigned long)hdev->log);
741 d5970055 Michael S. Tsirkin
        if (r < 0) {
742 d5970055 Michael S. Tsirkin
            r = -errno;
743 54dd9321 Michael S. Tsirkin
            goto fail_log;
744 d5970055 Michael S. Tsirkin
        }
745 d5970055 Michael S. Tsirkin
    }
746 d154e0ba Michael S. Tsirkin
747 d5970055 Michael S. Tsirkin
    hdev->started = true;
748 d5970055 Michael S. Tsirkin
749 d5970055 Michael S. Tsirkin
    return 0;
750 54dd9321 Michael S. Tsirkin
fail_log:
751 d5970055 Michael S. Tsirkin
fail_vq:
752 d5970055 Michael S. Tsirkin
    while (--i >= 0) {
753 d5970055 Michael S. Tsirkin
        vhost_virtqueue_cleanup(hdev,
754 d5970055 Michael S. Tsirkin
                                vdev,
755 d5970055 Michael S. Tsirkin
                                hdev->vqs + i,
756 d5970055 Michael S. Tsirkin
                                i);
757 d5970055 Michael S. Tsirkin
    }
758 54dd9321 Michael S. Tsirkin
fail_mem:
759 54dd9321 Michael S. Tsirkin
fail_features:
760 54dd9321 Michael S. Tsirkin
    vdev->binding->set_guest_notifiers(vdev->binding_opaque, false);
761 54dd9321 Michael S. Tsirkin
fail_notifiers:
762 d5970055 Michael S. Tsirkin
fail:
763 d5970055 Michael S. Tsirkin
    return r;
764 d5970055 Michael S. Tsirkin
}
765 d5970055 Michael S. Tsirkin
766 d5970055 Michael S. Tsirkin
void vhost_dev_stop(struct vhost_dev *hdev, VirtIODevice *vdev)
767 d5970055 Michael S. Tsirkin
{
768 54dd9321 Michael S. Tsirkin
    int i, r;
769 54dd9321 Michael S. Tsirkin
770 d5970055 Michael S. Tsirkin
    for (i = 0; i < hdev->nvqs; ++i) {
771 d5970055 Michael S. Tsirkin
        vhost_virtqueue_cleanup(hdev,
772 d5970055 Michael S. Tsirkin
                                vdev,
773 d5970055 Michael S. Tsirkin
                                hdev->vqs + i,
774 d5970055 Michael S. Tsirkin
                                i);
775 d5970055 Michael S. Tsirkin
    }
776 d5970055 Michael S. Tsirkin
    vhost_client_sync_dirty_bitmap(&hdev->client, 0,
777 d5970055 Michael S. Tsirkin
                                   (target_phys_addr_t)~0x0ull);
778 54dd9321 Michael S. Tsirkin
    r = vdev->binding->set_guest_notifiers(vdev->binding_opaque, false);
779 54dd9321 Michael S. Tsirkin
    if (r < 0) {
780 54dd9321 Michael S. Tsirkin
        fprintf(stderr, "vhost guest notifier cleanup failed: %d\n", r);
781 54dd9321 Michael S. Tsirkin
        fflush(stderr);
782 54dd9321 Michael S. Tsirkin
    }
783 54dd9321 Michael S. Tsirkin
    assert (r >= 0);
784 54dd9321 Michael S. Tsirkin
785 d5970055 Michael S. Tsirkin
    hdev->started = false;
786 d5970055 Michael S. Tsirkin
    qemu_free(hdev->log);
787 d5970055 Michael S. Tsirkin
    hdev->log_size = 0;
788 d5970055 Michael S. Tsirkin
}