Home | History | Annotate | Line # | Download | only in kern
exec_aout.c revision 1.25
      1  1.25      chs /*	$NetBSD: exec_aout.c,v 1.25 2002/10/05 22:34:05 chs 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.22    lukem 
     33  1.22    lukem #include <sys/cdefs.h>
     34  1.25      chs __KERNEL_RCSID(0, "$NetBSD: exec_aout.c,v 1.25 2002/10/05 22:34:05 chs Exp $");
     35   1.1      cgd 
     36   1.2  deraadt #include <sys/param.h>
     37   1.2  deraadt #include <sys/systm.h>
     38   1.2  deraadt #include <sys/proc.h>
     39   1.2  deraadt #include <sys/malloc.h>
     40   1.2  deraadt #include <sys/vnode.h>
     41   1.2  deraadt #include <sys/exec.h>
     42   1.2  deraadt #include <sys/resourcevar.h>
     43   1.1      cgd 
     44  1.20  thorpej #include <uvm/uvm_extern.h>
     45  1.20  thorpej 
     46   1.1      cgd /*
     47   1.1      cgd  * exec_aout_makecmds(): Check if it's an a.out-format executable.
     48   1.1      cgd  *
     49   1.1      cgd  * Given a proc pointer and an exec package pointer, see if the referent
     50   1.1      cgd  * of the epp is in a.out format.  First check 'standard' magic numbers for
     51   1.1      cgd  * this architecture.  If that fails, try a cpu-dependent hook.
     52   1.1      cgd  *
     53   1.1      cgd  * This function, in the former case, or the hook, in the latter, is
     54   1.1      cgd  * responsible for creating a set of vmcmds which can be used to build
     55   1.1      cgd  * the process's vm space and inserting them into the exec package.
     56   1.1      cgd  */
     57   1.1      cgd 
     58   1.1      cgd int
     59  1.19  thorpej exec_aout_makecmds(struct proc *p, struct exec_package *epp)
     60   1.1      cgd {
     61   1.1      cgd 	u_long midmag, magic;
     62   1.1      cgd 	u_short mid;
     63   1.1      cgd 	int error;
     64   1.8      cgd 	struct exec *execp = epp->ep_hdr;
     65   1.1      cgd 
     66   1.8      cgd 	if (epp->ep_hdrvalid < sizeof(struct exec))
     67   1.8      cgd 		return ENOEXEC;
     68   1.8      cgd 
     69   1.8      cgd 	midmag = ntohl(execp->a_midmag);
     70   1.1      cgd 	mid = (midmag >> 16) & 0x3ff;
     71   1.1      cgd 	magic = midmag & 0xffff;
     72   1.1      cgd 
     73   1.1      cgd 	midmag = mid << 16 | magic;
     74   1.1      cgd 
     75   1.1      cgd 	switch (midmag) {
     76   1.1      cgd 	case (MID_MACHINE << 16) | ZMAGIC:
     77   1.1      cgd 		error = exec_aout_prep_zmagic(p, epp);
     78   1.1      cgd 		break;
     79   1.2  deraadt 	case (MID_MACHINE << 16) | NMAGIC:
     80   1.2  deraadt 		error = exec_aout_prep_nmagic(p, epp);
     81   1.2  deraadt 		break;
     82   1.1      cgd 	case (MID_MACHINE << 16) | OMAGIC:
     83   1.2  deraadt 		error = exec_aout_prep_omagic(p, epp);
     84   1.2  deraadt 		break;
     85   1.1      cgd 	default:
     86   1.1      cgd 		error = cpu_exec_aout_makecmds(p, epp);
     87   1.1      cgd 	}
     88   1.1      cgd 
     89   1.3      cgd 	if (error)
     90   1.6      cgd 		kill_vmcmds(&epp->ep_vmcmds);
     91   1.1      cgd 
     92   1.1      cgd 	return error;
     93   1.1      cgd }
     94   1.1      cgd 
     95   1.1      cgd /*
     96   1.1      cgd  * exec_aout_prep_zmagic(): Prepare a 'native' ZMAGIC binary's exec package
     97   1.1      cgd  *
     98   1.1      cgd  * First, set of the various offsets/lengths in the exec package.
     99   1.1      cgd  *
    100   1.1      cgd  * Then, mark the text image busy (so it can be demand paged) or error
    101   1.1      cgd  * out if this is not possible.  Finally, set up vmcmds for the
    102   1.1      cgd  * text, data, bss, and stack segments.
    103   1.1      cgd  */
    104   1.1      cgd 
    105   1.1      cgd int
    106  1.19  thorpej exec_aout_prep_zmagic(struct proc *p, struct exec_package *epp)
    107   1.1      cgd {
    108   1.8      cgd 	struct exec *execp = epp->ep_hdr;
    109  1.25      chs 	int error;
    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.25      chs 	error = vn_marktext(epp->ep_vp);
    118  1.25      chs 	if (error)
    119  1.25      chs 		return (error);
    120   1.1      cgd 
    121   1.1      cgd 	/* set up command for text segment */
    122  1.17      chs 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_pagedvn, round_page(execp->a_text),
    123   1.3      cgd 	    epp->ep_taddr, epp->ep_vp, 0, VM_PROT_READ|VM_PROT_EXECUTE);
    124   1.1      cgd 
    125   1.1      cgd 	/* set up command for data segment */
    126  1.17      chs 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_pagedvn, round_page(execp->a_data),
    127   1.3      cgd 	    epp->ep_daddr, epp->ep_vp, execp->a_text,
    128   1.3      cgd 	    VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
    129   1.1      cgd 
    130   1.1      cgd 	/* set up command for bss segment */
    131  1.23      chs 	if (execp->a_bss > 0)
    132  1.23      chs 		NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, execp->a_bss,
    133  1.23      chs 		    epp->ep_daddr + execp->a_data, NULLVP, 0,
    134  1.23      chs 		    VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
    135   1.2  deraadt 
    136   1.3      cgd 	return exec_aout_setup_stack(p, epp);
    137   1.2  deraadt }
    138   1.2  deraadt 
    139   1.2  deraadt /*
    140   1.2  deraadt  * exec_aout_prep_nmagic(): Prepare a 'native' NMAGIC binary's exec package
    141   1.2  deraadt  */
    142   1.2  deraadt 
    143   1.2  deraadt int
    144  1.19  thorpej exec_aout_prep_nmagic(struct proc *p, struct exec_package *epp)
    145   1.2  deraadt {
    146   1.8      cgd 	struct exec *execp = epp->ep_hdr;
    147   1.2  deraadt 	long bsize, baddr;
    148   1.2  deraadt 
    149   1.2  deraadt 	epp->ep_taddr = USRTEXT;
    150   1.2  deraadt 	epp->ep_tsize = execp->a_text;
    151   1.2  deraadt 	epp->ep_daddr = roundup(epp->ep_taddr + execp->a_text, __LDPGSZ);
    152   1.2  deraadt 	epp->ep_dsize = execp->a_data + execp->a_bss;
    153   1.2  deraadt 	epp->ep_entry = execp->a_entry;
    154   1.2  deraadt 
    155   1.2  deraadt 	/* set up command for text segment */
    156   1.3      cgd 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, execp->a_text,
    157   1.3      cgd 	    epp->ep_taddr, epp->ep_vp, sizeof(struct exec),
    158   1.3      cgd 	    VM_PROT_READ|VM_PROT_EXECUTE);
    159   1.2  deraadt 
    160   1.2  deraadt 	/* set up command for data segment */
    161   1.3      cgd 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, execp->a_data,
    162   1.3      cgd 	    epp->ep_daddr, epp->ep_vp, execp->a_text + sizeof(struct exec),
    163   1.3      cgd 	    VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
    164   1.2  deraadt 
    165   1.2  deraadt 	/* set up command for bss segment */
    166  1.20  thorpej 	baddr = round_page(epp->ep_daddr + execp->a_data);
    167   1.2  deraadt 	bsize = epp->ep_daddr + epp->ep_dsize - baddr;
    168   1.3      cgd 	if (bsize > 0)
    169   1.3      cgd 		NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, bsize, baddr,
    170   1.5  mycroft 		    NULLVP, 0, VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
    171   1.2  deraadt 
    172   1.3      cgd 	return exec_aout_setup_stack(p, epp);
    173   1.2  deraadt }
    174   1.2  deraadt 
    175   1.2  deraadt /*
    176   1.2  deraadt  * exec_aout_prep_omagic(): Prepare a 'native' OMAGIC binary's exec package
    177   1.2  deraadt  */
    178   1.2  deraadt 
    179   1.2  deraadt int
    180  1.19  thorpej exec_aout_prep_omagic(struct proc *p, struct exec_package *epp)
    181   1.2  deraadt {
    182   1.8      cgd 	struct exec *execp = epp->ep_hdr;
    183  1.13       pk 	long dsize, bsize, baddr;
    184   1.2  deraadt 
    185   1.2  deraadt 	epp->ep_taddr = USRTEXT;
    186   1.2  deraadt 	epp->ep_tsize = execp->a_text;
    187   1.2  deraadt 	epp->ep_daddr = epp->ep_taddr + execp->a_text;
    188   1.2  deraadt 	epp->ep_dsize = execp->a_data + execp->a_bss;
    189   1.2  deraadt 	epp->ep_entry = execp->a_entry;
    190   1.2  deraadt 
    191   1.2  deraadt 	/* set up command for text and data segments */
    192   1.3      cgd 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn,
    193   1.3      cgd 	    execp->a_text + execp->a_data, epp->ep_taddr, epp->ep_vp,
    194   1.3      cgd 	    sizeof(struct exec), VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
    195   1.2  deraadt 
    196   1.2  deraadt 	/* set up command for bss segment */
    197  1.20  thorpej 	baddr = round_page(epp->ep_daddr + execp->a_data);
    198   1.2  deraadt 	bsize = epp->ep_daddr + epp->ep_dsize - baddr;
    199   1.3      cgd 	if (bsize > 0)
    200   1.3      cgd 		NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, bsize, baddr,
    201   1.5  mycroft 		    NULLVP, 0, VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
    202   1.2  deraadt 
    203  1.13       pk 	/*
    204  1.13       pk 	 * Make sure (# of pages) mapped above equals (vm_tsize + vm_dsize);
    205  1.13       pk 	 * obreak(2) relies on this fact. Both `vm_tsize' and `vm_dsize' are
    206  1.13       pk 	 * computed (in execve(2)) by rounding *up* `ep_tsize' and `ep_dsize'
    207  1.13       pk 	 * respectively to page boundaries.
    208  1.13       pk 	 * Compensate `ep_dsize' for the amount of data covered by the last
    209  1.13       pk 	 * text page.
    210  1.13       pk 	 */
    211  1.20  thorpej 	dsize = epp->ep_dsize + execp->a_text - round_page(execp->a_text);
    212  1.13       pk 	epp->ep_dsize = (dsize > 0) ? dsize : 0;
    213   1.3      cgd 	return exec_aout_setup_stack(p, epp);
    214   1.2  deraadt }
    215   1.2  deraadt 
    216   1.2  deraadt /*
    217   1.2  deraadt  * exec_aout_setup_stack(): Set up the stack segment for an a.out
    218   1.2  deraadt  * executable.
    219   1.2  deraadt  *
    220   1.2  deraadt  * Note that the ep_ssize parameter must be set to be the current stack
    221   1.2  deraadt  * limit; this is adjusted in the body of execve() to yield the
    222   1.2  deraadt  * appropriate stack segment usage once the argument length is
    223   1.2  deraadt  * calculated.
    224   1.3      cgd  *
    225   1.3      cgd  * This function returns an int for uniformity with other (future) formats'
    226   1.3      cgd  * stack setup functions.  They might have errors to return.
    227   1.2  deraadt  */
    228   1.2  deraadt 
    229   1.2  deraadt int
    230  1.19  thorpej exec_aout_setup_stack(struct proc *p, struct exec_package *epp)
    231   1.2  deraadt {
    232   1.2  deraadt 
    233   1.2  deraadt 	epp->ep_maxsaddr = USRSTACK - MAXSSIZ;
    234   1.2  deraadt 	epp->ep_minsaddr = USRSTACK;
    235   1.2  deraadt 	epp->ep_ssize = p->p_rlimit[RLIMIT_STACK].rlim_cur;
    236   1.1      cgd 
    237   1.1      cgd 	/*
    238   1.1      cgd 	 * set up commands for stack.  note that this takes *two*, one to
    239   1.1      cgd 	 * map the part of the stack which we can access, and one to map
    240   1.1      cgd 	 * the part which we can't.
    241   1.1      cgd 	 *
    242   1.1      cgd 	 * arguably, it could be made into one, but that would require the
    243   1.1      cgd 	 * addition of another mapping proc, which is unnecessary
    244   1.1      cgd 	 *
    245  1.15      cgd 	 * note that in memory, things assumed to be: 0 ... ep_maxsaddr
    246   1.1      cgd 	 * <stack> ep_minsaddr
    247   1.1      cgd 	 */
    248   1.3      cgd 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero,
    249   1.1      cgd 	    ((epp->ep_minsaddr - epp->ep_ssize) - epp->ep_maxsaddr),
    250   1.5  mycroft 	    epp->ep_maxsaddr, NULLVP, 0, VM_PROT_NONE);
    251   1.3      cgd 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, epp->ep_ssize,
    252   1.5  mycroft 	    (epp->ep_minsaddr - epp->ep_ssize), NULLVP, 0,
    253   1.3      cgd 	    VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
    254   1.1      cgd 
    255   1.1      cgd 	return 0;
    256   1.1      cgd }
    257