evtchn.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  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/miscdevice.h>
  41. #include <linux/major.h>
  42. #include <linux/proc_fs.h>
  43. #include <linux/stat.h>
  44. #include <linux/poll.h>
  45. #include <linux/irq.h>
  46. #include <linux/init.h>
  47. #include <linux/gfp.h>
  48. #include <linux/mutex.h>
  49. #include <linux/cpu.h>
  50. #include <xen/xen.h>
  51. #include <xen/events.h>
  52. #include <xen/evtchn.h>
  53. #include <asm/xen/hypervisor.h>
  54. struct per_user_data {
  55. struct mutex bind_mutex; /* serialize bind/unbind operations */
  56. /* Notification ring, accessed via /dev/xen/evtchn. */
  57. #define EVTCHN_RING_SIZE (PAGE_SIZE / sizeof(evtchn_port_t))
  58. #define EVTCHN_RING_MASK(_i) ((_i)&(EVTCHN_RING_SIZE-1))
  59. evtchn_port_t *ring;
  60. unsigned int ring_cons, ring_prod, ring_overflow;
  61. struct mutex ring_cons_mutex; /* protect against concurrent readers */
  62. /* Processes wait on this queue when ring is empty. */
  63. wait_queue_head_t evtchn_wait;
  64. struct fasync_struct *evtchn_async_queue;
  65. const char *name;
  66. };
  67. /* Who's bound to each port? */
  68. static struct per_user_data *port_user[NR_EVENT_CHANNELS];
  69. static DEFINE_SPINLOCK(port_user_lock); /* protects port_user[] and ring_prod */
  70. irqreturn_t evtchn_interrupt(int irq, void *data)
  71. {
  72. unsigned int port = (unsigned long)data;
  73. struct per_user_data *u;
  74. spin_lock(&port_user_lock);
  75. u = port_user[port];
  76. disable_irq_nosync(irq);
  77. if ((u->ring_prod - u->ring_cons) < EVTCHN_RING_SIZE) {
  78. u->ring[EVTCHN_RING_MASK(u->ring_prod)] = port;
  79. wmb(); /* Ensure ring contents visible */
  80. if (u->ring_cons == u->ring_prod++) {
  81. wake_up_interruptible(&u->evtchn_wait);
  82. kill_fasync(&u->evtchn_async_queue,
  83. SIGIO, POLL_IN);
  84. }
  85. } else {
  86. u->ring_overflow = 1;
  87. }
  88. spin_unlock(&port_user_lock);
  89. return IRQ_HANDLED;
  90. }
  91. static ssize_t evtchn_read(struct file *file, char __user *buf,
  92. size_t count, loff_t *ppos)
  93. {
  94. int rc;
  95. unsigned int c, p, bytes1 = 0, bytes2 = 0;
  96. struct per_user_data *u = file->private_data;
  97. /* Whole number of ports. */
  98. count &= ~(sizeof(evtchn_port_t)-1);
  99. if (count == 0)
  100. return 0;
  101. if (count > PAGE_SIZE)
  102. count = PAGE_SIZE;
  103. for (;;) {
  104. mutex_lock(&u->ring_cons_mutex);
  105. rc = -EFBIG;
  106. if (u->ring_overflow)
  107. goto unlock_out;
  108. c = u->ring_cons;
  109. p = u->ring_prod;
  110. if (c != p)
  111. break;
  112. mutex_unlock(&u->ring_cons_mutex);
  113. if (file->f_flags & O_NONBLOCK)
  114. return -EAGAIN;
  115. rc = wait_event_interruptible(u->evtchn_wait,
  116. u->ring_cons != u->ring_prod);
  117. if (rc)
  118. return rc;
  119. }
  120. /* Byte lengths of two chunks. Chunk split (if any) is at ring wrap. */
  121. if (((c ^ p) & EVTCHN_RING_SIZE) != 0) {
  122. bytes1 = (EVTCHN_RING_SIZE - EVTCHN_RING_MASK(c)) *
  123. sizeof(evtchn_port_t);
  124. bytes2 = EVTCHN_RING_MASK(p) * sizeof(evtchn_port_t);
  125. } else {
  126. bytes1 = (p - c) * sizeof(evtchn_port_t);
  127. bytes2 = 0;
  128. }
  129. /* Truncate chunks according to caller's maximum byte count. */
  130. if (bytes1 > count) {
  131. bytes1 = count;
  132. bytes2 = 0;
  133. } else if ((bytes1 + bytes2) > count) {
  134. bytes2 = count - bytes1;
  135. }
  136. rc = -EFAULT;
  137. rmb(); /* Ensure that we see the port before we copy it. */
  138. if (copy_to_user(buf, &u->ring[EVTCHN_RING_MASK(c)], bytes1) ||
  139. ((bytes2 != 0) &&
  140. copy_to_user(&buf[bytes1], &u->ring[0], bytes2)))
  141. goto unlock_out;
  142. u->ring_cons += (bytes1 + bytes2) / sizeof(evtchn_port_t);
  143. rc = bytes1 + bytes2;
  144. unlock_out:
  145. mutex_unlock(&u->ring_cons_mutex);
  146. return rc;
  147. }
  148. static ssize_t evtchn_write(struct file *file, const char __user *buf,
  149. size_t count, loff_t *ppos)
  150. {
  151. int rc, i;
  152. evtchn_port_t *kbuf = (evtchn_port_t *)__get_free_page(GFP_KERNEL);
  153. struct per_user_data *u = file->private_data;
  154. if (kbuf == NULL)
  155. return -ENOMEM;
  156. /* Whole number of ports. */
  157. count &= ~(sizeof(evtchn_port_t)-1);
  158. rc = 0;
  159. if (count == 0)
  160. goto out;
  161. if (count > PAGE_SIZE)
  162. count = PAGE_SIZE;
  163. rc = -EFAULT;
  164. if (copy_from_user(kbuf, buf, count) != 0)
  165. goto out;
  166. spin_lock_irq(&port_user_lock);
  167. for (i = 0; i < (count/sizeof(evtchn_port_t)); i++)
  168. if ((kbuf[i] < NR_EVENT_CHANNELS) && (port_user[kbuf[i]] == u))
  169. enable_irq(irq_from_evtchn(kbuf[i]));
  170. spin_unlock_irq(&port_user_lock);
  171. rc = count;
  172. out:
  173. free_page((unsigned long)kbuf);
  174. return rc;
  175. }
  176. static int evtchn_bind_to_user(struct per_user_data *u, int port)
  177. {
  178. int rc = 0;
  179. /*
  180. * Ports are never reused, so every caller should pass in a
  181. * unique port.
  182. *
  183. * (Locking not necessary because we haven't registered the
  184. * interrupt handler yet, and our caller has already
  185. * serialized bind operations.)
  186. */
  187. BUG_ON(port_user[port] != NULL);
  188. port_user[port] = u;
  189. rc = bind_evtchn_to_irqhandler(port, evtchn_interrupt, IRQF_DISABLED,
  190. u->name, (void *)(unsigned long)port);
  191. if (rc >= 0)
  192. rc = 0;
  193. return rc;
  194. }
  195. static void evtchn_unbind_from_user(struct per_user_data *u, int port)
  196. {
  197. int irq = irq_from_evtchn(port);
  198. unbind_from_irqhandler(irq, (void *)(unsigned long)port);
  199. /* make sure we unbind the irq handler before clearing the port */
  200. barrier();
  201. port_user[port] = NULL;
  202. }
  203. static long evtchn_ioctl(struct file *file,
  204. unsigned int cmd, unsigned long arg)
  205. {
  206. int rc;
  207. struct per_user_data *u = file->private_data;
  208. void __user *uarg = (void __user *) arg;
  209. /* Prevent bind from racing with unbind */
  210. mutex_lock(&u->bind_mutex);
  211. switch (cmd) {
  212. case IOCTL_EVTCHN_BIND_VIRQ: {
  213. struct ioctl_evtchn_bind_virq bind;
  214. struct evtchn_bind_virq bind_virq;
  215. rc = -EFAULT;
  216. if (copy_from_user(&bind, uarg, sizeof(bind)))
  217. break;
  218. bind_virq.virq = bind.virq;
  219. bind_virq.vcpu = 0;
  220. rc = HYPERVISOR_event_channel_op(EVTCHNOP_bind_virq,
  221. &bind_virq);
  222. if (rc != 0)
  223. break;
  224. rc = evtchn_bind_to_user(u, bind_virq.port);
  225. if (rc == 0)
  226. rc = bind_virq.port;
  227. break;
  228. }
  229. case IOCTL_EVTCHN_BIND_INTERDOMAIN: {
  230. struct ioctl_evtchn_bind_interdomain bind;
  231. struct evtchn_bind_interdomain bind_interdomain;
  232. rc = -EFAULT;
  233. if (copy_from_user(&bind, uarg, sizeof(bind)))
  234. break;
  235. bind_interdomain.remote_dom = bind.remote_domain;
  236. bind_interdomain.remote_port = bind.remote_port;
  237. rc = HYPERVISOR_event_channel_op(EVTCHNOP_bind_interdomain,
  238. &bind_interdomain);
  239. if (rc != 0)
  240. break;
  241. rc = evtchn_bind_to_user(u, bind_interdomain.local_port);
  242. if (rc == 0)
  243. rc = bind_interdomain.local_port;
  244. break;
  245. }
  246. case IOCTL_EVTCHN_BIND_UNBOUND_PORT: {
  247. struct ioctl_evtchn_bind_unbound_port bind;
  248. struct evtchn_alloc_unbound alloc_unbound;
  249. rc = -EFAULT;
  250. if (copy_from_user(&bind, uarg, sizeof(bind)))
  251. break;
  252. alloc_unbound.dom = DOMID_SELF;
  253. alloc_unbound.remote_dom = bind.remote_domain;
  254. rc = HYPERVISOR_event_channel_op(EVTCHNOP_alloc_unbound,
  255. &alloc_unbound);
  256. if (rc != 0)
  257. break;
  258. rc = evtchn_bind_to_user(u, alloc_unbound.port);
  259. if (rc == 0)
  260. rc = alloc_unbound.port;
  261. break;
  262. }
  263. case IOCTL_EVTCHN_UNBIND: {
  264. struct ioctl_evtchn_unbind unbind;
  265. rc = -EFAULT;
  266. if (copy_from_user(&unbind, uarg, sizeof(unbind)))
  267. break;
  268. rc = -EINVAL;
  269. if (unbind.port >= NR_EVENT_CHANNELS)
  270. break;
  271. spin_lock_irq(&port_user_lock);
  272. rc = -ENOTCONN;
  273. if (port_user[unbind.port] != u) {
  274. spin_unlock_irq(&port_user_lock);
  275. break;
  276. }
  277. evtchn_unbind_from_user(u, unbind.port);
  278. spin_unlock_irq(&port_user_lock);
  279. rc = 0;
  280. break;
  281. }
  282. case IOCTL_EVTCHN_NOTIFY: {
  283. struct ioctl_evtchn_notify notify;
  284. rc = -EFAULT;
  285. if (copy_from_user(&notify, uarg, sizeof(notify)))
  286. break;
  287. if (notify.port >= NR_EVENT_CHANNELS) {
  288. rc = -EINVAL;
  289. } else if (port_user[notify.port] != u) {
  290. rc = -ENOTCONN;
  291. } else {
  292. notify_remote_via_evtchn(notify.port);
  293. rc = 0;
  294. }
  295. break;
  296. }
  297. case IOCTL_EVTCHN_RESET: {
  298. /* Initialise the ring to empty. Clear errors. */
  299. mutex_lock(&u->ring_cons_mutex);
  300. spin_lock_irq(&port_user_lock);
  301. u->ring_cons = u->ring_prod = u->ring_overflow = 0;
  302. spin_unlock_irq(&port_user_lock);
  303. mutex_unlock(&u->ring_cons_mutex);
  304. rc = 0;
  305. break;
  306. }
  307. default:
  308. rc = -ENOSYS;
  309. break;
  310. }
  311. mutex_unlock(&u->bind_mutex);
  312. return rc;
  313. }
  314. static unsigned int evtchn_poll(struct file *file, poll_table *wait)
  315. {
  316. unsigned int mask = POLLOUT | POLLWRNORM;
  317. struct per_user_data *u = file->private_data;
  318. poll_wait(file, &u->evtchn_wait, wait);
  319. if (u->ring_cons != u->ring_prod)
  320. mask |= POLLIN | POLLRDNORM;
  321. if (u->ring_overflow)
  322. mask = POLLERR;
  323. return mask;
  324. }
  325. static int evtchn_fasync(int fd, struct file *filp, int on)
  326. {
  327. struct per_user_data *u = filp->private_data;
  328. return fasync_helper(fd, filp, on, &u->evtchn_async_queue);
  329. }
  330. static int evtchn_open(struct inode *inode, struct file *filp)
  331. {
  332. struct per_user_data *u;
  333. u = kzalloc(sizeof(*u), GFP_KERNEL);
  334. if (u == NULL)
  335. return -ENOMEM;
  336. u->name = kasprintf(GFP_KERNEL, "evtchn:%s", current->comm);
  337. if (u->name == NULL) {
  338. kfree(u);
  339. return -ENOMEM;
  340. }
  341. init_waitqueue_head(&u->evtchn_wait);
  342. u->ring = (evtchn_port_t *)__get_free_page(GFP_KERNEL);
  343. if (u->ring == NULL) {
  344. kfree(u->name);
  345. kfree(u);
  346. return -ENOMEM;
  347. }
  348. mutex_init(&u->bind_mutex);
  349. mutex_init(&u->ring_cons_mutex);
  350. filp->private_data = u;
  351. return 0;
  352. }
  353. static int evtchn_release(struct inode *inode, struct file *filp)
  354. {
  355. int i;
  356. struct per_user_data *u = filp->private_data;
  357. spin_lock_irq(&port_user_lock);
  358. free_page((unsigned long)u->ring);
  359. for (i = 0; i < NR_EVENT_CHANNELS; i++) {
  360. if (port_user[i] != u)
  361. continue;
  362. evtchn_unbind_from_user(port_user[i], i);
  363. }
  364. spin_unlock_irq(&port_user_lock);
  365. kfree(u->name);
  366. kfree(u);
  367. return 0;
  368. }
  369. static const struct file_operations evtchn_fops = {
  370. .owner = THIS_MODULE,
  371. .read = evtchn_read,
  372. .write = evtchn_write,
  373. .unlocked_ioctl = evtchn_ioctl,
  374. .poll = evtchn_poll,
  375. .fasync = evtchn_fasync,
  376. .open = evtchn_open,
  377. .release = evtchn_release,
  378. };
  379. static struct miscdevice evtchn_miscdev = {
  380. .minor = MISC_DYNAMIC_MINOR,
  381. .name = "evtchn",
  382. .fops = &evtchn_fops,
  383. };
  384. static int __init evtchn_init(void)
  385. {
  386. int err;
  387. if (!xen_domain())
  388. return -ENODEV;
  389. spin_lock_init(&port_user_lock);
  390. memset(port_user, 0, sizeof(port_user));
  391. /* Create '/dev/misc/evtchn'. */
  392. err = misc_register(&evtchn_miscdev);
  393. if (err != 0) {
  394. printk(KERN_ALERT "Could not register /dev/misc/evtchn\n");
  395. return err;
  396. }
  397. printk(KERN_INFO "Event-channel device installed.\n");
  398. return 0;
  399. }
  400. static void __exit evtchn_cleanup(void)
  401. {
  402. misc_deregister(&evtchn_miscdev);
  403. }
  404. module_init(evtchn_init);
  405. module_exit(evtchn_cleanup);
  406. MODULE_LICENSE("GPL");