privcmd.c 13 KB

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