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
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
/*
 * Copyright (c) 2015 Julien Grall <julien.grall@citrix.com>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

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

#include <sys/param.h>
#include <sys/module.h>
#include <sys/systm.h>
#include <sys/eventhandler.h>
#include <sys/consio.h>
#include <sys/priv.h>
#include <sys/proc.h>
#include <sys/uio.h>
#include <sys/tty.h>
#include <sys/systm.h>
#include <sys/taskqueue.h>
#include <sys/conf.h>
#include <sys/kernel.h>
#include <sys/bus.h>
#include <sys/cons.h>
#include <sys/kdb.h>
#include <sys/proc.h>
#include <sys/reboot.h>

#include <machine/stdarg.h>

#include <vm/vm.h>
#include <vm/pmap.h>

#include <xen/xen-os.h>
#include <xen/hypervisor.h>
#include <xen/xen_intr.h>
#include <xen/interface/io/console.h>

#include "opt_ddb.h"
#include "opt_printf.h"

#ifdef DDB
#include <ddb/ddb.h>
#endif

static char driver_name[] = "xc";

struct xencons_priv;

typedef void xencons_early_init_t(struct xencons_priv *cons);
typedef int xencons_init_t(device_t dev, struct tty *tp,
    driver_intr_t intr_handler);
typedef int xencons_read_t(struct xencons_priv *cons, char *buffer,
    unsigned int size);
typedef int xencons_write_t(struct xencons_priv *cons, const char *buffer,
    unsigned int size);

struct xencons_ops {
	/*
	 * Called by the low-level driver during early boot.
	 * Only the minimal set up to get a console should be done here.
	 */
	xencons_early_init_t	*early_init;
	/* Prepare the console to be fully use */
	xencons_init_t		*init;
	/* Read/write helpers */
	xencons_read_t		*read;
	xencons_write_t		*write;
};

struct xencons_priv {
	/* Mutex to protect the shared ring and the internal buffers */
	struct mtx			mtx;
	/* Interrupt handler used for notify the backend */
	xen_intr_handle_t		intr_handle;
	/* KDB internal state */
#ifdef KDB
	int				altbrk;
#endif
	/* Status of the tty */
	bool				opened;
	/* Callout used when the write buffer is full */
	struct callout			callout;

	/* Internal buffers must be used with mtx locked */
#define WBUF_SIZE     4096
#define WBUF_MASK(_i) ((_i)&(WBUF_SIZE-1))
	char				wbuf[WBUF_SIZE];
	unsigned int			wc, wp; /* Consumer/producer wbuf */

#define RBUF_SIZE     1024
#define RBUF_MASK(_i) ((_i)&(RBUF_SIZE-1))
	char				rbuf[RBUF_SIZE];
	unsigned int			rc, rp; /* Consumer/producer rbuf */

	/* Pointer to the console operations */
	const struct xencons_ops	*ops;

	/*
	 * Ring specific fields
	 * XXX: make an union?
	 */
	/* Event channel number for early notification (PV only) */
	uint32_t			evtchn;
	/* Console shared page */
	struct xencons_interface	*intf;
};

/*
 * Data for the main console
 * Necessary to support low-level console driver
 */
static struct xencons_priv main_cons;

#define XC_POLLTIME 	(hz/10)

/*----------------------------- Debug function ------------------------------*/
struct putchar_arg {
	char	*buf;
	size_t	size;
	size_t	n_next;
};

static void
putchar(int c, void *arg)
{
	struct putchar_arg *pca;

	pca = (struct putchar_arg *)arg;

	if (pca->buf == NULL) {
		/*
		 * We have no buffer, output directly to the
		 * console char by char.
		 */
		HYPERVISOR_console_write((char *)&c, 1);
	} else {
		pca->buf[pca->n_next++] = c;
		if ((pca->size == pca->n_next) || (c = '\0')) {
			/* Flush the buffer */
			HYPERVISOR_console_write(pca->buf, pca->n_next);
			pca->n_next = 0;
		}
	}
}

