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> |
Daniel De Graaf | aab8f11 | 2011-02-03 12:19:02 -0500 | [diff] [blame] | 35 | #include <linux/highmem.h> |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 36 | |
| 37 | #include <xen/xen.h> |
| 38 | #include <xen/grant_table.h> |
| 39 | #include <xen/gntdev.h> |
Daniel De Graaf | bdc612d | 2011-02-03 12:19:04 -0500 | [diff] [blame] | 40 | #include <xen/events.h> |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 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 | |
Daniel De Graaf | ef91082 | 2011-02-03 12:18:59 -0500 | [diff] [blame] | 50 | static int limit = 1024*1024; |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 51 | module_param(limit, int, 0644); |
Daniel De Graaf | ef91082 | 2011-02-03 12:18:59 -0500 | [diff] [blame] | 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); |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 56 | |
Daniel De Graaf | aab8f11 | 2011-02-03 12:19:02 -0500 | [diff] [blame] | 57 | static int use_ptemod; |
| 58 | |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 59 | struct gntdev_priv { |
| 60 | struct list_head maps; |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 61 | /* lock protects maps from concurrent changes */ |
| 62 | spinlock_t lock; |
| 63 | struct mm_struct *mm; |
| 64 | struct mmu_notifier mn; |
| 65 | }; |
| 66 | |
Daniel De Graaf | bdc612d | 2011-02-03 12:19:04 -0500 | [diff] [blame] | 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 | |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 74 | struct grant_map { |
| 75 | struct list_head next; |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 76 | struct vm_area_struct *vma; |
| 77 | int index; |
| 78 | int count; |
| 79 | int flags; |
Daniel De Graaf | 68b025c | 2011-02-03 12:19:01 -0500 | [diff] [blame] | 80 | atomic_t users; |
Daniel De Graaf | bdc612d | 2011-02-03 12:19:04 -0500 | [diff] [blame] | 81 | struct unmap_notify notify; |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 82 | struct ioctl_gntdev_grant_ref *grants; |
| 83 | struct gnttab_map_grant_ref *map_ops; |
| 84 | struct gnttab_unmap_grant_ref *unmap_ops; |
Stefano Stabellini | a12b4eb | 2010-12-10 14:56:42 +0000 | [diff] [blame] | 85 | struct page **pages; |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 86 | }; |
| 87 | |
Daniel De Graaf | aab8f11 | 2011-02-03 12:19:02 -0500 | [diff] [blame] | 88 | static int unmap_grant_pages(struct grant_map *map, int offset, int pages); |
| 89 | |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 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 | |
Daniel De Graaf | ef91082 | 2011-02-03 12:18:59 -0500 | [diff] [blame] | 98 | pr_debug("%s: maps list (priv %p)\n", __func__, priv); |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 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; |
Stefano Stabellini | a12b4eb | 2010-12-10 14:56:42 +0000 | [diff] [blame] | 109 | int i; |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 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); |
Stefano Stabellini | a12b4eb | 2010-12-10 14:56:42 +0000 | [diff] [blame] | 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) |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 123 | goto err; |
| 124 | |
Stefano Stabellini | a12b4eb | 2010-12-10 14:56:42 +0000 | [diff] [blame] | 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; |
Daniel De Graaf | 77c35ac | 2011-02-23 08:11:35 -0500 | [diff] [blame^] | 129 | add->map_ops[i].handle = -1; |
| 130 | add->unmap_ops[i].handle = -1; |
Stefano Stabellini | a12b4eb | 2010-12-10 14:56:42 +0000 | [diff] [blame] | 131 | } |
| 132 | |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 133 | add->index = 0; |
| 134 | add->count = count; |
Daniel De Graaf | 68b025c | 2011-02-03 12:19:01 -0500 | [diff] [blame] | 135 | atomic_set(&add->users, 1); |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 136 | |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 137 | return add; |
| 138 | |
| 139 | err: |
Stefano Stabellini | a12b4eb | 2010-12-10 14:56:42 +0000 | [diff] [blame] | 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); |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 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: |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 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; |
Daniel De Graaf | bdc612d | 2011-02-03 12:19:04 -0500 | [diff] [blame] | 178 | if (count && map->count != count) |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 179 | continue; |
| 180 | return map; |
| 181 | } |
| 182 | return NULL; |
| 183 | } |
| 184 | |
Daniel De Graaf | 68b025c | 2011-02-03 12:19:01 -0500 | [diff] [blame] | 185 | static void gntdev_put_map(struct grant_map *map) |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 186 | { |
Stefano Stabellini | a12b4eb | 2010-12-10 14:56:42 +0000 | [diff] [blame] | 187 | int i; |
| 188 | |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 189 | if (!map) |
| 190 | return; |
Stefano Stabellini | a12b4eb | 2010-12-10 14:56:42 +0000 | [diff] [blame] | 191 | |
Daniel De Graaf | 68b025c | 2011-02-03 12:19:01 -0500 | [diff] [blame] | 192 | if (!atomic_dec_and_test(&map->users)) |
| 193 | return; |
| 194 | |
| 195 | atomic_sub(map->count, &pages_mapped); |
| 196 | |
Daniel De Graaf | bdc612d | 2011-02-03 12:19:04 -0500 | [diff] [blame] | 197 | if (map->notify.flags & UNMAP_NOTIFY_SEND_EVENT) { |
| 198 | notify_remote_via_evtchn(map->notify.event); |
| 199 | } |
| 200 | |
Daniel De Graaf | aab8f11 | 2011-02-03 12:19:02 -0500 | [diff] [blame] | 201 | if (map->pages) { |
| 202 | if (!use_ptemod) |
| 203 | unmap_grant_pages(map, 0, map->count); |
| 204 | |
Stefano Stabellini | a12b4eb | 2010-12-10 14:56:42 +0000 | [diff] [blame] | 205 | for (i = 0; i < map->count; i++) { |
Daniel De Graaf | aab8f11 | 2011-02-03 12:19:02 -0500 | [diff] [blame] | 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) |
Stefano Stabellini | a12b4eb | 2010-12-10 14:56:42 +0000 | [diff] [blame] | 223 | __free_page(map->pages[i]); |
Daniel De Graaf | aab8f11 | 2011-02-03 12:19:02 -0500 | [diff] [blame] | 224 | else |
| 225 | pr_debug("Discard page %d=%ld\n", i, |
| 226 | page_to_pfn(map->pages[i])); |
Stefano Stabellini | a12b4eb | 2010-12-10 14:56:42 +0000 | [diff] [blame] | 227 | } |
Daniel De Graaf | aab8f11 | 2011-02-03 12:19:02 -0500 | [diff] [blame] | 228 | } |
Stefano Stabellini | a12b4eb | 2010-12-10 14:56:42 +0000 | [diff] [blame] | 229 | kfree(map->pages); |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 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; |
Daniel De Graaf | aab8f11 | 2011-02-03 12:19:02 -0500 | [diff] [blame] | 243 | int flags = map->flags | GNTMAP_application_map | GNTMAP_contains_pte; |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 244 | u64 pte_maddr; |
| 245 | |
| 246 | BUG_ON(pgnr >= map->count); |
Jeremy Fitzhardinge | ba5d101 | 2010-12-08 10:54:32 -0800 | [diff] [blame] | 247 | pte_maddr = arbitrary_virt_to_machine(pte).maddr; |
| 248 | |
Daniel De Graaf | aab8f11 | 2011-02-03 12:19:02 -0500 | [diff] [blame] | 249 | gnttab_set_map_op(&map->map_ops[pgnr], pte_maddr, flags, |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 250 | map->grants[pgnr].ref, |
| 251 | map->grants[pgnr].domid); |
Daniel De Graaf | aab8f11 | 2011-02-03 12:19:02 -0500 | [diff] [blame] | 252 | gnttab_set_unmap_op(&map->unmap_ops[pgnr], pte_maddr, flags, |
Daniel De Graaf | 77c35ac | 2011-02-23 08:11:35 -0500 | [diff] [blame^] | 253 | -1 /* handle */); |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 254 | return 0; |
| 255 | } |
| 256 | |
| 257 | static int map_grant_pages(struct grant_map *map) |
| 258 | { |
| 259 | int i, err = 0; |
Daniel De Graaf | aab8f11 | 2011-02-03 12:19:02 -0500 | [diff] [blame] | 260 | phys_addr_t addr; |
| 261 | |
| 262 | if (!use_ptemod) { |
Daniel De Graaf | 12996fc | 2011-02-09 16:11:32 -0500 | [diff] [blame] | 263 | /* Note: it could already be mapped */ |
Daniel De Graaf | 77c35ac | 2011-02-23 08:11:35 -0500 | [diff] [blame^] | 264 | if (map->map_ops[0].handle != -1) |
Daniel De Graaf | 12996fc | 2011-02-09 16:11:32 -0500 | [diff] [blame] | 265 | return 0; |
Daniel De Graaf | aab8f11 | 2011-02-03 12:19:02 -0500 | [diff] [blame] | 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, |
Daniel De Graaf | 77c35ac | 2011-02-23 08:11:35 -0500 | [diff] [blame^] | 273 | map->flags, -1 /* handle */); |
Daniel De Graaf | aab8f11 | 2011-02-03 12:19:02 -0500 | [diff] [blame] | 274 | } |
| 275 | } |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 276 | |
| 277 | pr_debug("map %d+%d\n", map->index, map->count); |
Stefano Stabellini | a12b4eb | 2010-12-10 14:56:42 +0000 | [diff] [blame] | 278 | err = gnttab_map_refs(map->map_ops, map->pages, map->count); |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 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; |
Daniel De Graaf | 77c35ac | 2011-02-23 08:11:35 -0500 | [diff] [blame^] | 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 | } |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 290 | } |
| 291 | return err; |
| 292 | } |
| 293 | |
Daniel De Graaf | b57c186 | 2011-02-09 15:12:00 -0500 | [diff] [blame] | 294 | static int __unmap_grant_pages(struct grant_map *map, int offset, int pages) |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 295 | { |
| 296 | int i, err = 0; |
| 297 | |
Daniel De Graaf | bdc612d | 2011-02-03 12:19:04 -0500 | [diff] [blame] | 298 | if (map->notify.flags & UNMAP_NOTIFY_CLEAR_BYTE) { |
| 299 | int pgno = (map->notify.addr >> PAGE_SHIFT); |
Daniel De Graaf | 0ea22f0 | 2011-02-08 09:14:06 -0500 | [diff] [blame] | 300 | if (pgno >= offset && pgno < offset + pages && use_ptemod) { |
| 301 | void __user *tmp; |
| 302 | tmp = map->vma->vm_start + map->notify.addr; |
Daniel De Graaf | 9960be9 | 2011-02-09 18:15:50 -0500 | [diff] [blame] | 303 | err = copy_to_user(tmp, &err, 1); |
| 304 | if (err) |
| 305 | return err; |
Daniel De Graaf | 0ea22f0 | 2011-02-08 09:14:06 -0500 | [diff] [blame] | 306 | map->notify.flags &= ~UNMAP_NOTIFY_CLEAR_BYTE; |
| 307 | } else if (pgno >= offset && pgno < offset + pages) { |
Daniel De Graaf | bdc612d | 2011-02-03 12:19:04 -0500 | [diff] [blame] | 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 | |
Daniel De Graaf | 0ea22f0 | 2011-02-08 09:14:06 -0500 | [diff] [blame] | 315 | err = gnttab_unmap_refs(map->unmap_ops + offset, map->pages + offset, pages); |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 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; |
Daniel De Graaf | 77c35ac | 2011-02-23 08:11:35 -0500 | [diff] [blame^] | 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; |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 326 | } |
| 327 | return err; |
| 328 | } |
| 329 | |
Daniel De Graaf | b57c186 | 2011-02-09 15:12:00 -0500 | [diff] [blame] | 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) { |
Daniel De Graaf | 77c35ac | 2011-02-23 08:11:35 -0500 | [diff] [blame^] | 340 | while (pages && map->unmap_ops[offset].handle == -1) { |
Daniel De Graaf | b57c186 | 2011-02-09 15:12:00 -0500 | [diff] [blame] | 341 | offset++; |
| 342 | pages--; |
| 343 | } |
| 344 | range = 0; |
| 345 | while (range < pages) { |
Daniel De Graaf | 77c35ac | 2011-02-23 08:11:35 -0500 | [diff] [blame^] | 346 | if (map->unmap_ops[offset+range].handle == -1) { |
Daniel De Graaf | b57c186 | 2011-02-09 15:12:00 -0500 | [diff] [blame] | 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 | |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 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); |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 367 | map->vma = NULL; |
| 368 | vma->vm_private_data = NULL; |
Daniel De Graaf | 68b025c | 2011-02-03 12:19:01 -0500 | [diff] [blame] | 369 | gntdev_put_map(map); |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 370 | } |
| 371 | |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 372 | static struct vm_operations_struct gntdev_vmops = { |
| 373 | .close = gntdev_vma_close, |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 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; |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 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); |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 455 | |
Daniel De Graaf | aab8f11 | 2011-02-03 12:19:02 -0500 | [diff] [blame] | 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); |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 465 | } |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 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; |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 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); |
Daniel De Graaf | 68b025c | 2011-02-03 12:19:01 -0500 | [diff] [blame] | 488 | list_del(&map->next); |
| 489 | gntdev_put_map(map); |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 490 | } |
| 491 | spin_unlock(&priv->lock); |
| 492 | |
Daniel De Graaf | aab8f11 | 2011-02-03 12:19:02 -0500 | [diff] [blame] | 493 | if (use_ptemod) |
| 494 | mmu_notifier_unregister(&priv->mn, priv->mm); |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 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; |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 511 | |
| 512 | err = -ENOMEM; |
| 513 | map = gntdev_alloc_map(priv, op.count); |
| 514 | if (!map) |
| 515 | return err; |
Daniel De Graaf | ef91082 | 2011-02-03 12:18:59 -0500 | [diff] [blame] | 516 | |
Daniel De Graaf | 68b025c | 2011-02-03 12:19:01 -0500 | [diff] [blame] | 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); |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 520 | return err; |
| 521 | } |
| 522 | |
Daniel De Graaf | 68b025c | 2011-02-03 12:19:01 -0500 | [diff] [blame] | 523 | if (copy_from_user(map->grants, &u->refs, |
| 524 | sizeof(map->grants[0]) * op.count) != 0) { |
| 525 | gntdev_put_map(map); |
Daniel De Graaf | ef91082 | 2011-02-03 12:18:59 -0500 | [diff] [blame] | 526 | return err; |
| 527 | } |
| 528 | |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 529 | 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 Graaf | 68b025c | 2011-02-03 12:19:01 -0500 | [diff] [blame] | 534 | if (copy_to_user(u, &op, sizeof(op)) != 0) |
| 535 | return -EFAULT; |
| 536 | |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 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); |
Daniel De Graaf | 68b025c | 2011-02-03 12:19:01 -0500 | [diff] [blame] | 553 | if (map) { |
| 554 | list_del(&map->next); |
| 555 | gntdev_put_map(map); |
| 556 | err = 0; |
| 557 | } |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 558 | spin_unlock(&priv->lock); |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 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; |
Daniel De Graaf | a879211 | 2011-02-03 12:19:00 -0500 | [diff] [blame] | 566 | struct vm_area_struct *vma; |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 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 | |
Daniel De Graaf | a879211 | 2011-02-03 12:19:00 -0500 | [diff] [blame] | 573 | vma = find_vma(current->mm, op.vaddr); |
| 574 | if (!vma || vma->vm_ops != &gntdev_vmops) |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 575 | return -EINVAL; |
Daniel De Graaf | a879211 | 2011-02-03 12:19:00 -0500 | [diff] [blame] | 576 | |
| 577 | map = vma->vm_private_data; |
| 578 | if (!map) |
| 579 | return -EINVAL; |
| 580 | |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 581 | op.offset = map->index << PAGE_SHIFT; |
| 582 | op.count = map->count; |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 583 | |
| 584 | if (copy_to_user(u, &op, sizeof(op)) != 0) |
| 585 | return -EFAULT; |
| 586 | return 0; |
| 587 | } |
| 588 | |
Daniel De Graaf | bdc612d | 2011-02-03 12:19:04 -0500 | [diff] [blame] | 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: |
Daniel De Graaf | 9960be9 | 2011-02-09 18:15:50 -0500 | [diff] [blame] | 613 | if ((op.action & UNMAP_NOTIFY_CLEAR_BYTE) && |
| 614 | (map->flags & GNTMAP_readonly)) { |
| 615 | rc = -EINVAL; |
| 616 | goto unlock_out; |
| 617 | } |
| 618 | |
Daniel De Graaf | bdc612d | 2011-02-03 12:19:04 -0500 | [diff] [blame] | 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 | |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 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 | |
Daniel De Graaf | bdc612d | 2011-02-03 12:19:04 -0500 | [diff] [blame] | 644 | case IOCTL_GNTDEV_SET_UNMAP_NOTIFY: |
| 645 | return gntdev_ioctl_notify(priv, ptr); |
| 646 | |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 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; |
Daniel De Graaf | aab8f11 | 2011-02-03 12:19:02 -0500 | [diff] [blame] | 661 | int i, err = -EINVAL; |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 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; |
Daniel De Graaf | aab8f11 | 2011-02-03 12:19:02 -0500 | [diff] [blame] | 673 | if (use_ptemod && map->vma) |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 674 | goto unlock_out; |
Daniel De Graaf | aab8f11 | 2011-02-03 12:19:02 -0500 | [diff] [blame] | 675 | if (use_ptemod && priv->mm != vma->vm_mm) { |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 676 | printk(KERN_WARNING "Huh? Other mm?\n"); |
| 677 | goto unlock_out; |
| 678 | } |
| 679 | |
Daniel De Graaf | 68b025c | 2011-02-03 12:19:01 -0500 | [diff] [blame] | 680 | atomic_inc(&map->users); |
| 681 | |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 682 | vma->vm_ops = &gntdev_vmops; |
| 683 | |
Jeremy Fitzhardinge | 8d3eaea | 2010-11-11 14:39:12 -0800 | [diff] [blame] | 684 | vma->vm_flags |= VM_RESERVED|VM_DONTCOPY|VM_DONTEXPAND|VM_PFNMAP; |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 685 | |
| 686 | vma->vm_private_data = map; |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 687 | |
Daniel De Graaf | aab8f11 | 2011-02-03 12:19:02 -0500 | [diff] [blame] | 688 | if (use_ptemod) |
| 689 | map->vma = vma; |
| 690 | |
Daniel De Graaf | 12996fc | 2011-02-09 16:11:32 -0500 | [diff] [blame] | 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 | } |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 700 | |
Daniel De Graaf | f0a70c8 | 2011-01-07 11:51:47 +0000 | [diff] [blame] | 701 | spin_unlock(&priv->lock); |
| 702 | |
Daniel De Graaf | aab8f11 | 2011-02-03 12:19:02 -0500 | [diff] [blame] | 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"); |
Daniel De Graaf | 90b6f30 | 2011-02-03 14:16:54 -0500 | [diff] [blame] | 709 | goto out_put_map; |
Daniel De Graaf | aab8f11 | 2011-02-03 12:19:02 -0500 | [diff] [blame] | 710 | } |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 711 | } |
| 712 | |
| 713 | err = map_grant_pages(map); |
Daniel De Graaf | 90b6f30 | 2011-02-03 14:16:54 -0500 | [diff] [blame] | 714 | if (err) |
| 715 | goto out_put_map; |
Daniel De Graaf | f0a70c8 | 2011-01-07 11:51:47 +0000 | [diff] [blame] | 716 | |
Daniel De Graaf | aab8f11 | 2011-02-03 12:19:02 -0500 | [diff] [blame] | 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) |
Daniel De Graaf | 90b6f30 | 2011-02-03 14:16:54 -0500 | [diff] [blame] | 722 | goto out_put_map; |
Daniel De Graaf | aab8f11 | 2011-02-03 12:19:02 -0500 | [diff] [blame] | 723 | } |
| 724 | } |
| 725 | |
Daniel De Graaf | f0a70c8 | 2011-01-07 11:51:47 +0000 | [diff] [blame] | 726 | return 0; |
| 727 | |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 728 | unlock_out: |
| 729 | spin_unlock(&priv->lock); |
| 730 | return err; |
Daniel De Graaf | 90b6f30 | 2011-02-03 14:16:54 -0500 | [diff] [blame] | 731 | |
| 732 | out_put_map: |
Daniel De Graaf | 84e4075 | 2011-02-09 15:11:59 -0500 | [diff] [blame] | 733 | if (use_ptemod) |
| 734 | map->vma = NULL; |
Daniel De Graaf | 90b6f30 | 2011-02-03 14:16:54 -0500 | [diff] [blame] | 735 | gntdev_put_map(map); |
| 736 | return err; |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 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 | |
Daniel De Graaf | aab8f11 | 2011-02-03 12:19:02 -0500 | [diff] [blame] | 762 | use_ptemod = xen_pv_domain(); |
| 763 | |
Gerd Hoffmann | ab31523 | 2010-12-14 18:40:46 +0000 | [diff] [blame] | 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 | /* ------------------------------------------------------------------ */ |