evtchn.c 13 KB

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