xref: /illumos-gate/usr/src/uts/intel/os/syscall.c (revision 2570281c)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
58efd794bSsudheer  * Common Development and Distribution License (the "License").
68efd794bSsudheer  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
21c4978b50Sraf 
227c478bd9Sstevel@tonic-gate /*
2396992ee7SEthindra Ramamurthy  * Copyright (c) 1992, 2010, Oracle and/or its affiliates. All rights reserved.
246a0b1217SPatrick Mooney  * Copyright 2019 Joyent, Inc.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #include <sys/param.h>
287c478bd9Sstevel@tonic-gate #include <sys/vmparam.h>
297c478bd9Sstevel@tonic-gate #include <sys/types.h>
307c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
317c478bd9Sstevel@tonic-gate #include <sys/systm.h>
327c478bd9Sstevel@tonic-gate #include <sys/signal.h>
337c478bd9Sstevel@tonic-gate #include <sys/stack.h>
347c478bd9Sstevel@tonic-gate #include <sys/cred.h>
357c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
367c478bd9Sstevel@tonic-gate #include <sys/user.h>
377c478bd9Sstevel@tonic-gate #include <sys/privregs.h>
387c478bd9Sstevel@tonic-gate #include <sys/psw.h>
397c478bd9Sstevel@tonic-gate #include <sys/debug.h>
407c478bd9Sstevel@tonic-gate #include <sys/errno.h>
417c478bd9Sstevel@tonic-gate #include <sys/proc.h>
427c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
437c478bd9Sstevel@tonic-gate #include <sys/var.h>
447c478bd9Sstevel@tonic-gate #include <sys/inline.h>
457c478bd9Sstevel@tonic-gate #include <sys/syscall.h>
467c478bd9Sstevel@tonic-gate #include <sys/ucontext.h>
477c478bd9Sstevel@tonic-gate #include <sys/cpuvar.h>
487c478bd9Sstevel@tonic-gate #include <sys/siginfo.h>
497c478bd9Sstevel@tonic-gate #include <sys/trap.h>
507c478bd9Sstevel@tonic-gate #include <sys/vtrace.h>
517c478bd9Sstevel@tonic-gate #include <sys/sysinfo.h>
527c478bd9Sstevel@tonic-gate #include <sys/procfs.h>
5307a48826SRoger A. Faulkner #include <sys/prsystm.h>
547c478bd9Sstevel@tonic-gate #include <c2/audit.h>
557c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
567c478bd9Sstevel@tonic-gate #include <sys/aio_impl.h>
577c478bd9Sstevel@tonic-gate #include <sys/copyops.h>
587c478bd9Sstevel@tonic-gate #include <sys/priv.h>
597c478bd9Sstevel@tonic-gate #include <sys/msacct.h>
607c478bd9Sstevel@tonic-gate 
617c478bd9Sstevel@tonic-gate int syscalltrace = 0;
627c478bd9Sstevel@tonic-gate #ifdef SYSCALLTRACE
637c478bd9Sstevel@tonic-gate static kmutex_t systrace_lock;		/* syscall tracing lock */
647c478bd9Sstevel@tonic-gate #else
657c478bd9Sstevel@tonic-gate #define	syscalltrace 0
667c478bd9Sstevel@tonic-gate #endif /* SYSCALLTRACE */
677c478bd9Sstevel@tonic-gate 
687c478bd9Sstevel@tonic-gate typedef	int64_t (*llfcn_t)();	/* function returning long long */
697c478bd9Sstevel@tonic-gate 
707c478bd9Sstevel@tonic-gate int pre_syscall(void);
717c478bd9Sstevel@tonic-gate void post_syscall(long rval1, long rval2);
727c478bd9Sstevel@tonic-gate static krwlock_t *lock_syscall(struct sysent *, uint_t);
73d8aa0f5aSsudheer void deferred_singlestep_trap(caddr_t);
747c478bd9Sstevel@tonic-gate 
757c478bd9Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
767c478bd9Sstevel@tonic-gate #define	LWP_GETSYSENT(lwp)	\
777c478bd9Sstevel@tonic-gate 	(lwp_getdatamodel(lwp) == DATAMODEL_NATIVE ? sysent : sysent32)
787c478bd9Sstevel@tonic-gate #else
797c478bd9Sstevel@tonic-gate #define	LWP_GETSYSENT(lwp)	(sysent)
807c478bd9Sstevel@tonic-gate #endif
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate /*
837c478bd9Sstevel@tonic-gate  * If watchpoints are active, don't make copying in of
847c478bd9Sstevel@tonic-gate  * system call arguments take a read watchpoint trap.
857c478bd9Sstevel@tonic-gate  */
867c478bd9Sstevel@tonic-gate static int
copyin_args(struct regs * rp,long * ap,uint_t nargs)877c478bd9Sstevel@tonic-gate copyin_args(struct regs *rp, long *ap, uint_t nargs)
887c478bd9Sstevel@tonic-gate {
897c478bd9Sstevel@tonic-gate 	greg_t *sp = 1 + (greg_t *)rp->r_sp;		/* skip ret addr */
907c478bd9Sstevel@tonic-gate 
917c478bd9Sstevel@tonic-gate 	ASSERT(nargs <= MAXSYSARGS);
927c478bd9Sstevel@tonic-gate 
937c478bd9Sstevel@tonic-gate 	return (copyin_nowatch(sp, ap, nargs * sizeof (*sp)));
947c478bd9Sstevel@tonic-gate }
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate #if defined(_SYSCALL32_IMPL)
977c478bd9Sstevel@tonic-gate static int
copyin_args32(struct regs * rp,long * ap,uint_t nargs)987c478bd9Sstevel@tonic-gate copyin_args32(struct regs *rp, long *ap, uint_t nargs)
997c478bd9Sstevel@tonic-gate {
1007c478bd9Sstevel@tonic-gate 	greg32_t *sp = 1 + (greg32_t *)rp->r_sp;	/* skip ret addr */
1017c478bd9Sstevel@tonic-gate 	uint32_t a32[MAXSYSARGS];
1027c478bd9Sstevel@tonic-gate 	int rc;
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate 	ASSERT(nargs <= MAXSYSARGS);
1057c478bd9Sstevel@tonic-gate 
1067c478bd9Sstevel@tonic-gate 	if ((rc = copyin_nowatch(sp, a32, nargs * sizeof (*sp))) == 0) {
1077c478bd9Sstevel@tonic-gate 		uint32_t *a32p = &a32[0];
1087c478bd9Sstevel@tonic-gate 
1097c478bd9Sstevel@tonic-gate 		while (nargs--)
1107c478bd9Sstevel@tonic-gate 			*ap++ = (ulong_t)*a32p++;
1117c478bd9Sstevel@tonic-gate 	}
1127c478bd9Sstevel@tonic-gate 	return (rc);
1137c478bd9Sstevel@tonic-gate }
1147c478bd9Sstevel@tonic-gate #define	COPYIN_ARGS32	copyin_args32
1157c478bd9Sstevel@tonic-gate #else
1167c478bd9Sstevel@tonic-gate #define	COPYIN_ARGS32	copyin_args
1177c478bd9Sstevel@tonic-gate #endif
1187c478bd9Sstevel@tonic-gate 
1197c478bd9Sstevel@tonic-gate /*
1207c478bd9Sstevel@tonic-gate  * Error handler for system calls where arg copy gets fault.
1217c478bd9Sstevel@tonic-gate  */
1227c478bd9Sstevel@tonic-gate static longlong_t
syscall_err()1237c478bd9Sstevel@tonic-gate syscall_err()
1247c478bd9Sstevel@tonic-gate {
1257c478bd9Sstevel@tonic-gate 	return (0);
1267c478bd9Sstevel@tonic-gate }
1277c478bd9Sstevel@tonic-gate 
1287c478bd9Sstevel@tonic-gate /*
1297c478bd9Sstevel@tonic-gate  * Corresponding sysent entry to allow syscall_entry caller
1307c478bd9Sstevel@tonic-gate  * to invoke syscall_err.
1317c478bd9Sstevel@tonic-gate  */
1327c478bd9Sstevel@tonic-gate static struct sysent sysent_err =  {
1337c478bd9Sstevel@tonic-gate 	0, SE_32RVAL1, NULL, NULL, (llfcn_t)syscall_err
1347c478bd9Sstevel@tonic-gate };
1357c478bd9Sstevel@tonic-gate 
1367c478bd9Sstevel@tonic-gate /*
1377c478bd9Sstevel@tonic-gate  * Called from syscall() when a non-trivial 32-bit system call occurs.
1381ec00b5aSToomas Soome  *	Sets up the args and returns a pointer to the handler.
1397c478bd9Sstevel@tonic-gate  */
1407c478bd9Sstevel@tonic-gate struct sysent *
syscall_entry(kthread_t * t,long * argp)1417c478bd9Sstevel@tonic-gate syscall_entry(kthread_t *t, long *argp)
1427c478bd9Sstevel@tonic-gate {
1437c478bd9Sstevel@tonic-gate 	klwp_t *lwp = ttolwp(t);
1447c478bd9Sstevel@tonic-gate 	struct regs *rp = lwptoregs(lwp);
1457c478bd9Sstevel@tonic-gate 	unsigned int code;
1467c478bd9Sstevel@tonic-gate 	struct sysent *callp;
1477c478bd9Sstevel@tonic-gate 	struct sysent *se = LWP_GETSYSENT(lwp);
1487c478bd9Sstevel@tonic-gate 	int error = 0;
1497c478bd9Sstevel@tonic-gate 	uint_t nargs;
1507c478bd9Sstevel@tonic-gate 
1517c478bd9Sstevel@tonic-gate 	ASSERT(t == curthread && curthread->t_schedflag & TS_DONT_SWAP);
1527c478bd9Sstevel@tonic-gate 
1537c478bd9Sstevel@tonic-gate 	lwp->lwp_ru.sysc++;
1547c478bd9Sstevel@tonic-gate 	lwp->lwp_eosys = NORMALRETURN;	/* assume this will be normal */
1557c478bd9Sstevel@tonic-gate 
1567c478bd9Sstevel@tonic-gate 	/*
1577c478bd9Sstevel@tonic-gate 	 * Set lwp_ap to point to the args, even if none are needed for this
1587c478bd9Sstevel@tonic-gate 	 * system call.  This is for the loadable-syscall case where the
1597c478bd9Sstevel@tonic-gate 	 * number of args won't be known until the system call is loaded, and
1607c478bd9Sstevel@tonic-gate 	 * also maintains a non-NULL lwp_ap setup for get_syscall_args(). Note
1617c478bd9Sstevel@tonic-gate 	 * that lwp_ap MUST be set to a non-NULL value _BEFORE_ t_sysnum is
1627c478bd9Sstevel@tonic-gate 	 * set to non-zero; otherwise get_syscall_args(), seeing a non-zero
1637c478bd9Sstevel@tonic-gate 	 * t_sysnum for this thread, will charge ahead and dereference lwp_ap.
1647c478bd9Sstevel@tonic-gate 	 */
1657c478bd9Sstevel@tonic-gate 	lwp->lwp_ap = argp;		/* for get_syscall_args */
1667c478bd9Sstevel@tonic-gate 
1677c478bd9Sstevel@tonic-gate 	code = rp->r_r0;
1687c478bd9Sstevel@tonic-gate 	t->t_sysnum = (short)code;
1697c478bd9Sstevel@tonic-gate 	callp = code >= NSYSCALL ? &nosys_ent : se + code;
1707c478bd9Sstevel@tonic-gate 
1717c478bd9Sstevel@tonic-gate 	if ((t->t_pre_sys | syscalltrace) != 0) {
1727c478bd9Sstevel@tonic-gate 		error = pre_syscall();
173be0c6780Sdmick 
1747c478bd9Sstevel@tonic-gate 		/*
175be0c6780Sdmick 		 * pre_syscall() has taken care so that lwp_ap is current;
176be0c6780Sdmick 		 * it either points to syscall-entry-saved amd64 regs,
177be0c6780Sdmick 		 * or it points to lwp_arg[], which has been re-copied from
178be0c6780Sdmick 		 * the ia32 ustack, but either way, it's a current copy after
179be0c6780Sdmick 		 * /proc has possibly mucked with the syscall args.
1807c478bd9Sstevel@tonic-gate 		 */
181be0c6780Sdmick 
1827c478bd9Sstevel@tonic-gate 		if (error)
1837c478bd9Sstevel@tonic-gate 			return (&sysent_err);	/* use dummy handler */
1847c478bd9Sstevel@tonic-gate 	}
1857c478bd9Sstevel@tonic-gate 
1867c478bd9Sstevel@tonic-gate 	/*
187be0c6780Sdmick 	 * Fetch the system call arguments to the kernel stack copy used
188be0c6780Sdmick 	 * for syscall handling.
1897c478bd9Sstevel@tonic-gate 	 * Note: for loadable system calls the number of arguments required
1907c478bd9Sstevel@tonic-gate 	 * may not be known at this point, and will be zero if the system call
1917c478bd9Sstevel@tonic-gate 	 * was never loaded.  Once the system call has been loaded, the number
1927c478bd9Sstevel@tonic-gate 	 * of args is not allowed to be changed.
1937c478bd9Sstevel@tonic-gate 	 */
1947c478bd9Sstevel@tonic-gate 	if ((nargs = (uint_t)callp->sy_narg) != 0 &&
1957c478bd9Sstevel@tonic-gate 	    COPYIN_ARGS32(rp, argp, nargs)) {
1967c478bd9Sstevel@tonic-gate 		(void) set_errno(EFAULT);
1977c478bd9Sstevel@tonic-gate 		return (&sysent_err);	/* use dummy handler */
1987c478bd9Sstevel@tonic-gate 	}
1997c478bd9Sstevel@tonic-gate 
2007c478bd9Sstevel@tonic-gate 	return (callp);		/* return sysent entry for caller */
2017c478bd9Sstevel@tonic-gate }
2027c478bd9Sstevel@tonic-gate 
2037c478bd9Sstevel@tonic-gate void
syscall_exit(kthread_t * t,long rval1,long rval2)2047c478bd9Sstevel@tonic-gate syscall_exit(kthread_t *t, long rval1, long rval2)
2057c478bd9Sstevel@tonic-gate {
2067c478bd9Sstevel@tonic-gate 	/*
2077c478bd9Sstevel@tonic-gate 	 * Handle signals and other post-call events if necessary.
2087c478bd9Sstevel@tonic-gate 	 */
2097c478bd9Sstevel@tonic-gate 	if ((t->t_post_sys_ast | syscalltrace) == 0) {
2107c478bd9Sstevel@tonic-gate 		klwp_t *lwp = ttolwp(t);
2117c478bd9Sstevel@tonic-gate 		struct regs *rp = lwptoregs(lwp);
2127c478bd9Sstevel@tonic-gate 
2137c478bd9Sstevel@tonic-gate 		/*
2147c478bd9Sstevel@tonic-gate 		 * Normal return.
2157c478bd9Sstevel@tonic-gate 		 * Clear error indication and set return values.
2167c478bd9Sstevel@tonic-gate 		 */
2177c478bd9Sstevel@tonic-gate 		rp->r_ps &= ~PS_C;	/* reset carry bit */
2187c478bd9Sstevel@tonic-gate 		rp->r_r0 = rval1;
2197c478bd9Sstevel@tonic-gate 		rp->r_r1 = rval2;
2207c478bd9Sstevel@tonic-gate 		lwp->lwp_state = LWP_USER;
2211ec00b5aSToomas Soome 	} else {
2227c478bd9Sstevel@tonic-gate 		post_syscall(rval1, rval2);
2231ec00b5aSToomas Soome 	}
2247c478bd9Sstevel@tonic-gate 	t->t_sysnum = 0;		/* invalidate args */
2257c478bd9Sstevel@tonic-gate }
2267c478bd9Sstevel@tonic-gate 
2277c478bd9Sstevel@tonic-gate /*
2287c478bd9Sstevel@tonic-gate  * Perform pre-system-call processing, including stopping for tracing,
2297c478bd9Sstevel@tonic-gate  * auditing, etc.
2307c478bd9Sstevel@tonic-gate  *
2317c478bd9Sstevel@tonic-gate  * This routine is called only if the t_pre_sys flag is set. Any condition
2327c478bd9Sstevel@tonic-gate  * requiring pre-syscall handling must set the t_pre_sys flag. If the
2337c478bd9Sstevel@tonic-gate  * condition is persistent, this routine will repost t_pre_sys.
2347c478bd9Sstevel@tonic-gate  */
2357c478bd9Sstevel@tonic-gate int
pre_syscall()2367c478bd9Sstevel@tonic-gate pre_syscall()
2377c478bd9Sstevel@tonic-gate {
2387c478bd9Sstevel@tonic-gate 	kthread_t *t = curthread;
2397c478bd9Sstevel@tonic-gate 	unsigned code = t->t_sysnum;
2407c478bd9Sstevel@tonic-gate 	klwp_t *lwp = ttolwp(t);
2417c478bd9Sstevel@tonic-gate 	proc_t *p = ttoproc(t);
2427c478bd9Sstevel@tonic-gate 	int	repost;
2437c478bd9Sstevel@tonic-gate 
2447c478bd9Sstevel@tonic-gate 	t->t_pre_sys = repost = 0;	/* clear pre-syscall processing flag */
2457c478bd9Sstevel@tonic-gate 
2467c478bd9Sstevel@tonic-gate 	ASSERT(t->t_schedflag & TS_DONT_SWAP);
2477c478bd9Sstevel@tonic-gate 
2487c478bd9Sstevel@tonic-gate #if defined(DEBUG)
2497c478bd9Sstevel@tonic-gate 	/*
2507c478bd9Sstevel@tonic-gate 	 * On the i386 kernel, lwp_ap points at the piece of the thread
2517c478bd9Sstevel@tonic-gate 	 * stack that we copy the users arguments into.
2527c478bd9Sstevel@tonic-gate 	 *
2537c478bd9Sstevel@tonic-gate 	 * On the amd64 kernel, the syscall arguments in the rdi..r9
2547c478bd9Sstevel@tonic-gate 	 * registers should be pointed at by lwp_ap.  If the args need to
2557c478bd9Sstevel@tonic-gate 	 * be copied so that those registers can be changed without losing
2567c478bd9Sstevel@tonic-gate 	 * the ability to get the args for /proc, they can be saved by
2577c478bd9Sstevel@tonic-gate 	 * save_syscall_args(), and lwp_ap will be restored by post_syscall().
2587c478bd9Sstevel@tonic-gate 	 */
2597c478bd9Sstevel@tonic-gate 	if (lwp_getdatamodel(lwp) == DATAMODEL_NATIVE) {
2607c478bd9Sstevel@tonic-gate #if defined(_LP64)
2617c478bd9Sstevel@tonic-gate 		ASSERT(lwp->lwp_ap == (long *)&lwptoregs(lwp)->r_rdi);
2627c478bd9Sstevel@tonic-gate 	} else {
2637c478bd9Sstevel@tonic-gate #endif
2647c478bd9Sstevel@tonic-gate 		ASSERT((caddr_t)lwp->lwp_ap > t->t_stkbase &&
265d3e55dcdSgww 		    (caddr_t)lwp->lwp_ap < t->t_stk);
2667c478bd9Sstevel@tonic-gate 	}
2677c478bd9Sstevel@tonic-gate #endif	/* DEBUG */
2687c478bd9Sstevel@tonic-gate 
2697c478bd9Sstevel@tonic-gate 	/*
2707c478bd9Sstevel@tonic-gate 	 * Make sure the thread is holding the latest credentials for the
2717c478bd9Sstevel@tonic-gate 	 * process.  The credentials in the process right now apply to this
2727c478bd9Sstevel@tonic-gate 	 * thread for the entire system call.
2737c478bd9Sstevel@tonic-gate 	 */
2747c478bd9Sstevel@tonic-gate 	if (t->t_cred != p->p_cred) {
2757c478bd9Sstevel@tonic-gate 		cred_t *oldcred = t->t_cred;
2767c478bd9Sstevel@tonic-gate 		/*
2777c478bd9Sstevel@tonic-gate 		 * DTrace accesses t_cred in probe context.  t_cred must
2787c478bd9Sstevel@tonic-gate 		 * always be either NULL, or point to a valid, allocated cred
2797c478bd9Sstevel@tonic-gate 		 * structure.
2807c478bd9Sstevel@tonic-gate 		 */
2817c478bd9Sstevel@tonic-gate 		t->t_cred = crgetcred();
2827c478bd9Sstevel@tonic-gate 		crfree(oldcred);
2837c478bd9Sstevel@tonic-gate 	}
2847c478bd9Sstevel@tonic-gate 
2857c478bd9Sstevel@tonic-gate 	/*
286*bbf21555SRichard Lowe 	 * From the proc(5) manual page:
2877c478bd9Sstevel@tonic-gate 	 * When entry to a system call is being traced, the traced process
2887c478bd9Sstevel@tonic-gate 	 * stops after having begun the call to the system but before the
2897c478bd9Sstevel@tonic-gate 	 * system call arguments have been fetched from the process.
2907c478bd9Sstevel@tonic-gate 	 */
2917c478bd9Sstevel@tonic-gate 	if (PTOU(p)->u_systrap) {
2927c478bd9Sstevel@tonic-gate 		if (prismember(&PTOU(p)->u_entrymask, code)) {
2937c478bd9Sstevel@tonic-gate 			mutex_enter(&p->p_lock);
2947c478bd9Sstevel@tonic-gate 			/*
2957c478bd9Sstevel@tonic-gate 			 * Recheck stop condition, now that lock is held.
2967c478bd9Sstevel@tonic-gate 			 */
2977c478bd9Sstevel@tonic-gate 			if (PTOU(p)->u_systrap &&
2987c478bd9Sstevel@tonic-gate 			    prismember(&PTOU(p)->u_entrymask, code)) {
2997c478bd9Sstevel@tonic-gate 				stop(PR_SYSENTRY, code);
300be0c6780Sdmick 
3017c478bd9Sstevel@tonic-gate 				/*
302be0c6780Sdmick 				 * /proc may have modified syscall args,
303be0c6780Sdmick 				 * either in regs for amd64 or on ustack
304be0c6780Sdmick 				 * for ia32.  Either way, arrange to
305be0c6780Sdmick 				 * copy them again, both for the syscall
306be0c6780Sdmick 				 * handler and for other consumers in
307be0c6780Sdmick 				 * post_syscall (like audit).  Here, we
308be0c6780Sdmick 				 * only do amd64, and just set lwp_ap
309be0c6780Sdmick 				 * back to the kernel-entry stack copy;
310be0c6780Sdmick 				 * the syscall ml code redoes
311be0c6780Sdmick 				 * move-from-regs to set up for the
312be0c6780Sdmick 				 * syscall handler after we return.  For
313be0c6780Sdmick 				 * ia32, save_syscall_args() below makes
314be0c6780Sdmick 				 * an lwp_ap-accessible copy.
3157c478bd9Sstevel@tonic-gate 				 */
316be0c6780Sdmick #if defined(_LP64)
3177c478bd9Sstevel@tonic-gate 				if (lwp_getdatamodel(lwp) == DATAMODEL_NATIVE) {
3187c478bd9Sstevel@tonic-gate 					lwp->lwp_argsaved = 0;
3197c478bd9Sstevel@tonic-gate 					lwp->lwp_ap =
3207c478bd9Sstevel@tonic-gate 					    (long *)&lwptoregs(lwp)->r_rdi;
3217c478bd9Sstevel@tonic-gate 				}
3227c478bd9Sstevel@tonic-gate #endif
3237c478bd9Sstevel@tonic-gate 			}
3247c478bd9Sstevel@tonic-gate 			mutex_exit(&p->p_lock);
3257c478bd9Sstevel@tonic-gate 		}
3267c478bd9Sstevel@tonic-gate 		repost = 1;
3277c478bd9Sstevel@tonic-gate 	}
3287c478bd9Sstevel@tonic-gate 
329be0c6780Sdmick 	/*
330be0c6780Sdmick 	 * ia32 kernel, or ia32 proc on amd64 kernel: keep args in
331be0c6780Sdmick 	 * lwp_arg for post-syscall processing, regardless of whether
332be0c6780Sdmick 	 * they might have been changed in /proc above.
333be0c6780Sdmick 	 */
334be0c6780Sdmick #if defined(_LP64)
335be0c6780Sdmick 	if (lwp_getdatamodel(lwp) != DATAMODEL_NATIVE)
336be0c6780Sdmick #endif
337be0c6780Sdmick 		(void) save_syscall_args();
338be0c6780Sdmick 
3397c478bd9Sstevel@tonic-gate 	if (lwp->lwp_sysabort) {
3407c478bd9Sstevel@tonic-gate 		/*
3417c478bd9Sstevel@tonic-gate 		 * lwp_sysabort may have been set via /proc while the process
3427c478bd9Sstevel@tonic-gate 		 * was stopped on PR_SYSENTRY.  If so, abort the system call.
3437c478bd9Sstevel@tonic-gate 		 * Override any error from the copyin() of the arguments.
3447c478bd9Sstevel@tonic-gate 		 */
3457c478bd9Sstevel@tonic-gate 		lwp->lwp_sysabort = 0;
3467c478bd9Sstevel@tonic-gate 		(void) set_errno(EINTR);	/* forces post_sys */
3477c478bd9Sstevel@tonic-gate 		t->t_pre_sys = 1;	/* repost anyway */
3487c478bd9Sstevel@tonic-gate 		return (1);		/* don't do system call, return EINTR */
3497c478bd9Sstevel@tonic-gate 	}
3507c478bd9Sstevel@tonic-gate 
351005d3febSMarek Pospisil 	/*
352005d3febSMarek Pospisil 	 * begin auditing for this syscall if the c2audit module is loaded
353005d3febSMarek Pospisil 	 * and auditing is enabled
354005d3febSMarek Pospisil 	 */
355005d3febSMarek Pospisil 	if (audit_active == C2AUDIT_LOADED) {
356005d3febSMarek Pospisil 		uint32_t auditing = au_zone_getstate(NULL);
357005d3febSMarek Pospisil 
358005d3febSMarek Pospisil 		if (auditing & AU_AUDIT_MASK) {
359005d3febSMarek Pospisil 			int error;
360005d3febSMarek Pospisil 			if (error = audit_start(T_SYSCALL, code, auditing, \
361005d3febSMarek Pospisil 			    0, lwp)) {
362005d3febSMarek Pospisil 				t->t_pre_sys = 1;	/* repost anyway */
363005d3febSMarek Pospisil 				(void) set_errno(error);
364005d3febSMarek Pospisil 				return (1);
365005d3febSMarek Pospisil 			}
366005d3febSMarek Pospisil 			repost = 1;
3677c478bd9Sstevel@tonic-gate 		}
3687c478bd9Sstevel@tonic-gate 	}
3697c478bd9Sstevel@tonic-gate 
3707c478bd9Sstevel@tonic-gate #ifdef SYSCALLTRACE
3717c478bd9Sstevel@tonic-gate 	if (syscalltrace) {
3727c478bd9Sstevel@tonic-gate 		int i;
3737c478bd9Sstevel@tonic-gate 		long *ap;
3747c478bd9Sstevel@tonic-gate 		char *cp;
3757c478bd9Sstevel@tonic-gate 		char *sysname;
3767c478bd9Sstevel@tonic-gate 		struct sysent *callp;
3777c478bd9Sstevel@tonic-gate 
3787c478bd9Sstevel@tonic-gate 		if (code >= NSYSCALL)
3797c478bd9Sstevel@tonic-gate 			callp = &nosys_ent;	/* nosys has no args */
3807c478bd9Sstevel@tonic-gate 		else
3817c478bd9Sstevel@tonic-gate 			callp = LWP_GETSYSENT(lwp) + code;
3827c478bd9Sstevel@tonic-gate 		(void) save_syscall_args();
3837c478bd9Sstevel@tonic-gate 		mutex_enter(&systrace_lock);
3847c478bd9Sstevel@tonic-gate 		printf("%d: ", p->p_pid);
3851ec00b5aSToomas Soome 		if (code >= NSYSCALL) {
3867c478bd9Sstevel@tonic-gate 			printf("0x%x", code);
3871ec00b5aSToomas Soome 		} else {
3887c478bd9Sstevel@tonic-gate 			sysname = mod_getsysname(code);
3897c478bd9Sstevel@tonic-gate 			printf("%s[0x%x/0x%p]", sysname == NULL ? "NULL" :
3907c478bd9Sstevel@tonic-gate 			    sysname, code, callp->sy_callc);
3917c478bd9Sstevel@tonic-gate 		}
3927c478bd9Sstevel@tonic-gate 		cp = "(";
3937c478bd9Sstevel@tonic-gate 		for (i = 0, ap = lwp->lwp_ap; i < callp->sy_narg; i++, ap++) {
3947c478bd9Sstevel@tonic-gate 			printf("%s%lx", cp, *ap);
3957c478bd9Sstevel@tonic-gate 			cp = ", ";
3967c478bd9Sstevel@tonic-gate 		}
3977c478bd9Sstevel@tonic-gate 		if (i)
3987c478bd9Sstevel@tonic-gate 			printf(")");
3997c478bd9Sstevel@tonic-gate 		printf(" %s id=0x%p\n", PTOU(p)->u_comm, curthread);
4007c478bd9Sstevel@tonic-gate 		mutex_exit(&systrace_lock);
4017c478bd9Sstevel@tonic-gate 	}
4027c478bd9Sstevel@tonic-gate #endif /* SYSCALLTRACE */
4037c478bd9Sstevel@tonic-gate 
4047c478bd9Sstevel@tonic-gate 	/*
4057c478bd9Sstevel@tonic-gate 	 * If there was a continuing reason for pre-syscall processing,
4067c478bd9Sstevel@tonic-gate 	 * set the t_pre_sys flag for the next system call.
4077c478bd9Sstevel@tonic-gate 	 */
4087c478bd9Sstevel@tonic-gate 	if (repost)
4097c478bd9Sstevel@tonic-gate 		t->t_pre_sys = 1;
4107c478bd9Sstevel@tonic-gate 	lwp->lwp_error = 0;	/* for old drivers */
4117c478bd9Sstevel@tonic-gate 	lwp->lwp_badpriv = PRIV_NONE;
4127c478bd9Sstevel@tonic-gate 	return (0);
4137c478bd9Sstevel@tonic-gate }
4147c478bd9Sstevel@tonic-gate 
4157c478bd9Sstevel@tonic-gate 
4167c478bd9Sstevel@tonic-gate /*
4177c478bd9Sstevel@tonic-gate  * Post-syscall processing.  Perform abnormal system call completion
4187c478bd9Sstevel@tonic-gate  * actions such as /proc tracing, profiling, signals, preemption, etc.
4197c478bd9Sstevel@tonic-gate  *
4207c478bd9Sstevel@tonic-gate  * This routine is called only if t_post_sys, t_sig_check, or t_astflag is set.
4217c478bd9Sstevel@tonic-gate  * Any condition requiring pre-syscall handling must set one of these.
4227c478bd9Sstevel@tonic-gate  * If the condition is persistent, this routine will repost t_post_sys.
4237c478bd9Sstevel@tonic-gate  */
4247c478bd9Sstevel@tonic-gate void
post_syscall(long rval1,long rval2)4257c478bd9Sstevel@tonic-gate post_syscall(long rval1, long rval2)
4267c478bd9Sstevel@tonic-gate {
4277c478bd9Sstevel@tonic-gate 	kthread_t *t = curthread;
4287c478bd9Sstevel@tonic-gate 	klwp_t *lwp = ttolwp(t);
4297c478bd9Sstevel@tonic-gate 	proc_t *p = ttoproc(t);
4307c478bd9Sstevel@tonic-gate 	struct regs *rp = lwptoregs(lwp);
4317c478bd9Sstevel@tonic-gate 	uint_t	error;
4327c478bd9Sstevel@tonic-gate 	uint_t	code = t->t_sysnum;
4337c478bd9Sstevel@tonic-gate 	int	repost = 0;
4347c478bd9Sstevel@tonic-gate 	int	proc_stop = 0;		/* non-zero if stopping */
4357c478bd9Sstevel@tonic-gate 	int	sigprof = 0;		/* non-zero if sending SIGPROF */
4367c478bd9Sstevel@tonic-gate 
4377c478bd9Sstevel@tonic-gate 	t->t_post_sys = 0;
4387c478bd9Sstevel@tonic-gate 
4397c478bd9Sstevel@tonic-gate 	error = lwp->lwp_errno;
4407c478bd9Sstevel@tonic-gate 
4417c478bd9Sstevel@tonic-gate 	/*
4427c478bd9Sstevel@tonic-gate 	 * Code can be zero if this is a new LWP returning after a forkall(),
4437c478bd9Sstevel@tonic-gate 	 * other than the one which matches the one in the parent which called
4447c478bd9Sstevel@tonic-gate 	 * forkall().  In these LWPs, skip most of post-syscall activity.
4457c478bd9Sstevel@tonic-gate 	 */
4467c478bd9Sstevel@tonic-gate 	if (code == 0)
4477c478bd9Sstevel@tonic-gate 		goto sig_check;
4488efd794bSsudheer 	/*
4498efd794bSsudheer 	 * If the trace flag is set, mark the lwp to take a single-step trap
4508efd794bSsudheer 	 * on return to user level (below). The x86 lcall interface and
4518efd794bSsudheer 	 * sysenter has already done this, and turned off the flag, but
4528efd794bSsudheer 	 * amd64 syscall interface has not.
4538efd794bSsudheer 	 */
4548efd794bSsudheer 	if (rp->r_ps & PS_T) {
4558efd794bSsudheer 		lwp->lwp_pcb.pcb_flags |= DEBUG_PENDING;
4568efd794bSsudheer 		rp->r_ps &= ~PS_T;
457d8aa0f5aSsudheer 		aston(curthread);
4588efd794bSsudheer 	}
459005d3febSMarek Pospisil 
460005d3febSMarek Pospisil 	/* put out audit record for this syscall */
461005d3febSMarek Pospisil 	if (AU_AUDITING()) {
4627c478bd9Sstevel@tonic-gate 		rval_t	rval;
4637c478bd9Sstevel@tonic-gate 
4647c478bd9Sstevel@tonic-gate 		/* XX64 -- truncation of 64-bit return values? */
4657c478bd9Sstevel@tonic-gate 		rval.r_val1 = (int)rval1;
4667c478bd9Sstevel@tonic-gate 		rval.r_val2 = (int)rval2;
4677c478bd9Sstevel@tonic-gate 		audit_finish(T_SYSCALL, code, error, &rval);
4687c478bd9Sstevel@tonic-gate 		repost = 1;
4697c478bd9Sstevel@tonic-gate 	}
4707c478bd9Sstevel@tonic-gate 
4717c478bd9Sstevel@tonic-gate 	if (curthread->t_pdmsg != NULL) {
4727c478bd9Sstevel@tonic-gate 		char *m = curthread->t_pdmsg;
4737c478bd9Sstevel@tonic-gate 
4747c478bd9Sstevel@tonic-gate 		uprintf("%s", m);
4757c478bd9Sstevel@tonic-gate 		kmem_free(m, strlen(m) + 1);
4767c478bd9Sstevel@tonic-gate 		curthread->t_pdmsg = NULL;
4777c478bd9Sstevel@tonic-gate 	}
4787c478bd9Sstevel@tonic-gate 
4797c478bd9Sstevel@tonic-gate 	/*
4807c478bd9Sstevel@tonic-gate 	 * If we're going to stop for /proc tracing, set the flag and
4817c478bd9Sstevel@tonic-gate 	 * save the arguments so that the return values don't smash them.
4827c478bd9Sstevel@tonic-gate 	 */
4837c478bd9Sstevel@tonic-gate 	if (PTOU(p)->u_systrap) {
4847c478bd9Sstevel@tonic-gate 		if (prismember(&PTOU(p)->u_exitmask, code)) {
4857c478bd9Sstevel@tonic-gate 			if (lwp_getdatamodel(lwp) == DATAMODEL_LP64)
4867c478bd9Sstevel@tonic-gate 				(void) save_syscall_args();
4877c478bd9Sstevel@tonic-gate 			proc_stop = 1;
4887c478bd9Sstevel@tonic-gate 		}
4897c478bd9Sstevel@tonic-gate 		repost = 1;
4907c478bd9Sstevel@tonic-gate 	}
4917c478bd9Sstevel@tonic-gate 
4927c478bd9Sstevel@tonic-gate 	/*
4937c478bd9Sstevel@tonic-gate 	 * Similarly check to see if SIGPROF might be sent.
4947c478bd9Sstevel@tonic-gate 	 */
4957c478bd9Sstevel@tonic-gate 	if (curthread->t_rprof != NULL &&
4967c478bd9Sstevel@tonic-gate 	    curthread->t_rprof->rp_anystate != 0) {
4977c478bd9Sstevel@tonic-gate 		if (lwp_getdatamodel(lwp) == DATAMODEL_LP64)
4987c478bd9Sstevel@tonic-gate 			(void) save_syscall_args();
4997c478bd9Sstevel@tonic-gate 		sigprof = 1;
5007c478bd9Sstevel@tonic-gate 	}
5017c478bd9Sstevel@tonic-gate 
5027c478bd9Sstevel@tonic-gate 	if (lwp->lwp_eosys == NORMALRETURN) {
5037c478bd9Sstevel@tonic-gate 		if (error == 0) {
5047c478bd9Sstevel@tonic-gate #ifdef SYSCALLTRACE
5057c478bd9Sstevel@tonic-gate 			if (syscalltrace) {
5067c478bd9Sstevel@tonic-gate 				mutex_enter(&systrace_lock);
5077c478bd9Sstevel@tonic-gate 				printf(
5087c478bd9Sstevel@tonic-gate 				    "%d: r_val1=0x%lx, r_val2=0x%lx, id 0x%p\n",
5097c478bd9Sstevel@tonic-gate 				    p->p_pid, rval1, rval2, curthread);
5107c478bd9Sstevel@tonic-gate 				mutex_exit(&systrace_lock);
5117c478bd9Sstevel@tonic-gate 			}
5127c478bd9Sstevel@tonic-gate #endif /* SYSCALLTRACE */
5137c478bd9Sstevel@tonic-gate 			rp->r_ps &= ~PS_C;
5147c478bd9Sstevel@tonic-gate 			rp->r_r0 = rval1;
5157c478bd9Sstevel@tonic-gate 			rp->r_r1 = rval2;
5167c478bd9Sstevel@tonic-gate 		} else {
5177c478bd9Sstevel@tonic-gate 			int sig;
5187c478bd9Sstevel@tonic-gate #ifdef SYSCALLTRACE
5197c478bd9Sstevel@tonic-gate 			if (syscalltrace) {
5207c478bd9Sstevel@tonic-gate 				mutex_enter(&systrace_lock);
5217c478bd9Sstevel@tonic-gate 				printf("%d: error=%d, id 0x%p\n",
5227c478bd9Sstevel@tonic-gate 				    p->p_pid, error, curthread);
5237c478bd9Sstevel@tonic-gate 				mutex_exit(&systrace_lock);
5247c478bd9Sstevel@tonic-gate 			}
5257c478bd9Sstevel@tonic-gate #endif /* SYSCALLTRACE */
5267c478bd9Sstevel@tonic-gate 			if (error == EINTR && t->t_activefd.a_stale)
5277c478bd9Sstevel@tonic-gate 				error = EBADF;
5287c478bd9Sstevel@tonic-gate 			if (error == EINTR &&
5297c478bd9Sstevel@tonic-gate 			    (sig = lwp->lwp_cursig) != 0 &&
5307c478bd9Sstevel@tonic-gate 			    sigismember(&PTOU(p)->u_sigrestart, sig) &&
5317c478bd9Sstevel@tonic-gate 			    PTOU(p)->u_signal[sig - 1] != SIG_DFL &&
5327c478bd9Sstevel@tonic-gate 			    PTOU(p)->u_signal[sig - 1] != SIG_IGN)
5337c478bd9Sstevel@tonic-gate 				error = ERESTART;
5347c478bd9Sstevel@tonic-gate 			rp->r_r0 = error;
5357c478bd9Sstevel@tonic-gate 			rp->r_ps |= PS_C;
5367c478bd9Sstevel@tonic-gate 		}
5377c478bd9Sstevel@tonic-gate 	}
5387c478bd9Sstevel@tonic-gate 
5397c478bd9Sstevel@tonic-gate 	/*
540*bbf21555SRichard Lowe 	 * From the proc(5) manual page:
5417c478bd9Sstevel@tonic-gate 	 * When exit from a system call is being traced, the traced process
5427c478bd9Sstevel@tonic-gate 	 * stops on completion of the system call just prior to checking for
5437c478bd9Sstevel@tonic-gate 	 * signals and returning to user level.  At this point all return
5447c478bd9Sstevel@tonic-gate 	 * values have been stored into the traced process's saved registers.
5457c478bd9Sstevel@tonic-gate 	 */
5467c478bd9Sstevel@tonic-gate 	if (proc_stop) {
5477c478bd9Sstevel@tonic-gate 		mutex_enter(&p->p_lock);
5487c478bd9Sstevel@tonic-gate 		if (PTOU(p)->u_systrap &&
5497c478bd9Sstevel@tonic-gate 		    prismember(&PTOU(p)->u_exitmask, code))
5507c478bd9Sstevel@tonic-gate 			stop(PR_SYSEXIT, code);
5517c478bd9Sstevel@tonic-gate 		mutex_exit(&p->p_lock);
5527c478bd9Sstevel@tonic-gate 	}
5537c478bd9Sstevel@tonic-gate 
5547c478bd9Sstevel@tonic-gate 	/*
5557c478bd9Sstevel@tonic-gate 	 * If we are the parent returning from a successful
5567c478bd9Sstevel@tonic-gate 	 * vfork, wait for the child to exec or exit.
5577c478bd9Sstevel@tonic-gate 	 * This code must be here and not in the bowels of the system
5587c478bd9Sstevel@tonic-gate 	 * so that /proc can intercept exit from vfork in a timely way.
5597c478bd9Sstevel@tonic-gate 	 */
5608d186f16Sraf 	if (t->t_flag & T_VFPARENT) {
561657b1f3dSraf 		ASSERT(code == SYS_vfork || code == SYS_forksys);
562657b1f3dSraf 		ASSERT(rp->r_r1 == 0 && error == 0);
5637c478bd9Sstevel@tonic-gate 		vfwait((pid_t)rval1);
5648d186f16Sraf 		t->t_flag &= ~T_VFPARENT;
565657b1f3dSraf 	}
5667c478bd9Sstevel@tonic-gate 
5677c478bd9Sstevel@tonic-gate 	/*
5687c478bd9Sstevel@tonic-gate 	 * If profiling is active, bill the current PC in user-land
5697c478bd9Sstevel@tonic-gate 	 * and keep reposting until profiling is disabled.
5707c478bd9Sstevel@tonic-gate 	 */
5717c478bd9Sstevel@tonic-gate 	if (p->p_prof.pr_scale) {
5727c478bd9Sstevel@tonic-gate 		if (lwp->lwp_oweupc)
5737c478bd9Sstevel@tonic-gate 			profil_tick(rp->r_pc);
5747c478bd9Sstevel@tonic-gate 		repost = 1;
5757c478bd9Sstevel@tonic-gate 	}
5767c478bd9Sstevel@tonic-gate 
5777c478bd9Sstevel@tonic-gate sig_check:
5787c478bd9Sstevel@tonic-gate 	/*
5797c478bd9Sstevel@tonic-gate 	 * Reset flag for next time.
5807c478bd9Sstevel@tonic-gate 	 * We must do this after stopping on PR_SYSEXIT
5817c478bd9Sstevel@tonic-gate 	 * because /proc uses the information in lwp_eosys.
5827c478bd9Sstevel@tonic-gate 	 */
5837c478bd9Sstevel@tonic-gate 	lwp->lwp_eosys = NORMALRETURN;
5847c478bd9Sstevel@tonic-gate 	clear_stale_fd();
585c4978b50Sraf 	t->t_flag &= ~T_FORKALL;
5867c478bd9Sstevel@tonic-gate 
5877c478bd9Sstevel@tonic-gate 	if (t->t_astflag | t->t_sig_check) {
5887c478bd9Sstevel@tonic-gate 		/*
5897c478bd9Sstevel@tonic-gate 		 * Turn off the AST flag before checking all the conditions that
5907c478bd9Sstevel@tonic-gate 		 * may have caused an AST.  This flag is on whenever a signal or
5917c478bd9Sstevel@tonic-gate 		 * unusual condition should be handled after the next trap or
5927c478bd9Sstevel@tonic-gate 		 * syscall.
5937c478bd9Sstevel@tonic-gate 		 */
5947c478bd9Sstevel@tonic-gate 		astoff(t);
595d8aa0f5aSsudheer 		/*
596d8aa0f5aSsudheer 		 * If a single-step trap occurred on a syscall (see trap())
597d8aa0f5aSsudheer 		 * recognize it now.  Do this before checking for signals
598d8aa0f5aSsudheer 		 * because deferred_singlestep_trap() may generate a SIGTRAP to
599d8aa0f5aSsudheer 		 * the LWP or may otherwise mark the LWP to call issig(FORREAL).
600d8aa0f5aSsudheer 		 */
601d8aa0f5aSsudheer 		if (lwp->lwp_pcb.pcb_flags & DEBUG_PENDING)
602d8aa0f5aSsudheer 			deferred_singlestep_trap((caddr_t)rp->r_pc);
603d8aa0f5aSsudheer 
6047c478bd9Sstevel@tonic-gate 		t->t_sig_check = 0;
6057c478bd9Sstevel@tonic-gate 
606efd37614Sdv 		/*
607efd37614Sdv 		 * The following check is legal for the following reasons:
608efd37614Sdv 		 *	1) The thread we are checking, is ourselves, so there is
609efd37614Sdv 		 *	   no way the proc can go away.
610efd37614Sdv 		 *	2) The only time we need to be protected by the
611efd37614Sdv 		 *	   lock is if the binding is changed.
612efd37614Sdv 		 *
613efd37614Sdv 		 *	Note we will still take the lock and check the binding
614efd37614Sdv 		 *	if the condition was true without the lock held.  This
615efd37614Sdv 		 *	prevents lock contention among threads owned by the
6161ec00b5aSToomas Soome 		 *	same proc.
617efd37614Sdv 		 */
618efd37614Sdv 
6197c478bd9Sstevel@tonic-gate 		if (curthread->t_proc_flag & TP_CHANGEBIND) {
620efd37614Sdv 			mutex_enter(&p->p_lock);
621efd37614Sdv 			if (curthread->t_proc_flag & TP_CHANGEBIND) {
622efd37614Sdv 				timer_lwpbind();
623efd37614Sdv 				curthread->t_proc_flag &= ~TP_CHANGEBIND;
624efd37614Sdv 			}
625efd37614Sdv 			mutex_exit(&p->p_lock);
6267c478bd9Sstevel@tonic-gate 		}
6277c478bd9Sstevel@tonic-gate 
6287c478bd9Sstevel@tonic-gate 		/*
6297c478bd9Sstevel@tonic-gate 		 * for kaio requests on the special kaio poll queue,
6307c478bd9Sstevel@tonic-gate 		 * copyout their results to user memory.
6317c478bd9Sstevel@tonic-gate 		 */
6327c478bd9Sstevel@tonic-gate 		if (p->p_aio)
6337c478bd9Sstevel@tonic-gate 			aio_cleanup(0);
6347c478bd9Sstevel@tonic-gate 		/*
6357c478bd9Sstevel@tonic-gate 		 * If this LWP was asked to hold, call holdlwp(), which will
6367c478bd9Sstevel@tonic-gate 		 * stop.  holdlwps() sets this up and calls pokelwps() which
6377c478bd9Sstevel@tonic-gate 		 * sets the AST flag.
6387c478bd9Sstevel@tonic-gate 		 *
6397c478bd9Sstevel@tonic-gate 		 * Also check TP_EXITLWP, since this is used by fresh new LWPs
6407c478bd9Sstevel@tonic-gate 		 * through lwp_rtt().  That flag is set if the lwp_create(2)
6417c478bd9Sstevel@tonic-gate 		 * syscall failed after creating the LWP.
6427c478bd9Sstevel@tonic-gate 		 */
6437c478bd9Sstevel@tonic-gate 		if (ISHOLD(p) || (t->t_proc_flag & TP_EXITLWP))
6447c478bd9Sstevel@tonic-gate 			holdlwp();
6457c478bd9Sstevel@tonic-gate 
6467c478bd9Sstevel@tonic-gate 		/*
6477c478bd9Sstevel@tonic-gate 		 * All code that sets signals and makes ISSIG_PENDING
6487c478bd9Sstevel@tonic-gate 		 * evaluate true must set t_sig_check afterwards.
6497c478bd9Sstevel@tonic-gate 		 */
6507c478bd9Sstevel@tonic-gate 		if (ISSIG_PENDING(t, lwp, p)) {
6517c478bd9Sstevel@tonic-gate 			if (issig(FORREAL))
6527c478bd9Sstevel@tonic-gate 				psig();
6537c478bd9Sstevel@tonic-gate 			t->t_sig_check = 1;	/* recheck next time */
6547c478bd9Sstevel@tonic-gate 		}
6557c478bd9Sstevel@tonic-gate 
6567c478bd9Sstevel@tonic-gate 		if (sigprof) {
657e0cf54a5SRoger A. Faulkner 			int nargs = (code > 0 && code < NSYSCALL)?
658e0cf54a5SRoger A. Faulkner 			    LWP_GETSYSENT(lwp)[code].sy_narg : 0;
659e0cf54a5SRoger A. Faulkner 			realsigprof(code, nargs, error);
6607c478bd9Sstevel@tonic-gate 			t->t_sig_check = 1;	/* recheck next time */
6617c478bd9Sstevel@tonic-gate 		}
6627c478bd9Sstevel@tonic-gate 
6637c478bd9Sstevel@tonic-gate 		/*
6647c478bd9Sstevel@tonic-gate 		 * If a performance counter overflow interrupt was
6657c478bd9Sstevel@tonic-gate 		 * delivered *during* the syscall, then re-enable the
6667c478bd9Sstevel@tonic-gate 		 * AST so that we take a trip through trap() to cause
6677c478bd9Sstevel@tonic-gate 		 * the SIGEMT to be delivered.
6687c478bd9Sstevel@tonic-gate 		 */
6697c478bd9Sstevel@tonic-gate 		if (lwp->lwp_pcb.pcb_flags & CPC_OVERFLOW)
6707c478bd9Sstevel@tonic-gate 			aston(t);
67165a89a64Smarx 
67265a89a64Smarx 		/*
67365a89a64Smarx 		 * /proc can't enable/disable the trace bit itself
67465a89a64Smarx 		 * because that could race with the call gate used by
67565a89a64Smarx 		 * system calls via "lcall". If that happened, an
67665a89a64Smarx 		 * invalid EFLAGS would result. prstep()/prnostep()
67765a89a64Smarx 		 * therefore schedule an AST for the purpose.
67865a89a64Smarx 		 */
67965a89a64Smarx 		if (lwp->lwp_pcb.pcb_flags & REQUEST_STEP) {
68065a89a64Smarx 			lwp->lwp_pcb.pcb_flags &= ~REQUEST_STEP;
68165a89a64Smarx 			rp->r_ps |= PS_T;
68265a89a64Smarx 		}
68365a89a64Smarx 		if (lwp->lwp_pcb.pcb_flags & REQUEST_NOSTEP) {
68465a89a64Smarx 			lwp->lwp_pcb.pcb_flags &= ~REQUEST_NOSTEP;
68565a89a64Smarx 			rp->r_ps &= ~PS_T;
68665a89a64Smarx 		}
6877c478bd9Sstevel@tonic-gate 	}
6887c478bd9Sstevel@tonic-gate 
6897c478bd9Sstevel@tonic-gate 	lwp->lwp_errno = 0;		/* clear error for next time */
6907c478bd9Sstevel@tonic-gate 
6917c478bd9Sstevel@tonic-gate 	/*
6927c478bd9Sstevel@tonic-gate 	 * Set state to LWP_USER here so preempt won't give us a kernel
6937c478bd9Sstevel@tonic-gate 	 * priority if it occurs after this point.  Call CL_TRAPRET() to
6947c478bd9Sstevel@tonic-gate 	 * restore the user-level priority.
6957c478bd9Sstevel@tonic-gate 	 *
6967c478bd9Sstevel@tonic-gate 	 * It is important that no locks (other than spinlocks) be entered
6977c478bd9Sstevel@tonic-gate 	 * after this point before returning to user mode (unless lwp_state
6987c478bd9Sstevel@tonic-gate 	 * is set back to LWP_SYS).
6997c478bd9Sstevel@tonic-gate 	 *
7007c478bd9Sstevel@tonic-gate 	 * XXX Sampled times past this point are charged to the user.
7017c478bd9Sstevel@tonic-gate 	 */
7027c478bd9Sstevel@tonic-gate 	lwp->lwp_state = LWP_USER;
7037c478bd9Sstevel@tonic-gate 
7047c478bd9Sstevel@tonic-gate 	if (t->t_trapret) {
7057c478bd9Sstevel@tonic-gate 		t->t_trapret = 0;
7067c478bd9Sstevel@tonic-gate 		thread_lock(t);
7077c478bd9Sstevel@tonic-gate 		CL_TRAPRET(t);
7087c478bd9Sstevel@tonic-gate 		thread_unlock(t);
7097c478bd9Sstevel@tonic-gate 	}
710c97ad5cdSakolb 	if (CPU->cpu_runrun || t->t_schedflag & TS_ANYWAITQ)
7117c478bd9Sstevel@tonic-gate 		preempt();
71207a48826SRoger A. Faulkner 	prunstop();
7137c478bd9Sstevel@tonic-gate 
7147c478bd9Sstevel@tonic-gate 	lwp->lwp_errno = 0;		/* clear error for next time */
7157c478bd9Sstevel@tonic-gate 
7167c478bd9Sstevel@tonic-gate 	/*
7177c478bd9Sstevel@tonic-gate 	 * The thread lock must be held in order to clear sysnum and reset
7187c478bd9Sstevel@tonic-gate 	 * lwp_ap atomically with respect to other threads in the system that
7197c478bd9Sstevel@tonic-gate 	 * may be looking at the args via lwp_ap from get_syscall_args().
7207c478bd9Sstevel@tonic-gate 	 */
7217c478bd9Sstevel@tonic-gate 
7227c478bd9Sstevel@tonic-gate 	thread_lock(t);
7237c478bd9Sstevel@tonic-gate 	t->t_sysnum = 0;		/* no longer in a system call */
7247c478bd9Sstevel@tonic-gate 
7257c478bd9Sstevel@tonic-gate 	if (lwp_getdatamodel(lwp) == DATAMODEL_NATIVE) {
7267c478bd9Sstevel@tonic-gate #if defined(_LP64)
7277c478bd9Sstevel@tonic-gate 		/*
7287c478bd9Sstevel@tonic-gate 		 * In case the args were copied to the lwp, reset the
7297c478bd9Sstevel@tonic-gate 		 * pointer so the next syscall will have the right
7307c478bd9Sstevel@tonic-gate 		 * lwp_ap pointer.
7317c478bd9Sstevel@tonic-gate 		 */
7327c478bd9Sstevel@tonic-gate 		lwp->lwp_ap = (long *)&rp->r_rdi;
7337c478bd9Sstevel@tonic-gate 	} else {
7347c478bd9Sstevel@tonic-gate #endif
7357c478bd9Sstevel@tonic-gate 		lwp->lwp_ap = NULL;	/* reset on every syscall entry */
7367c478bd9Sstevel@tonic-gate 	}
7377c478bd9Sstevel@tonic-gate 	thread_unlock(t);
7387c478bd9Sstevel@tonic-gate 
7397c478bd9Sstevel@tonic-gate 	lwp->lwp_argsaved = 0;
7407c478bd9Sstevel@tonic-gate 
7417c478bd9Sstevel@tonic-gate 	/*
7427c478bd9Sstevel@tonic-gate 	 * If there was a continuing reason for post-syscall processing,
7437c478bd9Sstevel@tonic-gate 	 * set the t_post_sys flag for the next system call.
7447c478bd9Sstevel@tonic-gate 	 */
7457c478bd9Sstevel@tonic-gate 	if (repost)
7467c478bd9Sstevel@tonic-gate 		t->t_post_sys = 1;
7477c478bd9Sstevel@tonic-gate 
7487c478bd9Sstevel@tonic-gate 	/*
7497c478bd9Sstevel@tonic-gate 	 * If there is a ustack registered for this lwp, and the stack rlimit
7507c478bd9Sstevel@tonic-gate 	 * has been altered, read in the ustack. If the saved stack rlimit
7517c478bd9Sstevel@tonic-gate 	 * matches the bounds of the ustack, update the ustack to reflect
7527c478bd9Sstevel@tonic-gate 	 * the new rlimit. If the new stack rlimit is RLIM_INFINITY, disable
7537c478bd9Sstevel@tonic-gate 	 * stack checking by setting the size to 0.
7547c478bd9Sstevel@tonic-gate 	 */
7557c478bd9Sstevel@tonic-gate 	if (lwp->lwp_ustack != 0 && lwp->lwp_old_stk_ctl != 0) {
7567c478bd9Sstevel@tonic-gate 		rlim64_t new_size;
7577c478bd9Sstevel@tonic-gate 		caddr_t top;
7587c478bd9Sstevel@tonic-gate 		stack_t stk;
7597c478bd9Sstevel@tonic-gate 		struct rlimit64 rl;
7607c478bd9Sstevel@tonic-gate 
7617c478bd9Sstevel@tonic-gate 		mutex_enter(&p->p_lock);
7627c478bd9Sstevel@tonic-gate 		new_size = p->p_stk_ctl;
7637c478bd9Sstevel@tonic-gate 		top = p->p_usrstack;
7647c478bd9Sstevel@tonic-gate 		(void) rctl_rlimit_get(rctlproc_legacy[RLIMIT_STACK], p, &rl);
7657c478bd9Sstevel@tonic-gate 		mutex_exit(&p->p_lock);
7667c478bd9Sstevel@tonic-gate 
7677c478bd9Sstevel@tonic-gate 		if (rl.rlim_cur == RLIM64_INFINITY)
7687c478bd9Sstevel@tonic-gate 			new_size = 0;
7697c478bd9Sstevel@tonic-gate 
7707c478bd9Sstevel@tonic-gate 		if (copyin((stack_t *)lwp->lwp_ustack, &stk,
7717c478bd9Sstevel@tonic-gate 		    sizeof (stack_t)) == 0 &&
7727c478bd9Sstevel@tonic-gate 		    (stk.ss_size == lwp->lwp_old_stk_ctl ||
773d3e55dcdSgww 		    stk.ss_size == 0) &&
7747c478bd9Sstevel@tonic-gate 		    stk.ss_sp == top - stk.ss_size) {
7757c478bd9Sstevel@tonic-gate 			stk.ss_sp = (void *)((uintptr_t)stk.ss_sp +
7767c478bd9Sstevel@tonic-gate 			    stk.ss_size - (uintptr_t)new_size);
7777c478bd9Sstevel@tonic-gate 			stk.ss_size = new_size;
7787c478bd9Sstevel@tonic-gate 
7797c478bd9Sstevel@tonic-gate 			(void) copyout(&stk, (stack_t *)lwp->lwp_ustack,
7807c478bd9Sstevel@tonic-gate 			    sizeof (stack_t));
7817c478bd9Sstevel@tonic-gate 		}
7827c478bd9Sstevel@tonic-gate 
7837c478bd9Sstevel@tonic-gate 		lwp->lwp_old_stk_ctl = 0;
7847c478bd9Sstevel@tonic-gate 	}
7857c478bd9Sstevel@tonic-gate }
7867c478bd9Sstevel@tonic-gate 
7877c478bd9Sstevel@tonic-gate /*
7887c478bd9Sstevel@tonic-gate  * Called from post_syscall() when a deferred singlestep is to be taken.
7897c478bd9Sstevel@tonic-gate  */
790d8aa0f5aSsudheer void
deferred_singlestep_trap(caddr_t pc)7917c478bd9Sstevel@tonic-gate deferred_singlestep_trap(caddr_t pc)
7927c478bd9Sstevel@tonic-gate {
7937c478bd9Sstevel@tonic-gate 	proc_t *p = ttoproc(curthread);
7947c478bd9Sstevel@tonic-gate 	klwp_t *lwp = ttolwp(curthread);
7957c478bd9Sstevel@tonic-gate 	pcb_t *pcb = &lwp->lwp_pcb;
7967c478bd9Sstevel@tonic-gate 	uint_t fault = 0;
7977c478bd9Sstevel@tonic-gate 	k_siginfo_t siginfo;
7987c478bd9Sstevel@tonic-gate 
7997c478bd9Sstevel@tonic-gate 	bzero(&siginfo, sizeof (siginfo));
8007c478bd9Sstevel@tonic-gate 
8017c478bd9Sstevel@tonic-gate 	/*
8027c478bd9Sstevel@tonic-gate 	 * If both NORMAL_STEP and WATCH_STEP are in
8039acbbeafSnn 	 * effect, give precedence to WATCH_STEP.
8047c478bd9Sstevel@tonic-gate 	 * If neither is set, user must have set the
8057c478bd9Sstevel@tonic-gate 	 * PS_T bit in %efl; treat this as NORMAL_STEP.
8067c478bd9Sstevel@tonic-gate 	 */
8079acbbeafSnn 	if ((fault = undo_watch_step(&siginfo)) == 0 &&
8089acbbeafSnn 	    ((pcb->pcb_flags & NORMAL_STEP) ||
8099acbbeafSnn 	    !(pcb->pcb_flags & WATCH_STEP))) {
8107c478bd9Sstevel@tonic-gate 		siginfo.si_signo = SIGTRAP;
8117c478bd9Sstevel@tonic-gate 		siginfo.si_code = TRAP_TRACE;
8127c478bd9Sstevel@tonic-gate 		siginfo.si_addr  = pc;
8137c478bd9Sstevel@tonic-gate 		fault = FLTTRACE;
8147c478bd9Sstevel@tonic-gate 	}
8157c478bd9Sstevel@tonic-gate 	pcb->pcb_flags &= ~(DEBUG_PENDING|NORMAL_STEP|WATCH_STEP);
8167c478bd9Sstevel@tonic-gate 
8177c478bd9Sstevel@tonic-gate 	if (fault) {
8187c478bd9Sstevel@tonic-gate 		/*
8197c478bd9Sstevel@tonic-gate 		 * Remember the fault and fault adddress
8207c478bd9Sstevel@tonic-gate 		 * for real-time (SIGPROF) profiling.
8217c478bd9Sstevel@tonic-gate 		 */
8227c478bd9Sstevel@tonic-gate 		lwp->lwp_lastfault = fault;
8237c478bd9Sstevel@tonic-gate 		lwp->lwp_lastfaddr = siginfo.si_addr;
8247c478bd9Sstevel@tonic-gate 		/*
8257c478bd9Sstevel@tonic-gate 		 * If a debugger has declared this fault to be an
8267c478bd9Sstevel@tonic-gate 		 * event of interest, stop the lwp.  Otherwise just
8277c478bd9Sstevel@tonic-gate 		 * deliver the associated signal.
8287c478bd9Sstevel@tonic-gate 		 */
8297c478bd9Sstevel@tonic-gate 		if (prismember(&p->p_fltmask, fault) &&
8307c478bd9Sstevel@tonic-gate 		    stop_on_fault(fault, &siginfo) == 0)
8317c478bd9Sstevel@tonic-gate 			siginfo.si_signo = 0;
8327c478bd9Sstevel@tonic-gate 	}
8337c478bd9Sstevel@tonic-gate 
8347c478bd9Sstevel@tonic-gate 	if (siginfo.si_signo)
8357c478bd9Sstevel@tonic-gate 		trapsig(&siginfo, 1);
8367c478bd9Sstevel@tonic-gate }
8377c478bd9Sstevel@tonic-gate 
8387c478bd9Sstevel@tonic-gate /*
8397c478bd9Sstevel@tonic-gate  * nonexistent system call-- signal lwp (may want to handle it)
8407c478bd9Sstevel@tonic-gate  * flag error if lwp won't see signal immediately
8417c478bd9Sstevel@tonic-gate  */
8427c478bd9Sstevel@tonic-gate int64_t
nosys(void)843954fa1e9SToomas Soome nosys(void)
8447c478bd9Sstevel@tonic-gate {
8457c478bd9Sstevel@tonic-gate 	tsignal(curthread, SIGSYS);
8467c478bd9Sstevel@tonic-gate 	return (set_errno(ENOSYS));
8477c478bd9Sstevel@tonic-gate }
8487c478bd9Sstevel@tonic-gate 
849954fa1e9SToomas Soome int
nosys32(void)850954fa1e9SToomas Soome nosys32(void)
851954fa1e9SToomas Soome {
852954fa1e9SToomas Soome 	return (nosys());
853954fa1e9SToomas Soome }
854954fa1e9SToomas Soome 
8557c478bd9Sstevel@tonic-gate /*
8567c478bd9Sstevel@tonic-gate  * Execute a 32-bit system call on behalf of the current thread.
8577c478bd9Sstevel@tonic-gate  */
8587c478bd9Sstevel@tonic-gate void
dosyscall(void)8597c478bd9Sstevel@tonic-gate dosyscall(void)
8607c478bd9Sstevel@tonic-gate {
8617c478bd9Sstevel@tonic-gate 	/*
8627c478bd9Sstevel@tonic-gate 	 * Need space on the stack to store syscall arguments.
8637c478bd9Sstevel@tonic-gate 	 */
8647c478bd9Sstevel@tonic-gate 	long		syscall_args[MAXSYSARGS];
8657c478bd9Sstevel@tonic-gate 	struct sysent	*se;
8667c478bd9Sstevel@tonic-gate 	int64_t		ret;
8677c478bd9Sstevel@tonic-gate 
8687c478bd9Sstevel@tonic-gate 	syscall_mstate(LMS_TRAP, LMS_SYSTEM);
8697c478bd9Sstevel@tonic-gate 
8707c478bd9Sstevel@tonic-gate 	ASSERT(curproc->p_model == DATAMODEL_ILP32);
8717c478bd9Sstevel@tonic-gate 
8727c478bd9Sstevel@tonic-gate 	CPU_STATS_ENTER_K();
8737c478bd9Sstevel@tonic-gate 	CPU_STATS_ADDQ(CPU, sys, syscall, 1);
8747c478bd9Sstevel@tonic-gate 	CPU_STATS_EXIT_K();
8757c478bd9Sstevel@tonic-gate 
8767c478bd9Sstevel@tonic-gate 	se = syscall_entry(curthread, syscall_args);
8777c478bd9Sstevel@tonic-gate 
8787c478bd9Sstevel@tonic-gate 	/*
8797c478bd9Sstevel@tonic-gate 	 * syscall_entry() copied all 8 arguments into syscall_args.
8807c478bd9Sstevel@tonic-gate 	 */
8817c478bd9Sstevel@tonic-gate 	ret = se->sy_callc(syscall_args[0], syscall_args[1], syscall_args[2],
8827c478bd9Sstevel@tonic-gate 	    syscall_args[3], syscall_args[4], syscall_args[5], syscall_args[6],
8837c478bd9Sstevel@tonic-gate 	    syscall_args[7]);
8847c478bd9Sstevel@tonic-gate 
8857c478bd9Sstevel@tonic-gate 	syscall_exit(curthread, (int)ret & 0xffffffffu, (int)(ret >> 32));
8867c478bd9Sstevel@tonic-gate 	syscall_mstate(LMS_SYSTEM, LMS_TRAP);
8877c478bd9Sstevel@tonic-gate }
8887c478bd9Sstevel@tonic-gate 
8897c478bd9Sstevel@tonic-gate /*
8907c478bd9Sstevel@tonic-gate  * Get the arguments to the current system call. See comment atop
8917c478bd9Sstevel@tonic-gate  * save_syscall_args() regarding lwp_ap usage.
8927c478bd9Sstevel@tonic-gate  */
8937c478bd9Sstevel@tonic-gate 
8947c478bd9Sstevel@tonic-gate uint_t
get_syscall_args(klwp_t * lwp,long * argp,int * nargsp)8957c478bd9Sstevel@tonic-gate get_syscall_args(klwp_t *lwp, long *argp, int *nargsp)
8967c478bd9Sstevel@tonic-gate {
8977c478bd9Sstevel@tonic-gate 	kthread_t	*t = lwptot(lwp);
8987c478bd9Sstevel@tonic-gate 	ulong_t	mask = 0xfffffffful;
8997c478bd9Sstevel@tonic-gate 	uint_t	code;
9007c478bd9Sstevel@tonic-gate 	long	*ap;
9017c478bd9Sstevel@tonic-gate 	int	nargs;
9027c478bd9Sstevel@tonic-gate 
9037c478bd9Sstevel@tonic-gate #if defined(_LP64)
9047c478bd9Sstevel@tonic-gate 	if (lwp_getdatamodel(lwp) == DATAMODEL_LP64)
9057c478bd9Sstevel@tonic-gate 		mask = 0xfffffffffffffffful;
9067c478bd9Sstevel@tonic-gate #endif
9077c478bd9Sstevel@tonic-gate 
9087c478bd9Sstevel@tonic-gate 	/*
9097c478bd9Sstevel@tonic-gate 	 * The thread lock must be held while looking at the arguments to ensure
9107c478bd9Sstevel@tonic-gate 	 * they don't go away via post_syscall().
9117c478bd9Sstevel@tonic-gate 	 * get_syscall_args() is the only routine to read them which is callable
9127c478bd9Sstevel@tonic-gate 	 * outside the LWP in question and hence the only one that must be
9137c478bd9Sstevel@tonic-gate 	 * synchronized in this manner.
9147c478bd9Sstevel@tonic-gate 	 */
9157c478bd9Sstevel@tonic-gate 	thread_lock(t);
9167c478bd9Sstevel@tonic-gate 
9177c478bd9Sstevel@tonic-gate 	code = t->t_sysnum;
9187c478bd9Sstevel@tonic-gate 	ap = lwp->lwp_ap;
9197c478bd9Sstevel@tonic-gate 
9207c478bd9Sstevel@tonic-gate 	thread_unlock(t);
9217c478bd9Sstevel@tonic-gate 
9227c478bd9Sstevel@tonic-gate 	if (code != 0 && code < NSYSCALL) {
9237c478bd9Sstevel@tonic-gate 		nargs = LWP_GETSYSENT(lwp)[code].sy_narg;
9247c478bd9Sstevel@tonic-gate 
9257c478bd9Sstevel@tonic-gate 		ASSERT(nargs <= MAXSYSARGS);
9267c478bd9Sstevel@tonic-gate 
9277c478bd9Sstevel@tonic-gate 		*nargsp = nargs;
9287c478bd9Sstevel@tonic-gate 		while (nargs-- > 0)
9297c478bd9Sstevel@tonic-gate 			*argp++ = *ap++ & mask;
9307c478bd9Sstevel@tonic-gate 	} else {
9317c478bd9Sstevel@tonic-gate 		*nargsp = 0;
9327c478bd9Sstevel@tonic-gate 	}
9337c478bd9Sstevel@tonic-gate 
9347c478bd9Sstevel@tonic-gate 	return (code);
9357c478bd9Sstevel@tonic-gate }
9367c478bd9Sstevel@tonic-gate 
9377c478bd9Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
9387c478bd9Sstevel@tonic-gate /*
9397c478bd9Sstevel@tonic-gate  * Get the arguments to the current 32-bit system call.
9407c478bd9Sstevel@tonic-gate  */
9417c478bd9Sstevel@tonic-gate uint_t
get_syscall32_args(klwp_t * lwp,int * argp,int * nargsp)9427c478bd9Sstevel@tonic-gate get_syscall32_args(klwp_t *lwp, int *argp, int *nargsp)
9437c478bd9Sstevel@tonic-gate {
9447c478bd9Sstevel@tonic-gate 	long args[MAXSYSARGS];
9457c478bd9Sstevel@tonic-gate 	uint_t i, code;
9467c478bd9Sstevel@tonic-gate 
9477c478bd9Sstevel@tonic-gate 	code = get_syscall_args(lwp, args, nargsp);
9487c478bd9Sstevel@tonic-gate 
9497c478bd9Sstevel@tonic-gate 	for (i = 0; i != *nargsp; i++)
9507c478bd9Sstevel@tonic-gate 		*argp++ = (int)args[i];
9517c478bd9Sstevel@tonic-gate 	return (code);
9527c478bd9Sstevel@tonic-gate }
9537c478bd9Sstevel@tonic-gate #endif
9547c478bd9Sstevel@tonic-gate 
9557c478bd9Sstevel@tonic-gate /*
9567c478bd9Sstevel@tonic-gate  * Save the system call arguments in a safe place.
9577c478bd9Sstevel@tonic-gate  *
9587c478bd9Sstevel@tonic-gate  * On the i386 kernel:
9597c478bd9Sstevel@tonic-gate  *
9607c478bd9Sstevel@tonic-gate  *	Copy the users args prior to changing the stack or stack pointer.
9617c478bd9Sstevel@tonic-gate  *	This is so /proc will be able to get a valid copy of the
9627c478bd9Sstevel@tonic-gate  *	args from the user stack even after the user stack has been changed.
9637c478bd9Sstevel@tonic-gate  *	Note that the kernel stack copy of the args may also have been
9647c478bd9Sstevel@tonic-gate  *	changed by a system call handler which takes C-style arguments.
9657c478bd9Sstevel@tonic-gate  *
9667c478bd9Sstevel@tonic-gate  *	Note that this may be called by stop() from trap().  In that case
9677c478bd9Sstevel@tonic-gate  *	t_sysnum will be zero (syscall_exit clears it), so no args will be
9687c478bd9Sstevel@tonic-gate  *	copied.
9697c478bd9Sstevel@tonic-gate  *
9707c478bd9Sstevel@tonic-gate  * On the amd64 kernel:
9717c478bd9Sstevel@tonic-gate  *
9727c478bd9Sstevel@tonic-gate  *	For 64-bit applications, lwp->lwp_ap normally points to %rdi..%r9
9737c478bd9Sstevel@tonic-gate  *	in the reg structure. If the user is going to change the argument
9747c478bd9Sstevel@tonic-gate  *	registers, rax, or the stack and might want to get the args (for
9757c478bd9Sstevel@tonic-gate  *	/proc tracing), it must copy the args elsewhere via save_syscall_args().
9767c478bd9Sstevel@tonic-gate  *
9777c478bd9Sstevel@tonic-gate  *	For 32-bit applications, lwp->lwp_ap normally points to a copy of
9787c478bd9Sstevel@tonic-gate  *	the system call arguments on the kernel stack made from the user
9797c478bd9Sstevel@tonic-gate  *	stack.  Copy the args prior to change the stack or stack pointer.
9807c478bd9Sstevel@tonic-gate  *	This is so /proc will be able to get a valid copy of the args
9817c478bd9Sstevel@tonic-gate  *	from the user stack even after that stack has been changed.
9827c478bd9Sstevel@tonic-gate  *
9837c478bd9Sstevel@tonic-gate  *	This may be called from stop() even when we're not in a system call.
9847c478bd9Sstevel@tonic-gate  *	Since there's no easy way to tell, this must be safe (not panic).
9857c478bd9Sstevel@tonic-gate  *	If the copyins get data faults, return non-zero.
9867c478bd9Sstevel@tonic-gate  */
9877c478bd9Sstevel@tonic-gate int
save_syscall_args()9887c478bd9Sstevel@tonic-gate save_syscall_args()
9897c478bd9Sstevel@tonic-gate {
9907c478bd9Sstevel@tonic-gate 	kthread_t	*t = curthread;
9917c478bd9Sstevel@tonic-gate 	klwp_t		*lwp = ttolwp(t);
9927c478bd9Sstevel@tonic-gate 	uint_t		code = t->t_sysnum;
9937c478bd9Sstevel@tonic-gate 	uint_t		nargs;
9947c478bd9Sstevel@tonic-gate 
9957c478bd9Sstevel@tonic-gate 	if (lwp->lwp_argsaved || code == 0)
9967c478bd9Sstevel@tonic-gate 		return (0);		/* args already saved or not needed */
9977c478bd9Sstevel@tonic-gate 
9987c478bd9Sstevel@tonic-gate 	if (code >= NSYSCALL) {
9997c478bd9Sstevel@tonic-gate 		nargs = 0;		/* illegal syscall */
10007c478bd9Sstevel@tonic-gate 	} else {
10017c478bd9Sstevel@tonic-gate 		struct sysent *se = LWP_GETSYSENT(lwp);
10027c478bd9Sstevel@tonic-gate 		struct sysent *callp = se + code;
10037c478bd9Sstevel@tonic-gate 
10047c478bd9Sstevel@tonic-gate 		nargs = callp->sy_narg;
10057c478bd9Sstevel@tonic-gate 		if (LOADABLE_SYSCALL(callp) && nargs == 0) {
10067c478bd9Sstevel@tonic-gate 			krwlock_t	*module_lock;
10077c478bd9Sstevel@tonic-gate 
10087c478bd9Sstevel@tonic-gate 			/*
10097c478bd9Sstevel@tonic-gate 			 * Find out how many arguments the system
10107c478bd9Sstevel@tonic-gate 			 * call uses.
10117c478bd9Sstevel@tonic-gate 			 *
10127c478bd9Sstevel@tonic-gate 			 * We have the property that loaded syscalls
10137c478bd9Sstevel@tonic-gate 			 * never change the number of arguments they
10147c478bd9Sstevel@tonic-gate 			 * use after they've been loaded once.  This
10157c478bd9Sstevel@tonic-gate 			 * allows us to stop for /proc tracing without
10167c478bd9Sstevel@tonic-gate 			 * holding the module lock.
10177c478bd9Sstevel@tonic-gate 			 * /proc is assured that sy_narg is valid.
10187c478bd9Sstevel@tonic-gate 			 */
10197c478bd9Sstevel@tonic-gate 			module_lock = lock_syscall(se, code);
10207c478bd9Sstevel@tonic-gate 			nargs = callp->sy_narg;
10217c478bd9Sstevel@tonic-gate 			rw_exit(module_lock);
10227c478bd9Sstevel@tonic-gate 		}
10237c478bd9Sstevel@tonic-gate 	}
10247c478bd9Sstevel@tonic-gate 
10257c478bd9Sstevel@tonic-gate 	/*
10267c478bd9Sstevel@tonic-gate 	 * Fetch the system call arguments.
10277c478bd9Sstevel@tonic-gate 	 */
10287c478bd9Sstevel@tonic-gate 	if (nargs == 0)
10297c478bd9Sstevel@tonic-gate 		goto out;
10307c478bd9Sstevel@tonic-gate 
10317c478bd9Sstevel@tonic-gate 	ASSERT(nargs <= MAXSYSARGS);
10327c478bd9Sstevel@tonic-gate 
10337c478bd9Sstevel@tonic-gate 	if (lwp_getdatamodel(lwp) == DATAMODEL_NATIVE) {
10347c478bd9Sstevel@tonic-gate #if defined(_LP64)
10357c478bd9Sstevel@tonic-gate 		struct regs *rp = lwptoregs(lwp);
10367c478bd9Sstevel@tonic-gate 
10377c478bd9Sstevel@tonic-gate 		lwp->lwp_arg[0] = rp->r_rdi;
10387c478bd9Sstevel@tonic-gate 		lwp->lwp_arg[1] = rp->r_rsi;
10397c478bd9Sstevel@tonic-gate 		lwp->lwp_arg[2] = rp->r_rdx;
10407c478bd9Sstevel@tonic-gate 		lwp->lwp_arg[3] = rp->r_rcx;
10417c478bd9Sstevel@tonic-gate 		lwp->lwp_arg[4] = rp->r_r8;
10427c478bd9Sstevel@tonic-gate 		lwp->lwp_arg[5] = rp->r_r9;
10437c478bd9Sstevel@tonic-gate 		if (nargs > 6 && copyin_args(rp, &lwp->lwp_arg[6], nargs - 6))
10447c478bd9Sstevel@tonic-gate 			return (-1);
10457c478bd9Sstevel@tonic-gate 	} else {
10467c478bd9Sstevel@tonic-gate #endif
10477c478bd9Sstevel@tonic-gate 		if (COPYIN_ARGS32(lwptoregs(lwp), lwp->lwp_arg, nargs))
10487c478bd9Sstevel@tonic-gate 			return (-1);
10497c478bd9Sstevel@tonic-gate 	}
10507c478bd9Sstevel@tonic-gate out:
10517c478bd9Sstevel@tonic-gate 	lwp->lwp_ap = lwp->lwp_arg;
10527c478bd9Sstevel@tonic-gate 	lwp->lwp_argsaved = 1;
10537c478bd9Sstevel@tonic-gate 	t->t_post_sys = 1;	/* so lwp_ap will be reset */
10547c478bd9Sstevel@tonic-gate 	return (0);
10557c478bd9Sstevel@tonic-gate }
10567c478bd9Sstevel@tonic-gate 
10577c478bd9Sstevel@tonic-gate void
reset_syscall_args(void)10587c478bd9Sstevel@tonic-gate reset_syscall_args(void)
10597c478bd9Sstevel@tonic-gate {
10607c478bd9Sstevel@tonic-gate 	ttolwp(curthread)->lwp_argsaved = 0;
10617c478bd9Sstevel@tonic-gate }
10627c478bd9Sstevel@tonic-gate 
10637c478bd9Sstevel@tonic-gate /*
10647c478bd9Sstevel@tonic-gate  * Call a system call which takes a pointer to the user args struct and
10657c478bd9Sstevel@tonic-gate  * a pointer to the return values.  This is a bit slower than the standard
10667c478bd9Sstevel@tonic-gate  * C arg-passing method in some cases.
10677c478bd9Sstevel@tonic-gate  */
10687c478bd9Sstevel@tonic-gate int64_t
syscall_ap(void)10697c478bd9Sstevel@tonic-gate syscall_ap(void)
10707c478bd9Sstevel@tonic-gate {
10717c478bd9Sstevel@tonic-gate 	uint_t	error;
10727c478bd9Sstevel@tonic-gate 	struct sysent *callp;
10737c478bd9Sstevel@tonic-gate 	rval_t	rval;
10747c478bd9Sstevel@tonic-gate 	kthread_t *t = curthread;
10757c478bd9Sstevel@tonic-gate 	klwp_t	*lwp = ttolwp(t);
10767c478bd9Sstevel@tonic-gate 	struct regs *rp = lwptoregs(lwp);
10777c478bd9Sstevel@tonic-gate 
10787c478bd9Sstevel@tonic-gate 	callp = LWP_GETSYSENT(lwp) + t->t_sysnum;
10797c478bd9Sstevel@tonic-gate 
10807c478bd9Sstevel@tonic-gate 	/*
10817c478bd9Sstevel@tonic-gate 	 * If the arguments don't fit in registers %rdi-%r9, make sure they
10827c478bd9Sstevel@tonic-gate 	 * have been copied to the lwp_arg array.
10837c478bd9Sstevel@tonic-gate 	 */
10847c478bd9Sstevel@tonic-gate 	if (callp->sy_narg > 6 && save_syscall_args())
10857c478bd9Sstevel@tonic-gate 		return ((int64_t)set_errno(EFAULT));
10867c478bd9Sstevel@tonic-gate 
10877c478bd9Sstevel@tonic-gate 	rval.r_val1 = 0;
10887c478bd9Sstevel@tonic-gate 	rval.r_val2 = rp->r_r1;
10897c478bd9Sstevel@tonic-gate 	lwp->lwp_error = 0;	/* for old drivers */
10907c478bd9Sstevel@tonic-gate 	error = (*(callp->sy_call))(lwp->lwp_ap, &rval);
10917c478bd9Sstevel@tonic-gate 	if (error)
10927c478bd9Sstevel@tonic-gate 		return ((longlong_t)set_errno(error));
10937c478bd9Sstevel@tonic-gate 	return (rval.r_vals);
10947c478bd9Sstevel@tonic-gate }
10957c478bd9Sstevel@tonic-gate 
10967c478bd9Sstevel@tonic-gate /*
10977c478bd9Sstevel@tonic-gate  * Load system call module.
10987c478bd9Sstevel@tonic-gate  *	Returns with pointer to held read lock for module.
10997c478bd9Sstevel@tonic-gate  */
11007c478bd9Sstevel@tonic-gate static krwlock_t *
lock_syscall(struct sysent * table,uint_t code)11017c478bd9Sstevel@tonic-gate lock_syscall(struct sysent *table, uint_t code)
11027c478bd9Sstevel@tonic-gate {
11037c478bd9Sstevel@tonic-gate 	krwlock_t	*module_lock;
11047c478bd9Sstevel@tonic-gate 	struct modctl	*modp;
11057c478bd9Sstevel@tonic-gate 	int		id;
11067c478bd9Sstevel@tonic-gate 	struct sysent   *callp;
11077c478bd9Sstevel@tonic-gate 
11087c478bd9Sstevel@tonic-gate 	callp = table + code;
11097c478bd9Sstevel@tonic-gate 	module_lock = callp->sy_lock;
11107c478bd9Sstevel@tonic-gate 
11117c478bd9Sstevel@tonic-gate 	/*
11127c478bd9Sstevel@tonic-gate 	 * Optimization to only call modload if we don't have a loaded
11137c478bd9Sstevel@tonic-gate 	 * syscall.
11147c478bd9Sstevel@tonic-gate 	 */
11157c478bd9Sstevel@tonic-gate 	rw_enter(module_lock, RW_READER);
11167c478bd9Sstevel@tonic-gate 	if (LOADED_SYSCALL(callp))
11177c478bd9Sstevel@tonic-gate 		return (module_lock);
11187c478bd9Sstevel@tonic-gate 	rw_exit(module_lock);
11197c478bd9Sstevel@tonic-gate 
11207c478bd9Sstevel@tonic-gate 	for (;;) {
11217c478bd9Sstevel@tonic-gate 		if ((id = modload("sys", syscallnames[code])) == -1)
11227c478bd9Sstevel@tonic-gate 			break;
11237c478bd9Sstevel@tonic-gate 
11247c478bd9Sstevel@tonic-gate 		/*
11257c478bd9Sstevel@tonic-gate 		 * If we loaded successfully at least once, the modctl
11267c478bd9Sstevel@tonic-gate 		 * will still be valid, so we try to grab it by filename.
11277c478bd9Sstevel@tonic-gate 		 * If this call fails, it's because the mod_filename
11287c478bd9Sstevel@tonic-gate 		 * was changed after the call to modload() (mod_hold_by_name()
11297c478bd9Sstevel@tonic-gate 		 * is the likely culprit).  We can safely just take
11307c478bd9Sstevel@tonic-gate 		 * another lap if this is the case;  the modload() will
11317c478bd9Sstevel@tonic-gate 		 * change the mod_filename back to one by which we can
11327c478bd9Sstevel@tonic-gate 		 * find the modctl.
11337c478bd9Sstevel@tonic-gate 		 */
11347c478bd9Sstevel@tonic-gate 		modp = mod_find_by_filename("sys", syscallnames[code]);
11357c478bd9Sstevel@tonic-gate 
11367c478bd9Sstevel@tonic-gate 		if (modp == NULL)
11377c478bd9Sstevel@tonic-gate 			continue;
11387c478bd9Sstevel@tonic-gate 
11397c478bd9Sstevel@tonic-gate 		mutex_enter(&mod_lock);
11407c478bd9Sstevel@tonic-gate 
11417c478bd9Sstevel@tonic-gate 		if (!modp->mod_installed) {
11427c478bd9Sstevel@tonic-gate 			mutex_exit(&mod_lock);
11437c478bd9Sstevel@tonic-gate 			continue;
11447c478bd9Sstevel@tonic-gate 		}
11457c478bd9Sstevel@tonic-gate 		break;
11467c478bd9Sstevel@tonic-gate 	}
11477c478bd9Sstevel@tonic-gate 	rw_enter(module_lock, RW_READER);
11487c478bd9Sstevel@tonic-gate 
11497c478bd9Sstevel@tonic-gate 	if (id != -1)
11507c478bd9Sstevel@tonic-gate 		mutex_exit(&mod_lock);
11517c478bd9Sstevel@tonic-gate 
11527c478bd9Sstevel@tonic-gate 	return (module_lock);
11537c478bd9Sstevel@tonic-gate }
11547c478bd9Sstevel@tonic-gate 
11557c478bd9Sstevel@tonic-gate /*
11567c478bd9Sstevel@tonic-gate  * Loadable syscall support.
11577c478bd9Sstevel@tonic-gate  *	If needed, load the module, then reserve it by holding a read
11587c478bd9Sstevel@tonic-gate  *	lock for the duration of the call.
11597c478bd9Sstevel@tonic-gate  *	Later, if the syscall is not unloadable, it could patch the vector.
11607c478bd9Sstevel@tonic-gate  */
11617c478bd9Sstevel@tonic-gate /*ARGSUSED*/
11627c478bd9Sstevel@tonic-gate int64_t
loadable_syscall(long a0,long a1,long a2,long a3,long a4,long a5,long a6,long a7)11637c478bd9Sstevel@tonic-gate loadable_syscall(
11647c478bd9Sstevel@tonic-gate     long a0, long a1, long a2, long a3,
11657c478bd9Sstevel@tonic-gate     long a4, long a5, long a6, long a7)
11667c478bd9Sstevel@tonic-gate {
11677c478bd9Sstevel@tonic-gate 	klwp_t *lwp = ttolwp(curthread);
11687c478bd9Sstevel@tonic-gate 	int64_t	rval;
11697c478bd9Sstevel@tonic-gate 	struct sysent *callp;
11707c478bd9Sstevel@tonic-gate 	struct sysent *se = LWP_GETSYSENT(lwp);
11717c478bd9Sstevel@tonic-gate 	krwlock_t *module_lock;
11727c478bd9Sstevel@tonic-gate 	int code, error = 0;
11737c478bd9Sstevel@tonic-gate 
11747c478bd9Sstevel@tonic-gate 	code = curthread->t_sysnum;
11757c478bd9Sstevel@tonic-gate 	callp = se + code;
11767c478bd9Sstevel@tonic-gate 
11777c478bd9Sstevel@tonic-gate 	/*
11787c478bd9Sstevel@tonic-gate 	 * Try to autoload the system call if necessary
11797c478bd9Sstevel@tonic-gate 	 */
11807c478bd9Sstevel@tonic-gate 	module_lock = lock_syscall(se, code);
11817c478bd9Sstevel@tonic-gate 
11827c478bd9Sstevel@tonic-gate 	/*
11837c478bd9Sstevel@tonic-gate 	 * we've locked either the loaded syscall or nosys
11847c478bd9Sstevel@tonic-gate 	 */
11857c478bd9Sstevel@tonic-gate 
11867c478bd9Sstevel@tonic-gate 	if (lwp_getdatamodel(lwp) == DATAMODEL_NATIVE) {
11877c478bd9Sstevel@tonic-gate #if defined(_LP64)
11887c478bd9Sstevel@tonic-gate 		if (callp->sy_flags & SE_ARGC) {
11891ec00b5aSToomas Soome 			rval = (int64_t)(*callp->sy_call)(a0, a1, a2, a3,
11901ec00b5aSToomas Soome 			    a4, a5);
11911ec00b5aSToomas Soome 		} else {
11927c478bd9Sstevel@tonic-gate 			rval = syscall_ap();
11931ec00b5aSToomas Soome 		}
11947c478bd9Sstevel@tonic-gate 	} else {
11957c478bd9Sstevel@tonic-gate #endif
11967c478bd9Sstevel@tonic-gate 		/*
11977c478bd9Sstevel@tonic-gate 		 * Now that it's loaded, make sure enough args were copied.
11987c478bd9Sstevel@tonic-gate 		 */
11997c478bd9Sstevel@tonic-gate 		if (COPYIN_ARGS32(lwptoregs(lwp), lwp->lwp_ap, callp->sy_narg))
12007c478bd9Sstevel@tonic-gate 			error = EFAULT;
12017c478bd9Sstevel@tonic-gate 		if (error) {
12027c478bd9Sstevel@tonic-gate 			rval = set_errno(error);
12037c478bd9Sstevel@tonic-gate 		} else if (callp->sy_flags & SE_ARGC) {
12041ec00b5aSToomas Soome 			rval = (int64_t)(*callp->sy_call)(lwp->lwp_ap[0],
12051ec00b5aSToomas Soome 			    lwp->lwp_ap[1], lwp->lwp_ap[2], lwp->lwp_ap[3],
12061ec00b5aSToomas Soome 			    lwp->lwp_ap[4], lwp->lwp_ap[5]);
12071ec00b5aSToomas Soome 		} else {
12087c478bd9Sstevel@tonic-gate 			rval = syscall_ap();
12091ec00b5aSToomas Soome 		}
12107c478bd9Sstevel@tonic-gate 	}
12117c478bd9Sstevel@tonic-gate 
12127c478bd9Sstevel@tonic-gate 	rw_exit(module_lock);
12137c478bd9Sstevel@tonic-gate 	return (rval);
12147c478bd9Sstevel@tonic-gate }
12157c478bd9Sstevel@tonic-gate 
12167c478bd9Sstevel@tonic-gate /*
12177c478bd9Sstevel@tonic-gate  * Indirect syscall handled in libc on x86 architectures
12187c478bd9Sstevel@tonic-gate  */
12197c478bd9Sstevel@tonic-gate int64_t
indir()12207c478bd9Sstevel@tonic-gate indir()
12217c478bd9Sstevel@tonic-gate {
12227c478bd9Sstevel@tonic-gate 	return (nosys());
12237c478bd9Sstevel@tonic-gate }
12247c478bd9Sstevel@tonic-gate 
12257c478bd9Sstevel@tonic-gate /*
12267c478bd9Sstevel@tonic-gate  * set_errno - set an error return from the current system call.
12277c478bd9Sstevel@tonic-gate  *	This could be a macro.
12287c478bd9Sstevel@tonic-gate  *	This returns the value it is passed, so that the caller can
12297c478bd9Sstevel@tonic-gate  *	use tail-recursion-elimination and do return (set_errno(ERRNO));
12307c478bd9Sstevel@tonic-gate  */
12317c478bd9Sstevel@tonic-gate uint_t
set_errno(uint_t error)12327c478bd9Sstevel@tonic-gate set_errno(uint_t error)
12337c478bd9Sstevel@tonic-gate {
12347c478bd9Sstevel@tonic-gate 	ASSERT(error != 0);		/* must not be used to clear errno */
12357c478bd9Sstevel@tonic-gate 
12367c478bd9Sstevel@tonic-gate 	curthread->t_post_sys = 1;	/* have post_syscall do error return */
12377c478bd9Sstevel@tonic-gate 	return (ttolwp(curthread)->lwp_errno = error);
12387c478bd9Sstevel@tonic-gate }
12397c478bd9Sstevel@tonic-gate 
12407c478bd9Sstevel@tonic-gate /*
12417c478bd9Sstevel@tonic-gate  * set_proc_pre_sys - Set pre-syscall processing for entire process.
12427c478bd9Sstevel@tonic-gate  */
12437c478bd9Sstevel@tonic-gate void
set_proc_pre_sys(proc_t * p)12447c478bd9Sstevel@tonic-gate set_proc_pre_sys(proc_t *p)
12457c478bd9Sstevel@tonic-gate {
12467c478bd9Sstevel@tonic-gate 	kthread_t	*t;
12477c478bd9Sstevel@tonic-gate 	kthread_t	*first;
12487c478bd9Sstevel@tonic-gate 
12497c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&p->p_lock));
12507c478bd9Sstevel@tonic-gate 
12517c478bd9Sstevel@tonic-gate 	t = first = p->p_tlist;
12527c478bd9Sstevel@tonic-gate 	do {
12537c478bd9Sstevel@tonic-gate 		t->t_pre_sys = 1;
12547c478bd9Sstevel@tonic-gate 	} while ((t = t->t_forw) != first);
12557c478bd9Sstevel@tonic-gate }
12567c478bd9Sstevel@tonic-gate 
12577c478bd9Sstevel@tonic-gate /*
12587c478bd9Sstevel@tonic-gate  * set_proc_post_sys - Set post-syscall processing for entire process.
12597c478bd9Sstevel@tonic-gate  */
12607c478bd9Sstevel@tonic-gate void
set_proc_post_sys(proc_t * p)12617c478bd9Sstevel@tonic-gate set_proc_post_sys(proc_t *p)
12627c478bd9Sstevel@tonic-gate {
12637c478bd9Sstevel@tonic-gate 	kthread_t	*t;
12647c478bd9Sstevel@tonic-gate 	kthread_t	*first;
12657c478bd9Sstevel@tonic-gate 
12667c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&p->p_lock));
12677c478bd9Sstevel@tonic-gate 
12687c478bd9Sstevel@tonic-gate 	t = first = p->p_tlist;
12697c478bd9Sstevel@tonic-gate 	do {
12707c478bd9Sstevel@tonic-gate 		t->t_post_sys = 1;
12717c478bd9Sstevel@tonic-gate 	} while ((t = t->t_forw) != first);
12727c478bd9Sstevel@tonic-gate }
12737c478bd9Sstevel@tonic-gate 
12747c478bd9Sstevel@tonic-gate /*
12757c478bd9Sstevel@tonic-gate  * set_proc_sys - Set pre- and post-syscall processing for entire process.
12767c478bd9Sstevel@tonic-gate  */
12777c478bd9Sstevel@tonic-gate void
set_proc_sys(proc_t * p)12787c478bd9Sstevel@tonic-gate set_proc_sys(proc_t *p)
12797c478bd9Sstevel@tonic-gate {
12807c478bd9Sstevel@tonic-gate 	kthread_t	*t;
12817c478bd9Sstevel@tonic-gate 	kthread_t	*first;
12827c478bd9Sstevel@tonic-gate 
12837c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&p->p_lock));
12847c478bd9Sstevel@tonic-gate 
12857c478bd9Sstevel@tonic-gate 	t = first = p->p_tlist;
12867c478bd9Sstevel@tonic-gate 	do {
12877c478bd9Sstevel@tonic-gate 		t->t_pre_sys = 1;
12887c478bd9Sstevel@tonic-gate 		t->t_post_sys = 1;
12897c478bd9Sstevel@tonic-gate 	} while ((t = t->t_forw) != first);
12907c478bd9Sstevel@tonic-gate }
12917c478bd9Sstevel@tonic-gate 
12927c478bd9Sstevel@tonic-gate /*
12937c478bd9Sstevel@tonic-gate  * set_all_proc_sys - set pre- and post-syscall processing flags for all
12947c478bd9Sstevel@tonic-gate  * user processes.
12957c478bd9Sstevel@tonic-gate  *
12967c478bd9Sstevel@tonic-gate  * This is needed when auditing, tracing, or other facilities which affect
12977c478bd9Sstevel@tonic-gate  * all processes are turned on.
12987c478bd9Sstevel@tonic-gate  */
12997c478bd9Sstevel@tonic-gate void
set_all_proc_sys()13007c478bd9Sstevel@tonic-gate set_all_proc_sys()
13017c478bd9Sstevel@tonic-gate {
13027c478bd9Sstevel@tonic-gate 	kthread_t	*t;
13037c478bd9Sstevel@tonic-gate 	kthread_t	*first;
13047c478bd9Sstevel@tonic-gate 
13057c478bd9Sstevel@tonic-gate 	mutex_enter(&pidlock);
13067c478bd9Sstevel@tonic-gate 	t = first = curthread;
13077c478bd9Sstevel@tonic-gate 	do {
13087c478bd9Sstevel@tonic-gate 		t->t_pre_sys = 1;
13097c478bd9Sstevel@tonic-gate 		t->t_post_sys = 1;
13107c478bd9Sstevel@tonic-gate 	} while ((t = t->t_next) != first);
13117c478bd9Sstevel@tonic-gate 	mutex_exit(&pidlock);
13127c478bd9Sstevel@tonic-gate }
13137c478bd9Sstevel@tonic-gate 
1314005d3febSMarek Pospisil /*
1315005d3febSMarek Pospisil  * set_all_zone_usr_proc_sys - set pre- and post-syscall processing flags for
1316005d3febSMarek Pospisil  * all user processes running in the zone of the current process
1317005d3febSMarek Pospisil  *
1318005d3febSMarek Pospisil  * This is needed when auditing, tracing, or other facilities which affect
1319005d3febSMarek Pospisil  * all processes are turned on.
1320005d3febSMarek Pospisil  */
1321005d3febSMarek Pospisil void
set_all_zone_usr_proc_sys(zoneid_t zoneid)1322005d3febSMarek Pospisil set_all_zone_usr_proc_sys(zoneid_t zoneid)
1323005d3febSMarek Pospisil {
1324005d3febSMarek Pospisil 	proc_t	    *p;
1325005d3febSMarek Pospisil 	kthread_t   *t;
1326005d3febSMarek Pospisil 
1327005d3febSMarek Pospisil 	mutex_enter(&pidlock);
1328005d3febSMarek Pospisil 	for (p = practive; p != NULL; p = p->p_next) {
1329005d3febSMarek Pospisil 		/* skip kernel and incomplete processes */
1330005d3febSMarek Pospisil 		if (p->p_exec == NULLVP || p->p_as == &kas ||
1331005d3febSMarek Pospisil 		    p->p_stat == SIDL || p->p_stat == SZOMB ||
1332005d3febSMarek Pospisil 		    (p->p_flag & (SSYS | SEXITING | SEXITLWPS)))
1333005d3febSMarek Pospisil 			continue;
1334005d3febSMarek Pospisil 		/*
1335005d3febSMarek Pospisil 		 * Only processes in the given zone (eventually in
1336005d3febSMarek Pospisil 		 * all zones) are taken into account
1337005d3febSMarek Pospisil 		 */
1338005d3febSMarek Pospisil 		if (zoneid == ALL_ZONES || p->p_zone->zone_id == zoneid) {
1339005d3febSMarek Pospisil 			mutex_enter(&p->p_lock);
134096992ee7SEthindra Ramamurthy 			if ((t = p->p_tlist) == NULL) {
134196992ee7SEthindra Ramamurthy 				mutex_exit(&p->p_lock);
1342005d3febSMarek Pospisil 				continue;
134396992ee7SEthindra Ramamurthy 			}
1344005d3febSMarek Pospisil 			/*
1345005d3febSMarek Pospisil 			 * Set pre- and post-syscall processing flags
1346005d3febSMarek Pospisil 			 * for all threads of the process
1347005d3febSMarek Pospisil 			 */
1348005d3febSMarek Pospisil 			do {
1349005d3febSMarek Pospisil 				t->t_pre_sys = 1;
1350005d3febSMarek Pospisil 				t->t_post_sys = 1;
1351005d3febSMarek Pospisil 			} while (p->p_tlist != (t = t->t_forw));
1352005d3febSMarek Pospisil 			mutex_exit(&p->p_lock);
1353005d3febSMarek Pospisil 		}
1354005d3febSMarek Pospisil 	}
1355005d3febSMarek Pospisil 	mutex_exit(&pidlock);
1356005d3febSMarek Pospisil }
1357005d3febSMarek Pospisil 
13587c478bd9Sstevel@tonic-gate /*
13597c478bd9Sstevel@tonic-gate  * set_proc_ast - Set asynchronous service trap (AST) flag for all
13607c478bd9Sstevel@tonic-gate  * threads in process.
13617c478bd9Sstevel@tonic-gate  */
13627c478bd9Sstevel@tonic-gate void
set_proc_ast(proc_t * p)13637c478bd9Sstevel@tonic-gate set_proc_ast(proc_t *p)
13647c478bd9Sstevel@tonic-gate {
13657c478bd9Sstevel@tonic-gate 	kthread_t	*t;
13667c478bd9Sstevel@tonic-gate 	kthread_t	*first;
13677c478bd9Sstevel@tonic-gate 
13687c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&p->p_lock));
13697c478bd9Sstevel@tonic-gate 
13707c478bd9Sstevel@tonic-gate 	t = first = p->p_tlist;
13717c478bd9Sstevel@tonic-gate 	do {
13727c478bd9Sstevel@tonic-gate 		aston(t);
13737c478bd9Sstevel@tonic-gate 	} while ((t = t->t_forw) != first);
13747c478bd9Sstevel@tonic-gate }
1375