privcmd.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582
  1. /******************************************************************************
  2. * privcmd.c
  3. *
  4. * Interface to privileged domain-0 commands.
  5. *
  6. * Copyright (c) 2002-2004, K A Fraser, B Dragovic
  7. */
  8. #define pr_fmt(fmt) "xen:" KBUILD_MODNAME ": " fmt
  9. #include <linux/kernel.h>
  10. #include <linux/module.h>
  11. #include <linux/sched.h>
  12. #include <linux/slab.h>
  13. #include <linux/string.h>
  14. #include <linux/errno.h>
  15. #include <linux/mm.h>
  16. #include <linux/mman.h>
  17. #include <linux/uaccess.h>
  18. #include <linux/swap.h>
  19. #include <linux/highmem.h>
  20. #include <linux/pagemap.h>
  21. #include <linux/seq_file.h>
  22. #include <linux/miscdevice.h>
  23. #include <asm/pgalloc.h>
  24. #include <asm/pgtable.h>
  25. #include <asm/tlb.h>
  26. #include <asm/xen/hypervisor.h>
  27. #include <asm/xen/hypercall.h>
  28. #include <xen/xen.h>
  29. #include <xen/privcmd.h>
  30. #include <xen/interface/xen.h>
  31. #include <xen/features.h>
  32. #include <xen/page.h>
  33. #include <xen/xen-ops.h>
  34. #include <xen/balloon.h>
  35. #include "privcmd.h"
  36. MODULE_LICENSE("GPL");
  37. #define PRIV_VMA_LOCKED ((void *)1)
  38. #ifndef HAVE_ARCH_PRIVCMD_MMAP
  39. static int privcmd_enforce_singleshot_mapping(struct vm_area_struct *vma);
  40. #endif
  41. static long privcmd_ioctl_hypercall(void __user *udata)
  42. {
  43. struct privcmd_hypercall hypercall;
  44. long ret;
  45. if (copy_from_user(&hypercall, udata, sizeof(hypercall)))
  46. return -EFAULT;
  47. ret = privcmd_call(hypercall.op,
  48. hypercall.arg[0], hypercall.arg[1],
  49. hypercall.arg[2], hypercall.arg[3],
  50. hypercall.arg[4]);
  51. return ret;
  52. }
  53. static void free_page_list(struct list_head *pages)
  54. {
  55. struct page *p, *n;
  56. list_for_each_entry_safe(p, n, pages, lru)
  57. __free_page(p);
  58. INIT_LIST_HEAD(pages);
  59. }
  60. /*
  61. * Given an array of items in userspace, return a list of pages
  62. * containing the data. If copying fails, either because of memory
  63. * allocation failure or a problem reading user memory, return an
  64. * error code; its up to the caller to dispose of any partial list.
  65. */
  66. static int gather_array(struct list_head *pagelist,
  67. unsigned nelem, size_t size,
  68. const void __user *data)
  69. {
  70. unsigned pageidx;
  71. void *pagedata;
  72. int ret;
  73. if (size > PAGE_SIZE)
  74. return 0;
  75. pageidx = PAGE_SIZE;
  76. pagedata = NULL; /* quiet, gcc */
  77. while (nelem--) {
  78. if (pageidx > PAGE_SIZE-size) {
  79. struct page *page = alloc_page(GFP_KERNEL);
  80. ret = -ENOMEM;
  81. if (page == NULL)
  82. goto fail;
  83. pagedata = page_address(page);
  84. list_add_tail(&page->lru, pagelist);
  85. pageidx = 0;
  86. }
  87. ret = -EFAULT;
  88. if (copy_from_user(pagedata + pageidx, data, size))
  89. goto fail;
  90. data += size;
  91. pageidx += size;
  92. }
  93. ret = 0;
  94. fail:
  95. return ret;
  96. }
  97. /*
  98. * Call function "fn" on each element of the array fragmented
  99. * over a list of pages.
  100. */
  101. static int traverse_pages(unsigned nelem, size_t size,
  102. struct list_head *pos,
  103. int (*fn)(void *data, void *state),
  104. void *state)
  105. {
  106. void *pagedata;
  107. unsigned pageidx;
  108. int ret = 0;
  109. BUG_ON(size > PAGE_SIZE);
  110. pageidx = PAGE_SIZE;
  111. pagedata = NULL; /* hush, gcc */
  112. while (nelem--) {
  113. if (pageidx > PAGE_SIZE-size) {
  114. struct page *page;
  115. pos = pos->next;
  116. page = list_entry(pos, struct page, lru);
  117. pagedata = page_address(page);
  118. pageidx = 0;
  119. }
  120. ret = (*fn)(pagedata + pageidx, state);
  121. if (ret)
  122. break;
  123. pageidx += size;
  124. }
  125. return ret;
  126. }
  127. struct mmap_mfn_state {
  128. unsigned long va;
  129. struct vm_area_struct *vma;
  130. domid_t domain;
  131. };
  132. static int mmap_mfn_range(void *data, void *state)
  133. {
  134. struct privcmd_mmap_entry *msg = data;
  135. struct mmap_mfn_state *st = state;
  136. struct vm_area_struct *vma = st->vma;
  137. int rc;
  138. /* Do not allow range to wrap the address space. */
  139. if ((msg->npages > (LONG_MAX >> PAGE_SHIFT)) ||
  140. ((unsigned long)(msg->npages << PAGE_SHIFT) >= -st->va))
  141. return -EINVAL;
  142. /* Range chunks must be contiguous in va space. */
  143. if ((msg->va != st->va) ||
  144. ((msg->va+(msg->npages<<PAGE_SHIFT)) > vma->vm_end))
  145. return -EINVAL;
  146. rc = xen_remap_domain_mfn_range(vma,
  147. msg->va & PAGE_MASK,
  148. msg->mfn, msg->npages,
  149. vma->vm_page_prot,
  150. st->domain, NULL);
  151. if (rc < 0)
  152. return rc;
  153. st->va += msg->npages << PAGE_SHIFT;
  154. return 0;
  155. }
  156. static long privcmd_ioctl_mmap(void __user *udata)
  157. {
  158. struct privcmd_mmap mmapcmd;
  159. struct mm_struct *mm = current->mm;
  160. struct vm_area_struct *vma;
  161. int rc;
  162. LIST_HEAD(pagelist);
  163. struct mmap_mfn_state state;
  164. /* We only support privcmd_ioctl_mmap_batch for auto translated. */
  165. if (xen_feature(XENFEAT_auto_translated_physmap))
  166. return -ENOSYS;
  167. if (copy_from_user(&mmapcmd, udata, sizeof(mmapcmd)))
  168. return -EFAULT;
  169. rc = gather_array(&pagelist,
  170. mmapcmd.num, sizeof(struct privcmd_mmap_entry),
  171. mmapcmd.entry);
  172. if (rc || list_empty(&pagelist))
  173. goto out;
  174. down_write(&mm->mmap_sem);
  175. {
  176. struct page *page = list_first_entry(&pagelist,
  177. struct page, lru);
  178. struct privcmd_mmap_entry *msg = page_address(page);
  179. vma = find_vma(mm, msg->va);
  180. rc = -EINVAL;
  181. if (!vma || (msg->va != vma->vm_start) ||
  182. !privcmd_enforce_singleshot_mapping(vma))
  183. goto out_up;
  184. }
  185. state.va = vma->vm_start;
  186. state.vma = vma;
  187. state.domain = mmapcmd.dom;
  188. rc = traverse_pages(mmapcmd.num, sizeof(struct privcmd_mmap_entry),
  189. &pagelist,
  190. mmap_mfn_range, &state);
  191. out_up:
  192. up_write(&mm->mmap_sem);
  193. out:
  194. free_page_list(&pagelist);
  195. return rc;
  196. }
  197. struct mmap_batch_state {
  198. domid_t domain;
  199. unsigned long va;
  200. struct vm_area_struct *vma;
  201. int index;
  202. /* A tristate:
  203. * 0 for no errors
  204. * 1 if at least one error has happened (and no
  205. * -ENOENT errors have happened)
  206. * -ENOENT if at least 1 -ENOENT has happened.
  207. */
  208. int global_error;
  209. int version;
  210. /* User-space mfn array to store errors in the second pass for V1. */
  211. xen_pfn_t __user *user_mfn;
  212. /* User-space int array to store errors in the second pass for V2. */
  213. int __user *user_err;
  214. };
  215. /* auto translated dom0 note: if domU being created is PV, then mfn is
  216. * mfn(addr on bus). If it's auto xlated, then mfn is pfn (input to HAP).
  217. */
  218. static int mmap_batch_fn(void *data, void *state)
  219. {
  220. xen_pfn_t *mfnp = data;
  221. struct mmap_batch_state *st = state;
  222. struct vm_area_struct *vma = st->vma;
  223. struct page **pages = vma->vm_private_data;
  224. struct page *cur_page = NULL;
  225. int ret;
  226. if (xen_feature(XENFEAT_auto_translated_physmap))
  227. cur_page = pages[st->index++];
  228. ret = xen_remap_domain_mfn_range(st->vma, st->va & PAGE_MASK, *mfnp, 1,
  229. st->vma->vm_page_prot, st->domain,
  230. &cur_page);
  231. /* Store error code for second pass. */
  232. if (st->version == 1) {
  233. if (ret < 0) {
  234. /*
  235. * V1 encodes the error codes in the 32bit top nibble of the
  236. * mfn (with its known limitations vis-a-vis 64 bit callers).
  237. */
  238. *mfnp |= (ret == -ENOENT) ?
  239. PRIVCMD_MMAPBATCH_PAGED_ERROR :
  240. PRIVCMD_MMAPBATCH_MFN_ERROR;
  241. }
  242. } else { /* st->version == 2 */
  243. *((int *) mfnp) = ret;
  244. }
  245. /* And see if it affects the global_error. */
  246. if (ret < 0) {
  247. if (ret == -ENOENT)
  248. st->global_error = -ENOENT;
  249. else {
  250. /* Record that at least one error has happened. */
  251. if (st->global_error == 0)
  252. st->global_error = 1;
  253. }
  254. }
  255. st->va += PAGE_SIZE;
  256. return 0;
  257. }
  258. static int mmap_return_errors(void *data, void *state)
  259. {
  260. struct mmap_batch_state *st = state;
  261. if (st->version == 1) {
  262. xen_pfn_t mfnp = *((xen_pfn_t *) data);
  263. if (mfnp & PRIVCMD_MMAPBATCH_MFN_ERROR)
  264. return __put_user(mfnp, st->user_mfn++);
  265. else
  266. st->user_mfn++;
  267. } else { /* st->version == 2 */
  268. int err = *((int *) data);
  269. if (err)
  270. return __put_user(err, st->user_err++);
  271. else
  272. st->user_err++;
  273. }
  274. return 0;
  275. }
  276. /* Allocate pfns that are then mapped with gmfns from foreign domid. Update
  277. * the vma with the page info to use later.
  278. * Returns: 0 if success, otherwise -errno
  279. */
  280. static int alloc_empty_pages(struct vm_area_struct *vma, int numpgs)
  281. {
  282. int rc;
  283. struct page **pages;
  284. pages = kcalloc(numpgs, sizeof(pages[0]), GFP_KERNEL);
  285. if (pages == NULL)
  286. return -ENOMEM;
  287. rc = alloc_xenballooned_pages(numpgs, pages, 0);
  288. if (rc != 0) {
  289. pr_warn("%s Could not alloc %d pfns rc:%d\n", __func__,
  290. numpgs, rc);
  291. kfree(pages);
  292. return -ENOMEM;
  293. }
  294. BUG_ON(vma->vm_private_data != PRIV_VMA_LOCKED);
  295. vma->vm_private_data = pages;
  296. return 0;
  297. }
  298. static struct vm_operations_struct privcmd_vm_ops;
  299. static long privcmd_ioctl_mmap_batch(void __user *udata, int version)
  300. {
  301. int ret;
  302. struct privcmd_mmapbatch_v2 m;
  303. struct mm_struct *mm = current->mm;
  304. struct vm_area_struct *vma;
  305. unsigned long nr_pages;
  306. LIST_HEAD(pagelist);
  307. struct mmap_batch_state state;
  308. switch (version) {
  309. case 1:
  310. if (copy_from_user(&m, udata, sizeof(struct privcmd_mmapbatch)))
  311. return -EFAULT;
  312. /* Returns per-frame error in m.arr. */
  313. m.err = NULL;
  314. if (!access_ok(VERIFY_WRITE, m.arr, m.num * sizeof(*m.arr)))
  315. return -EFAULT;
  316. break;
  317. case 2:
  318. if (copy_from_user(&m, udata, sizeof(struct privcmd_mmapbatch_v2)))
  319. return -EFAULT;
  320. /* Returns per-frame error code in m.err. */
  321. if (!access_ok(VERIFY_WRITE, m.err, m.num * (sizeof(*m.err))))
  322. return -EFAULT;
  323. break;
  324. default:
  325. return -EINVAL;
  326. }
  327. nr_pages = m.num;
  328. if ((m.num <= 0) || (nr_pages > (LONG_MAX >> PAGE_SHIFT)))
  329. return -EINVAL;
  330. ret = gather_array(&pagelist, m.num, sizeof(xen_pfn_t), m.arr);
  331. if (ret)
  332. goto out;
  333. if (list_empty(&pagelist)) {
  334. ret = -EINVAL;
  335. goto out;
  336. }
  337. if (version == 2) {
  338. /* Zero error array now to only copy back actual errors. */
  339. if (clear_user(m.err, sizeof(int) * m.num)) {
  340. ret = -EFAULT;
  341. goto out;
  342. }
  343. }
  344. down_write(&mm->mmap_sem);
  345. vma = find_vma(mm, m.addr);
  346. if (!vma ||
  347. vma->vm_ops != &privcmd_vm_ops ||
  348. (m.addr != vma->vm_start) ||
  349. ((m.addr + (nr_pages << PAGE_SHIFT)) != vma->vm_end) ||
  350. !privcmd_enforce_singleshot_mapping(vma)) {
  351. up_write(&mm->mmap_sem);
  352. ret = -EINVAL;
  353. goto out;
  354. }
  355. if (xen_feature(XENFEAT_auto_translated_physmap)) {
  356. ret = alloc_empty_pages(vma, m.num);
  357. if (ret < 0) {
  358. up_write(&mm->mmap_sem);
  359. goto out;
  360. }
  361. }
  362. state.domain = m.dom;
  363. state.vma = vma;
  364. state.va = m.addr;
  365. state.index = 0;
  366. state.global_error = 0;
  367. state.version = version;
  368. /* mmap_batch_fn guarantees ret == 0 */
  369. BUG_ON(traverse_pages(m.num, sizeof(xen_pfn_t),
  370. &pagelist, mmap_batch_fn, &state));
  371. up_write(&mm->mmap_sem);
  372. if (state.global_error) {
  373. /* Write back errors in second pass. */
  374. state.user_mfn = (xen_pfn_t *)m.arr;
  375. state.user_err = m.err;
  376. ret = traverse_pages(m.num, sizeof(xen_pfn_t),
  377. &pagelist, mmap_return_errors, &state);
  378. } else
  379. ret = 0;
  380. /* If we have not had any EFAULT-like global errors then set the global
  381. * error to -ENOENT if necessary. */
  382. if ((ret == 0) && (state.global_error == -ENOENT))
  383. ret = -ENOENT;
  384. out:
  385. free_page_list(&pagelist);
  386. return ret;
  387. }
  388. static long privcmd_ioctl(struct file *file,
  389. unsigned int cmd, unsigned long data)
  390. {
  391. int ret = -ENOSYS;
  392. void __user *udata = (void __user *) data;
  393. switch (cmd) {
  394. case IOCTL_PRIVCMD_HYPERCALL:
  395. ret = privcmd_ioctl_hypercall(udata);
  396. break;
  397. case IOCTL_PRIVCMD_MMAP:
  398. ret = privcmd_ioctl_mmap(udata);
  399. break;
  400. case IOCTL_PRIVCMD_MMAPBATCH:
  401. ret = privcmd_ioctl_mmap_batch(udata, 1);
  402. break;
  403. case IOCTL_PRIVCMD_MMAPBATCH_V2:
  404. ret = privcmd_ioctl_mmap_batch(udata, 2);
  405. break;
  406. default:
  407. ret = -EINVAL;
  408. break;
  409. }
  410. return ret;
  411. }
  412. static void privcmd_close(struct vm_area_struct *vma)
  413. {
  414. struct page **pages = vma->vm_private_data;
  415. int numpgs = (vma->vm_end - vma->vm_start) >> PAGE_SHIFT;
  416. if (!xen_feature(XENFEAT_auto_translated_physmap) || !numpgs || !pages)
  417. return;
  418. xen_unmap_domain_mfn_range(vma, numpgs, pages);
  419. free_xenballooned_pages(numpgs, pages);
  420. kfree(pages);
  421. }
  422. static int privcmd_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  423. {
  424. printk(KERN_DEBUG "privcmd_fault: vma=%p %lx-%lx, pgoff=%lx, uv=%p\n",
  425. vma, vma->vm_start, vma->vm_end,
  426. vmf->pgoff, vmf->virtual_address);
  427. return VM_FAULT_SIGBUS;
  428. }
  429. static struct vm_operations_struct privcmd_vm_ops = {
  430. .close = privcmd_close,
  431. .fault = privcmd_fault
  432. };
  433. static int privcmd_mmap(struct file *file, struct vm_area_struct *vma)
  434. {
  435. /* DONTCOPY is essential for Xen because copy_page_range doesn't know
  436. * how to recreate these mappings */
  437. vma->vm_flags |= VM_IO | VM_PFNMAP | VM_DONTCOPY |
  438. VM_DONTEXPAND | VM_DONTDUMP;
  439. vma->vm_ops = &privcmd_vm_ops;
  440. vma->vm_private_data = NULL;
  441. return 0;
  442. }
  443. static int privcmd_enforce_singleshot_mapping(struct vm_area_struct *vma)
  444. {
  445. return !cmpxchg(&vma->vm_private_data, NULL, PRIV_VMA_LOCKED);
  446. }
  447. const struct file_operations xen_privcmd_fops = {
  448. .owner = THIS_MODULE,
  449. .unlocked_ioctl = privcmd_ioctl,
  450. .mmap = privcmd_mmap,
  451. };
  452. EXPORT_SYMBOL_GPL(xen_privcmd_fops);
  453. static struct miscdevice privcmd_dev = {
  454. .minor = MISC_DYNAMIC_MINOR,
  455. .name = "xen/privcmd",
  456. .fops = &xen_privcmd_fops,
  457. };
  458. static int __init privcmd_init(void)
  459. {
  460. int err;
  461. if (!xen_domain())
  462. return -ENODEV;
  463. err = misc_register(&privcmd_dev);
  464. if (err != 0) {
  465. pr_err("Could not register Xen privcmd device\n");
  466. return err;
  467. }
  468. return 0;
  469. }
  470. static void __exit privcmd_exit(void)
  471. {
  472. misc_deregister(&privcmd_dev);
  473. }
  474. module_init(privcmd_init);
  475. module_exit(privcmd_exit);