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.
11 #include <sys/resource.h>
13 #include "hw/xen_backend.h"
17 #include <xen/hvm/params.h>
20 #include "xen-mapcache.h"
24 //#define MAPCACHE_DEBUG
27 # define DPRINTF(fmt, ...) do { \
28 fprintf(stderr, "xen_mapcache: " fmt, ## __VA_ARGS__); \
31 # define DPRINTF(fmt, ...) do { } while (0)
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 */
41 #define MCACHE_BUCKET_SIZE (1UL << MCACHE_BUCKET_SHIFT)
43 /* This is the size of the virtual address space reserve to QEMU that will not
45 * From empirical tests I observed that qemu use 75MB more than the
48 #define NON_MCACHE_MEMORY_SIZE (80 * 1024 * 1024)
50 #define mapcache_lock() ((void)0)
51 #define mapcache_unlock() ((void)0)
53 typedef struct MapCacheEntry
{
54 target_phys_addr_t paddr_index
;
56 unsigned long *valid_mapping
;
58 target_phys_addr_t size
;
59 struct MapCacheEntry
*next
;
62 typedef struct MapCacheRev
{
64 target_phys_addr_t paddr_index
;
65 target_phys_addr_t size
;
66 QTAILQ_ENTRY(MapCacheRev
) next
;
69 typedef struct MapCache
{
71 unsigned long nr_buckets
;
72 QTAILQ_HEAD(map_cache_head
, MapCacheRev
) locked_entries
;
74 /* For most cases (>99.9%), the page address is the same. */
75 target_phys_addr_t last_address_index
;
76 uint8_t *last_address_vaddr
;
77 unsigned long max_mcache_size
;
78 unsigned int mcache_bucket_shift
;
81 static MapCache
*mapcache
;
83 static inline int test_bits(int nr
, int size
, const unsigned long *addr
)
85 unsigned long res
= find_next_zero_bit(addr
, size
+ nr
, nr
);
92 void xen_map_cache_init(void)
95 struct rlimit rlimit_as
;
97 mapcache
= g_malloc0(sizeof (MapCache
));
99 QTAILQ_INIT(&mapcache
->locked_entries
);
100 mapcache
->last_address_index
= -1;
102 if (geteuid() == 0) {
103 rlimit_as
.rlim_cur
= RLIM_INFINITY
;
104 rlimit_as
.rlim_max
= RLIM_INFINITY
;
105 mapcache
->max_mcache_size
= MCACHE_MAX_SIZE
;
107 getrlimit(RLIMIT_AS
, &rlimit_as
);
108 rlimit_as
.rlim_cur
= rlimit_as
.rlim_max
;
110 if (rlimit_as
.rlim_max
!= RLIM_INFINITY
) {
111 fprintf(stderr
, "Warning: QEMU's maximum size of virtual"
112 " memory is not infinity.\n");
114 if (rlimit_as
.rlim_max
< MCACHE_MAX_SIZE
+ NON_MCACHE_MEMORY_SIZE
) {
115 mapcache
->max_mcache_size
= rlimit_as
.rlim_max
-
116 NON_MCACHE_MEMORY_SIZE
;
118 mapcache
->max_mcache_size
= MCACHE_MAX_SIZE
;
122 setrlimit(RLIMIT_AS
, &rlimit_as
);
124 mapcache
->nr_buckets
=
125 (((mapcache
->max_mcache_size
>> XC_PAGE_SHIFT
) +
126 (1UL << (MCACHE_BUCKET_SHIFT
- XC_PAGE_SHIFT
)) - 1) >>
127 (MCACHE_BUCKET_SHIFT
- XC_PAGE_SHIFT
));
129 size
= mapcache
->nr_buckets
* sizeof (MapCacheEntry
);
130 size
= (size
+ XC_PAGE_SIZE
- 1) & ~(XC_PAGE_SIZE
- 1);
131 DPRINTF("%s, nr_buckets = %lx size %lu\n", __func__
,
132 mapcache
->nr_buckets
, size
);
133 mapcache
->entry
= g_malloc0(size
);
136 static void xen_remap_bucket(MapCacheEntry
*entry
,
137 target_phys_addr_t size
,
138 target_phys_addr_t address_index
)
144 target_phys_addr_t nb_pfn
= size
>> XC_PAGE_SHIFT
;
146 trace_xen_remap_bucket(address_index
);
148 pfns
= g_malloc0(nb_pfn
* sizeof (xen_pfn_t
));
149 err
= g_malloc0(nb_pfn
* sizeof (int));
151 if (entry
->vaddr_base
!= NULL
) {
152 if (munmap(entry
->vaddr_base
, entry
->size
) != 0) {
153 perror("unmap fails");
157 if (entry
->valid_mapping
!= NULL
) {
158 g_free(entry
->valid_mapping
);
159 entry
->valid_mapping
= NULL
;
162 for (i
= 0; i
< nb_pfn
; i
++) {
163 pfns
[i
] = (address_index
<< (MCACHE_BUCKET_SHIFT
-XC_PAGE_SHIFT
)) + i
;
166 vaddr_base
= xc_map_foreign_bulk(xen_xc
, xen_domid
, PROT_READ
|PROT_WRITE
,
168 if (vaddr_base
== NULL
) {
169 perror("xc_map_foreign_bulk");
173 entry
->vaddr_base
= vaddr_base
;
174 entry
->paddr_index
= address_index
;
176 entry
->valid_mapping
= (unsigned long *) g_malloc0(sizeof(unsigned long) *
177 BITS_TO_LONGS(size
>> XC_PAGE_SHIFT
));
179 bitmap_zero(entry
->valid_mapping
, nb_pfn
);
180 for (i
= 0; i
< nb_pfn
; i
++) {
182 bitmap_set(entry
->valid_mapping
, i
, 1);
190 uint8_t *xen_map_cache(target_phys_addr_t phys_addr
, target_phys_addr_t size
,
193 MapCacheEntry
*entry
, *pentry
= NULL
;
194 target_phys_addr_t address_index
= phys_addr
>> MCACHE_BUCKET_SHIFT
;
195 target_phys_addr_t address_offset
= phys_addr
& (MCACHE_BUCKET_SIZE
- 1);
196 target_phys_addr_t __size
= size
;
198 trace_xen_map_cache(phys_addr
);
200 if (address_index
== mapcache
->last_address_index
&& !lock
&& !__size
) {
201 trace_xen_map_cache_return(mapcache
->last_address_vaddr
+ address_offset
);
202 return mapcache
->last_address_vaddr
+ address_offset
;
205 /* size is always a multiple of MCACHE_BUCKET_SIZE */
206 if ((address_offset
+ (__size
% MCACHE_BUCKET_SIZE
)) > MCACHE_BUCKET_SIZE
)
207 __size
+= MCACHE_BUCKET_SIZE
;
208 if (__size
% MCACHE_BUCKET_SIZE
)
209 __size
+= MCACHE_BUCKET_SIZE
- (__size
% MCACHE_BUCKET_SIZE
);
211 __size
= MCACHE_BUCKET_SIZE
;
213 entry
= &mapcache
->entry
[address_index
% mapcache
->nr_buckets
];
215 while (entry
&& entry
->lock
&& entry
->vaddr_base
&&
216 (entry
->paddr_index
!= address_index
|| entry
->size
!= __size
||
217 !test_bits(address_offset
>> XC_PAGE_SHIFT
, size
>> XC_PAGE_SHIFT
,
218 entry
->valid_mapping
))) {
223 entry
= g_malloc0(sizeof (MapCacheEntry
));
224 pentry
->next
= entry
;
225 xen_remap_bucket(entry
, __size
, address_index
);
226 } else if (!entry
->lock
) {
227 if (!entry
->vaddr_base
|| entry
->paddr_index
!= address_index
||
228 entry
->size
!= __size
||
229 !test_bits(address_offset
>> XC_PAGE_SHIFT
, size
>> XC_PAGE_SHIFT
,
230 entry
->valid_mapping
)) {
231 xen_remap_bucket(entry
, __size
, address_index
);
235 if(!test_bits(address_offset
>> XC_PAGE_SHIFT
, size
>> XC_PAGE_SHIFT
,
236 entry
->valid_mapping
)) {
237 mapcache
->last_address_index
= -1;
238 trace_xen_map_cache_return(NULL
);
242 mapcache
->last_address_index
= address_index
;
243 mapcache
->last_address_vaddr
= entry
->vaddr_base
;
245 MapCacheRev
*reventry
= g_malloc0(sizeof(MapCacheRev
));
247 reventry
->vaddr_req
= mapcache
->last_address_vaddr
+ address_offset
;
248 reventry
->paddr_index
= mapcache
->last_address_index
;
249 reventry
->size
= entry
->size
;
250 QTAILQ_INSERT_HEAD(&mapcache
->locked_entries
, reventry
, next
);
253 trace_xen_map_cache_return(mapcache
->last_address_vaddr
+ address_offset
);
254 return mapcache
->last_address_vaddr
+ address_offset
;
257 ram_addr_t
xen_ram_addr_from_mapcache(void *ptr
)
259 MapCacheEntry
*entry
= NULL
;
260 MapCacheRev
*reventry
;
261 target_phys_addr_t paddr_index
;
262 target_phys_addr_t size
;
265 QTAILQ_FOREACH(reventry
, &mapcache
->locked_entries
, next
) {
266 if (reventry
->vaddr_req
== ptr
) {
267 paddr_index
= reventry
->paddr_index
;
268 size
= reventry
->size
;
274 fprintf(stderr
, "%s, could not find %p\n", __func__
, ptr
);
275 QTAILQ_FOREACH(reventry
, &mapcache
->locked_entries
, next
) {
276 DPRINTF(" "TARGET_FMT_plx
" -> %p is present\n", reventry
->paddr_index
,
277 reventry
->vaddr_req
);
283 entry
= &mapcache
->entry
[paddr_index
% mapcache
->nr_buckets
];
284 while (entry
&& (entry
->paddr_index
!= paddr_index
|| entry
->size
!= size
)) {
288 DPRINTF("Trying to find address %p that is not in the mapcache!\n", ptr
);
291 return (reventry
->paddr_index
<< MCACHE_BUCKET_SHIFT
) +
292 ((unsigned long) ptr
- (unsigned long) entry
->vaddr_base
);
295 void xen_invalidate_map_cache_entry(uint8_t *buffer
)
297 MapCacheEntry
*entry
= NULL
, *pentry
= NULL
;
298 MapCacheRev
*reventry
;
299 target_phys_addr_t paddr_index
;
300 target_phys_addr_t size
;
303 if (mapcache
->last_address_vaddr
== buffer
) {
304 mapcache
->last_address_index
= -1;
307 QTAILQ_FOREACH(reventry
, &mapcache
->locked_entries
, next
) {
308 if (reventry
->vaddr_req
== buffer
) {
309 paddr_index
= reventry
->paddr_index
;
310 size
= reventry
->size
;
316 DPRINTF("%s, could not find %p\n", __func__
, buffer
);
317 QTAILQ_FOREACH(reventry
, &mapcache
->locked_entries
, next
) {
318 DPRINTF(" "TARGET_FMT_plx
" -> %p is present\n", reventry
->paddr_index
, reventry
->vaddr_req
);
322 QTAILQ_REMOVE(&mapcache
->locked_entries
, reventry
, next
);
325 entry
= &mapcache
->entry
[paddr_index
% mapcache
->nr_buckets
];
326 while (entry
&& (entry
->paddr_index
!= paddr_index
|| entry
->size
!= size
)) {
331 DPRINTF("Trying to unmap address %p that is not in the mapcache!\n", buffer
);
335 if (entry
->lock
> 0 || pentry
== NULL
) {
339 pentry
->next
= entry
->next
;
340 if (munmap(entry
->vaddr_base
, entry
->size
) != 0) {
341 perror("unmap fails");
344 g_free(entry
->valid_mapping
);
348 void xen_invalidate_map_cache(void)
351 MapCacheRev
*reventry
;
353 /* Flush pending AIO before destroying the mapcache */
356 QTAILQ_FOREACH(reventry
, &mapcache
->locked_entries
, next
) {
357 DPRINTF("There should be no locked mappings at this time, "
358 "but "TARGET_FMT_plx
" -> %p is present\n",
359 reventry
->paddr_index
, reventry
->vaddr_req
);
364 for (i
= 0; i
< mapcache
->nr_buckets
; i
++) {
365 MapCacheEntry
*entry
= &mapcache
->entry
[i
];
367 if (entry
->vaddr_base
== NULL
) {
371 if (munmap(entry
->vaddr_base
, entry
->size
) != 0) {
372 perror("unmap fails");
376 entry
->paddr_index
= 0;
377 entry
->vaddr_base
= NULL
;
379 g_free(entry
->valid_mapping
);
380 entry
->valid_mapping
= NULL
;
383 mapcache
->last_address_index
= -1;
384 mapcache
->last_address_vaddr
= NULL
;