blob: 8feecf01d55c95c9241080cf36823f197b3b2d5c [file] [log] [blame]
Ian Campbellf7116282009-02-06 19:21:19 -08001/******************************************************************************
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
Joe Perches283c0972013-06-28 03:21:41 -070034#define pr_fmt(fmt) "xen:" KBUILD_MODNAME ": " fmt
35
Ian Campbellf7116282009-02-06 19:21:19 -080036#include <linux/module.h>
37#include <linux/kernel.h>
38#include <linux/sched.h>
39#include <linux/slab.h>
40#include <linux/string.h>
41#include <linux/errno.h>
42#include <linux/fs.h>
Ian Campbellf7116282009-02-06 19:21:19 -080043#include <linux/miscdevice.h>
44#include <linux/major.h>
45#include <linux/proc_fs.h>
46#include <linux/stat.h>
47#include <linux/poll.h>
48#include <linux/irq.h>
49#include <linux/init.h>
Ian Campbellf7116282009-02-06 19:21:19 -080050#include <linux/mutex.h>
51#include <linux/cpu.h>
Jeremy Fitzhardinge1ccbf532009-10-06 15:11:14 -070052
53#include <xen/xen.h>
Ian Campbellf7116282009-02-06 19:21:19 -080054#include <xen/events.h>
55#include <xen/evtchn.h>
56#include <asm/xen/hypervisor.h>
57
58struct per_user_data {
Jeremy Fitzhardinge0a4666b2009-02-12 13:03:24 -080059 struct mutex bind_mutex; /* serialize bind/unbind operations */
60
Ian Campbellf7116282009-02-06 19:21:19 -080061 /* Notification ring, accessed via /dev/xen/evtchn. */
62#define EVTCHN_RING_SIZE (PAGE_SIZE / sizeof(evtchn_port_t))
63#define EVTCHN_RING_MASK(_i) ((_i)&(EVTCHN_RING_SIZE-1))
64 evtchn_port_t *ring;
65 unsigned int ring_cons, ring_prod, ring_overflow;
66 struct mutex ring_cons_mutex; /* protect against concurrent readers */
67
68 /* Processes wait on this queue when ring is empty. */
69 wait_queue_head_t evtchn_wait;
70 struct fasync_struct *evtchn_async_queue;
71 const char *name;
72};
73
Jeremy Fitzhardingee3cc0672009-09-18 16:31:22 -070074/*
75 * Who's bound to each port? This is logically an array of struct
76 * per_user_data *, but we encode the current enabled-state in bit 0.
77 */
Jeremy Fitzhardinge93afe0b2009-09-18 16:36:58 -070078static unsigned long *port_user;
Jeremy Fitzhardinge0a4666b2009-02-12 13:03:24 -080079static DEFINE_SPINLOCK(port_user_lock); /* protects port_user[] and ring_prod */
Ian Campbellf7116282009-02-06 19:21:19 -080080
Jeremy Fitzhardingee3cc0672009-09-18 16:31:22 -070081static inline struct per_user_data *get_port_user(unsigned port)
82{
83 return (struct per_user_data *)(port_user[port] & ~1);
84}
85
86static inline void set_port_user(unsigned port, struct per_user_data *u)
87{
88 port_user[port] = (unsigned long)u;
89}
90
91static inline bool get_port_enabled(unsigned port)
92{
93 return port_user[port] & 1;
94}
95
96static inline void set_port_enabled(unsigned port, bool enabled)
97{
98 if (enabled)
99 port_user[port] |= 1;
100 else
101 port_user[port] &= ~1;
102}
103
Jeremy Fitzhardinge70697d542010-10-05 11:13:44 -0700104static irqreturn_t evtchn_interrupt(int irq, void *data)
Ian Campbellf7116282009-02-06 19:21:19 -0800105{
106 unsigned int port = (unsigned long)data;
107 struct per_user_data *u;
108
109 spin_lock(&port_user_lock);
110
Jeremy Fitzhardingee3cc0672009-09-18 16:31:22 -0700111 u = get_port_user(port);
112
Jeremy Fitzhardinge0edce912009-09-18 16:55:29 -0700113 WARN(!get_port_enabled(port),
114 "Interrupt for port %d, but apparently not enabled; per-user %p\n",
115 port, u);
Ian Campbellf7116282009-02-06 19:21:19 -0800116
117 disable_irq_nosync(irq);
Jeremy Fitzhardingee3cc0672009-09-18 16:31:22 -0700118 set_port_enabled(port, false);
Ian Campbellf7116282009-02-06 19:21:19 -0800119
120 if ((u->ring_prod - u->ring_cons) < EVTCHN_RING_SIZE) {
121 u->ring[EVTCHN_RING_MASK(u->ring_prod)] = port;
122 wmb(); /* Ensure ring contents visible */
123 if (u->ring_cons == u->ring_prod++) {
124 wake_up_interruptible(&u->evtchn_wait);
125 kill_fasync(&u->evtchn_async_queue,
126 SIGIO, POLL_IN);
127 }
Jeremy Fitzhardingee3cc0672009-09-18 16:31:22 -0700128 } else
Ian Campbellf7116282009-02-06 19:21:19 -0800129 u->ring_overflow = 1;
Ian Campbellf7116282009-02-06 19:21:19 -0800130
131 spin_unlock(&port_user_lock);
132
133 return IRQ_HANDLED;
134}
135
136static ssize_t evtchn_read(struct file *file, char __user *buf,
137 size_t count, loff_t *ppos)
138{
139 int rc;
140 unsigned int c, p, bytes1 = 0, bytes2 = 0;
141 struct per_user_data *u = file->private_data;
142
143 /* Whole number of ports. */
144 count &= ~(sizeof(evtchn_port_t)-1);
145
146 if (count == 0)
147 return 0;
148
149 if (count > PAGE_SIZE)
150 count = PAGE_SIZE;
151
152 for (;;) {
153 mutex_lock(&u->ring_cons_mutex);
154
155 rc = -EFBIG;
156 if (u->ring_overflow)
157 goto unlock_out;
158
159 c = u->ring_cons;
160 p = u->ring_prod;
161 if (c != p)
162 break;
163
164 mutex_unlock(&u->ring_cons_mutex);
165
166 if (file->f_flags & O_NONBLOCK)
167 return -EAGAIN;
168
169 rc = wait_event_interruptible(u->evtchn_wait,
170 u->ring_cons != u->ring_prod);
171 if (rc)
172 return rc;
173 }
174
175 /* Byte lengths of two chunks. Chunk split (if any) is at ring wrap. */
176 if (((c ^ p) & EVTCHN_RING_SIZE) != 0) {
177 bytes1 = (EVTCHN_RING_SIZE - EVTCHN_RING_MASK(c)) *
178 sizeof(evtchn_port_t);
179 bytes2 = EVTCHN_RING_MASK(p) * sizeof(evtchn_port_t);
180 } else {
181 bytes1 = (p - c) * sizeof(evtchn_port_t);
182 bytes2 = 0;
183 }
184
185 /* Truncate chunks according to caller's maximum byte count. */
186 if (bytes1 > count) {
187 bytes1 = count;
188 bytes2 = 0;
189 } else if ((bytes1 + bytes2) > count) {
190 bytes2 = count - bytes1;
191 }
192
193 rc = -EFAULT;
194 rmb(); /* Ensure that we see the port before we copy it. */
195 if (copy_to_user(buf, &u->ring[EVTCHN_RING_MASK(c)], bytes1) ||
196 ((bytes2 != 0) &&
197 copy_to_user(&buf[bytes1], &u->ring[0], bytes2)))
198 goto unlock_out;
199
200 u->ring_cons += (bytes1 + bytes2) / sizeof(evtchn_port_t);
201 rc = bytes1 + bytes2;
202
203 unlock_out:
204 mutex_unlock(&u->ring_cons_mutex);
205 return rc;
206}
207
208static ssize_t evtchn_write(struct file *file, const char __user *buf,
209 size_t count, loff_t *ppos)
210{
211 int rc, i;
212 evtchn_port_t *kbuf = (evtchn_port_t *)__get_free_page(GFP_KERNEL);
213 struct per_user_data *u = file->private_data;
214
215 if (kbuf == NULL)
216 return -ENOMEM;
217
218 /* Whole number of ports. */
219 count &= ~(sizeof(evtchn_port_t)-1);
220
221 rc = 0;
222 if (count == 0)
223 goto out;
224
225 if (count > PAGE_SIZE)
226 count = PAGE_SIZE;
227
228 rc = -EFAULT;
229 if (copy_from_user(kbuf, buf, count) != 0)
230 goto out;
231
232 spin_lock_irq(&port_user_lock);
Jeremy Fitzhardingee3cc0672009-09-18 16:31:22 -0700233
234 for (i = 0; i < (count/sizeof(evtchn_port_t)); i++) {
235 unsigned port = kbuf[i];
236
237 if (port < NR_EVENT_CHANNELS &&
238 get_port_user(port) == u &&
239 !get_port_enabled(port)) {
240 set_port_enabled(port, true);
241 enable_irq(irq_from_evtchn(port));
242 }
243 }
244
Ian Campbellf7116282009-02-06 19:21:19 -0800245 spin_unlock_irq(&port_user_lock);
246
247 rc = count;
248
249 out:
250 free_page((unsigned long)kbuf);
251 return rc;
252}
253
254static int evtchn_bind_to_user(struct per_user_data *u, int port)
255{
Ian Campbellf7116282009-02-06 19:21:19 -0800256 int rc = 0;
257
Jeremy Fitzhardinge0a4666b2009-02-12 13:03:24 -0800258 /*
259 * Ports are never reused, so every caller should pass in a
260 * unique port.
261 *
262 * (Locking not necessary because we haven't registered the
263 * interrupt handler yet, and our caller has already
264 * serialized bind operations.)
265 */
Jeremy Fitzhardingee3cc0672009-09-18 16:31:22 -0700266 BUG_ON(get_port_user(port) != NULL);
267 set_port_user(port, u);
Jeremy Fitzhardinge0edce912009-09-18 16:55:29 -0700268 set_port_enabled(port, true); /* start enabled */
Ian Campbellf7116282009-02-06 19:21:19 -0800269
Jeremy Fitzhardinge0a4666b2009-02-12 13:03:24 -0800270 rc = bind_evtchn_to_irqhandler(port, evtchn_interrupt, IRQF_DISABLED,
271 u->name, (void *)(unsigned long)port);
272 if (rc >= 0)
Daniel De Graaf420eb552011-10-27 17:58:47 -0400273 rc = evtchn_make_refcounted(port);
Wei Liue7e44e42013-02-18 14:57:58 +0000274 else {
275 /* bind failed, should close the port now */
276 struct evtchn_close close;
277 close.port = port;
278 if (HYPERVISOR_event_channel_op(EVTCHNOP_close, &close) != 0)
279 BUG();
280 set_port_user(port, NULL);
281 }
Jeremy Fitzhardinge0a4666b2009-02-12 13:03:24 -0800282
Ian Campbellf7116282009-02-06 19:21:19 -0800283 return rc;
284}
285
286static void evtchn_unbind_from_user(struct per_user_data *u, int port)
287{
288 int irq = irq_from_evtchn(port);
289
Wei Liue7e44e42013-02-18 14:57:58 +0000290 BUG_ON(irq < 0);
291
Ian Campbellf7116282009-02-06 19:21:19 -0800292 unbind_from_irqhandler(irq, (void *)(unsigned long)port);
Jeremy Fitzhardinge0a4666b2009-02-12 13:03:24 -0800293
Jeremy Fitzhardingee3cc0672009-09-18 16:31:22 -0700294 set_port_user(port, NULL);
Ian Campbellf7116282009-02-06 19:21:19 -0800295}
296
297static long evtchn_ioctl(struct file *file,
298 unsigned int cmd, unsigned long arg)
299{
300 int rc;
301 struct per_user_data *u = file->private_data;
302 void __user *uarg = (void __user *) arg;
303
Jeremy Fitzhardinge0a4666b2009-02-12 13:03:24 -0800304 /* Prevent bind from racing with unbind */
305 mutex_lock(&u->bind_mutex);
306
Ian Campbellf7116282009-02-06 19:21:19 -0800307 switch (cmd) {
308 case IOCTL_EVTCHN_BIND_VIRQ: {
309 struct ioctl_evtchn_bind_virq bind;
310 struct evtchn_bind_virq bind_virq;
311
312 rc = -EFAULT;
313 if (copy_from_user(&bind, uarg, sizeof(bind)))
314 break;
315
316 bind_virq.virq = bind.virq;
317 bind_virq.vcpu = 0;
318 rc = HYPERVISOR_event_channel_op(EVTCHNOP_bind_virq,
319 &bind_virq);
320 if (rc != 0)
321 break;
322
323 rc = evtchn_bind_to_user(u, bind_virq.port);
324 if (rc == 0)
325 rc = bind_virq.port;
326 break;
327 }
328
329 case IOCTL_EVTCHN_BIND_INTERDOMAIN: {
330 struct ioctl_evtchn_bind_interdomain bind;
331 struct evtchn_bind_interdomain bind_interdomain;
332
333 rc = -EFAULT;
334 if (copy_from_user(&bind, uarg, sizeof(bind)))
335 break;
336
337 bind_interdomain.remote_dom = bind.remote_domain;
338 bind_interdomain.remote_port = bind.remote_port;
339 rc = HYPERVISOR_event_channel_op(EVTCHNOP_bind_interdomain,
340 &bind_interdomain);
341 if (rc != 0)
342 break;
343
344 rc = evtchn_bind_to_user(u, bind_interdomain.local_port);
345 if (rc == 0)
346 rc = bind_interdomain.local_port;
347 break;
348 }
349
350 case IOCTL_EVTCHN_BIND_UNBOUND_PORT: {
351 struct ioctl_evtchn_bind_unbound_port bind;
352 struct evtchn_alloc_unbound alloc_unbound;
353
354 rc = -EFAULT;
355 if (copy_from_user(&bind, uarg, sizeof(bind)))
356 break;
357
358 alloc_unbound.dom = DOMID_SELF;
359 alloc_unbound.remote_dom = bind.remote_domain;
360 rc = HYPERVISOR_event_channel_op(EVTCHNOP_alloc_unbound,
361 &alloc_unbound);
362 if (rc != 0)
363 break;
364
365 rc = evtchn_bind_to_user(u, alloc_unbound.port);
366 if (rc == 0)
367 rc = alloc_unbound.port;
368 break;
369 }
370
371 case IOCTL_EVTCHN_UNBIND: {
372 struct ioctl_evtchn_unbind unbind;
373
374 rc = -EFAULT;
375 if (copy_from_user(&unbind, uarg, sizeof(unbind)))
376 break;
377
378 rc = -EINVAL;
379 if (unbind.port >= NR_EVENT_CHANNELS)
380 break;
381
382 spin_lock_irq(&port_user_lock);
383
384 rc = -ENOTCONN;
Jeremy Fitzhardingee3cc0672009-09-18 16:31:22 -0700385 if (get_port_user(unbind.port) != u) {
Ian Campbellf7116282009-02-06 19:21:19 -0800386 spin_unlock_irq(&port_user_lock);
387 break;
388 }
389
Jeremy Fitzhardinge3f5e5542010-05-28 15:28:27 -0700390 disable_irq(irq_from_evtchn(unbind.port));
Ian Campbellf7116282009-02-06 19:21:19 -0800391
392 spin_unlock_irq(&port_user_lock);
393
Jeremy Fitzhardinge3f5e5542010-05-28 15:28:27 -0700394 evtchn_unbind_from_user(u, unbind.port);
395
Ian Campbellf7116282009-02-06 19:21:19 -0800396 rc = 0;
397 break;
398 }
399
400 case IOCTL_EVTCHN_NOTIFY: {
401 struct ioctl_evtchn_notify notify;
402
403 rc = -EFAULT;
404 if (copy_from_user(&notify, uarg, sizeof(notify)))
405 break;
406
407 if (notify.port >= NR_EVENT_CHANNELS) {
408 rc = -EINVAL;
Jeremy Fitzhardingee3cc0672009-09-18 16:31:22 -0700409 } else if (get_port_user(notify.port) != u) {
Ian Campbellf7116282009-02-06 19:21:19 -0800410 rc = -ENOTCONN;
411 } else {
412 notify_remote_via_evtchn(notify.port);
413 rc = 0;
414 }
415 break;
416 }
417
418 case IOCTL_EVTCHN_RESET: {
419 /* Initialise the ring to empty. Clear errors. */
420 mutex_lock(&u->ring_cons_mutex);
421 spin_lock_irq(&port_user_lock);
422 u->ring_cons = u->ring_prod = u->ring_overflow = 0;
423 spin_unlock_irq(&port_user_lock);
424 mutex_unlock(&u->ring_cons_mutex);
425 rc = 0;
426 break;
427 }
428
429 default:
430 rc = -ENOSYS;
431 break;
432 }
Jeremy Fitzhardinge0a4666b2009-02-12 13:03:24 -0800433 mutex_unlock(&u->bind_mutex);
Ian Campbellf7116282009-02-06 19:21:19 -0800434
435 return rc;
436}
437
438static unsigned int evtchn_poll(struct file *file, poll_table *wait)
439{
440 unsigned int mask = POLLOUT | POLLWRNORM;
441 struct per_user_data *u = file->private_data;
442
443 poll_wait(file, &u->evtchn_wait, wait);
444 if (u->ring_cons != u->ring_prod)
445 mask |= POLLIN | POLLRDNORM;
446 if (u->ring_overflow)
447 mask = POLLERR;
448 return mask;
449}
450
451static int evtchn_fasync(int fd, struct file *filp, int on)
452{
453 struct per_user_data *u = filp->private_data;
454 return fasync_helper(fd, filp, on, &u->evtchn_async_queue);
455}
456
457static int evtchn_open(struct inode *inode, struct file *filp)
458{
459 struct per_user_data *u;
460
461 u = kzalloc(sizeof(*u), GFP_KERNEL);
462 if (u == NULL)
463 return -ENOMEM;
464
465 u->name = kasprintf(GFP_KERNEL, "evtchn:%s", current->comm);
466 if (u->name == NULL) {
467 kfree(u);
468 return -ENOMEM;
469 }
470
471 init_waitqueue_head(&u->evtchn_wait);
472
473 u->ring = (evtchn_port_t *)__get_free_page(GFP_KERNEL);
474 if (u->ring == NULL) {
475 kfree(u->name);
476 kfree(u);
477 return -ENOMEM;
478 }
479
Jeremy Fitzhardinge0a4666b2009-02-12 13:03:24 -0800480 mutex_init(&u->bind_mutex);
Ian Campbellf7116282009-02-06 19:21:19 -0800481 mutex_init(&u->ring_cons_mutex);
482
483 filp->private_data = u;
484
Justin P. Mattock6eab04a2011-04-08 19:49:08 -0700485 return nonseekable_open(inode, filp);
Ian Campbellf7116282009-02-06 19:21:19 -0800486}
487
488static int evtchn_release(struct inode *inode, struct file *filp)
489{
490 int i;
491 struct per_user_data *u = filp->private_data;
492
493 spin_lock_irq(&port_user_lock);
494
495 free_page((unsigned long)u->ring);
496
497 for (i = 0; i < NR_EVENT_CHANNELS; i++) {
Jeremy Fitzhardingee3cc0672009-09-18 16:31:22 -0700498 if (get_port_user(i) != u)
Ian Campbellf7116282009-02-06 19:21:19 -0800499 continue;
500
Jeremy Fitzhardinge3f5e5542010-05-28 15:28:27 -0700501 disable_irq(irq_from_evtchn(i));
Ian Campbellf7116282009-02-06 19:21:19 -0800502 }
503
504 spin_unlock_irq(&port_user_lock);
505
Jeremy Fitzhardinge3f5e5542010-05-28 15:28:27 -0700506 for (i = 0; i < NR_EVENT_CHANNELS; i++) {
507 if (get_port_user(i) != u)
508 continue;
509
510 evtchn_unbind_from_user(get_port_user(i), i);
511 }
512
Ian Campbellf7116282009-02-06 19:21:19 -0800513 kfree(u->name);
514 kfree(u);
515
516 return 0;
517}
518
519static const struct file_operations evtchn_fops = {
520 .owner = THIS_MODULE,
521 .read = evtchn_read,
522 .write = evtchn_write,
523 .unlocked_ioctl = evtchn_ioctl,
524 .poll = evtchn_poll,
525 .fasync = evtchn_fasync,
526 .open = evtchn_open,
527 .release = evtchn_release,
Jeremy Fitzhardingebc7fc5e2010-11-18 22:32:17 -0800528 .llseek = no_llseek,
Ian Campbellf7116282009-02-06 19:21:19 -0800529};
530
531static struct miscdevice evtchn_miscdev = {
532 .minor = MISC_DYNAMIC_MINOR,
Bastian Blank376d9082010-05-28 15:43:49 -0700533 .name = "xen/evtchn",
Ian Campbellf7116282009-02-06 19:21:19 -0800534 .fops = &evtchn_fops,
535};
536static int __init evtchn_init(void)
537{
538 int err;
539
540 if (!xen_domain())
541 return -ENODEV;
542
Jeremy Fitzhardinge93afe0b2009-09-18 16:36:58 -0700543 port_user = kcalloc(NR_EVENT_CHANNELS, sizeof(*port_user), GFP_KERNEL);
544 if (port_user == NULL)
545 return -ENOMEM;
546
Ian Campbellf7116282009-02-06 19:21:19 -0800547 spin_lock_init(&port_user_lock);
Ian Campbellf7116282009-02-06 19:21:19 -0800548
Wei Liu18283ea2013-02-18 14:57:59 +0000549 /* Create '/dev/xen/evtchn'. */
Ian Campbellf7116282009-02-06 19:21:19 -0800550 err = misc_register(&evtchn_miscdev);
551 if (err != 0) {
Joe Perches283c0972013-06-28 03:21:41 -0700552 pr_err("Could not register /dev/xen/evtchn\n");
Ian Campbellf7116282009-02-06 19:21:19 -0800553 return err;
554 }
555
Joe Perches283c0972013-06-28 03:21:41 -0700556 pr_info("Event-channel device installed\n");
Ian Campbellf7116282009-02-06 19:21:19 -0800557
558 return 0;
559}
560
561static void __exit evtchn_cleanup(void)
562{
Jeremy Fitzhardinge93afe0b2009-09-18 16:36:58 -0700563 kfree(port_user);
564 port_user = NULL;
565
Ian Campbellf7116282009-02-06 19:21:19 -0800566 misc_deregister(&evtchn_miscdev);
567}
568
569module_init(evtchn_init);
570module_exit(evtchn_cleanup);
571
572MODULE_LICENSE("GPL");