Home | History | Annotate | Line # | Download | only in lib
exec.c revision 1.23
      1  1.23        ad /*	$NetBSD: exec.c,v 1.23 2008/05/02 15:26:38 ad Exp $	 */
      2  1.23        ad 
      3  1.23        ad /*-
      4  1.23        ad  * Copyright (c) 2008 The NetBSD Foundation, Inc.
      5  1.23        ad  * All rights reserved.
      6  1.23        ad  *
      7  1.23        ad  * Redistribution and use in source and binary forms, with or without
      8  1.23        ad  * modification, are permitted provided that the following conditions
      9  1.23        ad  * are met:
     10  1.23        ad  * 1. Redistributions of source code must retain the above copyright
     11  1.23        ad  *    notice, this list of conditions and the following disclaimer.
     12  1.23        ad  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.23        ad  *    notice, this list of conditions and the following disclaimer in the
     14  1.23        ad  *    documentation and/or other materials provided with the distribution.
     15  1.23        ad  *
     16  1.23        ad  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     17  1.23        ad  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     18  1.23        ad  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19  1.23        ad  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     20  1.23        ad  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21  1.23        ad  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22  1.23        ad  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     23  1.23        ad  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     24  1.23        ad  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     25  1.23        ad  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26  1.23        ad  * POSSIBILITY OF SUCH DAMAGE.
     27  1.23        ad  */
     28   1.1     perry 
     29   1.1     perry /*
     30   1.1     perry  * Copyright (c) 1982, 1986, 1990, 1993
     31   1.1     perry  *	The Regents of the University of California.  All rights reserved.
     32  1.19       agc  *
     33  1.19       agc  * Redistribution and use in source and binary forms, with or without
     34  1.19       agc  * modification, are permitted provided that the following conditions
     35  1.19       agc  * are met:
     36  1.19       agc  * 1. Redistributions of source code must retain the above copyright
     37  1.19       agc  *    notice, this list of conditions and the following disclaimer.
     38  1.19       agc  * 2. Redistributions in binary form must reproduce the above copyright
     39  1.19       agc  *    notice, this list of conditions and the following disclaimer in the
     40  1.19       agc  *    documentation and/or other materials provided with the distribution.
     41  1.19       agc  * 3. Neither the name of the University nor the names of its contributors
     42  1.19       agc  *    may be used to endorse or promote products derived from this software
     43  1.19       agc  *    without specific prior written permission.
     44  1.19       agc  *
     45  1.19       agc  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     46  1.19       agc  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     47  1.19       agc  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     48  1.19       agc  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     49  1.19       agc  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     50  1.19       agc  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     51  1.19       agc  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     52  1.19       agc  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     53  1.19       agc  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     54  1.19       agc  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     55  1.19       agc  * SUCH DAMAGE.
     56  1.19       agc  *
     57  1.19       agc  * 	@(#)boot.c	8.1 (Berkeley) 6/10/93
     58  1.19       agc  */
     59  1.19       agc 
     60  1.19       agc /*
     61   1.1     perry  * Copyright (c) 1996
     62   1.1     perry  *	Matthias Drochner.  All rights reserved.
     63   1.1     perry  * Copyright (c) 1996
     64   1.1     perry  * 	Perry E. Metzger.  All rights reserved.
     65   1.8  christos  * Copyright (c) 1997
     66   1.8  christos  * 	Martin Husemann.  All rights reserved.
     67   1.1     perry  *
     68   1.1     perry  * Redistribution and use in source and binary forms, with or without
     69   1.1     perry  * modification, are permitted provided that the following conditions
     70   1.1     perry  * are met:
     71   1.1     perry  * 1. Redistributions of source code must retain the above copyright
     72   1.1     perry  *    notice, this list of conditions and the following disclaimer.
     73   1.1     perry  * 2. Redistributions in binary form must reproduce the above copyright
     74   1.1     perry  *    notice, this list of conditions and the following disclaimer in the
     75   1.1     perry  *    documentation and/or other materials provided with the distribution.
     76   1.1     perry  * 3. All advertising materials mentioning features or use of this software
     77   1.1     perry  *    must display the following acknowledgement:
     78   1.1     perry  *	This product includes software developed by the University of
     79   1.1     perry  *	California, Berkeley and its contributors.
     80   1.1     perry  * 4. Neither the name of the University nor the names of its contributors
     81   1.1     perry  *    may be used to endorse or promote products derived from this software
     82   1.1     perry  *    without specific prior written permission.
     83   1.1     perry  *
     84   1.1     perry  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     85   1.1     perry  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     86   1.1     perry  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     87   1.1     perry  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     88   1.1     perry  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     89   1.1     perry  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     90   1.1     perry  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     91   1.1     perry  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     92   1.1     perry  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     93   1.1     perry  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     94   1.1     perry  * SUCH DAMAGE.
     95   1.1     perry  *
     96   1.1     perry  * 	@(#)boot.c	8.1 (Berkeley) 6/10/93
     97   1.1     perry  */
     98   1.1     perry 
     99   1.2   thorpej /*
    100   1.8  christos  * starts NetBSD a.out kernel
    101   1.8  christos  * needs lowlevel startup from startprog.S
    102   1.8  christos  * This is a special version of exec.c to support use of XMS.
    103   1.1     perry  */
    104   1.8  christos 
    105   1.1     perry #include <sys/param.h>
    106   1.1     perry #include <sys/reboot.h>
    107   1.1     perry 
    108   1.1     perry #include <lib/libsa/stand.h>
    109  1.23        ad #include <lib/libkern/libkern.h>
    110   1.4  drochner 
    111   1.6  christos #include "loadfile.h"
    112   1.1     perry #include "libi386.h"
    113   1.4  drochner #include "bootinfo.h"
    114  1.23        ad #include "bootmod.h"
    115  1.16  jdolecek #ifdef SUPPORT_PS2
    116  1.16  jdolecek #include "biosmca.h"
    117  1.16  jdolecek #endif
    118   1.1     perry 
    119   1.6  christos #define BOOT_NARGS	6
    120   1.1     perry 
    121  1.23        ad #ifndef	PAGE_SIZE
    122  1.23        ad #define	PAGE_SIZE	4096
    123  1.23        ad #endif
    124  1.23        ad 
    125   1.4  drochner extern struct btinfo_console btinfo_console;
    126   1.4  drochner 
    127  1.23        ad boot_module_t *boot_modules;
    128  1.23        ad bool boot_modules_enabled = true;
    129  1.23        ad bool kernel_loaded;
    130  1.23        ad 
    131  1.23        ad static struct btinfo_modulelist *btinfo_modulelist;
    132  1.23        ad static size_t btinfo_modulelist_size;
    133  1.23        ad static uint32_t image_end;
    134  1.23        ad 
    135  1.23        ad static uint8_t *module_alloc(size_t);
    136  1.23        ad static void	module_init(void);
    137  1.23        ad 
    138  1.21  junyoung int
    139  1.21  junyoung exec_netbsd(const char *file, physaddr_t loadaddr, int boothowto)
    140   1.1     perry {
    141   1.6  christos 	u_long          boot_argv[BOOT_NARGS];
    142   1.6  christos 	int		fd;
    143   1.8  christos 	u_long		marks[MARK_MAX];
    144   1.8  christos 	struct btinfo_symtab btinfo_symtab;
    145   1.8  christos 	u_long		extmem;
    146  1.15       dbj 	u_long		basemem;
    147   1.8  christos #ifdef XMS
    148   1.8  christos 	u_long		xmsmem;
    149   1.8  christos 	physaddr_t	origaddr = loadaddr;
    150   1.8  christos #endif
    151   1.8  christos 
    152   1.1     perry #ifdef	DEBUG
    153  1.13  drochner 	printf("exec: file=%s loadaddr=0x%lx\n",
    154  1.13  drochner 	       file ? file : "NULL", loadaddr);
    155   1.1     perry #endif
    156   1.4  drochner 
    157  1.23        ad 	BI_ALLOC(32); /* ??? */
    158   1.4  drochner 
    159   1.4  drochner 	BI_ADD(&btinfo_console, BTINFO_CONSOLE, sizeof(struct btinfo_console));
    160   1.4  drochner 
    161   1.8  christos 	extmem = getextmem();
    162  1.15       dbj 	basemem = getbasemem();
    163   1.8  christos 
    164   1.8  christos #ifdef XMS
    165   1.8  christos 	if ((getextmem1() == 0) && (xmsmem = checkxms())) {
    166   1.8  christos 	        u_long kernsize;
    167   1.8  christos 
    168   1.8  christos 		/*
    169   1.8  christos 		 * With "CONSERVATIVE_MEMDETECT", extmem is 0 because
    170   1.8  christos 		 *  getextmem() is getextmem1(). Without, the "smart"
    171   1.8  christos 		 *  methods could fail to report all memory as well.
    172   1.8  christos 		 * xmsmem is a few kB less than the actual size, but
    173   1.8  christos 		 *  better than nothing.
    174   1.8  christos 		 */
    175   1.8  christos 		if (xmsmem > extmem)
    176   1.8  christos 			extmem = xmsmem;
    177  1.21  junyoung 		/*
    178   1.8  christos 		 * Get the size of the kernel
    179   1.8  christos 		 */
    180   1.8  christos 		marks[MARK_START] = loadaddr;
    181  1.12  christos 		if ((fd = loadfile(file, marks, COUNT_KERNEL)) == -1)
    182   1.8  christos 			goto out;
    183   1.8  christos 		close(fd);
    184   1.8  christos 
    185   1.8  christos 		kernsize = marks[MARK_END];
    186   1.8  christos 		kernsize = (kernsize + 1023) / 1024;
    187   1.8  christos 
    188   1.8  christos 		loadaddr = xmsalloc(kernsize);
    189   1.8  christos 		if (!loadaddr)
    190  1.21  junyoung 			return ENOMEM;
    191   1.8  christos 	}
    192   1.8  christos #endif
    193   1.7  christos 	marks[MARK_START] = loadaddr;
    194  1.12  christos 	if ((fd = loadfile(file, marks, LOAD_KERNEL)) == -1)
    195   1.4  drochner 		goto out;
    196   1.1     perry 
    197   1.1     perry 	boot_argv[0] = boothowto;
    198   1.5  drochner 	boot_argv[1] = 0;
    199   1.8  christos 	boot_argv[2] = vtophys(bootinfo);	/* old cyl offset */
    200   1.8  christos 	/* argv[3] below */
    201   1.8  christos 	boot_argv[4] = extmem;
    202  1.15       dbj 	boot_argv[5] = basemem;
    203   1.8  christos 
    204   1.8  christos 	close(fd);
    205  1.10  drochner 
    206  1.10  drochner 	/*
    207  1.10  drochner 	 * Gather some information for the kernel. Do this after the
    208  1.10  drochner 	 * "point of no return" to avoid memory leaks.
    209  1.10  drochner 	 * (but before DOS might be trashed in the XMS case)
    210  1.10  drochner 	 */
    211  1.10  drochner #ifdef PASS_BIOSGEOM
    212  1.10  drochner 	bi_getbiosgeom();
    213  1.10  drochner #endif
    214  1.10  drochner #ifdef PASS_MEMMAP
    215  1.10  drochner 	bi_getmemmap();
    216  1.10  drochner #endif
    217   1.8  christos 
    218   1.8  christos #ifdef XMS
    219   1.8  christos 	if (loadaddr != origaddr) {
    220   1.8  christos 		/*
    221  1.14      ross 		 * We now have done our last DOS IO, so we may
    222   1.8  christos 		 * trash the OS. Copy the data from the temporary
    223  1.20       wiz 		 * buffer to its real address.
    224   1.8  christos 		 */
    225   1.8  christos 		marks[MARK_START] -= loadaddr;
    226   1.8  christos 		marks[MARK_END] -= loadaddr;
    227   1.8  christos 		marks[MARK_SYM] -= loadaddr;
    228   1.8  christos 		marks[MARK_END] -= loadaddr;
    229   1.8  christos 		ppbcopy(loadaddr, origaddr, marks[MARK_END]);
    230   1.8  christos 	}
    231   1.8  christos #endif
    232   1.8  christos 	marks[MARK_END] = (((u_long) marks[MARK_END] + sizeof(int) - 1)) &
    233   1.8  christos 	    (-sizeof(int));
    234   1.7  christos 	boot_argv[3] = marks[MARK_END];
    235  1.23        ad 	image_end = marks[MARK_END];
    236  1.23        ad 	kernel_loaded = true;
    237   1.1     perry 
    238  1.23        ad 	/* pull in any modules if necessary */
    239  1.23        ad 	if (boot_modules_enabled) {
    240  1.23        ad 		module_init();
    241  1.23        ad 		if (btinfo_modulelist) {
    242  1.23        ad 			BI_ADD(btinfo_modulelist, BTINFO_MODULELIST,
    243  1.23        ad 			    btinfo_modulelist_size);
    244  1.23        ad 		}
    245  1.23        ad 	}
    246   1.1     perry 
    247   1.1     perry #ifdef DEBUG
    248   1.8  christos 	printf("Start @ 0x%lx [%ld=0x%lx-0x%lx]...\n", marks[MARK_ENTRY],
    249   1.7  christos 	    marks[MARK_NSYM], marks[MARK_SYM], marks[MARK_END]);
    250   1.1     perry #endif
    251   1.1     perry 
    252   1.8  christos 	btinfo_symtab.nsym = marks[MARK_NSYM];
    253   1.8  christos 	btinfo_symtab.ssym = marks[MARK_SYM];
    254   1.8  christos 	btinfo_symtab.esym = marks[MARK_END];
    255   1.8  christos 	BI_ADD(&btinfo_symtab, BTINFO_SYMTAB, sizeof(struct btinfo_symtab));
    256   1.8  christos 
    257  1.15       dbj 	startprog(marks[MARK_ENTRY], BOOT_NARGS, boot_argv,
    258  1.21  junyoung 		  x86_trunc_page(basemem*1024));
    259   1.1     perry 	panic("exec returned");
    260   1.1     perry 
    261   1.4  drochner out:
    262   1.4  drochner 	BI_FREE();
    263   1.4  drochner 	bootinfo = 0;
    264  1.21  junyoung 	return -1;
    265   1.1     perry }
    266  1.23        ad 
    267  1.23        ad static uint8_t *
    268  1.23        ad module_alloc(size_t len)
    269  1.23        ad {
    270  1.23        ad 	uint32_t addr;
    271  1.23        ad 
    272  1.23        ad 	addr = (image_end + PAGE_SIZE - 1) & ~(PAGE_SIZE - 1);
    273  1.23        ad 	image_end += (len + PAGE_SIZE - 1) & ~(PAGE_SIZE - 1);
    274  1.23        ad 
    275  1.23        ad #ifdef DEBUG
    276  1.23        ad 	printf("Allocated %d bytes of module memory at %x\n", len, addr);
    277  1.23        ad #endif
    278  1.23        ad 
    279  1.23        ad 	return (uint8_t *)addr;
    280  1.23        ad }
    281  1.23        ad 
    282  1.23        ad static void
    283  1.23        ad module_init(void)
    284  1.23        ad {
    285  1.23        ad 	struct bi_modulelist_entry *bi;
    286  1.23        ad 	struct stat st;
    287  1.23        ad 	char *buf;
    288  1.23        ad 	boot_module_t *bm;
    289  1.23        ad 	size_t len;
    290  1.23        ad 	off_t off;
    291  1.23        ad 	int err, fd;
    292  1.23        ad 
    293  1.23        ad 	/* First, see which modules are valid and calculate btinfo size */
    294  1.23        ad 	len = sizeof(struct btinfo_modulelist);
    295  1.23        ad 	for (bm = boot_modules; bm; bm = bm->bm_next) {
    296  1.23        ad 		fd = open(bm->bm_path, 0);
    297  1.23        ad 		if (fd == -1) {
    298  1.23        ad 			printf("WARNING: couldn't open %s\n", bm->bm_path);
    299  1.23        ad 			bm->bm_len = -1;
    300  1.23        ad 			continue;
    301  1.23        ad 		}
    302  1.23        ad 		err = fstat(fd, &st);
    303  1.23        ad 		if (err == -1) {
    304  1.23        ad 			printf("WARNING: couldn't stat %s\n", bm->bm_path);
    305  1.23        ad 			close(fd);
    306  1.23        ad 			bm->bm_len = -1;
    307  1.23        ad 			continue;
    308  1.23        ad 		}
    309  1.23        ad 		bm->bm_len = st.st_size;
    310  1.23        ad 		close(fd);
    311  1.23        ad 		len += sizeof(struct bi_modulelist_entry);
    312  1.23        ad 	}
    313  1.23        ad 
    314  1.23        ad 	/* Allocate the module list */
    315  1.23        ad 	btinfo_modulelist = alloc(len);
    316  1.23        ad 	if (btinfo_modulelist == NULL) {
    317  1.23        ad 		printf("WARNING: couldn't allocate module list\n");
    318  1.23        ad 		return;
    319  1.23        ad 	}
    320  1.23        ad 	memset(btinfo_modulelist, 0, len);
    321  1.23        ad 	btinfo_modulelist_size = len;
    322  1.23        ad 
    323  1.23        ad 	/* Fill in btinfo structure */
    324  1.23        ad 	buf = (char *)btinfo_modulelist;
    325  1.23        ad 	btinfo_modulelist->num = 0;
    326  1.23        ad 	off = sizeof(struct btinfo_modulelist);
    327  1.23        ad 
    328  1.23        ad 	for (bm = boot_modules; bm; bm = bm->bm_next) {
    329  1.23        ad 		if (bm->bm_len == -1)
    330  1.23        ad 			continue;
    331  1.23        ad 		printf("Loading %s ", bm->bm_path);
    332  1.23        ad 		bm->bm_data = module_alloc(bm->bm_len);
    333  1.23        ad 		if (bm->bm_data == NULL) {
    334  1.23        ad 			printf("NO MEMORY\n");
    335  1.23        ad 			continue;
    336  1.23        ad 		}
    337  1.23        ad 		fd = open(bm->bm_path, 0);
    338  1.23        ad 		if (fd == -1) {
    339  1.23        ad 			printf("ERROR: couldn't open %s\n", bm->bm_path);
    340  1.23        ad 			continue;
    341  1.23        ad 		}
    342  1.23        ad 		len = pread(fd, bm->bm_data, bm->bm_len);
    343  1.23        ad 		if (len != bm->bm_len) {
    344  1.23        ad 			printf(" FAILED\n");
    345  1.23        ad 		} else {
    346  1.23        ad 			btinfo_modulelist->num++;
    347  1.23        ad 			bi = (struct bi_modulelist_entry *)(buf + off);
    348  1.23        ad 			off += sizeof(struct bi_modulelist_entry);
    349  1.23        ad 			strncpy(bi->path, bm->bm_path, sizeof(bi->path) - 1);
    350  1.23        ad 			bi->base = (uint32_t)bm->bm_data;
    351  1.23        ad 			bi->len = bm->bm_len;
    352  1.23        ad 			bi->type = BI_MODULE_ELF;
    353  1.23        ad 			printf(" \n");
    354  1.23        ad 		}
    355  1.23        ad 		close(fd);
    356  1.23        ad 	}
    357  1.23        ad 	btinfo_modulelist->endpa = image_end;
    358  1.23        ad }
    359