Home | History | Annotate | Line # | Download | only in common
linux_exec_aout.c revision 1.44.2.2
      1  1.44.2.2   nathanw /*	$NetBSD: linux_exec_aout.c,v 1.44.2.2 2001/11/14 19:13:09 nathanw Exp $	*/
      2      1.29  christos 
      3      1.29  christos /*-
      4      1.33      fvdl  * Copyright (c) 1995, 1998 The NetBSD Foundation, Inc.
      5      1.29  christos  * All rights reserved.
      6      1.29  christos  *
      7      1.29  christos  * This code is derived from software contributed to The NetBSD Foundation
      8      1.33      fvdl  * by Christos Zoulas, Frank van der Linden and Eric Haszlakiewicz.
      9      1.29  christos  *
     10      1.29  christos  * Redistribution and use in source and binary forms, with or without
     11      1.29  christos  * modification, are permitted provided that the following conditions
     12      1.29  christos  * are met:
     13      1.29  christos  * 1. Redistributions of source code must retain the above copyright
     14      1.29  christos  *    notice, this list of conditions and the following disclaimer.
     15      1.29  christos  * 2. Redistributions in binary form must reproduce the above copyright
     16      1.29  christos  *    notice, this list of conditions and the following disclaimer in the
     17      1.29  christos  *    documentation and/or other materials provided with the distribution.
     18      1.29  christos  * 3. All advertising materials mentioning features or use of this software
     19      1.29  christos  *    must display the following acknowledgement:
     20      1.31       erh  *	This product includes software developed by the NetBSD
     21      1.31       erh  *	Foundation, Inc. and its contributors.
     22      1.29  christos  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23      1.29  christos  *    contributors may be used to endorse or promote products derived
     24      1.29  christos  *    from this software without specific prior written permission.
     25      1.29  christos  *
     26      1.29  christos  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27      1.29  christos  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28      1.29  christos  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29      1.29  christos  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30      1.29  christos  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31      1.29  christos  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32      1.29  christos  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33      1.29  christos  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34      1.29  christos  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35      1.29  christos  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36      1.29  christos  * POSSIBILITY OF SUCH DAMAGE.
     37      1.29  christos  */
     38       1.1      fvdl 
     39       1.1      fvdl /*
     40       1.7      fvdl  * based on exec_aout.c, sunos_exec.c and svr4_exec.c
     41       1.1      fvdl  */
     42       1.1      fvdl 
     43  1.44.2.2   nathanw #include <sys/cdefs.h>
     44  1.44.2.2   nathanw __KERNEL_RCSID(0, "$NetBSD: linux_exec_aout.c,v 1.44.2.2 2001/11/14 19:13:09 nathanw Exp $");
     45  1.44.2.2   nathanw 
     46       1.1      fvdl #include <sys/param.h>
     47       1.1      fvdl #include <sys/systm.h>
     48       1.1      fvdl #include <sys/kernel.h>
     49       1.1      fvdl #include <sys/proc.h>
     50       1.1      fvdl #include <sys/malloc.h>
     51       1.1      fvdl #include <sys/namei.h>
     52       1.1      fvdl #include <sys/vnode.h>
     53      1.13  christos #include <sys/mount.h>
     54      1.25  christos #include <sys/exec.h>
     55       1.8      fvdl #include <sys/exec_elf.h>
     56       1.1      fvdl 
     57       1.1      fvdl #include <sys/mman.h>
     58      1.13  christos #include <sys/syscallargs.h>
     59       1.1      fvdl 
     60      1.32  christos #include <machine/cpu.h>
     61      1.32  christos #include <machine/reg.h>
     62      1.32  christos 
     63      1.32  christos #include <compat/linux/common/linux_types.h>
     64      1.32  christos #include <compat/linux/common/linux_signal.h>
     65      1.32  christos #include <compat/linux/common/linux_util.h>
     66      1.32  christos #include <compat/linux/common/linux_exec.h>
     67      1.32  christos #include <compat/linux/common/linux_machdep.h>
     68      1.32  christos 
     69      1.32  christos #include <compat/linux/linux_syscallargs.h>
     70       1.4  christos #include <compat/linux/linux_syscall.h>
     71      1.31       erh 
     72  1.44.2.1   nathanw int linux_aout_copyargs __P((struct exec_package *, struct ps_strings *,
     73  1.44.2.1   nathanw     char **, void *));
     74       1.4  christos 
     75      1.42  jdolecek static int exec_linux_aout_prep_zmagic __P((struct proc *,
     76  1.44.2.1   nathanw     struct exec_package *));
     77      1.42  jdolecek static int exec_linux_aout_prep_nmagic __P((struct proc *,
     78  1.44.2.1   nathanw     struct exec_package *));
     79      1.42  jdolecek static int exec_linux_aout_prep_omagic __P((struct proc *,
     80  1.44.2.1   nathanw     struct exec_package *));
     81      1.42  jdolecek static int exec_linux_aout_prep_qmagic __P((struct proc *,
     82  1.44.2.1   nathanw     struct exec_package *));
     83       1.6      fvdl 
     84  1.44.2.1   nathanw int
     85  1.44.2.1   nathanw linux_aout_copyargs(pack, arginfo, stackp, argp)
     86       1.4  christos 	struct exec_package *pack;
     87       1.4  christos 	struct ps_strings *arginfo;
     88  1.44.2.1   nathanw 	char **stackp;
     89       1.4  christos 	void *argp;
     90       1.4  christos {
     91  1.44.2.1   nathanw 	char **cpp = (char **)*stackp;
     92  1.44.2.1   nathanw 	char **stk = (char **)*stackp;
     93       1.4  christos 	char *dp, *sp;
     94       1.4  christos 	size_t len;
     95       1.4  christos 	void *nullp = NULL;
     96       1.4  christos 	int argc = arginfo->ps_nargvstr;
     97       1.4  christos 	int envc = arginfo->ps_nenvstr;
     98  1.44.2.1   nathanw 	int error;
     99       1.4  christos 
    100  1.44.2.1   nathanw 	if ((error = copyout(&argc, cpp++, sizeof(argc))) != 0)
    101  1.44.2.1   nathanw 		return error;
    102       1.4  christos 
    103       1.4  christos 	/* leave room for envp and argv */
    104       1.4  christos 	cpp += 2;
    105  1.44.2.1   nathanw 	if ((error = copyout(&cpp, &stk[1], sizeof (cpp))) != 0)
    106  1.44.2.1   nathanw 		return error;
    107       1.4  christos 
    108       1.4  christos 	dp = (char *) (cpp + argc + envc + 2);
    109       1.4  christos 	sp = argp;
    110       1.4  christos 
    111       1.4  christos 	/* XXX don't copy them out, remap them! */
    112       1.5   mycroft 	arginfo->ps_argvstr = cpp; /* remember location of argv for later */
    113       1.4  christos 
    114       1.4  christos 	for (; --argc >= 0; sp += len, dp += len)
    115  1.44.2.1   nathanw 		if ((error = copyout(&dp, cpp++, sizeof(dp))) != 0 ||
    116  1.44.2.1   nathanw 		    (error = copyoutstr(sp, dp, ARG_MAX, &len)) != 0)
    117  1.44.2.1   nathanw 			return error;
    118       1.4  christos 
    119  1.44.2.1   nathanw 	if ((error = copyout(&nullp, cpp++, sizeof(nullp))) != 0)
    120  1.44.2.1   nathanw 		return error;
    121       1.4  christos 
    122  1.44.2.1   nathanw 	if ((error = copyout(&cpp, &stk[2], sizeof (cpp))) != 0)
    123  1.44.2.1   nathanw 		return error;
    124       1.4  christos 
    125       1.5   mycroft 	arginfo->ps_envstr = cpp; /* remember location of envp for later */
    126       1.4  christos 
    127       1.4  christos 	for (; --envc >= 0; sp += len, dp += len)
    128  1.44.2.1   nathanw 		if ((error = copyout(&dp, cpp++, sizeof(dp))) != 0 ||
    129  1.44.2.1   nathanw 		    (error = copyoutstr(sp, dp, ARG_MAX, &len)) != 0)
    130  1.44.2.1   nathanw 			return error;
    131       1.4  christos 
    132  1.44.2.1   nathanw 	if ((error = copyout(&nullp, cpp++, sizeof(nullp))) != 0)
    133  1.44.2.1   nathanw 		return error;
    134       1.4  christos 
    135  1.44.2.1   nathanw 	*stackp = (char *)cpp;
    136  1.44.2.1   nathanw 	return 0;
    137       1.4  christos }
    138       1.4  christos 
    139       1.1      fvdl int
    140       1.1      fvdl exec_linux_aout_makecmds(p, epp)
    141       1.1      fvdl 	struct proc *p;
    142       1.1      fvdl 	struct exec_package *epp;
    143       1.1      fvdl {
    144       1.1      fvdl 	struct exec *linux_ep = epp->ep_hdr;
    145       1.1      fvdl 	int machtype, magic;
    146       1.1      fvdl 	int error = ENOEXEC;
    147       1.1      fvdl 
    148       1.1      fvdl 	magic = LINUX_N_MAGIC(linux_ep);
    149       1.1      fvdl 	machtype = LINUX_N_MACHTYPE(linux_ep);
    150       1.1      fvdl 
    151       1.1      fvdl 
    152       1.1      fvdl 	if (machtype != LINUX_MID_MACHINE)
    153       1.1      fvdl 		return (ENOEXEC);
    154       1.1      fvdl 
    155       1.1      fvdl 	switch (magic) {
    156       1.1      fvdl 	case QMAGIC:
    157       1.1      fvdl 		error = exec_linux_aout_prep_qmagic(p, epp);
    158       1.1      fvdl 		break;
    159       1.1      fvdl 	case ZMAGIC:
    160       1.1      fvdl 		error = exec_linux_aout_prep_zmagic(p, epp);
    161       1.1      fvdl 		break;
    162       1.1      fvdl 	case NMAGIC:
    163       1.1      fvdl 		error = exec_linux_aout_prep_nmagic(p, epp);
    164       1.1      fvdl 		break;
    165       1.1      fvdl 	case OMAGIC:
    166       1.1      fvdl 		error = exec_linux_aout_prep_omagic(p, epp);
    167       1.1      fvdl 		break;
    168       1.1      fvdl 	}
    169       1.1      fvdl 	return error;
    170       1.1      fvdl }
    171       1.1      fvdl 
    172       1.1      fvdl /*
    173       1.1      fvdl  * Since text starts at 0x400 in Linux ZMAGIC executables, and 0x400
    174       1.1      fvdl  * is very likely not page aligned on most architectures, it is treated
    175       1.1      fvdl  * as an NMAGIC here. XXX
    176       1.1      fvdl  */
    177       1.1      fvdl 
    178      1.42  jdolecek static int
    179       1.1      fvdl exec_linux_aout_prep_zmagic(p, epp)
    180       1.1      fvdl 	struct proc *p;
    181       1.1      fvdl 	struct exec_package *epp;
    182       1.1      fvdl {
    183       1.1      fvdl 	struct exec *execp = epp->ep_hdr;
    184       1.1      fvdl 
    185       1.1      fvdl 	epp->ep_taddr = LINUX_N_TXTADDR(*execp, ZMAGIC);
    186       1.1      fvdl 	epp->ep_tsize = execp->a_text;
    187       1.1      fvdl 	epp->ep_daddr = LINUX_N_DATADDR(*execp, ZMAGIC);
    188       1.1      fvdl 	epp->ep_dsize = execp->a_data + execp->a_bss;
    189       1.1      fvdl 	epp->ep_entry = execp->a_entry;
    190       1.1      fvdl 
    191       1.1      fvdl 	/* set up command for text segment */
    192       1.1      fvdl 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, execp->a_text,
    193       1.1      fvdl 	    epp->ep_taddr, epp->ep_vp, LINUX_N_TXTOFF(*execp, ZMAGIC),
    194       1.1      fvdl 	    VM_PROT_READ|VM_PROT_EXECUTE);
    195       1.1      fvdl 
    196       1.1      fvdl 	/* set up command for data segment */
    197       1.1      fvdl 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, execp->a_data,
    198       1.1      fvdl 	    epp->ep_daddr, epp->ep_vp, LINUX_N_DATOFF(*execp, ZMAGIC),
    199       1.1      fvdl 	    VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
    200       1.1      fvdl 
    201       1.1      fvdl 	/* set up command for bss segment */
    202       1.1      fvdl 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, execp->a_bss,
    203       1.1      fvdl 	    epp->ep_daddr + execp->a_data, NULLVP, 0,
    204       1.1      fvdl 	    VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
    205       1.1      fvdl 
    206       1.1      fvdl 	return exec_aout_setup_stack(p, epp);
    207       1.1      fvdl }
    208       1.1      fvdl 
    209       1.1      fvdl /*
    210       1.1      fvdl  * exec_aout_prep_nmagic(): Prepare Linux NMAGIC package.
    211       1.1      fvdl  * Not different from the normal stuff.
    212       1.1      fvdl  */
    213       1.1      fvdl 
    214      1.42  jdolecek static int
    215       1.1      fvdl exec_linux_aout_prep_nmagic(p, epp)
    216       1.1      fvdl 	struct proc *p;
    217       1.1      fvdl 	struct exec_package *epp;
    218       1.1      fvdl {
    219       1.1      fvdl 	struct exec *execp = epp->ep_hdr;
    220       1.1      fvdl 	long bsize, baddr;
    221       1.1      fvdl 
    222       1.1      fvdl 	epp->ep_taddr = LINUX_N_TXTADDR(*execp, NMAGIC);
    223       1.1      fvdl 	epp->ep_tsize = execp->a_text;
    224       1.1      fvdl 	epp->ep_daddr = LINUX_N_DATADDR(*execp, NMAGIC);
    225       1.1      fvdl 	epp->ep_dsize = execp->a_data + execp->a_bss;
    226       1.1      fvdl 	epp->ep_entry = execp->a_entry;
    227       1.1      fvdl 
    228       1.1      fvdl 	/* set up command for text segment */
    229       1.1      fvdl 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, execp->a_text,
    230       1.1      fvdl 	    epp->ep_taddr, epp->ep_vp, LINUX_N_TXTOFF(*execp, NMAGIC),
    231       1.1      fvdl 	    VM_PROT_READ|VM_PROT_EXECUTE);
    232       1.1      fvdl 
    233       1.1      fvdl 	/* set up command for data segment */
    234       1.1      fvdl 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, execp->a_data,
    235       1.1      fvdl 	    epp->ep_daddr, epp->ep_vp, LINUX_N_DATOFF(*execp, NMAGIC),
    236       1.1      fvdl 	    VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
    237       1.1      fvdl 
    238       1.1      fvdl 	/* set up command for bss segment */
    239       1.1      fvdl 	baddr = roundup(epp->ep_daddr + execp->a_data, NBPG);
    240       1.1      fvdl 	bsize = epp->ep_daddr + epp->ep_dsize - baddr;
    241       1.1      fvdl 	if (bsize > 0)
    242       1.1      fvdl 		NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, bsize, baddr,
    243       1.1      fvdl 		    NULLVP, 0, VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
    244       1.1      fvdl 
    245       1.1      fvdl 	return exec_aout_setup_stack(p, epp);
    246       1.1      fvdl }
    247       1.1      fvdl 
    248       1.1      fvdl /*
    249       1.1      fvdl  * exec_aout_prep_omagic(): Prepare Linux OMAGIC package.
    250       1.1      fvdl  * Business as usual.
    251       1.1      fvdl  */
    252       1.1      fvdl 
    253      1.42  jdolecek static int
    254       1.1      fvdl exec_linux_aout_prep_omagic(p, epp)
    255       1.1      fvdl 	struct proc *p;
    256       1.1      fvdl 	struct exec_package *epp;
    257       1.1      fvdl {
    258       1.1      fvdl 	struct exec *execp = epp->ep_hdr;
    259       1.1      fvdl 	long dsize, bsize, baddr;
    260       1.1      fvdl 
    261       1.1      fvdl 	epp->ep_taddr = LINUX_N_TXTADDR(*execp, OMAGIC);
    262       1.1      fvdl 	epp->ep_tsize = execp->a_text;
    263       1.1      fvdl 	epp->ep_daddr = LINUX_N_DATADDR(*execp, OMAGIC);
    264       1.1      fvdl 	epp->ep_dsize = execp->a_data + execp->a_bss;
    265       1.1      fvdl 	epp->ep_entry = execp->a_entry;
    266       1.1      fvdl 
    267       1.1      fvdl 	/* set up command for text and data segments */
    268       1.1      fvdl 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn,
    269       1.1      fvdl 	    execp->a_text + execp->a_data, epp->ep_taddr, epp->ep_vp,
    270       1.1      fvdl 	    LINUX_N_TXTOFF(*execp, OMAGIC), VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
    271       1.1      fvdl 
    272       1.1      fvdl 	/* set up command for bss segment */
    273       1.1      fvdl 	baddr = roundup(epp->ep_daddr + execp->a_data, NBPG);
    274       1.1      fvdl 	bsize = epp->ep_daddr + epp->ep_dsize - baddr;
    275       1.1      fvdl 	if (bsize > 0)
    276       1.1      fvdl 		NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, bsize, baddr,
    277       1.1      fvdl 		    NULLVP, 0, VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
    278       1.1      fvdl 
    279       1.1      fvdl 	/*
    280       1.1      fvdl 	 * Make sure (# of pages) mapped above equals (vm_tsize + vm_dsize);
    281       1.1      fvdl 	 * obreak(2) relies on this fact. Both `vm_tsize' and `vm_dsize' are
    282       1.1      fvdl 	 * computed (in execve(2)) by rounding *up* `ep_tsize' and `ep_dsize'
    283       1.1      fvdl 	 * respectively to page boundaries.
    284       1.1      fvdl 	 * Compensate `ep_dsize' for the amount of data covered by the last
    285       1.1      fvdl 	 * text page.
    286       1.1      fvdl 	 */
    287       1.1      fvdl 	dsize = epp->ep_dsize + execp->a_text - roundup(execp->a_text, NBPG);
    288       1.1      fvdl 	epp->ep_dsize = (dsize > 0) ? dsize : 0;
    289       1.1      fvdl 	return exec_aout_setup_stack(p, epp);
    290       1.1      fvdl }
    291       1.1      fvdl 
    292      1.42  jdolecek static int
    293       1.1      fvdl exec_linux_aout_prep_qmagic(p, epp)
    294       1.1      fvdl 	struct proc *p;
    295       1.1      fvdl 	struct exec_package *epp;
    296       1.1      fvdl {
    297       1.1      fvdl 	struct exec *execp = epp->ep_hdr;
    298       1.1      fvdl 
    299       1.1      fvdl 	epp->ep_taddr = LINUX_N_TXTADDR(*execp, QMAGIC);
    300       1.1      fvdl 	epp->ep_tsize = execp->a_text;
    301       1.1      fvdl 	epp->ep_daddr = LINUX_N_DATADDR(*execp, QMAGIC);
    302       1.1      fvdl 	epp->ep_dsize = execp->a_data + execp->a_bss;
    303       1.1      fvdl 	epp->ep_entry = execp->a_entry;
    304       1.1      fvdl 
    305       1.1      fvdl 	/*
    306       1.1      fvdl 	 * check if vnode is in open for writing, because we want to
    307       1.1      fvdl 	 * demand-page out of it.  if it is, don't do it, for various
    308       1.1      fvdl 	 * reasons
    309       1.1      fvdl 	 */
    310       1.1      fvdl 	if ((execp->a_text != 0 || execp->a_data != 0) &&
    311       1.1      fvdl 	    epp->ep_vp->v_writecount != 0) {
    312       1.1      fvdl #ifdef DIAGNOSTIC
    313       1.1      fvdl 		if (epp->ep_vp->v_flag & VTEXT)
    314       1.1      fvdl 			panic("exec: a VTEXT vnode has writecount != 0\n");
    315       1.1      fvdl #endif
    316       1.1      fvdl 		return ETXTBSY;
    317       1.1      fvdl 	}
    318  1.44.2.2   nathanw 	epp->ep_vp->v_flag |= VTEXT;
    319       1.1      fvdl 
    320       1.1      fvdl 	/* set up command for text segment */
    321       1.1      fvdl 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_pagedvn, execp->a_text,
    322       1.1      fvdl 	    epp->ep_taddr, epp->ep_vp, LINUX_N_TXTOFF(*execp, QMAGIC),
    323       1.1      fvdl 	    VM_PROT_READ|VM_PROT_EXECUTE);
    324       1.1      fvdl 
    325       1.1      fvdl 	/* set up command for data segment */
    326       1.1      fvdl 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_pagedvn, execp->a_data,
    327       1.1      fvdl 	    epp->ep_daddr, epp->ep_vp, LINUX_N_DATOFF(*execp, QMAGIC),
    328       1.1      fvdl 	    VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
    329       1.1      fvdl 
    330       1.1      fvdl 	/* set up command for bss segment */
    331       1.1      fvdl 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, execp->a_bss,
    332       1.1      fvdl 	    epp->ep_daddr + execp->a_data, NULLVP, 0,
    333       1.1      fvdl 	    VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
    334       1.1      fvdl 
    335       1.1      fvdl 	return exec_aout_setup_stack(p, epp);
    336       1.1      fvdl }
    337