2010-08-31 15:41:25 +00:00
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#include <sys/resource.h>
|
|
|
|
|
|
|
|
#include "hw/xen_backend.h"
|
|
|
|
#include "blockdev.h"
|
2011-03-22 14:50:28 +00:00
|
|
|
#include "bitmap.h"
|
2010-08-31 15:41:25 +00:00
|
|
|
|
|
|
|
#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
|
2011-03-22 14:50:28 +00:00
|
|
|
# define MCACHE_MAX_SIZE (1UL<<31) /* 2GB Cap */
|
2010-08-31 15:41:25 +00:00
|
|
|
#elif defined(__x86_64__)
|
|
|
|
# define MCACHE_BUCKET_SHIFT 20
|
2011-03-22 14:50:28 +00:00
|
|
|
# define MCACHE_MAX_SIZE (1UL<<35) /* 32GB Cap */
|
2010-08-31 15:41:25 +00:00
|
|
|
#endif
|
|
|
|
#define MCACHE_BUCKET_SIZE (1UL << MCACHE_BUCKET_SHIFT)
|
|
|
|
|
2011-06-21 20:59:08 +00:00
|
|
|
#define mapcache_lock() ((void)0)
|
|
|
|
#define mapcache_unlock() ((void)0)
|
|
|
|
|
2010-08-31 15:41:25 +00:00
|
|
|
typedef struct MapCacheEntry {
|
|
|
|
target_phys_addr_t paddr_index;
|
|
|
|
uint8_t *vaddr_base;
|
2011-05-19 17:35:42 +00:00
|
|
|
unsigned long *valid_mapping;
|
2010-08-31 15:41:25 +00:00
|
|
|
uint8_t lock;
|
2011-05-19 17:35:42 +00:00
|
|
|
target_phys_addr_t size;
|
2010-08-31 15:41:25 +00:00
|
|
|
struct MapCacheEntry *next;
|
|
|
|
} MapCacheEntry;
|
|
|
|
|
|
|
|
typedef struct MapCacheRev {
|
|
|
|
uint8_t *vaddr_req;
|
|
|
|
target_phys_addr_t paddr_index;
|
2011-05-19 17:35:42 +00:00
|
|
|
target_phys_addr_t size;
|
2010-08-31 15:41:25 +00:00
|
|
|
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;
|
|
|
|
} MapCache;
|
|
|
|
|
|
|
|
static MapCache *mapcache;
|
|
|
|
|
2011-05-19 17:35:42 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2011-06-21 20:59:08 +00:00
|
|
|
void xen_map_cache_init(void)
|
2010-08-31 15:41:25 +00:00
|
|
|
{
|
|
|
|
unsigned long size;
|
|
|
|
struct rlimit rlimit_as;
|
|
|
|
|
2011-08-21 03:09:37 +00:00
|
|
|
mapcache = g_malloc0(sizeof (MapCache));
|
2010-08-31 15:41:25 +00:00
|
|
|
|
|
|
|
QTAILQ_INIT(&mapcache->locked_entries);
|
|
|
|
mapcache->last_address_index = -1;
|
|
|
|
|
|
|
|
getrlimit(RLIMIT_AS, &rlimit_as);
|
2011-03-22 14:50:28 +00:00
|
|
|
if (rlimit_as.rlim_max < MCACHE_MAX_SIZE) {
|
|
|
|
rlimit_as.rlim_cur = rlimit_as.rlim_max;
|
|
|
|
} else {
|
|
|
|
rlimit_as.rlim_cur = MCACHE_MAX_SIZE;
|
|
|
|
}
|
|
|
|
|
2010-08-31 15:41:25 +00:00
|
|
|
setrlimit(RLIMIT_AS, &rlimit_as);
|
2011-03-22 14:50:28 +00:00
|
|
|
mapcache->max_mcache_size = rlimit_as.rlim_cur;
|
2010-08-31 15:41:25 +00:00
|
|
|
|
|
|
|
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);
|
2011-06-21 20:59:08 +00:00
|
|
|
DPRINTF("%s, nr_buckets = %lx size %lu\n", __func__,
|
|
|
|
mapcache->nr_buckets, size);
|
2011-08-21 03:09:37 +00:00
|
|
|
mapcache->entry = g_malloc0(size);
|
2010-08-31 15:41:25 +00:00
|
|
|
}
|
|
|
|
|
2011-06-21 20:59:08 +00:00
|
|
|
static void xen_remap_bucket(MapCacheEntry *entry,
|
|
|
|
target_phys_addr_t size,
|
|
|
|
target_phys_addr_t address_index)
|
2010-08-31 15:41:25 +00:00
|
|
|
{
|
|
|
|
uint8_t *vaddr_base;
|
|
|
|
xen_pfn_t *pfns;
|
|
|
|
int *err;
|
2011-03-22 14:50:28 +00:00
|
|
|
unsigned int i;
|
2010-08-31 15:41:25 +00:00
|
|
|
target_phys_addr_t nb_pfn = size >> XC_PAGE_SHIFT;
|
|
|
|
|
2011-06-21 20:59:08 +00:00
|
|
|
trace_xen_remap_bucket(address_index);
|
2010-08-31 15:41:25 +00:00
|
|
|
|
2011-08-21 03:09:37 +00:00
|
|
|
pfns = g_malloc0(nb_pfn * sizeof (xen_pfn_t));
|
|
|
|
err = g_malloc0(nb_pfn * sizeof (int));
|
2010-08-31 15:41:25 +00:00
|
|
|
|
|
|
|
if (entry->vaddr_base != NULL) {
|
2011-05-19 17:35:42 +00:00
|
|
|
if (munmap(entry->vaddr_base, entry->size) != 0) {
|
2010-08-31 15:41:25 +00:00
|
|
|
perror("unmap fails");
|
|
|
|
exit(-1);
|
|
|
|
}
|
|
|
|
}
|
2011-05-19 17:35:42 +00:00
|
|
|
if (entry->valid_mapping != NULL) {
|
2011-08-21 03:09:37 +00:00
|
|
|
g_free(entry->valid_mapping);
|
2011-05-19 17:35:42 +00:00
|
|
|
entry->valid_mapping = NULL;
|
|
|
|
}
|
2010-08-31 15:41:25 +00:00
|
|
|
|
|
|
|
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;
|
2011-05-19 17:35:42 +00:00
|
|
|
entry->size = size;
|
2011-08-21 03:09:37 +00:00
|
|
|
entry->valid_mapping = (unsigned long *) g_malloc0(sizeof(unsigned long) *
|
2011-05-19 17:35:42 +00:00
|
|
|
BITS_TO_LONGS(size >> XC_PAGE_SHIFT));
|
2010-08-31 15:41:25 +00:00
|
|
|
|
2011-03-22 14:50:28 +00:00
|
|
|
bitmap_zero(entry->valid_mapping, nb_pfn);
|
|
|
|
for (i = 0; i < nb_pfn; i++) {
|
|
|
|
if (!err[i]) {
|
|
|
|
bitmap_set(entry->valid_mapping, i, 1);
|
2010-08-31 15:41:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-08-21 03:09:37 +00:00
|
|
|
g_free(pfns);
|
|
|
|
g_free(err);
|
2010-08-31 15:41:25 +00:00
|
|
|
}
|
|
|
|
|
2011-06-21 20:59:08 +00:00
|
|
|
uint8_t *xen_map_cache(target_phys_addr_t phys_addr, target_phys_addr_t size,
|
|
|
|
uint8_t lock)
|
2010-08-31 15:41:25 +00:00
|
|
|
{
|
|
|
|
MapCacheEntry *entry, *pentry = NULL;
|
|
|
|
target_phys_addr_t address_index = phys_addr >> MCACHE_BUCKET_SHIFT;
|
|
|
|
target_phys_addr_t address_offset = phys_addr & (MCACHE_BUCKET_SIZE - 1);
|
2011-05-19 17:35:42 +00:00
|
|
|
target_phys_addr_t __size = size;
|
2010-08-31 15:41:25 +00:00
|
|
|
|
2011-06-21 20:59:08 +00:00
|
|
|
trace_xen_map_cache(phys_addr);
|
2010-08-31 15:41:25 +00:00
|
|
|
|
2011-05-19 17:35:42 +00:00
|
|
|
if (address_index == mapcache->last_address_index && !lock && !__size) {
|
2011-06-21 20:59:08 +00:00
|
|
|
trace_xen_map_cache_return(mapcache->last_address_vaddr + address_offset);
|
2010-08-31 15:41:25 +00:00
|
|
|
return mapcache->last_address_vaddr + address_offset;
|
|
|
|
}
|
|
|
|
|
2011-05-19 17:35:42 +00:00
|
|
|
/* size is always a multiple of MCACHE_BUCKET_SIZE */
|
|
|
|
if ((address_offset + (__size % MCACHE_BUCKET_SIZE)) > MCACHE_BUCKET_SIZE)
|
|
|
|
__size += MCACHE_BUCKET_SIZE;
|
|
|
|
if (__size % MCACHE_BUCKET_SIZE)
|
|
|
|
__size += MCACHE_BUCKET_SIZE - (__size % MCACHE_BUCKET_SIZE);
|
|
|
|
if (!__size)
|
|
|
|
__size = MCACHE_BUCKET_SIZE;
|
|
|
|
|
2010-08-31 15:41:25 +00:00
|
|
|
entry = &mapcache->entry[address_index % mapcache->nr_buckets];
|
|
|
|
|
2011-05-19 17:35:42 +00:00
|
|
|
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))) {
|
2010-08-31 15:41:25 +00:00
|
|
|
pentry = entry;
|
|
|
|
entry = entry->next;
|
|
|
|
}
|
|
|
|
if (!entry) {
|
2011-08-21 03:09:37 +00:00
|
|
|
entry = g_malloc0(sizeof (MapCacheEntry));
|
2010-08-31 15:41:25 +00:00
|
|
|
pentry->next = entry;
|
2011-06-21 20:59:08 +00:00
|
|
|
xen_remap_bucket(entry, __size, address_index);
|
2010-08-31 15:41:25 +00:00
|
|
|
} else if (!entry->lock) {
|
|
|
|
if (!entry->vaddr_base || entry->paddr_index != address_index ||
|
2011-05-19 17:35:42 +00:00
|
|
|
entry->size != __size ||
|
|
|
|
!test_bits(address_offset >> XC_PAGE_SHIFT, size >> XC_PAGE_SHIFT,
|
|
|
|
entry->valid_mapping)) {
|
2011-06-21 20:59:08 +00:00
|
|
|
xen_remap_bucket(entry, __size, address_index);
|
2010-08-31 15:41:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-05-19 17:35:42 +00:00
|
|
|
if(!test_bits(address_offset >> XC_PAGE_SHIFT, size >> XC_PAGE_SHIFT,
|
|
|
|
entry->valid_mapping)) {
|
2010-08-31 15:41:25 +00:00
|
|
|
mapcache->last_address_index = -1;
|
2011-06-21 20:59:08 +00:00
|
|
|
trace_xen_map_cache_return(NULL);
|
2010-08-31 15:41:25 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
mapcache->last_address_index = address_index;
|
|
|
|
mapcache->last_address_vaddr = entry->vaddr_base;
|
|
|
|
if (lock) {
|
2011-08-21 03:09:37 +00:00
|
|
|
MapCacheRev *reventry = g_malloc0(sizeof(MapCacheRev));
|
2010-08-31 15:41:25 +00:00
|
|
|
entry->lock++;
|
|
|
|
reventry->vaddr_req = mapcache->last_address_vaddr + address_offset;
|
|
|
|
reventry->paddr_index = mapcache->last_address_index;
|
2011-05-19 17:35:42 +00:00
|
|
|
reventry->size = entry->size;
|
2010-08-31 15:41:25 +00:00
|
|
|
QTAILQ_INSERT_HEAD(&mapcache->locked_entries, reventry, next);
|
|
|
|
}
|
|
|
|
|
2011-06-21 20:59:08 +00:00
|
|
|
trace_xen_map_cache_return(mapcache->last_address_vaddr + address_offset);
|
2010-08-31 15:41:25 +00:00
|
|
|
return mapcache->last_address_vaddr + address_offset;
|
|
|
|
}
|
|
|
|
|
2011-06-21 20:59:08 +00:00
|
|
|
ram_addr_t xen_ram_addr_from_mapcache(void *ptr)
|
2010-08-31 15:41:25 +00:00
|
|
|
{
|
2011-07-26 14:33:11 +00:00
|
|
|
MapCacheEntry *entry = NULL;
|
2010-08-31 15:41:25 +00:00
|
|
|
MapCacheRev *reventry;
|
|
|
|
target_phys_addr_t paddr_index;
|
2011-05-19 17:35:42 +00:00
|
|
|
target_phys_addr_t size;
|
2010-08-31 15:41:25 +00:00
|
|
|
int found = 0;
|
|
|
|
|
|
|
|
QTAILQ_FOREACH(reventry, &mapcache->locked_entries, next) {
|
|
|
|
if (reventry->vaddr_req == ptr) {
|
|
|
|
paddr_index = reventry->paddr_index;
|
2011-05-19 17:35:42 +00:00
|
|
|
size = reventry->size;
|
2010-08-31 15:41:25 +00:00
|
|
|
found = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!found) {
|
2011-06-21 20:59:08 +00:00
|
|
|
fprintf(stderr, "%s, could not find %p\n", __func__, ptr);
|
2010-08-31 15:41:25 +00:00
|
|
|
QTAILQ_FOREACH(reventry, &mapcache->locked_entries, next) {
|
|
|
|
DPRINTF(" "TARGET_FMT_plx" -> %p is present\n", reventry->paddr_index,
|
|
|
|
reventry->vaddr_req);
|
|
|
|
}
|
|
|
|
abort();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-05-19 17:35:42 +00:00
|
|
|
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);
|
2010-08-31 15:41:25 +00:00
|
|
|
}
|
|
|
|
|
2011-06-21 20:59:08 +00:00
|
|
|
void xen_invalidate_map_cache_entry(uint8_t *buffer)
|
2010-08-31 15:41:25 +00:00
|
|
|
{
|
|
|
|
MapCacheEntry *entry = NULL, *pentry = NULL;
|
|
|
|
MapCacheRev *reventry;
|
|
|
|
target_phys_addr_t paddr_index;
|
2011-05-19 17:35:42 +00:00
|
|
|
target_phys_addr_t size;
|
2010-08-31 15:41:25 +00:00
|
|
|
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;
|
2011-05-19 17:35:42 +00:00
|
|
|
size = reventry->size;
|
2010-08-31 15:41:25 +00:00
|
|
|
found = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!found) {
|
2011-06-21 20:59:08 +00:00
|
|
|
DPRINTF("%s, could not find %p\n", __func__, buffer);
|
2010-08-31 15:41:25 +00:00
|
|
|
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);
|
2011-08-21 03:09:37 +00:00
|
|
|
g_free(reventry);
|
2010-08-31 15:41:25 +00:00
|
|
|
|
|
|
|
entry = &mapcache->entry[paddr_index % mapcache->nr_buckets];
|
2011-05-19 17:35:42 +00:00
|
|
|
while (entry && (entry->paddr_index != paddr_index || entry->size != size)) {
|
2010-08-31 15:41:25 +00:00
|
|
|
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;
|
2011-05-19 17:35:42 +00:00
|
|
|
if (munmap(entry->vaddr_base, entry->size) != 0) {
|
2010-08-31 15:41:25 +00:00
|
|
|
perror("unmap fails");
|
|
|
|
exit(-1);
|
|
|
|
}
|
2011-08-21 03:09:37 +00:00
|
|
|
g_free(entry->valid_mapping);
|
|
|
|
g_free(entry);
|
2010-08-31 15:41:25 +00:00
|
|
|
}
|
|
|
|
|
2011-06-21 20:59:08 +00:00
|
|
|
void xen_invalidate_map_cache(void)
|
2010-08-31 15:41:25 +00:00
|
|
|
{
|
|
|
|
unsigned long i;
|
|
|
|
MapCacheRev *reventry;
|
|
|
|
|
|
|
|
/* Flush pending AIO before destroying the mapcache */
|
|
|
|
qemu_aio_flush();
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2011-05-19 17:35:42 +00:00
|
|
|
if (munmap(entry->vaddr_base, entry->size) != 0) {
|
2010-08-31 15:41:25 +00:00
|
|
|
perror("unmap fails");
|
|
|
|
exit(-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
entry->paddr_index = 0;
|
|
|
|
entry->vaddr_base = NULL;
|
2011-05-19 17:35:42 +00:00
|
|
|
entry->size = 0;
|
2011-08-21 03:09:37 +00:00
|
|
|
g_free(entry->valid_mapping);
|
2011-05-19 17:35:42 +00:00
|
|
|
entry->valid_mapping = NULL;
|
2010-08-31 15:41:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
mapcache->last_address_index = -1;
|
|
|
|
mapcache->last_address_vaddr = NULL;
|
|
|
|
|
|
|
|
mapcache_unlock();
|
|
|
|
}
|