Training courses

Kernel and Embedded Linux

Bootlin training courses

Embedded Linux, kernel,
Yocto Project, Buildroot, real-time,
graphics, boot time, debugging...

Bootlin logo

Elixir Cross Referencer

  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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
/*
 * xenstore_dev.c
 * 
 * Driver giving user-space access to the kernel's connection to the
 * XenStore service.
 * 
 * Copyright (c) 2005, Christian Limpach
 * Copyright (c) 2005, Rusty Russell, IBM Corporation
 * 
 * This file may be 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.
 */


#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");

#include <sys/types.h>
#include <sys/cdefs.h>
#include <sys/errno.h>
#include <sys/uio.h>
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/proc.h>
#include <sys/kernel.h>
#include <sys/malloc.h>
#include <sys/conf.h>
#include <sys/module.h>
#include <sys/selinfo.h>
#include <sys/poll.h>

#include <xen/xen-os.h>

#include <xen/hypervisor.h>
#include <xen/xenstore/xenstorevar.h>
#include <xen/xenstore/xenstore_internal.h>

struct xs_dev_transaction {
	LIST_ENTRY(xs_dev_transaction) list;
	struct xs_transaction handle;
};

struct xs_dev_watch {
	LIST_ENTRY(xs_dev_watch) list;
	struct xs_watch watch;
	char *token;
	struct xs_dev_data *user;
};

struct xs_dev_data {
	/* In-progress transaction. */
	LIST_HEAD(, xs_dev_transaction) transactions;

	/* Active watches. */
	LIST_HEAD(, xs_dev_watch) watches;

	/* Partial request. */
	unsigned int len;
	union {
		struct xsd_sockmsg msg;
		char buffer[PAGE_SIZE];
	} u;

	/* Response queue. */
#define MASK_READ_IDX(idx) ((idx)&(PAGE_SIZE-1))
	char read_buffer[PAGE_SIZE];
	unsigned int read_cons, read_prod;

	/* Serializes writes to the read buffer. */
	struct mtx lock;

	/* Polling structure (for reads only ATM). */
	struct selinfo ev_rsel;
};

static void
xs_queue_reply(struct xs_dev_data *u, const char *data, unsigned int len)
{
	unsigned int i;

	for (i = 0; i < len; i++, u->read_prod++)
		u->read_buffer[MASK_READ_IDX(u->read_prod)] = data[i];

	KASSERT((u->read_prod - u->read_cons) <= sizeof(u->read_buffer),
	    ("xenstore reply too big"));

	wakeup(u);
	selwakeup(&u->ev_rsel);
}

static const char *
xs_dev_error_to_string(int error)
{
	unsigned int i;

	for (i = 0; i < nitems(xsd_errors); i++)
		if (xsd_errors[i].errnum == error)
			return (xsd_errors[i].errstring);

	return (NULL);
}

static void
xs_dev_return_error(struct xs_dev_data *u, int error, int req_id, int tx_id)
{
	struct xsd_sockmsg msg;
	const char *payload;

	msg.type = XS_ERROR;
	msg.req_id = req_id;
	msg.tx_id = tx_id;
	payload = NULL;


	payload = xs_dev_error_to_string(error);
	if (payload == NULL)
		payload = xs_dev_error_to_string(EINVAL);
	KASSERT(payload != NULL, ("Unable to find string for EINVAL errno"));

	msg.len = strlen(payload) + 1;

	mtx_lock(&u->lock);
	xs_queue_reply(u, (char *)&msg, sizeof(msg));
	xs_queue_reply(u, payload, msg.len);
	mtx_unlock(&u->lock);
}

static int
xs_dev_watch_message_parse_string(const char **p, const char *end,
    const char **string_r)
{
	const char *nul;

	nul = memchr(*p, 0, end - *p);
	if (!nul)
		return (EINVAL);

	*string_r = *p;
	*p = nul+1;

	return (0);
}

