evtchn.c 14 KB

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