void
xc_printf(const char *fmt, ...)
{
	va_list ap;
	struct putchar_arg pca;
#ifdef PRINTF_BUFR_SIZE
	char buf[PRINTF_BUFR_SIZE];

	pca.buf = buf;
	pca.size = sizeof(buf);
	pca.n_next = 0;
#else
	pca.buf = NULL;
	pca.size = 0;
#endif

	KASSERT((xen_domain()), ("call to xc_printf from non Xen guest"));

	va_start(ap, fmt);
	kvprintf(fmt, putchar, &pca, 10, ap);
	va_end(ap);

#ifdef PRINTF_BUFR_SIZE
	if (pca.n_next != 0)
		HYPERVISOR_console_write(buf, pca.n_next);
#endif
}

/*---------------------- Helpers for the console lock -----------------------*/
/*
 * The lock is not used when the kernel is panicing as it will never recover
 * and we want to output no matter what it costs.
 */
static inline void xencons_lock(struct xencons_priv *cons)
{

	if (!KERNEL_PANICKED())
		mtx_lock_spin(&cons->mtx);

}

static inline void xencons_unlock(struct xencons_priv *cons)
{

	if (!KERNEL_PANICKED())
		mtx_unlock_spin(&cons->mtx);
}

#define xencons_lock_assert(cons)	mtx_assert(&(cons)->mtx, MA_OWNED)

/*------------------ Helpers for the hypervisor console ---------------------*/
static void
xencons_early_init_hypervisor(struct xencons_priv *cons)
{
	/*
	 * Nothing to setup for the low-level console when using
	 * the hypervisor console.
	 */
}

static int
xencons_init_hypervisor(device_t dev, struct tty *tp,
    driver_intr_t intr_handler)
{
	struct xencons_priv *cons;
	int err;

	cons = tty_softc(tp);

	err = xen_intr_bind_virq(dev, VIRQ_CONSOLE, 0, NULL,
	    intr_handler, tp, INTR_TYPE_TTY | INTR_MPSAFE, &cons->intr_handle);
	if (err != 0)
		device_printf(dev, "Can't register console interrupt\n");

	return (err);
}

static int
xencons_write_hypervisor(struct xencons_priv *cons, const char *buffer,
    unsigned int size)
{

	HYPERVISOR_console_io(CONSOLEIO_write, size, buffer);

	return (size);
}

static int
xencons_read_hypervisor(struct xencons_priv *cons, char *buffer,
    unsigned int size)
{

	xencons_lock_assert(cons);

	return (HYPERVISOR_console_io(CONSOLEIO_read, size, buffer));
}

static const struct xencons_ops xencons_hypervisor_ops = {
	.early_init	= xencons_early_init_hypervisor,
	.init		= xencons_init_hypervisor,
	.read		= xencons_read_hypervisor,
	.write		= xencons_write_hypervisor,
};

/*------------------ Helpers for the ring console ---------------------------*/
static void
xencons_early_init_ring(struct xencons_priv *cons)
{
	cons->intf = pmap_mapdev_attr(ptoa(xen_get_console_mfn()), PAGE_SIZE,
	    PAT_WRITE_BACK);
	cons->evtchn = xen_get_console_evtchn();
}

static int
xencons_init_ring(device_t dev, struct tty *tp, driver_intr_t intr_handler)
{
	struct xencons_priv *cons;
	int err;

	cons = tty_softc(tp);

	if (cons->evtchn == 0)
		return (ENODEV);

	err = xen_intr_bind_local_port(dev, cons->evtchn, NULL,
	    intr_handler, tp, INTR_TYPE_TTY | INTR_MPSAFE, &cons->intr_handle);
	if (err != 0)
		return (err);

	return (0);
}

