xen-mapcache.c 12 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 15 16
 */

#include "config.h"

#include <sys/resource.h>

#include "hw/xen_backend.h"
#include "blockdev.h"
17
#include "bitmap.h"
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37

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

#include "xen-mapcache.h"
#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 56 57
typedef struct MapCacheEntry {
    target_phys_addr_t paddr_index;
    uint8_t *vaddr_base;
58
    unsigned long *valid_mapping;
59
    uint8_t lock;
60
    target_phys_addr_t size;
61 62 63 64 65 66
    struct MapCacheEntry *next;
} MapCacheEntry;

typedef struct MapCacheRev {
    uint8_t *vaddr_req;
    target_phys_addr_t paddr_index;
67
    target_phys_addr_t size;
68 69 70 71 72 73 74 75 76 77 78 79 80
    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. */
    target_phys_addr_t last_address_index;
    uint8_t *last_address_vaddr;
    unsigned long max_mcache_size;
    unsigned int mcache_bucket_shift;
81 82 83

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

static MapCache *mapcache;

88 89 90 91 92 93 94 95 96
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;
}

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

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

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

107 108 109
    QTAILQ_INIT(&mapcache->locked_entries);
    mapcache->last_address_index = -1;

110 111 112 113
    if (geteuid() == 0) {
        rlimit_as.rlim_cur = RLIM_INFINITY;
        rlimit_as.rlim_max = RLIM_INFINITY;
        mapcache->max_mcache_size = MCACHE_MAX_SIZE;
114
    } else {
115 116 117 118 119 120 121 122 123 124 125 126 127
        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;
        }
128 129
    }

130 131 132 133 134 135 136 137 138
    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);
139 140
    DPRINTF("%s, nr_buckets = %lx size %lu\n", __func__,
            mapcache->nr_buckets, size);
141
    mapcache->entry = g_malloc0(size);
142 143
}

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

154
    trace_xen_remap_bucket(address_index);
155

156 157
    pfns = g_malloc0(nb_pfn * sizeof (xen_pfn_t));
    err = g_malloc0(nb_pfn * sizeof (int));
158 159

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

    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;
183
    entry->size = size;
184
    entry->valid_mapping = (unsigned long *) g_malloc0(sizeof(unsigned long) *
185
            BITS_TO_LONGS(size >> XC_PAGE_SHIFT));
186

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

194 195
    g_free(pfns);
    g_free(err);
196 197
}

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

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

211
    trace_xen_map_cache(phys_addr);
212

213
    if (address_index == mapcache->last_address_index && !lock && !__size) {
214
        trace_xen_map_cache_return(mapcache->last_address_vaddr + address_offset);
215 216 217
        return mapcache->last_address_vaddr + address_offset;
    }

218
    /* size is always a multiple of MCACHE_BUCKET_SIZE */
219 220 221 222 223 224
    if (size) {
        __size = size + address_offset;
        if (__size % MCACHE_BUCKET_SIZE) {
            __size += MCACHE_BUCKET_SIZE - (__size % MCACHE_BUCKET_SIZE);
        }
    } else {
225
        __size = MCACHE_BUCKET_SIZE;
226
    }
227

228 229
    entry = &mapcache->entry[address_index % mapcache->nr_buckets];

230 231 232 233
    while (entry && entry->lock && entry->vaddr_base &&
            (entry->paddr_index != address_index || entry->size != __size ||
             !test_bits(address_offset >> XC_PAGE_SHIFT, size >> XC_PAGE_SHIFT,
                 entry->valid_mapping))) {
234 235 236 237
        pentry = entry;
        entry = entry->next;
    }
    if (!entry) {
238
        entry = g_malloc0(sizeof (MapCacheEntry));
239
        pentry->next = entry;
240
        xen_remap_bucket(entry, __size, address_index);
241 242
    } else if (!entry->lock) {
        if (!entry->vaddr_base || entry->paddr_index != address_index ||
243 244 245
                entry->size != __size ||
                !test_bits(address_offset >> XC_PAGE_SHIFT, size >> XC_PAGE_SHIFT,
                    entry->valid_mapping)) {
246
            xen_remap_bucket(entry, __size, address_index);
247 248 249
        }
    }

