xlate_mmu.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. /* map fgfn of domid to lpfn in the current domain */
  39. static int map_foreign_page(unsigned long lpfn, unsigned long fgfn,
  40. unsigned int domid)
  41. {
  42. int rc;
  43. struct xen_add_to_physmap_range xatp = {
  44. .domid = DOMID_SELF,
  45. .foreign_domid = domid,
  46. .size = 1,
  47. .space = XENMAPSPACE_gmfn_foreign,
  48. };
  49. xen_ulong_t idx = fgfn;
  50. xen_pfn_t gpfn = lpfn;
  51. int err = 0;
  52. set_xen_guest_handle(xatp.idxs, &idx);
  53. set_xen_guest_handle(xatp.gpfns, &gpfn);
  54. set_xen_guest_handle(xatp.errs, &err);
  55. rc = HYPERVISOR_memory_op(XENMEM_add_to_physmap_range, &xatp);
  56. return rc < 0 ? rc : err;
  57. }
  58. struct remap_data {
  59. xen_pfn_t *fgfn; /* foreign domain's gfn */
  60. pgprot_t prot;
  61. domid_t domid;
  62. struct vm_area_struct *vma;
  63. int index;
  64. struct page **pages;
  65. struct xen_remap_gfn_info *info;
  66. int *err_ptr;
  67. int mapped;
  68. };
  69. static int remap_pte_fn(pte_t *ptep, pgtable_t token, unsigned long addr,
  70. void *data)
  71. {
  72. struct remap_data *info = data;
  73. struct page *page = info->pages[info->index++];
  74. unsigned long pfn = page_to_pfn(page);
  75. pte_t pte = pte_mkspecial(pfn_pte(pfn, info->prot));
  76. int rc;
  77. rc = map_foreign_page(pfn, *info->fgfn, info->domid);
  78. *info->err_ptr++ = rc;
  79. if (!rc) {
  80. set_pte_at(info->vma->vm_mm, addr, ptep, pte);
  81. info->mapped++;
  82. }
  83. info->fgfn++;
  84. return 0;
  85. }
  86. int xen_xlate_remap_gfn_array(struct vm_area_struct *vma,
  87. unsigned long addr,
  88. xen_pfn_t *gfn, int nr,
  89. int *err_ptr, pgprot_t prot,
  90. unsigned domid,
  91. struct page **pages)
  92. {
  93. int err;
  94. struct remap_data data;
  95. unsigned long range = nr << PAGE_SHIFT;
  96. /* Kept here for the purpose of making sure code doesn't break
  97. x86 PVOPS */
  98. BUG_ON(!((vma->vm_flags & (VM_PFNMAP | VM_IO)) == (VM_PFNMAP | VM_IO)));
  99. data.fgfn = gfn;
  100. data.prot = prot;
  101. data.domid = domid;
  102. data.vma = vma;
  103. data.pages = pages;
  104. data.index = 0;
  105. data.err_ptr = err_ptr;
  106. data.mapped = 0;
  107. err = apply_to_page_range(vma->vm_mm, addr, range,
  108. remap_pte_fn, &data);
  109. return err < 0 ? err : data.mapped;
  110. }
  111. EXPORT_SYMBOL_GPL(xen_xlate_remap_gfn_array);
  112. int xen_xlate_unmap_gfn_range(struct vm_area_struct *vma,
  113. int nr, struct page **pages)
  114. {
  115. int i;
  116. for (i = 0; i < nr; i++) {
  117. struct xen_remove_from_physmap xrp;
  118. unsigned long pfn;
  119. pfn = page_to_pfn(pages[i]);
  120. xrp.domid = DOMID_SELF;
  121. xrp.gpfn = pfn;
  122. (void)HYPERVISOR_memory_op(XENMEM_remove_from_physmap, &xrp);
  123. }
  124. return 0;
  125. }
  126. EXPORT_SYMBOL_GPL(xen_xlate_unmap_gfn_range);