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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
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
227
228
229
230
231
232
233
234
235
236
237
238
239
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
275
276
277
278
279
280
281
282
283
284
285
286
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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
|
/******************************************************************************
* evtchn.c
*
* Xenolinux driver for receiving and demuxing event-channel signals.
*
* Copyright (c) 2004, K A Fraser
*/
#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/uio.h>
#include <sys/bus.h>
#include <sys/malloc.h>
#include <sys/kernel.h>
#include <sys/lock.h>
#include <sys/mutex.h>
#include <sys/selinfo.h>
#include <sys/poll.h>
#include <sys/conf.h>
#include <sys/fcntl.h>
#include <sys/ioccom.h>
#include <machine/xen/xen-os.h>
#include <xen/xen_intr.h>
#include <machine/bus.h>
#include <sys/rman.h>
#include <machine/resource.h>
#include <machine/xen/synch_bitops.h>
#include <xen/hypervisor.h>
#include <xen/evtchn.h>
typedef struct evtchn_sotfc {
struct selinfo ev_rsel;
} evtchn_softc_t;
#ifdef linuxcrap
/* NB. This must be shared amongst drivers if more things go in /dev/xen */
static devfs_handle_t xen_dev_dir;
#endif
/* Only one process may open /dev/xen/evtchn at any time. */
static unsigned long evtchn_dev_inuse;
/* Notification ring, accessed via /dev/xen/evtchn. */
#define EVTCHN_RING_SIZE 2048 /* 2048 16-bit entries */
#define EVTCHN_RING_MASK(_i) ((_i)&(EVTCHN_RING_SIZE-1))
static uint16_t *ring;
static unsigned int ring_cons, ring_prod, ring_overflow;
/* Which ports is user-space bound to? */
static uint32_t bound_ports[32];
/* Unique address for processes to sleep on */
static void *evtchn_waddr = ˚
static struct mtx lock, upcall_lock;
static d_read_t evtchn_read;
static d_write_t evtchn_write;
static d_ioctl_t evtchn_ioctl;
static d_poll_t evtchn_poll;
static d_open_t evtchn_open;
static d_close_t evtchn_close;
void
evtchn_device_upcall(int port)
{
mtx_lock(&upcall_lock);
mask_evtchn(port);
clear_evtchn(port);
if ( ring != NULL ) {
if ( (ring_prod - ring_cons) < EVTCHN_RING_SIZE ) {
ring[EVTCHN_RING_MASK(ring_prod)] = (uint16_t)port;
if ( ring_cons == ring_prod++ ) {
wakeup(evtchn_waddr);
}
}
else {
ring_overflow = 1;
}
}
mtx_unlock(&upcall_lock);
}
static void
__evtchn_reset_buffer_ring(void)
{
/* Initialise the ring to empty. Clear errors. */
ring_cons = ring_prod = ring_overflow = 0;
}
static int
evtchn_read(struct cdev *dev, struct uio *uio, int ioflag)
{
int rc;
unsigned int count, c, p, sst = 0, bytes1 = 0, bytes2 = 0;
count = uio->uio_resid;
count &= ~1; /* even number of bytes */
if ( count == 0 )
{
rc = 0;
goto out;
}
if ( count > PAGE_SIZE )
count = PAGE_SIZE;
for ( ; ; ) {
if ( (c = ring_cons) != (p = ring_prod) )
break;
if ( ring_overflow ) {
rc = EFBIG;
goto out;
}
if (sst != 0) {
rc = EINTR;
goto out;
}
/* PCATCH == check for signals before and after sleeping
* PWAIT == priority of waiting on resource
*/
sst = tsleep(evtchn_waddr, PWAIT|PCATCH, "evchwt", 10);
}
/* 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(uint16_t);
bytes2 = EVTCHN_RING_MASK(p) * sizeof(uint16_t);
}
else {
bytes1 = (p - c) * sizeof(uint16_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;
}
if ( uiomove(&ring[EVTCHN_RING_MASK(c)], bytes1, uio) ||
((bytes2 != 0) && uiomove(&ring[0], bytes2, uio)))
/* keeping this around as its replacement is not equivalent
* copyout(&ring[0], &buf[bytes1], bytes2)
*/
{
rc = EFAULT;
goto out;
}
ring_cons += (bytes1 + bytes2) / sizeof(uint16_t);
rc = bytes1 + bytes2;
out:
return rc;
}
static int
evtchn_write(struct cdev *dev, struct uio *uio, int ioflag)
{
int rc, i, count;
count = uio->uio_resid;
uint16_t *kbuf = (uint16_t *)malloc(PAGE_SIZE, M_DEVBUF, M_WAITOK);
if ( kbuf == NULL )
return ENOMEM;
count &= ~1; /* even number of bytes */
if ( count == 0 ) {
rc = 0;
goto out;
}
if ( count > PAGE_SIZE )
count = PAGE_SIZE;
if ( uiomove(kbuf, count, uio) != 0 ) {
rc = EFAULT;
goto out;
}
mtx_lock_spin(&lock);
for ( i = 0; i < (count/2); i++ )
if ( test_bit(kbuf[i], &bound_ports[0]) )
unmask_evtchn(kbuf[i]);
mtx_unlock_spin(&lock);
rc = count;
out:
free(kbuf, M_DEVBUF);
return rc;
}
static int
evtchn_ioctl(struct cdev *dev, unsigned long cmd, caddr_t arg,
int mode, struct thread *td __unused)
{
int rc = 0;
mtx_lock_spin(&lock);
switch ( cmd )
{
case EVTCHN_RESET:
__evtchn_reset_buffer_ring();
break;
case EVTCHN_BIND:
if ( !synch_test_and_set_bit((uintptr_t)arg, &bound_ports[0]) )
unmask_evtchn((uintptr_t)arg);
else
rc = EINVAL;
break;
case EVTCHN_UNBIND:
if ( synch_test_and_clear_bit((uintptr_t)arg, &bound_ports[0]) )
mask_evtchn((uintptr_t)arg);
else
rc = EINVAL;
break;
default:
rc = ENOSYS;
break;
}
mtx_unlock_spin(&lock);
return rc;
}
static int
evtchn_poll(struct cdev *dev, int poll_events, struct thread *td)
{
evtchn_softc_t *sc;
unsigned int mask = POLLOUT | POLLWRNORM;
sc = dev->si_drv1;
if ( ring_cons != ring_prod )
mask |= POLLIN | POLLRDNORM;
else if ( ring_overflow )
mask = POLLERR;
else
selrecord(td, &sc->ev_rsel);
return mask;
}
static int
evtchn_open(struct cdev *dev, int flag, int otyp, struct thread *td)
{
uint16_t *_ring;
if (flag & O_NONBLOCK)
return EBUSY;
if ( synch_test_and_set_bit(0, &evtchn_dev_inuse) )
return EBUSY;
if ( (_ring = (uint16_t *)malloc(PAGE_SIZE, M_DEVBUF, M_WAITOK)) == NULL )
return ENOMEM;
mtx_lock_spin(&lock);
ring = _ring;
__evtchn_reset_buffer_ring();
mtx_unlock_spin(&lock);
return 0;
}
static int
evtchn_close(struct cdev *dev, int flag, int otyp, struct thread *td __unused)
{
int i;
mtx_lock_spin(&lock);
if (ring != NULL) {
free(ring, M_DEVBUF);
ring = NULL;
}
for ( i = 0; i < NR_EVENT_CHANNELS; i++ )
if ( synch_test_and_clear_bit(i, &bound_ports[0]) )
mask_evtchn(i);
mtx_unlock_spin(&lock);
evtchn_dev_inuse = 0;
return 0;
}
static struct cdevsw evtchn_devsw = {
d_version: D_VERSION,
d_open: evtchn_open,
d_close: evtchn_close,
d_read: evtchn_read,
d_write: evtchn_write,
d_ioctl: evtchn_ioctl,
d_poll: evtchn_poll,
d_name: "evtchn",
d_flags: 0,
};
/* XXX - if this device is ever supposed to support use by more than one process
* this global static will have to go away
*/
static struct cdev *evtchn_dev;
static int
evtchn_dev_init(void *dummy __unused)
{
/* XXX I believe we don't need these leaving them here for now until we
* have some semblance of it working
*/
mtx_init(&upcall_lock, "evtchup", NULL, MTX_DEF);
/* (DEVFS) create '/dev/misc/evtchn'. */
evtchn_dev = make_dev(&evtchn_devsw, 0, UID_ROOT, GID_WHEEL, 0600, "xen/evtchn");
mtx_init(&lock, "evch", NULL, MTX_SPIN | MTX_NOWITNESS);
evtchn_dev->si_drv1 = malloc(sizeof(evtchn_softc_t), M_DEVBUF, M_WAITOK);
bzero(evtchn_dev->si_drv1, sizeof(evtchn_softc_t));
/* XXX I don't think we need any of this rubbish */
#if 0
if ( err != 0 )
{
printk(KERN_ALERT "Could not register /dev/misc/evtchn\n");
return err;
}
/* (DEVFS) create directory '/dev/xen'. */
xen_dev_dir = devfs_mk_dir(NULL, "xen", NULL);
/* (DEVFS) &link_dest[pos] == '../misc/evtchn'. */
pos = devfs_generate_path(evtchn_miscdev.devfs_handle,
&link_dest[3],
sizeof(link_dest) - 3);
if ( pos >= 0 )
strncpy(&link_dest[pos], "../", 3);
/* (DEVFS) symlink '/dev/xen/evtchn' -> '../misc/evtchn'. */
(void)devfs_mk_symlink(xen_dev_dir,
"evtchn",
DEVFS_FL_DEFAULT,
&link_dest[pos],
&symlink_handle,
NULL);
/* (DEVFS) automatically destroy the symlink with its destination. */
devfs_auto_unregister(evtchn_miscdev.devfs_handle, symlink_handle);
#endif
if (bootverbose)
printf("Event-channel device installed.\n");
return 0;
}
SYSINIT(evtchn_dev_init, SI_SUB_DRIVERS, SI_ORDER_FIRST, evtchn_dev_init, NULL);
|