blob: f70a4f4698c5994348a14dd216bb91ad72ec5b6a [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
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>
Ian Campbellf7116282009-02-06 19:21:19 -080041#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/gfp.h>
49#include <linux/mutex.h>
50#include <linux/cpu.h>
Jeremy Fitzhardinge1ccbf532009-10-06 15:11:14 -070051
52#include <xen/xen.h>
Ian Campbellf7116282009-02-06 19:21:19 -080053#include <xen/events.h>
54#include <xen/evtchn.h>
55#include <asm/xen/hypervisor.h>
56
57struct per_user_data {
Jeremy Fitzhardinge0a4666b2009-02-12 13:03:24 -080058 struct mutex bind_mutex; /* serialize bind/unbind operations */
59
Ian Campbellf7116282009-02-06 19:21:19 -080060 /* Notification ring, accessed via /dev/xen/evtchn. */
61#define EVTCHN_RING_SIZE (PAGE_SIZE / sizeof(evtchn_port_t))
62#define EVTCHN_RING_MASK(_i) ((_i)&(EVTCHN_RING_SIZE-1))
63 evtchn_port_t *ring;
64 unsigned int ring_cons, ring_prod, ring_overflow;
65 struct mutex ring_cons_mutex; /* protect against concurrent readers */
66
67 /* Processes wait on this queue when ring is empty. */
68 wait_queue_head_t evtchn_wait;
69 struct fasync_struct *evtchn_async_queue;
70 const char *name;
71};
72
73/* Who's bound to each port? */
74static struct per_user_data *port_user[NR_EVENT_CHANNELS];
Jeremy Fitzhardinge0a4666b2009-02-12 13:03:24 -080075static DEFINE_SPINLOCK(port_user_lock); /* protects port_user[] and ring_prod */
Ian Campbellf7116282009-02-06 19:21:19 -080076
77irqreturn_t evtchn_interrupt(int irq, void *data)
78{
79 unsigned int port = (unsigned long)data;
80 struct per_user_data *u;
81
82 spin_lock(&port_user_lock);
83
84 u = port_user[port];
85
86 disable_irq_nosync(irq);
87
88 if ((u->ring_prod - u->ring_cons) < EVTCHN_RING_SIZE) {
89 u->ring[EVTCHN_RING_MASK(u->ring_prod)] = port;
90 wmb(); /* Ensure ring contents visible */
91 if (u->ring_cons == u->ring_prod++) {
92 wake_up_interruptible(&u->evtchn_wait);
93 kill_fasync(&u->evtchn_async_queue,
94 SIGIO, POLL_IN);
95 }
96 } else {
97 u->ring_overflow = 1;
98 }
99
100 spin_unlock(&port_user_lock);
101
102 return IRQ_HANDLED;
103}
104
105static ssize_t evtchn_read(struct file *file, char __user *buf,
106 size_t count, loff_t *ppos)
107{
108 int rc;
109 unsigned int c, p, bytes1 = 0, bytes2 = 0;
110 struct per_user_data *u = file->private_data;
111
112 /* Whole number of ports. */
113 count &= ~(sizeof(evtchn_port_t)-1);
114
115 if (count == 0)
116 return 0;
117
118 if (count > PAGE_SIZE)
119 count = PAGE_SIZE;
120
121 for (;;) {
122 mutex_lock(&u->ring_cons_mutex);
123
124 rc = -EFBIG;
125 if (u->ring_overflow)
126 goto unlock_out;
127
128 c = u->ring_cons;
129 p = u->ring_prod;
130 if (c != p)
131 break;
132
133 mutex_unlock(&u->ring_cons_mutex);
134
135 if (file->f_flags & O_NONBLOCK)
136 return -EAGAIN;
137
138 rc = wait_event_interruptible(u->evtchn_wait,
139 u->ring_cons != u->ring_prod);
140 if (rc)
141 return rc;
142 }
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
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
162 rc = -EFAULT;
163 rmb(); /* Ensure that we see the port before we copy it. */
164 if (copy_to_user(buf, &u->ring[EVTCHN_RING_MASK(c)], bytes1) ||
165 ((bytes2 != 0) &&
166 copy_to_user(&buf[bytes1], &u->ring[0], bytes2)))
167 goto unlock_out;
168
169 u->ring_cons += (bytes1 + bytes2) / sizeof(evtchn_port_t);
170 rc = bytes1 + bytes2;
171
172 unlock_out:
173 mutex_unlock(&u->ring_cons_mutex);
174 return rc;
175}
176
177static ssize_t evtchn_write(struct file *file, const char __user *buf,
178 size_t count, loff_t *ppos)
179{
180 int rc, i;
181 evtchn_port_t *kbuf = (evtchn_port_t *)__get_free_page(GFP_KERNEL);
182 struct per_user_data *u = file->private_data;
183
184 if (kbuf == NULL)
185 return -ENOMEM;
186
187 /* Whole number of ports. */
188 count &= ~(sizeof(evtchn_port_t)-1);
189
190 rc = 0;
191 if (count == 0)
192 goto out;
193
194 if (count > PAGE_SIZE)
195 count = PAGE_SIZE;
196
197 rc = -EFAULT;
198 if (copy_from_user(kbuf, buf, count) != 0)
199 goto out;
200
201 spin_lock_irq(&port_user_lock);
202 for (i = 0; i < (count/sizeof(evtchn_port_t)); i++)
203 if ((kbuf[i] < NR_EVENT_CHANNELS) && (port_user[kbuf[i]] == u))
204 enable_irq(irq_from_evtchn(kbuf[i]));
205 spin_unlock_irq(&port_user_lock);
206
207 rc = count;
208
209 out:
210 free_page((unsigned long)kbuf);
211 return rc;
212}
213
214static int evtchn_bind_to_user(struct per_user_data *u, int port)
215{
Ian Campbellf7116282009-02-06 19:21:19 -0800216 int rc = 0;
217
Jeremy Fitzhardinge0a4666b2009-02-12 13:03:24 -0800218 /*
219 * Ports are never reused, so every caller should pass in a
220 * unique port.
221 *
222 * (Locking not necessary because we haven't registered the
223 * interrupt handler yet, and our caller has already
224 * serialized bind operations.)
225 */
Ian Campbellf7116282009-02-06 19:21:19 -0800226 BUG_ON(port_user[port] != NULL);
Ian Campbellf7116282009-02-06 19:21:19 -0800227 port_user[port] = u;
228
Jeremy Fitzhardinge0a4666b2009-02-12 13:03:24 -0800229 rc = bind_evtchn_to_irqhandler(port, evtchn_interrupt, IRQF_DISABLED,
230 u->name, (void *)(unsigned long)port);
231 if (rc >= 0)
232 rc = 0;
233
Ian Campbellf7116282009-02-06 19:21:19 -0800234 return rc;
235}
236
237static void evtchn_unbind_from_user(struct per_user_data *u, int port)
238{
239 int irq = irq_from_evtchn(port);
240
241 unbind_from_irqhandler(irq, (void *)(unsigned long)port);
Jeremy Fitzhardinge0a4666b2009-02-12 13:03:24 -0800242
243 /* make sure we unbind the irq handler before clearing the port */
244 barrier();
245
Ian Campbellf7116282009-02-06 19:21:19 -0800246 port_user[port] = NULL;
247}
248
249static long evtchn_ioctl(struct file *file,
250 unsigned int cmd, unsigned long arg)
251{
252 int rc;
253 struct per_user_data *u = file->private_data;
254 void __user *uarg = (void __user *) arg;
255
Jeremy Fitzhardinge0a4666b2009-02-12 13:03:24 -0800256 /* Prevent bind from racing with unbind */
257 mutex_lock(&u->bind_mutex);
258
Ian Campbellf7116282009-02-06 19:21:19 -0800259 switch (cmd) {
260 case IOCTL_EVTCHN_BIND_VIRQ: {
261 struct ioctl_evtchn_bind_virq bind;
262 struct evtchn_bind_virq bind_virq;
263
264 rc = -EFAULT;
265 if (copy_from_user(&bind, uarg, sizeof(bind)))
266 break;
267
268 bind_virq.virq = bind.virq;
269 bind_virq.vcpu = 0;
270 rc = HYPERVISOR_event_channel_op(EVTCHNOP_bind_virq,
271 &bind_virq);
272 if (rc != 0)
273 break;
274
275 rc = evtchn_bind_to_user(u, bind_virq.port);
276 if (rc == 0)
277 rc = bind_virq.port;
278 break;
279 }
280
281 case IOCTL_EVTCHN_BIND_INTERDOMAIN: {
282 struct ioctl_evtchn_bind_interdomain bind;
283 struct evtchn_bind_interdomain bind_interdomain;
284
285 rc = -EFAULT;
286 if (copy_from_user(&bind, uarg, sizeof(bind)))
287 break;
288
289 bind_interdomain.remote_dom = bind.remote_domain;
290 bind_interdomain.remote_port = bind.remote_port;
291 rc = HYPERVISOR_event_channel_op(EVTCHNOP_bind_interdomain,
292 &bind_interdomain);
293 if (rc != 0)
294 break;
295
296 rc = evtchn_bind_to_user(u, bind_interdomain.local_port);
297 if (rc == 0)
298 rc = bind_interdomain.local_port;
299 break;
300 }
301
302 case IOCTL_EVTCHN_BIND_UNBOUND_PORT: {
303 struct ioctl_evtchn_bind_unbound_port bind;
304 struct evtchn_alloc_unbound alloc_unbound;
305
306 rc = -EFAULT;
307 if (copy_from_user(&bind, uarg, sizeof(bind)))
308 break;
309
310 alloc_unbound.dom = DOMID_SELF;
311 alloc_unbound.remote_dom = bind.remote_domain;
312 rc = HYPERVISOR_event_channel_op(EVTCHNOP_alloc_unbound,
313 &alloc_unbound);
314 if (rc != 0)
315 break;
316
317 rc = evtchn_bind_to_user(u, alloc_unbound.port);
318 if (rc == 0)
319 rc = alloc_unbound.port;
320 break;
321 }
322
323 case IOCTL_EVTCHN_UNBIND: {
324 struct ioctl_evtchn_unbind unbind;
325
326 rc = -EFAULT;
327 if (copy_from_user(&unbind, uarg, sizeof(unbind)))
328 break;
329
330 rc = -EINVAL;
331 if (unbind.port >= NR_EVENT_CHANNELS)
332 break;
333
334 spin_lock_irq(&port_user_lock);
335
336 rc = -ENOTCONN;
337 if (port_user[unbind.port] != u) {
338 spin_unlock_irq(&port_user_lock);
339 break;
340 }
341
342 evtchn_unbind_from_user(u, unbind.port);
343
344 spin_unlock_irq(&port_user_lock);
345
346 rc = 0;
347 break;
348 }
349
350 case IOCTL_EVTCHN_NOTIFY: {
351 struct ioctl_evtchn_notify notify;
352
353 rc = -EFAULT;
354 if (copy_from_user(&notify, uarg, sizeof(notify)))
355 break;
356
357 if (notify.port >= NR_EVENT_CHANNELS) {
358 rc = -EINVAL;
359 } else if (port_user[notify.port] != u) {
360 rc = -ENOTCONN;
361 } else {
362 notify_remote_via_evtchn(notify.port);
363 rc = 0;
364 }
365 break;
366 }
367
368 case IOCTL_EVTCHN_RESET: {
369 /* Initialise the ring to empty. Clear errors. */
370 mutex_lock(&u->ring_cons_mutex);
371 spin_lock_irq(&port_user_lock);
372 u->ring_cons = u->ring_prod = u->ring_overflow = 0;
373 spin_unlock_irq(&port_user_lock);
374 mutex_unlock(&u->ring_cons_mutex);
375 rc = 0;
376 break;
377 }
378
379 default:
380 rc = -ENOSYS;
381 break;
382 }
Jeremy Fitzhardinge0a4666b2009-02-12 13:03:24 -0800383 mutex_unlock(&u->bind_mutex);
Ian Campbellf7116282009-02-06 19:21:19 -0800384
385 return rc;
386}
387
388static unsigned int evtchn_poll(struct file *file, poll_table *wait)
389{
390 unsigned int mask = POLLOUT | POLLWRNORM;
391 struct per_user_data *u = file->private_data;
392
393 poll_wait(file, &u->evtchn_wait, wait);
394 if (u->ring_cons != u->ring_prod)
395 mask |= POLLIN | POLLRDNORM;
396 if (u->ring_overflow)
397 mask = POLLERR;
398 return mask;
399}
400
401static int evtchn_fasync(int fd, struct file *filp, int on)
402{
403 struct per_user_data *u = filp->private_data;
404 return fasync_helper(fd, filp, on, &u->evtchn_async_queue);
405}
406
407static int evtchn_open(struct inode *inode, struct file *filp)
408{
409 struct per_user_data *u;
410
411 u = kzalloc(sizeof(*u), GFP_KERNEL);
412 if (u == NULL)
413 return -ENOMEM;
414
415 u->name = kasprintf(GFP_KERNEL, "evtchn:%s", current->comm);
416 if (u->name == NULL) {
417 kfree(u);
418 return -ENOMEM;
419 }
420
421 init_waitqueue_head(&u->evtchn_wait);
422
423 u->ring = (evtchn_port_t *)__get_free_page(GFP_KERNEL);
424 if (u->ring == NULL) {
425 kfree(u->name);
426 kfree(u);
427 return -ENOMEM;
428 }
429
Jeremy Fitzhardinge0a4666b2009-02-12 13:03:24 -0800430 mutex_init(&u->bind_mutex);
Ian Campbellf7116282009-02-06 19:21:19 -0800431 mutex_init(&u->ring_cons_mutex);
432
433 filp->private_data = u;
434
435 return 0;
436}
437
438static int evtchn_release(struct inode *inode, struct file *filp)
439{
440 int i;
441 struct per_user_data *u = filp->private_data;
442
443 spin_lock_irq(&port_user_lock);
444
445 free_page((unsigned long)u->ring);
446
447 for (i = 0; i < NR_EVENT_CHANNELS; i++) {
448 if (port_user[i] != u)
449 continue;
450
451 evtchn_unbind_from_user(port_user[i], i);
452 }
453
454 spin_unlock_irq(&port_user_lock);
455
456 kfree(u->name);
457 kfree(u);
458
459 return 0;
460}
461
462static const struct file_operations evtchn_fops = {
463 .owner = THIS_MODULE,
464 .read = evtchn_read,
465 .write = evtchn_write,
466 .unlocked_ioctl = evtchn_ioctl,
467 .poll = evtchn_poll,
468 .fasync = evtchn_fasync,
469 .open = evtchn_open,
470 .release = evtchn_release,
471};
472
473static struct miscdevice evtchn_miscdev = {
474 .minor = MISC_DYNAMIC_MINOR,
475 .name = "evtchn",
476 .fops = &evtchn_fops,
477};
478static int __init evtchn_init(void)
479{
480 int err;
481
482 if (!xen_domain())
483 return -ENODEV;
484
485 spin_lock_init(&port_user_lock);
486 memset(port_user, 0, sizeof(port_user));
487
488 /* Create '/dev/misc/evtchn'. */
489 err = misc_register(&evtchn_miscdev);
490 if (err != 0) {
491 printk(KERN_ALERT "Could not register /dev/misc/evtchn\n");
492 return err;
493 }
494
495 printk(KERN_INFO "Event-channel device installed.\n");
496
497 return 0;
498}
499
500static void __exit evtchn_cleanup(void)
501{
502 misc_deregister(&evtchn_miscdev);
503}
504
505module_init(evtchn_init);
506module_exit(evtchn_cleanup);
507
508MODULE_LICENSE("GPL");