Statistics
| Branch: | Revision:

root / hw / vhost.c @ 8f354003

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