1 /******************************************************************************
4 * Driver for receiving and demuxing event-channel signals.
6 * Copyright (c) 2004-2005, K A Fraser
7 * Multi-process extensions Copyright (c) 2004, Steven Smith
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:
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:
22 * The above copyright notice and this permission notice shall be included in
23 * all copies or substantial portions of the Software.
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
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>
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/gfp.h>
49 #include <linux/mutex.h>
50 #include <linux/cpu.h>
53 #include <xen/events.h>
54 #include <xen/evtchn.h>
55 #include <asm/xen/hypervisor.h>
57 struct per_user_data
{
58 struct mutex bind_mutex
; /* serialize bind/unbind operations */
60 /* 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))
64 unsigned int ring_cons
, ring_prod
, ring_overflow
;
65 struct mutex ring_cons_mutex
; /* protect against concurrent readers */
67 /* Processes wait on this queue when ring is empty. */
68 wait_queue_head_t evtchn_wait
;
69 struct fasync_struct
*evtchn_async_queue
;
73 /* Who's bound to each port? */
74 static struct per_user_data
*port_user
[NR_EVENT_CHANNELS
];
75 static DEFINE_SPINLOCK(port_user_lock
); /* protects port_user[] and ring_prod */
77 irqreturn_t
evtchn_interrupt(int irq
, void *data
)
79 unsigned int port
= (unsigned long)data
;
80 struct per_user_data
*u
;
82 spin_lock(&port_user_lock
);
86 disable_irq_nosync(irq
);
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
,
100 spin_unlock(&port_user_lock
);
105 static ssize_t
evtchn_read(struct file
*file
, char __user
*buf
,
106 size_t count
, loff_t
*ppos
)
109 unsigned int c
, p
, bytes1
= 0, bytes2
= 0;
110 struct per_user_data
*u
= file
->private_data
;
112 /* Whole number of ports. */
113 count
&= ~(sizeof(evtchn_port_t
)-1);
118 if (count
> PAGE_SIZE
)
122 mutex_lock(&u
->ring_cons_mutex
);
125 if (u
->ring_overflow
)
133 mutex_unlock(&u
->ring_cons_mutex
);
135 if (file
->f_flags
& O_NONBLOCK
)
138 rc
= wait_event_interruptible(u
->evtchn_wait
,
139 u
->ring_cons
!= u
->ring_prod
);
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
);
150 bytes1
= (p
- c
) * sizeof(evtchn_port_t
);
154 /* Truncate chunks according to caller's maximum byte count. */
155 if (bytes1
> count
) {
158 } else if ((bytes1
+ bytes2
) > count
) {
159 bytes2
= count
- bytes1
;
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
) ||
166 copy_to_user(&buf
[bytes1
], &u
->ring
[0], bytes2
)))
169 u
->ring_cons
+= (bytes1
+ bytes2
) / sizeof(evtchn_port_t
);
170 rc
= bytes1
+ bytes2
;
173 mutex_unlock(&u
->ring_cons_mutex
);
177 static ssize_t
evtchn_write(struct file
*file
, const char __user
*buf
,
178 size_t count
, loff_t
*ppos
)
181 evtchn_port_t
*kbuf
= (evtchn_port_t
*)__get_free_page(GFP_KERNEL
);
182 struct per_user_data
*u
= file
->private_data
;
187 /* Whole number of ports. */
188 count
&= ~(sizeof(evtchn_port_t
)-1);
194 if (count
> PAGE_SIZE
)
198 if (copy_from_user(kbuf
, buf
, count
) != 0)
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
);
210 free_page((unsigned long)kbuf
);
214 static int evtchn_bind_to_user(struct per_user_data
*u
, int port
)
219 * Ports are never reused, so every caller should pass in a
222 * (Locking not necessary because we haven't registered the
223 * interrupt handler yet, and our caller has already
224 * serialized bind operations.)
226 BUG_ON(port_user
[port
] != NULL
);
229 rc
= bind_evtchn_to_irqhandler(port
, evtchn_interrupt
, IRQF_DISABLED
,
230 u
->name
, (void *)(unsigned long)port
);
237 static void evtchn_unbind_from_user(struct per_user_data
*u
, int port
)
239 int irq
= irq_from_evtchn(port
);
241 unbind_from_irqhandler(irq
, (void *)(unsigned long)port
);
243 /* make sure we unbind the irq handler before clearing the port */
246 port_user
[port
] = NULL
;
249 static long evtchn_ioctl(struct file
*file
,
250 unsigned int cmd
, unsigned long arg
)
253 struct per_user_data
*u
= file
->private_data
;
254 void __user
*uarg
= (void __user
*) arg
;
256 /* Prevent bind from racing with unbind */
257 mutex_lock(&u
->bind_mutex
);
260 case IOCTL_EVTCHN_BIND_VIRQ
: {
261 struct ioctl_evtchn_bind_virq bind
;
262 struct evtchn_bind_virq bind_virq
;
265 if (copy_from_user(&bind
, uarg
, sizeof(bind
)))
268 bind_virq
.virq
= bind
.virq
;
270 rc
= HYPERVISOR_event_channel_op(EVTCHNOP_bind_virq
,
275 rc
= evtchn_bind_to_user(u
, bind_virq
.port
);
281 case IOCTL_EVTCHN_BIND_INTERDOMAIN
: {
282 struct ioctl_evtchn_bind_interdomain bind
;
283 struct evtchn_bind_interdomain bind_interdomain
;
286 if (copy_from_user(&bind
, uarg
, sizeof(bind
)))
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
,
296 rc
= evtchn_bind_to_user(u
, bind_interdomain
.local_port
);
298 rc
= bind_interdomain
.local_port
;
302 case IOCTL_EVTCHN_BIND_UNBOUND_PORT
: {
303 struct ioctl_evtchn_bind_unbound_port bind
;
304 struct evtchn_alloc_unbound alloc_unbound
;
307 if (copy_from_user(&bind
, uarg
, sizeof(bind
)))
310 alloc_unbound
.dom
= DOMID_SELF
;
311 alloc_unbound
.remote_dom
= bind
.remote_domain
;
312 rc
= HYPERVISOR_event_channel_op(EVTCHNOP_alloc_unbound
,
317 rc
= evtchn_bind_to_user(u
, alloc_unbound
.port
);
319 rc
= alloc_unbound
.port
;
323 case IOCTL_EVTCHN_UNBIND
: {
324 struct ioctl_evtchn_unbind unbind
;
327 if (copy_from_user(&unbind
, uarg
, sizeof(unbind
)))
331 if (unbind
.port
>= NR_EVENT_CHANNELS
)
334 spin_lock_irq(&port_user_lock
);
337 if (port_user
[unbind
.port
] != u
) {
338 spin_unlock_irq(&port_user_lock
);
342 evtchn_unbind_from_user(u
, unbind
.port
);
344 spin_unlock_irq(&port_user_lock
);
350 case IOCTL_EVTCHN_NOTIFY
: {
351 struct ioctl_evtchn_notify notify
;
354 if (copy_from_user(¬ify
, uarg
, sizeof(notify
)))
357 if (notify
.port
>= NR_EVENT_CHANNELS
) {
359 } else if (port_user
[notify
.port
] != u
) {
362 notify_remote_via_evtchn(notify
.port
);
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
);
383 mutex_unlock(&u
->bind_mutex
);
388 static unsigned int evtchn_poll(struct file
*file
, poll_table
*wait
)
390 unsigned int mask
= POLLOUT
| POLLWRNORM
;
391 struct per_user_data
*u
= file
->private_data
;
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
)
401 static int evtchn_fasync(int fd
, struct file
*filp
, int on
)
403 struct per_user_data
*u
= filp
->private_data
;
404 return fasync_helper(fd
, filp
, on
, &u
->evtchn_async_queue
);
407 static int evtchn_open(struct inode
*inode
, struct file
*filp
)
409 struct per_user_data
*u
;
411 u
= kzalloc(sizeof(*u
), GFP_KERNEL
);
415 u
->name
= kasprintf(GFP_KERNEL
, "evtchn:%s", current
->comm
);
416 if (u
->name
== NULL
) {
421 init_waitqueue_head(&u
->evtchn_wait
);
423 u
->ring
= (evtchn_port_t
*)__get_free_page(GFP_KERNEL
);
424 if (u
->ring
== NULL
) {
430 mutex_init(&u
->bind_mutex
);
431 mutex_init(&u
->ring_cons_mutex
);
433 filp
->private_data
= u
;
438 static int evtchn_release(struct inode
*inode
, struct file
*filp
)
441 struct per_user_data
*u
= filp
->private_data
;
443 spin_lock_irq(&port_user_lock
);
445 free_page((unsigned long)u
->ring
);
447 for (i
= 0; i
< NR_EVENT_CHANNELS
; i
++) {
448 if (port_user
[i
] != u
)
451 evtchn_unbind_from_user(port_user
[i
], i
);
454 spin_unlock_irq(&port_user_lock
);
462 static const struct file_operations evtchn_fops
= {
463 .owner
= THIS_MODULE
,
465 .write
= evtchn_write
,
466 .unlocked_ioctl
= evtchn_ioctl
,
468 .fasync
= evtchn_fasync
,
470 .release
= evtchn_release
,
473 static struct miscdevice evtchn_miscdev
= {
474 .minor
= MISC_DYNAMIC_MINOR
,
476 .fops
= &evtchn_fops
,
478 static int __init
evtchn_init(void)
485 spin_lock_init(&port_user_lock
);
486 memset(port_user
, 0, sizeof(port_user
));
488 /* Create '/dev/misc/evtchn'. */
489 err
= misc_register(&evtchn_miscdev
);
491 printk(KERN_ALERT
"Could not register /dev/misc/evtchn\n");
495 printk(KERN_INFO
"Event-channel device installed.\n");
500 static void __exit
evtchn_cleanup(void)
502 misc_deregister(&evtchn_miscdev
);
505 module_init(evtchn_init
);
506 module_exit(evtchn_cleanup
);
508 MODULE_LICENSE("GPL");