blob: 3d3be78c1093788f1591426157fdd4db9f3d5459 [file] [log] [blame]
Bastian Blanke9f0fec2011-12-10 19:29:48 +01001#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
Konrad Rzeszutek Wilk01464a62011-12-21 14:19:47 -05009#include <xen/xen.h>
Bastian Blanke9f0fec2011-12-10 19:29:48 +010010#include <xen/page.h>
11#include <xen/xenbus_dev.h>
12
13#include "xenbus_comms.h"
14
15MODULE_LICENSE("GPL");
16
17static 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
25static 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
41static 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
59const struct file_operations xenbus_backend_fops = {
60 .open = xenbus_backend_open,
61 .mmap = xenbus_backend_mmap,
62 .unlocked_ioctl = xenbus_backend_ioctl,
63};
64
65static struct miscdevice xenbus_backend_dev = {
66 .minor = MISC_DYNAMIC_MINOR,
67 .name = "xen/xenbus_backend",
68 .fops = &xenbus_backend_fops,
69};
70
71static 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
84static void __exit xenbus_backend_exit(void)
85{
86 misc_deregister(&xenbus_backend_dev);
87}
88
89module_init(xenbus_backend_init);
90module_exit(xenbus_backend_exit);