xref: /titanic_52/usr/src/uts/sparc/os/syscall.c (revision 73427c57f824c3ec3b396181b163f37d50c5b3b1)
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 #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 (t->t_flag & T_VFPARENT) {
712 		ASSERT(code == SYS_vfork || code == SYS_forksys);
713 		ASSERT(rp->r_o1 == 0 && error == 0);
714 		vfwait((pid_t)rval1);
715 		t->t_flag &= ~T_VFPARENT;
716 	}
717 
718 	/*
719 	 * If profiling is active, bill the current PC in user-land
720 	 * and keep reposting until profiling is disabled.
721 	 */
722 	if (p->p_prof.pr_scale) {
723 		if (lwp->lwp_oweupc)
724 			profil_tick(rp->r_pc);
725 		repost = 1;
726 	}
727 
728 sig_check:
729 	/*
730 	 * Reset flag for next time.
731 	 * We must do this after stopping on PR_SYSEXIT
732 	 * because /proc uses the information in lwp_eosys.
733 	 */
734 	lwp->lwp_eosys = NORMALRETURN;
735 	clear_stale_fd();
736 	t->t_flag &= ~T_FORKALL;
737 
738 	if (t->t_astflag | t->t_sig_check) {
739 		/*
740 		 * Turn off the AST flag before checking all the conditions that
741 		 * may have caused an AST.  This flag is on whenever a signal or
742 		 * unusual condition should be handled after the next trap or
743 		 * syscall.
744 		 */
745 		astoff(t);
746 		t->t_sig_check = 0;
747 
748 		/*
749 		 * The following check is legal for the following reasons:
750 		 *	1) The thread we are checking, is ourselves, so there is
751 		 *	   no way the proc can go away.
752 		 *	2) The only time we need to be protected by the
753 		 *	   lock is if the binding is changed.
754 		 *
755 		 *	Note we will still take the lock and check the binding
756 		 *	if the condition was true without the lock held.  This
757 		 *	prevents lock contention among threads owned by the
758 		 *	same proc.
759 		 */
760 
761 		if (curthread->t_proc_flag & TP_CHANGEBIND) {
762 			mutex_enter(&p->p_lock);
763 			if (curthread->t_proc_flag & TP_CHANGEBIND) {
764 				timer_lwpbind();
765 				curthread->t_proc_flag &= ~TP_CHANGEBIND;
766 			}
767 			mutex_exit(&p->p_lock);
768 		}
769 
770 		/*
771 		 * for kaio requests on the special kaio poll queue,
772 		 * copyout their results to user memory.
773 		 */
774 		if (p->p_aio)
775 			aio_cleanup(0);
776 
777 		/*
778 		 * If this LWP was asked to hold, call holdlwp(), which will
779 		 * stop.  holdlwps() sets this up and calls pokelwps() which
780 		 * sets the AST flag.
781 		 *
782 		 * Also check TP_EXITLWP, since this is used by fresh new LWPs
783 		 * through lwp_rtt().  That flag is set if the lwp_create(2)
784 		 * syscall failed after creating the LWP.
785 		 */
786 		if (ISHOLD(p) || (t->t_proc_flag & TP_EXITLWP))
787 			holdlwp();
788 
789 		/*
790 		 * All code that sets signals and makes ISSIG_PENDING
791 		 * evaluate true must set t_sig_check afterwards.
792 		 */
793 		if (ISSIG_PENDING(t, lwp, p)) {
794 			if (issig(FORREAL))
795 				psig();
796 			t->t_sig_check = 1;	/* recheck next time */
797 		}
798 
799 		if (sigprof) {
800 			realsigprof(code, error);
801 			t->t_sig_check = 1;	/* recheck next time */
802 		}
803 
804 		/*
805 		 * If a performance counter overflow interrupt was
806 		 * delivered *during* the syscall, then re-enable the
807 		 * AST so that we take a trip through trap() to cause
808 		 * the SIGEMT to be delivered.
809 		 */
810 		if (lwp->lwp_pcb.pcb_flags & CPC_OVERFLOW)
811 			aston(t);
812 
813 		/*
814 		 * If an asynchronous hardware error is pending, turn AST flag
815 		 * back on.  AST will be checked again before we return to user
816 		 * mode and we'll come back through trap() to handle the error.
817 		 */
818 		if (lwp->lwp_pcb.pcb_flags & ASYNC_HWERR)
819 			aston(t);
820 	}
821 
822 	/*
823 	 * Restore register window if a debugger modified it.
824 	 * Set up to perform a single-step if a debugger requested it.
825 	 */
826 	if (lwp->lwp_pcb.pcb_xregstat != XREGNONE)
827 		xregrestore(lwp, 1);
828 
829 	lwp->lwp_errno = 0;		/* clear error for next time */
830 
831 #ifndef NPROBE
832 	/* Kernel probe */
833 	if (tnf_tracing_active) {
834 		TNF_PROBE_3(syscall_end, "syscall thread", /* CSTYLED */,
835 			tnf_long,	rval1,		rval1,
836 			tnf_long,	rval2,		rval2,
837 			tnf_long,	errno,		(long)error);
838 		repost = 1;
839 	}
840 #endif /* NPROBE */
841 
842 	/*
843 	 * Set state to LWP_USER here so preempt won't give us a kernel
844 	 * priority if it occurs after this point.  Call CL_TRAPRET() to
845 	 * restore the user-level priority.
846 	 *
847 	 * It is important that no locks (other than spinlocks) be entered
848 	 * after this point before returning to user mode (unless lwp_state
849 	 * is set back to LWP_SYS).
850 	 *
851 	 * Sampled times past this point are charged to the user.
852 	 */
853 	lwp->lwp_state = LWP_USER;
854 
855 	if (t->t_trapret) {
856 		t->t_trapret = 0;
857 		thread_lock(t);
858 		CL_TRAPRET(t);
859 		thread_unlock(t);
860 	}
861 	if (CPU->cpu_runrun || t->t_schedflag & TS_ANYWAITQ)
862 		preempt();
863 
864 	/*
865 	 * t_post_sys will be set if pcb_step is active.
866 	 */
867 	if (lwp->lwp_pcb.pcb_step != STEP_NONE) {
868 		prdostep();
869 		repost = 1;
870 	}
871 
872 	t->t_sysnum = 0;	/* no longer in a system call */
873 
874 	/*
875 	 * In case the args were copied to the lwp, reset the
876 	 * pointer so the next syscall will have the right lwp_ap pointer.
877 	 */
878 	lwp->lwp_ap = (long *)&rp->r_o0;
879 	lwp->lwp_argsaved = 0;
880 
881 	/*
882 	 * If there was a continuing reason for post-syscall processing,
883 	 * set the t_post_sys flag for the next system call.
884 	 */
885 	if (repost)
886 		t->t_post_sys = 1;
887 
888 	/*
889 	 * If there is a ustack registered for this lwp, and the stack rlimit
890 	 * has been altered, read in the ustack. If the saved stack rlimit
891 	 * matches the bounds of the ustack, update the ustack to reflect
892 	 * the new rlimit. If the new stack rlimit is RLIM_INFINITY, disable
893 	 * stack checking by setting the size to 0.
894 	 */
895 	if (lwp->lwp_ustack != 0 && lwp->lwp_old_stk_ctl != 0) {
896 		rlim64_t new_size;
897 		model_t model;
898 		caddr_t top;
899 		struct rlimit64 rl;
900 
901 		mutex_enter(&p->p_lock);
902 		new_size = p->p_stk_ctl;
903 		model = p->p_model;
904 		top = p->p_usrstack;
905 		(void) rctl_rlimit_get(rctlproc_legacy[RLIMIT_STACK], p, &rl);
906 		mutex_exit(&p->p_lock);
907 
908 		if (rl.rlim_cur == RLIM64_INFINITY)
909 			new_size = 0;
910 
911 		if (model == DATAMODEL_NATIVE) {
912 			stack_t stk;
913 
914 			if (copyin((stack_t *)lwp->lwp_ustack, &stk,
915 			    sizeof (stack_t)) == 0 &&
916 			    (stk.ss_size == lwp->lwp_old_stk_ctl ||
917 				stk.ss_size == 0) &&
918 			    stk.ss_sp == top - stk.ss_size) {
919 				stk.ss_sp = (void *)((uintptr_t)stk.ss_sp +
920 				    stk.ss_size - new_size);
921 				stk.ss_size = new_size;
922 
923 				(void) copyout(&stk,
924 				    (stack_t *)lwp->lwp_ustack,
925 				    sizeof (stack_t));
926 			}
927 		} else {
928 			stack32_t stk32;
929 
930 			if (copyin((stack32_t *)lwp->lwp_ustack, &stk32,
931 			    sizeof (stack32_t)) == 0 &&
932 			    (stk32.ss_size == lwp->lwp_old_stk_ctl ||
933 			    stk32.ss_size == 0) &&
934 			    stk32.ss_sp ==
935 			    (caddr32_t)(uintptr_t)(top - stk32.ss_size)) {
936 				stk32.ss_sp += stk32.ss_size - new_size;
937 				stk32.ss_size = new_size;
938 
939 				(void) copyout(&stk32,
940 				    (stack32_t *)lwp->lwp_ustack,
941 				    sizeof (stack32_t));
942 			}
943 		}
944 
945 		lwp->lwp_old_stk_ctl = 0;
946 	}
947 
948 	syscall_mstate(LMS_SYSTEM, LMS_USER);
949 }
950 
951 /*
952  * Call a system call which takes a pointer to the user args struct and
953  * a pointer to the return values.  This is a bit slower than the standard
954  * C arg-passing method in some cases.
955  */
956 int64_t
957 syscall_ap()
958 {
959 	uint_t	error;
960 	struct sysent *callp;
961 	rval_t	rval;
962 	klwp_t	*lwp = ttolwp(curthread);
963 	struct regs *rp = lwptoregs(lwp);
964 
965 	callp = LWP_GETSYSENT(lwp) + curthread->t_sysnum;
966 
967 	/*
968 	 * If the arguments don't fit in registers %o0 - o5, make sure they
969 	 * have been copied to the lwp_arg array.
970 	 */
971 	if (callp->sy_narg > 6 && save_syscall_args())
972 		return ((int64_t)set_errno(EFAULT));
973 
974 	rval.r_val1 = 0;
975 	rval.r_val2 = (int)rp->r_o1;
976 	lwp->lwp_error = 0;	/* for old drivers */
977 	error = (*(callp->sy_call))(lwp->lwp_ap, &rval);
978 	if (error)
979 		return ((int64_t)set_errno(error));
980 	return (rval.r_vals);
981 }
982 
983 /*
984  * Load system call module.
985  *	Returns with pointer to held read lock for module.
986  */
987 static krwlock_t *
988 lock_syscall(struct sysent *table, uint_t code)
989 {
990 	krwlock_t	*module_lock;
991 	struct modctl	*modp;
992 	int		id;
993 	struct sysent   *callp;
994 
995 	module_lock = table[code].sy_lock;
996 	callp = &table[code];
997 
998 	/*
999 	 * Optimization to only call modload if we don't have a loaded
1000 	 * syscall.
1001 	 */
1002 	rw_enter(module_lock, RW_READER);
1003 	if (LOADED_SYSCALL(callp))
1004 		return (module_lock);
1005 	rw_exit(module_lock);
1006 
1007 	for (;;) {
1008 		if ((id = modload("sys", syscallnames[code])) == -1)
1009 			break;
1010 
1011 		/*
1012 		 * If we loaded successfully at least once, the modctl
1013 		 * will still be valid, so we try to grab it by filename.
1014 		 * If this call fails, it's because the mod_filename
1015 		 * was changed after the call to modload() (mod_hold_by_name()
1016 		 * is the likely culprit).  We can safely just take
1017 		 * another lap if this is the case;  the modload() will
1018 		 * change the mod_filename back to one by which we can
1019 		 * find the modctl.
1020 		 */
1021 		modp = mod_find_by_filename("sys", syscallnames[code]);
1022 
1023 		if (modp == NULL)
1024 			continue;
1025 
1026 		mutex_enter(&mod_lock);
1027 
1028 		if (!modp->mod_installed) {
1029 			mutex_exit(&mod_lock);
1030 			continue;
1031 		}
1032 		break;
1033 	}
1034 
1035 	rw_enter(module_lock, RW_READER);
1036 
1037 	if (id != -1)
1038 		mutex_exit(&mod_lock);
1039 
1040 	return (module_lock);
1041 }
1042 
1043 /*
1044  * Loadable syscall support.
1045  *	If needed, load the module, then reserve it by holding a read
1046  * 	lock for the duration of the call.
1047  *	Later, if the syscall is not unloadable, it could patch the vector.
1048  */
1049 /*ARGSUSED*/
1050 int64_t
1051 loadable_syscall(
1052     long a0, long a1, long a2, long a3,
1053     long a4, long a5, long a6, long a7)
1054 {
1055 	int64_t		rval;
1056 	struct sysent	*callp;
1057 	struct sysent	*se = LWP_GETSYSENT(ttolwp(curthread));
1058 	krwlock_t	*module_lock;
1059 	int		code;
1060 
1061 	code = curthread->t_sysnum;
1062 	callp = se + code;
1063 
1064 	/*
1065 	 * Try to autoload the system call if necessary.
1066 	 */
1067 	module_lock = lock_syscall(se, code);
1068 	THREAD_KPRI_RELEASE();	/* drop priority given by rw_enter */
1069 
1070 	/*
1071 	 * we've locked either the loaded syscall or nosys
1072 	 */
1073 	if (callp->sy_flags & SE_ARGC) {
1074 		int64_t (*sy_call)();
1075 
1076 		sy_call = (int64_t (*)())callp->sy_call;
1077 		rval = (*sy_call)(a0, a1, a2, a3, a4, a5);
1078 	} else {
1079 		rval = syscall_ap();
1080 	}
1081 
1082 	THREAD_KPRI_REQUEST();	/* regain priority from read lock */
1083 	rw_exit(module_lock);
1084 	return (rval);
1085 }
1086 
1087 /*
1088  * Handle indirect system calls.
1089  *	This interface should be deprecated.  The library can handle
1090  *	this more efficiently, but keep this implementation for old binaries.
1091  *
1092  * XX64	Needs some work.
1093  */
1094 int64_t
1095 indir(int code, long a0, long a1, long a2, long a3, long a4)
1096 {
1097 	klwp_t		*lwp = ttolwp(curthread);
1098 	struct sysent	*callp;
1099 
1100 	if (code <= 0 || code >= NSYSCALL)
1101 		return (nosys());
1102 
1103 	ASSERT(lwp->lwp_ap != NULL);
1104 
1105 	curthread->t_sysnum = code;
1106 	callp = LWP_GETSYSENT(lwp) + code;
1107 
1108 	/*
1109 	 * Handle argument setup, unless already done in pre_syscall().
1110 	 */
1111 	if (callp->sy_narg > 5) {
1112 		if (save_syscall_args()) 	/* move args to LWP array */
1113 			return ((int64_t)set_errno(EFAULT));
1114 	} else if (!lwp->lwp_argsaved) {
1115 		long *ap;
1116 
1117 		ap = lwp->lwp_ap;		/* args haven't been saved */
1118 		lwp->lwp_ap = ap + 1;		/* advance arg pointer */
1119 		curthread->t_post_sys = 1;	/* so lwp_ap will be reset */
1120 	}
1121 	return ((*callp->sy_callc)(a0, a1, a2, a3, a4, lwp->lwp_arg[5]));
1122 }
1123 
1124 /*
1125  * set_errno - set an error return from the current system call.
1126  *	This could be a macro.
1127  *	This returns the value it is passed, so that the caller can
1128  *	use tail-recursion-elimination and do return (set_errno(ERRNO));
1129  */
1130 uint_t
1131 set_errno(uint_t error)
1132 {
1133 	ASSERT(error != 0);		/* must not be used to clear errno */
1134 
1135 	curthread->t_post_sys = 1;	/* have post_syscall do error return */
1136 	return (ttolwp(curthread)->lwp_errno = error);
1137 }
1138 
1139 /*
1140  * set_proc_pre_sys - Set pre-syscall processing for entire process.
1141  */
1142 void
1143 set_proc_pre_sys(proc_t *p)
1144 {
1145 	kthread_t	*t;
1146 	kthread_t	*first;
1147 
1148 	ASSERT(MUTEX_HELD(&p->p_lock));
1149 
1150 	t = first = p->p_tlist;
1151 	do {
1152 		t->t_pre_sys = 1;
1153 	} while ((t = t->t_forw) != first);
1154 }
1155 
1156 /*
1157  * set_proc_post_sys - Set post-syscall processing for entire process.
1158  */
1159 void
1160 set_proc_post_sys(proc_t *p)
1161 {
1162 	kthread_t	*t;
1163 	kthread_t	*first;
1164 
1165 	ASSERT(MUTEX_HELD(&p->p_lock));
1166 
1167 	t = first = p->p_tlist;
1168 	do {
1169 		t->t_post_sys = 1;
1170 	} while ((t = t->t_forw) != first);
1171 }
1172 
1173 /*
1174  * set_proc_sys - Set pre- and post-syscall processing for entire process.
1175  */
1176 void
1177 set_proc_sys(proc_t *p)
1178 {
1179 	kthread_t	*t;
1180 	kthread_t	*first;
1181 
1182 	ASSERT(MUTEX_HELD(&p->p_lock));
1183 
1184 	t = first = p->p_tlist;
1185 	do {
1186 		t->t_pre_sys = 1;
1187 		t->t_post_sys = 1;
1188 	} while ((t = t->t_forw) != first);
1189 }
1190 
1191 /*
1192  * set_all_proc_sys - set pre- and post-syscall processing flags for all
1193  * user processes.
1194  *
1195  * This is needed when auditing, tracing, or other facilities which affect
1196  * all processes are turned on.
1197  */
1198 void
1199 set_all_proc_sys()
1200 {
1201 	kthread_t	*t;
1202 	kthread_t	*first;
1203 
1204 	mutex_enter(&pidlock);
1205 	t = first = curthread;
1206 	do {
1207 		t->t_pre_sys = 1;
1208 		t->t_post_sys = 1;
1209 	} while ((t = t->t_next) != first);
1210 	mutex_exit(&pidlock);
1211 }
1212 
1213 /*
1214  * set_proc_ast - Set asynchronous service trap (AST) flag for all
1215  * threads in process.
1216  */
1217 void
1218 set_proc_ast(proc_t *p)
1219 {
1220 	kthread_t	*t;
1221 	kthread_t	*first;
1222 
1223 	ASSERT(MUTEX_HELD(&p->p_lock));
1224 
1225 	t = first = p->p_tlist;
1226 	do {
1227 		aston(t);
1228 	} while ((t = t->t_forw) != first);
1229 }
1230