p2m.c 13 KB

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