xref: /titanic_44/usr/src/uts/sparc/os/syscall.c (revision de860bd9529e1034e1666f74f7bc0ec8cd5ca701)
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 2007 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)(uintptr_t)(caddr32_t)(uintptr_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)(uintptr_t)(caddr32_t)(uintptr_t)
311 				    (rp->r_sp + 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)(uintptr_t)(caddr32_t)(uintptr_t)
329 				    (rp->r_sp + 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 	if (audit_active) {	/* begin auditing for this syscall */
500 		int error;
501 		if (error = audit_start(T_SYSCALL, code, 0, lwp)) {
502 			t->t_pre_sys = 1;	/* repost anyway */
503 			lwp->lwp_error = 0;	/* for old drivers */
504 			return (error);
505 		}
506 		repost = 1;
507 	}
508 
509 #ifndef NPROBE
510 	/* Kernel probe */
511 	if (tnf_tracing_active) {
512 		TNF_PROBE_1(syscall_start, "syscall thread", /* CSTYLED */,
513 			tnf_sysnum,	sysnum,		t->t_sysnum);
514 		t->t_post_sys = 1;	/* make sure post_syscall runs */
515 		repost = 1;
516 	}
517 #endif /* NPROBE */
518 
519 #ifdef SYSCALLTRACE
520 	if (syscalltrace) {
521 		int i;
522 		long *ap;
523 		char *cp;
524 		char *sysname;
525 		struct sysent *callp;
526 
527 		if (code >= NSYSCALL)
528 			callp = &nosys_ent;	/* nosys has no args */
529 		else
530 			callp = LWP_GETSYSENT(lwp) + code;
531 		(void) save_syscall_args();
532 		mutex_enter(&systrace_lock);
533 		printf("%d: ", p->p_pid);
534 		if (code >= NSYSCALL)
535 			printf("0x%x", code);
536 		else {
537 			sysname = mod_getsysname(code);
538 			printf("%s[0x%x]", sysname == NULL ? "NULL" :
539 			    sysname, code);
540 		}
541 		cp = "(";
542 		for (i = 0, ap = lwp->lwp_ap; i < callp->sy_narg; i++, ap++) {
543 			printf("%s%lx", cp, *ap);
544 			cp = ", ";
545 		}
546 		if (i)
547 			printf(")");
548 		printf(" %s id=0x%p\n", PTOU(p)->u_comm, curthread);
549 		mutex_exit(&systrace_lock);
550 	}
551 #endif /* SYSCALLTRACE */
552 
553 	/*
554 	 * If there was a continuing reason for pre-syscall processing,
555 	 * set the t_pre_sys flag for the next system call.
556 	 */
557 	if (repost)
558 		t->t_pre_sys = 1;
559 	lwp->lwp_error = 0;	/* for old drivers */
560 	lwp->lwp_badpriv = PRIV_NONE;	/* for privilege tracing */
561 	return (0);
562 }
563 
564 /*
565  * Post-syscall processing.  Perform abnormal system call completion
566  * actions such as /proc tracing, profiling, signals, preemption, etc.
567  *
568  * This routine is called only if t_post_sys, t_sig_check, or t_astflag is set.
569  * Any condition requiring pre-syscall handling must set one of these.
570  * If the condition is persistent, this routine will repost t_post_sys.
571  */
572 void
573 post_syscall(long rval1, long rval2)
574 {
575 	kthread_t	*t = curthread;
576 	proc_t	*p = curproc;
577 	klwp_t	*lwp = ttolwp(t);
578 	struct regs *rp = lwptoregs(lwp);
579 	uint_t	error;
580 	int	code = t->t_sysnum;
581 	int	repost = 0;
582 	int	proc_stop = 0;		/* non-zero if stopping for /proc */
583 	int	sigprof = 0;		/* non-zero if sending SIGPROF */
584 
585 	t->t_post_sys = 0;
586 
587 	error = lwp->lwp_errno;
588 
589 	/*
590 	 * Code can be zero if this is a new LWP returning after a forkall(),
591 	 * other than the one which matches the one in the parent which called
592 	 * forkall().  In these LWPs, skip most of post-syscall activity.
593 	 */
594 	if (code == 0)
595 		goto sig_check;
596 
597 	if (audit_active) {	/* put out audit record for this syscall */
598 		rval_t	rval;	/* fix audit_finish() someday */
599 
600 		/* XX64 -- truncation of 64-bit return values? */
601 		rval.r_val1 = (int)rval1;
602 		rval.r_val2 = (int)rval2;
603 		audit_finish(T_SYSCALL, code, error, &rval);
604 		repost = 1;
605 	}
606 
607 	if (curthread->t_pdmsg != NULL) {
608 		char *m = curthread->t_pdmsg;
609 
610 		uprintf("%s", m);
611 		kmem_free(m, strlen(m) + 1);
612 		curthread->t_pdmsg = NULL;
613 	}
614 
615 	/*
616 	 * If we're going to stop for /proc tracing, set the flag and
617 	 * save the arguments so that the return values don't smash them.
618 	 */
619 	if (PTOU(p)->u_systrap) {
620 		if (prismember(&PTOU(p)->u_exitmask, code)) {
621 			proc_stop = 1;
622 			(void) save_syscall_args();
623 		}
624 		repost = 1;
625 	}
626 
627 	/*
628 	 * Similarly check to see if SIGPROF might be sent.
629 	 */
630 	if (curthread->t_rprof != NULL &&
631 	    curthread->t_rprof->rp_anystate != 0) {
632 		(void) save_syscall_args();
633 		sigprof = 1;
634 	}
635 
636 	if (lwp->lwp_eosys == NORMALRETURN) {
637 		if (error == 0) {
638 #ifdef SYSCALLTRACE
639 			if (syscalltrace) {
640 				mutex_enter(&systrace_lock);
641 				printf(
642 				    "%d: r_val1=0x%lx, r_val2=0x%lx, id 0x%p\n",
643 				    p->p_pid, rval1, rval2, curthread);
644 				mutex_exit(&systrace_lock);
645 			}
646 #endif /* SYSCALLTRACE */
647 			rp->r_tstate &= ~TSTATE_IC;
648 			rp->r_o0 = rval1;
649 			rp->r_o1 = rval2;
650 		} else {
651 			int sig;
652 
653 #ifdef SYSCALLTRACE
654 			if (syscalltrace) {
655 				mutex_enter(&systrace_lock);
656 				printf("%d: error=%d, id 0x%p\n",
657 				    p->p_pid, error, curthread);
658 				mutex_exit(&systrace_lock);
659 			}
660 #endif /* SYSCALLTRACE */
661 			if (error == EINTR && t->t_activefd.a_stale)
662 				error = EBADF;
663 			if (error == EINTR &&
664 			    (sig = lwp->lwp_cursig) != 0 &&
665 			    sigismember(&PTOU(p)->u_sigrestart, sig) &&
666 			    PTOU(p)->u_signal[sig - 1] != SIG_DFL &&
667 			    PTOU(p)->u_signal[sig - 1] != SIG_IGN)
668 				error = ERESTART;
669 			rp->r_o0 = error;
670 			rp->r_tstate |= TSTATE_IC;
671 		}
672 		/*
673 		 * The default action is to redo the trap instruction.
674 		 * We increment the pc and npc past it for NORMALRETURN.
675 		 * JUSTRETURN has set up a new pc and npc already.
676 		 * If we are a cloned thread of forkall(), don't
677 		 * adjust here because we have already inherited
678 		 * the adjusted values from our clone.
679 		 */
680 		if (!(t->t_flag & T_FORKALL)) {
681 			rp->r_pc = rp->r_npc;
682 			rp->r_npc += 4;
683 		}
684 	}
685 
686 	/*
687 	 * From the proc(4) manual page:
688 	 * When exit from a system call is being traced, the traced process
689 	 * stops on completion of the system call just prior to checking for
690 	 * signals and returning to user level.  At this point all return
691 	 * values have been stored into the traced process's saved registers.
692 	 */
693 	if (proc_stop) {
694 		mutex_enter(&p->p_lock);
695 		if (PTOU(p)->u_systrap &&
696 		    prismember(&PTOU(p)->u_exitmask, code))
697 			stop(PR_SYSEXIT, code);
698 		mutex_exit(&p->p_lock);
699 	}
700 
701 	/*
702 	 * If we are the parent returning from a successful
703 	 * vfork, wait for the child to exec or exit.
704 	 * This code must be here and not in the bowels of the system
705 	 * so that /proc can intercept exit from vfork in a timely way.
706 	 */
707 	if (t->t_flag & T_VFPARENT) {
708 		ASSERT(code == SYS_vfork || code == SYS_forksys);
709 		ASSERT(rp->r_o1 == 0 && error == 0);
710 		vfwait((pid_t)rval1);
711 		t->t_flag &= ~T_VFPARENT;
712 	}
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 		/*
745 		 * The following check is legal for the following reasons:
746 		 *	1) The thread we are checking, is ourselves, so there is
747 		 *	   no way the proc can go away.
748 		 *	2) The only time we need to be protected by the
749 		 *	   lock is if the binding is changed.
750 		 *
751 		 *	Note we will still take the lock and check the binding
752 		 *	if the condition was true without the lock held.  This
753 		 *	prevents lock contention among threads owned by the
754 		 *	same proc.
755 		 */
756 
757 		if (curthread->t_proc_flag & TP_CHANGEBIND) {
758 			mutex_enter(&p->p_lock);
759 			if (curthread->t_proc_flag & TP_CHANGEBIND) {
760 				timer_lwpbind();
761 				curthread->t_proc_flag &= ~TP_CHANGEBIND;
762 			}
763 			mutex_exit(&p->p_lock);
764 		}
765 
766 		/*
767 		 * for kaio requests on the special kaio poll queue,
768 		 * copyout their results to user memory.
769 		 */
770 		if (p->p_aio)
771 			aio_cleanup(0);
772 
773 		/*
774 		 * If this LWP was asked to hold, call holdlwp(), which will
775 		 * stop.  holdlwps() sets this up and calls pokelwps() which
776 		 * sets the AST flag.
777 		 *
778 		 * Also check TP_EXITLWP, since this is used by fresh new LWPs
779 		 * through lwp_rtt().  That flag is set if the lwp_create(2)
780 		 * syscall failed after creating the LWP.
781 		 */
782 		if (ISHOLD(p) || (t->t_proc_flag & TP_EXITLWP))
783 			holdlwp();
784 
785 		/*
786 		 * All code that sets signals and makes ISSIG_PENDING
787 		 * evaluate true must set t_sig_check afterwards.
788 		 */
789 		if (ISSIG_PENDING(t, lwp, p)) {
790 			if (issig(FORREAL))
791 				psig();
792 			t->t_sig_check = 1;	/* recheck next time */
793 		}
794 
795 		if (sigprof) {
796 			realsigprof(code, error);
797 			t->t_sig_check = 1;	/* recheck next time */
798 		}
799 
800 		/*
801 		 * If a performance counter overflow interrupt was
802 		 * delivered *during* the syscall, then re-enable the
803 		 * AST so that we take a trip through trap() to cause
804 		 * the SIGEMT to be delivered.
805 		 */
806 		if (lwp->lwp_pcb.pcb_flags & CPC_OVERFLOW)
807 			aston(t);
808 
809 		/*
810 		 * If an asynchronous hardware error is pending, turn AST flag
811 		 * back on.  AST will be checked again before we return to user
812 		 * mode and we'll come back through trap() to handle the error.
813 		 */
814 		if (lwp->lwp_pcb.pcb_flags & ASYNC_HWERR)
815 			aston(t);
816 	}
817 
818 	/*
819 	 * Restore register window if a debugger modified it.
820 	 * Set up to perform a single-step if a debugger requested it.
821 	 */
822 	if (lwp->lwp_pcb.pcb_xregstat != XREGNONE)
823 		xregrestore(lwp, 1);
824 
825 	lwp->lwp_errno = 0;		/* clear error for next time */
826 
827 #ifndef NPROBE
828 	/* Kernel probe */
829 	if (tnf_tracing_active) {
830 		TNF_PROBE_3(syscall_end, "syscall thread", /* CSTYLED */,
831 		    tnf_long,	rval1,		rval1,
832 		    tnf_long,	rval2,		rval2,
833 		    tnf_long,	errno,		(long)error);
834 		repost = 1;
835 	}
836 #endif /* NPROBE */
837 
838 	/*
839 	 * Set state to LWP_USER here so preempt won't give us a kernel
840 	 * priority if it occurs after this point.  Call CL_TRAPRET() to
841 	 * restore the user-level priority.
842 	 *
843 	 * It is important that no locks (other than spinlocks) be entered
844 	 * after this point before returning to user mode (unless lwp_state
845 	 * is set back to LWP_SYS).
846 	 *
847 	 * Sampled times past this point are charged to the user.
848 	 */
849 	lwp->lwp_state = LWP_USER;
850 
851 	if (t->t_trapret) {
852 		t->t_trapret = 0;
853 		thread_lock(t);
854 		CL_TRAPRET(t);
855 		thread_unlock(t);
856 	}
857 	if (CPU->cpu_runrun || t->t_schedflag & TS_ANYWAITQ)
858 		preempt();
859 
860 	/*
861 	 * t_post_sys will be set if pcb_step is active.
862 	 */
863 	if (lwp->lwp_pcb.pcb_step != STEP_NONE) {
864 		prdostep();
865 		repost = 1;
866 	}
867 
868 	t->t_sysnum = 0;	/* no longer in a system call */
869 
870 	/*
871 	 * In case the args were copied to the lwp, reset the
872 	 * pointer so the next syscall will have the right lwp_ap pointer.
873 	 */
874 	lwp->lwp_ap = (long *)&rp->r_o0;
875 	lwp->lwp_argsaved = 0;
876 
877 	/*
878 	 * If there was a continuing reason for post-syscall processing,
879 	 * set the t_post_sys flag for the next system call.
880 	 */
881 	if (repost)
882 		t->t_post_sys = 1;
883 
884 	/*
885 	 * If there is a ustack registered for this lwp, and the stack rlimit
886 	 * has been altered, read in the ustack. If the saved stack rlimit
887 	 * matches the bounds of the ustack, update the ustack to reflect
888 	 * the new rlimit. If the new stack rlimit is RLIM_INFINITY, disable
889 	 * stack checking by setting the size to 0.
890 	 */
891 	if (lwp->lwp_ustack != 0 && lwp->lwp_old_stk_ctl != 0) {
892 		rlim64_t new_size;
893 		model_t model;
894 		caddr_t top;
895 		struct rlimit64 rl;
896 
897 		mutex_enter(&p->p_lock);
898 		new_size = p->p_stk_ctl;
899 		model = p->p_model;
900 		top = p->p_usrstack;
901 		(void) rctl_rlimit_get(rctlproc_legacy[RLIMIT_STACK], p, &rl);
902 		mutex_exit(&p->p_lock);
903 
904 		if (rl.rlim_cur == RLIM64_INFINITY)
905 			new_size = 0;
906 
907 		if (model == DATAMODEL_NATIVE) {
908 			stack_t stk;
909 
910 			if (copyin((stack_t *)lwp->lwp_ustack, &stk,
911 			    sizeof (stack_t)) == 0 &&
912 			    (stk.ss_size == lwp->lwp_old_stk_ctl ||
913 			    stk.ss_size == 0) &&
914 			    stk.ss_sp == top - stk.ss_size) {
915 				stk.ss_sp = (void *)((uintptr_t)stk.ss_sp +
916 				    stk.ss_size - new_size);
917 				stk.ss_size = new_size;
918 
919 				(void) copyout(&stk,
920 				    (stack_t *)lwp->lwp_ustack,
921 				    sizeof (stack_t));
922 			}
923 		} else {
924 			stack32_t stk32;
925 
926 			if (copyin((stack32_t *)lwp->lwp_ustack, &stk32,
927 			    sizeof (stack32_t)) == 0 &&
928 			    (stk32.ss_size == lwp->lwp_old_stk_ctl ||
929 			    stk32.ss_size == 0) &&
930 			    stk32.ss_sp ==
931 			    (caddr32_t)(uintptr_t)(top - stk32.ss_size)) {
932 				stk32.ss_sp += stk32.ss_size - new_size;
933 				stk32.ss_size = new_size;
934 
935 				(void) copyout(&stk32,
936 				    (stack32_t *)lwp->lwp_ustack,
937 				    sizeof (stack32_t));
938 			}
939 		}
940 
941 		lwp->lwp_old_stk_ctl = 0;
942 	}
943 
944 	syscall_mstate(LMS_SYSTEM, LMS_USER);
945 }
946 
947 /*
948  * Call a system call which takes a pointer to the user args struct and
949  * a pointer to the return values.  This is a bit slower than the standard
950  * C arg-passing method in some cases.
951  */
952 int64_t
953 syscall_ap()
954 {
955 	uint_t	error;
956 	struct sysent *callp;
957 	rval_t	rval;
958 	klwp_t	*lwp = ttolwp(curthread);
959 	struct regs *rp = lwptoregs(lwp);
960 
961 	callp = LWP_GETSYSENT(lwp) + curthread->t_sysnum;
962 
963 	/*
964 	 * If the arguments don't fit in registers %o0 - o5, make sure they
965 	 * have been copied to the lwp_arg array.
966 	 */
967 	if (callp->sy_narg > 6 && save_syscall_args())
968 		return ((int64_t)set_errno(EFAULT));
969 
970 	rval.r_val1 = 0;
971 	rval.r_val2 = (int)rp->r_o1;
972 	lwp->lwp_error = 0;	/* for old drivers */
973 	error = (*(callp->sy_call))(lwp->lwp_ap, &rval);
974 	if (error)
975 		return ((int64_t)set_errno(error));
976 	return (rval.r_vals);
977 }
978 
979 /*
980  * Load system call module.
981  *	Returns with pointer to held read lock for module.
982  */
983 static krwlock_t *
984 lock_syscall(struct sysent *table, uint_t code)
985 {
986 	krwlock_t	*module_lock;
987 	struct modctl	*modp;
988 	int		id;
989 	struct sysent   *callp;
990 
991 	module_lock = table[code].sy_lock;
992 	callp = &table[code];
993 
994 	/*
995 	 * Optimization to only call modload if we don't have a loaded
996 	 * syscall.
997 	 */
998 	rw_enter(module_lock, RW_READER);
999 	if (LOADED_SYSCALL(callp))
1000 		return (module_lock);
1001 	rw_exit(module_lock);
1002 
1003 	for (;;) {
1004 		if ((id = modload("sys", syscallnames[code])) == -1)
1005 			break;
1006 
1007 		/*
1008 		 * If we loaded successfully at least once, the modctl
1009 		 * will still be valid, so we try to grab it by filename.
1010 		 * If this call fails, it's because the mod_filename
1011 		 * was changed after the call to modload() (mod_hold_by_name()
1012 		 * is the likely culprit).  We can safely just take
1013 		 * another lap if this is the case;  the modload() will
1014 		 * change the mod_filename back to one by which we can
1015 		 * find the modctl.
1016 		 */
1017 		modp = mod_find_by_filename("sys", syscallnames[code]);
1018 
1019 		if (modp == NULL)
1020 			continue;
1021 
1022 		mutex_enter(&mod_lock);
1023 
1024 		if (!modp->mod_installed) {
1025 			mutex_exit(&mod_lock);
1026 			continue;
1027 		}
1028 		break;
1029 	}
1030 
1031 	rw_enter(module_lock, RW_READER);
1032 
1033 	if (id != -1)
1034 		mutex_exit(&mod_lock);
1035 
1036 	return (module_lock);
1037 }
1038 
1039 /*
1040  * Loadable syscall support.
1041  *	If needed, load the module, then reserve it by holding a read
1042  * 	lock for the duration of the call.
1043  *	Later, if the syscall is not unloadable, it could patch the vector.
1044  */
1045 /*ARGSUSED*/
1046 int64_t
1047 loadable_syscall(
1048     long a0, long a1, long a2, long a3,
1049     long a4, long a5, long a6, long a7)
1050 {
1051 	int64_t		rval;
1052 	struct sysent	*callp;
1053 	struct sysent	*se = LWP_GETSYSENT(ttolwp(curthread));
1054 	krwlock_t	*module_lock;
1055 	int		code;
1056 
1057 	code = curthread->t_sysnum;
1058 	callp = se + code;
1059 
1060 	/*
1061 	 * Try to autoload the system call if necessary.
1062 	 */
1063 	module_lock = lock_syscall(se, code);
1064 	THREAD_KPRI_RELEASE();	/* drop priority given by rw_enter */
1065 
1066 	/*
1067 	 * we've locked either the loaded syscall or nosys
1068 	 */
1069 	if (callp->sy_flags & SE_ARGC) {
1070 		int64_t (*sy_call)();
1071 
1072 		sy_call = (int64_t (*)())callp->sy_call;
1073 		rval = (*sy_call)(a0, a1, a2, a3, a4, a5);
1074 	} else {
1075 		rval = syscall_ap();
1076 	}
1077 
1078 	THREAD_KPRI_REQUEST();	/* regain priority from read lock */
1079 	rw_exit(module_lock);
1080 	return (rval);
1081 }
1082 
1083 /*
1084  * Handle indirect system calls.
1085  *	This interface should be deprecated.  The library can handle
1086  *	this more efficiently, but keep this implementation for old binaries.
1087  *
1088  * XX64	Needs some work.
1089  */
1090 int64_t
1091 indir(int code, long a0, long a1, long a2, long a3, long a4)
1092 {
1093 	klwp_t		*lwp = ttolwp(curthread);
1094 	struct sysent	*callp;
1095 
1096 	if (code <= 0 || code >= NSYSCALL)
1097 		return (nosys());
1098 
1099 	ASSERT(lwp->lwp_ap != NULL);
1100 
1101 	curthread->t_sysnum = code;
1102 	callp = LWP_GETSYSENT(lwp) + code;
1103 
1104 	/*
1105 	 * Handle argument setup, unless already done in pre_syscall().
1106 	 */
1107 	if (callp->sy_narg > 5) {
1108 		if (save_syscall_args()) 	/* move args to LWP array */
1109 			return ((int64_t)set_errno(EFAULT));
1110 	} else if (!lwp->lwp_argsaved) {
1111 		long *ap;
1112 
1113 		ap = lwp->lwp_ap;		/* args haven't been saved */
1114 		lwp->lwp_ap = ap + 1;		/* advance arg pointer */
1115 		curthread->t_post_sys = 1;	/* so lwp_ap will be reset */
1116 	}
1117 	return ((*callp->sy_callc)(a0, a1, a2, a3, a4, lwp->lwp_arg[5]));
1118 }
1119 
1120 /*
1121  * set_errno - set an error return from the current system call.
1122  *	This could be a macro.
1123  *	This returns the value it is passed, so that the caller can
1124  *	use tail-recursion-elimination and do return (set_errno(ERRNO));
1125  */
1126 uint_t
1127 set_errno(uint_t error)
1128 {
1129 	ASSERT(error != 0);		/* must not be used to clear errno */
1130 
1131 	curthread->t_post_sys = 1;	/* have post_syscall do error return */
1132 	return (ttolwp(curthread)->lwp_errno = error);
1133 }
1134 
1135 /*
1136  * set_proc_pre_sys - Set pre-syscall processing for entire process.
1137  */
1138 void
1139 set_proc_pre_sys(proc_t *p)
1140 {
1141 	kthread_t	*t;
1142 	kthread_t	*first;
1143 
1144 	ASSERT(MUTEX_HELD(&p->p_lock));
1145 
1146 	t = first = p->p_tlist;
1147 	do {
1148 		t->t_pre_sys = 1;
1149 	} while ((t = t->t_forw) != first);
1150 }
1151 
1152 /*
1153  * set_proc_post_sys - Set post-syscall processing for entire process.
1154  */
1155 void
1156 set_proc_post_sys(proc_t *p)
1157 {
1158 	kthread_t	*t;
1159 	kthread_t	*first;
1160 
1161 	ASSERT(MUTEX_HELD(&p->p_lock));
1162 
1163 	t = first = p->p_tlist;
1164 	do {
1165 		t->t_post_sys = 1;
1166 	} while ((t = t->t_forw) != first);
1167 }
1168 
1169 /*
1170  * set_proc_sys - Set pre- and post-syscall processing for entire process.
1171  */
1172 void
1173 set_proc_sys(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 		t->t_pre_sys = 1;
1183 		t->t_post_sys = 1;
1184 	} while ((t = t->t_forw) != first);
1185 }
1186 
1187 /*
1188  * set_all_proc_sys - set pre- and post-syscall processing flags for all
1189  * user processes.
1190  *
1191  * This is needed when auditing, tracing, or other facilities which affect
1192  * all processes are turned on.
1193  */
1194 void
1195 set_all_proc_sys()
1196 {
1197 	kthread_t	*t;
1198 	kthread_t	*first;
1199 
1200 	mutex_enter(&pidlock);
1201 	t = first = curthread;
1202 	do {
1203 		t->t_pre_sys = 1;
1204 		t->t_post_sys = 1;
1205 	} while ((t = t->t_next) != first);
1206 	mutex_exit(&pidlock);
1207 }
1208 
1209 /*
1210  * set_proc_ast - Set asynchronous service trap (AST) flag for all
1211  * threads in process.
1212  */
1213 void
1214 set_proc_ast(proc_t *p)
1215 {
1216 	kthread_t	*t;
1217 	kthread_t	*first;
1218 
1219 	ASSERT(MUTEX_HELD(&p->p_lock));
1220 
1221 	t = first = p->p_tlist;
1222 	do {
1223 		aston(t);
1224 	} while ((t = t->t_forw) != first);
1225 }
1226