]>
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; | |
80 | int is_mapped; | |
68b025c8 | 81 | atomic_t users; |
bdc612dc | 82 | struct unmap_notify notify; |
ab31523c GH |
83 | struct ioctl_gntdev_grant_ref *grants; |
84 | struct gnttab_map_grant_ref *map_ops; | |
85 | struct gnttab_unmap_grant_ref *unmap_ops; | |
a12b4eb3 | 86 | struct page **pages; |
ab31523c GH |
87 | }; |
88 | ||
aab8f11a DDG |
89 | static int unmap_grant_pages(struct grant_map *map, int offset, int pages); |
90 | ||
ab31523c GH |
91 | /* ------------------------------------------------------------------ */ |
92 | ||
93 | static void gntdev_print_maps(struct gntdev_priv *priv, | |
94 | char *text, int text_index) | |
95 | { | |
96 | #ifdef DEBUG | |
97 | struct grant_map *map; | |
98 | ||
ef91082e | 99 | pr_debug("%s: maps list (priv %p)\n", __func__, priv); |
ab31523c GH |
100 | list_for_each_entry(map, &priv->maps, next) |
101 | pr_debug(" index %2d, count %2d %s\n", | |
102 | map->index, map->count, | |
103 | map->index == text_index && text ? text : ""); | |
104 | #endif | |
105 | } | |
106 | ||
107 | static struct grant_map *gntdev_alloc_map(struct gntdev_priv *priv, int count) | |
108 | { | |
109 | struct grant_map *add; | |
a12b4eb3 | 110 | int i; |
ab31523c GH |
111 | |
112 | add = kzalloc(sizeof(struct grant_map), GFP_KERNEL); | |
113 | if (NULL == add) | |
114 | return NULL; | |
115 | ||
116 | add->grants = kzalloc(sizeof(add->grants[0]) * count, GFP_KERNEL); | |
117 | add->map_ops = kzalloc(sizeof(add->map_ops[0]) * count, GFP_KERNEL); | |
118 | add->unmap_ops = kzalloc(sizeof(add->unmap_ops[0]) * count, GFP_KERNEL); | |
a12b4eb3 SS |
119 | add->pages = kzalloc(sizeof(add->pages[0]) * count, GFP_KERNEL); |
120 | if (NULL == add->grants || | |
121 | NULL == add->map_ops || | |
122 | NULL == add->unmap_ops || | |
123 | NULL == add->pages) | |
ab31523c GH |
124 | goto err; |
125 | ||
a12b4eb3 SS |
126 | for (i = 0; i < count; i++) { |
127 | add->pages[i] = alloc_page(GFP_KERNEL | __GFP_HIGHMEM); | |
128 | if (add->pages[i] == NULL) | |
129 | goto err; | |
130 | } | |
131 | ||
ab31523c GH |
132 | add->index = 0; |
133 | add->count = count; | |
68b025c8 | 134 | atomic_set(&add->users, 1); |
ab31523c | 135 | |
ab31523c GH |
136 | return add; |
137 | ||
138 | err: | |
a12b4eb3 SS |
139 | if (add->pages) |
140 | for (i = 0; i < count; i++) { | |
141 | if (add->pages[i]) | |
142 | __free_page(add->pages[i]); | |
143 | } | |
144 | kfree(add->pages); | |
ab31523c GH |
145 | kfree(add->grants); |
146 | kfree(add->map_ops); | |
147 | kfree(add->unmap_ops); | |
148 | kfree(add); | |
149 | return NULL; | |
150 | } | |
151 | ||
152 | static void gntdev_add_map(struct gntdev_priv *priv, struct grant_map *add) | |
153 | { | |
154 | struct grant_map *map; | |
155 | ||
156 | list_for_each_entry(map, &priv->maps, next) { | |
157 | if (add->index + add->count < map->index) { | |
158 | list_add_tail(&add->next, &map->next); | |
159 | goto done; | |
160 | } | |
161 | add->index = map->index + map->count; | |
162 | } | |
163 | list_add_tail(&add->next, &priv->maps); | |
164 | ||
165 | done: | |
ab31523c GH |
166 | gntdev_print_maps(priv, "[new]", add->index); |
167 | } | |
168 | ||
169 | static struct grant_map *gntdev_find_map_index(struct gntdev_priv *priv, | |
170 | int index, int count) | |
171 | { | |
172 | struct grant_map *map; | |
173 | ||
174 | list_for_each_entry(map, &priv->maps, next) { | |
175 | if (map->index != index) | |
176 | continue; | |
bdc612dc | 177 | if (count && map->count != count) |
ab31523c GH |
178 | continue; |
179 | return map; | |
180 | } | |
181 | return NULL; | |
182 | } | |
183 | ||
68b025c8 | 184 | static void gntdev_put_map(struct grant_map *map) |
ab31523c | 185 | { |
a12b4eb3 SS |
186 | int i; |
187 | ||
ab31523c GH |
188 | if (!map) |
189 | return; | |
a12b4eb3 | 190 | |
68b025c8 DDG |
191 | if (!atomic_dec_and_test(&map->users)) |
192 | return; | |
193 | ||
194 | atomic_sub(map->count, &pages_mapped); | |
195 | ||
bdc612dc DDG |
196 | if (map->notify.flags & UNMAP_NOTIFY_SEND_EVENT) { |
197 | notify_remote_via_evtchn(map->notify.event); | |
198 | } | |
199 | ||
aab8f11a DDG |
200 | if (map->pages) { |
201 | if (!use_ptemod) | |
202 | unmap_grant_pages(map, 0, map->count); | |
203 | ||
a12b4eb3 | 204 | for (i = 0; i < map->count; i++) { |
aab8f11a DDG |
205 | uint32_t check, *tmp; |
206 | if (!map->pages[i]) | |
207 | continue; | |
208 | /* XXX When unmapping in an HVM domain, Xen will | |
209 | * sometimes end up mapping the GFN to an invalid MFN. | |
210 | * In this case, writes will be discarded and reads will | |
211 | * return all 0xFF bytes. Leak these unusable GFNs | |
212 | * until Xen supports fixing their p2m mapping. | |
213 | * | |
214 | * Confirmed present in Xen 4.1-RC3 with HVM source | |
215 | */ | |
216 | tmp = kmap(map->pages[i]); | |
217 | *tmp = 0xdeaddead; | |
218 | mb(); | |
219 | check = *tmp; | |
220 | kunmap(map->pages[i]); | |
221 | if (check == 0xdeaddead) | |
a12b4eb3 | 222 | __free_page(map->pages[i]); |
aab8f11a DDG |
223 | else |
224 | pr_debug("Discard page %d=%ld\n", i, | |
225 | page_to_pfn(map->pages[i])); | |
a12b4eb3 | 226 | } |
aab8f11a | 227 | } |
a12b4eb3 | 228 | kfree(map->pages); |
ab31523c GH |
229 | kfree(map->grants); |
230 | kfree(map->map_ops); | |
231 | kfree(map->unmap_ops); | |
232 | kfree(map); | |
233 | } | |
234 | ||
235 | /* ------------------------------------------------------------------ */ | |
236 | ||
237 | static int find_grant_ptes(pte_t *pte, pgtable_t token, | |
238 | unsigned long addr, void *data) | |
239 | { | |
240 | struct grant_map *map = data; | |
241 | unsigned int pgnr = (addr - map->vma->vm_start) >> PAGE_SHIFT; | |
aab8f11a | 242 | int flags = map->flags | GNTMAP_application_map | GNTMAP_contains_pte; |
ab31523c GH |
243 | u64 pte_maddr; |
244 | ||
245 | BUG_ON(pgnr >= map->count); | |
ba5d1012 JF |
246 | pte_maddr = arbitrary_virt_to_machine(pte).maddr; |
247 | ||
aab8f11a | 248 | gnttab_set_map_op(&map->map_ops[pgnr], pte_maddr, flags, |
ab31523c GH |
249 | map->grants[pgnr].ref, |
250 | map->grants[pgnr].domid); | |
aab8f11a | 251 | gnttab_set_unmap_op(&map->unmap_ops[pgnr], pte_maddr, flags, |
ab31523c GH |
252 | 0 /* handle */); |
253 | return 0; | |
254 | } | |
255 | ||
256 | static int map_grant_pages(struct grant_map *map) | |
257 | { | |
258 | int i, err = 0; | |
aab8f11a DDG |
259 | phys_addr_t addr; |
260 | ||
261 | if (!use_ptemod) { | |
262 | for (i = 0; i < map->count; i++) { | |
263 | addr = (phys_addr_t) | |
264 | pfn_to_kaddr(page_to_pfn(map->pages[i])); | |
265 | gnttab_set_map_op(&map->map_ops[i], addr, map->flags, | |
266 | map->grants[i].ref, | |
267 | map->grants[i].domid); | |
268 | gnttab_set_unmap_op(&map->unmap_ops[i], addr, | |
269 | map->flags, 0 /* handle */); | |
270 | } | |
271 | } | |
ab31523c GH |
272 | |
273 | pr_debug("map %d+%d\n", map->index, map->count); | |
a12b4eb3 | 274 | err = gnttab_map_refs(map->map_ops, map->pages, map->count); |
ab31523c GH |
275 | if (err) |
276 | return err; | |
277 | ||
278 | for (i = 0; i < map->count; i++) { | |
279 | if (map->map_ops[i].status) | |
280 | err = -EINVAL; | |
281 | map->unmap_ops[i].handle = map->map_ops[i].handle; | |
282 | } | |
283 | return err; | |
284 | } | |
285 | ||
286 | static int unmap_grant_pages(struct grant_map *map, int offset, int pages) | |
287 | { | |
288 | int i, err = 0; | |
289 | ||
bdc612dc DDG |
290 | if (map->notify.flags & UNMAP_NOTIFY_CLEAR_BYTE) { |
291 | int pgno = (map->notify.addr >> PAGE_SHIFT); | |
0ea22f07 DDG |
292 | if (pgno >= offset && pgno < offset + pages && use_ptemod) { |
293 | void __user *tmp; | |
294 | tmp = map->vma->vm_start + map->notify.addr; | |
295 | copy_to_user(tmp, &err, 1); | |
296 | map->notify.flags &= ~UNMAP_NOTIFY_CLEAR_BYTE; | |
297 | } else if (pgno >= offset && pgno < offset + pages) { | |
bdc612dc DDG |
298 | uint8_t *tmp = kmap(map->pages[pgno]); |
299 | tmp[map->notify.addr & (PAGE_SIZE-1)] = 0; | |
300 | kunmap(map->pages[pgno]); | |
301 | map->notify.flags &= ~UNMAP_NOTIFY_CLEAR_BYTE; | |
302 | } | |
303 | } | |
304 | ||
ab31523c | 305 | pr_debug("map %d+%d [%d+%d]\n", map->index, map->count, offset, pages); |
0ea22f07 | 306 | err = gnttab_unmap_refs(map->unmap_ops + offset, map->pages + offset, pages); |
ab31523c GH |
307 | if (err) |
308 | return err; | |
309 | ||
310 | for (i = 0; i < pages; i++) { | |
311 | if (map->unmap_ops[offset+i].status) | |
312 | err = -EINVAL; | |
313 | map->unmap_ops[offset+i].handle = 0; | |
314 | } | |
315 | return err; | |
316 | } | |
317 | ||
318 | /* ------------------------------------------------------------------ */ | |
319 | ||
320 | static void gntdev_vma_close(struct vm_area_struct *vma) | |
321 | { | |
322 | struct grant_map *map = vma->vm_private_data; | |
323 | ||
324 | pr_debug("close %p\n", vma); | |
325 | map->is_mapped = 0; | |
326 | map->vma = NULL; | |
327 | vma->vm_private_data = NULL; | |
68b025c8 | 328 | gntdev_put_map(map); |
ab31523c GH |
329 | } |
330 | ||
ab31523c GH |
331 | static struct vm_operations_struct gntdev_vmops = { |
332 | .close = gntdev_vma_close, | |
ab31523c GH |
333 | }; |
334 | ||
335 | /* ------------------------------------------------------------------ */ | |
336 | ||
337 | static void mn_invl_range_start(struct mmu_notifier *mn, | |
338 | struct mm_struct *mm, | |
339 | unsigned long start, unsigned long end) | |
340 | { | |
341 | struct gntdev_priv *priv = container_of(mn, struct gntdev_priv, mn); | |
342 | struct grant_map *map; | |
343 | unsigned long mstart, mend; | |
344 | int err; | |
345 | ||
346 | spin_lock(&priv->lock); | |
347 | list_for_each_entry(map, &priv->maps, next) { | |
348 | if (!map->vma) | |
349 | continue; | |
350 | if (!map->is_mapped) | |
351 | continue; | |
352 | if (map->vma->vm_start >= end) | |
353 | continue; | |
354 | if (map->vma->vm_end <= start) | |
355 | continue; | |
356 | mstart = max(start, map->vma->vm_start); | |
357 | mend = min(end, map->vma->vm_end); | |
358 | pr_debug("map %d+%d (%lx %lx), range %lx %lx, mrange %lx %lx\n", | |
359 | map->index, map->count, | |
360 | map->vma->vm_start, map->vma->vm_end, | |
361 | start, end, mstart, mend); | |
362 | err = unmap_grant_pages(map, | |
363 | (mstart - map->vma->vm_start) >> PAGE_SHIFT, | |
364 | (mend - mstart) >> PAGE_SHIFT); | |
365 | WARN_ON(err); | |
366 | } | |
367 | spin_unlock(&priv->lock); | |
368 | } | |
369 | ||
370 | static void mn_invl_page(struct mmu_notifier *mn, | |
371 | struct mm_struct *mm, | |
372 | unsigned long address) | |
373 | { | |
374 | mn_invl_range_start(mn, mm, address, address + PAGE_SIZE); | |
375 | } | |
376 | ||
377 | static void mn_release(struct mmu_notifier *mn, | |
378 | struct mm_struct *mm) | |
379 | { | |
380 | struct gntdev_priv *priv = container_of(mn, struct gntdev_priv, mn); | |
381 | struct grant_map *map; | |
382 | int err; | |
383 | ||
384 | spin_lock(&priv->lock); | |
385 | list_for_each_entry(map, &priv->maps, next) { | |
386 | if (!map->vma) | |
387 | continue; | |
388 | pr_debug("map %d+%d (%lx %lx)\n", | |
389 | map->index, map->count, | |
390 | map->vma->vm_start, map->vma->vm_end); | |
391 | err = unmap_grant_pages(map, /* offset */ 0, map->count); | |
392 | WARN_ON(err); | |
393 | } | |
394 | spin_unlock(&priv->lock); | |
395 | } | |
396 | ||
397 | struct mmu_notifier_ops gntdev_mmu_ops = { | |
398 | .release = mn_release, | |
399 | .invalidate_page = mn_invl_page, | |
400 | .invalidate_range_start = mn_invl_range_start, | |
401 | }; | |
402 | ||
403 | /* ------------------------------------------------------------------ */ | |
404 | ||
405 | static int gntdev_open(struct inode *inode, struct file *flip) | |
406 | { | |
407 | struct gntdev_priv *priv; | |
408 | int ret = 0; | |
409 | ||
410 | priv = kzalloc(sizeof(*priv), GFP_KERNEL); | |
411 | if (!priv) | |
412 | return -ENOMEM; | |
413 | ||
414 | INIT_LIST_HEAD(&priv->maps); | |
415 | spin_lock_init(&priv->lock); | |
ab31523c | 416 | |
aab8f11a DDG |
417 | if (use_ptemod) { |
418 | priv->mm = get_task_mm(current); | |
419 | if (!priv->mm) { | |
420 | kfree(priv); | |
421 | return -ENOMEM; | |
422 | } | |
423 | priv->mn.ops = &gntdev_mmu_ops; | |
424 | ret = mmu_notifier_register(&priv->mn, priv->mm); | |
425 | mmput(priv->mm); | |
ab31523c | 426 | } |
ab31523c GH |
427 | |
428 | if (ret) { | |
429 | kfree(priv); | |
430 | return ret; | |
431 | } | |
432 | ||
433 | flip->private_data = priv; | |
434 | pr_debug("priv %p\n", priv); | |
435 | ||
436 | return 0; | |
437 | } | |
438 | ||
439 | static int gntdev_release(struct inode *inode, struct file *flip) | |
440 | { | |
441 | struct gntdev_priv *priv = flip->private_data; | |
442 | struct grant_map *map; | |
ab31523c GH |
443 | |
444 | pr_debug("priv %p\n", priv); | |
445 | ||
446 | spin_lock(&priv->lock); | |
447 | while (!list_empty(&priv->maps)) { | |
448 | map = list_entry(priv->maps.next, struct grant_map, next); | |
68b025c8 DDG |
449 | list_del(&map->next); |
450 | gntdev_put_map(map); | |
ab31523c GH |
451 | } |
452 | spin_unlock(&priv->lock); | |
453 | ||
aab8f11a DDG |
454 | if (use_ptemod) |
455 | mmu_notifier_unregister(&priv->mn, priv->mm); | |
ab31523c GH |
456 | kfree(priv); |
457 | return 0; | |
458 | } | |
459 | ||
460 | static long gntdev_ioctl_map_grant_ref(struct gntdev_priv *priv, | |
461 | struct ioctl_gntdev_map_grant_ref __user *u) | |
462 | { | |
463 | struct ioctl_gntdev_map_grant_ref op; | |
464 | struct grant_map *map; | |
465 | int err; | |
466 | ||
467 | if (copy_from_user(&op, u, sizeof(op)) != 0) | |
468 | return -EFAULT; | |
469 | pr_debug("priv %p, add %d\n", priv, op.count); | |
470 | if (unlikely(op.count <= 0)) | |
471 | return -EINVAL; | |
ab31523c GH |
472 | |
473 | err = -ENOMEM; | |
474 | map = gntdev_alloc_map(priv, op.count); | |
475 | if (!map) | |
476 | return err; | |
ef91082e | 477 | |
68b025c8 DDG |
478 | if (unlikely(atomic_add_return(op.count, &pages_mapped) > limit)) { |
479 | pr_debug("can't map: over limit\n"); | |
480 | gntdev_put_map(map); | |
ab31523c GH |
481 | return err; |
482 | } | |
483 | ||
68b025c8 DDG |
484 | if (copy_from_user(map->grants, &u->refs, |
485 | sizeof(map->grants[0]) * op.count) != 0) { | |
486 | gntdev_put_map(map); | |
ef91082e DDG |
487 | return err; |
488 | } | |
489 | ||
ab31523c GH |
490 | spin_lock(&priv->lock); |
491 | gntdev_add_map(priv, map); | |
492 | op.index = map->index << PAGE_SHIFT; | |
493 | spin_unlock(&priv->lock); | |
494 | ||
68b025c8 DDG |
495 | if (copy_to_user(u, &op, sizeof(op)) != 0) |
496 | return -EFAULT; | |
497 | ||
ab31523c GH |
498 | return 0; |
499 | } | |
500 | ||
501 | static long gntdev_ioctl_unmap_grant_ref(struct gntdev_priv *priv, | |
502 | struct ioctl_gntdev_unmap_grant_ref __user *u) | |
503 | { | |
504 | struct ioctl_gntdev_unmap_grant_ref op; | |
505 | struct grant_map *map; | |
506 | int err = -ENOENT; | |
507 | ||
508 | if (copy_from_user(&op, u, sizeof(op)) != 0) | |
509 | return -EFAULT; | |
510 | pr_debug("priv %p, del %d+%d\n", priv, (int)op.index, (int)op.count); | |
511 | ||
512 | spin_lock(&priv->lock); | |
513 | map = gntdev_find_map_index(priv, op.index >> PAGE_SHIFT, op.count); | |
68b025c8 DDG |
514 | if (map) { |
515 | list_del(&map->next); | |
516 | gntdev_put_map(map); | |
517 | err = 0; | |
518 | } | |
ab31523c | 519 | spin_unlock(&priv->lock); |
ab31523c GH |
520 | return err; |
521 | } | |
522 | ||
523 | static long gntdev_ioctl_get_offset_for_vaddr(struct gntdev_priv *priv, | |
524 | struct ioctl_gntdev_get_offset_for_vaddr __user *u) | |
525 | { | |
526 | struct ioctl_gntdev_get_offset_for_vaddr op; | |
a879211b | 527 | struct vm_area_struct *vma; |
ab31523c GH |
528 | struct grant_map *map; |
529 | ||
530 | if (copy_from_user(&op, u, sizeof(op)) != 0) | |
531 | return -EFAULT; | |
532 | pr_debug("priv %p, offset for vaddr %lx\n", priv, (unsigned long)op.vaddr); | |
533 | ||
a879211b DDG |
534 | vma = find_vma(current->mm, op.vaddr); |
535 | if (!vma || vma->vm_ops != &gntdev_vmops) | |
ab31523c | 536 | return -EINVAL; |
a879211b DDG |
537 | |
538 | map = vma->vm_private_data; | |
539 | if (!map) | |
540 | return -EINVAL; | |
541 | ||
ab31523c GH |
542 | op.offset = map->index << PAGE_SHIFT; |
543 | op.count = map->count; | |
ab31523c GH |
544 | |
545 | if (copy_to_user(u, &op, sizeof(op)) != 0) | |
546 | return -EFAULT; | |
547 | return 0; | |
548 | } | |
549 | ||
bdc612dc DDG |
550 | static long gntdev_ioctl_notify(struct gntdev_priv *priv, void __user *u) |
551 | { | |
552 | struct ioctl_gntdev_unmap_notify op; | |
553 | struct grant_map *map; | |
554 | int rc; | |
555 | ||
556 | if (copy_from_user(&op, u, sizeof(op))) | |
557 | return -EFAULT; | |
558 | ||
559 | if (op.action & ~(UNMAP_NOTIFY_CLEAR_BYTE|UNMAP_NOTIFY_SEND_EVENT)) | |
560 | return -EINVAL; | |
561 | ||
562 | spin_lock(&priv->lock); | |
563 | ||
564 | list_for_each_entry(map, &priv->maps, next) { | |
565 | uint64_t begin = map->index << PAGE_SHIFT; | |
566 | uint64_t end = (map->index + map->count) << PAGE_SHIFT; | |
567 | if (op.index >= begin && op.index < end) | |
568 | goto found; | |
569 | } | |
570 | rc = -ENOENT; | |
571 | goto unlock_out; | |
572 | ||
573 | found: | |
574 | map->notify.flags = op.action; | |
575 | map->notify.addr = op.index - (map->index << PAGE_SHIFT); | |
576 | map->notify.event = op.event_channel_port; | |
577 | rc = 0; | |
578 | unlock_out: | |
579 | spin_unlock(&priv->lock); | |
580 | return rc; | |
581 | } | |
582 | ||
ab31523c GH |
583 | static long gntdev_ioctl(struct file *flip, |
584 | unsigned int cmd, unsigned long arg) | |
585 | { | |
586 | struct gntdev_priv *priv = flip->private_data; | |
587 | void __user *ptr = (void __user *)arg; | |
588 | ||
589 | switch (cmd) { | |
590 | case IOCTL_GNTDEV_MAP_GRANT_REF: | |
591 | return gntdev_ioctl_map_grant_ref(priv, ptr); | |
592 | ||
593 | case IOCTL_GNTDEV_UNMAP_GRANT_REF: | |
594 | return gntdev_ioctl_unmap_grant_ref(priv, ptr); | |
595 | ||
596 | case IOCTL_GNTDEV_GET_OFFSET_FOR_VADDR: | |
597 | return gntdev_ioctl_get_offset_for_vaddr(priv, ptr); | |
598 | ||
bdc612dc DDG |
599 | case IOCTL_GNTDEV_SET_UNMAP_NOTIFY: |
600 | return gntdev_ioctl_notify(priv, ptr); | |
601 | ||
ab31523c GH |
602 | default: |
603 | pr_debug("priv %p, unknown cmd %x\n", priv, cmd); | |
604 | return -ENOIOCTLCMD; | |
605 | } | |
606 | ||
607 | return 0; | |
608 | } | |
609 | ||
610 | static int gntdev_mmap(struct file *flip, struct vm_area_struct *vma) | |
611 | { | |
612 | struct gntdev_priv *priv = flip->private_data; | |
613 | int index = vma->vm_pgoff; | |
614 | int count = (vma->vm_end - vma->vm_start) >> PAGE_SHIFT; | |
615 | struct grant_map *map; | |
aab8f11a | 616 | int i, err = -EINVAL; |
ab31523c GH |
617 | |
618 | if ((vma->vm_flags & VM_WRITE) && !(vma->vm_flags & VM_SHARED)) | |
619 | return -EINVAL; | |
620 | ||
621 | pr_debug("map %d+%d at %lx (pgoff %lx)\n", | |
622 | index, count, vma->vm_start, vma->vm_pgoff); | |
623 | ||
624 | spin_lock(&priv->lock); | |
625 | map = gntdev_find_map_index(priv, index, count); | |
626 | if (!map) | |
627 | goto unlock_out; | |
aab8f11a | 628 | if (use_ptemod && map->vma) |
ab31523c | 629 | goto unlock_out; |
aab8f11a | 630 | if (use_ptemod && priv->mm != vma->vm_mm) { |
ab31523c GH |
631 | printk(KERN_WARNING "Huh? Other mm?\n"); |
632 | goto unlock_out; | |
633 | } | |
634 | ||
68b025c8 DDG |
635 | atomic_inc(&map->users); |
636 | ||
ab31523c GH |
637 | vma->vm_ops = &gntdev_vmops; |
638 | ||
8d3eaea2 | 639 | vma->vm_flags |= VM_RESERVED|VM_DONTCOPY|VM_DONTEXPAND|VM_PFNMAP; |
ab31523c GH |
640 | |
641 | vma->vm_private_data = map; | |
ab31523c | 642 | |
aab8f11a DDG |
643 | if (use_ptemod) |
644 | map->vma = vma; | |
645 | ||
646 | map->flags = GNTMAP_host_map; | |
ab31523c GH |
647 | if (!(vma->vm_flags & VM_WRITE)) |
648 | map->flags |= GNTMAP_readonly; | |
649 | ||
f0a70c88 DDG |
650 | spin_unlock(&priv->lock); |
651 | ||
aab8f11a DDG |
652 | if (use_ptemod) { |
653 | err = apply_to_page_range(vma->vm_mm, vma->vm_start, | |
654 | vma->vm_end - vma->vm_start, | |
655 | find_grant_ptes, map); | |
656 | if (err) { | |
657 | printk(KERN_WARNING "find_grant_ptes() failure.\n"); | |
90b6f305 | 658 | goto out_put_map; |
aab8f11a | 659 | } |
ab31523c GH |
660 | } |
661 | ||
662 | err = map_grant_pages(map); | |
90b6f305 DDG |
663 | if (err) |
664 | goto out_put_map; | |
f0a70c88 | 665 | |
ab31523c GH |
666 | map->is_mapped = 1; |
667 | ||
aab8f11a DDG |
668 | if (!use_ptemod) { |
669 | for (i = 0; i < count; i++) { | |
670 | err = vm_insert_page(vma, vma->vm_start + i*PAGE_SIZE, | |
671 | map->pages[i]); | |
672 | if (err) | |
90b6f305 | 673 | goto out_put_map; |
aab8f11a DDG |
674 | } |
675 | } | |
676 | ||
f0a70c88 DDG |
677 | return 0; |
678 | ||
ab31523c GH |
679 | unlock_out: |
680 | spin_unlock(&priv->lock); | |
681 | return err; | |
90b6f305 DDG |
682 | |
683 | out_put_map: | |
684 | gntdev_put_map(map); | |
685 | return err; | |
ab31523c GH |
686 | } |
687 | ||
688 | static const struct file_operations gntdev_fops = { | |
689 | .owner = THIS_MODULE, | |
690 | .open = gntdev_open, | |
691 | .release = gntdev_release, | |
692 | .mmap = gntdev_mmap, | |
693 | .unlocked_ioctl = gntdev_ioctl | |
694 | }; | |
695 | ||
696 | static struct miscdevice gntdev_miscdev = { | |
697 | .minor = MISC_DYNAMIC_MINOR, | |
698 | .name = "xen/gntdev", | |
699 | .fops = &gntdev_fops, | |
700 | }; | |
701 | ||
702 | /* ------------------------------------------------------------------ */ | |
703 | ||
704 | static int __init gntdev_init(void) | |
705 | { | |
706 | int err; | |
707 | ||
708 | if (!xen_domain()) | |
709 | return -ENODEV; | |
710 | ||
aab8f11a DDG |
711 | use_ptemod = xen_pv_domain(); |
712 | ||
ab31523c GH |
713 | err = misc_register(&gntdev_miscdev); |
714 | if (err != 0) { | |
715 | printk(KERN_ERR "Could not register gntdev device\n"); | |
716 | return err; | |
717 | } | |
718 | return 0; | |
719 | } | |
720 | ||
721 | static void __exit gntdev_exit(void) | |
722 | { | |
723 | misc_deregister(&gntdev_miscdev); | |
724 | } | |
725 | ||
726 | module_init(gntdev_init); | |
727 | module_exit(gntdev_exit); | |
728 | ||
729 | /* ------------------------------------------------------------------ */ |