Home | History | Annotate | Line # | Download | only in kern
exec_aout.c revision 1.3
      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.3 1994/01/08 07:14:58 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 #ifdef EXEC_DEBUG
     71 	printf("exec_makecmds: a_midmag is %x, magic=%x mid=%x\n",
     72 	    epp->ep_execp->a_midmag, magic, mid);
     73 #endif
     74 
     75 	midmag = mid << 16 | magic;
     76 
     77 	switch (midmag) {
     78 	case (MID_MACHINE << 16) | ZMAGIC:
     79 		error = exec_aout_prep_zmagic(p, epp);
     80 		break;
     81 	case (MID_MACHINE << 16) | NMAGIC:
     82 		error = exec_aout_prep_nmagic(p, epp);
     83 		break;
     84 	case (MID_MACHINE << 16) | OMAGIC:
     85 		error = exec_aout_prep_omagic(p, epp);
     86 		break;
     87 	default:
     88 		error = cpu_exec_aout_makecmds(p, epp);
     89 	}
     90 
     91 	if (error)
     92 		KILL_VMCMDS(&epp->ep_vmcmds);
     93 
     94 bad:
     95 
     96 #ifdef EXEC_DEBUG
     97 	printf("exec_makecmds returning with error = %d\n", error);
     98 #endif
     99 	return error;
    100 }
    101 
    102 /*
    103  * exec_aout_prep_zmagic(): Prepare a 'native' ZMAGIC binary's exec package
    104  *
    105  * First, set of the various offsets/lengths in the exec package.
    106  *
    107  * Then, mark the text image busy (so it can be demand paged) or error
    108  * out if this is not possible.  Finally, set up vmcmds for the
    109  * text, data, bss, and stack segments.
    110  */
    111 
    112 int
    113 exec_aout_prep_zmagic(p, epp)
    114 	struct proc *p;
    115 	struct exec_package *epp;
    116 {
    117 	struct exec *execp = epp->ep_execp;
    118 
    119 	epp->ep_taddr = USRTEXT;
    120 	epp->ep_tsize = execp->a_text;
    121 	epp->ep_daddr = epp->ep_taddr + execp->a_text;
    122 	epp->ep_dsize = execp->a_data + execp->a_bss;
    123 	epp->ep_entry = execp->a_entry;
    124 
    125 	/*
    126 	 * check if vnode is in open for writing, because we want to
    127 	 * demand-page out of it.  if it is, don't do it, for various
    128 	 * reasons
    129 	 */
    130 	if ((execp->a_text != 0 || execp->a_data != 0) &&
    131 	    epp->ep_vp->v_writecount != 0) {
    132 #ifdef DIAGNOSTIC
    133 		if (epp->ep_vp->v_flag & VTEXT)
    134 			panic("exec: a VTEXT vnode has writecount != 0\n");
    135 #endif
    136 		return ETXTBSY;
    137 	}
    138 	epp->ep_vp->v_flag |= VTEXT;
    139 
    140 	/* set up command for text segment */
    141 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_pagedvn, execp->a_text,
    142 	    epp->ep_taddr, epp->ep_vp, 0, VM_PROT_READ|VM_PROT_EXECUTE);
    143 
    144 	/* set up command for data segment */
    145 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_pagedvn, execp->a_data,
    146 	    epp->ep_daddr, epp->ep_vp, execp->a_text,
    147 	    VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
    148 
    149 	/* set up command for bss segment */
    150 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, execp->a_bss,
    151 	    epp->ep_daddr + execp->a_data, 0, 0,
    152 	    VM_PROT_READ|VM_PROT_WRITE |VM_PROT_EXECUTE);
    153 
    154 	return exec_aout_setup_stack(p, epp);
    155 }
    156 
    157 /*
    158  * exec_aout_prep_nmagic(): Prepare a 'native' NMAGIC binary's exec package
    159  */
    160 
    161 int
    162 exec_aout_prep_nmagic(p, epp)
    163 	struct proc *p;
    164 	struct exec_package *epp;
    165 {
    166 	struct exec *execp = epp->ep_execp;
    167 	long bsize, baddr;
    168 
    169 	epp->ep_taddr = USRTEXT;
    170 	epp->ep_tsize = execp->a_text;
    171 	epp->ep_daddr = roundup(epp->ep_taddr + execp->a_text, __LDPGSZ);
    172 	epp->ep_dsize = execp->a_data + execp->a_bss;
    173 	epp->ep_entry = execp->a_entry;
    174 
    175 	/* set up command for text segment */
    176 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, execp->a_text,
    177 	    epp->ep_taddr, epp->ep_vp, sizeof(struct exec),
    178 	    VM_PROT_READ|VM_PROT_EXECUTE);
    179 
    180 	/* set up command for data segment */
    181 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, execp->a_data,
    182 	    epp->ep_daddr, epp->ep_vp, execp->a_text + sizeof(struct exec),
    183 	    VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
    184 
    185 	/* set up command for bss segment */
    186 	baddr = roundup(epp->ep_daddr + execp->a_data, NBPG);
    187 	bsize = epp->ep_daddr + epp->ep_dsize - baddr;
    188 	if (bsize > 0)
    189 		NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, bsize, baddr,
    190 		    0, 0, VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE);
    191 
    192 	return exec_aout_setup_stack(p, epp);
    193 }
    194 
    195 /*
    196  * exec_aout_prep_omagic(): Prepare a 'native' OMAGIC binary's exec package
    197  */
    198 
    199 int
    200 exec_aout_prep_omagic(p, epp)
    201 	struct proc *p;
    202 	struct exec_package *epp;
    203 {
    204 	struct exec *execp = epp->ep_execp;
    205 	long bsize, baddr;
    206 
    207 	epp->ep_taddr = USRTEXT;
    208 	epp->ep_tsize = execp->a_text;
    209 	epp->ep_daddr = epp->ep_taddr + execp->a_text;
    210 	epp->ep_dsize = execp->a_data + execp->a_bss;
    211 	epp->ep_entry = execp->a_entry;
    212 
    213 	/* set up command for text and data segments */
    214 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn,
    215 	    execp->a_text + execp->a_data, epp->ep_taddr, epp->ep_vp,
    216 	    sizeof(struct exec), VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
    217 
    218 	/* set up command for bss segment */
    219 	baddr = roundup(epp->ep_daddr + execp->a_data, NBPG);
    220 	bsize = epp->ep_daddr + epp->ep_dsize - baddr;
    221 	if (bsize > 0)
    222 		NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, bsize, baddr,
    223 		    0, 0, VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
    224 
    225 	return exec_aout_setup_stack(p, epp);
    226 }
    227 
    228 /*
    229  * exec_aout_setup_stack(): Set up the stack segment for an a.out
    230  * executable.
    231  *
    232  * Note that the ep_ssize parameter must be set to be the current stack
    233  * limit; this is adjusted in the body of execve() to yield the
    234  * appropriate stack segment usage once the argument length is
    235  * calculated.
    236  *
    237  * This function returns an int for uniformity with other (future) formats'
    238  * stack setup functions.  They might have errors to return.
    239  */
    240 
    241 int
    242 exec_aout_setup_stack(p, epp)
    243 	struct proc *p;
    244 	struct exec_package *epp;
    245 {
    246 
    247 	epp->ep_maxsaddr = USRSTACK - MAXSSIZ;
    248 	epp->ep_minsaddr = USRSTACK;
    249 	epp->ep_ssize = p->p_rlimit[RLIMIT_STACK].rlim_cur;
    250 
    251 	/*
    252 	 * set up commands for stack.  note that this takes *two*, one to
    253 	 * map the part of the stack which we can access, and one to map
    254 	 * the part which we can't.
    255 	 *
    256 	 * arguably, it could be made into one, but that would require the
    257 	 * addition of another mapping proc, which is unnecessary
    258 	 *
    259 	 * note that in memory, things assumed to be: 0 ....... ep_maxsaddr
    260 	 * <stack> ep_minsaddr
    261 	 */
    262 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero,
    263 	    ((epp->ep_minsaddr - epp->ep_ssize) - epp->ep_maxsaddr),
    264 	    epp->ep_maxsaddr, 0, 0, VM_PROT_NONE);
    265 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, epp->ep_ssize,
    266 	    (epp->ep_minsaddr - epp->ep_ssize), 0, 0,
    267 	    VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
    268 
    269 	return 0;
    270 }
    271