xen-mapcache.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  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. *
  7. */
  8. #include "config.h"
  9. #include <sys/resource.h>
  10. #include "hw/xen_backend.h"
  11. #include "blockdev.h"
  12. #include "bitmap.h"
  13. #include <xen/hvm/params.h>
  14. #include <sys/mman.h>
  15. #include "xen-mapcache.h"
  16. #include "trace.h"
  17. //#define MAPCACHE_DEBUG
  18. #ifdef MAPCACHE_DEBUG
  19. # define DPRINTF(fmt, ...) do { \
  20. fprintf(stderr, "xen_mapcache: " fmt, ## __VA_ARGS__); \
  21. } while (0)
  22. #else
  23. # define DPRINTF(fmt, ...) do { } while (0)
  24. #endif
  25. #if defined(__i386__)
  26. # define MCACHE_BUCKET_SHIFT 16
  27. # define MCACHE_MAX_SIZE (1UL<<31) /* 2GB Cap */
  28. #elif defined(__x86_64__)
  29. # define MCACHE_BUCKET_SHIFT 20
  30. # define MCACHE_MAX_SIZE (1UL<<35) /* 32GB Cap */
  31. #endif
  32. #define MCACHE_BUCKET_SIZE (1UL << MCACHE_BUCKET_SHIFT)
  33. #define mapcache_lock() ((void)0)
  34. #define mapcache_unlock() ((void)0)
  35. typedef struct MapCacheEntry {
  36. target_phys_addr_t paddr_index;
  37. uint8_t *vaddr_base;
  38. unsigned long *valid_mapping;
  39. uint8_t lock;
  40. target_phys_addr_t size;
  41. struct MapCacheEntry *next;
  42. } MapCacheEntry;
  43. typedef struct MapCacheRev {
  44. uint8_t *vaddr_req;
  45. target_phys_addr_t paddr_index;
  46. target_phys_addr_t size;
  47. QTAILQ_ENTRY(MapCacheRev) next;
  48. } MapCacheRev;
  49. typedef struct MapCache {
  50. MapCacheEntry *entry;
  51. unsigned long nr_buckets;
  52. QTAILQ_HEAD(map_cache_head, MapCacheRev) locked_entries;
  53. /* For most cases (>99.9%), the page address is the same. */
  54. target_phys_addr_t last_address_index;
  55. uint8_t *last_address_vaddr;
  56. unsigned long max_mcache_size;
  57. unsigned int mcache_bucket_shift;
  58. } MapCache;
  59. static MapCache *mapcache;
  60. static inline int test_bits(int nr, int size, const unsigned long *addr)
  61. {
  62. unsigned long res = find_next_zero_bit(addr, size + nr, nr);
  63. if (res >= nr + size)
  64. return 1;
  65. else
  66. return 0;
  67. }
  68. void xen_map_cache_init(void)
  69. {
  70. unsigned long size;
  71. struct rlimit rlimit_as;
  72. mapcache = qemu_mallocz(sizeof (MapCache));
  73. QTAILQ_INIT(&mapcache->locked_entries);
  74. mapcache->last_address_index = -1;
  75. getrlimit(RLIMIT_AS, &rlimit_as);
  76. if (rlimit_as.rlim_max < MCACHE_MAX_SIZE) {
  77. rlimit_as.rlim_cur = rlimit_as.rlim_max;
  78. } else {
  79. rlimit_as.rlim_cur = MCACHE_MAX_SIZE;
  80. }
  81. setrlimit(RLIMIT_AS, &rlimit_as);
  82. mapcache->max_mcache_size = rlimit_as.rlim_cur;
  83. mapcache->nr_buckets =
  84. (((mapcache->max_mcache_size >> XC_PAGE_SHIFT) +
  85. (1UL << (MCACHE_BUCKET_SHIFT - XC_PAGE_SHIFT)) - 1) >>
  86. (MCACHE_BUCKET_SHIFT - XC_PAGE_SHIFT));
  87. size = mapcache->nr_buckets * sizeof (MapCacheEntry);
  88. size = (size + XC_PAGE_SIZE - 1) & ~(XC_PAGE_SIZE - 1);
  89. DPRINTF("%s, nr_buckets = %lx size %lu\n", __func__,
  90. mapcache->nr_buckets, size);
  91. mapcache->entry = qemu_mallocz(size);
  92. }
  93. static void xen_remap_bucket(MapCacheEntry *entry,
  94. target_phys_addr_t size,
  95. target_phys_addr_t address_index)
  96. {
  97. uint8_t *vaddr_base;
  98. xen_pfn_t *pfns;
  99. int *err;
  100. unsigned int i;
  101. target_phys_addr_t nb_pfn = size >> XC_PAGE_SHIFT;
  102. trace_xen_remap_bucket(address_index);
  103. pfns = qemu_mallocz(nb_pfn * sizeof (xen_pfn_t));
  104. err = qemu_mallocz(nb_pfn * sizeof (int));
  105. if (entry->vaddr_base != NULL) {
  106. if (munmap(entry->vaddr_base, entry->size) != 0) {
  107. perror("unmap fails");
  108. exit(-1);
  109. }
  110. }
  111. if (entry->valid_mapping != NULL) {
  112. qemu_free(entry->valid_mapping);
  113. entry->valid_mapping = NULL;
  114. }
  115. for (i = 0; i < nb_pfn; i++) {
  116. pfns[i] = (address_index << (MCACHE_BUCKET_SHIFT-XC_PAGE_SHIFT)) + i;
  117. }
  118. vaddr_base = xc_map_foreign_bulk(xen_xc, xen_domid, PROT_READ|PROT_WRITE,
  119. pfns, err, nb_pfn);
  120. if (vaddr_base == NULL) {
  121. perror("xc_map_foreign_bulk");
  122. exit(-1);
  123. }
  124. entry->vaddr_base = vaddr_base;
  125. entry->paddr_index = address_index;
  126. entry->size = size;
  127. entry->valid_mapping = (unsigned long *) qemu_mallocz(sizeof(unsigned long) *
  128. BITS_TO_LONGS(size >> XC_PAGE_SHIFT));
  129. bitmap_zero(entry->valid_mapping, nb_pfn);
  130. for (i = 0; i < nb_pfn; i++) {
  131. if (!err[i]) {
  132. bitmap_set(entry->valid_mapping, i, 1);
  133. }
  134. }
  135. qemu_free(pfns);
  136. qemu_free(err);
  137. }
  138. uint8_t *xen_map_cache(target_phys_addr_t phys_addr, target_phys_addr_t size,
  139. uint8_t lock)
  140. {
  141. MapCacheEntry *entry, *pentry = NULL;
  142. target_phys_addr_t address_index = phys_addr >> MCACHE_BUCKET_SHIFT;
  143. target_phys_addr_t address_offset = phys_addr & (MCACHE_BUCKET_SIZE - 1);
  144. target_phys_addr_t __size = size;
  145. trace_xen_map_cache(phys_addr);
  146. if (address_index == mapcache->last_address_index && !lock && !__size) {
  147. trace_xen_map_cache_return(mapcache->last_address_vaddr + address_offset);
  148. return mapcache->last_address_vaddr + address_offset;
  149. }
  150. /* size is always a multiple of MCACHE_BUCKET_SIZE */
  151. if ((address_offset + (__size % MCACHE_BUCKET_SIZE)) > MCACHE_BUCKET_SIZE)
  152. __size += MCACHE_BUCKET_SIZE;
  153. if (__size % MCACHE_BUCKET_SIZE)
  154. __size += MCACHE_BUCKET_SIZE - (__size % MCACHE_BUCKET_SIZE);
  155. if (!__size)
  156. __size = MCACHE_BUCKET_SIZE;
  157. entry = &mapcache->entry[address_index % mapcache->nr_buckets];
  158. while (entry && entry->lock && entry->vaddr_base &&
  159. (entry->paddr_index != address_index || entry->size != __size ||
  160. !test_bits(address_offset >> XC_PAGE_SHIFT, size >> XC_PAGE_SHIFT,
  161. entry->valid_mapping))) {
  162. pentry = entry;
  163. entry = entry->next;
  164. }
  165. if (!entry) {
  166. entry = qemu_mallocz(sizeof (MapCacheEntry));
  167. pentry->next = entry;
  168. xen_remap_bucket(entry, __size, address_index);
  169. } else if (!entry->lock) {
  170. if (!entry->vaddr_base || entry->paddr_index != address_index ||
  171. entry->size != __size ||
  172. !test_bits(address_offset >> XC_PAGE_SHIFT, size >> XC_PAGE_SHIFT,
  173. entry->valid_mapping)) {
  174. xen_remap_bucket(entry, __size, address_index);
  175. }
  176. }
  177. if(!test_bits(address_offset >> XC_PAGE_SHIFT, size >> XC_PAGE_SHIFT,
  178. entry->valid_mapping)) {
  179. mapcache->last_address_index = -1;
  180. trace_xen_map_cache_return(NULL);
  181. return NULL;
  182. }
  183. mapcache->last_address_index = address_index;
  184. mapcache->last_address_vaddr = entry->vaddr_base;
  185. if (lock) {
  186. MapCacheRev *reventry = qemu_mallocz(sizeof(MapCacheRev));
  187. entry->lock++;
  188. reventry->vaddr_req = mapcache->last_address_vaddr + address_offset;
  189. reventry->paddr_index = mapcache->last_address_index;
  190. reventry->size = entry->size;
  191. QTAILQ_INSERT_HEAD(&mapcache->locked_entries, reventry, next);
  192. }
  193. trace_xen_map_cache_return(mapcache->last_address_vaddr + address_offset);
  194. return mapcache->last_address_vaddr + address_offset;
  195. }
  196. ram_addr_t xen_ram_addr_from_mapcache(void *ptr)
  197. {
  198. MapCacheEntry *entry = NULL, *pentry = NULL;
  199. MapCacheRev *reventry;
  200. target_phys_addr_t paddr_index;
  201. target_phys_addr_t size;
  202. int found = 0;
  203. QTAILQ_FOREACH(reventry, &mapcache->locked_entries, next) {
  204. if (reventry->vaddr_req == ptr) {
  205. paddr_index = reventry->paddr_index;
  206. size = reventry->size;
  207. found = 1;
  208. break;
  209. }
  210. }
  211. if (!found) {
  212. fprintf(stderr, "%s, could not find %p\n", __func__, ptr);
  213. QTAILQ_FOREACH(reventry, &mapcache->locked_entries, next) {
  214. DPRINTF(" "TARGET_FMT_plx" -> %p is present\n", reventry->paddr_index,
  215. reventry->vaddr_req);
  216. }
  217. abort();
  218. return 0;
  219. }
  220. entry = &mapcache->entry[paddr_index % mapcache->nr_buckets];
  221. while (entry && (entry->paddr_index != paddr_index || entry->size != size)) {
  222. pentry = entry;
  223. entry = entry->next;
  224. }
  225. if (!entry) {
  226. DPRINTF("Trying to find address %p that is not in the mapcache!\n", ptr);
  227. return 0;
  228. }
  229. return (reventry->paddr_index << MCACHE_BUCKET_SHIFT) +
  230. ((unsigned long) ptr - (unsigned long) entry->vaddr_base);
  231. }
  232. void xen_invalidate_map_cache_entry(uint8_t *buffer)
  233. {
  234. MapCacheEntry *entry = NULL, *pentry = NULL;
  235. MapCacheRev *reventry;
  236. target_phys_addr_t paddr_index;
  237. target_phys_addr_t size;
  238. int found = 0;
  239. if (mapcache->last_address_vaddr == buffer) {
  240. mapcache->last_address_index = -1;
  241. }
  242. QTAILQ_FOREACH(reventry, &mapcache->locked_entries, next) {
  243. if (reventry->vaddr_req == buffer) {
  244. paddr_index = reventry->paddr_index;
  245. size = reventry->size;
  246. found = 1;
  247. break;
  248. }
  249. }
  250. if (!found) {
  251. DPRINTF("%s, could not find %p\n", __func__, buffer);
  252. QTAILQ_FOREACH(reventry, &mapcache->locked_entries, next) {
  253. DPRINTF(" "TARGET_FMT_plx" -> %p is present\n", reventry->paddr_index, reventry->vaddr_req);
  254. }
  255. return;
  256. }
  257. QTAILQ_REMOVE(&mapcache->locked_entries, reventry, next);
  258. qemu_free(reventry);
  259. entry = &mapcache->entry[paddr_index % mapcache->nr_buckets];
  260. while (entry && (entry->paddr_index != paddr_index || entry->size != size)) {
  261. pentry = entry;
  262. entry = entry->next;
  263. }
  264. if (!entry) {
  265. DPRINTF("Trying to unmap address %p that is not in the mapcache!\n", buffer);
  266. return;
  267. }
  268. entry->lock--;
  269. if (entry->lock > 0 || pentry == NULL) {
  270. return;
  271. }
  272. pentry->next = entry->next;
  273. if (munmap(entry->vaddr_base, entry->size) != 0) {
  274. perror("unmap fails");
  275. exit(-1);
  276. }
  277. qemu_free(entry->valid_mapping);
  278. qemu_free(entry);
  279. }
  280. void xen_invalidate_map_cache(void)
  281. {
  282. unsigned long i;
  283. MapCacheRev *reventry;
  284. /* Flush pending AIO before destroying the mapcache */
  285. qemu_aio_flush();
  286. QTAILQ_FOREACH(reventry, &mapcache->locked_entries, next) {
  287. DPRINTF("There should be no locked mappings at this time, "
  288. "but "TARGET_FMT_plx" -> %p is present\n",
  289. reventry->paddr_index, reventry->vaddr_req);
  290. }
  291. mapcache_lock();
  292. for (i = 0; i < mapcache->nr_buckets; i++) {
  293. MapCacheEntry *entry = &mapcache->entry[i];
  294. if (entry->vaddr_base == NULL) {
  295. continue;
  296. }
  297. if (munmap(entry->vaddr_base, entry->size) != 0) {
  298. perror("unmap fails");
  299. exit(-1);
  300. }
  301. entry->paddr_index = 0;
  302. entry->vaddr_base = NULL;
  303. entry->size = 0;
  304. qemu_free(entry->valid_mapping);
  305. entry->valid_mapping = NULL;
  306. }
  307. mapcache->last_address_index = -1;
  308. mapcache->last_address_vaddr = NULL;
  309. mapcache_unlock();
  310. }