gntdev.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827
  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/balloon.h>
  37. #include <xen/gntdev.h>
  38. #include <xen/events.h>
  39. #include <asm/xen/hypervisor.h>
  40. #include <asm/xen/hypercall.h>
  41. #include <asm/xen/page.h>
  42. MODULE_LICENSE("GPL");
  43. MODULE_AUTHOR("Derek G. Murray <Derek.Murray@cl.cam.ac.uk>, "
  44. "Gerd Hoffmann <kraxel@redhat.com>");
  45. MODULE_DESCRIPTION("User-space granted page access driver");
  46. static int limit = 1024*1024;
  47. module_param(limit, int, 0644);
  48. MODULE_PARM_DESC(limit, "Maximum number of grants that may be mapped by "
  49. "the gntdev device");
  50. static atomic_t pages_mapped = ATOMIC_INIT(0);
  51. static int use_ptemod;
  52. struct gntdev_priv {
  53. struct list_head maps;
  54. /* lock protects maps from concurrent changes */
  55. spinlock_t lock;
  56. struct mm_struct *mm;
  57. struct mmu_notifier mn;
  58. };
  59. struct unmap_notify {
  60. int flags;
  61. /* Address relative to the start of the grant_map */
  62. int addr;
  63. int event;
  64. };
  65. struct grant_map {
  66. struct list_head next;
  67. struct vm_area_struct *vma;
  68. int index;
  69. int count;
  70. int flags;
  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 gnttab_map_grant_ref *kmap_ops;
  77. struct page **pages;
  78. };
  79. static int unmap_grant_pages(struct grant_map *map, int offset, int pages);
  80. /* ------------------------------------------------------------------ */
  81. static void gntdev_print_maps(struct gntdev_priv *priv,
  82. char *text, int text_index)
  83. {
  84. #ifdef DEBUG
  85. struct grant_map *map;
  86. pr_debug("%s: maps list (priv %p)\n", __func__, priv);
  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. static void gntdev_free_map(struct grant_map *map)
  94. {
  95. if (map == NULL)
  96. return;
  97. if (map->pages)
  98. free_xenballooned_pages(map->count, map->pages);
  99. kfree(map->pages);
  100. kfree(map->grants);
  101. kfree(map->map_ops);
  102. kfree(map->unmap_ops);
  103. kfree(map->kmap_ops);
  104. kfree(map);
  105. }
  106. static struct grant_map *gntdev_alloc_map(struct gntdev_priv *priv, int count)
  107. {
  108. struct grant_map *add;
  109. int i;
  110. add = kzalloc(sizeof(struct grant_map), GFP_KERNEL);
  111. if (NULL == add)
  112. return NULL;
  113. add->grants = kcalloc(count, sizeof(add->grants[0]), GFP_KERNEL);
  114. add->map_ops = kcalloc(count, sizeof(add->map_ops[0]), GFP_KERNEL);
  115. add->unmap_ops = kcalloc(count, sizeof(add->unmap_ops[0]), GFP_KERNEL);
  116. add->kmap_ops = kcalloc(count, sizeof(add->kmap_ops[0]), GFP_KERNEL);
  117. add->pages = kcalloc(count, sizeof(add->pages[0]), GFP_KERNEL);
  118. if (NULL == add->grants ||
  119. NULL == add->map_ops ||
  120. NULL == add->unmap_ops ||
  121. NULL == add->kmap_ops ||
  122. NULL == add->pages)
  123. goto err;
  124. if (alloc_xenballooned_pages(count, add->pages, false /* lowmem */))
  125. goto err;
  126. for (i = 0; i < count; i++) {
  127. add->map_ops[i].handle = -1;
  128. add->unmap_ops[i].handle = -1;
  129. add->kmap_ops[i].handle = -1;
  130. }
  131. add->index = 0;
  132. add->count = count;
  133. atomic_set(&add->users, 1);
  134. return add;
  135. err:
  136. gntdev_free_map(add);
  137. return NULL;
  138. }
  139. static void gntdev_add_map(struct gntdev_priv *priv, struct grant_map *add)
  140. {
  141. struct grant_map *map;
  142. list_for_each_entry(map, &priv->maps, next) {
  143. if (add->index + add->count < map->index) {
  144. list_add_tail(&add->next, &map->next);
  145. goto done;
  146. }
  147. add->index = map->index + map->count;
  148. }
  149. list_add_tail(&add->next, &priv->maps);
  150. done:
  151. gntdev_print_maps(priv, "[new]", add->index);
  152. }
  153. static struct grant_map *gntdev_find_map_index(struct gntdev_priv *priv,
  154. int index, int count)
  155. {
  156. struct grant_map *map;
  157. list_for_each_entry(map, &priv->maps, next) {
  158. if (map->index != index)
  159. continue;
  160. if (count && map->count != count)
  161. continue;
  162. return map;
  163. }
  164. return NULL;
  165. }
  166. static void gntdev_put_map(struct grant_map *map)
  167. {
  168. if (!map)
  169. return;
  170. if (!atomic_dec_and_test(&map->users))
  171. return;
  172. atomic_sub(map->count, &pages_mapped);
  173. if (map->notify.flags & UNMAP_NOTIFY_SEND_EVENT) {
  174. notify_remote_via_evtchn(map->notify.event);
  175. evtchn_put(map->notify.event);
  176. }
  177. if (map->pages && !use_ptemod)
  178. unmap_grant_pages(map, 0, map->count);
  179. gntdev_free_map(map);
  180. }
  181. /* ------------------------------------------------------------------ */
  182. static int find_grant_ptes(pte_t *pte, pgtable_t token,
  183. unsigned long addr, void *data)
  184. {
  185. struct grant_map *map = data;
  186. unsigned int pgnr = (addr - map->vma->vm_start) >> PAGE_SHIFT;
  187. int flags = map->flags | GNTMAP_application_map | GNTMAP_contains_pte;
  188. u64 pte_maddr;
  189. BUG_ON(pgnr >= map->count);
  190. pte_maddr = arbitrary_virt_to_machine(pte).maddr;
  191. gnttab_set_map_op(&map->map_ops[pgnr], pte_maddr, flags,
  192. map->grants[pgnr].ref,
  193. map->grants[pgnr].domid);
  194. gnttab_set_unmap_op(&map->unmap_ops[pgnr], pte_maddr, flags,
  195. -1 /* handle */);
  196. return 0;
  197. }
  198. static int map_grant_pages(struct grant_map *map)
  199. {
  200. int i, err = 0;
  201. if (!use_ptemod) {
  202. /* Note: it could already be mapped */
  203. if (map->map_ops[0].handle != -1)
  204. return 0;
  205. for (i = 0; i < map->count; i++) {
  206. unsigned long addr = (unsigned long)
  207. pfn_to_kaddr(page_to_pfn(map->pages[i]));
  208. gnttab_set_map_op(&map->map_ops[i], addr, map->flags,
  209. map->grants[i].ref,
  210. map->grants[i].domid);
  211. gnttab_set_unmap_op(&map->unmap_ops[i], addr,
  212. map->flags, -1 /* handle */);
  213. }
  214. } else {
  215. /*
  216. * Setup the map_ops corresponding to the pte entries pointing
  217. * to the kernel linear addresses of the struct pages.
  218. * These ptes are completely different from the user ptes dealt
  219. * with find_grant_ptes.
  220. */
  221. for (i = 0; i < map->count; i++) {
  222. unsigned level;
  223. unsigned long address = (unsigned long)
  224. pfn_to_kaddr(page_to_pfn(map->pages[i]));
  225. pte_t *ptep;
  226. u64 pte_maddr = 0;
  227. BUG_ON(PageHighMem(map->pages[i]));
  228. ptep = lookup_address(address, &level);
  229. pte_maddr = arbitrary_virt_to_machine(ptep).maddr;
  230. gnttab_set_map_op(&map->kmap_ops[i], pte_maddr,
  231. map->flags |
  232. GNTMAP_host_map |
  233. GNTMAP_contains_pte,
  234. map->grants[i].ref,
  235. map->grants[i].domid);
  236. }
  237. }
  238. pr_debug("map %d+%d\n", map->index, map->count);
  239. err = gnttab_map_refs(map->map_ops, use_ptemod ? map->kmap_ops : NULL,
  240. map->pages, map->count);
  241. if (err)
  242. return err;
  243. for (i = 0; i < map->count; i++) {
  244. if (map->map_ops[i].status)
  245. err = -EINVAL;
  246. else {
  247. BUG_ON(map->map_ops[i].handle == -1);
  248. map->unmap_ops[i].handle = map->map_ops[i].handle;
  249. pr_debug("map handle=%d\n", map->map_ops[i].handle);
  250. }
  251. }
  252. return err;
  253. }
  254. static int __unmap_grant_pages(struct grant_map *map, int offset, int pages)
  255. {
  256. int i, err = 0;
  257. if (map->notify.flags & UNMAP_NOTIFY_CLEAR_BYTE) {
  258. int pgno = (map->notify.addr >> PAGE_SHIFT);
  259. if (pgno >= offset && pgno < offset + pages && use_ptemod) {
  260. void __user *tmp = (void __user *)
  261. map->vma->vm_start + map->notify.addr;
  262. err = copy_to_user(tmp, &err, 1);
  263. if (err)
  264. return -EFAULT;
  265. map->notify.flags &= ~UNMAP_NOTIFY_CLEAR_BYTE;
  266. } else if (pgno >= offset && pgno < offset + pages) {
  267. uint8_t *tmp = kmap(map->pages[pgno]);
  268. tmp[map->notify.addr & (PAGE_SIZE-1)] = 0;
  269. kunmap(map->pages[pgno]);
  270. map->notify.flags &= ~UNMAP_NOTIFY_CLEAR_BYTE;
  271. }
  272. }
  273. err = gnttab_unmap_refs(map->unmap_ops + offset,
  274. use_ptemod ? map->kmap_ops + offset : NULL, map->pages + offset,
  275. pages);
  276. if (err)
  277. return err;
  278. for (i = 0; i < pages; i++) {
  279. if (map->unmap_ops[offset+i].status)
  280. err = -EINVAL;
  281. pr_debug("unmap handle=%d st=%d\n",
  282. map->unmap_ops[offset+i].handle,
  283. map->unmap_ops[offset+i].status);
  284. map->unmap_ops[offset+i].handle = -1;
  285. }
  286. return err;
  287. }
  288. static int unmap_grant_pages(struct grant_map *map, int offset, int pages)
  289. {
  290. int range, err = 0;
  291. pr_debug("unmap %d+%d [%d+%d]\n", map->index, map->count, offset, pages);
  292. /* It is possible the requested range will have a "hole" where we
  293. * already unmapped some of the grants. Only unmap valid ranges.
  294. */
  295. while (pages && !err) {
  296. while (pages && map->unmap_ops[offset].handle == -1) {
  297. offset++;
  298. pages--;
  299. }
  300. range = 0;
  301. while (range < pages) {
  302. if (map->unmap_ops[offset+range].handle == -1) {
  303. range--;
  304. break;
  305. }
  306. range++;
  307. }
  308. err = __unmap_grant_pages(map, offset, range);
  309. offset += range;
  310. pages -= range;
  311. }
  312. return err;
  313. }
  314. /* ------------------------------------------------------------------ */
  315. static void gntdev_vma_open(struct vm_area_struct *vma)
  316. {
  317. struct grant_map *map = vma->vm_private_data;
  318. pr_debug("gntdev_vma_open %p\n", vma);
  319. atomic_inc(&map->users);
  320. }
  321. static void gntdev_vma_close(struct vm_area_struct *vma)
  322. {
  323. struct grant_map *map = vma->vm_private_data;
  324. pr_debug("gntdev_vma_close %p\n", vma);
  325. map->vma = NULL;
  326. vma->vm_private_data = NULL;
  327. gntdev_put_map(map);
  328. }
  329. static struct vm_operations_struct gntdev_vmops = {
  330. .open = gntdev_vma_open,
  331. .close = gntdev_vma_close,
  332. };
  333. /* ------------------------------------------------------------------ */
  334. static void mn_invl_range_start(struct mmu_notifier *mn,
  335. struct mm_struct *mm,
  336. unsigned long start, unsigned long end)
  337. {
  338. struct gntdev_priv *priv = container_of(mn, struct gntdev_priv, mn);
  339. struct grant_map *map;
  340. unsigned long mstart, mend;
  341. int err;
  342. spin_lock(&priv->lock);
  343. list_for_each_entry(map, &priv->maps, next) {
  344. if (!map->vma)
  345. continue;
  346. if (map->vma->vm_start >= end)
  347. continue;
  348. if (map->vma->vm_end <= start)
  349. continue;
  350. mstart = max(start, map->vma->vm_start);
  351. mend = min(end, map->vma->vm_end);
  352. pr_debug("map %d+%d (%lx %lx), range %lx %lx, mrange %lx %lx\n",
  353. map->index, map->count,
  354. map->vma->vm_start, map->vma->vm_end,
  355. start, end, mstart, mend);
  356. err = unmap_grant_pages(map,
  357. (mstart - map->vma->vm_start) >> PAGE_SHIFT,
  358. (mend - mstart) >> PAGE_SHIFT);
  359. WARN_ON(err);
  360. }
  361. spin_unlock(&priv->lock);
  362. }
  363. static void mn_invl_page(struct mmu_notifier *mn,
  364. struct mm_struct *mm,
  365. unsigned long address)
  366. {
  367. mn_invl_range_start(mn, mm, address, address + PAGE_SIZE);
  368. }
  369. static void mn_release(struct mmu_notifier *mn,
  370. struct mm_struct *mm)
  371. {
  372. struct gntdev_priv *priv = container_of(mn, struct gntdev_priv, mn);
  373. struct grant_map *map;
  374. int err;
  375. spin_lock(&priv->lock);
  376. list_for_each_entry(map, &priv->maps, next) {
  377. if (!map->vma)
  378. continue;
  379. pr_debug("map %d+%d (%lx %lx)\n",
  380. map->index, map->count,
  381. map->vma->vm_start, map->vma->vm_end);
  382. err = unmap_grant_pages(map, /* offset */ 0, map->count);
  383. WARN_ON(err);
  384. }
  385. spin_unlock(&priv->lock);
  386. }
  387. struct mmu_notifier_ops gntdev_mmu_ops = {
  388. .release = mn_release,
  389. .invalidate_page = mn_invl_page,
  390. .invalidate_range_start = mn_invl_range_start,
  391. };
  392. /* ------------------------------------------------------------------ */
  393. static int gntdev_open(struct inode *inode, struct file *flip)
  394. {
  395. struct gntdev_priv *priv;
  396. int ret = 0;
  397. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  398. if (!priv)
  399. return -ENOMEM;
  400. INIT_LIST_HEAD(&priv->maps);
  401. spin_lock_init(&priv->lock);
  402. if (use_ptemod) {
  403. priv->mm = get_task_mm(current);
  404. if (!priv->mm) {
  405. kfree(priv);
  406. return -ENOMEM;
  407. }
  408. priv->mn.ops = &gntdev_mmu_ops;
  409. ret = mmu_notifier_register(&priv->mn, priv->mm);
  410. mmput(priv->mm);
  411. }
  412. if (ret) {
  413. kfree(priv);
  414. return ret;
  415. }
  416. flip->private_data = priv;
  417. pr_debug("priv %p\n", priv);
  418. return 0;
  419. }
  420. static int gntdev_release(struct inode *inode, struct file *flip)
  421. {
  422. struct gntdev_priv *priv = flip->private_data;
  423. struct grant_map *map;
  424. pr_debug("priv %p\n", priv);
  425. while (!list_empty(&priv->maps)) {
  426. map = list_entry(priv->maps.next, struct grant_map, next);
  427. list_del(&map->next);
  428. gntdev_put_map(map);
  429. }
  430. if (use_ptemod)
  431. mmu_notifier_unregister(&priv->mn, priv->mm);
  432. kfree(priv);
  433. return 0;
  434. }
  435. static long gntdev_ioctl_map_grant_ref(struct gntdev_priv *priv,
  436. struct ioctl_gntdev_map_grant_ref __user *u)
  437. {
  438. struct ioctl_gntdev_map_grant_ref op;
  439. struct grant_map *map;
  440. int err;
  441. if (copy_from_user(&op, u, sizeof(op)) != 0)
  442. return -EFAULT;
  443. pr_debug("priv %p, add %d\n", priv, op.count);
  444. if (unlikely(op.count <= 0))
  445. return -EINVAL;
  446. err = -ENOMEM;
  447. map = gntdev_alloc_map(priv, op.count);
  448. if (!map)
  449. return err;
  450. if (unlikely(atomic_add_return(op.count, &pages_mapped) > limit)) {
  451. pr_debug("can't map: over limit\n");
  452. gntdev_put_map(map);
  453. return err;
  454. }
  455. if (copy_from_user(map->grants, &u->refs,
  456. sizeof(map->grants[0]) * op.count) != 0) {
  457. gntdev_put_map(map);
  458. return err;
  459. }
  460. spin_lock(&priv->lock);
  461. gntdev_add_map(priv, map);
  462. op.index = map->index << PAGE_SHIFT;
  463. spin_unlock(&priv->lock);
  464. if (copy_to_user(u, &op, sizeof(op)) != 0)
  465. return -EFAULT;
  466. return 0;
  467. }
  468. static long gntdev_ioctl_unmap_grant_ref(struct gntdev_priv *priv,
  469. struct ioctl_gntdev_unmap_grant_ref __user *u)
  470. {
  471. struct ioctl_gntdev_unmap_grant_ref op;
  472. struct grant_map *map;
  473. int err = -ENOENT;
  474. if (copy_from_user(&op, u, sizeof(op)) != 0)
  475. return -EFAULT;
  476. pr_debug("priv %p, del %d+%d\n", priv, (int)op.index, (int)op.count);
  477. spin_lock(&priv->lock);
  478. map = gntdev_find_map_index(priv, op.index >> PAGE_SHIFT, op.count);
  479. if (map) {
  480. list_del(&map->next);
  481. err = 0;
  482. }
  483. spin_unlock(&priv->lock);
  484. if (map)
  485. gntdev_put_map(map);
  486. return err;
  487. }
  488. static long gntdev_ioctl_get_offset_for_vaddr(struct gntdev_priv *priv,
  489. struct ioctl_gntdev_get_offset_for_vaddr __user *u)
  490. {
  491. struct ioctl_gntdev_get_offset_for_vaddr op;
  492. struct vm_area_struct *vma;
  493. struct grant_map *map;
  494. if (copy_from_user(&op, u, sizeof(op)) != 0)
  495. return -EFAULT;
  496. pr_debug("priv %p, offset for vaddr %lx\n", priv, (unsigned long)op.vaddr);
  497. vma = find_vma(current->mm, op.vaddr);
  498. if (!vma || vma->vm_ops != &gntdev_vmops)
  499. return -EINVAL;
  500. map = vma->vm_private_data;
  501. if (!map)
  502. return -EINVAL;
  503. op.offset = map->index << PAGE_SHIFT;
  504. op.count = map->count;
  505. if (copy_to_user(u, &op, sizeof(op)) != 0)
  506. return -EFAULT;
  507. return 0;
  508. }
  509. static long gntdev_ioctl_notify(struct gntdev_priv *priv, void __user *u)
  510. {
  511. struct ioctl_gntdev_unmap_notify op;
  512. struct grant_map *map;
  513. int rc;
  514. int out_flags;
  515. unsigned int out_event;
  516. if (copy_from_user(&op, u, sizeof(op)))
  517. return -EFAULT;
  518. if (op.action & ~(UNMAP_NOTIFY_CLEAR_BYTE|UNMAP_NOTIFY_SEND_EVENT))
  519. return -EINVAL;
  520. /* We need to grab a reference to the event channel we are going to use
  521. * to send the notify before releasing the reference we may already have
  522. * (if someone has called this ioctl twice). This is required so that
  523. * it is possible to change the clear_byte part of the notification
  524. * without disturbing the event channel part, which may now be the last
  525. * reference to that event channel.
  526. */
  527. if (op.action & UNMAP_NOTIFY_SEND_EVENT) {
  528. if (evtchn_get(op.event_channel_port))
  529. return -EINVAL;
  530. }
  531. out_flags = op.action;
  532. out_event = op.event_channel_port;
  533. spin_lock(&priv->lock);
  534. list_for_each_entry(map, &priv->maps, next) {
  535. uint64_t begin = map->index << PAGE_SHIFT;
  536. uint64_t end = (map->index + map->count) << PAGE_SHIFT;
  537. if (op.index >= begin && op.index < end)
  538. goto found;
  539. }
  540. rc = -ENOENT;
  541. goto unlock_out;
  542. found:
  543. if ((op.action & UNMAP_NOTIFY_CLEAR_BYTE) &&
  544. (map->flags & GNTMAP_readonly)) {
  545. rc = -EINVAL;
  546. goto unlock_out;
  547. }
  548. out_flags = map->notify.flags;
  549. out_event = map->notify.event;
  550. map->notify.flags = op.action;
  551. map->notify.addr = op.index - (map->index << PAGE_SHIFT);
  552. map->notify.event = op.event_channel_port;
  553. rc = 0;
  554. unlock_out:
  555. spin_unlock(&priv->lock);
  556. /* Drop the reference to the event channel we did not save in the map */
  557. if (out_flags & UNMAP_NOTIFY_SEND_EVENT)
  558. evtchn_put(out_event);
  559. return rc;
  560. }
  561. static long gntdev_ioctl(struct file *flip,
  562. unsigned int cmd, unsigned long arg)
  563. {
  564. struct gntdev_priv *priv = flip->private_data;
  565. void __user *ptr = (void __user *)arg;
  566. switch (cmd) {
  567. case IOCTL_GNTDEV_MAP_GRANT_REF:
  568. return gntdev_ioctl_map_grant_ref(priv, ptr);
  569. case IOCTL_GNTDEV_UNMAP_GRANT_REF:
  570. return gntdev_ioctl_unmap_grant_ref(priv, ptr);
  571. case IOCTL_GNTDEV_GET_OFFSET_FOR_VADDR:
  572. return gntdev_ioctl_get_offset_for_vaddr(priv, ptr);
  573. case IOCTL_GNTDEV_SET_UNMAP_NOTIFY:
  574. return gntdev_ioctl_notify(priv, ptr);
  575. default:
  576. pr_debug("priv %p, unknown cmd %x\n", priv, cmd);
  577. return -ENOIOCTLCMD;
  578. }
  579. return 0;
  580. }
  581. static int gntdev_mmap(struct file *flip, struct vm_area_struct *vma)
  582. {
  583. struct gntdev_priv *priv = flip->private_data;
  584. int index = vma->vm_pgoff;
  585. int count = (vma->vm_end - vma->vm_start) >> PAGE_SHIFT;
  586. struct grant_map *map;
  587. int i, err = -EINVAL;
  588. if ((vma->vm_flags & VM_WRITE) && !(vma->vm_flags & VM_SHARED))
  589. return -EINVAL;
  590. pr_debug("map %d+%d at %lx (pgoff %lx)\n",
  591. index, count, vma->vm_start, vma->vm_pgoff);
  592. spin_lock(&priv->lock);
  593. map = gntdev_find_map_index(priv, index, count);
  594. if (!map)
  595. goto unlock_out;
  596. if (use_ptemod && map->vma)
  597. goto unlock_out;
  598. if (use_ptemod && priv->mm != vma->vm_mm) {
  599. printk(KERN_WARNING "Huh? Other mm?\n");
  600. goto unlock_out;
  601. }
  602. atomic_inc(&map->users);
  603. vma->vm_ops = &gntdev_vmops;
  604. vma->vm_flags |= VM_RESERVED|VM_DONTEXPAND;
  605. if (use_ptemod)
  606. vma->vm_flags |= VM_DONTCOPY;
  607. vma->vm_private_data = map;
  608. if (use_ptemod)
  609. map->vma = vma;
  610. if (map->flags) {
  611. if ((vma->vm_flags & VM_WRITE) &&
  612. (map->flags & GNTMAP_readonly))
  613. goto out_unlock_put;
  614. } else {
  615. map->flags = GNTMAP_host_map;
  616. if (!(vma->vm_flags & VM_WRITE))
  617. map->flags |= GNTMAP_readonly;
  618. }
  619. spin_unlock(&priv->lock);
  620. if (use_ptemod) {
  621. err = apply_to_page_range(vma->vm_mm, vma->vm_start,
  622. vma->vm_end - vma->vm_start,
  623. find_grant_ptes, map);
  624. if (err) {
  625. printk(KERN_WARNING "find_grant_ptes() failure.\n");
  626. goto out_put_map;
  627. }
  628. }
  629. err = map_grant_pages(map);
  630. if (err)
  631. goto out_put_map;
  632. if (!use_ptemod) {
  633. for (i = 0; i < count; i++) {
  634. err = vm_insert_page(vma, vma->vm_start + i*PAGE_SIZE,
  635. map->pages[i]);
  636. if (err)
  637. goto out_put_map;
  638. }
  639. }
  640. return 0;
  641. unlock_out:
  642. spin_unlock(&priv->lock);
  643. return err;
  644. out_unlock_put:
  645. spin_unlock(&priv->lock);
  646. out_put_map:
  647. if (use_ptemod)
  648. map->vma = NULL;
  649. gntdev_put_map(map);
  650. return err;
  651. }
  652. static const struct file_operations gntdev_fops = {
  653. .owner = THIS_MODULE,
  654. .open = gntdev_open,
  655. .release = gntdev_release,
  656. .mmap = gntdev_mmap,
  657. .unlocked_ioctl = gntdev_ioctl
  658. };
  659. static struct miscdevice gntdev_miscdev = {
  660. .minor = MISC_DYNAMIC_MINOR,
  661. .name = "xen/gntdev",
  662. .fops = &gntdev_fops,
  663. };
  664. /* ------------------------------------------------------------------ */
  665. static int __init gntdev_init(void)
  666. {
  667. int err;
  668. if (!xen_domain())
  669. return -ENODEV;
  670. use_ptemod = xen_pv_domain();
  671. err = misc_register(&gntdev_miscdev);
  672. if (err != 0) {
  673. printk(KERN_ERR "Could not register gntdev device\n");
  674. return err;
  675. }
  676. return 0;
  677. }
  678. static void __exit gntdev_exit(void)
  679. {
  680. misc_deregister(&gntdev_miscdev);
  681. }
  682. module_init(gntdev_init);
  683. module_exit(gntdev_exit);
  684. /* ------------------------------------------------------------------ */