evtchn.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  1. /******************************************************************************
  2. * evtchn.c
  3. *
  4. * Driver for receiving and demuxing event-channel signals.
  5. *
  6. * Copyright (c) 2004-2005, K A Fraser
  7. * Multi-process extensions Copyright (c) 2004, Steven Smith
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License version 2
  11. * as published by the Free Software Foundation; or, when distributed
  12. * separately from the Linux kernel or incorporated into other
  13. * software packages, subject to the following license:
  14. *
  15. * Permission is hereby granted, free of charge, to any person obtaining a copy
  16. * of this source file (the "Software"), to deal in the Software without
  17. * restriction, including without limitation the rights to use, copy, modify,
  18. * merge, publish, distribute, sublicense, and/or sell copies of the Software,
  19. * and to permit persons to whom the Software is furnished to do so, subject to
  20. * the following conditions:
  21. *
  22. * The above copyright notice and this permission notice shall be included in
  23. * all copies or substantial portions of the Software.
  24. *
  25. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  26. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  27. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  28. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  29. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  30. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  31. * IN THE SOFTWARE.
  32. */
  33. #include <linux/module.h>
  34. #include <linux/kernel.h>
  35. #include <linux/sched.h>
  36. #include <linux/slab.h>
  37. #include <linux/string.h>
  38. #include <linux/errno.h>
  39. #include <linux/fs.h>
  40. #include <linux/errno.h>
  41. #include <linux/miscdevice.h>
  42. #include <linux/major.h>
  43. #include <linux/proc_fs.h>
  44. #include <linux/stat.h>
  45. #include <linux/poll.h>
  46. #include <linux/irq.h>
  47. #include <linux/init.h>
  48. #include <linux/gfp.h>
  49. #include <linux/mutex.h>
  50. #include <linux/cpu.h>
  51. #include <xen/events.h>
  52. #include <xen/evtchn.h>
  53. #include <asm/xen/hypervisor.h>
  54. struct per_user_data {
  55. /* Notification ring, accessed via /dev/xen/evtchn. */
  56. #define EVTCHN_RING_SIZE (PAGE_SIZE / sizeof(evtchn_port_t))
  57. #define EVTCHN_RING_MASK(_i) ((_i)&(EVTCHN_RING_SIZE-1))
  58. evtchn_port_t *ring;
  59. unsigned int ring_cons, ring_prod, ring_overflow;
  60. struct mutex ring_cons_mutex; /* protect against concurrent readers */
  61. /* Processes wait on this queue when ring is empty. */
  62. wait_queue_head_t evtchn_wait;
  63. struct fasync_struct *evtchn_async_queue;
  64. const char *name;
  65. };
  66. /* Who's bound to each port? */
  67. static struct per_user_data *port_user[NR_EVENT_CHANNELS];
  68. static DEFINE_SPINLOCK(port_user_lock);
  69. irqreturn_t evtchn_interrupt(int irq, void *data)
  70. {
  71. unsigned int port = (unsigned long)data;
  72. struct per_user_data *u;
  73. spin_lock(&port_user_lock);
  74. u = port_user[port];
  75. disable_irq_nosync(irq);
  76. if ((u->ring_prod - u->ring_cons) < EVTCHN_RING_SIZE) {
  77. u->ring[EVTCHN_RING_MASK(u->ring_prod)] = port;
  78. wmb(); /* Ensure ring contents visible */
  79. if (u->ring_cons == u->ring_prod++) {
  80. wake_up_interruptible(&u->evtchn_wait);
  81. kill_fasync(&u->evtchn_async_queue,
  82. SIGIO, POLL_IN);
  83. }
  84. } else {
  85. u->ring_overflow = 1;
  86. }
  87. spin_unlock(&port_user_lock);
  88. return IRQ_HANDLED;
  89. }
  90. static ssize_t evtchn_read(struct file *file, char __user *buf,
  91. size_t count, loff_t *ppos)
  92. {
  93. int rc;
  94. unsigned int c, p, bytes1 = 0, bytes2 = 0;
  95. struct per_user_data *u = file->private_data;
  96. /* Whole number of ports. */
  97. count &= ~(sizeof(evtchn_port_t)-1);
  98. if (count == 0)
  99. return 0;
  100. if (count > PAGE_SIZE)
  101. count = PAGE_SIZE;
  102. for (;;) {
  103. mutex_lock(&u->ring_cons_mutex);
  104. rc = -EFBIG;
  105. if (u->ring_overflow)
  106. goto unlock_out;
  107. c = u->ring_cons;
  108. p = u->ring_prod;
  109. if (c != p)
  110. break;
  111. mutex_unlock(&u->ring_cons_mutex);
  112. if (file->f_flags & O_NONBLOCK)
  113. return -EAGAIN;
  114. rc = wait_event_interruptible(u->evtchn_wait,
  115. u->ring_cons != u->ring_prod);
  116. if (rc)
  117. return rc;
  118. }
  119. /* Byte lengths of two chunks. Chunk split (if any) is at ring wrap. */
  120. if (((c ^ p) & EVTCHN_RING_SIZE) != 0) {
  121. bytes1 = (EVTCHN_RING_SIZE - EVTCHN_RING_MASK(c)) *
  122. sizeof(evtchn_port_t);
  123. bytes2 = EVTCHN_RING_MASK(p) * sizeof(evtchn_port_t);
  124. } else {
  125. bytes1 = (p - c) * sizeof(evtchn_port_t);
  126. bytes2 = 0;
  127. }
  128. /* Truncate chunks according to caller's maximum byte count. */
  129. if (bytes1 > count) {
  130. bytes1 = count;
  131. bytes2 = 0;
  132. } else if ((bytes1 + bytes2) > count) {
  133. bytes2 = count - bytes1;
  134. }
  135. rc = -EFAULT;
  136. rmb(); /* Ensure that we see the port before we copy it. */
  137. if (copy_to_user(buf, &u->ring[EVTCHN_RING_MASK(c)], bytes1) ||
  138. ((bytes2 != 0) &&
  139. copy_to_user(&buf[bytes1], &u->ring[0], bytes2)))
  140. goto unlock_out;
  141. u->ring_cons += (bytes1 + bytes2) / sizeof(evtchn_port_t);
  142. rc = bytes1 + bytes2;
  143. unlock_out:
  144. mutex_unlock(&u->ring_cons_mutex);
  145. return rc;
  146. }
  147. static ssize_t evtchn_write(struct file *file, const char __user *buf,
  148. size_t count, loff_t *ppos)
  149. {
  150. int rc, i;
  151. evtchn_port_t *kbuf = (evtchn_port_t *)__get_free_page(GFP_KERNEL);
  152. struct per_user_data *u = file->private_data;
  153. if (kbuf == NULL)
  154. return -ENOMEM;
  155. /* Whole number of ports. */
  156. count &= ~(sizeof(evtchn_port_t)-1);
  157. rc = 0;
  158. if (count == 0)
  159. goto out;
  160. if (count > PAGE_SIZE)
  161. count = PAGE_SIZE;
  162. rc = -EFAULT;
  163. if (copy_from_user(kbuf, buf, count) != 0)
  164. goto out;
  165. spin_lock_irq(&port_user_lock);
  166. for (i = 0; i < (count/sizeof(evtchn_port_t)); i++)
  167. if ((kbuf[i] < NR_EVENT_CHANNELS) && (port_user[kbuf[i]] == u))
  168. enable_irq(irq_from_evtchn(kbuf[i]));
  169. spin_unlock_irq(&port_user_lock);
  170. rc = count;
  171. out:
  172. free_page((unsigned long)kbuf);
  173. return rc;
  174. }
  175. static int evtchn_bind_to_user(struct per_user_data *u, int port)
  176. {
  177. int irq;
  178. int rc = 0;
  179. spin_lock_irq(&port_user_lock);
  180. BUG_ON(port_user[port] != NULL);
  181. irq = bind_evtchn_to_irqhandler(port, evtchn_interrupt, IRQF_DISABLED,
  182. u->name, (void *)(unsigned long)port);
  183. if (rc < 0)
  184. goto fail;
  185. port_user[port] = u;
  186. fail:
  187. spin_unlock_irq(&port_user_lock);
  188. return rc;
  189. }
  190. static void evtchn_unbind_from_user(struct per_user_data *u, int port)
  191. {
  192. int irq = irq_from_evtchn(port);
  193. unbind_from_irqhandler(irq, (void *)(unsigned long)port);
  194. port_user[port] = NULL;
  195. }
  196. static long evtchn_ioctl(struct file *file,
  197. unsigned int cmd, unsigned long arg)
  198. {
  199. int rc;
  200. struct per_user_data *u = file->private_data;
  201. void __user *uarg = (void __user *) arg;
  202. switch (cmd) {
  203. case IOCTL_EVTCHN_BIND_VIRQ: {
  204. struct ioctl_evtchn_bind_virq bind;
  205. struct evtchn_bind_virq bind_virq;
  206. rc = -EFAULT;
  207. if (copy_from_user(&bind, uarg, sizeof(bind)))
  208. break;
  209. bind_virq.virq = bind.virq;
  210. bind_virq.vcpu = 0;
  211. rc = HYPERVISOR_event_channel_op(EVTCHNOP_bind_virq,
  212. &bind_virq);
  213. if (rc != 0)
  214. break;
  215. rc = evtchn_bind_to_user(u, bind_virq.port);
  216. if (rc == 0)
  217. rc = bind_virq.port;
  218. break;
  219. }
  220. case IOCTL_EVTCHN_BIND_INTERDOMAIN: {
  221. struct ioctl_evtchn_bind_interdomain bind;
  222. struct evtchn_bind_interdomain bind_interdomain;
  223. rc = -EFAULT;
  224. if (copy_from_user(&bind, uarg, sizeof(bind)))
  225. break;
  226. bind_interdomain.remote_dom = bind.remote_domain;
  227. bind_interdomain.remote_port = bind.remote_port;
  228. rc = HYPERVISOR_event_channel_op(EVTCHNOP_bind_interdomain,
  229. &bind_interdomain);
  230. if (rc != 0)
  231. break;
  232. rc = evtchn_bind_to_user(u, bind_interdomain.local_port);
  233. if (rc == 0)
  234. rc = bind_interdomain.local_port;
  235. break;
  236. }
  237. case IOCTL_EVTCHN_BIND_UNBOUND_PORT: {
  238. struct ioctl_evtchn_bind_unbound_port bind;
  239. struct evtchn_alloc_unbound alloc_unbound;
  240. rc = -EFAULT;
  241. if (copy_from_user(&bind, uarg, sizeof(bind)))
  242. break;
  243. alloc_unbound.dom = DOMID_SELF;
  244. alloc_unbound.remote_dom = bind.remote_domain;
  245. rc = HYPERVISOR_event_channel_op(EVTCHNOP_alloc_unbound,
  246. &alloc_unbound);
  247. if (rc != 0)
  248. break;
  249. rc = evtchn_bind_to_user(u, alloc_unbound.port);
  250. if (rc == 0)
  251. rc = alloc_unbound.port;
  252. break;
  253. }
  254. case IOCTL_EVTCHN_UNBIND: {
  255. struct ioctl_evtchn_unbind unbind;
  256. rc = -EFAULT;
  257. if (copy_from_user(&unbind, uarg, sizeof(unbind)))
  258. break;
  259. rc = -EINVAL;
  260. if (unbind.port >= NR_EVENT_CHANNELS)
  261. break;
  262. spin_lock_irq(&port_user_lock);
  263. rc = -ENOTCONN;
  264. if (port_user[unbind.port] != u) {
  265. spin_unlock_irq(&port_user_lock);
  266. break;
  267. }
  268. evtchn_unbind_from_user(u, unbind.port);
  269. spin_unlock_irq(&port_user_lock);
  270. rc = 0;
  271. break;
  272. }
  273. case IOCTL_EVTCHN_NOTIFY: {
  274. struct ioctl_evtchn_notify notify;
  275. rc = -EFAULT;
  276. if (copy_from_user(&notify, uarg, sizeof(notify)))
  277. break;
  278. if (notify.port >= NR_EVENT_CHANNELS) {
  279. rc = -EINVAL;
  280. } else if (port_user[notify.port] != u) {
  281. rc = -ENOTCONN;
  282. } else {
  283. notify_remote_via_evtchn(notify.port);
  284. rc = 0;
  285. }
  286. break;
  287. }
  288. case IOCTL_EVTCHN_RESET: {
  289. /* Initialise the ring to empty. Clear errors. */
  290. mutex_lock(&u->ring_cons_mutex);
  291. spin_lock_irq(&port_user_lock);
  292. u->ring_cons = u->ring_prod = u->ring_overflow = 0;
  293. spin_unlock_irq(&port_user_lock);
  294. mutex_unlock(&u->ring_cons_mutex);
  295. rc = 0;
  296. break;
  297. }
  298. default:
  299. rc = -ENOSYS;
  300. break;
  301. }
  302. return rc;
  303. }
  304. static unsigned int evtchn_poll(struct file *file, poll_table *wait)
  305. {
  306. unsigned int mask = POLLOUT | POLLWRNORM;
  307. struct per_user_data *u = file->private_data;
  308. poll_wait(file, &u->evtchn_wait, wait);
  309. if (u->ring_cons != u->ring_prod)
  310. mask |= POLLIN | POLLRDNORM;
  311. if (u->ring_overflow)
  312. mask = POLLERR;
  313. return mask;
  314. }
  315. static int evtchn_fasync(int fd, struct file *filp, int on)
  316. {
  317. struct per_user_data *u = filp->private_data;
  318. return fasync_helper(fd, filp, on, &u->evtchn_async_queue);
  319. }
  320. static int evtchn_open(struct inode *inode, struct file *filp)
  321. {
  322. struct per_user_data *u;
  323. u = kzalloc(sizeof(*u), GFP_KERNEL);
  324. if (u == NULL)
  325. return -ENOMEM;
  326. u->name = kasprintf(GFP_KERNEL, "evtchn:%s", current->comm);
  327. if (u->name == NULL) {
  328. kfree(u);
  329. return -ENOMEM;
  330. }
  331. init_waitqueue_head(&u->evtchn_wait);
  332. u->ring = (evtchn_port_t *)__get_free_page(GFP_KERNEL);
  333. if (u->ring == NULL) {
  334. kfree(u->name);
  335. kfree(u);
  336. return -ENOMEM;
  337. }
  338. mutex_init(&u->ring_cons_mutex);
  339. filp->private_data = u;
  340. return 0;
  341. }
  342. static int evtchn_release(struct inode *inode, struct file *filp)
  343. {
  344. int i;
  345. struct per_user_data *u = filp->private_data;
  346. spin_lock_irq(&port_user_lock);
  347. free_page((unsigned long)u->ring);
  348. for (i = 0; i < NR_EVENT_CHANNELS; i++) {
  349. if (port_user[i] != u)
  350. continue;
  351. evtchn_unbind_from_user(port_user[i], i);
  352. }
  353. spin_unlock_irq(&port_user_lock);
  354. kfree(u->name);
  355. kfree(u);
  356. return 0;
  357. }
  358. static const struct file_operations evtchn_fops = {
  359. .owner = THIS_MODULE,
  360. .read = evtchn_read,
  361. .write = evtchn_write,
  362. .unlocked_ioctl = evtchn_ioctl,
  363. .poll = evtchn_poll,
  364. .fasync = evtchn_fasync,
  365. .open = evtchn_open,
  366. .release = evtchn_release,
  367. };
  368. static struct miscdevice evtchn_miscdev = {
  369. .minor = MISC_DYNAMIC_MINOR,
  370. .name = "evtchn",
  371. .fops = &evtchn_fops,
  372. };
  373. static int __init evtchn_init(void)
  374. {
  375. int err;
  376. if (!xen_domain())
  377. return -ENODEV;
  378. spin_lock_init(&port_user_lock);
  379. memset(port_user, 0, sizeof(port_user));
  380. /* Create '/dev/misc/evtchn'. */
  381. err = misc_register(&evtchn_miscdev);
  382. if (err != 0) {
  383. printk(KERN_ALERT "Could not register /dev/misc/evtchn\n");
  384. return err;
  385. }
  386. printk(KERN_INFO "Event-channel device installed.\n");
  387. return 0;
  388. }
  389. static void __exit evtchn_cleanup(void)
  390. {
  391. misc_deregister(&evtchn_miscdev);
  392. }
  393. module_init(evtchn_init);
  394. module_exit(evtchn_cleanup);
  395. MODULE_LICENSE("GPL");