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