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