xref: /titanic_52/usr/src/uts/sparc/os/syscall.c (revision 692d834dec3070f47507bc0f44d3d92620b49cfa)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #include <sys/param.h>
28 #include <sys/vmparam.h>
29 #include <sys/types.h>
30 #include <sys/sysmacros.h>
31 #include <sys/systm.h>
32 #include <sys/cmn_err.h>
33 #include <sys/signal.h>
34 #include <sys/stack.h>
35 #include <sys/cred.h>
36 #include <sys/user.h>
37 #include <sys/debug.h>
38 #include <sys/errno.h>
39 #include <sys/proc.h>
40 #include <sys/var.h>
41 #include <sys/inline.h>
42 #include <sys/syscall.h>
43 #include <sys/ucontext.h>
44 #include <sys/cpuvar.h>
45 #include <sys/siginfo.h>
46 #include <sys/trap.h>
47 #include <sys/machtrap.h>
48 #include <sys/sysinfo.h>
49 #include <sys/procfs.h>
50 #include <sys/prsystm.h>
51 #include <sys/fpu/fpusystm.h>
52 #include <sys/modctl.h>
53 #include <sys/aio_impl.h>
54 #include <c2/audit.h>
55 #include <sys/tnf.h>
56 #include <sys/tnf_probe.h>
57 #include <sys/machpcb.h>
58 #include <sys/privregs.h>
59 #include <sys/copyops.h>
60 #include <sys/timer.h>
61 #include <sys/priv.h>
62 #include <sys/msacct.h>
63 
64 int syscalltrace = 0;
65 #ifdef SYSCALLTRACE
66 static kmutex_t	systrace_lock;		/* syscall tracing lock */
67 #endif /* SYSCALLTRACE */
68 
69 static krwlock_t *lock_syscall(struct sysent *, uint_t);
70 
71 #ifdef _SYSCALL32_IMPL
72 static struct sysent *
73 lwp_getsysent(klwp_t *lwp)
74 {
75 	if (lwp_getdatamodel(lwp) == DATAMODEL_NATIVE)
76 		return (sysent);
77 	return (sysent32);
78 }
79 #define	LWP_GETSYSENT(lwp)	(lwp_getsysent(lwp))
80 #else
81 #define	LWP_GETSYSENT(lwp)	(sysent)
82 #endif
83 
84 /*
85  * Called to restore the lwp's register window just before
86  * returning to user level (only if the registers have been
87  * fetched or modified through /proc).
88  */
89 /*ARGSUSED1*/
90 void
91 xregrestore(klwp_t *lwp, int shared)
92 {
93 	/*
94 	 * If locals+ins were modified by /proc copy them out.
95 	 * Also copy to the shared window, if necessary.
96 	 */
97 	if (lwp->lwp_pcb.pcb_xregstat == XREGMODIFIED) {
98 		struct machpcb *mpcb = lwptompcb(lwp);
99 		caddr_t sp = (caddr_t)lwptoregs(lwp)->r_sp;
100 
101 		size_t rwinsize;
102 		caddr_t rwp;
103 		int is64;
104 
105 		if (lwp_getdatamodel(lwp) == DATAMODEL_LP64) {
106 			rwinsize = sizeof (struct rwindow);
107 			rwp = sp + STACK_BIAS;
108 			is64 = 1;
109 		} else {
110 			rwinsize = sizeof (struct rwindow32);
111 			sp = (caddr_t)(uintptr_t)(caddr32_t)(uintptr_t)sp;
112 			rwp = sp;
113 			is64 = 0;
114 		}
115 
116 		if (is64)
117 			(void) copyout_nowatch(&lwp->lwp_pcb.pcb_xregs,
118 			    rwp, rwinsize);
119 		else {
120 			struct rwindow32 rwindow32;
121 			int watched;
122 
123 			watched = watch_disable_addr(rwp, rwinsize, S_WRITE);
124 			rwindow_nto32(&lwp->lwp_pcb.pcb_xregs, &rwindow32);
125 			(void) copyout(&rwindow32, rwp, rwinsize);
126 			if (watched)
127 				watch_enable_addr(rwp, rwinsize, S_WRITE);
128 		}
129 
130 		/* also copy to the user return window */
131 		mpcb->mpcb_rsp[0] = sp;
132 		mpcb->mpcb_rsp[1] = NULL;
133 		bcopy(&lwp->lwp_pcb.pcb_xregs, &mpcb->mpcb_rwin[0],
134 		    sizeof (lwp->lwp_pcb.pcb_xregs));
135 	}
136 	lwp->lwp_pcb.pcb_xregstat = XREGNONE;
137 }
138 
139 
140 /*
141  * Get the arguments to the current system call.
142  *	lwp->lwp_ap normally points to the out regs in the reg structure.
143  *	If the user is going to change the out registers and might want to
144  *	get the args (for /proc tracing), it must copy the args elsewhere
145  *	via save_syscall_args().
146  */
147 uint_t
148 get_syscall_args(klwp_t *lwp, long *argp, int *nargsp)
149 {
150 	kthread_t	*t = lwptot(lwp);
151 	uint_t	code = t->t_sysnum;
152 	long	mask;
153 	long	*ap;
154 	int	nargs;
155 
156 	if (lwptoproc(lwp)->p_model == DATAMODEL_ILP32)
157 		mask = (uint32_t)0xffffffffU;
158 	else
159 		mask = 0xffffffffffffffff;
160 
161 	if (code != 0 && code < NSYSCALL) {
162 
163 		nargs = LWP_GETSYSENT(lwp)[code].sy_narg;
164 
165 		ASSERT(nargs <= MAXSYSARGS);
166 
167 		*nargsp = nargs;
168 		ap = lwp->lwp_ap;
169 		while (nargs-- > 0)
170 			*argp++ = *ap++ & mask;
171 	} else {
172 		*nargsp = 0;
173 	}
174 	return (code);
175 }
176 
177 #ifdef _SYSCALL32_IMPL
178 /*
179  * Get the arguments to the current 32-bit system call.
180  */
181 uint_t
182 get_syscall32_args(klwp_t *lwp, int *argp, int *nargsp)
183 {
184 	long args[MAXSYSARGS];
185 	uint_t i, code;
186 
187 	code = get_syscall_args(lwp, args, nargsp);
188 	for (i = 0; i != *nargsp; i++)
189 		*argp++ = (int)args[i];
190 	return (code);
191 }
192 #endif
193 
194 /*
195  * 	Save the system call arguments in a safe place.
196  *	lwp->lwp_ap normally points to the out regs in the reg structure.
197  *	If the user is going to change the out registers, g1, or the stack,
198  *	and might want to get the args (for /proc tracing), it must copy
199  *	the args elsewhere via save_syscall_args().
200  *
201  *	This may be called from stop() even when we're not in a system call.
202  *	Since there's no easy way to tell, this must be safe (not panic).
203  *	If the copyins get data faults, return non-zero.
204  */
205 int
206 save_syscall_args()
207 {
208 	kthread_t	*t = curthread;
209 	klwp_t		*lwp = ttolwp(t);
210 	struct regs	*rp = lwptoregs(lwp);
211 	uint_t		code = t->t_sysnum;
212 	uint_t		nargs;
213 	int		i;
214 	caddr_t		ua;
215 	model_t		datamodel;
216 
217 	if (lwp->lwp_argsaved || code == 0)
218 		return (0);		/* args already saved or not needed */
219 
220 	if (code >= NSYSCALL) {
221 		nargs = 0;		/* illegal syscall */
222 	} else {
223 		struct sysent *se = LWP_GETSYSENT(lwp);
224 		struct sysent *callp = se + code;
225 
226 		nargs = callp->sy_narg;
227 		if (LOADABLE_SYSCALL(callp) && nargs == 0) {
228 			krwlock_t	*module_lock;
229 
230 			/*
231 			 * Find out how many arguments the system
232 			 * call uses.
233 			 *
234 			 * We have the property that loaded syscalls
235 			 * never change the number of arguments they
236 			 * use after they've been loaded once.  This
237 			 * allows us to stop for /proc tracing without
238 			 * holding the module lock.
239 			 * /proc is assured that sy_narg is valid.
240 			 */
241 			module_lock = lock_syscall(se, code);
242 			nargs = callp->sy_narg;
243 			rw_exit(module_lock);
244 		}
245 	}
246 
247 	/*
248 	 * Fetch the system call arguments.
249 	 */
250 	if (nargs == 0)
251 		goto out;
252 
253 
254 	ASSERT(nargs <= MAXSYSARGS);
255 
256 	if ((datamodel = lwp_getdatamodel(lwp)) == DATAMODEL_ILP32) {
257 
258 		if (rp->r_g1 == 0) {	/* indirect syscall */
259 
260 			lwp->lwp_arg[0] = (uint32_t)rp->r_o1;
261 			lwp->lwp_arg[1] = (uint32_t)rp->r_o2;
262 			lwp->lwp_arg[2] = (uint32_t)rp->r_o3;
263 			lwp->lwp_arg[3] = (uint32_t)rp->r_o4;
264 			lwp->lwp_arg[4] = (uint32_t)rp->r_o5;
265 			if (nargs > 5) {
266 				ua = (caddr_t)(uintptr_t)(caddr32_t)(uintptr_t)
267 				    (rp->r_sp + MINFRAME32);
268 				for (i = 5; i < nargs; i++) {
269 					uint32_t a;
270 					if (fuword32(ua, &a) != 0)
271 						return (-1);
272 					lwp->lwp_arg[i] = a;
273 					ua += sizeof (a);
274 				}
275 			}
276 		} else {
277 			lwp->lwp_arg[0] = (uint32_t)rp->r_o0;
278 			lwp->lwp_arg[1] = (uint32_t)rp->r_o1;
279 			lwp->lwp_arg[2] = (uint32_t)rp->r_o2;
280 			lwp->lwp_arg[3] = (uint32_t)rp->r_o3;
281 			lwp->lwp_arg[4] = (uint32_t)rp->r_o4;
282 			lwp->lwp_arg[5] = (uint32_t)rp->r_o5;
283 			if (nargs > 6) {
284 				ua = (caddr_t)(uintptr_t)(caddr32_t)(uintptr_t)
285 				    (rp->r_sp + MINFRAME32);
286 				for (i = 6; i < nargs; i++) {
287 					uint32_t a;
288 					if (fuword32(ua, &a) != 0)
289 						return (-1);
290 					lwp->lwp_arg[i] = a;
291 					ua += sizeof (a);
292 				}
293 			}
294 		}
295 	} else {
296 		ASSERT(datamodel == DATAMODEL_LP64);
297 		lwp->lwp_arg[0] = rp->r_o0;
298 		lwp->lwp_arg[1] = rp->r_o1;
299 		lwp->lwp_arg[2] = rp->r_o2;
300 		lwp->lwp_arg[3] = rp->r_o3;
301 		lwp->lwp_arg[4] = rp->r_o4;
302 		lwp->lwp_arg[5] = rp->r_o5;
303 		if (nargs > 6) {
304 			ua = (caddr_t)rp->r_sp + MINFRAME + STACK_BIAS;
305 			for (i = 6; i < nargs; i++) {
306 				unsigned long a;
307 				if (fulword(ua, &a) != 0)
308 					return (-1);
309 				lwp->lwp_arg[i] = a;
310 				ua += sizeof (a);
311 			}
312 		}
313 	}
314 
315 out:
316 	lwp->lwp_ap = lwp->lwp_arg;
317 	lwp->lwp_argsaved = 1;
318 	t->t_post_sys = 1;	/* so lwp_ap will be reset */
319 	return (0);
320 }
321 
322 void
323 reset_syscall_args(void)
324 {
325 	klwp_t *lwp = ttolwp(curthread);
326 
327 	lwp->lwp_ap = (long *)&lwptoregs(lwp)->r_o0;
328 	lwp->lwp_argsaved = 0;
329 }
330 
331 /*
332  * nonexistent system call-- signal lwp (may want to handle it)
333  * flag error if lwp won't see signal immediately
334  * This works for old or new calling sequence.
335  */
336 int64_t
337 nosys()
338 {
339 	tsignal(curthread, SIGSYS);
340 	return ((int64_t)set_errno(ENOSYS));
341 }
342 
343 /*
344  * Perform pre-system-call processing, including stopping for tracing,
345  * auditing, microstate-accounting, etc.
346  *
347  * This routine is called only if the t_pre_sys flag is set.  Any condition
348  * requiring pre-syscall handling must set the t_pre_sys flag.  If the
349  * condition is persistent, this routine will repost t_pre_sys.
350  */
351 int
352 pre_syscall(int arg0)
353 {
354 	unsigned int code;
355 	kthread_t *t = curthread;
356 	proc_t *p = ttoproc(t);
357 	klwp_t *lwp = ttolwp(t);
358 	struct regs *rp = lwptoregs(lwp);
359 	int	repost;
360 
361 	t->t_pre_sys = repost = 0;	/* clear pre-syscall processing flag */
362 
363 	ASSERT(t->t_schedflag & TS_DONT_SWAP);
364 
365 	syscall_mstate(LMS_USER, LMS_SYSTEM);
366 
367 	/*
368 	 * The syscall arguments in the out registers should be pointed to
369 	 * by lwp_ap.  If the args need to be copied so that the outs can
370 	 * be changed without losing the ability to get the args for /proc,
371 	 * they can be saved by save_syscall_args(), and lwp_ap will be
372 	 * restored by post_syscall().
373 	 */
374 	ASSERT(lwp->lwp_ap == (long *)&rp->r_o0);
375 
376 	/*
377 	 * Make sure the thread is holding the latest credentials for the
378 	 * process.  The credentials in the process right now apply to this
379 	 * thread for the entire system call.
380 	 */
381 	if (t->t_cred != p->p_cred) {
382 		cred_t *oldcred = t->t_cred;
383 		/*
384 		 * DTrace accesses t_cred in probe context.  t_cred must
385 		 * always be either NULL, or point to a valid, allocated cred
386 		 * structure.
387 		 */
388 		t->t_cred = crgetcred();
389 		crfree(oldcred);
390 	}
391 
392 	/*
393 	 * Undo special arrangements to single-step the lwp
394 	 * so that a debugger will see valid register contents.
395 	 * Also so that the pc is valid for syncfpu().
396 	 * Also so that a syscall like exec() can be stepped.
397 	 */
398 	if (lwp->lwp_pcb.pcb_step != STEP_NONE) {
399 		(void) prundostep();
400 		repost = 1;
401 	}
402 
403 	/*
404 	 * Check for indirect system call in case we stop for tracing.
405 	 * Don't allow multiple indirection.
406 	 */
407 	code = t->t_sysnum;
408 	if (code == 0 && arg0 != 0) {		/* indirect syscall */
409 		code = arg0;
410 		t->t_sysnum = arg0;
411 	}
412 
413 	/*
414 	 * From the proc(4) manual page:
415 	 * When entry to a system call is being traced, the traced process
416 	 * stops after having begun the call to the system but before the
417 	 * system call arguments have been fetched from the process.
418 	 * If proc changes the args we must refetch them after starting.
419 	 */
420 	if (PTOU(p)->u_systrap) {
421 		if (prismember(&PTOU(p)->u_entrymask, code)) {
422 			/*
423 			 * Recheck stop condition, now that lock is held.
424 			 */
425 			mutex_enter(&p->p_lock);
426 			if (PTOU(p)->u_systrap &&
427 			    prismember(&PTOU(p)->u_entrymask, code)) {
428 				stop(PR_SYSENTRY, code);
429 				/*
430 				 * Must refetch args since they were
431 				 * possibly modified by /proc.  Indicate
432 				 * that the valid copy is in the
433 				 * registers.
434 				 */
435 				lwp->lwp_argsaved = 0;
436 				lwp->lwp_ap = (long *)&rp->r_o0;
437 			}
438 			mutex_exit(&p->p_lock);
439 		}
440 		repost = 1;
441 	}
442 
443 	if (lwp->lwp_sysabort) {
444 		/*
445 		 * lwp_sysabort may have been set via /proc while the process
446 		 * was stopped on PR_SYSENTRY.  If so, abort the system call.
447 		 * Override any error from the copyin() of the arguments.
448 		 */
449 		lwp->lwp_sysabort = 0;
450 		(void) set_errno(EINTR); /* sets post-sys processing */
451 		t->t_pre_sys = 1;	/* repost anyway */
452 		return (1);		/* don't do system call, return EINTR */
453 	}
454 
455 	if (audit_active) {	/* begin auditing for this syscall */
456 		int error;
457 		if (error = audit_start(T_SYSCALL, code, 0, lwp)) {
458 			t->t_pre_sys = 1;	/* repost anyway */
459 			lwp->lwp_error = 0;	/* for old drivers */
460 			return (error);
461 		}
462 		repost = 1;
463 	}
464 
465 #ifndef NPROBE
466 	/* Kernel probe */
467 	if (tnf_tracing_active) {
468 		TNF_PROBE_1(syscall_start, "syscall thread", /* CSTYLED */,
469 			tnf_sysnum,	sysnum,		t->t_sysnum);
470 		t->t_post_sys = 1;	/* make sure post_syscall runs */
471 		repost = 1;
472 	}
473 #endif /* NPROBE */
474 
475 #ifdef SYSCALLTRACE
476 	if (syscalltrace) {
477 		int i;
478 		long *ap;
479 		char *cp;
480 		char *sysname;
481 		struct sysent *callp;
482 
483 		if (code >= NSYSCALL)
484 			callp = &nosys_ent;	/* nosys has no args */
485 		else
486 			callp = LWP_GETSYSENT(lwp) + code;
487 		(void) save_syscall_args();
488 		mutex_enter(&systrace_lock);
489 		printf("%d: ", p->p_pid);
490 		if (code >= NSYSCALL)
491 			printf("0x%x", code);
492 		else {
493 			sysname = mod_getsysname(code);
494 			printf("%s[0x%x]", sysname == NULL ? "NULL" :
495 			    sysname, code);
496 		}
497 		cp = "(";
498 		for (i = 0, ap = lwp->lwp_ap; i < callp->sy_narg; i++, ap++) {
499 			printf("%s%lx", cp, *ap);
500 			cp = ", ";
501 		}
502 		if (i)
503 			printf(")");
504 		printf(" %s id=0x%p\n", PTOU(p)->u_comm, curthread);
505 		mutex_exit(&systrace_lock);
506 	}
507 #endif /* SYSCALLTRACE */
508 
509 	/*
510 	 * If there was a continuing reason for pre-syscall processing,
511 	 * set the t_pre_sys flag for the next system call.
512 	 */
513 	if (repost)
514 		t->t_pre_sys = 1;
515 	lwp->lwp_error = 0;	/* for old drivers */
516 	lwp->lwp_badpriv = PRIV_NONE;	/* for privilege tracing */
517 	return (0);
518 }
519 
520 /*
521  * Post-syscall processing.  Perform abnormal system call completion
522  * actions such as /proc tracing, profiling, signals, preemption, etc.
523  *
524  * This routine is called only if t_post_sys, t_sig_check, or t_astflag is set.
525  * Any condition requiring pre-syscall handling must set one of these.
526  * If the condition is persistent, this routine will repost t_post_sys.
527  */
528 void
529 post_syscall(long rval1, long rval2)
530 {
531 	kthread_t	*t = curthread;
532 	proc_t	*p = curproc;
533 	klwp_t	*lwp = ttolwp(t);
534 	struct regs *rp = lwptoregs(lwp);
535 	uint_t	error;
536 	int	code = t->t_sysnum;
537 	int	repost = 0;
538 	int	proc_stop = 0;		/* non-zero if stopping for /proc */
539 	int	sigprof = 0;		/* non-zero if sending SIGPROF */
540 
541 	t->t_post_sys = 0;
542 
543 	error = lwp->lwp_errno;
544 
545 	/*
546 	 * Code can be zero if this is a new LWP returning after a forkall(),
547 	 * other than the one which matches the one in the parent which called
548 	 * forkall().  In these LWPs, skip most of post-syscall activity.
549 	 */
550 	if (code == 0)
551 		goto sig_check;
552 
553 	if (audit_active) {	/* put out audit record for this syscall */
554 		rval_t	rval;	/* fix audit_finish() someday */
555 
556 		/* XX64 -- truncation of 64-bit return values? */
557 		rval.r_val1 = (int)rval1;
558 		rval.r_val2 = (int)rval2;
559 		audit_finish(T_SYSCALL, code, error, &rval);
560 		repost = 1;
561 	}
562 
563 	if (curthread->t_pdmsg != NULL) {
564 		char *m = curthread->t_pdmsg;
565 
566 		uprintf("%s", m);
567 		kmem_free(m, strlen(m) + 1);
568 		curthread->t_pdmsg = NULL;
569 	}
570 
571 	/*
572 	 * If we're going to stop for /proc tracing, set the flag and
573 	 * save the arguments so that the return values don't smash them.
574 	 */
575 	if (PTOU(p)->u_systrap) {
576 		if (prismember(&PTOU(p)->u_exitmask, code)) {
577 			proc_stop = 1;
578 			(void) save_syscall_args();
579 		}
580 		repost = 1;
581 	}
582 
583 	/*
584 	 * Similarly check to see if SIGPROF might be sent.
585 	 */
586 	if (curthread->t_rprof != NULL &&
587 	    curthread->t_rprof->rp_anystate != 0) {
588 		(void) save_syscall_args();
589 		sigprof = 1;
590 	}
591 
592 	if (lwp->lwp_eosys == NORMALRETURN) {
593 		if (error == 0) {
594 #ifdef SYSCALLTRACE
595 			if (syscalltrace) {
596 				mutex_enter(&systrace_lock);
597 				printf(
598 				    "%d: r_val1=0x%lx, r_val2=0x%lx, id 0x%p\n",
599 				    p->p_pid, rval1, rval2, curthread);
600 				mutex_exit(&systrace_lock);
601 			}
602 #endif /* SYSCALLTRACE */
603 			rp->r_tstate &= ~TSTATE_IC;
604 			rp->r_o0 = rval1;
605 			rp->r_o1 = rval2;
606 		} else {
607 			int sig;
608 
609 #ifdef SYSCALLTRACE
610 			if (syscalltrace) {
611 				mutex_enter(&systrace_lock);
612 				printf("%d: error=%d, id 0x%p\n",
613 				    p->p_pid, error, curthread);
614 				mutex_exit(&systrace_lock);
615 			}
616 #endif /* SYSCALLTRACE */
617 			if (error == EINTR && t->t_activefd.a_stale)
618 				error = EBADF;
619 			if (error == EINTR &&
620 			    (sig = lwp->lwp_cursig) != 0 &&
621 			    sigismember(&PTOU(p)->u_sigrestart, sig) &&
622 			    PTOU(p)->u_signal[sig - 1] != SIG_DFL &&
623 			    PTOU(p)->u_signal[sig - 1] != SIG_IGN)
624 				error = ERESTART;
625 			rp->r_o0 = error;
626 			rp->r_tstate |= TSTATE_IC;
627 		}
628 		/*
629 		 * The default action is to redo the trap instruction.
630 		 * We increment the pc and npc past it for NORMALRETURN.
631 		 * JUSTRETURN has set up a new pc and npc already.
632 		 * If we are a cloned thread of forkall(), don't
633 		 * adjust here because we have already inherited
634 		 * the adjusted values from our clone.
635 		 */
636 		if (!(t->t_flag & T_FORKALL)) {
637 			rp->r_pc = rp->r_npc;
638 			rp->r_npc += 4;
639 		}
640 	}
641 
642 	/*
643 	 * From the proc(4) manual page:
644 	 * When exit from a system call is being traced, the traced process
645 	 * stops on completion of the system call just prior to checking for
646 	 * signals and returning to user level.  At this point all return
647 	 * values have been stored into the traced process's saved registers.
648 	 */
649 	if (proc_stop) {
650 		mutex_enter(&p->p_lock);
651 		if (PTOU(p)->u_systrap &&
652 		    prismember(&PTOU(p)->u_exitmask, code))
653 			stop(PR_SYSEXIT, code);
654 		mutex_exit(&p->p_lock);
655 	}
656 
657 	/*
658 	 * If we are the parent returning from a successful
659 	 * vfork, wait for the child to exec or exit.
660 	 * This code must be here and not in the bowels of the system
661 	 * so that /proc can intercept exit from vfork in a timely way.
662 	 */
663 	if (t->t_flag & T_VFPARENT) {
664 		ASSERT(code == SYS_vfork || code == SYS_forksys);
665 		ASSERT(rp->r_o1 == 0 && error == 0);
666 		vfwait((pid_t)rval1);
667 		t->t_flag &= ~T_VFPARENT;
668 	}
669 
670 	/*
671 	 * If profiling is active, bill the current PC in user-land
672 	 * and keep reposting until profiling is disabled.
673 	 */
674 	if (p->p_prof.pr_scale) {
675 		if (lwp->lwp_oweupc)
676 			profil_tick(rp->r_pc);
677 		repost = 1;
678 	}
679 
680 sig_check:
681 	/*
682 	 * Reset flag for next time.
683 	 * We must do this after stopping on PR_SYSEXIT
684 	 * because /proc uses the information in lwp_eosys.
685 	 */
686 	lwp->lwp_eosys = NORMALRETURN;
687 	clear_stale_fd();
688 	t->t_flag &= ~T_FORKALL;
689 
690 	if (t->t_astflag | t->t_sig_check) {
691 		/*
692 		 * Turn off the AST flag before checking all the conditions that
693 		 * may have caused an AST.  This flag is on whenever a signal or
694 		 * unusual condition should be handled after the next trap or
695 		 * syscall.
696 		 */
697 		astoff(t);
698 		t->t_sig_check = 0;
699 
700 		/*
701 		 * The following check is legal for the following reasons:
702 		 *	1) The thread we are checking, is ourselves, so there is
703 		 *	   no way the proc can go away.
704 		 *	2) The only time we need to be protected by the
705 		 *	   lock is if the binding is changed.
706 		 *
707 		 *	Note we will still take the lock and check the binding
708 		 *	if the condition was true without the lock held.  This
709 		 *	prevents lock contention among threads owned by the
710 		 *	same proc.
711 		 */
712 
713 		if (curthread->t_proc_flag & TP_CHANGEBIND) {
714 			mutex_enter(&p->p_lock);
715 			if (curthread->t_proc_flag & TP_CHANGEBIND) {
716 				timer_lwpbind();
717 				curthread->t_proc_flag &= ~TP_CHANGEBIND;
718 			}
719 			mutex_exit(&p->p_lock);
720 		}
721 
722 		/*
723 		 * for kaio requests on the special kaio poll queue,
724 		 * copyout their results to user memory.
725 		 */
726 		if (p->p_aio)
727 			aio_cleanup(0);
728 
729 		/*
730 		 * If this LWP was asked to hold, call holdlwp(), which will
731 		 * stop.  holdlwps() sets this up and calls pokelwps() which
732 		 * sets the AST flag.
733 		 *
734 		 * Also check TP_EXITLWP, since this is used by fresh new LWPs
735 		 * through lwp_rtt().  That flag is set if the lwp_create(2)
736 		 * syscall failed after creating the LWP.
737 		 */
738 		if (ISHOLD(p) || (t->t_proc_flag & TP_EXITLWP))
739 			holdlwp();
740 
741 		/*
742 		 * All code that sets signals and makes ISSIG_PENDING
743 		 * evaluate true must set t_sig_check afterwards.
744 		 */
745 		if (ISSIG_PENDING(t, lwp, p)) {
746 			if (issig(FORREAL))
747 				psig();
748 			t->t_sig_check = 1;	/* recheck next time */
749 		}
750 
751 		if (sigprof) {
752 			int nargs = (code > 0 && code < NSYSCALL)?
753 			    LWP_GETSYSENT(lwp)[code].sy_narg : 0;
754 			realsigprof(code, nargs, error);
755 			t->t_sig_check = 1;	/* recheck next time */
756 		}
757 
758 		/*
759 		 * If a performance counter overflow interrupt was
760 		 * delivered *during* the syscall, then re-enable the
761 		 * AST so that we take a trip through trap() to cause
762 		 * the SIGEMT to be delivered.
763 		 */
764 		if (lwp->lwp_pcb.pcb_flags & CPC_OVERFLOW)
765 			aston(t);
766 
767 		/*
768 		 * If an asynchronous hardware error is pending, turn AST flag
769 		 * back on.  AST will be checked again before we return to user
770 		 * mode and we'll come back through trap() to handle the error.
771 		 */
772 		if (lwp->lwp_pcb.pcb_flags & ASYNC_HWERR)
773 			aston(t);
774 	}
775 
776 	/*
777 	 * Restore register window if a debugger modified it.
778 	 * Set up to perform a single-step if a debugger requested it.
779 	 */
780 	if (lwp->lwp_pcb.pcb_xregstat != XREGNONE)
781 		xregrestore(lwp, 1);
782 
783 	lwp->lwp_errno = 0;		/* clear error for next time */
784 
785 #ifndef NPROBE
786 	/* Kernel probe */
787 	if (tnf_tracing_active) {
788 		TNF_PROBE_3(syscall_end, "syscall thread", /* CSTYLED */,
789 		    tnf_long,	rval1,		rval1,
790 		    tnf_long,	rval2,		rval2,
791 		    tnf_long,	errno,		(long)error);
792 		repost = 1;
793 	}
794 #endif /* NPROBE */
795 
796 	/*
797 	 * Set state to LWP_USER here so preempt won't give us a kernel
798 	 * priority if it occurs after this point.  Call CL_TRAPRET() to
799 	 * restore the user-level priority.
800 	 *
801 	 * It is important that no locks (other than spinlocks) be entered
802 	 * after this point before returning to user mode (unless lwp_state
803 	 * is set back to LWP_SYS).
804 	 *
805 	 * Sampled times past this point are charged to the user.
806 	 */
807 	lwp->lwp_state = LWP_USER;
808 
809 	if (t->t_trapret) {
810 		t->t_trapret = 0;
811 		thread_lock(t);
812 		CL_TRAPRET(t);
813 		thread_unlock(t);
814 	}
815 	if (CPU->cpu_runrun || t->t_schedflag & TS_ANYWAITQ)
816 		preempt();
817 	prunstop();
818 
819 	/*
820 	 * t_post_sys will be set if pcb_step is active.
821 	 */
822 	if (lwp->lwp_pcb.pcb_step != STEP_NONE) {
823 		prdostep();
824 		repost = 1;
825 	}
826 
827 	t->t_sysnum = 0;	/* no longer in a system call */
828 
829 	/*
830 	 * In case the args were copied to the lwp, reset the
831 	 * pointer so the next syscall will have the right lwp_ap pointer.
832 	 */
833 	lwp->lwp_ap = (long *)&rp->r_o0;
834 	lwp->lwp_argsaved = 0;
835 
836 	/*
837 	 * If there was a continuing reason for post-syscall processing,
838 	 * set the t_post_sys flag for the next system call.
839 	 */
840 	if (repost)
841 		t->t_post_sys = 1;
842 
843 	/*
844 	 * If there is a ustack registered for this lwp, and the stack rlimit
845 	 * has been altered, read in the ustack. If the saved stack rlimit
846 	 * matches the bounds of the ustack, update the ustack to reflect
847 	 * the new rlimit. If the new stack rlimit is RLIM_INFINITY, disable
848 	 * stack checking by setting the size to 0.
849 	 */
850 	if (lwp->lwp_ustack != 0 && lwp->lwp_old_stk_ctl != 0) {
851 		rlim64_t new_size;
852 		model_t model;
853 		caddr_t top;
854 		struct rlimit64 rl;
855 
856 		mutex_enter(&p->p_lock);
857 		new_size = p->p_stk_ctl;
858 		model = p->p_model;
859 		top = p->p_usrstack;
860 		(void) rctl_rlimit_get(rctlproc_legacy[RLIMIT_STACK], p, &rl);
861 		mutex_exit(&p->p_lock);
862 
863 		if (rl.rlim_cur == RLIM64_INFINITY)
864 			new_size = 0;
865 
866 		if (model == DATAMODEL_NATIVE) {
867 			stack_t stk;
868 
869 			if (copyin((stack_t *)lwp->lwp_ustack, &stk,
870 			    sizeof (stack_t)) == 0 &&
871 			    (stk.ss_size == lwp->lwp_old_stk_ctl ||
872 			    stk.ss_size == 0) &&
873 			    stk.ss_sp == top - stk.ss_size) {
874 				stk.ss_sp = (void *)((uintptr_t)stk.ss_sp +
875 				    stk.ss_size - new_size);
876 				stk.ss_size = new_size;
877 
878 				(void) copyout(&stk,
879 				    (stack_t *)lwp->lwp_ustack,
880 				    sizeof (stack_t));
881 			}
882 		} else {
883 			stack32_t stk32;
884 
885 			if (copyin((stack32_t *)lwp->lwp_ustack, &stk32,
886 			    sizeof (stack32_t)) == 0 &&
887 			    (stk32.ss_size == lwp->lwp_old_stk_ctl ||
888 			    stk32.ss_size == 0) &&
889 			    stk32.ss_sp ==
890 			    (caddr32_t)(uintptr_t)(top - stk32.ss_size)) {
891 				stk32.ss_sp += stk32.ss_size - new_size;
892 				stk32.ss_size = new_size;
893 
894 				(void) copyout(&stk32,
895 				    (stack32_t *)lwp->lwp_ustack,
896 				    sizeof (stack32_t));
897 			}
898 		}
899 
900 		lwp->lwp_old_stk_ctl = 0;
901 	}
902 
903 	syscall_mstate(LMS_SYSTEM, LMS_USER);
904 }
905 
906 /*
907  * Call a system call which takes a pointer to the user args struct and
908  * a pointer to the return values.  This is a bit slower than the standard
909  * C arg-passing method in some cases.
910  */
911 int64_t
912 syscall_ap()
913 {
914 	uint_t	error;
915 	struct sysent *callp;
916 	rval_t	rval;
917 	klwp_t	*lwp = ttolwp(curthread);
918 	struct regs *rp = lwptoregs(lwp);
919 
920 	callp = LWP_GETSYSENT(lwp) + curthread->t_sysnum;
921 
922 	/*
923 	 * If the arguments don't fit in registers %o0 - o5, make sure they
924 	 * have been copied to the lwp_arg array.
925 	 */
926 	if (callp->sy_narg > 6 && save_syscall_args())
927 		return ((int64_t)set_errno(EFAULT));
928 
929 	rval.r_val1 = 0;
930 	rval.r_val2 = (int)rp->r_o1;
931 	lwp->lwp_error = 0;	/* for old drivers */
932 	error = (*(callp->sy_call))(lwp->lwp_ap, &rval);
933 	if (error)
934 		return ((int64_t)set_errno(error));
935 	return (rval.r_vals);
936 }
937 
938 /*
939  * Load system call module.
940  *	Returns with pointer to held read lock for module.
941  */
942 static krwlock_t *
943 lock_syscall(struct sysent *table, uint_t code)
944 {
945 	krwlock_t	*module_lock;
946 	struct modctl	*modp;
947 	int		id;
948 	struct sysent   *callp;
949 
950 	module_lock = table[code].sy_lock;
951 	callp = &table[code];
952 
953 	/*
954 	 * Optimization to only call modload if we don't have a loaded
955 	 * syscall.
956 	 */
957 	rw_enter(module_lock, RW_READER);
958 	if (LOADED_SYSCALL(callp))
959 		return (module_lock);
960 	rw_exit(module_lock);
961 
962 	for (;;) {
963 		if ((id = modload("sys", syscallnames[code])) == -1)
964 			break;
965 
966 		/*
967 		 * If we loaded successfully at least once, the modctl
968 		 * will still be valid, so we try to grab it by filename.
969 		 * If this call fails, it's because the mod_filename
970 		 * was changed after the call to modload() (mod_hold_by_name()
971 		 * is the likely culprit).  We can safely just take
972 		 * another lap if this is the case;  the modload() will
973 		 * change the mod_filename back to one by which we can
974 		 * find the modctl.
975 		 */
976 		modp = mod_find_by_filename("sys", syscallnames[code]);
977 
978 		if (modp == NULL)
979 			continue;
980 
981 		mutex_enter(&mod_lock);
982 
983 		if (!modp->mod_installed) {
984 			mutex_exit(&mod_lock);
985 			continue;
986 		}
987 		break;
988 	}
989 
990 	rw_enter(module_lock, RW_READER);
991 
992 	if (id != -1)
993 		mutex_exit(&mod_lock);
994 
995 	return (module_lock);
996 }
997 
998 /*
999  * Loadable syscall support.
1000  *	If needed, load the module, then reserve it by holding a read
1001  * 	lock for the duration of the call.
1002  *	Later, if the syscall is not unloadable, it could patch the vector.
1003  */
1004 /*ARGSUSED*/
1005 int64_t
1006 loadable_syscall(
1007     long a0, long a1, long a2, long a3,
1008     long a4, long a5, long a6, long a7)
1009 {
1010 	int64_t		rval;
1011 	struct sysent	*callp;
1012 	struct sysent	*se = LWP_GETSYSENT(ttolwp(curthread));
1013 	krwlock_t	*module_lock;
1014 	int		code;
1015 
1016 	code = curthread->t_sysnum;
1017 	callp = se + code;
1018 
1019 	/*
1020 	 * Try to autoload the system call if necessary.
1021 	 */
1022 	module_lock = lock_syscall(se, code);
1023 	THREAD_KPRI_RELEASE();	/* drop priority given by rw_enter */
1024 
1025 	/*
1026 	 * we've locked either the loaded syscall or nosys
1027 	 */
1028 	if (callp->sy_flags & SE_ARGC) {
1029 		int64_t (*sy_call)();
1030 
1031 		sy_call = (int64_t (*)())callp->sy_call;
1032 		rval = (*sy_call)(a0, a1, a2, a3, a4, a5);
1033 	} else {
1034 		rval = syscall_ap();
1035 	}
1036 
1037 	THREAD_KPRI_REQUEST();	/* regain priority from read lock */
1038 	rw_exit(module_lock);
1039 	return (rval);
1040 }
1041 
1042 /*
1043  * Handle indirect system calls.
1044  *	This interface should be deprecated.  The library can handle
1045  *	this more efficiently, but keep this implementation for old binaries.
1046  *
1047  * XX64	Needs some work.
1048  */
1049 int64_t
1050 indir(int code, long a0, long a1, long a2, long a3, long a4)
1051 {
1052 	klwp_t		*lwp = ttolwp(curthread);
1053 	struct sysent	*callp;
1054 
1055 	if (code <= 0 || code >= NSYSCALL)
1056 		return (nosys());
1057 
1058 	ASSERT(lwp->lwp_ap != NULL);
1059 
1060 	curthread->t_sysnum = code;
1061 	callp = LWP_GETSYSENT(lwp) + code;
1062 
1063 	/*
1064 	 * Handle argument setup, unless already done in pre_syscall().
1065 	 */
1066 	if (callp->sy_narg > 5) {
1067 		if (save_syscall_args()) 	/* move args to LWP array */
1068 			return ((int64_t)set_errno(EFAULT));
1069 	} else if (!lwp->lwp_argsaved) {
1070 		long *ap;
1071 
1072 		ap = lwp->lwp_ap;		/* args haven't been saved */
1073 		lwp->lwp_ap = ap + 1;		/* advance arg pointer */
1074 		curthread->t_post_sys = 1;	/* so lwp_ap will be reset */
1075 	}
1076 	return ((*callp->sy_callc)(a0, a1, a2, a3, a4, lwp->lwp_arg[5]));
1077 }
1078 
1079 /*
1080  * set_errno - set an error return from the current system call.
1081  *	This could be a macro.
1082  *	This returns the value it is passed, so that the caller can
1083  *	use tail-recursion-elimination and do return (set_errno(ERRNO));
1084  */
1085 uint_t
1086 set_errno(uint_t error)
1087 {
1088 	ASSERT(error != 0);		/* must not be used to clear errno */
1089 
1090 	curthread->t_post_sys = 1;	/* have post_syscall do error return */
1091 	return (ttolwp(curthread)->lwp_errno = error);
1092 }
1093 
1094 /*
1095  * set_proc_pre_sys - Set pre-syscall processing for entire process.
1096  */
1097 void
1098 set_proc_pre_sys(proc_t *p)
1099 {
1100 	kthread_t	*t;
1101 	kthread_t	*first;
1102 
1103 	ASSERT(MUTEX_HELD(&p->p_lock));
1104 
1105 	t = first = p->p_tlist;
1106 	do {
1107 		t->t_pre_sys = 1;
1108 	} while ((t = t->t_forw) != first);
1109 }
1110 
1111 /*
1112  * set_proc_post_sys - Set post-syscall processing for entire process.
1113  */
1114 void
1115 set_proc_post_sys(proc_t *p)
1116 {
1117 	kthread_t	*t;
1118 	kthread_t	*first;
1119 
1120 	ASSERT(MUTEX_HELD(&p->p_lock));
1121 
1122 	t = first = p->p_tlist;
1123 	do {
1124 		t->t_post_sys = 1;
1125 	} while ((t = t->t_forw) != first);
1126 }
1127 
1128 /*
1129  * set_proc_sys - Set pre- and post-syscall processing for entire process.
1130  */
1131 void
1132 set_proc_sys(proc_t *p)
1133 {
1134 	kthread_t	*t;
1135 	kthread_t	*first;
1136 
1137 	ASSERT(MUTEX_HELD(&p->p_lock));
1138 
1139 	t = first = p->p_tlist;
1140 	do {
1141 		t->t_pre_sys = 1;
1142 		t->t_post_sys = 1;
1143 	} while ((t = t->t_forw) != first);
1144 }
1145 
1146 /*
1147  * set_all_proc_sys - set pre- and post-syscall processing flags for all
1148  * user processes.
1149  *
1150  * This is needed when auditing, tracing, or other facilities which affect
1151  * all processes are turned on.
1152  */
1153 void
1154 set_all_proc_sys()
1155 {
1156 	kthread_t	*t;
1157 	kthread_t	*first;
1158 
1159 	mutex_enter(&pidlock);
1160 	t = first = curthread;
1161 	do {
1162 		t->t_pre_sys = 1;
1163 		t->t_post_sys = 1;
1164 	} while ((t = t->t_next) != first);
1165 	mutex_exit(&pidlock);
1166 }
1167 
1168 /*
1169  * set_proc_ast - Set asynchronous service trap (AST) flag for all
1170  * threads in process.
1171  */
1172 void
1173 set_proc_ast(proc_t *p)
1174 {
1175 	kthread_t	*t;
1176 	kthread_t	*first;
1177 
1178 	ASSERT(MUTEX_HELD(&p->p_lock));
1179 
1180 	t = first = p->p_tlist;
1181 	do {
1182 		aston(t);
1183 	} while ((t = t->t_forw) != first);
1184 }
1185