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
/*	$NetBSD: kern_history.c,v 1.19 2019/10/09 05:59:51 skrll Exp $	 */

/*
 * Copyright (c) 1997 Charles D. Cranor and Washington University.
 * 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 ``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 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.
 *
 * from: NetBSD: uvm_stat.c,v 1.36 2011/02/02 15:13:34 chuck Exp
 * from: Id: uvm_stat.c,v 1.1.2.3 1997/12/19 15:01:00 mrg Exp
 */

/*
 * subr_kernhist.c
 */

#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: kern_history.c,v 1.19 2019/10/09 05:59:51 skrll Exp $");

#include "opt_ddb.h"
#include "opt_kernhist.h"
#include "opt_syscall_debug.h"
#include "opt_usb.h"
#include "opt_uvmhist.h"
#include "opt_biohist.h"
#include "opt_sysctl.h"

#include <sys/atomic.h>
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/cpu.h>
#include <sys/sysctl.h>
#include <sys/kernhist.h>
#include <sys/kmem.h>

#ifdef UVMHIST
#include <uvm/uvm.h>
#endif

#ifdef USB_DEBUG
#include <dev/usb/usbhist.h>
#endif

#ifdef BIOHIST
#include <sys/biohist.h>
#endif

#ifdef SYSCALL_DEBUG
KERNHIST_DECL(scdebughist);
#endif

struct addr_xlt {
	const char *addr;
	size_t len;
	uint32_t offset;
};

/*
 * globals
 */

struct kern_history_head kern_histories;
bool kernhist_sysctl_ready = 0;

int kernhist_print_enabled = 1;

int sysctl_hist_node;

static int sysctl_kernhist_helper(SYSCTLFN_PROTO);

#ifdef DDB

/*
 * prototypes
 */

void kernhist_dump(struct kern_history *, size_t count,
    void (*)(const char *, ...) __printflike(1, 2));
static void kernhist_info(struct kern_history *,
    void (*)(const char *, ...));
void kernhist_dumpmask(uint32_t);
static void kernhist_dump_histories(struct kern_history *[], size_t count,
    void (*)(const char *, ...) __printflike(1, 2));

/* display info about one kernhist */
static void
kernhist_info(struct kern_history *l, void (*pr)(const char *, ...))
{

	pr("kernhist '%s': at %p total %u next free %u\n",
	    l->name, l, l->n, l->f);
}

/*
 * call this from ddb
 *
 * expects the system to be quiesced, no locking
 */
void
kernhist_dump(struct kern_history *l, size_t count,
    void (*pr)(const char *, ...))
{
	int lcv;

	lcv = l->f;
	if (count > l->n)
		pr("%s: count %zu > size %u\n", __func__, count, l->n);
	else if (count)
		lcv = (lcv - count) % l->n;

	do {
		if (l->e[lcv].fmt)
			kernhist_entry_print(&l->e[lcv], pr);
		lcv = (lcv + 1) % l->n;
	} while (lcv != l->f);
}

/*
 * print a merged list of kern_history structures.  count is unused so far.
 */
