2006-05-15 01:42:26 +04:00
|
|
|
/* $NetBSD: kloader.c,v 1.10 2006/05/14 21:42:26 elad Exp $ */
|
2002-01-27 08:14:33 +03:00
|
|
|
|
|
|
|
/*-
|
2004-07-06 17:09:18 +04:00
|
|
|
* Copyright (c) 2001, 2002, 2004 The NetBSD Foundation, Inc.
|
2002-01-27 08:14:33 +03:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. All advertising materials mentioning features or use of this software
|
|
|
|
* must display the following acknowledgement:
|
|
|
|
* This product includes software developed by the NetBSD
|
|
|
|
* Foundation, Inc. and its contributors.
|
|
|
|
* 4. Neither the name of The NetBSD Foundation nor the names of its
|
|
|
|
* contributors may be used to endorse or promote products derived
|
|
|
|
* from this software without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
|
|
|
|
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
|
|
|
|
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
|
|
|
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
|
|
|
|
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2003-07-15 06:29:24 +04:00
|
|
|
#include <sys/cdefs.h>
|
2006-05-15 01:42:26 +04:00
|
|
|
__KERNEL_RCSID(0, "$NetBSD: kloader.c,v 1.10 2006/05/14 21:42:26 elad Exp $");
|
2003-07-15 06:29:24 +04:00
|
|
|
|
2002-01-29 21:44:22 +03:00
|
|
|
#include "debug_kloader.h"
|
2002-01-27 08:14:33 +03:00
|
|
|
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/systm.h>
|
2004-03-23 06:39:11 +03:00
|
|
|
#include <sys/malloc.h>
|
2002-01-27 08:14:33 +03:00
|
|
|
#include <sys/proc.h>
|
|
|
|
#include <sys/vnode.h>
|
|
|
|
#include <sys/namei.h>
|
|
|
|
#include <sys/fcntl.h>
|
2004-07-06 17:09:18 +04:00
|
|
|
#define ELFSIZE 32
|
2002-01-27 08:14:33 +03:00
|
|
|
#include <sys/exec_elf.h>
|
|
|
|
|
|
|
|
#include <uvm/uvm_extern.h>
|
|
|
|
|
2002-01-29 21:44:22 +03:00
|
|
|
#include <machine/kloader.h>
|
2002-01-27 08:14:33 +03:00
|
|
|
|
2004-07-06 17:09:18 +04:00
|
|
|
#define PRINTF(fmt, args...) printf("kloader: " fmt, ##args)
|
|
|
|
|
2002-01-27 08:14:33 +03:00
|
|
|
#ifdef KLOADER_DEBUG
|
2004-07-06 17:09:18 +04:00
|
|
|
int kloader_debug = 1;
|
|
|
|
#define DPRINTF(fmt, args...) \
|
|
|
|
if (kloader_debug) \
|
|
|
|
printf("%s: " fmt, __FUNCTION__ , ##args)
|
|
|
|
#define _DPRINTF(fmt, args...) \
|
|
|
|
if (kloader_debug) \
|
|
|
|
printf(fmt, ##args)
|
|
|
|
#define DPRINTFN(n, fmt, args...) \
|
|
|
|
if (kloader_debug > (n)) \
|
|
|
|
printf("%s: " fmt, __FUNCTION__ , ##args)
|
|
|
|
#define _DPRINTFN(n, fmt, args...) \
|
|
|
|
if (kloader_debug > (n)) \
|
|
|
|
printf(fmt, ##args)
|
|
|
|
#define STATIC
|
|
|
|
#else
|
|
|
|
#define DPRINTF(fmt, args...) ((void)0)
|
|
|
|
#define _DPRINTF(fmt, args...) ((void)0)
|
|
|
|
#define DPRINTFN(n, fmt, args...) ((void)0)
|
|
|
|
#define _DPRINTFN(n, fmt, args...) ((void)0)
|
|
|
|
#define STATIC static
|
2002-01-27 08:14:33 +03:00
|
|
|
#endif
|
|
|
|
|
2002-01-29 21:44:22 +03:00
|
|
|
struct kloader {
|
2002-01-27 08:14:33 +03:00
|
|
|
struct pglist pg_head;
|
|
|
|
struct vm_page *cur_pg;
|
2004-03-23 02:10:55 +03:00
|
|
|
struct kloader_page_tag *cur_tag;
|
2002-01-27 08:14:33 +03:00
|
|
|
struct vnode *vp;
|
|
|
|
struct kloader_page_tag *tagstart;
|
|
|
|
struct kloader_bootinfo *bootinfo;
|
2004-03-23 02:10:55 +03:00
|
|
|
struct kloader_bootinfo *rebootinfo;
|
2002-01-27 08:14:33 +03:00
|
|
|
vaddr_t loader_sp;
|
2002-01-29 21:44:22 +03:00
|
|
|
kloader_bootfunc_t *loader;
|
2002-01-27 08:14:33 +03:00
|
|
|
int setuped;
|
2002-02-11 20:13:28 +03:00
|
|
|
int called;
|
2002-01-29 21:44:22 +03:00
|
|
|
struct kloader_ops *ops;
|
|
|
|
};
|
|
|
|
|
2004-07-06 17:09:18 +04:00
|
|
|
#define BUCKET_SIZE (PAGE_SIZE - sizeof(struct kloader_page_tag))
|
2005-12-11 15:16:03 +03:00
|
|
|
#define KLOADER_LWP (&lwp0)
|
2002-01-29 21:44:22 +03:00
|
|
|
STATIC struct kloader kloader;
|
2002-01-27 08:14:33 +03:00
|
|
|
|
2004-07-06 17:09:18 +04:00
|
|
|
#define ROUND4(x) (((x) + 3) & ~3)
|
2004-03-23 06:39:11 +03:00
|
|
|
|
2002-01-27 08:14:33 +03:00
|
|
|
STATIC int kloader_load(void);
|
2004-03-23 02:10:55 +03:00
|
|
|
|
2002-01-27 08:14:33 +03:00
|
|
|
STATIC int kloader_alloc_memory(size_t);
|
2004-03-23 02:10:55 +03:00
|
|
|
STATIC struct kloader_page_tag *kloader_get_tag(vaddr_t);
|
2004-03-23 06:39:11 +03:00
|
|
|
STATIC void kloader_from_file(vaddr_t, off_t, size_t);
|
|
|
|
STATIC void kloader_copy(vaddr_t, const void *, size_t);
|
|
|
|
STATIC void kloader_zero(vaddr_t, size_t);
|
2004-03-23 02:10:55 +03:00
|
|
|
|
|
|
|
STATIC void kloader_load_segment(Elf_Phdr *);
|
|
|
|
|
2002-01-27 08:14:33 +03:00
|
|
|
STATIC struct vnode *kloader_open(const char *);
|
|
|
|
STATIC void kloader_close(void);
|
|
|
|
STATIC int kloader_read(size_t, size_t, void *);
|
|
|
|
|
|
|
|
#ifdef KLOADER_DEBUG
|
|
|
|
STATIC void kloader_pagetag_dump(void);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
void
|
2002-01-29 21:44:22 +03:00
|
|
|
__kloader_reboot_setup(struct kloader_ops *ops, const char *filename)
|
2002-01-27 08:14:33 +03:00
|
|
|
{
|
|
|
|
|
|
|
|
if (kloader.bootinfo == NULL) {
|
|
|
|
PRINTF("No bootinfo.\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2002-01-29 21:44:22 +03:00
|
|
|
if (ops == NULL || ops->jump == NULL || ops->boot == NULL) {
|
|
|
|
PRINTF("No boot operations.\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
kloader.ops = ops;
|
|
|
|
|
2004-07-06 17:09:18 +04:00
|
|
|
if (kloader.called++ == 0) {
|
|
|
|
PRINTF("kernel file name: %s\n", filename);
|
|
|
|
kloader.vp = kloader_open(filename);
|
|
|
|
if (kloader.vp == NULL)
|
|
|
|
return;
|
2002-01-27 08:14:33 +03:00
|
|
|
|
2004-07-06 17:09:18 +04:00
|
|
|
if (kloader_load() == 0) {
|
|
|
|
kloader.setuped = TRUE;
|
2002-01-27 08:14:33 +03:00
|
|
|
#ifdef KLOADER_DEBUG
|
2004-07-06 17:09:18 +04:00
|
|
|
kloader_pagetag_dump();
|
2002-01-27 08:14:33 +03:00
|
|
|
#endif
|
2004-07-06 17:09:18 +04:00
|
|
|
}
|
|
|
|
kloader_close();
|
|
|
|
} else {
|
|
|
|
/* Fatal case. reboot from DDB etc. */
|
|
|
|
kloader_reboot();
|
|
|
|
}
|
2002-01-27 08:14:33 +03:00
|
|
|
}
|
|
|
|
|
2004-03-23 02:10:55 +03:00
|
|
|
|
2002-01-27 08:14:33 +03:00
|
|
|
void
|
|
|
|
kloader_reboot()
|
|
|
|
{
|
|
|
|
|
2004-07-06 17:09:18 +04:00
|
|
|
if (kloader.setuped) {
|
|
|
|
PRINTF("Rebooting...\n");
|
|
|
|
(*kloader.ops->jump)(kloader.loader, kloader.loader_sp,
|
|
|
|
kloader.rebootinfo, kloader.tagstart);
|
|
|
|
}
|
2002-01-27 08:14:33 +03:00
|
|
|
|
2004-07-06 17:09:18 +04:00
|
|
|
if (kloader.ops->reset != NULL) {
|
2005-09-24 15:20:07 +04:00
|
|
|
PRINTF("Resetting...\n");
|
2004-07-06 17:09:18 +04:00
|
|
|
(*kloader.ops->reset)();
|
|
|
|
}
|
|
|
|
while (/*CONSTCOND*/1)
|
|
|
|
;
|
|
|
|
/* NOTREACHED */
|
2002-01-27 08:14:33 +03:00
|
|
|
}
|
|
|
|
|
2004-03-23 02:10:55 +03:00
|
|
|
|
2002-01-27 08:14:33 +03:00
|
|
|
int
|
|
|
|
kloader_load()
|
|
|
|
{
|
|
|
|
Elf_Ehdr eh;
|
2004-06-12 18:31:49 +04:00
|
|
|
Elf_Phdr *ph, *p;
|
|
|
|
Elf_Shdr *sh;
|
2004-03-23 06:39:11 +03:00
|
|
|
Elf_Addr entry;
|
2002-01-27 08:14:33 +03:00
|
|
|
vaddr_t kv;
|
|
|
|
size_t sz;
|
2004-03-23 06:39:11 +03:00
|
|
|
size_t shstrsz;
|
|
|
|
char *shstrtab;
|
|
|
|
int symndx, strndx;
|
2004-03-23 02:10:55 +03:00
|
|
|
size_t ksymsz;
|
|
|
|
struct kloader_bootinfo nbi; /* new boot info */
|
|
|
|
char *oldbuf, *newbuf;
|
|
|
|
char **ap;
|
2002-01-27 08:14:33 +03:00
|
|
|
int i;
|
|
|
|
|
2004-06-12 18:31:49 +04:00
|
|
|
ph = NULL;
|
|
|
|
sh = NULL;
|
|
|
|
shstrtab = NULL;
|
|
|
|
|
2002-01-27 08:14:33 +03:00
|
|
|
/* read kernel's ELF header */
|
|
|
|
kloader_read(0, sizeof(Elf_Ehdr), &eh);
|
2004-06-12 18:31:49 +04:00
|
|
|
|
2002-01-27 08:14:33 +03:00
|
|
|
if (eh.e_ident[EI_MAG0] != ELFMAG0 ||
|
|
|
|
eh.e_ident[EI_MAG1] != ELFMAG1 ||
|
|
|
|
eh.e_ident[EI_MAG2] != ELFMAG2 ||
|
|
|
|
eh.e_ident[EI_MAG3] != ELFMAG3) {
|
2004-03-23 02:10:55 +03:00
|
|
|
PRINTF("not an ELF file\n");
|
2004-06-12 18:31:49 +04:00
|
|
|
goto err;
|
2002-01-27 08:14:33 +03:00
|
|
|
}
|
|
|
|
|
2004-03-23 02:10:55 +03:00
|
|
|
/* read program headers */
|
2004-06-12 18:31:49 +04:00
|
|
|
sz = eh.e_phentsize * eh.e_phnum;
|
|
|
|
if ((ph = malloc(sz, M_TEMP, M_NOWAIT)) == NULL) {
|
|
|
|
PRINTF("can't allocate program header table.\n");
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
if (kloader_read(eh.e_phoff, sz, ph) != 0) {
|
|
|
|
PRINTF("program header read error.\n");
|
|
|
|
goto err;
|
|
|
|
}
|
2004-03-23 02:10:55 +03:00
|
|
|
|
|
|
|
/* read section headers */
|
2004-06-12 18:31:49 +04:00
|
|
|
sz = eh.e_shentsize * eh.e_shnum;
|
|
|
|
if ((sh = malloc(sz, M_TEMP, M_NOWAIT)) == NULL) {
|
|
|
|
PRINTF("can't allocate section header table.\n");
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
if (kloader_read(eh.e_shoff, eh.e_shentsize * eh.e_shnum, sh) != 0) {
|
|
|
|
PRINTF("section header read error.\n");
|
|
|
|
goto err;
|
|
|
|
}
|
2002-01-27 08:14:33 +03:00
|
|
|
|
2004-03-23 06:39:11 +03:00
|
|
|
/* read section names */
|
|
|
|
shstrsz = ROUND4(sh[eh.e_shstrndx].sh_size);
|
|
|
|
shstrtab = malloc(shstrsz, M_TEMP, M_NOWAIT);
|
|
|
|
if (shstrtab == NULL) {
|
|
|
|
PRINTF("unable to allocate memory for .shstrtab\n");
|
2004-06-12 18:31:49 +04:00
|
|
|
goto err;
|
2004-03-23 06:39:11 +03:00
|
|
|
}
|
|
|
|
DPRINTF("reading 0x%x bytes of .shstrtab at 0x%x\n",
|
|
|
|
sh[eh.e_shstrndx].sh_size, sh[eh.e_shstrndx].sh_offset);
|
|
|
|
kloader_read(sh[eh.e_shstrndx].sh_offset, sh[eh.e_shstrndx].sh_size,
|
|
|
|
shstrtab);
|
|
|
|
|
|
|
|
/* save entry point, code to construct symbol table overwrites it */
|
|
|
|
entry = eh.e_entry;
|
|
|
|
|
2002-01-27 08:14:33 +03:00
|
|
|
|
2004-03-23 02:10:55 +03:00
|
|
|
/*
|
2005-09-24 15:20:07 +04:00
|
|
|
* Calculate memory size
|
2004-03-23 02:10:55 +03:00
|
|
|
*/
|
|
|
|
sz = 0;
|
|
|
|
|
|
|
|
/* loadable segments */
|
|
|
|
for (i = 0; i < eh.e_phnum; i++) {
|
2002-01-27 08:14:33 +03:00
|
|
|
if (ph[i].p_type == PT_LOAD) {
|
2004-03-23 06:39:11 +03:00
|
|
|
DPRINTF("segment %d size = file 0x%x memory 0x%x\n",
|
2004-03-23 02:10:55 +03:00
|
|
|
i, ph[i].p_filesz, ph[i].p_memsz);
|
|
|
|
#ifdef KLOADER_ZERO_BSS
|
|
|
|
sz += round_page(ph[i].p_memsz);
|
|
|
|
#else
|
|
|
|
sz += round_page(ph[i].p_filesz);
|
|
|
|
#endif
|
2004-03-25 07:04:28 +03:00
|
|
|
sz += PAGE_SIZE; /* compensate for partial last tag */
|
2002-01-27 08:14:33 +03:00
|
|
|
}
|
|
|
|
}
|
2004-03-23 02:10:55 +03:00
|
|
|
|
|
|
|
if (sz == 0) /* nothing to load? */
|
2004-06-12 18:31:49 +04:00
|
|
|
goto err;
|
2004-03-23 02:10:55 +03:00
|
|
|
|
2004-03-23 06:39:11 +03:00
|
|
|
/* symbols/strings sections */
|
|
|
|
symndx = strndx = -1;
|
|
|
|
for (i = 0; i < eh.e_shnum; i++) {
|
|
|
|
if (strcmp(shstrtab + sh[i].sh_name, ".symtab") == 0)
|
|
|
|
symndx = i;
|
|
|
|
else if (strcmp(shstrtab + sh[i].sh_name, ".strtab") == 0)
|
|
|
|
strndx = i;
|
|
|
|
else if (i != eh.e_shstrndx)
|
|
|
|
/* while here, mark all other sections as unused */
|
|
|
|
sh[i].sh_type = SHT_NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (symndx < 0 || strndx < 0) {
|
|
|
|
if (symndx < 0)
|
|
|
|
PRINTF("no .symtab section\n");
|
|
|
|
if (strndx < 0)
|
|
|
|
PRINTF("no .strtab section\n");
|
|
|
|
ksymsz = SELFMAG; /* just a bad magic */
|
|
|
|
} else {
|
|
|
|
ksymsz = sizeof(Elf_Ehdr)
|
|
|
|
+ eh.e_shentsize * eh.e_shnum
|
|
|
|
+ shstrsz /* rounded to 4 bytes */
|
|
|
|
+ sh[symndx].sh_size
|
|
|
|
+ sh[strndx].sh_size;
|
2005-09-24 21:00:20 +04:00
|
|
|
DPRINTF("ksyms size = 0x%zx\n", ksymsz);
|
2004-03-23 06:39:11 +03:00
|
|
|
}
|
|
|
|
sz += ROUND4(ksymsz);
|
2004-03-23 02:10:55 +03:00
|
|
|
|
|
|
|
/* boot info for the new kernel */
|
|
|
|
sz += sizeof(struct kloader_bootinfo);
|
2002-01-27 08:14:33 +03:00
|
|
|
|
|
|
|
/* get memory for new kernel */
|
|
|
|
if (kloader_alloc_memory(sz) != 0)
|
2004-06-12 18:31:49 +04:00
|
|
|
goto err;
|
2002-01-27 08:14:33 +03:00
|
|
|
|
2004-03-23 02:10:55 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Copy new kernel in.
|
|
|
|
*/
|
|
|
|
kv = 0; /* XXX: -Wuninitialized */
|
2002-01-27 08:14:33 +03:00
|
|
|
for (i = 0, p = ph; i < eh.e_phnum; i++, p++) {
|
|
|
|
if (p->p_type == PT_LOAD) {
|
2004-03-23 02:10:55 +03:00
|
|
|
kloader_load_segment(p);
|
2004-06-12 02:56:00 +04:00
|
|
|
kv = p->p_vaddr + ROUND4(p->p_memsz);
|
2002-01-27 08:14:33 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-03-23 06:39:11 +03:00
|
|
|
|
2004-03-23 02:10:55 +03:00
|
|
|
/*
|
2004-03-23 06:39:11 +03:00
|
|
|
* Construct symbol table for ksyms.
|
2004-03-23 02:10:55 +03:00
|
|
|
*/
|
2004-03-23 06:39:11 +03:00
|
|
|
if (symndx < 0 || strndx < 0) {
|
|
|
|
kloader_zero(kv, SELFMAG);
|
|
|
|
kv += SELFMAG;
|
|
|
|
} else {
|
|
|
|
Elf_Off eoff;
|
|
|
|
off_t symoff, stroff;
|
|
|
|
|
|
|
|
/* save offsets of .symtab and .strtab before we change them */
|
|
|
|
symoff = sh[symndx].sh_offset;
|
|
|
|
stroff = sh[strndx].sh_offset;
|
|
|
|
|
|
|
|
/* no loadable segments */
|
|
|
|
eh.e_entry = 0;
|
|
|
|
eh.e_phnum = 0;
|
|
|
|
eh.e_phoff = 0;
|
|
|
|
|
|
|
|
/* change offsets to reflect new layout */
|
|
|
|
eoff = sizeof(Elf_Ehdr);
|
|
|
|
eh.e_shoff = eoff;
|
|
|
|
|
|
|
|
eoff += eh.e_shentsize * eh.e_shnum;
|
|
|
|
sh[eh.e_shstrndx].sh_offset = eoff;
|
|
|
|
|
|
|
|
eoff += shstrsz;
|
|
|
|
sh[symndx].sh_offset = eoff;
|
|
|
|
|
|
|
|
eoff += sh[symndx].sh_size;
|
|
|
|
sh[strndx].sh_offset = eoff;
|
|
|
|
|
|
|
|
/* local copies massaged, can serve them now */
|
|
|
|
DPRINTF("ksyms ELF header\n");
|
|
|
|
kloader_copy(kv, &eh, sizeof(Elf_Ehdr));
|
|
|
|
kv += sizeof(Elf_Ehdr);
|
|
|
|
|
|
|
|
DPRINTF("ksyms section headers\n");
|
|
|
|
kloader_copy(kv, sh, eh.e_shentsize * eh.e_shnum);
|
|
|
|
kv += eh.e_shentsize * eh.e_shnum;
|
|
|
|
|
|
|
|
DPRINTF("ksyms .shstrtab\n");
|
|
|
|
kloader_copy(kv, shstrtab, shstrsz);
|
|
|
|
kv += shstrsz;
|
|
|
|
|
|
|
|
DPRINTF("ksyms .symtab\n");
|
|
|
|
kloader_from_file(kv, symoff, sh[symndx].sh_size);
|
|
|
|
kv += sh[symndx].sh_size;
|
|
|
|
|
|
|
|
DPRINTF("ksyms .strtab\n");
|
|
|
|
kloader_from_file(kv, stroff, ROUND4(sh[strndx].sh_size));
|
|
|
|
kv += ROUND4(sh[strndx].sh_size);
|
|
|
|
}
|
2004-03-23 02:10:55 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Create boot info to pass to the new kernel.
|
|
|
|
* All pointers in it are *not* valid until the new kernel runs!
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* get a private copy of current bootinfo to vivisect */
|
|
|
|
memcpy(&nbi, kloader.bootinfo,
|
|
|
|
sizeof(struct kloader_bootinfo));
|
|
|
|
|
|
|
|
/* new kernel entry point */
|
2004-03-23 06:39:11 +03:00
|
|
|
nbi.entry = entry;
|
2004-03-23 02:10:55 +03:00
|
|
|
|
|
|
|
/* where args currently are, see kloader_bootinfo_set() */
|
|
|
|
oldbuf = &kloader.bootinfo->_argbuf[0];
|
|
|
|
|
|
|
|
/* where args *will* be after boot code copied them */
|
|
|
|
newbuf = (char *)(void *)kv
|
|
|
|
+ offsetof(struct kloader_bootinfo, _argbuf);
|
|
|
|
|
2004-03-23 06:39:11 +03:00
|
|
|
DPRINTF("argv: old %p -> new %p\n", oldbuf, newbuf);
|
2004-03-23 02:10:55 +03:00
|
|
|
|
|
|
|
/* not a valid pointer in this kernel! */
|
|
|
|
nbi.argv = (void *)newbuf;
|
|
|
|
|
|
|
|
/* local copy that we populate with new (not yet valid) pointers */
|
|
|
|
ap = (char **)(void *)nbi._argbuf;
|
|
|
|
|
|
|
|
for (i = 0; i < kloader.bootinfo->argc; ++i) {
|
2004-03-23 06:39:11 +03:00
|
|
|
DPRINTFN(1, " [%d]: %p -> ", i, kloader.bootinfo->argv[i]);
|
2004-03-23 02:10:55 +03:00
|
|
|
ap[i] = newbuf +
|
|
|
|
(kloader.bootinfo->argv[i] - oldbuf);
|
|
|
|
_DPRINTFN(1, "%p\n", ap[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* arrange for the new bootinfo to get copied */
|
2004-03-23 06:39:11 +03:00
|
|
|
DPRINTF("bootinfo\n");
|
2004-03-23 02:10:55 +03:00
|
|
|
kloader_copy(kv, &nbi, sizeof(struct kloader_bootinfo));
|
|
|
|
|
|
|
|
/* will be valid by the time the new kernel starts */
|
|
|
|
kloader.rebootinfo = (void *)kv;
|
2004-03-23 06:39:11 +03:00
|
|
|
/* kv += sizeof(struct kloader_bootinfo); */
|
2004-03-23 02:10:55 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Copy loader code
|
|
|
|
*/
|
2002-01-27 08:14:33 +03:00
|
|
|
KDASSERT(kloader.cur_pg);
|
|
|
|
kloader.loader = (void *)PG_VADDR(kloader.cur_pg);
|
2002-01-29 21:44:22 +03:00
|
|
|
memcpy(kloader.loader, kloader.ops->boot, PAGE_SIZE);
|
2002-01-27 08:14:33 +03:00
|
|
|
|
2004-03-23 02:10:55 +03:00
|
|
|
/* loader stack starts at the bottom of that page */
|
2002-01-29 21:44:22 +03:00
|
|
|
kloader.loader_sp = (vaddr_t)kloader.loader + PAGE_SIZE;
|
2002-01-27 08:14:33 +03:00
|
|
|
|
2004-03-23 06:39:11 +03:00
|
|
|
DPRINTF("[loader] addr=%p sp=%p [kernel] entry=%p\n",
|
|
|
|
kloader.loader, (void *)kloader.loader_sp, (void *)nbi.entry);
|
2002-01-27 08:14:33 +03:00
|
|
|
|
|
|
|
return (0);
|
2004-06-12 18:31:49 +04:00
|
|
|
err:
|
|
|
|
if (ph != NULL)
|
|
|
|
free(ph, M_TEMP);
|
|
|
|
if (sh != NULL)
|
|
|
|
free(sh, M_TEMP);
|
|
|
|
if (shstrtab != NULL)
|
|
|
|
free(shstrtab, M_TEMP);
|
|
|
|
|
|
|
|
return 1;
|
2002-01-27 08:14:33 +03:00
|
|
|
}
|
|
|
|
|
2004-03-23 02:10:55 +03:00
|
|
|
|
2002-01-27 08:14:33 +03:00
|
|
|
int
|
|
|
|
kloader_alloc_memory(size_t sz)
|
|
|
|
{
|
2002-01-29 21:44:22 +03:00
|
|
|
extern paddr_t avail_start, avail_end;
|
2002-06-02 18:44:35 +04:00
|
|
|
int n, error;
|
2002-01-27 08:14:33 +03:00
|
|
|
|
2004-03-23 02:10:55 +03:00
|
|
|
n = (sz + BUCKET_SIZE - 1) / BUCKET_SIZE /* kernel &co */
|
2002-01-29 21:44:22 +03:00
|
|
|
+ 1; /* 2nd loader */
|
2002-01-27 08:14:33 +03:00
|
|
|
|
2002-06-02 18:44:35 +04:00
|
|
|
error = uvm_pglistalloc(n * PAGE_SIZE, avail_start, avail_end,
|
|
|
|
PAGE_SIZE, 0, &kloader.pg_head, n, 0);
|
|
|
|
if (error) {
|
|
|
|
PRINTF("can't allocate memory.\n");
|
|
|
|
return (1);
|
2002-01-27 08:14:33 +03:00
|
|
|
}
|
2004-06-12 18:31:49 +04:00
|
|
|
DPRINTF("allocated %d pages.\n", n);
|
2002-01-27 08:14:33 +03:00
|
|
|
|
|
|
|
kloader.cur_pg = TAILQ_FIRST(&kloader.pg_head);
|
|
|
|
kloader.tagstart = (void *)PG_VADDR(kloader.cur_pg);
|
2004-03-23 02:10:55 +03:00
|
|
|
kloader.cur_tag = NULL;
|
2002-01-27 08:14:33 +03:00
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2004-03-23 02:10:55 +03:00
|
|
|
|
|
|
|
struct kloader_page_tag *
|
|
|
|
kloader_get_tag(vaddr_t dst)
|
|
|
|
{
|
|
|
|
struct vm_page *pg;
|
|
|
|
vaddr_t addr;
|
|
|
|
struct kloader_page_tag *tag;
|
|
|
|
|
|
|
|
tag = kloader.cur_tag;
|
|
|
|
if (tag != NULL /* has tag */
|
|
|
|
&& tag->sz < BUCKET_SIZE /* that has free space */
|
|
|
|
&& tag->dst + tag->sz == dst) /* and new data are contiguous */
|
|
|
|
{
|
2004-03-23 06:39:11 +03:00
|
|
|
DPRINTFN(1, "current tag %x/%x ok\n", tag->dst, tag->sz);
|
|
|
|
return (tag);
|
2004-03-23 02:10:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
pg = kloader.cur_pg;
|
|
|
|
KDASSERT(pg != NULL);
|
|
|
|
kloader.cur_pg = TAILQ_NEXT(pg, pageq);
|
|
|
|
|
|
|
|
addr = PG_VADDR(pg);
|
|
|
|
tag = (void *)addr;
|
|
|
|
|
2004-06-12 02:56:00 +04:00
|
|
|
/*
|
|
|
|
* 2nd loader uses simple word-by-word copy, so destination
|
|
|
|
* address of a tag must be properly aligned.
|
|
|
|
*/
|
|
|
|
KASSERT(ALIGNED_POINTER(dst, register_t));
|
|
|
|
|
2004-03-23 02:10:55 +03:00
|
|
|
tag->src = addr + sizeof(struct kloader_page_tag);
|
|
|
|
tag->dst = dst;
|
|
|
|
tag->sz = 0;
|
2004-06-12 18:31:49 +04:00
|
|
|
tag->next = 0; /* Terminate. this member may overwrite after. */
|
2004-03-23 02:10:55 +03:00
|
|
|
if (kloader.cur_tag)
|
|
|
|
kloader.cur_tag->next = addr;
|
|
|
|
kloader.cur_tag = tag;
|
|
|
|
|
|
|
|
return (tag);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Operations to populate kloader_page_tag's with data.
|
|
|
|
*/
|
|
|
|
|
2004-03-23 06:39:11 +03:00
|
|
|
void
|
2004-03-23 02:10:55 +03:00
|
|
|
kloader_from_file(vaddr_t dst, off_t ofs, size_t sz)
|
2002-01-27 08:14:33 +03:00
|
|
|
{
|
2004-03-23 02:10:55 +03:00
|
|
|
struct kloader_page_tag *tag;
|
2004-03-23 06:39:11 +03:00
|
|
|
size_t freesz;
|
|
|
|
|
|
|
|
while (sz > 0) {
|
|
|
|
tag = kloader_get_tag(dst);
|
|
|
|
KDASSERT(tag != NULL);
|
|
|
|
freesz = BUCKET_SIZE - tag->sz;
|
|
|
|
if (freesz > sz)
|
|
|
|
freesz = sz;
|
|
|
|
|
2005-09-24 21:00:20 +04:00
|
|
|
DPRINTFN(1, "0x%08lx + 0x%zx <- 0x%lx\n", dst, freesz,
|
|
|
|
(unsigned long)ofs);
|
2004-03-23 06:39:11 +03:00
|
|
|
kloader_read(ofs, freesz, (void *)(tag->src + tag->sz));
|
|
|
|
|
|
|
|
tag->sz += freesz;
|
|
|
|
sz -= freesz;
|
|
|
|
ofs += freesz;
|
|
|
|
dst += freesz;
|
|
|
|
}
|
2004-03-23 02:10:55 +03:00
|
|
|
}
|
2002-01-27 08:14:33 +03:00
|
|
|
|
2004-03-23 06:39:11 +03:00
|
|
|
|
|
|
|
void
|
|
|
|
kloader_copy(vaddr_t dst, const void *src, size_t sz)
|
2004-03-23 02:10:55 +03:00
|
|
|
{
|
|
|
|
struct kloader_page_tag *tag;
|
2004-03-23 06:39:11 +03:00
|
|
|
size_t freesz;
|
|
|
|
|
|
|
|
while (sz > 0) {
|
|
|
|
tag = kloader_get_tag(dst);
|
|
|
|
KDASSERT(tag != NULL);
|
|
|
|
freesz = BUCKET_SIZE - tag->sz;
|
|
|
|
if (freesz > sz)
|
|
|
|
freesz = sz;
|
|
|
|
|
2005-09-24 21:00:20 +04:00
|
|
|
DPRINTFN(1, "0x%08lx + 0x%zx <- %p\n", dst, freesz, src);
|
2004-03-23 06:39:11 +03:00
|
|
|
memcpy((void *)(tag->src + tag->sz), src, freesz);
|
|
|
|
|
|
|
|
tag->sz += freesz;
|
|
|
|
sz -= freesz;
|
2005-06-01 02:22:36 +04:00
|
|
|
src = (const char *)src + freesz;
|
2004-03-23 06:39:11 +03:00
|
|
|
dst += freesz;
|
|
|
|
}
|
2002-01-27 08:14:33 +03:00
|
|
|
}
|
|
|
|
|
2004-03-23 06:39:11 +03:00
|
|
|
|
|
|
|
void
|
2004-03-23 02:10:55 +03:00
|
|
|
kloader_zero(vaddr_t dst, size_t sz)
|
2002-01-27 08:14:33 +03:00
|
|
|
{
|
2004-03-23 02:10:55 +03:00
|
|
|
struct kloader_page_tag *tag;
|
2004-03-23 06:39:11 +03:00
|
|
|
size_t freesz;
|
|
|
|
|
|
|
|
while (sz > 0) {
|
|
|
|
tag = kloader_get_tag(dst);
|
|
|
|
KDASSERT(tag != NULL);
|
|
|
|
freesz = BUCKET_SIZE - tag->sz;
|
|
|
|
if (freesz > sz)
|
|
|
|
freesz = sz;
|
2002-01-27 08:14:33 +03:00
|
|
|
|
2005-09-24 21:00:20 +04:00
|
|
|
DPRINTFN(1, "0x%08lx + 0x%zx\n", dst, freesz);
|
2004-03-23 06:39:11 +03:00
|
|
|
memset((void *)(tag->src + tag->sz), 0, freesz);
|
|
|
|
|
|
|
|
tag->sz += freesz;
|
|
|
|
sz -= freesz;
|
|
|
|
dst += freesz;
|
|
|
|
}
|
2002-01-27 08:14:33 +03:00
|
|
|
}
|
|
|
|
|
2004-03-23 02:10:55 +03:00
|
|
|
|
2002-01-27 08:14:33 +03:00
|
|
|
void
|
2004-03-23 02:10:55 +03:00
|
|
|
kloader_load_segment(Elf_Phdr *p)
|
2002-01-27 08:14:33 +03:00
|
|
|
{
|
|
|
|
|
2004-03-23 02:10:55 +03:00
|
|
|
DPRINTF("memory 0x%08x 0x%x <- file 0x%x 0x%x\n",
|
|
|
|
p->p_vaddr, p->p_memsz, p->p_offset, p->p_filesz);
|
2002-01-27 08:14:33 +03:00
|
|
|
|
2004-03-23 06:39:11 +03:00
|
|
|
kloader_from_file(p->p_vaddr, p->p_offset, p->p_filesz);
|
2004-03-23 02:10:55 +03:00
|
|
|
#ifdef KLOADER_ZERO_BSS
|
2004-03-23 06:39:11 +03:00
|
|
|
kloader_zero(p->p_vaddr + p->p_filesz, p->p_memsz - p->p_filesz);
|
2004-03-23 02:10:55 +03:00
|
|
|
#endif
|
2002-01-27 08:14:33 +03:00
|
|
|
}
|
|
|
|
|
2004-03-23 02:10:55 +03:00
|
|
|
|
2002-01-27 08:14:33 +03:00
|
|
|
/*
|
|
|
|
* file access
|
|
|
|
*/
|
|
|
|
struct vnode *
|
|
|
|
kloader_open(const char *filename)
|
|
|
|
{
|
2005-12-11 15:16:03 +03:00
|
|
|
struct lwp *l = KLOADER_LWP;
|
2002-01-27 08:14:33 +03:00
|
|
|
struct nameidata nid;
|
2006-01-02 23:51:09 +03:00
|
|
|
int error;
|
2002-01-27 08:14:33 +03:00
|
|
|
|
2005-12-11 15:16:03 +03:00
|
|
|
NDINIT(&nid, LOOKUP, FOLLOW, UIO_SYSSPACE, filename, l);
|
2002-01-27 08:14:33 +03:00
|
|
|
|
2006-01-02 23:51:09 +03:00
|
|
|
error = namei(&nid);
|
|
|
|
if (error != 0) {
|
|
|
|
PRINTF("%s: namei failed, errno=%d\n", filename, error);
|
|
|
|
return (NULL);
|
2002-01-27 08:14:33 +03:00
|
|
|
}
|
|
|
|
|
2006-01-02 23:51:09 +03:00
|
|
|
error = vn_open(&nid, FREAD, 0);
|
|
|
|
if (error != 0) {
|
|
|
|
PRINTF("%s: open failed, errno=%d\n", filename, error);
|
|
|
|
return (NULL);
|
2002-01-27 08:14:33 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return (nid.ni_vp);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
kloader_close()
|
|
|
|
{
|
2005-12-11 15:16:03 +03:00
|
|
|
struct lwp *l = KLOADER_LWP;
|
2002-01-27 08:14:33 +03:00
|
|
|
struct vnode *vp = kloader.vp;
|
|
|
|
|
|
|
|
VOP_UNLOCK(vp, 0);
|
2006-05-15 01:42:26 +04:00
|
|
|
vn_close(vp, FREAD, l->l_proc->p_cred, l);
|
2002-01-27 08:14:33 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
kloader_read(size_t ofs, size_t size, void *buf)
|
|
|
|
{
|
2005-12-11 15:16:03 +03:00
|
|
|
struct lwp *l = KLOADER_LWP;
|
2002-01-27 08:14:33 +03:00
|
|
|
struct vnode *vp = kloader.vp;
|
|
|
|
size_t resid;
|
|
|
|
int error;
|
|
|
|
|
|
|
|
error = vn_rdwr(UIO_READ, vp, buf, size, ofs, UIO_SYSSPACE,
|
2006-05-15 01:42:26 +04:00
|
|
|
IO_NODELOCKED | IO_SYNC, l->l_proc->p_cred, &resid, NULL);
|
2002-01-27 08:14:33 +03:00
|
|
|
|
|
|
|
if (error)
|
|
|
|
PRINTF("read error.\n");
|
|
|
|
|
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
2004-03-23 02:10:55 +03:00
|
|
|
|
2002-01-27 08:14:33 +03:00
|
|
|
/*
|
|
|
|
* bootinfo
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
kloader_bootinfo_set(struct kloader_bootinfo *kbi, int argc, char *argv[],
|
2002-01-29 21:44:22 +03:00
|
|
|
struct bootinfo *bi, int printok)
|
2002-01-27 08:14:33 +03:00
|
|
|
{
|
|
|
|
char *p, *pend, *buf;
|
|
|
|
int i;
|
2004-03-23 02:10:55 +03:00
|
|
|
|
2002-01-27 08:14:33 +03:00
|
|
|
kloader.bootinfo = kbi;
|
|
|
|
buf = kbi->_argbuf;
|
2004-07-06 17:09:18 +04:00
|
|
|
if (bi != NULL)
|
|
|
|
memcpy(&kbi->bootinfo, bi, sizeof(struct bootinfo));
|
2002-01-27 08:14:33 +03:00
|
|
|
kbi->argc = argc;
|
|
|
|
kbi->argv = (char **)buf;
|
|
|
|
|
|
|
|
p = &buf[argc * sizeof(char **)];
|
|
|
|
pend = &buf[KLOADER_KERNELARGS_MAX - 1];
|
|
|
|
|
|
|
|
for (i = 0; i < argc; i++) {
|
|
|
|
char *q = argv[i];
|
|
|
|
int len = strlen(q) + 1;
|
|
|
|
if ((p + len) > pend) {
|
|
|
|
kloader.bootinfo = NULL;
|
|
|
|
if (printok)
|
|
|
|
PRINTF("buffer insufficient.\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
kbi->argv[i] = p;
|
|
|
|
memcpy(p, q, len);
|
|
|
|
p += len;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-03-23 02:10:55 +03:00
|
|
|
|
2002-01-27 08:14:33 +03:00
|
|
|
#ifdef KLOADER_DEBUG
|
|
|
|
void
|
|
|
|
kloader_pagetag_dump()
|
|
|
|
{
|
|
|
|
struct kloader_page_tag *tag = kloader.tagstart;
|
|
|
|
struct kloader_page_tag *p, *op;
|
2004-06-12 18:31:49 +04:00
|
|
|
boolean_t print;
|
2002-01-27 08:14:33 +03:00
|
|
|
int i, n;
|
|
|
|
|
|
|
|
p = tag;
|
2004-06-12 18:31:49 +04:00
|
|
|
op = NULL;
|
2002-01-27 08:14:33 +03:00
|
|
|
i = 0, n = 15;
|
|
|
|
|
|
|
|
PRINTF("[page tag chain]\n");
|
|
|
|
do {
|
2004-06-12 18:31:49 +04:00
|
|
|
print = FALSE;
|
|
|
|
if (i < n)
|
|
|
|
print = TRUE;
|
2006-01-20 05:47:30 +03:00
|
|
|
if ((uint32_t)p & 3) {
|
2004-06-12 18:31:49 +04:00
|
|
|
printf("tag alignment error\n");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if ((p->src & 3) || (p->dst & 3)) {
|
|
|
|
printf("data alignement error.\n");
|
|
|
|
print = TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (print) {
|
2002-01-27 08:14:33 +03:00
|
|
|
printf("[%2d] next 0x%08x src 0x%08x dst 0x%08x"
|
|
|
|
" sz 0x%x\n", i, p->next, p->src, p->dst, p->sz);
|
|
|
|
} else if (i == n) {
|
|
|
|
printf("[...]\n");
|
|
|
|
}
|
|
|
|
op = p;
|
|
|
|
i++;
|
|
|
|
} while ((p = (struct kloader_page_tag *)(p->next)) != 0);
|
2004-06-12 18:31:49 +04:00
|
|
|
|
|
|
|
if (op != NULL)
|
|
|
|
printf("[%d(last)] next 0x%08x src 0x%08x dst 0x%08x sz 0x%x\n",
|
|
|
|
i - 1, op->next, op->src, op->dst, op->sz);
|
2002-01-27 08:14:33 +03:00
|
|
|
}
|
2004-06-12 18:31:49 +04:00
|
|
|
|
2002-01-27 08:14:33 +03:00
|
|
|
#endif /* KLOADER_DEBUG */
|