xenbus_dev_backend.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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/init.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. 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_gfn(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,
  57. unsigned long data)
  58. {
  59. if (!capable(CAP_SYS_ADMIN))
  60. return -EPERM;
  61. switch (cmd) {
  62. case IOCTL_XENBUS_BACKEND_EVTCHN:
  63. if (xen_store_evtchn > 0)
  64. return xen_store_evtchn;
  65. return -ENODEV;
  66. case IOCTL_XENBUS_BACKEND_SETUP:
  67. return xenbus_alloc(data);
  68. default:
  69. return -ENOTTY;
  70. }
  71. }
  72. static int xenbus_backend_mmap(struct file *file, struct vm_area_struct *vma)
  73. {
  74. size_t size = vma->vm_end - vma->vm_start;
  75. if (!capable(CAP_SYS_ADMIN))
  76. return -EPERM;
  77. if ((size > PAGE_SIZE) || (vma->vm_pgoff != 0))
  78. return -EINVAL;
  79. if (remap_pfn_range(vma, vma->vm_start,
  80. virt_to_pfn(xen_store_interface),
  81. size, vma->vm_page_prot))
  82. return -EAGAIN;
  83. return 0;
  84. }
  85. static const struct file_operations xenbus_backend_fops = {
  86. .open = xenbus_backend_open,
  87. .mmap = xenbus_backend_mmap,
  88. .unlocked_ioctl = xenbus_backend_ioctl,
  89. };
  90. static struct miscdevice xenbus_backend_dev = {
  91. .minor = MISC_DYNAMIC_MINOR,
  92. .name = "xen/xenbus_backend",
  93. .fops = &xenbus_backend_fops,
  94. };
  95. static int __init xenbus_backend_init(void)
  96. {
  97. int err;
  98. if (!xen_initial_domain())
  99. return -ENODEV;
  100. err = misc_register(&xenbus_backend_dev);
  101. if (err)
  102. pr_err("Could not register xenbus backend device\n");
  103. return err;
  104. }
  105. device_initcall(xenbus_backend_init);