static int
xs_dev_watch_message_parse(const struct xsd_sockmsg *msg, const char **path_r,
    const char **token_r)
{
	const char *p, *end;
	int error;

	p = (const char *)msg + sizeof(*msg);
	end = p + msg->len;
	KASSERT(p <= end, ("payload overflow"));

	error = xs_dev_watch_message_parse_string(&p, end, path_r);
	if (error)
		return (error);
	error = xs_dev_watch_message_parse_string(&p, end, token_r);
	if (error)
		return (error);

	return (0);
}

static struct xs_dev_watch *
xs_dev_find_watch(struct xs_dev_data *u, const char *token)
{
	struct xs_dev_watch *watch;

	LIST_FOREACH(watch, &u->watches, list)
		if (strcmp(watch->token, token) == 0)
			return (watch);

	return (NULL);
}

static void
xs_dev_watch_cb(struct xs_watch *watch, const char **vec, unsigned int len)
{
	struct xs_dev_watch *dwatch;
	struct xsd_sockmsg msg;
	char *payload;

	dwatch = (struct xs_dev_watch *)watch->callback_data;
	msg.type = XS_WATCH_EVENT;
	msg.req_id = msg.tx_id = 0;
	msg.len = strlen(vec[XS_WATCH_PATH]) + strlen(dwatch->token) + 2;

	payload = malloc(msg.len, M_XENSTORE, M_WAITOK);
	strcpy(payload, vec[XS_WATCH_PATH]);
	strcpy(&payload[strlen(vec[XS_WATCH_PATH]) + 1], dwatch->token);
	mtx_lock(&dwatch->user->lock);
	xs_queue_reply(dwatch->user, (char *)&msg, sizeof(msg));
	xs_queue_reply(dwatch->user, payload, msg.len);
	mtx_unlock(&dwatch->user->lock);
	free(payload, M_XENSTORE);
}

static struct xs_dev_transaction *
xs_dev_find_transaction(struct xs_dev_data *u, uint32_t tx_id)
{
	struct xs_dev_transaction *trans;

	LIST_FOREACH(trans, &u->transactions, list)
		if (trans->handle.id == tx_id)
			return (trans);

	return (NULL);
}

static int 
xs_dev_read(struct cdev *dev, struct uio *uio, int ioflag)
{
	int error;
	struct xs_dev_data *u;

	error = devfs_get_cdevpriv((void **)&u);
	if (error != 0)
		return (error);

	while (u->read_prod == u->read_cons) {
		error = tsleep(u, PCATCH, "xsdread", hz/10);
		if (error && error != EWOULDBLOCK)
			return (error);
	}

	while (uio->uio_resid > 0) {
		if (u->read_cons == u->read_prod)
			break;
		error = uiomove(&u->read_buffer[MASK_READ_IDX(u->read_cons)],
		    1, uio);
		if (error)
			return (error);
		u->read_cons++;
	}
	return (0);
}

