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 #define pr_fmt(fmt) "xen:" KBUILD_MODNAME ": " fmt
36 #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>
43 #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>
50 #include <linux/mutex.h>
51 #include <linux/cpu.h>
54 #include <xen/events.h>
55 #include <xen/evtchn.h>
56 #include <asm/xen/hypervisor.h>
58 struct per_user_data
{
59 struct mutex bind_mutex
; /* serialize bind/unbind operations */
60 struct rb_root evtchns
;
62 /* Notification ring, accessed via /dev/xen/evtchn. */
63 #define EVTCHN_RING_SIZE (PAGE_SIZE / sizeof(evtchn_port_t))
64 #define EVTCHN_RING_MASK(_i) ((_i)&(EVTCHN_RING_SIZE-1))
66 unsigned int ring_cons
, ring_prod
, ring_overflow
;
67 struct mutex ring_cons_mutex
; /* protect against concurrent readers */
68 spinlock_t ring_prod_lock
; /* product against concurrent interrupts */
70 /* Processes wait on this queue when ring is empty. */
71 wait_queue_head_t evtchn_wait
;
72 struct fasync_struct
*evtchn_async_queue
;
78 struct per_user_data
*user
;
83 static int add_evtchn(struct per_user_data
*u
, struct user_evtchn
*evtchn
)
85 struct rb_node
**new = &(u
->evtchns
.rb_node
), *parent
= NULL
;
88 struct user_evtchn
*this;
90 this = container_of(*new, struct user_evtchn
, node
);
93 if (this->port
< evtchn
->port
)
94 new = &((*new)->rb_left
);
95 else if (this->port
> evtchn
->port
)
96 new = &((*new)->rb_right
);
101 /* Add new node and rebalance tree. */
102 rb_link_node(&evtchn
->node
, parent
, new);
103 rb_insert_color(&evtchn
->node
, &u
->evtchns
);
108 static void del_evtchn(struct per_user_data
*u
, struct user_evtchn
*evtchn
)
110 rb_erase(&evtchn
->node
, &u
->evtchns
);
114 static struct user_evtchn
*find_evtchn(struct per_user_data
*u
, unsigned port
)
116 struct rb_node
*node
= u
->evtchns
.rb_node
;
119 struct user_evtchn
*evtchn
;
121 evtchn
= container_of(node
, struct user_evtchn
, node
);
123 if (evtchn
->port
< port
)
124 node
= node
->rb_left
;
125 else if (evtchn
->port
> port
)
126 node
= node
->rb_right
;
133 static irqreturn_t
evtchn_interrupt(int irq
, void *data
)
135 struct user_evtchn
*evtchn
= data
;
136 struct per_user_data
*u
= evtchn
->user
;
138 WARN(!evtchn
->enabled
,
139 "Interrupt for port %d, but apparently not enabled; per-user %p\n",
142 disable_irq_nosync(irq
);
143 evtchn
->enabled
= false;
145 spin_lock(&u
->ring_prod_lock
);
147 if ((u
->ring_prod
- u
->ring_cons
) < EVTCHN_RING_SIZE
) {
148 u
->ring
[EVTCHN_RING_MASK(u
->ring_prod
)] = evtchn
->port
;
149 wmb(); /* Ensure ring contents visible */
150 if (u
->ring_cons
== u
->ring_prod
++) {
151 wake_up_interruptible(&u
->evtchn_wait
);
152 kill_fasync(&u
->evtchn_async_queue
,
156 u
->ring_overflow
= 1;
158 spin_unlock(&u
->ring_prod_lock
);
163 static ssize_t
evtchn_read(struct file
*file
, char __user
*buf
,
164 size_t count
, loff_t
*ppos
)
167 unsigned int c
, p
, bytes1
= 0, bytes2
= 0;
168 struct per_user_data
*u
= file
->private_data
;
170 /* Whole number of ports. */
171 count
&= ~(sizeof(evtchn_port_t
)-1);
176 if (count
> PAGE_SIZE
)
180 mutex_lock(&u
->ring_cons_mutex
);
183 if (u
->ring_overflow
)
191 mutex_unlock(&u
->ring_cons_mutex
);
193 if (file
->f_flags
& O_NONBLOCK
)
196 rc
= wait_event_interruptible(u
->evtchn_wait
,
197 u
->ring_cons
!= u
->ring_prod
);
202 /* Byte lengths of two chunks. Chunk split (if any) is at ring wrap. */
203 if (((c
^ p
) & EVTCHN_RING_SIZE
) != 0) {
204 bytes1
= (EVTCHN_RING_SIZE
- EVTCHN_RING_MASK(c
)) *
205 sizeof(evtchn_port_t
);
206 bytes2
= EVTCHN_RING_MASK(p
) * sizeof(evtchn_port_t
);
208 bytes1
= (p
- c
) * sizeof(evtchn_port_t
);
212 /* Truncate chunks according to caller's maximum byte count. */
213 if (bytes1
> count
) {
216 } else if ((bytes1
+ bytes2
) > count
) {
217 bytes2
= count
- bytes1
;
221 rmb(); /* Ensure that we see the port before we copy it. */
222 if (copy_to_user(buf
, &u
->ring
[EVTCHN_RING_MASK(c
)], bytes1
) ||
224 copy_to_user(&buf
[bytes1
], &u
->ring
[0], bytes2
)))
227 u
->ring_cons
+= (bytes1
+ bytes2
) / sizeof(evtchn_port_t
);
228 rc
= bytes1
+ bytes2
;
231 mutex_unlock(&u
->ring_cons_mutex
);
235 static ssize_t
evtchn_write(struct file
*file
, const char __user
*buf
,
236 size_t count
, loff_t
*ppos
)
239 evtchn_port_t
*kbuf
= (evtchn_port_t
*)__get_free_page(GFP_KERNEL
);
240 struct per_user_data
*u
= file
->private_data
;
245 /* Whole number of ports. */
246 count
&= ~(sizeof(evtchn_port_t
)-1);
252 if (count
> PAGE_SIZE
)
256 if (copy_from_user(kbuf
, buf
, count
) != 0)
259 mutex_lock(&u
->bind_mutex
);
261 for (i
= 0; i
< (count
/sizeof(evtchn_port_t
)); i
++) {
262 unsigned port
= kbuf
[i
];
263 struct user_evtchn
*evtchn
;
265 evtchn
= find_evtchn(u
, port
);
266 if (evtchn
&& !evtchn
->enabled
) {
267 evtchn
->enabled
= true;
268 enable_irq(irq_from_evtchn(port
));
272 mutex_unlock(&u
->bind_mutex
);
277 free_page((unsigned long)kbuf
);
281 static int evtchn_bind_to_user(struct per_user_data
*u
, int port
)
283 struct user_evtchn
*evtchn
;
284 struct evtchn_close close
;
288 * Ports are never reused, so every caller should pass in a
291 * (Locking not necessary because we haven't registered the
292 * interrupt handler yet, and our caller has already
293 * serialized bind operations.)
296 evtchn
= kzalloc(sizeof(*evtchn
), GFP_KERNEL
);
302 evtchn
->enabled
= true; /* start enabled */
304 rc
= add_evtchn(u
, evtchn
);
308 rc
= bind_evtchn_to_irqhandler(port
, evtchn_interrupt
, 0,
313 rc
= evtchn_make_refcounted(port
);
317 /* bind failed, should close the port now */
319 if (HYPERVISOR_event_channel_op(EVTCHNOP_close
, &close
) != 0)
321 del_evtchn(u
, evtchn
);
325 static void evtchn_unbind_from_user(struct per_user_data
*u
,
326 struct user_evtchn
*evtchn
)
328 int irq
= irq_from_evtchn(evtchn
->port
);
332 unbind_from_irqhandler(irq
, evtchn
);
334 del_evtchn(u
, evtchn
);
337 static long evtchn_ioctl(struct file
*file
,
338 unsigned int cmd
, unsigned long arg
)
341 struct per_user_data
*u
= file
->private_data
;
342 void __user
*uarg
= (void __user
*) arg
;
344 /* Prevent bind from racing with unbind */
345 mutex_lock(&u
->bind_mutex
);
348 case IOCTL_EVTCHN_BIND_VIRQ
: {
349 struct ioctl_evtchn_bind_virq bind
;
350 struct evtchn_bind_virq bind_virq
;
353 if (copy_from_user(&bind
, uarg
, sizeof(bind
)))
356 bind_virq
.virq
= bind
.virq
;
358 rc
= HYPERVISOR_event_channel_op(EVTCHNOP_bind_virq
,
363 rc
= evtchn_bind_to_user(u
, bind_virq
.port
);
369 case IOCTL_EVTCHN_BIND_INTERDOMAIN
: {
370 struct ioctl_evtchn_bind_interdomain bind
;
371 struct evtchn_bind_interdomain bind_interdomain
;
374 if (copy_from_user(&bind
, uarg
, sizeof(bind
)))
377 bind_interdomain
.remote_dom
= bind
.remote_domain
;
378 bind_interdomain
.remote_port
= bind
.remote_port
;
379 rc
= HYPERVISOR_event_channel_op(EVTCHNOP_bind_interdomain
,
384 rc
= evtchn_bind_to_user(u
, bind_interdomain
.local_port
);
386 rc
= bind_interdomain
.local_port
;
390 case IOCTL_EVTCHN_BIND_UNBOUND_PORT
: {
391 struct ioctl_evtchn_bind_unbound_port bind
;
392 struct evtchn_alloc_unbound alloc_unbound
;
395 if (copy_from_user(&bind
, uarg
, sizeof(bind
)))
398 alloc_unbound
.dom
= DOMID_SELF
;
399 alloc_unbound
.remote_dom
= bind
.remote_domain
;
400 rc
= HYPERVISOR_event_channel_op(EVTCHNOP_alloc_unbound
,
405 rc
= evtchn_bind_to_user(u
, alloc_unbound
.port
);
407 rc
= alloc_unbound
.port
;
411 case IOCTL_EVTCHN_UNBIND
: {
412 struct ioctl_evtchn_unbind unbind
;
413 struct user_evtchn
*evtchn
;
416 if (copy_from_user(&unbind
, uarg
, sizeof(unbind
)))
420 if (unbind
.port
>= xen_evtchn_nr_channels())
424 evtchn
= find_evtchn(u
, unbind
.port
);
428 disable_irq(irq_from_evtchn(unbind
.port
));
429 evtchn_unbind_from_user(u
, evtchn
);
434 case IOCTL_EVTCHN_NOTIFY
: {
435 struct ioctl_evtchn_notify notify
;
436 struct user_evtchn
*evtchn
;
439 if (copy_from_user(¬ify
, uarg
, sizeof(notify
)))
443 evtchn
= find_evtchn(u
, notify
.port
);
445 notify_remote_via_evtchn(notify
.port
);
451 case IOCTL_EVTCHN_RESET
: {
452 /* Initialise the ring to empty. Clear errors. */
453 mutex_lock(&u
->ring_cons_mutex
);
454 spin_lock_irq(&u
->ring_prod_lock
);
455 u
->ring_cons
= u
->ring_prod
= u
->ring_overflow
= 0;
456 spin_unlock_irq(&u
->ring_prod_lock
);
457 mutex_unlock(&u
->ring_cons_mutex
);
466 mutex_unlock(&u
->bind_mutex
);
471 static unsigned int evtchn_poll(struct file
*file
, poll_table
*wait
)
473 unsigned int mask
= POLLOUT
| POLLWRNORM
;
474 struct per_user_data
*u
= file
->private_data
;
476 poll_wait(file
, &u
->evtchn_wait
, wait
);
477 if (u
->ring_cons
!= u
->ring_prod
)
478 mask
|= POLLIN
| POLLRDNORM
;
479 if (u
->ring_overflow
)
484 static int evtchn_fasync(int fd
, struct file
*filp
, int on
)
486 struct per_user_data
*u
= filp
->private_data
;
487 return fasync_helper(fd
, filp
, on
, &u
->evtchn_async_queue
);
490 static int evtchn_open(struct inode
*inode
, struct file
*filp
)
492 struct per_user_data
*u
;
494 u
= kzalloc(sizeof(*u
), GFP_KERNEL
);
498 u
->name
= kasprintf(GFP_KERNEL
, "evtchn:%s", current
->comm
);
499 if (u
->name
== NULL
) {
504 init_waitqueue_head(&u
->evtchn_wait
);
506 u
->ring
= (evtchn_port_t
*)__get_free_page(GFP_KERNEL
);
507 if (u
->ring
== NULL
) {
513 mutex_init(&u
->bind_mutex
);
514 mutex_init(&u
->ring_cons_mutex
);
515 spin_lock_init(&u
->ring_prod_lock
);
517 filp
->private_data
= u
;
519 return nonseekable_open(inode
, filp
);
522 static int evtchn_release(struct inode
*inode
, struct file
*filp
)
524 struct per_user_data
*u
= filp
->private_data
;
525 struct rb_node
*node
;
527 while ((node
= u
->evtchns
.rb_node
)) {
528 struct user_evtchn
*evtchn
;
530 evtchn
= rb_entry(node
, struct user_evtchn
, node
);
531 disable_irq(irq_from_evtchn(evtchn
->port
));
532 evtchn_unbind_from_user(u
, evtchn
);
535 free_page((unsigned long)u
->ring
);
542 static const struct file_operations evtchn_fops
= {
543 .owner
= THIS_MODULE
,
545 .write
= evtchn_write
,
546 .unlocked_ioctl
= evtchn_ioctl
,
548 .fasync
= evtchn_fasync
,
550 .release
= evtchn_release
,
554 static struct miscdevice evtchn_miscdev
= {
555 .minor
= MISC_DYNAMIC_MINOR
,
556 .name
= "xen/evtchn",
557 .fops
= &evtchn_fops
,
559 static int __init
evtchn_init(void)
566 /* Create '/dev/xen/evtchn'. */
567 err
= misc_register(&evtchn_miscdev
);
569 pr_err("Could not register /dev/xen/evtchn\n");
573 pr_info("Event-channel device installed\n");
578 static void __exit
evtchn_cleanup(void)
580 misc_deregister(&evtchn_miscdev
);
583 module_init(evtchn_init
);
584 module_exit(evtchn_cleanup
);
586 MODULE_LICENSE("GPL");