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