xenbus_dev_backend.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  2. #include <linux/slab.h>
  3. #include <linux/types.h>
  4. #include <linux/mm.h>
  5. #include <linux/fs.h>
  6. #include <linux/miscdevice.h>
  7. #include <linux/module.h>
  8. #include <linux/capability.h>
  9. #include <xen/xen.h>
  10. #include <xen/page.h>
  11. #include <xen/xenbus.h>
  12. #include <xen/xenbus_dev.h>
  13. #include <xen/grant_table.h>
  14. #include <xen/events.h>
  15. #include <asm/xen/hypervisor.h>
  16. #include "xenbus_comms.h"
  17. MODULE_LICENSE("GPL");
  18. static int xenbus_backend_open(struct inode *inode, struct file *filp)
  19. {
  20. if (!capable(CAP_SYS_ADMIN))
  21. return -EPERM;
  22. return nonseekable_open(inode, filp);
  23. }
  24. static long xenbus_alloc(domid_t domid)
  25. {
  26. struct evtchn_alloc_unbound arg;
  27. int err = -EEXIST;
  28. xs_suspend();
  29. /* If xenstored_ready is nonzero, that means we have already talked to
  30. * xenstore and set up watches. These watches will be restored by
  31. * xs_resume, but that requires communication over the port established
  32. * below that is not visible to anyone until the ioctl returns.
  33. *
  34. * This can be resolved by splitting the ioctl into two parts
  35. * (postponing the resume until xenstored is active) but this is
  36. * unnecessarily complex for the intended use where xenstored is only
  37. * started once - so return -EEXIST if it's already running.
  38. */
  39. if (xenstored_ready)
  40. goto out_err;
  41. gnttab_grant_foreign_access_ref(GNTTAB_RESERVED_XENSTORE, domid,
  42. virt_to_gfn(xen_store_interface), 0 /* writable */);
  43. arg.dom = DOMID_SELF;
  44. arg.remote_dom = domid;
  45. err = HYPERVISOR_event_channel_op(EVTCHNOP_alloc_unbound, &arg);
  46. if (err)
  47. goto out_err;
  48. if (xen_store_evtchn > 0)
  49. xb_deinit_comms();
  50. xen_store_evtchn = arg.port;
  51. xs_resume();
  52. return arg.port;
  53. out_err:
  54. xs_suspend_cancel();
  55. return err;
  56. }
  57. static long xenbus_backend_ioctl(struct file *file, unsigned int cmd,
  58. unsigned long data)
  59. {
  60. if (!capable(CAP_SYS_ADMIN))
  61. return -EPERM;
  62. switch (cmd) {
  63. case IOCTL_XENBUS_BACKEND_EVTCHN:
  64. if (xen_store_evtchn > 0)
  65. return xen_store_evtchn;
  66. return -ENODEV;
  67. case IOCTL_XENBUS_BACKEND_SETUP:
  68. return xenbus_alloc(data);
  69. default:
  70. return -ENOTTY;
  71. }
  72. }
  73. static int xenbus_backend_mmap(struct file *file, struct vm_area_struct *vma)
  74. {
  75. size_t size = vma->vm_end - vma->vm_start;
  76. if (!capable(CAP_SYS_ADMIN))
  77. return -EPERM;
  78. if ((size > PAGE_SIZE) || (vma->vm_pgoff != 0))
  79. return -EINVAL;
  80. if (remap_pfn_range(vma, vma->vm_start,
  81. virt_to_pfn(xen_store_interface),
  82. size, vma->vm_page_prot))
  83. return -EAGAIN;
  84. return 0;
  85. }
  86. static const struct file_operations xenbus_backend_fops = {
  87. .open = xenbus_backend_open,
  88. .mmap = xenbus_backend_mmap,
  89. .unlocked_ioctl = xenbus_backend_ioctl,
  90. };
  91. static struct miscdevice xenbus_backend_dev = {
  92. .minor = MISC_DYNAMIC_MINOR,
  93. .name = "xen/xenbus_backend",
  94. .fops = &xenbus_backend_fops,
  95. };
  96. static int __init xenbus_backend_init(void)
  97. {
  98. int err;
  99. if (!xen_initial_domain())
  100. return -ENODEV;
  101. err = misc_register(&xenbus_backend_dev);
  102. if (err)
  103. pr_err("Could not register xenbus backend device\n");
  104. return err;
  105. }
  106. static void __exit xenbus_backend_exit(void)
  107. {
  108. misc_deregister(&xenbus_backend_dev);
  109. }
  110. module_init(xenbus_backend_init);
  111. module_exit(xenbus_backend_exit);