2
0

xen-mapcache.c 13 KB

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