1f7116284SIan Campbell /****************************************************************************** 2f7116284SIan Campbell * evtchn.c 3f7116284SIan Campbell * 4f7116284SIan Campbell * Driver for receiving and demuxing event-channel signals. 5f7116284SIan Campbell * 6f7116284SIan Campbell * Copyright (c) 2004-2005, K A Fraser 7f7116284SIan Campbell * Multi-process extensions Copyright (c) 2004, Steven Smith 8f7116284SIan Campbell * 9f7116284SIan Campbell * This program is free software; you can redistribute it and/or 10f7116284SIan Campbell * modify it under the terms of the GNU General Public License version 2 11f7116284SIan Campbell * as published by the Free Software Foundation; or, when distributed 12f7116284SIan Campbell * separately from the Linux kernel or incorporated into other 13f7116284SIan Campbell * software packages, subject to the following license: 14f7116284SIan Campbell * 15f7116284SIan Campbell * Permission is hereby granted, free of charge, to any person obtaining a copy 16f7116284SIan Campbell * of this source file (the "Software"), to deal in the Software without 17f7116284SIan Campbell * restriction, including without limitation the rights to use, copy, modify, 18f7116284SIan Campbell * merge, publish, distribute, sublicense, and/or sell copies of the Software, 19f7116284SIan Campbell * and to permit persons to whom the Software is furnished to do so, subject to 20f7116284SIan Campbell * the following conditions: 21f7116284SIan Campbell * 22f7116284SIan Campbell * The above copyright notice and this permission notice shall be included in 23f7116284SIan Campbell * all copies or substantial portions of the Software. 24f7116284SIan Campbell * 25f7116284SIan Campbell * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 26f7116284SIan Campbell * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 27f7116284SIan Campbell * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 28f7116284SIan Campbell * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 29f7116284SIan Campbell * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 30f7116284SIan Campbell * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 31f7116284SIan Campbell * IN THE SOFTWARE. 32f7116284SIan Campbell */ 33f7116284SIan Campbell 34f7116284SIan Campbell #include <linux/module.h> 35f7116284SIan Campbell #include <linux/kernel.h> 36f7116284SIan Campbell #include <linux/sched.h> 37f7116284SIan Campbell #include <linux/slab.h> 38f7116284SIan Campbell #include <linux/string.h> 39f7116284SIan Campbell #include <linux/errno.h> 40f7116284SIan Campbell #include <linux/fs.h> 41f7116284SIan Campbell #include <linux/errno.h> 42f7116284SIan Campbell #include <linux/miscdevice.h> 43f7116284SIan Campbell #include <linux/major.h> 44f7116284SIan Campbell #include <linux/proc_fs.h> 45f7116284SIan Campbell #include <linux/stat.h> 46f7116284SIan Campbell #include <linux/poll.h> 47f7116284SIan Campbell #include <linux/irq.h> 48f7116284SIan Campbell #include <linux/init.h> 49f7116284SIan Campbell #include <linux/gfp.h> 50f7116284SIan Campbell #include <linux/mutex.h> 51f7116284SIan Campbell #include <linux/cpu.h> 52f7116284SIan Campbell #include <xen/events.h> 53f7116284SIan Campbell #include <xen/evtchn.h> 54f7116284SIan Campbell #include <asm/xen/hypervisor.h> 55f7116284SIan Campbell 56f7116284SIan Campbell struct per_user_data { 57f7116284SIan Campbell /* Notification ring, accessed via /dev/xen/evtchn. */ 58f7116284SIan Campbell #define EVTCHN_RING_SIZE (PAGE_SIZE / sizeof(evtchn_port_t)) 59f7116284SIan Campbell #define EVTCHN_RING_MASK(_i) ((_i)&(EVTCHN_RING_SIZE-1)) 60f7116284SIan Campbell evtchn_port_t *ring; 61f7116284SIan Campbell unsigned int ring_cons, ring_prod, ring_overflow; 62f7116284SIan Campbell struct mutex ring_cons_mutex; /* protect against concurrent readers */ 63f7116284SIan Campbell 64f7116284SIan Campbell /* Processes wait on this queue when ring is empty. */ 65f7116284SIan Campbell wait_queue_head_t evtchn_wait; 66f7116284SIan Campbell struct fasync_struct *evtchn_async_queue; 67f7116284SIan Campbell const char *name; 68f7116284SIan Campbell }; 69f7116284SIan Campbell 70f7116284SIan Campbell /* Who's bound to each port? */ 71f7116284SIan Campbell static struct per_user_data *port_user[NR_EVENT_CHANNELS]; 72f7116284SIan Campbell static DEFINE_SPINLOCK(port_user_lock); 73f7116284SIan Campbell 74f7116284SIan Campbell irqreturn_t evtchn_interrupt(int irq, void *data) 75f7116284SIan Campbell { 76f7116284SIan Campbell unsigned int port = (unsigned long)data; 77f7116284SIan Campbell struct per_user_data *u; 78f7116284SIan Campbell 79f7116284SIan Campbell spin_lock(&port_user_lock); 80f7116284SIan Campbell 81f7116284SIan Campbell u = port_user[port]; 82f7116284SIan Campbell 83f7116284SIan Campbell disable_irq_nosync(irq); 84f7116284SIan Campbell 85f7116284SIan Campbell if ((u->ring_prod - u->ring_cons) < EVTCHN_RING_SIZE) { 86f7116284SIan Campbell u->ring[EVTCHN_RING_MASK(u->ring_prod)] = port; 87f7116284SIan Campbell wmb(); /* Ensure ring contents visible */ 88f7116284SIan Campbell if (u->ring_cons == u->ring_prod++) { 89f7116284SIan Campbell wake_up_interruptible(&u->evtchn_wait); 90f7116284SIan Campbell kill_fasync(&u->evtchn_async_queue, 91f7116284SIan Campbell SIGIO, POLL_IN); 92f7116284SIan Campbell } 93f7116284SIan Campbell } else { 94f7116284SIan Campbell u->ring_overflow = 1; 95f7116284SIan Campbell } 96f7116284SIan Campbell 97f7116284SIan Campbell spin_unlock(&port_user_lock); 98f7116284SIan Campbell 99f7116284SIan Campbell return IRQ_HANDLED; 100f7116284SIan Campbell } 101f7116284SIan Campbell 102f7116284SIan Campbell static ssize_t evtchn_read(struct file *file, char __user *buf, 103f7116284SIan Campbell size_t count, loff_t *ppos) 104f7116284SIan Campbell { 105f7116284SIan Campbell int rc; 106f7116284SIan Campbell unsigned int c, p, bytes1 = 0, bytes2 = 0; 107f7116284SIan Campbell struct per_user_data *u = file->private_data; 108f7116284SIan Campbell 109f7116284SIan Campbell /* Whole number of ports. */ 110f7116284SIan Campbell count &= ~(sizeof(evtchn_port_t)-1); 111f7116284SIan Campbell 112f7116284SIan Campbell if (count == 0) 113f7116284SIan Campbell return 0; 114f7116284SIan Campbell 115f7116284SIan Campbell if (count > PAGE_SIZE) 116f7116284SIan Campbell count = PAGE_SIZE; 117f7116284SIan Campbell 118f7116284SIan Campbell for (;;) { 119f7116284SIan Campbell mutex_lock(&u->ring_cons_mutex); 120f7116284SIan Campbell 121f7116284SIan Campbell rc = -EFBIG; 122f7116284SIan Campbell if (u->ring_overflow) 123f7116284SIan Campbell goto unlock_out; 124f7116284SIan Campbell 125f7116284SIan Campbell c = u->ring_cons; 126f7116284SIan Campbell p = u->ring_prod; 127f7116284SIan Campbell if (c != p) 128f7116284SIan Campbell break; 129f7116284SIan Campbell 130f7116284SIan Campbell mutex_unlock(&u->ring_cons_mutex); 131f7116284SIan Campbell 132f7116284SIan Campbell if (file->f_flags & O_NONBLOCK) 133f7116284SIan Campbell return -EAGAIN; 134f7116284SIan Campbell 135f7116284SIan Campbell rc = wait_event_interruptible(u->evtchn_wait, 136f7116284SIan Campbell u->ring_cons != u->ring_prod); 137f7116284SIan Campbell if (rc) 138f7116284SIan Campbell return rc; 139f7116284SIan Campbell } 140f7116284SIan Campbell 141f7116284SIan Campbell /* Byte lengths of two chunks. Chunk split (if any) is at ring wrap. */ 142f7116284SIan Campbell if (((c ^ p) & EVTCHN_RING_SIZE) != 0) { 143f7116284SIan Campbell bytes1 = (EVTCHN_RING_SIZE - EVTCHN_RING_MASK(c)) * 144f7116284SIan Campbell sizeof(evtchn_port_t); 145f7116284SIan Campbell bytes2 = EVTCHN_RING_MASK(p) * sizeof(evtchn_port_t); 146f7116284SIan Campbell } else { 147f7116284SIan Campbell bytes1 = (p - c) * sizeof(evtchn_port_t); 148f7116284SIan Campbell bytes2 = 0; 149f7116284SIan Campbell } 150f7116284SIan Campbell 151f7116284SIan Campbell /* Truncate chunks according to caller's maximum byte count. */ 152f7116284SIan Campbell if (bytes1 > count) { 153f7116284SIan Campbell bytes1 = count; 154f7116284SIan Campbell bytes2 = 0; 155f7116284SIan Campbell } else if ((bytes1 + bytes2) > count) { 156f7116284SIan Campbell bytes2 = count - bytes1; 157f7116284SIan Campbell } 158f7116284SIan Campbell 159f7116284SIan Campbell rc = -EFAULT; 160f7116284SIan Campbell rmb(); /* Ensure that we see the port before we copy it. */ 161f7116284SIan Campbell if (copy_to_user(buf, &u->ring[EVTCHN_RING_MASK(c)], bytes1) || 162f7116284SIan Campbell ((bytes2 != 0) && 163f7116284SIan Campbell copy_to_user(&buf[bytes1], &u->ring[0], bytes2))) 164f7116284SIan Campbell goto unlock_out; 165f7116284SIan Campbell 166f7116284SIan Campbell u->ring_cons += (bytes1 + bytes2) / sizeof(evtchn_port_t); 167f7116284SIan Campbell rc = bytes1 + bytes2; 168f7116284SIan Campbell 169f7116284SIan Campbell unlock_out: 170f7116284SIan Campbell mutex_unlock(&u->ring_cons_mutex); 171f7116284SIan Campbell return rc; 172f7116284SIan Campbell } 173f7116284SIan Campbell 174f7116284SIan Campbell static ssize_t evtchn_write(struct file *file, const char __user *buf, 175f7116284SIan Campbell size_t count, loff_t *ppos) 176f7116284SIan Campbell { 177f7116284SIan Campbell int rc, i; 178f7116284SIan Campbell evtchn_port_t *kbuf = (evtchn_port_t *)__get_free_page(GFP_KERNEL); 179f7116284SIan Campbell struct per_user_data *u = file->private_data; 180f7116284SIan Campbell 181f7116284SIan Campbell if (kbuf == NULL) 182f7116284SIan Campbell return -ENOMEM; 183f7116284SIan Campbell 184f7116284SIan Campbell /* Whole number of ports. */ 185f7116284SIan Campbell count &= ~(sizeof(evtchn_port_t)-1); 186f7116284SIan Campbell 187f7116284SIan Campbell rc = 0; 188f7116284SIan Campbell if (count == 0) 189f7116284SIan Campbell goto out; 190f7116284SIan Campbell 191f7116284SIan Campbell if (count > PAGE_SIZE) 192f7116284SIan Campbell count = PAGE_SIZE; 193f7116284SIan Campbell 194f7116284SIan Campbell rc = -EFAULT; 195f7116284SIan Campbell if (copy_from_user(kbuf, buf, count) != 0) 196f7116284SIan Campbell goto out; 197f7116284SIan Campbell 198f7116284SIan Campbell spin_lock_irq(&port_user_lock); 199f7116284SIan Campbell for (i = 0; i < (count/sizeof(evtchn_port_t)); i++) 200f7116284SIan Campbell if ((kbuf[i] < NR_EVENT_CHANNELS) && (port_user[kbuf[i]] == u)) 201f7116284SIan Campbell enable_irq(irq_from_evtchn(kbuf[i])); 202f7116284SIan Campbell spin_unlock_irq(&port_user_lock); 203f7116284SIan Campbell 204f7116284SIan Campbell rc = count; 205f7116284SIan Campbell 206f7116284SIan Campbell out: 207f7116284SIan Campbell free_page((unsigned long)kbuf); 208f7116284SIan Campbell return rc; 209f7116284SIan Campbell } 210f7116284SIan Campbell 211f7116284SIan Campbell static int evtchn_bind_to_user(struct per_user_data *u, int port) 212f7116284SIan Campbell { 213f7116284SIan Campbell int irq; 214f7116284SIan Campbell int rc = 0; 215f7116284SIan Campbell 216f7116284SIan Campbell spin_lock_irq(&port_user_lock); 217f7116284SIan Campbell 218f7116284SIan Campbell BUG_ON(port_user[port] != NULL); 219f7116284SIan Campbell 220f7116284SIan Campbell irq = bind_evtchn_to_irqhandler(port, evtchn_interrupt, IRQF_DISABLED, 221f7116284SIan Campbell u->name, (void *)(unsigned long)port); 222f7116284SIan Campbell if (rc < 0) 223f7116284SIan Campbell goto fail; 224f7116284SIan Campbell 225f7116284SIan Campbell port_user[port] = u; 226f7116284SIan Campbell 227f7116284SIan Campbell fail: 228f7116284SIan Campbell spin_unlock_irq(&port_user_lock); 229f7116284SIan Campbell return rc; 230f7116284SIan Campbell } 231f7116284SIan Campbell 232f7116284SIan Campbell static void evtchn_unbind_from_user(struct per_user_data *u, int port) 233f7116284SIan Campbell { 234f7116284SIan Campbell int irq = irq_from_evtchn(port); 235f7116284SIan Campbell 236f7116284SIan Campbell unbind_from_irqhandler(irq, (void *)(unsigned long)port); 237f7116284SIan Campbell port_user[port] = NULL; 238f7116284SIan Campbell } 239f7116284SIan Campbell 240f7116284SIan Campbell static long evtchn_ioctl(struct file *file, 241f7116284SIan Campbell unsigned int cmd, unsigned long arg) 242f7116284SIan Campbell { 243f7116284SIan Campbell int rc; 244f7116284SIan Campbell struct per_user_data *u = file->private_data; 245f7116284SIan Campbell void __user *uarg = (void __user *) arg; 246f7116284SIan Campbell 247f7116284SIan Campbell switch (cmd) { 248f7116284SIan Campbell case IOCTL_EVTCHN_BIND_VIRQ: { 249f7116284SIan Campbell struct ioctl_evtchn_bind_virq bind; 250f7116284SIan Campbell struct evtchn_bind_virq bind_virq; 251f7116284SIan Campbell 252f7116284SIan Campbell rc = -EFAULT; 253f7116284SIan Campbell if (copy_from_user(&bind, uarg, sizeof(bind))) 254f7116284SIan Campbell break; 255f7116284SIan Campbell 256f7116284SIan Campbell bind_virq.virq = bind.virq; 257f7116284SIan Campbell bind_virq.vcpu = 0; 258f7116284SIan Campbell rc = HYPERVISOR_event_channel_op(EVTCHNOP_bind_virq, 259f7116284SIan Campbell &bind_virq); 260f7116284SIan Campbell if (rc != 0) 261f7116284SIan Campbell break; 262f7116284SIan Campbell 263f7116284SIan Campbell rc = evtchn_bind_to_user(u, bind_virq.port); 264f7116284SIan Campbell if (rc == 0) 265f7116284SIan Campbell rc = bind_virq.port; 266f7116284SIan Campbell break; 267f7116284SIan Campbell } 268f7116284SIan Campbell 269f7116284SIan Campbell case IOCTL_EVTCHN_BIND_INTERDOMAIN: { 270f7116284SIan Campbell struct ioctl_evtchn_bind_interdomain bind; 271f7116284SIan Campbell struct evtchn_bind_interdomain bind_interdomain; 272f7116284SIan Campbell 273f7116284SIan Campbell rc = -EFAULT; 274f7116284SIan Campbell if (copy_from_user(&bind, uarg, sizeof(bind))) 275f7116284SIan Campbell break; 276f7116284SIan Campbell 277f7116284SIan Campbell bind_interdomain.remote_dom = bind.remote_domain; 278f7116284SIan Campbell bind_interdomain.remote_port = bind.remote_port; 279f7116284SIan Campbell rc = HYPERVISOR_event_channel_op(EVTCHNOP_bind_interdomain, 280f7116284SIan Campbell &bind_interdomain); 281f7116284SIan Campbell if (rc != 0) 282f7116284SIan Campbell break; 283f7116284SIan Campbell 284f7116284SIan Campbell rc = evtchn_bind_to_user(u, bind_interdomain.local_port); 285f7116284SIan Campbell if (rc == 0) 286f7116284SIan Campbell rc = bind_interdomain.local_port; 287f7116284SIan Campbell break; 288f7116284SIan Campbell } 289f7116284SIan Campbell 290f7116284SIan Campbell case IOCTL_EVTCHN_BIND_UNBOUND_PORT: { 291f7116284SIan Campbell struct ioctl_evtchn_bind_unbound_port bind; 292f7116284SIan Campbell struct evtchn_alloc_unbound alloc_unbound; 293f7116284SIan Campbell 294f7116284SIan Campbell rc = -EFAULT; 295f7116284SIan Campbell if (copy_from_user(&bind, uarg, sizeof(bind))) 296f7116284SIan Campbell break; 297f7116284SIan Campbell 298f7116284SIan Campbell alloc_unbound.dom = DOMID_SELF; 299f7116284SIan Campbell alloc_unbound.remote_dom = bind.remote_domain; 300f7116284SIan Campbell rc = HYPERVISOR_event_channel_op(EVTCHNOP_alloc_unbound, 301f7116284SIan Campbell &alloc_unbound); 302f7116284SIan Campbell if (rc != 0) 303f7116284SIan Campbell break; 304f7116284SIan Campbell 305f7116284SIan Campbell rc = evtchn_bind_to_user(u, alloc_unbound.port); 306f7116284SIan Campbell if (rc == 0) 307f7116284SIan Campbell rc = alloc_unbound.port; 308f7116284SIan Campbell break; 309f7116284SIan Campbell } 310f7116284SIan Campbell 311f7116284SIan Campbell case IOCTL_EVTCHN_UNBIND: { 312f7116284SIan Campbell struct ioctl_evtchn_unbind unbind; 313f7116284SIan Campbell 314f7116284SIan Campbell rc = -EFAULT; 315f7116284SIan Campbell if (copy_from_user(&unbind, uarg, sizeof(unbind))) 316f7116284SIan Campbell break; 317f7116284SIan Campbell 318f7116284SIan Campbell rc = -EINVAL; 319f7116284SIan Campbell if (unbind.port >= NR_EVENT_CHANNELS) 320f7116284SIan Campbell break; 321f7116284SIan Campbell 322f7116284SIan Campbell spin_lock_irq(&port_user_lock); 323f7116284SIan Campbell 324f7116284SIan Campbell rc = -ENOTCONN; 325f7116284SIan Campbell if (port_user[unbind.port] != u) { 326f7116284SIan Campbell spin_unlock_irq(&port_user_lock); 327f7116284SIan Campbell break; 328f7116284SIan Campbell } 329f7116284SIan Campbell 330f7116284SIan Campbell evtchn_unbind_from_user(u, unbind.port); 331f7116284SIan Campbell 332f7116284SIan Campbell spin_unlock_irq(&port_user_lock); 333f7116284SIan Campbell 334f7116284SIan Campbell rc = 0; 335f7116284SIan Campbell break; 336f7116284SIan Campbell } 337f7116284SIan Campbell 338f7116284SIan Campbell case IOCTL_EVTCHN_NOTIFY: { 339f7116284SIan Campbell struct ioctl_evtchn_notify notify; 340f7116284SIan Campbell 341f7116284SIan Campbell rc = -EFAULT; 342f7116284SIan Campbell if (copy_from_user(¬ify, uarg, sizeof(notify))) 343f7116284SIan Campbell break; 344f7116284SIan Campbell 345f7116284SIan Campbell if (notify.port >= NR_EVENT_CHANNELS) { 346f7116284SIan Campbell rc = -EINVAL; 347f7116284SIan Campbell } else if (port_user[notify.port] != u) { 348f7116284SIan Campbell rc = -ENOTCONN; 349f7116284SIan Campbell } else { 350f7116284SIan Campbell notify_remote_via_evtchn(notify.port); 351f7116284SIan Campbell rc = 0; 352f7116284SIan Campbell } 353f7116284SIan Campbell break; 354f7116284SIan Campbell } 355f7116284SIan Campbell 356f7116284SIan Campbell case IOCTL_EVTCHN_RESET: { 357f7116284SIan Campbell /* Initialise the ring to empty. Clear errors. */ 358f7116284SIan Campbell mutex_lock(&u->ring_cons_mutex); 359f7116284SIan Campbell spin_lock_irq(&port_user_lock); 360f7116284SIan Campbell u->ring_cons = u->ring_prod = u->ring_overflow = 0; 361f7116284SIan Campbell spin_unlock_irq(&port_user_lock); 362f7116284SIan Campbell mutex_unlock(&u->ring_cons_mutex); 363f7116284SIan Campbell rc = 0; 364f7116284SIan Campbell break; 365f7116284SIan Campbell } 366f7116284SIan Campbell 367f7116284SIan Campbell default: 368f7116284SIan Campbell rc = -ENOSYS; 369f7116284SIan Campbell break; 370f7116284SIan Campbell } 371f7116284SIan Campbell 372f7116284SIan Campbell return rc; 373f7116284SIan Campbell } 374f7116284SIan Campbell 375f7116284SIan Campbell static unsigned int evtchn_poll(struct file *file, poll_table *wait) 376f7116284SIan Campbell { 377f7116284SIan Campbell unsigned int mask = POLLOUT | POLLWRNORM; 378f7116284SIan Campbell struct per_user_data *u = file->private_data; 379f7116284SIan Campbell 380f7116284SIan Campbell poll_wait(file, &u->evtchn_wait, wait); 381f7116284SIan Campbell if (u->ring_cons != u->ring_prod) 382f7116284SIan Campbell mask |= POLLIN | POLLRDNORM; 383f7116284SIan Campbell if (u->ring_overflow) 384f7116284SIan Campbell mask = POLLERR; 385f7116284SIan Campbell return mask; 386f7116284SIan Campbell } 387f7116284SIan Campbell 388f7116284SIan Campbell static int evtchn_fasync(int fd, struct file *filp, int on) 389f7116284SIan Campbell { 390f7116284SIan Campbell struct per_user_data *u = filp->private_data; 391f7116284SIan Campbell return fasync_helper(fd, filp, on, &u->evtchn_async_queue); 392f7116284SIan Campbell } 393f7116284SIan Campbell 394f7116284SIan Campbell static int evtchn_open(struct inode *inode, struct file *filp) 395f7116284SIan Campbell { 396f7116284SIan Campbell struct per_user_data *u; 397f7116284SIan Campbell 398f7116284SIan Campbell u = kzalloc(sizeof(*u), GFP_KERNEL); 399f7116284SIan Campbell if (u == NULL) 400f7116284SIan Campbell return -ENOMEM; 401f7116284SIan Campbell 402f7116284SIan Campbell u->name = kasprintf(GFP_KERNEL, "evtchn:%s", current->comm); 403f7116284SIan Campbell if (u->name == NULL) { 404f7116284SIan Campbell kfree(u); 405f7116284SIan Campbell return -ENOMEM; 406f7116284SIan Campbell } 407f7116284SIan Campbell 408f7116284SIan Campbell init_waitqueue_head(&u->evtchn_wait); 409f7116284SIan Campbell 410f7116284SIan Campbell u->ring = (evtchn_port_t *)__get_free_page(GFP_KERNEL); 411f7116284SIan Campbell if (u->ring == NULL) { 412f7116284SIan Campbell kfree(u->name); 413f7116284SIan Campbell kfree(u); 414f7116284SIan Campbell return -ENOMEM; 415f7116284SIan Campbell } 416f7116284SIan Campbell 417f7116284SIan Campbell mutex_init(&u->ring_cons_mutex); 418f7116284SIan Campbell 419f7116284SIan Campbell filp->private_data = u; 420f7116284SIan Campbell 421f7116284SIan Campbell return 0; 422f7116284SIan Campbell } 423f7116284SIan Campbell 424f7116284SIan Campbell static int evtchn_release(struct inode *inode, struct file *filp) 425f7116284SIan Campbell { 426f7116284SIan Campbell int i; 427f7116284SIan Campbell struct per_user_data *u = filp->private_data; 428f7116284SIan Campbell 429f7116284SIan Campbell spin_lock_irq(&port_user_lock); 430f7116284SIan Campbell 431f7116284SIan Campbell free_page((unsigned long)u->ring); 432f7116284SIan Campbell 433f7116284SIan Campbell for (i = 0; i < NR_EVENT_CHANNELS; i++) { 434f7116284SIan Campbell if (port_user[i] != u) 435f7116284SIan Campbell continue; 436f7116284SIan Campbell 437f7116284SIan Campbell evtchn_unbind_from_user(port_user[i], i); 438f7116284SIan Campbell } 439f7116284SIan Campbell 440f7116284SIan Campbell spin_unlock_irq(&port_user_lock); 441f7116284SIan Campbell 442f7116284SIan Campbell kfree(u->name); 443f7116284SIan Campbell kfree(u); 444f7116284SIan Campbell 445f7116284SIan Campbell return 0; 446f7116284SIan Campbell } 447f7116284SIan Campbell 448f7116284SIan Campbell static const struct file_operations evtchn_fops = { 449f7116284SIan Campbell .owner = THIS_MODULE, 450f7116284SIan Campbell .read = evtchn_read, 451f7116284SIan Campbell .write = evtchn_write, 452f7116284SIan Campbell .unlocked_ioctl = evtchn_ioctl, 453f7116284SIan Campbell .poll = evtchn_poll, 454f7116284SIan Campbell .fasync = evtchn_fasync, 455f7116284SIan Campbell .open = evtchn_open, 456f7116284SIan Campbell .release = evtchn_release, 457f7116284SIan Campbell }; 458f7116284SIan Campbell 459f7116284SIan Campbell static struct miscdevice evtchn_miscdev = { 460f7116284SIan Campbell .minor = MISC_DYNAMIC_MINOR, 461f7116284SIan Campbell .name = "evtchn", 462f7116284SIan Campbell .fops = &evtchn_fops, 463f7116284SIan Campbell }; 464f7116284SIan Campbell static int __init evtchn_init(void) 465f7116284SIan Campbell { 466f7116284SIan Campbell int err; 467f7116284SIan Campbell 468f7116284SIan Campbell if (!xen_domain()) 469f7116284SIan Campbell return -ENODEV; 470f7116284SIan Campbell 471f7116284SIan Campbell spin_lock_init(&port_user_lock); 472f7116284SIan Campbell memset(port_user, 0, sizeof(port_user)); 473f7116284SIan Campbell 474f7116284SIan Campbell /* Create '/dev/misc/evtchn'. */ 475f7116284SIan Campbell err = misc_register(&evtchn_miscdev); 476f7116284SIan Campbell if (err != 0) { 477f7116284SIan Campbell printk(KERN_ALERT "Could not register /dev/misc/evtchn\n"); 478f7116284SIan Campbell return err; 479f7116284SIan Campbell } 480f7116284SIan Campbell 481f7116284SIan Campbell printk(KERN_INFO "Event-channel device installed.\n"); 482f7116284SIan Campbell 483f7116284SIan Campbell return 0; 484f7116284SIan Campbell } 485f7116284SIan Campbell 486f7116284SIan Campbell static void __exit evtchn_cleanup(void) 487f7116284SIan Campbell { 488f7116284SIan Campbell misc_deregister(&evtchn_miscdev); 489f7116284SIan Campbell } 490f7116284SIan Campbell 491f7116284SIan Campbell module_init(evtchn_init); 492f7116284SIan Campbell module_exit(evtchn_cleanup); 493f7116284SIan Campbell 494f7116284SIan Campbell MODULE_LICENSE("GPL"); 495