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
/*-
 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
 *
 * Copyright (c) 1998 Michael Smith
 * All rights reserved.
 * Copyright (c) 2020 NetApp Inc.
 * Copyright (c) 2020 Klara Inc.
 *
 * 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/systm.h>
#include <sys/linker.h>
#include <sys/sbuf.h>
#include <sys/sysctl.h>

#include <machine/metadata.h>

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

/*
 * Preloaded module support
 */

vm_offset_t preload_addr_relocate = 0;
caddr_t preload_metadata;

/*
 * Search for the preloaded module (name)
 */
caddr_t
preload_search_by_name(const char *name)
{
    caddr_t	curp;
    uint32_t	*hdr;
    int		next;
    
    if (preload_metadata != NULL) {
	curp = preload_metadata;
	for (;;) {
	    hdr = (uint32_t *)curp;
	    if (hdr[0] == 0 && hdr[1] == 0)
		break;

	    /* Search for a MODINFO_NAME field */
	    if ((hdr[0] == MODINFO_NAME) &&
		!strcmp(name, curp + sizeof(uint32_t) * 2))
		return(curp);

	    /* skip to next field */
	    next = sizeof(uint32_t) * 2 + hdr[1];
	    next = roundup(next, sizeof(u_long));
	    curp += next;
	}
    }
    return(NULL);
}

/*
 * Search for the first preloaded module of (type)
 */
caddr_t
preload_search_by_type(const char *type)
{
    caddr_t	curp, lname;
    uint32_t	*hdr;
    int		next;

    if (preload_metadata != NULL) {
	curp = preload_metadata;
	lname = NULL;
	for (;;) {
	    hdr = (uint32_t *)curp;
	    if (hdr[0] == 0 && hdr[1] == 0)
		break;

	    /* remember the start of each record */
	    if (hdr[0] == MODINFO_NAME)
		lname = curp;

	    /* Search for a MODINFO_TYPE field */
	    if ((hdr[0] == MODINFO_TYPE) &&
		!strcmp(type, curp + sizeof(uint32_t) * 2))
		return(lname);

	    /* skip to next field */
	    next = sizeof(uint32_t) * 2 + hdr[1];
	    next = roundup(next, sizeof(u_long));
	    curp += next;
	}
    }
    return(NULL);
}

/*
 * Walk through the preloaded module list
 */
caddr_t
preload_search_next_name(caddr_t base)
{
    caddr_t	curp;
    uint32_t	*hdr;
    int		next;
    
    if (preload_metadata != NULL) {
	/* Pick up where we left off last time */
	if (base) {
	    /* skip to next field */
	    curp = base;
	    hdr = (uint32_t *)curp;
	    next = sizeof(uint32_t) * 2 + hdr[1];
	    next = roundup(next, sizeof(u_long));
	    curp += next;
	} else
	    curp = preload_metadata;

	for (;;) {
	    hdr = (uint32_t *)curp;
	    if (hdr[0] == 0 && hdr[1] == 0)
		break;

	    /* Found a new record? */
	    if (hdr[0] == MODINFO_NAME)
		return curp;

	    /* skip to next field */
	    next = sizeof(uint32_t) * 2 + hdr[1];
	    next = roundup(next, sizeof(u_long));
	    curp += next;
	}
    }
    return(NULL);
}

/*
 * Given a preloaded module handle (mod), return a pointer
 * to the data for the attribute (inf).
 */
caddr_t
preload_search_info(caddr_t mod, int inf)
{
    caddr_t	curp;
    uint32_t	*hdr;
    uint32_t	type = 0;
    int		next;

    if (mod == NULL)
    	return (NULL);

    curp = mod;
    for (;;) {
	hdr = (uint32_t *)curp;
	/* end of module data? */
	if (hdr[0] == 0 && hdr[1] == 0)
	    break;
	/* 
	 * We give up once we've looped back to what we were looking at 
	 * first - this should normally be a MODINFO_NAME field.
	 */
	if (type == 0) {
	    type = hdr[0];
	} else {
	    if (hdr[0] == type)
		break;
	}

	/* 
	 * Attribute match? Return pointer to data.
	 * Consumer may safely assume that size value precedes	
	 * data.
	 */
	if (hdr[0] == inf)
	    return(curp + (sizeof(uint32_t) * 2));

	/* skip to next field */
	next = sizeof(uint32_t) * 2 + hdr[1];
	next = roundup(next, sizeof(u_long));
	curp += next;
    }
    return(NULL);
}

