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>
44 #include <linux/stat.h>
45 #include <linux/poll.h>
52 #include <xen/events.h>
54 #include <asm/xen/hypervisor.h>
60 #define EVTCHN_RING_SIZE (PAGE_SIZE / sizeof(evtchn_port_t))
61 #define EVTCHN_RING_MASK(_i) ((_i)&(EVTCHN_RING_SIZE-1))
76 static unsigned long *port_user;
84 static inline void set_port_user(
unsigned port,
struct per_user_data *
u)
89 static inline bool get_port_enabled(
unsigned port)
91 return port_user[
port] & 1;
94 static inline void set_port_enabled(
unsigned port,
bool enabled)
99 port_user[
port] &= ~1;
104 unsigned int port = (
unsigned long)data;
107 spin_lock(&port_user_lock);
109 u = get_port_user(port);
111 WARN(!get_port_enabled(port),
112 "Interrupt for port %d, but apparently not enabled; per-user %p\n",
116 set_port_enabled(port,
false);
129 spin_unlock(&port_user_lock);
135 size_t count, loff_t *ppos)
138 unsigned int c,
p, bytes1 = 0, bytes2 = 0;
184 if (bytes1 > count) {
187 }
else if ((bytes1 + bytes2) >
count) {
188 bytes2 = count - bytes1;
199 rc = bytes1 + bytes2;
206 static ssize_t evtchn_write(
struct file *file,
const char __user *buf,
207 size_t count, loff_t *ppos)
230 spin_lock_irq(&port_user_lock);
233 unsigned port = kbuf[
i];
236 get_port_user(port) == u &&
237 !get_port_enabled(port)) {
238 set_port_enabled(port,
true);
243 spin_unlock_irq(&port_user_lock);
252 static int evtchn_bind_to_user(
struct per_user_data *u,
int port)
265 set_port_user(port, u);
266 set_port_enabled(port,
true);
269 u->
name, (
void *)(
unsigned long)port);
276 static void evtchn_unbind_from_user(
struct per_user_data *u,
int port)
282 set_port_user(port,
NULL);
285 static long evtchn_ioctl(
struct file *file,
286 unsigned int cmd,
unsigned long arg)
304 bind_virq.virq = bind.virq;
311 rc = evtchn_bind_to_user(u, bind_virq.port);
325 bind_interdomain.remote_dom = bind.remote_domain;
326 bind_interdomain.remote_port = bind.remote_port;
332 rc = evtchn_bind_to_user(u, bind_interdomain.local_port);
334 rc = bind_interdomain.local_port;
347 alloc_unbound.remote_dom = bind.remote_domain;
353 rc = evtchn_bind_to_user(u, alloc_unbound.port);
355 rc = alloc_unbound.port;
370 spin_lock_irq(&port_user_lock);
373 if (get_port_user(unbind.port) != u) {
374 spin_unlock_irq(&port_user_lock);
380 spin_unlock_irq(&port_user_lock);
382 evtchn_unbind_from_user(u, unbind.port);
397 }
else if (get_port_user(
notify.port) != u) {
400 notify_remote_via_evtchn(
notify.port);
409 spin_lock_irq(&port_user_lock);
411 spin_unlock_irq(&port_user_lock);
426 static unsigned int evtchn_poll(
struct file *file,
poll_table *
wait)
439 static int evtchn_fasync(
int fd,
struct file *filp,
int on)
445 static int evtchn_open(
struct inode *
inode,
struct file *filp)
476 static int evtchn_release(
struct inode *inode,
struct file *filp)
481 spin_lock_irq(&port_user_lock);
486 if (get_port_user(i) != u)
492 spin_unlock_irq(&port_user_lock);
495 if (get_port_user(i) != u)
498 evtchn_unbind_from_user(get_port_user(i), i);
510 .write = evtchn_write,
511 .unlocked_ioctl = evtchn_ioctl,
513 .fasync = evtchn_fasync,
515 .release = evtchn_release,
521 .name =
"xen/evtchn",
522 .fops = &evtchn_fops,
524 static int __init evtchn_init(
void)
531 port_user = kcalloc(NR_EVENT_CHANNELS,
sizeof(*port_user),
GFP_KERNEL);
532 if (port_user ==
NULL)
549 static void __exit evtchn_cleanup(
void)