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