1 /*	$NetBSD: kloader.c,v 1.27 2015/06/11 08:14:38 matt 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.27 2015/06/11 08:14:38 matt 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.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;		/* XXX use bus_dma(9) */
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
__kloader_reboot_setup(struct kloader_ops * ops,const char * filename)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
kloader_reboot(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 		continue;
163 	/* NOTREACHED */
164 }
165 
166 
167 int
kloader_load(void)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 	 * Calculate memory size
239 	 */
240 	sz = 0;
241 
242 	/* loadable segments */
243 	for (i = 0; i < eh.e_phnum; i++) {
244 		if (ph[i].p_type == PT_LOAD) {
245 			DPRINTF("segment %d size = file 0x%x memory 0x%x\n",
246 			    i, ph[i].p_filesz, ph[i].p_memsz);
247 #ifdef KLOADER_ZERO_BSS
248 			sz += round_page(ph[i].p_memsz);
249 #else
250 			sz += round_page(ph[i].p_filesz);
251 #endif
252 			sz += PAGE_SIZE; /* compensate for partial last tag */
253 		}
254 	}
255 
256 	if (sz == 0)		/* nothing to load? */
257 		goto err;
258 
259 	/* symbols/strings sections */
260 	symndx = strndx = -1;
261 	for (i = 0; i < eh.e_shnum; i++) {
262 		if (strcmp(shstrtab + sh[i].sh_name, ".symtab") == 0)
263 			symndx = i;
264 		else if (strcmp(shstrtab + sh[i].sh_name, ".strtab") == 0)
265 			strndx = i;
266 		else if (i != eh.e_shstrndx)
267 			/* while here, mark all other sections as unused */
268 			sh[i].sh_type = SHT_NULL;
269 	}
270 
271 	if (symndx < 0 || strndx < 0) {
272 		if (symndx < 0)
273 			PRINTF("no .symtab section\n");
274 		if (strndx < 0)
275 			PRINTF("no .strtab section\n");
276 		ksymsz = SELFMAG; /* just a bad magic */
277 	} else {
278 		ksymsz = sizeof(Elf_Ehdr)
279 		    + eh.e_shentsize * eh.e_shnum
280 		    + shstrsz		/* rounded to 4 bytes */
281 		    + sh[symndx].sh_size
282 		    + sh[strndx].sh_size;
283 		DPRINTF("ksyms size = 0x%zx\n", ksymsz);
284 	}
285 	sz += ROUND4(ksymsz);
286 
287 	/* boot info for the new kernel */
288 	sz += sizeof(struct kloader_bootinfo);
289 
290 	/* get memory for new kernel */
291 	if (kloader_alloc_memory(sz) != 0)
292 		goto err;
293 
294 	/*
295 	 * Copy new kernel in.
296 	 */
297 	kv = 0;			/* XXX: -Wuninitialized */
298 	for (i = 0, p = ph; i < eh.e_phnum; i++, p++) {
299 		if (p->p_type == PT_LOAD) {
300 			kloader_load_segment(p);
301 			kv = p->p_vaddr + ROUND4(p->p_memsz);
302 		}
303 	}
304 
305 	/*
306 	 * Construct symbol table for ksyms.
307 	 */
308 	if (symndx < 0 || strndx < 0) {
309 		kloader_zero(kv, SELFMAG);
310 		kv += SELFMAG;
311 	} else {
312 		Elf_Off eoff;
313 		off_t symoff, stroff;
314 
315 		/* save offsets of .symtab and .strtab before we change them */
316 		symoff = sh[symndx].sh_offset;
317 		stroff = sh[strndx].sh_offset;
318 
319 		/* no loadable segments */
320 		eh.e_entry = 0;
321 		eh.e_phnum = 0;
322 		eh.e_phoff = 0;
323 
324 		/* change offsets to reflect new layout */
325 		eoff = sizeof(Elf_Ehdr);
326 		eh.e_shoff = eoff;
327 
328 		eoff += eh.e_shentsize * eh.e_shnum;
329 		sh[eh.e_shstrndx].sh_offset = eoff;
330 
331 		eoff += shstrsz;
332 		sh[symndx].sh_offset = eoff;
333 
334 		eoff += sh[symndx].sh_size;
335 		sh[strndx].sh_offset = eoff;
336 
337 		/* local copies massaged, can serve them now */
338 		DPRINTF("ksyms ELF header\n");
339 		kloader_copy(kv, &eh, sizeof(Elf_Ehdr));
340 		kv += sizeof(Elf_Ehdr);
341 
342 		DPRINTF("ksyms section headers\n");
343 		kloader_copy(kv, sh, eh.e_shentsize * eh.e_shnum);
344 		kv += eh.e_shentsize * eh.e_shnum;
345 
346 		DPRINTF("ksyms .shstrtab\n");
347 		kloader_copy(kv, shstrtab, shstrsz);
348 		kv += shstrsz;
349 
350 		DPRINTF("ksyms .symtab\n");
351 		kloader_from_file(kv, symoff, sh[symndx].sh_size);
352 		kv += sh[symndx].sh_size;
353 
354 		DPRINTF("ksyms .strtab\n");
355 		kloader_from_file(kv, stroff, ROUND4(sh[strndx].sh_size));
356 		kv += ROUND4(sh[strndx].sh_size);
357 	}
358 
359 	/*
360 	 * Create boot info to pass to the new kernel.
361 	 * All pointers in it are *not* valid until the new kernel runs!
362 	 */
363 
364 	/* get a private copy of current bootinfo to vivisect */
365 	memcpy(&nbi, kloader.bootinfo, sizeof(struct kloader_bootinfo));
366 
367 	/* new kernel entry point */
368 	nbi.entry = entry;
369 
370 	/* where args currently are, see kloader_bootinfo_set() */
371 	oldbuf = &kloader.bootinfo->_argbuf[0];
372 
373 	/* where args *will* be after boot code copied them */
374 	newbuf = (char *)(void *)kv
375 	    + offsetof(struct kloader_bootinfo, _argbuf);
376 
377 	DPRINTF("argv: old %p -> new %p\n", oldbuf, newbuf);
378 
379 	/* not a valid pointer in this kernel! */
380 	nbi.argv = (void *)newbuf;
381 
382 	/* local copy that we populate with new (not yet valid) pointers */
383 	ap = (char **)(void *)nbi._argbuf;
384 
385 	for (i = 0; i < kloader.bootinfo->argc; ++i) {
386 		DPRINTFN(1, " [%d]: %p -> ", i, kloader.bootinfo->argv[i]);
387 		ap[i] = newbuf +
388 		    (kloader.bootinfo->argv[i] - oldbuf);
389 		_DPRINTFN(1, "%p\n", ap[i]);
390 	}
391 
392 	/* arrange for the new bootinfo to get copied */
393 	DPRINTF("bootinfo\n");
394 	kloader_copy(kv, &nbi, sizeof(struct kloader_bootinfo));
395 
396 	/* will be valid by the time the new kernel starts */
397 	kloader.rebootinfo = (void *)kv;
398 	/* kv += sizeof(struct kloader_bootinfo); */
399 
400 	/*
401 	 * Copy loader code
402 	 */
403 	KDASSERT(kloader.cur_pg);
404 	kloader.loader = (void *)PG_VADDR(kloader.cur_pg);
405 	memcpy(kloader.loader, kloader.ops->boot, PAGE_SIZE);
406 
407 	/* loader stack starts at the bottom of that page */
408 	kloader.loader_sp = (vaddr_t)kloader.loader + PAGE_SIZE;
409 
410 	DPRINTF("[loader] addr=%p sp=%p [kernel] entry=%p\n",
411 	    kloader.loader, (void *)kloader.loader_sp, (void *)nbi.entry);
412 
413 	return (0);
414  err:
415 	if (ph != NULL)
416 		free(ph, M_TEMP);
417 	if (sh != NULL)
418 		free(sh, M_TEMP);
419 	if (shstrtab != NULL)
420 		free(shstrtab, M_TEMP);
421 
422 	return 1;
423 }
424 
425 
426 int
kloader_alloc_memory(size_t sz)427 kloader_alloc_memory(size_t sz)
428 {
429 	int n, error;
430 
431 	n = (sz + BUCKET_SIZE - 1) / BUCKET_SIZE	/* kernel &co */
432 	    + 1;					/* 2nd loader */
433 
434 	error = uvm_pglistalloc(n * PAGE_SIZE, avail_start, avail_end,
435 	    PAGE_SIZE, 0, &kloader.pg_head, n, 0);
436 	if (error) {
437 		PRINTF("can't allocate memory.\n");
438 		return (1);
439 	}
440 	DPRINTF("allocated %d pages.\n", n);
441 
442 	kloader.cur_pg = TAILQ_FIRST(&kloader.pg_head);
443 	kloader.tagstart = (void *)PG_VADDR(kloader.cur_pg);
444 	kloader.cur_tag = NULL;
445 
446 	return (0);
447 }
448 
449 
450 struct kloader_page_tag *
kloader_get_tag(vaddr_t dst)451 kloader_get_tag(vaddr_t dst)
452 {
453 	struct vm_page *pg;
454 	vaddr_t addr;
455 	struct kloader_page_tag *tag;
456 
457 	tag = kloader.cur_tag;
458 	if (tag != NULL		/* has tag */
459 	    && tag->sz < BUCKET_SIZE /* that has free space */
460 	    && tag->dst + tag->sz == dst) /* and new data are contiguous */
461 	{
462 		DPRINTFN(1, "current tag %x/%x ok\n", tag->dst, tag->sz);
463 		return (tag);
464 	}
465 
466 	pg = kloader.cur_pg;
467 	KDASSERT(pg != NULL);
468 	kloader.cur_pg = TAILQ_NEXT(pg, pageq.queue);
469 
470 	addr = PG_VADDR(pg);
471 	tag = (void *)addr;
472 
473 	/*
474 	 * 2nd loader uses simple word-by-word copy, so destination
475 	 * address of a tag must be properly aligned.
476 	 */
477 	KASSERT(ALIGNED_POINTER(dst, register_t));
478 
479 	tag->src = addr + sizeof(struct kloader_page_tag);
480 	tag->dst = dst;
481 	tag->sz = 0;
482 	tag->next = 0;	/* Terminate. this member may overwrite after. */
483 	if (kloader.cur_tag)
484 		kloader.cur_tag->next = addr;
485 	kloader.cur_tag = tag;
486 
487 	return (tag);
488 }
489 
490 
491 /*
492  * Operations to populate kloader_page_tag's with data.
493  */
494 
495 void
kloader_from_file(vaddr_t dst,off_t ofs,size_t sz)496 kloader_from_file(vaddr_t dst, off_t ofs, size_t sz)
497 {
498 	struct kloader_page_tag *tag;
499 	size_t freesz;
500 
501 	while (sz > 0) {
502 		tag = kloader_get_tag(dst);
503 		KDASSERT(tag != NULL);
504 		freesz = BUCKET_SIZE - tag->sz;
505 		if (freesz > sz)
506 			freesz = sz;
507 
508 		DPRINTFN(1, "0x%08"PRIxVADDR" + 0x%zx <- 0x%lx\n", dst, freesz,
509 		    (unsigned long)ofs);
510 		kloader_read(ofs, freesz, (void *)(tag->src + tag->sz));
511 
512 		tag->sz += freesz;
513 		sz -= freesz;
514 		ofs += freesz;
515 		dst += freesz;
516 	}
517 }
518 
519 
520 void
kloader_copy(vaddr_t dst,const void * src,size_t sz)521 kloader_copy(vaddr_t dst, const void *src, size_t sz)
522 {
523 	struct kloader_page_tag *tag;
524 	size_t freesz;
525 
526 	while (sz > 0) {
527 		tag = kloader_get_tag(dst);
528 		KDASSERT(tag != NULL);
529 		freesz = BUCKET_SIZE - tag->sz;
530 		if (freesz > sz)
531 			freesz = sz;
532 
533 		DPRINTFN(1, "0x%08"PRIxVADDR" + 0x%zx <- %p\n", dst, freesz, src);
534 		memcpy((void *)(tag->src + tag->sz), src, freesz);
535 
536 		tag->sz += freesz;
537 		sz -= freesz;
538 		src = (const char *)src + freesz;
539 		dst += freesz;
540 	}
541 }
542 
543 
544 void
kloader_zero(vaddr_t dst,size_t sz)545 kloader_zero(vaddr_t dst, size_t sz)
546 {
547 	struct kloader_page_tag *tag;
548 	size_t freesz;
549 
550 	while (sz > 0) {
551 		tag = kloader_get_tag(dst);
552 		KDASSERT(tag != NULL);
553 		freesz = BUCKET_SIZE - tag->sz;
554 		if (freesz > sz)
555 			freesz = sz;
556 
557 		DPRINTFN(1, "0x%08"PRIxVADDR" + 0x%zx\n", dst, freesz);
558 		memset((void *)(tag->src + tag->sz), 0, freesz);
559 
560 		tag->sz += freesz;
561 		sz -= freesz;
562 		dst += freesz;
563 	}
564 }
565 
566 
567 void
kloader_load_segment(Elf_Phdr * p)568 kloader_load_segment(Elf_Phdr *p)
569 {
570 
571 	DPRINTF("memory 0x%08x 0x%x <- file 0x%x 0x%x\n",
572 	    p->p_vaddr, p->p_memsz, p->p_offset, p->p_filesz);
573 
574 	kloader_from_file(p->p_vaddr, p->p_offset, p->p_filesz);
575 #ifdef KLOADER_ZERO_BSS
576 	kloader_zero(p->p_vaddr + p->p_filesz, p->p_memsz - p->p_filesz);
577 #endif
578 }
579 
580 
581 /*
582  * file access
583  */
584 struct vnode *
kloader_open(const char * filename)585 kloader_open(const char *filename)
586 {
587 	struct pathbuf *pb;
588 	struct nameidata nid;
589 	int error;
590 
591 	pb = pathbuf_create(filename);
592 	if (pb == NULL) {
593 		PRINTF("%s: pathbuf_create failed\n", filename);
594 		return (NULL);
595 	}
596 
597 	NDINIT(&nid, LOOKUP, FOLLOW, pb);
598 
599 	error = namei(&nid);
600 	if (error != 0) {
601 		PRINTF("%s: namei failed, errno=%d\n", filename, error);
602 		pathbuf_destroy(pb);
603 		return (NULL);
604 	}
605 
606 	error = vn_open(&nid, FREAD, 0);
607 	if (error != 0) {
608 		PRINTF("%s: open failed, errno=%d\n", filename, error);
609 		pathbuf_destroy(pb);
610 		return (NULL);
611 	}
612 
613 	pathbuf_destroy(pb);
614 	return (nid.ni_vp);
615 }
616 
617 void
kloader_close(void)618 kloader_close(void)
619 {
620 	struct lwp *l = KLOADER_LWP;
621 	struct vnode *vp = kloader.vp;
622 
623 	VOP_UNLOCK(vp);
624 	vn_close(vp, FREAD, l->l_cred);
625 }
626 
627 int
kloader_read(size_t ofs,size_t size,void * buf)628 kloader_read(size_t ofs, size_t size, void *buf)
629 {
630 	struct lwp *l = KLOADER_LWP;
631 	struct vnode *vp = kloader.vp;
632 	size_t resid;
633 	int error;
634 
635 	error = vn_rdwr(UIO_READ, vp, buf, size, ofs, UIO_SYSSPACE,
636 	    IO_NODELOCKED | IO_SYNC, l->l_cred, &resid, NULL);
637 
638 	if (error)
639 		PRINTF("read error.\n");
640 
641 	return (error);
642 }
643 
644 
645 /*
646  * bootinfo
647  */
648 void
kloader_bootinfo_set(struct kloader_bootinfo * kbi,int argc,char * argv[],struct bootinfo * bi,int printok)649 kloader_bootinfo_set(struct kloader_bootinfo *kbi, int argc, char *argv[],
650     struct bootinfo *bi, int printok)
651 {
652 	char *p, *pend, *buf;
653 	int i;
654 
655 	kloader.bootinfo = kbi;
656 	buf = kbi->_argbuf;
657 	if (bi != NULL)
658 		memcpy(&kbi->bootinfo, bi, sizeof(struct bootinfo));
659 	kbi->argc = argc;
660 	kbi->argv = (char **)buf;
661 
662 	p = &buf[argc * sizeof(char **)];
663 	pend = &buf[KLOADER_KERNELARGS_MAX - 1];
664 
665 	for (i = 0; i < argc; i++) {
666 		char *q = argv[i];
667 		int len = strlen(q) + 1;
668 		if ((p + len) > pend) {
669 			kloader.bootinfo = NULL;
670 			if (printok)
671 				PRINTF("buffer insufficient.\n");
672 			return;
673 		}
674 		kbi->argv[i] = p;
675 		memcpy(p, q, len);
676 		p += len;
677 	}
678 }
679 
680 
681 #ifdef KLOADER_DEBUG
682 void
kloader_pagetag_dump(void)683 kloader_pagetag_dump(void)
684 {
685 	struct kloader_page_tag *tag = kloader.tagstart;
686 	struct kloader_page_tag *p, *op;
687 	bool print;
688 	int i, n;
689 
690 	p = tag;
691 	op = NULL;
692 	i = 0, n = 15;
693 
694 	PRINTF("[page tag chain]\n");
695 	do  {
696 		print = FALSE;
697 		if (i < n)
698 			print = TRUE;
699 		if ((uint32_t)p & 3) {
700 			printf("tag alignment error\n");
701 			break;
702 		}
703 		if ((p->src & 3) || (p->dst & 3)) {
704 			printf("data alignment error.\n");
705 			print = TRUE;
706 		}
707 
708 		if (print) {
709 			printf("[%2d] next 0x%08x src 0x%08x dst 0x%08x"
710 			    " sz 0x%x\n", i, p->next, p->src, p->dst, p->sz);
711 		} else if (i == n) {
712 			printf("[...]\n");
713 		}
714 		op = p;
715 		i++;
716 	} while ((p = (struct kloader_page_tag *)(p->next)) != 0);
717 
718 	if (op != NULL)
719 		printf("[%d(last)] next 0x%08x src 0x%08x dst 0x%08x sz 0x%x\n",
720 		    i - 1, op->next, op->src, op->dst, op->sz);
721 }
722 
723 #endif /* KLOADER_DEBUG */
724