Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 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> |
| 35 | |
| 36 | #include <xen/xen.h> |
| 37 | #include <xen/grant_table.h> |
| 38 | #include <xen/gntdev.h> |
| 39 | #include <asm/xen/hypervisor.h> |
| 40 | #include <asm/xen/hypercall.h> |
| 41 | #include <asm/xen/page.h> |
| 42 | |
| 43 | MODULE_LICENSE("GPL"); |
| 44 | MODULE_AUTHOR("Derek G. Murray <Derek.Murray@cl.cam.ac.uk>, " |
| 45 | "Gerd Hoffmann <kraxel@redhat.com>"); |
| 46 | MODULE_DESCRIPTION("User-space granted page access driver"); |
| 47 | |
| 48 | static int limit = 1024; |
| 49 | module_param(limit, int, 0644); |
| 50 | MODULE_PARM_DESC(limit, "Maximum number of grants that may be mapped at " |
| 51 | "once by a gntdev instance"); |
| 52 | |
| 53 | struct gntdev_priv { |
| 54 | struct list_head maps; |
| 55 | uint32_t used; |
| 56 | uint32_t limit; |
| 57 | /* lock protects maps from concurrent changes */ |
| 58 | spinlock_t lock; |
| 59 | struct mm_struct *mm; |
| 60 | struct mmu_notifier mn; |
| 61 | }; |
| 62 | |
| 63 | struct grant_map { |
| 64 | struct list_head next; |
| 65 | struct gntdev_priv *priv; |
| 66 | struct vm_area_struct *vma; |
| 67 | int index; |
| 68 | int count; |
| 69 | int flags; |
| 70 | int is_mapped; |
| 71 | struct ioctl_gntdev_grant_ref *grants; |
| 72 | struct gnttab_map_grant_ref *map_ops; |
| 73 | struct gnttab_unmap_grant_ref *unmap_ops; |
| 74 | }; |
| 75 | |
| 76 | /* ------------------------------------------------------------------ */ |
| 77 | |
| 78 | static void gntdev_print_maps(struct gntdev_priv *priv, |
| 79 | char *text, int text_index) |
| 80 | { |
| 81 | #ifdef DEBUG |
| 82 | struct grant_map *map; |
| 83 | |
| 84 | pr_debug("maps list (priv %p, usage %d/%d)\n", |
| 85 | priv, priv->used, priv->limit); |
| 86 | |
| 87 | list_for_each_entry(map, &priv->maps, next) |
| 88 | pr_debug(" index %2d, count %2d %s\n", |
| 89 | map->index, map->count, |
| 90 | map->index == text_index && text ? text : ""); |
| 91 | #endif |
| 92 | } |
| 93 | |
| 94 | static struct grant_map *gntdev_alloc_map(struct gntdev_priv *priv, int count) |
| 95 | { |
| 96 | struct grant_map *add; |
| 97 | |
| 98 | add = kzalloc(sizeof(struct grant_map), GFP_KERNEL); |
| 99 | if (NULL == add) |
| 100 | return NULL; |
| 101 | |
| 102 | add->grants = kzalloc(sizeof(add->grants[0]) * count, GFP_KERNEL); |
| 103 | add->map_ops = kzalloc(sizeof(add->map_ops[0]) * count, GFP_KERNEL); |
| 104 | add->unmap_ops = kzalloc(sizeof(add->unmap_ops[0]) * count, GFP_KERNEL); |
| 105 | if (NULL == add->grants || |
| 106 | NULL == add->map_ops || |
| 107 | NULL == add->unmap_ops) |
| 108 | goto err; |
| 109 | |
| 110 | add->index = 0; |
| 111 | add->count = count; |
| 112 | add->priv = priv; |
| 113 | |
| 114 | if (add->count + priv->used > priv->limit) |
| 115 | goto err; |
| 116 | |
| 117 | return add; |
| 118 | |
| 119 | err: |
| 120 | kfree(add->grants); |
| 121 | kfree(add->map_ops); |
| 122 | kfree(add->unmap_ops); |
| 123 | kfree(add); |
| 124 | return NULL; |
| 125 | } |
| 126 | |
| 127 | static void gntdev_add_map(struct gntdev_priv *priv, struct grant_map *add) |
| 128 | { |
| 129 | struct grant_map *map; |
| 130 | |
| 131 | list_for_each_entry(map, &priv->maps, next) { |
| 132 | if (add->index + add->count < map->index) { |
| 133 | list_add_tail(&add->next, &map->next); |
| 134 | goto done; |
| 135 | } |
| 136 | add->index = map->index + map->count; |
| 137 | } |
| 138 | list_add_tail(&add->next, &priv->maps); |
| 139 | |
| 140 | done: |
| 141 | priv->used += add->count; |
| 142 | gntdev_print_maps(priv, "[new]", add->index); |
| 143 | } |
| 144 | |
| 145 | static struct grant_map *gntdev_find_map_index(struct gntdev_priv *priv, |
| 146 | int index, int count) |
| 147 | { |
| 148 | struct grant_map *map; |
| 149 | |
| 150 | list_for_each_entry(map, &priv->maps, next) { |
| 151 | if (map->index != index) |
| 152 | continue; |
| 153 | if (map->count != count) |
| 154 | continue; |
| 155 | return map; |
| 156 | } |
| 157 | return NULL; |
| 158 | } |
| 159 | |
| 160 | static struct grant_map *gntdev_find_map_vaddr(struct gntdev_priv *priv, |
| 161 | unsigned long vaddr) |
| 162 | { |
| 163 | struct grant_map *map; |
| 164 | |
| 165 | list_for_each_entry(map, &priv->maps, next) { |
| 166 | if (!map->vma) |
| 167 | continue; |
| 168 | if (vaddr < map->vma->vm_start) |
| 169 | continue; |
| 170 | if (vaddr >= map->vma->vm_end) |
| 171 | continue; |
| 172 | return map; |
| 173 | } |
| 174 | return NULL; |
| 175 | } |
| 176 | |
| 177 | static int gntdev_del_map(struct grant_map *map) |
| 178 | { |
| 179 | int i; |
| 180 | |
| 181 | if (map->vma) |
| 182 | return -EBUSY; |
| 183 | for (i = 0; i < map->count; i++) |
| 184 | if (map->unmap_ops[i].handle) |
| 185 | return -EBUSY; |
| 186 | |
| 187 | map->priv->used -= map->count; |
| 188 | list_del(&map->next); |
| 189 | return 0; |
| 190 | } |
| 191 | |
| 192 | static void gntdev_free_map(struct grant_map *map) |
| 193 | { |
| 194 | if (!map) |
| 195 | return; |
| 196 | kfree(map->grants); |
| 197 | kfree(map->map_ops); |
| 198 | kfree(map->unmap_ops); |
| 199 | kfree(map); |
| 200 | } |
| 201 | |
| 202 | /* ------------------------------------------------------------------ */ |
| 203 | |
| 204 | static int find_grant_ptes(pte_t *pte, pgtable_t token, |
| 205 | unsigned long addr, void *data) |
| 206 | { |
| 207 | struct grant_map *map = data; |
| 208 | unsigned int pgnr = (addr - map->vma->vm_start) >> PAGE_SHIFT; |
| 209 | u64 pte_maddr; |
| 210 | |
| 211 | BUG_ON(pgnr >= map->count); |
Jeremy Fitzhardinge | ba5d101 | 2010-12-08 10:54:32 -0800 | [diff] [blame] | 212 | pte_maddr = arbitrary_virt_to_machine(pte).maddr; |
| 213 | |
Ian Campbell | 9329e76 | 2010-12-08 11:57:40 +0000 | [diff] [blame] | 214 | gnttab_set_map_op(&map->map_ops[pgnr], pte_maddr, |
| 215 | GNTMAP_contains_pte | map->flags, |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 216 | map->grants[pgnr].ref, |
| 217 | map->grants[pgnr].domid); |
Ian Campbell | 9329e76 | 2010-12-08 11:57:40 +0000 | [diff] [blame] | 218 | gnttab_set_unmap_op(&map->unmap_ops[pgnr], pte_maddr, |
| 219 | GNTMAP_contains_pte | map->flags, |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 220 | 0 /* handle */); |
| 221 | return 0; |
| 222 | } |
| 223 | |
| 224 | static int map_grant_pages(struct grant_map *map) |
| 225 | { |
| 226 | int i, err = 0; |
| 227 | |
| 228 | pr_debug("map %d+%d\n", map->index, map->count); |
| 229 | err = HYPERVISOR_grant_table_op(GNTTABOP_map_grant_ref, |
| 230 | map->map_ops, map->count); |
| 231 | if (err) |
| 232 | return err; |
| 233 | |
| 234 | for (i = 0; i < map->count; i++) { |
| 235 | if (map->map_ops[i].status) |
| 236 | err = -EINVAL; |
| 237 | map->unmap_ops[i].handle = map->map_ops[i].handle; |
| 238 | } |
| 239 | return err; |
| 240 | } |
| 241 | |
| 242 | static int unmap_grant_pages(struct grant_map *map, int offset, int pages) |
| 243 | { |
| 244 | int i, err = 0; |
| 245 | |
| 246 | pr_debug("map %d+%d [%d+%d]\n", map->index, map->count, offset, pages); |
| 247 | err = HYPERVISOR_grant_table_op(GNTTABOP_unmap_grant_ref, |
| 248 | map->unmap_ops + offset, pages); |
| 249 | if (err) |
| 250 | return err; |
| 251 | |
| 252 | for (i = 0; i < pages; i++) { |
| 253 | if (map->unmap_ops[offset+i].status) |
| 254 | err = -EINVAL; |
| 255 | map->unmap_ops[offset+i].handle = 0; |
| 256 | } |
| 257 | return err; |
| 258 | } |
| 259 | |
| 260 | /* ------------------------------------------------------------------ */ |
| 261 | |
| 262 | static void gntdev_vma_close(struct vm_area_struct *vma) |
| 263 | { |
| 264 | struct grant_map *map = vma->vm_private_data; |
| 265 | |
| 266 | pr_debug("close %p\n", vma); |
| 267 | map->is_mapped = 0; |
| 268 | map->vma = NULL; |
| 269 | vma->vm_private_data = NULL; |
| 270 | } |
| 271 | |
| 272 | static int gntdev_vma_fault(struct vm_area_struct *vma, struct vm_fault *vmf) |
| 273 | { |
| 274 | pr_debug("vaddr %p, pgoff %ld (shouldn't happen)\n", |
| 275 | vmf->virtual_address, vmf->pgoff); |
| 276 | vmf->flags = VM_FAULT_ERROR; |
| 277 | return 0; |
| 278 | } |
| 279 | |
| 280 | static struct vm_operations_struct gntdev_vmops = { |
| 281 | .close = gntdev_vma_close, |
| 282 | .fault = gntdev_vma_fault, |
| 283 | }; |
| 284 | |
| 285 | /* ------------------------------------------------------------------ */ |
| 286 | |
| 287 | static void mn_invl_range_start(struct mmu_notifier *mn, |
| 288 | struct mm_struct *mm, |
| 289 | unsigned long start, unsigned long end) |
| 290 | { |
| 291 | struct gntdev_priv *priv = container_of(mn, struct gntdev_priv, mn); |
| 292 | struct grant_map *map; |
| 293 | unsigned long mstart, mend; |
| 294 | int err; |
| 295 | |
| 296 | spin_lock(&priv->lock); |
| 297 | list_for_each_entry(map, &priv->maps, next) { |
| 298 | if (!map->vma) |
| 299 | continue; |
| 300 | if (!map->is_mapped) |
| 301 | continue; |
| 302 | if (map->vma->vm_start >= end) |
| 303 | continue; |
| 304 | if (map->vma->vm_end <= start) |
| 305 | continue; |
| 306 | mstart = max(start, map->vma->vm_start); |
| 307 | mend = min(end, map->vma->vm_end); |
| 308 | pr_debug("map %d+%d (%lx %lx), range %lx %lx, mrange %lx %lx\n", |
| 309 | map->index, map->count, |
| 310 | map->vma->vm_start, map->vma->vm_end, |
| 311 | start, end, mstart, mend); |
| 312 | err = unmap_grant_pages(map, |
| 313 | (mstart - map->vma->vm_start) >> PAGE_SHIFT, |
| 314 | (mend - mstart) >> PAGE_SHIFT); |
| 315 | WARN_ON(err); |
| 316 | } |
| 317 | spin_unlock(&priv->lock); |
| 318 | } |
| 319 | |
| 320 | static void mn_invl_page(struct mmu_notifier *mn, |
| 321 | struct mm_struct *mm, |
| 322 | unsigned long address) |
| 323 | { |
| 324 | mn_invl_range_start(mn, mm, address, address + PAGE_SIZE); |
| 325 | } |
| 326 | |
| 327 | static void mn_release(struct mmu_notifier *mn, |
| 328 | struct mm_struct *mm) |
| 329 | { |
| 330 | struct gntdev_priv *priv = container_of(mn, struct gntdev_priv, mn); |
| 331 | struct grant_map *map; |
| 332 | int err; |
| 333 | |
| 334 | spin_lock(&priv->lock); |
| 335 | list_for_each_entry(map, &priv->maps, next) { |
| 336 | if (!map->vma) |
| 337 | continue; |
| 338 | pr_debug("map %d+%d (%lx %lx)\n", |
| 339 | map->index, map->count, |
| 340 | map->vma->vm_start, map->vma->vm_end); |
| 341 | err = unmap_grant_pages(map, /* offset */ 0, map->count); |
| 342 | WARN_ON(err); |
| 343 | } |
| 344 | spin_unlock(&priv->lock); |
| 345 | } |
| 346 | |
| 347 | struct mmu_notifier_ops gntdev_mmu_ops = { |
| 348 | .release = mn_release, |
| 349 | .invalidate_page = mn_invl_page, |
| 350 | .invalidate_range_start = mn_invl_range_start, |
| 351 | }; |
| 352 | |
| 353 | /* ------------------------------------------------------------------ */ |
| 354 | |
| 355 | static int gntdev_open(struct inode *inode, struct file *flip) |
| 356 | { |
| 357 | struct gntdev_priv *priv; |
| 358 | int ret = 0; |
| 359 | |
| 360 | priv = kzalloc(sizeof(*priv), GFP_KERNEL); |
| 361 | if (!priv) |
| 362 | return -ENOMEM; |
| 363 | |
| 364 | INIT_LIST_HEAD(&priv->maps); |
| 365 | spin_lock_init(&priv->lock); |
| 366 | priv->limit = limit; |
| 367 | |
| 368 | priv->mm = get_task_mm(current); |
| 369 | if (!priv->mm) { |
| 370 | kfree(priv); |
| 371 | return -ENOMEM; |
| 372 | } |
| 373 | priv->mn.ops = &gntdev_mmu_ops; |
| 374 | ret = mmu_notifier_register(&priv->mn, priv->mm); |
| 375 | mmput(priv->mm); |
| 376 | |
| 377 | if (ret) { |
| 378 | kfree(priv); |
| 379 | return ret; |
| 380 | } |
| 381 | |
| 382 | flip->private_data = priv; |
| 383 | pr_debug("priv %p\n", priv); |
| 384 | |
| 385 | return 0; |
| 386 | } |
| 387 | |
| 388 | static int gntdev_release(struct inode *inode, struct file *flip) |
| 389 | { |
| 390 | struct gntdev_priv *priv = flip->private_data; |
| 391 | struct grant_map *map; |
| 392 | int err; |
| 393 | |
| 394 | pr_debug("priv %p\n", priv); |
| 395 | |
| 396 | spin_lock(&priv->lock); |
| 397 | while (!list_empty(&priv->maps)) { |
| 398 | map = list_entry(priv->maps.next, struct grant_map, next); |
| 399 | err = gntdev_del_map(map); |
| 400 | if (WARN_ON(err)) |
| 401 | gntdev_free_map(map); |
| 402 | |
| 403 | } |
| 404 | spin_unlock(&priv->lock); |
| 405 | |
| 406 | mmu_notifier_unregister(&priv->mn, priv->mm); |
| 407 | kfree(priv); |
| 408 | return 0; |
| 409 | } |
| 410 | |
| 411 | static long gntdev_ioctl_map_grant_ref(struct gntdev_priv *priv, |
| 412 | struct ioctl_gntdev_map_grant_ref __user *u) |
| 413 | { |
| 414 | struct ioctl_gntdev_map_grant_ref op; |
| 415 | struct grant_map *map; |
| 416 | int err; |
| 417 | |
| 418 | if (copy_from_user(&op, u, sizeof(op)) != 0) |
| 419 | return -EFAULT; |
| 420 | pr_debug("priv %p, add %d\n", priv, op.count); |
| 421 | if (unlikely(op.count <= 0)) |
| 422 | return -EINVAL; |
| 423 | if (unlikely(op.count > priv->limit)) |
| 424 | return -EINVAL; |
| 425 | |
| 426 | err = -ENOMEM; |
| 427 | map = gntdev_alloc_map(priv, op.count); |
| 428 | if (!map) |
| 429 | return err; |
| 430 | if (copy_from_user(map->grants, &u->refs, |
| 431 | sizeof(map->grants[0]) * op.count) != 0) { |
| 432 | gntdev_free_map(map); |
| 433 | return err; |
| 434 | } |
| 435 | |
| 436 | spin_lock(&priv->lock); |
| 437 | gntdev_add_map(priv, map); |
| 438 | op.index = map->index << PAGE_SHIFT; |
| 439 | spin_unlock(&priv->lock); |
| 440 | |
| 441 | if (copy_to_user(u, &op, sizeof(op)) != 0) { |
| 442 | spin_lock(&priv->lock); |
| 443 | gntdev_del_map(map); |
| 444 | spin_unlock(&priv->lock); |
| 445 | gntdev_free_map(map); |
| 446 | return err; |
| 447 | } |
| 448 | return 0; |
| 449 | } |
| 450 | |
| 451 | static long gntdev_ioctl_unmap_grant_ref(struct gntdev_priv *priv, |
| 452 | struct ioctl_gntdev_unmap_grant_ref __user *u) |
| 453 | { |
| 454 | struct ioctl_gntdev_unmap_grant_ref op; |
| 455 | struct grant_map *map; |
| 456 | int err = -ENOENT; |
| 457 | |
| 458 | if (copy_from_user(&op, u, sizeof(op)) != 0) |
| 459 | return -EFAULT; |
| 460 | pr_debug("priv %p, del %d+%d\n", priv, (int)op.index, (int)op.count); |
| 461 | |
| 462 | spin_lock(&priv->lock); |
| 463 | map = gntdev_find_map_index(priv, op.index >> PAGE_SHIFT, op.count); |
| 464 | if (map) |
| 465 | err = gntdev_del_map(map); |
| 466 | spin_unlock(&priv->lock); |
| 467 | if (!err) |
| 468 | gntdev_free_map(map); |
| 469 | return err; |
| 470 | } |
| 471 | |
| 472 | static long gntdev_ioctl_get_offset_for_vaddr(struct gntdev_priv *priv, |
| 473 | struct ioctl_gntdev_get_offset_for_vaddr __user *u) |
| 474 | { |
| 475 | struct ioctl_gntdev_get_offset_for_vaddr op; |
| 476 | struct grant_map *map; |
| 477 | |
| 478 | if (copy_from_user(&op, u, sizeof(op)) != 0) |
| 479 | return -EFAULT; |
| 480 | pr_debug("priv %p, offset for vaddr %lx\n", priv, (unsigned long)op.vaddr); |
| 481 | |
| 482 | spin_lock(&priv->lock); |
| 483 | map = gntdev_find_map_vaddr(priv, op.vaddr); |
| 484 | if (map == NULL || |
| 485 | map->vma->vm_start != op.vaddr) { |
| 486 | spin_unlock(&priv->lock); |
| 487 | return -EINVAL; |
| 488 | } |
| 489 | op.offset = map->index << PAGE_SHIFT; |
| 490 | op.count = map->count; |
| 491 | spin_unlock(&priv->lock); |
| 492 | |
| 493 | if (copy_to_user(u, &op, sizeof(op)) != 0) |
| 494 | return -EFAULT; |
| 495 | return 0; |
| 496 | } |
| 497 | |
| 498 | static long gntdev_ioctl_set_max_grants(struct gntdev_priv *priv, |
| 499 | struct ioctl_gntdev_set_max_grants __user *u) |
| 500 | { |
| 501 | struct ioctl_gntdev_set_max_grants op; |
| 502 | |
| 503 | if (copy_from_user(&op, u, sizeof(op)) != 0) |
| 504 | return -EFAULT; |
| 505 | pr_debug("priv %p, limit %d\n", priv, op.count); |
| 506 | if (op.count > limit) |
| 507 | return -E2BIG; |
| 508 | |
| 509 | spin_lock(&priv->lock); |
| 510 | priv->limit = op.count; |
| 511 | spin_unlock(&priv->lock); |
| 512 | return 0; |
| 513 | } |
| 514 | |
| 515 | static long gntdev_ioctl(struct file *flip, |
| 516 | unsigned int cmd, unsigned long arg) |
| 517 | { |
| 518 | struct gntdev_priv *priv = flip->private_data; |
| 519 | void __user *ptr = (void __user *)arg; |
| 520 | |
| 521 | switch (cmd) { |
| 522 | case IOCTL_GNTDEV_MAP_GRANT_REF: |
| 523 | return gntdev_ioctl_map_grant_ref(priv, ptr); |
| 524 | |
| 525 | case IOCTL_GNTDEV_UNMAP_GRANT_REF: |
| 526 | return gntdev_ioctl_unmap_grant_ref(priv, ptr); |
| 527 | |
| 528 | case IOCTL_GNTDEV_GET_OFFSET_FOR_VADDR: |
| 529 | return gntdev_ioctl_get_offset_for_vaddr(priv, ptr); |
| 530 | |
| 531 | case IOCTL_GNTDEV_SET_MAX_GRANTS: |
| 532 | return gntdev_ioctl_set_max_grants(priv, ptr); |
| 533 | |
| 534 | default: |
| 535 | pr_debug("priv %p, unknown cmd %x\n", priv, cmd); |
| 536 | return -ENOIOCTLCMD; |
| 537 | } |
| 538 | |
| 539 | return 0; |
| 540 | } |
| 541 | |
| 542 | static int gntdev_mmap(struct file *flip, struct vm_area_struct *vma) |
| 543 | { |
| 544 | struct gntdev_priv *priv = flip->private_data; |
| 545 | int index = vma->vm_pgoff; |
| 546 | int count = (vma->vm_end - vma->vm_start) >> PAGE_SHIFT; |
| 547 | struct grant_map *map; |
| 548 | int err = -EINVAL; |
| 549 | |
| 550 | if ((vma->vm_flags & VM_WRITE) && !(vma->vm_flags & VM_SHARED)) |
| 551 | return -EINVAL; |
| 552 | |
| 553 | pr_debug("map %d+%d at %lx (pgoff %lx)\n", |
| 554 | index, count, vma->vm_start, vma->vm_pgoff); |
| 555 | |
| 556 | spin_lock(&priv->lock); |
| 557 | map = gntdev_find_map_index(priv, index, count); |
| 558 | if (!map) |
| 559 | goto unlock_out; |
| 560 | if (map->vma) |
| 561 | goto unlock_out; |
| 562 | if (priv->mm != vma->vm_mm) { |
| 563 | printk(KERN_WARNING "Huh? Other mm?\n"); |
| 564 | goto unlock_out; |
| 565 | } |
| 566 | |
| 567 | vma->vm_ops = &gntdev_vmops; |
| 568 | |
Jeremy Fitzhardinge | 8d3eaea | 2010-11-11 14:39:12 -0800 | [diff] [blame] | 569 | vma->vm_flags |= VM_RESERVED|VM_DONTCOPY|VM_DONTEXPAND|VM_PFNMAP; |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 570 | |
| 571 | vma->vm_private_data = map; |
| 572 | map->vma = vma; |
| 573 | |
Ian Campbell | 9329e76 | 2010-12-08 11:57:40 +0000 | [diff] [blame] | 574 | map->flags = GNTMAP_host_map | GNTMAP_application_map; |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 575 | if (!(vma->vm_flags & VM_WRITE)) |
| 576 | map->flags |= GNTMAP_readonly; |
| 577 | |
Daniel De Graaf | f0a70c8 | 2011-01-07 11:51:47 +0000 | [diff] [blame^] | 578 | spin_unlock(&priv->lock); |
| 579 | |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 580 | err = apply_to_page_range(vma->vm_mm, vma->vm_start, |
| 581 | vma->vm_end - vma->vm_start, |
| 582 | find_grant_ptes, map); |
| 583 | if (err) { |
| 584 | printk(KERN_WARNING "find_grant_ptes() failure.\n"); |
Daniel De Graaf | f0a70c8 | 2011-01-07 11:51:47 +0000 | [diff] [blame^] | 585 | return err; |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 586 | } |
| 587 | |
| 588 | err = map_grant_pages(map); |
| 589 | if (err) { |
| 590 | printk(KERN_WARNING "map_grant_pages() failure.\n"); |
Daniel De Graaf | f0a70c8 | 2011-01-07 11:51:47 +0000 | [diff] [blame^] | 591 | return err; |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 592 | } |
Daniel De Graaf | f0a70c8 | 2011-01-07 11:51:47 +0000 | [diff] [blame^] | 593 | |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 594 | map->is_mapped = 1; |
| 595 | |
Daniel De Graaf | f0a70c8 | 2011-01-07 11:51:47 +0000 | [diff] [blame^] | 596 | return 0; |
| 597 | |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 598 | unlock_out: |
| 599 | spin_unlock(&priv->lock); |
| 600 | return err; |
| 601 | } |
| 602 | |
| 603 | static const struct file_operations gntdev_fops = { |
| 604 | .owner = THIS_MODULE, |
| 605 | .open = gntdev_open, |
| 606 | .release = gntdev_release, |
| 607 | .mmap = gntdev_mmap, |
| 608 | .unlocked_ioctl = gntdev_ioctl |
| 609 | }; |
| 610 | |
| 611 | static struct miscdevice gntdev_miscdev = { |
| 612 | .minor = MISC_DYNAMIC_MINOR, |
| 613 | .name = "xen/gntdev", |
| 614 | .fops = &gntdev_fops, |
| 615 | }; |
| 616 | |
| 617 | /* ------------------------------------------------------------------ */ |
| 618 | |
| 619 | static int __init gntdev_init(void) |
| 620 | { |
| 621 | int err; |
| 622 | |
| 623 | if (!xen_domain()) |
| 624 | return -ENODEV; |
| 625 | |
| 626 | err = misc_register(&gntdev_miscdev); |
| 627 | if (err != 0) { |
| 628 | printk(KERN_ERR "Could not register gntdev device\n"); |
| 629 | return err; |
| 630 | } |
| 631 | return 0; |
| 632 | } |
| 633 | |
| 634 | static void __exit gntdev_exit(void) |
| 635 | { |
| 636 | misc_deregister(&gntdev_miscdev); |
| 637 | } |
| 638 | |
| 639 | module_init(gntdev_init); |
| 640 | module_exit(gntdev_exit); |
| 641 | |
| 642 | /* ------------------------------------------------------------------ */ |