Home | History | Annotate | Line # | Download | only in kern
exec_aout.c revision 1.18
      1 /*	$NetBSD: exec_aout.c,v 1.18 2000/06/27 17:41:07 mrg 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 
     41 /*
     42  * exec_aout_makecmds(): Check if it's an a.out-format executable.
     43  *
     44  * Given a proc pointer and an exec package pointer, see if the referent
     45  * of the epp is in a.out format.  First check 'standard' magic numbers for
     46  * this architecture.  If that fails, try a cpu-dependent hook.
     47  *
     48  * This function, in the former case, or the hook, in the latter, is
     49  * responsible for creating a set of vmcmds which can be used to build
     50  * the process's vm space and inserting them into the exec package.
     51  */
     52 
     53 int
     54 exec_aout_makecmds(p, epp)
     55 	struct proc *p;
     56 	struct exec_package *epp;
     57 {
     58 	u_long midmag, magic;
     59 	u_short mid;
     60 	int error;
     61 	struct exec *execp = epp->ep_hdr;
     62 
     63 	if (epp->ep_hdrvalid < sizeof(struct exec))
     64 		return ENOEXEC;
     65 
     66 	midmag = ntohl(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 	return error;
     90 }
     91 
     92 /*
     93  * exec_aout_prep_zmagic(): Prepare a 'native' ZMAGIC binary's exec package
     94  *
     95  * First, set of the various offsets/lengths in the exec package.
     96  *
     97  * Then, mark the text image busy (so it can be demand paged) or error
     98  * out if this is not possible.  Finally, set up vmcmds for the
     99  * text, data, bss, and stack segments.
    100  */
    101 
    102 int
    103 exec_aout_prep_zmagic(p, epp)
    104 	struct proc *p;
    105 	struct exec_package *epp;
    106 {
    107 	struct exec *execp = epp->ep_hdr;
    108 
    109 	epp->ep_taddr = USRTEXT;
    110 	epp->ep_tsize = execp->a_text;
    111 	epp->ep_daddr = epp->ep_taddr + execp->a_text;
    112 	epp->ep_dsize = execp->a_data + execp->a_bss;
    113 	epp->ep_entry = execp->a_entry;
    114 
    115 	/*
    116 	 * check if vnode is in open for writing, because we want to
    117 	 * demand-page out of it.  if it is, don't do it, for various
    118 	 * reasons
    119 	 */
    120 	if ((execp->a_text != 0 || execp->a_data != 0) &&
    121 	    epp->ep_vp->v_writecount != 0) {
    122 #ifdef DIAGNOSTIC
    123 		if (epp->ep_vp->v_flag & VTEXT)
    124 			panic("exec: a VTEXT vnode has writecount != 0\n");
    125 #endif
    126 		return ETXTBSY;
    127 	}
    128 	vn_marktext(epp->ep_vp);
    129 
    130 	/* set up command for text segment */
    131 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_pagedvn, round_page(execp->a_text),
    132 	    epp->ep_taddr, epp->ep_vp, 0, VM_PROT_READ|VM_PROT_EXECUTE);
    133 
    134 	/* set up command for data segment */
    135 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_pagedvn, round_page(execp->a_data),
    136 	    epp->ep_daddr, epp->ep_vp, execp->a_text,
    137 	    VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
    138 
    139 	/* set up command for bss segment */
    140 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, execp->a_bss,
    141 	    epp->ep_daddr + execp->a_data, NULLVP, 0,
    142 	    VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
    143 
    144 	return exec_aout_setup_stack(p, epp);
    145 }
    146 
    147 /*
    148  * exec_aout_prep_nmagic(): Prepare a 'native' NMAGIC binary's exec package
    149  */
    150 
    151 int
    152 exec_aout_prep_nmagic(p, epp)
    153 	struct proc *p;
    154 	struct exec_package *epp;
    155 {
    156 	struct exec *execp = epp->ep_hdr;
    157 	long bsize, baddr;
    158 
    159 	epp->ep_taddr = USRTEXT;
    160 	epp->ep_tsize = execp->a_text;
    161 	epp->ep_daddr = roundup(epp->ep_taddr + execp->a_text, __LDPGSZ);
    162 	epp->ep_dsize = execp->a_data + execp->a_bss;
    163 	epp->ep_entry = execp->a_entry;
    164 
    165 	/* set up command for text segment */
    166 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, execp->a_text,
    167 	    epp->ep_taddr, epp->ep_vp, sizeof(struct exec),
    168 	    VM_PROT_READ|VM_PROT_EXECUTE);
    169 
    170 	/* set up command for data segment */
    171 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, execp->a_data,
    172 	    epp->ep_daddr, epp->ep_vp, execp->a_text + sizeof(struct exec),
    173 	    VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
    174 
    175 	/* set up command for bss segment */
    176 	baddr = roundup(epp->ep_daddr + execp->a_data, NBPG);
    177 	bsize = epp->ep_daddr + epp->ep_dsize - baddr;
    178 	if (bsize > 0)
    179 		NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, bsize, baddr,
    180 		    NULLVP, 0, VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
    181 
    182 	return exec_aout_setup_stack(p, epp);
    183 }
    184 
    185 /*
    186  * exec_aout_prep_omagic(): Prepare a 'native' OMAGIC binary's exec package
    187  */
    188 
    189 int
    190 exec_aout_prep_omagic(p, epp)
    191 	struct proc *p;
    192 	struct exec_package *epp;
    193 {
    194 	struct exec *execp = epp->ep_hdr;
    195 	long dsize, bsize, baddr;
    196 
    197 	epp->ep_taddr = USRTEXT;
    198 	epp->ep_tsize = execp->a_text;
    199 	epp->ep_daddr = epp->ep_taddr + execp->a_text;
    200 	epp->ep_dsize = execp->a_data + execp->a_bss;
    201 	epp->ep_entry = execp->a_entry;
    202 
    203 	/* set up command for text and data segments */
    204 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn,
    205 	    execp->a_text + execp->a_data, epp->ep_taddr, epp->ep_vp,
    206 	    sizeof(struct exec), VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
    207 
    208 	/* set up command for bss segment */
    209 	baddr = roundup(epp->ep_daddr + execp->a_data, NBPG);
    210 	bsize = epp->ep_daddr + epp->ep_dsize - baddr;
    211 	if (bsize > 0)
    212 		NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, bsize, baddr,
    213 		    NULLVP, 0, VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
    214 
    215 	/*
    216 	 * Make sure (# of pages) mapped above equals (vm_tsize + vm_dsize);
    217 	 * obreak(2) relies on this fact. Both `vm_tsize' and `vm_dsize' are
    218 	 * computed (in execve(2)) by rounding *up* `ep_tsize' and `ep_dsize'
    219 	 * respectively to page boundaries.
    220 	 * Compensate `ep_dsize' for the amount of data covered by the last
    221 	 * text page.
    222 	 */
    223 	dsize = epp->ep_dsize + execp->a_text - roundup(execp->a_text, NBPG);
    224 	epp->ep_dsize = (dsize > 0) ? dsize : 0;
    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, NULLVP, 0, VM_PROT_NONE);
    265 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, epp->ep_ssize,
    266 	    (epp->ep_minsaddr - epp->ep_ssize), NULLVP, 0,
    267 	    VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
    268 
    269 	return 0;
    270 }
    271