Statistics
| Branch: | Revision:

root / hw / vhost.c @ 0fd542fb

History | View | Annotate | Download (22.7 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 d5970055 Michael S. Tsirkin
static void vhost_client_set_memory(CPUPhysMemoryClient *client,
301 d5970055 Michael S. Tsirkin
                                    target_phys_addr_t start_addr,
302 d5970055 Michael S. Tsirkin
                                    ram_addr_t size,
303 0fd542fb Michael S. Tsirkin
                                    ram_addr_t phys_offset,
304 0fd542fb Michael S. Tsirkin
                                    bool log_dirty)
305 d5970055 Michael S. Tsirkin
{
306 d5970055 Michael S. Tsirkin
    struct vhost_dev *dev = container_of(client, struct vhost_dev, client);
307 d5970055 Michael S. Tsirkin
    ram_addr_t flags = phys_offset & ~TARGET_PAGE_MASK;
308 d5970055 Michael S. Tsirkin
    int s = offsetof(struct vhost_memory, regions) +
309 d5970055 Michael S. Tsirkin
        (dev->mem->nregions + 1) * sizeof dev->mem->regions[0];
310 d5970055 Michael S. Tsirkin
    uint64_t log_size;
311 d5970055 Michael S. Tsirkin
    int r;
312 d5970055 Michael S. Tsirkin
    dev->mem = qemu_realloc(dev->mem, s);
313 d5970055 Michael S. Tsirkin
314 d5970055 Michael S. Tsirkin
    assert(size);
315 d5970055 Michael S. Tsirkin
316 d5970055 Michael S. Tsirkin
    vhost_dev_unassign_memory(dev, start_addr, size);
317 d5970055 Michael S. Tsirkin
    if (flags == IO_MEM_RAM) {
318 d5970055 Michael S. Tsirkin
        /* Add given mapping, merging adjacent regions if any */
319 d5970055 Michael S. Tsirkin
        vhost_dev_assign_memory(dev, start_addr, size,
320 d5970055 Michael S. Tsirkin
                                (uintptr_t)qemu_get_ram_ptr(phys_offset));
321 d5970055 Michael S. Tsirkin
    } else {
322 d5970055 Michael S. Tsirkin
        /* Remove old mapping for this memory, if any. */
323 d5970055 Michael S. Tsirkin
        vhost_dev_unassign_memory(dev, start_addr, size);
324 d5970055 Michael S. Tsirkin
    }
325 d5970055 Michael S. Tsirkin
326 d5970055 Michael S. Tsirkin
    if (!dev->started) {
327 d5970055 Michael S. Tsirkin
        return;
328 d5970055 Michael S. Tsirkin
    }
329 d5970055 Michael S. Tsirkin
330 d5970055 Michael S. Tsirkin
    if (dev->started) {
331 d5970055 Michael S. Tsirkin
        r = vhost_verify_ring_mappings(dev, start_addr, size);
332 d5970055 Michael S. Tsirkin
        assert(r >= 0);
333 d5970055 Michael S. Tsirkin
    }
334 d5970055 Michael S. Tsirkin
335 d5970055 Michael S. Tsirkin
    if (!dev->log_enabled) {
336 d5970055 Michael S. Tsirkin
        r = ioctl(dev->control, VHOST_SET_MEM_TABLE, dev->mem);
337 d5970055 Michael S. Tsirkin
        assert(r >= 0);
338 d5970055 Michael S. Tsirkin
        return;
339 d5970055 Michael S. Tsirkin
    }
340 d5970055 Michael S. Tsirkin
    log_size = vhost_get_log_size(dev);
341 d5970055 Michael S. Tsirkin
    /* We allocate an extra 4K bytes to log,
342 d5970055 Michael S. Tsirkin
     * to reduce the * number of reallocations. */
343 d5970055 Michael S. Tsirkin
#define VHOST_LOG_BUFFER (0x1000 / sizeof *dev->log)
344 d5970055 Michael S. Tsirkin
    /* To log more, must increase log size before table update. */
345 d5970055 Michael S. Tsirkin
    if (dev->log_size < log_size) {
346 d5970055 Michael S. Tsirkin
        vhost_dev_log_resize(dev, log_size + VHOST_LOG_BUFFER);
347 d5970055 Michael S. Tsirkin
    }
348 d5970055 Michael S. Tsirkin
    r = ioctl(dev->control, VHOST_SET_MEM_TABLE, dev->mem);
349 d5970055 Michael S. Tsirkin
    assert(r >= 0);
350 d5970055 Michael S. Tsirkin
    /* To log less, can only decrease log size after table update. */
351 d5970055 Michael S. Tsirkin
    if (dev->log_size > log_size + VHOST_LOG_BUFFER) {
352 d5970055 Michael S. Tsirkin
        vhost_dev_log_resize(dev, log_size);
353 d5970055 Michael S. Tsirkin
    }
354 d5970055 Michael S. Tsirkin
}
355 d5970055 Michael S. Tsirkin
356 d5970055 Michael S. Tsirkin
static int vhost_virtqueue_set_addr(struct vhost_dev *dev,
357 d5970055 Michael S. Tsirkin
                                    struct vhost_virtqueue *vq,
358 d5970055 Michael S. Tsirkin
                                    unsigned idx, bool enable_log)
359 d5970055 Michael S. Tsirkin
{
360 d5970055 Michael S. Tsirkin
    struct vhost_vring_addr addr = {
361 d5970055 Michael S. Tsirkin
        .index = idx,
362 2b3af999 Stefan Weil
        .desc_user_addr = (uint64_t)(unsigned long)vq->desc,
363 2b3af999 Stefan Weil
        .avail_user_addr = (uint64_t)(unsigned long)vq->avail,
364 2b3af999 Stefan Weil
        .used_user_addr = (uint64_t)(unsigned long)vq->used,
365 d5970055 Michael S. Tsirkin
        .log_guest_addr = vq->used_phys,
366 d5970055 Michael S. Tsirkin
        .flags = enable_log ? (1 << VHOST_VRING_F_LOG) : 0,
367 d5970055 Michael S. Tsirkin
    };
368 d5970055 Michael S. Tsirkin
    int r = ioctl(dev->control, VHOST_SET_VRING_ADDR, &addr);
369 d5970055 Michael S. Tsirkin
    if (r < 0) {
370 d5970055 Michael S. Tsirkin
        return -errno;
371 d5970055 Michael S. Tsirkin
    }
372 d5970055 Michael S. Tsirkin
    return 0;
373 d5970055 Michael S. Tsirkin
}
374 d5970055 Michael S. Tsirkin
375 d5970055 Michael S. Tsirkin
static int vhost_dev_set_features(struct vhost_dev *dev, bool enable_log)
376 d5970055 Michael S. Tsirkin
{
377 d5970055 Michael S. Tsirkin
    uint64_t features = dev->acked_features;
378 d5970055 Michael S. Tsirkin
    int r;
379 d5970055 Michael S. Tsirkin
    if (enable_log) {
380 d5970055 Michael S. Tsirkin
        features |= 0x1 << VHOST_F_LOG_ALL;
381 d5970055 Michael S. Tsirkin
    }
382 d5970055 Michael S. Tsirkin
    r = ioctl(dev->control, VHOST_SET_FEATURES, &features);
383 d5970055 Michael S. Tsirkin
    return r < 0 ? -errno : 0;
384 d5970055 Michael S. Tsirkin
}
385 d5970055 Michael S. Tsirkin
386 d5970055 Michael S. Tsirkin
static int vhost_dev_set_log(struct vhost_dev *dev, bool enable_log)
387 d5970055 Michael S. Tsirkin
{
388 d5970055 Michael S. Tsirkin
    int r, t, i;
389 d5970055 Michael S. Tsirkin
    r = vhost_dev_set_features(dev, enable_log);
390 d5970055 Michael S. Tsirkin
    if (r < 0) {
391 d5970055 Michael S. Tsirkin
        goto err_features;
392 d5970055 Michael S. Tsirkin
    }
393 d5970055 Michael S. Tsirkin
    for (i = 0; i < dev->nvqs; ++i) {
394 d5970055 Michael S. Tsirkin
        r = vhost_virtqueue_set_addr(dev, dev->vqs + i, i,
395 d5970055 Michael S. Tsirkin
                                     enable_log);
396 d5970055 Michael S. Tsirkin
        if (r < 0) {
397 d5970055 Michael S. Tsirkin
            goto err_vq;
398 d5970055 Michael S. Tsirkin
        }
399 d5970055 Michael S. Tsirkin
    }
400 d5970055 Michael S. Tsirkin
    return 0;
401 d5970055 Michael S. Tsirkin
err_vq:
402 d5970055 Michael S. Tsirkin
    for (; i >= 0; --i) {
403 d5970055 Michael S. Tsirkin
        t = vhost_virtqueue_set_addr(dev, dev->vqs + i, i,
404 d5970055 Michael S. Tsirkin
                                     dev->log_enabled);
405 d5970055 Michael S. Tsirkin
        assert(t >= 0);
406 d5970055 Michael S. Tsirkin
    }
407 d5970055 Michael S. Tsirkin
    t = vhost_dev_set_features(dev, dev->log_enabled);
408 d5970055 Michael S. Tsirkin
    assert(t >= 0);
409 d5970055 Michael S. Tsirkin
err_features:
410 d5970055 Michael S. Tsirkin
    return r;
411 d5970055 Michael S. Tsirkin
}
412 d5970055 Michael S. Tsirkin
413 d5970055 Michael S. Tsirkin
static int vhost_client_migration_log(CPUPhysMemoryClient *client,
414 d5970055 Michael S. Tsirkin
                                      int enable)
415 d5970055 Michael S. Tsirkin
{
416 d5970055 Michael S. Tsirkin
    struct vhost_dev *dev = container_of(client, struct vhost_dev, client);
417 d5970055 Michael S. Tsirkin
    int r;
418 d5970055 Michael S. Tsirkin
    if (!!enable == dev->log_enabled) {
419 d5970055 Michael S. Tsirkin
        return 0;
420 d5970055 Michael S. Tsirkin
    }
421 d5970055 Michael S. Tsirkin
    if (!dev->started) {
422 d5970055 Michael S. Tsirkin
        dev->log_enabled = enable;
423 d5970055 Michael S. Tsirkin
        return 0;
424 d5970055 Michael S. Tsirkin
    }
425 d5970055 Michael S. Tsirkin
    if (!enable) {
426 d5970055 Michael S. Tsirkin
        r = vhost_dev_set_log(dev, false);
427 d5970055 Michael S. Tsirkin
        if (r < 0) {
428 d5970055 Michael S. Tsirkin
            return r;
429 d5970055 Michael S. Tsirkin
        }
430 d5970055 Michael S. Tsirkin
        if (dev->log) {
431 d5970055 Michael S. Tsirkin
            qemu_free(dev->log);
432 d5970055 Michael S. Tsirkin
        }
433 d5970055 Michael S. Tsirkin
        dev->log = NULL;
434 d5970055 Michael S. Tsirkin
        dev->log_size = 0;
435 d5970055 Michael S. Tsirkin
    } else {
436 d5970055 Michael S. Tsirkin
        vhost_dev_log_resize(dev, vhost_get_log_size(dev));
437 d5970055 Michael S. Tsirkin
        r = vhost_dev_set_log(dev, true);
438 d5970055 Michael S. Tsirkin
        if (r < 0) {
439 d5970055 Michael S. Tsirkin
            return r;
440 d5970055 Michael S. Tsirkin
        }
441 d5970055 Michael S. Tsirkin
    }
442 d5970055 Michael S. Tsirkin
    dev->log_enabled = enable;
443 d5970055 Michael S. Tsirkin
    return 0;
444 d5970055 Michael S. Tsirkin
}
445 d5970055 Michael S. Tsirkin
446 d5970055 Michael S. Tsirkin
static int vhost_virtqueue_init(struct vhost_dev *dev,
447 d5970055 Michael S. Tsirkin
                                struct VirtIODevice *vdev,
448 d5970055 Michael S. Tsirkin
                                struct vhost_virtqueue *vq,
449 d5970055 Michael S. Tsirkin
                                unsigned idx)
450 d5970055 Michael S. Tsirkin
{
451 d5970055 Michael S. Tsirkin
    target_phys_addr_t s, l, a;
452 d5970055 Michael S. Tsirkin
    int r;
453 d5970055 Michael S. Tsirkin
    struct vhost_vring_file file = {
454 d5970055 Michael S. Tsirkin
        .index = idx,
455 d5970055 Michael S. Tsirkin
    };
456 d5970055 Michael S. Tsirkin
    struct vhost_vring_state state = {
457 d5970055 Michael S. Tsirkin
        .index = idx,
458 d5970055 Michael S. Tsirkin
    };
459 d5970055 Michael S. Tsirkin
    struct VirtQueue *vvq = virtio_get_queue(vdev, idx);
460 d5970055 Michael S. Tsirkin
461 d5970055 Michael S. Tsirkin
    if (!vdev->binding->set_host_notifier) {
462 d5970055 Michael S. Tsirkin
        fprintf(stderr, "binding does not support host notifiers\n");
463 d5970055 Michael S. Tsirkin
        return -ENOSYS;
464 d5970055 Michael S. Tsirkin
    }
465 d5970055 Michael S. Tsirkin
466 d5970055 Michael S. Tsirkin
    vq->num = state.num = virtio_queue_get_num(vdev, idx);
467 d5970055 Michael S. Tsirkin
    r = ioctl(dev->control, VHOST_SET_VRING_NUM, &state);
468 d5970055 Michael S. Tsirkin
    if (r) {
469 d5970055 Michael S. Tsirkin
        return -errno;
470 d5970055 Michael S. Tsirkin
    }
471 d5970055 Michael S. Tsirkin
472 d5970055 Michael S. Tsirkin
    state.num = virtio_queue_get_last_avail_idx(vdev, idx);
473 d5970055 Michael S. Tsirkin
    r = ioctl(dev->control, VHOST_SET_VRING_BASE, &state);
474 d5970055 Michael S. Tsirkin
    if (r) {
475 d5970055 Michael S. Tsirkin
        return -errno;
476 d5970055 Michael S. Tsirkin
    }
477 d5970055 Michael S. Tsirkin
478 d5970055 Michael S. Tsirkin
    s = l = virtio_queue_get_desc_size(vdev, idx);
479 d5970055 Michael S. Tsirkin
    a = virtio_queue_get_desc_addr(vdev, idx);
480 d5970055 Michael S. Tsirkin
    vq->desc = cpu_physical_memory_map(a, &l, 0);
481 d5970055 Michael S. Tsirkin
    if (!vq->desc || l != s) {
482 d5970055 Michael S. Tsirkin
        r = -ENOMEM;
483 d5970055 Michael S. Tsirkin
        goto fail_alloc_desc;
484 d5970055 Michael S. Tsirkin
    }
485 d5970055 Michael S. Tsirkin
    s = l = virtio_queue_get_avail_size(vdev, idx);
486 d5970055 Michael S. Tsirkin
    a = virtio_queue_get_avail_addr(vdev, idx);
487 d5970055 Michael S. Tsirkin
    vq->avail = cpu_physical_memory_map(a, &l, 0);
488 d5970055 Michael S. Tsirkin
    if (!vq->avail || l != s) {
489 d5970055 Michael S. Tsirkin
        r = -ENOMEM;
490 d5970055 Michael S. Tsirkin
        goto fail_alloc_avail;
491 d5970055 Michael S. Tsirkin
    }
492 d5970055 Michael S. Tsirkin
    vq->used_size = s = l = virtio_queue_get_used_size(vdev, idx);
493 d5970055 Michael S. Tsirkin
    vq->used_phys = a = virtio_queue_get_used_addr(vdev, idx);
494 d5970055 Michael S. Tsirkin
    vq->used = cpu_physical_memory_map(a, &l, 1);
495 d5970055 Michael S. Tsirkin
    if (!vq->used || l != s) {
496 d5970055 Michael S. Tsirkin
        r = -ENOMEM;
497 d5970055 Michael S. Tsirkin
        goto fail_alloc_used;
498 d5970055 Michael S. Tsirkin
    }
499 d5970055 Michael S. Tsirkin
500 d5970055 Michael S. Tsirkin
    vq->ring_size = s = l = virtio_queue_get_ring_size(vdev, idx);
501 d5970055 Michael S. Tsirkin
    vq->ring_phys = a = virtio_queue_get_ring_addr(vdev, idx);
502 d5970055 Michael S. Tsirkin
    vq->ring = cpu_physical_memory_map(a, &l, 1);
503 d5970055 Michael S. Tsirkin
    if (!vq->ring || l != s) {
504 d5970055 Michael S. Tsirkin
        r = -ENOMEM;
505 d5970055 Michael S. Tsirkin
        goto fail_alloc_ring;
506 d5970055 Michael S. Tsirkin
    }
507 d5970055 Michael S. Tsirkin
508 d5970055 Michael S. Tsirkin
    r = vhost_virtqueue_set_addr(dev, vq, idx, dev->log_enabled);
509 d5970055 Michael S. Tsirkin
    if (r < 0) {
510 d5970055 Michael S. Tsirkin
        r = -errno;
511 d5970055 Michael S. Tsirkin
        goto fail_alloc;
512 d5970055 Michael S. Tsirkin
    }
513 d5970055 Michael S. Tsirkin
    r = vdev->binding->set_host_notifier(vdev->binding_opaque, idx, true);
514 d5970055 Michael S. Tsirkin
    if (r < 0) {
515 d5970055 Michael S. Tsirkin
        fprintf(stderr, "Error binding host notifier: %d\n", -r);
516 d5970055 Michael S. Tsirkin
        goto fail_host_notifier;
517 d5970055 Michael S. Tsirkin
    }
518 d5970055 Michael S. Tsirkin
519 d5970055 Michael S. Tsirkin
    file.fd = event_notifier_get_fd(virtio_queue_get_host_notifier(vvq));
520 d5970055 Michael S. Tsirkin
    r = ioctl(dev->control, VHOST_SET_VRING_KICK, &file);
521 d5970055 Michael S. Tsirkin
    if (r) {
522 c8852121 Michael S. Tsirkin
        r = -errno;
523 d5970055 Michael S. Tsirkin
        goto fail_kick;
524 d5970055 Michael S. Tsirkin
    }
525 d5970055 Michael S. Tsirkin
526 d5970055 Michael S. Tsirkin
    file.fd = event_notifier_get_fd(virtio_queue_get_guest_notifier(vvq));
527 d5970055 Michael S. Tsirkin
    r = ioctl(dev->control, VHOST_SET_VRING_CALL, &file);
528 d5970055 Michael S. Tsirkin
    if (r) {
529 c8852121 Michael S. Tsirkin
        r = -errno;
530 d5970055 Michael S. Tsirkin
        goto fail_call;
531 d5970055 Michael S. Tsirkin
    }
532 d5970055 Michael S. Tsirkin
533 d5970055 Michael S. Tsirkin
    return 0;
534 d5970055 Michael S. Tsirkin
535 d5970055 Michael S. Tsirkin
fail_call:
536 d5970055 Michael S. Tsirkin
fail_kick:
537 d5970055 Michael S. Tsirkin
    vdev->binding->set_host_notifier(vdev->binding_opaque, idx, false);
538 d5970055 Michael S. Tsirkin
fail_host_notifier:
539 d5970055 Michael S. Tsirkin
fail_alloc:
540 d5970055 Michael S. Tsirkin
    cpu_physical_memory_unmap(vq->ring, virtio_queue_get_ring_size(vdev, idx),
541 d5970055 Michael S. Tsirkin
                              0, 0);
542 d5970055 Michael S. Tsirkin
fail_alloc_ring:
543 d5970055 Michael S. Tsirkin
    cpu_physical_memory_unmap(vq->used, virtio_queue_get_used_size(vdev, idx),
544 d5970055 Michael S. Tsirkin
                              0, 0);
545 d5970055 Michael S. Tsirkin
fail_alloc_used:
546 d5970055 Michael S. Tsirkin
    cpu_physical_memory_unmap(vq->avail, virtio_queue_get_avail_size(vdev, idx),
547 d5970055 Michael S. Tsirkin
                              0, 0);
548 d5970055 Michael S. Tsirkin
fail_alloc_avail:
549 d5970055 Michael S. Tsirkin
    cpu_physical_memory_unmap(vq->desc, virtio_queue_get_desc_size(vdev, idx),
550 d5970055 Michael S. Tsirkin
                              0, 0);
551 d5970055 Michael S. Tsirkin
fail_alloc_desc:
552 d5970055 Michael S. Tsirkin
    return r;
553 d5970055 Michael S. Tsirkin
}
554 d5970055 Michael S. Tsirkin
555 d5970055 Michael S. Tsirkin
static void vhost_virtqueue_cleanup(struct vhost_dev *dev,
556 d5970055 Michael S. Tsirkin
                                    struct VirtIODevice *vdev,
557 d5970055 Michael S. Tsirkin
                                    struct vhost_virtqueue *vq,
558 d5970055 Michael S. Tsirkin
                                    unsigned idx)
559 d5970055 Michael S. Tsirkin
{
560 d5970055 Michael S. Tsirkin
    struct vhost_vring_state state = {
561 d5970055 Michael S. Tsirkin
        .index = idx,
562 d5970055 Michael S. Tsirkin
    };
563 d5970055 Michael S. Tsirkin
    int r;
564 d5970055 Michael S. Tsirkin
    r = vdev->binding->set_host_notifier(vdev->binding_opaque, idx, false);
565 d5970055 Michael S. Tsirkin
    if (r < 0) {
566 d5970055 Michael S. Tsirkin
        fprintf(stderr, "vhost VQ %d host cleanup failed: %d\n", idx, r);
567 d5970055 Michael S. Tsirkin
        fflush(stderr);
568 d5970055 Michael S. Tsirkin
    }
569 d5970055 Michael S. Tsirkin
    assert (r >= 0);
570 d5970055 Michael S. Tsirkin
    r = ioctl(dev->control, VHOST_GET_VRING_BASE, &state);
571 d5970055 Michael S. Tsirkin
    if (r < 0) {
572 d5970055 Michael S. Tsirkin
        fprintf(stderr, "vhost VQ %d ring restore failed: %d\n", idx, r);
573 d5970055 Michael S. Tsirkin
        fflush(stderr);
574 d5970055 Michael S. Tsirkin
    }
575 d5970055 Michael S. Tsirkin
    virtio_queue_set_last_avail_idx(vdev, idx, state.num);
576 d5970055 Michael S. Tsirkin
    assert (r >= 0);
577 d5970055 Michael S. Tsirkin
    cpu_physical_memory_unmap(vq->ring, virtio_queue_get_ring_size(vdev, idx),
578 d5970055 Michael S. Tsirkin
                              0, virtio_queue_get_ring_size(vdev, idx));
579 d5970055 Michael S. Tsirkin
    cpu_physical_memory_unmap(vq->used, virtio_queue_get_used_size(vdev, idx),
580 d5970055 Michael S. Tsirkin
                              1, virtio_queue_get_used_size(vdev, idx));
581 d5970055 Michael S. Tsirkin
    cpu_physical_memory_unmap(vq->avail, virtio_queue_get_avail_size(vdev, idx),
582 d5970055 Michael S. Tsirkin
                              0, virtio_queue_get_avail_size(vdev, idx));
583 d5970055 Michael S. Tsirkin
    cpu_physical_memory_unmap(vq->desc, virtio_queue_get_desc_size(vdev, idx),
584 d5970055 Michael S. Tsirkin
                              0, virtio_queue_get_desc_size(vdev, idx));
585 d5970055 Michael S. Tsirkin
}
586 d5970055 Michael S. Tsirkin
587 5430a28f mst@redhat.com
int vhost_dev_init(struct vhost_dev *hdev, int devfd, bool force)
588 d5970055 Michael S. Tsirkin
{
589 d5970055 Michael S. Tsirkin
    uint64_t features;
590 d5970055 Michael S. Tsirkin
    int r;
591 d5970055 Michael S. Tsirkin
    if (devfd >= 0) {
592 d5970055 Michael S. Tsirkin
        hdev->control = devfd;
593 d5970055 Michael S. Tsirkin
    } else {
594 d5970055 Michael S. Tsirkin
        hdev->control = open("/dev/vhost-net", O_RDWR);
595 d5970055 Michael S. Tsirkin
        if (hdev->control < 0) {
596 d5970055 Michael S. Tsirkin
            return -errno;
597 d5970055 Michael S. Tsirkin
        }
598 d5970055 Michael S. Tsirkin
    }
599 d5970055 Michael S. Tsirkin
    r = ioctl(hdev->control, VHOST_SET_OWNER, NULL);
600 d5970055 Michael S. Tsirkin
    if (r < 0) {
601 d5970055 Michael S. Tsirkin
        goto fail;
602 d5970055 Michael S. Tsirkin
    }
603 d5970055 Michael S. Tsirkin
604 d5970055 Michael S. Tsirkin
    r = ioctl(hdev->control, VHOST_GET_FEATURES, &features);
605 d5970055 Michael S. Tsirkin
    if (r < 0) {
606 d5970055 Michael S. Tsirkin
        goto fail;
607 d5970055 Michael S. Tsirkin
    }
608 d5970055 Michael S. Tsirkin
    hdev->features = features;
609 d5970055 Michael S. Tsirkin
610 d5970055 Michael S. Tsirkin
    hdev->client.set_memory = vhost_client_set_memory;
611 d5970055 Michael S. Tsirkin
    hdev->client.sync_dirty_bitmap = vhost_client_sync_dirty_bitmap;
612 d5970055 Michael S. Tsirkin
    hdev->client.migration_log = vhost_client_migration_log;
613 e5896b12 Anthony PERARD
    hdev->client.log_start = NULL;
614 e5896b12 Anthony PERARD
    hdev->client.log_stop = NULL;
615 d5970055 Michael S. Tsirkin
    hdev->mem = qemu_mallocz(offsetof(struct vhost_memory, regions));
616 d5970055 Michael S. Tsirkin
    hdev->log = NULL;
617 d5970055 Michael S. Tsirkin
    hdev->log_size = 0;
618 d5970055 Michael S. Tsirkin
    hdev->log_enabled = false;
619 d5970055 Michael S. Tsirkin
    hdev->started = false;
620 d5970055 Michael S. Tsirkin
    cpu_register_phys_memory_client(&hdev->client);
621 5430a28f mst@redhat.com
    hdev->force = force;
622 d5970055 Michael S. Tsirkin
    return 0;
623 d5970055 Michael S. Tsirkin
fail:
624 d5970055 Michael S. Tsirkin
    r = -errno;
625 d5970055 Michael S. Tsirkin
    close(hdev->control);
626 d5970055 Michael S. Tsirkin
    return r;
627 d5970055 Michael S. Tsirkin
}
628 d5970055 Michael S. Tsirkin
629 d5970055 Michael S. Tsirkin
void vhost_dev_cleanup(struct vhost_dev *hdev)
630 d5970055 Michael S. Tsirkin
{
631 d5970055 Michael S. Tsirkin
    cpu_unregister_phys_memory_client(&hdev->client);
632 d5970055 Michael S. Tsirkin
    qemu_free(hdev->mem);
633 d5970055 Michael S. Tsirkin
    close(hdev->control);
634 d5970055 Michael S. Tsirkin
}
635 d5970055 Michael S. Tsirkin
636 5430a28f mst@redhat.com
bool vhost_dev_query(struct vhost_dev *hdev, VirtIODevice *vdev)
637 5430a28f mst@redhat.com
{
638 5430a28f mst@redhat.com
    return !vdev->binding->query_guest_notifiers ||
639 5430a28f mst@redhat.com
        vdev->binding->query_guest_notifiers(vdev->binding_opaque) ||
640 5430a28f mst@redhat.com
        hdev->force;
641 5430a28f mst@redhat.com
}
642 5430a28f mst@redhat.com
643 d5970055 Michael S. Tsirkin
int vhost_dev_start(struct vhost_dev *hdev, VirtIODevice *vdev)
644 d5970055 Michael S. Tsirkin
{
645 d5970055 Michael S. Tsirkin
    int i, r;
646 54dd9321 Michael S. Tsirkin
    if (!vdev->binding->set_guest_notifiers) {
647 54dd9321 Michael S. Tsirkin
        fprintf(stderr, "binding does not support guest notifiers\n");
648 54dd9321 Michael S. Tsirkin
        r = -ENOSYS;
649 54dd9321 Michael S. Tsirkin
        goto fail;
650 54dd9321 Michael S. Tsirkin
    }
651 54dd9321 Michael S. Tsirkin
652 54dd9321 Michael S. Tsirkin
    r = vdev->binding->set_guest_notifiers(vdev->binding_opaque, true);
653 54dd9321 Michael S. Tsirkin
    if (r < 0) {
654 54dd9321 Michael S. Tsirkin
        fprintf(stderr, "Error binding guest notifier: %d\n", -r);
655 54dd9321 Michael S. Tsirkin
        goto fail_notifiers;
656 54dd9321 Michael S. Tsirkin
    }
657 d5970055 Michael S. Tsirkin
658 d5970055 Michael S. Tsirkin
    r = vhost_dev_set_features(hdev, hdev->log_enabled);
659 d5970055 Michael S. Tsirkin
    if (r < 0) {
660 54dd9321 Michael S. Tsirkin
        goto fail_features;
661 d5970055 Michael S. Tsirkin
    }
662 d5970055 Michael S. Tsirkin
    r = ioctl(hdev->control, VHOST_SET_MEM_TABLE, hdev->mem);
663 d5970055 Michael S. Tsirkin
    if (r < 0) {
664 d5970055 Michael S. Tsirkin
        r = -errno;
665 54dd9321 Michael S. Tsirkin
        goto fail_mem;
666 d5970055 Michael S. Tsirkin
    }
667 d154e0ba Michael S. Tsirkin
    for (i = 0; i < hdev->nvqs; ++i) {
668 d154e0ba Michael S. Tsirkin
        r = vhost_virtqueue_init(hdev,
669 d154e0ba Michael S. Tsirkin
                                 vdev,
670 d154e0ba Michael S. Tsirkin
                                 hdev->vqs + i,
671 d154e0ba Michael S. Tsirkin
                                 i);
672 d154e0ba Michael S. Tsirkin
        if (r < 0) {
673 d154e0ba Michael S. Tsirkin
            goto fail_vq;
674 d154e0ba Michael S. Tsirkin
        }
675 d154e0ba Michael S. Tsirkin
    }
676 d154e0ba Michael S. Tsirkin
677 d5970055 Michael S. Tsirkin
    if (hdev->log_enabled) {
678 d5970055 Michael S. Tsirkin
        hdev->log_size = vhost_get_log_size(hdev);
679 d5970055 Michael S. Tsirkin
        hdev->log = hdev->log_size ?
680 d5970055 Michael S. Tsirkin
            qemu_mallocz(hdev->log_size * sizeof *hdev->log) : NULL;
681 d5970055 Michael S. Tsirkin
        r = ioctl(hdev->control, VHOST_SET_LOG_BASE,
682 d5970055 Michael S. Tsirkin
                  (uint64_t)(unsigned long)hdev->log);
683 d5970055 Michael S. Tsirkin
        if (r < 0) {
684 d5970055 Michael S. Tsirkin
            r = -errno;
685 54dd9321 Michael S. Tsirkin
            goto fail_log;
686 d5970055 Michael S. Tsirkin
        }
687 d5970055 Michael S. Tsirkin
    }
688 d154e0ba Michael S. Tsirkin
689 d5970055 Michael S. Tsirkin
    hdev->started = true;
690 d5970055 Michael S. Tsirkin
691 d5970055 Michael S. Tsirkin
    return 0;
692 54dd9321 Michael S. Tsirkin
fail_log:
693 d5970055 Michael S. Tsirkin
fail_vq:
694 d5970055 Michael S. Tsirkin
    while (--i >= 0) {
695 d5970055 Michael S. Tsirkin
        vhost_virtqueue_cleanup(hdev,
696 d5970055 Michael S. Tsirkin
                                vdev,
697 d5970055 Michael S. Tsirkin
                                hdev->vqs + i,
698 d5970055 Michael S. Tsirkin
                                i);
699 d5970055 Michael S. Tsirkin
    }
700 54dd9321 Michael S. Tsirkin
fail_mem:
701 54dd9321 Michael S. Tsirkin
fail_features:
702 54dd9321 Michael S. Tsirkin
    vdev->binding->set_guest_notifiers(vdev->binding_opaque, false);
703 54dd9321 Michael S. Tsirkin
fail_notifiers:
704 d5970055 Michael S. Tsirkin
fail:
705 d5970055 Michael S. Tsirkin
    return r;
706 d5970055 Michael S. Tsirkin
}
707 d5970055 Michael S. Tsirkin
708 d5970055 Michael S. Tsirkin
void vhost_dev_stop(struct vhost_dev *hdev, VirtIODevice *vdev)
709 d5970055 Michael S. Tsirkin
{
710 54dd9321 Michael S. Tsirkin
    int i, r;
711 54dd9321 Michael S. Tsirkin
712 d5970055 Michael S. Tsirkin
    for (i = 0; i < hdev->nvqs; ++i) {
713 d5970055 Michael S. Tsirkin
        vhost_virtqueue_cleanup(hdev,
714 d5970055 Michael S. Tsirkin
                                vdev,
715 d5970055 Michael S. Tsirkin
                                hdev->vqs + i,
716 d5970055 Michael S. Tsirkin
                                i);
717 d5970055 Michael S. Tsirkin
    }
718 d5970055 Michael S. Tsirkin
    vhost_client_sync_dirty_bitmap(&hdev->client, 0,
719 d5970055 Michael S. Tsirkin
                                   (target_phys_addr_t)~0x0ull);
720 54dd9321 Michael S. Tsirkin
    r = vdev->binding->set_guest_notifiers(vdev->binding_opaque, false);
721 54dd9321 Michael S. Tsirkin
    if (r < 0) {
722 54dd9321 Michael S. Tsirkin
        fprintf(stderr, "vhost guest notifier cleanup failed: %d\n", r);
723 54dd9321 Michael S. Tsirkin
        fflush(stderr);
724 54dd9321 Michael S. Tsirkin
    }
725 54dd9321 Michael S. Tsirkin
    assert (r >= 0);
726 54dd9321 Michael S. Tsirkin
727 d5970055 Michael S. Tsirkin
    hdev->started = false;
728 d5970055 Michael S. Tsirkin
    qemu_free(hdev->log);
729 d5970055 Michael S. Tsirkin
    hdev->log_size = 0;
730 d5970055 Michael S. Tsirkin
}