xlate_mmu.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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, unsigned long addr, void *data)
  85. {
  86. struct remap_data *info = data;
  87. struct page *page = info->pages[info->index++];
  88. pte_t pte = pte_mkspecial(pfn_pte(page_to_pfn(page), info->prot));
  89. int rc, nr_gfn;
  90. uint32_t i;
  91. struct xen_add_to_physmap_range xatp = {
  92. .domid = DOMID_SELF,
  93. .foreign_domid = info->domid,
  94. .space = XENMAPSPACE_gmfn_foreign,
  95. };
  96. nr_gfn = min_t(typeof(info->nr_fgfn), XEN_PFN_PER_PAGE, info->nr_fgfn);
  97. info->nr_fgfn -= nr_gfn;
  98. info->h_iter = 0;
  99. xen_for_each_gfn(&page, nr_gfn, setup_hparams, info);
  100. BUG_ON(info->h_iter != nr_gfn);
  101. set_xen_guest_handle(xatp.idxs, info->h_idxs);
  102. set_xen_guest_handle(xatp.gpfns, info->h_gpfns);
  103. set_xen_guest_handle(xatp.errs, info->h_errs);
  104. xatp.size = nr_gfn;
  105. rc = HYPERVISOR_memory_op(XENMEM_add_to_physmap_range, &xatp);
  106. /* info->err_ptr expect to have one error status per Xen PFN */
  107. for (i = 0; i < nr_gfn; i++) {
  108. int err = (rc < 0) ? rc : info->h_errs[i];
  109. *(info->err_ptr++) = err;
  110. if (!err)
  111. info->mapped++;
  112. }
  113. /*
  114. * Note: The hypercall will return 0 in most of the case if even if
  115. * all the fgmfn are not mapped. We still have to update the pte
  116. * as the userspace may decide to continue.
  117. */
  118. if (!rc)
  119. set_pte_at(info->vma->vm_mm, addr, ptep, pte);
  120. return 0;
  121. }
  122. int xen_xlate_remap_gfn_array(struct vm_area_struct *vma,
  123. unsigned long addr,
  124. xen_pfn_t *gfn, int nr,
  125. int *err_ptr, pgprot_t prot,
  126. unsigned domid,
  127. struct page **pages)
  128. {
  129. int err;
  130. struct remap_data data;
  131. unsigned long range = DIV_ROUND_UP(nr, XEN_PFN_PER_PAGE) << PAGE_SHIFT;
  132. /* Kept here for the purpose of making sure code doesn't break
  133. x86 PVOPS */
  134. BUG_ON(!((vma->vm_flags & (VM_PFNMAP | VM_IO)) == (VM_PFNMAP | VM_IO)));
  135. data.fgfn = gfn;
  136. data.nr_fgfn = nr;
  137. data.prot = prot;
  138. data.domid = domid;
  139. data.vma = vma;
  140. data.pages = pages;
  141. data.index = 0;
  142. data.err_ptr = err_ptr;
  143. data.mapped = 0;
  144. err = apply_to_page_range(vma->vm_mm, addr, range,
  145. remap_pte_fn, &data);
  146. return err < 0 ? err : data.mapped;
  147. }
  148. EXPORT_SYMBOL_GPL(xen_xlate_remap_gfn_array);
  149. static void unmap_gfn(unsigned long gfn, void *data)
  150. {
  151. struct xen_remove_from_physmap xrp;
  152. xrp.domid = DOMID_SELF;
  153. xrp.gpfn = gfn;
  154. (void)HYPERVISOR_memory_op(XENMEM_remove_from_physmap, &xrp);
  155. }
  156. int xen_xlate_unmap_gfn_range(struct vm_area_struct *vma,
  157. int nr, struct page **pages)
  158. {
  159. xen_for_each_gfn(pages, nr, unmap_gfn, NULL);
  160. return 0;
  161. }
  162. EXPORT_SYMBOL_GPL(xen_xlate_unmap_gfn_range);
  163. struct map_balloon_pages {
  164. xen_pfn_t *pfns;
  165. unsigned int idx;
  166. };
  167. static void setup_balloon_gfn(unsigned long gfn, void *data)
  168. {
  169. struct map_balloon_pages *info = data;
  170. info->pfns[info->idx++] = gfn;
  171. }
  172. /**
  173. * xen_xlate_map_ballooned_pages - map a new set of ballooned pages
  174. * @gfns: returns the array of corresponding GFNs
  175. * @virt: returns the virtual address of the mapped region
  176. * @nr_grant_frames: number of GFNs
  177. * @return 0 on success, error otherwise
  178. *
  179. * This allocates a set of ballooned pages and maps them into the
  180. * kernel's address space.
  181. */
  182. int __init xen_xlate_map_ballooned_pages(xen_pfn_t **gfns, void **virt,
  183. unsigned long nr_grant_frames)
  184. {
  185. struct page **pages;
  186. xen_pfn_t *pfns;
  187. void *vaddr;
  188. struct map_balloon_pages data;
  189. int rc;
  190. unsigned long nr_pages;
  191. BUG_ON(nr_grant_frames == 0);
  192. nr_pages = DIV_ROUND_UP(nr_grant_frames, XEN_PFN_PER_PAGE);
  193. pages = kcalloc(nr_pages, sizeof(pages[0]), GFP_KERNEL);
  194. if (!pages)
  195. return -ENOMEM;
  196. pfns = kcalloc(nr_grant_frames, sizeof(pfns[0]), GFP_KERNEL);
  197. if (!pfns) {
  198. kfree(pages);
  199. return -ENOMEM;
  200. }
  201. rc = xen_alloc_unpopulated_pages(nr_pages, pages);
  202. if (rc) {
  203. pr_warn("%s Couldn't balloon alloc %ld pages rc:%d\n", __func__,
  204. nr_pages, rc);
  205. kfree(pages);
  206. kfree(pfns);
  207. return rc;
  208. }
  209. data.pfns = pfns;
  210. data.idx = 0;
  211. xen_for_each_gfn(pages, nr_grant_frames, setup_balloon_gfn, &data);
  212. vaddr = vmap(pages, nr_pages, 0, PAGE_KERNEL);
  213. if (!vaddr) {
  214. pr_warn("%s Couldn't map %ld pages rc:%d\n", __func__,
  215. nr_pages, rc);
  216. xen_free_unpopulated_pages(nr_pages, pages);
  217. kfree(pages);
  218. kfree(pfns);
  219. return -ENOMEM;
  220. }
  221. kfree(pages);
  222. *gfns = pfns;
  223. *virt = vaddr;
  224. return 0;
  225. }
  226. struct remap_pfn {
  227. struct mm_struct *mm;
  228. struct page **pages;
  229. pgprot_t prot;
  230. unsigned long i;
  231. };
  232. static int remap_pfn_fn(pte_t *ptep, unsigned long addr, void *data)
  233. {
  234. struct remap_pfn *r = data;
  235. struct page *page = r->pages[r->i];
  236. pte_t pte = pte_mkspecial(pfn_pte(page_to_pfn(page), r->prot));
  237. set_pte_at(r->mm, addr, ptep, pte);
  238. r->i++;
  239. return 0;
  240. }
  241. /* Used by the privcmd module, but has to be built-in on ARM */
  242. int xen_remap_vma_range(struct vm_area_struct *vma, unsigned long addr, unsigned long len)
  243. {
  244. struct remap_pfn r = {
  245. .mm = vma->vm_mm,
  246. .pages = vma->vm_private_data,
  247. .prot = vma->vm_page_prot,
  248. };
  249. return apply_to_page_range(vma->vm_mm, addr, len, remap_pfn_fn, &r);
  250. }
  251. EXPORT_SYMBOL_GPL(xen_remap_vma_range);