static void
xencons_notify_ring(struct xencons_priv *cons)
{
	/*
	 * The console may be used before the ring interrupt is properly
	 * initialized.
	 * If so, fallback to directly use the event channel hypercall.
	 */
	if (__predict_true(cons->intr_handle != NULL))
		xen_intr_signal(cons->intr_handle);
	else {
		struct evtchn_send send = {
			.port = cons->evtchn
		};

		HYPERVISOR_event_channel_op(EVTCHNOP_send, &send);
	}
}

static int
xencons_write_ring(struct xencons_priv *cons, const char *buffer,
    unsigned int size)
{
	struct xencons_interface *intf;
	XENCONS_RING_IDX wcons, wprod;
	int sent;

	intf = cons->intf;

	xencons_lock_assert(cons);

	wcons = intf->out_cons;
	wprod = intf->out_prod;

	mb();
	KASSERT((wprod - wcons) <= sizeof(intf->out),
		("console send ring inconsistent"));

	for (sent = 0; sent < size; sent++, wprod++) {
		if ((wprod - wcons) >= sizeof(intf->out))
			break;
		intf->out[MASK_XENCONS_IDX(wprod, intf->out)] = buffer[sent];
	}

	wmb();
	intf->out_prod = wprod;

	xencons_notify_ring(cons);

	return (sent);
}

static int
xencons_read_ring(struct xencons_priv *cons, char *buffer, unsigned int size)
{
	struct xencons_interface *intf;
	XENCONS_RING_IDX rcons, rprod;
	unsigned int rsz;

	intf = cons->intf;

	xencons_lock_assert(cons);

	rcons = intf->in_cons;
	rprod = intf->in_prod;
	rmb();

	for (rsz = 0; rsz < size; rsz++, rcons++) {
		if (rprod == rcons)
			break;
		buffer[rsz] = intf->in[MASK_XENCONS_IDX(rcons, intf->in)];
	}

	wmb();
	intf->in_cons = rcons;

	/* No need to notify the backend if nothing has been read */
	if (rsz != 0)
		xencons_notify_ring(cons);

	return (rsz);
}

static const struct xencons_ops xencons_ring_ops = {
	.early_init	= xencons_early_init_ring,
	.init		= xencons_init_ring,
	.read		= xencons_read_ring,
	.write		= xencons_write_ring,
};

/*------------------ Common implementation of the console -------------------*/

/*
 * Called by the low-level driver during early boot to initialize the
 * main console driver.
 * Only the minimal set up to get a console should be done here.
 */
static void
xencons_early_init(void)
{

	mtx_init(&main_cons.mtx, "XCONS LOCK", NULL, MTX_SPIN);

	if (xen_get_console_evtchn() == 0)
		main_cons.ops = &xencons_hypervisor_ops;
	else
		main_cons.ops = &xencons_ring_ops;

	main_cons.ops->early_init(&main_cons);
}

/*
 * Receive character from the console and put them in the internal buffer
 * XXX: Handle overflow of the internal buffer
 */
static void
xencons_rx(struct xencons_priv *cons)
{
	char buf[16];
	int sz;

	xencons_lock(cons);
	while ((sz = cons->ops->read(cons, buf, sizeof(buf))) > 0) {
		int i;

		for (i = 0; i < sz; i++)
			cons->rbuf[RBUF_MASK(cons->rp++)] = buf[i];
	}
	xencons_unlock(cons);
}

/* Return true if the write buffer is full */
static bool
xencons_tx_full(struct xencons_priv *cons)
{
	unsigned int used;

	xencons_lock(cons);
	used = cons->wp - cons->wc;
	xencons_unlock(cons);

	return (used >= WBUF_SIZE);
}

