xen: fix compilation on 32-bit hosts
[qemu.git] / hw / i386 / xen / xen-mapcache.c
blobbb1078c681eee66c6666f61490dbbfb53340fcb6
1 /*
2 * Copyright (C) 2011 Citrix Ltd.
4 * This work is licensed under the terms of the GNU GPL, version 2. See
5 * the COPYING file in the top-level directory.
7 * Contributions after 2012-01-13 are licensed under the terms of the
8 * GNU GPL, version 2 or (at your option) any later version.
9 */
11 #include "qemu/osdep.h"
13 #include <sys/resource.h>
15 #include "hw/xen/xen_backend.h"
16 #include "sysemu/blockdev.h"
17 #include "qemu/bitmap.h"
19 #include <xen/hvm/params.h>
21 #include "sysemu/xen-mapcache.h"
22 #include "trace.h"
25 //#define MAPCACHE_DEBUG
27 #ifdef MAPCACHE_DEBUG
28 # define DPRINTF(fmt, ...) do { \
29 fprintf(stderr, "xen_mapcache: " fmt, ## __VA_ARGS__); \
30 } while (0)
31 #else
32 # define DPRINTF(fmt, ...) do { } while (0)
33 #endif
35 #if HOST_LONG_BITS == 32
36 # define MCACHE_BUCKET_SHIFT 16
37 # define MCACHE_MAX_SIZE (1UL<<31) /* 2GB Cap */
38 #else
39 # define MCACHE_BUCKET_SHIFT 20
40 # define MCACHE_MAX_SIZE (1UL<<35) /* 32GB Cap */
41 #endif
42 #define MCACHE_BUCKET_SIZE (1UL << MCACHE_BUCKET_SHIFT)
44 /* This is the size of the virtual address space reserve to QEMU that will not
45 * be use by MapCache.
46 * From empirical tests I observed that qemu use 75MB more than the
47 * max_mcache_size.
49 #define NON_MCACHE_MEMORY_SIZE (80 * 1024 * 1024)
51 typedef struct MapCacheEntry {
52 hwaddr paddr_index;
53 uint8_t *vaddr_base;
54 unsigned long *valid_mapping;
55 uint8_t lock;
56 #define XEN_MAPCACHE_ENTRY_DUMMY (1 << 0)
57 uint8_t flags;
58 hwaddr size;
59 struct MapCacheEntry *next;
60 } MapCacheEntry;
62 typedef struct MapCacheRev {
63 uint8_t *vaddr_req;
64 hwaddr paddr_index;
65 hwaddr size;
66 QTAILQ_ENTRY(MapCacheRev) next;
67 bool dma;
68 } MapCacheRev;
70 typedef struct MapCache {
71 MapCacheEntry *entry;
72 unsigned long nr_buckets;
73 QTAILQ_HEAD(map_cache_head, MapCacheRev) locked_entries;
75 /* For most cases (>99.9%), the page address is the same. */
76 MapCacheEntry *last_entry;
77 unsigned long max_mcache_size;
78 unsigned int mcache_bucket_shift;
80 phys_offset_to_gaddr_t phys_offset_to_gaddr;
81 QemuMutex lock;
82 void *opaque;
83 } MapCache;
85 static MapCache *mapcache;
87 static inline void mapcache_lock(void)
89 qemu_mutex_lock(&mapcache->lock);
92 static inline void mapcache_unlock(void)
94 qemu_mutex_unlock(&mapcache->lock);
97 static inline int test_bits(int nr, int size, const unsigned long *addr)
99 unsigned long res = find_next_zero_bit(addr, size + nr, nr);
100 if (res >= nr + size)
101 return 1;
102 else
103 return 0;
106 void xen_map_cache_init(phys_offset_to_gaddr_t f, void *opaque)
108 unsigned long size;
109 struct rlimit rlimit_as;
111 mapcache = g_malloc0(sizeof (MapCache));
113 mapcache->phys_offset_to_gaddr = f;
114 mapcache->opaque = opaque;
115 qemu_mutex_init(&mapcache->lock);
117 QTAILQ_INIT(&mapcache->locked_entries);
119 if (geteuid() == 0) {
120 rlimit_as.rlim_cur = RLIM_INFINITY;
121 rlimit_as.rlim_max = RLIM_INFINITY;
122 mapcache->max_mcache_size = MCACHE_MAX_SIZE;
123 } else {
124 getrlimit(RLIMIT_AS, &rlimit_as);
125 rlimit_as.rlim_cur = rlimit_as.rlim_max;
127 if (rlimit_as.rlim_max != RLIM_INFINITY) {
128 fprintf(stderr, "Warning: QEMU's maximum size of virtual"
129 " memory is not infinity.\n");
131 if (rlimit_as.rlim_max < MCACHE_MAX_SIZE + NON_MCACHE_MEMORY_SIZE) {
132 mapcache->max_mcache_size = rlimit_as.rlim_max -
133 NON_MCACHE_MEMORY_SIZE;
134 } else {
135 mapcache->max_mcache_size = MCACHE_MAX_SIZE;
139 setrlimit(RLIMIT_AS, &rlimit_as);
141 mapcache->nr_buckets =
142 (((mapcache->max_mcache_size >> XC_PAGE_SHIFT) +
143 (1UL << (MCACHE_BUCKET_SHIFT - XC_PAGE_SHIFT)) - 1) >>
144 (MCACHE_BUCKET_SHIFT - XC_PAGE_SHIFT));
146 size = mapcache->nr_buckets * sizeof (MapCacheEntry);
147 size = (size + XC_PAGE_SIZE - 1) & ~(XC_PAGE_SIZE - 1);
148 DPRINTF("%s, nr_buckets = %lx size %lu\n", __func__,
149 mapcache->nr_buckets, size);
150 mapcache->entry = g_malloc0(size);
153 static void xen_remap_bucket(MapCacheEntry *entry,
154 void *vaddr,
155 hwaddr size,
156 hwaddr address_index,
157 bool dummy)
159 uint8_t *vaddr_base;
160 xen_pfn_t *pfns;
161 int *err;
162 unsigned int i;
163 hwaddr nb_pfn = size >> XC_PAGE_SHIFT;
165 trace_xen_remap_bucket(address_index);
167 pfns = g_malloc0(nb_pfn * sizeof (xen_pfn_t));
168 err = g_malloc0(nb_pfn * sizeof (int));
170 if (entry->vaddr_base != NULL) {
171 if (!(entry->flags & XEN_MAPCACHE_ENTRY_DUMMY)) {
172 ram_block_notify_remove(entry->vaddr_base, entry->size);
174 if (munmap(entry->vaddr_base, entry->size) != 0) {
175 perror("unmap fails");
176 exit(-1);
179 g_free(entry->valid_mapping);
180 entry->valid_mapping = NULL;
182 for (i = 0; i < nb_pfn; i++) {
183 pfns[i] = (address_index << (MCACHE_BUCKET_SHIFT-XC_PAGE_SHIFT)) + i;
186 if (!dummy) {
187 vaddr_base = xenforeignmemory_map2(xen_fmem, xen_domid, vaddr,
188 PROT_READ | PROT_WRITE, 0,
189 nb_pfn, pfns, err);
190 if (vaddr_base == NULL) {
191 perror("xenforeignmemory_map2");
192 exit(-1);
194 } else {
196 * We create dummy mappings where we are unable to create a foreign
197 * mapping immediately due to certain circumstances (i.e. on resume now)
199 vaddr_base = mmap(vaddr, size, PROT_READ | PROT_WRITE,
200 MAP_ANON | MAP_SHARED, -1, 0);
201 if (vaddr_base == NULL) {
202 perror("mmap");
203 exit(-1);
207 if (!(entry->flags & XEN_MAPCACHE_ENTRY_DUMMY)) {
208 ram_block_notify_add(vaddr_base, size);
211 entry->vaddr_base = vaddr_base;
212 entry->paddr_index = address_index;
213 entry->size = size;
214 entry->valid_mapping = (unsigned long *) g_malloc0(sizeof(unsigned long) *
215 BITS_TO_LONGS(size >> XC_PAGE_SHIFT));
217 if (dummy) {
218 entry->flags |= XEN_MAPCACHE_ENTRY_DUMMY;
219 } else {
220 entry->flags &= ~(XEN_MAPCACHE_ENTRY_DUMMY);
223 bitmap_zero(entry->valid_mapping, nb_pfn);
224 for (i = 0; i < nb_pfn; i++) {
225 if (!err[i]) {
226 bitmap_set(entry->valid_mapping, i, 1);
230 g_free(pfns);
231 g_free(err);
234 static uint8_t *xen_map_cache_unlocked(hwaddr phys_addr, hwaddr size,
235 uint8_t lock, bool dma)
237 MapCacheEntry *entry, *pentry = NULL;
238 hwaddr address_index;
239 hwaddr address_offset;
240 hwaddr cache_size = size;
241 hwaddr test_bit_size;
242 bool translated G_GNUC_UNUSED = false;
243 bool dummy = false;
245 tryagain:
246 address_index = phys_addr >> MCACHE_BUCKET_SHIFT;
247 address_offset = phys_addr & (MCACHE_BUCKET_SIZE - 1);
249 trace_xen_map_cache(phys_addr);
251 /* test_bit_size is always a multiple of XC_PAGE_SIZE */
252 if (size) {
253 test_bit_size = size + (phys_addr & (XC_PAGE_SIZE - 1));
255 if (test_bit_size % XC_PAGE_SIZE) {
256 test_bit_size += XC_PAGE_SIZE - (test_bit_size % XC_PAGE_SIZE);
258 } else {
259 test_bit_size = XC_PAGE_SIZE;
262 if (mapcache->last_entry != NULL &&
263 mapcache->last_entry->paddr_index == address_index &&
264 !lock && !size &&
265 test_bits(address_offset >> XC_PAGE_SHIFT,
266 test_bit_size >> XC_PAGE_SHIFT,
267 mapcache->last_entry->valid_mapping)) {
268 trace_xen_map_cache_return(mapcache->last_entry->vaddr_base + address_offset);
269 return mapcache->last_entry->vaddr_base + address_offset;
272 /* size is always a multiple of MCACHE_BUCKET_SIZE */
273 if (size) {
274 cache_size = size + address_offset;
275 if (cache_size % MCACHE_BUCKET_SIZE) {
276 cache_size += MCACHE_BUCKET_SIZE - (cache_size % MCACHE_BUCKET_SIZE);
278 } else {
279 cache_size = MCACHE_BUCKET_SIZE;
282 entry = &mapcache->entry[address_index % mapcache->nr_buckets];
284 while (entry && entry->lock && entry->vaddr_base &&
285 (entry->paddr_index != address_index || entry->size != cache_size ||
286 !test_bits(address_offset >> XC_PAGE_SHIFT,
287 test_bit_size >> XC_PAGE_SHIFT,
288 entry->valid_mapping))) {
289 pentry = entry;
290 entry = entry->next;
292 if (!entry) {
293 entry = g_malloc0(sizeof (MapCacheEntry));
294 pentry->next = entry;
295 xen_remap_bucket(entry, NULL, cache_size, address_index, dummy);
296 } else if (!entry->lock) {
297 if (!entry->vaddr_base || entry->paddr_index != address_index ||
298 entry->size != cache_size ||
299 !test_bits(address_offset >> XC_PAGE_SHIFT,
300 test_bit_size >> XC_PAGE_SHIFT,
301 entry->valid_mapping)) {
302 xen_remap_bucket(entry, NULL, cache_size, address_index, dummy);
306 if(!test_bits(address_offset >> XC_PAGE_SHIFT,
307 test_bit_size >> XC_PAGE_SHIFT,
308 entry->valid_mapping)) {
309 mapcache->last_entry = NULL;
310 #ifdef XEN_COMPAT_PHYSMAP
311 if (!translated && mapcache->phys_offset_to_gaddr) {
312 phys_addr = mapcache->phys_offset_to_gaddr(phys_addr, size, mapcache->opaque);
313 translated = true;
314 goto tryagain;
316 #endif
317 if (!dummy && runstate_check(RUN_STATE_INMIGRATE)) {
318 dummy = true;
319 goto tryagain;
321 trace_xen_map_cache_return(NULL);
322 return NULL;
325 mapcache->last_entry = entry;
326 if (lock) {
327 MapCacheRev *reventry = g_malloc0(sizeof(MapCacheRev));
328 entry->lock++;
329 reventry->dma = dma;
330 reventry->vaddr_req = mapcache->last_entry->vaddr_base + address_offset;
331 reventry->paddr_index = mapcache->last_entry->paddr_index;
332 reventry->size = entry->size;
333 QTAILQ_INSERT_HEAD(&mapcache->locked_entries, reventry, next);
336 trace_xen_map_cache_return(mapcache->last_entry->vaddr_base + address_offset);
337 return mapcache->last_entry->vaddr_base + address_offset;
340 uint8_t *xen_map_cache(hwaddr phys_addr, hwaddr size,
341 uint8_t lock, bool dma)
343 uint8_t *p;
345 mapcache_lock();
346 p = xen_map_cache_unlocked(phys_addr, size, lock, dma);
347 mapcache_unlock();
348 return p;
351 ram_addr_t xen_ram_addr_from_mapcache(void *ptr)
353 MapCacheEntry *entry = NULL;
354 MapCacheRev *reventry;
355 hwaddr paddr_index;
356 hwaddr size;
357 ram_addr_t raddr;
358 int found = 0;
360 mapcache_lock();
361 QTAILQ_FOREACH(reventry, &mapcache->locked_entries, next) {
362 if (reventry->vaddr_req == ptr) {
363 paddr_index = reventry->paddr_index;
364 size = reventry->size;
365 found = 1;
366 break;
369 if (!found) {
370 fprintf(stderr, "%s, could not find %p\n", __func__, ptr);
371 QTAILQ_FOREACH(reventry, &mapcache->locked_entries, next) {
372 DPRINTF(" "TARGET_FMT_plx" -> %p is present\n", reventry->paddr_index,
373 reventry->vaddr_req);
375 abort();
376 return 0;
379 entry = &mapcache->entry[paddr_index % mapcache->nr_buckets];
380 while (entry && (entry->paddr_index != paddr_index || entry->size != size)) {
381 entry = entry->next;
383 if (!entry) {
384 DPRINTF("Trying to find address %p that is not in the mapcache!\n", ptr);
385 raddr = 0;
386 } else {
387 raddr = (reventry->paddr_index << MCACHE_BUCKET_SHIFT) +
388 ((unsigned long) ptr - (unsigned long) entry->vaddr_base);
390 mapcache_unlock();
391 return raddr;
394 static void xen_invalidate_map_cache_entry_unlocked(uint8_t *buffer)
396 MapCacheEntry *entry = NULL, *pentry = NULL;
397 MapCacheRev *reventry;
398 hwaddr paddr_index;
399 hwaddr size;
400 int found = 0;
402 QTAILQ_FOREACH(reventry, &mapcache->locked_entries, next) {
403 if (reventry->vaddr_req == buffer) {
404 paddr_index = reventry->paddr_index;
405 size = reventry->size;
406 found = 1;
407 break;
410 if (!found) {
411 DPRINTF("%s, could not find %p\n", __func__, buffer);
412 QTAILQ_FOREACH(reventry, &mapcache->locked_entries, next) {
413 DPRINTF(" "TARGET_FMT_plx" -> %p is present\n", reventry->paddr_index, reventry->vaddr_req);
415 return;
417 QTAILQ_REMOVE(&mapcache->locked_entries, reventry, next);
418 g_free(reventry);
420 if (mapcache->last_entry != NULL &&
421 mapcache->last_entry->paddr_index == paddr_index) {
422 mapcache->last_entry = NULL;
425 entry = &mapcache->entry[paddr_index % mapcache->nr_buckets];
426 while (entry && (entry->paddr_index != paddr_index || entry->size != size)) {
427 pentry = entry;
428 entry = entry->next;
430 if (!entry) {
431 DPRINTF("Trying to unmap address %p that is not in the mapcache!\n", buffer);
432 return;
434 entry->lock--;
435 if (entry->lock > 0 || pentry == NULL) {
436 return;
439 pentry->next = entry->next;
440 ram_block_notify_remove(entry->vaddr_base, entry->size);
441 if (munmap(entry->vaddr_base, entry->size) != 0) {
442 perror("unmap fails");
443 exit(-1);
445 g_free(entry->valid_mapping);
446 g_free(entry);
449 void xen_invalidate_map_cache_entry(uint8_t *buffer)
451 mapcache_lock();
452 xen_invalidate_map_cache_entry_unlocked(buffer);
453 mapcache_unlock();
456 void xen_invalidate_map_cache(void)
458 unsigned long i;
459 MapCacheRev *reventry;
461 /* Flush pending AIO before destroying the mapcache */
462 bdrv_drain_all();
464 mapcache_lock();
466 QTAILQ_FOREACH(reventry, &mapcache->locked_entries, next) {
467 if (!reventry->dma) {
468 continue;
470 fprintf(stderr, "Locked DMA mapping while invalidating mapcache!"
471 " "TARGET_FMT_plx" -> %p is present\n",
472 reventry->paddr_index, reventry->vaddr_req);
475 for (i = 0; i < mapcache->nr_buckets; i++) {
476 MapCacheEntry *entry = &mapcache->entry[i];
478 if (entry->vaddr_base == NULL) {
479 continue;
481 if (entry->lock > 0) {
482 continue;
485 if (munmap(entry->vaddr_base, entry->size) != 0) {
486 perror("unmap fails");
487 exit(-1);
490 entry->paddr_index = 0;
491 entry->vaddr_base = NULL;
492 entry->size = 0;
493 g_free(entry->valid_mapping);
494 entry->valid_mapping = NULL;
497 mapcache->last_entry = NULL;
499 mapcache_unlock();
502 static uint8_t *xen_replace_cache_entry_unlocked(hwaddr old_phys_addr,
503 hwaddr new_phys_addr,
504 hwaddr size)
506 MapCacheEntry *entry;
507 hwaddr address_index, address_offset;
508 hwaddr test_bit_size, cache_size = size;
510 address_index = old_phys_addr >> MCACHE_BUCKET_SHIFT;
511 address_offset = old_phys_addr & (MCACHE_BUCKET_SIZE - 1);
513 assert(size);
514 /* test_bit_size is always a multiple of XC_PAGE_SIZE */
515 test_bit_size = size + (old_phys_addr & (XC_PAGE_SIZE - 1));
516 if (test_bit_size % XC_PAGE_SIZE) {
517 test_bit_size += XC_PAGE_SIZE - (test_bit_size % XC_PAGE_SIZE);
519 cache_size = size + address_offset;
520 if (cache_size % MCACHE_BUCKET_SIZE) {
521 cache_size += MCACHE_BUCKET_SIZE - (cache_size % MCACHE_BUCKET_SIZE);
524 entry = &mapcache->entry[address_index % mapcache->nr_buckets];
525 while (entry && !(entry->paddr_index == address_index &&
526 entry->size == cache_size)) {
527 entry = entry->next;
529 if (!entry) {
530 DPRINTF("Trying to update an entry for "TARGET_FMT_plx \
531 "that is not in the mapcache!\n", old_phys_addr);
532 return NULL;
535 address_index = new_phys_addr >> MCACHE_BUCKET_SHIFT;
536 address_offset = new_phys_addr & (MCACHE_BUCKET_SIZE - 1);
538 fprintf(stderr, "Replacing a dummy mapcache entry for "TARGET_FMT_plx \
539 " with "TARGET_FMT_plx"\n", old_phys_addr, new_phys_addr);
541 xen_remap_bucket(entry, entry->vaddr_base,
542 cache_size, address_index, false);
543 if (!test_bits(address_offset >> XC_PAGE_SHIFT,
544 test_bit_size >> XC_PAGE_SHIFT,
545 entry->valid_mapping)) {
546 DPRINTF("Unable to update a mapcache entry for "TARGET_FMT_plx"!\n",
547 old_phys_addr);
548 return NULL;
551 return entry->vaddr_base + address_offset;
554 uint8_t *xen_replace_cache_entry(hwaddr old_phys_addr,
555 hwaddr new_phys_addr,
556 hwaddr size)
558 uint8_t *p;
560 mapcache_lock();
561 p = xen_replace_cache_entry_unlocked(old_phys_addr, new_phys_addr, size);
562 mapcache_unlock();
563 return p;