Home | History | Annotate | Line # | Download | only in dev
kloader.c revision 1.25.8.1
      1  1.25.8.1      yamt /*	$NetBSD: kloader.c,v 1.25.8.1 2014/05/22 11:40:19 yamt 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.8.1      yamt __KERNEL_RCSID(0, "$NetBSD: kloader.c,v 1.25.8.1 2014/05/22 11:40:19 yamt 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 	extern paddr_t avail_start, avail_end;
    430       1.1       uch 	int n, error;
    431       1.1       uch 
    432       1.1       uch 	n = (sz + BUCKET_SIZE - 1) / BUCKET_SIZE	/* kernel &co */
    433       1.1       uch 	    + 1;					/* 2nd loader */
    434       1.1       uch 
    435       1.1       uch 	error = uvm_pglistalloc(n * PAGE_SIZE, avail_start, avail_end,
    436      1.21  uebayasi 	    PAGE_SIZE, 0, &kloader.pg_head, n, 0);
    437       1.1       uch 	if (error) {
    438       1.1       uch 		PRINTF("can't allocate memory.\n");
    439       1.1       uch 		return (1);
    440       1.1       uch 	}
    441       1.1       uch 	DPRINTF("allocated %d pages.\n", n);
    442       1.1       uch 
    443       1.1       uch 	kloader.cur_pg = TAILQ_FIRST(&kloader.pg_head);
    444       1.1       uch 	kloader.tagstart = (void *)PG_VADDR(kloader.cur_pg);
    445       1.1       uch 	kloader.cur_tag = NULL;
    446       1.1       uch 
    447       1.1       uch 	return (0);
    448       1.1       uch }
    449       1.1       uch 
    450       1.1       uch 
    451       1.1       uch struct kloader_page_tag *
    452       1.1       uch kloader_get_tag(vaddr_t dst)
    453       1.1       uch {
    454       1.1       uch 	struct vm_page *pg;
    455       1.1       uch 	vaddr_t addr;
    456       1.1       uch 	struct kloader_page_tag *tag;
    457       1.1       uch 
    458       1.1       uch 	tag = kloader.cur_tag;
    459       1.1       uch 	if (tag != NULL		/* has tag */
    460       1.1       uch 	    && tag->sz < BUCKET_SIZE /* that has free space */
    461       1.1       uch 	    && tag->dst + tag->sz == dst) /* and new data are contiguous */
    462       1.1       uch 	{
    463       1.1       uch 		DPRINTFN(1, "current tag %x/%x ok\n", tag->dst, tag->sz);
    464       1.1       uch 		return (tag);
    465       1.1       uch 	}
    466       1.1       uch 
    467       1.1       uch 	pg = kloader.cur_pg;
    468       1.1       uch 	KDASSERT(pg != NULL);
    469      1.18        ad 	kloader.cur_pg = TAILQ_NEXT(pg, pageq.queue);
    470       1.1       uch 
    471       1.1       uch 	addr = PG_VADDR(pg);
    472       1.1       uch 	tag = (void *)addr;
    473       1.1       uch 
    474       1.1       uch 	/*
    475       1.1       uch 	 * 2nd loader uses simple word-by-word copy, so destination
    476       1.1       uch 	 * address of a tag must be properly aligned.
    477       1.1       uch 	 */
    478       1.1       uch 	KASSERT(ALIGNED_POINTER(dst, register_t));
    479       1.1       uch 
    480       1.1       uch 	tag->src = addr + sizeof(struct kloader_page_tag);
    481       1.1       uch 	tag->dst = dst;
    482       1.1       uch 	tag->sz = 0;
    483       1.1       uch 	tag->next = 0;	/* Terminate. this member may overwrite after. */
    484       1.1       uch 	if (kloader.cur_tag)
    485       1.1       uch 		kloader.cur_tag->next = addr;
    486       1.1       uch 	kloader.cur_tag = tag;
    487       1.1       uch 
    488       1.1       uch 	return (tag);
    489       1.1       uch }
    490       1.1       uch 
    491       1.1       uch 
    492       1.1       uch /*
    493       1.1       uch  * Operations to populate kloader_page_tag's with data.
    494       1.1       uch  */
    495       1.1       uch 
    496       1.1       uch void
    497       1.1       uch kloader_from_file(vaddr_t dst, off_t ofs, size_t sz)
    498       1.1       uch {
    499       1.1       uch 	struct kloader_page_tag *tag;
    500       1.1       uch 	size_t freesz;
    501       1.1       uch 
    502       1.1       uch 	while (sz > 0) {
    503       1.1       uch 		tag = kloader_get_tag(dst);
    504       1.1       uch 		KDASSERT(tag != NULL);
    505       1.1       uch 		freesz = BUCKET_SIZE - tag->sz;
    506       1.1       uch 		if (freesz > sz)
    507       1.1       uch 			freesz = sz;
    508       1.1       uch 
    509      1.20      matt 		DPRINTFN(1, "0x%08"PRIxVADDR" + 0x%zx <- 0x%lx\n", dst, freesz,
    510       1.6     peter 		    (unsigned long)ofs);
    511       1.1       uch 		kloader_read(ofs, freesz, (void *)(tag->src + tag->sz));
    512       1.1       uch 
    513       1.1       uch 		tag->sz += freesz;
    514       1.1       uch 		sz -= freesz;
    515       1.1       uch 		ofs += freesz;
    516       1.1       uch 		dst += freesz;
    517       1.1       uch 	}
    518       1.1       uch }
    519       1.1       uch 
    520       1.1       uch 
    521       1.1       uch void
    522       1.1       uch kloader_copy(vaddr_t dst, const void *src, size_t sz)
    523       1.1       uch {
    524       1.1       uch 	struct kloader_page_tag *tag;
    525       1.1       uch 	size_t freesz;
    526       1.1       uch 
    527       1.1       uch 	while (sz > 0) {
    528       1.1       uch 		tag = kloader_get_tag(dst);
    529       1.1       uch 		KDASSERT(tag != NULL);
    530       1.1       uch 		freesz = BUCKET_SIZE - tag->sz;
    531       1.1       uch 		if (freesz > sz)
    532       1.1       uch 			freesz = sz;
    533       1.1       uch 
    534      1.20      matt 		DPRINTFN(1, "0x%08"PRIxVADDR" + 0x%zx <- %p\n", dst, freesz, src);
    535       1.1       uch 		memcpy((void *)(tag->src + tag->sz), src, freesz);
    536       1.1       uch 
    537       1.1       uch 		tag->sz += freesz;
    538       1.1       uch 		sz -= freesz;
    539       1.3       uwe 		src = (const char *)src + freesz;
    540       1.1       uch 		dst += freesz;
    541       1.1       uch 	}
    542       1.1       uch }
    543       1.1       uch 
    544       1.1       uch 
    545       1.1       uch void
    546       1.1       uch kloader_zero(vaddr_t dst, size_t sz)
    547       1.1       uch {
    548       1.1       uch 	struct kloader_page_tag *tag;
    549       1.1       uch 	size_t freesz;
    550       1.1       uch 
    551       1.1       uch 	while (sz > 0) {
    552       1.1       uch 		tag = kloader_get_tag(dst);
    553       1.1       uch 		KDASSERT(tag != NULL);
    554       1.1       uch 		freesz = BUCKET_SIZE - tag->sz;
    555       1.1       uch 		if (freesz > sz)
    556       1.1       uch 			freesz = sz;
    557       1.1       uch 
    558      1.20      matt 		DPRINTFN(1, "0x%08"PRIxVADDR" + 0x%zx\n", dst, freesz);
    559       1.1       uch 		memset((void *)(tag->src + tag->sz), 0, freesz);
    560       1.1       uch 
    561       1.1       uch 		tag->sz += freesz;
    562       1.1       uch 		sz -= freesz;
    563       1.1       uch 		dst += freesz;
    564       1.1       uch 	}
    565       1.1       uch }
    566       1.1       uch 
    567       1.1       uch 
    568       1.1       uch void
    569       1.1       uch kloader_load_segment(Elf_Phdr *p)
    570       1.1       uch {
    571       1.1       uch 
    572       1.1       uch 	DPRINTF("memory 0x%08x 0x%x <- file 0x%x 0x%x\n",
    573      1.21  uebayasi 	    p->p_vaddr, p->p_memsz, p->p_offset, p->p_filesz);
    574       1.1       uch 
    575       1.1       uch 	kloader_from_file(p->p_vaddr, p->p_offset, p->p_filesz);
    576       1.1       uch #ifdef KLOADER_ZERO_BSS
    577       1.1       uch 	kloader_zero(p->p_vaddr + p->p_filesz, p->p_memsz - p->p_filesz);
    578       1.1       uch #endif
    579       1.1       uch }
    580       1.1       uch 
    581       1.1       uch 
    582       1.1       uch /*
    583       1.1       uch  * file access
    584       1.1       uch  */
    585       1.1       uch struct vnode *
    586       1.1       uch kloader_open(const char *filename)
    587       1.1       uch {
    588      1.24  dholland 	struct pathbuf *pb;
    589       1.1       uch 	struct nameidata nid;
    590       1.8       uwe 	int error;
    591       1.1       uch 
    592      1.24  dholland 	pb = pathbuf_create(filename);
    593      1.24  dholland 	if (pb == NULL) {
    594      1.24  dholland 		PRINTF("%s: pathbuf_create failed\n", filename);
    595      1.24  dholland 		return (NULL);
    596      1.24  dholland 	}
    597      1.24  dholland 
    598      1.24  dholland 	NDINIT(&nid, LOOKUP, FOLLOW, pb);
    599       1.1       uch 
    600       1.8       uwe 	error = namei(&nid);
    601       1.8       uwe 	if (error != 0) {
    602       1.8       uwe 		PRINTF("%s: namei failed, errno=%d\n", filename, error);
    603      1.24  dholland 		pathbuf_destroy(pb);
    604       1.8       uwe 		return (NULL);
    605       1.1       uch 	}
    606       1.1       uch 
    607       1.8       uwe 	error = vn_open(&nid, FREAD, 0);
    608       1.8       uwe 	if (error != 0) {
    609       1.8       uwe 		PRINTF("%s: open failed, errno=%d\n", filename, error);
    610      1.25  dholland 		pathbuf_destroy(pb);
    611       1.8       uwe 		return (NULL);
    612       1.1       uch 	}
    613       1.1       uch 
    614      1.24  dholland 	pathbuf_destroy(pb);
    615       1.1       uch 	return (nid.ni_vp);
    616       1.1       uch }
    617       1.1       uch 
    618       1.1       uch void
    619      1.19    cegger kloader_close(void)
    620       1.1       uch {
    621       1.7  christos 	struct lwp *l = KLOADER_LWP;
    622       1.1       uch 	struct vnode *vp = kloader.vp;
    623       1.1       uch 
    624      1.22   hannken 	VOP_UNLOCK(vp);
    625      1.16        ad 	vn_close(vp, FREAD, l->l_cred);
    626       1.1       uch }
    627       1.1       uch 
    628       1.1       uch int
    629       1.1       uch kloader_read(size_t ofs, size_t size, void *buf)
    630       1.1       uch {
    631       1.7  christos 	struct lwp *l = KLOADER_LWP;
    632       1.1       uch 	struct vnode *vp = kloader.vp;
    633       1.1       uch 	size_t resid;
    634       1.1       uch 	int error;
    635       1.1       uch 
    636       1.1       uch 	error = vn_rdwr(UIO_READ, vp, buf, size, ofs, UIO_SYSSPACE,
    637      1.11        ad 	    IO_NODELOCKED | IO_SYNC, l->l_cred, &resid, NULL);
    638       1.1       uch 
    639       1.1       uch 	if (error)
    640       1.1       uch 		PRINTF("read error.\n");
    641       1.1       uch 
    642       1.1       uch 	return (error);
    643       1.1       uch }
    644       1.1       uch 
    645       1.1       uch 
    646       1.1       uch /*
    647       1.1       uch  * bootinfo
    648       1.1       uch  */
    649       1.1       uch void
    650       1.1       uch kloader_bootinfo_set(struct kloader_bootinfo *kbi, int argc, char *argv[],
    651       1.1       uch     struct bootinfo *bi, int printok)
    652       1.1       uch {
    653       1.1       uch 	char *p, *pend, *buf;
    654       1.1       uch 	int i;
    655       1.1       uch 
    656       1.1       uch 	kloader.bootinfo = kbi;
    657       1.1       uch 	buf = kbi->_argbuf;
    658       1.1       uch 	if (bi != NULL)
    659       1.1       uch 		memcpy(&kbi->bootinfo, bi, sizeof(struct bootinfo));
    660       1.1       uch 	kbi->argc = argc;
    661       1.1       uch 	kbi->argv = (char **)buf;
    662       1.1       uch 
    663       1.1       uch 	p = &buf[argc * sizeof(char **)];
    664       1.1       uch 	pend = &buf[KLOADER_KERNELARGS_MAX - 1];
    665       1.1       uch 
    666       1.1       uch 	for (i = 0; i < argc; i++) {
    667       1.1       uch 		char *q = argv[i];
    668       1.1       uch 		int len = strlen(q) + 1;
    669       1.1       uch 		if ((p + len) > pend) {
    670       1.1       uch 			kloader.bootinfo = NULL;
    671       1.1       uch 			if (printok)
    672       1.1       uch 				PRINTF("buffer insufficient.\n");
    673       1.1       uch 			return;
    674       1.1       uch 		}
    675       1.1       uch 		kbi->argv[i] = p;
    676       1.1       uch 		memcpy(p, q, len);
    677       1.1       uch 		p += len;
    678       1.1       uch 	}
    679       1.1       uch }
    680       1.1       uch 
    681       1.1       uch 
    682       1.1       uch #ifdef KLOADER_DEBUG
    683       1.1       uch void
    684      1.19    cegger kloader_pagetag_dump(void)
    685       1.1       uch {
    686       1.1       uch 	struct kloader_page_tag *tag = kloader.tagstart;
    687       1.1       uch 	struct kloader_page_tag *p, *op;
    688      1.12   thorpej 	bool print;
    689       1.1       uch 	int i, n;
    690       1.1       uch 
    691       1.1       uch 	p = tag;
    692       1.1       uch 	op = NULL;
    693       1.1       uch 	i = 0, n = 15;
    694       1.1       uch 
    695       1.1       uch 	PRINTF("[page tag chain]\n");
    696       1.1       uch 	do  {
    697       1.1       uch 		print = FALSE;
    698       1.1       uch 		if (i < n)
    699       1.1       uch 			print = TRUE;
    700       1.9       uwe 		if ((uint32_t)p & 3) {
    701       1.1       uch 			printf("tag alignment error\n");
    702       1.1       uch 			break;
    703       1.1       uch 		}
    704       1.1       uch 		if ((p->src & 3) || (p->dst & 3)) {
    705  1.25.8.1      yamt 			printf("data alignment error.\n");
    706       1.1       uch 			print = TRUE;
    707       1.1       uch 		}
    708       1.1       uch 
    709       1.1       uch 		if (print) {
    710       1.1       uch 			printf("[%2d] next 0x%08x src 0x%08x dst 0x%08x"
    711       1.1       uch 			    " sz 0x%x\n", i, p->next, p->src, p->dst, p->sz);
    712       1.1       uch 		} else if (i == n) {
    713       1.1       uch 			printf("[...]\n");
    714       1.1       uch 		}
    715       1.1       uch 		op = p;
    716       1.1       uch 		i++;
    717       1.1       uch 	} while ((p = (struct kloader_page_tag *)(p->next)) != 0);
    718       1.1       uch 
    719       1.1       uch 	if (op != NULL)
    720       1.1       uch 		printf("[%d(last)] next 0x%08x src 0x%08x dst 0x%08x sz 0x%x\n",
    721       1.1       uch 		    i - 1, op->next, op->src, op->dst, op->sz);
    722       1.1       uch }
    723       1.1       uch 
    724       1.1       uch #endif /* KLOADER_DEBUG */
    725