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