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