gntdev.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722
  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. #undef DEBUG
  20. #include <linux/module.h>
  21. #include <linux/kernel.h>
  22. #include <linux/init.h>
  23. #include <linux/miscdevice.h>
  24. #include <linux/fs.h>
  25. #include <linux/mm.h>
  26. #include <linux/mman.h>
  27. #include <linux/mmu_notifier.h>
  28. #include <linux/types.h>
  29. #include <linux/uaccess.h>
  30. #include <linux/sched.h>
  31. #include <linux/spinlock.h>
  32. #include <linux/slab.h>
  33. #include <linux/highmem.h>
  34. #include <xen/xen.h>
  35. #include <xen/grant_table.h>
  36. #include <xen/gntdev.h>
  37. #include <xen/events.h>
  38. #include <asm/xen/hypervisor.h>
  39. #include <asm/xen/hypercall.h>
  40. #include <asm/xen/page.h>
  41. MODULE_LICENSE("GPL");
  42. MODULE_AUTHOR("Derek G. Murray <Derek.Murray@cl.cam.ac.uk>, "
  43. "Gerd Hoffmann <kraxel@redhat.com>");
  44. MODULE_DESCRIPTION("User-space granted page access driver");
  45. static int limit = 1024*1024;
  46. module_param(limit, int, 0644);
  47. MODULE_PARM_DESC(limit, "Maximum number of grants that may be mapped by "
  48. "the gntdev device");
  49. static atomic_t pages_mapped = ATOMIC_INIT(0);
  50. static int use_ptemod;
  51. struct gntdev_priv {
  52. struct list_head maps;
  53. /* lock protects maps from concurrent changes */
  54. spinlock_t lock;
  55. struct mm_struct *mm;
  56. struct mmu_notifier mn;
  57. };
  58. struct unmap_notify {
  59. int flags;
  60. /* Address relative to the start of the grant_map */
  61. int addr;
  62. int event;
  63. };
  64. struct grant_map {
  65. struct list_head next;
  66. struct vm_area_struct *vma;
  67. int index;
  68. int count;
  69. int flags;
  70. int is_mapped;
  71. atomic_t users;
  72. struct unmap_notify notify;
  73. struct ioctl_gntdev_grant_ref *grants;
  74. struct gnttab_map_grant_ref *map_ops;
  75. struct gnttab_unmap_grant_ref *unmap_ops;
  76. struct page **pages;
  77. };
  78. static int unmap_grant_pages(struct grant_map *map, int offset, int pages);
  79. /* ------------------------------------------------------------------ */
  80. static void gntdev_print_maps(struct gntdev_priv *priv,
  81. char *text, int text_index)
  82. {
  83. #ifdef DEBUG
  84. struct grant_map *map;
  85. pr_debug("%s: maps list (priv %p)\n", __func__, priv);
  86. list_for_each_entry(map, &priv->maps, next)
  87. pr_debug(" index %2d, count %2d %s\n",
  88. map->index, map->count,
  89. map->index == text_index && text ? text : "");
  90. #endif
  91. }
  92. static struct grant_map *gntdev_alloc_map(struct gntdev_priv *priv, int count)
  93. {
  94. struct grant_map *add;
  95. int i;
  96. add = kzalloc(sizeof(struct grant_map), GFP_KERNEL);
  97. if (NULL == add)
  98. return NULL;
  99. add->grants = kzalloc(sizeof(add->grants[0]) * count, GFP_KERNEL);
  100. add->map_ops = kzalloc(sizeof(add->map_ops[0]) * count, GFP_KERNEL);
  101. add->unmap_ops = kzalloc(sizeof(add->unmap_ops[0]) * count, GFP_KERNEL);
  102. add->pages = kzalloc(sizeof(add->pages[0]) * count, GFP_KERNEL);
  103. if (NULL == add->grants ||
  104. NULL == add->map_ops ||
  105. NULL == add->unmap_ops ||
  106. NULL == add->pages)
  107. goto err;
  108. for (i = 0; i < count; i++) {
  109. add->pages[i] = alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
  110. if (add->pages[i] == NULL)
  111. goto err;
  112. }
  113. add->index = 0;
  114. add->count = count;
  115. atomic_set(&add->users, 1);
  116. return add;
  117. err:
  118. if (add->pages)
  119. for (i = 0; i < count; i++) {
  120. if (add->pages[i])
  121. __free_page(add->pages[i]);
  122. }
  123. kfree(add->pages);
  124. kfree(add->grants);
  125. kfree(add->map_ops);
  126. kfree(add->unmap_ops);
  127. kfree(add);
  128. return NULL;
  129. }
  130. static void gntdev_add_map(struct gntdev_priv *priv, struct grant_map *add)
  131. {
  132. struct grant_map *map;
  133. list_for_each_entry(map, &priv->maps, next) {
  134. if (add->index + add->count < map->index) {
  135. list_add_tail(&add->next, &map->next);
  136. goto done;
  137. }
  138. add->index = map->index + map->count;
  139. }
  140. list_add_tail(&add->next, &priv->maps);
  141. done:
  142. gntdev_print_maps(priv, "[new]", add->index);
  143. }
  144. static struct grant_map *gntdev_find_map_index(struct gntdev_priv *priv,
  145. int index, int count)
  146. {
  147. struct grant_map *map;
  148. list_for_each_entry(map, &priv->maps, next) {
  149. if (map->index != index)
  150. continue;
  151. if (count && map->count != count)
  152. continue;
  153. return map;
  154. }
  155. return NULL;
  156. }
  157. static void gntdev_put_map(struct grant_map *map)
  158. {
  159. int i;
  160. if (!map)
  161. return;
  162. if (!atomic_dec_and_test(&map->users))
  163. return;
  164. atomic_sub(map->count, &pages_mapped);
  165. if (map->notify.flags & UNMAP_NOTIFY_SEND_EVENT) {
  166. notify_remote_via_evtchn(map->notify.event);
  167. }
  168. if (map->pages) {
  169. if (!use_ptemod)
  170. unmap_grant_pages(map, 0, map->count);
  171. for (i = 0; i < map->count; i++) {
  172. uint32_t check, *tmp;
  173. if (!map->pages[i])
  174. continue;
  175. /* XXX When unmapping in an HVM domain, Xen will
  176. * sometimes end up mapping the GFN to an invalid MFN.
  177. * In this case, writes will be discarded and reads will
  178. * return all 0xFF bytes. Leak these unusable GFNs
  179. * until Xen supports fixing their p2m mapping.
  180. *
  181. * Confirmed present in Xen 4.1-RC3 with HVM source
  182. */
  183. tmp = kmap(map->pages[i]);
  184. *tmp = 0xdeaddead;
  185. mb();
  186. check = *tmp;
  187. kunmap(map->pages[i]);
  188. if (check == 0xdeaddead)
  189. __free_page(map->pages[i]);
  190. else
  191. pr_debug("Discard page %d=%ld\n", i,
  192. page_to_pfn(map->pages[i]));
  193. }
  194. }
  195. kfree(map->pages);
  196. kfree(map->grants);
  197. kfree(map->map_ops);
  198. kfree(map->unmap_ops);
  199. kfree(map);
  200. }
  201. /* ------------------------------------------------------------------ */
  202. static int find_grant_ptes(pte_t *pte, pgtable_t token,
  203. unsigned long addr, void *data)
  204. {
  205. struct grant_map *map = data;
  206. unsigned int pgnr = (addr - map->vma->vm_start) >> PAGE_SHIFT;
  207. int flags = map->flags | GNTMAP_application_map | GNTMAP_contains_pte;
  208. u64 pte_maddr;
  209. BUG_ON(pgnr >= map->count);
  210. pte_maddr = arbitrary_virt_to_machine(pte).maddr;
  211. gnttab_set_map_op(&map->map_ops[pgnr], pte_maddr, flags,
  212. map->grants[pgnr].ref,
  213. map->grants[pgnr].domid);
  214. gnttab_set_unmap_op(&map->unmap_ops[pgnr], pte_maddr, flags,
  215. 0 /* handle */);
  216. return 0;
  217. }
  218. static int map_grant_pages(struct grant_map *map)
  219. {
  220. int i, err = 0;
  221. phys_addr_t addr;
  222. if (!use_ptemod) {
  223. for (i = 0; i < map->count; i++) {
  224. addr = (phys_addr_t)
  225. pfn_to_kaddr(page_to_pfn(map->pages[i]));
  226. gnttab_set_map_op(&map->map_ops[i], addr, map->flags,
  227. map->grants[i].ref,
  228. map->grants[i].domid);
  229. gnttab_set_unmap_op(&map->unmap_ops[i], addr,
  230. map->flags, 0 /* handle */);
  231. }
  232. }
  233. pr_debug("map %d+%d\n", map->index, map->count);
  234. err = gnttab_map_refs(map->map_ops, map->pages, map->count);
  235. if (err)
  236. return err;
  237. for (i = 0; i < map->count; i++) {
  238. if (map->map_ops[i].status)
  239. err = -EINVAL;
  240. map->unmap_ops[i].handle = map->map_ops[i].handle;
  241. }
  242. return err;
  243. }
  244. static int unmap_grant_pages(struct grant_map *map, int offset, int pages)
  245. {
  246. int i, err = 0;
  247. if (map->notify.flags & UNMAP_NOTIFY_CLEAR_BYTE) {
  248. int pgno = (map->notify.addr >> PAGE_SHIFT);
  249. if (pgno >= offset && pgno < offset + pages) {
  250. uint8_t *tmp = kmap(map->pages[pgno]);
  251. tmp[map->notify.addr & (PAGE_SIZE-1)] = 0;
  252. kunmap(map->pages[pgno]);
  253. map->notify.flags &= ~UNMAP_NOTIFY_CLEAR_BYTE;
  254. }
  255. }
  256. pr_debug("map %d+%d [%d+%d]\n", map->index, map->count, offset, pages);
  257. err = gnttab_unmap_refs(map->unmap_ops + offset, map->pages, pages);
  258. if (err)
  259. return err;
  260. for (i = 0; i < pages; i++) {
  261. if (map->unmap_ops[offset+i].status)
  262. err = -EINVAL;
  263. map->unmap_ops[offset+i].handle = 0;
  264. }
  265. return err;
  266. }
  267. /* ------------------------------------------------------------------ */
  268. static void gntdev_vma_close(struct vm_area_struct *vma)
  269. {
  270. struct grant_map *map = vma->vm_private_data;
  271. pr_debug("close %p\n", vma);
  272. map->is_mapped = 0;
  273. map->vma = NULL;
  274. vma->vm_private_data = NULL;
  275. gntdev_put_map(map);
  276. }
  277. static struct vm_operations_struct gntdev_vmops = {
  278. .close = gntdev_vma_close,
  279. };
  280. /* ------------------------------------------------------------------ */
  281. static void mn_invl_range_start(struct mmu_notifier *mn,
  282. struct mm_struct *mm,
  283. unsigned long start, unsigned long end)
  284. {
  285. struct gntdev_priv *priv = container_of(mn, struct gntdev_priv, mn);
  286. struct grant_map *map;
  287. unsigned long mstart, mend;
  288. int err;
  289. spin_lock(&priv->lock);
  290. list_for_each_entry(map, &priv->maps, next) {
  291. if (!map->vma)
  292. continue;
  293. if (!map->is_mapped)
  294. continue;
  295. if (map->vma->vm_start >= end)
  296. continue;
  297. if (map->vma->vm_end <= start)
  298. continue;
  299. mstart = max(start, map->vma->vm_start);
  300. mend = min(end, map->vma->vm_end);
  301. pr_debug("map %d+%d (%lx %lx), range %lx %lx, mrange %lx %lx\n",
  302. map->index, map->count,
  303. map->vma->vm_start, map->vma->vm_end,
  304. start, end, mstart, mend);
  305. err = unmap_grant_pages(map,
  306. (mstart - map->vma->vm_start) >> PAGE_SHIFT,
  307. (mend - mstart) >> PAGE_SHIFT);
  308. WARN_ON(err);
  309. }
  310. spin_unlock(&priv->lock);
  311. }
  312. static void mn_invl_page(struct mmu_notifier *mn,
  313. struct mm_struct *mm,
  314. unsigned long address)
  315. {
  316. mn_invl_range_start(mn, mm, address, address + PAGE_SIZE);
  317. }
  318. static void mn_release(struct mmu_notifier *mn,
  319. struct mm_struct *mm)
  320. {
  321. struct gntdev_priv *priv = container_of(mn, struct gntdev_priv, mn);
  322. struct grant_map *map;
  323. int err;
  324. spin_lock(&priv->lock);
  325. list_for_each_entry(map, &priv->maps, next) {
  326. if (!map->vma)
  327. continue;
  328. pr_debug("map %d+%d (%lx %lx)\n",
  329. map->index, map->count,
  330. map->vma->vm_start, map->vma->vm_end);
  331. err = unmap_grant_pages(map, /* offset */ 0, map->count);
  332. WARN_ON(err);
  333. }
  334. spin_unlock(&priv->lock);
  335. }
  336. struct mmu_notifier_ops gntdev_mmu_ops = {
  337. .release = mn_release,
  338. .invalidate_page = mn_invl_page,
  339. .invalidate_range_start = mn_invl_range_start,
  340. };
  341. /* ------------------------------------------------------------------ */
  342. static int gntdev_open(struct inode *inode, struct file *flip)
  343. {
  344. struct gntdev_priv *priv;
  345. int ret = 0;
  346. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  347. if (!priv)
  348. return -ENOMEM;
  349. INIT_LIST_HEAD(&priv->maps);
  350. spin_lock_init(&priv->lock);
  351. if (use_ptemod) {
  352. priv->mm = get_task_mm(current);
  353. if (!priv->mm) {
  354. kfree(priv);
  355. return -ENOMEM;
  356. }
  357. priv->mn.ops = &gntdev_mmu_ops;
  358. ret = mmu_notifier_register(&priv->mn, priv->mm);
  359. mmput(priv->mm);
  360. }
  361. if (ret) {
  362. kfree(priv);
  363. return ret;
  364. }
  365. flip->private_data = priv;
  366. pr_debug("priv %p\n", priv);
  367. return 0;
  368. }
  369. static int gntdev_release(struct inode *inode, struct file *flip)
  370. {
  371. struct gntdev_priv *priv = flip->private_data;
  372. struct grant_map *map;
  373. pr_debug("priv %p\n", priv);
  374. spin_lock(&priv->lock);
  375. while (!list_empty(&priv->maps)) {
  376. map = list_entry(priv->maps.next, struct grant_map, next);
  377. list_del(&map->next);
  378. gntdev_put_map(map);
  379. }
  380. spin_unlock(&priv->lock);
  381. if (use_ptemod)
  382. mmu_notifier_unregister(&priv->mn, priv->mm);
  383. kfree(priv);
  384. return 0;
  385. }
  386. static long gntdev_ioctl_map_grant_ref(struct gntdev_priv *priv,
  387. struct ioctl_gntdev_map_grant_ref __user *u)
  388. {
  389. struct ioctl_gntdev_map_grant_ref op;
  390. struct grant_map *map;
  391. int err;
  392. if (copy_from_user(&op, u, sizeof(op)) != 0)
  393. return -EFAULT;
  394. pr_debug("priv %p, add %d\n", priv, op.count);
  395. if (unlikely(op.count <= 0))
  396. return -EINVAL;
  397. err = -ENOMEM;
  398. map = gntdev_alloc_map(priv, op.count);
  399. if (!map)
  400. return err;
  401. if (unlikely(atomic_add_return(op.count, &pages_mapped) > limit)) {
  402. pr_debug("can't map: over limit\n");
  403. gntdev_put_map(map);
  404. return err;
  405. }
  406. if (copy_from_user(map->grants, &u->refs,
  407. sizeof(map->grants[0]) * op.count) != 0) {
  408. gntdev_put_map(map);
  409. return err;
  410. }
  411. spin_lock(&priv->lock);
  412. gntdev_add_map(priv, map);
  413. op.index = map->index << PAGE_SHIFT;
  414. spin_unlock(&priv->lock);
  415. if (copy_to_user(u, &op, sizeof(op)) != 0)
  416. return -EFAULT;
  417. return 0;
  418. }
  419. static long gntdev_ioctl_unmap_grant_ref(struct gntdev_priv *priv,
  420. struct ioctl_gntdev_unmap_grant_ref __user *u)
  421. {
  422. struct ioctl_gntdev_unmap_grant_ref op;
  423. struct grant_map *map;
  424. int err = -ENOENT;
  425. if (copy_from_user(&op, u, sizeof(op)) != 0)
  426. return -EFAULT;
  427. pr_debug("priv %p, del %d+%d\n", priv, (int)op.index, (int)op.count);
  428. spin_lock(&priv->lock);
  429. map = gntdev_find_map_index(priv, op.index >> PAGE_SHIFT, op.count);
  430. if (map) {
  431. list_del(&map->next);
  432. gntdev_put_map(map);
  433. err = 0;
  434. }
  435. spin_unlock(&priv->lock);
  436. return err;
  437. }
  438. static long gntdev_ioctl_get_offset_for_vaddr(struct gntdev_priv *priv,
  439. struct ioctl_gntdev_get_offset_for_vaddr __user *u)
  440. {
  441. struct ioctl_gntdev_get_offset_for_vaddr op;
  442. struct vm_area_struct *vma;
  443. struct grant_map *map;
  444. if (copy_from_user(&op, u, sizeof(op)) != 0)
  445. return -EFAULT;
  446. pr_debug("priv %p, offset for vaddr %lx\n", priv, (unsigned long)op.vaddr);
  447. vma = find_vma(current->mm, op.vaddr);
  448. if (!vma || vma->vm_ops != &gntdev_vmops)
  449. return -EINVAL;
  450. map = vma->vm_private_data;
  451. if (!map)
  452. return -EINVAL;
  453. op.offset = map->index << PAGE_SHIFT;
  454. op.count = map->count;
  455. if (copy_to_user(u, &op, sizeof(op)) != 0)
  456. return -EFAULT;
  457. return 0;
  458. }
  459. static long gntdev_ioctl_notify(struct gntdev_priv *priv, void __user *u)
  460. {
  461. struct ioctl_gntdev_unmap_notify op;
  462. struct grant_map *map;
  463. int rc;
  464. if (copy_from_user(&op, u, sizeof(op)))
  465. return -EFAULT;
  466. if (op.action & ~(UNMAP_NOTIFY_CLEAR_BYTE|UNMAP_NOTIFY_SEND_EVENT))
  467. return -EINVAL;
  468. spin_lock(&priv->lock);
  469. list_for_each_entry(map, &priv->maps, next) {
  470. uint64_t begin = map->index << PAGE_SHIFT;
  471. uint64_t end = (map->index + map->count) << PAGE_SHIFT;
  472. if (op.index >= begin && op.index < end)
  473. goto found;
  474. }
  475. rc = -ENOENT;
  476. goto unlock_out;
  477. found:
  478. map->notify.flags = op.action;
  479. map->notify.addr = op.index - (map->index << PAGE_SHIFT);
  480. map->notify.event = op.event_channel_port;
  481. rc = 0;
  482. unlock_out:
  483. spin_unlock(&priv->lock);
  484. return rc;
  485. }
  486. static long gntdev_ioctl(struct file *flip,
  487. unsigned int cmd, unsigned long arg)
  488. {
  489. struct gntdev_priv *priv = flip->private_data;
  490. void __user *ptr = (void __user *)arg;
  491. switch (cmd) {
  492. case IOCTL_GNTDEV_MAP_GRANT_REF:
  493. return gntdev_ioctl_map_grant_ref(priv, ptr);
  494. case IOCTL_GNTDEV_UNMAP_GRANT_REF:
  495. return gntdev_ioctl_unmap_grant_ref(priv, ptr);
  496. case IOCTL_GNTDEV_GET_OFFSET_FOR_VADDR:
  497. return gntdev_ioctl_get_offset_for_vaddr(priv, ptr);
  498. case IOCTL_GNTDEV_SET_UNMAP_NOTIFY:
  499. return gntdev_ioctl_notify(priv, ptr);
  500. default:
  501. pr_debug("priv %p, unknown cmd %x\n", priv, cmd);
  502. return -ENOIOCTLCMD;
  503. }
  504. return 0;
  505. }
  506. static int gntdev_mmap(struct file *flip, struct vm_area_struct *vma)
  507. {
  508. struct gntdev_priv *priv = flip->private_data;
  509. int index = vma->vm_pgoff;
  510. int count = (vma->vm_end - vma->vm_start) >> PAGE_SHIFT;
  511. struct grant_map *map;
  512. int i, err = -EINVAL;
  513. if ((vma->vm_flags & VM_WRITE) && !(vma->vm_flags & VM_SHARED))
  514. return -EINVAL;
  515. pr_debug("map %d+%d at %lx (pgoff %lx)\n",
  516. index, count, vma->vm_start, vma->vm_pgoff);
  517. spin_lock(&priv->lock);
  518. map = gntdev_find_map_index(priv, index, count);
  519. if (!map)
  520. goto unlock_out;
  521. if (use_ptemod && map->vma)
  522. goto unlock_out;
  523. if (use_ptemod && priv->mm != vma->vm_mm) {
  524. printk(KERN_WARNING "Huh? Other mm?\n");
  525. goto unlock_out;
  526. }
  527. atomic_inc(&map->users);
  528. vma->vm_ops = &gntdev_vmops;
  529. vma->vm_flags |= VM_RESERVED|VM_DONTCOPY|VM_DONTEXPAND|VM_PFNMAP;
  530. vma->vm_private_data = map;
  531. if (use_ptemod)
  532. map->vma = vma;
  533. map->flags = GNTMAP_host_map;
  534. if (!(vma->vm_flags & VM_WRITE))
  535. map->flags |= GNTMAP_readonly;
  536. spin_unlock(&priv->lock);
  537. if (use_ptemod) {
  538. err = apply_to_page_range(vma->vm_mm, vma->vm_start,
  539. vma->vm_end - vma->vm_start,
  540. find_grant_ptes, map);
  541. if (err) {
  542. printk(KERN_WARNING "find_grant_ptes() failure.\n");
  543. return err;
  544. }
  545. }
  546. err = map_grant_pages(map);
  547. if (err) {
  548. printk(KERN_WARNING "map_grant_pages() failure.\n");
  549. return err;
  550. }
  551. map->is_mapped = 1;
  552. if (!use_ptemod) {
  553. for (i = 0; i < count; i++) {
  554. err = vm_insert_page(vma, vma->vm_start + i*PAGE_SIZE,
  555. map->pages[i]);
  556. if (err)
  557. return err;
  558. }
  559. }
  560. return 0;
  561. unlock_out:
  562. spin_unlock(&priv->lock);
  563. return err;
  564. }
  565. static const struct file_operations gntdev_fops = {
  566. .owner = THIS_MODULE,
  567. .open = gntdev_open,
  568. .release = gntdev_release,
  569. .mmap = gntdev_mmap,
  570. .unlocked_ioctl = gntdev_ioctl
  571. };
  572. static struct miscdevice gntdev_miscdev = {
  573. .minor = MISC_DYNAMIC_MINOR,
  574. .name = "xen/gntdev",
  575. .fops = &gntdev_fops,
  576. };
  577. /* ------------------------------------------------------------------ */
  578. static int __init gntdev_init(void)
  579. {
  580. int err;
  581. if (!xen_domain())
  582. return -ENODEV;
  583. use_ptemod = xen_pv_domain();
  584. err = misc_register(&gntdev_miscdev);
  585. if (err != 0) {
  586. printk(KERN_ERR "Could not register gntdev device\n");
  587. return err;
  588. }
  589. return 0;
  590. }
  591. static void __exit gntdev_exit(void)
  592. {
  593. misc_deregister(&gntdev_miscdev);
  594. }
  595. module_init(gntdev_init);
  596. module_exit(gntdev_exit);
  597. /* ------------------------------------------------------------------ */