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