xlate_mmu.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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/xen-ops.h>
  36. #include <xen/page.h>
  37. #include <xen/interface/xen.h>
  38. #include <xen/interface/memory.h>
  39. typedef void (*xen_gfn_fn_t)(unsigned long gfn, void *data);
  40. /* Break down the pages in 4KB chunk and call fn for each gfn */
  41. static void xen_for_each_gfn(struct page **pages, unsigned nr_gfn,
  42. xen_gfn_fn_t fn, void *data)
  43. {
  44. unsigned long xen_pfn = 0;
  45. struct page *page;
  46. int i;
  47. for (i = 0; i < nr_gfn; i++) {
  48. if ((i % XEN_PFN_PER_PAGE) == 0) {
  49. page = pages[i / XEN_PFN_PER_PAGE];
  50. xen_pfn = page_to_xen_pfn(page);
  51. }
  52. fn(pfn_to_gfn(xen_pfn++), data);
  53. }
  54. }
  55. struct remap_data {
  56. xen_pfn_t *fgfn; /* foreign domain's gfn */
  57. int nr_fgfn; /* Number of foreign gfn left to map */
  58. pgprot_t prot;
  59. domid_t domid;
  60. struct vm_area_struct *vma;
  61. int index;
  62. struct page **pages;
  63. struct xen_remap_gfn_info *info;
  64. int *err_ptr;
  65. int mapped;
  66. /* Hypercall parameters */
  67. int h_errs[XEN_PFN_PER_PAGE];
  68. xen_ulong_t h_idxs[XEN_PFN_PER_PAGE];
  69. xen_pfn_t h_gpfns[XEN_PFN_PER_PAGE];
  70. int h_iter; /* Iterator */
  71. };
  72. static void setup_hparams(unsigned long gfn, void *data)
  73. {
  74. struct remap_data *info = data;
  75. info->h_idxs[info->h_iter] = *info->fgfn;
  76. info->h_gpfns[info->h_iter] = gfn;
  77. info->h_errs[info->h_iter] = 0;
  78. info->h_iter++;
  79. info->fgfn++;
  80. }
  81. static int remap_pte_fn(pte_t *ptep, pgtable_t token, unsigned long addr,
  82. void *data)
  83. {
  84. struct remap_data *info = data;
  85. struct page *page = info->pages[info->index++];
  86. pte_t pte = pte_mkspecial(pfn_pte(page_to_pfn(page), info->prot));
  87. int rc, nr_gfn;
  88. uint32_t i;
  89. struct xen_add_to_physmap_range xatp = {
  90. .domid = DOMID_SELF,
  91. .foreign_domid = info->domid,
  92. .space = XENMAPSPACE_gmfn_foreign,
  93. };
  94. nr_gfn = min_t(typeof(info->nr_fgfn), XEN_PFN_PER_PAGE, info->nr_fgfn);
  95. info->nr_fgfn -= nr_gfn;
  96. info->h_iter = 0;
  97. xen_for_each_gfn(&page, nr_gfn, setup_hparams, info);
  98. BUG_ON(info->h_iter != nr_gfn);
  99. set_xen_guest_handle(xatp.idxs, info->h_idxs);
  100. set_xen_guest_handle(xatp.gpfns, info->h_gpfns);
  101. set_xen_guest_handle(xatp.errs, info->h_errs);
  102. xatp.size = nr_gfn;
  103. rc = HYPERVISOR_memory_op(XENMEM_add_to_physmap_range, &xatp);
  104. /* info->err_ptr expect to have one error status per Xen PFN */
  105. for (i = 0; i < nr_gfn; i++) {
  106. int err = (rc < 0) ? rc : info->h_errs[i];
  107. *(info->err_ptr++) = err;
  108. if (!err)
  109. info->mapped++;
  110. }
  111. /*
  112. * Note: The hypercall will return 0 in most of the case if even if
  113. * all the fgmfn are not mapped. We still have to update the pte
  114. * as the userspace may decide to continue.
  115. */
  116. if (!rc)
  117. set_pte_at(info->vma->vm_mm, addr, ptep, pte);
  118. return 0;
  119. }
  120. int xen_xlate_remap_gfn_array(struct vm_area_struct *vma,
  121. unsigned long addr,
  122. xen_pfn_t *gfn, int nr,
  123. int *err_ptr, pgprot_t prot,
  124. unsigned domid,
  125. struct page **pages)
  126. {
  127. int err;
  128. struct remap_data data;
  129. unsigned long range = DIV_ROUND_UP(nr, XEN_PFN_PER_PAGE) << PAGE_SHIFT;
  130. /* Kept here for the purpose of making sure code doesn't break
  131. x86 PVOPS */
  132. BUG_ON(!((vma->vm_flags & (VM_PFNMAP | VM_IO)) == (VM_PFNMAP | VM_IO)));
  133. data.fgfn = gfn;
  134. data.nr_fgfn = nr;
  135. data.prot = prot;
  136. data.domid = domid;
  137. data.vma = vma;
  138. data.pages = pages;
  139. data.index = 0;
  140. data.err_ptr = err_ptr;
  141. data.mapped = 0;
  142. err = apply_to_page_range(vma->vm_mm, addr, range,
  143. remap_pte_fn, &data);
  144. return err < 0 ? err : data.mapped;
  145. }
  146. EXPORT_SYMBOL_GPL(xen_xlate_remap_gfn_array);
  147. static void unmap_gfn(unsigned long gfn, void *data)
  148. {
  149. struct xen_remove_from_physmap xrp;
  150. xrp.domid = DOMID_SELF;
  151. xrp.gpfn = gfn;
  152. (void)HYPERVISOR_memory_op(XENMEM_remove_from_physmap, &xrp);
  153. }
  154. int xen_xlate_unmap_gfn_range(struct vm_area_struct *vma,
  155. int nr, struct page **pages)
  156. {
  157. xen_for_each_gfn(pages, nr, unmap_gfn, NULL);
  158. return 0;
  159. }
  160. EXPORT_SYMBOL_GPL(xen_xlate_unmap_gfn_range);