Home | History | Annotate | Line # | Download | only in kern
exec_aout.c revision 1.1.2.1
      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.1.2.1 1993/10/15 06:23:47 deraadt Exp $
     31  */
     32 
     33 #include "param.h"
     34 #include "systm.h"
     35 #include "filedesc.h"
     36 #include "kernel.h"
     37 #include "proc.h"
     38 #include "mount.h"
     39 #include "malloc.h"
     40 #include "namei.h"
     41 #include "vnode.h"
     42 #include "file.h"
     43 #include "exec.h"
     44 #include "resourcevar.h"
     45 #include "wait.h"
     46 
     47 #include "machine/cpu.h"
     48 #include "machine/reg.h"
     49 #include "machine/exec.h"
     50 
     51 #include "mman.h"
     52 #include "vm/vm.h"
     53 #include "vm/vm_param.h"
     54 #include "vm/vm_map.h"
     55 #include "vm/vm_kern.h"
     56 #include "vm/vm_pager.h"
     57 
     58 /*
     59  * exec_aout_makecmds(): Check if it's an a.out-format executable.
     60  *
     61  * Given a proc pointer and an exec package pointer, see if the referent
     62  * of the epp is in a.out format.  First check 'standard' magic numbers for
     63  * this architecture.  If that fails, try a cpu-dependent hook.
     64  *
     65  * This function, in the former case, or the hook, in the latter, is
     66  * responsible for creating a set of vmcmds which can be used to build
     67  * the process's vm space and inserting them into the exec package.
     68  *
     69  * XXX: NMAGIC and OMAGIC are currently not supported.
     70  */
     71 
     72 int
     73 exec_aout_makecmds(p, epp)
     74 	struct proc *p;
     75 	struct exec_package *epp;
     76 {
     77 	u_long midmag, magic;
     78 	u_short mid;
     79 	int error;
     80 
     81 	midmag = ntohl(epp->ep_execp->a_midmag);
     82 	mid = (midmag >> 16) & 0x3ff;
     83 	magic = midmag & 0xffff;
     84 
     85 #ifdef EXEC_DEBUG
     86 	printf("exec_makecmds: a_midmag is %x, magic=%x mid=%x\n",
     87 	    epp->ep_execp->a_midmag, magic, mid);
     88 #endif
     89 
     90 	midmag = mid << 16 | magic;
     91 
     92 	switch (midmag) {
     93 	case (MID_MACHINE << 16) | ZMAGIC:
     94 		error = exec_aout_prep_zmagic(p, epp);
     95 		break;
     96 	case (MID_MACHINE << 16) | NMAGIC:
     97 		error = exec_aout_prep_nmagic(p, epp);
     98 		break;
     99 	case (MID_MACHINE << 16) | OMAGIC:
    100 		printf("exec_aout_makecmds: OMAGIC not supported (yet)\n");
    101 	default:
    102 		error = cpu_exec_aout_makecmds(p, epp);
    103 	}
    104 
    105 	if (error && epp->ep_vcp)
    106 		kill_vmcmd(&epp->ep_vcp);
    107 
    108 bad:
    109 
    110 #ifdef EXEC_DEBUG
    111 	printf("exec_makecmds returning with error = %d\n", error);
    112 #endif
    113 	return error;
    114 }
    115 
    116 /*
    117  * exec_aout_prep_zmagic(): Prepare a 'native' ZMAGIC binary's exec package
    118  *
    119  * First, set of the various offsets/lengths in the exec package.
    120  * Note that the ep_ssize parameter must be set to be the current stack
    121  * limit; this is adjusted in the body of execve() to yield the
    122  * appropriate stack segment usage once the argument length is
    123  * calculated.
    124  *
    125  * Then, mark the text image busy (so it can be demand paged) or error
    126  * out if this is not possible.  Finally, set up vmcmds for the
    127  * text, data, bss, and stack segments.
    128  */
    129 
    130 int
    131 exec_aout_prep_zmagic(p, epp)
    132 	struct proc *p;
    133 	struct exec_package *epp;
    134 {
    135 	struct exec *execp = epp->ep_execp;
    136 	struct exec_vmcmd *ccmdp;
    137 
    138 	epp->ep_taddr = USRTEXT;
    139 	epp->ep_tsize = execp->a_text;
    140 	epp->ep_daddr = epp->ep_taddr + execp->a_text;
    141 	epp->ep_dsize = execp->a_data + execp->a_bss;
    142 	epp->ep_maxsaddr = USRSTACK - MAXSSIZ;
    143 	epp->ep_minsaddr = USRSTACK;
    144 	epp->ep_ssize = p->p_rlimit[RLIMIT_STACK].rlim_cur;
    145 	epp->ep_entry = execp->a_entry;
    146 
    147 	/*
    148 	 * check if vnode is in open for writing, because we want to
    149 	 * demand-page out of it.  if it is, don't do it, for various
    150 	 * reasons
    151 	 */
    152 	if ((execp->a_text != 0 || execp->a_data != 0) &&
    153 	    (epp->ep_vp->v_flag & VTEXT) == 0 && epp->ep_vp->v_writecount != 0) {
    154 #ifdef DIAGNOSTIC
    155 		if (epp->ep_vp->v_flag & VTEXT)
    156 			panic("exec: a VTEXT vnode has writecount != 0\n");
    157 #endif
    158 		epp->ep_vcp = NULL;
    159 		return ETXTBSY;
    160 	}
    161 	epp->ep_vp->v_flag |= VTEXT;
    162 
    163 	/* set up command for text segment */
    164 	epp->ep_vcp = new_vmcmd(vmcmd_map_pagedvn,
    165 	    execp->a_text,
    166 	    epp->ep_taddr,
    167 	    epp->ep_vp,
    168 	    0,
    169 	    VM_PROT_READ | VM_PROT_EXECUTE);
    170 	ccmdp = epp->ep_vcp;
    171 
    172 	/* set up command for data segment */
    173 	ccmdp->ev_next = new_vmcmd(vmcmd_map_pagedvn,
    174 	    execp->a_data,
    175 	    epp->ep_daddr,
    176 	    epp->ep_vp,
    177 	    execp->a_text,
    178 	    VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE);
    179 	ccmdp = ccmdp->ev_next;
    180 
    181 	/* set up command for bss segment */
    182 	ccmdp->ev_next = new_vmcmd(vmcmd_map_zero,
    183 	    execp->a_bss,
    184 	    epp->ep_daddr + execp->a_data,
    185 	    0,
    186 	    0,
    187 	    VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE);
    188 	ccmdp = ccmdp->ev_next;
    189 
    190 	/*
    191 	 * set up commands for stack.  note that this takes *two*, one to
    192 	 * map the part of the stack which we can access, and one to map
    193 	 * the part which we can't.
    194 	 *
    195 	 * arguably, it could be made into one, but that would require the
    196 	 * addition of another mapping proc, which is unnecessary
    197 	 *
    198 	 * note that in memory, things assumed to be: 0 ....... ep_maxsaddr
    199 	 * <stack> ep_minsaddr
    200 	 */
    201 	ccmdp->ev_next = new_vmcmd(vmcmd_map_zero,
    202 	    ((epp->ep_minsaddr - epp->ep_ssize) - epp->ep_maxsaddr),
    203 	    epp->ep_maxsaddr,
    204 	    0,
    205 	    0,
    206 	    VM_PROT_NONE);
    207 	ccmdp = ccmdp->ev_next;
    208 	ccmdp->ev_next = new_vmcmd(vmcmd_map_zero,
    209 	    epp->ep_ssize,
    210 	    (epp->ep_minsaddr - epp->ep_ssize),
    211 	    0,
    212 	    0,
    213 	    VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE);
    214 
    215 	return 0;
    216 }
    217 
    218 /*
    219  * exec_aout_prep_nmagic(): Prepare a 'native' NMAGIC binary's exec package
    220  *
    221  * First, set of the various offsets/lengths in the exec package.
    222  * Note that the ep_ssize parameter must be set to be the current stack
    223  * limit; this is adjusted in the body of execve() to yield the
    224  * appropriate stack segment usage once the argument length is
    225  * calculated.
    226  *
    227  * Then, mark the text image busy (so it can be demand paged) or error
    228  * out if this is not possible.  Finally, set up vmcmds for the
    229  * text, data, bss, and stack segments.
    230  */
    231 
    232 int
    233 exec_aout_prep_nmagic(p, epp)
    234 	struct proc *p;
    235 	struct exec_package *epp;
    236 {
    237 	struct exec *execp = epp->ep_execp;
    238 	struct exec_vmcmd *ccmdp;
    239 	long bsssize;
    240 
    241 	epp->ep_taddr = USRTEXT;
    242 	epp->ep_tsize = execp->a_text;
    243 	epp->ep_daddr = roundup(epp->ep_taddr + execp->a_text, __LDPGSZ);
    244 	epp->ep_dsize = execp->a_data + execp->a_bss;
    245 	epp->ep_maxsaddr = USRSTACK - MAXSSIZ;
    246 	epp->ep_minsaddr = USRSTACK;
    247 	epp->ep_ssize = p->p_rlimit[RLIMIT_STACK].rlim_cur;
    248 	epp->ep_entry = execp->a_entry;
    249 
    250 	/* set up command for text segment */
    251 	epp->ep_vcp = new_vmcmd(vmcmd_map_readvn,
    252 	    execp->a_text,
    253 	    epp->ep_taddr,
    254 	    epp->ep_vp,
    255 	    sizeof(struct exec),
    256 	    VM_PROT_READ | VM_PROT_EXECUTE);
    257 	ccmdp = epp->ep_vcp;
    258 
    259 	/* set up command for data segment */
    260 	ccmdp->ev_next = new_vmcmd(vmcmd_map_readvn,
    261 	    execp->a_data,
    262 	    epp->ep_daddr,
    263 	    epp->ep_vp,
    264 	    execp->a_text + sizeof(struct exec),
    265 	    VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE);
    266 	ccmdp = ccmdp->ev_next;
    267 
    268 	/* set up command for bss segment */
    269 	bsssize = epp->ep_daddr + execp->a_data + execp->a_bss -
    270 		roundup(epp->ep_daddr + execp->a_data, __LDPGSZ);
    271 	if(bsssize > 0) {
    272 		ccmdp->ev_next = new_vmcmd(vmcmd_map_zero,
    273 		    bsssize, roundup(epp->ep_daddr + execp->a_data, __LDPGSZ),
    274 		    0, 0, VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE);
    275 		ccmdp = ccmdp->ev_next;
    276 	}
    277 
    278 	/*
    279 	 * set up commands for stack.  note that this takes *two*, one to
    280 	 * map the part of the stack which we can access, and one to map
    281 	 * the part which we can't.
    282 	 *
    283 	 * arguably, it could be made into one, but that would require the
    284 	 * addition of another mapping proc, which is unnecessary
    285 	 *
    286 	 * note that in memory, things assumed to be: 0 ....... ep_maxsaddr
    287 	 * <stack> ep_minsaddr
    288 	 */
    289 	ccmdp->ev_next = new_vmcmd(vmcmd_map_zero,
    290 	    ((epp->ep_minsaddr - epp->ep_ssize) - epp->ep_maxsaddr),
    291 	    epp->ep_maxsaddr,
    292 	    0,
    293 	    0,
    294 	    VM_PROT_NONE);
    295 	ccmdp = ccmdp->ev_next;
    296 	ccmdp->ev_next = new_vmcmd(vmcmd_map_zero,
    297 	    epp->ep_ssize,
    298 	    (epp->ep_minsaddr - epp->ep_ssize),
    299 	    0,
    300 	    0,
    301 	    VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE);
    302 
    303 	return 0;
    304 }
    305