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 34283c0972SJoe Perches #define pr_fmt(fmt) "xen:" KBUILD_MODNAME ": " fmt 35283c0972SJoe Perches 36f7116284SIan Campbell #include <linux/module.h> 37f7116284SIan Campbell #include <linux/kernel.h> 38f7116284SIan Campbell #include <linux/sched.h> 39f7116284SIan Campbell #include <linux/slab.h> 40f7116284SIan Campbell #include <linux/string.h> 41f7116284SIan Campbell #include <linux/errno.h> 42f7116284SIan Campbell #include <linux/fs.h> 43f7116284SIan Campbell #include <linux/miscdevice.h> 44f7116284SIan Campbell #include <linux/major.h> 45f7116284SIan Campbell #include <linux/proc_fs.h> 46f7116284SIan Campbell #include <linux/stat.h> 47f7116284SIan Campbell #include <linux/poll.h> 48f7116284SIan Campbell #include <linux/irq.h> 49f7116284SIan Campbell #include <linux/init.h> 50f7116284SIan Campbell #include <linux/mutex.h> 51f7116284SIan Campbell #include <linux/cpu.h> 521ccbf534SJeremy Fitzhardinge 531ccbf534SJeremy Fitzhardinge #include <xen/xen.h> 54f7116284SIan Campbell #include <xen/events.h> 55f7116284SIan Campbell #include <xen/evtchn.h> 56f7116284SIan Campbell #include <asm/xen/hypervisor.h> 57f7116284SIan Campbell 58f7116284SIan Campbell struct per_user_data { 590a4666b5SJeremy Fitzhardinge struct mutex bind_mutex; /* serialize bind/unbind operations */ 6073cc4bb0SDavid Vrabel struct rb_root evtchns; 610a4666b5SJeremy Fitzhardinge 62f7116284SIan Campbell /* Notification ring, accessed via /dev/xen/evtchn. */ 63f7116284SIan Campbell #define EVTCHN_RING_SIZE (PAGE_SIZE / sizeof(evtchn_port_t)) 64f7116284SIan Campbell #define EVTCHN_RING_MASK(_i) ((_i)&(EVTCHN_RING_SIZE-1)) 65f7116284SIan Campbell evtchn_port_t *ring; 66f7116284SIan Campbell unsigned int ring_cons, ring_prod, ring_overflow; 67f7116284SIan Campbell struct mutex ring_cons_mutex; /* protect against concurrent readers */ 6873cc4bb0SDavid Vrabel spinlock_t ring_prod_lock; /* product against concurrent interrupts */ 69f7116284SIan Campbell 70f7116284SIan Campbell /* Processes wait on this queue when ring is empty. */ 71f7116284SIan Campbell wait_queue_head_t evtchn_wait; 72f7116284SIan Campbell struct fasync_struct *evtchn_async_queue; 73f7116284SIan Campbell const char *name; 74f7116284SIan Campbell }; 75f7116284SIan Campbell 7673cc4bb0SDavid Vrabel struct user_evtchn { 7773cc4bb0SDavid Vrabel struct rb_node node; 7873cc4bb0SDavid Vrabel struct per_user_data *user; 7973cc4bb0SDavid Vrabel unsigned port; 8073cc4bb0SDavid Vrabel bool enabled; 8173cc4bb0SDavid Vrabel }; 82f7116284SIan Campbell 8373cc4bb0SDavid Vrabel static int add_evtchn(struct per_user_data *u, struct user_evtchn *evtchn) 84e3cc067bSJeremy Fitzhardinge { 8573cc4bb0SDavid Vrabel struct rb_node **new = &(u->evtchns.rb_node), *parent = NULL; 86e3cc067bSJeremy Fitzhardinge 8773cc4bb0SDavid Vrabel while (*new) { 8873cc4bb0SDavid Vrabel struct user_evtchn *this; 89e3cc067bSJeremy Fitzhardinge 9073cc4bb0SDavid Vrabel this = container_of(*new, struct user_evtchn, node); 91e3cc067bSJeremy Fitzhardinge 9273cc4bb0SDavid Vrabel parent = *new; 9373cc4bb0SDavid Vrabel if (this->port < evtchn->port) 9473cc4bb0SDavid Vrabel new = &((*new)->rb_left); 9573cc4bb0SDavid Vrabel else if (this->port > evtchn->port) 9673cc4bb0SDavid Vrabel new = &((*new)->rb_right); 97e3cc067bSJeremy Fitzhardinge else 9873cc4bb0SDavid Vrabel return -EEXIST; 9973cc4bb0SDavid Vrabel } 10073cc4bb0SDavid Vrabel 10173cc4bb0SDavid Vrabel /* Add new node and rebalance tree. */ 10273cc4bb0SDavid Vrabel rb_link_node(&evtchn->node, parent, new); 10373cc4bb0SDavid Vrabel rb_insert_color(&evtchn->node, &u->evtchns); 10473cc4bb0SDavid Vrabel 10573cc4bb0SDavid Vrabel return 0; 10673cc4bb0SDavid Vrabel } 10773cc4bb0SDavid Vrabel 10873cc4bb0SDavid Vrabel static void del_evtchn(struct per_user_data *u, struct user_evtchn *evtchn) 10973cc4bb0SDavid Vrabel { 11073cc4bb0SDavid Vrabel rb_erase(&evtchn->node, &u->evtchns); 11173cc4bb0SDavid Vrabel kfree(evtchn); 11273cc4bb0SDavid Vrabel } 11373cc4bb0SDavid Vrabel 11473cc4bb0SDavid Vrabel static struct user_evtchn *find_evtchn(struct per_user_data *u, unsigned port) 11573cc4bb0SDavid Vrabel { 11673cc4bb0SDavid Vrabel struct rb_node *node = u->evtchns.rb_node; 11773cc4bb0SDavid Vrabel 11873cc4bb0SDavid Vrabel while (node) { 11973cc4bb0SDavid Vrabel struct user_evtchn *evtchn; 12073cc4bb0SDavid Vrabel 12173cc4bb0SDavid Vrabel evtchn = container_of(node, struct user_evtchn, node); 12273cc4bb0SDavid Vrabel 12373cc4bb0SDavid Vrabel if (evtchn->port < port) 12473cc4bb0SDavid Vrabel node = node->rb_left; 12573cc4bb0SDavid Vrabel else if (evtchn->port > port) 12673cc4bb0SDavid Vrabel node = node->rb_right; 12773cc4bb0SDavid Vrabel else 12873cc4bb0SDavid Vrabel return evtchn; 12973cc4bb0SDavid Vrabel } 13073cc4bb0SDavid Vrabel return NULL; 131e3cc067bSJeremy Fitzhardinge } 132e3cc067bSJeremy Fitzhardinge 13370697d54SJeremy Fitzhardinge static irqreturn_t evtchn_interrupt(int irq, void *data) 134f7116284SIan Campbell { 13573cc4bb0SDavid Vrabel struct user_evtchn *evtchn = data; 13673cc4bb0SDavid Vrabel struct per_user_data *u = evtchn->user; 137f7116284SIan Campbell 13873cc4bb0SDavid Vrabel WARN(!evtchn->enabled, 139e3cc067bSJeremy Fitzhardinge "Interrupt for port %d, but apparently not enabled; per-user %p\n", 14073cc4bb0SDavid Vrabel evtchn->port, u); 141f7116284SIan Campbell 142f7116284SIan Campbell disable_irq_nosync(irq); 14373cc4bb0SDavid Vrabel evtchn->enabled = false; 14473cc4bb0SDavid Vrabel 14573cc4bb0SDavid Vrabel spin_lock(&u->ring_prod_lock); 146f7116284SIan Campbell 147f7116284SIan Campbell if ((u->ring_prod - u->ring_cons) < EVTCHN_RING_SIZE) { 14873cc4bb0SDavid Vrabel u->ring[EVTCHN_RING_MASK(u->ring_prod)] = evtchn->port; 149f7116284SIan Campbell wmb(); /* Ensure ring contents visible */ 150f7116284SIan Campbell if (u->ring_cons == u->ring_prod++) { 151f7116284SIan Campbell wake_up_interruptible(&u->evtchn_wait); 152f7116284SIan Campbell kill_fasync(&u->evtchn_async_queue, 153f7116284SIan Campbell SIGIO, POLL_IN); 154f7116284SIan Campbell } 155e3cc067bSJeremy Fitzhardinge } else 156f7116284SIan Campbell u->ring_overflow = 1; 157f7116284SIan Campbell 15873cc4bb0SDavid Vrabel spin_unlock(&u->ring_prod_lock); 159f7116284SIan Campbell 160f7116284SIan Campbell return IRQ_HANDLED; 161f7116284SIan Campbell } 162f7116284SIan Campbell 163f7116284SIan Campbell static ssize_t evtchn_read(struct file *file, char __user *buf, 164f7116284SIan Campbell size_t count, loff_t *ppos) 165f7116284SIan Campbell { 166f7116284SIan Campbell int rc; 167f7116284SIan Campbell unsigned int c, p, bytes1 = 0, bytes2 = 0; 168f7116284SIan Campbell struct per_user_data *u = file->private_data; 169f7116284SIan Campbell 170f7116284SIan Campbell /* Whole number of ports. */ 171f7116284SIan Campbell count &= ~(sizeof(evtchn_port_t)-1); 172f7116284SIan Campbell 173f7116284SIan Campbell if (count == 0) 174f7116284SIan Campbell return 0; 175f7116284SIan Campbell 176f7116284SIan Campbell if (count > PAGE_SIZE) 177f7116284SIan Campbell count = PAGE_SIZE; 178f7116284SIan Campbell 179f7116284SIan Campbell for (;;) { 180f7116284SIan Campbell mutex_lock(&u->ring_cons_mutex); 181f7116284SIan Campbell 182f7116284SIan Campbell rc = -EFBIG; 183f7116284SIan Campbell if (u->ring_overflow) 184f7116284SIan Campbell goto unlock_out; 185f7116284SIan Campbell 186f7116284SIan Campbell c = u->ring_cons; 187f7116284SIan Campbell p = u->ring_prod; 188f7116284SIan Campbell if (c != p) 189f7116284SIan Campbell break; 190f7116284SIan Campbell 191f7116284SIan Campbell mutex_unlock(&u->ring_cons_mutex); 192f7116284SIan Campbell 193f7116284SIan Campbell if (file->f_flags & O_NONBLOCK) 194f7116284SIan Campbell return -EAGAIN; 195f7116284SIan Campbell 196f7116284SIan Campbell rc = wait_event_interruptible(u->evtchn_wait, 197f7116284SIan Campbell u->ring_cons != u->ring_prod); 198f7116284SIan Campbell if (rc) 199f7116284SIan Campbell return rc; 200f7116284SIan Campbell } 201f7116284SIan Campbell 202f7116284SIan Campbell /* Byte lengths of two chunks. Chunk split (if any) is at ring wrap. */ 203f7116284SIan Campbell if (((c ^ p) & EVTCHN_RING_SIZE) != 0) { 204f7116284SIan Campbell bytes1 = (EVTCHN_RING_SIZE - EVTCHN_RING_MASK(c)) * 205f7116284SIan Campbell sizeof(evtchn_port_t); 206f7116284SIan Campbell bytes2 = EVTCHN_RING_MASK(p) * sizeof(evtchn_port_t); 207f7116284SIan Campbell } else { 208f7116284SIan Campbell bytes1 = (p - c) * sizeof(evtchn_port_t); 209f7116284SIan Campbell bytes2 = 0; 210f7116284SIan Campbell } 211f7116284SIan Campbell 212f7116284SIan Campbell /* Truncate chunks according to caller's maximum byte count. */ 213f7116284SIan Campbell if (bytes1 > count) { 214f7116284SIan Campbell bytes1 = count; 215f7116284SIan Campbell bytes2 = 0; 216f7116284SIan Campbell } else if ((bytes1 + bytes2) > count) { 217f7116284SIan Campbell bytes2 = count - bytes1; 218f7116284SIan Campbell } 219f7116284SIan Campbell 220f7116284SIan Campbell rc = -EFAULT; 221f7116284SIan Campbell rmb(); /* Ensure that we see the port before we copy it. */ 222f7116284SIan Campbell if (copy_to_user(buf, &u->ring[EVTCHN_RING_MASK(c)], bytes1) || 223f7116284SIan Campbell ((bytes2 != 0) && 224f7116284SIan Campbell copy_to_user(&buf[bytes1], &u->ring[0], bytes2))) 225f7116284SIan Campbell goto unlock_out; 226f7116284SIan Campbell 227f7116284SIan Campbell u->ring_cons += (bytes1 + bytes2) / sizeof(evtchn_port_t); 228f7116284SIan Campbell rc = bytes1 + bytes2; 229f7116284SIan Campbell 230f7116284SIan Campbell unlock_out: 231f7116284SIan Campbell mutex_unlock(&u->ring_cons_mutex); 232f7116284SIan Campbell return rc; 233f7116284SIan Campbell } 234f7116284SIan Campbell 235f7116284SIan Campbell static ssize_t evtchn_write(struct file *file, const char __user *buf, 236f7116284SIan Campbell size_t count, loff_t *ppos) 237f7116284SIan Campbell { 238f7116284SIan Campbell int rc, i; 239f7116284SIan Campbell evtchn_port_t *kbuf = (evtchn_port_t *)__get_free_page(GFP_KERNEL); 240f7116284SIan Campbell struct per_user_data *u = file->private_data; 241f7116284SIan Campbell 242f7116284SIan Campbell if (kbuf == NULL) 243f7116284SIan Campbell return -ENOMEM; 244f7116284SIan Campbell 245f7116284SIan Campbell /* Whole number of ports. */ 246f7116284SIan Campbell count &= ~(sizeof(evtchn_port_t)-1); 247f7116284SIan Campbell 248f7116284SIan Campbell rc = 0; 249f7116284SIan Campbell if (count == 0) 250f7116284SIan Campbell goto out; 251f7116284SIan Campbell 252f7116284SIan Campbell if (count > PAGE_SIZE) 253f7116284SIan Campbell count = PAGE_SIZE; 254f7116284SIan Campbell 255f7116284SIan Campbell rc = -EFAULT; 256f7116284SIan Campbell if (copy_from_user(kbuf, buf, count) != 0) 257f7116284SIan Campbell goto out; 258f7116284SIan Campbell 25973cc4bb0SDavid Vrabel mutex_lock(&u->bind_mutex); 260e3cc067bSJeremy Fitzhardinge 261e3cc067bSJeremy Fitzhardinge for (i = 0; i < (count/sizeof(evtchn_port_t)); i++) { 262e3cc067bSJeremy Fitzhardinge unsigned port = kbuf[i]; 26373cc4bb0SDavid Vrabel struct user_evtchn *evtchn; 264e3cc067bSJeremy Fitzhardinge 26573cc4bb0SDavid Vrabel evtchn = find_evtchn(u, port); 26673cc4bb0SDavid Vrabel if (evtchn && !evtchn->enabled) { 26773cc4bb0SDavid Vrabel evtchn->enabled = true; 268e3cc067bSJeremy Fitzhardinge enable_irq(irq_from_evtchn(port)); 269e3cc067bSJeremy Fitzhardinge } 270e3cc067bSJeremy Fitzhardinge } 271e3cc067bSJeremy Fitzhardinge 27273cc4bb0SDavid Vrabel mutex_unlock(&u->bind_mutex); 273f7116284SIan Campbell 274f7116284SIan Campbell rc = count; 275f7116284SIan Campbell 276f7116284SIan Campbell out: 277f7116284SIan Campbell free_page((unsigned long)kbuf); 278f7116284SIan Campbell return rc; 279f7116284SIan Campbell } 280f7116284SIan Campbell 281f7116284SIan Campbell static int evtchn_bind_to_user(struct per_user_data *u, int port) 282f7116284SIan Campbell { 28373cc4bb0SDavid Vrabel struct user_evtchn *evtchn; 28473cc4bb0SDavid Vrabel struct evtchn_close close; 285f7116284SIan Campbell int rc = 0; 286f7116284SIan Campbell 2870a4666b5SJeremy Fitzhardinge /* 2880a4666b5SJeremy Fitzhardinge * Ports are never reused, so every caller should pass in a 2890a4666b5SJeremy Fitzhardinge * unique port. 2900a4666b5SJeremy Fitzhardinge * 2910a4666b5SJeremy Fitzhardinge * (Locking not necessary because we haven't registered the 2920a4666b5SJeremy Fitzhardinge * interrupt handler yet, and our caller has already 2930a4666b5SJeremy Fitzhardinge * serialized bind operations.) 2940a4666b5SJeremy Fitzhardinge */ 29573cc4bb0SDavid Vrabel 29673cc4bb0SDavid Vrabel evtchn = kzalloc(sizeof(*evtchn), GFP_KERNEL); 29773cc4bb0SDavid Vrabel if (!evtchn) 29873cc4bb0SDavid Vrabel return -ENOMEM; 29973cc4bb0SDavid Vrabel 30073cc4bb0SDavid Vrabel evtchn->user = u; 30173cc4bb0SDavid Vrabel evtchn->port = port; 30273cc4bb0SDavid Vrabel evtchn->enabled = true; /* start enabled */ 30373cc4bb0SDavid Vrabel 30473cc4bb0SDavid Vrabel rc = add_evtchn(u, evtchn); 30573cc4bb0SDavid Vrabel if (rc < 0) 30673cc4bb0SDavid Vrabel goto err; 307f7116284SIan Campbell 308*af09d1a7SMichael Opdenacker rc = bind_evtchn_to_irqhandler(port, evtchn_interrupt, 0, 30973cc4bb0SDavid Vrabel u->name, evtchn); 31073cc4bb0SDavid Vrabel if (rc < 0) 31173cc4bb0SDavid Vrabel goto err; 31273cc4bb0SDavid Vrabel 313420eb554SDaniel De Graaf rc = evtchn_make_refcounted(port); 31473cc4bb0SDavid Vrabel return rc; 31573cc4bb0SDavid Vrabel 31673cc4bb0SDavid Vrabel err: 317e7e44e44SWei Liu /* bind failed, should close the port now */ 318e7e44e44SWei Liu close.port = port; 319e7e44e44SWei Liu if (HYPERVISOR_event_channel_op(EVTCHNOP_close, &close) != 0) 320e7e44e44SWei Liu BUG(); 32173cc4bb0SDavid Vrabel del_evtchn(u, evtchn); 322f7116284SIan Campbell return rc; 323f7116284SIan Campbell } 324f7116284SIan Campbell 32573cc4bb0SDavid Vrabel static void evtchn_unbind_from_user(struct per_user_data *u, 32673cc4bb0SDavid Vrabel struct user_evtchn *evtchn) 327f7116284SIan Campbell { 32873cc4bb0SDavid Vrabel int irq = irq_from_evtchn(evtchn->port); 329f7116284SIan Campbell 330e7e44e44SWei Liu BUG_ON(irq < 0); 331e7e44e44SWei Liu 33273cc4bb0SDavid Vrabel unbind_from_irqhandler(irq, evtchn); 3330a4666b5SJeremy Fitzhardinge 33473cc4bb0SDavid Vrabel del_evtchn(u, evtchn); 335f7116284SIan Campbell } 336f7116284SIan Campbell 337f7116284SIan Campbell static long evtchn_ioctl(struct file *file, 338f7116284SIan Campbell unsigned int cmd, unsigned long arg) 339f7116284SIan Campbell { 340f7116284SIan Campbell int rc; 341f7116284SIan Campbell struct per_user_data *u = file->private_data; 342f7116284SIan Campbell void __user *uarg = (void __user *) arg; 343f7116284SIan Campbell 3440a4666b5SJeremy Fitzhardinge /* Prevent bind from racing with unbind */ 3450a4666b5SJeremy Fitzhardinge mutex_lock(&u->bind_mutex); 3460a4666b5SJeremy Fitzhardinge 347f7116284SIan Campbell switch (cmd) { 348f7116284SIan Campbell case IOCTL_EVTCHN_BIND_VIRQ: { 349f7116284SIan Campbell struct ioctl_evtchn_bind_virq bind; 350f7116284SIan Campbell struct evtchn_bind_virq bind_virq; 351f7116284SIan Campbell 352f7116284SIan Campbell rc = -EFAULT; 353f7116284SIan Campbell if (copy_from_user(&bind, uarg, sizeof(bind))) 354f7116284SIan Campbell break; 355f7116284SIan Campbell 356f7116284SIan Campbell bind_virq.virq = bind.virq; 357f7116284SIan Campbell bind_virq.vcpu = 0; 358f7116284SIan Campbell rc = HYPERVISOR_event_channel_op(EVTCHNOP_bind_virq, 359f7116284SIan Campbell &bind_virq); 360f7116284SIan Campbell if (rc != 0) 361f7116284SIan Campbell break; 362f7116284SIan Campbell 363f7116284SIan Campbell rc = evtchn_bind_to_user(u, bind_virq.port); 364f7116284SIan Campbell if (rc == 0) 365f7116284SIan Campbell rc = bind_virq.port; 366f7116284SIan Campbell break; 367f7116284SIan Campbell } 368f7116284SIan Campbell 369f7116284SIan Campbell case IOCTL_EVTCHN_BIND_INTERDOMAIN: { 370f7116284SIan Campbell struct ioctl_evtchn_bind_interdomain bind; 371f7116284SIan Campbell struct evtchn_bind_interdomain bind_interdomain; 372f7116284SIan Campbell 373f7116284SIan Campbell rc = -EFAULT; 374f7116284SIan Campbell if (copy_from_user(&bind, uarg, sizeof(bind))) 375f7116284SIan Campbell break; 376f7116284SIan Campbell 377f7116284SIan Campbell bind_interdomain.remote_dom = bind.remote_domain; 378f7116284SIan Campbell bind_interdomain.remote_port = bind.remote_port; 379f7116284SIan Campbell rc = HYPERVISOR_event_channel_op(EVTCHNOP_bind_interdomain, 380f7116284SIan Campbell &bind_interdomain); 381f7116284SIan Campbell if (rc != 0) 382f7116284SIan Campbell break; 383f7116284SIan Campbell 384f7116284SIan Campbell rc = evtchn_bind_to_user(u, bind_interdomain.local_port); 385f7116284SIan Campbell if (rc == 0) 386f7116284SIan Campbell rc = bind_interdomain.local_port; 387f7116284SIan Campbell break; 388f7116284SIan Campbell } 389f7116284SIan Campbell 390f7116284SIan Campbell case IOCTL_EVTCHN_BIND_UNBOUND_PORT: { 391f7116284SIan Campbell struct ioctl_evtchn_bind_unbound_port bind; 392f7116284SIan Campbell struct evtchn_alloc_unbound alloc_unbound; 393f7116284SIan Campbell 394f7116284SIan Campbell rc = -EFAULT; 395f7116284SIan Campbell if (copy_from_user(&bind, uarg, sizeof(bind))) 396f7116284SIan Campbell break; 397f7116284SIan Campbell 398f7116284SIan Campbell alloc_unbound.dom = DOMID_SELF; 399f7116284SIan Campbell alloc_unbound.remote_dom = bind.remote_domain; 400f7116284SIan Campbell rc = HYPERVISOR_event_channel_op(EVTCHNOP_alloc_unbound, 401f7116284SIan Campbell &alloc_unbound); 402f7116284SIan Campbell if (rc != 0) 403f7116284SIan Campbell break; 404f7116284SIan Campbell 405f7116284SIan Campbell rc = evtchn_bind_to_user(u, alloc_unbound.port); 406f7116284SIan Campbell if (rc == 0) 407f7116284SIan Campbell rc = alloc_unbound.port; 408f7116284SIan Campbell break; 409f7116284SIan Campbell } 410f7116284SIan Campbell 411f7116284SIan Campbell case IOCTL_EVTCHN_UNBIND: { 412f7116284SIan Campbell struct ioctl_evtchn_unbind unbind; 41373cc4bb0SDavid Vrabel struct user_evtchn *evtchn; 414f7116284SIan Campbell 415f7116284SIan Campbell rc = -EFAULT; 416f7116284SIan Campbell if (copy_from_user(&unbind, uarg, sizeof(unbind))) 417f7116284SIan Campbell break; 418f7116284SIan Campbell 419f7116284SIan Campbell rc = -EINVAL; 420f7116284SIan Campbell if (unbind.port >= NR_EVENT_CHANNELS) 421f7116284SIan Campbell break; 422f7116284SIan Campbell 423f7116284SIan Campbell rc = -ENOTCONN; 42473cc4bb0SDavid Vrabel evtchn = find_evtchn(u, unbind.port); 42573cc4bb0SDavid Vrabel if (!evtchn) 426f7116284SIan Campbell break; 427f7116284SIan Campbell 4283f5e554fSJeremy Fitzhardinge disable_irq(irq_from_evtchn(unbind.port)); 42973cc4bb0SDavid Vrabel evtchn_unbind_from_user(u, evtchn); 430f7116284SIan Campbell rc = 0; 431f7116284SIan Campbell break; 432f7116284SIan Campbell } 433f7116284SIan Campbell 434f7116284SIan Campbell case IOCTL_EVTCHN_NOTIFY: { 435f7116284SIan Campbell struct ioctl_evtchn_notify notify; 43673cc4bb0SDavid Vrabel struct user_evtchn *evtchn; 437f7116284SIan Campbell 438f7116284SIan Campbell rc = -EFAULT; 439f7116284SIan Campbell if (copy_from_user(¬ify, uarg, sizeof(notify))) 440f7116284SIan Campbell break; 441f7116284SIan Campbell 442f7116284SIan Campbell rc = -ENOTCONN; 44373cc4bb0SDavid Vrabel evtchn = find_evtchn(u, notify.port); 44473cc4bb0SDavid Vrabel if (evtchn) { 445f7116284SIan Campbell notify_remote_via_evtchn(notify.port); 446f7116284SIan Campbell rc = 0; 447f7116284SIan Campbell } 448f7116284SIan Campbell break; 449f7116284SIan Campbell } 450f7116284SIan Campbell 451f7116284SIan Campbell case IOCTL_EVTCHN_RESET: { 452f7116284SIan Campbell /* Initialise the ring to empty. Clear errors. */ 453f7116284SIan Campbell mutex_lock(&u->ring_cons_mutex); 45473cc4bb0SDavid Vrabel spin_lock_irq(&u->ring_prod_lock); 455f7116284SIan Campbell u->ring_cons = u->ring_prod = u->ring_overflow = 0; 45673cc4bb0SDavid Vrabel spin_unlock_irq(&u->ring_prod_lock); 457f7116284SIan Campbell mutex_unlock(&u->ring_cons_mutex); 458f7116284SIan Campbell rc = 0; 459f7116284SIan Campbell break; 460f7116284SIan Campbell } 461f7116284SIan Campbell 462f7116284SIan Campbell default: 463f7116284SIan Campbell rc = -ENOSYS; 464f7116284SIan Campbell break; 465f7116284SIan Campbell } 4660a4666b5SJeremy Fitzhardinge mutex_unlock(&u->bind_mutex); 467f7116284SIan Campbell 468f7116284SIan Campbell return rc; 469f7116284SIan Campbell } 470f7116284SIan Campbell 471f7116284SIan Campbell static unsigned int evtchn_poll(struct file *file, poll_table *wait) 472f7116284SIan Campbell { 473f7116284SIan Campbell unsigned int mask = POLLOUT | POLLWRNORM; 474f7116284SIan Campbell struct per_user_data *u = file->private_data; 475f7116284SIan Campbell 476f7116284SIan Campbell poll_wait(file, &u->evtchn_wait, wait); 477f7116284SIan Campbell if (u->ring_cons != u->ring_prod) 478f7116284SIan Campbell mask |= POLLIN | POLLRDNORM; 479f7116284SIan Campbell if (u->ring_overflow) 480f7116284SIan Campbell mask = POLLERR; 481f7116284SIan Campbell return mask; 482f7116284SIan Campbell } 483f7116284SIan Campbell 484f7116284SIan Campbell static int evtchn_fasync(int fd, struct file *filp, int on) 485f7116284SIan Campbell { 486f7116284SIan Campbell struct per_user_data *u = filp->private_data; 487f7116284SIan Campbell return fasync_helper(fd, filp, on, &u->evtchn_async_queue); 488f7116284SIan Campbell } 489f7116284SIan Campbell 490f7116284SIan Campbell static int evtchn_open(struct inode *inode, struct file *filp) 491f7116284SIan Campbell { 492f7116284SIan Campbell struct per_user_data *u; 493f7116284SIan Campbell 494f7116284SIan Campbell u = kzalloc(sizeof(*u), GFP_KERNEL); 495f7116284SIan Campbell if (u == NULL) 496f7116284SIan Campbell return -ENOMEM; 497f7116284SIan Campbell 498f7116284SIan Campbell u->name = kasprintf(GFP_KERNEL, "evtchn:%s", current->comm); 499f7116284SIan Campbell if (u->name == NULL) { 500f7116284SIan Campbell kfree(u); 501f7116284SIan Campbell return -ENOMEM; 502f7116284SIan Campbell } 503f7116284SIan Campbell 504f7116284SIan Campbell init_waitqueue_head(&u->evtchn_wait); 505f7116284SIan Campbell 506f7116284SIan Campbell u->ring = (evtchn_port_t *)__get_free_page(GFP_KERNEL); 507f7116284SIan Campbell if (u->ring == NULL) { 508f7116284SIan Campbell kfree(u->name); 509f7116284SIan Campbell kfree(u); 510f7116284SIan Campbell return -ENOMEM; 511f7116284SIan Campbell } 512f7116284SIan Campbell 5130a4666b5SJeremy Fitzhardinge mutex_init(&u->bind_mutex); 514f7116284SIan Campbell mutex_init(&u->ring_cons_mutex); 51573cc4bb0SDavid Vrabel spin_lock_init(&u->ring_prod_lock); 516f7116284SIan Campbell 517f7116284SIan Campbell filp->private_data = u; 518f7116284SIan Campbell 5196eab04a8SJustin P. Mattock return nonseekable_open(inode, filp); 520f7116284SIan Campbell } 521f7116284SIan Campbell 522f7116284SIan Campbell static int evtchn_release(struct inode *inode, struct file *filp) 523f7116284SIan Campbell { 524f7116284SIan Campbell struct per_user_data *u = filp->private_data; 52573cc4bb0SDavid Vrabel struct rb_node *node; 526f7116284SIan Campbell 52773cc4bb0SDavid Vrabel while ((node = u->evtchns.rb_node)) { 52873cc4bb0SDavid Vrabel struct user_evtchn *evtchn; 529f7116284SIan Campbell 53073cc4bb0SDavid Vrabel evtchn = rb_entry(node, struct user_evtchn, node); 53173cc4bb0SDavid Vrabel disable_irq(irq_from_evtchn(evtchn->port)); 53273cc4bb0SDavid Vrabel evtchn_unbind_from_user(u, evtchn); 5333f5e554fSJeremy Fitzhardinge } 5343f5e554fSJeremy Fitzhardinge 535179fbd5aSDavid Vrabel free_page((unsigned long)u->ring); 536f7116284SIan Campbell kfree(u->name); 537f7116284SIan Campbell kfree(u); 538f7116284SIan Campbell 539f7116284SIan Campbell return 0; 540f7116284SIan Campbell } 541f7116284SIan Campbell 542f7116284SIan Campbell static const struct file_operations evtchn_fops = { 543f7116284SIan Campbell .owner = THIS_MODULE, 544f7116284SIan Campbell .read = evtchn_read, 545f7116284SIan Campbell .write = evtchn_write, 546f7116284SIan Campbell .unlocked_ioctl = evtchn_ioctl, 547f7116284SIan Campbell .poll = evtchn_poll, 548f7116284SIan Campbell .fasync = evtchn_fasync, 549f7116284SIan Campbell .open = evtchn_open, 550f7116284SIan Campbell .release = evtchn_release, 551bc7fc5e3SJeremy Fitzhardinge .llseek = no_llseek, 552f7116284SIan Campbell }; 553f7116284SIan Campbell 554f7116284SIan Campbell static struct miscdevice evtchn_miscdev = { 555f7116284SIan Campbell .minor = MISC_DYNAMIC_MINOR, 556376d908fSBastian Blank .name = "xen/evtchn", 557f7116284SIan Campbell .fops = &evtchn_fops, 558f7116284SIan Campbell }; 559f7116284SIan Campbell static int __init evtchn_init(void) 560f7116284SIan Campbell { 561f7116284SIan Campbell int err; 562f7116284SIan Campbell 563f7116284SIan Campbell if (!xen_domain()) 564f7116284SIan Campbell return -ENODEV; 565f7116284SIan Campbell 56618283ea7SWei Liu /* Create '/dev/xen/evtchn'. */ 567f7116284SIan Campbell err = misc_register(&evtchn_miscdev); 568f7116284SIan Campbell if (err != 0) { 569283c0972SJoe Perches pr_err("Could not register /dev/xen/evtchn\n"); 570f7116284SIan Campbell return err; 571f7116284SIan Campbell } 572f7116284SIan Campbell 573283c0972SJoe Perches pr_info("Event-channel device installed\n"); 574f7116284SIan Campbell 575f7116284SIan Campbell return 0; 576f7116284SIan Campbell } 577f7116284SIan Campbell 578f7116284SIan Campbell static void __exit evtchn_cleanup(void) 579f7116284SIan Campbell { 580f7116284SIan Campbell misc_deregister(&evtchn_miscdev); 581f7116284SIan Campbell } 582f7116284SIan Campbell 583f7116284SIan Campbell module_init(evtchn_init); 584f7116284SIan Campbell module_exit(evtchn_cleanup); 585f7116284SIan Campbell 586f7116284SIan Campbell MODULE_LICENSE("GPL"); 587