/*
 * Delete a preload record by name.
 */
void
preload_delete_name(const char *name)
{
    caddr_t	addr, curp;
    uint32_t	*hdr, sz;
    int		next;
    int		clearing;

    addr = 0;
    sz = 0;
    
    if (preload_metadata != NULL) {
	clearing = 0;
	curp = preload_metadata;
	for (;;) {
	    hdr = (uint32_t *)curp;
	    if (hdr[0] == MODINFO_NAME || (hdr[0] == 0 && hdr[1] == 0)) {
		/* Free memory used to store the file. */
		if (addr != 0 && sz != 0)
		    kmem_bootstrap_free((vm_offset_t)addr, sz);
		addr = 0;
		sz = 0;

		if (hdr[0] == 0)
		    break;
		if (!strcmp(name, curp + sizeof(uint32_t) * 2))
		    clearing = 1;	/* got it, start clearing */
		else if (clearing) {
		    clearing = 0;	/* at next one now.. better stop */
		}
	    }
	    if (clearing) {
		if (hdr[0] == MODINFO_ADDR)
		    addr = *(caddr_t *)(curp + sizeof(uint32_t) * 2);
		else if (hdr[0] == MODINFO_SIZE)
		    sz = *(uint32_t *)(curp + sizeof(uint32_t) * 2);
		hdr[0] = MODINFO_EMPTY;
	    }

	    /* skip to next field */
	    next = sizeof(uint32_t) * 2 + hdr[1];
	    next = roundup(next, sizeof(u_long));
	    curp += next;
	}
    }
}

void *
preload_fetch_addr(caddr_t mod)
{
	caddr_t *mdp;

	mdp = (caddr_t *)preload_search_info(mod, MODINFO_ADDR);
	if (mdp == NULL)
		return (NULL);
	return (*mdp + preload_addr_relocate);
}

size_t
preload_fetch_size(caddr_t mod)
{
	size_t *mdp;

	mdp = (size_t *)preload_search_info(mod, MODINFO_SIZE);
	if (mdp == NULL)
		return (0);
	return (*mdp);
}

/* Called from locore.  Convert physical pointers to kvm. Sigh. */
void
preload_bootstrap_relocate(vm_offset_t offset)
{
    caddr_t	curp;
    uint32_t	*hdr;
    vm_offset_t	*ptr;
    int		next;
    
    if (preload_metadata != NULL) {
	curp = preload_metadata;
	for (;;) {
	    hdr = (uint32_t *)curp;
	    if (hdr[0] == 0 && hdr[1] == 0)
		break;

	    /* Deal with the ones that we know we have to fix */
	    switch (hdr[0]) {
	    case MODINFO_ADDR:
	    case MODINFO_METADATA|MODINFOMD_SSYM:
	    case MODINFO_METADATA|MODINFOMD_ESYM:
		ptr = (vm_offset_t *)(curp + (sizeof(uint32_t) * 2));
		*ptr += offset;
		break;
	    }
	    /* The rest is beyond us for now */

	    /* skip to next field */
	    next = sizeof(uint32_t) * 2 + hdr[1];
	    next = roundup(next, sizeof(u_long));
	    curp += next;
	}
    }
}

/*
 * Parse the modinfo type and append to the provided sbuf.
 */
