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