xen-mapcache.c 12.4 KB
Newer Older
1 2 3 4 5 6
/*
 * Copyright (C) 2011       Citrix Ltd.
 *
 * This work is licensed under the terms of the GNU GPL, version 2.  See
 * the COPYING file in the top-level directory.
 *
7 8
 * Contributions after 2012-01-13 are licensed under the terms of the
 * GNU GPL, version 2 or (at your option) any later version.
9 10 11 12 13 14
 */

#include "config.h"

#include <sys/resource.h>

15
#include "hw/xen/xen_backend.h"
16
#include "sysemu/blockdev.h"
17
#include "qemu/bitmap.h"
18 19 20 21

#include <xen/hvm/params.h>
#include <sys/mman.h>

22
#include "sysemu/xen-mapcache.h"
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
#include "trace.h"


//#define MAPCACHE_DEBUG

#ifdef MAPCACHE_DEBUG
#  define DPRINTF(fmt, ...) do { \
    fprintf(stderr, "xen_mapcache: " fmt, ## __VA_ARGS__); \
} while (0)
#else
#  define DPRINTF(fmt, ...) do { } while (0)
#endif

#if defined(__i386__)
#  define MCACHE_BUCKET_SHIFT 16
38
#  define MCACHE_MAX_SIZE     (1UL<<31) /* 2GB Cap */
39 40
#elif defined(__x86_64__)
#  define MCACHE_BUCKET_SHIFT 20
41
#  define MCACHE_MAX_SIZE     (1UL<<35) /* 32GB Cap */
42 43 44
#endif
#define MCACHE_BUCKET_SIZE (1UL << MCACHE_BUCKET_SHIFT)

45 46 47 48 49 50 51
/* This is the size of the virtual address space reserve to QEMU that will not
 * be use by MapCache.
 * From empirical tests I observed that qemu use 75MB more than the
 * max_mcache_size.
 */
#define NON_MCACHE_MEMORY_SIZE (80 * 1024 * 1024)

52 53 54
#define mapcache_lock()   ((void)0)
#define mapcache_unlock() ((void)0)

55
typedef struct MapCacheEntry {
56
    hwaddr paddr_index;
57
    uint8_t *vaddr_base;
58
    unsigned long *valid_mapping;
59
    uint8_t lock;
60
    hwaddr size;
61 62 63 64 65
    struct MapCacheEntry *next;
} MapCacheEntry;

typedef struct MapCacheRev {
    uint8_t *vaddr_req;
66 67
    hwaddr paddr_index;
    hwaddr size;
68 69 70 71 72 73 74 75 76
    QTAILQ_ENTRY(MapCacheRev) next;
} MapCacheRev;

typedef struct MapCache {
    MapCacheEntry *entry;
    unsigned long nr_buckets;
    QTAILQ_HEAD(map_cache_head, MapCacheRev) locked_entries;

    /* For most cases (>99.9%), the page address is the same. */
77
    MapCacheEntry *last_entry;
78 79
    unsigned long max_mcache_size;
    unsigned int mcache_bucket_shift;
80 81 82

    phys_offset_to_gaddr_t phys_offset_to_gaddr;
    void *opaque;
83 84 85 86
} MapCache;

static MapCache *mapcache;

87 88 89 90 91 92 93 94 95
static inline int test_bits(int nr, int size, const unsigned long *addr)
{
    unsigned long res = find_next_zero_bit(addr, size + nr, nr);
    if (res >= nr + size)
        return 1;
    else
        return 0;
}

