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