250 251
    if(!test_bits(address_offset >> XC_PAGE_SHIFT, size >> XC_PAGE_SHIFT,
                entry->valid_mapping)) {
252
        mapcache->last_address_index = -1;
253 254 255 256 257
        if (!translated && mapcache->phys_offset_to_gaddr) {
            phys_addr = mapcache->phys_offset_to_gaddr(phys_addr, size, mapcache->opaque);
            translated = true;
            goto tryagain;
        }
258
        trace_xen_map_cache_return(NULL);
259 260 261 262 263 264
        return NULL;
    }

    mapcache->last_address_index = address_index;
    mapcache->last_address_vaddr = entry->vaddr_base;
    if (lock) {
265
        MapCacheRev *reventry = g_malloc0(sizeof(MapCacheRev));
266 267 268
        entry->lock++;
        reventry->vaddr_req = mapcache->last_address_vaddr + address_offset;
        reventry->paddr_index = mapcache->last_address_index;
269
        reventry->size = entry->size;
270 271 272
        QTAILQ_INSERT_HEAD(&mapcache->locked_entries, reventry, next);
    }

273
    trace_xen_map_cache_return(mapcache->last_address_vaddr + address_offset);
274 275 276
    return mapcache->last_address_vaddr + address_offset;
}

277
ram_addr_t xen_ram_addr_from_mapcache(void *ptr)
278
{
279
    MapCacheEntry *entry = NULL;
280 281
    MapCacheRev *reventry;
    target_phys_addr_t paddr_index;
282
    target_phys_addr_t size;
283 284 285 286 287
    int found = 0;

    QTAILQ_FOREACH(reventry, &mapcache->locked_entries, next) {
        if (reventry->vaddr_req == ptr) {
            paddr_index = reventry->paddr_index;
288
            size = reventry->size;
289 290 291 292 293
            found = 1;
            break;
        }
    }
    if (!found) {
294
        fprintf(stderr, "%s, could not find %p\n", __func__, ptr);
295 296 297 298 299 300 301 302
        QTAILQ_FOREACH(reventry, &mapcache->locked_entries, next) {
            DPRINTF("   "TARGET_FMT_plx" -> %p is present\n", reventry->paddr_index,
                    reventry->vaddr_req);
        }
        abort();
        return 0;
    }

303 304 305 306 307 308 309 310 311 312
    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);
313 314
}

315
void xen_invalidate_map_cache_entry(uint8_t *buffer)
316 317 318 319
{
    MapCacheEntry *entry = NULL, *pentry = NULL;
    MapCacheRev *reventry;
    target_phys_addr_t paddr_index;
320
    target_phys_addr_t size;
321 322 323 324 325 326 327 328 329
    int found = 0;

    if (mapcache->last_address_vaddr == buffer) {
        mapcache->last_address_index = -1;
    }

    QTAILQ_FOREACH(reventry, &mapcache->locked_entries, next) {
        if (reventry->vaddr_req == buffer) {
            paddr_index = reventry->paddr_index;
330
            size = reventry->size;
331 332 333 334 335
            found = 1;
            break;
        }
    }
    if (!found) {
336
        DPRINTF("%s, could not find %p\n", __func__, buffer);
337 338 339 340 341 342
        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);
343
    g_free(reventry);
344 345

    entry = &mapcache->entry[paddr_index % mapcache->nr_buckets];
346
    while (entry && (entry->paddr_index != paddr_index || entry->size != size)) {
347 348 349 350 351 352 353 354 355 356 357 358 359
        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;
360
    if (munmap(entry->vaddr_base, entry->size) != 0) {
361 362 363
        perror("unmap fails");
        exit(-1);
    }
364 365
    g_free(entry->valid_mapping);
    g_free(entry);
366 367
}

368
void xen_invalidate_map_cache(void)
369 370 371 372 373
{
    unsigned long i;
    MapCacheRev *reventry;

    /* Flush pending AIO before destroying the mapcache */
374
    bdrv_drain_all();
375 376 377 378 379 380 381 382 383 384 385 386 387 388 389

    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;
        }
390 391 392
        if (entry->lock > 0) {
            continue;
        }
393

394
        if (munmap(entry->vaddr_base, entry->size) != 0) {
395 396 397 398 399 400
            perror("unmap fails");
            exit(-1);
        }

        entry->paddr_index = 0;
        entry->vaddr_base = NULL;
401
        entry->size = 0;
402
        g_free(entry->valid_mapping);
403
        entry->valid_mapping = NULL;
404 405 406 407 408 409 410
    }

    mapcache->last_address_index = -1;
    mapcache->last_address_vaddr = NULL;

    mapcache_unlock();
}