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