1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30#include <linux/kernel.h>
31#include <linux/mm.h>
32#include <linux/slab.h>
33#include <linux/vmalloc.h>
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>
42#include <xen/balloon.h>
43
44typedef void (*xen_gfn_fn_t)(unsigned long gfn, void *data);
45
46
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;
53
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 }
61}
62
63struct remap_data {
64 xen_pfn_t *fgfn;
65 int nr_fgfn;
66 pgprot_t prot;
67 domid_t domid;
68 struct vm_area_struct *vma;
69 int index;
70 struct page **pages;
71 struct xen_remap_gfn_info *info;
72 int *err_ptr;
73 int mapped;
74
75
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;
81};
82
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
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++];
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 };
108
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
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++;
130 }
131
132
133
134
135
136
137 if (!rc)
138 set_pte_at(info->vma->vm_mm, addr, ptep, pte);
139
140 return 0;
141}
142
143int xen_xlate_remap_gfn_array(struct vm_area_struct *vma,
144 unsigned long addr,
145 xen_pfn_t *gfn, int nr,
146 int *err_ptr, pgprot_t prot,
147 unsigned domid,
148 struct page **pages)
149{
150 int err;
151 struct remap_data data;
152 unsigned long range = DIV_ROUND_UP(nr, XEN_PFN_PER_PAGE) << PAGE_SHIFT;
153
154
155
156 BUG_ON(!((vma->vm_flags & (VM_PFNMAP | VM_IO)) == (VM_PFNMAP | VM_IO)));
157
158 data.fgfn = gfn;
159 data.nr_fgfn = nr;
160 data.prot = prot;
161 data.domid = domid;
162 data.vma = vma;
163 data.pages = pages;
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,
169 remap_pte_fn, &data);
170 return err < 0 ? err : data.mapped;
171}
172EXPORT_SYMBOL_GPL(xen_xlate_remap_gfn_array);
173
174static void unmap_gfn(unsigned long gfn, void *data)
175{
176 struct xen_remove_from_physmap xrp;
177
178 xrp.domid = DOMID_SELF;
179 xrp.gpfn = gfn;
180 (void)HYPERVISOR_memory_op(XENMEM_remove_from_physmap, &xrp);
181}
182
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);
187
188 return 0;
189}
190EXPORT_SYMBOL_GPL(xen_xlate_unmap_gfn_range);
191
192struct map_balloon_pages {
193 xen_pfn_t *pfns;
194 unsigned int idx;
195};
196
197static void setup_balloon_gfn(unsigned long gfn, void *data)
198{
199 struct map_balloon_pages *info = data;
200
201 info->pfns[info->idx++] = gfn;
202}
203
204
205
206
207
208
209
210
211
212
213
214int __init xen_xlate_map_ballooned_pages(xen_pfn_t **gfns, void **virt,
215 unsigned long nr_grant_frames)
216{
217 struct page **pages;
218 xen_pfn_t *pfns;
219 void *vaddr;
220 struct map_balloon_pages data;
221 int rc;
222 unsigned long nr_pages;
223
224 BUG_ON(nr_grant_frames == 0);
225 nr_pages = DIV_ROUND_UP(nr_grant_frames, XEN_PFN_PER_PAGE);
226 pages = kcalloc(nr_pages, sizeof(pages[0]), GFP_KERNEL);
227 if (!pages)
228 return -ENOMEM;
229
230 pfns = kcalloc(nr_grant_frames, sizeof(pfns[0]), GFP_KERNEL);
231 if (!pfns) {
232 kfree(pages);
233 return -ENOMEM;
234 }
235 rc = alloc_xenballooned_pages(nr_pages, pages);
236 if (rc) {
237 pr_warn("%s Couldn't balloon alloc %ld pages rc:%d\n", __func__,
238 nr_pages, rc);
239 kfree(pages);
240 kfree(pfns);
241 return rc;
242 }
243
244 data.pfns = pfns;
245 data.idx = 0;
246 xen_for_each_gfn(pages, nr_grant_frames, setup_balloon_gfn, &data);
247
248 vaddr = vmap(pages, nr_pages, 0, PAGE_KERNEL);
249 if (!vaddr) {
250 pr_warn("%s Couldn't map %ld pages rc:%d\n", __func__,
251 nr_pages, rc);
252 free_xenballooned_pages(nr_pages, pages);
253 kfree(pages);
254 kfree(pfns);
255 return -ENOMEM;
256 }
257 kfree(pages);
258
259 *gfns = pfns;
260 *virt = vaddr;
261
262 return 0;
263}
264EXPORT_SYMBOL_GPL(xen_xlate_map_ballooned_pages);
265