Home | History | Annotate | Line # | Download | only in vax1k
vax1k_exec.c revision 1.2.4.2
      1  1.2.4.2  thorpej /*	$NetBSD: vax1k_exec.c,v 1.2.4.2 2002/01/10 19:52:30 thorpej Exp $	*/
      2      1.1    ragge 
      3      1.1    ragge /*
      4      1.1    ragge  * Copyright (c) 1993, 1994 Christopher G. Demetriou
      5      1.1    ragge  * All rights reserved.
      6      1.1    ragge  *
      7      1.1    ragge  * Redistribution and use in source and binary forms, with or without
      8      1.1    ragge  * modification, are permitted provided that the following conditions
      9      1.1    ragge  * are met:
     10      1.1    ragge  * 1. Redistributions of source code must retain the above copyright
     11      1.1    ragge  *    notice, this list of conditions and the following disclaimer.
     12      1.1    ragge  * 2. Redistributions in binary form must reproduce the above copyright
     13      1.1    ragge  *    notice, this list of conditions and the following disclaimer in the
     14      1.1    ragge  *    documentation and/or other materials provided with the distribution.
     15      1.1    ragge  * 3. All advertising materials mentioning features or use of this software
     16      1.1    ragge  *    must display the following acknowledgement:
     17      1.1    ragge  *      This product includes software developed by Christopher G. Demetriou.
     18      1.1    ragge  * 4. The name of the author may not be used to endorse or promote products
     19      1.1    ragge  *    derived from this software without specific prior written permission
     20      1.1    ragge  *
     21      1.1    ragge  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     22      1.1    ragge  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     23      1.1    ragge  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     24      1.1    ragge  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     25      1.1    ragge  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     26      1.1    ragge  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     27      1.1    ragge  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     28      1.1    ragge  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     29      1.1    ragge  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     30      1.1    ragge  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     31      1.1    ragge  */
     32      1.1    ragge 
     33      1.1    ragge /*
     34      1.1    ragge  * Exec glue to provide compatibility with older NetBSD vax1k exectuables.
     35      1.1    ragge  *
     36      1.1    ragge  * Because NetBSD/vax now uses 4k page size, older binaries (that started
     37      1.1    ragge  * on an 1k boundary) cannot be mmap'ed. Therefore they are read in
     38      1.1    ragge  * (via vn_rdwr) as OMAGIC binaries and executed. This will use a little
     39      1.1    ragge  * bit more memory, but otherwise won't affect the execution speed.
     40      1.1    ragge  */
     41  1.2.4.2  thorpej 
     42  1.2.4.2  thorpej #include <sys/cdefs.h>
     43  1.2.4.2  thorpej __KERNEL_RCSID(0, "$NetBSD: vax1k_exec.c,v 1.2.4.2 2002/01/10 19:52:30 thorpej Exp $");
     44      1.1    ragge 
     45      1.1    ragge #include <sys/param.h>
     46      1.1    ragge #include <sys/systm.h>
     47      1.1    ragge #include <sys/proc.h>
     48      1.1    ragge #include <sys/malloc.h>
     49      1.1    ragge #include <sys/vnode.h>
     50      1.1    ragge #include <sys/exec.h>
     51      1.1    ragge #include <sys/resourcevar.h>
     52      1.1    ragge 
     53      1.1    ragge #include <compat/vax1k/vax1k_exec.h>
     54      1.1    ragge 
     55  1.2.4.1  thorpej #if defined(_KERNEL_OPT)
     56  1.2.4.1  thorpej #include "opt_compat_43.h"
     57  1.2.4.1  thorpej #else
     58  1.2.4.1  thorpej #define COMPAT_43	/* enable 4.3BSD binaries for lkm */
     59  1.2.4.1  thorpej #endif
     60  1.2.4.1  thorpej 
     61  1.2.4.1  thorpej int	exec_vax1k_prep_anymagic __P((struct proc *p, struct exec_package *epp,
     62  1.2.4.1  thorpej 				      int, int));
     63      1.1    ragge 
     64      1.1    ragge /*
     65      1.1    ragge  * exec_vax1k_makecmds(): Check if it's an a.out-format executable
     66      1.1    ragge  * with an vax1k magic number.
     67      1.1    ragge  *
     68      1.1    ragge  * Given a proc pointer and an exec package pointer, see if the referent
     69      1.1    ragge  * of the epp is in a.out format.  Just check 'standard' magic numbers for
     70      1.1    ragge  * this architecture.
     71      1.1    ragge  *
     72      1.1    ragge  * This function, in the former case, or the hook, in the latter, is
     73      1.1    ragge  * responsible for creating a set of vmcmds which can be used to build
     74      1.1    ragge  * the process's vm space and inserting them into the exec package.
     75      1.1    ragge  */
     76      1.1    ragge 
     77      1.1    ragge int
     78      1.1    ragge exec_vax1k_makecmds(p, epp)
     79      1.1    ragge 	struct proc *p;
     80      1.1    ragge 	struct exec_package *epp;
     81      1.1    ragge {
     82      1.1    ragge 	u_long midmag, magic;
     83      1.1    ragge 	u_short mid;
     84      1.1    ragge 	int error;
     85      1.1    ragge 	struct exec *execp = epp->ep_hdr;
     86      1.1    ragge 
     87      1.1    ragge 	if (epp->ep_hdrvalid < sizeof(struct exec))
     88      1.1    ragge 		return ENOEXEC;
     89      1.1    ragge 
     90      1.1    ragge 	midmag = ntohl(execp->a_midmag);
     91      1.1    ragge 	mid = (midmag >> 16) & 0x3ff;
     92      1.1    ragge 	magic = midmag & 0xffff;
     93      1.1    ragge 
     94      1.1    ragge 	midmag = mid << 16 | magic;
     95      1.1    ragge 
     96      1.1    ragge 	switch (midmag) {
     97      1.1    ragge 	case (MID_VAX1K << 16) | ZMAGIC:
     98  1.2.4.1  thorpej 		error = exec_vax1k_prep_anymagic(p, epp, 0, 0);
     99  1.2.4.1  thorpej 		goto done;
    100      1.1    ragge 
    101      1.1    ragge 	case (MID_VAX1K << 16) | NMAGIC:
    102  1.2.4.1  thorpej 		error = exec_vax1k_prep_anymagic(p, epp,
    103  1.2.4.1  thorpej 						 sizeof(struct exec), 1);
    104  1.2.4.1  thorpej 		goto done;
    105  1.2.4.1  thorpej 
    106      1.1    ragge 	case (MID_VAX1K << 16) | OMAGIC:
    107  1.2.4.1  thorpej 		error = exec_vax1k_prep_anymagic(p, epp,
    108  1.2.4.1  thorpej 						 sizeof(struct exec), 0);
    109  1.2.4.1  thorpej 		goto done;
    110  1.2.4.1  thorpej 	}
    111      1.1    ragge 
    112  1.2.4.1  thorpej #ifdef COMPAT_43
    113  1.2.4.1  thorpej 	/*
    114  1.2.4.1  thorpej 	 * 4.3BSD pre-dates cpu midmag (e.g. MID_VAX1K).   instead, we
    115  1.2.4.1  thorpej 	 * expect a magic number in native byte order.
    116  1.2.4.1  thorpej 	 */
    117  1.2.4.1  thorpej 	switch (execp->a_midmag) {
    118  1.2.4.1  thorpej 	case ZMAGIC:
    119  1.2.4.1  thorpej 		error = exec_vax1k_prep_anymagic(p, epp, VAX1K_LDPGSZ, 0);
    120  1.2.4.1  thorpej 		goto done;
    121  1.2.4.1  thorpej 
    122  1.2.4.1  thorpej 	case NMAGIC:
    123  1.2.4.1  thorpej 		error = exec_vax1k_prep_anymagic(p, epp,
    124  1.2.4.1  thorpej 					 	sizeof(struct exec), 1);
    125  1.2.4.1  thorpej 		goto done;
    126  1.2.4.1  thorpej 
    127  1.2.4.1  thorpej 	case OMAGIC:
    128  1.2.4.1  thorpej 		error = exec_vax1k_prep_anymagic(p, epp,
    129  1.2.4.1  thorpej 					 	sizeof(struct exec), 0);
    130  1.2.4.1  thorpej 		goto done;
    131      1.1    ragge 	}
    132  1.2.4.1  thorpej #endif
    133  1.2.4.1  thorpej 
    134  1.2.4.1  thorpej 	/* failed... */
    135  1.2.4.1  thorpej 	error = ENOEXEC;
    136      1.1    ragge 
    137  1.2.4.1  thorpej done:
    138      1.1    ragge 	if (error)
    139      1.1    ragge 		kill_vmcmds(&epp->ep_vmcmds);
    140      1.1    ragge 
    141      1.1    ragge 	return error;
    142      1.1    ragge }
    143      1.1    ragge 
    144      1.1    ragge /*
    145      1.1    ragge  * exec_vax1k_prep_anymagic(): Prepare an vax1k ?MAGIC binary's exec package
    146      1.1    ragge  *
    147      1.1    ragge  * First, set of the various offsets/lengths in the exec package.
    148      1.1    ragge  * Note that all code is mapped RW; no protection, but because it is
    149      1.1    ragge  * only used for compatibility it won't hurt.
    150      1.1    ragge  *
    151      1.1    ragge  */
    152      1.1    ragge int
    153  1.2.4.1  thorpej exec_vax1k_prep_anymagic(p, epp, text_foffset, textpad)
    154  1.2.4.1  thorpej 	struct proc *p;
    155  1.2.4.1  thorpej 	struct exec_package *epp;
    156  1.2.4.1  thorpej 	int text_foffset, textpad;
    157      1.1    ragge {
    158      1.1    ragge         struct exec *execp = epp->ep_hdr;
    159      1.1    ragge 
    160  1.2.4.1  thorpej 	epp->ep_taddr = execp->a_entry & ~(VAX1K_USRTEXT - 1);
    161  1.2.4.1  thorpej 	epp->ep_tsize = execp->a_text;
    162  1.2.4.1  thorpej 	epp->ep_daddr = epp->ep_taddr + epp->ep_tsize;
    163  1.2.4.1  thorpej 	if (textpad)			/* pad for NMAGIC? */
    164  1.2.4.1  thorpej 		epp->ep_daddr = (epp->ep_daddr + (VAX1K_LDPGSZ - 1)) &
    165  1.2.4.1  thorpej 						~(VAX1K_LDPGSZ - 1);
    166  1.2.4.1  thorpej 	epp->ep_dsize = execp->a_data;
    167  1.2.4.1  thorpej 	epp->ep_entry = execp->a_entry;
    168  1.2.4.1  thorpej 
    169  1.2.4.1  thorpej 	/* first allocate memory for text+data+bss */
    170  1.2.4.1  thorpej         NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero,
    171  1.2.4.1  thorpej 		round_page(epp->ep_daddr + epp->ep_dsize + execp->a_bss) -
    172  1.2.4.1  thorpej 			trunc_page(epp->ep_taddr),	/* size */
    173  1.2.4.1  thorpej 		trunc_page(epp->ep_taddr), NULLVP,	/* addr, vnode */
    174  1.2.4.1  thorpej 		0, VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
    175  1.2.4.1  thorpej 
    176  1.2.4.1  thorpej 	/* then read the text in the area we just allocated */
    177  1.2.4.1  thorpej        	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_readvn,
    178  1.2.4.1  thorpej 		epp->ep_tsize, epp->ep_taddr, epp->ep_vp, text_foffset,
    179  1.2.4.1  thorpej     	VM_PROT_WRITE|VM_PROT_READ|VM_PROT_EXECUTE);
    180  1.2.4.1  thorpej 
    181  1.2.4.1  thorpej 	/* next read the data */
    182  1.2.4.1  thorpej 	if (epp->ep_dsize) {
    183  1.2.4.1  thorpej         	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_readvn,
    184  1.2.4.1  thorpej 			epp->ep_dsize, epp->ep_daddr, epp->ep_vp,
    185  1.2.4.1  thorpej 			text_foffset + epp->ep_tsize,
    186  1.2.4.1  thorpej 			VM_PROT_WRITE|VM_PROT_READ|VM_PROT_EXECUTE);
    187  1.2.4.1  thorpej 	}
    188  1.2.4.1  thorpej 
    189  1.2.4.1  thorpej 	/* now bump up the dsize to include the bss so that sbrk works */
    190  1.2.4.1  thorpej 	epp->ep_dsize += execp->a_bss;
    191      1.1    ragge 
    192  1.2.4.1  thorpej 	/* finally, setup the stack ... */
    193      1.1    ragge         return exec_aout_setup_stack(p, epp);
    194      1.1    ragge }
    195