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