p2m.c 23 KB

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