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