xenbus_dev_backend.c 3.1 KB

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