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