blob: cdc28dc8b5d966d848dfe6c0b48efa95225698da [file] [log] [blame]
Gerd Hoffmannab315232010-12-14 18:40:46 +00001/******************************************************************************
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>
Daniel De Graafaab8f112011-02-03 12:19:02 -050035#include <linux/highmem.h>
Gerd Hoffmannab315232010-12-14 18:40:46 +000036
37#include <xen/xen.h>
38#include <xen/grant_table.h>
39#include <xen/gntdev.h>
Daniel De Graafbdc612d2011-02-03 12:19:04 -050040#include <xen/events.h>
Gerd Hoffmannab315232010-12-14 18:40:46 +000041#include <asm/xen/hypervisor.h>
42#include <asm/xen/hypercall.h>
43#include <asm/xen/page.h>
44
45MODULE_LICENSE("GPL");
46MODULE_AUTHOR("Derek G. Murray <Derek.Murray@cl.cam.ac.uk>, "
47 "Gerd Hoffmann <kraxel@redhat.com>");
48MODULE_DESCRIPTION("User-space granted page access driver");
49
Daniel De Graafef910822011-02-03 12:18:59 -050050static int limit = 1024*1024;
Gerd Hoffmannab315232010-12-14 18:40:46 +000051module_param(limit, int, 0644);
Daniel De Graafef910822011-02-03 12:18:59 -050052MODULE_PARM_DESC(limit, "Maximum number of grants that may be mapped by "
53 "the gntdev device");
54
55static atomic_t pages_mapped = ATOMIC_INIT(0);
Gerd Hoffmannab315232010-12-14 18:40:46 +000056
Daniel De Graafaab8f112011-02-03 12:19:02 -050057static int use_ptemod;
58
Gerd Hoffmannab315232010-12-14 18:40:46 +000059struct gntdev_priv {
60 struct list_head maps;
Gerd Hoffmannab315232010-12-14 18:40:46 +000061 /* lock protects maps from concurrent changes */
62 spinlock_t lock;
63 struct mm_struct *mm;
64 struct mmu_notifier mn;
65};
66
Daniel De Graafbdc612d2011-02-03 12:19:04 -050067struct unmap_notify {
68 int flags;
69 /* Address relative to the start of the grant_map */
70 int addr;
71 int event;
72};
73
Gerd Hoffmannab315232010-12-14 18:40:46 +000074struct grant_map {
75 struct list_head next;
Gerd Hoffmannab315232010-12-14 18:40:46 +000076 struct vm_area_struct *vma;
77 int index;
78 int count;
79 int flags;
Daniel De Graaf68b025c2011-02-03 12:19:01 -050080 atomic_t users;
Daniel De Graafbdc612d2011-02-03 12:19:04 -050081 struct unmap_notify notify;
Gerd Hoffmannab315232010-12-14 18:40:46 +000082 struct ioctl_gntdev_grant_ref *grants;
83 struct gnttab_map_grant_ref *map_ops;
84 struct gnttab_unmap_grant_ref *unmap_ops;
Stefano Stabellinia12b4eb2010-12-10 14:56:42 +000085 struct page **pages;
Gerd Hoffmannab315232010-12-14 18:40:46 +000086};
87
Daniel De Graafaab8f112011-02-03 12:19:02 -050088static int unmap_grant_pages(struct grant_map *map, int offset, int pages);
89
Gerd Hoffmannab315232010-12-14 18:40:46 +000090/* ------------------------------------------------------------------ */
91
92static void gntdev_print_maps(struct gntdev_priv *priv,
93 char *text, int text_index)
94{
95#ifdef DEBUG
96 struct grant_map *map;
97
Daniel De Graafef910822011-02-03 12:18:59 -050098 pr_debug("%s: maps list (priv %p)\n", __func__, priv);
Gerd Hoffmannab315232010-12-14 18:40:46 +000099 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
106static struct grant_map *gntdev_alloc_map(struct gntdev_priv *priv, int count)
107{
108 struct grant_map *add;
Stefano Stabellinia12b4eb2010-12-10 14:56:42 +0000109 int i;
Gerd Hoffmannab315232010-12-14 18:40:46 +0000110
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);
Stefano Stabellinia12b4eb2010-12-10 14:56:42 +0000118 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)
Gerd Hoffmannab315232010-12-14 18:40:46 +0000123 goto err;
124
Stefano Stabellinia12b4eb2010-12-10 14:56:42 +0000125 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;
Daniel De Graaf77c35ac2011-02-23 08:11:35 -0500129 add->map_ops[i].handle = -1;
130 add->unmap_ops[i].handle = -1;
Stefano Stabellinia12b4eb2010-12-10 14:56:42 +0000131 }
132
Gerd Hoffmannab315232010-12-14 18:40:46 +0000133 add->index = 0;
134 add->count = count;
Daniel De Graaf68b025c2011-02-03 12:19:01 -0500135 atomic_set(&add->users, 1);
Gerd Hoffmannab315232010-12-14 18:40:46 +0000136
Gerd Hoffmannab315232010-12-14 18:40:46 +0000137 return add;
138
139err:
Stefano Stabellinia12b4eb2010-12-10 14:56:42 +0000140 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);
Gerd Hoffmannab315232010-12-14 18:40:46 +0000146 kfree(add->grants);
147 kfree(add->map_ops);
148 kfree(add->unmap_ops);
149 kfree(add);
150 return NULL;
151}
152
153static 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
166done:
Gerd Hoffmannab315232010-12-14 18:40:46 +0000167 gntdev_print_maps(priv, "[new]", add->index);
168}
169
170static 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;
Daniel De Graafbdc612d2011-02-03 12:19:04 -0500178 if (count && map->count != count)
Gerd Hoffmannab315232010-12-14 18:40:46 +0000179 continue;
180 return map;
181 }
182 return NULL;
183}
184
Daniel De Graaf68b025c2011-02-03 12:19:01 -0500185static void gntdev_put_map(struct grant_map *map)
Gerd Hoffmannab315232010-12-14 18:40:46 +0000186{
Stefano Stabellinia12b4eb2010-12-10 14:56:42 +0000187 int i;
188
Gerd Hoffmannab315232010-12-14 18:40:46 +0000189 if (!map)
190 return;
Stefano Stabellinia12b4eb2010-12-10 14:56:42 +0000191
Daniel De Graaf68b025c2011-02-03 12:19:01 -0500192 if (!atomic_dec_and_test(&map->users))
193 return;
194
195 atomic_sub(map->count, &pages_mapped);
196
Daniel De Graafbdc612d2011-02-03 12:19:04 -0500197 if (map->notify.flags & UNMAP_NOTIFY_SEND_EVENT) {
198 notify_remote_via_evtchn(map->notify.event);
199 }
200
Daniel De Graafaab8f112011-02-03 12:19:02 -0500201 if (map->pages) {
202 if (!use_ptemod)
203 unmap_grant_pages(map, 0, map->count);
204
Stefano Stabellinia12b4eb2010-12-10 14:56:42 +0000205 for (i = 0; i < map->count; i++) {
Daniel De Graafaab8f112011-02-03 12:19:02 -0500206 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)
Stefano Stabellinia12b4eb2010-12-10 14:56:42 +0000223 __free_page(map->pages[i]);
Daniel De Graafaab8f112011-02-03 12:19:02 -0500224 else
225 pr_debug("Discard page %d=%ld\n", i,
226 page_to_pfn(map->pages[i]));
Stefano Stabellinia12b4eb2010-12-10 14:56:42 +0000227 }
Daniel De Graafaab8f112011-02-03 12:19:02 -0500228 }
Stefano Stabellinia12b4eb2010-12-10 14:56:42 +0000229 kfree(map->pages);
Gerd Hoffmannab315232010-12-14 18:40:46 +0000230 kfree(map->grants);
231 kfree(map->map_ops);
232 kfree(map->unmap_ops);
233 kfree(map);
234}
235
236/* ------------------------------------------------------------------ */
237
238static 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;
Daniel De Graafaab8f112011-02-03 12:19:02 -0500243 int flags = map->flags | GNTMAP_application_map | GNTMAP_contains_pte;
Gerd Hoffmannab315232010-12-14 18:40:46 +0000244 u64 pte_maddr;
245
246 BUG_ON(pgnr >= map->count);
Jeremy Fitzhardingeba5d1012010-12-08 10:54:32 -0800247 pte_maddr = arbitrary_virt_to_machine(pte).maddr;
248
Daniel De Graafaab8f112011-02-03 12:19:02 -0500249 gnttab_set_map_op(&map->map_ops[pgnr], pte_maddr, flags,
Gerd Hoffmannab315232010-12-14 18:40:46 +0000250 map->grants[pgnr].ref,
251 map->grants[pgnr].domid);
Daniel De Graafaab8f112011-02-03 12:19:02 -0500252 gnttab_set_unmap_op(&map->unmap_ops[pgnr], pte_maddr, flags,
Daniel De Graaf77c35ac2011-02-23 08:11:35 -0500253 -1 /* handle */);
Gerd Hoffmannab315232010-12-14 18:40:46 +0000254 return 0;
255}
256
257static int map_grant_pages(struct grant_map *map)
258{
259 int i, err = 0;
Daniel De Graafaab8f112011-02-03 12:19:02 -0500260 phys_addr_t addr;
261
262 if (!use_ptemod) {
Daniel De Graaf12996fc2011-02-09 16:11:32 -0500263 /* Note: it could already be mapped */
Daniel De Graaf77c35ac2011-02-23 08:11:35 -0500264 if (map->map_ops[0].handle != -1)
Daniel De Graaf12996fc2011-02-09 16:11:32 -0500265 return 0;
Daniel De Graafaab8f112011-02-03 12:19:02 -0500266 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,
Daniel De Graaf77c35ac2011-02-23 08:11:35 -0500273 map->flags, -1 /* handle */);
Daniel De Graafaab8f112011-02-03 12:19:02 -0500274 }
275 }
Gerd Hoffmannab315232010-12-14 18:40:46 +0000276
277 pr_debug("map %d+%d\n", map->index, map->count);
Stefano Stabellinia12b4eb2010-12-10 14:56:42 +0000278 err = gnttab_map_refs(map->map_ops, map->pages, map->count);
Gerd Hoffmannab315232010-12-14 18:40:46 +0000279 if (err)
280 return err;
281
282 for (i = 0; i < map->count; i++) {
283 if (map->map_ops[i].status)
284 err = -EINVAL;
Daniel De Graaf77c35ac2011-02-23 08:11:35 -0500285 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 }
Gerd Hoffmannab315232010-12-14 18:40:46 +0000290 }
291 return err;
292}
293
Daniel De Graafb57c1862011-02-09 15:12:00 -0500294static int __unmap_grant_pages(struct grant_map *map, int offset, int pages)
Gerd Hoffmannab315232010-12-14 18:40:46 +0000295{
296 int i, err = 0;
297
Daniel De Graafbdc612d2011-02-03 12:19:04 -0500298 if (map->notify.flags & UNMAP_NOTIFY_CLEAR_BYTE) {
299 int pgno = (map->notify.addr >> PAGE_SHIFT);
Daniel De Graaf0ea22f02011-02-08 09:14:06 -0500300 if (pgno >= offset && pgno < offset + pages && use_ptemod) {
301 void __user *tmp;
302 tmp = map->vma->vm_start + map->notify.addr;
Daniel De Graaf9960be92011-02-09 18:15:50 -0500303 err = copy_to_user(tmp, &err, 1);
304 if (err)
305 return err;
Daniel De Graaf0ea22f02011-02-08 09:14:06 -0500306 map->notify.flags &= ~UNMAP_NOTIFY_CLEAR_BYTE;
307 } else if (pgno >= offset && pgno < offset + pages) {
Daniel De Graafbdc612d2011-02-03 12:19:04 -0500308 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
Daniel De Graaf0ea22f02011-02-08 09:14:06 -0500315 err = gnttab_unmap_refs(map->unmap_ops + offset, map->pages + offset, pages);
Gerd Hoffmannab315232010-12-14 18:40:46 +0000316 if (err)
317 return err;
318
319 for (i = 0; i < pages; i++) {
320 if (map->unmap_ops[offset+i].status)
321 err = -EINVAL;
Daniel De Graaf77c35ac2011-02-23 08:11:35 -0500322 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;
Gerd Hoffmannab315232010-12-14 18:40:46 +0000326 }
327 return err;
328}
329
Daniel De Graafb57c1862011-02-09 15:12:00 -0500330static 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) {
Daniel De Graaf77c35ac2011-02-23 08:11:35 -0500340 while (pages && map->unmap_ops[offset].handle == -1) {
Daniel De Graafb57c1862011-02-09 15:12:00 -0500341 offset++;
342 pages--;
343 }
344 range = 0;
345 while (range < pages) {
Daniel De Graaf77c35ac2011-02-23 08:11:35 -0500346 if (map->unmap_ops[offset+range].handle == -1) {
Daniel De Graafb57c1862011-02-09 15:12:00 -0500347 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
Gerd Hoffmannab315232010-12-14 18:40:46 +0000360/* ------------------------------------------------------------------ */
361
362static 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);
Gerd Hoffmannab315232010-12-14 18:40:46 +0000367 map->vma = NULL;
368 vma->vm_private_data = NULL;
Daniel De Graaf68b025c2011-02-03 12:19:01 -0500369 gntdev_put_map(map);
Gerd Hoffmannab315232010-12-14 18:40:46 +0000370}
371
Gerd Hoffmannab315232010-12-14 18:40:46 +0000372static struct vm_operations_struct gntdev_vmops = {
373 .close = gntdev_vma_close,
Gerd Hoffmannab315232010-12-14 18:40:46 +0000374};
375
376/* ------------------------------------------------------------------ */
377
378static 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;
Gerd Hoffmannab315232010-12-14 18:40:46 +0000391 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
409static 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
416static 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
436struct 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
444static 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);
Gerd Hoffmannab315232010-12-14 18:40:46 +0000455
Daniel De Graafaab8f112011-02-03 12:19:02 -0500456 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);
Gerd Hoffmannab315232010-12-14 18:40:46 +0000465 }
Gerd Hoffmannab315232010-12-14 18:40:46 +0000466
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
478static int gntdev_release(struct inode *inode, struct file *flip)
479{
480 struct gntdev_priv *priv = flip->private_data;
481 struct grant_map *map;
Gerd Hoffmannab315232010-12-14 18:40:46 +0000482
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);
Daniel De Graaf68b025c2011-02-03 12:19:01 -0500488 list_del(&map->next);
489 gntdev_put_map(map);
Gerd Hoffmannab315232010-12-14 18:40:46 +0000490 }
491 spin_unlock(&priv->lock);
492
Daniel De Graafaab8f112011-02-03 12:19:02 -0500493 if (use_ptemod)
494 mmu_notifier_unregister(&priv->mn, priv->mm);
Gerd Hoffmannab315232010-12-14 18:40:46 +0000495 kfree(priv);
496 return 0;
497}
498
499static 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;
Gerd Hoffmannab315232010-12-14 18:40:46 +0000511
512 err = -ENOMEM;
513 map = gntdev_alloc_map(priv, op.count);
514 if (!map)
515 return err;
Daniel De Graafef910822011-02-03 12:18:59 -0500516
Daniel De Graaf68b025c2011-02-03 12:19:01 -0500517 if (unlikely(atomic_add_return(op.count, &pages_mapped) > limit)) {
518 pr_debug("can't map: over limit\n");
519 gntdev_put_map(map);
Gerd Hoffmannab315232010-12-14 18:40:46 +0000520 return err;
521 }
522
Daniel De Graaf68b025c2011-02-03 12:19:01 -0500523 if (copy_from_user(map->grants, &u->refs,
524 sizeof(map->grants[0]) * op.count) != 0) {
525 gntdev_put_map(map);
Daniel De Graafef910822011-02-03 12:18:59 -0500526 return err;
527 }
528
Gerd Hoffmannab315232010-12-14 18:40:46 +0000529 spin_lock(&priv->lock);
530 gntdev_add_map(priv, map);
531 op.index = map->index << PAGE_SHIFT;
532 spin_unlock(&priv->lock);
533
Daniel De Graaf68b025c2011-02-03 12:19:01 -0500534 if (copy_to_user(u, &op, sizeof(op)) != 0)
535 return -EFAULT;
536
Gerd Hoffmannab315232010-12-14 18:40:46 +0000537 return 0;
538}
539
540static 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);
Daniel De Graaf68b025c2011-02-03 12:19:01 -0500553 if (map) {
554 list_del(&map->next);
555 gntdev_put_map(map);
556 err = 0;
557 }
Gerd Hoffmannab315232010-12-14 18:40:46 +0000558 spin_unlock(&priv->lock);
Gerd Hoffmannab315232010-12-14 18:40:46 +0000559 return err;
560}
561
562static 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;
Daniel De Graafa8792112011-02-03 12:19:00 -0500566 struct vm_area_struct *vma;
Gerd Hoffmannab315232010-12-14 18:40:46 +0000567 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
Daniel De Graafa8792112011-02-03 12:19:00 -0500573 vma = find_vma(current->mm, op.vaddr);
574 if (!vma || vma->vm_ops != &gntdev_vmops)
Gerd Hoffmannab315232010-12-14 18:40:46 +0000575 return -EINVAL;
Daniel De Graafa8792112011-02-03 12:19:00 -0500576
577 map = vma->vm_private_data;
578 if (!map)
579 return -EINVAL;
580
Gerd Hoffmannab315232010-12-14 18:40:46 +0000581 op.offset = map->index << PAGE_SHIFT;
582 op.count = map->count;
Gerd Hoffmannab315232010-12-14 18:40:46 +0000583
584 if (copy_to_user(u, &op, sizeof(op)) != 0)
585 return -EFAULT;
586 return 0;
587}
588
Daniel De Graafbdc612d2011-02-03 12:19:04 -0500589static 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:
Daniel De Graaf9960be92011-02-09 18:15:50 -0500613 if ((op.action & UNMAP_NOTIFY_CLEAR_BYTE) &&
614 (map->flags & GNTMAP_readonly)) {
615 rc = -EINVAL;
616 goto unlock_out;
617 }
618
Daniel De Graafbdc612d2011-02-03 12:19:04 -0500619 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
Gerd Hoffmannab315232010-12-14 18:40:46 +0000628static 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
Daniel De Graafbdc612d2011-02-03 12:19:04 -0500644 case IOCTL_GNTDEV_SET_UNMAP_NOTIFY:
645 return gntdev_ioctl_notify(priv, ptr);
646
Gerd Hoffmannab315232010-12-14 18:40:46 +0000647 default:
648 pr_debug("priv %p, unknown cmd %x\n", priv, cmd);
649 return -ENOIOCTLCMD;
650 }
651
652 return 0;
653}
654
655static 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;
Daniel De Graafaab8f112011-02-03 12:19:02 -0500661 int i, err = -EINVAL;
Gerd Hoffmannab315232010-12-14 18:40:46 +0000662
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;
Daniel De Graafaab8f112011-02-03 12:19:02 -0500673 if (use_ptemod && map->vma)
Gerd Hoffmannab315232010-12-14 18:40:46 +0000674 goto unlock_out;
Daniel De Graafaab8f112011-02-03 12:19:02 -0500675 if (use_ptemod && priv->mm != vma->vm_mm) {
Gerd Hoffmannab315232010-12-14 18:40:46 +0000676 printk(KERN_WARNING "Huh? Other mm?\n");
677 goto unlock_out;
678 }
679
Daniel De Graaf68b025c2011-02-03 12:19:01 -0500680 atomic_inc(&map->users);
681
Gerd Hoffmannab315232010-12-14 18:40:46 +0000682 vma->vm_ops = &gntdev_vmops;
683
Jeremy Fitzhardinge8d3eaea2010-11-11 14:39:12 -0800684 vma->vm_flags |= VM_RESERVED|VM_DONTCOPY|VM_DONTEXPAND|VM_PFNMAP;
Gerd Hoffmannab315232010-12-14 18:40:46 +0000685
686 vma->vm_private_data = map;
Gerd Hoffmannab315232010-12-14 18:40:46 +0000687
Daniel De Graafaab8f112011-02-03 12:19:02 -0500688 if (use_ptemod)
689 map->vma = vma;
690
Daniel De Graaf12996fc2011-02-09 16:11:32 -0500691 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 }
Gerd Hoffmannab315232010-12-14 18:40:46 +0000700
Daniel De Graaff0a70c82011-01-07 11:51:47 +0000701 spin_unlock(&priv->lock);
702
Daniel De Graafaab8f112011-02-03 12:19:02 -0500703 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");
Daniel De Graaf90b6f302011-02-03 14:16:54 -0500709 goto out_put_map;
Daniel De Graafaab8f112011-02-03 12:19:02 -0500710 }
Gerd Hoffmannab315232010-12-14 18:40:46 +0000711 }
712
713 err = map_grant_pages(map);
Daniel De Graaf90b6f302011-02-03 14:16:54 -0500714 if (err)
715 goto out_put_map;
Daniel De Graaff0a70c82011-01-07 11:51:47 +0000716
Daniel De Graafaab8f112011-02-03 12:19:02 -0500717 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)
Daniel De Graaf90b6f302011-02-03 14:16:54 -0500722 goto out_put_map;
Daniel De Graafaab8f112011-02-03 12:19:02 -0500723 }
724 }
725
Daniel De Graaff0a70c82011-01-07 11:51:47 +0000726 return 0;
727
Gerd Hoffmannab315232010-12-14 18:40:46 +0000728unlock_out:
729 spin_unlock(&priv->lock);
730 return err;
Daniel De Graaf90b6f302011-02-03 14:16:54 -0500731
732out_put_map:
Daniel De Graaf84e40752011-02-09 15:11:59 -0500733 if (use_ptemod)
734 map->vma = NULL;
Daniel De Graaf90b6f302011-02-03 14:16:54 -0500735 gntdev_put_map(map);
736 return err;
Gerd Hoffmannab315232010-12-14 18:40:46 +0000737}
738
739static 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
747static struct miscdevice gntdev_miscdev = {
748 .minor = MISC_DYNAMIC_MINOR,
749 .name = "xen/gntdev",
750 .fops = &gntdev_fops,
751};
752
753/* ------------------------------------------------------------------ */
754
755static int __init gntdev_init(void)
756{
757 int err;
758
759 if (!xen_domain())
760 return -ENODEV;
761
Daniel De Graafaab8f112011-02-03 12:19:02 -0500762 use_ptemod = xen_pv_domain();
763
Gerd Hoffmannab315232010-12-14 18:40:46 +0000764 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
772static void __exit gntdev_exit(void)
773{
774 misc_deregister(&gntdev_miscdev);
775}
776
777module_init(gntdev_init);
778module_exit(gntdev_exit);
779
780/* ------------------------------------------------------------------ */