gntdev.c 19 KB

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