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/miscdevice.h> 42f7116284SIan Campbell #include <linux/major.h> 43f7116284SIan Campbell #include <linux/proc_fs.h> 44f7116284SIan Campbell #include <linux/stat.h> 45f7116284SIan Campbell #include <linux/poll.h> 46f7116284SIan Campbell #include <linux/irq.h> 47f7116284SIan Campbell #include <linux/init.h> 48f7116284SIan Campbell #include <linux/mutex.h> 49f7116284SIan Campbell #include <linux/cpu.h> 501ccbf534SJeremy Fitzhardinge 511ccbf534SJeremy Fitzhardinge #include <xen/xen.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 { 570a4666b5SJeremy Fitzhardinge struct mutex bind_mutex; /* serialize bind/unbind operations */ 580a4666b5SJeremy Fitzhardinge 59f7116284SIan Campbell /* Notification ring, accessed via /dev/xen/evtchn. */ 60f7116284SIan Campbell #define EVTCHN_RING_SIZE (PAGE_SIZE / sizeof(evtchn_port_t)) 61f7116284SIan Campbell #define EVTCHN_RING_MASK(_i) ((_i)&(EVTCHN_RING_SIZE-1)) 62f7116284SIan Campbell evtchn_port_t *ring; 63f7116284SIan Campbell unsigned int ring_cons, ring_prod, ring_overflow; 64f7116284SIan Campbell struct mutex ring_cons_mutex; /* protect against concurrent readers */ 65f7116284SIan Campbell 66f7116284SIan Campbell /* Processes wait on this queue when ring is empty. */ 67f7116284SIan Campbell wait_queue_head_t evtchn_wait; 68f7116284SIan Campbell struct fasync_struct *evtchn_async_queue; 69f7116284SIan Campbell const char *name; 70f7116284SIan Campbell }; 71f7116284SIan Campbell 72f7116284SIan Campbell /* Who's bound to each port? */ 73f7116284SIan Campbell static struct per_user_data *port_user[NR_EVENT_CHANNELS]; 740a4666b5SJeremy Fitzhardinge static DEFINE_SPINLOCK(port_user_lock); /* protects port_user[] and ring_prod */ 75f7116284SIan Campbell 76f7116284SIan Campbell irqreturn_t evtchn_interrupt(int irq, void *data) 77f7116284SIan Campbell { 78f7116284SIan Campbell unsigned int port = (unsigned long)data; 79f7116284SIan Campbell struct per_user_data *u; 80f7116284SIan Campbell 81f7116284SIan Campbell spin_lock(&port_user_lock); 82f7116284SIan Campbell 83f7116284SIan Campbell u = port_user[port]; 84f7116284SIan Campbell 85f7116284SIan Campbell disable_irq_nosync(irq); 86f7116284SIan Campbell 87f7116284SIan Campbell if ((u->ring_prod - u->ring_cons) < EVTCHN_RING_SIZE) { 88f7116284SIan Campbell u->ring[EVTCHN_RING_MASK(u->ring_prod)] = port; 89f7116284SIan Campbell wmb(); /* Ensure ring contents visible */ 90f7116284SIan Campbell if (u->ring_cons == u->ring_prod++) { 91f7116284SIan Campbell wake_up_interruptible(&u->evtchn_wait); 92f7116284SIan Campbell kill_fasync(&u->evtchn_async_queue, 93f7116284SIan Campbell SIGIO, POLL_IN); 94f7116284SIan Campbell } 95f7116284SIan Campbell } else { 96f7116284SIan Campbell u->ring_overflow = 1; 97f7116284SIan Campbell } 98f7116284SIan Campbell 99f7116284SIan Campbell spin_unlock(&port_user_lock); 100f7116284SIan Campbell 101f7116284SIan Campbell return IRQ_HANDLED; 102f7116284SIan Campbell } 103f7116284SIan Campbell 104f7116284SIan Campbell static ssize_t evtchn_read(struct file *file, char __user *buf, 105f7116284SIan Campbell size_t count, loff_t *ppos) 106f7116284SIan Campbell { 107f7116284SIan Campbell int rc; 108f7116284SIan Campbell unsigned int c, p, bytes1 = 0, bytes2 = 0; 109f7116284SIan Campbell struct per_user_data *u = file->private_data; 110f7116284SIan Campbell 111f7116284SIan Campbell /* Whole number of ports. */ 112f7116284SIan Campbell count &= ~(sizeof(evtchn_port_t)-1); 113f7116284SIan Campbell 114f7116284SIan Campbell if (count == 0) 115f7116284SIan Campbell return 0; 116f7116284SIan Campbell 117f7116284SIan Campbell if (count > PAGE_SIZE) 118f7116284SIan Campbell count = PAGE_SIZE; 119f7116284SIan Campbell 120f7116284SIan Campbell for (;;) { 121f7116284SIan Campbell mutex_lock(&u->ring_cons_mutex); 122f7116284SIan Campbell 123f7116284SIan Campbell rc = -EFBIG; 124f7116284SIan Campbell if (u->ring_overflow) 125f7116284SIan Campbell goto unlock_out; 126f7116284SIan Campbell 127f7116284SIan Campbell c = u->ring_cons; 128f7116284SIan Campbell p = u->ring_prod; 129f7116284SIan Campbell if (c != p) 130f7116284SIan Campbell break; 131f7116284SIan Campbell 132f7116284SIan Campbell mutex_unlock(&u->ring_cons_mutex); 133f7116284SIan Campbell 134f7116284SIan Campbell if (file->f_flags & O_NONBLOCK) 135f7116284SIan Campbell return -EAGAIN; 136f7116284SIan Campbell 137f7116284SIan Campbell rc = wait_event_interruptible(u->evtchn_wait, 138f7116284SIan Campbell u->ring_cons != u->ring_prod); 139f7116284SIan Campbell if (rc) 140f7116284SIan Campbell return rc; 141f7116284SIan Campbell } 142f7116284SIan Campbell 143f7116284SIan Campbell /* Byte lengths of two chunks. Chunk split (if any) is at ring wrap. */ 144f7116284SIan Campbell if (((c ^ p) & EVTCHN_RING_SIZE) != 0) { 145f7116284SIan Campbell bytes1 = (EVTCHN_RING_SIZE - EVTCHN_RING_MASK(c)) * 146f7116284SIan Campbell sizeof(evtchn_port_t); 147f7116284SIan Campbell bytes2 = EVTCHN_RING_MASK(p) * sizeof(evtchn_port_t); 148f7116284SIan Campbell } else { 149f7116284SIan Campbell bytes1 = (p - c) * sizeof(evtchn_port_t); 150f7116284SIan Campbell bytes2 = 0; 151f7116284SIan Campbell } 152f7116284SIan Campbell 153f7116284SIan Campbell /* Truncate chunks according to caller's maximum byte count. */ 154f7116284SIan Campbell if (bytes1 > count) { 155f7116284SIan Campbell bytes1 = count; 156f7116284SIan Campbell bytes2 = 0; 157f7116284SIan Campbell } else if ((bytes1 + bytes2) > count) { 158f7116284SIan Campbell bytes2 = count - bytes1; 159f7116284SIan Campbell } 160f7116284SIan Campbell 161f7116284SIan Campbell rc = -EFAULT; 162f7116284SIan Campbell rmb(); /* Ensure that we see the port before we copy it. */ 163f7116284SIan Campbell if (copy_to_user(buf, &u->ring[EVTCHN_RING_MASK(c)], bytes1) || 164f7116284SIan Campbell ((bytes2 != 0) && 165f7116284SIan Campbell copy_to_user(&buf[bytes1], &u->ring[0], bytes2))) 166f7116284SIan Campbell goto unlock_out; 167f7116284SIan Campbell 168f7116284SIan Campbell u->ring_cons += (bytes1 + bytes2) / sizeof(evtchn_port_t); 169f7116284SIan Campbell rc = bytes1 + bytes2; 170f7116284SIan Campbell 171f7116284SIan Campbell unlock_out: 172f7116284SIan Campbell mutex_unlock(&u->ring_cons_mutex); 173f7116284SIan Campbell return rc; 174f7116284SIan Campbell } 175f7116284SIan Campbell 176f7116284SIan Campbell static ssize_t evtchn_write(struct file *file, const char __user *buf, 177f7116284SIan Campbell size_t count, loff_t *ppos) 178f7116284SIan Campbell { 179f7116284SIan Campbell int rc, i; 180f7116284SIan Campbell evtchn_port_t *kbuf = (evtchn_port_t *)__get_free_page(GFP_KERNEL); 181f7116284SIan Campbell struct per_user_data *u = file->private_data; 182f7116284SIan Campbell 183f7116284SIan Campbell if (kbuf == NULL) 184f7116284SIan Campbell return -ENOMEM; 185f7116284SIan Campbell 186f7116284SIan Campbell /* Whole number of ports. */ 187f7116284SIan Campbell count &= ~(sizeof(evtchn_port_t)-1); 188f7116284SIan Campbell 189f7116284SIan Campbell rc = 0; 190f7116284SIan Campbell if (count == 0) 191f7116284SIan Campbell goto out; 192f7116284SIan Campbell 193f7116284SIan Campbell if (count > PAGE_SIZE) 194f7116284SIan Campbell count = PAGE_SIZE; 195f7116284SIan Campbell 196f7116284SIan Campbell rc = -EFAULT; 197f7116284SIan Campbell if (copy_from_user(kbuf, buf, count) != 0) 198f7116284SIan Campbell goto out; 199f7116284SIan Campbell 200f7116284SIan Campbell spin_lock_irq(&port_user_lock); 201f7116284SIan Campbell for (i = 0; i < (count/sizeof(evtchn_port_t)); i++) 202f7116284SIan Campbell if ((kbuf[i] < NR_EVENT_CHANNELS) && (port_user[kbuf[i]] == u)) 203f7116284SIan Campbell enable_irq(irq_from_evtchn(kbuf[i])); 204f7116284SIan Campbell spin_unlock_irq(&port_user_lock); 205f7116284SIan Campbell 206f7116284SIan Campbell rc = count; 207f7116284SIan Campbell 208f7116284SIan Campbell out: 209f7116284SIan Campbell free_page((unsigned long)kbuf); 210f7116284SIan Campbell return rc; 211f7116284SIan Campbell } 212f7116284SIan Campbell 213f7116284SIan Campbell static int evtchn_bind_to_user(struct per_user_data *u, int port) 214f7116284SIan Campbell { 215f7116284SIan Campbell int rc = 0; 216f7116284SIan Campbell 2170a4666b5SJeremy Fitzhardinge /* 2180a4666b5SJeremy Fitzhardinge * Ports are never reused, so every caller should pass in a 2190a4666b5SJeremy Fitzhardinge * unique port. 2200a4666b5SJeremy Fitzhardinge * 2210a4666b5SJeremy Fitzhardinge * (Locking not necessary because we haven't registered the 2220a4666b5SJeremy Fitzhardinge * interrupt handler yet, and our caller has already 2230a4666b5SJeremy Fitzhardinge * serialized bind operations.) 2240a4666b5SJeremy Fitzhardinge */ 225f7116284SIan Campbell BUG_ON(port_user[port] != NULL); 226f7116284SIan Campbell port_user[port] = u; 227f7116284SIan Campbell 2280a4666b5SJeremy Fitzhardinge rc = bind_evtchn_to_irqhandler(port, evtchn_interrupt, IRQF_DISABLED, 2290a4666b5SJeremy Fitzhardinge u->name, (void *)(unsigned long)port); 2300a4666b5SJeremy Fitzhardinge if (rc >= 0) 2310a4666b5SJeremy Fitzhardinge rc = 0; 2320a4666b5SJeremy Fitzhardinge 233f7116284SIan Campbell return rc; 234f7116284SIan Campbell } 235f7116284SIan Campbell 236f7116284SIan Campbell static void evtchn_unbind_from_user(struct per_user_data *u, int port) 237f7116284SIan Campbell { 238f7116284SIan Campbell int irq = irq_from_evtchn(port); 239f7116284SIan Campbell 240f7116284SIan Campbell unbind_from_irqhandler(irq, (void *)(unsigned long)port); 2410a4666b5SJeremy Fitzhardinge 2420a4666b5SJeremy Fitzhardinge /* make sure we unbind the irq handler before clearing the port */ 2430a4666b5SJeremy Fitzhardinge barrier(); 2440a4666b5SJeremy Fitzhardinge 245f7116284SIan Campbell port_user[port] = NULL; 246f7116284SIan Campbell } 247f7116284SIan Campbell 248f7116284SIan Campbell static long evtchn_ioctl(struct file *file, 249f7116284SIan Campbell unsigned int cmd, unsigned long arg) 250f7116284SIan Campbell { 251f7116284SIan Campbell int rc; 252f7116284SIan Campbell struct per_user_data *u = file->private_data; 253f7116284SIan Campbell void __user *uarg = (void __user *) arg; 254f7116284SIan Campbell 2550a4666b5SJeremy Fitzhardinge /* Prevent bind from racing with unbind */ 2560a4666b5SJeremy Fitzhardinge mutex_lock(&u->bind_mutex); 2570a4666b5SJeremy Fitzhardinge 258f7116284SIan Campbell switch (cmd) { 259f7116284SIan Campbell case IOCTL_EVTCHN_BIND_VIRQ: { 260f7116284SIan Campbell struct ioctl_evtchn_bind_virq bind; 261f7116284SIan Campbell struct evtchn_bind_virq bind_virq; 262f7116284SIan Campbell 263f7116284SIan Campbell rc = -EFAULT; 264f7116284SIan Campbell if (copy_from_user(&bind, uarg, sizeof(bind))) 265f7116284SIan Campbell break; 266f7116284SIan Campbell 267f7116284SIan Campbell bind_virq.virq = bind.virq; 268f7116284SIan Campbell bind_virq.vcpu = 0; 269f7116284SIan Campbell rc = HYPERVISOR_event_channel_op(EVTCHNOP_bind_virq, 270f7116284SIan Campbell &bind_virq); 271f7116284SIan Campbell if (rc != 0) 272f7116284SIan Campbell break; 273f7116284SIan Campbell 274f7116284SIan Campbell rc = evtchn_bind_to_user(u, bind_virq.port); 275f7116284SIan Campbell if (rc == 0) 276f7116284SIan Campbell rc = bind_virq.port; 277f7116284SIan Campbell break; 278f7116284SIan Campbell } 279f7116284SIan Campbell 280f7116284SIan Campbell case IOCTL_EVTCHN_BIND_INTERDOMAIN: { 281f7116284SIan Campbell struct ioctl_evtchn_bind_interdomain bind; 282f7116284SIan Campbell struct evtchn_bind_interdomain bind_interdomain; 283f7116284SIan Campbell 284f7116284SIan Campbell rc = -EFAULT; 285f7116284SIan Campbell if (copy_from_user(&bind, uarg, sizeof(bind))) 286f7116284SIan Campbell break; 287f7116284SIan Campbell 288f7116284SIan Campbell bind_interdomain.remote_dom = bind.remote_domain; 289f7116284SIan Campbell bind_interdomain.remote_port = bind.remote_port; 290f7116284SIan Campbell rc = HYPERVISOR_event_channel_op(EVTCHNOP_bind_interdomain, 291f7116284SIan Campbell &bind_interdomain); 292f7116284SIan Campbell if (rc != 0) 293f7116284SIan Campbell break; 294f7116284SIan Campbell 295f7116284SIan Campbell rc = evtchn_bind_to_user(u, bind_interdomain.local_port); 296f7116284SIan Campbell if (rc == 0) 297f7116284SIan Campbell rc = bind_interdomain.local_port; 298f7116284SIan Campbell break; 299f7116284SIan Campbell } 300f7116284SIan Campbell 301f7116284SIan Campbell case IOCTL_EVTCHN_BIND_UNBOUND_PORT: { 302f7116284SIan Campbell struct ioctl_evtchn_bind_unbound_port bind; 303f7116284SIan Campbell struct evtchn_alloc_unbound alloc_unbound; 304f7116284SIan Campbell 305f7116284SIan Campbell rc = -EFAULT; 306f7116284SIan Campbell if (copy_from_user(&bind, uarg, sizeof(bind))) 307f7116284SIan Campbell break; 308f7116284SIan Campbell 309f7116284SIan Campbell alloc_unbound.dom = DOMID_SELF; 310f7116284SIan Campbell alloc_unbound.remote_dom = bind.remote_domain; 311f7116284SIan Campbell rc = HYPERVISOR_event_channel_op(EVTCHNOP_alloc_unbound, 312f7116284SIan Campbell &alloc_unbound); 313f7116284SIan Campbell if (rc != 0) 314f7116284SIan Campbell break; 315f7116284SIan Campbell 316f7116284SIan Campbell rc = evtchn_bind_to_user(u, alloc_unbound.port); 317f7116284SIan Campbell if (rc == 0) 318f7116284SIan Campbell rc = alloc_unbound.port; 319f7116284SIan Campbell break; 320f7116284SIan Campbell } 321f7116284SIan Campbell 322f7116284SIan Campbell case IOCTL_EVTCHN_UNBIND: { 323f7116284SIan Campbell struct ioctl_evtchn_unbind unbind; 324f7116284SIan Campbell 325f7116284SIan Campbell rc = -EFAULT; 326f7116284SIan Campbell if (copy_from_user(&unbind, uarg, sizeof(unbind))) 327f7116284SIan Campbell break; 328f7116284SIan Campbell 329f7116284SIan Campbell rc = -EINVAL; 330f7116284SIan Campbell if (unbind.port >= NR_EVENT_CHANNELS) 331f7116284SIan Campbell break; 332f7116284SIan Campbell 333f7116284SIan Campbell spin_lock_irq(&port_user_lock); 334f7116284SIan Campbell 335f7116284SIan Campbell rc = -ENOTCONN; 336f7116284SIan Campbell if (port_user[unbind.port] != u) { 337f7116284SIan Campbell spin_unlock_irq(&port_user_lock); 338f7116284SIan Campbell break; 339f7116284SIan Campbell } 340f7116284SIan Campbell 341f7116284SIan Campbell evtchn_unbind_from_user(u, unbind.port); 342f7116284SIan Campbell 343f7116284SIan Campbell spin_unlock_irq(&port_user_lock); 344f7116284SIan Campbell 345f7116284SIan Campbell rc = 0; 346f7116284SIan Campbell break; 347f7116284SIan Campbell } 348f7116284SIan Campbell 349f7116284SIan Campbell case IOCTL_EVTCHN_NOTIFY: { 350f7116284SIan Campbell struct ioctl_evtchn_notify notify; 351f7116284SIan Campbell 352f7116284SIan Campbell rc = -EFAULT; 353f7116284SIan Campbell if (copy_from_user(¬ify, uarg, sizeof(notify))) 354f7116284SIan Campbell break; 355f7116284SIan Campbell 356f7116284SIan Campbell if (notify.port >= NR_EVENT_CHANNELS) { 357f7116284SIan Campbell rc = -EINVAL; 358f7116284SIan Campbell } else if (port_user[notify.port] != u) { 359f7116284SIan Campbell rc = -ENOTCONN; 360f7116284SIan Campbell } else { 361f7116284SIan Campbell notify_remote_via_evtchn(notify.port); 362f7116284SIan Campbell rc = 0; 363f7116284SIan Campbell } 364f7116284SIan Campbell break; 365f7116284SIan Campbell } 366f7116284SIan Campbell 367f7116284SIan Campbell case IOCTL_EVTCHN_RESET: { 368f7116284SIan Campbell /* Initialise the ring to empty. Clear errors. */ 369f7116284SIan Campbell mutex_lock(&u->ring_cons_mutex); 370f7116284SIan Campbell spin_lock_irq(&port_user_lock); 371f7116284SIan Campbell u->ring_cons = u->ring_prod = u->ring_overflow = 0; 372f7116284SIan Campbell spin_unlock_irq(&port_user_lock); 373f7116284SIan Campbell mutex_unlock(&u->ring_cons_mutex); 374f7116284SIan Campbell rc = 0; 375f7116284SIan Campbell break; 376f7116284SIan Campbell } 377f7116284SIan Campbell 378f7116284SIan Campbell default: 379f7116284SIan Campbell rc = -ENOSYS; 380f7116284SIan Campbell break; 381f7116284SIan Campbell } 3820a4666b5SJeremy Fitzhardinge mutex_unlock(&u->bind_mutex); 383f7116284SIan Campbell 384f7116284SIan Campbell return rc; 385f7116284SIan Campbell } 386f7116284SIan Campbell 387f7116284SIan Campbell static unsigned int evtchn_poll(struct file *file, poll_table *wait) 388f7116284SIan Campbell { 389f7116284SIan Campbell unsigned int mask = POLLOUT | POLLWRNORM; 390f7116284SIan Campbell struct per_user_data *u = file->private_data; 391f7116284SIan Campbell 392f7116284SIan Campbell poll_wait(file, &u->evtchn_wait, wait); 393f7116284SIan Campbell if (u->ring_cons != u->ring_prod) 394f7116284SIan Campbell mask |= POLLIN | POLLRDNORM; 395f7116284SIan Campbell if (u->ring_overflow) 396f7116284SIan Campbell mask = POLLERR; 397f7116284SIan Campbell return mask; 398f7116284SIan Campbell } 399f7116284SIan Campbell 400f7116284SIan Campbell static int evtchn_fasync(int fd, struct file *filp, int on) 401f7116284SIan Campbell { 402f7116284SIan Campbell struct per_user_data *u = filp->private_data; 403f7116284SIan Campbell return fasync_helper(fd, filp, on, &u->evtchn_async_queue); 404f7116284SIan Campbell } 405f7116284SIan Campbell 406f7116284SIan Campbell static int evtchn_open(struct inode *inode, struct file *filp) 407f7116284SIan Campbell { 408f7116284SIan Campbell struct per_user_data *u; 409f7116284SIan Campbell 410f7116284SIan Campbell u = kzalloc(sizeof(*u), GFP_KERNEL); 411f7116284SIan Campbell if (u == NULL) 412f7116284SIan Campbell return -ENOMEM; 413f7116284SIan Campbell 414f7116284SIan Campbell u->name = kasprintf(GFP_KERNEL, "evtchn:%s", current->comm); 415f7116284SIan Campbell if (u->name == NULL) { 416f7116284SIan Campbell kfree(u); 417f7116284SIan Campbell return -ENOMEM; 418f7116284SIan Campbell } 419f7116284SIan Campbell 420f7116284SIan Campbell init_waitqueue_head(&u->evtchn_wait); 421f7116284SIan Campbell 422f7116284SIan Campbell u->ring = (evtchn_port_t *)__get_free_page(GFP_KERNEL); 423f7116284SIan Campbell if (u->ring == NULL) { 424f7116284SIan Campbell kfree(u->name); 425f7116284SIan Campbell kfree(u); 426f7116284SIan Campbell return -ENOMEM; 427f7116284SIan Campbell } 428f7116284SIan Campbell 4290a4666b5SJeremy Fitzhardinge mutex_init(&u->bind_mutex); 430f7116284SIan Campbell mutex_init(&u->ring_cons_mutex); 431f7116284SIan Campbell 432f7116284SIan Campbell filp->private_data = u; 433f7116284SIan Campbell 434*bc7fc5e3SJeremy Fitzhardinge return nonseekable_open(inode, filp);; 435f7116284SIan Campbell } 436f7116284SIan Campbell 437f7116284SIan Campbell static int evtchn_release(struct inode *inode, struct file *filp) 438f7116284SIan Campbell { 439f7116284SIan Campbell int i; 440f7116284SIan Campbell struct per_user_data *u = filp->private_data; 441f7116284SIan Campbell 442f7116284SIan Campbell spin_lock_irq(&port_user_lock); 443f7116284SIan Campbell 444f7116284SIan Campbell free_page((unsigned long)u->ring); 445f7116284SIan Campbell 446f7116284SIan Campbell for (i = 0; i < NR_EVENT_CHANNELS; i++) { 447f7116284SIan Campbell if (port_user[i] != u) 448f7116284SIan Campbell continue; 449f7116284SIan Campbell 450f7116284SIan Campbell evtchn_unbind_from_user(port_user[i], i); 451f7116284SIan Campbell } 452f7116284SIan Campbell 453f7116284SIan Campbell spin_unlock_irq(&port_user_lock); 454f7116284SIan Campbell 455f7116284SIan Campbell kfree(u->name); 456f7116284SIan Campbell kfree(u); 457f7116284SIan Campbell 458f7116284SIan Campbell return 0; 459f7116284SIan Campbell } 460f7116284SIan Campbell 461f7116284SIan Campbell static const struct file_operations evtchn_fops = { 462f7116284SIan Campbell .owner = THIS_MODULE, 463f7116284SIan Campbell .read = evtchn_read, 464f7116284SIan Campbell .write = evtchn_write, 465f7116284SIan Campbell .unlocked_ioctl = evtchn_ioctl, 466f7116284SIan Campbell .poll = evtchn_poll, 467f7116284SIan Campbell .fasync = evtchn_fasync, 468f7116284SIan Campbell .open = evtchn_open, 469f7116284SIan Campbell .release = evtchn_release, 470*bc7fc5e3SJeremy Fitzhardinge .llseek = no_llseek, 471f7116284SIan Campbell }; 472f7116284SIan Campbell 473f7116284SIan Campbell static struct miscdevice evtchn_miscdev = { 474f7116284SIan Campbell .minor = MISC_DYNAMIC_MINOR, 475f7116284SIan Campbell .name = "evtchn", 476f7116284SIan Campbell .fops = &evtchn_fops, 477f7116284SIan Campbell }; 478f7116284SIan Campbell static int __init evtchn_init(void) 479f7116284SIan Campbell { 480f7116284SIan Campbell int err; 481f7116284SIan Campbell 482f7116284SIan Campbell if (!xen_domain()) 483f7116284SIan Campbell return -ENODEV; 484f7116284SIan Campbell 485f7116284SIan Campbell spin_lock_init(&port_user_lock); 486f7116284SIan Campbell memset(port_user, 0, sizeof(port_user)); 487f7116284SIan Campbell 488f7116284SIan Campbell /* Create '/dev/misc/evtchn'. */ 489f7116284SIan Campbell err = misc_register(&evtchn_miscdev); 490f7116284SIan Campbell if (err != 0) { 491f7116284SIan Campbell printk(KERN_ALERT "Could not register /dev/misc/evtchn\n"); 492f7116284SIan Campbell return err; 493f7116284SIan Campbell } 494f7116284SIan Campbell 495f7116284SIan Campbell printk(KERN_INFO "Event-channel device installed.\n"); 496f7116284SIan Campbell 497f7116284SIan Campbell return 0; 498f7116284SIan Campbell } 499f7116284SIan Campbell 500f7116284SIan Campbell static void __exit evtchn_cleanup(void) 501f7116284SIan Campbell { 502f7116284SIan Campbell misc_deregister(&evtchn_miscdev); 503f7116284SIan Campbell } 504f7116284SIan Campbell 505f7116284SIan Campbell module_init(evtchn_init); 506f7116284SIan Campbell module_exit(evtchn_cleanup); 507f7116284SIan Campbell 508f7116284SIan Campbell MODULE_LICENSE("GPL"); 509