xref: /titanic_41/usr/src/uts/common/disp/ts.c (revision f127cb91970601c2695eead0ee127f1cac48bb7a)
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 		 * CPU caps are not enforced on exiting processes - it is
1325 		 * usually desirable to exit as soon as possible to free
1326 		 * resources.
1327 		 */
1328 		thread_lock(t);
1329 		tspp = (tsproc_t *)t->t_cldata;
1330 		(void) cpucaps_charge(t, &tspp->ts_caps, CPUCAPS_CHARGE_ONLY);
1331 		thread_unlock(t);
1332 	}
1333 }
1334 
1335 /*
1336  * Return the global scheduling priority that would be assigned
1337  * to a thread entering the time-sharing class with the ts_upri.
1338  */
1339 static pri_t
1340 ts_globpri(kthread_t *t)
1341 {
1342 	tsproc_t *tspp;
1343 	pri_t	tspri;
1344 
1345 	ASSERT(MUTEX_HELD(&ttoproc(t)->p_lock));
1346 	tspp = (tsproc_t *)t->t_cldata;
1347 	tspri = tsmedumdpri + tspp->ts_upri;
1348 	if (tspri > ts_maxumdpri)
1349 		tspri = ts_maxumdpri;
1350 	else if (tspri < 0)
1351 		tspri = 0;
1352 	return (ts_dptbl[tspri].ts_globpri);
1353 }
1354 
1355 /*
1356  * Arrange for thread to be placed in appropriate location
1357  * on dispatcher queue.
1358  *
1359  * This is called with the current thread in TS_ONPROC and locked.
1360  */
1361 static void
1362 ts_preempt(kthread_t *t)
1363 {
1364 	tsproc_t	*tspp = (tsproc_t *)(t->t_cldata);
1365 	klwp_t		*lwp = curthread->t_lwp;
1366 	pri_t		oldpri = t->t_pri;
1367 
1368 	ASSERT(t == curthread);
1369 	ASSERT(THREAD_LOCK_HELD(curthread));
1370 
1371 	/*
1372 	 * If preempted in the kernel, make sure the thread has
1373 	 * a kernel priority if needed.
1374 	 */
1375 	if (!(tspp->ts_flags & TSKPRI) && lwp != NULL && t->t_kpri_req) {
1376 		tspp->ts_flags |= TSKPRI;
1377 		THREAD_CHANGE_PRI(t, ts_kmdpris[0]);
1378 		ASSERT(t->t_pri >= 0 && t->t_pri <= ts_maxglobpri);
1379 		t->t_trapret = 1;		/* so ts_trapret will run */
1380 		aston(t);
1381 	}
1382 
1383 	/*
1384 	 * This thread may be placed on wait queue by CPU Caps. In this case we
1385 	 * do not need to do anything until it is removed from the wait queue.
1386 	 * Do not enforce CPU caps on threads running at a kernel priority
1387 	 */
1388 	if (CPUCAPS_ON()) {
1389 		(void) cpucaps_charge(t, &tspp->ts_caps,
1390 		    CPUCAPS_CHARGE_ENFORCE);
1391 		if (!(tspp->ts_flags & TSKPRI) && CPUCAPS_ENFORCE(t))
1392 			return;
1393 	}
1394 
1395 	/*
1396 	 * If thread got preempted in the user-land then we know
1397 	 * it isn't holding any locks.  Mark it as swappable.
1398 	 */
1399 	ASSERT(t->t_schedflag & TS_DONT_SWAP);
1400 	if (lwp != NULL && lwp->lwp_state == LWP_USER)
1401 		t->t_schedflag &= ~TS_DONT_SWAP;
1402 
1403 	/*
1404 	 * Check to see if we're doing "preemption control" here.  If
1405 	 * we are, and if the user has requested that this thread not
1406 	 * be preempted, and if preemptions haven't been put off for
1407 	 * too long, let the preemption happen here but try to make
1408 	 * sure the thread is rescheduled as soon as possible.  We do
1409 	 * this by putting it on the front of the highest priority run
1410 	 * queue in the TS class.  If the preemption has been put off
1411 	 * for too long, clear the "nopreempt" bit and let the thread
1412 	 * be preempted.
1413 	 */
1414 	if (t->t_schedctl && schedctl_get_nopreempt(t)) {
1415 		if (tspp->ts_timeleft > -SC_MAX_TICKS) {
1416 			DTRACE_SCHED1(schedctl__nopreempt, kthread_t *, t);
1417 			if (!(tspp->ts_flags & TSKPRI)) {
1418 				/*
1419 				 * If not already remembered, remember current
1420 				 * priority for restoration in ts_yield().
1421 				 */
1422 				if (!(tspp->ts_flags & TSRESTORE)) {
1423 					tspp->ts_scpri = t->t_pri;
1424 					tspp->ts_flags |= TSRESTORE;
1425 				}
1426 				THREAD_CHANGE_PRI(t, ts_maxumdpri);
1427 				t->t_schedflag |= TS_DONT_SWAP;
1428 			}
1429 			schedctl_set_yield(t, 1);
1430 			setfrontdq(t);
1431 			goto done;
1432 		} else {
1433 			if (tspp->ts_flags & TSRESTORE) {
1434 				THREAD_CHANGE_PRI(t, tspp->ts_scpri);
1435 				tspp->ts_flags &= ~TSRESTORE;
1436 			}
1437 			schedctl_set_nopreempt(t, 0);
1438 			DTRACE_SCHED1(schedctl__preempt, kthread_t *, t);
1439 			TNF_PROBE_2(schedctl_preempt, "schedctl TS ts_preempt",
1440 			    /* CSTYLED */, tnf_pid, pid, ttoproc(t)->p_pid,
1441 			    tnf_lwpid, lwpid, t->t_tid);
1442 			/*
1443 			 * Fall through and be preempted below.
1444 			 */
1445 		}
1446 	}
1447 
1448 	if ((tspp->ts_flags & (TSBACKQ|TSKPRI)) == TSBACKQ) {
1449 		tspp->ts_timeleft = ts_dptbl[tspp->ts_cpupri].ts_quantum;
1450 		tspp->ts_dispwait = 0;
1451 		tspp->ts_flags &= ~TSBACKQ;
1452 		setbackdq(t);
1453 	} else if ((tspp->ts_flags & (TSBACKQ|TSKPRI)) == (TSBACKQ|TSKPRI)) {
1454 		tspp->ts_flags &= ~TSBACKQ;
1455 		setbackdq(t);
1456 	} else {
1457 		setfrontdq(t);
1458 	}
1459 
1460 done:
1461 	TRACE_2(TR_FAC_DISP, TR_PREEMPT,
1462 	    "preempt:tid %p old pri %d", t, oldpri);
1463 }
1464 
1465 static void
1466 ts_setrun(kthread_t *t)
1467 {
1468 	tsproc_t *tspp = (tsproc_t *)(t->t_cldata);
1469 
1470 	ASSERT(THREAD_LOCK_HELD(t));	/* t should be in transition */
1471 
1472 	if (tspp->ts_dispwait > ts_dptbl[tspp->ts_umdpri].ts_maxwait) {
1473 		tspp->ts_cpupri = ts_dptbl[tspp->ts_cpupri].ts_slpret;
1474 		TS_NEWUMDPRI(tspp);
1475 		tspp->ts_timeleft = ts_dptbl[tspp->ts_cpupri].ts_quantum;
1476 		tspp->ts_dispwait = 0;
1477 		if ((tspp->ts_flags & TSKPRI) == 0) {
1478 			THREAD_CHANGE_PRI(t,
1479 			    ts_dptbl[tspp->ts_umdpri].ts_globpri);
1480 			ASSERT(t->t_pri >= 0 && t->t_pri <= ts_maxglobpri);
1481 		}
1482 	}
1483 
1484 	tspp->ts_flags &= ~TSBACKQ;
1485 
1486 	if (tspp->ts_flags & TSIA) {
1487 		if (tspp->ts_flags & TSIASET)
1488 			setfrontdq(t);
1489 		else
1490 			setbackdq(t);
1491 	} else {
1492 		if (t->t_disp_time != lbolt)
1493 			setbackdq(t);
1494 		else
1495 			setfrontdq(t);
1496 	}
1497 }
1498 
1499 
1500 /*
1501  * Prepare thread for sleep. We reset the thread priority so it will
1502  * run at the kernel priority level when it wakes up.
1503  */
1504 static void
1505 ts_sleep(kthread_t *t)
1506 {
1507 	tsproc_t	*tspp = (tsproc_t *)(t->t_cldata);
1508 	int		flags;
1509 	pri_t		old_pri = t->t_pri;
1510 
1511 	ASSERT(t == curthread);
1512 	ASSERT(THREAD_LOCK_HELD(t));
1513 
1514 	/*
1515 	 * Account for time spent on CPU before going to sleep.
1516 	 */
1517 	(void) CPUCAPS_CHARGE(t, &tspp->ts_caps, CPUCAPS_CHARGE_ENFORCE);
1518 
1519 	flags = tspp->ts_flags;
1520 	if (t->t_kpri_req) {
1521 		tspp->ts_flags = flags | TSKPRI;
1522 		THREAD_CHANGE_PRI(t, ts_kmdpris[0]);
1523 		ASSERT(t->t_pri >= 0 && t->t_pri <= ts_maxglobpri);
1524 		t->t_trapret = 1;		/* so ts_trapret will run */
1525 		aston(t);
1526 	} else if (tspp->ts_dispwait > ts_dptbl[tspp->ts_umdpri].ts_maxwait) {
1527 		/*
1528 		 * If thread has blocked in the kernel (as opposed to
1529 		 * being merely preempted), recompute the user mode priority.
1530 		 */
1531 		tspp->ts_cpupri = ts_dptbl[tspp->ts_cpupri].ts_slpret;
1532 		TS_NEWUMDPRI(tspp);
1533 		tspp->ts_timeleft = ts_dptbl[tspp->ts_cpupri].ts_quantum;
1534 		tspp->ts_dispwait = 0;
1535 
1536 		THREAD_CHANGE_PRI(curthread,
1537 		    ts_dptbl[tspp->ts_umdpri].ts_globpri);
1538 		ASSERT(curthread->t_pri >= 0 &&
1539 		    curthread->t_pri <= ts_maxglobpri);
1540 		tspp->ts_flags = flags & ~TSKPRI;
1541 
1542 		if (DISP_MUST_SURRENDER(curthread))
1543 			cpu_surrender(curthread);
1544 	} else if (flags & TSKPRI) {
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 	}
1554 	t->t_stime = lbolt;		/* time stamp for the swapper */
1555 	TRACE_2(TR_FAC_DISP, TR_SLEEP,
1556 	    "sleep:tid %p old pri %d", t, old_pri);
1557 }
1558 
1559 
1560 /*
1561  * Return Values:
1562  *
1563  *	-1 if the thread is loaded or is not eligible to be swapped in.
1564  *
1565  *	effective priority of the specified thread based on swapout time
1566  *		and size of process (epri >= 0 , epri <= SHRT_MAX).
1567  */
1568 /* ARGSUSED */
1569 static pri_t
1570 ts_swapin(kthread_t *t, int flags)
1571 {
1572 	tsproc_t	*tspp = (tsproc_t *)(t->t_cldata);
1573 	long		epri = -1;
1574 	proc_t		*pp = ttoproc(t);
1575 
1576 	ASSERT(THREAD_LOCK_HELD(t));
1577 
1578 	/*
1579 	 * We know that pri_t is a short.
1580 	 * Be sure not to overrun its range.
1581 	 */
1582 	if (t->t_state == TS_RUN && (t->t_schedflag & TS_LOAD) == 0) {
1583 		time_t swapout_time;
1584 
1585 		swapout_time = (lbolt - t->t_stime) / hz;
1586 		if (INHERITED(t) || (tspp->ts_flags & (TSKPRI | TSIASET)))
1587 			epri = (long)DISP_PRIO(t) + swapout_time;
1588 		else {
1589 			/*
1590 			 * Threads which have been out for a long time,
1591 			 * have high user mode priority and are associated
1592 			 * with a small address space are more deserving
1593 			 */
1594 			epri = ts_dptbl[tspp->ts_umdpri].ts_globpri;
1595 			ASSERT(epri >= 0 && epri <= ts_maxumdpri);
1596 			epri += swapout_time - pp->p_swrss / nz(maxpgio)/2;
1597 		}
1598 		/*
1599 		 * Scale epri so SHRT_MAX/2 represents zero priority.
1600 		 */
1601 		epri += SHRT_MAX/2;
1602 		if (epri < 0)
1603 			epri = 0;
1604 		else if (epri > SHRT_MAX)
1605 			epri = SHRT_MAX;
1606 	}
1607 	return ((pri_t)epri);
1608 }
1609 
1610 /*
1611  * Return Values
1612  *	-1 if the thread isn't loaded or is not eligible to be swapped out.
1613  *
1614  *	effective priority of the specified thread based on if the swapper
1615  *		is in softswap or hardswap mode.
1616  *
1617  *		Softswap:  Return a low effective priority for threads
1618  *			   sleeping for more than maxslp secs.
1619  *
1620  *		Hardswap:  Return an effective priority such that threads
1621  *			   which have been in memory for a while and are
1622  *			   associated with a small address space are swapped
1623  *			   in before others.
1624  *
1625  *		(epri >= 0 , epri <= SHRT_MAX).
1626  */
1627 time_t	ts_minrun = 2;		/* XXX - t_pri becomes 59 within 2 secs */
1628 time_t	ts_minslp = 2;		/* min time on sleep queue for hardswap */
1629 
1630 static pri_t
1631 ts_swapout(kthread_t *t, int flags)
1632 {
1633 	tsproc_t	*tspp = (tsproc_t *)(t->t_cldata);
1634 	long		epri = -1;
1635 	proc_t		*pp = ttoproc(t);
1636 	time_t		swapin_time;
1637 
1638 	ASSERT(THREAD_LOCK_HELD(t));
1639 
1640 	if (INHERITED(t) || (tspp->ts_flags & (TSKPRI | TSIASET)) ||
1641 	    (t->t_proc_flag & TP_LWPEXIT) ||
1642 	    (t->t_state & (TS_ZOMB | TS_FREE | TS_STOPPED |
1643 	    TS_ONPROC | TS_WAIT)) ||
1644 	    !(t->t_schedflag & TS_LOAD) || !SWAP_OK(t))
1645 		return (-1);
1646 
1647 	ASSERT(t->t_state & (TS_SLEEP | TS_RUN));
1648 
1649 	/*
1650 	 * We know that pri_t is a short.
1651 	 * Be sure not to overrun its range.
1652 	 */
1653 	swapin_time = (lbolt - t->t_stime) / hz;
1654 	if (flags == SOFTSWAP) {
1655 		if (t->t_state == TS_SLEEP && swapin_time > maxslp) {
1656 			epri = 0;
1657 		} else {
1658 			return ((pri_t)epri);
1659 		}
1660 	} else {
1661 		pri_t pri;
1662 
1663 		if ((t->t_state == TS_SLEEP && swapin_time > ts_minslp) ||
1664 		    (t->t_state == TS_RUN && swapin_time > ts_minrun)) {
1665 			pri = ts_dptbl[tspp->ts_umdpri].ts_globpri;
1666 			ASSERT(pri >= 0 && pri <= ts_maxumdpri);
1667 			epri = swapin_time -
1668 			    (rm_asrss(pp->p_as) / nz(maxpgio)/2) - (long)pri;
1669 		} else {
1670 			return ((pri_t)epri);
1671 		}
1672 	}
1673 
1674 	/*
1675 	 * Scale epri so SHRT_MAX/2 represents zero priority.
1676 	 */
1677 	epri += SHRT_MAX/2;
1678 	if (epri < 0)
1679 		epri = 0;
1680 	else if (epri > SHRT_MAX)
1681 		epri = SHRT_MAX;
1682 
1683 	return ((pri_t)epri);
1684 }
1685 
1686 /*
1687  * Check for time slice expiration.  If time slice has expired
1688  * move thread to priority specified in tsdptbl for time slice expiration
1689  * and set runrun to cause preemption.
1690  */
1691 static void
1692 ts_tick(kthread_t *t)
1693 {
1694 	tsproc_t *tspp = (tsproc_t *)(t->t_cldata);
1695 	klwp_t *lwp;
1696 	boolean_t call_cpu_surrender = B_FALSE;
1697 	pri_t	oldpri = t->t_pri;
1698 
1699 	ASSERT(MUTEX_HELD(&(ttoproc(t))->p_lock));
1700 
1701 	thread_lock(t);
1702 
1703 	/*
1704 	 * Keep track of thread's project CPU usage.  Note that projects
1705 	 * get charged even when threads are running in the kernel.
1706 	 */
1707 	if (CPUCAPS_ON()) {
1708 		call_cpu_surrender = cpucaps_charge(t, &tspp->ts_caps,
1709 		    CPUCAPS_CHARGE_ENFORCE) && !(tspp->ts_flags & TSKPRI);
1710 	}
1711 
1712 	if ((tspp->ts_flags & TSKPRI) == 0) {
1713 		if (--tspp->ts_timeleft <= 0) {
1714 			pri_t	new_pri;
1715 
1716 			/*
1717 			 * If we're doing preemption control and trying to
1718 			 * avoid preempting this thread, just note that
1719 			 * the thread should yield soon and let it keep
1720 			 * running (unless it's been a while).
1721 			 */
1722 			if (t->t_schedctl && schedctl_get_nopreempt(t)) {
1723 				if (tspp->ts_timeleft > -SC_MAX_TICKS) {
1724 					DTRACE_SCHED1(schedctl__nopreempt,
1725 					    kthread_t *, t);
1726 					schedctl_set_yield(t, 1);
1727 					thread_unlock_nopreempt(t);
1728 					return;
1729 				}
1730 
1731 				TNF_PROBE_2(schedctl_failsafe,
1732 				    "schedctl TS ts_tick", /* CSTYLED */,
1733 				    tnf_pid, pid, ttoproc(t)->p_pid,
1734 				    tnf_lwpid, lwpid, t->t_tid);
1735 			}
1736 			tspp->ts_flags &= ~TSRESTORE;
1737 			tspp->ts_cpupri = ts_dptbl[tspp->ts_cpupri].ts_tqexp;
1738 			TS_NEWUMDPRI(tspp);
1739 			tspp->ts_dispwait = 0;
1740 			new_pri = ts_dptbl[tspp->ts_umdpri].ts_globpri;
1741 			ASSERT(new_pri >= 0 && new_pri <= ts_maxglobpri);
1742 			/*
1743 			 * When the priority of a thread is changed,
1744 			 * it may be necessary to adjust its position
1745 			 * on a sleep queue or dispatch queue.
1746 			 * The function thread_change_pri accomplishes
1747 			 * this.
1748 			 */
1749 			if (thread_change_pri(t, new_pri, 0)) {
1750 				if ((t->t_schedflag & TS_LOAD) &&
1751 				    (lwp = t->t_lwp) &&
1752 				    lwp->lwp_state == LWP_USER)
1753 					t->t_schedflag &= ~TS_DONT_SWAP;
1754 				tspp->ts_timeleft =
1755 				    ts_dptbl[tspp->ts_cpupri].ts_quantum;
1756 			} else {
1757 				call_cpu_surrender = B_TRUE;
1758 			}
1759 			TRACE_2(TR_FAC_DISP, TR_TICK,
1760 			    "tick:tid %p old pri %d", t, oldpri);
1761 		} else if (t->t_state == TS_ONPROC &&
1762 			    t->t_pri < t->t_disp_queue->disp_maxrunpri) {
1763 			call_cpu_surrender = B_TRUE;
1764 		}
1765 	}
1766 
1767 	if (call_cpu_surrender) {
1768 		tspp->ts_flags |= TSBACKQ;
1769 		cpu_surrender(t);
1770 	}
1771 
1772 	thread_unlock_nopreempt(t);	/* clock thread can't be preempted */
1773 }
1774 
1775 
1776 /*
1777  * If thread is currently at a kernel mode priority (has slept)
1778  * we assign it the appropriate user mode priority and time quantum
1779  * here.  If we are lowering the thread's priority below that of
1780  * other runnable threads we will normally set runrun via cpu_surrender() to
1781  * cause preemption.
1782  */
1783 static void
1784 ts_trapret(kthread_t *t)
1785 {
1786 	tsproc_t	*tspp = (tsproc_t *)t->t_cldata;
1787 	cpu_t		*cp = CPU;
1788 	pri_t		old_pri = curthread->t_pri;
1789 
1790 	ASSERT(THREAD_LOCK_HELD(t));
1791 	ASSERT(t == curthread);
1792 	ASSERT(cp->cpu_dispthread == t);
1793 	ASSERT(t->t_state == TS_ONPROC);
1794 
1795 	t->t_kpri_req = 0;
1796 	if (tspp->ts_dispwait > ts_dptbl[tspp->ts_umdpri].ts_maxwait) {
1797 		tspp->ts_cpupri = ts_dptbl[tspp->ts_cpupri].ts_slpret;
1798 		TS_NEWUMDPRI(tspp);
1799 		tspp->ts_timeleft = ts_dptbl[tspp->ts_cpupri].ts_quantum;
1800 		tspp->ts_dispwait = 0;
1801 
1802 		/*
1803 		 * If thread has blocked in the kernel (as opposed to
1804 		 * being merely preempted), recompute the user mode priority.
1805 		 */
1806 		THREAD_CHANGE_PRI(t, ts_dptbl[tspp->ts_umdpri].ts_globpri);
1807 		cp->cpu_dispatch_pri = DISP_PRIO(t);
1808 		ASSERT(t->t_pri >= 0 && t->t_pri <= ts_maxglobpri);
1809 		tspp->ts_flags &= ~TSKPRI;
1810 
1811 		if (DISP_MUST_SURRENDER(t))
1812 			cpu_surrender(t);
1813 	} else if (tspp->ts_flags & TSKPRI) {
1814 		/*
1815 		 * If thread has blocked in the kernel (as opposed to
1816 		 * being merely preempted), recompute the user mode priority.
1817 		 */
1818 		THREAD_CHANGE_PRI(t, ts_dptbl[tspp->ts_umdpri].ts_globpri);
1819 		cp->cpu_dispatch_pri = DISP_PRIO(t);
1820 		ASSERT(t->t_pri >= 0 && t->t_pri <= ts_maxglobpri);
1821 		tspp->ts_flags &= ~TSKPRI;
1822 
1823 		if (DISP_MUST_SURRENDER(t))
1824 			cpu_surrender(t);
1825 	}
1826 
1827 	/*
1828 	 * Swapout lwp if the swapper is waiting for this thread to
1829 	 * reach a safe point.
1830 	 */
1831 	if ((t->t_schedflag & TS_SWAPENQ) && !(tspp->ts_flags & TSIASET)) {
1832 		thread_unlock(t);
1833 		swapout_lwp(ttolwp(t));
1834 		thread_lock(t);
1835 	}
1836 
1837 	TRACE_2(TR_FAC_DISP, TR_TRAPRET,
1838 	    "trapret:tid %p old pri %d", t, old_pri);
1839 }
1840 
1841 
1842 /*
1843  * Update the ts_dispwait values of all time sharing threads that
1844  * are currently runnable at a user mode priority and bump the priority
1845  * if ts_dispwait exceeds ts_maxwait.  Called once per second via
1846  * timeout which we reset here.
1847  *
1848  * There are several lists of time sharing threads broken up by a hash on
1849  * the thread pointer.  Each list has its own lock.  This avoids blocking
1850  * all ts_enterclass, ts_fork, and ts_exitclass operations while ts_update
1851  * runs.  ts_update traverses each list in turn.
1852  *
1853  * If multiple threads have their priorities updated to the same value,
1854  * the system implicitly favors the one that is updated first (since it
1855  * winds up first on the run queue).  To avoid this unfairness, the
1856  * traversal of threads starts at the list indicated by a marker.  When
1857  * threads in more than one list have their priorities updated, the marker
1858  * is moved.  This changes the order the threads will be placed on the run
1859  * queue the next time ts_update is called and preserves fairness over the
1860  * long run.  The marker doesn't need to be protected by a lock since it's
1861  * only accessed by ts_update, which is inherently single-threaded (only
1862  * one instance can be running at a time).
1863  */
1864 static void
1865 ts_update(void *arg)
1866 {
1867 	int		i;
1868 	int		new_marker = -1;
1869 	static int	ts_update_marker;
1870 
1871 	/*
1872 	 * Start with the ts_update_marker list, then do the rest.
1873 	 */
1874 	i = ts_update_marker;
1875 	do {
1876 		/*
1877 		 * If this is the first list after the current marker to
1878 		 * have threads with priorities updated, advance the marker
1879 		 * to this list for the next time ts_update runs.
1880 		 */
1881 		if (ts_update_list(i) && new_marker == -1 &&
1882 		    i != ts_update_marker) {
1883 			new_marker = i;
1884 		}
1885 	} while ((i = TS_LIST_NEXT(i)) != ts_update_marker);
1886 
1887 	/* advance marker for next ts_update call */
1888 	if (new_marker != -1)
1889 		ts_update_marker = new_marker;
1890 
1891 	(void) timeout(ts_update, arg, hz);
1892 }
1893 
1894 /*
1895  * Updates priority for a list of threads.  Returns 1 if the priority of
1896  * one of the threads was actually updated, 0 if none were for various
1897  * reasons (thread is no longer in the TS or IA class, isn't runnable,
1898  * hasn't waited long enough, has the preemption control no-preempt bit
1899  * set, etc.)
1900  */
1901 static int
1902 ts_update_list(int i)
1903 {
1904 	tsproc_t *tspp;
1905 	kthread_t *tx;
1906 	int updated = 0;
1907 
1908 	mutex_enter(&ts_list_lock[i]);
1909 	for (tspp = ts_plisthead[i].ts_next; tspp != &ts_plisthead[i];
1910 	    tspp = tspp->ts_next) {
1911 		tx = tspp->ts_tp;
1912 		/*
1913 		 * Lock the thread and verify state.
1914 		 */
1915 		thread_lock(tx);
1916 		/*
1917 		 * Skip the thread if it is no longer in the TS (or IA) class.
1918 		 */
1919 		if (tx->t_clfuncs != &ts_classfuncs.thread &&
1920 		    tx->t_clfuncs != &ia_classfuncs.thread)
1921 			goto next;
1922 		tspp->ts_dispwait++;
1923 		if ((tspp->ts_flags & TSKPRI) != 0)
1924 			goto next;
1925 		if (tspp->ts_dispwait <= ts_dptbl[tspp->ts_umdpri].ts_maxwait)
1926 			goto next;
1927 		if (tx->t_schedctl && schedctl_get_nopreempt(tx))
1928 			goto next;
1929 		if (tx->t_state != TS_RUN && tx->t_state != TS_WAIT &&
1930 		    (tx->t_state != TS_SLEEP || !ts_sleep_promote)) {
1931 			/* make next syscall/trap do CL_TRAPRET */
1932 			tx->t_trapret = 1;
1933 			aston(tx);
1934 			goto next;
1935 		}
1936 		tspp->ts_cpupri = ts_dptbl[tspp->ts_cpupri].ts_lwait;
1937 		TS_NEWUMDPRI(tspp);
1938 		tspp->ts_dispwait = 0;
1939 		updated = 1;
1940 
1941 		/*
1942 		 * Only dequeue it if needs to move; otherwise it should
1943 		 * just round-robin here.
1944 		 */
1945 		if (tx->t_pri != ts_dptbl[tspp->ts_umdpri].ts_globpri) {
1946 			pri_t oldpri = tx->t_pri;
1947 			ts_change_priority(tx, tspp);
1948 			TRACE_2(TR_FAC_DISP, TR_UPDATE,
1949 			    "update:tid %p old pri %d", tx, oldpri);
1950 		}
1951 next:
1952 		thread_unlock(tx);
1953 	}
1954 	mutex_exit(&ts_list_lock[i]);
1955 
1956 	return (updated);
1957 }
1958 
1959 /*
1960  * Processes waking up go to the back of their queue.  We don't
1961  * need to assign a time quantum here because thread is still
1962  * at a kernel mode priority and the time slicing is not done
1963  * for threads running in the kernel after sleeping.  The proper
1964  * time quantum will be assigned by ts_trapret before the thread
1965  * returns to user mode.
1966  */
1967 static void
1968 ts_wakeup(kthread_t *t)
1969 {
1970 	tsproc_t	*tspp = (tsproc_t *)(t->t_cldata);
1971 
1972 	ASSERT(THREAD_LOCK_HELD(t));
1973 
1974 	t->t_stime = lbolt;		/* time stamp for the swapper */
1975 
1976 	if (tspp->ts_flags & TSKPRI) {
1977 		tspp->ts_flags &= ~TSBACKQ;
1978 		if (tspp->ts_flags & TSIASET)
1979 			setfrontdq(t);
1980 		else
1981 			setbackdq(t);
1982 	} else if (t->t_kpri_req) {
1983 		/*
1984 		 * Give thread a priority boost if we were asked.
1985 		 */
1986 		tspp->ts_flags |= TSKPRI;
1987 		THREAD_CHANGE_PRI(t, ts_kmdpris[0]);
1988 		setbackdq(t);
1989 		t->t_trapret = 1;	/* so that ts_trapret will run */
1990 		aston(t);
1991 	} else {
1992 		if (tspp->ts_dispwait > ts_dptbl[tspp->ts_umdpri].ts_maxwait) {
1993 			tspp->ts_cpupri = ts_dptbl[tspp->ts_cpupri].ts_slpret;
1994 			TS_NEWUMDPRI(tspp);
1995 			tspp->ts_timeleft =
1996 			    ts_dptbl[tspp->ts_cpupri].ts_quantum;
1997 			tspp->ts_dispwait = 0;
1998 			THREAD_CHANGE_PRI(t,
1999 			    ts_dptbl[tspp->ts_umdpri].ts_globpri);
2000 			ASSERT(t->t_pri >= 0 && t->t_pri <= ts_maxglobpri);
2001 		}
2002 
2003 		tspp->ts_flags &= ~TSBACKQ;
2004 
2005 		if (tspp->ts_flags & TSIA) {
2006 			if (tspp->ts_flags & TSIASET)
2007 				setfrontdq(t);
2008 			else
2009 				setbackdq(t);
2010 		} else {
2011 			if (t->t_disp_time != lbolt)
2012 				setbackdq(t);
2013 			else
2014 				setfrontdq(t);
2015 		}
2016 	}
2017 }
2018 
2019 
2020 /*
2021  * When a thread yields, put it on the back of the run queue.
2022  */
2023 static void
2024 ts_yield(kthread_t *t)
2025 {
2026 	tsproc_t	*tspp = (tsproc_t *)(t->t_cldata);
2027 
2028 	ASSERT(t == curthread);
2029 	ASSERT(THREAD_LOCK_HELD(t));
2030 
2031 	/*
2032 	 * Collect CPU usage spent before yielding
2033 	 */
2034 	(void) CPUCAPS_CHARGE(t, &tspp->ts_caps, CPUCAPS_CHARGE_ENFORCE);
2035 
2036 	/*
2037 	 * Clear the preemption control "yield" bit since the user is
2038 	 * doing a yield.
2039 	 */
2040 	if (t->t_schedctl)
2041 		schedctl_set_yield(t, 0);
2042 	/*
2043 	 * If ts_preempt() artifically increased the thread's priority
2044 	 * to avoid preemption, restore the original priority now.
2045 	 */
2046 	if (tspp->ts_flags & TSRESTORE) {
2047 		THREAD_CHANGE_PRI(t, tspp->ts_scpri);
2048 		tspp->ts_flags &= ~TSRESTORE;
2049 	}
2050 	if (tspp->ts_timeleft <= 0) {
2051 		/*
2052 		 * Time slice was artificially extended to avoid
2053 		 * preemption, so pretend we're preempting it now.
2054 		 */
2055 		DTRACE_SCHED1(schedctl__yield, int, -tspp->ts_timeleft);
2056 		tspp->ts_cpupri = ts_dptbl[tspp->ts_cpupri].ts_tqexp;
2057 		TS_NEWUMDPRI(tspp);
2058 		tspp->ts_timeleft = ts_dptbl[tspp->ts_cpupri].ts_quantum;
2059 		tspp->ts_dispwait = 0;
2060 		THREAD_CHANGE_PRI(t, ts_dptbl[tspp->ts_umdpri].ts_globpri);
2061 		ASSERT(t->t_pri >= 0 && t->t_pri <= ts_maxglobpri);
2062 	}
2063 	tspp->ts_flags &= ~TSBACKQ;
2064 	setbackdq(t);
2065 }
2066 
2067 
2068 /*
2069  * Increment the nice value of the specified thread by incr and
2070  * return the new value in *retvalp.
2071  */
2072 static int
2073 ts_donice(kthread_t *t, cred_t *cr, int incr, int *retvalp)
2074 {
2075 	int		newnice;
2076 	tsproc_t	*tspp = (tsproc_t *)(t->t_cldata);
2077 	tsparms_t	tsparms;
2078 
2079 	ASSERT(MUTEX_HELD(&(ttoproc(t))->p_lock));
2080 
2081 	/* If there's no change to priority, just return current setting */
2082 	if (incr == 0) {
2083 		if (retvalp) {
2084 			*retvalp = tspp->ts_nice - NZERO;
2085 		}
2086 		return (0);
2087 	}
2088 
2089 	if ((incr < 0 || incr > 2 * NZERO) &&
2090 	    secpolicy_setpriority(cr) != 0)
2091 		return (EPERM);
2092 
2093 	/*
2094 	 * Specifying a nice increment greater than the upper limit of
2095 	 * 2 * NZERO - 1 will result in the thread's nice value being
2096 	 * set to the upper limit.  We check for this before computing
2097 	 * the new value because otherwise we could get overflow
2098 	 * if a privileged process specified some ridiculous increment.
2099 	 */
2100 	if (incr > 2 * NZERO - 1)
2101 		incr = 2 * NZERO - 1;
2102 
2103 	newnice = tspp->ts_nice + incr;
2104 	if (newnice >= 2 * NZERO)
2105 		newnice = 2 * NZERO - 1;
2106 	else if (newnice < 0)
2107 		newnice = 0;
2108 
2109 	tsparms.ts_uprilim = tsparms.ts_upri =
2110 		-((newnice - NZERO) * ts_maxupri) / NZERO;
2111 	/*
2112 	 * Reset the uprilim and upri values of the thread.
2113 	 * Call ts_parmsset even if thread is interactive since we're
2114 	 * not changing mode.
2115 	 */
2116 	(void) ts_parmsset(t, (void *)&tsparms, (id_t)0, (cred_t *)NULL);
2117 
2118 	/*
2119 	 * Although ts_parmsset already reset ts_nice it may
2120 	 * not have been set to precisely the value calculated above
2121 	 * because ts_parmsset determines the nice value from the
2122 	 * user priority and we may have truncated during the integer
2123 	 * conversion from nice value to user priority and back.
2124 	 * We reset ts_nice to the value we calculated above.
2125 	 */
2126 	tspp->ts_nice = (char)newnice;
2127 
2128 	if (retvalp)
2129 		*retvalp = newnice - NZERO;
2130 	return (0);
2131 }
2132 
2133 
2134 /*
2135  * ia_set_process_group marks foreground processes as interactive
2136  * and background processes as non-interactive iff the session
2137  * leader is interactive.  This routine is called from two places:
2138  *	strioctl:SPGRP when a new process group gets
2139  * 		control of the tty.
2140  *	ia_parmsset-when the process in question is a session leader.
2141  * ia_set_process_group assumes that pidlock is held by the caller,
2142  * either strioctl or priocntlsys.  If the caller is priocntlsys
2143  * (via ia_parmsset) then the p_lock of the session leader is held
2144  * and the code needs to be careful about acquiring other p_locks.
2145  */
2146 static void
2147 ia_set_process_group(pid_t sid, pid_t bg_pgid, pid_t fg_pgid)
2148 {
2149 	proc_t 		*leader, *fg, *bg;
2150 	tsproc_t	*tspp;
2151 	kthread_t	*tx;
2152 	int		plocked = 0;
2153 
2154 	ASSERT(MUTEX_HELD(&pidlock));
2155 
2156 	/*
2157 	 * see if the session leader is interactive AND
2158 	 * if it is currently "on" AND controlling a tty
2159 	 * iff it is then make the processes in the foreground
2160 	 * group interactive and the processes in the background
2161 	 * group non-interactive.
2162 	 */
2163 	if ((leader = (proc_t *)prfind(sid)) == NULL) {
2164 		return;
2165 	}
2166 	if (leader->p_stat == SIDL) {
2167 		return;
2168 	}
2169 	if ((tx = proctot(leader)) == NULL) {
2170 		return;
2171 	}
2172 	/*
2173 	 * XXX do all the threads in the leader
2174 	 */
2175 	if (tx->t_cid != ia_cid) {
2176 		return;
2177 	}
2178 	tspp = tx->t_cldata;
2179 	/*
2180 	 * session leaders that are not interactive need not have
2181 	 * any processing done for them.  They are typically shells
2182 	 * that do not have focus and are changing the process group
2183 	 * attatched to the tty, e.g. a process that is exiting
2184 	 */
2185 	mutex_enter(&leader->p_sessp->s_lock);
2186 	if (!(tspp->ts_flags & TSIASET) ||
2187 	    (leader->p_sessp->s_vp == NULL) ||
2188 	    (leader->p_sessp->s_vp->v_stream == NULL)) {
2189 		mutex_exit(&leader->p_sessp->s_lock);
2190 		return;
2191 	}
2192 	mutex_exit(&leader->p_sessp->s_lock);
2193 
2194 	/*
2195 	 * If we're already holding the leader's p_lock, we should use
2196 	 * mutex_tryenter instead of mutex_enter to avoid deadlocks from
2197 	 * lock ordering violations.
2198 	 */
2199 	if (mutex_owned(&leader->p_lock))
2200 		plocked = 1;
2201 
2202 	if (fg_pgid == 0)
2203 		goto skip;
2204 	/*
2205 	 * now look for all processes in the foreground group and
2206 	 * make them interactive
2207 	 */
2208 	for (fg = (proc_t *)pgfind(fg_pgid); fg != NULL; fg = fg->p_pglink) {
2209 		/*
2210 		 * if the process is SIDL it's begin forked, ignore it
2211 		 */
2212 		if (fg->p_stat == SIDL) {
2213 			continue;
2214 		}
2215 		/*
2216 		 * sesssion leaders must be turned on/off explicitly
2217 		 * not implicitly as happens to other members of
2218 		 * the process group.
2219 		 */
2220 		if (fg->p_pid  == fg->p_sessp->s_sid) {
2221 			continue;
2222 		}
2223 
2224 		TRACE_1(TR_FAC_IA, TR_GROUP_ON,
2225 		    "group on:proc %p", fg);
2226 
2227 		if (plocked) {
2228 			if (mutex_tryenter(&fg->p_lock) == 0)
2229 				continue;
2230 		} else {
2231 			mutex_enter(&fg->p_lock);
2232 		}
2233 
2234 		if ((tx = proctot(fg)) == NULL) {
2235 			mutex_exit(&fg->p_lock);
2236 			continue;
2237 		}
2238 		do {
2239 			thread_lock(tx);
2240 			/*
2241 			 * if this thread is not interactive continue
2242 			 */
2243 			if (tx->t_cid != ia_cid) {
2244 				thread_unlock(tx);
2245 				continue;
2246 			}
2247 			tspp = tx->t_cldata;
2248 			tspp->ts_flags |= TSIASET;
2249 			tspp->ts_boost = ia_boost;
2250 			TS_NEWUMDPRI(tspp);
2251 			if ((tspp->ts_flags & TSKPRI) != 0) {
2252 				thread_unlock(tx);
2253 				continue;
2254 			}
2255 			tspp->ts_dispwait = 0;
2256 			ts_change_priority(tx, tspp);
2257 			thread_unlock(tx);
2258 		} while ((tx = tx->t_forw) != fg->p_tlist);
2259 		mutex_exit(&fg->p_lock);
2260 	}
2261 skip:
2262 	if (bg_pgid == 0)
2263 		return;
2264 	for (bg = (proc_t *)pgfind(bg_pgid); bg != NULL; bg = bg->p_pglink) {
2265 		if (bg->p_stat == SIDL) {
2266 			continue;
2267 		}
2268 		/*
2269 		 * sesssion leaders must be turned off explicitly
2270 		 * not implicitly as happens to other members of
2271 		 * the process group.
2272 		 */
2273 		if (bg->p_pid == bg->p_sessp->s_sid) {
2274 			continue;
2275 		}
2276 
2277 		TRACE_1(TR_FAC_IA, TR_GROUP_OFF,
2278 		    "group off:proc %p", bg);
2279 
2280 		if (plocked) {
2281 			if (mutex_tryenter(&bg->p_lock) == 0)
2282 				continue;
2283 		} else {
2284 			mutex_enter(&bg->p_lock);
2285 		}
2286 
2287 		if ((tx = proctot(bg)) == NULL) {
2288 			mutex_exit(&bg->p_lock);
2289 			continue;
2290 		}
2291 		do {
2292 			thread_lock(tx);
2293 			/*
2294 			 * if this thread is not interactive continue
2295 			 */
2296 			if (tx->t_cid != ia_cid) {
2297 				thread_unlock(tx);
2298 				continue;
2299 			}
2300 			tspp = tx->t_cldata;
2301 			tspp->ts_flags &= ~TSIASET;
2302 			tspp->ts_boost = -ia_boost;
2303 			TS_NEWUMDPRI(tspp);
2304 			if ((tspp->ts_flags & TSKPRI) != 0) {
2305 				thread_unlock(tx);
2306 				continue;
2307 			}
2308 
2309 			tspp->ts_dispwait = 0;
2310 			ts_change_priority(tx, tspp);
2311 			thread_unlock(tx);
2312 		} while ((tx = tx->t_forw) != bg->p_tlist);
2313 		mutex_exit(&bg->p_lock);
2314 	}
2315 }
2316 
2317 
2318 static void
2319 ts_change_priority(kthread_t *t, tsproc_t *tspp)
2320 {
2321 	pri_t	new_pri;
2322 
2323 	ASSERT(THREAD_LOCK_HELD(t));
2324 	new_pri = ts_dptbl[tspp->ts_umdpri].ts_globpri;
2325 	ASSERT(new_pri >= 0 && new_pri <= ts_maxglobpri);
2326 	tspp->ts_flags &= ~TSRESTORE;
2327 	if (t == curthread || t->t_state == TS_ONPROC) {
2328 		/* curthread is always onproc */
2329 		cpu_t	*cp = t->t_disp_queue->disp_cpu;
2330 		THREAD_CHANGE_PRI(t, new_pri);
2331 		if (t == cp->cpu_dispthread)
2332 			cp->cpu_dispatch_pri = DISP_PRIO(t);
2333 		if (DISP_MUST_SURRENDER(t)) {
2334 			tspp->ts_flags |= TSBACKQ;
2335 			cpu_surrender(t);
2336 		} else {
2337 			tspp->ts_timeleft =
2338 			    ts_dptbl[tspp->ts_cpupri].ts_quantum;
2339 		}
2340 	} else {
2341 		int	frontq;
2342 
2343 		frontq = (tspp->ts_flags & TSIASET) != 0;
2344 		/*
2345 		 * When the priority of a thread is changed,
2346 		 * it may be necessary to adjust its position
2347 		 * on a sleep queue or dispatch queue.
2348 		 * The function thread_change_pri accomplishes
2349 		 * this.
2350 		 */
2351 		if (thread_change_pri(t, new_pri, frontq)) {
2352 			/*
2353 			 * The thread was on a run queue. Reset
2354 			 * its CPU timeleft from the quantum
2355 			 * associated with the new priority.
2356 			 */
2357 			tspp->ts_timeleft =
2358 			    ts_dptbl[tspp->ts_cpupri].ts_quantum;
2359 		} else {
2360 			tspp->ts_flags |= TSBACKQ;
2361 		}
2362 	}
2363 }
2364 
2365 static int
2366 ts_alloc(void **p, int flag)
2367 {
2368 	void *bufp;
2369 	bufp = kmem_alloc(sizeof (tsproc_t), flag);
2370 	if (bufp == NULL) {
2371 		return (ENOMEM);
2372 	} else {
2373 		*p = bufp;
2374 		return (0);
2375 	}
2376 }
2377 
2378 static void
2379 ts_free(void *bufp)
2380 {
2381 	if (bufp)
2382 		kmem_free(bufp, sizeof (tsproc_t));
2383 }
2384