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