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