Home | History | Annotate | Line # | Download | only in kern
exec_ecoff.c revision 1.22.2.1
      1 /*	$NetBSD: exec_ecoff.c,v 1.22.2.1 2003/07/02 15:26:34 darrenr Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1994 Adam Glass
      5  * Copyright (c) 1993, 1994, 1996, 1999 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  *      for the NetBSD Project.
     20  * 4. The name of the author may not be used to endorse or promote products
     21  *    derived from this software without specific prior written permission
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     24  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     25  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     26  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     27  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     28  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     29  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     30  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     31  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     32  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     33  */
     34 
     35 #include <sys/cdefs.h>
     36 __KERNEL_RCSID(0, "$NetBSD: exec_ecoff.c,v 1.22.2.1 2003/07/02 15:26:34 darrenr Exp $");
     37 
     38 #include <sys/param.h>
     39 #include <sys/systm.h>
     40 #include <sys/proc.h>
     41 #include <sys/malloc.h>
     42 #include <sys/vnode.h>
     43 #include <sys/exec.h>
     44 #include <sys/resourcevar.h>
     45 
     46 #include <sys/exec_ecoff.h>
     47 
     48 /*
     49  * exec_ecoff_makecmds(): Check if it's an ecoff-format executable.
     50  *
     51  * Given a proc pointer and an exec package pointer, see if the referent
     52  * of the epp is in ecoff format.  Check 'standard' magic numbers for
     53  * this architecture.  If that fails, return failure.
     54  *
     55  * This function is  responsible for creating a set of vmcmds which can be
     56  * used to build the process's vm space and inserting them into the exec
     57  * package.
     58  */
     59 int
     60 exec_ecoff_makecmds(struct lwp *l, struct exec_package *epp)
     61 {
     62 	int error;
     63 	struct ecoff_exechdr *execp = epp->ep_hdr;
     64 	struct proc *p = l->l_proc;
     65 
     66 	if (epp->ep_hdrvalid < ECOFF_HDR_SIZE)
     67 		return ENOEXEC;
     68 
     69 	if (ECOFF_BADMAG(execp))
     70 		return ENOEXEC;
     71 
     72 	error = (*epp->ep_esch->u.ecoff_probe_func)(p, epp);
     73 
     74 	/*
     75 	 * if there was an error or there are already vmcmds set up,
     76 	 * we return.  (the latter can happen if cpu_exec_ecoff_hook()
     77 	 * recursively invokes check_exec() to handle loading of a
     78 	 * dynamically linked binary's shared loader.
     79 	 */
     80 	if (error || epp->ep_vmcmds.evs_cnt)
     81 		return (error);
     82 
     83 	/*
     84 	 * prepare the exec package to map the executable.
     85 	 */
     86 	switch (execp->a.magic) {
     87 	case ECOFF_OMAGIC:
     88 		error = exec_ecoff_prep_omagic(p, epp, epp->ep_hdr,
     89 		   epp->ep_vp);
     90 		break;
     91 	case ECOFF_NMAGIC:
     92 		error = exec_ecoff_prep_nmagic(p, epp, epp->ep_hdr,
     93 		   epp->ep_vp);
     94 		break;
     95 	case ECOFF_ZMAGIC:
     96 		error = exec_ecoff_prep_zmagic(p, epp, epp->ep_hdr,
     97 		   epp->ep_vp);
     98 		break;
     99 	default:
    100 		return ENOEXEC;
    101 	}
    102 
    103 	/* set up the stack */
    104 	if (!error)
    105 		error = exec_ecoff_setup_stack(p, epp);
    106 
    107 	if (error)
    108 		kill_vmcmds(&epp->ep_vmcmds);
    109 
    110 	return error;
    111 }
    112 
    113 /*
    114  * exec_ecoff_setup_stack(): Set up the stack segment for an ecoff
    115  * executable.
    116  *
    117  * Note that the ep_ssize parameter must be set to be the current stack
    118  * limit; this is adjusted in the body of execve() to yield the
    119  * appropriate stack segment usage once the argument length is
    120  * calculated.
    121  *
    122  * This function returns an int for uniformity with other (future) formats'
    123  * stack setup functions.  They might have errors to return.
    124  */
    125 int
    126 exec_ecoff_setup_stack(struct proc *p, struct exec_package *epp)
    127 {
    128 
    129 	epp->ep_maxsaddr = USRSTACK - MAXSSIZ;
    130 	epp->ep_minsaddr = USRSTACK;
    131 	epp->ep_ssize = p->p_rlimit[RLIMIT_STACK].rlim_cur;
    132 
    133 	/*
    134 	 * set up commands for stack.  note that this takes *two*, one to
    135 	 * map the part of the stack which we can access, and one to map
    136 	 * the part which we can't.
    137 	 *
    138 	 * arguably, it could be made into one, but that would require the
    139 	 * addition of another mapping proc, which is unnecessary
    140 	 *
    141 	 * note that in memory, things assumed to be: 0 ... ep_maxsaddr
    142 	 * <stack> ep_minsaddr
    143 	 */
    144 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero,
    145 	    ((epp->ep_minsaddr - epp->ep_ssize) - epp->ep_maxsaddr),
    146 	    epp->ep_maxsaddr, NULLVP, 0, VM_PROT_NONE);
    147 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, epp->ep_ssize,
    148 	    (epp->ep_minsaddr - epp->ep_ssize), NULLVP, 0,
    149 	    VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
    150 
    151 	return 0;
    152 }
    153 
    154 /*
    155  * exec_ecoff_prep_omagic(): Prepare a ECOFF OMAGIC binary's exec package
    156  */
    157 int
    158 exec_ecoff_prep_omagic(struct proc *p, struct exec_package *epp,
    159     struct ecoff_exechdr *execp, struct vnode *vp)
    160 {
    161 	struct ecoff_aouthdr *eap = &execp->a;
    162 
    163 	epp->ep_taddr = ECOFF_SEGMENT_ALIGN(execp, eap->text_start);
    164 	epp->ep_tsize = eap->tsize;
    165 	epp->ep_daddr = ECOFF_SEGMENT_ALIGN(execp, eap->data_start);
    166 	epp->ep_dsize = eap->dsize + eap->bsize;
    167 	epp->ep_entry = eap->entry;
    168 
    169 	/* set up command for text and data segments */
    170 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn,
    171 	    eap->tsize + eap->dsize, epp->ep_taddr, vp,
    172 	    ECOFF_TXTOFF(execp),
    173 	    VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
    174 
    175 	/* set up command for bss segment */
    176 	if (eap->bsize > 0)
    177 		NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, eap->bsize,
    178 		    ECOFF_SEGMENT_ALIGN(execp, eap->bss_start), NULLVP, 0,
    179 		    VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
    180 
    181 	return 0;
    182 }
    183 
    184 /*
    185  * exec_ecoff_prep_nmagic(): Prepare a 'native' NMAGIC ECOFF binary's exec
    186  *                           package.
    187  */
    188 int
    189 exec_ecoff_prep_nmagic(struct proc *p, struct exec_package *epp,
    190     struct ecoff_exechdr *execp, struct vnode *vp)
    191 {
    192 	struct ecoff_aouthdr *eap = &execp->a;
    193 
    194 	epp->ep_taddr = ECOFF_SEGMENT_ALIGN(execp, eap->text_start);
    195 	epp->ep_tsize = eap->tsize;
    196 	epp->ep_daddr = ECOFF_ROUND(eap->data_start, ECOFF_LDPGSZ);
    197 	epp->ep_dsize = eap->dsize + eap->bsize;
    198 	epp->ep_entry = eap->entry;
    199 
    200 	/* set up command for text segment */
    201 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, epp->ep_tsize,
    202 	    epp->ep_taddr, vp, ECOFF_TXTOFF(execp),
    203 	    VM_PROT_READ|VM_PROT_EXECUTE);
    204 
    205 	/* set up command for data segment */
    206 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, epp->ep_dsize,
    207 	    epp->ep_daddr, vp, ECOFF_DATOFF(execp),
    208 	    VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
    209 
    210 	/* set up command for bss segment */
    211 	if (eap->bsize > 0)
    212 		NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, eap->bsize,
    213 		    ECOFF_SEGMENT_ALIGN(execp, eap->bss_start), NULLVP, 0,
    214 		    VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
    215 
    216 	return 0;
    217 }
    218 
    219 /*
    220  * exec_ecoff_prep_zmagic(): Prepare a ECOFF ZMAGIC binary's exec package
    221  *
    222  * First, set the various offsets/lengths in the exec package.
    223  *
    224  * Then, mark the text image busy (so it can be demand paged) or error
    225  * out if this is not possible.  Finally, set up vmcmds for the
    226  * text, data, bss, and stack segments.
    227  */
    228 int
    229 exec_ecoff_prep_zmagic(struct proc *p, struct exec_package *epp,
    230     struct ecoff_exechdr *execp, struct vnode *vp)
    231 {
    232 	struct ecoff_aouthdr *eap = &execp->a;
    233 	int error;
    234 
    235 	epp->ep_taddr = ECOFF_SEGMENT_ALIGN(execp, eap->text_start);
    236 	epp->ep_tsize = eap->tsize;
    237 	epp->ep_daddr = ECOFF_SEGMENT_ALIGN(execp, eap->data_start);
    238 	epp->ep_dsize = eap->dsize + eap->bsize;
    239 	epp->ep_entry = eap->entry;
    240 
    241 	error = vn_marktext(vp);
    242 	if (error)
    243 		return (error);
    244 
    245 	/* set up command for text segment */
    246 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_pagedvn, eap->tsize,
    247 	    epp->ep_taddr, vp, ECOFF_TXTOFF(execp),
    248 	    VM_PROT_READ|VM_PROT_EXECUTE);
    249 
    250 	/* set up command for data segment */
    251 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_pagedvn, eap->dsize,
    252 	    epp->ep_daddr, vp, ECOFF_DATOFF(execp),
    253 	    VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
    254 
    255 	/* set up command for bss segment */
    256 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, eap->bsize,
    257 	    ECOFF_SEGMENT_ALIGN(execp, eap->bss_start), NULLVP, 0,
    258 	    VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
    259 
    260 	return 0;
    261 }
    262