1 #include <linux/slab.h>
2 #include <linux/types.h>
3 #include <linux/mm.h>
4 #include <linux/fs.h>
5 #include <linux/miscdevice.h>
6 #include <linux/module.h>
7 #include <linux/capability.h>
8
9 #include <xen/xen.h>
10 #include <xen/page.h>
11 #include <xen/xenbus_dev.h>
12
13 #include "xenbus_comms.h"
14
15 MODULE_LICENSE("GPL");
16
xenbus_backend_open(struct inode * inode,struct file * filp)17 static int xenbus_backend_open(struct inode *inode, struct file *filp)
18 {
19 if (!capable(CAP_SYS_ADMIN))
20 return -EPERM;
21
22 return nonseekable_open(inode, filp);
23 }
24
xenbus_backend_ioctl(struct file * file,unsigned int cmd,unsigned long data)25 static long xenbus_backend_ioctl(struct file *file, unsigned int cmd, unsigned long data)
26 {
27 if (!capable(CAP_SYS_ADMIN))
28 return -EPERM;
29
30 switch (cmd) {
31 case IOCTL_XENBUS_BACKEND_EVTCHN:
32 if (xen_store_evtchn > 0)
33 return xen_store_evtchn;
34 return -ENODEV;
35
36 default:
37 return -ENOTTY;
38 }
39 }
40
xenbus_backend_mmap(struct file * file,struct vm_area_struct * vma)41 static int xenbus_backend_mmap(struct file *file, struct vm_area_struct *vma)
42 {
43 size_t size = vma->vm_end - vma->vm_start;
44
45 if (!capable(CAP_SYS_ADMIN))
46 return -EPERM;
47
48 if ((size > PAGE_SIZE) || (vma->vm_pgoff != 0))
49 return -EINVAL;
50
51 if (remap_pfn_range(vma, vma->vm_start,
52 virt_to_pfn(xen_store_interface),
53 size, vma->vm_page_prot))
54 return -EAGAIN;
55
56 return 0;
57 }
58
59 const struct file_operations xenbus_backend_fops = {
60 .open = xenbus_backend_open,
61 .mmap = xenbus_backend_mmap,
62 .unlocked_ioctl = xenbus_backend_ioctl,
63 };
64
65 static struct miscdevice xenbus_backend_dev = {
66 .minor = MISC_DYNAMIC_MINOR,
67 .name = "xen/xenbus_backend",
68 .fops = &xenbus_backend_fops,
69 };
70
xenbus_backend_init(void)71 static int __init xenbus_backend_init(void)
72 {
73 int err;
74
75 if (!xen_initial_domain())
76 return -ENODEV;
77
78 err = misc_register(&xenbus_backend_dev);
79 if (err)
80 printk(KERN_ERR "Could not register xenbus backend device\n");
81 return err;
82 }
83
xenbus_backend_exit(void)84 static void __exit xenbus_backend_exit(void)
85 {
86 misc_deregister(&xenbus_backend_dev);
87 }
88
89 module_init(xenbus_backend_init);
90 module_exit(xenbus_backend_exit);
91