Blame view

drivers/xen/evtchn.c 16.7 KB
f7116284c   Ian Campbell   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
  /******************************************************************************
   * 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.
   */
283c0972d   Joe Perches   xen: Convert prin...
33
  #define pr_fmt(fmt) "xen:" KBUILD_MODNAME ": " fmt
f7116284c   Ian Campbell   xen: add /dev/xen...
34
35
36
37
38
39
40
  #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   Ian Campbell   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   Ian Campbell   xen: add /dev/xen...
48
49
  #include <linux/mutex.h>
  #include <linux/cpu.h>
862001549   David Vrabel   xen/evtchn: dynam...
50
51
  #include <linux/mm.h>
  #include <linux/vmalloc.h>
1ccbf5344   Jeremy Fitzhardinge   xen: move Xen-tes...
52
53
  
  #include <xen/xen.h>
f7116284c   Ian Campbell   xen: add /dev/xen...
54
55
  #include <xen/events.h>
  #include <xen/evtchn.h>
cbbb46823   Vitaly Kuznetsov   xen/evtchn: use x...
56
  #include <xen/xen-ops.h>
f7116284c   Ian Campbell   xen: add /dev/xen...
57
58
59
  #include <asm/xen/hypervisor.h>
  
  struct per_user_data {
0a4666b53   Jeremy Fitzhardinge   xen/dev-evtchn: c...
60
  	struct mutex bind_mutex; /* serialize bind/unbind operations */
73cc4bb0c   David Vrabel   xen/evtchn: impro...
61
  	struct rb_root evtchns;
862001549   David Vrabel   xen/evtchn: dynam...
62
  	unsigned int nr_evtchns;
0a4666b53   Jeremy Fitzhardinge   xen/dev-evtchn: c...
63

f7116284c   Ian Campbell   xen: add /dev/xen...
64
  	/* Notification ring, accessed via /dev/xen/evtchn. */
862001549   David Vrabel   xen/evtchn: dynam...
65
  	unsigned int ring_size;
f7116284c   Ian Campbell   xen: add /dev/xen...
66
67
68
  	evtchn_port_t *ring;
  	unsigned int ring_cons, ring_prod, ring_overflow;
  	struct mutex ring_cons_mutex; /* protect against concurrent readers */
73cc4bb0c   David Vrabel   xen/evtchn: impro...
69
  	spinlock_t ring_prod_lock; /* product against concurrent interrupts */
f7116284c   Ian Campbell   xen: add /dev/xen...
70
71
72
73
74
  
  	/* Processes wait on this queue when ring is empty. */
  	wait_queue_head_t evtchn_wait;
  	struct fasync_struct *evtchn_async_queue;
  	const char *name;
fbc872c38   David Vrabel   xen/evtchn: add I...
75
76
  
  	domid_t restrict_domid;
f7116284c   Ian Campbell   xen: add /dev/xen...
77
  };
fbc872c38   David Vrabel   xen/evtchn: add I...
78
  #define UNRESTRICTED_DOMID ((domid_t)-1)
73cc4bb0c   David Vrabel   xen/evtchn: impro...
79
80
81
  struct user_evtchn {
  	struct rb_node node;
  	struct per_user_data *user;
0102e4efd   Yan Yankovskyi   xen: Use evtchn_t...
82
  	evtchn_port_t port;
73cc4bb0c   David Vrabel   xen/evtchn: impro...
83
84
  	bool enabled;
  };
f7116284c   Ian Campbell   xen: add /dev/xen...
85

862001549   David Vrabel   xen/evtchn: dynam...
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
  static void evtchn_free_ring(evtchn_port_t *ring)
  {
  	kvfree(ring);
  }
  
  static unsigned int evtchn_ring_offset(struct per_user_data *u,
  				       unsigned int idx)
  {
  	return idx & (u->ring_size - 1);
  }
  
  static evtchn_port_t *evtchn_ring_entry(struct per_user_data *u,
  					unsigned int idx)
  {
  	return u->ring + evtchn_ring_offset(u, idx);
  }
73cc4bb0c   David Vrabel   xen/evtchn: impro...
102
  static int add_evtchn(struct per_user_data *u, struct user_evtchn *evtchn)
