sun4m: avoid structure holes spotted by pahole
[qemu/mdroth.git] / xen-mapcache.c
blob5b247ee092f303574507e6e67cac0d1ed6f5f24a
1 /*
2 * Copyright (C) 2011 Citrix Ltd.
4 * This work is licensed under the terms of the GNU GPL, version 2. See
5 * the COPYING file in the top-level directory.
7 */
9 #include "config.h"
11 #include <sys/resource.h>
13 #include "hw/xen_backend.h"
14 #include "blockdev.h"
15 #include "bitmap.h"
17 #include <xen/hvm/params.h>
18 #include <sys/mman.h>
20 #include "xen-mapcache.h"
21 #include "trace.h"
24 //#define MAPCACHE_DEBUG
26 #ifdef MAPCACHE_DEBUG
27 # define DPRINTF(fmt, ...) do { \
28 fprintf(stderr, "xen_mapcache: " fmt, ## __VA_ARGS__); \
29 } while (0)
30 #else
31 # define DPRINTF(fmt, ...) do { } while (0)
32 #endif
34 #if defined(__i386__)
35 # define MCACHE_BUCKET_SHIFT 16
36 # define MCACHE_MAX_SIZE (1UL<<31) /* 2GB Cap */
37 #elif defined(__x86_64__)
38 # define MCACHE_BUCKET_SHIFT 20
39 # define MCACHE_MAX_SIZE (1UL<<35) /* 32GB Cap */
40 #endif
41 #define MCACHE_BUCKET_SIZE (1UL << MCACHE_BUCKET_SHIFT)
43 #define mapcache_lock() ((void)0)
44 #define mapcache_unlock() ((void)0)
46 typedef struct MapCacheEntry {
47 target_phys_addr_t paddr_index;
48 uint8_t *vaddr_base;
49 unsigned long *valid_mapping;
50 uint8_t lock;
51 target_phys_addr_t size;
52 struct MapCacheEntry *next;
53 } MapCacheEntry;
55 typedef struct MapCacheRev {
56 uint8_t *vaddr_req;
57 target_phys_addr_t paddr_index;
58 target_phys_addr_t size;
59 QTAILQ_ENTRY(MapCacheRev) next;
60 } MapCacheRev;
62 typedef struct MapCache {
63 MapCacheEntry *entry;
64 unsigned long nr_buckets;
65 QTAILQ_HEAD(map_cache_head, MapCacheRev) locked_entries;
67 /* For most cases (>99.9%), the page address is the same. */
68 target_phys_addr_t last_address_index;
69 uint8_t *last_address_vaddr;
70 unsigned long max_mcache_size;
71 unsigned int mcache_bucket_shift;
72 } MapCache;
74 static MapCache *mapcache;
76 static inline int test_bits(int nr, int size, const unsigned long *addr)
78 unsigned long res = find_next_zero_bit(addr, size + nr, nr);
79 if (res >= nr + size)
80 return 1;
81 else
82 return 0;
85 void xen_map_cache_init(void)
87 unsigned long size;
88 struct rlimit rlimit_as;
90 mapcache = g_malloc0(sizeof (MapCache));
92 QTAILQ_INIT(&mapcache->locked_entries);
93 mapcache->last_address_index = -1;
95 getrlimit(RLIMIT_AS, &rlimit_as);
96 if (rlimit_as.rlim_max < MCACHE_MAX_SIZE) {
97 rlimit_as.rlim_cur = rlimit_as.rlim_max;
98 } else {
99 rlimit_as.rlim_cur = MCACHE_MAX_SIZE;
102 setrlimit(RLIMIT_AS, &rlimit_as);
103 mapcache->max_mcache_size = rlimit_as.rlim_cur;
105 mapcache->nr_buckets =
106 (((mapcache->max_mcache_size >> XC_PAGE_SHIFT) +
107 (1UL << (MCACHE_BUCKET_SHIFT - XC_PAGE_SHIFT)) - 1) >>
108 (MCACHE_BUCKET_SHIFT - XC_PAGE_SHIFT));
110 size = mapcache->nr_buckets * sizeof (MapCacheEntry);
111 size = (size + XC_PAGE_SIZE - 1) & ~(XC_PAGE_SIZE - 1);
112 DPRINTF("%s, nr_buckets = %lx size %lu\n", __func__,
113 mapcache->nr_buckets, size);
114 mapcache->entry = g_malloc0(size);
117 static void xen_remap_bucket(MapCacheEntry *entry,
118 target_phys_addr_t size,
119 target_phys_addr_t address_index)
121 uint8_t *vaddr_base;
122 xen_pfn_t *pfns;
123 int *err;
124 unsigned int i;
125 target_phys_addr_t nb_pfn = size >> XC_PAGE_SHIFT;
127 trace_xen_remap_bucket(address_index);
129 pfns = g_malloc0(nb_pfn * sizeof (xen_pfn_t));
130 err = g_malloc0(nb_pfn * sizeof (int));
132 if (entry->vaddr_base != NULL) {
133 if (munmap(entry->vaddr_base, entry->size) != 0) {
134 perror("unmap fails");
135 exit(-1);
138 if (entry->valid_mapping != NULL) {
139 g_free(entry->valid_mapping);
140 entry->valid_mapping = NULL;
143 for (i = 0; i < nb_pfn; i++) {
144 pfns[i] = (address_index << (MCACHE_BUCKET_SHIFT-XC_PAGE_SHIFT)) + i;
147 vaddr_base = xc_map_foreign_bulk(xen_xc, xen_domid, PROT_READ|PROT_WRITE,
148 pfns, err, nb_pfn);
149 if (vaddr_base == NULL) {
150 perror("xc_map_foreign_bulk");
151 exit(-1);
154 entry->vaddr_base = vaddr_base;
155 entry->paddr_index = address_index;
156 entry->size = size;
157 entry->valid_mapping = (unsigned long *) g_malloc0(sizeof(unsigned long) *
158 BITS_TO_LONGS(size >> XC_PAGE_SHIFT));
160 bitmap_zero(entry->valid_mapping, nb_pfn);
161 for (i = 0; i < nb_pfn; i++) {
162 if (!err[i]) {
163 bitmap_set(entry->valid_mapping, i, 1);
167 g_free(pfns);
168 g_free(err);
171 uint8_t *xen_map_cache(target_phys_addr_t phys_addr, target_phys_addr_t size,
172 uint8_t lock)
174 MapCacheEntry *entry, *pentry = NULL;
175 target_phys_addr_t address_index = phys_addr >> MCACHE_BUCKET_SHIFT;
176 target_phys_addr_t address_offset = phys_addr & (MCACHE_BUCKET_SIZE - 1);
177 target_phys_addr_t __size = size;
179 trace_xen_map_cache(phys_addr);
181 if (address_index == mapcache->last_address_index && !lock && !__size) {
182 trace_xen_map_cache_return(mapcache->last_address_vaddr + address_offset);
183 return mapcache->last_address_vaddr + address_offset;
186 /* size is always a multiple of MCACHE_BUCKET_SIZE */
187 if ((address_offset + (__size % MCACHE_BUCKET_SIZE)) > MCACHE_BUCKET_SIZE)
188 __size += MCACHE_BUCKET_SIZE;
189 if (__size % MCACHE_BUCKET_SIZE)
190 __size += MCACHE_BUCKET_SIZE - (__size % MCACHE_BUCKET_SIZE);
191 if (!__size)
192 __size = MCACHE_BUCKET_SIZE;
194 entry = &mapcache->entry[address_index % mapcache->nr_buckets];
196 while (entry && entry->lock && entry->vaddr_base &&
197 (entry->paddr_index != address_index || entry->size != __size ||
198 !test_bits(address_offset >> XC_PAGE_SHIFT, size >> XC_PAGE_SHIFT,
199 entry->valid_mapping))) {
200 pentry = entry;
201 entry = entry->next;
203 if (!entry) {
204 entry = g_malloc0(sizeof (MapCacheEntry));
205 pentry->next = entry;
206 xen_remap_bucket(entry, __size, address_index);
207 } else if (!entry->lock) {
208 if (!entry->vaddr_base || entry->paddr_index != address_index ||
209 entry->size != __size ||
210 !test_bits(address_offset >> XC_PAGE_SHIFT, size >> XC_PAGE_SHIFT,
211 entry->valid_mapping)) {
212 xen_remap_bucket(entry, __size, address_index);
216 if(!test_bits(address_offset >> XC_PAGE_SHIFT, size >> XC_PAGE_SHIFT,
217 entry->valid_mapping)) {
218 mapcache->last_address_index = -1;
219 trace_xen_map_cache_return(NULL);
220 return NULL;
223 mapcache->last_address_index = address_index;
224 mapcache->last_address_vaddr = entry->vaddr_base;
225 if (lock) {
226 MapCacheRev *reventry = g_malloc0(sizeof(MapCacheRev));
227 entry->lock++;
228 reventry->vaddr_req = mapcache->last_address_vaddr + address_offset;
229 reventry->paddr_index = mapcache->last_address_index;
230 reventry->size = entry->size;
231 QTAILQ_INSERT_HEAD(&mapcache->locked_entries, reventry, next);
234 trace_xen_map_cache_return(mapcache->last_address_vaddr + address_offset);
235 return mapcache->last_address_vaddr + address_offset;
238 ram_addr_t xen_ram_addr_from_mapcache(void *ptr)
240 MapCacheEntry *entry = NULL;
241 MapCacheRev *reventry;
242 target_phys_addr_t paddr_index;
243 target_phys_addr_t size;
244 int found = 0;
246 QTAILQ_FOREACH(reventry, &mapcache->locked_entries, next) {
247 if (reventry->vaddr_req == ptr) {
248 paddr_index = reventry->paddr_index;
249 size = reventry->size;
250 found = 1;
251 break;
254 if (!found) {
255 fprintf(stderr, "%s, could not find %p\n", __func__, ptr);
256 QTAILQ_FOREACH(reventry, &mapcache->locked_entries, next) {
257 DPRINTF(" "TARGET_FMT_plx" -> %p is present\n", reventry->paddr_index,
258 reventry->vaddr_req);
260 abort();
261 return 0;
264 entry = &mapcache->entry[paddr_index % mapcache->nr_buckets];
265 while (entry && (entry->paddr_index != paddr_index || entry->size != size)) {
266 entry = entry->next;
268 if (!entry) {
269 DPRINTF("Trying to find address %p that is not in the mapcache!\n", ptr);
270 return 0;
272 return (reventry->paddr_index << MCACHE_BUCKET_SHIFT) +
273 ((unsigned long) ptr - (unsigned long) entry->vaddr_base);
276 void xen_invalidate_map_cache_entry(uint8_t *buffer)
278 MapCacheEntry *entry = NULL, *pentry = NULL;
279 MapCacheRev *reventry;
280 target_phys_addr_t paddr_index;
281 target_phys_addr_t size;
282 int found = 0;
284 if (mapcache->last_address_vaddr == buffer) {
285 mapcache->last_address_index = -1;
288 QTAILQ_FOREACH(reventry, &mapcache->locked_entries, next) {
289 if (reventry->vaddr_req == buffer) {
290 paddr_index = reventry->paddr_index;
291 size = reventry->size;
292 found = 1;
293 break;
296 if (!found) {
297 DPRINTF("%s, could not find %p\n", __func__, buffer);
298 QTAILQ_FOREACH(reventry, &mapcache->locked_entries, next) {
299 DPRINTF(" "TARGET_FMT_plx" -> %p is present\n", reventry->paddr_index, reventry->vaddr_req);
301 return;
303 QTAILQ_REMOVE(&mapcache->locked_entries, reventry, next);
304 g_free(reventry);
306 entry = &mapcache->entry[paddr_index % mapcache->nr_buckets];
307 while (entry && (entry->paddr_index != paddr_index || entry->size != size)) {
308 pentry = entry;
309 entry = entry->next;
311 if (!entry) {
312 DPRINTF("Trying to unmap address %p that is not in the mapcache!\n", buffer);
313 return;
315 entry->lock--;
316 if (entry->lock > 0 || pentry == NULL) {
317 return;
320 pentry->next = entry->next;
321 if (munmap(entry->vaddr_base, entry->size) != 0) {
322 perror("unmap fails");
323 exit(-1);
325 g_free(entry->valid_mapping);
326 g_free(entry);
329 void xen_invalidate_map_cache(void)
331 unsigned long i;
332 MapCacheRev *reventry;
334 /* Flush pending AIO before destroying the mapcache */
335 qemu_aio_flush();
337 QTAILQ_FOREACH(reventry, &mapcache->locked_entries, next) {
338 DPRINTF("There should be no locked mappings at this time, "
339 "but "TARGET_FMT_plx" -> %p is present\n",
340 reventry->paddr_index, reventry->vaddr_req);
343 mapcache_lock();
345 for (i = 0; i < mapcache->nr_buckets; i++) {
346 MapCacheEntry *entry = &mapcache->entry[i];
348 if (entry->vaddr_base == NULL) {
349 continue;
352 if (munmap(entry->vaddr_base, entry->size) != 0) {
353 perror("unmap fails");
354 exit(-1);
357 entry->paddr_index = 0;
358 entry->vaddr_base = NULL;
359 entry->size = 0;
360 g_free(entry->valid_mapping);
361 entry->valid_mapping = NULL;
364 mapcache->last_address_index = -1;
365 mapcache->last_address_vaddr = NULL;
367 mapcache_unlock();