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