xref: /titanic_44/usr/src/uts/common/disp/rt.c (revision 24d819e6779cf7765ce55eee9cf6f0a8fcea4e30)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5d4204c85Sraf  * Common Development and Distribution License (the "License").
6d4204c85Sraf  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
21d4204c85Sraf 
227c478bd9Sstevel@tonic-gate /*
23d4204c85Sraf  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*24d819e6SJerry Jelinek  * Copyright 2013 Joyent, Inc.  All rights reserved.
267c478bd9Sstevel@tonic-gate  */
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
297c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
307c478bd9Sstevel@tonic-gate 
317c478bd9Sstevel@tonic-gate #include <sys/types.h>
327c478bd9Sstevel@tonic-gate #include <sys/param.h>
337c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
347c478bd9Sstevel@tonic-gate #include <sys/cred.h>
357c478bd9Sstevel@tonic-gate #include <sys/proc.h>
367c478bd9Sstevel@tonic-gate #include <sys/pcb.h>
377c478bd9Sstevel@tonic-gate #include <sys/signal.h>
387c478bd9Sstevel@tonic-gate #include <sys/user.h>
397c478bd9Sstevel@tonic-gate #include <sys/priocntl.h>
407c478bd9Sstevel@tonic-gate #include <sys/class.h>
417c478bd9Sstevel@tonic-gate #include <sys/disp.h>
427c478bd9Sstevel@tonic-gate #include <sys/procset.h>
437c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
447c478bd9Sstevel@tonic-gate #include <sys/debug.h>
457c478bd9Sstevel@tonic-gate #include <sys/rt.h>
467c478bd9Sstevel@tonic-gate #include <sys/rtpriocntl.h>
477c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
487c478bd9Sstevel@tonic-gate #include <sys/systm.h>
49d4204c85Sraf #include <sys/schedctl.h>
507c478bd9Sstevel@tonic-gate #include <sys/errno.h>
517c478bd9Sstevel@tonic-gate #include <sys/cpuvar.h>
527c478bd9Sstevel@tonic-gate #include <sys/vmsystm.h>
537c478bd9Sstevel@tonic-gate #include <sys/time.h>
547c478bd9Sstevel@tonic-gate #include <sys/policy.h>
557c478bd9Sstevel@tonic-gate #include <sys/sdt.h>
567c478bd9Sstevel@tonic-gate #include <sys/cpupart.h>
577c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate static pri_t	rt_init(id_t, int, classfuncs_t **);
607c478bd9Sstevel@tonic-gate 
617c478bd9Sstevel@tonic-gate static struct sclass csw = {
627c478bd9Sstevel@tonic-gate 	"RT",
637c478bd9Sstevel@tonic-gate 	rt_init,
647c478bd9Sstevel@tonic-gate 	0
657c478bd9Sstevel@tonic-gate };
667c478bd9Sstevel@tonic-gate 
677c478bd9Sstevel@tonic-gate static struct modlsched modlsched = {
687c478bd9Sstevel@tonic-gate 	&mod_schedops, "realtime scheduling class", &csw
697c478bd9Sstevel@tonic-gate };
707c478bd9Sstevel@tonic-gate 
717c478bd9Sstevel@tonic-gate static struct modlinkage modlinkage = {
727c478bd9Sstevel@tonic-gate 	MODREV_1, (void *)&modlsched, NULL
737c478bd9Sstevel@tonic-gate };
747c478bd9Sstevel@tonic-gate 
757c478bd9Sstevel@tonic-gate int
_init()767c478bd9Sstevel@tonic-gate _init()
777c478bd9Sstevel@tonic-gate {
787c478bd9Sstevel@tonic-gate 	return (mod_install(&modlinkage));
797c478bd9Sstevel@tonic-gate }
807c478bd9Sstevel@tonic-gate 
817c478bd9Sstevel@tonic-gate int
_fini()827c478bd9Sstevel@tonic-gate _fini()
837c478bd9Sstevel@tonic-gate {
847c478bd9Sstevel@tonic-gate 	return (EBUSY);		/* don't remove RT for now */
857c478bd9Sstevel@tonic-gate }
867c478bd9Sstevel@tonic-gate 
877c478bd9Sstevel@tonic-gate int
_info(struct modinfo * modinfop)887c478bd9Sstevel@tonic-gate _info(struct modinfo *modinfop)
897c478bd9Sstevel@tonic-gate {
907c478bd9Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
917c478bd9Sstevel@tonic-gate }
927c478bd9Sstevel@tonic-gate 
937c478bd9Sstevel@tonic-gate 
947c478bd9Sstevel@tonic-gate /*
957c478bd9Sstevel@tonic-gate  * Class specific code for the real-time class
967c478bd9Sstevel@tonic-gate  */
977c478bd9Sstevel@tonic-gate 
987c478bd9Sstevel@tonic-gate /*
997c478bd9Sstevel@tonic-gate  * Extern declarations for variables defined in the rt master file
1007c478bd9Sstevel@tonic-gate  */
1017c478bd9Sstevel@tonic-gate #define	RTMAXPRI 59
1027c478bd9Sstevel@tonic-gate 
1037c478bd9Sstevel@tonic-gate pri_t rt_maxpri = RTMAXPRI;	/* maximum real-time priority */
1047c478bd9Sstevel@tonic-gate rtdpent_t *rt_dptbl;	  /* real-time dispatcher parameter table */
1057c478bd9Sstevel@tonic-gate 
1067c478bd9Sstevel@tonic-gate /*
1077c478bd9Sstevel@tonic-gate  * control flags (kparms->rt_cflags).
1087c478bd9Sstevel@tonic-gate  */
1097c478bd9Sstevel@tonic-gate #define	RT_DOPRI	0x01	/* change priority */
1107c478bd9Sstevel@tonic-gate #define	RT_DOTQ		0x02	/* change RT time quantum */
1117c478bd9Sstevel@tonic-gate #define	RT_DOSIG	0x04	/* change RT time quantum signal */
1127c478bd9Sstevel@tonic-gate 
1137c478bd9Sstevel@tonic-gate static int	rt_admin(caddr_t, cred_t *);
1147c478bd9Sstevel@tonic-gate static int	rt_enterclass(kthread_t *, id_t, void *, cred_t *, void *);
1157c478bd9Sstevel@tonic-gate static int	rt_fork(kthread_t *, kthread_t *, void *);
1167c478bd9Sstevel@tonic-gate static int	rt_getclinfo(void *);
1177c478bd9Sstevel@tonic-gate static int	rt_getclpri(pcpri_t *);
1187c478bd9Sstevel@tonic-gate static int	rt_parmsin(void *);
1197c478bd9Sstevel@tonic-gate static int	rt_parmsout(void *, pc_vaparms_t *);
1207c478bd9Sstevel@tonic-gate static int	rt_vaparmsin(void *, pc_vaparms_t *);
1217c478bd9Sstevel@tonic-gate static int	rt_vaparmsout(void *, pc_vaparms_t *);
1227c478bd9Sstevel@tonic-gate static int	rt_parmsset(kthread_t *, void *, id_t, cred_t *);
1237c478bd9Sstevel@tonic-gate static int	rt_donice(kthread_t *, cred_t *, int, int *);
124d4204c85Sraf static int	rt_doprio(kthread_t *, cred_t *, int, int *);
1257c478bd9Sstevel@tonic-gate static void	rt_exitclass(void *);
1267c478bd9Sstevel@tonic-gate static int	rt_canexit(kthread_t *, cred_t *);
1277c478bd9Sstevel@tonic-gate static void	rt_forkret(kthread_t *, kthread_t *);
1287c478bd9Sstevel@tonic-gate static void	rt_nullsys();
1297c478bd9Sstevel@tonic-gate static void	rt_parmsget(kthread_t *, void *);
1307c478bd9Sstevel@tonic-gate static void	rt_preempt(kthread_t *);
1317c478bd9Sstevel@tonic-gate static void	rt_setrun(kthread_t *);
1327c478bd9Sstevel@tonic-gate static void	rt_tick(kthread_t *);
1337c478bd9Sstevel@tonic-gate static void	rt_wakeup(kthread_t *);
1347c478bd9Sstevel@tonic-gate static pri_t	rt_swapin(kthread_t *, int);
1357c478bd9Sstevel@tonic-gate static pri_t	rt_swapout(kthread_t *, int);
1367c478bd9Sstevel@tonic-gate static pri_t	rt_globpri(kthread_t *);
1377c478bd9Sstevel@tonic-gate static void	rt_yield(kthread_t *);
1387c478bd9Sstevel@tonic-gate static int	rt_alloc(void **, int);
1397c478bd9Sstevel@tonic-gate static void	rt_free(void *);
1407c478bd9Sstevel@tonic-gate 
1417c478bd9Sstevel@tonic-gate static void	rt_change_priority(kthread_t *, rtproc_t *);
1427c478bd9Sstevel@tonic-gate 
1437c478bd9Sstevel@tonic-gate static id_t	rt_cid;		/* real-time class ID */
1447c478bd9Sstevel@tonic-gate static rtproc_t	rt_plisthead;	/* dummy rtproc at head of rtproc list */
1457c478bd9Sstevel@tonic-gate static kmutex_t	rt_dptblock;	/* protects realtime dispatch table */
1467c478bd9Sstevel@tonic-gate static kmutex_t	rt_list_lock;	/* protects RT thread list */
1477c478bd9Sstevel@tonic-gate 
1487c478bd9Sstevel@tonic-gate extern rtdpent_t *rt_getdptbl(void);
1497c478bd9Sstevel@tonic-gate 
1507c478bd9Sstevel@tonic-gate static struct classfuncs rt_classfuncs = {
1517c478bd9Sstevel@tonic-gate 	/* class ops */
1527c478bd9Sstevel@tonic-gate 	rt_admin,
1537c478bd9Sstevel@tonic-gate 	rt_getclinfo,
1547c478bd9Sstevel@tonic-gate 	rt_parmsin,
1557c478bd9Sstevel@tonic-gate 	rt_parmsout,
1567c478bd9Sstevel@tonic-gate 	rt_vaparmsin,
1577c478bd9Sstevel@tonic-gate 	rt_vaparmsout,
1587c478bd9Sstevel@tonic-gate 	rt_getclpri,
1597c478bd9Sstevel@tonic-gate 	rt_alloc,
1607c478bd9Sstevel@tonic-gate 	rt_free,
1617c478bd9Sstevel@tonic-gate 	/* thread ops */
1627c478bd9Sstevel@tonic-gate 	rt_enterclass,
1637c478bd9Sstevel@tonic-gate 	rt_exitclass,
1647c478bd9Sstevel@tonic-gate 	rt_canexit,
1657c478bd9Sstevel@tonic-gate 	rt_fork,
1667c478bd9Sstevel@tonic-gate 	rt_forkret,
1677c478bd9Sstevel@tonic-gate 	rt_parmsget,
1687c478bd9Sstevel@tonic-gate 	rt_parmsset,
1697c478bd9Sstevel@tonic-gate 	rt_nullsys,	/* stop */
1707c478bd9Sstevel@tonic-gate 	rt_nullsys,	/* exit */
1717c478bd9Sstevel@tonic-gate 	rt_nullsys,	/* active */
1727c478bd9Sstevel@tonic-gate 	rt_nullsys,	/* inactive */
1737c478bd9Sstevel@tonic-gate 	rt_swapin,
1747c478bd9Sstevel@tonic-gate 	rt_swapout,
1757c478bd9Sstevel@tonic-gate 	rt_nullsys,	/* trapret */
1767c478bd9Sstevel@tonic-gate 	rt_preempt,
1777c478bd9Sstevel@tonic-gate 	rt_setrun,
1787c478bd9Sstevel@tonic-gate 	rt_nullsys,	/* sleep */
1797c478bd9Sstevel@tonic-gate 	rt_tick,
1807c478bd9Sstevel@tonic-gate 	rt_wakeup,
1817c478bd9Sstevel@tonic-gate 	rt_donice,
1827c478bd9Sstevel@tonic-gate 	rt_globpri,
1837c478bd9Sstevel@tonic-gate 	rt_nullsys,	/* set_process_group */
1847c478bd9Sstevel@tonic-gate 	rt_yield,
185d4204c85Sraf 	rt_doprio,
1867c478bd9Sstevel@tonic-gate };
1877c478bd9Sstevel@tonic-gate 
1887c478bd9Sstevel@tonic-gate /*
1897c478bd9Sstevel@tonic-gate  * Real-time class initialization. Called by dispinit() at boot time.
1907c478bd9Sstevel@tonic-gate  * We can ignore the clparmsz argument since we know that the smallest
1917c478bd9Sstevel@tonic-gate  * possible parameter buffer is big enough for us.
1927c478bd9Sstevel@tonic-gate  */
1937c478bd9Sstevel@tonic-gate /* ARGSUSED */
1947c478bd9Sstevel@tonic-gate pri_t
rt_init(id_t cid,int clparmsz,classfuncs_t ** clfuncspp)1957c478bd9Sstevel@tonic-gate rt_init(id_t cid, int clparmsz, classfuncs_t **clfuncspp)
1967c478bd9Sstevel@tonic-gate {
1977c478bd9Sstevel@tonic-gate 	rt_dptbl = rt_getdptbl();
1987c478bd9Sstevel@tonic-gate 	rt_cid = cid;	/* Record our class ID */
1997c478bd9Sstevel@tonic-gate 
2007c478bd9Sstevel@tonic-gate 	/*
2017c478bd9Sstevel@tonic-gate 	 * Initialize the rtproc list.
2027c478bd9Sstevel@tonic-gate 	 */
2037c478bd9Sstevel@tonic-gate 	rt_plisthead.rt_next = rt_plisthead.rt_prev = &rt_plisthead;
2047c478bd9Sstevel@tonic-gate 
2057c478bd9Sstevel@tonic-gate 	/*
2067c478bd9Sstevel@tonic-gate 	 * We're required to return a pointer to our classfuncs
2077c478bd9Sstevel@tonic-gate 	 * structure and the highest global priority value we use.
2087c478bd9Sstevel@tonic-gate 	 */
2097c478bd9Sstevel@tonic-gate 	*clfuncspp = &rt_classfuncs;
2107c478bd9Sstevel@tonic-gate 	mutex_init(&rt_dptblock, NULL, MUTEX_DEFAULT, NULL);
2117c478bd9Sstevel@tonic-gate 	mutex_init(&rt_list_lock, NULL, MUTEX_DEFAULT, NULL);
2127c478bd9Sstevel@tonic-gate 	return (rt_dptbl[rt_maxpri].rt_globpri);
2137c478bd9Sstevel@tonic-gate }
2147c478bd9Sstevel@tonic-gate 
2157c478bd9Sstevel@tonic-gate /*
2167c478bd9Sstevel@tonic-gate  * Get or reset the rt_dptbl values per the user's request.
2177c478bd9Sstevel@tonic-gate  */
2187c478bd9Sstevel@tonic-gate /* ARGSUSED */
2197c478bd9Sstevel@tonic-gate static int
rt_admin(caddr_t uaddr,cred_t * reqpcredp)2207c478bd9Sstevel@tonic-gate rt_admin(caddr_t uaddr, cred_t *reqpcredp)
2217c478bd9Sstevel@tonic-gate {
2227c478bd9Sstevel@tonic-gate 	rtadmin_t	rtadmin;
2237c478bd9Sstevel@tonic-gate 	rtdpent_t	*tmpdpp;
2247c478bd9Sstevel@tonic-gate 	size_t		userdpsz;
2257c478bd9Sstevel@tonic-gate 	size_t		rtdpsz;
2267c478bd9Sstevel@tonic-gate 	int		i;
2277c478bd9Sstevel@tonic-gate 
2287c478bd9Sstevel@tonic-gate 	if (get_udatamodel() == DATAMODEL_NATIVE) {
2297c478bd9Sstevel@tonic-gate 		if (copyin(uaddr, &rtadmin, sizeof (rtadmin_t)))
2307c478bd9Sstevel@tonic-gate 			return (EFAULT);
2317c478bd9Sstevel@tonic-gate 	}
2327c478bd9Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
2337c478bd9Sstevel@tonic-gate 	else {
2347c478bd9Sstevel@tonic-gate 		/* rtadmin struct from ILP32 callers */
2357c478bd9Sstevel@tonic-gate 		rtadmin32_t rtadmin32;
2367c478bd9Sstevel@tonic-gate 		if (copyin(uaddr, &rtadmin32, sizeof (rtadmin32_t)))
2377c478bd9Sstevel@tonic-gate 			return (EFAULT);
2387c478bd9Sstevel@tonic-gate 		rtadmin.rt_dpents =
2397c478bd9Sstevel@tonic-gate 		    (struct rtdpent *)(uintptr_t)rtadmin32.rt_dpents;
2407c478bd9Sstevel@tonic-gate 		rtadmin.rt_ndpents = rtadmin32.rt_ndpents;
2417c478bd9Sstevel@tonic-gate 		rtadmin.rt_cmd = rtadmin32.rt_cmd;
2427c478bd9Sstevel@tonic-gate 	}
2437c478bd9Sstevel@tonic-gate #endif /* _SYSCALL32_IMPL */
2447c478bd9Sstevel@tonic-gate 
2457c478bd9Sstevel@tonic-gate 	rtdpsz = (rt_maxpri + 1) * sizeof (rtdpent_t);
2467c478bd9Sstevel@tonic-gate 
2477c478bd9Sstevel@tonic-gate 	switch (rtadmin.rt_cmd) {
2487c478bd9Sstevel@tonic-gate 
2497c478bd9Sstevel@tonic-gate 	case RT_GETDPSIZE:
2507c478bd9Sstevel@tonic-gate 		rtadmin.rt_ndpents = rt_maxpri + 1;
2517c478bd9Sstevel@tonic-gate 
2527c478bd9Sstevel@tonic-gate 		if (get_udatamodel() == DATAMODEL_NATIVE) {
2537c478bd9Sstevel@tonic-gate 			if (copyout(&rtadmin, uaddr, sizeof (rtadmin_t)))
2547c478bd9Sstevel@tonic-gate 				return (EFAULT);
2557c478bd9Sstevel@tonic-gate 		}
2567c478bd9Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
2577c478bd9Sstevel@tonic-gate 		else {
2587c478bd9Sstevel@tonic-gate 			/* return rtadmin struct to ILP32 callers */
2597c478bd9Sstevel@tonic-gate 			rtadmin32_t rtadmin32;
2607c478bd9Sstevel@tonic-gate 			rtadmin32.rt_dpents =
2617c478bd9Sstevel@tonic-gate 			    (caddr32_t)(uintptr_t)rtadmin.rt_dpents;
2627c478bd9Sstevel@tonic-gate 			rtadmin32.rt_ndpents = rtadmin.rt_ndpents;
2637c478bd9Sstevel@tonic-gate 			rtadmin32.rt_cmd = rtadmin.rt_cmd;
2647c478bd9Sstevel@tonic-gate 			if (copyout(&rtadmin32, uaddr, sizeof (rtadmin32_t)))
2657c478bd9Sstevel@tonic-gate 				return (EFAULT);
2667c478bd9Sstevel@tonic-gate 		}
2677c478bd9Sstevel@tonic-gate #endif /* _SYSCALL32_IMPL */
2687c478bd9Sstevel@tonic-gate 
2697c478bd9Sstevel@tonic-gate 		break;
2707c478bd9Sstevel@tonic-gate 
2717c478bd9Sstevel@tonic-gate 	case RT_GETDPTBL:
2727c478bd9Sstevel@tonic-gate 		userdpsz = MIN(rtadmin.rt_ndpents * sizeof (rtdpent_t),
2737c478bd9Sstevel@tonic-gate 		    rtdpsz);
2747c478bd9Sstevel@tonic-gate 		if (copyout(rt_dptbl, rtadmin.rt_dpents, userdpsz))
2757c478bd9Sstevel@tonic-gate 			return (EFAULT);
2767c478bd9Sstevel@tonic-gate 		rtadmin.rt_ndpents = userdpsz / sizeof (rtdpent_t);
2777c478bd9Sstevel@tonic-gate 
2787c478bd9Sstevel@tonic-gate 		if (get_udatamodel() == DATAMODEL_NATIVE) {
2797c478bd9Sstevel@tonic-gate 			if (copyout(&rtadmin, uaddr, sizeof (rtadmin_t)))
2807c478bd9Sstevel@tonic-gate 				return (EFAULT);
2817c478bd9Sstevel@tonic-gate 		}
2827c478bd9Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
2837c478bd9Sstevel@tonic-gate 		else {
2847c478bd9Sstevel@tonic-gate 			/* return rtadmin struct to ILP32 callers */
2857c478bd9Sstevel@tonic-gate 			rtadmin32_t rtadmin32;
2867c478bd9Sstevel@tonic-gate 			rtadmin32.rt_dpents =
2877c478bd9Sstevel@tonic-gate 			    (caddr32_t)(uintptr_t)rtadmin.rt_dpents;
2887c478bd9Sstevel@tonic-gate 			rtadmin32.rt_ndpents = rtadmin.rt_ndpents;
2897c478bd9Sstevel@tonic-gate 			rtadmin32.rt_cmd = rtadmin.rt_cmd;
2907c478bd9Sstevel@tonic-gate 			if (copyout(&rtadmin32, uaddr, sizeof (rtadmin32_t)))
2917c478bd9Sstevel@tonic-gate 				return (EFAULT);
2927c478bd9Sstevel@tonic-gate 		}
2937c478bd9Sstevel@tonic-gate #endif /* _SYSCALL32_IMPL */
2947c478bd9Sstevel@tonic-gate 		break;
2957c478bd9Sstevel@tonic-gate 
2967c478bd9Sstevel@tonic-gate 	case RT_SETDPTBL:
2977c478bd9Sstevel@tonic-gate 		/*
2987c478bd9Sstevel@tonic-gate 		 * We require that the requesting process has sufficient
2997c478bd9Sstevel@tonic-gate 		 * priveleges.  We also require that the table supplied by
3007c478bd9Sstevel@tonic-gate 		 * the user exactly match the current rt_dptbl in size.
3017c478bd9Sstevel@tonic-gate 		 */
3027c478bd9Sstevel@tonic-gate 		if (secpolicy_dispadm(reqpcredp) != 0)
3037c478bd9Sstevel@tonic-gate 			return (EPERM);
3047c478bd9Sstevel@tonic-gate 		if (rtadmin.rt_ndpents * sizeof (rtdpent_t) != rtdpsz)
3057c478bd9Sstevel@tonic-gate 			return (EINVAL);
3067c478bd9Sstevel@tonic-gate 
3077c478bd9Sstevel@tonic-gate 		/*
3087c478bd9Sstevel@tonic-gate 		 * We read the user supplied table into a temporary buffer
3097c478bd9Sstevel@tonic-gate 		 * where the time quantum values are validated before
3107c478bd9Sstevel@tonic-gate 		 * being copied to the rt_dptbl.
3117c478bd9Sstevel@tonic-gate 		 */
3127c478bd9Sstevel@tonic-gate 		tmpdpp = kmem_alloc(rtdpsz, KM_SLEEP);
3137c478bd9Sstevel@tonic-gate 		if (copyin(rtadmin.rt_dpents, tmpdpp, rtdpsz)) {
3147c478bd9Sstevel@tonic-gate 			kmem_free(tmpdpp, rtdpsz);
3157c478bd9Sstevel@tonic-gate 			return (EFAULT);
3167c478bd9Sstevel@tonic-gate 		}
3177c478bd9Sstevel@tonic-gate 		for (i = 0; i < rtadmin.rt_ndpents; i++) {
3187c478bd9Sstevel@tonic-gate 
3197c478bd9Sstevel@tonic-gate 			/*
3207c478bd9Sstevel@tonic-gate 			 * Validate the user supplied time quantum values.
3217c478bd9Sstevel@tonic-gate 			 */
3227c478bd9Sstevel@tonic-gate 			if (tmpdpp[i].rt_quantum <= 0 &&
3237c478bd9Sstevel@tonic-gate 			    tmpdpp[i].rt_quantum != RT_TQINF) {
3247c478bd9Sstevel@tonic-gate 				kmem_free(tmpdpp, rtdpsz);
3257c478bd9Sstevel@tonic-gate 				return (EINVAL);
3267c478bd9Sstevel@tonic-gate 			}
3277c478bd9Sstevel@tonic-gate 		}
3287c478bd9Sstevel@tonic-gate 
3297c478bd9Sstevel@tonic-gate 		/*
3307c478bd9Sstevel@tonic-gate 		 * Copy the user supplied values over the current rt_dptbl
3317c478bd9Sstevel@tonic-gate 		 * values.  The rt_globpri member is read-only so we don't
3327c478bd9Sstevel@tonic-gate 		 * overwrite it.
3337c478bd9Sstevel@tonic-gate 		 */
3347c478bd9Sstevel@tonic-gate 		mutex_enter(&rt_dptblock);
3357c478bd9Sstevel@tonic-gate 		for (i = 0; i < rtadmin.rt_ndpents; i++)
3367c478bd9Sstevel@tonic-gate 			rt_dptbl[i].rt_quantum = tmpdpp[i].rt_quantum;
3377c478bd9Sstevel@tonic-gate 		mutex_exit(&rt_dptblock);
3387c478bd9Sstevel@tonic-gate 		kmem_free(tmpdpp, rtdpsz);
3397c478bd9Sstevel@tonic-gate 		break;
3407c478bd9Sstevel@tonic-gate 
3417c478bd9Sstevel@tonic-gate 	default:
3427c478bd9Sstevel@tonic-gate 		return (EINVAL);
3437c478bd9Sstevel@tonic-gate 	}
3447c478bd9Sstevel@tonic-gate 	return (0);
3457c478bd9Sstevel@tonic-gate }
3467c478bd9Sstevel@tonic-gate 
3477c478bd9Sstevel@tonic-gate 
3487c478bd9Sstevel@tonic-gate /*
3497c478bd9Sstevel@tonic-gate  * Allocate a real-time class specific proc structure and
3507c478bd9Sstevel@tonic-gate  * initialize it with the parameters supplied. Also move thread
3517c478bd9Sstevel@tonic-gate  * to specified real-time priority.
3527c478bd9Sstevel@tonic-gate  */
3537c478bd9Sstevel@tonic-gate /* ARGSUSED */
3547c478bd9Sstevel@tonic-gate static int
rt_enterclass(kthread_t * t,id_t cid,void * parmsp,cred_t * reqpcredp,void * bufp)3557c478bd9Sstevel@tonic-gate rt_enterclass(kthread_t *t, id_t cid, void *parmsp, cred_t *reqpcredp,
3567c478bd9Sstevel@tonic-gate     void *bufp)
3577c478bd9Sstevel@tonic-gate {
3587c478bd9Sstevel@tonic-gate 	rtkparms_t *rtkparmsp = (rtkparms_t *)parmsp;
3597c478bd9Sstevel@tonic-gate 	rtproc_t *rtpp;
3607c478bd9Sstevel@tonic-gate 
3617c478bd9Sstevel@tonic-gate 	/*
3627c478bd9Sstevel@tonic-gate 	 * For a thread to enter the real-time class the thread
3637c478bd9Sstevel@tonic-gate 	 * which initiates the request must be privileged.
3647c478bd9Sstevel@tonic-gate 	 * This may have been checked previously but if our
3657c478bd9Sstevel@tonic-gate 	 * caller passed us a credential structure we assume it
3667c478bd9Sstevel@tonic-gate 	 * hasn't and we check it here.
3677c478bd9Sstevel@tonic-gate 	 */
3687c478bd9Sstevel@tonic-gate 	if (reqpcredp != NULL && secpolicy_setpriority(reqpcredp) != 0)
3697c478bd9Sstevel@tonic-gate 		return (EPERM);
3707c478bd9Sstevel@tonic-gate 
3717c478bd9Sstevel@tonic-gate 	rtpp = (rtproc_t *)bufp;
3727c478bd9Sstevel@tonic-gate 	ASSERT(rtpp != NULL);
3737c478bd9Sstevel@tonic-gate 
3747c478bd9Sstevel@tonic-gate 	/*
3757c478bd9Sstevel@tonic-gate 	 * If this thread's lwp is swapped out, it will be brought in
3767c478bd9Sstevel@tonic-gate 	 * when it is put onto the runqueue.
3777c478bd9Sstevel@tonic-gate 	 *
3787c478bd9Sstevel@tonic-gate 	 * Now, Initialize the rtproc structure.
3797c478bd9Sstevel@tonic-gate 	 */
3807c478bd9Sstevel@tonic-gate 	if (rtkparmsp == NULL) {
3817c478bd9Sstevel@tonic-gate 		/*
3827c478bd9Sstevel@tonic-gate 		 * Use default values
3837c478bd9Sstevel@tonic-gate 		 */
3847c478bd9Sstevel@tonic-gate 		rtpp->rt_pri = 0;
3857c478bd9Sstevel@tonic-gate 		rtpp->rt_pquantum = rt_dptbl[0].rt_quantum;
3867c478bd9Sstevel@tonic-gate 		rtpp->rt_tqsignal = 0;
3877c478bd9Sstevel@tonic-gate 	} else {
3887c478bd9Sstevel@tonic-gate 		/*
3897c478bd9Sstevel@tonic-gate 		 * Use supplied values
3907c478bd9Sstevel@tonic-gate 		 */
3917c478bd9Sstevel@tonic-gate 		if ((rtkparmsp->rt_cflags & RT_DOPRI) == 0)
3927c478bd9Sstevel@tonic-gate 			rtpp->rt_pri = 0;
3937c478bd9Sstevel@tonic-gate 		else
3947c478bd9Sstevel@tonic-gate 			rtpp->rt_pri = rtkparmsp->rt_pri;
3957c478bd9Sstevel@tonic-gate 
3967c478bd9Sstevel@tonic-gate 		if (rtkparmsp->rt_tqntm == RT_TQINF)
3977c478bd9Sstevel@tonic-gate 			rtpp->rt_pquantum = RT_TQINF;
3987c478bd9Sstevel@tonic-gate 		else if (rtkparmsp->rt_tqntm == RT_TQDEF ||
3997c478bd9Sstevel@tonic-gate 		    (rtkparmsp->rt_cflags & RT_DOTQ) == 0)
4007c478bd9Sstevel@tonic-gate 			rtpp->rt_pquantum = rt_dptbl[rtpp->rt_pri].rt_quantum;
4017c478bd9Sstevel@tonic-gate 		else
4027c478bd9Sstevel@tonic-gate 			rtpp->rt_pquantum = rtkparmsp->rt_tqntm;
4037c478bd9Sstevel@tonic-gate 
4047c478bd9Sstevel@tonic-gate 		if ((rtkparmsp->rt_cflags & RT_DOSIG) == 0)
4057c478bd9Sstevel@tonic-gate 			rtpp->rt_tqsignal = 0;
4067c478bd9Sstevel@tonic-gate 		else
4077c478bd9Sstevel@tonic-gate 			rtpp->rt_tqsignal = rtkparmsp->rt_tqsig;
4087c478bd9Sstevel@tonic-gate 	}
4097c478bd9Sstevel@tonic-gate 	rtpp->rt_flags = 0;
4107c478bd9Sstevel@tonic-gate 	rtpp->rt_tp = t;
4117c478bd9Sstevel@tonic-gate 	/*
4127c478bd9Sstevel@tonic-gate 	 * Reset thread priority
4137c478bd9Sstevel@tonic-gate 	 */
4147c478bd9Sstevel@tonic-gate 	thread_lock(t);
4157c478bd9Sstevel@tonic-gate 	t->t_clfuncs = &(sclass[cid].cl_funcs->thread);
4167c478bd9Sstevel@tonic-gate 	t->t_cid = cid;
4177c478bd9Sstevel@tonic-gate 	t->t_cldata = (void *)rtpp;
4187c478bd9Sstevel@tonic-gate 	t->t_schedflag &= ~TS_RUNQMATCH;
4197c478bd9Sstevel@tonic-gate 	rt_change_priority(t, rtpp);
4207c478bd9Sstevel@tonic-gate 	thread_unlock(t);
4217c478bd9Sstevel@tonic-gate 	/*
4227c478bd9Sstevel@tonic-gate 	 * Link new structure into rtproc list
4237c478bd9Sstevel@tonic-gate 	 */
4247c478bd9Sstevel@tonic-gate 	mutex_enter(&rt_list_lock);
4257c478bd9Sstevel@tonic-gate 	rtpp->rt_next = rt_plisthead.rt_next;
4267c478bd9Sstevel@tonic-gate 	rtpp->rt_prev = &rt_plisthead;
4277c478bd9Sstevel@tonic-gate 	rt_plisthead.rt_next->rt_prev = rtpp;
4287c478bd9Sstevel@tonic-gate 	rt_plisthead.rt_next = rtpp;
4297c478bd9Sstevel@tonic-gate 	mutex_exit(&rt_list_lock);
4307c478bd9Sstevel@tonic-gate 	return (0);
4317c478bd9Sstevel@tonic-gate }
4327c478bd9Sstevel@tonic-gate 
4337c478bd9Sstevel@tonic-gate 
4347c478bd9Sstevel@tonic-gate /*
4357c478bd9Sstevel@tonic-gate  * Free rtproc structure of thread.
4367c478bd9Sstevel@tonic-gate  */
4377c478bd9Sstevel@tonic-gate static void
rt_exitclass(void * procp)4387c478bd9Sstevel@tonic-gate rt_exitclass(void *procp)
4397c478bd9Sstevel@tonic-gate {
4407c478bd9Sstevel@tonic-gate 	rtproc_t *rtprocp = (rtproc_t *)procp;
4417c478bd9Sstevel@tonic-gate 
4427c478bd9Sstevel@tonic-gate 	mutex_enter(&rt_list_lock);
4437c478bd9Sstevel@tonic-gate 	rtprocp->rt_prev->rt_next = rtprocp->rt_next;
4447c478bd9Sstevel@tonic-gate 	rtprocp->rt_next->rt_prev = rtprocp->rt_prev;
4457c478bd9Sstevel@tonic-gate 	mutex_exit(&rt_list_lock);
4467c478bd9Sstevel@tonic-gate 	kmem_free(rtprocp, sizeof (rtproc_t));
4477c478bd9Sstevel@tonic-gate }
4487c478bd9Sstevel@tonic-gate 
4497c478bd9Sstevel@tonic-gate 
4507c478bd9Sstevel@tonic-gate /*
4517c478bd9Sstevel@tonic-gate  * Allocate and initialize real-time class specific
4527c478bd9Sstevel@tonic-gate  * proc structure for child.
4537c478bd9Sstevel@tonic-gate  */
4547c478bd9Sstevel@tonic-gate /* ARGSUSED */
4557c478bd9Sstevel@tonic-gate static int
rt_fork(kthread_t * t,kthread_t * ct,void * bufp)4567c478bd9Sstevel@tonic-gate rt_fork(kthread_t *t, kthread_t *ct, void *bufp)
4577c478bd9Sstevel@tonic-gate {
4587c478bd9Sstevel@tonic-gate 	rtproc_t *prtpp;
4597c478bd9Sstevel@tonic-gate 	rtproc_t *crtpp;
4607c478bd9Sstevel@tonic-gate 
4617c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&ttoproc(t)->p_lock));
4627c478bd9Sstevel@tonic-gate 
4637c478bd9Sstevel@tonic-gate 	/*
4647c478bd9Sstevel@tonic-gate 	 * Initialize child's rtproc structure
4657c478bd9Sstevel@tonic-gate 	 */
4667c478bd9Sstevel@tonic-gate 	crtpp = (rtproc_t *)bufp;
4677c478bd9Sstevel@tonic-gate 	ASSERT(crtpp != NULL);
4687c478bd9Sstevel@tonic-gate 	prtpp = (rtproc_t *)t->t_cldata;
4697c478bd9Sstevel@tonic-gate 	thread_lock(t);
4707c478bd9Sstevel@tonic-gate 	crtpp->rt_timeleft = crtpp->rt_pquantum = prtpp->rt_pquantum;
4717c478bd9Sstevel@tonic-gate 	crtpp->rt_pri = prtpp->rt_pri;
4727c478bd9Sstevel@tonic-gate 	crtpp->rt_flags = prtpp->rt_flags & ~RTBACKQ;
4737c478bd9Sstevel@tonic-gate 	crtpp->rt_tqsignal = prtpp->rt_tqsignal;
4747c478bd9Sstevel@tonic-gate 
4757c478bd9Sstevel@tonic-gate 	crtpp->rt_tp = ct;
4767c478bd9Sstevel@tonic-gate 	thread_unlock(t);
4777c478bd9Sstevel@tonic-gate 
4787c478bd9Sstevel@tonic-gate 	/*
4797c478bd9Sstevel@tonic-gate 	 * Link new structure into rtproc list
4807c478bd9Sstevel@tonic-gate 	 */
4817c478bd9Sstevel@tonic-gate 	ct->t_cldata = (void *)crtpp;
4827c478bd9Sstevel@tonic-gate 	mutex_enter(&rt_list_lock);
4837c478bd9Sstevel@tonic-gate 	crtpp->rt_next = rt_plisthead.rt_next;
4847c478bd9Sstevel@tonic-gate 	crtpp->rt_prev = &rt_plisthead;
4857c478bd9Sstevel@tonic-gate 	rt_plisthead.rt_next->rt_prev = crtpp;
4867c478bd9Sstevel@tonic-gate 	rt_plisthead.rt_next = crtpp;
4877c478bd9Sstevel@tonic-gate 	mutex_exit(&rt_list_lock);
4887c478bd9Sstevel@tonic-gate 	return (0);
4897c478bd9Sstevel@tonic-gate }
4907c478bd9Sstevel@tonic-gate 
4917c478bd9Sstevel@tonic-gate 
4927c478bd9Sstevel@tonic-gate /*
4937c478bd9Sstevel@tonic-gate  * The child goes to the back of its dispatcher queue while the
4947c478bd9Sstevel@tonic-gate  * parent continues to run after a real time thread forks.
4957c478bd9Sstevel@tonic-gate  */
4967c478bd9Sstevel@tonic-gate /* ARGSUSED */
4977c478bd9Sstevel@tonic-gate static void
rt_forkret(kthread_t * t,kthread_t * ct)4987c478bd9Sstevel@tonic-gate rt_forkret(kthread_t *t, kthread_t *ct)
4997c478bd9Sstevel@tonic-gate {
5007c478bd9Sstevel@tonic-gate 	proc_t *pp = ttoproc(t);
5017c478bd9Sstevel@tonic-gate 	proc_t *cp = ttoproc(ct);
5027c478bd9Sstevel@tonic-gate 
5037c478bd9Sstevel@tonic-gate 	ASSERT(t == curthread);
5047c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&pidlock));
5057c478bd9Sstevel@tonic-gate 
5067c478bd9Sstevel@tonic-gate 	/*
5077c478bd9Sstevel@tonic-gate 	 * Grab the child's p_lock before dropping pidlock to ensure
5087c478bd9Sstevel@tonic-gate 	 * the process does not disappear before we set it running.
5097c478bd9Sstevel@tonic-gate 	 */
5107c478bd9Sstevel@tonic-gate 	mutex_enter(&cp->p_lock);
5117c478bd9Sstevel@tonic-gate 	mutex_exit(&pidlock);
5127c478bd9Sstevel@tonic-gate 	continuelwps(cp);
5137c478bd9Sstevel@tonic-gate 	mutex_exit(&cp->p_lock);
5147c478bd9Sstevel@tonic-gate 
5157c478bd9Sstevel@tonic-gate 	mutex_enter(&pp->p_lock);
5167c478bd9Sstevel@tonic-gate 	continuelwps(pp);
5177c478bd9Sstevel@tonic-gate 	mutex_exit(&pp->p_lock);
5187c478bd9Sstevel@tonic-gate }
5197c478bd9Sstevel@tonic-gate 
5207c478bd9Sstevel@tonic-gate 
5217c478bd9Sstevel@tonic-gate /*
5227c478bd9Sstevel@tonic-gate  * Get information about the real-time class into the buffer
5237c478bd9Sstevel@tonic-gate  * pointed to by rtinfop.  The maximum configured real-time
5247c478bd9Sstevel@tonic-gate  * priority is the only information we supply.  We ignore the
5257c478bd9Sstevel@tonic-gate  * class and credential arguments because anyone can have this
5267c478bd9Sstevel@tonic-gate  * information.
5277c478bd9Sstevel@tonic-gate  */
5287c478bd9Sstevel@tonic-gate /* ARGSUSED */
5297c478bd9Sstevel@tonic-gate static int
rt_getclinfo(void * infop)5307c478bd9Sstevel@tonic-gate rt_getclinfo(void *infop)
5317c478bd9Sstevel@tonic-gate {
5327c478bd9Sstevel@tonic-gate 	rtinfo_t *rtinfop = (rtinfo_t *)infop;
5337c478bd9Sstevel@tonic-gate 	rtinfop->rt_maxpri = rt_maxpri;
5347c478bd9Sstevel@tonic-gate 	return (0);
5357c478bd9Sstevel@tonic-gate }
5367c478bd9Sstevel@tonic-gate 
5377c478bd9Sstevel@tonic-gate /*
538d4204c85Sraf  * Return the user mode scheduling priority range.
5397c478bd9Sstevel@tonic-gate  */
5407c478bd9Sstevel@tonic-gate static int
rt_getclpri(pcpri_t * pcprip)5417c478bd9Sstevel@tonic-gate rt_getclpri(pcpri_t *pcprip)
5427c478bd9Sstevel@tonic-gate {
543d4204c85Sraf 	pcprip->pc_clpmax = rt_maxpri;
544d4204c85Sraf 	pcprip->pc_clpmin = 0;
5457c478bd9Sstevel@tonic-gate 	return (0);
5467c478bd9Sstevel@tonic-gate }
547d4204c85Sraf 
5487c478bd9Sstevel@tonic-gate static void
rt_nullsys()5497c478bd9Sstevel@tonic-gate rt_nullsys()
5507c478bd9Sstevel@tonic-gate {
5517c478bd9Sstevel@tonic-gate }
5527c478bd9Sstevel@tonic-gate 
5537c478bd9Sstevel@tonic-gate /* ARGSUSED */
5547c478bd9Sstevel@tonic-gate static int
rt_canexit(kthread_t * t,cred_t * cred)5557c478bd9Sstevel@tonic-gate rt_canexit(kthread_t *t, cred_t *cred)
5567c478bd9Sstevel@tonic-gate {
5577c478bd9Sstevel@tonic-gate 	/*
5587c478bd9Sstevel@tonic-gate 	 * Thread can always leave RT class
5597c478bd9Sstevel@tonic-gate 	 */
5607c478bd9Sstevel@tonic-gate 	return (0);
5617c478bd9Sstevel@tonic-gate }
5627c478bd9Sstevel@tonic-gate 
5637c478bd9Sstevel@tonic-gate /*
5647c478bd9Sstevel@tonic-gate  * Get the real-time scheduling parameters of the thread pointed to by
5657c478bd9Sstevel@tonic-gate  * rtprocp into the buffer pointed to by rtkparmsp.
5667c478bd9Sstevel@tonic-gate  */
5677c478bd9Sstevel@tonic-gate static void
rt_parmsget(kthread_t * t,void * parmsp)5687c478bd9Sstevel@tonic-gate rt_parmsget(kthread_t *t, void *parmsp)
5697c478bd9Sstevel@tonic-gate {
5707c478bd9Sstevel@tonic-gate 	rtproc_t	*rtprocp = (rtproc_t *)t->t_cldata;
5717c478bd9Sstevel@tonic-gate 	rtkparms_t	*rtkparmsp = (rtkparms_t *)parmsp;
5727c478bd9Sstevel@tonic-gate 
5737c478bd9Sstevel@tonic-gate 	rtkparmsp->rt_pri = rtprocp->rt_pri;
5747c478bd9Sstevel@tonic-gate 	rtkparmsp->rt_tqntm = rtprocp->rt_pquantum;
5757c478bd9Sstevel@tonic-gate 	rtkparmsp->rt_tqsig = rtprocp->rt_tqsignal;
5767c478bd9Sstevel@tonic-gate }
5777c478bd9Sstevel@tonic-gate 
5787c478bd9Sstevel@tonic-gate 
5797c478bd9Sstevel@tonic-gate 
5807c478bd9Sstevel@tonic-gate /*
5817c478bd9Sstevel@tonic-gate  * Check the validity of the real-time parameters in the buffer
5827c478bd9Sstevel@tonic-gate  * pointed to by rtprmsp.
5837c478bd9Sstevel@tonic-gate  * We convert the rtparms buffer from the user supplied format to
5847c478bd9Sstevel@tonic-gate  * our internal format (i.e. time quantum expressed in ticks).
5857c478bd9Sstevel@tonic-gate  */
5867c478bd9Sstevel@tonic-gate static int
rt_parmsin(void * prmsp)5877c478bd9Sstevel@tonic-gate rt_parmsin(void *prmsp)
5887c478bd9Sstevel@tonic-gate {
5897c478bd9Sstevel@tonic-gate 	rtparms_t *rtprmsp = (rtparms_t *)prmsp;
5907c478bd9Sstevel@tonic-gate 	longlong_t	ticks;
5917c478bd9Sstevel@tonic-gate 	uint_t		cflags;
5927c478bd9Sstevel@tonic-gate 
5937c478bd9Sstevel@tonic-gate 	/*
5947c478bd9Sstevel@tonic-gate 	 * First check the validity of parameters and convert
5957c478bd9Sstevel@tonic-gate 	 * the buffer to kernel format.
5967c478bd9Sstevel@tonic-gate 	 */
5977c478bd9Sstevel@tonic-gate 	if ((rtprmsp->rt_pri < 0 || rtprmsp->rt_pri > rt_maxpri) &&
5987c478bd9Sstevel@tonic-gate 	    rtprmsp->rt_pri != RT_NOCHANGE)
5997c478bd9Sstevel@tonic-gate 		return (EINVAL);
6007c478bd9Sstevel@tonic-gate 
6017c478bd9Sstevel@tonic-gate 	cflags = (rtprmsp->rt_pri != RT_NOCHANGE ? RT_DOPRI : 0);
6027c478bd9Sstevel@tonic-gate 
6037c478bd9Sstevel@tonic-gate 	if ((rtprmsp->rt_tqsecs == 0 && rtprmsp->rt_tqnsecs == 0) ||
6047c478bd9Sstevel@tonic-gate 	    rtprmsp->rt_tqnsecs >= NANOSEC)
6057c478bd9Sstevel@tonic-gate 		return (EINVAL);
6067c478bd9Sstevel@tonic-gate 
6077c478bd9Sstevel@tonic-gate 	if (rtprmsp->rt_tqnsecs != RT_NOCHANGE)
6087c478bd9Sstevel@tonic-gate 		cflags |= RT_DOTQ;
6097c478bd9Sstevel@tonic-gate 
6107c478bd9Sstevel@tonic-gate 	if (rtprmsp->rt_tqnsecs >= 0) {
6117c478bd9Sstevel@tonic-gate 		if ((ticks = SEC_TO_TICK((longlong_t)rtprmsp->rt_tqsecs) +
6127c478bd9Sstevel@tonic-gate 		    NSEC_TO_TICK_ROUNDUP(rtprmsp->rt_tqnsecs)) > INT_MAX)
6137c478bd9Sstevel@tonic-gate 			return (ERANGE);
6147c478bd9Sstevel@tonic-gate 
6157c478bd9Sstevel@tonic-gate 		((rtkparms_t *)rtprmsp)->rt_tqntm = (int)ticks;
6167c478bd9Sstevel@tonic-gate 	} else {
6177c478bd9Sstevel@tonic-gate 		if (rtprmsp->rt_tqnsecs != RT_NOCHANGE &&
6187c478bd9Sstevel@tonic-gate 		    rtprmsp->rt_tqnsecs != RT_TQINF &&
6197c478bd9Sstevel@tonic-gate 		    rtprmsp->rt_tqnsecs != RT_TQDEF)
6207c478bd9Sstevel@tonic-gate 			return (EINVAL);
6217c478bd9Sstevel@tonic-gate 
6227c478bd9Sstevel@tonic-gate 		((rtkparms_t *)rtprmsp)->rt_tqntm = rtprmsp->rt_tqnsecs;
6237c478bd9Sstevel@tonic-gate 	}
6247c478bd9Sstevel@tonic-gate 	((rtkparms_t *)rtprmsp)->rt_cflags = cflags;
6257c478bd9Sstevel@tonic-gate 
6267c478bd9Sstevel@tonic-gate 	return (0);
6277c478bd9Sstevel@tonic-gate }
6287c478bd9Sstevel@tonic-gate 
6297c478bd9Sstevel@tonic-gate 
6307c478bd9Sstevel@tonic-gate /*
6317c478bd9Sstevel@tonic-gate  * Check the validity of the real-time parameters in the pc_vaparms_t
6327c478bd9Sstevel@tonic-gate  * structure vaparmsp and put them in the buffer pointed to by rtprmsp.
6337c478bd9Sstevel@tonic-gate  * pc_vaparms_t contains (key, value) pairs of parameter.
6347c478bd9Sstevel@tonic-gate  * rt_vaparmsin() is the variable parameter version of rt_parmsin().
6357c478bd9Sstevel@tonic-gate  */
6367c478bd9Sstevel@tonic-gate static int
rt_vaparmsin(void * prmsp,pc_vaparms_t * vaparmsp)6377c478bd9Sstevel@tonic-gate rt_vaparmsin(void *prmsp, pc_vaparms_t *vaparmsp)
6387c478bd9Sstevel@tonic-gate {
6397c478bd9Sstevel@tonic-gate 	uint_t		secs = 0;
6407c478bd9Sstevel@tonic-gate 	uint_t		cnt;
6417c478bd9Sstevel@tonic-gate 	int		nsecs = 0;
6427c478bd9Sstevel@tonic-gate 	int		priflag, secflag, nsecflag, sigflag;
6437c478bd9Sstevel@tonic-gate 	longlong_t	ticks;
6447c478bd9Sstevel@tonic-gate 	rtkparms_t	*rtprmsp = (rtkparms_t *)prmsp;
6457c478bd9Sstevel@tonic-gate 	pc_vaparm_t	*vpp = &vaparmsp->pc_parms[0];
6467c478bd9Sstevel@tonic-gate 
6477c478bd9Sstevel@tonic-gate 
6487c478bd9Sstevel@tonic-gate 	/*
6497c478bd9Sstevel@tonic-gate 	 * First check the validity of parameters and convert them
6507c478bd9Sstevel@tonic-gate 	 * from the user supplied format to the internal format.
6517c478bd9Sstevel@tonic-gate 	 */
6527c478bd9Sstevel@tonic-gate 	priflag = secflag = nsecflag = sigflag = 0;
6537c478bd9Sstevel@tonic-gate 	rtprmsp->rt_cflags = 0;
6547c478bd9Sstevel@tonic-gate 
6557c478bd9Sstevel@tonic-gate 	if (vaparmsp->pc_vaparmscnt > PC_VAPARMCNT)
6567c478bd9Sstevel@tonic-gate 		return (EINVAL);
6577c478bd9Sstevel@tonic-gate 
6587c478bd9Sstevel@tonic-gate 	for (cnt = 0; cnt < vaparmsp->pc_vaparmscnt; cnt++, vpp++) {
6597c478bd9Sstevel@tonic-gate 
6607c478bd9Sstevel@tonic-gate 		switch (vpp->pc_key) {
6617c478bd9Sstevel@tonic-gate 		case RT_KY_PRI:
6627c478bd9Sstevel@tonic-gate 			if (priflag++)
6637c478bd9Sstevel@tonic-gate 				return (EINVAL);
6647c478bd9Sstevel@tonic-gate 			rtprmsp->rt_cflags |= RT_DOPRI;
6657c478bd9Sstevel@tonic-gate 			rtprmsp->rt_pri = (pri_t)vpp->pc_parm;
6667c478bd9Sstevel@tonic-gate 			if (rtprmsp->rt_pri < 0 || rtprmsp->rt_pri > rt_maxpri)
6677c478bd9Sstevel@tonic-gate 				return (EINVAL);
6687c478bd9Sstevel@tonic-gate 			break;
6697c478bd9Sstevel@tonic-gate 
6707c478bd9Sstevel@tonic-gate 		case RT_KY_TQSECS:
6717c478bd9Sstevel@tonic-gate 			if (secflag++)
6727c478bd9Sstevel@tonic-gate 				return (EINVAL);
6737c478bd9Sstevel@tonic-gate 			rtprmsp->rt_cflags |= RT_DOTQ;
6747c478bd9Sstevel@tonic-gate 			secs = (uint_t)vpp->pc_parm;
6757c478bd9Sstevel@tonic-gate 			break;
6767c478bd9Sstevel@tonic-gate 
6777c478bd9Sstevel@tonic-gate 		case RT_KY_TQNSECS:
6787c478bd9Sstevel@tonic-gate 			if (nsecflag++)
6797c478bd9Sstevel@tonic-gate 				return (EINVAL);
6807c478bd9Sstevel@tonic-gate 			rtprmsp->rt_cflags |= RT_DOTQ;
6817c478bd9Sstevel@tonic-gate 			nsecs = (int)vpp->pc_parm;
6827c478bd9Sstevel@tonic-gate 			break;
6837c478bd9Sstevel@tonic-gate 
6847c478bd9Sstevel@tonic-gate 		case RT_KY_TQSIG:
6857c478bd9Sstevel@tonic-gate 			if (sigflag++)
6867c478bd9Sstevel@tonic-gate 				return (EINVAL);
6877c478bd9Sstevel@tonic-gate 			rtprmsp->rt_cflags |= RT_DOSIG;
6887c478bd9Sstevel@tonic-gate 			rtprmsp->rt_tqsig = (int)vpp->pc_parm;
6897c478bd9Sstevel@tonic-gate 			if (rtprmsp->rt_tqsig < 0 || rtprmsp->rt_tqsig >= NSIG)
6907c478bd9Sstevel@tonic-gate 				return (EINVAL);
6917c478bd9Sstevel@tonic-gate 			break;
6927c478bd9Sstevel@tonic-gate 
6937c478bd9Sstevel@tonic-gate 		default:
6947c478bd9Sstevel@tonic-gate 			return (EINVAL);
6957c478bd9Sstevel@tonic-gate 		}
6967c478bd9Sstevel@tonic-gate 	}
6977c478bd9Sstevel@tonic-gate 
6987c478bd9Sstevel@tonic-gate 	if (vaparmsp->pc_vaparmscnt == 0) {
6997c478bd9Sstevel@tonic-gate 		/*
7007c478bd9Sstevel@tonic-gate 		 * Use default parameters.
7017c478bd9Sstevel@tonic-gate 		 */
7027c478bd9Sstevel@tonic-gate 		rtprmsp->rt_pri = 0;
7037c478bd9Sstevel@tonic-gate 		rtprmsp->rt_tqntm = RT_TQDEF;
7047c478bd9Sstevel@tonic-gate 		rtprmsp->rt_tqsig = 0;
7057c478bd9Sstevel@tonic-gate 		rtprmsp->rt_cflags = RT_DOPRI | RT_DOTQ | RT_DOSIG;
7067c478bd9Sstevel@tonic-gate 	} else if ((rtprmsp->rt_cflags & RT_DOTQ) != 0) {
7077c478bd9Sstevel@tonic-gate 		if ((secs == 0 && nsecs == 0) || nsecs >= NANOSEC)
7087c478bd9Sstevel@tonic-gate 			return (EINVAL);
7097c478bd9Sstevel@tonic-gate 
7107c478bd9Sstevel@tonic-gate 		if (nsecs >= 0) {
7117c478bd9Sstevel@tonic-gate 			if ((ticks = SEC_TO_TICK((longlong_t)secs) +
7127c478bd9Sstevel@tonic-gate 			    NSEC_TO_TICK_ROUNDUP(nsecs)) > INT_MAX)
7137c478bd9Sstevel@tonic-gate 				return (ERANGE);
7147c478bd9Sstevel@tonic-gate 
7157c478bd9Sstevel@tonic-gate 			rtprmsp->rt_tqntm = (int)ticks;
7167c478bd9Sstevel@tonic-gate 		} else {
7177c478bd9Sstevel@tonic-gate 			if (nsecs != RT_TQINF && nsecs != RT_TQDEF)
7187c478bd9Sstevel@tonic-gate 				return (EINVAL);
7197c478bd9Sstevel@tonic-gate 			rtprmsp->rt_tqntm = nsecs;
7207c478bd9Sstevel@tonic-gate 		}
7217c478bd9Sstevel@tonic-gate 	}
7227c478bd9Sstevel@tonic-gate 
7237c478bd9Sstevel@tonic-gate 	return (0);
7247c478bd9Sstevel@tonic-gate }
7257c478bd9Sstevel@tonic-gate 
7267c478bd9Sstevel@tonic-gate /*
7277c478bd9Sstevel@tonic-gate  * Do required processing on the real-time parameter buffer
7287c478bd9Sstevel@tonic-gate  * before it is copied out to the user.
7297c478bd9Sstevel@tonic-gate  * All we have to do is convert the buffer from kernel to user format
7307c478bd9Sstevel@tonic-gate  * (i.e. convert time quantum from ticks to seconds-nanoseconds).
7317c478bd9Sstevel@tonic-gate  */
7327c478bd9Sstevel@tonic-gate /* ARGSUSED */
7337c478bd9Sstevel@tonic-gate static int
rt_parmsout(void * prmsp,pc_vaparms_t * vaparmsp)7347c478bd9Sstevel@tonic-gate rt_parmsout(void *prmsp, pc_vaparms_t *vaparmsp)
7357c478bd9Sstevel@tonic-gate {
7367c478bd9Sstevel@tonic-gate 	rtkparms_t	*rtkprmsp = (rtkparms_t *)prmsp;
7377c478bd9Sstevel@tonic-gate 
7387c478bd9Sstevel@tonic-gate 	if (vaparmsp != NULL)
7397c478bd9Sstevel@tonic-gate 		return (0);
7407c478bd9Sstevel@tonic-gate 
7417c478bd9Sstevel@tonic-gate 	if (rtkprmsp->rt_tqntm < 0) {
7427c478bd9Sstevel@tonic-gate 		/*
7437c478bd9Sstevel@tonic-gate 		 * Quantum field set to special value (e.g. RT_TQINF)
7447c478bd9Sstevel@tonic-gate 		 */
7457c478bd9Sstevel@tonic-gate 		((rtparms_t *)rtkprmsp)->rt_tqnsecs = rtkprmsp->rt_tqntm;
7467c478bd9Sstevel@tonic-gate 		((rtparms_t *)rtkprmsp)->rt_tqsecs = 0;
7477c478bd9Sstevel@tonic-gate 	} else {
7487c478bd9Sstevel@tonic-gate 		/* Convert quantum from ticks to seconds-nanoseconds */
7497c478bd9Sstevel@tonic-gate 
7507c478bd9Sstevel@tonic-gate 		timestruc_t ts;
7517c478bd9Sstevel@tonic-gate 		TICK_TO_TIMESTRUC(rtkprmsp->rt_tqntm, &ts);
7527c478bd9Sstevel@tonic-gate 		((rtparms_t *)rtkprmsp)->rt_tqsecs = ts.tv_sec;
7537c478bd9Sstevel@tonic-gate 		((rtparms_t *)rtkprmsp)->rt_tqnsecs = ts.tv_nsec;
7547c478bd9Sstevel@tonic-gate 	}
7557c478bd9Sstevel@tonic-gate 
7567c478bd9Sstevel@tonic-gate 	return (0);
7577c478bd9Sstevel@tonic-gate }
7587c478bd9Sstevel@tonic-gate 
7597c478bd9Sstevel@tonic-gate 
7607c478bd9Sstevel@tonic-gate /*
7617c478bd9Sstevel@tonic-gate  * Copy all selected real-time class parameters to the user.
7627c478bd9Sstevel@tonic-gate  * The parameters are specified by a key.
7637c478bd9Sstevel@tonic-gate  */
7647c478bd9Sstevel@tonic-gate static int
rt_vaparmsout(void * prmsp,pc_vaparms_t * vaparmsp)7657c478bd9Sstevel@tonic-gate rt_vaparmsout(void *prmsp, pc_vaparms_t *vaparmsp)
7667c478bd9Sstevel@tonic-gate {
7677c478bd9Sstevel@tonic-gate 	rtkparms_t	*rtkprmsp = (rtkparms_t *)prmsp;
7687c478bd9Sstevel@tonic-gate 	timestruc_t	ts;
7697c478bd9Sstevel@tonic-gate 	uint_t		cnt;
7707c478bd9Sstevel@tonic-gate 	uint_t		secs;
7717c478bd9Sstevel@tonic-gate 	int		nsecs;
7727c478bd9Sstevel@tonic-gate 	int		priflag, secflag, nsecflag, sigflag;
7737c478bd9Sstevel@tonic-gate 	pc_vaparm_t	*vpp = &vaparmsp->pc_parms[0];
7747c478bd9Sstevel@tonic-gate 
7757c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_NOT_HELD(&curproc->p_lock));
7767c478bd9Sstevel@tonic-gate 
7777c478bd9Sstevel@tonic-gate 	priflag = secflag = nsecflag = sigflag = 0;
7787c478bd9Sstevel@tonic-gate 
7797c478bd9Sstevel@tonic-gate 	if (vaparmsp->pc_vaparmscnt > PC_VAPARMCNT)
7807c478bd9Sstevel@tonic-gate 		return (EINVAL);
7817c478bd9Sstevel@tonic-gate 
7827c478bd9Sstevel@tonic-gate 	if (rtkprmsp->rt_tqntm < 0) {
7837c478bd9Sstevel@tonic-gate 		/*
7847c478bd9Sstevel@tonic-gate 		 * Quantum field set to special value (e.g. RT_TQINF).
7857c478bd9Sstevel@tonic-gate 		 */
7867c478bd9Sstevel@tonic-gate 		secs = 0;
7877c478bd9Sstevel@tonic-gate 		nsecs = rtkprmsp->rt_tqntm;
7887c478bd9Sstevel@tonic-gate 	} else {
7897c478bd9Sstevel@tonic-gate 		/*
7907c478bd9Sstevel@tonic-gate 		 * Convert quantum from ticks to seconds-nanoseconds.
7917c478bd9Sstevel@tonic-gate 		 */
7927c478bd9Sstevel@tonic-gate 		TICK_TO_TIMESTRUC(rtkprmsp->rt_tqntm, &ts);
7937c478bd9Sstevel@tonic-gate 		secs = ts.tv_sec;
7947c478bd9Sstevel@tonic-gate 		nsecs = ts.tv_nsec;
7957c478bd9Sstevel@tonic-gate 	}
7967c478bd9Sstevel@tonic-gate 
7977c478bd9Sstevel@tonic-gate 
7987c478bd9Sstevel@tonic-gate 	for (cnt = 0; cnt < vaparmsp->pc_vaparmscnt; cnt++, vpp++) {
7997c478bd9Sstevel@tonic-gate 
8007c478bd9Sstevel@tonic-gate 		switch (vpp->pc_key) {
8017c478bd9Sstevel@tonic-gate 		case RT_KY_PRI:
8027c478bd9Sstevel@tonic-gate 			if (priflag++)
8037c478bd9Sstevel@tonic-gate 				return (EINVAL);
8047c478bd9Sstevel@tonic-gate 			if (copyout(&rtkprmsp->rt_pri,
8057c478bd9Sstevel@tonic-gate 			    (caddr_t)(uintptr_t)vpp->pc_parm, sizeof (pri_t)))
8067c478bd9Sstevel@tonic-gate 				return (EFAULT);
8077c478bd9Sstevel@tonic-gate 			break;
8087c478bd9Sstevel@tonic-gate 
8097c478bd9Sstevel@tonic-gate 		case RT_KY_TQSECS:
8107c478bd9Sstevel@tonic-gate 			if (secflag++)
8117c478bd9Sstevel@tonic-gate 				return (EINVAL);
8127c478bd9Sstevel@tonic-gate 			if (copyout(&secs, (caddr_t)(uintptr_t)vpp->pc_parm,
8137c478bd9Sstevel@tonic-gate 			    sizeof (uint_t)))
8147c478bd9Sstevel@tonic-gate 				return (EFAULT);
8157c478bd9Sstevel@tonic-gate 			break;
8167c478bd9Sstevel@tonic-gate 
8177c478bd9Sstevel@tonic-gate 		case RT_KY_TQNSECS:
8187c478bd9Sstevel@tonic-gate 			if (nsecflag++)
8197c478bd9Sstevel@tonic-gate 				return (EINVAL);
8207c478bd9Sstevel@tonic-gate 			if (copyout(&nsecs, (caddr_t)(uintptr_t)vpp->pc_parm,
8217c478bd9Sstevel@tonic-gate 			    sizeof (int)))
8227c478bd9Sstevel@tonic-gate 				return (EFAULT);
8237c478bd9Sstevel@tonic-gate 			break;
8247c478bd9Sstevel@tonic-gate 
8257c478bd9Sstevel@tonic-gate 		case RT_KY_TQSIG:
8267c478bd9Sstevel@tonic-gate 			if (sigflag++)
8277c478bd9Sstevel@tonic-gate 				return (EINVAL);
8287c478bd9Sstevel@tonic-gate 			if (copyout(&rtkprmsp->rt_tqsig,
8297c478bd9Sstevel@tonic-gate 			    (caddr_t)(uintptr_t)vpp->pc_parm, sizeof (int)))
8307c478bd9Sstevel@tonic-gate 				return (EFAULT);
8317c478bd9Sstevel@tonic-gate 			break;
8327c478bd9Sstevel@tonic-gate 
8337c478bd9Sstevel@tonic-gate 		default:
8347c478bd9Sstevel@tonic-gate 			return (EINVAL);
8357c478bd9Sstevel@tonic-gate 		}
8367c478bd9Sstevel@tonic-gate 	}
8377c478bd9Sstevel@tonic-gate 
8387c478bd9Sstevel@tonic-gate 	return (0);
8397c478bd9Sstevel@tonic-gate }
8407c478bd9Sstevel@tonic-gate 
8417c478bd9Sstevel@tonic-gate 
8427c478bd9Sstevel@tonic-gate /*
8437c478bd9Sstevel@tonic-gate  * Set the scheduling parameters of the thread pointed to by rtprocp
8447c478bd9Sstevel@tonic-gate  * to those specified in the buffer pointed to by rtkprmsp.
8457c478bd9Sstevel@tonic-gate  * Note that the parameters are expected to be in kernel format
8467c478bd9Sstevel@tonic-gate  * (i.e. time quantm expressed in ticks).  Real time parameters copied
8477c478bd9Sstevel@tonic-gate  * in from the user should be processed by rt_parmsin() before they are
8487c478bd9Sstevel@tonic-gate  * passed to this function.
8497c478bd9Sstevel@tonic-gate  */
8507c478bd9Sstevel@tonic-gate static int
rt_parmsset(kthread_t * tx,void * prmsp,id_t reqpcid,cred_t * reqpcredp)8517c478bd9Sstevel@tonic-gate rt_parmsset(kthread_t *tx, void *prmsp, id_t reqpcid, cred_t *reqpcredp)
8527c478bd9Sstevel@tonic-gate {
8537c478bd9Sstevel@tonic-gate 	rtkparms_t *rtkprmsp = (rtkparms_t *)prmsp;
8547c478bd9Sstevel@tonic-gate 	rtproc_t *rtpp = (rtproc_t *)tx->t_cldata;
8557c478bd9Sstevel@tonic-gate 
8567c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&(ttoproc(tx))->p_lock));
8577c478bd9Sstevel@tonic-gate 
8587c478bd9Sstevel@tonic-gate 	/*
8597c478bd9Sstevel@tonic-gate 	 * Basic permissions enforced by generic kernel code
8607c478bd9Sstevel@tonic-gate 	 * for all classes require that a thread attempting
8617c478bd9Sstevel@tonic-gate 	 * to change the scheduling parameters of a target thread
8627c478bd9Sstevel@tonic-gate 	 * be privileged or have a real or effective UID
8637c478bd9Sstevel@tonic-gate 	 * matching that of the target thread. We are not
8647c478bd9Sstevel@tonic-gate 	 * called unless these basic permission checks have
8657c478bd9Sstevel@tonic-gate 	 * already passed. The real-time class requires in addition
8667c478bd9Sstevel@tonic-gate 	 * that the requesting thread be real-time unless it is privileged.
8677c478bd9Sstevel@tonic-gate 	 * This may also have been checked previously but if our caller
8687c478bd9Sstevel@tonic-gate 	 * passes us a credential structure we assume it hasn't and
8697c478bd9Sstevel@tonic-gate 	 * we check it here.
8707c478bd9Sstevel@tonic-gate 	 */
8717c478bd9Sstevel@tonic-gate 	if (reqpcredp != NULL && reqpcid != rt_cid &&
872*24d819e6SJerry Jelinek 	    secpolicy_raisepriority(reqpcredp) != 0)
8737c478bd9Sstevel@tonic-gate 		return (EPERM);
8747c478bd9Sstevel@tonic-gate 
8757c478bd9Sstevel@tonic-gate 	thread_lock(tx);
8767c478bd9Sstevel@tonic-gate 	if ((rtkprmsp->rt_cflags & RT_DOPRI) != 0) {
8777c478bd9Sstevel@tonic-gate 		rtpp->rt_pri = rtkprmsp->rt_pri;
8787c478bd9Sstevel@tonic-gate 		rt_change_priority(tx, rtpp);
8797c478bd9Sstevel@tonic-gate 	}
8807c478bd9Sstevel@tonic-gate 	if (rtkprmsp->rt_tqntm == RT_TQINF)
8817c478bd9Sstevel@tonic-gate 		rtpp->rt_pquantum = RT_TQINF;
8827c478bd9Sstevel@tonic-gate 	else if (rtkprmsp->rt_tqntm == RT_TQDEF)
8837c478bd9Sstevel@tonic-gate 		rtpp->rt_timeleft = rtpp->rt_pquantum =
8847c478bd9Sstevel@tonic-gate 		    rt_dptbl[rtpp->rt_pri].rt_quantum;
8857c478bd9Sstevel@tonic-gate 	else if ((rtkprmsp->rt_cflags & RT_DOTQ) != 0)
8867c478bd9Sstevel@tonic-gate 		rtpp->rt_timeleft = rtpp->rt_pquantum = rtkprmsp->rt_tqntm;
8877c478bd9Sstevel@tonic-gate 
8887c478bd9Sstevel@tonic-gate 	if ((rtkprmsp->rt_cflags & RT_DOSIG) != 0)
8897c478bd9Sstevel@tonic-gate 		rtpp->rt_tqsignal = rtkprmsp->rt_tqsig;
8907c478bd9Sstevel@tonic-gate 
8917c478bd9Sstevel@tonic-gate 	thread_unlock(tx);
8927c478bd9Sstevel@tonic-gate 	return (0);
8937c478bd9Sstevel@tonic-gate }
8947c478bd9Sstevel@tonic-gate 
8957c478bd9Sstevel@tonic-gate 
8967c478bd9Sstevel@tonic-gate /*
8977c478bd9Sstevel@tonic-gate  * Arrange for thread to be placed in appropriate location
8987c478bd9Sstevel@tonic-gate  * on dispatcher queue.  Runs at splhi() since the clock
8997c478bd9Sstevel@tonic-gate  * interrupt can cause RTBACKQ to be set.
9007c478bd9Sstevel@tonic-gate  */
9017c478bd9Sstevel@tonic-gate static void
rt_preempt(kthread_t * t)9027c478bd9Sstevel@tonic-gate rt_preempt(kthread_t *t)
9037c478bd9Sstevel@tonic-gate {
9047c478bd9Sstevel@tonic-gate 	rtproc_t *rtpp = (rtproc_t *)(t->t_cldata);
9057c478bd9Sstevel@tonic-gate 	klwp_t *lwp;
9067c478bd9Sstevel@tonic-gate 
9077c478bd9Sstevel@tonic-gate 	ASSERT(THREAD_LOCK_HELD(t));
9087c478bd9Sstevel@tonic-gate 
9097c478bd9Sstevel@tonic-gate 	/*
9107c478bd9Sstevel@tonic-gate 	 * If the state is user I allow swapping because I know I won't
9117c478bd9Sstevel@tonic-gate 	 * be holding any locks.
9127c478bd9Sstevel@tonic-gate 	 */
9137c478bd9Sstevel@tonic-gate 	if ((lwp = curthread->t_lwp) != NULL && lwp->lwp_state == LWP_USER)
9147c478bd9Sstevel@tonic-gate 		t->t_schedflag &= ~TS_DONT_SWAP;
9157c478bd9Sstevel@tonic-gate 	if ((rtpp->rt_flags & RTBACKQ) != 0) {
9167c478bd9Sstevel@tonic-gate 		rtpp->rt_timeleft = rtpp->rt_pquantum;
9177c478bd9Sstevel@tonic-gate 		rtpp->rt_flags &= ~RTBACKQ;
9187c478bd9Sstevel@tonic-gate 		setbackdq(t);
9197c478bd9Sstevel@tonic-gate 	} else
9207c478bd9Sstevel@tonic-gate 		setfrontdq(t);
9217c478bd9Sstevel@tonic-gate 
9227c478bd9Sstevel@tonic-gate }
9237c478bd9Sstevel@tonic-gate 
9247c478bd9Sstevel@tonic-gate /*
9257c478bd9Sstevel@tonic-gate  * Return the global priority associated with this rt_pri.
9267c478bd9Sstevel@tonic-gate  */
9277c478bd9Sstevel@tonic-gate static pri_t
rt_globpri(kthread_t * t)9287c478bd9Sstevel@tonic-gate rt_globpri(kthread_t *t)
9297c478bd9Sstevel@tonic-gate {
9307c478bd9Sstevel@tonic-gate 	rtproc_t *rtprocp = (rtproc_t *)t->t_cldata;
9317c478bd9Sstevel@tonic-gate 	return (rt_dptbl[rtprocp->rt_pri].rt_globpri);
9327c478bd9Sstevel@tonic-gate }
9337c478bd9Sstevel@tonic-gate 
9347c478bd9Sstevel@tonic-gate static void
rt_setrun(kthread_t * t)9357c478bd9Sstevel@tonic-gate rt_setrun(kthread_t *t)
9367c478bd9Sstevel@tonic-gate {
9377c478bd9Sstevel@tonic-gate 	rtproc_t *rtpp = (rtproc_t *)(t->t_cldata);
9387c478bd9Sstevel@tonic-gate 
9397c478bd9Sstevel@tonic-gate 	ASSERT(THREAD_LOCK_HELD(t));
9407c478bd9Sstevel@tonic-gate 
9417c478bd9Sstevel@tonic-gate 	rtpp->rt_timeleft = rtpp->rt_pquantum;
9427c478bd9Sstevel@tonic-gate 	rtpp->rt_flags &= ~RTBACKQ;
9437c478bd9Sstevel@tonic-gate 	setbackdq(t);
9447c478bd9Sstevel@tonic-gate }
9457c478bd9Sstevel@tonic-gate 
9467c478bd9Sstevel@tonic-gate /*
9477c478bd9Sstevel@tonic-gate  * Returns the priority of the thread, -1 if the thread is loaded or ineligible
9487c478bd9Sstevel@tonic-gate  * for swapin.
9497c478bd9Sstevel@tonic-gate  *
9507c478bd9Sstevel@tonic-gate  * FX and RT threads are designed so that they don't swapout; however, it
9517c478bd9Sstevel@tonic-gate  * is possible that while the thread is swapped out and in another class, it
9527c478bd9Sstevel@tonic-gate  * can be changed to FX or RT.  Since these threads should be swapped in as
9537c478bd9Sstevel@tonic-gate  * soon as they're runnable, rt_swapin returns SHRT_MAX, and fx_swapin
9547c478bd9Sstevel@tonic-gate  * returns SHRT_MAX - 1, so that it gives deference to any swapped out RT
9557c478bd9Sstevel@tonic-gate  * threads.
9567c478bd9Sstevel@tonic-gate  */
9577c478bd9Sstevel@tonic-gate /* ARGSUSED */
9587c478bd9Sstevel@tonic-gate static pri_t
rt_swapin(kthread_t * t,int flags)9597c478bd9Sstevel@tonic-gate rt_swapin(kthread_t *t, int flags)
9607c478bd9Sstevel@tonic-gate {
9617c478bd9Sstevel@tonic-gate 	pri_t	tpri = -1;
9627c478bd9Sstevel@tonic-gate 
9637c478bd9Sstevel@tonic-gate 	ASSERT(THREAD_LOCK_HELD(t));
9647c478bd9Sstevel@tonic-gate 
9657c478bd9Sstevel@tonic-gate 	if (t->t_state == TS_RUN && (t->t_schedflag & TS_LOAD) == 0) {
9667c478bd9Sstevel@tonic-gate 		tpri = (pri_t)SHRT_MAX;
9677c478bd9Sstevel@tonic-gate 	}
9687c478bd9Sstevel@tonic-gate 
9697c478bd9Sstevel@tonic-gate 	return (tpri);
9707c478bd9Sstevel@tonic-gate }
9717c478bd9Sstevel@tonic-gate 
9727c478bd9Sstevel@tonic-gate /*
9737c478bd9Sstevel@tonic-gate  * Return an effective priority for swapout.
9747c478bd9Sstevel@tonic-gate  */
9757c478bd9Sstevel@tonic-gate /* ARGSUSED */
9767c478bd9Sstevel@tonic-gate static pri_t
rt_swapout(kthread_t * t,int flags)9777c478bd9Sstevel@tonic-gate rt_swapout(kthread_t *t, int flags)
9787c478bd9Sstevel@tonic-gate {
9797c478bd9Sstevel@tonic-gate 	ASSERT(THREAD_LOCK_HELD(t));
9807c478bd9Sstevel@tonic-gate 
9817c478bd9Sstevel@tonic-gate 	return (-1);
9827c478bd9Sstevel@tonic-gate }
9837c478bd9Sstevel@tonic-gate 
9847c478bd9Sstevel@tonic-gate /*
9857c478bd9Sstevel@tonic-gate  * Check for time slice expiration (unless thread has infinite time
9867c478bd9Sstevel@tonic-gate  * slice).  If time slice has expired arrange for thread to be preempted
9877c478bd9Sstevel@tonic-gate  * and placed on back of queue.
9887c478bd9Sstevel@tonic-gate  */
9897c478bd9Sstevel@tonic-gate static void
rt_tick(kthread_t * t)9907c478bd9Sstevel@tonic-gate rt_tick(kthread_t *t)
9917c478bd9Sstevel@tonic-gate {
9927c478bd9Sstevel@tonic-gate 	rtproc_t *rtpp = (rtproc_t *)(t->t_cldata);
9937c478bd9Sstevel@tonic-gate 
9947c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&(ttoproc(t))->p_lock));
9957c478bd9Sstevel@tonic-gate 
9967c478bd9Sstevel@tonic-gate 	thread_lock(t);
9977c478bd9Sstevel@tonic-gate 	if ((rtpp->rt_pquantum != RT_TQINF && --rtpp->rt_timeleft == 0) ||
998b3383343Smishra 	    (t->t_state == TS_ONPROC && DISP_MUST_SURRENDER(t))) {
9997c478bd9Sstevel@tonic-gate 		if (rtpp->rt_timeleft == 0 && rtpp->rt_tqsignal) {
10007c478bd9Sstevel@tonic-gate 			thread_unlock(t);
10017c478bd9Sstevel@tonic-gate 			sigtoproc(ttoproc(t), t, rtpp->rt_tqsignal);
10027c478bd9Sstevel@tonic-gate 			thread_lock(t);
10037c478bd9Sstevel@tonic-gate 		}
10047c478bd9Sstevel@tonic-gate 		rtpp->rt_flags |= RTBACKQ;
10057c478bd9Sstevel@tonic-gate 		cpu_surrender(t);
10067c478bd9Sstevel@tonic-gate 	}
10077c478bd9Sstevel@tonic-gate 	thread_unlock(t);
10087c478bd9Sstevel@tonic-gate }
10097c478bd9Sstevel@tonic-gate 
10107c478bd9Sstevel@tonic-gate 
10117c478bd9Sstevel@tonic-gate /*
10127c478bd9Sstevel@tonic-gate  * Place the thread waking up on the dispatcher queue.
10137c478bd9Sstevel@tonic-gate  */
10147c478bd9Sstevel@tonic-gate static void
rt_wakeup(kthread_t * t)10157c478bd9Sstevel@tonic-gate rt_wakeup(kthread_t *t)
10167c478bd9Sstevel@tonic-gate {
10177c478bd9Sstevel@tonic-gate 	rtproc_t *rtpp = (rtproc_t *)(t->t_cldata);
10187c478bd9Sstevel@tonic-gate 
10197c478bd9Sstevel@tonic-gate 	ASSERT(THREAD_LOCK_HELD(t));
10207c478bd9Sstevel@tonic-gate 
10217c478bd9Sstevel@tonic-gate 	rtpp->rt_timeleft = rtpp->rt_pquantum;
10227c478bd9Sstevel@tonic-gate 	rtpp->rt_flags &= ~RTBACKQ;
10237c478bd9Sstevel@tonic-gate 	setbackdq(t);
10247c478bd9Sstevel@tonic-gate }
10257c478bd9Sstevel@tonic-gate 
10267c478bd9Sstevel@tonic-gate static void
rt_yield(kthread_t * t)10277c478bd9Sstevel@tonic-gate rt_yield(kthread_t *t)
10287c478bd9Sstevel@tonic-gate {
10297c478bd9Sstevel@tonic-gate 	rtproc_t *rtpp = (rtproc_t *)(t->t_cldata);
10307c478bd9Sstevel@tonic-gate 
10317c478bd9Sstevel@tonic-gate 	ASSERT(t == curthread);
10327c478bd9Sstevel@tonic-gate 	ASSERT(THREAD_LOCK_HELD(t));
10337c478bd9Sstevel@tonic-gate 
10347c478bd9Sstevel@tonic-gate 	rtpp->rt_flags &= ~RTBACKQ;
10357c478bd9Sstevel@tonic-gate 	setbackdq(t);
10367c478bd9Sstevel@tonic-gate }
10377c478bd9Sstevel@tonic-gate 
10387c478bd9Sstevel@tonic-gate /* ARGSUSED */
10397c478bd9Sstevel@tonic-gate static int
rt_donice(kthread_t * t,cred_t * cr,int incr,int * retvalp)10407c478bd9Sstevel@tonic-gate rt_donice(kthread_t *t, cred_t *cr, int incr, int *retvalp)
10417c478bd9Sstevel@tonic-gate {
10427c478bd9Sstevel@tonic-gate 	return (EINVAL);
10437c478bd9Sstevel@tonic-gate }
10447c478bd9Sstevel@tonic-gate 
1045d4204c85Sraf /*
1046d4204c85Sraf  * Increment the priority of the specified thread by incr and
1047d4204c85Sraf  * return the new value in *retvalp.
1048d4204c85Sraf  */
1049d4204c85Sraf static int
rt_doprio(kthread_t * t,cred_t * cr,int incr,int * retvalp)1050d4204c85Sraf rt_doprio(kthread_t *t, cred_t *cr, int incr, int *retvalp)
1051d4204c85Sraf {
1052d4204c85Sraf 	int newpri;
1053d4204c85Sraf 	rtproc_t *rtpp = (rtproc_t *)(t->t_cldata);
1054d4204c85Sraf 	rtkparms_t rtkparms;
1055d4204c85Sraf 
1056d4204c85Sraf 	/* If there's no change to the priority, just return current setting */
1057d4204c85Sraf 	if (incr == 0) {
1058d4204c85Sraf 		*retvalp = rtpp->rt_pri;
1059d4204c85Sraf 		return (0);
1060d4204c85Sraf 	}
1061d4204c85Sraf 
1062d4204c85Sraf 	newpri = rtpp->rt_pri + incr;
1063d4204c85Sraf 	if (newpri > rt_maxpri || newpri < 0)
1064d4204c85Sraf 		return (EINVAL);
1065d4204c85Sraf 
1066d4204c85Sraf 	*retvalp = newpri;
1067d4204c85Sraf 	rtkparms.rt_pri = newpri;
1068d4204c85Sraf 	rtkparms.rt_tqntm = RT_NOCHANGE;
1069d4204c85Sraf 	rtkparms.rt_tqsig = 0;
1070d4204c85Sraf 	rtkparms.rt_cflags = RT_DOPRI;
1071d4204c85Sraf 	return (rt_parmsset(t, &rtkparms, rt_cid, cr));
1072d4204c85Sraf }
1073d4204c85Sraf 
10747c478bd9Sstevel@tonic-gate static int
rt_alloc(void ** p,int flag)10757c478bd9Sstevel@tonic-gate rt_alloc(void **p, int flag)
10767c478bd9Sstevel@tonic-gate {
10777c478bd9Sstevel@tonic-gate 	void *bufp;
10787c478bd9Sstevel@tonic-gate 	bufp = kmem_alloc(sizeof (rtproc_t), flag);
10797c478bd9Sstevel@tonic-gate 	if (bufp == NULL) {
10807c478bd9Sstevel@tonic-gate 		return (ENOMEM);
10817c478bd9Sstevel@tonic-gate 	} else {
10827c478bd9Sstevel@tonic-gate 		*p = bufp;
10837c478bd9Sstevel@tonic-gate 		return (0);
10847c478bd9Sstevel@tonic-gate 	}
10857c478bd9Sstevel@tonic-gate }
10867c478bd9Sstevel@tonic-gate 
10877c478bd9Sstevel@tonic-gate static void
rt_free(void * bufp)10887c478bd9Sstevel@tonic-gate rt_free(void *bufp)
10897c478bd9Sstevel@tonic-gate {
10907c478bd9Sstevel@tonic-gate 	if (bufp)
10917c478bd9Sstevel@tonic-gate 		kmem_free(bufp, sizeof (rtproc_t));
10927c478bd9Sstevel@tonic-gate }
10937c478bd9Sstevel@tonic-gate 
10947c478bd9Sstevel@tonic-gate static void
rt_change_priority(kthread_t * t,rtproc_t * rtpp)10957c478bd9Sstevel@tonic-gate rt_change_priority(kthread_t *t, rtproc_t *rtpp)
10967c478bd9Sstevel@tonic-gate {
10977c478bd9Sstevel@tonic-gate 	pri_t new_pri;
10987c478bd9Sstevel@tonic-gate 
10997c478bd9Sstevel@tonic-gate 	ASSERT(THREAD_LOCK_HELD(t));
11007c478bd9Sstevel@tonic-gate 
11017c478bd9Sstevel@tonic-gate 	new_pri = rt_dptbl[rtpp->rt_pri].rt_globpri;
11027c478bd9Sstevel@tonic-gate 
1103d4204c85Sraf 	t->t_cpri = rtpp->rt_pri;
11047c478bd9Sstevel@tonic-gate 	if (t == curthread || t->t_state == TS_ONPROC) {
11057c478bd9Sstevel@tonic-gate 		cpu_t	*cp = t->t_disp_queue->disp_cpu;
11067c478bd9Sstevel@tonic-gate 		THREAD_CHANGE_PRI(t, new_pri);
11077c478bd9Sstevel@tonic-gate 		if (t == cp->cpu_dispthread)
11087c478bd9Sstevel@tonic-gate 			cp->cpu_dispatch_pri = DISP_PRIO(t);
11097c478bd9Sstevel@tonic-gate 		if (DISP_MUST_SURRENDER(t)) {
11107c478bd9Sstevel@tonic-gate 			rtpp->rt_flags |= RTBACKQ;
11117c478bd9Sstevel@tonic-gate 			cpu_surrender(t);
11127c478bd9Sstevel@tonic-gate 		} else {
11137c478bd9Sstevel@tonic-gate 			rtpp->rt_timeleft = rtpp->rt_pquantum;
11147c478bd9Sstevel@tonic-gate 		}
11157c478bd9Sstevel@tonic-gate 	} else {
11167c478bd9Sstevel@tonic-gate 		/*
11177c478bd9Sstevel@tonic-gate 		 * When the priority of a thread is changed,
11187c478bd9Sstevel@tonic-gate 		 * it may be necessary to adjust its position
11197c478bd9Sstevel@tonic-gate 		 * on a sleep queue or dispatch queue.  The
11207c478bd9Sstevel@tonic-gate 		 * function thread_change_pri() accomplishes this.
11217c478bd9Sstevel@tonic-gate 		 */
11227c478bd9Sstevel@tonic-gate 		if (thread_change_pri(t, new_pri, 0)) {
11237c478bd9Sstevel@tonic-gate 			/*
11247c478bd9Sstevel@tonic-gate 			 * The thread was on a run queue.
11257c478bd9Sstevel@tonic-gate 			 * Reset its CPU timeleft.
11267c478bd9Sstevel@tonic-gate 			 */
11277c478bd9Sstevel@tonic-gate 			rtpp->rt_timeleft = rtpp->rt_pquantum;
11287c478bd9Sstevel@tonic-gate 		} else {
11297c478bd9Sstevel@tonic-gate 			rtpp->rt_flags |= RTBACKQ;
11307c478bd9Sstevel@tonic-gate 		}
11317c478bd9Sstevel@tonic-gate 	}
11327c478bd9Sstevel@tonic-gate }
1133