static void
xencons_tx_flush(struct xencons_priv *cons, int force)
{
	int        sz;

	xencons_lock(cons);
	while (cons->wc != cons->wp) {
		int sent;
		sz = cons->wp - cons->wc;
		if (sz > (WBUF_SIZE - WBUF_MASK(cons->wc)))
			sz = WBUF_SIZE - WBUF_MASK(cons->wc);
		sent = cons->ops->write(cons, &cons->wbuf[WBUF_MASK(cons->wc)],
		    sz);

		/*
		 * The other end may not have been initialized. Ignore
		 * the force.
		 */
		if (__predict_false(sent < 0))
			break;

		/*
		 * If force is set, spin until the console data is
		 * flushed through the domain controller.
		 */
		if (sent == 0 && __predict_true(!force))
			break;

		cons->wc += sent;
	}
	xencons_unlock(cons);
}

static bool
xencons_putc(struct xencons_priv *cons, int c, bool force_flush)
{

	xencons_lock(cons);
	if ((cons->wp - cons->wc) < WBUF_SIZE)
		cons->wbuf[WBUF_MASK(cons->wp++)] = c;
	xencons_unlock(cons);

	xencons_tx_flush(cons, force_flush);

	return (xencons_tx_full(cons));
}

static int
xencons_getc(struct xencons_priv *cons)
{
	int ret;

	xencons_lock(cons);
	if (cons->rp != cons->rc) {
		/* We need to return only one char */
		ret = (int)cons->rbuf[RBUF_MASK(cons->rc)];
		cons->rc++;
	} else {
		ret = -1;
	}

	xencons_unlock(cons);

	return (ret);
}

static bool
xencons_tx(struct tty *tp)
{
	bool cons_full;
	char c;
	struct xencons_priv *cons;

	cons = tty_softc(tp);

	tty_assert_locked(tp);

	/*
	 * Don't transmit any character if the buffer is full. Otherwise,
	 * characters may be lost
	 */
	if (xencons_tx_full(cons))
		return (false);

	cons_full = false;
	while (!cons_full && ttydisc_getc(tp, &c, 1) == 1)
		cons_full = xencons_putc(cons, c, false);

	return (!cons_full);
}

static void
xencons_intr(void *arg)
{
	struct tty *tp;
	struct xencons_priv *cons;
	int ret;

	tp = arg;
	cons = tty_softc(tp);

	/*
	 * The input will be used by the low-level console when KDB is active
	 */
	if (kdb_active)
		return;

	/*
	 * It's not necessary to retrieve input when the tty is not opened
	 */
	if (!cons->opened)
		return;

	xencons_rx(cons);

	tty_lock(tp);
	while ((ret = xencons_getc(cons)) != -1) {
#ifdef KDB
		kdb_alt_break(ret, &cons->altbrk);
#endif
		ttydisc_rint(tp, ret, 0);
	}
	ttydisc_rint_done(tp);
	tty_unlock(tp);

	/* Try to flush remaining characters if necessary */
	xencons_tx_flush(cons, 0);
}

/*
 * Helpers to call while shutting down:
 *	- Force flush all output
 */
static void
xencons_shutdown(void *arg, int howto)
{
	struct tty *tp;

	tp = arg;

	xencons_tx_flush(tty_softc(tp), 1);
}

/*---------------------- Low-level console driver ---------------------------*/
static void
xencons_cnprobe(struct consdev *cp)
{

	if (!xen_domain())
		return;

	cp->cn_pri = (boothowto & RB_SERIAL) ? CN_REMOTE : CN_NORMAL;
	sprintf(cp->cn_name, "%s0", driver_name);
}

static void
xencons_cninit(struct consdev *cp)
{

	xencons_early_init();
}

static void
xencons_cnterm(struct consdev *cp)
{
}

static void
xencons_cngrab(struct consdev *cp)
{
}

static void
xencons_cnungrab(struct consdev *cp)
{
}

static int
xencons_cngetc(struct consdev *dev)
{

	xencons_rx(&main_cons);

	return (xencons_getc(&main_cons));
}