static int 
xs_dev_write(struct cdev *dev, struct uio *uio, int ioflag)
{
	int error;
	const char *wpath, *wtoken;
	struct xs_dev_data *u;
	struct xs_dev_transaction *trans;
	struct xs_dev_watch *watch;
	void *reply;
	static const char *ok = "OK";
	int len = uio->uio_resid;

	error = devfs_get_cdevpriv((void **)&u);
	if (error != 0)
		return (error);

	if ((len + u->len) > sizeof(u->u.buffer))
		return (EINVAL);

	error = uiomove(u->u.buffer + u->len, len, uio);
	if (error)
		return (error);

	u->len += len;
	if (u->len < (sizeof(u->u.msg) + u->u.msg.len))
		return (0);

	switch (u->u.msg.type) {
	case XS_TRANSACTION_START:
	case XS_TRANSACTION_END:
	case XS_DIRECTORY:
	case XS_READ:
	case XS_GET_PERMS:
	case XS_RELEASE:
	case XS_GET_DOMAIN_PATH:
	case XS_WRITE:
	case XS_MKDIR:
	case XS_RM:
	case XS_SET_PERMS:
		/* Check that this transaction id is not hijacked. */
		if (u->u.msg.tx_id != 0 &&
		    xs_dev_find_transaction(u, u->u.msg.tx_id) == NULL) {
			error = EINVAL;
			break;
		}
		error = xs_dev_request_and_reply(&u->u.msg, &reply);
		if (!error) {
			if (u->u.msg.type == XS_TRANSACTION_START) {
				trans = malloc(sizeof(*trans), M_XENSTORE,
				    M_WAITOK);
				trans->handle.id = strtoul(reply, NULL, 0);
				LIST_INSERT_HEAD(&u->transactions, trans, list);
			} else if (u->u.msg.type == XS_TRANSACTION_END) {
				trans = xs_dev_find_transaction(u,
				    u->u.msg.tx_id);
				KASSERT(trans != NULL,
				    ("Unable to find transaction"));
				LIST_REMOVE(trans, list);
				free(trans, M_XENSTORE);
			}
			mtx_lock(&u->lock);
			xs_queue_reply(u, (char *)&u->u.msg, sizeof(u->u.msg));
			xs_queue_reply(u, (char *)reply, u->u.msg.len);
			mtx_unlock(&u->lock);
			free(reply, M_XENSTORE);
		}
		break;
	case XS_WATCH:
		u->u.msg.tx_id = 0;
		error = xs_dev_watch_message_parse(&u->u.msg, &wpath, &wtoken);
		if (error)
			break;
		if (xs_dev_find_watch(u, wtoken) != NULL) {
			error = EINVAL;
			break;
		}

		watch = malloc(sizeof(*watch), M_XENSTORE, M_WAITOK);
		watch->watch.node = strdup(wpath, M_XENSTORE);
		watch->watch.callback = xs_dev_watch_cb;
		watch->watch.callback_data = (uintptr_t)watch;
		watch->token = strdup(wtoken, M_XENSTORE);
		watch->user = u;

		error = xs_register_watch(&watch->watch);
		if (error != 0) {
			free(watch->token, M_XENSTORE);
			free(watch->watch.node, M_XENSTORE);
			free(watch, M_XENSTORE);
			break;
		}

		LIST_INSERT_HEAD(&u->watches, watch, list);
		u->u.msg.len = sizeof(ok);
		mtx_lock(&u->lock);
		xs_queue_reply(u, (char *)&u->u.msg, sizeof(u->u.msg));
		xs_queue_reply(u, ok, sizeof(ok));
		mtx_unlock(&u->lock);
		break;
	case XS_UNWATCH:
		u->u.msg.tx_id = 0;
		error = xs_dev_watch_message_parse(&u->u.msg, &wpath, &wtoken);
		if (error)
			break;
		watch = xs_dev_find_watch(u, wtoken);
		if (watch == NULL) {
			error = EINVAL;
			break;
		}

		LIST_REMOVE(watch, list);
		xs_unregister_watch(&watch->watch);
		free(watch->watch.node, M_XENSTORE);
		free(watch->token, M_XENSTORE);
		free(watch, M_XENSTORE);
		u->u.msg.len = sizeof(ok);
		mtx_lock(&u->lock);
		xs_queue_reply(u, (char *)&u->u.msg, sizeof(u->u.msg));
		xs_queue_reply(u, ok, sizeof(ok));
		mtx_unlock(&u->lock);
		break;
	default:
		error = EINVAL;
		break;
	}

	if (error != 0)
		xs_dev_return_error(u, error, u->u.msg.req_id, u->u.msg.tx_id);

	/* Reset the write buffer. */
	u->len = 0;

	return (0);
}

static int
xs_dev_poll(struct cdev *dev, int events, struct thread *td)
{
	struct xs_dev_data *u;
	int error, mask;

	error = devfs_get_cdevpriv((void **)&u);
	if (error != 0)
		return (POLLERR);

	/* we can always write */
	mask = events & (POLLOUT | POLLWRNORM);

	if (events & (POLLIN | POLLRDNORM)) {
		if (u->read_cons != u->read_prod) {
			mask |= events & (POLLIN | POLLRDNORM);
		} else {
			/* Record that someone is waiting */
			selrecord(td, &u->ev_rsel);
		}
	}

	return (mask);
}

