xenbus_dev_backend.c 1.8 KB

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