-
Notifications
You must be signed in to change notification settings - Fork 4
/
memmap.c
460 lines (381 loc) · 12 KB
/
memmap.c
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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
#include <string.h>
#include <inttypes.h>
#include <sys/stat.h>
#include "queue.h"
#include "memmap.h"
#include "platform.h"
#include "logging.h"
#include "objref.h"
struct vhd_mmap_callbacks {
/* gets called after mapping guest memory region */
int (*map_cb)(void *addr, size_t len);
/* gets called before unmapping guest memory region */
int (*unmap_cb)(void *addr, size_t len);
};
struct vhd_memory_region {
struct objref ref;
/* start of the region in guest physical space */
uint64_t gpa;
/* start of the region in master's virtual space */
uint64_t uva;
/* start of the region in this process' virtual space */
void *ptr;
/* region size */
size_t size;
/* offset of the region from the file base */
off_t offset;
/* unique identifiers of this region for caching purposes */
dev_t device;
ino_t inode;
/* callbacks associated with this memory region */
struct vhd_mmap_callbacks callbacks;
LIST_ENTRY(vhd_memory_region) region_link;
};
static LIST_HEAD(, vhd_memory_region) g_regions =
LIST_HEAD_INITIALIZER(g_regions);
static void region_init_id(struct vhd_memory_region *reg, int fd)
{
struct stat stat;
if (fstat(fd, &stat) < 0) {
return;
}
reg->device = stat.st_dev;
reg->inode = stat.st_ino;
}
/*
* This should be no less than VHOST_USER_MEM_REGIONS_MAX, to accept any
* allowed VHOST_USER_SET_MEM_TABLE message. The master may use more via
* VHOST_USER_ADD_MEM_REG message if VHOST_USER_PROTOCOL_F_CONFIGURE_MEM_SLOTS
* is negotiated.
*/
#define VHD_RAM_SLOTS_MAX 32
size_t vhd_memmap_max_memslots(void)
{
return VHD_RAM_SLOTS_MAX;
}
struct vhd_memory_map {
struct objref ref;
struct vhd_mmap_callbacks callbacks;
/* actual number of slots used */
unsigned num;
struct vhd_memory_region *regions[VHD_RAM_SLOTS_MAX];
};
/*
* Returns actual pointer where uva points to
* or NULL in case of mapping absence
*/
void *uva_to_ptr(struct vhd_memory_map *mm, uint64_t uva)
{
unsigned i;
for (i = 0; i < mm->num; i++) {
struct vhd_memory_region *reg = mm->regions[i];
if (uva >= reg->uva && uva - reg->uva < reg->size) {
return reg->ptr + (uva - reg->uva);
}
}
return NULL;
}
static void *map_memory(void *addr, size_t len, int fd, off_t offset)
{
size_t aligned_len, map_len;
/*
* Some apps map memory in very small chunks, make sure it's at least the
* size of a page so that remap doesn't fail later on.
*/
len = VHD_ALIGN_UP(len, PAGE_SIZE);
aligned_len = VHD_ALIGN_PTR_UP(len, HUGE_PAGE_SIZE);
map_len = aligned_len + HUGE_PAGE_SIZE + PAGE_SIZE;
char *map = mmap(addr, map_len, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1,
0);
if (map == MAP_FAILED) {
VHD_LOG_ERROR("unable to map memory: %s", strerror(errno));
return MAP_FAILED;
}
char *aligned_addr = VHD_ALIGN_PTR_UP(map + PAGE_SIZE, HUGE_PAGE_SIZE);
addr = mmap(aligned_addr, len, PROT_READ | PROT_WRITE,
MAP_SHARED | MAP_FIXED, fd, offset);
if (addr == MAP_FAILED) {
VHD_LOG_ERROR("unable to remap memory region %p-%p: %s", aligned_addr,
aligned_addr + len, strerror(errno));
munmap(map, map_len);
return MAP_FAILED;
}
aligned_addr = addr;
size_t tail_len = aligned_len - len;
if (tail_len) {
char *tail = aligned_addr + len;
addr = mmap(tail, tail_len, PROT_READ | PROT_WRITE,
MAP_SHARED | MAP_ANONYMOUS | MAP_FIXED, -1, 0);
if (addr == MAP_FAILED) {
VHD_LOG_ERROR("unable to remap memory region %p-%p: %s", tail,
tail + tail_len, strerror(errno));
munmap(map, map_len);
return MAP_FAILED;
}
}
char *start = aligned_addr - PAGE_SIZE;
char *end = aligned_addr + aligned_len + PAGE_SIZE;
munmap(map, start - map);
munmap(end, map + map_len - end);
return aligned_addr;
}
static int unmap_memory(void *addr, size_t len)
{
size_t map_len = VHD_ALIGN_PTR_UP(len, HUGE_PAGE_SIZE) + PAGE_SIZE * 2;
char *map = addr - PAGE_SIZE;
return munmap(map, map_len);
}
static int map_region(struct vhd_memory_region *region, uint64_t gpa,
uint64_t uva, size_t size, int fd, off_t offset)
{
void *ptr;
ptr = map_memory(NULL, size, fd, offset);
if (ptr == MAP_FAILED) {
int ret = -errno;
VHD_LOG_ERROR("can't mmap memory: %s", strerror(-ret));
return ret;
}
if (region->callbacks.map_cb) {
size_t len = VHD_ALIGN_PTR_UP(size, HUGE_PAGE_SIZE);
int ret = region->callbacks.map_cb(ptr, len);
if (ret < 0) {
VHD_LOG_ERROR("map callback failed for region %p-%p: %s",
ptr, ptr + len, strerror(-ret));
munmap(ptr, size);
return ret;
}
}
/* Mark memory as defined explicitly */
VHD_MEMCHECK_DEFINED(ptr, size);
region->ptr = ptr;
region->gpa = gpa;
region->uva = uva;
region->size = size;
region->offset = offset;
region_init_id(region, fd);
return 0;
}
static int unmap_region(struct vhd_memory_region *reg)
{
int ret;
if (reg->callbacks.unmap_cb) {
size_t len = VHD_ALIGN_PTR_UP(reg->size, HUGE_PAGE_SIZE);
ret = reg->callbacks.unmap_cb(reg->ptr, len);
if (ret < 0) {
VHD_LOG_ERROR("unmap callback failed for region %p-%p: %s",
reg->ptr, reg->ptr + reg->size, strerror(-ret));
return ret;
}
}
ret = unmap_memory(reg->ptr, reg->size);
if (ret < 0) {
VHD_LOG_ERROR("failed to unmap region at %p", reg->ptr);
return ret;
}
return 0;
}
static void region_release(struct objref *objref)
{
struct vhd_memory_region *reg =
containerof(objref, struct vhd_memory_region, ref);
LIST_REMOVE(reg, region_link);
unmap_region(reg);
vhd_free(reg);
}
static void region_ref(struct vhd_memory_region *reg)
{
objref_get(®->ref);
}
static void region_unref(struct vhd_memory_region *reg)
{
objref_put(®->ref);
}
static inline struct vhd_memory_region *region_get_cached(
uint64_t gpa, uint64_t uva,
size_t size, int fd,
off_t offset,
struct vhd_mmap_callbacks *callbacks
)
{
struct vhd_memory_region *region;
struct stat stat;
if (fstat(fd, &stat) < 0) {
return NULL;
}
LIST_FOREACH(region, &g_regions, region_link) {
if (region->inode != stat.st_ino || region->device != stat.st_dev) {
continue;
}
if (region->gpa != gpa || region->uva != uva ||
region->size != size || region->offset != offset) {
continue;
}
if (region->callbacks.map_cb != callbacks->map_cb ||
region->callbacks.unmap_cb != callbacks->unmap_cb) {
continue;
}
region_ref(region);
return region;
}
return NULL;
}
static void memmap_release(struct objref *objref)
{
struct vhd_memory_map *mm =
containerof(objref, struct vhd_memory_map, ref);
unsigned i;
for (i = 0; i < mm->num; i++) {
region_unref(mm->regions[i]);
}
vhd_free(mm);
}
void vhd_memmap_ref(struct vhd_memory_map *mm) __attribute__ ((weak));
void vhd_memmap_ref(struct vhd_memory_map *mm)
{
objref_get(&mm->ref);
}
void vhd_memmap_unref(struct vhd_memory_map *mm) __attribute__ ((weak));
void vhd_memmap_unref(struct vhd_memory_map *mm)
{
objref_put(&mm->ref);
}
uint64_t ptr_to_gpa(struct vhd_memory_map *mm, void *ptr)
{
unsigned i;
for (i = 0; i < mm->num; ++i) {
struct vhd_memory_region *reg = mm->regions[i];
if (ptr >= reg->ptr && ptr < reg->ptr + reg->size) {
return (ptr - reg->ptr) + reg->gpa;
}
}
VHD_LOG_WARN("Failed to translate ptr %p to gpa", ptr);
return TRANSLATION_FAILED;
}
void *gpa_range_to_ptr(struct vhd_memory_map *mm,
uint64_t gpa, size_t len) __attribute__ ((weak));
void *gpa_range_to_ptr(struct vhd_memory_map *mm, uint64_t gpa, size_t len)
{
unsigned i;
for (i = 0; i < mm->num; i++) {
struct vhd_memory_region *reg = mm->regions[i];
if (gpa >= reg->gpa && gpa - reg->gpa < reg->size) {
/*
* Check (overflow-safe) that length fits in a single region.
*
* TODO: should we handle gpa areas that cross region boundaries
* but are otherwise valid?
*/
if (len > reg->size || gpa - reg->gpa + len > reg->size) {
return NULL;
}
return reg->ptr + (gpa - reg->gpa);
}
}
return NULL;
}
struct vhd_memory_map *vhd_memmap_new(int (*map_cb)(void *, size_t),
int (*unmap_cb)(void *, size_t))
{
struct vhd_memory_map *mm = vhd_alloc(sizeof(*mm));
*mm = (struct vhd_memory_map) {
.callbacks = (struct vhd_mmap_callbacks) {
.map_cb = map_cb,
.unmap_cb = unmap_cb,
}
};
objref_init(&mm->ref, memmap_release);
return mm;
}
struct vhd_memory_map *vhd_memmap_dup(struct vhd_memory_map *mm)
{
size_t i;
struct vhd_memory_map *new_mm = vhd_alloc(sizeof(*mm));
new_mm->callbacks = mm->callbacks;
new_mm->num = mm->num;
objref_init(&new_mm->ref, memmap_release);
for (i = 0; i < mm->num; i++) {
struct vhd_memory_region *reg = mm->regions[i];
region_ref(reg);
new_mm->regions[i] = reg;
}
return new_mm;
}
int vhd_memmap_add_slot(struct vhd_memory_map *mm, uint64_t gpa, uint64_t uva,
size_t size, int fd, off_t offset)
{
int ret;
unsigned i;
struct vhd_memory_region *region;
/* check for overflow */
if (gpa + size < gpa || uva + size < uva) {
return -EINVAL;
}
/* check for spare slots */
if (mm->num == VHD_RAM_SLOTS_MAX) {
return -ENOBUFS;
}
/* check for intersection with existing slots */
for (i = 0; i < mm->num; i++) {
struct vhd_memory_region *reg = mm->regions[i];
if (reg->gpa + reg->size <= gpa || gpa + size <= reg->gpa ||
reg->uva + reg->size <= uva || uva + size <= reg->uva) {
continue;
}
return -EINVAL;
}
/* find appropriate position to keep ascending order in gpa */
for (i = mm->num; i > 0; i--) {
struct vhd_memory_region *reg = mm->regions[i - 1];
if (reg->gpa < gpa) {
break;
}
}
region = region_get_cached(gpa, uva, size, fd, offset, &mm->callbacks);
if (region == NULL) {
region = vhd_calloc(1, sizeof(*region));
*region = (struct vhd_memory_region) {
.callbacks = mm->callbacks,
};
objref_init(®ion->ref, region_release);
ret = map_region(region, gpa, uva, size, fd, offset);
if (ret < 0) {
vhd_free(region);
return ret;
}
LIST_INSERT_HEAD(&g_regions, region, region_link);
} else {
VHD_LOG_INFO(
"region %jd-%ju (GPA 0x%016"PRIX64" -> 0x%016"PRIX64") cache hit, "
"reusing (%u refs total)", region->device, region->inode,
region->gpa, region->gpa + region->size, objref_read(®ion->ref)
);
}
if (i < mm->num) {
memmove(&mm->regions[i + 1], &mm->regions[i],
sizeof(mm->regions[0]) * (mm->num - i));
}
mm->regions[i] = region;
mm->num++;
return 0;
}
int vhd_memmap_del_slot(struct vhd_memory_map *mm, uint64_t gpa, uint64_t uva,
size_t size)
{
unsigned i;
for (i = 0; i < mm->num; i++) {
struct vhd_memory_region *reg = mm->regions[i];
if (reg->gpa == gpa && reg->uva == uva && reg->size == size) {
break;
}
}
if (i == mm->num) {
return -ENXIO;
}
region_unref(mm->regions[i]);
mm->num--;
if (i < mm->num) {
memmove(&mm->regions[i], &mm->regions[i + 1],
sizeof(mm->regions[0]) * (mm->num - i));
}
return 0;
}