xref: /titanic_44/usr/src/uts/intel/ia32/os/syscall.c (revision 28167c24ba5be8b7c1d05e02d053f4a55cd21cc9)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 
23 /*
24  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
25  * Use is subject to license terms.
26  */
27 
28 #pragma ident	"%Z%%M%	%I%	%E% SMI"
29 
30 #include <sys/param.h>
31 #include <sys/vmparam.h>
32 #include <sys/types.h>
33 #include <sys/sysmacros.h>
34 #include <sys/systm.h>
35 #include <sys/signal.h>
36 #include <sys/stack.h>
37 #include <sys/cred.h>
38 #include <sys/cmn_err.h>
39 #include <sys/user.h>
40 #include <sys/privregs.h>
41 #include <sys/psw.h>
42 #include <sys/debug.h>
43 #include <sys/errno.h>
44 #include <sys/proc.h>
45 #include <sys/modctl.h>
46 #include <sys/var.h>
47 #include <sys/inline.h>
48 #include <sys/syscall.h>
49 #include <sys/ucontext.h>
50 #include <sys/cpuvar.h>
51 #include <sys/siginfo.h>
52 #include <sys/trap.h>
53 #include <sys/vtrace.h>
54 #include <sys/sysinfo.h>
55 #include <sys/procfs.h>
56 #include <c2/audit.h>
57 #include <sys/modctl.h>
58 #include <sys/aio_impl.h>
59 #include <sys/tnf.h>
60 #include <sys/tnf_probe.h>
61 #include <sys/copyops.h>
62 #include <sys/priv.h>
63 #include <sys/msacct.h>
64 
65 int syscalltrace = 0;
66 #ifdef SYSCALLTRACE
67 static kmutex_t systrace_lock;		/* syscall tracing lock */
68 #else
69 #define	syscalltrace 0
70 #endif /* SYSCALLTRACE */
71 
72 typedef	int64_t (*llfcn_t)();	/* function returning long long */
73 
74 int pre_syscall(void);
75 void post_syscall(long rval1, long rval2);
76 static krwlock_t *lock_syscall(struct sysent *, uint_t);
77 static void deferred_singlestep_trap(caddr_t);
78 
79 #ifdef _SYSCALL32_IMPL
80 #define	LWP_GETSYSENT(lwp)	\
81 	(lwp_getdatamodel(lwp) == DATAMODEL_NATIVE ? sysent : sysent32)
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 	mutex_enter(&p->p_lock);
122 	lwp->lwp_siginfo.si_signo = 0;
123 	bzero(curthread->t_rprof, sizeof (*curthread->t_rprof));
124 	mutex_exit(&p->p_lock);
125 }
126 
127 /*
128  * If watchpoints are active, don't make copying in of
129  * system call arguments take a read watchpoint trap.
130  */
131 static int
132 copyin_args(struct regs *rp, long *ap, uint_t nargs)
133 {
134 	greg_t *sp = 1 + (greg_t *)rp->r_sp;		/* skip ret addr */
135 
136 	ASSERT(nargs <= MAXSYSARGS);
137 
138 	return (copyin_nowatch(sp, ap, nargs * sizeof (*sp)));
139 }
140 
141 #if defined(_SYSCALL32_IMPL)
142 static int
143 copyin_args32(struct regs *rp, long *ap, uint_t nargs)
144 {
145 	greg32_t *sp = 1 + (greg32_t *)rp->r_sp;	/* skip ret addr */
146 	uint32_t a32[MAXSYSARGS];
147 	int rc;
148 
149 	ASSERT(nargs <= MAXSYSARGS);
150 
151 	if ((rc = copyin_nowatch(sp, a32, nargs * sizeof (*sp))) == 0) {
152 		uint32_t *a32p = &a32[0];
153 
154 		while (nargs--)
155 			*ap++ = (ulong_t)*a32p++;
156 	}
157 	return (rc);
158 }
159 #define	COPYIN_ARGS32	copyin_args32
160 #else
161 #define	COPYIN_ARGS32	copyin_args
162 #endif
163 
164 /*
165  * Error handler for system calls where arg copy gets fault.
166  */
167 static longlong_t
168 syscall_err()
169 {
170 	return (0);
171 }
172 
173 /*
174  * Corresponding sysent entry to allow syscall_entry caller
175  * to invoke syscall_err.
176  */
177 static struct sysent sysent_err =  {
178 	0, SE_32RVAL1, NULL, NULL, (llfcn_t)syscall_err
179 };
180 
181 /*
182  * Called from syscall() when a non-trivial 32-bit system call occurs.
183  * 	Sets up the args and returns a pointer to the handler.
184  */
185 struct sysent *
186 syscall_entry(kthread_t *t, long *argp)
187 {
188 	klwp_t *lwp = ttolwp(t);
189 	struct regs *rp = lwptoregs(lwp);
190 	unsigned int code;
191 	struct sysent *callp;
192 	struct sysent *se = LWP_GETSYSENT(lwp);
193 	int error = 0;
194 	uint_t nargs;
195 
196 	ASSERT(t == curthread && curthread->t_schedflag & TS_DONT_SWAP);
197 
198 	lwp->lwp_ru.sysc++;
199 	lwp->lwp_eosys = NORMALRETURN;	/* assume this will be normal */
200 
201 	/*
202 	 * Set lwp_ap to point to the args, even if none are needed for this
203 	 * system call.  This is for the loadable-syscall case where the
204 	 * number of args won't be known until the system call is loaded, and
205 	 * also maintains a non-NULL lwp_ap setup for get_syscall_args(). Note
206 	 * that lwp_ap MUST be set to a non-NULL value _BEFORE_ t_sysnum is
207 	 * set to non-zero; otherwise get_syscall_args(), seeing a non-zero
208 	 * t_sysnum for this thread, will charge ahead and dereference lwp_ap.
209 	 */
210 	lwp->lwp_ap = argp;		/* for get_syscall_args */
211 
212 	code = rp->r_r0;
213 	t->t_sysnum = (short)code;
214 	callp = code >= NSYSCALL ? &nosys_ent : se + code;
215 
216 	if ((t->t_pre_sys | syscalltrace) != 0) {
217 		error = pre_syscall();
218 		/*
219 		 * Reset lwp_ap so that the args will be refetched if
220 		 * the lwp stopped for /proc purposes in pre_syscall().
221 		 */
222 		lwp->lwp_argsaved = 0;
223 		lwp->lwp_ap = argp;
224 		if (error)
225 			return (&sysent_err);	/* use dummy handler */
226 	}
227 
228 	/*
229 	 * Fetch the system call arguments.
230 	 * Note: for loadable system calls the number of arguments required
231 	 * may not be known at this point, and will be zero if the system call
232 	 * was never loaded.  Once the system call has been loaded, the number
233 	 * of args is not allowed to be changed.
234 	 */
235 	if ((nargs = (uint_t)callp->sy_narg) != 0 &&
236 	    COPYIN_ARGS32(rp, argp, nargs)) {
237 		(void) set_errno(EFAULT);
238 		return (&sysent_err);	/* use dummy handler */
239 	}
240 
241 	return (callp);		/* return sysent entry for caller */
242 }
243 
244 void
245 syscall_exit(kthread_t *t, long rval1, long rval2)
246 {
247 	/*
248 	 * Handle signals and other post-call events if necessary.
249 	 */
250 	if ((t->t_post_sys_ast | syscalltrace) == 0) {
251 		klwp_t *lwp = ttolwp(t);
252 		struct regs *rp = lwptoregs(lwp);
253 
254 		/*
255 		 * Normal return.
256 		 * Clear error indication and set return values.
257 		 */
258 		rp->r_ps &= ~PS_C;	/* reset carry bit */
259 		rp->r_r0 = rval1;
260 		rp->r_r1 = rval2;
261 		lwp->lwp_state = LWP_USER;
262 	} else
263 		post_syscall(rval1, rval2);
264 	t->t_sysnum = 0;		/* invalidate args */
265 }
266 
267 /*
268  * Perform pre-system-call processing, including stopping for tracing,
269  * auditing, etc.
270  *
271  * This routine is called only if the t_pre_sys flag is set. Any condition
272  * requiring pre-syscall handling must set the t_pre_sys flag. If the
273  * condition is persistent, this routine will repost t_pre_sys.
274  */
275 int
276 pre_syscall()
277 {
278 	kthread_t *t = curthread;
279 	unsigned code = t->t_sysnum;
280 	klwp_t *lwp = ttolwp(t);
281 	proc_t *p = ttoproc(t);
282 	int	repost;
283 
284 	t->t_pre_sys = repost = 0;	/* clear pre-syscall processing flag */
285 
286 	ASSERT(t->t_schedflag & TS_DONT_SWAP);
287 
288 #if defined(DEBUG)
289 	/*
290 	 * On the i386 kernel, lwp_ap points at the piece of the thread
291 	 * stack that we copy the users arguments into.
292 	 *
293 	 * On the amd64 kernel, the syscall arguments in the rdi..r9
294 	 * registers should be pointed at by lwp_ap.  If the args need to
295 	 * be copied so that those registers can be changed without losing
296 	 * the ability to get the args for /proc, they can be saved by
297 	 * save_syscall_args(), and lwp_ap will be restored by post_syscall().
298 	 */
299 	if (lwp_getdatamodel(lwp) == DATAMODEL_NATIVE) {
300 #if defined(_LP64)
301 		ASSERT(lwp->lwp_ap == (long *)&lwptoregs(lwp)->r_rdi);
302 	} else {
303 #endif
304 		ASSERT((caddr_t)lwp->lwp_ap > t->t_stkbase &&
305 			(caddr_t)lwp->lwp_ap < t->t_stk);
306 	}
307 #endif	/* DEBUG */
308 
309 	/*
310 	 * Make sure the thread is holding the latest credentials for the
311 	 * process.  The credentials in the process right now apply to this
312 	 * thread for the entire system call.
313 	 */
314 	if (t->t_cred != p->p_cred) {
315 		cred_t *oldcred = t->t_cred;
316 		/*
317 		 * DTrace accesses t_cred in probe context.  t_cred must
318 		 * always be either NULL, or point to a valid, allocated cred
319 		 * structure.
320 		 */
321 		t->t_cred = crgetcred();
322 		crfree(oldcred);
323 	}
324 
325 	/*
326 	 * From the proc(4) manual page:
327 	 * When entry to a system call is being traced, the traced process
328 	 * stops after having begun the call to the system but before the
329 	 * system call arguments have been fetched from the process.
330 	 */
331 	if (PTOU(p)->u_systrap) {
332 		if (prismember(&PTOU(p)->u_entrymask, code)) {
333 			mutex_enter(&p->p_lock);
334 			/*
335 			 * Recheck stop condition, now that lock is held.
336 			 */
337 			if (PTOU(p)->u_systrap &&
338 			    prismember(&PTOU(p)->u_entrymask, code)) {
339 				stop(PR_SYSENTRY, code);
340 #if defined(_LP64)
341 				/*
342 				 * Must refetch args since they were
343 				 * possibly modified by /proc.
344 				 * Indicate that a valid copy is in registers.
345 				 */
346 				if (lwp_getdatamodel(lwp) == DATAMODEL_NATIVE) {
347 					lwp->lwp_argsaved = 0;
348 					lwp->lwp_ap =
349 					    (long *)&lwptoregs(lwp)->r_rdi;
350 				}
351 #endif
352 			}
353 			mutex_exit(&p->p_lock);
354 		}
355 		repost = 1;
356 	}
357 
358 	if (lwp->lwp_sysabort) {
359 		/*
360 		 * lwp_sysabort may have been set via /proc while the process
361 		 * was stopped on PR_SYSENTRY.  If so, abort the system call.
362 		 * Override any error from the copyin() of the arguments.
363 		 */
364 		lwp->lwp_sysabort = 0;
365 		(void) set_errno(EINTR);	/* forces post_sys */
366 		t->t_pre_sys = 1;	/* repost anyway */
367 		return (1);		/* don't do system call, return EINTR */
368 	}
369 
370 #ifdef C2_AUDIT
371 	if (audit_active) {	/* begin auditing for this syscall */
372 		int error;
373 		if (error = audit_start(T_SYSCALL, code, 0, lwp)) {
374 			t->t_pre_sys = 1;	/* repost anyway */
375 			(void) set_errno(error);
376 			return (1);
377 		}
378 		repost = 1;
379 	}
380 #endif /* C2_AUDIT */
381 
382 #ifndef NPROBE
383 	/* Kernel probe */
384 	if (tnf_tracing_active) {
385 		TNF_PROBE_1(syscall_start, "syscall thread", /* CSTYLED */,
386 			tnf_sysnum,	sysnum,		t->t_sysnum);
387 		t->t_post_sys = 1;	/* make sure post_syscall runs */
388 		repost = 1;
389 	}
390 #endif /* NPROBE */
391 
392 #ifdef SYSCALLTRACE
393 	if (syscalltrace) {
394 		int i;
395 		long *ap;
396 		char *cp;
397 		char *sysname;
398 		struct sysent *callp;
399 
400 		if (code >= NSYSCALL)
401 			callp = &nosys_ent;	/* nosys has no args */
402 		else
403 			callp = LWP_GETSYSENT(lwp) + code;
404 		(void) save_syscall_args();
405 		mutex_enter(&systrace_lock);
406 		printf("%d: ", p->p_pid);
407 		if (code >= NSYSCALL)
408 			printf("0x%x", code);
409 		else {
410 			sysname = mod_getsysname(code);
411 			printf("%s[0x%x/0x%p]", sysname == NULL ? "NULL" :
412 			    sysname, code, callp->sy_callc);
413 		}
414 		cp = "(";
415 		for (i = 0, ap = lwp->lwp_ap; i < callp->sy_narg; i++, ap++) {
416 			printf("%s%lx", cp, *ap);
417 			cp = ", ";
418 		}
419 		if (i)
420 			printf(")");
421 		printf(" %s id=0x%p\n", PTOU(p)->u_comm, curthread);
422 		mutex_exit(&systrace_lock);
423 	}
424 #endif /* SYSCALLTRACE */
425 
426 	/*
427 	 * If there was a continuing reason for pre-syscall processing,
428 	 * set the t_pre_sys flag for the next system call.
429 	 */
430 	if (repost)
431 		t->t_pre_sys = 1;
432 	lwp->lwp_error = 0;	/* for old drivers */
433 	lwp->lwp_badpriv = PRIV_NONE;
434 	return (0);
435 }
436 
437 
438 /*
439  * Post-syscall processing.  Perform abnormal system call completion
440  * actions such as /proc tracing, profiling, signals, preemption, etc.
441  *
442  * This routine is called only if t_post_sys, t_sig_check, or t_astflag is set.
443  * Any condition requiring pre-syscall handling must set one of these.
444  * If the condition is persistent, this routine will repost t_post_sys.
445  */
446 void
447 post_syscall(long rval1, long rval2)
448 {
449 	kthread_t *t = curthread;
450 	klwp_t *lwp = ttolwp(t);
451 	proc_t *p = ttoproc(t);
452 	struct regs *rp = lwptoregs(lwp);
453 	uint_t	error;
454 	uint_t	code = t->t_sysnum;
455 	int	repost = 0;
456 	int	proc_stop = 0;		/* non-zero if stopping */
457 	int	sigprof = 0;		/* non-zero if sending SIGPROF */
458 
459 	t->t_post_sys = 0;
460 
461 	error = lwp->lwp_errno;
462 
463 	/*
464 	 * Code can be zero if this is a new LWP returning after a forkall(),
465 	 * other than the one which matches the one in the parent which called
466 	 * forkall().  In these LWPs, skip most of post-syscall activity.
467 	 */
468 	if (code == 0)
469 		goto sig_check;
470 
471 #ifdef C2_AUDIT
472 	if (audit_active) {	/* put out audit record for this syscall */
473 		rval_t	rval;
474 
475 		/* XX64 -- truncation of 64-bit return values? */
476 		rval.r_val1 = (int)rval1;
477 		rval.r_val2 = (int)rval2;
478 		audit_finish(T_SYSCALL, code, error, &rval);
479 		repost = 1;
480 	}
481 #endif /* C2_AUDIT */
482 
483 	if (curthread->t_pdmsg != NULL) {
484 		char *m = curthread->t_pdmsg;
485 
486 		uprintf("%s", m);
487 		kmem_free(m, strlen(m) + 1);
488 		curthread->t_pdmsg = NULL;
489 	}
490 
491 	/*
492 	 * If we're going to stop for /proc tracing, set the flag and
493 	 * save the arguments so that the return values don't smash them.
494 	 */
495 	if (PTOU(p)->u_systrap) {
496 		if (prismember(&PTOU(p)->u_exitmask, code)) {
497 			if (lwp_getdatamodel(lwp) == DATAMODEL_LP64)
498 				(void) save_syscall_args();
499 			proc_stop = 1;
500 		}
501 		repost = 1;
502 	}
503 
504 	/*
505 	 * Similarly check to see if SIGPROF might be sent.
506 	 */
507 	if (curthread->t_rprof != NULL &&
508 	    curthread->t_rprof->rp_anystate != 0) {
509 		if (lwp_getdatamodel(lwp) == DATAMODEL_LP64)
510 			(void) save_syscall_args();
511 		sigprof = 1;
512 	}
513 
514 	if (lwp->lwp_eosys == NORMALRETURN) {
515 		if (error == 0) {
516 #ifdef SYSCALLTRACE
517 			if (syscalltrace) {
518 				mutex_enter(&systrace_lock);
519 				printf(
520 				    "%d: r_val1=0x%lx, r_val2=0x%lx, id 0x%p\n",
521 				    p->p_pid, rval1, rval2, curthread);
522 				mutex_exit(&systrace_lock);
523 			}
524 #endif /* SYSCALLTRACE */
525 			rp->r_ps &= ~PS_C;
526 			rp->r_r0 = rval1;
527 			rp->r_r1 = rval2;
528 		} else {
529 			int sig;
530 #ifdef SYSCALLTRACE
531 			if (syscalltrace) {
532 				mutex_enter(&systrace_lock);
533 				printf("%d: error=%d, id 0x%p\n",
534 				    p->p_pid, error, curthread);
535 				mutex_exit(&systrace_lock);
536 			}
537 #endif /* SYSCALLTRACE */
538 			if (error == EINTR && t->t_activefd.a_stale)
539 				error = EBADF;
540 			if (error == EINTR &&
541 			    (sig = lwp->lwp_cursig) != 0 &&
542 			    sigismember(&PTOU(p)->u_sigrestart, sig) &&
543 			    PTOU(p)->u_signal[sig - 1] != SIG_DFL &&
544 			    PTOU(p)->u_signal[sig - 1] != SIG_IGN)
545 				error = ERESTART;
546 			rp->r_r0 = error;
547 			rp->r_ps |= PS_C;
548 		}
549 	}
550 
551 	/*
552 	 * From the proc(4) manual page:
553 	 * When exit from a system call is being traced, the traced process
554 	 * stops on completion of the system call just prior to checking for
555 	 * signals and returning to user level.  At this point all return
556 	 * values have been stored into the traced process's saved registers.
557 	 */
558 	if (proc_stop) {
559 		mutex_enter(&p->p_lock);
560 		if (PTOU(p)->u_systrap &&
561 		    prismember(&PTOU(p)->u_exitmask, code))
562 			stop(PR_SYSEXIT, code);
563 		mutex_exit(&p->p_lock);
564 	}
565 
566 	/*
567 	 * If we are the parent returning from a successful
568 	 * vfork, wait for the child to exec or exit.
569 	 * This code must be here and not in the bowels of the system
570 	 * so that /proc can intercept exit from vfork in a timely way.
571 	 */
572 	if (code == SYS_vfork && rp->r_r1 == 0 && error == 0)
573 		vfwait((pid_t)rval1);
574 
575 	/*
576 	 * If profiling is active, bill the current PC in user-land
577 	 * and keep reposting until profiling is disabled.
578 	 */
579 	if (p->p_prof.pr_scale) {
580 		if (lwp->lwp_oweupc)
581 			profil_tick(rp->r_pc);
582 		repost = 1;
583 	}
584 
585 sig_check:
586 	/*
587 	 * Reset flag for next time.
588 	 * We must do this after stopping on PR_SYSEXIT
589 	 * because /proc uses the information in lwp_eosys.
590 	 */
591 	lwp->lwp_eosys = NORMALRETURN;
592 	clear_stale_fd();
593 	t->t_flag &= ~T_FORKALL;
594 
595 	/*
596 	 * If a single-step trap occurred on a syscall (see trap())
597 	 * recognize it now.  Do this before checking for signals
598 	 * because deferred_singlestep_trap() may generate a SIGTRAP to
599 	 * the LWP or may otherwise mark the LWP to call issig(FORREAL).
600 	 */
601 	if (lwp->lwp_pcb.pcb_flags & DEBUG_PENDING)
602 		deferred_singlestep_trap((caddr_t)rp->r_pc);
603 
604 	if (t->t_astflag | t->t_sig_check) {
605 		/*
606 		 * Turn off the AST flag before checking all the conditions that
607 		 * may have caused an AST.  This flag is on whenever a signal or
608 		 * unusual condition should be handled after the next trap or
609 		 * syscall.
610 		 */
611 		astoff(t);
612 		t->t_sig_check = 0;
613 
614 		mutex_enter(&p->p_lock);
615 		if (curthread->t_proc_flag & TP_CHANGEBIND) {
616 			timer_lwpbind();
617 			curthread->t_proc_flag &= ~TP_CHANGEBIND;
618 		}
619 		mutex_exit(&p->p_lock);
620 
621 		/*
622 		 * for kaio requests on the special kaio poll queue,
623 		 * copyout their results to user memory.
624 		 */
625 		if (p->p_aio)
626 			aio_cleanup(0);
627 		/*
628 		 * If this LWP was asked to hold, call holdlwp(), which will
629 		 * stop.  holdlwps() sets this up and calls pokelwps() which
630 		 * sets the AST flag.
631 		 *
632 		 * Also check TP_EXITLWP, since this is used by fresh new LWPs
633 		 * through lwp_rtt().  That flag is set if the lwp_create(2)
634 		 * syscall failed after creating the LWP.
635 		 */
636 		if (ISHOLD(p) || (t->t_proc_flag & TP_EXITLWP))
637 			holdlwp();
638 
639 		/*
640 		 * All code that sets signals and makes ISSIG_PENDING
641 		 * evaluate true must set t_sig_check afterwards.
642 		 */
643 		if (ISSIG_PENDING(t, lwp, p)) {
644 			if (issig(FORREAL))
645 				psig();
646 			t->t_sig_check = 1;	/* recheck next time */
647 		}
648 
649 		if (sigprof) {
650 			realsigprof(code, error);
651 			t->t_sig_check = 1;	/* recheck next time */
652 		}
653 
654 		/*
655 		 * If a performance counter overflow interrupt was
656 		 * delivered *during* the syscall, then re-enable the
657 		 * AST so that we take a trip through trap() to cause
658 		 * the SIGEMT to be delivered.
659 		 */
660 		if (lwp->lwp_pcb.pcb_flags & CPC_OVERFLOW)
661 			aston(t);
662 	}
663 
664 	lwp->lwp_errno = 0;		/* clear error for next time */
665 
666 #ifndef NPROBE
667 	/* Kernel probe */
668 	if (tnf_tracing_active) {
669 		TNF_PROBE_3(syscall_end, "syscall thread", /* CSTYLED */,
670 			tnf_long,	rval1,		rval1,
671 			tnf_long,	rval2,		rval2,
672 			tnf_long,	errno,		(long)error);
673 		repost = 1;
674 	}
675 #endif /* NPROBE */
676 
677 	/*
678 	 * Set state to LWP_USER here so preempt won't give us a kernel
679 	 * priority if it occurs after this point.  Call CL_TRAPRET() to
680 	 * restore the user-level priority.
681 	 *
682 	 * It is important that no locks (other than spinlocks) be entered
683 	 * after this point before returning to user mode (unless lwp_state
684 	 * is set back to LWP_SYS).
685 	 *
686 	 * XXX Sampled times past this point are charged to the user.
687 	 */
688 	lwp->lwp_state = LWP_USER;
689 
690 	if (t->t_trapret) {
691 		t->t_trapret = 0;
692 		thread_lock(t);
693 		CL_TRAPRET(t);
694 		thread_unlock(t);
695 	}
696 	if (CPU->cpu_runrun)
697 		preempt();
698 
699 	lwp->lwp_errno = 0;		/* clear error for next time */
700 
701 	/*
702 	 * The thread lock must be held in order to clear sysnum and reset
703 	 * lwp_ap atomically with respect to other threads in the system that
704 	 * may be looking at the args via lwp_ap from get_syscall_args().
705 	 */
706 
707 	thread_lock(t);
708 	t->t_sysnum = 0;		/* no longer in a system call */
709 
710 	if (lwp_getdatamodel(lwp) == DATAMODEL_NATIVE) {
711 #if defined(_LP64)
712 		/*
713 		 * In case the args were copied to the lwp, reset the
714 		 * pointer so the next syscall will have the right
715 		 * lwp_ap pointer.
716 		 */
717 		lwp->lwp_ap = (long *)&rp->r_rdi;
718 	} else {
719 #endif
720 		lwp->lwp_ap = NULL;	/* reset on every syscall entry */
721 	}
722 	thread_unlock(t);
723 
724 	lwp->lwp_argsaved = 0;
725 
726 	/*
727 	 * If there was a continuing reason for post-syscall processing,
728 	 * set the t_post_sys flag for the next system call.
729 	 */
730 	if (repost)
731 		t->t_post_sys = 1;
732 
733 	/*
734 	 * If there is a ustack registered for this lwp, and the stack rlimit
735 	 * has been altered, read in the ustack. If the saved stack rlimit
736 	 * matches the bounds of the ustack, update the ustack to reflect
737 	 * the new rlimit. If the new stack rlimit is RLIM_INFINITY, disable
738 	 * stack checking by setting the size to 0.
739 	 */
740 	if (lwp->lwp_ustack != 0 && lwp->lwp_old_stk_ctl != 0) {
741 		rlim64_t new_size;
742 		caddr_t top;
743 		stack_t stk;
744 		struct rlimit64 rl;
745 
746 		mutex_enter(&p->p_lock);
747 		new_size = p->p_stk_ctl;
748 		top = p->p_usrstack;
749 		(void) rctl_rlimit_get(rctlproc_legacy[RLIMIT_STACK], p, &rl);
750 		mutex_exit(&p->p_lock);
751 
752 		if (rl.rlim_cur == RLIM64_INFINITY)
753 			new_size = 0;
754 
755 		if (copyin((stack_t *)lwp->lwp_ustack, &stk,
756 		    sizeof (stack_t)) == 0 &&
757 		    (stk.ss_size == lwp->lwp_old_stk_ctl ||
758 			stk.ss_size == 0) &&
759 		    stk.ss_sp == top - stk.ss_size) {
760 			stk.ss_sp = (void *)((uintptr_t)stk.ss_sp +
761 			    stk.ss_size - (uintptr_t)new_size);
762 			stk.ss_size = new_size;
763 
764 			(void) copyout(&stk, (stack_t *)lwp->lwp_ustack,
765 			    sizeof (stack_t));
766 		}
767 
768 		lwp->lwp_old_stk_ctl = 0;
769 	}
770 }
771 
772 /*
773  * Called from post_syscall() when a deferred singlestep is to be taken.
774  */
775 static void
776 deferred_singlestep_trap(caddr_t pc)
777 {
778 	proc_t *p = ttoproc(curthread);
779 	klwp_t *lwp = ttolwp(curthread);
780 	pcb_t *pcb = &lwp->lwp_pcb;
781 	uint_t fault = 0;
782 	k_siginfo_t siginfo;
783 
784 	bzero(&siginfo, sizeof (siginfo));
785 
786 	/*
787 	 * If both NORMAL_STEP and WATCH_STEP are in
788 	 * effect, give precedence to NORMAL_STEP.
789 	 * If neither is set, user must have set the
790 	 * PS_T bit in %efl; treat this as NORMAL_STEP.
791 	 */
792 	if ((pcb->pcb_flags & NORMAL_STEP) ||
793 	    !(pcb->pcb_flags & WATCH_STEP)) {
794 		siginfo.si_signo = SIGTRAP;
795 		siginfo.si_code = TRAP_TRACE;
796 		siginfo.si_addr  = pc;
797 		fault = FLTTRACE;
798 		if (pcb->pcb_flags & WATCH_STEP)
799 			(void) undo_watch_step(NULL);
800 	} else {
801 		fault = undo_watch_step(&siginfo);
802 	}
803 	pcb->pcb_flags &= ~(DEBUG_PENDING|NORMAL_STEP|WATCH_STEP);
804 
805 	if (fault) {
806 		/*
807 		 * Remember the fault and fault adddress
808 		 * for real-time (SIGPROF) profiling.
809 		 */
810 		lwp->lwp_lastfault = fault;
811 		lwp->lwp_lastfaddr = siginfo.si_addr;
812 		/*
813 		 * If a debugger has declared this fault to be an
814 		 * event of interest, stop the lwp.  Otherwise just
815 		 * deliver the associated signal.
816 		 */
817 		if (prismember(&p->p_fltmask, fault) &&
818 		    stop_on_fault(fault, &siginfo) == 0)
819 			siginfo.si_signo = 0;
820 	}
821 
822 	if (siginfo.si_signo)
823 		trapsig(&siginfo, 1);
824 }
825 
826 /*
827  * nonexistent system call-- signal lwp (may want to handle it)
828  * flag error if lwp won't see signal immediately
829  */
830 int64_t
831 nosys()
832 {
833 	tsignal(curthread, SIGSYS);
834 	return (set_errno(ENOSYS));
835 }
836 
837 /*
838  * Execute a 32-bit system call on behalf of the current thread.
839  */
840 void
841 dosyscall(void)
842 {
843 	/*
844 	 * Need space on the stack to store syscall arguments.
845 	 */
846 	long		syscall_args[MAXSYSARGS];
847 	struct sysent	*se;
848 	int64_t		ret;
849 
850 	syscall_mstate(LMS_TRAP, LMS_SYSTEM);
851 
852 	ASSERT(curproc->p_model == DATAMODEL_ILP32);
853 
854 	CPU_STATS_ENTER_K();
855 	CPU_STATS_ADDQ(CPU, sys, syscall, 1);
856 	CPU_STATS_EXIT_K();
857 
858 	se = syscall_entry(curthread, syscall_args);
859 
860 	/*
861 	 * syscall_entry() copied all 8 arguments into syscall_args.
862 	 */
863 	ret = se->sy_callc(syscall_args[0], syscall_args[1], syscall_args[2],
864 	    syscall_args[3], syscall_args[4], syscall_args[5], syscall_args[6],
865 	    syscall_args[7]);
866 
867 	syscall_exit(curthread, (int)ret & 0xffffffffu, (int)(ret >> 32));
868 	syscall_mstate(LMS_SYSTEM, LMS_TRAP);
869 }
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