]>
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> | |
432d268c | 20 | |
9c17d615 | 21 | #include "sysemu/xen-mapcache.h" |
0ab8ed18 | 22 | #include "trace-root.h" |
432d268c JN |
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 | ||
643f5932 | 35 | #if HOST_LONG_BITS == 32 |
432d268c | 36 | # define MCACHE_BUCKET_SHIFT 16 |
ea6c5f8f | 37 | # define MCACHE_MAX_SIZE (1UL<<31) /* 2GB Cap */ |
643f5932 | 38 | #else |
432d268c | 39 | # define MCACHE_BUCKET_SHIFT 20 |
ea6c5f8f | 40 | # define MCACHE_MAX_SIZE (1UL<<35) /* 32GB Cap */ |
432d268c JN |
41 | #endif |
42 | #define MCACHE_BUCKET_SIZE (1UL << MCACHE_BUCKET_SHIFT) | |
43 | ||
56c119e5 AP |
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 | ||
432d268c | 51 | typedef struct MapCacheEntry { |
a8170e5e | 52 | hwaddr paddr_index; |
432d268c | 53 | uint8_t *vaddr_base; |
c13390cd | 54 | unsigned long *valid_mapping; |
432d268c | 55 | uint8_t lock; |
a8170e5e | 56 | hwaddr size; |
432d268c JN |
57 | struct MapCacheEntry *next; |
58 | } MapCacheEntry; | |
59 | ||
60 | typedef struct MapCacheRev { | |
61 | uint8_t *vaddr_req; | |
a8170e5e AK |
62 | hwaddr paddr_index; |
63 | hwaddr size; | |
432d268c JN |
64 | QTAILQ_ENTRY(MapCacheRev) next; |
65 | } MapCacheRev; | |
66 | ||
67 | typedef struct MapCache { | |
68 | MapCacheEntry *entry; | |
69 | unsigned long nr_buckets; | |
70 | QTAILQ_HEAD(map_cache_head, MapCacheRev) locked_entries; | |
71 | ||
72 | /* For most cases (>99.9%), the page address is the same. */ | |
e2deee3e | 73 | MapCacheEntry *last_entry; |
432d268c JN |
74 | unsigned long max_mcache_size; |
75 | unsigned int mcache_bucket_shift; | |
cd1ba7de AP |
76 | |
77 | phys_offset_to_gaddr_t phys_offset_to_gaddr; | |
86a6a9bf | 78 | QemuMutex lock; |
cd1ba7de | 79 | void *opaque; |
432d268c JN |
80 | } MapCache; |
81 | ||
82 | static MapCache *mapcache; | |
83 | ||
86a6a9bf PB |
84 | static inline void mapcache_lock(void) |
85 | { | |
86 | qemu_mutex_lock(&mapcache->lock); | |
87 | } | |
88 | ||
89 | static inline void mapcache_unlock(void) | |
90 | { | |
91 | qemu_mutex_unlock(&mapcache->lock); | |
92 | } | |
93 | ||
c13390cd SS |
94 | static inline int test_bits(int nr, int size, const unsigned long *addr) |
95 | { | |
96 | unsigned long res = find_next_zero_bit(addr, size + nr, nr); | |
97 | if (res >= nr + size) | |
98 | return 1; | |
99 | else | |
100 | return 0; | |
101 | } | |
102 | ||
cd1ba7de | 103 | void xen_map_cache_init(phys_offset_to_gaddr_t f, void *opaque) |
432d268c JN |
104 | { |
105 | unsigned long size; | |
106 | struct rlimit rlimit_as; | |
107 | ||
7267c094 | 108 | mapcache = g_malloc0(sizeof (MapCache)); |
432d268c | 109 | |
cd1ba7de AP |
110 | mapcache->phys_offset_to_gaddr = f; |
111 | mapcache->opaque = opaque; | |
86a6a9bf | 112 | qemu_mutex_init(&mapcache->lock); |
cd1ba7de | 113 | |
432d268c | 114 | QTAILQ_INIT(&mapcache->locked_entries); |
432d268c | 115 | |
56c119e5 AP |
116 | if (geteuid() == 0) { |
117 | rlimit_as.rlim_cur = RLIM_INFINITY; | |
118 | rlimit_as.rlim_max = RLIM_INFINITY; | |
119 | mapcache->max_mcache_size = MCACHE_MAX_SIZE; | |
ea6c5f8f | 120 | } else { |
56c119e5 AP |
121 | getrlimit(RLIMIT_AS, &rlimit_as); |
122 | rlimit_as.rlim_cur = rlimit_as.rlim_max; | |
123 | ||
124 | if (rlimit_as.rlim_max != RLIM_INFINITY) { | |
125 | fprintf(stderr, "Warning: QEMU's maximum size of virtual" | |
126 | " memory is not infinity.\n"); | |
127 | } | |
128 | if (rlimit_as.rlim_max < MCACHE_MAX_SIZE + NON_MCACHE_MEMORY_SIZE) { | |
129 | mapcache->max_mcache_size = rlimit_as.rlim_max - | |
130 | NON_MCACHE_MEMORY_SIZE; | |
131 | } else { | |
132 | mapcache->max_mcache_size = MCACHE_MAX_SIZE; | |
133 | } | |
ea6c5f8f JB |
134 | } |
135 | ||
432d268c | 136 | setrlimit(RLIMIT_AS, &rlimit_as); |
432d268c JN |
137 | |
138 | mapcache->nr_buckets = | |
139 | (((mapcache->max_mcache_size >> XC_PAGE_SHIFT) + | |
140 | (1UL << (MCACHE_BUCKET_SHIFT - XC_PAGE_SHIFT)) - 1) >> | |
141 | (MCACHE_BUCKET_SHIFT - XC_PAGE_SHIFT)); | |
142 | ||
143 | size = mapcache->nr_buckets * sizeof (MapCacheEntry); | |
144 | size = (size + XC_PAGE_SIZE - 1) & ~(XC_PAGE_SIZE - 1); | |
e41d7c69 JK |
145 | DPRINTF("%s, nr_buckets = %lx size %lu\n", __func__, |
146 | mapcache->nr_buckets, size); | |
7267c094 | 147 | mapcache->entry = g_malloc0(size); |
432d268c JN |
148 | } |
149 | ||
e41d7c69 | 150 | static void xen_remap_bucket(MapCacheEntry *entry, |
a8170e5e AK |
151 | hwaddr size, |
152 | hwaddr address_index) | |
432d268c JN |
153 | { |
154 | uint8_t *vaddr_base; | |
155 | xen_pfn_t *pfns; | |
156 | int *err; | |
ea6c5f8f | 157 | unsigned int i; |
a8170e5e | 158 | hwaddr nb_pfn = size >> XC_PAGE_SHIFT; |
432d268c | 159 | |
e41d7c69 | 160 | trace_xen_remap_bucket(address_index); |
432d268c | 161 | |
7267c094 AL |
162 | pfns = g_malloc0(nb_pfn * sizeof (xen_pfn_t)); |
163 | err = g_malloc0(nb_pfn * sizeof (int)); | |
432d268c JN |
164 | |
165 | if (entry->vaddr_base != NULL) { | |
0987d735 | 166 | ram_block_notify_remove(entry->vaddr_base, entry->size); |
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 | |
0987d735 | 192 | ram_block_notify_add(entry->vaddr_base, entry->size); |
ea6c5f8f JB |
193 | bitmap_zero(entry->valid_mapping, nb_pfn); |
194 | for (i = 0; i < nb_pfn; i++) { | |
195 | if (!err[i]) { | |
196 | bitmap_set(entry->valid_mapping, i, 1); | |
432d268c | 197 | } |
432d268c JN |
198 | } |
199 | ||
7267c094 AL |
200 | g_free(pfns); |
201 | g_free(err); | |
432d268c JN |
202 | } |
203 | ||
86a6a9bf PB |
204 | static uint8_t *xen_map_cache_unlocked(hwaddr phys_addr, hwaddr size, |
205 | uint8_t lock) | |
432d268c JN |
206 | { |
207 | MapCacheEntry *entry, *pentry = NULL; | |
a8170e5e AK |
208 | hwaddr address_index; |
209 | hwaddr address_offset; | |
9b6d7b36 PB |
210 | hwaddr cache_size = size; |
211 | hwaddr test_bit_size; | |
cd1ba7de AP |
212 | bool translated = false; |
213 | ||
214 | tryagain: | |
215 | address_index = phys_addr >> MCACHE_BUCKET_SHIFT; | |
216 | address_offset = phys_addr & (MCACHE_BUCKET_SIZE - 1); | |
432d268c | 217 | |
e41d7c69 | 218 | trace_xen_map_cache(phys_addr); |
432d268c | 219 | |
9b6d7b36 | 220 | /* test_bit_size is always a multiple of XC_PAGE_SIZE */ |
044d4e1a | 221 | if (size) { |
9b6d7b36 | 222 | test_bit_size = size + (phys_addr & (XC_PAGE_SIZE - 1)); |
044d4e1a | 223 | |
9b6d7b36 PB |
224 | if (test_bit_size % XC_PAGE_SIZE) { |
225 | test_bit_size += XC_PAGE_SIZE - (test_bit_size % XC_PAGE_SIZE); | |
044d4e1a H |
226 | } |
227 | } else { | |
9b6d7b36 | 228 | test_bit_size = XC_PAGE_SIZE; |
044d4e1a H |
229 | } |
230 | ||
e2deee3e SS |
231 | if (mapcache->last_entry != NULL && |
232 | mapcache->last_entry->paddr_index == address_index && | |
9b6d7b36 | 233 | !lock && !size && |
044d4e1a | 234 | test_bits(address_offset >> XC_PAGE_SHIFT, |
9b6d7b36 | 235 | test_bit_size >> XC_PAGE_SHIFT, |
044d4e1a | 236 | mapcache->last_entry->valid_mapping)) { |
e2deee3e SS |
237 | trace_xen_map_cache_return(mapcache->last_entry->vaddr_base + address_offset); |
238 | return mapcache->last_entry->vaddr_base + address_offset; | |
432d268c JN |
239 | } |
240 | ||
c13390cd | 241 | /* size is always a multiple of MCACHE_BUCKET_SIZE */ |
09ab48ee | 242 | if (size) { |
9b6d7b36 PB |
243 | cache_size = size + address_offset; |
244 | if (cache_size % MCACHE_BUCKET_SIZE) { | |
245 | cache_size += MCACHE_BUCKET_SIZE - (cache_size % MCACHE_BUCKET_SIZE); | |
09ab48ee AP |
246 | } |
247 | } else { | |
9b6d7b36 | 248 | cache_size = MCACHE_BUCKET_SIZE; |
09ab48ee | 249 | } |
c13390cd | 250 | |
432d268c JN |
251 | entry = &mapcache->entry[address_index % mapcache->nr_buckets]; |
252 | ||
c13390cd | 253 | while (entry && entry->lock && entry->vaddr_base && |
9b6d7b36 | 254 | (entry->paddr_index != address_index || entry->size != cache_size || |
044d4e1a | 255 | !test_bits(address_offset >> XC_PAGE_SHIFT, |
9b6d7b36 | 256 | test_bit_size >> XC_PAGE_SHIFT, |
c13390cd | 257 | entry->valid_mapping))) { |
432d268c JN |
258 | pentry = entry; |
259 | entry = entry->next; | |
260 | } | |
261 | if (!entry) { | |
7267c094 | 262 | entry = g_malloc0(sizeof (MapCacheEntry)); |
432d268c | 263 | pentry->next = entry; |
9b6d7b36 | 264 | xen_remap_bucket(entry, cache_size, address_index); |
432d268c JN |
265 | } else if (!entry->lock) { |
266 | if (!entry->vaddr_base || entry->paddr_index != address_index || | |
9b6d7b36 | 267 | entry->size != cache_size || |
044d4e1a | 268 | !test_bits(address_offset >> XC_PAGE_SHIFT, |
9b6d7b36 | 269 | test_bit_size >> XC_PAGE_SHIFT, |
c13390cd | 270 | entry->valid_mapping)) { |
9b6d7b36 | 271 | xen_remap_bucket(entry, cache_size, address_index); |
432d268c JN |
272 | } |
273 | } | |
274 | ||
044d4e1a | 275 | if(!test_bits(address_offset >> XC_PAGE_SHIFT, |
9b6d7b36 | 276 | test_bit_size >> XC_PAGE_SHIFT, |
c13390cd | 277 | entry->valid_mapping)) { |
e2deee3e | 278 | mapcache->last_entry = NULL; |
cd1ba7de AP |
279 | if (!translated && mapcache->phys_offset_to_gaddr) { |
280 | phys_addr = mapcache->phys_offset_to_gaddr(phys_addr, size, mapcache->opaque); | |
281 | translated = true; | |
282 | goto tryagain; | |
283 | } | |
e41d7c69 | 284 | trace_xen_map_cache_return(NULL); |
432d268c JN |
285 | return NULL; |
286 | } | |
287 | ||
e2deee3e | 288 | mapcache->last_entry = entry; |
432d268c | 289 | if (lock) { |
7267c094 | 290 | MapCacheRev *reventry = g_malloc0(sizeof(MapCacheRev)); |
432d268c | 291 | entry->lock++; |
e2deee3e SS |
292 | reventry->vaddr_req = mapcache->last_entry->vaddr_base + address_offset; |
293 | reventry->paddr_index = mapcache->last_entry->paddr_index; | |
c13390cd | 294 | reventry->size = entry->size; |
432d268c JN |
295 | QTAILQ_INSERT_HEAD(&mapcache->locked_entries, reventry, next); |
296 | } | |
297 | ||
e2deee3e SS |
298 | trace_xen_map_cache_return(mapcache->last_entry->vaddr_base + address_offset); |
299 | return mapcache->last_entry->vaddr_base + address_offset; | |
432d268c JN |
300 | } |
301 | ||
86a6a9bf PB |
302 | uint8_t *xen_map_cache(hwaddr phys_addr, hwaddr size, |
303 | uint8_t lock) | |
304 | { | |
305 | uint8_t *p; | |
306 | ||
307 | mapcache_lock(); | |
308 | p = xen_map_cache_unlocked(phys_addr, size, lock); | |
309 | mapcache_unlock(); | |
310 | return p; | |
311 | } | |
312 | ||
e41d7c69 | 313 | ram_addr_t xen_ram_addr_from_mapcache(void *ptr) |
432d268c | 314 | { |
ecf169b7 | 315 | MapCacheEntry *entry = NULL; |
432d268c | 316 | MapCacheRev *reventry; |
a8170e5e AK |
317 | hwaddr paddr_index; |
318 | hwaddr size; | |
86a6a9bf | 319 | ram_addr_t raddr; |
432d268c JN |
320 | int found = 0; |
321 | ||
86a6a9bf | 322 | mapcache_lock(); |
432d268c JN |
323 | QTAILQ_FOREACH(reventry, &mapcache->locked_entries, next) { |
324 | if (reventry->vaddr_req == ptr) { | |
325 | paddr_index = reventry->paddr_index; | |
c13390cd | 326 | size = reventry->size; |
432d268c JN |
327 | found = 1; |
328 | break; | |
329 | } | |
330 | } | |
331 | if (!found) { | |
e41d7c69 | 332 | fprintf(stderr, "%s, could not find %p\n", __func__, ptr); |
432d268c JN |
333 | QTAILQ_FOREACH(reventry, &mapcache->locked_entries, next) { |
334 | DPRINTF(" "TARGET_FMT_plx" -> %p is present\n", reventry->paddr_index, | |
335 | reventry->vaddr_req); | |
336 | } | |
337 | abort(); | |
338 | return 0; | |
339 | } | |
340 | ||
c13390cd SS |
341 | entry = &mapcache->entry[paddr_index % mapcache->nr_buckets]; |
342 | while (entry && (entry->paddr_index != paddr_index || entry->size != size)) { | |
c13390cd SS |
343 | entry = entry->next; |
344 | } | |
345 | if (!entry) { | |
346 | DPRINTF("Trying to find address %p that is not in the mapcache!\n", ptr); | |
86a6a9bf PB |
347 | raddr = 0; |
348 | } else { | |
349 | raddr = (reventry->paddr_index << MCACHE_BUCKET_SHIFT) + | |
350 | ((unsigned long) ptr - (unsigned long) entry->vaddr_base); | |
c13390cd | 351 | } |
86a6a9bf PB |
352 | mapcache_unlock(); |
353 | return raddr; | |
432d268c JN |
354 | } |
355 | ||
86a6a9bf | 356 | static void xen_invalidate_map_cache_entry_unlocked(uint8_t *buffer) |
432d268c JN |
357 | { |
358 | MapCacheEntry *entry = NULL, *pentry = NULL; | |
359 | MapCacheRev *reventry; | |
a8170e5e AK |
360 | hwaddr paddr_index; |
361 | hwaddr size; | |
432d268c JN |
362 | int found = 0; |
363 | ||
432d268c JN |
364 | QTAILQ_FOREACH(reventry, &mapcache->locked_entries, next) { |
365 | if (reventry->vaddr_req == buffer) { | |
366 | paddr_index = reventry->paddr_index; | |
c13390cd | 367 | size = reventry->size; |
432d268c JN |
368 | found = 1; |
369 | break; | |
370 | } | |
371 | } | |
372 | if (!found) { | |
e41d7c69 | 373 | DPRINTF("%s, could not find %p\n", __func__, buffer); |
432d268c JN |
374 | QTAILQ_FOREACH(reventry, &mapcache->locked_entries, next) { |
375 | DPRINTF(" "TARGET_FMT_plx" -> %p is present\n", reventry->paddr_index, reventry->vaddr_req); | |
376 | } | |
377 | return; | |
378 | } | |
379 | QTAILQ_REMOVE(&mapcache->locked_entries, reventry, next); | |
7267c094 | 380 | g_free(reventry); |
432d268c | 381 | |
e2deee3e SS |
382 | if (mapcache->last_entry != NULL && |
383 | mapcache->last_entry->paddr_index == paddr_index) { | |
384 | mapcache->last_entry = NULL; | |
27b7652e FZ |
385 | } |
386 | ||
432d268c | 387 | entry = &mapcache->entry[paddr_index % mapcache->nr_buckets]; |
c13390cd | 388 | while (entry && (entry->paddr_index != paddr_index || entry->size != size)) { |
432d268c JN |
389 | pentry = entry; |
390 | entry = entry->next; | |
391 | } | |
392 | if (!entry) { | |
393 | DPRINTF("Trying to unmap address %p that is not in the mapcache!\n", buffer); | |
394 | return; | |
395 | } | |
396 | entry->lock--; | |
397 | if (entry->lock > 0 || pentry == NULL) { | |
398 | return; | |
399 | } | |
400 | ||
401 | pentry->next = entry->next; | |
0987d735 | 402 | ram_block_notify_remove(entry->vaddr_base, entry->size); |
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 | } |