1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
|
// SPDX-License-Identifier: GPL-2.0
/*
* Copyright (C) 2025 Ant Group
* Author: Tiwei Bie <tiwei.btw@antgroup.com>
*/
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/eventfd.h>
#include <linux/limits.h>
#include <linux/vfio.h>
#include <linux/pci_regs.h>
#include <as-layout.h>
#include <um_malloc.h>
#include "vfio_user.h"
int uml_vfio_user_open_container(void)
{
int r, fd;
fd = open("/dev/vfio/vfio", O_RDWR);
if (fd < 0)
return -errno;
r = ioctl(fd, VFIO_GET_API_VERSION);
if (r != VFIO_API_VERSION) {
r = r < 0 ? -errno : -EINVAL;
goto error;
}
r = ioctl(fd, VFIO_CHECK_EXTENSION, VFIO_TYPE1_IOMMU);
if (r <= 0) {
r = r < 0 ? -errno : -EINVAL;
goto error;
}
return fd;
error:
close(fd);
return r;
}
int uml_vfio_user_setup_iommu(int container)
{
/*
* This is a bit tricky. See the big comment in
* vhost_user_set_mem_table() in virtio_uml.c.
*/
unsigned long reserved = uml_reserved - uml_physmem;
struct vfio_iommu_type1_dma_map dma_map = {
.argsz = sizeof(dma_map),
.flags = VFIO_DMA_MAP_FLAG_READ | VFIO_DMA_MAP_FLAG_WRITE,
.vaddr = uml_reserved,
.iova = reserved,
.size = physmem_size - reserved,
};
if (ioctl(container, VFIO_SET_IOMMU, VFIO_TYPE1_IOMMU) < 0)
return -errno;
if (ioctl(container, VFIO_IOMMU_MAP_DMA, &dma_map) < 0)
return -errno;
return 0;
}
int uml_vfio_user_get_group_id(const char *device)
{
char *path, *buf, *end;
const char *name;
int r;
path = uml_kmalloc(PATH_MAX, UM_GFP_KERNEL);
if (!path)
return -ENOMEM;
sprintf(path, "/sys/bus/pci/devices/%s/iommu_group", device);
buf = uml_kmalloc(PATH_MAX + 1, UM_GFP_KERNEL);
if (!buf) {
r = -ENOMEM;
goto free_path;
}
r = readlink(path, buf, PATH_MAX);
if (r < 0) {
r = -errno;
goto free_buf;
}
buf[r] = '\0';
name = basename(buf);
r = strtoul(name, &end, 10);
if (*end != '\0' || end == name) {
r = -EINVAL;
goto free_buf;
}
free_buf:
kfree(buf);
free_path:
kfree(path);
return r;
}
int uml_vfio_user_open_group(int group_id)
{
char *path;
int fd;
path = uml_kmalloc(PATH_MAX, UM_GFP_KERNEL);
if (!path)
return -ENOMEM;
sprintf(path, "/dev/vfio/%d", group_id);
fd = open(path, O_RDWR);
if (fd < 0) {
fd = -errno;
goto out;
}
out:
kfree(path);
return fd;
}
int uml_vfio_user_set_container(int container, int group)
{
if (ioctl(group, VFIO_GROUP_SET_CONTAINER, &container) < 0)
return -errno;
return 0;
}
int uml_vfio_user_unset_container(int container, int group)
{
if (ioctl(group, VFIO_GROUP_UNSET_CONTAINER, &container) < 0)
return -errno;
return 0;
}
static int vfio_set_irqs(int device, int start, int count, int *irqfd)
{
struct vfio_irq_set *irq_set;
int argsz = sizeof(*irq_set) + sizeof(*irqfd) * count;
int err = 0;
irq_set = uml_kmalloc(argsz, UM_GFP_KERNEL);
if (!irq_set)
return -ENOMEM;
irq_set->argsz = argsz;
irq_set->flags = VFIO_IRQ_SET_DATA_EVENTFD | VFIO_IRQ_SET_ACTION_TRIGGER;
irq_set->index = VFIO_PCI_MSIX_IRQ_INDEX;
irq_set->start = start;
irq_set->count = count;
memcpy(irq_set->data, irqfd, sizeof(*irqfd) * count);
if (ioctl(device, VFIO_DEVICE_SET_IRQS, irq_set) < 0) {
err = -errno;
goto out;
}
out:
kfree(irq_set);
return err;
}
int uml_vfio_user_setup_device(struct uml_vfio_user_device *dev,
int group, const char *device)
{
struct vfio_device_info device_info = { .argsz = sizeof(device_info) };
struct vfio_irq_info irq_info = { .argsz = sizeof(irq_info) };
int err, i;
dev->device = ioctl(group, VFIO_GROUP_GET_DEVICE_FD, device);
if (dev->device < 0)
return -errno;
if (ioctl(dev->device, VFIO_DEVICE_GET_INFO, &device_info) < 0) {
err = -errno;
goto close_device;
}
dev->num_regions = device_info.num_regions;
if (dev->num_regions > VFIO_PCI_CONFIG_REGION_INDEX + 1)
dev->num_regions = VFIO_PCI_CONFIG_REGION_INDEX + 1;
dev->region = uml_kmalloc(sizeof(*dev->region) * dev->num_regions,
UM_GFP_KERNEL);
if (!dev->region) {
err = -ENOMEM;
goto close_device;
}
for (i = 0; i < dev->num_regions; i++) {
struct vfio_region_info region = {
.argsz = sizeof(region),
.index = i,
};
if (ioctl(dev->device, VFIO_DEVICE_GET_REGION_INFO, ®ion) < 0) {
err = -errno;
goto free_region;
}
dev->region[i].size = region.size;
dev->region[i].offset = region.offset;
}
/* Only MSI-X is supported currently. */
irq_info.index = VFIO_PCI_MSIX_IRQ_INDEX;
if (ioctl(dev->device, VFIO_DEVICE_GET_IRQ_INFO, &irq_info) < 0) {
err = -errno;
goto free_region;
}
dev->irq_count = irq_info.count;
dev->irqfd = uml_kmalloc(sizeof(int) * dev->irq_count, UM_GFP_KERNEL);
if (!dev->irqfd) {
err = -ENOMEM;
goto free_region;
}
memset(dev->irqfd, -1, sizeof(int) * dev->irq_count);
err = vfio_set_irqs(dev->device, 0, dev->irq_count, dev->irqfd);
if (err)
goto free_irqfd;
return 0;
free_irqfd:
kfree(dev->irqfd);
free_region:
kfree(dev->region);
close_device:
close(dev->device);
return err;
}
void uml_vfio_user_teardown_device(struct uml_vfio_user_device *dev)
{
kfree(dev->irqfd);
kfree(dev->region);
close(dev->device);
}
int uml_vfio_user_activate_irq(struct uml_vfio_user_device *dev, int index)
{
int irqfd;
irqfd = eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC);
if (irqfd < 0)
return -errno;
dev->irqfd[index] = irqfd;
return irqfd;
}
void uml_vfio_user_deactivate_irq(struct uml_vfio_user_device *dev, int index)
{
close(dev->irqfd[index]);
dev->irqfd[index] = -1;
}
int uml_vfio_user_update_irqs(struct uml_vfio_user_device *dev)
{
return vfio_set_irqs(dev->device, 0, dev->irq_count, dev->irqfd);
}
static int vfio_region_read(struct uml_vfio_user_device *dev, unsigned int index,
uint64_t offset, void *buf, uint64_t size)
{
if (index >= dev->num_regions || offset + size > dev->region[index].size)
return -EINVAL;
if (pread(dev->device, buf, size, dev->region[index].offset + offset) < 0)
return -errno;
return 0;
}
static int vfio_region_write(struct uml_vfio_user_device *dev, unsigned int index,
uint64_t offset, const void *buf, uint64_t size)
{
if (index >= dev->num_regions || offset + size > dev->region[index].size)
return -EINVAL;
if (pwrite(dev->device, buf, size, dev->region[index].offset + offset) < 0)
return -errno;
return 0;
}
int uml_vfio_user_cfgspace_read(struct uml_vfio_user_device *dev,
unsigned int offset, void *buf, int size)
{
return vfio_region_read(dev, VFIO_PCI_CONFIG_REGION_INDEX,
offset, buf, size);
}
int uml_vfio_user_cfgspace_write(struct uml_vfio_user_device *dev,
unsigned int offset, const void *buf, int size)
{
return vfio_region_write(dev, VFIO_PCI_CONFIG_REGION_INDEX,
offset, buf, size);
}
int uml_vfio_user_bar_read(struct uml_vfio_user_device *dev, int bar,
unsigned int offset, void *buf, int size)
{
return vfio_region_read(dev, bar, offset, buf, size);
}
int uml_vfio_user_bar_write(struct uml_vfio_user_device *dev, int bar,
unsigned int offset, const void *buf, int size)
{
return vfio_region_write(dev, bar, offset, buf, size);
}
|