96
void xen_map_cache_init(phys_offset_to_gaddr_t f, void *opaque)
97 98 99 100
{
    unsigned long size;
    struct rlimit rlimit_as;

101
    mapcache = g_malloc0(sizeof (MapCache));
102

103 104 105
    mapcache->phys_offset_to_gaddr = f;
    mapcache->opaque = opaque;

106 107
    QTAILQ_INIT(&mapcache->locked_entries);

108 109 110 111
    if (geteuid() == 0) {
        rlimit_as.rlim_cur = RLIM_INFINITY;
        rlimit_as.rlim_max = RLIM_INFINITY;
        mapcache->max_mcache_size = MCACHE_MAX_SIZE;
112
    } else {
113 114 115 116 117 118 119 120 121 122 123 124 125
        getrlimit(RLIMIT_AS, &rlimit_as);
        rlimit_as.rlim_cur = rlimit_as.rlim_max;

        if (rlimit_as.rlim_max != RLIM_INFINITY) {
            fprintf(stderr, "Warning: QEMU's maximum size of virtual"
                    " memory is not infinity.\n");
        }
        if (rlimit_as.rlim_max < MCACHE_MAX_SIZE + NON_MCACHE_MEMORY_SIZE) {
            mapcache->max_mcache_size = rlimit_as.rlim_max -
                NON_MCACHE_MEMORY_SIZE;
        } else {
            mapcache->max_mcache_size = MCACHE_MAX_SIZE;
        }
126 127
    }

128 129 130 131 132 133 134 135 136
    setrlimit(RLIMIT_AS, &rlimit_as);

    mapcache->nr_buckets =
        (((mapcache->max_mcache_size >> XC_PAGE_SHIFT) +
          (1UL << (MCACHE_BUCKET_SHIFT - XC_PAGE_SHIFT)) - 1) >>
         (MCACHE_BUCKET_SHIFT - XC_PAGE_SHIFT));

    size = mapcache->nr_buckets * sizeof (MapCacheEntry);
    size = (size + XC_PAGE_SIZE - 1) & ~(XC_PAGE_SIZE - 1);
137 138
    DPRINTF("%s, nr_buckets = %lx size %lu\n", __func__,
            mapcache->nr_buckets, size);
139
    mapcache->entry = g_malloc0(size);
140 141
}

142
static void xen_remap_bucket(MapCacheEntry *entry,
143 144
                             hwaddr size,
                             hwaddr address_index)
145 146 147 148
{
    uint8_t *vaddr_base;
    xen_pfn_t *pfns;
    int *err;
149
    unsigned int i;
150
    hwaddr nb_pfn = size >> XC_PAGE_SHIFT;
151

152
    trace_xen_remap_bucket(address_index);
153

154 155
    pfns = g_malloc0(nb_pfn * sizeof (xen_pfn_t));
    err = g_malloc0(nb_pfn * sizeof (int));
156 157

    if (entry->vaddr_base != NULL) {
158
        if (munmap(entry->vaddr_base, entry->size) != 0) {
159 160 161 162
            perror("unmap fails");
            exit(-1);
        }
    }
163
    if (entry->valid_mapping != NULL) {
164
        g_free(entry->valid_mapping);
165 166
        entry->valid_mapping = NULL;
    }
167 168 169 170 171 172 173 174 175 176 177 178 179 180

    for (i = 0; i < nb_pfn; i++) {
        pfns[i] = (address_index << (MCACHE_BUCKET_SHIFT-XC_PAGE_SHIFT)) + i;
    }

    vaddr_base = xc_map_foreign_bulk(xen_xc, xen_domid, PROT_READ|PROT_WRITE,
                                     pfns, err, nb_pfn);
    if (vaddr_base == NULL) {
        perror("xc_map_foreign_bulk");
        exit(-1);
    }

    entry->vaddr_base = vaddr_base;
    entry->paddr_index = address_index;
181
    entry->size = size;
182
    entry->valid_mapping = (unsigned long *) g_malloc0(sizeof(unsigned long) *
183
            BITS_TO_LONGS(size >> XC_PAGE_SHIFT));
184

185 186 187 188
    bitmap_zero(entry->valid_mapping, nb_pfn);
    for (i = 0; i < nb_pfn; i++) {
        if (!err[i]) {
            bitmap_set(entry->valid_mapping, i, 1);
189 190 191
        }
    }

192 193
    g_free(pfns);
    g_free(err);
194 195
}

196
uint8_t *xen_map_cache(hwaddr phys_addr, hwaddr size,
197
                       uint8_t lock)
