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 * Contributions after 2012-01-13 are licensed under the terms of the
8 * GNU GPL, version 2 or (at your option) any later version.
13 #include <sys/resource.h>
15 #include "hw/xen/xen_backend.h"
16 #include "sysemu/blockdev.h"
17 #include "qemu/bitmap.h"
19 #include <xen/hvm/params.h>
22 #include "sysemu/xen-mapcache.h"
26 //#define MAPCACHE_DEBUG
29 # define DPRINTF(fmt, ...) do { \
30 fprintf(stderr, "xen_mapcache: " fmt, ## __VA_ARGS__); \
33 # define DPRINTF(fmt, ...) do { } while (0)
36 #if HOST_LONG_BITS == 32
37 # define MCACHE_BUCKET_SHIFT 16
38 # define MCACHE_MAX_SIZE (1UL<<31) /* 2GB Cap */
40 # define MCACHE_BUCKET_SHIFT 20
41 # define MCACHE_MAX_SIZE (1UL<<35) /* 32GB Cap */
43 #define MCACHE_BUCKET_SIZE (1UL << MCACHE_BUCKET_SHIFT)
45 /* This is the size of the virtual address space reserve to QEMU that will not
47 * From empirical tests I observed that qemu use 75MB more than the
50 #define NON_MCACHE_MEMORY_SIZE (80 * 1024 * 1024)
52 typedef struct MapCacheEntry
{
55 unsigned long *valid_mapping
;
58 struct MapCacheEntry
*next
;
61 typedef struct MapCacheRev
{
65 QTAILQ_ENTRY(MapCacheRev
) next
;
68 typedef struct MapCache
{
70 unsigned long nr_buckets
;
71 QTAILQ_HEAD(map_cache_head
, MapCacheRev
) locked_entries
;
73 /* For most cases (>99.9%), the page address is the same. */
74 MapCacheEntry
*last_entry
;
75 unsigned long max_mcache_size
;
76 unsigned int mcache_bucket_shift
;
78 phys_offset_to_gaddr_t phys_offset_to_gaddr
;
83 static MapCache
*mapcache
;
85 static inline void mapcache_lock(void)
87 qemu_mutex_lock(&mapcache
->lock
);
90 static inline void mapcache_unlock(void)
92 qemu_mutex_unlock(&mapcache
->lock
);
95 static inline int test_bits(int nr
, int size
, const unsigned long *addr
)
97 unsigned long res
= find_next_zero_bit(addr
, size
+ nr
, nr
);
104 void xen_map_cache_init(phys_offset_to_gaddr_t f
, void *opaque
)
107 struct rlimit rlimit_as
;
109 mapcache
= g_malloc0(sizeof (MapCache
));
111 mapcache
->phys_offset_to_gaddr
= f
;
112 mapcache
->opaque
= opaque
;
113 qemu_mutex_init(&mapcache
->lock
);
115 QTAILQ_INIT(&mapcache
->locked_entries
);
117 if (geteuid() == 0) {
118 rlimit_as
.rlim_cur
= RLIM_INFINITY
;
119 rlimit_as
.rlim_max
= RLIM_INFINITY
;
120 mapcache
->max_mcache_size
= MCACHE_MAX_SIZE
;
122 getrlimit(RLIMIT_AS
, &rlimit_as
);
123 rlimit_as
.rlim_cur
= rlimit_as
.rlim_max
;
125 if (rlimit_as
.rlim_max
!= RLIM_INFINITY
) {
126 fprintf(stderr
, "Warning: QEMU's maximum size of virtual"
127 " memory is not infinity.\n");
129 if (rlimit_as
.rlim_max
< MCACHE_MAX_SIZE
+ NON_MCACHE_MEMORY_SIZE
) {
130 mapcache
->max_mcache_size
= rlimit_as
.rlim_max
-
131 NON_MCACHE_MEMORY_SIZE
;
133 mapcache
->max_mcache_size
= MCACHE_MAX_SIZE
;
137 setrlimit(RLIMIT_AS
, &rlimit_as
);
139 mapcache
->nr_buckets
=
140 (((mapcache
->max_mcache_size
>> XC_PAGE_SHIFT
) +
141 (1UL << (MCACHE_BUCKET_SHIFT
- XC_PAGE_SHIFT
)) - 1) >>
142 (MCACHE_BUCKET_SHIFT
- XC_PAGE_SHIFT
));
144 size
= mapcache
->nr_buckets
* sizeof (MapCacheEntry
);
145 size
= (size
+ XC_PAGE_SIZE
- 1) & ~(XC_PAGE_SIZE
- 1);
146 DPRINTF("%s, nr_buckets = %lx size %lu\n", __func__
,
147 mapcache
->nr_buckets
, size
);
148 mapcache
->entry
= g_malloc0(size
);
151 static void xen_remap_bucket(MapCacheEntry
*entry
,
153 hwaddr address_index
)
159 hwaddr nb_pfn
= size
>> XC_PAGE_SHIFT
;
161 trace_xen_remap_bucket(address_index
);
163 pfns
= g_malloc0(nb_pfn
* sizeof (xen_pfn_t
));
164 err
= g_malloc0(nb_pfn
* sizeof (int));
166 if (entry
->vaddr_base
!= NULL
) {
167 if (munmap(entry
->vaddr_base
, entry
->size
) != 0) {
168 perror("unmap fails");
172 if (entry
->valid_mapping
!= NULL
) {
173 g_free(entry
->valid_mapping
);
174 entry
->valid_mapping
= NULL
;
177 for (i
= 0; i
< nb_pfn
; i
++) {
178 pfns
[i
] = (address_index
<< (MCACHE_BUCKET_SHIFT
-XC_PAGE_SHIFT
)) + i
;
181 vaddr_base
= xc_map_foreign_bulk(xen_xc
, xen_domid
, PROT_READ
|PROT_WRITE
,
183 if (vaddr_base
== NULL
) {
184 perror("xc_map_foreign_bulk");
188 entry
->vaddr_base
= vaddr_base
;
189 entry
->paddr_index
= address_index
;
191 entry
->valid_mapping
= (unsigned long *) g_malloc0(sizeof(unsigned long) *
192 BITS_TO_LONGS(size
>> XC_PAGE_SHIFT
));
194 bitmap_zero(entry
->valid_mapping
, nb_pfn
);
195 for (i
= 0; i
< nb_pfn
; i
++) {
197 bitmap_set(entry
->valid_mapping
, i
, 1);
205 static uint8_t *xen_map_cache_unlocked(hwaddr phys_addr
, hwaddr size
,
208 MapCacheEntry
*entry
, *pentry
= NULL
;
209 hwaddr address_index
;
210 hwaddr address_offset
;
211 hwaddr cache_size
= size
;
212 hwaddr test_bit_size
;
213 bool translated
= false;
216 address_index
= phys_addr
>> MCACHE_BUCKET_SHIFT
;
217 address_offset
= phys_addr
& (MCACHE_BUCKET_SIZE
- 1);
219 trace_xen_map_cache(phys_addr
);
221 /* test_bit_size is always a multiple of XC_PAGE_SIZE */
223 test_bit_size
= size
+ (phys_addr
& (XC_PAGE_SIZE
- 1));
225 if (test_bit_size
% XC_PAGE_SIZE
) {
226 test_bit_size
+= XC_PAGE_SIZE
- (test_bit_size
% XC_PAGE_SIZE
);
229 test_bit_size
= XC_PAGE_SIZE
;
232 if (mapcache
->last_entry
!= NULL
&&
233 mapcache
->last_entry
->paddr_index
== address_index
&&
235 test_bits(address_offset
>> XC_PAGE_SHIFT
,
236 test_bit_size
>> XC_PAGE_SHIFT
,
237 mapcache
->last_entry
->valid_mapping
)) {
238 trace_xen_map_cache_return(mapcache
->last_entry
->vaddr_base
+ address_offset
);
239 return mapcache
->last_entry
->vaddr_base
+ address_offset
;
242 /* size is always a multiple of MCACHE_BUCKET_SIZE */
244 cache_size
= size
+ address_offset
;
245 if (cache_size
% MCACHE_BUCKET_SIZE
) {
246 cache_size
+= MCACHE_BUCKET_SIZE
- (cache_size
% MCACHE_BUCKET_SIZE
);
249 cache_size
= MCACHE_BUCKET_SIZE
;
252 entry
= &mapcache
->entry
[address_index
% mapcache
->nr_buckets
];
254 while (entry
&& entry
->lock
&& entry
->vaddr_base
&&
255 (entry
->paddr_index
!= address_index
|| entry
->size
!= cache_size
||
256 !test_bits(address_offset
>> XC_PAGE_SHIFT
,
257 test_bit_size
>> XC_PAGE_SHIFT
,
258 entry
->valid_mapping
))) {
263 entry
= g_malloc0(sizeof (MapCacheEntry
));
264 pentry
->next
= entry
;
265 xen_remap_bucket(entry
, cache_size
, address_index
);
266 } else if (!entry
->lock
) {
267 if (!entry
->vaddr_base
|| entry
->paddr_index
!= address_index
||
268 entry
->size
!= cache_size
||
269 !test_bits(address_offset
>> XC_PAGE_SHIFT
,
270 test_bit_size
>> XC_PAGE_SHIFT
,
271 entry
->valid_mapping
)) {
272 xen_remap_bucket(entry
, cache_size
, address_index
);
276 if(!test_bits(address_offset
>> XC_PAGE_SHIFT
,
277 test_bit_size
>> XC_PAGE_SHIFT
,
278 entry
->valid_mapping
)) {
279 mapcache
->last_entry
= NULL
;
280 if (!translated
&& mapcache
->phys_offset_to_gaddr
) {
281 phys_addr
= mapcache
->phys_offset_to_gaddr(phys_addr
, size
, mapcache
->opaque
);
285 trace_xen_map_cache_return(NULL
);
289 mapcache
->last_entry
= entry
;
291 MapCacheRev
*reventry
= g_malloc0(sizeof(MapCacheRev
));
293 reventry
->vaddr_req
= mapcache
->last_entry
->vaddr_base
+ address_offset
;
294 reventry
->paddr_index
= mapcache
->last_entry
->paddr_index
;
295 reventry
->size
= entry
->size
;
296 QTAILQ_INSERT_HEAD(&mapcache
->locked_entries
, reventry
, next
);
299 trace_xen_map_cache_return(mapcache
->last_entry
->vaddr_base
+ address_offset
);
300 return mapcache
->last_entry
->vaddr_base
+ address_offset
;
303 uint8_t *xen_map_cache(hwaddr phys_addr
, hwaddr size
,
309 p
= xen_map_cache_unlocked(phys_addr
, size
, lock
);
314 ram_addr_t
xen_ram_addr_from_mapcache(void *ptr
)
316 MapCacheEntry
*entry
= NULL
;
317 MapCacheRev
*reventry
;
324 QTAILQ_FOREACH(reventry
, &mapcache
->locked_entries
, next
) {
325 if (reventry
->vaddr_req
== ptr
) {
326 paddr_index
= reventry
->paddr_index
;
327 size
= reventry
->size
;
333 fprintf(stderr
, "%s, could not find %p\n", __func__
, ptr
);
334 QTAILQ_FOREACH(reventry
, &mapcache
->locked_entries
, next
) {
335 DPRINTF(" "TARGET_FMT_plx
" -> %p is present\n", reventry
->paddr_index
,
336 reventry
->vaddr_req
);
342 entry
= &mapcache
->entry
[paddr_index
% mapcache
->nr_buckets
];
343 while (entry
&& (entry
->paddr_index
!= paddr_index
|| entry
->size
!= size
)) {
347 DPRINTF("Trying to find address %p that is not in the mapcache!\n", ptr
);
350 raddr
= (reventry
->paddr_index
<< MCACHE_BUCKET_SHIFT
) +
351 ((unsigned long) ptr
- (unsigned long) entry
->vaddr_base
);
357 static void xen_invalidate_map_cache_entry_unlocked(uint8_t *buffer
)
359 MapCacheEntry
*entry
= NULL
, *pentry
= NULL
;
360 MapCacheRev
*reventry
;
365 QTAILQ_FOREACH(reventry
, &mapcache
->locked_entries
, next
) {
366 if (reventry
->vaddr_req
== buffer
) {
367 paddr_index
= reventry
->paddr_index
;
368 size
= reventry
->size
;
374 DPRINTF("%s, could not find %p\n", __func__
, buffer
);
375 QTAILQ_FOREACH(reventry
, &mapcache
->locked_entries
, next
) {
376 DPRINTF(" "TARGET_FMT_plx
" -> %p is present\n", reventry
->paddr_index
, reventry
->vaddr_req
);
380 QTAILQ_REMOVE(&mapcache
->locked_entries
, reventry
, next
);
383 if (mapcache
->last_entry
!= NULL
&&
384 mapcache
->last_entry
->paddr_index
== paddr_index
) {
385 mapcache
->last_entry
= NULL
;
388 entry
= &mapcache
->entry
[paddr_index
% mapcache
->nr_buckets
];
389 while (entry
&& (entry
->paddr_index
!= paddr_index
|| entry
->size
!= size
)) {
394 DPRINTF("Trying to unmap address %p that is not in the mapcache!\n", buffer
);
398 if (entry
->lock
> 0 || pentry
== NULL
) {
402 pentry
->next
= entry
->next
;
403 if (munmap(entry
->vaddr_base
, entry
->size
) != 0) {
404 perror("unmap fails");
407 g_free(entry
->valid_mapping
);
411 void xen_invalidate_map_cache_entry(uint8_t *buffer
)
414 xen_invalidate_map_cache_entry_unlocked(buffer
);
418 void xen_invalidate_map_cache(void)
421 MapCacheRev
*reventry
;
423 /* Flush pending AIO before destroying the mapcache */
428 QTAILQ_FOREACH(reventry
, &mapcache
->locked_entries
, next
) {
429 DPRINTF("There should be no locked mappings at this time, "
430 "but "TARGET_FMT_plx
" -> %p is present\n",
431 reventry
->paddr_index
, reventry
->vaddr_req
);
434 for (i
= 0; i
< mapcache
->nr_buckets
; i
++) {
435 MapCacheEntry
*entry
= &mapcache
->entry
[i
];
437 if (entry
->vaddr_base
== NULL
) {
440 if (entry
->lock
> 0) {
444 if (munmap(entry
->vaddr_base
, entry
->size
) != 0) {
445 perror("unmap fails");
449 entry
->paddr_index
= 0;
450 entry
->vaddr_base
= NULL
;
452 g_free(entry
->valid_mapping
);
453 entry
->valid_mapping
= NULL
;
456 mapcache
->last_entry
= NULL
;