blob: 2ca18ce3b56fc388c93259b1dce7e9bf2e238504 [file] [log] [blame]
Jun Nakajima432d2682010-08-31 16:41:25 +01001/*
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 *
7 */
8
9#include "config.h"
10
11#include <sys/resource.h>
12
13#include "hw/xen_backend.h"
14#include "blockdev.h"
John Babovalea6c5f82011-03-22 14:50:28 +000015#include "bitmap.h"
Jun Nakajima432d2682010-08-31 16:41:25 +010016
17#include <xen/hvm/params.h>
18#include <sys/mman.h>
19
20#include "xen-mapcache.h"
21#include "trace.h"
22
23
24//#define MAPCACHE_DEBUG
25
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
33
34#if defined(__i386__)
35# define MCACHE_BUCKET_SHIFT 16
John Babovalea6c5f82011-03-22 14:50:28 +000036# define MCACHE_MAX_SIZE (1UL<<31) /* 2GB Cap */
Jun Nakajima432d2682010-08-31 16:41:25 +010037#elif defined(__x86_64__)
38# define MCACHE_BUCKET_SHIFT 20
John Babovalea6c5f82011-03-22 14:50:28 +000039# define MCACHE_MAX_SIZE (1UL<<35) /* 32GB Cap */
Jun Nakajima432d2682010-08-31 16:41:25 +010040#endif
41#define MCACHE_BUCKET_SIZE (1UL << MCACHE_BUCKET_SHIFT)
42
Jun Nakajima432d2682010-08-31 16:41:25 +010043typedef struct MapCacheEntry {
44 target_phys_addr_t paddr_index;
45 uint8_t *vaddr_base;
46 DECLARE_BITMAP(valid_mapping, MCACHE_BUCKET_SIZE >> XC_PAGE_SHIFT);
47 uint8_t lock;
48 struct MapCacheEntry *next;
49} MapCacheEntry;
50
51typedef struct MapCacheRev {
52 uint8_t *vaddr_req;
53 target_phys_addr_t paddr_index;
54 QTAILQ_ENTRY(MapCacheRev) next;
55} MapCacheRev;
56
57typedef struct MapCache {
58 MapCacheEntry *entry;
59 unsigned long nr_buckets;
60 QTAILQ_HEAD(map_cache_head, MapCacheRev) locked_entries;
61
62 /* For most cases (>99.9%), the page address is the same. */
63 target_phys_addr_t last_address_index;
64 uint8_t *last_address_vaddr;
65 unsigned long max_mcache_size;
66 unsigned int mcache_bucket_shift;
67} MapCache;
68
69static MapCache *mapcache;
70
Jun Nakajima432d2682010-08-31 16:41:25 +010071void qemu_map_cache_init(void)
72{
73 unsigned long size;
74 struct rlimit rlimit_as;
75
76 mapcache = qemu_mallocz(sizeof (MapCache));
77
78 QTAILQ_INIT(&mapcache->locked_entries);
79 mapcache->last_address_index = -1;
80
81 getrlimit(RLIMIT_AS, &rlimit_as);
John Babovalea6c5f82011-03-22 14:50:28 +000082 if (rlimit_as.rlim_max < MCACHE_MAX_SIZE) {
83 rlimit_as.rlim_cur = rlimit_as.rlim_max;
84 } else {
85 rlimit_as.rlim_cur = MCACHE_MAX_SIZE;
86 }
87
Jun Nakajima432d2682010-08-31 16:41:25 +010088 setrlimit(RLIMIT_AS, &rlimit_as);
John Babovalea6c5f82011-03-22 14:50:28 +000089 mapcache->max_mcache_size = rlimit_as.rlim_cur;
Jun Nakajima432d2682010-08-31 16:41:25 +010090
91 mapcache->nr_buckets =
92 (((mapcache->max_mcache_size >> XC_PAGE_SHIFT) +
93 (1UL << (MCACHE_BUCKET_SHIFT - XC_PAGE_SHIFT)) - 1) >>
94 (MCACHE_BUCKET_SHIFT - XC_PAGE_SHIFT));
95
96 size = mapcache->nr_buckets * sizeof (MapCacheEntry);
97 size = (size + XC_PAGE_SIZE - 1) & ~(XC_PAGE_SIZE - 1);
98 DPRINTF("qemu_map_cache_init, nr_buckets = %lx size %lu\n", mapcache->nr_buckets, size);
99 mapcache->entry = qemu_mallocz(size);
100}
101
102static void qemu_remap_bucket(MapCacheEntry *entry,
103 target_phys_addr_t size,
104 target_phys_addr_t address_index)
105{
106 uint8_t *vaddr_base;
107 xen_pfn_t *pfns;
108 int *err;
John Babovalea6c5f82011-03-22 14:50:28 +0000109 unsigned int i;
Jun Nakajima432d2682010-08-31 16:41:25 +0100110 target_phys_addr_t nb_pfn = size >> XC_PAGE_SHIFT;
111
112 trace_qemu_remap_bucket(address_index);
113
114 pfns = qemu_mallocz(nb_pfn * sizeof (xen_pfn_t));
115 err = qemu_mallocz(nb_pfn * sizeof (int));
116
117 if (entry->vaddr_base != NULL) {
118 if (munmap(entry->vaddr_base, size) != 0) {
119 perror("unmap fails");
120 exit(-1);
121 }
122 }
123
124 for (i = 0; i < nb_pfn; i++) {
125 pfns[i] = (address_index << (MCACHE_BUCKET_SHIFT-XC_PAGE_SHIFT)) + i;
126 }
127
128 vaddr_base = xc_map_foreign_bulk(xen_xc, xen_domid, PROT_READ|PROT_WRITE,
129 pfns, err, nb_pfn);
130 if (vaddr_base == NULL) {
131 perror("xc_map_foreign_bulk");
132 exit(-1);
133 }
134
135 entry->vaddr_base = vaddr_base;
136 entry->paddr_index = address_index;
137
John Babovalea6c5f82011-03-22 14:50:28 +0000138 bitmap_zero(entry->valid_mapping, nb_pfn);
139 for (i = 0; i < nb_pfn; i++) {
140 if (!err[i]) {
141 bitmap_set(entry->valid_mapping, i, 1);
Jun Nakajima432d2682010-08-31 16:41:25 +0100142 }
Jun Nakajima432d2682010-08-31 16:41:25 +0100143 }
144
145 qemu_free(pfns);
146 qemu_free(err);
147}
148
149uint8_t *qemu_map_cache(target_phys_addr_t phys_addr, target_phys_addr_t size, uint8_t lock)
150{
151 MapCacheEntry *entry, *pentry = NULL;
152 target_phys_addr_t address_index = phys_addr >> MCACHE_BUCKET_SHIFT;
153 target_phys_addr_t address_offset = phys_addr & (MCACHE_BUCKET_SIZE - 1);
154
155 trace_qemu_map_cache(phys_addr);
156
157 if (address_index == mapcache->last_address_index && !lock) {
158 trace_qemu_map_cache_return(mapcache->last_address_vaddr + address_offset);
159 return mapcache->last_address_vaddr + address_offset;
160 }
161
162 entry = &mapcache->entry[address_index % mapcache->nr_buckets];
163
164 while (entry && entry->lock && entry->paddr_index != address_index && entry->vaddr_base) {
165 pentry = entry;
166 entry = entry->next;
167 }
168 if (!entry) {
169 entry = qemu_mallocz(sizeof (MapCacheEntry));
170 pentry->next = entry;
171 qemu_remap_bucket(entry, size ? : MCACHE_BUCKET_SIZE, address_index);
172 } else if (!entry->lock) {
173 if (!entry->vaddr_base || entry->paddr_index != address_index ||
174 !test_bit(address_offset >> XC_PAGE_SHIFT, entry->valid_mapping)) {
175 qemu_remap_bucket(entry, size ? : MCACHE_BUCKET_SIZE, address_index);
176 }
177 }
178
179 if (!test_bit(address_offset >> XC_PAGE_SHIFT, entry->valid_mapping)) {
180 mapcache->last_address_index = -1;
181 trace_qemu_map_cache_return(NULL);
182 return NULL;
183 }
184
185 mapcache->last_address_index = address_index;
186 mapcache->last_address_vaddr = entry->vaddr_base;
187 if (lock) {
188 MapCacheRev *reventry = qemu_mallocz(sizeof(MapCacheRev));
189 entry->lock++;
190 reventry->vaddr_req = mapcache->last_address_vaddr + address_offset;
191 reventry->paddr_index = mapcache->last_address_index;
192 QTAILQ_INSERT_HEAD(&mapcache->locked_entries, reventry, next);
193 }
194
195 trace_qemu_map_cache_return(mapcache->last_address_vaddr + address_offset);
196 return mapcache->last_address_vaddr + address_offset;
197}
198
199ram_addr_t qemu_ram_addr_from_mapcache(void *ptr)
200{
201 MapCacheRev *reventry;
202 target_phys_addr_t paddr_index;
203 int found = 0;
204
205 QTAILQ_FOREACH(reventry, &mapcache->locked_entries, next) {
206 if (reventry->vaddr_req == ptr) {
207 paddr_index = reventry->paddr_index;
208 found = 1;
209 break;
210 }
211 }
212 if (!found) {
213 fprintf(stderr, "qemu_ram_addr_from_mapcache, could not find %p\n", ptr);
214 QTAILQ_FOREACH(reventry, &mapcache->locked_entries, next) {
215 DPRINTF(" "TARGET_FMT_plx" -> %p is present\n", reventry->paddr_index,
216 reventry->vaddr_req);
217 }
218 abort();
219 return 0;
220 }
221
222 return paddr_index << MCACHE_BUCKET_SHIFT;
223}
224
225void qemu_invalidate_entry(uint8_t *buffer)
226{
227 MapCacheEntry *entry = NULL, *pentry = NULL;
228 MapCacheRev *reventry;
229 target_phys_addr_t paddr_index;
230 int found = 0;
231
232 if (mapcache->last_address_vaddr == buffer) {
233 mapcache->last_address_index = -1;
234 }
235
236 QTAILQ_FOREACH(reventry, &mapcache->locked_entries, next) {
237 if (reventry->vaddr_req == buffer) {
238 paddr_index = reventry->paddr_index;
239 found = 1;
240 break;
241 }
242 }
243 if (!found) {
244 DPRINTF("qemu_invalidate_entry, could not find %p\n", buffer);
245 QTAILQ_FOREACH(reventry, &mapcache->locked_entries, next) {
246 DPRINTF(" "TARGET_FMT_plx" -> %p is present\n", reventry->paddr_index, reventry->vaddr_req);
247 }
248 return;
249 }
250 QTAILQ_REMOVE(&mapcache->locked_entries, reventry, next);
251 qemu_free(reventry);
252
253 entry = &mapcache->entry[paddr_index % mapcache->nr_buckets];
254 while (entry && entry->paddr_index != paddr_index) {
255 pentry = entry;
256 entry = entry->next;
257 }
258 if (!entry) {
259 DPRINTF("Trying to unmap address %p that is not in the mapcache!\n", buffer);
260 return;
261 }
262 entry->lock--;
263 if (entry->lock > 0 || pentry == NULL) {
264 return;
265 }
266
267 pentry->next = entry->next;
268 if (munmap(entry->vaddr_base, MCACHE_BUCKET_SIZE) != 0) {
269 perror("unmap fails");
270 exit(-1);
271 }
272 qemu_free(entry);
273}
274
275void qemu_invalidate_map_cache(void)
276{
277 unsigned long i;
278 MapCacheRev *reventry;
279
280 /* Flush pending AIO before destroying the mapcache */
281 qemu_aio_flush();
282
283 QTAILQ_FOREACH(reventry, &mapcache->locked_entries, next) {
284 DPRINTF("There should be no locked mappings at this time, "
285 "but "TARGET_FMT_plx" -> %p is present\n",
286 reventry->paddr_index, reventry->vaddr_req);
287 }
288
289 mapcache_lock();
290
291 for (i = 0; i < mapcache->nr_buckets; i++) {
292 MapCacheEntry *entry = &mapcache->entry[i];
293
294 if (entry->vaddr_base == NULL) {
295 continue;
296 }
297
298 if (munmap(entry->vaddr_base, MCACHE_BUCKET_SIZE) != 0) {
299 perror("unmap fails");
300 exit(-1);
301 }
302
303 entry->paddr_index = 0;
304 entry->vaddr_base = NULL;
305 }
306
307 mapcache->last_address_index = -1;
308 mapcache->last_address_vaddr = NULL;
309
310 mapcache_unlock();
311}
312
313uint8_t *xen_map_block(target_phys_addr_t phys_addr, target_phys_addr_t size)
314{
315 uint8_t *vaddr_base;
316 xen_pfn_t *pfns;
317 int *err;
318 unsigned int i;
319 target_phys_addr_t nb_pfn = size >> XC_PAGE_SHIFT;
320
321 trace_xen_map_block(phys_addr, size);
322 phys_addr >>= XC_PAGE_SHIFT;
323
324 pfns = qemu_mallocz(nb_pfn * sizeof (xen_pfn_t));
325 err = qemu_mallocz(nb_pfn * sizeof (int));
326
327 for (i = 0; i < nb_pfn; i++) {
328 pfns[i] = phys_addr + i;
329 }
330
331 vaddr_base = xc_map_foreign_bulk(xen_xc, xen_domid, PROT_READ|PROT_WRITE,
332 pfns, err, nb_pfn);
333 if (vaddr_base == NULL) {
334 perror("xc_map_foreign_bulk");
335 exit(-1);
336 }
337
338 qemu_free(pfns);
339 qemu_free(err);
340
341 return vaddr_base;
342}