Home | History | Annotate | Line # | Download | only in common
compat_exec.c revision 1.5
      1  1.5     lukem /*	$NetBSD: compat_exec.c,v 1.5 2001/11/13 02:07:59 lukem Exp $	*/
      2  1.1  christos 
      3  1.1  christos /*
      4  1.1  christos  * Copyright (c) 1993, 1994 Christopher G. Demetriou
      5  1.1  christos  * All rights reserved.
      6  1.1  christos  *
      7  1.1  christos  * Redistribution and use in source and binary forms, with or without
      8  1.1  christos  * modification, are permitted provided that the following conditions
      9  1.1  christos  * are met:
     10  1.1  christos  * 1. Redistributions of source code must retain the above copyright
     11  1.1  christos  *    notice, this list of conditions and the following disclaimer.
     12  1.1  christos  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1  christos  *    notice, this list of conditions and the following disclaimer in the
     14  1.1  christos  *    documentation and/or other materials provided with the distribution.
     15  1.1  christos  * 3. All advertising materials mentioning features or use of this software
     16  1.1  christos  *    must display the following acknowledgement:
     17  1.1  christos  *      This product includes software developed by Christopher G. Demetriou.
     18  1.1  christos  * 4. The name of the author may not be used to endorse or promote products
     19  1.1  christos  *    derived from this software without specific prior written permission
     20  1.1  christos  *
     21  1.1  christos  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     22  1.1  christos  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     23  1.1  christos  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     24  1.1  christos  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     25  1.1  christos  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     26  1.1  christos  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     27  1.1  christos  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     28  1.1  christos  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     29  1.1  christos  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     30  1.1  christos  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     31  1.1  christos  */
     32  1.5     lukem 
     33  1.5     lukem #include <sys/cdefs.h>
     34  1.5     lukem __KERNEL_RCSID(0, "$NetBSD: compat_exec.c,v 1.5 2001/11/13 02:07:59 lukem Exp $");
     35  1.1  christos 
     36  1.1  christos #include <sys/param.h>
     37  1.1  christos #include <sys/systm.h>
     38  1.1  christos #include <sys/proc.h>
     39  1.1  christos #include <sys/malloc.h>
     40  1.1  christos #include <sys/vnode.h>
     41  1.1  christos #include <sys/exec.h>
     42  1.1  christos #include <sys/resourcevar.h>
     43  1.1  christos 
     44  1.1  christos /*
     45  1.1  christos  * exec_aout_prep_oldzmagic():
     46  1.1  christos  *	Prepare the vmcmds to build a vmspace for an old ZMAGIC
     47  1.1  christos  *	binary. [386BSD/BSDI/4.4BSD/NetBSD0.8]
     48  1.1  christos  *
     49  1.1  christos  * Cloned from exec_aout_prep_zmagic() in kern/exec_aout.c; a more verbose
     50  1.1  christos  * description of operation is there.
     51  1.1  christos  * There were copies of this in the mac68k, hp300, and i386 ports.
     52  1.1  christos  */
     53  1.1  christos int
     54  1.1  christos exec_aout_prep_oldzmagic(p, epp)
     55  1.1  christos 	struct proc *p;
     56  1.1  christos 	struct exec_package *epp;
     57  1.1  christos {
     58  1.1  christos 	struct exec *execp = epp->ep_hdr;
     59  1.1  christos 
     60  1.1  christos 	epp->ep_taddr = 0;
     61  1.1  christos 	epp->ep_tsize = execp->a_text;
     62  1.1  christos 	epp->ep_daddr = epp->ep_taddr + execp->a_text;
     63  1.1  christos 	epp->ep_dsize = execp->a_data + execp->a_bss;
     64  1.1  christos 	epp->ep_entry = execp->a_entry;
     65  1.1  christos 
     66  1.1  christos 	/*
     67  1.1  christos 	 * check if vnode is in open for writing, because we want to
     68  1.1  christos 	 * demand-page out of it.  if it is, don't do it, for various
     69  1.1  christos 	 * reasons
     70  1.1  christos 	 */
     71  1.1  christos 	if ((execp->a_text != 0 || execp->a_data != 0) &&
     72  1.1  christos 	    epp->ep_vp->v_writecount != 0) {
     73  1.1  christos #ifdef DIAGNOSTIC
     74  1.1  christos 		if (epp->ep_vp->v_flag & VTEXT)
     75  1.1  christos 			panic("exec: a VTEXT vnode has writecount != 0\n");
     76  1.1  christos #endif
     77  1.1  christos 		return ETXTBSY;
     78  1.1  christos 	}
     79  1.4   thorpej 	epp->ep_vp->v_flag |= VTEXT;
     80  1.1  christos 
     81  1.1  christos 	/* set up command for text segment */
     82  1.1  christos 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_pagedvn, execp->a_text,
     83  1.1  christos 	    epp->ep_taddr, epp->ep_vp, NBPG, /* XXX should NBPG be CLBYTES? */
     84  1.1  christos 	    VM_PROT_READ|VM_PROT_EXECUTE);
     85  1.1  christos 
     86  1.1  christos 	/* set up command for data segment */
     87  1.1  christos 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_pagedvn, execp->a_data,
     88  1.1  christos 	    epp->ep_daddr, epp->ep_vp,
     89  1.1  christos 	    execp->a_text + NBPG, /* XXX should NBPG be CLBYTES? */
     90  1.1  christos 	    VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
     91  1.1  christos 
     92  1.1  christos 	/* set up command for bss segment */
     93  1.1  christos 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, execp->a_bss,
     94  1.1  christos 	    epp->ep_daddr + execp->a_data, NULLVP, 0,
     95  1.1  christos 	    VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
     96  1.1  christos 
     97  1.1  christos 	return exec_aout_setup_stack(p, epp);
     98  1.1  christos }
     99  1.1  christos 
    100  1.1  christos 
    101  1.1  christos /*
    102  1.1  christos  * exec_aout_prep_oldnmagic():
    103  1.1  christos  *	Prepare the vmcmds to build a vmspace for an old NMAGIC
    104  1.1  christos  *	binary. [BSDI]
    105  1.1  christos  *
    106  1.1  christos  * Cloned from exec_aout_prep_nmagic() in kern/exec_aout.c; with text starting
    107  1.1  christos  * at 0.
    108  1.1  christos  * XXX: There must be a better way to share this code.
    109  1.1  christos  */
    110  1.1  christos int
    111  1.1  christos exec_aout_prep_oldnmagic(p, epp)
    112  1.1  christos 	struct proc *p;
    113  1.1  christos 	struct exec_package *epp;
    114  1.1  christos {
    115  1.1  christos 	struct exec *execp = epp->ep_hdr;
    116  1.1  christos 	long bsize, baddr;
    117  1.1  christos 
    118  1.1  christos 	epp->ep_taddr = 0;
    119  1.1  christos 	epp->ep_tsize = execp->a_text;
    120  1.1  christos 	epp->ep_daddr = roundup(epp->ep_taddr + execp->a_text, __LDPGSZ);
    121  1.1  christos 	epp->ep_dsize = execp->a_data + execp->a_bss;
    122  1.1  christos 	epp->ep_entry = execp->a_entry;
    123  1.1  christos 
    124  1.1  christos 	/* set up command for text segment */
    125  1.1  christos 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, execp->a_text,
    126  1.1  christos 	    epp->ep_taddr, epp->ep_vp, sizeof(struct exec),
    127  1.1  christos 	    VM_PROT_READ|VM_PROT_EXECUTE);
    128  1.1  christos 
    129  1.1  christos 	/* set up command for data segment */
    130  1.1  christos 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, execp->a_data,
    131  1.1  christos 	    epp->ep_daddr, epp->ep_vp, execp->a_text + sizeof(struct exec),
    132  1.1  christos 	    VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
    133  1.1  christos 
    134  1.1  christos 	/* set up command for bss segment */
    135  1.1  christos 	baddr = roundup(epp->ep_daddr + execp->a_data, NBPG);
    136  1.1  christos 	bsize = epp->ep_daddr + epp->ep_dsize - baddr;
    137  1.1  christos 	if (bsize > 0)
    138  1.1  christos 		NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, bsize, baddr,
    139  1.1  christos 		    NULLVP, 0, VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
    140  1.1  christos 
    141  1.1  christos 	return exec_aout_setup_stack(p, epp);
    142  1.1  christos }
    143  1.1  christos 
    144  1.1  christos 
    145  1.1  christos /*
    146  1.1  christos  * exec_aout_prep_oldomagic():
    147  1.1  christos  *	Prepare the vmcmds to build a vmspace for an old OMAGIC
    148  1.1  christos  *	binary. [BSDI]
    149  1.1  christos  *
    150  1.1  christos  * Cloned from exec_aout_prep_omagic() in kern/exec_aout.c; with text starting
    151  1.1  christos  * at 0.
    152  1.1  christos  * XXX: There must be a better way to share this code.
    153  1.1  christos  */
    154  1.1  christos int
    155  1.1  christos exec_aout_prep_oldomagic(p, epp)
    156  1.1  christos 	struct proc *p;
    157  1.1  christos 	struct exec_package *epp;
    158  1.1  christos {
    159  1.1  christos 	struct exec *execp = epp->ep_hdr;
    160  1.1  christos 	long dsize, bsize, baddr;
    161  1.1  christos 
    162  1.1  christos 	epp->ep_taddr = 0;
    163  1.1  christos 	epp->ep_tsize = execp->a_text;
    164  1.1  christos 	epp->ep_daddr = epp->ep_taddr + execp->a_text;
    165  1.1  christos 	epp->ep_dsize = execp->a_data + execp->a_bss;
    166  1.1  christos 	epp->ep_entry = execp->a_entry;
    167  1.1  christos 
    168  1.1  christos 	/* set up command for text and data segments */
    169  1.1  christos 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn,
    170  1.1  christos 	    execp->a_text + execp->a_data, epp->ep_taddr, epp->ep_vp,
    171  1.1  christos 	    sizeof(struct exec), VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
    172  1.1  christos 
    173  1.1  christos 	/* set up command for bss segment */
    174  1.1  christos 	baddr = roundup(epp->ep_daddr + execp->a_data, NBPG);
    175  1.1  christos 	bsize = epp->ep_daddr + epp->ep_dsize - baddr;
    176  1.1  christos 	if (bsize > 0)
    177  1.1  christos 		NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, bsize, baddr,
    178  1.1  christos 		    NULLVP, 0, VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
    179  1.1  christos 
    180  1.1  christos 	/*
    181  1.1  christos 	 * Make sure (# of pages) mapped above equals (vm_tsize + vm_dsize);
    182  1.1  christos 	 * obreak(2) relies on this fact. Both `vm_tsize' and `vm_dsize' are
    183  1.1  christos 	 * computed (in execve(2)) by rounding *up* `ep_tsize' and `ep_dsize'
    184  1.1  christos 	 * respectively to page boundaries.
    185  1.1  christos 	 * Compensate `ep_dsize' for the amount of data covered by the last
    186  1.1  christos 	 * text page.
    187  1.1  christos 	 */
    188  1.1  christos 	dsize = epp->ep_dsize + execp->a_text - roundup(execp->a_text, NBPG);
    189  1.1  christos 	epp->ep_dsize = (dsize > 0) ? dsize : 0;
    190  1.1  christos 	return exec_aout_setup_stack(p, epp);
    191  1.1  christos }
    192