10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
52712Snn35248 * Common Development and Distribution License (the "License").
62712Snn35248 * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate * See the License for the specific language governing permissions
110Sstevel@tonic-gate * and limitations under the License.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate *
190Sstevel@tonic-gate * CDDL HEADER END
200Sstevel@tonic-gate */
21951Sraf
220Sstevel@tonic-gate /*
23*12785SPramod.Batni@Sun.COM * Copyright (c) 1994, 2010, Oracle and/or its affiliates. All rights reserved.
240Sstevel@tonic-gate */
250Sstevel@tonic-gate
260Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
270Sstevel@tonic-gate /* All Rights Reserved */
280Sstevel@tonic-gate
290Sstevel@tonic-gate #include <sys/types.h>
300Sstevel@tonic-gate #include <sys/param.h>
310Sstevel@tonic-gate #include <sys/sysmacros.h>
320Sstevel@tonic-gate #include <sys/cred.h>
330Sstevel@tonic-gate #include <sys/proc.h>
340Sstevel@tonic-gate #include <sys/session.h>
350Sstevel@tonic-gate #include <sys/strsubr.h>
360Sstevel@tonic-gate #include <sys/signal.h>
370Sstevel@tonic-gate #include <sys/user.h>
380Sstevel@tonic-gate #include <sys/priocntl.h>
390Sstevel@tonic-gate #include <sys/class.h>
400Sstevel@tonic-gate #include <sys/disp.h>
410Sstevel@tonic-gate #include <sys/procset.h>
420Sstevel@tonic-gate #include <sys/debug.h>
430Sstevel@tonic-gate #include <sys/ts.h>
440Sstevel@tonic-gate #include <sys/tspriocntl.h>
450Sstevel@tonic-gate #include <sys/iapriocntl.h>
460Sstevel@tonic-gate #include <sys/kmem.h>
470Sstevel@tonic-gate #include <sys/errno.h>
480Sstevel@tonic-gate #include <sys/cpuvar.h>
490Sstevel@tonic-gate #include <sys/systm.h> /* for lbolt */
500Sstevel@tonic-gate #include <sys/vtrace.h>
510Sstevel@tonic-gate #include <sys/vmsystm.h>
520Sstevel@tonic-gate #include <sys/schedctl.h>
530Sstevel@tonic-gate #include <sys/tnf_probe.h>
540Sstevel@tonic-gate #include <sys/atomic.h>
550Sstevel@tonic-gate #include <sys/policy.h>
560Sstevel@tonic-gate #include <sys/sdt.h>
570Sstevel@tonic-gate #include <sys/cpupart.h>
580Sstevel@tonic-gate #include <vm/rm.h>
590Sstevel@tonic-gate #include <vm/seg_kmem.h>
600Sstevel@tonic-gate #include <sys/modctl.h>
613792Sakolb #include <sys/cpucaps.h>
620Sstevel@tonic-gate
630Sstevel@tonic-gate static pri_t ts_init(id_t, int, classfuncs_t **);
640Sstevel@tonic-gate
650Sstevel@tonic-gate static struct sclass csw = {
660Sstevel@tonic-gate "TS",
670Sstevel@tonic-gate ts_init,
680Sstevel@tonic-gate 0
690Sstevel@tonic-gate };
700Sstevel@tonic-gate
710Sstevel@tonic-gate static struct modlsched modlsched = {
720Sstevel@tonic-gate &mod_schedops, "time sharing sched class", &csw
730Sstevel@tonic-gate };
740Sstevel@tonic-gate
750Sstevel@tonic-gate static struct modlinkage modlinkage = {
760Sstevel@tonic-gate MODREV_1, (void *)&modlsched, NULL
770Sstevel@tonic-gate };
780Sstevel@tonic-gate
790Sstevel@tonic-gate int
_init()800Sstevel@tonic-gate _init()
810Sstevel@tonic-gate {
820Sstevel@tonic-gate return (mod_install(&modlinkage));
830Sstevel@tonic-gate }
840Sstevel@tonic-gate
850Sstevel@tonic-gate int
_fini()860Sstevel@tonic-gate _fini()
870Sstevel@tonic-gate {
880Sstevel@tonic-gate return (EBUSY); /* don't remove TS for now */
890Sstevel@tonic-gate }
900Sstevel@tonic-gate
910Sstevel@tonic-gate int
_info(struct modinfo * modinfop)920Sstevel@tonic-gate _info(struct modinfo *modinfop)
930Sstevel@tonic-gate {
940Sstevel@tonic-gate return (mod_info(&modlinkage, modinfop));
950Sstevel@tonic-gate }
960Sstevel@tonic-gate
970Sstevel@tonic-gate /*
980Sstevel@tonic-gate * Class specific code for the time-sharing class
990Sstevel@tonic-gate */
1000Sstevel@tonic-gate
1010Sstevel@tonic-gate
1020Sstevel@tonic-gate /*
1030Sstevel@tonic-gate * Extern declarations for variables defined in the ts master file
1040Sstevel@tonic-gate */
1050Sstevel@tonic-gate #define TSMAXUPRI 60
1060Sstevel@tonic-gate
1070Sstevel@tonic-gate pri_t ts_maxupri = TSMAXUPRI; /* max time-sharing user priority */
1080Sstevel@tonic-gate pri_t ts_maxumdpri; /* maximum user mode ts priority */
1090Sstevel@tonic-gate
1100Sstevel@tonic-gate pri_t ia_maxupri = IAMAXUPRI; /* max interactive user priority */
1110Sstevel@tonic-gate pri_t ia_boost = IA_BOOST; /* boost value for interactive */
1120Sstevel@tonic-gate
1130Sstevel@tonic-gate tsdpent_t *ts_dptbl; /* time-sharing disp parameter table */
1140Sstevel@tonic-gate pri_t *ts_kmdpris; /* array of global pris used by ts procs when */
1150Sstevel@tonic-gate /* sleeping or running in kernel after sleep */
1160Sstevel@tonic-gate
1170Sstevel@tonic-gate static id_t ia_cid;
1180Sstevel@tonic-gate
1190Sstevel@tonic-gate int ts_sleep_promote = 1;
1200Sstevel@tonic-gate
1210Sstevel@tonic-gate #define tsmedumdpri (ts_maxumdpri >> 1)
1220Sstevel@tonic-gate
1230Sstevel@tonic-gate #define TS_NEWUMDPRI(tspp) \
1240Sstevel@tonic-gate { \
1250Sstevel@tonic-gate pri_t pri; \
1260Sstevel@tonic-gate pri = (tspp)->ts_cpupri + (tspp)->ts_upri + (tspp)->ts_boost; \
1270Sstevel@tonic-gate if (pri > ts_maxumdpri) \
1280Sstevel@tonic-gate (tspp)->ts_umdpri = ts_maxumdpri; \
1290Sstevel@tonic-gate else if (pri < 0) \
1300Sstevel@tonic-gate (tspp)->ts_umdpri = 0; \
1310Sstevel@tonic-gate else \
1320Sstevel@tonic-gate (tspp)->ts_umdpri = pri; \
1330Sstevel@tonic-gate ASSERT((tspp)->ts_umdpri >= 0 && (tspp)->ts_umdpri <= ts_maxumdpri); \
1340Sstevel@tonic-gate }
1350Sstevel@tonic-gate
1360Sstevel@tonic-gate /*
1370Sstevel@tonic-gate * The tsproc_t structures are kept in an array of circular doubly linked
1380Sstevel@tonic-gate * lists. A hash on the thread pointer is used to determine which list
1390Sstevel@tonic-gate * each thread should be placed. Each list has a dummy "head" which is
1400Sstevel@tonic-gate * never removed, so the list is never empty. ts_update traverses these
1410Sstevel@tonic-gate * lists to update the priorities of threads that have been waiting on
1420Sstevel@tonic-gate * the run queue.
1430Sstevel@tonic-gate */
1440Sstevel@tonic-gate
1450Sstevel@tonic-gate #define TS_LISTS 16 /* number of lists, must be power of 2 */
1460Sstevel@tonic-gate
1470Sstevel@tonic-gate /* hash function, argument is a thread pointer */
1480Sstevel@tonic-gate #define TS_LIST_HASH(tp) (((uintptr_t)(tp) >> 9) & (TS_LISTS - 1))
1490Sstevel@tonic-gate
1500Sstevel@tonic-gate /* iterate to the next list */
1510Sstevel@tonic-gate #define TS_LIST_NEXT(i) (((i) + 1) & (TS_LISTS - 1))
1520Sstevel@tonic-gate
1530Sstevel@tonic-gate /*
1540Sstevel@tonic-gate * Insert thread into the appropriate tsproc list.
1550Sstevel@tonic-gate */
1560Sstevel@tonic-gate #define TS_LIST_INSERT(tspp) \
1570Sstevel@tonic-gate { \
1580Sstevel@tonic-gate int index = TS_LIST_HASH(tspp->ts_tp); \
1590Sstevel@tonic-gate kmutex_t *lockp = &ts_list_lock[index]; \
1600Sstevel@tonic-gate tsproc_t *headp = &ts_plisthead[index]; \
1610Sstevel@tonic-gate mutex_enter(lockp); \
1620Sstevel@tonic-gate tspp->ts_next = headp->ts_next; \
1630Sstevel@tonic-gate tspp->ts_prev = headp; \
1640Sstevel@tonic-gate headp->ts_next->ts_prev = tspp; \
1650Sstevel@tonic-gate headp->ts_next = tspp; \
1660Sstevel@tonic-gate mutex_exit(lockp); \
1670Sstevel@tonic-gate }
1680Sstevel@tonic-gate
1690Sstevel@tonic-gate /*
1700Sstevel@tonic-gate * Remove thread from tsproc list.
1710Sstevel@tonic-gate */
1720Sstevel@tonic-gate #define TS_LIST_DELETE(tspp) \
1730Sstevel@tonic-gate { \
1740Sstevel@tonic-gate int index = TS_LIST_HASH(tspp->ts_tp); \
1750Sstevel@tonic-gate kmutex_t *lockp = &ts_list_lock[index]; \
1760Sstevel@tonic-gate mutex_enter(lockp); \
1770Sstevel@tonic-gate tspp->ts_prev->ts_next = tspp->ts_next; \
1780Sstevel@tonic-gate tspp->ts_next->ts_prev = tspp->ts_prev; \
1790Sstevel@tonic-gate mutex_exit(lockp); \
1800Sstevel@tonic-gate }
1810Sstevel@tonic-gate
1820Sstevel@tonic-gate
1830Sstevel@tonic-gate static int ts_admin(caddr_t, cred_t *);
1840Sstevel@tonic-gate static int ts_enterclass(kthread_t *, id_t, void *, cred_t *, void *);
1850Sstevel@tonic-gate static int ts_fork(kthread_t *, kthread_t *, void *);
1860Sstevel@tonic-gate static int ts_getclinfo(void *);
1870Sstevel@tonic-gate static int ts_getclpri(pcpri_t *);
1880Sstevel@tonic-gate static int ts_parmsin(void *);
1890Sstevel@tonic-gate static int ts_parmsout(void *, pc_vaparms_t *);
1900Sstevel@tonic-gate static int ts_vaparmsin(void *, pc_vaparms_t *);
1910Sstevel@tonic-gate static int ts_vaparmsout(void *, pc_vaparms_t *);
1920Sstevel@tonic-gate static int ts_parmsset(kthread_t *, void *, id_t, cred_t *);
1933792Sakolb static void ts_exit(kthread_t *);
1940Sstevel@tonic-gate static int ts_donice(kthread_t *, cred_t *, int, int *);
1956247Sraf static int ts_doprio(kthread_t *, cred_t *, int, int *);
1960Sstevel@tonic-gate static void ts_exitclass(void *);
1970Sstevel@tonic-gate static int ts_canexit(kthread_t *, cred_t *);
1980Sstevel@tonic-gate static void ts_forkret(kthread_t *, kthread_t *);
1990Sstevel@tonic-gate static void ts_nullsys();
2000Sstevel@tonic-gate static void ts_parmsget(kthread_t *, void *);
2010Sstevel@tonic-gate static void ts_preempt(kthread_t *);
2020Sstevel@tonic-gate static void ts_setrun(kthread_t *);
2030Sstevel@tonic-gate static void ts_sleep(kthread_t *);
2040Sstevel@tonic-gate static pri_t ts_swapin(kthread_t *, int);
2050Sstevel@tonic-gate static pri_t ts_swapout(kthread_t *, int);
2060Sstevel@tonic-gate static void ts_tick(kthread_t *);
2070Sstevel@tonic-gate static void ts_trapret(kthread_t *);
2080Sstevel@tonic-gate static void ts_update(void *);
2090Sstevel@tonic-gate static int ts_update_list(int);
2100Sstevel@tonic-gate static void ts_wakeup(kthread_t *);
2110Sstevel@tonic-gate static pri_t ts_globpri(kthread_t *);
2120Sstevel@tonic-gate static void ts_yield(kthread_t *);
2130Sstevel@tonic-gate extern tsdpent_t *ts_getdptbl(void);
2140Sstevel@tonic-gate extern pri_t *ts_getkmdpris(void);
2150Sstevel@tonic-gate extern pri_t td_getmaxumdpri(void);
2160Sstevel@tonic-gate static int ts_alloc(void **, int);
2170Sstevel@tonic-gate static void ts_free(void *);
2180Sstevel@tonic-gate
2190Sstevel@tonic-gate pri_t ia_init(id_t, int, classfuncs_t **);
2200Sstevel@tonic-gate static int ia_getclinfo(void *);
2216247Sraf static int ia_getclpri(pcpri_t *);
2220Sstevel@tonic-gate static int ia_parmsin(void *);
2230Sstevel@tonic-gate static int ia_vaparmsin(void *, pc_vaparms_t *);
2240Sstevel@tonic-gate static int ia_vaparmsout(void *, pc_vaparms_t *);
2250Sstevel@tonic-gate static int ia_parmsset(kthread_t *, void *, id_t, cred_t *);
2260Sstevel@tonic-gate static void ia_parmsget(kthread_t *, void *);
2270Sstevel@tonic-gate static void ia_set_process_group(pid_t, pid_t, pid_t);
2280Sstevel@tonic-gate
2290Sstevel@tonic-gate static void ts_change_priority(kthread_t *, tsproc_t *);
2300Sstevel@tonic-gate
2310Sstevel@tonic-gate extern pri_t ts_maxkmdpri; /* maximum kernel mode ts priority */
2320Sstevel@tonic-gate static pri_t ts_maxglobpri; /* maximum global priority used by ts class */
2330Sstevel@tonic-gate static kmutex_t ts_dptblock; /* protects time sharing dispatch table */
2340Sstevel@tonic-gate static kmutex_t ts_list_lock[TS_LISTS]; /* protects tsproc lists */
2350Sstevel@tonic-gate static tsproc_t ts_plisthead[TS_LISTS]; /* dummy tsproc at head of lists */
2360Sstevel@tonic-gate
2370Sstevel@tonic-gate static gid_t IA_gid = 0;
2380Sstevel@tonic-gate
2390Sstevel@tonic-gate static struct classfuncs ts_classfuncs = {
2400Sstevel@tonic-gate /* class functions */
2410Sstevel@tonic-gate ts_admin,
2420Sstevel@tonic-gate ts_getclinfo,
2430Sstevel@tonic-gate ts_parmsin,
2440Sstevel@tonic-gate ts_parmsout,
2450Sstevel@tonic-gate ts_vaparmsin,
2460Sstevel@tonic-gate ts_vaparmsout,
2470Sstevel@tonic-gate ts_getclpri,
2480Sstevel@tonic-gate ts_alloc,
2490Sstevel@tonic-gate ts_free,
2500Sstevel@tonic-gate
2510Sstevel@tonic-gate /* thread functions */
2520Sstevel@tonic-gate ts_enterclass,
2530Sstevel@tonic-gate ts_exitclass,
2540Sstevel@tonic-gate ts_canexit,
2550Sstevel@tonic-gate ts_fork,
2560Sstevel@tonic-gate ts_forkret,
2570Sstevel@tonic-gate ts_parmsget,
2580Sstevel@tonic-gate ts_parmsset,
2590Sstevel@tonic-gate ts_nullsys, /* stop */
2603792Sakolb ts_exit,
2610Sstevel@tonic-gate ts_nullsys, /* active */
2620Sstevel@tonic-gate ts_nullsys, /* inactive */
2630Sstevel@tonic-gate ts_swapin,
2640Sstevel@tonic-gate ts_swapout,
2650Sstevel@tonic-gate ts_trapret,
2660Sstevel@tonic-gate ts_preempt,
2670Sstevel@tonic-gate ts_setrun,
2680Sstevel@tonic-gate ts_sleep,
2690Sstevel@tonic-gate ts_tick,
2700Sstevel@tonic-gate ts_wakeup,
2710Sstevel@tonic-gate ts_donice,
2720Sstevel@tonic-gate ts_globpri,
2730Sstevel@tonic-gate ts_nullsys, /* set_process_group */
2740Sstevel@tonic-gate ts_yield,
2756247Sraf ts_doprio,
2760Sstevel@tonic-gate };
2770Sstevel@tonic-gate
2780Sstevel@tonic-gate /*
2790Sstevel@tonic-gate * ia_classfuncs is used for interactive class threads; IA threads are stored
2800Sstevel@tonic-gate * on the same class list as TS threads, and most of the class functions are
2810Sstevel@tonic-gate * identical, but a few have different enough functionality to require their
2820Sstevel@tonic-gate * own functions.
2830Sstevel@tonic-gate */
2840Sstevel@tonic-gate static struct classfuncs ia_classfuncs = {
2850Sstevel@tonic-gate /* class functions */
2860Sstevel@tonic-gate ts_admin,
2870Sstevel@tonic-gate ia_getclinfo,
2880Sstevel@tonic-gate ia_parmsin,
2890Sstevel@tonic-gate ts_parmsout,
2900Sstevel@tonic-gate ia_vaparmsin,
2910Sstevel@tonic-gate ia_vaparmsout,
2926247Sraf ia_getclpri,
2930Sstevel@tonic-gate ts_alloc,
2940Sstevel@tonic-gate ts_free,
2950Sstevel@tonic-gate
2960Sstevel@tonic-gate /* thread functions */
2970Sstevel@tonic-gate ts_enterclass,
2980Sstevel@tonic-gate ts_exitclass,
2990Sstevel@tonic-gate ts_canexit,
3000Sstevel@tonic-gate ts_fork,
3010Sstevel@tonic-gate ts_forkret,
3020Sstevel@tonic-gate ia_parmsget,
3030Sstevel@tonic-gate ia_parmsset,
3040Sstevel@tonic-gate ts_nullsys, /* stop */
3053792Sakolb ts_exit,
3060Sstevel@tonic-gate ts_nullsys, /* active */
3070Sstevel@tonic-gate ts_nullsys, /* inactive */
3080Sstevel@tonic-gate ts_swapin,
3090Sstevel@tonic-gate ts_swapout,
3100Sstevel@tonic-gate ts_trapret,
3110Sstevel@tonic-gate ts_preempt,
3120Sstevel@tonic-gate ts_setrun,
3130Sstevel@tonic-gate ts_sleep,
3140Sstevel@tonic-gate ts_tick,
3150Sstevel@tonic-gate ts_wakeup,
3160Sstevel@tonic-gate ts_donice,
3170Sstevel@tonic-gate ts_globpri,
3180Sstevel@tonic-gate ia_set_process_group,
3190Sstevel@tonic-gate ts_yield,
3206247Sraf ts_doprio,
3210Sstevel@tonic-gate };
3220Sstevel@tonic-gate
3230Sstevel@tonic-gate
3240Sstevel@tonic-gate /*
3250Sstevel@tonic-gate * Time sharing class initialization. Called by dispinit() at boot time.
3260Sstevel@tonic-gate * We can ignore the clparmsz argument since we know that the smallest
3270Sstevel@tonic-gate * possible parameter buffer is big enough for us.
3280Sstevel@tonic-gate */
3290Sstevel@tonic-gate /* ARGSUSED */
3300Sstevel@tonic-gate static pri_t
ts_init(id_t cid,int clparmsz,classfuncs_t ** clfuncspp)3310Sstevel@tonic-gate ts_init(id_t cid, int clparmsz, classfuncs_t **clfuncspp)
3320Sstevel@tonic-gate {
3330Sstevel@tonic-gate int i;
3340Sstevel@tonic-gate extern pri_t ts_getmaxumdpri(void);
3350Sstevel@tonic-gate
3360Sstevel@tonic-gate ts_dptbl = ts_getdptbl();
3370Sstevel@tonic-gate ts_kmdpris = ts_getkmdpris();
3380Sstevel@tonic-gate ts_maxumdpri = ts_getmaxumdpri();
3390Sstevel@tonic-gate ts_maxglobpri = MAX(ts_kmdpris[0], ts_dptbl[ts_maxumdpri].ts_globpri);
3400Sstevel@tonic-gate
3410Sstevel@tonic-gate /*
3420Sstevel@tonic-gate * Initialize the tsproc lists.
3430Sstevel@tonic-gate */
3440Sstevel@tonic-gate for (i = 0; i < TS_LISTS; i++) {
3450Sstevel@tonic-gate ts_plisthead[i].ts_next = ts_plisthead[i].ts_prev =
3460Sstevel@tonic-gate &ts_plisthead[i];
3470Sstevel@tonic-gate }
3480Sstevel@tonic-gate
3490Sstevel@tonic-gate /*
3500Sstevel@tonic-gate * We're required to return a pointer to our classfuncs
3510Sstevel@tonic-gate * structure and the highest global priority value we use.
3520Sstevel@tonic-gate */
3530Sstevel@tonic-gate *clfuncspp = &ts_classfuncs;
3540Sstevel@tonic-gate return (ts_maxglobpri);
3550Sstevel@tonic-gate }
3560Sstevel@tonic-gate
3570Sstevel@tonic-gate
3580Sstevel@tonic-gate /*
3590Sstevel@tonic-gate * Interactive class scheduler initialization
3600Sstevel@tonic-gate */
3610Sstevel@tonic-gate /* ARGSUSED */
3620Sstevel@tonic-gate pri_t
ia_init(id_t cid,int clparmsz,classfuncs_t ** clfuncspp)3630Sstevel@tonic-gate ia_init(id_t cid, int clparmsz, classfuncs_t **clfuncspp)
3640Sstevel@tonic-gate {
3650Sstevel@tonic-gate /*
3660Sstevel@tonic-gate * We're required to return a pointer to our classfuncs
3670Sstevel@tonic-gate * structure and the highest global priority value we use.
3680Sstevel@tonic-gate */
3690Sstevel@tonic-gate ia_cid = cid;
3700Sstevel@tonic-gate *clfuncspp = &ia_classfuncs;
3710Sstevel@tonic-gate return (ts_maxglobpri);
3720Sstevel@tonic-gate }
3730Sstevel@tonic-gate
3740Sstevel@tonic-gate
3750Sstevel@tonic-gate /*
3760Sstevel@tonic-gate * Get or reset the ts_dptbl values per the user's request.
3770Sstevel@tonic-gate */
3780Sstevel@tonic-gate static int
ts_admin(caddr_t uaddr,cred_t * reqpcredp)3790Sstevel@tonic-gate ts_admin(caddr_t uaddr, cred_t *reqpcredp)
3800Sstevel@tonic-gate {
3810Sstevel@tonic-gate tsadmin_t tsadmin;
3820Sstevel@tonic-gate tsdpent_t *tmpdpp;
3830Sstevel@tonic-gate int userdpsz;
3840Sstevel@tonic-gate int i;
3850Sstevel@tonic-gate size_t tsdpsz;
3860Sstevel@tonic-gate
3870Sstevel@tonic-gate if (get_udatamodel() == DATAMODEL_NATIVE) {
3880Sstevel@tonic-gate if (copyin(uaddr, &tsadmin, sizeof (tsadmin_t)))
3890Sstevel@tonic-gate return (EFAULT);
3900Sstevel@tonic-gate }
3910Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
3920Sstevel@tonic-gate else {
3930Sstevel@tonic-gate /* get tsadmin struct from ILP32 caller */
3940Sstevel@tonic-gate tsadmin32_t tsadmin32;
3950Sstevel@tonic-gate if (copyin(uaddr, &tsadmin32, sizeof (tsadmin32_t)))
3960Sstevel@tonic-gate return (EFAULT);
3970Sstevel@tonic-gate tsadmin.ts_dpents =
3980Sstevel@tonic-gate (struct tsdpent *)(uintptr_t)tsadmin32.ts_dpents;
3990Sstevel@tonic-gate tsadmin.ts_ndpents = tsadmin32.ts_ndpents;
4000Sstevel@tonic-gate tsadmin.ts_cmd = tsadmin32.ts_cmd;
4010Sstevel@tonic-gate }
4020Sstevel@tonic-gate #endif /* _SYSCALL32_IMPL */
4030Sstevel@tonic-gate
4040Sstevel@tonic-gate tsdpsz = (ts_maxumdpri + 1) * sizeof (tsdpent_t);
4050Sstevel@tonic-gate
4060Sstevel@tonic-gate switch (tsadmin.ts_cmd) {
4070Sstevel@tonic-gate case TS_GETDPSIZE:
4080Sstevel@tonic-gate tsadmin.ts_ndpents = ts_maxumdpri + 1;
4090Sstevel@tonic-gate
4100Sstevel@tonic-gate if (get_udatamodel() == DATAMODEL_NATIVE) {
4110Sstevel@tonic-gate if (copyout(&tsadmin, uaddr, sizeof (tsadmin_t)))
4120Sstevel@tonic-gate return (EFAULT);
4130Sstevel@tonic-gate }
4140Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
4150Sstevel@tonic-gate else {
4160Sstevel@tonic-gate /* return tsadmin struct to ILP32 caller */
4170Sstevel@tonic-gate tsadmin32_t tsadmin32;
4180Sstevel@tonic-gate tsadmin32.ts_dpents =
4190Sstevel@tonic-gate (caddr32_t)(uintptr_t)tsadmin.ts_dpents;
4200Sstevel@tonic-gate tsadmin32.ts_ndpents = tsadmin.ts_ndpents;
4210Sstevel@tonic-gate tsadmin32.ts_cmd = tsadmin.ts_cmd;
4220Sstevel@tonic-gate if (copyout(&tsadmin32, uaddr, sizeof (tsadmin32_t)))
4230Sstevel@tonic-gate return (EFAULT);
4240Sstevel@tonic-gate }
4250Sstevel@tonic-gate #endif /* _SYSCALL32_IMPL */
4260Sstevel@tonic-gate break;
4270Sstevel@tonic-gate
4280Sstevel@tonic-gate case TS_GETDPTBL:
4290Sstevel@tonic-gate userdpsz = MIN(tsadmin.ts_ndpents * sizeof (tsdpent_t),
4300Sstevel@tonic-gate tsdpsz);
4310Sstevel@tonic-gate if (copyout(ts_dptbl, tsadmin.ts_dpents, userdpsz))
4320Sstevel@tonic-gate return (EFAULT);
4330Sstevel@tonic-gate
4340Sstevel@tonic-gate tsadmin.ts_ndpents = userdpsz / sizeof (tsdpent_t);
4350Sstevel@tonic-gate
4360Sstevel@tonic-gate if (get_udatamodel() == DATAMODEL_NATIVE) {
4370Sstevel@tonic-gate if (copyout(&tsadmin, uaddr, sizeof (tsadmin_t)))
4380Sstevel@tonic-gate return (EFAULT);
4390Sstevel@tonic-gate }
4400Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
4410Sstevel@tonic-gate else {
4420Sstevel@tonic-gate /* return tsadmin struct to ILP32 callers */
4430Sstevel@tonic-gate tsadmin32_t tsadmin32;
4440Sstevel@tonic-gate tsadmin32.ts_dpents =
4450Sstevel@tonic-gate (caddr32_t)(uintptr_t)tsadmin.ts_dpents;
4460Sstevel@tonic-gate tsadmin32.ts_ndpents = tsadmin.ts_ndpents;
4470Sstevel@tonic-gate tsadmin32.ts_cmd = tsadmin.ts_cmd;
4480Sstevel@tonic-gate if (copyout(&tsadmin32, uaddr, sizeof (tsadmin32_t)))
4490Sstevel@tonic-gate return (EFAULT);
4500Sstevel@tonic-gate }
4510Sstevel@tonic-gate #endif /* _SYSCALL32_IMPL */
4520Sstevel@tonic-gate break;
4530Sstevel@tonic-gate
4540Sstevel@tonic-gate case TS_SETDPTBL:
4550Sstevel@tonic-gate /*
4560Sstevel@tonic-gate * We require that the requesting process has sufficient
4570Sstevel@tonic-gate * priveleges. We also require that the table supplied by
4580Sstevel@tonic-gate * the user exactly match the current ts_dptbl in size.
4590Sstevel@tonic-gate */
4600Sstevel@tonic-gate if (secpolicy_dispadm(reqpcredp) != 0)
4610Sstevel@tonic-gate return (EPERM);
4620Sstevel@tonic-gate
4630Sstevel@tonic-gate if (tsadmin.ts_ndpents * sizeof (tsdpent_t) != tsdpsz) {
4640Sstevel@tonic-gate return (EINVAL);
4650Sstevel@tonic-gate }
4660Sstevel@tonic-gate
4670Sstevel@tonic-gate /*
4680Sstevel@tonic-gate * We read the user supplied table into a temporary buffer
4690Sstevel@tonic-gate * where it is validated before being copied over the
4700Sstevel@tonic-gate * ts_dptbl.
4710Sstevel@tonic-gate */
4720Sstevel@tonic-gate tmpdpp = kmem_alloc(tsdpsz, KM_SLEEP);
4730Sstevel@tonic-gate if (copyin((caddr_t)tsadmin.ts_dpents, (caddr_t)tmpdpp,
4740Sstevel@tonic-gate tsdpsz)) {
4750Sstevel@tonic-gate kmem_free(tmpdpp, tsdpsz);
4760Sstevel@tonic-gate return (EFAULT);
4770Sstevel@tonic-gate }
4780Sstevel@tonic-gate for (i = 0; i < tsadmin.ts_ndpents; i++) {
4790Sstevel@tonic-gate
4800Sstevel@tonic-gate /*
4810Sstevel@tonic-gate * Validate the user supplied values. All we are doing
4820Sstevel@tonic-gate * here is verifying that the values are within their
4830Sstevel@tonic-gate * allowable ranges and will not panic the system. We
4840Sstevel@tonic-gate * make no attempt to ensure that the resulting
4850Sstevel@tonic-gate * configuration makes sense or results in reasonable
4860Sstevel@tonic-gate * performance.
4870Sstevel@tonic-gate */
4880Sstevel@tonic-gate if (tmpdpp[i].ts_quantum <= 0) {
4890Sstevel@tonic-gate kmem_free(tmpdpp, tsdpsz);
4900Sstevel@tonic-gate return (EINVAL);
4910Sstevel@tonic-gate }
4920Sstevel@tonic-gate if (tmpdpp[i].ts_tqexp > ts_maxumdpri ||
4930Sstevel@tonic-gate tmpdpp[i].ts_tqexp < 0) {
4940Sstevel@tonic-gate kmem_free(tmpdpp, tsdpsz);
4950Sstevel@tonic-gate return (EINVAL);
4960Sstevel@tonic-gate }
4970Sstevel@tonic-gate if (tmpdpp[i].ts_slpret > ts_maxumdpri ||
4980Sstevel@tonic-gate tmpdpp[i].ts_slpret < 0) {
4990Sstevel@tonic-gate kmem_free(tmpdpp, tsdpsz);
5000Sstevel@tonic-gate return (EINVAL);
5010Sstevel@tonic-gate }
5020Sstevel@tonic-gate if (tmpdpp[i].ts_maxwait < 0) {
5030Sstevel@tonic-gate kmem_free(tmpdpp, tsdpsz);
5040Sstevel@tonic-gate return (EINVAL);
5050Sstevel@tonic-gate }
5060Sstevel@tonic-gate if (tmpdpp[i].ts_lwait > ts_maxumdpri ||
5070Sstevel@tonic-gate tmpdpp[i].ts_lwait < 0) {
5080Sstevel@tonic-gate kmem_free(tmpdpp, tsdpsz);
5090Sstevel@tonic-gate return (EINVAL);
5100Sstevel@tonic-gate }
5110Sstevel@tonic-gate }
5120Sstevel@tonic-gate
5130Sstevel@tonic-gate /*
5140Sstevel@tonic-gate * Copy the user supplied values over the current ts_dptbl
5150Sstevel@tonic-gate * values. The ts_globpri member is read-only so we don't
5160Sstevel@tonic-gate * overwrite it.
5170Sstevel@tonic-gate */
5180Sstevel@tonic-gate mutex_enter(&ts_dptblock);
5190Sstevel@tonic-gate for (i = 0; i < tsadmin.ts_ndpents; i++) {
5200Sstevel@tonic-gate ts_dptbl[i].ts_quantum = tmpdpp[i].ts_quantum;
5210Sstevel@tonic-gate ts_dptbl[i].ts_tqexp = tmpdpp[i].ts_tqexp;
5220Sstevel@tonic-gate ts_dptbl[i].ts_slpret = tmpdpp[i].ts_slpret;
5230Sstevel@tonic-gate ts_dptbl[i].ts_maxwait = tmpdpp[i].ts_maxwait;
5240Sstevel@tonic-gate ts_dptbl[i].ts_lwait = tmpdpp[i].ts_lwait;
5250Sstevel@tonic-gate }
5260Sstevel@tonic-gate mutex_exit(&ts_dptblock);
5270Sstevel@tonic-gate kmem_free(tmpdpp, tsdpsz);
5280Sstevel@tonic-gate break;
5290Sstevel@tonic-gate
5300Sstevel@tonic-gate default:
5310Sstevel@tonic-gate return (EINVAL);
5320Sstevel@tonic-gate }
5330Sstevel@tonic-gate return (0);
5340Sstevel@tonic-gate }
5350Sstevel@tonic-gate
5360Sstevel@tonic-gate
5370Sstevel@tonic-gate /*
5380Sstevel@tonic-gate * Allocate a time-sharing class specific thread structure and
5390Sstevel@tonic-gate * initialize it with the parameters supplied. Also move the thread
5400Sstevel@tonic-gate * to specified time-sharing priority.
5410Sstevel@tonic-gate */
5420Sstevel@tonic-gate static int
ts_enterclass(kthread_t * t,id_t cid,void * parmsp,cred_t * reqpcredp,void * bufp)5430Sstevel@tonic-gate ts_enterclass(kthread_t *t, id_t cid, void *parmsp,
5440Sstevel@tonic-gate cred_t *reqpcredp, void *bufp)
5450Sstevel@tonic-gate {
5460Sstevel@tonic-gate tsparms_t *tsparmsp = (tsparms_t *)parmsp;
5470Sstevel@tonic-gate tsproc_t *tspp;
5480Sstevel@tonic-gate pri_t reqtsuprilim;
5490Sstevel@tonic-gate pri_t reqtsupri;
5500Sstevel@tonic-gate static uint32_t tspexists = 0; /* set on first occurrence of */
5510Sstevel@tonic-gate /* a time-sharing process */
5520Sstevel@tonic-gate
5530Sstevel@tonic-gate tspp = (tsproc_t *)bufp;
5540Sstevel@tonic-gate ASSERT(tspp != NULL);
5550Sstevel@tonic-gate
5560Sstevel@tonic-gate /*
5570Sstevel@tonic-gate * Initialize the tsproc structure.
5580Sstevel@tonic-gate */
5590Sstevel@tonic-gate tspp->ts_cpupri = tsmedumdpri;
5600Sstevel@tonic-gate if (cid == ia_cid) {
5610Sstevel@tonic-gate /*
5620Sstevel@tonic-gate * Check to make sure caller is either privileged or the
5630Sstevel@tonic-gate * window system. When the window system is converted
5640Sstevel@tonic-gate * to using privileges, the second check can go away.
5650Sstevel@tonic-gate */
5660Sstevel@tonic-gate if (reqpcredp != NULL && !groupmember(IA_gid, reqpcredp) &&
5670Sstevel@tonic-gate secpolicy_setpriority(reqpcredp) != 0)
5680Sstevel@tonic-gate return (EPERM);
5690Sstevel@tonic-gate /*
5700Sstevel@tonic-gate * Belongs to IA "class", so set appropriate flags.
5710Sstevel@tonic-gate * Mark as 'on' so it will not be a swap victim
5720Sstevel@tonic-gate * while forking.
5730Sstevel@tonic-gate */
5740Sstevel@tonic-gate tspp->ts_flags = TSIA | TSIASET;
5750Sstevel@tonic-gate tspp->ts_boost = ia_boost;
5760Sstevel@tonic-gate } else {
5770Sstevel@tonic-gate tspp->ts_flags = 0;
5780Sstevel@tonic-gate tspp->ts_boost = 0;
5790Sstevel@tonic-gate }
5800Sstevel@tonic-gate
5810Sstevel@tonic-gate if (tsparmsp == NULL) {
5820Sstevel@tonic-gate /*
5830Sstevel@tonic-gate * Use default values.
5840Sstevel@tonic-gate */
5850Sstevel@tonic-gate tspp->ts_uprilim = tspp->ts_upri = 0;
5860Sstevel@tonic-gate tspp->ts_nice = NZERO;
5870Sstevel@tonic-gate } else {
5880Sstevel@tonic-gate /*
5890Sstevel@tonic-gate * Use supplied values.
5900Sstevel@tonic-gate */
5910Sstevel@tonic-gate if (tsparmsp->ts_uprilim == TS_NOCHANGE)
5920Sstevel@tonic-gate reqtsuprilim = 0;
5930Sstevel@tonic-gate else {
5940Sstevel@tonic-gate if (tsparmsp->ts_uprilim > 0 &&
5950Sstevel@tonic-gate secpolicy_setpriority(reqpcredp) != 0)
5960Sstevel@tonic-gate return (EPERM);
5970Sstevel@tonic-gate reqtsuprilim = tsparmsp->ts_uprilim;
5980Sstevel@tonic-gate }
5990Sstevel@tonic-gate
6000Sstevel@tonic-gate if (tsparmsp->ts_upri == TS_NOCHANGE) {
6010Sstevel@tonic-gate reqtsupri = reqtsuprilim;
6020Sstevel@tonic-gate } else {
6030Sstevel@tonic-gate if (tsparmsp->ts_upri > 0 &&
6040Sstevel@tonic-gate secpolicy_setpriority(reqpcredp) != 0)
6050Sstevel@tonic-gate return (EPERM);
6060Sstevel@tonic-gate /*
6070Sstevel@tonic-gate * Set the user priority to the requested value
6080Sstevel@tonic-gate * or the upri limit, whichever is lower.
6090Sstevel@tonic-gate */
6100Sstevel@tonic-gate reqtsupri = tsparmsp->ts_upri;
6110Sstevel@tonic-gate if (reqtsupri > reqtsuprilim)
6120Sstevel@tonic-gate reqtsupri = reqtsuprilim;
6130Sstevel@tonic-gate }
6140Sstevel@tonic-gate
6150Sstevel@tonic-gate
6160Sstevel@tonic-gate tspp->ts_uprilim = reqtsuprilim;
6170Sstevel@tonic-gate tspp->ts_upri = reqtsupri;
6186247Sraf tspp->ts_nice = NZERO - (NZERO * reqtsupri) / ts_maxupri;
6190Sstevel@tonic-gate }
6200Sstevel@tonic-gate TS_NEWUMDPRI(tspp);
6210Sstevel@tonic-gate
6220Sstevel@tonic-gate tspp->ts_dispwait = 0;
6230Sstevel@tonic-gate tspp->ts_timeleft = ts_dptbl[tspp->ts_cpupri].ts_quantum;
6240Sstevel@tonic-gate tspp->ts_tp = t;
6253792Sakolb cpucaps_sc_init(&tspp->ts_caps);
6260Sstevel@tonic-gate
6270Sstevel@tonic-gate /*
6280Sstevel@tonic-gate * Reset priority. Process goes to a "user mode" priority
6290Sstevel@tonic-gate * here regardless of whether or not it has slept since
6300Sstevel@tonic-gate * entering the kernel.
6310Sstevel@tonic-gate */
6320Sstevel@tonic-gate thread_lock(t); /* get dispatcher lock on thread */
6330Sstevel@tonic-gate t->t_clfuncs = &(sclass[cid].cl_funcs->thread);
6340Sstevel@tonic-gate t->t_cid = cid;
6350Sstevel@tonic-gate t->t_cldata = (void *)tspp;
6360Sstevel@tonic-gate t->t_schedflag &= ~TS_RUNQMATCH;
6370Sstevel@tonic-gate ts_change_priority(t, tspp);
6380Sstevel@tonic-gate thread_unlock(t);
6390Sstevel@tonic-gate
6400Sstevel@tonic-gate /*
6410Sstevel@tonic-gate * Link new structure into tsproc list.
6420Sstevel@tonic-gate */
6430Sstevel@tonic-gate TS_LIST_INSERT(tspp);
6440Sstevel@tonic-gate
6450Sstevel@tonic-gate /*
6460Sstevel@tonic-gate * If this is the first time-sharing thread to occur since
6470Sstevel@tonic-gate * boot we set up the initial call to ts_update() here.
6480Sstevel@tonic-gate * Use an atomic compare-and-swap since that's easier and
6490Sstevel@tonic-gate * faster than a mutex (but check with an ordinary load first
6500Sstevel@tonic-gate * since most of the time this will already be done).
6510Sstevel@tonic-gate */
6520Sstevel@tonic-gate if (tspexists == 0 && cas32(&tspexists, 0, 1) == 0)
6530Sstevel@tonic-gate (void) timeout(ts_update, NULL, hz);
6540Sstevel@tonic-gate
6550Sstevel@tonic-gate return (0);
6560Sstevel@tonic-gate }
6570Sstevel@tonic-gate
6580Sstevel@tonic-gate
6590Sstevel@tonic-gate /*
6600Sstevel@tonic-gate * Free tsproc structure of thread.
6610Sstevel@tonic-gate */
6620Sstevel@tonic-gate static void
ts_exitclass(void * procp)6630Sstevel@tonic-gate ts_exitclass(void *procp)
6640Sstevel@tonic-gate {
6650Sstevel@tonic-gate tsproc_t *tspp = (tsproc_t *)procp;
6660Sstevel@tonic-gate
6670Sstevel@tonic-gate /* Remove tsproc_t structure from list */
6680Sstevel@tonic-gate TS_LIST_DELETE(tspp);
6690Sstevel@tonic-gate kmem_free(tspp, sizeof (tsproc_t));
6700Sstevel@tonic-gate }
6710Sstevel@tonic-gate
6720Sstevel@tonic-gate /* ARGSUSED */
6730Sstevel@tonic-gate static int
ts_canexit(kthread_t * t,cred_t * cred)6740Sstevel@tonic-gate ts_canexit(kthread_t *t, cred_t *cred)
6750Sstevel@tonic-gate {
6760Sstevel@tonic-gate /*
6770Sstevel@tonic-gate * A thread can always leave a TS/IA class
6780Sstevel@tonic-gate */
6790Sstevel@tonic-gate return (0);
6800Sstevel@tonic-gate }
6810Sstevel@tonic-gate
6820Sstevel@tonic-gate static int
ts_fork(kthread_t * t,kthread_t * ct,void * bufp)6830Sstevel@tonic-gate ts_fork(kthread_t *t, kthread_t *ct, void *bufp)
6840Sstevel@tonic-gate {
6850Sstevel@tonic-gate tsproc_t *ptspp; /* ptr to parent's tsproc structure */
6860Sstevel@tonic-gate tsproc_t *ctspp; /* ptr to child's tsproc structure */
6870Sstevel@tonic-gate
6880Sstevel@tonic-gate ASSERT(MUTEX_HELD(&ttoproc(t)->p_lock));
6890Sstevel@tonic-gate
6900Sstevel@tonic-gate ctspp = (tsproc_t *)bufp;
6910Sstevel@tonic-gate ASSERT(ctspp != NULL);
6920Sstevel@tonic-gate ptspp = (tsproc_t *)t->t_cldata;
6930Sstevel@tonic-gate /*
6940Sstevel@tonic-gate * Initialize child's tsproc structure.
6950Sstevel@tonic-gate */
6960Sstevel@tonic-gate thread_lock(t);
6970Sstevel@tonic-gate ctspp->ts_timeleft = ts_dptbl[ptspp->ts_cpupri].ts_quantum;
6980Sstevel@tonic-gate ctspp->ts_cpupri = ptspp->ts_cpupri;
6990Sstevel@tonic-gate ctspp->ts_boost = ptspp->ts_boost;
7000Sstevel@tonic-gate ctspp->ts_uprilim = ptspp->ts_uprilim;
7010Sstevel@tonic-gate ctspp->ts_upri = ptspp->ts_upri;
7020Sstevel@tonic-gate TS_NEWUMDPRI(ctspp);
7030Sstevel@tonic-gate ctspp->ts_nice = ptspp->ts_nice;
7040Sstevel@tonic-gate ctspp->ts_dispwait = 0;
705951Sraf ctspp->ts_flags = ptspp->ts_flags & ~(TSKPRI | TSBACKQ | TSRESTORE);
7060Sstevel@tonic-gate ctspp->ts_tp = ct;
7073792Sakolb cpucaps_sc_init(&ctspp->ts_caps);
7080Sstevel@tonic-gate thread_unlock(t);
7090Sstevel@tonic-gate
7100Sstevel@tonic-gate /*
7110Sstevel@tonic-gate * Link new structure into tsproc list.
7120Sstevel@tonic-gate */
7130Sstevel@tonic-gate ct->t_cldata = (void *)ctspp;
7140Sstevel@tonic-gate TS_LIST_INSERT(ctspp);
7150Sstevel@tonic-gate return (0);
7160Sstevel@tonic-gate }
7170Sstevel@tonic-gate
7180Sstevel@tonic-gate
7190Sstevel@tonic-gate /*
7200Sstevel@tonic-gate * Child is placed at back of dispatcher queue and parent gives
7210Sstevel@tonic-gate * up processor so that the child runs first after the fork.
7220Sstevel@tonic-gate * This allows the child immediately execing to break the multiple
7230Sstevel@tonic-gate * use of copy on write pages with no disk home. The parent will
7240Sstevel@tonic-gate * get to steal them back rather than uselessly copying them.
7250Sstevel@tonic-gate */
7260Sstevel@tonic-gate static void
ts_forkret(kthread_t * t,kthread_t * ct)7270Sstevel@tonic-gate ts_forkret(kthread_t *t, kthread_t *ct)
7280Sstevel@tonic-gate {
7290Sstevel@tonic-gate proc_t *pp = ttoproc(t);
7300Sstevel@tonic-gate proc_t *cp = ttoproc(ct);
7310Sstevel@tonic-gate tsproc_t *tspp;
7320Sstevel@tonic-gate
7330Sstevel@tonic-gate ASSERT(t == curthread);
7340Sstevel@tonic-gate ASSERT(MUTEX_HELD(&pidlock));
7350Sstevel@tonic-gate
7360Sstevel@tonic-gate /*
7370Sstevel@tonic-gate * Grab the child's p_lock before dropping pidlock to ensure
7380Sstevel@tonic-gate * the process does not disappear before we set it running.
7390Sstevel@tonic-gate */
7400Sstevel@tonic-gate mutex_enter(&cp->p_lock);
7410Sstevel@tonic-gate continuelwps(cp);
7420Sstevel@tonic-gate mutex_exit(&cp->p_lock);
7430Sstevel@tonic-gate
7440Sstevel@tonic-gate mutex_enter(&pp->p_lock);
745*12785SPramod.Batni@Sun.COM mutex_exit(&pidlock);
7460Sstevel@tonic-gate continuelwps(pp);
7470Sstevel@tonic-gate
7480Sstevel@tonic-gate thread_lock(t);
7490Sstevel@tonic-gate tspp = (tsproc_t *)(t->t_cldata);
7500Sstevel@tonic-gate tspp->ts_cpupri = ts_dptbl[tspp->ts_cpupri].ts_tqexp;
7510Sstevel@tonic-gate TS_NEWUMDPRI(tspp);
7520Sstevel@tonic-gate tspp->ts_timeleft = ts_dptbl[tspp->ts_cpupri].ts_quantum;
7530Sstevel@tonic-gate tspp->ts_dispwait = 0;
7540Sstevel@tonic-gate t->t_pri = ts_dptbl[tspp->ts_umdpri].ts_globpri;
7550Sstevel@tonic-gate ASSERT(t->t_pri >= 0 && t->t_pri <= ts_maxglobpri);
7560Sstevel@tonic-gate tspp->ts_flags &= ~TSKPRI;
7570Sstevel@tonic-gate THREAD_TRANSITION(t);
7580Sstevel@tonic-gate ts_setrun(t);
7590Sstevel@tonic-gate thread_unlock(t);
760*12785SPramod.Batni@Sun.COM /*
761*12785SPramod.Batni@Sun.COM * Safe to drop p_lock now since since it is safe to change
762*12785SPramod.Batni@Sun.COM * the scheduling class after this point.
763*12785SPramod.Batni@Sun.COM */
764*12785SPramod.Batni@Sun.COM mutex_exit(&pp->p_lock);
7650Sstevel@tonic-gate
7660Sstevel@tonic-gate swtch();
7670Sstevel@tonic-gate }
7680Sstevel@tonic-gate
7690Sstevel@tonic-gate
7700Sstevel@tonic-gate /*
7710Sstevel@tonic-gate * Get information about the time-sharing class into the buffer
7720Sstevel@tonic-gate * pointed to by tsinfop. The maximum configured user priority
7730Sstevel@tonic-gate * is the only information we supply. ts_getclinfo() is called
7740Sstevel@tonic-gate * for TS threads, and ia_getclinfo() is called for IA threads.
7750Sstevel@tonic-gate */
7760Sstevel@tonic-gate static int
ts_getclinfo(void * infop)7770Sstevel@tonic-gate ts_getclinfo(void *infop)
7780Sstevel@tonic-gate {
7790Sstevel@tonic-gate tsinfo_t *tsinfop = (tsinfo_t *)infop;
7800Sstevel@tonic-gate tsinfop->ts_maxupri = ts_maxupri;
7810Sstevel@tonic-gate return (0);
7820Sstevel@tonic-gate }
7830Sstevel@tonic-gate
7840Sstevel@tonic-gate static int
ia_getclinfo(void * infop)7850Sstevel@tonic-gate ia_getclinfo(void *infop)
7860Sstevel@tonic-gate {
7870Sstevel@tonic-gate iainfo_t *iainfop = (iainfo_t *)infop;
7880Sstevel@tonic-gate iainfop->ia_maxupri = ia_maxupri;
7890Sstevel@tonic-gate return (0);
7900Sstevel@tonic-gate }
7910Sstevel@tonic-gate
7920Sstevel@tonic-gate
7930Sstevel@tonic-gate /*
7946247Sraf * Return the user mode scheduling priority range.
7950Sstevel@tonic-gate */
7960Sstevel@tonic-gate static int
ts_getclpri(pcpri_t * pcprip)7970Sstevel@tonic-gate ts_getclpri(pcpri_t *pcprip)
7980Sstevel@tonic-gate {
7996247Sraf pcprip->pc_clpmax = ts_maxupri;
8006247Sraf pcprip->pc_clpmin = -ts_maxupri;
8016247Sraf return (0);
8026247Sraf }
8036247Sraf
8046247Sraf
8056247Sraf static int
ia_getclpri(pcpri_t * pcprip)8066247Sraf ia_getclpri(pcpri_t *pcprip)
8076247Sraf {
8086247Sraf pcprip->pc_clpmax = ia_maxupri;
8096247Sraf pcprip->pc_clpmin = -ia_maxupri;
8100Sstevel@tonic-gate return (0);
8110Sstevel@tonic-gate }
8120Sstevel@tonic-gate
8130Sstevel@tonic-gate
8140Sstevel@tonic-gate static void
ts_nullsys()8150Sstevel@tonic-gate ts_nullsys()
8160Sstevel@tonic-gate {}
8170Sstevel@tonic-gate
8180Sstevel@tonic-gate
8190Sstevel@tonic-gate /*
8200Sstevel@tonic-gate * Get the time-sharing parameters of the thread pointed to by
8210Sstevel@tonic-gate * tsprocp into the buffer pointed to by tsparmsp. ts_parmsget()
8220Sstevel@tonic-gate * is called for TS threads, and ia_parmsget() is called for IA
8230Sstevel@tonic-gate * threads.
8240Sstevel@tonic-gate */
8250Sstevel@tonic-gate static void
ts_parmsget(kthread_t * t,void * parmsp)8260Sstevel@tonic-gate ts_parmsget(kthread_t *t, void *parmsp)
8270Sstevel@tonic-gate {
8280Sstevel@tonic-gate tsproc_t *tspp = (tsproc_t *)t->t_cldata;
8290Sstevel@tonic-gate tsparms_t *tsparmsp = (tsparms_t *)parmsp;
8300Sstevel@tonic-gate
8310Sstevel@tonic-gate tsparmsp->ts_uprilim = tspp->ts_uprilim;
8320Sstevel@tonic-gate tsparmsp->ts_upri = tspp->ts_upri;
8330Sstevel@tonic-gate }
8340Sstevel@tonic-gate
8350Sstevel@tonic-gate static void
ia_parmsget(kthread_t * t,void * parmsp)8360Sstevel@tonic-gate ia_parmsget(kthread_t *t, void *parmsp)
8370Sstevel@tonic-gate {
8380Sstevel@tonic-gate tsproc_t *tspp = (tsproc_t *)t->t_cldata;
8390Sstevel@tonic-gate iaparms_t *iaparmsp = (iaparms_t *)parmsp;
8400Sstevel@tonic-gate
8410Sstevel@tonic-gate iaparmsp->ia_uprilim = tspp->ts_uprilim;
8420Sstevel@tonic-gate iaparmsp->ia_upri = tspp->ts_upri;
8430Sstevel@tonic-gate if (tspp->ts_flags & TSIASET)
8440Sstevel@tonic-gate iaparmsp->ia_mode = IA_SET_INTERACTIVE;
8450Sstevel@tonic-gate else
8460Sstevel@tonic-gate iaparmsp->ia_mode = IA_INTERACTIVE_OFF;
8470Sstevel@tonic-gate }
8480Sstevel@tonic-gate
8490Sstevel@tonic-gate
8500Sstevel@tonic-gate /*
8510Sstevel@tonic-gate * Check the validity of the time-sharing parameters in the buffer
8520Sstevel@tonic-gate * pointed to by tsparmsp.
8530Sstevel@tonic-gate * ts_parmsin() is called for TS threads, and ia_parmsin() is called
8540Sstevel@tonic-gate * for IA threads.
8550Sstevel@tonic-gate */
8560Sstevel@tonic-gate static int
ts_parmsin(void * parmsp)8570Sstevel@tonic-gate ts_parmsin(void *parmsp)
8580Sstevel@tonic-gate {
8590Sstevel@tonic-gate tsparms_t *tsparmsp = (tsparms_t *)parmsp;
8600Sstevel@tonic-gate /*
8610Sstevel@tonic-gate * Check validity of parameters.
8620Sstevel@tonic-gate */
8630Sstevel@tonic-gate if ((tsparmsp->ts_uprilim > ts_maxupri ||
8640Sstevel@tonic-gate tsparmsp->ts_uprilim < -ts_maxupri) &&
8650Sstevel@tonic-gate tsparmsp->ts_uprilim != TS_NOCHANGE)
8660Sstevel@tonic-gate return (EINVAL);
8670Sstevel@tonic-gate
8680Sstevel@tonic-gate if ((tsparmsp->ts_upri > ts_maxupri ||
8690Sstevel@tonic-gate tsparmsp->ts_upri < -ts_maxupri) &&
8700Sstevel@tonic-gate tsparmsp->ts_upri != TS_NOCHANGE)
8710Sstevel@tonic-gate return (EINVAL);
8720Sstevel@tonic-gate
8730Sstevel@tonic-gate return (0);
8740Sstevel@tonic-gate }
8750Sstevel@tonic-gate
8760Sstevel@tonic-gate static int
ia_parmsin(void * parmsp)8770Sstevel@tonic-gate ia_parmsin(void *parmsp)
8780Sstevel@tonic-gate {
8790Sstevel@tonic-gate iaparms_t *iaparmsp = (iaparms_t *)parmsp;
8800Sstevel@tonic-gate
8810Sstevel@tonic-gate if ((iaparmsp->ia_uprilim > ia_maxupri ||
8820Sstevel@tonic-gate iaparmsp->ia_uprilim < -ia_maxupri) &&
8830Sstevel@tonic-gate iaparmsp->ia_uprilim != IA_NOCHANGE) {
8840Sstevel@tonic-gate return (EINVAL);
8850Sstevel@tonic-gate }
8860Sstevel@tonic-gate
8870Sstevel@tonic-gate if ((iaparmsp->ia_upri > ia_maxupri ||
8880Sstevel@tonic-gate iaparmsp->ia_upri < -ia_maxupri) &&
8890Sstevel@tonic-gate iaparmsp->ia_upri != IA_NOCHANGE) {
8900Sstevel@tonic-gate return (EINVAL);
8910Sstevel@tonic-gate }
8920Sstevel@tonic-gate
8930Sstevel@tonic-gate return (0);
8940Sstevel@tonic-gate }
8950Sstevel@tonic-gate
8960Sstevel@tonic-gate
8970Sstevel@tonic-gate /*
8980Sstevel@tonic-gate * Check the validity of the time-sharing parameters in the pc_vaparms_t
8990Sstevel@tonic-gate * structure vaparmsp and put them in the buffer pointed to by tsparmsp.
9000Sstevel@tonic-gate * pc_vaparms_t contains (key, value) pairs of parameter.
9010Sstevel@tonic-gate * ts_vaparmsin() is called for TS threads, and ia_vaparmsin() is called
9020Sstevel@tonic-gate * for IA threads. ts_vaparmsin() is the variable parameter version of
9030Sstevel@tonic-gate * ts_parmsin() and ia_vaparmsin() is the variable parameter version of
9040Sstevel@tonic-gate * ia_parmsin().
9050Sstevel@tonic-gate */
9060Sstevel@tonic-gate static int
ts_vaparmsin(void * parmsp,pc_vaparms_t * vaparmsp)9070Sstevel@tonic-gate ts_vaparmsin(void *parmsp, pc_vaparms_t *vaparmsp)
9080Sstevel@tonic-gate {
9090Sstevel@tonic-gate tsparms_t *tsparmsp = (tsparms_t *)parmsp;
9100Sstevel@tonic-gate int priflag = 0;
9110Sstevel@tonic-gate int limflag = 0;
9120Sstevel@tonic-gate uint_t cnt;
9130Sstevel@tonic-gate pc_vaparm_t *vpp = &vaparmsp->pc_parms[0];
9140Sstevel@tonic-gate
9150Sstevel@tonic-gate
9160Sstevel@tonic-gate /*
9170Sstevel@tonic-gate * TS_NOCHANGE (-32768) is outside of the range of values for
9180Sstevel@tonic-gate * ts_uprilim and ts_upri. If the structure tsparms_t is changed,
9190Sstevel@tonic-gate * TS_NOCHANGE should be replaced by a flag word (in the same manner
9200Sstevel@tonic-gate * as in rt.c).
9210Sstevel@tonic-gate */
9220Sstevel@tonic-gate tsparmsp->ts_uprilim = TS_NOCHANGE;
9230Sstevel@tonic-gate tsparmsp->ts_upri = TS_NOCHANGE;
9240Sstevel@tonic-gate
9250Sstevel@tonic-gate /*
9260Sstevel@tonic-gate * Get the varargs parameter and check validity of parameters.
9270Sstevel@tonic-gate */
9280Sstevel@tonic-gate if (vaparmsp->pc_vaparmscnt > PC_VAPARMCNT)
9290Sstevel@tonic-gate return (EINVAL);
9300Sstevel@tonic-gate
9310Sstevel@tonic-gate for (cnt = 0; cnt < vaparmsp->pc_vaparmscnt; cnt++, vpp++) {
9320Sstevel@tonic-gate
9330Sstevel@tonic-gate switch (vpp->pc_key) {
9340Sstevel@tonic-gate case TS_KY_UPRILIM:
9350Sstevel@tonic-gate if (limflag++)
9360Sstevel@tonic-gate return (EINVAL);
9370Sstevel@tonic-gate tsparmsp->ts_uprilim = (pri_t)vpp->pc_parm;
9380Sstevel@tonic-gate if (tsparmsp->ts_uprilim > ts_maxupri ||
9390Sstevel@tonic-gate tsparmsp->ts_uprilim < -ts_maxupri)
9400Sstevel@tonic-gate return (EINVAL);
9410Sstevel@tonic-gate break;
9420Sstevel@tonic-gate
9430Sstevel@tonic-gate case TS_KY_UPRI:
9440Sstevel@tonic-gate if (priflag++)
9450Sstevel@tonic-gate return (EINVAL);
9460Sstevel@tonic-gate tsparmsp->ts_upri = (pri_t)vpp->pc_parm;
9470Sstevel@tonic-gate if (tsparmsp->ts_upri > ts_maxupri ||
9480Sstevel@tonic-gate tsparmsp->ts_upri < -ts_maxupri)
9490Sstevel@tonic-gate return (EINVAL);
9500Sstevel@tonic-gate break;
9510Sstevel@tonic-gate
9520Sstevel@tonic-gate default:
9530Sstevel@tonic-gate return (EINVAL);
9540Sstevel@tonic-gate }
9550Sstevel@tonic-gate }
9560Sstevel@tonic-gate
9570Sstevel@tonic-gate if (vaparmsp->pc_vaparmscnt == 0) {
9580Sstevel@tonic-gate /*
9590Sstevel@tonic-gate * Use default parameters.
9600Sstevel@tonic-gate */
9610Sstevel@tonic-gate tsparmsp->ts_upri = tsparmsp->ts_uprilim = 0;
9620Sstevel@tonic-gate }
9630Sstevel@tonic-gate
9640Sstevel@tonic-gate return (0);
9650Sstevel@tonic-gate }
9660Sstevel@tonic-gate
9670Sstevel@tonic-gate static int
ia_vaparmsin(void * parmsp,pc_vaparms_t * vaparmsp)9680Sstevel@tonic-gate ia_vaparmsin(void *parmsp, pc_vaparms_t *vaparmsp)
9690Sstevel@tonic-gate {
9700Sstevel@tonic-gate iaparms_t *iaparmsp = (iaparms_t *)parmsp;
9710Sstevel@tonic-gate int priflag = 0;
9720Sstevel@tonic-gate int limflag = 0;
9730Sstevel@tonic-gate int mflag = 0;
9740Sstevel@tonic-gate uint_t cnt;
9750Sstevel@tonic-gate pc_vaparm_t *vpp = &vaparmsp->pc_parms[0];
9760Sstevel@tonic-gate
9770Sstevel@tonic-gate /*
9780Sstevel@tonic-gate * IA_NOCHANGE (-32768) is outside of the range of values for
9790Sstevel@tonic-gate * ia_uprilim, ia_upri and ia_mode. If the structure iaparms_t is
9800Sstevel@tonic-gate * changed, IA_NOCHANGE should be replaced by a flag word (in the
9810Sstevel@tonic-gate * same manner as in rt.c).
9820Sstevel@tonic-gate */
9830Sstevel@tonic-gate iaparmsp->ia_uprilim = IA_NOCHANGE;
9840Sstevel@tonic-gate iaparmsp->ia_upri = IA_NOCHANGE;
9850Sstevel@tonic-gate iaparmsp->ia_mode = IA_NOCHANGE;
9860Sstevel@tonic-gate
9870Sstevel@tonic-gate /*
9880Sstevel@tonic-gate * Get the varargs parameter and check validity of parameters.
9890Sstevel@tonic-gate */
9900Sstevel@tonic-gate if (vaparmsp->pc_vaparmscnt > PC_VAPARMCNT)
9910Sstevel@tonic-gate return (EINVAL);
9920Sstevel@tonic-gate
9930Sstevel@tonic-gate for (cnt = 0; cnt < vaparmsp->pc_vaparmscnt; cnt++, vpp++) {
9940Sstevel@tonic-gate
9950Sstevel@tonic-gate switch (vpp->pc_key) {
9960Sstevel@tonic-gate case IA_KY_UPRILIM:
9970Sstevel@tonic-gate if (limflag++)
9980Sstevel@tonic-gate return (EINVAL);
9990Sstevel@tonic-gate iaparmsp->ia_uprilim = (pri_t)vpp->pc_parm;
10000Sstevel@tonic-gate if (iaparmsp->ia_uprilim > ia_maxupri ||
10010Sstevel@tonic-gate iaparmsp->ia_uprilim < -ia_maxupri)
10020Sstevel@tonic-gate return (EINVAL);
10030Sstevel@tonic-gate break;
10040Sstevel@tonic-gate
10050Sstevel@tonic-gate case IA_KY_UPRI:
10060Sstevel@tonic-gate if (priflag++)
10070Sstevel@tonic-gate return (EINVAL);
10080Sstevel@tonic-gate iaparmsp->ia_upri = (pri_t)vpp->pc_parm;
10090Sstevel@tonic-gate if (iaparmsp->ia_upri > ia_maxupri ||
10100Sstevel@tonic-gate iaparmsp->ia_upri < -ia_maxupri)
10110Sstevel@tonic-gate return (EINVAL);
10120Sstevel@tonic-gate break;
10130Sstevel@tonic-gate
10140Sstevel@tonic-gate case IA_KY_MODE:
10150Sstevel@tonic-gate if (mflag++)
10160Sstevel@tonic-gate return (EINVAL);
10170Sstevel@tonic-gate iaparmsp->ia_mode = (int)vpp->pc_parm;
10180Sstevel@tonic-gate if (iaparmsp->ia_mode != IA_SET_INTERACTIVE &&
10190Sstevel@tonic-gate iaparmsp->ia_mode != IA_INTERACTIVE_OFF)
10200Sstevel@tonic-gate return (EINVAL);
10210Sstevel@tonic-gate break;
10220Sstevel@tonic-gate
10230Sstevel@tonic-gate default:
10240Sstevel@tonic-gate return (EINVAL);
10250Sstevel@tonic-gate }
10260Sstevel@tonic-gate }
10270Sstevel@tonic-gate
10280Sstevel@tonic-gate if (vaparmsp->pc_vaparmscnt == 0) {
10290Sstevel@tonic-gate /*
10300Sstevel@tonic-gate * Use default parameters.
10310Sstevel@tonic-gate */
10320Sstevel@tonic-gate iaparmsp->ia_upri = iaparmsp->ia_uprilim = 0;
10330Sstevel@tonic-gate iaparmsp->ia_mode = IA_SET_INTERACTIVE;
10340Sstevel@tonic-gate }
10350Sstevel@tonic-gate
10360Sstevel@tonic-gate return (0);
10370Sstevel@tonic-gate }
10380Sstevel@tonic-gate
10390Sstevel@tonic-gate /*
10400Sstevel@tonic-gate * Nothing to do here but return success.
10410Sstevel@tonic-gate */
10420Sstevel@tonic-gate /* ARGSUSED */
10430Sstevel@tonic-gate static int
ts_parmsout(void * parmsp,pc_vaparms_t * vaparmsp)10440Sstevel@tonic-gate ts_parmsout(void *parmsp, pc_vaparms_t *vaparmsp)
10450Sstevel@tonic-gate {
10460Sstevel@tonic-gate return (0);
10470Sstevel@tonic-gate }
10480Sstevel@tonic-gate
10490Sstevel@tonic-gate
10500Sstevel@tonic-gate /*
10510Sstevel@tonic-gate * Copy all selected time-sharing class parameters to the user.
10520Sstevel@tonic-gate * The parameters are specified by a key.
10530Sstevel@tonic-gate */
10540Sstevel@tonic-gate static int
ts_vaparmsout(void * prmsp,pc_vaparms_t * vaparmsp)10550Sstevel@tonic-gate ts_vaparmsout(void *prmsp, pc_vaparms_t *vaparmsp)
10560Sstevel@tonic-gate {
10570Sstevel@tonic-gate tsparms_t *tsprmsp = (tsparms_t *)prmsp;
10580Sstevel@tonic-gate int priflag = 0;
10590Sstevel@tonic-gate int limflag = 0;
10600Sstevel@tonic-gate uint_t cnt;
10610Sstevel@tonic-gate pc_vaparm_t *vpp = &vaparmsp->pc_parms[0];
10620Sstevel@tonic-gate
10630Sstevel@tonic-gate ASSERT(MUTEX_NOT_HELD(&curproc->p_lock));
10640Sstevel@tonic-gate
10650Sstevel@tonic-gate if (vaparmsp->pc_vaparmscnt > PC_VAPARMCNT)
10660Sstevel@tonic-gate return (EINVAL);
10670Sstevel@tonic-gate
10680Sstevel@tonic-gate for (cnt = 0; cnt < vaparmsp->pc_vaparmscnt; cnt++, vpp++) {
10690Sstevel@tonic-gate
10700Sstevel@tonic-gate switch (vpp->pc_key) {
10710Sstevel@tonic-gate case TS_KY_UPRILIM:
10720Sstevel@tonic-gate if (limflag++)
10730Sstevel@tonic-gate return (EINVAL);
10740Sstevel@tonic-gate if (copyout(&tsprmsp->ts_uprilim,
10750Sstevel@tonic-gate (caddr_t)(uintptr_t)vpp->pc_parm, sizeof (pri_t)))
10760Sstevel@tonic-gate return (EFAULT);
10770Sstevel@tonic-gate break;
10780Sstevel@tonic-gate
10790Sstevel@tonic-gate case TS_KY_UPRI:
10800Sstevel@tonic-gate if (priflag++)
10810Sstevel@tonic-gate return (EINVAL);
10820Sstevel@tonic-gate if (copyout(&tsprmsp->ts_upri,
10830Sstevel@tonic-gate (caddr_t)(uintptr_t)vpp->pc_parm, sizeof (pri_t)))
10840Sstevel@tonic-gate return (EFAULT);
10850Sstevel@tonic-gate break;
10860Sstevel@tonic-gate
10870Sstevel@tonic-gate default:
10880Sstevel@tonic-gate return (EINVAL);
10890Sstevel@tonic-gate }
10900Sstevel@tonic-gate }
10910Sstevel@tonic-gate
10920Sstevel@tonic-gate return (0);
10930Sstevel@tonic-gate }
10940Sstevel@tonic-gate
10950Sstevel@tonic-gate
10960Sstevel@tonic-gate /*
10970Sstevel@tonic-gate * Copy all selected interactive class parameters to the user.
10980Sstevel@tonic-gate * The parameters are specified by a key.
10990Sstevel@tonic-gate */
11000Sstevel@tonic-gate static int
ia_vaparmsout(void * prmsp,pc_vaparms_t * vaparmsp)11010Sstevel@tonic-gate ia_vaparmsout(void *prmsp, pc_vaparms_t *vaparmsp)
11020Sstevel@tonic-gate {
11030Sstevel@tonic-gate iaparms_t *iaprmsp = (iaparms_t *)prmsp;
11040Sstevel@tonic-gate int priflag = 0;
11050Sstevel@tonic-gate int limflag = 0;
11060Sstevel@tonic-gate int mflag = 0;
11070Sstevel@tonic-gate uint_t cnt;
11080Sstevel@tonic-gate pc_vaparm_t *vpp = &vaparmsp->pc_parms[0];
11090Sstevel@tonic-gate
11100Sstevel@tonic-gate ASSERT(MUTEX_NOT_HELD(&curproc->p_lock));
11110Sstevel@tonic-gate
11120Sstevel@tonic-gate if (vaparmsp->pc_vaparmscnt > PC_VAPARMCNT)
11130Sstevel@tonic-gate return (EINVAL);
11140Sstevel@tonic-gate
11150Sstevel@tonic-gate for (cnt = 0; cnt < vaparmsp->pc_vaparmscnt; cnt++, vpp++) {
11160Sstevel@tonic-gate
11170Sstevel@tonic-gate switch (vpp->pc_key) {
11180Sstevel@tonic-gate case IA_KY_UPRILIM:
11190Sstevel@tonic-gate if (limflag++)
11200Sstevel@tonic-gate return (EINVAL);
11210Sstevel@tonic-gate if (copyout(&iaprmsp->ia_uprilim,
11220Sstevel@tonic-gate (caddr_t)(uintptr_t)vpp->pc_parm, sizeof (pri_t)))
11230Sstevel@tonic-gate return (EFAULT);
11240Sstevel@tonic-gate break;
11250Sstevel@tonic-gate
11260Sstevel@tonic-gate case IA_KY_UPRI:
11270Sstevel@tonic-gate if (priflag++)
11280Sstevel@tonic-gate return (EINVAL);
11290Sstevel@tonic-gate if (copyout(&iaprmsp->ia_upri,
11300Sstevel@tonic-gate (caddr_t)(uintptr_t)vpp->pc_parm, sizeof (pri_t)))
11310Sstevel@tonic-gate return (EFAULT);
11320Sstevel@tonic-gate break;
11330Sstevel@tonic-gate
11340Sstevel@tonic-gate case IA_KY_MODE:
11350Sstevel@tonic-gate if (mflag++)
11360Sstevel@tonic-gate return (EINVAL);
11370Sstevel@tonic-gate if (copyout(&iaprmsp->ia_mode,
11380Sstevel@tonic-gate (caddr_t)(uintptr_t)vpp->pc_parm, sizeof (int)))
11390Sstevel@tonic-gate return (EFAULT);
11400Sstevel@tonic-gate break;
11410Sstevel@tonic-gate
11420Sstevel@tonic-gate default:
11430Sstevel@tonic-gate return (EINVAL);
11440Sstevel@tonic-gate }
11450Sstevel@tonic-gate }
11460Sstevel@tonic-gate return (0);
11470Sstevel@tonic-gate }
11480Sstevel@tonic-gate
11490Sstevel@tonic-gate
11500Sstevel@tonic-gate /*
11510Sstevel@tonic-gate * Set the scheduling parameters of the thread pointed to by tsprocp
11520Sstevel@tonic-gate * to those specified in the buffer pointed to by tsparmsp.
11530Sstevel@tonic-gate * ts_parmsset() is called for TS threads, and ia_parmsset() is
11540Sstevel@tonic-gate * called for IA threads.
11550Sstevel@tonic-gate */
11560Sstevel@tonic-gate /* ARGSUSED */
11570Sstevel@tonic-gate static int
ts_parmsset(kthread_t * tx,void * parmsp,id_t reqpcid,cred_t * reqpcredp)11580Sstevel@tonic-gate ts_parmsset(kthread_t *tx, void *parmsp, id_t reqpcid, cred_t *reqpcredp)
11590Sstevel@tonic-gate {
11600Sstevel@tonic-gate char nice;
11610Sstevel@tonic-gate pri_t reqtsuprilim;
11620Sstevel@tonic-gate pri_t reqtsupri;
11630Sstevel@tonic-gate tsparms_t *tsparmsp = (tsparms_t *)parmsp;
11640Sstevel@tonic-gate tsproc_t *tspp = (tsproc_t *)tx->t_cldata;
11650Sstevel@tonic-gate
11660Sstevel@tonic-gate ASSERT(MUTEX_HELD(&(ttoproc(tx))->p_lock));
11670Sstevel@tonic-gate
11680Sstevel@tonic-gate if (tsparmsp->ts_uprilim == TS_NOCHANGE)
11690Sstevel@tonic-gate reqtsuprilim = tspp->ts_uprilim;
11700Sstevel@tonic-gate else
11710Sstevel@tonic-gate reqtsuprilim = tsparmsp->ts_uprilim;
11720Sstevel@tonic-gate
11730Sstevel@tonic-gate if (tsparmsp->ts_upri == TS_NOCHANGE)
11740Sstevel@tonic-gate reqtsupri = tspp->ts_upri;
11750Sstevel@tonic-gate else
11760Sstevel@tonic-gate reqtsupri = tsparmsp->ts_upri;
11770Sstevel@tonic-gate
11780Sstevel@tonic-gate /*
11790Sstevel@tonic-gate * Make sure the user priority doesn't exceed the upri limit.
11800Sstevel@tonic-gate */
11810Sstevel@tonic-gate if (reqtsupri > reqtsuprilim)
11820Sstevel@tonic-gate reqtsupri = reqtsuprilim;
11830Sstevel@tonic-gate
11840Sstevel@tonic-gate /*
11850Sstevel@tonic-gate * Basic permissions enforced by generic kernel code
11860Sstevel@tonic-gate * for all classes require that a thread attempting
11870Sstevel@tonic-gate * to change the scheduling parameters of a target
11880Sstevel@tonic-gate * thread be privileged or have a real or effective
11890Sstevel@tonic-gate * UID matching that of the target thread. We are not
11900Sstevel@tonic-gate * called unless these basic permission checks have
11910Sstevel@tonic-gate * already passed. The time-sharing class requires in
11920Sstevel@tonic-gate * addition that the calling thread be privileged if it
11930Sstevel@tonic-gate * is attempting to raise the upri limit above its current
11940Sstevel@tonic-gate * value This may have been checked previously but if our
11950Sstevel@tonic-gate * caller passed us a non-NULL credential pointer we assume
11960Sstevel@tonic-gate * it hasn't and we check it here.
11970Sstevel@tonic-gate */
11980Sstevel@tonic-gate if (reqpcredp != NULL &&
11990Sstevel@tonic-gate reqtsuprilim > tspp->ts_uprilim &&
12000Sstevel@tonic-gate secpolicy_setpriority(reqpcredp) != 0)
12010Sstevel@tonic-gate return (EPERM);
12020Sstevel@tonic-gate
12030Sstevel@tonic-gate /*
12040Sstevel@tonic-gate * Set ts_nice to the nice value corresponding to the user
12050Sstevel@tonic-gate * priority we are setting. Note that setting the nice field
12060Sstevel@tonic-gate * of the parameter struct won't affect upri or nice.
12070Sstevel@tonic-gate */
12080Sstevel@tonic-gate nice = NZERO - (reqtsupri * NZERO) / ts_maxupri;
12090Sstevel@tonic-gate if (nice >= 2 * NZERO)
12100Sstevel@tonic-gate nice = 2 * NZERO - 1;
12110Sstevel@tonic-gate
12120Sstevel@tonic-gate thread_lock(tx);
12130Sstevel@tonic-gate
12140Sstevel@tonic-gate tspp->ts_uprilim = reqtsuprilim;
12150Sstevel@tonic-gate tspp->ts_upri = reqtsupri;
12160Sstevel@tonic-gate TS_NEWUMDPRI(tspp);
12170Sstevel@tonic-gate tspp->ts_nice = nice;
12180Sstevel@tonic-gate
12190Sstevel@tonic-gate if ((tspp->ts_flags & TSKPRI) != 0) {
12200Sstevel@tonic-gate thread_unlock(tx);
12210Sstevel@tonic-gate return (0);
12220Sstevel@tonic-gate }
12230Sstevel@tonic-gate
12240Sstevel@tonic-gate tspp->ts_dispwait = 0;
12250Sstevel@tonic-gate ts_change_priority(tx, tspp);
12260Sstevel@tonic-gate thread_unlock(tx);
12270Sstevel@tonic-gate return (0);
12280Sstevel@tonic-gate }
12290Sstevel@tonic-gate
12300Sstevel@tonic-gate
12310Sstevel@tonic-gate static int
ia_parmsset(kthread_t * tx,void * parmsp,id_t reqpcid,cred_t * reqpcredp)12320Sstevel@tonic-gate ia_parmsset(kthread_t *tx, void *parmsp, id_t reqpcid, cred_t *reqpcredp)
12330Sstevel@tonic-gate {
12340Sstevel@tonic-gate tsproc_t *tspp = (tsproc_t *)tx->t_cldata;
12350Sstevel@tonic-gate iaparms_t *iaparmsp = (iaparms_t *)parmsp;
12360Sstevel@tonic-gate proc_t *p;
12370Sstevel@tonic-gate pid_t pid, pgid, sid;
12380Sstevel@tonic-gate pid_t on, off;
12390Sstevel@tonic-gate struct stdata *stp;
12400Sstevel@tonic-gate int sess_held;
12410Sstevel@tonic-gate
12420Sstevel@tonic-gate /*
12430Sstevel@tonic-gate * Handle user priority changes
12440Sstevel@tonic-gate */
12450Sstevel@tonic-gate if (iaparmsp->ia_mode == IA_NOCHANGE)
12460Sstevel@tonic-gate return (ts_parmsset(tx, parmsp, reqpcid, reqpcredp));
12470Sstevel@tonic-gate
12480Sstevel@tonic-gate /*
12490Sstevel@tonic-gate * Check permissions for changing modes.
12500Sstevel@tonic-gate */
12510Sstevel@tonic-gate
12520Sstevel@tonic-gate if (reqpcredp != NULL && !groupmember(IA_gid, reqpcredp) &&
12530Sstevel@tonic-gate secpolicy_setpriority(reqpcredp) != 0) {
12540Sstevel@tonic-gate /*
12550Sstevel@tonic-gate * Silently fail in case this is just a priocntl
12560Sstevel@tonic-gate * call with upri and uprilim set to IA_NOCHANGE.
12570Sstevel@tonic-gate */
12580Sstevel@tonic-gate return (0);
12590Sstevel@tonic-gate }
12600Sstevel@tonic-gate
12610Sstevel@tonic-gate ASSERT(MUTEX_HELD(&pidlock));
12620Sstevel@tonic-gate if ((p = ttoproc(tx)) == NULL) {
12630Sstevel@tonic-gate return (0);
12640Sstevel@tonic-gate }
12650Sstevel@tonic-gate ASSERT(MUTEX_HELD(&p->p_lock));
12660Sstevel@tonic-gate if (p->p_stat == SIDL) {
12670Sstevel@tonic-gate return (0);
12680Sstevel@tonic-gate }
12690Sstevel@tonic-gate pid = p->p_pid;
12700Sstevel@tonic-gate sid = p->p_sessp->s_sid;
12710Sstevel@tonic-gate pgid = p->p_pgrp;
12720Sstevel@tonic-gate if (iaparmsp->ia_mode == IA_SET_INTERACTIVE) {
12730Sstevel@tonic-gate /*
12740Sstevel@tonic-gate * session leaders must be turned on now so all processes
12750Sstevel@tonic-gate * in the group controlling the tty will be turned on or off.
12760Sstevel@tonic-gate * if the ia_mode is off for the session leader,
12770Sstevel@tonic-gate * ia_set_process_group will return without setting the
12780Sstevel@tonic-gate * processes in the group controlling the tty on.
12790Sstevel@tonic-gate */
12800Sstevel@tonic-gate thread_lock(tx);
12810Sstevel@tonic-gate tspp->ts_flags |= TSIASET;
12820Sstevel@tonic-gate thread_unlock(tx);
12830Sstevel@tonic-gate }
12842712Snn35248 mutex_enter(&p->p_sessp->s_lock);
12850Sstevel@tonic-gate sess_held = 1;
12860Sstevel@tonic-gate if ((pid == sid) && (p->p_sessp->s_vp != NULL) &&
12870Sstevel@tonic-gate ((stp = p->p_sessp->s_vp->v_stream) != NULL)) {
12880Sstevel@tonic-gate if ((stp->sd_pgidp != NULL) && (stp->sd_sidp != NULL)) {
12890Sstevel@tonic-gate pgid = stp->sd_pgidp->pid_id;
12900Sstevel@tonic-gate sess_held = 0;
12912712Snn35248 mutex_exit(&p->p_sessp->s_lock);
12920Sstevel@tonic-gate if (iaparmsp->ia_mode ==
12930Sstevel@tonic-gate IA_SET_INTERACTIVE) {
12940Sstevel@tonic-gate off = 0;
12950Sstevel@tonic-gate on = pgid;
12960Sstevel@tonic-gate } else {
12970Sstevel@tonic-gate off = pgid;
12980Sstevel@tonic-gate on = 0;
12990Sstevel@tonic-gate }
13000Sstevel@tonic-gate TRACE_3(TR_FAC_IA, TR_ACTIVE_CHAIN,
13010Sstevel@tonic-gate "active chain:pid %d gid %d %p",
13020Sstevel@tonic-gate pid, pgid, p);
13030Sstevel@tonic-gate ia_set_process_group(sid, off, on);
13040Sstevel@tonic-gate }
13050Sstevel@tonic-gate }
13060Sstevel@tonic-gate if (sess_held)
13072712Snn35248 mutex_exit(&p->p_sessp->s_lock);
13080Sstevel@tonic-gate
13090Sstevel@tonic-gate thread_lock(tx);
13100Sstevel@tonic-gate
13110Sstevel@tonic-gate if (iaparmsp->ia_mode == IA_SET_INTERACTIVE) {
13120Sstevel@tonic-gate tspp->ts_flags |= TSIASET;
13130Sstevel@tonic-gate tspp->ts_boost = ia_boost;
13140Sstevel@tonic-gate } else {
13150Sstevel@tonic-gate tspp->ts_flags &= ~TSIASET;
13160Sstevel@tonic-gate tspp->ts_boost = -ia_boost;
13170Sstevel@tonic-gate }
13180Sstevel@tonic-gate thread_unlock(tx);
13190Sstevel@tonic-gate
13200Sstevel@tonic-gate return (ts_parmsset(tx, parmsp, reqpcid, reqpcredp));
13210Sstevel@tonic-gate }
13220Sstevel@tonic-gate
13233792Sakolb static void
ts_exit(kthread_t * t)13243792Sakolb ts_exit(kthread_t *t)
13253792Sakolb {
13263792Sakolb tsproc_t *tspp;
13273792Sakolb
13283792Sakolb if (CPUCAPS_ON()) {
13293792Sakolb /*
13303792Sakolb * A thread could be exiting in between clock ticks,
13313792Sakolb * so we need to calculate how much CPU time it used
13323792Sakolb * since it was charged last time.
13334698Sakolb *
13344698Sakolb * CPU caps are not enforced on exiting processes - it is
13354698Sakolb * usually desirable to exit as soon as possible to free
13364698Sakolb * resources.
13373792Sakolb */
13383792Sakolb thread_lock(t);
13393792Sakolb tspp = (tsproc_t *)t->t_cldata;
13403792Sakolb (void) cpucaps_charge(t, &tspp->ts_caps, CPUCAPS_CHARGE_ONLY);
13413792Sakolb thread_unlock(t);
13423792Sakolb }
13433792Sakolb }
13443792Sakolb
13450Sstevel@tonic-gate /*
13460Sstevel@tonic-gate * Return the global scheduling priority that would be assigned
13470Sstevel@tonic-gate * to a thread entering the time-sharing class with the ts_upri.
13480Sstevel@tonic-gate */
13490Sstevel@tonic-gate static pri_t
ts_globpri(kthread_t * t)13500Sstevel@tonic-gate ts_globpri(kthread_t *t)
13510Sstevel@tonic-gate {
13520Sstevel@tonic-gate tsproc_t *tspp;
13530Sstevel@tonic-gate pri_t tspri;
13540Sstevel@tonic-gate
13550Sstevel@tonic-gate ASSERT(MUTEX_HELD(&ttoproc(t)->p_lock));
13560Sstevel@tonic-gate tspp = (tsproc_t *)t->t_cldata;
13570Sstevel@tonic-gate tspri = tsmedumdpri + tspp->ts_upri;
13580Sstevel@tonic-gate if (tspri > ts_maxumdpri)
13590Sstevel@tonic-gate tspri = ts_maxumdpri;
13600Sstevel@tonic-gate else if (tspri < 0)
13610Sstevel@tonic-gate tspri = 0;
13620Sstevel@tonic-gate return (ts_dptbl[tspri].ts_globpri);
13630Sstevel@tonic-gate }
13640Sstevel@tonic-gate
13650Sstevel@tonic-gate /*
13660Sstevel@tonic-gate * Arrange for thread to be placed in appropriate location
13670Sstevel@tonic-gate * on dispatcher queue.
13680Sstevel@tonic-gate *
13690Sstevel@tonic-gate * This is called with the current thread in TS_ONPROC and locked.
13700Sstevel@tonic-gate */
13710Sstevel@tonic-gate static void
ts_preempt(kthread_t * t)13720Sstevel@tonic-gate ts_preempt(kthread_t *t)
13730Sstevel@tonic-gate {
13740Sstevel@tonic-gate tsproc_t *tspp = (tsproc_t *)(t->t_cldata);
13753792Sakolb klwp_t *lwp = curthread->t_lwp;
13760Sstevel@tonic-gate pri_t oldpri = t->t_pri;
13770Sstevel@tonic-gate
13780Sstevel@tonic-gate ASSERT(t == curthread);
13790Sstevel@tonic-gate ASSERT(THREAD_LOCK_HELD(curthread));
13800Sstevel@tonic-gate
13810Sstevel@tonic-gate /*
13820Sstevel@tonic-gate * If preempted in the kernel, make sure the thread has
13830Sstevel@tonic-gate * a kernel priority if needed.
13840Sstevel@tonic-gate */
13850Sstevel@tonic-gate if (!(tspp->ts_flags & TSKPRI) && lwp != NULL && t->t_kpri_req) {
13860Sstevel@tonic-gate tspp->ts_flags |= TSKPRI;
13870Sstevel@tonic-gate THREAD_CHANGE_PRI(t, ts_kmdpris[0]);
13880Sstevel@tonic-gate ASSERT(t->t_pri >= 0 && t->t_pri <= ts_maxglobpri);
13890Sstevel@tonic-gate t->t_trapret = 1; /* so ts_trapret will run */
13900Sstevel@tonic-gate aston(t);
13910Sstevel@tonic-gate }
13923792Sakolb
13930Sstevel@tonic-gate /*
13943792Sakolb * This thread may be placed on wait queue by CPU Caps. In this case we
13953792Sakolb * do not need to do anything until it is removed from the wait queue.
13963792Sakolb * Do not enforce CPU caps on threads running at a kernel priority
13973792Sakolb */
13983792Sakolb if (CPUCAPS_ON()) {
13994698Sakolb (void) cpucaps_charge(t, &tspp->ts_caps,
14004698Sakolb CPUCAPS_CHARGE_ENFORCE);
14013792Sakolb if (!(tspp->ts_flags & TSKPRI) && CPUCAPS_ENFORCE(t))
14023792Sakolb return;
14033792Sakolb }
14043792Sakolb
14053792Sakolb /*
14063792Sakolb * If thread got preempted in the user-land then we know
14073792Sakolb * it isn't holding any locks. Mark it as swappable.
14080Sstevel@tonic-gate */
14090Sstevel@tonic-gate ASSERT(t->t_schedflag & TS_DONT_SWAP);
14100Sstevel@tonic-gate if (lwp != NULL && lwp->lwp_state == LWP_USER)
14110Sstevel@tonic-gate t->t_schedflag &= ~TS_DONT_SWAP;
14120Sstevel@tonic-gate
14130Sstevel@tonic-gate /*
14140Sstevel@tonic-gate * Check to see if we're doing "preemption control" here. If
14150Sstevel@tonic-gate * we are, and if the user has requested that this thread not
14160Sstevel@tonic-gate * be preempted, and if preemptions haven't been put off for
14170Sstevel@tonic-gate * too long, let the preemption happen here but try to make
14180Sstevel@tonic-gate * sure the thread is rescheduled as soon as possible. We do
14190Sstevel@tonic-gate * this by putting it on the front of the highest priority run
14200Sstevel@tonic-gate * queue in the TS class. If the preemption has been put off
14210Sstevel@tonic-gate * for too long, clear the "nopreempt" bit and let the thread
14220Sstevel@tonic-gate * be preempted.
14230Sstevel@tonic-gate */
14240Sstevel@tonic-gate if (t->t_schedctl && schedctl_get_nopreempt(t)) {
14250Sstevel@tonic-gate if (tspp->ts_timeleft > -SC_MAX_TICKS) {
14260Sstevel@tonic-gate DTRACE_SCHED1(schedctl__nopreempt, kthread_t *, t);
14270Sstevel@tonic-gate if (!(tspp->ts_flags & TSKPRI)) {
1428951Sraf /*
1429951Sraf * If not already remembered, remember current
1430951Sraf * priority for restoration in ts_yield().
1431951Sraf */
1432951Sraf if (!(tspp->ts_flags & TSRESTORE)) {
1433951Sraf tspp->ts_scpri = t->t_pri;
1434951Sraf tspp->ts_flags |= TSRESTORE;
1435951Sraf }
14360Sstevel@tonic-gate THREAD_CHANGE_PRI(t, ts_maxumdpri);
14370Sstevel@tonic-gate t->t_schedflag |= TS_DONT_SWAP;
14380Sstevel@tonic-gate }
1439951Sraf schedctl_set_yield(t, 1);
14400Sstevel@tonic-gate setfrontdq(t);
14410Sstevel@tonic-gate goto done;
14420Sstevel@tonic-gate } else {
1443951Sraf if (tspp->ts_flags & TSRESTORE) {
1444951Sraf THREAD_CHANGE_PRI(t, tspp->ts_scpri);
1445951Sraf tspp->ts_flags &= ~TSRESTORE;
1446951Sraf }
14470Sstevel@tonic-gate schedctl_set_nopreempt(t, 0);
14480Sstevel@tonic-gate DTRACE_SCHED1(schedctl__preempt, kthread_t *, t);
14490Sstevel@tonic-gate TNF_PROBE_2(schedctl_preempt, "schedctl TS ts_preempt",
14500Sstevel@tonic-gate /* CSTYLED */, tnf_pid, pid, ttoproc(t)->p_pid,
14510Sstevel@tonic-gate tnf_lwpid, lwpid, t->t_tid);
14520Sstevel@tonic-gate /*
14530Sstevel@tonic-gate * Fall through and be preempted below.
14540Sstevel@tonic-gate */
14550Sstevel@tonic-gate }
14560Sstevel@tonic-gate }
14570Sstevel@tonic-gate
14580Sstevel@tonic-gate if ((tspp->ts_flags & (TSBACKQ|TSKPRI)) == TSBACKQ) {
14590Sstevel@tonic-gate tspp->ts_timeleft = ts_dptbl[tspp->ts_cpupri].ts_quantum;
14600Sstevel@tonic-gate tspp->ts_dispwait = 0;
14610Sstevel@tonic-gate tspp->ts_flags &= ~TSBACKQ;
14620Sstevel@tonic-gate setbackdq(t);
14630Sstevel@tonic-gate } else if ((tspp->ts_flags & (TSBACKQ|TSKPRI)) == (TSBACKQ|TSKPRI)) {
14640Sstevel@tonic-gate tspp->ts_flags &= ~TSBACKQ;
14650Sstevel@tonic-gate setbackdq(t);
14660Sstevel@tonic-gate } else {
14673792Sakolb setfrontdq(t);
14680Sstevel@tonic-gate }
14690Sstevel@tonic-gate
14700Sstevel@tonic-gate done:
14710Sstevel@tonic-gate TRACE_2(TR_FAC_DISP, TR_PREEMPT,
14720Sstevel@tonic-gate "preempt:tid %p old pri %d", t, oldpri);
14730Sstevel@tonic-gate }
14740Sstevel@tonic-gate
14750Sstevel@tonic-gate static void
ts_setrun(kthread_t * t)14760Sstevel@tonic-gate ts_setrun(kthread_t *t)
14770Sstevel@tonic-gate {
14780Sstevel@tonic-gate tsproc_t *tspp = (tsproc_t *)(t->t_cldata);
14790Sstevel@tonic-gate
14800Sstevel@tonic-gate ASSERT(THREAD_LOCK_HELD(t)); /* t should be in transition */
14810Sstevel@tonic-gate
14820Sstevel@tonic-gate if (tspp->ts_dispwait > ts_dptbl[tspp->ts_umdpri].ts_maxwait) {
14830Sstevel@tonic-gate tspp->ts_cpupri = ts_dptbl[tspp->ts_cpupri].ts_slpret;
14840Sstevel@tonic-gate TS_NEWUMDPRI(tspp);
14850Sstevel@tonic-gate tspp->ts_timeleft = ts_dptbl[tspp->ts_cpupri].ts_quantum;
14860Sstevel@tonic-gate tspp->ts_dispwait = 0;
14870Sstevel@tonic-gate if ((tspp->ts_flags & TSKPRI) == 0) {
14880Sstevel@tonic-gate THREAD_CHANGE_PRI(t,
14890Sstevel@tonic-gate ts_dptbl[tspp->ts_umdpri].ts_globpri);
14900Sstevel@tonic-gate ASSERT(t->t_pri >= 0 && t->t_pri <= ts_maxglobpri);
14910Sstevel@tonic-gate }
14920Sstevel@tonic-gate }
14930Sstevel@tonic-gate
14940Sstevel@tonic-gate tspp->ts_flags &= ~TSBACKQ;
14950Sstevel@tonic-gate
14960Sstevel@tonic-gate if (tspp->ts_flags & TSIA) {
14970Sstevel@tonic-gate if (tspp->ts_flags & TSIASET)
14980Sstevel@tonic-gate setfrontdq(t);
14990Sstevel@tonic-gate else
15000Sstevel@tonic-gate setbackdq(t);
15010Sstevel@tonic-gate } else {
150211066Srafael.vanoni@sun.com if (t->t_disp_time != ddi_get_lbolt())
15030Sstevel@tonic-gate setbackdq(t);
15040Sstevel@tonic-gate else
15050Sstevel@tonic-gate setfrontdq(t);
15060Sstevel@tonic-gate }
15070Sstevel@tonic-gate }
15080Sstevel@tonic-gate
15090Sstevel@tonic-gate
15100Sstevel@tonic-gate /*
15110Sstevel@tonic-gate * Prepare thread for sleep. We reset the thread priority so it will
15120Sstevel@tonic-gate * run at the kernel priority level when it wakes up.
15130Sstevel@tonic-gate */
15140Sstevel@tonic-gate static void
ts_sleep(kthread_t * t)15150Sstevel@tonic-gate ts_sleep(kthread_t *t)
15160Sstevel@tonic-gate {
15170Sstevel@tonic-gate tsproc_t *tspp = (tsproc_t *)(t->t_cldata);
15180Sstevel@tonic-gate int flags;
15190Sstevel@tonic-gate pri_t old_pri = t->t_pri;
15200Sstevel@tonic-gate
15210Sstevel@tonic-gate ASSERT(t == curthread);
15220Sstevel@tonic-gate ASSERT(THREAD_LOCK_HELD(t));
15230Sstevel@tonic-gate
15243792Sakolb /*
15253792Sakolb * Account for time spent on CPU before going to sleep.
15263792Sakolb */
15274698Sakolb (void) CPUCAPS_CHARGE(t, &tspp->ts_caps, CPUCAPS_CHARGE_ENFORCE);
15283792Sakolb
15290Sstevel@tonic-gate flags = tspp->ts_flags;
15300Sstevel@tonic-gate if (t->t_kpri_req) {
15310Sstevel@tonic-gate tspp->ts_flags = flags | TSKPRI;
15320Sstevel@tonic-gate THREAD_CHANGE_PRI(t, ts_kmdpris[0]);
15330Sstevel@tonic-gate ASSERT(t->t_pri >= 0 && t->t_pri <= ts_maxglobpri);
15340Sstevel@tonic-gate t->t_trapret = 1; /* so ts_trapret will run */
15350Sstevel@tonic-gate aston(t);
15360Sstevel@tonic-gate } else if (tspp->ts_dispwait > ts_dptbl[tspp->ts_umdpri].ts_maxwait) {
15370Sstevel@tonic-gate /*
15380Sstevel@tonic-gate * If thread has blocked in the kernel (as opposed to
15390Sstevel@tonic-gate * being merely preempted), recompute the user mode priority.
15400Sstevel@tonic-gate */
15410Sstevel@tonic-gate tspp->ts_cpupri = ts_dptbl[tspp->ts_cpupri].ts_slpret;
15420Sstevel@tonic-gate TS_NEWUMDPRI(tspp);
15430Sstevel@tonic-gate tspp->ts_timeleft = ts_dptbl[tspp->ts_cpupri].ts_quantum;
15440Sstevel@tonic-gate tspp->ts_dispwait = 0;
15450Sstevel@tonic-gate
15460Sstevel@tonic-gate THREAD_CHANGE_PRI(curthread,
15470Sstevel@tonic-gate ts_dptbl[tspp->ts_umdpri].ts_globpri);
15480Sstevel@tonic-gate ASSERT(curthread->t_pri >= 0 &&
15490Sstevel@tonic-gate curthread->t_pri <= ts_maxglobpri);
15500Sstevel@tonic-gate tspp->ts_flags = flags & ~TSKPRI;
15510Sstevel@tonic-gate
15520Sstevel@tonic-gate if (DISP_MUST_SURRENDER(curthread))
15530Sstevel@tonic-gate cpu_surrender(curthread);
15540Sstevel@tonic-gate } else if (flags & TSKPRI) {
15550Sstevel@tonic-gate THREAD_CHANGE_PRI(curthread,
15560Sstevel@tonic-gate ts_dptbl[tspp->ts_umdpri].ts_globpri);
15570Sstevel@tonic-gate ASSERT(curthread->t_pri >= 0 &&
15580Sstevel@tonic-gate curthread->t_pri <= ts_maxglobpri);
15590Sstevel@tonic-gate tspp->ts_flags = flags & ~TSKPRI;
15600Sstevel@tonic-gate
15610Sstevel@tonic-gate if (DISP_MUST_SURRENDER(curthread))
15620Sstevel@tonic-gate cpu_surrender(curthread);
15630Sstevel@tonic-gate }
156411066Srafael.vanoni@sun.com t->t_stime = ddi_get_lbolt(); /* time stamp for the swapper */
15650Sstevel@tonic-gate TRACE_2(TR_FAC_DISP, TR_SLEEP,
15660Sstevel@tonic-gate "sleep:tid %p old pri %d", t, old_pri);
15670Sstevel@tonic-gate }
15680Sstevel@tonic-gate
15690Sstevel@tonic-gate
15700Sstevel@tonic-gate /*
15710Sstevel@tonic-gate * Return Values:
15720Sstevel@tonic-gate *
15730Sstevel@tonic-gate * -1 if the thread is loaded or is not eligible to be swapped in.
15740Sstevel@tonic-gate *
15750Sstevel@tonic-gate * effective priority of the specified thread based on swapout time
15760Sstevel@tonic-gate * and size of process (epri >= 0 , epri <= SHRT_MAX).
15770Sstevel@tonic-gate */
15780Sstevel@tonic-gate /* ARGSUSED */
15790Sstevel@tonic-gate static pri_t
ts_swapin(kthread_t * t,int flags)15800Sstevel@tonic-gate ts_swapin(kthread_t *t, int flags)
15810Sstevel@tonic-gate {
15820Sstevel@tonic-gate tsproc_t *tspp = (tsproc_t *)(t->t_cldata);
15830Sstevel@tonic-gate long epri = -1;
15840Sstevel@tonic-gate proc_t *pp = ttoproc(t);
15850Sstevel@tonic-gate
15860Sstevel@tonic-gate ASSERT(THREAD_LOCK_HELD(t));
15870Sstevel@tonic-gate
15880Sstevel@tonic-gate /*
15890Sstevel@tonic-gate * We know that pri_t is a short.
15900Sstevel@tonic-gate * Be sure not to overrun its range.
15910Sstevel@tonic-gate */
15920Sstevel@tonic-gate if (t->t_state == TS_RUN && (t->t_schedflag & TS_LOAD) == 0) {
15930Sstevel@tonic-gate time_t swapout_time;
15940Sstevel@tonic-gate
159511066Srafael.vanoni@sun.com swapout_time = (ddi_get_lbolt() - t->t_stime) / hz;
15960Sstevel@tonic-gate if (INHERITED(t) || (tspp->ts_flags & (TSKPRI | TSIASET)))
15970Sstevel@tonic-gate epri = (long)DISP_PRIO(t) + swapout_time;
15980Sstevel@tonic-gate else {
15990Sstevel@tonic-gate /*
16000Sstevel@tonic-gate * Threads which have been out for a long time,
16010Sstevel@tonic-gate * have high user mode priority and are associated
16020Sstevel@tonic-gate * with a small address space are more deserving
16030Sstevel@tonic-gate */
16040Sstevel@tonic-gate epri = ts_dptbl[tspp->ts_umdpri].ts_globpri;
16050Sstevel@tonic-gate ASSERT(epri >= 0 && epri <= ts_maxumdpri);
16060Sstevel@tonic-gate epri += swapout_time - pp->p_swrss / nz(maxpgio)/2;
16070Sstevel@tonic-gate }
16080Sstevel@tonic-gate /*
16090Sstevel@tonic-gate * Scale epri so SHRT_MAX/2 represents zero priority.
16100Sstevel@tonic-gate */
16110Sstevel@tonic-gate epri += SHRT_MAX/2;
16120Sstevel@tonic-gate if (epri < 0)
16130Sstevel@tonic-gate epri = 0;
16140Sstevel@tonic-gate else if (epri > SHRT_MAX)
16150Sstevel@tonic-gate epri = SHRT_MAX;
16160Sstevel@tonic-gate }
16170Sstevel@tonic-gate return ((pri_t)epri);
16180Sstevel@tonic-gate }
16190Sstevel@tonic-gate
16200Sstevel@tonic-gate /*
16210Sstevel@tonic-gate * Return Values
16220Sstevel@tonic-gate * -1 if the thread isn't loaded or is not eligible to be swapped out.
16230Sstevel@tonic-gate *
16240Sstevel@tonic-gate * effective priority of the specified thread based on if the swapper
16250Sstevel@tonic-gate * is in softswap or hardswap mode.
16260Sstevel@tonic-gate *
16270Sstevel@tonic-gate * Softswap: Return a low effective priority for threads
16280Sstevel@tonic-gate * sleeping for more than maxslp secs.
16290Sstevel@tonic-gate *
16300Sstevel@tonic-gate * Hardswap: Return an effective priority such that threads
16310Sstevel@tonic-gate * which have been in memory for a while and are
16320Sstevel@tonic-gate * associated with a small address space are swapped
16330Sstevel@tonic-gate * in before others.
16340Sstevel@tonic-gate *
16350Sstevel@tonic-gate * (epri >= 0 , epri <= SHRT_MAX).
16360Sstevel@tonic-gate */
16370Sstevel@tonic-gate time_t ts_minrun = 2; /* XXX - t_pri becomes 59 within 2 secs */
16380Sstevel@tonic-gate time_t ts_minslp = 2; /* min time on sleep queue for hardswap */
16390Sstevel@tonic-gate
16400Sstevel@tonic-gate static pri_t
ts_swapout(kthread_t * t,int flags)16410Sstevel@tonic-gate ts_swapout(kthread_t *t, int flags)
16420Sstevel@tonic-gate {
16430Sstevel@tonic-gate tsproc_t *tspp = (tsproc_t *)(t->t_cldata);
16440Sstevel@tonic-gate long epri = -1;
16450Sstevel@tonic-gate proc_t *pp = ttoproc(t);
16460Sstevel@tonic-gate time_t swapin_time;
16470Sstevel@tonic-gate
16480Sstevel@tonic-gate ASSERT(THREAD_LOCK_HELD(t));
16490Sstevel@tonic-gate
16500Sstevel@tonic-gate if (INHERITED(t) || (tspp->ts_flags & (TSKPRI | TSIASET)) ||
16510Sstevel@tonic-gate (t->t_proc_flag & TP_LWPEXIT) ||
16523792Sakolb (t->t_state & (TS_ZOMB | TS_FREE | TS_STOPPED |
16533792Sakolb TS_ONPROC | TS_WAIT)) ||
16540Sstevel@tonic-gate !(t->t_schedflag & TS_LOAD) || !SWAP_OK(t))
16550Sstevel@tonic-gate return (-1);
16560Sstevel@tonic-gate
16570Sstevel@tonic-gate ASSERT(t->t_state & (TS_SLEEP | TS_RUN));
16580Sstevel@tonic-gate
16590Sstevel@tonic-gate /*
16600Sstevel@tonic-gate * We know that pri_t is a short.
16610Sstevel@tonic-gate * Be sure not to overrun its range.
16620Sstevel@tonic-gate */
166311066Srafael.vanoni@sun.com swapin_time = (ddi_get_lbolt() - t->t_stime) / hz;
16640Sstevel@tonic-gate if (flags == SOFTSWAP) {
16650Sstevel@tonic-gate if (t->t_state == TS_SLEEP && swapin_time > maxslp) {
16660Sstevel@tonic-gate epri = 0;
16670Sstevel@tonic-gate } else {
16680Sstevel@tonic-gate return ((pri_t)epri);
16690Sstevel@tonic-gate }
16700Sstevel@tonic-gate } else {
16710Sstevel@tonic-gate pri_t pri;
16720Sstevel@tonic-gate
16730Sstevel@tonic-gate if ((t->t_state == TS_SLEEP && swapin_time > ts_minslp) ||
16740Sstevel@tonic-gate (t->t_state == TS_RUN && swapin_time > ts_minrun)) {
16750Sstevel@tonic-gate pri = ts_dptbl[tspp->ts_umdpri].ts_globpri;
16760Sstevel@tonic-gate ASSERT(pri >= 0 && pri <= ts_maxumdpri);
16770Sstevel@tonic-gate epri = swapin_time -
16780Sstevel@tonic-gate (rm_asrss(pp->p_as) / nz(maxpgio)/2) - (long)pri;
16790Sstevel@tonic-gate } else {
16800Sstevel@tonic-gate return ((pri_t)epri);
16810Sstevel@tonic-gate }
16820Sstevel@tonic-gate }
16830Sstevel@tonic-gate
16840Sstevel@tonic-gate /*
16850Sstevel@tonic-gate * Scale epri so SHRT_MAX/2 represents zero priority.
16860Sstevel@tonic-gate */
16870Sstevel@tonic-gate epri += SHRT_MAX/2;
16880Sstevel@tonic-gate if (epri < 0)
16890Sstevel@tonic-gate epri = 0;
16900Sstevel@tonic-gate else if (epri > SHRT_MAX)
16910Sstevel@tonic-gate epri = SHRT_MAX;
16920Sstevel@tonic-gate
16930Sstevel@tonic-gate return ((pri_t)epri);
16940Sstevel@tonic-gate }
16950Sstevel@tonic-gate
16960Sstevel@tonic-gate /*
16970Sstevel@tonic-gate * Check for time slice expiration. If time slice has expired
16980Sstevel@tonic-gate * move thread to priority specified in tsdptbl for time slice expiration
16990Sstevel@tonic-gate * and set runrun to cause preemption.
17000Sstevel@tonic-gate */
17010Sstevel@tonic-gate static void
ts_tick(kthread_t * t)17020Sstevel@tonic-gate ts_tick(kthread_t *t)
17030Sstevel@tonic-gate {
17040Sstevel@tonic-gate tsproc_t *tspp = (tsproc_t *)(t->t_cldata);
17050Sstevel@tonic-gate klwp_t *lwp;
17063792Sakolb boolean_t call_cpu_surrender = B_FALSE;
17070Sstevel@tonic-gate pri_t oldpri = t->t_pri;
17080Sstevel@tonic-gate
17090Sstevel@tonic-gate ASSERT(MUTEX_HELD(&(ttoproc(t))->p_lock));
17100Sstevel@tonic-gate
17110Sstevel@tonic-gate thread_lock(t);
17123792Sakolb
17133792Sakolb /*
17143792Sakolb * Keep track of thread's project CPU usage. Note that projects
17153792Sakolb * get charged even when threads are running in the kernel.
17163792Sakolb */
17173792Sakolb if (CPUCAPS_ON()) {
17183792Sakolb call_cpu_surrender = cpucaps_charge(t, &tspp->ts_caps,
17193792Sakolb CPUCAPS_CHARGE_ENFORCE) && !(tspp->ts_flags & TSKPRI);
17203792Sakolb }
17213792Sakolb
17220Sstevel@tonic-gate if ((tspp->ts_flags & TSKPRI) == 0) {
17230Sstevel@tonic-gate if (--tspp->ts_timeleft <= 0) {
17240Sstevel@tonic-gate pri_t new_pri;
17250Sstevel@tonic-gate
17260Sstevel@tonic-gate /*
17270Sstevel@tonic-gate * If we're doing preemption control and trying to
17280Sstevel@tonic-gate * avoid preempting this thread, just note that
17290Sstevel@tonic-gate * the thread should yield soon and let it keep
17300Sstevel@tonic-gate * running (unless it's been a while).
17310Sstevel@tonic-gate */
17320Sstevel@tonic-gate if (t->t_schedctl && schedctl_get_nopreempt(t)) {
17330Sstevel@tonic-gate if (tspp->ts_timeleft > -SC_MAX_TICKS) {
17340Sstevel@tonic-gate DTRACE_SCHED1(schedctl__nopreempt,
17350Sstevel@tonic-gate kthread_t *, t);
17360Sstevel@tonic-gate schedctl_set_yield(t, 1);
17370Sstevel@tonic-gate thread_unlock_nopreempt(t);
17380Sstevel@tonic-gate return;
17390Sstevel@tonic-gate }
17400Sstevel@tonic-gate
17410Sstevel@tonic-gate TNF_PROBE_2(schedctl_failsafe,
17420Sstevel@tonic-gate "schedctl TS ts_tick", /* CSTYLED */,
17430Sstevel@tonic-gate tnf_pid, pid, ttoproc(t)->p_pid,
17440Sstevel@tonic-gate tnf_lwpid, lwpid, t->t_tid);
17450Sstevel@tonic-gate }
1746951Sraf tspp->ts_flags &= ~TSRESTORE;
17470Sstevel@tonic-gate tspp->ts_cpupri = ts_dptbl[tspp->ts_cpupri].ts_tqexp;
17480Sstevel@tonic-gate TS_NEWUMDPRI(tspp);
17490Sstevel@tonic-gate tspp->ts_dispwait = 0;
17500Sstevel@tonic-gate new_pri = ts_dptbl[tspp->ts_umdpri].ts_globpri;
17510Sstevel@tonic-gate ASSERT(new_pri >= 0 && new_pri <= ts_maxglobpri);
17520Sstevel@tonic-gate /*
17530Sstevel@tonic-gate * When the priority of a thread is changed,
17540Sstevel@tonic-gate * it may be necessary to adjust its position
17550Sstevel@tonic-gate * on a sleep queue or dispatch queue.
17560Sstevel@tonic-gate * The function thread_change_pri accomplishes
17570Sstevel@tonic-gate * this.
17580Sstevel@tonic-gate */
17590Sstevel@tonic-gate if (thread_change_pri(t, new_pri, 0)) {
17600Sstevel@tonic-gate if ((t->t_schedflag & TS_LOAD) &&
17610Sstevel@tonic-gate (lwp = t->t_lwp) &&
17620Sstevel@tonic-gate lwp->lwp_state == LWP_USER)
17630Sstevel@tonic-gate t->t_schedflag &= ~TS_DONT_SWAP;
17640Sstevel@tonic-gate tspp->ts_timeleft =
17650Sstevel@tonic-gate ts_dptbl[tspp->ts_cpupri].ts_quantum;
17660Sstevel@tonic-gate } else {
17673792Sakolb call_cpu_surrender = B_TRUE;
17680Sstevel@tonic-gate }
17690Sstevel@tonic-gate TRACE_2(TR_FAC_DISP, TR_TICK,
17700Sstevel@tonic-gate "tick:tid %p old pri %d", t, oldpri);
1771477Smishra } else if (t->t_state == TS_ONPROC &&
17726247Sraf t->t_pri < t->t_disp_queue->disp_maxrunpri) {
17733792Sakolb call_cpu_surrender = B_TRUE;
17740Sstevel@tonic-gate }
17750Sstevel@tonic-gate }
17763792Sakolb
17773792Sakolb if (call_cpu_surrender) {
17783792Sakolb tspp->ts_flags |= TSBACKQ;
17793792Sakolb cpu_surrender(t);
17803792Sakolb }
17813792Sakolb
17820Sstevel@tonic-gate thread_unlock_nopreempt(t); /* clock thread can't be preempted */
17830Sstevel@tonic-gate }
17840Sstevel@tonic-gate
17850Sstevel@tonic-gate
17860Sstevel@tonic-gate /*
17870Sstevel@tonic-gate * If thread is currently at a kernel mode priority (has slept)
17880Sstevel@tonic-gate * we assign it the appropriate user mode priority and time quantum
17890Sstevel@tonic-gate * here. If we are lowering the thread's priority below that of
17900Sstevel@tonic-gate * other runnable threads we will normally set runrun via cpu_surrender() to
17910Sstevel@tonic-gate * cause preemption.
17920Sstevel@tonic-gate */
17930Sstevel@tonic-gate static void
ts_trapret(kthread_t * t)17940Sstevel@tonic-gate ts_trapret(kthread_t *t)
17950Sstevel@tonic-gate {
17960Sstevel@tonic-gate tsproc_t *tspp = (tsproc_t *)t->t_cldata;
17970Sstevel@tonic-gate cpu_t *cp = CPU;
17980Sstevel@tonic-gate pri_t old_pri = curthread->t_pri;
17990Sstevel@tonic-gate
18000Sstevel@tonic-gate ASSERT(THREAD_LOCK_HELD(t));
18010Sstevel@tonic-gate ASSERT(t == curthread);
18020Sstevel@tonic-gate ASSERT(cp->cpu_dispthread == t);
18030Sstevel@tonic-gate ASSERT(t->t_state == TS_ONPROC);
18040Sstevel@tonic-gate
18050Sstevel@tonic-gate t->t_kpri_req = 0;
18060Sstevel@tonic-gate if (tspp->ts_dispwait > ts_dptbl[tspp->ts_umdpri].ts_maxwait) {
18070Sstevel@tonic-gate tspp->ts_cpupri = ts_dptbl[tspp->ts_cpupri].ts_slpret;
18080Sstevel@tonic-gate TS_NEWUMDPRI(tspp);
18090Sstevel@tonic-gate tspp->ts_timeleft = ts_dptbl[tspp->ts_cpupri].ts_quantum;
18100Sstevel@tonic-gate tspp->ts_dispwait = 0;
18110Sstevel@tonic-gate
18120Sstevel@tonic-gate /*
18130Sstevel@tonic-gate * If thread has blocked in the kernel (as opposed to
18140Sstevel@tonic-gate * being merely preempted), recompute the user mode priority.
18150Sstevel@tonic-gate */
18160Sstevel@tonic-gate THREAD_CHANGE_PRI(t, ts_dptbl[tspp->ts_umdpri].ts_globpri);
18170Sstevel@tonic-gate cp->cpu_dispatch_pri = DISP_PRIO(t);
18180Sstevel@tonic-gate ASSERT(t->t_pri >= 0 && t->t_pri <= ts_maxglobpri);
18190Sstevel@tonic-gate tspp->ts_flags &= ~TSKPRI;
18200Sstevel@tonic-gate
18210Sstevel@tonic-gate if (DISP_MUST_SURRENDER(t))
18220Sstevel@tonic-gate cpu_surrender(t);
18230Sstevel@tonic-gate } else if (tspp->ts_flags & TSKPRI) {
18240Sstevel@tonic-gate /*
18250Sstevel@tonic-gate * If thread has blocked in the kernel (as opposed to
18260Sstevel@tonic-gate * being merely preempted), recompute the user mode priority.
18270Sstevel@tonic-gate */
18280Sstevel@tonic-gate THREAD_CHANGE_PRI(t, ts_dptbl[tspp->ts_umdpri].ts_globpri);
18290Sstevel@tonic-gate cp->cpu_dispatch_pri = DISP_PRIO(t);
18300Sstevel@tonic-gate ASSERT(t->t_pri >= 0 && t->t_pri <= ts_maxglobpri);
18310Sstevel@tonic-gate tspp->ts_flags &= ~TSKPRI;
18320Sstevel@tonic-gate
18330Sstevel@tonic-gate if (DISP_MUST_SURRENDER(t))
18340Sstevel@tonic-gate cpu_surrender(t);
18350Sstevel@tonic-gate }
18360Sstevel@tonic-gate
18370Sstevel@tonic-gate /*
18380Sstevel@tonic-gate * Swapout lwp if the swapper is waiting for this thread to
18390Sstevel@tonic-gate * reach a safe point.
18400Sstevel@tonic-gate */
18410Sstevel@tonic-gate if ((t->t_schedflag & TS_SWAPENQ) && !(tspp->ts_flags & TSIASET)) {
18420Sstevel@tonic-gate thread_unlock(t);
18430Sstevel@tonic-gate swapout_lwp(ttolwp(t));
18440Sstevel@tonic-gate thread_lock(t);
18450Sstevel@tonic-gate }
18460Sstevel@tonic-gate
18470Sstevel@tonic-gate TRACE_2(TR_FAC_DISP, TR_TRAPRET,
18480Sstevel@tonic-gate "trapret:tid %p old pri %d", t, old_pri);
18490Sstevel@tonic-gate }
18500Sstevel@tonic-gate
18510Sstevel@tonic-gate
18520Sstevel@tonic-gate /*
18530Sstevel@tonic-gate * Update the ts_dispwait values of all time sharing threads that
18540Sstevel@tonic-gate * are currently runnable at a user mode priority and bump the priority
18550Sstevel@tonic-gate * if ts_dispwait exceeds ts_maxwait. Called once per second via
18560Sstevel@tonic-gate * timeout which we reset here.
18570Sstevel@tonic-gate *
18580Sstevel@tonic-gate * There are several lists of time sharing threads broken up by a hash on
18590Sstevel@tonic-gate * the thread pointer. Each list has its own lock. This avoids blocking
18600Sstevel@tonic-gate * all ts_enterclass, ts_fork, and ts_exitclass operations while ts_update
18610Sstevel@tonic-gate * runs. ts_update traverses each list in turn.
18620Sstevel@tonic-gate *
18630Sstevel@tonic-gate * If multiple threads have their priorities updated to the same value,
18640Sstevel@tonic-gate * the system implicitly favors the one that is updated first (since it
18650Sstevel@tonic-gate * winds up first on the run queue). To avoid this unfairness, the
18660Sstevel@tonic-gate * traversal of threads starts at the list indicated by a marker. When
18670Sstevel@tonic-gate * threads in more than one list have their priorities updated, the marker
18680Sstevel@tonic-gate * is moved. This changes the order the threads will be placed on the run
18690Sstevel@tonic-gate * queue the next time ts_update is called and preserves fairness over the
18700Sstevel@tonic-gate * long run. The marker doesn't need to be protected by a lock since it's
18710Sstevel@tonic-gate * only accessed by ts_update, which is inherently single-threaded (only
18720Sstevel@tonic-gate * one instance can be running at a time).
18730Sstevel@tonic-gate */
18740Sstevel@tonic-gate static void
ts_update(void * arg)18750Sstevel@tonic-gate ts_update(void *arg)
18760Sstevel@tonic-gate {
18770Sstevel@tonic-gate int i;
18780Sstevel@tonic-gate int new_marker = -1;
18790Sstevel@tonic-gate static int ts_update_marker;
18800Sstevel@tonic-gate
18810Sstevel@tonic-gate /*
18820Sstevel@tonic-gate * Start with the ts_update_marker list, then do the rest.
18830Sstevel@tonic-gate */
18840Sstevel@tonic-gate i = ts_update_marker;
18850Sstevel@tonic-gate do {
18860Sstevel@tonic-gate /*
18870Sstevel@tonic-gate * If this is the first list after the current marker to
18880Sstevel@tonic-gate * have threads with priorities updated, advance the marker
18890Sstevel@tonic-gate * to this list for the next time ts_update runs.
18900Sstevel@tonic-gate */
18910Sstevel@tonic-gate if (ts_update_list(i) && new_marker == -1 &&
18920Sstevel@tonic-gate i != ts_update_marker) {
18930Sstevel@tonic-gate new_marker = i;
18940Sstevel@tonic-gate }
18950Sstevel@tonic-gate } while ((i = TS_LIST_NEXT(i)) != ts_update_marker);
18960Sstevel@tonic-gate
18970Sstevel@tonic-gate /* advance marker for next ts_update call */
18980Sstevel@tonic-gate if (new_marker != -1)
18990Sstevel@tonic-gate ts_update_marker = new_marker;
19000Sstevel@tonic-gate
19010Sstevel@tonic-gate (void) timeout(ts_update, arg, hz);
19020Sstevel@tonic-gate }
19030Sstevel@tonic-gate
19040Sstevel@tonic-gate /*
19050Sstevel@tonic-gate * Updates priority for a list of threads. Returns 1 if the priority of
19060Sstevel@tonic-gate * one of the threads was actually updated, 0 if none were for various
19070Sstevel@tonic-gate * reasons (thread is no longer in the TS or IA class, isn't runnable,
19080Sstevel@tonic-gate * hasn't waited long enough, has the preemption control no-preempt bit
19090Sstevel@tonic-gate * set, etc.)
19100Sstevel@tonic-gate */
19110Sstevel@tonic-gate static int
ts_update_list(int i)19120Sstevel@tonic-gate ts_update_list(int i)
19130Sstevel@tonic-gate {
19140Sstevel@tonic-gate tsproc_t *tspp;
19150Sstevel@tonic-gate kthread_t *tx;
19160Sstevel@tonic-gate int updated = 0;
19170Sstevel@tonic-gate
19180Sstevel@tonic-gate mutex_enter(&ts_list_lock[i]);
19190Sstevel@tonic-gate for (tspp = ts_plisthead[i].ts_next; tspp != &ts_plisthead[i];
19200Sstevel@tonic-gate tspp = tspp->ts_next) {
19210Sstevel@tonic-gate tx = tspp->ts_tp;
19220Sstevel@tonic-gate /*
19230Sstevel@tonic-gate * Lock the thread and verify state.
19240Sstevel@tonic-gate */
19250Sstevel@tonic-gate thread_lock(tx);
19260Sstevel@tonic-gate /*
19270Sstevel@tonic-gate * Skip the thread if it is no longer in the TS (or IA) class.
19280Sstevel@tonic-gate */
19290Sstevel@tonic-gate if (tx->t_clfuncs != &ts_classfuncs.thread &&
19300Sstevel@tonic-gate tx->t_clfuncs != &ia_classfuncs.thread)
19310Sstevel@tonic-gate goto next;
19320Sstevel@tonic-gate tspp->ts_dispwait++;
19330Sstevel@tonic-gate if ((tspp->ts_flags & TSKPRI) != 0)
19340Sstevel@tonic-gate goto next;
19350Sstevel@tonic-gate if (tspp->ts_dispwait <= ts_dptbl[tspp->ts_umdpri].ts_maxwait)
19360Sstevel@tonic-gate goto next;
19370Sstevel@tonic-gate if (tx->t_schedctl && schedctl_get_nopreempt(tx))
19380Sstevel@tonic-gate goto next;
19393792Sakolb if (tx->t_state != TS_RUN && tx->t_state != TS_WAIT &&
19403792Sakolb (tx->t_state != TS_SLEEP || !ts_sleep_promote)) {
19410Sstevel@tonic-gate /* make next syscall/trap do CL_TRAPRET */
19420Sstevel@tonic-gate tx->t_trapret = 1;
19430Sstevel@tonic-gate aston(tx);
19440Sstevel@tonic-gate goto next;
19450Sstevel@tonic-gate }
19460Sstevel@tonic-gate tspp->ts_cpupri = ts_dptbl[tspp->ts_cpupri].ts_lwait;
19470Sstevel@tonic-gate TS_NEWUMDPRI(tspp);
19480Sstevel@tonic-gate tspp->ts_dispwait = 0;
19490Sstevel@tonic-gate updated = 1;
19500Sstevel@tonic-gate
19510Sstevel@tonic-gate /*
19520Sstevel@tonic-gate * Only dequeue it if needs to move; otherwise it should
19530Sstevel@tonic-gate * just round-robin here.
19540Sstevel@tonic-gate */
19550Sstevel@tonic-gate if (tx->t_pri != ts_dptbl[tspp->ts_umdpri].ts_globpri) {
19560Sstevel@tonic-gate pri_t oldpri = tx->t_pri;
19570Sstevel@tonic-gate ts_change_priority(tx, tspp);
19580Sstevel@tonic-gate TRACE_2(TR_FAC_DISP, TR_UPDATE,
19590Sstevel@tonic-gate "update:tid %p old pri %d", tx, oldpri);
19600Sstevel@tonic-gate }
19610Sstevel@tonic-gate next:
19620Sstevel@tonic-gate thread_unlock(tx);
19630Sstevel@tonic-gate }
19640Sstevel@tonic-gate mutex_exit(&ts_list_lock[i]);
19650Sstevel@tonic-gate
19660Sstevel@tonic-gate return (updated);
19670Sstevel@tonic-gate }
19680Sstevel@tonic-gate
19690Sstevel@tonic-gate /*
19700Sstevel@tonic-gate * Processes waking up go to the back of their queue. We don't
19710Sstevel@tonic-gate * need to assign a time quantum here because thread is still
19720Sstevel@tonic-gate * at a kernel mode priority and the time slicing is not done
19730Sstevel@tonic-gate * for threads running in the kernel after sleeping. The proper
19740Sstevel@tonic-gate * time quantum will be assigned by ts_trapret before the thread
19750Sstevel@tonic-gate * returns to user mode.
19760Sstevel@tonic-gate */
19770Sstevel@tonic-gate static void
ts_wakeup(kthread_t * t)19780Sstevel@tonic-gate ts_wakeup(kthread_t *t)
19790Sstevel@tonic-gate {
19800Sstevel@tonic-gate tsproc_t *tspp = (tsproc_t *)(t->t_cldata);
19810Sstevel@tonic-gate
19820Sstevel@tonic-gate ASSERT(THREAD_LOCK_HELD(t));
19830Sstevel@tonic-gate
198411066Srafael.vanoni@sun.com t->t_stime = ddi_get_lbolt(); /* time stamp for the swapper */
19850Sstevel@tonic-gate
19860Sstevel@tonic-gate if (tspp->ts_flags & TSKPRI) {
19870Sstevel@tonic-gate tspp->ts_flags &= ~TSBACKQ;
19880Sstevel@tonic-gate if (tspp->ts_flags & TSIASET)
19890Sstevel@tonic-gate setfrontdq(t);
19900Sstevel@tonic-gate else
19910Sstevel@tonic-gate setbackdq(t);
19920Sstevel@tonic-gate } else if (t->t_kpri_req) {
19930Sstevel@tonic-gate /*
19940Sstevel@tonic-gate * Give thread a priority boost if we were asked.
19950Sstevel@tonic-gate */
19960Sstevel@tonic-gate tspp->ts_flags |= TSKPRI;
19970Sstevel@tonic-gate THREAD_CHANGE_PRI(t, ts_kmdpris[0]);
19980Sstevel@tonic-gate setbackdq(t);
19990Sstevel@tonic-gate t->t_trapret = 1; /* so that ts_trapret will run */
20000Sstevel@tonic-gate aston(t);
20010Sstevel@tonic-gate } else {
20020Sstevel@tonic-gate if (tspp->ts_dispwait > ts_dptbl[tspp->ts_umdpri].ts_maxwait) {
20030Sstevel@tonic-gate tspp->ts_cpupri = ts_dptbl[tspp->ts_cpupri].ts_slpret;
20040Sstevel@tonic-gate TS_NEWUMDPRI(tspp);
20050Sstevel@tonic-gate tspp->ts_timeleft =
20060Sstevel@tonic-gate ts_dptbl[tspp->ts_cpupri].ts_quantum;
20070Sstevel@tonic-gate tspp->ts_dispwait = 0;
20080Sstevel@tonic-gate THREAD_CHANGE_PRI(t,
20090Sstevel@tonic-gate ts_dptbl[tspp->ts_umdpri].ts_globpri);
20100Sstevel@tonic-gate ASSERT(t->t_pri >= 0 && t->t_pri <= ts_maxglobpri);
20110Sstevel@tonic-gate }
20120Sstevel@tonic-gate
20130Sstevel@tonic-gate tspp->ts_flags &= ~TSBACKQ;
20140Sstevel@tonic-gate
20150Sstevel@tonic-gate if (tspp->ts_flags & TSIA) {
20160Sstevel@tonic-gate if (tspp->ts_flags & TSIASET)
20170Sstevel@tonic-gate setfrontdq(t);
20180Sstevel@tonic-gate else
20190Sstevel@tonic-gate setbackdq(t);
20200Sstevel@tonic-gate } else {
202111066Srafael.vanoni@sun.com if (t->t_disp_time != ddi_get_lbolt())
20220Sstevel@tonic-gate setbackdq(t);
20230Sstevel@tonic-gate else
20240Sstevel@tonic-gate setfrontdq(t);
20250Sstevel@tonic-gate }
20260Sstevel@tonic-gate }
20270Sstevel@tonic-gate }
20280Sstevel@tonic-gate
20290Sstevel@tonic-gate
20300Sstevel@tonic-gate /*
20310Sstevel@tonic-gate * When a thread yields, put it on the back of the run queue.
20320Sstevel@tonic-gate */
20330Sstevel@tonic-gate static void
ts_yield(kthread_t * t)20340Sstevel@tonic-gate ts_yield(kthread_t *t)
20350Sstevel@tonic-gate {
20360Sstevel@tonic-gate tsproc_t *tspp = (tsproc_t *)(t->t_cldata);
20370Sstevel@tonic-gate
20380Sstevel@tonic-gate ASSERT(t == curthread);
20390Sstevel@tonic-gate ASSERT(THREAD_LOCK_HELD(t));
20400Sstevel@tonic-gate
20410Sstevel@tonic-gate /*
20423792Sakolb * Collect CPU usage spent before yielding
20433792Sakolb */
20444698Sakolb (void) CPUCAPS_CHARGE(t, &tspp->ts_caps, CPUCAPS_CHARGE_ENFORCE);
20453792Sakolb
20463792Sakolb /*
20470Sstevel@tonic-gate * Clear the preemption control "yield" bit since the user is
20480Sstevel@tonic-gate * doing a yield.
20490Sstevel@tonic-gate */
20500Sstevel@tonic-gate if (t->t_schedctl)
20510Sstevel@tonic-gate schedctl_set_yield(t, 0);
2052951Sraf /*
2053951Sraf * If ts_preempt() artifically increased the thread's priority
2054951Sraf * to avoid preemption, restore the original priority now.
2055951Sraf */
2056951Sraf if (tspp->ts_flags & TSRESTORE) {
2057951Sraf THREAD_CHANGE_PRI(t, tspp->ts_scpri);
2058951Sraf tspp->ts_flags &= ~TSRESTORE;
2059951Sraf }
20600Sstevel@tonic-gate if (tspp->ts_timeleft <= 0) {
20610Sstevel@tonic-gate /*
20620Sstevel@tonic-gate * Time slice was artificially extended to avoid
20630Sstevel@tonic-gate * preemption, so pretend we're preempting it now.
20640Sstevel@tonic-gate */
20650Sstevel@tonic-gate DTRACE_SCHED1(schedctl__yield, int, -tspp->ts_timeleft);
20660Sstevel@tonic-gate tspp->ts_cpupri = ts_dptbl[tspp->ts_cpupri].ts_tqexp;
20670Sstevel@tonic-gate TS_NEWUMDPRI(tspp);
20680Sstevel@tonic-gate tspp->ts_timeleft = ts_dptbl[tspp->ts_cpupri].ts_quantum;
20690Sstevel@tonic-gate tspp->ts_dispwait = 0;
20700Sstevel@tonic-gate THREAD_CHANGE_PRI(t, ts_dptbl[tspp->ts_umdpri].ts_globpri);
20710Sstevel@tonic-gate ASSERT(t->t_pri >= 0 && t->t_pri <= ts_maxglobpri);
20720Sstevel@tonic-gate }
20730Sstevel@tonic-gate tspp->ts_flags &= ~TSBACKQ;
20740Sstevel@tonic-gate setbackdq(t);
20750Sstevel@tonic-gate }
20760Sstevel@tonic-gate
20770Sstevel@tonic-gate
20780Sstevel@tonic-gate /*
20790Sstevel@tonic-gate * Increment the nice value of the specified thread by incr and
20800Sstevel@tonic-gate * return the new value in *retvalp.
20810Sstevel@tonic-gate */
20820Sstevel@tonic-gate static int
ts_donice(kthread_t * t,cred_t * cr,int incr,int * retvalp)20830Sstevel@tonic-gate ts_donice(kthread_t *t, cred_t *cr, int incr, int *retvalp)
20840Sstevel@tonic-gate {
20850Sstevel@tonic-gate int newnice;
20860Sstevel@tonic-gate tsproc_t *tspp = (tsproc_t *)(t->t_cldata);
20870Sstevel@tonic-gate tsparms_t tsparms;
20880Sstevel@tonic-gate
20890Sstevel@tonic-gate ASSERT(MUTEX_HELD(&(ttoproc(t))->p_lock));
20900Sstevel@tonic-gate
20910Sstevel@tonic-gate /* If there's no change to priority, just return current setting */
20920Sstevel@tonic-gate if (incr == 0) {
20930Sstevel@tonic-gate if (retvalp) {
20940Sstevel@tonic-gate *retvalp = tspp->ts_nice - NZERO;
20950Sstevel@tonic-gate }
20960Sstevel@tonic-gate return (0);
20970Sstevel@tonic-gate }
20980Sstevel@tonic-gate
20990Sstevel@tonic-gate if ((incr < 0 || incr > 2 * NZERO) &&
21000Sstevel@tonic-gate secpolicy_setpriority(cr) != 0)
21010Sstevel@tonic-gate return (EPERM);
21020Sstevel@tonic-gate
21030Sstevel@tonic-gate /*
21040Sstevel@tonic-gate * Specifying a nice increment greater than the upper limit of
21050Sstevel@tonic-gate * 2 * NZERO - 1 will result in the thread's nice value being
21060Sstevel@tonic-gate * set to the upper limit. We check for this before computing
21070Sstevel@tonic-gate * the new value because otherwise we could get overflow
21080Sstevel@tonic-gate * if a privileged process specified some ridiculous increment.
21090Sstevel@tonic-gate */
21100Sstevel@tonic-gate if (incr > 2 * NZERO - 1)
21110Sstevel@tonic-gate incr = 2 * NZERO - 1;
21120Sstevel@tonic-gate
21130Sstevel@tonic-gate newnice = tspp->ts_nice + incr;
21140Sstevel@tonic-gate if (newnice >= 2 * NZERO)
21150Sstevel@tonic-gate newnice = 2 * NZERO - 1;
21160Sstevel@tonic-gate else if (newnice < 0)
21170Sstevel@tonic-gate newnice = 0;
21180Sstevel@tonic-gate
21190Sstevel@tonic-gate tsparms.ts_uprilim = tsparms.ts_upri =
21206247Sraf -((newnice - NZERO) * ts_maxupri) / NZERO;
21210Sstevel@tonic-gate /*
21220Sstevel@tonic-gate * Reset the uprilim and upri values of the thread.
21230Sstevel@tonic-gate * Call ts_parmsset even if thread is interactive since we're
21240Sstevel@tonic-gate * not changing mode.
21250Sstevel@tonic-gate */
21260Sstevel@tonic-gate (void) ts_parmsset(t, (void *)&tsparms, (id_t)0, (cred_t *)NULL);
21270Sstevel@tonic-gate
21280Sstevel@tonic-gate /*
21290Sstevel@tonic-gate * Although ts_parmsset already reset ts_nice it may
21300Sstevel@tonic-gate * not have been set to precisely the value calculated above
21310Sstevel@tonic-gate * because ts_parmsset determines the nice value from the
21320Sstevel@tonic-gate * user priority and we may have truncated during the integer
21330Sstevel@tonic-gate * conversion from nice value to user priority and back.
21340Sstevel@tonic-gate * We reset ts_nice to the value we calculated above.
21350Sstevel@tonic-gate */
21360Sstevel@tonic-gate tspp->ts_nice = (char)newnice;
21370Sstevel@tonic-gate
21380Sstevel@tonic-gate if (retvalp)
21390Sstevel@tonic-gate *retvalp = newnice - NZERO;
21400Sstevel@tonic-gate return (0);
21410Sstevel@tonic-gate }
21420Sstevel@tonic-gate
21436247Sraf /*
21446247Sraf * Increment the priority of the specified thread by incr and
21456247Sraf * return the new value in *retvalp.
21466247Sraf */
21476247Sraf static int
ts_doprio(kthread_t * t,cred_t * cr,int incr,int * retvalp)21486247Sraf ts_doprio(kthread_t *t, cred_t *cr, int incr, int *retvalp)
21496247Sraf {
21506247Sraf int newpri;
21516247Sraf tsproc_t *tspp = (tsproc_t *)(t->t_cldata);
21526247Sraf tsparms_t tsparms;
21536247Sraf
21546247Sraf ASSERT(MUTEX_HELD(&(ttoproc(t))->p_lock));
21556247Sraf
21566247Sraf /* If there's no change to the priority, just return current setting */
21576247Sraf if (incr == 0) {
21586247Sraf *retvalp = tspp->ts_upri;
21596247Sraf return (0);
21606247Sraf }
21616247Sraf
21626247Sraf newpri = tspp->ts_upri + incr;
21636247Sraf if (newpri > ts_maxupri || newpri < -ts_maxupri)
21646247Sraf return (EINVAL);
21656247Sraf
21666247Sraf *retvalp = newpri;
21676247Sraf tsparms.ts_uprilim = tsparms.ts_upri = newpri;
21686247Sraf /*
21696247Sraf * Reset the uprilim and upri values of the thread.
21706247Sraf * Call ts_parmsset even if thread is interactive since we're
21716247Sraf * not changing mode.
21726247Sraf */
21736247Sraf return (ts_parmsset(t, &tsparms, 0, cr));
21746247Sraf }
21750Sstevel@tonic-gate
21760Sstevel@tonic-gate /*
21770Sstevel@tonic-gate * ia_set_process_group marks foreground processes as interactive
21780Sstevel@tonic-gate * and background processes as non-interactive iff the session
21790Sstevel@tonic-gate * leader is interactive. This routine is called from two places:
21800Sstevel@tonic-gate * strioctl:SPGRP when a new process group gets
21810Sstevel@tonic-gate * control of the tty.
21820Sstevel@tonic-gate * ia_parmsset-when the process in question is a session leader.
21830Sstevel@tonic-gate * ia_set_process_group assumes that pidlock is held by the caller,
21840Sstevel@tonic-gate * either strioctl or priocntlsys. If the caller is priocntlsys
21850Sstevel@tonic-gate * (via ia_parmsset) then the p_lock of the session leader is held
21860Sstevel@tonic-gate * and the code needs to be careful about acquiring other p_locks.
21870Sstevel@tonic-gate */
21880Sstevel@tonic-gate static void
ia_set_process_group(pid_t sid,pid_t bg_pgid,pid_t fg_pgid)21890Sstevel@tonic-gate ia_set_process_group(pid_t sid, pid_t bg_pgid, pid_t fg_pgid)
21900Sstevel@tonic-gate {
21910Sstevel@tonic-gate proc_t *leader, *fg, *bg;
21920Sstevel@tonic-gate tsproc_t *tspp;
21930Sstevel@tonic-gate kthread_t *tx;
21940Sstevel@tonic-gate int plocked = 0;
21950Sstevel@tonic-gate
21960Sstevel@tonic-gate ASSERT(MUTEX_HELD(&pidlock));
21970Sstevel@tonic-gate
21980Sstevel@tonic-gate /*
21990Sstevel@tonic-gate * see if the session leader is interactive AND
22000Sstevel@tonic-gate * if it is currently "on" AND controlling a tty
22010Sstevel@tonic-gate * iff it is then make the processes in the foreground
22020Sstevel@tonic-gate * group interactive and the processes in the background
22030Sstevel@tonic-gate * group non-interactive.
22040Sstevel@tonic-gate */
22050Sstevel@tonic-gate if ((leader = (proc_t *)prfind(sid)) == NULL) {
22060Sstevel@tonic-gate return;
22070Sstevel@tonic-gate }
22080Sstevel@tonic-gate if (leader->p_stat == SIDL) {
22090Sstevel@tonic-gate return;
22100Sstevel@tonic-gate }
22110Sstevel@tonic-gate if ((tx = proctot(leader)) == NULL) {
22120Sstevel@tonic-gate return;
22130Sstevel@tonic-gate }
22140Sstevel@tonic-gate /*
22150Sstevel@tonic-gate * XXX do all the threads in the leader
22160Sstevel@tonic-gate */
22170Sstevel@tonic-gate if (tx->t_cid != ia_cid) {
22180Sstevel@tonic-gate return;
22190Sstevel@tonic-gate }
22200Sstevel@tonic-gate tspp = tx->t_cldata;
22210Sstevel@tonic-gate /*
22220Sstevel@tonic-gate * session leaders that are not interactive need not have
22230Sstevel@tonic-gate * any processing done for them. They are typically shells
22240Sstevel@tonic-gate * that do not have focus and are changing the process group
22250Sstevel@tonic-gate * attatched to the tty, e.g. a process that is exiting
22260Sstevel@tonic-gate */
22272712Snn35248 mutex_enter(&leader->p_sessp->s_lock);
22280Sstevel@tonic-gate if (!(tspp->ts_flags & TSIASET) ||
22290Sstevel@tonic-gate (leader->p_sessp->s_vp == NULL) ||
22300Sstevel@tonic-gate (leader->p_sessp->s_vp->v_stream == NULL)) {
22312712Snn35248 mutex_exit(&leader->p_sessp->s_lock);
22320Sstevel@tonic-gate return;
22330Sstevel@tonic-gate }
22342712Snn35248 mutex_exit(&leader->p_sessp->s_lock);
22350Sstevel@tonic-gate
22360Sstevel@tonic-gate /*
22370Sstevel@tonic-gate * If we're already holding the leader's p_lock, we should use
22380Sstevel@tonic-gate * mutex_tryenter instead of mutex_enter to avoid deadlocks from
22390Sstevel@tonic-gate * lock ordering violations.
22400Sstevel@tonic-gate */
22410Sstevel@tonic-gate if (mutex_owned(&leader->p_lock))
22420Sstevel@tonic-gate plocked = 1;
22430Sstevel@tonic-gate
22440Sstevel@tonic-gate if (fg_pgid == 0)
22450Sstevel@tonic-gate goto skip;
22460Sstevel@tonic-gate /*
22470Sstevel@tonic-gate * now look for all processes in the foreground group and
22480Sstevel@tonic-gate * make them interactive
22490Sstevel@tonic-gate */
22500Sstevel@tonic-gate for (fg = (proc_t *)pgfind(fg_pgid); fg != NULL; fg = fg->p_pglink) {
22510Sstevel@tonic-gate /*
22520Sstevel@tonic-gate * if the process is SIDL it's begin forked, ignore it
22530Sstevel@tonic-gate */
22540Sstevel@tonic-gate if (fg->p_stat == SIDL) {
22550Sstevel@tonic-gate continue;
22560Sstevel@tonic-gate }
22570Sstevel@tonic-gate /*
22580Sstevel@tonic-gate * sesssion leaders must be turned on/off explicitly
22590Sstevel@tonic-gate * not implicitly as happens to other members of
22600Sstevel@tonic-gate * the process group.
22610Sstevel@tonic-gate */
22620Sstevel@tonic-gate if (fg->p_pid == fg->p_sessp->s_sid) {
22630Sstevel@tonic-gate continue;
22640Sstevel@tonic-gate }
22650Sstevel@tonic-gate
22660Sstevel@tonic-gate TRACE_1(TR_FAC_IA, TR_GROUP_ON,
22670Sstevel@tonic-gate "group on:proc %p", fg);
22680Sstevel@tonic-gate
22690Sstevel@tonic-gate if (plocked) {
22700Sstevel@tonic-gate if (mutex_tryenter(&fg->p_lock) == 0)
22710Sstevel@tonic-gate continue;
22720Sstevel@tonic-gate } else {
22730Sstevel@tonic-gate mutex_enter(&fg->p_lock);
22740Sstevel@tonic-gate }
22750Sstevel@tonic-gate
22760Sstevel@tonic-gate if ((tx = proctot(fg)) == NULL) {
22770Sstevel@tonic-gate mutex_exit(&fg->p_lock);
22780Sstevel@tonic-gate continue;
22790Sstevel@tonic-gate }
22800Sstevel@tonic-gate do {
22810Sstevel@tonic-gate thread_lock(tx);
22820Sstevel@tonic-gate /*
22830Sstevel@tonic-gate * if this thread is not interactive continue
22840Sstevel@tonic-gate */
22850Sstevel@tonic-gate if (tx->t_cid != ia_cid) {
22860Sstevel@tonic-gate thread_unlock(tx);
22870Sstevel@tonic-gate continue;
22880Sstevel@tonic-gate }
22890Sstevel@tonic-gate tspp = tx->t_cldata;
22900Sstevel@tonic-gate tspp->ts_flags |= TSIASET;
22910Sstevel@tonic-gate tspp->ts_boost = ia_boost;
22920Sstevel@tonic-gate TS_NEWUMDPRI(tspp);
22930Sstevel@tonic-gate if ((tspp->ts_flags & TSKPRI) != 0) {
22940Sstevel@tonic-gate thread_unlock(tx);
22950Sstevel@tonic-gate continue;
22960Sstevel@tonic-gate }
22970Sstevel@tonic-gate tspp->ts_dispwait = 0;
22980Sstevel@tonic-gate ts_change_priority(tx, tspp);
22990Sstevel@tonic-gate thread_unlock(tx);
23000Sstevel@tonic-gate } while ((tx = tx->t_forw) != fg->p_tlist);
23010Sstevel@tonic-gate mutex_exit(&fg->p_lock);
23020Sstevel@tonic-gate }
23030Sstevel@tonic-gate skip:
23040Sstevel@tonic-gate if (bg_pgid == 0)
23050Sstevel@tonic-gate return;
23060Sstevel@tonic-gate for (bg = (proc_t *)pgfind(bg_pgid); bg != NULL; bg = bg->p_pglink) {
23070Sstevel@tonic-gate if (bg->p_stat == SIDL) {
23080Sstevel@tonic-gate continue;
23090Sstevel@tonic-gate }
23100Sstevel@tonic-gate /*
23110Sstevel@tonic-gate * sesssion leaders must be turned off explicitly
23120Sstevel@tonic-gate * not implicitly as happens to other members of
23130Sstevel@tonic-gate * the process group.
23140Sstevel@tonic-gate */
23150Sstevel@tonic-gate if (bg->p_pid == bg->p_sessp->s_sid) {
23160Sstevel@tonic-gate continue;
23170Sstevel@tonic-gate }
23180Sstevel@tonic-gate
23190Sstevel@tonic-gate TRACE_1(TR_FAC_IA, TR_GROUP_OFF,
23200Sstevel@tonic-gate "group off:proc %p", bg);
23210Sstevel@tonic-gate
23220Sstevel@tonic-gate if (plocked) {
23230Sstevel@tonic-gate if (mutex_tryenter(&bg->p_lock) == 0)
23240Sstevel@tonic-gate continue;
23250Sstevel@tonic-gate } else {
23260Sstevel@tonic-gate mutex_enter(&bg->p_lock);
23270Sstevel@tonic-gate }
23280Sstevel@tonic-gate
23290Sstevel@tonic-gate if ((tx = proctot(bg)) == NULL) {
23300Sstevel@tonic-gate mutex_exit(&bg->p_lock);
23310Sstevel@tonic-gate continue;
23320Sstevel@tonic-gate }
23330Sstevel@tonic-gate do {
23340Sstevel@tonic-gate thread_lock(tx);
23350Sstevel@tonic-gate /*
23360Sstevel@tonic-gate * if this thread is not interactive continue
23370Sstevel@tonic-gate */
23380Sstevel@tonic-gate if (tx->t_cid != ia_cid) {
23390Sstevel@tonic-gate thread_unlock(tx);
23400Sstevel@tonic-gate continue;
23410Sstevel@tonic-gate }
23420Sstevel@tonic-gate tspp = tx->t_cldata;
23430Sstevel@tonic-gate tspp->ts_flags &= ~TSIASET;
23440Sstevel@tonic-gate tspp->ts_boost = -ia_boost;
23450Sstevel@tonic-gate TS_NEWUMDPRI(tspp);
23460Sstevel@tonic-gate if ((tspp->ts_flags & TSKPRI) != 0) {
23470Sstevel@tonic-gate thread_unlock(tx);
23480Sstevel@tonic-gate continue;
23490Sstevel@tonic-gate }
23500Sstevel@tonic-gate
23510Sstevel@tonic-gate tspp->ts_dispwait = 0;
23520Sstevel@tonic-gate ts_change_priority(tx, tspp);
23530Sstevel@tonic-gate thread_unlock(tx);
23540Sstevel@tonic-gate } while ((tx = tx->t_forw) != bg->p_tlist);
23550Sstevel@tonic-gate mutex_exit(&bg->p_lock);
23560Sstevel@tonic-gate }
23570Sstevel@tonic-gate }
23580Sstevel@tonic-gate
23590Sstevel@tonic-gate
23600Sstevel@tonic-gate static void
ts_change_priority(kthread_t * t,tsproc_t * tspp)23610Sstevel@tonic-gate ts_change_priority(kthread_t *t, tsproc_t *tspp)
23620Sstevel@tonic-gate {
23630Sstevel@tonic-gate pri_t new_pri;
23640Sstevel@tonic-gate
23650Sstevel@tonic-gate ASSERT(THREAD_LOCK_HELD(t));
23660Sstevel@tonic-gate new_pri = ts_dptbl[tspp->ts_umdpri].ts_globpri;
23670Sstevel@tonic-gate ASSERT(new_pri >= 0 && new_pri <= ts_maxglobpri);
2368951Sraf tspp->ts_flags &= ~TSRESTORE;
23696247Sraf t->t_cpri = tspp->ts_upri;
23700Sstevel@tonic-gate if (t == curthread || t->t_state == TS_ONPROC) {
23710Sstevel@tonic-gate /* curthread is always onproc */
23720Sstevel@tonic-gate cpu_t *cp = t->t_disp_queue->disp_cpu;
23730Sstevel@tonic-gate THREAD_CHANGE_PRI(t, new_pri);
23740Sstevel@tonic-gate if (t == cp->cpu_dispthread)
23750Sstevel@tonic-gate cp->cpu_dispatch_pri = DISP_PRIO(t);
23760Sstevel@tonic-gate if (DISP_MUST_SURRENDER(t)) {
23770Sstevel@tonic-gate tspp->ts_flags |= TSBACKQ;
23780Sstevel@tonic-gate cpu_surrender(t);
23790Sstevel@tonic-gate } else {
23800Sstevel@tonic-gate tspp->ts_timeleft =
23810Sstevel@tonic-gate ts_dptbl[tspp->ts_cpupri].ts_quantum;
23820Sstevel@tonic-gate }
23830Sstevel@tonic-gate } else {
23840Sstevel@tonic-gate int frontq;
23850Sstevel@tonic-gate
23860Sstevel@tonic-gate frontq = (tspp->ts_flags & TSIASET) != 0;
23870Sstevel@tonic-gate /*
23880Sstevel@tonic-gate * When the priority of a thread is changed,
23890Sstevel@tonic-gate * it may be necessary to adjust its position
23900Sstevel@tonic-gate * on a sleep queue or dispatch queue.
23910Sstevel@tonic-gate * The function thread_change_pri accomplishes
23920Sstevel@tonic-gate * this.
23930Sstevel@tonic-gate */
23940Sstevel@tonic-gate if (thread_change_pri(t, new_pri, frontq)) {
23950Sstevel@tonic-gate /*
23960Sstevel@tonic-gate * The thread was on a run queue. Reset
23970Sstevel@tonic-gate * its CPU timeleft from the quantum
23980Sstevel@tonic-gate * associated with the new priority.
23990Sstevel@tonic-gate */
24000Sstevel@tonic-gate tspp->ts_timeleft =
24010Sstevel@tonic-gate ts_dptbl[tspp->ts_cpupri].ts_quantum;
24020Sstevel@tonic-gate } else {
24030Sstevel@tonic-gate tspp->ts_flags |= TSBACKQ;
24040Sstevel@tonic-gate }
24050Sstevel@tonic-gate }
24060Sstevel@tonic-gate }
24070Sstevel@tonic-gate
24080Sstevel@tonic-gate static int
ts_alloc(void ** p,int flag)24090Sstevel@tonic-gate ts_alloc(void **p, int flag)
24100Sstevel@tonic-gate {
24110Sstevel@tonic-gate void *bufp;
24120Sstevel@tonic-gate bufp = kmem_alloc(sizeof (tsproc_t), flag);
24130Sstevel@tonic-gate if (bufp == NULL) {
24140Sstevel@tonic-gate return (ENOMEM);
24150Sstevel@tonic-gate } else {
24160Sstevel@tonic-gate *p = bufp;
24170Sstevel@tonic-gate return (0);
24180Sstevel@tonic-gate }
24190Sstevel@tonic-gate }
24200Sstevel@tonic-gate
24210Sstevel@tonic-gate static void
ts_free(void * bufp)24220Sstevel@tonic-gate ts_free(void *bufp)
24230Sstevel@tonic-gate {
24240Sstevel@tonic-gate if (bufp)
24250Sstevel@tonic-gate kmem_free(bufp, sizeof (tsproc_t));
24260Sstevel@tonic-gate }
2427