p2m.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  1. /*
  2. * Xen leaves the responsibility for maintaining p2m mappings to the
  3. * guests themselves, but it must also access and update the p2m array
  4. * during suspend/resume when all the pages are reallocated.
  5. *
  6. * The p2m table is logically a flat array, but we implement it as a
  7. * three-level tree to allow the address space to be sparse.
  8. *
  9. * Xen
  10. * |
  11. * p2m_top p2m_top_mfn
  12. * / \ / \
  13. * p2m_mid p2m_mid p2m_mid_mfn p2m_mid_mfn
  14. * / \ / \ / /
  15. * p2m p2m p2m p2m p2m p2m p2m ...
  16. *
  17. * The p2m_mid_mfn pages are mapped by p2m_top_mfn_p.
  18. *
  19. * The p2m_top and p2m_top_mfn levels are limited to 1 page, so the
  20. * maximum representable pseudo-physical address space is:
  21. * P2M_TOP_PER_PAGE * P2M_MID_PER_PAGE * P2M_PER_PAGE pages
  22. *
  23. * P2M_PER_PAGE depends on the architecture, as a mfn is always
  24. * unsigned long (8 bytes on 64-bit, 4 bytes on 32), leading to
  25. * 512 and 1024 entries respectively.
  26. */
  27. #include <linux/init.h>
  28. #include <linux/module.h>
  29. #include <linux/list.h>
  30. #include <linux/hash.h>
  31. #include <asm/cache.h>
  32. #include <asm/setup.h>
  33. #include <asm/xen/page.h>
  34. #include <asm/xen/hypercall.h>
  35. #include <asm/xen/hypervisor.h>
  36. #include "xen-ops.h"
  37. static void __init m2p_override_init(void);
  38. unsigned long xen_max_p2m_pfn __read_mostly;
  39. #define P2M_PER_PAGE (PAGE_SIZE / sizeof(unsigned long))
  40. #define P2M_MID_PER_PAGE (PAGE_SIZE / sizeof(unsigned long *))
  41. #define P2M_TOP_PER_PAGE (PAGE_SIZE / sizeof(unsigned long **))
  42. #define MAX_P2M_PFN (P2M_TOP_PER_PAGE * P2M_MID_PER_PAGE * P2M_PER_PAGE)
  43. /* Placeholders for holes in the address space */
  44. static RESERVE_BRK_ARRAY(unsigned long, p2m_missing, P2M_PER_PAGE);
  45. static RESERVE_BRK_ARRAY(unsigned long *, p2m_mid_missing, P2M_MID_PER_PAGE);
  46. static RESERVE_BRK_ARRAY(unsigned long, p2m_mid_missing_mfn, P2M_MID_PER_PAGE);
  47. static RESERVE_BRK_ARRAY(unsigned long **, p2m_top, P2M_TOP_PER_PAGE);
  48. static RESERVE_BRK_ARRAY(unsigned long, p2m_top_mfn, P2M_TOP_PER_PAGE);
  49. static RESERVE_BRK_ARRAY(unsigned long *, p2m_top_mfn_p, P2M_TOP_PER_PAGE);
  50. RESERVE_BRK(p2m_mid, PAGE_SIZE * (MAX_DOMAIN_PAGES / (P2M_PER_PAGE * P2M_MID_PER_PAGE)));
  51. RESERVE_BRK(p2m_mid_mfn, PAGE_SIZE * (MAX_DOMAIN_PAGES / (P2M_PER_PAGE * P2M_MID_PER_PAGE)));
  52. static inline unsigned p2m_top_index(unsigned long pfn)
  53. {
  54. BUG_ON(pfn >= MAX_P2M_PFN);
  55. return pfn / (P2M_MID_PER_PAGE * P2M_PER_PAGE);
  56. }
  57. static inline unsigned p2m_mid_index(unsigned long pfn)
  58. {
  59. return (pfn / P2M_PER_PAGE) % P2M_MID_PER_PAGE;
  60. }
  61. static inline unsigned p2m_index(unsigned long pfn)
  62. {
  63. return pfn % P2M_PER_PAGE;
  64. }
  65. static void p2m_top_init(unsigned long ***top)
  66. {
  67. unsigned i;
  68. for (i = 0; i < P2M_TOP_PER_PAGE; i++)
  69. top[i] = p2m_mid_missing;
  70. }
  71. static void p2m_top_mfn_init(unsigned long *top)
  72. {
  73. unsigned i;
  74. for (i = 0; i < P2M_TOP_PER_PAGE; i++)
  75. top[i] = virt_to_mfn(p2m_mid_missing_mfn);
  76. }
  77. static void p2m_top_mfn_p_init(unsigned long **top)
  78. {
  79. unsigned i;
  80. for (i = 0; i < P2M_TOP_PER_PAGE; i++)
  81. top[i] = p2m_mid_missing_mfn;
  82. }
  83. static void p2m_mid_init(unsigned long **mid)
  84. {
  85. unsigned i;
  86. for (i = 0; i < P2M_MID_PER_PAGE; i++)
  87. mid[i] = p2m_missing;
  88. }
  89. static void p2m_mid_mfn_init(unsigned long *mid)
  90. {
  91. unsigned i;
  92. for (i = 0; i < P2M_MID_PER_PAGE; i++)
  93. mid[i] = virt_to_mfn(p2m_missing);
  94. }
  95. static void p2m_init(unsigned long *p2m)
  96. {
  97. unsigned i;
  98. for (i = 0; i < P2M_MID_PER_PAGE; i++)
  99. p2m[i] = INVALID_P2M_ENTRY;
  100. }
  101. /*
  102. * Build the parallel p2m_top_mfn and p2m_mid_mfn structures
  103. *
  104. * This is called both at boot time, and after resuming from suspend:
  105. * - At boot time we're called very early, and must use extend_brk()
  106. * to allocate memory.
  107. *
  108. * - After resume we're called from within stop_machine, but the mfn
  109. * tree should alreay be completely allocated.
  110. */
  111. void xen_build_mfn_list_list(void)
  112. {
  113. unsigned long pfn;
  114. /* Pre-initialize p2m_top_mfn to be completely missing */
  115. if (p2m_top_mfn == NULL) {
  116. p2m_mid_missing_mfn = extend_brk(PAGE_SIZE, PAGE_SIZE);
  117. p2m_mid_mfn_init(p2m_mid_missing_mfn);
  118. p2m_top_mfn_p = extend_brk(PAGE_SIZE, PAGE_SIZE);
  119. p2m_top_mfn_p_init(p2m_top_mfn_p);
  120. p2m_top_mfn = extend_brk(PAGE_SIZE, PAGE_SIZE);
  121. p2m_top_mfn_init(p2m_top_mfn);
  122. } else {
  123. /* Reinitialise, mfn's all change after migration */
  124. p2m_mid_mfn_init(p2m_mid_missing_mfn);
  125. }
  126. for (pfn = 0; pfn < xen_max_p2m_pfn; pfn += P2M_PER_PAGE) {
  127. unsigned topidx = p2m_top_index(pfn);
  128. unsigned mididx = p2m_mid_index(pfn);
  129. unsigned long **mid;
  130. unsigned long *mid_mfn_p;
  131. mid = p2m_top[topidx];
  132. mid_mfn_p = p2m_top_mfn_p[topidx];
  133. /* Don't bother allocating any mfn mid levels if
  134. * they're just missing, just update the stored mfn,
  135. * since all could have changed over a migrate.
  136. */
  137. if (mid == p2m_mid_missing) {
  138. BUG_ON(mididx);
  139. BUG_ON(mid_mfn_p != p2m_mid_missing_mfn);
  140. p2m_top_mfn[topidx] = virt_to_mfn(p2m_mid_missing_mfn);
  141. pfn += (P2M_MID_PER_PAGE - 1) * P2M_PER_PAGE;
  142. continue;
  143. }
  144. if (mid_mfn_p == p2m_mid_missing_mfn) {
  145. /*
  146. * XXX boot-time only! We should never find
  147. * missing parts of the mfn tree after
  148. * runtime. extend_brk() will BUG if we call
  149. * it too late.
  150. */
  151. mid_mfn_p = extend_brk(PAGE_SIZE, PAGE_SIZE);
  152. p2m_mid_mfn_init(mid_mfn_p);
  153. p2m_top_mfn_p[topidx] = mid_mfn_p;
  154. }
  155. p2m_top_mfn[topidx] = virt_to_mfn(mid_mfn_p);
  156. mid_mfn_p[mididx] = virt_to_mfn(mid[mididx]);
  157. }
  158. }
  159. void xen_setup_mfn_list_list(void)
  160. {
  161. BUG_ON(HYPERVISOR_shared_info == &xen_dummy_shared_info);
  162. HYPERVISOR_shared_info->arch.pfn_to_mfn_frame_list_list =
  163. virt_to_mfn(p2m_top_mfn);
  164. HYPERVISOR_shared_info->arch.max_pfn = xen_max_p2m_pfn;
  165. }
  166. /* Set up p2m_top to point to the domain-builder provided p2m pages */
  167. void __init xen_build_dynamic_phys_to_machine(void)
  168. {
  169. unsigned long *mfn_list = (unsigned long *)xen_start_info->mfn_list;
  170. unsigned long max_pfn = min(MAX_DOMAIN_PAGES, xen_start_info->nr_pages);
  171. unsigned long pfn;
  172. xen_max_p2m_pfn = max_pfn;
  173. p2m_missing = extend_brk(PAGE_SIZE, PAGE_SIZE);
  174. p2m_init(p2m_missing);
  175. p2m_mid_missing = extend_brk(PAGE_SIZE, PAGE_SIZE);
  176. p2m_mid_init(p2m_mid_missing);
  177. p2m_top = extend_brk(PAGE_SIZE, PAGE_SIZE);
  178. p2m_top_init(p2m_top);
  179. /*
  180. * The domain builder gives us a pre-constructed p2m array in
  181. * mfn_list for all the pages initially given to us, so we just
  182. * need to graft that into our tree structure.
  183. */
  184. for (pfn = 0; pfn < max_pfn; pfn += P2M_PER_PAGE) {
  185. unsigned topidx = p2m_top_index(pfn);
  186. unsigned mididx = p2m_mid_index(pfn);
  187. if (p2m_top[topidx] == p2m_mid_missing) {
  188. unsigned long **mid = extend_brk(PAGE_SIZE, PAGE_SIZE);
  189. p2m_mid_init(mid);
  190. p2m_top[topidx] = mid;
  191. }
  192. p2m_top[topidx][mididx] = &mfn_list[pfn];
  193. }
  194. m2p_override_init();
  195. }
  196. unsigned long get_phys_to_machine(unsigned long pfn)
  197. {
  198. unsigned topidx, mididx, idx;
  199. if (unlikely(pfn >= MAX_P2M_PFN))
  200. return INVALID_P2M_ENTRY;
  201. topidx = p2m_top_index(pfn);
  202. mididx = p2m_mid_index(pfn);
  203. idx = p2m_index(pfn);
  204. return p2m_top[topidx][mididx][idx];
  205. }
  206. EXPORT_SYMBOL_GPL(get_phys_to_machine);
  207. static void *alloc_p2m_page(void)
  208. {
  209. return (void *)__get_free_page(GFP_KERNEL | __GFP_REPEAT);
  210. }
  211. static void free_p2m_page(void *p)
  212. {
  213. free_page((unsigned long)p);
  214. }
  215. /*
  216. * Fully allocate the p2m structure for a given pfn. We need to check
  217. * that both the top and mid levels are allocated, and make sure the
  218. * parallel mfn tree is kept in sync. We may race with other cpus, so
  219. * the new pages are installed with cmpxchg; if we lose the race then
  220. * simply free the page we allocated and use the one that's there.
  221. */
  222. static bool alloc_p2m(unsigned long pfn)
  223. {
  224. unsigned topidx, mididx;
  225. unsigned long ***top_p, **mid;
  226. unsigned long *top_mfn_p, *mid_mfn;
  227. topidx = p2m_top_index(pfn);
  228. mididx = p2m_mid_index(pfn);
  229. top_p = &p2m_top[topidx];
  230. mid = *top_p;
  231. if (mid == p2m_mid_missing) {
  232. /* Mid level is missing, allocate a new one */
  233. mid = alloc_p2m_page();
  234. if (!mid)
  235. return false;
  236. p2m_mid_init(mid);
  237. if (cmpxchg(top_p, p2m_mid_missing, mid) != p2m_mid_missing)
  238. free_p2m_page(mid);
  239. }
  240. top_mfn_p = &p2m_top_mfn[topidx];
  241. mid_mfn = p2m_top_mfn_p[topidx];
  242. BUG_ON(virt_to_mfn(mid_mfn) != *top_mfn_p);
  243. if (mid_mfn == p2m_mid_missing_mfn) {
  244. /* Separately check the mid mfn level */
  245. unsigned long missing_mfn;
  246. unsigned long mid_mfn_mfn;
  247. mid_mfn = alloc_p2m_page();
  248. if (!mid_mfn)
  249. return false;
  250. p2m_mid_mfn_init(mid_mfn);
  251. missing_mfn = virt_to_mfn(p2m_mid_missing_mfn);
  252. mid_mfn_mfn = virt_to_mfn(mid_mfn);
  253. if (cmpxchg(top_mfn_p, missing_mfn, mid_mfn_mfn) != missing_mfn)
  254. free_p2m_page(mid_mfn);
  255. else
  256. p2m_top_mfn_p[topidx] = mid_mfn;
  257. }
  258. if (p2m_top[topidx][mididx] == p2m_missing) {
  259. /* p2m leaf page is missing */
  260. unsigned long *p2m;
  261. p2m = alloc_p2m_page();
  262. if (!p2m)
  263. return false;
  264. p2m_init(p2m);
  265. if (cmpxchg(&mid[mididx], p2m_missing, p2m) != p2m_missing)
  266. free_p2m_page(p2m);
  267. else
  268. mid_mfn[mididx] = virt_to_mfn(p2m);
  269. }
  270. return true;
  271. }
  272. /* Try to install p2m mapping; fail if intermediate bits missing */
  273. bool __set_phys_to_machine(unsigned long pfn, unsigned long mfn)
  274. {
  275. unsigned topidx, mididx, idx;
  276. if (unlikely(pfn >= MAX_P2M_PFN)) {
  277. BUG_ON(mfn != INVALID_P2M_ENTRY);
  278. return true;
  279. }
  280. topidx = p2m_top_index(pfn);
  281. mididx = p2m_mid_index(pfn);
  282. idx = p2m_index(pfn);
  283. if (p2m_top[topidx][mididx] == p2m_missing)
  284. return mfn == INVALID_P2M_ENTRY;
  285. p2m_top[topidx][mididx][idx] = mfn;
  286. return true;
  287. }
  288. bool set_phys_to_machine(unsigned long pfn, unsigned long mfn)
  289. {
  290. if (unlikely(xen_feature(XENFEAT_auto_translated_physmap))) {
  291. BUG_ON(pfn != mfn && mfn != INVALID_P2M_ENTRY);
  292. return true;
  293. }
  294. if (unlikely(!__set_phys_to_machine(pfn, mfn))) {
  295. if (!alloc_p2m(pfn))
  296. return false;
  297. if (!__set_phys_to_machine(pfn, mfn))
  298. return false;
  299. }
  300. return true;
  301. }
  302. #define M2P_OVERRIDE_HASH_SHIFT 10
  303. #define M2P_OVERRIDE_HASH (1 << M2P_OVERRIDE_HASH_SHIFT)
  304. static RESERVE_BRK_ARRAY(struct list_head, m2p_overrides, M2P_OVERRIDE_HASH);
  305. static DEFINE_SPINLOCK(m2p_override_lock);
  306. static void __init m2p_override_init(void)
  307. {
  308. unsigned i;
  309. m2p_overrides = extend_brk(sizeof(*m2p_overrides) * M2P_OVERRIDE_HASH,
  310. sizeof(unsigned long));
  311. for (i = 0; i < M2P_OVERRIDE_HASH; i++)
  312. INIT_LIST_HEAD(&m2p_overrides[i]);
  313. }
  314. static unsigned long mfn_hash(unsigned long mfn)
  315. {
  316. return hash_long(mfn, M2P_OVERRIDE_HASH_SHIFT);
  317. }
  318. /* Add an MFN override for a particular page */
  319. void m2p_add_override(unsigned long mfn, struct page *page)
  320. {
  321. unsigned long flags;
  322. unsigned long pfn = page_to_pfn(page);
  323. page->private = mfn;
  324. page->index = pfn_to_mfn(pfn);
  325. __set_phys_to_machine(pfn, FOREIGN_FRAME(mfn));
  326. spin_lock_irqsave(&m2p_override_lock, flags);
  327. list_add(&page->lru, &m2p_overrides[mfn_hash(mfn)]);
  328. spin_unlock_irqrestore(&m2p_override_lock, flags);
  329. }
  330. void m2p_remove_override(struct page *page)
  331. {
  332. unsigned long flags;
  333. unsigned long mfn;
  334. unsigned long pfn;
  335. pfn = page_to_pfn(page);
  336. mfn = get_phys_to_machine(pfn);
  337. if (mfn == INVALID_P2M_ENTRY || !(mfn & FOREIGN_FRAME_BIT))
  338. return;
  339. spin_lock_irqsave(&m2p_override_lock, flags);
  340. list_del(&page->lru);
  341. spin_unlock_irqrestore(&m2p_override_lock, flags);
  342. __set_phys_to_machine(pfn, page->index);
  343. }
  344. struct page *m2p_find_override(unsigned long mfn)
  345. {
  346. unsigned long flags;
  347. struct list_head *bucket = &m2p_overrides[mfn_hash(mfn)];
  348. struct page *p, *ret;
  349. ret = NULL;
  350. spin_lock_irqsave(&m2p_override_lock, flags);
  351. list_for_each_entry(p, bucket, lru) {
  352. if (p->private == mfn) {
  353. ret = p;
  354. break;
  355. }
  356. }
  357. spin_unlock_irqrestore(&m2p_override_lock, flags);
  358. return ret;
  359. }
  360. unsigned long m2p_find_override_pfn(unsigned long mfn, unsigned long pfn)
  361. {
  362. struct page *p = m2p_find_override(mfn);
  363. unsigned long ret = pfn;
  364. if (p)
  365. ret = page_to_pfn(p);
  366. return ret;
  367. }