Home | History | Annotate | Line # | Download | only in common
linux_exec_aout.c revision 1.67
      1  1.67      maxv /*	$NetBSD: linux_exec_aout.c,v 1.67 2014/11/09 17:48:08 maxv 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  *
     19  1.29  christos  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  1.29  christos  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  1.29  christos  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  1.29  christos  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  1.29  christos  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  1.29  christos  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  1.29  christos  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  1.29  christos  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  1.29  christos  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  1.29  christos  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  1.29  christos  * POSSIBILITY OF SUCH DAMAGE.
     30  1.29  christos  */
     31   1.1      fvdl 
     32   1.1      fvdl /*
     33   1.7      fvdl  * based on exec_aout.c, sunos_exec.c and svr4_exec.c
     34   1.1      fvdl  */
     35  1.47     lukem 
     36  1.47     lukem #include <sys/cdefs.h>
     37  1.67      maxv __KERNEL_RCSID(0, "$NetBSD: linux_exec_aout.c,v 1.67 2014/11/09 17:48:08 maxv Exp $");
     38  1.66      matt 
     39  1.66      matt #ifdef _KERNEL_OPT
     40  1.66      matt #include "opt_execfmt.h"
     41  1.66      matt #endif
     42   1.1      fvdl 
     43   1.1      fvdl #include <sys/param.h>
     44   1.1      fvdl #include <sys/systm.h>
     45   1.1      fvdl #include <sys/kernel.h>
     46   1.1      fvdl #include <sys/proc.h>
     47   1.1      fvdl #include <sys/namei.h>
     48   1.1      fvdl #include <sys/vnode.h>
     49  1.13  christos #include <sys/mount.h>
     50  1.25  christos #include <sys/exec.h>
     51   1.1      fvdl 
     52   1.1      fvdl #include <sys/mman.h>
     53  1.13  christos #include <sys/syscallargs.h>
     54   1.1      fvdl 
     55  1.62        ad #include <sys/cpu.h>
     56  1.32  christos #include <machine/reg.h>
     57  1.32  christos 
     58  1.32  christos #include <compat/linux/common/linux_types.h>
     59  1.32  christos #include <compat/linux/common/linux_signal.h>
     60  1.32  christos #include <compat/linux/common/linux_util.h>
     61  1.32  christos #include <compat/linux/common/linux_exec.h>
     62  1.32  christos #include <compat/linux/common/linux_machdep.h>
     63  1.32  christos 
     64  1.32  christos #include <compat/linux/linux_syscallargs.h>
     65   1.4  christos #include <compat/linux/linux_syscall.h>
     66  1.31       erh 
     67  1.63       dsl int linux_aout_copyargs(struct lwp *, struct exec_package *,
     68  1.63       dsl     struct ps_strings *, char **, void *);
     69   1.4  christos 
     70  1.63       dsl static int exec_linux_aout_prep_zmagic(struct lwp *,
     71  1.63       dsl     struct exec_package *);
     72  1.63       dsl static int exec_linux_aout_prep_nmagic(struct lwp *,
     73  1.63       dsl     struct exec_package *);
     74  1.63       dsl static int exec_linux_aout_prep_omagic(struct lwp *,
     75  1.63       dsl     struct exec_package *);
     76  1.63       dsl static int exec_linux_aout_prep_qmagic(struct lwp *,
     77  1.63       dsl     struct exec_package *);
     78   1.6      fvdl 
     79  1.45  christos int
     80  1.60  christos linux_aout_copyargs(struct lwp *l, struct exec_package *pack,
     81  1.59  christos     struct ps_strings *arginfo, char **stackp, void *argp)
     82   1.4  christos {
     83  1.45  christos 	char **cpp = (char **)*stackp;
     84  1.45  christos 	char **stk = (char **)*stackp;
     85   1.4  christos 	char *dp, *sp;
     86   1.4  christos 	size_t len;
     87   1.4  christos 	void *nullp = NULL;
     88   1.4  christos 	int argc = arginfo->ps_nargvstr;
     89   1.4  christos 	int envc = arginfo->ps_nenvstr;
     90  1.45  christos 	int error;
     91   1.4  christos 
     92  1.45  christos 	if ((error = copyout(&argc, cpp++, sizeof(argc))) != 0)
     93  1.45  christos 		return error;
     94   1.4  christos 
     95   1.4  christos 	/* leave room for envp and argv */
     96   1.4  christos 	cpp += 2;
     97  1.45  christos 	if ((error = copyout(&cpp, &stk[1], sizeof (cpp))) != 0)
     98  1.45  christos 		return error;
     99   1.4  christos 
    100   1.4  christos 	dp = (char *) (cpp + argc + envc + 2);
    101   1.4  christos 	sp = argp;
    102   1.4  christos 
    103   1.4  christos 	/* XXX don't copy them out, remap them! */
    104   1.5   mycroft 	arginfo->ps_argvstr = cpp; /* remember location of argv for later */
    105   1.4  christos 
    106   1.4  christos 	for (; --argc >= 0; sp += len, dp += len)
    107  1.45  christos 		if ((error = copyout(&dp, cpp++, sizeof(dp))) != 0 ||
    108  1.45  christos 		    (error = copyoutstr(sp, dp, ARG_MAX, &len)) != 0)
    109  1.45  christos 			return error;
    110   1.4  christos 
    111  1.45  christos 	if ((error = copyout(&nullp, cpp++, sizeof(nullp))) != 0)
    112  1.45  christos 		return error;
    113   1.4  christos 
    114  1.45  christos 	if ((error = copyout(&cpp, &stk[2], sizeof (cpp))) != 0)
    115  1.45  christos 		return error;
    116   1.4  christos 
    117   1.5   mycroft 	arginfo->ps_envstr = cpp; /* remember location of envp for later */
    118   1.4  christos 
    119   1.4  christos 	for (; --envc >= 0; sp += len, dp += len)
    120  1.45  christos 		if ((error = copyout(&dp, cpp++, sizeof(dp))) != 0 ||
    121  1.45  christos 		    (error = copyoutstr(sp, dp, ARG_MAX, &len)) != 0)
    122  1.45  christos 			return error;
    123   1.4  christos 
    124  1.45  christos 	if ((error = copyout(&nullp, cpp++, sizeof(nullp))) != 0)
    125  1.45  christos 		return error;
    126   1.4  christos 
    127  1.45  christos 	*stackp = (char *)cpp;
    128  1.45  christos 	return 0;
    129   1.4  christos }
    130   1.4  christos 
    131   1.1      fvdl int
    132  1.64       dsl exec_linux_aout_makecmds(struct lwp *l, struct exec_package *epp)
    133   1.1      fvdl {
    134   1.1      fvdl 	struct exec *linux_ep = epp->ep_hdr;
    135   1.1      fvdl 	int machtype, magic;
    136   1.1      fvdl 	int error = ENOEXEC;
    137   1.1      fvdl 
    138   1.1      fvdl 	magic = LINUX_N_MAGIC(linux_ep);
    139   1.1      fvdl 	machtype = LINUX_N_MACHTYPE(linux_ep);
    140   1.1      fvdl 
    141   1.1      fvdl 
    142   1.1      fvdl 	if (machtype != LINUX_MID_MACHINE)
    143   1.1      fvdl 		return (ENOEXEC);
    144   1.1      fvdl 
    145   1.1      fvdl 	switch (magic) {
    146   1.1      fvdl 	case QMAGIC:
    147  1.58  christos 		error = exec_linux_aout_prep_qmagic(l, epp);
    148   1.1      fvdl 		break;
    149   1.1      fvdl 	case ZMAGIC:
    150  1.58  christos 		error = exec_linux_aout_prep_zmagic(l, epp);
    151   1.1      fvdl 		break;
    152   1.1      fvdl 	case NMAGIC:
    153  1.58  christos 		error = exec_linux_aout_prep_nmagic(l, epp);
    154   1.1      fvdl 		break;
    155   1.1      fvdl 	case OMAGIC:
    156  1.58  christos 		error = exec_linux_aout_prep_omagic(l, epp);
    157   1.1      fvdl 		break;
    158   1.1      fvdl 	}
    159   1.1      fvdl 	return error;
    160   1.1      fvdl }
    161   1.1      fvdl 
    162   1.1      fvdl /*
    163   1.1      fvdl  * Since text starts at 0x400 in Linux ZMAGIC executables, and 0x400
    164   1.1      fvdl  * is very likely not page aligned on most architectures, it is treated
    165   1.1      fvdl  * as an NMAGIC here. XXX
    166   1.1      fvdl  */
    167   1.1      fvdl 
    168  1.42  jdolecek static int
    169  1.64       dsl exec_linux_aout_prep_zmagic(struct lwp *l, struct exec_package *epp)
    170   1.1      fvdl {
    171   1.1      fvdl 	struct exec *execp = epp->ep_hdr;
    172   1.1      fvdl 
    173   1.1      fvdl 	epp->ep_taddr = LINUX_N_TXTADDR(*execp, ZMAGIC);
    174   1.1      fvdl 	epp->ep_tsize = execp->a_text;
    175   1.1      fvdl 	epp->ep_daddr = LINUX_N_DATADDR(*execp, ZMAGIC);
    176   1.1      fvdl 	epp->ep_dsize = execp->a_data + execp->a_bss;
    177   1.1      fvdl 	epp->ep_entry = execp->a_entry;
    178   1.1      fvdl 
    179   1.1      fvdl 	/* set up command for text segment */
    180   1.1      fvdl 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, execp->a_text,
    181   1.1      fvdl 	    epp->ep_taddr, epp->ep_vp, LINUX_N_TXTOFF(*execp, ZMAGIC),
    182   1.1      fvdl 	    VM_PROT_READ|VM_PROT_EXECUTE);
    183   1.1      fvdl 
    184   1.1      fvdl 	/* set up command for data segment */
    185   1.1      fvdl 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, execp->a_data,
    186   1.1      fvdl 	    epp->ep_daddr, epp->ep_vp, LINUX_N_DATOFF(*execp, ZMAGIC),
    187   1.1      fvdl 	    VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
    188   1.1      fvdl 
    189   1.1      fvdl 	/* set up command for bss segment */
    190  1.56  christos 	if (execp->a_bss)
    191  1.56  christos 		NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, execp->a_bss,
    192  1.56  christos 		    epp->ep_daddr + execp->a_data, NULLVP, 0,
    193  1.56  christos 		    VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
    194   1.1      fvdl 
    195  1.58  christos 	return (*epp->ep_esch->es_setup_stack)(l, epp);
    196   1.1      fvdl }
    197   1.1      fvdl 
    198   1.1      fvdl /*
    199   1.1      fvdl  * exec_aout_prep_nmagic(): Prepare Linux NMAGIC package.
    200   1.1      fvdl  * Not different from the normal stuff.
    201   1.1      fvdl  */
    202   1.1      fvdl 
    203  1.42  jdolecek static int
    204  1.64       dsl exec_linux_aout_prep_nmagic(struct lwp *l, struct exec_package *epp)
    205   1.1      fvdl {
    206   1.1      fvdl 	struct exec *execp = epp->ep_hdr;
    207   1.1      fvdl 	long bsize, baddr;
    208   1.1      fvdl 
    209   1.1      fvdl 	epp->ep_taddr = LINUX_N_TXTADDR(*execp, NMAGIC);
    210   1.1      fvdl 	epp->ep_tsize = execp->a_text;
    211   1.1      fvdl 	epp->ep_daddr = LINUX_N_DATADDR(*execp, NMAGIC);
    212   1.1      fvdl 	epp->ep_dsize = execp->a_data + execp->a_bss;
    213   1.1      fvdl 	epp->ep_entry = execp->a_entry;
    214   1.1      fvdl 
    215   1.1      fvdl 	/* set up command for text segment */
    216   1.1      fvdl 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, execp->a_text,
    217   1.1      fvdl 	    epp->ep_taddr, epp->ep_vp, LINUX_N_TXTOFF(*execp, NMAGIC),
    218   1.1      fvdl 	    VM_PROT_READ|VM_PROT_EXECUTE);
    219   1.1      fvdl 
    220   1.1      fvdl 	/* set up command for data segment */
    221   1.1      fvdl 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, execp->a_data,
    222   1.1      fvdl 	    epp->ep_daddr, epp->ep_vp, LINUX_N_DATOFF(*execp, NMAGIC),
    223   1.1      fvdl 	    VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
    224   1.1      fvdl 
    225   1.1      fvdl 	/* set up command for bss segment */
    226  1.52   thorpej 	baddr = roundup(epp->ep_daddr + execp->a_data, PAGE_SIZE);
    227   1.1      fvdl 	bsize = epp->ep_daddr + epp->ep_dsize - baddr;
    228   1.1      fvdl 	if (bsize > 0)
    229   1.1      fvdl 		NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, bsize, baddr,
    230   1.1      fvdl 		    NULLVP, 0, VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
    231   1.1      fvdl 
    232  1.58  christos 	return (*epp->ep_esch->es_setup_stack)(l, epp);
    233   1.1      fvdl }
    234   1.1      fvdl 
    235   1.1      fvdl /*
    236   1.1      fvdl  * exec_aout_prep_omagic(): Prepare Linux OMAGIC package.
    237   1.1      fvdl  * Business as usual.
    238   1.1      fvdl  */
    239   1.1      fvdl 
    240  1.42  jdolecek static int
    241  1.64       dsl exec_linux_aout_prep_omagic(struct lwp *l, struct exec_package *epp)
    242   1.1      fvdl {
    243   1.1      fvdl 	struct exec *execp = epp->ep_hdr;
    244   1.1      fvdl 	long dsize, bsize, baddr;
    245   1.1      fvdl 
    246   1.1      fvdl 	epp->ep_taddr = LINUX_N_TXTADDR(*execp, OMAGIC);
    247   1.1      fvdl 	epp->ep_tsize = execp->a_text;
    248   1.1      fvdl 	epp->ep_daddr = LINUX_N_DATADDR(*execp, OMAGIC);
    249   1.1      fvdl 	epp->ep_dsize = execp->a_data + execp->a_bss;
    250   1.1      fvdl 	epp->ep_entry = execp->a_entry;
    251   1.1      fvdl 
    252   1.1      fvdl 	/* set up command for text and data segments */
    253   1.1      fvdl 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn,
    254   1.1      fvdl 	    execp->a_text + execp->a_data, epp->ep_taddr, epp->ep_vp,
    255   1.1      fvdl 	    LINUX_N_TXTOFF(*execp, OMAGIC), VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
    256   1.1      fvdl 
    257   1.1      fvdl 	/* set up command for bss segment */
    258  1.52   thorpej 	baddr = roundup(epp->ep_daddr + execp->a_data, PAGE_SIZE);
    259   1.1      fvdl 	bsize = epp->ep_daddr + epp->ep_dsize - baddr;
    260   1.1      fvdl 	if (bsize > 0)
    261   1.1      fvdl 		NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, bsize, baddr,
    262   1.1      fvdl 		    NULLVP, 0, VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
    263   1.1      fvdl 
    264   1.1      fvdl 	/*
    265   1.1      fvdl 	 * Make sure (# of pages) mapped above equals (vm_tsize + vm_dsize);
    266   1.1      fvdl 	 * obreak(2) relies on this fact. Both `vm_tsize' and `vm_dsize' are
    267   1.1      fvdl 	 * computed (in execve(2)) by rounding *up* `ep_tsize' and `ep_dsize'
    268   1.1      fvdl 	 * respectively to page boundaries.
    269   1.1      fvdl 	 * Compensate `ep_dsize' for the amount of data covered by the last
    270  1.57     perry 	 * text page.
    271   1.1      fvdl 	 */
    272  1.52   thorpej 	dsize = epp->ep_dsize + execp->a_text - roundup(execp->a_text,
    273  1.52   thorpej 							PAGE_SIZE);
    274   1.1      fvdl 	epp->ep_dsize = (dsize > 0) ? dsize : 0;
    275  1.58  christos 	return (*epp->ep_esch->es_setup_stack)(l, epp);
    276   1.1      fvdl }
    277   1.1      fvdl 
    278  1.42  jdolecek static int
    279  1.64       dsl exec_linux_aout_prep_qmagic(struct lwp *l, struct exec_package *epp)
    280   1.1      fvdl {
    281   1.1      fvdl 	struct exec *execp = epp->ep_hdr;
    282  1.50       chs 	int error;
    283   1.1      fvdl 
    284   1.1      fvdl 	epp->ep_taddr = LINUX_N_TXTADDR(*execp, QMAGIC);
    285   1.1      fvdl 	epp->ep_tsize = execp->a_text;
    286   1.1      fvdl 	epp->ep_daddr = LINUX_N_DATADDR(*execp, QMAGIC);
    287   1.1      fvdl 	epp->ep_dsize = execp->a_data + execp->a_bss;
    288   1.1      fvdl 	epp->ep_entry = execp->a_entry;
    289   1.1      fvdl 
    290  1.50       chs 	error = vn_marktext(epp->ep_vp);
    291  1.50       chs 	if (error)
    292  1.50       chs 		return (error);
    293   1.1      fvdl 
    294   1.1      fvdl 	/* set up command for text segment */
    295   1.1      fvdl 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_pagedvn, execp->a_text,
    296   1.1      fvdl 	    epp->ep_taddr, epp->ep_vp, LINUX_N_TXTOFF(*execp, QMAGIC),
    297   1.1      fvdl 	    VM_PROT_READ|VM_PROT_EXECUTE);
    298   1.1      fvdl 
    299   1.1      fvdl 	/* set up command for data segment */
    300   1.1      fvdl 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_pagedvn, execp->a_data,
    301   1.1      fvdl 	    epp->ep_daddr, epp->ep_vp, LINUX_N_DATOFF(*execp, QMAGIC),
    302   1.1      fvdl 	    VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
    303   1.1      fvdl 
    304   1.1      fvdl 	/* set up command for bss segment */
    305  1.56  christos 	if (execp->a_bss)
    306  1.56  christos 		NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, execp->a_bss,
    307  1.56  christos 		    epp->ep_daddr + execp->a_data, NULLVP, 0,
    308  1.56  christos 		    VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
    309   1.1      fvdl 
    310  1.58  christos 	return (*epp->ep_esch->es_setup_stack)(l, epp);
    311   1.1      fvdl }
    312