xlate_mmu.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /*
  2. * MMU operations common to all auto-translated physmap guests.
  3. *
  4. * Copyright (C) 2015 Citrix Systems R&D Ltd.
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License version 2
  8. * as published by the Free Software Foundation; or, when distributed
  9. * separately from the Linux kernel or incorporated into other
  10. * software packages, subject to the following license:
  11. *
  12. * Permission is hereby granted, free of charge, to any person obtaining a copy
  13. * of this source file (the "Software"), to deal in the Software without
  14. * restriction, including without limitation the rights to use, copy, modify,
  15. * merge, publish, distribute, sublicense, and/or sell copies of the Software,
  16. * and to permit persons to whom the Software is furnished to do so, subject to
  17. * the following conditions:
  18. *
  19. * The above copyright notice and this permission notice shall be included in
  20. * all copies or substantial portions of the Software.
  21. *
  22. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  23. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  24. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  25. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  26. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  27. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  28. * IN THE SOFTWARE.
  29. */
  30. #include <linux/kernel.h>
  31. #include <linux/mm.h>
  32. #include <asm/xen/hypercall.h>
  33. #include <asm/xen/hypervisor.h>
  34. #include <xen/xen.h>
  35. #include <xen/page.h>
  36. #include <xen/interface/xen.h>
  37. #include <xen/interface/memory.h>
  38. typedef void (*xen_gfn_fn_t)(unsigned long gfn, void *data);
  39. /* Break down the pages in 4KB chunk and call fn for each gfn */
  40. static void xen_for_each_gfn(struct page **pages, unsigned nr_gfn,
  41. xen_gfn_fn_t fn, void *data)
  42. {
  43. unsigned long xen_pfn = 0;
  44. struct page *page;
  45. int i;
  46. for (i = 0; i < nr_gfn; i++) {
  47. if ((i % XEN_PFN_PER_PAGE) == 0) {
  48. page = pages[i / XEN_PFN_PER_PAGE];
  49. xen_pfn = page_to_xen_pfn(page);
  50. }
  51. fn(pfn_to_gfn(xen_pfn++), data);
  52. }
  53. }
  54. struct remap_data {
  55. xen_pfn_t *fgfn; /* foreign domain's gfn */
  56. int nr_fgfn; /* Number of foreign gfn left to map */
  57. pgprot_t prot;
  58. domid_t domid;
  59. struct vm_area_struct *vma;
  60. int index;
  61. struct page **pages;
  62. struct xen_remap_gfn_info *info;
  63. int *err_ptr;
  64. int mapped;
  65. /* Hypercall parameters */
  66. int h_errs[XEN_PFN_PER_PAGE];
  67. xen_ulong_t h_idxs[XEN_PFN_PER_PAGE];
  68. xen_pfn_t h_gpfns[XEN_PFN_PER_PAGE];
  69. int h_iter; /* Iterator */
  70. };
  71. static void setup_hparams(unsigned long gfn, void *data)
  72. {
  73. struct remap_data *info = data;
  74. info->h_idxs[info->h_iter] = *info->fgfn;
  75. info->h_gpfns[info->h_iter] = gfn;
  76. info->h_errs[info->h_iter] = 0;
  77. info->h_iter++;
  78. info->fgfn++;
  79. }
  80. static int remap_pte_fn(pte_t *ptep, pgtable_t token, unsigned long addr,
  81. void *data)
  82. {
  83. struct remap_data *info = data;
  84. struct page *page = info->pages[info->index++];
  85. pte_t pte = pte_mkspecial(pfn_pte(page_to_pfn(page), info->prot));
  86. int rc, nr_gfn;
  87. uint32_t i;
  88. struct xen_add_to_physmap_range xatp = {
  89. .domid = DOMID_SELF,
  90. .foreign_domid = info->domid,
  91. .space = XENMAPSPACE_gmfn_foreign,
  92. };
  93. nr_gfn = min_t(typeof(info->nr_fgfn), XEN_PFN_PER_PAGE, info->nr_fgfn);
  94. info->nr_fgfn -= nr_gfn;
  95. info->h_iter = 0;
  96. xen_for_each_gfn(&page, nr_gfn, setup_hparams, info);
  97. BUG_ON(info->h_iter != nr_gfn);
  98. set_xen_guest_handle(xatp.idxs, info->h_idxs);
  99. set_xen_guest_handle(xatp.gpfns, info->h_gpfns);
  100. set_xen_guest_handle(xatp.errs, info->h_errs);
  101. xatp.size = nr_gfn;
  102. rc = HYPERVISOR_memory_op(XENMEM_add_to_physmap_range, &xatp);
  103. /* info->err_ptr expect to have one error status per Xen PFN */
  104. for (i = 0; i < nr_gfn; i++) {
  105. int err = (rc < 0) ? rc : info->h_errs[i];
  106. *(info->err_ptr++) = err;
  107. if (!err)
  108. info->mapped++;
  109. }
  110. /*
  111. * Note: The hypercall will return 0 in most of the case if even if
  112. * all the fgmfn are not mapped. We still have to update the pte
  113. * as the userspace may decide to continue.
  114. */
  115. if (!rc)
  116. set_pte_at(info->vma->vm_mm, addr, ptep, pte);
  117. return 0;
  118. }
  119. int xen_xlate_remap_gfn_array(struct vm_area_struct *vma,
  120. unsigned long addr,
  121. xen_pfn_t *gfn, int nr,
  122. int *err_ptr, pgprot_t prot,
  123. unsigned domid,
  124. struct page **pages)
  125. {
  126. int err;
  127. struct remap_data data;
  128. unsigned long range = DIV_ROUND_UP(nr, XEN_PFN_PER_PAGE) << PAGE_SHIFT;
  129. /* Kept here for the purpose of making sure code doesn't break
  130. x86 PVOPS */
  131. BUG_ON(!((vma->vm_flags & (VM_PFNMAP | VM_IO)) == (VM_PFNMAP | VM_IO)));
  132. data.fgfn = gfn;
  133. data.nr_fgfn = nr;
  134. data.prot = prot;
  135. data.domid = domid;
  136. data.vma = vma;
  137. data.pages = pages;
  138. data.index = 0;
  139. data.err_ptr = err_ptr;
  140. data.mapped = 0;
  141. err = apply_to_page_range(vma->vm_mm, addr, range,
  142. remap_pte_fn, &data);
  143. return err < 0 ? err : data.mapped;
  144. }
  145. EXPORT_SYMBOL_GPL(xen_xlate_remap_gfn_array);
  146. static void unmap_gfn(unsigned long gfn, void *data)
  147. {
  148. struct xen_remove_from_physmap xrp;
  149. xrp.domid = DOMID_SELF;
  150. xrp.gpfn = gfn;
  151. (void)HYPERVISOR_memory_op(XENMEM_remove_from_physmap, &xrp);
  152. }
  153. int xen_xlate_unmap_gfn_range(struct vm_area_struct *vma,
  154. int nr, struct page **pages)
  155. {
  156. xen_for_each_gfn(pages, nr, unmap_gfn, NULL);
  157. return 0;
  158. }
  159. EXPORT_SYMBOL_GPL(xen_xlate_unmap_gfn_range);