Coverage Report

Created: 2026-11-20 08:13

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/qemu/hw/vfio-user/container.c
Line
Count
Source
1
/*
2
 * Container for vfio-user IOMMU type: rather than communicating with the kernel
3
 * vfio driver, we communicate over a socket to a server using the vfio-user
4
 * protocol.
5
 *
6
 * SPDX-License-Identifier: GPL-2.0-or-later
7
 */
8
9
#include "qemu/osdep.h"
10
#include <sys/ioctl.h>
11
#include <linux/vfio.h>
12
13
#include "hw/vfio-user/container.h"
14
#include "hw/vfio-user/device.h"
15
#include "hw/vfio-user/trace.h"
16
#include "hw/vfio/vfio-device.h"
17
#include "hw/vfio/vfio-listener.h"
18
#include "system/ramblock.h"
19
#include "qapi/error.h"
20
21
/*
22
 * When DMA space is the physical address space, the region add/del listeners
23
 * will fire during memory update transactions.  These depend on BQL being held,
24
 * so do any resulting map/demap ops async while keeping BQL.
25
 */
26
static void vfio_user_listener_begin(VFIOContainer *bcontainer)
27
0
{
28
0
    VFIOUserContainer *container = VFIO_IOMMU_USER(bcontainer);
29
30
0
    container->proxy->async_ops = true;
31
0
}
32
33
static void vfio_user_listener_commit(VFIOContainer *bcontainer)
34
0
{
35
0
    VFIOUserContainer *container = VFIO_IOMMU_USER(bcontainer);
36
37
    /* wait here for any async requests sent during the transaction */
38
0
    container->proxy->async_ops = false;
39
0
    vfio_user_wait_reqs(container->proxy);
40
0
}
41
42
static int vfio_user_dma_unmap(const VFIOContainer *bcontainer,
43
                               hwaddr iova, uint64_t size,
44
                               IOMMUTLBEntry *iotlb, bool unmap_all)
45
0
{
46
0
    VFIOUserContainer *container = VFIO_IOMMU_USER(bcontainer);
47
48
0
    Error *local_err = NULL;
49
0
    int ret = 0;
50
51
0
    VFIOUserDMAUnmap *msgp = g_malloc(sizeof(*msgp));
52
53
0
    vfio_user_request_msg(&msgp->hdr, VFIO_USER_DMA_UNMAP, sizeof(*msgp), 0);
54
0
    msgp->argsz = sizeof(struct vfio_iommu_type1_dma_unmap);
55
0
    msgp->flags = unmap_all ? VFIO_DMA_UNMAP_FLAG_ALL : 0;
56
0
    msgp->iova = iova;
57
0
    msgp->size = size;
58
0
    trace_vfio_user_dma_unmap(msgp->iova, msgp->size, msgp->flags,
59
0
                              container->proxy->async_ops);
60
61
0
    if (container->proxy->async_ops) {
62
0
        if (!vfio_user_send_nowait(container->proxy, &msgp->hdr, NULL,
63
0
                              0, &local_err)) {
64
0
            error_report_err(local_err);
65
0
            ret = -EFAULT;
66
0
        }
67
0
    } else {
68
0
        if (!vfio_user_send_wait(container->proxy, &msgp->hdr, NULL,
69
0
                                 0, &local_err)) {
70
0
                error_report_err(local_err);
71
0
                ret = -EFAULT;
72
0
        }
73
74
0
        if (msgp->hdr.flags & VFIO_USER_ERROR) {
75
0
            ret = -msgp->hdr.error_reply;
76
0
        }
77
78
0
        g_free(msgp);
79
0
    }
80
81
0
    return ret;
82
0
}
83
84
static int vfio_user_dma_map(const VFIOContainer *bcontainer, hwaddr iova,
85
                             uint64_t size, void *vaddr, bool readonly,
86
                             MemoryRegion *mrp)
