0001
0002 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
0003
0004 #include <linux/slab.h>
0005 #include <linux/types.h>
0006 #include <linux/mm.h>
0007 #include <linux/fs.h>
0008 #include <linux/miscdevice.h>
0009 #include <linux/init.h>
0010 #include <linux/capability.h>
0011
0012 #include <xen/xen.h>
0013 #include <xen/page.h>
0014 #include <xen/xenbus.h>
0015 #include <xen/xenbus_dev.h>
0016 #include <xen/grant_table.h>
0017 #include <xen/events.h>
0018 #include <asm/xen/hypervisor.h>
0019
0020 #include "xenbus.h"
0021
0022 static int xenbus_backend_open(struct inode *inode, struct file *filp)
0023 {
0024 if (!capable(CAP_SYS_ADMIN))
0025 return -EPERM;
0026
0027 return nonseekable_open(inode, filp);
0028 }
0029
0030 static long xenbus_alloc(domid_t domid)
0031 {
0032 struct evtchn_alloc_unbound arg;
0033 int err = -EEXIST;
0034
0035 xs_suspend();
0036
0037
0038
0039
0040
0041
0042
0043
0044
0045
0046
0047 if (xenstored_ready)
0048 goto out_err;
0049
0050 gnttab_grant_foreign_access_ref(GNTTAB_RESERVED_XENSTORE, domid,
0051 virt_to_gfn(xen_store_interface), 0 );
0052
0053 arg.dom = DOMID_SELF;
0054 arg.remote_dom = domid;
0055
0056 err = HYPERVISOR_event_channel_op(EVTCHNOP_alloc_unbound, &arg);
0057 if (err)
0058 goto out_err;
0059
0060 if (xen_store_evtchn > 0)
0061 xb_deinit_comms();
0062
0063 xen_store_evtchn = arg.port;
0064
0065 xs_resume();
0066
0067 return arg.port;
0068
0069 out_err:
0070 xs_suspend_cancel();
0071 return err;
0072 }
0073
0074 static long xenbus_backend_ioctl(struct file *file, unsigned int cmd,
0075 unsigned long data)
0076 {
0077 if (!capable(CAP_SYS_ADMIN))
0078 return -EPERM;
0079
0080 switch (cmd) {
0081 case IOCTL_XENBUS_BACKEND_EVTCHN:
0082 if (xen_store_evtchn > 0)
0083 return xen_store_evtchn;
0084 return -ENODEV;
0085 case IOCTL_XENBUS_BACKEND_SETUP:
0086 return xenbus_alloc(data);
0087 default:
0088 return -ENOTTY;
0089 }
0090 }
0091
0092 static int xenbus_backend_mmap(struct file *file, struct vm_area_struct *vma)
0093 {
0094 size_t size = vma->vm_end - vma->vm_start;
0095
0096 if (!capable(CAP_SYS_ADMIN))
0097 return -EPERM;
0098
0099 if ((size > PAGE_SIZE) || (vma->vm_pgoff != 0))
0100 return -EINVAL;
0101
0102 if (remap_pfn_range(vma, vma->vm_start,
0103 virt_to_pfn(xen_store_interface),
0104 size, vma->vm_page_prot))
0105 return -EAGAIN;
0106
0107 return 0;
0108 }
0109
0110 static const struct file_operations xenbus_backend_fops = {
0111 .open = xenbus_backend_open,
0112 .mmap = xenbus_backend_mmap,
0113 .unlocked_ioctl = xenbus_backend_ioctl,
0114 };
0115
0116 static struct miscdevice xenbus_backend_dev = {
0117 .minor = MISC_DYNAMIC_MINOR,
0118 .name = "xen/xenbus_backend",
0119 .fops = &xenbus_backend_fops,
0120 };
0121
0122 static int __init xenbus_backend_init(void)
0123 {
0124 int err;
0125
0126 if (!xen_initial_domain())
0127 return -ENODEV;
0128
0129 err = misc_register(&xenbus_backend_dev);
0130 if (err)
0131 pr_err("Could not register xenbus backend device\n");
0132 return err;
0133 }
0134 device_initcall(xenbus_backend_init);