xref: /titanic_44/usr/src/uts/intel/ia32/os/syscall.c (revision aad98a6d8e89f8f5a62a1793da807d4bc5e5b159)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 
23 /*
24  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
25  * Use is subject to license terms.
26  */
27 
28 #pragma ident	"%Z%%M%	%I%	%E% SMI"
29 
30 #include <sys/param.h>
31 #include <sys/vmparam.h>
32 #include <sys/types.h>
33 #include <sys/sysmacros.h>
34 #include <sys/systm.h>
35 #include <sys/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 #ifdef _SYSCALL32_IMPL
838 /*
839  * Execute a 32-bit system call on behalf of the current thread.
840  */
841 void
842 dosyscall(void)
843 {
844 	/*
845 	 * Need space on the stack to store syscall arguments.
846 	 */
847 	long		syscall_args[MAXSYSARGS];
848 	struct sysent	*se;
849 	int64_t		ret;
850 
851 	syscall_mstate(LMS_TRAP, LMS_SYSTEM);
852 
853 	ASSERT(curproc->p_model == DATAMODEL_ILP32);
854 
855 	CPU_STATS_ENTER_K();
856 	CPU_STATS_ADDQ(CPU, sys, syscall, 1);
857 	CPU_STATS_EXIT_K();
858 
859 	se = syscall_entry(curthread, syscall_args);
860 
861 	/*
862 	 * syscall_entry() copied all 8 arguments into syscall_args.
863 	 */
864 	ret = se->sy_callc(syscall_args[0], syscall_args[1], syscall_args[2],
865 	    syscall_args[3], syscall_args[4], syscall_args[5], syscall_args[6],
866 	    syscall_args[7]);
867 
868 	syscall_exit(curthread, (int)ret & 0xffffffffu, (int)(ret >> 32));
869 	syscall_mstate(LMS_SYSTEM, LMS_TRAP);
870 }
871 #endif /* _SYSCALL32_IMPL */
872 
873 /*
874  * Get the arguments to the current system call. See comment atop
875  * save_syscall_args() regarding lwp_ap usage.
876  */
877 
878 uint_t
879 get_syscall_args(klwp_t *lwp, long *argp, int *nargsp)
880 {
881 	kthread_t	*t = lwptot(lwp);
882 	ulong_t	mask = 0xfffffffful;
883 	uint_t	code;
884 	long	*ap;
885 	int	nargs;
886 
887 #if defined(_LP64)
888 	if (lwp_getdatamodel(lwp) == DATAMODEL_LP64)
889 		mask = 0xfffffffffffffffful;
890 #endif
891 
892 	/*
893 	 * The thread lock must be held while looking at the arguments to ensure
894 	 * they don't go away via post_syscall().
895 	 * get_syscall_args() is the only routine to read them which is callable
896 	 * outside the LWP in question and hence the only one that must be
897 	 * synchronized in this manner.
898 	 */
899 	thread_lock(t);
900 
901 	code = t->t_sysnum;
902 	ap = lwp->lwp_ap;
903 
904 	thread_unlock(t);
905 
906 	if (code != 0 && code < NSYSCALL) {
907 		nargs = LWP_GETSYSENT(lwp)[code].sy_narg;
908 
909 		ASSERT(nargs <= MAXSYSARGS);
910 
911 		*nargsp = nargs;
912 		while (nargs-- > 0)
913 			*argp++ = *ap++ & mask;
914 	} else {
915 		*nargsp = 0;
916 	}
917 
918 	return (code);
919 }
920 
921 #ifdef _SYSCALL32_IMPL
922 /*
923  * Get the arguments to the current 32-bit system call.
924  */
925 uint_t
926 get_syscall32_args(klwp_t *lwp, int *argp, int *nargsp)
927 {
928 	long args[MAXSYSARGS];
929 	uint_t i, code;
930 
931 	code = get_syscall_args(lwp, args, nargsp);
932 
933 	for (i = 0; i != *nargsp; i++)
934 		*argp++ = (int)args[i];
935 	return (code);
936 }
937 #endif
938 
939 /*
940  * Save the system call arguments in a safe place.
941  *
942  * On the i386 kernel:
943  *
944  *	Copy the users args prior to changing the stack or stack pointer.
945  *	This is so /proc will be able to get a valid copy of the
946  *	args from the user stack even after the user stack has been changed.
947  *	Note that the kernel stack copy of the args may also have been
948  *	changed by a system call handler which takes C-style arguments.
949  *
950  *	Note that this may be called by stop() from trap().  In that case
951  *	t_sysnum will be zero (syscall_exit clears it), so no args will be
952  *	copied.
953  *
954  * On the amd64 kernel:
955  *
956  *	For 64-bit applications, lwp->lwp_ap normally points to %rdi..%r9
957  *	in the reg structure. If the user is going to change the argument
958  *	registers, rax, or the stack and might want to get the args (for
959  *	/proc tracing), it must copy the args elsewhere via save_syscall_args().
960  *
961  *	For 32-bit applications, lwp->lwp_ap normally points to a copy of
962  *	the system call arguments on the kernel stack made from the user
963  *	stack.  Copy the args prior to change the stack or stack pointer.
964  *	This is so /proc will be able to get a valid copy of the args
965  *	from the user stack even after that stack has been changed.
966  *
967  *	This may be called from stop() even when we're not in a system call.
968  *	Since there's no easy way to tell, this must be safe (not panic).
969  *	If the copyins get data faults, return non-zero.
970  */
971 int
972 save_syscall_args()
973 {
974 	kthread_t	*t = curthread;
975 	klwp_t		*lwp = ttolwp(t);
976 	uint_t		code = t->t_sysnum;
977 	uint_t		nargs;
978 
979 	if (lwp->lwp_argsaved || code == 0)
980 		return (0);		/* args already saved or not needed */
981 
982 	if (code >= NSYSCALL) {
983 		nargs = 0;		/* illegal syscall */
984 	} else {
985 		struct sysent *se = LWP_GETSYSENT(lwp);
986 		struct sysent *callp = se + code;
987 
988 		nargs = callp->sy_narg;
989 		if (LOADABLE_SYSCALL(callp) && nargs == 0) {
990 			krwlock_t	*module_lock;
991 
992 			/*
993 			 * Find out how many arguments the system
994 			 * call uses.
995 			 *
996 			 * We have the property that loaded syscalls
997 			 * never change the number of arguments they
998 			 * use after they've been loaded once.  This
999 			 * allows us to stop for /proc tracing without
1000 			 * holding the module lock.
1001 			 * /proc is assured that sy_narg is valid.
1002 			 */
1003 			module_lock = lock_syscall(se, code);
1004 			nargs = callp->sy_narg;
1005 			rw_exit(module_lock);
1006 		}
1007 	}
1008 
1009 	/*
1010 	 * Fetch the system call arguments.
1011 	 */
1012 	if (nargs == 0)
1013 		goto out;
1014 
1015 	ASSERT(nargs <= MAXSYSARGS);
1016 
1017 	if (lwp_getdatamodel(lwp) == DATAMODEL_NATIVE) {
1018 #if defined(_LP64)
1019 		struct regs *rp = lwptoregs(lwp);
1020 
1021 		lwp->lwp_arg[0] = rp->r_rdi;
1022 		lwp->lwp_arg[1] = rp->r_rsi;
1023 		lwp->lwp_arg[2] = rp->r_rdx;
1024 		lwp->lwp_arg[3] = rp->r_rcx;
1025 		lwp->lwp_arg[4] = rp->r_r8;
1026 		lwp->lwp_arg[5] = rp->r_r9;
1027 		if (nargs > 6 && copyin_args(rp, &lwp->lwp_arg[6], nargs - 6))
1028 			return (-1);
1029 	} else {
1030 #endif
1031 		if (COPYIN_ARGS32(lwptoregs(lwp), lwp->lwp_arg, nargs))
1032 			return (-1);
1033 	}
1034 out:
1035 	lwp->lwp_ap = lwp->lwp_arg;
1036 	lwp->lwp_argsaved = 1;
1037 	t->t_post_sys = 1;	/* so lwp_ap will be reset */
1038 	return (0);
1039 }
1040 
1041 void
1042 reset_syscall_args(void)
1043 {
1044 	ttolwp(curthread)->lwp_argsaved = 0;
1045 }
1046 
1047 /*
1048  * Call a system call which takes a pointer to the user args struct and
1049  * a pointer to the return values.  This is a bit slower than the standard
1050  * C arg-passing method in some cases.
1051  */
1052 int64_t
1053 syscall_ap(void)
1054 {
1055 	uint_t	error;
1056 	struct sysent *callp;
1057 	rval_t	rval;
1058 	kthread_t *t = curthread;
1059 	klwp_t	*lwp = ttolwp(t);
1060 	struct regs *rp = lwptoregs(lwp);
1061 
1062 	callp = LWP_GETSYSENT(lwp) + t->t_sysnum;
1063 
1064 #if defined(__amd64)
1065 	/*
1066 	 * If the arguments don't fit in registers %rdi-%r9, make sure they
1067 	 * have been copied to the lwp_arg array.
1068 	 */
1069 	if (callp->sy_narg > 6 && save_syscall_args())
1070 		return ((int64_t)set_errno(EFAULT));
1071 #endif
1072 
1073 	rval.r_val1 = 0;
1074 	rval.r_val2 = rp->r_r1;
1075 	lwp->lwp_error = 0;	/* for old drivers */
1076 	error = (*(callp->sy_call))(lwp->lwp_ap, &rval);
1077 	if (error)
1078 		return ((longlong_t)set_errno(error));
1079 	return (rval.r_vals);
1080 }
1081 
1082 /*
1083  * Load system call module.
1084  *	Returns with pointer to held read lock for module.
1085  */
1086 static krwlock_t *
1087 lock_syscall(struct sysent *table, uint_t code)
1088 {
1089 	krwlock_t	*module_lock;
1090 	struct modctl	*modp;
1091 	int		id;
1092 	struct sysent   *callp;
1093 
1094 	callp = table + code;
1095 	module_lock = callp->sy_lock;
1096 
1097 	/*
1098 	 * Optimization to only call modload if we don't have a loaded
1099 	 * syscall.
1100 	 */
1101 	rw_enter(module_lock, RW_READER);
1102 	if (LOADED_SYSCALL(callp))
1103 		return (module_lock);
1104 	rw_exit(module_lock);
1105 
1106 	for (;;) {
1107 		if ((id = modload("sys", syscallnames[code])) == -1)
1108 			break;
1109 
1110 		/*
1111 		 * If we loaded successfully at least once, the modctl
1112 		 * will still be valid, so we try to grab it by filename.
1113 		 * If this call fails, it's because the mod_filename
1114 		 * was changed after the call to modload() (mod_hold_by_name()
1115 		 * is the likely culprit).  We can safely just take
1116 		 * another lap if this is the case;  the modload() will
1117 		 * change the mod_filename back to one by which we can
1118 		 * find the modctl.
1119 		 */
1120 		modp = mod_find_by_filename("sys", syscallnames[code]);
1121 
1122 		if (modp == NULL)
1123 			continue;
1124 
1125 		mutex_enter(&mod_lock);
1126 
1127 		if (!modp->mod_installed) {
1128 			mutex_exit(&mod_lock);
1129 			continue;
1130 		}
1131 		break;
1132 	}
1133 	rw_enter(module_lock, RW_READER);
1134 
1135 	if (id != -1)
1136 		mutex_exit(&mod_lock);
1137 
1138 	return (module_lock);
1139 }
1140 
1141 /*
1142  * Loadable syscall support.
1143  *	If needed, load the module, then reserve it by holding a read
1144  *	lock for the duration of the call.
1145  *	Later, if the syscall is not unloadable, it could patch the vector.
1146  */
1147 /*ARGSUSED*/
1148 int64_t
1149 loadable_syscall(
1150     long a0, long a1, long a2, long a3,
1151     long a4, long a5, long a6, long a7)
1152 {
1153 	klwp_t *lwp = ttolwp(curthread);
1154 	int64_t	rval;
1155 	struct sysent *callp;
1156 	struct sysent *se = LWP_GETSYSENT(lwp);
1157 	krwlock_t *module_lock;
1158 	int code, error = 0;
1159 	int64_t (*sy_call)();
1160 
1161 	code = curthread->t_sysnum;
1162 	callp = se + code;
1163 
1164 	/*
1165 	 * Try to autoload the system call if necessary
1166 	 */
1167 	module_lock = lock_syscall(se, code);
1168 	THREAD_KPRI_RELEASE();	/* drop priority given by rw_enter */
1169 
1170 	/*
1171 	 * we've locked either the loaded syscall or nosys
1172 	 */
1173 
1174 	if (lwp_getdatamodel(lwp) == DATAMODEL_NATIVE) {
1175 #if defined(_LP64)
1176 		if (callp->sy_flags & SE_ARGC) {
1177 			sy_call = (int64_t (*)())callp->sy_call;
1178 			rval = (*sy_call)(a0, a1, a2, a3, a4, a5);
1179 		} else
1180 			rval = syscall_ap();
1181 	} else {
1182 #endif
1183 		/*
1184 		 * Now that it's loaded, make sure enough args were copied.
1185 		 */
1186 		if (COPYIN_ARGS32(lwptoregs(lwp), lwp->lwp_ap, callp->sy_narg))
1187 			error = EFAULT;
1188 		if (error) {
1189 			rval = set_errno(error);
1190 		} else if (callp->sy_flags & SE_ARGC) {
1191 			sy_call = (int64_t (*)())callp->sy_call;
1192 			rval = (*sy_call)(lwp->lwp_ap[0], lwp->lwp_ap[1],
1193 			    lwp->lwp_ap[2], lwp->lwp_ap[3], lwp->lwp_ap[4],
1194 			    lwp->lwp_ap[5]);
1195 		} else
1196 			rval = syscall_ap();
1197 	}
1198 
1199 	THREAD_KPRI_REQUEST();	/* regain priority from read lock */
1200 	rw_exit(module_lock);
1201 	return (rval);
1202 }
1203 
1204 /*
1205  * Indirect syscall handled in libc on x86 architectures
1206  */
1207 int64_t
1208 indir()
1209 {
1210 	return (nosys());
1211 }
1212 
1213 /*
1214  * set_errno - set an error return from the current system call.
1215  *	This could be a macro.
1216  *	This returns the value it is passed, so that the caller can
1217  *	use tail-recursion-elimination and do return (set_errno(ERRNO));
1218  */
1219 uint_t
1220 set_errno(uint_t error)
1221 {
1222 	ASSERT(error != 0);		/* must not be used to clear errno */
1223 
1224 	curthread->t_post_sys = 1;	/* have post_syscall do error return */
1225 	return (ttolwp(curthread)->lwp_errno = error);
1226 }
1227 
1228 /*
1229  * set_proc_pre_sys - Set pre-syscall processing for entire process.
1230  */
1231 void
1232 set_proc_pre_sys(proc_t *p)
1233 {
1234 	kthread_t	*t;
1235 	kthread_t	*first;
1236 
1237 	ASSERT(MUTEX_HELD(&p->p_lock));
1238 
1239 	t = first = p->p_tlist;
1240 	do {
1241 		t->t_pre_sys = 1;
1242 	} while ((t = t->t_forw) != first);
1243 }
1244 
1245 /*
1246  * set_proc_post_sys - Set post-syscall processing for entire process.
1247  */
1248 void
1249 set_proc_post_sys(proc_t *p)
1250 {
1251 	kthread_t	*t;
1252 	kthread_t	*first;
1253 
1254 	ASSERT(MUTEX_HELD(&p->p_lock));
1255 
1256 	t = first = p->p_tlist;
1257 	do {
1258 		t->t_post_sys = 1;
1259 	} while ((t = t->t_forw) != first);
1260 }
1261 
1262 /*
1263  * set_proc_sys - Set pre- and post-syscall processing for entire process.
1264  */
1265 void
1266 set_proc_sys(proc_t *p)
1267 {
1268 	kthread_t	*t;
1269 	kthread_t	*first;
1270 
1271 	ASSERT(MUTEX_HELD(&p->p_lock));
1272 
1273 	t = first = p->p_tlist;
1274 	do {
1275 		t->t_pre_sys = 1;
1276 		t->t_post_sys = 1;
1277 	} while ((t = t->t_forw) != first);
1278 }
1279 
1280 /*
1281  * set_all_proc_sys - set pre- and post-syscall processing flags for all
1282  * user processes.
1283  *
1284  * This is needed when auditing, tracing, or other facilities which affect
1285  * all processes are turned on.
1286  */
1287 void
1288 set_all_proc_sys()
1289 {
1290 	kthread_t	*t;
1291 	kthread_t	*first;
1292 
1293 	mutex_enter(&pidlock);
1294 	t = first = curthread;
1295 	do {
1296 		t->t_pre_sys = 1;
1297 		t->t_post_sys = 1;
1298 	} while ((t = t->t_next) != first);
1299 	mutex_exit(&pidlock);
1300 }
1301 
1302 /*
1303  * set_proc_ast - Set asynchronous service trap (AST) flag for all
1304  * threads in process.
1305  */
1306 void
1307 set_proc_ast(proc_t *p)
1308 {
1309 	kthread_t	*t;
1310 	kthread_t	*first;
1311 
1312 	ASSERT(MUTEX_HELD(&p->p_lock));
1313 
1314 	t = first = p->p_tlist;
1315 	do {
1316 		aston(t);
1317 	} while ((t = t->t_forw) != first);
1318 }
1319