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 */
61 /* 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))
65 unsigned int ring_cons, ring_prod, ring_overflow;
66 struct mutex ring_cons_mutex; /* protect against concurrent readers */
68 /* Processes wait on this queue when ring is empty. */
69 wait_queue_head_t evtchn_wait;
70 struct fasync_struct *evtchn_async_queue;
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.
78 static unsigned long *port_user;
79 static DEFINE_SPINLOCK(port_user_lock); /* protects port_user[] and ring_prod */
81 static inline struct per_user_data *get_port_user(unsigned port)
83 return (struct per_user_data *)(port_user[port] & ~1);
86 static inline void set_port_user(unsigned port, struct per_user_data *u)
88 port_user[port] = (unsigned long)u;
91 static inline bool get_port_enabled(unsigned port)
93 return port_user[port] & 1;
96 static inline void set_port_enabled(unsigned port, bool enabled)
101 port_user[port] &= ~1;
104 static irqreturn_t evtchn_interrupt(int irq, void *data)
106 unsigned int port = (unsigned long)data;
107 struct per_user_data *u;
109 spin_lock(&port_user_lock);
111 u = get_port_user(port);
113 WARN(!get_port_enabled(port),
114 "Interrupt for port %d, but apparently not enabled; per-user %p\n",
117 disable_irq_nosync(irq);
118 set_port_enabled(port, false);
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,
129 u->ring_overflow = 1;
131 spin_unlock(&port_user_lock);
136 static ssize_t evtchn_read(struct file *file, char __user *buf,
137 size_t count, loff_t *ppos)
140 unsigned int c, p, bytes1 = 0, bytes2 = 0;
141 struct per_user_data *u = file->private_data;
143 /* Whole number of ports. */
144 count &= ~(sizeof(evtchn_port_t)-1);
149 if (count > PAGE_SIZE)
153 mutex_lock(&u->ring_cons_mutex);
156 if (u->ring_overflow)
164 mutex_unlock(&u->ring_cons_mutex);
166 if (file->f_flags & O_NONBLOCK)
169 rc = wait_event_interruptible(u->evtchn_wait,
170 u->ring_cons != u->ring_prod);
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);
181 bytes1 = (p - c) * sizeof(evtchn_port_t);
185 /* Truncate chunks according to caller's maximum byte count. */
186 if (bytes1 > count) {
189 } else if ((bytes1 + bytes2) > count) {
190 bytes2 = count - bytes1;
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) ||
197 copy_to_user(&buf[bytes1], &u->ring[0], bytes2)))
200 u->ring_cons += (bytes1 + bytes2) / sizeof(evtchn_port_t);
201 rc = bytes1 + bytes2;
204 mutex_unlock(&u->ring_cons_mutex);
208 static ssize_t evtchn_write(struct file *file, const char __user *buf,
209 size_t count, loff_t *ppos)
212 evtchn_port_t *kbuf = (evtchn_port_t *)__get_free_page(GFP_KERNEL);
213 struct per_user_data *u = file->private_data;
218 /* Whole number of ports. */
219 count &= ~(sizeof(evtchn_port_t)-1);
225 if (count > PAGE_SIZE)
229 if (copy_from_user(kbuf, buf, count) != 0)
232 spin_lock_irq(&port_user_lock);
234 for (i = 0; i < (count/sizeof(evtchn_port_t)); i++) {
235 unsigned port = kbuf[i];
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));
245 spin_unlock_irq(&port_user_lock);
250 free_page((unsigned long)kbuf);
254 static int evtchn_bind_to_user(struct per_user_data *u, int port)
259 * Ports are never reused, so every caller should pass in a
262 * (Locking not necessary because we haven't registered the
263 * interrupt handler yet, and our caller has already
264 * serialized bind operations.)
266 BUG_ON(get_port_user(port) != NULL);
267 set_port_user(port, u);
268 set_port_enabled(port, true); /* start enabled */
270 rc = bind_evtchn_to_irqhandler(port, evtchn_interrupt, IRQF_DISABLED,
271 u->name, (void *)(unsigned long)port);
273 rc = evtchn_make_refcounted(port);
275 /* bind failed, should close the port now */
276 struct evtchn_close close;
278 if (HYPERVISOR_event_channel_op(EVTCHNOP_close, &close) != 0)
280 set_port_user(port, NULL);
286 static void evtchn_unbind_from_user(struct per_user_data *u, int port)
288 int irq = irq_from_evtchn(port);
292 unbind_from_irqhandler(irq, (void *)(unsigned long)port);
294 set_port_user(port, NULL);
297 static long evtchn_ioctl(struct file *file,
298 unsigned int cmd, unsigned long arg)
301 struct per_user_data *u = file->private_data;
302 void __user *uarg = (void __user *) arg;
304 /* Prevent bind from racing with unbind */
305 mutex_lock(&u->bind_mutex);
308 case IOCTL_EVTCHN_BIND_VIRQ: {
309 struct ioctl_evtchn_bind_virq bind;
310 struct evtchn_bind_virq bind_virq;
313 if (copy_from_user(&bind, uarg, sizeof(bind)))
316 bind_virq.virq = bind.virq;
318 rc = HYPERVISOR_event_channel_op(EVTCHNOP_bind_virq,
323 rc = evtchn_bind_to_user(u, bind_virq.port);
329 case IOCTL_EVTCHN_BIND_INTERDOMAIN: {
330 struct ioctl_evtchn_bind_interdomain bind;
331 struct evtchn_bind_interdomain bind_interdomain;
334 if (copy_from_user(&bind, uarg, sizeof(bind)))
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,
344 rc = evtchn_bind_to_user(u, bind_interdomain.local_port);
346 rc = bind_interdomain.local_port;
350 case IOCTL_EVTCHN_BIND_UNBOUND_PORT: {
351 struct ioctl_evtchn_bind_unbound_port bind;
352 struct evtchn_alloc_unbound alloc_unbound;
355 if (copy_from_user(&bind, uarg, sizeof(bind)))
358 alloc_unbound.dom = DOMID_SELF;
359 alloc_unbound.remote_dom = bind.remote_domain;
360 rc = HYPERVISOR_event_channel_op(EVTCHNOP_alloc_unbound,
365 rc = evtchn_bind_to_user(u, alloc_unbound.port);
367 rc = alloc_unbound.port;
371 case IOCTL_EVTCHN_UNBIND: {
372 struct ioctl_evtchn_unbind unbind;
375 if (copy_from_user(&unbind, uarg, sizeof(unbind)))
379 if (unbind.port >= NR_EVENT_CHANNELS)
383 if (get_port_user(unbind.port) != u)
386 disable_irq(irq_from_evtchn(unbind.port));
388 evtchn_unbind_from_user(u, unbind.port);
394 case IOCTL_EVTCHN_NOTIFY: {
395 struct ioctl_evtchn_notify notify;
398 if (copy_from_user(¬ify, uarg, sizeof(notify)))
401 if (notify.port >= NR_EVENT_CHANNELS) {
403 } else if (get_port_user(notify.port) != u) {
406 notify_remote_via_evtchn(notify.port);
412 case IOCTL_EVTCHN_RESET: {
413 /* Initialise the ring to empty. Clear errors. */
414 mutex_lock(&u->ring_cons_mutex);
415 spin_lock_irq(&port_user_lock);
416 u->ring_cons = u->ring_prod = u->ring_overflow = 0;
417 spin_unlock_irq(&port_user_lock);
418 mutex_unlock(&u->ring_cons_mutex);
427 mutex_unlock(&u->bind_mutex);
432 static unsigned int evtchn_poll(struct file *file, poll_table *wait)
434 unsigned int mask = POLLOUT | POLLWRNORM;
435 struct per_user_data *u = file->private_data;
437 poll_wait(file, &u->evtchn_wait, wait);
438 if (u->ring_cons != u->ring_prod)
439 mask |= POLLIN | POLLRDNORM;
440 if (u->ring_overflow)
445 static int evtchn_fasync(int fd, struct file *filp, int on)
447 struct per_user_data *u = filp->private_data;
448 return fasync_helper(fd, filp, on, &u->evtchn_async_queue);
451 static int evtchn_open(struct inode *inode, struct file *filp)
453 struct per_user_data *u;
455 u = kzalloc(sizeof(*u), GFP_KERNEL);
459 u->name = kasprintf(GFP_KERNEL, "evtchn:%s", current->comm);
460 if (u->name == NULL) {
465 init_waitqueue_head(&u->evtchn_wait);
467 u->ring = (evtchn_port_t *)__get_free_page(GFP_KERNEL);
468 if (u->ring == NULL) {
474 mutex_init(&u->bind_mutex);
475 mutex_init(&u->ring_cons_mutex);
477 filp->private_data = u;
479 return nonseekable_open(inode, filp);
482 static int evtchn_release(struct inode *inode, struct file *filp)
485 struct per_user_data *u = filp->private_data;
487 for (i = 0; i < NR_EVENT_CHANNELS; i++) {
488 if (get_port_user(i) != u)
491 disable_irq(irq_from_evtchn(i));
492 evtchn_unbind_from_user(get_port_user(i), i);
495 free_page((unsigned long)u->ring);
502 static const struct file_operations evtchn_fops = {
503 .owner = THIS_MODULE,
505 .write = evtchn_write,
506 .unlocked_ioctl = evtchn_ioctl,
508 .fasync = evtchn_fasync,
510 .release = evtchn_release,
514 static struct miscdevice evtchn_miscdev = {
515 .minor = MISC_DYNAMIC_MINOR,
516 .name = "xen/evtchn",
517 .fops = &evtchn_fops,
519 static int __init evtchn_init(void)
526 port_user = kcalloc(NR_EVENT_CHANNELS, sizeof(*port_user), GFP_KERNEL);
527 if (port_user == NULL)
530 spin_lock_init(&port_user_lock);
532 /* Create '/dev/xen/evtchn'. */
533 err = misc_register(&evtchn_miscdev);
535 pr_err("Could not register /dev/xen/evtchn\n");
539 pr_info("Event-channel device installed\n");
544 static void __exit evtchn_cleanup(void)
549 misc_deregister(&evtchn_miscdev);
552 module_init(evtchn_init);
553 module_exit(evtchn_cleanup);
555 MODULE_LICENSE("GPL");