xen-mapcache.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  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/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. MapCacheEntry *last_entry;
  63. unsigned long max_mcache_size;
  64. unsigned int mcache_bucket_shift;
  65. phys_offset_to_gaddr_t phys_offset_to_gaddr;
  66. void *opaque;
  67. } MapCache;
  68. static MapCache *mapcache;
  69. static inline int test_bits(int nr, int size, const unsigned long *addr)
  70. {
  71. unsigned long res = find_next_zero_bit(addr, size + nr, nr);
  72. if (res >= nr + size)
  73. return 1;
  74. else
  75. return 0;
  76. }
  77. void xen_map_cache_init(phys_offset_to_gaddr_t f, void *opaque)
  78. {
  79. unsigned long size;
  80. struct rlimit rlimit_as;
  81. mapcache = g_malloc0(sizeof (MapCache));
  82. mapcache->phys_offset_to_gaddr = f;
  83. mapcache->opaque = opaque;
  84. QTAILQ_INIT(&mapcache->locked_entries);
  85. if (geteuid() == 0) {
  86. rlimit_as.rlim_cur = RLIM_INFINITY;
  87. rlimit_as.rlim_max = RLIM_INFINITY;
  88. mapcache->max_mcache_size = MCACHE_MAX_SIZE;
  89. } else {
  90. getrlimit(RLIMIT_AS, &rlimit_as);
  91. rlimit_as.rlim_cur = rlimit_as.rlim_max;
  92. if (rlimit_as.rlim_max != RLIM_INFINITY) {
  93. fprintf(stderr, "Warning: QEMU's maximum size of virtual"
  94. " memory is not infinity.\n");
  95. }
  96. if (rlimit_as.rlim_max < MCACHE_MAX_SIZE + NON_MCACHE_MEMORY_SIZE) {
  97. mapcache->max_mcache_size = rlimit_as.rlim_max -
  98. NON_MCACHE_MEMORY_SIZE;
  99. } else {
  100. mapcache->max_mcache_size = MCACHE_MAX_SIZE;
  101. }
  102. }
  103. setrlimit(RLIMIT_AS, &rlimit_as);
  104. mapcache->nr_buckets =
  105. (((mapcache->max_mcache_size >> XC_PAGE_SHIFT) +
  106. (1UL << (MCACHE_BUCKET_SHIFT - XC_PAGE_SHIFT)) - 1) >>
  107. (MCACHE_BUCKET_SHIFT - XC_PAGE_SHIFT));
  108. size = mapcache->nr_buckets * sizeof (MapCacheEntry);
  109. size = (size + XC_PAGE_SIZE - 1) & ~(XC_PAGE_SIZE - 1);
  110. DPRINTF("%s, nr_buckets = %lx size %lu\n", __func__,
  111. mapcache->nr_buckets, size);
  112. mapcache->entry = g_malloc0(size);
  113. }
  114. static void xen_remap_bucket(MapCacheEntry *entry,
  115. hwaddr size,
  116. hwaddr address_index)
  117. {
  118. uint8_t *vaddr_base;
  119. xen_pfn_t *pfns;
  120. int *err;
  121. unsigned int i;
  122. hwaddr nb_pfn = size >> XC_PAGE_SHIFT;
  123. trace_xen_remap_bucket(address_index);
  124. pfns = g_malloc0(nb_pfn * sizeof (xen_pfn_t));
  125. err = g_malloc0(nb_pfn * sizeof (int));
  126. if (entry->vaddr_base != NULL) {
  127. if (munmap(entry->vaddr_base, entry->size) != 0) {
  128. perror("unmap fails");
  129. exit(-1);
  130. }
  131. }
  132. if (entry->valid_mapping != NULL) {
  133. g_free(entry->valid_mapping);
  134. entry->valid_mapping = NULL;
  135. }
  136. for (i = 0; i < nb_pfn; i++) {
  137. pfns[i] = (address_index << (MCACHE_BUCKET_SHIFT-XC_PAGE_SHIFT)) + i;
  138. }
  139. vaddr_base = xc_map_foreign_bulk(xen_xc, xen_domid, PROT_READ|PROT_WRITE,
  140. pfns, err, nb_pfn);
  141. if (vaddr_base == NULL) {
  142. perror("xc_map_foreign_bulk");
  143. exit(-1);
  144. }
  145. entry->vaddr_base = vaddr_base;
  146. entry->paddr_index = address_index;
  147. entry->size = size;
  148. entry->valid_mapping = (unsigned long *) g_malloc0(sizeof(unsigned long) *
  149. BITS_TO_LONGS(size >> XC_PAGE_SHIFT));
  150. bitmap_zero(entry->valid_mapping, nb_pfn);
  151. for (i = 0; i < nb_pfn; i++) {
  152. if (!err[i]) {
  153. bitmap_set(entry->valid_mapping, i, 1);
  154. }
  155. }
  156. g_free(pfns);
  157. g_free(err);
  158. }
  159. uint8_t *xen_map_cache(hwaddr phys_addr, hwaddr size,
  160. uint8_t lock)
  161. {
  162. MapCacheEntry *entry, *pentry = NULL;
  163. hwaddr address_index;
  164. hwaddr address_offset;
  165. hwaddr __size = size;
  166. hwaddr __test_bit_size = size;
  167. bool translated = false;
  168. tryagain:
  169. address_index = phys_addr >> MCACHE_BUCKET_SHIFT;
  170. address_offset = phys_addr & (MCACHE_BUCKET_SIZE - 1);
  171. trace_xen_map_cache(phys_addr);
  172. /* __test_bit_size is always a multiple of XC_PAGE_SIZE */
  173. if (size) {
  174. __test_bit_size = size + (phys_addr & (XC_PAGE_SIZE - 1));
  175. if (__test_bit_size % XC_PAGE_SIZE) {
  176. __test_bit_size += XC_PAGE_SIZE - (__test_bit_size % XC_PAGE_SIZE);
  177. }
  178. } else {
  179. __test_bit_size = XC_PAGE_SIZE;
  180. }
  181. if (mapcache->last_entry != NULL &&
  182. mapcache->last_entry->paddr_index == address_index &&
  183. !lock && !__size &&
  184. test_bits(address_offset >> XC_PAGE_SHIFT,
  185. __test_bit_size >> XC_PAGE_SHIFT,
  186. mapcache->last_entry->valid_mapping)) {
  187. trace_xen_map_cache_return(mapcache->last_entry->vaddr_base + address_offset);
  188. return mapcache->last_entry->vaddr_base + address_offset;
  189. }
  190. /* size is always a multiple of MCACHE_BUCKET_SIZE */
  191. if (size) {
  192. __size = size + address_offset;
  193. if (__size % MCACHE_BUCKET_SIZE) {
  194. __size += MCACHE_BUCKET_SIZE - (__size % MCACHE_BUCKET_SIZE);
  195. }
  196. } else {
  197. __size = MCACHE_BUCKET_SIZE;
  198. }
  199. entry = &mapcache->entry[address_index % mapcache->nr_buckets];
  200. while (entry && entry->lock && entry->vaddr_base &&
  201. (entry->paddr_index != address_index || entry->size != __size ||
  202. !test_bits(address_offset >> XC_PAGE_SHIFT,
  203. __test_bit_size >> XC_PAGE_SHIFT,
  204. entry->valid_mapping))) {
  205. pentry = entry;
  206. entry = entry->next;
  207. }
  208. if (!entry) {
  209. entry = g_malloc0(sizeof (MapCacheEntry));
  210. pentry->next = entry;
  211. xen_remap_bucket(entry, __size, address_index);
  212. } else if (!entry->lock) {
  213. if (!entry->vaddr_base || entry->paddr_index != address_index ||
  214. entry->size != __size ||
  215. !test_bits(address_offset >> XC_PAGE_SHIFT,
  216. __test_bit_size >> XC_PAGE_SHIFT,
  217. entry->valid_mapping)) {
  218. xen_remap_bucket(entry, __size, address_index);
  219. }
  220. }
  221. if(!test_bits(address_offset >> XC_PAGE_SHIFT,
  222. __test_bit_size >> XC_PAGE_SHIFT,
  223. entry->valid_mapping)) {
  224. mapcache->last_entry = NULL;
  225. if (!translated && mapcache->phys_offset_to_gaddr) {
  226. phys_addr = mapcache->phys_offset_to_gaddr(phys_addr, size, mapcache->opaque);
  227. translated = true;
  228. goto tryagain;
  229. }
  230. trace_xen_map_cache_return(NULL);
  231. return NULL;
  232. }
  233. mapcache->last_entry = entry;
  234. if (lock) {
  235. MapCacheRev *reventry = g_malloc0(sizeof(MapCacheRev));
  236. entry->lock++;
  237. reventry->vaddr_req = mapcache->last_entry->vaddr_base + address_offset;
  238. reventry->paddr_index = mapcache->last_entry->paddr_index;
  239. reventry->size = entry->size;
  240. QTAILQ_INSERT_HEAD(&mapcache->locked_entries, reventry, next);
  241. }
  242. trace_xen_map_cache_return(mapcache->last_entry->vaddr_base + address_offset);
  243. return mapcache->last_entry->vaddr_base + address_offset;
  244. }
  245. ram_addr_t xen_ram_addr_from_mapcache(void *ptr)
  246. {
  247. MapCacheEntry *entry = NULL;
  248. MapCacheRev *reventry;
  249. hwaddr paddr_index;
  250. hwaddr size;
  251. int found = 0;
  252. QTAILQ_FOREACH(reventry, &mapcache->locked_entries, next) {
  253. if (reventry->vaddr_req == ptr) {
  254. paddr_index = reventry->paddr_index;
  255. size = reventry->size;
  256. found = 1;
  257. break;
  258. }
  259. }
  260. if (!found) {
  261. fprintf(stderr, "%s, could not find %p\n", __func__, ptr);
  262. QTAILQ_FOREACH(reventry, &mapcache->locked_entries, next) {
  263. DPRINTF(" "TARGET_FMT_plx" -> %p is present\n", reventry->paddr_index,
  264. reventry->vaddr_req);
  265. }
  266. abort();
  267. return 0;
  268. }
  269. entry = &mapcache->entry[paddr_index % mapcache->nr_buckets];
  270. while (entry && (entry->paddr_index != paddr_index || entry->size != size)) {
  271. entry = entry->next;
  272. }
  273. if (!entry) {
  274. DPRINTF("Trying to find address %p that is not in the mapcache!\n", ptr);
  275. return 0;
  276. }
  277. return (reventry->paddr_index << MCACHE_BUCKET_SHIFT) +
  278. ((unsigned long) ptr - (unsigned long) entry->vaddr_base);
  279. }
  280. void xen_invalidate_map_cache_entry(uint8_t *buffer)
  281. {
  282. MapCacheEntry *entry = NULL, *pentry = NULL;
  283. MapCacheRev *reventry;
  284. hwaddr paddr_index;
  285. hwaddr size;
  286. int found = 0;
  287. QTAILQ_FOREACH(reventry, &mapcache->locked_entries, next) {
  288. if (reventry->vaddr_req == buffer) {
  289. paddr_index = reventry->paddr_index;
  290. size = reventry->size;
  291. found = 1;
  292. break;
  293. }
  294. }
  295. if (!found) {
  296. DPRINTF("%s, could not find %p\n", __func__, buffer);
  297. QTAILQ_FOREACH(reventry, &mapcache->locked_entries, next) {
  298. DPRINTF(" "TARGET_FMT_plx" -> %p is present\n", reventry->paddr_index, reventry->vaddr_req);
  299. }
  300. return;
  301. }
  302. QTAILQ_REMOVE(&mapcache->locked_entries, reventry, next);
  303. g_free(reventry);
  304. if (mapcache->last_entry != NULL &&
  305. mapcache->last_entry->paddr_index == paddr_index) {
  306. mapcache->last_entry = NULL;
  307. }
  308. entry = &mapcache->entry[paddr_index % mapcache->nr_buckets];
  309. while (entry && (entry->paddr_index != paddr_index || entry->size != size)) {
  310. pentry = entry;
  311. entry = entry->next;
  312. }
  313. if (!entry) {
  314. DPRINTF("Trying to unmap address %p that is not in the mapcache!\n", buffer);
  315. return;
  316. }
  317. entry->lock--;
  318. if (entry->lock > 0 || pentry == NULL) {
  319. return;
  320. }
  321. pentry->next = entry->next;
  322. if (munmap(entry->vaddr_base, entry->size) != 0) {
  323. perror("unmap fails");
  324. exit(-1);
  325. }
  326. g_free(entry->valid_mapping);
  327. g_free(entry);
  328. }
  329. void xen_invalidate_map_cache(void)
  330. {
  331. unsigned long i;
  332. MapCacheRev *reventry;
  333. /* Flush pending AIO before destroying the mapcache */
  334. bdrv_drain_all();
  335. QTAILQ_FOREACH(reventry, &mapcache->locked_entries, next) {
  336. DPRINTF("There should be no locked mappings at this time, "
  337. "but "TARGET_FMT_plx" -> %p is present\n",
  338. reventry->paddr_index, reventry->vaddr_req);
  339. }
  340. mapcache_lock();
  341. for (i = 0; i < mapcache->nr_buckets; i++) {
  342. MapCacheEntry *entry = &mapcache->entry[i];
  343. if (entry->vaddr_base == NULL) {
  344. continue;
  345. }
  346. if (entry->lock > 0) {
  347. continue;
  348. }
  349. if (munmap(entry->vaddr_base, entry->size) != 0) {
  350. perror("unmap fails");
  351. exit(-1);
  352. }
  353. entry->paddr_index = 0;
  354. entry->vaddr_base = NULL;
  355. entry->size = 0;
  356. g_free(entry->valid_mapping);
  357. entry->valid_mapping = NULL;
  358. }
  359. mapcache->last_entry = NULL;
  360. mapcache_unlock();
  361. }