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