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