Add QEMU_NORETURN to function cpu_resume_from_signal
[qemu/rayw.git] / xen-mapcache.c
bloba45647936377d5477894353b9639118a022a3b07
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 "config.h"
13 #include <sys/resource.h>
15 #include "hw/xen_backend.h"
16 #include "blockdev.h"
17 #include "bitmap.h"
19 #include <xen/hvm/params.h>
20 #include <sys/mman.h>
22 #include "xen-mapcache.h"
23 #include "trace.h"
26 //#define MAPCACHE_DEBUG
28 #ifdef MAPCACHE_DEBUG
29 # define DPRINTF(fmt, ...) do { \
30 fprintf(stderr, "xen_mapcache: " fmt, ## __VA_ARGS__); \
31 } while (0)
32 #else
33 # define DPRINTF(fmt, ...) do { } while (0)
34 #endif
36 #if defined(__i386__)
37 # define MCACHE_BUCKET_SHIFT 16
38 # define MCACHE_MAX_SIZE (1UL<<31) /* 2GB Cap */
39 #elif defined(__x86_64__)
40 # define MCACHE_BUCKET_SHIFT 20
41 # define MCACHE_MAX_SIZE (1UL<<35) /* 32GB Cap */
42 #endif
43 #define MCACHE_BUCKET_SIZE (1UL << MCACHE_BUCKET_SHIFT)
45 /* This is the size of the virtual address space reserve to QEMU that will not
46 * be use by MapCache.
47 * From empirical tests I observed that qemu use 75MB more than the
48 * max_mcache_size.
50 #define NON_MCACHE_MEMORY_SIZE (80 * 1024 * 1024)
52 #define mapcache_lock() ((void)0)
53 #define mapcache_unlock() ((void)0)
55 typedef struct MapCacheEntry {
56 target_phys_addr_t paddr_index;
57 uint8_t *vaddr_base;
58 unsigned long *valid_mapping;
59 uint8_t lock;
60 target_phys_addr_t size;
61 struct MapCacheEntry *next;
62 } MapCacheEntry;
64 typedef struct MapCacheRev {
65 uint8_t *vaddr_req;
66 target_phys_addr_t paddr_index;
67 target_phys_addr_t size;
68 QTAILQ_ENTRY(MapCacheRev) next;
69 } MapCacheRev;
71 typedef struct MapCache {
72 MapCacheEntry *entry;
73 unsigned long nr_buckets;
74 QTAILQ_HEAD(map_cache_head, MapCacheRev) locked_entries;
76 /* For most cases (>99.9%), the page address is the same. */
77 target_phys_addr_t last_address_index;
78 uint8_t *last_address_vaddr;
79 unsigned long max_mcache_size;
80 unsigned int mcache_bucket_shift;
82 phys_offset_to_gaddr_t phys_offset_to_gaddr;
83 void *opaque;
84 } MapCache;
86 static MapCache *mapcache;
88 static inline int test_bits(int nr, int size, const unsigned long *addr)
90 unsigned long res = find_next_zero_bit(addr, size + nr, nr);
91 if (res >= nr + size)
92 return 1;
93 else
94 return 0;
97 void xen_map_cache_init(phys_offset_to_gaddr_t f, void *opaque)
99 unsigned long size;
100 struct rlimit rlimit_as;
102 mapcache = g_malloc0(sizeof (MapCache));
104 mapcache->phys_offset_to_gaddr = f;
105 mapcache->opaque = opaque;
107 QTAILQ_INIT(&mapcache->locked_entries);
108 mapcache->last_address_index = -1;
110 if (geteuid() == 0) {
111 rlimit_as.rlim_cur = RLIM_INFINITY;
112 rlimit_as.rlim_max = RLIM_INFINITY;
113 mapcache->max_mcache_size = MCACHE_MAX_SIZE;
114 } else {
115 getrlimit(RLIMIT_AS, &rlimit_as);
116 rlimit_as.rlim_cur = rlimit_as.rlim_max;
118 if (rlimit_as.rlim_max != RLIM_INFINITY) {
119 fprintf(stderr, "Warning: QEMU's maximum size of virtual"
120 " memory is not infinity.\n");
122 if (rlimit_as.rlim_max < MCACHE_MAX_SIZE + NON_MCACHE_MEMORY_SIZE) {
123 mapcache->max_mcache_size = rlimit_as.rlim_max -
124 NON_MCACHE_MEMORY_SIZE;
125 } else {
126 mapcache->max_mcache_size = MCACHE_MAX_SIZE;
130 setrlimit(RLIMIT_AS, &rlimit_as);
132 mapcache->nr_buckets =
133 (((mapcache->max_mcache_size >> XC_PAGE_SHIFT) +
134 (1UL << (MCACHE_BUCKET_SHIFT - XC_PAGE_SHIFT)) - 1) >>
135 (MCACHE_BUCKET_SHIFT - XC_PAGE_SHIFT));
137 size = mapcache->nr_buckets * sizeof (MapCacheEntry);
138 size = (size + XC_PAGE_SIZE - 1) & ~(XC_PAGE_SIZE - 1);
139 DPRINTF("%s, nr_buckets = %lx size %lu\n", __func__,
140 mapcache->nr_buckets, size);
141 mapcache->entry = g_malloc0(size);
144 static void xen_remap_bucket(MapCacheEntry *entry,
145 target_phys_addr_t size,
146 target_phys_addr_t address_index)
148 uint8_t *vaddr_base;
149 xen_pfn_t *pfns;
150 int *err;
151 unsigned int i;
152 target_phys_addr_t nb_pfn = size >> XC_PAGE_SHIFT;
154 trace_xen_remap_bucket(address_index);
156 pfns = g_malloc0(nb_pfn * sizeof (xen_pfn_t));
157 err = g_malloc0(nb_pfn * sizeof (int));
159 if (entry->vaddr_base != NULL) {
160 if (munmap(entry->vaddr_base, entry->size) != 0) {
161 perror("unmap fails");
162 exit(-1);
165 if (entry->valid_mapping != NULL) {
166 g_free(entry->valid_mapping);
167 entry->valid_mapping = NULL;
170 for (i = 0; i < nb_pfn; i++) {
171 pfns[i] = (address_index << (MCACHE_BUCKET_SHIFT-XC_PAGE_SHIFT)) + i;
174 vaddr_base = xc_map_foreign_bulk(xen_xc, xen_domid, PROT_READ|PROT_WRITE,
175 pfns, err, nb_pfn);
176 if (vaddr_base == NULL) {
177 perror("xc_map_foreign_bulk");
178 exit(-1);
181 entry->vaddr_base = vaddr_base;
182 entry->paddr_index = address_index;
183 entry->size = size;
184 entry->valid_mapping = (unsigned long *) g_malloc0(sizeof(unsigned long) *
185 BITS_TO_LONGS(size >> XC_PAGE_SHIFT));
187 bitmap_zero(entry->valid_mapping, nb_pfn);
188 for (i = 0; i < nb_pfn; i++) {
189 if (!err[i]) {
190 bitmap_set(entry->valid_mapping, i, 1);
194 g_free(pfns);
195 g_free(err);
198 uint8_t *xen_map_cache(target_phys_addr_t phys_addr, target_phys_addr_t size,
199 uint8_t lock)
201 MapCacheEntry *entry, *pentry = NULL;
202 target_phys_addr_t address_index;
203 target_phys_addr_t address_offset;
204 target_phys_addr_t __size = size;
205 bool translated = false;
207 tryagain:
208 address_index = phys_addr >> MCACHE_BUCKET_SHIFT;
209 address_offset = phys_addr & (MCACHE_BUCKET_SIZE - 1);
211 trace_xen_map_cache(phys_addr);
213 if (address_index == mapcache->last_address_index && !lock && !__size) {
214 trace_xen_map_cache_return(mapcache->last_address_vaddr + address_offset);
215 return mapcache->last_address_vaddr + address_offset;
218 /* size is always a multiple of MCACHE_BUCKET_SIZE */
219 if ((address_offset + (__size % MCACHE_BUCKET_SIZE)) > MCACHE_BUCKET_SIZE)
220 __size += MCACHE_BUCKET_SIZE;
221 if (__size % MCACHE_BUCKET_SIZE)
222 __size += MCACHE_BUCKET_SIZE - (__size % MCACHE_BUCKET_SIZE);
223 if (!__size)
224 __size = MCACHE_BUCKET_SIZE;
226 entry = &mapcache->entry[address_index % mapcache->nr_buckets];
228 while (entry && entry->lock && entry->vaddr_base &&
229 (entry->paddr_index != address_index || entry->size != __size ||
230 !test_bits(address_offset >> XC_PAGE_SHIFT, size >> XC_PAGE_SHIFT,
231 entry->valid_mapping))) {
232 pentry = entry;
233 entry = entry->next;
235 if (!entry) {
236 entry = g_malloc0(sizeof (MapCacheEntry));
237 pentry->next = entry;
238 xen_remap_bucket(entry, __size, address_index);
239 } else if (!entry->lock) {
240 if (!entry->vaddr_base || entry->paddr_index != address_index ||
241 entry->size != __size ||
242 !test_bits(address_offset >> XC_PAGE_SHIFT, size >> XC_PAGE_SHIFT,
243 entry->valid_mapping)) {
244 xen_remap_bucket(entry, __size, address_index);
248 if(!test_bits(address_offset >> XC_PAGE_SHIFT, size >> XC_PAGE_SHIFT,
249 entry->valid_mapping)) {
250 mapcache->last_address_index = -1;
251 if (!translated && mapcache->phys_offset_to_gaddr) {
252 phys_addr = mapcache->phys_offset_to_gaddr(phys_addr, size, mapcache->opaque);
253 translated = true;
254 goto tryagain;
256 trace_xen_map_cache_return(NULL);
257 return NULL;
260 mapcache->last_address_index = address_index;
261 mapcache->last_address_vaddr = entry->vaddr_base;
262 if (lock) {
263 MapCacheRev *reventry = g_malloc0(sizeof(MapCacheRev));
264 entry->lock++;
265 reventry->vaddr_req = mapcache->last_address_vaddr + address_offset;
266 reventry->paddr_index = mapcache->last_address_index;
267 reventry->size = entry->size;
268 QTAILQ_INSERT_HEAD(&mapcache->locked_entries, reventry, next);
271 trace_xen_map_cache_return(mapcache->last_address_vaddr + address_offset);
272 return mapcache->last_address_vaddr + address_offset;
275 ram_addr_t xen_ram_addr_from_mapcache(void *ptr)
277 MapCacheEntry *entry = NULL;
278 MapCacheRev *reventry;
279 target_phys_addr_t paddr_index;
280 target_phys_addr_t size;
281 int found = 0;
283 QTAILQ_FOREACH(reventry, &mapcache->locked_entries, next) {
284 if (reventry->vaddr_req == ptr) {
285 paddr_index = reventry->paddr_index;
286 size = reventry->size;
287 found = 1;
288 break;
291 if (!found) {
292 fprintf(stderr, "%s, could not find %p\n", __func__, ptr);
293 QTAILQ_FOREACH(reventry, &mapcache->locked_entries, next) {
294 DPRINTF(" "TARGET_FMT_plx" -> %p is present\n", reventry->paddr_index,
295 reventry->vaddr_req);
297 abort();
298 return 0;
301 entry = &mapcache->entry[paddr_index % mapcache->nr_buckets];
302 while (entry && (entry->paddr_index != paddr_index || entry->size != size)) {
303 entry = entry->next;
305 if (!entry) {
306 DPRINTF("Trying to find address %p that is not in the mapcache!\n", ptr);
307 return 0;
309 return (reventry->paddr_index << MCACHE_BUCKET_SHIFT) +
310 ((unsigned long) ptr - (unsigned long) entry->vaddr_base);
313 void xen_invalidate_map_cache_entry(uint8_t *buffer)
315 MapCacheEntry *entry = NULL, *pentry = NULL;
316 MapCacheRev *reventry;
317 target_phys_addr_t paddr_index;
318 target_phys_addr_t size;
319 int found = 0;
321 if (mapcache->last_address_vaddr == buffer) {
322 mapcache->last_address_index = -1;
325 QTAILQ_FOREACH(reventry, &mapcache->locked_entries, next) {
326 if (reventry->vaddr_req == buffer) {
327 paddr_index = reventry->paddr_index;
328 size = reventry->size;
329 found = 1;
330 break;
333 if (!found) {
334 DPRINTF("%s, could not find %p\n", __func__, buffer);
335 QTAILQ_FOREACH(reventry, &mapcache->locked_entries, next) {
336 DPRINTF(" "TARGET_FMT_plx" -> %p is present\n", reventry->paddr_index, reventry->vaddr_req);
338 return;
340 QTAILQ_REMOVE(&mapcache->locked_entries, reventry, next);
341 g_free(reventry);
343 entry = &mapcache->entry[paddr_index % mapcache->nr_buckets];
344 while (entry && (entry->paddr_index != paddr_index || entry->size != size)) {
345 pentry = entry;
346 entry = entry->next;
348 if (!entry) {
349 DPRINTF("Trying to unmap address %p that is not in the mapcache!\n", buffer);
350 return;
352 entry->lock--;
353 if (entry->lock > 0 || pentry == NULL) {
354 return;
357 pentry->next = entry->next;
358 if (munmap(entry->vaddr_base, entry->size) != 0) {
359 perror("unmap fails");
360 exit(-1);
362 g_free(entry->valid_mapping);
363 g_free(entry);
366 void xen_invalidate_map_cache(void)
368 unsigned long i;
369 MapCacheRev *reventry;
371 /* Flush pending AIO before destroying the mapcache */
372 bdrv_drain_all();
374 QTAILQ_FOREACH(reventry, &mapcache->locked_entries, next) {
375 DPRINTF("There should be no locked mappings at this time, "
376 "but "TARGET_FMT_plx" -> %p is present\n",
377 reventry->paddr_index, reventry->vaddr_req);
380 mapcache_lock();
382 for (i = 0; i < mapcache->nr_buckets; i++) {
383 MapCacheEntry *entry = &mapcache->entry[i];
385 if (entry->vaddr_base == NULL) {
386 continue;
389 if (munmap(entry->vaddr_base, entry->size) != 0) {
390 perror("unmap fails");
391 exit(-1);
394 entry->paddr_index = 0;
395 entry->vaddr_base = NULL;
396 entry->size = 0;
397 g_free(entry->valid_mapping);
398 entry->valid_mapping = NULL;
401 mapcache->last_address_index = -1;
402 mapcache->last_address_vaddr = NULL;
404 mapcache_unlock();