xlate_mmu.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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 <linux/slab.h>
  33. #include <linux/vmalloc.h>
  34. #include <asm/xen/hypercall.h>
  35. #include <asm/xen/hypervisor.h>
  36. #include <xen/xen.h>
  37. #include <xen/xen-ops.h>
  38. #include <xen/page.h>
  39. #include <xen/interface/xen.h>
  40. #include <xen/interface/memory.h>
  41. #include <xen/balloon.h>
  42. typedef void (*xen_gfn_fn_t)(unsigned long gfn, void *data);
  43. /* Break down the pages in 4KB chunk and call fn for each gfn */
  44. static void xen_for_each_gfn(struct page **pages, unsigned nr_gfn,
  45. xen_gfn_fn_t fn, void *data)
  46. {
  47. unsigned long xen_pfn = 0;
  48. struct page *page;
  49. int i;
  50. for (i = 0; i < nr_gfn; i++) {
  51. if ((i % XEN_PFN_PER_PAGE) == 0) {
  52. page = pages[i / XEN_PFN_PER_PAGE];
  53. xen_pfn = page_to_xen_pfn(page);
  54. }
  55. fn(pfn_to_gfn(xen_pfn++), data);
  56. }
  57. }
  58. struct remap_data {
  59. xen_pfn_t *fgfn; /* foreign domain's gfn */
  60. int nr_fgfn; /* Number of foreign gfn left to map */
  61. pgprot_t prot;
  62. domid_t domid;
  63. struct vm_area_struct *vma;
  64. int index;
  65. struct page **pages;
  66. struct xen_remap_gfn_info *info;
  67. int *err_ptr;
  68. int mapped;
  69. /* Hypercall parameters */
  70. int h_errs[XEN_PFN_PER_PAGE];
  71. xen_ulong_t h_idxs[XEN_PFN_PER_PAGE];
  72. xen_pfn_t h_gpfns[XEN_PFN_PER_PAGE];
  73. int h_iter; /* Iterator */
  74. };
  75. static void setup_hparams(unsigned long gfn, void *data)
  76. {
  77. struct remap_data *info = data;
  78. info->h_idxs[info->h_iter] = *info->fgfn;
  79. info->h_gpfns[info->h_iter] = gfn;
  80. info->h_errs[info->h_iter] = 0;
  81. info->h_iter++;
  82. info->fgfn++;
  83. }
  84. static int remap_pte_fn(pte_t *ptep, pgtable_t token, unsigned long addr,
  85. void *data)
  86. {
  87. struct remap_data *info = data;
  88. struct page *page = info->pages[info->index++];
  89. pte_t pte = pte_mkspecial(pfn_pte(page_to_pfn(page), info->prot));
  90. int rc, nr_gfn;
  91. uint32_t i;
  92. struct xen_add_to_physmap_range xatp = {
  93. .domid = DOMID_SELF,
  94. .foreign_domid = info->domid,
  95. .space = XENMAPSPACE_gmfn_foreign,
  96. };
  97. nr_gfn = min_t(typeof(info->nr_fgfn), XEN_PFN_PER_PAGE, info->nr_fgfn);
  98. info->nr_fgfn -= nr_gfn;
  99. info->h_iter = 0;
  100. xen_for_each_gfn(&page, nr_gfn, setup_hparams, info);
  101. BUG_ON(info->h_iter != nr_gfn);
  102. set_xen_guest_handle(xatp.idxs, info->h_idxs);
  103. set_xen_guest_handle(xatp.gpfns, info->h_gpfns);
  104. set_xen_guest_handle(xatp.errs, info->h_errs);
  105. xatp.size = nr_gfn;
  106. rc = HYPERVISOR_memory_op(XENMEM_add_to_physmap_range, &xatp);
  107. /* info->err_ptr expect to have one error status per Xen PFN */
  108. for (i = 0; i < nr_gfn; i++) {
  109. int err = (rc < 0) ? rc : info->h_errs[i];
  110. *(info->err_ptr++) = err;
  111. if (!err)
  112. info->mapped++;
  113. }
  114. /*
  115. * Note: The hypercall will return 0 in most of the case if even if
  116. * all the fgmfn are not mapped. We still have to update the pte
  117. * as the userspace may decide to continue.
  118. */
  119. if (!rc)
  120. set_pte_at(info->vma->vm_mm, addr, ptep, pte);
  121. return 0;
  122. }
  123. int xen_xlate_remap_gfn_array(struct vm_area_struct *vma,
  124. unsigned long addr,
  125. xen_pfn_t *gfn, int nr,
  126. int *err_ptr, pgprot_t prot,
  127. unsigned domid,
  128. struct page **pages)
  129. {
  130. int err;
  131. struct remap_data data;
  132. unsigned long range = DIV_ROUND_UP(nr, XEN_PFN_PER_PAGE) << PAGE_SHIFT;
  133. /* Kept here for the purpose of making sure code doesn't break
  134. x86 PVOPS */
  135. BUG_ON(!((vma->vm_flags & (VM_PFNMAP | VM_IO)) == (VM_PFNMAP | VM_IO)));
  136. data.fgfn = gfn;
  137. data.nr_fgfn = nr;
  138. data.prot = prot;
  139. data.domid = domid;
  140. data.vma = vma;
  141. data.pages = pages;
  142. data.index = 0;
  143. data.err_ptr = err_ptr;
  144. data.mapped = 0;
  145. err = apply_to_page_range(vma->vm_mm, addr, range,
  146. remap_pte_fn, &data);
  147. return err < 0 ? err : data.mapped;
  148. }
  149. EXPORT_SYMBOL_GPL(xen_xlate_remap_gfn_array);
  150. static void unmap_gfn(unsigned long gfn, void *data)
  151. {
  152. struct xen_remove_from_physmap xrp;
  153. xrp.domid = DOMID_SELF;
  154. xrp.gpfn = gfn;
  155. (void)HYPERVISOR_memory_op(XENMEM_remove_from_physmap, &xrp);
  156. }
  157. int xen_xlate_unmap_gfn_range(struct vm_area_struct *vma,
  158. int nr, struct page **pages)
  159. {
  160. xen_for_each_gfn(pages, nr, unmap_gfn, NULL);
  161. return 0;
  162. }
  163. EXPORT_SYMBOL_GPL(xen_xlate_unmap_gfn_range);
  164. struct map_balloon_pages {
  165. xen_pfn_t *pfns;
  166. unsigned int idx;
  167. };
  168. static void setup_balloon_gfn(unsigned long gfn, void *data)
  169. {
  170. struct map_balloon_pages *info = data;
  171. info->pfns[info->idx++] = gfn;
  172. }
  173. /**
  174. * xen_xlate_map_ballooned_pages - map a new set of ballooned pages
  175. * @gfns: returns the array of corresponding GFNs
  176. * @virt: returns the virtual address of the mapped region
  177. * @nr_grant_frames: number of GFNs
  178. * @return 0 on success, error otherwise
  179. *
  180. * This allocates a set of ballooned pages and maps them into the
  181. * kernel's address space.
  182. */
  183. int __init xen_xlate_map_ballooned_pages(xen_pfn_t **gfns, void **virt,
  184. unsigned long nr_grant_frames)
  185. {
  186. struct page **pages;
  187. xen_pfn_t *pfns;
  188. void *vaddr;
  189. struct map_balloon_pages data;
  190. int rc;
  191. unsigned long nr_pages;
  192. BUG_ON(nr_grant_frames == 0);
  193. nr_pages = DIV_ROUND_UP(nr_grant_frames, XEN_PFN_PER_PAGE);
  194. pages = kcalloc(nr_pages, sizeof(pages[0]), GFP_KERNEL);
  195. if (!pages)
  196. return -ENOMEM;
  197. pfns = kcalloc(nr_grant_frames, sizeof(pfns[0]), GFP_KERNEL);
  198. if (!pfns) {
  199. kfree(pages);
  200. return -ENOMEM;
  201. }
  202. rc = alloc_xenballooned_pages(nr_pages, pages);
  203. if (rc) {
  204. pr_warn("%s Couldn't balloon alloc %ld pages rc:%d\n", __func__,
  205. nr_pages, rc);
  206. kfree(pages);
  207. kfree(pfns);
  208. return rc;
  209. }
  210. data.pfns = pfns;
  211. data.idx = 0;
  212. xen_for_each_gfn(pages, nr_grant_frames, setup_balloon_gfn, &data);
  213. vaddr = vmap(pages, nr_pages, 0, PAGE_KERNEL);
  214. if (!vaddr) {
  215. pr_warn("%s Couldn't map %ld pages rc:%d\n", __func__,
  216. nr_pages, rc);
  217. free_xenballooned_pages(nr_pages, pages);
  218. kfree(pages);
  219. kfree(pfns);
  220. return -ENOMEM;
  221. }
  222. kfree(pages);
  223. *gfns = pfns;
  224. *virt = vaddr;
  225. return 0;
  226. }
  227. EXPORT_SYMBOL_GPL(xen_xlate_map_ballooned_pages);