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
|
// SPDX-License-Identifier: GPL-2.0-only
#include <stdio.h>
#include <sys/mman.h>
#include <unistd.h>
#include <uapi/linux/types.h>
#include <linux/iommufd.h>
#include <linux/limits.h>
#include <linux/mman.h>
#include <linux/sizes.h>
#include <linux/vfio.h>
#include <vfio_util.h>
#include "../kselftest_harness.h"
static const char *device_bdf;
struct iommu_mapping {
u64 pgd;
u64 p4d;
u64 pud;
u64 pmd;
u64 pte;
};
static void parse_next_value(char **line, u64 *value)
{
char *token;
token = strtok_r(*line, " \t|\n", line);
if (!token)
return;
/* Caller verifies `value`. No need to check return value. */
sscanf(token, "0x%lx", value);
}
static int intel_iommu_mapping_get(const char *bdf, u64 iova,
struct iommu_mapping *mapping)
{
char iommu_mapping_path[PATH_MAX], line[PATH_MAX];
u64 line_iova = -1;
int ret = -ENOENT;
FILE *file;
char *rest;
snprintf(iommu_mapping_path, sizeof(iommu_mapping_path),
"/sys/kernel/debug/iommu/intel/%s/domain_translation_struct",
bdf);
printf("Searching for IOVA 0x%lx in %s\n", iova, iommu_mapping_path);
file = fopen(iommu_mapping_path, "r");
VFIO_ASSERT_NOT_NULL(file, "fopen(%s) failed", iommu_mapping_path);
while (fgets(line, sizeof(line), file)) {
rest = line;
parse_next_value(&rest, &line_iova);
if (line_iova != (iova / getpagesize()))
continue;
/*
* Ensure each struct field is initialized in case of empty
* page table values.
*/
memset(mapping, 0, sizeof(*mapping));
parse_next_value(&rest, &mapping->pgd);
parse_next_value(&rest, &mapping->p4d);
parse_next_value(&rest, &mapping->pud);
parse_next_value(&rest, &mapping->pmd);
parse_next_value(&rest, &mapping->pte);
ret = 0;
break;
}
fclose(file);
if (ret)
printf("IOVA not found\n");
return ret;
}
static int iommu_mapping_get(const char *bdf, u64 iova,
struct iommu_mapping *mapping)
{
if (!access("/sys/kernel/debug/iommu/intel", F_OK))
return intel_iommu_mapping_get(bdf, iova, mapping);
return -EOPNOTSUPP;
}
FIXTURE(vfio_dma_mapping_test) {
struct vfio_pci_device *device;
struct iova_allocator *iova_allocator;
};
FIXTURE_VARIANT(vfio_dma_mapping_test) {
const char *iommu_mode;
u64 size;
int mmap_flags;
};
#define FIXTURE_VARIANT_ADD_IOMMU_MODE(_iommu_mode, _name, _size, _mmap_flags) \
FIXTURE_VARIANT_ADD(vfio_dma_mapping_test, _iommu_mode ## _ ## _name) { \
.iommu_mode = #_iommu_mode, \
.size = (_size), \
.mmap_flags = MAP_ANONYMOUS | MAP_PRIVATE | (_mmap_flags), \
}
FIXTURE_VARIANT_ADD_ALL_IOMMU_MODES(anonymous, 0, 0);
FIXTURE_VARIANT_ADD_ALL_IOMMU_MODES(anonymous_hugetlb_2mb, SZ_2M, MAP_HUGETLB | MAP_HUGE_2MB);
FIXTURE_VARIANT_ADD_ALL_IOMMU_MODES(anonymous_hugetlb_1gb, SZ_1G, MAP_HUGETLB | MAP_HUGE_1GB);
#undef FIXTURE_VARIANT_ADD_IOMMU_MODE
FIXTURE_SETUP(vfio_dma_mapping_test)
{
self->device = vfio_pci_device_init(device_bdf, variant->iommu_mode);
self->iova_allocator = iova_allocator_init(self->device);
}
FIXTURE_TEARDOWN(vfio_dma_mapping_test)
{
iova_allocator_cleanup(self->iova_allocator);
vfio_pci_device_cleanup(self->device);
}
TEST_F(vfio_dma_mapping_test, dma_map_unmap)
{
const u64 size = variant->size ?: getpagesize();
const int flags = variant->mmap_flags;
struct vfio_dma_region region;
struct iommu_mapping mapping;
u64 mapping_size = size;
u64 unmapped;
int rc;
region.vaddr = mmap(NULL, size, PROT_READ | PROT_WRITE, flags, -1, 0);
/* Skip the test if there aren't enough HugeTLB pages available. */
if (flags & MAP_HUGETLB && region.vaddr == MAP_FAILED)
SKIP(return, "mmap() failed: %s (%d)\n", strerror(errno), errno);
else
ASSERT_NE(region.vaddr, MAP_FAILED);
region.iova = iova_allocator_alloc(self->iova_allocator, size);
region.size = size;
vfio_pci_dma_map(self->device, ®ion);
printf("Mapped HVA %p (size 0x%lx) at IOVA 0x%lx\n", region.vaddr, size, region.iova);
ASSERT_EQ(region.iova, to_iova(self->device, region.vaddr));
rc = iommu_mapping_get(device_bdf, region.iova, &mapping);
if (rc == -EOPNOTSUPP)
goto unmap;
/*
* IOMMUFD compatibility-mode does not support huge mappings when
* using VFIO_TYPE1_IOMMU.
*/
if (!strcmp(variant->iommu_mode, "iommufd_compat_type1"))
mapping_size = SZ_4K;
ASSERT_EQ(0, rc);
printf("Found IOMMU mappings for IOVA 0x%lx:\n", region.iova);
printf("PGD: 0x%016lx\n", mapping.pgd);
printf("P4D: 0x%016lx\n", mapping.p4d);
printf("PUD: 0x%016lx\n", mapping.pud);
printf("PMD: 0x%016lx\n", mapping.pmd);
printf("PTE: 0x%016lx\n", mapping.pte);
switch (mapping_size) {
case SZ_4K:
ASSERT_NE(0, mapping.pte);
break;
case SZ_2M:
ASSERT_EQ(0, mapping.pte);
ASSERT_NE(0, mapping.pmd);
break;
case SZ_1G:
ASSERT_EQ(0, mapping.pte);
ASSERT_EQ(0, mapping.pmd);
ASSERT_NE(0, mapping.pud);
break;
default:
VFIO_FAIL("Unrecognized size: 0x%lx\n", mapping_size);
}
unmap:
rc = __vfio_pci_dma_unmap(self->device, ®ion, &unmapped);
ASSERT_EQ(rc, 0);
ASSERT_EQ(unmapped, region.size);
printf("Unmapped IOVA 0x%lx\n", region.iova);
ASSERT_EQ(INVALID_IOVA, __to_iova(self->device, region.vaddr));
ASSERT_NE(0, iommu_mapping_get(device_bdf, region.iova, &mapping));
ASSERT_TRUE(!munmap(region.vaddr, size));
}
FIXTURE(vfio_dma_map_limit_test) {
struct vfio_pci_device *device;
struct vfio_dma_region region;
size_t mmap_size;
};
FIXTURE_VARIANT(vfio_dma_map_limit_test) {
const char *iommu_mode;
};
#define FIXTURE_VARIANT_ADD_IOMMU_MODE(_iommu_mode) \
FIXTURE_VARIANT_ADD(vfio_dma_map_limit_test, _iommu_mode) { \
.iommu_mode = #_iommu_mode, \
}
FIXTURE_VARIANT_ADD_ALL_IOMMU_MODES();
#undef FIXTURE_VARIANT_ADD_IOMMU_MODE
FIXTURE_SETUP(vfio_dma_map_limit_test)
{
struct vfio_dma_region *region = &self->region;
struct iommu_iova_range *ranges;
u64 region_size = getpagesize();
iova_t last_iova;
u32 nranges;
/*
* Over-allocate mmap by double the size to provide enough backing vaddr
* for overflow tests
*/
self->mmap_size = 2 * region_size;
self->device = vfio_pci_device_init(device_bdf, variant->iommu_mode);
region->vaddr = mmap(NULL, self->mmap_size, PROT_READ | PROT_WRITE,
MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
ASSERT_NE(region->vaddr, MAP_FAILED);
ranges = vfio_pci_iova_ranges(self->device, &nranges);
VFIO_ASSERT_NOT_NULL(ranges);
last_iova = ranges[nranges - 1].last;
free(ranges);
/* One page prior to the last iova */
region->iova = last_iova & ~(region_size - 1);
region->size = region_size;
}
FIXTURE_TEARDOWN(vfio_dma_map_limit_test)
{
vfio_pci_device_cleanup(self->device);
ASSERT_EQ(munmap(self->region.vaddr, self->mmap_size), 0);
}
TEST_F(vfio_dma_map_limit_test, unmap_range)
{
struct vfio_dma_region *region = &self->region;
u64 unmapped;
int rc;
vfio_pci_dma_map(self->device, region);
ASSERT_EQ(region->iova, to_iova(self->device, region->vaddr));
rc = __vfio_pci_dma_unmap(self->device, region, &unmapped);
ASSERT_EQ(rc, 0);
ASSERT_EQ(unmapped, region->size);
}
TEST_F(vfio_dma_map_limit_test, unmap_all)
{
struct vfio_dma_region *region = &self->region;
u64 unmapped;
int rc;
vfio_pci_dma_map(self->device, region);
ASSERT_EQ(region->iova, to_iova(self->device, region->vaddr));
rc = __vfio_pci_dma_unmap_all(self->device, &unmapped);
ASSERT_EQ(rc, 0);
ASSERT_EQ(unmapped, region->size);
}
TEST_F(vfio_dma_map_limit_test, overflow)
{
struct vfio_dma_region *region = &self->region;
int rc;
region->iova = ~(iova_t)0 & ~(region->size - 1);
region->size = self->mmap_size;
rc = __vfio_pci_dma_map(self->device, region);
ASSERT_EQ(rc, -EOVERFLOW);
rc = __vfio_pci_dma_unmap(self->device, region, NULL);
ASSERT_EQ(rc, -EOVERFLOW);
}
int main(int argc, char *argv[])
{
device_bdf = vfio_selftests_get_bdf(&argc, argv);
return test_harness_run(argc, argv);
}
|