xref: /csrg-svn/sys/kern/kern_exec.c (revision 59378)
1 /*-
2  * Copyright (c) 1982, 1986, 1991 The Regents of the University of California.
3  * All rights reserved.
4  *
5  * %sccs.include.proprietary.c%
6  *
7  *	@(#)kern_exec.c	7.74 (Berkeley) 04/27/93
8  */
9 
10 #include <sys/param.h>
11 #include <sys/systm.h>
12 #include <sys/filedesc.h>
13 #include <sys/kernel.h>
14 #include <sys/proc.h>
15 #include <sys/mount.h>
16 #include <sys/malloc.h>
17 #include <sys/namei.h>
18 #include <sys/vnode.h>
19 #include <sys/file.h>
20 #include <sys/acct.h>
21 #include <sys/exec.h>
22 #include <sys/ktrace.h>
23 #include <sys/resourcevar.h>
24 
25 #include <machine/cpu.h>
26 #include <machine/reg.h>
27 
28 #include <sys/mman.h>
29 #include <vm/vm.h>
30 #include <vm/vm_param.h>
31 #include <vm/vm_map.h>
32 #include <vm/vm_kern.h>
33 #include <vm/vm_pager.h>
34 
35 #include <sys/signalvar.h>
36 
37 #ifdef HPUXCOMPAT
38 #include <sys/user.h>			/* for pcb */
39 #include <hp/hpux/hpux_exec.h>
40 #endif
41 
42 #ifdef COPY_SIGCODE
43 extern char sigcode[], esigcode[];
44 #define	szsigcode	(esigcode - sigcode)
45 #else
46 #define	szsigcode	0
47 #endif
48 
49 /*
50  * exec system call
51  */
52 struct execve_args {
53 	char	*fname;
54 	char	**argp;
55 	char	**envp;
56 };
57 /* ARGSUSED */
58 execve(p, uap, retval)
59 	register struct proc *p;
60 	register struct execve_args *uap;
61 	int *retval;
62 {
63 	register struct ucred *cred = p->p_ucred;
64 	register struct filedesc *fdp = p->p_fd;
65 	int na, ne, ucp, ap, cc, ssize;
66 	register char *cp;
67 	register int nc;
68 	unsigned len;
69 	int indir, uid, gid;
70 	char *sharg;
71 	struct vnode *vp;
72 	int resid, error, paged = 0;
73 	vm_offset_t execargs = 0;
74 	struct vattr vattr;
75 	char cfarg[MAXINTERP];
76 	union {
77 		char	ex_shell[MAXINTERP];	/* #! and interpreter name */
78 		struct	exec ex_exec;
79 #ifdef HPUXCOMPAT
80 		struct	hpux_exec ex_hexec;
81 #endif
82 	} exdata;
83 #ifdef HPUXCOMPAT
84 	struct hpux_exec hhead;
85 #endif
86 	struct nameidata nd;
87 	struct ps_strings ps;
88 
89 	NDINIT(&nd, LOOKUP, FOLLOW | SAVENAME, UIO_USERSPACE,
90 		uap->fname, p);
91 	if (error = namei(&nd))
92 		return (error);
93 	vp = nd.ni_vp;
94 	LEASE_CHECK(vp, p, cred, LEASE_READ);
95 	VOP_LOCK(vp);
96 	indir = 0;
97 	uid = cred->cr_uid;
98 	gid = cred->cr_gid;
99 	if (error = VOP_GETATTR(vp, &vattr, cred, p))
100 		goto bad;
101 	if (vp->v_mount->mnt_flag & MNT_NOEXEC) {
102 		error = EACCES;
103 		goto bad;
104 	}
105 	if ((vp->v_mount->mnt_flag & MNT_NOSUID) == 0) {
106 		if (vattr.va_mode & VSUID)
107 			uid = vattr.va_uid;
108 		if (vattr.va_mode & VSGID)
109 			gid = vattr.va_gid;
110 	}
111 
112   again:
113 	if (error = VOP_ACCESS(vp, VEXEC, cred, p))
114 		goto bad;
115 	if ((p->p_flag & STRC) && (error = VOP_ACCESS(vp, VREAD, cred, p)))
116 		goto bad;
117 	if (vp->v_type != VREG ||
118 	    (vattr.va_mode & (VEXEC|(VEXEC>>3)|(VEXEC>>6))) == 0) {
119 		error = EACCES;
120 		goto bad;
121 	}
122 
123 	/*
124 	 * Read in first few bytes of file for segment sizes, magic number:
125 	 *	OMAGIC = plain executable
126 	 *	NMAGIC = RO text
127 	 *	ZMAGIC = demand paged RO text
128 	 * Also an ASCII line beginning with #! is
129 	 * the file name of a ``shell'' and arguments may be prepended
130 	 * to the argument list if given here.
131 	 *
132 	 * SHELL NAMES ARE LIMITED IN LENGTH.
133 	 *
134 	 * ONLY ONE ARGUMENT MAY BE PASSED TO THE SHELL FROM
135 	 * THE ASCII LINE.
136 	 */
137 	exdata.ex_shell[0] = '\0';	/* for zero length files */
138 	error = vn_rdwr(UIO_READ, vp, (caddr_t)&exdata, sizeof (exdata),
139 	    (off_t)0, UIO_SYSSPACE, (IO_UNIT|IO_NODELOCKED), cred, &resid,
140 	    (struct proc *)0);
141 	if (error)
142 		goto bad;
143 #ifndef lint
144 	if (resid > sizeof(exdata) - sizeof(exdata.ex_exec) &&
145 	    exdata.ex_shell[0] != '#') {
146 		error = ENOEXEC;
147 		goto bad;
148 	}
149 #endif
150 #if defined(hp300) || defined(luna68k)
151 	switch ((int)exdata.ex_exec.a_mid) {
152 
153 	/*
154 	 * An ancient hp200 or hp300 binary, shouldn't happen anymore.
155 	 * Mark as invalid.
156 	 */
157 	case MID_ZERO:
158 		exdata.ex_exec.a_magic = 0;
159 		break;
160 
161 	/*
162 	 * HP200 series has a smaller page size so we cannot
163 	 * demand-load or even write protect text, so we just
164 	 * treat as OMAGIC.
165 	 */
166 	case MID_HP200:
167 		exdata.ex_exec.a_magic = OMAGIC;
168 		break;
169 
170 	case MID_HP300:
171 		break;
172 
173 #ifdef HPUXCOMPAT
174 	case MID_HPUX:
175 		/*
176 		 * Save a.out header.  This is eventually saved in the pcb,
177 		 * but we cannot do that yet in case the exec fails before
178 		 * the image is overlayed.
179 		 */
180 		bcopy((caddr_t)&exdata.ex_hexec,
181 		      (caddr_t)&hhead, sizeof hhead);
182 		/*
183 		 * Shuffle important fields to their BSD locations.
184 		 * Note that the order in which this is done is important.
185 		 */
186 		exdata.ex_exec.a_text = exdata.ex_hexec.ha_text;
187 		exdata.ex_exec.a_data = exdata.ex_hexec.ha_data;
188 		exdata.ex_exec.a_bss = exdata.ex_hexec.ha_bss;
189 		exdata.ex_exec.a_entry = exdata.ex_hexec.ha_entry;
190 		/*
191 		 * For ZMAGIC files, make sizes consistant with those
192 		 * generated by BSD ld.
193 		 */
194 		if (exdata.ex_exec.a_magic == ZMAGIC) {
195 			exdata.ex_exec.a_text =
196 				ctob(btoc(exdata.ex_exec.a_text));
197 			nc = exdata.ex_exec.a_data + exdata.ex_exec.a_bss;
198 			exdata.ex_exec.a_data =
199 				ctob(btoc(exdata.ex_exec.a_data));
200 			nc -= (int)exdata.ex_exec.a_data;
201 			exdata.ex_exec.a_bss = (nc < 0) ? 0 : nc;
202 		}
203 		break;
204 #endif
205 	}
206 #endif
207 	switch ((int)exdata.ex_exec.a_magic) {
208 
209 	case OMAGIC:
210 #ifdef COFF
211 		if (exdata.ex_exec.ex_fhdr.magic != COFF_MAGIC) {
212 			error = ENOEXEC;
213 			goto bad;
214 		}
215 #endif
216 #ifdef sparc
217 		if (exdata.ex_exec.a_mid != MID_SUN_SPARC) {
218 			error = ENOEXEC;
219 			goto bad;
220 		}
221 #endif
222 		exdata.ex_exec.a_data += exdata.ex_exec.a_text;
223 		exdata.ex_exec.a_text = 0;
224 		break;
225 
226 	case ZMAGIC:
227 		paged = 1;
228 		/* FALLTHROUGH */
229 
230 	case NMAGIC:
231 #ifdef COFF
232 		if (exdata.ex_exec.ex_fhdr.magic != COFF_MAGIC) {
233 			error = ENOEXEC;
234 			goto bad;
235 		}
236 #endif
237 #ifdef sparc
238 		if (exdata.ex_exec.a_mid != MID_SUN_SPARC) {
239 			error = ENOEXEC;
240 			goto bad;
241 		}
242 #endif
243 		if (exdata.ex_exec.a_text == 0) {
244 			error = ENOEXEC;
245 			goto bad;
246 		}
247 		break;
248 
249 	default:
250 		if (exdata.ex_shell[0] != '#' ||
251 		    exdata.ex_shell[1] != '!' ||
252 		    indir) {
253 			error = ENOEXEC;
254 			goto bad;
255 		}
256 		for (cp = &exdata.ex_shell[2];; ++cp) {
257 			if (cp >= &exdata.ex_shell[MAXINTERP]) {
258 				error = ENOEXEC;
259 				goto bad;
260 			}
261 			if (*cp == '\n') {
262 				*cp = '\0';
263 				break;
264 			}
265 			if (*cp == '\t')
266 				*cp = ' ';
267 		}
268 		cp = &exdata.ex_shell[2];
269 		while (*cp == ' ')
270 			cp++;
271 		nd.ni_dirp = cp;
272 		while (*cp && *cp != ' ')
273 			cp++;
274 		cfarg[0] = '\0';
275 		if (*cp) {
276 			*cp++ = '\0';
277 			while (*cp == ' ')
278 				cp++;
279 			if (*cp)
280 				bcopy((caddr_t)cp, (caddr_t)cfarg, MAXINTERP);
281 		}
282 		indir = 1;
283 		vput(vp);
284 		nd.ni_segflg = UIO_SYSSPACE;
285 		if (error = namei(&nd))
286 			return (error);
287 		vp = nd.ni_vp;
288 		if (error = VOP_GETATTR(vp, &vattr, cred, p))
289 			goto bad;
290 		uid = cred->cr_uid;	/* shell scripts can't be setuid */
291 		gid = cred->cr_gid;
292 		goto again;
293 	}
294 
295 	/*
296 	 * Collect arguments on "file" in swap space.
297 	 */
298 	na = 0;
299 	ne = 0;
300 	nc = 0;
301 	cc = NCARGS;
302 	execargs = kmem_alloc_wait(exec_map, NCARGS);
303 #ifdef DIAGNOSTIC
304 	if (execargs == (vm_offset_t)0)
305 		panic("execve: kmem_alloc_wait");
306 #endif
307 	cp = (char *) execargs;
308 	/*
309 	 * Copy arguments into file in argdev area.
310 	 */
311 	if (uap->argp) for (;;) {
312 		ap = NULL;
313 		sharg = NULL;
314 		if (indir && na == 0) {
315 			sharg = nd.ni_cnd.cn_nameptr;
316 			ap = (int)sharg;
317 			uap->argp++;		/* ignore argv[0] */
318 		} else if (indir && (na == 1 && cfarg[0])) {
319 			sharg = cfarg;
320 			ap = (int)sharg;
321 		} else if (indir && (na == 1 || na == 2 && cfarg[0]))
322 			ap = (int)uap->fname;
323 		else if (uap->argp) {
324 			ap = fuword((caddr_t)uap->argp);
325 			uap->argp++;
326 		}
327 		if (ap == NULL && uap->envp) {
328 			uap->argp = NULL;
329 			if ((ap = fuword((caddr_t)uap->envp)) != NULL)
330 				uap->envp++, ne++;
331 		}
332 		if (ap == NULL)
333 			break;
334 		na++;
335 		if (ap == -1) {
336 			error = EFAULT;
337 			goto bad;
338 		}
339 		do {
340 			if (nc >= NCARGS-1) {
341 				error = E2BIG;
342 				break;
343 			}
344 			if (sharg) {
345 				error = copystr(sharg, cp, (unsigned)cc, &len);
346 				sharg += len;
347 			} else {
348 				error = copyinstr((caddr_t)ap, cp, (unsigned)cc,
349 				    &len);
350 				ap += len;
351 			}
352 			cp += len;
353 			nc += len;
354 			cc -= len;
355 		} while (error == ENAMETOOLONG);
356 		if (error)
357 			goto bad;
358 	}
359 
360 	/*
361 	 * XXX the following is excessively bogus
362 	 *
363 	 * Compute initial process stack size and location of argc
364 	 * and character strings.  `nc' is currently just the number
365 	 * of characters of arg and env strings.
366 	 *
367 	 * nc = size of ps_strings structure +
368 	 *	size of signal code +
369 	 *	4 bytes of NULL pointer +
370 	 *	nc,
371 	 * rounded to nearest integer;
372 	 * ucp = USRSTACK - nc;		[user characters pointer]
373 	 * apsize = padding (if any) +
374 	 *	4 bytes of NULL pointer +
375 	 *	ne 4-byte pointers to env strings +
376 	 *	4 bytes of NULL pointer +
377 	 *	(na-ne) 4-byte pointers to arg strings +
378 	 *	4 bytes of argc;
379 	 * (this is the same as nc + (na+3)*4)
380 	 * ap = ucp - apsize;	[user address of argc]
381 	 * ssize = ssize + nc + machine-dependent space;
382 	 */
383 	nc = (sizeof(ps) + szsigcode + 4 + nc + NBPW-1) & ~(NBPW - 1);
384 #if defined(sparc) || defined(mips)
385 	ucp = USRSTACK;
386 	ssize = ALIGN(nc + (na + 3) * NBPW);
387 	ap = ucp - ssize;
388 	ucp -= nc;
389 #ifdef sparc
390 	ssize += sizeof(struct rwindow);
391 #endif
392 #else
393 	ssize = (na + 3) * NBPW;
394 	ucp = USRSTACK - nc;
395 	ap = ucp - ssize;
396 	ssize += nc;
397 #endif
398 	error = getxfile(p, vp, &exdata.ex_exec, paged, ssize, uid, gid);
399 	if (error)
400 		goto bad;
401 	vput(vp);
402 	vp = NULL;
403 
404 #ifdef HPUXCOMPAT
405 	/*
406 	 * We are now committed to the exec so we can save the exec
407 	 * header in the pcb where we can dump it if necessary in core()
408 	 */
409 	if (p->p_md.md_flags & MDP_HPUX)
410 		bcopy((caddr_t)&hhead,
411 		      (caddr_t)p->p_addr->u_md.md_exec, sizeof hhead);
412 #endif
413 
414 	/*
415 	 * Copy back arglist.
416 	 */
417 	cpu_setstack(p, ap);
418 	(void) suword((caddr_t)ap, na-ne);
419 	nc = 0;
420 	cp = (char *) execargs;
421 	cc = NCARGS;
422 	ps.ps_argvstr = (char *)ucp;	/* first argv string */
423 	ps.ps_nargvstr = na - ne;	/* argc */
424 	for (;;) {
425 		ap += NBPW;
426 		if (na == ne) {
427 			(void) suword((caddr_t)ap, 0);
428 			ap += NBPW;
429 			ps.ps_envstr = (char *)ucp;
430 			ps.ps_nenvstr = ne;
431 		}
432 		if (--na < 0)
433 			break;
434 		(void) suword((caddr_t)ap, ucp);
435 		do {
436 			error = copyoutstr(cp, (caddr_t)ucp, (unsigned)cc,
437 			    &len);
438 			ucp += len;
439 			cp += len;
440 			nc += len;
441 			cc -= len;
442 		} while (error == ENAMETOOLONG);
443 		if (error == EFAULT)
444 			panic("exec: EFAULT");
445 	}
446 	(void) suword((caddr_t)ap, 0);
447 	(void) copyout((caddr_t)&ps, (caddr_t)PS_STRINGS, sizeof(ps));
448 
449 	execsigs(p);
450 
451 	for (nc = fdp->fd_lastfile; nc >= 0; --nc) {
452 		if (fdp->fd_ofileflags[nc] & UF_EXCLOSE) {
453 			(void) closef(fdp->fd_ofiles[nc], p);
454 			fdp->fd_ofiles[nc] = NULL;
455 			fdp->fd_ofileflags[nc] = 0;
456 			if (nc < fdp->fd_freefile)
457 				fdp->fd_freefile = nc;
458 		}
459 		fdp->fd_ofileflags[nc] &= ~UF_MAPPED;
460 	}
461 	/*
462 	 * Adjust fd_lastfile to account for descriptors closed above.
463 	 * Don't decrement fd_lastfile past 0, as it's unsigned.
464 	 */
465 	while (fdp->fd_lastfile > 0 && fdp->fd_ofiles[fdp->fd_lastfile] == NULL)
466 		fdp->fd_lastfile--;
467 	setregs(p, exdata.ex_exec.a_entry, retval);
468 #ifdef COPY_SIGCODE
469 	/*
470 	 * Install sigcode at top of user stack.
471 	 */
472 	copyout((caddr_t)sigcode, (caddr_t)PS_STRINGS - szsigcode, szsigcode);
473 #endif
474 	/*
475 	 * Remember file name for accounting.
476 	 */
477 	p->p_acflag &= ~AFORK;
478 	if (nd.ni_cnd.cn_namelen > MAXCOMLEN)
479 		nd.ni_cnd.cn_namelen = MAXCOMLEN;
480 	bcopy((caddr_t)nd.ni_cnd.cn_nameptr, (caddr_t)p->p_comm,
481 	    (unsigned)nd.ni_cnd.cn_namelen);
482 	p->p_comm[nd.ni_cnd.cn_namelen] = '\0';
483 	cpu_exec(p);
484 bad:
485 	FREE(nd.ni_cnd.cn_pnbuf, M_NAMEI);
486 	if (execargs)
487 		kmem_free_wakeup(exec_map, execargs, NCARGS);
488 	if (vp)
489 		vput(vp);
490 	return (error);
491 }
492 
493 /*
494  * Read in and set up memory for executed file.
495  */
496 getxfile(p, vp, ep, paged, ssize, uid, gid)
497 	register struct proc *p;
498 	register struct vnode *vp;
499 	register struct exec *ep;
500 	int paged, ssize, uid, gid;
501 {
502 	register struct ucred *cred = p->p_ucred;
503 	register struct vmspace *vm = p->p_vmspace;
504 	vm_offset_t addr;
505 	vm_size_t xts, size;
506 	segsz_t ds;
507 	off_t toff;
508 	int error = 0;
509 
510 #ifdef HPUXCOMPAT
511 	if (ep->a_mid == MID_HPUX)
512 		toff = paged ? CLBYTES : sizeof(struct hpux_exec);
513 	else
514 #endif
515 #ifdef COFF
516 	toff = N_TXTOFF(*ep);
517 #else
518 #ifdef sparc
519 	if (ep->a_mid == MID_SUN_SPARC)
520 		toff = paged ? 0 : sizeof(struct exec);
521 	else
522 #endif
523 	if (paged)
524 #ifdef mips
525 		toff = 0;
526 #else
527 		toff = CLBYTES;
528 #endif
529 	else
530 		toff = sizeof (struct exec);
531 #endif
532 	if (ep->a_text != 0 && (vp->v_flag & VTEXT) == 0 &&
533 	    vp->v_writecount != 0)
534 		return (ETXTBSY);
535 
536 	/*
537 	 * Compute text and data sizes and make sure not too large.
538 	 * Text size is rounded to an ``ld page''; data+bss is left
539 	 * in machine pages.  Check data and bss separately as they
540 	 * may overflow when summed together.  (XXX not done yet)
541 	 */
542 	xts = roundup(ep->a_text, __LDPGSZ);
543 	ds = clrnd(btoc(ep->a_data + ep->a_bss));
544 
545 	/*
546 	 * If we're sharing the address space, allocate a new space
547 	 * and release our reference to the old one.  Otherwise,
548 	 * empty out the existing vmspace.
549 	 */
550 #ifdef sparc
551 	kill_user_windows(p);		/* before addrs go away */
552 #endif
553 	if (vm->vm_refcnt > 1) {
554 		p->p_vmspace = vmspace_alloc(VM_MIN_ADDRESS,
555 		    VM_MAXUSER_ADDRESS, 1);
556 		vmspace_free(vm);
557 		vm = p->p_vmspace;
558 	} else {
559 #ifdef SYSVSHM
560 		if (vm->vm_shm)
561 			shmexit(p);
562 #endif
563 		(void) vm_map_remove(&vm->vm_map, VM_MIN_ADDRESS,
564 		    VM_MAXUSER_ADDRESS);
565 	}
566 	/*
567 	 * If parent is waiting for us to exec or exit,
568 	 * SPPWAIT will be set; clear it and wakeup parent.
569 	 */
570 	if (p->p_flag & SPPWAIT) {
571 		p->p_flag &= ~SPPWAIT;
572 		wakeup((caddr_t) p->p_pptr);
573 	}
574 #if defined(HP380)
575 	/* default to copyback caching on 68040 */
576 	if (mmutype == MMU_68040)
577 		p->p_md.md_flags |= (MDP_CCBDATA|MDP_CCBSTACK);
578 #endif
579 #ifdef HPUXCOMPAT
580 	p->p_md.md_flags &= ~(MDP_HPUX|MDP_HPUXMMAP);
581 	/* note that we are an HP-UX binary */
582 	if (ep->a_mid == MID_HPUX)
583 		p->p_md.md_flags |= MDP_HPUX;
584 	/* deal with miscellaneous attributes */
585 	if (ep->a_trsize & HPUXM_VALID) {
586 		if (ep->a_trsize & HPUXM_DATAWT)
587 			p->p_md.md_flags &= ~MDP_CCBDATA;
588 		if (ep->a_trsize & HPUXM_STKWT)
589 			p->p_md.md_flags &= ~MDP_CCBSTACK;
590 	}
591 #endif
592 #ifdef ULTRIXCOMPAT
593 	/*
594 	 * Always start out as an ULTRIX process.
595 	 * A system call in crt0.o will change us to BSD system calls later.
596 	 */
597 	p->p_md.md_flags |= MDP_ULTRIX;
598 #endif
599 	p->p_flag |= SEXEC;
600 #ifndef COFF
601 	addr = VM_MIN_ADDRESS;
602 	if (vm_allocate(&vm->vm_map, &addr, xts + ctob(ds), FALSE)) {
603 		uprintf("Cannot allocate text+data space\n");
604 		error = ENOMEM;			/* XXX */
605 		goto badmap;
606 	}
607 	vm->vm_taddr = (caddr_t)VM_MIN_ADDRESS;
608 	vm->vm_daddr = (caddr_t)(VM_MIN_ADDRESS + xts);
609 #else /* COFF */
610 	addr = (vm_offset_t)ep->ex_aout.codeStart;
611 	vm->vm_taddr = (caddr_t)addr;
612 	if (vm_allocate(&vm->vm_map, &addr, xts, FALSE)) {
613 		uprintf("Cannot allocate text space\n");
614 		error = ENOMEM;			/* XXX */
615 		goto badmap;
616 	}
617 	addr = (vm_offset_t)ep->ex_aout.heapStart;
618 	vm->vm_daddr = (caddr_t)addr;
619 	if (vm_allocate(&vm->vm_map, &addr, round_page(ctob(ds)), FALSE)) {
620 		uprintf("Cannot allocate data space\n");
621 		error = ENOMEM;			/* XXX */
622 		goto badmap;
623 	}
624 #endif /* COFF */
625 	size = round_page(MAXSSIZ);		/* XXX */
626 #ifdef	i386
627 	addr = trunc_page(USRSTACK - size) - NBPG;	/* XXX */
628 #else
629 	addr = trunc_page(USRSTACK - size);
630 #endif
631 	if (vm_allocate(&vm->vm_map, &addr, size, FALSE)) {
632 		uprintf("Cannot allocate stack space\n");
633 		error = ENOMEM;			/* XXX */
634 		goto badmap;
635 	}
636 	size -= round_page(p->p_rlimit[RLIMIT_STACK].rlim_cur);
637 	if (vm_map_protect(&vm->vm_map, addr, addr+size, VM_PROT_NONE, FALSE)) {
638 		uprintf("Cannot protect stack space\n");
639 		error = ENOMEM;
640 		goto badmap;
641 	}
642 	vm->vm_maxsaddr = (caddr_t)addr;
643 
644 	if (paged == 0) {
645 		/*
646 		 * Read in data segment.
647 		 */
648 		(void) vn_rdwr(UIO_READ, vp, vm->vm_daddr, (int) ep->a_data,
649 			(off_t)(toff + ep->a_text), UIO_USERSPACE,
650 			(IO_UNIT|IO_NODELOCKED), cred, (int *)0, p);
651 		/*
652 		 * Read in text segment if necessary (0410),
653 		 * and read-protect it.
654 		 */
655 		if (ep->a_text > 0) {
656 			error = vn_rdwr(UIO_READ, vp, vm->vm_taddr,
657 			    (int)ep->a_text, toff, UIO_USERSPACE,
658 			    (IO_UNIT|IO_NODELOCKED), cred, (int *)0, p);
659 			(void) vm_map_protect(&vm->vm_map,
660 			    (vm_offset_t)vm->vm_taddr,
661 			    (vm_offset_t)vm->vm_taddr + trunc_page(ep->a_text),
662 			    VM_PROT_READ|VM_PROT_EXECUTE, FALSE);
663 		}
664 	} else {
665 		/*
666 		 * Allocate a region backed by the exec'ed vnode.
667 		 */
668 #ifndef COFF
669 		addr = VM_MIN_ADDRESS;
670 		size = round_page(xts + ep->a_data);
671 		error = vm_mmap(&vm->vm_map, &addr, size,
672 			VM_PROT_ALL, VM_PROT_ALL,
673 			MAP_COPY|MAP_FIXED,
674 			(caddr_t)vp, (vm_offset_t)toff);
675 		(void) vm_map_protect(&vm->vm_map, addr, addr + xts,
676 			VM_PROT_READ|VM_PROT_EXECUTE, FALSE);
677 #else /* COFF */
678 		addr = (vm_offset_t)vm->vm_taddr;
679 		size = xts;
680 		error = vm_mmap(&vm->vm_map, &addr, size,
681 			VM_PROT_READ|VM_PROT_EXECUTE, VM_PROT_ALL,
682 			MAP_COPY|MAP_FIXED,
683 			(caddr_t)vp, (vm_offset_t)toff);
684 		toff += size;
685 		addr = (vm_offset_t)vm->vm_daddr;
686 		size = round_page(ep->a_data);
687 		error = vm_mmap(&vm->vm_map, &addr, size,
688 			VM_PROT_ALL, VM_PROT_ALL,
689 			MAP_COPY|MAP_FIXED,
690 			(caddr_t)vp, (vm_offset_t)toff);
691 #endif /* COFF */
692 		vp->v_flag |= VTEXT;
693 	}
694 	if (error) {
695 badmap:
696 		killproc(p, "VM allocation in exec");
697 		p->p_flag |= SKEEP;
698 		return(error);
699 	}
700 
701 	/*
702 	 * set SUID/SGID protections, if no tracing
703 	 */
704 	p->p_flag &= ~SUGID;
705 	if ((p->p_flag & STRC) == 0) {
706 		if (uid != cred->cr_uid || gid != cred->cr_gid) {
707 			p->p_ucred = cred = crcopy(cred);
708 			/*
709 			 * If process is being ktraced, turn off - unless
710 			 * root set it.
711 			 */
712 			if (p->p_tracep && !(p->p_traceflag & KTRFAC_ROOT)) {
713 				vrele(p->p_tracep);
714 				p->p_tracep = NULL;
715 				p->p_traceflag = 0;
716 			}
717 			cred->cr_uid = uid;
718 			cred->cr_gid = gid;
719 			p->p_flag |= SUGID;
720 		}
721 	} else
722 		psignal(p, SIGTRAP);
723 	p->p_cred->p_svuid = cred->cr_uid;
724 	p->p_cred->p_svgid = cred->cr_gid;
725 	vm->vm_tsize = btoc(xts);
726 	vm->vm_dsize = ds;
727 	vm->vm_ssize = btoc(ssize);
728 	if (p->p_flag & SPROFIL)
729 		stopprofclock(p);
730 #if defined(tahoe)
731 	/* move this when tahoe cpu_exec is created */
732 	p->p_addr->u_pcb.pcb_savacc.faddr = (float *)NULL;
733 #endif
734 	return (0);
735 }
736