87
0
{
88
0
    VFIOUserContainer *container = VFIO_IOMMU_USER(bcontainer);
89
90
0
    int fd = memory_region_get_fd(mrp);
91
0
    Error *local_err = NULL;
92
0
    int ret = 0;
93
94
0
    VFIOUserFDs *fds = NULL;
95
0
    VFIOUserDMAMap *msgp = g_malloc0(sizeof(*msgp));
96
97
0
    vfio_user_request_msg(&msgp->hdr, VFIO_USER_DMA_MAP, sizeof(*msgp), 0);
98
0
    msgp->argsz = sizeof(struct vfio_iommu_type1_dma_map);
99
0
    msgp->flags = VFIO_DMA_MAP_FLAG_READ;
100
0
    msgp->offset = 0;
101
0
    msgp->iova = iova;
102
0
    msgp->size = size;
103
104
    /*
105
     * vaddr enters as a QEMU process address; make it either a file offset
106
     * for mapped areas or leave as 0.
107
     */
108
0
    if (fd != -1) {
109
0
        msgp->offset = qemu_ram_block_host_offset(mrp->ram_block, vaddr);
110
0
    }
111
112
0
    if (!readonly) {
113
0
        msgp->flags |= VFIO_DMA_MAP_FLAG_WRITE;
114
0
    }
115
116
0
    trace_vfio_user_dma_map(msgp->iova, msgp->size, msgp->offset, msgp->flags,
117
0
                            container->proxy->async_ops);
118
119
    /*
120
     * The async_ops case sends without blocking. They're later waited for in
121
     * vfio_send_wait_reqs.
122
     */
123
0
    if (container->proxy->async_ops) {
124
        /* can't use auto variable since we don't block */
125
0
        if (fd != -1) {
126
0
            fds = vfio_user_getfds(1);
127
0
            fds->send_fds = 1;
128
0
            fds->fds[0] = fd;
129
0
        }
130
131
0
        if (!vfio_user_send_nowait(container->proxy, &msgp->hdr, fds,
132
0
                              0, &local_err)) {
133
0
            error_report_err(local_err);
134
0
            ret = -EFAULT;
135
0
        }
136
0
    } else {
137
0
        VFIOUserFDs local_fds = { 1, 0, &fd };
138
139
0
        fds = fd != -1 ? &local_fds : NULL;
140
141
0
        if (!vfio_user_send_wait(container->proxy, &msgp->hdr, fds,
142
0
                                 0, &local_err)) {
143
0
                error_report_err(local_err);
144
0
                ret = -EFAULT;
145
0
        }
146
147
0
        if (msgp->hdr.flags & VFIO_USER_ERROR) {
148
0
            ret = -msgp->hdr.error_reply;
149
0
        }
150
151
0
        g_free(msgp);
152
0
    }
153
154
0
    return ret;
155
0
}
156
157
static int
158
vfio_user_set_dirty_page_tracking(const VFIOContainer *bcontainer,
159
                                    bool start, Error **errp)
160
0
{
161
0
    error_setg_errno(errp, ENOTSUP, "Not supported");
162
0
    return -ENOTSUP;
163
0
}
164
165
static int vfio_user_query_dirty_bitmap(const VFIOContainer *bcontainer,
166
                                        VFIOBitmap *vbmap, hwaddr iova,
167
                                        hwaddr size, uint64_t backend_flag,
168
                                        Error **errp)
169
0
{
170
0
    error_setg_errno(errp, ENOTSUP, "Not supported");
171
0
    return -ENOTSUP;
172
0
}
173
174
static bool vfio_user_setup(VFIOContainer *bcontainer, Error **errp)
175
0
{
176
0
    VFIOUserContainer *container = VFIO_IOMMU_USER(bcontainer);
177
178
0
    assert(container->proxy->dma_pgsizes != 0);
179
0
    bcontainer->pgsizes = container->proxy->dma_pgsizes;
180
0
    bcontainer->dma_max_mappings = container->proxy->max_dma;
181
182
    /* No live migration support yet. */
183
0
    bcontainer->dirty_pages_supported = false;
184
0
    bcontainer->max_dirty_bitmap_size = container->proxy->max_bitmap;
185
0
    bcontainer->dirty_pgsizes = container->proxy->migr_pgsize;
186
187
0
    return true;
188
0
}
189
190
static VFIOUserContainer *vfio_user_create_container(VFIODevice *vbasedev,
191
                                                     Error **errp)
192
0
{
193
0
    VFIOUserContainer *container;
194
195
0
    container = VFIO_IOMMU_USER(object_new(TYPE_VFIO_IOMMU_USER));
196
0
    container->proxy = vbasedev->proxy;
197
0
    return container;
198
0
}
199
200
/*
201
 * Try to mirror vfio_container_connect() as much as possible.
202
 */
203
static VFIOUserContainer *
204
vfio_user_container_connect(AddressSpace *as, VFIODevice *vbasedev,
205
                            Error **errp)
