xref: /titanic_44/usr/src/uts/sparc/os/syscall.c (revision 75521904d7c3dbe11337904d9bead2518c94cc50)
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
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
22*75521904Sraf 
237c478bd9Sstevel@tonic-gate /*
247c478bd9Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
257c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
267c478bd9Sstevel@tonic-gate  */
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate #include <sys/param.h>
317c478bd9Sstevel@tonic-gate #include <sys/vmparam.h>
327c478bd9Sstevel@tonic-gate #include <sys/types.h>
337c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
347c478bd9Sstevel@tonic-gate #include <sys/systm.h>
357c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
367c478bd9Sstevel@tonic-gate #include <sys/signal.h>
377c478bd9Sstevel@tonic-gate #include <sys/stack.h>
387c478bd9Sstevel@tonic-gate #include <sys/cred.h>
397c478bd9Sstevel@tonic-gate #include <sys/user.h>
407c478bd9Sstevel@tonic-gate #include <sys/debug.h>
417c478bd9Sstevel@tonic-gate #include <sys/errno.h>
427c478bd9Sstevel@tonic-gate #include <sys/proc.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/machtrap.h>
517c478bd9Sstevel@tonic-gate #include <sys/sysinfo.h>
527c478bd9Sstevel@tonic-gate #include <sys/procfs.h>
537c478bd9Sstevel@tonic-gate #include <sys/prsystm.h>
547c478bd9Sstevel@tonic-gate #include <sys/fpu/fpusystm.h>
557c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
567c478bd9Sstevel@tonic-gate #include <sys/aio_impl.h>
577c478bd9Sstevel@tonic-gate #include <c2/audit.h>
587c478bd9Sstevel@tonic-gate #include <sys/tnf.h>
597c478bd9Sstevel@tonic-gate #include <sys/tnf_probe.h>
607c478bd9Sstevel@tonic-gate #include <sys/machpcb.h>
617c478bd9Sstevel@tonic-gate #include <sys/privregs.h>
627c478bd9Sstevel@tonic-gate #include <sys/copyops.h>
637c478bd9Sstevel@tonic-gate #include <sys/timer.h>
647c478bd9Sstevel@tonic-gate #include <sys/priv.h>
657c478bd9Sstevel@tonic-gate #include <sys/msacct.h>
667c478bd9Sstevel@tonic-gate 
677c478bd9Sstevel@tonic-gate int syscalltrace = 0;
687c478bd9Sstevel@tonic-gate #ifdef SYSCALLTRACE
697c478bd9Sstevel@tonic-gate static kmutex_t	systrace_lock;		/* syscall tracing lock */
707c478bd9Sstevel@tonic-gate #endif /* SYSCALLTRACE */
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate static krwlock_t *lock_syscall(struct sysent *, uint_t);
737c478bd9Sstevel@tonic-gate 
747c478bd9Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
757c478bd9Sstevel@tonic-gate static struct sysent *
767c478bd9Sstevel@tonic-gate lwp_getsysent(klwp_t *lwp)
777c478bd9Sstevel@tonic-gate {
787c478bd9Sstevel@tonic-gate 	if (lwp_getdatamodel(lwp) == DATAMODEL_NATIVE)
797c478bd9Sstevel@tonic-gate 		return (sysent);
807c478bd9Sstevel@tonic-gate 	return (sysent32);
817c478bd9Sstevel@tonic-gate }
827c478bd9Sstevel@tonic-gate #define	LWP_GETSYSENT(lwp)	(lwp_getsysent(lwp))
837c478bd9Sstevel@tonic-gate #else
847c478bd9Sstevel@tonic-gate #define	LWP_GETSYSENT(lwp)	(sysent)
857c478bd9Sstevel@tonic-gate #endif
867c478bd9Sstevel@tonic-gate 
877c478bd9Sstevel@tonic-gate /*
887c478bd9Sstevel@tonic-gate  * Arrange for the real time profiling signal to be dispatched.
897c478bd9Sstevel@tonic-gate  */
907c478bd9Sstevel@tonic-gate void
917c478bd9Sstevel@tonic-gate realsigprof(int sysnum, int error)
927c478bd9Sstevel@tonic-gate {
937c478bd9Sstevel@tonic-gate 	proc_t *p;
947c478bd9Sstevel@tonic-gate 	klwp_t *lwp;
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate 	if (curthread->t_rprof->rp_anystate == 0)
977c478bd9Sstevel@tonic-gate 		return;
987c478bd9Sstevel@tonic-gate 	p = ttoproc(curthread);
997c478bd9Sstevel@tonic-gate 	lwp = ttolwp(curthread);
1007c478bd9Sstevel@tonic-gate 	mutex_enter(&p->p_lock);
1017c478bd9Sstevel@tonic-gate 	if (sigismember(&p->p_ignore, SIGPROF) ||
1027c478bd9Sstevel@tonic-gate 	    signal_is_blocked(curthread, SIGPROF)) {
1037c478bd9Sstevel@tonic-gate 		mutex_exit(&p->p_lock);
1047c478bd9Sstevel@tonic-gate 		return;
1057c478bd9Sstevel@tonic-gate 	}
1067c478bd9Sstevel@tonic-gate 	lwp->lwp_siginfo.si_signo = SIGPROF;
1077c478bd9Sstevel@tonic-gate 	lwp->lwp_siginfo.si_code = PROF_SIG;
1087c478bd9Sstevel@tonic-gate 	lwp->lwp_siginfo.si_errno = error;
1097c478bd9Sstevel@tonic-gate 	hrt2ts(gethrtime(), &lwp->lwp_siginfo.si_tstamp);
1107c478bd9Sstevel@tonic-gate 	lwp->lwp_siginfo.si_syscall = sysnum;
1117c478bd9Sstevel@tonic-gate 	lwp->lwp_siginfo.si_nsysarg = (sysnum > 0 && sysnum < NSYSCALL) ?
1127c478bd9Sstevel@tonic-gate 		LWP_GETSYSENT(lwp)[sysnum].sy_narg : 0;
1137c478bd9Sstevel@tonic-gate 	lwp->lwp_siginfo.si_fault = lwp->lwp_lastfault;
1147c478bd9Sstevel@tonic-gate 	lwp->lwp_siginfo.si_faddr = lwp->lwp_lastfaddr;
1157c478bd9Sstevel@tonic-gate 	lwp->lwp_lastfault = 0;
1167c478bd9Sstevel@tonic-gate 	lwp->lwp_lastfaddr = NULL;
1177c478bd9Sstevel@tonic-gate 	sigtoproc(p, curthread, SIGPROF);
1187c478bd9Sstevel@tonic-gate 	mutex_exit(&p->p_lock);
1197c478bd9Sstevel@tonic-gate 	ASSERT(lwp->lwp_cursig == 0);
1207c478bd9Sstevel@tonic-gate 	if (issig(FORREAL)) {
1217c478bd9Sstevel@tonic-gate 		psig();
1227c478bd9Sstevel@tonic-gate 	}
1237c478bd9Sstevel@tonic-gate 	mutex_enter(&p->p_lock);
1247c478bd9Sstevel@tonic-gate 	lwp->lwp_siginfo.si_signo = 0;
1257c478bd9Sstevel@tonic-gate 	bzero(curthread->t_rprof, sizeof (*curthread->t_rprof));
1267c478bd9Sstevel@tonic-gate 	mutex_exit(&p->p_lock);
1277c478bd9Sstevel@tonic-gate }
1287c478bd9Sstevel@tonic-gate 
1297c478bd9Sstevel@tonic-gate /*
1307c478bd9Sstevel@tonic-gate  * Called to restore the lwp's register window just before
1317c478bd9Sstevel@tonic-gate  * returning to user level (only if the registers have been
1327c478bd9Sstevel@tonic-gate  * fetched or modified through /proc).
1337c478bd9Sstevel@tonic-gate  */
1347c478bd9Sstevel@tonic-gate /*ARGSUSED1*/
1357c478bd9Sstevel@tonic-gate void
1367c478bd9Sstevel@tonic-gate xregrestore(klwp_t *lwp, int shared)
1377c478bd9Sstevel@tonic-gate {
1387c478bd9Sstevel@tonic-gate 	/*
1397c478bd9Sstevel@tonic-gate 	 * If locals+ins were modified by /proc copy them out.
1407c478bd9Sstevel@tonic-gate 	 * Also copy to the shared window, if necessary.
1417c478bd9Sstevel@tonic-gate 	 */
1427c478bd9Sstevel@tonic-gate 	if (lwp->lwp_pcb.pcb_xregstat == XREGMODIFIED) {
1437c478bd9Sstevel@tonic-gate 		struct machpcb *mpcb = lwptompcb(lwp);
1447c478bd9Sstevel@tonic-gate 		caddr_t sp = (caddr_t)lwptoregs(lwp)->r_sp;
1457c478bd9Sstevel@tonic-gate 
1467c478bd9Sstevel@tonic-gate 		size_t rwinsize;
1477c478bd9Sstevel@tonic-gate 		caddr_t rwp;
1487c478bd9Sstevel@tonic-gate 		int is64;
1497c478bd9Sstevel@tonic-gate 
1507c478bd9Sstevel@tonic-gate 		if (lwp_getdatamodel(lwp) == DATAMODEL_LP64) {
1517c478bd9Sstevel@tonic-gate 			rwinsize = sizeof (struct rwindow);
1527c478bd9Sstevel@tonic-gate 			rwp = sp + STACK_BIAS;
1537c478bd9Sstevel@tonic-gate 			is64 = 1;
1547c478bd9Sstevel@tonic-gate 		} else {
1557c478bd9Sstevel@tonic-gate 			rwinsize = sizeof (struct rwindow32);
156*75521904Sraf 			sp = (caddr_t)(uintptr_t)(caddr32_t)(uintptr_t)sp;
1577c478bd9Sstevel@tonic-gate 			rwp = sp;
1587c478bd9Sstevel@tonic-gate 			is64 = 0;
1597c478bd9Sstevel@tonic-gate 		}
1607c478bd9Sstevel@tonic-gate 
1617c478bd9Sstevel@tonic-gate 		if (is64)
1627c478bd9Sstevel@tonic-gate 			(void) copyout_nowatch(&lwp->lwp_pcb.pcb_xregs,
1637c478bd9Sstevel@tonic-gate 				rwp, rwinsize);
1647c478bd9Sstevel@tonic-gate 		else {
1657c478bd9Sstevel@tonic-gate 			struct rwindow32 rwindow32;
1667c478bd9Sstevel@tonic-gate 			int watched;
1677c478bd9Sstevel@tonic-gate 
1687c478bd9Sstevel@tonic-gate 			watched = watch_disable_addr(rwp, rwinsize, S_WRITE);
1697c478bd9Sstevel@tonic-gate 			rwindow_nto32(&lwp->lwp_pcb.pcb_xregs, &rwindow32);
1707c478bd9Sstevel@tonic-gate 			(void) copyout(&rwindow32, rwp, rwinsize);
1717c478bd9Sstevel@tonic-gate 			if (watched)
1727c478bd9Sstevel@tonic-gate 				watch_enable_addr(rwp, rwinsize, S_WRITE);
1737c478bd9Sstevel@tonic-gate 		}
1747c478bd9Sstevel@tonic-gate 
1757c478bd9Sstevel@tonic-gate 		/* also copy to the user return window */
1767c478bd9Sstevel@tonic-gate 		mpcb->mpcb_rsp[0] = sp;
1777c478bd9Sstevel@tonic-gate 		mpcb->mpcb_rsp[1] = NULL;
1787c478bd9Sstevel@tonic-gate 		bcopy(&lwp->lwp_pcb.pcb_xregs, &mpcb->mpcb_rwin[0],
1797c478bd9Sstevel@tonic-gate 			sizeof (lwp->lwp_pcb.pcb_xregs));
1807c478bd9Sstevel@tonic-gate 	}
1817c478bd9Sstevel@tonic-gate 	lwp->lwp_pcb.pcb_xregstat = XREGNONE;
1827c478bd9Sstevel@tonic-gate }
1837c478bd9Sstevel@tonic-gate 
1847c478bd9Sstevel@tonic-gate 
1857c478bd9Sstevel@tonic-gate /*
1867c478bd9Sstevel@tonic-gate  * Get the arguments to the current system call.
1877c478bd9Sstevel@tonic-gate  *	lwp->lwp_ap normally points to the out regs in the reg structure.
1887c478bd9Sstevel@tonic-gate  *	If the user is going to change the out registers and might want to
1897c478bd9Sstevel@tonic-gate  *	get the args (for /proc tracing), it must copy the args elsewhere
1907c478bd9Sstevel@tonic-gate  *	via save_syscall_args().
1917c478bd9Sstevel@tonic-gate  */
1927c478bd9Sstevel@tonic-gate uint_t
1937c478bd9Sstevel@tonic-gate get_syscall_args(klwp_t *lwp, long *argp, int *nargsp)
1947c478bd9Sstevel@tonic-gate {
1957c478bd9Sstevel@tonic-gate 	kthread_t	*t = lwptot(lwp);
1967c478bd9Sstevel@tonic-gate 	uint_t	code = t->t_sysnum;
1977c478bd9Sstevel@tonic-gate 	long	mask;
1987c478bd9Sstevel@tonic-gate 	long	*ap;
1997c478bd9Sstevel@tonic-gate 	int	nargs;
2007c478bd9Sstevel@tonic-gate 
2017c478bd9Sstevel@tonic-gate 	if (lwptoproc(lwp)->p_model == DATAMODEL_ILP32)
2027c478bd9Sstevel@tonic-gate 		mask = (uint32_t)0xffffffffU;
2037c478bd9Sstevel@tonic-gate 	else
2047c478bd9Sstevel@tonic-gate 		mask = 0xffffffffffffffff;
2057c478bd9Sstevel@tonic-gate 
2067c478bd9Sstevel@tonic-gate 	if (code != 0 && code < NSYSCALL) {
2077c478bd9Sstevel@tonic-gate 
2087c478bd9Sstevel@tonic-gate 		nargs = LWP_GETSYSENT(lwp)[code].sy_narg;
2097c478bd9Sstevel@tonic-gate 
2107c478bd9Sstevel@tonic-gate 		ASSERT(nargs <= MAXSYSARGS);
2117c478bd9Sstevel@tonic-gate 
2127c478bd9Sstevel@tonic-gate 		*nargsp = nargs;
2137c478bd9Sstevel@tonic-gate 		ap = lwp->lwp_ap;
2147c478bd9Sstevel@tonic-gate 		while (nargs-- > 0)
2157c478bd9Sstevel@tonic-gate 			*argp++ = *ap++ & mask;
2167c478bd9Sstevel@tonic-gate 	} else {
2177c478bd9Sstevel@tonic-gate 		*nargsp = 0;
2187c478bd9Sstevel@tonic-gate 	}
2197c478bd9Sstevel@tonic-gate 	return (code);
2207c478bd9Sstevel@tonic-gate }
2217c478bd9Sstevel@tonic-gate 
2227c478bd9Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
2237c478bd9Sstevel@tonic-gate /*
2247c478bd9Sstevel@tonic-gate  * Get the arguments to the current 32-bit system call.
2257c478bd9Sstevel@tonic-gate  */
2267c478bd9Sstevel@tonic-gate uint_t
2277c478bd9Sstevel@tonic-gate get_syscall32_args(klwp_t *lwp, int *argp, int *nargsp)
2287c478bd9Sstevel@tonic-gate {
2297c478bd9Sstevel@tonic-gate 	long args[MAXSYSARGS];
2307c478bd9Sstevel@tonic-gate 	uint_t i, code;
2317c478bd9Sstevel@tonic-gate 
2327c478bd9Sstevel@tonic-gate 	code = get_syscall_args(lwp, args, nargsp);
2337c478bd9Sstevel@tonic-gate 	for (i = 0; i != *nargsp; i++)
2347c478bd9Sstevel@tonic-gate 		*argp++ = (int)args[i];
2357c478bd9Sstevel@tonic-gate 	return (code);
2367c478bd9Sstevel@tonic-gate }
2377c478bd9Sstevel@tonic-gate #endif
2387c478bd9Sstevel@tonic-gate 
2397c478bd9Sstevel@tonic-gate /*
2407c478bd9Sstevel@tonic-gate  * 	Save the system call arguments in a safe place.
2417c478bd9Sstevel@tonic-gate  *	lwp->lwp_ap normally points to the out regs in the reg structure.
2427c478bd9Sstevel@tonic-gate  *	If the user is going to change the out registers, g1, or the stack,
2437c478bd9Sstevel@tonic-gate  *	and might want to get the args (for /proc tracing), it must copy
2447c478bd9Sstevel@tonic-gate  *	the args elsewhere via save_syscall_args().
2457c478bd9Sstevel@tonic-gate  *
2467c478bd9Sstevel@tonic-gate  *	This may be called from stop() even when we're not in a system call.
2477c478bd9Sstevel@tonic-gate  *	Since there's no easy way to tell, this must be safe (not panic).
2487c478bd9Sstevel@tonic-gate  *	If the copyins get data faults, return non-zero.
2497c478bd9Sstevel@tonic-gate  */
2507c478bd9Sstevel@tonic-gate int
2517c478bd9Sstevel@tonic-gate save_syscall_args()
2527c478bd9Sstevel@tonic-gate {
2537c478bd9Sstevel@tonic-gate 	kthread_t	*t = curthread;
2547c478bd9Sstevel@tonic-gate 	klwp_t		*lwp = ttolwp(t);
2557c478bd9Sstevel@tonic-gate 	struct regs	*rp = lwptoregs(lwp);
2567c478bd9Sstevel@tonic-gate 	uint_t		code = t->t_sysnum;
2577c478bd9Sstevel@tonic-gate 	uint_t		nargs;
2587c478bd9Sstevel@tonic-gate 	int		i;
2597c478bd9Sstevel@tonic-gate 	caddr_t		ua;
2607c478bd9Sstevel@tonic-gate 	model_t		datamodel;
2617c478bd9Sstevel@tonic-gate 
2627c478bd9Sstevel@tonic-gate 	if (lwp->lwp_argsaved || code == 0)
2637c478bd9Sstevel@tonic-gate 		return (0);		/* args already saved or not needed */
2647c478bd9Sstevel@tonic-gate 
2657c478bd9Sstevel@tonic-gate 	if (code >= NSYSCALL) {
2667c478bd9Sstevel@tonic-gate 		nargs = 0;		/* illegal syscall */
2677c478bd9Sstevel@tonic-gate 	} else {
2687c478bd9Sstevel@tonic-gate 		struct sysent *se = LWP_GETSYSENT(lwp);
2697c478bd9Sstevel@tonic-gate 		struct sysent *callp = se + code;
2707c478bd9Sstevel@tonic-gate 
2717c478bd9Sstevel@tonic-gate 		nargs = callp->sy_narg;
2727c478bd9Sstevel@tonic-gate 		if (LOADABLE_SYSCALL(callp) && nargs == 0) {
2737c478bd9Sstevel@tonic-gate 			krwlock_t	*module_lock;
2747c478bd9Sstevel@tonic-gate 
2757c478bd9Sstevel@tonic-gate 			/*
2767c478bd9Sstevel@tonic-gate 			 * Find out how many arguments the system
2777c478bd9Sstevel@tonic-gate 			 * call uses.
2787c478bd9Sstevel@tonic-gate 			 *
2797c478bd9Sstevel@tonic-gate 			 * We have the property that loaded syscalls
2807c478bd9Sstevel@tonic-gate 			 * never change the number of arguments they
2817c478bd9Sstevel@tonic-gate 			 * use after they've been loaded once.  This
2827c478bd9Sstevel@tonic-gate 			 * allows us to stop for /proc tracing without
2837c478bd9Sstevel@tonic-gate 			 * holding the module lock.
2847c478bd9Sstevel@tonic-gate 			 * /proc is assured that sy_narg is valid.
2857c478bd9Sstevel@tonic-gate 			 */
2867c478bd9Sstevel@tonic-gate 			module_lock = lock_syscall(se, code);
2877c478bd9Sstevel@tonic-gate 			nargs = callp->sy_narg;
2887c478bd9Sstevel@tonic-gate 			rw_exit(module_lock);
2897c478bd9Sstevel@tonic-gate 		}
2907c478bd9Sstevel@tonic-gate 	}
2917c478bd9Sstevel@tonic-gate 
2927c478bd9Sstevel@tonic-gate 	/*
2937c478bd9Sstevel@tonic-gate 	 * Fetch the system call arguments.
2947c478bd9Sstevel@tonic-gate 	 */
2957c478bd9Sstevel@tonic-gate 	if (nargs == 0)
2967c478bd9Sstevel@tonic-gate 		goto out;
2977c478bd9Sstevel@tonic-gate 
2987c478bd9Sstevel@tonic-gate 
2997c478bd9Sstevel@tonic-gate 	ASSERT(nargs <= MAXSYSARGS);
3007c478bd9Sstevel@tonic-gate 
3017c478bd9Sstevel@tonic-gate 	if ((datamodel = lwp_getdatamodel(lwp)) == DATAMODEL_ILP32) {
3027c478bd9Sstevel@tonic-gate 
3037c478bd9Sstevel@tonic-gate 		if (rp->r_g1 == 0) {	/* indirect syscall */
3047c478bd9Sstevel@tonic-gate 
3057c478bd9Sstevel@tonic-gate 			lwp->lwp_arg[0] = (uint32_t)rp->r_o1;
3067c478bd9Sstevel@tonic-gate 			lwp->lwp_arg[1] = (uint32_t)rp->r_o2;
3077c478bd9Sstevel@tonic-gate 			lwp->lwp_arg[2] = (uint32_t)rp->r_o3;
3087c478bd9Sstevel@tonic-gate 			lwp->lwp_arg[3] = (uint32_t)rp->r_o4;
3097c478bd9Sstevel@tonic-gate 			lwp->lwp_arg[4] = (uint32_t)rp->r_o5;
3107c478bd9Sstevel@tonic-gate 			if (nargs > 5) {
311*75521904Sraf 				ua = (caddr_t)(uintptr_t)(caddr32_t)(uintptr_t)
312*75521904Sraf 				    (rp->r_sp + MINFRAME32);
3137c478bd9Sstevel@tonic-gate 				for (i = 5; i < nargs; i++) {
3147c478bd9Sstevel@tonic-gate 					uint32_t a;
3157c478bd9Sstevel@tonic-gate 					if (fuword32(ua, &a) != 0)
3167c478bd9Sstevel@tonic-gate 						return (-1);
3177c478bd9Sstevel@tonic-gate 					lwp->lwp_arg[i] = a;
3187c478bd9Sstevel@tonic-gate 					ua += sizeof (a);
3197c478bd9Sstevel@tonic-gate 				}
3207c478bd9Sstevel@tonic-gate 			}
3217c478bd9Sstevel@tonic-gate 		} else {
3227c478bd9Sstevel@tonic-gate 			lwp->lwp_arg[0] = (uint32_t)rp->r_o0;
3237c478bd9Sstevel@tonic-gate 			lwp->lwp_arg[1] = (uint32_t)rp->r_o1;
3247c478bd9Sstevel@tonic-gate 			lwp->lwp_arg[2] = (uint32_t)rp->r_o2;
3257c478bd9Sstevel@tonic-gate 			lwp->lwp_arg[3] = (uint32_t)rp->r_o3;
3267c478bd9Sstevel@tonic-gate 			lwp->lwp_arg[4] = (uint32_t)rp->r_o4;
3277c478bd9Sstevel@tonic-gate 			lwp->lwp_arg[5] = (uint32_t)rp->r_o5;
3287c478bd9Sstevel@tonic-gate 			if (nargs > 6) {
329*75521904Sraf 				ua = (caddr_t)(uintptr_t)(caddr32_t)(uintptr_t)
330*75521904Sraf 				    (rp->r_sp + MINFRAME32);
3317c478bd9Sstevel@tonic-gate 				for (i = 6; i < nargs; i++) {
3327c478bd9Sstevel@tonic-gate 					uint32_t a;
3337c478bd9Sstevel@tonic-gate 					if (fuword32(ua, &a) != 0)
3347c478bd9Sstevel@tonic-gate 						return (-1);
3357c478bd9Sstevel@tonic-gate 					lwp->lwp_arg[i] = a;
3367c478bd9Sstevel@tonic-gate 					ua += sizeof (a);
3377c478bd9Sstevel@tonic-gate 				}
3387c478bd9Sstevel@tonic-gate 			}
3397c478bd9Sstevel@tonic-gate 		}
3407c478bd9Sstevel@tonic-gate 	} else {
3417c478bd9Sstevel@tonic-gate 		ASSERT(datamodel == DATAMODEL_LP64);
3427c478bd9Sstevel@tonic-gate 		lwp->lwp_arg[0] = rp->r_o0;
3437c478bd9Sstevel@tonic-gate 		lwp->lwp_arg[1] = rp->r_o1;
3447c478bd9Sstevel@tonic-gate 		lwp->lwp_arg[2] = rp->r_o2;
3457c478bd9Sstevel@tonic-gate 		lwp->lwp_arg[3] = rp->r_o3;
3467c478bd9Sstevel@tonic-gate 		lwp->lwp_arg[4] = rp->r_o4;
3477c478bd9Sstevel@tonic-gate 		lwp->lwp_arg[5] = rp->r_o5;
3487c478bd9Sstevel@tonic-gate 		if (nargs > 6) {
3497c478bd9Sstevel@tonic-gate 			ua = (caddr_t)rp->r_sp + MINFRAME + STACK_BIAS;
3507c478bd9Sstevel@tonic-gate 			for (i = 6; i < nargs; i++) {
3517c478bd9Sstevel@tonic-gate 				unsigned long a;
3527c478bd9Sstevel@tonic-gate 				if (fulword(ua, &a) != 0)
3537c478bd9Sstevel@tonic-gate 					return (-1);
3547c478bd9Sstevel@tonic-gate 				lwp->lwp_arg[i] = a;
3557c478bd9Sstevel@tonic-gate 				ua += sizeof (a);
3567c478bd9Sstevel@tonic-gate 			}
3577c478bd9Sstevel@tonic-gate 		}
3587c478bd9Sstevel@tonic-gate 	}
3597c478bd9Sstevel@tonic-gate 
3607c478bd9Sstevel@tonic-gate out:
3617c478bd9Sstevel@tonic-gate 	lwp->lwp_ap = lwp->lwp_arg;
3627c478bd9Sstevel@tonic-gate 	lwp->lwp_argsaved = 1;
3637c478bd9Sstevel@tonic-gate 	t->t_post_sys = 1;	/* so lwp_ap will be reset */
3647c478bd9Sstevel@tonic-gate 	return (0);
3657c478bd9Sstevel@tonic-gate }
3667c478bd9Sstevel@tonic-gate 
3677c478bd9Sstevel@tonic-gate void
3687c478bd9Sstevel@tonic-gate reset_syscall_args(void)
3697c478bd9Sstevel@tonic-gate {
3707c478bd9Sstevel@tonic-gate 	klwp_t *lwp = ttolwp(curthread);
3717c478bd9Sstevel@tonic-gate 
3727c478bd9Sstevel@tonic-gate 	lwp->lwp_ap = (long *)&lwptoregs(lwp)->r_o0;
3737c478bd9Sstevel@tonic-gate 	lwp->lwp_argsaved = 0;
3747c478bd9Sstevel@tonic-gate }
3757c478bd9Sstevel@tonic-gate 
3767c478bd9Sstevel@tonic-gate /*
3777c478bd9Sstevel@tonic-gate  * nonexistent system call-- signal lwp (may want to handle it)
3787c478bd9Sstevel@tonic-gate  * flag error if lwp won't see signal immediately
3797c478bd9Sstevel@tonic-gate  * This works for old or new calling sequence.
3807c478bd9Sstevel@tonic-gate  */
3817c478bd9Sstevel@tonic-gate int64_t
3827c478bd9Sstevel@tonic-gate nosys()
3837c478bd9Sstevel@tonic-gate {
3847c478bd9Sstevel@tonic-gate 	tsignal(curthread, SIGSYS);
3857c478bd9Sstevel@tonic-gate 	return ((int64_t)set_errno(ENOSYS));
3867c478bd9Sstevel@tonic-gate }
3877c478bd9Sstevel@tonic-gate 
3887c478bd9Sstevel@tonic-gate /*
3897c478bd9Sstevel@tonic-gate  * Perform pre-system-call processing, including stopping for tracing,
3907c478bd9Sstevel@tonic-gate  * auditing, microstate-accounting, etc.
3917c478bd9Sstevel@tonic-gate  *
3927c478bd9Sstevel@tonic-gate  * This routine is called only if the t_pre_sys flag is set.  Any condition
3937c478bd9Sstevel@tonic-gate  * requiring pre-syscall handling must set the t_pre_sys flag.  If the
3947c478bd9Sstevel@tonic-gate  * condition is persistent, this routine will repost t_pre_sys.
3957c478bd9Sstevel@tonic-gate  */
3967c478bd9Sstevel@tonic-gate int
3977c478bd9Sstevel@tonic-gate pre_syscall(int arg0)
3987c478bd9Sstevel@tonic-gate {
3997c478bd9Sstevel@tonic-gate 	unsigned int code;
4007c478bd9Sstevel@tonic-gate 	kthread_t *t = curthread;
4017c478bd9Sstevel@tonic-gate 	proc_t *p = ttoproc(t);
4027c478bd9Sstevel@tonic-gate 	klwp_t *lwp = ttolwp(t);
4037c478bd9Sstevel@tonic-gate 	struct regs *rp = lwptoregs(lwp);
4047c478bd9Sstevel@tonic-gate 	int	repost;
4057c478bd9Sstevel@tonic-gate 
4067c478bd9Sstevel@tonic-gate 	t->t_pre_sys = repost = 0;	/* clear pre-syscall processing flag */
4077c478bd9Sstevel@tonic-gate 
4087c478bd9Sstevel@tonic-gate 	ASSERT(t->t_schedflag & TS_DONT_SWAP);
4097c478bd9Sstevel@tonic-gate 
4107c478bd9Sstevel@tonic-gate 	syscall_mstate(LMS_USER, LMS_SYSTEM);
4117c478bd9Sstevel@tonic-gate 
4127c478bd9Sstevel@tonic-gate 	/*
4137c478bd9Sstevel@tonic-gate 	 * The syscall arguments in the out registers should be pointed to
4147c478bd9Sstevel@tonic-gate 	 * by lwp_ap.  If the args need to be copied so that the outs can
4157c478bd9Sstevel@tonic-gate 	 * be changed without losing the ability to get the args for /proc,
4167c478bd9Sstevel@tonic-gate 	 * they can be saved by save_syscall_args(), and lwp_ap will be
4177c478bd9Sstevel@tonic-gate 	 * restored by post_syscall().
4187c478bd9Sstevel@tonic-gate 	 */
4197c478bd9Sstevel@tonic-gate 	ASSERT(lwp->lwp_ap == (long *)&rp->r_o0);
4207c478bd9Sstevel@tonic-gate 
4217c478bd9Sstevel@tonic-gate 	/*
4227c478bd9Sstevel@tonic-gate 	 * Make sure the thread is holding the latest credentials for the
4237c478bd9Sstevel@tonic-gate 	 * process.  The credentials in the process right now apply to this
4247c478bd9Sstevel@tonic-gate 	 * thread for the entire system call.
4257c478bd9Sstevel@tonic-gate 	 */
4267c478bd9Sstevel@tonic-gate 	if (t->t_cred != p->p_cred) {
4277c478bd9Sstevel@tonic-gate 		cred_t *oldcred = t->t_cred;
4287c478bd9Sstevel@tonic-gate 		/*
4297c478bd9Sstevel@tonic-gate 		 * DTrace accesses t_cred in probe context.  t_cred must
4307c478bd9Sstevel@tonic-gate 		 * always be either NULL, or point to a valid, allocated cred
4317c478bd9Sstevel@tonic-gate 		 * structure.
4327c478bd9Sstevel@tonic-gate 		 */
4337c478bd9Sstevel@tonic-gate 		t->t_cred = crgetcred();
4347c478bd9Sstevel@tonic-gate 		crfree(oldcred);
4357c478bd9Sstevel@tonic-gate 	}
4367c478bd9Sstevel@tonic-gate 
4377c478bd9Sstevel@tonic-gate 	/*
4387c478bd9Sstevel@tonic-gate 	 * Undo special arrangements to single-step the lwp
4397c478bd9Sstevel@tonic-gate 	 * so that a debugger will see valid register contents.
4407c478bd9Sstevel@tonic-gate 	 * Also so that the pc is valid for syncfpu().
4417c478bd9Sstevel@tonic-gate 	 * Also so that a syscall like exec() can be stepped.
4427c478bd9Sstevel@tonic-gate 	 */
4437c478bd9Sstevel@tonic-gate 	if (lwp->lwp_pcb.pcb_step != STEP_NONE) {
4447c478bd9Sstevel@tonic-gate 		(void) prundostep();
4457c478bd9Sstevel@tonic-gate 		repost = 1;
4467c478bd9Sstevel@tonic-gate 	}
4477c478bd9Sstevel@tonic-gate 
4487c478bd9Sstevel@tonic-gate 	/*
4497c478bd9Sstevel@tonic-gate 	 * Check for indirect system call in case we stop for tracing.
4507c478bd9Sstevel@tonic-gate 	 * Don't allow multiple indirection.
4517c478bd9Sstevel@tonic-gate 	 */
4527c478bd9Sstevel@tonic-gate 	code = t->t_sysnum;
4537c478bd9Sstevel@tonic-gate 	if (code == 0 && arg0 != 0) {		/* indirect syscall */
4547c478bd9Sstevel@tonic-gate 		code = arg0;
4557c478bd9Sstevel@tonic-gate 		t->t_sysnum = arg0;
4567c478bd9Sstevel@tonic-gate 	}
4577c478bd9Sstevel@tonic-gate 
4587c478bd9Sstevel@tonic-gate 	/*
4597c478bd9Sstevel@tonic-gate 	 * From the proc(4) manual page:
4607c478bd9Sstevel@tonic-gate 	 * When entry to a system call is being traced, the traced process
4617c478bd9Sstevel@tonic-gate 	 * stops after having begun the call to the system but before the
4627c478bd9Sstevel@tonic-gate 	 * system call arguments have been fetched from the process.
4637c478bd9Sstevel@tonic-gate 	 * If proc changes the args we must refetch them after starting.
4647c478bd9Sstevel@tonic-gate 	 */
4657c478bd9Sstevel@tonic-gate 	if (PTOU(p)->u_systrap) {
4667c478bd9Sstevel@tonic-gate 		if (prismember(&PTOU(p)->u_entrymask, code)) {
4677c478bd9Sstevel@tonic-gate 			/*
4687c478bd9Sstevel@tonic-gate 			 * Recheck stop condition, now that lock is held.
4697c478bd9Sstevel@tonic-gate 			 */
4707c478bd9Sstevel@tonic-gate 			mutex_enter(&p->p_lock);
4717c478bd9Sstevel@tonic-gate 			if (PTOU(p)->u_systrap &&
4727c478bd9Sstevel@tonic-gate 			    prismember(&PTOU(p)->u_entrymask, code)) {
4737c478bd9Sstevel@tonic-gate 				stop(PR_SYSENTRY, code);
4747c478bd9Sstevel@tonic-gate 				/*
4757c478bd9Sstevel@tonic-gate 				 * Must refetch args since they were
4767c478bd9Sstevel@tonic-gate 				 * possibly modified by /proc.  Indicate
4777c478bd9Sstevel@tonic-gate 				 * that the valid copy is in the
4787c478bd9Sstevel@tonic-gate 				 * registers.
4797c478bd9Sstevel@tonic-gate 				 */
4807c478bd9Sstevel@tonic-gate 				lwp->lwp_argsaved = 0;
4817c478bd9Sstevel@tonic-gate 				lwp->lwp_ap = (long *)&rp->r_o0;
4827c478bd9Sstevel@tonic-gate 			}
4837c478bd9Sstevel@tonic-gate 			mutex_exit(&p->p_lock);
4847c478bd9Sstevel@tonic-gate 		}
4857c478bd9Sstevel@tonic-gate 		repost = 1;
4867c478bd9Sstevel@tonic-gate 	}
4877c478bd9Sstevel@tonic-gate 
4887c478bd9Sstevel@tonic-gate 	if (lwp->lwp_sysabort) {
4897c478bd9Sstevel@tonic-gate 		/*
4907c478bd9Sstevel@tonic-gate 		 * lwp_sysabort may have been set via /proc while the process
4917c478bd9Sstevel@tonic-gate 		 * was stopped on PR_SYSENTRY.  If so, abort the system call.
4927c478bd9Sstevel@tonic-gate 		 * Override any error from the copyin() of the arguments.
4937c478bd9Sstevel@tonic-gate 		 */
4947c478bd9Sstevel@tonic-gate 		lwp->lwp_sysabort = 0;
4957c478bd9Sstevel@tonic-gate 		(void) set_errno(EINTR); /* sets post-sys processing */
4967c478bd9Sstevel@tonic-gate 		t->t_pre_sys = 1;	/* repost anyway */
4977c478bd9Sstevel@tonic-gate 		return (1);		/* don't do system call, return EINTR */
4987c478bd9Sstevel@tonic-gate 	}
4997c478bd9Sstevel@tonic-gate 
5007c478bd9Sstevel@tonic-gate #ifdef C2_AUDIT
5017c478bd9Sstevel@tonic-gate 	if (audit_active) {	/* begin auditing for this syscall */
5027c478bd9Sstevel@tonic-gate 		int error;
5037c478bd9Sstevel@tonic-gate 		if (error = audit_start(T_SYSCALL, code, 0, lwp)) {
5047c478bd9Sstevel@tonic-gate 			t->t_pre_sys = 1;	/* repost anyway */
5057c478bd9Sstevel@tonic-gate 			lwp->lwp_error = 0;	/* for old drivers */
5067c478bd9Sstevel@tonic-gate 			return (error);
5077c478bd9Sstevel@tonic-gate 		}
5087c478bd9Sstevel@tonic-gate 		repost = 1;
5097c478bd9Sstevel@tonic-gate 	}
5107c478bd9Sstevel@tonic-gate #endif /* C2_AUDIT */
5117c478bd9Sstevel@tonic-gate 
5127c478bd9Sstevel@tonic-gate #ifndef NPROBE
5137c478bd9Sstevel@tonic-gate 	/* Kernel probe */
5147c478bd9Sstevel@tonic-gate 	if (tnf_tracing_active) {
5157c478bd9Sstevel@tonic-gate 		TNF_PROBE_1(syscall_start, "syscall thread", /* CSTYLED */,
5167c478bd9Sstevel@tonic-gate 			tnf_sysnum,	sysnum,		t->t_sysnum);
5177c478bd9Sstevel@tonic-gate 		t->t_post_sys = 1;	/* make sure post_syscall runs */
5187c478bd9Sstevel@tonic-gate 		repost = 1;
5197c478bd9Sstevel@tonic-gate 	}
5207c478bd9Sstevel@tonic-gate #endif /* NPROBE */
5217c478bd9Sstevel@tonic-gate 
5227c478bd9Sstevel@tonic-gate #ifdef SYSCALLTRACE
5237c478bd9Sstevel@tonic-gate 	if (syscalltrace) {
5247c478bd9Sstevel@tonic-gate 		int i;
5257c478bd9Sstevel@tonic-gate 		long *ap;
5267c478bd9Sstevel@tonic-gate 		char *cp;
5277c478bd9Sstevel@tonic-gate 		char *sysname;
5287c478bd9Sstevel@tonic-gate 		struct sysent *callp;
5297c478bd9Sstevel@tonic-gate 
5307c478bd9Sstevel@tonic-gate 		if (code >= NSYSCALL)
5317c478bd9Sstevel@tonic-gate 			callp = &nosys_ent;	/* nosys has no args */
5327c478bd9Sstevel@tonic-gate 		else
5337c478bd9Sstevel@tonic-gate 			callp = LWP_GETSYSENT(lwp) + code;
5347c478bd9Sstevel@tonic-gate 		(void) save_syscall_args();
5357c478bd9Sstevel@tonic-gate 		mutex_enter(&systrace_lock);
5367c478bd9Sstevel@tonic-gate 		printf("%d: ", p->p_pid);
5377c478bd9Sstevel@tonic-gate 		if (code >= NSYSCALL)
5387c478bd9Sstevel@tonic-gate 			printf("0x%x", code);
5397c478bd9Sstevel@tonic-gate 		else {
5407c478bd9Sstevel@tonic-gate 			sysname = mod_getsysname(code);
5417c478bd9Sstevel@tonic-gate 			printf("%s[0x%x]", sysname == NULL ? "NULL" :
5427c478bd9Sstevel@tonic-gate 			    sysname, code);
5437c478bd9Sstevel@tonic-gate 		}
5447c478bd9Sstevel@tonic-gate 		cp = "(";
5457c478bd9Sstevel@tonic-gate 		for (i = 0, ap = lwp->lwp_ap; i < callp->sy_narg; i++, ap++) {
5467c478bd9Sstevel@tonic-gate 			printf("%s%lx", cp, *ap);
5477c478bd9Sstevel@tonic-gate 			cp = ", ";
5487c478bd9Sstevel@tonic-gate 		}
5497c478bd9Sstevel@tonic-gate 		if (i)
5507c478bd9Sstevel@tonic-gate 			printf(")");
5517c478bd9Sstevel@tonic-gate 		printf(" %s id=0x%p\n", PTOU(p)->u_comm, curthread);
5527c478bd9Sstevel@tonic-gate 		mutex_exit(&systrace_lock);
5537c478bd9Sstevel@tonic-gate 	}
5547c478bd9Sstevel@tonic-gate #endif /* SYSCALLTRACE */
5557c478bd9Sstevel@tonic-gate 
5567c478bd9Sstevel@tonic-gate 	/*
5577c478bd9Sstevel@tonic-gate 	 * If there was a continuing reason for pre-syscall processing,
5587c478bd9Sstevel@tonic-gate 	 * set the t_pre_sys flag for the next system call.
5597c478bd9Sstevel@tonic-gate 	 */
5607c478bd9Sstevel@tonic-gate 	if (repost)
5617c478bd9Sstevel@tonic-gate 		t->t_pre_sys = 1;
5627c478bd9Sstevel@tonic-gate 	lwp->lwp_error = 0;	/* for old drivers */
5637c478bd9Sstevel@tonic-gate 	lwp->lwp_badpriv = PRIV_NONE;	/* for privilege tracing */
5647c478bd9Sstevel@tonic-gate 	return (0);
5657c478bd9Sstevel@tonic-gate }
5667c478bd9Sstevel@tonic-gate 
5677c478bd9Sstevel@tonic-gate /*
5687c478bd9Sstevel@tonic-gate  * Post-syscall processing.  Perform abnormal system call completion
5697c478bd9Sstevel@tonic-gate  * actions such as /proc tracing, profiling, signals, preemption, etc.
5707c478bd9Sstevel@tonic-gate  *
5717c478bd9Sstevel@tonic-gate  * This routine is called only if t_post_sys, t_sig_check, or t_astflag is set.
5727c478bd9Sstevel@tonic-gate  * Any condition requiring pre-syscall handling must set one of these.
5737c478bd9Sstevel@tonic-gate  * If the condition is persistent, this routine will repost t_post_sys.
5747c478bd9Sstevel@tonic-gate  */
5757c478bd9Sstevel@tonic-gate void
5767c478bd9Sstevel@tonic-gate post_syscall(long rval1, long rval2)
5777c478bd9Sstevel@tonic-gate {
5787c478bd9Sstevel@tonic-gate 	kthread_t	*t = curthread;
5797c478bd9Sstevel@tonic-gate 	proc_t	*p = curproc;
5807c478bd9Sstevel@tonic-gate 	klwp_t	*lwp = ttolwp(t);
5817c478bd9Sstevel@tonic-gate 	struct regs *rp = lwptoregs(lwp);
5827c478bd9Sstevel@tonic-gate 	uint_t	error;
5837c478bd9Sstevel@tonic-gate 	int	code = t->t_sysnum;
5847c478bd9Sstevel@tonic-gate 	int	repost = 0;
5857c478bd9Sstevel@tonic-gate 	int	proc_stop = 0;		/* non-zero if stopping for /proc */
5867c478bd9Sstevel@tonic-gate 	int	sigprof = 0;		/* non-zero if sending SIGPROF */
5877c478bd9Sstevel@tonic-gate 
5887c478bd9Sstevel@tonic-gate 	t->t_post_sys = 0;
5897c478bd9Sstevel@tonic-gate 
5907c478bd9Sstevel@tonic-gate 	error = lwp->lwp_errno;
5917c478bd9Sstevel@tonic-gate 
5927c478bd9Sstevel@tonic-gate 	/*
5937c478bd9Sstevel@tonic-gate 	 * Code can be zero if this is a new LWP returning after a forkall(),
5947c478bd9Sstevel@tonic-gate 	 * other than the one which matches the one in the parent which called
5957c478bd9Sstevel@tonic-gate 	 * forkall().  In these LWPs, skip most of post-syscall activity.
5967c478bd9Sstevel@tonic-gate 	 */
5977c478bd9Sstevel@tonic-gate 	if (code == 0)
5987c478bd9Sstevel@tonic-gate 		goto sig_check;
5997c478bd9Sstevel@tonic-gate 
6007c478bd9Sstevel@tonic-gate #ifdef C2_AUDIT
6017c478bd9Sstevel@tonic-gate 	if (audit_active) {	/* put out audit record for this syscall */
6027c478bd9Sstevel@tonic-gate 		rval_t	rval;	/* fix audit_finish() someday */
6037c478bd9Sstevel@tonic-gate 
6047c478bd9Sstevel@tonic-gate 		/* XX64 -- truncation of 64-bit return values? */
6057c478bd9Sstevel@tonic-gate 		rval.r_val1 = (int)rval1;
6067c478bd9Sstevel@tonic-gate 		rval.r_val2 = (int)rval2;
6077c478bd9Sstevel@tonic-gate 		audit_finish(T_SYSCALL, code, error, &rval);
6087c478bd9Sstevel@tonic-gate 		repost = 1;
6097c478bd9Sstevel@tonic-gate 	}
6107c478bd9Sstevel@tonic-gate #endif /* C2_AUDIT */
6117c478bd9Sstevel@tonic-gate 
6127c478bd9Sstevel@tonic-gate 	if (curthread->t_pdmsg != NULL) {
6137c478bd9Sstevel@tonic-gate 		char *m = curthread->t_pdmsg;
6147c478bd9Sstevel@tonic-gate 
6157c478bd9Sstevel@tonic-gate 		uprintf("%s", m);
6167c478bd9Sstevel@tonic-gate 		kmem_free(m, strlen(m) + 1);
6177c478bd9Sstevel@tonic-gate 		curthread->t_pdmsg = NULL;
6187c478bd9Sstevel@tonic-gate 	}
6197c478bd9Sstevel@tonic-gate 
6207c478bd9Sstevel@tonic-gate 	/*
6217c478bd9Sstevel@tonic-gate 	 * If we're going to stop for /proc tracing, set the flag and
6227c478bd9Sstevel@tonic-gate 	 * save the arguments so that the return values don't smash them.
6237c478bd9Sstevel@tonic-gate 	 */
6247c478bd9Sstevel@tonic-gate 	if (PTOU(p)->u_systrap) {
6257c478bd9Sstevel@tonic-gate 		if (prismember(&PTOU(p)->u_exitmask, code)) {
6267c478bd9Sstevel@tonic-gate 			proc_stop = 1;
6277c478bd9Sstevel@tonic-gate 			(void) save_syscall_args();
6287c478bd9Sstevel@tonic-gate 		}
6297c478bd9Sstevel@tonic-gate 		repost = 1;
6307c478bd9Sstevel@tonic-gate 	}
6317c478bd9Sstevel@tonic-gate 
6327c478bd9Sstevel@tonic-gate 	/*
6337c478bd9Sstevel@tonic-gate 	 * Similarly check to see if SIGPROF might be sent.
6347c478bd9Sstevel@tonic-gate 	 */
6357c478bd9Sstevel@tonic-gate 	if (curthread->t_rprof != NULL &&
6367c478bd9Sstevel@tonic-gate 	    curthread->t_rprof->rp_anystate != 0) {
6377c478bd9Sstevel@tonic-gate 		(void) save_syscall_args();
6387c478bd9Sstevel@tonic-gate 		sigprof = 1;
6397c478bd9Sstevel@tonic-gate 	}
6407c478bd9Sstevel@tonic-gate 
6417c478bd9Sstevel@tonic-gate 	if (lwp->lwp_eosys == NORMALRETURN) {
6427c478bd9Sstevel@tonic-gate 		if (error == 0) {
6437c478bd9Sstevel@tonic-gate #ifdef SYSCALLTRACE
6447c478bd9Sstevel@tonic-gate 			if (syscalltrace) {
6457c478bd9Sstevel@tonic-gate 				mutex_enter(&systrace_lock);
6467c478bd9Sstevel@tonic-gate 				printf(
6477c478bd9Sstevel@tonic-gate 				    "%d: r_val1=0x%lx, r_val2=0x%lx, id 0x%p\n",
6487c478bd9Sstevel@tonic-gate 				    p->p_pid, rval1, rval2, curthread);
6497c478bd9Sstevel@tonic-gate 				mutex_exit(&systrace_lock);
6507c478bd9Sstevel@tonic-gate 			}
6517c478bd9Sstevel@tonic-gate #endif /* SYSCALLTRACE */
6527c478bd9Sstevel@tonic-gate 			rp->r_tstate &= ~TSTATE_IC;
6537c478bd9Sstevel@tonic-gate 			rp->r_o0 = rval1;
6547c478bd9Sstevel@tonic-gate 			rp->r_o1 = rval2;
6557c478bd9Sstevel@tonic-gate 		} else {
6567c478bd9Sstevel@tonic-gate 			int sig;
6577c478bd9Sstevel@tonic-gate 
6587c478bd9Sstevel@tonic-gate #ifdef SYSCALLTRACE
6597c478bd9Sstevel@tonic-gate 			if (syscalltrace) {
6607c478bd9Sstevel@tonic-gate 				mutex_enter(&systrace_lock);
6617c478bd9Sstevel@tonic-gate 				printf("%d: error=%d, id 0x%p\n",
6627c478bd9Sstevel@tonic-gate 				    p->p_pid, error, curthread);
6637c478bd9Sstevel@tonic-gate 				mutex_exit(&systrace_lock);
6647c478bd9Sstevel@tonic-gate 			}
6657c478bd9Sstevel@tonic-gate #endif /* SYSCALLTRACE */
6667c478bd9Sstevel@tonic-gate 			if (error == EINTR && t->t_activefd.a_stale)
6677c478bd9Sstevel@tonic-gate 				error = EBADF;
6687c478bd9Sstevel@tonic-gate 			if (error == EINTR &&
6697c478bd9Sstevel@tonic-gate 			    (sig = lwp->lwp_cursig) != 0 &&
6707c478bd9Sstevel@tonic-gate 			    sigismember(&PTOU(p)->u_sigrestart, sig) &&
6717c478bd9Sstevel@tonic-gate 			    PTOU(p)->u_signal[sig - 1] != SIG_DFL &&
6727c478bd9Sstevel@tonic-gate 			    PTOU(p)->u_signal[sig - 1] != SIG_IGN)
6737c478bd9Sstevel@tonic-gate 				error = ERESTART;
6747c478bd9Sstevel@tonic-gate 			rp->r_o0 = error;
6757c478bd9Sstevel@tonic-gate 			rp->r_tstate |= TSTATE_IC;
6767c478bd9Sstevel@tonic-gate 		}
6777c478bd9Sstevel@tonic-gate 		/*
6787c478bd9Sstevel@tonic-gate 		 * The default action is to redo the trap instruction.
6797c478bd9Sstevel@tonic-gate 		 * We increment the pc and npc past it for NORMALRETURN.
6807c478bd9Sstevel@tonic-gate 		 * JUSTRETURN has set up a new pc and npc already.
681c4978b50Sraf 		 * If we are a cloned thread of forkall(), don't
682c4978b50Sraf 		 * adjust here because we have already inherited
683c4978b50Sraf 		 * the adjusted values from our clone.
6847c478bd9Sstevel@tonic-gate 		 */
685c4978b50Sraf 		if (!(t->t_flag & T_FORKALL)) {
6867c478bd9Sstevel@tonic-gate 			rp->r_pc = rp->r_npc;
6877c478bd9Sstevel@tonic-gate 			rp->r_npc += 4;
6887c478bd9Sstevel@tonic-gate 		}
689c4978b50Sraf 	}
6907c478bd9Sstevel@tonic-gate 
6917c478bd9Sstevel@tonic-gate 	/*
6927c478bd9Sstevel@tonic-gate 	 * From the proc(4) manual page:
6937c478bd9Sstevel@tonic-gate 	 * When exit from a system call is being traced, the traced process
6947c478bd9Sstevel@tonic-gate 	 * stops on completion of the system call just prior to checking for
6957c478bd9Sstevel@tonic-gate 	 * signals and returning to user level.  At this point all return
6967c478bd9Sstevel@tonic-gate 	 * values have been stored into the traced process's saved registers.
6977c478bd9Sstevel@tonic-gate 	 */
6987c478bd9Sstevel@tonic-gate 	if (proc_stop) {
6997c478bd9Sstevel@tonic-gate 		mutex_enter(&p->p_lock);
7007c478bd9Sstevel@tonic-gate 		if (PTOU(p)->u_systrap &&
7017c478bd9Sstevel@tonic-gate 		    prismember(&PTOU(p)->u_exitmask, code))
7027c478bd9Sstevel@tonic-gate 			stop(PR_SYSEXIT, code);
7037c478bd9Sstevel@tonic-gate 		mutex_exit(&p->p_lock);
7047c478bd9Sstevel@tonic-gate 	}
7057c478bd9Sstevel@tonic-gate 
7067c478bd9Sstevel@tonic-gate 	/*
7077c478bd9Sstevel@tonic-gate 	 * If we are the parent returning from a successful
7087c478bd9Sstevel@tonic-gate 	 * vfork, wait for the child to exec or exit.
7097c478bd9Sstevel@tonic-gate 	 * This code must be here and not in the bowels of the system
7107c478bd9Sstevel@tonic-gate 	 * so that /proc can intercept exit from vfork in a timely way.
7117c478bd9Sstevel@tonic-gate 	 */
7127c478bd9Sstevel@tonic-gate 	if (code == SYS_vfork && rp->r_o1 == 0 && error == 0)
7137c478bd9Sstevel@tonic-gate 		vfwait((pid_t)rval1);
7147c478bd9Sstevel@tonic-gate 
7157c478bd9Sstevel@tonic-gate 	/*
7167c478bd9Sstevel@tonic-gate 	 * If profiling is active, bill the current PC in user-land
7177c478bd9Sstevel@tonic-gate 	 * and keep reposting until profiling is disabled.
7187c478bd9Sstevel@tonic-gate 	 */
7197c478bd9Sstevel@tonic-gate 	if (p->p_prof.pr_scale) {
7207c478bd9Sstevel@tonic-gate 		if (lwp->lwp_oweupc)
7217c478bd9Sstevel@tonic-gate 			profil_tick(rp->r_pc);
7227c478bd9Sstevel@tonic-gate 		repost = 1;
7237c478bd9Sstevel@tonic-gate 	}
7247c478bd9Sstevel@tonic-gate 
7257c478bd9Sstevel@tonic-gate sig_check:
7267c478bd9Sstevel@tonic-gate 	/*
7277c478bd9Sstevel@tonic-gate 	 * Reset flag for next time.
7287c478bd9Sstevel@tonic-gate 	 * We must do this after stopping on PR_SYSEXIT
7297c478bd9Sstevel@tonic-gate 	 * because /proc uses the information in lwp_eosys.
7307c478bd9Sstevel@tonic-gate 	 */
7317c478bd9Sstevel@tonic-gate 	lwp->lwp_eosys = NORMALRETURN;
7327c478bd9Sstevel@tonic-gate 	clear_stale_fd();
733c4978b50Sraf 	t->t_flag &= ~T_FORKALL;
7347c478bd9Sstevel@tonic-gate 
7357c478bd9Sstevel@tonic-gate 	if (t->t_astflag | t->t_sig_check) {
7367c478bd9Sstevel@tonic-gate 		/*
7377c478bd9Sstevel@tonic-gate 		 * Turn off the AST flag before checking all the conditions that
7387c478bd9Sstevel@tonic-gate 		 * may have caused an AST.  This flag is on whenever a signal or
7397c478bd9Sstevel@tonic-gate 		 * unusual condition should be handled after the next trap or
7407c478bd9Sstevel@tonic-gate 		 * syscall.
7417c478bd9Sstevel@tonic-gate 		 */
7427c478bd9Sstevel@tonic-gate 		astoff(t);
7437c478bd9Sstevel@tonic-gate 		t->t_sig_check = 0;
7447c478bd9Sstevel@tonic-gate 
7457c478bd9Sstevel@tonic-gate 		mutex_enter(&p->p_lock);
7467c478bd9Sstevel@tonic-gate 		if (curthread->t_proc_flag & TP_CHANGEBIND) {
7477c478bd9Sstevel@tonic-gate 			timer_lwpbind();
7487c478bd9Sstevel@tonic-gate 			curthread->t_proc_flag &= ~TP_CHANGEBIND;
7497c478bd9Sstevel@tonic-gate 		}
7507c478bd9Sstevel@tonic-gate 		mutex_exit(&p->p_lock);
7517c478bd9Sstevel@tonic-gate 
7527c478bd9Sstevel@tonic-gate 		/*
7537c478bd9Sstevel@tonic-gate 		 * for kaio requests on the special kaio poll queue,
7547c478bd9Sstevel@tonic-gate 		 * copyout their results to user memory.
7557c478bd9Sstevel@tonic-gate 		 */
7567c478bd9Sstevel@tonic-gate 		if (p->p_aio)
7577c478bd9Sstevel@tonic-gate 			aio_cleanup(0);
7587c478bd9Sstevel@tonic-gate 
7597c478bd9Sstevel@tonic-gate 		/*
7607c478bd9Sstevel@tonic-gate 		 * If this LWP was asked to hold, call holdlwp(), which will
7617c478bd9Sstevel@tonic-gate 		 * stop.  holdlwps() sets this up and calls pokelwps() which
7627c478bd9Sstevel@tonic-gate 		 * sets the AST flag.
7637c478bd9Sstevel@tonic-gate 		 *
7647c478bd9Sstevel@tonic-gate 		 * Also check TP_EXITLWP, since this is used by fresh new LWPs
7657c478bd9Sstevel@tonic-gate 		 * through lwp_rtt().  That flag is set if the lwp_create(2)
7667c478bd9Sstevel@tonic-gate 		 * syscall failed after creating the LWP.
7677c478bd9Sstevel@tonic-gate 		 */
7687c478bd9Sstevel@tonic-gate 		if (ISHOLD(p) || (t->t_proc_flag & TP_EXITLWP))
7697c478bd9Sstevel@tonic-gate 			holdlwp();
7707c478bd9Sstevel@tonic-gate 
7717c478bd9Sstevel@tonic-gate 		/*
7727c478bd9Sstevel@tonic-gate 		 * All code that sets signals and makes ISSIG_PENDING
7737c478bd9Sstevel@tonic-gate 		 * evaluate true must set t_sig_check afterwards.
7747c478bd9Sstevel@tonic-gate 		 */
7757c478bd9Sstevel@tonic-gate 		if (ISSIG_PENDING(t, lwp, p)) {
7767c478bd9Sstevel@tonic-gate 			if (issig(FORREAL))
7777c478bd9Sstevel@tonic-gate 				psig();
7787c478bd9Sstevel@tonic-gate 			t->t_sig_check = 1;	/* recheck next time */
7797c478bd9Sstevel@tonic-gate 		}
7807c478bd9Sstevel@tonic-gate 
7817c478bd9Sstevel@tonic-gate 		if (sigprof) {
7827c478bd9Sstevel@tonic-gate 			realsigprof(code, error);
7837c478bd9Sstevel@tonic-gate 			t->t_sig_check = 1;	/* recheck next time */
7847c478bd9Sstevel@tonic-gate 		}
7857c478bd9Sstevel@tonic-gate 
7867c478bd9Sstevel@tonic-gate 		/*
7877c478bd9Sstevel@tonic-gate 		 * If a performance counter overflow interrupt was
7887c478bd9Sstevel@tonic-gate 		 * delivered *during* the syscall, then re-enable the
7897c478bd9Sstevel@tonic-gate 		 * AST so that we take a trip through trap() to cause
7907c478bd9Sstevel@tonic-gate 		 * the SIGEMT to be delivered.
7917c478bd9Sstevel@tonic-gate 		 */
7927c478bd9Sstevel@tonic-gate 		if (lwp->lwp_pcb.pcb_flags & CPC_OVERFLOW)
7937c478bd9Sstevel@tonic-gate 			aston(t);
7947c478bd9Sstevel@tonic-gate 
7957c478bd9Sstevel@tonic-gate 		/*
7967c478bd9Sstevel@tonic-gate 		 * If an asynchronous hardware error is pending, turn AST flag
7977c478bd9Sstevel@tonic-gate 		 * back on.  AST will be checked again before we return to user
7987c478bd9Sstevel@tonic-gate 		 * mode and we'll come back through trap() to handle the error.
7997c478bd9Sstevel@tonic-gate 		 */
8007c478bd9Sstevel@tonic-gate 		if (lwp->lwp_pcb.pcb_flags & ASYNC_HWERR)
8017c478bd9Sstevel@tonic-gate 			aston(t);
8027c478bd9Sstevel@tonic-gate 	}
8037c478bd9Sstevel@tonic-gate 
8047c478bd9Sstevel@tonic-gate 	/*
8057c478bd9Sstevel@tonic-gate 	 * Restore register window if a debugger modified it.
8067c478bd9Sstevel@tonic-gate 	 * Set up to perform a single-step if a debugger requested it.
8077c478bd9Sstevel@tonic-gate 	 */
8087c478bd9Sstevel@tonic-gate 	if (lwp->lwp_pcb.pcb_xregstat != XREGNONE)
8097c478bd9Sstevel@tonic-gate 		xregrestore(lwp, 1);
8107c478bd9Sstevel@tonic-gate 
8117c478bd9Sstevel@tonic-gate 	lwp->lwp_errno = 0;		/* clear error for next time */
8127c478bd9Sstevel@tonic-gate 
8137c478bd9Sstevel@tonic-gate #ifndef NPROBE
8147c478bd9Sstevel@tonic-gate 	/* Kernel probe */
8157c478bd9Sstevel@tonic-gate 	if (tnf_tracing_active) {
8167c478bd9Sstevel@tonic-gate 		TNF_PROBE_3(syscall_end, "syscall thread", /* CSTYLED */,
8177c478bd9Sstevel@tonic-gate 			tnf_long,	rval1,		rval1,
8187c478bd9Sstevel@tonic-gate 			tnf_long,	rval2,		rval2,
8197c478bd9Sstevel@tonic-gate 			tnf_long,	errno,		(long)error);
8207c478bd9Sstevel@tonic-gate 		repost = 1;
8217c478bd9Sstevel@tonic-gate 	}
8227c478bd9Sstevel@tonic-gate #endif /* NPROBE */
8237c478bd9Sstevel@tonic-gate 
8247c478bd9Sstevel@tonic-gate 	/*
8257c478bd9Sstevel@tonic-gate 	 * Set state to LWP_USER here so preempt won't give us a kernel
8267c478bd9Sstevel@tonic-gate 	 * priority if it occurs after this point.  Call CL_TRAPRET() to
8277c478bd9Sstevel@tonic-gate 	 * restore the user-level priority.
8287c478bd9Sstevel@tonic-gate 	 *
8297c478bd9Sstevel@tonic-gate 	 * It is important that no locks (other than spinlocks) be entered
8307c478bd9Sstevel@tonic-gate 	 * after this point before returning to user mode (unless lwp_state
8317c478bd9Sstevel@tonic-gate 	 * is set back to LWP_SYS).
8327c478bd9Sstevel@tonic-gate 	 *
8337c478bd9Sstevel@tonic-gate 	 * Sampled times past this point are charged to the user.
8347c478bd9Sstevel@tonic-gate 	 */
8357c478bd9Sstevel@tonic-gate 	lwp->lwp_state = LWP_USER;
8367c478bd9Sstevel@tonic-gate 
8377c478bd9Sstevel@tonic-gate 	if (t->t_trapret) {
8387c478bd9Sstevel@tonic-gate 		t->t_trapret = 0;
8397c478bd9Sstevel@tonic-gate 		thread_lock(t);
8407c478bd9Sstevel@tonic-gate 		CL_TRAPRET(t);
8417c478bd9Sstevel@tonic-gate 		thread_unlock(t);
8427c478bd9Sstevel@tonic-gate 	}
8437c478bd9Sstevel@tonic-gate 	if (CPU->cpu_runrun)
8447c478bd9Sstevel@tonic-gate 		preempt();
8457c478bd9Sstevel@tonic-gate 
8467c478bd9Sstevel@tonic-gate 	/*
8477c478bd9Sstevel@tonic-gate 	 * t_post_sys will be set if pcb_step is active.
8487c478bd9Sstevel@tonic-gate 	 */
8497c478bd9Sstevel@tonic-gate 	if (lwp->lwp_pcb.pcb_step != STEP_NONE) {
8507c478bd9Sstevel@tonic-gate 		prdostep();
8517c478bd9Sstevel@tonic-gate 		repost = 1;
8527c478bd9Sstevel@tonic-gate 	}
8537c478bd9Sstevel@tonic-gate 
854169ea130Srab 	t->t_sysnum = 0;	/* no longer in a system call */
855169ea130Srab 
8567c478bd9Sstevel@tonic-gate 	/*
8577c478bd9Sstevel@tonic-gate 	 * In case the args were copied to the lwp, reset the
8587c478bd9Sstevel@tonic-gate 	 * pointer so the next syscall will have the right lwp_ap pointer.
8597c478bd9Sstevel@tonic-gate 	 */
8607c478bd9Sstevel@tonic-gate 	lwp->lwp_ap = (long *)&rp->r_o0;
8617c478bd9Sstevel@tonic-gate 	lwp->lwp_argsaved = 0;
8627c478bd9Sstevel@tonic-gate 
8637c478bd9Sstevel@tonic-gate 	/*
8647c478bd9Sstevel@tonic-gate 	 * If there was a continuing reason for post-syscall processing,
8657c478bd9Sstevel@tonic-gate 	 * set the t_post_sys flag for the next system call.
8667c478bd9Sstevel@tonic-gate 	 */
8677c478bd9Sstevel@tonic-gate 	if (repost)
8687c478bd9Sstevel@tonic-gate 		t->t_post_sys = 1;
8697c478bd9Sstevel@tonic-gate 
8707c478bd9Sstevel@tonic-gate 	/*
8717c478bd9Sstevel@tonic-gate 	 * If there is a ustack registered for this lwp, and the stack rlimit
8727c478bd9Sstevel@tonic-gate 	 * has been altered, read in the ustack. If the saved stack rlimit
8737c478bd9Sstevel@tonic-gate 	 * matches the bounds of the ustack, update the ustack to reflect
8747c478bd9Sstevel@tonic-gate 	 * the new rlimit. If the new stack rlimit is RLIM_INFINITY, disable
8757c478bd9Sstevel@tonic-gate 	 * stack checking by setting the size to 0.
8767c478bd9Sstevel@tonic-gate 	 */
8777c478bd9Sstevel@tonic-gate 	if (lwp->lwp_ustack != 0 && lwp->lwp_old_stk_ctl != 0) {
8787c478bd9Sstevel@tonic-gate 		rlim64_t new_size;
8797c478bd9Sstevel@tonic-gate 		model_t model;
8807c478bd9Sstevel@tonic-gate 		caddr_t top;
8817c478bd9Sstevel@tonic-gate 		struct rlimit64 rl;
8827c478bd9Sstevel@tonic-gate 
8837c478bd9Sstevel@tonic-gate 		mutex_enter(&p->p_lock);
8847c478bd9Sstevel@tonic-gate 		new_size = p->p_stk_ctl;
8857c478bd9Sstevel@tonic-gate 		model = p->p_model;
8867c478bd9Sstevel@tonic-gate 		top = p->p_usrstack;
8877c478bd9Sstevel@tonic-gate 		(void) rctl_rlimit_get(rctlproc_legacy[RLIMIT_STACK], p, &rl);
8887c478bd9Sstevel@tonic-gate 		mutex_exit(&p->p_lock);
8897c478bd9Sstevel@tonic-gate 
8907c478bd9Sstevel@tonic-gate 		if (rl.rlim_cur == RLIM64_INFINITY)
8917c478bd9Sstevel@tonic-gate 			new_size = 0;
8927c478bd9Sstevel@tonic-gate 
8937c478bd9Sstevel@tonic-gate 		if (model == DATAMODEL_NATIVE) {
8947c478bd9Sstevel@tonic-gate 			stack_t stk;
8957c478bd9Sstevel@tonic-gate 
8967c478bd9Sstevel@tonic-gate 			if (copyin((stack_t *)lwp->lwp_ustack, &stk,
8977c478bd9Sstevel@tonic-gate 			    sizeof (stack_t)) == 0 &&
8987c478bd9Sstevel@tonic-gate 			    (stk.ss_size == lwp->lwp_old_stk_ctl ||
8997c478bd9Sstevel@tonic-gate 				stk.ss_size == 0) &&
9007c478bd9Sstevel@tonic-gate 			    stk.ss_sp == top - stk.ss_size) {
9017c478bd9Sstevel@tonic-gate 				stk.ss_sp = (void *)((uintptr_t)stk.ss_sp +
9027c478bd9Sstevel@tonic-gate 				    stk.ss_size - new_size);
9037c478bd9Sstevel@tonic-gate 				stk.ss_size = new_size;
9047c478bd9Sstevel@tonic-gate 
9057c478bd9Sstevel@tonic-gate 				(void) copyout(&stk,
9067c478bd9Sstevel@tonic-gate 				    (stack_t *)lwp->lwp_ustack,
9077c478bd9Sstevel@tonic-gate 				    sizeof (stack_t));
9087c478bd9Sstevel@tonic-gate 			}
9097c478bd9Sstevel@tonic-gate 		} else {
9107c478bd9Sstevel@tonic-gate 			stack32_t stk32;
9117c478bd9Sstevel@tonic-gate 
9127c478bd9Sstevel@tonic-gate 			if (copyin((stack32_t *)lwp->lwp_ustack, &stk32,
9137c478bd9Sstevel@tonic-gate 			    sizeof (stack32_t)) == 0 &&
9147c478bd9Sstevel@tonic-gate 			    (stk32.ss_size == lwp->lwp_old_stk_ctl ||
9157c478bd9Sstevel@tonic-gate 			    stk32.ss_size == 0) &&
916*75521904Sraf 			    stk32.ss_sp ==
917*75521904Sraf 			    (caddr32_t)(uintptr_t)(top - stk32.ss_size)) {
9187c478bd9Sstevel@tonic-gate 				stk32.ss_sp += stk32.ss_size - new_size;
9197c478bd9Sstevel@tonic-gate 				stk32.ss_size = new_size;
9207c478bd9Sstevel@tonic-gate 
9217c478bd9Sstevel@tonic-gate 				(void) copyout(&stk32,
9227c478bd9Sstevel@tonic-gate 				    (stack32_t *)lwp->lwp_ustack,
9237c478bd9Sstevel@tonic-gate 				    sizeof (stack32_t));
9247c478bd9Sstevel@tonic-gate 			}
9257c478bd9Sstevel@tonic-gate 		}
9267c478bd9Sstevel@tonic-gate 
9277c478bd9Sstevel@tonic-gate 		lwp->lwp_old_stk_ctl = 0;
9287c478bd9Sstevel@tonic-gate 	}
9297c478bd9Sstevel@tonic-gate 
9307c478bd9Sstevel@tonic-gate 	syscall_mstate(LMS_SYSTEM, LMS_USER);
9317c478bd9Sstevel@tonic-gate }
9327c478bd9Sstevel@tonic-gate 
9337c478bd9Sstevel@tonic-gate /*
9347c478bd9Sstevel@tonic-gate  * Call a system call which takes a pointer to the user args struct and
9357c478bd9Sstevel@tonic-gate  * a pointer to the return values.  This is a bit slower than the standard
9367c478bd9Sstevel@tonic-gate  * C arg-passing method in some cases.
9377c478bd9Sstevel@tonic-gate  */
9387c478bd9Sstevel@tonic-gate int64_t
9397c478bd9Sstevel@tonic-gate syscall_ap()
9407c478bd9Sstevel@tonic-gate {
9417c478bd9Sstevel@tonic-gate 	uint_t	error;
9427c478bd9Sstevel@tonic-gate 	struct sysent *callp;
9437c478bd9Sstevel@tonic-gate 	rval_t	rval;
9447c478bd9Sstevel@tonic-gate 	klwp_t	*lwp = ttolwp(curthread);
9457c478bd9Sstevel@tonic-gate 	struct regs *rp = lwptoregs(lwp);
9467c478bd9Sstevel@tonic-gate 
9477c478bd9Sstevel@tonic-gate 	callp = LWP_GETSYSENT(lwp) + curthread->t_sysnum;
9487c478bd9Sstevel@tonic-gate 
9497c478bd9Sstevel@tonic-gate 	/*
9507c478bd9Sstevel@tonic-gate 	 * If the arguments don't fit in registers %o0 - o5, make sure they
9517c478bd9Sstevel@tonic-gate 	 * have been copied to the lwp_arg array.
9527c478bd9Sstevel@tonic-gate 	 */
9537c478bd9Sstevel@tonic-gate 	if (callp->sy_narg > 6 && save_syscall_args())
9547c478bd9Sstevel@tonic-gate 		return ((int64_t)set_errno(EFAULT));
9557c478bd9Sstevel@tonic-gate 
9567c478bd9Sstevel@tonic-gate 	rval.r_val1 = 0;
9577c478bd9Sstevel@tonic-gate 	rval.r_val2 = (int)rp->r_o1;
9587c478bd9Sstevel@tonic-gate 	lwp->lwp_error = 0;	/* for old drivers */
9597c478bd9Sstevel@tonic-gate 	error = (*(callp->sy_call))(lwp->lwp_ap, &rval);
9607c478bd9Sstevel@tonic-gate 	if (error)
9617c478bd9Sstevel@tonic-gate 		return ((int64_t)set_errno(error));
9627c478bd9Sstevel@tonic-gate 	return (rval.r_vals);
9637c478bd9Sstevel@tonic-gate }
9647c478bd9Sstevel@tonic-gate 
9657c478bd9Sstevel@tonic-gate /*
9667c478bd9Sstevel@tonic-gate  * Load system call module.
9677c478bd9Sstevel@tonic-gate  *	Returns with pointer to held read lock for module.
9687c478bd9Sstevel@tonic-gate  */
9697c478bd9Sstevel@tonic-gate static krwlock_t *
9707c478bd9Sstevel@tonic-gate lock_syscall(struct sysent *table, uint_t code)
9717c478bd9Sstevel@tonic-gate {
9727c478bd9Sstevel@tonic-gate 	krwlock_t	*module_lock;
9737c478bd9Sstevel@tonic-gate 	struct modctl	*modp;
9747c478bd9Sstevel@tonic-gate 	int		id;
9757c478bd9Sstevel@tonic-gate 	struct sysent   *callp;
9767c478bd9Sstevel@tonic-gate 
9777c478bd9Sstevel@tonic-gate 	module_lock = table[code].sy_lock;
9787c478bd9Sstevel@tonic-gate 	callp = &table[code];
9797c478bd9Sstevel@tonic-gate 
9807c478bd9Sstevel@tonic-gate 	/*
9817c478bd9Sstevel@tonic-gate 	 * Optimization to only call modload if we don't have a loaded
9827c478bd9Sstevel@tonic-gate 	 * syscall.
9837c478bd9Sstevel@tonic-gate 	 */
9847c478bd9Sstevel@tonic-gate 	rw_enter(module_lock, RW_READER);
9857c478bd9Sstevel@tonic-gate 	if (LOADED_SYSCALL(callp))
9867c478bd9Sstevel@tonic-gate 		return (module_lock);
9877c478bd9Sstevel@tonic-gate 	rw_exit(module_lock);
9887c478bd9Sstevel@tonic-gate 
9897c478bd9Sstevel@tonic-gate 	for (;;) {
9907c478bd9Sstevel@tonic-gate 		if ((id = modload("sys", syscallnames[code])) == -1)
9917c478bd9Sstevel@tonic-gate 			break;
9927c478bd9Sstevel@tonic-gate 
9937c478bd9Sstevel@tonic-gate 		/*
9947c478bd9Sstevel@tonic-gate 		 * If we loaded successfully at least once, the modctl
9957c478bd9Sstevel@tonic-gate 		 * will still be valid, so we try to grab it by filename.
9967c478bd9Sstevel@tonic-gate 		 * If this call fails, it's because the mod_filename
9977c478bd9Sstevel@tonic-gate 		 * was changed after the call to modload() (mod_hold_by_name()
9987c478bd9Sstevel@tonic-gate 		 * is the likely culprit).  We can safely just take
9997c478bd9Sstevel@tonic-gate 		 * another lap if this is the case;  the modload() will
10007c478bd9Sstevel@tonic-gate 		 * change the mod_filename back to one by which we can
10017c478bd9Sstevel@tonic-gate 		 * find the modctl.
10027c478bd9Sstevel@tonic-gate 		 */
10037c478bd9Sstevel@tonic-gate 		modp = mod_find_by_filename("sys", syscallnames[code]);
10047c478bd9Sstevel@tonic-gate 
10057c478bd9Sstevel@tonic-gate 		if (modp == NULL)
10067c478bd9Sstevel@tonic-gate 			continue;
10077c478bd9Sstevel@tonic-gate 
10087c478bd9Sstevel@tonic-gate 		mutex_enter(&mod_lock);
10097c478bd9Sstevel@tonic-gate 
10107c478bd9Sstevel@tonic-gate 		if (!modp->mod_installed) {
10117c478bd9Sstevel@tonic-gate 			mutex_exit(&mod_lock);
10127c478bd9Sstevel@tonic-gate 			continue;
10137c478bd9Sstevel@tonic-gate 		}
10147c478bd9Sstevel@tonic-gate 		break;
10157c478bd9Sstevel@tonic-gate 	}
10167c478bd9Sstevel@tonic-gate 
10177c478bd9Sstevel@tonic-gate 	rw_enter(module_lock, RW_READER);
10187c478bd9Sstevel@tonic-gate 
10197c478bd9Sstevel@tonic-gate 	if (id != -1)
10207c478bd9Sstevel@tonic-gate 		mutex_exit(&mod_lock);
10217c478bd9Sstevel@tonic-gate 
10227c478bd9Sstevel@tonic-gate 	return (module_lock);
10237c478bd9Sstevel@tonic-gate }
10247c478bd9Sstevel@tonic-gate 
10257c478bd9Sstevel@tonic-gate /*
10267c478bd9Sstevel@tonic-gate  * Loadable syscall support.
10277c478bd9Sstevel@tonic-gate  *	If needed, load the module, then reserve it by holding a read
10287c478bd9Sstevel@tonic-gate  * 	lock for the duration of the call.
10297c478bd9Sstevel@tonic-gate  *	Later, if the syscall is not unloadable, it could patch the vector.
10307c478bd9Sstevel@tonic-gate  */
10317c478bd9Sstevel@tonic-gate /*ARGSUSED*/
10327c478bd9Sstevel@tonic-gate int64_t
10337c478bd9Sstevel@tonic-gate loadable_syscall(
10347c478bd9Sstevel@tonic-gate     long a0, long a1, long a2, long a3,
10357c478bd9Sstevel@tonic-gate     long a4, long a5, long a6, long a7)
10367c478bd9Sstevel@tonic-gate {
10377c478bd9Sstevel@tonic-gate 	int64_t		rval;
10387c478bd9Sstevel@tonic-gate 	struct sysent	*callp;
10397c478bd9Sstevel@tonic-gate 	struct sysent	*se = LWP_GETSYSENT(ttolwp(curthread));
10407c478bd9Sstevel@tonic-gate 	krwlock_t	*module_lock;
10417c478bd9Sstevel@tonic-gate 	int		code;
10427c478bd9Sstevel@tonic-gate 
10437c478bd9Sstevel@tonic-gate 	code = curthread->t_sysnum;
10447c478bd9Sstevel@tonic-gate 	callp = se + code;
10457c478bd9Sstevel@tonic-gate 
10467c478bd9Sstevel@tonic-gate 	/*
10477c478bd9Sstevel@tonic-gate 	 * Try to autoload the system call if necessary.
10487c478bd9Sstevel@tonic-gate 	 */
10497c478bd9Sstevel@tonic-gate 	module_lock = lock_syscall(se, code);
10507c478bd9Sstevel@tonic-gate 	THREAD_KPRI_RELEASE();	/* drop priority given by rw_enter */
10517c478bd9Sstevel@tonic-gate 
10527c478bd9Sstevel@tonic-gate 	/*
10537c478bd9Sstevel@tonic-gate 	 * we've locked either the loaded syscall or nosys
10547c478bd9Sstevel@tonic-gate 	 */
10557c478bd9Sstevel@tonic-gate 	if (callp->sy_flags & SE_ARGC) {
10567c478bd9Sstevel@tonic-gate 		int64_t (*sy_call)();
10577c478bd9Sstevel@tonic-gate 
10587c478bd9Sstevel@tonic-gate 		sy_call = (int64_t (*)())callp->sy_call;
10597c478bd9Sstevel@tonic-gate 		rval = (*sy_call)(a0, a1, a2, a3, a4, a5);
10607c478bd9Sstevel@tonic-gate 	} else {
10617c478bd9Sstevel@tonic-gate 		rval = syscall_ap();
10627c478bd9Sstevel@tonic-gate 	}
10637c478bd9Sstevel@tonic-gate 
10647c478bd9Sstevel@tonic-gate 	THREAD_KPRI_REQUEST();	/* regain priority from read lock */
10657c478bd9Sstevel@tonic-gate 	rw_exit(module_lock);
10667c478bd9Sstevel@tonic-gate 	return (rval);
10677c478bd9Sstevel@tonic-gate }
10687c478bd9Sstevel@tonic-gate 
10697c478bd9Sstevel@tonic-gate /*
10707c478bd9Sstevel@tonic-gate  * Handle indirect system calls.
10717c478bd9Sstevel@tonic-gate  *	This interface should be deprecated.  The library can handle
10727c478bd9Sstevel@tonic-gate  *	this more efficiently, but keep this implementation for old binaries.
10737c478bd9Sstevel@tonic-gate  *
10747c478bd9Sstevel@tonic-gate  * XX64	Needs some work.
10757c478bd9Sstevel@tonic-gate  */
10767c478bd9Sstevel@tonic-gate int64_t
10777c478bd9Sstevel@tonic-gate indir(int code, long a0, long a1, long a2, long a3, long a4)
10787c478bd9Sstevel@tonic-gate {
10797c478bd9Sstevel@tonic-gate 	klwp_t		*lwp = ttolwp(curthread);
10807c478bd9Sstevel@tonic-gate 	struct sysent	*callp;
10817c478bd9Sstevel@tonic-gate 
10827c478bd9Sstevel@tonic-gate 	if (code <= 0 || code >= NSYSCALL)
10837c478bd9Sstevel@tonic-gate 		return (nosys());
10847c478bd9Sstevel@tonic-gate 
10857c478bd9Sstevel@tonic-gate 	ASSERT(lwp->lwp_ap != NULL);
10867c478bd9Sstevel@tonic-gate 
10877c478bd9Sstevel@tonic-gate 	curthread->t_sysnum = code;
10887c478bd9Sstevel@tonic-gate 	callp = LWP_GETSYSENT(lwp) + code;
10897c478bd9Sstevel@tonic-gate 
10907c478bd9Sstevel@tonic-gate 	/*
10917c478bd9Sstevel@tonic-gate 	 * Handle argument setup, unless already done in pre_syscall().
10927c478bd9Sstevel@tonic-gate 	 */
10937c478bd9Sstevel@tonic-gate 	if (callp->sy_narg > 5) {
10947c478bd9Sstevel@tonic-gate 		if (save_syscall_args()) 	/* move args to LWP array */
10957c478bd9Sstevel@tonic-gate 			return ((int64_t)set_errno(EFAULT));
10967c478bd9Sstevel@tonic-gate 	} else if (!lwp->lwp_argsaved) {
10977c478bd9Sstevel@tonic-gate 		long *ap;
10987c478bd9Sstevel@tonic-gate 
10997c478bd9Sstevel@tonic-gate 		ap = lwp->lwp_ap;		/* args haven't been saved */
11007c478bd9Sstevel@tonic-gate 		lwp->lwp_ap = ap + 1;		/* advance arg pointer */
11017c478bd9Sstevel@tonic-gate 		curthread->t_post_sys = 1;	/* so lwp_ap will be reset */
11027c478bd9Sstevel@tonic-gate 	}
11037c478bd9Sstevel@tonic-gate 	return ((*callp->sy_callc)(a0, a1, a2, a3, a4, lwp->lwp_arg[5]));
11047c478bd9Sstevel@tonic-gate }
11057c478bd9Sstevel@tonic-gate 
11067c478bd9Sstevel@tonic-gate /*
11077c478bd9Sstevel@tonic-gate  * set_errno - set an error return from the current system call.
11087c478bd9Sstevel@tonic-gate  *	This could be a macro.
11097c478bd9Sstevel@tonic-gate  *	This returns the value it is passed, so that the caller can
11107c478bd9Sstevel@tonic-gate  *	use tail-recursion-elimination and do return (set_errno(ERRNO));
11117c478bd9Sstevel@tonic-gate  */
11127c478bd9Sstevel@tonic-gate uint_t
11137c478bd9Sstevel@tonic-gate set_errno(uint_t error)
11147c478bd9Sstevel@tonic-gate {
11157c478bd9Sstevel@tonic-gate 	ASSERT(error != 0);		/* must not be used to clear errno */
11167c478bd9Sstevel@tonic-gate 
11177c478bd9Sstevel@tonic-gate 	curthread->t_post_sys = 1;	/* have post_syscall do error return */
11187c478bd9Sstevel@tonic-gate 	return (ttolwp(curthread)->lwp_errno = error);
11197c478bd9Sstevel@tonic-gate }
11207c478bd9Sstevel@tonic-gate 
11217c478bd9Sstevel@tonic-gate /*
11227c478bd9Sstevel@tonic-gate  * set_proc_pre_sys - Set pre-syscall processing for entire process.
11237c478bd9Sstevel@tonic-gate  */
11247c478bd9Sstevel@tonic-gate void
11257c478bd9Sstevel@tonic-gate set_proc_pre_sys(proc_t *p)
11267c478bd9Sstevel@tonic-gate {
11277c478bd9Sstevel@tonic-gate 	kthread_t	*t;
11287c478bd9Sstevel@tonic-gate 	kthread_t	*first;
11297c478bd9Sstevel@tonic-gate 
11307c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&p->p_lock));
11317c478bd9Sstevel@tonic-gate 
11327c478bd9Sstevel@tonic-gate 	t = first = p->p_tlist;
11337c478bd9Sstevel@tonic-gate 	do {
11347c478bd9Sstevel@tonic-gate 		t->t_pre_sys = 1;
11357c478bd9Sstevel@tonic-gate 	} while ((t = t->t_forw) != first);
11367c478bd9Sstevel@tonic-gate }
11377c478bd9Sstevel@tonic-gate 
11387c478bd9Sstevel@tonic-gate /*
11397c478bd9Sstevel@tonic-gate  * set_proc_post_sys - Set post-syscall processing for entire process.
11407c478bd9Sstevel@tonic-gate  */
11417c478bd9Sstevel@tonic-gate void
11427c478bd9Sstevel@tonic-gate set_proc_post_sys(proc_t *p)
11437c478bd9Sstevel@tonic-gate {
11447c478bd9Sstevel@tonic-gate 	kthread_t	*t;
11457c478bd9Sstevel@tonic-gate 	kthread_t	*first;
11467c478bd9Sstevel@tonic-gate 
11477c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&p->p_lock));
11487c478bd9Sstevel@tonic-gate 
11497c478bd9Sstevel@tonic-gate 	t = first = p->p_tlist;
11507c478bd9Sstevel@tonic-gate 	do {
11517c478bd9Sstevel@tonic-gate 		t->t_post_sys = 1;
11527c478bd9Sstevel@tonic-gate 	} while ((t = t->t_forw) != first);
11537c478bd9Sstevel@tonic-gate }
11547c478bd9Sstevel@tonic-gate 
11557c478bd9Sstevel@tonic-gate /*
11567c478bd9Sstevel@tonic-gate  * set_proc_sys - Set pre- and post-syscall processing for entire process.
11577c478bd9Sstevel@tonic-gate  */
11587c478bd9Sstevel@tonic-gate void
11597c478bd9Sstevel@tonic-gate set_proc_sys(proc_t *p)
11607c478bd9Sstevel@tonic-gate {
11617c478bd9Sstevel@tonic-gate 	kthread_t	*t;
11627c478bd9Sstevel@tonic-gate 	kthread_t	*first;
11637c478bd9Sstevel@tonic-gate 
11647c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&p->p_lock));
11657c478bd9Sstevel@tonic-gate 
11667c478bd9Sstevel@tonic-gate 	t = first = p->p_tlist;
11677c478bd9Sstevel@tonic-gate 	do {
11687c478bd9Sstevel@tonic-gate 		t->t_pre_sys = 1;
11697c478bd9Sstevel@tonic-gate 		t->t_post_sys = 1;
11707c478bd9Sstevel@tonic-gate 	} while ((t = t->t_forw) != first);
11717c478bd9Sstevel@tonic-gate }
11727c478bd9Sstevel@tonic-gate 
11737c478bd9Sstevel@tonic-gate /*
11747c478bd9Sstevel@tonic-gate  * set_all_proc_sys - set pre- and post-syscall processing flags for all
11757c478bd9Sstevel@tonic-gate  * user processes.
11767c478bd9Sstevel@tonic-gate  *
11777c478bd9Sstevel@tonic-gate  * This is needed when auditing, tracing, or other facilities which affect
11787c478bd9Sstevel@tonic-gate  * all processes are turned on.
11797c478bd9Sstevel@tonic-gate  */
11807c478bd9Sstevel@tonic-gate void
11817c478bd9Sstevel@tonic-gate set_all_proc_sys()
11827c478bd9Sstevel@tonic-gate {
11837c478bd9Sstevel@tonic-gate 	kthread_t	*t;
11847c478bd9Sstevel@tonic-gate 	kthread_t	*first;
11857c478bd9Sstevel@tonic-gate 
11867c478bd9Sstevel@tonic-gate 	mutex_enter(&pidlock);
11877c478bd9Sstevel@tonic-gate 	t = first = curthread;
11887c478bd9Sstevel@tonic-gate 	do {
11897c478bd9Sstevel@tonic-gate 		t->t_pre_sys = 1;
11907c478bd9Sstevel@tonic-gate 		t->t_post_sys = 1;
11917c478bd9Sstevel@tonic-gate 	} while ((t = t->t_next) != first);
11927c478bd9Sstevel@tonic-gate 	mutex_exit(&pidlock);
11937c478bd9Sstevel@tonic-gate }
11947c478bd9Sstevel@tonic-gate 
11957c478bd9Sstevel@tonic-gate /*
11967c478bd9Sstevel@tonic-gate  * set_proc_ast - Set asynchronous service trap (AST) flag for all
11977c478bd9Sstevel@tonic-gate  * threads in process.
11987c478bd9Sstevel@tonic-gate  */
11997c478bd9Sstevel@tonic-gate void
12007c478bd9Sstevel@tonic-gate set_proc_ast(proc_t *p)
12017c478bd9Sstevel@tonic-gate {
12027c478bd9Sstevel@tonic-gate 	kthread_t	*t;
12037c478bd9Sstevel@tonic-gate 	kthread_t	*first;
12047c478bd9Sstevel@tonic-gate 
12057c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&p->p_lock));
12067c478bd9Sstevel@tonic-gate 
12077c478bd9Sstevel@tonic-gate 	t = first = p->p_tlist;
12087c478bd9Sstevel@tonic-gate 	do {
12097c478bd9Sstevel@tonic-gate 		aston(t);
12107c478bd9Sstevel@tonic-gate 	} while ((t = t->t_forw) != first);
12117c478bd9Sstevel@tonic-gate }
1212