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