static void
preload_modinfo_type(struct sbuf *sbp, int type)
{

	if ((type & MODINFO_METADATA) == 0) {
		switch (type) {
		case MODINFO_END:
			sbuf_cat(sbp, "MODINFO_END");
			break;
		case MODINFO_NAME:
			sbuf_cat(sbp, "MODINFO_NAME");
			break;
		case MODINFO_TYPE:
			sbuf_cat(sbp, "MODINFO_TYPE");
			break;
		case MODINFO_ADDR:
			sbuf_cat(sbp, "MODINFO_ADDR");
			break;
		case MODINFO_SIZE:
			sbuf_cat(sbp, "MODINFO_SIZE");
			break;
		case MODINFO_EMPTY:
			sbuf_cat(sbp, "MODINFO_EMPTY");
			break;
		case MODINFO_ARGS:
			sbuf_cat(sbp, "MODINFO_ARGS");
			break;
		default:
			sbuf_cat(sbp, "unrecognized modinfo attribute");
		}

		return;
	}

	sbuf_cat(sbp, "MODINFO_METADATA | ");
	switch (type & ~MODINFO_METADATA) {
	case MODINFOMD_ELFHDR:
		sbuf_cat(sbp, "MODINFOMD_ELFHDR");
		break;
	case MODINFOMD_SSYM:
		sbuf_cat(sbp, "MODINFOMD_SSYM");
		break;
	case MODINFOMD_ESYM:
		sbuf_cat(sbp, "MODINFOMD_ESYM");
		break;
	case MODINFOMD_DYNAMIC:
		sbuf_cat(sbp, "MODINFOMD_DYNAMIC");
		break;
	case MODINFOMD_ENVP:
		sbuf_cat(sbp, "MODINFOMD_ENVP");
		break;
	case MODINFOMD_HOWTO:
		sbuf_cat(sbp, "MODINFOMD_HOWTO");
		break;
	case MODINFOMD_KERNEND:
		sbuf_cat(sbp, "MODINFOMD_KERNEND");
		break;
	case MODINFOMD_SHDR:
		sbuf_cat(sbp, "MODINFOMD_SHDR");
		break;
	case MODINFOMD_CTORS_ADDR:
		sbuf_cat(sbp, "MODINFOMD_CTORS_ADDR");
		break;
	case MODINFOMD_CTORS_SIZE:
		sbuf_cat(sbp, "MODINFOMD_CTORS_SIZE");
		break;
	case MODINFOMD_FW_HANDLE:
		sbuf_cat(sbp, "MODINFOMD_FW_HANDLE");
		break;
	case MODINFOMD_KEYBUF:
		sbuf_cat(sbp, "MODINFOMD_KEYBUF");
		break;
#ifdef MODINFOMD_SMAP
	case MODINFOMD_SMAP:
		sbuf_cat(sbp, "MODINFOMD_SMAP");
		break;
#endif
#ifdef MODINFOMD_SMAP_XATTR
	case MODINFOMD_SMAP_XATTR:
		sbuf_cat(sbp, "MODINFOMD_SMAP_XATTR");
		break;
#endif
#ifdef MODINFOMD_DTBP
	case MODINFOMD_DTBP:
		sbuf_cat(sbp, "MODINFOMD_DTBP");
		break;
#endif
#ifdef MODINFOMD_EFI_MAP
	case MODINFOMD_EFI_MAP:
		sbuf_cat(sbp, "MODINFOMD_EFI_MAP");
		break;
#endif
#ifdef MODINFOMD_EFI_FB
	case MODINFOMD_EFI_FB:
		sbuf_cat(sbp, "MODINFOMD_EFI_FB");
		break;
#endif
#ifdef MODINFOMD_MODULEP
	case MODINFOMD_MODULEP:
		sbuf_cat(sbp, "MODINFOMD_MODULEP");
		break;
#endif
	default:
		sbuf_cat(sbp, "unrecognized metadata type");
	}
}

/*
 * Print the modinfo value, depending on type.
 */
