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