qemu/hw/i386/xen/xen-mapcache.c
<<
>>
Prefs
   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
  13#include <sys/resource.h>
  14
  15#include "hw/xen/xen_backend.h"
  16#include "sysemu/blockdev.h"
  17#include "qemu/bitmap.h"
  18
  19#include <xen/hvm/params.h>
  20
  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 * 1024 * 1024)
  50
  51typedef 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
  62typedef 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
  70typedef struct MapCache {
  71    MapCacheEntry *entry;
  72    unsigned long nr_buckets;
  73    QTAILQ_HEAD(map_cache_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
  85static MapCache *mapcache;
  86
  87static inline void mapcache_lock(void)
  88{
  89    qemu_mutex_lock(&mapcache->lock);
  90}
  91
  92static inline void mapcache_unlock(void)
  93{
  94    qemu_mutex_unlock(&mapcache->lock);
  95}
  96
  97static 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
 106void 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            fprintf(stderr, "Warning: QEMU's maximum size of virtual"
 129                    " memory is not infinity.\n");
 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
 153static 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    if (!dummy) {
 187        vaddr_base = xenforeignmemory_map2(xen_fmem, xen_domid, vaddr,
 188                                           PROT_READ | PROT_WRITE, 0,
 189                                           nb_pfn, pfns, err);
 190        if (vaddr_base == NULL) {
 191            perror("xenforeignmemory_map2");
 192            exit(-1);
 193        }
 194    } else {
 195        /*
 196         * We create dummy mappings where we are unable to create a foreign
 197         * mapping immediately due to certain circumstances (i.e. on resume now)
 198         */
 199        vaddr_base = mmap(vaddr, size, PROT_READ | PROT_WRITE,
 200                          MAP_ANON | MAP_SHARED, -1, 0);
 201        if (vaddr_base == NULL) {
 202            perror("mmap");
 203            exit(-1);
 204        }
 205    }
 206
 207    if (!(entry->flags & XEN_MAPCACHE_ENTRY_DUMMY)) {
 208        ram_block_notify_add(vaddr_base, size);
 209    }
 210
 211    entry->vaddr_base = vaddr_base;
 212    entry->paddr_index = address_index;
 213    entry->size = size;
 214    entry->valid_mapping = (unsigned long *) g_malloc0(sizeof(unsigned long) *
 215            BITS_TO_LONGS(size >> XC_PAGE_SHIFT));
 216
 217    if (dummy) {
 218        entry->flags |= XEN_MAPCACHE_ENTRY_DUMMY;
 219    } else {
 220        entry->flags &= ~(XEN_MAPCACHE_ENTRY_DUMMY);
 221    }
 222
 223    bitmap_zero(entry->valid_mapping, nb_pfn);
 224    for (i = 0; i < nb_pfn; i++) {
 225        if (!err[i]) {
 226            bitmap_set(entry->valid_mapping, i, 1);
 227        }
 228    }
 229
 230    g_free(pfns);
 231    g_free(err);
 232}
 233
 234static uint8_t *xen_map_cache_unlocked(hwaddr phys_addr, hwaddr size,
 235                                       uint8_t lock, bool dma)
 236{
 237    MapCacheEntry *entry, *pentry = NULL,
 238                  *free_entry = NULL, *free_pentry = NULL;
 239    hwaddr address_index;
 240    hwaddr address_offset;
 241    hwaddr cache_size = size;
 242    hwaddr test_bit_size;
 243    bool translated G_GNUC_UNUSED = false;
 244    bool dummy = false;
 245
 246tryagain:
 247    address_index  = phys_addr >> MCACHE_BUCKET_SHIFT;
 248    address_offset = phys_addr & (MCACHE_BUCKET_SIZE - 1);
 249
 250    trace_xen_map_cache(phys_addr);
 251
 252    /* test_bit_size is always a multiple of XC_PAGE_SIZE */
 253    if (size) {
 254        test_bit_size = size + (phys_addr & (XC_PAGE_SIZE - 1));
 255
 256        if (test_bit_size % XC_PAGE_SIZE) {
 257            test_bit_size += XC_PAGE_SIZE - (test_bit_size % XC_PAGE_SIZE);
 258        }
 259    } else {
 260        test_bit_size = XC_PAGE_SIZE;
 261    }
 262
 263    if (mapcache->last_entry != NULL &&
 264        mapcache->last_entry->paddr_index == address_index &&
 265        !lock && !size &&
 266        test_bits(address_offset >> XC_PAGE_SHIFT,
 267                  test_bit_size >> XC_PAGE_SHIFT,
 268                  mapcache->last_entry->valid_mapping)) {
 269        trace_xen_map_cache_return(mapcache->last_entry->vaddr_base + address_offset);
 270        return mapcache->last_entry->vaddr_base + address_offset;
 271    }
 272
 273    /* size is always a multiple of MCACHE_BUCKET_SIZE */
 274    if (size) {
 275        cache_size = size + address_offset;
 276        if (cache_size % MCACHE_BUCKET_SIZE) {
 277            cache_size += MCACHE_BUCKET_SIZE - (cache_size % MCACHE_BUCKET_SIZE);
 278        }
 279    } else {
 280        cache_size = MCACHE_BUCKET_SIZE;
 281    }
 282
 283    entry = &mapcache->entry[address_index % mapcache->nr_buckets];
 284
 285    while (entry && (lock || entry->lock) && entry->vaddr_base &&
 286            (entry->paddr_index != address_index || entry->size != cache_size ||
 287             !test_bits(address_offset >> XC_PAGE_SHIFT,
 288                 test_bit_size >> XC_PAGE_SHIFT,
 289                 entry->valid_mapping))) {
 290        if (!free_entry && !entry->lock) {
 291            free_entry = entry;
 292            free_pentry = pentry;
 293        }
 294        pentry = entry;
 295        entry = entry->next;
 296    }
 297    if (!entry && free_entry) {
 298        entry = free_entry;
 299        pentry = free_pentry;
 300    }
 301    if (!entry) {
 302        entry = g_malloc0(sizeof (MapCacheEntry));
 303        pentry->next = entry;
 304        xen_remap_bucket(entry, NULL, cache_size, address_index, dummy);
 305    } else if (!entry->lock) {
 306        if (!entry->vaddr_base || entry->paddr_index != address_index ||
 307                entry->size != cache_size ||
 308                !test_bits(address_offset >> XC_PAGE_SHIFT,
 309                    test_bit_size >> XC_PAGE_SHIFT,
 310                    entry->valid_mapping)) {
 311            xen_remap_bucket(entry, NULL, cache_size, address_index, dummy);
 312        }
 313    }
 314
 315    if(!test_bits(address_offset >> XC_PAGE_SHIFT,
 316                test_bit_size >> XC_PAGE_SHIFT,
 317                entry->valid_mapping)) {
 318        mapcache->last_entry = NULL;
 319#ifdef XEN_COMPAT_PHYSMAP
 320        if (!translated && mapcache->phys_offset_to_gaddr) {
 321            phys_addr = mapcache->phys_offset_to_gaddr(phys_addr, size, mapcache->opaque);
 322            translated = true;
 323            goto tryagain;
 324        }
 325#endif
 326        if (!dummy && runstate_check(RUN_STATE_INMIGRATE)) {
 327            dummy = true;
 328            goto tryagain;
 329        }
 330        trace_xen_map_cache_return(NULL);
 331        return NULL;
 332    }
 333
 334    mapcache->last_entry = entry;
 335    if (lock) {
 336        MapCacheRev *reventry = g_malloc0(sizeof(MapCacheRev));
 337        entry->lock++;
 338        reventry->dma = dma;
 339        reventry->vaddr_req = mapcache->last_entry->vaddr_base + address_offset;
 340        reventry->paddr_index = mapcache->last_entry->paddr_index;
 341        reventry->size = entry->size;
 342        QTAILQ_INSERT_HEAD(&mapcache->locked_entries, reventry, next);
 343    }
 344
 345    trace_xen_map_cache_return(mapcache->last_entry->vaddr_base + address_offset);
 346    return mapcache->last_entry->vaddr_base + address_offset;
 347}
 348
 349uint8_t *xen_map_cache(hwaddr phys_addr, hwaddr size,
 350                       uint8_t lock, bool dma)
 351{
 352    uint8_t *p;
 353
 354    mapcache_lock();
 355    p = xen_map_cache_unlocked(phys_addr, size, lock, dma);
 356    mapcache_unlock();
 357    return p;
 358}
 359
 360ram_addr_t xen_ram_addr_from_mapcache(void *ptr)
 361{
 362    MapCacheEntry *entry = NULL;
 363    MapCacheRev *reventry;
 364    hwaddr paddr_index;
 365    hwaddr size;
 366    ram_addr_t raddr;
 367    int found = 0;
 368
 369    mapcache_lock();
 370    QTAILQ_FOREACH(reventry, &mapcache->locked_entries, next) {
 371        if (reventry->vaddr_req == ptr) {
 372            paddr_index = reventry->paddr_index;
 373            size = reventry->size;
 374            found = 1;
 375            break;
 376        }
 377    }
 378    if (!found) {
 379        fprintf(stderr, "%s, could not find %p\n", __func__, ptr);
 380        QTAILQ_FOREACH(reventry, &mapcache->locked_entries, next) {
 381            DPRINTF("   "TARGET_FMT_plx" -> %p is present\n", reventry->paddr_index,
 382                    reventry->vaddr_req);
 383        }
 384        abort();
 385        return 0;
 386    }
 387
 388    entry = &mapcache->entry[paddr_index % mapcache->nr_buckets];
 389    while (entry && (entry->paddr_index != paddr_index || entry->size != size)) {
 390        entry = entry->next;
 391    }
 392    if (!entry) {
 393        DPRINTF("Trying to find address %p that is not in the mapcache!\n", ptr);
 394        raddr = 0;
 395    } else {
 396        raddr = (reventry->paddr_index << MCACHE_BUCKET_SHIFT) +
 397             ((unsigned long) ptr - (unsigned long) entry->vaddr_base);
 398    }
 399    mapcache_unlock();
 400    return raddr;
 401}
 402
 403static void xen_invalidate_map_cache_entry_unlocked(uint8_t *buffer)
 404{
 405    MapCacheEntry *entry = NULL, *pentry = NULL;
 406    MapCacheRev *reventry;
 407    hwaddr paddr_index;
 408    hwaddr size;
 409    int found = 0;
 410
 411    QTAILQ_FOREACH(reventry, &mapcache->locked_entries, next) {
 412        if (reventry->vaddr_req == buffer) {
 413            paddr_index = reventry->paddr_index;
 414            size = reventry->size;
 415            found = 1;
 416            break;
 417        }
 418    }
 419    if (!found) {
 420        DPRINTF("%s, could not find %p\n", __func__, buffer);
 421        QTAILQ_FOREACH(reventry, &mapcache->locked_entries, next) {
 422            DPRINTF("   "TARGET_FMT_plx" -> %p is present\n", reventry->paddr_index, reventry->vaddr_req);
 423        }
 424        return;
 425    }
 426    QTAILQ_REMOVE(&mapcache->locked_entries, reventry, next);
 427    g_free(reventry);
 428
 429    if (mapcache->last_entry != NULL &&
 430        mapcache->last_entry->paddr_index == paddr_index) {
 431        mapcache->last_entry = NULL;
 432    }
 433
 434    entry = &mapcache->entry[paddr_index % mapcache->nr_buckets];
 435    while (entry && (entry->paddr_index != paddr_index || entry->size != size)) {
 436        pentry = entry;
 437        entry = entry->next;
 438    }
 439    if (!entry) {
 440        DPRINTF("Trying to unmap address %p that is not in the mapcache!\n", buffer);
 441        return;
 442    }
 443    entry->lock--;
 444    if (entry->lock > 0 || pentry == NULL) {
 445        return;
 446    }
 447
 448    pentry->next = entry->next;
 449    ram_block_notify_remove(entry->vaddr_base, entry->size);
 450    if (munmap(entry->vaddr_base, entry->size) != 0) {
 451        perror("unmap fails");
 452        exit(-1);
 453    }
 454    g_free(entry->valid_mapping);
 455    g_free(entry);
 456}
 457
 458void xen_invalidate_map_cache_entry(uint8_t *buffer)
 459{
 460    mapcache_lock();
 461    xen_invalidate_map_cache_entry_unlocked(buffer);
 462    mapcache_unlock();
 463}
 464
 465void xen_invalidate_map_cache(void)
 466{
 467    unsigned long i;
 468    MapCacheRev *reventry;
 469
 470    /* Flush pending AIO before destroying the mapcache */
 471    bdrv_drain_all();
 472
 473    mapcache_lock();
 474
 475    QTAILQ_FOREACH(reventry, &mapcache->locked_entries, next) {
 476        if (!reventry->dma) {
 477            continue;
 478        }
 479        fprintf(stderr, "Locked DMA mapping while invalidating mapcache!"
 480                " "TARGET_FMT_plx" -> %p is present\n",
 481                reventry->paddr_index, reventry->vaddr_req);
 482    }
 483
 484    for (i = 0; i < mapcache->nr_buckets; i++) {
 485        MapCacheEntry *entry = &mapcache->entry[i];
 486
 487        if (entry->vaddr_base == NULL) {
 488            continue;
 489        }
 490        if (entry->lock > 0) {
 491            continue;
 492        }
 493
 494        if (munmap(entry->vaddr_base, entry->size) != 0) {
 495            perror("unmap fails");
 496            exit(-1);
 497        }
 498
 499        entry->paddr_index = 0;
 500        entry->vaddr_base = NULL;
 501        entry->size = 0;
 502        g_free(entry->valid_mapping);
 503        entry->valid_mapping = NULL;
 504    }
 505
 506    mapcache->last_entry = NULL;
 507
 508    mapcache_unlock();
 509}
 510
 511static uint8_t *xen_replace_cache_entry_unlocked(hwaddr old_phys_addr,
 512                                                 hwaddr new_phys_addr,
 513                                                 hwaddr size)
 514{
 515    MapCacheEntry *entry;
 516    hwaddr address_index, address_offset;
 517    hwaddr test_bit_size, cache_size = size;
 518
 519    address_index  = old_phys_addr >> MCACHE_BUCKET_SHIFT;
 520    address_offset = old_phys_addr & (MCACHE_BUCKET_SIZE - 1);
 521
 522    assert(size);
 523    /* test_bit_size is always a multiple of XC_PAGE_SIZE */
 524    test_bit_size = size + (old_phys_addr & (XC_PAGE_SIZE - 1));
 525    if (test_bit_size % XC_PAGE_SIZE) {
 526        test_bit_size += XC_PAGE_SIZE - (test_bit_size % XC_PAGE_SIZE);
 527    }
 528    cache_size = size + address_offset;
 529    if (cache_size % MCACHE_BUCKET_SIZE) {
 530        cache_size += MCACHE_BUCKET_SIZE - (cache_size % MCACHE_BUCKET_SIZE);
 531    }
 532
 533    entry = &mapcache->entry[address_index % mapcache->nr_buckets];
 534    while (entry && !(entry->paddr_index == address_index &&
 535                      entry->size == cache_size)) {
 536        entry = entry->next;
 537    }
 538    if (!entry) {
 539        DPRINTF("Trying to update an entry for "TARGET_FMT_plx \
 540                "that is not in the mapcache!\n", old_phys_addr);
 541        return NULL;
 542    }
 543
 544    address_index  = new_phys_addr >> MCACHE_BUCKET_SHIFT;
 545    address_offset = new_phys_addr & (MCACHE_BUCKET_SIZE - 1);
 546
 547    fprintf(stderr, "Replacing a dummy mapcache entry for "TARGET_FMT_plx \
 548            " with "TARGET_FMT_plx"\n", old_phys_addr, new_phys_addr);
 549
 550    xen_remap_bucket(entry, entry->vaddr_base,
 551                     cache_size, address_index, false);
 552    if (!test_bits(address_offset >> XC_PAGE_SHIFT,
 553                test_bit_size >> XC_PAGE_SHIFT,
 554                entry->valid_mapping)) {
 555        DPRINTF("Unable to update a mapcache entry for "TARGET_FMT_plx"!\n",
 556                old_phys_addr);
 557        return NULL;
 558    }
 559
 560    return entry->vaddr_base + address_offset;
 561}
 562
 563uint8_t *xen_replace_cache_entry(hwaddr old_phys_addr,
 564                                 hwaddr new_phys_addr,
 565                                 hwaddr size)
 566{
 567    uint8_t *p;
 568
 569    mapcache_lock();
 570    p = xen_replace_cache_entry_unlocked(old_phys_addr, new_phys_addr, size);
 571    mapcache_unlock();
 572    return p;
 573}
 574