static void
kernhist_dump_histories(struct kern_history *hists[], size_t count,
    void (*pr)(const char *, ...))
{
	struct bintime	bt;
	int	cur[MAXHISTS];
	int	lcv, hi;

	/* find the first of each list */
	for (lcv = 0; hists[lcv]; lcv++)
		 cur[lcv] = hists[lcv]->f;

	/*
	 * here we loop "forever", finding the next earliest
	 * history entry and printing it.  cur[X] is the current
	 * entry to test for the history in hists[X].  if it is
	 * -1, then this history is finished.
	 */
	for (;;) {
		hi = -1;
		bt.sec = 0; bt.frac = 0;

		/* loop over each history */
		for (lcv = 0; hists[lcv]; lcv++) {
restart:
			if (cur[lcv] == -1)
				continue;
			if (!hists[lcv]->e)
				continue;

			/*
			 * if the format is empty, go to the next entry
			 * and retry.
			 */
			if (hists[lcv]->e[cur[lcv]].fmt == NULL) {
				cur[lcv] = (cur[lcv] + 1) % (hists[lcv]->n);
				if (cur[lcv] == hists[lcv]->f)
					cur[lcv] = -1;
				goto restart;
			}

			/*
			 * if the time hasn't been set yet, or this entry is
			 * earlier than the current bt, set the time and history
			 * index.
			 */
			if (bt.sec == 0 ||
			    bintimecmp(&hists[lcv]->e[cur[lcv]].bt, &bt, <)) {
				bt = hists[lcv]->e[cur[lcv]].bt;
				hi = lcv;
			}
		}

		/* if we didn't find any entries, we must be done */
		if (hi == -1)
			break;

		/* print and move to the next entry */
		kernhist_entry_print(&hists[hi]->e[cur[hi]], pr);

		cur[hi] = (cur[hi] + 1) % (hists[hi]->n);
		if (cur[hi] == hists[hi]->f)
			cur[hi] = -1;
	}
}

/*
 * call this from ddb.  `bitmask' is from <sys/kernhist.h>.  it
 * merges the named histories.
 *
 * expects the system to be quiesced, no locking
 */
void
kernhist_dumpmask(uint32_t bitmask)	/* XXX only support 32 hists */
{
	struct kern_history *hists[MAXHISTS + 1];
	int i = 0;

#ifdef UVMHIST
	if ((bitmask & KERNHIST_UVMMAPHIST) || bitmask == 0)
		hists[i++] = &maphist;

	if ((bitmask & KERNHIST_UVMPDHIST) || bitmask == 0)
		hists[i++] = &pdhist;

	if ((bitmask & KERNHIST_UVMUBCHIST) || bitmask == 0)
		hists[i++] = &ubchist;

	if ((bitmask & KERNHIST_UVMLOANHIST) || bitmask == 0)
		hists[i++] = &loanhist;
#endif

#ifdef USB_DEBUG
	if ((bitmask & KERNHIST_USBHIST) || bitmask == 0)
		hists[i++] = &usbhist;
#endif

#ifdef SYSCALL_DEBUG
	if ((bitmask & KERNHIST_SCDEBUGHIST) || bitmask == 0)
		hists[i++] = &scdebughist;
#endif

#ifdef BIOHIST
	if ((bitmask & KERNHIST_BIOHIST) || bitmask == 0)
		hists[i++] = &biohist;
#endif

	hists[i] = NULL;

	kernhist_dump_histories(hists, 0, printf);
}

/*
 * kernhist_print: ddb hook to print kern history.
 */
void
kernhist_print(void *addr, size_t count, const char *modif,
    void (*pr)(const char *, ...) __printflike(1,2))
{
	struct kern_history *h;

	LIST_FOREACH(h, &kern_histories, list) {
		if (h == addr)
			break;
	}

	if (h == NULL) {
		struct kern_history *hists[MAXHISTS + 1];
		int i = 0;
#ifdef UVMHIST
		hists[i++] = &maphist;
		hists[i++] = &pdhist;
		hists[i++] = &ubchist;
		hists[i++] = &loanhist;
#endif
#ifdef USB_DEBUG
		hists[i++] = &usbhist;
#endif

#ifdef SYSCALL_DEBUG
		hists[i++] = &scdebughist;
#endif
#ifdef BIOHIST
		hists[i++] = &biohist;
#endif
		hists[i] = NULL;

		if (*modif == 'i') {
			int lcv;

			for (lcv = 0; hists[lcv]; lcv++)
				kernhist_info(hists[lcv], pr);
		} else {
			kernhist_dump_histories(hists, count, pr);
		}
	} else {
		if (*modif == 'i')
			kernhist_info(h, pr);
		else
			kernhist_dump(h, count, pr);
	}
}

#endif

/*
 * sysctl interface
 */

/*
 * sysctl_kernhist_new()
 *
 *	If the specified history (or, if no history is specified, any
 *	history) does not already have a sysctl node (under kern.hist)
 *	we create a new one and record it's node number.
 */
