cmd: remove various sparc-only bins
[unleashed.git] / kernel / sched / ts / ts.c
blob3db6b9d488abc10167ddde5b7ce78dd092e0c592
1 /*
2 * CDDL HEADER START
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.
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.
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]
19 * CDDL HEADER END
23 * Copyright (c) 1994, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright 2013, Joyent, Inc. All rights reserved.
27 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
28 /* All Rights Reserved */
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/tnf_probe.h>
55 #include <sys/atomic.h>
56 #include <sys/policy.h>
57 #include <sys/sdt.h>
58 #include <sys/cpupart.h>
59 #include <vm/rm.h>
60 #include <vm/seg_kmem.h>
61 #include <sys/modctl.h>
62 #include <sys/cpucaps.h>
64 static pri_t ts_init(id_t, int, classfuncs_t **);
66 static struct sclass csw = {
67 "TS",
68 ts_init,
72 static struct modlsched modlsched = {
73 &mod_schedops, "time sharing sched class", &csw
76 static struct modlinkage modlinkage = {
77 MODREV_1, (void *)&modlsched, NULL
80 int
81 _init()
83 return (mod_install(&modlinkage));
86 int
87 _fini()
89 return (EBUSY); /* don't remove TS for now */
92 int
93 _info(struct modinfo *modinfop)
95 return (mod_info(&modlinkage, modinfop));
99 * Class specific code for the time-sharing class
104 * Extern declarations for variables defined in the ts master file
106 #define TSMAXUPRI 60
108 pri_t ts_maxupri = TSMAXUPRI; /* max time-sharing user priority */
109 pri_t ts_maxumdpri; /* maximum user mode ts priority */
111 pri_t ia_maxupri = IAMAXUPRI; /* max interactive user priority */
112 pri_t ia_boost = IA_BOOST; /* boost value for interactive */
114 tsdpent_t *ts_dptbl; /* time-sharing disp parameter table */
115 pri_t *ts_kmdpris; /* array of global pris used by ts procs when */
116 /* sleeping or running in kernel after sleep */
118 static id_t ia_cid;
120 int ts_sleep_promote = 1;
122 #define tsmedumdpri (ts_maxumdpri >> 1)
124 #define TS_NEWUMDPRI(tspp) \
126 pri_t pri; \
127 pri = (tspp)->ts_cpupri + (tspp)->ts_upri + (tspp)->ts_boost; \
128 if (pri > ts_maxumdpri) \
129 (tspp)->ts_umdpri = ts_maxumdpri; \
130 else if (pri < 0) \
131 (tspp)->ts_umdpri = 0; \
132 else \
133 (tspp)->ts_umdpri = pri; \
134 ASSERT((tspp)->ts_umdpri >= 0 && (tspp)->ts_umdpri <= ts_maxumdpri); \
138 * The tsproc_t structures are kept in an array of circular doubly linked
139 * lists. A hash on the thread pointer is used to determine which list
140 * each thread should be placed. Each list has a dummy "head" which is
141 * never removed, so the list is never empty. ts_update traverses these
142 * lists to update the priorities of threads that have been waiting on
143 * the run queue.
146 #define TS_LISTS 16 /* number of lists, must be power of 2 */
148 /* hash function, argument is a thread pointer */
149 #define TS_LIST_HASH(tp) (((uintptr_t)(tp) >> 9) & (TS_LISTS - 1))
151 /* iterate to the next list */
152 #define TS_LIST_NEXT(i) (((i) + 1) & (TS_LISTS - 1))
155 * Insert thread into the appropriate tsproc list.
157 #define TS_LIST_INSERT(tspp) \
159 int index = TS_LIST_HASH(tspp->ts_tp); \
160 kmutex_t *lockp = &ts_list_lock[index]; \
161 tsproc_t *headp = &ts_plisthead[index]; \
162 mutex_enter(lockp); \
163 tspp->ts_next = headp->ts_next; \
164 tspp->ts_prev = headp; \
165 headp->ts_next->ts_prev = tspp; \
166 headp->ts_next = tspp; \
167 mutex_exit(lockp); \
171 * Remove thread from tsproc list.
173 #define TS_LIST_DELETE(tspp) \
175 int index = TS_LIST_HASH(tspp->ts_tp); \
176 kmutex_t *lockp = &ts_list_lock[index]; \
177 mutex_enter(lockp); \
178 tspp->ts_prev->ts_next = tspp->ts_next; \
179 tspp->ts_next->ts_prev = tspp->ts_prev; \
180 mutex_exit(lockp); \
184 static int ts_admin(caddr_t, cred_t *);
185 static int ts_enterclass(kthread_t *, id_t, void *, cred_t *, void *);
186 static int ts_fork(kthread_t *, kthread_t *, void *);
187 static int ts_getclinfo(void *);
188 static int ts_getclpri(pcpri_t *);
189 static int ts_parmsin(void *);
190 static int ts_parmsout(void *, pc_vaparms_t *);
191 static int ts_vaparmsin(void *, pc_vaparms_t *);
192 static int ts_vaparmsout(void *, pc_vaparms_t *);
193 static int ts_parmsset(kthread_t *, void *, id_t, cred_t *);
194 static void ts_exit(kthread_t *);
195 static int ts_donice(kthread_t *, cred_t *, int, int *);
196 static int ts_doprio(kthread_t *, cred_t *, int, int *);
197 static void ts_exitclass(void *);
198 static int ts_canexit(kthread_t *, cred_t *);
199 static void ts_forkret(kthread_t *, kthread_t *);
200 static void ts_nullsys();
201 static void ts_parmsget(kthread_t *, void *);
202 static void ts_preempt(kthread_t *);
203 static void ts_setrun(kthread_t *);
204 static void ts_sleep(kthread_t *);
205 static void ts_tick(kthread_t *);
206 static void ts_trapret(kthread_t *);
207 static void ts_update(void *);
208 static int ts_update_list(int);
209 static void ts_wakeup(kthread_t *);
210 static pri_t ts_globpri(kthread_t *);
211 static void ts_yield(kthread_t *);
212 extern tsdpent_t *ts_getdptbl(void);
213 extern pri_t *ts_getkmdpris(void);
214 extern pri_t td_getmaxumdpri(void);
215 static int ts_alloc(void **, int);
216 static void ts_free(void *);
218 pri_t ia_init(id_t, int, classfuncs_t **);
219 static int ia_getclinfo(void *);
220 static int ia_getclpri(pcpri_t *);
221 static int ia_parmsin(void *);
222 static int ia_vaparmsin(void *, pc_vaparms_t *);
223 static int ia_vaparmsout(void *, pc_vaparms_t *);
224 static int ia_parmsset(kthread_t *, void *, id_t, cred_t *);
225 static void ia_parmsget(kthread_t *, void *);
226 static void ia_set_process_group(pid_t, pid_t, pid_t);
228 static void ts_change_priority(kthread_t *, tsproc_t *);
230 extern pri_t ts_maxkmdpri; /* maximum kernel mode ts priority */
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 */
236 static gid_t IA_gid = 0;
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,
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_trapret,
263 ts_preempt,
264 ts_setrun,
265 ts_sleep,
266 ts_tick,
267 ts_wakeup,
268 ts_donice,
269 ts_globpri,
270 ts_nullsys, /* set_process_group */
271 ts_yield,
272 ts_doprio,
276 * ia_classfuncs is used for interactive class threads; IA threads are stored
277 * on the same class list as TS threads, and most of the class functions are
278 * identical, but a few have different enough functionality to require their
279 * own functions.
281 static struct classfuncs ia_classfuncs = {
282 /* class functions */
283 ts_admin,
284 ia_getclinfo,
285 ia_parmsin,
286 ts_parmsout,
287 ia_vaparmsin,
288 ia_vaparmsout,
289 ia_getclpri,
290 ts_alloc,
291 ts_free,
293 /* thread functions */
294 ts_enterclass,
295 ts_exitclass,
296 ts_canexit,
297 ts_fork,
298 ts_forkret,
299 ia_parmsget,
300 ia_parmsset,
301 ts_nullsys, /* stop */
302 ts_exit,
303 ts_nullsys, /* active */
304 ts_nullsys, /* inactive */
305 ts_trapret,
306 ts_preempt,
307 ts_setrun,
308 ts_sleep,
309 ts_tick,
310 ts_wakeup,
311 ts_donice,
312 ts_globpri,
313 ia_set_process_group,
314 ts_yield,
315 ts_doprio,
320 * Time sharing class initialization. Called by dispinit() at boot time.
321 * We can ignore the clparmsz argument since we know that the smallest
322 * possible parameter buffer is big enough for us.
324 /* ARGSUSED */
325 static pri_t
326 ts_init(id_t cid, int clparmsz, classfuncs_t **clfuncspp)
328 int i;
329 extern pri_t ts_getmaxumdpri(void);
331 ts_dptbl = ts_getdptbl();
332 ts_kmdpris = ts_getkmdpris();
333 ts_maxumdpri = ts_getmaxumdpri();
334 ts_maxglobpri = MAX(ts_kmdpris[0], ts_dptbl[ts_maxumdpri].ts_globpri);
337 * Initialize the tsproc lists.
339 for (i = 0; i < TS_LISTS; i++) {
340 ts_plisthead[i].ts_next = ts_plisthead[i].ts_prev =
341 &ts_plisthead[i];
345 * We're required to return a pointer to our classfuncs
346 * structure and the highest global priority value we use.
348 *clfuncspp = &ts_classfuncs;
349 return (ts_maxglobpri);
354 * Interactive class scheduler initialization
356 /* ARGSUSED */
357 pri_t
358 ia_init(id_t cid, int clparmsz, classfuncs_t **clfuncspp)
361 * We're required to return a pointer to our classfuncs
362 * structure and the highest global priority value we use.
364 ia_cid = cid;
365 *clfuncspp = &ia_classfuncs;
366 return (ts_maxglobpri);
371 * Get or reset the ts_dptbl values per the user's request.
373 static int
374 ts_admin(caddr_t uaddr, cred_t *reqpcredp)
376 tsadmin_t tsadmin;
377 tsdpent_t *tmpdpp;
378 int userdpsz;
379 int i;
380 size_t tsdpsz;
382 if (get_udatamodel() == DATAMODEL_NATIVE) {
383 if (copyin(uaddr, &tsadmin, sizeof (tsadmin_t)))
384 return (EFAULT);
386 #ifdef _SYSCALL32_IMPL
387 else {
388 /* get tsadmin struct from ILP32 caller */
389 tsadmin32_t tsadmin32;
390 if (copyin(uaddr, &tsadmin32, sizeof (tsadmin32_t)))
391 return (EFAULT);
392 tsadmin.ts_dpents =
393 (struct tsdpent *)(uintptr_t)tsadmin32.ts_dpents;
394 tsadmin.ts_ndpents = tsadmin32.ts_ndpents;
395 tsadmin.ts_cmd = tsadmin32.ts_cmd;
397 #endif /* _SYSCALL32_IMPL */
399 tsdpsz = (ts_maxumdpri + 1) * sizeof (tsdpent_t);
401 switch (tsadmin.ts_cmd) {
402 case TS_GETDPSIZE:
403 tsadmin.ts_ndpents = ts_maxumdpri + 1;
405 if (get_udatamodel() == DATAMODEL_NATIVE) {
406 if (copyout(&tsadmin, uaddr, sizeof (tsadmin_t)))
407 return (EFAULT);
409 #ifdef _SYSCALL32_IMPL
410 else {
411 /* return tsadmin struct to ILP32 caller */
412 tsadmin32_t tsadmin32;
413 tsadmin32.ts_dpents =
414 (caddr32_t)(uintptr_t)tsadmin.ts_dpents;
415 tsadmin32.ts_ndpents = tsadmin.ts_ndpents;
416 tsadmin32.ts_cmd = tsadmin.ts_cmd;
417 if (copyout(&tsadmin32, uaddr, sizeof (tsadmin32_t)))
418 return (EFAULT);
420 #endif /* _SYSCALL32_IMPL */
421 break;
423 case TS_GETDPTBL:
424 userdpsz = MIN(tsadmin.ts_ndpents * sizeof (tsdpent_t),
425 tsdpsz);
426 if (copyout(ts_dptbl, tsadmin.ts_dpents, userdpsz))
427 return (EFAULT);
429 tsadmin.ts_ndpents = userdpsz / sizeof (tsdpent_t);
431 if (get_udatamodel() == DATAMODEL_NATIVE) {
432 if (copyout(&tsadmin, uaddr, sizeof (tsadmin_t)))
433 return (EFAULT);
435 #ifdef _SYSCALL32_IMPL
436 else {
437 /* return tsadmin struct to ILP32 callers */
438 tsadmin32_t tsadmin32;
439 tsadmin32.ts_dpents =
440 (caddr32_t)(uintptr_t)tsadmin.ts_dpents;
441 tsadmin32.ts_ndpents = tsadmin.ts_ndpents;
442 tsadmin32.ts_cmd = tsadmin.ts_cmd;
443 if (copyout(&tsadmin32, uaddr, sizeof (tsadmin32_t)))
444 return (EFAULT);
446 #endif /* _SYSCALL32_IMPL */
447 break;
449 case TS_SETDPTBL:
451 * We require that the requesting process has sufficient
452 * priveleges. We also require that the table supplied by
453 * the user exactly match the current ts_dptbl in size.
455 if (secpolicy_dispadm(reqpcredp) != 0)
456 return (EPERM);
458 if (tsadmin.ts_ndpents * sizeof (tsdpent_t) != tsdpsz) {
459 return (EINVAL);
463 * We read the user supplied table into a temporary buffer
464 * where it is validated before being copied over the
465 * ts_dptbl.
467 tmpdpp = kmem_alloc(tsdpsz, KM_SLEEP);
468 if (copyin((caddr_t)tsadmin.ts_dpents, (caddr_t)tmpdpp,
469 tsdpsz)) {
470 kmem_free(tmpdpp, tsdpsz);
471 return (EFAULT);
473 for (i = 0; i < tsadmin.ts_ndpents; i++) {
476 * Validate the user supplied values. All we are doing
477 * here is verifying that the values are within their
478 * allowable ranges and will not panic the system. We
479 * make no attempt to ensure that the resulting
480 * configuration makes sense or results in reasonable
481 * performance.
483 if (tmpdpp[i].ts_quantum <= 0) {
484 kmem_free(tmpdpp, tsdpsz);
485 return (EINVAL);
487 if (tmpdpp[i].ts_tqexp > ts_maxumdpri ||
488 tmpdpp[i].ts_tqexp < 0) {
489 kmem_free(tmpdpp, tsdpsz);
490 return (EINVAL);
492 if (tmpdpp[i].ts_slpret > ts_maxumdpri ||
493 tmpdpp[i].ts_slpret < 0) {
494 kmem_free(tmpdpp, tsdpsz);
495 return (EINVAL);
497 if (tmpdpp[i].ts_maxwait < 0) {
498 kmem_free(tmpdpp, tsdpsz);
499 return (EINVAL);
501 if (tmpdpp[i].ts_lwait > ts_maxumdpri ||
502 tmpdpp[i].ts_lwait < 0) {
503 kmem_free(tmpdpp, tsdpsz);
504 return (EINVAL);
509 * Copy the user supplied values over the current ts_dptbl
510 * values. The ts_globpri member is read-only so we don't
511 * overwrite it.
513 mutex_enter(&ts_dptblock);
514 for (i = 0; i < tsadmin.ts_ndpents; i++) {
515 ts_dptbl[i].ts_quantum = tmpdpp[i].ts_quantum;
516 ts_dptbl[i].ts_tqexp = tmpdpp[i].ts_tqexp;
517 ts_dptbl[i].ts_slpret = tmpdpp[i].ts_slpret;
518 ts_dptbl[i].ts_maxwait = tmpdpp[i].ts_maxwait;
519 ts_dptbl[i].ts_lwait = tmpdpp[i].ts_lwait;
521 mutex_exit(&ts_dptblock);
522 kmem_free(tmpdpp, tsdpsz);
523 break;
525 default:
526 return (EINVAL);
528 return (0);
533 * Allocate a time-sharing class specific thread structure and
534 * initialize it with the parameters supplied. Also move the thread
535 * to specified time-sharing priority.
537 static int
538 ts_enterclass(kthread_t *t, id_t cid, void *parmsp,
539 cred_t *reqpcredp, void *bufp)
541 tsparms_t *tsparmsp = (tsparms_t *)parmsp;
542 tsproc_t *tspp;
543 pri_t reqtsuprilim;
544 pri_t reqtsupri;
545 static uint32_t tspexists = 0; /* set on first occurrence of */
546 /* a time-sharing process */
548 tspp = (tsproc_t *)bufp;
549 ASSERT(tspp != NULL);
552 * Initialize the tsproc structure.
554 tspp->ts_cpupri = tsmedumdpri;
555 if (cid == ia_cid) {
557 * Check to make sure caller is either privileged or the
558 * window system. When the window system is converted
559 * to using privileges, the second check can go away.
561 if (reqpcredp != NULL && !groupmember(IA_gid, reqpcredp) &&
562 secpolicy_setpriority(reqpcredp) != 0)
563 return (EPERM);
565 * Belongs to IA "class", so set appropriate flags.
566 * Mark as 'on' so it will not be a swap victim
567 * while forking.
569 tspp->ts_flags = TSIA | TSIASET;
570 tspp->ts_boost = ia_boost;
571 } else {
572 tspp->ts_flags = 0;
573 tspp->ts_boost = 0;
576 if (tsparmsp == NULL) {
578 * Use default values.
580 tspp->ts_uprilim = tspp->ts_upri = 0;
581 tspp->ts_nice = NZERO;
582 } else {
584 * Use supplied values.
586 if (tsparmsp->ts_uprilim == TS_NOCHANGE)
587 reqtsuprilim = 0;
588 else {
589 if (tsparmsp->ts_uprilim > 0 &&
590 secpolicy_setpriority(reqpcredp) != 0)
591 return (EPERM);
592 reqtsuprilim = tsparmsp->ts_uprilim;
595 if (tsparmsp->ts_upri == TS_NOCHANGE) {
596 reqtsupri = reqtsuprilim;
597 } else {
598 if (tsparmsp->ts_upri > 0 &&
599 secpolicy_setpriority(reqpcredp) != 0)
600 return (EPERM);
602 * Set the user priority to the requested value
603 * or the upri limit, whichever is lower.
605 reqtsupri = tsparmsp->ts_upri;
606 if (reqtsupri > reqtsuprilim)
607 reqtsupri = reqtsuprilim;
611 tspp->ts_uprilim = reqtsuprilim;
612 tspp->ts_upri = reqtsupri;
613 tspp->ts_nice = NZERO - (NZERO * reqtsupri) / ts_maxupri;
615 TS_NEWUMDPRI(tspp);
617 tspp->ts_dispwait = 0;
618 tspp->ts_timeleft = ts_dptbl[tspp->ts_cpupri].ts_quantum;
619 tspp->ts_tp = t;
620 cpucaps_sc_init(&tspp->ts_caps);
623 * Reset priority. Process goes to a "user mode" priority
624 * here regardless of whether or not it has slept since
625 * entering the kernel.
627 thread_lock(t); /* get dispatcher lock on thread */
628 t->t_clfuncs = &(sclass[cid].cl_funcs->thread);
629 t->t_cid = cid;
630 t->t_cldata = (void *)tspp;
631 t->t_schedflag &= ~TS_RUNQMATCH;
632 ts_change_priority(t, tspp);
633 thread_unlock(t);
636 * Link new structure into tsproc list.
638 TS_LIST_INSERT(tspp);
641 * If this is the first time-sharing thread to occur since
642 * boot we set up the initial call to ts_update() here.
643 * Use an atomic compare-and-swap since that's easier and
644 * faster than a mutex (but check with an ordinary load first
645 * since most of the time this will already be done).
647 if (tspexists == 0 && atomic_cas_32(&tspexists, 0, 1) == 0)
648 (void) timeout(ts_update, NULL, hz);
650 return (0);
655 * Free tsproc structure of thread.
657 static void
658 ts_exitclass(void *procp)
660 tsproc_t *tspp = (tsproc_t *)procp;
662 /* Remove tsproc_t structure from list */
663 TS_LIST_DELETE(tspp);
664 kmem_free(tspp, sizeof (tsproc_t));
667 /* ARGSUSED */
668 static int
669 ts_canexit(kthread_t *t, cred_t *cred)
672 * A thread can always leave a TS/IA class
674 return (0);
677 static int
678 ts_fork(kthread_t *t, kthread_t *ct, void *bufp)
680 tsproc_t *ptspp; /* ptr to parent's tsproc structure */
681 tsproc_t *ctspp; /* ptr to child's tsproc structure */
683 ASSERT(MUTEX_HELD(&ttoproc(t)->p_lock));
685 ctspp = (tsproc_t *)bufp;
686 ASSERT(ctspp != NULL);
687 ptspp = (tsproc_t *)t->t_cldata;
689 * Initialize child's tsproc structure.
691 thread_lock(t);
692 ctspp->ts_timeleft = ts_dptbl[ptspp->ts_cpupri].ts_quantum;
693 ctspp->ts_cpupri = ptspp->ts_cpupri;
694 ctspp->ts_boost = ptspp->ts_boost;
695 ctspp->ts_uprilim = ptspp->ts_uprilim;
696 ctspp->ts_upri = ptspp->ts_upri;
697 TS_NEWUMDPRI(ctspp);
698 ctspp->ts_nice = ptspp->ts_nice;
699 ctspp->ts_dispwait = 0;
700 ctspp->ts_flags = ptspp->ts_flags & ~(TSKPRI | TSBACKQ | TSRESTORE);
701 ctspp->ts_tp = ct;
702 cpucaps_sc_init(&ctspp->ts_caps);
703 thread_unlock(t);
706 * Link new structure into tsproc list.
708 ct->t_cldata = (void *)ctspp;
709 TS_LIST_INSERT(ctspp);
710 return (0);
715 * Child is placed at back of dispatcher queue and parent gives
716 * up processor so that the child runs first after the fork.
717 * This allows the child immediately execing to break the multiple
718 * use of copy on write pages with no disk home. The parent will
719 * get to steal them back rather than uselessly copying them.
721 static void
722 ts_forkret(kthread_t *t, kthread_t *ct)
724 proc_t *pp = ttoproc(t);
725 proc_t *cp = ttoproc(ct);
726 tsproc_t *tspp;
728 ASSERT(t == curthread);
729 ASSERT(MUTEX_HELD(&pidlock));
732 * Grab the child's p_lock before dropping pidlock to ensure
733 * the process does not disappear before we set it running.
735 mutex_enter(&cp->p_lock);
736 continuelwps(cp);
737 mutex_exit(&cp->p_lock);
739 mutex_enter(&pp->p_lock);
740 mutex_exit(&pidlock);
741 continuelwps(pp);
743 thread_lock(t);
744 tspp = (tsproc_t *)(t->t_cldata);
745 tspp->ts_cpupri = ts_dptbl[tspp->ts_cpupri].ts_tqexp;
746 TS_NEWUMDPRI(tspp);
747 tspp->ts_timeleft = ts_dptbl[tspp->ts_cpupri].ts_quantum;
748 tspp->ts_dispwait = 0;
749 t->t_pri = ts_dptbl[tspp->ts_umdpri].ts_globpri;
750 ASSERT(t->t_pri >= 0 && t->t_pri <= ts_maxglobpri);
751 tspp->ts_flags &= ~TSKPRI;
752 THREAD_TRANSITION(t);
753 ts_setrun(t);
754 thread_unlock(t);
756 * Safe to drop p_lock now since since it is safe to change
757 * the scheduling class after this point.
759 mutex_exit(&pp->p_lock);
761 swtch();
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.
771 static int
772 ts_getclinfo(void *infop)
774 tsinfo_t *tsinfop = (tsinfo_t *)infop;
775 tsinfop->ts_maxupri = ts_maxupri;
776 return (0);
779 static int
780 ia_getclinfo(void *infop)
782 iainfo_t *iainfop = (iainfo_t *)infop;
783 iainfop->ia_maxupri = ia_maxupri;
784 return (0);
789 * Return the user mode scheduling priority range.
791 static int
792 ts_getclpri(pcpri_t *pcprip)
794 pcprip->pc_clpmax = ts_maxupri;
795 pcprip->pc_clpmin = -ts_maxupri;
796 return (0);
800 static int
801 ia_getclpri(pcpri_t *pcprip)
803 pcprip->pc_clpmax = ia_maxupri;
804 pcprip->pc_clpmin = -ia_maxupri;
805 return (0);
809 static void
810 ts_nullsys()
815 * Get the time-sharing parameters of the thread pointed to by
816 * tsprocp into the buffer pointed to by tsparmsp. ts_parmsget()
817 * is called for TS threads, and ia_parmsget() is called for IA
818 * threads.
820 static void
821 ts_parmsget(kthread_t *t, void *parmsp)
823 tsproc_t *tspp = (tsproc_t *)t->t_cldata;
824 tsparms_t *tsparmsp = (tsparms_t *)parmsp;
826 tsparmsp->ts_uprilim = tspp->ts_uprilim;
827 tsparmsp->ts_upri = tspp->ts_upri;
830 static void
831 ia_parmsget(kthread_t *t, void *parmsp)
833 tsproc_t *tspp = (tsproc_t *)t->t_cldata;
834 iaparms_t *iaparmsp = (iaparms_t *)parmsp;
836 iaparmsp->ia_uprilim = tspp->ts_uprilim;
837 iaparmsp->ia_upri = tspp->ts_upri;
838 if (tspp->ts_flags & TSIASET)
839 iaparmsp->ia_mode = IA_SET_INTERACTIVE;
840 else
841 iaparmsp->ia_mode = IA_INTERACTIVE_OFF;
846 * Check the validity of the time-sharing parameters in the buffer
847 * pointed to by tsparmsp.
848 * ts_parmsin() is called for TS threads, and ia_parmsin() is called
849 * for IA threads.
851 static int
852 ts_parmsin(void *parmsp)
854 tsparms_t *tsparmsp = (tsparms_t *)parmsp;
856 * Check validity of parameters.
858 if ((tsparmsp->ts_uprilim > ts_maxupri ||
859 tsparmsp->ts_uprilim < -ts_maxupri) &&
860 tsparmsp->ts_uprilim != TS_NOCHANGE)
861 return (EINVAL);
863 if ((tsparmsp->ts_upri > ts_maxupri ||
864 tsparmsp->ts_upri < -ts_maxupri) &&
865 tsparmsp->ts_upri != TS_NOCHANGE)
866 return (EINVAL);
868 return (0);
871 static int
872 ia_parmsin(void *parmsp)
874 iaparms_t *iaparmsp = (iaparms_t *)parmsp;
876 if ((iaparmsp->ia_uprilim > ia_maxupri ||
877 iaparmsp->ia_uprilim < -ia_maxupri) &&
878 iaparmsp->ia_uprilim != IA_NOCHANGE) {
879 return (EINVAL);
882 if ((iaparmsp->ia_upri > ia_maxupri ||
883 iaparmsp->ia_upri < -ia_maxupri) &&
884 iaparmsp->ia_upri != IA_NOCHANGE) {
885 return (EINVAL);
888 return (0);
893 * Check the validity of the time-sharing parameters in the pc_vaparms_t
894 * structure vaparmsp and put them in the buffer pointed to by tsparmsp.
895 * pc_vaparms_t contains (key, value) pairs of parameter.
896 * ts_vaparmsin() is called for TS threads, and ia_vaparmsin() is called
897 * for IA threads. ts_vaparmsin() is the variable parameter version of
898 * ts_parmsin() and ia_vaparmsin() is the variable parameter version of
899 * ia_parmsin().
901 static int
902 ts_vaparmsin(void *parmsp, pc_vaparms_t *vaparmsp)
904 tsparms_t *tsparmsp = (tsparms_t *)parmsp;
905 int priflag = 0;
906 int limflag = 0;
907 uint_t cnt;
908 pc_vaparm_t *vpp = &vaparmsp->pc_parms[0];
912 * TS_NOCHANGE (-32768) is outside of the range of values for
913 * ts_uprilim and ts_upri. If the structure tsparms_t is changed,
914 * TS_NOCHANGE should be replaced by a flag word (in the same manner
915 * as in rt.c).
917 tsparmsp->ts_uprilim = TS_NOCHANGE;
918 tsparmsp->ts_upri = TS_NOCHANGE;
921 * Get the varargs parameter and check validity of parameters.
923 if (vaparmsp->pc_vaparmscnt > PC_VAPARMCNT)
924 return (EINVAL);
926 for (cnt = 0; cnt < vaparmsp->pc_vaparmscnt; cnt++, vpp++) {
928 switch (vpp->pc_key) {
929 case TS_KY_UPRILIM:
930 if (limflag++)
931 return (EINVAL);
932 tsparmsp->ts_uprilim = (pri_t)vpp->pc_parm;
933 if (tsparmsp->ts_uprilim > ts_maxupri ||
934 tsparmsp->ts_uprilim < -ts_maxupri)
935 return (EINVAL);
936 break;
938 case TS_KY_UPRI:
939 if (priflag++)
940 return (EINVAL);
941 tsparmsp->ts_upri = (pri_t)vpp->pc_parm;
942 if (tsparmsp->ts_upri > ts_maxupri ||
943 tsparmsp->ts_upri < -ts_maxupri)
944 return (EINVAL);
945 break;
947 default:
948 return (EINVAL);
952 if (vaparmsp->pc_vaparmscnt == 0) {
954 * Use default parameters.
956 tsparmsp->ts_upri = tsparmsp->ts_uprilim = 0;
959 return (0);
962 static int
963 ia_vaparmsin(void *parmsp, pc_vaparms_t *vaparmsp)
965 iaparms_t *iaparmsp = (iaparms_t *)parmsp;
966 int priflag = 0;
967 int limflag = 0;
968 int mflag = 0;
969 uint_t cnt;
970 pc_vaparm_t *vpp = &vaparmsp->pc_parms[0];
973 * IA_NOCHANGE (-32768) is outside of the range of values for
974 * ia_uprilim, ia_upri and ia_mode. If the structure iaparms_t is
975 * changed, IA_NOCHANGE should be replaced by a flag word (in the
976 * same manner as in rt.c).
978 iaparmsp->ia_uprilim = IA_NOCHANGE;
979 iaparmsp->ia_upri = IA_NOCHANGE;
980 iaparmsp->ia_mode = IA_NOCHANGE;
983 * Get the varargs parameter and check validity of parameters.
985 if (vaparmsp->pc_vaparmscnt > PC_VAPARMCNT)
986 return (EINVAL);
988 for (cnt = 0; cnt < vaparmsp->pc_vaparmscnt; cnt++, vpp++) {
990 switch (vpp->pc_key) {
991 case IA_KY_UPRILIM:
992 if (limflag++)
993 return (EINVAL);
994 iaparmsp->ia_uprilim = (pri_t)vpp->pc_parm;
995 if (iaparmsp->ia_uprilim > ia_maxupri ||
996 iaparmsp->ia_uprilim < -ia_maxupri)
997 return (EINVAL);
998 break;
1000 case IA_KY_UPRI:
1001 if (priflag++)
1002 return (EINVAL);
1003 iaparmsp->ia_upri = (pri_t)vpp->pc_parm;
1004 if (iaparmsp->ia_upri > ia_maxupri ||
1005 iaparmsp->ia_upri < -ia_maxupri)
1006 return (EINVAL);
1007 break;
1009 case IA_KY_MODE:
1010 if (mflag++)
1011 return (EINVAL);
1012 iaparmsp->ia_mode = (int)vpp->pc_parm;
1013 if (iaparmsp->ia_mode != IA_SET_INTERACTIVE &&
1014 iaparmsp->ia_mode != IA_INTERACTIVE_OFF)
1015 return (EINVAL);
1016 break;
1018 default:
1019 return (EINVAL);
1023 if (vaparmsp->pc_vaparmscnt == 0) {
1025 * Use default parameters.
1027 iaparmsp->ia_upri = iaparmsp->ia_uprilim = 0;
1028 iaparmsp->ia_mode = IA_SET_INTERACTIVE;
1031 return (0);
1035 * Nothing to do here but return success.
1037 /* ARGSUSED */
1038 static int
1039 ts_parmsout(void *parmsp, pc_vaparms_t *vaparmsp)
1041 return (0);
1046 * Copy all selected time-sharing class parameters to the user.
1047 * The parameters are specified by a key.
1049 static int
1050 ts_vaparmsout(void *prmsp, pc_vaparms_t *vaparmsp)
1052 tsparms_t *tsprmsp = (tsparms_t *)prmsp;
1053 int priflag = 0;
1054 int limflag = 0;
1055 uint_t cnt;
1056 pc_vaparm_t *vpp = &vaparmsp->pc_parms[0];
1058 ASSERT(MUTEX_NOT_HELD(&curproc->p_lock));
1060 if (vaparmsp->pc_vaparmscnt > PC_VAPARMCNT)
1061 return (EINVAL);
1063 for (cnt = 0; cnt < vaparmsp->pc_vaparmscnt; cnt++, vpp++) {
1065 switch (vpp->pc_key) {
1066 case TS_KY_UPRILIM:
1067 if (limflag++)
1068 return (EINVAL);
1069 if (copyout(&tsprmsp->ts_uprilim,
1070 (caddr_t)(uintptr_t)vpp->pc_parm, sizeof (pri_t)))
1071 return (EFAULT);
1072 break;
1074 case TS_KY_UPRI:
1075 if (priflag++)
1076 return (EINVAL);
1077 if (copyout(&tsprmsp->ts_upri,
1078 (caddr_t)(uintptr_t)vpp->pc_parm, sizeof (pri_t)))
1079 return (EFAULT);
1080 break;
1082 default:
1083 return (EINVAL);
1087 return (0);
1092 * Copy all selected interactive class parameters to the user.
1093 * The parameters are specified by a key.
1095 static int
1096 ia_vaparmsout(void *prmsp, pc_vaparms_t *vaparmsp)
1098 iaparms_t *iaprmsp = (iaparms_t *)prmsp;
1099 int priflag = 0;
1100 int limflag = 0;
1101 int mflag = 0;
1102 uint_t cnt;
1103 pc_vaparm_t *vpp = &vaparmsp->pc_parms[0];
1105 ASSERT(MUTEX_NOT_HELD(&curproc->p_lock));
1107 if (vaparmsp->pc_vaparmscnt > PC_VAPARMCNT)
1108 return (EINVAL);
1110 for (cnt = 0; cnt < vaparmsp->pc_vaparmscnt; cnt++, vpp++) {
1112 switch (vpp->pc_key) {
1113 case IA_KY_UPRILIM:
1114 if (limflag++)
1115 return (EINVAL);
1116 if (copyout(&iaprmsp->ia_uprilim,
1117 (caddr_t)(uintptr_t)vpp->pc_parm, sizeof (pri_t)))
1118 return (EFAULT);
1119 break;
1121 case IA_KY_UPRI:
1122 if (priflag++)
1123 return (EINVAL);
1124 if (copyout(&iaprmsp->ia_upri,
1125 (caddr_t)(uintptr_t)vpp->pc_parm, sizeof (pri_t)))
1126 return (EFAULT);
1127 break;
1129 case IA_KY_MODE:
1130 if (mflag++)
1131 return (EINVAL);
1132 if (copyout(&iaprmsp->ia_mode,
1133 (caddr_t)(uintptr_t)vpp->pc_parm, sizeof (int)))
1134 return (EFAULT);
1135 break;
1137 default:
1138 return (EINVAL);
1141 return (0);
1146 * Set the scheduling parameters of the thread pointed to by tsprocp
1147 * to those specified in the buffer pointed to by tsparmsp.
1148 * ts_parmsset() is called for TS threads, and ia_parmsset() is
1149 * called for IA threads.
1151 /* ARGSUSED */
1152 static int
1153 ts_parmsset(kthread_t *tx, void *parmsp, id_t reqpcid, cred_t *reqpcredp)
1155 char nice;
1156 pri_t reqtsuprilim;
1157 pri_t reqtsupri;
1158 tsparms_t *tsparmsp = (tsparms_t *)parmsp;
1159 tsproc_t *tspp = (tsproc_t *)tx->t_cldata;
1161 ASSERT(MUTEX_HELD(&(ttoproc(tx))->p_lock));
1163 if (tsparmsp->ts_uprilim == TS_NOCHANGE)
1164 reqtsuprilim = tspp->ts_uprilim;
1165 else
1166 reqtsuprilim = tsparmsp->ts_uprilim;
1168 if (tsparmsp->ts_upri == TS_NOCHANGE)
1169 reqtsupri = tspp->ts_upri;
1170 else
1171 reqtsupri = tsparmsp->ts_upri;
1174 * Make sure the user priority doesn't exceed the upri limit.
1176 if (reqtsupri > reqtsuprilim)
1177 reqtsupri = reqtsuprilim;
1180 * Basic permissions enforced by generic kernel code
1181 * for all classes require that a thread attempting
1182 * to change the scheduling parameters of a target
1183 * thread be privileged or have a real or effective
1184 * UID matching that of the target thread. We are not
1185 * called unless these basic permission checks have
1186 * already passed. The time-sharing class requires in
1187 * addition that the calling thread be privileged if it
1188 * is attempting to raise the upri limit above its current
1189 * value This may have been checked previously but if our
1190 * caller passed us a non-NULL credential pointer we assume
1191 * it hasn't and we check it here.
1193 if (reqpcredp != NULL &&
1194 reqtsuprilim > tspp->ts_uprilim &&
1195 secpolicy_raisepriority(reqpcredp) != 0)
1196 return (EPERM);
1199 * Set ts_nice to the nice value corresponding to the user
1200 * priority we are setting. Note that setting the nice field
1201 * of the parameter struct won't affect upri or nice.
1203 nice = NZERO - (reqtsupri * NZERO) / ts_maxupri;
1204 if (nice >= 2 * NZERO)
1205 nice = 2 * NZERO - 1;
1207 thread_lock(tx);
1209 tspp->ts_uprilim = reqtsuprilim;
1210 tspp->ts_upri = reqtsupri;
1211 TS_NEWUMDPRI(tspp);
1212 tspp->ts_nice = nice;
1214 if ((tspp->ts_flags & TSKPRI) != 0) {
1215 thread_unlock(tx);
1216 return (0);
1219 tspp->ts_dispwait = 0;
1220 ts_change_priority(tx, tspp);
1221 thread_unlock(tx);
1222 return (0);
1226 static int
1227 ia_parmsset(kthread_t *tx, void *parmsp, id_t reqpcid, cred_t *reqpcredp)
1229 tsproc_t *tspp = (tsproc_t *)tx->t_cldata;
1230 iaparms_t *iaparmsp = (iaparms_t *)parmsp;
1231 proc_t *p;
1232 pid_t pid, pgid, sid;
1233 pid_t on, off;
1234 struct stdata *stp;
1235 int sess_held;
1238 * Handle user priority changes
1240 if (iaparmsp->ia_mode == IA_NOCHANGE)
1241 return (ts_parmsset(tx, parmsp, reqpcid, reqpcredp));
1244 * Check permissions for changing modes.
1247 if (reqpcredp != NULL && !groupmember(IA_gid, reqpcredp) &&
1248 secpolicy_raisepriority(reqpcredp) != 0) {
1250 * Silently fail in case this is just a priocntl
1251 * call with upri and uprilim set to IA_NOCHANGE.
1253 return (0);
1256 ASSERT(MUTEX_HELD(&pidlock));
1257 if ((p = ttoproc(tx)) == NULL) {
1258 return (0);
1260 ASSERT(MUTEX_HELD(&p->p_lock));
1261 if (p->p_stat == SIDL) {
1262 return (0);
1264 pid = p->p_pid;
1265 sid = p->p_sessp->s_sid;
1266 pgid = p->p_pgrp;
1267 if (iaparmsp->ia_mode == IA_SET_INTERACTIVE) {
1269 * session leaders must be turned on now so all processes
1270 * in the group controlling the tty will be turned on or off.
1271 * if the ia_mode is off for the session leader,
1272 * ia_set_process_group will return without setting the
1273 * processes in the group controlling the tty on.
1275 thread_lock(tx);
1276 tspp->ts_flags |= TSIASET;
1277 thread_unlock(tx);
1279 mutex_enter(&p->p_sessp->s_lock);
1280 sess_held = 1;
1281 if ((pid == sid) && (p->p_sessp->s_vp != NULL) &&
1282 ((stp = p->p_sessp->s_vp->v_stream) != NULL)) {
1283 if ((stp->sd_pgidp != NULL) && (stp->sd_sidp != NULL)) {
1284 pgid = stp->sd_pgidp->pid_id;
1285 sess_held = 0;
1286 mutex_exit(&p->p_sessp->s_lock);
1287 if (iaparmsp->ia_mode ==
1288 IA_SET_INTERACTIVE) {
1289 off = 0;
1290 on = pgid;
1291 } else {
1292 off = pgid;
1293 on = 0;
1295 TRACE_3(TR_FAC_IA, TR_ACTIVE_CHAIN,
1296 "active chain:pid %d gid %d %p",
1297 pid, pgid, p);
1298 ia_set_process_group(sid, off, on);
1301 if (sess_held)
1302 mutex_exit(&p->p_sessp->s_lock);
1304 thread_lock(tx);
1306 if (iaparmsp->ia_mode == IA_SET_INTERACTIVE) {
1307 tspp->ts_flags |= TSIASET;
1308 tspp->ts_boost = ia_boost;
1309 } else {
1310 tspp->ts_flags &= ~TSIASET;
1311 tspp->ts_boost = -ia_boost;
1313 thread_unlock(tx);
1315 return (ts_parmsset(tx, parmsp, reqpcid, reqpcredp));
1318 static void
1319 ts_exit(kthread_t *t)
1321 tsproc_t *tspp;
1323 if (CPUCAPS_ON()) {
1325 * A thread could be exiting in between clock ticks,
1326 * so we need to calculate how much CPU time it used
1327 * since it was charged last time.
1329 * CPU caps are not enforced on exiting processes - it is
1330 * usually desirable to exit as soon as possible to free
1331 * resources.
1333 thread_lock(t);
1334 tspp = (tsproc_t *)t->t_cldata;
1335 (void) cpucaps_charge(t, &tspp->ts_caps, CPUCAPS_CHARGE_ONLY);
1336 thread_unlock(t);
1341 * Return the global scheduling priority that would be assigned
1342 * to a thread entering the time-sharing class with the ts_upri.
1344 static pri_t
1345 ts_globpri(kthread_t *t)
1347 tsproc_t *tspp;
1348 pri_t tspri;
1350 ASSERT(MUTEX_HELD(&ttoproc(t)->p_lock));
1351 tspp = (tsproc_t *)t->t_cldata;
1352 tspri = tsmedumdpri + tspp->ts_upri;
1353 if (tspri > ts_maxumdpri)
1354 tspri = ts_maxumdpri;
1355 else if (tspri < 0)
1356 tspri = 0;
1357 return (ts_dptbl[tspri].ts_globpri);
1361 * Arrange for thread to be placed in appropriate location
1362 * on dispatcher queue.
1364 * This is called with the current thread in TS_ONPROC and locked.
1366 static void
1367 ts_preempt(kthread_t *t)
1369 tsproc_t *tspp = (tsproc_t *)(t->t_cldata);
1370 klwp_t *lwp = curthread->t_lwp;
1371 pri_t oldpri = t->t_pri;
1373 ASSERT(t == curthread);
1374 ASSERT(THREAD_LOCK_HELD(curthread));
1377 * If preempted in the kernel, make sure the thread has
1378 * a kernel priority if needed.
1380 if (!(tspp->ts_flags & TSKPRI) && lwp != NULL && t->t_kpri_req) {
1381 tspp->ts_flags |= TSKPRI;
1382 THREAD_CHANGE_PRI(t, ts_kmdpris[0]);
1383 ASSERT(t->t_pri >= 0 && t->t_pri <= ts_maxglobpri);
1384 t->t_trapret = 1; /* so ts_trapret will run */
1385 aston(t);
1389 * This thread may be placed on wait queue by CPU Caps. In this case we
1390 * do not need to do anything until it is removed from the wait queue.
1391 * Do not enforce CPU caps on threads running at a kernel priority
1393 if (CPUCAPS_ON()) {
1394 (void) cpucaps_charge(t, &tspp->ts_caps,
1395 CPUCAPS_CHARGE_ENFORCE);
1396 if (!(tspp->ts_flags & TSKPRI) && CPUCAPS_ENFORCE(t))
1397 return;
1401 * Check to see if we're doing "preemption control" here. If
1402 * we are, and if the user has requested that this thread not
1403 * be preempted, and if preemptions haven't been put off for
1404 * too long, let the preemption happen here but try to make
1405 * sure the thread is rescheduled as soon as possible. We do
1406 * this by putting it on the front of the highest priority run
1407 * queue in the TS class. If the preemption has been put off
1408 * for too long, clear the "nopreempt" bit and let the thread
1409 * be preempted.
1411 if (t->t_schedctl && schedctl_get_nopreempt(t)) {
1412 if (tspp->ts_timeleft > -SC_MAX_TICKS) {
1413 DTRACE_SCHED1(schedctl__nopreempt, kthread_t *, t);
1414 if (!(tspp->ts_flags & TSKPRI)) {
1416 * If not already remembered, remember current
1417 * priority for restoration in ts_yield().
1419 if (!(tspp->ts_flags & TSRESTORE)) {
1420 tspp->ts_scpri = t->t_pri;
1421 tspp->ts_flags |= TSRESTORE;
1423 THREAD_CHANGE_PRI(t, ts_maxumdpri);
1425 schedctl_set_yield(t, 1);
1426 setfrontdq(t);
1427 goto done;
1428 } else {
1429 if (tspp->ts_flags & TSRESTORE) {
1430 THREAD_CHANGE_PRI(t, tspp->ts_scpri);
1431 tspp->ts_flags &= ~TSRESTORE;
1433 schedctl_set_nopreempt(t, 0);
1434 DTRACE_SCHED1(schedctl__preempt, kthread_t *, t);
1436 * Fall through and be preempted below.
1441 if ((tspp->ts_flags & (TSBACKQ|TSKPRI)) == TSBACKQ) {
1442 tspp->ts_timeleft = ts_dptbl[tspp->ts_cpupri].ts_quantum;
1443 tspp->ts_dispwait = 0;
1444 tspp->ts_flags &= ~TSBACKQ;
1445 setbackdq(t);
1446 } else if ((tspp->ts_flags & (TSBACKQ|TSKPRI)) == (TSBACKQ|TSKPRI)) {
1447 tspp->ts_flags &= ~TSBACKQ;
1448 setbackdq(t);
1449 } else {
1450 setfrontdq(t);
1453 done:
1454 TRACE_2(TR_FAC_DISP, TR_PREEMPT,
1455 "preempt:tid %p old pri %d", t, oldpri);
1458 static void
1459 ts_setrun(kthread_t *t)
1461 tsproc_t *tspp = (tsproc_t *)(t->t_cldata);
1463 ASSERT(THREAD_LOCK_HELD(t)); /* t should be in transition */
1465 if (tspp->ts_dispwait > ts_dptbl[tspp->ts_umdpri].ts_maxwait) {
1466 tspp->ts_cpupri = ts_dptbl[tspp->ts_cpupri].ts_slpret;
1467 TS_NEWUMDPRI(tspp);
1468 tspp->ts_timeleft = ts_dptbl[tspp->ts_cpupri].ts_quantum;
1469 tspp->ts_dispwait = 0;
1470 if ((tspp->ts_flags & TSKPRI) == 0) {
1471 THREAD_CHANGE_PRI(t,
1472 ts_dptbl[tspp->ts_umdpri].ts_globpri);
1473 ASSERT(t->t_pri >= 0 && t->t_pri <= ts_maxglobpri);
1477 tspp->ts_flags &= ~TSBACKQ;
1479 if (tspp->ts_flags & TSIA) {
1480 if (tspp->ts_flags & TSIASET)
1481 setfrontdq(t);
1482 else
1483 setbackdq(t);
1484 } else {
1485 if (t->t_disp_time != ddi_get_lbolt())
1486 setbackdq(t);
1487 else
1488 setfrontdq(t);
1494 * Prepare thread for sleep. We reset the thread priority so it will
1495 * run at the kernel priority level when it wakes up.
1497 static void
1498 ts_sleep(kthread_t *t)
1500 tsproc_t *tspp = (tsproc_t *)(t->t_cldata);
1501 int flags;
1502 pri_t old_pri = t->t_pri;
1504 ASSERT(t == curthread);
1505 ASSERT(THREAD_LOCK_HELD(t));
1508 * Account for time spent on CPU before going to sleep.
1510 (void) CPUCAPS_CHARGE(t, &tspp->ts_caps, CPUCAPS_CHARGE_ENFORCE);
1512 flags = tspp->ts_flags;
1513 if (t->t_kpri_req) {
1514 tspp->ts_flags = flags | TSKPRI;
1515 THREAD_CHANGE_PRI(t, ts_kmdpris[0]);
1516 ASSERT(t->t_pri >= 0 && t->t_pri <= ts_maxglobpri);
1517 t->t_trapret = 1; /* so ts_trapret will run */
1518 aston(t);
1519 } else if (tspp->ts_dispwait > ts_dptbl[tspp->ts_umdpri].ts_maxwait) {
1521 * If thread has blocked in the kernel (as opposed to
1522 * being merely preempted), recompute the user mode priority.
1524 tspp->ts_cpupri = ts_dptbl[tspp->ts_cpupri].ts_slpret;
1525 TS_NEWUMDPRI(tspp);
1526 tspp->ts_timeleft = ts_dptbl[tspp->ts_cpupri].ts_quantum;
1527 tspp->ts_dispwait = 0;
1529 THREAD_CHANGE_PRI(curthread,
1530 ts_dptbl[tspp->ts_umdpri].ts_globpri);
1531 ASSERT(curthread->t_pri >= 0 &&
1532 curthread->t_pri <= ts_maxglobpri);
1533 tspp->ts_flags = flags & ~TSKPRI;
1535 if (DISP_MUST_SURRENDER(curthread))
1536 cpu_surrender(curthread);
1537 } else if (flags & TSKPRI) {
1538 THREAD_CHANGE_PRI(curthread,
1539 ts_dptbl[tspp->ts_umdpri].ts_globpri);
1540 ASSERT(curthread->t_pri >= 0 &&
1541 curthread->t_pri <= ts_maxglobpri);
1542 tspp->ts_flags = flags & ~TSKPRI;
1544 if (DISP_MUST_SURRENDER(curthread))
1545 cpu_surrender(curthread);
1547 TRACE_2(TR_FAC_DISP, TR_SLEEP,
1548 "sleep:tid %p old pri %d", t, old_pri);
1552 * Check for time slice expiration. If time slice has expired
1553 * move thread to priority specified in tsdptbl for time slice expiration
1554 * and set runrun to cause preemption.
1556 static void
1557 ts_tick(kthread_t *t)
1559 tsproc_t *tspp = (tsproc_t *)(t->t_cldata);
1560 bool call_cpu_surrender = false;
1561 pri_t oldpri = t->t_pri;
1563 ASSERT(MUTEX_HELD(&(ttoproc(t))->p_lock));
1565 thread_lock(t);
1568 * Keep track of thread's project CPU usage. Note that projects
1569 * get charged even when threads are running in the kernel.
1571 if (CPUCAPS_ON()) {
1572 call_cpu_surrender = cpucaps_charge(t, &tspp->ts_caps,
1573 CPUCAPS_CHARGE_ENFORCE) && !(tspp->ts_flags & TSKPRI);
1576 if ((tspp->ts_flags & TSKPRI) == 0) {
1577 if (--tspp->ts_timeleft <= 0) {
1578 pri_t new_pri;
1581 * If we're doing preemption control and trying to
1582 * avoid preempting this thread, just note that
1583 * the thread should yield soon and let it keep
1584 * running (unless it's been a while).
1586 if (t->t_schedctl && schedctl_get_nopreempt(t)) {
1587 if (tspp->ts_timeleft > -SC_MAX_TICKS) {
1588 DTRACE_SCHED1(schedctl__nopreempt,
1589 kthread_t *, t);
1590 schedctl_set_yield(t, 1);
1591 thread_unlock_nopreempt(t);
1592 return;
1595 tspp->ts_flags &= ~TSRESTORE;
1596 tspp->ts_cpupri = ts_dptbl[tspp->ts_cpupri].ts_tqexp;
1597 TS_NEWUMDPRI(tspp);
1598 tspp->ts_dispwait = 0;
1599 new_pri = ts_dptbl[tspp->ts_umdpri].ts_globpri;
1600 ASSERT(new_pri >= 0 && new_pri <= ts_maxglobpri);
1602 * When the priority of a thread is changed,
1603 * it may be necessary to adjust its position
1604 * on a sleep queue or dispatch queue.
1605 * The function thread_change_pri accomplishes
1606 * this.
1608 if (thread_change_pri(t, new_pri, 0)) {
1609 tspp->ts_timeleft =
1610 ts_dptbl[tspp->ts_cpupri].ts_quantum;
1611 } else {
1612 call_cpu_surrender = true;
1614 TRACE_2(TR_FAC_DISP, TR_TICK,
1615 "tick:tid %p old pri %d", t, oldpri);
1616 } else if (t->t_state == TS_ONPROC &&
1617 t->t_pri < t->t_disp_queue->disp_maxrunpri) {
1618 call_cpu_surrender = true;
1622 if (call_cpu_surrender) {
1623 tspp->ts_flags |= TSBACKQ;
1624 cpu_surrender(t);
1627 thread_unlock_nopreempt(t); /* clock thread can't be preempted */
1632 * If thread is currently at a kernel mode priority (has slept)
1633 * we assign it the appropriate user mode priority and time quantum
1634 * here. If we are lowering the thread's priority below that of
1635 * other runnable threads we will normally set runrun via cpu_surrender() to
1636 * cause preemption.
1638 static void
1639 ts_trapret(kthread_t *t)
1641 tsproc_t *tspp = (tsproc_t *)t->t_cldata;
1642 cpu_t *cp = CPU;
1643 pri_t old_pri = curthread->t_pri;
1645 ASSERT(THREAD_LOCK_HELD(t));
1646 ASSERT(t == curthread);
1647 ASSERT(cp->cpu_dispthread == t);
1648 ASSERT(t->t_state == TS_ONPROC);
1650 t->t_kpri_req = 0;
1651 if (tspp->ts_dispwait > ts_dptbl[tspp->ts_umdpri].ts_maxwait) {
1652 tspp->ts_cpupri = ts_dptbl[tspp->ts_cpupri].ts_slpret;
1653 TS_NEWUMDPRI(tspp);
1654 tspp->ts_timeleft = ts_dptbl[tspp->ts_cpupri].ts_quantum;
1655 tspp->ts_dispwait = 0;
1658 * If thread has blocked in the kernel (as opposed to
1659 * being merely preempted), recompute the user mode priority.
1661 THREAD_CHANGE_PRI(t, ts_dptbl[tspp->ts_umdpri].ts_globpri);
1662 cp->cpu_dispatch_pri = DISP_PRIO(t);
1663 ASSERT(t->t_pri >= 0 && t->t_pri <= ts_maxglobpri);
1664 tspp->ts_flags &= ~TSKPRI;
1666 if (DISP_MUST_SURRENDER(t))
1667 cpu_surrender(t);
1668 } else if (tspp->ts_flags & TSKPRI) {
1670 * If thread has blocked in the kernel (as opposed to
1671 * being merely preempted), recompute the user mode priority.
1673 THREAD_CHANGE_PRI(t, ts_dptbl[tspp->ts_umdpri].ts_globpri);
1674 cp->cpu_dispatch_pri = DISP_PRIO(t);
1675 ASSERT(t->t_pri >= 0 && t->t_pri <= ts_maxglobpri);
1676 tspp->ts_flags &= ~TSKPRI;
1678 if (DISP_MUST_SURRENDER(t))
1679 cpu_surrender(t);
1682 TRACE_2(TR_FAC_DISP, TR_TRAPRET,
1683 "trapret:tid %p old pri %d", t, old_pri);
1688 * Update the ts_dispwait values of all time sharing threads that
1689 * are currently runnable at a user mode priority and bump the priority
1690 * if ts_dispwait exceeds ts_maxwait. Called once per second via
1691 * timeout which we reset here.
1693 * There are several lists of time sharing threads broken up by a hash on
1694 * the thread pointer. Each list has its own lock. This avoids blocking
1695 * all ts_enterclass, ts_fork, and ts_exitclass operations while ts_update
1696 * runs. ts_update traverses each list in turn.
1698 * If multiple threads have their priorities updated to the same value,
1699 * the system implicitly favors the one that is updated first (since it
1700 * winds up first on the run queue). To avoid this unfairness, the
1701 * traversal of threads starts at the list indicated by a marker. When
1702 * threads in more than one list have their priorities updated, the marker
1703 * is moved. This changes the order the threads will be placed on the run
1704 * queue the next time ts_update is called and preserves fairness over the
1705 * long run. The marker doesn't need to be protected by a lock since it's
1706 * only accessed by ts_update, which is inherently single-threaded (only
1707 * one instance can be running at a time).
1709 static void
1710 ts_update(void *arg)
1712 int i;
1713 int new_marker = -1;
1714 static int ts_update_marker;
1717 * Start with the ts_update_marker list, then do the rest.
1719 i = ts_update_marker;
1720 do {
1722 * If this is the first list after the current marker to
1723 * have threads with priorities updated, advance the marker
1724 * to this list for the next time ts_update runs.
1726 if (ts_update_list(i) && new_marker == -1 &&
1727 i != ts_update_marker) {
1728 new_marker = i;
1730 } while ((i = TS_LIST_NEXT(i)) != ts_update_marker);
1732 /* advance marker for next ts_update call */
1733 if (new_marker != -1)
1734 ts_update_marker = new_marker;
1736 (void) timeout(ts_update, arg, hz);
1740 * Updates priority for a list of threads. Returns 1 if the priority of
1741 * one of the threads was actually updated, 0 if none were for various
1742 * reasons (thread is no longer in the TS or IA class, isn't runnable,
1743 * hasn't waited long enough, has the preemption control no-preempt bit
1744 * set, etc.)
1746 static int
1747 ts_update_list(int i)
1749 tsproc_t *tspp;
1750 kthread_t *tx;
1751 int updated = 0;
1753 mutex_enter(&ts_list_lock[i]);
1754 for (tspp = ts_plisthead[i].ts_next; tspp != &ts_plisthead[i];
1755 tspp = tspp->ts_next) {
1756 tx = tspp->ts_tp;
1758 * Lock the thread and verify state.
1760 thread_lock(tx);
1762 * Skip the thread if it is no longer in the TS (or IA) class.
1764 if (tx->t_clfuncs != &ts_classfuncs.thread &&
1765 tx->t_clfuncs != &ia_classfuncs.thread)
1766 goto next;
1767 tspp->ts_dispwait++;
1768 if ((tspp->ts_flags & TSKPRI) != 0)
1769 goto next;
1770 if (tspp->ts_dispwait <= ts_dptbl[tspp->ts_umdpri].ts_maxwait)
1771 goto next;
1772 if (tx->t_schedctl && schedctl_get_nopreempt(tx))
1773 goto next;
1774 if (tx->t_state != TS_RUN && tx->t_state != TS_WAIT &&
1775 (tx->t_state != TS_SLEEP || !ts_sleep_promote)) {
1776 /* make next syscall/trap do CL_TRAPRET */
1777 tx->t_trapret = 1;
1778 aston(tx);
1779 goto next;
1781 tspp->ts_cpupri = ts_dptbl[tspp->ts_cpupri].ts_lwait;
1782 TS_NEWUMDPRI(tspp);
1783 tspp->ts_dispwait = 0;
1784 updated = 1;
1787 * Only dequeue it if needs to move; otherwise it should
1788 * just round-robin here.
1790 if (tx->t_pri != ts_dptbl[tspp->ts_umdpri].ts_globpri) {
1791 pri_t oldpri = tx->t_pri;
1792 ts_change_priority(tx, tspp);
1793 TRACE_2(TR_FAC_DISP, TR_UPDATE,
1794 "update:tid %p old pri %d", tx, oldpri);
1796 next:
1797 thread_unlock(tx);
1799 mutex_exit(&ts_list_lock[i]);
1801 return (updated);
1805 * Processes waking up go to the back of their queue. We don't
1806 * need to assign a time quantum here because thread is still
1807 * at a kernel mode priority and the time slicing is not done
1808 * for threads running in the kernel after sleeping. The proper
1809 * time quantum will be assigned by ts_trapret before the thread
1810 * returns to user mode.
1812 static void
1813 ts_wakeup(kthread_t *t)
1815 tsproc_t *tspp = (tsproc_t *)(t->t_cldata);
1817 ASSERT(THREAD_LOCK_HELD(t));
1819 if (tspp->ts_flags & TSKPRI) {
1820 tspp->ts_flags &= ~TSBACKQ;
1821 if (tspp->ts_flags & TSIASET)
1822 setfrontdq(t);
1823 else
1824 setbackdq(t);
1825 } else if (t->t_kpri_req) {
1827 * Give thread a priority boost if we were asked.
1829 tspp->ts_flags |= TSKPRI;
1830 THREAD_CHANGE_PRI(t, ts_kmdpris[0]);
1831 setbackdq(t);
1832 t->t_trapret = 1; /* so that ts_trapret will run */
1833 aston(t);
1834 } else {
1835 if (tspp->ts_dispwait > ts_dptbl[tspp->ts_umdpri].ts_maxwait) {
1836 tspp->ts_cpupri = ts_dptbl[tspp->ts_cpupri].ts_slpret;
1837 TS_NEWUMDPRI(tspp);
1838 tspp->ts_timeleft =
1839 ts_dptbl[tspp->ts_cpupri].ts_quantum;
1840 tspp->ts_dispwait = 0;
1841 THREAD_CHANGE_PRI(t,
1842 ts_dptbl[tspp->ts_umdpri].ts_globpri);
1843 ASSERT(t->t_pri >= 0 && t->t_pri <= ts_maxglobpri);
1846 tspp->ts_flags &= ~TSBACKQ;
1848 if (tspp->ts_flags & TSIA) {
1849 if (tspp->ts_flags & TSIASET)
1850 setfrontdq(t);
1851 else
1852 setbackdq(t);
1853 } else {
1854 if (t->t_disp_time != ddi_get_lbolt())
1855 setbackdq(t);
1856 else
1857 setfrontdq(t);
1864 * When a thread yields, put it on the back of the run queue.
1866 static void
1867 ts_yield(kthread_t *t)
1869 tsproc_t *tspp = (tsproc_t *)(t->t_cldata);
1871 ASSERT(t == curthread);
1872 ASSERT(THREAD_LOCK_HELD(t));
1875 * Collect CPU usage spent before yielding
1877 (void) CPUCAPS_CHARGE(t, &tspp->ts_caps, CPUCAPS_CHARGE_ENFORCE);
1880 * Clear the preemption control "yield" bit since the user is
1881 * doing a yield.
1883 if (t->t_schedctl)
1884 schedctl_set_yield(t, 0);
1886 * If ts_preempt() artifically increased the thread's priority
1887 * to avoid preemption, restore the original priority now.
1889 if (tspp->ts_flags & TSRESTORE) {
1890 THREAD_CHANGE_PRI(t, tspp->ts_scpri);
1891 tspp->ts_flags &= ~TSRESTORE;
1893 if (tspp->ts_timeleft <= 0) {
1895 * Time slice was artificially extended to avoid
1896 * preemption, so pretend we're preempting it now.
1898 DTRACE_SCHED1(schedctl__yield, int, -tspp->ts_timeleft);
1899 tspp->ts_cpupri = ts_dptbl[tspp->ts_cpupri].ts_tqexp;
1900 TS_NEWUMDPRI(tspp);
1901 tspp->ts_timeleft = ts_dptbl[tspp->ts_cpupri].ts_quantum;
1902 tspp->ts_dispwait = 0;
1903 THREAD_CHANGE_PRI(t, ts_dptbl[tspp->ts_umdpri].ts_globpri);
1904 ASSERT(t->t_pri >= 0 && t->t_pri <= ts_maxglobpri);
1906 tspp->ts_flags &= ~TSBACKQ;
1907 setbackdq(t);
1912 * Increment the nice value of the specified thread by incr and
1913 * return the new value in *retvalp.
1915 static int
1916 ts_donice(kthread_t *t, cred_t *cr, int incr, int *retvalp)
1918 int newnice;
1919 tsproc_t *tspp = (tsproc_t *)(t->t_cldata);
1920 tsparms_t tsparms;
1922 ASSERT(MUTEX_HELD(&(ttoproc(t))->p_lock));
1924 /* If there's no change to priority, just return current setting */
1925 if (incr == 0) {
1926 if (retvalp) {
1927 *retvalp = tspp->ts_nice - NZERO;
1929 return (0);
1932 if ((incr < 0 || incr > 2 * NZERO) &&
1933 secpolicy_raisepriority(cr) != 0)
1934 return (EPERM);
1937 * Specifying a nice increment greater than the upper limit of
1938 * 2 * NZERO - 1 will result in the thread's nice value being
1939 * set to the upper limit. We check for this before computing
1940 * the new value because otherwise we could get overflow
1941 * if a privileged process specified some ridiculous increment.
1943 if (incr > 2 * NZERO - 1)
1944 incr = 2 * NZERO - 1;
1946 newnice = tspp->ts_nice + incr;
1947 if (newnice >= 2 * NZERO)
1948 newnice = 2 * NZERO - 1;
1949 else if (newnice < 0)
1950 newnice = 0;
1952 tsparms.ts_uprilim = tsparms.ts_upri =
1953 -((newnice - NZERO) * ts_maxupri) / NZERO;
1955 * Reset the uprilim and upri values of the thread.
1956 * Call ts_parmsset even if thread is interactive since we're
1957 * not changing mode.
1959 (void) ts_parmsset(t, (void *)&tsparms, (id_t)0, (cred_t *)NULL);
1962 * Although ts_parmsset already reset ts_nice it may
1963 * not have been set to precisely the value calculated above
1964 * because ts_parmsset determines the nice value from the
1965 * user priority and we may have truncated during the integer
1966 * conversion from nice value to user priority and back.
1967 * We reset ts_nice to the value we calculated above.
1969 tspp->ts_nice = (char)newnice;
1971 if (retvalp)
1972 *retvalp = newnice - NZERO;
1973 return (0);
1977 * Increment the priority of the specified thread by incr and
1978 * return the new value in *retvalp.
1980 static int
1981 ts_doprio(kthread_t *t, cred_t *cr, int incr, int *retvalp)
1983 int newpri;
1984 tsproc_t *tspp = (tsproc_t *)(t->t_cldata);
1985 tsparms_t tsparms;
1987 ASSERT(MUTEX_HELD(&(ttoproc(t))->p_lock));
1989 /* If there's no change to the priority, just return current setting */
1990 if (incr == 0) {
1991 *retvalp = tspp->ts_upri;
1992 return (0);
1995 newpri = tspp->ts_upri + incr;
1996 if (newpri > ts_maxupri || newpri < -ts_maxupri)
1997 return (EINVAL);
1999 *retvalp = newpri;
2000 tsparms.ts_uprilim = tsparms.ts_upri = newpri;
2002 * Reset the uprilim and upri values of the thread.
2003 * Call ts_parmsset even if thread is interactive since we're
2004 * not changing mode.
2006 return (ts_parmsset(t, &tsparms, 0, cr));
2010 * ia_set_process_group marks foreground processes as interactive
2011 * and background processes as non-interactive iff the session
2012 * leader is interactive. This routine is called from two places:
2013 * strioctl:SPGRP when a new process group gets
2014 * control of the tty.
2015 * ia_parmsset-when the process in question is a session leader.
2016 * ia_set_process_group assumes that pidlock is held by the caller,
2017 * either strioctl or priocntlsys. If the caller is priocntlsys
2018 * (via ia_parmsset) then the p_lock of the session leader is held
2019 * and the code needs to be careful about acquiring other p_locks.
2021 static void
2022 ia_set_process_group(pid_t sid, pid_t bg_pgid, pid_t fg_pgid)
2024 proc_t *leader, *fg, *bg;
2025 tsproc_t *tspp;
2026 kthread_t *tx;
2027 int plocked = 0;
2029 ASSERT(MUTEX_HELD(&pidlock));
2032 * see if the session leader is interactive AND
2033 * if it is currently "on" AND controlling a tty
2034 * iff it is then make the processes in the foreground
2035 * group interactive and the processes in the background
2036 * group non-interactive.
2038 if ((leader = (proc_t *)prfind(sid)) == NULL) {
2039 return;
2041 if (leader->p_stat == SIDL) {
2042 return;
2044 if ((tx = proctot(leader)) == NULL) {
2045 return;
2048 * XXX do all the threads in the leader
2050 if (tx->t_cid != ia_cid) {
2051 return;
2053 tspp = tx->t_cldata;
2055 * session leaders that are not interactive need not have
2056 * any processing done for them. They are typically shells
2057 * that do not have focus and are changing the process group
2058 * attatched to the tty, e.g. a process that is exiting
2060 mutex_enter(&leader->p_sessp->s_lock);
2061 if (!(tspp->ts_flags & TSIASET) ||
2062 (leader->p_sessp->s_vp == NULL) ||
2063 (leader->p_sessp->s_vp->v_stream == NULL)) {
2064 mutex_exit(&leader->p_sessp->s_lock);
2065 return;
2067 mutex_exit(&leader->p_sessp->s_lock);
2070 * If we're already holding the leader's p_lock, we should use
2071 * mutex_tryenter instead of mutex_enter to avoid deadlocks from
2072 * lock ordering violations.
2074 if (mutex_owned(&leader->p_lock))
2075 plocked = 1;
2077 if (fg_pgid == 0)
2078 goto skip;
2080 * now look for all processes in the foreground group and
2081 * make them interactive
2083 for (fg = (proc_t *)pgfind(fg_pgid); fg != NULL; fg = fg->p_pglink) {
2085 * if the process is SIDL it's begin forked, ignore it
2087 if (fg->p_stat == SIDL) {
2088 continue;
2091 * sesssion leaders must be turned on/off explicitly
2092 * not implicitly as happens to other members of
2093 * the process group.
2095 if (fg->p_pid == fg->p_sessp->s_sid) {
2096 continue;
2099 TRACE_1(TR_FAC_IA, TR_GROUP_ON,
2100 "group on:proc %p", fg);
2102 if (plocked) {
2103 if (mutex_tryenter(&fg->p_lock) == 0)
2104 continue;
2105 } else {
2106 mutex_enter(&fg->p_lock);
2109 if ((tx = proctot(fg)) == NULL) {
2110 mutex_exit(&fg->p_lock);
2111 continue;
2113 do {
2114 thread_lock(tx);
2116 * if this thread is not interactive continue
2118 if (tx->t_cid != ia_cid) {
2119 thread_unlock(tx);
2120 continue;
2122 tspp = tx->t_cldata;
2123 tspp->ts_flags |= TSIASET;
2124 tspp->ts_boost = ia_boost;
2125 TS_NEWUMDPRI(tspp);
2126 if ((tspp->ts_flags & TSKPRI) != 0) {
2127 thread_unlock(tx);
2128 continue;
2130 tspp->ts_dispwait = 0;
2131 ts_change_priority(tx, tspp);
2132 thread_unlock(tx);
2133 } while ((tx = tx->t_forw) != fg->p_tlist);
2134 mutex_exit(&fg->p_lock);
2136 skip:
2137 if (bg_pgid == 0)
2138 return;
2139 for (bg = (proc_t *)pgfind(bg_pgid); bg != NULL; bg = bg->p_pglink) {
2140 if (bg->p_stat == SIDL) {
2141 continue;
2144 * sesssion leaders must be turned off explicitly
2145 * not implicitly as happens to other members of
2146 * the process group.
2148 if (bg->p_pid == bg->p_sessp->s_sid) {
2149 continue;
2152 TRACE_1(TR_FAC_IA, TR_GROUP_OFF,
2153 "group off:proc %p", bg);
2155 if (plocked) {
2156 if (mutex_tryenter(&bg->p_lock) == 0)
2157 continue;
2158 } else {
2159 mutex_enter(&bg->p_lock);
2162 if ((tx = proctot(bg)) == NULL) {
2163 mutex_exit(&bg->p_lock);
2164 continue;
2166 do {
2167 thread_lock(tx);
2169 * if this thread is not interactive continue
2171 if (tx->t_cid != ia_cid) {
2172 thread_unlock(tx);
2173 continue;
2175 tspp = tx->t_cldata;
2176 tspp->ts_flags &= ~TSIASET;
2177 tspp->ts_boost = -ia_boost;
2178 TS_NEWUMDPRI(tspp);
2179 if ((tspp->ts_flags & TSKPRI) != 0) {
2180 thread_unlock(tx);
2181 continue;
2184 tspp->ts_dispwait = 0;
2185 ts_change_priority(tx, tspp);
2186 thread_unlock(tx);
2187 } while ((tx = tx->t_forw) != bg->p_tlist);
2188 mutex_exit(&bg->p_lock);
2193 static void
2194 ts_change_priority(kthread_t *t, tsproc_t *tspp)
2196 pri_t new_pri;
2198 ASSERT(THREAD_LOCK_HELD(t));
2199 new_pri = ts_dptbl[tspp->ts_umdpri].ts_globpri;
2200 ASSERT(new_pri >= 0 && new_pri <= ts_maxglobpri);
2201 tspp->ts_flags &= ~TSRESTORE;
2202 t->t_cpri = tspp->ts_upri;
2203 if (t == curthread || t->t_state == TS_ONPROC) {
2204 /* curthread is always onproc */
2205 cpu_t *cp = t->t_disp_queue->disp_cpu;
2206 THREAD_CHANGE_PRI(t, new_pri);
2207 if (t == cp->cpu_dispthread)
2208 cp->cpu_dispatch_pri = DISP_PRIO(t);
2209 if (DISP_MUST_SURRENDER(t)) {
2210 tspp->ts_flags |= TSBACKQ;
2211 cpu_surrender(t);
2212 } else {
2213 tspp->ts_timeleft =
2214 ts_dptbl[tspp->ts_cpupri].ts_quantum;
2216 } else {
2217 int frontq;
2219 frontq = (tspp->ts_flags & TSIASET) != 0;
2221 * When the priority of a thread is changed,
2222 * it may be necessary to adjust its position
2223 * on a sleep queue or dispatch queue.
2224 * The function thread_change_pri accomplishes
2225 * this.
2227 if (thread_change_pri(t, new_pri, frontq)) {
2229 * The thread was on a run queue. Reset
2230 * its CPU timeleft from the quantum
2231 * associated with the new priority.
2233 tspp->ts_timeleft =
2234 ts_dptbl[tspp->ts_cpupri].ts_quantum;
2235 } else {
2236 tspp->ts_flags |= TSBACKQ;
2241 static int
2242 ts_alloc(void **p, int flag)
2244 void *bufp;
2245 bufp = kmem_alloc(sizeof (tsproc_t), flag);
2246 if (bufp == NULL) {
2247 return (ENOMEM);
2248 } else {
2249 *p = bufp;
2250 return (0);
2254 static void
2255 ts_free(void *bufp)
2257 if (bufp)
2258 kmem_free(bufp, sizeof (tsproc_t));