1 // SPDX-License-Identifier: GPL-2.0-only
2 /******************************************************************************
5 * Interface to privileged domain-0 commands.
7 * Copyright (c) 2002-2004, K A Fraser, B Dragovic
10 #define pr_fmt(fmt) "xen:" KBUILD_MODNAME ": " fmt
12 #include <linux/eventfd.h>
13 #include <linux/file.h>
14 #include <linux/kernel.h>
15 #include <linux/module.h>
16 #include <linux/mutex.h>
17 #include <linux/poll.h>
18 #include <linux/sched.h>
19 #include <linux/slab.h>
20 #include <linux/string.h>
21 #include <linux/workqueue.h>
22 #include <linux/errno.h>
24 #include <linux/mman.h>
25 #include <linux/uaccess.h>
26 #include <linux/swap.h>
27 #include <linux/highmem.h>
28 #include <linux/pagemap.h>
29 #include <linux/seq_file.h>
30 #include <linux/miscdevice.h>
31 #include <linux/moduleparam.h>
32 #include <linux/virtio_mmio.h>
34 #include <asm/xen/hypervisor.h>
35 #include <asm/xen/hypercall.h>
38 #include <xen/events.h>
39 #include <xen/privcmd.h>
40 #include <xen/interface/xen.h>
41 #include <xen/interface/memory.h>
42 #include <xen/interface/hvm/dm_op.h>
43 #include <xen/interface/hvm/ioreq.h>
44 #include <xen/features.h>
46 #include <xen/xen-ops.h>
47 #include <xen/balloon.h>
51 MODULE_LICENSE("GPL");
53 #define PRIV_VMA_LOCKED ((void *)1)
55 static unsigned int privcmd_dm_op_max_num = 16;
56 module_param_named(dm_op_max_nr_bufs, privcmd_dm_op_max_num, uint, 0644);
57 MODULE_PARM_DESC(dm_op_max_nr_bufs,
58 "Maximum number of buffers per dm_op hypercall");
60 static unsigned int privcmd_dm_op_buf_max_size = 4096;
61 module_param_named(dm_op_buf_max_size, privcmd_dm_op_buf_max_size, uint,
63 MODULE_PARM_DESC(dm_op_buf_max_size,
64 "Maximum size of a dm_op hypercall buffer");
70 static int privcmd_vma_range_is_mapped(
71 struct vm_area_struct *vma,
73 unsigned long nr_pages);
75 static long privcmd_ioctl_hypercall(struct file *file, void __user *udata)
77 struct privcmd_data *data = file->private_data;
78 struct privcmd_hypercall hypercall;
81 /* Disallow arbitrary hypercalls if restricted */
82 if (data->domid != DOMID_INVALID)
85 if (copy_from_user(&hypercall, udata, sizeof(hypercall)))
88 xen_preemptible_hcall_begin();
89 ret = privcmd_call(hypercall.op,
90 hypercall.arg[0], hypercall.arg[1],
91 hypercall.arg[2], hypercall.arg[3],
93 xen_preemptible_hcall_end();
98 static void free_page_list(struct list_head *pages)
102 list_for_each_entry_safe(p, n, pages, lru)
105 INIT_LIST_HEAD(pages);
109 * Given an array of items in userspace, return a list of pages
110 * containing the data. If copying fails, either because of memory
111 * allocation failure or a problem reading user memory, return an
112 * error code; its up to the caller to dispose of any partial list.
114 static int gather_array(struct list_head *pagelist,
115 unsigned nelem, size_t size,
116 const void __user *data)
122 if (size > PAGE_SIZE)
126 pagedata = NULL; /* quiet, gcc */
128 if (pageidx > PAGE_SIZE-size) {
129 struct page *page = alloc_page(GFP_KERNEL);
135 pagedata = page_address(page);
137 list_add_tail(&page->lru, pagelist);
142 if (copy_from_user(pagedata + pageidx, data, size))
156 * Call function "fn" on each element of the array fragmented
157 * over a list of pages.
159 static int traverse_pages(unsigned nelem, size_t size,
160 struct list_head *pos,
161 int (*fn)(void *data, void *state),
168 BUG_ON(size > PAGE_SIZE);
171 pagedata = NULL; /* hush, gcc */
174 if (pageidx > PAGE_SIZE-size) {
177 page = list_entry(pos, struct page, lru);
178 pagedata = page_address(page);
182 ret = (*fn)(pagedata + pageidx, state);
192 * Similar to traverse_pages, but use each page as a "block" of
193 * data to be processed as one unit.
195 static int traverse_pages_block(unsigned nelem, size_t size,
196 struct list_head *pos,
197 int (*fn)(void *data, int nr, void *state),
203 BUG_ON(size > PAGE_SIZE);
206 int nr = (PAGE_SIZE/size);
211 page = list_entry(pos, struct page, lru);
212 pagedata = page_address(page);
213 ret = (*fn)(pagedata, nr, state);
222 struct mmap_gfn_state {
224 struct vm_area_struct *vma;
228 static int mmap_gfn_range(void *data, void *state)
230 struct privcmd_mmap_entry *msg = data;
231 struct mmap_gfn_state *st = state;
232 struct vm_area_struct *vma = st->vma;
235 /* Do not allow range to wrap the address space. */
236 if ((msg->npages > (LONG_MAX >> PAGE_SHIFT)) ||
237 ((unsigned long)(msg->npages << PAGE_SHIFT) >= -st->va))
240 /* Range chunks must be contiguous in va space. */
241 if ((msg->va != st->va) ||
242 ((msg->va+(msg->npages<<PAGE_SHIFT)) > vma->vm_end))
245 rc = xen_remap_domain_gfn_range(vma,
247 msg->mfn, msg->npages,
253 st->va += msg->npages << PAGE_SHIFT;
258 static long privcmd_ioctl_mmap(struct file *file, void __user *udata)
260 struct privcmd_data *data = file->private_data;
261 struct privcmd_mmap mmapcmd;
262 struct mm_struct *mm = current->mm;
263 struct vm_area_struct *vma;
266 struct mmap_gfn_state state;
268 /* We only support privcmd_ioctl_mmap_batch for non-auto-translated. */
269 if (xen_feature(XENFEAT_auto_translated_physmap))
272 if (copy_from_user(&mmapcmd, udata, sizeof(mmapcmd)))
275 /* If restriction is in place, check the domid matches */
276 if (data->domid != DOMID_INVALID && data->domid != mmapcmd.dom)
279 rc = gather_array(&pagelist,
280 mmapcmd.num, sizeof(struct privcmd_mmap_entry),
283 if (rc || list_empty(&pagelist))
289 struct page *page = list_first_entry(&pagelist,
291 struct privcmd_mmap_entry *msg = page_address(page);
293 vma = vma_lookup(mm, msg->va);
296 if (!vma || (msg->va != vma->vm_start) || vma->vm_private_data)
298 vma->vm_private_data = PRIV_VMA_LOCKED;
301 state.va = vma->vm_start;
303 state.domain = mmapcmd.dom;
305 rc = traverse_pages(mmapcmd.num, sizeof(struct privcmd_mmap_entry),
307 mmap_gfn_range, &state);
311 mmap_write_unlock(mm);
314 free_page_list(&pagelist);
319 struct mmap_batch_state {
322 struct vm_area_struct *vma;
326 * 1 if at least one error has happened (and no
327 * -ENOENT errors have happened)
328 * -ENOENT if at least 1 -ENOENT has happened.
333 /* User-space gfn array to store errors in the second pass for V1. */
334 xen_pfn_t __user *user_gfn;
335 /* User-space int array to store errors in the second pass for V2. */
336 int __user *user_err;
339 /* auto translated dom0 note: if domU being created is PV, then gfn is
340 * mfn(addr on bus). If it's auto xlated, then gfn is pfn (input to HAP).
342 static int mmap_batch_fn(void *data, int nr, void *state)
344 xen_pfn_t *gfnp = data;
345 struct mmap_batch_state *st = state;
346 struct vm_area_struct *vma = st->vma;
347 struct page **pages = vma->vm_private_data;
348 struct page **cur_pages = NULL;
351 if (xen_feature(XENFEAT_auto_translated_physmap))
352 cur_pages = &pages[st->index];
355 ret = xen_remap_domain_gfn_array(st->vma, st->va & PAGE_MASK, gfnp, nr,
356 (int *)gfnp, st->vma->vm_page_prot,
357 st->domain, cur_pages);
359 /* Adjust the global_error? */
362 st->global_error = -ENOENT;
364 /* Record that at least one error has happened. */
365 if (st->global_error == 0)
366 st->global_error = 1;
369 st->va += XEN_PAGE_SIZE * nr;
370 st->index += nr / XEN_PFN_PER_PAGE;
375 static int mmap_return_error(int err, struct mmap_batch_state *st)
379 if (st->version == 1) {
383 ret = get_user(gfn, st->user_gfn);
387 * V1 encodes the error codes in the 32bit top
388 * nibble of the gfn (with its known
389 * limitations vis-a-vis 64 bit callers).
391 gfn |= (err == -ENOENT) ?
392 PRIVCMD_MMAPBATCH_PAGED_ERROR :
393 PRIVCMD_MMAPBATCH_MFN_ERROR;
394 return __put_user(gfn, st->user_gfn++);
397 } else { /* st->version == 2 */
399 return __put_user(err, st->user_err++);
407 static int mmap_return_errors(void *data, int nr, void *state)
409 struct mmap_batch_state *st = state;
414 for (i = 0; i < nr; i++) {
415 ret = mmap_return_error(errs[i], st);
422 /* Allocate pfns that are then mapped with gfns from foreign domid. Update
423 * the vma with the page info to use later.
424 * Returns: 0 if success, otherwise -errno
426 static int alloc_empty_pages(struct vm_area_struct *vma, int numpgs)
431 pages = kvcalloc(numpgs, sizeof(pages[0]), GFP_KERNEL);
435 rc = xen_alloc_unpopulated_pages(numpgs, pages);
437 pr_warn("%s Could not alloc %d pfns rc:%d\n", __func__,
442 BUG_ON(vma->vm_private_data != NULL);
443 vma->vm_private_data = pages;
448 static const struct vm_operations_struct privcmd_vm_ops;
450 static long privcmd_ioctl_mmap_batch(
451 struct file *file, void __user *udata, int version)
453 struct privcmd_data *data = file->private_data;
455 struct privcmd_mmapbatch_v2 m;
456 struct mm_struct *mm = current->mm;
457 struct vm_area_struct *vma;
458 unsigned long nr_pages;
460 struct mmap_batch_state state;
464 if (copy_from_user(&m, udata, sizeof(struct privcmd_mmapbatch)))
466 /* Returns per-frame error in m.arr. */
468 if (!access_ok(m.arr, m.num * sizeof(*m.arr)))
472 if (copy_from_user(&m, udata, sizeof(struct privcmd_mmapbatch_v2)))
474 /* Returns per-frame error code in m.err. */
475 if (!access_ok(m.err, m.num * (sizeof(*m.err))))
482 /* If restriction is in place, check the domid matches */
483 if (data->domid != DOMID_INVALID && data->domid != m.dom)
486 nr_pages = DIV_ROUND_UP(m.num, XEN_PFN_PER_PAGE);
487 if ((m.num <= 0) || (nr_pages > (LONG_MAX >> PAGE_SHIFT)))
490 ret = gather_array(&pagelist, m.num, sizeof(xen_pfn_t), m.arr);
494 if (list_empty(&pagelist)) {
500 /* Zero error array now to only copy back actual errors. */
501 if (clear_user(m.err, sizeof(int) * m.num)) {
509 vma = find_vma(mm, m.addr);
511 vma->vm_ops != &privcmd_vm_ops) {
517 * Caller must either:
519 * Map the whole VMA range, which will also allocate all the
520 * pages required for the auto_translated_physmap case.
524 * Map unmapped holes left from a previous map attempt (e.g.,
525 * because those foreign frames were previously paged out).
527 if (vma->vm_private_data == NULL) {
528 if (m.addr != vma->vm_start ||
529 m.addr + (nr_pages << PAGE_SHIFT) != vma->vm_end) {
533 if (xen_feature(XENFEAT_auto_translated_physmap)) {
534 ret = alloc_empty_pages(vma, nr_pages);
538 vma->vm_private_data = PRIV_VMA_LOCKED;
540 if (m.addr < vma->vm_start ||
541 m.addr + (nr_pages << PAGE_SHIFT) > vma->vm_end) {
545 if (privcmd_vma_range_is_mapped(vma, m.addr, nr_pages)) {
551 state.domain = m.dom;
555 state.global_error = 0;
556 state.version = version;
558 BUILD_BUG_ON(((PAGE_SIZE / sizeof(xen_pfn_t)) % XEN_PFN_PER_PAGE) != 0);
559 /* mmap_batch_fn guarantees ret == 0 */
560 BUG_ON(traverse_pages_block(m.num, sizeof(xen_pfn_t),
561 &pagelist, mmap_batch_fn, &state));
563 mmap_write_unlock(mm);
565 if (state.global_error) {
566 /* Write back errors in second pass. */
567 state.user_gfn = (xen_pfn_t *)m.arr;
568 state.user_err = m.err;
569 ret = traverse_pages_block(m.num, sizeof(xen_pfn_t),
570 &pagelist, mmap_return_errors, &state);
574 /* If we have not had any EFAULT-like global errors then set the global
575 * error to -ENOENT if necessary. */
576 if ((ret == 0) && (state.global_error == -ENOENT))
580 free_page_list(&pagelist);
584 mmap_write_unlock(mm);
588 static int lock_pages(
589 struct privcmd_dm_op_buf kbufs[], unsigned int num,
590 struct page *pages[], unsigned int nr_pages, unsigned int *pinned)
592 unsigned int i, off = 0;
594 for (i = 0; i < num; ) {
595 unsigned int requested;
598 requested = DIV_ROUND_UP(
599 offset_in_page(kbufs[i].uptr) + kbufs[i].size,
601 if (requested > nr_pages)
604 page_count = pin_user_pages_fast(
605 (unsigned long)kbufs[i].uptr + off * PAGE_SIZE,
606 requested, FOLL_WRITE, pages);
608 return page_count ? : -EFAULT;
610 *pinned += page_count;
611 nr_pages -= page_count;
614 off = (requested == page_count) ? 0 : off + page_count;
621 static void unlock_pages(struct page *pages[], unsigned int nr_pages)
623 unpin_user_pages_dirty_lock(pages, nr_pages, true);
626 static long privcmd_ioctl_dm_op(struct file *file, void __user *udata)
628 struct privcmd_data *data = file->private_data;
629 struct privcmd_dm_op kdata;
630 struct privcmd_dm_op_buf *kbufs;
631 unsigned int nr_pages = 0;
632 struct page **pages = NULL;
633 struct xen_dm_op_buf *xbufs = NULL;
636 unsigned int pinned = 0;
638 if (copy_from_user(&kdata, udata, sizeof(kdata)))
641 /* If restriction is in place, check the domid matches */
642 if (data->domid != DOMID_INVALID && data->domid != kdata.dom)
648 if (kdata.num > privcmd_dm_op_max_num)
651 kbufs = kcalloc(kdata.num, sizeof(*kbufs), GFP_KERNEL);
655 if (copy_from_user(kbufs, kdata.ubufs,
656 sizeof(*kbufs) * kdata.num)) {
661 for (i = 0; i < kdata.num; i++) {
662 if (kbufs[i].size > privcmd_dm_op_buf_max_size) {
667 if (!access_ok(kbufs[i].uptr,
673 nr_pages += DIV_ROUND_UP(
674 offset_in_page(kbufs[i].uptr) + kbufs[i].size,
678 pages = kcalloc(nr_pages, sizeof(*pages), GFP_KERNEL);
684 xbufs = kcalloc(kdata.num, sizeof(*xbufs), GFP_KERNEL);
690 rc = lock_pages(kbufs, kdata.num, pages, nr_pages, &pinned);
694 for (i = 0; i < kdata.num; i++) {
695 set_xen_guest_handle(xbufs[i].h, kbufs[i].uptr);
696 xbufs[i].size = kbufs[i].size;
699 xen_preemptible_hcall_begin();
700 rc = HYPERVISOR_dm_op(kdata.dom, kdata.num, xbufs);
701 xen_preemptible_hcall_end();
704 unlock_pages(pages, pinned);
712 static long privcmd_ioctl_restrict(struct file *file, void __user *udata)
714 struct privcmd_data *data = file->private_data;
717 if (copy_from_user(&dom, udata, sizeof(dom)))
720 /* Set restriction to the specified domain, or check it matches */
721 if (data->domid == DOMID_INVALID)
723 else if (data->domid != dom)
729 static long privcmd_ioctl_mmap_resource(struct file *file,
730 struct privcmd_mmap_resource __user *udata)
732 struct privcmd_data *data = file->private_data;
733 struct mm_struct *mm = current->mm;
734 struct vm_area_struct *vma;
735 struct privcmd_mmap_resource kdata;
736 xen_pfn_t *pfns = NULL;
737 struct xen_mem_acquire_resource xdata = { };
740 if (copy_from_user(&kdata, udata, sizeof(kdata)))
743 /* If restriction is in place, check the domid matches */
744 if (data->domid != DOMID_INVALID && data->domid != kdata.dom)
747 /* Both fields must be set or unset */
748 if (!!kdata.addr != !!kdata.num)
751 xdata.domid = kdata.dom;
752 xdata.type = kdata.type;
755 if (!kdata.addr && !kdata.num) {
756 /* Query the size of the resource. */
757 rc = HYPERVISOR_memory_op(XENMEM_acquire_resource, &xdata);
760 return __put_user(xdata.nr_frames, &udata->num);
765 vma = find_vma(mm, kdata.addr);
766 if (!vma || vma->vm_ops != &privcmd_vm_ops) {
771 pfns = kcalloc(kdata.num, sizeof(*pfns), GFP_KERNEL | __GFP_NOWARN);
777 if (IS_ENABLED(CONFIG_XEN_AUTO_XLATE) &&
778 xen_feature(XENFEAT_auto_translated_physmap)) {
779 unsigned int nr = DIV_ROUND_UP(kdata.num, XEN_PFN_PER_PAGE);
783 rc = alloc_empty_pages(vma, nr);
787 pages = vma->vm_private_data;
789 for (i = 0; i < kdata.num; i++) {
791 page_to_xen_pfn(pages[i / XEN_PFN_PER_PAGE]);
793 pfns[i] = pfn + (i % XEN_PFN_PER_PAGE);
796 vma->vm_private_data = PRIV_VMA_LOCKED;
798 xdata.frame = kdata.idx;
799 xdata.nr_frames = kdata.num;
800 set_xen_guest_handle(xdata.frame_list, pfns);
802 xen_preemptible_hcall_begin();
803 rc = HYPERVISOR_memory_op(XENMEM_acquire_resource, &xdata);
804 xen_preemptible_hcall_end();
809 if (IS_ENABLED(CONFIG_XEN_AUTO_XLATE) &&
810 xen_feature(XENFEAT_auto_translated_physmap)) {
811 rc = xen_remap_vma_range(vma, kdata.addr, kdata.num << PAGE_SHIFT);
814 (xdata.flags & XENMEM_rsrc_acq_caller_owned) ?
815 DOMID_SELF : kdata.dom;
816 int num, *errs = (int *)pfns;
818 BUILD_BUG_ON(sizeof(*errs) > sizeof(*pfns));
819 num = xen_remap_domain_mfn_array(vma,
820 kdata.addr & PAGE_MASK,
821 pfns, kdata.num, errs,
826 else if (num != kdata.num) {
829 for (i = 0; i < num; i++) {
839 mmap_write_unlock(mm);
845 #ifdef CONFIG_XEN_PRIVCMD_EVENTFD
847 static struct workqueue_struct *irqfd_cleanup_wq;
848 static DEFINE_MUTEX(irqfds_lock);
849 static LIST_HEAD(irqfds_list);
851 struct privcmd_kernel_irqfd {
852 struct xen_dm_op_buf xbufs;
855 struct eventfd_ctx *eventfd;
856 struct work_struct shutdown;
857 wait_queue_entry_t wait;
858 struct list_head list;
862 static void irqfd_deactivate(struct privcmd_kernel_irqfd *kirqfd)
864 lockdep_assert_held(&irqfds_lock);
866 list_del_init(&kirqfd->list);
867 queue_work(irqfd_cleanup_wq, &kirqfd->shutdown);
870 static void irqfd_shutdown(struct work_struct *work)
872 struct privcmd_kernel_irqfd *kirqfd =
873 container_of(work, struct privcmd_kernel_irqfd, shutdown);
876 eventfd_ctx_remove_wait_queue(kirqfd->eventfd, &kirqfd->wait, &cnt);
877 eventfd_ctx_put(kirqfd->eventfd);
881 static void irqfd_inject(struct privcmd_kernel_irqfd *kirqfd)
886 eventfd_ctx_do_read(kirqfd->eventfd, &cnt);
888 xen_preemptible_hcall_begin();
889 rc = HYPERVISOR_dm_op(kirqfd->dom, 1, &kirqfd->xbufs);
890 xen_preemptible_hcall_end();
892 /* Don't repeat the error message for consecutive failures */
893 if (rc && !kirqfd->error) {
894 pr_err("Failed to configure irq for guest domain: %d\n",
902 irqfd_wakeup(wait_queue_entry_t *wait, unsigned int mode, int sync, void *key)
904 struct privcmd_kernel_irqfd *kirqfd =
905 container_of(wait, struct privcmd_kernel_irqfd, wait);
906 __poll_t flags = key_to_poll(key);
909 irqfd_inject(kirqfd);
911 if (flags & EPOLLHUP) {
912 mutex_lock(&irqfds_lock);
913 irqfd_deactivate(kirqfd);
914 mutex_unlock(&irqfds_lock);
921 irqfd_poll_func(struct file *file, wait_queue_head_t *wqh, poll_table *pt)
923 struct privcmd_kernel_irqfd *kirqfd =
924 container_of(pt, struct privcmd_kernel_irqfd, pt);
926 add_wait_queue_priority(wqh, &kirqfd->wait);
929 static int privcmd_irqfd_assign(struct privcmd_irqfd *irqfd)
931 struct privcmd_kernel_irqfd *kirqfd, *tmp;
937 kirqfd = kzalloc(sizeof(*kirqfd) + irqfd->size, GFP_KERNEL);
942 if (copy_from_user(dm_op, u64_to_user_ptr(irqfd->dm_op), irqfd->size)) {
947 kirqfd->xbufs.size = irqfd->size;
948 set_xen_guest_handle(kirqfd->xbufs.h, dm_op);
949 kirqfd->dom = irqfd->dom;
950 INIT_WORK(&kirqfd->shutdown, irqfd_shutdown);
952 f = fdget(irqfd->fd);
958 kirqfd->eventfd = eventfd_ctx_fileget(f.file);
959 if (IS_ERR(kirqfd->eventfd)) {
960 ret = PTR_ERR(kirqfd->eventfd);
965 * Install our own custom wake-up handling so we are notified via a
966 * callback whenever someone signals the underlying eventfd.
968 init_waitqueue_func_entry(&kirqfd->wait, irqfd_wakeup);
969 init_poll_funcptr(&kirqfd->pt, irqfd_poll_func);
971 mutex_lock(&irqfds_lock);
973 list_for_each_entry(tmp, &irqfds_list, list) {
974 if (kirqfd->eventfd == tmp->eventfd) {
976 mutex_unlock(&irqfds_lock);
981 list_add_tail(&kirqfd->list, &irqfds_list);
982 mutex_unlock(&irqfds_lock);
985 * Check if there was an event already pending on the eventfd before we
986 * registered, and trigger it as if we didn't miss it.
988 events = vfs_poll(f.file, &kirqfd->pt);
989 if (events & EPOLLIN)
990 irqfd_inject(kirqfd);
993 * Do not drop the file until the kirqfd is fully initialized, otherwise
994 * we might race against the EPOLLHUP.
1000 eventfd_ctx_put(kirqfd->eventfd);
1010 static int privcmd_irqfd_deassign(struct privcmd_irqfd *irqfd)
1012 struct privcmd_kernel_irqfd *kirqfd;
1013 struct eventfd_ctx *eventfd;
1015 eventfd = eventfd_ctx_fdget(irqfd->fd);
1016 if (IS_ERR(eventfd))
1017 return PTR_ERR(eventfd);
1019 mutex_lock(&irqfds_lock);
1021 list_for_each_entry(kirqfd, &irqfds_list, list) {
1022 if (kirqfd->eventfd == eventfd) {
1023 irqfd_deactivate(kirqfd);
1028 mutex_unlock(&irqfds_lock);
1030 eventfd_ctx_put(eventfd);
1033 * Block until we know all outstanding shutdown jobs have completed so
1034 * that we guarantee there will not be any more interrupts once this
1035 * deassign function returns.
1037 flush_workqueue(irqfd_cleanup_wq);
1042 static long privcmd_ioctl_irqfd(struct file *file, void __user *udata)
1044 struct privcmd_data *data = file->private_data;
1045 struct privcmd_irqfd irqfd;
1047 if (copy_from_user(&irqfd, udata, sizeof(irqfd)))
1050 /* No other flags should be set */
1051 if (irqfd.flags & ~PRIVCMD_IRQFD_FLAG_DEASSIGN)
1054 /* If restriction is in place, check the domid matches */
1055 if (data->domid != DOMID_INVALID && data->domid != irqfd.dom)
1058 if (irqfd.flags & PRIVCMD_IRQFD_FLAG_DEASSIGN)
1059 return privcmd_irqfd_deassign(&irqfd);
1061 return privcmd_irqfd_assign(&irqfd);
1064 static int privcmd_irqfd_init(void)
1066 irqfd_cleanup_wq = alloc_workqueue("privcmd-irqfd-cleanup", 0, 0);
1067 if (!irqfd_cleanup_wq)
1073 static void privcmd_irqfd_exit(void)
1075 struct privcmd_kernel_irqfd *kirqfd, *tmp;
1077 mutex_lock(&irqfds_lock);
1079 list_for_each_entry_safe(kirqfd, tmp, &irqfds_list, list)
1080 irqfd_deactivate(kirqfd);
1082 mutex_unlock(&irqfds_lock);
1084 destroy_workqueue(irqfd_cleanup_wq);
1087 /* Ioeventfd Support */
1088 #define QUEUE_NOTIFY_VQ_MASK 0xFFFF
1090 static DEFINE_MUTEX(ioreq_lock);
1091 static LIST_HEAD(ioreq_list);
1093 /* per-eventfd structure */
1094 struct privcmd_kernel_ioeventfd {
1095 struct eventfd_ctx *eventfd;
1096 struct list_head list;
1098 unsigned int addr_len;
1102 /* per-guest CPU / port structure */
1106 struct privcmd_kernel_ioreq *kioreq;
1109 /* per-guest structure */
1110 struct privcmd_kernel_ioreq {
1114 struct ioreq *ioreq;
1115 spinlock_t lock; /* Protects ioeventfds list */
1116 struct list_head ioeventfds;
1117 struct list_head list;
1118 struct ioreq_port ports[0];
1121 static irqreturn_t ioeventfd_interrupt(int irq, void *dev_id)
1123 struct ioreq_port *port = dev_id;
1124 struct privcmd_kernel_ioreq *kioreq = port->kioreq;
1125 struct ioreq *ioreq = &kioreq->ioreq[port->vcpu];
1126 struct privcmd_kernel_ioeventfd *kioeventfd;
1127 unsigned int state = STATE_IOREQ_READY;
1129 if (ioreq->state != STATE_IOREQ_READY ||
1130 ioreq->type != IOREQ_TYPE_COPY || ioreq->dir != IOREQ_WRITE)
1134 * We need a barrier, smp_mb(), here to ensure reads are finished before
1135 * `state` is updated. Since the lock implementation ensures that
1136 * appropriate barrier will be added anyway, we can avoid adding
1137 * explicit barrier here.
1139 * Ideally we don't need to update `state` within the locks, but we do
1140 * that here to avoid adding explicit barrier.
1143 spin_lock(&kioreq->lock);
1144 ioreq->state = STATE_IOREQ_INPROCESS;
1146 list_for_each_entry(kioeventfd, &kioreq->ioeventfds, list) {
1147 if (ioreq->addr == kioeventfd->addr + VIRTIO_MMIO_QUEUE_NOTIFY &&
1148 ioreq->size == kioeventfd->addr_len &&
1149 (ioreq->data & QUEUE_NOTIFY_VQ_MASK) == kioeventfd->vq) {
1150 eventfd_signal(kioeventfd->eventfd, 1);
1151 state = STATE_IORESP_READY;
1155 spin_unlock(&kioreq->lock);
1158 * We need a barrier, smp_mb(), here to ensure writes are finished
1159 * before `state` is updated. Since the lock implementation ensures that
1160 * appropriate barrier will be added anyway, we can avoid adding
1161 * explicit barrier here.
1164 ioreq->state = state;
1166 if (state == STATE_IORESP_READY) {
1167 notify_remote_via_evtchn(port->port);
1174 static void ioreq_free(struct privcmd_kernel_ioreq *kioreq)
1176 struct ioreq_port *ports = kioreq->ports;
1179 lockdep_assert_held(&ioreq_lock);
1181 list_del(&kioreq->list);
1183 for (i = kioreq->vcpus - 1; i >= 0; i--)
1184 unbind_from_irqhandler(irq_from_evtchn(ports[i].port), &ports[i]);
1190 struct privcmd_kernel_ioreq *alloc_ioreq(struct privcmd_ioeventfd *ioeventfd)
1192 struct privcmd_kernel_ioreq *kioreq;
1193 struct mm_struct *mm = current->mm;
1194 struct vm_area_struct *vma;
1195 struct page **pages;
1196 unsigned int *ports;
1199 lockdep_assert_held(&ioreq_lock);
1201 size = struct_size(kioreq, ports, ioeventfd->vcpus);
1202 kioreq = kzalloc(size, GFP_KERNEL);
1204 return ERR_PTR(-ENOMEM);
1206 kioreq->dom = ioeventfd->dom;
1207 kioreq->vcpus = ioeventfd->vcpus;
1208 kioreq->uioreq = ioeventfd->ioreq;
1209 spin_lock_init(&kioreq->lock);
1210 INIT_LIST_HEAD(&kioreq->ioeventfds);
1212 /* The memory for ioreq server must have been mapped earlier */
1213 mmap_write_lock(mm);
1214 vma = find_vma(mm, (unsigned long)ioeventfd->ioreq);
1216 pr_err("Failed to find vma for ioreq page!\n");
1217 mmap_write_unlock(mm);
1222 pages = vma->vm_private_data;
1223 kioreq->ioreq = (struct ioreq *)(page_to_virt(pages[0]));
1224 mmap_write_unlock(mm);
1226 size = sizeof(*ports) * kioreq->vcpus;
1227 ports = kzalloc(size, GFP_KERNEL);
1233 if (copy_from_user(ports, u64_to_user_ptr(ioeventfd->ports), size)) {
1235 goto error_kfree_ports;
1238 for (i = 0; i < kioreq->vcpus; i++) {
1239 kioreq->ports[i].vcpu = i;
1240 kioreq->ports[i].port = ports[i];
1241 kioreq->ports[i].kioreq = kioreq;
1243 ret = bind_evtchn_to_irqhandler_lateeoi(ports[i],
1244 ioeventfd_interrupt, IRQF_SHARED, "ioeventfd",
1252 list_add_tail(&kioreq->list, &ioreq_list);
1258 unbind_from_irqhandler(irq_from_evtchn(ports[i]), &kioreq->ports[i]);
1263 return ERR_PTR(ret);
1266 static struct privcmd_kernel_ioreq *
1267 get_ioreq(struct privcmd_ioeventfd *ioeventfd, struct eventfd_ctx *eventfd)
1269 struct privcmd_kernel_ioreq *kioreq;
1270 unsigned long flags;
1272 list_for_each_entry(kioreq, &ioreq_list, list) {
1273 struct privcmd_kernel_ioeventfd *kioeventfd;
1276 * kioreq fields can be accessed here without a lock as they are
1277 * never updated after being added to the ioreq_list.
1279 if (kioreq->uioreq != ioeventfd->ioreq) {
1281 } else if (kioreq->dom != ioeventfd->dom ||
1282 kioreq->vcpus != ioeventfd->vcpus) {
1283 pr_err("Invalid ioeventfd configuration mismatch, dom (%u vs %u), vcpus (%u vs %u)\n",
1284 kioreq->dom, ioeventfd->dom, kioreq->vcpus,
1286 return ERR_PTR(-EINVAL);
1289 /* Look for a duplicate eventfd for the same guest */
1290 spin_lock_irqsave(&kioreq->lock, flags);
1291 list_for_each_entry(kioeventfd, &kioreq->ioeventfds, list) {
1292 if (eventfd == kioeventfd->eventfd) {
1293 spin_unlock_irqrestore(&kioreq->lock, flags);
1294 return ERR_PTR(-EBUSY);
1297 spin_unlock_irqrestore(&kioreq->lock, flags);
1302 /* Matching kioreq isn't found, allocate a new one */
1303 return alloc_ioreq(ioeventfd);
1306 static void ioeventfd_free(struct privcmd_kernel_ioeventfd *kioeventfd)
1308 list_del(&kioeventfd->list);
1309 eventfd_ctx_put(kioeventfd->eventfd);
1313 static int privcmd_ioeventfd_assign(struct privcmd_ioeventfd *ioeventfd)
1315 struct privcmd_kernel_ioeventfd *kioeventfd;
1316 struct privcmd_kernel_ioreq *kioreq;
1317 unsigned long flags;
1321 /* Check for range overflow */
1322 if (ioeventfd->addr + ioeventfd->addr_len < ioeventfd->addr)
1325 /* Vhost requires us to support length 1, 2, 4, and 8 */
1326 if (!(ioeventfd->addr_len == 1 || ioeventfd->addr_len == 2 ||
1327 ioeventfd->addr_len == 4 || ioeventfd->addr_len == 8))
1330 /* 4096 vcpus limit enough ? */
1331 if (!ioeventfd->vcpus || ioeventfd->vcpus > 4096)
1334 kioeventfd = kzalloc(sizeof(*kioeventfd), GFP_KERNEL);
1338 f = fdget(ioeventfd->event_fd);
1344 kioeventfd->eventfd = eventfd_ctx_fileget(f.file);
1347 if (IS_ERR(kioeventfd->eventfd)) {
1348 ret = PTR_ERR(kioeventfd->eventfd);
1352 kioeventfd->addr = ioeventfd->addr;
1353 kioeventfd->addr_len = ioeventfd->addr_len;
1354 kioeventfd->vq = ioeventfd->vq;
1356 mutex_lock(&ioreq_lock);
1357 kioreq = get_ioreq(ioeventfd, kioeventfd->eventfd);
1358 if (IS_ERR(kioreq)) {
1359 mutex_unlock(&ioreq_lock);
1360 ret = PTR_ERR(kioreq);
1364 spin_lock_irqsave(&kioreq->lock, flags);
1365 list_add_tail(&kioeventfd->list, &kioreq->ioeventfds);
1366 spin_unlock_irqrestore(&kioreq->lock, flags);
1368 mutex_unlock(&ioreq_lock);
1373 eventfd_ctx_put(kioeventfd->eventfd);
1380 static int privcmd_ioeventfd_deassign(struct privcmd_ioeventfd *ioeventfd)
1382 struct privcmd_kernel_ioreq *kioreq, *tkioreq;
1383 struct eventfd_ctx *eventfd;
1384 unsigned long flags;
1387 eventfd = eventfd_ctx_fdget(ioeventfd->event_fd);
1388 if (IS_ERR(eventfd))
1389 return PTR_ERR(eventfd);
1391 mutex_lock(&ioreq_lock);
1392 list_for_each_entry_safe(kioreq, tkioreq, &ioreq_list, list) {
1393 struct privcmd_kernel_ioeventfd *kioeventfd, *tmp;
1395 * kioreq fields can be accessed here without a lock as they are
1396 * never updated after being added to the ioreq_list.
1398 if (kioreq->dom != ioeventfd->dom ||
1399 kioreq->uioreq != ioeventfd->ioreq ||
1400 kioreq->vcpus != ioeventfd->vcpus)
1403 spin_lock_irqsave(&kioreq->lock, flags);
1404 list_for_each_entry_safe(kioeventfd, tmp, &kioreq->ioeventfds, list) {
1405 if (eventfd == kioeventfd->eventfd) {
1406 ioeventfd_free(kioeventfd);
1407 spin_unlock_irqrestore(&kioreq->lock, flags);
1409 if (list_empty(&kioreq->ioeventfds))
1414 spin_unlock_irqrestore(&kioreq->lock, flags);
1418 pr_err("Ioeventfd isn't already assigned, dom: %u, addr: %llu\n",
1419 ioeventfd->dom, ioeventfd->addr);
1423 mutex_unlock(&ioreq_lock);
1424 eventfd_ctx_put(eventfd);
1429 static long privcmd_ioctl_ioeventfd(struct file *file, void __user *udata)
1431 struct privcmd_data *data = file->private_data;
1432 struct privcmd_ioeventfd ioeventfd;
1434 if (copy_from_user(&ioeventfd, udata, sizeof(ioeventfd)))
1437 /* No other flags should be set */
1438 if (ioeventfd.flags & ~PRIVCMD_IOEVENTFD_FLAG_DEASSIGN)
1441 /* If restriction is in place, check the domid matches */
1442 if (data->domid != DOMID_INVALID && data->domid != ioeventfd.dom)
1445 if (ioeventfd.flags & PRIVCMD_IOEVENTFD_FLAG_DEASSIGN)
1446 return privcmd_ioeventfd_deassign(&ioeventfd);
1448 return privcmd_ioeventfd_assign(&ioeventfd);
1451 static void privcmd_ioeventfd_exit(void)
1453 struct privcmd_kernel_ioreq *kioreq, *tmp;
1454 unsigned long flags;
1456 mutex_lock(&ioreq_lock);
1457 list_for_each_entry_safe(kioreq, tmp, &ioreq_list, list) {
1458 struct privcmd_kernel_ioeventfd *kioeventfd, *tmp;
1460 spin_lock_irqsave(&kioreq->lock, flags);
1461 list_for_each_entry_safe(kioeventfd, tmp, &kioreq->ioeventfds, list)
1462 ioeventfd_free(kioeventfd);
1463 spin_unlock_irqrestore(&kioreq->lock, flags);
1467 mutex_unlock(&ioreq_lock);
1470 static inline long privcmd_ioctl_irqfd(struct file *file, void __user *udata)
1475 static inline int privcmd_irqfd_init(void)
1480 static inline void privcmd_irqfd_exit(void)
1484 static inline long privcmd_ioctl_ioeventfd(struct file *file, void __user *udata)
1489 static inline void privcmd_ioeventfd_exit(void)
1492 #endif /* CONFIG_XEN_PRIVCMD_EVENTFD */
1494 static long privcmd_ioctl(struct file *file,
1495 unsigned int cmd, unsigned long data)
1498 void __user *udata = (void __user *) data;
1501 case IOCTL_PRIVCMD_HYPERCALL:
1502 ret = privcmd_ioctl_hypercall(file, udata);
1505 case IOCTL_PRIVCMD_MMAP:
1506 ret = privcmd_ioctl_mmap(file, udata);
1509 case IOCTL_PRIVCMD_MMAPBATCH:
1510 ret = privcmd_ioctl_mmap_batch(file, udata, 1);
1513 case IOCTL_PRIVCMD_MMAPBATCH_V2:
1514 ret = privcmd_ioctl_mmap_batch(file, udata, 2);
1517 case IOCTL_PRIVCMD_DM_OP:
1518 ret = privcmd_ioctl_dm_op(file, udata);
1521 case IOCTL_PRIVCMD_RESTRICT:
1522 ret = privcmd_ioctl_restrict(file, udata);
1525 case IOCTL_PRIVCMD_MMAP_RESOURCE:
1526 ret = privcmd_ioctl_mmap_resource(file, udata);
1529 case IOCTL_PRIVCMD_IRQFD:
1530 ret = privcmd_ioctl_irqfd(file, udata);
1533 case IOCTL_PRIVCMD_IOEVENTFD:
1534 ret = privcmd_ioctl_ioeventfd(file, udata);
1544 static int privcmd_open(struct inode *ino, struct file *file)
1546 struct privcmd_data *data = kzalloc(sizeof(*data), GFP_KERNEL);
1551 /* DOMID_INVALID implies no restriction */
1552 data->domid = DOMID_INVALID;
1554 file->private_data = data;
1558 static int privcmd_release(struct inode *ino, struct file *file)
1560 struct privcmd_data *data = file->private_data;
1566 static void privcmd_close(struct vm_area_struct *vma)
1568 struct page **pages = vma->vm_private_data;
1569 int numpgs = vma_pages(vma);
1570 int numgfns = (vma->vm_end - vma->vm_start) >> XEN_PAGE_SHIFT;
1573 if (!xen_feature(XENFEAT_auto_translated_physmap) || !numpgs || !pages)
1576 rc = xen_unmap_domain_gfn_range(vma, numgfns, pages);
1578 xen_free_unpopulated_pages(numpgs, pages);
1580 pr_crit("unable to unmap MFN range: leaking %d pages. rc=%d\n",
1585 static vm_fault_t privcmd_fault(struct vm_fault *vmf)
1587 printk(KERN_DEBUG "privcmd_fault: vma=%p %lx-%lx, pgoff=%lx, uv=%p\n",
1588 vmf->vma, vmf->vma->vm_start, vmf->vma->vm_end,
1589 vmf->pgoff, (void *)vmf->address);
1591 return VM_FAULT_SIGBUS;
1594 static const struct vm_operations_struct privcmd_vm_ops = {
1595 .close = privcmd_close,
1596 .fault = privcmd_fault
1599 static int privcmd_mmap(struct file *file, struct vm_area_struct *vma)
1601 /* DONTCOPY is essential for Xen because copy_page_range doesn't know
1602 * how to recreate these mappings */
1603 vm_flags_set(vma, VM_IO | VM_PFNMAP | VM_DONTCOPY |
1604 VM_DONTEXPAND | VM_DONTDUMP);
1605 vma->vm_ops = &privcmd_vm_ops;
1606 vma->vm_private_data = NULL;
1612 * For MMAPBATCH*. This allows asserting the singleshot mapping
1613 * on a per pfn/pte basis. Mapping calls that fail with ENOENT
1614 * can be then retried until success.
1616 static int is_mapped_fn(pte_t *pte, unsigned long addr, void *data)
1618 return pte_none(ptep_get(pte)) ? 0 : -EBUSY;
1621 static int privcmd_vma_range_is_mapped(
1622 struct vm_area_struct *vma,
1624 unsigned long nr_pages)
1626 return apply_to_page_range(vma->vm_mm, addr, nr_pages << PAGE_SHIFT,
1627 is_mapped_fn, NULL) != 0;
1630 const struct file_operations xen_privcmd_fops = {
1631 .owner = THIS_MODULE,
1632 .unlocked_ioctl = privcmd_ioctl,
1633 .open = privcmd_open,
1634 .release = privcmd_release,
1635 .mmap = privcmd_mmap,
1637 EXPORT_SYMBOL_GPL(xen_privcmd_fops);
1639 static struct miscdevice privcmd_dev = {
1640 .minor = MISC_DYNAMIC_MINOR,
1641 .name = "xen/privcmd",
1642 .fops = &xen_privcmd_fops,
1645 static int __init privcmd_init(void)
1652 err = misc_register(&privcmd_dev);
1654 pr_err("Could not register Xen privcmd device\n");
1658 err = misc_register(&xen_privcmdbuf_dev);
1660 pr_err("Could not register Xen hypercall-buf device\n");
1661 goto err_privcmdbuf;
1664 err = privcmd_irqfd_init();
1666 pr_err("irqfd init failed\n");
1673 misc_deregister(&xen_privcmdbuf_dev);
1675 misc_deregister(&privcmd_dev);
1679 static void __exit privcmd_exit(void)
1681 privcmd_ioeventfd_exit();
1682 privcmd_irqfd_exit();
1683 misc_deregister(&privcmd_dev);
1684 misc_deregister(&xen_privcmdbuf_dev);
1687 module_init(privcmd_init);
1688 module_exit(privcmd_exit);