void
sysctl_kernhist_new(struct kern_history *hist)
{
	int error;
	struct kern_history *h;
	const struct sysctlnode *rnode = NULL;

	membar_consumer();
	if (kernhist_sysctl_ready == 0)
		return;

	LIST_FOREACH(h, &kern_histories, list) {
		if (hist && h != hist)
			continue;
		if (h->s != 0)
			continue;
		error = sysctl_createv(NULL, 0, NULL, &rnode,
			    CTLFLAG_PERMANENT,
			    CTLTYPE_STRUCT, h->name,
			    SYSCTL_DESCR("history data"),
			    sysctl_kernhist_helper, 0, NULL, 0,
			    CTL_KERN, sysctl_hist_node, CTL_CREATE, CTL_EOL);
		if (error == 0)
			h->s = rnode->sysctl_num;
		if (hist == h)
			break;
	}
}

/*
 * sysctl_kerhnist_init()
 *
 *	Create the 2nd level "hw.hist" sysctl node
 */
void
sysctl_kernhist_init(void)
{
	const struct sysctlnode *rnode = NULL;

	sysctl_createv(NULL, 0, NULL, &rnode,
			CTLFLAG_PERMANENT,
			CTLTYPE_NODE, "hist",
			SYSCTL_DESCR("kernel history tables"),
			sysctl_kernhist_helper, 0, NULL, 0,
			CTL_KERN, CTL_CREATE, CTL_EOL);
	sysctl_hist_node = rnode->sysctl_num;

	kernhist_sysctl_ready = 1;
	membar_producer();

	sysctl_kernhist_new(NULL);
}

/*
 * find_string()
 *
 *	Search the address-to-offset translation table for matching an
 *	address and len, and return the index of the entry we found.  If
 *	not found, returns index 0 which points to the "?" entry.  (We
 *	start matching at index 1, ignoring any matches of the "?" entry
 *	itself.)
 */
static int
find_string(struct addr_xlt table[], size_t *count, const char *string,
	    size_t len)
{
	int i;

	for (i = 1; i < *count; i++)
		if (string == table[i].addr && len == table[i].len)
			return i;

	return 0;
}

/*
 * add_string()
 *
 *	If the string and len are unique, add a new address-to-offset
 *	entry in the translation table and set the offset of the next
 *	entry.
 */
static void
add_string(struct addr_xlt table[], size_t *count, const char *string,
	   size_t len)
{

	if (find_string(table, count, string, len) == 0) {
		table[*count].addr = string;
		table[*count].len = len;
		table[*count + 1].offset = table[*count].offset + len + 1;
		(*count)++;
	}
}

/*
 * sysctl_kernhist_helper
 *
 *	This helper routine is called for all accesses to the kern.hist
 *	hierarchy.
 */
