1 /* $NetBSD: linux_exec_aout.c,v 1.37 1999/02/09 20:37:19 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 <vm/vm.h> 58 #include <vm/vm_param.h> 59 #include <vm/vm_map.h> 60 61 #include <machine/cpu.h> 62 #include <machine/reg.h> 63 64 #include <compat/linux/common/linux_types.h> 65 #include <compat/linux/common/linux_signal.h> 66 #include <compat/linux/common/linux_util.h> 67 #include <compat/linux/common/linux_exec.h> 68 #include <compat/linux/common/linux_machdep.h> 69 #include <compat/linux/common/linux_errno.h> 70 71 #include <compat/linux/linux_syscallargs.h> 72 #include <compat/linux/linux_syscall.h> 73 74 75 static void *linux_aout_copyargs __P((struct exec_package *, 76 struct ps_strings *, void *, void *)); 77 78 #define LINUX_AOUT_AUX_ARGSIZ 2 79 80 extern char linux_sigcode[], linux_esigcode[]; 81 extern struct sysent linux_sysent[]; 82 extern char *linux_syscallnames[]; 83 84 int exec_linux_aout_prep_zmagic __P((struct proc *, struct exec_package *)); 85 int exec_linux_aout_prep_nmagic __P((struct proc *, struct exec_package *)); 86 int exec_linux_aout_prep_omagic __P((struct proc *, struct exec_package *)); 87 int exec_linux_aout_prep_qmagic __P((struct proc *, struct exec_package *)); 88 89 struct emul emul_linux_aout = { 90 "linux", 91 native_to_linux_errno, 92 linux_sendsig, 93 LINUX_SYS_syscall, 94 LINUX_SYS_MAXSYSCALL, 95 linux_sysent, 96 linux_syscallnames, 97 LINUX_AOUT_AUX_ARGSIZ, 98 linux_aout_copyargs, 99 linux_setregs, 100 linux_sigcode, 101 linux_esigcode, 102 }; 103 104 static void * 105 linux_aout_copyargs(pack, arginfo, stack, argp) 106 struct exec_package *pack; 107 struct ps_strings *arginfo; 108 void *stack; 109 void *argp; 110 { 111 char **cpp = stack; 112 char **stk = stack; 113 char *dp, *sp; 114 size_t len; 115 void *nullp = NULL; 116 int argc = arginfo->ps_nargvstr; 117 int envc = arginfo->ps_nenvstr; 118 119 if (copyout(&argc, cpp++, sizeof(argc))) 120 return NULL; 121 122 /* leave room for envp and argv */ 123 cpp += 2; 124 if (copyout(&cpp, &stk[1], sizeof (cpp))) 125 return NULL; 126 127 dp = (char *) (cpp + argc + envc + 2); 128 sp = argp; 129 130 /* XXX don't copy them out, remap them! */ 131 arginfo->ps_argvstr = cpp; /* remember location of argv for later */ 132 133 for (; --argc >= 0; sp += len, dp += len) 134 if (copyout(&dp, cpp++, sizeof(dp)) || 135 copyoutstr(sp, dp, ARG_MAX, &len)) 136 return NULL; 137 138 if (copyout(&nullp, cpp++, sizeof(nullp))) 139 return NULL; 140 141 if (copyout(&cpp, &stk[2], sizeof (cpp))) 142 return NULL; 143 144 arginfo->ps_envstr = cpp; /* remember location of envp for later */ 145 146 for (; --envc >= 0; sp += len, dp += len) 147 if (copyout(&dp, cpp++, sizeof(dp)) || 148 copyoutstr(sp, dp, ARG_MAX, &len)) 149 return NULL; 150 151 if (copyout(&nullp, cpp++, sizeof(nullp))) 152 return NULL; 153 154 return cpp; 155 } 156 157 int 158 exec_linux_aout_makecmds(p, epp) 159 struct proc *p; 160 struct exec_package *epp; 161 { 162 struct exec *linux_ep = epp->ep_hdr; 163 int machtype, magic; 164 int error = ENOEXEC; 165 166 magic = LINUX_N_MAGIC(linux_ep); 167 machtype = LINUX_N_MACHTYPE(linux_ep); 168 169 170 if (machtype != LINUX_MID_MACHINE) 171 return (ENOEXEC); 172 173 switch (magic) { 174 case QMAGIC: 175 error = exec_linux_aout_prep_qmagic(p, epp); 176 break; 177 case ZMAGIC: 178 error = exec_linux_aout_prep_zmagic(p, epp); 179 break; 180 case NMAGIC: 181 error = exec_linux_aout_prep_nmagic(p, epp); 182 break; 183 case OMAGIC: 184 error = exec_linux_aout_prep_omagic(p, epp); 185 break; 186 } 187 if (error == 0) 188 epp->ep_emul = &emul_linux_aout; 189 return error; 190 } 191 192 /* 193 * Since text starts at 0x400 in Linux ZMAGIC executables, and 0x400 194 * is very likely not page aligned on most architectures, it is treated 195 * as an NMAGIC here. XXX 196 */ 197 198 int 199 exec_linux_aout_prep_zmagic(p, epp) 200 struct proc *p; 201 struct exec_package *epp; 202 { 203 struct exec *execp = epp->ep_hdr; 204 205 epp->ep_taddr = LINUX_N_TXTADDR(*execp, ZMAGIC); 206 epp->ep_tsize = execp->a_text; 207 epp->ep_daddr = LINUX_N_DATADDR(*execp, ZMAGIC); 208 epp->ep_dsize = execp->a_data + execp->a_bss; 209 epp->ep_entry = execp->a_entry; 210 211 /* set up command for text segment */ 212 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, execp->a_text, 213 epp->ep_taddr, epp->ep_vp, LINUX_N_TXTOFF(*execp, ZMAGIC), 214 VM_PROT_READ|VM_PROT_EXECUTE); 215 216 /* set up command for data segment */ 217 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, execp->a_data, 218 epp->ep_daddr, epp->ep_vp, LINUX_N_DATOFF(*execp, ZMAGIC), 219 VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE); 220 221 /* set up command for bss segment */ 222 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, execp->a_bss, 223 epp->ep_daddr + execp->a_data, NULLVP, 0, 224 VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE); 225 226 return exec_aout_setup_stack(p, epp); 227 } 228 229 /* 230 * exec_aout_prep_nmagic(): Prepare Linux NMAGIC package. 231 * Not different from the normal stuff. 232 */ 233 234 int 235 exec_linux_aout_prep_nmagic(p, epp) 236 struct proc *p; 237 struct exec_package *epp; 238 { 239 struct exec *execp = epp->ep_hdr; 240 long bsize, baddr; 241 242 epp->ep_taddr = LINUX_N_TXTADDR(*execp, NMAGIC); 243 epp->ep_tsize = execp->a_text; 244 epp->ep_daddr = LINUX_N_DATADDR(*execp, NMAGIC); 245 epp->ep_dsize = execp->a_data + execp->a_bss; 246 epp->ep_entry = execp->a_entry; 247 248 /* set up command for text segment */ 249 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, execp->a_text, 250 epp->ep_taddr, epp->ep_vp, LINUX_N_TXTOFF(*execp, NMAGIC), 251 VM_PROT_READ|VM_PROT_EXECUTE); 252 253 /* set up command for data segment */ 254 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, execp->a_data, 255 epp->ep_daddr, epp->ep_vp, LINUX_N_DATOFF(*execp, NMAGIC), 256 VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE); 257 258 /* set up command for bss segment */ 259 baddr = roundup(epp->ep_daddr + execp->a_data, NBPG); 260 bsize = epp->ep_daddr + epp->ep_dsize - baddr; 261 if (bsize > 0) 262 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, bsize, baddr, 263 NULLVP, 0, VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE); 264 265 return exec_aout_setup_stack(p, epp); 266 } 267 268 /* 269 * exec_aout_prep_omagic(): Prepare Linux OMAGIC package. 270 * Business as usual. 271 */ 272 273 int 274 exec_linux_aout_prep_omagic(p, epp) 275 struct proc *p; 276 struct exec_package *epp; 277 { 278 struct exec *execp = epp->ep_hdr; 279 long dsize, bsize, baddr; 280 281 epp->ep_taddr = LINUX_N_TXTADDR(*execp, OMAGIC); 282 epp->ep_tsize = execp->a_text; 283 epp->ep_daddr = LINUX_N_DATADDR(*execp, OMAGIC); 284 epp->ep_dsize = execp->a_data + execp->a_bss; 285 epp->ep_entry = execp->a_entry; 286 287 /* set up command for text and data segments */ 288 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, 289 execp->a_text + execp->a_data, epp->ep_taddr, epp->ep_vp, 290 LINUX_N_TXTOFF(*execp, OMAGIC), VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE); 291 292 /* set up command for bss segment */ 293 baddr = roundup(epp->ep_daddr + execp->a_data, NBPG); 294 bsize = epp->ep_daddr + epp->ep_dsize - baddr; 295 if (bsize > 0) 296 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, bsize, baddr, 297 NULLVP, 0, VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE); 298 299 /* 300 * Make sure (# of pages) mapped above equals (vm_tsize + vm_dsize); 301 * obreak(2) relies on this fact. Both `vm_tsize' and `vm_dsize' are 302 * computed (in execve(2)) by rounding *up* `ep_tsize' and `ep_dsize' 303 * respectively to page boundaries. 304 * Compensate `ep_dsize' for the amount of data covered by the last 305 * text page. 306 */ 307 dsize = epp->ep_dsize + execp->a_text - roundup(execp->a_text, NBPG); 308 epp->ep_dsize = (dsize > 0) ? dsize : 0; 309 return exec_aout_setup_stack(p, epp); 310 } 311 312 int 313 exec_linux_aout_prep_qmagic(p, epp) 314 struct proc *p; 315 struct exec_package *epp; 316 { 317 struct exec *execp = epp->ep_hdr; 318 319 epp->ep_taddr = LINUX_N_TXTADDR(*execp, QMAGIC); 320 epp->ep_tsize = execp->a_text; 321 epp->ep_daddr = LINUX_N_DATADDR(*execp, QMAGIC); 322 epp->ep_dsize = execp->a_data + execp->a_bss; 323 epp->ep_entry = execp->a_entry; 324 325 /* 326 * check if vnode is in open for writing, because we want to 327 * demand-page out of it. if it is, don't do it, for various 328 * reasons 329 */ 330 if ((execp->a_text != 0 || execp->a_data != 0) && 331 epp->ep_vp->v_writecount != 0) { 332 #ifdef DIAGNOSTIC 333 if (epp->ep_vp->v_flag & VTEXT) 334 panic("exec: a VTEXT vnode has writecount != 0\n"); 335 #endif 336 return ETXTBSY; 337 } 338 epp->ep_vp->v_flag |= VTEXT; 339 340 /* set up command for text segment */ 341 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_pagedvn, execp->a_text, 342 epp->ep_taddr, epp->ep_vp, LINUX_N_TXTOFF(*execp, QMAGIC), 343 VM_PROT_READ|VM_PROT_EXECUTE); 344 345 /* set up command for data segment */ 346 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_pagedvn, execp->a_data, 347 epp->ep_daddr, epp->ep_vp, LINUX_N_DATOFF(*execp, QMAGIC), 348 VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE); 349 350 /* set up command for bss segment */ 351 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, execp->a_bss, 352 epp->ep_daddr + execp->a_data, NULLVP, 0, 353 VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE); 354 355 return exec_aout_setup_stack(p, epp); 356 } 357 358 /* 359 * The Linux system call to load shared libraries, a.out version. The 360 * a.out shared libs are just files that are mapped onto a fixed 361 * address in the process' address space. The address is given in 362 * a_entry. Read in the header, set up some VM commands and run them. 363 * 364 * Yes, both text and data are mapped at once, so we're left with 365 * writeable text for the shared libs. The Linux crt0 seemed to break 366 * sometimes when data was mapped seperately. It munmapped a uselib() 367 * of ld.so by hand, which failed with shared text and data for ld.so 368 * Yuck. 369 * 370 * Because of the problem with ZMAGIC executables (text starts 371 * at 0x400 in the file, but needs to be mapped at 0), ZMAGIC 372 * shared libs are not handled very efficiently :-( 373 */ 374 375 int 376 linux_sys_uselib(p, v, retval) 377 struct proc *p; 378 void *v; 379 register_t *retval; 380 { 381 struct linux_sys_uselib_args /* { 382 syscallarg(const char *) path; 383 } */ *uap = v; 384 caddr_t sg; 385 long bsize, dsize, tsize, taddr, baddr, daddr; 386 struct nameidata ni; 387 struct vnode *vp; 388 struct exec hdr; 389 struct exec_vmcmd_set vcset; 390 int i, magic, error; 391 size_t rem; 392 393 sg = stackgap_init(p->p_emul); 394 LINUX_CHECK_ALT_EXIST(p, &sg, SCARG(uap, path)); 395 396 NDINIT(&ni, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p); 397 398 if ((error = namei(&ni))) 399 return error; 400 401 vp = ni.ni_vp; 402 403 if ((error = vn_rdwr(UIO_READ, vp, (caddr_t) &hdr, LINUX_AOUT_HDR_SIZE, 404 0, UIO_SYSSPACE, IO_NODELOCKED, p->p_ucred, 405 &rem, p))) { 406 vrele(vp); 407 return error; 408 } 409 410 if (rem != 0) { 411 vrele(vp); 412 return ENOEXEC; 413 } 414 415 if (LINUX_N_MACHTYPE(&hdr) != LINUX_MID_MACHINE) 416 return ENOEXEC; 417 418 magic = LINUX_N_MAGIC(&hdr); 419 taddr = hdr.a_entry & (~(NBPG - 1)); 420 tsize = hdr.a_text; 421 daddr = taddr + tsize; 422 dsize = hdr.a_data + hdr.a_bss; 423 424 if ((hdr.a_text != 0 || hdr.a_data != 0) && vp->v_writecount != 0) { 425 vrele(vp); 426 return ETXTBSY; 427 } 428 vp->v_flag |= VTEXT; 429 430 vcset.evs_cnt = 0; 431 vcset.evs_used = 0; 432 433 NEW_VMCMD(&vcset, 434 magic == ZMAGIC ? vmcmd_map_readvn : vmcmd_map_pagedvn, 435 hdr.a_text + hdr.a_data, taddr, 436 vp, LINUX_N_TXTOFF(hdr, magic), 437 VM_PROT_READ|VM_PROT_EXECUTE|VM_PROT_WRITE); 438 439 baddr = roundup(daddr + hdr.a_data, NBPG); 440 bsize = daddr + dsize - baddr; 441 if (bsize > 0) { 442 NEW_VMCMD(&vcset, vmcmd_map_zero, bsize, baddr, 443 NULLVP, 0, VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE); 444 } 445 446 for (i = 0; i < vcset.evs_used && !error; i++) { 447 struct exec_vmcmd *vcp; 448 449 vcp = &vcset.evs_cmds[i]; 450 error = (*vcp->ev_proc)(p, vcp); 451 } 452 453 kill_vmcmds(&vcset); 454 455 vrele(vp); 456 457 return error; 458 } 459