p2m.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814
  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 logical flat p2m table is mapped to a linear kernel memory area.
  7. * For accesses by Xen a three-level tree linked via mfns only is set up to
  8. * allow the address space to be sparse.
  9. *
  10. * Xen
  11. * |
  12. * p2m_top_mfn
  13. * / \
  14. * p2m_mid_mfn p2m_mid_mfn
  15. * / /
  16. * p2m p2m p2m ...
  17. *
  18. * The p2m_mid_mfn pages are mapped by p2m_top_mfn_p.
  19. *
  20. * The p2m_top_mfn level is limited to 1 page, so the maximum representable
  21. * pseudo-physical address space is:
  22. * P2M_TOP_PER_PAGE * P2M_MID_PER_PAGE * P2M_PER_PAGE pages
  23. *
  24. * P2M_PER_PAGE depends on the architecture, as a mfn is always
  25. * unsigned long (8 bytes on 64-bit, 4 bytes on 32), leading to
  26. * 512 and 1024 entries respectively.
  27. *
  28. * In short, these structures contain the Machine Frame Number (MFN) of the PFN.
  29. *
  30. * However not all entries are filled with MFNs. Specifically for all other
  31. * leaf entries, or for the top root, or middle one, for which there is a void
  32. * entry, we assume it is "missing". So (for example)
  33. * pfn_to_mfn(0x90909090)=INVALID_P2M_ENTRY.
  34. * We have a dedicated page p2m_missing with all entries being
  35. * INVALID_P2M_ENTRY. This page may be referenced multiple times in the p2m
  36. * list/tree in case there are multiple areas with P2M_PER_PAGE invalid pfns.
  37. *
  38. * We also have the possibility of setting 1-1 mappings on certain regions, so
  39. * that:
  40. * pfn_to_mfn(0xc0000)=0xc0000
  41. *
  42. * The benefit of this is, that we can assume for non-RAM regions (think
  43. * PCI BARs, or ACPI spaces), we can create mappings easily because we
  44. * get the PFN value to match the MFN.
  45. *
  46. * For this to work efficiently we have one new page p2m_identity. All entries
  47. * in p2m_identity are set to INVALID_P2M_ENTRY type (Xen toolstack only
  48. * recognizes that and MFNs, no other fancy value).
  49. *
  50. * On lookup we spot that the entry points to p2m_identity and return the
  51. * identity value instead of dereferencing and returning INVALID_P2M_ENTRY.
  52. * If the entry points to an allocated page, we just proceed as before and
  53. * return the PFN. If the PFN has IDENTITY_FRAME_BIT set we unmask that in
  54. * appropriate functions (pfn_to_mfn).
  55. *
  56. * The reason for having the IDENTITY_FRAME_BIT instead of just returning the
  57. * PFN is that we could find ourselves where pfn_to_mfn(pfn)==pfn for a
  58. * non-identity pfn. To protect ourselves against we elect to set (and get) the
  59. * IDENTITY_FRAME_BIT on all identity mapped PFNs.
  60. */
  61. #include <linux/init.h>
  62. #include <linux/module.h>
  63. #include <linux/list.h>
  64. #include <linux/hash.h>
  65. #include <linux/sched.h>
  66. #include <linux/seq_file.h>
  67. #include <linux/bootmem.h>
  68. #include <linux/slab.h>
  69. #include <linux/vmalloc.h>
  70. #include <asm/cache.h>
  71. #include <asm/setup.h>
  72. #include <asm/uaccess.h>
  73. #include <asm/xen/page.h>
  74. #include <asm/xen/hypercall.h>
  75. #include <asm/xen/hypervisor.h>
  76. #include <xen/balloon.h>
  77. #include <xen/grant_table.h>
  78. #include "multicalls.h"
  79. #include "xen-ops.h"
  80. #define P2M_MID_PER_PAGE (PAGE_SIZE / sizeof(unsigned long *))
  81. #define P2M_TOP_PER_PAGE (PAGE_SIZE / sizeof(unsigned long **))
  82. #define MAX_P2M_PFN (P2M_TOP_PER_PAGE * P2M_MID_PER_PAGE * P2M_PER_PAGE)
  83. #define PMDS_PER_MID_PAGE (P2M_MID_PER_PAGE / PTRS_PER_PTE)
  84. unsigned long *xen_p2m_addr __read_mostly;
  85. EXPORT_SYMBOL_GPL(xen_p2m_addr);
  86. unsigned long xen_p2m_size __read_mostly;
  87. EXPORT_SYMBOL_GPL(xen_p2m_size);
  88. unsigned long xen_max_p2m_pfn __read_mostly;
  89. EXPORT_SYMBOL_GPL(xen_max_p2m_pfn);
  90. #ifdef CONFIG_XEN_BALLOON_MEMORY_HOTPLUG_LIMIT
  91. #define P2M_LIMIT CONFIG_XEN_BALLOON_MEMORY_HOTPLUG_LIMIT
  92. #else
  93. #define P2M_LIMIT 0
  94. #endif
  95. static DEFINE_SPINLOCK(p2m_update_lock);
  96. static unsigned long *p2m_mid_missing_mfn;
  97. static unsigned long *p2m_top_mfn;
  98. static unsigned long **p2m_top_mfn_p;
  99. static unsigned long *p2m_missing;
  100. static unsigned long *p2m_identity;
  101. static pte_t *p2m_missing_pte;
  102. static pte_t *p2m_identity_pte;
  103. static inline unsigned p2m_top_index(unsigned long pfn)
  104. {
  105. BUG_ON(pfn >= MAX_P2M_PFN);
  106. return pfn / (P2M_MID_PER_PAGE * P2M_PER_PAGE);
  107. }
  108. static inline unsigned p2m_mid_index(unsigned long pfn)
  109. {
  110. return (pfn / P2M_PER_PAGE) % P2M_MID_PER_PAGE;
  111. }
  112. static inline unsigned p2m_index(unsigned long pfn)
  113. {
  114. return pfn % P2M_PER_PAGE;
  115. }
  116. static void p2m_top_mfn_init(unsigned long *top)
  117. {
  118. unsigned i;
  119. for (i = 0; i < P2M_TOP_PER_PAGE; i++)
  120. top[i] = virt_to_mfn(p2m_mid_missing_mfn);
  121. }
  122. static void p2m_top_mfn_p_init(unsigned long **top)
  123. {
  124. unsigned i;
  125. for (i = 0; i < P2M_TOP_PER_PAGE; i++)
  126. top[i] = p2m_mid_missing_mfn;
  127. }
  128. static void p2m_mid_mfn_init(unsigned long *mid, unsigned long *leaf)
  129. {
  130. unsigned i;
  131. for (i = 0; i < P2M_MID_PER_PAGE; i++)
  132. mid[i] = virt_to_mfn(leaf);
  133. }
  134. static void p2m_init(unsigned long *p2m)
  135. {
  136. unsigned i;
  137. for (i = 0; i < P2M_PER_PAGE; i++)
  138. p2m[i] = INVALID_P2M_ENTRY;
  139. }
  140. static void p2m_init_identity(unsigned long *p2m, unsigned long pfn)
  141. {
  142. unsigned i;
  143. for (i = 0; i < P2M_PER_PAGE; i++)
  144. p2m[i] = IDENTITY_FRAME(pfn + i);
  145. }
  146. static void * __ref alloc_p2m_page(void)
  147. {
  148. if (unlikely(!slab_is_available()))
  149. return alloc_bootmem_align(PAGE_SIZE, PAGE_SIZE);
  150. return (void *)__get_free_page(GFP_KERNEL | __GFP_REPEAT);
  151. }
  152. static void __ref free_p2m_page(void *p)
  153. {
  154. if (unlikely(!slab_is_available())) {
  155. free_bootmem((unsigned long)p, PAGE_SIZE);
  156. return;
  157. }
  158. free_page((unsigned long)p);
  159. }
  160. /*
  161. * Build the parallel p2m_top_mfn and p2m_mid_mfn structures
  162. *
  163. * This is called both at boot time, and after resuming from suspend:
  164. * - At boot time we're called rather early, and must use alloc_bootmem*()
  165. * to allocate memory.
  166. *
  167. * - After resume we're called from within stop_machine, but the mfn
  168. * tree should already be completely allocated.
  169. */
  170. void __ref xen_build_mfn_list_list(void)
  171. {
  172. unsigned long pfn, mfn;
  173. pte_t *ptep;
  174. unsigned int level, topidx, mididx;
  175. unsigned long *mid_mfn_p;
  176. if (xen_feature(XENFEAT_auto_translated_physmap) ||
  177. xen_start_info->flags & SIF_VIRT_P2M_4TOOLS)
  178. return;
  179. /* Pre-initialize p2m_top_mfn to be completely missing */
  180. if (p2m_top_mfn == NULL) {
  181. p2m_mid_missing_mfn = alloc_p2m_page();
  182. p2m_mid_mfn_init(p2m_mid_missing_mfn, p2m_missing);
  183. p2m_top_mfn_p = alloc_p2m_page();
  184. p2m_top_mfn_p_init(p2m_top_mfn_p);
  185. p2m_top_mfn = alloc_p2m_page();
  186. p2m_top_mfn_init(p2m_top_mfn);
  187. } else {
  188. /* Reinitialise, mfn's all change after migration */
  189. p2m_mid_mfn_init(p2m_mid_missing_mfn, p2m_missing);
  190. }
  191. for (pfn = 0; pfn < xen_max_p2m_pfn && pfn < MAX_P2M_PFN;
  192. pfn += P2M_PER_PAGE) {
  193. topidx = p2m_top_index(pfn);
  194. mididx = p2m_mid_index(pfn);
  195. mid_mfn_p = p2m_top_mfn_p[topidx];
  196. ptep = lookup_address((unsigned long)(xen_p2m_addr + pfn),
  197. &level);
  198. BUG_ON(!ptep || level != PG_LEVEL_4K);
  199. mfn = pte_mfn(*ptep);
  200. ptep = (pte_t *)((unsigned long)ptep & ~(PAGE_SIZE - 1));
  201. /* Don't bother allocating any mfn mid levels if
  202. * they're just missing, just update the stored mfn,
  203. * since all could have changed over a migrate.
  204. */
  205. if (ptep == p2m_missing_pte || ptep == p2m_identity_pte) {
  206. BUG_ON(mididx);
  207. BUG_ON(mid_mfn_p != p2m_mid_missing_mfn);
  208. p2m_top_mfn[topidx] = virt_to_mfn(p2m_mid_missing_mfn);
  209. pfn += (P2M_MID_PER_PAGE - 1) * P2M_PER_PAGE;
  210. continue;
  211. }
  212. if (mid_mfn_p == p2m_mid_missing_mfn) {
  213. mid_mfn_p = alloc_p2m_page();
  214. p2m_mid_mfn_init(mid_mfn_p, p2m_missing);
  215. p2m_top_mfn_p[topidx] = mid_mfn_p;
  216. }
  217. p2m_top_mfn[topidx] = virt_to_mfn(mid_mfn_p);
  218. mid_mfn_p[mididx] = mfn;
  219. }
  220. }
  221. void xen_setup_mfn_list_list(void)
  222. {
  223. if (xen_feature(XENFEAT_auto_translated_physmap))
  224. return;
  225. BUG_ON(HYPERVISOR_shared_info == &xen_dummy_shared_info);
  226. if (xen_start_info->flags & SIF_VIRT_P2M_4TOOLS)
  227. HYPERVISOR_shared_info->arch.pfn_to_mfn_frame_list_list = ~0UL;
  228. else
  229. HYPERVISOR_shared_info->arch.pfn_to_mfn_frame_list_list =
  230. virt_to_mfn(p2m_top_mfn);
  231. HYPERVISOR_shared_info->arch.max_pfn = xen_max_p2m_pfn;
  232. HYPERVISOR_shared_info->arch.p2m_generation = 0;
  233. HYPERVISOR_shared_info->arch.p2m_vaddr = (unsigned long)xen_p2m_addr;
  234. HYPERVISOR_shared_info->arch.p2m_cr3 =
  235. xen_pfn_to_cr3(virt_to_mfn(swapper_pg_dir));
  236. }
  237. /* Set up p2m_top to point to the domain-builder provided p2m pages */
  238. void __init xen_build_dynamic_phys_to_machine(void)
  239. {
  240. unsigned long pfn;
  241. if (xen_feature(XENFEAT_auto_translated_physmap))
  242. return;
  243. xen_p2m_addr = (unsigned long *)xen_start_info->mfn_list;
  244. xen_p2m_size = ALIGN(xen_start_info->nr_pages, P2M_PER_PAGE);
  245. for (pfn = xen_start_info->nr_pages; pfn < xen_p2m_size; pfn++)
  246. xen_p2m_addr[pfn] = INVALID_P2M_ENTRY;
  247. xen_max_p2m_pfn = xen_p2m_size;
  248. }
  249. #define P2M_TYPE_IDENTITY 0
  250. #define P2M_TYPE_MISSING 1
  251. #define P2M_TYPE_PFN 2
  252. #define P2M_TYPE_UNKNOWN 3
  253. static int xen_p2m_elem_type(unsigned long pfn)
  254. {
  255. unsigned long mfn;
  256. if (pfn >= xen_p2m_size)
  257. return P2M_TYPE_IDENTITY;
  258. mfn = xen_p2m_addr[pfn];
  259. if (mfn == INVALID_P2M_ENTRY)
  260. return P2M_TYPE_MISSING;
  261. if (mfn & IDENTITY_FRAME_BIT)
  262. return P2M_TYPE_IDENTITY;
  263. return P2M_TYPE_PFN;
  264. }
  265. static void __init xen_rebuild_p2m_list(unsigned long *p2m)
  266. {
  267. unsigned int i, chunk;
  268. unsigned long pfn;
  269. unsigned long *mfns;
  270. pte_t *ptep;
  271. pmd_t *pmdp;
  272. int type;
  273. p2m_missing = alloc_p2m_page();
  274. p2m_init(p2m_missing);
  275. p2m_identity = alloc_p2m_page();
  276. p2m_init(p2m_identity);
  277. p2m_missing_pte = alloc_p2m_page();
  278. paravirt_alloc_pte(&init_mm, __pa(p2m_missing_pte) >> PAGE_SHIFT);
  279. p2m_identity_pte = alloc_p2m_page();
  280. paravirt_alloc_pte(&init_mm, __pa(p2m_identity_pte) >> PAGE_SHIFT);
  281. for (i = 0; i < PTRS_PER_PTE; i++) {
  282. set_pte(p2m_missing_pte + i,
  283. pfn_pte(PFN_DOWN(__pa(p2m_missing)), PAGE_KERNEL_RO));
  284. set_pte(p2m_identity_pte + i,
  285. pfn_pte(PFN_DOWN(__pa(p2m_identity)), PAGE_KERNEL_RO));
  286. }
  287. for (pfn = 0; pfn < xen_max_p2m_pfn; pfn += chunk) {
  288. /*
  289. * Try to map missing/identity PMDs or p2m-pages if possible.
  290. * We have to respect the structure of the mfn_list_list
  291. * which will be built just afterwards.
  292. * Chunk size to test is one p2m page if we are in the middle
  293. * of a mfn_list_list mid page and the complete mid page area
  294. * if we are at index 0 of the mid page. Please note that a
  295. * mid page might cover more than one PMD, e.g. on 32 bit PAE
  296. * kernels.
  297. */
  298. chunk = (pfn & (P2M_PER_PAGE * P2M_MID_PER_PAGE - 1)) ?
  299. P2M_PER_PAGE : P2M_PER_PAGE * P2M_MID_PER_PAGE;
  300. type = xen_p2m_elem_type(pfn);
  301. i = 0;
  302. if (type != P2M_TYPE_PFN)
  303. for (i = 1; i < chunk; i++)
  304. if (xen_p2m_elem_type(pfn + i) != type)
  305. break;
  306. if (i < chunk)
  307. /* Reset to minimal chunk size. */
  308. chunk = P2M_PER_PAGE;
  309. if (type == P2M_TYPE_PFN || i < chunk) {
  310. /* Use initial p2m page contents. */
  311. #ifdef CONFIG_X86_64
  312. mfns = alloc_p2m_page();
  313. copy_page(mfns, xen_p2m_addr + pfn);
  314. #else
  315. mfns = xen_p2m_addr + pfn;
  316. #endif
  317. ptep = populate_extra_pte((unsigned long)(p2m + pfn));
  318. set_pte(ptep,
  319. pfn_pte(PFN_DOWN(__pa(mfns)), PAGE_KERNEL));
  320. continue;
  321. }
  322. if (chunk == P2M_PER_PAGE) {
  323. /* Map complete missing or identity p2m-page. */
  324. mfns = (type == P2M_TYPE_MISSING) ?
  325. p2m_missing : p2m_identity;
  326. ptep = populate_extra_pte((unsigned long)(p2m + pfn));
  327. set_pte(ptep,
  328. pfn_pte(PFN_DOWN(__pa(mfns)), PAGE_KERNEL_RO));
  329. continue;
  330. }
  331. /* Complete missing or identity PMD(s) can be mapped. */
  332. ptep = (type == P2M_TYPE_MISSING) ?
  333. p2m_missing_pte : p2m_identity_pte;
  334. for (i = 0; i < PMDS_PER_MID_PAGE; i++) {
  335. pmdp = populate_extra_pmd(
  336. (unsigned long)(p2m + pfn) + i * PMD_SIZE);
  337. set_pmd(pmdp, __pmd(__pa(ptep) | _KERNPG_TABLE));
  338. }
  339. }
  340. }
  341. void __init xen_vmalloc_p2m_tree(void)
  342. {
  343. static struct vm_struct vm;
  344. unsigned long p2m_limit;
  345. p2m_limit = (phys_addr_t)P2M_LIMIT * 1024 * 1024 * 1024 / PAGE_SIZE;
  346. vm.flags = VM_ALLOC;
  347. vm.size = ALIGN(sizeof(unsigned long) * max(xen_max_p2m_pfn, p2m_limit),
  348. PMD_SIZE * PMDS_PER_MID_PAGE);
  349. vm_area_register_early(&vm, PMD_SIZE * PMDS_PER_MID_PAGE);
  350. pr_notice("p2m virtual area at %p, size is %lx\n", vm.addr, vm.size);
  351. xen_max_p2m_pfn = vm.size / sizeof(unsigned long);
  352. xen_rebuild_p2m_list(vm.addr);
  353. xen_p2m_addr = vm.addr;
  354. xen_p2m_size = xen_max_p2m_pfn;
  355. xen_inv_extra_mem();
  356. }
  357. unsigned long get_phys_to_machine(unsigned long pfn)
  358. {
  359. pte_t *ptep;
  360. unsigned int level;
  361. if (unlikely(pfn >= xen_p2m_size)) {
  362. if (pfn < xen_max_p2m_pfn)
  363. return xen_chk_extra_mem(pfn);
  364. return IDENTITY_FRAME(pfn);
  365. }
  366. ptep = lookup_address((unsigned long)(xen_p2m_addr + pfn), &level);
  367. BUG_ON(!ptep || level != PG_LEVEL_4K);
  368. /*
  369. * The INVALID_P2M_ENTRY is filled in both p2m_*identity
  370. * and in p2m_*missing, so returning the INVALID_P2M_ENTRY
  371. * would be wrong.
  372. */
  373. if (pte_pfn(*ptep) == PFN_DOWN(__pa(p2m_identity)))
  374. return IDENTITY_FRAME(pfn);
  375. return xen_p2m_addr[pfn];
  376. }
  377. EXPORT_SYMBOL_GPL(get_phys_to_machine);
  378. /*
  379. * Allocate new pmd(s). It is checked whether the old pmd is still in place.
  380. * If not, nothing is changed. This is okay as the only reason for allocating
  381. * a new pmd is to replace p2m_missing_pte or p2m_identity_pte by a individual
  382. * pmd. In case of PAE/x86-32 there are multiple pmds to allocate!
  383. */
  384. static pte_t *alloc_p2m_pmd(unsigned long addr, pte_t *pte_pg)
  385. {
  386. pte_t *ptechk;
  387. pte_t *pte_newpg[PMDS_PER_MID_PAGE];
  388. pmd_t *pmdp;
  389. unsigned int level;
  390. unsigned long flags;
  391. unsigned long vaddr;
  392. int i;
  393. /* Do all allocations first to bail out in error case. */
  394. for (i = 0; i < PMDS_PER_MID_PAGE; i++) {
  395. pte_newpg[i] = alloc_p2m_page();
  396. if (!pte_newpg[i]) {
  397. for (i--; i >= 0; i--)
  398. free_p2m_page(pte_newpg[i]);
  399. return NULL;
  400. }
  401. }
  402. vaddr = addr & ~(PMD_SIZE * PMDS_PER_MID_PAGE - 1);
  403. for (i = 0; i < PMDS_PER_MID_PAGE; i++) {
  404. copy_page(pte_newpg[i], pte_pg);
  405. paravirt_alloc_pte(&init_mm, __pa(pte_newpg[i]) >> PAGE_SHIFT);
  406. pmdp = lookup_pmd_address(vaddr);
  407. BUG_ON(!pmdp);
  408. spin_lock_irqsave(&p2m_update_lock, flags);
  409. ptechk = lookup_address(vaddr, &level);
  410. if (ptechk == pte_pg) {
  411. HYPERVISOR_shared_info->arch.p2m_generation++;
  412. wmb(); /* Tools are synchronizing via p2m_generation. */
  413. set_pmd(pmdp,
  414. __pmd(__pa(pte_newpg[i]) | _KERNPG_TABLE));
  415. wmb(); /* Tools are synchronizing via p2m_generation. */
  416. HYPERVISOR_shared_info->arch.p2m_generation++;
  417. pte_newpg[i] = NULL;
  418. }
  419. spin_unlock_irqrestore(&p2m_update_lock, flags);
  420. if (pte_newpg[i]) {
  421. paravirt_release_pte(__pa(pte_newpg[i]) >> PAGE_SHIFT);
  422. free_p2m_page(pte_newpg[i]);
  423. }
  424. vaddr += PMD_SIZE;
  425. }
  426. return lookup_address(addr, &level);
  427. }
  428. /*
  429. * Fully allocate the p2m structure for a given pfn. We need to check
  430. * that both the top and mid levels are allocated, and make sure the
  431. * parallel mfn tree is kept in sync. We may race with other cpus, so
  432. * the new pages are installed with cmpxchg; if we lose the race then
  433. * simply free the page we allocated and use the one that's there.
  434. */
  435. static bool alloc_p2m(unsigned long pfn)
  436. {
  437. unsigned topidx;
  438. unsigned long *top_mfn_p, *mid_mfn;
  439. pte_t *ptep, *pte_pg;
  440. unsigned int level;
  441. unsigned long flags;
  442. unsigned long addr = (unsigned long)(xen_p2m_addr + pfn);
  443. unsigned long p2m_pfn;
  444. ptep = lookup_address(addr, &level);
  445. BUG_ON(!ptep || level != PG_LEVEL_4K);
  446. pte_pg = (pte_t *)((unsigned long)ptep & ~(PAGE_SIZE - 1));
  447. if (pte_pg == p2m_missing_pte || pte_pg == p2m_identity_pte) {
  448. /* PMD level is missing, allocate a new one */
  449. ptep = alloc_p2m_pmd(addr, pte_pg);
  450. if (!ptep)
  451. return false;
  452. }
  453. if (p2m_top_mfn && pfn < MAX_P2M_PFN) {
  454. topidx = p2m_top_index(pfn);
  455. top_mfn_p = &p2m_top_mfn[topidx];
  456. mid_mfn = ACCESS_ONCE(p2m_top_mfn_p[topidx]);
  457. BUG_ON(virt_to_mfn(mid_mfn) != *top_mfn_p);
  458. if (mid_mfn == p2m_mid_missing_mfn) {
  459. /* Separately check the mid mfn level */
  460. unsigned long missing_mfn;
  461. unsigned long mid_mfn_mfn;
  462. unsigned long old_mfn;
  463. mid_mfn = alloc_p2m_page();
  464. if (!mid_mfn)
  465. return false;
  466. p2m_mid_mfn_init(mid_mfn, p2m_missing);
  467. missing_mfn = virt_to_mfn(p2m_mid_missing_mfn);
  468. mid_mfn_mfn = virt_to_mfn(mid_mfn);
  469. old_mfn = cmpxchg(top_mfn_p, missing_mfn, mid_mfn_mfn);
  470. if (old_mfn != missing_mfn) {
  471. free_p2m_page(mid_mfn);
  472. mid_mfn = mfn_to_virt(old_mfn);
  473. } else {
  474. p2m_top_mfn_p[topidx] = mid_mfn;
  475. }
  476. }
  477. } else {
  478. mid_mfn = NULL;
  479. }
  480. p2m_pfn = pte_pfn(READ_ONCE(*ptep));
  481. if (p2m_pfn == PFN_DOWN(__pa(p2m_identity)) ||
  482. p2m_pfn == PFN_DOWN(__pa(p2m_missing))) {
  483. /* p2m leaf page is missing */
  484. unsigned long *p2m;
  485. p2m = alloc_p2m_page();
  486. if (!p2m)
  487. return false;
  488. if (p2m_pfn == PFN_DOWN(__pa(p2m_missing)))
  489. p2m_init(p2m);
  490. else
  491. p2m_init_identity(p2m, pfn & ~(P2M_PER_PAGE - 1));
  492. spin_lock_irqsave(&p2m_update_lock, flags);
  493. if (pte_pfn(*ptep) == p2m_pfn) {
  494. HYPERVISOR_shared_info->arch.p2m_generation++;
  495. wmb(); /* Tools are synchronizing via p2m_generation. */
  496. set_pte(ptep,
  497. pfn_pte(PFN_DOWN(__pa(p2m)), PAGE_KERNEL));
  498. wmb(); /* Tools are synchronizing via p2m_generation. */
  499. HYPERVISOR_shared_info->arch.p2m_generation++;
  500. if (mid_mfn)
  501. mid_mfn[p2m_mid_index(pfn)] = virt_to_mfn(p2m);
  502. p2m = NULL;
  503. }
  504. spin_unlock_irqrestore(&p2m_update_lock, flags);
  505. if (p2m)
  506. free_p2m_page(p2m);
  507. }
  508. return true;
  509. }
  510. unsigned long __init set_phys_range_identity(unsigned long pfn_s,
  511. unsigned long pfn_e)
  512. {
  513. unsigned long pfn;
  514. if (unlikely(pfn_s >= xen_p2m_size))
  515. return 0;
  516. if (unlikely(xen_feature(XENFEAT_auto_translated_physmap)))
  517. return pfn_e - pfn_s;
  518. if (pfn_s > pfn_e)
  519. return 0;
  520. if (pfn_e > xen_p2m_size)
  521. pfn_e = xen_p2m_size;
  522. for (pfn = pfn_s; pfn < pfn_e; pfn++)
  523. xen_p2m_addr[pfn] = IDENTITY_FRAME(pfn);
  524. return pfn - pfn_s;
  525. }
  526. bool __set_phys_to_machine(unsigned long pfn, unsigned long mfn)
  527. {
  528. pte_t *ptep;
  529. unsigned int level;
  530. /* don't track P2M changes in autotranslate guests */
  531. if (unlikely(xen_feature(XENFEAT_auto_translated_physmap)))
  532. return true;
  533. if (unlikely(pfn >= xen_p2m_size)) {
  534. BUG_ON(mfn != INVALID_P2M_ENTRY);
  535. return true;
  536. }
  537. /*
  538. * The interface requires atomic updates on p2m elements.
  539. * xen_safe_write_ulong() is using __put_user which does an atomic
  540. * store via asm().
  541. */
  542. if (likely(!xen_safe_write_ulong(xen_p2m_addr + pfn, mfn)))
  543. return true;
  544. ptep = lookup_address((unsigned long)(xen_p2m_addr + pfn), &level);
  545. BUG_ON(!ptep || level != PG_LEVEL_4K);
  546. if (pte_pfn(*ptep) == PFN_DOWN(__pa(p2m_missing)))
  547. return mfn == INVALID_P2M_ENTRY;
  548. if (pte_pfn(*ptep) == PFN_DOWN(__pa(p2m_identity)))
  549. return mfn == IDENTITY_FRAME(pfn);
  550. return false;
  551. }
  552. bool set_phys_to_machine(unsigned long pfn, unsigned long mfn)
  553. {
  554. if (unlikely(!__set_phys_to_machine(pfn, mfn))) {
  555. if (!alloc_p2m(pfn))
  556. return false;
  557. return __set_phys_to_machine(pfn, mfn);
  558. }
  559. return true;
  560. }
  561. int set_foreign_p2m_mapping(struct gnttab_map_grant_ref *map_ops,
  562. struct gnttab_map_grant_ref *kmap_ops,
  563. struct page **pages, unsigned int count)
  564. {
  565. int i, ret = 0;
  566. pte_t *pte;
  567. if (xen_feature(XENFEAT_auto_translated_physmap))
  568. return 0;
  569. if (kmap_ops) {
  570. ret = HYPERVISOR_grant_table_op(GNTTABOP_map_grant_ref,
  571. kmap_ops, count);
  572. if (ret)
  573. goto out;
  574. }
  575. for (i = 0; i < count; i++) {
  576. unsigned long mfn, pfn;
  577. /* Do not add to override if the map failed. */
  578. if (map_ops[i].status)
  579. continue;
  580. if (map_ops[i].flags & GNTMAP_contains_pte) {
  581. pte = (pte_t *)(mfn_to_virt(PFN_DOWN(map_ops[i].host_addr)) +
  582. (map_ops[i].host_addr & ~PAGE_MASK));
  583. mfn = pte_mfn(*pte);
  584. } else {
  585. mfn = PFN_DOWN(map_ops[i].dev_bus_addr);
  586. }
  587. pfn = page_to_pfn(pages[i]);
  588. WARN(pfn_to_mfn(pfn) != INVALID_P2M_ENTRY, "page must be ballooned");
  589. if (unlikely(!set_phys_to_machine(pfn, FOREIGN_FRAME(mfn)))) {
  590. ret = -ENOMEM;
  591. goto out;
  592. }
  593. }
  594. out:
  595. return ret;
  596. }
  597. EXPORT_SYMBOL_GPL(set_foreign_p2m_mapping);
  598. int clear_foreign_p2m_mapping(struct gnttab_unmap_grant_ref *unmap_ops,
  599. struct gnttab_unmap_grant_ref *kunmap_ops,
  600. struct page **pages, unsigned int count)
  601. {
  602. int i, ret = 0;
  603. if (xen_feature(XENFEAT_auto_translated_physmap))
  604. return 0;
  605. for (i = 0; i < count; i++) {
  606. unsigned long mfn = __pfn_to_mfn(page_to_pfn(pages[i]));
  607. unsigned long pfn = page_to_pfn(pages[i]);
  608. if (mfn == INVALID_P2M_ENTRY || !(mfn & FOREIGN_FRAME_BIT)) {
  609. ret = -EINVAL;
  610. goto out;
  611. }
  612. set_phys_to_machine(pfn, INVALID_P2M_ENTRY);
  613. }
  614. if (kunmap_ops)
  615. ret = HYPERVISOR_grant_table_op(GNTTABOP_unmap_grant_ref,
  616. kunmap_ops, count);
  617. out:
  618. return ret;
  619. }
  620. EXPORT_SYMBOL_GPL(clear_foreign_p2m_mapping);
  621. #ifdef CONFIG_XEN_DEBUG_FS
  622. #include <linux/debugfs.h>
  623. #include "debugfs.h"
  624. static int p2m_dump_show(struct seq_file *m, void *v)
  625. {
  626. static const char * const type_name[] = {
  627. [P2M_TYPE_IDENTITY] = "identity",
  628. [P2M_TYPE_MISSING] = "missing",
  629. [P2M_TYPE_PFN] = "pfn",
  630. [P2M_TYPE_UNKNOWN] = "abnormal"};
  631. unsigned long pfn, first_pfn;
  632. int type, prev_type;
  633. prev_type = xen_p2m_elem_type(0);
  634. first_pfn = 0;
  635. for (pfn = 0; pfn < xen_p2m_size; pfn++) {
  636. type = xen_p2m_elem_type(pfn);
  637. if (type != prev_type) {
  638. seq_printf(m, " [0x%lx->0x%lx] %s\n", first_pfn, pfn,
  639. type_name[prev_type]);
  640. prev_type = type;
  641. first_pfn = pfn;
  642. }
  643. }
  644. seq_printf(m, " [0x%lx->0x%lx] %s\n", first_pfn, pfn,
  645. type_name[prev_type]);
  646. return 0;
  647. }
  648. static int p2m_dump_open(struct inode *inode, struct file *filp)
  649. {
  650. return single_open(filp, p2m_dump_show, NULL);
  651. }
  652. static const struct file_operations p2m_dump_fops = {
  653. .open = p2m_dump_open,
  654. .read = seq_read,
  655. .llseek = seq_lseek,
  656. .release = single_release,
  657. };
  658. static struct dentry *d_mmu_debug;
  659. static int __init xen_p2m_debugfs(void)
  660. {
  661. struct dentry *d_xen = xen_init_debugfs();
  662. if (d_xen == NULL)
  663. return -ENOMEM;
  664. d_mmu_debug = debugfs_create_dir("mmu", d_xen);
  665. debugfs_create_file("p2m", 0600, d_mmu_debug, NULL, &p2m_dump_fops);
  666. return 0;
  667. }
  668. fs_initcall(xen_p2m_debugfs);
  669. #endif /* CONFIG_XEN_DEBUG_FS */