static int
sysctl_kernhist_helper(SYSCTLFN_ARGS)
{
	struct kern_history *h;
	struct kern_history_ent *in_evt;
	struct sysctl_history_event *out_evt;
	struct sysctl_history *buf;
	struct addr_xlt *xlate_t, *xlt;
	size_t bufsize, xlate_s;
	size_t xlate_c;
	const char *strp __diagused;
	char *next;
	int i, j;
	int error;

	if (namelen == 1 && name[0] == CTL_QUERY)
		return sysctl_query(SYSCTLFN_CALL(rnode));

	/*
	 * Disallow userland updates, verify that we arrived at a
	 * valid history rnode
	 */
	if (newp)
		return EPERM;
	if (namelen != 1 || name[0] != CTL_EOL)
		return EINVAL;

	/* Find the correct kernhist for this sysctl node */
	LIST_FOREACH(h, &kern_histories, list) {
		if (h->s == rnode->sysctl_num)
			break;
	}
	if (h == NULL)
		return ENOENT;

	/*
	 * Worst case is two string pointers per history entry, plus
	 * two for the history name and "?" string; allocate an extra
	 * entry since we pre-set the "next" entry's offset member.
	 */
	xlate_s = sizeof(struct addr_xlt) * h->n * 2 + 3;
	xlate_t = kmem_alloc(xlate_s, KM_SLEEP);
	xlate_c = 0;

	/* offset 0 reserved for NULL pointer, ie unused history entry */
	xlate_t[0].offset = 1;

	/*
	 * If the history gets updated and an unexpected string is
	 * found later, we'll point it here.  Otherwise, we'd have to
	 * repeat this process iteratively, and it could take multiple
	 * iterations before terminating.
	 */
	add_string(xlate_t, &xlate_c, "?", 0);

	/* Copy the history name itself to the export structure */
	add_string(xlate_t, &xlate_c, h->name, h->namelen);

	/*
	 * Loop through all used history entries to find the unique
	 * fn and fmt strings
	 */
	for (i = 0, in_evt = h->e; i < h->n; i++, in_evt++) {
		if (in_evt->fn == NULL)
			continue;
		add_string(xlate_t, &xlate_c, in_evt->fn, in_evt->fnlen);
		add_string(xlate_t, &xlate_c, in_evt->fmt, in_evt->fmtlen);
	}

	/* Total buffer size includes header, events, and string table */
	bufsize = sizeof(struct sysctl_history) +
	    h->n * sizeof(struct sysctl_history_event) +
	    xlate_t[xlate_c].offset;
	buf = kmem_alloc(bufsize, KM_SLEEP);

	/*
	 * Copy history header info to the export structure
	 */
	j = find_string(xlate_t, &xlate_c, h->name, h->namelen);
	buf->sh_nameoffset = xlate_t[j].offset;
	buf->sh_numentries = h->n;
	buf->sh_nextfree = h->f;

	/*
	 * Loop through the history events again, copying the data to
	 * the export structure
	 */
	for (i = 0, in_evt = h->e, out_evt = buf->sh_events; i < h->n;
	    i++, in_evt++, out_evt++) {
		if (in_evt->fn == NULL) {	/* skip unused entries */
			out_evt->she_funcoffset = 0;
			out_evt->she_fmtoffset = 0;
			continue;
		}
		out_evt->she_bintime = in_evt->bt;
		out_evt->she_callnumber = in_evt->call;
		out_evt->she_cpunum = in_evt->cpunum;
		out_evt->she_values[0] = in_evt->v[0];
		out_evt->she_values[1] = in_evt->v[1];
		out_evt->she_values[2] = in_evt->v[2];
		out_evt->she_values[3] = in_evt->v[3];
		j = find_string(xlate_t, &xlate_c, in_evt->fn, in_evt->fnlen);
		out_evt->she_funcoffset = xlate_t[j].offset;
		j = find_string(xlate_t, &xlate_c, in_evt->fmt, in_evt->fmtlen);
		out_evt->she_fmtoffset = xlate_t[j].offset;
	}

	/*
	 * Finally, fill the text string area with all the unique
	 * strings we found earlier.
	 *
	 * Skip the initial byte, since we use an offset of 0 to mean
	 * a NULL pointer (which means an unused history event).
	 */
	strp = next = (char *)(&buf->sh_events[h->n]);
	*next++ = '\0';

	/*
	 * Then copy each string into the export structure, making
	 * sure to terminate each string with a '\0' character
	 */
	for (i = 0, xlt = xlate_t; i < xlate_c; i++, xlt++) {
		KASSERTMSG((next - strp) == xlt->offset,
		    "entry %d at wrong offset %"PRIu32, i, xlt->offset);
		memcpy(next, xlt->addr, xlt->len);
		next += xlt->len;
		*next++ = '\0';
	}

	/* Copy data to userland */
	error = copyout(buf, oldp, uimin(bufsize, *oldlenp));

	/* If copyout was successful but only partial, report ENOMEM */
	if (error == 0 && *oldlenp < bufsize)
		error = ENOMEM;

	*oldlenp = bufsize;	/* inform userland of space requirements */

	/* Free up the stuff we allocated */
	kmem_free(buf, bufsize);
	kmem_free(xlate_t, xlate_s);

	return error;
}