2
0

xen-mapcache.c 12 KB

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