xref: /titanic_52/usr/src/uts/common/disp/ts.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
28*7c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
29*7c478bd9Sstevel@tonic-gate 
30*7c478bd9Sstevel@tonic-gate 
31*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"	/* from SVr4.0 1.23 */
32*7c478bd9Sstevel@tonic-gate 
33*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
34*7c478bd9Sstevel@tonic-gate #include <sys/param.h>
35*7c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
36*7c478bd9Sstevel@tonic-gate #include <sys/cred.h>
37*7c478bd9Sstevel@tonic-gate #include <sys/proc.h>
38*7c478bd9Sstevel@tonic-gate #include <sys/session.h>
39*7c478bd9Sstevel@tonic-gate #include <sys/strsubr.h>
40*7c478bd9Sstevel@tonic-gate #include <sys/signal.h>
41*7c478bd9Sstevel@tonic-gate #include <sys/user.h>
42*7c478bd9Sstevel@tonic-gate #include <sys/priocntl.h>
43*7c478bd9Sstevel@tonic-gate #include <sys/class.h>
44*7c478bd9Sstevel@tonic-gate #include <sys/disp.h>
45*7c478bd9Sstevel@tonic-gate #include <sys/procset.h>
46*7c478bd9Sstevel@tonic-gate #include <sys/debug.h>
47*7c478bd9Sstevel@tonic-gate #include <sys/ts.h>
48*7c478bd9Sstevel@tonic-gate #include <sys/tspriocntl.h>
49*7c478bd9Sstevel@tonic-gate #include <sys/iapriocntl.h>
50*7c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
51*7c478bd9Sstevel@tonic-gate #include <sys/errno.h>
52*7c478bd9Sstevel@tonic-gate #include <sys/cpuvar.h>
53*7c478bd9Sstevel@tonic-gate #include <sys/systm.h>		/* for lbolt */
54*7c478bd9Sstevel@tonic-gate #include <sys/vtrace.h>
55*7c478bd9Sstevel@tonic-gate #include <sys/vmsystm.h>
56*7c478bd9Sstevel@tonic-gate #include <sys/schedctl.h>
57*7c478bd9Sstevel@tonic-gate #include <sys/tnf_probe.h>
58*7c478bd9Sstevel@tonic-gate #include <sys/atomic.h>
59*7c478bd9Sstevel@tonic-gate #include <sys/policy.h>
60*7c478bd9Sstevel@tonic-gate #include <sys/sdt.h>
61*7c478bd9Sstevel@tonic-gate #include <sys/cpupart.h>
62*7c478bd9Sstevel@tonic-gate 
63*7c478bd9Sstevel@tonic-gate #include <vm/rm.h>
64*7c478bd9Sstevel@tonic-gate #include <vm/seg_kmem.h>
65*7c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
66*7c478bd9Sstevel@tonic-gate 
67*7c478bd9Sstevel@tonic-gate static pri_t ts_init(id_t, int, classfuncs_t **);
68*7c478bd9Sstevel@tonic-gate 
69*7c478bd9Sstevel@tonic-gate static struct sclass csw = {
70*7c478bd9Sstevel@tonic-gate 	"TS",
71*7c478bd9Sstevel@tonic-gate 	ts_init,
72*7c478bd9Sstevel@tonic-gate 	0
73*7c478bd9Sstevel@tonic-gate };
74*7c478bd9Sstevel@tonic-gate 
75*7c478bd9Sstevel@tonic-gate static struct modlsched modlsched = {
76*7c478bd9Sstevel@tonic-gate 	&mod_schedops, "time sharing sched class", &csw
77*7c478bd9Sstevel@tonic-gate };
78*7c478bd9Sstevel@tonic-gate 
79*7c478bd9Sstevel@tonic-gate static struct modlinkage modlinkage = {
80*7c478bd9Sstevel@tonic-gate 	MODREV_1, (void *)&modlsched, NULL
81*7c478bd9Sstevel@tonic-gate };
82*7c478bd9Sstevel@tonic-gate 
83*7c478bd9Sstevel@tonic-gate int
84*7c478bd9Sstevel@tonic-gate _init()
85*7c478bd9Sstevel@tonic-gate {
86*7c478bd9Sstevel@tonic-gate 	return (mod_install(&modlinkage));
87*7c478bd9Sstevel@tonic-gate }
88*7c478bd9Sstevel@tonic-gate 
89*7c478bd9Sstevel@tonic-gate int
90*7c478bd9Sstevel@tonic-gate _fini()
91*7c478bd9Sstevel@tonic-gate {
92*7c478bd9Sstevel@tonic-gate 	return (EBUSY);		/* don't remove TS for now */
93*7c478bd9Sstevel@tonic-gate }
94*7c478bd9Sstevel@tonic-gate 
95*7c478bd9Sstevel@tonic-gate int
96*7c478bd9Sstevel@tonic-gate _info(struct modinfo *modinfop)
97*7c478bd9Sstevel@tonic-gate {
98*7c478bd9Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
99*7c478bd9Sstevel@tonic-gate }
100*7c478bd9Sstevel@tonic-gate 
101*7c478bd9Sstevel@tonic-gate /*
102*7c478bd9Sstevel@tonic-gate  * Class specific code for the time-sharing class
103*7c478bd9Sstevel@tonic-gate  */
104*7c478bd9Sstevel@tonic-gate 
105*7c478bd9Sstevel@tonic-gate 
106*7c478bd9Sstevel@tonic-gate /*
107*7c478bd9Sstevel@tonic-gate  * Extern declarations for variables defined in the ts master file
108*7c478bd9Sstevel@tonic-gate  */
109*7c478bd9Sstevel@tonic-gate #define	TSMAXUPRI 60
110*7c478bd9Sstevel@tonic-gate 
111*7c478bd9Sstevel@tonic-gate pri_t	ts_maxupri = TSMAXUPRI;	/* max time-sharing user priority */
112*7c478bd9Sstevel@tonic-gate pri_t	ts_maxumdpri;		/* maximum user mode ts priority */
113*7c478bd9Sstevel@tonic-gate 
114*7c478bd9Sstevel@tonic-gate pri_t	ia_maxupri = IAMAXUPRI;	/* max interactive user priority */
115*7c478bd9Sstevel@tonic-gate pri_t	ia_boost = IA_BOOST;	/* boost value for interactive */
116*7c478bd9Sstevel@tonic-gate 
117*7c478bd9Sstevel@tonic-gate tsdpent_t  *ts_dptbl;	/* time-sharing disp parameter table */
118*7c478bd9Sstevel@tonic-gate pri_t	*ts_kmdpris;	/* array of global pris used by ts procs when */
119*7c478bd9Sstevel@tonic-gate 			/*  sleeping or running in kernel after sleep */
120*7c478bd9Sstevel@tonic-gate 
121*7c478bd9Sstevel@tonic-gate static id_t ia_cid;
122*7c478bd9Sstevel@tonic-gate 
123*7c478bd9Sstevel@tonic-gate int ts_sleep_promote = 1;
124*7c478bd9Sstevel@tonic-gate 
125*7c478bd9Sstevel@tonic-gate #define	tsmedumdpri	(ts_maxumdpri >> 1)
126*7c478bd9Sstevel@tonic-gate 
127*7c478bd9Sstevel@tonic-gate #define	TS_NEWUMDPRI(tspp) \
128*7c478bd9Sstevel@tonic-gate { \
129*7c478bd9Sstevel@tonic-gate 	pri_t pri; \
130*7c478bd9Sstevel@tonic-gate 	pri = (tspp)->ts_cpupri + (tspp)->ts_upri + (tspp)->ts_boost; \
131*7c478bd9Sstevel@tonic-gate 	if (pri > ts_maxumdpri) \
132*7c478bd9Sstevel@tonic-gate 		(tspp)->ts_umdpri = ts_maxumdpri; \
133*7c478bd9Sstevel@tonic-gate 	else if (pri < 0) \
134*7c478bd9Sstevel@tonic-gate 		(tspp)->ts_umdpri = 0; \
135*7c478bd9Sstevel@tonic-gate 	else \
136*7c478bd9Sstevel@tonic-gate 		(tspp)->ts_umdpri = pri; \
137*7c478bd9Sstevel@tonic-gate 	ASSERT((tspp)->ts_umdpri >= 0 && (tspp)->ts_umdpri <= ts_maxumdpri); \
138*7c478bd9Sstevel@tonic-gate }
139*7c478bd9Sstevel@tonic-gate 
140*7c478bd9Sstevel@tonic-gate /*
141*7c478bd9Sstevel@tonic-gate  * The tsproc_t structures are kept in an array of circular doubly linked
142*7c478bd9Sstevel@tonic-gate  * lists.  A hash on the thread pointer is used to determine which list
143*7c478bd9Sstevel@tonic-gate  * each thread should be placed.  Each list has a dummy "head" which is
144*7c478bd9Sstevel@tonic-gate  * never removed, so the list is never empty.  ts_update traverses these
145*7c478bd9Sstevel@tonic-gate  * lists to update the priorities of threads that have been waiting on
146*7c478bd9Sstevel@tonic-gate  * the run queue.
147*7c478bd9Sstevel@tonic-gate  */
148*7c478bd9Sstevel@tonic-gate 
149*7c478bd9Sstevel@tonic-gate #define	TS_LISTS 16		/* number of lists, must be power of 2 */
150*7c478bd9Sstevel@tonic-gate 
151*7c478bd9Sstevel@tonic-gate /* hash function, argument is a thread pointer */
152*7c478bd9Sstevel@tonic-gate #define	TS_LIST_HASH(tp)	(((uintptr_t)(tp) >> 9) & (TS_LISTS - 1))
153*7c478bd9Sstevel@tonic-gate 
154*7c478bd9Sstevel@tonic-gate /* iterate to the next list */
155*7c478bd9Sstevel@tonic-gate #define	TS_LIST_NEXT(i)		(((i) + 1) & (TS_LISTS - 1))
156*7c478bd9Sstevel@tonic-gate 
157*7c478bd9Sstevel@tonic-gate /*
158*7c478bd9Sstevel@tonic-gate  * Insert thread into the appropriate tsproc list.
159*7c478bd9Sstevel@tonic-gate  */
160*7c478bd9Sstevel@tonic-gate #define	TS_LIST_INSERT(tspp)				\
161*7c478bd9Sstevel@tonic-gate {							\
162*7c478bd9Sstevel@tonic-gate 	int index = TS_LIST_HASH(tspp->ts_tp);		\
163*7c478bd9Sstevel@tonic-gate 	kmutex_t *lockp = &ts_list_lock[index];		\
164*7c478bd9Sstevel@tonic-gate 	tsproc_t *headp = &ts_plisthead[index];		\
165*7c478bd9Sstevel@tonic-gate 	mutex_enter(lockp);				\
166*7c478bd9Sstevel@tonic-gate 	tspp->ts_next = headp->ts_next;			\
167*7c478bd9Sstevel@tonic-gate 	tspp->ts_prev = headp;				\
168*7c478bd9Sstevel@tonic-gate 	headp->ts_next->ts_prev = tspp;			\
169*7c478bd9Sstevel@tonic-gate 	headp->ts_next = tspp;				\
170*7c478bd9Sstevel@tonic-gate 	mutex_exit(lockp);				\
171*7c478bd9Sstevel@tonic-gate }
172*7c478bd9Sstevel@tonic-gate 
173*7c478bd9Sstevel@tonic-gate /*
174*7c478bd9Sstevel@tonic-gate  * Remove thread from tsproc list.
175*7c478bd9Sstevel@tonic-gate  */
176*7c478bd9Sstevel@tonic-gate #define	TS_LIST_DELETE(tspp)				\
177*7c478bd9Sstevel@tonic-gate {							\
178*7c478bd9Sstevel@tonic-gate 	int index = TS_LIST_HASH(tspp->ts_tp);		\
179*7c478bd9Sstevel@tonic-gate 	kmutex_t *lockp = &ts_list_lock[index];		\
180*7c478bd9Sstevel@tonic-gate 	mutex_enter(lockp);				\
181*7c478bd9Sstevel@tonic-gate 	tspp->ts_prev->ts_next = tspp->ts_next;		\
182*7c478bd9Sstevel@tonic-gate 	tspp->ts_next->ts_prev = tspp->ts_prev;		\
183*7c478bd9Sstevel@tonic-gate 	mutex_exit(lockp);				\
184*7c478bd9Sstevel@tonic-gate }
185*7c478bd9Sstevel@tonic-gate 
186*7c478bd9Sstevel@tonic-gate 
187*7c478bd9Sstevel@tonic-gate static int	ts_admin(caddr_t, cred_t *);
188*7c478bd9Sstevel@tonic-gate static int	ts_enterclass(kthread_t *, id_t, void *, cred_t *, void *);
189*7c478bd9Sstevel@tonic-gate static int	ts_fork(kthread_t *, kthread_t *, void *);
190*7c478bd9Sstevel@tonic-gate static int	ts_getclinfo(void *);
191*7c478bd9Sstevel@tonic-gate static int	ts_getclpri(pcpri_t *);
192*7c478bd9Sstevel@tonic-gate static int	ts_parmsin(void *);
193*7c478bd9Sstevel@tonic-gate static int	ts_parmsout(void *, pc_vaparms_t *);
194*7c478bd9Sstevel@tonic-gate static int	ts_vaparmsin(void *, pc_vaparms_t *);
195*7c478bd9Sstevel@tonic-gate static int	ts_vaparmsout(void *, pc_vaparms_t *);
196*7c478bd9Sstevel@tonic-gate static int	ts_parmsset(kthread_t *, void *, id_t, cred_t *);
197*7c478bd9Sstevel@tonic-gate static int	ts_donice(kthread_t *, cred_t *, int, int *);
198*7c478bd9Sstevel@tonic-gate static void	ts_exitclass(void *);
199*7c478bd9Sstevel@tonic-gate static int	ts_canexit(kthread_t *, cred_t *);
200*7c478bd9Sstevel@tonic-gate static void	ts_forkret(kthread_t *, kthread_t *);
201*7c478bd9Sstevel@tonic-gate static void	ts_nullsys();
202*7c478bd9Sstevel@tonic-gate static void	ts_parmsget(kthread_t *, void *);
203*7c478bd9Sstevel@tonic-gate static void	ts_preempt(kthread_t *);
204*7c478bd9Sstevel@tonic-gate static void	ts_setrun(kthread_t *);
205*7c478bd9Sstevel@tonic-gate static void	ts_sleep(kthread_t *);
206*7c478bd9Sstevel@tonic-gate static pri_t	ts_swapin(kthread_t *, int);
207*7c478bd9Sstevel@tonic-gate static pri_t	ts_swapout(kthread_t *, int);
208*7c478bd9Sstevel@tonic-gate static void	ts_tick(kthread_t *);
209*7c478bd9Sstevel@tonic-gate static void	ts_trapret(kthread_t *);
210*7c478bd9Sstevel@tonic-gate static void	ts_update(void *);
211*7c478bd9Sstevel@tonic-gate static int	ts_update_list(int);
212*7c478bd9Sstevel@tonic-gate static void	ts_wakeup(kthread_t *);
213*7c478bd9Sstevel@tonic-gate static pri_t	ts_globpri(kthread_t *);
214*7c478bd9Sstevel@tonic-gate static void	ts_yield(kthread_t *);
215*7c478bd9Sstevel@tonic-gate extern tsdpent_t *ts_getdptbl(void);
216*7c478bd9Sstevel@tonic-gate extern pri_t	*ts_getkmdpris(void);
217*7c478bd9Sstevel@tonic-gate extern pri_t	td_getmaxumdpri(void);
218*7c478bd9Sstevel@tonic-gate static int	ts_alloc(void **, int);
219*7c478bd9Sstevel@tonic-gate static void	ts_free(void *);
220*7c478bd9Sstevel@tonic-gate 
221*7c478bd9Sstevel@tonic-gate pri_t		ia_init(id_t, int, classfuncs_t **);
222*7c478bd9Sstevel@tonic-gate static int	ia_getclinfo(void *);
223*7c478bd9Sstevel@tonic-gate static int	ia_parmsin(void *);
224*7c478bd9Sstevel@tonic-gate static int	ia_vaparmsin(void *, pc_vaparms_t *);
225*7c478bd9Sstevel@tonic-gate static int	ia_vaparmsout(void *, pc_vaparms_t *);
226*7c478bd9Sstevel@tonic-gate static int	ia_parmsset(kthread_t *, void *, id_t, cred_t *);
227*7c478bd9Sstevel@tonic-gate static void	ia_parmsget(kthread_t *, void *);
228*7c478bd9Sstevel@tonic-gate static void	ia_set_process_group(pid_t, pid_t, pid_t);
229*7c478bd9Sstevel@tonic-gate 
230*7c478bd9Sstevel@tonic-gate static void	ts_change_priority(kthread_t *, tsproc_t *);
231*7c478bd9Sstevel@tonic-gate 
232*7c478bd9Sstevel@tonic-gate extern pri_t	ts_maxkmdpri;	/* maximum kernel mode ts priority */
233*7c478bd9Sstevel@tonic-gate static pri_t	ts_maxglobpri;	/* maximum global priority used by ts class */
234*7c478bd9Sstevel@tonic-gate static kmutex_t	ts_dptblock;	/* protects time sharing dispatch table */
235*7c478bd9Sstevel@tonic-gate static kmutex_t	ts_list_lock[TS_LISTS];	/* protects tsproc lists */
236*7c478bd9Sstevel@tonic-gate static tsproc_t	ts_plisthead[TS_LISTS];	/* dummy tsproc at head of lists */
237*7c478bd9Sstevel@tonic-gate 
238*7c478bd9Sstevel@tonic-gate static gid_t	IA_gid = 0;
239*7c478bd9Sstevel@tonic-gate 
240*7c478bd9Sstevel@tonic-gate static struct classfuncs ts_classfuncs = {
241*7c478bd9Sstevel@tonic-gate 	/* class functions */
242*7c478bd9Sstevel@tonic-gate 	ts_admin,
243*7c478bd9Sstevel@tonic-gate 	ts_getclinfo,
244*7c478bd9Sstevel@tonic-gate 	ts_parmsin,
245*7c478bd9Sstevel@tonic-gate 	ts_parmsout,
246*7c478bd9Sstevel@tonic-gate 	ts_vaparmsin,
247*7c478bd9Sstevel@tonic-gate 	ts_vaparmsout,
248*7c478bd9Sstevel@tonic-gate 	ts_getclpri,
249*7c478bd9Sstevel@tonic-gate 	ts_alloc,
250*7c478bd9Sstevel@tonic-gate 	ts_free,
251*7c478bd9Sstevel@tonic-gate 
252*7c478bd9Sstevel@tonic-gate 	/* thread functions */
253*7c478bd9Sstevel@tonic-gate 	ts_enterclass,
254*7c478bd9Sstevel@tonic-gate 	ts_exitclass,
255*7c478bd9Sstevel@tonic-gate 	ts_canexit,
256*7c478bd9Sstevel@tonic-gate 	ts_fork,
257*7c478bd9Sstevel@tonic-gate 	ts_forkret,
258*7c478bd9Sstevel@tonic-gate 	ts_parmsget,
259*7c478bd9Sstevel@tonic-gate 	ts_parmsset,
260*7c478bd9Sstevel@tonic-gate 	ts_nullsys,	/* stop */
261*7c478bd9Sstevel@tonic-gate 	ts_nullsys,	/* exit */
262*7c478bd9Sstevel@tonic-gate 	ts_nullsys,	/* active */
263*7c478bd9Sstevel@tonic-gate 	ts_nullsys,	/* inactive */
264*7c478bd9Sstevel@tonic-gate 	ts_swapin,
265*7c478bd9Sstevel@tonic-gate 	ts_swapout,
266*7c478bd9Sstevel@tonic-gate 	ts_trapret,
267*7c478bd9Sstevel@tonic-gate 	ts_preempt,
268*7c478bd9Sstevel@tonic-gate 	ts_setrun,
269*7c478bd9Sstevel@tonic-gate 	ts_sleep,
270*7c478bd9Sstevel@tonic-gate 	ts_tick,
271*7c478bd9Sstevel@tonic-gate 	ts_wakeup,
272*7c478bd9Sstevel@tonic-gate 	ts_donice,
273*7c478bd9Sstevel@tonic-gate 	ts_globpri,
274*7c478bd9Sstevel@tonic-gate 	ts_nullsys,	/* set_process_group */
275*7c478bd9Sstevel@tonic-gate 	ts_yield,
276*7c478bd9Sstevel@tonic-gate };
277*7c478bd9Sstevel@tonic-gate 
278*7c478bd9Sstevel@tonic-gate /*
279*7c478bd9Sstevel@tonic-gate  * ia_classfuncs is used for interactive class threads; IA threads are stored
280*7c478bd9Sstevel@tonic-gate  * on the same class list as TS threads, and most of the class functions are
281*7c478bd9Sstevel@tonic-gate  * identical, but a few have different enough functionality to require their
282*7c478bd9Sstevel@tonic-gate  * own functions.
283*7c478bd9Sstevel@tonic-gate  */
284*7c478bd9Sstevel@tonic-gate static struct classfuncs ia_classfuncs = {
285*7c478bd9Sstevel@tonic-gate 	/* class functions */
286*7c478bd9Sstevel@tonic-gate 	ts_admin,
287*7c478bd9Sstevel@tonic-gate 	ia_getclinfo,
288*7c478bd9Sstevel@tonic-gate 	ia_parmsin,
289*7c478bd9Sstevel@tonic-gate 	ts_parmsout,
290*7c478bd9Sstevel@tonic-gate 	ia_vaparmsin,
291*7c478bd9Sstevel@tonic-gate 	ia_vaparmsout,
292*7c478bd9Sstevel@tonic-gate 	ts_getclpri,
293*7c478bd9Sstevel@tonic-gate 	ts_alloc,
294*7c478bd9Sstevel@tonic-gate 	ts_free,
295*7c478bd9Sstevel@tonic-gate 
296*7c478bd9Sstevel@tonic-gate 	/* thread functions */
297*7c478bd9Sstevel@tonic-gate 	ts_enterclass,
298*7c478bd9Sstevel@tonic-gate 	ts_exitclass,
299*7c478bd9Sstevel@tonic-gate 	ts_canexit,
300*7c478bd9Sstevel@tonic-gate 	ts_fork,
301*7c478bd9Sstevel@tonic-gate 	ts_forkret,
302*7c478bd9Sstevel@tonic-gate 	ia_parmsget,
303*7c478bd9Sstevel@tonic-gate 	ia_parmsset,
304*7c478bd9Sstevel@tonic-gate 	ts_nullsys,	/* stop */
305*7c478bd9Sstevel@tonic-gate 	ts_nullsys,	/* exit */
306*7c478bd9Sstevel@tonic-gate 	ts_nullsys,	/* active */
307*7c478bd9Sstevel@tonic-gate 	ts_nullsys,	/* inactive */
308*7c478bd9Sstevel@tonic-gate 	ts_swapin,
309*7c478bd9Sstevel@tonic-gate 	ts_swapout,
310*7c478bd9Sstevel@tonic-gate 	ts_trapret,
311*7c478bd9Sstevel@tonic-gate 	ts_preempt,
312*7c478bd9Sstevel@tonic-gate 	ts_setrun,
313*7c478bd9Sstevel@tonic-gate 	ts_sleep,
314*7c478bd9Sstevel@tonic-gate 	ts_tick,
315*7c478bd9Sstevel@tonic-gate 	ts_wakeup,
316*7c478bd9Sstevel@tonic-gate 	ts_donice,
317*7c478bd9Sstevel@tonic-gate 	ts_globpri,
318*7c478bd9Sstevel@tonic-gate 	ia_set_process_group,
319*7c478bd9Sstevel@tonic-gate 	ts_yield,
320*7c478bd9Sstevel@tonic-gate };
321*7c478bd9Sstevel@tonic-gate 
322*7c478bd9Sstevel@tonic-gate 
323*7c478bd9Sstevel@tonic-gate /*
324*7c478bd9Sstevel@tonic-gate  * Time sharing class initialization.  Called by dispinit() at boot time.
325*7c478bd9Sstevel@tonic-gate  * We can ignore the clparmsz argument since we know that the smallest
326*7c478bd9Sstevel@tonic-gate  * possible parameter buffer is big enough for us.
327*7c478bd9Sstevel@tonic-gate  */
328*7c478bd9Sstevel@tonic-gate /* ARGSUSED */
329*7c478bd9Sstevel@tonic-gate static pri_t
330*7c478bd9Sstevel@tonic-gate ts_init(id_t cid, int clparmsz, classfuncs_t **clfuncspp)
331*7c478bd9Sstevel@tonic-gate {
332*7c478bd9Sstevel@tonic-gate 	int i;
333*7c478bd9Sstevel@tonic-gate 	extern pri_t ts_getmaxumdpri(void);
334*7c478bd9Sstevel@tonic-gate 
335*7c478bd9Sstevel@tonic-gate 	ts_dptbl = ts_getdptbl();
336*7c478bd9Sstevel@tonic-gate 	ts_kmdpris = ts_getkmdpris();
337*7c478bd9Sstevel@tonic-gate 	ts_maxumdpri = ts_getmaxumdpri();
338*7c478bd9Sstevel@tonic-gate 	ts_maxglobpri = MAX(ts_kmdpris[0], ts_dptbl[ts_maxumdpri].ts_globpri);
339*7c478bd9Sstevel@tonic-gate 
340*7c478bd9Sstevel@tonic-gate 	/*
341*7c478bd9Sstevel@tonic-gate 	 * Initialize the tsproc lists.
342*7c478bd9Sstevel@tonic-gate 	 */
343*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < TS_LISTS; i++) {
344*7c478bd9Sstevel@tonic-gate 		ts_plisthead[i].ts_next = ts_plisthead[i].ts_prev =
345*7c478bd9Sstevel@tonic-gate 		    &ts_plisthead[i];
346*7c478bd9Sstevel@tonic-gate 	}
347*7c478bd9Sstevel@tonic-gate 
348*7c478bd9Sstevel@tonic-gate 	/*
349*7c478bd9Sstevel@tonic-gate 	 * We're required to return a pointer to our classfuncs
350*7c478bd9Sstevel@tonic-gate 	 * structure and the highest global priority value we use.
351*7c478bd9Sstevel@tonic-gate 	 */
352*7c478bd9Sstevel@tonic-gate 	*clfuncspp = &ts_classfuncs;
353*7c478bd9Sstevel@tonic-gate 	return (ts_maxglobpri);
354*7c478bd9Sstevel@tonic-gate }
355*7c478bd9Sstevel@tonic-gate 
356*7c478bd9Sstevel@tonic-gate 
357*7c478bd9Sstevel@tonic-gate /*
358*7c478bd9Sstevel@tonic-gate  * Interactive class scheduler initialization
359*7c478bd9Sstevel@tonic-gate  */
360*7c478bd9Sstevel@tonic-gate /* ARGSUSED */
361*7c478bd9Sstevel@tonic-gate pri_t
362*7c478bd9Sstevel@tonic-gate ia_init(id_t cid, int clparmsz, classfuncs_t **clfuncspp)
363*7c478bd9Sstevel@tonic-gate {
364*7c478bd9Sstevel@tonic-gate 	/*
365*7c478bd9Sstevel@tonic-gate 	 * We're required to return a pointer to our classfuncs
366*7c478bd9Sstevel@tonic-gate 	 * structure and the highest global priority value we use.
367*7c478bd9Sstevel@tonic-gate 	 */
368*7c478bd9Sstevel@tonic-gate 	ia_cid = cid;
369*7c478bd9Sstevel@tonic-gate 	*clfuncspp = &ia_classfuncs;
370*7c478bd9Sstevel@tonic-gate 	return (ts_maxglobpri);
371*7c478bd9Sstevel@tonic-gate }
372*7c478bd9Sstevel@tonic-gate 
373*7c478bd9Sstevel@tonic-gate 
374*7c478bd9Sstevel@tonic-gate /*
375*7c478bd9Sstevel@tonic-gate  * Get or reset the ts_dptbl values per the user's request.
376*7c478bd9Sstevel@tonic-gate  */
377*7c478bd9Sstevel@tonic-gate static int
378*7c478bd9Sstevel@tonic-gate ts_admin(caddr_t uaddr, cred_t *reqpcredp)
379*7c478bd9Sstevel@tonic-gate {
380*7c478bd9Sstevel@tonic-gate 	tsadmin_t	tsadmin;
381*7c478bd9Sstevel@tonic-gate 	tsdpent_t	*tmpdpp;
382*7c478bd9Sstevel@tonic-gate 	int		userdpsz;
383*7c478bd9Sstevel@tonic-gate 	int		i;
384*7c478bd9Sstevel@tonic-gate 	size_t		tsdpsz;
385*7c478bd9Sstevel@tonic-gate 
386*7c478bd9Sstevel@tonic-gate 	if (get_udatamodel() == DATAMODEL_NATIVE) {
387*7c478bd9Sstevel@tonic-gate 		if (copyin(uaddr, &tsadmin, sizeof (tsadmin_t)))
388*7c478bd9Sstevel@tonic-gate 			return (EFAULT);
389*7c478bd9Sstevel@tonic-gate 	}
390*7c478bd9Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
391*7c478bd9Sstevel@tonic-gate 	else {
392*7c478bd9Sstevel@tonic-gate 		/* get tsadmin struct from ILP32 caller */
393*7c478bd9Sstevel@tonic-gate 		tsadmin32_t tsadmin32;
394*7c478bd9Sstevel@tonic-gate 		if (copyin(uaddr, &tsadmin32, sizeof (tsadmin32_t)))
395*7c478bd9Sstevel@tonic-gate 			return (EFAULT);
396*7c478bd9Sstevel@tonic-gate 		tsadmin.ts_dpents =
397*7c478bd9Sstevel@tonic-gate 		    (struct tsdpent *)(uintptr_t)tsadmin32.ts_dpents;
398*7c478bd9Sstevel@tonic-gate 		tsadmin.ts_ndpents = tsadmin32.ts_ndpents;
399*7c478bd9Sstevel@tonic-gate 		tsadmin.ts_cmd = tsadmin32.ts_cmd;
400*7c478bd9Sstevel@tonic-gate 	}
401*7c478bd9Sstevel@tonic-gate #endif /* _SYSCALL32_IMPL */
402*7c478bd9Sstevel@tonic-gate 
403*7c478bd9Sstevel@tonic-gate 	tsdpsz = (ts_maxumdpri + 1) * sizeof (tsdpent_t);
404*7c478bd9Sstevel@tonic-gate 
405*7c478bd9Sstevel@tonic-gate 	switch (tsadmin.ts_cmd) {
406*7c478bd9Sstevel@tonic-gate 	case TS_GETDPSIZE:
407*7c478bd9Sstevel@tonic-gate 		tsadmin.ts_ndpents = ts_maxumdpri + 1;
408*7c478bd9Sstevel@tonic-gate 
409*7c478bd9Sstevel@tonic-gate 		if (get_udatamodel() == DATAMODEL_NATIVE) {
410*7c478bd9Sstevel@tonic-gate 			if (copyout(&tsadmin, uaddr, sizeof (tsadmin_t)))
411*7c478bd9Sstevel@tonic-gate 				return (EFAULT);
412*7c478bd9Sstevel@tonic-gate 		}
413*7c478bd9Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
414*7c478bd9Sstevel@tonic-gate 		else {
415*7c478bd9Sstevel@tonic-gate 			/* return tsadmin struct to ILP32 caller */
416*7c478bd9Sstevel@tonic-gate 			tsadmin32_t tsadmin32;
417*7c478bd9Sstevel@tonic-gate 			tsadmin32.ts_dpents =
418*7c478bd9Sstevel@tonic-gate 			    (caddr32_t)(uintptr_t)tsadmin.ts_dpents;
419*7c478bd9Sstevel@tonic-gate 			tsadmin32.ts_ndpents = tsadmin.ts_ndpents;
420*7c478bd9Sstevel@tonic-gate 			tsadmin32.ts_cmd = tsadmin.ts_cmd;
421*7c478bd9Sstevel@tonic-gate 			if (copyout(&tsadmin32, uaddr, sizeof (tsadmin32_t)))
422*7c478bd9Sstevel@tonic-gate 				return (EFAULT);
423*7c478bd9Sstevel@tonic-gate 		}
424*7c478bd9Sstevel@tonic-gate #endif /* _SYSCALL32_IMPL */
425*7c478bd9Sstevel@tonic-gate 		break;
426*7c478bd9Sstevel@tonic-gate 
427*7c478bd9Sstevel@tonic-gate 	case TS_GETDPTBL:
428*7c478bd9Sstevel@tonic-gate 		userdpsz = MIN(tsadmin.ts_ndpents * sizeof (tsdpent_t),
429*7c478bd9Sstevel@tonic-gate 		    tsdpsz);
430*7c478bd9Sstevel@tonic-gate 		if (copyout(ts_dptbl, tsadmin.ts_dpents, userdpsz))
431*7c478bd9Sstevel@tonic-gate 			return (EFAULT);
432*7c478bd9Sstevel@tonic-gate 
433*7c478bd9Sstevel@tonic-gate 		tsadmin.ts_ndpents = userdpsz / sizeof (tsdpent_t);
434*7c478bd9Sstevel@tonic-gate 
435*7c478bd9Sstevel@tonic-gate 		if (get_udatamodel() == DATAMODEL_NATIVE) {
436*7c478bd9Sstevel@tonic-gate 			if (copyout(&tsadmin, uaddr, sizeof (tsadmin_t)))
437*7c478bd9Sstevel@tonic-gate 				return (EFAULT);
438*7c478bd9Sstevel@tonic-gate 		}
439*7c478bd9Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
440*7c478bd9Sstevel@tonic-gate 		else {
441*7c478bd9Sstevel@tonic-gate 			/* return tsadmin struct to ILP32 callers */
442*7c478bd9Sstevel@tonic-gate 			tsadmin32_t tsadmin32;
443*7c478bd9Sstevel@tonic-gate 			tsadmin32.ts_dpents =
444*7c478bd9Sstevel@tonic-gate 			    (caddr32_t)(uintptr_t)tsadmin.ts_dpents;
445*7c478bd9Sstevel@tonic-gate 			tsadmin32.ts_ndpents = tsadmin.ts_ndpents;
446*7c478bd9Sstevel@tonic-gate 			tsadmin32.ts_cmd = tsadmin.ts_cmd;
447*7c478bd9Sstevel@tonic-gate 			if (copyout(&tsadmin32, uaddr, sizeof (tsadmin32_t)))
448*7c478bd9Sstevel@tonic-gate 				return (EFAULT);
449*7c478bd9Sstevel@tonic-gate 		}
450*7c478bd9Sstevel@tonic-gate #endif /* _SYSCALL32_IMPL */
451*7c478bd9Sstevel@tonic-gate 		break;
452*7c478bd9Sstevel@tonic-gate 
453*7c478bd9Sstevel@tonic-gate 	case TS_SETDPTBL:
454*7c478bd9Sstevel@tonic-gate 		/*
455*7c478bd9Sstevel@tonic-gate 		 * We require that the requesting process has sufficient
456*7c478bd9Sstevel@tonic-gate 		 * priveleges.  We also require that the table supplied by
457*7c478bd9Sstevel@tonic-gate 		 * the user exactly match the current ts_dptbl in size.
458*7c478bd9Sstevel@tonic-gate 		 */
459*7c478bd9Sstevel@tonic-gate 		if (secpolicy_dispadm(reqpcredp) != 0)
460*7c478bd9Sstevel@tonic-gate 			return (EPERM);
461*7c478bd9Sstevel@tonic-gate 
462*7c478bd9Sstevel@tonic-gate 		if (tsadmin.ts_ndpents * sizeof (tsdpent_t) != tsdpsz) {
463*7c478bd9Sstevel@tonic-gate 			return (EINVAL);
464*7c478bd9Sstevel@tonic-gate 		}
465*7c478bd9Sstevel@tonic-gate 
466*7c478bd9Sstevel@tonic-gate 		/*
467*7c478bd9Sstevel@tonic-gate 		 * We read the user supplied table into a temporary buffer
468*7c478bd9Sstevel@tonic-gate 		 * where it is validated before being copied over the
469*7c478bd9Sstevel@tonic-gate 		 * ts_dptbl.
470*7c478bd9Sstevel@tonic-gate 		 */
471*7c478bd9Sstevel@tonic-gate 		tmpdpp = kmem_alloc(tsdpsz, KM_SLEEP);
472*7c478bd9Sstevel@tonic-gate 		if (copyin((caddr_t)tsadmin.ts_dpents, (caddr_t)tmpdpp,
473*7c478bd9Sstevel@tonic-gate 		    tsdpsz)) {
474*7c478bd9Sstevel@tonic-gate 			kmem_free(tmpdpp, tsdpsz);
475*7c478bd9Sstevel@tonic-gate 			return (EFAULT);
476*7c478bd9Sstevel@tonic-gate 		}
477*7c478bd9Sstevel@tonic-gate 		for (i = 0; i < tsadmin.ts_ndpents; i++) {
478*7c478bd9Sstevel@tonic-gate 
479*7c478bd9Sstevel@tonic-gate 			/*
480*7c478bd9Sstevel@tonic-gate 			 * Validate the user supplied values.  All we are doing
481*7c478bd9Sstevel@tonic-gate 			 * here is verifying that the values are within their
482*7c478bd9Sstevel@tonic-gate 			 * allowable ranges and will not panic the system.  We
483*7c478bd9Sstevel@tonic-gate 			 * make no attempt to ensure that the resulting
484*7c478bd9Sstevel@tonic-gate 			 * configuration makes sense or results in reasonable
485*7c478bd9Sstevel@tonic-gate 			 * performance.
486*7c478bd9Sstevel@tonic-gate 			 */
487*7c478bd9Sstevel@tonic-gate 			if (tmpdpp[i].ts_quantum <= 0) {
488*7c478bd9Sstevel@tonic-gate 				kmem_free(tmpdpp, tsdpsz);
489*7c478bd9Sstevel@tonic-gate 				return (EINVAL);
490*7c478bd9Sstevel@tonic-gate 			}
491*7c478bd9Sstevel@tonic-gate 			if (tmpdpp[i].ts_tqexp > ts_maxumdpri ||
492*7c478bd9Sstevel@tonic-gate 			    tmpdpp[i].ts_tqexp < 0) {
493*7c478bd9Sstevel@tonic-gate 				kmem_free(tmpdpp, tsdpsz);
494*7c478bd9Sstevel@tonic-gate 				return (EINVAL);
495*7c478bd9Sstevel@tonic-gate 			}
496*7c478bd9Sstevel@tonic-gate 			if (tmpdpp[i].ts_slpret > ts_maxumdpri ||
497*7c478bd9Sstevel@tonic-gate 			    tmpdpp[i].ts_slpret < 0) {
498*7c478bd9Sstevel@tonic-gate 				kmem_free(tmpdpp, tsdpsz);
499*7c478bd9Sstevel@tonic-gate 				return (EINVAL);
500*7c478bd9Sstevel@tonic-gate 			}
501*7c478bd9Sstevel@tonic-gate 			if (tmpdpp[i].ts_maxwait < 0) {
502*7c478bd9Sstevel@tonic-gate 				kmem_free(tmpdpp, tsdpsz);
503*7c478bd9Sstevel@tonic-gate 				return (EINVAL);
504*7c478bd9Sstevel@tonic-gate 			}
505*7c478bd9Sstevel@tonic-gate 			if (tmpdpp[i].ts_lwait > ts_maxumdpri ||
506*7c478bd9Sstevel@tonic-gate 			    tmpdpp[i].ts_lwait < 0) {
507*7c478bd9Sstevel@tonic-gate 				kmem_free(tmpdpp, tsdpsz);
508*7c478bd9Sstevel@tonic-gate 				return (EINVAL);
509*7c478bd9Sstevel@tonic-gate 			}
510*7c478bd9Sstevel@tonic-gate 		}
511*7c478bd9Sstevel@tonic-gate 
512*7c478bd9Sstevel@tonic-gate 		/*
513*7c478bd9Sstevel@tonic-gate 		 * Copy the user supplied values over the current ts_dptbl
514*7c478bd9Sstevel@tonic-gate 		 * values.  The ts_globpri member is read-only so we don't
515*7c478bd9Sstevel@tonic-gate 		 * overwrite it.
516*7c478bd9Sstevel@tonic-gate 		 */
517*7c478bd9Sstevel@tonic-gate 		mutex_enter(&ts_dptblock);
518*7c478bd9Sstevel@tonic-gate 		for (i = 0; i < tsadmin.ts_ndpents; i++) {
519*7c478bd9Sstevel@tonic-gate 			ts_dptbl[i].ts_quantum = tmpdpp[i].ts_quantum;
520*7c478bd9Sstevel@tonic-gate 			ts_dptbl[i].ts_tqexp = tmpdpp[i].ts_tqexp;
521*7c478bd9Sstevel@tonic-gate 			ts_dptbl[i].ts_slpret = tmpdpp[i].ts_slpret;
522*7c478bd9Sstevel@tonic-gate 			ts_dptbl[i].ts_maxwait = tmpdpp[i].ts_maxwait;
523*7c478bd9Sstevel@tonic-gate 			ts_dptbl[i].ts_lwait = tmpdpp[i].ts_lwait;
524*7c478bd9Sstevel@tonic-gate 		}
525*7c478bd9Sstevel@tonic-gate 		mutex_exit(&ts_dptblock);
526*7c478bd9Sstevel@tonic-gate 		kmem_free(tmpdpp, tsdpsz);
527*7c478bd9Sstevel@tonic-gate 		break;
528*7c478bd9Sstevel@tonic-gate 
529*7c478bd9Sstevel@tonic-gate 	default:
530*7c478bd9Sstevel@tonic-gate 		return (EINVAL);
531*7c478bd9Sstevel@tonic-gate 	}
532*7c478bd9Sstevel@tonic-gate 	return (0);
533*7c478bd9Sstevel@tonic-gate }
534*7c478bd9Sstevel@tonic-gate 
535*7c478bd9Sstevel@tonic-gate 
536*7c478bd9Sstevel@tonic-gate /*
537*7c478bd9Sstevel@tonic-gate  * Allocate a time-sharing class specific thread structure and
538*7c478bd9Sstevel@tonic-gate  * initialize it with the parameters supplied. Also move the thread
539*7c478bd9Sstevel@tonic-gate  * to specified time-sharing priority.
540*7c478bd9Sstevel@tonic-gate  */
541*7c478bd9Sstevel@tonic-gate static int
542*7c478bd9Sstevel@tonic-gate ts_enterclass(kthread_t *t, id_t cid, void *parmsp,
543*7c478bd9Sstevel@tonic-gate 	cred_t *reqpcredp, void *bufp)
544*7c478bd9Sstevel@tonic-gate {
545*7c478bd9Sstevel@tonic-gate 	tsparms_t	*tsparmsp = (tsparms_t *)parmsp;
546*7c478bd9Sstevel@tonic-gate 	tsproc_t	*tspp;
547*7c478bd9Sstevel@tonic-gate 	pri_t		reqtsuprilim;
548*7c478bd9Sstevel@tonic-gate 	pri_t		reqtsupri;
549*7c478bd9Sstevel@tonic-gate 	static uint32_t	tspexists = 0;	/* set on first occurrence of */
550*7c478bd9Sstevel@tonic-gate 					/*   a time-sharing process */
551*7c478bd9Sstevel@tonic-gate 
552*7c478bd9Sstevel@tonic-gate 	tspp = (tsproc_t *)bufp;
553*7c478bd9Sstevel@tonic-gate 	ASSERT(tspp != NULL);
554*7c478bd9Sstevel@tonic-gate 
555*7c478bd9Sstevel@tonic-gate 	/*
556*7c478bd9Sstevel@tonic-gate 	 * Initialize the tsproc structure.
557*7c478bd9Sstevel@tonic-gate 	 */
558*7c478bd9Sstevel@tonic-gate 	tspp->ts_cpupri = tsmedumdpri;
559*7c478bd9Sstevel@tonic-gate 	if (cid == ia_cid) {
560*7c478bd9Sstevel@tonic-gate 		/*
561*7c478bd9Sstevel@tonic-gate 		 * Check to make sure caller is either privileged or the
562*7c478bd9Sstevel@tonic-gate 		 * window system.  When the window system is converted
563*7c478bd9Sstevel@tonic-gate 		 * to using privileges, the second check can go away.
564*7c478bd9Sstevel@tonic-gate 		 */
565*7c478bd9Sstevel@tonic-gate 		if (reqpcredp != NULL && !groupmember(IA_gid, reqpcredp) &&
566*7c478bd9Sstevel@tonic-gate 		    secpolicy_setpriority(reqpcredp) != 0)
567*7c478bd9Sstevel@tonic-gate 			return (EPERM);
568*7c478bd9Sstevel@tonic-gate 		/*
569*7c478bd9Sstevel@tonic-gate 		 * Belongs to IA "class", so set appropriate flags.
570*7c478bd9Sstevel@tonic-gate 		 * Mark as 'on' so it will not be a swap victim
571*7c478bd9Sstevel@tonic-gate 		 * while forking.
572*7c478bd9Sstevel@tonic-gate 		 */
573*7c478bd9Sstevel@tonic-gate 		tspp->ts_flags = TSIA | TSIASET;
574*7c478bd9Sstevel@tonic-gate 		tspp->ts_boost = ia_boost;
575*7c478bd9Sstevel@tonic-gate 	} else {
576*7c478bd9Sstevel@tonic-gate 		tspp->ts_flags = 0;
577*7c478bd9Sstevel@tonic-gate 		tspp->ts_boost = 0;
578*7c478bd9Sstevel@tonic-gate 	}
579*7c478bd9Sstevel@tonic-gate 
580*7c478bd9Sstevel@tonic-gate 	if (tsparmsp == NULL) {
581*7c478bd9Sstevel@tonic-gate 		/*
582*7c478bd9Sstevel@tonic-gate 		 * Use default values.
583*7c478bd9Sstevel@tonic-gate 		 */
584*7c478bd9Sstevel@tonic-gate 		tspp->ts_uprilim = tspp->ts_upri = 0;
585*7c478bd9Sstevel@tonic-gate 		tspp->ts_nice = NZERO;
586*7c478bd9Sstevel@tonic-gate 	} else {
587*7c478bd9Sstevel@tonic-gate 		/*
588*7c478bd9Sstevel@tonic-gate 		 * Use supplied values.
589*7c478bd9Sstevel@tonic-gate 		 */
590*7c478bd9Sstevel@tonic-gate 		if (tsparmsp->ts_uprilim == TS_NOCHANGE)
591*7c478bd9Sstevel@tonic-gate 			reqtsuprilim = 0;
592*7c478bd9Sstevel@tonic-gate 		else {
593*7c478bd9Sstevel@tonic-gate 			if (tsparmsp->ts_uprilim > 0 &&
594*7c478bd9Sstevel@tonic-gate 			    secpolicy_setpriority(reqpcredp) != 0)
595*7c478bd9Sstevel@tonic-gate 				return (EPERM);
596*7c478bd9Sstevel@tonic-gate 			reqtsuprilim = tsparmsp->ts_uprilim;
597*7c478bd9Sstevel@tonic-gate 		}
598*7c478bd9Sstevel@tonic-gate 
599*7c478bd9Sstevel@tonic-gate 		if (tsparmsp->ts_upri == TS_NOCHANGE) {
600*7c478bd9Sstevel@tonic-gate 			reqtsupri = reqtsuprilim;
601*7c478bd9Sstevel@tonic-gate 		} else {
602*7c478bd9Sstevel@tonic-gate 			if (tsparmsp->ts_upri > 0 &&
603*7c478bd9Sstevel@tonic-gate 			    secpolicy_setpriority(reqpcredp) != 0)
604*7c478bd9Sstevel@tonic-gate 				return (EPERM);
605*7c478bd9Sstevel@tonic-gate 			/*
606*7c478bd9Sstevel@tonic-gate 			 * Set the user priority to the requested value
607*7c478bd9Sstevel@tonic-gate 			 * or the upri limit, whichever is lower.
608*7c478bd9Sstevel@tonic-gate 			 */
609*7c478bd9Sstevel@tonic-gate 			reqtsupri = tsparmsp->ts_upri;
610*7c478bd9Sstevel@tonic-gate 			if (reqtsupri > reqtsuprilim)
611*7c478bd9Sstevel@tonic-gate 				reqtsupri = reqtsuprilim;
612*7c478bd9Sstevel@tonic-gate 		}
613*7c478bd9Sstevel@tonic-gate 
614*7c478bd9Sstevel@tonic-gate 
615*7c478bd9Sstevel@tonic-gate 		tspp->ts_uprilim = reqtsuprilim;
616*7c478bd9Sstevel@tonic-gate 		tspp->ts_upri = reqtsupri;
617*7c478bd9Sstevel@tonic-gate 		tspp->ts_nice = NZERO - (NZERO * reqtsupri)
618*7c478bd9Sstevel@tonic-gate 			/ ts_maxupri;
619*7c478bd9Sstevel@tonic-gate 	}
620*7c478bd9Sstevel@tonic-gate 	TS_NEWUMDPRI(tspp);
621*7c478bd9Sstevel@tonic-gate 
622*7c478bd9Sstevel@tonic-gate 	tspp->ts_dispwait = 0;
623*7c478bd9Sstevel@tonic-gate 	tspp->ts_timeleft = ts_dptbl[tspp->ts_cpupri].ts_quantum;
624*7c478bd9Sstevel@tonic-gate 	tspp->ts_tp = t;
625*7c478bd9Sstevel@tonic-gate 
626*7c478bd9Sstevel@tonic-gate 	/*
627*7c478bd9Sstevel@tonic-gate 	 * Reset priority. Process goes to a "user mode" priority
628*7c478bd9Sstevel@tonic-gate 	 * here regardless of whether or not it has slept since
629*7c478bd9Sstevel@tonic-gate 	 * entering the kernel.
630*7c478bd9Sstevel@tonic-gate 	 */
631*7c478bd9Sstevel@tonic-gate 	thread_lock(t);			/* get dispatcher lock on thread */
632*7c478bd9Sstevel@tonic-gate 	t->t_clfuncs = &(sclass[cid].cl_funcs->thread);
633*7c478bd9Sstevel@tonic-gate 	t->t_cid = cid;
634*7c478bd9Sstevel@tonic-gate 	t->t_cldata = (void *)tspp;
635*7c478bd9Sstevel@tonic-gate 	t->t_schedflag &= ~TS_RUNQMATCH;
636*7c478bd9Sstevel@tonic-gate 	ts_change_priority(t, tspp);
637*7c478bd9Sstevel@tonic-gate 	thread_unlock(t);
638*7c478bd9Sstevel@tonic-gate 
639*7c478bd9Sstevel@tonic-gate 	/*
640*7c478bd9Sstevel@tonic-gate 	 * Link new structure into tsproc list.
641*7c478bd9Sstevel@tonic-gate 	 */
642*7c478bd9Sstevel@tonic-gate 	TS_LIST_INSERT(tspp);
643*7c478bd9Sstevel@tonic-gate 
644*7c478bd9Sstevel@tonic-gate 	/*
645*7c478bd9Sstevel@tonic-gate 	 * If this is the first time-sharing thread to occur since
646*7c478bd9Sstevel@tonic-gate 	 * boot we set up the initial call to ts_update() here.
647*7c478bd9Sstevel@tonic-gate 	 * Use an atomic compare-and-swap since that's easier and
648*7c478bd9Sstevel@tonic-gate 	 * faster than a mutex (but check with an ordinary load first
649*7c478bd9Sstevel@tonic-gate 	 * since most of the time this will already be done).
650*7c478bd9Sstevel@tonic-gate 	 */
651*7c478bd9Sstevel@tonic-gate 	if (tspexists == 0 && cas32(&tspexists, 0, 1) == 0)
652*7c478bd9Sstevel@tonic-gate 		(void) timeout(ts_update, NULL, hz);
653*7c478bd9Sstevel@tonic-gate 
654*7c478bd9Sstevel@tonic-gate 	return (0);
655*7c478bd9Sstevel@tonic-gate }
656*7c478bd9Sstevel@tonic-gate 
657*7c478bd9Sstevel@tonic-gate 
658*7c478bd9Sstevel@tonic-gate /*
659*7c478bd9Sstevel@tonic-gate  * Free tsproc structure of thread.
660*7c478bd9Sstevel@tonic-gate  */
661*7c478bd9Sstevel@tonic-gate static void
662*7c478bd9Sstevel@tonic-gate ts_exitclass(void *procp)
663*7c478bd9Sstevel@tonic-gate {
664*7c478bd9Sstevel@tonic-gate 	tsproc_t *tspp = (tsproc_t *)procp;
665*7c478bd9Sstevel@tonic-gate 
666*7c478bd9Sstevel@tonic-gate 	/* Remove tsproc_t structure from list */
667*7c478bd9Sstevel@tonic-gate 	TS_LIST_DELETE(tspp);
668*7c478bd9Sstevel@tonic-gate 	kmem_free(tspp, sizeof (tsproc_t));
669*7c478bd9Sstevel@tonic-gate }
670*7c478bd9Sstevel@tonic-gate 
671*7c478bd9Sstevel@tonic-gate /* ARGSUSED */
672*7c478bd9Sstevel@tonic-gate static int
673*7c478bd9Sstevel@tonic-gate ts_canexit(kthread_t *t, cred_t *cred)
674*7c478bd9Sstevel@tonic-gate {
675*7c478bd9Sstevel@tonic-gate 	/*
676*7c478bd9Sstevel@tonic-gate 	 * A thread can always leave a TS/IA class
677*7c478bd9Sstevel@tonic-gate 	 */
678*7c478bd9Sstevel@tonic-gate 	return (0);
679*7c478bd9Sstevel@tonic-gate }
680*7c478bd9Sstevel@tonic-gate 
681*7c478bd9Sstevel@tonic-gate static int
682*7c478bd9Sstevel@tonic-gate ts_fork(kthread_t *t, kthread_t *ct, void *bufp)
683*7c478bd9Sstevel@tonic-gate {
684*7c478bd9Sstevel@tonic-gate 	tsproc_t	*ptspp;		/* ptr to parent's tsproc structure */
685*7c478bd9Sstevel@tonic-gate 	tsproc_t	*ctspp;		/* ptr to child's tsproc structure */
686*7c478bd9Sstevel@tonic-gate 
687*7c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&ttoproc(t)->p_lock));
688*7c478bd9Sstevel@tonic-gate 
689*7c478bd9Sstevel@tonic-gate 	ctspp = (tsproc_t *)bufp;
690*7c478bd9Sstevel@tonic-gate 	ASSERT(ctspp != NULL);
691*7c478bd9Sstevel@tonic-gate 	ptspp = (tsproc_t *)t->t_cldata;
692*7c478bd9Sstevel@tonic-gate 	/*
693*7c478bd9Sstevel@tonic-gate 	 * Initialize child's tsproc structure.
694*7c478bd9Sstevel@tonic-gate 	 */
695*7c478bd9Sstevel@tonic-gate 	thread_lock(t);
696*7c478bd9Sstevel@tonic-gate 	ctspp->ts_timeleft = ts_dptbl[ptspp->ts_cpupri].ts_quantum;
697*7c478bd9Sstevel@tonic-gate 	ctspp->ts_cpupri = ptspp->ts_cpupri;
698*7c478bd9Sstevel@tonic-gate 	ctspp->ts_boost = ptspp->ts_boost;
699*7c478bd9Sstevel@tonic-gate 	ctspp->ts_uprilim = ptspp->ts_uprilim;
700*7c478bd9Sstevel@tonic-gate 	ctspp->ts_upri = ptspp->ts_upri;
701*7c478bd9Sstevel@tonic-gate 	TS_NEWUMDPRI(ctspp);
702*7c478bd9Sstevel@tonic-gate 	ctspp->ts_nice = ptspp->ts_nice;
703*7c478bd9Sstevel@tonic-gate 	ctspp->ts_dispwait = 0;
704*7c478bd9Sstevel@tonic-gate 	ctspp->ts_flags = ptspp->ts_flags & ~(TSKPRI | TSBACKQ);
705*7c478bd9Sstevel@tonic-gate 	ctspp->ts_tp = ct;
706*7c478bd9Sstevel@tonic-gate 	thread_unlock(t);
707*7c478bd9Sstevel@tonic-gate 
708*7c478bd9Sstevel@tonic-gate 	/*
709*7c478bd9Sstevel@tonic-gate 	 * Link new structure into tsproc list.
710*7c478bd9Sstevel@tonic-gate 	 */
711*7c478bd9Sstevel@tonic-gate 	ct->t_cldata = (void *)ctspp;
712*7c478bd9Sstevel@tonic-gate 	TS_LIST_INSERT(ctspp);
713*7c478bd9Sstevel@tonic-gate 	return (0);
714*7c478bd9Sstevel@tonic-gate }
715*7c478bd9Sstevel@tonic-gate 
716*7c478bd9Sstevel@tonic-gate 
717*7c478bd9Sstevel@tonic-gate /*
718*7c478bd9Sstevel@tonic-gate  * Child is placed at back of dispatcher queue and parent gives
719*7c478bd9Sstevel@tonic-gate  * up processor so that the child runs first after the fork.
720*7c478bd9Sstevel@tonic-gate  * This allows the child immediately execing to break the multiple
721*7c478bd9Sstevel@tonic-gate  * use of copy on write pages with no disk home. The parent will
722*7c478bd9Sstevel@tonic-gate  * get to steal them back rather than uselessly copying them.
723*7c478bd9Sstevel@tonic-gate  */
724*7c478bd9Sstevel@tonic-gate static void
725*7c478bd9Sstevel@tonic-gate ts_forkret(kthread_t *t, kthread_t *ct)
726*7c478bd9Sstevel@tonic-gate {
727*7c478bd9Sstevel@tonic-gate 	proc_t	*pp = ttoproc(t);
728*7c478bd9Sstevel@tonic-gate 	proc_t	*cp = ttoproc(ct);
729*7c478bd9Sstevel@tonic-gate 	tsproc_t *tspp;
730*7c478bd9Sstevel@tonic-gate 
731*7c478bd9Sstevel@tonic-gate 	ASSERT(t == curthread);
732*7c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&pidlock));
733*7c478bd9Sstevel@tonic-gate 
734*7c478bd9Sstevel@tonic-gate 	/*
735*7c478bd9Sstevel@tonic-gate 	 * Grab the child's p_lock before dropping pidlock to ensure
736*7c478bd9Sstevel@tonic-gate 	 * the process does not disappear before we set it running.
737*7c478bd9Sstevel@tonic-gate 	 */
738*7c478bd9Sstevel@tonic-gate 	mutex_enter(&cp->p_lock);
739*7c478bd9Sstevel@tonic-gate 	mutex_exit(&pidlock);
740*7c478bd9Sstevel@tonic-gate 	continuelwps(cp);
741*7c478bd9Sstevel@tonic-gate 	mutex_exit(&cp->p_lock);
742*7c478bd9Sstevel@tonic-gate 
743*7c478bd9Sstevel@tonic-gate 	mutex_enter(&pp->p_lock);
744*7c478bd9Sstevel@tonic-gate 	continuelwps(pp);
745*7c478bd9Sstevel@tonic-gate 	mutex_exit(&pp->p_lock);
746*7c478bd9Sstevel@tonic-gate 
747*7c478bd9Sstevel@tonic-gate 	thread_lock(t);
748*7c478bd9Sstevel@tonic-gate 	tspp = (tsproc_t *)(t->t_cldata);
749*7c478bd9Sstevel@tonic-gate 	tspp->ts_cpupri = ts_dptbl[tspp->ts_cpupri].ts_tqexp;
750*7c478bd9Sstevel@tonic-gate 	TS_NEWUMDPRI(tspp);
751*7c478bd9Sstevel@tonic-gate 	tspp->ts_timeleft = ts_dptbl[tspp->ts_cpupri].ts_quantum;
752*7c478bd9Sstevel@tonic-gate 	tspp->ts_dispwait = 0;
753*7c478bd9Sstevel@tonic-gate 	t->t_pri = ts_dptbl[tspp->ts_umdpri].ts_globpri;
754*7c478bd9Sstevel@tonic-gate 	ASSERT(t->t_pri >= 0 && t->t_pri <= ts_maxglobpri);
755*7c478bd9Sstevel@tonic-gate 	tspp->ts_flags &= ~TSKPRI;
756*7c478bd9Sstevel@tonic-gate 	THREAD_TRANSITION(t);
757*7c478bd9Sstevel@tonic-gate 	ts_setrun(t);
758*7c478bd9Sstevel@tonic-gate 	thread_unlock(t);
759*7c478bd9Sstevel@tonic-gate 
760*7c478bd9Sstevel@tonic-gate 	swtch();
761*7c478bd9Sstevel@tonic-gate }
762*7c478bd9Sstevel@tonic-gate 
763*7c478bd9Sstevel@tonic-gate 
764*7c478bd9Sstevel@tonic-gate /*
765*7c478bd9Sstevel@tonic-gate  * Get information about the time-sharing class into the buffer
766*7c478bd9Sstevel@tonic-gate  * pointed to by tsinfop. The maximum configured user priority
767*7c478bd9Sstevel@tonic-gate  * is the only information we supply.  ts_getclinfo() is called
768*7c478bd9Sstevel@tonic-gate  * for TS threads, and ia_getclinfo() is called for IA threads.
769*7c478bd9Sstevel@tonic-gate  */
770*7c478bd9Sstevel@tonic-gate static int
771*7c478bd9Sstevel@tonic-gate ts_getclinfo(void *infop)
772*7c478bd9Sstevel@tonic-gate {
773*7c478bd9Sstevel@tonic-gate 	tsinfo_t *tsinfop = (tsinfo_t *)infop;
774*7c478bd9Sstevel@tonic-gate 	tsinfop->ts_maxupri = ts_maxupri;
775*7c478bd9Sstevel@tonic-gate 	return (0);
776*7c478bd9Sstevel@tonic-gate }
777*7c478bd9Sstevel@tonic-gate 
778*7c478bd9Sstevel@tonic-gate static int
779*7c478bd9Sstevel@tonic-gate ia_getclinfo(void *infop)
780*7c478bd9Sstevel@tonic-gate {
781*7c478bd9Sstevel@tonic-gate 	iainfo_t *iainfop = (iainfo_t *)infop;
782*7c478bd9Sstevel@tonic-gate 	iainfop->ia_maxupri = ia_maxupri;
783*7c478bd9Sstevel@tonic-gate 	return (0);
784*7c478bd9Sstevel@tonic-gate }
785*7c478bd9Sstevel@tonic-gate 
786*7c478bd9Sstevel@tonic-gate 
787*7c478bd9Sstevel@tonic-gate /*
788*7c478bd9Sstevel@tonic-gate  * Return the global scheduling priority ranges for the timesharing
789*7c478bd9Sstevel@tonic-gate  * class in pcpri_t structure.
790*7c478bd9Sstevel@tonic-gate  */
791*7c478bd9Sstevel@tonic-gate static int
792*7c478bd9Sstevel@tonic-gate ts_getclpri(pcpri_t *pcprip)
793*7c478bd9Sstevel@tonic-gate {
794*7c478bd9Sstevel@tonic-gate 	pcprip->pc_clpmax = ts_dptbl[ts_maxumdpri].ts_globpri;
795*7c478bd9Sstevel@tonic-gate 	pcprip->pc_clpmin = ts_dptbl[0].ts_globpri;
796*7c478bd9Sstevel@tonic-gate 	return (0);
797*7c478bd9Sstevel@tonic-gate }
798*7c478bd9Sstevel@tonic-gate 
799*7c478bd9Sstevel@tonic-gate 
800*7c478bd9Sstevel@tonic-gate static void
801*7c478bd9Sstevel@tonic-gate ts_nullsys()
802*7c478bd9Sstevel@tonic-gate {}
803*7c478bd9Sstevel@tonic-gate 
804*7c478bd9Sstevel@tonic-gate 
805*7c478bd9Sstevel@tonic-gate /*
806*7c478bd9Sstevel@tonic-gate  * Get the time-sharing parameters of the thread pointed to by
807*7c478bd9Sstevel@tonic-gate  * tsprocp into the buffer pointed to by tsparmsp.  ts_parmsget()
808*7c478bd9Sstevel@tonic-gate  * is called for TS threads, and ia_parmsget() is called for IA
809*7c478bd9Sstevel@tonic-gate  * threads.
810*7c478bd9Sstevel@tonic-gate  */
811*7c478bd9Sstevel@tonic-gate static void
812*7c478bd9Sstevel@tonic-gate ts_parmsget(kthread_t *t, void *parmsp)
813*7c478bd9Sstevel@tonic-gate {
814*7c478bd9Sstevel@tonic-gate 	tsproc_t *tspp = (tsproc_t *)t->t_cldata;
815*7c478bd9Sstevel@tonic-gate 	tsparms_t *tsparmsp = (tsparms_t *)parmsp;
816*7c478bd9Sstevel@tonic-gate 
817*7c478bd9Sstevel@tonic-gate 	tsparmsp->ts_uprilim = tspp->ts_uprilim;
818*7c478bd9Sstevel@tonic-gate 	tsparmsp->ts_upri = tspp->ts_upri;
819*7c478bd9Sstevel@tonic-gate }
820*7c478bd9Sstevel@tonic-gate 
821*7c478bd9Sstevel@tonic-gate static void
822*7c478bd9Sstevel@tonic-gate ia_parmsget(kthread_t *t, void *parmsp)
823*7c478bd9Sstevel@tonic-gate {
824*7c478bd9Sstevel@tonic-gate 	tsproc_t *tspp = (tsproc_t *)t->t_cldata;
825*7c478bd9Sstevel@tonic-gate 	iaparms_t *iaparmsp = (iaparms_t *)parmsp;
826*7c478bd9Sstevel@tonic-gate 
827*7c478bd9Sstevel@tonic-gate 	iaparmsp->ia_uprilim = tspp->ts_uprilim;
828*7c478bd9Sstevel@tonic-gate 	iaparmsp->ia_upri = tspp->ts_upri;
829*7c478bd9Sstevel@tonic-gate 	if (tspp->ts_flags & TSIASET)
830*7c478bd9Sstevel@tonic-gate 		iaparmsp->ia_mode = IA_SET_INTERACTIVE;
831*7c478bd9Sstevel@tonic-gate 	else
832*7c478bd9Sstevel@tonic-gate 		iaparmsp->ia_mode = IA_INTERACTIVE_OFF;
833*7c478bd9Sstevel@tonic-gate 	iaparmsp->ia_nice = tspp->ts_nice;
834*7c478bd9Sstevel@tonic-gate }
835*7c478bd9Sstevel@tonic-gate 
836*7c478bd9Sstevel@tonic-gate 
837*7c478bd9Sstevel@tonic-gate /*
838*7c478bd9Sstevel@tonic-gate  * Check the validity of the time-sharing parameters in the buffer
839*7c478bd9Sstevel@tonic-gate  * pointed to by tsparmsp.
840*7c478bd9Sstevel@tonic-gate  * ts_parmsin() is called for TS threads, and ia_parmsin() is called
841*7c478bd9Sstevel@tonic-gate  * for IA threads.
842*7c478bd9Sstevel@tonic-gate  */
843*7c478bd9Sstevel@tonic-gate static int
844*7c478bd9Sstevel@tonic-gate ts_parmsin(void *parmsp)
845*7c478bd9Sstevel@tonic-gate {
846*7c478bd9Sstevel@tonic-gate 	tsparms_t	*tsparmsp = (tsparms_t *)parmsp;
847*7c478bd9Sstevel@tonic-gate 	/*
848*7c478bd9Sstevel@tonic-gate 	 * Check validity of parameters.
849*7c478bd9Sstevel@tonic-gate 	 */
850*7c478bd9Sstevel@tonic-gate 	if ((tsparmsp->ts_uprilim > ts_maxupri ||
851*7c478bd9Sstevel@tonic-gate 	    tsparmsp->ts_uprilim < -ts_maxupri) &&
852*7c478bd9Sstevel@tonic-gate 	    tsparmsp->ts_uprilim != TS_NOCHANGE)
853*7c478bd9Sstevel@tonic-gate 		return (EINVAL);
854*7c478bd9Sstevel@tonic-gate 
855*7c478bd9Sstevel@tonic-gate 	if ((tsparmsp->ts_upri > ts_maxupri ||
856*7c478bd9Sstevel@tonic-gate 	    tsparmsp->ts_upri < -ts_maxupri) &&
857*7c478bd9Sstevel@tonic-gate 	    tsparmsp->ts_upri != TS_NOCHANGE)
858*7c478bd9Sstevel@tonic-gate 		return (EINVAL);
859*7c478bd9Sstevel@tonic-gate 
860*7c478bd9Sstevel@tonic-gate 	return (0);
861*7c478bd9Sstevel@tonic-gate }
862*7c478bd9Sstevel@tonic-gate 
863*7c478bd9Sstevel@tonic-gate static int
864*7c478bd9Sstevel@tonic-gate ia_parmsin(void *parmsp)
865*7c478bd9Sstevel@tonic-gate {
866*7c478bd9Sstevel@tonic-gate 	iaparms_t	*iaparmsp = (iaparms_t *)parmsp;
867*7c478bd9Sstevel@tonic-gate 
868*7c478bd9Sstevel@tonic-gate 	if ((iaparmsp->ia_uprilim > ia_maxupri ||
869*7c478bd9Sstevel@tonic-gate 	    iaparmsp->ia_uprilim < -ia_maxupri) &&
870*7c478bd9Sstevel@tonic-gate 	    iaparmsp->ia_uprilim != IA_NOCHANGE) {
871*7c478bd9Sstevel@tonic-gate 		return (EINVAL);
872*7c478bd9Sstevel@tonic-gate 	}
873*7c478bd9Sstevel@tonic-gate 
874*7c478bd9Sstevel@tonic-gate 	if ((iaparmsp->ia_upri > ia_maxupri ||
875*7c478bd9Sstevel@tonic-gate 	    iaparmsp->ia_upri < -ia_maxupri) &&
876*7c478bd9Sstevel@tonic-gate 	    iaparmsp->ia_upri != IA_NOCHANGE) {
877*7c478bd9Sstevel@tonic-gate 		return (EINVAL);
878*7c478bd9Sstevel@tonic-gate 	}
879*7c478bd9Sstevel@tonic-gate 
880*7c478bd9Sstevel@tonic-gate 	return (0);
881*7c478bd9Sstevel@tonic-gate }
882*7c478bd9Sstevel@tonic-gate 
883*7c478bd9Sstevel@tonic-gate 
884*7c478bd9Sstevel@tonic-gate /*
885*7c478bd9Sstevel@tonic-gate  * Check the validity of the time-sharing parameters in the pc_vaparms_t
886*7c478bd9Sstevel@tonic-gate  * structure vaparmsp and put them in the buffer pointed to by tsparmsp.
887*7c478bd9Sstevel@tonic-gate  * pc_vaparms_t contains (key, value) pairs of parameter.
888*7c478bd9Sstevel@tonic-gate  * ts_vaparmsin() is called for TS threads, and ia_vaparmsin() is called
889*7c478bd9Sstevel@tonic-gate  * for IA threads. ts_vaparmsin() is the variable parameter version of
890*7c478bd9Sstevel@tonic-gate  * ts_parmsin() and ia_vaparmsin() is the variable parameter version of
891*7c478bd9Sstevel@tonic-gate  * ia_parmsin().
892*7c478bd9Sstevel@tonic-gate  */
893*7c478bd9Sstevel@tonic-gate static int
894*7c478bd9Sstevel@tonic-gate ts_vaparmsin(void *parmsp, pc_vaparms_t *vaparmsp)
895*7c478bd9Sstevel@tonic-gate {
896*7c478bd9Sstevel@tonic-gate 	tsparms_t	*tsparmsp = (tsparms_t *)parmsp;
897*7c478bd9Sstevel@tonic-gate 	int		priflag = 0;
898*7c478bd9Sstevel@tonic-gate 	int		limflag = 0;
899*7c478bd9Sstevel@tonic-gate 	uint_t		cnt;
900*7c478bd9Sstevel@tonic-gate 	pc_vaparm_t	*vpp = &vaparmsp->pc_parms[0];
901*7c478bd9Sstevel@tonic-gate 
902*7c478bd9Sstevel@tonic-gate 
903*7c478bd9Sstevel@tonic-gate 	/*
904*7c478bd9Sstevel@tonic-gate 	 * TS_NOCHANGE (-32768) is outside of the range of values for
905*7c478bd9Sstevel@tonic-gate 	 * ts_uprilim and ts_upri. If the structure tsparms_t is changed,
906*7c478bd9Sstevel@tonic-gate 	 * TS_NOCHANGE should be replaced by a flag word (in the same manner
907*7c478bd9Sstevel@tonic-gate 	 * as in rt.c).
908*7c478bd9Sstevel@tonic-gate 	 */
909*7c478bd9Sstevel@tonic-gate 	tsparmsp->ts_uprilim = TS_NOCHANGE;
910*7c478bd9Sstevel@tonic-gate 	tsparmsp->ts_upri = TS_NOCHANGE;
911*7c478bd9Sstevel@tonic-gate 
912*7c478bd9Sstevel@tonic-gate 	/*
913*7c478bd9Sstevel@tonic-gate 	 * Get the varargs parameter and check validity of parameters.
914*7c478bd9Sstevel@tonic-gate 	 */
915*7c478bd9Sstevel@tonic-gate 	if (vaparmsp->pc_vaparmscnt > PC_VAPARMCNT)
916*7c478bd9Sstevel@tonic-gate 		return (EINVAL);
917*7c478bd9Sstevel@tonic-gate 
918*7c478bd9Sstevel@tonic-gate 	for (cnt = 0; cnt < vaparmsp->pc_vaparmscnt; cnt++, vpp++) {
919*7c478bd9Sstevel@tonic-gate 
920*7c478bd9Sstevel@tonic-gate 		switch (vpp->pc_key) {
921*7c478bd9Sstevel@tonic-gate 		case TS_KY_UPRILIM:
922*7c478bd9Sstevel@tonic-gate 			if (limflag++)
923*7c478bd9Sstevel@tonic-gate 				return (EINVAL);
924*7c478bd9Sstevel@tonic-gate 			tsparmsp->ts_uprilim = (pri_t)vpp->pc_parm;
925*7c478bd9Sstevel@tonic-gate 			if (tsparmsp->ts_uprilim > ts_maxupri ||
926*7c478bd9Sstevel@tonic-gate 			    tsparmsp->ts_uprilim < -ts_maxupri)
927*7c478bd9Sstevel@tonic-gate 				return (EINVAL);
928*7c478bd9Sstevel@tonic-gate 			break;
929*7c478bd9Sstevel@tonic-gate 
930*7c478bd9Sstevel@tonic-gate 		case TS_KY_UPRI:
931*7c478bd9Sstevel@tonic-gate 			if (priflag++)
932*7c478bd9Sstevel@tonic-gate 				return (EINVAL);
933*7c478bd9Sstevel@tonic-gate 			tsparmsp->ts_upri = (pri_t)vpp->pc_parm;
934*7c478bd9Sstevel@tonic-gate 			if (tsparmsp->ts_upri > ts_maxupri ||
935*7c478bd9Sstevel@tonic-gate 			    tsparmsp->ts_upri < -ts_maxupri)
936*7c478bd9Sstevel@tonic-gate 				return (EINVAL);
937*7c478bd9Sstevel@tonic-gate 			break;
938*7c478bd9Sstevel@tonic-gate 
939*7c478bd9Sstevel@tonic-gate 		default:
940*7c478bd9Sstevel@tonic-gate 			return (EINVAL);
941*7c478bd9Sstevel@tonic-gate 		}
942*7c478bd9Sstevel@tonic-gate 	}
943*7c478bd9Sstevel@tonic-gate 
944*7c478bd9Sstevel@tonic-gate 	if (vaparmsp->pc_vaparmscnt == 0) {
945*7c478bd9Sstevel@tonic-gate 		/*
946*7c478bd9Sstevel@tonic-gate 		 * Use default parameters.
947*7c478bd9Sstevel@tonic-gate 		 */
948*7c478bd9Sstevel@tonic-gate 		tsparmsp->ts_upri = tsparmsp->ts_uprilim = 0;
949*7c478bd9Sstevel@tonic-gate 	}
950*7c478bd9Sstevel@tonic-gate 
951*7c478bd9Sstevel@tonic-gate 	return (0);
952*7c478bd9Sstevel@tonic-gate }
953*7c478bd9Sstevel@tonic-gate 
954*7c478bd9Sstevel@tonic-gate static int
955*7c478bd9Sstevel@tonic-gate ia_vaparmsin(void *parmsp, pc_vaparms_t *vaparmsp)
956*7c478bd9Sstevel@tonic-gate {
957*7c478bd9Sstevel@tonic-gate 	iaparms_t	*iaparmsp = (iaparms_t *)parmsp;
958*7c478bd9Sstevel@tonic-gate 	int		priflag = 0;
959*7c478bd9Sstevel@tonic-gate 	int		limflag = 0;
960*7c478bd9Sstevel@tonic-gate 	int		mflag = 0;
961*7c478bd9Sstevel@tonic-gate 	uint_t		cnt;
962*7c478bd9Sstevel@tonic-gate 	pc_vaparm_t	*vpp = &vaparmsp->pc_parms[0];
963*7c478bd9Sstevel@tonic-gate 
964*7c478bd9Sstevel@tonic-gate 	/*
965*7c478bd9Sstevel@tonic-gate 	 * IA_NOCHANGE (-32768) is outside of the range of values for
966*7c478bd9Sstevel@tonic-gate 	 * ia_uprilim, ia_upri and ia_mode. If the structure iaparms_t is
967*7c478bd9Sstevel@tonic-gate 	 * changed, IA_NOCHANGE should be replaced by a flag word (in the
968*7c478bd9Sstevel@tonic-gate 	 * same manner as in rt.c).
969*7c478bd9Sstevel@tonic-gate 	 */
970*7c478bd9Sstevel@tonic-gate 	iaparmsp->ia_uprilim = IA_NOCHANGE;
971*7c478bd9Sstevel@tonic-gate 	iaparmsp->ia_upri = IA_NOCHANGE;
972*7c478bd9Sstevel@tonic-gate 	iaparmsp->ia_mode = IA_NOCHANGE;
973*7c478bd9Sstevel@tonic-gate 
974*7c478bd9Sstevel@tonic-gate 	/*
975*7c478bd9Sstevel@tonic-gate 	 * Get the varargs parameter and check validity of parameters.
976*7c478bd9Sstevel@tonic-gate 	 */
977*7c478bd9Sstevel@tonic-gate 	if (vaparmsp->pc_vaparmscnt > PC_VAPARMCNT)
978*7c478bd9Sstevel@tonic-gate 		return (EINVAL);
979*7c478bd9Sstevel@tonic-gate 
980*7c478bd9Sstevel@tonic-gate 	for (cnt = 0; cnt < vaparmsp->pc_vaparmscnt; cnt++, vpp++) {
981*7c478bd9Sstevel@tonic-gate 
982*7c478bd9Sstevel@tonic-gate 		switch (vpp->pc_key) {
983*7c478bd9Sstevel@tonic-gate 		case IA_KY_UPRILIM:
984*7c478bd9Sstevel@tonic-gate 			if (limflag++)
985*7c478bd9Sstevel@tonic-gate 				return (EINVAL);
986*7c478bd9Sstevel@tonic-gate 			iaparmsp->ia_uprilim = (pri_t)vpp->pc_parm;
987*7c478bd9Sstevel@tonic-gate 			if (iaparmsp->ia_uprilim > ia_maxupri ||
988*7c478bd9Sstevel@tonic-gate 			    iaparmsp->ia_uprilim < -ia_maxupri)
989*7c478bd9Sstevel@tonic-gate 				return (EINVAL);
990*7c478bd9Sstevel@tonic-gate 			break;
991*7c478bd9Sstevel@tonic-gate 
992*7c478bd9Sstevel@tonic-gate 		case IA_KY_UPRI:
993*7c478bd9Sstevel@tonic-gate 			if (priflag++)
994*7c478bd9Sstevel@tonic-gate 				return (EINVAL);
995*7c478bd9Sstevel@tonic-gate 			iaparmsp->ia_upri = (pri_t)vpp->pc_parm;
996*7c478bd9Sstevel@tonic-gate 			if (iaparmsp->ia_upri > ia_maxupri ||
997*7c478bd9Sstevel@tonic-gate 			    iaparmsp->ia_upri < -ia_maxupri)
998*7c478bd9Sstevel@tonic-gate 				return (EINVAL);
999*7c478bd9Sstevel@tonic-gate 			break;
1000*7c478bd9Sstevel@tonic-gate 
1001*7c478bd9Sstevel@tonic-gate 		case IA_KY_MODE:
1002*7c478bd9Sstevel@tonic-gate 			if (mflag++)
1003*7c478bd9Sstevel@tonic-gate 				return (EINVAL);
1004*7c478bd9Sstevel@tonic-gate 			iaparmsp->ia_mode = (int)vpp->pc_parm;
1005*7c478bd9Sstevel@tonic-gate 			if (iaparmsp->ia_mode != IA_SET_INTERACTIVE &&
1006*7c478bd9Sstevel@tonic-gate 			    iaparmsp->ia_mode != IA_INTERACTIVE_OFF)
1007*7c478bd9Sstevel@tonic-gate 				return (EINVAL);
1008*7c478bd9Sstevel@tonic-gate 			break;
1009*7c478bd9Sstevel@tonic-gate 
1010*7c478bd9Sstevel@tonic-gate 		default:
1011*7c478bd9Sstevel@tonic-gate 			return (EINVAL);
1012*7c478bd9Sstevel@tonic-gate 		}
1013*7c478bd9Sstevel@tonic-gate 	}
1014*7c478bd9Sstevel@tonic-gate 
1015*7c478bd9Sstevel@tonic-gate 	if (vaparmsp->pc_vaparmscnt == 0) {
1016*7c478bd9Sstevel@tonic-gate 		/*
1017*7c478bd9Sstevel@tonic-gate 		 * Use default parameters.
1018*7c478bd9Sstevel@tonic-gate 		 */
1019*7c478bd9Sstevel@tonic-gate 		iaparmsp->ia_upri = iaparmsp->ia_uprilim = 0;
1020*7c478bd9Sstevel@tonic-gate 		iaparmsp->ia_mode = IA_SET_INTERACTIVE;
1021*7c478bd9Sstevel@tonic-gate 	}
1022*7c478bd9Sstevel@tonic-gate 
1023*7c478bd9Sstevel@tonic-gate 	return (0);
1024*7c478bd9Sstevel@tonic-gate }
1025*7c478bd9Sstevel@tonic-gate 
1026*7c478bd9Sstevel@tonic-gate /*
1027*7c478bd9Sstevel@tonic-gate  * Nothing to do here but return success.
1028*7c478bd9Sstevel@tonic-gate  */
1029*7c478bd9Sstevel@tonic-gate /* ARGSUSED */
1030*7c478bd9Sstevel@tonic-gate static int
1031*7c478bd9Sstevel@tonic-gate ts_parmsout(void *parmsp, pc_vaparms_t *vaparmsp)
1032*7c478bd9Sstevel@tonic-gate {
1033*7c478bd9Sstevel@tonic-gate 	return (0);
1034*7c478bd9Sstevel@tonic-gate }
1035*7c478bd9Sstevel@tonic-gate 
1036*7c478bd9Sstevel@tonic-gate 
1037*7c478bd9Sstevel@tonic-gate /*
1038*7c478bd9Sstevel@tonic-gate  * Copy all selected time-sharing class parameters to the user.
1039*7c478bd9Sstevel@tonic-gate  * The parameters are specified by a key.
1040*7c478bd9Sstevel@tonic-gate  */
1041*7c478bd9Sstevel@tonic-gate static int
1042*7c478bd9Sstevel@tonic-gate ts_vaparmsout(void *prmsp, pc_vaparms_t *vaparmsp)
1043*7c478bd9Sstevel@tonic-gate {
1044*7c478bd9Sstevel@tonic-gate 	tsparms_t	*tsprmsp = (tsparms_t *)prmsp;
1045*7c478bd9Sstevel@tonic-gate 	int		priflag = 0;
1046*7c478bd9Sstevel@tonic-gate 	int		limflag = 0;
1047*7c478bd9Sstevel@tonic-gate 	uint_t		cnt;
1048*7c478bd9Sstevel@tonic-gate 	pc_vaparm_t	*vpp = &vaparmsp->pc_parms[0];
1049*7c478bd9Sstevel@tonic-gate 
1050*7c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_NOT_HELD(&curproc->p_lock));
1051*7c478bd9Sstevel@tonic-gate 
1052*7c478bd9Sstevel@tonic-gate 	if (vaparmsp->pc_vaparmscnt > PC_VAPARMCNT)
1053*7c478bd9Sstevel@tonic-gate 		return (EINVAL);
1054*7c478bd9Sstevel@tonic-gate 
1055*7c478bd9Sstevel@tonic-gate 	for (cnt = 0; cnt < vaparmsp->pc_vaparmscnt; cnt++, vpp++) {
1056*7c478bd9Sstevel@tonic-gate 
1057*7c478bd9Sstevel@tonic-gate 		switch (vpp->pc_key) {
1058*7c478bd9Sstevel@tonic-gate 		case TS_KY_UPRILIM:
1059*7c478bd9Sstevel@tonic-gate 			if (limflag++)
1060*7c478bd9Sstevel@tonic-gate 				return (EINVAL);
1061*7c478bd9Sstevel@tonic-gate 			if (copyout(&tsprmsp->ts_uprilim,
1062*7c478bd9Sstevel@tonic-gate 			    (caddr_t)(uintptr_t)vpp->pc_parm, sizeof (pri_t)))
1063*7c478bd9Sstevel@tonic-gate 				return (EFAULT);
1064*7c478bd9Sstevel@tonic-gate 			break;
1065*7c478bd9Sstevel@tonic-gate 
1066*7c478bd9Sstevel@tonic-gate 		case TS_KY_UPRI:
1067*7c478bd9Sstevel@tonic-gate 			if (priflag++)
1068*7c478bd9Sstevel@tonic-gate 				return (EINVAL);
1069*7c478bd9Sstevel@tonic-gate 			if (copyout(&tsprmsp->ts_upri,
1070*7c478bd9Sstevel@tonic-gate 			    (caddr_t)(uintptr_t)vpp->pc_parm, sizeof (pri_t)))
1071*7c478bd9Sstevel@tonic-gate 				return (EFAULT);
1072*7c478bd9Sstevel@tonic-gate 			break;
1073*7c478bd9Sstevel@tonic-gate 
1074*7c478bd9Sstevel@tonic-gate 		default:
1075*7c478bd9Sstevel@tonic-gate 			return (EINVAL);
1076*7c478bd9Sstevel@tonic-gate 		}
1077*7c478bd9Sstevel@tonic-gate 	}
1078*7c478bd9Sstevel@tonic-gate 
1079*7c478bd9Sstevel@tonic-gate 	return (0);
1080*7c478bd9Sstevel@tonic-gate }
1081*7c478bd9Sstevel@tonic-gate 
1082*7c478bd9Sstevel@tonic-gate 
1083*7c478bd9Sstevel@tonic-gate /*
1084*7c478bd9Sstevel@tonic-gate  * Copy all selected interactive class parameters to the user.
1085*7c478bd9Sstevel@tonic-gate  * The parameters are specified by a key.
1086*7c478bd9Sstevel@tonic-gate  */
1087*7c478bd9Sstevel@tonic-gate static int
1088*7c478bd9Sstevel@tonic-gate ia_vaparmsout(void *prmsp, pc_vaparms_t *vaparmsp)
1089*7c478bd9Sstevel@tonic-gate {
1090*7c478bd9Sstevel@tonic-gate 	iaparms_t	*iaprmsp = (iaparms_t *)prmsp;
1091*7c478bd9Sstevel@tonic-gate 	int		priflag = 0;
1092*7c478bd9Sstevel@tonic-gate 	int		limflag = 0;
1093*7c478bd9Sstevel@tonic-gate 	int		mflag = 0;
1094*7c478bd9Sstevel@tonic-gate 	uint_t		cnt;
1095*7c478bd9Sstevel@tonic-gate 	pc_vaparm_t	*vpp = &vaparmsp->pc_parms[0];
1096*7c478bd9Sstevel@tonic-gate 
1097*7c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_NOT_HELD(&curproc->p_lock));
1098*7c478bd9Sstevel@tonic-gate 
1099*7c478bd9Sstevel@tonic-gate 	if (vaparmsp->pc_vaparmscnt > PC_VAPARMCNT)
1100*7c478bd9Sstevel@tonic-gate 		return (EINVAL);
1101*7c478bd9Sstevel@tonic-gate 
1102*7c478bd9Sstevel@tonic-gate 	for (cnt = 0; cnt < vaparmsp->pc_vaparmscnt; cnt++, vpp++) {
1103*7c478bd9Sstevel@tonic-gate 
1104*7c478bd9Sstevel@tonic-gate 		switch (vpp->pc_key) {
1105*7c478bd9Sstevel@tonic-gate 		case IA_KY_UPRILIM:
1106*7c478bd9Sstevel@tonic-gate 			if (limflag++)
1107*7c478bd9Sstevel@tonic-gate 				return (EINVAL);
1108*7c478bd9Sstevel@tonic-gate 			if (copyout(&iaprmsp->ia_uprilim,
1109*7c478bd9Sstevel@tonic-gate 			    (caddr_t)(uintptr_t)vpp->pc_parm, sizeof (pri_t)))
1110*7c478bd9Sstevel@tonic-gate 				return (EFAULT);
1111*7c478bd9Sstevel@tonic-gate 			break;
1112*7c478bd9Sstevel@tonic-gate 
1113*7c478bd9Sstevel@tonic-gate 		case IA_KY_UPRI:
1114*7c478bd9Sstevel@tonic-gate 			if (priflag++)
1115*7c478bd9Sstevel@tonic-gate 				return (EINVAL);
1116*7c478bd9Sstevel@tonic-gate 			if (copyout(&iaprmsp->ia_upri,
1117*7c478bd9Sstevel@tonic-gate 			    (caddr_t)(uintptr_t)vpp->pc_parm, sizeof (pri_t)))
1118*7c478bd9Sstevel@tonic-gate 				return (EFAULT);
1119*7c478bd9Sstevel@tonic-gate 			break;
1120*7c478bd9Sstevel@tonic-gate 
1121*7c478bd9Sstevel@tonic-gate 		case IA_KY_MODE:
1122*7c478bd9Sstevel@tonic-gate 			if (mflag++)
1123*7c478bd9Sstevel@tonic-gate 				return (EINVAL);
1124*7c478bd9Sstevel@tonic-gate 			if (copyout(&iaprmsp->ia_mode,
1125*7c478bd9Sstevel@tonic-gate 			    (caddr_t)(uintptr_t)vpp->pc_parm, sizeof (int)))
1126*7c478bd9Sstevel@tonic-gate 				return (EFAULT);
1127*7c478bd9Sstevel@tonic-gate 			break;
1128*7c478bd9Sstevel@tonic-gate 
1129*7c478bd9Sstevel@tonic-gate 		default:
1130*7c478bd9Sstevel@tonic-gate 			return (EINVAL);
1131*7c478bd9Sstevel@tonic-gate 		}
1132*7c478bd9Sstevel@tonic-gate 	}
1133*7c478bd9Sstevel@tonic-gate 	return (0);
1134*7c478bd9Sstevel@tonic-gate }
1135*7c478bd9Sstevel@tonic-gate 
1136*7c478bd9Sstevel@tonic-gate 
1137*7c478bd9Sstevel@tonic-gate /*
1138*7c478bd9Sstevel@tonic-gate  * Set the scheduling parameters of the thread pointed to by tsprocp
1139*7c478bd9Sstevel@tonic-gate  * to those specified in the buffer pointed to by tsparmsp.
1140*7c478bd9Sstevel@tonic-gate  * ts_parmsset() is called for TS threads, and ia_parmsset() is
1141*7c478bd9Sstevel@tonic-gate  * called for IA threads.
1142*7c478bd9Sstevel@tonic-gate  */
1143*7c478bd9Sstevel@tonic-gate /* ARGSUSED */
1144*7c478bd9Sstevel@tonic-gate static int
1145*7c478bd9Sstevel@tonic-gate ts_parmsset(kthread_t *tx, void *parmsp, id_t reqpcid, cred_t *reqpcredp)
1146*7c478bd9Sstevel@tonic-gate {
1147*7c478bd9Sstevel@tonic-gate 	char		nice;
1148*7c478bd9Sstevel@tonic-gate 	pri_t		reqtsuprilim;
1149*7c478bd9Sstevel@tonic-gate 	pri_t		reqtsupri;
1150*7c478bd9Sstevel@tonic-gate 	tsparms_t	*tsparmsp = (tsparms_t *)parmsp;
1151*7c478bd9Sstevel@tonic-gate 	tsproc_t	*tspp = (tsproc_t *)tx->t_cldata;
1152*7c478bd9Sstevel@tonic-gate 
1153*7c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&(ttoproc(tx))->p_lock));
1154*7c478bd9Sstevel@tonic-gate 
1155*7c478bd9Sstevel@tonic-gate 	if (tsparmsp->ts_uprilim == TS_NOCHANGE)
1156*7c478bd9Sstevel@tonic-gate 		reqtsuprilim = tspp->ts_uprilim;
1157*7c478bd9Sstevel@tonic-gate 	else
1158*7c478bd9Sstevel@tonic-gate 		reqtsuprilim = tsparmsp->ts_uprilim;
1159*7c478bd9Sstevel@tonic-gate 
1160*7c478bd9Sstevel@tonic-gate 	if (tsparmsp->ts_upri == TS_NOCHANGE)
1161*7c478bd9Sstevel@tonic-gate 		reqtsupri = tspp->ts_upri;
1162*7c478bd9Sstevel@tonic-gate 	else
1163*7c478bd9Sstevel@tonic-gate 		reqtsupri = tsparmsp->ts_upri;
1164*7c478bd9Sstevel@tonic-gate 
1165*7c478bd9Sstevel@tonic-gate 	/*
1166*7c478bd9Sstevel@tonic-gate 	 * Make sure the user priority doesn't exceed the upri limit.
1167*7c478bd9Sstevel@tonic-gate 	 */
1168*7c478bd9Sstevel@tonic-gate 	if (reqtsupri > reqtsuprilim)
1169*7c478bd9Sstevel@tonic-gate 		reqtsupri = reqtsuprilim;
1170*7c478bd9Sstevel@tonic-gate 
1171*7c478bd9Sstevel@tonic-gate 	/*
1172*7c478bd9Sstevel@tonic-gate 	 * Basic permissions enforced by generic kernel code
1173*7c478bd9Sstevel@tonic-gate 	 * for all classes require that a thread attempting
1174*7c478bd9Sstevel@tonic-gate 	 * to change the scheduling parameters of a target
1175*7c478bd9Sstevel@tonic-gate 	 * thread be privileged or have a real or effective
1176*7c478bd9Sstevel@tonic-gate 	 * UID matching that of the target thread. We are not
1177*7c478bd9Sstevel@tonic-gate 	 * called unless these basic permission checks have
1178*7c478bd9Sstevel@tonic-gate 	 * already passed. The time-sharing class requires in
1179*7c478bd9Sstevel@tonic-gate 	 * addition that the calling thread be privileged if it
1180*7c478bd9Sstevel@tonic-gate 	 * is attempting to raise the upri limit above its current
1181*7c478bd9Sstevel@tonic-gate 	 * value This may have been checked previously but if our
1182*7c478bd9Sstevel@tonic-gate 	 * caller passed us a non-NULL credential pointer we assume
1183*7c478bd9Sstevel@tonic-gate 	 * it hasn't and we check it here.
1184*7c478bd9Sstevel@tonic-gate 	 */
1185*7c478bd9Sstevel@tonic-gate 	if (reqpcredp != NULL &&
1186*7c478bd9Sstevel@tonic-gate 	    reqtsuprilim > tspp->ts_uprilim &&
1187*7c478bd9Sstevel@tonic-gate 	    secpolicy_setpriority(reqpcredp) != 0)
1188*7c478bd9Sstevel@tonic-gate 		return (EPERM);
1189*7c478bd9Sstevel@tonic-gate 
1190*7c478bd9Sstevel@tonic-gate 	/*
1191*7c478bd9Sstevel@tonic-gate 	 * Set ts_nice to the nice value corresponding to the user
1192*7c478bd9Sstevel@tonic-gate 	 * priority we are setting.  Note that setting the nice field
1193*7c478bd9Sstevel@tonic-gate 	 * of the parameter struct won't affect upri or nice.
1194*7c478bd9Sstevel@tonic-gate 	 */
1195*7c478bd9Sstevel@tonic-gate 	nice = NZERO - (reqtsupri * NZERO) / ts_maxupri;
1196*7c478bd9Sstevel@tonic-gate 	if (nice >= 2 * NZERO)
1197*7c478bd9Sstevel@tonic-gate 		nice = 2 * NZERO - 1;
1198*7c478bd9Sstevel@tonic-gate 
1199*7c478bd9Sstevel@tonic-gate 	thread_lock(tx);
1200*7c478bd9Sstevel@tonic-gate 
1201*7c478bd9Sstevel@tonic-gate 	tspp->ts_uprilim = reqtsuprilim;
1202*7c478bd9Sstevel@tonic-gate 	tspp->ts_upri = reqtsupri;
1203*7c478bd9Sstevel@tonic-gate 	TS_NEWUMDPRI(tspp);
1204*7c478bd9Sstevel@tonic-gate 	tspp->ts_nice = nice;
1205*7c478bd9Sstevel@tonic-gate 
1206*7c478bd9Sstevel@tonic-gate 	if ((tspp->ts_flags & TSKPRI) != 0) {
1207*7c478bd9Sstevel@tonic-gate 		thread_unlock(tx);
1208*7c478bd9Sstevel@tonic-gate 		return (0);
1209*7c478bd9Sstevel@tonic-gate 	}
1210*7c478bd9Sstevel@tonic-gate 
1211*7c478bd9Sstevel@tonic-gate 	tspp->ts_dispwait = 0;
1212*7c478bd9Sstevel@tonic-gate 	ts_change_priority(tx, tspp);
1213*7c478bd9Sstevel@tonic-gate 	thread_unlock(tx);
1214*7c478bd9Sstevel@tonic-gate 	return (0);
1215*7c478bd9Sstevel@tonic-gate }
1216*7c478bd9Sstevel@tonic-gate 
1217*7c478bd9Sstevel@tonic-gate 
1218*7c478bd9Sstevel@tonic-gate static int
1219*7c478bd9Sstevel@tonic-gate ia_parmsset(kthread_t *tx, void *parmsp, id_t reqpcid, cred_t *reqpcredp)
1220*7c478bd9Sstevel@tonic-gate {
1221*7c478bd9Sstevel@tonic-gate 	tsproc_t	*tspp = (tsproc_t *)tx->t_cldata;
1222*7c478bd9Sstevel@tonic-gate 	iaparms_t	*iaparmsp = (iaparms_t *)parmsp;
1223*7c478bd9Sstevel@tonic-gate 	proc_t		*p;
1224*7c478bd9Sstevel@tonic-gate 	pid_t		pid, pgid, sid;
1225*7c478bd9Sstevel@tonic-gate 	pid_t		on, off;
1226*7c478bd9Sstevel@tonic-gate 	struct stdata 	*stp;
1227*7c478bd9Sstevel@tonic-gate 	int		sess_held;
1228*7c478bd9Sstevel@tonic-gate 
1229*7c478bd9Sstevel@tonic-gate 	/*
1230*7c478bd9Sstevel@tonic-gate 	 * Handle user priority changes
1231*7c478bd9Sstevel@tonic-gate 	 */
1232*7c478bd9Sstevel@tonic-gate 	if (iaparmsp->ia_mode == IA_NOCHANGE)
1233*7c478bd9Sstevel@tonic-gate 		return (ts_parmsset(tx, parmsp, reqpcid, reqpcredp));
1234*7c478bd9Sstevel@tonic-gate 
1235*7c478bd9Sstevel@tonic-gate 	/*
1236*7c478bd9Sstevel@tonic-gate 	 * Check permissions for changing modes.
1237*7c478bd9Sstevel@tonic-gate 	 */
1238*7c478bd9Sstevel@tonic-gate 
1239*7c478bd9Sstevel@tonic-gate 	if (reqpcredp != NULL && !groupmember(IA_gid, reqpcredp) &&
1240*7c478bd9Sstevel@tonic-gate 	    secpolicy_setpriority(reqpcredp) != 0) {
1241*7c478bd9Sstevel@tonic-gate 		/*
1242*7c478bd9Sstevel@tonic-gate 		 * Silently fail in case this is just a priocntl
1243*7c478bd9Sstevel@tonic-gate 		 * call with upri and uprilim set to IA_NOCHANGE.
1244*7c478bd9Sstevel@tonic-gate 		 */
1245*7c478bd9Sstevel@tonic-gate 		return (0);
1246*7c478bd9Sstevel@tonic-gate 	}
1247*7c478bd9Sstevel@tonic-gate 
1248*7c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&pidlock));
1249*7c478bd9Sstevel@tonic-gate 	if ((p = ttoproc(tx)) == NULL) {
1250*7c478bd9Sstevel@tonic-gate 		return (0);
1251*7c478bd9Sstevel@tonic-gate 	}
1252*7c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&p->p_lock));
1253*7c478bd9Sstevel@tonic-gate 	if (p->p_stat == SIDL) {
1254*7c478bd9Sstevel@tonic-gate 		return (0);
1255*7c478bd9Sstevel@tonic-gate 	}
1256*7c478bd9Sstevel@tonic-gate 	pid = p->p_pid;
1257*7c478bd9Sstevel@tonic-gate 	sid = p->p_sessp->s_sid;
1258*7c478bd9Sstevel@tonic-gate 	pgid = p->p_pgrp;
1259*7c478bd9Sstevel@tonic-gate 	if (iaparmsp->ia_mode == IA_SET_INTERACTIVE) {
1260*7c478bd9Sstevel@tonic-gate 		/*
1261*7c478bd9Sstevel@tonic-gate 		 * session leaders must be turned on now so all processes
1262*7c478bd9Sstevel@tonic-gate 		 * in the group controlling the tty will be turned on or off.
1263*7c478bd9Sstevel@tonic-gate 		 * if the ia_mode is off for the session leader,
1264*7c478bd9Sstevel@tonic-gate 		 * ia_set_process_group will return without setting the
1265*7c478bd9Sstevel@tonic-gate 		 * processes in the group controlling the tty on.
1266*7c478bd9Sstevel@tonic-gate 		 */
1267*7c478bd9Sstevel@tonic-gate 		thread_lock(tx);
1268*7c478bd9Sstevel@tonic-gate 		tspp->ts_flags |= TSIASET;
1269*7c478bd9Sstevel@tonic-gate 		thread_unlock(tx);
1270*7c478bd9Sstevel@tonic-gate 	}
1271*7c478bd9Sstevel@tonic-gate 	TTY_HOLD(p->p_sessp);
1272*7c478bd9Sstevel@tonic-gate 	sess_held = 1;
1273*7c478bd9Sstevel@tonic-gate 	if ((pid == sid) && (p->p_sessp->s_vp != NULL) &&
1274*7c478bd9Sstevel@tonic-gate 	    ((stp = p->p_sessp->s_vp->v_stream) != NULL)) {
1275*7c478bd9Sstevel@tonic-gate 		if ((stp->sd_pgidp != NULL) && (stp->sd_sidp != NULL)) {
1276*7c478bd9Sstevel@tonic-gate 			pgid = stp->sd_pgidp->pid_id;
1277*7c478bd9Sstevel@tonic-gate 			sess_held = 0;
1278*7c478bd9Sstevel@tonic-gate 			TTY_RELE(p->p_sessp);
1279*7c478bd9Sstevel@tonic-gate 			if (iaparmsp->ia_mode ==
1280*7c478bd9Sstevel@tonic-gate 			    IA_SET_INTERACTIVE) {
1281*7c478bd9Sstevel@tonic-gate 				off = 0;
1282*7c478bd9Sstevel@tonic-gate 				on = pgid;
1283*7c478bd9Sstevel@tonic-gate 			} else {
1284*7c478bd9Sstevel@tonic-gate 				off = pgid;
1285*7c478bd9Sstevel@tonic-gate 				on = 0;
1286*7c478bd9Sstevel@tonic-gate 			}
1287*7c478bd9Sstevel@tonic-gate 			TRACE_3(TR_FAC_IA, TR_ACTIVE_CHAIN,
1288*7c478bd9Sstevel@tonic-gate 			    "active chain:pid %d gid %d %p",
1289*7c478bd9Sstevel@tonic-gate 			    pid, pgid, p);
1290*7c478bd9Sstevel@tonic-gate 			ia_set_process_group(sid, off, on);
1291*7c478bd9Sstevel@tonic-gate 		}
1292*7c478bd9Sstevel@tonic-gate 	}
1293*7c478bd9Sstevel@tonic-gate 	if (sess_held)
1294*7c478bd9Sstevel@tonic-gate 		TTY_RELE(p->p_sessp);
1295*7c478bd9Sstevel@tonic-gate 
1296*7c478bd9Sstevel@tonic-gate 	thread_lock(tx);
1297*7c478bd9Sstevel@tonic-gate 
1298*7c478bd9Sstevel@tonic-gate 	if (iaparmsp->ia_mode == IA_SET_INTERACTIVE) {
1299*7c478bd9Sstevel@tonic-gate 		tspp->ts_flags |= TSIASET;
1300*7c478bd9Sstevel@tonic-gate 		tspp->ts_boost = ia_boost;
1301*7c478bd9Sstevel@tonic-gate 	} else {
1302*7c478bd9Sstevel@tonic-gate 		tspp->ts_flags &= ~TSIASET;
1303*7c478bd9Sstevel@tonic-gate 		tspp->ts_boost = -ia_boost;
1304*7c478bd9Sstevel@tonic-gate 	}
1305*7c478bd9Sstevel@tonic-gate 	thread_unlock(tx);
1306*7c478bd9Sstevel@tonic-gate 
1307*7c478bd9Sstevel@tonic-gate 	return (ts_parmsset(tx, parmsp, reqpcid, reqpcredp));
1308*7c478bd9Sstevel@tonic-gate }
1309*7c478bd9Sstevel@tonic-gate 
1310*7c478bd9Sstevel@tonic-gate /*
1311*7c478bd9Sstevel@tonic-gate  * Return the global scheduling priority that would be assigned
1312*7c478bd9Sstevel@tonic-gate  * to a thread entering the time-sharing class with the ts_upri.
1313*7c478bd9Sstevel@tonic-gate  */
1314*7c478bd9Sstevel@tonic-gate static pri_t
1315*7c478bd9Sstevel@tonic-gate ts_globpri(kthread_t *t)
1316*7c478bd9Sstevel@tonic-gate {
1317*7c478bd9Sstevel@tonic-gate 	tsproc_t *tspp;
1318*7c478bd9Sstevel@tonic-gate 	pri_t	tspri;
1319*7c478bd9Sstevel@tonic-gate 
1320*7c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&ttoproc(t)->p_lock));
1321*7c478bd9Sstevel@tonic-gate 	tspp = (tsproc_t *)t->t_cldata;
1322*7c478bd9Sstevel@tonic-gate 	tspri = tsmedumdpri + tspp->ts_upri;
1323*7c478bd9Sstevel@tonic-gate 	if (tspri > ts_maxumdpri)
1324*7c478bd9Sstevel@tonic-gate 		tspri = ts_maxumdpri;
1325*7c478bd9Sstevel@tonic-gate 	else if (tspri < 0)
1326*7c478bd9Sstevel@tonic-gate 		tspri = 0;
1327*7c478bd9Sstevel@tonic-gate 	return (ts_dptbl[tspri].ts_globpri);
1328*7c478bd9Sstevel@tonic-gate }
1329*7c478bd9Sstevel@tonic-gate 
1330*7c478bd9Sstevel@tonic-gate /*
1331*7c478bd9Sstevel@tonic-gate  * Arrange for thread to be placed in appropriate location
1332*7c478bd9Sstevel@tonic-gate  * on dispatcher queue.
1333*7c478bd9Sstevel@tonic-gate  *
1334*7c478bd9Sstevel@tonic-gate  * This is called with the current thread in TS_ONPROC and locked.
1335*7c478bd9Sstevel@tonic-gate  */
1336*7c478bd9Sstevel@tonic-gate static void
1337*7c478bd9Sstevel@tonic-gate ts_preempt(kthread_t *t)
1338*7c478bd9Sstevel@tonic-gate {
1339*7c478bd9Sstevel@tonic-gate 	tsproc_t	*tspp = (tsproc_t *)(t->t_cldata);
1340*7c478bd9Sstevel@tonic-gate 	klwp_t		*lwp;
1341*7c478bd9Sstevel@tonic-gate #ifdef KSLICE
1342*7c478bd9Sstevel@tonic-gate 	extern int	kslice;
1343*7c478bd9Sstevel@tonic-gate #endif
1344*7c478bd9Sstevel@tonic-gate 	pri_t		oldpri = t->t_pri;
1345*7c478bd9Sstevel@tonic-gate 
1346*7c478bd9Sstevel@tonic-gate 	ASSERT(t == curthread);
1347*7c478bd9Sstevel@tonic-gate 	ASSERT(THREAD_LOCK_HELD(curthread));
1348*7c478bd9Sstevel@tonic-gate 
1349*7c478bd9Sstevel@tonic-gate 	/*
1350*7c478bd9Sstevel@tonic-gate 	 * If preempted in the kernel, make sure the thread has
1351*7c478bd9Sstevel@tonic-gate 	 * a kernel priority if needed.
1352*7c478bd9Sstevel@tonic-gate 	 */
1353*7c478bd9Sstevel@tonic-gate 	lwp = curthread->t_lwp;
1354*7c478bd9Sstevel@tonic-gate 	if (!(tspp->ts_flags & TSKPRI) && lwp != NULL && t->t_kpri_req) {
1355*7c478bd9Sstevel@tonic-gate 		tspp->ts_flags |= TSKPRI;
1356*7c478bd9Sstevel@tonic-gate 		THREAD_CHANGE_PRI(t, ts_kmdpris[0]);
1357*7c478bd9Sstevel@tonic-gate 		ASSERT(t->t_pri >= 0 && t->t_pri <= ts_maxglobpri);
1358*7c478bd9Sstevel@tonic-gate 		t->t_trapret = 1;		/* so ts_trapret will run */
1359*7c478bd9Sstevel@tonic-gate 		aston(t);
1360*7c478bd9Sstevel@tonic-gate 	}
1361*7c478bd9Sstevel@tonic-gate 	/*
1362*7c478bd9Sstevel@tonic-gate 	 * If preempted in user-land mark the thread
1363*7c478bd9Sstevel@tonic-gate 	 * as swappable because I know it isn't holding any locks.
1364*7c478bd9Sstevel@tonic-gate 	 */
1365*7c478bd9Sstevel@tonic-gate 	ASSERT(t->t_schedflag & TS_DONT_SWAP);
1366*7c478bd9Sstevel@tonic-gate 	if (lwp != NULL && lwp->lwp_state == LWP_USER)
1367*7c478bd9Sstevel@tonic-gate 		t->t_schedflag &= ~TS_DONT_SWAP;
1368*7c478bd9Sstevel@tonic-gate 
1369*7c478bd9Sstevel@tonic-gate 	/*
1370*7c478bd9Sstevel@tonic-gate 	 * Check to see if we're doing "preemption control" here.  If
1371*7c478bd9Sstevel@tonic-gate 	 * we are, and if the user has requested that this thread not
1372*7c478bd9Sstevel@tonic-gate 	 * be preempted, and if preemptions haven't been put off for
1373*7c478bd9Sstevel@tonic-gate 	 * too long, let the preemption happen here but try to make
1374*7c478bd9Sstevel@tonic-gate 	 * sure the thread is rescheduled as soon as possible.  We do
1375*7c478bd9Sstevel@tonic-gate 	 * this by putting it on the front of the highest priority run
1376*7c478bd9Sstevel@tonic-gate 	 * queue in the TS class.  If the preemption has been put off
1377*7c478bd9Sstevel@tonic-gate 	 * for too long, clear the "nopreempt" bit and let the thread
1378*7c478bd9Sstevel@tonic-gate 	 * be preempted.
1379*7c478bd9Sstevel@tonic-gate 	 */
1380*7c478bd9Sstevel@tonic-gate 	if (t->t_schedctl && schedctl_get_nopreempt(t)) {
1381*7c478bd9Sstevel@tonic-gate 		if (tspp->ts_timeleft > -SC_MAX_TICKS) {
1382*7c478bd9Sstevel@tonic-gate 			DTRACE_SCHED1(schedctl__nopreempt, kthread_t *, t);
1383*7c478bd9Sstevel@tonic-gate 			if (!(tspp->ts_flags & TSKPRI)) {
1384*7c478bd9Sstevel@tonic-gate 				THREAD_CHANGE_PRI(t, ts_maxumdpri);
1385*7c478bd9Sstevel@tonic-gate 				t->t_schedflag |= TS_DONT_SWAP;
1386*7c478bd9Sstevel@tonic-gate 				schedctl_set_yield(t, 1);
1387*7c478bd9Sstevel@tonic-gate 			}
1388*7c478bd9Sstevel@tonic-gate 			setfrontdq(t);
1389*7c478bd9Sstevel@tonic-gate 			goto done;
1390*7c478bd9Sstevel@tonic-gate 		} else {
1391*7c478bd9Sstevel@tonic-gate 			schedctl_set_nopreempt(t, 0);
1392*7c478bd9Sstevel@tonic-gate 			DTRACE_SCHED1(schedctl__preempt, kthread_t *, t);
1393*7c478bd9Sstevel@tonic-gate 			TNF_PROBE_2(schedctl_preempt, "schedctl TS ts_preempt",
1394*7c478bd9Sstevel@tonic-gate 			    /* CSTYLED */, tnf_pid, pid, ttoproc(t)->p_pid,
1395*7c478bd9Sstevel@tonic-gate 			    tnf_lwpid, lwpid, t->t_tid);
1396*7c478bd9Sstevel@tonic-gate 			/*
1397*7c478bd9Sstevel@tonic-gate 			 * Fall through and be preempted below.
1398*7c478bd9Sstevel@tonic-gate 			 */
1399*7c478bd9Sstevel@tonic-gate 		}
1400*7c478bd9Sstevel@tonic-gate 	}
1401*7c478bd9Sstevel@tonic-gate 
1402*7c478bd9Sstevel@tonic-gate 	if ((tspp->ts_flags & (TSBACKQ|TSKPRI)) == TSBACKQ) {
1403*7c478bd9Sstevel@tonic-gate 		tspp->ts_timeleft = ts_dptbl[tspp->ts_cpupri].ts_quantum;
1404*7c478bd9Sstevel@tonic-gate 		tspp->ts_dispwait = 0;
1405*7c478bd9Sstevel@tonic-gate 		tspp->ts_flags &= ~TSBACKQ;
1406*7c478bd9Sstevel@tonic-gate 		setbackdq(t);
1407*7c478bd9Sstevel@tonic-gate 	} else if ((tspp->ts_flags & (TSBACKQ|TSKPRI)) == (TSBACKQ|TSKPRI)) {
1408*7c478bd9Sstevel@tonic-gate 		tspp->ts_flags &= ~TSBACKQ;
1409*7c478bd9Sstevel@tonic-gate 		setbackdq(t);
1410*7c478bd9Sstevel@tonic-gate 	} else {
1411*7c478bd9Sstevel@tonic-gate #ifdef KSLICE
1412*7c478bd9Sstevel@tonic-gate 		if (kslice)
1413*7c478bd9Sstevel@tonic-gate 			setbackdq(t);
1414*7c478bd9Sstevel@tonic-gate 		else
1415*7c478bd9Sstevel@tonic-gate #endif
1416*7c478bd9Sstevel@tonic-gate 			setfrontdq(t);
1417*7c478bd9Sstevel@tonic-gate 	}
1418*7c478bd9Sstevel@tonic-gate 
1419*7c478bd9Sstevel@tonic-gate done:
1420*7c478bd9Sstevel@tonic-gate 	TRACE_2(TR_FAC_DISP, TR_PREEMPT,
1421*7c478bd9Sstevel@tonic-gate 	    "preempt:tid %p old pri %d", t, oldpri);
1422*7c478bd9Sstevel@tonic-gate }
1423*7c478bd9Sstevel@tonic-gate 
1424*7c478bd9Sstevel@tonic-gate static void
1425*7c478bd9Sstevel@tonic-gate ts_setrun(kthread_t *t)
1426*7c478bd9Sstevel@tonic-gate {
1427*7c478bd9Sstevel@tonic-gate 	tsproc_t *tspp = (tsproc_t *)(t->t_cldata);
1428*7c478bd9Sstevel@tonic-gate 
1429*7c478bd9Sstevel@tonic-gate 	ASSERT(THREAD_LOCK_HELD(t));	/* t should be in transition */
1430*7c478bd9Sstevel@tonic-gate 
1431*7c478bd9Sstevel@tonic-gate 	if (tspp->ts_dispwait > ts_dptbl[tspp->ts_umdpri].ts_maxwait) {
1432*7c478bd9Sstevel@tonic-gate 		tspp->ts_cpupri = ts_dptbl[tspp->ts_cpupri].ts_slpret;
1433*7c478bd9Sstevel@tonic-gate 		TS_NEWUMDPRI(tspp);
1434*7c478bd9Sstevel@tonic-gate 		tspp->ts_timeleft = ts_dptbl[tspp->ts_cpupri].ts_quantum;
1435*7c478bd9Sstevel@tonic-gate 		tspp->ts_dispwait = 0;
1436*7c478bd9Sstevel@tonic-gate 		if ((tspp->ts_flags & TSKPRI) == 0) {
1437*7c478bd9Sstevel@tonic-gate 			THREAD_CHANGE_PRI(t,
1438*7c478bd9Sstevel@tonic-gate 			    ts_dptbl[tspp->ts_umdpri].ts_globpri);
1439*7c478bd9Sstevel@tonic-gate 			ASSERT(t->t_pri >= 0 && t->t_pri <= ts_maxglobpri);
1440*7c478bd9Sstevel@tonic-gate 		}
1441*7c478bd9Sstevel@tonic-gate 	}
1442*7c478bd9Sstevel@tonic-gate 
1443*7c478bd9Sstevel@tonic-gate 	tspp->ts_flags &= ~TSBACKQ;
1444*7c478bd9Sstevel@tonic-gate 
1445*7c478bd9Sstevel@tonic-gate 	if (tspp->ts_flags & TSIA) {
1446*7c478bd9Sstevel@tonic-gate 		if (tspp->ts_flags & TSIASET)
1447*7c478bd9Sstevel@tonic-gate 			setfrontdq(t);
1448*7c478bd9Sstevel@tonic-gate 		else
1449*7c478bd9Sstevel@tonic-gate 			setbackdq(t);
1450*7c478bd9Sstevel@tonic-gate 	} else {
1451*7c478bd9Sstevel@tonic-gate 		if (t->t_disp_time != lbolt)
1452*7c478bd9Sstevel@tonic-gate 			setbackdq(t);
1453*7c478bd9Sstevel@tonic-gate 		else
1454*7c478bd9Sstevel@tonic-gate 			setfrontdq(t);
1455*7c478bd9Sstevel@tonic-gate 	}
1456*7c478bd9Sstevel@tonic-gate }
1457*7c478bd9Sstevel@tonic-gate 
1458*7c478bd9Sstevel@tonic-gate 
1459*7c478bd9Sstevel@tonic-gate /*
1460*7c478bd9Sstevel@tonic-gate  * Prepare thread for sleep. We reset the thread priority so it will
1461*7c478bd9Sstevel@tonic-gate  * run at the kernel priority level when it wakes up.
1462*7c478bd9Sstevel@tonic-gate  */
1463*7c478bd9Sstevel@tonic-gate static void
1464*7c478bd9Sstevel@tonic-gate ts_sleep(kthread_t *t)
1465*7c478bd9Sstevel@tonic-gate {
1466*7c478bd9Sstevel@tonic-gate 	tsproc_t	*tspp = (tsproc_t *)(t->t_cldata);
1467*7c478bd9Sstevel@tonic-gate 	int		flags;
1468*7c478bd9Sstevel@tonic-gate 	pri_t		old_pri = t->t_pri;
1469*7c478bd9Sstevel@tonic-gate 
1470*7c478bd9Sstevel@tonic-gate 	ASSERT(t == curthread);
1471*7c478bd9Sstevel@tonic-gate 	ASSERT(THREAD_LOCK_HELD(t));
1472*7c478bd9Sstevel@tonic-gate 
1473*7c478bd9Sstevel@tonic-gate 	flags = tspp->ts_flags;
1474*7c478bd9Sstevel@tonic-gate 	if (t->t_kpri_req) {
1475*7c478bd9Sstevel@tonic-gate 		tspp->ts_flags = flags | TSKPRI;
1476*7c478bd9Sstevel@tonic-gate 		THREAD_CHANGE_PRI(t, ts_kmdpris[0]);
1477*7c478bd9Sstevel@tonic-gate 		ASSERT(t->t_pri >= 0 && t->t_pri <= ts_maxglobpri);
1478*7c478bd9Sstevel@tonic-gate 		t->t_trapret = 1;		/* so ts_trapret will run */
1479*7c478bd9Sstevel@tonic-gate 		aston(t);
1480*7c478bd9Sstevel@tonic-gate 	} else if (tspp->ts_dispwait > ts_dptbl[tspp->ts_umdpri].ts_maxwait) {
1481*7c478bd9Sstevel@tonic-gate 		/*
1482*7c478bd9Sstevel@tonic-gate 		 * If thread has blocked in the kernel (as opposed to
1483*7c478bd9Sstevel@tonic-gate 		 * being merely preempted), recompute the user mode priority.
1484*7c478bd9Sstevel@tonic-gate 		 */
1485*7c478bd9Sstevel@tonic-gate 		tspp->ts_cpupri = ts_dptbl[tspp->ts_cpupri].ts_slpret;
1486*7c478bd9Sstevel@tonic-gate 		TS_NEWUMDPRI(tspp);
1487*7c478bd9Sstevel@tonic-gate 		tspp->ts_timeleft = ts_dptbl[tspp->ts_cpupri].ts_quantum;
1488*7c478bd9Sstevel@tonic-gate 		tspp->ts_dispwait = 0;
1489*7c478bd9Sstevel@tonic-gate 
1490*7c478bd9Sstevel@tonic-gate 		THREAD_CHANGE_PRI(curthread,
1491*7c478bd9Sstevel@tonic-gate 		    ts_dptbl[tspp->ts_umdpri].ts_globpri);
1492*7c478bd9Sstevel@tonic-gate 		ASSERT(curthread->t_pri >= 0 &&
1493*7c478bd9Sstevel@tonic-gate 		    curthread->t_pri <= ts_maxglobpri);
1494*7c478bd9Sstevel@tonic-gate 		tspp->ts_flags = flags & ~TSKPRI;
1495*7c478bd9Sstevel@tonic-gate 
1496*7c478bd9Sstevel@tonic-gate 		if (DISP_MUST_SURRENDER(curthread))
1497*7c478bd9Sstevel@tonic-gate 			cpu_surrender(curthread);
1498*7c478bd9Sstevel@tonic-gate 	} else if (flags & TSKPRI) {
1499*7c478bd9Sstevel@tonic-gate 		THREAD_CHANGE_PRI(curthread,
1500*7c478bd9Sstevel@tonic-gate 		    ts_dptbl[tspp->ts_umdpri].ts_globpri);
1501*7c478bd9Sstevel@tonic-gate 		ASSERT(curthread->t_pri >= 0 &&
1502*7c478bd9Sstevel@tonic-gate 		    curthread->t_pri <= ts_maxglobpri);
1503*7c478bd9Sstevel@tonic-gate 		tspp->ts_flags = flags & ~TSKPRI;
1504*7c478bd9Sstevel@tonic-gate 
1505*7c478bd9Sstevel@tonic-gate 		if (DISP_MUST_SURRENDER(curthread))
1506*7c478bd9Sstevel@tonic-gate 			cpu_surrender(curthread);
1507*7c478bd9Sstevel@tonic-gate 	}
1508*7c478bd9Sstevel@tonic-gate 	t->t_stime = lbolt;		/* time stamp for the swapper */
1509*7c478bd9Sstevel@tonic-gate 	TRACE_2(TR_FAC_DISP, TR_SLEEP,
1510*7c478bd9Sstevel@tonic-gate 	    "sleep:tid %p old pri %d", t, old_pri);
1511*7c478bd9Sstevel@tonic-gate }
1512*7c478bd9Sstevel@tonic-gate 
1513*7c478bd9Sstevel@tonic-gate 
1514*7c478bd9Sstevel@tonic-gate /*
1515*7c478bd9Sstevel@tonic-gate  * Return Values:
1516*7c478bd9Sstevel@tonic-gate  *
1517*7c478bd9Sstevel@tonic-gate  *	-1 if the thread is loaded or is not eligible to be swapped in.
1518*7c478bd9Sstevel@tonic-gate  *
1519*7c478bd9Sstevel@tonic-gate  *	effective priority of the specified thread based on swapout time
1520*7c478bd9Sstevel@tonic-gate  *		and size of process (epri >= 0 , epri <= SHRT_MAX).
1521*7c478bd9Sstevel@tonic-gate  */
1522*7c478bd9Sstevel@tonic-gate /* ARGSUSED */
1523*7c478bd9Sstevel@tonic-gate static pri_t
1524*7c478bd9Sstevel@tonic-gate ts_swapin(kthread_t *t, int flags)
1525*7c478bd9Sstevel@tonic-gate {
1526*7c478bd9Sstevel@tonic-gate 	tsproc_t	*tspp = (tsproc_t *)(t->t_cldata);
1527*7c478bd9Sstevel@tonic-gate 	long		epri = -1;
1528*7c478bd9Sstevel@tonic-gate 	proc_t		*pp = ttoproc(t);
1529*7c478bd9Sstevel@tonic-gate 
1530*7c478bd9Sstevel@tonic-gate 	ASSERT(THREAD_LOCK_HELD(t));
1531*7c478bd9Sstevel@tonic-gate 
1532*7c478bd9Sstevel@tonic-gate 	/*
1533*7c478bd9Sstevel@tonic-gate 	 * We know that pri_t is a short.
1534*7c478bd9Sstevel@tonic-gate 	 * Be sure not to overrun its range.
1535*7c478bd9Sstevel@tonic-gate 	 */
1536*7c478bd9Sstevel@tonic-gate 	if (t->t_state == TS_RUN && (t->t_schedflag & TS_LOAD) == 0) {
1537*7c478bd9Sstevel@tonic-gate 		time_t swapout_time;
1538*7c478bd9Sstevel@tonic-gate 
1539*7c478bd9Sstevel@tonic-gate 		swapout_time = (lbolt - t->t_stime) / hz;
1540*7c478bd9Sstevel@tonic-gate 		if (INHERITED(t) || (tspp->ts_flags & (TSKPRI | TSIASET)))
1541*7c478bd9Sstevel@tonic-gate 			epri = (long)DISP_PRIO(t) + swapout_time;
1542*7c478bd9Sstevel@tonic-gate 		else {
1543*7c478bd9Sstevel@tonic-gate 			/*
1544*7c478bd9Sstevel@tonic-gate 			 * Threads which have been out for a long time,
1545*7c478bd9Sstevel@tonic-gate 			 * have high user mode priority and are associated
1546*7c478bd9Sstevel@tonic-gate 			 * with a small address space are more deserving
1547*7c478bd9Sstevel@tonic-gate 			 */
1548*7c478bd9Sstevel@tonic-gate 			epri = ts_dptbl[tspp->ts_umdpri].ts_globpri;
1549*7c478bd9Sstevel@tonic-gate 			ASSERT(epri >= 0 && epri <= ts_maxumdpri);
1550*7c478bd9Sstevel@tonic-gate 			epri += swapout_time - pp->p_swrss / nz(maxpgio)/2;
1551*7c478bd9Sstevel@tonic-gate 		}
1552*7c478bd9Sstevel@tonic-gate 		/*
1553*7c478bd9Sstevel@tonic-gate 		 * Scale epri so SHRT_MAX/2 represents zero priority.
1554*7c478bd9Sstevel@tonic-gate 		 */
1555*7c478bd9Sstevel@tonic-gate 		epri += SHRT_MAX/2;
1556*7c478bd9Sstevel@tonic-gate 		if (epri < 0)
1557*7c478bd9Sstevel@tonic-gate 			epri = 0;
1558*7c478bd9Sstevel@tonic-gate 		else if (epri > SHRT_MAX)
1559*7c478bd9Sstevel@tonic-gate 			epri = SHRT_MAX;
1560*7c478bd9Sstevel@tonic-gate 	}
1561*7c478bd9Sstevel@tonic-gate 	return ((pri_t)epri);
1562*7c478bd9Sstevel@tonic-gate }
1563*7c478bd9Sstevel@tonic-gate 
1564*7c478bd9Sstevel@tonic-gate /*
1565*7c478bd9Sstevel@tonic-gate  * Return Values
1566*7c478bd9Sstevel@tonic-gate  *	-1 if the thread isn't loaded or is not eligible to be swapped out.
1567*7c478bd9Sstevel@tonic-gate  *
1568*7c478bd9Sstevel@tonic-gate  *	effective priority of the specified thread based on if the swapper
1569*7c478bd9Sstevel@tonic-gate  *		is in softswap or hardswap mode.
1570*7c478bd9Sstevel@tonic-gate  *
1571*7c478bd9Sstevel@tonic-gate  *		Softswap:  Return a low effective priority for threads
1572*7c478bd9Sstevel@tonic-gate  *			   sleeping for more than maxslp secs.
1573*7c478bd9Sstevel@tonic-gate  *
1574*7c478bd9Sstevel@tonic-gate  *		Hardswap:  Return an effective priority such that threads
1575*7c478bd9Sstevel@tonic-gate  *			   which have been in memory for a while and are
1576*7c478bd9Sstevel@tonic-gate  *			   associated with a small address space are swapped
1577*7c478bd9Sstevel@tonic-gate  *			   in before others.
1578*7c478bd9Sstevel@tonic-gate  *
1579*7c478bd9Sstevel@tonic-gate  *		(epri >= 0 , epri <= SHRT_MAX).
1580*7c478bd9Sstevel@tonic-gate  */
1581*7c478bd9Sstevel@tonic-gate time_t	ts_minrun = 2;		/* XXX - t_pri becomes 59 within 2 secs */
1582*7c478bd9Sstevel@tonic-gate time_t	ts_minslp = 2;		/* min time on sleep queue for hardswap */
1583*7c478bd9Sstevel@tonic-gate 
1584*7c478bd9Sstevel@tonic-gate static pri_t
1585*7c478bd9Sstevel@tonic-gate ts_swapout(kthread_t *t, int flags)
1586*7c478bd9Sstevel@tonic-gate {
1587*7c478bd9Sstevel@tonic-gate 	tsproc_t	*tspp = (tsproc_t *)(t->t_cldata);
1588*7c478bd9Sstevel@tonic-gate 	long		epri = -1;
1589*7c478bd9Sstevel@tonic-gate 	proc_t		*pp = ttoproc(t);
1590*7c478bd9Sstevel@tonic-gate 	time_t		swapin_time;
1591*7c478bd9Sstevel@tonic-gate 
1592*7c478bd9Sstevel@tonic-gate 	ASSERT(THREAD_LOCK_HELD(t));
1593*7c478bd9Sstevel@tonic-gate 
1594*7c478bd9Sstevel@tonic-gate 	if (INHERITED(t) || (tspp->ts_flags & (TSKPRI | TSIASET)) ||
1595*7c478bd9Sstevel@tonic-gate 	    (t->t_proc_flag & TP_LWPEXIT) ||
1596*7c478bd9Sstevel@tonic-gate 	    (t->t_state & (TS_ZOMB | TS_FREE | TS_STOPPED | TS_ONPROC)) ||
1597*7c478bd9Sstevel@tonic-gate 	    !(t->t_schedflag & TS_LOAD) || !SWAP_OK(t))
1598*7c478bd9Sstevel@tonic-gate 		return (-1);
1599*7c478bd9Sstevel@tonic-gate 
1600*7c478bd9Sstevel@tonic-gate 	ASSERT(t->t_state & (TS_SLEEP | TS_RUN));
1601*7c478bd9Sstevel@tonic-gate 
1602*7c478bd9Sstevel@tonic-gate 	/*
1603*7c478bd9Sstevel@tonic-gate 	 * We know that pri_t is a short.
1604*7c478bd9Sstevel@tonic-gate 	 * Be sure not to overrun its range.
1605*7c478bd9Sstevel@tonic-gate 	 */
1606*7c478bd9Sstevel@tonic-gate 	swapin_time = (lbolt - t->t_stime) / hz;
1607*7c478bd9Sstevel@tonic-gate 	if (flags == SOFTSWAP) {
1608*7c478bd9Sstevel@tonic-gate 		if (t->t_state == TS_SLEEP && swapin_time > maxslp) {
1609*7c478bd9Sstevel@tonic-gate 			epri = 0;
1610*7c478bd9Sstevel@tonic-gate 		} else {
1611*7c478bd9Sstevel@tonic-gate 			return ((pri_t)epri);
1612*7c478bd9Sstevel@tonic-gate 		}
1613*7c478bd9Sstevel@tonic-gate 	} else {
1614*7c478bd9Sstevel@tonic-gate 		pri_t pri;
1615*7c478bd9Sstevel@tonic-gate 
1616*7c478bd9Sstevel@tonic-gate 		if ((t->t_state == TS_SLEEP && swapin_time > ts_minslp) ||
1617*7c478bd9Sstevel@tonic-gate 		    (t->t_state == TS_RUN && swapin_time > ts_minrun)) {
1618*7c478bd9Sstevel@tonic-gate 			pri = ts_dptbl[tspp->ts_umdpri].ts_globpri;
1619*7c478bd9Sstevel@tonic-gate 			ASSERT(pri >= 0 && pri <= ts_maxumdpri);
1620*7c478bd9Sstevel@tonic-gate 			epri = swapin_time -
1621*7c478bd9Sstevel@tonic-gate 			    (rm_asrss(pp->p_as) / nz(maxpgio)/2) - (long)pri;
1622*7c478bd9Sstevel@tonic-gate 		} else {
1623*7c478bd9Sstevel@tonic-gate 			return ((pri_t)epri);
1624*7c478bd9Sstevel@tonic-gate 		}
1625*7c478bd9Sstevel@tonic-gate 	}
1626*7c478bd9Sstevel@tonic-gate 
1627*7c478bd9Sstevel@tonic-gate 	/*
1628*7c478bd9Sstevel@tonic-gate 	 * Scale epri so SHRT_MAX/2 represents zero priority.
1629*7c478bd9Sstevel@tonic-gate 	 */
1630*7c478bd9Sstevel@tonic-gate 	epri += SHRT_MAX/2;
1631*7c478bd9Sstevel@tonic-gate 	if (epri < 0)
1632*7c478bd9Sstevel@tonic-gate 		epri = 0;
1633*7c478bd9Sstevel@tonic-gate 	else if (epri > SHRT_MAX)
1634*7c478bd9Sstevel@tonic-gate 		epri = SHRT_MAX;
1635*7c478bd9Sstevel@tonic-gate 
1636*7c478bd9Sstevel@tonic-gate 	return ((pri_t)epri);
1637*7c478bd9Sstevel@tonic-gate }
1638*7c478bd9Sstevel@tonic-gate 
1639*7c478bd9Sstevel@tonic-gate /*
1640*7c478bd9Sstevel@tonic-gate  * Check for time slice expiration.  If time slice has expired
1641*7c478bd9Sstevel@tonic-gate  * move thread to priority specified in tsdptbl for time slice expiration
1642*7c478bd9Sstevel@tonic-gate  * and set runrun to cause preemption.
1643*7c478bd9Sstevel@tonic-gate  */
1644*7c478bd9Sstevel@tonic-gate 
1645*7c478bd9Sstevel@tonic-gate static void
1646*7c478bd9Sstevel@tonic-gate ts_tick(kthread_t *t)
1647*7c478bd9Sstevel@tonic-gate {
1648*7c478bd9Sstevel@tonic-gate 	tsproc_t *tspp = (tsproc_t *)(t->t_cldata);
1649*7c478bd9Sstevel@tonic-gate 	klwp_t *lwp;
1650*7c478bd9Sstevel@tonic-gate 	pri_t	oldpri = t->t_pri;
1651*7c478bd9Sstevel@tonic-gate 
1652*7c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&(ttoproc(t))->p_lock));
1653*7c478bd9Sstevel@tonic-gate 
1654*7c478bd9Sstevel@tonic-gate 	thread_lock(t);
1655*7c478bd9Sstevel@tonic-gate 	if ((tspp->ts_flags & TSKPRI) == 0) {
1656*7c478bd9Sstevel@tonic-gate 		if (--tspp->ts_timeleft <= 0) {
1657*7c478bd9Sstevel@tonic-gate 			pri_t	new_pri;
1658*7c478bd9Sstevel@tonic-gate 
1659*7c478bd9Sstevel@tonic-gate 			/*
1660*7c478bd9Sstevel@tonic-gate 			 * If we're doing preemption control and trying to
1661*7c478bd9Sstevel@tonic-gate 			 * avoid preempting this thread, just note that
1662*7c478bd9Sstevel@tonic-gate 			 * the thread should yield soon and let it keep
1663*7c478bd9Sstevel@tonic-gate 			 * running (unless it's been a while).
1664*7c478bd9Sstevel@tonic-gate 			 */
1665*7c478bd9Sstevel@tonic-gate 			if (t->t_schedctl && schedctl_get_nopreempt(t)) {
1666*7c478bd9Sstevel@tonic-gate 				if (tspp->ts_timeleft > -SC_MAX_TICKS) {
1667*7c478bd9Sstevel@tonic-gate 					DTRACE_SCHED1(schedctl__nopreempt,
1668*7c478bd9Sstevel@tonic-gate 					    kthread_t *, t);
1669*7c478bd9Sstevel@tonic-gate 					schedctl_set_yield(t, 1);
1670*7c478bd9Sstevel@tonic-gate 					thread_unlock_nopreempt(t);
1671*7c478bd9Sstevel@tonic-gate 					return;
1672*7c478bd9Sstevel@tonic-gate 				}
1673*7c478bd9Sstevel@tonic-gate 
1674*7c478bd9Sstevel@tonic-gate 				TNF_PROBE_2(schedctl_failsafe,
1675*7c478bd9Sstevel@tonic-gate 				    "schedctl TS ts_tick", /* CSTYLED */,
1676*7c478bd9Sstevel@tonic-gate 				    tnf_pid, pid, ttoproc(t)->p_pid,
1677*7c478bd9Sstevel@tonic-gate 				    tnf_lwpid, lwpid, t->t_tid);
1678*7c478bd9Sstevel@tonic-gate 			}
1679*7c478bd9Sstevel@tonic-gate 			tspp->ts_cpupri = ts_dptbl[tspp->ts_cpupri].ts_tqexp;
1680*7c478bd9Sstevel@tonic-gate 			TS_NEWUMDPRI(tspp);
1681*7c478bd9Sstevel@tonic-gate 			tspp->ts_dispwait = 0;
1682*7c478bd9Sstevel@tonic-gate 			new_pri = ts_dptbl[tspp->ts_umdpri].ts_globpri;
1683*7c478bd9Sstevel@tonic-gate 			ASSERT(new_pri >= 0 && new_pri <= ts_maxglobpri);
1684*7c478bd9Sstevel@tonic-gate 			/*
1685*7c478bd9Sstevel@tonic-gate 			 * When the priority of a thread is changed,
1686*7c478bd9Sstevel@tonic-gate 			 * it may be necessary to adjust its position
1687*7c478bd9Sstevel@tonic-gate 			 * on a sleep queue or dispatch queue.
1688*7c478bd9Sstevel@tonic-gate 			 * The function thread_change_pri accomplishes
1689*7c478bd9Sstevel@tonic-gate 			 * this.
1690*7c478bd9Sstevel@tonic-gate 			 */
1691*7c478bd9Sstevel@tonic-gate 			if (thread_change_pri(t, new_pri, 0)) {
1692*7c478bd9Sstevel@tonic-gate 				if ((t->t_schedflag & TS_LOAD) &&
1693*7c478bd9Sstevel@tonic-gate 				    (lwp = t->t_lwp) &&
1694*7c478bd9Sstevel@tonic-gate 				    lwp->lwp_state == LWP_USER)
1695*7c478bd9Sstevel@tonic-gate 					t->t_schedflag &= ~TS_DONT_SWAP;
1696*7c478bd9Sstevel@tonic-gate 				tspp->ts_timeleft =
1697*7c478bd9Sstevel@tonic-gate 				    ts_dptbl[tspp->ts_cpupri].ts_quantum;
1698*7c478bd9Sstevel@tonic-gate 			} else {
1699*7c478bd9Sstevel@tonic-gate 				tspp->ts_flags |= TSBACKQ;
1700*7c478bd9Sstevel@tonic-gate 				cpu_surrender(t);
1701*7c478bd9Sstevel@tonic-gate 			}
1702*7c478bd9Sstevel@tonic-gate 			TRACE_2(TR_FAC_DISP, TR_TICK,
1703*7c478bd9Sstevel@tonic-gate 			    "tick:tid %p old pri %d", t, oldpri);
1704*7c478bd9Sstevel@tonic-gate 		} else if (t->t_pri < t->t_disp_queue->disp_maxrunpri) {
1705*7c478bd9Sstevel@tonic-gate 			tspp->ts_flags |= TSBACKQ;
1706*7c478bd9Sstevel@tonic-gate 			cpu_surrender(t);
1707*7c478bd9Sstevel@tonic-gate 		}
1708*7c478bd9Sstevel@tonic-gate 	}
1709*7c478bd9Sstevel@tonic-gate 	thread_unlock_nopreempt(t);	/* clock thread can't be preempted */
1710*7c478bd9Sstevel@tonic-gate }
1711*7c478bd9Sstevel@tonic-gate 
1712*7c478bd9Sstevel@tonic-gate 
1713*7c478bd9Sstevel@tonic-gate /*
1714*7c478bd9Sstevel@tonic-gate  * If thread is currently at a kernel mode priority (has slept)
1715*7c478bd9Sstevel@tonic-gate  * we assign it the appropriate user mode priority and time quantum
1716*7c478bd9Sstevel@tonic-gate  * here.  If we are lowering the thread's priority below that of
1717*7c478bd9Sstevel@tonic-gate  * other runnable threads we will normally set runrun via cpu_surrender() to
1718*7c478bd9Sstevel@tonic-gate  * cause preemption.
1719*7c478bd9Sstevel@tonic-gate  */
1720*7c478bd9Sstevel@tonic-gate static void
1721*7c478bd9Sstevel@tonic-gate ts_trapret(kthread_t *t)
1722*7c478bd9Sstevel@tonic-gate {
1723*7c478bd9Sstevel@tonic-gate 	tsproc_t	*tspp = (tsproc_t *)t->t_cldata;
1724*7c478bd9Sstevel@tonic-gate 	cpu_t		*cp = CPU;
1725*7c478bd9Sstevel@tonic-gate 	pri_t		old_pri = curthread->t_pri;
1726*7c478bd9Sstevel@tonic-gate 
1727*7c478bd9Sstevel@tonic-gate 	ASSERT(THREAD_LOCK_HELD(t));
1728*7c478bd9Sstevel@tonic-gate 	ASSERT(t == curthread);
1729*7c478bd9Sstevel@tonic-gate 	ASSERT(cp->cpu_dispthread == t);
1730*7c478bd9Sstevel@tonic-gate 	ASSERT(t->t_state == TS_ONPROC);
1731*7c478bd9Sstevel@tonic-gate 
1732*7c478bd9Sstevel@tonic-gate 	t->t_kpri_req = 0;
1733*7c478bd9Sstevel@tonic-gate 	if (tspp->ts_dispwait > ts_dptbl[tspp->ts_umdpri].ts_maxwait) {
1734*7c478bd9Sstevel@tonic-gate 		tspp->ts_cpupri = ts_dptbl[tspp->ts_cpupri].ts_slpret;
1735*7c478bd9Sstevel@tonic-gate 		TS_NEWUMDPRI(tspp);
1736*7c478bd9Sstevel@tonic-gate 		tspp->ts_timeleft = ts_dptbl[tspp->ts_cpupri].ts_quantum;
1737*7c478bd9Sstevel@tonic-gate 		tspp->ts_dispwait = 0;
1738*7c478bd9Sstevel@tonic-gate 
1739*7c478bd9Sstevel@tonic-gate 		/*
1740*7c478bd9Sstevel@tonic-gate 		 * If thread has blocked in the kernel (as opposed to
1741*7c478bd9Sstevel@tonic-gate 		 * being merely preempted), recompute the user mode priority.
1742*7c478bd9Sstevel@tonic-gate 		 */
1743*7c478bd9Sstevel@tonic-gate 		THREAD_CHANGE_PRI(t, ts_dptbl[tspp->ts_umdpri].ts_globpri);
1744*7c478bd9Sstevel@tonic-gate 		cp->cpu_dispatch_pri = DISP_PRIO(t);
1745*7c478bd9Sstevel@tonic-gate 		ASSERT(t->t_pri >= 0 && t->t_pri <= ts_maxglobpri);
1746*7c478bd9Sstevel@tonic-gate 		tspp->ts_flags &= ~TSKPRI;
1747*7c478bd9Sstevel@tonic-gate 
1748*7c478bd9Sstevel@tonic-gate 		if (DISP_MUST_SURRENDER(t))
1749*7c478bd9Sstevel@tonic-gate 			cpu_surrender(t);
1750*7c478bd9Sstevel@tonic-gate 	} else if (tspp->ts_flags & TSKPRI) {
1751*7c478bd9Sstevel@tonic-gate 		/*
1752*7c478bd9Sstevel@tonic-gate 		 * If thread has blocked in the kernel (as opposed to
1753*7c478bd9Sstevel@tonic-gate 		 * being merely preempted), recompute the user mode priority.
1754*7c478bd9Sstevel@tonic-gate 		 */
1755*7c478bd9Sstevel@tonic-gate 		THREAD_CHANGE_PRI(t, ts_dptbl[tspp->ts_umdpri].ts_globpri);
1756*7c478bd9Sstevel@tonic-gate 		cp->cpu_dispatch_pri = DISP_PRIO(t);
1757*7c478bd9Sstevel@tonic-gate 		ASSERT(t->t_pri >= 0 && t->t_pri <= ts_maxglobpri);
1758*7c478bd9Sstevel@tonic-gate 		tspp->ts_flags &= ~TSKPRI;
1759*7c478bd9Sstevel@tonic-gate 
1760*7c478bd9Sstevel@tonic-gate 		if (DISP_MUST_SURRENDER(t))
1761*7c478bd9Sstevel@tonic-gate 			cpu_surrender(t);
1762*7c478bd9Sstevel@tonic-gate 	}
1763*7c478bd9Sstevel@tonic-gate 
1764*7c478bd9Sstevel@tonic-gate 	/*
1765*7c478bd9Sstevel@tonic-gate 	 * Swapout lwp if the swapper is waiting for this thread to
1766*7c478bd9Sstevel@tonic-gate 	 * reach a safe point.
1767*7c478bd9Sstevel@tonic-gate 	 */
1768*7c478bd9Sstevel@tonic-gate 	if ((t->t_schedflag & TS_SWAPENQ) && !(tspp->ts_flags & TSIASET)) {
1769*7c478bd9Sstevel@tonic-gate 		thread_unlock(t);
1770*7c478bd9Sstevel@tonic-gate 		swapout_lwp(ttolwp(t));
1771*7c478bd9Sstevel@tonic-gate 		thread_lock(t);
1772*7c478bd9Sstevel@tonic-gate 	}
1773*7c478bd9Sstevel@tonic-gate 
1774*7c478bd9Sstevel@tonic-gate 	TRACE_2(TR_FAC_DISP, TR_TRAPRET,
1775*7c478bd9Sstevel@tonic-gate 	    "trapret:tid %p old pri %d", t, old_pri);
1776*7c478bd9Sstevel@tonic-gate }
1777*7c478bd9Sstevel@tonic-gate 
1778*7c478bd9Sstevel@tonic-gate 
1779*7c478bd9Sstevel@tonic-gate /*
1780*7c478bd9Sstevel@tonic-gate  * Update the ts_dispwait values of all time sharing threads that
1781*7c478bd9Sstevel@tonic-gate  * are currently runnable at a user mode priority and bump the priority
1782*7c478bd9Sstevel@tonic-gate  * if ts_dispwait exceeds ts_maxwait.  Called once per second via
1783*7c478bd9Sstevel@tonic-gate  * timeout which we reset here.
1784*7c478bd9Sstevel@tonic-gate  *
1785*7c478bd9Sstevel@tonic-gate  * There are several lists of time sharing threads broken up by a hash on
1786*7c478bd9Sstevel@tonic-gate  * the thread pointer.  Each list has its own lock.  This avoids blocking
1787*7c478bd9Sstevel@tonic-gate  * all ts_enterclass, ts_fork, and ts_exitclass operations while ts_update
1788*7c478bd9Sstevel@tonic-gate  * runs.  ts_update traverses each list in turn.
1789*7c478bd9Sstevel@tonic-gate  *
1790*7c478bd9Sstevel@tonic-gate  * If multiple threads have their priorities updated to the same value,
1791*7c478bd9Sstevel@tonic-gate  * the system implicitly favors the one that is updated first (since it
1792*7c478bd9Sstevel@tonic-gate  * winds up first on the run queue).  To avoid this unfairness, the
1793*7c478bd9Sstevel@tonic-gate  * traversal of threads starts at the list indicated by a marker.  When
1794*7c478bd9Sstevel@tonic-gate  * threads in more than one list have their priorities updated, the marker
1795*7c478bd9Sstevel@tonic-gate  * is moved.  This changes the order the threads will be placed on the run
1796*7c478bd9Sstevel@tonic-gate  * queue the next time ts_update is called and preserves fairness over the
1797*7c478bd9Sstevel@tonic-gate  * long run.  The marker doesn't need to be protected by a lock since it's
1798*7c478bd9Sstevel@tonic-gate  * only accessed by ts_update, which is inherently single-threaded (only
1799*7c478bd9Sstevel@tonic-gate  * one instance can be running at a time).
1800*7c478bd9Sstevel@tonic-gate  */
1801*7c478bd9Sstevel@tonic-gate static void
1802*7c478bd9Sstevel@tonic-gate ts_update(void *arg)
1803*7c478bd9Sstevel@tonic-gate {
1804*7c478bd9Sstevel@tonic-gate 	int		i;
1805*7c478bd9Sstevel@tonic-gate 	int		new_marker = -1;
1806*7c478bd9Sstevel@tonic-gate 	static int	ts_update_marker;
1807*7c478bd9Sstevel@tonic-gate 
1808*7c478bd9Sstevel@tonic-gate 	/*
1809*7c478bd9Sstevel@tonic-gate 	 * Start with the ts_update_marker list, then do the rest.
1810*7c478bd9Sstevel@tonic-gate 	 */
1811*7c478bd9Sstevel@tonic-gate 	i = ts_update_marker;
1812*7c478bd9Sstevel@tonic-gate 	do {
1813*7c478bd9Sstevel@tonic-gate 		/*
1814*7c478bd9Sstevel@tonic-gate 		 * If this is the first list after the current marker to
1815*7c478bd9Sstevel@tonic-gate 		 * have threads with priorities updated, advance the marker
1816*7c478bd9Sstevel@tonic-gate 		 * to this list for the next time ts_update runs.
1817*7c478bd9Sstevel@tonic-gate 		 */
1818*7c478bd9Sstevel@tonic-gate 		if (ts_update_list(i) && new_marker == -1 &&
1819*7c478bd9Sstevel@tonic-gate 		    i != ts_update_marker) {
1820*7c478bd9Sstevel@tonic-gate 			new_marker = i;
1821*7c478bd9Sstevel@tonic-gate 		}
1822*7c478bd9Sstevel@tonic-gate 	} while ((i = TS_LIST_NEXT(i)) != ts_update_marker);
1823*7c478bd9Sstevel@tonic-gate 
1824*7c478bd9Sstevel@tonic-gate 	/* advance marker for next ts_update call */
1825*7c478bd9Sstevel@tonic-gate 	if (new_marker != -1)
1826*7c478bd9Sstevel@tonic-gate 		ts_update_marker = new_marker;
1827*7c478bd9Sstevel@tonic-gate 
1828*7c478bd9Sstevel@tonic-gate 	(void) timeout(ts_update, arg, hz);
1829*7c478bd9Sstevel@tonic-gate }
1830*7c478bd9Sstevel@tonic-gate 
1831*7c478bd9Sstevel@tonic-gate /*
1832*7c478bd9Sstevel@tonic-gate  * Updates priority for a list of threads.  Returns 1 if the priority of
1833*7c478bd9Sstevel@tonic-gate  * one of the threads was actually updated, 0 if none were for various
1834*7c478bd9Sstevel@tonic-gate  * reasons (thread is no longer in the TS or IA class, isn't runnable,
1835*7c478bd9Sstevel@tonic-gate  * hasn't waited long enough, has the preemption control no-preempt bit
1836*7c478bd9Sstevel@tonic-gate  * set, etc.)
1837*7c478bd9Sstevel@tonic-gate  */
1838*7c478bd9Sstevel@tonic-gate static int
1839*7c478bd9Sstevel@tonic-gate ts_update_list(int i)
1840*7c478bd9Sstevel@tonic-gate {
1841*7c478bd9Sstevel@tonic-gate 	tsproc_t *tspp;
1842*7c478bd9Sstevel@tonic-gate 	kthread_t *tx;
1843*7c478bd9Sstevel@tonic-gate 	int updated = 0;
1844*7c478bd9Sstevel@tonic-gate 
1845*7c478bd9Sstevel@tonic-gate 	mutex_enter(&ts_list_lock[i]);
1846*7c478bd9Sstevel@tonic-gate 	for (tspp = ts_plisthead[i].ts_next; tspp != &ts_plisthead[i];
1847*7c478bd9Sstevel@tonic-gate 	    tspp = tspp->ts_next) {
1848*7c478bd9Sstevel@tonic-gate 		tx = tspp->ts_tp;
1849*7c478bd9Sstevel@tonic-gate 		/*
1850*7c478bd9Sstevel@tonic-gate 		 * Lock the thread and verify state.
1851*7c478bd9Sstevel@tonic-gate 		 */
1852*7c478bd9Sstevel@tonic-gate 		thread_lock(tx);
1853*7c478bd9Sstevel@tonic-gate 		/*
1854*7c478bd9Sstevel@tonic-gate 		 * Skip the thread if it is no longer in the TS (or IA) class.
1855*7c478bd9Sstevel@tonic-gate 		 */
1856*7c478bd9Sstevel@tonic-gate 		if (tx->t_clfuncs != &ts_classfuncs.thread &&
1857*7c478bd9Sstevel@tonic-gate 		    tx->t_clfuncs != &ia_classfuncs.thread)
1858*7c478bd9Sstevel@tonic-gate 			goto next;
1859*7c478bd9Sstevel@tonic-gate 		tspp->ts_dispwait++;
1860*7c478bd9Sstevel@tonic-gate 		if ((tspp->ts_flags & TSKPRI) != 0)
1861*7c478bd9Sstevel@tonic-gate 			goto next;
1862*7c478bd9Sstevel@tonic-gate 		if (tspp->ts_dispwait <= ts_dptbl[tspp->ts_umdpri].ts_maxwait)
1863*7c478bd9Sstevel@tonic-gate 			goto next;
1864*7c478bd9Sstevel@tonic-gate 		if (tx->t_schedctl && schedctl_get_nopreempt(tx))
1865*7c478bd9Sstevel@tonic-gate 			goto next;
1866*7c478bd9Sstevel@tonic-gate 		if (tx->t_state != TS_RUN && (tx->t_state != TS_SLEEP ||
1867*7c478bd9Sstevel@tonic-gate 		    !ts_sleep_promote)) {
1868*7c478bd9Sstevel@tonic-gate 			/* make next syscall/trap do CL_TRAPRET */
1869*7c478bd9Sstevel@tonic-gate 			tx->t_trapret = 1;
1870*7c478bd9Sstevel@tonic-gate 			aston(tx);
1871*7c478bd9Sstevel@tonic-gate 			goto next;
1872*7c478bd9Sstevel@tonic-gate 		}
1873*7c478bd9Sstevel@tonic-gate 		tspp->ts_cpupri = ts_dptbl[tspp->ts_cpupri].ts_lwait;
1874*7c478bd9Sstevel@tonic-gate 		TS_NEWUMDPRI(tspp);
1875*7c478bd9Sstevel@tonic-gate 		tspp->ts_dispwait = 0;
1876*7c478bd9Sstevel@tonic-gate 		updated = 1;
1877*7c478bd9Sstevel@tonic-gate 
1878*7c478bd9Sstevel@tonic-gate 		/*
1879*7c478bd9Sstevel@tonic-gate 		 * Only dequeue it if needs to move; otherwise it should
1880*7c478bd9Sstevel@tonic-gate 		 * just round-robin here.
1881*7c478bd9Sstevel@tonic-gate 		 */
1882*7c478bd9Sstevel@tonic-gate 		if (tx->t_pri != ts_dptbl[tspp->ts_umdpri].ts_globpri) {
1883*7c478bd9Sstevel@tonic-gate 			pri_t oldpri = tx->t_pri;
1884*7c478bd9Sstevel@tonic-gate 			ts_change_priority(tx, tspp);
1885*7c478bd9Sstevel@tonic-gate 			TRACE_2(TR_FAC_DISP, TR_UPDATE,
1886*7c478bd9Sstevel@tonic-gate 			    "update:tid %p old pri %d", tx, oldpri);
1887*7c478bd9Sstevel@tonic-gate 		}
1888*7c478bd9Sstevel@tonic-gate next:
1889*7c478bd9Sstevel@tonic-gate 		thread_unlock(tx);
1890*7c478bd9Sstevel@tonic-gate 	}
1891*7c478bd9Sstevel@tonic-gate 	mutex_exit(&ts_list_lock[i]);
1892*7c478bd9Sstevel@tonic-gate 
1893*7c478bd9Sstevel@tonic-gate 	return (updated);
1894*7c478bd9Sstevel@tonic-gate }
1895*7c478bd9Sstevel@tonic-gate 
1896*7c478bd9Sstevel@tonic-gate 
1897*7c478bd9Sstevel@tonic-gate /*
1898*7c478bd9Sstevel@tonic-gate  * Processes waking up go to the back of their queue.  We don't
1899*7c478bd9Sstevel@tonic-gate  * need to assign a time quantum here because thread is still
1900*7c478bd9Sstevel@tonic-gate  * at a kernel mode priority and the time slicing is not done
1901*7c478bd9Sstevel@tonic-gate  * for threads running in the kernel after sleeping.  The proper
1902*7c478bd9Sstevel@tonic-gate  * time quantum will be assigned by ts_trapret before the thread
1903*7c478bd9Sstevel@tonic-gate  * returns to user mode.
1904*7c478bd9Sstevel@tonic-gate  */
1905*7c478bd9Sstevel@tonic-gate static void
1906*7c478bd9Sstevel@tonic-gate ts_wakeup(kthread_t *t)
1907*7c478bd9Sstevel@tonic-gate {
1908*7c478bd9Sstevel@tonic-gate 	tsproc_t	*tspp = (tsproc_t *)(t->t_cldata);
1909*7c478bd9Sstevel@tonic-gate 
1910*7c478bd9Sstevel@tonic-gate 	ASSERT(THREAD_LOCK_HELD(t));
1911*7c478bd9Sstevel@tonic-gate 
1912*7c478bd9Sstevel@tonic-gate 	t->t_stime = lbolt;		/* time stamp for the swapper */
1913*7c478bd9Sstevel@tonic-gate 
1914*7c478bd9Sstevel@tonic-gate 	if (tspp->ts_flags & TSKPRI) {
1915*7c478bd9Sstevel@tonic-gate 		tspp->ts_flags &= ~TSBACKQ;
1916*7c478bd9Sstevel@tonic-gate 		if (tspp->ts_flags & TSIASET)
1917*7c478bd9Sstevel@tonic-gate 			setfrontdq(t);
1918*7c478bd9Sstevel@tonic-gate 		else
1919*7c478bd9Sstevel@tonic-gate 			setbackdq(t);
1920*7c478bd9Sstevel@tonic-gate 	} else if (t->t_kpri_req) {
1921*7c478bd9Sstevel@tonic-gate 		/*
1922*7c478bd9Sstevel@tonic-gate 		 * Give thread a priority boost if we were asked.
1923*7c478bd9Sstevel@tonic-gate 		 */
1924*7c478bd9Sstevel@tonic-gate 		tspp->ts_flags |= TSKPRI;
1925*7c478bd9Sstevel@tonic-gate 		THREAD_CHANGE_PRI(t, ts_kmdpris[0]);
1926*7c478bd9Sstevel@tonic-gate 		setbackdq(t);
1927*7c478bd9Sstevel@tonic-gate 		t->t_trapret = 1;	/* so that ts_trapret will run */
1928*7c478bd9Sstevel@tonic-gate 		aston(t);
1929*7c478bd9Sstevel@tonic-gate 	} else {
1930*7c478bd9Sstevel@tonic-gate 		if (tspp->ts_dispwait > ts_dptbl[tspp->ts_umdpri].ts_maxwait) {
1931*7c478bd9Sstevel@tonic-gate 			tspp->ts_cpupri = ts_dptbl[tspp->ts_cpupri].ts_slpret;
1932*7c478bd9Sstevel@tonic-gate 			TS_NEWUMDPRI(tspp);
1933*7c478bd9Sstevel@tonic-gate 			tspp->ts_timeleft =
1934*7c478bd9Sstevel@tonic-gate 			    ts_dptbl[tspp->ts_cpupri].ts_quantum;
1935*7c478bd9Sstevel@tonic-gate 			tspp->ts_dispwait = 0;
1936*7c478bd9Sstevel@tonic-gate 			THREAD_CHANGE_PRI(t,
1937*7c478bd9Sstevel@tonic-gate 			    ts_dptbl[tspp->ts_umdpri].ts_globpri);
1938*7c478bd9Sstevel@tonic-gate 			ASSERT(t->t_pri >= 0 && t->t_pri <= ts_maxglobpri);
1939*7c478bd9Sstevel@tonic-gate 		}
1940*7c478bd9Sstevel@tonic-gate 
1941*7c478bd9Sstevel@tonic-gate 		tspp->ts_flags &= ~TSBACKQ;
1942*7c478bd9Sstevel@tonic-gate 
1943*7c478bd9Sstevel@tonic-gate 		if (tspp->ts_flags & TSIA) {
1944*7c478bd9Sstevel@tonic-gate 			if (tspp->ts_flags & TSIASET)
1945*7c478bd9Sstevel@tonic-gate 				setfrontdq(t);
1946*7c478bd9Sstevel@tonic-gate 			else
1947*7c478bd9Sstevel@tonic-gate 				setbackdq(t);
1948*7c478bd9Sstevel@tonic-gate 		} else {
1949*7c478bd9Sstevel@tonic-gate 			if (t->t_disp_time != lbolt)
1950*7c478bd9Sstevel@tonic-gate 				setbackdq(t);
1951*7c478bd9Sstevel@tonic-gate 			else
1952*7c478bd9Sstevel@tonic-gate 				setfrontdq(t);
1953*7c478bd9Sstevel@tonic-gate 		}
1954*7c478bd9Sstevel@tonic-gate 	}
1955*7c478bd9Sstevel@tonic-gate }
1956*7c478bd9Sstevel@tonic-gate 
1957*7c478bd9Sstevel@tonic-gate 
1958*7c478bd9Sstevel@tonic-gate /*
1959*7c478bd9Sstevel@tonic-gate  * When a thread yields, put it on the back of the run queue.
1960*7c478bd9Sstevel@tonic-gate  */
1961*7c478bd9Sstevel@tonic-gate static void
1962*7c478bd9Sstevel@tonic-gate ts_yield(kthread_t *t)
1963*7c478bd9Sstevel@tonic-gate {
1964*7c478bd9Sstevel@tonic-gate 	tsproc_t	*tspp = (tsproc_t *)(t->t_cldata);
1965*7c478bd9Sstevel@tonic-gate 
1966*7c478bd9Sstevel@tonic-gate 	ASSERT(t == curthread);
1967*7c478bd9Sstevel@tonic-gate 	ASSERT(THREAD_LOCK_HELD(t));
1968*7c478bd9Sstevel@tonic-gate 
1969*7c478bd9Sstevel@tonic-gate 	/*
1970*7c478bd9Sstevel@tonic-gate 	 * Clear the preemption control "yield" bit since the user is
1971*7c478bd9Sstevel@tonic-gate 	 * doing a yield.
1972*7c478bd9Sstevel@tonic-gate 	 */
1973*7c478bd9Sstevel@tonic-gate 	if (t->t_schedctl)
1974*7c478bd9Sstevel@tonic-gate 		schedctl_set_yield(t, 0);
1975*7c478bd9Sstevel@tonic-gate 	if (tspp->ts_timeleft <= 0) {
1976*7c478bd9Sstevel@tonic-gate 		/*
1977*7c478bd9Sstevel@tonic-gate 		 * Time slice was artificially extended to avoid
1978*7c478bd9Sstevel@tonic-gate 		 * preemption, so pretend we're preempting it now.
1979*7c478bd9Sstevel@tonic-gate 		 */
1980*7c478bd9Sstevel@tonic-gate 		DTRACE_SCHED1(schedctl__yield, int, -tspp->ts_timeleft);
1981*7c478bd9Sstevel@tonic-gate 		tspp->ts_cpupri = ts_dptbl[tspp->ts_cpupri].ts_tqexp;
1982*7c478bd9Sstevel@tonic-gate 		TS_NEWUMDPRI(tspp);
1983*7c478bd9Sstevel@tonic-gate 		tspp->ts_timeleft = ts_dptbl[tspp->ts_cpupri].ts_quantum;
1984*7c478bd9Sstevel@tonic-gate 		tspp->ts_dispwait = 0;
1985*7c478bd9Sstevel@tonic-gate 		THREAD_CHANGE_PRI(t, ts_dptbl[tspp->ts_umdpri].ts_globpri);
1986*7c478bd9Sstevel@tonic-gate 		ASSERT(t->t_pri >= 0 && t->t_pri <= ts_maxglobpri);
1987*7c478bd9Sstevel@tonic-gate 	}
1988*7c478bd9Sstevel@tonic-gate 	tspp->ts_flags &= ~TSBACKQ;
1989*7c478bd9Sstevel@tonic-gate 	setbackdq(t);
1990*7c478bd9Sstevel@tonic-gate }
1991*7c478bd9Sstevel@tonic-gate 
1992*7c478bd9Sstevel@tonic-gate 
1993*7c478bd9Sstevel@tonic-gate /*
1994*7c478bd9Sstevel@tonic-gate  * Increment the nice value of the specified thread by incr and
1995*7c478bd9Sstevel@tonic-gate  * return the new value in *retvalp.
1996*7c478bd9Sstevel@tonic-gate  */
1997*7c478bd9Sstevel@tonic-gate static int
1998*7c478bd9Sstevel@tonic-gate ts_donice(kthread_t *t, cred_t *cr, int incr, int *retvalp)
1999*7c478bd9Sstevel@tonic-gate {
2000*7c478bd9Sstevel@tonic-gate 	int		newnice;
2001*7c478bd9Sstevel@tonic-gate 	tsproc_t	*tspp = (tsproc_t *)(t->t_cldata);
2002*7c478bd9Sstevel@tonic-gate 	tsparms_t	tsparms;
2003*7c478bd9Sstevel@tonic-gate 
2004*7c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&(ttoproc(t))->p_lock));
2005*7c478bd9Sstevel@tonic-gate 
2006*7c478bd9Sstevel@tonic-gate 	/* If there's no change to priority, just return current setting */
2007*7c478bd9Sstevel@tonic-gate 	if (incr == 0) {
2008*7c478bd9Sstevel@tonic-gate 		if (retvalp) {
2009*7c478bd9Sstevel@tonic-gate 			*retvalp = tspp->ts_nice - NZERO;
2010*7c478bd9Sstevel@tonic-gate 		}
2011*7c478bd9Sstevel@tonic-gate 		return (0);
2012*7c478bd9Sstevel@tonic-gate 	}
2013*7c478bd9Sstevel@tonic-gate 
2014*7c478bd9Sstevel@tonic-gate 	if ((incr < 0 || incr > 2 * NZERO) &&
2015*7c478bd9Sstevel@tonic-gate 	    secpolicy_setpriority(cr) != 0)
2016*7c478bd9Sstevel@tonic-gate 		return (EPERM);
2017*7c478bd9Sstevel@tonic-gate 
2018*7c478bd9Sstevel@tonic-gate 	/*
2019*7c478bd9Sstevel@tonic-gate 	 * Specifying a nice increment greater than the upper limit of
2020*7c478bd9Sstevel@tonic-gate 	 * 2 * NZERO - 1 will result in the thread's nice value being
2021*7c478bd9Sstevel@tonic-gate 	 * set to the upper limit.  We check for this before computing
2022*7c478bd9Sstevel@tonic-gate 	 * the new value because otherwise we could get overflow
2023*7c478bd9Sstevel@tonic-gate 	 * if a privileged process specified some ridiculous increment.
2024*7c478bd9Sstevel@tonic-gate 	 */
2025*7c478bd9Sstevel@tonic-gate 	if (incr > 2 * NZERO - 1)
2026*7c478bd9Sstevel@tonic-gate 		incr = 2 * NZERO - 1;
2027*7c478bd9Sstevel@tonic-gate 
2028*7c478bd9Sstevel@tonic-gate 	newnice = tspp->ts_nice + incr;
2029*7c478bd9Sstevel@tonic-gate 	if (newnice >= 2 * NZERO)
2030*7c478bd9Sstevel@tonic-gate 		newnice = 2 * NZERO - 1;
2031*7c478bd9Sstevel@tonic-gate 	else if (newnice < 0)
2032*7c478bd9Sstevel@tonic-gate 		newnice = 0;
2033*7c478bd9Sstevel@tonic-gate 
2034*7c478bd9Sstevel@tonic-gate 	tsparms.ts_uprilim = tsparms.ts_upri =
2035*7c478bd9Sstevel@tonic-gate 		-((newnice - NZERO) * ts_maxupri) / NZERO;
2036*7c478bd9Sstevel@tonic-gate 	/*
2037*7c478bd9Sstevel@tonic-gate 	 * Reset the uprilim and upri values of the thread.
2038*7c478bd9Sstevel@tonic-gate 	 * Call ts_parmsset even if thread is interactive since we're
2039*7c478bd9Sstevel@tonic-gate 	 * not changing mode.
2040*7c478bd9Sstevel@tonic-gate 	 */
2041*7c478bd9Sstevel@tonic-gate 	(void) ts_parmsset(t, (void *)&tsparms, (id_t)0, (cred_t *)NULL);
2042*7c478bd9Sstevel@tonic-gate 
2043*7c478bd9Sstevel@tonic-gate 	/*
2044*7c478bd9Sstevel@tonic-gate 	 * Although ts_parmsset already reset ts_nice it may
2045*7c478bd9Sstevel@tonic-gate 	 * not have been set to precisely the value calculated above
2046*7c478bd9Sstevel@tonic-gate 	 * because ts_parmsset determines the nice value from the
2047*7c478bd9Sstevel@tonic-gate 	 * user priority and we may have truncated during the integer
2048*7c478bd9Sstevel@tonic-gate 	 * conversion from nice value to user priority and back.
2049*7c478bd9Sstevel@tonic-gate 	 * We reset ts_nice to the value we calculated above.
2050*7c478bd9Sstevel@tonic-gate 	 */
2051*7c478bd9Sstevel@tonic-gate 	tspp->ts_nice = (char)newnice;
2052*7c478bd9Sstevel@tonic-gate 
2053*7c478bd9Sstevel@tonic-gate 	if (retvalp)
2054*7c478bd9Sstevel@tonic-gate 		*retvalp = newnice - NZERO;
2055*7c478bd9Sstevel@tonic-gate 	return (0);
2056*7c478bd9Sstevel@tonic-gate }
2057*7c478bd9Sstevel@tonic-gate 
2058*7c478bd9Sstevel@tonic-gate 
2059*7c478bd9Sstevel@tonic-gate /*
2060*7c478bd9Sstevel@tonic-gate  * ia_set_process_group marks foreground processes as interactive
2061*7c478bd9Sstevel@tonic-gate  * and background processes as non-interactive iff the session
2062*7c478bd9Sstevel@tonic-gate  * leader is interactive.  This routine is called from two places:
2063*7c478bd9Sstevel@tonic-gate  *	strioctl:SPGRP when a new process group gets
2064*7c478bd9Sstevel@tonic-gate  * 		control of the tty.
2065*7c478bd9Sstevel@tonic-gate  *	ia_parmsset-when the process in question is a session leader.
2066*7c478bd9Sstevel@tonic-gate  * ia_set_process_group assumes that pidlock is held by the caller,
2067*7c478bd9Sstevel@tonic-gate  * either strioctl or priocntlsys.  If the caller is priocntlsys
2068*7c478bd9Sstevel@tonic-gate  * (via ia_parmsset) then the p_lock of the session leader is held
2069*7c478bd9Sstevel@tonic-gate  * and the code needs to be careful about acquiring other p_locks.
2070*7c478bd9Sstevel@tonic-gate  */
2071*7c478bd9Sstevel@tonic-gate static void
2072*7c478bd9Sstevel@tonic-gate ia_set_process_group(pid_t sid, pid_t bg_pgid, pid_t fg_pgid)
2073*7c478bd9Sstevel@tonic-gate {
2074*7c478bd9Sstevel@tonic-gate 	proc_t 		*leader, *fg, *bg;
2075*7c478bd9Sstevel@tonic-gate 	tsproc_t	*tspp;
2076*7c478bd9Sstevel@tonic-gate 	kthread_t	*tx;
2077*7c478bd9Sstevel@tonic-gate 	int		plocked = 0;
2078*7c478bd9Sstevel@tonic-gate 
2079*7c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&pidlock));
2080*7c478bd9Sstevel@tonic-gate 
2081*7c478bd9Sstevel@tonic-gate 	/*
2082*7c478bd9Sstevel@tonic-gate 	 * see if the session leader is interactive AND
2083*7c478bd9Sstevel@tonic-gate 	 * if it is currently "on" AND controlling a tty
2084*7c478bd9Sstevel@tonic-gate 	 * iff it is then make the processes in the foreground
2085*7c478bd9Sstevel@tonic-gate 	 * group interactive and the processes in the background
2086*7c478bd9Sstevel@tonic-gate 	 * group non-interactive.
2087*7c478bd9Sstevel@tonic-gate 	 */
2088*7c478bd9Sstevel@tonic-gate 	if ((leader = (proc_t *)prfind(sid)) == NULL) {
2089*7c478bd9Sstevel@tonic-gate 		return;
2090*7c478bd9Sstevel@tonic-gate 	}
2091*7c478bd9Sstevel@tonic-gate 	if (leader->p_stat == SIDL) {
2092*7c478bd9Sstevel@tonic-gate 		return;
2093*7c478bd9Sstevel@tonic-gate 	}
2094*7c478bd9Sstevel@tonic-gate 	if ((tx = proctot(leader)) == NULL) {
2095*7c478bd9Sstevel@tonic-gate 		return;
2096*7c478bd9Sstevel@tonic-gate 	}
2097*7c478bd9Sstevel@tonic-gate 	/*
2098*7c478bd9Sstevel@tonic-gate 	 * XXX do all the threads in the leader
2099*7c478bd9Sstevel@tonic-gate 	 */
2100*7c478bd9Sstevel@tonic-gate 	if (tx->t_cid != ia_cid) {
2101*7c478bd9Sstevel@tonic-gate 		return;
2102*7c478bd9Sstevel@tonic-gate 	}
2103*7c478bd9Sstevel@tonic-gate 	tspp = tx->t_cldata;
2104*7c478bd9Sstevel@tonic-gate 	/*
2105*7c478bd9Sstevel@tonic-gate 	 * session leaders that are not interactive need not have
2106*7c478bd9Sstevel@tonic-gate 	 * any processing done for them.  They are typically shells
2107*7c478bd9Sstevel@tonic-gate 	 * that do not have focus and are changing the process group
2108*7c478bd9Sstevel@tonic-gate 	 * attatched to the tty, e.g. a process that is exiting
2109*7c478bd9Sstevel@tonic-gate 	 */
2110*7c478bd9Sstevel@tonic-gate 	TTY_HOLD(leader->p_sessp);
2111*7c478bd9Sstevel@tonic-gate 	if (!(tspp->ts_flags & TSIASET) ||
2112*7c478bd9Sstevel@tonic-gate 	    (leader->p_sessp->s_vp == NULL) ||
2113*7c478bd9Sstevel@tonic-gate 	    (leader->p_sessp->s_vp->v_stream == NULL)) {
2114*7c478bd9Sstevel@tonic-gate 		TTY_RELE(leader->p_sessp);
2115*7c478bd9Sstevel@tonic-gate 		return;
2116*7c478bd9Sstevel@tonic-gate 	}
2117*7c478bd9Sstevel@tonic-gate 	TTY_RELE(leader->p_sessp);
2118*7c478bd9Sstevel@tonic-gate 
2119*7c478bd9Sstevel@tonic-gate 	/*
2120*7c478bd9Sstevel@tonic-gate 	 * If we're already holding the leader's p_lock, we should use
2121*7c478bd9Sstevel@tonic-gate 	 * mutex_tryenter instead of mutex_enter to avoid deadlocks from
2122*7c478bd9Sstevel@tonic-gate 	 * lock ordering violations.
2123*7c478bd9Sstevel@tonic-gate 	 */
2124*7c478bd9Sstevel@tonic-gate 	if (mutex_owned(&leader->p_lock))
2125*7c478bd9Sstevel@tonic-gate 		plocked = 1;
2126*7c478bd9Sstevel@tonic-gate 
2127*7c478bd9Sstevel@tonic-gate 	if (fg_pgid == 0)
2128*7c478bd9Sstevel@tonic-gate 		goto skip;
2129*7c478bd9Sstevel@tonic-gate 	/*
2130*7c478bd9Sstevel@tonic-gate 	 * now look for all processes in the foreground group and
2131*7c478bd9Sstevel@tonic-gate 	 * make them interactive
2132*7c478bd9Sstevel@tonic-gate 	 */
2133*7c478bd9Sstevel@tonic-gate 	for (fg = (proc_t *)pgfind(fg_pgid); fg != NULL; fg = fg->p_pglink) {
2134*7c478bd9Sstevel@tonic-gate 		/*
2135*7c478bd9Sstevel@tonic-gate 		 * if the process is SIDL it's begin forked, ignore it
2136*7c478bd9Sstevel@tonic-gate 		 */
2137*7c478bd9Sstevel@tonic-gate 		if (fg->p_stat == SIDL) {
2138*7c478bd9Sstevel@tonic-gate 			continue;
2139*7c478bd9Sstevel@tonic-gate 		}
2140*7c478bd9Sstevel@tonic-gate 		/*
2141*7c478bd9Sstevel@tonic-gate 		 * sesssion leaders must be turned on/off explicitly
2142*7c478bd9Sstevel@tonic-gate 		 * not implicitly as happens to other members of
2143*7c478bd9Sstevel@tonic-gate 		 * the process group.
2144*7c478bd9Sstevel@tonic-gate 		 */
2145*7c478bd9Sstevel@tonic-gate 		if (fg->p_pid  == fg->p_sessp->s_sid) {
2146*7c478bd9Sstevel@tonic-gate 			continue;
2147*7c478bd9Sstevel@tonic-gate 		}
2148*7c478bd9Sstevel@tonic-gate 
2149*7c478bd9Sstevel@tonic-gate 		TRACE_1(TR_FAC_IA, TR_GROUP_ON,
2150*7c478bd9Sstevel@tonic-gate 		    "group on:proc %p", fg);
2151*7c478bd9Sstevel@tonic-gate 
2152*7c478bd9Sstevel@tonic-gate 		if (plocked) {
2153*7c478bd9Sstevel@tonic-gate 			if (mutex_tryenter(&fg->p_lock) == 0)
2154*7c478bd9Sstevel@tonic-gate 				continue;
2155*7c478bd9Sstevel@tonic-gate 		} else {
2156*7c478bd9Sstevel@tonic-gate 			mutex_enter(&fg->p_lock);
2157*7c478bd9Sstevel@tonic-gate 		}
2158*7c478bd9Sstevel@tonic-gate 
2159*7c478bd9Sstevel@tonic-gate 		if ((tx = proctot(fg)) == NULL) {
2160*7c478bd9Sstevel@tonic-gate 			mutex_exit(&fg->p_lock);
2161*7c478bd9Sstevel@tonic-gate 			continue;
2162*7c478bd9Sstevel@tonic-gate 		}
2163*7c478bd9Sstevel@tonic-gate 		do {
2164*7c478bd9Sstevel@tonic-gate 			thread_lock(tx);
2165*7c478bd9Sstevel@tonic-gate 			/*
2166*7c478bd9Sstevel@tonic-gate 			 * if this thread is not interactive continue
2167*7c478bd9Sstevel@tonic-gate 			 */
2168*7c478bd9Sstevel@tonic-gate 			if (tx->t_cid != ia_cid) {
2169*7c478bd9Sstevel@tonic-gate 				thread_unlock(tx);
2170*7c478bd9Sstevel@tonic-gate 				continue;
2171*7c478bd9Sstevel@tonic-gate 			}
2172*7c478bd9Sstevel@tonic-gate 			tspp = tx->t_cldata;
2173*7c478bd9Sstevel@tonic-gate 			tspp->ts_flags |= TSIASET;
2174*7c478bd9Sstevel@tonic-gate 			tspp->ts_boost = ia_boost;
2175*7c478bd9Sstevel@tonic-gate 			TS_NEWUMDPRI(tspp);
2176*7c478bd9Sstevel@tonic-gate 			if ((tspp->ts_flags & TSKPRI) != 0) {
2177*7c478bd9Sstevel@tonic-gate 				thread_unlock(tx);
2178*7c478bd9Sstevel@tonic-gate 				continue;
2179*7c478bd9Sstevel@tonic-gate 			}
2180*7c478bd9Sstevel@tonic-gate 			tspp->ts_dispwait = 0;
2181*7c478bd9Sstevel@tonic-gate 			ts_change_priority(tx, tspp);
2182*7c478bd9Sstevel@tonic-gate 			thread_unlock(tx);
2183*7c478bd9Sstevel@tonic-gate 		} while ((tx = tx->t_forw) != fg->p_tlist);
2184*7c478bd9Sstevel@tonic-gate 		mutex_exit(&fg->p_lock);
2185*7c478bd9Sstevel@tonic-gate 	}
2186*7c478bd9Sstevel@tonic-gate skip:
2187*7c478bd9Sstevel@tonic-gate 	if (bg_pgid == 0)
2188*7c478bd9Sstevel@tonic-gate 		return;
2189*7c478bd9Sstevel@tonic-gate 	for (bg = (proc_t *)pgfind(bg_pgid); bg != NULL; bg = bg->p_pglink) {
2190*7c478bd9Sstevel@tonic-gate 		if (bg->p_stat == SIDL) {
2191*7c478bd9Sstevel@tonic-gate 			continue;
2192*7c478bd9Sstevel@tonic-gate 		}
2193*7c478bd9Sstevel@tonic-gate 		/*
2194*7c478bd9Sstevel@tonic-gate 		 * sesssion leaders must be turned off explicitly
2195*7c478bd9Sstevel@tonic-gate 		 * not implicitly as happens to other members of
2196*7c478bd9Sstevel@tonic-gate 		 * the process group.
2197*7c478bd9Sstevel@tonic-gate 		 */
2198*7c478bd9Sstevel@tonic-gate 		if (bg->p_pid == bg->p_sessp->s_sid) {
2199*7c478bd9Sstevel@tonic-gate 			continue;
2200*7c478bd9Sstevel@tonic-gate 		}
2201*7c478bd9Sstevel@tonic-gate 
2202*7c478bd9Sstevel@tonic-gate 		TRACE_1(TR_FAC_IA, TR_GROUP_OFF,
2203*7c478bd9Sstevel@tonic-gate 		    "group off:proc %p", bg);
2204*7c478bd9Sstevel@tonic-gate 
2205*7c478bd9Sstevel@tonic-gate 		if (plocked) {
2206*7c478bd9Sstevel@tonic-gate 			if (mutex_tryenter(&bg->p_lock) == 0)
2207*7c478bd9Sstevel@tonic-gate 				continue;
2208*7c478bd9Sstevel@tonic-gate 		} else {
2209*7c478bd9Sstevel@tonic-gate 			mutex_enter(&bg->p_lock);
2210*7c478bd9Sstevel@tonic-gate 		}
2211*7c478bd9Sstevel@tonic-gate 
2212*7c478bd9Sstevel@tonic-gate 		if ((tx = proctot(bg)) == NULL) {
2213*7c478bd9Sstevel@tonic-gate 			mutex_exit(&bg->p_lock);
2214*7c478bd9Sstevel@tonic-gate 			continue;
2215*7c478bd9Sstevel@tonic-gate 		}
2216*7c478bd9Sstevel@tonic-gate 		do {
2217*7c478bd9Sstevel@tonic-gate 			thread_lock(tx);
2218*7c478bd9Sstevel@tonic-gate 			/*
2219*7c478bd9Sstevel@tonic-gate 			 * if this thread is not interactive continue
2220*7c478bd9Sstevel@tonic-gate 			 */
2221*7c478bd9Sstevel@tonic-gate 			if (tx->t_cid != ia_cid) {
2222*7c478bd9Sstevel@tonic-gate 				thread_unlock(tx);
2223*7c478bd9Sstevel@tonic-gate 				continue;
2224*7c478bd9Sstevel@tonic-gate 			}
2225*7c478bd9Sstevel@tonic-gate 			tspp = tx->t_cldata;
2226*7c478bd9Sstevel@tonic-gate 			tspp->ts_flags &= ~TSIASET;
2227*7c478bd9Sstevel@tonic-gate 			tspp->ts_boost = -ia_boost;
2228*7c478bd9Sstevel@tonic-gate 			TS_NEWUMDPRI(tspp);
2229*7c478bd9Sstevel@tonic-gate 			if ((tspp->ts_flags & TSKPRI) != 0) {
2230*7c478bd9Sstevel@tonic-gate 				thread_unlock(tx);
2231*7c478bd9Sstevel@tonic-gate 				continue;
2232*7c478bd9Sstevel@tonic-gate 			}
2233*7c478bd9Sstevel@tonic-gate 
2234*7c478bd9Sstevel@tonic-gate 			tspp->ts_dispwait = 0;
2235*7c478bd9Sstevel@tonic-gate 			ts_change_priority(tx, tspp);
2236*7c478bd9Sstevel@tonic-gate 			thread_unlock(tx);
2237*7c478bd9Sstevel@tonic-gate 		} while ((tx = tx->t_forw) != bg->p_tlist);
2238*7c478bd9Sstevel@tonic-gate 		mutex_exit(&bg->p_lock);
2239*7c478bd9Sstevel@tonic-gate 	}
2240*7c478bd9Sstevel@tonic-gate }
2241*7c478bd9Sstevel@tonic-gate 
2242*7c478bd9Sstevel@tonic-gate 
2243*7c478bd9Sstevel@tonic-gate static void
2244*7c478bd9Sstevel@tonic-gate ts_change_priority(kthread_t *t, tsproc_t *tspp)
2245*7c478bd9Sstevel@tonic-gate {
2246*7c478bd9Sstevel@tonic-gate 	pri_t	new_pri;
2247*7c478bd9Sstevel@tonic-gate 
2248*7c478bd9Sstevel@tonic-gate 	ASSERT(THREAD_LOCK_HELD(t));
2249*7c478bd9Sstevel@tonic-gate 	new_pri = ts_dptbl[tspp->ts_umdpri].ts_globpri;
2250*7c478bd9Sstevel@tonic-gate 	ASSERT(new_pri >= 0 && new_pri <= ts_maxglobpri);
2251*7c478bd9Sstevel@tonic-gate 	if (t == curthread || t->t_state == TS_ONPROC) {
2252*7c478bd9Sstevel@tonic-gate 		/* curthread is always onproc */
2253*7c478bd9Sstevel@tonic-gate 		cpu_t	*cp = t->t_disp_queue->disp_cpu;
2254*7c478bd9Sstevel@tonic-gate 		THREAD_CHANGE_PRI(t, new_pri);
2255*7c478bd9Sstevel@tonic-gate 		if (t == cp->cpu_dispthread)
2256*7c478bd9Sstevel@tonic-gate 			cp->cpu_dispatch_pri = DISP_PRIO(t);
2257*7c478bd9Sstevel@tonic-gate 		if (DISP_MUST_SURRENDER(t)) {
2258*7c478bd9Sstevel@tonic-gate 			tspp->ts_flags |= TSBACKQ;
2259*7c478bd9Sstevel@tonic-gate 			cpu_surrender(t);
2260*7c478bd9Sstevel@tonic-gate 		} else {
2261*7c478bd9Sstevel@tonic-gate 			tspp->ts_timeleft =
2262*7c478bd9Sstevel@tonic-gate 			    ts_dptbl[tspp->ts_cpupri].ts_quantum;
2263*7c478bd9Sstevel@tonic-gate 		}
2264*7c478bd9Sstevel@tonic-gate 	} else {
2265*7c478bd9Sstevel@tonic-gate 		int	frontq;
2266*7c478bd9Sstevel@tonic-gate 
2267*7c478bd9Sstevel@tonic-gate 		frontq = (tspp->ts_flags & TSIASET) != 0;
2268*7c478bd9Sstevel@tonic-gate 		/*
2269*7c478bd9Sstevel@tonic-gate 		 * When the priority of a thread is changed,
2270*7c478bd9Sstevel@tonic-gate 		 * it may be necessary to adjust its position
2271*7c478bd9Sstevel@tonic-gate 		 * on a sleep queue or dispatch queue.
2272*7c478bd9Sstevel@tonic-gate 		 * The function thread_change_pri accomplishes
2273*7c478bd9Sstevel@tonic-gate 		 * this.
2274*7c478bd9Sstevel@tonic-gate 		 */
2275*7c478bd9Sstevel@tonic-gate 		if (thread_change_pri(t, new_pri, frontq)) {
2276*7c478bd9Sstevel@tonic-gate 			/*
2277*7c478bd9Sstevel@tonic-gate 			 * The thread was on a run queue. Reset
2278*7c478bd9Sstevel@tonic-gate 			 * its CPU timeleft from the quantum
2279*7c478bd9Sstevel@tonic-gate 			 * associated with the new priority.
2280*7c478bd9Sstevel@tonic-gate 			 */
2281*7c478bd9Sstevel@tonic-gate 			tspp->ts_timeleft =
2282*7c478bd9Sstevel@tonic-gate 			    ts_dptbl[tspp->ts_cpupri].ts_quantum;
2283*7c478bd9Sstevel@tonic-gate 		} else {
2284*7c478bd9Sstevel@tonic-gate 			tspp->ts_flags |= TSBACKQ;
2285*7c478bd9Sstevel@tonic-gate 		}
2286*7c478bd9Sstevel@tonic-gate 	}
2287*7c478bd9Sstevel@tonic-gate }
2288*7c478bd9Sstevel@tonic-gate 
2289*7c478bd9Sstevel@tonic-gate static int
2290*7c478bd9Sstevel@tonic-gate ts_alloc(void **p, int flag)
2291*7c478bd9Sstevel@tonic-gate {
2292*7c478bd9Sstevel@tonic-gate 	void *bufp;
2293*7c478bd9Sstevel@tonic-gate 	bufp = kmem_alloc(sizeof (tsproc_t), flag);
2294*7c478bd9Sstevel@tonic-gate 	if (bufp == NULL) {
2295*7c478bd9Sstevel@tonic-gate 		return (ENOMEM);
2296*7c478bd9Sstevel@tonic-gate 	} else {
2297*7c478bd9Sstevel@tonic-gate 		*p = bufp;
2298*7c478bd9Sstevel@tonic-gate 		return (0);
2299*7c478bd9Sstevel@tonic-gate 	}
2300*7c478bd9Sstevel@tonic-gate }
2301*7c478bd9Sstevel@tonic-gate 
2302*7c478bd9Sstevel@tonic-gate static void
2303*7c478bd9Sstevel@tonic-gate ts_free(void *bufp)
2304*7c478bd9Sstevel@tonic-gate {
2305*7c478bd9Sstevel@tonic-gate 	if (bufp)
2306*7c478bd9Sstevel@tonic-gate 		kmem_free(bufp, sizeof (tsproc_t));
2307*7c478bd9Sstevel@tonic-gate }
2308