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