206
0
{
207
0
    VFIOContainer *bcontainer;
208
0
    VFIOUserContainer *container;
209
0
    VFIOAddressSpace *space;
210
0
    VFIOIOMMUClass *vioc;
211
0
    int ret;
212
213
0
    space = vfio_address_space_get(as);
214
215
0
    container = vfio_user_create_container(vbasedev, errp);
216
0
    if (!container) {
217
0
        goto put_space_exit;
218
0
    }
219
220
0
    bcontainer = VFIO_IOMMU(container);
221
222
0
    ret = ram_block_uncoordinated_discard_disable(true);
223
0
    if (ret) {
224
0
        error_setg_errno(errp, -ret, "Cannot set discarding of RAM broken");
225
0
        goto free_container_exit;
226
0
    }
227
228
0
    vioc = VFIO_IOMMU_GET_CLASS(bcontainer);
229
0
    assert(vioc->setup);
230
231
0
    if (!vioc->setup(bcontainer, errp)) {
232
0
        goto enable_discards_exit;
233
0
    }
234
235
0
    vfio_address_space_insert(space, bcontainer);
236
237
0
    if (!vfio_listener_register(bcontainer, errp)) {
238
0
        goto listener_release_exit;
239
0
    }
240
241
0
    bcontainer->initialized = true;
242
243
0
    return container;
244
245
0
listener_release_exit:
246
0
    vfio_listener_unregister(bcontainer);
247
0
    if (vioc->release) {
248
0
        vioc->release(bcontainer);
249
0
    }
250
251
0
enable_discards_exit:
252
0
    ram_block_uncoordinated_discard_disable(false);
253
254
0
free_container_exit:
255
0
    object_unref(container);
256
257
0
put_space_exit:
258
0
    vfio_address_space_put(space);
259
260
0
    return NULL;
261
0
}
262
263
static void vfio_user_container_disconnect(VFIOUserContainer *container)
264
0
{
265
0
    VFIOContainer *bcontainer = VFIO_IOMMU(container);
266
0
    VFIOIOMMUClass *vioc = VFIO_IOMMU_GET_CLASS(bcontainer);
267
0
    VFIOAddressSpace *space = bcontainer->space;
268
269
0
    ram_block_uncoordinated_discard_disable(false);
270
271
0
    vfio_listener_unregister(bcontainer);
272
0
    if (vioc->release) {
273
0
        vioc->release(bcontainer);
274
0
    }
275
276
0
    object_unref(container);
277
278
0
    vfio_address_space_put(space);
279
0
}
280
281
static bool vfio_user_device_get(VFIOUserContainer *container,
282
                                 VFIODevice *vbasedev, Error **errp)
283
0
{
284
0
    struct vfio_device_info info = { .argsz = sizeof(info) };
285
286
287
0
    if (!vfio_user_get_device_info(vbasedev->proxy, &info, errp)) {
288
0
        return false;
289
0
    }
290
291
0
    vbasedev->fd = -1;
292
293
0
    vfio_device_prepare(vbasedev, VFIO_IOMMU(container), &info);
294
295
0
    return true;
296
0
}
297
298
/*
299
 * vfio_user_device_attach: attach a device to a new container.
300
 */
301
static bool vfio_user_device_attach(const char *name, VFIODevice *vbasedev,
302
                                    AddressSpace *as, Error **errp)
303
0
{
304
0
    VFIOUserContainer *container;
305
306
0
    container = vfio_user_container_connect(as, vbasedev, errp);
307
0
    if (container == NULL) {
308
0
        error_prepend(errp, "failed to connect proxy");
309
0
        return false;
310
0
    }
311
312
0
    if (!vfio_user_device_get(container, vbasedev, errp)) {
313
0
        vfio_user_container_disconnect(container);
314
0
        return false;
315
0
    }
316
317
0
    return true;
318
0
}
319
320
static void vfio_user_device_detach(VFIODevice *vbasedev)
321
0
{
322
0
    VFIOUserContainer *container = VFIO_IOMMU_USER(vbasedev->bcontainer);
323
324
0
    vfio_device_unprepare(vbasedev);
325
326
0
    vfio_user_container_disconnect(container);
327
0
}
328
329
static int vfio_user_pci_hot_reset(VFIODevice *vbasedev, bool single)
330
0
{
331
    /* ->needs_reset is always false for vfio-user. */
332
0
    return 0;
333
0
}
334
335
static void vfio_iommu_user_class_init(ObjectClass *klass, const void *data)
336
63
{
337
63
    VFIOIOMMUClass *vioc = VFIO_IOMMU_CLASS(klass);
338
339
63
    vioc->setup = vfio_user_setup;
340
63
    vioc->listener_begin = vfio_user_listener_begin,
341
63
    vioc->listener_commit = vfio_user_listener_commit,
342
63
    vioc->dma_map = vfio_user_dma_map;
343
63
    vioc->dma_unmap = vfio_user_dma_unmap;
344
63
    vioc->attach_device = vfio_user_device_attach;
345
63
    vioc->detach_device = vfio_user_device_detach;
346
63
    vioc->set_dirty_page_tracking = vfio_user_set_dirty_page_tracking;
347
63
    vioc->query_dirty_bitmap = vfio_user_query_dirty_bitmap;
348
63
    vioc->pci_hot_reset = vfio_user_pci_hot_reset;
349
63
};
350
351
static const TypeInfo types[] = {
352
    {
353
        .name = TYPE_VFIO_IOMMU_USER,
354
        .parent = TYPE_VFIO_IOMMU,
355
        .instance_size = sizeof(VFIOUserContainer),
356
        .class_init = vfio_iommu_user_class_init,
357
    },
358
};
359
360
DEFINE_TYPES(types)