static void
xs_dev_dtor(void *arg)
{
	struct xs_dev_data *u = arg;
	struct xs_dev_transaction *trans, *tmpt;
	struct xs_dev_watch *watch, *tmpw;

	seldrain(&u->ev_rsel);

	LIST_FOREACH_SAFE(trans, &u->transactions, list, tmpt) {
		xs_transaction_end(trans->handle, 1);
		LIST_REMOVE(trans, list);
		free(trans, M_XENSTORE);
	}

	LIST_FOREACH_SAFE(watch, &u->watches, list, tmpw) {
		LIST_REMOVE(watch, list);
		xs_unregister_watch(&watch->watch);
		free(watch->watch.node, M_XENSTORE);
		free(watch->token, M_XENSTORE);
		free(watch, M_XENSTORE);
	}
	mtx_destroy(&u->lock);

	free(u, M_XENSTORE);
}

static int
xs_dev_open(struct cdev *dev, int oflags, int devtype, struct thread *td)
{
	struct xs_dev_data *u;
	int error;

	u = malloc(sizeof(*u), M_XENSTORE, M_WAITOK|M_ZERO);
	mtx_init(&u->lock, "xsdev_lock", NULL, MTX_DEF);
	LIST_INIT(&u->transactions);
	LIST_INIT(&u->watches);
	error = devfs_set_cdevpriv(u, xs_dev_dtor);
	if (error != 0)
		free(u, M_XENSTORE);

	return (error);
}

static struct cdevsw xs_dev_cdevsw = {
	.d_version = D_VERSION,	
	.d_read = xs_dev_read,
	.d_write = xs_dev_write,
	.d_open = xs_dev_open,
	.d_poll = xs_dev_poll,
	.d_name = "xs_dev",
};

/*------------------ Private Device Attachment Functions  --------------------*/
/**
 * \brief Identify instances of this device type in the system.
 *
 * \param driver  The driver performing this identify action.
 * \param parent  The NewBus parent device for any devices this method adds.
 */
static void
xs_dev_identify(driver_t *driver __unused, device_t parent)
{
	/*
	 * A single device instance for our driver is always present
	 * in a system operating under Xen.
	 */
	BUS_ADD_CHILD(parent, 0, driver->name, 0);
}

/**
 * \brief Probe for the existence of the Xenstore device
 *
 * \param dev  NewBus device_t for this instance.
 *
 * \return  Always returns 0 indicating success.
 */
static int 
xs_dev_probe(device_t dev)
{

	device_set_desc(dev, "Xenstore user-space device");
	return (0);
}

/**
 * \brief Attach the Xenstore device.
 *
 * \param dev  NewBus device_t for this instance.
 *
 * \return  On success, 0. Otherwise an errno value indicating the
 *          type of failure.
 */
static int
xs_dev_attach(device_t dev)
{
	struct cdev *xs_cdev;

	xs_cdev = make_dev_credf(MAKEDEV_ETERNAL, &xs_dev_cdevsw, 0, NULL,
	    UID_ROOT, GID_WHEEL, 0400, "xen/xenstore");
	if (xs_cdev == NULL)
		return (EINVAL);

	return (0);
}

/*-------------------- Private Device Attachment Data  -----------------------*/
static device_method_t xs_dev_methods[] = {
	/* Device interface */
	DEVMETHOD(device_identify,	xs_dev_identify),
	DEVMETHOD(device_probe,         xs_dev_probe),
	DEVMETHOD(device_attach,        xs_dev_attach),

	DEVMETHOD_END
};

DEFINE_CLASS_0(xs_dev, xs_dev_driver, xs_dev_methods, 0);
devclass_t xs_dev_devclass;

DRIVER_MODULE(xs_dev, xenstore, xs_dev_driver, xs_dev_devclass,
    NULL, NULL);