198 199
{
    MapCacheEntry *entry, *pentry = NULL;
200 201 202
    hwaddr address_index;
    hwaddr address_offset;
    hwaddr __size = size;
203
    hwaddr __test_bit_size = size;
204 205 206 207 208
    bool translated = false;

tryagain:
    address_index  = phys_addr >> MCACHE_BUCKET_SHIFT;
    address_offset = phys_addr & (MCACHE_BUCKET_SIZE - 1);
209

210
    trace_xen_map_cache(phys_addr);
211

212 213 214 215 216 217 218 219 220 221 222
    /* __test_bit_size is always a multiple of XC_PAGE_SIZE */
    if (size) {
        __test_bit_size = size + (phys_addr & (XC_PAGE_SIZE - 1));

        if (__test_bit_size % XC_PAGE_SIZE) {
            __test_bit_size += XC_PAGE_SIZE - (__test_bit_size % XC_PAGE_SIZE);
        }
    } else {
        __test_bit_size = XC_PAGE_SIZE;
    }

223 224
    if (mapcache->last_entry != NULL &&
        mapcache->last_entry->paddr_index == address_index &&
225 226 227 228
        !lock && !__size &&
        test_bits(address_offset >> XC_PAGE_SHIFT,
                  __test_bit_size >> XC_PAGE_SHIFT,
                  mapcache->last_entry->valid_mapping)) {
229 230
        trace_xen_map_cache_return(mapcache->last_entry->vaddr_base + address_offset);
        return mapcache->last_entry->vaddr_base + address_offset;
231 232
    }

233
    /* size is always a multiple of MCACHE_BUCKET_SIZE */
234 235 236 237 238 239
    if (size) {
        __size = size + address_offset;
        if (__size % MCACHE_BUCKET_SIZE) {
            __size += MCACHE_BUCKET_SIZE - (__size % MCACHE_BUCKET_SIZE);
        }
    } else {
240
        __size = MCACHE_BUCKET_SIZE;
241
    }
242

243 244
    entry = &mapcache->entry[address_index % mapcache->nr_buckets];

245 246
    while (entry && entry->lock && entry->vaddr_base &&
            (entry->paddr_index != address_index || entry->size != __size ||
247 248
             !test_bits(address_offset >> XC_PAGE_SHIFT,
                 __test_bit_size >> XC_PAGE_SHIFT,
249
                 entry->valid_mapping))) {
250 251 252 253
        pentry = entry;
        entry = entry->next;
    }
    if (!entry) {
254
        entry = g_malloc0(sizeof (MapCacheEntry));
255
        pentry->next = entry;
256
        xen_remap_bucket(entry, __size, address_index);
257 258
    } else if (!entry->lock) {
        if (!entry->vaddr_base || entry->paddr_index != address_index ||
259
                entry->size != __size ||
260 261
                !test_bits(address_offset >> XC_PAGE_SHIFT,
                    __test_bit_size >> XC_PAGE_SHIFT,
262
                    entry->valid_mapping)) {
263
            xen_remap_bucket(entry, __size, address_index);
264 265 266
        }
    }

267 268
    if(!test_bits(address_offset >> XC_PAGE_SHIFT,
                __test_bit_size >> XC_PAGE_SHIFT,
269
                entry->valid_mapping)) {
270
        mapcache->last_entry = NULL;
271 272 273 274 275
        if (!translated && mapcache->phys_offset_to_gaddr) {
            phys_addr = mapcache->phys_offset_to_gaddr(phys_addr, size, mapcache->opaque);
            translated = true;
            goto tryagain;
        }
276
        trace_xen_map_cache_return(NULL);
277 278 279
        return NULL;
    }

280
    mapcache->last_entry = entry;
281
    if (lock) {
282
        MapCacheRev *reventry = g_malloc0(sizeof(MapCacheRev));
283
        entry->lock++;
284 285
        reventry->vaddr_req = mapcache->last_entry->vaddr_base + address_offset;
        reventry->paddr_index = mapcache->last_entry->paddr_index;
286
        reventry->size = entry->size;
287 288 289
        QTAILQ_INSERT_HEAD(&mapcache->locked_entries, reventry, next);
    }

290 291
    trace_xen_map_cache_return(mapcache->last_entry->vaddr_base + address_offset);
    return mapcache->last_entry->vaddr_base + address_offset;
292 293
}

