Home | History | Annotate | Line # | Download | only in common
compat_exec.c revision 1.1.26.1
      1 /*	$NetBSD: compat_exec.c,v 1.1.26.1 1999/07/04 01:33:34 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_prep_oldzmagic():
     44  *	Prepare the vmcmds to build a vmspace for an old ZMAGIC
     45  *	binary. [386BSD/BSDI/4.4BSD/NetBSD0.8]
     46  *
     47  * Cloned from exec_aout_prep_zmagic() in kern/exec_aout.c; a more verbose
     48  * description of operation is there.
     49  * There were copies of this in the mac68k, hp300, and i386 ports.
     50  */
     51 int
     52 exec_aout_prep_oldzmagic(p, epp)
     53 	struct proc *p;
     54 	struct exec_package *epp;
     55 {
     56 	struct exec *execp = epp->ep_hdr;
     57 
     58 	epp->ep_taddr = 0;
     59 	epp->ep_tsize = execp->a_text;
     60 	epp->ep_daddr = epp->ep_taddr + execp->a_text;
     61 	epp->ep_dsize = execp->a_data + execp->a_bss;
     62 	epp->ep_entry = execp->a_entry;
     63 
     64 	/*
     65 	 * check if vnode is in open for writing, because we want to
     66 	 * demand-page out of it.  if it is, don't do it, for various
     67 	 * reasons
     68 	 */
     69 	if ((execp->a_text != 0 || execp->a_data != 0) &&
     70 	    epp->ep_vp->v_writecount != 0) {
     71 #ifdef DIAGNOSTIC
     72 		if (epp->ep_vp->v_flag & VTEXT)
     73 			panic("exec: a VTEXT vnode has writecount != 0\n");
     74 #endif
     75 		return ETXTBSY;
     76 	}
     77 
     78 	/*
     79 	 * mark this vnode as being the image of a running process.
     80 	 * also flush any cached file mappings now so the process will
     81 	 * have a better chance of being able to use the cache.
     82 	 */
     83 
     84 	epp->ep_vp->v_flag |= VTEXT;
     85 	ubc_flush(&epp->ep_vp->v_uvm.u_obj, 0, 0);
     86 
     87 	/* set up command for text segment */
     88 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_pagedvn, execp->a_text,
     89 	    epp->ep_taddr, epp->ep_vp, NBPG, /* XXX should NBPG be CLBYTES? */
     90 	    VM_PROT_READ|VM_PROT_EXECUTE);
     91 
     92 	/* set up command for data segment */
     93 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_pagedvn, execp->a_data,
     94 	    epp->ep_daddr, epp->ep_vp,
     95 	    execp->a_text + NBPG, /* XXX should NBPG be CLBYTES? */
     96 	    VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
     97 
     98 	/* set up command for bss segment */
     99 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, execp->a_bss,
    100 	    epp->ep_daddr + execp->a_data, NULLVP, 0,
    101 	    VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
    102 
    103 	return exec_aout_setup_stack(p, epp);
    104 }
    105 
    106 
    107 /*
    108  * exec_aout_prep_oldnmagic():
    109  *	Prepare the vmcmds to build a vmspace for an old NMAGIC
    110  *	binary. [BSDI]
    111  *
    112  * Cloned from exec_aout_prep_nmagic() in kern/exec_aout.c; with text starting
    113  * at 0.
    114  * XXX: There must be a better way to share this code.
    115  */
    116 int
    117 exec_aout_prep_oldnmagic(p, epp)
    118 	struct proc *p;
    119 	struct exec_package *epp;
    120 {
    121 	struct exec *execp = epp->ep_hdr;
    122 	long bsize, baddr;
    123 
    124 	epp->ep_taddr = 0;
    125 	epp->ep_tsize = execp->a_text;
    126 	epp->ep_daddr = roundup(epp->ep_taddr + execp->a_text, __LDPGSZ);
    127 	epp->ep_dsize = execp->a_data + execp->a_bss;
    128 	epp->ep_entry = execp->a_entry;
    129 
    130 	/* set up command for text segment */
    131 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, execp->a_text,
    132 	    epp->ep_taddr, epp->ep_vp, sizeof(struct exec),
    133 	    VM_PROT_READ|VM_PROT_EXECUTE);
    134 
    135 	/* set up command for data segment */
    136 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, execp->a_data,
    137 	    epp->ep_daddr, epp->ep_vp, execp->a_text + sizeof(struct exec),
    138 	    VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
    139 
    140 	/* set up command for bss segment */
    141 	baddr = roundup(epp->ep_daddr + execp->a_data, NBPG);
    142 	bsize = epp->ep_daddr + epp->ep_dsize - baddr;
    143 	if (bsize > 0)
    144 		NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, bsize, baddr,
    145 		    NULLVP, 0, VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
    146 
    147 	return exec_aout_setup_stack(p, epp);
    148 }
    149 
    150 
    151 /*
    152  * exec_aout_prep_oldomagic():
    153  *	Prepare the vmcmds to build a vmspace for an old OMAGIC
    154  *	binary. [BSDI]
    155  *
    156  * Cloned from exec_aout_prep_omagic() in kern/exec_aout.c; with text starting
    157  * at 0.
    158  * XXX: There must be a better way to share this code.
    159  */
    160 int
    161 exec_aout_prep_oldomagic(p, epp)
    162 	struct proc *p;
    163 	struct exec_package *epp;
    164 {
    165 	struct exec *execp = epp->ep_hdr;
    166 	long dsize, bsize, baddr;
    167 
    168 	epp->ep_taddr = 0;
    169 	epp->ep_tsize = execp->a_text;
    170 	epp->ep_daddr = epp->ep_taddr + execp->a_text;
    171 	epp->ep_dsize = execp->a_data + execp->a_bss;
    172 	epp->ep_entry = execp->a_entry;
    173 
    174 	/* set up command for text and data segments */
    175 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn,
    176 	    execp->a_text + execp->a_data, epp->ep_taddr, epp->ep_vp,
    177 	    sizeof(struct exec), VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
    178 
    179 	/* set up command for bss segment */
    180 	baddr = roundup(epp->ep_daddr + execp->a_data, NBPG);
    181 	bsize = epp->ep_daddr + epp->ep_dsize - baddr;
    182 	if (bsize > 0)
    183 		NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, bsize, baddr,
    184 		    NULLVP, 0, VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
    185 
    186 	/*
    187 	 * Make sure (# of pages) mapped above equals (vm_tsize + vm_dsize);
    188 	 * obreak(2) relies on this fact. Both `vm_tsize' and `vm_dsize' are
    189 	 * computed (in execve(2)) by rounding *up* `ep_tsize' and `ep_dsize'
    190 	 * respectively to page boundaries.
    191 	 * Compensate `ep_dsize' for the amount of data covered by the last
    192 	 * text page.
    193 	 */
    194 	dsize = epp->ep_dsize + execp->a_text - roundup(execp->a_text, NBPG);
    195 	epp->ep_dsize = (dsize > 0) ? dsize : 0;
    196 	return exec_aout_setup_stack(p, epp);
    197 }
    198