xref: /onnv-gate/usr/src/uts/sparc/os/syscall.c (revision 13070:87f89233b883)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
51972Sdv142724  * Common Development and Distribution License (the "License").
61972Sdv142724  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
211048Sraf 
220Sstevel@tonic-gate /*
23*13070SEthindra.Ramamurthy@Sun.COM  * Copyright (c) 1991, 2010, Oracle and/or its affiliates. All rights reserved.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #include <sys/param.h>
270Sstevel@tonic-gate #include <sys/vmparam.h>
280Sstevel@tonic-gate #include <sys/types.h>
290Sstevel@tonic-gate #include <sys/sysmacros.h>
300Sstevel@tonic-gate #include <sys/systm.h>
310Sstevel@tonic-gate #include <sys/cmn_err.h>
320Sstevel@tonic-gate #include <sys/signal.h>
330Sstevel@tonic-gate #include <sys/stack.h>
340Sstevel@tonic-gate #include <sys/cred.h>
350Sstevel@tonic-gate #include <sys/user.h>
360Sstevel@tonic-gate #include <sys/debug.h>
370Sstevel@tonic-gate #include <sys/errno.h>
380Sstevel@tonic-gate #include <sys/proc.h>
390Sstevel@tonic-gate #include <sys/var.h>
400Sstevel@tonic-gate #include <sys/inline.h>
410Sstevel@tonic-gate #include <sys/syscall.h>
420Sstevel@tonic-gate #include <sys/ucontext.h>
430Sstevel@tonic-gate #include <sys/cpuvar.h>
440Sstevel@tonic-gate #include <sys/siginfo.h>
450Sstevel@tonic-gate #include <sys/trap.h>
460Sstevel@tonic-gate #include <sys/machtrap.h>
470Sstevel@tonic-gate #include <sys/sysinfo.h>
480Sstevel@tonic-gate #include <sys/procfs.h>
490Sstevel@tonic-gate #include <sys/prsystm.h>
500Sstevel@tonic-gate #include <sys/fpu/fpusystm.h>
510Sstevel@tonic-gate #include <sys/modctl.h>
520Sstevel@tonic-gate #include <sys/aio_impl.h>
530Sstevel@tonic-gate #include <c2/audit.h>
540Sstevel@tonic-gate #include <sys/tnf.h>
550Sstevel@tonic-gate #include <sys/tnf_probe.h>
560Sstevel@tonic-gate #include <sys/machpcb.h>
570Sstevel@tonic-gate #include <sys/privregs.h>
580Sstevel@tonic-gate #include <sys/copyops.h>
590Sstevel@tonic-gate #include <sys/timer.h>
600Sstevel@tonic-gate #include <sys/priv.h>
610Sstevel@tonic-gate #include <sys/msacct.h>
620Sstevel@tonic-gate 
630Sstevel@tonic-gate int syscalltrace = 0;
640Sstevel@tonic-gate #ifdef SYSCALLTRACE
650Sstevel@tonic-gate static kmutex_t	systrace_lock;		/* syscall tracing lock */
660Sstevel@tonic-gate #endif /* SYSCALLTRACE */
670Sstevel@tonic-gate 
680Sstevel@tonic-gate static krwlock_t *lock_syscall(struct sysent *, uint_t);
690Sstevel@tonic-gate 
700Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
710Sstevel@tonic-gate static struct sysent *
lwp_getsysent(klwp_t * lwp)720Sstevel@tonic-gate lwp_getsysent(klwp_t *lwp)
730Sstevel@tonic-gate {
740Sstevel@tonic-gate 	if (lwp_getdatamodel(lwp) == DATAMODEL_NATIVE)
750Sstevel@tonic-gate 		return (sysent);
760Sstevel@tonic-gate 	return (sysent32);
770Sstevel@tonic-gate }
780Sstevel@tonic-gate #define	LWP_GETSYSENT(lwp)	(lwp_getsysent(lwp))
790Sstevel@tonic-gate #else
800Sstevel@tonic-gate #define	LWP_GETSYSENT(lwp)	(sysent)
810Sstevel@tonic-gate #endif
820Sstevel@tonic-gate 
830Sstevel@tonic-gate /*
840Sstevel@tonic-gate  * Called to restore the lwp's register window just before
850Sstevel@tonic-gate  * returning to user level (only if the registers have been
860Sstevel@tonic-gate  * fetched or modified through /proc).
870Sstevel@tonic-gate  */
880Sstevel@tonic-gate /*ARGSUSED1*/
890Sstevel@tonic-gate void
xregrestore(klwp_t * lwp,int shared)900Sstevel@tonic-gate xregrestore(klwp_t *lwp, int shared)
910Sstevel@tonic-gate {
920Sstevel@tonic-gate 	/*
930Sstevel@tonic-gate 	 * If locals+ins were modified by /proc copy them out.
940Sstevel@tonic-gate 	 * Also copy to the shared window, if necessary.
950Sstevel@tonic-gate 	 */
960Sstevel@tonic-gate 	if (lwp->lwp_pcb.pcb_xregstat == XREGMODIFIED) {
970Sstevel@tonic-gate 		struct machpcb *mpcb = lwptompcb(lwp);
980Sstevel@tonic-gate 		caddr_t sp = (caddr_t)lwptoregs(lwp)->r_sp;
990Sstevel@tonic-gate 
1000Sstevel@tonic-gate 		size_t rwinsize;
1010Sstevel@tonic-gate 		caddr_t rwp;
1020Sstevel@tonic-gate 		int is64;
1030Sstevel@tonic-gate 
1040Sstevel@tonic-gate 		if (lwp_getdatamodel(lwp) == DATAMODEL_LP64) {
1050Sstevel@tonic-gate 			rwinsize = sizeof (struct rwindow);
1060Sstevel@tonic-gate 			rwp = sp + STACK_BIAS;
1070Sstevel@tonic-gate 			is64 = 1;
1080Sstevel@tonic-gate 		} else {
1090Sstevel@tonic-gate 			rwinsize = sizeof (struct rwindow32);
1101048Sraf 			sp = (caddr_t)(uintptr_t)(caddr32_t)(uintptr_t)sp;
1110Sstevel@tonic-gate 			rwp = sp;
1120Sstevel@tonic-gate 			is64 = 0;
1130Sstevel@tonic-gate 		}
1140Sstevel@tonic-gate 
1150Sstevel@tonic-gate 		if (is64)
1160Sstevel@tonic-gate 			(void) copyout_nowatch(&lwp->lwp_pcb.pcb_xregs,
1175753Sgww 			    rwp, rwinsize);
1180Sstevel@tonic-gate 		else {
1190Sstevel@tonic-gate 			struct rwindow32 rwindow32;
1200Sstevel@tonic-gate 			int watched;
1210Sstevel@tonic-gate 
1220Sstevel@tonic-gate 			watched = watch_disable_addr(rwp, rwinsize, S_WRITE);
1230Sstevel@tonic-gate 			rwindow_nto32(&lwp->lwp_pcb.pcb_xregs, &rwindow32);
1240Sstevel@tonic-gate 			(void) copyout(&rwindow32, rwp, rwinsize);
1250Sstevel@tonic-gate 			if (watched)
1260Sstevel@tonic-gate 				watch_enable_addr(rwp, rwinsize, S_WRITE);
1270Sstevel@tonic-gate 		}
1280Sstevel@tonic-gate 
1290Sstevel@tonic-gate 		/* also copy to the user return window */
1300Sstevel@tonic-gate 		mpcb->mpcb_rsp[0] = sp;
1310Sstevel@tonic-gate 		mpcb->mpcb_rsp[1] = NULL;
1320Sstevel@tonic-gate 		bcopy(&lwp->lwp_pcb.pcb_xregs, &mpcb->mpcb_rwin[0],
1335753Sgww 		    sizeof (lwp->lwp_pcb.pcb_xregs));
1340Sstevel@tonic-gate 	}
1350Sstevel@tonic-gate 	lwp->lwp_pcb.pcb_xregstat = XREGNONE;
1360Sstevel@tonic-gate }
1370Sstevel@tonic-gate 
1380Sstevel@tonic-gate 
1390Sstevel@tonic-gate /*
1400Sstevel@tonic-gate  * Get the arguments to the current system call.
1410Sstevel@tonic-gate  *	lwp->lwp_ap normally points to the out regs in the reg structure.
1420Sstevel@tonic-gate  *	If the user is going to change the out registers and might want to
1430Sstevel@tonic-gate  *	get the args (for /proc tracing), it must copy the args elsewhere
1440Sstevel@tonic-gate  *	via save_syscall_args().
1450Sstevel@tonic-gate  */
1460Sstevel@tonic-gate uint_t
get_syscall_args(klwp_t * lwp,long * argp,int * nargsp)1470Sstevel@tonic-gate get_syscall_args(klwp_t *lwp, long *argp, int *nargsp)
1480Sstevel@tonic-gate {
1490Sstevel@tonic-gate 	kthread_t	*t = lwptot(lwp);
1500Sstevel@tonic-gate 	uint_t	code = t->t_sysnum;
1510Sstevel@tonic-gate 	long	mask;
1520Sstevel@tonic-gate 	long	*ap;
1530Sstevel@tonic-gate 	int	nargs;
1540Sstevel@tonic-gate 
1550Sstevel@tonic-gate 	if (lwptoproc(lwp)->p_model == DATAMODEL_ILP32)
1560Sstevel@tonic-gate 		mask = (uint32_t)0xffffffffU;
1570Sstevel@tonic-gate 	else
1580Sstevel@tonic-gate 		mask = 0xffffffffffffffff;
1590Sstevel@tonic-gate 
1600Sstevel@tonic-gate 	if (code != 0 && code < NSYSCALL) {
1610Sstevel@tonic-gate 
1620Sstevel@tonic-gate 		nargs = LWP_GETSYSENT(lwp)[code].sy_narg;
1630Sstevel@tonic-gate 
1640Sstevel@tonic-gate 		ASSERT(nargs <= MAXSYSARGS);
1650Sstevel@tonic-gate 
1660Sstevel@tonic-gate 		*nargsp = nargs;
1670Sstevel@tonic-gate 		ap = lwp->lwp_ap;
1680Sstevel@tonic-gate 		while (nargs-- > 0)
1690Sstevel@tonic-gate 			*argp++ = *ap++ & mask;
1700Sstevel@tonic-gate 	} else {
1710Sstevel@tonic-gate 		*nargsp = 0;
1720Sstevel@tonic-gate 	}
1730Sstevel@tonic-gate 	return (code);
1740Sstevel@tonic-gate }
1750Sstevel@tonic-gate 
1760Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
1770Sstevel@tonic-gate /*
1780Sstevel@tonic-gate  * Get the arguments to the current 32-bit system call.
1790Sstevel@tonic-gate  */
1800Sstevel@tonic-gate uint_t
get_syscall32_args(klwp_t * lwp,int * argp,int * nargsp)1810Sstevel@tonic-gate get_syscall32_args(klwp_t *lwp, int *argp, int *nargsp)
1820Sstevel@tonic-gate {
1830Sstevel@tonic-gate 	long args[MAXSYSARGS];
1840Sstevel@tonic-gate 	uint_t i, code;
1850Sstevel@tonic-gate 
1860Sstevel@tonic-gate 	code = get_syscall_args(lwp, args, nargsp);
1870Sstevel@tonic-gate 	for (i = 0; i != *nargsp; i++)
1880Sstevel@tonic-gate 		*argp++ = (int)args[i];
1890Sstevel@tonic-gate 	return (code);
1900Sstevel@tonic-gate }
1910Sstevel@tonic-gate #endif
1920Sstevel@tonic-gate 
1930Sstevel@tonic-gate /*
1940Sstevel@tonic-gate  * 	Save the system call arguments in a safe place.
1950Sstevel@tonic-gate  *	lwp->lwp_ap normally points to the out regs in the reg structure.
1960Sstevel@tonic-gate  *	If the user is going to change the out registers, g1, or the stack,
1970Sstevel@tonic-gate  *	and might want to get the args (for /proc tracing), it must copy
1980Sstevel@tonic-gate  *	the args elsewhere via save_syscall_args().
1990Sstevel@tonic-gate  *
2000Sstevel@tonic-gate  *	This may be called from stop() even when we're not in a system call.
2010Sstevel@tonic-gate  *	Since there's no easy way to tell, this must be safe (not panic).
2020Sstevel@tonic-gate  *	If the copyins get data faults, return non-zero.
2030Sstevel@tonic-gate  */
2040Sstevel@tonic-gate int
save_syscall_args()2050Sstevel@tonic-gate save_syscall_args()
2060Sstevel@tonic-gate {
2070Sstevel@tonic-gate 	kthread_t	*t = curthread;
2080Sstevel@tonic-gate 	klwp_t		*lwp = ttolwp(t);
2090Sstevel@tonic-gate 	struct regs	*rp = lwptoregs(lwp);
2100Sstevel@tonic-gate 	uint_t		code = t->t_sysnum;
2110Sstevel@tonic-gate 	uint_t		nargs;
2120Sstevel@tonic-gate 	int		i;
2130Sstevel@tonic-gate 	caddr_t		ua;
2140Sstevel@tonic-gate 	model_t		datamodel;
2150Sstevel@tonic-gate 
2160Sstevel@tonic-gate 	if (lwp->lwp_argsaved || code == 0)
2170Sstevel@tonic-gate 		return (0);		/* args already saved or not needed */
2180Sstevel@tonic-gate 
2190Sstevel@tonic-gate 	if (code >= NSYSCALL) {
2200Sstevel@tonic-gate 		nargs = 0;		/* illegal syscall */
2210Sstevel@tonic-gate 	} else {
2220Sstevel@tonic-gate 		struct sysent *se = LWP_GETSYSENT(lwp);
2230Sstevel@tonic-gate 		struct sysent *callp = se + code;
2240Sstevel@tonic-gate 
2250Sstevel@tonic-gate 		nargs = callp->sy_narg;
2260Sstevel@tonic-gate 		if (LOADABLE_SYSCALL(callp) && nargs == 0) {
2270Sstevel@tonic-gate 			krwlock_t	*module_lock;
2280Sstevel@tonic-gate 
2290Sstevel@tonic-gate 			/*
2300Sstevel@tonic-gate 			 * Find out how many arguments the system
2310Sstevel@tonic-gate 			 * call uses.
2320Sstevel@tonic-gate 			 *
2330Sstevel@tonic-gate 			 * We have the property that loaded syscalls
2340Sstevel@tonic-gate 			 * never change the number of arguments they
2350Sstevel@tonic-gate 			 * use after they've been loaded once.  This
2360Sstevel@tonic-gate 			 * allows us to stop for /proc tracing without
2370Sstevel@tonic-gate 			 * holding the module lock.
2380Sstevel@tonic-gate 			 * /proc is assured that sy_narg is valid.
2390Sstevel@tonic-gate 			 */
2400Sstevel@tonic-gate 			module_lock = lock_syscall(se, code);
2410Sstevel@tonic-gate 			nargs = callp->sy_narg;
2420Sstevel@tonic-gate 			rw_exit(module_lock);
2430Sstevel@tonic-gate 		}
2440Sstevel@tonic-gate 	}
2450Sstevel@tonic-gate 
2460Sstevel@tonic-gate 	/*
2470Sstevel@tonic-gate 	 * Fetch the system call arguments.
2480Sstevel@tonic-gate 	 */
2490Sstevel@tonic-gate 	if (nargs == 0)
2500Sstevel@tonic-gate 		goto out;
2510Sstevel@tonic-gate 
2520Sstevel@tonic-gate 
2530Sstevel@tonic-gate 	ASSERT(nargs <= MAXSYSARGS);
2540Sstevel@tonic-gate 
2550Sstevel@tonic-gate 	if ((datamodel = lwp_getdatamodel(lwp)) == DATAMODEL_ILP32) {
2560Sstevel@tonic-gate 
2570Sstevel@tonic-gate 		if (rp->r_g1 == 0) {	/* indirect syscall */
2580Sstevel@tonic-gate 
2590Sstevel@tonic-gate 			lwp->lwp_arg[0] = (uint32_t)rp->r_o1;
2600Sstevel@tonic-gate 			lwp->lwp_arg[1] = (uint32_t)rp->r_o2;
2610Sstevel@tonic-gate 			lwp->lwp_arg[2] = (uint32_t)rp->r_o3;
2620Sstevel@tonic-gate 			lwp->lwp_arg[3] = (uint32_t)rp->r_o4;
2630Sstevel@tonic-gate 			lwp->lwp_arg[4] = (uint32_t)rp->r_o5;
2640Sstevel@tonic-gate 			if (nargs > 5) {
2651048Sraf 				ua = (caddr_t)(uintptr_t)(caddr32_t)(uintptr_t)
2661048Sraf 				    (rp->r_sp + MINFRAME32);
2670Sstevel@tonic-gate 				for (i = 5; i < nargs; i++) {
2680Sstevel@tonic-gate 					uint32_t a;
2690Sstevel@tonic-gate 					if (fuword32(ua, &a) != 0)
2700Sstevel@tonic-gate 						return (-1);
2710Sstevel@tonic-gate 					lwp->lwp_arg[i] = a;
2720Sstevel@tonic-gate 					ua += sizeof (a);
2730Sstevel@tonic-gate 				}
2740Sstevel@tonic-gate 			}
2750Sstevel@tonic-gate 		} else {
2760Sstevel@tonic-gate 			lwp->lwp_arg[0] = (uint32_t)rp->r_o0;
2770Sstevel@tonic-gate 			lwp->lwp_arg[1] = (uint32_t)rp->r_o1;
2780Sstevel@tonic-gate 			lwp->lwp_arg[2] = (uint32_t)rp->r_o2;
2790Sstevel@tonic-gate 			lwp->lwp_arg[3] = (uint32_t)rp->r_o3;
2800Sstevel@tonic-gate 			lwp->lwp_arg[4] = (uint32_t)rp->r_o4;
2810Sstevel@tonic-gate 			lwp->lwp_arg[5] = (uint32_t)rp->r_o5;
2820Sstevel@tonic-gate 			if (nargs > 6) {
2831048Sraf 				ua = (caddr_t)(uintptr_t)(caddr32_t)(uintptr_t)
2841048Sraf 				    (rp->r_sp + MINFRAME32);
2850Sstevel@tonic-gate 				for (i = 6; i < nargs; i++) {
2860Sstevel@tonic-gate 					uint32_t a;
2870Sstevel@tonic-gate 					if (fuword32(ua, &a) != 0)
2880Sstevel@tonic-gate 						return (-1);
2890Sstevel@tonic-gate 					lwp->lwp_arg[i] = a;
2900Sstevel@tonic-gate 					ua += sizeof (a);
2910Sstevel@tonic-gate 				}
2920Sstevel@tonic-gate 			}
2930Sstevel@tonic-gate 		}
2940Sstevel@tonic-gate 	} else {
2950Sstevel@tonic-gate 		ASSERT(datamodel == DATAMODEL_LP64);
2960Sstevel@tonic-gate 		lwp->lwp_arg[0] = rp->r_o0;
2970Sstevel@tonic-gate 		lwp->lwp_arg[1] = rp->r_o1;
2980Sstevel@tonic-gate 		lwp->lwp_arg[2] = rp->r_o2;
2990Sstevel@tonic-gate 		lwp->lwp_arg[3] = rp->r_o3;
3000Sstevel@tonic-gate 		lwp->lwp_arg[4] = rp->r_o4;
3010Sstevel@tonic-gate 		lwp->lwp_arg[5] = rp->r_o5;
3020Sstevel@tonic-gate 		if (nargs > 6) {
3030Sstevel@tonic-gate 			ua = (caddr_t)rp->r_sp + MINFRAME + STACK_BIAS;
3040Sstevel@tonic-gate 			for (i = 6; i < nargs; i++) {
3050Sstevel@tonic-gate 				unsigned long a;
3060Sstevel@tonic-gate 				if (fulword(ua, &a) != 0)
3070Sstevel@tonic-gate 					return (-1);
3080Sstevel@tonic-gate 				lwp->lwp_arg[i] = a;
3090Sstevel@tonic-gate 				ua += sizeof (a);
3100Sstevel@tonic-gate 			}
3110Sstevel@tonic-gate 		}
3120Sstevel@tonic-gate 	}
3130Sstevel@tonic-gate 
3140Sstevel@tonic-gate out:
3150Sstevel@tonic-gate 	lwp->lwp_ap = lwp->lwp_arg;
3160Sstevel@tonic-gate 	lwp->lwp_argsaved = 1;
3170Sstevel@tonic-gate 	t->t_post_sys = 1;	/* so lwp_ap will be reset */
3180Sstevel@tonic-gate 	return (0);
3190Sstevel@tonic-gate }
3200Sstevel@tonic-gate 
3210Sstevel@tonic-gate void
reset_syscall_args(void)3220Sstevel@tonic-gate reset_syscall_args(void)
3230Sstevel@tonic-gate {
3240Sstevel@tonic-gate 	klwp_t *lwp = ttolwp(curthread);
3250Sstevel@tonic-gate 
3260Sstevel@tonic-gate 	lwp->lwp_ap = (long *)&lwptoregs(lwp)->r_o0;
3270Sstevel@tonic-gate 	lwp->lwp_argsaved = 0;
3280Sstevel@tonic-gate }
3290Sstevel@tonic-gate 
3300Sstevel@tonic-gate /*
3310Sstevel@tonic-gate  * nonexistent system call-- signal lwp (may want to handle it)
3320Sstevel@tonic-gate  * flag error if lwp won't see signal immediately
3330Sstevel@tonic-gate  * This works for old or new calling sequence.
3340Sstevel@tonic-gate  */
3350Sstevel@tonic-gate int64_t
nosys()3360Sstevel@tonic-gate nosys()
3370Sstevel@tonic-gate {
3380Sstevel@tonic-gate 	tsignal(curthread, SIGSYS);
3390Sstevel@tonic-gate 	return ((int64_t)set_errno(ENOSYS));
3400Sstevel@tonic-gate }
3410Sstevel@tonic-gate 
3420Sstevel@tonic-gate /*
3430Sstevel@tonic-gate  * Perform pre-system-call processing, including stopping for tracing,
3440Sstevel@tonic-gate  * auditing, microstate-accounting, etc.
3450Sstevel@tonic-gate  *
3460Sstevel@tonic-gate  * This routine is called only if the t_pre_sys flag is set.  Any condition
3470Sstevel@tonic-gate  * requiring pre-syscall handling must set the t_pre_sys flag.  If the
3480Sstevel@tonic-gate  * condition is persistent, this routine will repost t_pre_sys.
3490Sstevel@tonic-gate  */
3500Sstevel@tonic-gate int
pre_syscall(int arg0)3510Sstevel@tonic-gate pre_syscall(int arg0)
3520Sstevel@tonic-gate {
3530Sstevel@tonic-gate 	unsigned int code;
3540Sstevel@tonic-gate 	kthread_t *t = curthread;
3550Sstevel@tonic-gate 	proc_t *p = ttoproc(t);
3560Sstevel@tonic-gate 	klwp_t *lwp = ttolwp(t);
3570Sstevel@tonic-gate 	struct regs *rp = lwptoregs(lwp);
3580Sstevel@tonic-gate 	int	repost;
3590Sstevel@tonic-gate 
3600Sstevel@tonic-gate 	t->t_pre_sys = repost = 0;	/* clear pre-syscall processing flag */
3610Sstevel@tonic-gate 
3620Sstevel@tonic-gate 	ASSERT(t->t_schedflag & TS_DONT_SWAP);
3630Sstevel@tonic-gate 
3640Sstevel@tonic-gate 	syscall_mstate(LMS_USER, LMS_SYSTEM);
3650Sstevel@tonic-gate 
3660Sstevel@tonic-gate 	/*
3670Sstevel@tonic-gate 	 * The syscall arguments in the out registers should be pointed to
3680Sstevel@tonic-gate 	 * by lwp_ap.  If the args need to be copied so that the outs can
3690Sstevel@tonic-gate 	 * be changed without losing the ability to get the args for /proc,
3700Sstevel@tonic-gate 	 * they can be saved by save_syscall_args(), and lwp_ap will be
3710Sstevel@tonic-gate 	 * restored by post_syscall().
3720Sstevel@tonic-gate 	 */
3730Sstevel@tonic-gate 	ASSERT(lwp->lwp_ap == (long *)&rp->r_o0);
3740Sstevel@tonic-gate 
3750Sstevel@tonic-gate 	/*
3760Sstevel@tonic-gate 	 * Make sure the thread is holding the latest credentials for the
3770Sstevel@tonic-gate 	 * process.  The credentials in the process right now apply to this
3780Sstevel@tonic-gate 	 * thread for the entire system call.
3790Sstevel@tonic-gate 	 */
3800Sstevel@tonic-gate 	if (t->t_cred != p->p_cred) {
3810Sstevel@tonic-gate 		cred_t *oldcred = t->t_cred;
3820Sstevel@tonic-gate 		/*
3830Sstevel@tonic-gate 		 * DTrace accesses t_cred in probe context.  t_cred must
3840Sstevel@tonic-gate 		 * always be either NULL, or point to a valid, allocated cred
3850Sstevel@tonic-gate 		 * structure.
3860Sstevel@tonic-gate 		 */
3870Sstevel@tonic-gate 		t->t_cred = crgetcred();
3880Sstevel@tonic-gate 		crfree(oldcred);
3890Sstevel@tonic-gate 	}
3900Sstevel@tonic-gate 
3910Sstevel@tonic-gate 	/*
3920Sstevel@tonic-gate 	 * Undo special arrangements to single-step the lwp
3930Sstevel@tonic-gate 	 * so that a debugger will see valid register contents.
3940Sstevel@tonic-gate 	 * Also so that the pc is valid for syncfpu().
3950Sstevel@tonic-gate 	 * Also so that a syscall like exec() can be stepped.
3960Sstevel@tonic-gate 	 */
3970Sstevel@tonic-gate 	if (lwp->lwp_pcb.pcb_step != STEP_NONE) {
3980Sstevel@tonic-gate 		(void) prundostep();
3990Sstevel@tonic-gate 		repost = 1;
4000Sstevel@tonic-gate 	}
4010Sstevel@tonic-gate 
4020Sstevel@tonic-gate 	/*
4030Sstevel@tonic-gate 	 * Check for indirect system call in case we stop for tracing.
4040Sstevel@tonic-gate 	 * Don't allow multiple indirection.
4050Sstevel@tonic-gate 	 */
4060Sstevel@tonic-gate 	code = t->t_sysnum;
4070Sstevel@tonic-gate 	if (code == 0 && arg0 != 0) {		/* indirect syscall */
4080Sstevel@tonic-gate 		code = arg0;
4090Sstevel@tonic-gate 		t->t_sysnum = arg0;
4100Sstevel@tonic-gate 	}
4110Sstevel@tonic-gate 
4120Sstevel@tonic-gate 	/*
4130Sstevel@tonic-gate 	 * From the proc(4) manual page:
4140Sstevel@tonic-gate 	 * When entry to a system call is being traced, the traced process
4150Sstevel@tonic-gate 	 * stops after having begun the call to the system but before the
4160Sstevel@tonic-gate 	 * system call arguments have been fetched from the process.
4170Sstevel@tonic-gate 	 * If proc changes the args we must refetch them after starting.
4180Sstevel@tonic-gate 	 */
4190Sstevel@tonic-gate 	if (PTOU(p)->u_systrap) {
4200Sstevel@tonic-gate 		if (prismember(&PTOU(p)->u_entrymask, code)) {
4210Sstevel@tonic-gate 			/*
4220Sstevel@tonic-gate 			 * Recheck stop condition, now that lock is held.
4230Sstevel@tonic-gate 			 */
4240Sstevel@tonic-gate 			mutex_enter(&p->p_lock);
4250Sstevel@tonic-gate 			if (PTOU(p)->u_systrap &&
4260Sstevel@tonic-gate 			    prismember(&PTOU(p)->u_entrymask, code)) {
4270Sstevel@tonic-gate 				stop(PR_SYSENTRY, code);
4280Sstevel@tonic-gate 				/*
4290Sstevel@tonic-gate 				 * Must refetch args since they were
4300Sstevel@tonic-gate 				 * possibly modified by /proc.  Indicate
4310Sstevel@tonic-gate 				 * that the valid copy is in the
4320Sstevel@tonic-gate 				 * registers.
4330Sstevel@tonic-gate 				 */
4340Sstevel@tonic-gate 				lwp->lwp_argsaved = 0;
4350Sstevel@tonic-gate 				lwp->lwp_ap = (long *)&rp->r_o0;
4360Sstevel@tonic-gate 			}
4370Sstevel@tonic-gate 			mutex_exit(&p->p_lock);
4380Sstevel@tonic-gate 		}
4390Sstevel@tonic-gate 		repost = 1;
4400Sstevel@tonic-gate 	}
4410Sstevel@tonic-gate 
4420Sstevel@tonic-gate 	if (lwp->lwp_sysabort) {
4430Sstevel@tonic-gate 		/*
4440Sstevel@tonic-gate 		 * lwp_sysabort may have been set via /proc while the process
4450Sstevel@tonic-gate 		 * was stopped on PR_SYSENTRY.  If so, abort the system call.
4460Sstevel@tonic-gate 		 * Override any error from the copyin() of the arguments.
4470Sstevel@tonic-gate 		 */
4480Sstevel@tonic-gate 		lwp->lwp_sysabort = 0;
4490Sstevel@tonic-gate 		(void) set_errno(EINTR); /* sets post-sys processing */
4500Sstevel@tonic-gate 		t->t_pre_sys = 1;	/* repost anyway */
4510Sstevel@tonic-gate 		return (1);		/* don't do system call, return EINTR */
4520Sstevel@tonic-gate 	}
4530Sstevel@tonic-gate 
45411861SMarek.Pospisil@Sun.COM 	/* begin auditing for this syscall */
45511861SMarek.Pospisil@Sun.COM 	if (audit_active == C2AUDIT_LOADED) {
45611861SMarek.Pospisil@Sun.COM 		uint32_t auditing = au_zone_getstate(NULL);
45711861SMarek.Pospisil@Sun.COM 
45811861SMarek.Pospisil@Sun.COM 		if (auditing & AU_AUDIT_MASK) {
45911861SMarek.Pospisil@Sun.COM 			int error;
46011861SMarek.Pospisil@Sun.COM 			if (error = audit_start(T_SYSCALL, code, auditing, \
46111861SMarek.Pospisil@Sun.COM 			    0, lwp)) {
46211861SMarek.Pospisil@Sun.COM 				t->t_pre_sys = 1;	/* repost anyway */
46311861SMarek.Pospisil@Sun.COM 				lwp->lwp_error = 0;	/* for old drivers */
46411861SMarek.Pospisil@Sun.COM 				return (error);
46511861SMarek.Pospisil@Sun.COM 			}
46611861SMarek.Pospisil@Sun.COM 			repost = 1;
4670Sstevel@tonic-gate 		}
4680Sstevel@tonic-gate 	}
4690Sstevel@tonic-gate 
4700Sstevel@tonic-gate #ifndef NPROBE
4710Sstevel@tonic-gate 	/* Kernel probe */
4720Sstevel@tonic-gate 	if (tnf_tracing_active) {
4730Sstevel@tonic-gate 		TNF_PROBE_1(syscall_start, "syscall thread", /* CSTYLED */,
4740Sstevel@tonic-gate 			tnf_sysnum,	sysnum,		t->t_sysnum);
4750Sstevel@tonic-gate 		t->t_post_sys = 1;	/* make sure post_syscall runs */
4760Sstevel@tonic-gate 		repost = 1;
4770Sstevel@tonic-gate 	}
4780Sstevel@tonic-gate #endif /* NPROBE */
4790Sstevel@tonic-gate 
4800Sstevel@tonic-gate #ifdef SYSCALLTRACE
4810Sstevel@tonic-gate 	if (syscalltrace) {
4820Sstevel@tonic-gate 		int i;
4830Sstevel@tonic-gate 		long *ap;
4840Sstevel@tonic-gate 		char *cp;
4850Sstevel@tonic-gate 		char *sysname;
4860Sstevel@tonic-gate 		struct sysent *callp;
4870Sstevel@tonic-gate 
4880Sstevel@tonic-gate 		if (code >= NSYSCALL)
4890Sstevel@tonic-gate 			callp = &nosys_ent;	/* nosys has no args */
4900Sstevel@tonic-gate 		else
4910Sstevel@tonic-gate 			callp = LWP_GETSYSENT(lwp) + code;
4920Sstevel@tonic-gate 		(void) save_syscall_args();
4930Sstevel@tonic-gate 		mutex_enter(&systrace_lock);
4940Sstevel@tonic-gate 		printf("%d: ", p->p_pid);
4950Sstevel@tonic-gate 		if (code >= NSYSCALL)
4960Sstevel@tonic-gate 			printf("0x%x", code);
4970Sstevel@tonic-gate 		else {
4980Sstevel@tonic-gate 			sysname = mod_getsysname(code);
4990Sstevel@tonic-gate 			printf("%s[0x%x]", sysname == NULL ? "NULL" :
5000Sstevel@tonic-gate 			    sysname, code);
5010Sstevel@tonic-gate 		}
5020Sstevel@tonic-gate 		cp = "(";
5030Sstevel@tonic-gate 		for (i = 0, ap = lwp->lwp_ap; i < callp->sy_narg; i++, ap++) {
5040Sstevel@tonic-gate 			printf("%s%lx", cp, *ap);
5050Sstevel@tonic-gate 			cp = ", ";
5060Sstevel@tonic-gate 		}
5070Sstevel@tonic-gate 		if (i)
5080Sstevel@tonic-gate 			printf(")");
5090Sstevel@tonic-gate 		printf(" %s id=0x%p\n", PTOU(p)->u_comm, curthread);
5100Sstevel@tonic-gate 		mutex_exit(&systrace_lock);
5110Sstevel@tonic-gate 	}
5120Sstevel@tonic-gate #endif /* SYSCALLTRACE */
5130Sstevel@tonic-gate 
5140Sstevel@tonic-gate 	/*
5150Sstevel@tonic-gate 	 * If there was a continuing reason for pre-syscall processing,
5160Sstevel@tonic-gate 	 * set the t_pre_sys flag for the next system call.
5170Sstevel@tonic-gate 	 */
5180Sstevel@tonic-gate 	if (repost)
5190Sstevel@tonic-gate 		t->t_pre_sys = 1;
5200Sstevel@tonic-gate 	lwp->lwp_error = 0;	/* for old drivers */
5210Sstevel@tonic-gate 	lwp->lwp_badpriv = PRIV_NONE;	/* for privilege tracing */
5220Sstevel@tonic-gate 	return (0);
5230Sstevel@tonic-gate }
5240Sstevel@tonic-gate 
5250Sstevel@tonic-gate /*
5260Sstevel@tonic-gate  * Post-syscall processing.  Perform abnormal system call completion
5270Sstevel@tonic-gate  * actions such as /proc tracing, profiling, signals, preemption, etc.
5280Sstevel@tonic-gate  *
5290Sstevel@tonic-gate  * This routine is called only if t_post_sys, t_sig_check, or t_astflag is set.
5300Sstevel@tonic-gate  * Any condition requiring pre-syscall handling must set one of these.
5310Sstevel@tonic-gate  * If the condition is persistent, this routine will repost t_post_sys.
5320Sstevel@tonic-gate  */
5330Sstevel@tonic-gate void
post_syscall(long rval1,long rval2)5340Sstevel@tonic-gate post_syscall(long rval1, long rval2)
5350Sstevel@tonic-gate {
5360Sstevel@tonic-gate 	kthread_t	*t = curthread;
5370Sstevel@tonic-gate 	proc_t	*p = curproc;
5380Sstevel@tonic-gate 	klwp_t	*lwp = ttolwp(t);
5390Sstevel@tonic-gate 	struct regs *rp = lwptoregs(lwp);
5400Sstevel@tonic-gate 	uint_t	error;
5410Sstevel@tonic-gate 	int	code = t->t_sysnum;
5420Sstevel@tonic-gate 	int	repost = 0;
5430Sstevel@tonic-gate 	int	proc_stop = 0;		/* non-zero if stopping for /proc */
5440Sstevel@tonic-gate 	int	sigprof = 0;		/* non-zero if sending SIGPROF */
5450Sstevel@tonic-gate 
5460Sstevel@tonic-gate 	t->t_post_sys = 0;
5470Sstevel@tonic-gate 
5480Sstevel@tonic-gate 	error = lwp->lwp_errno;
5490Sstevel@tonic-gate 
5500Sstevel@tonic-gate 	/*
5510Sstevel@tonic-gate 	 * Code can be zero if this is a new LWP returning after a forkall(),
5520Sstevel@tonic-gate 	 * other than the one which matches the one in the parent which called
5530Sstevel@tonic-gate 	 * forkall().  In these LWPs, skip most of post-syscall activity.
5540Sstevel@tonic-gate 	 */
5550Sstevel@tonic-gate 	if (code == 0)
5560Sstevel@tonic-gate 		goto sig_check;
5570Sstevel@tonic-gate 
55811861SMarek.Pospisil@Sun.COM 	/* put out audit record for this syscall */
55911861SMarek.Pospisil@Sun.COM 	if (AU_AUDITING()) {
5600Sstevel@tonic-gate 		rval_t	rval;	/* fix audit_finish() someday */
5610Sstevel@tonic-gate 
5620Sstevel@tonic-gate 		/* XX64 -- truncation of 64-bit return values? */
5630Sstevel@tonic-gate 		rval.r_val1 = (int)rval1;
5640Sstevel@tonic-gate 		rval.r_val2 = (int)rval2;
5650Sstevel@tonic-gate 		audit_finish(T_SYSCALL, code, error, &rval);
5660Sstevel@tonic-gate 		repost = 1;
5670Sstevel@tonic-gate 	}
5680Sstevel@tonic-gate 
5690Sstevel@tonic-gate 	if (curthread->t_pdmsg != NULL) {
5700Sstevel@tonic-gate 		char *m = curthread->t_pdmsg;
5710Sstevel@tonic-gate 
5720Sstevel@tonic-gate 		uprintf("%s", m);
5730Sstevel@tonic-gate 		kmem_free(m, strlen(m) + 1);
5740Sstevel@tonic-gate 		curthread->t_pdmsg = NULL;
5750Sstevel@tonic-gate 	}
5760Sstevel@tonic-gate 
5770Sstevel@tonic-gate 	/*
5780Sstevel@tonic-gate 	 * If we're going to stop for /proc tracing, set the flag and
5790Sstevel@tonic-gate 	 * save the arguments so that the return values don't smash them.
5800Sstevel@tonic-gate 	 */
5810Sstevel@tonic-gate 	if (PTOU(p)->u_systrap) {
5820Sstevel@tonic-gate 		if (prismember(&PTOU(p)->u_exitmask, code)) {
5830Sstevel@tonic-gate 			proc_stop = 1;
5840Sstevel@tonic-gate 			(void) save_syscall_args();
5850Sstevel@tonic-gate 		}
5860Sstevel@tonic-gate 		repost = 1;
5870Sstevel@tonic-gate 	}
5880Sstevel@tonic-gate 
5890Sstevel@tonic-gate 	/*
5900Sstevel@tonic-gate 	 * Similarly check to see if SIGPROF might be sent.
5910Sstevel@tonic-gate 	 */
5920Sstevel@tonic-gate 	if (curthread->t_rprof != NULL &&
5930Sstevel@tonic-gate 	    curthread->t_rprof->rp_anystate != 0) {
5940Sstevel@tonic-gate 		(void) save_syscall_args();
5950Sstevel@tonic-gate 		sigprof = 1;
5960Sstevel@tonic-gate 	}
5970Sstevel@tonic-gate 
5980Sstevel@tonic-gate 	if (lwp->lwp_eosys == NORMALRETURN) {
5990Sstevel@tonic-gate 		if (error == 0) {
6000Sstevel@tonic-gate #ifdef SYSCALLTRACE
6010Sstevel@tonic-gate 			if (syscalltrace) {
6020Sstevel@tonic-gate 				mutex_enter(&systrace_lock);
6030Sstevel@tonic-gate 				printf(
6040Sstevel@tonic-gate 				    "%d: r_val1=0x%lx, r_val2=0x%lx, id 0x%p\n",
6050Sstevel@tonic-gate 				    p->p_pid, rval1, rval2, curthread);
6060Sstevel@tonic-gate 				mutex_exit(&systrace_lock);
6070Sstevel@tonic-gate 			}
6080Sstevel@tonic-gate #endif /* SYSCALLTRACE */
6090Sstevel@tonic-gate 			rp->r_tstate &= ~TSTATE_IC;
6100Sstevel@tonic-gate 			rp->r_o0 = rval1;
6110Sstevel@tonic-gate 			rp->r_o1 = rval2;
6120Sstevel@tonic-gate 		} else {
6130Sstevel@tonic-gate 			int sig;
6140Sstevel@tonic-gate 
6150Sstevel@tonic-gate #ifdef SYSCALLTRACE
6160Sstevel@tonic-gate 			if (syscalltrace) {
6170Sstevel@tonic-gate 				mutex_enter(&systrace_lock);
6180Sstevel@tonic-gate 				printf("%d: error=%d, id 0x%p\n",
6190Sstevel@tonic-gate 				    p->p_pid, error, curthread);
6200Sstevel@tonic-gate 				mutex_exit(&systrace_lock);
6210Sstevel@tonic-gate 			}
6220Sstevel@tonic-gate #endif /* SYSCALLTRACE */
6230Sstevel@tonic-gate 			if (error == EINTR && t->t_activefd.a_stale)
6240Sstevel@tonic-gate 				error = EBADF;
6250Sstevel@tonic-gate 			if (error == EINTR &&
6260Sstevel@tonic-gate 			    (sig = lwp->lwp_cursig) != 0 &&
6270Sstevel@tonic-gate 			    sigismember(&PTOU(p)->u_sigrestart, sig) &&
6280Sstevel@tonic-gate 			    PTOU(p)->u_signal[sig - 1] != SIG_DFL &&
6290Sstevel@tonic-gate 			    PTOU(p)->u_signal[sig - 1] != SIG_IGN)
6300Sstevel@tonic-gate 				error = ERESTART;
6310Sstevel@tonic-gate 			rp->r_o0 = error;
6320Sstevel@tonic-gate 			rp->r_tstate |= TSTATE_IC;
6330Sstevel@tonic-gate 		}
6340Sstevel@tonic-gate 		/*
6350Sstevel@tonic-gate 		 * The default action is to redo the trap instruction.
6360Sstevel@tonic-gate 		 * We increment the pc and npc past it for NORMALRETURN.
6370Sstevel@tonic-gate 		 * JUSTRETURN has set up a new pc and npc already.
638769Sraf 		 * If we are a cloned thread of forkall(), don't
639769Sraf 		 * adjust here because we have already inherited
640769Sraf 		 * the adjusted values from our clone.
6410Sstevel@tonic-gate 		 */
642769Sraf 		if (!(t->t_flag & T_FORKALL)) {
643769Sraf 			rp->r_pc = rp->r_npc;
644769Sraf 			rp->r_npc += 4;
645769Sraf 		}
6460Sstevel@tonic-gate 	}
6470Sstevel@tonic-gate 
6480Sstevel@tonic-gate 	/*
6490Sstevel@tonic-gate 	 * From the proc(4) manual page:
6500Sstevel@tonic-gate 	 * When exit from a system call is being traced, the traced process
6510Sstevel@tonic-gate 	 * stops on completion of the system call just prior to checking for
6520Sstevel@tonic-gate 	 * signals and returning to user level.  At this point all return
6530Sstevel@tonic-gate 	 * values have been stored into the traced process's saved registers.
6540Sstevel@tonic-gate 	 */
6550Sstevel@tonic-gate 	if (proc_stop) {
6560Sstevel@tonic-gate 		mutex_enter(&p->p_lock);
6570Sstevel@tonic-gate 		if (PTOU(p)->u_systrap &&
6580Sstevel@tonic-gate 		    prismember(&PTOU(p)->u_exitmask, code))
6590Sstevel@tonic-gate 			stop(PR_SYSEXIT, code);
6600Sstevel@tonic-gate 		mutex_exit(&p->p_lock);
6610Sstevel@tonic-gate 	}
6620Sstevel@tonic-gate 
6630Sstevel@tonic-gate 	/*
6640Sstevel@tonic-gate 	 * If we are the parent returning from a successful
6650Sstevel@tonic-gate 	 * vfork, wait for the child to exec or exit.
6660Sstevel@tonic-gate 	 * This code must be here and not in the bowels of the system
6670Sstevel@tonic-gate 	 * so that /proc can intercept exit from vfork in a timely way.
6680Sstevel@tonic-gate 	 */
6693828Sraf 	if (t->t_flag & T_VFPARENT) {
6703235Sraf 		ASSERT(code == SYS_vfork || code == SYS_forksys);
6713235Sraf 		ASSERT(rp->r_o1 == 0 && error == 0);
6720Sstevel@tonic-gate 		vfwait((pid_t)rval1);
6733828Sraf 		t->t_flag &= ~T_VFPARENT;
6743235Sraf 	}
6750Sstevel@tonic-gate 
6760Sstevel@tonic-gate 	/*
6770Sstevel@tonic-gate 	 * If profiling is active, bill the current PC in user-land
6780Sstevel@tonic-gate 	 * and keep reposting until profiling is disabled.
6790Sstevel@tonic-gate 	 */
6800Sstevel@tonic-gate 	if (p->p_prof.pr_scale) {
6810Sstevel@tonic-gate 		if (lwp->lwp_oweupc)
6820Sstevel@tonic-gate 			profil_tick(rp->r_pc);
6830Sstevel@tonic-gate 		repost = 1;
6840Sstevel@tonic-gate 	}
6850Sstevel@tonic-gate 
6860Sstevel@tonic-gate sig_check:
6870Sstevel@tonic-gate 	/*
6880Sstevel@tonic-gate 	 * Reset flag for next time.
6890Sstevel@tonic-gate 	 * We must do this after stopping on PR_SYSEXIT
6900Sstevel@tonic-gate 	 * because /proc uses the information in lwp_eosys.
6910Sstevel@tonic-gate 	 */
6920Sstevel@tonic-gate 	lwp->lwp_eosys = NORMALRETURN;
6930Sstevel@tonic-gate 	clear_stale_fd();
694769Sraf 	t->t_flag &= ~T_FORKALL;
6950Sstevel@tonic-gate 
6960Sstevel@tonic-gate 	if (t->t_astflag | t->t_sig_check) {
6970Sstevel@tonic-gate 		/*
6980Sstevel@tonic-gate 		 * Turn off the AST flag before checking all the conditions that
6990Sstevel@tonic-gate 		 * may have caused an AST.  This flag is on whenever a signal or
7000Sstevel@tonic-gate 		 * unusual condition should be handled after the next trap or
7010Sstevel@tonic-gate 		 * syscall.
7020Sstevel@tonic-gate 		 */
7030Sstevel@tonic-gate 		astoff(t);
7040Sstevel@tonic-gate 		t->t_sig_check = 0;
7050Sstevel@tonic-gate 
7061972Sdv142724 		/*
7071972Sdv142724 		 * The following check is legal for the following reasons:
7081972Sdv142724 		 *	1) The thread we are checking, is ourselves, so there is
7091972Sdv142724 		 *	   no way the proc can go away.
7101972Sdv142724 		 *	2) The only time we need to be protected by the
7111972Sdv142724 		 *	   lock is if the binding is changed.
7121972Sdv142724 		 *
7131972Sdv142724 		 *	Note we will still take the lock and check the binding
7141972Sdv142724 		 *	if the condition was true without the lock held.  This
7151972Sdv142724 		 *	prevents lock contention among threads owned by the
7161972Sdv142724 		 *	same proc.
7171972Sdv142724 		 */
7181972Sdv142724 
7190Sstevel@tonic-gate 		if (curthread->t_proc_flag & TP_CHANGEBIND) {
7201972Sdv142724 			mutex_enter(&p->p_lock);
7211972Sdv142724 			if (curthread->t_proc_flag & TP_CHANGEBIND) {
7221972Sdv142724 				timer_lwpbind();
7231972Sdv142724 				curthread->t_proc_flag &= ~TP_CHANGEBIND;
7241972Sdv142724 			}
7251972Sdv142724 			mutex_exit(&p->p_lock);
7260Sstevel@tonic-gate 		}
7270Sstevel@tonic-gate 
7280Sstevel@tonic-gate 		/*
7290Sstevel@tonic-gate 		 * for kaio requests on the special kaio poll queue,
7300Sstevel@tonic-gate 		 * copyout their results to user memory.
7310Sstevel@tonic-gate 		 */
7320Sstevel@tonic-gate 		if (p->p_aio)
7330Sstevel@tonic-gate 			aio_cleanup(0);
7340Sstevel@tonic-gate 
7350Sstevel@tonic-gate 		/*
7360Sstevel@tonic-gate 		 * If this LWP was asked to hold, call holdlwp(), which will
7370Sstevel@tonic-gate 		 * stop.  holdlwps() sets this up and calls pokelwps() which
7380Sstevel@tonic-gate 		 * sets the AST flag.
7390Sstevel@tonic-gate 		 *
7400Sstevel@tonic-gate 		 * Also check TP_EXITLWP, since this is used by fresh new LWPs
7410Sstevel@tonic-gate 		 * through lwp_rtt().  That flag is set if the lwp_create(2)
7420Sstevel@tonic-gate 		 * syscall failed after creating the LWP.
7430Sstevel@tonic-gate 		 */
7440Sstevel@tonic-gate 		if (ISHOLD(p) || (t->t_proc_flag & TP_EXITLWP))
7450Sstevel@tonic-gate 			holdlwp();
7460Sstevel@tonic-gate 
7470Sstevel@tonic-gate 		/*
7480Sstevel@tonic-gate 		 * All code that sets signals and makes ISSIG_PENDING
7490Sstevel@tonic-gate 		 * evaluate true must set t_sig_check afterwards.
7500Sstevel@tonic-gate 		 */
7510Sstevel@tonic-gate 		if (ISSIG_PENDING(t, lwp, p)) {
7520Sstevel@tonic-gate 			if (issig(FORREAL))
7530Sstevel@tonic-gate 				psig();
7540Sstevel@tonic-gate 			t->t_sig_check = 1;	/* recheck next time */
7550Sstevel@tonic-gate 		}
7560Sstevel@tonic-gate 
7570Sstevel@tonic-gate 		if (sigprof) {
7589870SRoger.Faulkner@Sun.COM 			int nargs = (code > 0 && code < NSYSCALL)?
7599870SRoger.Faulkner@Sun.COM 			    LWP_GETSYSENT(lwp)[code].sy_narg : 0;
7609870SRoger.Faulkner@Sun.COM 			realsigprof(code, nargs, error);
7610Sstevel@tonic-gate 			t->t_sig_check = 1;	/* recheck next time */
7620Sstevel@tonic-gate 		}
7630Sstevel@tonic-gate 
7640Sstevel@tonic-gate 		/*
7650Sstevel@tonic-gate 		 * If a performance counter overflow interrupt was
7660Sstevel@tonic-gate 		 * delivered *during* the syscall, then re-enable the
7670Sstevel@tonic-gate 		 * AST so that we take a trip through trap() to cause
7680Sstevel@tonic-gate 		 * the SIGEMT to be delivered.
7690Sstevel@tonic-gate 		 */
7700Sstevel@tonic-gate 		if (lwp->lwp_pcb.pcb_flags & CPC_OVERFLOW)
7710Sstevel@tonic-gate 			aston(t);
7720Sstevel@tonic-gate 
7730Sstevel@tonic-gate 		/*
7740Sstevel@tonic-gate 		 * If an asynchronous hardware error is pending, turn AST flag
7750Sstevel@tonic-gate 		 * back on.  AST will be checked again before we return to user
7760Sstevel@tonic-gate 		 * mode and we'll come back through trap() to handle the error.
7770Sstevel@tonic-gate 		 */
7780Sstevel@tonic-gate 		if (lwp->lwp_pcb.pcb_flags & ASYNC_HWERR)
7790Sstevel@tonic-gate 			aston(t);
7800Sstevel@tonic-gate 	}
7810Sstevel@tonic-gate 
7820Sstevel@tonic-gate 	/*
7830Sstevel@tonic-gate 	 * Restore register window if a debugger modified it.
7840Sstevel@tonic-gate 	 * Set up to perform a single-step if a debugger requested it.
7850Sstevel@tonic-gate 	 */
7860Sstevel@tonic-gate 	if (lwp->lwp_pcb.pcb_xregstat != XREGNONE)
7870Sstevel@tonic-gate 		xregrestore(lwp, 1);
7880Sstevel@tonic-gate 
7890Sstevel@tonic-gate 	lwp->lwp_errno = 0;		/* clear error for next time */
7900Sstevel@tonic-gate 
7910Sstevel@tonic-gate #ifndef NPROBE
7920Sstevel@tonic-gate 	/* Kernel probe */
7930Sstevel@tonic-gate 	if (tnf_tracing_active) {
7940Sstevel@tonic-gate 		TNF_PROBE_3(syscall_end, "syscall thread", /* CSTYLED */,
7955753Sgww 		    tnf_long,	rval1,		rval1,
7965753Sgww 		    tnf_long,	rval2,		rval2,
7975753Sgww 		    tnf_long,	errno,		(long)error);
7980Sstevel@tonic-gate 		repost = 1;
7990Sstevel@tonic-gate 	}
8000Sstevel@tonic-gate #endif /* NPROBE */
8010Sstevel@tonic-gate 
8020Sstevel@tonic-gate 	/*
8030Sstevel@tonic-gate 	 * Set state to LWP_USER here so preempt won't give us a kernel
8040Sstevel@tonic-gate 	 * priority if it occurs after this point.  Call CL_TRAPRET() to
8050Sstevel@tonic-gate 	 * restore the user-level priority.
8060Sstevel@tonic-gate 	 *
8070Sstevel@tonic-gate 	 * It is important that no locks (other than spinlocks) be entered
8080Sstevel@tonic-gate 	 * after this point before returning to user mode (unless lwp_state
8090Sstevel@tonic-gate 	 * is set back to LWP_SYS).
8100Sstevel@tonic-gate 	 *
8110Sstevel@tonic-gate 	 * Sampled times past this point are charged to the user.
8120Sstevel@tonic-gate 	 */
8130Sstevel@tonic-gate 	lwp->lwp_state = LWP_USER;
8140Sstevel@tonic-gate 
8150Sstevel@tonic-gate 	if (t->t_trapret) {
8160Sstevel@tonic-gate 		t->t_trapret = 0;
8170Sstevel@tonic-gate 		thread_lock(t);
8180Sstevel@tonic-gate 		CL_TRAPRET(t);
8190Sstevel@tonic-gate 		thread_unlock(t);
8200Sstevel@tonic-gate 	}
8213792Sakolb 	if (CPU->cpu_runrun || t->t_schedflag & TS_ANYWAITQ)
8220Sstevel@tonic-gate 		preempt();
82310230SRoger.Faulkner@Sun.COM 	prunstop();
8240Sstevel@tonic-gate 
8250Sstevel@tonic-gate 	/*
8260Sstevel@tonic-gate 	 * t_post_sys will be set if pcb_step is active.
8270Sstevel@tonic-gate 	 */
8280Sstevel@tonic-gate 	if (lwp->lwp_pcb.pcb_step != STEP_NONE) {
8290Sstevel@tonic-gate 		prdostep();
8300Sstevel@tonic-gate 		repost = 1;
8310Sstevel@tonic-gate 	}
8320Sstevel@tonic-gate 
833715Srab 	t->t_sysnum = 0;	/* no longer in a system call */
834715Srab 
8350Sstevel@tonic-gate 	/*
8360Sstevel@tonic-gate 	 * In case the args were copied to the lwp, reset the
8370Sstevel@tonic-gate 	 * pointer so the next syscall will have the right lwp_ap pointer.
8380Sstevel@tonic-gate 	 */
8390Sstevel@tonic-gate 	lwp->lwp_ap = (long *)&rp->r_o0;
8400Sstevel@tonic-gate 	lwp->lwp_argsaved = 0;
8410Sstevel@tonic-gate 
8420Sstevel@tonic-gate 	/*
8430Sstevel@tonic-gate 	 * If there was a continuing reason for post-syscall processing,
8440Sstevel@tonic-gate 	 * set the t_post_sys flag for the next system call.
8450Sstevel@tonic-gate 	 */
8460Sstevel@tonic-gate 	if (repost)
8470Sstevel@tonic-gate 		t->t_post_sys = 1;
8480Sstevel@tonic-gate 
8490Sstevel@tonic-gate 	/*
8500Sstevel@tonic-gate 	 * If there is a ustack registered for this lwp, and the stack rlimit
8510Sstevel@tonic-gate 	 * has been altered, read in the ustack. If the saved stack rlimit
8520Sstevel@tonic-gate 	 * matches the bounds of the ustack, update the ustack to reflect
8530Sstevel@tonic-gate 	 * the new rlimit. If the new stack rlimit is RLIM_INFINITY, disable
8540Sstevel@tonic-gate 	 * stack checking by setting the size to 0.
8550Sstevel@tonic-gate 	 */
8560Sstevel@tonic-gate 	if (lwp->lwp_ustack != 0 && lwp->lwp_old_stk_ctl != 0) {
8570Sstevel@tonic-gate 		rlim64_t new_size;
8580Sstevel@tonic-gate 		model_t model;
8590Sstevel@tonic-gate 		caddr_t top;
8600Sstevel@tonic-gate 		struct rlimit64 rl;
8610Sstevel@tonic-gate 
8620Sstevel@tonic-gate 		mutex_enter(&p->p_lock);
8630Sstevel@tonic-gate 		new_size = p->p_stk_ctl;
8640Sstevel@tonic-gate 		model = p->p_model;
8650Sstevel@tonic-gate 		top = p->p_usrstack;
8660Sstevel@tonic-gate 		(void) rctl_rlimit_get(rctlproc_legacy[RLIMIT_STACK], p, &rl);
8670Sstevel@tonic-gate 		mutex_exit(&p->p_lock);
8680Sstevel@tonic-gate 
8690Sstevel@tonic-gate 		if (rl.rlim_cur == RLIM64_INFINITY)
8700Sstevel@tonic-gate 			new_size = 0;
8710Sstevel@tonic-gate 
8720Sstevel@tonic-gate 		if (model == DATAMODEL_NATIVE) {
8730Sstevel@tonic-gate 			stack_t stk;
8740Sstevel@tonic-gate 
8750Sstevel@tonic-gate 			if (copyin((stack_t *)lwp->lwp_ustack, &stk,
8760Sstevel@tonic-gate 			    sizeof (stack_t)) == 0 &&
8770Sstevel@tonic-gate 			    (stk.ss_size == lwp->lwp_old_stk_ctl ||
8785753Sgww 			    stk.ss_size == 0) &&
8790Sstevel@tonic-gate 			    stk.ss_sp == top - stk.ss_size) {
8800Sstevel@tonic-gate 				stk.ss_sp = (void *)((uintptr_t)stk.ss_sp +
8810Sstevel@tonic-gate 				    stk.ss_size - new_size);
8820Sstevel@tonic-gate 				stk.ss_size = new_size;
8830Sstevel@tonic-gate 
8840Sstevel@tonic-gate 				(void) copyout(&stk,
8850Sstevel@tonic-gate 				    (stack_t *)lwp->lwp_ustack,
8860Sstevel@tonic-gate 				    sizeof (stack_t));
8870Sstevel@tonic-gate 			}
8880Sstevel@tonic-gate 		} else {
8890Sstevel@tonic-gate 			stack32_t stk32;
8900Sstevel@tonic-gate 
8910Sstevel@tonic-gate 			if (copyin((stack32_t *)lwp->lwp_ustack, &stk32,
8920Sstevel@tonic-gate 			    sizeof (stack32_t)) == 0 &&
8930Sstevel@tonic-gate 			    (stk32.ss_size == lwp->lwp_old_stk_ctl ||
8941048Sraf 			    stk32.ss_size == 0) &&
8951048Sraf 			    stk32.ss_sp ==
8961048Sraf 			    (caddr32_t)(uintptr_t)(top - stk32.ss_size)) {
8970Sstevel@tonic-gate 				stk32.ss_sp += stk32.ss_size - new_size;
8980Sstevel@tonic-gate 				stk32.ss_size = new_size;
8990Sstevel@tonic-gate 
9000Sstevel@tonic-gate 				(void) copyout(&stk32,
9010Sstevel@tonic-gate 				    (stack32_t *)lwp->lwp_ustack,
9020Sstevel@tonic-gate 				    sizeof (stack32_t));
9030Sstevel@tonic-gate 			}
9040Sstevel@tonic-gate 		}
9050Sstevel@tonic-gate 
9060Sstevel@tonic-gate 		lwp->lwp_old_stk_ctl = 0;
9070Sstevel@tonic-gate 	}
9080Sstevel@tonic-gate 
9090Sstevel@tonic-gate 	syscall_mstate(LMS_SYSTEM, LMS_USER);
9100Sstevel@tonic-gate }
9110Sstevel@tonic-gate 
9120Sstevel@tonic-gate /*
9130Sstevel@tonic-gate  * Call a system call which takes a pointer to the user args struct and
9140Sstevel@tonic-gate  * a pointer to the return values.  This is a bit slower than the standard
9150Sstevel@tonic-gate  * C arg-passing method in some cases.
9160Sstevel@tonic-gate  */
9170Sstevel@tonic-gate int64_t
syscall_ap()9180Sstevel@tonic-gate syscall_ap()
9190Sstevel@tonic-gate {
9200Sstevel@tonic-gate 	uint_t	error;
9210Sstevel@tonic-gate 	struct sysent *callp;
9220Sstevel@tonic-gate 	rval_t	rval;
9230Sstevel@tonic-gate 	klwp_t	*lwp = ttolwp(curthread);
9240Sstevel@tonic-gate 	struct regs *rp = lwptoregs(lwp);
9250Sstevel@tonic-gate 
9260Sstevel@tonic-gate 	callp = LWP_GETSYSENT(lwp) + curthread->t_sysnum;
9270Sstevel@tonic-gate 
9280Sstevel@tonic-gate 	/*
9290Sstevel@tonic-gate 	 * If the arguments don't fit in registers %o0 - o5, make sure they
9300Sstevel@tonic-gate 	 * have been copied to the lwp_arg array.
9310Sstevel@tonic-gate 	 */
9320Sstevel@tonic-gate 	if (callp->sy_narg > 6 && save_syscall_args())
9330Sstevel@tonic-gate 		return ((int64_t)set_errno(EFAULT));
9340Sstevel@tonic-gate 
9350Sstevel@tonic-gate 	rval.r_val1 = 0;
9360Sstevel@tonic-gate 	rval.r_val2 = (int)rp->r_o1;
9370Sstevel@tonic-gate 	lwp->lwp_error = 0;	/* for old drivers */
9380Sstevel@tonic-gate 	error = (*(callp->sy_call))(lwp->lwp_ap, &rval);
9390Sstevel@tonic-gate 	if (error)
9400Sstevel@tonic-gate 		return ((int64_t)set_errno(error));
9410Sstevel@tonic-gate 	return (rval.r_vals);
9420Sstevel@tonic-gate }
9430Sstevel@tonic-gate 
9440Sstevel@tonic-gate /*
9450Sstevel@tonic-gate  * Load system call module.
9460Sstevel@tonic-gate  *	Returns with pointer to held read lock for module.
9470Sstevel@tonic-gate  */
9480Sstevel@tonic-gate static krwlock_t *
lock_syscall(struct sysent * table,uint_t code)9490Sstevel@tonic-gate lock_syscall(struct sysent *table, uint_t code)
9500Sstevel@tonic-gate {
9510Sstevel@tonic-gate 	krwlock_t	*module_lock;
9520Sstevel@tonic-gate 	struct modctl	*modp;
9530Sstevel@tonic-gate 	int		id;
9540Sstevel@tonic-gate 	struct sysent   *callp;
9550Sstevel@tonic-gate 
9560Sstevel@tonic-gate 	module_lock = table[code].sy_lock;
9570Sstevel@tonic-gate 	callp = &table[code];
9580Sstevel@tonic-gate 
9590Sstevel@tonic-gate 	/*
9600Sstevel@tonic-gate 	 * Optimization to only call modload if we don't have a loaded
9610Sstevel@tonic-gate 	 * syscall.
9620Sstevel@tonic-gate 	 */
9630Sstevel@tonic-gate 	rw_enter(module_lock, RW_READER);
9640Sstevel@tonic-gate 	if (LOADED_SYSCALL(callp))
9650Sstevel@tonic-gate 		return (module_lock);
9660Sstevel@tonic-gate 	rw_exit(module_lock);
9670Sstevel@tonic-gate 
9680Sstevel@tonic-gate 	for (;;) {
9690Sstevel@tonic-gate 		if ((id = modload("sys", syscallnames[code])) == -1)
9700Sstevel@tonic-gate 			break;
9710Sstevel@tonic-gate 
9720Sstevel@tonic-gate 		/*
9730Sstevel@tonic-gate 		 * If we loaded successfully at least once, the modctl
9740Sstevel@tonic-gate 		 * will still be valid, so we try to grab it by filename.
9750Sstevel@tonic-gate 		 * If this call fails, it's because the mod_filename
9760Sstevel@tonic-gate 		 * was changed after the call to modload() (mod_hold_by_name()
9770Sstevel@tonic-gate 		 * is the likely culprit).  We can safely just take
9780Sstevel@tonic-gate 		 * another lap if this is the case;  the modload() will
9790Sstevel@tonic-gate 		 * change the mod_filename back to one by which we can
9800Sstevel@tonic-gate 		 * find the modctl.
9810Sstevel@tonic-gate 		 */
9820Sstevel@tonic-gate 		modp = mod_find_by_filename("sys", syscallnames[code]);
9830Sstevel@tonic-gate 
9840Sstevel@tonic-gate 		if (modp == NULL)
9850Sstevel@tonic-gate 			continue;
9860Sstevel@tonic-gate 
9870Sstevel@tonic-gate 		mutex_enter(&mod_lock);
9880Sstevel@tonic-gate 
9890Sstevel@tonic-gate 		if (!modp->mod_installed) {
9900Sstevel@tonic-gate 			mutex_exit(&mod_lock);
9910Sstevel@tonic-gate 			continue;
9920Sstevel@tonic-gate 		}
9930Sstevel@tonic-gate 		break;
9940Sstevel@tonic-gate 	}
9950Sstevel@tonic-gate 
9960Sstevel@tonic-gate 	rw_enter(module_lock, RW_READER);
9970Sstevel@tonic-gate 
9980Sstevel@tonic-gate 	if (id != -1)
9990Sstevel@tonic-gate 		mutex_exit(&mod_lock);
10000Sstevel@tonic-gate 
10010Sstevel@tonic-gate 	return (module_lock);
10020Sstevel@tonic-gate }
10030Sstevel@tonic-gate 
10040Sstevel@tonic-gate /*
10050Sstevel@tonic-gate  * Loadable syscall support.
10060Sstevel@tonic-gate  *	If needed, load the module, then reserve it by holding a read
10070Sstevel@tonic-gate  * 	lock for the duration of the call.
10080Sstevel@tonic-gate  *	Later, if the syscall is not unloadable, it could patch the vector.
10090Sstevel@tonic-gate  */
10100Sstevel@tonic-gate /*ARGSUSED*/
10110Sstevel@tonic-gate int64_t
loadable_syscall(long a0,long a1,long a2,long a3,long a4,long a5,long a6,long a7)10120Sstevel@tonic-gate loadable_syscall(
10130Sstevel@tonic-gate     long a0, long a1, long a2, long a3,
10140Sstevel@tonic-gate     long a4, long a5, long a6, long a7)
10150Sstevel@tonic-gate {
10160Sstevel@tonic-gate 	int64_t		rval;
10170Sstevel@tonic-gate 	struct sysent	*callp;
10180Sstevel@tonic-gate 	struct sysent	*se = LWP_GETSYSENT(ttolwp(curthread));
10190Sstevel@tonic-gate 	krwlock_t	*module_lock;
10200Sstevel@tonic-gate 	int		code;
10210Sstevel@tonic-gate 
10220Sstevel@tonic-gate 	code = curthread->t_sysnum;
10230Sstevel@tonic-gate 	callp = se + code;
10240Sstevel@tonic-gate 
10250Sstevel@tonic-gate 	/*
10260Sstevel@tonic-gate 	 * Try to autoload the system call if necessary.
10270Sstevel@tonic-gate 	 */
10280Sstevel@tonic-gate 	module_lock = lock_syscall(se, code);
10290Sstevel@tonic-gate 	THREAD_KPRI_RELEASE();	/* drop priority given by rw_enter */
10300Sstevel@tonic-gate 
10310Sstevel@tonic-gate 	/*
10320Sstevel@tonic-gate 	 * we've locked either the loaded syscall or nosys
10330Sstevel@tonic-gate 	 */
10340Sstevel@tonic-gate 	if (callp->sy_flags & SE_ARGC) {
10350Sstevel@tonic-gate 		int64_t (*sy_call)();
10360Sstevel@tonic-gate 
10370Sstevel@tonic-gate 		sy_call = (int64_t (*)())callp->sy_call;
10380Sstevel@tonic-gate 		rval = (*sy_call)(a0, a1, a2, a3, a4, a5);
10390Sstevel@tonic-gate 	} else {
10400Sstevel@tonic-gate 		rval = syscall_ap();
10410Sstevel@tonic-gate 	}
10420Sstevel@tonic-gate 
10430Sstevel@tonic-gate 	THREAD_KPRI_REQUEST();	/* regain priority from read lock */
10440Sstevel@tonic-gate 	rw_exit(module_lock);
10450Sstevel@tonic-gate 	return (rval);
10460Sstevel@tonic-gate }
10470Sstevel@tonic-gate 
10480Sstevel@tonic-gate /*
10490Sstevel@tonic-gate  * Handle indirect system calls.
10500Sstevel@tonic-gate  *	This interface should be deprecated.  The library can handle
10510Sstevel@tonic-gate  *	this more efficiently, but keep this implementation for old binaries.
10520Sstevel@tonic-gate  *
10530Sstevel@tonic-gate  * XX64	Needs some work.
10540Sstevel@tonic-gate  */
10550Sstevel@tonic-gate int64_t
indir(int code,long a0,long a1,long a2,long a3,long a4)10560Sstevel@tonic-gate indir(int code, long a0, long a1, long a2, long a3, long a4)
10570Sstevel@tonic-gate {
10580Sstevel@tonic-gate 	klwp_t		*lwp = ttolwp(curthread);
10590Sstevel@tonic-gate 	struct sysent	*callp;
10600Sstevel@tonic-gate 
10610Sstevel@tonic-gate 	if (code <= 0 || code >= NSYSCALL)
10620Sstevel@tonic-gate 		return (nosys());
10630Sstevel@tonic-gate 
10640Sstevel@tonic-gate 	ASSERT(lwp->lwp_ap != NULL);
10650Sstevel@tonic-gate 
10660Sstevel@tonic-gate 	curthread->t_sysnum = code;
10670Sstevel@tonic-gate 	callp = LWP_GETSYSENT(lwp) + code;
10680Sstevel@tonic-gate 
10690Sstevel@tonic-gate 	/*
10700Sstevel@tonic-gate 	 * Handle argument setup, unless already done in pre_syscall().
10710Sstevel@tonic-gate 	 */
10720Sstevel@tonic-gate 	if (callp->sy_narg > 5) {
10730Sstevel@tonic-gate 		if (save_syscall_args()) 	/* move args to LWP array */
10740Sstevel@tonic-gate 			return ((int64_t)set_errno(EFAULT));
10750Sstevel@tonic-gate 	} else if (!lwp->lwp_argsaved) {
10760Sstevel@tonic-gate 		long *ap;
10770Sstevel@tonic-gate 
10780Sstevel@tonic-gate 		ap = lwp->lwp_ap;		/* args haven't been saved */
10790Sstevel@tonic-gate 		lwp->lwp_ap = ap + 1;		/* advance arg pointer */
10800Sstevel@tonic-gate 		curthread->t_post_sys = 1;	/* so lwp_ap will be reset */
10810Sstevel@tonic-gate 	}
10820Sstevel@tonic-gate 	return ((*callp->sy_callc)(a0, a1, a2, a3, a4, lwp->lwp_arg[5]));
10830Sstevel@tonic-gate }
10840Sstevel@tonic-gate 
10850Sstevel@tonic-gate /*
10860Sstevel@tonic-gate  * set_errno - set an error return from the current system call.
10870Sstevel@tonic-gate  *	This could be a macro.
10880Sstevel@tonic-gate  *	This returns the value it is passed, so that the caller can
10890Sstevel@tonic-gate  *	use tail-recursion-elimination and do return (set_errno(ERRNO));
10900Sstevel@tonic-gate  */
10910Sstevel@tonic-gate uint_t
set_errno(uint_t error)10920Sstevel@tonic-gate set_errno(uint_t error)
10930Sstevel@tonic-gate {
10940Sstevel@tonic-gate 	ASSERT(error != 0);		/* must not be used to clear errno */
10950Sstevel@tonic-gate 
10960Sstevel@tonic-gate 	curthread->t_post_sys = 1;	/* have post_syscall do error return */
10970Sstevel@tonic-gate 	return (ttolwp(curthread)->lwp_errno = error);
10980Sstevel@tonic-gate }
10990Sstevel@tonic-gate 
11000Sstevel@tonic-gate /*
11010Sstevel@tonic-gate  * set_proc_pre_sys - Set pre-syscall processing for entire process.
11020Sstevel@tonic-gate  */
11030Sstevel@tonic-gate void
set_proc_pre_sys(proc_t * p)11040Sstevel@tonic-gate set_proc_pre_sys(proc_t *p)
11050Sstevel@tonic-gate {
11060Sstevel@tonic-gate 	kthread_t	*t;
11070Sstevel@tonic-gate 	kthread_t	*first;
11080Sstevel@tonic-gate 
11090Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&p->p_lock));
11100Sstevel@tonic-gate 
11110Sstevel@tonic-gate 	t = first = p->p_tlist;
11120Sstevel@tonic-gate 	do {
11130Sstevel@tonic-gate 		t->t_pre_sys = 1;
11140Sstevel@tonic-gate 	} while ((t = t->t_forw) != first);
11150Sstevel@tonic-gate }
11160Sstevel@tonic-gate 
11170Sstevel@tonic-gate /*
11180Sstevel@tonic-gate  * set_proc_post_sys - Set post-syscall processing for entire process.
11190Sstevel@tonic-gate  */
11200Sstevel@tonic-gate void
set_proc_post_sys(proc_t * p)11210Sstevel@tonic-gate set_proc_post_sys(proc_t *p)
11220Sstevel@tonic-gate {
11230Sstevel@tonic-gate 	kthread_t	*t;
11240Sstevel@tonic-gate 	kthread_t	*first;
11250Sstevel@tonic-gate 
11260Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&p->p_lock));
11270Sstevel@tonic-gate 
11280Sstevel@tonic-gate 	t = first = p->p_tlist;
11290Sstevel@tonic-gate 	do {
11300Sstevel@tonic-gate 		t->t_post_sys = 1;
11310Sstevel@tonic-gate 	} while ((t = t->t_forw) != first);
11320Sstevel@tonic-gate }
11330Sstevel@tonic-gate 
11340Sstevel@tonic-gate /*
11350Sstevel@tonic-gate  * set_proc_sys - Set pre- and post-syscall processing for entire process.
11360Sstevel@tonic-gate  */
11370Sstevel@tonic-gate void
set_proc_sys(proc_t * p)11380Sstevel@tonic-gate set_proc_sys(proc_t *p)
11390Sstevel@tonic-gate {
11400Sstevel@tonic-gate 	kthread_t	*t;
11410Sstevel@tonic-gate 	kthread_t	*first;
11420Sstevel@tonic-gate 
11430Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&p->p_lock));
11440Sstevel@tonic-gate 
11450Sstevel@tonic-gate 	t = first = p->p_tlist;
11460Sstevel@tonic-gate 	do {
11470Sstevel@tonic-gate 		t->t_pre_sys = 1;
11480Sstevel@tonic-gate 		t->t_post_sys = 1;
11490Sstevel@tonic-gate 	} while ((t = t->t_forw) != first);
11500Sstevel@tonic-gate }
11510Sstevel@tonic-gate 
11520Sstevel@tonic-gate /*
11530Sstevel@tonic-gate  * set_all_proc_sys - set pre- and post-syscall processing flags for all
11540Sstevel@tonic-gate  * user processes.
11550Sstevel@tonic-gate  *
11560Sstevel@tonic-gate  * This is needed when auditing, tracing, or other facilities which affect
11570Sstevel@tonic-gate  * all processes are turned on.
11580Sstevel@tonic-gate  */
11590Sstevel@tonic-gate void
set_all_proc_sys()11600Sstevel@tonic-gate set_all_proc_sys()
11610Sstevel@tonic-gate {
11620Sstevel@tonic-gate 	kthread_t	*t;
11630Sstevel@tonic-gate 	kthread_t	*first;
11640Sstevel@tonic-gate 
11650Sstevel@tonic-gate 	mutex_enter(&pidlock);
11660Sstevel@tonic-gate 	t = first = curthread;
11670Sstevel@tonic-gate 	do {
11680Sstevel@tonic-gate 		t->t_pre_sys = 1;
11690Sstevel@tonic-gate 		t->t_post_sys = 1;
11700Sstevel@tonic-gate 	} while ((t = t->t_next) != first);
11710Sstevel@tonic-gate 	mutex_exit(&pidlock);
11720Sstevel@tonic-gate }
11730Sstevel@tonic-gate 
11740Sstevel@tonic-gate /*
117511861SMarek.Pospisil@Sun.COM  * set_all_zone_usr_proc_sys - set pre- and post-syscall processing flags for
117611861SMarek.Pospisil@Sun.COM  * all user processes running in the zone of the current process
117711861SMarek.Pospisil@Sun.COM  *
117811861SMarek.Pospisil@Sun.COM  * This is needed when auditing is turned on.
117911861SMarek.Pospisil@Sun.COM  */
118011861SMarek.Pospisil@Sun.COM void
set_all_zone_usr_proc_sys(zoneid_t zoneid)118111861SMarek.Pospisil@Sun.COM set_all_zone_usr_proc_sys(zoneid_t zoneid)
118211861SMarek.Pospisil@Sun.COM {
118311861SMarek.Pospisil@Sun.COM 	proc_t	    *p;
118411861SMarek.Pospisil@Sun.COM 	kthread_t   *t;
118511861SMarek.Pospisil@Sun.COM 
118611861SMarek.Pospisil@Sun.COM 	mutex_enter(&pidlock);
118711861SMarek.Pospisil@Sun.COM 	for (p = practive; p != NULL; p = p->p_next) {
118811861SMarek.Pospisil@Sun.COM 		/* skip kernel processes */
118911861SMarek.Pospisil@Sun.COM 		if (p->p_exec == NULLVP || p->p_as == &kas ||
119011861SMarek.Pospisil@Sun.COM 		    p->p_stat == SIDL || p->p_stat == SZOMB ||
119111861SMarek.Pospisil@Sun.COM 		    (p->p_flag & (SSYS | SEXITING | SEXITLWPS)))
119211861SMarek.Pospisil@Sun.COM 			continue;
119311861SMarek.Pospisil@Sun.COM 		/*
119411861SMarek.Pospisil@Sun.COM 		 * Only processes in the given zone (eventually in
119511861SMarek.Pospisil@Sun.COM 		 * all zones) are taken into account
119611861SMarek.Pospisil@Sun.COM 		 */
119711861SMarek.Pospisil@Sun.COM 		if (zoneid == ALL_ZONES || p->p_zone->zone_id == zoneid) {
119811861SMarek.Pospisil@Sun.COM 			mutex_enter(&p->p_lock);
1199*13070SEthindra.Ramamurthy@Sun.COM 			if ((t = p->p_tlist) == NULL) {
1200*13070SEthindra.Ramamurthy@Sun.COM 				mutex_exit(&p->p_lock);
120111861SMarek.Pospisil@Sun.COM 				continue;
1202*13070SEthindra.Ramamurthy@Sun.COM 			}
120311861SMarek.Pospisil@Sun.COM 			/*
120411861SMarek.Pospisil@Sun.COM 			 * Set pre- and post-syscall processing flags
120511861SMarek.Pospisil@Sun.COM 			 * for all threads of the process
120611861SMarek.Pospisil@Sun.COM 			 */
120711861SMarek.Pospisil@Sun.COM 			do {
120811861SMarek.Pospisil@Sun.COM 				t->t_pre_sys = 1;
120911861SMarek.Pospisil@Sun.COM 				t->t_post_sys = 1;
121011861SMarek.Pospisil@Sun.COM 			} while (p->p_tlist != (t = t->t_forw));
121111861SMarek.Pospisil@Sun.COM 			mutex_exit(&p->p_lock);
121211861SMarek.Pospisil@Sun.COM 		}
121311861SMarek.Pospisil@Sun.COM 	}
121411861SMarek.Pospisil@Sun.COM 	mutex_exit(&pidlock);
121511861SMarek.Pospisil@Sun.COM }
121611861SMarek.Pospisil@Sun.COM 
121711861SMarek.Pospisil@Sun.COM /*
12180Sstevel@tonic-gate  * set_proc_ast - Set asynchronous service trap (AST) flag for all
12190Sstevel@tonic-gate  * threads in process.
12200Sstevel@tonic-gate  */
12210Sstevel@tonic-gate void
set_proc_ast(proc_t * p)12220Sstevel@tonic-gate set_proc_ast(proc_t *p)
12230Sstevel@tonic-gate {
12240Sstevel@tonic-gate 	kthread_t	*t;
12250Sstevel@tonic-gate 	kthread_t	*first;
12260Sstevel@tonic-gate 
12270Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&p->p_lock));
12280Sstevel@tonic-gate 
12290Sstevel@tonic-gate 	t = first = p->p_tlist;
12300Sstevel@tonic-gate 	do {
12310Sstevel@tonic-gate 		aston(t);
12320Sstevel@tonic-gate 	} while ((t = t->t_forw) != first);
12330Sstevel@tonic-gate }
1234