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