xref: /netbsd-src/sys/dev/kloader.c (revision b5677b36047b601b9addaaa494a58ceae82c2a6c)
1 /*	$NetBSD: kloader.c,v 1.19 2009/03/18 10:22:39 cegger Exp $	*/
2 
3 /*-
4  * Copyright (c) 2001, 2002, 2004 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  * POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 __KERNEL_RCSID(0, "$NetBSD: kloader.c,v 1.19 2009/03/18 10:22:39 cegger Exp $");
31 
32 #include "debug_kloader.h"
33 
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/malloc.h>
37 #include <sys/proc.h>
38 #include <sys/vnode.h>
39 #include <sys/namei.h>
40 #include <sys/fcntl.h>
41 #define	ELFSIZE	32
42 #include <sys/exec_elf.h>
43 
44 #include <uvm/uvm_extern.h>
45 
46 #include <machine/kloader.h>
47 
48 #define	PRINTF(fmt, args...)	printf("kloader: " fmt, ##args)
49 
50 #ifdef KLOADER_DEBUG
51 int	kloader_debug = 1;
52 #define	DPRINTF(fmt, args...)						\
53 	if (kloader_debug)						\
54 		printf("%s: " fmt, __func__ , ##args)
55 #define	_DPRINTF(fmt, args...)						\
56 	if (kloader_debug)						\
57 		printf(fmt, ##args)
58 #define	DPRINTFN(n, fmt, args...)					\
59 	if (kloader_debug > (n))					\
60 		printf("%s: " fmt, __func__ , ##args)
61 #define	_DPRINTFN(n, fmt, args...)					\
62 	if (kloader_debug > (n))					\
63 		printf(fmt, ##args)
64 #define	STATIC
65 #else
66 #define	DPRINTF(fmt, args...)		((void)0)
67 #define	_DPRINTF(fmt, args...)		((void)0)
68 #define	DPRINTFN(n, fmt, args...)	((void)0)
69 #define	_DPRINTFN(n, fmt, args...)	((void)0)
70 #define	STATIC	static
71 #endif
72 
73 struct kloader {
74 	struct pglist pg_head;
75 	struct vm_page *cur_pg;
76 	struct kloader_page_tag *cur_tag;
77 	struct vnode *vp;
78 	struct kloader_page_tag *tagstart;
79 	struct kloader_bootinfo *bootinfo;
80 	struct kloader_bootinfo *rebootinfo;
81 	vaddr_t loader_sp;
82 	kloader_bootfunc_t *loader;
83 	int setuped;
84 	int called;
85 	struct kloader_ops *ops;
86 };
87 
88 #define	BUCKET_SIZE	(PAGE_SIZE - sizeof(struct kloader_page_tag))
89 #define	KLOADER_LWP	(&lwp0)
90 STATIC struct kloader kloader;
91 
92 #define	ROUND4(x)	(((x) + 3) & ~3)
93 
94 STATIC int kloader_load(void);
95 
96 STATIC int kloader_alloc_memory(size_t);
97 STATIC struct kloader_page_tag *kloader_get_tag(vaddr_t);
98 STATIC void kloader_from_file(vaddr_t, off_t, size_t);
99 STATIC void kloader_copy(vaddr_t, const void *, size_t);
100 STATIC void kloader_zero(vaddr_t, size_t);
101 
102 STATIC void kloader_load_segment(Elf_Phdr *);
103 
104 STATIC struct vnode *kloader_open(const char *);
105 STATIC void kloader_close(void);
106 STATIC int kloader_read(size_t, size_t, void *);
107 
108 #ifdef KLOADER_DEBUG
109 STATIC void kloader_pagetag_dump(void);
110 #endif
111 
112 void
113 __kloader_reboot_setup(struct kloader_ops *ops, const char *filename)
114 {
115 
116 	if (kloader.bootinfo == NULL) {
117 		PRINTF("No bootinfo.\n");
118 		return;
119 	}
120 
121 	if (ops == NULL || ops->jump == NULL || ops->boot == NULL) {
122 		PRINTF("No boot operations.\n");
123 		return;
124 	}
125 	kloader.ops = ops;
126 
127 	if (kloader.called++ == 0) {
128 		PRINTF("kernel file name: %s\n", filename);
129 		kloader.vp = kloader_open(filename);
130 		if (kloader.vp == NULL)
131 			return;
132 
133 		if (kloader_load() == 0) {
134 			kloader.setuped = TRUE;
135 #ifdef KLOADER_DEBUG
136 			kloader_pagetag_dump();
137 #endif
138 		}
139 		kloader_close();
140 	} else {
141 		/* Fatal case. reboot from DDB etc. */
142 		kloader_reboot();
143 	}
144 }
145 
146 
147 void
148 kloader_reboot(void)
149 {
150 
151 	if (kloader.setuped) {
152 		PRINTF("Rebooting...\n");
153 		(*kloader.ops->jump)(kloader.loader, kloader.loader_sp,
154 		    kloader.rebootinfo, kloader.tagstart);
155 	}
156 
157 	if (kloader.ops->reset != NULL) {
158 		PRINTF("Resetting...\n");
159 		(*kloader.ops->reset)();
160 	}
161 	while (/*CONSTCOND*/1)
162 		;
163 	/* NOTREACHED */
164 }
165 
166 
167 int
168 kloader_load(void)
169 {
170 	Elf_Ehdr eh;
171 	Elf_Phdr *ph, *p;
172 	Elf_Shdr *sh;
173 	Elf_Addr entry;
174 	vaddr_t kv;
175 	size_t sz;
176 	size_t shstrsz;
177 	char *shstrtab;
178 	int symndx, strndx;
179 	size_t ksymsz;
180 	struct kloader_bootinfo nbi; /* new boot info */
181 	char *oldbuf, *newbuf;
182 	char **ap;
183 	int i;
184 
185 	ph = NULL;
186 	sh = NULL;
187 	shstrtab = NULL;
188 
189 	/* read kernel's ELF header */
190 	kloader_read(0, sizeof(Elf_Ehdr), &eh);
191 
192 	if (eh.e_ident[EI_MAG0] != ELFMAG0 ||
193 	    eh.e_ident[EI_MAG1] != ELFMAG1 ||
194 	    eh.e_ident[EI_MAG2] != ELFMAG2 ||
195 	    eh.e_ident[EI_MAG3] != ELFMAG3) {
196 		PRINTF("not an ELF file\n");
197 		goto err;
198 	}
199 
200 	/* read program headers */
201 	sz = eh.e_phentsize * eh.e_phnum;
202 	if ((ph = malloc(sz, M_TEMP, M_NOWAIT)) == NULL) {
203 		PRINTF("can't allocate program header table.\n");
204 		goto err;
205 	}
206 	if (kloader_read(eh.e_phoff, sz, ph) != 0) {
207 		PRINTF("program header read error.\n");
208 		goto err;
209 	}
210 
211 	/* read section headers */
212 	sz = eh.e_shentsize * eh.e_shnum;
213 	if ((sh = malloc(sz, M_TEMP, M_NOWAIT)) == NULL) {
214 		PRINTF("can't allocate section header table.\n");
215 		goto err;
216 	}
217 	if (kloader_read(eh.e_shoff, eh.e_shentsize * eh.e_shnum, sh) != 0) {
218 		PRINTF("section header read error.\n");
219 		goto err;
220 	}
221 
222 	/* read section names */
223 	shstrsz = ROUND4(sh[eh.e_shstrndx].sh_size);
224 	shstrtab = malloc(shstrsz, M_TEMP, M_NOWAIT);
225 	if (shstrtab == NULL) {
226 		PRINTF("unable to allocate memory for .shstrtab\n");
227 		goto err;
228 	}
229 	DPRINTF("reading 0x%x bytes of .shstrtab at 0x%x\n",
230 		sh[eh.e_shstrndx].sh_size, sh[eh.e_shstrndx].sh_offset);
231 	kloader_read(sh[eh.e_shstrndx].sh_offset, sh[eh.e_shstrndx].sh_size,
232 		     shstrtab);
233 
234 	/* save entry point, code to construct symbol table overwrites it */
235 	entry = eh.e_entry;
236 
237 
238 	/*
239 	 * Calculate memory size
240 	 */
241 	sz = 0;
242 
243 	/* loadable segments */
244 	for (i = 0; i < eh.e_phnum; i++) {
245 		if (ph[i].p_type == PT_LOAD) {
246 			DPRINTF("segment %d size = file 0x%x memory 0x%x\n",
247 				i, ph[i].p_filesz, ph[i].p_memsz);
248 #ifdef KLOADER_ZERO_BSS
249 			sz += round_page(ph[i].p_memsz);
250 #else
251 			sz += round_page(ph[i].p_filesz);
252 #endif
253 			sz += PAGE_SIZE; /* compensate for partial last tag */
254 		}
255 	}
256 
257 	if (sz == 0)		/* nothing to load? */
258 		goto err;
259 
260 	/* symbols/strings sections */
261 	symndx = strndx = -1;
262 	for (i = 0; i < eh.e_shnum; i++) {
263 	    if (strcmp(shstrtab + sh[i].sh_name, ".symtab") == 0)
264 		    symndx = i;
265 	    else if (strcmp(shstrtab + sh[i].sh_name, ".strtab") == 0)
266 		    strndx = i;
267 	    else if (i != eh.e_shstrndx)
268 		    /* while here, mark all other sections as unused */
269 		    sh[i].sh_type = SHT_NULL;
270 	}
271 
272 	if (symndx < 0 || strndx < 0) {
273 		if (symndx < 0)
274 			PRINTF("no .symtab section\n");
275 		if (strndx < 0)
276 			PRINTF("no .strtab section\n");
277 		ksymsz = SELFMAG; /* just a bad magic */
278 	} else {
279 		ksymsz = sizeof(Elf_Ehdr)
280 			+ eh.e_shentsize * eh.e_shnum
281 			+ shstrsz		/* rounded to 4 bytes */
282 			+ sh[symndx].sh_size
283 			+ sh[strndx].sh_size;
284 		DPRINTF("ksyms size = 0x%zx\n", ksymsz);
285 	}
286 	sz += ROUND4(ksymsz);
287 
288 	/* boot info for the new kernel */
289 	sz += sizeof(struct kloader_bootinfo);
290 
291 	/* get memory for new kernel */
292 	if (kloader_alloc_memory(sz) != 0)
293 		goto err;
294 
295 
296 	/*
297 	 * Copy new kernel in.
298 	 */
299 	kv = 0;			/* XXX: -Wuninitialized */
300 	for (i = 0, p = ph; i < eh.e_phnum; i++, p++) {
301 		if (p->p_type == PT_LOAD) {
302 			kloader_load_segment(p);
303 			kv = p->p_vaddr + ROUND4(p->p_memsz);
304 		}
305 	}
306 
307 
308 	/*
309 	 * Construct symbol table for ksyms.
310 	 */
311 	if (symndx < 0 || strndx < 0) {
312 		kloader_zero(kv, SELFMAG);
313 		kv += SELFMAG;
314 	} else {
315 		Elf_Off eoff;
316 		off_t symoff, stroff;
317 
318 		/* save offsets of .symtab and .strtab before we change them */
319 		symoff = sh[symndx].sh_offset;
320 		stroff = sh[strndx].sh_offset;
321 
322 		/* no loadable segments */
323 		eh.e_entry = 0;
324 		eh.e_phnum = 0;
325 		eh.e_phoff = 0;
326 
327 		/* change offsets to reflect new layout */
328 		eoff = sizeof(Elf_Ehdr);
329 		eh.e_shoff = eoff;
330 
331 		eoff += eh.e_shentsize * eh.e_shnum;
332 		sh[eh.e_shstrndx].sh_offset = eoff;
333 
334 		eoff += shstrsz;
335 		sh[symndx].sh_offset = eoff;
336 
337 		eoff += sh[symndx].sh_size;
338 		sh[strndx].sh_offset = eoff;
339 
340 		/* local copies massaged, can serve them now */
341 		DPRINTF("ksyms ELF header\n");
342 		kloader_copy(kv, &eh, sizeof(Elf_Ehdr));
343 		kv += sizeof(Elf_Ehdr);
344 
345 		DPRINTF("ksyms section headers\n");
346 		kloader_copy(kv, sh, eh.e_shentsize * eh.e_shnum);
347 		kv += eh.e_shentsize * eh.e_shnum;
348 
349 		DPRINTF("ksyms .shstrtab\n");
350 		kloader_copy(kv, shstrtab, shstrsz);
351 		kv += shstrsz;
352 
353 		DPRINTF("ksyms .symtab\n");
354 		kloader_from_file(kv, symoff, sh[symndx].sh_size);
355 		kv += sh[symndx].sh_size;
356 
357 		DPRINTF("ksyms .strtab\n");
358 		kloader_from_file(kv, stroff, ROUND4(sh[strndx].sh_size));
359 		kv += ROUND4(sh[strndx].sh_size);
360 	}
361 
362 	/*
363 	 * Create boot info to pass to the new kernel.
364 	 * All pointers in it are *not* valid until the new kernel runs!
365 	 */
366 
367 	/* get a private copy of current bootinfo to vivisect */
368 	memcpy(&nbi, kloader.bootinfo,
369 	       sizeof(struct kloader_bootinfo));
370 
371 	/* new kernel entry point */
372 	nbi.entry = entry;
373 
374 	/* where args currently are, see kloader_bootinfo_set() */
375 	oldbuf = &kloader.bootinfo->_argbuf[0];
376 
377 	/* where args *will* be after boot code copied them */
378 	newbuf = (char *)(void *)kv
379 		+ offsetof(struct kloader_bootinfo, _argbuf);
380 
381 	DPRINTF("argv: old %p -> new %p\n", oldbuf, newbuf);
382 
383 	/* not a valid pointer in this kernel! */
384 	nbi.argv = (void *)newbuf;
385 
386 	/* local copy that we populate with new (not yet valid) pointers */
387 	ap = (char **)(void *)nbi._argbuf;
388 
389 	for (i = 0; i < kloader.bootinfo->argc; ++i) {
390 		DPRINTFN(1, " [%d]: %p -> ", i, kloader.bootinfo->argv[i]);
391 		ap[i] = newbuf +
392 			(kloader.bootinfo->argv[i] - oldbuf);
393 		_DPRINTFN(1, "%p\n", ap[i]);
394 	}
395 
396 	/* arrange for the new bootinfo to get copied */
397 	DPRINTF("bootinfo\n");
398 	kloader_copy(kv, &nbi, sizeof(struct kloader_bootinfo));
399 
400 	/* will be valid by the time the new kernel starts */
401 	kloader.rebootinfo = (void *)kv;
402 	/* kv += sizeof(struct kloader_bootinfo); */
403 
404 	/*
405 	 * Copy loader code
406 	 */
407 	KDASSERT(kloader.cur_pg);
408 	kloader.loader = (void *)PG_VADDR(kloader.cur_pg);
409 	memcpy(kloader.loader, kloader.ops->boot, PAGE_SIZE);
410 
411 	/* loader stack starts at the bottom of that page */
412 	kloader.loader_sp = (vaddr_t)kloader.loader + PAGE_SIZE;
413 
414 	DPRINTF("[loader] addr=%p sp=%p [kernel] entry=%p\n",
415 		kloader.loader, (void *)kloader.loader_sp, (void *)nbi.entry);
416 
417 	return (0);
418  err:
419 	if (ph != NULL)
420 		free(ph, M_TEMP);
421 	if (sh != NULL)
422 		free(sh, M_TEMP);
423 	if (shstrtab != NULL)
424 		free(shstrtab, M_TEMP);
425 
426 	return 1;
427 }
428 
429 
430 int
431 kloader_alloc_memory(size_t sz)
432 {
433 	extern paddr_t avail_start, avail_end;
434 	int n, error;
435 
436 	n = (sz + BUCKET_SIZE - 1) / BUCKET_SIZE	/* kernel &co */
437 	    + 1;					/* 2nd loader */
438 
439 	error = uvm_pglistalloc(n * PAGE_SIZE, avail_start, avail_end,
440 				PAGE_SIZE, 0, &kloader.pg_head, n, 0);
441 	if (error) {
442 		PRINTF("can't allocate memory.\n");
443 		return (1);
444 	}
445 	DPRINTF("allocated %d pages.\n", n);
446 
447 	kloader.cur_pg = TAILQ_FIRST(&kloader.pg_head);
448 	kloader.tagstart = (void *)PG_VADDR(kloader.cur_pg);
449 	kloader.cur_tag = NULL;
450 
451 	return (0);
452 }
453 
454 
455 struct kloader_page_tag *
456 kloader_get_tag(vaddr_t dst)
457 {
458 	struct vm_page *pg;
459 	vaddr_t addr;
460 	struct kloader_page_tag *tag;
461 
462 	tag = kloader.cur_tag;
463 	if (tag != NULL		/* has tag */
464 	    && tag->sz < BUCKET_SIZE /* that has free space */
465 	    && tag->dst + tag->sz == dst) /* and new data are contiguous */
466 	{
467 		DPRINTFN(1, "current tag %x/%x ok\n", tag->dst, tag->sz);
468 		return (tag);
469 	}
470 
471 	pg = kloader.cur_pg;
472 	KDASSERT(pg != NULL);
473 	kloader.cur_pg = TAILQ_NEXT(pg, pageq.queue);
474 
475 	addr = PG_VADDR(pg);
476 	tag = (void *)addr;
477 
478 	/*
479 	 * 2nd loader uses simple word-by-word copy, so destination
480 	 * address of a tag must be properly aligned.
481 	 */
482 	KASSERT(ALIGNED_POINTER(dst, register_t));
483 
484 	tag->src = addr + sizeof(struct kloader_page_tag);
485 	tag->dst = dst;
486 	tag->sz = 0;
487 	tag->next = 0;	/* Terminate. this member may overwrite after. */
488 	if (kloader.cur_tag)
489 		kloader.cur_tag->next = addr;
490 	kloader.cur_tag = tag;
491 
492 	return (tag);
493 }
494 
495 
496 /*
497  * Operations to populate kloader_page_tag's with data.
498  */
499 
500 void
501 kloader_from_file(vaddr_t dst, off_t ofs, size_t sz)
502 {
503 	struct kloader_page_tag *tag;
504 	size_t freesz;
505 
506 	while (sz > 0) {
507 		tag = kloader_get_tag(dst);
508 		KDASSERT(tag != NULL);
509 		freesz = BUCKET_SIZE - tag->sz;
510 		if (freesz > sz)
511 			freesz = sz;
512 
513 		DPRINTFN(1, "0x%08lx + 0x%zx <- 0x%lx\n", dst, freesz,
514 		    (unsigned long)ofs);
515 		kloader_read(ofs, freesz, (void *)(tag->src + tag->sz));
516 
517 		tag->sz += freesz;
518 		sz -= freesz;
519 		ofs += freesz;
520 		dst += freesz;
521 	}
522 }
523 
524 
525 void
526 kloader_copy(vaddr_t dst, const void *src, size_t sz)
527 {
528 	struct kloader_page_tag *tag;
529 	size_t freesz;
530 
531 	while (sz > 0) {
532 		tag = kloader_get_tag(dst);
533 		KDASSERT(tag != NULL);
534 		freesz = BUCKET_SIZE - tag->sz;
535 		if (freesz > sz)
536 			freesz = sz;
537 
538 		DPRINTFN(1, "0x%08lx + 0x%zx <- %p\n", dst, freesz, src);
539 		memcpy((void *)(tag->src + tag->sz), src, freesz);
540 
541 		tag->sz += freesz;
542 		sz -= freesz;
543 		src = (const char *)src + freesz;
544 		dst += freesz;
545 	}
546 }
547 
548 
549 void
550 kloader_zero(vaddr_t dst, size_t sz)
551 {
552 	struct kloader_page_tag *tag;
553 	size_t freesz;
554 
555 	while (sz > 0) {
556 		tag = kloader_get_tag(dst);
557 		KDASSERT(tag != NULL);
558 		freesz = BUCKET_SIZE - tag->sz;
559 		if (freesz > sz)
560 			freesz = sz;
561 
562 		DPRINTFN(1, "0x%08lx + 0x%zx\n", dst, freesz);
563 		memset((void *)(tag->src + tag->sz), 0, freesz);
564 
565 		tag->sz += freesz;
566 		sz -= freesz;
567 		dst += freesz;
568 	}
569 }
570 
571 
572 void
573 kloader_load_segment(Elf_Phdr *p)
574 {
575 
576 	DPRINTF("memory 0x%08x 0x%x <- file 0x%x 0x%x\n",
577 		p->p_vaddr, p->p_memsz, p->p_offset, p->p_filesz);
578 
579 	kloader_from_file(p->p_vaddr, p->p_offset, p->p_filesz);
580 #ifdef KLOADER_ZERO_BSS
581 	kloader_zero(p->p_vaddr + p->p_filesz, p->p_memsz - p->p_filesz);
582 #endif
583 }
584 
585 
586 /*
587  * file access
588  */
589 struct vnode *
590 kloader_open(const char *filename)
591 {
592 	struct nameidata nid;
593 	int error;
594 
595 	NDINIT(&nid, LOOKUP, FOLLOW, UIO_SYSSPACE, filename);
596 
597 	error = namei(&nid);
598 	if (error != 0) {
599 		PRINTF("%s: namei failed, errno=%d\n", filename, error);
600 		return (NULL);
601 	}
602 
603 	error = vn_open(&nid, FREAD, 0);
604 	if (error != 0) {
605 		PRINTF("%s: open failed, errno=%d\n", filename, error);
606 		return (NULL);
607 	}
608 
609 	return (nid.ni_vp);
610 }
611 
612 void
613 kloader_close(void)
614 {
615 	struct lwp *l = KLOADER_LWP;
616 	struct vnode *vp = kloader.vp;
617 
618 	VOP_UNLOCK(vp, 0);
619 	vn_close(vp, FREAD, l->l_cred);
620 }
621 
622 int
623 kloader_read(size_t ofs, size_t size, void *buf)
624 {
625 	struct lwp *l = KLOADER_LWP;
626 	struct vnode *vp = kloader.vp;
627 	size_t resid;
628 	int error;
629 
630 	error = vn_rdwr(UIO_READ, vp, buf, size, ofs, UIO_SYSSPACE,
631 	    IO_NODELOCKED | IO_SYNC, l->l_cred, &resid, NULL);
632 
633 	if (error)
634 		PRINTF("read error.\n");
635 
636 	return (error);
637 }
638 
639 
640 /*
641  * bootinfo
642  */
643 void
644 kloader_bootinfo_set(struct kloader_bootinfo *kbi, int argc, char *argv[],
645     struct bootinfo *bi, int printok)
646 {
647 	char *p, *pend, *buf;
648 	int i;
649 
650 	kloader.bootinfo = kbi;
651 	buf = kbi->_argbuf;
652 	if (bi != NULL)
653 		memcpy(&kbi->bootinfo, bi, sizeof(struct bootinfo));
654 	kbi->argc = argc;
655 	kbi->argv = (char **)buf;
656 
657 	p = &buf[argc * sizeof(char **)];
658 	pend = &buf[KLOADER_KERNELARGS_MAX - 1];
659 
660 	for (i = 0; i < argc; i++) {
661 		char *q = argv[i];
662 		int len = strlen(q) + 1;
663 		if ((p + len) > pend) {
664 			kloader.bootinfo = NULL;
665 			if (printok)
666 				PRINTF("buffer insufficient.\n");
667 			return;
668 		}
669 		kbi->argv[i] = p;
670 		memcpy(p, q, len);
671 		p += len;
672 	}
673 }
674 
675 
676 #ifdef KLOADER_DEBUG
677 void
678 kloader_pagetag_dump(void)
679 {
680 	struct kloader_page_tag *tag = kloader.tagstart;
681 	struct kloader_page_tag *p, *op;
682 	bool print;
683 	int i, n;
684 
685 	p = tag;
686 	op = NULL;
687 	i = 0, n = 15;
688 
689 	PRINTF("[page tag chain]\n");
690 	do  {
691 		print = FALSE;
692 		if (i < n)
693 			print = TRUE;
694 		if ((uint32_t)p & 3) {
695 			printf("tag alignment error\n");
696 			break;
697 		}
698 		if ((p->src & 3) || (p->dst & 3)) {
699 			printf("data alignement error.\n");
700 			print = TRUE;
701 		}
702 
703 		if (print) {
704 			printf("[%2d] next 0x%08x src 0x%08x dst 0x%08x"
705 			    " sz 0x%x\n", i, p->next, p->src, p->dst, p->sz);
706 		} else if (i == n) {
707 			printf("[...]\n");
708 		}
709 		op = p;
710 		i++;
711 	} while ((p = (struct kloader_page_tag *)(p->next)) != 0);
712 
713 	if (op != NULL)
714 		printf("[%d(last)] next 0x%08x src 0x%08x dst 0x%08x sz 0x%x\n",
715 		    i - 1, op->next, op->src, op->dst, op->sz);
716 }
717 
718 #endif /* KLOADER_DEBUG */
719