Home | History | Annotate | Line # | Download | only in kern
exec_ecoff.c revision 1.6
      1 /*	$NetBSD: exec_ecoff.c,v 1.6 1996/03/07 14:31:16 christos Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1994 Adam Glass
      5  * Copyright (c) 1993, 1994 Christopher G. Demetriou
      6  * All rights reserved.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in the
     15  *    documentation and/or other materials provided with the distribution.
     16  * 3. All advertising materials mentioning features or use of this software
     17  *    must display the following acknowledgement:
     18  *      This product includes software developed by Christopher G. Demetriou.
     19  * 4. The name of the author may not be used to endorse or promote products
     20  *    derived from this software without specific prior written permission
     21  *
     22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     23  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     24  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     25  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     27  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     28  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     29  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     30  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     31  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     32  */
     33 
     34 #include <sys/param.h>
     35 #include <sys/systm.h>
     36 #include <sys/proc.h>
     37 #include <sys/malloc.h>
     38 #include <sys/vnode.h>
     39 #include <sys/exec.h>
     40 #include <sys/resourcevar.h>
     41 #include <vm/vm.h>
     42 
     43 #include <sys/exec_ecoff.h>
     44 
     45 int	exec_ecoff_prep_omagic __P((struct proc *, struct exec_package *,
     46 				    struct ecoff_filehdr *,
     47 				    struct ecoff_aouthdr *));
     48 int	exec_ecoff_prep_nmagic __P((struct proc *, struct exec_package *,
     49 				    struct ecoff_filehdr *,
     50 				    struct ecoff_aouthdr *));
     51 int	exec_ecoff_prep_zmagic __P((struct proc *, struct exec_package *,
     52 				    struct ecoff_filehdr *,
     53 				    struct ecoff_aouthdr *));
     54 int	exec_ecoff_setup_stack __P((struct proc *, struct exec_package *));
     55 
     56 /*
     57  * exec_ecoff_makecmds(): Check if it's an ecoff-format executable.
     58  *
     59  * Given a proc pointer and an exec package pointer, see if the referent
     60  * of the epp is in ecoff format.  Check 'standard' magic numbers for
     61  * this architecture.  If that fails, return failure.
     62  *
     63  * This function is  responsible for creating a set of vmcmds which can be
     64  * used to build the process's vm space and inserting them into the exec
     65  * package.
     66  */
     67 
     68 int
     69 exec_ecoff_makecmds(p, epp)
     70 	struct proc *p;
     71 	struct exec_package *epp;
     72 {
     73 	int error;
     74 	struct ecoff_filehdr *efp = epp->ep_hdr;
     75 	struct ecoff_aouthdr *eap;
     76 
     77 	if (epp->ep_hdrvalid < ECOFF_HDR_SIZE)
     78 		return ENOEXEC;
     79 
     80 	if (ECOFF_BADMAG(efp))
     81 		return ENOEXEC;
     82 
     83 	eap = (struct ecoff_aouthdr *)
     84 	    ((caddr_t)epp->ep_hdr + sizeof(struct ecoff_filehdr));
     85 	switch (eap->ea_magic) {
     86 	case ECOFF_OMAGIC:
     87 		error = exec_ecoff_prep_omagic(p, epp, efp, eap);
     88 		break;
     89 	case ECOFF_NMAGIC:
     90 		error = exec_ecoff_prep_nmagic(p, epp, efp, eap);
     91 		break;
     92 	case ECOFF_ZMAGIC:
     93 		error = exec_ecoff_prep_zmagic(p, epp, efp, eap);
     94 		break;
     95 	default:
     96 		return ENOEXEC;
     97 	}
     98 
     99 	if (error == 0)
    100 		error = cpu_exec_ecoff_hook(p, epp, eap);
    101 
    102 	if (error)
    103 		kill_vmcmds(&epp->ep_vmcmds);
    104 
    105 	return error;
    106 }
    107 
    108 /*
    109  * exec_ecoff_setup_stack(): Set up the stack segment for an ecoff
    110  * executable.
    111  *
    112  * Note that the ep_ssize parameter must be set to be the current stack
    113  * limit; this is adjusted in the body of execve() to yield the
    114  * appropriate stack segment usage once the argument length is
    115  * calculated.
    116  *
    117  * This function returns an int for uniformity with other (future) formats'
    118  * stack setup functions.  They might have errors to return.
    119  */
    120 
    121 int
    122 exec_ecoff_setup_stack(p, epp)
    123 	struct proc *p;
    124 	struct exec_package *epp;
    125 {
    126 
    127 	epp->ep_maxsaddr = USRSTACK - MAXSSIZ;
    128 	epp->ep_minsaddr = USRSTACK;
    129 	epp->ep_ssize = p->p_rlimit[RLIMIT_STACK].rlim_cur;
    130 
    131 	/*
    132 	 * set up commands for stack.  note that this takes *two*, one to
    133 	 * map the part of the stack which we can access, and one to map
    134 	 * the part which we can't.
    135 	 *
    136 	 * arguably, it could be made into one, but that would require the
    137 	 * addition of another mapping proc, which is unnecessary
    138 	 *
    139 	 * note that in memory, things assumed to be: 0 ....... ep_maxsaddr
    140 	 * <stack> ep_minsaddr
    141 	 */
    142 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero,
    143 		  ((epp->ep_minsaddr - epp->ep_ssize) - epp->ep_maxsaddr),
    144 		  epp->ep_maxsaddr, NULLVP, 0, VM_PROT_NONE);
    145 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, epp->ep_ssize,
    146 		  (epp->ep_minsaddr - epp->ep_ssize), NULLVP, 0,
    147 		  VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
    148 
    149 	return 0;
    150 }
    151 
    152 
    153 /*
    154  * exec_ecoff_prep_omagic(): Prepare a ECOFF OMAGIC binary's exec package
    155  */
    156 
    157 int
    158 exec_ecoff_prep_omagic(p, epp, efp, eap)
    159 	struct proc *p;
    160 	struct exec_package *epp;
    161 	struct ecoff_filehdr *efp;
    162 	struct ecoff_aouthdr *eap;
    163 {
    164 	epp->ep_taddr = ECOFF_SEGMENT_ALIGN(eap, eap->ea_text_start);
    165 	epp->ep_tsize = eap->ea_tsize;
    166 	epp->ep_daddr = ECOFF_SEGMENT_ALIGN(eap, eap->ea_data_start);
    167 	epp->ep_dsize = eap->ea_dsize + eap->ea_bsize;
    168 	epp->ep_entry = eap->ea_entry;
    169 
    170 	/* set up command for text and data segments */
    171 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn,
    172 		  eap->ea_tsize + eap->ea_dsize, epp->ep_taddr, epp->ep_vp,
    173 		  ECOFF_TXTOFF(efp, eap),
    174 		  VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
    175 
    176 	/* set up command for bss segment */
    177 	if (eap->ea_bsize > 0)
    178 		NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, eap->ea_bsize,
    179 			  ECOFF_SEGMENT_ALIGN(eap, eap->ea_bss_start),
    180 			  NULLVP, 0,
    181 			  VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
    182 
    183 	return exec_ecoff_setup_stack(p, epp);
    184 }
    185 
    186 /*
    187  * exec_ecoff_prep_nmagic(): Prepare a 'native' NMAGIC ECOFF binary's exec
    188  *                           package.
    189  */
    190 
    191 int
    192 exec_ecoff_prep_nmagic(p, epp, efp, eap)
    193 	struct proc *p;
    194 	struct exec_package *epp;
    195 	struct ecoff_filehdr *efp;
    196 	struct ecoff_aouthdr *eap;
    197 {
    198 	epp->ep_taddr = ECOFF_SEGMENT_ALIGN(eap, eap->ea_text_start);
    199 	epp->ep_tsize = eap->ea_tsize;
    200 	epp->ep_daddr = ECOFF_ROUND(eap->ea_data_start, ECOFF_LDPGSZ);
    201 	epp->ep_dsize = eap->ea_dsize + eap->ea_bsize;
    202 	epp->ep_entry = eap->ea_entry;
    203 
    204 	/* set up command for text segment */
    205 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, epp->ep_tsize,
    206 		  epp->ep_taddr, epp->ep_vp, ECOFF_TXTOFF(efp, eap),
    207 		  VM_PROT_READ|VM_PROT_EXECUTE);
    208 
    209 	/* set up command for data segment */
    210 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, epp->ep_dsize,
    211 		  epp->ep_daddr, epp->ep_vp, ECOFF_DATOFF(efp, eap),
    212 		  VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
    213 
    214 	/* set up command for bss segment */
    215 	if (eap->ea_bsize > 0)
    216 		NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, eap->ea_bsize,
    217 			  ECOFF_SEGMENT_ALIGN(eap, eap->ea_bss_start),
    218 			  NULLVP, 0,
    219 			  VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
    220 
    221 	return exec_ecoff_setup_stack(p, epp);
    222 }
    223 
    224 /*
    225  * exec_ecoff_prep_zmagic(): Prepare a ECOFF ZMAGIC binary's exec package
    226  *
    227  * First, set the various offsets/lengths in the exec package.
    228  *
    229  * Then, mark the text image busy (so it can be demand paged) or error
    230  * out if this is not possible.  Finally, set up vmcmds for the
    231  * text, data, bss, and stack segments.
    232  */
    233 
    234 int
    235 exec_ecoff_prep_zmagic(p, epp, efp, eap)
    236 	struct proc *p;
    237 	struct exec_package *epp;
    238 	struct ecoff_filehdr *efp;
    239 	struct ecoff_aouthdr *eap;
    240 {
    241 	epp->ep_taddr = ECOFF_SEGMENT_ALIGN(eap, eap->ea_text_start);
    242 	epp->ep_tsize = eap->ea_tsize;
    243 	epp->ep_daddr = ECOFF_SEGMENT_ALIGN(eap, eap->ea_data_start);
    244 	epp->ep_dsize = eap->ea_dsize + eap->ea_bsize;
    245 	epp->ep_entry = eap->ea_entry;
    246 
    247 	/*
    248 	 * check if vnode is in open for writing, because we want to
    249 	 * demand-page out of it.  if it is, don't do it, for various
    250 	 * reasons
    251 	 */
    252 	if ((eap->ea_tsize != 0 || eap->ea_dsize != 0) &&
    253 	    epp->ep_vp->v_writecount != 0) {
    254 #ifdef DIAGNOSTIC
    255 		if (epp->ep_vp->v_flag & VTEXT)
    256 			panic("exec: a VTEXT vnode has writecount != 0\n");
    257 #endif
    258 		return ETXTBSY;
    259 	}
    260 	epp->ep_vp->v_flag |= VTEXT;
    261 
    262 	/* set up command for text segment */
    263 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_pagedvn, eap->ea_tsize,
    264 		  epp->ep_taddr, epp->ep_vp, ECOFF_TXTOFF(efp, eap),
    265 		  VM_PROT_READ|VM_PROT_EXECUTE);
    266 
    267 	/* set up command for data segment */
    268 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_pagedvn, eap->ea_dsize,
    269 		  epp->ep_daddr, epp->ep_vp,
    270 		  ECOFF_DATOFF(efp, eap),
    271 		  VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
    272 
    273 	/* set up command for bss segment */
    274 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, eap->ea_bsize,
    275 		  ECOFF_SEGMENT_ALIGN(eap, eap->ea_bss_start), NULLVP, 0,
    276 		  VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
    277 
    278 	return exec_ecoff_setup_stack(p, epp);
    279 }
    280