e3cc067b0   Jeremy Fitzhardinge   xen/evtchn: track...
103
  {
73cc4bb0c   David Vrabel   xen/evtchn: impro...
104
  	struct rb_node **new = &(u->evtchns.rb_node), *parent = NULL;
e3cc067b0   Jeremy Fitzhardinge   xen/evtchn: track...
105

862001549   David Vrabel   xen/evtchn: dynam...
106
  	u->nr_evtchns++;
73cc4bb0c   David Vrabel   xen/evtchn: impro...
107
108
  	while (*new) {
  		struct user_evtchn *this;
2f60b2883   Geliang Tang   xen/evtchn: use r...
109
  		this = rb_entry(*new, struct user_evtchn, node);
73cc4bb0c   David Vrabel   xen/evtchn: impro...
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
  
  		parent = *new;
  		if (this->port < evtchn->port)
  			new = &((*new)->rb_left);
  		else if (this->port > evtchn->port)
  			new = &((*new)->rb_right);
  		else
  			return -EEXIST;
  	}
  
  	/* Add new node and rebalance tree. */
  	rb_link_node(&evtchn->node, parent, new);
  	rb_insert_color(&evtchn->node, &u->evtchns);
  
  	return 0;
e3cc067b0   Jeremy Fitzhardinge   xen/evtchn: track...
125
  }
73cc4bb0c   David Vrabel   xen/evtchn: impro...
126
  static void del_evtchn(struct per_user_data *u, struct user_evtchn *evtchn)
e3cc067b0   Jeremy Fitzhardinge   xen/evtchn: track...
127
  {
862001549   David Vrabel   xen/evtchn: dynam...
128
  	u->nr_evtchns--;
73cc4bb0c   David Vrabel   xen/evtchn: impro...
129
130
  	rb_erase(&evtchn->node, &u->evtchns);
  	kfree(evtchn);
e3cc067b0   Jeremy Fitzhardinge   xen/evtchn: track...
131
  }
0102e4efd   Yan Yankovskyi   xen: Use evtchn_t...
132
133
  static struct user_evtchn *find_evtchn(struct per_user_data *u,
  				       evtchn_port_t port)
e3cc067b0   Jeremy Fitzhardinge   xen/evtchn: track...
134
  {
73cc4bb0c   David Vrabel   xen/evtchn: impro...
135
136
137
138
  	struct rb_node *node = u->evtchns.rb_node;
  
  	while (node) {
  		struct user_evtchn *evtchn;
2f60b2883   Geliang Tang   xen/evtchn: use r...
139
  		evtchn = rb_entry(node, struct user_evtchn, node);
73cc4bb0c   David Vrabel   xen/evtchn: impro...
140
141
142
143
144
145
146
147
148
  
  		if (evtchn->port < port)
  			node = node->rb_left;
  		else if (evtchn->port > port)
  			node = node->rb_right;
  		else
  			return evtchn;
  	}
  	return NULL;
e3cc067b0   Jeremy Fitzhardinge   xen/evtchn: track...
149
  }
70697d540   Jeremy Fitzhardinge   xen/evtchn: add m...
150
  static irqreturn_t evtchn_interrupt(int irq, void *data)
