xref: /onnv-gate/usr/src/uts/common/disp/rt.c (revision 6247:ad4c702ff226)
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
5*6247Sraf  * Common Development and Distribution License (the "License").
6*6247Sraf  * 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  */
21*6247Sraf 
220Sstevel@tonic-gate /*
23*6247Sraf  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
280Sstevel@tonic-gate /*	  All Rights Reserved  	*/
290Sstevel@tonic-gate 
300Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
310Sstevel@tonic-gate 
320Sstevel@tonic-gate #include <sys/types.h>
330Sstevel@tonic-gate #include <sys/param.h>
340Sstevel@tonic-gate #include <sys/sysmacros.h>
350Sstevel@tonic-gate #include <sys/cred.h>
360Sstevel@tonic-gate #include <sys/proc.h>
370Sstevel@tonic-gate #include <sys/pcb.h>
380Sstevel@tonic-gate #include <sys/signal.h>
390Sstevel@tonic-gate #include <sys/user.h>
400Sstevel@tonic-gate #include <sys/priocntl.h>
410Sstevel@tonic-gate #include <sys/class.h>
420Sstevel@tonic-gate #include <sys/disp.h>
430Sstevel@tonic-gate #include <sys/procset.h>
440Sstevel@tonic-gate #include <sys/cmn_err.h>
450Sstevel@tonic-gate #include <sys/debug.h>
460Sstevel@tonic-gate #include <sys/rt.h>
470Sstevel@tonic-gate #include <sys/rtpriocntl.h>
480Sstevel@tonic-gate #include <sys/kmem.h>
490Sstevel@tonic-gate #include <sys/systm.h>
50*6247Sraf #include <sys/schedctl.h>
510Sstevel@tonic-gate #include <sys/errno.h>
520Sstevel@tonic-gate #include <sys/cpuvar.h>
530Sstevel@tonic-gate #include <sys/vmsystm.h>
540Sstevel@tonic-gate #include <sys/time.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 <sys/modctl.h>
590Sstevel@tonic-gate 
600Sstevel@tonic-gate static pri_t	rt_init(id_t, int, classfuncs_t **);
610Sstevel@tonic-gate 
620Sstevel@tonic-gate static struct sclass csw = {
630Sstevel@tonic-gate 	"RT",
640Sstevel@tonic-gate 	rt_init,
650Sstevel@tonic-gate 	0
660Sstevel@tonic-gate };
670Sstevel@tonic-gate 
680Sstevel@tonic-gate static struct modlsched modlsched = {
690Sstevel@tonic-gate 	&mod_schedops, "realtime scheduling class", &csw
700Sstevel@tonic-gate };
710Sstevel@tonic-gate 
720Sstevel@tonic-gate static struct modlinkage modlinkage = {
730Sstevel@tonic-gate 	MODREV_1, (void *)&modlsched, NULL
740Sstevel@tonic-gate };
750Sstevel@tonic-gate 
760Sstevel@tonic-gate int
_init()770Sstevel@tonic-gate _init()
780Sstevel@tonic-gate {
790Sstevel@tonic-gate 	return (mod_install(&modlinkage));
800Sstevel@tonic-gate }
810Sstevel@tonic-gate 
820Sstevel@tonic-gate int
_fini()830Sstevel@tonic-gate _fini()
840Sstevel@tonic-gate {
850Sstevel@tonic-gate 	return (EBUSY);		/* don't remove RT for now */
860Sstevel@tonic-gate }
870Sstevel@tonic-gate 
880Sstevel@tonic-gate int
_info(struct modinfo * modinfop)890Sstevel@tonic-gate _info(struct modinfo *modinfop)
900Sstevel@tonic-gate {
910Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
920Sstevel@tonic-gate }
930Sstevel@tonic-gate 
940Sstevel@tonic-gate 
950Sstevel@tonic-gate /*
960Sstevel@tonic-gate  * Class specific code for the real-time class
970Sstevel@tonic-gate  */
980Sstevel@tonic-gate 
990Sstevel@tonic-gate /*
1000Sstevel@tonic-gate  * Extern declarations for variables defined in the rt master file
1010Sstevel@tonic-gate  */
1020Sstevel@tonic-gate #define	RTMAXPRI 59
1030Sstevel@tonic-gate 
1040Sstevel@tonic-gate pri_t rt_maxpri = RTMAXPRI;	/* maximum real-time priority */
1050Sstevel@tonic-gate rtdpent_t *rt_dptbl;	  /* real-time dispatcher parameter table */
1060Sstevel@tonic-gate 
1070Sstevel@tonic-gate /*
1080Sstevel@tonic-gate  * control flags (kparms->rt_cflags).
1090Sstevel@tonic-gate  */
1100Sstevel@tonic-gate #define	RT_DOPRI	0x01	/* change priority */
1110Sstevel@tonic-gate #define	RT_DOTQ		0x02	/* change RT time quantum */
1120Sstevel@tonic-gate #define	RT_DOSIG	0x04	/* change RT time quantum signal */
1130Sstevel@tonic-gate 
1140Sstevel@tonic-gate static int	rt_admin(caddr_t, cred_t *);
1150Sstevel@tonic-gate static int	rt_enterclass(kthread_t *, id_t, void *, cred_t *, void *);
1160Sstevel@tonic-gate static int	rt_fork(kthread_t *, kthread_t *, void *);
1170Sstevel@tonic-gate static int	rt_getclinfo(void *);
1180Sstevel@tonic-gate static int	rt_getclpri(pcpri_t *);
1190Sstevel@tonic-gate static int	rt_parmsin(void *);
1200Sstevel@tonic-gate static int	rt_parmsout(void *, pc_vaparms_t *);
1210Sstevel@tonic-gate static int	rt_vaparmsin(void *, pc_vaparms_t *);
1220Sstevel@tonic-gate static int	rt_vaparmsout(void *, pc_vaparms_t *);
1230Sstevel@tonic-gate static int	rt_parmsset(kthread_t *, void *, id_t, cred_t *);
1240Sstevel@tonic-gate static int	rt_donice(kthread_t *, cred_t *, int, int *);
125*6247Sraf static int	rt_doprio(kthread_t *, cred_t *, int, int *);
1260Sstevel@tonic-gate static void	rt_exitclass(void *);
1270Sstevel@tonic-gate static int	rt_canexit(kthread_t *, cred_t *);
1280Sstevel@tonic-gate static void	rt_forkret(kthread_t *, kthread_t *);
1290Sstevel@tonic-gate static void	rt_nullsys();
1300Sstevel@tonic-gate static void	rt_parmsget(kthread_t *, void *);
1310Sstevel@tonic-gate static void	rt_preempt(kthread_t *);
1320Sstevel@tonic-gate static void	rt_setrun(kthread_t *);
1330Sstevel@tonic-gate static void	rt_tick(kthread_t *);
1340Sstevel@tonic-gate static void	rt_wakeup(kthread_t *);
1350Sstevel@tonic-gate static pri_t	rt_swapin(kthread_t *, int);
1360Sstevel@tonic-gate static pri_t	rt_swapout(kthread_t *, int);
1370Sstevel@tonic-gate static pri_t	rt_globpri(kthread_t *);
1380Sstevel@tonic-gate static void	rt_yield(kthread_t *);
1390Sstevel@tonic-gate static int	rt_alloc(void **, int);
1400Sstevel@tonic-gate static void	rt_free(void *);
1410Sstevel@tonic-gate 
1420Sstevel@tonic-gate static void	rt_change_priority(kthread_t *, rtproc_t *);
1430Sstevel@tonic-gate 
1440Sstevel@tonic-gate static id_t	rt_cid;		/* real-time class ID */
1450Sstevel@tonic-gate static rtproc_t	rt_plisthead;	/* dummy rtproc at head of rtproc list */
1460Sstevel@tonic-gate static kmutex_t	rt_dptblock;	/* protects realtime dispatch table */
1470Sstevel@tonic-gate static kmutex_t	rt_list_lock;	/* protects RT thread list */
1480Sstevel@tonic-gate 
1490Sstevel@tonic-gate extern rtdpent_t *rt_getdptbl(void);
1500Sstevel@tonic-gate 
1510Sstevel@tonic-gate static struct classfuncs rt_classfuncs = {
1520Sstevel@tonic-gate 	/* class ops */
1530Sstevel@tonic-gate 	rt_admin,
1540Sstevel@tonic-gate 	rt_getclinfo,
1550Sstevel@tonic-gate 	rt_parmsin,
1560Sstevel@tonic-gate 	rt_parmsout,
1570Sstevel@tonic-gate 	rt_vaparmsin,
1580Sstevel@tonic-gate 	rt_vaparmsout,
1590Sstevel@tonic-gate 	rt_getclpri,
1600Sstevel@tonic-gate 	rt_alloc,
1610Sstevel@tonic-gate 	rt_free,
1620Sstevel@tonic-gate 	/* thread ops */
1630Sstevel@tonic-gate 	rt_enterclass,
1640Sstevel@tonic-gate 	rt_exitclass,
1650Sstevel@tonic-gate 	rt_canexit,
1660Sstevel@tonic-gate 	rt_fork,
1670Sstevel@tonic-gate 	rt_forkret,
1680Sstevel@tonic-gate 	rt_parmsget,
1690Sstevel@tonic-gate 	rt_parmsset,
1700Sstevel@tonic-gate 	rt_nullsys,	/* stop */
1710Sstevel@tonic-gate 	rt_nullsys,	/* exit */
1720Sstevel@tonic-gate 	rt_nullsys,	/* active */
1730Sstevel@tonic-gate 	rt_nullsys,	/* inactive */
1740Sstevel@tonic-gate 	rt_swapin,
1750Sstevel@tonic-gate 	rt_swapout,
1760Sstevel@tonic-gate 	rt_nullsys,	/* trapret */
1770Sstevel@tonic-gate 	rt_preempt,
1780Sstevel@tonic-gate 	rt_setrun,
1790Sstevel@tonic-gate 	rt_nullsys,	/* sleep */
1800Sstevel@tonic-gate 	rt_tick,
1810Sstevel@tonic-gate 	rt_wakeup,
1820Sstevel@tonic-gate 	rt_donice,
1830Sstevel@tonic-gate 	rt_globpri,
1840Sstevel@tonic-gate 	rt_nullsys,	/* set_process_group */
1850Sstevel@tonic-gate 	rt_yield,
186*6247Sraf 	rt_doprio,
1870Sstevel@tonic-gate };
1880Sstevel@tonic-gate 
1890Sstevel@tonic-gate /*
1900Sstevel@tonic-gate  * Real-time class initialization. Called by dispinit() at boot time.
1910Sstevel@tonic-gate  * We can ignore the clparmsz argument since we know that the smallest
1920Sstevel@tonic-gate  * possible parameter buffer is big enough for us.
1930Sstevel@tonic-gate  */
1940Sstevel@tonic-gate /* ARGSUSED */
1950Sstevel@tonic-gate pri_t
rt_init(id_t cid,int clparmsz,classfuncs_t ** clfuncspp)1960Sstevel@tonic-gate rt_init(id_t cid, int clparmsz, classfuncs_t **clfuncspp)
1970Sstevel@tonic-gate {
1980Sstevel@tonic-gate 	rt_dptbl = rt_getdptbl();
1990Sstevel@tonic-gate 	rt_cid = cid;	/* Record our class ID */
2000Sstevel@tonic-gate 
2010Sstevel@tonic-gate 	/*
2020Sstevel@tonic-gate 	 * Initialize the rtproc list.
2030Sstevel@tonic-gate 	 */
2040Sstevel@tonic-gate 	rt_plisthead.rt_next = rt_plisthead.rt_prev = &rt_plisthead;
2050Sstevel@tonic-gate 
2060Sstevel@tonic-gate 	/*
2070Sstevel@tonic-gate 	 * We're required to return a pointer to our classfuncs
2080Sstevel@tonic-gate 	 * structure and the highest global priority value we use.
2090Sstevel@tonic-gate 	 */
2100Sstevel@tonic-gate 	*clfuncspp = &rt_classfuncs;
2110Sstevel@tonic-gate 	mutex_init(&rt_dptblock, NULL, MUTEX_DEFAULT, NULL);
2120Sstevel@tonic-gate 	mutex_init(&rt_list_lock, NULL, MUTEX_DEFAULT, NULL);
2130Sstevel@tonic-gate 	return (rt_dptbl[rt_maxpri].rt_globpri);
2140Sstevel@tonic-gate }
2150Sstevel@tonic-gate 
2160Sstevel@tonic-gate /*
2170Sstevel@tonic-gate  * Get or reset the rt_dptbl values per the user's request.
2180Sstevel@tonic-gate  */
2190Sstevel@tonic-gate /* ARGSUSED */
2200Sstevel@tonic-gate static int
rt_admin(caddr_t uaddr,cred_t * reqpcredp)2210Sstevel@tonic-gate rt_admin(caddr_t uaddr, cred_t *reqpcredp)
2220Sstevel@tonic-gate {
2230Sstevel@tonic-gate 	rtadmin_t	rtadmin;
2240Sstevel@tonic-gate 	rtdpent_t	*tmpdpp;
2250Sstevel@tonic-gate 	size_t		userdpsz;
2260Sstevel@tonic-gate 	size_t		rtdpsz;
2270Sstevel@tonic-gate 	int		i;
2280Sstevel@tonic-gate 
2290Sstevel@tonic-gate 	if (get_udatamodel() == DATAMODEL_NATIVE) {
2300Sstevel@tonic-gate 		if (copyin(uaddr, &rtadmin, sizeof (rtadmin_t)))
2310Sstevel@tonic-gate 			return (EFAULT);
2320Sstevel@tonic-gate 	}
2330Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
2340Sstevel@tonic-gate 	else {
2350Sstevel@tonic-gate 		/* rtadmin struct from ILP32 callers */
2360Sstevel@tonic-gate 		rtadmin32_t rtadmin32;
2370Sstevel@tonic-gate 		if (copyin(uaddr, &rtadmin32, sizeof (rtadmin32_t)))
2380Sstevel@tonic-gate 			return (EFAULT);
2390Sstevel@tonic-gate 		rtadmin.rt_dpents =
2400Sstevel@tonic-gate 		    (struct rtdpent *)(uintptr_t)rtadmin32.rt_dpents;
2410Sstevel@tonic-gate 		rtadmin.rt_ndpents = rtadmin32.rt_ndpents;
2420Sstevel@tonic-gate 		rtadmin.rt_cmd = rtadmin32.rt_cmd;
2430Sstevel@tonic-gate 	}
2440Sstevel@tonic-gate #endif /* _SYSCALL32_IMPL */
2450Sstevel@tonic-gate 
2460Sstevel@tonic-gate 	rtdpsz = (rt_maxpri + 1) * sizeof (rtdpent_t);
2470Sstevel@tonic-gate 
2480Sstevel@tonic-gate 	switch (rtadmin.rt_cmd) {
2490Sstevel@tonic-gate 
2500Sstevel@tonic-gate 	case RT_GETDPSIZE:
2510Sstevel@tonic-gate 		rtadmin.rt_ndpents = rt_maxpri + 1;
2520Sstevel@tonic-gate 
2530Sstevel@tonic-gate 		if (get_udatamodel() == DATAMODEL_NATIVE) {
2540Sstevel@tonic-gate 			if (copyout(&rtadmin, uaddr, sizeof (rtadmin_t)))
2550Sstevel@tonic-gate 				return (EFAULT);
2560Sstevel@tonic-gate 		}
2570Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
2580Sstevel@tonic-gate 		else {
2590Sstevel@tonic-gate 			/* return rtadmin struct to ILP32 callers */
2600Sstevel@tonic-gate 			rtadmin32_t rtadmin32;
2610Sstevel@tonic-gate 			rtadmin32.rt_dpents =
2620Sstevel@tonic-gate 			    (caddr32_t)(uintptr_t)rtadmin.rt_dpents;
2630Sstevel@tonic-gate 			rtadmin32.rt_ndpents = rtadmin.rt_ndpents;
2640Sstevel@tonic-gate 			rtadmin32.rt_cmd = rtadmin.rt_cmd;
2650Sstevel@tonic-gate 			if (copyout(&rtadmin32, uaddr, sizeof (rtadmin32_t)))
2660Sstevel@tonic-gate 				return (EFAULT);
2670Sstevel@tonic-gate 		}
2680Sstevel@tonic-gate #endif /* _SYSCALL32_IMPL */
2690Sstevel@tonic-gate 
2700Sstevel@tonic-gate 		break;
2710Sstevel@tonic-gate 
2720Sstevel@tonic-gate 	case RT_GETDPTBL:
2730Sstevel@tonic-gate 		userdpsz = MIN(rtadmin.rt_ndpents * sizeof (rtdpent_t),
2740Sstevel@tonic-gate 		    rtdpsz);
2750Sstevel@tonic-gate 		if (copyout(rt_dptbl, rtadmin.rt_dpents, userdpsz))
2760Sstevel@tonic-gate 			return (EFAULT);
2770Sstevel@tonic-gate 		rtadmin.rt_ndpents = userdpsz / sizeof (rtdpent_t);
2780Sstevel@tonic-gate 
2790Sstevel@tonic-gate 		if (get_udatamodel() == DATAMODEL_NATIVE) {
2800Sstevel@tonic-gate 			if (copyout(&rtadmin, uaddr, sizeof (rtadmin_t)))
2810Sstevel@tonic-gate 				return (EFAULT);
2820Sstevel@tonic-gate 		}
2830Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
2840Sstevel@tonic-gate 		else {
2850Sstevel@tonic-gate 			/* return rtadmin struct to ILP32 callers */
2860Sstevel@tonic-gate 			rtadmin32_t rtadmin32;
2870Sstevel@tonic-gate 			rtadmin32.rt_dpents =
2880Sstevel@tonic-gate 			    (caddr32_t)(uintptr_t)rtadmin.rt_dpents;
2890Sstevel@tonic-gate 			rtadmin32.rt_ndpents = rtadmin.rt_ndpents;
2900Sstevel@tonic-gate 			rtadmin32.rt_cmd = rtadmin.rt_cmd;
2910Sstevel@tonic-gate 			if (copyout(&rtadmin32, uaddr, sizeof (rtadmin32_t)))
2920Sstevel@tonic-gate 				return (EFAULT);
2930Sstevel@tonic-gate 		}
2940Sstevel@tonic-gate #endif /* _SYSCALL32_IMPL */
2950Sstevel@tonic-gate 		break;
2960Sstevel@tonic-gate 
2970Sstevel@tonic-gate 	case RT_SETDPTBL:
2980Sstevel@tonic-gate 		/*
2990Sstevel@tonic-gate 		 * We require that the requesting process has sufficient
3000Sstevel@tonic-gate 		 * priveleges.  We also require that the table supplied by
3010Sstevel@tonic-gate 		 * the user exactly match the current rt_dptbl in size.
3020Sstevel@tonic-gate 		 */
3030Sstevel@tonic-gate 		if (secpolicy_dispadm(reqpcredp) != 0)
3040Sstevel@tonic-gate 			return (EPERM);
3050Sstevel@tonic-gate 		if (rtadmin.rt_ndpents * sizeof (rtdpent_t) != rtdpsz)
3060Sstevel@tonic-gate 			return (EINVAL);
3070Sstevel@tonic-gate 
3080Sstevel@tonic-gate 		/*
3090Sstevel@tonic-gate 		 * We read the user supplied table into a temporary buffer
3100Sstevel@tonic-gate 		 * where the time quantum values are validated before
3110Sstevel@tonic-gate 		 * being copied to the rt_dptbl.
3120Sstevel@tonic-gate 		 */
3130Sstevel@tonic-gate 		tmpdpp = kmem_alloc(rtdpsz, KM_SLEEP);
3140Sstevel@tonic-gate 		if (copyin(rtadmin.rt_dpents, tmpdpp, rtdpsz)) {
3150Sstevel@tonic-gate 			kmem_free(tmpdpp, rtdpsz);
3160Sstevel@tonic-gate 			return (EFAULT);
3170Sstevel@tonic-gate 		}
3180Sstevel@tonic-gate 		for (i = 0; i < rtadmin.rt_ndpents; i++) {
3190Sstevel@tonic-gate 
3200Sstevel@tonic-gate 			/*
3210Sstevel@tonic-gate 			 * Validate the user supplied time quantum values.
3220Sstevel@tonic-gate 			 */
3230Sstevel@tonic-gate 			if (tmpdpp[i].rt_quantum <= 0 &&
3240Sstevel@tonic-gate 			    tmpdpp[i].rt_quantum != RT_TQINF) {
3250Sstevel@tonic-gate 				kmem_free(tmpdpp, rtdpsz);
3260Sstevel@tonic-gate 				return (EINVAL);
3270Sstevel@tonic-gate 			}
3280Sstevel@tonic-gate 		}
3290Sstevel@tonic-gate 
3300Sstevel@tonic-gate 		/*
3310Sstevel@tonic-gate 		 * Copy the user supplied values over the current rt_dptbl
3320Sstevel@tonic-gate 		 * values.  The rt_globpri member is read-only so we don't
3330Sstevel@tonic-gate 		 * overwrite it.
3340Sstevel@tonic-gate 		 */
3350Sstevel@tonic-gate 		mutex_enter(&rt_dptblock);
3360Sstevel@tonic-gate 		for (i = 0; i < rtadmin.rt_ndpents; i++)
3370Sstevel@tonic-gate 			rt_dptbl[i].rt_quantum = tmpdpp[i].rt_quantum;
3380Sstevel@tonic-gate 		mutex_exit(&rt_dptblock);
3390Sstevel@tonic-gate 		kmem_free(tmpdpp, rtdpsz);
3400Sstevel@tonic-gate 		break;
3410Sstevel@tonic-gate 
3420Sstevel@tonic-gate 	default:
3430Sstevel@tonic-gate 		return (EINVAL);
3440Sstevel@tonic-gate 	}
3450Sstevel@tonic-gate 	return (0);
3460Sstevel@tonic-gate }
3470Sstevel@tonic-gate 
3480Sstevel@tonic-gate 
3490Sstevel@tonic-gate /*
3500Sstevel@tonic-gate  * Allocate a real-time class specific proc structure and
3510Sstevel@tonic-gate  * initialize it with the parameters supplied. Also move thread
3520Sstevel@tonic-gate  * to specified real-time priority.
3530Sstevel@tonic-gate  */
3540Sstevel@tonic-gate /* ARGSUSED */
3550Sstevel@tonic-gate static int
rt_enterclass(kthread_t * t,id_t cid,void * parmsp,cred_t * reqpcredp,void * bufp)3560Sstevel@tonic-gate rt_enterclass(kthread_t *t, id_t cid, void *parmsp, cred_t *reqpcredp,
3570Sstevel@tonic-gate     void *bufp)
3580Sstevel@tonic-gate {
3590Sstevel@tonic-gate 	rtkparms_t *rtkparmsp = (rtkparms_t *)parmsp;
3600Sstevel@tonic-gate 	rtproc_t *rtpp;
3610Sstevel@tonic-gate 
3620Sstevel@tonic-gate 	/*
3630Sstevel@tonic-gate 	 * For a thread to enter the real-time class the thread
3640Sstevel@tonic-gate 	 * which initiates the request must be privileged.
3650Sstevel@tonic-gate 	 * This may have been checked previously but if our
3660Sstevel@tonic-gate 	 * caller passed us a credential structure we assume it
3670Sstevel@tonic-gate 	 * hasn't and we check it here.
3680Sstevel@tonic-gate 	 */
3690Sstevel@tonic-gate 	if (reqpcredp != NULL && secpolicy_setpriority(reqpcredp) != 0)
3700Sstevel@tonic-gate 		return (EPERM);
3710Sstevel@tonic-gate 
3720Sstevel@tonic-gate 	rtpp = (rtproc_t *)bufp;
3730Sstevel@tonic-gate 	ASSERT(rtpp != NULL);
3740Sstevel@tonic-gate 
3750Sstevel@tonic-gate 	/*
3760Sstevel@tonic-gate 	 * If this thread's lwp is swapped out, it will be brought in
3770Sstevel@tonic-gate 	 * when it is put onto the runqueue.
3780Sstevel@tonic-gate 	 *
3790Sstevel@tonic-gate 	 * Now, Initialize the rtproc structure.
3800Sstevel@tonic-gate 	 */
3810Sstevel@tonic-gate 	if (rtkparmsp == NULL) {
3820Sstevel@tonic-gate 		/*
3830Sstevel@tonic-gate 		 * Use default values
3840Sstevel@tonic-gate 		 */
3850Sstevel@tonic-gate 		rtpp->rt_pri = 0;
3860Sstevel@tonic-gate 		rtpp->rt_pquantum = rt_dptbl[0].rt_quantum;
3870Sstevel@tonic-gate 		rtpp->rt_tqsignal = 0;
3880Sstevel@tonic-gate 	} else {
3890Sstevel@tonic-gate 		/*
3900Sstevel@tonic-gate 		 * Use supplied values
3910Sstevel@tonic-gate 		 */
3920Sstevel@tonic-gate 		if ((rtkparmsp->rt_cflags & RT_DOPRI) == 0)
3930Sstevel@tonic-gate 			rtpp->rt_pri = 0;
3940Sstevel@tonic-gate 		else
3950Sstevel@tonic-gate 			rtpp->rt_pri = rtkparmsp->rt_pri;
3960Sstevel@tonic-gate 
3970Sstevel@tonic-gate 		if (rtkparmsp->rt_tqntm == RT_TQINF)
3980Sstevel@tonic-gate 			rtpp->rt_pquantum = RT_TQINF;
3990Sstevel@tonic-gate 		else if (rtkparmsp->rt_tqntm == RT_TQDEF ||
4000Sstevel@tonic-gate 		    (rtkparmsp->rt_cflags & RT_DOTQ) == 0)
4010Sstevel@tonic-gate 			rtpp->rt_pquantum = rt_dptbl[rtpp->rt_pri].rt_quantum;
4020Sstevel@tonic-gate 		else
4030Sstevel@tonic-gate 			rtpp->rt_pquantum = rtkparmsp->rt_tqntm;
4040Sstevel@tonic-gate 
4050Sstevel@tonic-gate 		if ((rtkparmsp->rt_cflags & RT_DOSIG) == 0)
4060Sstevel@tonic-gate 			rtpp->rt_tqsignal = 0;
4070Sstevel@tonic-gate 		else
4080Sstevel@tonic-gate 			rtpp->rt_tqsignal = rtkparmsp->rt_tqsig;
4090Sstevel@tonic-gate 	}
4100Sstevel@tonic-gate 	rtpp->rt_flags = 0;
4110Sstevel@tonic-gate 	rtpp->rt_tp = t;
4120Sstevel@tonic-gate 	/*
4130Sstevel@tonic-gate 	 * Reset thread priority
4140Sstevel@tonic-gate 	 */
4150Sstevel@tonic-gate 	thread_lock(t);
4160Sstevel@tonic-gate 	t->t_clfuncs = &(sclass[cid].cl_funcs->thread);
4170Sstevel@tonic-gate 	t->t_cid = cid;
4180Sstevel@tonic-gate 	t->t_cldata = (void *)rtpp;
4190Sstevel@tonic-gate 	t->t_schedflag &= ~TS_RUNQMATCH;
4200Sstevel@tonic-gate 	rt_change_priority(t, rtpp);
4210Sstevel@tonic-gate 	thread_unlock(t);
4220Sstevel@tonic-gate 	/*
4230Sstevel@tonic-gate 	 * Link new structure into rtproc list
4240Sstevel@tonic-gate 	 */
4250Sstevel@tonic-gate 	mutex_enter(&rt_list_lock);
4260Sstevel@tonic-gate 	rtpp->rt_next = rt_plisthead.rt_next;
4270Sstevel@tonic-gate 	rtpp->rt_prev = &rt_plisthead;
4280Sstevel@tonic-gate 	rt_plisthead.rt_next->rt_prev = rtpp;
4290Sstevel@tonic-gate 	rt_plisthead.rt_next = rtpp;
4300Sstevel@tonic-gate 	mutex_exit(&rt_list_lock);
4310Sstevel@tonic-gate 	return (0);
4320Sstevel@tonic-gate }
4330Sstevel@tonic-gate 
4340Sstevel@tonic-gate 
4350Sstevel@tonic-gate /*
4360Sstevel@tonic-gate  * Free rtproc structure of thread.
4370Sstevel@tonic-gate  */
4380Sstevel@tonic-gate static void
rt_exitclass(void * procp)4390Sstevel@tonic-gate rt_exitclass(void *procp)
4400Sstevel@tonic-gate {
4410Sstevel@tonic-gate 	rtproc_t *rtprocp = (rtproc_t *)procp;
4420Sstevel@tonic-gate 
4430Sstevel@tonic-gate 	mutex_enter(&rt_list_lock);
4440Sstevel@tonic-gate 	rtprocp->rt_prev->rt_next = rtprocp->rt_next;
4450Sstevel@tonic-gate 	rtprocp->rt_next->rt_prev = rtprocp->rt_prev;
4460Sstevel@tonic-gate 	mutex_exit(&rt_list_lock);
4470Sstevel@tonic-gate 	kmem_free(rtprocp, sizeof (rtproc_t));
4480Sstevel@tonic-gate }
4490Sstevel@tonic-gate 
4500Sstevel@tonic-gate 
4510Sstevel@tonic-gate /*
4520Sstevel@tonic-gate  * Allocate and initialize real-time class specific
4530Sstevel@tonic-gate  * proc structure for child.
4540Sstevel@tonic-gate  */
4550Sstevel@tonic-gate /* ARGSUSED */
4560Sstevel@tonic-gate static int
rt_fork(kthread_t * t,kthread_t * ct,void * bufp)4570Sstevel@tonic-gate rt_fork(kthread_t *t, kthread_t *ct, void *bufp)
4580Sstevel@tonic-gate {
4590Sstevel@tonic-gate 	rtproc_t *prtpp;
4600Sstevel@tonic-gate 	rtproc_t *crtpp;
4610Sstevel@tonic-gate 
4620Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&ttoproc(t)->p_lock));
4630Sstevel@tonic-gate 
4640Sstevel@tonic-gate 	/*
4650Sstevel@tonic-gate 	 * Initialize child's rtproc structure
4660Sstevel@tonic-gate 	 */
4670Sstevel@tonic-gate 	crtpp = (rtproc_t *)bufp;
4680Sstevel@tonic-gate 	ASSERT(crtpp != NULL);
4690Sstevel@tonic-gate 	prtpp = (rtproc_t *)t->t_cldata;
4700Sstevel@tonic-gate 	thread_lock(t);
4710Sstevel@tonic-gate 	crtpp->rt_timeleft = crtpp->rt_pquantum = prtpp->rt_pquantum;
4720Sstevel@tonic-gate 	crtpp->rt_pri = prtpp->rt_pri;
4730Sstevel@tonic-gate 	crtpp->rt_flags = prtpp->rt_flags & ~RTBACKQ;
4740Sstevel@tonic-gate 	crtpp->rt_tqsignal = prtpp->rt_tqsignal;
4750Sstevel@tonic-gate 
4760Sstevel@tonic-gate 	crtpp->rt_tp = ct;
4770Sstevel@tonic-gate 	thread_unlock(t);
4780Sstevel@tonic-gate 
4790Sstevel@tonic-gate 	/*
4800Sstevel@tonic-gate 	 * Link new structure into rtproc list
4810Sstevel@tonic-gate 	 */
4820Sstevel@tonic-gate 	ct->t_cldata = (void *)crtpp;
4830Sstevel@tonic-gate 	mutex_enter(&rt_list_lock);
4840Sstevel@tonic-gate 	crtpp->rt_next = rt_plisthead.rt_next;
4850Sstevel@tonic-gate 	crtpp->rt_prev = &rt_plisthead;
4860Sstevel@tonic-gate 	rt_plisthead.rt_next->rt_prev = crtpp;
4870Sstevel@tonic-gate 	rt_plisthead.rt_next = crtpp;
4880Sstevel@tonic-gate 	mutex_exit(&rt_list_lock);
4890Sstevel@tonic-gate 	return (0);
4900Sstevel@tonic-gate }
4910Sstevel@tonic-gate 
4920Sstevel@tonic-gate 
4930Sstevel@tonic-gate /*
4940Sstevel@tonic-gate  * The child goes to the back of its dispatcher queue while the
4950Sstevel@tonic-gate  * parent continues to run after a real time thread forks.
4960Sstevel@tonic-gate  */
4970Sstevel@tonic-gate /* ARGSUSED */
4980Sstevel@tonic-gate static void
rt_forkret(kthread_t * t,kthread_t * ct)4990Sstevel@tonic-gate rt_forkret(kthread_t *t, kthread_t *ct)
5000Sstevel@tonic-gate {
5010Sstevel@tonic-gate 	proc_t *pp = ttoproc(t);
5020Sstevel@tonic-gate 	proc_t *cp = ttoproc(ct);
5030Sstevel@tonic-gate 
5040Sstevel@tonic-gate 	ASSERT(t == curthread);
5050Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&pidlock));
5060Sstevel@tonic-gate 
5070Sstevel@tonic-gate 	/*
5080Sstevel@tonic-gate 	 * Grab the child's p_lock before dropping pidlock to ensure
5090Sstevel@tonic-gate 	 * the process does not disappear before we set it running.
5100Sstevel@tonic-gate 	 */
5110Sstevel@tonic-gate 	mutex_enter(&cp->p_lock);
5120Sstevel@tonic-gate 	mutex_exit(&pidlock);
5130Sstevel@tonic-gate 	continuelwps(cp);
5140Sstevel@tonic-gate 	mutex_exit(&cp->p_lock);
5150Sstevel@tonic-gate 
5160Sstevel@tonic-gate 	mutex_enter(&pp->p_lock);
5170Sstevel@tonic-gate 	continuelwps(pp);
5180Sstevel@tonic-gate 	mutex_exit(&pp->p_lock);
5190Sstevel@tonic-gate }
5200Sstevel@tonic-gate 
5210Sstevel@tonic-gate 
5220Sstevel@tonic-gate /*
5230Sstevel@tonic-gate  * Get information about the real-time class into the buffer
5240Sstevel@tonic-gate  * pointed to by rtinfop.  The maximum configured real-time
5250Sstevel@tonic-gate  * priority is the only information we supply.  We ignore the
5260Sstevel@tonic-gate  * class and credential arguments because anyone can have this
5270Sstevel@tonic-gate  * information.
5280Sstevel@tonic-gate  */
5290Sstevel@tonic-gate /* ARGSUSED */
5300Sstevel@tonic-gate static int
rt_getclinfo(void * infop)5310Sstevel@tonic-gate rt_getclinfo(void *infop)
5320Sstevel@tonic-gate {
5330Sstevel@tonic-gate 	rtinfo_t *rtinfop = (rtinfo_t *)infop;
5340Sstevel@tonic-gate 	rtinfop->rt_maxpri = rt_maxpri;
5350Sstevel@tonic-gate 	return (0);
5360Sstevel@tonic-gate }
5370Sstevel@tonic-gate 
5380Sstevel@tonic-gate /*
539*6247Sraf  * Return the user mode scheduling priority range.
5400Sstevel@tonic-gate  */
5410Sstevel@tonic-gate static int
rt_getclpri(pcpri_t * pcprip)5420Sstevel@tonic-gate rt_getclpri(pcpri_t *pcprip)
5430Sstevel@tonic-gate {
544*6247Sraf 	pcprip->pc_clpmax = rt_maxpri;
545*6247Sraf 	pcprip->pc_clpmin = 0;
5460Sstevel@tonic-gate 	return (0);
5470Sstevel@tonic-gate }
548*6247Sraf 
5490Sstevel@tonic-gate static void
rt_nullsys()5500Sstevel@tonic-gate rt_nullsys()
5510Sstevel@tonic-gate {
5520Sstevel@tonic-gate }
5530Sstevel@tonic-gate 
5540Sstevel@tonic-gate /* ARGSUSED */
5550Sstevel@tonic-gate static int
rt_canexit(kthread_t * t,cred_t * cred)5560Sstevel@tonic-gate rt_canexit(kthread_t *t, cred_t *cred)
5570Sstevel@tonic-gate {
5580Sstevel@tonic-gate 	/*
5590Sstevel@tonic-gate 	 * Thread can always leave RT class
5600Sstevel@tonic-gate 	 */
5610Sstevel@tonic-gate 	return (0);
5620Sstevel@tonic-gate }
5630Sstevel@tonic-gate 
5640Sstevel@tonic-gate /*
5650Sstevel@tonic-gate  * Get the real-time scheduling parameters of the thread pointed to by
5660Sstevel@tonic-gate  * rtprocp into the buffer pointed to by rtkparmsp.
5670Sstevel@tonic-gate  */
5680Sstevel@tonic-gate static void
rt_parmsget(kthread_t * t,void * parmsp)5690Sstevel@tonic-gate rt_parmsget(kthread_t *t, void *parmsp)
5700Sstevel@tonic-gate {
5710Sstevel@tonic-gate 	rtproc_t	*rtprocp = (rtproc_t *)t->t_cldata;
5720Sstevel@tonic-gate 	rtkparms_t	*rtkparmsp = (rtkparms_t *)parmsp;
5730Sstevel@tonic-gate 
5740Sstevel@tonic-gate 	rtkparmsp->rt_pri = rtprocp->rt_pri;
5750Sstevel@tonic-gate 	rtkparmsp->rt_tqntm = rtprocp->rt_pquantum;
5760Sstevel@tonic-gate 	rtkparmsp->rt_tqsig = rtprocp->rt_tqsignal;
5770Sstevel@tonic-gate }
5780Sstevel@tonic-gate 
5790Sstevel@tonic-gate 
5800Sstevel@tonic-gate 
5810Sstevel@tonic-gate /*
5820Sstevel@tonic-gate  * Check the validity of the real-time parameters in the buffer
5830Sstevel@tonic-gate  * pointed to by rtprmsp.
5840Sstevel@tonic-gate  * We convert the rtparms buffer from the user supplied format to
5850Sstevel@tonic-gate  * our internal format (i.e. time quantum expressed in ticks).
5860Sstevel@tonic-gate  */
5870Sstevel@tonic-gate static int
rt_parmsin(void * prmsp)5880Sstevel@tonic-gate rt_parmsin(void *prmsp)
5890Sstevel@tonic-gate {
5900Sstevel@tonic-gate 	rtparms_t *rtprmsp = (rtparms_t *)prmsp;
5910Sstevel@tonic-gate 	longlong_t	ticks;
5920Sstevel@tonic-gate 	uint_t		cflags;
5930Sstevel@tonic-gate 
5940Sstevel@tonic-gate 	/*
5950Sstevel@tonic-gate 	 * First check the validity of parameters and convert
5960Sstevel@tonic-gate 	 * the buffer to kernel format.
5970Sstevel@tonic-gate 	 */
5980Sstevel@tonic-gate 	if ((rtprmsp->rt_pri < 0 || rtprmsp->rt_pri > rt_maxpri) &&
5990Sstevel@tonic-gate 	    rtprmsp->rt_pri != RT_NOCHANGE)
6000Sstevel@tonic-gate 		return (EINVAL);
6010Sstevel@tonic-gate 
6020Sstevel@tonic-gate 	cflags = (rtprmsp->rt_pri != RT_NOCHANGE ? RT_DOPRI : 0);
6030Sstevel@tonic-gate 
6040Sstevel@tonic-gate 	if ((rtprmsp->rt_tqsecs == 0 && rtprmsp->rt_tqnsecs == 0) ||
6050Sstevel@tonic-gate 	    rtprmsp->rt_tqnsecs >= NANOSEC)
6060Sstevel@tonic-gate 		return (EINVAL);
6070Sstevel@tonic-gate 
6080Sstevel@tonic-gate 	if (rtprmsp->rt_tqnsecs != RT_NOCHANGE)
6090Sstevel@tonic-gate 		cflags |= RT_DOTQ;
6100Sstevel@tonic-gate 
6110Sstevel@tonic-gate 	if (rtprmsp->rt_tqnsecs >= 0) {
6120Sstevel@tonic-gate 		if ((ticks = SEC_TO_TICK((longlong_t)rtprmsp->rt_tqsecs) +
6130Sstevel@tonic-gate 		    NSEC_TO_TICK_ROUNDUP(rtprmsp->rt_tqnsecs)) > INT_MAX)
6140Sstevel@tonic-gate 			return (ERANGE);
6150Sstevel@tonic-gate 
6160Sstevel@tonic-gate 		((rtkparms_t *)rtprmsp)->rt_tqntm = (int)ticks;
6170Sstevel@tonic-gate 	} else {
6180Sstevel@tonic-gate 		if (rtprmsp->rt_tqnsecs != RT_NOCHANGE &&
6190Sstevel@tonic-gate 		    rtprmsp->rt_tqnsecs != RT_TQINF &&
6200Sstevel@tonic-gate 		    rtprmsp->rt_tqnsecs != RT_TQDEF)
6210Sstevel@tonic-gate 			return (EINVAL);
6220Sstevel@tonic-gate 
6230Sstevel@tonic-gate 		((rtkparms_t *)rtprmsp)->rt_tqntm = rtprmsp->rt_tqnsecs;
6240Sstevel@tonic-gate 	}
6250Sstevel@tonic-gate 	((rtkparms_t *)rtprmsp)->rt_cflags = cflags;
6260Sstevel@tonic-gate 
6270Sstevel@tonic-gate 	return (0);
6280Sstevel@tonic-gate }
6290Sstevel@tonic-gate 
6300Sstevel@tonic-gate 
6310Sstevel@tonic-gate /*
6320Sstevel@tonic-gate  * Check the validity of the real-time parameters in the pc_vaparms_t
6330Sstevel@tonic-gate  * structure vaparmsp and put them in the buffer pointed to by rtprmsp.
6340Sstevel@tonic-gate  * pc_vaparms_t contains (key, value) pairs of parameter.
6350Sstevel@tonic-gate  * rt_vaparmsin() is the variable parameter version of rt_parmsin().
6360Sstevel@tonic-gate  */
6370Sstevel@tonic-gate static int
rt_vaparmsin(void * prmsp,pc_vaparms_t * vaparmsp)6380Sstevel@tonic-gate rt_vaparmsin(void *prmsp, pc_vaparms_t *vaparmsp)
6390Sstevel@tonic-gate {
6400Sstevel@tonic-gate 	uint_t		secs = 0;
6410Sstevel@tonic-gate 	uint_t		cnt;
6420Sstevel@tonic-gate 	int		nsecs = 0;
6430Sstevel@tonic-gate 	int		priflag, secflag, nsecflag, sigflag;
6440Sstevel@tonic-gate 	longlong_t	ticks;
6450Sstevel@tonic-gate 	rtkparms_t	*rtprmsp = (rtkparms_t *)prmsp;
6460Sstevel@tonic-gate 	pc_vaparm_t	*vpp = &vaparmsp->pc_parms[0];
6470Sstevel@tonic-gate 
6480Sstevel@tonic-gate 
6490Sstevel@tonic-gate 	/*
6500Sstevel@tonic-gate 	 * First check the validity of parameters and convert them
6510Sstevel@tonic-gate 	 * from the user supplied format to the internal format.
6520Sstevel@tonic-gate 	 */
6530Sstevel@tonic-gate 	priflag = secflag = nsecflag = sigflag = 0;
6540Sstevel@tonic-gate 	rtprmsp->rt_cflags = 0;
6550Sstevel@tonic-gate 
6560Sstevel@tonic-gate 	if (vaparmsp->pc_vaparmscnt > PC_VAPARMCNT)
6570Sstevel@tonic-gate 		return (EINVAL);
6580Sstevel@tonic-gate 
6590Sstevel@tonic-gate 	for (cnt = 0; cnt < vaparmsp->pc_vaparmscnt; cnt++, vpp++) {
6600Sstevel@tonic-gate 
6610Sstevel@tonic-gate 		switch (vpp->pc_key) {
6620Sstevel@tonic-gate 		case RT_KY_PRI:
6630Sstevel@tonic-gate 			if (priflag++)
6640Sstevel@tonic-gate 				return (EINVAL);
6650Sstevel@tonic-gate 			rtprmsp->rt_cflags |= RT_DOPRI;
6660Sstevel@tonic-gate 			rtprmsp->rt_pri = (pri_t)vpp->pc_parm;
6670Sstevel@tonic-gate 			if (rtprmsp->rt_pri < 0 || rtprmsp->rt_pri > rt_maxpri)
6680Sstevel@tonic-gate 				return (EINVAL);
6690Sstevel@tonic-gate 			break;
6700Sstevel@tonic-gate 
6710Sstevel@tonic-gate 		case RT_KY_TQSECS:
6720Sstevel@tonic-gate 			if (secflag++)
6730Sstevel@tonic-gate 				return (EINVAL);
6740Sstevel@tonic-gate 			rtprmsp->rt_cflags |= RT_DOTQ;
6750Sstevel@tonic-gate 			secs = (uint_t)vpp->pc_parm;
6760Sstevel@tonic-gate 			break;
6770Sstevel@tonic-gate 
6780Sstevel@tonic-gate 		case RT_KY_TQNSECS:
6790Sstevel@tonic-gate 			if (nsecflag++)
6800Sstevel@tonic-gate 				return (EINVAL);
6810Sstevel@tonic-gate 			rtprmsp->rt_cflags |= RT_DOTQ;
6820Sstevel@tonic-gate 			nsecs = (int)vpp->pc_parm;
6830Sstevel@tonic-gate 			break;
6840Sstevel@tonic-gate 
6850Sstevel@tonic-gate 		case RT_KY_TQSIG:
6860Sstevel@tonic-gate 			if (sigflag++)
6870Sstevel@tonic-gate 				return (EINVAL);
6880Sstevel@tonic-gate 			rtprmsp->rt_cflags |= RT_DOSIG;
6890Sstevel@tonic-gate 			rtprmsp->rt_tqsig = (int)vpp->pc_parm;
6900Sstevel@tonic-gate 			if (rtprmsp->rt_tqsig < 0 || rtprmsp->rt_tqsig >= NSIG)
6910Sstevel@tonic-gate 				return (EINVAL);
6920Sstevel@tonic-gate 			break;
6930Sstevel@tonic-gate 
6940Sstevel@tonic-gate 		default:
6950Sstevel@tonic-gate 			return (EINVAL);
6960Sstevel@tonic-gate 		}
6970Sstevel@tonic-gate 	}
6980Sstevel@tonic-gate 
6990Sstevel@tonic-gate 	if (vaparmsp->pc_vaparmscnt == 0) {
7000Sstevel@tonic-gate 		/*
7010Sstevel@tonic-gate 		 * Use default parameters.
7020Sstevel@tonic-gate 		 */
7030Sstevel@tonic-gate 		rtprmsp->rt_pri = 0;
7040Sstevel@tonic-gate 		rtprmsp->rt_tqntm = RT_TQDEF;
7050Sstevel@tonic-gate 		rtprmsp->rt_tqsig = 0;
7060Sstevel@tonic-gate 		rtprmsp->rt_cflags = RT_DOPRI | RT_DOTQ | RT_DOSIG;
7070Sstevel@tonic-gate 	} else if ((rtprmsp->rt_cflags & RT_DOTQ) != 0) {
7080Sstevel@tonic-gate 		if ((secs == 0 && nsecs == 0) || nsecs >= NANOSEC)
7090Sstevel@tonic-gate 			return (EINVAL);
7100Sstevel@tonic-gate 
7110Sstevel@tonic-gate 		if (nsecs >= 0) {
7120Sstevel@tonic-gate 			if ((ticks = SEC_TO_TICK((longlong_t)secs) +
7130Sstevel@tonic-gate 			    NSEC_TO_TICK_ROUNDUP(nsecs)) > INT_MAX)
7140Sstevel@tonic-gate 				return (ERANGE);
7150Sstevel@tonic-gate 
7160Sstevel@tonic-gate 			rtprmsp->rt_tqntm = (int)ticks;
7170Sstevel@tonic-gate 		} else {
7180Sstevel@tonic-gate 			if (nsecs != RT_TQINF && nsecs != RT_TQDEF)
7190Sstevel@tonic-gate 				return (EINVAL);
7200Sstevel@tonic-gate 			rtprmsp->rt_tqntm = nsecs;
7210Sstevel@tonic-gate 		}
7220Sstevel@tonic-gate 	}
7230Sstevel@tonic-gate 
7240Sstevel@tonic-gate 	return (0);
7250Sstevel@tonic-gate }
7260Sstevel@tonic-gate 
7270Sstevel@tonic-gate /*
7280Sstevel@tonic-gate  * Do required processing on the real-time parameter buffer
7290Sstevel@tonic-gate  * before it is copied out to the user.
7300Sstevel@tonic-gate  * All we have to do is convert the buffer from kernel to user format
7310Sstevel@tonic-gate  * (i.e. convert time quantum from ticks to seconds-nanoseconds).
7320Sstevel@tonic-gate  */
7330Sstevel@tonic-gate /* ARGSUSED */
7340Sstevel@tonic-gate static int
rt_parmsout(void * prmsp,pc_vaparms_t * vaparmsp)7350Sstevel@tonic-gate rt_parmsout(void *prmsp, pc_vaparms_t *vaparmsp)
7360Sstevel@tonic-gate {
7370Sstevel@tonic-gate 	rtkparms_t	*rtkprmsp = (rtkparms_t *)prmsp;
7380Sstevel@tonic-gate 
7390Sstevel@tonic-gate 	if (vaparmsp != NULL)
7400Sstevel@tonic-gate 		return (0);
7410Sstevel@tonic-gate 
7420Sstevel@tonic-gate 	if (rtkprmsp->rt_tqntm < 0) {
7430Sstevel@tonic-gate 		/*
7440Sstevel@tonic-gate 		 * Quantum field set to special value (e.g. RT_TQINF)
7450Sstevel@tonic-gate 		 */
7460Sstevel@tonic-gate 		((rtparms_t *)rtkprmsp)->rt_tqnsecs = rtkprmsp->rt_tqntm;
7470Sstevel@tonic-gate 		((rtparms_t *)rtkprmsp)->rt_tqsecs = 0;
7480Sstevel@tonic-gate 	} else {
7490Sstevel@tonic-gate 		/* Convert quantum from ticks to seconds-nanoseconds */
7500Sstevel@tonic-gate 
7510Sstevel@tonic-gate 		timestruc_t ts;
7520Sstevel@tonic-gate 		TICK_TO_TIMESTRUC(rtkprmsp->rt_tqntm, &ts);
7530Sstevel@tonic-gate 		((rtparms_t *)rtkprmsp)->rt_tqsecs = ts.tv_sec;
7540Sstevel@tonic-gate 		((rtparms_t *)rtkprmsp)->rt_tqnsecs = ts.tv_nsec;
7550Sstevel@tonic-gate 	}
7560Sstevel@tonic-gate 
7570Sstevel@tonic-gate 	return (0);
7580Sstevel@tonic-gate }
7590Sstevel@tonic-gate 
7600Sstevel@tonic-gate 
7610Sstevel@tonic-gate /*
7620Sstevel@tonic-gate  * Copy all selected real-time class parameters to the user.
7630Sstevel@tonic-gate  * The parameters are specified by a key.
7640Sstevel@tonic-gate  */
7650Sstevel@tonic-gate static int
rt_vaparmsout(void * prmsp,pc_vaparms_t * vaparmsp)7660Sstevel@tonic-gate rt_vaparmsout(void *prmsp, pc_vaparms_t *vaparmsp)
7670Sstevel@tonic-gate {
7680Sstevel@tonic-gate 	rtkparms_t	*rtkprmsp = (rtkparms_t *)prmsp;
7690Sstevel@tonic-gate 	timestruc_t	ts;
7700Sstevel@tonic-gate 	uint_t		cnt;
7710Sstevel@tonic-gate 	uint_t		secs;
7720Sstevel@tonic-gate 	int		nsecs;
7730Sstevel@tonic-gate 	int		priflag, secflag, nsecflag, sigflag;
7740Sstevel@tonic-gate 	pc_vaparm_t	*vpp = &vaparmsp->pc_parms[0];
7750Sstevel@tonic-gate 
7760Sstevel@tonic-gate 	ASSERT(MUTEX_NOT_HELD(&curproc->p_lock));
7770Sstevel@tonic-gate 
7780Sstevel@tonic-gate 	priflag = secflag = nsecflag = sigflag = 0;
7790Sstevel@tonic-gate 
7800Sstevel@tonic-gate 	if (vaparmsp->pc_vaparmscnt > PC_VAPARMCNT)
7810Sstevel@tonic-gate 		return (EINVAL);
7820Sstevel@tonic-gate 
7830Sstevel@tonic-gate 	if (rtkprmsp->rt_tqntm < 0) {
7840Sstevel@tonic-gate 		/*
7850Sstevel@tonic-gate 		 * Quantum field set to special value (e.g. RT_TQINF).
7860Sstevel@tonic-gate 		 */
7870Sstevel@tonic-gate 		secs = 0;
7880Sstevel@tonic-gate 		nsecs = rtkprmsp->rt_tqntm;
7890Sstevel@tonic-gate 	} else {
7900Sstevel@tonic-gate 		/*
7910Sstevel@tonic-gate 		 * Convert quantum from ticks to seconds-nanoseconds.
7920Sstevel@tonic-gate 		 */
7930Sstevel@tonic-gate 		TICK_TO_TIMESTRUC(rtkprmsp->rt_tqntm, &ts);
7940Sstevel@tonic-gate 		secs = ts.tv_sec;
7950Sstevel@tonic-gate 		nsecs = ts.tv_nsec;
7960Sstevel@tonic-gate 	}
7970Sstevel@tonic-gate 
7980Sstevel@tonic-gate 
7990Sstevel@tonic-gate 	for (cnt = 0; cnt < vaparmsp->pc_vaparmscnt; cnt++, vpp++) {
8000Sstevel@tonic-gate 
8010Sstevel@tonic-gate 		switch (vpp->pc_key) {
8020Sstevel@tonic-gate 		case RT_KY_PRI:
8030Sstevel@tonic-gate 			if (priflag++)
8040Sstevel@tonic-gate 				return (EINVAL);
8050Sstevel@tonic-gate 			if (copyout(&rtkprmsp->rt_pri,
8060Sstevel@tonic-gate 			    (caddr_t)(uintptr_t)vpp->pc_parm, sizeof (pri_t)))
8070Sstevel@tonic-gate 				return (EFAULT);
8080Sstevel@tonic-gate 			break;
8090Sstevel@tonic-gate 
8100Sstevel@tonic-gate 		case RT_KY_TQSECS:
8110Sstevel@tonic-gate 			if (secflag++)
8120Sstevel@tonic-gate 				return (EINVAL);
8130Sstevel@tonic-gate 			if (copyout(&secs, (caddr_t)(uintptr_t)vpp->pc_parm,
8140Sstevel@tonic-gate 			    sizeof (uint_t)))
8150Sstevel@tonic-gate 				return (EFAULT);
8160Sstevel@tonic-gate 			break;
8170Sstevel@tonic-gate 
8180Sstevel@tonic-gate 		case RT_KY_TQNSECS:
8190Sstevel@tonic-gate 			if (nsecflag++)
8200Sstevel@tonic-gate 				return (EINVAL);
8210Sstevel@tonic-gate 			if (copyout(&nsecs, (caddr_t)(uintptr_t)vpp->pc_parm,
8220Sstevel@tonic-gate 			    sizeof (int)))
8230Sstevel@tonic-gate 				return (EFAULT);
8240Sstevel@tonic-gate 			break;
8250Sstevel@tonic-gate 
8260Sstevel@tonic-gate 		case RT_KY_TQSIG:
8270Sstevel@tonic-gate 			if (sigflag++)
8280Sstevel@tonic-gate 				return (EINVAL);
8290Sstevel@tonic-gate 			if (copyout(&rtkprmsp->rt_tqsig,
8300Sstevel@tonic-gate 			    (caddr_t)(uintptr_t)vpp->pc_parm, sizeof (int)))
8310Sstevel@tonic-gate 				return (EFAULT);
8320Sstevel@tonic-gate 			break;
8330Sstevel@tonic-gate 
8340Sstevel@tonic-gate 		default:
8350Sstevel@tonic-gate 			return (EINVAL);
8360Sstevel@tonic-gate 		}
8370Sstevel@tonic-gate 	}
8380Sstevel@tonic-gate 
8390Sstevel@tonic-gate 	return (0);
8400Sstevel@tonic-gate }
8410Sstevel@tonic-gate 
8420Sstevel@tonic-gate 
8430Sstevel@tonic-gate /*
8440Sstevel@tonic-gate  * Set the scheduling parameters of the thread pointed to by rtprocp
8450Sstevel@tonic-gate  * to those specified in the buffer pointed to by rtkprmsp.
8460Sstevel@tonic-gate  * Note that the parameters are expected to be in kernel format
8470Sstevel@tonic-gate  * (i.e. time quantm expressed in ticks).  Real time parameters copied
8480Sstevel@tonic-gate  * in from the user should be processed by rt_parmsin() before they are
8490Sstevel@tonic-gate  * passed to this function.
8500Sstevel@tonic-gate  */
8510Sstevel@tonic-gate static int
rt_parmsset(kthread_t * tx,void * prmsp,id_t reqpcid,cred_t * reqpcredp)8520Sstevel@tonic-gate rt_parmsset(kthread_t *tx, void *prmsp, id_t reqpcid, cred_t *reqpcredp)
8530Sstevel@tonic-gate {
8540Sstevel@tonic-gate 	rtkparms_t *rtkprmsp = (rtkparms_t *)prmsp;
8550Sstevel@tonic-gate 	rtproc_t *rtpp = (rtproc_t *)tx->t_cldata;
8560Sstevel@tonic-gate 
8570Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&(ttoproc(tx))->p_lock));
8580Sstevel@tonic-gate 
8590Sstevel@tonic-gate 	/*
8600Sstevel@tonic-gate 	 * Basic permissions enforced by generic kernel code
8610Sstevel@tonic-gate 	 * for all classes require that a thread attempting
8620Sstevel@tonic-gate 	 * to change the scheduling parameters of a target thread
8630Sstevel@tonic-gate 	 * be privileged or have a real or effective UID
8640Sstevel@tonic-gate 	 * matching that of the target thread. We are not
8650Sstevel@tonic-gate 	 * called unless these basic permission checks have
8660Sstevel@tonic-gate 	 * already passed. The real-time class requires in addition
8670Sstevel@tonic-gate 	 * that the requesting thread be real-time unless it is privileged.
8680Sstevel@tonic-gate 	 * This may also have been checked previously but if our caller
8690Sstevel@tonic-gate 	 * passes us a credential structure we assume it hasn't and
8700Sstevel@tonic-gate 	 * we check it here.
8710Sstevel@tonic-gate 	 */
8720Sstevel@tonic-gate 	if (reqpcredp != NULL && reqpcid != rt_cid &&
8730Sstevel@tonic-gate 	    secpolicy_setpriority(reqpcredp) != 0)
8740Sstevel@tonic-gate 		return (EPERM);
8750Sstevel@tonic-gate 
8760Sstevel@tonic-gate 	thread_lock(tx);
8770Sstevel@tonic-gate 	if ((rtkprmsp->rt_cflags & RT_DOPRI) != 0) {
8780Sstevel@tonic-gate 		rtpp->rt_pri = rtkprmsp->rt_pri;
8790Sstevel@tonic-gate 		rt_change_priority(tx, rtpp);
8800Sstevel@tonic-gate 	}
8810Sstevel@tonic-gate 	if (rtkprmsp->rt_tqntm == RT_TQINF)
8820Sstevel@tonic-gate 		rtpp->rt_pquantum = RT_TQINF;
8830Sstevel@tonic-gate 	else if (rtkprmsp->rt_tqntm == RT_TQDEF)
8840Sstevel@tonic-gate 		rtpp->rt_timeleft = rtpp->rt_pquantum =
8850Sstevel@tonic-gate 		    rt_dptbl[rtpp->rt_pri].rt_quantum;
8860Sstevel@tonic-gate 	else if ((rtkprmsp->rt_cflags & RT_DOTQ) != 0)
8870Sstevel@tonic-gate 		rtpp->rt_timeleft = rtpp->rt_pquantum = rtkprmsp->rt_tqntm;
8880Sstevel@tonic-gate 
8890Sstevel@tonic-gate 	if ((rtkprmsp->rt_cflags & RT_DOSIG) != 0)
8900Sstevel@tonic-gate 		rtpp->rt_tqsignal = rtkprmsp->rt_tqsig;
8910Sstevel@tonic-gate 
8920Sstevel@tonic-gate 	thread_unlock(tx);
8930Sstevel@tonic-gate 	return (0);
8940Sstevel@tonic-gate }
8950Sstevel@tonic-gate 
8960Sstevel@tonic-gate 
8970Sstevel@tonic-gate /*
8980Sstevel@tonic-gate  * Arrange for thread to be placed in appropriate location
8990Sstevel@tonic-gate  * on dispatcher queue.  Runs at splhi() since the clock
9000Sstevel@tonic-gate  * interrupt can cause RTBACKQ to be set.
9010Sstevel@tonic-gate  */
9020Sstevel@tonic-gate static void
rt_preempt(kthread_t * t)9030Sstevel@tonic-gate rt_preempt(kthread_t *t)
9040Sstevel@tonic-gate {
9050Sstevel@tonic-gate 	rtproc_t *rtpp = (rtproc_t *)(t->t_cldata);
9060Sstevel@tonic-gate 	klwp_t *lwp;
9070Sstevel@tonic-gate 
9080Sstevel@tonic-gate 	ASSERT(THREAD_LOCK_HELD(t));
9090Sstevel@tonic-gate 
9100Sstevel@tonic-gate 	/*
9110Sstevel@tonic-gate 	 * If the state is user I allow swapping because I know I won't
9120Sstevel@tonic-gate 	 * be holding any locks.
9130Sstevel@tonic-gate 	 */
9140Sstevel@tonic-gate 	if ((lwp = curthread->t_lwp) != NULL && lwp->lwp_state == LWP_USER)
9150Sstevel@tonic-gate 		t->t_schedflag &= ~TS_DONT_SWAP;
9160Sstevel@tonic-gate 	if ((rtpp->rt_flags & RTBACKQ) != 0) {
9170Sstevel@tonic-gate 		rtpp->rt_timeleft = rtpp->rt_pquantum;
9180Sstevel@tonic-gate 		rtpp->rt_flags &= ~RTBACKQ;
9190Sstevel@tonic-gate 		setbackdq(t);
9200Sstevel@tonic-gate 	} else
9210Sstevel@tonic-gate 		setfrontdq(t);
9220Sstevel@tonic-gate 
9230Sstevel@tonic-gate }
9240Sstevel@tonic-gate 
9250Sstevel@tonic-gate /*
9260Sstevel@tonic-gate  * Return the global priority associated with this rt_pri.
9270Sstevel@tonic-gate  */
9280Sstevel@tonic-gate static pri_t
rt_globpri(kthread_t * t)9290Sstevel@tonic-gate rt_globpri(kthread_t *t)
9300Sstevel@tonic-gate {
9310Sstevel@tonic-gate 	rtproc_t *rtprocp = (rtproc_t *)t->t_cldata;
9320Sstevel@tonic-gate 	return (rt_dptbl[rtprocp->rt_pri].rt_globpri);
9330Sstevel@tonic-gate }
9340Sstevel@tonic-gate 
9350Sstevel@tonic-gate static void
rt_setrun(kthread_t * t)9360Sstevel@tonic-gate rt_setrun(kthread_t *t)
9370Sstevel@tonic-gate {
9380Sstevel@tonic-gate 	rtproc_t *rtpp = (rtproc_t *)(t->t_cldata);
9390Sstevel@tonic-gate 
9400Sstevel@tonic-gate 	ASSERT(THREAD_LOCK_HELD(t));
9410Sstevel@tonic-gate 
9420Sstevel@tonic-gate 	rtpp->rt_timeleft = rtpp->rt_pquantum;
9430Sstevel@tonic-gate 	rtpp->rt_flags &= ~RTBACKQ;
9440Sstevel@tonic-gate 	setbackdq(t);
9450Sstevel@tonic-gate }
9460Sstevel@tonic-gate 
9470Sstevel@tonic-gate /*
9480Sstevel@tonic-gate  * Returns the priority of the thread, -1 if the thread is loaded or ineligible
9490Sstevel@tonic-gate  * for swapin.
9500Sstevel@tonic-gate  *
9510Sstevel@tonic-gate  * FX and RT threads are designed so that they don't swapout; however, it
9520Sstevel@tonic-gate  * is possible that while the thread is swapped out and in another class, it
9530Sstevel@tonic-gate  * can be changed to FX or RT.  Since these threads should be swapped in as
9540Sstevel@tonic-gate  * soon as they're runnable, rt_swapin returns SHRT_MAX, and fx_swapin
9550Sstevel@tonic-gate  * returns SHRT_MAX - 1, so that it gives deference to any swapped out RT
9560Sstevel@tonic-gate  * threads.
9570Sstevel@tonic-gate  */
9580Sstevel@tonic-gate /* ARGSUSED */
9590Sstevel@tonic-gate static pri_t
rt_swapin(kthread_t * t,int flags)9600Sstevel@tonic-gate rt_swapin(kthread_t *t, int flags)
9610Sstevel@tonic-gate {
9620Sstevel@tonic-gate 	pri_t	tpri = -1;
9630Sstevel@tonic-gate 
9640Sstevel@tonic-gate 	ASSERT(THREAD_LOCK_HELD(t));
9650Sstevel@tonic-gate 
9660Sstevel@tonic-gate 	if (t->t_state == TS_RUN && (t->t_schedflag & TS_LOAD) == 0) {
9670Sstevel@tonic-gate 		tpri = (pri_t)SHRT_MAX;
9680Sstevel@tonic-gate 	}
9690Sstevel@tonic-gate 
9700Sstevel@tonic-gate 	return (tpri);
9710Sstevel@tonic-gate }
9720Sstevel@tonic-gate 
9730Sstevel@tonic-gate /*
9740Sstevel@tonic-gate  * Return an effective priority for swapout.
9750Sstevel@tonic-gate  */
9760Sstevel@tonic-gate /* ARGSUSED */
9770Sstevel@tonic-gate static pri_t
rt_swapout(kthread_t * t,int flags)9780Sstevel@tonic-gate rt_swapout(kthread_t *t, int flags)
9790Sstevel@tonic-gate {
9800Sstevel@tonic-gate 	ASSERT(THREAD_LOCK_HELD(t));
9810Sstevel@tonic-gate 
9820Sstevel@tonic-gate 	return (-1);
9830Sstevel@tonic-gate }
9840Sstevel@tonic-gate 
9850Sstevel@tonic-gate /*
9860Sstevel@tonic-gate  * Check for time slice expiration (unless thread has infinite time
9870Sstevel@tonic-gate  * slice).  If time slice has expired arrange for thread to be preempted
9880Sstevel@tonic-gate  * and placed on back of queue.
9890Sstevel@tonic-gate  */
9900Sstevel@tonic-gate static void
rt_tick(kthread_t * t)9910Sstevel@tonic-gate rt_tick(kthread_t *t)
9920Sstevel@tonic-gate {
9930Sstevel@tonic-gate 	rtproc_t *rtpp = (rtproc_t *)(t->t_cldata);
9940Sstevel@tonic-gate 
9950Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&(ttoproc(t))->p_lock));
9960Sstevel@tonic-gate 
9970Sstevel@tonic-gate 	thread_lock(t);
9980Sstevel@tonic-gate 	if ((rtpp->rt_pquantum != RT_TQINF && --rtpp->rt_timeleft == 0) ||
999477Smishra 	    (t->t_state == TS_ONPROC && DISP_MUST_SURRENDER(t))) {
10000Sstevel@tonic-gate 		if (rtpp->rt_timeleft == 0 && rtpp->rt_tqsignal) {
10010Sstevel@tonic-gate 			thread_unlock(t);
10020Sstevel@tonic-gate 			sigtoproc(ttoproc(t), t, rtpp->rt_tqsignal);
10030Sstevel@tonic-gate 			thread_lock(t);
10040Sstevel@tonic-gate 		}
10050Sstevel@tonic-gate 		rtpp->rt_flags |= RTBACKQ;
10060Sstevel@tonic-gate 		cpu_surrender(t);
10070Sstevel@tonic-gate 	}
10080Sstevel@tonic-gate 	thread_unlock(t);
10090Sstevel@tonic-gate }
10100Sstevel@tonic-gate 
10110Sstevel@tonic-gate 
10120Sstevel@tonic-gate /*
10130Sstevel@tonic-gate  * Place the thread waking up on the dispatcher queue.
10140Sstevel@tonic-gate  */
10150Sstevel@tonic-gate static void
rt_wakeup(kthread_t * t)10160Sstevel@tonic-gate rt_wakeup(kthread_t *t)
10170Sstevel@tonic-gate {
10180Sstevel@tonic-gate 	rtproc_t *rtpp = (rtproc_t *)(t->t_cldata);
10190Sstevel@tonic-gate 
10200Sstevel@tonic-gate 	ASSERT(THREAD_LOCK_HELD(t));
10210Sstevel@tonic-gate 
10220Sstevel@tonic-gate 	rtpp->rt_timeleft = rtpp->rt_pquantum;
10230Sstevel@tonic-gate 	rtpp->rt_flags &= ~RTBACKQ;
10240Sstevel@tonic-gate 	setbackdq(t);
10250Sstevel@tonic-gate }
10260Sstevel@tonic-gate 
10270Sstevel@tonic-gate static void
rt_yield(kthread_t * t)10280Sstevel@tonic-gate rt_yield(kthread_t *t)
10290Sstevel@tonic-gate {
10300Sstevel@tonic-gate 	rtproc_t *rtpp = (rtproc_t *)(t->t_cldata);
10310Sstevel@tonic-gate 
10320Sstevel@tonic-gate 	ASSERT(t == curthread);
10330Sstevel@tonic-gate 	ASSERT(THREAD_LOCK_HELD(t));
10340Sstevel@tonic-gate 
10350Sstevel@tonic-gate 	rtpp->rt_flags &= ~RTBACKQ;
10360Sstevel@tonic-gate 	setbackdq(t);
10370Sstevel@tonic-gate }
10380Sstevel@tonic-gate 
10390Sstevel@tonic-gate /* ARGSUSED */
10400Sstevel@tonic-gate static int
rt_donice(kthread_t * t,cred_t * cr,int incr,int * retvalp)10410Sstevel@tonic-gate rt_donice(kthread_t *t, cred_t *cr, int incr, int *retvalp)
10420Sstevel@tonic-gate {
10430Sstevel@tonic-gate 	return (EINVAL);
10440Sstevel@tonic-gate }
10450Sstevel@tonic-gate 
1046*6247Sraf /*
1047*6247Sraf  * Increment the priority of the specified thread by incr and
1048*6247Sraf  * return the new value in *retvalp.
1049*6247Sraf  */
1050*6247Sraf static int
rt_doprio(kthread_t * t,cred_t * cr,int incr,int * retvalp)1051*6247Sraf rt_doprio(kthread_t *t, cred_t *cr, int incr, int *retvalp)
1052*6247Sraf {
1053*6247Sraf 	int newpri;
1054*6247Sraf 	rtproc_t *rtpp = (rtproc_t *)(t->t_cldata);
1055*6247Sraf 	rtkparms_t rtkparms;
1056*6247Sraf 
1057*6247Sraf 	/* If there's no change to the priority, just return current setting */
1058*6247Sraf 	if (incr == 0) {
1059*6247Sraf 		*retvalp = rtpp->rt_pri;
1060*6247Sraf 		return (0);
1061*6247Sraf 	}
1062*6247Sraf 
1063*6247Sraf 	newpri = rtpp->rt_pri + incr;
1064*6247Sraf 	if (newpri > rt_maxpri || newpri < 0)
1065*6247Sraf 		return (EINVAL);
1066*6247Sraf 
1067*6247Sraf 	*retvalp = newpri;
1068*6247Sraf 	rtkparms.rt_pri = newpri;
1069*6247Sraf 	rtkparms.rt_tqntm = RT_NOCHANGE;
1070*6247Sraf 	rtkparms.rt_tqsig = 0;
1071*6247Sraf 	rtkparms.rt_cflags = RT_DOPRI;
1072*6247Sraf 	return (rt_parmsset(t, &rtkparms, rt_cid, cr));
1073*6247Sraf }
1074*6247Sraf 
10750Sstevel@tonic-gate static int
rt_alloc(void ** p,int flag)10760Sstevel@tonic-gate rt_alloc(void **p, int flag)
10770Sstevel@tonic-gate {
10780Sstevel@tonic-gate 	void *bufp;
10790Sstevel@tonic-gate 	bufp = kmem_alloc(sizeof (rtproc_t), flag);
10800Sstevel@tonic-gate 	if (bufp == NULL) {
10810Sstevel@tonic-gate 		return (ENOMEM);
10820Sstevel@tonic-gate 	} else {
10830Sstevel@tonic-gate 		*p = bufp;
10840Sstevel@tonic-gate 		return (0);
10850Sstevel@tonic-gate 	}
10860Sstevel@tonic-gate }
10870Sstevel@tonic-gate 
10880Sstevel@tonic-gate static void
rt_free(void * bufp)10890Sstevel@tonic-gate rt_free(void *bufp)
10900Sstevel@tonic-gate {
10910Sstevel@tonic-gate 	if (bufp)
10920Sstevel@tonic-gate 		kmem_free(bufp, sizeof (rtproc_t));
10930Sstevel@tonic-gate }
10940Sstevel@tonic-gate 
10950Sstevel@tonic-gate static void
rt_change_priority(kthread_t * t,rtproc_t * rtpp)10960Sstevel@tonic-gate rt_change_priority(kthread_t *t, rtproc_t *rtpp)
10970Sstevel@tonic-gate {
10980Sstevel@tonic-gate 	pri_t new_pri;
10990Sstevel@tonic-gate 
11000Sstevel@tonic-gate 	ASSERT(THREAD_LOCK_HELD(t));
11010Sstevel@tonic-gate 
11020Sstevel@tonic-gate 	new_pri = rt_dptbl[rtpp->rt_pri].rt_globpri;
11030Sstevel@tonic-gate 
1104*6247Sraf 	t->t_cpri = rtpp->rt_pri;
11050Sstevel@tonic-gate 	if (t == curthread || t->t_state == TS_ONPROC) {
11060Sstevel@tonic-gate 		cpu_t	*cp = t->t_disp_queue->disp_cpu;
11070Sstevel@tonic-gate 		THREAD_CHANGE_PRI(t, new_pri);
11080Sstevel@tonic-gate 		if (t == cp->cpu_dispthread)
11090Sstevel@tonic-gate 			cp->cpu_dispatch_pri = DISP_PRIO(t);
11100Sstevel@tonic-gate 		if (DISP_MUST_SURRENDER(t)) {
11110Sstevel@tonic-gate 			rtpp->rt_flags |= RTBACKQ;
11120Sstevel@tonic-gate 			cpu_surrender(t);
11130Sstevel@tonic-gate 		} else {
11140Sstevel@tonic-gate 			rtpp->rt_timeleft = rtpp->rt_pquantum;
11150Sstevel@tonic-gate 		}
11160Sstevel@tonic-gate 	} else {
11170Sstevel@tonic-gate 		/*
11180Sstevel@tonic-gate 		 * When the priority of a thread is changed,
11190Sstevel@tonic-gate 		 * it may be necessary to adjust its position
11200Sstevel@tonic-gate 		 * on a sleep queue or dispatch queue.  The
11210Sstevel@tonic-gate 		 * function thread_change_pri() accomplishes this.
11220Sstevel@tonic-gate 		 */
11230Sstevel@tonic-gate 		if (thread_change_pri(t, new_pri, 0)) {
11240Sstevel@tonic-gate 			/*
11250Sstevel@tonic-gate 			 * The thread was on a run queue.
11260Sstevel@tonic-gate 			 * Reset its CPU timeleft.
11270Sstevel@tonic-gate 			 */
11280Sstevel@tonic-gate 			rtpp->rt_timeleft = rtpp->rt_pquantum;
11290Sstevel@tonic-gate 		} else {
11300Sstevel@tonic-gate 			rtpp->rt_flags |= RTBACKQ;
11310Sstevel@tonic-gate 		}
11320Sstevel@tonic-gate 	}
11330Sstevel@tonic-gate }
1134