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