Home | History | Annotate | Line # | Download | only in common
compat_exec.c revision 1.15
      1 /*	$NetBSD: compat_exec.c,v 1.15 2007/12/08 18:35:53 dsl 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.15 2007/12/08 18:35:53 dsl Exp $");
     35 
     36 #include <sys/param.h>
     37 #include <sys/systm.h>
     38 #include <sys/proc.h>
     39 #include <sys/vnode.h>
     40 #include <sys/exec.h>
     41 #include <sys/resourcevar.h>
     42 
     43 /*
     44  * exec_aout_prep_oldzmagic():
     45  *	Prepare the vmcmds to build a vmspace for an old ZMAGIC
     46  *	binary. [386BSD/BSDI/4.4BSD/NetBSD0.8]
     47  *
     48  * Cloned from exec_aout_prep_zmagic() in kern/exec_aout.c; a more verbose
     49  * description of operation is there.
     50  * There were copies of this in the mac68k, hp300, and i386 ports.
     51  */
     52 int
     53 exec_aout_prep_oldzmagic(struct lwp *l, struct exec_package *epp)
     54 {
     55 	struct exec *execp = epp->ep_hdr;
     56 	int error;
     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 	error = vn_marktext(epp->ep_vp);
     65 	if (error)
     66 		return (error);
     67 
     68 	/* set up command for text segment */
     69 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_pagedvn, execp->a_text,
     70 	    epp->ep_taddr, epp->ep_vp, PAGE_SIZE, /* XXX CLBYTES? */
     71 	    VM_PROT_READ|VM_PROT_EXECUTE);
     72 
     73 	/* set up command for data segment */
     74 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_pagedvn, execp->a_data,
     75 	    epp->ep_daddr, epp->ep_vp,
     76 	    execp->a_text + PAGE_SIZE, /* XXX CLBYTES? */
     77 	    VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
     78 
     79 	/* set up command for bss segment */
     80 	if (execp->a_bss)
     81 	    NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, execp->a_bss,
     82 		epp->ep_daddr + execp->a_data, NULLVP, 0,
     83 		VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
     84 
     85 	return (*epp->ep_esch->es_setup_stack)(l, epp);
     86 }
     87 
     88 
     89 /*
     90  * exec_aout_prep_oldnmagic():
     91  *	Prepare the vmcmds to build a vmspace for an old NMAGIC
     92  *	binary. [BSDI]
     93  *
     94  * Cloned from exec_aout_prep_nmagic() in kern/exec_aout.c; with text starting
     95  * at 0.
     96  * XXX: There must be a better way to share this code.
     97  */
     98 int
     99 exec_aout_prep_oldnmagic(struct lwp *l, struct exec_package *epp)
    100 {
    101 	struct exec *execp = epp->ep_hdr;
    102 	long bsize, baddr;
    103 
    104 	epp->ep_taddr = 0;
    105 	epp->ep_tsize = execp->a_text;
    106 	epp->ep_daddr = roundup(epp->ep_taddr + execp->a_text, AOUT_LDPGSZ);
    107 	epp->ep_dsize = execp->a_data + execp->a_bss;
    108 	epp->ep_entry = execp->a_entry;
    109 
    110 	/* set up command for text segment */
    111 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, execp->a_text,
    112 	    epp->ep_taddr, epp->ep_vp, sizeof(struct exec),
    113 	    VM_PROT_READ|VM_PROT_EXECUTE);
    114 
    115 	/* set up command for data segment */
    116 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, execp->a_data,
    117 	    epp->ep_daddr, epp->ep_vp, execp->a_text + sizeof(struct exec),
    118 	    VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
    119 
    120 	/* set up command for bss segment */
    121 	baddr = roundup(epp->ep_daddr + execp->a_data, PAGE_SIZE);
    122 	bsize = epp->ep_daddr + epp->ep_dsize - baddr;
    123 	if (bsize > 0)
    124 		NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, bsize, baddr,
    125 		    NULLVP, 0, VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
    126 
    127 	return (*epp->ep_esch->es_setup_stack)(l, epp);
    128 }
    129 
    130 
    131 /*
    132  * exec_aout_prep_oldomagic():
    133  *	Prepare the vmcmds to build a vmspace for an old OMAGIC
    134  *	binary. [BSDI]
    135  *
    136  * Cloned from exec_aout_prep_omagic() in kern/exec_aout.c; with text starting
    137  * at 0.
    138  * XXX: There must be a better way to share this code.
    139  */
    140 int
    141 exec_aout_prep_oldomagic(struct lwp *l, struct exec_package *epp)
    142 {
    143 	struct exec *execp = epp->ep_hdr;
    144 	long dsize, bsize, baddr;
    145 
    146 	epp->ep_taddr = 0;
    147 	epp->ep_tsize = execp->a_text;
    148 	epp->ep_daddr = epp->ep_taddr + execp->a_text;
    149 	epp->ep_dsize = execp->a_data + execp->a_bss;
    150 	epp->ep_entry = execp->a_entry;
    151 
    152 	/* set up command for text and data segments */
    153 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn,
    154 	    execp->a_text + execp->a_data, epp->ep_taddr, epp->ep_vp,
    155 	    sizeof(struct exec), VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
    156 
    157 	/* set up command for bss segment */
    158 	baddr = roundup(epp->ep_daddr + execp->a_data, PAGE_SIZE);
    159 	bsize = epp->ep_daddr + epp->ep_dsize - baddr;
    160 	if (bsize > 0)
    161 		NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, bsize, baddr,
    162 		    NULLVP, 0, VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
    163 
    164 	/*
    165 	 * Make sure (# of pages) mapped above equals (vm_tsize + vm_dsize);
    166 	 * obreak(2) relies on this fact. Both `vm_tsize' and `vm_dsize' are
    167 	 * computed (in execve(2)) by rounding *up* `ep_tsize' and `ep_dsize'
    168 	 * respectively to page boundaries.
    169 	 * Compensate `ep_dsize' for the amount of data covered by the last
    170 	 * text page.
    171 	 */
    172 	dsize = epp->ep_dsize + execp->a_text - roundup(execp->a_text,
    173 							PAGE_SIZE);
    174 	epp->ep_dsize = (dsize > 0) ? dsize : 0;
    175 	return (*epp->ep_esch->es_setup_stack)(l, epp);
    176 }
    177