p2m.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027
  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 <asm/cache.h>
  70. #include <asm/setup.h>
  71. #include <asm/uaccess.h>
  72. #include <asm/xen/page.h>
  73. #include <asm/xen/hypercall.h>
  74. #include <asm/xen/hypervisor.h>
  75. #include <xen/balloon.h>
  76. #include <xen/grant_table.h>
  77. #include "p2m.h"
  78. #include "multicalls.h"
  79. #include "xen-ops.h"
  80. #define PMDS_PER_MID_PAGE (P2M_MID_PER_PAGE / PTRS_PER_PTE)
  81. static void __init m2p_override_init(void);
  82. unsigned long *xen_p2m_addr __read_mostly;
  83. EXPORT_SYMBOL_GPL(xen_p2m_addr);
  84. unsigned long xen_p2m_size __read_mostly;
  85. EXPORT_SYMBOL_GPL(xen_p2m_size);
  86. unsigned long xen_max_p2m_pfn __read_mostly;
  87. EXPORT_SYMBOL_GPL(xen_max_p2m_pfn);
  88. static DEFINE_SPINLOCK(p2m_update_lock);
  89. static unsigned long *p2m_mid_missing_mfn;
  90. static unsigned long *p2m_top_mfn;
  91. static unsigned long **p2m_top_mfn_p;
  92. static unsigned long *p2m_missing;
  93. static unsigned long *p2m_identity;
  94. static pte_t *p2m_missing_pte;
  95. static pte_t *p2m_identity_pte;
  96. static inline unsigned p2m_top_index(unsigned long pfn)
  97. {
  98. BUG_ON(pfn >= MAX_P2M_PFN);
  99. return pfn / (P2M_MID_PER_PAGE * P2M_PER_PAGE);
  100. }
  101. static inline unsigned p2m_mid_index(unsigned long pfn)
  102. {
  103. return (pfn / P2M_PER_PAGE) % P2M_MID_PER_PAGE;
  104. }
  105. static inline unsigned p2m_index(unsigned long pfn)
  106. {
  107. return pfn % P2M_PER_PAGE;
  108. }
  109. static void p2m_top_mfn_init(unsigned long *top)
  110. {
  111. unsigned i;
  112. for (i = 0; i < P2M_TOP_PER_PAGE; i++)
  113. top[i] = virt_to_mfn(p2m_mid_missing_mfn);
  114. }
  115. static void p2m_top_mfn_p_init(unsigned long **top)
  116. {
  117. unsigned i;
  118. for (i = 0; i < P2M_TOP_PER_PAGE; i++)
  119. top[i] = p2m_mid_missing_mfn;
  120. }
  121. static void p2m_mid_mfn_init(unsigned long *mid, unsigned long *leaf)
  122. {
  123. unsigned i;
  124. for (i = 0; i < P2M_MID_PER_PAGE; i++)
  125. mid[i] = virt_to_mfn(leaf);
  126. }
  127. static void p2m_init(unsigned long *p2m)
  128. {
  129. unsigned i;
  130. for (i = 0; i < P2M_PER_PAGE; i++)
  131. p2m[i] = INVALID_P2M_ENTRY;
  132. }
  133. static void p2m_init_identity(unsigned long *p2m, unsigned long pfn)
  134. {
  135. unsigned i;
  136. for (i = 0; i < P2M_PER_PAGE; i++)
  137. p2m[i] = IDENTITY_FRAME(pfn + i);
  138. }
  139. static void * __ref alloc_p2m_page(void)
  140. {
  141. if (unlikely(!slab_is_available()))
  142. return alloc_bootmem_align(PAGE_SIZE, PAGE_SIZE);
  143. return (void *)__get_free_page(GFP_KERNEL | __GFP_REPEAT);
  144. }
  145. /* Only to be called in case of a race for a page just allocated! */
  146. static void free_p2m_page(void *p)
  147. {
  148. BUG_ON(!slab_is_available());
  149. free_page((unsigned long)p);
  150. }
  151. /*
  152. * Build the parallel p2m_top_mfn and p2m_mid_mfn structures
  153. *
  154. * This is called both at boot time, and after resuming from suspend:
  155. * - At boot time we're called rather early, and must use alloc_bootmem*()
  156. * to allocate memory.
  157. *
  158. * - After resume we're called from within stop_machine, but the mfn
  159. * tree should already be completely allocated.
  160. */
  161. void __ref xen_build_mfn_list_list(void)
  162. {
  163. unsigned long pfn, mfn;
  164. pte_t *ptep;
  165. unsigned int level, topidx, mididx;
  166. unsigned long *mid_mfn_p;
  167. if (xen_feature(XENFEAT_auto_translated_physmap))
  168. return;
  169. /* Pre-initialize p2m_top_mfn to be completely missing */
  170. if (p2m_top_mfn == NULL) {
  171. p2m_mid_missing_mfn = alloc_p2m_page();
  172. p2m_mid_mfn_init(p2m_mid_missing_mfn, p2m_missing);
  173. p2m_top_mfn_p = alloc_p2m_page();
  174. p2m_top_mfn_p_init(p2m_top_mfn_p);
  175. p2m_top_mfn = alloc_p2m_page();
  176. p2m_top_mfn_init(p2m_top_mfn);
  177. } else {
  178. /* Reinitialise, mfn's all change after migration */
  179. p2m_mid_mfn_init(p2m_mid_missing_mfn, p2m_missing);
  180. }
  181. for (pfn = 0; pfn < xen_max_p2m_pfn && pfn < MAX_P2M_PFN;
  182. pfn += P2M_PER_PAGE) {
  183. topidx = p2m_top_index(pfn);
  184. mididx = p2m_mid_index(pfn);
  185. mid_mfn_p = p2m_top_mfn_p[topidx];
  186. ptep = lookup_address((unsigned long)(xen_p2m_addr + pfn),
  187. &level);
  188. BUG_ON(!ptep || level != PG_LEVEL_4K);
  189. mfn = pte_mfn(*ptep);
  190. ptep = (pte_t *)((unsigned long)ptep & ~(PAGE_SIZE - 1));
  191. /* Don't bother allocating any mfn mid levels if
  192. * they're just missing, just update the stored mfn,
  193. * since all could have changed over a migrate.
  194. */
  195. if (ptep == p2m_missing_pte || ptep == p2m_identity_pte) {
  196. BUG_ON(mididx);
  197. BUG_ON(mid_mfn_p != p2m_mid_missing_mfn);
  198. p2m_top_mfn[topidx] = virt_to_mfn(p2m_mid_missing_mfn);
  199. pfn += (P2M_MID_PER_PAGE - 1) * P2M_PER_PAGE;
  200. continue;
  201. }
  202. if (mid_mfn_p == p2m_mid_missing_mfn) {
  203. mid_mfn_p = alloc_p2m_page();
  204. p2m_mid_mfn_init(mid_mfn_p, p2m_missing);
  205. p2m_top_mfn_p[topidx] = mid_mfn_p;
  206. }
  207. p2m_top_mfn[topidx] = virt_to_mfn(mid_mfn_p);
  208. mid_mfn_p[mididx] = mfn;
  209. }
  210. }
  211. void xen_setup_mfn_list_list(void)
  212. {
  213. if (xen_feature(XENFEAT_auto_translated_physmap))
  214. return;
  215. BUG_ON(HYPERVISOR_shared_info == &xen_dummy_shared_info);
  216. HYPERVISOR_shared_info->arch.pfn_to_mfn_frame_list_list =
  217. virt_to_mfn(p2m_top_mfn);
  218. HYPERVISOR_shared_info->arch.max_pfn = xen_max_p2m_pfn;
  219. }
  220. /* Set up p2m_top to point to the domain-builder provided p2m pages */
  221. void __init xen_build_dynamic_phys_to_machine(void)
  222. {
  223. unsigned long pfn;
  224. if (xen_feature(XENFEAT_auto_translated_physmap))
  225. return;
  226. xen_p2m_addr = (unsigned long *)xen_start_info->mfn_list;
  227. xen_p2m_size = ALIGN(xen_start_info->nr_pages, P2M_PER_PAGE);
  228. for (pfn = xen_start_info->nr_pages; pfn < xen_p2m_size; pfn++)
  229. xen_p2m_addr[pfn] = INVALID_P2M_ENTRY;
  230. xen_max_p2m_pfn = xen_p2m_size;
  231. }
  232. #define P2M_TYPE_IDENTITY 0
  233. #define P2M_TYPE_MISSING 1
  234. #define P2M_TYPE_PFN 2
  235. #define P2M_TYPE_UNKNOWN 3
  236. static int xen_p2m_elem_type(unsigned long pfn)
  237. {
  238. unsigned long mfn;
  239. if (pfn >= xen_p2m_size)
  240. return P2M_TYPE_IDENTITY;
  241. mfn = xen_p2m_addr[pfn];
  242. if (mfn == INVALID_P2M_ENTRY)
  243. return P2M_TYPE_MISSING;
  244. if (mfn & IDENTITY_FRAME_BIT)
  245. return P2M_TYPE_IDENTITY;
  246. return P2M_TYPE_PFN;
  247. }
  248. static void __init xen_rebuild_p2m_list(unsigned long *p2m)
  249. {
  250. unsigned int i, chunk;
  251. unsigned long pfn;
  252. unsigned long *mfns;
  253. pte_t *ptep;
  254. pmd_t *pmdp;
  255. int type;
  256. p2m_missing = alloc_p2m_page();
  257. p2m_init(p2m_missing);
  258. p2m_identity = alloc_p2m_page();
  259. p2m_init(p2m_identity);
  260. p2m_missing_pte = alloc_p2m_page();
  261. paravirt_alloc_pte(&init_mm, __pa(p2m_missing_pte) >> PAGE_SHIFT);
  262. p2m_identity_pte = alloc_p2m_page();
  263. paravirt_alloc_pte(&init_mm, __pa(p2m_identity_pte) >> PAGE_SHIFT);
  264. for (i = 0; i < PTRS_PER_PTE; i++) {
  265. set_pte(p2m_missing_pte + i,
  266. pfn_pte(PFN_DOWN(__pa(p2m_missing)), PAGE_KERNEL_RO));
  267. set_pte(p2m_identity_pte + i,
  268. pfn_pte(PFN_DOWN(__pa(p2m_identity)), PAGE_KERNEL_RO));
  269. }
  270. for (pfn = 0; pfn < xen_max_p2m_pfn; pfn += chunk) {
  271. /*
  272. * Try to map missing/identity PMDs or p2m-pages if possible.
  273. * We have to respect the structure of the mfn_list_list
  274. * which will be built just afterwards.
  275. * Chunk size to test is one p2m page if we are in the middle
  276. * of a mfn_list_list mid page and the complete mid page area
  277. * if we are at index 0 of the mid page. Please note that a
  278. * mid page might cover more than one PMD, e.g. on 32 bit PAE
  279. * kernels.
  280. */
  281. chunk = (pfn & (P2M_PER_PAGE * P2M_MID_PER_PAGE - 1)) ?
  282. P2M_PER_PAGE : P2M_PER_PAGE * P2M_MID_PER_PAGE;
  283. type = xen_p2m_elem_type(pfn);
  284. i = 0;
  285. if (type != P2M_TYPE_PFN)
  286. for (i = 1; i < chunk; i++)
  287. if (xen_p2m_elem_type(pfn + i) != type)
  288. break;
  289. if (i < chunk)
  290. /* Reset to minimal chunk size. */
  291. chunk = P2M_PER_PAGE;
  292. if (type == P2M_TYPE_PFN || i < chunk) {
  293. /* Use initial p2m page contents. */
  294. #ifdef CONFIG_X86_64
  295. mfns = alloc_p2m_page();
  296. copy_page(mfns, xen_p2m_addr + pfn);
  297. #else
  298. mfns = xen_p2m_addr + pfn;
  299. #endif
  300. ptep = populate_extra_pte((unsigned long)(p2m + pfn));
  301. set_pte(ptep,
  302. pfn_pte(PFN_DOWN(__pa(mfns)), PAGE_KERNEL));
  303. continue;
  304. }
  305. if (chunk == P2M_PER_PAGE) {
  306. /* Map complete missing or identity p2m-page. */
  307. mfns = (type == P2M_TYPE_MISSING) ?
  308. p2m_missing : p2m_identity;
  309. ptep = populate_extra_pte((unsigned long)(p2m + pfn));
  310. set_pte(ptep,
  311. pfn_pte(PFN_DOWN(__pa(mfns)), PAGE_KERNEL_RO));
  312. continue;
  313. }
  314. /* Complete missing or identity PMD(s) can be mapped. */
  315. ptep = (type == P2M_TYPE_MISSING) ?
  316. p2m_missing_pte : p2m_identity_pte;
  317. for (i = 0; i < PMDS_PER_MID_PAGE; i++) {
  318. pmdp = populate_extra_pmd(
  319. (unsigned long)(p2m + pfn + i * PTRS_PER_PTE));
  320. set_pmd(pmdp, __pmd(__pa(ptep) | _KERNPG_TABLE));
  321. }
  322. }
  323. }
  324. void __init xen_vmalloc_p2m_tree(void)
  325. {
  326. static struct vm_struct vm;
  327. vm.flags = VM_ALLOC;
  328. vm.size = ALIGN(sizeof(unsigned long) * xen_max_p2m_pfn,
  329. PMD_SIZE * PMDS_PER_MID_PAGE);
  330. vm_area_register_early(&vm, PMD_SIZE * PMDS_PER_MID_PAGE);
  331. pr_notice("p2m virtual area at %p, size is %lx\n", vm.addr, vm.size);
  332. xen_max_p2m_pfn = vm.size / sizeof(unsigned long);
  333. xen_rebuild_p2m_list(vm.addr);
  334. xen_p2m_addr = vm.addr;
  335. xen_p2m_size = xen_max_p2m_pfn;
  336. xen_inv_extra_mem();
  337. m2p_override_init();
  338. }
  339. unsigned long get_phys_to_machine(unsigned long pfn)
  340. {
  341. pte_t *ptep;
  342. unsigned int level;
  343. if (unlikely(pfn >= xen_p2m_size)) {
  344. if (pfn < xen_max_p2m_pfn)
  345. return xen_chk_extra_mem(pfn);
  346. return IDENTITY_FRAME(pfn);
  347. }
  348. ptep = lookup_address((unsigned long)(xen_p2m_addr + pfn), &level);
  349. BUG_ON(!ptep || level != PG_LEVEL_4K);
  350. /*
  351. * The INVALID_P2M_ENTRY is filled in both p2m_*identity
  352. * and in p2m_*missing, so returning the INVALID_P2M_ENTRY
  353. * would be wrong.
  354. */
  355. if (pte_pfn(*ptep) == PFN_DOWN(__pa(p2m_identity)))
  356. return IDENTITY_FRAME(pfn);
  357. return xen_p2m_addr[pfn];
  358. }
  359. EXPORT_SYMBOL_GPL(get_phys_to_machine);
  360. /*
  361. * Allocate new pmd(s). It is checked whether the old pmd is still in place.
  362. * If not, nothing is changed. This is okay as the only reason for allocating
  363. * a new pmd is to replace p2m_missing_pte or p2m_identity_pte by a individual
  364. * pmd. In case of PAE/x86-32 there are multiple pmds to allocate!
  365. */
  366. static pte_t *alloc_p2m_pmd(unsigned long addr, pte_t *ptep, pte_t *pte_pg)
  367. {
  368. pte_t *ptechk;
  369. pte_t *pteret = ptep;
  370. pte_t *pte_newpg[PMDS_PER_MID_PAGE];
  371. pmd_t *pmdp;
  372. unsigned int level;
  373. unsigned long flags;
  374. unsigned long vaddr;
  375. int i;
  376. /* Do all allocations first to bail out in error case. */
  377. for (i = 0; i < PMDS_PER_MID_PAGE; i++) {
  378. pte_newpg[i] = alloc_p2m_page();
  379. if (!pte_newpg[i]) {
  380. for (i--; i >= 0; i--)
  381. free_p2m_page(pte_newpg[i]);
  382. return NULL;
  383. }
  384. }
  385. vaddr = addr & ~(PMD_SIZE * PMDS_PER_MID_PAGE - 1);
  386. for (i = 0; i < PMDS_PER_MID_PAGE; i++) {
  387. copy_page(pte_newpg[i], pte_pg);
  388. paravirt_alloc_pte(&init_mm, __pa(pte_newpg[i]) >> PAGE_SHIFT);
  389. pmdp = lookup_pmd_address(vaddr);
  390. BUG_ON(!pmdp);
  391. spin_lock_irqsave(&p2m_update_lock, flags);
  392. ptechk = lookup_address(vaddr, &level);
  393. if (ptechk == pte_pg) {
  394. set_pmd(pmdp,
  395. __pmd(__pa(pte_newpg[i]) | _KERNPG_TABLE));
  396. if (vaddr == (addr & ~(PMD_SIZE - 1)))
  397. pteret = pte_offset_kernel(pmdp, addr);
  398. pte_newpg[i] = NULL;
  399. }
  400. spin_unlock_irqrestore(&p2m_update_lock, flags);
  401. if (pte_newpg[i]) {
  402. paravirt_release_pte(__pa(pte_newpg[i]) >> PAGE_SHIFT);
  403. free_p2m_page(pte_newpg[i]);
  404. }
  405. vaddr += PMD_SIZE;
  406. }
  407. return pteret;
  408. }
  409. /*
  410. * Fully allocate the p2m structure for a given pfn. We need to check
  411. * that both the top and mid levels are allocated, and make sure the
  412. * parallel mfn tree is kept in sync. We may race with other cpus, so
  413. * the new pages are installed with cmpxchg; if we lose the race then
  414. * simply free the page we allocated and use the one that's there.
  415. */
  416. static bool alloc_p2m(unsigned long pfn)
  417. {
  418. unsigned topidx, mididx;
  419. unsigned long *top_mfn_p, *mid_mfn;
  420. pte_t *ptep, *pte_pg;
  421. unsigned int level;
  422. unsigned long flags;
  423. unsigned long addr = (unsigned long)(xen_p2m_addr + pfn);
  424. unsigned long p2m_pfn;
  425. topidx = p2m_top_index(pfn);
  426. mididx = p2m_mid_index(pfn);
  427. ptep = lookup_address(addr, &level);
  428. BUG_ON(!ptep || level != PG_LEVEL_4K);
  429. pte_pg = (pte_t *)((unsigned long)ptep & ~(PAGE_SIZE - 1));
  430. if (pte_pg == p2m_missing_pte || pte_pg == p2m_identity_pte) {
  431. /* PMD level is missing, allocate a new one */
  432. ptep = alloc_p2m_pmd(addr, ptep, pte_pg);
  433. if (!ptep)
  434. return false;
  435. }
  436. if (p2m_top_mfn) {
  437. top_mfn_p = &p2m_top_mfn[topidx];
  438. mid_mfn = ACCESS_ONCE(p2m_top_mfn_p[topidx]);
  439. BUG_ON(virt_to_mfn(mid_mfn) != *top_mfn_p);
  440. if (mid_mfn == p2m_mid_missing_mfn) {
  441. /* Separately check the mid mfn level */
  442. unsigned long missing_mfn;
  443. unsigned long mid_mfn_mfn;
  444. unsigned long old_mfn;
  445. mid_mfn = alloc_p2m_page();
  446. if (!mid_mfn)
  447. return false;
  448. p2m_mid_mfn_init(mid_mfn, p2m_missing);
  449. missing_mfn = virt_to_mfn(p2m_mid_missing_mfn);
  450. mid_mfn_mfn = virt_to_mfn(mid_mfn);
  451. old_mfn = cmpxchg(top_mfn_p, missing_mfn, mid_mfn_mfn);
  452. if (old_mfn != missing_mfn) {
  453. free_p2m_page(mid_mfn);
  454. mid_mfn = mfn_to_virt(old_mfn);
  455. } else {
  456. p2m_top_mfn_p[topidx] = mid_mfn;
  457. }
  458. }
  459. } else {
  460. mid_mfn = NULL;
  461. }
  462. p2m_pfn = pte_pfn(ACCESS_ONCE(*ptep));
  463. if (p2m_pfn == PFN_DOWN(__pa(p2m_identity)) ||
  464. p2m_pfn == PFN_DOWN(__pa(p2m_missing))) {
  465. /* p2m leaf page is missing */
  466. unsigned long *p2m;
  467. p2m = alloc_p2m_page();
  468. if (!p2m)
  469. return false;
  470. if (p2m_pfn == PFN_DOWN(__pa(p2m_missing)))
  471. p2m_init(p2m);
  472. else
  473. p2m_init_identity(p2m, pfn);
  474. spin_lock_irqsave(&p2m_update_lock, flags);
  475. if (pte_pfn(*ptep) == p2m_pfn) {
  476. set_pte(ptep,
  477. pfn_pte(PFN_DOWN(__pa(p2m)), PAGE_KERNEL));
  478. if (mid_mfn)
  479. mid_mfn[mididx] = virt_to_mfn(p2m);
  480. p2m = NULL;
  481. }
  482. spin_unlock_irqrestore(&p2m_update_lock, flags);
  483. if (p2m)
  484. free_p2m_page(p2m);
  485. }
  486. return true;
  487. }
  488. unsigned long __init set_phys_range_identity(unsigned long pfn_s,
  489. unsigned long pfn_e)
  490. {
  491. unsigned long pfn;
  492. if (unlikely(pfn_s >= xen_p2m_size))
  493. return 0;
  494. if (unlikely(xen_feature(XENFEAT_auto_translated_physmap)))
  495. return pfn_e - pfn_s;
  496. if (pfn_s > pfn_e)
  497. return 0;
  498. if (pfn_e > xen_p2m_size)
  499. pfn_e = xen_p2m_size;
  500. for (pfn = pfn_s; pfn < pfn_e; pfn++)
  501. xen_p2m_addr[pfn] = IDENTITY_FRAME(pfn);
  502. return pfn - pfn_s;
  503. }
  504. bool __set_phys_to_machine(unsigned long pfn, unsigned long mfn)
  505. {
  506. pte_t *ptep;
  507. unsigned int level;
  508. /* don't track P2M changes in autotranslate guests */
  509. if (unlikely(xen_feature(XENFEAT_auto_translated_physmap)))
  510. return true;
  511. if (unlikely(pfn >= xen_p2m_size)) {
  512. BUG_ON(mfn != INVALID_P2M_ENTRY);
  513. return true;
  514. }
  515. if (likely(!xen_safe_write_ulong(xen_p2m_addr + pfn, mfn)))
  516. return true;
  517. ptep = lookup_address((unsigned long)(xen_p2m_addr + pfn), &level);
  518. BUG_ON(!ptep || level != PG_LEVEL_4K);
  519. if (pte_pfn(*ptep) == PFN_DOWN(__pa(p2m_missing)))
  520. return mfn == INVALID_P2M_ENTRY;
  521. if (pte_pfn(*ptep) == PFN_DOWN(__pa(p2m_identity)))
  522. return mfn == IDENTITY_FRAME(pfn);
  523. return false;
  524. }
  525. bool set_phys_to_machine(unsigned long pfn, unsigned long mfn)
  526. {
  527. if (unlikely(!__set_phys_to_machine(pfn, mfn))) {
  528. if (!alloc_p2m(pfn))
  529. return false;
  530. return __set_phys_to_machine(pfn, mfn);
  531. }
  532. return true;
  533. }
  534. #define M2P_OVERRIDE_HASH_SHIFT 10
  535. #define M2P_OVERRIDE_HASH (1 << M2P_OVERRIDE_HASH_SHIFT)
  536. static struct list_head *m2p_overrides;
  537. static DEFINE_SPINLOCK(m2p_override_lock);
  538. static void __init m2p_override_init(void)
  539. {
  540. unsigned i;
  541. m2p_overrides = alloc_bootmem_align(
  542. sizeof(*m2p_overrides) * M2P_OVERRIDE_HASH,
  543. sizeof(unsigned long));
  544. for (i = 0; i < M2P_OVERRIDE_HASH; i++)
  545. INIT_LIST_HEAD(&m2p_overrides[i]);
  546. }
  547. static unsigned long mfn_hash(unsigned long mfn)
  548. {
  549. return hash_long(mfn, M2P_OVERRIDE_HASH_SHIFT);
  550. }
  551. /* Add an MFN override for a particular page */
  552. static int m2p_add_override(unsigned long mfn, struct page *page,
  553. struct gnttab_map_grant_ref *kmap_op)
  554. {
  555. unsigned long flags;
  556. unsigned long pfn;
  557. unsigned long uninitialized_var(address);
  558. unsigned level;
  559. pte_t *ptep = NULL;
  560. pfn = page_to_pfn(page);
  561. if (!PageHighMem(page)) {
  562. address = (unsigned long)__va(pfn << PAGE_SHIFT);
  563. ptep = lookup_address(address, &level);
  564. if (WARN(ptep == NULL || level != PG_LEVEL_4K,
  565. "m2p_add_override: pfn %lx not mapped", pfn))
  566. return -EINVAL;
  567. }
  568. if (kmap_op != NULL) {
  569. if (!PageHighMem(page)) {
  570. struct multicall_space mcs =
  571. xen_mc_entry(sizeof(*kmap_op));
  572. MULTI_grant_table_op(mcs.mc,
  573. GNTTABOP_map_grant_ref, kmap_op, 1);
  574. xen_mc_issue(PARAVIRT_LAZY_MMU);
  575. }
  576. }
  577. spin_lock_irqsave(&m2p_override_lock, flags);
  578. list_add(&page->lru, &m2p_overrides[mfn_hash(mfn)]);
  579. spin_unlock_irqrestore(&m2p_override_lock, flags);
  580. /* p2m(m2p(mfn)) == mfn: the mfn is already present somewhere in
  581. * this domain. Set the FOREIGN_FRAME_BIT in the p2m for the other
  582. * pfn so that the following mfn_to_pfn(mfn) calls will return the
  583. * pfn from the m2p_override (the backend pfn) instead.
  584. * We need to do this because the pages shared by the frontend
  585. * (xen-blkfront) can be already locked (lock_page, called by
  586. * do_read_cache_page); when the userspace backend tries to use them
  587. * with direct_IO, mfn_to_pfn returns the pfn of the frontend, so
  588. * do_blockdev_direct_IO is going to try to lock the same pages
  589. * again resulting in a deadlock.
  590. * As a side effect get_user_pages_fast might not be safe on the
  591. * frontend pages while they are being shared with the backend,
  592. * because mfn_to_pfn (that ends up being called by GUPF) will
  593. * return the backend pfn rather than the frontend pfn. */
  594. pfn = mfn_to_pfn_no_overrides(mfn);
  595. if (__pfn_to_mfn(pfn) == mfn)
  596. set_phys_to_machine(pfn, FOREIGN_FRAME(mfn));
  597. return 0;
  598. }
  599. int set_foreign_p2m_mapping(struct gnttab_map_grant_ref *map_ops,
  600. struct gnttab_map_grant_ref *kmap_ops,
  601. struct page **pages, unsigned int count)
  602. {
  603. int i, ret = 0;
  604. bool lazy = false;
  605. pte_t *pte;
  606. if (xen_feature(XENFEAT_auto_translated_physmap))
  607. return 0;
  608. if (kmap_ops &&
  609. !in_interrupt() &&
  610. paravirt_get_lazy_mode() == PARAVIRT_LAZY_NONE) {
  611. arch_enter_lazy_mmu_mode();
  612. lazy = true;
  613. }
  614. for (i = 0; i < count; i++) {
  615. unsigned long mfn, pfn;
  616. /* Do not add to override if the map failed. */
  617. if (map_ops[i].status)
  618. continue;
  619. if (map_ops[i].flags & GNTMAP_contains_pte) {
  620. pte = (pte_t *)(mfn_to_virt(PFN_DOWN(map_ops[i].host_addr)) +
  621. (map_ops[i].host_addr & ~PAGE_MASK));
  622. mfn = pte_mfn(*pte);
  623. } else {
  624. mfn = PFN_DOWN(map_ops[i].dev_bus_addr);
  625. }
  626. pfn = page_to_pfn(pages[i]);
  627. WARN_ON(PagePrivate(pages[i]));
  628. SetPagePrivate(pages[i]);
  629. set_page_private(pages[i], mfn);
  630. pages[i]->index = pfn_to_mfn(pfn);
  631. if (unlikely(!set_phys_to_machine(pfn, FOREIGN_FRAME(mfn)))) {
  632. ret = -ENOMEM;
  633. goto out;
  634. }
  635. if (kmap_ops) {
  636. ret = m2p_add_override(mfn, pages[i], &kmap_ops[i]);
  637. if (ret)
  638. goto out;
  639. }
  640. }
  641. out:
  642. if (lazy)
  643. arch_leave_lazy_mmu_mode();
  644. return ret;
  645. }
  646. EXPORT_SYMBOL_GPL(set_foreign_p2m_mapping);
  647. static struct page *m2p_find_override(unsigned long mfn)
  648. {
  649. unsigned long flags;
  650. struct list_head *bucket;
  651. struct page *p, *ret;
  652. if (unlikely(!m2p_overrides))
  653. return NULL;
  654. ret = NULL;
  655. bucket = &m2p_overrides[mfn_hash(mfn)];
  656. spin_lock_irqsave(&m2p_override_lock, flags);
  657. list_for_each_entry(p, bucket, lru) {
  658. if (page_private(p) == mfn) {
  659. ret = p;
  660. break;
  661. }
  662. }
  663. spin_unlock_irqrestore(&m2p_override_lock, flags);
  664. return ret;
  665. }
  666. static int m2p_remove_override(struct page *page,
  667. struct gnttab_map_grant_ref *kmap_op,
  668. unsigned long mfn)
  669. {
  670. unsigned long flags;
  671. unsigned long pfn;
  672. unsigned long uninitialized_var(address);
  673. unsigned level;
  674. pte_t *ptep = NULL;
  675. pfn = page_to_pfn(page);
  676. if (!PageHighMem(page)) {
  677. address = (unsigned long)__va(pfn << PAGE_SHIFT);
  678. ptep = lookup_address(address, &level);
  679. if (WARN(ptep == NULL || level != PG_LEVEL_4K,
  680. "m2p_remove_override: pfn %lx not mapped", pfn))
  681. return -EINVAL;
  682. }
  683. spin_lock_irqsave(&m2p_override_lock, flags);
  684. list_del(&page->lru);
  685. spin_unlock_irqrestore(&m2p_override_lock, flags);
  686. if (kmap_op != NULL) {
  687. if (!PageHighMem(page)) {
  688. struct multicall_space mcs;
  689. struct gnttab_unmap_and_replace *unmap_op;
  690. struct page *scratch_page = get_balloon_scratch_page();
  691. unsigned long scratch_page_address = (unsigned long)
  692. __va(page_to_pfn(scratch_page) << PAGE_SHIFT);
  693. /*
  694. * It might be that we queued all the m2p grant table
  695. * hypercalls in a multicall, then m2p_remove_override
  696. * get called before the multicall has actually been
  697. * issued. In this case handle is going to -1 because
  698. * it hasn't been modified yet.
  699. */
  700. if (kmap_op->handle == -1)
  701. xen_mc_flush();
  702. /*
  703. * Now if kmap_op->handle is negative it means that the
  704. * hypercall actually returned an error.
  705. */
  706. if (kmap_op->handle == GNTST_general_error) {
  707. pr_warn("m2p_remove_override: pfn %lx mfn %lx, failed to modify kernel mappings",
  708. pfn, mfn);
  709. put_balloon_scratch_page();
  710. return -1;
  711. }
  712. xen_mc_batch();
  713. mcs = __xen_mc_entry(
  714. sizeof(struct gnttab_unmap_and_replace));
  715. unmap_op = mcs.args;
  716. unmap_op->host_addr = kmap_op->host_addr;
  717. unmap_op->new_addr = scratch_page_address;
  718. unmap_op->handle = kmap_op->handle;
  719. MULTI_grant_table_op(mcs.mc,
  720. GNTTABOP_unmap_and_replace, unmap_op, 1);
  721. mcs = __xen_mc_entry(0);
  722. MULTI_update_va_mapping(mcs.mc, scratch_page_address,
  723. pfn_pte(page_to_pfn(scratch_page),
  724. PAGE_KERNEL_RO), 0);
  725. xen_mc_issue(PARAVIRT_LAZY_MMU);
  726. kmap_op->host_addr = 0;
  727. put_balloon_scratch_page();
  728. }
  729. }
  730. /* p2m(m2p(mfn)) == FOREIGN_FRAME(mfn): the mfn is already present
  731. * somewhere in this domain, even before being added to the
  732. * m2p_override (see comment above in m2p_add_override).
  733. * If there are no other entries in the m2p_override corresponding
  734. * to this mfn, then remove the FOREIGN_FRAME_BIT from the p2m for
  735. * the original pfn (the one shared by the frontend): the backend
  736. * cannot do any IO on this page anymore because it has been
  737. * unshared. Removing the FOREIGN_FRAME_BIT from the p2m entry of
  738. * the original pfn causes mfn_to_pfn(mfn) to return the frontend
  739. * pfn again. */
  740. mfn &= ~FOREIGN_FRAME_BIT;
  741. pfn = mfn_to_pfn_no_overrides(mfn);
  742. if (__pfn_to_mfn(pfn) == FOREIGN_FRAME(mfn) &&
  743. m2p_find_override(mfn) == NULL)
  744. set_phys_to_machine(pfn, mfn);
  745. return 0;
  746. }
  747. int clear_foreign_p2m_mapping(struct gnttab_unmap_grant_ref *unmap_ops,
  748. struct gnttab_map_grant_ref *kmap_ops,
  749. struct page **pages, unsigned int count)
  750. {
  751. int i, ret = 0;
  752. bool lazy = false;
  753. if (xen_feature(XENFEAT_auto_translated_physmap))
  754. return 0;
  755. if (kmap_ops &&
  756. !in_interrupt() &&
  757. paravirt_get_lazy_mode() == PARAVIRT_LAZY_NONE) {
  758. arch_enter_lazy_mmu_mode();
  759. lazy = true;
  760. }
  761. for (i = 0; i < count; i++) {
  762. unsigned long mfn = __pfn_to_mfn(page_to_pfn(pages[i]));
  763. unsigned long pfn = page_to_pfn(pages[i]);
  764. if (mfn == INVALID_P2M_ENTRY || !(mfn & FOREIGN_FRAME_BIT)) {
  765. ret = -EINVAL;
  766. goto out;
  767. }
  768. set_page_private(pages[i], INVALID_P2M_ENTRY);
  769. WARN_ON(!PagePrivate(pages[i]));
  770. ClearPagePrivate(pages[i]);
  771. set_phys_to_machine(pfn, pages[i]->index);
  772. if (kmap_ops)
  773. ret = m2p_remove_override(pages[i], &kmap_ops[i], mfn);
  774. if (ret)
  775. goto out;
  776. }
  777. out:
  778. if (lazy)
  779. arch_leave_lazy_mmu_mode();
  780. return ret;
  781. }
  782. EXPORT_SYMBOL_GPL(clear_foreign_p2m_mapping);
  783. unsigned long m2p_find_override_pfn(unsigned long mfn, unsigned long pfn)
  784. {
  785. struct page *p = m2p_find_override(mfn);
  786. unsigned long ret = pfn;
  787. if (p)
  788. ret = page_to_pfn(p);
  789. return ret;
  790. }
  791. EXPORT_SYMBOL_GPL(m2p_find_override_pfn);
  792. #ifdef CONFIG_XEN_DEBUG_FS
  793. #include <linux/debugfs.h>
  794. #include "debugfs.h"
  795. static int p2m_dump_show(struct seq_file *m, void *v)
  796. {
  797. static const char * const type_name[] = {
  798. [P2M_TYPE_IDENTITY] = "identity",
  799. [P2M_TYPE_MISSING] = "missing",
  800. [P2M_TYPE_PFN] = "pfn",
  801. [P2M_TYPE_UNKNOWN] = "abnormal"};
  802. unsigned long pfn, first_pfn;
  803. int type, prev_type;
  804. prev_type = xen_p2m_elem_type(0);
  805. first_pfn = 0;
  806. for (pfn = 0; pfn < xen_p2m_size; pfn++) {
  807. type = xen_p2m_elem_type(pfn);
  808. if (type != prev_type) {
  809. seq_printf(m, " [0x%lx->0x%lx] %s\n", first_pfn, pfn,
  810. type_name[prev_type]);
  811. prev_type = type;
  812. first_pfn = pfn;
  813. }
  814. }
  815. seq_printf(m, " [0x%lx->0x%lx] %s\n", first_pfn, pfn,
  816. type_name[prev_type]);
  817. return 0;
  818. }
  819. static int p2m_dump_open(struct inode *inode, struct file *filp)
  820. {
  821. return single_open(filp, p2m_dump_show, NULL);
  822. }
  823. static const struct file_operations p2m_dump_fops = {
  824. .open = p2m_dump_open,
  825. .read = seq_read,
  826. .llseek = seq_lseek,
  827. .release = single_release,
  828. };
  829. static struct dentry *d_mmu_debug;
  830. static int __init xen_p2m_debugfs(void)
  831. {
  832. struct dentry *d_xen = xen_init_debugfs();
  833. if (d_xen == NULL)
  834. return -ENOMEM;
  835. d_mmu_debug = debugfs_create_dir("mmu", d_xen);
  836. debugfs_create_file("p2m", 0600, d_mmu_debug, NULL, &p2m_dump_fops);
  837. return 0;
  838. }
  839. fs_initcall(xen_p2m_debugfs);
  840. #endif /* CONFIG_XEN_DEBUG_FS */