static void
xencons_cnputc(struct consdev *dev, int c)
{
	/*
	 * The low-level console is used by KDB and panic. We have to ensure
	 * that any character sent will be seen by the backend.
	 */
	xencons_putc(&main_cons, c, true);
}

CONSOLE_DRIVER(xencons);

/*----------------------------- TTY driver ---------------------------------*/

static int
xencons_tty_open(struct tty *tp)
{
	struct xencons_priv *cons;

	cons = tty_softc(tp);

	cons->opened = true;

	return (0);
}

static void
xencons_tty_close(struct tty *tp)
{
	struct xencons_priv *cons;

	cons = tty_softc(tp);

	cons->opened = false;
}

static void
xencons_timeout(void *v)
{
	struct tty *tp;
	struct xencons_priv *cons;

	tp = v;
	cons = tty_softc(tp);

	if (!xencons_tx(tp))
		callout_reset(&cons->callout, XC_POLLTIME,
		    xencons_timeout, tp);
}

static void
xencons_tty_outwakeup(struct tty *tp)
{
	struct xencons_priv *cons;

	cons = tty_softc(tp);

	callout_stop(&cons->callout);

	if (!xencons_tx(tp))
		callout_reset(&cons->callout, XC_POLLTIME,
		    xencons_timeout, tp);
}

static struct ttydevsw xencons_ttydevsw = {
        .tsw_flags	= TF_NOPREFIX,
        .tsw_open	= xencons_tty_open,
        .tsw_close	= xencons_tty_close,
        .tsw_outwakeup	= xencons_tty_outwakeup,
};

/*------------------------ Main console driver ------------------------------*/
static void
xencons_identify(driver_t *driver, device_t parent)
{
	device_t child;

	if (main_cons.ops == NULL)
		return;

	child = BUS_ADD_CHILD(parent, 0, driver_name, 0);
}

static int
xencons_probe(device_t dev)
{

	device_set_desc(dev, "Xen Console");
	return (BUS_PROBE_NOWILDCARD);
}

static int
xencons_attach(device_t dev)
{
	struct tty *tp;
	/*
	 * The main console is already allocated statically in order to
	 * support low-level console
	 */
	struct xencons_priv *cons;
	int err;

	cons = &main_cons;

	tp = tty_alloc(&xencons_ttydevsw, cons);
	tty_makedev(tp, NULL, "%s%r", driver_name, 0);
	device_set_softc(dev, tp);

	callout_init_mtx(&cons->callout, tty_getlock(tp), 0);

	err = cons->ops->init(dev, tp, xencons_intr);
	if (err != 0) {
		device_printf(dev, "Unable to initialize the console (%d)\n",
		    err);
		return (err);
	}

	/* register handler to flush console on shutdown */
	if ((EVENTHANDLER_REGISTER(shutdown_post_sync, xencons_shutdown,
	    tp, SHUTDOWN_PRI_DEFAULT)) == NULL)
		device_printf(dev, "shutdown event registration failed!\n");

	return (0);
}

static int
xencons_resume(device_t dev)
{
	struct xencons_priv *cons;
	struct tty *tp;
	int err;

	tp = device_get_softc(dev);
	cons = tty_softc(tp);
	xen_intr_unbind(&cons->intr_handle);

	err = cons->ops->init(dev, tp, xencons_intr);
	if (err != 0) {
		device_printf(dev, "Unable to resume the console (%d)\n", err);
		return (err);
	}

	return (0);
}

static devclass_t xencons_devclass;

static device_method_t xencons_methods[] = {
	DEVMETHOD(device_identify, xencons_identify),
	DEVMETHOD(device_probe, xencons_probe),
	DEVMETHOD(device_attach, xencons_attach),
	DEVMETHOD(device_resume, xencons_resume),

	DEVMETHOD_END
};

static driver_t xencons_driver = {
	driver_name,
	xencons_methods,
	0,
};

DRIVER_MODULE(xc, xenpv, xencons_driver, xencons_devclass, 0, 0);