p2m.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  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 <asm/cache.h>
  30. #include <asm/setup.h>
  31. #include <asm/xen/page.h>
  32. #include <asm/xen/hypercall.h>
  33. #include <asm/xen/hypervisor.h>
  34. #include "xen-ops.h"
  35. unsigned long xen_max_p2m_pfn __read_mostly;
  36. #define P2M_PER_PAGE (PAGE_SIZE / sizeof(unsigned long))
  37. #define P2M_MID_PER_PAGE (PAGE_SIZE / sizeof(unsigned long *))
  38. #define P2M_TOP_PER_PAGE (PAGE_SIZE / sizeof(unsigned long **))
  39. #define MAX_P2M_PFN (P2M_TOP_PER_PAGE * P2M_MID_PER_PAGE * P2M_PER_PAGE)
  40. /* Placeholders for holes in the address space */
  41. static RESERVE_BRK_ARRAY(unsigned long, p2m_missing, P2M_PER_PAGE);
  42. static RESERVE_BRK_ARRAY(unsigned long *, p2m_mid_missing, P2M_MID_PER_PAGE);
  43. static RESERVE_BRK_ARRAY(unsigned long, p2m_mid_missing_mfn, P2M_MID_PER_PAGE);
  44. static RESERVE_BRK_ARRAY(unsigned long **, p2m_top, P2M_TOP_PER_PAGE);
  45. static RESERVE_BRK_ARRAY(unsigned long, p2m_top_mfn, P2M_TOP_PER_PAGE);
  46. static RESERVE_BRK_ARRAY(unsigned long *, p2m_top_mfn_p, P2M_TOP_PER_PAGE);
  47. RESERVE_BRK(p2m_mid, PAGE_SIZE * (MAX_DOMAIN_PAGES / (P2M_PER_PAGE * P2M_MID_PER_PAGE)));
  48. RESERVE_BRK(p2m_mid_mfn, PAGE_SIZE * (MAX_DOMAIN_PAGES / (P2M_PER_PAGE * P2M_MID_PER_PAGE)));
  49. static inline unsigned p2m_top_index(unsigned long pfn)
  50. {
  51. BUG_ON(pfn >= MAX_P2M_PFN);
  52. return pfn / (P2M_MID_PER_PAGE * P2M_PER_PAGE);
  53. }
  54. static inline unsigned p2m_mid_index(unsigned long pfn)
  55. {
  56. return (pfn / P2M_PER_PAGE) % P2M_MID_PER_PAGE;
  57. }
  58. static inline unsigned p2m_index(unsigned long pfn)
  59. {
  60. return pfn % P2M_PER_PAGE;
  61. }
  62. static void p2m_top_init(unsigned long ***top)
  63. {
  64. unsigned i;
  65. for (i = 0; i < P2M_TOP_PER_PAGE; i++)
  66. top[i] = p2m_mid_missing;
  67. }
  68. static void p2m_top_mfn_init(unsigned long *top)
  69. {
  70. unsigned i;
  71. for (i = 0; i < P2M_TOP_PER_PAGE; i++)
  72. top[i] = virt_to_mfn(p2m_mid_missing_mfn);
  73. }
  74. static void p2m_top_mfn_p_init(unsigned long **top)
  75. {
  76. unsigned i;
  77. for (i = 0; i < P2M_TOP_PER_PAGE; i++)
  78. top[i] = p2m_mid_missing_mfn;
  79. }
  80. static void p2m_mid_init(unsigned long **mid)
  81. {
  82. unsigned i;
  83. for (i = 0; i < P2M_MID_PER_PAGE; i++)
  84. mid[i] = p2m_missing;
  85. }
  86. static void p2m_mid_mfn_init(unsigned long *mid)
  87. {
  88. unsigned i;
  89. for (i = 0; i < P2M_MID_PER_PAGE; i++)
  90. mid[i] = virt_to_mfn(p2m_missing);
  91. }
  92. static void p2m_init(unsigned long *p2m)
  93. {
  94. unsigned i;
  95. for (i = 0; i < P2M_MID_PER_PAGE; i++)
  96. p2m[i] = INVALID_P2M_ENTRY;
  97. }
  98. /*
  99. * Build the parallel p2m_top_mfn and p2m_mid_mfn structures
  100. *
  101. * This is called both at boot time, and after resuming from suspend:
  102. * - At boot time we're called very early, and must use extend_brk()
  103. * to allocate memory.
  104. *
  105. * - After resume we're called from within stop_machine, but the mfn
  106. * tree should alreay be completely allocated.
  107. */
  108. void xen_build_mfn_list_list(void)
  109. {
  110. unsigned long pfn;
  111. /* Pre-initialize p2m_top_mfn to be completely missing */
  112. if (p2m_top_mfn == NULL) {
  113. p2m_mid_missing_mfn = extend_brk(PAGE_SIZE, PAGE_SIZE);
  114. p2m_mid_mfn_init(p2m_mid_missing_mfn);
  115. p2m_top_mfn_p = extend_brk(PAGE_SIZE, PAGE_SIZE);
  116. p2m_top_mfn_p_init(p2m_top_mfn_p);
  117. p2m_top_mfn = extend_brk(PAGE_SIZE, PAGE_SIZE);
  118. p2m_top_mfn_init(p2m_top_mfn);
  119. } else {
  120. /* Reinitialise, mfn's all change after migration */
  121. p2m_mid_mfn_init(p2m_mid_missing_mfn);
  122. }
  123. for (pfn = 0; pfn < xen_max_p2m_pfn; pfn += P2M_PER_PAGE) {
  124. unsigned topidx = p2m_top_index(pfn);
  125. unsigned mididx = p2m_mid_index(pfn);
  126. unsigned long **mid;
  127. unsigned long *mid_mfn_p;
  128. mid = p2m_top[topidx];
  129. mid_mfn_p = p2m_top_mfn_p[topidx];
  130. /* Don't bother allocating any mfn mid levels if
  131. * they're just missing, just update the stored mfn,
  132. * since all could have changed over a migrate.
  133. */
  134. if (mid == p2m_mid_missing) {
  135. BUG_ON(mididx);
  136. BUG_ON(mid_mfn_p != p2m_mid_missing_mfn);
  137. p2m_top_mfn[topidx] = virt_to_mfn(p2m_mid_missing_mfn);
  138. pfn += (P2M_MID_PER_PAGE - 1) * P2M_PER_PAGE;
  139. continue;
  140. }
  141. if (mid_mfn_p == p2m_mid_missing_mfn) {
  142. /*
  143. * XXX boot-time only! We should never find
  144. * missing parts of the mfn tree after
  145. * runtime. extend_brk() will BUG if we call
  146. * it too late.
  147. */
  148. mid_mfn_p = extend_brk(PAGE_SIZE, PAGE_SIZE);
  149. p2m_mid_mfn_init(mid_mfn_p);
  150. p2m_top_mfn_p[topidx] = mid_mfn_p;
  151. }
  152. p2m_top_mfn[topidx] = virt_to_mfn(mid_mfn_p);
  153. mid_mfn_p[mididx] = virt_to_mfn(mid[mididx]);
  154. }
  155. }
  156. void xen_setup_mfn_list_list(void)
  157. {
  158. BUG_ON(HYPERVISOR_shared_info == &xen_dummy_shared_info);
  159. HYPERVISOR_shared_info->arch.pfn_to_mfn_frame_list_list =
  160. virt_to_mfn(p2m_top_mfn);
  161. HYPERVISOR_shared_info->arch.max_pfn = xen_max_p2m_pfn;
  162. }
  163. /* Set up p2m_top to point to the domain-builder provided p2m pages */
  164. void __init xen_build_dynamic_phys_to_machine(void)
  165. {
  166. unsigned long *mfn_list = (unsigned long *)xen_start_info->mfn_list;
  167. unsigned long max_pfn = min(MAX_DOMAIN_PAGES, xen_start_info->nr_pages);
  168. unsigned long pfn;
  169. xen_max_p2m_pfn = max_pfn;
  170. p2m_missing = extend_brk(PAGE_SIZE, PAGE_SIZE);
  171. p2m_init(p2m_missing);
  172. p2m_mid_missing = extend_brk(PAGE_SIZE, PAGE_SIZE);
  173. p2m_mid_init(p2m_mid_missing);
  174. p2m_top = extend_brk(PAGE_SIZE, PAGE_SIZE);
  175. p2m_top_init(p2m_top);
  176. /*
  177. * The domain builder gives us a pre-constructed p2m array in
  178. * mfn_list for all the pages initially given to us, so we just
  179. * need to graft that into our tree structure.
  180. */
  181. for (pfn = 0; pfn < max_pfn; pfn += P2M_PER_PAGE) {
  182. unsigned topidx = p2m_top_index(pfn);
  183. unsigned mididx = p2m_mid_index(pfn);
  184. if (p2m_top[topidx] == p2m_mid_missing) {
  185. unsigned long **mid = extend_brk(PAGE_SIZE, PAGE_SIZE);
  186. p2m_mid_init(mid);
  187. p2m_top[topidx] = mid;
  188. }
  189. p2m_top[topidx][mididx] = &mfn_list[pfn];
  190. }
  191. }
  192. unsigned long get_phys_to_machine(unsigned long pfn)
  193. {
  194. unsigned topidx, mididx, idx;
  195. if (unlikely(pfn >= MAX_P2M_PFN))
  196. return INVALID_P2M_ENTRY;
  197. topidx = p2m_top_index(pfn);
  198. mididx = p2m_mid_index(pfn);
  199. idx = p2m_index(pfn);
  200. return p2m_top[topidx][mididx][idx];
  201. }
  202. EXPORT_SYMBOL_GPL(get_phys_to_machine);
  203. static void *alloc_p2m_page(void)
  204. {
  205. return (void *)__get_free_page(GFP_KERNEL | __GFP_REPEAT);
  206. }
  207. static void free_p2m_page(void *p)
  208. {
  209. free_page((unsigned long)p);
  210. }
  211. /*
  212. * Fully allocate the p2m structure for a given pfn. We need to check
  213. * that both the top and mid levels are allocated, and make sure the
  214. * parallel mfn tree is kept in sync. We may race with other cpus, so
  215. * the new pages are installed with cmpxchg; if we lose the race then
  216. * simply free the page we allocated and use the one that's there.
  217. */
  218. static bool alloc_p2m(unsigned long pfn)
  219. {
  220. unsigned topidx, mididx;
  221. unsigned long ***top_p, **mid;
  222. unsigned long *top_mfn_p, *mid_mfn;
  223. topidx = p2m_top_index(pfn);
  224. mididx = p2m_mid_index(pfn);
  225. top_p = &p2m_top[topidx];
  226. mid = *top_p;
  227. if (mid == p2m_mid_missing) {
  228. /* Mid level is missing, allocate a new one */
  229. mid = alloc_p2m_page();
  230. if (!mid)
  231. return false;
  232. p2m_mid_init(mid);
  233. if (cmpxchg(top_p, p2m_mid_missing, mid) != p2m_mid_missing)
  234. free_p2m_page(mid);
  235. }
  236. top_mfn_p = &p2m_top_mfn[topidx];
  237. mid_mfn = p2m_top_mfn_p[topidx];
  238. BUG_ON(virt_to_mfn(mid_mfn) != *top_mfn_p);
  239. if (mid_mfn == p2m_mid_missing_mfn) {
  240. /* Separately check the mid mfn level */
  241. unsigned long missing_mfn;
  242. unsigned long mid_mfn_mfn;
  243. mid_mfn = alloc_p2m_page();
  244. if (!mid_mfn)
  245. return false;
  246. p2m_mid_mfn_init(mid_mfn);
  247. missing_mfn = virt_to_mfn(p2m_mid_missing_mfn);
  248. mid_mfn_mfn = virt_to_mfn(mid_mfn);
  249. if (cmpxchg(top_mfn_p, missing_mfn, mid_mfn_mfn) != missing_mfn)
  250. free_p2m_page(mid_mfn);
  251. else
  252. p2m_top_mfn_p[topidx] = mid_mfn;
  253. }
  254. if (p2m_top[topidx][mididx] == p2m_missing) {
  255. /* p2m leaf page is missing */
  256. unsigned long *p2m;
  257. p2m = alloc_p2m_page();
  258. if (!p2m)
  259. return false;
  260. p2m_init(p2m);
  261. if (cmpxchg(&mid[mididx], p2m_missing, p2m) != p2m_missing)
  262. free_p2m_page(p2m);
  263. else
  264. mid_mfn[mididx] = virt_to_mfn(p2m);
  265. }
  266. return true;
  267. }
  268. /* Try to install p2m mapping; fail if intermediate bits missing */
  269. bool __set_phys_to_machine(unsigned long pfn, unsigned long mfn)
  270. {
  271. unsigned topidx, mididx, idx;
  272. if (unlikely(pfn >= MAX_P2M_PFN)) {
  273. BUG_ON(mfn != INVALID_P2M_ENTRY);
  274. return true;
  275. }
  276. topidx = p2m_top_index(pfn);
  277. mididx = p2m_mid_index(pfn);
  278. idx = p2m_index(pfn);
  279. if (p2m_top[topidx][mididx] == p2m_missing)
  280. return mfn == INVALID_P2M_ENTRY;
  281. p2m_top[topidx][mididx][idx] = mfn;
  282. return true;
  283. }
  284. bool set_phys_to_machine(unsigned long pfn, unsigned long mfn)
  285. {
  286. if (unlikely(xen_feature(XENFEAT_auto_translated_physmap))) {
  287. BUG_ON(pfn != mfn && mfn != INVALID_P2M_ENTRY);
  288. return true;
  289. }
  290. if (unlikely(!__set_phys_to_machine(pfn, mfn))) {
  291. if (!alloc_p2m(pfn))
  292. return false;
  293. if (!__set_phys_to_machine(pfn, mfn))
  294. return false;
  295. }
  296. return true;
  297. }