]>
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 | ||
21cbfe5f | 11 | #include "qemu/osdep.h" |
432d268c JN |
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 | } | |
012aef07 MA |
172 | g_free(entry->valid_mapping); |
173 | entry->valid_mapping = NULL; | |
432d268c JN |
174 | |
175 | for (i = 0; i < nb_pfn; i++) { | |
176 | pfns[i] = (address_index << (MCACHE_BUCKET_SHIFT-XC_PAGE_SHIFT)) + i; | |
177 | } | |
178 | ||
e0cb42ae IC |
179 | vaddr_base = xenforeignmemory_map(xen_fmem, xen_domid, PROT_READ|PROT_WRITE, |
180 | nb_pfn, pfns, err); | |
432d268c | 181 | if (vaddr_base == NULL) { |
e0cb42ae | 182 | perror("xenforeignmemory_map"); |
432d268c JN |
183 | exit(-1); |
184 | } | |
185 | ||
186 | entry->vaddr_base = vaddr_base; | |
187 | entry->paddr_index = address_index; | |
c13390cd | 188 | entry->size = size; |
7267c094 | 189 | entry->valid_mapping = (unsigned long *) g_malloc0(sizeof(unsigned long) * |
c13390cd | 190 | BITS_TO_LONGS(size >> XC_PAGE_SHIFT)); |
432d268c | 191 | |
ea6c5f8f JB |
192 | bitmap_zero(entry->valid_mapping, nb_pfn); |
193 | for (i = 0; i < nb_pfn; i++) { | |
194 | if (!err[i]) { | |
195 | bitmap_set(entry->valid_mapping, i, 1); | |
432d268c | 196 | } |
432d268c JN |
197 | } |
198 | ||
7267c094 AL |
199 | g_free(pfns); |
200 | g_free(err); | |
432d268c JN |
201 | } |
202 | ||
86a6a9bf PB |
203 | static uint8_t *xen_map_cache_unlocked(hwaddr phys_addr, hwaddr size, |
204 | uint8_t lock) | |
432d268c JN |
205 | { |
206 | MapCacheEntry *entry, *pentry = NULL; | |
a8170e5e AK |
207 | hwaddr address_index; |
208 | hwaddr address_offset; | |
9b6d7b36 PB |
209 | hwaddr cache_size = size; |
210 | hwaddr test_bit_size; | |
cd1ba7de AP |
211 | bool translated = false; |
212 | ||
213 | tryagain: | |
214 | address_index = phys_addr >> MCACHE_BUCKET_SHIFT; | |
215 | address_offset = phys_addr & (MCACHE_BUCKET_SIZE - 1); | |
432d268c | 216 | |
e41d7c69 | 217 | trace_xen_map_cache(phys_addr); |
432d268c | 218 | |
9b6d7b36 | 219 | /* test_bit_size is always a multiple of XC_PAGE_SIZE */ |
044d4e1a | 220 | if (size) { |
9b6d7b36 | 221 | test_bit_size = size + (phys_addr & (XC_PAGE_SIZE - 1)); |
044d4e1a | 222 | |
9b6d7b36 PB |
223 | if (test_bit_size % XC_PAGE_SIZE) { |
224 | test_bit_size += XC_PAGE_SIZE - (test_bit_size % XC_PAGE_SIZE); | |
044d4e1a H |
225 | } |
226 | } else { | |
9b6d7b36 | 227 | test_bit_size = XC_PAGE_SIZE; |
044d4e1a H |
228 | } |
229 | ||
e2deee3e SS |
230 | if (mapcache->last_entry != NULL && |
231 | mapcache->last_entry->paddr_index == address_index && | |
9b6d7b36 | 232 | !lock && !size && |
044d4e1a | 233 | test_bits(address_offset >> XC_PAGE_SHIFT, |
9b6d7b36 | 234 | test_bit_size >> XC_PAGE_SHIFT, |
044d4e1a | 235 | mapcache->last_entry->valid_mapping)) { |
e2deee3e SS |
236 | trace_xen_map_cache_return(mapcache->last_entry->vaddr_base + address_offset); |
237 | return mapcache->last_entry->vaddr_base + address_offset; | |
432d268c JN |
238 | } |
239 | ||
c13390cd | 240 | /* size is always a multiple of MCACHE_BUCKET_SIZE */ |
09ab48ee | 241 | if (size) { |
9b6d7b36 PB |
242 | cache_size = size + address_offset; |
243 | if (cache_size % MCACHE_BUCKET_SIZE) { | |
244 | cache_size += MCACHE_BUCKET_SIZE - (cache_size % MCACHE_BUCKET_SIZE); | |
09ab48ee AP |
245 | } |
246 | } else { | |
9b6d7b36 | 247 | cache_size = MCACHE_BUCKET_SIZE; |
09ab48ee | 248 | } |
c13390cd | 249 | |
432d268c JN |
250 | entry = &mapcache->entry[address_index % mapcache->nr_buckets]; |
251 | ||
c13390cd | 252 | while (entry && entry->lock && entry->vaddr_base && |
9b6d7b36 | 253 | (entry->paddr_index != address_index || entry->size != cache_size || |
044d4e1a | 254 | !test_bits(address_offset >> XC_PAGE_SHIFT, |
9b6d7b36 | 255 | test_bit_size >> XC_PAGE_SHIFT, |
c13390cd | 256 | entry->valid_mapping))) { |
432d268c JN |
257 | pentry = entry; |
258 | entry = entry->next; | |
259 | } | |
260 | if (!entry) { | |
7267c094 | 261 | entry = g_malloc0(sizeof (MapCacheEntry)); |
432d268c | 262 | pentry->next = entry; |
9b6d7b36 | 263 | xen_remap_bucket(entry, cache_size, address_index); |
432d268c JN |
264 | } else if (!entry->lock) { |
265 | if (!entry->vaddr_base || entry->paddr_index != address_index || | |
9b6d7b36 | 266 | entry->size != cache_size || |
044d4e1a | 267 | !test_bits(address_offset >> XC_PAGE_SHIFT, |
9b6d7b36 | 268 | test_bit_size >> XC_PAGE_SHIFT, |
c13390cd | 269 | entry->valid_mapping)) { |
9b6d7b36 | 270 | xen_remap_bucket(entry, cache_size, address_index); |
432d268c JN |
271 | } |
272 | } | |
273 | ||
044d4e1a | 274 | if(!test_bits(address_offset >> XC_PAGE_SHIFT, |
9b6d7b36 | 275 | test_bit_size >> XC_PAGE_SHIFT, |
c13390cd | 276 | entry->valid_mapping)) { |
e2deee3e | 277 | mapcache->last_entry = NULL; |
cd1ba7de AP |
278 | if (!translated && mapcache->phys_offset_to_gaddr) { |
279 | phys_addr = mapcache->phys_offset_to_gaddr(phys_addr, size, mapcache->opaque); | |
280 | translated = true; | |
281 | goto tryagain; | |
282 | } | |
e41d7c69 | 283 | trace_xen_map_cache_return(NULL); |
432d268c JN |
284 | return NULL; |
285 | } | |
286 | ||
e2deee3e | 287 | mapcache->last_entry = entry; |
432d268c | 288 | if (lock) { |
7267c094 | 289 | MapCacheRev *reventry = g_malloc0(sizeof(MapCacheRev)); |
432d268c | 290 | entry->lock++; |
e2deee3e SS |
291 | reventry->vaddr_req = mapcache->last_entry->vaddr_base + address_offset; |
292 | reventry->paddr_index = mapcache->last_entry->paddr_index; | |
c13390cd | 293 | reventry->size = entry->size; |
432d268c JN |
294 | QTAILQ_INSERT_HEAD(&mapcache->locked_entries, reventry, next); |
295 | } | |
296 | ||
e2deee3e SS |
297 | trace_xen_map_cache_return(mapcache->last_entry->vaddr_base + address_offset); |
298 | return mapcache->last_entry->vaddr_base + address_offset; | |
432d268c JN |
299 | } |
300 | ||
86a6a9bf PB |
301 | uint8_t *xen_map_cache(hwaddr phys_addr, hwaddr size, |
302 | uint8_t lock) | |
303 | { | |
304 | uint8_t *p; | |
305 | ||
306 | mapcache_lock(); | |
307 | p = xen_map_cache_unlocked(phys_addr, size, lock); | |
308 | mapcache_unlock(); | |
309 | return p; | |
310 | } | |
311 | ||
e41d7c69 | 312 | ram_addr_t xen_ram_addr_from_mapcache(void *ptr) |
432d268c | 313 | { |
ecf169b7 | 314 | MapCacheEntry *entry = NULL; |
432d268c | 315 | MapCacheRev *reventry; |
a8170e5e AK |
316 | hwaddr paddr_index; |
317 | hwaddr size; | |
86a6a9bf | 318 | ram_addr_t raddr; |
432d268c JN |
319 | int found = 0; |
320 | ||
86a6a9bf | 321 | mapcache_lock(); |
432d268c JN |
322 | QTAILQ_FOREACH(reventry, &mapcache->locked_entries, next) { |
323 | if (reventry->vaddr_req == ptr) { | |
324 | paddr_index = reventry->paddr_index; | |
c13390cd | 325 | size = reventry->size; |
432d268c JN |
326 | found = 1; |
327 | break; | |
328 | } | |
329 | } | |
330 | if (!found) { | |
e41d7c69 | 331 | fprintf(stderr, "%s, could not find %p\n", __func__, ptr); |
432d268c JN |
332 | QTAILQ_FOREACH(reventry, &mapcache->locked_entries, next) { |
333 | DPRINTF(" "TARGET_FMT_plx" -> %p is present\n", reventry->paddr_index, | |
334 | reventry->vaddr_req); | |
335 | } | |
336 | abort(); | |
337 | return 0; | |
338 | } | |
339 | ||
c13390cd SS |
340 | entry = &mapcache->entry[paddr_index % mapcache->nr_buckets]; |
341 | while (entry && (entry->paddr_index != paddr_index || entry->size != size)) { | |
c13390cd SS |
342 | entry = entry->next; |
343 | } | |
344 | if (!entry) { | |
345 | DPRINTF("Trying to find address %p that is not in the mapcache!\n", ptr); | |
86a6a9bf PB |
346 | raddr = 0; |
347 | } else { | |
348 | raddr = (reventry->paddr_index << MCACHE_BUCKET_SHIFT) + | |
349 | ((unsigned long) ptr - (unsigned long) entry->vaddr_base); | |
c13390cd | 350 | } |
86a6a9bf PB |
351 | mapcache_unlock(); |
352 | return raddr; | |
432d268c JN |
353 | } |
354 | ||
86a6a9bf | 355 | static void xen_invalidate_map_cache_entry_unlocked(uint8_t *buffer) |
432d268c JN |
356 | { |
357 | MapCacheEntry *entry = NULL, *pentry = NULL; | |
358 | MapCacheRev *reventry; | |
a8170e5e AK |
359 | hwaddr paddr_index; |
360 | hwaddr size; | |
432d268c JN |
361 | int found = 0; |
362 | ||
432d268c JN |
363 | QTAILQ_FOREACH(reventry, &mapcache->locked_entries, next) { |
364 | if (reventry->vaddr_req == buffer) { | |
365 | paddr_index = reventry->paddr_index; | |
c13390cd | 366 | size = reventry->size; |
432d268c JN |
367 | found = 1; |
368 | break; | |
369 | } | |
370 | } | |
371 | if (!found) { | |
e41d7c69 | 372 | DPRINTF("%s, could not find %p\n", __func__, buffer); |
432d268c JN |
373 | QTAILQ_FOREACH(reventry, &mapcache->locked_entries, next) { |
374 | DPRINTF(" "TARGET_FMT_plx" -> %p is present\n", reventry->paddr_index, reventry->vaddr_req); | |
375 | } | |
376 | return; | |
377 | } | |
378 | QTAILQ_REMOVE(&mapcache->locked_entries, reventry, next); | |
7267c094 | 379 | g_free(reventry); |
432d268c | 380 | |
e2deee3e SS |
381 | if (mapcache->last_entry != NULL && |
382 | mapcache->last_entry->paddr_index == paddr_index) { | |
383 | mapcache->last_entry = NULL; | |
27b7652e FZ |
384 | } |
385 | ||
432d268c | 386 | entry = &mapcache->entry[paddr_index % mapcache->nr_buckets]; |
c13390cd | 387 | while (entry && (entry->paddr_index != paddr_index || entry->size != size)) { |
432d268c JN |
388 | pentry = entry; |
389 | entry = entry->next; | |
390 | } | |
391 | if (!entry) { | |
392 | DPRINTF("Trying to unmap address %p that is not in the mapcache!\n", buffer); | |
393 | return; | |
394 | } | |
395 | entry->lock--; | |
396 | if (entry->lock > 0 || pentry == NULL) { | |
397 | return; | |
398 | } | |
399 | ||
400 | pentry->next = entry->next; | |
c13390cd | 401 | if (munmap(entry->vaddr_base, entry->size) != 0) { |
432d268c JN |
402 | perror("unmap fails"); |
403 | exit(-1); | |
404 | } | |
7267c094 AL |
405 | g_free(entry->valid_mapping); |
406 | g_free(entry); | |
432d268c JN |
407 | } |
408 | ||
86a6a9bf PB |
409 | void xen_invalidate_map_cache_entry(uint8_t *buffer) |
410 | { | |
411 | mapcache_lock(); | |
412 | xen_invalidate_map_cache_entry_unlocked(buffer); | |
413 | mapcache_unlock(); | |
414 | } | |
415 | ||
e41d7c69 | 416 | void xen_invalidate_map_cache(void) |
432d268c JN |
417 | { |
418 | unsigned long i; | |
419 | MapCacheRev *reventry; | |
420 | ||
421 | /* Flush pending AIO before destroying the mapcache */ | |
922453bc | 422 | bdrv_drain_all(); |
432d268c | 423 | |
86a6a9bf PB |
424 | mapcache_lock(); |
425 | ||
432d268c JN |
426 | QTAILQ_FOREACH(reventry, &mapcache->locked_entries, next) { |
427 | DPRINTF("There should be no locked mappings at this time, " | |
428 | "but "TARGET_FMT_plx" -> %p is present\n", | |
429 | reventry->paddr_index, reventry->vaddr_req); | |
430 | } | |
431 | ||
432d268c JN |
432 | for (i = 0; i < mapcache->nr_buckets; i++) { |
433 | MapCacheEntry *entry = &mapcache->entry[i]; | |
434 | ||
435 | if (entry->vaddr_base == NULL) { | |
436 | continue; | |
437 | } | |
852a7cec JG |
438 | if (entry->lock > 0) { |
439 | continue; | |
440 | } | |
432d268c | 441 | |
c13390cd | 442 | if (munmap(entry->vaddr_base, entry->size) != 0) { |
432d268c JN |
443 | perror("unmap fails"); |
444 | exit(-1); | |
445 | } | |
446 | ||
447 | entry->paddr_index = 0; | |
448 | entry->vaddr_base = NULL; | |
c13390cd | 449 | entry->size = 0; |
7267c094 | 450 | g_free(entry->valid_mapping); |
c13390cd | 451 | entry->valid_mapping = NULL; |
432d268c JN |
452 | } |
453 | ||
e2deee3e | 454 | mapcache->last_entry = NULL; |
432d268c JN |
455 | |
456 | mapcache_unlock(); | |
457 | } |