Statistics
| Branch: | Revision:

root / hw / vhost.c @ 7c7b829e

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