evtchn.c 13 KB

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