]>
Commit | Line | Data |
---|---|---|
432d268c JN |
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 | * | |
6b620ca3 PB |
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. | |
432d268c JN |
9 | */ |
10 | ||
11 | #include "config.h" | |
12 | ||
13 | #include <sys/resource.h> | |
14 | ||
0d09e41a | 15 | #include "hw/xen/xen_backend.h" |
9c17d615 | 16 | #include "sysemu/blockdev.h" |
1de7afc9 | 17 | #include "qemu/bitmap.h" |
432d268c JN |
18 | |
19 | #include <xen/hvm/params.h> | |
20 | #include <sys/mman.h> | |
21 | ||
9c17d615 | 22 | #include "sysemu/xen-mapcache.h" |
432d268c JN |
23 | #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 | ||
643f5932 | 36 | #if HOST_LONG_BITS == 32 |
432d268c | 37 | # define MCACHE_BUCKET_SHIFT 16 |
ea6c5f8f | 38 | # define MCACHE_MAX_SIZE (1UL<<31) /* 2GB Cap */ |
643f5932 | 39 | #else |
432d268c | 40 | # define MCACHE_BUCKET_SHIFT 20 |
ea6c5f8f | 41 | # define MCACHE_MAX_SIZE (1UL<<35) /* 32GB Cap */ |
432d268c JN |
42 | #endif |
43 | #define MCACHE_BUCKET_SIZE (1UL << MCACHE_BUCKET_SHIFT) | |
44 | ||
56c119e5 AP |
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. | |
49 | */ | |
50 | #define NON_MCACHE_MEMORY_SIZE (80 * 1024 * 1024) | |
51 | ||
432d268c | 52 | typedef struct MapCacheEntry { |
a8170e5e | 53 | hwaddr paddr_index; |
432d268c | 54 | uint8_t *vaddr_base; |
c13390cd | 55 | unsigned long *valid_mapping; |
432d268c | 56 | uint8_t lock; |
a8170e5e | 57 | hwaddr size; |
432d268c JN |
58 | struct MapCacheEntry *next; |
59 | } MapCacheEntry; | |
60 | ||
61 | typedef struct MapCacheRev { | |
62 | uint8_t *vaddr_req; | |
a8170e5e AK |
63 | hwaddr paddr_index; |
64 | hwaddr size; | |
432d268c JN |
65 | QTAILQ_ENTRY(MapCacheRev) next; |
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. */ | |
e2deee3e | 74 | MapCacheEntry *last_entry; |
432d268c JN |
75 | unsigned long max_mcache_size; |
76 | unsigned int mcache_bucket_shift; | |
cd1ba7de AP |
77 | |
78 | phys_offset_to_gaddr_t phys_offset_to_gaddr; | |
86a6a9bf | 79 | QemuMutex lock; |
cd1ba7de | 80 | void *opaque; |
432d268c JN |
81 | } MapCache; |
82 | ||
83 | static MapCache *mapcache; | |
84 | ||
86a6a9bf PB |
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 | ||
c13390cd SS |
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 | ||
cd1ba7de | 104 | void xen_map_cache_init(phys_offset_to_gaddr_t f, void *opaque) |
432d268c JN |
105 | { |
106 | unsigned long size; | |
107 | struct rlimit rlimit_as; | |
108 | ||
7267c094 | 109 | mapcache = g_malloc0(sizeof (MapCache)); |
432d268c | 110 | |
cd1ba7de AP |
111 | mapcache->phys_offset_to_gaddr = f; |
112 | mapcache->opaque = opaque; | |
86a6a9bf | 113 | qemu_mutex_init(&mapcache->lock); |
cd1ba7de | 114 | |
432d268c | 115 | QTAILQ_INIT(&mapcache->locked_entries); |
432d268c | 116 | |
56c119e5 AP |
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; | |
ea6c5f8f | 121 | } else { |
56c119e5 AP |
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 | } | |
ea6c5f8f JB |
135 | } |
136 | ||
432d268c | 137 | setrlimit(RLIMIT_AS, &rlimit_as); |
432d268c JN |
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); | |
e41d7c69 JK |
146 | DPRINTF("%s, nr_buckets = %lx size %lu\n", __func__, |
147 | mapcache->nr_buckets, size); | |
7267c094 | 148 | mapcache->entry = g_malloc0(size); |
432d268c JN |
149 | } |
150 | ||
e41d7c69 | 151 | static void xen_remap_bucket(MapCacheEntry *entry, |
a8170e5e AK |
152 | hwaddr size, |
153 | hwaddr address_index) | |
432d268c JN |
154 | { |
155 | uint8_t *vaddr_base; | |
156 | xen_pfn_t *pfns; | |
157 | int *err; | |
ea6c5f8f | 158 | unsigned int i; |
a8170e5e | 159 | hwaddr nb_pfn = size >> XC_PAGE_SHIFT; |
432d268c | 160 | |
e41d7c69 | 161 | trace_xen_remap_bucket(address_index); |
432d268c | 162 | |
7267c094 AL |
163 | pfns = g_malloc0(nb_pfn * sizeof (xen_pfn_t)); |
164 | err = g_malloc0(nb_pfn * sizeof (int)); | |
432d268c JN |
165 | |
166 | if (entry->vaddr_base != NULL) { | |
c13390cd | 167 | if (munmap(entry->vaddr_base, entry->size) != 0) { |
432d268c JN |
168 | perror("unmap fails"); |
169 | exit(-1); | |
170 | } | |
171 | } | |
c13390cd | 172 | if (entry->valid_mapping != NULL) { |
7267c094 | 173 | g_free(entry->valid_mapping); |
c13390cd SS |
174 | entry->valid_mapping = NULL; |
175 | } | |
432d268c JN |
176 | |
177 | for (i = 0; i < nb_pfn; i++) { | |
178 | pfns[i] = (address_index << (MCACHE_BUCKET_SHIFT-XC_PAGE_SHIFT)) + i; | |
179 | } | |
180 | ||
181 | vaddr_base = xc_map_foreign_bulk(xen_xc, xen_domid, PROT_READ|PROT_WRITE, | |
182 | pfns, err, nb_pfn); | |
183 | if (vaddr_base == NULL) { | |
184 | perror("xc_map_foreign_bulk"); | |
185 | exit(-1); | |
186 | } | |
187 | ||
188 | entry->vaddr_base = vaddr_base; | |
189 | entry->paddr_index = address_index; | |
c13390cd | 190 | entry->size = size; |
7267c094 | 191 | entry->valid_mapping = (unsigned long *) g_malloc0(sizeof(unsigned long) * |
c13390cd | 192 | BITS_TO_LONGS(size >> XC_PAGE_SHIFT)); |
432d268c | 193 | |
ea6c5f8f JB |
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); | |
432d268c | 198 | } |
432d268c JN |
199 | } |
200 | ||
7267c094 AL |
201 | g_free(pfns); |
202 | g_free(err); | |
432d268c JN |
203 | } |
204 | ||
86a6a9bf PB |
205 | static uint8_t *xen_map_cache_unlocked(hwaddr phys_addr, hwaddr size, |
206 | uint8_t lock) | |
432d268c JN |
207 | { |
208 | MapCacheEntry *entry, *pentry = NULL; | |
a8170e5e AK |
209 | hwaddr address_index; |
210 | hwaddr address_offset; | |
9b6d7b36 PB |
211 | hwaddr cache_size = size; |
212 | hwaddr test_bit_size; | |
cd1ba7de AP |
213 | bool translated = false; |
214 | ||
215 | tryagain: | |
216 | address_index = phys_addr >> MCACHE_BUCKET_SHIFT; | |
217 | address_offset = phys_addr & (MCACHE_BUCKET_SIZE - 1); | |
432d268c | 218 | |
e41d7c69 | 219 | trace_xen_map_cache(phys_addr); |
432d268c | 220 | |
9b6d7b36 | 221 | /* test_bit_size is always a multiple of XC_PAGE_SIZE */ |
044d4e1a | 222 | if (size) { |
9b6d7b36 | 223 | test_bit_size = size + (phys_addr & (XC_PAGE_SIZE - 1)); |
044d4e1a | 224 | |
9b6d7b36 PB |
225 | if (test_bit_size % XC_PAGE_SIZE) { |
226 | test_bit_size += XC_PAGE_SIZE - (test_bit_size % XC_PAGE_SIZE); | |
044d4e1a H |
227 | } |
228 | } else { | |
9b6d7b36 | 229 | test_bit_size = XC_PAGE_SIZE; |
044d4e1a H |
230 | } |
231 | ||
e2deee3e SS |
232 | if (mapcache->last_entry != NULL && |
233 | mapcache->last_entry->paddr_index == address_index && | |
9b6d7b36 | 234 | !lock && !size && |
044d4e1a | 235 | test_bits(address_offset >> XC_PAGE_SHIFT, |
9b6d7b36 | 236 | test_bit_size >> XC_PAGE_SHIFT, |
044d4e1a | 237 | mapcache->last_entry->valid_mapping)) { |
e2deee3e SS |
238 | trace_xen_map_cache_return(mapcache->last_entry->vaddr_base + address_offset); |
239 | return mapcache->last_entry->vaddr_base + address_offset; | |
432d268c JN |
240 | } |
241 | ||
c13390cd | 242 | /* size is always a multiple of MCACHE_BUCKET_SIZE */ |
09ab48ee | 243 | if (size) { |
9b6d7b36 PB |
244 | cache_size = size + address_offset; |
245 | if (cache_size % MCACHE_BUCKET_SIZE) { | |
246 | cache_size += MCACHE_BUCKET_SIZE - (cache_size % MCACHE_BUCKET_SIZE); | |
09ab48ee AP |
247 | } |
248 | } else { | |
9b6d7b36 | 249 | cache_size = MCACHE_BUCKET_SIZE; |
09ab48ee | 250 | } |
c13390cd | 251 | |
432d268c JN |
252 | entry = &mapcache->entry[address_index % mapcache->nr_buckets]; |
253 | ||
c13390cd | 254 | while (entry && entry->lock && entry->vaddr_base && |
9b6d7b36 | 255 | (entry->paddr_index != address_index || entry->size != cache_size || |
044d4e1a | 256 | !test_bits(address_offset >> XC_PAGE_SHIFT, |
9b6d7b36 | 257 | test_bit_size >> XC_PAGE_SHIFT, |
c13390cd | 258 | entry->valid_mapping))) { |
432d268c JN |
259 | pentry = entry; |
260 | entry = entry->next; | |
261 | } | |
262 | if (!entry) { | |
7267c094 | 263 | entry = g_malloc0(sizeof (MapCacheEntry)); |
432d268c | 264 | pentry->next = entry; |
9b6d7b36 | 265 | xen_remap_bucket(entry, cache_size, address_index); |
432d268c JN |
266 | } else if (!entry->lock) { |
267 | if (!entry->vaddr_base || entry->paddr_index != address_index || | |
9b6d7b36 | 268 | entry->size != cache_size || |
044d4e1a | 269 | !test_bits(address_offset >> XC_PAGE_SHIFT, |
9b6d7b36 | 270 | test_bit_size >> XC_PAGE_SHIFT, |
c13390cd | 271 | entry->valid_mapping)) { |
9b6d7b36 | 272 | xen_remap_bucket(entry, cache_size, address_index); |
432d268c JN |
273 | } |
274 | } | |
275 | ||
044d4e1a | 276 | if(!test_bits(address_offset >> XC_PAGE_SHIFT, |
9b6d7b36 | 277 | test_bit_size >> XC_PAGE_SHIFT, |
c13390cd | 278 | entry->valid_mapping)) { |
e2deee3e | 279 | mapcache->last_entry = NULL; |
cd1ba7de AP |
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 | } | |
e41d7c69 | 285 | trace_xen_map_cache_return(NULL); |
432d268c JN |
286 | return NULL; |
287 | } | |
288 | ||
e2deee3e | 289 | mapcache->last_entry = entry; |
432d268c | 290 | if (lock) { |
7267c094 | 291 | MapCacheRev *reventry = g_malloc0(sizeof(MapCacheRev)); |
432d268c | 292 | entry->lock++; |
e2deee3e SS |
293 | reventry->vaddr_req = mapcache->last_entry->vaddr_base + address_offset; |
294 | reventry->paddr_index = mapcache->last_entry->paddr_index; | |
c13390cd | 295 | reventry->size = entry->size; |
432d268c JN |
296 | QTAILQ_INSERT_HEAD(&mapcache->locked_entries, reventry, next); |
297 | } | |
298 | ||
e2deee3e SS |
299 | trace_xen_map_cache_return(mapcache->last_entry->vaddr_base + address_offset); |
300 | return mapcache->last_entry->vaddr_base + address_offset; | |
432d268c JN |
301 | } |
302 | ||
86a6a9bf PB |
303 | uint8_t *xen_map_cache(hwaddr phys_addr, hwaddr size, |
304 | uint8_t lock) | |
305 | { | |
306 | uint8_t *p; | |
307 | ||
308 | mapcache_lock(); | |
309 | p = xen_map_cache_unlocked(phys_addr, size, lock); | |
310 | mapcache_unlock(); | |
311 | return p; | |
312 | } | |
313 | ||
e41d7c69 | 314 | ram_addr_t xen_ram_addr_from_mapcache(void *ptr) |
432d268c | 315 | { |
ecf169b7 | 316 | MapCacheEntry *entry = NULL; |
432d268c | 317 | MapCacheRev *reventry; |
a8170e5e AK |
318 | hwaddr paddr_index; |
319 | hwaddr size; | |
86a6a9bf | 320 | ram_addr_t raddr; |
432d268c JN |
321 | int found = 0; |
322 | ||
86a6a9bf | 323 | mapcache_lock(); |
432d268c JN |
324 | QTAILQ_FOREACH(reventry, &mapcache->locked_entries, next) { |
325 | if (reventry->vaddr_req == ptr) { | |
326 | paddr_index = reventry->paddr_index; | |
c13390cd | 327 | size = reventry->size; |
432d268c JN |
328 | found = 1; |
329 | break; | |
330 | } | |
331 | } | |
332 | if (!found) { | |
e41d7c69 | 333 | fprintf(stderr, "%s, could not find %p\n", __func__, ptr); |
432d268c JN |
334 | QTAILQ_FOREACH(reventry, &mapcache->locked_entries, next) { |
335 | DPRINTF(" "TARGET_FMT_plx" -> %p is present\n", reventry->paddr_index, | |
336 | reventry->vaddr_req); | |
337 | } | |
338 | abort(); | |
339 | return 0; | |
340 | } | |
341 | ||
c13390cd SS |
342 | entry = &mapcache->entry[paddr_index % mapcache->nr_buckets]; |
343 | while (entry && (entry->paddr_index != paddr_index || entry->size != size)) { | |
c13390cd SS |
344 | entry = entry->next; |
345 | } | |
346 | if (!entry) { | |
347 | DPRINTF("Trying to find address %p that is not in the mapcache!\n", ptr); | |
86a6a9bf PB |
348 | raddr = 0; |
349 | } else { | |
350 | raddr = (reventry->paddr_index << MCACHE_BUCKET_SHIFT) + | |
351 | ((unsigned long) ptr - (unsigned long) entry->vaddr_base); | |
c13390cd | 352 | } |
86a6a9bf PB |
353 | mapcache_unlock(); |
354 | return raddr; | |
432d268c JN |
355 | } |
356 | ||
86a6a9bf | 357 | static void xen_invalidate_map_cache_entry_unlocked(uint8_t *buffer) |
432d268c JN |
358 | { |
359 | MapCacheEntry *entry = NULL, *pentry = NULL; | |
360 | MapCacheRev *reventry; | |
a8170e5e AK |
361 | hwaddr paddr_index; |
362 | hwaddr size; | |
432d268c JN |
363 | int found = 0; |
364 | ||
432d268c JN |
365 | QTAILQ_FOREACH(reventry, &mapcache->locked_entries, next) { |
366 | if (reventry->vaddr_req == buffer) { | |
367 | paddr_index = reventry->paddr_index; | |
c13390cd | 368 | size = reventry->size; |
432d268c JN |
369 | found = 1; |
370 | break; | |
371 | } | |
372 | } | |
373 | if (!found) { | |
e41d7c69 | 374 | DPRINTF("%s, could not find %p\n", __func__, buffer); |
432d268c JN |
375 | QTAILQ_FOREACH(reventry, &mapcache->locked_entries, next) { |
376 | DPRINTF(" "TARGET_FMT_plx" -> %p is present\n", reventry->paddr_index, reventry->vaddr_req); | |
377 | } | |
378 | return; | |
379 | } | |
380 | QTAILQ_REMOVE(&mapcache->locked_entries, reventry, next); | |
7267c094 | 381 | g_free(reventry); |
432d268c | 382 | |
e2deee3e SS |
383 | if (mapcache->last_entry != NULL && |
384 | mapcache->last_entry->paddr_index == paddr_index) { | |
385 | mapcache->last_entry = NULL; | |
27b7652e FZ |
386 | } |
387 | ||
432d268c | 388 | entry = &mapcache->entry[paddr_index % mapcache->nr_buckets]; |
c13390cd | 389 | while (entry && (entry->paddr_index != paddr_index || entry->size != size)) { |
432d268c JN |
390 | pentry = entry; |
391 | entry = entry->next; | |
392 | } | |
393 | if (!entry) { | |
394 | DPRINTF("Trying to unmap address %p that is not in the mapcache!\n", buffer); | |
395 | return; | |
396 | } | |
397 | entry->lock--; | |
398 | if (entry->lock > 0 || pentry == NULL) { | |
399 | return; | |
400 | } | |
401 | ||
402 | pentry->next = entry->next; | |
c13390cd | 403 | if (munmap(entry->vaddr_base, entry->size) != 0) { |
432d268c JN |
404 | perror("unmap fails"); |
405 | exit(-1); | |
406 | } | |
7267c094 AL |
407 | g_free(entry->valid_mapping); |
408 | g_free(entry); | |
432d268c JN |
409 | } |
410 | ||
86a6a9bf PB |
411 | void xen_invalidate_map_cache_entry(uint8_t *buffer) |
412 | { | |
413 | mapcache_lock(); | |
414 | xen_invalidate_map_cache_entry_unlocked(buffer); | |
415 | mapcache_unlock(); | |
416 | } | |
417 | ||
e41d7c69 | 418 | void xen_invalidate_map_cache(void) |
432d268c JN |
419 | { |
420 | unsigned long i; | |
421 | MapCacheRev *reventry; | |
422 | ||
423 | /* Flush pending AIO before destroying the mapcache */ | |
922453bc | 424 | bdrv_drain_all(); |
432d268c | 425 | |
86a6a9bf PB |
426 | mapcache_lock(); |
427 | ||
432d268c JN |
428 | QTAILQ_FOREACH(reventry, &mapcache->locked_entries, next) { |
429 | DPRINTF("There should be no locked mappings at this time, " | |
430 | "but "TARGET_FMT_plx" -> %p is present\n", | |
431 | reventry->paddr_index, reventry->vaddr_req); | |
432 | } | |
433 | ||
432d268c JN |
434 | for (i = 0; i < mapcache->nr_buckets; i++) { |
435 | MapCacheEntry *entry = &mapcache->entry[i]; | |
436 | ||
437 | if (entry->vaddr_base == NULL) { | |
438 | continue; | |
439 | } | |
852a7cec JG |
440 | if (entry->lock > 0) { |
441 | continue; | |
442 | } | |
432d268c | 443 | |
c13390cd | 444 | if (munmap(entry->vaddr_base, entry->size) != 0) { |
432d268c JN |
445 | perror("unmap fails"); |
446 | exit(-1); | |
447 | } | |
448 | ||
449 | entry->paddr_index = 0; | |
450 | entry->vaddr_base = NULL; | |
c13390cd | 451 | entry->size = 0; |
7267c094 | 452 | g_free(entry->valid_mapping); |
c13390cd | 453 | entry->valid_mapping = NULL; |
432d268c JN |
454 | } |
455 | ||
e2deee3e | 456 | mapcache->last_entry = NULL; |
432d268c JN |
457 | |
458 | mapcache_unlock(); | |
459 | } |