Home | History | Annotate | Line # | Download | only in kern
exec_aout.c revision 1.15.24.1
      1 /*	$NetBSD: exec_aout.c,v 1.15.24.1 1999/07/04 01:33:35 chs 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 /*
     43  * exec_aout_makecmds(): Check if it's an a.out-format executable.
     44  *
     45  * Given a proc pointer and an exec package pointer, see if the referent
     46  * of the epp is in a.out format.  First check 'standard' magic numbers for
     47  * this architecture.  If that fails, try a cpu-dependent hook.
     48  *
     49  * This function, in the former case, or the hook, in the latter, is
     50  * responsible for creating a set of vmcmds which can be used to build
     51  * the process's vm space and inserting them into the exec package.
     52  */
     53 
     54 int
     55 exec_aout_makecmds(p, epp)
     56 	struct proc *p;
     57 	struct exec_package *epp;
     58 {
     59 	u_long midmag, magic;
     60 	u_short mid;
     61 	int error;
     62 	struct exec *execp = epp->ep_hdr;
     63 
     64 	if (epp->ep_hdrvalid < sizeof(struct exec))
     65 		return ENOEXEC;
     66 
     67 	midmag = ntohl(execp->a_midmag);
     68 	mid = (midmag >> 16) & 0x3ff;
     69 	magic = midmag & 0xffff;
     70 
     71 	midmag = mid << 16 | magic;
     72 
     73 	switch (midmag) {
     74 	case (MID_MACHINE << 16) | ZMAGIC:
     75 		error = exec_aout_prep_zmagic(p, epp);
     76 		break;
     77 	case (MID_MACHINE << 16) | NMAGIC:
     78 		error = exec_aout_prep_nmagic(p, epp);
     79 		break;
     80 	case (MID_MACHINE << 16) | OMAGIC:
     81 		error = exec_aout_prep_omagic(p, epp);
     82 		break;
     83 	default:
     84 		error = cpu_exec_aout_makecmds(p, epp);
     85 	}
     86 
     87 	if (error)
     88 		kill_vmcmds(&epp->ep_vmcmds);
     89 
     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_hdr;
    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 
    130 	/*
    131 	 * mark this vnode as being the image of a running process.
    132 	 * also flush any cached file mappings now so the process will
    133 	 * have a better chance of being able to use the cache.
    134 	 */
    135 
    136 	epp->ep_vp->v_flag |= VTEXT;
    137 	ubc_flush(&epp->ep_vp->v_uvm.u_obj, 0, 0);
    138 
    139 	/* set up command for text segment */
    140 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_pagedvn, execp->a_text,
    141 	    epp->ep_taddr, epp->ep_vp, 0, VM_PROT_READ|VM_PROT_EXECUTE);
    142 
    143 	/* set up command for data segment */
    144 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_pagedvn, execp->a_data,
    145 	    epp->ep_daddr, epp->ep_vp, execp->a_text,
    146 	    VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
    147 
    148 	/* set up command for bss segment */
    149 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, execp->a_bss,
    150 	    epp->ep_daddr + execp->a_data, NULLVP, 0,
    151 	    VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
    152 
    153 	return exec_aout_setup_stack(p, epp);
    154 }
    155 
    156 /*
    157  * exec_aout_prep_nmagic(): Prepare a 'native' NMAGIC binary's exec package
    158  */
    159 
    160 int
    161 exec_aout_prep_nmagic(p, epp)
    162 	struct proc *p;
    163 	struct exec_package *epp;
    164 {
    165 	struct exec *execp = epp->ep_hdr;
    166 	long bsize, baddr;
    167 
    168 	epp->ep_taddr = USRTEXT;
    169 	epp->ep_tsize = execp->a_text;
    170 	epp->ep_daddr = roundup(epp->ep_taddr + execp->a_text, __LDPGSZ);
    171 	epp->ep_dsize = execp->a_data + execp->a_bss;
    172 	epp->ep_entry = execp->a_entry;
    173 
    174 	/* set up command for text segment */
    175 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, execp->a_text,
    176 	    epp->ep_taddr, epp->ep_vp, sizeof(struct exec),
    177 	    VM_PROT_READ|VM_PROT_EXECUTE);
    178 
    179 	/* set up command for data segment */
    180 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, execp->a_data,
    181 	    epp->ep_daddr, epp->ep_vp, execp->a_text + sizeof(struct exec),
    182 	    VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
    183 
    184 	/* set up command for bss segment */
    185 	baddr = roundup(epp->ep_daddr + execp->a_data, NBPG);
    186 	bsize = epp->ep_daddr + epp->ep_dsize - baddr;
    187 	if (bsize > 0)
    188 		NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, bsize, baddr,
    189 		    NULLVP, 0, VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
    190 
    191 	return exec_aout_setup_stack(p, epp);
    192 }
    193 
    194 /*
    195  * exec_aout_prep_omagic(): Prepare a 'native' OMAGIC binary's exec package
    196  */
    197 
    198 int
    199 exec_aout_prep_omagic(p, epp)
    200 	struct proc *p;
    201 	struct exec_package *epp;
    202 {
    203 	struct exec *execp = epp->ep_hdr;
    204 	long dsize, bsize, baddr;
    205 
    206 	epp->ep_taddr = USRTEXT;
    207 	epp->ep_tsize = execp->a_text;
    208 	epp->ep_daddr = epp->ep_taddr + execp->a_text;
    209 	epp->ep_dsize = execp->a_data + execp->a_bss;
    210 	epp->ep_entry = execp->a_entry;
    211 
    212 	/* set up command for text and data segments */
    213 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn,
    214 	    execp->a_text + execp->a_data, epp->ep_taddr, epp->ep_vp,
    215 	    sizeof(struct exec), VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
    216 
    217 	/* set up command for bss segment */
    218 	baddr = roundup(epp->ep_daddr + execp->a_data, NBPG);
    219 	bsize = epp->ep_daddr + epp->ep_dsize - baddr;
    220 	if (bsize > 0)
    221 		NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, bsize, baddr,
    222 		    NULLVP, 0, VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
    223 
    224 	/*
    225 	 * Make sure (# of pages) mapped above equals (vm_tsize + vm_dsize);
    226 	 * obreak(2) relies on this fact. Both `vm_tsize' and `vm_dsize' are
    227 	 * computed (in execve(2)) by rounding *up* `ep_tsize' and `ep_dsize'
    228 	 * respectively to page boundaries.
    229 	 * Compensate `ep_dsize' for the amount of data covered by the last
    230 	 * text page.
    231 	 */
    232 	dsize = epp->ep_dsize + execp->a_text - roundup(execp->a_text, NBPG);
    233 	epp->ep_dsize = (dsize > 0) ? dsize : 0;
    234 	return exec_aout_setup_stack(p, epp);
    235 }
    236 
    237 /*
    238  * exec_aout_setup_stack(): Set up the stack segment for an a.out
    239  * executable.
    240  *
    241  * Note that the ep_ssize parameter must be set to be the current stack
    242  * limit; this is adjusted in the body of execve() to yield the
    243  * appropriate stack segment usage once the argument length is
    244  * calculated.
    245  *
    246  * This function returns an int for uniformity with other (future) formats'
    247  * stack setup functions.  They might have errors to return.
    248  */
    249 
    250 int
    251 exec_aout_setup_stack(p, epp)
    252 	struct proc *p;
    253 	struct exec_package *epp;
    254 {
    255 
    256 	epp->ep_maxsaddr = USRSTACK - MAXSSIZ;
    257 	epp->ep_minsaddr = USRSTACK;
    258 	epp->ep_ssize = p->p_rlimit[RLIMIT_STACK].rlim_cur;
    259 
    260 	/*
    261 	 * set up commands for stack.  note that this takes *two*, one to
    262 	 * map the part of the stack which we can access, and one to map
    263 	 * the part which we can't.
    264 	 *
    265 	 * arguably, it could be made into one, but that would require the
    266 	 * addition of another mapping proc, which is unnecessary
    267 	 *
    268 	 * note that in memory, things assumed to be: 0 ... ep_maxsaddr
    269 	 * <stack> ep_minsaddr
    270 	 */
    271 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero,
    272 	    ((epp->ep_minsaddr - epp->ep_ssize) - epp->ep_maxsaddr),
    273 	    epp->ep_maxsaddr, NULLVP, 0, VM_PROT_NONE);
    274 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, epp->ep_ssize,
    275 	    (epp->ep_minsaddr - epp->ep_ssize), NULLVP, 0,
    276 	    VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
    277 
    278 	return 0;
    279 }
    280