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