xref: /onnv-gate/usr/src/uts/sparc/os/syscall.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*0Sstevel@tonic-gate 
29*0Sstevel@tonic-gate #include <sys/param.h>
30*0Sstevel@tonic-gate #include <sys/vmparam.h>
31*0Sstevel@tonic-gate #include <sys/types.h>
32*0Sstevel@tonic-gate #include <sys/sysmacros.h>
33*0Sstevel@tonic-gate #include <sys/systm.h>
34*0Sstevel@tonic-gate #include <sys/cmn_err.h>
35*0Sstevel@tonic-gate #include <sys/signal.h>
36*0Sstevel@tonic-gate #include <sys/stack.h>
37*0Sstevel@tonic-gate #include <sys/cred.h>
38*0Sstevel@tonic-gate #include <sys/user.h>
39*0Sstevel@tonic-gate #include <sys/debug.h>
40*0Sstevel@tonic-gate #include <sys/errno.h>
41*0Sstevel@tonic-gate #include <sys/proc.h>
42*0Sstevel@tonic-gate #include <sys/var.h>
43*0Sstevel@tonic-gate #include <sys/inline.h>
44*0Sstevel@tonic-gate #include <sys/syscall.h>
45*0Sstevel@tonic-gate #include <sys/ucontext.h>
46*0Sstevel@tonic-gate #include <sys/cpuvar.h>
47*0Sstevel@tonic-gate #include <sys/siginfo.h>
48*0Sstevel@tonic-gate #include <sys/trap.h>
49*0Sstevel@tonic-gate #include <sys/machtrap.h>
50*0Sstevel@tonic-gate #include <sys/sysinfo.h>
51*0Sstevel@tonic-gate #include <sys/procfs.h>
52*0Sstevel@tonic-gate #include <sys/prsystm.h>
53*0Sstevel@tonic-gate #include <sys/fpu/fpusystm.h>
54*0Sstevel@tonic-gate #include <sys/modctl.h>
55*0Sstevel@tonic-gate #include <sys/aio_impl.h>
56*0Sstevel@tonic-gate #include <c2/audit.h>
57*0Sstevel@tonic-gate #include <sys/tnf.h>
58*0Sstevel@tonic-gate #include <sys/tnf_probe.h>
59*0Sstevel@tonic-gate #include <sys/machpcb.h>
60*0Sstevel@tonic-gate #include <sys/privregs.h>
61*0Sstevel@tonic-gate #include <sys/copyops.h>
62*0Sstevel@tonic-gate #include <sys/timer.h>
63*0Sstevel@tonic-gate #include <sys/priv.h>
64*0Sstevel@tonic-gate #include <sys/msacct.h>
65*0Sstevel@tonic-gate 
66*0Sstevel@tonic-gate int syscalltrace = 0;
67*0Sstevel@tonic-gate #ifdef SYSCALLTRACE
68*0Sstevel@tonic-gate static kmutex_t	systrace_lock;		/* syscall tracing lock */
69*0Sstevel@tonic-gate #endif /* SYSCALLTRACE */
70*0Sstevel@tonic-gate 
71*0Sstevel@tonic-gate static krwlock_t *lock_syscall(struct sysent *, uint_t);
72*0Sstevel@tonic-gate 
73*0Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
74*0Sstevel@tonic-gate static struct sysent *
75*0Sstevel@tonic-gate lwp_getsysent(klwp_t *lwp)
76*0Sstevel@tonic-gate {
77*0Sstevel@tonic-gate 	if (lwp_getdatamodel(lwp) == DATAMODEL_NATIVE)
78*0Sstevel@tonic-gate 		return (sysent);
79*0Sstevel@tonic-gate 	return (sysent32);
80*0Sstevel@tonic-gate }
81*0Sstevel@tonic-gate #define	LWP_GETSYSENT(lwp)	(lwp_getsysent(lwp))
82*0Sstevel@tonic-gate #else
83*0Sstevel@tonic-gate #define	LWP_GETSYSENT(lwp)	(sysent)
84*0Sstevel@tonic-gate #endif
85*0Sstevel@tonic-gate 
86*0Sstevel@tonic-gate /*
87*0Sstevel@tonic-gate  * Arrange for the real time profiling signal to be dispatched.
88*0Sstevel@tonic-gate  */
89*0Sstevel@tonic-gate void
90*0Sstevel@tonic-gate realsigprof(int sysnum, int error)
91*0Sstevel@tonic-gate {
92*0Sstevel@tonic-gate 	proc_t *p;
93*0Sstevel@tonic-gate 	klwp_t *lwp;
94*0Sstevel@tonic-gate 
95*0Sstevel@tonic-gate 	if (curthread->t_rprof->rp_anystate == 0)
96*0Sstevel@tonic-gate 		return;
97*0Sstevel@tonic-gate 	p = ttoproc(curthread);
98*0Sstevel@tonic-gate 	lwp = ttolwp(curthread);
99*0Sstevel@tonic-gate 	mutex_enter(&p->p_lock);
100*0Sstevel@tonic-gate 	if (sigismember(&p->p_ignore, SIGPROF) ||
101*0Sstevel@tonic-gate 	    signal_is_blocked(curthread, SIGPROF)) {
102*0Sstevel@tonic-gate 		mutex_exit(&p->p_lock);
103*0Sstevel@tonic-gate 		return;
104*0Sstevel@tonic-gate 	}
105*0Sstevel@tonic-gate 	lwp->lwp_siginfo.si_signo = SIGPROF;
106*0Sstevel@tonic-gate 	lwp->lwp_siginfo.si_code = PROF_SIG;
107*0Sstevel@tonic-gate 	lwp->lwp_siginfo.si_errno = error;
108*0Sstevel@tonic-gate 	hrt2ts(gethrtime(), &lwp->lwp_siginfo.si_tstamp);
109*0Sstevel@tonic-gate 	lwp->lwp_siginfo.si_syscall = sysnum;
110*0Sstevel@tonic-gate 	lwp->lwp_siginfo.si_nsysarg = (sysnum > 0 && sysnum < NSYSCALL) ?
111*0Sstevel@tonic-gate 		LWP_GETSYSENT(lwp)[sysnum].sy_narg : 0;
112*0Sstevel@tonic-gate 	lwp->lwp_siginfo.si_fault = lwp->lwp_lastfault;
113*0Sstevel@tonic-gate 	lwp->lwp_siginfo.si_faddr = lwp->lwp_lastfaddr;
114*0Sstevel@tonic-gate 	lwp->lwp_lastfault = 0;
115*0Sstevel@tonic-gate 	lwp->lwp_lastfaddr = NULL;
116*0Sstevel@tonic-gate 	sigtoproc(p, curthread, SIGPROF);
117*0Sstevel@tonic-gate 	mutex_exit(&p->p_lock);
118*0Sstevel@tonic-gate 	ASSERT(lwp->lwp_cursig == 0);
119*0Sstevel@tonic-gate 	if (issig(FORREAL)) {
120*0Sstevel@tonic-gate 		psig();
121*0Sstevel@tonic-gate 	}
122*0Sstevel@tonic-gate 	mutex_enter(&p->p_lock);
123*0Sstevel@tonic-gate 	lwp->lwp_siginfo.si_signo = 0;
124*0Sstevel@tonic-gate 	bzero(curthread->t_rprof, sizeof (*curthread->t_rprof));
125*0Sstevel@tonic-gate 	mutex_exit(&p->p_lock);
126*0Sstevel@tonic-gate }
127*0Sstevel@tonic-gate 
128*0Sstevel@tonic-gate /*
129*0Sstevel@tonic-gate  * Called to restore the lwp's register window just before
130*0Sstevel@tonic-gate  * returning to user level (only if the registers have been
131*0Sstevel@tonic-gate  * fetched or modified through /proc).
132*0Sstevel@tonic-gate  */
133*0Sstevel@tonic-gate /*ARGSUSED1*/
134*0Sstevel@tonic-gate void
135*0Sstevel@tonic-gate xregrestore(klwp_t *lwp, int shared)
136*0Sstevel@tonic-gate {
137*0Sstevel@tonic-gate 	/*
138*0Sstevel@tonic-gate 	 * If locals+ins were modified by /proc copy them out.
139*0Sstevel@tonic-gate 	 * Also copy to the shared window, if necessary.
140*0Sstevel@tonic-gate 	 */
141*0Sstevel@tonic-gate 	if (lwp->lwp_pcb.pcb_xregstat == XREGMODIFIED) {
142*0Sstevel@tonic-gate 		struct machpcb *mpcb = lwptompcb(lwp);
143*0Sstevel@tonic-gate 		caddr_t sp = (caddr_t)lwptoregs(lwp)->r_sp;
144*0Sstevel@tonic-gate 
145*0Sstevel@tonic-gate 		size_t rwinsize;
146*0Sstevel@tonic-gate 		caddr_t rwp;
147*0Sstevel@tonic-gate 		int is64;
148*0Sstevel@tonic-gate 
149*0Sstevel@tonic-gate 		if (lwp_getdatamodel(lwp) == DATAMODEL_LP64) {
150*0Sstevel@tonic-gate 			rwinsize = sizeof (struct rwindow);
151*0Sstevel@tonic-gate 			rwp = sp + STACK_BIAS;
152*0Sstevel@tonic-gate 			is64 = 1;
153*0Sstevel@tonic-gate 		} else {
154*0Sstevel@tonic-gate 			rwinsize = sizeof (struct rwindow32);
155*0Sstevel@tonic-gate 			sp = (caddr_t)(caddr32_t)sp;
156*0Sstevel@tonic-gate 			rwp = sp;
157*0Sstevel@tonic-gate 			is64 = 0;
158*0Sstevel@tonic-gate 		}
159*0Sstevel@tonic-gate 
160*0Sstevel@tonic-gate 		if (is64)
161*0Sstevel@tonic-gate 			(void) copyout_nowatch(&lwp->lwp_pcb.pcb_xregs,
162*0Sstevel@tonic-gate 				rwp, rwinsize);
163*0Sstevel@tonic-gate 		else {
164*0Sstevel@tonic-gate 			struct rwindow32 rwindow32;
165*0Sstevel@tonic-gate 			int watched;
166*0Sstevel@tonic-gate 
167*0Sstevel@tonic-gate 			watched = watch_disable_addr(rwp, rwinsize, S_WRITE);
168*0Sstevel@tonic-gate 			rwindow_nto32(&lwp->lwp_pcb.pcb_xregs, &rwindow32);
169*0Sstevel@tonic-gate 			(void) copyout(&rwindow32, rwp, rwinsize);
170*0Sstevel@tonic-gate 			if (watched)
171*0Sstevel@tonic-gate 				watch_enable_addr(rwp, rwinsize, S_WRITE);
172*0Sstevel@tonic-gate 		}
173*0Sstevel@tonic-gate 
174*0Sstevel@tonic-gate 		/* also copy to the user return window */
175*0Sstevel@tonic-gate 		mpcb->mpcb_rsp[0] = sp;
176*0Sstevel@tonic-gate 		mpcb->mpcb_rsp[1] = NULL;
177*0Sstevel@tonic-gate 		bcopy(&lwp->lwp_pcb.pcb_xregs, &mpcb->mpcb_rwin[0],
178*0Sstevel@tonic-gate 			sizeof (lwp->lwp_pcb.pcb_xregs));
179*0Sstevel@tonic-gate 	}
180*0Sstevel@tonic-gate 	lwp->lwp_pcb.pcb_xregstat = XREGNONE;
181*0Sstevel@tonic-gate }
182*0Sstevel@tonic-gate 
183*0Sstevel@tonic-gate 
184*0Sstevel@tonic-gate /*
185*0Sstevel@tonic-gate  * Get the arguments to the current system call.
186*0Sstevel@tonic-gate  *	lwp->lwp_ap normally points to the out regs in the reg structure.
187*0Sstevel@tonic-gate  *	If the user is going to change the out registers and might want to
188*0Sstevel@tonic-gate  *	get the args (for /proc tracing), it must copy the args elsewhere
189*0Sstevel@tonic-gate  *	via save_syscall_args().
190*0Sstevel@tonic-gate  */
191*0Sstevel@tonic-gate uint_t
192*0Sstevel@tonic-gate get_syscall_args(klwp_t *lwp, long *argp, int *nargsp)
193*0Sstevel@tonic-gate {
194*0Sstevel@tonic-gate 	kthread_t	*t = lwptot(lwp);
195*0Sstevel@tonic-gate 	uint_t	code = t->t_sysnum;
196*0Sstevel@tonic-gate 	long	mask;
197*0Sstevel@tonic-gate 	long	*ap;
198*0Sstevel@tonic-gate 	int	nargs;
199*0Sstevel@tonic-gate 
200*0Sstevel@tonic-gate 	if (lwptoproc(lwp)->p_model == DATAMODEL_ILP32)
201*0Sstevel@tonic-gate 		mask = (uint32_t)0xffffffffU;
202*0Sstevel@tonic-gate 	else
203*0Sstevel@tonic-gate 		mask = 0xffffffffffffffff;
204*0Sstevel@tonic-gate 
205*0Sstevel@tonic-gate 	if (code != 0 && code < NSYSCALL) {
206*0Sstevel@tonic-gate 
207*0Sstevel@tonic-gate 		nargs = LWP_GETSYSENT(lwp)[code].sy_narg;
208*0Sstevel@tonic-gate 
209*0Sstevel@tonic-gate 		ASSERT(nargs <= MAXSYSARGS);
210*0Sstevel@tonic-gate 
211*0Sstevel@tonic-gate 		*nargsp = nargs;
212*0Sstevel@tonic-gate 		ap = lwp->lwp_ap;
213*0Sstevel@tonic-gate 		while (nargs-- > 0)
214*0Sstevel@tonic-gate 			*argp++ = *ap++ & mask;
215*0Sstevel@tonic-gate 	} else {
216*0Sstevel@tonic-gate 		*nargsp = 0;
217*0Sstevel@tonic-gate 	}
218*0Sstevel@tonic-gate 	return (code);
219*0Sstevel@tonic-gate }
220*0Sstevel@tonic-gate 
221*0Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
222*0Sstevel@tonic-gate /*
223*0Sstevel@tonic-gate  * Get the arguments to the current 32-bit system call.
224*0Sstevel@tonic-gate  */
225*0Sstevel@tonic-gate uint_t
226*0Sstevel@tonic-gate get_syscall32_args(klwp_t *lwp, int *argp, int *nargsp)
227*0Sstevel@tonic-gate {
228*0Sstevel@tonic-gate 	long args[MAXSYSARGS];
229*0Sstevel@tonic-gate 	uint_t i, code;
230*0Sstevel@tonic-gate 
231*0Sstevel@tonic-gate 	code = get_syscall_args(lwp, args, nargsp);
232*0Sstevel@tonic-gate 	for (i = 0; i != *nargsp; i++)
233*0Sstevel@tonic-gate 		*argp++ = (int)args[i];
234*0Sstevel@tonic-gate 	return (code);
235*0Sstevel@tonic-gate }
236*0Sstevel@tonic-gate #endif
237*0Sstevel@tonic-gate 
238*0Sstevel@tonic-gate /*
239*0Sstevel@tonic-gate  * 	Save the system call arguments in a safe place.
240*0Sstevel@tonic-gate  *	lwp->lwp_ap normally points to the out regs in the reg structure.
241*0Sstevel@tonic-gate  *	If the user is going to change the out registers, g1, or the stack,
242*0Sstevel@tonic-gate  *	and might want to get the args (for /proc tracing), it must copy
243*0Sstevel@tonic-gate  *	the args elsewhere via save_syscall_args().
244*0Sstevel@tonic-gate  *
245*0Sstevel@tonic-gate  *	This may be called from stop() even when we're not in a system call.
246*0Sstevel@tonic-gate  *	Since there's no easy way to tell, this must be safe (not panic).
247*0Sstevel@tonic-gate  *	If the copyins get data faults, return non-zero.
248*0Sstevel@tonic-gate  */
249*0Sstevel@tonic-gate int
250*0Sstevel@tonic-gate save_syscall_args()
251*0Sstevel@tonic-gate {
252*0Sstevel@tonic-gate 	kthread_t	*t = curthread;
253*0Sstevel@tonic-gate 	klwp_t		*lwp = ttolwp(t);
254*0Sstevel@tonic-gate 	struct regs	*rp = lwptoregs(lwp);
255*0Sstevel@tonic-gate 	uint_t		code = t->t_sysnum;
256*0Sstevel@tonic-gate 	uint_t		nargs;
257*0Sstevel@tonic-gate 	int		i;
258*0Sstevel@tonic-gate 	caddr_t		ua;
259*0Sstevel@tonic-gate 	model_t		datamodel;
260*0Sstevel@tonic-gate 
261*0Sstevel@tonic-gate 	if (lwp->lwp_argsaved || code == 0)
262*0Sstevel@tonic-gate 		return (0);		/* args already saved or not needed */
263*0Sstevel@tonic-gate 
264*0Sstevel@tonic-gate 	if (code >= NSYSCALL) {
265*0Sstevel@tonic-gate 		nargs = 0;		/* illegal syscall */
266*0Sstevel@tonic-gate 	} else {
267*0Sstevel@tonic-gate 		struct sysent *se = LWP_GETSYSENT(lwp);
268*0Sstevel@tonic-gate 		struct sysent *callp = se + code;
269*0Sstevel@tonic-gate 
270*0Sstevel@tonic-gate 		nargs = callp->sy_narg;
271*0Sstevel@tonic-gate 		if (LOADABLE_SYSCALL(callp) && nargs == 0) {
272*0Sstevel@tonic-gate 			krwlock_t	*module_lock;
273*0Sstevel@tonic-gate 
274*0Sstevel@tonic-gate 			/*
275*0Sstevel@tonic-gate 			 * Find out how many arguments the system
276*0Sstevel@tonic-gate 			 * call uses.
277*0Sstevel@tonic-gate 			 *
278*0Sstevel@tonic-gate 			 * We have the property that loaded syscalls
279*0Sstevel@tonic-gate 			 * never change the number of arguments they
280*0Sstevel@tonic-gate 			 * use after they've been loaded once.  This
281*0Sstevel@tonic-gate 			 * allows us to stop for /proc tracing without
282*0Sstevel@tonic-gate 			 * holding the module lock.
283*0Sstevel@tonic-gate 			 * /proc is assured that sy_narg is valid.
284*0Sstevel@tonic-gate 			 */
285*0Sstevel@tonic-gate 			module_lock = lock_syscall(se, code);
286*0Sstevel@tonic-gate 			nargs = callp->sy_narg;
287*0Sstevel@tonic-gate 			rw_exit(module_lock);
288*0Sstevel@tonic-gate 		}
289*0Sstevel@tonic-gate 	}
290*0Sstevel@tonic-gate 
291*0Sstevel@tonic-gate 	/*
292*0Sstevel@tonic-gate 	 * Fetch the system call arguments.
293*0Sstevel@tonic-gate 	 */
294*0Sstevel@tonic-gate 	if (nargs == 0)
295*0Sstevel@tonic-gate 		goto out;
296*0Sstevel@tonic-gate 
297*0Sstevel@tonic-gate 
298*0Sstevel@tonic-gate 	ASSERT(nargs <= MAXSYSARGS);
299*0Sstevel@tonic-gate 
300*0Sstevel@tonic-gate 	if ((datamodel = lwp_getdatamodel(lwp)) == DATAMODEL_ILP32) {
301*0Sstevel@tonic-gate 
302*0Sstevel@tonic-gate 		if (rp->r_g1 == 0) {	/* indirect syscall */
303*0Sstevel@tonic-gate 
304*0Sstevel@tonic-gate 			lwp->lwp_arg[0] = (uint32_t)rp->r_o1;
305*0Sstevel@tonic-gate 			lwp->lwp_arg[1] = (uint32_t)rp->r_o2;
306*0Sstevel@tonic-gate 			lwp->lwp_arg[2] = (uint32_t)rp->r_o3;
307*0Sstevel@tonic-gate 			lwp->lwp_arg[3] = (uint32_t)rp->r_o4;
308*0Sstevel@tonic-gate 			lwp->lwp_arg[4] = (uint32_t)rp->r_o5;
309*0Sstevel@tonic-gate 			if (nargs > 5) {
310*0Sstevel@tonic-gate 				ua = (caddr_t)(caddr32_t)(rp->r_sp +
311*0Sstevel@tonic-gate 				    MINFRAME32);
312*0Sstevel@tonic-gate 				for (i = 5; i < nargs; i++) {
313*0Sstevel@tonic-gate 					uint32_t a;
314*0Sstevel@tonic-gate 					if (fuword32(ua, &a) != 0)
315*0Sstevel@tonic-gate 						return (-1);
316*0Sstevel@tonic-gate 					lwp->lwp_arg[i] = a;
317*0Sstevel@tonic-gate 					ua += sizeof (a);
318*0Sstevel@tonic-gate 				}
319*0Sstevel@tonic-gate 			}
320*0Sstevel@tonic-gate 		} else {
321*0Sstevel@tonic-gate 			lwp->lwp_arg[0] = (uint32_t)rp->r_o0;
322*0Sstevel@tonic-gate 			lwp->lwp_arg[1] = (uint32_t)rp->r_o1;
323*0Sstevel@tonic-gate 			lwp->lwp_arg[2] = (uint32_t)rp->r_o2;
324*0Sstevel@tonic-gate 			lwp->lwp_arg[3] = (uint32_t)rp->r_o3;
325*0Sstevel@tonic-gate 			lwp->lwp_arg[4] = (uint32_t)rp->r_o4;
326*0Sstevel@tonic-gate 			lwp->lwp_arg[5] = (uint32_t)rp->r_o5;
327*0Sstevel@tonic-gate 			if (nargs > 6) {
328*0Sstevel@tonic-gate 				ua = (caddr_t)(caddr32_t)(rp->r_sp +
329*0Sstevel@tonic-gate 				    MINFRAME32);
330*0Sstevel@tonic-gate 				for (i = 6; i < nargs; i++) {
331*0Sstevel@tonic-gate 					uint32_t a;
332*0Sstevel@tonic-gate 					if (fuword32(ua, &a) != 0)
333*0Sstevel@tonic-gate 						return (-1);
334*0Sstevel@tonic-gate 					lwp->lwp_arg[i] = a;
335*0Sstevel@tonic-gate 					ua += sizeof (a);
336*0Sstevel@tonic-gate 				}
337*0Sstevel@tonic-gate 			}
338*0Sstevel@tonic-gate 		}
339*0Sstevel@tonic-gate 	} else {
340*0Sstevel@tonic-gate 		ASSERT(datamodel == DATAMODEL_LP64);
341*0Sstevel@tonic-gate 		lwp->lwp_arg[0] = rp->r_o0;
342*0Sstevel@tonic-gate 		lwp->lwp_arg[1] = rp->r_o1;
343*0Sstevel@tonic-gate 		lwp->lwp_arg[2] = rp->r_o2;
344*0Sstevel@tonic-gate 		lwp->lwp_arg[3] = rp->r_o3;
345*0Sstevel@tonic-gate 		lwp->lwp_arg[4] = rp->r_o4;
346*0Sstevel@tonic-gate 		lwp->lwp_arg[5] = rp->r_o5;
347*0Sstevel@tonic-gate 		if (nargs > 6) {
348*0Sstevel@tonic-gate 			ua = (caddr_t)rp->r_sp + MINFRAME + STACK_BIAS;
349*0Sstevel@tonic-gate 			for (i = 6; i < nargs; i++) {
350*0Sstevel@tonic-gate 				unsigned long a;
351*0Sstevel@tonic-gate 				if (fulword(ua, &a) != 0)
352*0Sstevel@tonic-gate 					return (-1);
353*0Sstevel@tonic-gate 				lwp->lwp_arg[i] = a;
354*0Sstevel@tonic-gate 				ua += sizeof (a);
355*0Sstevel@tonic-gate 			}
356*0Sstevel@tonic-gate 		}
357*0Sstevel@tonic-gate 	}
358*0Sstevel@tonic-gate 
359*0Sstevel@tonic-gate out:
360*0Sstevel@tonic-gate 	lwp->lwp_ap = lwp->lwp_arg;
361*0Sstevel@tonic-gate 	lwp->lwp_argsaved = 1;
362*0Sstevel@tonic-gate 	t->t_post_sys = 1;	/* so lwp_ap will be reset */
363*0Sstevel@tonic-gate 	return (0);
364*0Sstevel@tonic-gate }
365*0Sstevel@tonic-gate 
366*0Sstevel@tonic-gate void
367*0Sstevel@tonic-gate reset_syscall_args(void)
368*0Sstevel@tonic-gate {
369*0Sstevel@tonic-gate 	klwp_t *lwp = ttolwp(curthread);
370*0Sstevel@tonic-gate 
371*0Sstevel@tonic-gate 	lwp->lwp_ap = (long *)&lwptoregs(lwp)->r_o0;
372*0Sstevel@tonic-gate 	lwp->lwp_argsaved = 0;
373*0Sstevel@tonic-gate }
374*0Sstevel@tonic-gate 
375*0Sstevel@tonic-gate /*
376*0Sstevel@tonic-gate  * nonexistent system call-- signal lwp (may want to handle it)
377*0Sstevel@tonic-gate  * flag error if lwp won't see signal immediately
378*0Sstevel@tonic-gate  * This works for old or new calling sequence.
379*0Sstevel@tonic-gate  */
380*0Sstevel@tonic-gate int64_t
381*0Sstevel@tonic-gate nosys()
382*0Sstevel@tonic-gate {
383*0Sstevel@tonic-gate 	tsignal(curthread, SIGSYS);
384*0Sstevel@tonic-gate 	return ((int64_t)set_errno(ENOSYS));
385*0Sstevel@tonic-gate }
386*0Sstevel@tonic-gate 
387*0Sstevel@tonic-gate /*
388*0Sstevel@tonic-gate  * Perform pre-system-call processing, including stopping for tracing,
389*0Sstevel@tonic-gate  * auditing, microstate-accounting, etc.
390*0Sstevel@tonic-gate  *
391*0Sstevel@tonic-gate  * This routine is called only if the t_pre_sys flag is set.  Any condition
392*0Sstevel@tonic-gate  * requiring pre-syscall handling must set the t_pre_sys flag.  If the
393*0Sstevel@tonic-gate  * condition is persistent, this routine will repost t_pre_sys.
394*0Sstevel@tonic-gate  */
395*0Sstevel@tonic-gate int
396*0Sstevel@tonic-gate pre_syscall(int arg0)
397*0Sstevel@tonic-gate {
398*0Sstevel@tonic-gate 	unsigned int code;
399*0Sstevel@tonic-gate 	kthread_t *t = curthread;
400*0Sstevel@tonic-gate 	proc_t *p = ttoproc(t);
401*0Sstevel@tonic-gate 	klwp_t *lwp = ttolwp(t);
402*0Sstevel@tonic-gate 	struct regs *rp = lwptoregs(lwp);
403*0Sstevel@tonic-gate 	int	repost;
404*0Sstevel@tonic-gate 
405*0Sstevel@tonic-gate 	t->t_pre_sys = repost = 0;	/* clear pre-syscall processing flag */
406*0Sstevel@tonic-gate 
407*0Sstevel@tonic-gate 	ASSERT(t->t_schedflag & TS_DONT_SWAP);
408*0Sstevel@tonic-gate 
409*0Sstevel@tonic-gate 	syscall_mstate(LMS_USER, LMS_SYSTEM);
410*0Sstevel@tonic-gate 
411*0Sstevel@tonic-gate 	/*
412*0Sstevel@tonic-gate 	 * The syscall arguments in the out registers should be pointed to
413*0Sstevel@tonic-gate 	 * by lwp_ap.  If the args need to be copied so that the outs can
414*0Sstevel@tonic-gate 	 * be changed without losing the ability to get the args for /proc,
415*0Sstevel@tonic-gate 	 * they can be saved by save_syscall_args(), and lwp_ap will be
416*0Sstevel@tonic-gate 	 * restored by post_syscall().
417*0Sstevel@tonic-gate 	 */
418*0Sstevel@tonic-gate 	ASSERT(lwp->lwp_ap == (long *)&rp->r_o0);
419*0Sstevel@tonic-gate 
420*0Sstevel@tonic-gate 	/*
421*0Sstevel@tonic-gate 	 * Make sure the thread is holding the latest credentials for the
422*0Sstevel@tonic-gate 	 * process.  The credentials in the process right now apply to this
423*0Sstevel@tonic-gate 	 * thread for the entire system call.
424*0Sstevel@tonic-gate 	 */
425*0Sstevel@tonic-gate 	if (t->t_cred != p->p_cred) {
426*0Sstevel@tonic-gate 		cred_t *oldcred = t->t_cred;
427*0Sstevel@tonic-gate 		/*
428*0Sstevel@tonic-gate 		 * DTrace accesses t_cred in probe context.  t_cred must
429*0Sstevel@tonic-gate 		 * always be either NULL, or point to a valid, allocated cred
430*0Sstevel@tonic-gate 		 * structure.
431*0Sstevel@tonic-gate 		 */
432*0Sstevel@tonic-gate 		t->t_cred = crgetcred();
433*0Sstevel@tonic-gate 		crfree(oldcred);
434*0Sstevel@tonic-gate 	}
435*0Sstevel@tonic-gate 
436*0Sstevel@tonic-gate 	/*
437*0Sstevel@tonic-gate 	 * Undo special arrangements to single-step the lwp
438*0Sstevel@tonic-gate 	 * so that a debugger will see valid register contents.
439*0Sstevel@tonic-gate 	 * Also so that the pc is valid for syncfpu().
440*0Sstevel@tonic-gate 	 * Also so that a syscall like exec() can be stepped.
441*0Sstevel@tonic-gate 	 */
442*0Sstevel@tonic-gate 	if (lwp->lwp_pcb.pcb_step != STEP_NONE) {
443*0Sstevel@tonic-gate 		(void) prundostep();
444*0Sstevel@tonic-gate 		repost = 1;
445*0Sstevel@tonic-gate 	}
446*0Sstevel@tonic-gate 
447*0Sstevel@tonic-gate 	/*
448*0Sstevel@tonic-gate 	 * Check for indirect system call in case we stop for tracing.
449*0Sstevel@tonic-gate 	 * Don't allow multiple indirection.
450*0Sstevel@tonic-gate 	 */
451*0Sstevel@tonic-gate 	code = t->t_sysnum;
452*0Sstevel@tonic-gate 	if (code == 0 && arg0 != 0) {		/* indirect syscall */
453*0Sstevel@tonic-gate 		code = arg0;
454*0Sstevel@tonic-gate 		t->t_sysnum = arg0;
455*0Sstevel@tonic-gate 	}
456*0Sstevel@tonic-gate 
457*0Sstevel@tonic-gate 	/*
458*0Sstevel@tonic-gate 	 * From the proc(4) manual page:
459*0Sstevel@tonic-gate 	 * When entry to a system call is being traced, the traced process
460*0Sstevel@tonic-gate 	 * stops after having begun the call to the system but before the
461*0Sstevel@tonic-gate 	 * system call arguments have been fetched from the process.
462*0Sstevel@tonic-gate 	 * If proc changes the args we must refetch them after starting.
463*0Sstevel@tonic-gate 	 */
464*0Sstevel@tonic-gate 	if (PTOU(p)->u_systrap) {
465*0Sstevel@tonic-gate 		if (prismember(&PTOU(p)->u_entrymask, code)) {
466*0Sstevel@tonic-gate 			/*
467*0Sstevel@tonic-gate 			 * Recheck stop condition, now that lock is held.
468*0Sstevel@tonic-gate 			 */
469*0Sstevel@tonic-gate 			mutex_enter(&p->p_lock);
470*0Sstevel@tonic-gate 			if (PTOU(p)->u_systrap &&
471*0Sstevel@tonic-gate 			    prismember(&PTOU(p)->u_entrymask, code)) {
472*0Sstevel@tonic-gate 				stop(PR_SYSENTRY, code);
473*0Sstevel@tonic-gate 				/*
474*0Sstevel@tonic-gate 				 * Must refetch args since they were
475*0Sstevel@tonic-gate 				 * possibly modified by /proc.  Indicate
476*0Sstevel@tonic-gate 				 * that the valid copy is in the
477*0Sstevel@tonic-gate 				 * registers.
478*0Sstevel@tonic-gate 				 */
479*0Sstevel@tonic-gate 				lwp->lwp_argsaved = 0;
480*0Sstevel@tonic-gate 				lwp->lwp_ap = (long *)&rp->r_o0;
481*0Sstevel@tonic-gate 			}
482*0Sstevel@tonic-gate 			mutex_exit(&p->p_lock);
483*0Sstevel@tonic-gate 		}
484*0Sstevel@tonic-gate 		repost = 1;
485*0Sstevel@tonic-gate 	}
486*0Sstevel@tonic-gate 
487*0Sstevel@tonic-gate 	if (lwp->lwp_sysabort) {
488*0Sstevel@tonic-gate 		/*
489*0Sstevel@tonic-gate 		 * lwp_sysabort may have been set via /proc while the process
490*0Sstevel@tonic-gate 		 * was stopped on PR_SYSENTRY.  If so, abort the system call.
491*0Sstevel@tonic-gate 		 * Override any error from the copyin() of the arguments.
492*0Sstevel@tonic-gate 		 */
493*0Sstevel@tonic-gate 		lwp->lwp_sysabort = 0;
494*0Sstevel@tonic-gate 		(void) set_errno(EINTR); /* sets post-sys processing */
495*0Sstevel@tonic-gate 		t->t_pre_sys = 1;	/* repost anyway */
496*0Sstevel@tonic-gate 		return (1);		/* don't do system call, return EINTR */
497*0Sstevel@tonic-gate 	}
498*0Sstevel@tonic-gate 
499*0Sstevel@tonic-gate #ifdef C2_AUDIT
500*0Sstevel@tonic-gate 	if (audit_active) {	/* begin auditing for this syscall */
501*0Sstevel@tonic-gate 		int error;
502*0Sstevel@tonic-gate 		if (error = audit_start(T_SYSCALL, code, 0, lwp)) {
503*0Sstevel@tonic-gate 			t->t_pre_sys = 1;	/* repost anyway */
504*0Sstevel@tonic-gate 			lwp->lwp_error = 0;	/* for old drivers */
505*0Sstevel@tonic-gate 			return (error);
506*0Sstevel@tonic-gate 		}
507*0Sstevel@tonic-gate 		repost = 1;
508*0Sstevel@tonic-gate 	}
509*0Sstevel@tonic-gate #endif /* C2_AUDIT */
510*0Sstevel@tonic-gate 
511*0Sstevel@tonic-gate #ifndef NPROBE
512*0Sstevel@tonic-gate 	/* Kernel probe */
513*0Sstevel@tonic-gate 	if (tnf_tracing_active) {
514*0Sstevel@tonic-gate 		TNF_PROBE_1(syscall_start, "syscall thread", /* CSTYLED */,
515*0Sstevel@tonic-gate 			tnf_sysnum,	sysnum,		t->t_sysnum);
516*0Sstevel@tonic-gate 		t->t_post_sys = 1;	/* make sure post_syscall runs */
517*0Sstevel@tonic-gate 		repost = 1;
518*0Sstevel@tonic-gate 	}
519*0Sstevel@tonic-gate #endif /* NPROBE */
520*0Sstevel@tonic-gate 
521*0Sstevel@tonic-gate #ifdef SYSCALLTRACE
522*0Sstevel@tonic-gate 	if (syscalltrace) {
523*0Sstevel@tonic-gate 		int i;
524*0Sstevel@tonic-gate 		long *ap;
525*0Sstevel@tonic-gate 		char *cp;
526*0Sstevel@tonic-gate 		char *sysname;
527*0Sstevel@tonic-gate 		struct sysent *callp;
528*0Sstevel@tonic-gate 
529*0Sstevel@tonic-gate 		if (code >= NSYSCALL)
530*0Sstevel@tonic-gate 			callp = &nosys_ent;	/* nosys has no args */
531*0Sstevel@tonic-gate 		else
532*0Sstevel@tonic-gate 			callp = LWP_GETSYSENT(lwp) + code;
533*0Sstevel@tonic-gate 		(void) save_syscall_args();
534*0Sstevel@tonic-gate 		mutex_enter(&systrace_lock);
535*0Sstevel@tonic-gate 		printf("%d: ", p->p_pid);
536*0Sstevel@tonic-gate 		if (code >= NSYSCALL)
537*0Sstevel@tonic-gate 			printf("0x%x", code);
538*0Sstevel@tonic-gate 		else {
539*0Sstevel@tonic-gate 			sysname = mod_getsysname(code);
540*0Sstevel@tonic-gate 			printf("%s[0x%x]", sysname == NULL ? "NULL" :
541*0Sstevel@tonic-gate 			    sysname, code);
542*0Sstevel@tonic-gate 		}
543*0Sstevel@tonic-gate 		cp = "(";
544*0Sstevel@tonic-gate 		for (i = 0, ap = lwp->lwp_ap; i < callp->sy_narg; i++, ap++) {
545*0Sstevel@tonic-gate 			printf("%s%lx", cp, *ap);
546*0Sstevel@tonic-gate 			cp = ", ";
547*0Sstevel@tonic-gate 		}
548*0Sstevel@tonic-gate 		if (i)
549*0Sstevel@tonic-gate 			printf(")");
550*0Sstevel@tonic-gate 		printf(" %s id=0x%p\n", PTOU(p)->u_comm, curthread);
551*0Sstevel@tonic-gate 		mutex_exit(&systrace_lock);
552*0Sstevel@tonic-gate 	}
553*0Sstevel@tonic-gate #endif /* SYSCALLTRACE */
554*0Sstevel@tonic-gate 
555*0Sstevel@tonic-gate 	/*
556*0Sstevel@tonic-gate 	 * If there was a continuing reason for pre-syscall processing,
557*0Sstevel@tonic-gate 	 * set the t_pre_sys flag for the next system call.
558*0Sstevel@tonic-gate 	 */
559*0Sstevel@tonic-gate 	if (repost)
560*0Sstevel@tonic-gate 		t->t_pre_sys = 1;
561*0Sstevel@tonic-gate 	lwp->lwp_error = 0;	/* for old drivers */
562*0Sstevel@tonic-gate 	lwp->lwp_badpriv = PRIV_NONE;	/* for privilege tracing */
563*0Sstevel@tonic-gate 	return (0);
564*0Sstevel@tonic-gate }
565*0Sstevel@tonic-gate 
566*0Sstevel@tonic-gate /*
567*0Sstevel@tonic-gate  * Post-syscall processing.  Perform abnormal system call completion
568*0Sstevel@tonic-gate  * actions such as /proc tracing, profiling, signals, preemption, etc.
569*0Sstevel@tonic-gate  *
570*0Sstevel@tonic-gate  * This routine is called only if t_post_sys, t_sig_check, or t_astflag is set.
571*0Sstevel@tonic-gate  * Any condition requiring pre-syscall handling must set one of these.
572*0Sstevel@tonic-gate  * If the condition is persistent, this routine will repost t_post_sys.
573*0Sstevel@tonic-gate  */
574*0Sstevel@tonic-gate void
575*0Sstevel@tonic-gate post_syscall(long rval1, long rval2)
576*0Sstevel@tonic-gate {
577*0Sstevel@tonic-gate 	kthread_t	*t = curthread;
578*0Sstevel@tonic-gate 	proc_t	*p = curproc;
579*0Sstevel@tonic-gate 	klwp_t	*lwp = ttolwp(t);
580*0Sstevel@tonic-gate 	struct regs *rp = lwptoregs(lwp);
581*0Sstevel@tonic-gate 	uint_t	error;
582*0Sstevel@tonic-gate 	int	code = t->t_sysnum;
583*0Sstevel@tonic-gate 	int	repost = 0;
584*0Sstevel@tonic-gate 	int	proc_stop = 0;		/* non-zero if stopping for /proc */
585*0Sstevel@tonic-gate 	int	sigprof = 0;		/* non-zero if sending SIGPROF */
586*0Sstevel@tonic-gate 
587*0Sstevel@tonic-gate 	t->t_post_sys = 0;
588*0Sstevel@tonic-gate 
589*0Sstevel@tonic-gate 	error = lwp->lwp_errno;
590*0Sstevel@tonic-gate 
591*0Sstevel@tonic-gate 	/*
592*0Sstevel@tonic-gate 	 * Code can be zero if this is a new LWP returning after a forkall(),
593*0Sstevel@tonic-gate 	 * other than the one which matches the one in the parent which called
594*0Sstevel@tonic-gate 	 * forkall().  In these LWPs, skip most of post-syscall activity.
595*0Sstevel@tonic-gate 	 */
596*0Sstevel@tonic-gate 	if (code == 0)
597*0Sstevel@tonic-gate 		goto sig_check;
598*0Sstevel@tonic-gate 
599*0Sstevel@tonic-gate #ifdef C2_AUDIT
600*0Sstevel@tonic-gate 	if (audit_active) {	/* put out audit record for this syscall */
601*0Sstevel@tonic-gate 		rval_t	rval;	/* fix audit_finish() someday */
602*0Sstevel@tonic-gate 
603*0Sstevel@tonic-gate 		/* XX64 -- truncation of 64-bit return values? */
604*0Sstevel@tonic-gate 		rval.r_val1 = (int)rval1;
605*0Sstevel@tonic-gate 		rval.r_val2 = (int)rval2;
606*0Sstevel@tonic-gate 		audit_finish(T_SYSCALL, code, error, &rval);
607*0Sstevel@tonic-gate 		repost = 1;
608*0Sstevel@tonic-gate 	}
609*0Sstevel@tonic-gate #endif /* C2_AUDIT */
610*0Sstevel@tonic-gate 
611*0Sstevel@tonic-gate 	if (curthread->t_pdmsg != NULL) {
612*0Sstevel@tonic-gate 		char *m = curthread->t_pdmsg;
613*0Sstevel@tonic-gate 
614*0Sstevel@tonic-gate 		uprintf("%s", m);
615*0Sstevel@tonic-gate 		kmem_free(m, strlen(m) + 1);
616*0Sstevel@tonic-gate 		curthread->t_pdmsg = NULL;
617*0Sstevel@tonic-gate 	}
618*0Sstevel@tonic-gate 
619*0Sstevel@tonic-gate 	/*
620*0Sstevel@tonic-gate 	 * If we're going to stop for /proc tracing, set the flag and
621*0Sstevel@tonic-gate 	 * save the arguments so that the return values don't smash them.
622*0Sstevel@tonic-gate 	 */
623*0Sstevel@tonic-gate 	if (PTOU(p)->u_systrap) {
624*0Sstevel@tonic-gate 		if (prismember(&PTOU(p)->u_exitmask, code)) {
625*0Sstevel@tonic-gate 			proc_stop = 1;
626*0Sstevel@tonic-gate 			(void) save_syscall_args();
627*0Sstevel@tonic-gate 		}
628*0Sstevel@tonic-gate 		repost = 1;
629*0Sstevel@tonic-gate 	}
630*0Sstevel@tonic-gate 
631*0Sstevel@tonic-gate 	/*
632*0Sstevel@tonic-gate 	 * Similarly check to see if SIGPROF might be sent.
633*0Sstevel@tonic-gate 	 */
634*0Sstevel@tonic-gate 	if (curthread->t_rprof != NULL &&
635*0Sstevel@tonic-gate 	    curthread->t_rprof->rp_anystate != 0) {
636*0Sstevel@tonic-gate 		(void) save_syscall_args();
637*0Sstevel@tonic-gate 		sigprof = 1;
638*0Sstevel@tonic-gate 	}
639*0Sstevel@tonic-gate 
640*0Sstevel@tonic-gate 	if (lwp->lwp_eosys == NORMALRETURN) {
641*0Sstevel@tonic-gate 		if (error == 0) {
642*0Sstevel@tonic-gate #ifdef SYSCALLTRACE
643*0Sstevel@tonic-gate 			if (syscalltrace) {
644*0Sstevel@tonic-gate 				mutex_enter(&systrace_lock);
645*0Sstevel@tonic-gate 				printf(
646*0Sstevel@tonic-gate 				    "%d: r_val1=0x%lx, r_val2=0x%lx, id 0x%p\n",
647*0Sstevel@tonic-gate 				    p->p_pid, rval1, rval2, curthread);
648*0Sstevel@tonic-gate 				mutex_exit(&systrace_lock);
649*0Sstevel@tonic-gate 			}
650*0Sstevel@tonic-gate #endif /* SYSCALLTRACE */
651*0Sstevel@tonic-gate 			rp->r_tstate &= ~TSTATE_IC;
652*0Sstevel@tonic-gate 			rp->r_o0 = rval1;
653*0Sstevel@tonic-gate 			rp->r_o1 = rval2;
654*0Sstevel@tonic-gate 		} else {
655*0Sstevel@tonic-gate 			int sig;
656*0Sstevel@tonic-gate 
657*0Sstevel@tonic-gate #ifdef SYSCALLTRACE
658*0Sstevel@tonic-gate 			if (syscalltrace) {
659*0Sstevel@tonic-gate 				mutex_enter(&systrace_lock);
660*0Sstevel@tonic-gate 				printf("%d: error=%d, id 0x%p\n",
661*0Sstevel@tonic-gate 				    p->p_pid, error, curthread);
662*0Sstevel@tonic-gate 				mutex_exit(&systrace_lock);
663*0Sstevel@tonic-gate 			}
664*0Sstevel@tonic-gate #endif /* SYSCALLTRACE */
665*0Sstevel@tonic-gate 			if (error == EINTR && t->t_activefd.a_stale)
666*0Sstevel@tonic-gate 				error = EBADF;
667*0Sstevel@tonic-gate 			if (error == EINTR &&
668*0Sstevel@tonic-gate 			    (sig = lwp->lwp_cursig) != 0 &&
669*0Sstevel@tonic-gate 			    sigismember(&PTOU(p)->u_sigrestart, sig) &&
670*0Sstevel@tonic-gate 			    PTOU(p)->u_signal[sig - 1] != SIG_DFL &&
671*0Sstevel@tonic-gate 			    PTOU(p)->u_signal[sig - 1] != SIG_IGN)
672*0Sstevel@tonic-gate 				error = ERESTART;
673*0Sstevel@tonic-gate 			rp->r_o0 = error;
674*0Sstevel@tonic-gate 			rp->r_tstate |= TSTATE_IC;
675*0Sstevel@tonic-gate 		}
676*0Sstevel@tonic-gate 		/*
677*0Sstevel@tonic-gate 		 * The default action is to redo the trap instruction.
678*0Sstevel@tonic-gate 		 * We increment the pc and npc past it for NORMALRETURN.
679*0Sstevel@tonic-gate 		 * JUSTRETURN has set up a new pc and npc already.
680*0Sstevel@tonic-gate 		 * RESTARTSYS automatically restarts by leaving pc and npc
681*0Sstevel@tonic-gate 		 * alone.
682*0Sstevel@tonic-gate 		 */
683*0Sstevel@tonic-gate 		rp->r_pc = rp->r_npc;
684*0Sstevel@tonic-gate 		rp->r_npc += 4;
685*0Sstevel@tonic-gate 	}
686*0Sstevel@tonic-gate 
687*0Sstevel@tonic-gate 	/*
688*0Sstevel@tonic-gate 	 * From the proc(4) manual page:
689*0Sstevel@tonic-gate 	 * When exit from a system call is being traced, the traced process
690*0Sstevel@tonic-gate 	 * stops on completion of the system call just prior to checking for
691*0Sstevel@tonic-gate 	 * signals and returning to user level.  At this point all return
692*0Sstevel@tonic-gate 	 * values have been stored into the traced process's saved registers.
693*0Sstevel@tonic-gate 	 */
694*0Sstevel@tonic-gate 	if (proc_stop) {
695*0Sstevel@tonic-gate 		mutex_enter(&p->p_lock);
696*0Sstevel@tonic-gate 		if (PTOU(p)->u_systrap &&
697*0Sstevel@tonic-gate 		    prismember(&PTOU(p)->u_exitmask, code))
698*0Sstevel@tonic-gate 			stop(PR_SYSEXIT, code);
699*0Sstevel@tonic-gate 		mutex_exit(&p->p_lock);
700*0Sstevel@tonic-gate 	}
701*0Sstevel@tonic-gate 
702*0Sstevel@tonic-gate 	/*
703*0Sstevel@tonic-gate 	 * If we are the parent returning from a successful
704*0Sstevel@tonic-gate 	 * vfork, wait for the child to exec or exit.
705*0Sstevel@tonic-gate 	 * This code must be here and not in the bowels of the system
706*0Sstevel@tonic-gate 	 * so that /proc can intercept exit from vfork in a timely way.
707*0Sstevel@tonic-gate 	 */
708*0Sstevel@tonic-gate 	if (code == SYS_vfork && rp->r_o1 == 0 && error == 0)
709*0Sstevel@tonic-gate 		vfwait((pid_t)rval1);
710*0Sstevel@tonic-gate 
711*0Sstevel@tonic-gate 	/*
712*0Sstevel@tonic-gate 	 * If profiling is active, bill the current PC in user-land
713*0Sstevel@tonic-gate 	 * and keep reposting until profiling is disabled.
714*0Sstevel@tonic-gate 	 */
715*0Sstevel@tonic-gate 	if (p->p_prof.pr_scale) {
716*0Sstevel@tonic-gate 		if (lwp->lwp_oweupc)
717*0Sstevel@tonic-gate 			profil_tick(rp->r_pc);
718*0Sstevel@tonic-gate 		repost = 1;
719*0Sstevel@tonic-gate 	}
720*0Sstevel@tonic-gate 
721*0Sstevel@tonic-gate 	t->t_sysnum = 0;	/* no longer in a system call */
722*0Sstevel@tonic-gate 
723*0Sstevel@tonic-gate sig_check:
724*0Sstevel@tonic-gate 	/*
725*0Sstevel@tonic-gate 	 * Reset flag for next time.
726*0Sstevel@tonic-gate 	 * We must do this after stopping on PR_SYSEXIT
727*0Sstevel@tonic-gate 	 * because /proc uses the information in lwp_eosys.
728*0Sstevel@tonic-gate 	 */
729*0Sstevel@tonic-gate 	lwp->lwp_eosys = NORMALRETURN;
730*0Sstevel@tonic-gate 	clear_stale_fd();
731*0Sstevel@tonic-gate 
732*0Sstevel@tonic-gate 	if (t->t_astflag | t->t_sig_check) {
733*0Sstevel@tonic-gate 		/*
734*0Sstevel@tonic-gate 		 * Turn off the AST flag before checking all the conditions that
735*0Sstevel@tonic-gate 		 * may have caused an AST.  This flag is on whenever a signal or
736*0Sstevel@tonic-gate 		 * unusual condition should be handled after the next trap or
737*0Sstevel@tonic-gate 		 * syscall.
738*0Sstevel@tonic-gate 		 */
739*0Sstevel@tonic-gate 		astoff(t);
740*0Sstevel@tonic-gate 		t->t_sig_check = 0;
741*0Sstevel@tonic-gate 
742*0Sstevel@tonic-gate 		mutex_enter(&p->p_lock);
743*0Sstevel@tonic-gate 		if (curthread->t_proc_flag & TP_CHANGEBIND) {
744*0Sstevel@tonic-gate 			timer_lwpbind();
745*0Sstevel@tonic-gate 			curthread->t_proc_flag &= ~TP_CHANGEBIND;
746*0Sstevel@tonic-gate 		}
747*0Sstevel@tonic-gate 		mutex_exit(&p->p_lock);
748*0Sstevel@tonic-gate 
749*0Sstevel@tonic-gate 		/*
750*0Sstevel@tonic-gate 		 * for kaio requests on the special kaio poll queue,
751*0Sstevel@tonic-gate 		 * copyout their results to user memory.
752*0Sstevel@tonic-gate 		 */
753*0Sstevel@tonic-gate 		if (p->p_aio)
754*0Sstevel@tonic-gate 			aio_cleanup(0);
755*0Sstevel@tonic-gate 
756*0Sstevel@tonic-gate 		/*
757*0Sstevel@tonic-gate 		 * If this LWP was asked to hold, call holdlwp(), which will
758*0Sstevel@tonic-gate 		 * stop.  holdlwps() sets this up and calls pokelwps() which
759*0Sstevel@tonic-gate 		 * sets the AST flag.
760*0Sstevel@tonic-gate 		 *
761*0Sstevel@tonic-gate 		 * Also check TP_EXITLWP, since this is used by fresh new LWPs
762*0Sstevel@tonic-gate 		 * through lwp_rtt().  That flag is set if the lwp_create(2)
763*0Sstevel@tonic-gate 		 * syscall failed after creating the LWP.
764*0Sstevel@tonic-gate 		 */
765*0Sstevel@tonic-gate 		if (ISHOLD(p) || (t->t_proc_flag & TP_EXITLWP))
766*0Sstevel@tonic-gate 			holdlwp();
767*0Sstevel@tonic-gate 
768*0Sstevel@tonic-gate 		/*
769*0Sstevel@tonic-gate 		 * All code that sets signals and makes ISSIG_PENDING
770*0Sstevel@tonic-gate 		 * evaluate true must set t_sig_check afterwards.
771*0Sstevel@tonic-gate 		 */
772*0Sstevel@tonic-gate 		if (ISSIG_PENDING(t, lwp, p)) {
773*0Sstevel@tonic-gate 			if (issig(FORREAL))
774*0Sstevel@tonic-gate 				psig();
775*0Sstevel@tonic-gate 			t->t_sig_check = 1;	/* recheck next time */
776*0Sstevel@tonic-gate 		}
777*0Sstevel@tonic-gate 
778*0Sstevel@tonic-gate 		if (sigprof) {
779*0Sstevel@tonic-gate 			realsigprof(code, error);
780*0Sstevel@tonic-gate 			t->t_sig_check = 1;	/* recheck next time */
781*0Sstevel@tonic-gate 		}
782*0Sstevel@tonic-gate 
783*0Sstevel@tonic-gate 		/*
784*0Sstevel@tonic-gate 		 * If a performance counter overflow interrupt was
785*0Sstevel@tonic-gate 		 * delivered *during* the syscall, then re-enable the
786*0Sstevel@tonic-gate 		 * AST so that we take a trip through trap() to cause
787*0Sstevel@tonic-gate 		 * the SIGEMT to be delivered.
788*0Sstevel@tonic-gate 		 */
789*0Sstevel@tonic-gate 		if (lwp->lwp_pcb.pcb_flags & CPC_OVERFLOW)
790*0Sstevel@tonic-gate 			aston(t);
791*0Sstevel@tonic-gate 
792*0Sstevel@tonic-gate 		/*
793*0Sstevel@tonic-gate 		 * If an asynchronous hardware error is pending, turn AST flag
794*0Sstevel@tonic-gate 		 * back on.  AST will be checked again before we return to user
795*0Sstevel@tonic-gate 		 * mode and we'll come back through trap() to handle the error.
796*0Sstevel@tonic-gate 		 */
797*0Sstevel@tonic-gate 		if (lwp->lwp_pcb.pcb_flags & ASYNC_HWERR)
798*0Sstevel@tonic-gate 			aston(t);
799*0Sstevel@tonic-gate 	}
800*0Sstevel@tonic-gate 
801*0Sstevel@tonic-gate 	/*
802*0Sstevel@tonic-gate 	 * Restore register window if a debugger modified it.
803*0Sstevel@tonic-gate 	 * Set up to perform a single-step if a debugger requested it.
804*0Sstevel@tonic-gate 	 */
805*0Sstevel@tonic-gate 	if (lwp->lwp_pcb.pcb_xregstat != XREGNONE)
806*0Sstevel@tonic-gate 		xregrestore(lwp, 1);
807*0Sstevel@tonic-gate 
808*0Sstevel@tonic-gate 	lwp->lwp_errno = 0;		/* clear error for next time */
809*0Sstevel@tonic-gate 
810*0Sstevel@tonic-gate #ifndef NPROBE
811*0Sstevel@tonic-gate 	/* Kernel probe */
812*0Sstevel@tonic-gate 	if (tnf_tracing_active) {
813*0Sstevel@tonic-gate 		TNF_PROBE_3(syscall_end, "syscall thread", /* CSTYLED */,
814*0Sstevel@tonic-gate 			tnf_long,	rval1,		rval1,
815*0Sstevel@tonic-gate 			tnf_long,	rval2,		rval2,
816*0Sstevel@tonic-gate 			tnf_long,	errno,		(long)error);
817*0Sstevel@tonic-gate 		repost = 1;
818*0Sstevel@tonic-gate 	}
819*0Sstevel@tonic-gate #endif /* NPROBE */
820*0Sstevel@tonic-gate 
821*0Sstevel@tonic-gate 	/*
822*0Sstevel@tonic-gate 	 * Set state to LWP_USER here so preempt won't give us a kernel
823*0Sstevel@tonic-gate 	 * priority if it occurs after this point.  Call CL_TRAPRET() to
824*0Sstevel@tonic-gate 	 * restore the user-level priority.
825*0Sstevel@tonic-gate 	 *
826*0Sstevel@tonic-gate 	 * It is important that no locks (other than spinlocks) be entered
827*0Sstevel@tonic-gate 	 * after this point before returning to user mode (unless lwp_state
828*0Sstevel@tonic-gate 	 * is set back to LWP_SYS).
829*0Sstevel@tonic-gate 	 *
830*0Sstevel@tonic-gate 	 * Sampled times past this point are charged to the user.
831*0Sstevel@tonic-gate 	 */
832*0Sstevel@tonic-gate 	lwp->lwp_state = LWP_USER;
833*0Sstevel@tonic-gate 
834*0Sstevel@tonic-gate 	if (t->t_trapret) {
835*0Sstevel@tonic-gate 		t->t_trapret = 0;
836*0Sstevel@tonic-gate 		thread_lock(t);
837*0Sstevel@tonic-gate 		CL_TRAPRET(t);
838*0Sstevel@tonic-gate 		thread_unlock(t);
839*0Sstevel@tonic-gate 	}
840*0Sstevel@tonic-gate 	if (CPU->cpu_runrun)
841*0Sstevel@tonic-gate 		preempt();
842*0Sstevel@tonic-gate 
843*0Sstevel@tonic-gate 	/*
844*0Sstevel@tonic-gate 	 * t_post_sys will be set if pcb_step is active.
845*0Sstevel@tonic-gate 	 */
846*0Sstevel@tonic-gate 	if (lwp->lwp_pcb.pcb_step != STEP_NONE) {
847*0Sstevel@tonic-gate 		prdostep();
848*0Sstevel@tonic-gate 		repost = 1;
849*0Sstevel@tonic-gate 	}
850*0Sstevel@tonic-gate 
851*0Sstevel@tonic-gate 	/*
852*0Sstevel@tonic-gate 	 * In case the args were copied to the lwp, reset the
853*0Sstevel@tonic-gate 	 * pointer so the next syscall will have the right lwp_ap pointer.
854*0Sstevel@tonic-gate 	 */
855*0Sstevel@tonic-gate 	lwp->lwp_ap = (long *)&rp->r_o0;
856*0Sstevel@tonic-gate 	lwp->lwp_argsaved = 0;
857*0Sstevel@tonic-gate 
858*0Sstevel@tonic-gate 	/*
859*0Sstevel@tonic-gate 	 * If there was a continuing reason for post-syscall processing,
860*0Sstevel@tonic-gate 	 * set the t_post_sys flag for the next system call.
861*0Sstevel@tonic-gate 	 */
862*0Sstevel@tonic-gate 	if (repost)
863*0Sstevel@tonic-gate 		t->t_post_sys = 1;
864*0Sstevel@tonic-gate 
865*0Sstevel@tonic-gate 	/*
866*0Sstevel@tonic-gate 	 * If there is a ustack registered for this lwp, and the stack rlimit
867*0Sstevel@tonic-gate 	 * has been altered, read in the ustack. If the saved stack rlimit
868*0Sstevel@tonic-gate 	 * matches the bounds of the ustack, update the ustack to reflect
869*0Sstevel@tonic-gate 	 * the new rlimit. If the new stack rlimit is RLIM_INFINITY, disable
870*0Sstevel@tonic-gate 	 * stack checking by setting the size to 0.
871*0Sstevel@tonic-gate 	 */
872*0Sstevel@tonic-gate 	if (lwp->lwp_ustack != 0 && lwp->lwp_old_stk_ctl != 0) {
873*0Sstevel@tonic-gate 		rlim64_t new_size;
874*0Sstevel@tonic-gate 		model_t model;
875*0Sstevel@tonic-gate 		caddr_t top;
876*0Sstevel@tonic-gate 		struct rlimit64 rl;
877*0Sstevel@tonic-gate 
878*0Sstevel@tonic-gate 		mutex_enter(&p->p_lock);
879*0Sstevel@tonic-gate 		new_size = p->p_stk_ctl;
880*0Sstevel@tonic-gate 		model = p->p_model;
881*0Sstevel@tonic-gate 		top = p->p_usrstack;
882*0Sstevel@tonic-gate 		(void) rctl_rlimit_get(rctlproc_legacy[RLIMIT_STACK], p, &rl);
883*0Sstevel@tonic-gate 		mutex_exit(&p->p_lock);
884*0Sstevel@tonic-gate 
885*0Sstevel@tonic-gate 		if (rl.rlim_cur == RLIM64_INFINITY)
886*0Sstevel@tonic-gate 			new_size = 0;
887*0Sstevel@tonic-gate 
888*0Sstevel@tonic-gate 		if (model == DATAMODEL_NATIVE) {
889*0Sstevel@tonic-gate 			stack_t stk;
890*0Sstevel@tonic-gate 
891*0Sstevel@tonic-gate 			if (copyin((stack_t *)lwp->lwp_ustack, &stk,
892*0Sstevel@tonic-gate 			    sizeof (stack_t)) == 0 &&
893*0Sstevel@tonic-gate 			    (stk.ss_size == lwp->lwp_old_stk_ctl ||
894*0Sstevel@tonic-gate 				stk.ss_size == 0) &&
895*0Sstevel@tonic-gate 			    stk.ss_sp == top - stk.ss_size) {
896*0Sstevel@tonic-gate 				stk.ss_sp = (void *)((uintptr_t)stk.ss_sp +
897*0Sstevel@tonic-gate 				    stk.ss_size - new_size);
898*0Sstevel@tonic-gate 				stk.ss_size = new_size;
899*0Sstevel@tonic-gate 
900*0Sstevel@tonic-gate 				(void) copyout(&stk,
901*0Sstevel@tonic-gate 				    (stack_t *)lwp->lwp_ustack,
902*0Sstevel@tonic-gate 				    sizeof (stack_t));
903*0Sstevel@tonic-gate 			}
904*0Sstevel@tonic-gate 		} else {
905*0Sstevel@tonic-gate 			stack32_t stk32;
906*0Sstevel@tonic-gate 
907*0Sstevel@tonic-gate 			if (copyin((stack32_t *)lwp->lwp_ustack, &stk32,
908*0Sstevel@tonic-gate 			    sizeof (stack32_t)) == 0 &&
909*0Sstevel@tonic-gate 			    (stk32.ss_size == lwp->lwp_old_stk_ctl ||
910*0Sstevel@tonic-gate 				stk32.ss_size == 0) &&
911*0Sstevel@tonic-gate 			    stk32.ss_sp == (caddr32_t)(top - stk32.ss_size)) {
912*0Sstevel@tonic-gate 				stk32.ss_sp += stk32.ss_size - new_size;
913*0Sstevel@tonic-gate 				stk32.ss_size = new_size;
914*0Sstevel@tonic-gate 
915*0Sstevel@tonic-gate 				(void) copyout(&stk32,
916*0Sstevel@tonic-gate 				    (stack32_t *)lwp->lwp_ustack,
917*0Sstevel@tonic-gate 				    sizeof (stack32_t));
918*0Sstevel@tonic-gate 			}
919*0Sstevel@tonic-gate 		}
920*0Sstevel@tonic-gate 
921*0Sstevel@tonic-gate 		lwp->lwp_old_stk_ctl = 0;
922*0Sstevel@tonic-gate 	}
923*0Sstevel@tonic-gate 
924*0Sstevel@tonic-gate 	syscall_mstate(LMS_SYSTEM, LMS_USER);
925*0Sstevel@tonic-gate }
926*0Sstevel@tonic-gate 
927*0Sstevel@tonic-gate /*
928*0Sstevel@tonic-gate  * Call a system call which takes a pointer to the user args struct and
929*0Sstevel@tonic-gate  * a pointer to the return values.  This is a bit slower than the standard
930*0Sstevel@tonic-gate  * C arg-passing method in some cases.
931*0Sstevel@tonic-gate  */
932*0Sstevel@tonic-gate int64_t
933*0Sstevel@tonic-gate syscall_ap()
934*0Sstevel@tonic-gate {
935*0Sstevel@tonic-gate 	uint_t	error;
936*0Sstevel@tonic-gate 	struct sysent *callp;
937*0Sstevel@tonic-gate 	rval_t	rval;
938*0Sstevel@tonic-gate 	klwp_t	*lwp = ttolwp(curthread);
939*0Sstevel@tonic-gate 	struct regs *rp = lwptoregs(lwp);
940*0Sstevel@tonic-gate 
941*0Sstevel@tonic-gate 	callp = LWP_GETSYSENT(lwp) + curthread->t_sysnum;
942*0Sstevel@tonic-gate 
943*0Sstevel@tonic-gate 	/*
944*0Sstevel@tonic-gate 	 * If the arguments don't fit in registers %o0 - o5, make sure they
945*0Sstevel@tonic-gate 	 * have been copied to the lwp_arg array.
946*0Sstevel@tonic-gate 	 */
947*0Sstevel@tonic-gate 	if (callp->sy_narg > 6 && save_syscall_args())
948*0Sstevel@tonic-gate 		return ((int64_t)set_errno(EFAULT));
949*0Sstevel@tonic-gate 
950*0Sstevel@tonic-gate 	rval.r_val1 = 0;
951*0Sstevel@tonic-gate 	rval.r_val2 = (int)rp->r_o1;
952*0Sstevel@tonic-gate 	lwp->lwp_error = 0;	/* for old drivers */
953*0Sstevel@tonic-gate 	error = (*(callp->sy_call))(lwp->lwp_ap, &rval);
954*0Sstevel@tonic-gate 	if (error)
955*0Sstevel@tonic-gate 		return ((int64_t)set_errno(error));
956*0Sstevel@tonic-gate 	return (rval.r_vals);
957*0Sstevel@tonic-gate }
958*0Sstevel@tonic-gate 
959*0Sstevel@tonic-gate /*
960*0Sstevel@tonic-gate  * Load system call module.
961*0Sstevel@tonic-gate  *	Returns with pointer to held read lock for module.
962*0Sstevel@tonic-gate  */
963*0Sstevel@tonic-gate static krwlock_t *
964*0Sstevel@tonic-gate lock_syscall(struct sysent *table, uint_t code)
965*0Sstevel@tonic-gate {
966*0Sstevel@tonic-gate 	krwlock_t	*module_lock;
967*0Sstevel@tonic-gate 	struct modctl	*modp;
968*0Sstevel@tonic-gate 	int		id;
969*0Sstevel@tonic-gate 	struct sysent   *callp;
970*0Sstevel@tonic-gate 
971*0Sstevel@tonic-gate 	module_lock = table[code].sy_lock;
972*0Sstevel@tonic-gate 	callp = &table[code];
973*0Sstevel@tonic-gate 
974*0Sstevel@tonic-gate 	/*
975*0Sstevel@tonic-gate 	 * Optimization to only call modload if we don't have a loaded
976*0Sstevel@tonic-gate 	 * syscall.
977*0Sstevel@tonic-gate 	 */
978*0Sstevel@tonic-gate 	rw_enter(module_lock, RW_READER);
979*0Sstevel@tonic-gate 	if (LOADED_SYSCALL(callp))
980*0Sstevel@tonic-gate 		return (module_lock);
981*0Sstevel@tonic-gate 	rw_exit(module_lock);
982*0Sstevel@tonic-gate 
983*0Sstevel@tonic-gate 	for (;;) {
984*0Sstevel@tonic-gate 		if ((id = modload("sys", syscallnames[code])) == -1)
985*0Sstevel@tonic-gate 			break;
986*0Sstevel@tonic-gate 
987*0Sstevel@tonic-gate 		/*
988*0Sstevel@tonic-gate 		 * If we loaded successfully at least once, the modctl
989*0Sstevel@tonic-gate 		 * will still be valid, so we try to grab it by filename.
990*0Sstevel@tonic-gate 		 * If this call fails, it's because the mod_filename
991*0Sstevel@tonic-gate 		 * was changed after the call to modload() (mod_hold_by_name()
992*0Sstevel@tonic-gate 		 * is the likely culprit).  We can safely just take
993*0Sstevel@tonic-gate 		 * another lap if this is the case;  the modload() will
994*0Sstevel@tonic-gate 		 * change the mod_filename back to one by which we can
995*0Sstevel@tonic-gate 		 * find the modctl.
996*0Sstevel@tonic-gate 		 */
997*0Sstevel@tonic-gate 		modp = mod_find_by_filename("sys", syscallnames[code]);
998*0Sstevel@tonic-gate 
999*0Sstevel@tonic-gate 		if (modp == NULL)
1000*0Sstevel@tonic-gate 			continue;
1001*0Sstevel@tonic-gate 
1002*0Sstevel@tonic-gate 		mutex_enter(&mod_lock);
1003*0Sstevel@tonic-gate 
1004*0Sstevel@tonic-gate 		if (!modp->mod_installed) {
1005*0Sstevel@tonic-gate 			mutex_exit(&mod_lock);
1006*0Sstevel@tonic-gate 			continue;
1007*0Sstevel@tonic-gate 		}
1008*0Sstevel@tonic-gate 		break;
1009*0Sstevel@tonic-gate 	}
1010*0Sstevel@tonic-gate 
1011*0Sstevel@tonic-gate 	rw_enter(module_lock, RW_READER);
1012*0Sstevel@tonic-gate 
1013*0Sstevel@tonic-gate 	if (id != -1)
1014*0Sstevel@tonic-gate 		mutex_exit(&mod_lock);
1015*0Sstevel@tonic-gate 
1016*0Sstevel@tonic-gate 	return (module_lock);
1017*0Sstevel@tonic-gate }
1018*0Sstevel@tonic-gate 
1019*0Sstevel@tonic-gate /*
1020*0Sstevel@tonic-gate  * Loadable syscall support.
1021*0Sstevel@tonic-gate  *	If needed, load the module, then reserve it by holding a read
1022*0Sstevel@tonic-gate  * 	lock for the duration of the call.
1023*0Sstevel@tonic-gate  *	Later, if the syscall is not unloadable, it could patch the vector.
1024*0Sstevel@tonic-gate  */
1025*0Sstevel@tonic-gate /*ARGSUSED*/
1026*0Sstevel@tonic-gate int64_t
1027*0Sstevel@tonic-gate loadable_syscall(
1028*0Sstevel@tonic-gate     long a0, long a1, long a2, long a3,
1029*0Sstevel@tonic-gate     long a4, long a5, long a6, long a7)
1030*0Sstevel@tonic-gate {
1031*0Sstevel@tonic-gate 	int64_t		rval;
1032*0Sstevel@tonic-gate 	struct sysent	*callp;
1033*0Sstevel@tonic-gate 	struct sysent	*se = LWP_GETSYSENT(ttolwp(curthread));
1034*0Sstevel@tonic-gate 	krwlock_t	*module_lock;
1035*0Sstevel@tonic-gate 	int		code;
1036*0Sstevel@tonic-gate 
1037*0Sstevel@tonic-gate 	code = curthread->t_sysnum;
1038*0Sstevel@tonic-gate 	callp = se + code;
1039*0Sstevel@tonic-gate 
1040*0Sstevel@tonic-gate 	/*
1041*0Sstevel@tonic-gate 	 * Try to autoload the system call if necessary.
1042*0Sstevel@tonic-gate 	 */
1043*0Sstevel@tonic-gate 	module_lock = lock_syscall(se, code);
1044*0Sstevel@tonic-gate 	THREAD_KPRI_RELEASE();	/* drop priority given by rw_enter */
1045*0Sstevel@tonic-gate 
1046*0Sstevel@tonic-gate 	/*
1047*0Sstevel@tonic-gate 	 * we've locked either the loaded syscall or nosys
1048*0Sstevel@tonic-gate 	 */
1049*0Sstevel@tonic-gate 	if (callp->sy_flags & SE_ARGC) {
1050*0Sstevel@tonic-gate 		int64_t (*sy_call)();
1051*0Sstevel@tonic-gate 
1052*0Sstevel@tonic-gate 		sy_call = (int64_t (*)())callp->sy_call;
1053*0Sstevel@tonic-gate 		rval = (*sy_call)(a0, a1, a2, a3, a4, a5);
1054*0Sstevel@tonic-gate 	} else {
1055*0Sstevel@tonic-gate 		rval = syscall_ap();
1056*0Sstevel@tonic-gate 	}
1057*0Sstevel@tonic-gate 
1058*0Sstevel@tonic-gate 	THREAD_KPRI_REQUEST();	/* regain priority from read lock */
1059*0Sstevel@tonic-gate 	rw_exit(module_lock);
1060*0Sstevel@tonic-gate 	return (rval);
1061*0Sstevel@tonic-gate }
1062*0Sstevel@tonic-gate 
1063*0Sstevel@tonic-gate /*
1064*0Sstevel@tonic-gate  * Handle indirect system calls.
1065*0Sstevel@tonic-gate  *	This interface should be deprecated.  The library can handle
1066*0Sstevel@tonic-gate  *	this more efficiently, but keep this implementation for old binaries.
1067*0Sstevel@tonic-gate  *
1068*0Sstevel@tonic-gate  * XX64	Needs some work.
1069*0Sstevel@tonic-gate  */
1070*0Sstevel@tonic-gate int64_t
1071*0Sstevel@tonic-gate indir(int code, long a0, long a1, long a2, long a3, long a4)
1072*0Sstevel@tonic-gate {
1073*0Sstevel@tonic-gate 	klwp_t		*lwp = ttolwp(curthread);
1074*0Sstevel@tonic-gate 	struct sysent	*callp;
1075*0Sstevel@tonic-gate 
1076*0Sstevel@tonic-gate 	if (code <= 0 || code >= NSYSCALL)
1077*0Sstevel@tonic-gate 		return (nosys());
1078*0Sstevel@tonic-gate 
1079*0Sstevel@tonic-gate 	ASSERT(lwp->lwp_ap != NULL);
1080*0Sstevel@tonic-gate 
1081*0Sstevel@tonic-gate 	curthread->t_sysnum = code;
1082*0Sstevel@tonic-gate 	callp = LWP_GETSYSENT(lwp) + code;
1083*0Sstevel@tonic-gate 
1084*0Sstevel@tonic-gate 	/*
1085*0Sstevel@tonic-gate 	 * Handle argument setup, unless already done in pre_syscall().
1086*0Sstevel@tonic-gate 	 */
1087*0Sstevel@tonic-gate 	if (callp->sy_narg > 5) {
1088*0Sstevel@tonic-gate 		if (save_syscall_args()) 	/* move args to LWP array */
1089*0Sstevel@tonic-gate 			return ((int64_t)set_errno(EFAULT));
1090*0Sstevel@tonic-gate 	} else if (!lwp->lwp_argsaved) {
1091*0Sstevel@tonic-gate 		long *ap;
1092*0Sstevel@tonic-gate 
1093*0Sstevel@tonic-gate 		ap = lwp->lwp_ap;		/* args haven't been saved */
1094*0Sstevel@tonic-gate 		lwp->lwp_ap = ap + 1;		/* advance arg pointer */
1095*0Sstevel@tonic-gate 		curthread->t_post_sys = 1;	/* so lwp_ap will be reset */
1096*0Sstevel@tonic-gate 	}
1097*0Sstevel@tonic-gate 	return ((*callp->sy_callc)(a0, a1, a2, a3, a4, lwp->lwp_arg[5]));
1098*0Sstevel@tonic-gate }
1099*0Sstevel@tonic-gate 
1100*0Sstevel@tonic-gate /*
1101*0Sstevel@tonic-gate  * set_errno - set an error return from the current system call.
1102*0Sstevel@tonic-gate  *	This could be a macro.
1103*0Sstevel@tonic-gate  *	This returns the value it is passed, so that the caller can
1104*0Sstevel@tonic-gate  *	use tail-recursion-elimination and do return (set_errno(ERRNO));
1105*0Sstevel@tonic-gate  */
1106*0Sstevel@tonic-gate uint_t
1107*0Sstevel@tonic-gate set_errno(uint_t error)
1108*0Sstevel@tonic-gate {
1109*0Sstevel@tonic-gate 	ASSERT(error != 0);		/* must not be used to clear errno */
1110*0Sstevel@tonic-gate 
1111*0Sstevel@tonic-gate 	curthread->t_post_sys = 1;	/* have post_syscall do error return */
1112*0Sstevel@tonic-gate 	return (ttolwp(curthread)->lwp_errno = error);
1113*0Sstevel@tonic-gate }
1114*0Sstevel@tonic-gate 
1115*0Sstevel@tonic-gate /*
1116*0Sstevel@tonic-gate  * set_proc_pre_sys - Set pre-syscall processing for entire process.
1117*0Sstevel@tonic-gate  */
1118*0Sstevel@tonic-gate void
1119*0Sstevel@tonic-gate set_proc_pre_sys(proc_t *p)
1120*0Sstevel@tonic-gate {
1121*0Sstevel@tonic-gate 	kthread_t	*t;
1122*0Sstevel@tonic-gate 	kthread_t	*first;
1123*0Sstevel@tonic-gate 
1124*0Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&p->p_lock));
1125*0Sstevel@tonic-gate 
1126*0Sstevel@tonic-gate 	t = first = p->p_tlist;
1127*0Sstevel@tonic-gate 	do {
1128*0Sstevel@tonic-gate 		t->t_pre_sys = 1;
1129*0Sstevel@tonic-gate 	} while ((t = t->t_forw) != first);
1130*0Sstevel@tonic-gate }
1131*0Sstevel@tonic-gate 
1132*0Sstevel@tonic-gate /*
1133*0Sstevel@tonic-gate  * set_proc_post_sys - Set post-syscall processing for entire process.
1134*0Sstevel@tonic-gate  */
1135*0Sstevel@tonic-gate void
1136*0Sstevel@tonic-gate set_proc_post_sys(proc_t *p)
1137*0Sstevel@tonic-gate {
1138*0Sstevel@tonic-gate 	kthread_t	*t;
1139*0Sstevel@tonic-gate 	kthread_t	*first;
1140*0Sstevel@tonic-gate 
1141*0Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&p->p_lock));
1142*0Sstevel@tonic-gate 
1143*0Sstevel@tonic-gate 	t = first = p->p_tlist;
1144*0Sstevel@tonic-gate 	do {
1145*0Sstevel@tonic-gate 		t->t_post_sys = 1;
1146*0Sstevel@tonic-gate 	} while ((t = t->t_forw) != first);
1147*0Sstevel@tonic-gate }
1148*0Sstevel@tonic-gate 
1149*0Sstevel@tonic-gate /*
1150*0Sstevel@tonic-gate  * set_proc_sys - Set pre- and post-syscall processing for entire process.
1151*0Sstevel@tonic-gate  */
1152*0Sstevel@tonic-gate void
1153*0Sstevel@tonic-gate set_proc_sys(proc_t *p)
1154*0Sstevel@tonic-gate {
1155*0Sstevel@tonic-gate 	kthread_t	*t;
1156*0Sstevel@tonic-gate 	kthread_t	*first;
1157*0Sstevel@tonic-gate 
1158*0Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&p->p_lock));
1159*0Sstevel@tonic-gate 
1160*0Sstevel@tonic-gate 	t = first = p->p_tlist;
1161*0Sstevel@tonic-gate 	do {
1162*0Sstevel@tonic-gate 		t->t_pre_sys = 1;
1163*0Sstevel@tonic-gate 		t->t_post_sys = 1;
1164*0Sstevel@tonic-gate 	} while ((t = t->t_forw) != first);
1165*0Sstevel@tonic-gate }
1166*0Sstevel@tonic-gate 
1167*0Sstevel@tonic-gate /*
1168*0Sstevel@tonic-gate  * set_all_proc_sys - set pre- and post-syscall processing flags for all
1169*0Sstevel@tonic-gate  * user processes.
1170*0Sstevel@tonic-gate  *
1171*0Sstevel@tonic-gate  * This is needed when auditing, tracing, or other facilities which affect
1172*0Sstevel@tonic-gate  * all processes are turned on.
1173*0Sstevel@tonic-gate  */
1174*0Sstevel@tonic-gate void
1175*0Sstevel@tonic-gate set_all_proc_sys()
1176*0Sstevel@tonic-gate {
1177*0Sstevel@tonic-gate 	kthread_t	*t;
1178*0Sstevel@tonic-gate 	kthread_t	*first;
1179*0Sstevel@tonic-gate 
1180*0Sstevel@tonic-gate 	mutex_enter(&pidlock);
1181*0Sstevel@tonic-gate 	t = first = curthread;
1182*0Sstevel@tonic-gate 	do {
1183*0Sstevel@tonic-gate 		t->t_pre_sys = 1;
1184*0Sstevel@tonic-gate 		t->t_post_sys = 1;
1185*0Sstevel@tonic-gate 	} while ((t = t->t_next) != first);
1186*0Sstevel@tonic-gate 	mutex_exit(&pidlock);
1187*0Sstevel@tonic-gate }
1188*0Sstevel@tonic-gate 
1189*0Sstevel@tonic-gate /*
1190*0Sstevel@tonic-gate  * set_proc_ast - Set asynchronous service trap (AST) flag for all
1191*0Sstevel@tonic-gate  * threads in process.
1192*0Sstevel@tonic-gate  */
1193*0Sstevel@tonic-gate void
1194*0Sstevel@tonic-gate set_proc_ast(proc_t *p)
1195*0Sstevel@tonic-gate {
1196*0Sstevel@tonic-gate 	kthread_t	*t;
1197*0Sstevel@tonic-gate 	kthread_t	*first;
1198*0Sstevel@tonic-gate 
1199*0Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&p->p_lock));
1200*0Sstevel@tonic-gate 
1201*0Sstevel@tonic-gate 	t = first = p->p_tlist;
1202*0Sstevel@tonic-gate 	do {
1203*0Sstevel@tonic-gate 		aston(t);
1204*0Sstevel@tonic-gate 	} while ((t = t->t_forw) != first);
1205*0Sstevel@tonic-gate }
1206