]>
Commit | Line | Data |
---|---|---|
ab31523c GH |
1 | /****************************************************************************** |
2 | * gntdev.c | |
3 | * | |
4 | * Device for accessing (in user-space) pages that have been granted by other | |
5 | * domains. | |
6 | * | |
7 | * Copyright (c) 2006-2007, D G Murray. | |
8 | * (c) 2009 Gerd Hoffmann <kraxel@redhat.com> | |
9 | * | |
10 | * This program is distributed in the hope that it will be useful, | |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | * GNU General Public License for more details. | |
14 | * | |
15 | * You should have received a copy of the GNU General Public License | |
16 | * along with this program; if not, write to the Free Software | |
17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
18 | */ | |
19 | ||
20 | #undef DEBUG | |
21 | ||
22 | #include <linux/module.h> | |
23 | #include <linux/kernel.h> | |
24 | #include <linux/init.h> | |
25 | #include <linux/miscdevice.h> | |
26 | #include <linux/fs.h> | |
27 | #include <linux/mm.h> | |
28 | #include <linux/mman.h> | |
29 | #include <linux/mmu_notifier.h> | |
30 | #include <linux/types.h> | |
31 | #include <linux/uaccess.h> | |
32 | #include <linux/sched.h> | |
33 | #include <linux/spinlock.h> | |
34 | #include <linux/slab.h> | |
aab8f11a | 35 | #include <linux/highmem.h> |
ab31523c GH |
36 | |
37 | #include <xen/xen.h> | |
38 | #include <xen/grant_table.h> | |
39 | #include <xen/gntdev.h> | |
bdc612dc | 40 | #include <xen/events.h> |
ab31523c GH |
41 | #include <asm/xen/hypervisor.h> |
42 | #include <asm/xen/hypercall.h> | |
43 | #include <asm/xen/page.h> | |
44 | ||
45 | MODULE_LICENSE("GPL"); | |
46 | MODULE_AUTHOR("Derek G. Murray <Derek.Murray@cl.cam.ac.uk>, " | |
47 | "Gerd Hoffmann <kraxel@redhat.com>"); | |
48 | MODULE_DESCRIPTION("User-space granted page access driver"); | |
49 | ||
ef91082e | 50 | static int limit = 1024*1024; |
ab31523c | 51 | module_param(limit, int, 0644); |
ef91082e DDG |
52 | MODULE_PARM_DESC(limit, "Maximum number of grants that may be mapped by " |
53 | "the gntdev device"); | |
54 | ||
55 | static atomic_t pages_mapped = ATOMIC_INIT(0); | |
ab31523c | 56 | |
aab8f11a DDG |
57 | static int use_ptemod; |
58 | ||
ab31523c GH |
59 | struct gntdev_priv { |
60 | struct list_head maps; | |
ab31523c GH |
61 | /* lock protects maps from concurrent changes */ |
62 | spinlock_t lock; | |
63 | struct mm_struct *mm; | |
64 | struct mmu_notifier mn; | |
65 | }; | |
66 | ||
bdc612dc DDG |
67 | struct unmap_notify { |
68 | int flags; | |
69 | /* Address relative to the start of the grant_map */ | |
70 | int addr; | |
71 | int event; | |
72 | }; | |
73 | ||
ab31523c GH |
74 | struct grant_map { |
75 | struct list_head next; | |
ab31523c GH |
76 | struct vm_area_struct *vma; |
77 | int index; | |
78 | int count; | |
79 | int flags; | |
68b025c8 | 80 | atomic_t users; |
bdc612dc | 81 | struct unmap_notify notify; |
ab31523c GH |
82 | struct ioctl_gntdev_grant_ref *grants; |
83 | struct gnttab_map_grant_ref *map_ops; | |
84 | struct gnttab_unmap_grant_ref *unmap_ops; | |
a12b4eb3 | 85 | struct page **pages; |
ab31523c GH |
86 | }; |
87 | ||
aab8f11a DDG |
88 | static int unmap_grant_pages(struct grant_map *map, int offset, int pages); |
89 | ||
ab31523c GH |
90 | /* ------------------------------------------------------------------ */ |
91 | ||
92 | static void gntdev_print_maps(struct gntdev_priv *priv, | |
93 | char *text, int text_index) | |
94 | { | |
95 | #ifdef DEBUG | |
96 | struct grant_map *map; | |
97 | ||
ef91082e | 98 | pr_debug("%s: maps list (priv %p)\n", __func__, priv); |
ab31523c GH |
99 | list_for_each_entry(map, &priv->maps, next) |
100 | pr_debug(" index %2d, count %2d %s\n", | |
101 | map->index, map->count, | |
102 | map->index == text_index && text ? text : ""); | |
103 | #endif | |
104 | } | |
105 | ||
106 | static struct grant_map *gntdev_alloc_map(struct gntdev_priv *priv, int count) | |
107 | { | |
108 | struct grant_map *add; | |
a12b4eb3 | 109 | int i; |
ab31523c GH |
110 | |
111 | add = kzalloc(sizeof(struct grant_map), GFP_KERNEL); | |
112 | if (NULL == add) | |
113 | return NULL; | |
114 | ||
115 | add->grants = kzalloc(sizeof(add->grants[0]) * count, GFP_KERNEL); | |
116 | add->map_ops = kzalloc(sizeof(add->map_ops[0]) * count, GFP_KERNEL); | |
117 | add->unmap_ops = kzalloc(sizeof(add->unmap_ops[0]) * count, GFP_KERNEL); | |
a12b4eb3 SS |
118 | add->pages = kzalloc(sizeof(add->pages[0]) * count, GFP_KERNEL); |
119 | if (NULL == add->grants || | |
120 | NULL == add->map_ops || | |
121 | NULL == add->unmap_ops || | |
122 | NULL == add->pages) | |
ab31523c GH |
123 | goto err; |
124 | ||
a12b4eb3 SS |
125 | for (i = 0; i < count; i++) { |
126 | add->pages[i] = alloc_page(GFP_KERNEL | __GFP_HIGHMEM); | |
127 | if (add->pages[i] == NULL) | |
128 | goto err; | |
77c35acb DDG |
129 | add->map_ops[i].handle = -1; |
130 | add->unmap_ops[i].handle = -1; | |
a12b4eb3 SS |
131 | } |
132 | ||
ab31523c GH |
133 | add->index = 0; |
134 | add->count = count; | |
68b025c8 | 135 | atomic_set(&add->users, 1); |
ab31523c | 136 | |
ab31523c GH |
137 | return add; |
138 | ||
139 | err: | |
a12b4eb3 SS |
140 | if (add->pages) |
141 | for (i = 0; i < count; i++) { | |
142 | if (add->pages[i]) | |
143 | __free_page(add->pages[i]); | |
144 | } | |
145 | kfree(add->pages); | |
ab31523c GH |
146 | kfree(add->grants); |
147 | kfree(add->map_ops); | |
148 | kfree(add->unmap_ops); | |
149 | kfree(add); | |
150 | return NULL; | |
151 | } | |
152 | ||
153 | static void gntdev_add_map(struct gntdev_priv *priv, struct grant_map *add) | |
154 | { | |
155 | struct grant_map *map; | |
156 | ||
157 | list_for_each_entry(map, &priv->maps, next) { | |
158 | if (add->index + add->count < map->index) { | |
159 | list_add_tail(&add->next, &map->next); | |
160 | goto done; | |
161 | } | |
162 | add->index = map->index + map->count; | |
163 | } | |
164 | list_add_tail(&add->next, &priv->maps); | |
165 | ||
166 | done: | |
ab31523c GH |
167 | gntdev_print_maps(priv, "[new]", add->index); |
168 | } | |
169 | ||
170 | static struct grant_map *gntdev_find_map_index(struct gntdev_priv *priv, | |
171 | int index, int count) | |
172 | { | |
173 | struct grant_map *map; | |
174 | ||
175 | list_for_each_entry(map, &priv->maps, next) { | |
176 | if (map->index != index) | |
177 | continue; | |
bdc612dc | 178 | if (count && map->count != count) |
ab31523c GH |
179 | continue; |
180 | return map; | |
181 | } | |
182 | return NULL; | |
183 | } | |
184 | ||
68b025c8 | 185 | static void gntdev_put_map(struct grant_map *map) |
ab31523c | 186 | { |
a12b4eb3 SS |
187 | int i; |
188 | ||
ab31523c GH |
189 | if (!map) |
190 | return; | |
a12b4eb3 | 191 | |
68b025c8 DDG |
192 | if (!atomic_dec_and_test(&map->users)) |
193 | return; | |
194 | ||
195 | atomic_sub(map->count, &pages_mapped); | |
196 | ||
bdc612dc DDG |
197 | if (map->notify.flags & UNMAP_NOTIFY_SEND_EVENT) { |
198 | notify_remote_via_evtchn(map->notify.event); | |
199 | } | |
200 | ||
aab8f11a DDG |
201 | if (map->pages) { |
202 | if (!use_ptemod) | |
203 | unmap_grant_pages(map, 0, map->count); | |
204 | ||
a12b4eb3 | 205 | for (i = 0; i < map->count; i++) { |
aab8f11a DDG |
206 | uint32_t check, *tmp; |
207 | if (!map->pages[i]) | |
208 | continue; | |
209 | /* XXX When unmapping in an HVM domain, Xen will | |
210 | * sometimes end up mapping the GFN to an invalid MFN. | |
211 | * In this case, writes will be discarded and reads will | |
212 | * return all 0xFF bytes. Leak these unusable GFNs | |
213 | * until Xen supports fixing their p2m mapping. | |
214 | * | |
215 | * Confirmed present in Xen 4.1-RC3 with HVM source | |
216 | */ | |
217 | tmp = kmap(map->pages[i]); | |
218 | *tmp = 0xdeaddead; | |
219 | mb(); | |
220 | check = *tmp; | |
221 | kunmap(map->pages[i]); | |
222 | if (check == 0xdeaddead) | |
a12b4eb3 | 223 | __free_page(map->pages[i]); |
aab8f11a DDG |
224 | else |
225 | pr_debug("Discard page %d=%ld\n", i, | |
226 | page_to_pfn(map->pages[i])); | |
a12b4eb3 | 227 | } |
aab8f11a | 228 | } |
a12b4eb3 | 229 | kfree(map->pages); |
ab31523c GH |
230 | kfree(map->grants); |
231 | kfree(map->map_ops); | |
232 | kfree(map->unmap_ops); | |
233 | kfree(map); | |
234 | } | |
235 | ||
236 | /* ------------------------------------------------------------------ */ | |
237 | ||
238 | static int find_grant_ptes(pte_t *pte, pgtable_t token, | |
239 | unsigned long addr, void *data) | |
240 | { | |
241 | struct grant_map *map = data; | |
242 | unsigned int pgnr = (addr - map->vma->vm_start) >> PAGE_SHIFT; | |
aab8f11a | 243 | int flags = map->flags | GNTMAP_application_map | GNTMAP_contains_pte; |
ab31523c GH |
244 | u64 pte_maddr; |
245 | ||
246 | BUG_ON(pgnr >= map->count); | |
ba5d1012 JF |
247 | pte_maddr = arbitrary_virt_to_machine(pte).maddr; |
248 | ||
aab8f11a | 249 | gnttab_set_map_op(&map->map_ops[pgnr], pte_maddr, flags, |
ab31523c GH |
250 | map->grants[pgnr].ref, |
251 | map->grants[pgnr].domid); | |
aab8f11a | 252 | gnttab_set_unmap_op(&map->unmap_ops[pgnr], pte_maddr, flags, |
77c35acb | 253 | -1 /* handle */); |
ab31523c GH |
254 | return 0; |
255 | } | |
256 | ||
257 | static int map_grant_pages(struct grant_map *map) | |
258 | { | |
259 | int i, err = 0; | |
aab8f11a DDG |
260 | phys_addr_t addr; |
261 | ||
262 | if (!use_ptemod) { | |
12996fc3 | 263 | /* Note: it could already be mapped */ |
77c35acb | 264 | if (map->map_ops[0].handle != -1) |
12996fc3 | 265 | return 0; |
aab8f11a DDG |
266 | for (i = 0; i < map->count; i++) { |
267 | addr = (phys_addr_t) | |
268 | pfn_to_kaddr(page_to_pfn(map->pages[i])); | |
269 | gnttab_set_map_op(&map->map_ops[i], addr, map->flags, | |
270 | map->grants[i].ref, | |
271 | map->grants[i].domid); | |
272 | gnttab_set_unmap_op(&map->unmap_ops[i], addr, | |
77c35acb | 273 | map->flags, -1 /* handle */); |
aab8f11a DDG |
274 | } |
275 | } | |
ab31523c GH |
276 | |
277 | pr_debug("map %d+%d\n", map->index, map->count); | |
a12b4eb3 | 278 | err = gnttab_map_refs(map->map_ops, map->pages, map->count); |
ab31523c GH |
279 | if (err) |
280 | return err; | |
281 | ||
282 | for (i = 0; i < map->count; i++) { | |
283 | if (map->map_ops[i].status) | |
284 | err = -EINVAL; | |
77c35acb DDG |
285 | else { |
286 | BUG_ON(map->map_ops[i].handle == -1); | |
287 | map->unmap_ops[i].handle = map->map_ops[i].handle; | |
288 | pr_debug("map handle=%d\n", map->map_ops[i].handle); | |
289 | } | |
ab31523c GH |
290 | } |
291 | return err; | |
292 | } | |
293 | ||
b57c1869 | 294 | static int __unmap_grant_pages(struct grant_map *map, int offset, int pages) |
ab31523c GH |
295 | { |
296 | int i, err = 0; | |
297 | ||
bdc612dc DDG |
298 | if (map->notify.flags & UNMAP_NOTIFY_CLEAR_BYTE) { |
299 | int pgno = (map->notify.addr >> PAGE_SHIFT); | |
0ea22f07 | 300 | if (pgno >= offset && pgno < offset + pages && use_ptemod) { |
f4ee4af4 DDG |
301 | void __user *tmp = (void __user *) |
302 | map->vma->vm_start + map->notify.addr; | |
9960be97 DDG |
303 | err = copy_to_user(tmp, &err, 1); |
304 | if (err) | |
305 | return err; | |
0ea22f07 DDG |
306 | map->notify.flags &= ~UNMAP_NOTIFY_CLEAR_BYTE; |
307 | } else if (pgno >= offset && pgno < offset + pages) { | |
bdc612dc DDG |
308 | uint8_t *tmp = kmap(map->pages[pgno]); |
309 | tmp[map->notify.addr & (PAGE_SIZE-1)] = 0; | |
310 | kunmap(map->pages[pgno]); | |
311 | map->notify.flags &= ~UNMAP_NOTIFY_CLEAR_BYTE; | |
312 | } | |
313 | } | |
314 | ||
0ea22f07 | 315 | err = gnttab_unmap_refs(map->unmap_ops + offset, map->pages + offset, pages); |
ab31523c GH |
316 | if (err) |
317 | return err; | |
318 | ||
319 | for (i = 0; i < pages; i++) { | |
320 | if (map->unmap_ops[offset+i].status) | |
321 | err = -EINVAL; | |
77c35acb DDG |
322 | pr_debug("unmap handle=%d st=%d\n", |
323 | map->unmap_ops[offset+i].handle, | |
324 | map->unmap_ops[offset+i].status); | |
325 | map->unmap_ops[offset+i].handle = -1; | |
ab31523c GH |
326 | } |
327 | return err; | |
328 | } | |
329 | ||
b57c1869 DDG |
330 | static int unmap_grant_pages(struct grant_map *map, int offset, int pages) |
331 | { | |
332 | int range, err = 0; | |
333 | ||
334 | pr_debug("unmap %d+%d [%d+%d]\n", map->index, map->count, offset, pages); | |
335 | ||
336 | /* It is possible the requested range will have a "hole" where we | |
337 | * already unmapped some of the grants. Only unmap valid ranges. | |
338 | */ | |
339 | while (pages && !err) { | |
77c35acb | 340 | while (pages && map->unmap_ops[offset].handle == -1) { |
b57c1869 DDG |
341 | offset++; |
342 | pages--; | |
343 | } | |
344 | range = 0; | |
345 | while (range < pages) { | |
77c35acb | 346 | if (map->unmap_ops[offset+range].handle == -1) { |
b57c1869 DDG |
347 | range--; |
348 | break; | |
349 | } | |
350 | range++; | |
351 | } | |
352 | err = __unmap_grant_pages(map, offset, range); | |
353 | offset += range; | |
354 | pages -= range; | |
355 | } | |
356 | ||
357 | return err; | |
358 | } | |
359 | ||
ab31523c GH |
360 | /* ------------------------------------------------------------------ */ |
361 | ||
362 | static void gntdev_vma_close(struct vm_area_struct *vma) | |
363 | { | |
364 | struct grant_map *map = vma->vm_private_data; | |
365 | ||
366 | pr_debug("close %p\n", vma); | |
ab31523c GH |
367 | map->vma = NULL; |
368 | vma->vm_private_data = NULL; | |
68b025c8 | 369 | gntdev_put_map(map); |
ab31523c GH |
370 | } |
371 | ||
ab31523c GH |
372 | static struct vm_operations_struct gntdev_vmops = { |
373 | .close = gntdev_vma_close, | |
ab31523c GH |
374 | }; |
375 | ||
376 | /* ------------------------------------------------------------------ */ | |
377 | ||
378 | static void mn_invl_range_start(struct mmu_notifier *mn, | |
379 | struct mm_struct *mm, | |
380 | unsigned long start, unsigned long end) | |
381 | { | |
382 | struct gntdev_priv *priv = container_of(mn, struct gntdev_priv, mn); | |
383 | struct grant_map *map; | |
384 | unsigned long mstart, mend; | |
385 | int err; | |
386 | ||
387 | spin_lock(&priv->lock); | |
388 | list_for_each_entry(map, &priv->maps, next) { | |
389 | if (!map->vma) | |
390 | continue; | |
ab31523c GH |
391 | if (map->vma->vm_start >= end) |
392 | continue; | |
393 | if (map->vma->vm_end <= start) | |
394 | continue; | |
395 | mstart = max(start, map->vma->vm_start); | |
396 | mend = min(end, map->vma->vm_end); | |
397 | pr_debug("map %d+%d (%lx %lx), range %lx %lx, mrange %lx %lx\n", | |
398 | map->index, map->count, | |
399 | map->vma->vm_start, map->vma->vm_end, | |
400 | start, end, mstart, mend); | |
401 | err = unmap_grant_pages(map, | |
402 | (mstart - map->vma->vm_start) >> PAGE_SHIFT, | |
403 | (mend - mstart) >> PAGE_SHIFT); | |
404 | WARN_ON(err); | |
405 | } | |
406 | spin_unlock(&priv->lock); | |
407 | } | |
408 | ||
409 | static void mn_invl_page(struct mmu_notifier *mn, | |
410 | struct mm_struct *mm, | |
411 | unsigned long address) | |
412 | { | |
413 | mn_invl_range_start(mn, mm, address, address + PAGE_SIZE); | |
414 | } | |
415 | ||
416 | static void mn_release(struct mmu_notifier *mn, | |
417 | struct mm_struct *mm) | |
418 | { | |
419 | struct gntdev_priv *priv = container_of(mn, struct gntdev_priv, mn); | |
420 | struct grant_map *map; | |
421 | int err; | |
422 | ||
423 | spin_lock(&priv->lock); | |
424 | list_for_each_entry(map, &priv->maps, next) { | |
425 | if (!map->vma) | |
426 | continue; | |
427 | pr_debug("map %d+%d (%lx %lx)\n", | |
428 | map->index, map->count, | |
429 | map->vma->vm_start, map->vma->vm_end); | |
430 | err = unmap_grant_pages(map, /* offset */ 0, map->count); | |
431 | WARN_ON(err); | |
432 | } | |
433 | spin_unlock(&priv->lock); | |
434 | } | |
435 | ||
436 | struct mmu_notifier_ops gntdev_mmu_ops = { | |
437 | .release = mn_release, | |
438 | .invalidate_page = mn_invl_page, | |
439 | .invalidate_range_start = mn_invl_range_start, | |
440 | }; | |
441 | ||
442 | /* ------------------------------------------------------------------ */ | |
443 | ||
444 | static int gntdev_open(struct inode *inode, struct file *flip) | |
445 | { | |
446 | struct gntdev_priv *priv; | |
447 | int ret = 0; | |
448 | ||
449 | priv = kzalloc(sizeof(*priv), GFP_KERNEL); | |
450 | if (!priv) | |
451 | return -ENOMEM; | |
452 | ||
453 | INIT_LIST_HEAD(&priv->maps); | |
454 | spin_lock_init(&priv->lock); | |
ab31523c | 455 | |
aab8f11a DDG |
456 | if (use_ptemod) { |
457 | priv->mm = get_task_mm(current); | |
458 | if (!priv->mm) { | |
459 | kfree(priv); | |
460 | return -ENOMEM; | |
461 | } | |
462 | priv->mn.ops = &gntdev_mmu_ops; | |
463 | ret = mmu_notifier_register(&priv->mn, priv->mm); | |
464 | mmput(priv->mm); | |
ab31523c | 465 | } |
ab31523c GH |
466 | |
467 | if (ret) { | |
468 | kfree(priv); | |
469 | return ret; | |
470 | } | |
471 | ||
472 | flip->private_data = priv; | |
473 | pr_debug("priv %p\n", priv); | |
474 | ||
475 | return 0; | |
476 | } | |
477 | ||
478 | static int gntdev_release(struct inode *inode, struct file *flip) | |
479 | { | |
480 | struct gntdev_priv *priv = flip->private_data; | |
481 | struct grant_map *map; | |
ab31523c GH |
482 | |
483 | pr_debug("priv %p\n", priv); | |
484 | ||
485 | spin_lock(&priv->lock); | |
486 | while (!list_empty(&priv->maps)) { | |
487 | map = list_entry(priv->maps.next, struct grant_map, next); | |
68b025c8 DDG |
488 | list_del(&map->next); |
489 | gntdev_put_map(map); | |
ab31523c GH |
490 | } |
491 | spin_unlock(&priv->lock); | |
492 | ||
aab8f11a DDG |
493 | if (use_ptemod) |
494 | mmu_notifier_unregister(&priv->mn, priv->mm); | |
ab31523c GH |
495 | kfree(priv); |
496 | return 0; | |
497 | } | |
498 | ||
499 | static long gntdev_ioctl_map_grant_ref(struct gntdev_priv *priv, | |
500 | struct ioctl_gntdev_map_grant_ref __user *u) | |
501 | { | |
502 | struct ioctl_gntdev_map_grant_ref op; | |
503 | struct grant_map *map; | |
504 | int err; | |
505 | ||
506 | if (copy_from_user(&op, u, sizeof(op)) != 0) | |
507 | return -EFAULT; | |
508 | pr_debug("priv %p, add %d\n", priv, op.count); | |
509 | if (unlikely(op.count <= 0)) | |
510 | return -EINVAL; | |
ab31523c GH |
511 | |
512 | err = -ENOMEM; | |
513 | map = gntdev_alloc_map(priv, op.count); | |
514 | if (!map) | |
515 | return err; | |
ef91082e | 516 | |
68b025c8 DDG |
517 | if (unlikely(atomic_add_return(op.count, &pages_mapped) > limit)) { |
518 | pr_debug("can't map: over limit\n"); | |
519 | gntdev_put_map(map); | |
ab31523c GH |
520 | return err; |
521 | } | |
522 | ||
68b025c8 DDG |
523 | if (copy_from_user(map->grants, &u->refs, |
524 | sizeof(map->grants[0]) * op.count) != 0) { | |
525 | gntdev_put_map(map); | |
ef91082e DDG |
526 | return err; |
527 | } | |
528 | ||
ab31523c GH |
529 | spin_lock(&priv->lock); |
530 | gntdev_add_map(priv, map); | |
531 | op.index = map->index << PAGE_SHIFT; | |
532 | spin_unlock(&priv->lock); | |
533 | ||
68b025c8 DDG |
534 | if (copy_to_user(u, &op, sizeof(op)) != 0) |
535 | return -EFAULT; | |
536 | ||
ab31523c GH |
537 | return 0; |
538 | } | |
539 | ||
540 | static long gntdev_ioctl_unmap_grant_ref(struct gntdev_priv *priv, | |
541 | struct ioctl_gntdev_unmap_grant_ref __user *u) | |
542 | { | |
543 | struct ioctl_gntdev_unmap_grant_ref op; | |
544 | struct grant_map *map; | |
545 | int err = -ENOENT; | |
546 | ||
547 | if (copy_from_user(&op, u, sizeof(op)) != 0) | |
548 | return -EFAULT; | |
549 | pr_debug("priv %p, del %d+%d\n", priv, (int)op.index, (int)op.count); | |
550 | ||
551 | spin_lock(&priv->lock); | |
552 | map = gntdev_find_map_index(priv, op.index >> PAGE_SHIFT, op.count); | |
68b025c8 DDG |
553 | if (map) { |
554 | list_del(&map->next); | |
555 | gntdev_put_map(map); | |
556 | err = 0; | |
557 | } | |
ab31523c | 558 | spin_unlock(&priv->lock); |
ab31523c GH |
559 | return err; |
560 | } | |
561 | ||
562 | static long gntdev_ioctl_get_offset_for_vaddr(struct gntdev_priv *priv, | |
563 | struct ioctl_gntdev_get_offset_for_vaddr __user *u) | |
564 | { | |
565 | struct ioctl_gntdev_get_offset_for_vaddr op; | |
a879211b | 566 | struct vm_area_struct *vma; |
ab31523c GH |
567 | struct grant_map *map; |
568 | ||
569 | if (copy_from_user(&op, u, sizeof(op)) != 0) | |
570 | return -EFAULT; | |
571 | pr_debug("priv %p, offset for vaddr %lx\n", priv, (unsigned long)op.vaddr); | |
572 | ||
a879211b DDG |
573 | vma = find_vma(current->mm, op.vaddr); |
574 | if (!vma || vma->vm_ops != &gntdev_vmops) | |
ab31523c | 575 | return -EINVAL; |
a879211b DDG |
576 | |
577 | map = vma->vm_private_data; | |
578 | if (!map) | |
579 | return -EINVAL; | |
580 | ||
ab31523c GH |
581 | op.offset = map->index << PAGE_SHIFT; |
582 | op.count = map->count; | |
ab31523c GH |
583 | |
584 | if (copy_to_user(u, &op, sizeof(op)) != 0) | |
585 | return -EFAULT; | |
586 | return 0; | |
587 | } | |
588 | ||
bdc612dc DDG |
589 | static long gntdev_ioctl_notify(struct gntdev_priv *priv, void __user *u) |
590 | { | |
591 | struct ioctl_gntdev_unmap_notify op; | |
592 | struct grant_map *map; | |
593 | int rc; | |
594 | ||
595 | if (copy_from_user(&op, u, sizeof(op))) | |
596 | return -EFAULT; | |
597 | ||
598 | if (op.action & ~(UNMAP_NOTIFY_CLEAR_BYTE|UNMAP_NOTIFY_SEND_EVENT)) | |
599 | return -EINVAL; | |
600 | ||
601 | spin_lock(&priv->lock); | |
602 | ||
603 | list_for_each_entry(map, &priv->maps, next) { | |
604 | uint64_t begin = map->index << PAGE_SHIFT; | |
605 | uint64_t end = (map->index + map->count) << PAGE_SHIFT; | |
606 | if (op.index >= begin && op.index < end) | |
607 | goto found; | |
608 | } | |
609 | rc = -ENOENT; | |
610 | goto unlock_out; | |
611 | ||
612 | found: | |
9960be97 DDG |
613 | if ((op.action & UNMAP_NOTIFY_CLEAR_BYTE) && |
614 | (map->flags & GNTMAP_readonly)) { | |
615 | rc = -EINVAL; | |
616 | goto unlock_out; | |
617 | } | |
618 | ||
bdc612dc DDG |
619 | map->notify.flags = op.action; |
620 | map->notify.addr = op.index - (map->index << PAGE_SHIFT); | |
621 | map->notify.event = op.event_channel_port; | |
622 | rc = 0; | |
623 | unlock_out: | |
624 | spin_unlock(&priv->lock); | |
625 | return rc; | |
626 | } | |
627 | ||
ab31523c GH |
628 | static long gntdev_ioctl(struct file *flip, |
629 | unsigned int cmd, unsigned long arg) | |
630 | { | |
631 | struct gntdev_priv *priv = flip->private_data; | |
632 | void __user *ptr = (void __user *)arg; | |
633 | ||
634 | switch (cmd) { | |
635 | case IOCTL_GNTDEV_MAP_GRANT_REF: | |
636 | return gntdev_ioctl_map_grant_ref(priv, ptr); | |
637 | ||
638 | case IOCTL_GNTDEV_UNMAP_GRANT_REF: | |
639 | return gntdev_ioctl_unmap_grant_ref(priv, ptr); | |
640 | ||
641 | case IOCTL_GNTDEV_GET_OFFSET_FOR_VADDR: | |
642 | return gntdev_ioctl_get_offset_for_vaddr(priv, ptr); | |
643 | ||
bdc612dc DDG |
644 | case IOCTL_GNTDEV_SET_UNMAP_NOTIFY: |
645 | return gntdev_ioctl_notify(priv, ptr); | |
646 | ||
ab31523c GH |
647 | default: |
648 | pr_debug("priv %p, unknown cmd %x\n", priv, cmd); | |
649 | return -ENOIOCTLCMD; | |
650 | } | |
651 | ||
652 | return 0; | |
653 | } | |
654 | ||
655 | static int gntdev_mmap(struct file *flip, struct vm_area_struct *vma) | |
656 | { | |
657 | struct gntdev_priv *priv = flip->private_data; | |
658 | int index = vma->vm_pgoff; | |
659 | int count = (vma->vm_end - vma->vm_start) >> PAGE_SHIFT; | |
660 | struct grant_map *map; | |
aab8f11a | 661 | int i, err = -EINVAL; |
ab31523c GH |
662 | |
663 | if ((vma->vm_flags & VM_WRITE) && !(vma->vm_flags & VM_SHARED)) | |
664 | return -EINVAL; | |
665 | ||
666 | pr_debug("map %d+%d at %lx (pgoff %lx)\n", | |
667 | index, count, vma->vm_start, vma->vm_pgoff); | |
668 | ||
669 | spin_lock(&priv->lock); | |
670 | map = gntdev_find_map_index(priv, index, count); | |
671 | if (!map) | |
672 | goto unlock_out; | |
aab8f11a | 673 | if (use_ptemod && map->vma) |
ab31523c | 674 | goto unlock_out; |
aab8f11a | 675 | if (use_ptemod && priv->mm != vma->vm_mm) { |
ab31523c GH |
676 | printk(KERN_WARNING "Huh? Other mm?\n"); |
677 | goto unlock_out; | |
678 | } | |
679 | ||
68b025c8 DDG |
680 | atomic_inc(&map->users); |
681 | ||
ab31523c GH |
682 | vma->vm_ops = &gntdev_vmops; |
683 | ||
8d3eaea2 | 684 | vma->vm_flags |= VM_RESERVED|VM_DONTCOPY|VM_DONTEXPAND|VM_PFNMAP; |
ab31523c GH |
685 | |
686 | vma->vm_private_data = map; | |
ab31523c | 687 | |
aab8f11a DDG |
688 | if (use_ptemod) |
689 | map->vma = vma; | |
690 | ||
12996fc3 DDG |
691 | if (map->flags) { |
692 | if ((vma->vm_flags & VM_WRITE) && | |
693 | (map->flags & GNTMAP_readonly)) | |
694 | return -EINVAL; | |
695 | } else { | |
696 | map->flags = GNTMAP_host_map; | |
697 | if (!(vma->vm_flags & VM_WRITE)) | |
698 | map->flags |= GNTMAP_readonly; | |
699 | } | |
ab31523c | 700 | |
f0a70c88 DDG |
701 | spin_unlock(&priv->lock); |
702 | ||
aab8f11a DDG |
703 | if (use_ptemod) { |
704 | err = apply_to_page_range(vma->vm_mm, vma->vm_start, | |
705 | vma->vm_end - vma->vm_start, | |
706 | find_grant_ptes, map); | |
707 | if (err) { | |
708 | printk(KERN_WARNING "find_grant_ptes() failure.\n"); | |
90b6f305 | 709 | goto out_put_map; |
aab8f11a | 710 | } |
ab31523c GH |
711 | } |
712 | ||
713 | err = map_grant_pages(map); | |
90b6f305 DDG |
714 | if (err) |
715 | goto out_put_map; | |
f0a70c88 | 716 | |
aab8f11a DDG |
717 | if (!use_ptemod) { |
718 | for (i = 0; i < count; i++) { | |
719 | err = vm_insert_page(vma, vma->vm_start + i*PAGE_SIZE, | |
720 | map->pages[i]); | |
721 | if (err) | |
90b6f305 | 722 | goto out_put_map; |
aab8f11a DDG |
723 | } |
724 | } | |
725 | ||
f0a70c88 DDG |
726 | return 0; |
727 | ||
ab31523c GH |
728 | unlock_out: |
729 | spin_unlock(&priv->lock); | |
730 | return err; | |
90b6f305 DDG |
731 | |
732 | out_put_map: | |
84e4075d DDG |
733 | if (use_ptemod) |
734 | map->vma = NULL; | |
90b6f305 DDG |
735 | gntdev_put_map(map); |
736 | return err; | |
ab31523c GH |
737 | } |
738 | ||
739 | static const struct file_operations gntdev_fops = { | |
740 | .owner = THIS_MODULE, | |
741 | .open = gntdev_open, | |
742 | .release = gntdev_release, | |
743 | .mmap = gntdev_mmap, | |
744 | .unlocked_ioctl = gntdev_ioctl | |
745 | }; | |
746 | ||
747 | static struct miscdevice gntdev_miscdev = { | |
748 | .minor = MISC_DYNAMIC_MINOR, | |
749 | .name = "xen/gntdev", | |
750 | .fops = &gntdev_fops, | |
751 | }; | |
752 | ||
753 | /* ------------------------------------------------------------------ */ | |
754 | ||
755 | static int __init gntdev_init(void) | |
756 | { | |
757 | int err; | |
758 | ||
759 | if (!xen_domain()) | |
760 | return -ENODEV; | |
761 | ||
aab8f11a DDG |
762 | use_ptemod = xen_pv_domain(); |
763 | ||
ab31523c GH |
764 | err = misc_register(&gntdev_miscdev); |
765 | if (err != 0) { | |
766 | printk(KERN_ERR "Could not register gntdev device\n"); | |
767 | return err; | |
768 | } | |
769 | return 0; | |
770 | } | |
771 | ||
772 | static void __exit gntdev_exit(void) | |
773 | { | |
774 | misc_deregister(&gntdev_miscdev); | |
775 | } | |
776 | ||
777 | module_init(gntdev_init); | |
778 | module_exit(gntdev_exit); | |
779 | ||
780 | /* ------------------------------------------------------------------ */ |