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