294
ram_addr_t xen_ram_addr_from_mapcache(void *ptr)
295
{
296
    MapCacheEntry *entry = NULL;
297
    MapCacheRev *reventry;
298 299
    hwaddr paddr_index;
    hwaddr size;
300 301 302 303 304
    int found = 0;

    QTAILQ_FOREACH(reventry, &mapcache->locked_entries, next) {
        if (reventry->vaddr_req == ptr) {
            paddr_index = reventry->paddr_index;
305
            size = reventry->size;
306 307 308 309 310
            found = 1;
            break;
        }
    }
    if (!found) {
311
        fprintf(stderr, "%s, could not find %p\n", __func__, ptr);
312 313 314 315 316 317 318 319
        QTAILQ_FOREACH(reventry, &mapcache->locked_entries, next) {
            DPRINTF("   "TARGET_FMT_plx" -> %p is present\n", reventry->paddr_index,
                    reventry->vaddr_req);
        }
        abort();
        return 0;
    }

320 321 322 323 324 325 326 327 328 329
    entry = &mapcache->entry[paddr_index % mapcache->nr_buckets];
    while (entry && (entry->paddr_index != paddr_index || entry->size != size)) {
        entry = entry->next;
    }
    if (!entry) {
        DPRINTF("Trying to find address %p that is not in the mapcache!\n", ptr);
        return 0;
    }
    return (reventry->paddr_index << MCACHE_BUCKET_SHIFT) +
        ((unsigned long) ptr - (unsigned long) entry->vaddr_base);
330 331
}

332
void xen_invalidate_map_cache_entry(uint8_t *buffer)
333 334 335
{
    MapCacheEntry *entry = NULL, *pentry = NULL;
    MapCacheRev *reventry;
336 337
    hwaddr paddr_index;
    hwaddr size;
338 339 340 341 342
    int found = 0;

    QTAILQ_FOREACH(reventry, &mapcache->locked_entries, next) {
        if (reventry->vaddr_req == buffer) {
            paddr_index = reventry->paddr_index;
343
            size = reventry->size;
344 345 346 347 348
            found = 1;
            break;
        }
    }
    if (!found) {
349
        DPRINTF("%s, could not find %p\n", __func__, buffer);
350 351 352 353 354 355
        QTAILQ_FOREACH(reventry, &mapcache->locked_entries, next) {
            DPRINTF("   "TARGET_FMT_plx" -> %p is present\n", reventry->paddr_index, reventry->vaddr_req);
        }
        return;
    }
    QTAILQ_REMOVE(&mapcache->locked_entries, reventry, next);
356
    g_free(reventry);
357

358 359 360
    if (mapcache->last_entry != NULL &&
        mapcache->last_entry->paddr_index == paddr_index) {
        mapcache->last_entry = NULL;
361 362
    }

363
    entry = &mapcache->entry[paddr_index % mapcache->nr_buckets];
364
    while (entry && (entry->paddr_index != paddr_index || entry->size != size)) {
365 366 367 368 369 370 371 372 373 374 375 376 377
        pentry = entry;
        entry = entry->next;
    }
    if (!entry) {
        DPRINTF("Trying to unmap address %p that is not in the mapcache!\n", buffer);
        return;
    }
    entry->lock--;
    if (entry->lock > 0 || pentry == NULL) {
        return;
    }

    pentry->next = entry->next;
378
    if (munmap(entry->vaddr_base, entry->size) != 0) {
379 380 381
        perror("unmap fails");
        exit(-1);
    }
382 383
    g_free(entry->valid_mapping);
    g_free(entry);
384 385
}

386
void xen_invalidate_map_cache(void)
387 388 389 390 391
{
    unsigned long i;
    MapCacheRev *reventry;

    /* Flush pending AIO before destroying the mapcache */
392
    bdrv_drain_all();
393 394 395 396 397 398 399 400 401 402 403 404 405 406 407

    QTAILQ_FOREACH(reventry, &mapcache->locked_entries, next) {
        DPRINTF("There should be no locked mappings at this time, "
                "but "TARGET_FMT_plx" -> %p is present\n",
                reventry->paddr_index, reventry->vaddr_req);
    }

    mapcache_lock();

    for (i = 0; i < mapcache->nr_buckets; i++) {
        MapCacheEntry *entry = &mapcache->entry[i];

        if (entry->vaddr_base == NULL) {
            continue;
        }
408 409 410
        if (entry->lock > 0) {
            continue;
        }
411

412
        if (munmap(entry->vaddr_base, entry->size) != 0) {
413 414 415 416 417 418
            perror("unmap fails");
            exit(-1);
        }

        entry->paddr_index = 0;
        entry->vaddr_base = NULL;
419
        entry->size = 0;
420
        g_free(entry->valid_mapping);
421
        entry->valid_mapping = NULL;
422 423
    }

424
    mapcache->last_entry = NULL;
425 426 427

    mapcache_unlock();
}