evtchn.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
  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 = evtchn_make_refcounted(port);
  224. else {
  225. /* bind failed, should close the port now */
  226. struct evtchn_close close;
  227. close.port = port;
  228. if (HYPERVISOR_event_channel_op(EVTCHNOP_close, &close) != 0)
  229. BUG();
  230. set_port_user(port, NULL);
  231. }
  232. return rc;
  233. }
  234. static void evtchn_unbind_from_user(struct per_user_data *u, int port)
  235. {
  236. int irq = irq_from_evtchn(port);
  237. BUG_ON(irq < 0);
  238. unbind_from_irqhandler(irq, (void *)(unsigned long)port);
  239. set_port_user(port, NULL);
  240. }
  241. static long evtchn_ioctl(struct file *file,
  242. unsigned int cmd, unsigned long arg)
  243. {
  244. int rc;
  245. struct per_user_data *u = file->private_data;
  246. void __user *uarg = (void __user *) arg;
  247. /* Prevent bind from racing with unbind */
  248. mutex_lock(&u->bind_mutex);
  249. switch (cmd) {
  250. case IOCTL_EVTCHN_BIND_VIRQ: {
  251. struct ioctl_evtchn_bind_virq bind;
  252. struct evtchn_bind_virq bind_virq;
  253. rc = -EFAULT;
  254. if (copy_from_user(&bind, uarg, sizeof(bind)))
  255. break;
  256. bind_virq.virq = bind.virq;
  257. bind_virq.vcpu = 0;
  258. rc = HYPERVISOR_event_channel_op(EVTCHNOP_bind_virq,
  259. &bind_virq);
  260. if (rc != 0)
  261. break;
  262. rc = evtchn_bind_to_user(u, bind_virq.port);
  263. if (rc == 0)
  264. rc = bind_virq.port;
  265. break;
  266. }
  267. case IOCTL_EVTCHN_BIND_INTERDOMAIN: {
  268. struct ioctl_evtchn_bind_interdomain bind;
  269. struct evtchn_bind_interdomain bind_interdomain;
  270. rc = -EFAULT;
  271. if (copy_from_user(&bind, uarg, sizeof(bind)))
  272. break;
  273. bind_interdomain.remote_dom = bind.remote_domain;
  274. bind_interdomain.remote_port = bind.remote_port;
  275. rc = HYPERVISOR_event_channel_op(EVTCHNOP_bind_interdomain,
  276. &bind_interdomain);
  277. if (rc != 0)
  278. break;
  279. rc = evtchn_bind_to_user(u, bind_interdomain.local_port);
  280. if (rc == 0)
  281. rc = bind_interdomain.local_port;
  282. break;
  283. }
  284. case IOCTL_EVTCHN_BIND_UNBOUND_PORT: {
  285. struct ioctl_evtchn_bind_unbound_port bind;
  286. struct evtchn_alloc_unbound alloc_unbound;
  287. rc = -EFAULT;
  288. if (copy_from_user(&bind, uarg, sizeof(bind)))
  289. break;
  290. alloc_unbound.dom = DOMID_SELF;
  291. alloc_unbound.remote_dom = bind.remote_domain;
  292. rc = HYPERVISOR_event_channel_op(EVTCHNOP_alloc_unbound,
  293. &alloc_unbound);
  294. if (rc != 0)
  295. break;
  296. rc = evtchn_bind_to_user(u, alloc_unbound.port);
  297. if (rc == 0)
  298. rc = alloc_unbound.port;
  299. break;
  300. }
  301. case IOCTL_EVTCHN_UNBIND: {
  302. struct ioctl_evtchn_unbind unbind;
  303. rc = -EFAULT;
  304. if (copy_from_user(&unbind, uarg, sizeof(unbind)))
  305. break;
  306. rc = -EINVAL;
  307. if (unbind.port >= NR_EVENT_CHANNELS)
  308. break;
  309. rc = -ENOTCONN;
  310. if (get_port_user(unbind.port) != u)
  311. break;
  312. disable_irq(irq_from_evtchn(unbind.port));
  313. evtchn_unbind_from_user(u, unbind.port);
  314. rc = 0;
  315. break;
  316. }
  317. case IOCTL_EVTCHN_NOTIFY: {
  318. struct ioctl_evtchn_notify notify;
  319. rc = -EFAULT;
  320. if (copy_from_user(&notify, uarg, sizeof(notify)))
  321. break;
  322. if (notify.port >= NR_EVENT_CHANNELS) {
  323. rc = -EINVAL;
  324. } else if (get_port_user(notify.port) != u) {
  325. rc = -ENOTCONN;
  326. } else {
  327. notify_remote_via_evtchn(notify.port);
  328. rc = 0;
  329. }
  330. break;
  331. }
  332. case IOCTL_EVTCHN_RESET: {
  333. /* Initialise the ring to empty. Clear errors. */
  334. mutex_lock(&u->ring_cons_mutex);
  335. spin_lock_irq(&port_user_lock);
  336. u->ring_cons = u->ring_prod = u->ring_overflow = 0;
  337. spin_unlock_irq(&port_user_lock);
  338. mutex_unlock(&u->ring_cons_mutex);
  339. rc = 0;
  340. break;
  341. }
  342. default:
  343. rc = -ENOSYS;
  344. break;
  345. }
  346. mutex_unlock(&u->bind_mutex);
  347. return rc;
  348. }
  349. static unsigned int evtchn_poll(struct file *file, poll_table *wait)
  350. {
  351. unsigned int mask = POLLOUT | POLLWRNORM;
  352. struct per_user_data *u = file->private_data;
  353. poll_wait(file, &u->evtchn_wait, wait);
  354. if (u->ring_cons != u->ring_prod)
  355. mask |= POLLIN | POLLRDNORM;
  356. if (u->ring_overflow)
  357. mask = POLLERR;
  358. return mask;
  359. }
  360. static int evtchn_fasync(int fd, struct file *filp, int on)
  361. {
  362. struct per_user_data *u = filp->private_data;
  363. return fasync_helper(fd, filp, on, &u->evtchn_async_queue);
  364. }
  365. static int evtchn_open(struct inode *inode, struct file *filp)
  366. {
  367. struct per_user_data *u;
  368. u = kzalloc(sizeof(*u), GFP_KERNEL);
  369. if (u == NULL)
  370. return -ENOMEM;
  371. u->name = kasprintf(GFP_KERNEL, "evtchn:%s", current->comm);
  372. if (u->name == NULL) {
  373. kfree(u);
  374. return -ENOMEM;
  375. }
  376. init_waitqueue_head(&u->evtchn_wait);
  377. u->ring = (evtchn_port_t *)__get_free_page(GFP_KERNEL);
  378. if (u->ring == NULL) {
  379. kfree(u->name);
  380. kfree(u);
  381. return -ENOMEM;
  382. }
  383. mutex_init(&u->bind_mutex);
  384. mutex_init(&u->ring_cons_mutex);
  385. filp->private_data = u;
  386. return nonseekable_open(inode, filp);
  387. }
  388. static int evtchn_release(struct inode *inode, struct file *filp)
  389. {
  390. int i;
  391. struct per_user_data *u = filp->private_data;
  392. for (i = 0; i < NR_EVENT_CHANNELS; i++) {
  393. if (get_port_user(i) != u)
  394. continue;
  395. disable_irq(irq_from_evtchn(i));
  396. evtchn_unbind_from_user(get_port_user(i), i);
  397. }
  398. free_page((unsigned long)u->ring);
  399. kfree(u->name);
  400. kfree(u);
  401. return 0;
  402. }
  403. static const struct file_operations evtchn_fops = {
  404. .owner = THIS_MODULE,
  405. .read = evtchn_read,
  406. .write = evtchn_write,
  407. .unlocked_ioctl = evtchn_ioctl,
  408. .poll = evtchn_poll,
  409. .fasync = evtchn_fasync,
  410. .open = evtchn_open,
  411. .release = evtchn_release,
  412. .llseek = no_llseek,
  413. };
  414. static struct miscdevice evtchn_miscdev = {
  415. .minor = MISC_DYNAMIC_MINOR,
  416. .name = "xen/evtchn",
  417. .fops = &evtchn_fops,
  418. };
  419. static int __init evtchn_init(void)
  420. {
  421. int err;
  422. if (!xen_domain())
  423. return -ENODEV;
  424. port_user = kcalloc(NR_EVENT_CHANNELS, sizeof(*port_user), GFP_KERNEL);
  425. if (port_user == NULL)
  426. return -ENOMEM;
  427. spin_lock_init(&port_user_lock);
  428. /* Create '/dev/misc/evtchn'. */
  429. err = misc_register(&evtchn_miscdev);
  430. if (err != 0) {
  431. printk(KERN_ALERT "Could not register /dev/misc/evtchn\n");
  432. return err;
  433. }
  434. printk(KERN_INFO "Event-channel device installed.\n");
  435. return 0;
  436. }
  437. static void __exit evtchn_cleanup(void)
  438. {
  439. kfree(port_user);
  440. port_user = NULL;
  441. misc_deregister(&evtchn_miscdev);
  442. }
  443. module_init(evtchn_init);
  444. module_exit(evtchn_cleanup);
  445. MODULE_LICENSE("GPL");