1 /**	$MirOS: src/sys/compat/linux/linux_exec.c,v 1.2 2005/03/06 21:27:30 tg Exp $ */
2 /*	$OpenBSD: linux_exec.c,v 1.23 2004/04/15 00:22:42 tedu Exp $	*/
3 /*	$NetBSD: linux_exec.c,v 1.13 1996/04/05 00:01:10 christos Exp $	*/
4 
5 /*-
6  * Copyright (c) 1994, 1995, 1998, 2000 The NetBSD Foundation, Inc.
7  * All rights reserved.
8  *
9  * This code is derived from software contributed to The NetBSD Foundation
10  * by Christos Zoulas, Frank van der Linden, Eric Haszlakiewicz and
11  * Thor Lancelot Simon.
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  * 1. Redistributions of source code must retain the above copyright
17  *    notice, this list of conditions and the following disclaimer.
18  * 2. Redistributions in binary form must reproduce the above copyright
19  *    notice, this list of conditions and the following disclaimer in the
20  *    documentation and/or other materials provided with the distribution.
21  * 3. All advertising materials mentioning features or use of this software
22  *    must display the following acknowledgement:
23  *        This product includes software developed by the NetBSD
24  *        Foundation, Inc. and its contributors.
25  * 4. Neither the name of The NetBSD Foundation nor the names of its
26  *    contributors may be used to endorse or promote products derived
27  *    from this software without specific prior written permission.
28  *
29  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
30  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
31  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
32  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
33  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
34  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
35  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
36  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
37  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
39  * POSSIBILITY OF SUCH DAMAGE.
40  */
41 
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/kernel.h>
45 #include <sys/proc.h>
46 #include <sys/malloc.h>
47 #include <sys/namei.h>
48 #include <sys/vnode.h>
49 #include <sys/mount.h>
50 #include <sys/exec.h>
51 #include <sys/exec_elf.h>
52 #include <sys/exec_olf.h>
53 
54 #include <sys/mman.h>
55 #include <sys/syscallargs.h>
56 
57 #include <uvm/uvm_extern.h>
58 
59 #include <machine/cpu.h>
60 #include <machine/reg.h>
61 #include <machine/linux_machdep.h>
62 
63 #include <compat/linux/linux_types.h>
64 #include <compat/linux/linux_syscall.h>
65 #include <compat/linux/linux_signal.h>
66 #include <compat/linux/linux_syscallargs.h>
67 #include <compat/linux/linux_util.h>
68 #include <compat/linux/linux_exec.h>
69 #include <compat/linux/linux_emuldata.h>
70 
71 static void *linux_aout_copyargs(struct exec_package *,
72 	struct ps_strings *, void *, void *);
73 
74 #define	LINUX_AOUT_AUX_ARGSIZ	2
75 #define LINUX_ELF_AUX_ARGSIZ (sizeof(AuxInfo) * 8 / sizeof(char *))
76 
77 
78 const char linux_emul_path[] = "/emul/linux";
79 extern int linux_error[];
80 extern char linux_sigcode[], linux_esigcode[];
81 extern struct sysent linux_sysent[];
82 #ifdef SYSCALL_DEBUG
83 extern char *linux_syscallnames[];
84 #endif
85 
86 int exec_linux_aout_prep_zmagic(struct proc *, struct exec_package *);
87 int exec_linux_aout_prep_nmagic(struct proc *, struct exec_package *);
88 int exec_linux_aout_prep_omagic(struct proc *, struct exec_package *);
89 int exec_linux_aout_prep_qmagic(struct proc *, struct exec_package *);
90 
91 void linux_e_proc_exec(struct proc *, struct exec_package *);
92 void linux_e_proc_fork(struct proc *, struct proc *);
93 void linux_e_proc_exit(struct proc *);
94 void linux_e_proc_init(struct proc *, struct vmspace *);
95 
96 struct emul emul_linux_aout = {
97 	"linux",
98 	linux_error,
99 	linux_sendsig,
100 	LINUX_SYS_syscall,
101 	LINUX_SYS_MAXSYSCALL,
102 	linux_sysent,
103 #ifdef SYSCALL_DEBUG
104 	linux_syscallnames,
105 #else
106 	NULL,
107 #endif
108 	LINUX_AOUT_AUX_ARGSIZ,
109 	linux_aout_copyargs,
110 	setregs,
111 	NULL,
112 	linux_sigcode,
113 	linux_esigcode,
114 	0,
115 	NULL,
116 	linux_e_proc_exec,
117 	linux_e_proc_fork,
118 	linux_e_proc_exit,
119 };
120 
121 struct emul emul_linux_elf = {
122 	"linux",
123 	linux_error,
124 	linux_sendsig,
125 	LINUX_SYS_syscall,
126 	LINUX_SYS_MAXSYSCALL,
127 	linux_sysent,
128 #ifdef SYSCALL_DEBUG
129 	linux_syscallnames,
130 #else
131 	NULL,
132 #endif
133 	LINUX_ELF_AUX_ARGSIZ,
134 	elf32_copyargs,
135 	setregs,
136 	exec_elf32_fixup,
137 	linux_sigcode,
138 	linux_esigcode,
139 	0,
140 	NULL,
141 	linux_e_proc_exec,
142 	linux_e_proc_fork,
143 	linux_e_proc_exit,
144 };
145 
146 /*
147  * Allocate per-process structures. Called when executing Linux
148  * process. We can reuse the old emuldata - if it's not null,
149  * the executed process is of same emulation as original forked one.
150  */
151 void
linux_e_proc_init(p,vmspace)152 linux_e_proc_init(p, vmspace)
153 	struct proc *p;
154 	struct vmspace *vmspace;
155 {
156 	if (!p->p_emuldata) {
157 		/* allocate new Linux emuldata */
158 		MALLOC(p->p_emuldata, void *, sizeof(struct linux_emuldata),
159 		    M_EMULDATA, M_WAITOK);
160 	}
161 
162 	memset(p->p_emuldata, '\0', sizeof(struct linux_emuldata));
163 
164 	/* Set the process idea of the break to the real value */
165 	((struct linux_emuldata *)(p->p_emuldata))->p_break =
166 	    vmspace->vm_daddr + ctob(vmspace->vm_dsize);
167 }
168 
169 void
linux_e_proc_exec(p,epp)170 linux_e_proc_exec(p, epp)
171 	struct proc *p;
172 	struct exec_package *epp;
173 {
174 	/* exec, use our vmspace */
175 	linux_e_proc_init(p, p->p_vmspace);
176 }
177 
178 /*
179  * Emulation per-process exit hook.
180  */
181 void
linux_e_proc_exit(p)182 linux_e_proc_exit(p)
183 	struct proc *p;
184 {
185 	/* free Linux emuldata and set the pointer to null */
186 	FREE(p->p_emuldata, M_EMULDATA);
187 	p->p_emuldata = NULL;
188 }
189 
190 /*
191  * Emulation fork hook.
192  */
193 void
linux_e_proc_fork(p,parent)194 linux_e_proc_fork(p, parent)
195 	struct proc *p, *parent;
196 {
197 	/*
198 	 * It could be desirable to copy some stuff from parent's
199 	 * emuldata. We don't need anything like that for now.
200 	 * So just allocate new emuldata for the new process.
201 	 */
202 	p->p_emuldata = NULL;
203 
204 	/* fork, use parent's vmspace (our vmspace may not be setup yet) */
205 	linux_e_proc_init(p, parent->p_vmspace);
206 }
207 
208 static void *
linux_aout_copyargs(pack,arginfo,stack,argp)209 linux_aout_copyargs(pack, arginfo, stack, argp)
210 	struct exec_package *pack;
211 	struct ps_strings *arginfo;
212 	void *stack;
213 	void *argp;
214 {
215 	char **cpp = stack;
216 	char **stk = stack;
217 	char *dp, *sp;
218 	size_t len;
219 	void *nullp = NULL;
220 	int argc = arginfo->ps_nargvstr;
221 	int envc = arginfo->ps_nenvstr;
222 
223 	if (copyout(&argc, cpp++, sizeof(argc)))
224 		return (NULL);
225 
226 	/* leave room for envp and argv */
227 	cpp += 2;
228 	if (copyout(&cpp, &stk[1], sizeof (cpp)))
229 		return (NULL);
230 
231 	dp = (char *)(cpp + argc + envc + 2);
232 	sp = argp;
233 
234 	/* XXX don't copy them out, remap them! */
235 	arginfo->ps_argvstr = cpp; /* remember location of argv for later */
236 
237 	for (; --argc >= 0; sp += len, dp += len)
238 		if (copyout(&dp, cpp++, sizeof(dp)) ||
239 		    copyoutstr(sp, dp, ARG_MAX, &len))
240 			return (NULL);
241 
242 	if (copyout(&nullp, cpp++, sizeof(nullp)))
243 		return (NULL);
244 
245 	if (copyout(&cpp, &stk[2], sizeof (cpp)))
246 		return (NULL);
247 
248 	arginfo->ps_envstr = cpp; /* remember location of envp for later */
249 
250 	for (; --envc >= 0; sp += len, dp += len)
251 		if (copyout(&dp, cpp++, sizeof(dp)) ||
252 		    copyoutstr(sp, dp, ARG_MAX, &len))
253 			return (NULL);
254 
255 	if (copyout(&nullp, cpp++, sizeof(nullp)))
256 		return (NULL);
257 
258 	return (cpp);
259 }
260 
261 int
exec_linux_aout_makecmds(p,epp)262 exec_linux_aout_makecmds(p, epp)
263 	struct proc *p;
264 	struct exec_package *epp;
265 {
266 	struct exec *linux_ep = epp->ep_hdr;
267 	int machtype, magic;
268 	int error = ENOEXEC;
269 
270 	magic = LINUX_N_MAGIC(linux_ep);
271 	machtype = LINUX_N_MACHTYPE(linux_ep);
272 
273 
274 	if (machtype != LINUX_MID_MACHINE)
275 		return (ENOEXEC);
276 
277 	switch (magic) {
278 	case QMAGIC:
279 		error = exec_linux_aout_prep_qmagic(p, epp);
280 		break;
281 	case ZMAGIC:
282 		error = exec_linux_aout_prep_zmagic(p, epp);
283 		break;
284 	case NMAGIC:
285 		error = exec_linux_aout_prep_nmagic(p, epp);
286 		break;
287 	case OMAGIC:
288 		error = exec_linux_aout_prep_omagic(p, epp);
289 		break;
290 	}
291 	if (error == 0)
292 		epp->ep_emul = &emul_linux_aout;
293 	return (error);
294 }
295 
296 /*
297  * Since text starts at 0x400 in Linux ZMAGIC executables, and 0x400
298  * is very likely not page aligned on most architectures, it is treated
299  * as an NMAGIC here. XXX
300  */
301 
302 int
exec_linux_aout_prep_zmagic(p,epp)303 exec_linux_aout_prep_zmagic(p, epp)
304 	struct proc *p;
305 	struct exec_package *epp;
306 {
307 	struct exec *execp = epp->ep_hdr;
308 
309 	epp->ep_taddr = LINUX_N_TXTADDR(*execp, ZMAGIC);
310 	epp->ep_tsize = execp->a_text;
311 	epp->ep_daddr = LINUX_N_DATADDR(*execp, ZMAGIC);
312 	epp->ep_dsize = execp->a_data + execp->a_bss;
313 	epp->ep_entry = execp->a_entry;
314 
315 	/* set up command for text segment */
316 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, execp->a_text,
317 	    epp->ep_taddr, epp->ep_vp, LINUX_N_TXTOFF(*execp, ZMAGIC),
318 	    VM_PROT_READ|VM_PROT_EXECUTE);
319 
320 	/* set up command for data segment */
321 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, execp->a_data,
322 	    epp->ep_daddr, epp->ep_vp, LINUX_N_DATOFF(*execp, ZMAGIC),
323 	    VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
324 
325 	/* set up command for bss segment */
326 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, execp->a_bss,
327 	    epp->ep_daddr + execp->a_data, NULLVP, 0,
328 	    VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
329 
330 	return (exec_setup_stack(p, epp));
331 }
332 
333 /*
334  * exec_aout_prep_nmagic(): Prepare Linux NMAGIC package.
335  * Not different from the normal stuff.
336  */
337 
338 int
exec_linux_aout_prep_nmagic(p,epp)339 exec_linux_aout_prep_nmagic(p, epp)
340 	struct proc *p;
341 	struct exec_package *epp;
342 {
343 	struct exec *execp = epp->ep_hdr;
344 	long bsize, baddr;
345 
346 	epp->ep_taddr = LINUX_N_TXTADDR(*execp, NMAGIC);
347 	epp->ep_tsize = execp->a_text;
348 	epp->ep_daddr = LINUX_N_DATADDR(*execp, NMAGIC);
349 	epp->ep_dsize = execp->a_data + execp->a_bss;
350 	epp->ep_entry = execp->a_entry;
351 
352 	/* set up command for text segment */
353 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, execp->a_text,
354 	    epp->ep_taddr, epp->ep_vp, LINUX_N_TXTOFF(*execp, NMAGIC),
355 	    VM_PROT_READ|VM_PROT_EXECUTE);
356 
357 	/* set up command for data segment */
358 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, execp->a_data,
359 	    epp->ep_daddr, epp->ep_vp, LINUX_N_DATOFF(*execp, NMAGIC),
360 	    VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
361 
362 	/* set up command for bss segment */
363 	baddr = round_page(epp->ep_daddr + execp->a_data);
364 	bsize = epp->ep_daddr + epp->ep_dsize - baddr;
365 	if (bsize > 0)
366 		NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, bsize, baddr,
367 		    NULLVP, 0, VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
368 
369 	return (exec_setup_stack(p, epp));
370 }
371 
372 /*
373  * exec_aout_prep_omagic(): Prepare Linux OMAGIC package.
374  * Business as usual.
375  */
376 
377 int
exec_linux_aout_prep_omagic(p,epp)378 exec_linux_aout_prep_omagic(p, epp)
379 	struct proc *p;
380 	struct exec_package *epp;
381 {
382 	struct exec *execp = epp->ep_hdr;
383 	long dsize, bsize, baddr;
384 
385 	epp->ep_taddr = LINUX_N_TXTADDR(*execp, OMAGIC);
386 	epp->ep_tsize = execp->a_text;
387 	epp->ep_daddr = LINUX_N_DATADDR(*execp, OMAGIC);
388 	epp->ep_dsize = execp->a_data + execp->a_bss;
389 	epp->ep_entry = execp->a_entry;
390 
391 	/* set up command for text and data segments */
392 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn,
393 	    execp->a_text + execp->a_data, epp->ep_taddr, epp->ep_vp,
394 	    LINUX_N_TXTOFF(*execp, OMAGIC), VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
395 
396 	/* set up command for bss segment */
397 	baddr = round_page(epp->ep_daddr + execp->a_data);
398 	bsize = epp->ep_daddr + epp->ep_dsize - baddr;
399 	if (bsize > 0)
400 		NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, bsize, baddr,
401 		    NULLVP, 0, VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
402 
403 	/*
404 	 * Make sure (# of pages) mapped above equals (vm_tsize + vm_dsize);
405 	 * obreak(2) relies on this fact. Both `vm_tsize' and `vm_dsize' are
406 	 * computed (in execve(2)) by rounding *up* `ep_tsize' and `ep_dsize'
407 	 * respectively to page boundaries.
408 	 * Compensate `ep_dsize' for the amount of data covered by the last
409 	 * text page.
410 	 */
411 	dsize = epp->ep_dsize + execp->a_text - round_page(execp->a_text);
412 	epp->ep_dsize = (dsize > 0) ? dsize : 0;
413 	return (exec_setup_stack(p, epp));
414 }
415 
416 int
exec_linux_aout_prep_qmagic(p,epp)417 exec_linux_aout_prep_qmagic(p, epp)
418 	struct proc *p;
419 	struct exec_package *epp;
420 {
421 	struct exec *execp = epp->ep_hdr;
422 
423 	epp->ep_taddr = LINUX_N_TXTADDR(*execp, QMAGIC);
424 	epp->ep_tsize = execp->a_text;
425 	epp->ep_daddr = LINUX_N_DATADDR(*execp, QMAGIC);
426 	epp->ep_dsize = execp->a_data + execp->a_bss;
427 	epp->ep_entry = execp->a_entry;
428 
429 	/*
430 	 * check if vnode is in open for writing, because we want to
431 	 * demand-page out of it.  if it is, don't do it, for various
432 	 * reasons
433 	 */
434 	if ((execp->a_text != 0 || execp->a_data != 0) &&
435 	    epp->ep_vp->v_writecount != 0) {
436 #ifdef DIAGNOSTIC
437 		if (epp->ep_vp->v_flag & VTEXT)
438 			panic("exec: a VTEXT vnode has writecount != 0");
439 #endif
440 		return (ETXTBSY);
441 	}
442 	vn_marktext(epp->ep_vp);
443 
444 	/* set up command for text segment */
445 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_pagedvn, execp->a_text,
446 	    epp->ep_taddr, epp->ep_vp, LINUX_N_TXTOFF(*execp, QMAGIC),
447 	    VM_PROT_READ|VM_PROT_EXECUTE);
448 
449 	/* set up command for data segment */
450 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_pagedvn, execp->a_data,
451 	    epp->ep_daddr, epp->ep_vp, LINUX_N_DATOFF(*execp, QMAGIC),
452 	    VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
453 
454 	/* set up command for bss segment */
455 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, execp->a_bss,
456 	    epp->ep_daddr + execp->a_data, NULLVP, 0,
457 	    VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
458 
459 	return (exec_setup_stack(p, epp));
460 }
461 
462 int
exec_linux_elf32_makecmds(struct proc * p,struct exec_package * epp)463 exec_linux_elf32_makecmds(struct proc *p, struct exec_package *epp)
464 {
465 	if (!(emul_linux_elf.e_flags & EMUL_ENABLED))
466 		return (ENOEXEC);
467 	return exec_elf32_makecmds(p, epp);
468 }
469 
470 int
linux_elf_probe(p,epp,itp,pos,os)471 linux_elf_probe(p, epp, itp, pos, os)
472 	struct proc *p;
473 	struct exec_package *epp;
474 	char *itp;
475 	u_long *pos;
476 	u_int8_t *os;
477 {
478 	Elf32_Ehdr *eh = epp->ep_hdr;
479 	char *bp, *brand;
480 	int error;
481 	size_t len;
482 
483 	if (!ELFNAME(os_pt_note)(p, epp, epp->ep_hdr, "MirOS Linux", 12, 4))
484 		goto emulated;
485 
486 	brand = elf32_check_brand(eh);
487 	if (brand && strcmp(brand, "Linux"))
488 		return (EINVAL);
489 
490 emulated:
491 	if (itp[0]) {
492 		if ((error = emul_find(p, NULL, linux_emul_path, itp, &bp, 0)))
493 			return (error);
494 		if ((error = copystr(bp, itp, MAXPATHLEN, &len)))
495 			return (error);
496 		free(bp, M_TEMP);
497 	}
498 	epp->ep_emul = &emul_linux_elf;
499 	*pos = ELF32_NO_ADDR;
500 	if (*os == OOS_NULL)
501 		*os = OOS_LINUX;
502 	return (0);
503 }
504 
505 /*
506  * The Linux system call to load shared libraries, a.out version. The
507  * a.out shared libs are just files that are mapped onto a fixed
508  * address in the process' address space. The address is given in
509  * a_entry. Read in the header, set up some VM commands and run them.
510  *
511  * Yes, both text and data are mapped at once, so we're left with
512  * writeable text for the shared libs. The Linux crt0 seemed to break
513  * sometimes when data was mapped separately. It munmapped a uselib()
514  * of ld.so by hand, which failed with shared text and data for ld.so
515  * Yuck.
516  *
517  * Because of the problem with ZMAGIC executables (text starts
518  * at 0x400 in the file, but needs to be mapped at 0), ZMAGIC
519  * shared libs are not handled very efficiently :-(
520  */
521 
522 int
linux_sys_uselib(p,v,retval)523 linux_sys_uselib(p, v, retval)
524 	struct proc *p;
525 	void *v;
526 	register_t *retval;
527 {
528 	struct linux_sys_uselib_args /* {
529 		syscallarg(char *) path;
530 	} */ *uap = v;
531 	caddr_t sg;
532 	long bsize, dsize, tsize, taddr, baddr, daddr;
533 	struct nameidata ni;
534 	struct vnode *vp;
535 	struct exec hdr;
536 	struct exec_vmcmd_set vcset;
537 	int i, magic, error;
538 	size_t rem;
539 
540 	sg = stackgap_init(p->p_emul);
541 	LINUX_CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
542 
543 	NDINIT(&ni, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
544 
545 	if ((error = namei(&ni)))
546 		return (error);
547 
548 	vp = ni.ni_vp;
549 
550 	if ((error = vn_rdwr(UIO_READ, vp, (caddr_t) &hdr, LINUX_AOUT_HDR_SIZE,
551 			     0, UIO_SYSSPACE, IO_NODELOCKED, p->p_ucred,
552 			     &rem, p))) {
553 		vrele(vp);
554 		return (error);
555 	}
556 
557 	if (rem != 0) {
558 		vrele(vp);
559 		return (ENOEXEC);
560 	}
561 
562 	if (LINUX_N_MACHTYPE(&hdr) != LINUX_MID_MACHINE)
563 		return (ENOEXEC);
564 
565 	magic = LINUX_N_MAGIC(&hdr);
566 	taddr = trunc_page(hdr.a_entry);
567 	tsize = hdr.a_text;
568 	daddr = taddr + tsize;
569 	dsize = hdr.a_data + hdr.a_bss;
570 
571 	if ((hdr.a_text != 0 || hdr.a_data != 0) && vp->v_writecount != 0) {
572 		vrele(vp);
573                 return (ETXTBSY);
574         }
575 	vn_marktext(vp);
576 
577 	VMCMDSET_INIT(&vcset);
578 
579 	NEW_VMCMD(
580 	    &vcset, magic == ZMAGIC ? vmcmd_map_readvn : vmcmd_map_pagedvn,
581 	    hdr.a_text + hdr.a_data, taddr, vp, LINUX_N_TXTOFF(hdr, magic),
582 	    VM_PROT_READ|VM_PROT_EXECUTE|VM_PROT_WRITE);
583 
584 	baddr = round_page(daddr + hdr.a_data);
585 	bsize = daddr + dsize - baddr;
586         if (bsize > 0) {
587                 NEW_VMCMD(&vcset, vmcmd_map_zero, bsize, baddr,
588                     NULLVP, 0, VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
589 	}
590 
591 	for (i = 0; i < vcset.evs_used && !error; i++) {
592 		struct exec_vmcmd *vcp;
593 
594 		vcp = &vcset.evs_cmds[i];
595 		error = (*vcp->ev_proc)(p, vcp);
596 	}
597 
598 	kill_vmcmds(&vcset);
599 
600 	vrele(vp);
601 
602 	return (error);
603 }
604 
605 /*
606  * Execve(2). Just check the alternate emulation path, and pass it on
607  * to the regular execve().
608  */
609 int
linux_sys_execve(p,v,retval)610 linux_sys_execve(p, v, retval)
611 	struct proc *p;
612 	void *v;
613 	register_t *retval;
614 {
615 	struct linux_sys_execve_args /* {
616 		syscallarg(char *) path;
617 		syscallarg(char **) argv;
618 		syscallarg(char **) envp;
619         } */ *uap = v;
620 	struct sys_execve_args ap;
621 	caddr_t sg;
622 
623 	sg = stackgap_init(p->p_emul);
624 	LINUX_CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
625 
626 	SCARG(&ap, path) = SCARG(uap, path);
627 	SCARG(&ap, argp) = SCARG(uap, argp);
628 	SCARG(&ap, envp) = SCARG(uap, envp);
629 
630 	return (sys_execve(p, &ap, retval));
631 }
632