[34] | 1 | /****************************************************************************** |
---|
| 2 | * evtchn.c |
---|
| 3 | * |
---|
| 4 | * Driver for receiving and demuxing event-channel signals. |
---|
| 5 | * |
---|
| 6 | * Copyright (c) 2004-2005, K A Fraser |
---|
| 7 | * Multi-process extensions Copyright (c) 2004, Steven Smith |
---|
| 8 | * |
---|
| 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: |
---|
| 14 | * |
---|
| 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: |
---|
| 21 | * |
---|
| 22 | * The above copyright notice and this permission notice shall be included in |
---|
| 23 | * all copies or substantial portions of the Software. |
---|
| 24 | * |
---|
| 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 |
---|
| 31 | * IN THE SOFTWARE. |
---|
| 32 | */ |
---|
| 33 | |
---|
| 34 | #include <linux/module.h> |
---|
| 35 | #include <linux/kernel.h> |
---|
| 36 | #include <linux/sched.h> |
---|
| 37 | #include <linux/slab.h> |
---|
| 38 | #include <linux/string.h> |
---|
| 39 | #include <linux/errno.h> |
---|
| 40 | #include <linux/fs.h> |
---|
| 41 | #include <linux/errno.h> |
---|
| 42 | #include <linux/miscdevice.h> |
---|
| 43 | #include <linux/major.h> |
---|
| 44 | #include <linux/proc_fs.h> |
---|
| 45 | #include <linux/stat.h> |
---|
| 46 | #include <linux/poll.h> |
---|
| 47 | #include <linux/irq.h> |
---|
| 48 | #include <linux/init.h> |
---|
| 49 | #include <linux/gfp.h> |
---|
| 50 | #include <linux/mutex.h> |
---|
| 51 | #include <xen/evtchn.h> |
---|
| 52 | #include <xen/public/evtchn.h> |
---|
| 53 | |
---|
| 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)) |
---|
| 58 | evtchn_port_t *ring; |
---|
| 59 | unsigned int ring_cons, ring_prod, ring_overflow; |
---|
| 60 | struct mutex ring_cons_mutex; /* protect against concurrent readers */ |
---|
| 61 | |
---|
| 62 | /* Processes wait on this queue when ring is empty. */ |
---|
| 63 | wait_queue_head_t evtchn_wait; |
---|
| 64 | struct fasync_struct *evtchn_async_queue; |
---|
| 65 | }; |
---|
| 66 | |
---|
| 67 | /* Who's bound to each port? */ |
---|
| 68 | static struct per_user_data *port_user[NR_EVENT_CHANNELS]; |
---|
| 69 | static spinlock_t port_user_lock; |
---|
| 70 | |
---|
| 71 | void evtchn_device_upcall(int port) |
---|
| 72 | { |
---|
| 73 | struct per_user_data *u; |
---|
| 74 | |
---|
| 75 | spin_lock(&port_user_lock); |
---|
| 76 | |
---|
| 77 | mask_evtchn(port); |
---|
| 78 | clear_evtchn(port); |
---|
| 79 | |
---|
| 80 | if ((u = port_user[port]) != NULL) { |
---|
| 81 | if ((u->ring_prod - u->ring_cons) < EVTCHN_RING_SIZE) { |
---|
| 82 | u->ring[EVTCHN_RING_MASK(u->ring_prod)] = port; |
---|
| 83 | if (u->ring_cons == u->ring_prod++) { |
---|
| 84 | wake_up_interruptible(&u->evtchn_wait); |
---|
| 85 | kill_fasync(&u->evtchn_async_queue, |
---|
| 86 | SIGIO, POLL_IN); |
---|
| 87 | } |
---|
| 88 | } else { |
---|
| 89 | u->ring_overflow = 1; |
---|
| 90 | } |
---|
| 91 | } |
---|
| 92 | |
---|
| 93 | spin_unlock(&port_user_lock); |
---|
| 94 | } |
---|
| 95 | |
---|
| 96 | static ssize_t evtchn_read(struct file *file, char __user *buf, |
---|
| 97 | size_t count, loff_t *ppos) |
---|
| 98 | { |
---|
| 99 | int rc; |
---|
| 100 | unsigned int c, p, bytes1 = 0, bytes2 = 0; |
---|
| 101 | struct per_user_data *u = file->private_data; |
---|
| 102 | |
---|
| 103 | /* Whole number of ports. */ |
---|
| 104 | count &= ~(sizeof(evtchn_port_t)-1); |
---|
| 105 | |
---|
| 106 | if (count == 0) |
---|
| 107 | return 0; |
---|
| 108 | |
---|
| 109 | if (count > PAGE_SIZE) |
---|
| 110 | count = PAGE_SIZE; |
---|
| 111 | |
---|
| 112 | for (;;) { |
---|
| 113 | mutex_lock(&u->ring_cons_mutex); |
---|
| 114 | |
---|
| 115 | rc = -EFBIG; |
---|
| 116 | if (u->ring_overflow) |
---|
| 117 | goto unlock_out; |
---|
| 118 | |
---|
| 119 | if ((c = u->ring_cons) != (p = u->ring_prod)) |
---|
| 120 | break; |
---|
| 121 | |
---|
| 122 | mutex_unlock(&u->ring_cons_mutex); |
---|
| 123 | |
---|
| 124 | if (file->f_flags & O_NONBLOCK) |
---|
| 125 | return -EAGAIN; |
---|
| 126 | |
---|
| 127 | rc = wait_event_interruptible( |
---|
| 128 | u->evtchn_wait, u->ring_cons != u->ring_prod); |
---|
| 129 | if (rc) |
---|
| 130 | return rc; |
---|
| 131 | } |
---|
| 132 | |
---|
| 133 | /* Byte lengths of two chunks. Chunk split (if any) is at ring wrap. */ |
---|
| 134 | if (((c ^ p) & EVTCHN_RING_SIZE) != 0) { |
---|
| 135 | bytes1 = (EVTCHN_RING_SIZE - EVTCHN_RING_MASK(c)) * |
---|
| 136 | sizeof(evtchn_port_t); |
---|
| 137 | bytes2 = EVTCHN_RING_MASK(p) * sizeof(evtchn_port_t); |
---|
| 138 | } else { |
---|
| 139 | bytes1 = (p - c) * sizeof(evtchn_port_t); |
---|
| 140 | bytes2 = 0; |
---|
| 141 | } |
---|
| 142 | |
---|
| 143 | /* Truncate chunks according to caller's maximum byte count. */ |
---|
| 144 | if (bytes1 > count) { |
---|
| 145 | bytes1 = count; |
---|
| 146 | bytes2 = 0; |
---|
| 147 | } else if ((bytes1 + bytes2) > count) { |
---|
| 148 | bytes2 = count - bytes1; |
---|
| 149 | } |
---|
| 150 | |
---|
| 151 | rc = -EFAULT; |
---|
| 152 | if (copy_to_user(buf, &u->ring[EVTCHN_RING_MASK(c)], bytes1) || |
---|
| 153 | ((bytes2 != 0) && |
---|
| 154 | copy_to_user(&buf[bytes1], &u->ring[0], bytes2))) |
---|
| 155 | goto unlock_out; |
---|
| 156 | |
---|
| 157 | u->ring_cons += (bytes1 + bytes2) / sizeof(evtchn_port_t); |
---|
| 158 | rc = bytes1 + bytes2; |
---|
| 159 | |
---|
| 160 | unlock_out: |
---|
| 161 | mutex_unlock(&u->ring_cons_mutex); |
---|
| 162 | return rc; |
---|
| 163 | } |
---|
| 164 | |
---|
| 165 | static ssize_t evtchn_write(struct file *file, const char __user *buf, |
---|
| 166 | size_t count, loff_t *ppos) |
---|
| 167 | { |
---|
| 168 | int rc, i; |
---|
| 169 | evtchn_port_t *kbuf = (evtchn_port_t *)__get_free_page(GFP_KERNEL); |
---|
| 170 | struct per_user_data *u = file->private_data; |
---|
| 171 | |
---|
| 172 | if (kbuf == NULL) |
---|
| 173 | return -ENOMEM; |
---|
| 174 | |
---|
| 175 | /* Whole number of ports. */ |
---|
| 176 | count &= ~(sizeof(evtchn_port_t)-1); |
---|
| 177 | |
---|
| 178 | rc = 0; |
---|
| 179 | if (count == 0) |
---|
| 180 | goto out; |
---|
| 181 | |
---|
| 182 | if (count > PAGE_SIZE) |
---|
| 183 | count = PAGE_SIZE; |
---|
| 184 | |
---|
| 185 | rc = -EFAULT; |
---|
| 186 | if (copy_from_user(kbuf, buf, count) != 0) |
---|
| 187 | goto out; |
---|
| 188 | |
---|
| 189 | spin_lock_irq(&port_user_lock); |
---|
| 190 | for (i = 0; i < (count/sizeof(evtchn_port_t)); i++) |
---|
| 191 | if ((kbuf[i] < NR_EVENT_CHANNELS) && (port_user[kbuf[i]] == u)) |
---|
| 192 | unmask_evtchn(kbuf[i]); |
---|
| 193 | spin_unlock_irq(&port_user_lock); |
---|
| 194 | |
---|
| 195 | rc = count; |
---|
| 196 | |
---|
| 197 | out: |
---|
| 198 | free_page((unsigned long)kbuf); |
---|
| 199 | return rc; |
---|
| 200 | } |
---|
| 201 | |
---|
| 202 | static void evtchn_bind_to_user(struct per_user_data *u, int port) |
---|
| 203 | { |
---|
| 204 | spin_lock_irq(&port_user_lock); |
---|
| 205 | BUG_ON(port_user[port] != NULL); |
---|
| 206 | port_user[port] = u; |
---|
| 207 | unmask_evtchn(port); |
---|
| 208 | spin_unlock_irq(&port_user_lock); |
---|
| 209 | } |
---|
| 210 | |
---|
| 211 | static int evtchn_ioctl(struct inode *inode, struct file *file, |
---|
| 212 | unsigned int cmd, unsigned long arg) |
---|
| 213 | { |
---|
| 214 | int rc; |
---|
| 215 | struct per_user_data *u = file->private_data; |
---|
| 216 | void __user *uarg = (void __user *) arg; |
---|
| 217 | |
---|
| 218 | switch (cmd) { |
---|
| 219 | case IOCTL_EVTCHN_BIND_VIRQ: { |
---|
| 220 | struct ioctl_evtchn_bind_virq bind; |
---|
| 221 | struct evtchn_bind_virq bind_virq; |
---|
| 222 | |
---|
| 223 | rc = -EFAULT; |
---|
| 224 | if (copy_from_user(&bind, uarg, sizeof(bind))) |
---|
| 225 | break; |
---|
| 226 | |
---|
| 227 | bind_virq.virq = bind.virq; |
---|
| 228 | bind_virq.vcpu = 0; |
---|
| 229 | rc = HYPERVISOR_event_channel_op(EVTCHNOP_bind_virq, |
---|
| 230 | &bind_virq); |
---|
| 231 | if (rc != 0) |
---|
| 232 | break; |
---|
| 233 | |
---|
| 234 | rc = bind_virq.port; |
---|
| 235 | evtchn_bind_to_user(u, rc); |
---|
| 236 | break; |
---|
| 237 | } |
---|
| 238 | |
---|
| 239 | case IOCTL_EVTCHN_BIND_INTERDOMAIN: { |
---|
| 240 | struct ioctl_evtchn_bind_interdomain bind; |
---|
| 241 | struct evtchn_bind_interdomain bind_interdomain; |
---|
| 242 | |
---|
| 243 | rc = -EFAULT; |
---|
| 244 | if (copy_from_user(&bind, uarg, sizeof(bind))) |
---|
| 245 | break; |
---|
| 246 | |
---|
| 247 | bind_interdomain.remote_dom = bind.remote_domain; |
---|
| 248 | bind_interdomain.remote_port = bind.remote_port; |
---|
| 249 | rc = HYPERVISOR_event_channel_op(EVTCHNOP_bind_interdomain, |
---|
| 250 | &bind_interdomain); |
---|
| 251 | if (rc != 0) |
---|
| 252 | break; |
---|
| 253 | |
---|
| 254 | rc = bind_interdomain.local_port; |
---|
| 255 | evtchn_bind_to_user(u, rc); |
---|
| 256 | break; |
---|
| 257 | } |
---|
| 258 | |
---|
| 259 | case IOCTL_EVTCHN_BIND_UNBOUND_PORT: { |
---|
| 260 | struct ioctl_evtchn_bind_unbound_port bind; |
---|
| 261 | struct evtchn_alloc_unbound alloc_unbound; |
---|
| 262 | |
---|
| 263 | rc = -EFAULT; |
---|
| 264 | if (copy_from_user(&bind, uarg, sizeof(bind))) |
---|
| 265 | break; |
---|
| 266 | |
---|
| 267 | alloc_unbound.dom = DOMID_SELF; |
---|
| 268 | alloc_unbound.remote_dom = bind.remote_domain; |
---|
| 269 | rc = HYPERVISOR_event_channel_op(EVTCHNOP_alloc_unbound, |
---|
| 270 | &alloc_unbound); |
---|
| 271 | if (rc != 0) |
---|
| 272 | break; |
---|
| 273 | |
---|
| 274 | rc = alloc_unbound.port; |
---|
| 275 | evtchn_bind_to_user(u, rc); |
---|
| 276 | break; |
---|
| 277 | } |
---|
| 278 | |
---|
| 279 | case IOCTL_EVTCHN_UNBIND: { |
---|
| 280 | struct ioctl_evtchn_unbind unbind; |
---|
| 281 | struct evtchn_close close; |
---|
| 282 | int ret; |
---|
| 283 | |
---|
| 284 | rc = -EFAULT; |
---|
| 285 | if (copy_from_user(&unbind, uarg, sizeof(unbind))) |
---|
| 286 | break; |
---|
| 287 | |
---|
| 288 | rc = -EINVAL; |
---|
| 289 | if (unbind.port >= NR_EVENT_CHANNELS) |
---|
| 290 | break; |
---|
| 291 | |
---|
| 292 | spin_lock_irq(&port_user_lock); |
---|
| 293 | |
---|
| 294 | rc = -ENOTCONN; |
---|
| 295 | if (port_user[unbind.port] != u) { |
---|
| 296 | spin_unlock_irq(&port_user_lock); |
---|
| 297 | break; |
---|
| 298 | } |
---|
| 299 | |
---|
| 300 | port_user[unbind.port] = NULL; |
---|
| 301 | mask_evtchn(unbind.port); |
---|
| 302 | |
---|
| 303 | spin_unlock_irq(&port_user_lock); |
---|
| 304 | |
---|
| 305 | close.port = unbind.port; |
---|
| 306 | ret = HYPERVISOR_event_channel_op(EVTCHNOP_close, &close); |
---|
| 307 | BUG_ON(ret); |
---|
| 308 | |
---|
| 309 | rc = 0; |
---|
| 310 | break; |
---|
| 311 | } |
---|
| 312 | |
---|
| 313 | case IOCTL_EVTCHN_NOTIFY: { |
---|
| 314 | struct ioctl_evtchn_notify notify; |
---|
| 315 | |
---|
| 316 | rc = -EFAULT; |
---|
| 317 | if (copy_from_user(¬ify, uarg, sizeof(notify))) |
---|
| 318 | break; |
---|
| 319 | |
---|
| 320 | if (notify.port >= NR_EVENT_CHANNELS) { |
---|
| 321 | rc = -EINVAL; |
---|
| 322 | } else if (port_user[notify.port] != u) { |
---|
| 323 | rc = -ENOTCONN; |
---|
| 324 | } else { |
---|
| 325 | notify_remote_via_evtchn(notify.port); |
---|
| 326 | rc = 0; |
---|
| 327 | } |
---|
| 328 | break; |
---|
| 329 | } |
---|
| 330 | |
---|
| 331 | case IOCTL_EVTCHN_RESET: { |
---|
| 332 | /* Initialise the ring to empty. Clear errors. */ |
---|
| 333 | mutex_lock(&u->ring_cons_mutex); |
---|
| 334 | spin_lock_irq(&port_user_lock); |
---|
| 335 | u->ring_cons = u->ring_prod = u->ring_overflow = 0; |
---|
| 336 | spin_unlock_irq(&port_user_lock); |
---|
| 337 | mutex_unlock(&u->ring_cons_mutex); |
---|
| 338 | rc = 0; |
---|
| 339 | break; |
---|
| 340 | } |
---|
| 341 | |
---|
| 342 | default: |
---|
| 343 | rc = -ENOSYS; |
---|
| 344 | break; |
---|
| 345 | } |
---|
| 346 | |
---|
| 347 | return rc; |
---|
| 348 | } |
---|
| 349 | |
---|
| 350 | static unsigned int evtchn_poll(struct file *file, poll_table *wait) |
---|
| 351 | { |
---|
| 352 | unsigned int mask = POLLOUT | POLLWRNORM; |
---|
| 353 | struct per_user_data *u = file->private_data; |
---|
| 354 | |
---|
| 355 | poll_wait(file, &u->evtchn_wait, wait); |
---|
| 356 | if (u->ring_cons != u->ring_prod) |
---|
| 357 | mask |= POLLIN | POLLRDNORM; |
---|
| 358 | if (u->ring_overflow) |
---|
| 359 | mask = POLLERR; |
---|
| 360 | return mask; |
---|
| 361 | } |
---|
| 362 | |
---|
| 363 | static int evtchn_fasync(int fd, struct file *filp, int on) |
---|
| 364 | { |
---|
| 365 | struct per_user_data *u = filp->private_data; |
---|
| 366 | return fasync_helper(fd, filp, on, &u->evtchn_async_queue); |
---|
| 367 | } |
---|
| 368 | |
---|
| 369 | static int evtchn_open(struct inode *inode, struct file *filp) |
---|
| 370 | { |
---|
| 371 | struct per_user_data *u; |
---|
| 372 | |
---|
| 373 | if ((u = kmalloc(sizeof(*u), GFP_KERNEL)) == NULL) |
---|
| 374 | return -ENOMEM; |
---|
| 375 | |
---|
| 376 | memset(u, 0, sizeof(*u)); |
---|
| 377 | init_waitqueue_head(&u->evtchn_wait); |
---|
| 378 | |
---|
| 379 | u->ring = (evtchn_port_t *)__get_free_page(GFP_KERNEL); |
---|
| 380 | if (u->ring == NULL) { |
---|
| 381 | kfree(u); |
---|
| 382 | return -ENOMEM; |
---|
| 383 | } |
---|
| 384 | |
---|
| 385 | mutex_init(&u->ring_cons_mutex); |
---|
| 386 | |
---|
| 387 | filp->private_data = u; |
---|
| 388 | |
---|
| 389 | return 0; |
---|
| 390 | } |
---|
| 391 | |
---|
| 392 | static int evtchn_release(struct inode *inode, struct file *filp) |
---|
| 393 | { |
---|
| 394 | int i; |
---|
| 395 | struct per_user_data *u = filp->private_data; |
---|
| 396 | struct evtchn_close close; |
---|
| 397 | |
---|
| 398 | spin_lock_irq(&port_user_lock); |
---|
| 399 | |
---|
| 400 | free_page((unsigned long)u->ring); |
---|
| 401 | |
---|
| 402 | for (i = 0; i < NR_EVENT_CHANNELS; i++) { |
---|
| 403 | int ret; |
---|
| 404 | if (port_user[i] != u) |
---|
| 405 | continue; |
---|
| 406 | |
---|
| 407 | port_user[i] = NULL; |
---|
| 408 | mask_evtchn(i); |
---|
| 409 | |
---|
| 410 | close.port = i; |
---|
| 411 | ret = HYPERVISOR_event_channel_op(EVTCHNOP_close, &close); |
---|
| 412 | BUG_ON(ret); |
---|
| 413 | } |
---|
| 414 | |
---|
| 415 | spin_unlock_irq(&port_user_lock); |
---|
| 416 | |
---|
| 417 | kfree(u); |
---|
| 418 | |
---|
| 419 | return 0; |
---|
| 420 | } |
---|
| 421 | |
---|
| 422 | static const struct file_operations evtchn_fops = { |
---|
| 423 | .owner = THIS_MODULE, |
---|
| 424 | .read = evtchn_read, |
---|
| 425 | .write = evtchn_write, |
---|
| 426 | .ioctl = evtchn_ioctl, |
---|
| 427 | .poll = evtchn_poll, |
---|
| 428 | .fasync = evtchn_fasync, |
---|
| 429 | .open = evtchn_open, |
---|
| 430 | .release = evtchn_release, |
---|
| 431 | }; |
---|
| 432 | |
---|
| 433 | static struct miscdevice evtchn_miscdev = { |
---|
| 434 | .minor = MISC_DYNAMIC_MINOR, |
---|
| 435 | .name = "evtchn", |
---|
| 436 | .fops = &evtchn_fops, |
---|
| 437 | }; |
---|
| 438 | |
---|
| 439 | static int __init evtchn_init(void) |
---|
| 440 | { |
---|
| 441 | int err; |
---|
| 442 | |
---|
| 443 | if (!is_running_on_xen()) |
---|
| 444 | return -ENODEV; |
---|
| 445 | |
---|
| 446 | spin_lock_init(&port_user_lock); |
---|
| 447 | memset(port_user, 0, sizeof(port_user)); |
---|
| 448 | |
---|
| 449 | /* Create '/dev/misc/evtchn'. */ |
---|
| 450 | err = misc_register(&evtchn_miscdev); |
---|
| 451 | if (err != 0) { |
---|
| 452 | printk(KERN_ALERT "Could not register /dev/misc/evtchn\n"); |
---|
| 453 | return err; |
---|
| 454 | } |
---|
| 455 | |
---|
| 456 | printk("Event-channel device installed.\n"); |
---|
| 457 | |
---|
| 458 | return 0; |
---|
| 459 | } |
---|
| 460 | |
---|
| 461 | static void evtchn_cleanup(void) |
---|
| 462 | { |
---|
| 463 | misc_deregister(&evtchn_miscdev); |
---|
| 464 | } |
---|
| 465 | |
---|
| 466 | module_init(evtchn_init); |
---|
| 467 | module_exit(evtchn_cleanup); |
---|
| 468 | |
---|
| 469 | MODULE_LICENSE("Dual BSD/GPL"); |
---|