Home | History | Annotate | Line # | Download | only in netbsd32
netbsd32_exec_aout.c revision 1.30
      1  1.30      maxv /*	$NetBSD: netbsd32_exec_aout.c,v 1.30 2016/08/06 15:13:13 maxv 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.30      maxv __KERNEL_RCSID(0, "$NetBSD: netbsd32_exec_aout.c,v 1.30 2016/08/06 15:13:13 maxv 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.29  christos #ifdef COMPAT_NOMID
     82  1.29  christos static int netbsd32_exec_aout_nomid(struct lwp *, struct exec_package *);
     83  1.29  christos #endif
     84  1.29  christos 
     85   1.1  jdolecek /*
     86   1.1  jdolecek  * exec_netbsd32_makecmds(): Check if it's an netbsd32 a.out format
     87   1.1  jdolecek  * executable.
     88   1.1  jdolecek  *
     89  1.21  christos  * Given a lwp pointer and an exec package pointer, see if the referent
     90   1.1  jdolecek  * of the epp is in netbsd32 a.out format.  Check 'standard' magic
     91   1.1  jdolecek  * numbers for this architecture.
     92   1.1  jdolecek  *
     93   1.1  jdolecek  * This function, in the former case, or the hook, in the latter, is
     94   1.1  jdolecek  * responsible for creating a set of vmcmds which can be used to build
     95   1.1  jdolecek  * the process's vm space and inserting them into the exec package.
     96   1.1  jdolecek  */
     97   1.1  jdolecek 
     98   1.1  jdolecek int
     99  1.23       dsl exec_netbsd32_makecmds(struct lwp *l, struct exec_package *epp)
    100   1.1  jdolecek {
    101   1.1  jdolecek 	netbsd32_u_long midmag, magic;
    102   1.1  jdolecek 	u_short mid;
    103   1.1  jdolecek 	int error;
    104   1.1  jdolecek 	struct netbsd32_exec *execp = epp->ep_hdr;
    105   1.1  jdolecek 
    106   1.1  jdolecek 	if (epp->ep_hdrvalid < sizeof(struct netbsd32_exec))
    107   1.1  jdolecek 		return ENOEXEC;
    108   1.1  jdolecek 
    109   1.1  jdolecek 	midmag = (netbsd32_u_long)ntohl(execp->a_midmag);
    110   1.1  jdolecek 	mid = (midmag >> 16) & 0x3ff;
    111   1.1  jdolecek 	magic = midmag & 0xffff;
    112   1.1  jdolecek 
    113   1.1  jdolecek 	midmag = mid << 16 | magic;
    114   1.1  jdolecek 
    115  1.19  drochner 	/* this is already needed by setup_stack() */
    116  1.19  drochner 	epp->ep_flags |= EXEC_32;
    117  1.19  drochner 
    118   1.1  jdolecek 	switch (midmag) {
    119  1.18  drochner 	case (NETBSD32_MID_MACHINE << 16) | ZMAGIC:
    120  1.21  christos 		error = netbsd32_exec_aout_prep_zmagic(l, epp);
    121   1.1  jdolecek 		break;
    122  1.18  drochner 	case (NETBSD32_MID_MACHINE << 16) | NMAGIC:
    123  1.21  christos 		error = netbsd32_exec_aout_prep_nmagic(l, epp);
    124   1.1  jdolecek 		break;
    125  1.18  drochner 	case (NETBSD32_MID_MACHINE << 16) | OMAGIC:
    126  1.21  christos 		error = netbsd32_exec_aout_prep_omagic(l, epp);
    127   1.1  jdolecek 		break;
    128   1.1  jdolecek 	default:
    129  1.29  christos #ifdef COMPAT_NOMID
    130  1.29  christos 		error = netbsd32_exec_aout_nomid(l,  epp);
    131  1.29  christos #else
    132   1.1  jdolecek 		error = ENOEXEC;
    133  1.29  christos #endif
    134   1.1  jdolecek 		break;
    135   1.1  jdolecek 	}
    136   1.1  jdolecek 
    137  1.19  drochner 	if (error) {
    138   1.1  jdolecek 		kill_vmcmds(&epp->ep_vmcmds);
    139  1.20     perry 		epp->ep_flags &= ~EXEC_32;
    140  1.27  christos 	} else
    141  1.27  christos 		epp->ep_flags &= ~EXEC_TOPDOWN_VM;
    142   1.1  jdolecek 	return error;
    143   1.1  jdolecek }
    144   1.1  jdolecek 
    145   1.1  jdolecek /*
    146   1.1  jdolecek  * netbsd32_exec_aout_prep_zmagic(): Prepare a 'native' ZMAGIC binary's
    147   1.1  jdolecek  * exec package
    148   1.1  jdolecek  *
    149   1.1  jdolecek  * First, set of the various offsets/lengths in the exec package.
    150   1.1  jdolecek  *
    151   1.1  jdolecek  * Then, mark the text image busy (so it can be demand paged) or error
    152   1.1  jdolecek  * out if this is not possible.  Finally, set up vmcmds for the
    153   1.1  jdolecek  * text, data, bss, and stack segments.
    154   1.1  jdolecek  */
    155   1.1  jdolecek 
    156   1.1  jdolecek int
    157  1.23       dsl netbsd32_exec_aout_prep_zmagic(struct lwp *l, struct exec_package *epp)
    158   1.1  jdolecek {
    159   1.1  jdolecek 	struct netbsd32_exec *execp = epp->ep_hdr;
    160  1.10       chs 	int error;
    161   1.1  jdolecek 
    162  1.12   thorpej 	epp->ep_taddr = AOUT_LDPGSZ;
    163   1.1  jdolecek 	epp->ep_tsize = execp->a_text;
    164   1.1  jdolecek 	epp->ep_daddr = epp->ep_taddr + execp->a_text;
    165   1.1  jdolecek 	epp->ep_dsize = execp->a_data + execp->a_bss;
    166   1.1  jdolecek 	epp->ep_entry = execp->a_entry;
    167  1.30      maxv 	epp->ep_vm_minaddr = exec_vm_minaddr(VM_MIN_ADDRESS);
    168   1.5       eeh 	epp->ep_vm_maxaddr = VM_MAXUSER_ADDRESS32;
    169   1.1  jdolecek 
    170  1.10       chs 	error = vn_marktext(epp->ep_vp);
    171  1.10       chs 	if (error)
    172  1.10       chs 		return (error);
    173   1.1  jdolecek 
    174   1.1  jdolecek 	/* set up command for text segment */
    175   1.1  jdolecek 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_pagedvn, execp->a_text,
    176   1.1  jdolecek 	    epp->ep_taddr, epp->ep_vp, 0, VM_PROT_READ|VM_PROT_EXECUTE);
    177   1.1  jdolecek 
    178   1.1  jdolecek 	/* set up command for data segment */
    179   1.1  jdolecek 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_pagedvn, execp->a_data,
    180   1.1  jdolecek 	    epp->ep_daddr, epp->ep_vp, execp->a_text,
    181   1.1  jdolecek 	    VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
    182   1.1  jdolecek 
    183   1.1  jdolecek 	/* set up command for bss segment */
    184  1.18  drochner 	if (execp->a_bss > 0)
    185  1.18  drochner 		NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, execp->a_bss,
    186  1.18  drochner 		    epp->ep_daddr + execp->a_data, NULLVP, 0,
    187  1.18  drochner 		    VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
    188   1.1  jdolecek 
    189  1.21  christos 	return (*epp->ep_esch->es_setup_stack)(l, epp);
    190   1.1  jdolecek }
    191   1.1  jdolecek 
    192   1.1  jdolecek /*
    193   1.1  jdolecek  * netbsd32_exec_aout_prep_nmagic(): Prepare a 'native' NMAGIC binary's
    194   1.1  jdolecek  * exec package
    195   1.1  jdolecek  */
    196   1.1  jdolecek 
    197   1.1  jdolecek int
    198  1.23       dsl netbsd32_exec_aout_prep_nmagic(struct lwp *l, struct exec_package *epp)
    199   1.1  jdolecek {
    200   1.1  jdolecek 	struct netbsd32_exec *execp = epp->ep_hdr;
    201   1.1  jdolecek 	long bsize, baddr;
    202   1.1  jdolecek 
    203  1.12   thorpej 	epp->ep_taddr = AOUT_LDPGSZ;
    204   1.1  jdolecek 	epp->ep_tsize = execp->a_text;
    205  1.12   thorpej 	epp->ep_daddr = roundup(epp->ep_taddr + execp->a_text, AOUT_LDPGSZ);
    206   1.1  jdolecek 	epp->ep_dsize = execp->a_data + execp->a_bss;
    207   1.1  jdolecek 	epp->ep_entry = execp->a_entry;
    208  1.30      maxv 	epp->ep_vm_minaddr = exec_vm_minaddr(VM_MIN_ADDRESS);
    209   1.5       eeh 	epp->ep_vm_maxaddr = VM_MAXUSER_ADDRESS32;
    210   1.1  jdolecek 
    211   1.1  jdolecek 	/* set up command for text segment */
    212   1.1  jdolecek 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, execp->a_text,
    213   1.4       mrg 	    epp->ep_taddr, epp->ep_vp, sizeof(struct netbsd32_exec),
    214   1.1  jdolecek 	    VM_PROT_READ|VM_PROT_EXECUTE);
    215   1.1  jdolecek 
    216   1.1  jdolecek 	/* set up command for data segment */
    217   1.1  jdolecek 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, execp->a_data,
    218   1.4       mrg 	    epp->ep_daddr, epp->ep_vp, execp->a_text + sizeof(struct netbsd32_exec),
    219   1.1  jdolecek 	    VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
    220   1.1  jdolecek 
    221   1.1  jdolecek 	/* set up command for bss segment */
    222  1.13   thorpej 	baddr = roundup(epp->ep_daddr + execp->a_data, PAGE_SIZE);
    223   1.1  jdolecek 	bsize = epp->ep_daddr + epp->ep_dsize - baddr;
    224   1.1  jdolecek 	if (bsize > 0)
    225   1.1  jdolecek 		NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, bsize, baddr,
    226   1.1  jdolecek 		    NULLVP, 0, VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
    227   1.1  jdolecek 
    228  1.21  christos 	return (*epp->ep_esch->es_setup_stack)(l, epp);
    229   1.1  jdolecek }
    230   1.1  jdolecek 
    231   1.1  jdolecek /*
    232   1.1  jdolecek  * netbsd32_exec_aout_prep_omagic(): Prepare a 'native' OMAGIC binary's
    233   1.1  jdolecek  * exec package
    234   1.1  jdolecek  */
    235   1.1  jdolecek 
    236   1.1  jdolecek int
    237  1.23       dsl netbsd32_exec_aout_prep_omagic(struct lwp *l, struct exec_package *epp)
    238   1.1  jdolecek {
    239   1.1  jdolecek 	struct netbsd32_exec *execp = epp->ep_hdr;
    240   1.1  jdolecek 	long dsize, bsize, baddr;
    241   1.1  jdolecek 
    242  1.12   thorpej 	epp->ep_taddr = AOUT_LDPGSZ;
    243   1.1  jdolecek 	epp->ep_tsize = execp->a_text;
    244   1.1  jdolecek 	epp->ep_daddr = epp->ep_taddr + execp->a_text;
    245   1.1  jdolecek 	epp->ep_dsize = execp->a_data + execp->a_bss;
    246   1.1  jdolecek 	epp->ep_entry = execp->a_entry;
    247  1.30      maxv 	epp->ep_vm_minaddr = exec_vm_minaddr(VM_MIN_ADDRESS);
    248   1.5       eeh 	epp->ep_vm_maxaddr = VM_MAXUSER_ADDRESS32;
    249   1.1  jdolecek 
    250   1.1  jdolecek 	/* set up command for text and data segments */
    251   1.1  jdolecek 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn,
    252   1.1  jdolecek 	    execp->a_text + execp->a_data, epp->ep_taddr, epp->ep_vp,
    253   1.4       mrg 	    sizeof(struct netbsd32_exec), VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
    254   1.1  jdolecek 
    255   1.1  jdolecek 	/* set up command for bss segment */
    256  1.13   thorpej 	baddr = roundup(epp->ep_daddr + execp->a_data, PAGE_SIZE);
    257   1.1  jdolecek 	bsize = epp->ep_daddr + epp->ep_dsize - baddr;
    258   1.1  jdolecek 	if (bsize > 0)
    259   1.1  jdolecek 		NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, bsize, baddr,
    260   1.1  jdolecek 		    NULLVP, 0, VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
    261   1.1  jdolecek 
    262   1.1  jdolecek 	/*
    263   1.1  jdolecek 	 * Make sure (# of pages) mapped above equals (vm_tsize + vm_dsize);
    264   1.1  jdolecek 	 * obreak(2) relies on this fact. Both `vm_tsize' and `vm_dsize' are
    265   1.1  jdolecek 	 * computed (in execve(2)) by rounding *up* `ep_tsize' and `ep_dsize'
    266   1.1  jdolecek 	 * respectively to page boundaries.
    267   1.1  jdolecek 	 * Compensate `ep_dsize' for the amount of data covered by the last
    268  1.20     perry 	 * text page.
    269   1.1  jdolecek 	 */
    270  1.13   thorpej 	dsize = epp->ep_dsize + execp->a_text - roundup(execp->a_text,
    271  1.13   thorpej 							PAGE_SIZE);
    272   1.1  jdolecek 	epp->ep_dsize = (dsize > 0) ? dsize : 0;
    273  1.21  christos 	return (*epp->ep_esch->es_setup_stack)(l, epp);
    274   1.1  jdolecek }
    275  1.29  christos 
    276  1.29  christos #ifdef COMPAT_NOMID
    277  1.29  christos /*
    278  1.29  christos  * netbsd32_exec_aout_prep_oldzmagic():
    279  1.29  christos  *	Prepare the vmcmds to build a vmspace for an old ZMAGIC
    280  1.29  christos  *	binary. [386BSD/BSDI/4.4BSD/NetBSD0.8]
    281  1.29  christos  *
    282  1.29  christos  * Cloned from exec_aout_prep_zmagic() in kern/exec_aout.c; a more verbose
    283  1.29  christos  * description of operation is there.
    284  1.29  christos  * There were copies of this in the mac68k, hp300, and i386 ports.
    285  1.29  christos  */
    286  1.29  christos static int
    287  1.29  christos netbsd32_exec_aout_prep_oldzmagic(struct lwp *l, struct exec_package *epp)
    288  1.29  christos {
    289  1.29  christos 	struct netbsd32_exec *execp = epp->ep_hdr;
    290  1.29  christos 	int error;
    291  1.29  christos 
    292  1.29  christos 	epp->ep_taddr = 0;
    293  1.29  christos 	epp->ep_tsize = execp->a_text;
    294  1.29  christos 	epp->ep_daddr = epp->ep_taddr + execp->a_text;
    295  1.29  christos 	epp->ep_dsize = execp->a_data + execp->a_bss;
    296  1.29  christos 	epp->ep_entry = execp->a_entry;
    297  1.30      maxv 	epp->ep_vm_minaddr = exec_vm_minaddr(VM_MIN_ADDRESS);
    298  1.29  christos 	epp->ep_vm_maxaddr = VM_MAXUSER_ADDRESS32;
    299  1.29  christos 
    300  1.29  christos 	error = vn_marktext(epp->ep_vp);
    301  1.29  christos 	if (error)
    302  1.29  christos 		return (error);
    303  1.29  christos 
    304  1.29  christos 	/* set up command for text segment */
    305  1.29  christos 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_pagedvn, execp->a_text,
    306  1.29  christos 	    epp->ep_taddr, epp->ep_vp, PAGE_SIZE, /* XXX CLBYTES? */
    307  1.29  christos 	    VM_PROT_READ|VM_PROT_EXECUTE);
    308  1.29  christos 
    309  1.29  christos 	/* set up command for data segment */
    310  1.29  christos 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_pagedvn, execp->a_data,
    311  1.29  christos 	    epp->ep_daddr, epp->ep_vp,
    312  1.29  christos 	    execp->a_text + PAGE_SIZE, /* XXX CLBYTES? */
    313  1.29  christos 	    VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
    314  1.29  christos 
    315  1.29  christos 	/* set up command for bss segment */
    316  1.29  christos 	if (execp->a_bss)
    317  1.29  christos 	    NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, execp->a_bss,
    318  1.29  christos 		epp->ep_daddr + execp->a_data, NULLVP, 0,
    319  1.29  christos 		VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
    320  1.29  christos 
    321  1.29  christos 	return (*epp->ep_esch->es_setup_stack)(l, epp);
    322  1.29  christos }
    323  1.29  christos 
    324  1.29  christos 
    325  1.29  christos /*
    326  1.29  christos  * netbsd32_exec_aout_prep_oldnmagic():
    327  1.29  christos  *	Prepare the vmcmds to build a vmspace for an old NMAGIC
    328  1.29  christos  *	binary. [BSDI]
    329  1.29  christos  *
    330  1.29  christos  * Cloned from exec_aout_prep_nmagic() in kern/exec_aout.c; with text starting
    331  1.29  christos  * at 0.
    332  1.29  christos  * XXX: There must be a better way to share this code.
    333  1.29  christos  */
    334  1.29  christos static int
    335  1.29  christos netbsd32_exec_aout_prep_oldnmagic(struct lwp *l, struct exec_package *epp)
    336  1.29  christos {
    337  1.29  christos 	struct netbsd32_exec *execp = epp->ep_hdr;
    338  1.29  christos 	long bsize, baddr;
    339  1.29  christos 
    340  1.29  christos 	epp->ep_taddr = 0;
    341  1.29  christos 	epp->ep_tsize = execp->a_text;
    342  1.29  christos 	epp->ep_daddr = roundup(epp->ep_taddr + execp->a_text, AOUT_LDPGSZ);
    343  1.29  christos 	epp->ep_dsize = execp->a_data + execp->a_bss;
    344  1.29  christos 	epp->ep_entry = execp->a_entry;
    345  1.30      maxv 	epp->ep_vm_minaddr = exec_vm_minaddr(VM_MIN_ADDRESS);
    346  1.29  christos 	epp->ep_vm_maxaddr = VM_MAXUSER_ADDRESS32;
    347  1.29  christos 
    348  1.29  christos 	/* set up command for text segment */
    349  1.29  christos 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, execp->a_text,
    350  1.29  christos 	    epp->ep_taddr, epp->ep_vp, sizeof(struct netbsd32_exec),
    351  1.29  christos 	    VM_PROT_READ|VM_PROT_EXECUTE);
    352  1.29  christos 
    353  1.29  christos 	/* set up command for data segment */
    354  1.29  christos 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, execp->a_data,
    355  1.29  christos 	    epp->ep_daddr, epp->ep_vp, execp->a_text + sizeof(struct netbsd32_exec),
    356  1.29  christos 	    VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
    357  1.29  christos 
    358  1.29  christos 	/* set up command for bss segment */
    359  1.29  christos 	baddr = roundup(epp->ep_daddr + execp->a_data, PAGE_SIZE);
    360  1.29  christos 	bsize = epp->ep_daddr + epp->ep_dsize - baddr;
    361  1.29  christos 	if (bsize > 0)
    362  1.29  christos 		NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, bsize, baddr,
    363  1.29  christos 		    NULLVP, 0, VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
    364  1.29  christos 
    365  1.29  christos 	return (*epp->ep_esch->es_setup_stack)(l, epp);
    366  1.29  christos }
    367  1.29  christos 
    368  1.29  christos 
    369  1.29  christos /*
    370  1.29  christos  * netbsd32_exec_aout_prep_oldomagic():
    371  1.29  christos  *	Prepare the vmcmds to build a vmspace for an old OMAGIC
    372  1.29  christos  *	binary. [BSDI]
    373  1.29  christos  *
    374  1.29  christos  * Cloned from exec_aout_prep_omagic() in kern/exec_aout.c; with text starting
    375  1.29  christos  * at 0.
    376  1.29  christos  * XXX: There must be a better way to share this code.
    377  1.29  christos  */
    378  1.29  christos static int
    379  1.29  christos netbsd32_exec_aout_prep_oldomagic(struct lwp *l, struct exec_package *epp)
    380  1.29  christos {
    381  1.29  christos 	struct netbsd32_exec *execp = epp->ep_hdr;
    382  1.29  christos 	long dsize, bsize, baddr;
    383  1.29  christos 
    384  1.29  christos 	epp->ep_taddr = 0;
    385  1.29  christos 	epp->ep_tsize = execp->a_text;
    386  1.29  christos 	epp->ep_daddr = epp->ep_taddr + execp->a_text;
    387  1.29  christos 	epp->ep_dsize = execp->a_data + execp->a_bss;
    388  1.29  christos 	epp->ep_entry = execp->a_entry;
    389  1.29  christos 
    390  1.29  christos 	/* set up command for text and data segments */
    391  1.29  christos 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn,
    392  1.29  christos 	    execp->a_text + execp->a_data, epp->ep_taddr, epp->ep_vp,
    393  1.29  christos 	    sizeof(struct netbsd32_exec), VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
    394  1.29  christos 
    395  1.29  christos 	/* set up command for bss segment */
    396  1.29  christos 	baddr = roundup(epp->ep_daddr + execp->a_data, PAGE_SIZE);
    397  1.29  christos 	bsize = epp->ep_daddr + epp->ep_dsize - baddr;
    398  1.29  christos 	if (bsize > 0)
    399  1.29  christos 		NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, bsize, baddr,
    400  1.29  christos 		    NULLVP, 0, VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
    401  1.29  christos 
    402  1.29  christos 	/*
    403  1.29  christos 	 * Make sure (# of pages) mapped above equals (vm_tsize + vm_dsize);
    404  1.29  christos 	 * obreak(2) relies on this fact. Both `vm_tsize' and `vm_dsize' are
    405  1.29  christos 	 * computed (in execve(2)) by rounding *up* `ep_tsize' and `ep_dsize'
    406  1.29  christos 	 * respectively to page boundaries.
    407  1.29  christos 	 * Compensate `ep_dsize' for the amount of data covered by the last
    408  1.29  christos 	 * text page.
    409  1.29  christos 	 */
    410  1.29  christos 	dsize = epp->ep_dsize + execp->a_text - roundup(execp->a_text,
    411  1.29  christos 							PAGE_SIZE);
    412  1.29  christos 	epp->ep_dsize = (dsize > 0) ? dsize : 0;
    413  1.29  christos 	return (*epp->ep_esch->es_setup_stack)(l, epp);
    414  1.29  christos }
    415  1.29  christos 
    416  1.29  christos static int
    417  1.29  christos netbsd32_exec_aout_nomid(struct lwp *l, struct exec_package *epp)
    418  1.29  christos {
    419  1.29  christos 	int error;
    420  1.29  christos 	u_long midmag, magic;
    421  1.29  christos 	u_short mid;
    422  1.29  christos 	struct exec *execp = epp->ep_hdr;
    423  1.29  christos 
    424  1.29  christos 	/* check on validity of epp->ep_hdr performed by exec_out_makecmds */
    425  1.29  christos 
    426  1.29  christos 	midmag = ntohl(execp->a_midmag);
    427  1.29  christos 	mid = (midmag >> 16) & 0xffff;
    428  1.29  christos 	magic = midmag & 0xffff;
    429  1.29  christos 
    430  1.29  christos 	if (magic == 0) {
    431  1.29  christos 		magic = (execp->a_midmag & 0xffff);
    432  1.29  christos 		mid = MID_ZERO;
    433  1.29  christos 	}
    434  1.29  christos 
    435  1.29  christos 	midmag = mid << 16 | magic;
    436  1.29  christos 
    437  1.29  christos 	switch (midmag) {
    438  1.29  christos 	case (MID_ZERO << 16) | ZMAGIC:
    439  1.29  christos 		/*
    440  1.29  christos 		 * 386BSD's ZMAGIC format:
    441  1.29  christos 		 */
    442  1.29  christos 		return netbsd32_exec_aout_prep_oldzmagic(l, epp);
    443  1.29  christos 		break;
    444  1.29  christos 
    445  1.29  christos 	case (MID_ZERO << 16) | QMAGIC:
    446  1.29  christos 		/*
    447  1.29  christos 		 * BSDI's QMAGIC format:
    448  1.29  christos 		 * same as new ZMAGIC format, but with different magic number
    449  1.29  christos 		 */
    450  1.29  christos 		return netbsd32_exec_aout_prep_zmagic(l, epp);
    451  1.29  christos 		break;
    452  1.29  christos 
    453  1.29  christos 	case (MID_ZERO << 16) | NMAGIC:
    454  1.29  christos 		/*
    455  1.29  christos 		 * BSDI's NMAGIC format:
    456  1.29  christos 		 * same as NMAGIC format, but with different magic number
    457  1.29  christos 		 * and with text starting at 0.
    458  1.29  christos 		 */
    459  1.29  christos 		return netbsd32_exec_aout_prep_oldnmagic(l, epp);
    460  1.29  christos 
    461  1.29  christos 	case (MID_ZERO << 16) | OMAGIC:
    462  1.29  christos 		/*
    463  1.29  christos 		 * BSDI's OMAGIC format:
    464  1.29  christos 		 * same as OMAGIC format, but with different magic number
    465  1.29  christos 		 * and with text starting at 0.
    466  1.29  christos 		 */
    467  1.29  christos 		return netbsd32_exec_aout_prep_oldomagic(l, epp);
    468  1.29  christos 
    469  1.29  christos 	default:
    470  1.29  christos 		return ENOEXEC;
    471  1.29  christos 	}
    472  1.29  christos 
    473  1.29  christos 	return error;
    474  1.29  christos }
    475  1.29  christos #endif
    476