Jun Nakajima | 432d268 | 2010-08-31 16:41:25 +0100 | [diff] [blame] | 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 | * |
Paolo Bonzini | 6b620ca | 2012-01-13 17:44:23 +0100 | [diff] [blame] | 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. |
Jun Nakajima | 432d268 | 2010-08-31 16:41:25 +0100 | [diff] [blame] | 9 | */ |
| 10 | |
| 11 | #include "config.h" |
| 12 | |
| 13 | #include <sys/resource.h> |
| 14 | |
| 15 | #include "hw/xen_backend.h" |
| 16 | #include "blockdev.h" |
John Baboval | ea6c5f8 | 2011-03-22 14:50:28 +0000 | [diff] [blame] | 17 | #include "bitmap.h" |
Jun Nakajima | 432d268 | 2010-08-31 16:41:25 +0100 | [diff] [blame] | 18 | |
| 19 | #include <xen/hvm/params.h> |
| 20 | #include <sys/mman.h> |
| 21 | |
| 22 | #include "xen-mapcache.h" |
| 23 | #include "trace.h" |
| 24 | |
| 25 | |
| 26 | //#define MAPCACHE_DEBUG |
| 27 | |
| 28 | #ifdef MAPCACHE_DEBUG |
| 29 | # define DPRINTF(fmt, ...) do { \ |
| 30 | fprintf(stderr, "xen_mapcache: " fmt, ## __VA_ARGS__); \ |
| 31 | } while (0) |
| 32 | #else |
| 33 | # define DPRINTF(fmt, ...) do { } while (0) |
| 34 | #endif |
| 35 | |
| 36 | #if defined(__i386__) |
| 37 | # define MCACHE_BUCKET_SHIFT 16 |
John Baboval | ea6c5f8 | 2011-03-22 14:50:28 +0000 | [diff] [blame] | 38 | # define MCACHE_MAX_SIZE (1UL<<31) /* 2GB Cap */ |
Jun Nakajima | 432d268 | 2010-08-31 16:41:25 +0100 | [diff] [blame] | 39 | #elif defined(__x86_64__) |
| 40 | # define MCACHE_BUCKET_SHIFT 20 |
John Baboval | ea6c5f8 | 2011-03-22 14:50:28 +0000 | [diff] [blame] | 41 | # define MCACHE_MAX_SIZE (1UL<<35) /* 32GB Cap */ |
Jun Nakajima | 432d268 | 2010-08-31 16:41:25 +0100 | [diff] [blame] | 42 | #endif |
| 43 | #define MCACHE_BUCKET_SIZE (1UL << MCACHE_BUCKET_SHIFT) |
| 44 | |
Anthony PERARD | 56c119e | 2011-09-09 12:50:18 +0000 | [diff] [blame] | 45 | /* This is the size of the virtual address space reserve to QEMU that will not |
| 46 | * be use by MapCache. |
| 47 | * From empirical tests I observed that qemu use 75MB more than the |
| 48 | * max_mcache_size. |
| 49 | */ |
| 50 | #define NON_MCACHE_MEMORY_SIZE (80 * 1024 * 1024) |
| 51 | |
Jan Kiszka | e41d7c6 | 2011-06-21 22:59:08 +0200 | [diff] [blame] | 52 | #define mapcache_lock() ((void)0) |
| 53 | #define mapcache_unlock() ((void)0) |
| 54 | |
Jun Nakajima | 432d268 | 2010-08-31 16:41:25 +0100 | [diff] [blame] | 55 | typedef struct MapCacheEntry { |
| 56 | target_phys_addr_t paddr_index; |
| 57 | uint8_t *vaddr_base; |
Stefano Stabellini | c13390c | 2011-05-19 18:35:42 +0100 | [diff] [blame] | 58 | unsigned long *valid_mapping; |
Jun Nakajima | 432d268 | 2010-08-31 16:41:25 +0100 | [diff] [blame] | 59 | uint8_t lock; |
Stefano Stabellini | c13390c | 2011-05-19 18:35:42 +0100 | [diff] [blame] | 60 | target_phys_addr_t size; |
Jun Nakajima | 432d268 | 2010-08-31 16:41:25 +0100 | [diff] [blame] | 61 | struct MapCacheEntry *next; |
| 62 | } MapCacheEntry; |
| 63 | |
| 64 | typedef struct MapCacheRev { |
| 65 | uint8_t *vaddr_req; |
| 66 | target_phys_addr_t paddr_index; |
Stefano Stabellini | c13390c | 2011-05-19 18:35:42 +0100 | [diff] [blame] | 67 | target_phys_addr_t size; |
Jun Nakajima | 432d268 | 2010-08-31 16:41:25 +0100 | [diff] [blame] | 68 | QTAILQ_ENTRY(MapCacheRev) next; |
| 69 | } MapCacheRev; |
| 70 | |
| 71 | typedef struct MapCache { |
| 72 | MapCacheEntry *entry; |
| 73 | unsigned long nr_buckets; |
| 74 | QTAILQ_HEAD(map_cache_head, MapCacheRev) locked_entries; |
| 75 | |
| 76 | /* For most cases (>99.9%), the page address is the same. */ |
| 77 | target_phys_addr_t last_address_index; |
| 78 | uint8_t *last_address_vaddr; |
| 79 | unsigned long max_mcache_size; |
| 80 | unsigned int mcache_bucket_shift; |
| 81 | } MapCache; |
| 82 | |
| 83 | static MapCache *mapcache; |
| 84 | |
Stefano Stabellini | c13390c | 2011-05-19 18:35:42 +0100 | [diff] [blame] | 85 | static inline int test_bits(int nr, int size, const unsigned long *addr) |
| 86 | { |
| 87 | unsigned long res = find_next_zero_bit(addr, size + nr, nr); |
| 88 | if (res >= nr + size) |
| 89 | return 1; |
| 90 | else |
| 91 | return 0; |
| 92 | } |
| 93 | |
Jan Kiszka | e41d7c6 | 2011-06-21 22:59:08 +0200 | [diff] [blame] | 94 | void xen_map_cache_init(void) |
Jun Nakajima | 432d268 | 2010-08-31 16:41:25 +0100 | [diff] [blame] | 95 | { |
| 96 | unsigned long size; |
| 97 | struct rlimit rlimit_as; |
| 98 | |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 99 | mapcache = g_malloc0(sizeof (MapCache)); |
Jun Nakajima | 432d268 | 2010-08-31 16:41:25 +0100 | [diff] [blame] | 100 | |
| 101 | QTAILQ_INIT(&mapcache->locked_entries); |
| 102 | mapcache->last_address_index = -1; |
| 103 | |
Anthony PERARD | 56c119e | 2011-09-09 12:50:18 +0000 | [diff] [blame] | 104 | if (geteuid() == 0) { |
| 105 | rlimit_as.rlim_cur = RLIM_INFINITY; |
| 106 | rlimit_as.rlim_max = RLIM_INFINITY; |
| 107 | mapcache->max_mcache_size = MCACHE_MAX_SIZE; |
John Baboval | ea6c5f8 | 2011-03-22 14:50:28 +0000 | [diff] [blame] | 108 | } else { |
Anthony PERARD | 56c119e | 2011-09-09 12:50:18 +0000 | [diff] [blame] | 109 | getrlimit(RLIMIT_AS, &rlimit_as); |
| 110 | rlimit_as.rlim_cur = rlimit_as.rlim_max; |
| 111 | |
| 112 | if (rlimit_as.rlim_max != RLIM_INFINITY) { |
| 113 | fprintf(stderr, "Warning: QEMU's maximum size of virtual" |
| 114 | " memory is not infinity.\n"); |
| 115 | } |
| 116 | if (rlimit_as.rlim_max < MCACHE_MAX_SIZE + NON_MCACHE_MEMORY_SIZE) { |
| 117 | mapcache->max_mcache_size = rlimit_as.rlim_max - |
| 118 | NON_MCACHE_MEMORY_SIZE; |
| 119 | } else { |
| 120 | mapcache->max_mcache_size = MCACHE_MAX_SIZE; |
| 121 | } |
John Baboval | ea6c5f8 | 2011-03-22 14:50:28 +0000 | [diff] [blame] | 122 | } |
| 123 | |
Jun Nakajima | 432d268 | 2010-08-31 16:41:25 +0100 | [diff] [blame] | 124 | setrlimit(RLIMIT_AS, &rlimit_as); |
Jun Nakajima | 432d268 | 2010-08-31 16:41:25 +0100 | [diff] [blame] | 125 | |
| 126 | mapcache->nr_buckets = |
| 127 | (((mapcache->max_mcache_size >> XC_PAGE_SHIFT) + |
| 128 | (1UL << (MCACHE_BUCKET_SHIFT - XC_PAGE_SHIFT)) - 1) >> |
| 129 | (MCACHE_BUCKET_SHIFT - XC_PAGE_SHIFT)); |
| 130 | |
| 131 | size = mapcache->nr_buckets * sizeof (MapCacheEntry); |
| 132 | size = (size + XC_PAGE_SIZE - 1) & ~(XC_PAGE_SIZE - 1); |
Jan Kiszka | e41d7c6 | 2011-06-21 22:59:08 +0200 | [diff] [blame] | 133 | DPRINTF("%s, nr_buckets = %lx size %lu\n", __func__, |
| 134 | mapcache->nr_buckets, size); |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 135 | mapcache->entry = g_malloc0(size); |
Jun Nakajima | 432d268 | 2010-08-31 16:41:25 +0100 | [diff] [blame] | 136 | } |
| 137 | |
Jan Kiszka | e41d7c6 | 2011-06-21 22:59:08 +0200 | [diff] [blame] | 138 | static void xen_remap_bucket(MapCacheEntry *entry, |
| 139 | target_phys_addr_t size, |
| 140 | target_phys_addr_t address_index) |
Jun Nakajima | 432d268 | 2010-08-31 16:41:25 +0100 | [diff] [blame] | 141 | { |
| 142 | uint8_t *vaddr_base; |
| 143 | xen_pfn_t *pfns; |
| 144 | int *err; |
John Baboval | ea6c5f8 | 2011-03-22 14:50:28 +0000 | [diff] [blame] | 145 | unsigned int i; |
Jun Nakajima | 432d268 | 2010-08-31 16:41:25 +0100 | [diff] [blame] | 146 | target_phys_addr_t nb_pfn = size >> XC_PAGE_SHIFT; |
| 147 | |
Jan Kiszka | e41d7c6 | 2011-06-21 22:59:08 +0200 | [diff] [blame] | 148 | trace_xen_remap_bucket(address_index); |
Jun Nakajima | 432d268 | 2010-08-31 16:41:25 +0100 | [diff] [blame] | 149 | |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 150 | pfns = g_malloc0(nb_pfn * sizeof (xen_pfn_t)); |
| 151 | err = g_malloc0(nb_pfn * sizeof (int)); |
Jun Nakajima | 432d268 | 2010-08-31 16:41:25 +0100 | [diff] [blame] | 152 | |
| 153 | if (entry->vaddr_base != NULL) { |
Stefano Stabellini | c13390c | 2011-05-19 18:35:42 +0100 | [diff] [blame] | 154 | if (munmap(entry->vaddr_base, entry->size) != 0) { |
Jun Nakajima | 432d268 | 2010-08-31 16:41:25 +0100 | [diff] [blame] | 155 | perror("unmap fails"); |
| 156 | exit(-1); |
| 157 | } |
| 158 | } |
Stefano Stabellini | c13390c | 2011-05-19 18:35:42 +0100 | [diff] [blame] | 159 | if (entry->valid_mapping != NULL) { |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 160 | g_free(entry->valid_mapping); |
Stefano Stabellini | c13390c | 2011-05-19 18:35:42 +0100 | [diff] [blame] | 161 | entry->valid_mapping = NULL; |
| 162 | } |
Jun Nakajima | 432d268 | 2010-08-31 16:41:25 +0100 | [diff] [blame] | 163 | |
| 164 | for (i = 0; i < nb_pfn; i++) { |
| 165 | pfns[i] = (address_index << (MCACHE_BUCKET_SHIFT-XC_PAGE_SHIFT)) + i; |
| 166 | } |
| 167 | |
| 168 | vaddr_base = xc_map_foreign_bulk(xen_xc, xen_domid, PROT_READ|PROT_WRITE, |
| 169 | pfns, err, nb_pfn); |
| 170 | if (vaddr_base == NULL) { |
| 171 | perror("xc_map_foreign_bulk"); |
| 172 | exit(-1); |
| 173 | } |
| 174 | |
| 175 | entry->vaddr_base = vaddr_base; |
| 176 | entry->paddr_index = address_index; |
Stefano Stabellini | c13390c | 2011-05-19 18:35:42 +0100 | [diff] [blame] | 177 | entry->size = size; |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 178 | entry->valid_mapping = (unsigned long *) g_malloc0(sizeof(unsigned long) * |
Stefano Stabellini | c13390c | 2011-05-19 18:35:42 +0100 | [diff] [blame] | 179 | BITS_TO_LONGS(size >> XC_PAGE_SHIFT)); |
Jun Nakajima | 432d268 | 2010-08-31 16:41:25 +0100 | [diff] [blame] | 180 | |
John Baboval | ea6c5f8 | 2011-03-22 14:50:28 +0000 | [diff] [blame] | 181 | bitmap_zero(entry->valid_mapping, nb_pfn); |
| 182 | for (i = 0; i < nb_pfn; i++) { |
| 183 | if (!err[i]) { |
| 184 | bitmap_set(entry->valid_mapping, i, 1); |
Jun Nakajima | 432d268 | 2010-08-31 16:41:25 +0100 | [diff] [blame] | 185 | } |
Jun Nakajima | 432d268 | 2010-08-31 16:41:25 +0100 | [diff] [blame] | 186 | } |
| 187 | |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 188 | g_free(pfns); |
| 189 | g_free(err); |
Jun Nakajima | 432d268 | 2010-08-31 16:41:25 +0100 | [diff] [blame] | 190 | } |
| 191 | |
Jan Kiszka | e41d7c6 | 2011-06-21 22:59:08 +0200 | [diff] [blame] | 192 | uint8_t *xen_map_cache(target_phys_addr_t phys_addr, target_phys_addr_t size, |
| 193 | uint8_t lock) |
Jun Nakajima | 432d268 | 2010-08-31 16:41:25 +0100 | [diff] [blame] | 194 | { |
| 195 | MapCacheEntry *entry, *pentry = NULL; |
| 196 | target_phys_addr_t address_index = phys_addr >> MCACHE_BUCKET_SHIFT; |
| 197 | target_phys_addr_t address_offset = phys_addr & (MCACHE_BUCKET_SIZE - 1); |
Stefano Stabellini | c13390c | 2011-05-19 18:35:42 +0100 | [diff] [blame] | 198 | target_phys_addr_t __size = size; |
Jun Nakajima | 432d268 | 2010-08-31 16:41:25 +0100 | [diff] [blame] | 199 | |
Jan Kiszka | e41d7c6 | 2011-06-21 22:59:08 +0200 | [diff] [blame] | 200 | trace_xen_map_cache(phys_addr); |
Jun Nakajima | 432d268 | 2010-08-31 16:41:25 +0100 | [diff] [blame] | 201 | |
Stefano Stabellini | c13390c | 2011-05-19 18:35:42 +0100 | [diff] [blame] | 202 | if (address_index == mapcache->last_address_index && !lock && !__size) { |
Jan Kiszka | e41d7c6 | 2011-06-21 22:59:08 +0200 | [diff] [blame] | 203 | trace_xen_map_cache_return(mapcache->last_address_vaddr + address_offset); |
Jun Nakajima | 432d268 | 2010-08-31 16:41:25 +0100 | [diff] [blame] | 204 | return mapcache->last_address_vaddr + address_offset; |
| 205 | } |
| 206 | |
Stefano Stabellini | c13390c | 2011-05-19 18:35:42 +0100 | [diff] [blame] | 207 | /* size is always a multiple of MCACHE_BUCKET_SIZE */ |
| 208 | if ((address_offset + (__size % MCACHE_BUCKET_SIZE)) > MCACHE_BUCKET_SIZE) |
| 209 | __size += MCACHE_BUCKET_SIZE; |
| 210 | if (__size % MCACHE_BUCKET_SIZE) |
| 211 | __size += MCACHE_BUCKET_SIZE - (__size % MCACHE_BUCKET_SIZE); |
| 212 | if (!__size) |
| 213 | __size = MCACHE_BUCKET_SIZE; |
| 214 | |
Jun Nakajima | 432d268 | 2010-08-31 16:41:25 +0100 | [diff] [blame] | 215 | entry = &mapcache->entry[address_index % mapcache->nr_buckets]; |
| 216 | |
Stefano Stabellini | c13390c | 2011-05-19 18:35:42 +0100 | [diff] [blame] | 217 | while (entry && entry->lock && entry->vaddr_base && |
| 218 | (entry->paddr_index != address_index || entry->size != __size || |
| 219 | !test_bits(address_offset >> XC_PAGE_SHIFT, size >> XC_PAGE_SHIFT, |
| 220 | entry->valid_mapping))) { |
Jun Nakajima | 432d268 | 2010-08-31 16:41:25 +0100 | [diff] [blame] | 221 | pentry = entry; |
| 222 | entry = entry->next; |
| 223 | } |
| 224 | if (!entry) { |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 225 | entry = g_malloc0(sizeof (MapCacheEntry)); |
Jun Nakajima | 432d268 | 2010-08-31 16:41:25 +0100 | [diff] [blame] | 226 | pentry->next = entry; |
Jan Kiszka | e41d7c6 | 2011-06-21 22:59:08 +0200 | [diff] [blame] | 227 | xen_remap_bucket(entry, __size, address_index); |
Jun Nakajima | 432d268 | 2010-08-31 16:41:25 +0100 | [diff] [blame] | 228 | } else if (!entry->lock) { |
| 229 | if (!entry->vaddr_base || entry->paddr_index != address_index || |
Stefano Stabellini | c13390c | 2011-05-19 18:35:42 +0100 | [diff] [blame] | 230 | entry->size != __size || |
| 231 | !test_bits(address_offset >> XC_PAGE_SHIFT, size >> XC_PAGE_SHIFT, |
| 232 | entry->valid_mapping)) { |
Jan Kiszka | e41d7c6 | 2011-06-21 22:59:08 +0200 | [diff] [blame] | 233 | xen_remap_bucket(entry, __size, address_index); |
Jun Nakajima | 432d268 | 2010-08-31 16:41:25 +0100 | [diff] [blame] | 234 | } |
| 235 | } |
| 236 | |
Stefano Stabellini | c13390c | 2011-05-19 18:35:42 +0100 | [diff] [blame] | 237 | if(!test_bits(address_offset >> XC_PAGE_SHIFT, size >> XC_PAGE_SHIFT, |
| 238 | entry->valid_mapping)) { |
Jun Nakajima | 432d268 | 2010-08-31 16:41:25 +0100 | [diff] [blame] | 239 | mapcache->last_address_index = -1; |
Jan Kiszka | e41d7c6 | 2011-06-21 22:59:08 +0200 | [diff] [blame] | 240 | trace_xen_map_cache_return(NULL); |
Jun Nakajima | 432d268 | 2010-08-31 16:41:25 +0100 | [diff] [blame] | 241 | return NULL; |
| 242 | } |
| 243 | |
| 244 | mapcache->last_address_index = address_index; |
| 245 | mapcache->last_address_vaddr = entry->vaddr_base; |
| 246 | if (lock) { |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 247 | MapCacheRev *reventry = g_malloc0(sizeof(MapCacheRev)); |
Jun Nakajima | 432d268 | 2010-08-31 16:41:25 +0100 | [diff] [blame] | 248 | entry->lock++; |
| 249 | reventry->vaddr_req = mapcache->last_address_vaddr + address_offset; |
| 250 | reventry->paddr_index = mapcache->last_address_index; |
Stefano Stabellini | c13390c | 2011-05-19 18:35:42 +0100 | [diff] [blame] | 251 | reventry->size = entry->size; |
Jun Nakajima | 432d268 | 2010-08-31 16:41:25 +0100 | [diff] [blame] | 252 | QTAILQ_INSERT_HEAD(&mapcache->locked_entries, reventry, next); |
| 253 | } |
| 254 | |
Jan Kiszka | e41d7c6 | 2011-06-21 22:59:08 +0200 | [diff] [blame] | 255 | trace_xen_map_cache_return(mapcache->last_address_vaddr + address_offset); |
Jun Nakajima | 432d268 | 2010-08-31 16:41:25 +0100 | [diff] [blame] | 256 | return mapcache->last_address_vaddr + address_offset; |
| 257 | } |
| 258 | |
Jan Kiszka | e41d7c6 | 2011-06-21 22:59:08 +0200 | [diff] [blame] | 259 | ram_addr_t xen_ram_addr_from_mapcache(void *ptr) |
Jun Nakajima | 432d268 | 2010-08-31 16:41:25 +0100 | [diff] [blame] | 260 | { |
Stefan Berger | ecf169b | 2011-07-26 10:33:11 -0400 | [diff] [blame] | 261 | MapCacheEntry *entry = NULL; |
Jun Nakajima | 432d268 | 2010-08-31 16:41:25 +0100 | [diff] [blame] | 262 | MapCacheRev *reventry; |
| 263 | target_phys_addr_t paddr_index; |
Stefano Stabellini | c13390c | 2011-05-19 18:35:42 +0100 | [diff] [blame] | 264 | target_phys_addr_t size; |
Jun Nakajima | 432d268 | 2010-08-31 16:41:25 +0100 | [diff] [blame] | 265 | int found = 0; |
| 266 | |
| 267 | QTAILQ_FOREACH(reventry, &mapcache->locked_entries, next) { |
| 268 | if (reventry->vaddr_req == ptr) { |
| 269 | paddr_index = reventry->paddr_index; |
Stefano Stabellini | c13390c | 2011-05-19 18:35:42 +0100 | [diff] [blame] | 270 | size = reventry->size; |
Jun Nakajima | 432d268 | 2010-08-31 16:41:25 +0100 | [diff] [blame] | 271 | found = 1; |
| 272 | break; |
| 273 | } |
| 274 | } |
| 275 | if (!found) { |
Jan Kiszka | e41d7c6 | 2011-06-21 22:59:08 +0200 | [diff] [blame] | 276 | fprintf(stderr, "%s, could not find %p\n", __func__, ptr); |
Jun Nakajima | 432d268 | 2010-08-31 16:41:25 +0100 | [diff] [blame] | 277 | QTAILQ_FOREACH(reventry, &mapcache->locked_entries, next) { |
| 278 | DPRINTF(" "TARGET_FMT_plx" -> %p is present\n", reventry->paddr_index, |
| 279 | reventry->vaddr_req); |
| 280 | } |
| 281 | abort(); |
| 282 | return 0; |
| 283 | } |
| 284 | |
Stefano Stabellini | c13390c | 2011-05-19 18:35:42 +0100 | [diff] [blame] | 285 | entry = &mapcache->entry[paddr_index % mapcache->nr_buckets]; |
| 286 | while (entry && (entry->paddr_index != paddr_index || entry->size != size)) { |
Stefano Stabellini | c13390c | 2011-05-19 18:35:42 +0100 | [diff] [blame] | 287 | entry = entry->next; |
| 288 | } |
| 289 | if (!entry) { |
| 290 | DPRINTF("Trying to find address %p that is not in the mapcache!\n", ptr); |
| 291 | return 0; |
| 292 | } |
| 293 | return (reventry->paddr_index << MCACHE_BUCKET_SHIFT) + |
| 294 | ((unsigned long) ptr - (unsigned long) entry->vaddr_base); |
Jun Nakajima | 432d268 | 2010-08-31 16:41:25 +0100 | [diff] [blame] | 295 | } |
| 296 | |
Jan Kiszka | e41d7c6 | 2011-06-21 22:59:08 +0200 | [diff] [blame] | 297 | void xen_invalidate_map_cache_entry(uint8_t *buffer) |
Jun Nakajima | 432d268 | 2010-08-31 16:41:25 +0100 | [diff] [blame] | 298 | { |
| 299 | MapCacheEntry *entry = NULL, *pentry = NULL; |
| 300 | MapCacheRev *reventry; |
| 301 | target_phys_addr_t paddr_index; |
Stefano Stabellini | c13390c | 2011-05-19 18:35:42 +0100 | [diff] [blame] | 302 | target_phys_addr_t size; |
Jun Nakajima | 432d268 | 2010-08-31 16:41:25 +0100 | [diff] [blame] | 303 | int found = 0; |
| 304 | |
| 305 | if (mapcache->last_address_vaddr == buffer) { |
| 306 | mapcache->last_address_index = -1; |
| 307 | } |
| 308 | |
| 309 | QTAILQ_FOREACH(reventry, &mapcache->locked_entries, next) { |
| 310 | if (reventry->vaddr_req == buffer) { |
| 311 | paddr_index = reventry->paddr_index; |
Stefano Stabellini | c13390c | 2011-05-19 18:35:42 +0100 | [diff] [blame] | 312 | size = reventry->size; |
Jun Nakajima | 432d268 | 2010-08-31 16:41:25 +0100 | [diff] [blame] | 313 | found = 1; |
| 314 | break; |
| 315 | } |
| 316 | } |
| 317 | if (!found) { |
Jan Kiszka | e41d7c6 | 2011-06-21 22:59:08 +0200 | [diff] [blame] | 318 | DPRINTF("%s, could not find %p\n", __func__, buffer); |
Jun Nakajima | 432d268 | 2010-08-31 16:41:25 +0100 | [diff] [blame] | 319 | QTAILQ_FOREACH(reventry, &mapcache->locked_entries, next) { |
| 320 | DPRINTF(" "TARGET_FMT_plx" -> %p is present\n", reventry->paddr_index, reventry->vaddr_req); |
| 321 | } |
| 322 | return; |
| 323 | } |
| 324 | QTAILQ_REMOVE(&mapcache->locked_entries, reventry, next); |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 325 | g_free(reventry); |
Jun Nakajima | 432d268 | 2010-08-31 16:41:25 +0100 | [diff] [blame] | 326 | |
| 327 | entry = &mapcache->entry[paddr_index % mapcache->nr_buckets]; |
Stefano Stabellini | c13390c | 2011-05-19 18:35:42 +0100 | [diff] [blame] | 328 | while (entry && (entry->paddr_index != paddr_index || entry->size != size)) { |
Jun Nakajima | 432d268 | 2010-08-31 16:41:25 +0100 | [diff] [blame] | 329 | pentry = entry; |
| 330 | entry = entry->next; |
| 331 | } |
| 332 | if (!entry) { |
| 333 | DPRINTF("Trying to unmap address %p that is not in the mapcache!\n", buffer); |
| 334 | return; |
| 335 | } |
| 336 | entry->lock--; |
| 337 | if (entry->lock > 0 || pentry == NULL) { |
| 338 | return; |
| 339 | } |
| 340 | |
| 341 | pentry->next = entry->next; |
Stefano Stabellini | c13390c | 2011-05-19 18:35:42 +0100 | [diff] [blame] | 342 | if (munmap(entry->vaddr_base, entry->size) != 0) { |
Jun Nakajima | 432d268 | 2010-08-31 16:41:25 +0100 | [diff] [blame] | 343 | perror("unmap fails"); |
| 344 | exit(-1); |
| 345 | } |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 346 | g_free(entry->valid_mapping); |
| 347 | g_free(entry); |
Jun Nakajima | 432d268 | 2010-08-31 16:41:25 +0100 | [diff] [blame] | 348 | } |
| 349 | |
Jan Kiszka | e41d7c6 | 2011-06-21 22:59:08 +0200 | [diff] [blame] | 350 | void xen_invalidate_map_cache(void) |
Jun Nakajima | 432d268 | 2010-08-31 16:41:25 +0100 | [diff] [blame] | 351 | { |
| 352 | unsigned long i; |
| 353 | MapCacheRev *reventry; |
| 354 | |
| 355 | /* Flush pending AIO before destroying the mapcache */ |
Stefan Hajnoczi | 922453b | 2011-11-30 12:23:43 +0000 | [diff] [blame] | 356 | bdrv_drain_all(); |
Jun Nakajima | 432d268 | 2010-08-31 16:41:25 +0100 | [diff] [blame] | 357 | |
| 358 | QTAILQ_FOREACH(reventry, &mapcache->locked_entries, next) { |
| 359 | DPRINTF("There should be no locked mappings at this time, " |
| 360 | "but "TARGET_FMT_plx" -> %p is present\n", |
| 361 | reventry->paddr_index, reventry->vaddr_req); |
| 362 | } |
| 363 | |
| 364 | mapcache_lock(); |
| 365 | |
| 366 | for (i = 0; i < mapcache->nr_buckets; i++) { |
| 367 | MapCacheEntry *entry = &mapcache->entry[i]; |
| 368 | |
| 369 | if (entry->vaddr_base == NULL) { |
| 370 | continue; |
| 371 | } |
| 372 | |
Stefano Stabellini | c13390c | 2011-05-19 18:35:42 +0100 | [diff] [blame] | 373 | if (munmap(entry->vaddr_base, entry->size) != 0) { |
Jun Nakajima | 432d268 | 2010-08-31 16:41:25 +0100 | [diff] [blame] | 374 | perror("unmap fails"); |
| 375 | exit(-1); |
| 376 | } |
| 377 | |
| 378 | entry->paddr_index = 0; |
| 379 | entry->vaddr_base = NULL; |
Stefano Stabellini | c13390c | 2011-05-19 18:35:42 +0100 | [diff] [blame] | 380 | entry->size = 0; |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 381 | g_free(entry->valid_mapping); |
Stefano Stabellini | c13390c | 2011-05-19 18:35:42 +0100 | [diff] [blame] | 382 | entry->valid_mapping = NULL; |
Jun Nakajima | 432d268 | 2010-08-31 16:41:25 +0100 | [diff] [blame] | 383 | } |
| 384 | |
| 385 | mapcache->last_address_index = -1; |
| 386 | mapcache->last_address_vaddr = NULL; |
| 387 | |
| 388 | mapcache_unlock(); |
| 389 | } |