1 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
3 #include <linux/slab.h>
4 #include <linux/types.h>
7 #include <linux/miscdevice.h>
8 #include <linux/module.h>
9 #include <linux/capability.h>
13 #include <xen/xenbus.h>
14 #include <xen/xenbus_dev.h>
15 #include <xen/grant_table.h>
16 #include <xen/events.h>
17 #include <asm/xen/hypervisor.h>
19 #include "xenbus_comms.h"
21 MODULE_LICENSE("GPL");
23 static int xenbus_backend_open(struct inode *inode, struct file *filp)
25 if (!capable(CAP_SYS_ADMIN))
28 return nonseekable_open(inode, filp);
31 static long xenbus_alloc(domid_t domid)
33 struct evtchn_alloc_unbound arg;
38 /* If xenstored_ready is nonzero, that means we have already talked to
39 * xenstore and set up watches. These watches will be restored by
40 * xs_resume, but that requires communication over the port established
41 * below that is not visible to anyone until the ioctl returns.
43 * This can be resolved by splitting the ioctl into two parts
44 * (postponing the resume until xenstored is active) but this is
45 * unnecessarily complex for the intended use where xenstored is only
46 * started once - so return -EEXIST if it's already running.
51 gnttab_grant_foreign_access_ref(GNTTAB_RESERVED_XENSTORE, domid,
52 virt_to_mfn(xen_store_interface), 0 /* writable */);
55 arg.remote_dom = domid;
57 err = HYPERVISOR_event_channel_op(EVTCHNOP_alloc_unbound, &arg);
61 if (xen_store_evtchn > 0)
64 xen_store_evtchn = arg.port;
75 static long xenbus_backend_ioctl(struct file *file, unsigned int cmd,
78 if (!capable(CAP_SYS_ADMIN))
82 case IOCTL_XENBUS_BACKEND_EVTCHN:
83 if (xen_store_evtchn > 0)
84 return xen_store_evtchn;
86 case IOCTL_XENBUS_BACKEND_SETUP:
87 return xenbus_alloc(data);
93 static int xenbus_backend_mmap(struct file *file, struct vm_area_struct *vma)
95 size_t size = vma->vm_end - vma->vm_start;
97 if (!capable(CAP_SYS_ADMIN))
100 if ((size > PAGE_SIZE) || (vma->vm_pgoff != 0))
103 if (remap_pfn_range(vma, vma->vm_start,
104 virt_to_pfn(xen_store_interface),
105 size, vma->vm_page_prot))
111 static const struct file_operations xenbus_backend_fops = {
112 .open = xenbus_backend_open,
113 .mmap = xenbus_backend_mmap,
114 .unlocked_ioctl = xenbus_backend_ioctl,
117 static struct miscdevice xenbus_backend_dev = {
118 .minor = MISC_DYNAMIC_MINOR,
119 .name = "xen/xenbus_backend",
120 .fops = &xenbus_backend_fops,
123 static int __init xenbus_backend_init(void)
127 if (!xen_initial_domain())
130 err = misc_register(&xenbus_backend_dev);
132 pr_err("Could not register xenbus backend device\n");
136 static void __exit xenbus_backend_exit(void)
138 misc_deregister(&xenbus_backend_dev);
141 module_init(xenbus_backend_init);
142 module_exit(xenbus_backend_exit);