direct-io.hg

view linux-2.6-xen-sparse/drivers/xen/evtchn/evtchn.c @ 8151:f5b119533cc8

Define explicit evtchn_port_t type (32 bits) and plumb up
to user space thru /dev/xen/evtchn.

Signed-off-by: Keir Fraser <keir@xensource.com>
author kaf24@firebug.cl.cam.ac.uk
date Thu Dec 01 15:22:22 2005 +0100 (2005-12-01)
parents 6cc603ac3f38
children fd9b2c1bb577
line source
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 file may be distributed separately from the Linux kernel, or
10 * incorporated into other software packages, subject to the following license:
11 *
12 * Permission is hereby granted, free of charge, to any person obtaining a copy
13 * of this source file (the "Software"), to deal in the Software without
14 * restriction, including without limitation the rights to use, copy, modify,
15 * merge, publish, distribute, sublicense, and/or sell copies of the Software,
16 * and to permit persons to whom the Software is furnished to do so, subject to
17 * the following conditions:
18 *
19 * The above copyright notice and this permission notice shall be included in
20 * all copies or substantial portions of the Software.
21 *
22 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
27 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
28 * IN THE SOFTWARE.
29 */
31 #include <linux/config.h>
32 #include <linux/module.h>
33 #include <linux/kernel.h>
34 #include <linux/sched.h>
35 #include <linux/slab.h>
36 #include <linux/string.h>
37 #include <linux/errno.h>
38 #include <linux/fs.h>
39 #include <linux/errno.h>
40 #include <linux/miscdevice.h>
41 #include <linux/major.h>
42 #include <linux/proc_fs.h>
43 #include <linux/stat.h>
44 #include <linux/poll.h>
45 #include <linux/irq.h>
46 #include <linux/init.h>
47 #include <linux/gfp.h>
48 #include <asm-xen/evtchn.h>
49 #include <asm-xen/linux-public/evtchn.h>
51 struct per_user_data {
52 /* Notification ring, accessed via /dev/xen/evtchn. */
53 #define EVTCHN_RING_SIZE (PAGE_SIZE / sizeof(evtchn_port_t))
54 #define EVTCHN_RING_MASK(_i) ((_i)&(EVTCHN_RING_SIZE-1))
55 evtchn_port_t *ring;
56 unsigned int ring_cons, ring_prod, ring_overflow;
58 /* Processes wait on this queue when ring is empty. */
59 wait_queue_head_t evtchn_wait;
60 struct fasync_struct *evtchn_async_queue;
61 };
63 /* Who's bound to each port? */
64 static struct per_user_data *port_user[NR_EVENT_CHANNELS];
65 static spinlock_t port_user_lock;
67 void evtchn_device_upcall(int port)
68 {
69 struct per_user_data *u;
71 spin_lock(&port_user_lock);
73 mask_evtchn(port);
74 clear_evtchn(port);
76 if ((u = port_user[port]) != NULL) {
77 if ((u->ring_prod - u->ring_cons) < EVTCHN_RING_SIZE) {
78 u->ring[EVTCHN_RING_MASK(u->ring_prod)] = port;
79 if (u->ring_cons == u->ring_prod++) {
80 wake_up_interruptible(&u->evtchn_wait);
81 kill_fasync(&u->evtchn_async_queue,
82 SIGIO, POLL_IN);
83 }
84 } else {
85 u->ring_overflow = 1;
86 }
87 }
89 spin_unlock(&port_user_lock);
90 }
92 static ssize_t evtchn_read(struct file *file, char __user *buf,
93 size_t count, loff_t *ppos)
94 {
95 int rc;
96 unsigned int c, p, bytes1 = 0, bytes2 = 0;
97 struct per_user_data *u = file->private_data;
99 /* Whole number of ports. */
100 count &= ~(sizeof(evtchn_port_t)-1);
102 if (count == 0)
103 return 0;
105 if (count > PAGE_SIZE)
106 count = PAGE_SIZE;
108 for (;;) {
109 if (u->ring_overflow)
110 return -EFBIG;
112 if ((c = u->ring_cons) != (p = u->ring_prod))
113 break;
115 if (file->f_flags & O_NONBLOCK)
116 return -EAGAIN;
118 rc = wait_event_interruptible(
119 u->evtchn_wait, u->ring_cons != u->ring_prod);
120 if (rc)
121 return rc;
122 }
124 /* Byte lengths of two chunks. Chunk split (if any) is at ring wrap. */
125 if (((c ^ p) & EVTCHN_RING_SIZE) != 0) {
126 bytes1 = (EVTCHN_RING_SIZE - EVTCHN_RING_MASK(c)) *
127 sizeof(evtchn_port_t);
128 bytes2 = EVTCHN_RING_MASK(p) * sizeof(evtchn_port_t);
129 } else {
130 bytes1 = (p - c) * sizeof(evtchn_port_t);
131 bytes2 = 0;
132 }
134 /* Truncate chunks according to caller's maximum byte count. */
135 if (bytes1 > count) {
136 bytes1 = count;
137 bytes2 = 0;
138 } else if ((bytes1 + bytes2) > count) {
139 bytes2 = count - bytes1;
140 }
142 if (copy_to_user(buf, &u->ring[EVTCHN_RING_MASK(c)], bytes1) ||
143 ((bytes2 != 0) &&
144 copy_to_user(&buf[bytes1], &u->ring[0], bytes2)))
145 return -EFAULT;
147 u->ring_cons += (bytes1 + bytes2) / sizeof(evtchn_port_t);
149 return bytes1 + bytes2;
150 }
152 static ssize_t evtchn_write(struct file *file, const char __user *buf,
153 size_t count, loff_t *ppos)
154 {
155 int rc, i;
156 evtchn_port_t *kbuf = (evtchn_port_t *)__get_free_page(GFP_KERNEL);
157 struct per_user_data *u = file->private_data;
159 if (kbuf == NULL)
160 return -ENOMEM;
162 /* Whole number of ports. */
163 count &= ~(sizeof(evtchn_port_t)-1);
165 if (count == 0) {
166 rc = 0;
167 goto out;
168 }
170 if (count > PAGE_SIZE)
171 count = PAGE_SIZE;
173 if (copy_from_user(kbuf, buf, count) != 0) {
174 rc = -EFAULT;
175 goto out;
176 }
178 spin_lock_irq(&port_user_lock);
179 for (i = 0; i < (count/sizeof(evtchn_port_t)); i++)
180 if ((kbuf[i] < NR_EVENT_CHANNELS) && (port_user[kbuf[i]] == u))
181 unmask_evtchn(kbuf[i]);
182 spin_unlock_irq(&port_user_lock);
184 rc = count;
186 out:
187 free_page((unsigned long)kbuf);
188 return rc;
189 }
191 static void evtchn_bind_to_user(struct per_user_data *u, int port)
192 {
193 spin_lock_irq(&port_user_lock);
194 BUG_ON(port_user[port] != NULL);
195 port_user[port] = u;
196 unmask_evtchn(port);
197 spin_unlock_irq(&port_user_lock);
198 }
200 static int evtchn_ioctl(struct inode *inode, struct file *file,
201 unsigned int cmd, unsigned long arg)
202 {
203 int rc;
204 struct per_user_data *u = file->private_data;
205 void __user *uarg = (void __user *) arg;
206 evtchn_op_t op = { 0 };
208 switch (cmd) {
209 case IOCTL_EVTCHN_BIND_VIRQ: {
210 struct ioctl_evtchn_bind_virq bind;
212 rc = -EFAULT;
213 if (copy_from_user(&bind, uarg, sizeof(bind)))
214 break;
216 op.cmd = EVTCHNOP_bind_virq;
217 op.u.bind_virq.virq = bind.virq;
218 op.u.bind_virq.vcpu = 0;
219 rc = HYPERVISOR_event_channel_op(&op);
220 if (rc != 0)
221 break;
223 rc = op.u.bind_virq.port;
224 evtchn_bind_to_user(u, rc);
225 break;
226 }
228 case IOCTL_EVTCHN_BIND_INTERDOMAIN: {
229 struct ioctl_evtchn_bind_interdomain bind;
231 rc = -EFAULT;
232 if (copy_from_user(&bind, uarg, sizeof(bind)))
233 break;
235 op.cmd = EVTCHNOP_bind_interdomain;
236 op.u.bind_interdomain.remote_dom = bind.remote_domain;
237 op.u.bind_interdomain.remote_port = bind.remote_port;
238 rc = HYPERVISOR_event_channel_op(&op);
239 if (rc != 0)
240 break;
242 rc = op.u.bind_interdomain.local_port;
243 evtchn_bind_to_user(u, rc);
244 break;
245 }
247 case IOCTL_EVTCHN_BIND_UNBOUND_PORT: {
248 struct ioctl_evtchn_bind_unbound_port bind;
250 rc = -EFAULT;
251 if (copy_from_user(&bind, uarg, sizeof(bind)))
252 break;
254 op.cmd = EVTCHNOP_alloc_unbound;
255 op.u.alloc_unbound.dom = DOMID_SELF;
256 op.u.alloc_unbound.remote_dom = bind.remote_domain;
257 rc = HYPERVISOR_event_channel_op(&op);
258 if (rc != 0)
259 break;
261 rc = op.u.alloc_unbound.port;
262 evtchn_bind_to_user(u, rc);
263 break;
264 }
266 case IOCTL_EVTCHN_UNBIND: {
267 struct ioctl_evtchn_unbind unbind;
268 int ret;
270 rc = -EFAULT;
271 if (copy_from_user(&unbind, uarg, sizeof(unbind)))
272 break;
274 rc = -EINVAL;
275 if (unbind.port >= NR_EVENT_CHANNELS)
276 break;
278 spin_lock_irq(&port_user_lock);
280 rc = -ENOTCONN;
281 if (port_user[unbind.port] != u) {
282 spin_unlock_irq(&port_user_lock);
283 break;
284 }
286 port_user[unbind.port] = NULL;
287 mask_evtchn(unbind.port);
289 spin_unlock_irq(&port_user_lock);
291 op.cmd = EVTCHNOP_close;
292 op.u.close.port = unbind.port;
293 ret = HYPERVISOR_event_channel_op(&op);
294 BUG_ON(ret);
296 rc = 0;
297 break;
298 }
300 case IOCTL_EVTCHN_NOTIFY: {
301 struct ioctl_evtchn_notify notify;
303 rc = -EFAULT;
304 if (copy_from_user(&notify, uarg, sizeof(notify)))
305 break;
307 if (notify.port >= NR_EVENT_CHANNELS) {
308 rc = -EINVAL;
309 } else if (port_user[notify.port] != u) {
310 rc = -ENOTCONN;
311 } else {
312 notify_remote_via_evtchn(notify.port);
313 rc = 0;
314 }
315 break;
316 }
318 case IOCTL_EVTCHN_RESET: {
319 /* Initialise the ring to empty. Clear errors. */
320 spin_lock_irq(&port_user_lock);
321 u->ring_cons = u->ring_prod = u->ring_overflow = 0;
322 spin_unlock_irq(&port_user_lock);
323 rc = 0;
324 break;
325 }
327 default:
328 rc = -ENOSYS;
329 break;
330 }
332 return rc;
333 }
335 static unsigned int evtchn_poll(struct file *file, poll_table *wait)
336 {
337 unsigned int mask = POLLOUT | POLLWRNORM;
338 struct per_user_data *u = file->private_data;
340 poll_wait(file, &u->evtchn_wait, wait);
341 if (u->ring_cons != u->ring_prod)
342 mask |= POLLIN | POLLRDNORM;
343 if (u->ring_overflow)
344 mask = POLLERR;
345 return mask;
346 }
348 static int evtchn_fasync(int fd, struct file *filp, int on)
349 {
350 struct per_user_data *u = filp->private_data;
351 return fasync_helper(fd, filp, on, &u->evtchn_async_queue);
352 }
354 static int evtchn_open(struct inode *inode, struct file *filp)
355 {
356 struct per_user_data *u;
358 if ((u = kmalloc(sizeof(*u), GFP_KERNEL)) == NULL)
359 return -ENOMEM;
361 memset(u, 0, sizeof(*u));
362 init_waitqueue_head(&u->evtchn_wait);
364 u->ring = (evtchn_port_t *)__get_free_page(GFP_KERNEL);
365 if (u->ring == NULL) {
366 kfree(u);
367 return -ENOMEM;
368 }
370 filp->private_data = u;
372 return 0;
373 }
375 static int evtchn_release(struct inode *inode, struct file *filp)
376 {
377 int i;
378 struct per_user_data *u = filp->private_data;
379 evtchn_op_t op = { 0 };
381 spin_lock_irq(&port_user_lock);
383 free_page((unsigned long)u->ring);
385 for (i = 0; i < NR_EVENT_CHANNELS; i++) {
386 int ret;
387 if (port_user[i] != u)
388 continue;
390 port_user[i] = NULL;
391 mask_evtchn(i);
393 op.cmd = EVTCHNOP_close;
394 op.u.close.port = i;
395 ret = HYPERVISOR_event_channel_op(&op);
396 BUG_ON(ret);
397 }
399 spin_unlock_irq(&port_user_lock);
401 kfree(u);
403 return 0;
404 }
406 static struct file_operations evtchn_fops = {
407 .owner = THIS_MODULE,
408 .read = evtchn_read,
409 .write = evtchn_write,
410 .ioctl = evtchn_ioctl,
411 .poll = evtchn_poll,
412 .fasync = evtchn_fasync,
413 .open = evtchn_open,
414 .release = evtchn_release,
415 };
417 static struct miscdevice evtchn_miscdev = {
418 .minor = EVTCHN_MINOR,
419 .name = "evtchn",
420 .fops = &evtchn_fops,
421 .devfs_name = "misc/evtchn",
422 };
424 static int __init evtchn_init(void)
425 {
426 int err;
428 spin_lock_init(&port_user_lock);
429 memset(port_user, 0, sizeof(port_user));
431 /* Create '/dev/misc/evtchn'. */
432 err = misc_register(&evtchn_miscdev);
433 if (err != 0) {
434 printk(KERN_ALERT "Could not register /dev/misc/evtchn\n");
435 return err;
436 }
438 printk("Event-channel device installed.\n");
440 return 0;
441 }
443 static void evtchn_cleanup(void)
444 {
445 misc_deregister(&evtchn_miscdev);
446 }
448 module_init(evtchn_init);
449 module_exit(evtchn_cleanup);
451 /*
452 * Local variables:
453 * c-file-style: "linux"
454 * indent-tabs-mode: t
455 * c-indent-level: 8
456 * c-basic-offset: 8
457 * tab-width: 8
458 * End:
459 */