f7116284c   Ian Campbell   xen: add /dev/xen...
151
  {
73cc4bb0c   David Vrabel   xen/evtchn: impro...
152
153
  	struct user_evtchn *evtchn = data;
  	struct per_user_data *u = evtchn->user;
e3cc067b0   Jeremy Fitzhardinge   xen/evtchn: track...
154

73cc4bb0c   David Vrabel   xen/evtchn: impro...
155
  	WARN(!evtchn->enabled,
0102e4efd   Yan Yankovskyi   xen: Use evtchn_t...
156
157
  	     "Interrupt for port %u, but apparently not enabled; per-user %p
  ",
73cc4bb0c   David Vrabel   xen/evtchn: impro...
158
  	     evtchn->port, u);
f7116284c   Ian Campbell   xen: add /dev/xen...
159

73cc4bb0c   David Vrabel   xen/evtchn: impro...
160
161
162
  	evtchn->enabled = false;
  
  	spin_lock(&u->ring_prod_lock);
f7116284c   Ian Campbell   xen: add /dev/xen...
163

862001549   David Vrabel   xen/evtchn: dynam...
164
165
  	if ((u->ring_prod - u->ring_cons) < u->ring_size) {
  		*evtchn_ring_entry(u, u->ring_prod) = evtchn->port;
f7116284c   Ian Campbell   xen: add /dev/xen...
166
167
168
169
170
171
  		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   Jeremy Fitzhardinge   xen/evtchn: track...
172
  	} else
f7116284c   Ian Campbell   xen: add /dev/xen...
173
  		u->ring_overflow = 1;
f7116284c   Ian Campbell   xen: add /dev/xen...
174

73cc4bb0c   David Vrabel   xen/evtchn: impro...
175
  	spin_unlock(&u->ring_prod_lock);
f7116284c   Ian Campbell   xen: add /dev/xen...
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
  
  	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. */
862001549   David Vrabel   xen/evtchn: dynam...
220
221
  	if (((c ^ p) & u->ring_size) != 0) {
  		bytes1 = (u->ring_size - evtchn_ring_offset(u, c)) *
f7116284c   Ian Campbell   xen: add /dev/xen...
222
  			sizeof(evtchn_port_t);
862001549   David Vrabel   xen/evtchn: dynam...
223
  		bytes2 = evtchn_ring_offset(u, p) * sizeof(evtchn_port_t);
f7116284c   Ian Campbell   xen: add /dev/xen...
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
  	} 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. */
862001549   David Vrabel   xen/evtchn: dynam...
239
  	if (copy_to_user(buf, evtchn_ring_entry(u, c), bytes1) ||
f7116284c   Ian Campbell   xen: add /dev/xen...
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
  	    ((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;
73cc4bb0c   David Vrabel   xen/evtchn: impro...
275
  	mutex_lock(&u->bind_mutex);
e3cc067b0   Jeremy Fitzhardinge   xen/evtchn: track...
276
277
  
  	for (i = 0; i < (count/sizeof(evtchn_port_t)); i++) {
0102e4efd   Yan Yankovskyi   xen: Use evtchn_t...
278
  		evtchn_port_t port = kbuf[i];
73cc4bb0c   David Vrabel   xen/evtchn: impro...
279
  		struct user_evtchn *evtchn;
e3cc067b0   Jeremy Fitzhardinge   xen/evtchn: track...
280

73cc4bb0c   David Vrabel   xen/evtchn: impro...
281
282
283
  		evtchn = find_evtchn(u, port);
  		if (evtchn && !evtchn->enabled) {
  			evtchn->enabled = true;
c44b849ce   Juergen Gross   xen/events: switc...
284
  			xen_irq_lateeoi(irq_from_evtchn(port), 0);
e3cc067b0   Jeremy Fitzhardinge   xen/evtchn: track...
285
286
  		}
  	}
73cc4bb0c   David Vrabel   xen/evtchn: impro...
287
  	mutex_unlock(&u->bind_mutex);
f7116284c   Ian Campbell   xen: add /dev/xen...
288
289
290
291
292
293
294
  
  	rc = count;
  
   out:
  	free_page((unsigned long)kbuf);
  	return rc;
  }
862001549   David Vrabel   xen/evtchn: dynam...
295
296
297
298
  static int evtchn_resize_ring(struct per_user_data *u)
  {
  	unsigned int new_size;
  	evtchn_port_t *new_ring, *old_ring;
862001549   David Vrabel   xen/evtchn: dynam...
299
300
301
302
303
304
305
306
307
308
309
310
  
  	/*
  	 * Ensure the ring is large enough to capture all possible
  	 * events. i.e., one free slot for each bound event.
  	 */
  	if (u->nr_evtchns <= u->ring_size)
  		return 0;
  
  	if (u->ring_size == 0)
  		new_size = 64;
  	else
  		new_size = 2 * u->ring_size;
344476e16   Kees Cook   treewide: kvmallo...
311
  	new_ring = kvmalloc_array(new_size, sizeof(*new_ring), GFP_KERNEL);
862001549   David Vrabel   xen/evtchn: dynam...
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
  	if (!new_ring)
  		return -ENOMEM;
  
  	old_ring = u->ring;
  
  	/*
  	 * Access to the ring contents is serialized by either the
  	 * prod /or/ cons lock so take both when resizing.
  	 */
  	mutex_lock(&u->ring_cons_mutex);
  	spin_lock_irq(&u->ring_prod_lock);
  
  	/*
  	 * Copy the old ring contents to the new ring.
  	 *
27e0e6385   Jan Beulich   xen/evtchn: fix r...
327
328
329
  	 * To take care of wrapping, a full ring, and the new index
  	 * pointing into the second half, simply copy the old contents
  	 * twice.
862001549   David Vrabel   xen/evtchn: dynam...
330
331
  	 *
  	 * +---------+    +------------------+
27e0e6385   Jan Beulich   xen/evtchn: fix r...
332
333
  	 * |34567  12| -> |34567  1234567  12|
  	 * +-----p-c-+    +-------c------p---+
862001549   David Vrabel   xen/evtchn: dynam...
334
  	 */
27e0e6385   Jan Beulich   xen/evtchn: fix r...
335
336
337
  	memcpy(new_ring, old_ring, u->ring_size * sizeof(*u->ring));
  	memcpy(new_ring + u->ring_size, old_ring,
  	       u->ring_size * sizeof(*u->ring));
862001549   David Vrabel   xen/evtchn: dynam...
338
339
340
341
342
343
344
345
346
347
348
  
  	u->ring = new_ring;
  	u->ring_size = new_size;
  
  	spin_unlock_irq(&u->ring_prod_lock);
  	mutex_unlock(&u->ring_cons_mutex);
  
  	evtchn_free_ring(old_ring);
  
  	return 0;
  }
0102e4efd   Yan Yankovskyi   xen: Use evtchn_t...
349
  static int evtchn_bind_to_user(struct per_user_data *u, evtchn_port_t port)
f7116284c   Ian Campbell   xen: add /dev/xen...
350
  {
73cc4bb0c   David Vrabel   xen/evtchn: impro...
351
352
  	struct user_evtchn *evtchn;
  	struct evtchn_close close;
f7116284c   Ian Campbell   xen: add /dev/xen...
353
  	int rc = 0;
0a4666b53   Jeremy Fitzhardinge   xen/dev-evtchn: c...
354
355
356
357
358
359
360
361
  	/*
  	 * 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.)
  	 */
73cc4bb0c   David Vrabel   xen/evtchn: impro...
362
363
364
365
366
367
368
369
370
371
372
373
  
  	evtchn = kzalloc(sizeof(*evtchn), GFP_KERNEL);
  	if (!evtchn)
  		return -ENOMEM;
  
  	evtchn->user = u;
  	evtchn->port = port;
  	evtchn->enabled = true; /* start enabled */
  
  	rc = add_evtchn(u, evtchn);
  	if (rc < 0)
  		goto err;
f7116284c   Ian Campbell   xen: add /dev/xen...
374

862001549   David Vrabel   xen/evtchn: dynam...
375
376
377
  	rc = evtchn_resize_ring(u);
  	if (rc < 0)
  		goto err;
c44b849ce   Juergen Gross   xen/events: switc...
378
379
  	rc = bind_evtchn_to_irqhandler_lateeoi(port, evtchn_interrupt, 0,
  					       u->name, evtchn);
73cc4bb0c   David Vrabel   xen/evtchn: impro...
380
381
  	if (rc < 0)
  		goto err;
0a4666b53   Jeremy Fitzhardinge   xen/dev-evtchn: c...
382

73cc4bb0c   David Vrabel   xen/evtchn: impro...
383
384
385
386
387
388
389
390
391
  	rc = evtchn_make_refcounted(port);
  	return rc;
  
  err:
  	/* bind failed, should close the port now */
  	close.port = port;
  	if (HYPERVISOR_event_channel_op(EVTCHNOP_close, &close) != 0)
  		BUG();
  	del_evtchn(u, evtchn);
f7116284c   Ian Campbell   xen: add /dev/xen...
392
393
  	return rc;
  }
73cc4bb0c   David Vrabel   xen/evtchn: impro...
394
395
  static void evtchn_unbind_from_user(struct per_user_data *u,
  				    struct user_evtchn *evtchn)
f7116284c   Ian Campbell   xen: add /dev/xen...
396
  {
73cc4bb0c   David Vrabel   xen/evtchn: impro...
397
  	int irq = irq_from_evtchn(evtchn->port);
f7116284c   Ian Campbell   xen: add /dev/xen...
398

e7e44e444   Wei Liu   xen: close evtchn...
399
  	BUG_ON(irq < 0);
73cc4bb0c   David Vrabel   xen/evtchn: impro...
400
  	unbind_from_irqhandler(irq, evtchn);
0a4666b53   Jeremy Fitzhardinge   xen/dev-evtchn: c...
401

73cc4bb0c   David Vrabel   xen/evtchn: impro...
402
  	del_evtchn(u, evtchn);
f7116284c   Ian Campbell   xen: add /dev/xen...
403
  }
c48f64ab4   Anoob Soman   xen-evtchn: Bind ...
404
  static DEFINE_PER_CPU(int, bind_last_selected_cpu);
0102e4efd   Yan Yankovskyi   xen: Use evtchn_t...
405
  static void evtchn_bind_interdom_next_vcpu(evtchn_port_t evtchn)
c48f64ab4   Anoob Soman   xen-evtchn: Bind ...
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
  {
  	unsigned int selected_cpu, irq;
  	struct irq_desc *desc;
  	unsigned long flags;
  
  	irq = irq_from_evtchn(evtchn);
  	desc = irq_to_desc(irq);
  
  	if (!desc)
  		return;
  
  	raw_spin_lock_irqsave(&desc->lock, flags);
  	selected_cpu = this_cpu_read(bind_last_selected_cpu);
  	selected_cpu = cpumask_next_and(selected_cpu,
  			desc->irq_common_data.affinity, cpu_online_mask);
  
  	if (unlikely(selected_cpu >= nr_cpu_ids))
  		selected_cpu = cpumask_first_and(desc->irq_common_data.affinity,
  				cpu_online_mask);
  
  	this_cpu_write(bind_last_selected_cpu, selected_cpu);
  
  	/* unmask expects irqs to be disabled */
bce5963bc   Juergen Gross   xen/events: fix b...
429
  	xen_set_affinity_evtchn(desc, selected_cpu);
c48f64ab4   Anoob Soman   xen-evtchn: Bind ...
430
431
  	raw_spin_unlock_irqrestore(&desc->lock, flags);
  }
f7116284c   Ian Campbell   xen: add /dev/xen...
432
433
434
435
436
437
  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   Jeremy Fitzhardinge   xen/dev-evtchn: c...
438
439
  	/* Prevent bind from racing with unbind */
  	mutex_lock(&u->bind_mutex);
f7116284c   Ian Campbell   xen: add /dev/xen...
440
441
442
443
  	switch (cmd) {
  	case IOCTL_EVTCHN_BIND_VIRQ: {
  		struct ioctl_evtchn_bind_virq bind;
  		struct evtchn_bind_virq bind_virq;
fbc872c38   David Vrabel   xen/evtchn: add I...
444
445
446
  		rc = -EACCES;
  		if (u->restrict_domid != UNRESTRICTED_DOMID)
  			break;
f7116284c   Ian Campbell   xen: add /dev/xen...
447
448
449
450
451
  		rc = -EFAULT;
  		if (copy_from_user(&bind, uarg, sizeof(bind)))
  			break;
  
  		bind_virq.virq = bind.virq;
cbbb46823   Vitaly Kuznetsov   xen/evtchn: use x...
452
  		bind_virq.vcpu = xen_vcpu_nr(0);
f7116284c   Ian Campbell   xen: add /dev/xen...
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
  		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;
fbc872c38   David Vrabel   xen/evtchn: add I...
471
472
473
474
  		rc = -EACCES;
  		if (u->restrict_domid != UNRESTRICTED_DOMID &&
  		    u->restrict_domid != bind.remote_domain)
  			break;
f7116284c   Ian Campbell   xen: add /dev/xen...
475
476
477
478
479
480
481
482
  		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);
c48f64ab4   Anoob Soman   xen-evtchn: Bind ...
483
  		if (rc == 0) {
f7116284c   Ian Campbell   xen: add /dev/xen...
484
  			rc = bind_interdomain.local_port;
c48f64ab4   Anoob Soman   xen-evtchn: Bind ...
485
486
  			evtchn_bind_interdom_next_vcpu(rc);
  		}
f7116284c   Ian Campbell   xen: add /dev/xen...
487
488
489
490
491
492
  		break;
  	}
  
  	case IOCTL_EVTCHN_BIND_UNBOUND_PORT: {
  		struct ioctl_evtchn_bind_unbound_port bind;
  		struct evtchn_alloc_unbound alloc_unbound;
fbc872c38   David Vrabel   xen/evtchn: add I...
493
494
495
  		rc = -EACCES;
  		if (u->restrict_domid != UNRESTRICTED_DOMID)
  			break;
f7116284c   Ian Campbell   xen: add /dev/xen...
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
  		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;
73cc4bb0c   David Vrabel   xen/evtchn: impro...
515
  		struct user_evtchn *evtchn;
f7116284c   Ian Campbell   xen: add /dev/xen...
516
517
518
519
520
521
  
  		rc = -EFAULT;
  		if (copy_from_user(&unbind, uarg, sizeof(unbind)))
  			break;
  
  		rc = -EINVAL;
0dc0064ad   David Vrabel   xen/evtchn: suppo...
522
  		if (unbind.port >= xen_evtchn_nr_channels())
f7116284c   Ian Campbell   xen: add /dev/xen...
523
  			break;
f7116284c   Ian Campbell   xen: add /dev/xen...
524
  		rc = -ENOTCONN;
73cc4bb0c   David Vrabel   xen/evtchn: impro...
525
526
  		evtchn = find_evtchn(u, unbind.port);
  		if (!evtchn)
f7116284c   Ian Campbell   xen: add /dev/xen...
527
  			break;
f7116284c   Ian Campbell   xen: add /dev/xen...
528

3f5e554f6   Jeremy Fitzhardinge   xen/evtchn: don't...
529
  		disable_irq(irq_from_evtchn(unbind.port));
73cc4bb0c   David Vrabel   xen/evtchn: impro...
530
  		evtchn_unbind_from_user(u, evtchn);
f7116284c   Ian Campbell   xen: add /dev/xen...
531
532
533
534
535
536
  		rc = 0;
  		break;
  	}
  
  	case IOCTL_EVTCHN_NOTIFY: {
  		struct ioctl_evtchn_notify notify;
73cc4bb0c   David Vrabel   xen/evtchn: impro...
537
  		struct user_evtchn *evtchn;
f7116284c   Ian Campbell   xen: add /dev/xen...
538
539
540
541
  
  		rc = -EFAULT;
  		if (copy_from_user(&notify, uarg, sizeof(notify)))
  			break;
73cc4bb0c   David Vrabel   xen/evtchn: impro...
542
543
544
  		rc = -ENOTCONN;
  		evtchn = find_evtchn(u, notify.port);
  		if (evtchn) {
f7116284c   Ian Campbell   xen: add /dev/xen...
545
546
547
548
549
550
551
552
553
  			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);
73cc4bb0c   David Vrabel   xen/evtchn: impro...
554
  		spin_lock_irq(&u->ring_prod_lock);
f7116284c   Ian Campbell   xen: add /dev/xen...
555
  		u->ring_cons = u->ring_prod = u->ring_overflow = 0;
73cc4bb0c   David Vrabel   xen/evtchn: impro...
556
  		spin_unlock_irq(&u->ring_prod_lock);
f7116284c   Ian Campbell   xen: add /dev/xen...
557
558
559
560
  		mutex_unlock(&u->ring_cons_mutex);
  		rc = 0;
  		break;
  	}
fbc872c38   David Vrabel   xen/evtchn: add I...
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
  	case IOCTL_EVTCHN_RESTRICT_DOMID: {
  		struct ioctl_evtchn_restrict_domid ierd;
  
  		rc = -EACCES;
  		if (u->restrict_domid != UNRESTRICTED_DOMID)
  			break;
  
  		rc = -EFAULT;
  		if (copy_from_user(&ierd, uarg, sizeof(ierd)))
  		    break;
  
  		rc = -EINVAL;
  		if (ierd.domid == 0 || ierd.domid >= DOMID_FIRST_RESERVED)
  			break;
  
  		u->restrict_domid = ierd.domid;
  		rc = 0;
  
  		break;
  	}
f7116284c   Ian Campbell   xen: add /dev/xen...
581
582
583
584
  	default:
  		rc = -ENOSYS;
  		break;
  	}
0a4666b53   Jeremy Fitzhardinge   xen/dev-evtchn: c...
585
  	mutex_unlock(&u->bind_mutex);
f7116284c   Ian Campbell   xen: add /dev/xen...
586
587
588
  
  	return rc;
  }
afc9a42b7   Al Viro   the rest of drive...
589
  static __poll_t evtchn_poll(struct file *file, poll_table *wait)
f7116284c   Ian Campbell   xen: add /dev/xen...
590
  {
a9a08845e   Linus Torvalds   vfs: do bulk POLL...
591
  	__poll_t mask = EPOLLOUT | EPOLLWRNORM;
f7116284c   Ian Campbell   xen: add /dev/xen...
592
593
594
595
  	struct per_user_data *u = file->private_data;
  
  	poll_wait(file, &u->evtchn_wait, wait);
  	if (u->ring_cons != u->ring_prod)
a9a08845e   Linus Torvalds   vfs: do bulk POLL...
596
  		mask |= EPOLLIN | EPOLLRDNORM;
f7116284c   Ian Campbell   xen: add /dev/xen...
597
  	if (u->ring_overflow)
a9a08845e   Linus Torvalds   vfs: do bulk POLL...
598
  		mask = EPOLLERR;
f7116284c   Ian Campbell   xen: add /dev/xen...
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
  	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);
0a4666b53   Jeremy Fitzhardinge   xen/dev-evtchn: c...
623
  	mutex_init(&u->bind_mutex);
f7116284c   Ian Campbell   xen: add /dev/xen...
624
  	mutex_init(&u->ring_cons_mutex);
73cc4bb0c   David Vrabel   xen/evtchn: impro...
625
  	spin_lock_init(&u->ring_prod_lock);
f7116284c   Ian Campbell   xen: add /dev/xen...
626

fbc872c38   David Vrabel   xen/evtchn: add I...
627
  	u->restrict_domid = UNRESTRICTED_DOMID;
f7116284c   Ian Campbell   xen: add /dev/xen...
628
  	filp->private_data = u;
c5bf68fe0   Kirill Smelkov   *: convert stream...
629
  	return stream_open(inode, filp);
f7116284c   Ian Campbell   xen: add /dev/xen...
630
631
632
633
  }
  
  static int evtchn_release(struct inode *inode, struct file *filp)
  {
f7116284c   Ian Campbell   xen: add /dev/xen...
634
  	struct per_user_data *u = filp->private_data;
73cc4bb0c   David Vrabel   xen/evtchn: impro...
635
  	struct rb_node *node;
f7116284c   Ian Campbell   xen: add /dev/xen...
636

73cc4bb0c   David Vrabel   xen/evtchn: impro...
637
638
  	while ((node = u->evtchns.rb_node)) {
  		struct user_evtchn *evtchn;
f7116284c   Ian Campbell   xen: add /dev/xen...
639

73cc4bb0c   David Vrabel   xen/evtchn: impro...
640
641
642
  		evtchn = rb_entry(node, struct user_evtchn, node);
  		disable_irq(irq_from_evtchn(evtchn->port));
  		evtchn_unbind_from_user(u, evtchn);
3f5e554f6   Jeremy Fitzhardinge   xen/evtchn: don't...
643
  	}
862001549   David Vrabel   xen/evtchn: dynam...
644
  	evtchn_free_ring(u->ring);
f7116284c   Ian Campbell   xen: add /dev/xen...
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
  	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   Jeremy Fitzhardinge   xen/evtchn: the e...
660
  	.llseek	 = no_llseek,
f7116284c   Ian Campbell   xen: add /dev/xen...
661
662
663
664
  };
  
  static struct miscdevice evtchn_miscdev = {
  	.minor        = MISC_DYNAMIC_MINOR,
376d908f5   Bastian Blank   xen/evtchn: Fix n...
665
  	.name         = "xen/evtchn",
f7116284c   Ian Campbell   xen: add /dev/xen...
666
667
668
669
670
671
672
673
  	.fops         = &evtchn_fops,
  };
  static int __init evtchn_init(void)
  {
  	int err;
  
  	if (!xen_domain())
  		return -ENODEV;
18283ea74   Wei Liu   xen-evtchn: corre...
674
  	/* Create '/dev/xen/evtchn'. */
f7116284c   Ian Campbell   xen: add /dev/xen...
675
676
  	err = misc_register(&evtchn_miscdev);
  	if (err != 0) {
283c0972d   Joe Perches   xen: Convert prin...
677
678
  		pr_err("Could not register /dev/xen/evtchn
  ");
f7116284c   Ian Campbell   xen: add /dev/xen...
679
680
  		return err;
  	}
283c0972d   Joe Perches   xen: Convert prin...
681
682
  	pr_info("Event-channel device installed
  ");
f7116284c   Ian Campbell   xen: add /dev/xen...
683
684
685
686
687
688
689
690
691
692
693
694
695
  
  	return 0;
  }
  
  static void __exit evtchn_cleanup(void)
  {
  	misc_deregister(&evtchn_miscdev);
  }
  
  module_init(evtchn_init);
  module_exit(evtchn_cleanup);
  
  MODULE_LICENSE("GPL");