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