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