evtchn.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  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/mutex.h>
  48. #include <linux/cpu.h>
  49. #include <xen/xen.h>
  50. #include <xen/events.h>
  51. #include <xen/evtchn.h>
  52. #include <asm/xen/hypervisor.h>
  53. struct per_user_data {
  54. struct mutex bind_mutex; /* serialize bind/unbind operations */
  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); /* protects port_user[] and ring_prod */
  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 rc = 0;
  178. /*
  179. * Ports are never reused, so every caller should pass in a
  180. * unique port.
  181. *
  182. * (Locking not necessary because we haven't registered the
  183. * interrupt handler yet, and our caller has already
  184. * serialized bind operations.)
  185. */
  186. BUG_ON(port_user[port] != NULL);
  187. port_user[port] = u;
  188. rc = bind_evtchn_to_irqhandler(port, evtchn_interrupt, IRQF_DISABLED,
  189. u->name, (void *)(unsigned long)port);
  190. if (rc >= 0)
  191. rc = 0;
  192. return rc;
  193. }
  194. static void evtchn_unbind_from_user(struct per_user_data *u, int port)
  195. {
  196. int irq = irq_from_evtchn(port);
  197. unbind_from_irqhandler(irq, (void *)(unsigned long)port);
  198. /* make sure we unbind the irq handler before clearing the port */
  199. barrier();
  200. port_user[port] = NULL;
  201. }
  202. static long evtchn_ioctl(struct file *file,
  203. unsigned int cmd, unsigned long arg)
  204. {
  205. int rc;
  206. struct per_user_data *u = file->private_data;
  207. void __user *uarg = (void __user *) arg;
  208. /* Prevent bind from racing with unbind */
  209. mutex_lock(&u->bind_mutex);
  210. switch (cmd) {
  211. case IOCTL_EVTCHN_BIND_VIRQ: {
  212. struct ioctl_evtchn_bind_virq bind;
  213. struct evtchn_bind_virq bind_virq;
  214. rc = -EFAULT;
  215. if (copy_from_user(&bind, uarg, sizeof(bind)))
  216. break;
  217. bind_virq.virq = bind.virq;
  218. bind_virq.vcpu = 0;
  219. rc = HYPERVISOR_event_channel_op(EVTCHNOP_bind_virq,
  220. &bind_virq);
  221. if (rc != 0)
  222. break;
  223. rc = evtchn_bind_to_user(u, bind_virq.port);
  224. if (rc == 0)
  225. rc = bind_virq.port;
  226. break;
  227. }
  228. case IOCTL_EVTCHN_BIND_INTERDOMAIN: {
  229. struct ioctl_evtchn_bind_interdomain bind;
  230. struct evtchn_bind_interdomain bind_interdomain;
  231. rc = -EFAULT;
  232. if (copy_from_user(&bind, uarg, sizeof(bind)))
  233. break;
  234. bind_interdomain.remote_dom = bind.remote_domain;
  235. bind_interdomain.remote_port = bind.remote_port;
  236. rc = HYPERVISOR_event_channel_op(EVTCHNOP_bind_interdomain,
  237. &bind_interdomain);
  238. if (rc != 0)
  239. break;
  240. rc = evtchn_bind_to_user(u, bind_interdomain.local_port);
  241. if (rc == 0)
  242. rc = bind_interdomain.local_port;
  243. break;
  244. }
  245. case IOCTL_EVTCHN_BIND_UNBOUND_PORT: {
  246. struct ioctl_evtchn_bind_unbound_port bind;
  247. struct evtchn_alloc_unbound alloc_unbound;
  248. rc = -EFAULT;
  249. if (copy_from_user(&bind, uarg, sizeof(bind)))
  250. break;
  251. alloc_unbound.dom = DOMID_SELF;
  252. alloc_unbound.remote_dom = bind.remote_domain;
  253. rc = HYPERVISOR_event_channel_op(EVTCHNOP_alloc_unbound,
  254. &alloc_unbound);
  255. if (rc != 0)
  256. break;
  257. rc = evtchn_bind_to_user(u, alloc_unbound.port);
  258. if (rc == 0)
  259. rc = alloc_unbound.port;
  260. break;
  261. }
  262. case IOCTL_EVTCHN_UNBIND: {
  263. struct ioctl_evtchn_unbind unbind;
  264. rc = -EFAULT;
  265. if (copy_from_user(&unbind, uarg, sizeof(unbind)))
  266. break;
  267. rc = -EINVAL;
  268. if (unbind.port >= NR_EVENT_CHANNELS)
  269. break;
  270. spin_lock_irq(&port_user_lock);
  271. rc = -ENOTCONN;
  272. if (port_user[unbind.port] != u) {
  273. spin_unlock_irq(&port_user_lock);
  274. break;
  275. }
  276. evtchn_unbind_from_user(u, unbind.port);
  277. spin_unlock_irq(&port_user_lock);
  278. rc = 0;
  279. break;
  280. }
  281. case IOCTL_EVTCHN_NOTIFY: {
  282. struct ioctl_evtchn_notify notify;
  283. rc = -EFAULT;
  284. if (copy_from_user(&notify, uarg, sizeof(notify)))
  285. break;
  286. if (notify.port >= NR_EVENT_CHANNELS) {
  287. rc = -EINVAL;
  288. } else if (port_user[notify.port] != u) {
  289. rc = -ENOTCONN;
  290. } else {
  291. notify_remote_via_evtchn(notify.port);
  292. rc = 0;
  293. }
  294. break;
  295. }
  296. case IOCTL_EVTCHN_RESET: {
  297. /* Initialise the ring to empty. Clear errors. */
  298. mutex_lock(&u->ring_cons_mutex);
  299. spin_lock_irq(&port_user_lock);
  300. u->ring_cons = u->ring_prod = u->ring_overflow = 0;
  301. spin_unlock_irq(&port_user_lock);
  302. mutex_unlock(&u->ring_cons_mutex);
  303. rc = 0;
  304. break;
  305. }
  306. default:
  307. rc = -ENOSYS;
  308. break;
  309. }
  310. mutex_unlock(&u->bind_mutex);
  311. return rc;
  312. }
  313. static unsigned int evtchn_poll(struct file *file, poll_table *wait)
  314. {
  315. unsigned int mask = POLLOUT | POLLWRNORM;
  316. struct per_user_data *u = file->private_data;
  317. poll_wait(file, &u->evtchn_wait, wait);
  318. if (u->ring_cons != u->ring_prod)
  319. mask |= POLLIN | POLLRDNORM;
  320. if (u->ring_overflow)
  321. mask = POLLERR;
  322. return mask;
  323. }
  324. static int evtchn_fasync(int fd, struct file *filp, int on)
  325. {
  326. struct per_user_data *u = filp->private_data;
  327. return fasync_helper(fd, filp, on, &u->evtchn_async_queue);
  328. }
  329. static int evtchn_open(struct inode *inode, struct file *filp)
  330. {
  331. struct per_user_data *u;
  332. u = kzalloc(sizeof(*u), GFP_KERNEL);
  333. if (u == NULL)
  334. return -ENOMEM;
  335. u->name = kasprintf(GFP_KERNEL, "evtchn:%s", current->comm);
  336. if (u->name == NULL) {
  337. kfree(u);
  338. return -ENOMEM;
  339. }
  340. init_waitqueue_head(&u->evtchn_wait);
  341. u->ring = (evtchn_port_t *)__get_free_page(GFP_KERNEL);
  342. if (u->ring == NULL) {
  343. kfree(u->name);
  344. kfree(u);
  345. return -ENOMEM;
  346. }
  347. mutex_init(&u->bind_mutex);
  348. mutex_init(&u->ring_cons_mutex);
  349. filp->private_data = u;
  350. return 0;
  351. }
  352. static int evtchn_release(struct inode *inode, struct file *filp)
  353. {
  354. int i;
  355. struct per_user_data *u = filp->private_data;
  356. spin_lock_irq(&port_user_lock);
  357. free_page((unsigned long)u->ring);
  358. for (i = 0; i < NR_EVENT_CHANNELS; i++) {
  359. if (port_user[i] != u)
  360. continue;
  361. evtchn_unbind_from_user(port_user[i], i);
  362. }
  363. spin_unlock_irq(&port_user_lock);
  364. kfree(u->name);
  365. kfree(u);
  366. return 0;
  367. }
  368. static const struct file_operations evtchn_fops = {
  369. .owner = THIS_MODULE,
  370. .read = evtchn_read,
  371. .write = evtchn_write,
  372. .unlocked_ioctl = evtchn_ioctl,
  373. .poll = evtchn_poll,
  374. .fasync = evtchn_fasync,
  375. .open = evtchn_open,
  376. .release = evtchn_release,
  377. };
  378. static struct miscdevice evtchn_miscdev = {
  379. .minor = MISC_DYNAMIC_MINOR,
  380. .name = "evtchn",
  381. .fops = &evtchn_fops,
  382. };
  383. static int __init evtchn_init(void)
  384. {
  385. int err;
  386. if (!xen_domain())
  387. return -ENODEV;
  388. spin_lock_init(&port_user_lock);
  389. memset(port_user, 0, sizeof(port_user));
  390. /* Create '/dev/misc/evtchn'. */
  391. err = misc_register(&evtchn_miscdev);
  392. if (err != 0) {
  393. printk(KERN_ALERT "Could not register /dev/misc/evtchn\n");
  394. return err;
  395. }
  396. printk(KERN_INFO "Event-channel device installed.\n");
  397. return 0;
  398. }
  399. static void __exit evtchn_cleanup(void)
  400. {
  401. misc_deregister(&evtchn_miscdev);
  402. }
  403. module_init(evtchn_init);
  404. module_exit(evtchn_cleanup);
  405. MODULE_LICENSE("GPL");