]> git.proxmox.com Git - mirror_qemu.git/blob - xen-mapcache.c
target/arm: Fix aa64 ldp register writeback
[mirror_qemu.git] / xen-mapcache.c
1 /*
2 * Copyright (C) 2011 Citrix Ltd.
3 *
4 * This work is licensed under the terms of the GNU GPL, version 2. See
5 * the COPYING file in the top-level directory.
6 *
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 */
10
11 #include "qemu/osdep.h"
12
13 #include <sys/resource.h>
14
15 #include "hw/xen/xen_backend.h"
16 #include "sysemu/blockdev.h"
17 #include "qemu/bitmap.h"
18
19 #include <xen/hvm/params.h>
20
21 #include "sysemu/xen-mapcache.h"
22 #include "trace-root.h"
23
24
25 //#define MAPCACHE_DEBUG
26
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
34
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)
43
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.
48 */
49 #define NON_MCACHE_MEMORY_SIZE (80 * 1024 * 1024)
50
51 typedef struct MapCacheEntry {
52 hwaddr paddr_index;
53 uint8_t *vaddr_base;
54 unsigned long *valid_mapping;
55 uint8_t lock;
56 hwaddr size;
57 struct MapCacheEntry *next;
58 } MapCacheEntry;
59
60 typedef struct MapCacheRev {
61 uint8_t *vaddr_req;
62 hwaddr paddr_index;
63 hwaddr size;
64 QTAILQ_ENTRY(MapCacheRev) next;
65 bool dma;
66 } MapCacheRev;
67
68 typedef struct MapCache {
69 MapCacheEntry *entry;
70 unsigned long nr_buckets;
71 QTAILQ_HEAD(map_cache_head, MapCacheRev) locked_entries;
72
73 /* For most cases (>99.9%), the page address is the same. */
74 MapCacheEntry *last_entry;
75 unsigned long max_mcache_size;
76 unsigned int mcache_bucket_shift;
77
78 phys_offset_to_gaddr_t phys_offset_to_gaddr;
79 QemuMutex lock;
80 void *opaque;
81 } MapCache;
82
83 static MapCache *mapcache;
84
85 static inline void mapcache_lock(void)
86 {
87 qemu_mutex_lock(&mapcache->lock);
88 }
89
90 static inline void mapcache_unlock(void)
91 {
92 qemu_mutex_unlock(&mapcache->lock);
93 }
94
95 static inline int test_bits(int nr, int size, const unsigned long *addr)
96 {
97 unsigned long res = find_next_zero_bit(addr, size + nr, nr);
98 if (res >= nr + size)
99 return 1;
100 else
101 return 0;
102 }
103
104 void xen_map_cache_init(phys_offset_to_gaddr_t f, void *opaque)
105 {
106 unsigned long size;
107 struct rlimit rlimit_as;
108
109 mapcache = g_malloc0(sizeof (MapCache));
110
111 mapcache->phys_offset_to_gaddr = f;
112 mapcache->opaque = opaque;
113 qemu_mutex_init(&mapcache->lock);
114
115 QTAILQ_INIT(&mapcache->locked_entries);
116
117 if (geteuid() == 0) {
118 rlimit_as.rlim_cur = RLIM_INFINITY;
119 rlimit_as.rlim_max = RLIM_INFINITY;
120 mapcache->max_mcache_size = MCACHE_MAX_SIZE;
121 } else {
122 getrlimit(RLIMIT_AS, &rlimit_as);
123 rlimit_as.rlim_cur = rlimit_as.rlim_max;
124
125 if (rlimit_as.rlim_max != RLIM_INFINITY) {
126 fprintf(stderr, "Warning: QEMU's maximum size of virtual"
127 " memory is not infinity.\n");
128 }
129 if (rlimit_as.rlim_max < MCACHE_MAX_SIZE + NON_MCACHE_MEMORY_SIZE) {
130 mapcache->max_mcache_size = rlimit_as.rlim_max -
131 NON_MCACHE_MEMORY_SIZE;
132 } else {
133 mapcache->max_mcache_size = MCACHE_MAX_SIZE;
134 }
135 }
136
137 setrlimit(RLIMIT_AS, &rlimit_as);
138
139 mapcache->nr_buckets =
140 (((mapcache->max_mcache_size >> XC_PAGE_SHIFT) +
141 (1UL << (MCACHE_BUCKET_SHIFT - XC_PAGE_SHIFT)) - 1) >>
142 (MCACHE_BUCKET_SHIFT - XC_PAGE_SHIFT));
143
144 size = mapcache->nr_buckets * sizeof (MapCacheEntry);
145 size = (size + XC_PAGE_SIZE - 1) & ~(XC_PAGE_SIZE - 1);
146 DPRINTF("%s, nr_buckets = %lx size %lu\n", __func__,
147 mapcache->nr_buckets, size);
148 mapcache->entry = g_malloc0(size);
149 }
150
151 static void xen_remap_bucket(MapCacheEntry *entry,
152 hwaddr size,
153 hwaddr address_index)
154 {
155 uint8_t *vaddr_base;
156 xen_pfn_t *pfns;
157 int *err;
158 unsigned int i;
159 hwaddr nb_pfn = size >> XC_PAGE_SHIFT;
160
161 trace_xen_remap_bucket(address_index);
162
163 pfns = g_malloc0(nb_pfn * sizeof (xen_pfn_t));
164 err = g_malloc0(nb_pfn * sizeof (int));
165
166 if (entry->vaddr_base != NULL) {
167 ram_block_notify_remove(entry->vaddr_base, entry->size);
168 if (munmap(entry->vaddr_base, entry->size) != 0) {
169 perror("unmap fails");
170 exit(-1);
171 }
172 }
173 g_free(entry->valid_mapping);
174 entry->valid_mapping = NULL;
175
176 for (i = 0; i < nb_pfn; i++) {
177 pfns[i] = (address_index << (MCACHE_BUCKET_SHIFT-XC_PAGE_SHIFT)) + i;
178 }
179
180 vaddr_base = xenforeignmemory_map(xen_fmem, xen_domid, PROT_READ|PROT_WRITE,
181 nb_pfn, pfns, err);
182 if (vaddr_base == NULL) {
183 perror("xenforeignmemory_map");
184 exit(-1);
185 }
186
187 entry->vaddr_base = vaddr_base;
188 entry->paddr_index = address_index;
189 entry->size = size;
190 entry->valid_mapping = (unsigned long *) g_malloc0(sizeof(unsigned long) *
191 BITS_TO_LONGS(size >> XC_PAGE_SHIFT));
192
193 ram_block_notify_add(entry->vaddr_base, entry->size);
194 bitmap_zero(entry->valid_mapping, nb_pfn);
195 for (i = 0; i < nb_pfn; i++) {
196 if (!err[i]) {
197 bitmap_set(entry->valid_mapping, i, 1);
198 }
199 }
200
201 g_free(pfns);
202 g_free(err);
203 }
204
205 static uint8_t *xen_map_cache_unlocked(hwaddr phys_addr, hwaddr size,
206 uint8_t lock, bool dma)
207 {
208 MapCacheEntry *entry, *pentry = NULL;
209 hwaddr address_index;
210 hwaddr address_offset;
211 hwaddr cache_size = size;
212 hwaddr test_bit_size;
213 bool translated = false;
214
215 tryagain:
216 address_index = phys_addr >> MCACHE_BUCKET_SHIFT;
217 address_offset = phys_addr & (MCACHE_BUCKET_SIZE - 1);
218
219 trace_xen_map_cache(phys_addr);
220
221 /* test_bit_size is always a multiple of XC_PAGE_SIZE */
222 if (size) {
223 test_bit_size = size + (phys_addr & (XC_PAGE_SIZE - 1));
224
225 if (test_bit_size % XC_PAGE_SIZE) {
226 test_bit_size += XC_PAGE_SIZE - (test_bit_size % XC_PAGE_SIZE);
227 }
228 } else {
229 test_bit_size = XC_PAGE_SIZE;
230 }
231
232 if (mapcache->last_entry != NULL &&
233 mapcache->last_entry->paddr_index == address_index &&
234 !lock && !size &&
235 test_bits(address_offset >> XC_PAGE_SHIFT,
236 test_bit_size >> XC_PAGE_SHIFT,
237 mapcache->last_entry->valid_mapping)) {
238 trace_xen_map_cache_return(mapcache->last_entry->vaddr_base + address_offset);
239 return mapcache->last_entry->vaddr_base + address_offset;
240 }
241
242 /* size is always a multiple of MCACHE_BUCKET_SIZE */
243 if (size) {
244 cache_size = size + address_offset;
245 if (cache_size % MCACHE_BUCKET_SIZE) {
246 cache_size += MCACHE_BUCKET_SIZE - (cache_size % MCACHE_BUCKET_SIZE);
247 }
248 } else {
249 cache_size = MCACHE_BUCKET_SIZE;
250 }
251
252 entry = &mapcache->entry[address_index % mapcache->nr_buckets];
253
254 while (entry && entry->lock && entry->vaddr_base &&
255 (entry->paddr_index != address_index || entry->size != cache_size ||
256 !test_bits(address_offset >> XC_PAGE_SHIFT,
257 test_bit_size >> XC_PAGE_SHIFT,
258 entry->valid_mapping))) {
259 pentry = entry;
260 entry = entry->next;
261 }
262 if (!entry) {
263 entry = g_malloc0(sizeof (MapCacheEntry));
264 pentry->next = entry;
265 xen_remap_bucket(entry, cache_size, address_index);
266 } else if (!entry->lock) {
267 if (!entry->vaddr_base || entry->paddr_index != address_index ||
268 entry->size != cache_size ||
269 !test_bits(address_offset >> XC_PAGE_SHIFT,
270 test_bit_size >> XC_PAGE_SHIFT,
271 entry->valid_mapping)) {
272 xen_remap_bucket(entry, cache_size, address_index);
273 }
274 }
275
276 if(!test_bits(address_offset >> XC_PAGE_SHIFT,
277 test_bit_size >> XC_PAGE_SHIFT,
278 entry->valid_mapping)) {
279 mapcache->last_entry = NULL;
280 if (!translated && mapcache->phys_offset_to_gaddr) {
281 phys_addr = mapcache->phys_offset_to_gaddr(phys_addr, size, mapcache->opaque);
282 translated = true;
283 goto tryagain;
284 }
285 trace_xen_map_cache_return(NULL);
286 return NULL;
287 }
288
289 mapcache->last_entry = entry;
290 if (lock) {
291 MapCacheRev *reventry = g_malloc0(sizeof(MapCacheRev));
292 entry->lock++;
293 reventry->dma = dma;
294 reventry->vaddr_req = mapcache->last_entry->vaddr_base + address_offset;
295 reventry->paddr_index = mapcache->last_entry->paddr_index;
296 reventry->size = entry->size;
297 QTAILQ_INSERT_HEAD(&mapcache->locked_entries, reventry, next);
298 }
299
300 trace_xen_map_cache_return(mapcache->last_entry->vaddr_base + address_offset);
301 return mapcache->last_entry->vaddr_base + address_offset;
302 }
303
304 uint8_t *xen_map_cache(hwaddr phys_addr, hwaddr size,
305 uint8_t lock, bool dma)
306 {
307 uint8_t *p;
308
309 mapcache_lock();
310 p = xen_map_cache_unlocked(phys_addr, size, lock, dma);
311 mapcache_unlock();
312 return p;
313 }
314
315 ram_addr_t xen_ram_addr_from_mapcache(void *ptr)
316 {
317 MapCacheEntry *entry = NULL;
318 MapCacheRev *reventry;
319 hwaddr paddr_index;
320 hwaddr size;
321 ram_addr_t raddr;
322 int found = 0;
323
324 mapcache_lock();
325 QTAILQ_FOREACH(reventry, &mapcache->locked_entries, next) {
326 if (reventry->vaddr_req == ptr) {
327 paddr_index = reventry->paddr_index;
328 size = reventry->size;
329 found = 1;
330 break;
331 }
332 }
333 if (!found) {
334 fprintf(stderr, "%s, could not find %p\n", __func__, ptr);
335 QTAILQ_FOREACH(reventry, &mapcache->locked_entries, next) {
336 DPRINTF(" "TARGET_FMT_plx" -> %p is present\n", reventry->paddr_index,
337 reventry->vaddr_req);
338 }
339 abort();
340 return 0;
341 }
342
343 entry = &mapcache->entry[paddr_index % mapcache->nr_buckets];
344 while (entry && (entry->paddr_index != paddr_index || entry->size != size)) {
345 entry = entry->next;
346 }
347 if (!entry) {
348 DPRINTF("Trying to find address %p that is not in the mapcache!\n", ptr);
349 raddr = 0;
350 } else {
351 raddr = (reventry->paddr_index << MCACHE_BUCKET_SHIFT) +
352 ((unsigned long) ptr - (unsigned long) entry->vaddr_base);
353 }
354 mapcache_unlock();
355 return raddr;
356 }
357
358 static void xen_invalidate_map_cache_entry_unlocked(uint8_t *buffer)
359 {
360 MapCacheEntry *entry = NULL, *pentry = NULL;
361 MapCacheRev *reventry;
362 hwaddr paddr_index;
363 hwaddr size;
364 int found = 0;
365
366 QTAILQ_FOREACH(reventry, &mapcache->locked_entries, next) {
367 if (reventry->vaddr_req == buffer) {
368 paddr_index = reventry->paddr_index;
369 size = reventry->size;
370 found = 1;
371 break;
372 }
373 }
374 if (!found) {
375 DPRINTF("%s, could not find %p\n", __func__, buffer);
376 QTAILQ_FOREACH(reventry, &mapcache->locked_entries, next) {
377 DPRINTF(" "TARGET_FMT_plx" -> %p is present\n", reventry->paddr_index, reventry->vaddr_req);
378 }
379 return;
380 }
381 QTAILQ_REMOVE(&mapcache->locked_entries, reventry, next);
382 g_free(reventry);
383
384 if (mapcache->last_entry != NULL &&
385 mapcache->last_entry->paddr_index == paddr_index) {
386 mapcache->last_entry = NULL;
387 }
388
389 entry = &mapcache->entry[paddr_index % mapcache->nr_buckets];
390 while (entry && (entry->paddr_index != paddr_index || entry->size != size)) {
391 pentry = entry;
392 entry = entry->next;
393 }
394 if (!entry) {
395 DPRINTF("Trying to unmap address %p that is not in the mapcache!\n", buffer);
396 return;
397 }
398 entry->lock--;
399 if (entry->lock > 0 || pentry == NULL) {
400 return;
401 }
402
403 pentry->next = entry->next;
404 ram_block_notify_remove(entry->vaddr_base, entry->size);
405 if (munmap(entry->vaddr_base, entry->size) != 0) {
406 perror("unmap fails");
407 exit(-1);
408 }
409 g_free(entry->valid_mapping);
410 g_free(entry);
411 }
412
413 void xen_invalidate_map_cache_entry(uint8_t *buffer)
414 {
415 mapcache_lock();
416 xen_invalidate_map_cache_entry_unlocked(buffer);
417 mapcache_unlock();
418 }
419
420 void xen_invalidate_map_cache(void)
421 {
422 unsigned long i;
423 MapCacheRev *reventry;
424
425 /* Flush pending AIO before destroying the mapcache */
426 bdrv_drain_all();
427
428 mapcache_lock();
429
430 QTAILQ_FOREACH(reventry, &mapcache->locked_entries, next) {
431 if (!reventry->dma) {
432 continue;
433 }
434 fprintf(stderr, "Locked DMA mapping while invalidating mapcache!"
435 " "TARGET_FMT_plx" -> %p is present\n",
436 reventry->paddr_index, reventry->vaddr_req);
437 }
438
439 for (i = 0; i < mapcache->nr_buckets; i++) {
440 MapCacheEntry *entry = &mapcache->entry[i];
441
442 if (entry->vaddr_base == NULL) {
443 continue;
444 }
445 if (entry->lock > 0) {
446 continue;
447 }
448
449 if (munmap(entry->vaddr_base, entry->size) != 0) {
450 perror("unmap fails");
451 exit(-1);
452 }
453
454 entry->paddr_index = 0;
455 entry->vaddr_base = NULL;
456 entry->size = 0;
457 g_free(entry->valid_mapping);
458 entry->valid_mapping = NULL;
459 }
460
461 mapcache->last_entry = NULL;
462
463 mapcache_unlock();
464 }