Linux 4.16.11
[linux/fpc-iii.git] / drivers / xen / xenbus / xenbus_dev_backend.c
blobedba5fecde4ddc6c3f8d9640a50b19650769bbfe
1 // SPDX-License-Identifier: GPL-2.0
2 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
4 #include <linux/slab.h>
5 #include <linux/types.h>
6 #include <linux/mm.h>
7 #include <linux/fs.h>
8 #include <linux/miscdevice.h>
9 #include <linux/init.h>
10 #include <linux/capability.h>
12 #include <xen/xen.h>
13 #include <xen/page.h>
14 #include <xen/xenbus.h>
15 #include <xen/xenbus_dev.h>
16 #include <xen/grant_table.h>
17 #include <xen/events.h>
18 #include <asm/xen/hypervisor.h>
20 #include "xenbus.h"
22 static int xenbus_backend_open(struct inode *inode, struct file *filp)
24 if (!capable(CAP_SYS_ADMIN))
25 return -EPERM;
27 return nonseekable_open(inode, filp);
30 static long xenbus_alloc(domid_t domid)
32 struct evtchn_alloc_unbound arg;
33 int err = -EEXIST;
35 xs_suspend();
37 /* If xenstored_ready is nonzero, that means we have already talked to
38 * xenstore and set up watches. These watches will be restored by
39 * xs_resume, but that requires communication over the port established
40 * below that is not visible to anyone until the ioctl returns.
42 * This can be resolved by splitting the ioctl into two parts
43 * (postponing the resume until xenstored is active) but this is
44 * unnecessarily complex for the intended use where xenstored is only
45 * started once - so return -EEXIST if it's already running.
47 if (xenstored_ready)
48 goto out_err;
50 gnttab_grant_foreign_access_ref(GNTTAB_RESERVED_XENSTORE, domid,
51 virt_to_gfn(xen_store_interface), 0 /* writable */);
53 arg.dom = DOMID_SELF;
54 arg.remote_dom = domid;
56 err = HYPERVISOR_event_channel_op(EVTCHNOP_alloc_unbound, &arg);
57 if (err)
58 goto out_err;
60 if (xen_store_evtchn > 0)
61 xb_deinit_comms();
63 xen_store_evtchn = arg.port;
65 xs_resume();
67 return arg.port;
69 out_err:
70 xs_suspend_cancel();
71 return err;
74 static long xenbus_backend_ioctl(struct file *file, unsigned int cmd,
75 unsigned long data)
77 if (!capable(CAP_SYS_ADMIN))
78 return -EPERM;
80 switch (cmd) {
81 case IOCTL_XENBUS_BACKEND_EVTCHN:
82 if (xen_store_evtchn > 0)
83 return xen_store_evtchn;
84 return -ENODEV;
85 case IOCTL_XENBUS_BACKEND_SETUP:
86 return xenbus_alloc(data);
87 default:
88 return -ENOTTY;
92 static int xenbus_backend_mmap(struct file *file, struct vm_area_struct *vma)
94 size_t size = vma->vm_end - vma->vm_start;
96 if (!capable(CAP_SYS_ADMIN))
97 return -EPERM;
99 if ((size > PAGE_SIZE) || (vma->vm_pgoff != 0))
100 return -EINVAL;
102 if (remap_pfn_range(vma, vma->vm_start,
103 virt_to_pfn(xen_store_interface),
104 size, vma->vm_page_prot))
105 return -EAGAIN;
107 return 0;
110 static const struct file_operations xenbus_backend_fops = {
111 .open = xenbus_backend_open,
112 .mmap = xenbus_backend_mmap,
113 .unlocked_ioctl = xenbus_backend_ioctl,
116 static struct miscdevice xenbus_backend_dev = {
117 .minor = MISC_DYNAMIC_MINOR,
118 .name = "xen/xenbus_backend",
119 .fops = &xenbus_backend_fops,
122 static int __init xenbus_backend_init(void)
124 int err;
126 if (!xen_initial_domain())
127 return -ENODEV;
129 err = misc_register(&xenbus_backend_dev);
130 if (err)
131 pr_err("Could not register xenbus backend device\n");
132 return err;
134 device_initcall(xenbus_backend_init);