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 #include "qemu/units.h"
13 #include "qemu/error-report.h"
14 
15 #include <sys/resource.h>
16 
17 #include "hw/xen/xen-legacy-backend.h"
18 #include "qemu/bitmap.h"
19 
20 #include "sysemu/runstate.h"
21 #include "sysemu/xen-mapcache.h"
22 #include "trace.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 * MiB)
50 
51 typedef struct MapCacheEntry {
52     hwaddr paddr_index;
53     uint8_t *vaddr_base;
54     unsigned long *valid_mapping;
55     uint8_t lock;
56 #define XEN_MAPCACHE_ENTRY_DUMMY (1 << 0)
57     uint8_t flags;
58     hwaddr size;
59     struct MapCacheEntry *next;
60 } MapCacheEntry;
61 
62 typedef struct MapCacheRev {
63     uint8_t *vaddr_req;
64     hwaddr paddr_index;
65     hwaddr size;
66     QTAILQ_ENTRY(MapCacheRev) next;
67     bool dma;
68 } MapCacheRev;
69 
70 typedef struct MapCache {
71     MapCacheEntry *entry;
72     unsigned long nr_buckets;
73     QTAILQ_HEAD(, MapCacheRev) locked_entries;
74 
75     /* For most cases (>99.9%), the page address is the same. */
76     MapCacheEntry *last_entry;
77     unsigned long max_mcache_size;
78     unsigned int mcache_bucket_shift;
79 
80     phys_offset_to_gaddr_t phys_offset_to_gaddr;
81     QemuMutex lock;
82     void *opaque;
83 } MapCache;
84 
85 static MapCache *mapcache;
86 
mapcache_lock(void)87 static inline void mapcache_lock(void)
88 {
89     qemu_mutex_lock(&mapcache->lock);
90 }
91 
mapcache_unlock(void)92 static inline void mapcache_unlock(void)
93 {
94     qemu_mutex_unlock(&mapcache->lock);
95 }
96 
test_bits(int nr,int size,const unsigned long * addr)97 static inline int test_bits(int nr, int size, const unsigned long *addr)
98 {
99     unsigned long res = find_next_zero_bit(addr, size + nr, nr);
100     if (res >= nr + size)
101         return 1;
102     else
103         return 0;
104 }
105 
xen_map_cache_init(phys_offset_to_gaddr_t f,void * opaque)106 void xen_map_cache_init(phys_offset_to_gaddr_t f, void *opaque)
107 {
108     unsigned long size;
109     struct rlimit rlimit_as;
110 
111     mapcache = g_malloc0(sizeof (MapCache));
112 
113     mapcache->phys_offset_to_gaddr = f;
114     mapcache->opaque = opaque;
115     qemu_mutex_init(&mapcache->lock);
116 
117     QTAILQ_INIT(&mapcache->locked_entries);
118 
119     if (geteuid() == 0) {
120         rlimit_as.rlim_cur = RLIM_INFINITY;
121         rlimit_as.rlim_max = RLIM_INFINITY;
122         mapcache->max_mcache_size = MCACHE_MAX_SIZE;
123     } else {
124         getrlimit(RLIMIT_AS, &rlimit_as);
125         rlimit_as.rlim_cur = rlimit_as.rlim_max;
126 
127         if (rlimit_as.rlim_max != RLIM_INFINITY) {
128             warn_report("QEMU's maximum size of virtual"
129                         " memory is not infinity");
130         }
131         if (rlimit_as.rlim_max < MCACHE_MAX_SIZE + NON_MCACHE_MEMORY_SIZE) {
132             mapcache->max_mcache_size = rlimit_as.rlim_max -
133                 NON_MCACHE_MEMORY_SIZE;
134         } else {
135             mapcache->max_mcache_size = MCACHE_MAX_SIZE;
136         }
137     }
138 
139     setrlimit(RLIMIT_AS, &rlimit_as);
140 
141     mapcache->nr_buckets =
142         (((mapcache->max_mcache_size >> XC_PAGE_SHIFT) +
143           (1UL << (MCACHE_BUCKET_SHIFT - XC_PAGE_SHIFT)) - 1) >>
144          (MCACHE_BUCKET_SHIFT - XC_PAGE_SHIFT));
145 
146     size = mapcache->nr_buckets * sizeof (MapCacheEntry);
147     size = (size + XC_PAGE_SIZE - 1) & ~(XC_PAGE_SIZE - 1);
148     DPRINTF("%s, nr_buckets = %lx size %lu\n", __func__,
149             mapcache->nr_buckets, size);
150     mapcache->entry = g_malloc0(size);
151 }
152 
xen_remap_bucket(MapCacheEntry * entry,void * vaddr,hwaddr size,hwaddr address_index,bool dummy)153 static void xen_remap_bucket(MapCacheEntry *entry,
154                              void *vaddr,
155                              hwaddr size,
156                              hwaddr address_index,
157                              bool dummy)
158 {
159     uint8_t *vaddr_base;
160     xen_pfn_t *pfns;
161     int *err;
162     unsigned int i;
163     hwaddr nb_pfn = size >> XC_PAGE_SHIFT;
164 
165     trace_xen_remap_bucket(address_index);
166 
167     pfns = g_malloc0(nb_pfn * sizeof (xen_pfn_t));
168     err = g_malloc0(nb_pfn * sizeof (int));
169 
170     if (entry->vaddr_base != NULL) {
171         if (!(entry->flags & XEN_MAPCACHE_ENTRY_DUMMY)) {
172             ram_block_notify_remove(entry->vaddr_base, entry->size);
173         }
174         if (munmap(entry->vaddr_base, entry->size) != 0) {
175             perror("unmap fails");
176             exit(-1);
177         }
178     }
179     g_free(entry->valid_mapping);
180     entry->valid_mapping = NULL;
181 
182     for (i = 0; i < nb_pfn; i++) {
183         pfns[i] = (address_index << (MCACHE_BUCKET_SHIFT-XC_PAGE_SHIFT)) + i;
184     }
185 
186     /*
187      * If the caller has requested the mapping at a specific address use
188      * MAP_FIXED to make sure it's honored.
189      */
190     if (!dummy) {
191         vaddr_base = xenforeignmemory_map2(xen_fmem, xen_domid, vaddr,
192                                            PROT_READ | PROT_WRITE,
193                                            vaddr ? MAP_FIXED : 0,
194                                            nb_pfn, pfns, err);
195         if (vaddr_base == NULL) {
196             perror("xenforeignmemory_map2");
197             exit(-1);
198         }
199     } else {
200         /*
201          * We create dummy mappings where we are unable to create a foreign
202          * mapping immediately due to certain circumstances (i.e. on resume now)
203          */
204         vaddr_base = mmap(vaddr, size, PROT_READ | PROT_WRITE,
205                           MAP_ANON | MAP_SHARED | (vaddr ? MAP_FIXED : 0),
206                           -1, 0);
207         if (vaddr_base == MAP_FAILED) {
208             perror("mmap");
209             exit(-1);
210         }
211     }
212 
213     if (!(entry->flags & XEN_MAPCACHE_ENTRY_DUMMY)) {
214         ram_block_notify_add(vaddr_base, size);
215     }
216 
217     entry->vaddr_base = vaddr_base;
218     entry->paddr_index = address_index;
219     entry->size = size;
220     entry->valid_mapping = (unsigned long *) g_malloc0(sizeof(unsigned long) *
221             BITS_TO_LONGS(size >> XC_PAGE_SHIFT));
222 
223     if (dummy) {
224         entry->flags |= XEN_MAPCACHE_ENTRY_DUMMY;
225     } else {
226         entry->flags &= ~(XEN_MAPCACHE_ENTRY_DUMMY);
227     }
228 
229     bitmap_zero(entry->valid_mapping, nb_pfn);
230     for (i = 0; i < nb_pfn; i++) {
231         if (!err[i]) {
232             bitmap_set(entry->valid_mapping, i, 1);
233         }
234     }
235 
236     g_free(pfns);
237     g_free(err);
238 }
239 
xen_map_cache_unlocked(hwaddr phys_addr,hwaddr size,uint8_t lock,bool dma)240 static uint8_t *xen_map_cache_unlocked(hwaddr phys_addr, hwaddr size,
241                                        uint8_t lock, bool dma)
242 {
243     MapCacheEntry *entry, *pentry = NULL,
244                   *free_entry = NULL, *free_pentry = NULL;
245     hwaddr address_index;
246     hwaddr address_offset;
247     hwaddr cache_size = size;
248     hwaddr test_bit_size;
249     bool translated G_GNUC_UNUSED = false;
250     bool dummy = false;
251 
252 tryagain:
253     address_index  = phys_addr >> MCACHE_BUCKET_SHIFT;
254     address_offset = phys_addr & (MCACHE_BUCKET_SIZE - 1);
255 
256     trace_xen_map_cache(phys_addr);
257 
258     /* test_bit_size is always a multiple of XC_PAGE_SIZE */
259     if (size) {
260         test_bit_size = size + (phys_addr & (XC_PAGE_SIZE - 1));
261 
262         if (test_bit_size % XC_PAGE_SIZE) {
263             test_bit_size += XC_PAGE_SIZE - (test_bit_size % XC_PAGE_SIZE);
264         }
265     } else {
266         test_bit_size = XC_PAGE_SIZE;
267     }
268 
269     if (mapcache->last_entry != NULL &&
270         mapcache->last_entry->paddr_index == address_index &&
271         !lock && !size &&
272         test_bits(address_offset >> XC_PAGE_SHIFT,
273                   test_bit_size >> XC_PAGE_SHIFT,
274                   mapcache->last_entry->valid_mapping)) {
275         trace_xen_map_cache_return(mapcache->last_entry->vaddr_base + address_offset);
276         return mapcache->last_entry->vaddr_base + address_offset;
277     }
278 
279     /* size is always a multiple of MCACHE_BUCKET_SIZE */
280     if (size) {
281         cache_size = size + address_offset;
282         if (cache_size % MCACHE_BUCKET_SIZE) {
283             cache_size += MCACHE_BUCKET_SIZE - (cache_size % MCACHE_BUCKET_SIZE);
284         }
285     } else {
286         cache_size = MCACHE_BUCKET_SIZE;
287     }
288 
289     entry = &mapcache->entry[address_index % mapcache->nr_buckets];
290 
291     while (entry && (lock || entry->lock) && entry->vaddr_base &&
292             (entry->paddr_index != address_index || entry->size != cache_size ||
293              !test_bits(address_offset >> XC_PAGE_SHIFT,
294                  test_bit_size >> XC_PAGE_SHIFT,
295                  entry->valid_mapping))) {
296         if (!free_entry && !entry->lock) {
297             free_entry = entry;
298             free_pentry = pentry;
299         }
300         pentry = entry;
301         entry = entry->next;
302     }
303     if (!entry && free_entry) {
304         entry = free_entry;
305         pentry = free_pentry;
306     }
307     if (!entry) {
308         entry = g_malloc0(sizeof (MapCacheEntry));
309         pentry->next = entry;
310         xen_remap_bucket(entry, NULL, cache_size, address_index, dummy);
311     } else if (!entry->lock) {
312         if (!entry->vaddr_base || entry->paddr_index != address_index ||
313                 entry->size != cache_size ||
314                 !test_bits(address_offset >> XC_PAGE_SHIFT,
315                     test_bit_size >> XC_PAGE_SHIFT,
316                     entry->valid_mapping)) {
317             xen_remap_bucket(entry, NULL, cache_size, address_index, dummy);
318         }
319     }
320 
321     if(!test_bits(address_offset >> XC_PAGE_SHIFT,
322                 test_bit_size >> XC_PAGE_SHIFT,
323                 entry->valid_mapping)) {
324         mapcache->last_entry = NULL;
325 #ifdef XEN_COMPAT_PHYSMAP
326         if (!translated && mapcache->phys_offset_to_gaddr) {
327             phys_addr = mapcache->phys_offset_to_gaddr(phys_addr, size);
328             translated = true;
329             goto tryagain;
330         }
331 #endif
332         if (!dummy && runstate_check(RUN_STATE_INMIGRATE)) {
333             dummy = true;
334             goto tryagain;
335         }
336         trace_xen_map_cache_return(NULL);
337         return NULL;
338     }
339 
340     mapcache->last_entry = entry;
341     if (lock) {
342         MapCacheRev *reventry = g_malloc0(sizeof(MapCacheRev));
343         entry->lock++;
344         reventry->dma = dma;
345         reventry->vaddr_req = mapcache->last_entry->vaddr_base + address_offset;
346         reventry->paddr_index = mapcache->last_entry->paddr_index;
347         reventry->size = entry->size;
348         QTAILQ_INSERT_HEAD(&mapcache->locked_entries, reventry, next);
349     }
350 
351     trace_xen_map_cache_return(mapcache->last_entry->vaddr_base + address_offset);
352     return mapcache->last_entry->vaddr_base + address_offset;
353 }
354 
xen_map_cache(hwaddr phys_addr,hwaddr size,uint8_t lock,bool dma)355 uint8_t *xen_map_cache(hwaddr phys_addr, hwaddr size,
356                        uint8_t lock, bool dma)
357 {
358     uint8_t *p;
359 
360     mapcache_lock();
361     p = xen_map_cache_unlocked(phys_addr, size, lock, dma);
362     mapcache_unlock();
363     return p;
364 }
365 
xen_ram_addr_from_mapcache(void * ptr)366 ram_addr_t xen_ram_addr_from_mapcache(void *ptr)
367 {
368     MapCacheEntry *entry = NULL;
369     MapCacheRev *reventry;
370     hwaddr paddr_index;
371     hwaddr size;
372     ram_addr_t raddr;
373     int found = 0;
374 
375     mapcache_lock();
376     QTAILQ_FOREACH(reventry, &mapcache->locked_entries, next) {
377         if (reventry->vaddr_req == ptr) {
378             paddr_index = reventry->paddr_index;
379             size = reventry->size;
380             found = 1;
381             break;
382         }
383     }
384     if (!found) {
385         fprintf(stderr, "%s, could not find %p\n", __func__, ptr);
386         QTAILQ_FOREACH(reventry, &mapcache->locked_entries, next) {
387             DPRINTF("   "TARGET_FMT_plx" -> %p is present\n", reventry->paddr_index,
388                     reventry->vaddr_req);
389         }
390         abort();
391         return 0;
392     }
393 
394     entry = &mapcache->entry[paddr_index % mapcache->nr_buckets];
395     while (entry && (entry->paddr_index != paddr_index || entry->size != size)) {
396         entry = entry->next;
397     }
398     if (!entry) {
399         DPRINTF("Trying to find address %p that is not in the mapcache!\n", ptr);
400         raddr = 0;
401     } else {
402         raddr = (reventry->paddr_index << MCACHE_BUCKET_SHIFT) +
403              ((unsigned long) ptr - (unsigned long) entry->vaddr_base);
404     }
405     mapcache_unlock();
406     return raddr;
407 }
408 
xen_invalidate_map_cache_entry_unlocked(uint8_t * buffer)409 static void xen_invalidate_map_cache_entry_unlocked(uint8_t *buffer)
410 {
411     MapCacheEntry *entry = NULL, *pentry = NULL;
412     MapCacheRev *reventry;
413     hwaddr paddr_index;
414     hwaddr size;
415     int found = 0;
416 
417     QTAILQ_FOREACH(reventry, &mapcache->locked_entries, next) {
418         if (reventry->vaddr_req == buffer) {
419             paddr_index = reventry->paddr_index;
420             size = reventry->size;
421             found = 1;
422             break;
423         }
424     }
425     if (!found) {
426         DPRINTF("%s, could not find %p\n", __func__, buffer);
427         QTAILQ_FOREACH(reventry, &mapcache->locked_entries, next) {
428             DPRINTF("   "TARGET_FMT_plx" -> %p is present\n", reventry->paddr_index, reventry->vaddr_req);
429         }
430         return;
431     }
432     QTAILQ_REMOVE(&mapcache->locked_entries, reventry, next);
433     g_free(reventry);
434 
435     if (mapcache->last_entry != NULL &&
436         mapcache->last_entry->paddr_index == paddr_index) {
437         mapcache->last_entry = NULL;
438     }
439 
440     entry = &mapcache->entry[paddr_index % mapcache->nr_buckets];
441     while (entry && (entry->paddr_index != paddr_index || entry->size != size)) {
442         pentry = entry;
443         entry = entry->next;
444     }
445     if (!entry) {
446         DPRINTF("Trying to unmap address %p that is not in the mapcache!\n", buffer);
447         return;
448     }
449     entry->lock--;
450     if (entry->lock > 0 || pentry == NULL) {
451         return;
452     }
453 
454     pentry->next = entry->next;
455     ram_block_notify_remove(entry->vaddr_base, entry->size);
456     if (munmap(entry->vaddr_base, entry->size) != 0) {
457         perror("unmap fails");
458         exit(-1);
459     }
460     g_free(entry->valid_mapping);
461     g_free(entry);
462 }
463 
xen_invalidate_map_cache_entry(uint8_t * buffer)464 void xen_invalidate_map_cache_entry(uint8_t *buffer)
465 {
466     mapcache_lock();
467     xen_invalidate_map_cache_entry_unlocked(buffer);
468     mapcache_unlock();
469 }
470 
xen_invalidate_map_cache(void)471 void xen_invalidate_map_cache(void)
472 {
473     unsigned long i;
474     MapCacheRev *reventry;
475 
476     /* Flush pending AIO before destroying the mapcache */
477     bdrv_drain_all();
478 
479     mapcache_lock();
480 
481     QTAILQ_FOREACH(reventry, &mapcache->locked_entries, next) {
482         if (!reventry->dma) {
483             continue;
484         }
485         fprintf(stderr, "Locked DMA mapping while invalidating mapcache!"
486                 " "TARGET_FMT_plx" -> %p is present\n",
487                 reventry->paddr_index, reventry->vaddr_req);
488     }
489 
490     for (i = 0; i < mapcache->nr_buckets; i++) {
491         MapCacheEntry *entry = &mapcache->entry[i];
492 
493         if (entry->vaddr_base == NULL) {
494             continue;
495         }
496         if (entry->lock > 0) {
497             continue;
498         }
499 
500         if (munmap(entry->vaddr_base, entry->size) != 0) {
501             perror("unmap fails");
502             exit(-1);
503         }
504 
505         entry->paddr_index = 0;
506         entry->vaddr_base = NULL;
507         entry->size = 0;
508         g_free(entry->valid_mapping);
509         entry->valid_mapping = NULL;
510     }
511 
512     mapcache->last_entry = NULL;
513 
514     mapcache_unlock();
515 }
516 
xen_replace_cache_entry_unlocked(hwaddr old_phys_addr,hwaddr new_phys_addr,hwaddr size)517 static uint8_t *xen_replace_cache_entry_unlocked(hwaddr old_phys_addr,
518                                                  hwaddr new_phys_addr,
519                                                  hwaddr size)
520 {
521     MapCacheEntry *entry;
522     hwaddr address_index, address_offset;
523     hwaddr test_bit_size, cache_size = size;
524 
525     address_index  = old_phys_addr >> MCACHE_BUCKET_SHIFT;
526     address_offset = old_phys_addr & (MCACHE_BUCKET_SIZE - 1);
527 
528     assert(size);
529     /* test_bit_size is always a multiple of XC_PAGE_SIZE */
530     test_bit_size = size + (old_phys_addr & (XC_PAGE_SIZE - 1));
531     if (test_bit_size % XC_PAGE_SIZE) {
532         test_bit_size += XC_PAGE_SIZE - (test_bit_size % XC_PAGE_SIZE);
533     }
534     cache_size = size + address_offset;
535     if (cache_size % MCACHE_BUCKET_SIZE) {
536         cache_size += MCACHE_BUCKET_SIZE - (cache_size % MCACHE_BUCKET_SIZE);
537     }
538 
539     entry = &mapcache->entry[address_index % mapcache->nr_buckets];
540     while (entry && !(entry->paddr_index == address_index &&
541                       entry->size == cache_size)) {
542         entry = entry->next;
543     }
544     if (!entry) {
545         DPRINTF("Trying to update an entry for "TARGET_FMT_plx \
546                 "that is not in the mapcache!\n", old_phys_addr);
547         return NULL;
548     }
549 
550     address_index  = new_phys_addr >> MCACHE_BUCKET_SHIFT;
551     address_offset = new_phys_addr & (MCACHE_BUCKET_SIZE - 1);
552 
553     fprintf(stderr, "Replacing a dummy mapcache entry for "TARGET_FMT_plx \
554             " with "TARGET_FMT_plx"\n", old_phys_addr, new_phys_addr);
555 
556     xen_remap_bucket(entry, entry->vaddr_base,
557                      cache_size, address_index, false);
558     if (!test_bits(address_offset >> XC_PAGE_SHIFT,
559                 test_bit_size >> XC_PAGE_SHIFT,
560                 entry->valid_mapping)) {
561         DPRINTF("Unable to update a mapcache entry for "TARGET_FMT_plx"!\n",
562                 old_phys_addr);
563         return NULL;
564     }
565 
566     return entry->vaddr_base + address_offset;
567 }
568 
xen_replace_cache_entry(hwaddr old_phys_addr,hwaddr new_phys_addr,hwaddr size)569 uint8_t *xen_replace_cache_entry(hwaddr old_phys_addr,
570                                  hwaddr new_phys_addr,
571                                  hwaddr size)
572 {
573     uint8_t *p;
574 
575     mapcache_lock();
576     p = xen_replace_cache_entry_unlocked(old_phys_addr, new_phys_addr, size);
577     mapcache_unlock();
578     return p;
579 }
580