]> git.proxmox.com Git - qemu.git/blame - xen-mapcache.c
qdev-properties-system.c: Allow vlan or netdev for -device, not both
[qemu.git] / xen-mapcache.c
CommitLineData
432d268c
JN
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 *
6b620ca3
PB
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.
432d268c
JN
9 */
10
11#include "config.h"
12
13#include <sys/resource.h>
14
0d09e41a 15#include "hw/xen/xen_backend.h"
9c17d615 16#include "sysemu/blockdev.h"
1de7afc9 17#include "qemu/bitmap.h"
432d268c
JN
18
19#include <xen/hvm/params.h>
20#include <sys/mman.h>
21
9c17d615 22#include "sysemu/xen-mapcache.h"
432d268c
JN
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
ea6c5f8f 38# define MCACHE_MAX_SIZE (1UL<<31) /* 2GB Cap */
432d268c
JN
39#elif defined(__x86_64__)
40# define MCACHE_BUCKET_SHIFT 20
ea6c5f8f 41# define MCACHE_MAX_SIZE (1UL<<35) /* 32GB Cap */
432d268c
JN
42#endif
43#define MCACHE_BUCKET_SIZE (1UL << MCACHE_BUCKET_SHIFT)
44
56c119e5
AP
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
e41d7c69
JK
52#define mapcache_lock() ((void)0)
53#define mapcache_unlock() ((void)0)
54
432d268c 55typedef struct MapCacheEntry {
a8170e5e 56 hwaddr paddr_index;
432d268c 57 uint8_t *vaddr_base;
c13390cd 58 unsigned long *valid_mapping;
432d268c 59 uint8_t lock;
a8170e5e 60 hwaddr size;
432d268c
JN
61 struct MapCacheEntry *next;
62} MapCacheEntry;
63
64typedef struct MapCacheRev {
65 uint8_t *vaddr_req;
a8170e5e
AK
66 hwaddr paddr_index;
67 hwaddr size;
432d268c
JN
68 QTAILQ_ENTRY(MapCacheRev) next;
69} MapCacheRev;
70
71typedef 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. */
e2deee3e 77 MapCacheEntry *last_entry;
432d268c
JN
78 unsigned long max_mcache_size;
79 unsigned int mcache_bucket_shift;
cd1ba7de
AP
80
81 phys_offset_to_gaddr_t phys_offset_to_gaddr;
82 void *opaque;
432d268c
JN
83} MapCache;
84
85static MapCache *mapcache;
86
c13390cd
SS
87static inline int test_bits(int nr, int size, const unsigned long *addr)
88{
89 unsigned long res = find_next_zero_bit(addr, size + nr, nr);
90 if (res >= nr + size)
91 return 1;
92 else
93 return 0;
94}
95
cd1ba7de 96void xen_map_cache_init(phys_offset_to_gaddr_t f, void *opaque)
432d268c
JN
97{
98 unsigned long size;
99 struct rlimit rlimit_as;
100
7267c094 101 mapcache = g_malloc0(sizeof (MapCache));
432d268c 102
cd1ba7de
AP
103 mapcache->phys_offset_to_gaddr = f;
104 mapcache->opaque = opaque;
105
432d268c 106 QTAILQ_INIT(&mapcache->locked_entries);
432d268c 107
56c119e5
AP
108 if (geteuid() == 0) {
109 rlimit_as.rlim_cur = RLIM_INFINITY;
110 rlimit_as.rlim_max = RLIM_INFINITY;
111 mapcache->max_mcache_size = MCACHE_MAX_SIZE;
ea6c5f8f 112 } else {
56c119e5
AP
113 getrlimit(RLIMIT_AS, &rlimit_as);
114 rlimit_as.rlim_cur = rlimit_as.rlim_max;
115
116 if (rlimit_as.rlim_max != RLIM_INFINITY) {
117 fprintf(stderr, "Warning: QEMU's maximum size of virtual"
118 " memory is not infinity.\n");
119 }
120 if (rlimit_as.rlim_max < MCACHE_MAX_SIZE + NON_MCACHE_MEMORY_SIZE) {
121 mapcache->max_mcache_size = rlimit_as.rlim_max -
122 NON_MCACHE_MEMORY_SIZE;
123 } else {
124 mapcache->max_mcache_size = MCACHE_MAX_SIZE;
125 }
ea6c5f8f
JB
126 }
127
432d268c 128 setrlimit(RLIMIT_AS, &rlimit_as);
432d268c
JN
129
130 mapcache->nr_buckets =
131 (((mapcache->max_mcache_size >> XC_PAGE_SHIFT) +
132 (1UL << (MCACHE_BUCKET_SHIFT - XC_PAGE_SHIFT)) - 1) >>
133 (MCACHE_BUCKET_SHIFT - XC_PAGE_SHIFT));
134
135 size = mapcache->nr_buckets * sizeof (MapCacheEntry);
136 size = (size + XC_PAGE_SIZE - 1) & ~(XC_PAGE_SIZE - 1);
e41d7c69
JK
137 DPRINTF("%s, nr_buckets = %lx size %lu\n", __func__,
138 mapcache->nr_buckets, size);
7267c094 139 mapcache->entry = g_malloc0(size);
432d268c
JN
140}
141
e41d7c69 142static void xen_remap_bucket(MapCacheEntry *entry,
a8170e5e
AK
143 hwaddr size,
144 hwaddr address_index)
432d268c
JN
145{
146 uint8_t *vaddr_base;
147 xen_pfn_t *pfns;
148 int *err;
ea6c5f8f 149 unsigned int i;
a8170e5e 150 hwaddr nb_pfn = size >> XC_PAGE_SHIFT;
432d268c 151
e41d7c69 152 trace_xen_remap_bucket(address_index);
432d268c 153
7267c094
AL
154 pfns = g_malloc0(nb_pfn * sizeof (xen_pfn_t));
155 err = g_malloc0(nb_pfn * sizeof (int));
432d268c
JN
156
157 if (entry->vaddr_base != NULL) {
c13390cd 158 if (munmap(entry->vaddr_base, entry->size) != 0) {
432d268c
JN
159 perror("unmap fails");
160 exit(-1);
161 }
162 }
c13390cd 163 if (entry->valid_mapping != NULL) {
7267c094 164 g_free(entry->valid_mapping);
c13390cd
SS
165 entry->valid_mapping = NULL;
166 }
432d268c
JN
167
168 for (i = 0; i < nb_pfn; i++) {
169 pfns[i] = (address_index << (MCACHE_BUCKET_SHIFT-XC_PAGE_SHIFT)) + i;
170 }
171
172 vaddr_base = xc_map_foreign_bulk(xen_xc, xen_domid, PROT_READ|PROT_WRITE,
173 pfns, err, nb_pfn);
174 if (vaddr_base == NULL) {
175 perror("xc_map_foreign_bulk");
176 exit(-1);
177 }
178
179 entry->vaddr_base = vaddr_base;
180 entry->paddr_index = address_index;
c13390cd 181 entry->size = size;
7267c094 182 entry->valid_mapping = (unsigned long *) g_malloc0(sizeof(unsigned long) *
c13390cd 183 BITS_TO_LONGS(size >> XC_PAGE_SHIFT));
432d268c 184
ea6c5f8f
JB
185 bitmap_zero(entry->valid_mapping, nb_pfn);
186 for (i = 0; i < nb_pfn; i++) {
187 if (!err[i]) {
188 bitmap_set(entry->valid_mapping, i, 1);
432d268c 189 }
432d268c
JN
190 }
191
7267c094
AL
192 g_free(pfns);
193 g_free(err);
432d268c
JN
194}
195
a8170e5e 196uint8_t *xen_map_cache(hwaddr phys_addr, hwaddr size,
e41d7c69 197 uint8_t lock)
432d268c
JN
198{
199 MapCacheEntry *entry, *pentry = NULL;
a8170e5e
AK
200 hwaddr address_index;
201 hwaddr address_offset;
202 hwaddr __size = size;
044d4e1a 203 hwaddr __test_bit_size = size;
cd1ba7de
AP
204 bool translated = false;
205
206tryagain:
207 address_index = phys_addr >> MCACHE_BUCKET_SHIFT;
208 address_offset = phys_addr & (MCACHE_BUCKET_SIZE - 1);
432d268c 209
e41d7c69 210 trace_xen_map_cache(phys_addr);
432d268c 211
044d4e1a
H
212 /* __test_bit_size is always a multiple of XC_PAGE_SIZE */
213 if (size) {
214 __test_bit_size = size + (phys_addr & (XC_PAGE_SIZE - 1));
215
216 if (__test_bit_size % XC_PAGE_SIZE) {
217 __test_bit_size += XC_PAGE_SIZE - (__test_bit_size % XC_PAGE_SIZE);
218 }
219 } else {
220 __test_bit_size = XC_PAGE_SIZE;
221 }
222
e2deee3e
SS
223 if (mapcache->last_entry != NULL &&
224 mapcache->last_entry->paddr_index == address_index &&
044d4e1a
H
225 !lock && !__size &&
226 test_bits(address_offset >> XC_PAGE_SHIFT,
227 __test_bit_size >> XC_PAGE_SHIFT,
228 mapcache->last_entry->valid_mapping)) {
e2deee3e
SS
229 trace_xen_map_cache_return(mapcache->last_entry->vaddr_base + address_offset);
230 return mapcache->last_entry->vaddr_base + address_offset;
432d268c
JN
231 }
232
c13390cd 233 /* size is always a multiple of MCACHE_BUCKET_SIZE */
09ab48ee
AP
234 if (size) {
235 __size = size + address_offset;
236 if (__size % MCACHE_BUCKET_SIZE) {
237 __size += MCACHE_BUCKET_SIZE - (__size % MCACHE_BUCKET_SIZE);
238 }
239 } else {
c13390cd 240 __size = MCACHE_BUCKET_SIZE;
09ab48ee 241 }
c13390cd 242
432d268c
JN
243 entry = &mapcache->entry[address_index % mapcache->nr_buckets];
244
c13390cd
SS
245 while (entry && entry->lock && entry->vaddr_base &&
246 (entry->paddr_index != address_index || entry->size != __size ||
044d4e1a
H
247 !test_bits(address_offset >> XC_PAGE_SHIFT,
248 __test_bit_size >> XC_PAGE_SHIFT,
c13390cd 249 entry->valid_mapping))) {
432d268c
JN
250 pentry = entry;
251 entry = entry->next;
252 }
253 if (!entry) {
7267c094 254 entry = g_malloc0(sizeof (MapCacheEntry));
432d268c 255 pentry->next = entry;
e41d7c69 256 xen_remap_bucket(entry, __size, address_index);
432d268c
JN
257 } else if (!entry->lock) {
258 if (!entry->vaddr_base || entry->paddr_index != address_index ||
c13390cd 259 entry->size != __size ||
044d4e1a
H
260 !test_bits(address_offset >> XC_PAGE_SHIFT,
261 __test_bit_size >> XC_PAGE_SHIFT,
c13390cd 262 entry->valid_mapping)) {
e41d7c69 263 xen_remap_bucket(entry, __size, address_index);
432d268c
JN
264 }
265 }
266
044d4e1a
H
267 if(!test_bits(address_offset >> XC_PAGE_SHIFT,
268 __test_bit_size >> XC_PAGE_SHIFT,
c13390cd 269 entry->valid_mapping)) {
e2deee3e 270 mapcache->last_entry = NULL;
cd1ba7de
AP
271 if (!translated && mapcache->phys_offset_to_gaddr) {
272 phys_addr = mapcache->phys_offset_to_gaddr(phys_addr, size, mapcache->opaque);
273 translated = true;
274 goto tryagain;
275 }
e41d7c69 276 trace_xen_map_cache_return(NULL);
432d268c
JN
277 return NULL;
278 }
279
e2deee3e 280 mapcache->last_entry = entry;
432d268c 281 if (lock) {
7267c094 282 MapCacheRev *reventry = g_malloc0(sizeof(MapCacheRev));
432d268c 283 entry->lock++;
e2deee3e
SS
284 reventry->vaddr_req = mapcache->last_entry->vaddr_base + address_offset;
285 reventry->paddr_index = mapcache->last_entry->paddr_index;
c13390cd 286 reventry->size = entry->size;
432d268c
JN
287 QTAILQ_INSERT_HEAD(&mapcache->locked_entries, reventry, next);
288 }
289
e2deee3e
SS
290 trace_xen_map_cache_return(mapcache->last_entry->vaddr_base + address_offset);
291 return mapcache->last_entry->vaddr_base + address_offset;
432d268c
JN
292}
293
e41d7c69 294ram_addr_t xen_ram_addr_from_mapcache(void *ptr)
432d268c 295{
ecf169b7 296 MapCacheEntry *entry = NULL;
432d268c 297 MapCacheRev *reventry;
a8170e5e
AK
298 hwaddr paddr_index;
299 hwaddr size;
432d268c
JN
300 int found = 0;
301
302 QTAILQ_FOREACH(reventry, &mapcache->locked_entries, next) {
303 if (reventry->vaddr_req == ptr) {
304 paddr_index = reventry->paddr_index;
c13390cd 305 size = reventry->size;
432d268c
JN
306 found = 1;
307 break;
308 }
309 }
310 if (!found) {
e41d7c69 311 fprintf(stderr, "%s, could not find %p\n", __func__, ptr);
432d268c
JN
312 QTAILQ_FOREACH(reventry, &mapcache->locked_entries, next) {
313 DPRINTF(" "TARGET_FMT_plx" -> %p is present\n", reventry->paddr_index,
314 reventry->vaddr_req);
315 }
316 abort();
317 return 0;
318 }
319
c13390cd
SS
320 entry = &mapcache->entry[paddr_index % mapcache->nr_buckets];
321 while (entry && (entry->paddr_index != paddr_index || entry->size != size)) {
c13390cd
SS
322 entry = entry->next;
323 }
324 if (!entry) {
325 DPRINTF("Trying to find address %p that is not in the mapcache!\n", ptr);
326 return 0;
327 }
328 return (reventry->paddr_index << MCACHE_BUCKET_SHIFT) +
329 ((unsigned long) ptr - (unsigned long) entry->vaddr_base);
432d268c
JN
330}
331
e41d7c69 332void xen_invalidate_map_cache_entry(uint8_t *buffer)
432d268c
JN
333{
334 MapCacheEntry *entry = NULL, *pentry = NULL;
335 MapCacheRev *reventry;
a8170e5e
AK
336 hwaddr paddr_index;
337 hwaddr size;
432d268c
JN
338 int found = 0;
339
432d268c
JN
340 QTAILQ_FOREACH(reventry, &mapcache->locked_entries, next) {
341 if (reventry->vaddr_req == buffer) {
342 paddr_index = reventry->paddr_index;
c13390cd 343 size = reventry->size;
432d268c
JN
344 found = 1;
345 break;
346 }
347 }
348 if (!found) {
e41d7c69 349 DPRINTF("%s, could not find %p\n", __func__, buffer);
432d268c
JN
350 QTAILQ_FOREACH(reventry, &mapcache->locked_entries, next) {
351 DPRINTF(" "TARGET_FMT_plx" -> %p is present\n", reventry->paddr_index, reventry->vaddr_req);
352 }
353 return;
354 }
355 QTAILQ_REMOVE(&mapcache->locked_entries, reventry, next);
7267c094 356 g_free(reventry);
432d268c 357
e2deee3e
SS
358 if (mapcache->last_entry != NULL &&
359 mapcache->last_entry->paddr_index == paddr_index) {
360 mapcache->last_entry = NULL;
27b7652e
FZ
361 }
362
432d268c 363 entry = &mapcache->entry[paddr_index % mapcache->nr_buckets];
c13390cd 364 while (entry && (entry->paddr_index != paddr_index || entry->size != size)) {
432d268c
JN
365 pentry = entry;
366 entry = entry->next;
367 }
368 if (!entry) {
369 DPRINTF("Trying to unmap address %p that is not in the mapcache!\n", buffer);
370 return;
371 }
372 entry->lock--;
373 if (entry->lock > 0 || pentry == NULL) {
374 return;
375 }
376
377 pentry->next = entry->next;
c13390cd 378 if (munmap(entry->vaddr_base, entry->size) != 0) {
432d268c
JN
379 perror("unmap fails");
380 exit(-1);
381 }
7267c094
AL
382 g_free(entry->valid_mapping);
383 g_free(entry);
432d268c
JN
384}
385
e41d7c69 386void xen_invalidate_map_cache(void)
432d268c
JN
387{
388 unsigned long i;
389 MapCacheRev *reventry;
390
391 /* Flush pending AIO before destroying the mapcache */
922453bc 392 bdrv_drain_all();
432d268c
JN
393
394 QTAILQ_FOREACH(reventry, &mapcache->locked_entries, next) {
395 DPRINTF("There should be no locked mappings at this time, "
396 "but "TARGET_FMT_plx" -> %p is present\n",
397 reventry->paddr_index, reventry->vaddr_req);
398 }
399
400 mapcache_lock();
401
402 for (i = 0; i < mapcache->nr_buckets; i++) {
403 MapCacheEntry *entry = &mapcache->entry[i];
404
405 if (entry->vaddr_base == NULL) {
406 continue;
407 }
852a7cec
JG
408 if (entry->lock > 0) {
409 continue;
410 }
432d268c 411
c13390cd 412 if (munmap(entry->vaddr_base, entry->size) != 0) {
432d268c
JN
413 perror("unmap fails");
414 exit(-1);
415 }
416
417 entry->paddr_index = 0;
418 entry->vaddr_base = NULL;
c13390cd 419 entry->size = 0;
7267c094 420 g_free(entry->valid_mapping);
c13390cd 421 entry->valid_mapping = NULL;
432d268c
JN
422 }
423
e2deee3e 424 mapcache->last_entry = NULL;
432d268c
JN
425
426 mapcache_unlock();
427}