static void
preload_modinfo_value(struct sbuf *sbp, uint32_t *bptr, int type, int len)
{
#ifdef __LP64__
#define sbuf_print_vmoffset(sb, o)	sbuf_printf(sb, "0x%016lx", o);
#else
#define sbuf_print_vmoffset(sb, o)	sbuf_printf(sb, "0x%08x", o);
#endif

	switch (type) {
	case MODINFO_NAME:
	case MODINFO_TYPE:
	case MODINFO_ARGS:
		sbuf_printf(sbp, "%s", (char *)bptr);
		break;
	case MODINFO_SIZE:
	case MODINFO_METADATA | MODINFOMD_CTORS_SIZE:
		sbuf_printf(sbp, "%lu", *(u_long *)bptr);
		break;
	case MODINFO_ADDR:
	case MODINFO_METADATA | MODINFOMD_SSYM:
	case MODINFO_METADATA | MODINFOMD_ESYM:
	case MODINFO_METADATA | MODINFOMD_DYNAMIC:
	case MODINFO_METADATA | MODINFOMD_KERNEND:
	case MODINFO_METADATA | MODINFOMD_ENVP:
	case MODINFO_METADATA | MODINFOMD_CTORS_ADDR:
#ifdef MODINFOMD_SMAP
	case MODINFO_METADATA | MODINFOMD_SMAP:
#endif
#ifdef MODINFOMD_SMAP_XATTR
	case MODINFO_METADATA | MODINFOMD_SMAP_XATTR:
#endif
#ifdef MODINFOMD_DTBP
	case MODINFO_METADATA | MODINFOMD_DTBP:
#endif
#ifdef MODINFOMD_EFI_FB
	case MODINFO_METADATA | MODINFOMD_EFI_FB:
#endif
		sbuf_print_vmoffset(sbp, *(vm_offset_t *)bptr);
		break;
	case MODINFO_METADATA | MODINFOMD_HOWTO:
		sbuf_printf(sbp, "0x%08x", *bptr);
		break;
	case MODINFO_METADATA | MODINFOMD_SHDR:
	case MODINFO_METADATA | MODINFOMD_ELFHDR:
	case MODINFO_METADATA | MODINFOMD_FW_HANDLE:
	case MODINFO_METADATA | MODINFOMD_KEYBUF:
#ifdef MODINFOMD_EFI_MAP
	case MODINFO_METADATA | MODINFOMD_EFI_MAP:
#endif
		/* Don't print data buffers. */
		sbuf_cat(sbp, "buffer contents omitted");
		break;
	default:
		break;
	}
#undef sbuf_print_vmoffset
}

static void
preload_dump_internal(struct sbuf *sbp)
{
	uint32_t *bptr, type, len;

	KASSERT(preload_metadata != NULL,
	    ("%s called without setting up preload_metadata", __func__));

	/*
	 * Iterate through the TLV-encoded sections.
	 */
	bptr = (uint32_t *)preload_metadata;
	sbuf_putc(sbp, '\n');
	while (bptr[0] != MODINFO_END || bptr[1] != MODINFO_END) {
		sbuf_printf(sbp, " %p:\n", bptr);
		type = *bptr++;
		len = *bptr++;

		sbuf_printf(sbp, "\ttype:\t(%#04x) ", type);
		preload_modinfo_type(sbp, type);
		sbuf_putc(sbp, '\n');
		sbuf_printf(sbp, "\tlen:\t%u\n", len);
		sbuf_cat(sbp, "\tvalue:\t");
		preload_modinfo_value(sbp, bptr, type, len);
		sbuf_putc(sbp, '\n');

		bptr += roundup(len, sizeof(u_long)) / sizeof(uint32_t);
	}
}

/*
 * Print the preloaded data to the console. Called from the machine-dependent
 * initialization routines, e.g. hammer_time().
 */
void
preload_dump(void)
{
	char buf[512];
	struct sbuf sb;

	/*
	 * This function is expected to be called before malloc is available,
	 * so use a static buffer and struct sbuf.
	 */
	sbuf_new(&sb, buf, sizeof(buf), SBUF_FIXEDLEN);
	sbuf_set_drain(&sb, sbuf_printf_drain, NULL);
	preload_dump_internal(&sb);

	sbuf_finish(&sb);
	sbuf_delete(&sb);
}

static int
sysctl_preload_dump(SYSCTL_HANDLER_ARGS)
{
	struct sbuf sb;
	int error;

	if (preload_metadata == NULL)
		return (EINVAL);

	sbuf_new_for_sysctl(&sb, NULL, 512, req);
	preload_dump_internal(&sb);

	error = sbuf_finish(&sb);
	sbuf_delete(&sb);

	return (error);
}
SYSCTL_PROC(_debug, OID_AUTO, dump_modinfo,
    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE,
    NULL, 0, sysctl_preload_dump, "A",
    "pretty-print the bootloader metadata");