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/config.h>
35 #include <linux/module.h>
36 #include <linux/kernel.h>
37 #include <linux/sched.h>
38 #include <linux/slab.h>
39 #include <linux/string.h>
40 #include <linux/errno.h>
42 #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/gfp.h>
51 #include <xen/evtchn.h>
52 #include <xen/public/evtchn.h>
54 struct per_user_data {
55 /* Notification ring, accessed via /dev/xen/evtchn. */
56 #define EVTCHN_RING_SIZE (PAGE_SIZE / sizeof(evtchn_port_t))
57 #define EVTCHN_RING_MASK(_i) ((_i)&(EVTCHN_RING_SIZE-1))
59 unsigned int ring_cons, ring_prod, ring_overflow;
61 /* Processes wait on this queue when ring is empty. */
62 wait_queue_head_t evtchn_wait;
63 struct fasync_struct *evtchn_async_queue;
66 /* Who's bound to each port? */
67 static struct per_user_data *port_user[NR_EVENT_CHANNELS];
68 static spinlock_t port_user_lock;
70 void evtchn_device_upcall(int port)
72 struct per_user_data *u;
74 spin_lock(&port_user_lock);
79 if ((u = port_user[port]) != NULL) {
80 if ((u->ring_prod - u->ring_cons) < EVTCHN_RING_SIZE) {
81 u->ring[EVTCHN_RING_MASK(u->ring_prod)] = port;
82 if (u->ring_cons == u->ring_prod++) {
83 wake_up_interruptible(&u->evtchn_wait);
84 kill_fasync(&u->evtchn_async_queue,
92 spin_unlock(&port_user_lock);
95 static ssize_t evtchn_read(struct file *file, char __user *buf,
96 size_t count, loff_t *ppos)
99 unsigned int c, p, bytes1 = 0, bytes2 = 0;
100 struct per_user_data *u = file->private_data;
102 /* Whole number of ports. */
103 count &= ~(sizeof(evtchn_port_t)-1);
108 if (count > PAGE_SIZE)
112 if (u->ring_overflow)
115 if ((c = u->ring_cons) != (p = u->ring_prod))
118 if (file->f_flags & O_NONBLOCK)
121 rc = wait_event_interruptible(
122 u->evtchn_wait, u->ring_cons != u->ring_prod);
127 /* Byte lengths of two chunks. Chunk split (if any) is at ring wrap. */
128 if (((c ^ p) & EVTCHN_RING_SIZE) != 0) {
129 bytes1 = (EVTCHN_RING_SIZE - EVTCHN_RING_MASK(c)) *
130 sizeof(evtchn_port_t);
131 bytes2 = EVTCHN_RING_MASK(p) * sizeof(evtchn_port_t);
133 bytes1 = (p - c) * sizeof(evtchn_port_t);
137 /* Truncate chunks according to caller's maximum byte count. */
138 if (bytes1 > count) {
141 } else if ((bytes1 + bytes2) > count) {
142 bytes2 = count - bytes1;
145 if (copy_to_user(buf, &u->ring[EVTCHN_RING_MASK(c)], bytes1) ||
147 copy_to_user(&buf[bytes1], &u->ring[0], bytes2)))
150 u->ring_cons += (bytes1 + bytes2) / sizeof(evtchn_port_t);
152 return bytes1 + bytes2;
155 static ssize_t evtchn_write(struct file *file, const char __user *buf,
156 size_t count, loff_t *ppos)
159 evtchn_port_t *kbuf = (evtchn_port_t *)__get_free_page(GFP_KERNEL);
160 struct per_user_data *u = file->private_data;
165 /* Whole number of ports. */
166 count &= ~(sizeof(evtchn_port_t)-1);
173 if (count > PAGE_SIZE)
176 if (copy_from_user(kbuf, buf, count) != 0) {
181 spin_lock_irq(&port_user_lock);
182 for (i = 0; i < (count/sizeof(evtchn_port_t)); i++)
183 if ((kbuf[i] < NR_EVENT_CHANNELS) && (port_user[kbuf[i]] == u))
184 unmask_evtchn(kbuf[i]);
185 spin_unlock_irq(&port_user_lock);
190 free_page((unsigned long)kbuf);
194 static void evtchn_bind_to_user(struct per_user_data *u, int port)
196 spin_lock_irq(&port_user_lock);
197 BUG_ON(port_user[port] != NULL);
200 spin_unlock_irq(&port_user_lock);
203 static int evtchn_ioctl(struct inode *inode, struct file *file,
204 unsigned int cmd, unsigned long arg)
207 struct per_user_data *u = file->private_data;
208 void __user *uarg = (void __user *) arg;
211 case IOCTL_EVTCHN_BIND_VIRQ: {
212 struct ioctl_evtchn_bind_virq bind;
213 struct evtchn_bind_virq bind_virq;
216 if (copy_from_user(&bind, uarg, sizeof(bind)))
219 bind_virq.virq = bind.virq;
221 rc = HYPERVISOR_event_channel_op(EVTCHNOP_bind_virq,
227 evtchn_bind_to_user(u, rc);
231 case IOCTL_EVTCHN_BIND_INTERDOMAIN: {
232 struct ioctl_evtchn_bind_interdomain bind;
233 struct evtchn_bind_interdomain bind_interdomain;
236 if (copy_from_user(&bind, uarg, sizeof(bind)))
239 bind_interdomain.remote_dom = bind.remote_domain;
240 bind_interdomain.remote_port = bind.remote_port;
241 rc = HYPERVISOR_event_channel_op(EVTCHNOP_bind_interdomain,
246 rc = bind_interdomain.local_port;
247 evtchn_bind_to_user(u, rc);
251 case IOCTL_EVTCHN_BIND_UNBOUND_PORT: {
252 struct ioctl_evtchn_bind_unbound_port bind;
253 struct evtchn_alloc_unbound alloc_unbound;
256 if (copy_from_user(&bind, uarg, sizeof(bind)))
259 alloc_unbound.dom = DOMID_SELF;
260 alloc_unbound.remote_dom = bind.remote_domain;
261 rc = HYPERVISOR_event_channel_op(EVTCHNOP_alloc_unbound,
266 rc = alloc_unbound.port;
267 evtchn_bind_to_user(u, rc);
271 case IOCTL_EVTCHN_UNBIND: {
272 struct ioctl_evtchn_unbind unbind;
273 struct evtchn_close close;
277 if (copy_from_user(&unbind, uarg, sizeof(unbind)))
281 if (unbind.port >= NR_EVENT_CHANNELS)
284 spin_lock_irq(&port_user_lock);
287 if (port_user[unbind.port] != u) {
288 spin_unlock_irq(&port_user_lock);
292 port_user[unbind.port] = NULL;
293 mask_evtchn(unbind.port);
295 spin_unlock_irq(&port_user_lock);
297 close.port = unbind.port;
298 ret = HYPERVISOR_event_channel_op(EVTCHNOP_close, &close);
305 case IOCTL_EVTCHN_NOTIFY: {
306 struct ioctl_evtchn_notify notify;
309 if (copy_from_user(¬ify, uarg, sizeof(notify)))
312 if (notify.port >= NR_EVENT_CHANNELS) {
314 } else if (port_user[notify.port] != u) {
317 notify_remote_via_evtchn(notify.port);
323 case IOCTL_EVTCHN_RESET: {
324 /* Initialise the ring to empty. Clear errors. */
325 spin_lock_irq(&port_user_lock);
326 u->ring_cons = u->ring_prod = u->ring_overflow = 0;
327 spin_unlock_irq(&port_user_lock);
340 static unsigned int evtchn_poll(struct file *file, poll_table *wait)
342 unsigned int mask = POLLOUT | POLLWRNORM;
343 struct per_user_data *u = file->private_data;
345 poll_wait(file, &u->evtchn_wait, wait);
346 if (u->ring_cons != u->ring_prod)
347 mask |= POLLIN | POLLRDNORM;
348 if (u->ring_overflow)
353 static int evtchn_fasync(int fd, struct file *filp, int on)
355 struct per_user_data *u = filp->private_data;
356 return fasync_helper(fd, filp, on, &u->evtchn_async_queue);
359 static int evtchn_open(struct inode *inode, struct file *filp)
361 struct per_user_data *u;
363 if ((u = kmalloc(sizeof(*u), GFP_KERNEL)) == NULL)
366 memset(u, 0, sizeof(*u));
367 init_waitqueue_head(&u->evtchn_wait);
369 u->ring = (evtchn_port_t *)__get_free_page(GFP_KERNEL);
370 if (u->ring == NULL) {
375 filp->private_data = u;
380 static int evtchn_release(struct inode *inode, struct file *filp)
383 struct per_user_data *u = filp->private_data;
384 struct evtchn_close close;
386 spin_lock_irq(&port_user_lock);
388 free_page((unsigned long)u->ring);
390 for (i = 0; i < NR_EVENT_CHANNELS; i++) {
392 if (port_user[i] != u)
399 ret = HYPERVISOR_event_channel_op(EVTCHNOP_close, &close);
403 spin_unlock_irq(&port_user_lock);
410 static struct file_operations evtchn_fops = {
411 .owner = THIS_MODULE,
413 .write = evtchn_write,
414 .ioctl = evtchn_ioctl,
416 .fasync = evtchn_fasync,
418 .release = evtchn_release,
421 static struct miscdevice evtchn_miscdev = {
422 .minor = EVTCHN_MINOR,
424 .fops = &evtchn_fops,
425 .devfs_name = "misc/evtchn",
428 static int __init evtchn_init(void)
432 if (!is_running_on_xen())
435 spin_lock_init(&port_user_lock);
436 memset(port_user, 0, sizeof(port_user));
438 /* Create '/dev/misc/evtchn'. */
439 err = misc_register(&evtchn_miscdev);
441 printk(KERN_ALERT "Could not register /dev/misc/evtchn\n");
445 printk("Event-channel device installed.\n");
450 static void evtchn_cleanup(void)
452 misc_deregister(&evtchn_miscdev);
455 module_init(evtchn_init);
456 module_exit(evtchn_cleanup);
458 MODULE_LICENSE("Dual BSD/GPL");