blob: eda914a75c983f14e5e54f87a2aae827e350d0f8 [file] [log] [blame]
Jun Nakajima432d2682010-08-31 16:41:25 +01001/*
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 *
Paolo Bonzini6b620ca2012-01-13 17:44:23 +01007 * Contributions after 2012-01-13 are licensed under the terms of the
8 * GNU GPL, version 2 or (at your option) any later version.
Jun Nakajima432d2682010-08-31 16:41:25 +01009 */
10
11#include "config.h"
12
13#include <sys/resource.h>
14
Paolo Bonzini0d09e412013-02-05 17:06:20 +010015#include "hw/xen/xen_backend.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010016#include "sysemu/blockdev.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010017#include "qemu/bitmap.h"
Jun Nakajima432d2682010-08-31 16:41:25 +010018
19#include <xen/hvm/params.h>
20#include <sys/mman.h>
21
Paolo Bonzini9c17d612012-12-17 18:20:04 +010022#include "sysemu/xen-mapcache.h"
Jun Nakajima432d2682010-08-31 16:41:25 +010023#include "trace.h"
24
25
26//#define MAPCACHE_DEBUG
27
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
35
36#if defined(__i386__)
37# define MCACHE_BUCKET_SHIFT 16
John Babovalea6c5f82011-03-22 14:50:28 +000038# define MCACHE_MAX_SIZE (1UL<<31) /* 2GB Cap */
Jun Nakajima432d2682010-08-31 16:41:25 +010039#elif defined(__x86_64__)
40# define MCACHE_BUCKET_SHIFT 20
John Babovalea6c5f82011-03-22 14:50:28 +000041# define MCACHE_MAX_SIZE (1UL<<35) /* 32GB Cap */
Jun Nakajima432d2682010-08-31 16:41:25 +010042#endif
43#define MCACHE_BUCKET_SIZE (1UL << MCACHE_BUCKET_SHIFT)
44
Anthony PERARD56c119e2011-09-09 12:50:18 +000045/* 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.
49 */
50#define NON_MCACHE_MEMORY_SIZE (80 * 1024 * 1024)
51
Jan Kiszkae41d7c62011-06-21 22:59:08 +020052#define mapcache_lock() ((void)0)
53#define mapcache_unlock() ((void)0)
54
Jun Nakajima432d2682010-08-31 16:41:25 +010055typedef struct MapCacheEntry {
Avi Kivitya8170e52012-10-23 12:30:10 +020056 hwaddr paddr_index;
Jun Nakajima432d2682010-08-31 16:41:25 +010057 uint8_t *vaddr_base;
Stefano Stabellinic13390c2011-05-19 18:35:42 +010058 unsigned long *valid_mapping;
Jun Nakajima432d2682010-08-31 16:41:25 +010059 uint8_t lock;
Avi Kivitya8170e52012-10-23 12:30:10 +020060 hwaddr size;
Jun Nakajima432d2682010-08-31 16:41:25 +010061 struct MapCacheEntry *next;
62} MapCacheEntry;
63
64typedef struct MapCacheRev {
65 uint8_t *vaddr_req;
Avi Kivitya8170e52012-10-23 12:30:10 +020066 hwaddr paddr_index;
67 hwaddr size;
Jun Nakajima432d2682010-08-31 16:41:25 +010068 QTAILQ_ENTRY(MapCacheRev) next;
69} MapCacheRev;
70
71typedef struct MapCache {
72 MapCacheEntry *entry;
73 unsigned long nr_buckets;
74 QTAILQ_HEAD(map_cache_head, MapCacheRev) locked_entries;
75
76 /* For most cases (>99.9%), the page address is the same. */
Stefano Stabellinie2deee32013-04-02 13:23:40 +000077 MapCacheEntry *last_entry;
Jun Nakajima432d2682010-08-31 16:41:25 +010078 unsigned long max_mcache_size;
79 unsigned int mcache_bucket_shift;
Anthony PERARDcd1ba7d2012-01-18 12:21:38 +000080
81 phys_offset_to_gaddr_t phys_offset_to_gaddr;
82 void *opaque;
Jun Nakajima432d2682010-08-31 16:41:25 +010083} MapCache;
84
85static MapCache *mapcache;
86
Stefano Stabellinic13390c2011-05-19 18:35:42 +010087static inline int test_bits(int nr, int size, const unsigned long *addr)
88{
89 unsigned long res = find_next_zero_bit(addr, size + nr, nr);
90 if (res >= nr + size)
91 return 1;
92 else
93 return 0;
94}
95
Anthony PERARDcd1ba7d2012-01-18 12:21:38 +000096void xen_map_cache_init(phys_offset_to_gaddr_t f, void *opaque)
Jun Nakajima432d2682010-08-31 16:41:25 +010097{
98 unsigned long size;
99 struct rlimit rlimit_as;
100
Anthony Liguori7267c092011-08-20 22:09:37 -0500101 mapcache = g_malloc0(sizeof (MapCache));
Jun Nakajima432d2682010-08-31 16:41:25 +0100102
Anthony PERARDcd1ba7d2012-01-18 12:21:38 +0000103 mapcache->phys_offset_to_gaddr = f;
104 mapcache->opaque = opaque;
105
Jun Nakajima432d2682010-08-31 16:41:25 +0100106 QTAILQ_INIT(&mapcache->locked_entries);
Jun Nakajima432d2682010-08-31 16:41:25 +0100107
Anthony PERARD56c119e2011-09-09 12:50:18 +0000108 if (geteuid() == 0) {
109 rlimit_as.rlim_cur = RLIM_INFINITY;
110 rlimit_as.rlim_max = RLIM_INFINITY;
111 mapcache->max_mcache_size = MCACHE_MAX_SIZE;
John Babovalea6c5f82011-03-22 14:50:28 +0000112 } else {
Anthony PERARD56c119e2011-09-09 12:50:18 +0000113 getrlimit(RLIMIT_AS, &rlimit_as);
114 rlimit_as.rlim_cur = rlimit_as.rlim_max;
115
116 if (rlimit_as.rlim_max != RLIM_INFINITY) {
117 fprintf(stderr, "Warning: QEMU's maximum size of virtual"
118 " memory is not infinity.\n");
119 }
120 if (rlimit_as.rlim_max < MCACHE_MAX_SIZE + NON_MCACHE_MEMORY_SIZE) {
121 mapcache->max_mcache_size = rlimit_as.rlim_max -
122 NON_MCACHE_MEMORY_SIZE;
123 } else {
124 mapcache->max_mcache_size = MCACHE_MAX_SIZE;
125 }
John Babovalea6c5f82011-03-22 14:50:28 +0000126 }
127
Jun Nakajima432d2682010-08-31 16:41:25 +0100128 setrlimit(RLIMIT_AS, &rlimit_as);
Jun Nakajima432d2682010-08-31 16:41:25 +0100129
130 mapcache->nr_buckets =
131 (((mapcache->max_mcache_size >> XC_PAGE_SHIFT) +
132 (1UL << (MCACHE_BUCKET_SHIFT - XC_PAGE_SHIFT)) - 1) >>
133 (MCACHE_BUCKET_SHIFT - XC_PAGE_SHIFT));
134
135 size = mapcache->nr_buckets * sizeof (MapCacheEntry);
136 size = (size + XC_PAGE_SIZE - 1) & ~(XC_PAGE_SIZE - 1);
Jan Kiszkae41d7c62011-06-21 22:59:08 +0200137 DPRINTF("%s, nr_buckets = %lx size %lu\n", __func__,
138 mapcache->nr_buckets, size);
Anthony Liguori7267c092011-08-20 22:09:37 -0500139 mapcache->entry = g_malloc0(size);
Jun Nakajima432d2682010-08-31 16:41:25 +0100140}
141
Jan Kiszkae41d7c62011-06-21 22:59:08 +0200142static void xen_remap_bucket(MapCacheEntry *entry,
Avi Kivitya8170e52012-10-23 12:30:10 +0200143 hwaddr size,
144 hwaddr address_index)
Jun Nakajima432d2682010-08-31 16:41:25 +0100145{
146 uint8_t *vaddr_base;
147 xen_pfn_t *pfns;
148 int *err;
John Babovalea6c5f82011-03-22 14:50:28 +0000149 unsigned int i;
Avi Kivitya8170e52012-10-23 12:30:10 +0200150 hwaddr nb_pfn = size >> XC_PAGE_SHIFT;
Jun Nakajima432d2682010-08-31 16:41:25 +0100151
Jan Kiszkae41d7c62011-06-21 22:59:08 +0200152 trace_xen_remap_bucket(address_index);
Jun Nakajima432d2682010-08-31 16:41:25 +0100153
Anthony Liguori7267c092011-08-20 22:09:37 -0500154 pfns = g_malloc0(nb_pfn * sizeof (xen_pfn_t));
155 err = g_malloc0(nb_pfn * sizeof (int));
Jun Nakajima432d2682010-08-31 16:41:25 +0100156
157 if (entry->vaddr_base != NULL) {
Stefano Stabellinic13390c2011-05-19 18:35:42 +0100158 if (munmap(entry->vaddr_base, entry->size) != 0) {
Jun Nakajima432d2682010-08-31 16:41:25 +0100159 perror("unmap fails");
160 exit(-1);
161 }
162 }
Stefano Stabellinic13390c2011-05-19 18:35:42 +0100163 if (entry->valid_mapping != NULL) {
Anthony Liguori7267c092011-08-20 22:09:37 -0500164 g_free(entry->valid_mapping);
Stefano Stabellinic13390c2011-05-19 18:35:42 +0100165 entry->valid_mapping = NULL;
166 }
Jun Nakajima432d2682010-08-31 16:41:25 +0100167
168 for (i = 0; i < nb_pfn; i++) {
169 pfns[i] = (address_index << (MCACHE_BUCKET_SHIFT-XC_PAGE_SHIFT)) + i;
170 }
171
172 vaddr_base = xc_map_foreign_bulk(xen_xc, xen_domid, PROT_READ|PROT_WRITE,
173 pfns, err, nb_pfn);
174 if (vaddr_base == NULL) {
175 perror("xc_map_foreign_bulk");
176 exit(-1);
177 }
178
179 entry->vaddr_base = vaddr_base;
180 entry->paddr_index = address_index;
Stefano Stabellinic13390c2011-05-19 18:35:42 +0100181 entry->size = size;
Anthony Liguori7267c092011-08-20 22:09:37 -0500182 entry->valid_mapping = (unsigned long *) g_malloc0(sizeof(unsigned long) *
Stefano Stabellinic13390c2011-05-19 18:35:42 +0100183 BITS_TO_LONGS(size >> XC_PAGE_SHIFT));
Jun Nakajima432d2682010-08-31 16:41:25 +0100184
John Babovalea6c5f82011-03-22 14:50:28 +0000185 bitmap_zero(entry->valid_mapping, nb_pfn);
186 for (i = 0; i < nb_pfn; i++) {
187 if (!err[i]) {
188 bitmap_set(entry->valid_mapping, i, 1);
Jun Nakajima432d2682010-08-31 16:41:25 +0100189 }
Jun Nakajima432d2682010-08-31 16:41:25 +0100190 }
191
Anthony Liguori7267c092011-08-20 22:09:37 -0500192 g_free(pfns);
193 g_free(err);
Jun Nakajima432d2682010-08-31 16:41:25 +0100194}
195
Avi Kivitya8170e52012-10-23 12:30:10 +0200196uint8_t *xen_map_cache(hwaddr phys_addr, hwaddr size,
Jan Kiszkae41d7c62011-06-21 22:59:08 +0200197 uint8_t lock)
Jun Nakajima432d2682010-08-31 16:41:25 +0100198{
199 MapCacheEntry *entry, *pentry = NULL;
Avi Kivitya8170e52012-10-23 12:30:10 +0200200 hwaddr address_index;
201 hwaddr address_offset;
202 hwaddr __size = size;
Hanweidong044d4e12013-04-02 13:22:41 +0000203 hwaddr __test_bit_size = size;
Anthony PERARDcd1ba7d2012-01-18 12:21:38 +0000204 bool translated = false;
205
206tryagain:
207 address_index = phys_addr >> MCACHE_BUCKET_SHIFT;
208 address_offset = phys_addr & (MCACHE_BUCKET_SIZE - 1);
Jun Nakajima432d2682010-08-31 16:41:25 +0100209
Jan Kiszkae41d7c62011-06-21 22:59:08 +0200210 trace_xen_map_cache(phys_addr);
Jun Nakajima432d2682010-08-31 16:41:25 +0100211
Hanweidong044d4e12013-04-02 13:22:41 +0000212 /* __test_bit_size is always a multiple of XC_PAGE_SIZE */
213 if (size) {
214 __test_bit_size = size + (phys_addr & (XC_PAGE_SIZE - 1));
215
216 if (__test_bit_size % XC_PAGE_SIZE) {
217 __test_bit_size += XC_PAGE_SIZE - (__test_bit_size % XC_PAGE_SIZE);
218 }
219 } else {
220 __test_bit_size = XC_PAGE_SIZE;
221 }
222
Stefano Stabellinie2deee32013-04-02 13:23:40 +0000223 if (mapcache->last_entry != NULL &&
224 mapcache->last_entry->paddr_index == address_index &&
Hanweidong044d4e12013-04-02 13:22:41 +0000225 !lock && !__size &&
226 test_bits(address_offset >> XC_PAGE_SHIFT,
227 __test_bit_size >> XC_PAGE_SHIFT,
228 mapcache->last_entry->valid_mapping)) {
Stefano Stabellinie2deee32013-04-02 13:23:40 +0000229 trace_xen_map_cache_return(mapcache->last_entry->vaddr_base + address_offset);
230 return mapcache->last_entry->vaddr_base + address_offset;
Jun Nakajima432d2682010-08-31 16:41:25 +0100231 }
232
Stefano Stabellinic13390c2011-05-19 18:35:42 +0100233 /* size is always a multiple of MCACHE_BUCKET_SIZE */
Anthony PERARD09ab48e2012-04-13 17:18:56 +0000234 if (size) {
235 __size = size + address_offset;
236 if (__size % MCACHE_BUCKET_SIZE) {
237 __size += MCACHE_BUCKET_SIZE - (__size % MCACHE_BUCKET_SIZE);
238 }
239 } else {
Stefano Stabellinic13390c2011-05-19 18:35:42 +0100240 __size = MCACHE_BUCKET_SIZE;
Anthony PERARD09ab48e2012-04-13 17:18:56 +0000241 }
Stefano Stabellinic13390c2011-05-19 18:35:42 +0100242
Jun Nakajima432d2682010-08-31 16:41:25 +0100243 entry = &mapcache->entry[address_index % mapcache->nr_buckets];
244
Stefano Stabellinic13390c2011-05-19 18:35:42 +0100245 while (entry && entry->lock && entry->vaddr_base &&
246 (entry->paddr_index != address_index || entry->size != __size ||
Hanweidong044d4e12013-04-02 13:22:41 +0000247 !test_bits(address_offset >> XC_PAGE_SHIFT,
248 __test_bit_size >> XC_PAGE_SHIFT,
Stefano Stabellinic13390c2011-05-19 18:35:42 +0100249 entry->valid_mapping))) {
Jun Nakajima432d2682010-08-31 16:41:25 +0100250 pentry = entry;
251 entry = entry->next;
252 }
253 if (!entry) {
Anthony Liguori7267c092011-08-20 22:09:37 -0500254 entry = g_malloc0(sizeof (MapCacheEntry));
Jun Nakajima432d2682010-08-31 16:41:25 +0100255 pentry->next = entry;
Jan Kiszkae41d7c62011-06-21 22:59:08 +0200256 xen_remap_bucket(entry, __size, address_index);
Jun Nakajima432d2682010-08-31 16:41:25 +0100257 } else if (!entry->lock) {
258 if (!entry->vaddr_base || entry->paddr_index != address_index ||
Stefano Stabellinic13390c2011-05-19 18:35:42 +0100259 entry->size != __size ||
Hanweidong044d4e12013-04-02 13:22:41 +0000260 !test_bits(address_offset >> XC_PAGE_SHIFT,
261 __test_bit_size >> XC_PAGE_SHIFT,
Stefano Stabellinic13390c2011-05-19 18:35:42 +0100262 entry->valid_mapping)) {
Jan Kiszkae41d7c62011-06-21 22:59:08 +0200263 xen_remap_bucket(entry, __size, address_index);
Jun Nakajima432d2682010-08-31 16:41:25 +0100264 }
265 }
266
Hanweidong044d4e12013-04-02 13:22:41 +0000267 if(!test_bits(address_offset >> XC_PAGE_SHIFT,
268 __test_bit_size >> XC_PAGE_SHIFT,
Stefano Stabellinic13390c2011-05-19 18:35:42 +0100269 entry->valid_mapping)) {
Stefano Stabellinie2deee32013-04-02 13:23:40 +0000270 mapcache->last_entry = NULL;
Anthony PERARDcd1ba7d2012-01-18 12:21:38 +0000271 if (!translated && mapcache->phys_offset_to_gaddr) {
272 phys_addr = mapcache->phys_offset_to_gaddr(phys_addr, size, mapcache->opaque);
273 translated = true;
274 goto tryagain;
275 }
Jan Kiszkae41d7c62011-06-21 22:59:08 +0200276 trace_xen_map_cache_return(NULL);
Jun Nakajima432d2682010-08-31 16:41:25 +0100277 return NULL;
278 }
279
Stefano Stabellinie2deee32013-04-02 13:23:40 +0000280 mapcache->last_entry = entry;
Jun Nakajima432d2682010-08-31 16:41:25 +0100281 if (lock) {
Anthony Liguori7267c092011-08-20 22:09:37 -0500282 MapCacheRev *reventry = g_malloc0(sizeof(MapCacheRev));
Jun Nakajima432d2682010-08-31 16:41:25 +0100283 entry->lock++;
Stefano Stabellinie2deee32013-04-02 13:23:40 +0000284 reventry->vaddr_req = mapcache->last_entry->vaddr_base + address_offset;
285 reventry->paddr_index = mapcache->last_entry->paddr_index;
Stefano Stabellinic13390c2011-05-19 18:35:42 +0100286 reventry->size = entry->size;
Jun Nakajima432d2682010-08-31 16:41:25 +0100287 QTAILQ_INSERT_HEAD(&mapcache->locked_entries, reventry, next);
288 }
289
Stefano Stabellinie2deee32013-04-02 13:23:40 +0000290 trace_xen_map_cache_return(mapcache->last_entry->vaddr_base + address_offset);
291 return mapcache->last_entry->vaddr_base + address_offset;
Jun Nakajima432d2682010-08-31 16:41:25 +0100292}
293
Jan Kiszkae41d7c62011-06-21 22:59:08 +0200294ram_addr_t xen_ram_addr_from_mapcache(void *ptr)
Jun Nakajima432d2682010-08-31 16:41:25 +0100295{
Stefan Bergerecf169b2011-07-26 10:33:11 -0400296 MapCacheEntry *entry = NULL;
Jun Nakajima432d2682010-08-31 16:41:25 +0100297 MapCacheRev *reventry;
Avi Kivitya8170e52012-10-23 12:30:10 +0200298 hwaddr paddr_index;
299 hwaddr size;
Jun Nakajima432d2682010-08-31 16:41:25 +0100300 int found = 0;
301
302 QTAILQ_FOREACH(reventry, &mapcache->locked_entries, next) {
303 if (reventry->vaddr_req == ptr) {
304 paddr_index = reventry->paddr_index;
Stefano Stabellinic13390c2011-05-19 18:35:42 +0100305 size = reventry->size;
Jun Nakajima432d2682010-08-31 16:41:25 +0100306 found = 1;
307 break;
308 }
309 }
310 if (!found) {
Jan Kiszkae41d7c62011-06-21 22:59:08 +0200311 fprintf(stderr, "%s, could not find %p\n", __func__, ptr);
Jun Nakajima432d2682010-08-31 16:41:25 +0100312 QTAILQ_FOREACH(reventry, &mapcache->locked_entries, next) {
313 DPRINTF(" "TARGET_FMT_plx" -> %p is present\n", reventry->paddr_index,
314 reventry->vaddr_req);
315 }
316 abort();
317 return 0;
318 }
319
Stefano Stabellinic13390c2011-05-19 18:35:42 +0100320 entry = &mapcache->entry[paddr_index % mapcache->nr_buckets];
321 while (entry && (entry->paddr_index != paddr_index || entry->size != size)) {
Stefano Stabellinic13390c2011-05-19 18:35:42 +0100322 entry = entry->next;
323 }
324 if (!entry) {
325 DPRINTF("Trying to find address %p that is not in the mapcache!\n", ptr);
326 return 0;
327 }
328 return (reventry->paddr_index << MCACHE_BUCKET_SHIFT) +
329 ((unsigned long) ptr - (unsigned long) entry->vaddr_base);
Jun Nakajima432d2682010-08-31 16:41:25 +0100330}
331
Jan Kiszkae41d7c62011-06-21 22:59:08 +0200332void xen_invalidate_map_cache_entry(uint8_t *buffer)
Jun Nakajima432d2682010-08-31 16:41:25 +0100333{
334 MapCacheEntry *entry = NULL, *pentry = NULL;
335 MapCacheRev *reventry;
Avi Kivitya8170e52012-10-23 12:30:10 +0200336 hwaddr paddr_index;
337 hwaddr size;
Jun Nakajima432d2682010-08-31 16:41:25 +0100338 int found = 0;
339
Jun Nakajima432d2682010-08-31 16:41:25 +0100340 QTAILQ_FOREACH(reventry, &mapcache->locked_entries, next) {
341 if (reventry->vaddr_req == buffer) {
342 paddr_index = reventry->paddr_index;
Stefano Stabellinic13390c2011-05-19 18:35:42 +0100343 size = reventry->size;
Jun Nakajima432d2682010-08-31 16:41:25 +0100344 found = 1;
345 break;
346 }
347 }
348 if (!found) {
Jan Kiszkae41d7c62011-06-21 22:59:08 +0200349 DPRINTF("%s, could not find %p\n", __func__, buffer);
Jun Nakajima432d2682010-08-31 16:41:25 +0100350 QTAILQ_FOREACH(reventry, &mapcache->locked_entries, next) {
351 DPRINTF(" "TARGET_FMT_plx" -> %p is present\n", reventry->paddr_index, reventry->vaddr_req);
352 }
353 return;
354 }
355 QTAILQ_REMOVE(&mapcache->locked_entries, reventry, next);
Anthony Liguori7267c092011-08-20 22:09:37 -0500356 g_free(reventry);
Jun Nakajima432d2682010-08-31 16:41:25 +0100357
Stefano Stabellinie2deee32013-04-02 13:23:40 +0000358 if (mapcache->last_entry != NULL &&
359 mapcache->last_entry->paddr_index == paddr_index) {
360 mapcache->last_entry = NULL;
Frediano Ziglio27b76522012-08-22 10:17:04 +0000361 }
362
Jun Nakajima432d2682010-08-31 16:41:25 +0100363 entry = &mapcache->entry[paddr_index % mapcache->nr_buckets];
Stefano Stabellinic13390c2011-05-19 18:35:42 +0100364 while (entry && (entry->paddr_index != paddr_index || entry->size != size)) {
Jun Nakajima432d2682010-08-31 16:41:25 +0100365 pentry = entry;
366 entry = entry->next;
367 }
368 if (!entry) {
369 DPRINTF("Trying to unmap address %p that is not in the mapcache!\n", buffer);
370 return;
371 }
372 entry->lock--;
373 if (entry->lock > 0 || pentry == NULL) {
374 return;
375 }
376
377 pentry->next = entry->next;
Stefano Stabellinic13390c2011-05-19 18:35:42 +0100378 if (munmap(entry->vaddr_base, entry->size) != 0) {
Jun Nakajima432d2682010-08-31 16:41:25 +0100379 perror("unmap fails");
380 exit(-1);
381 }
Anthony Liguori7267c092011-08-20 22:09:37 -0500382 g_free(entry->valid_mapping);
383 g_free(entry);
Jun Nakajima432d2682010-08-31 16:41:25 +0100384}
385
Jan Kiszkae41d7c62011-06-21 22:59:08 +0200386void xen_invalidate_map_cache(void)
Jun Nakajima432d2682010-08-31 16:41:25 +0100387{
388 unsigned long i;
389 MapCacheRev *reventry;
390
391 /* Flush pending AIO before destroying the mapcache */
Stefan Hajnoczi922453b2011-11-30 12:23:43 +0000392 bdrv_drain_all();
Jun Nakajima432d2682010-08-31 16:41:25 +0100393
394 QTAILQ_FOREACH(reventry, &mapcache->locked_entries, next) {
395 DPRINTF("There should be no locked mappings at this time, "
396 "but "TARGET_FMT_plx" -> %p is present\n",
397 reventry->paddr_index, reventry->vaddr_req);
398 }
399
400 mapcache_lock();
401
402 for (i = 0; i < mapcache->nr_buckets; i++) {
403 MapCacheEntry *entry = &mapcache->entry[i];
404
405 if (entry->vaddr_base == NULL) {
406 continue;
407 }
Julien Grall852a7ce2012-04-13 17:33:02 +0000408 if (entry->lock > 0) {
409 continue;
410 }
Jun Nakajima432d2682010-08-31 16:41:25 +0100411
Stefano Stabellinic13390c2011-05-19 18:35:42 +0100412 if (munmap(entry->vaddr_base, entry->size) != 0) {
Jun Nakajima432d2682010-08-31 16:41:25 +0100413 perror("unmap fails");
414 exit(-1);
415 }
416
417 entry->paddr_index = 0;
418 entry->vaddr_base = NULL;
Stefano Stabellinic13390c2011-05-19 18:35:42 +0100419 entry->size = 0;
Anthony Liguori7267c092011-08-20 22:09:37 -0500420 g_free(entry->valid_mapping);
Stefano Stabellinic13390c2011-05-19 18:35:42 +0100421 entry->valid_mapping = NULL;
Jun Nakajima432d2682010-08-31 16:41:25 +0100422 }
423
Stefano Stabellinie2deee32013-04-02 13:23:40 +0000424 mapcache->last_entry = NULL;
Jun Nakajima432d2682010-08-31 16:41:25 +0100425
426 mapcache_unlock();
427}