Home | History | Annotate | Line # | Download | only in netbsd32
netbsd32_exec_aout.c revision 1.21.44.1
      1 /*	$NetBSD: netbsd32_exec_aout.c,v 1.21.44.1 2007/12/09 19:37:19 jmcneill Exp $	*/
      2 /*	from: NetBSD: exec_aout.c,v 1.15 1996/09/26 23:34:46 cgd Exp */
      3 
      4 /*
      5  * Copyright (c) 1998, 2001 Matthew R. Green.
      6  * All rights reserved.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in the
     15  *    documentation and/or other materials provided with the distribution.
     16  * 3. The name of the author may not be used to endorse or promote products
     17  *    derived from this software without specific prior written permission
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     20  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     21  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     22  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     23  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     24  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     28  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     29  */
     30 
     31 /*
     32  * Copyright (c) 1993, 1994 Christopher G. Demetriou
     33  * All rights reserved.
     34  *
     35  * Redistribution and use in source and binary forms, with or without
     36  * modification, are permitted provided that the following conditions
     37  * are met:
     38  * 1. Redistributions of source code must retain the above copyright
     39  *    notice, this list of conditions and the following disclaimer.
     40  * 2. Redistributions in binary form must reproduce the above copyright
     41  *    notice, this list of conditions and the following disclaimer in the
     42  *    documentation and/or other materials provided with the distribution.
     43  * 3. All advertising materials mentioning features or use of this software
     44  *    must display the following acknowledgement:
     45  *      This product includes software developed by Christopher G. Demetriou.
     46  * 4. The name of the author may not be used to endorse or promote products
     47  *    derived from this software without specific prior written permission
     48  *
     49  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     50  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     51  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     52  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     53  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     54  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     55  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     56  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     57  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     58  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     59  */
     60 
     61 #include <sys/cdefs.h>
     62 __KERNEL_RCSID(0, "$NetBSD: netbsd32_exec_aout.c,v 1.21.44.1 2007/12/09 19:37:19 jmcneill Exp $");
     63 
     64 #include <sys/param.h>
     65 #include <sys/systm.h>
     66 #include <sys/proc.h>
     67 #include <sys/malloc.h>
     68 #include <sys/vnode.h>
     69 #include <sys/exec.h>
     70 #include <sys/exec_aout.h>
     71 #include <sys/resourcevar.h>
     72 #include <sys/signal.h>
     73 #include <sys/signalvar.h>
     74 
     75 #include <compat/netbsd32/netbsd32.h>
     76 #ifndef EXEC_AOUT
     77 #define EXEC_AOUT
     78 #endif
     79 #include <compat/netbsd32/netbsd32_exec.h>
     80 
     81 #include <machine/frame.h>
     82 #include <machine/netbsd32_machdep.h>
     83 
     84 int netbsd32_copyinargs(struct exec_package *, struct ps_strings *,
     85 			     void *, size_t, const void *, const void *);
     86 
     87 /*
     88  * exec_netbsd32_makecmds(): Check if it's an netbsd32 a.out format
     89  * executable.
     90  *
     91  * Given a lwp pointer and an exec package pointer, see if the referent
     92  * of the epp is in netbsd32 a.out format.  Check 'standard' magic
     93  * numbers for this architecture.
     94  *
     95  * This function, in the former case, or the hook, in the latter, is
     96  * responsible for creating a set of vmcmds which can be used to build
     97  * the process's vm space and inserting them into the exec package.
     98  */
     99 
    100 int
    101 exec_netbsd32_makecmds(struct lwp *l, struct exec_package *epp)
    102 {
    103 	netbsd32_u_long midmag, magic;
    104 	u_short mid;
    105 	int error;
    106 	struct netbsd32_exec *execp = epp->ep_hdr;
    107 
    108 	if (epp->ep_hdrvalid < sizeof(struct netbsd32_exec))
    109 		return ENOEXEC;
    110 
    111 	midmag = (netbsd32_u_long)ntohl(execp->a_midmag);
    112 	mid = (midmag >> 16) & 0x3ff;
    113 	magic = midmag & 0xffff;
    114 
    115 	midmag = mid << 16 | magic;
    116 
    117 	/* this is already needed by setup_stack() */
    118 	epp->ep_flags |= EXEC_32;
    119 
    120 	switch (midmag) {
    121 	case (NETBSD32_MID_MACHINE << 16) | ZMAGIC:
    122 		error = netbsd32_exec_aout_prep_zmagic(l, epp);
    123 		break;
    124 	case (NETBSD32_MID_MACHINE << 16) | NMAGIC:
    125 		error = netbsd32_exec_aout_prep_nmagic(l, epp);
    126 		break;
    127 	case (NETBSD32_MID_MACHINE << 16) | OMAGIC:
    128 		error = netbsd32_exec_aout_prep_omagic(l, epp);
    129 		break;
    130 	default:
    131 		/* Invalid magic */
    132 		error = ENOEXEC;
    133 		break;
    134 	}
    135 
    136 	if (error) {
    137 		kill_vmcmds(&epp->ep_vmcmds);
    138 		epp->ep_flags &= ~EXEC_32;
    139 	}
    140 	return error;
    141 }
    142 
    143 /*
    144  * netbsd32_exec_aout_prep_zmagic(): Prepare a 'native' ZMAGIC binary's
    145  * exec package
    146  *
    147  * First, set of the various offsets/lengths in the exec package.
    148  *
    149  * Then, mark the text image busy (so it can be demand paged) or error
    150  * out if this is not possible.  Finally, set up vmcmds for the
    151  * text, data, bss, and stack segments.
    152  */
    153 
    154 int
    155 netbsd32_exec_aout_prep_zmagic(struct lwp *l, struct exec_package *epp)
    156 {
    157 	struct netbsd32_exec *execp = epp->ep_hdr;
    158 	int error;
    159 
    160 	epp->ep_taddr = AOUT_LDPGSZ;
    161 	epp->ep_tsize = execp->a_text;
    162 	epp->ep_daddr = epp->ep_taddr + execp->a_text;
    163 	epp->ep_dsize = execp->a_data + execp->a_bss;
    164 	epp->ep_entry = execp->a_entry;
    165 	epp->ep_vm_minaddr = VM_MIN_ADDRESS;
    166 	epp->ep_vm_maxaddr = VM_MAXUSER_ADDRESS32;
    167 
    168 	error = vn_marktext(epp->ep_vp);
    169 	if (error)
    170 		return (error);
    171 
    172 	/* set up command for text segment */
    173 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_pagedvn, execp->a_text,
    174 	    epp->ep_taddr, epp->ep_vp, 0, VM_PROT_READ|VM_PROT_EXECUTE);
    175 
    176 	/* set up command for data segment */
    177 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_pagedvn, execp->a_data,
    178 	    epp->ep_daddr, epp->ep_vp, execp->a_text,
    179 	    VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
    180 
    181 	/* set up command for bss segment */
    182 	if (execp->a_bss > 0)
    183 		NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, execp->a_bss,
    184 		    epp->ep_daddr + execp->a_data, NULLVP, 0,
    185 		    VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
    186 
    187 	return (*epp->ep_esch->es_setup_stack)(l, epp);
    188 }
    189 
    190 /*
    191  * netbsd32_exec_aout_prep_nmagic(): Prepare a 'native' NMAGIC binary's
    192  * exec package
    193  */
    194 
    195 int
    196 netbsd32_exec_aout_prep_nmagic(struct lwp *l, struct exec_package *epp)
    197 {
    198 	struct netbsd32_exec *execp = epp->ep_hdr;
    199 	long bsize, baddr;
    200 
    201 	epp->ep_taddr = AOUT_LDPGSZ;
    202 	epp->ep_tsize = execp->a_text;
    203 	epp->ep_daddr = roundup(epp->ep_taddr + execp->a_text, AOUT_LDPGSZ);
    204 	epp->ep_dsize = execp->a_data + execp->a_bss;
    205 	epp->ep_entry = execp->a_entry;
    206 	epp->ep_vm_minaddr = VM_MIN_ADDRESS;
    207 	epp->ep_vm_maxaddr = VM_MAXUSER_ADDRESS32;
    208 
    209 	/* set up command for text segment */
    210 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, execp->a_text,
    211 	    epp->ep_taddr, epp->ep_vp, sizeof(struct netbsd32_exec),
    212 	    VM_PROT_READ|VM_PROT_EXECUTE);
    213 
    214 	/* set up command for data segment */
    215 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, execp->a_data,
    216 	    epp->ep_daddr, epp->ep_vp, execp->a_text + sizeof(struct netbsd32_exec),
    217 	    VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
    218 
    219 	/* set up command for bss segment */
    220 	baddr = roundup(epp->ep_daddr + execp->a_data, PAGE_SIZE);
    221 	bsize = epp->ep_daddr + epp->ep_dsize - baddr;
    222 	if (bsize > 0)
    223 		NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, bsize, baddr,
    224 		    NULLVP, 0, VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
    225 
    226 	return (*epp->ep_esch->es_setup_stack)(l, epp);
    227 }
    228 
    229 /*
    230  * netbsd32_exec_aout_prep_omagic(): Prepare a 'native' OMAGIC binary's
    231  * exec package
    232  */
    233 
    234 int
    235 netbsd32_exec_aout_prep_omagic(struct lwp *l, struct exec_package *epp)
    236 {
    237 	struct netbsd32_exec *execp = epp->ep_hdr;
    238 	long dsize, bsize, baddr;
    239 
    240 	epp->ep_taddr = AOUT_LDPGSZ;
    241 	epp->ep_tsize = execp->a_text;
    242 	epp->ep_daddr = epp->ep_taddr + execp->a_text;
    243 	epp->ep_dsize = execp->a_data + execp->a_bss;
    244 	epp->ep_entry = execp->a_entry;
    245 	epp->ep_vm_minaddr = VM_MIN_ADDRESS;
    246 	epp->ep_vm_maxaddr = VM_MAXUSER_ADDRESS32;
    247 
    248 	/* set up command for text and data segments */
    249 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn,
    250 	    execp->a_text + execp->a_data, epp->ep_taddr, epp->ep_vp,
    251 	    sizeof(struct netbsd32_exec), VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
    252 
    253 	/* set up command for bss segment */
    254 	baddr = roundup(epp->ep_daddr + execp->a_data, PAGE_SIZE);
    255 	bsize = epp->ep_daddr + epp->ep_dsize - baddr;
    256 	if (bsize > 0)
    257 		NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, bsize, baddr,
    258 		    NULLVP, 0, VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
    259 
    260 	/*
    261 	 * Make sure (# of pages) mapped above equals (vm_tsize + vm_dsize);
    262 	 * obreak(2) relies on this fact. Both `vm_tsize' and `vm_dsize' are
    263 	 * computed (in execve(2)) by rounding *up* `ep_tsize' and `ep_dsize'
    264 	 * respectively to page boundaries.
    265 	 * Compensate `ep_dsize' for the amount of data covered by the last
    266 	 * text page.
    267 	 */
    268 	dsize = epp->ep_dsize + execp->a_text - roundup(execp->a_text,
    269 							PAGE_SIZE);
    270 	epp->ep_dsize = (dsize > 0) ? dsize : 0;
    271 	return (*epp->ep_esch->es_setup_stack)(l, epp);
    272 }
    273