privcmd.c 14 KB

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