xref: /titanic_44/usr/src/uts/common/disp/priocntl.c (revision 35a5a3587fd94b666239c157d3722745250ccbd7)
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
59acbbeafSnn35248  * Common Development and Distribution License (the "License").
69acbbeafSnn35248  * 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 /*
23*35a5a358SJonathan Adams  * Copyright 2009 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 #include <sys/types.h>
317c478bd9Sstevel@tonic-gate #include <sys/param.h>
327c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
337c478bd9Sstevel@tonic-gate #include <sys/signal.h>
347c478bd9Sstevel@tonic-gate #include <sys/pcb.h>
357c478bd9Sstevel@tonic-gate #include <sys/user.h>
367c478bd9Sstevel@tonic-gate #include <sys/systm.h>
377c478bd9Sstevel@tonic-gate #include <sys/sysinfo.h>
387c478bd9Sstevel@tonic-gate #include <sys/var.h>
397c478bd9Sstevel@tonic-gate #include <sys/errno.h>
407c478bd9Sstevel@tonic-gate #include <sys/cred.h>
417c478bd9Sstevel@tonic-gate #include <sys/proc.h>
427c478bd9Sstevel@tonic-gate #include <sys/procset.h>
437c478bd9Sstevel@tonic-gate #include <sys/debug.h>
447c478bd9Sstevel@tonic-gate #include <sys/inline.h>
457c478bd9Sstevel@tonic-gate #include <sys/priocntl.h>
467c478bd9Sstevel@tonic-gate #include <sys/disp.h>
477c478bd9Sstevel@tonic-gate #include <sys/class.h>
487c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
497c478bd9Sstevel@tonic-gate #include <sys/t_lock.h>
507c478bd9Sstevel@tonic-gate #include <sys/uadmin.h>
517c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
527c478bd9Sstevel@tonic-gate #include <sys/policy.h>
53d4204c85Sraf #include <sys/schedctl.h>
547c478bd9Sstevel@tonic-gate 
557c478bd9Sstevel@tonic-gate /*
567c478bd9Sstevel@tonic-gate  * Structure used to pass arguments to the proccmp() function.
577c478bd9Sstevel@tonic-gate  * The arguments must be passed in a structure because proccmp()
587c478bd9Sstevel@tonic-gate  * is called indirectly through the dotoprocs() function which
597c478bd9Sstevel@tonic-gate  * will only pass through a single one word argument.
607c478bd9Sstevel@tonic-gate  */
617c478bd9Sstevel@tonic-gate struct pcmpargs {
627c478bd9Sstevel@tonic-gate 	id_t	*pcmp_cidp;
637c478bd9Sstevel@tonic-gate 	int	*pcmp_cntp;
64d4204c85Sraf 	kthread_t **pcmp_retthreadp;
657c478bd9Sstevel@tonic-gate };
667c478bd9Sstevel@tonic-gate 
677c478bd9Sstevel@tonic-gate /*
687c478bd9Sstevel@tonic-gate  * Structure used to pass arguments to the setparms() function
697c478bd9Sstevel@tonic-gate  * which is called indirectly through dotoprocs().
707c478bd9Sstevel@tonic-gate  */
717c478bd9Sstevel@tonic-gate struct stprmargs {
727c478bd9Sstevel@tonic-gate 	struct pcparms	*stp_parmsp;	/* pointer to parameters */
737c478bd9Sstevel@tonic-gate 	int		stp_error;	/* some errors returned here */
747c478bd9Sstevel@tonic-gate };
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate #if defined(_SYSCALL32_IMPL) && _LONG_LONG_ALIGNMENT_32 == 4
777c478bd9Sstevel@tonic-gate /*
787c478bd9Sstevel@tonic-gate  * A vaparm_t is an int followed by a long long -- this packs differently
797c478bd9Sstevel@tonic-gate  * between the 64-bit kernel ABI and the 32-bit user ABI.
807c478bd9Sstevel@tonic-gate  */
817c478bd9Sstevel@tonic-gate static int
copyin_vaparms32(caddr_t arg,pc_vaparms_t * vap,uio_seg_t seg)829acbbeafSnn35248 copyin_vaparms32(caddr_t arg, pc_vaparms_t *vap, uio_seg_t seg)
837c478bd9Sstevel@tonic-gate {
847c478bd9Sstevel@tonic-gate 	pc_vaparms32_t vaparms32;
857c478bd9Sstevel@tonic-gate 	pc_vaparm32_t *src;
867c478bd9Sstevel@tonic-gate 	pc_vaparm_t *dst;
877c478bd9Sstevel@tonic-gate 	uint_t cnt;
887c478bd9Sstevel@tonic-gate 
897c478bd9Sstevel@tonic-gate 	ASSERT(get_udatamodel() == DATAMODEL_ILP32);
907c478bd9Sstevel@tonic-gate 
919acbbeafSnn35248 	if ((seg == UIO_USERSPACE ? copyin : kcopy)(arg, &vaparms32,
929acbbeafSnn35248 	    sizeof (vaparms32)))
937c478bd9Sstevel@tonic-gate 		return (EFAULT);
947c478bd9Sstevel@tonic-gate 
957c478bd9Sstevel@tonic-gate 	vap->pc_vaparmscnt = vaparms32.pc_vaparmscnt;
967c478bd9Sstevel@tonic-gate 	if ((cnt = vaparms32.pc_vaparmscnt) > PC_VAPARMCNT)
977c478bd9Sstevel@tonic-gate 		cnt = PC_VAPARMCNT;
987c478bd9Sstevel@tonic-gate 	for (src = vaparms32.pc_parms, dst = vap->pc_parms;
997c478bd9Sstevel@tonic-gate 	    cnt--; src++, dst++) {
1007c478bd9Sstevel@tonic-gate 		dst->pc_key = src->pc_key;
1017c478bd9Sstevel@tonic-gate 		dst->pc_parm = src->pc_parm;
1027c478bd9Sstevel@tonic-gate 	}
1037c478bd9Sstevel@tonic-gate 	return (0);
1047c478bd9Sstevel@tonic-gate }
1057c478bd9Sstevel@tonic-gate 
1069acbbeafSnn35248 #define	COPYIN_VAPARMS(arg, vap, size, seg)	\
1077c478bd9Sstevel@tonic-gate 	(get_udatamodel() == DATAMODEL_NATIVE ?	\
1089acbbeafSnn35248 	(*copyinfn)(arg, vap, size) : copyin_vaparms32(arg, vap, seg))
1097c478bd9Sstevel@tonic-gate 
1107c478bd9Sstevel@tonic-gate #else
1117c478bd9Sstevel@tonic-gate 
1129acbbeafSnn35248 #define	COPYIN_VAPARMS(arg, vap, size, seg)	(*copyinfn)(arg, vap, size)
1137c478bd9Sstevel@tonic-gate 
1147c478bd9Sstevel@tonic-gate #endif
1157c478bd9Sstevel@tonic-gate 
1167c478bd9Sstevel@tonic-gate static int donice(procset_t *, pcnice_t *);
117d4204c85Sraf static int doprio(procset_t *, pcprio_t *);
1187c478bd9Sstevel@tonic-gate static int proccmp(proc_t *, struct pcmpargs *);
1197c478bd9Sstevel@tonic-gate static int setparms(proc_t *, struct stprmargs *);
120d4204c85Sraf extern int threadcmp(struct pcmpargs *, kthread_t *);
1217c478bd9Sstevel@tonic-gate 
1227c478bd9Sstevel@tonic-gate /*
1237c478bd9Sstevel@tonic-gate  * The priocntl system call.
1247c478bd9Sstevel@tonic-gate  */
1257c478bd9Sstevel@tonic-gate long
priocntl_common(int pc_version,procset_t * psp,int cmd,caddr_t arg,caddr_t arg2,uio_seg_t seg)1269acbbeafSnn35248 priocntl_common(int pc_version, procset_t *psp, int cmd, caddr_t arg,
1279acbbeafSnn35248     caddr_t arg2, uio_seg_t seg)
1287c478bd9Sstevel@tonic-gate {
1297c478bd9Sstevel@tonic-gate 	pcinfo_t		pcinfo;
1307c478bd9Sstevel@tonic-gate 	pcparms_t		pcparms;
1317c478bd9Sstevel@tonic-gate 	pcnice_t		pcnice;
132d4204c85Sraf 	pcprio_t		pcprio;
1337c478bd9Sstevel@tonic-gate 	pcadmin_t		pcadmin;
1347c478bd9Sstevel@tonic-gate 	pcpri_t			pcpri;
1357c478bd9Sstevel@tonic-gate 	procset_t		procset;
1367c478bd9Sstevel@tonic-gate 	struct stprmargs	stprmargs;
1377c478bd9Sstevel@tonic-gate 	struct pcmpargs		pcmpargs;
1387c478bd9Sstevel@tonic-gate 	pc_vaparms_t		vaparms;
1397c478bd9Sstevel@tonic-gate 	char			clname[PC_CLNMSZ];
1400209230bSgjelinek 	char			*outstr;
1417c478bd9Sstevel@tonic-gate 	int			count;
142d4204c85Sraf 	kthread_t		*retthreadp;
1437c478bd9Sstevel@tonic-gate 	proc_t			*initpp;
1447c478bd9Sstevel@tonic-gate 	int			clnullflag;
1457c478bd9Sstevel@tonic-gate 	int			error = 0;
1467c478bd9Sstevel@tonic-gate 	int			error1 = 0;
1477c478bd9Sstevel@tonic-gate 	int			rv = 0;
1487c478bd9Sstevel@tonic-gate 	pid_t			saved_pid;
1497c478bd9Sstevel@tonic-gate 	id_t			classid;
1500209230bSgjelinek 	int			size;
1519acbbeafSnn35248 	int (*copyinfn)(const void *, void *, size_t);
1529acbbeafSnn35248 	int (*copyoutfn)(const void *, void *, size_t);
1537c478bd9Sstevel@tonic-gate 
1547c478bd9Sstevel@tonic-gate 	/*
1557c478bd9Sstevel@tonic-gate 	 * First just check the version number. Right now there is only
1567c478bd9Sstevel@tonic-gate 	 * one version we know about and support.  If we get some other
1577c478bd9Sstevel@tonic-gate 	 * version number from the application it may be that the
1587c478bd9Sstevel@tonic-gate 	 * application was built with some future version and is trying
1597c478bd9Sstevel@tonic-gate 	 * to run on an old release of the system (that's us).  In any
1607c478bd9Sstevel@tonic-gate 	 * case if we don't recognize the version number all we can do is
1617c478bd9Sstevel@tonic-gate 	 * return error.
1627c478bd9Sstevel@tonic-gate 	 */
1637c478bd9Sstevel@tonic-gate 	if (pc_version != PC_VERSION)
1647c478bd9Sstevel@tonic-gate 		return (set_errno(EINVAL));
1657c478bd9Sstevel@tonic-gate 
1669acbbeafSnn35248 	if (seg == UIO_USERSPACE) {
1679acbbeafSnn35248 		copyinfn = copyin;
1689acbbeafSnn35248 		copyoutfn = copyout;
1699acbbeafSnn35248 	} else {
1709acbbeafSnn35248 		copyinfn = kcopy;
1719acbbeafSnn35248 		copyoutfn = kcopy;
1729acbbeafSnn35248 	}
1739acbbeafSnn35248 
1747c478bd9Sstevel@tonic-gate 	switch (cmd) {
1757c478bd9Sstevel@tonic-gate 	case PC_GETCID:
1767c478bd9Sstevel@tonic-gate 		/*
1777c478bd9Sstevel@tonic-gate 		 * If the arg pointer is NULL, the user just wants to
1787c478bd9Sstevel@tonic-gate 		 * know the number of classes. If non-NULL, the pointer
1797c478bd9Sstevel@tonic-gate 		 * should point to a valid user pcinfo buffer.  In the
1807c478bd9Sstevel@tonic-gate 		 * dynamic world we need to return the number of loaded
1817c478bd9Sstevel@tonic-gate 		 * classes, not the max number of available classes that
1827c478bd9Sstevel@tonic-gate 		 * can be loaded.
1837c478bd9Sstevel@tonic-gate 		 */
1847c478bd9Sstevel@tonic-gate 		if (arg == NULL) {
1857c478bd9Sstevel@tonic-gate 			rv = loaded_classes;
1867c478bd9Sstevel@tonic-gate 			break;
1877c478bd9Sstevel@tonic-gate 		} else {
1889acbbeafSnn35248 			if ((*copyinfn)(arg, &pcinfo, sizeof (pcinfo)))
1897c478bd9Sstevel@tonic-gate 				return (set_errno(EFAULT));
1907c478bd9Sstevel@tonic-gate 		}
1917c478bd9Sstevel@tonic-gate 
1927c478bd9Sstevel@tonic-gate 		pcinfo.pc_clname[PC_CLNMSZ-1] = '\0';
1937c478bd9Sstevel@tonic-gate 
1947c478bd9Sstevel@tonic-gate 		/*
1957c478bd9Sstevel@tonic-gate 		 * Get the class ID corresponding to user supplied name.
1967c478bd9Sstevel@tonic-gate 		 */
1977c478bd9Sstevel@tonic-gate 		error = getcid(pcinfo.pc_clname, &pcinfo.pc_cid);
1987c478bd9Sstevel@tonic-gate 		if (error)
1997c478bd9Sstevel@tonic-gate 			return (set_errno(error));
2007c478bd9Sstevel@tonic-gate 
2017c478bd9Sstevel@tonic-gate 		/*
2027c478bd9Sstevel@tonic-gate 		 * Can't get info about the sys class.
2037c478bd9Sstevel@tonic-gate 		 */
2047c478bd9Sstevel@tonic-gate 		if (pcinfo.pc_cid == 0)
2057c478bd9Sstevel@tonic-gate 			return (set_errno(EINVAL));
2067c478bd9Sstevel@tonic-gate 
2077c478bd9Sstevel@tonic-gate 		/*
2087c478bd9Sstevel@tonic-gate 		 * Get the class specific information.
2097c478bd9Sstevel@tonic-gate 		 * we MUST make sure that the class has not already
2107c478bd9Sstevel@tonic-gate 		 * been unloaded before we try the CL_GETCLINFO.
2117c478bd9Sstevel@tonic-gate 		 * If it has then we need to load it.
2127c478bd9Sstevel@tonic-gate 		 */
2137c478bd9Sstevel@tonic-gate 		error =
2147c478bd9Sstevel@tonic-gate 		    scheduler_load(pcinfo.pc_clname, &sclass[pcinfo.pc_cid]);
2157c478bd9Sstevel@tonic-gate 		if (error)
2167c478bd9Sstevel@tonic-gate 			return (set_errno(error));
2177c478bd9Sstevel@tonic-gate 		error = CL_GETCLINFO(&sclass[pcinfo.pc_cid], pcinfo.pc_clinfo);
2187c478bd9Sstevel@tonic-gate 		if (error)
2197c478bd9Sstevel@tonic-gate 			return (set_errno(error));
2207c478bd9Sstevel@tonic-gate 
2219acbbeafSnn35248 		if ((*copyoutfn)(&pcinfo, arg, sizeof (pcinfo)))
2227c478bd9Sstevel@tonic-gate 			return (set_errno(EFAULT));
2237c478bd9Sstevel@tonic-gate 
2247c478bd9Sstevel@tonic-gate 		rv = loaded_classes;
2257c478bd9Sstevel@tonic-gate 
2267c478bd9Sstevel@tonic-gate 		break;
2277c478bd9Sstevel@tonic-gate 
2287c478bd9Sstevel@tonic-gate 	case PC_GETCLINFO:
2297c478bd9Sstevel@tonic-gate 		/*
2307c478bd9Sstevel@tonic-gate 		 * If the arg pointer is NULL, the user just wants to know
2317c478bd9Sstevel@tonic-gate 		 * the number of classes. If non-NULL, the pointer should
2327c478bd9Sstevel@tonic-gate 		 * point to a valid user pcinfo buffer.
2337c478bd9Sstevel@tonic-gate 		 */
2347c478bd9Sstevel@tonic-gate 		if (arg == NULL) {
2357c478bd9Sstevel@tonic-gate 			rv = loaded_classes;
2367c478bd9Sstevel@tonic-gate 			break;
2377c478bd9Sstevel@tonic-gate 		} else {
2389acbbeafSnn35248 			if ((*copyinfn)(arg, &pcinfo, sizeof (pcinfo)))
2397c478bd9Sstevel@tonic-gate 				return (set_errno(EFAULT));
2407c478bd9Sstevel@tonic-gate 		}
2417c478bd9Sstevel@tonic-gate 
2427c478bd9Sstevel@tonic-gate 		if (pcinfo.pc_cid >= loaded_classes || pcinfo.pc_cid < 1)
2437c478bd9Sstevel@tonic-gate 			return (set_errno(EINVAL));
2447c478bd9Sstevel@tonic-gate 
2457c478bd9Sstevel@tonic-gate 		(void) strncpy(pcinfo.pc_clname, sclass[pcinfo.pc_cid].cl_name,
2467c478bd9Sstevel@tonic-gate 		    PC_CLNMSZ);
2477c478bd9Sstevel@tonic-gate 
2487c478bd9Sstevel@tonic-gate 		/*
2497c478bd9Sstevel@tonic-gate 		 * Get the class specific information.  we MUST make sure
2507c478bd9Sstevel@tonic-gate 		 * that the class has not already been unloaded before we
2517c478bd9Sstevel@tonic-gate 		 * try the CL_GETCLINFO.  If it has then we need to load
2527c478bd9Sstevel@tonic-gate 		 * it.
2537c478bd9Sstevel@tonic-gate 		 */
2547c478bd9Sstevel@tonic-gate 		error =
2557c478bd9Sstevel@tonic-gate 		    scheduler_load(pcinfo.pc_clname, &sclass[pcinfo.pc_cid]);
2567c478bd9Sstevel@tonic-gate 		if (error)
2577c478bd9Sstevel@tonic-gate 			return (set_errno(error));
2587c478bd9Sstevel@tonic-gate 		error = CL_GETCLINFO(&sclass[pcinfo.pc_cid], pcinfo.pc_clinfo);
2597c478bd9Sstevel@tonic-gate 		if (error)
2607c478bd9Sstevel@tonic-gate 			return (set_errno(error));
2617c478bd9Sstevel@tonic-gate 
2629acbbeafSnn35248 		if ((*copyoutfn)(&pcinfo, arg, sizeof (pcinfo)))
2637c478bd9Sstevel@tonic-gate 			return (set_errno(EFAULT));
2647c478bd9Sstevel@tonic-gate 
2657c478bd9Sstevel@tonic-gate 		rv = loaded_classes;
2667c478bd9Sstevel@tonic-gate 		break;
2677c478bd9Sstevel@tonic-gate 
2687c478bd9Sstevel@tonic-gate 	case PC_SETPARMS:
2697c478bd9Sstevel@tonic-gate 	case PC_SETXPARMS:
2707c478bd9Sstevel@tonic-gate 		/*
2717c478bd9Sstevel@tonic-gate 		 * First check the validity of the parameters we got from
2727c478bd9Sstevel@tonic-gate 		 * the user.  We don't do any permissions checking here
2737c478bd9Sstevel@tonic-gate 		 * because it's done on a per thread basis by parmsset().
2747c478bd9Sstevel@tonic-gate 		 */
2757c478bd9Sstevel@tonic-gate 		if (cmd == PC_SETPARMS) {
2769acbbeafSnn35248 			if ((*copyinfn)(arg, &pcparms, sizeof (pcparms)))
2777c478bd9Sstevel@tonic-gate 				return (set_errno(EFAULT));
2787c478bd9Sstevel@tonic-gate 
2797c478bd9Sstevel@tonic-gate 			error = parmsin(&pcparms, NULL);
2807c478bd9Sstevel@tonic-gate 		} else {
2819acbbeafSnn35248 			if ((*copyinfn)(arg, clname, PC_CLNMSZ) ||
2829acbbeafSnn35248 			    COPYIN_VAPARMS(arg2, &vaparms, sizeof (vaparms),
2839acbbeafSnn35248 			    seg))
2847c478bd9Sstevel@tonic-gate 				return (set_errno(EFAULT));
2857c478bd9Sstevel@tonic-gate 			clname[PC_CLNMSZ-1] = '\0';
2867c478bd9Sstevel@tonic-gate 
2877c478bd9Sstevel@tonic-gate 			if (getcid(clname, &pcparms.pc_cid))
2887c478bd9Sstevel@tonic-gate 				return (set_errno(EINVAL));
2897c478bd9Sstevel@tonic-gate 
2907c478bd9Sstevel@tonic-gate 			error = parmsin(&pcparms, &vaparms);
2917c478bd9Sstevel@tonic-gate 		}
2927c478bd9Sstevel@tonic-gate 
2937c478bd9Sstevel@tonic-gate 		if (error)
2947c478bd9Sstevel@tonic-gate 			return (set_errno(error));
2957c478bd9Sstevel@tonic-gate 
2967c478bd9Sstevel@tonic-gate 		/*
2977c478bd9Sstevel@tonic-gate 		 * Get the procset from the user.
2987c478bd9Sstevel@tonic-gate 		 */
2999acbbeafSnn35248 		if ((*copyinfn)(psp, &procset, sizeof (procset)))
3007c478bd9Sstevel@tonic-gate 			return (set_errno(EFAULT));
3017c478bd9Sstevel@tonic-gate 
3027c478bd9Sstevel@tonic-gate 		/*
3037c478bd9Sstevel@tonic-gate 		 * For performance we do a quick check here to catch
3047c478bd9Sstevel@tonic-gate 		 * common cases where the current thread is the only one
3057c478bd9Sstevel@tonic-gate 		 * in the set.  In such cases we can call parmsset()
3067c478bd9Sstevel@tonic-gate 		 * directly, avoiding the relatively lengthy path through
3077c478bd9Sstevel@tonic-gate 		 * dotoprocs().  The underlying classes expect pidlock to
3087c478bd9Sstevel@tonic-gate 		 * be held.
3097c478bd9Sstevel@tonic-gate 		 */
3107c478bd9Sstevel@tonic-gate 		if (cur_inset_only(&procset) == B_TRUE) {
3117c478bd9Sstevel@tonic-gate 			/* do a single LWP */
3127c478bd9Sstevel@tonic-gate 			if ((procset.p_lidtype == P_LWPID) ||
3137c478bd9Sstevel@tonic-gate 			    (procset.p_ridtype == P_LWPID)) {
3147c478bd9Sstevel@tonic-gate 				mutex_enter(&pidlock);
3157c478bd9Sstevel@tonic-gate 				mutex_enter(&curproc->p_lock);
3167c478bd9Sstevel@tonic-gate 				error = parmsset(&pcparms, curthread);
3177c478bd9Sstevel@tonic-gate 				mutex_exit(&curproc->p_lock);
3187c478bd9Sstevel@tonic-gate 				mutex_exit(&pidlock);
3197c478bd9Sstevel@tonic-gate 			} else {
3207c478bd9Sstevel@tonic-gate 				/* do the entire process otherwise */
3217c478bd9Sstevel@tonic-gate 				stprmargs.stp_parmsp = &pcparms;
3227c478bd9Sstevel@tonic-gate 				stprmargs.stp_error = 0;
3237c478bd9Sstevel@tonic-gate 				mutex_enter(&pidlock);
3247c478bd9Sstevel@tonic-gate 				error = setparms(curproc, &stprmargs);
3257c478bd9Sstevel@tonic-gate 				mutex_exit(&pidlock);
3267c478bd9Sstevel@tonic-gate 				if (error == 0 && stprmargs.stp_error != 0)
3277c478bd9Sstevel@tonic-gate 					error = stprmargs.stp_error;
3287c478bd9Sstevel@tonic-gate 			}
3297c478bd9Sstevel@tonic-gate 			if (error)
3307c478bd9Sstevel@tonic-gate 				return (set_errno(error));
3317c478bd9Sstevel@tonic-gate 		} else {
3327c478bd9Sstevel@tonic-gate 			stprmargs.stp_parmsp = &pcparms;
3337c478bd9Sstevel@tonic-gate 			stprmargs.stp_error = 0;
3347c478bd9Sstevel@tonic-gate 
3357c478bd9Sstevel@tonic-gate 			error1 = error = ESRCH;
3367c478bd9Sstevel@tonic-gate 
3377c478bd9Sstevel@tonic-gate 			/*
3387c478bd9Sstevel@tonic-gate 			 * The dotoprocs() call below will cause
3397c478bd9Sstevel@tonic-gate 			 * setparms() to be called for each thread in the
3407c478bd9Sstevel@tonic-gate 			 * specified procset. setparms() will in turn
3417c478bd9Sstevel@tonic-gate 			 * call parmsset() (which does the real work).
3427c478bd9Sstevel@tonic-gate 			 */
3437c478bd9Sstevel@tonic-gate 			if ((procset.p_lidtype != P_LWPID) ||
3447c478bd9Sstevel@tonic-gate 			    (procset.p_ridtype != P_LWPID)) {
3457c478bd9Sstevel@tonic-gate 				error1 = dotoprocs(&procset, setparms,
3467c478bd9Sstevel@tonic-gate 				    (char *)&stprmargs);
3477c478bd9Sstevel@tonic-gate 			}
3487c478bd9Sstevel@tonic-gate 
3497c478bd9Sstevel@tonic-gate 			/*
3507c478bd9Sstevel@tonic-gate 			 * take care of the case when any of the
3517c478bd9Sstevel@tonic-gate 			 * operands happen to be LWP's
3527c478bd9Sstevel@tonic-gate 			 */
3537c478bd9Sstevel@tonic-gate 
3547c478bd9Sstevel@tonic-gate 			if ((procset.p_lidtype == P_LWPID) ||
3557c478bd9Sstevel@tonic-gate 			    (procset.p_ridtype == P_LWPID)) {
3567c478bd9Sstevel@tonic-gate 				error = dotolwp(&procset, parmsset,
3577c478bd9Sstevel@tonic-gate 				    (char *)&pcparms);
3587c478bd9Sstevel@tonic-gate 				/*
3597c478bd9Sstevel@tonic-gate 				 * Dotolwp() returns with p_lock held.
3607c478bd9Sstevel@tonic-gate 				 * This is required for the GETPARMS case
3617c478bd9Sstevel@tonic-gate 				 * below. So, here we just release the
3627c478bd9Sstevel@tonic-gate 				 * p_lock.
3637c478bd9Sstevel@tonic-gate 				 */
3647c478bd9Sstevel@tonic-gate 				if (MUTEX_HELD(&curproc->p_lock))
3657c478bd9Sstevel@tonic-gate 					mutex_exit(&curproc->p_lock);
3667c478bd9Sstevel@tonic-gate 			}
3677c478bd9Sstevel@tonic-gate 
3687c478bd9Sstevel@tonic-gate 			/*
3697c478bd9Sstevel@tonic-gate 			 * If setparms() encounters a permissions error
3707c478bd9Sstevel@tonic-gate 			 * for one or more of the threads it returns
3717c478bd9Sstevel@tonic-gate 			 * EPERM in stp_error so dotoprocs() will
3727c478bd9Sstevel@tonic-gate 			 * continue through the thread set.  If
3737c478bd9Sstevel@tonic-gate 			 * dotoprocs() returned an error above, it was
3747c478bd9Sstevel@tonic-gate 			 * more serious than permissions and dotoprocs
3757c478bd9Sstevel@tonic-gate 			 * quit when the error was encountered.  We
3767c478bd9Sstevel@tonic-gate 			 * return the more serious error if there was
3777c478bd9Sstevel@tonic-gate 			 * one, otherwise we return EPERM if we got that
3787c478bd9Sstevel@tonic-gate 			 * back.
3797c478bd9Sstevel@tonic-gate 			 */
3807c478bd9Sstevel@tonic-gate 			if (error1 != ESRCH)
3817c478bd9Sstevel@tonic-gate 				error = error1;
3827c478bd9Sstevel@tonic-gate 			if (error == 0 && stprmargs.stp_error != 0)
3837c478bd9Sstevel@tonic-gate 				error = stprmargs.stp_error;
3847c478bd9Sstevel@tonic-gate 		}
3857c478bd9Sstevel@tonic-gate 		break;
3867c478bd9Sstevel@tonic-gate 
3877c478bd9Sstevel@tonic-gate 	case PC_GETPARMS:
3887c478bd9Sstevel@tonic-gate 	case PC_GETXPARMS:
3897c478bd9Sstevel@tonic-gate 		if (cmd == PC_GETPARMS) {
3909acbbeafSnn35248 			if ((*copyinfn)(arg, &pcparms, sizeof (pcparms)))
3917c478bd9Sstevel@tonic-gate 				return (set_errno(EFAULT));
3927c478bd9Sstevel@tonic-gate 		} else {
3937c478bd9Sstevel@tonic-gate 			if (arg != NULL) {
3949acbbeafSnn35248 				if ((*copyinfn)(arg, clname, PC_CLNMSZ))
3957c478bd9Sstevel@tonic-gate 					return (set_errno(EFAULT));
3967c478bd9Sstevel@tonic-gate 
3977c478bd9Sstevel@tonic-gate 				clname[PC_CLNMSZ-1] = '\0';
3987c478bd9Sstevel@tonic-gate 
3997c478bd9Sstevel@tonic-gate 				if (getcid(clname, &pcparms.pc_cid))
4007c478bd9Sstevel@tonic-gate 					return (set_errno(EINVAL));
4017c478bd9Sstevel@tonic-gate 			} else
4027c478bd9Sstevel@tonic-gate 				pcparms.pc_cid = PC_CLNULL;
4039acbbeafSnn35248 
4049acbbeafSnn35248 			if (COPYIN_VAPARMS(arg2, &vaparms, sizeof (vaparms),
4059acbbeafSnn35248 			    seg))
4067c478bd9Sstevel@tonic-gate 				return (set_errno(EFAULT));
4077c478bd9Sstevel@tonic-gate 		}
4087c478bd9Sstevel@tonic-gate 
4097c478bd9Sstevel@tonic-gate 		if (pcparms.pc_cid >= loaded_classes ||
4107c478bd9Sstevel@tonic-gate 		    (pcparms.pc_cid < 1 && pcparms.pc_cid != PC_CLNULL))
4117c478bd9Sstevel@tonic-gate 			return (set_errno(EINVAL));
4127c478bd9Sstevel@tonic-gate 
4139acbbeafSnn35248 		if ((*copyinfn)(psp, &procset, sizeof (procset)))
4147c478bd9Sstevel@tonic-gate 			return (set_errno(EFAULT));
4157c478bd9Sstevel@tonic-gate 
4167c478bd9Sstevel@tonic-gate 		/*
4177c478bd9Sstevel@tonic-gate 		 * Check to see if the current thread is the only one
4187c478bd9Sstevel@tonic-gate 		 * in the set. If not we must go through the whole set
4197c478bd9Sstevel@tonic-gate 		 * to select a thread.
4207c478bd9Sstevel@tonic-gate 		 */
4217c478bd9Sstevel@tonic-gate 		if (cur_inset_only(&procset) == B_TRUE) {
4227c478bd9Sstevel@tonic-gate 			/* do a single LWP */
4237c478bd9Sstevel@tonic-gate 			if ((procset.p_lidtype == P_LWPID) ||
4247c478bd9Sstevel@tonic-gate 			    (procset.p_ridtype == P_LWPID)) {
4257c478bd9Sstevel@tonic-gate 				if (pcparms.pc_cid != PC_CLNULL &&
4267c478bd9Sstevel@tonic-gate 				    pcparms.pc_cid != curthread->t_cid) {
4277c478bd9Sstevel@tonic-gate 					/*
4287c478bd9Sstevel@tonic-gate 					 * Specified thread not in
4297c478bd9Sstevel@tonic-gate 					 * specified class.
4307c478bd9Sstevel@tonic-gate 					 */
4317c478bd9Sstevel@tonic-gate 					return (set_errno(ESRCH));
4327c478bd9Sstevel@tonic-gate 				} else {
4337c478bd9Sstevel@tonic-gate 					mutex_enter(&curproc->p_lock);
4347c478bd9Sstevel@tonic-gate 					retthreadp = curthread;
4357c478bd9Sstevel@tonic-gate 				}
4367c478bd9Sstevel@tonic-gate 			} else {
4377c478bd9Sstevel@tonic-gate 				count = 0;
4387c478bd9Sstevel@tonic-gate 				retthreadp = NULL;
4397c478bd9Sstevel@tonic-gate 				pcmpargs.pcmp_cidp = &pcparms.pc_cid;
4407c478bd9Sstevel@tonic-gate 				pcmpargs.pcmp_cntp = &count;
4417c478bd9Sstevel@tonic-gate 				pcmpargs.pcmp_retthreadp = &retthreadp;
4427c478bd9Sstevel@tonic-gate 				/*
4437c478bd9Sstevel@tonic-gate 				 * Specified thread not in specified class.
4447c478bd9Sstevel@tonic-gate 				 */
4457c478bd9Sstevel@tonic-gate 				if (pcparms.pc_cid != PC_CLNULL &&
4467c478bd9Sstevel@tonic-gate 				    pcparms.pc_cid != curthread->t_cid)
4477c478bd9Sstevel@tonic-gate 					return (set_errno(ESRCH));
4487c478bd9Sstevel@tonic-gate 				error = proccmp(curproc, &pcmpargs);
4497c478bd9Sstevel@tonic-gate 				if (error) {
4507c478bd9Sstevel@tonic-gate 					if (retthreadp != NULL)
4517c478bd9Sstevel@tonic-gate 						mutex_exit(&(curproc->p_lock));
4527c478bd9Sstevel@tonic-gate 					return (set_errno(error));
4537c478bd9Sstevel@tonic-gate 				}
4547c478bd9Sstevel@tonic-gate 			}
4557c478bd9Sstevel@tonic-gate 		} else {
4567c478bd9Sstevel@tonic-gate 			/*
4577c478bd9Sstevel@tonic-gate 			 * get initpp early to avoid lock ordering problems
4587c478bd9Sstevel@tonic-gate 			 * (we cannot get pidlock while holding any p_lock).
4597c478bd9Sstevel@tonic-gate 			 */
4607c478bd9Sstevel@tonic-gate 			mutex_enter(&pidlock);
4617c478bd9Sstevel@tonic-gate 			initpp = prfind(P_INITPID);
4627c478bd9Sstevel@tonic-gate 			mutex_exit(&pidlock);
4637c478bd9Sstevel@tonic-gate 
4647c478bd9Sstevel@tonic-gate 			/*
4657c478bd9Sstevel@tonic-gate 			 * Select the thread (from the set) whose
4667c478bd9Sstevel@tonic-gate 			 * parameters we are going to return.  First we
4677c478bd9Sstevel@tonic-gate 			 * set up some locations for return values, then
4687c478bd9Sstevel@tonic-gate 			 * we call proccmp() indirectly through
4697c478bd9Sstevel@tonic-gate 			 * dotoprocs().  proccmp() will call a class
4707c478bd9Sstevel@tonic-gate 			 * specific routine which actually does the
4717c478bd9Sstevel@tonic-gate 			 * selection.  To understand how this works take
4727c478bd9Sstevel@tonic-gate 			 * a careful look at the code below, the
4737c478bd9Sstevel@tonic-gate 			 * dotoprocs() function, the proccmp() function,
4747c478bd9Sstevel@tonic-gate 			 * and the class specific cl_proccmp() functions.
4757c478bd9Sstevel@tonic-gate 			 */
4767c478bd9Sstevel@tonic-gate 			if (pcparms.pc_cid == PC_CLNULL)
4777c478bd9Sstevel@tonic-gate 				clnullflag = 1;
4787c478bd9Sstevel@tonic-gate 			else
4797c478bd9Sstevel@tonic-gate 				clnullflag = 0;
4807c478bd9Sstevel@tonic-gate 			count = 0;
4817c478bd9Sstevel@tonic-gate 			retthreadp = NULL;
4827c478bd9Sstevel@tonic-gate 			pcmpargs.pcmp_cidp = &pcparms.pc_cid;
4837c478bd9Sstevel@tonic-gate 			pcmpargs.pcmp_cntp = &count;
4847c478bd9Sstevel@tonic-gate 			pcmpargs.pcmp_retthreadp = &retthreadp;
4857c478bd9Sstevel@tonic-gate 			error1 = error = ESRCH;
4867c478bd9Sstevel@tonic-gate 
4877c478bd9Sstevel@tonic-gate 			if ((procset.p_lidtype != P_LWPID) ||
4887c478bd9Sstevel@tonic-gate 			    (procset.p_ridtype != P_LWPID)) {
4897c478bd9Sstevel@tonic-gate 				error1 = dotoprocs(&procset, proccmp,
4907c478bd9Sstevel@tonic-gate 				    (char *)&pcmpargs);
4917c478bd9Sstevel@tonic-gate 			}
4927c478bd9Sstevel@tonic-gate 
4937c478bd9Sstevel@tonic-gate 			/*
4947c478bd9Sstevel@tonic-gate 			 * take care of combination of LWP and process
4957c478bd9Sstevel@tonic-gate 			 * set case in a procset
4967c478bd9Sstevel@tonic-gate 			 */
4977c478bd9Sstevel@tonic-gate 			if ((procset.p_lidtype == P_LWPID) ||
4987c478bd9Sstevel@tonic-gate 			    (procset.p_ridtype == P_LWPID)) {
4997c478bd9Sstevel@tonic-gate 				error = dotolwp(&procset, threadcmp,
5007c478bd9Sstevel@tonic-gate 				    (char *)&pcmpargs);
5017c478bd9Sstevel@tonic-gate 			}
5027c478bd9Sstevel@tonic-gate 
5037c478bd9Sstevel@tonic-gate 			/*
5047c478bd9Sstevel@tonic-gate 			 * Both proccmp() and threadcmp() return with the
5057c478bd9Sstevel@tonic-gate 			 * p_lock held for the ttoproc(retthreadp). This
5067c478bd9Sstevel@tonic-gate 			 * is required to make sure that the process we
5077c478bd9Sstevel@tonic-gate 			 * chose as the winner doesn't go away
5087c478bd9Sstevel@tonic-gate 			 * i.e. retthreadp has to be a valid pointer.
5097c478bd9Sstevel@tonic-gate 			 *
5107c478bd9Sstevel@tonic-gate 			 * The case below can only happen if the thread
5117c478bd9Sstevel@tonic-gate 			 * with the highest priority was not in your
5127c478bd9Sstevel@tonic-gate 			 * process.  In that case, dotolwp will return
5137c478bd9Sstevel@tonic-gate 			 * holding p_lock for both your process as well
5147c478bd9Sstevel@tonic-gate 			 * as the process in which retthreadp is a
5157c478bd9Sstevel@tonic-gate 			 * thread.
5167c478bd9Sstevel@tonic-gate 			 */
5177c478bd9Sstevel@tonic-gate 			if ((retthreadp != NULL) &&
5187c478bd9Sstevel@tonic-gate 			    (ttoproc(retthreadp) != curproc) &&
5197c478bd9Sstevel@tonic-gate 			    MUTEX_HELD(&(curproc)->p_lock))
5207c478bd9Sstevel@tonic-gate 				mutex_exit(&(curproc)->p_lock);
5217c478bd9Sstevel@tonic-gate 
5227c478bd9Sstevel@tonic-gate 			ASSERT(retthreadp == NULL ||
5237c478bd9Sstevel@tonic-gate 			    MUTEX_HELD(&(ttoproc(retthreadp)->p_lock)));
5247c478bd9Sstevel@tonic-gate 			if (error1 != ESRCH)
5257c478bd9Sstevel@tonic-gate 				error = error1;
5267c478bd9Sstevel@tonic-gate 			if (error) {
5277c478bd9Sstevel@tonic-gate 				if (retthreadp != NULL)
528d4204c85Sraf 				    /* CSTYLED */
5297c478bd9Sstevel@tonic-gate 				    mutex_exit(&(ttoproc(retthreadp)->p_lock));
5307c478bd9Sstevel@tonic-gate 				ASSERT(MUTEX_NOT_HELD(&(curproc)->p_lock));
5317c478bd9Sstevel@tonic-gate 				return (set_errno(error));
5327c478bd9Sstevel@tonic-gate 			}
5337c478bd9Sstevel@tonic-gate 			/*
5347c478bd9Sstevel@tonic-gate 			 * dotoprocs() ignores the init process if it is
5357c478bd9Sstevel@tonic-gate 			 * in the set, unless it was the only process found.
5367c478bd9Sstevel@tonic-gate 			 * Since we are getting parameters here rather than
5377c478bd9Sstevel@tonic-gate 			 * setting them, we want to make sure init is not
5387c478bd9Sstevel@tonic-gate 			 * excluded if it is in the set.
5397c478bd9Sstevel@tonic-gate 			 */
540def11082Srh87107 			if (initpp != NULL && retthreadp != NULL &&
541def11082Srh87107 			    ttoproc(retthreadp) != initpp) {
542def11082Srh87107 				mutex_enter(&initpp->p_lock);
543def11082Srh87107 				if (procinset(initpp, &procset)) {
544def11082Srh87107 					mutex_exit(&initpp->p_lock);
5457c478bd9Sstevel@tonic-gate 					(void) proccmp(initpp, &pcmpargs);
546def11082Srh87107 				} else {
547def11082Srh87107 					mutex_exit(&initpp->p_lock);
548def11082Srh87107 				}
549def11082Srh87107 			}
5507c478bd9Sstevel@tonic-gate 
5517c478bd9Sstevel@tonic-gate 			/*
5527c478bd9Sstevel@tonic-gate 			 * If dotoprocs returned success it found at least
5537c478bd9Sstevel@tonic-gate 			 * one thread in the set.  If proccmp() failed to
5547c478bd9Sstevel@tonic-gate 			 * select a thread it is because the user specified
5557c478bd9Sstevel@tonic-gate 			 * a class and none of the threads in the set
5567c478bd9Sstevel@tonic-gate 			 * belonged to that class, or because the process
5577c478bd9Sstevel@tonic-gate 			 * specified was in the middle of exiting and had
5587c478bd9Sstevel@tonic-gate 			 * cleared its thread list.
5597c478bd9Sstevel@tonic-gate 			 */
5607c478bd9Sstevel@tonic-gate 			if (retthreadp == NULL) {
5617c478bd9Sstevel@tonic-gate 				/*
5627c478bd9Sstevel@tonic-gate 				 * Might be here and still holding p_lock
5637c478bd9Sstevel@tonic-gate 				 * if we did a dotolwp on an lwp that
5647c478bd9Sstevel@tonic-gate 				 * existed but was in the wrong class.
5657c478bd9Sstevel@tonic-gate 				 */
5667c478bd9Sstevel@tonic-gate 				if (MUTEX_HELD(&(curproc)->p_lock))
5677c478bd9Sstevel@tonic-gate 					mutex_exit(&(curproc)->p_lock);
5687c478bd9Sstevel@tonic-gate 				return (set_errno(ESRCH));
5697c478bd9Sstevel@tonic-gate 			}
5707c478bd9Sstevel@tonic-gate 
5717c478bd9Sstevel@tonic-gate 			/*
5727c478bd9Sstevel@tonic-gate 			 * User can only use PC_CLNULL with one thread in set.
5737c478bd9Sstevel@tonic-gate 			 */
5747c478bd9Sstevel@tonic-gate 			if (clnullflag && count > 1) {
5757c478bd9Sstevel@tonic-gate 				if (retthreadp != NULL)
5767c478bd9Sstevel@tonic-gate 					mutex_exit(
5777c478bd9Sstevel@tonic-gate 					    &(ttoproc(retthreadp)->p_lock));
5787c478bd9Sstevel@tonic-gate 				ASSERT(MUTEX_NOT_HELD(&(curproc)->p_lock));
5797c478bd9Sstevel@tonic-gate 				return (set_errno(EINVAL));
5807c478bd9Sstevel@tonic-gate 			}
5817c478bd9Sstevel@tonic-gate 		}
5827c478bd9Sstevel@tonic-gate 
5837c478bd9Sstevel@tonic-gate 		ASSERT(retthreadp == NULL ||
5847c478bd9Sstevel@tonic-gate 		    MUTEX_HELD(&(ttoproc(retthreadp)->p_lock)));
5857c478bd9Sstevel@tonic-gate 		/*
5867c478bd9Sstevel@tonic-gate 		 * It is possible to have retthreadp == NULL. Proccmp()
5877c478bd9Sstevel@tonic-gate 		 * in the rare case (p_tlist == NULL) could return without
5887c478bd9Sstevel@tonic-gate 		 * setting a value for retthreadp.
5897c478bd9Sstevel@tonic-gate 		 */
5907c478bd9Sstevel@tonic-gate 		if (retthreadp == NULL) {
5917c478bd9Sstevel@tonic-gate 			ASSERT(MUTEX_NOT_HELD(&(curproc)->p_lock));
5927c478bd9Sstevel@tonic-gate 			return (set_errno(ESRCH));
5937c478bd9Sstevel@tonic-gate 		}
5947c478bd9Sstevel@tonic-gate 		/*
5957c478bd9Sstevel@tonic-gate 		 * We've selected a thread so now get the parameters.
5967c478bd9Sstevel@tonic-gate 		 */
5977c478bd9Sstevel@tonic-gate 		parmsget(retthreadp, &pcparms);
5987c478bd9Sstevel@tonic-gate 
5997c478bd9Sstevel@tonic-gate 		/*
6007c478bd9Sstevel@tonic-gate 		 * Prepare to return parameters to the user
6017c478bd9Sstevel@tonic-gate 		 */
6027c478bd9Sstevel@tonic-gate 		error = parmsout(&pcparms,
6037c478bd9Sstevel@tonic-gate 		    (cmd == PC_GETPARMS ? NULL : &vaparms));
6047c478bd9Sstevel@tonic-gate 
6057c478bd9Sstevel@tonic-gate 		/*
6067c478bd9Sstevel@tonic-gate 		 * Save pid of selected thread before dropping p_lock.
6077c478bd9Sstevel@tonic-gate 		 */
6087c478bd9Sstevel@tonic-gate 		saved_pid = ttoproc(retthreadp)->p_pid;
6097c478bd9Sstevel@tonic-gate 		mutex_exit(&(ttoproc(retthreadp)->p_lock));
6107c478bd9Sstevel@tonic-gate 		ASSERT(MUTEX_NOT_HELD(&curproc->p_lock));
6117c478bd9Sstevel@tonic-gate 
6127c478bd9Sstevel@tonic-gate 		if (error)
6137c478bd9Sstevel@tonic-gate 			return (set_errno(error));
6147c478bd9Sstevel@tonic-gate 
6157c478bd9Sstevel@tonic-gate 		if (cmd == PC_GETPARMS) {
6169acbbeafSnn35248 			if ((*copyoutfn)(&pcparms, arg, sizeof (pcparms)))
6177c478bd9Sstevel@tonic-gate 				return (set_errno(EFAULT));
6189acbbeafSnn35248 		} else if ((error = vaparmsout(arg, &pcparms, &vaparms,
6199acbbeafSnn35248 		    seg)) != 0)
6207c478bd9Sstevel@tonic-gate 			return (set_errno(error));
6217c478bd9Sstevel@tonic-gate 
6227c478bd9Sstevel@tonic-gate 		/*
6237c478bd9Sstevel@tonic-gate 		 * And finally, return the pid of the selected thread.
6247c478bd9Sstevel@tonic-gate 		 */
6257c478bd9Sstevel@tonic-gate 		rv = saved_pid;
6267c478bd9Sstevel@tonic-gate 		break;
6277c478bd9Sstevel@tonic-gate 
6287c478bd9Sstevel@tonic-gate 	case PC_ADMIN:
6297c478bd9Sstevel@tonic-gate 		if (get_udatamodel() == DATAMODEL_NATIVE) {
6309acbbeafSnn35248 			if ((*copyinfn)(arg, &pcadmin, sizeof (pcadmin_t)))
6317c478bd9Sstevel@tonic-gate 				return (set_errno(EFAULT));
6327c478bd9Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
6337c478bd9Sstevel@tonic-gate 		} else {
6347c478bd9Sstevel@tonic-gate 			/* pcadmin struct from ILP32 callers */
6357c478bd9Sstevel@tonic-gate 			pcadmin32_t pcadmin32;
6367c478bd9Sstevel@tonic-gate 
6379acbbeafSnn35248 			if ((*copyinfn)(arg, &pcadmin32, sizeof (pcadmin32_t)))
6387c478bd9Sstevel@tonic-gate 				return (set_errno(EFAULT));
6397c478bd9Sstevel@tonic-gate 			pcadmin.pc_cid = pcadmin32.pc_cid;
6407c478bd9Sstevel@tonic-gate 			pcadmin.pc_cladmin = (caddr_t)(uintptr_t)
6417c478bd9Sstevel@tonic-gate 			    pcadmin32.pc_cladmin;
6427c478bd9Sstevel@tonic-gate #endif /* _SYSCALL32_IMPL */
6437c478bd9Sstevel@tonic-gate 		}
6447c478bd9Sstevel@tonic-gate 
6457c478bd9Sstevel@tonic-gate 		if (pcadmin.pc_cid >= loaded_classes ||
6467c478bd9Sstevel@tonic-gate 		    pcadmin.pc_cid < 1)
6477c478bd9Sstevel@tonic-gate 			return (set_errno(EINVAL));
6487c478bd9Sstevel@tonic-gate 
6497c478bd9Sstevel@tonic-gate 		/*
6507c478bd9Sstevel@tonic-gate 		 * Have the class do whatever the user is requesting.
6517c478bd9Sstevel@tonic-gate 		 */
6527c478bd9Sstevel@tonic-gate 		mutex_enter(&ualock);
6537c478bd9Sstevel@tonic-gate 		error = CL_ADMIN(&sclass[pcadmin.pc_cid], pcadmin.pc_cladmin,
6547c478bd9Sstevel@tonic-gate 		    CRED());
6557c478bd9Sstevel@tonic-gate 		mutex_exit(&ualock);
6567c478bd9Sstevel@tonic-gate 		break;
6577c478bd9Sstevel@tonic-gate 
6587c478bd9Sstevel@tonic-gate 	case PC_GETPRIRANGE:
6599acbbeafSnn35248 		if ((*copyinfn)(arg, &pcpri, sizeof (pcpri_t)))
6607c478bd9Sstevel@tonic-gate 			return (set_errno(EFAULT));
6617c478bd9Sstevel@tonic-gate 
6627c478bd9Sstevel@tonic-gate 		if (pcpri.pc_cid >= loaded_classes || pcpri.pc_cid < 0)
6637c478bd9Sstevel@tonic-gate 			return (set_errno(EINVAL));
6647c478bd9Sstevel@tonic-gate 
6657c478bd9Sstevel@tonic-gate 		error = CL_GETCLPRI(&sclass[pcpri.pc_cid], &pcpri);
6667c478bd9Sstevel@tonic-gate 		if (!error) {
6679acbbeafSnn35248 			if ((*copyoutfn)(&pcpri, arg, sizeof (pcpri)))
6687c478bd9Sstevel@tonic-gate 				return (set_errno(EFAULT));
6697c478bd9Sstevel@tonic-gate 		}
6707c478bd9Sstevel@tonic-gate 		break;
6717c478bd9Sstevel@tonic-gate 
6727c478bd9Sstevel@tonic-gate 	case PC_DONICE:
6737c478bd9Sstevel@tonic-gate 		/*
6747c478bd9Sstevel@tonic-gate 		 * Get pcnice and procset structures from the user.
6757c478bd9Sstevel@tonic-gate 		 */
6769acbbeafSnn35248 		if ((*copyinfn)(arg, &pcnice, sizeof (pcnice)) ||
6779acbbeafSnn35248 		    (*copyinfn)(psp, &procset, sizeof (procset)))
6787c478bd9Sstevel@tonic-gate 			return (set_errno(EFAULT));
6797c478bd9Sstevel@tonic-gate 
6807c478bd9Sstevel@tonic-gate 		error = donice(&procset, &pcnice);
6817c478bd9Sstevel@tonic-gate 
6827c478bd9Sstevel@tonic-gate 		if (!error && (pcnice.pc_op == PC_GETNICE)) {
6839acbbeafSnn35248 			if ((*copyoutfn)(&pcnice, arg, sizeof (pcnice)))
6847c478bd9Sstevel@tonic-gate 				return (set_errno(EFAULT));
6857c478bd9Sstevel@tonic-gate 		}
6867c478bd9Sstevel@tonic-gate 		break;
6877c478bd9Sstevel@tonic-gate 
688d4204c85Sraf 	case PC_DOPRIO:
689d4204c85Sraf 		/*
690d4204c85Sraf 		 * Get pcprio and procset structures from the user.
691d4204c85Sraf 		 */
692d4204c85Sraf 		if ((*copyinfn)(arg, &pcprio, sizeof (pcprio)) ||
693d4204c85Sraf 		    (*copyinfn)(psp, &procset, sizeof (procset)))
694d4204c85Sraf 			return (set_errno(EFAULT));
695d4204c85Sraf 
696d4204c85Sraf 		error = doprio(&procset, &pcprio);
697d4204c85Sraf 
698d4204c85Sraf 		if (!error && (pcprio.pc_op == PC_GETPRIO)) {
699d4204c85Sraf 			if ((*copyoutfn)(&pcprio, arg, sizeof (pcprio)))
700d4204c85Sraf 				return (set_errno(EFAULT));
701d4204c85Sraf 		}
702d4204c85Sraf 		break;
703d4204c85Sraf 
7047c478bd9Sstevel@tonic-gate 	case PC_SETDFLCL:
7057c478bd9Sstevel@tonic-gate 		if (secpolicy_dispadm(CRED()) != 0)
7067c478bd9Sstevel@tonic-gate 			return (set_errno(EPERM));
7077c478bd9Sstevel@tonic-gate 
7087c478bd9Sstevel@tonic-gate 		if (copyin(arg, (caddr_t)clname, PC_CLNMSZ) != 0)
7097c478bd9Sstevel@tonic-gate 			return (set_errno(EFAULT));
7107c478bd9Sstevel@tonic-gate 		clname[PC_CLNMSZ-1] = '\0';
7117c478bd9Sstevel@tonic-gate 
7127c478bd9Sstevel@tonic-gate 		if (getcid(clname, &classid) != 0)
7137c478bd9Sstevel@tonic-gate 			return (set_errno(EINVAL));
714*35a5a358SJonathan Adams 		if (CLASS_KERNEL(classid))
7157c478bd9Sstevel@tonic-gate 			return (set_errno(EINVAL));
7167c478bd9Sstevel@tonic-gate 		defaultcid = classid;
7177c478bd9Sstevel@tonic-gate 		ASSERT(defaultcid > 0 && defaultcid < loaded_classes);
7187c478bd9Sstevel@tonic-gate 		break;
7197c478bd9Sstevel@tonic-gate 
7200209230bSgjelinek 	case PC_GETDFLCL:
7210209230bSgjelinek 		mutex_enter(&class_lock);
7220209230bSgjelinek 
7230209230bSgjelinek 		if (defaultcid >= loaded_classes)
7240209230bSgjelinek 			outstr = "";
7250209230bSgjelinek 		else
7260209230bSgjelinek 			outstr = sclass[defaultcid].cl_name;
7270209230bSgjelinek 		size = strlen(outstr) + 1;
7280209230bSgjelinek 		if (arg != NULL)
7290209230bSgjelinek 			if ((*copyoutfn)(outstr, arg, size) != 0)
7300209230bSgjelinek 				error = EFAULT;
7310209230bSgjelinek 
7320209230bSgjelinek 		mutex_exit(&class_lock);
7330209230bSgjelinek 		break;
7340209230bSgjelinek 
7357c478bd9Sstevel@tonic-gate 	default:
7367c478bd9Sstevel@tonic-gate 		error = EINVAL;
7377c478bd9Sstevel@tonic-gate 		break;
7387c478bd9Sstevel@tonic-gate 	}
7397c478bd9Sstevel@tonic-gate 	return (error ? (set_errno(error)) : rv);
7407c478bd9Sstevel@tonic-gate }
7417c478bd9Sstevel@tonic-gate 
7429acbbeafSnn35248 long
priocntlsys(int pc_version,procset_t * psp,int cmd,caddr_t arg,caddr_t arg2)7439acbbeafSnn35248 priocntlsys(int pc_version, procset_t *psp, int cmd, caddr_t arg, caddr_t arg2)
7449acbbeafSnn35248 {
7459acbbeafSnn35248 	return (priocntl_common(pc_version, psp, cmd, arg, arg2,
7469acbbeafSnn35248 	    UIO_USERSPACE));
7479acbbeafSnn35248 }
7487c478bd9Sstevel@tonic-gate 
7497c478bd9Sstevel@tonic-gate /*
7507c478bd9Sstevel@tonic-gate  * The proccmp() function is part of the implementation of the
7517c478bd9Sstevel@tonic-gate  * PC_GETPARMS command of the priocntl system call.  This function works
7527c478bd9Sstevel@tonic-gate  * with the system call code and with the class specific cl_globpri()
7537c478bd9Sstevel@tonic-gate  * function to select one thread from a specified procset based on class
7547c478bd9Sstevel@tonic-gate  * specific criteria. proccmp() is called indirectly from the priocntl
7557c478bd9Sstevel@tonic-gate  * code through the dotoprocs function.  Basic strategy is dotoprocs()
7567c478bd9Sstevel@tonic-gate  * calls us once for each thread in the set.  We in turn call the class
7577c478bd9Sstevel@tonic-gate  * specific function to compare the current thread from dotoprocs to the
7587c478bd9Sstevel@tonic-gate  * "best" (according to the class criteria) found so far.  We keep the
7597c478bd9Sstevel@tonic-gate  * "best" thread in *pcmp_retthreadp.
7607c478bd9Sstevel@tonic-gate  */
7617c478bd9Sstevel@tonic-gate static int
proccmp(proc_t * pp,struct pcmpargs * argp)7627c478bd9Sstevel@tonic-gate proccmp(proc_t *pp, struct pcmpargs *argp)
7637c478bd9Sstevel@tonic-gate {
764d4204c85Sraf 	kthread_t	*tx;
765d4204c85Sraf 	kthread_t	*ty;
7667c478bd9Sstevel@tonic-gate 	int		last_pri = -1;
7677c478bd9Sstevel@tonic-gate 	int		tx_pri;
7687c478bd9Sstevel@tonic-gate 	int		found = 0;
7697c478bd9Sstevel@tonic-gate 
7707c478bd9Sstevel@tonic-gate 	mutex_enter(&pp->p_lock);
7717c478bd9Sstevel@tonic-gate 
7727c478bd9Sstevel@tonic-gate 	if (pp->p_tlist == NULL) {
7737c478bd9Sstevel@tonic-gate 		mutex_exit(&pp->p_lock);
7747c478bd9Sstevel@tonic-gate 		return (0);
7757c478bd9Sstevel@tonic-gate 	}
7767c478bd9Sstevel@tonic-gate 	(*argp->pcmp_cntp)++;	/* Increment count of procs in the set */
7777c478bd9Sstevel@tonic-gate 
7787c478bd9Sstevel@tonic-gate 	if (*argp->pcmp_cidp == PC_CLNULL) {
7797c478bd9Sstevel@tonic-gate 		/*
7807c478bd9Sstevel@tonic-gate 		 * If no cid is specified, then lets just pick the first one.
7817c478bd9Sstevel@tonic-gate 		 * It doesn't matter because if the number of processes in the
7827c478bd9Sstevel@tonic-gate 		 * set are more than 1, then we return EINVAL in priocntlsys.
7837c478bd9Sstevel@tonic-gate 		 */
7847c478bd9Sstevel@tonic-gate 		*argp->pcmp_cidp = pp->p_tlist->t_cid;
7857c478bd9Sstevel@tonic-gate 	}
7867c478bd9Sstevel@tonic-gate 	ty = tx = pp->p_tlist;
7877c478bd9Sstevel@tonic-gate 	do {
7887c478bd9Sstevel@tonic-gate 		if (tx->t_cid == *argp->pcmp_cidp) {
7897c478bd9Sstevel@tonic-gate 			/*
7907c478bd9Sstevel@tonic-gate 			 * We found one which matches the required cid.
7917c478bd9Sstevel@tonic-gate 			 */
7927c478bd9Sstevel@tonic-gate 			found = 1;
7937c478bd9Sstevel@tonic-gate 			if ((tx_pri = CL_GLOBPRI(tx)) > last_pri) {
7947c478bd9Sstevel@tonic-gate 				last_pri = tx_pri;
7957c478bd9Sstevel@tonic-gate 				ty = tx;
7967c478bd9Sstevel@tonic-gate 			}
7977c478bd9Sstevel@tonic-gate 		}
7987c478bd9Sstevel@tonic-gate 	} while ((tx = tx->t_forw) != pp->p_tlist);
7997c478bd9Sstevel@tonic-gate 	if (found) {
8007c478bd9Sstevel@tonic-gate 		if (*argp->pcmp_retthreadp == NULL) {
8017c478bd9Sstevel@tonic-gate 			/*
8027c478bd9Sstevel@tonic-gate 			 * First time through for this set.
8037c478bd9Sstevel@tonic-gate 			 * keep the mutex held. He might be the one!
8047c478bd9Sstevel@tonic-gate 			 */
8057c478bd9Sstevel@tonic-gate 			*argp->pcmp_retthreadp = ty;
8067c478bd9Sstevel@tonic-gate 		} else {
8077c478bd9Sstevel@tonic-gate 			tx = *argp->pcmp_retthreadp;
8087c478bd9Sstevel@tonic-gate 			if (CL_GLOBPRI(ty) <= CL_GLOBPRI(tx)) {
8097c478bd9Sstevel@tonic-gate 				mutex_exit(&pp->p_lock);
8107c478bd9Sstevel@tonic-gate 			} else {
8117c478bd9Sstevel@tonic-gate 				mutex_exit(&(ttoproc(tx)->p_lock));
8127c478bd9Sstevel@tonic-gate 				*argp->pcmp_retthreadp = ty;
8137c478bd9Sstevel@tonic-gate 			}
8147c478bd9Sstevel@tonic-gate 		}
8157c478bd9Sstevel@tonic-gate 	} else {
8167c478bd9Sstevel@tonic-gate 		/*
8177c478bd9Sstevel@tonic-gate 		 * We actually didn't find anything of the same cid in
8187c478bd9Sstevel@tonic-gate 		 * this process.
8197c478bd9Sstevel@tonic-gate 		 */
8207c478bd9Sstevel@tonic-gate 		mutex_exit(&pp->p_lock);
8217c478bd9Sstevel@tonic-gate 	}
8227c478bd9Sstevel@tonic-gate 	return (0);
8237c478bd9Sstevel@tonic-gate }
8247c478bd9Sstevel@tonic-gate 
8257c478bd9Sstevel@tonic-gate 
8267c478bd9Sstevel@tonic-gate int
threadcmp(struct pcmpargs * argp,kthread_t * tp)827d4204c85Sraf threadcmp(struct pcmpargs *argp, kthread_t *tp)
8287c478bd9Sstevel@tonic-gate {
829d4204c85Sraf 	kthread_t	*tx;
8307c478bd9Sstevel@tonic-gate 	proc_t		*pp;
8317c478bd9Sstevel@tonic-gate 
8327c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&(ttoproc(tp))->p_lock));
8337c478bd9Sstevel@tonic-gate 
8347c478bd9Sstevel@tonic-gate 	(*argp->pcmp_cntp)++;   /* Increment count of procs in the set */
8357c478bd9Sstevel@tonic-gate 	if (*argp->pcmp_cidp == PC_CLNULL) {
8367c478bd9Sstevel@tonic-gate 		/*
8377c478bd9Sstevel@tonic-gate 		 * If no cid is specified, then lets just pick the first one.
8387c478bd9Sstevel@tonic-gate 		 * It doesn't matter because if the number of threads in the
8397c478bd9Sstevel@tonic-gate 		 * set are more than 1, then we return EINVAL in priocntlsys.
8407c478bd9Sstevel@tonic-gate 		 */
8417c478bd9Sstevel@tonic-gate 		*argp->pcmp_cidp = tp->t_cid;
8427c478bd9Sstevel@tonic-gate 	}
8437c478bd9Sstevel@tonic-gate 	if (tp->t_cid == *argp->pcmp_cidp) {
8447c478bd9Sstevel@tonic-gate 		if (*argp->pcmp_retthreadp == NULL) {
8457c478bd9Sstevel@tonic-gate 			/*
8467c478bd9Sstevel@tonic-gate 			 * First time through for this set.
8477c478bd9Sstevel@tonic-gate 			 */
8487c478bd9Sstevel@tonic-gate 			*argp->pcmp_retthreadp = tp;
8497c478bd9Sstevel@tonic-gate 		} else {
8507c478bd9Sstevel@tonic-gate 			tx = *argp->pcmp_retthreadp;
8517c478bd9Sstevel@tonic-gate 			if (CL_GLOBPRI(tp) > CL_GLOBPRI(tx)) {
8527c478bd9Sstevel@tonic-gate 				/*
8537c478bd9Sstevel@tonic-gate 				 * Unlike proccmp(), we don't release the
8547c478bd9Sstevel@tonic-gate 				 * p_lock of the ttoproc(tp) if tp's global
8557c478bd9Sstevel@tonic-gate 				 * priority is less than tx's. We need to go
8567c478bd9Sstevel@tonic-gate 				 * through the entire list before we can do
8577c478bd9Sstevel@tonic-gate 				 * that. The p_lock is released by the caller
8587c478bd9Sstevel@tonic-gate 				 * of dotolwp().
8597c478bd9Sstevel@tonic-gate 				 */
8607c478bd9Sstevel@tonic-gate 				pp = ttoproc(tx);
8617c478bd9Sstevel@tonic-gate 				ASSERT(MUTEX_HELD(&pp->p_lock));
8627c478bd9Sstevel@tonic-gate 				if (pp != curproc) {
8637c478bd9Sstevel@tonic-gate 					mutex_exit(&pp->p_lock);
8647c478bd9Sstevel@tonic-gate 				}
8657c478bd9Sstevel@tonic-gate 				*argp->pcmp_retthreadp = tp;
8667c478bd9Sstevel@tonic-gate 			}
8677c478bd9Sstevel@tonic-gate 		}
8687c478bd9Sstevel@tonic-gate 	}
8697c478bd9Sstevel@tonic-gate 	return (0);
8707c478bd9Sstevel@tonic-gate }
8717c478bd9Sstevel@tonic-gate 
8727c478bd9Sstevel@tonic-gate 
8737c478bd9Sstevel@tonic-gate /*
8747c478bd9Sstevel@tonic-gate  * The setparms() function is called indirectly by priocntlsys()
8757c478bd9Sstevel@tonic-gate  * through the dotoprocs() function.  setparms() acts as an
8767c478bd9Sstevel@tonic-gate  * intermediary between dotoprocs() and the parmsset() function,
8777c478bd9Sstevel@tonic-gate  * calling parmsset() for each thread in the set and handling
8787c478bd9Sstevel@tonic-gate  * the error returns on their way back up to dotoprocs().
8797c478bd9Sstevel@tonic-gate  */
8807c478bd9Sstevel@tonic-gate static int
setparms(proc_t * targpp,struct stprmargs * stprmp)8817c478bd9Sstevel@tonic-gate setparms(proc_t *targpp, struct stprmargs *stprmp)
8827c478bd9Sstevel@tonic-gate {
8837c478bd9Sstevel@tonic-gate 	int error = 0;
884d4204c85Sraf 	kthread_t *t;
8857c478bd9Sstevel@tonic-gate 	int err;
8867c478bd9Sstevel@tonic-gate 
8877c478bd9Sstevel@tonic-gate 	mutex_enter(&targpp->p_lock);
8887c478bd9Sstevel@tonic-gate 	if ((t = targpp->p_tlist) == NULL) {
8897c478bd9Sstevel@tonic-gate 		mutex_exit(&targpp->p_lock);
8907c478bd9Sstevel@tonic-gate 		return (0);
8917c478bd9Sstevel@tonic-gate 	}
8927c478bd9Sstevel@tonic-gate 	do {
8937c478bd9Sstevel@tonic-gate 		err = parmsset(stprmp->stp_parmsp, t);
8947c478bd9Sstevel@tonic-gate 		if (error == 0)
8957c478bd9Sstevel@tonic-gate 			error = err;
8967c478bd9Sstevel@tonic-gate 	} while ((t = t->t_forw) != targpp->p_tlist);
8977c478bd9Sstevel@tonic-gate 	mutex_exit(&targpp->p_lock);
8987c478bd9Sstevel@tonic-gate 	if (error) {
8997c478bd9Sstevel@tonic-gate 		if (error == EPERM) {
9007c478bd9Sstevel@tonic-gate 			stprmp->stp_error = EPERM;
9017c478bd9Sstevel@tonic-gate 			return (0);
9027c478bd9Sstevel@tonic-gate 		} else {
9037c478bd9Sstevel@tonic-gate 			return (error);
9047c478bd9Sstevel@tonic-gate 		}
9057c478bd9Sstevel@tonic-gate 	} else
9067c478bd9Sstevel@tonic-gate 		return (0);
9077c478bd9Sstevel@tonic-gate }
9087c478bd9Sstevel@tonic-gate 
9099acbbeafSnn35248 int
setthreadnice(pcnice_t * pcnice,kthread_t * tp)9107c478bd9Sstevel@tonic-gate setthreadnice(pcnice_t *pcnice, kthread_t *tp)
9117c478bd9Sstevel@tonic-gate {
912d4204c85Sraf 	int error;
9137c478bd9Sstevel@tonic-gate 	int nice;
9147c478bd9Sstevel@tonic-gate 	int inc;
9157c478bd9Sstevel@tonic-gate 	id_t rtcid;
9167c478bd9Sstevel@tonic-gate 
9177c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&pidlock));
9187c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&(ttoproc(tp)->p_lock)));
9197c478bd9Sstevel@tonic-gate 
9207c478bd9Sstevel@tonic-gate 	/*
9217c478bd9Sstevel@tonic-gate 	 * The XPG5 standard requires that any realtime process or thread
9227c478bd9Sstevel@tonic-gate 	 * must be unaffected by a call to setpriority().
9237c478bd9Sstevel@tonic-gate 	 */
9247c478bd9Sstevel@tonic-gate 	error = getcidbyname("RT", &rtcid);
925d4204c85Sraf 	if (error == 0 && tp->t_cid == rtcid) {
9267c478bd9Sstevel@tonic-gate 		if (pcnice->pc_op == PC_SETNICE)
927d4204c85Sraf 			return (0);
9287c478bd9Sstevel@tonic-gate 	}
9297c478bd9Sstevel@tonic-gate 
9307c478bd9Sstevel@tonic-gate 	if ((error = CL_DONICE(tp, CRED(), 0, &nice)) != 0)
9317c478bd9Sstevel@tonic-gate 		return (error);
9327c478bd9Sstevel@tonic-gate 
9337c478bd9Sstevel@tonic-gate 	if (pcnice->pc_op == PC_GETNICE) {
9347c478bd9Sstevel@tonic-gate 		/*
9357c478bd9Sstevel@tonic-gate 		 * If there is no change to priority, we should return the
9367c478bd9Sstevel@tonic-gate 		 * highest priority (lowest numerical value) pertaining to
9377c478bd9Sstevel@tonic-gate 		 * any of the specified threads.
9387c478bd9Sstevel@tonic-gate 		 */
9397c478bd9Sstevel@tonic-gate 		if (nice < pcnice->pc_val)
9407c478bd9Sstevel@tonic-gate 			pcnice->pc_val = nice;
9417c478bd9Sstevel@tonic-gate 	} else {
9427c478bd9Sstevel@tonic-gate 		ASSERT(pcnice->pc_op == PC_SETNICE);
9437c478bd9Sstevel@tonic-gate 		/*
9447c478bd9Sstevel@tonic-gate 		 * Try to change the nice value of the thread.
9457c478bd9Sstevel@tonic-gate 		 */
9467c478bd9Sstevel@tonic-gate 		inc = pcnice->pc_val - nice;
9477c478bd9Sstevel@tonic-gate 
9487c478bd9Sstevel@tonic-gate 		error = CL_DONICE(tp, CRED(), inc, &inc);
949d4204c85Sraf 		schedctl_set_cidpri(tp);
9507c478bd9Sstevel@tonic-gate 	}
9517c478bd9Sstevel@tonic-gate 
9527c478bd9Sstevel@tonic-gate 	return (error);
9537c478bd9Sstevel@tonic-gate }
9547c478bd9Sstevel@tonic-gate 
9559acbbeafSnn35248 int
setprocnice(proc_t * pp,pcnice_t * pcnice)9567c478bd9Sstevel@tonic-gate setprocnice(proc_t *pp, pcnice_t *pcnice)
9577c478bd9Sstevel@tonic-gate {
9587c478bd9Sstevel@tonic-gate 	kthread_t *tp;
9597c478bd9Sstevel@tonic-gate 	int retval = 0;
960d4204c85Sraf 	int error;
9617c478bd9Sstevel@tonic-gate 
9627c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&pidlock));
9637c478bd9Sstevel@tonic-gate 	mutex_enter(&pp->p_lock);
9647c478bd9Sstevel@tonic-gate 
9657c478bd9Sstevel@tonic-gate 	if ((tp = pp->p_tlist) == NULL) {
9667c478bd9Sstevel@tonic-gate 		mutex_exit(&pp->p_lock);
9677c478bd9Sstevel@tonic-gate 		return (ESRCH);
9687c478bd9Sstevel@tonic-gate 	}
9697c478bd9Sstevel@tonic-gate 
9707c478bd9Sstevel@tonic-gate 	/*
9717c478bd9Sstevel@tonic-gate 	 * Check permissions before changing the nice value.
9727c478bd9Sstevel@tonic-gate 	 */
9737c478bd9Sstevel@tonic-gate 	if (pcnice->pc_op == PC_SETNICE) {
9747c478bd9Sstevel@tonic-gate 		if (!prochasprocperm(pp, curproc, CRED())) {
9757c478bd9Sstevel@tonic-gate 			mutex_exit(&pp->p_lock);
9767c478bd9Sstevel@tonic-gate 			return (EPERM);
9777c478bd9Sstevel@tonic-gate 		}
9787c478bd9Sstevel@tonic-gate 	}
9797c478bd9Sstevel@tonic-gate 
9807c478bd9Sstevel@tonic-gate 	do {
9817c478bd9Sstevel@tonic-gate 		error = setthreadnice(pcnice, tp);
9827c478bd9Sstevel@tonic-gate 		if (error)
9837c478bd9Sstevel@tonic-gate 			retval = error;
9847c478bd9Sstevel@tonic-gate 	} while ((tp = tp->t_forw) != pp->p_tlist);
9857c478bd9Sstevel@tonic-gate 
9867c478bd9Sstevel@tonic-gate 	mutex_exit(&pp->p_lock);
9877c478bd9Sstevel@tonic-gate 	return (retval);
9887c478bd9Sstevel@tonic-gate }
9897c478bd9Sstevel@tonic-gate 
9907c478bd9Sstevel@tonic-gate /*
9917c478bd9Sstevel@tonic-gate  * Update the nice value of the specified LWP or set of processes.
9927c478bd9Sstevel@tonic-gate  */
9937c478bd9Sstevel@tonic-gate static int
donice(procset_t * procset,pcnice_t * pcnice)9947c478bd9Sstevel@tonic-gate donice(procset_t *procset, pcnice_t *pcnice)
9957c478bd9Sstevel@tonic-gate {
9967c478bd9Sstevel@tonic-gate 	int err_proc = 0;
9977c478bd9Sstevel@tonic-gate 	int err_thread = 0;
9987c478bd9Sstevel@tonic-gate 	int err = 0;
9997c478bd9Sstevel@tonic-gate 
10007c478bd9Sstevel@tonic-gate 	/*
10017c478bd9Sstevel@tonic-gate 	 * Sanity check.
10027c478bd9Sstevel@tonic-gate 	 */
10037c478bd9Sstevel@tonic-gate 	if (pcnice->pc_op != PC_GETNICE && pcnice->pc_op != PC_SETNICE)
10047c478bd9Sstevel@tonic-gate 		return (EINVAL);
10057c478bd9Sstevel@tonic-gate 
10067c478bd9Sstevel@tonic-gate 	/*
10077c478bd9Sstevel@tonic-gate 	 * If it is PC_GETNICE operation then set pc_val to the largest
10087c478bd9Sstevel@tonic-gate 	 * possible nice value to help us find the lowest nice value
10097c478bd9Sstevel@tonic-gate 	 * pertaining to any of the specified processes.
10107c478bd9Sstevel@tonic-gate 	 */
10117c478bd9Sstevel@tonic-gate 	if (pcnice->pc_op == PC_GETNICE)
10127c478bd9Sstevel@tonic-gate 		pcnice->pc_val = NZERO;
10137c478bd9Sstevel@tonic-gate 
10147c478bd9Sstevel@tonic-gate 	if (procset->p_lidtype != P_LWPID ||
10157c478bd9Sstevel@tonic-gate 	    procset->p_ridtype != P_LWPID)
10167c478bd9Sstevel@tonic-gate 		err_proc = dotoprocs(procset, setprocnice, (char *)pcnice);
10177c478bd9Sstevel@tonic-gate 
10187c478bd9Sstevel@tonic-gate 	if (procset->p_lidtype == P_LWPID || procset->p_ridtype == P_LWPID) {
10197c478bd9Sstevel@tonic-gate 		err_thread = dotolwp(procset, setthreadnice, (char *)pcnice);
10207c478bd9Sstevel@tonic-gate 		/*
10217c478bd9Sstevel@tonic-gate 		 * dotolwp() can return with p_lock held.  This is required
10227c478bd9Sstevel@tonic-gate 		 * for the priocntl GETPARMS case.  So, here we just release
10237c478bd9Sstevel@tonic-gate 		 * the p_lock.
10247c478bd9Sstevel@tonic-gate 		 */
10257c478bd9Sstevel@tonic-gate 		if (MUTEX_HELD(&curproc->p_lock))
10267c478bd9Sstevel@tonic-gate 			mutex_exit(&curproc->p_lock);
10277c478bd9Sstevel@tonic-gate 
10287c478bd9Sstevel@tonic-gate 		/*
10297c478bd9Sstevel@tonic-gate 		 * If we were called for a single LWP, then ignore ESRCH
10307c478bd9Sstevel@tonic-gate 		 * returned by the previous dotoprocs() call.
10317c478bd9Sstevel@tonic-gate 		 */
10327c478bd9Sstevel@tonic-gate 		if (err_proc == ESRCH)
10337c478bd9Sstevel@tonic-gate 			err_proc = 0;
10347c478bd9Sstevel@tonic-gate 	}
10357c478bd9Sstevel@tonic-gate 
10367c478bd9Sstevel@tonic-gate 	/*
10377c478bd9Sstevel@tonic-gate 	 * dotoprocs() ignores the init process if it is in the set, unless
10387c478bd9Sstevel@tonic-gate 	 * it was the only process found. We want to make sure init is not
10397c478bd9Sstevel@tonic-gate 	 * excluded if we're going PC_GETNICE operation.
10407c478bd9Sstevel@tonic-gate 	 */
10417c478bd9Sstevel@tonic-gate 	if (pcnice->pc_op == PC_GETNICE) {
10427c478bd9Sstevel@tonic-gate 		proc_t *initpp;
10437c478bd9Sstevel@tonic-gate 
10447c478bd9Sstevel@tonic-gate 		mutex_enter(&pidlock);
1045def11082Srh87107 		if ((initpp = prfind(P_INITPID)) != NULL) {
1046def11082Srh87107 			mutex_enter(&initpp->p_lock);
1047def11082Srh87107 			if (procinset(initpp, procset)) {
1048def11082Srh87107 				mutex_exit(&initpp->p_lock);
10497c478bd9Sstevel@tonic-gate 				err = setprocnice(initpp, pcnice);
1050def11082Srh87107 			} else {
1051def11082Srh87107 				mutex_exit(&initpp->p_lock);
1052def11082Srh87107 			}
1053def11082Srh87107 		}
10547c478bd9Sstevel@tonic-gate 		mutex_exit(&pidlock);
10557c478bd9Sstevel@tonic-gate 	}
10567c478bd9Sstevel@tonic-gate 
10577c478bd9Sstevel@tonic-gate 	/*
10587c478bd9Sstevel@tonic-gate 	 * We're returning the latest error here that we've got back from
10597c478bd9Sstevel@tonic-gate 	 * the setthreadnice() or setprocnice(). That is, err_thread and/or
10607c478bd9Sstevel@tonic-gate 	 * err_proc can be replaced by err.
10617c478bd9Sstevel@tonic-gate 	 */
10627c478bd9Sstevel@tonic-gate 	if (!err)
10637c478bd9Sstevel@tonic-gate 		err = err_thread ? err_thread : err_proc;
10647c478bd9Sstevel@tonic-gate 
10657c478bd9Sstevel@tonic-gate 	return (err);
10667c478bd9Sstevel@tonic-gate }
1067d4204c85Sraf 
1068d4204c85Sraf int
setthreadprio(pcprio_t * pcprio,kthread_t * tp)1069d4204c85Sraf setthreadprio(pcprio_t *pcprio, kthread_t *tp)
1070d4204c85Sraf {
1071d4204c85Sraf 	int prio = 0;
1072d4204c85Sraf 	int incr;
1073d4204c85Sraf 	int error;
1074d4204c85Sraf 
1075d4204c85Sraf 	ASSERT(MUTEX_HELD(&pidlock));
1076d4204c85Sraf 	ASSERT(MUTEX_HELD(&(ttoproc(tp)->p_lock)));
1077d4204c85Sraf 
1078d4204c85Sraf 	if (pcprio->pc_op == PC_SETPRIO && pcprio->pc_cid != tp->t_cid) {
1079d4204c85Sraf 		/*
1080d4204c85Sraf 		 * Target thread must change to new class.
1081d4204c85Sraf 		 * See comments in parmsset(), from where this code was copied.
1082d4204c85Sraf 		 */
1083d4204c85Sraf 		void *bufp = NULL;
1084d4204c85Sraf 		caddr_t clprocp = (caddr_t)tp->t_cldata;
1085d4204c85Sraf 		id_t oldcid = tp->t_cid;
1086d4204c85Sraf 
1087d4204c85Sraf 		error = CL_CANEXIT(tp, NULL);
1088d4204c85Sraf 		if (error)
1089d4204c85Sraf 			return (error);
1090d4204c85Sraf 		if (CL_ALLOC(&bufp, pcprio->pc_cid, KM_NOSLEEP) != 0)
1091d4204c85Sraf 			return (ENOMEM);
1092d4204c85Sraf 		error = CL_ENTERCLASS(tp, pcprio->pc_cid, NULL, CRED(), bufp);
1093d4204c85Sraf 		if (error) {
1094d4204c85Sraf 			CL_FREE(pcprio->pc_cid, bufp);
1095d4204c85Sraf 			return (error);
1096d4204c85Sraf 		}
1097d4204c85Sraf 		CL_EXITCLASS(oldcid, clprocp);
1098d4204c85Sraf 		schedctl_set_cidpri(tp);
1099d4204c85Sraf 	}
1100d4204c85Sraf 
1101d4204c85Sraf 	if ((error = CL_DOPRIO(tp, CRED(), 0, &prio)) != 0)
1102d4204c85Sraf 		return (error);
1103d4204c85Sraf 
1104d4204c85Sraf 	if (pcprio->pc_op == PC_GETPRIO) {
1105d4204c85Sraf 		/*
1106d4204c85Sraf 		 * If we are not setting the priority, we should return the
1107d4204c85Sraf 		 * highest priority pertaining to any of the specified threads.
1108d4204c85Sraf 		 */
1109d4204c85Sraf 		if (prio > pcprio->pc_val) {
1110d4204c85Sraf 			pcprio->pc_cid = tp->t_cid;
1111d4204c85Sraf 			pcprio->pc_val = prio;
1112d4204c85Sraf 		}
1113d4204c85Sraf 	} else if (prio != pcprio->pc_val) {
1114d4204c85Sraf 		/*
1115d4204c85Sraf 		 * Try to change the priority of the thread.
1116d4204c85Sraf 		 */
1117d4204c85Sraf 		incr = pcprio->pc_val - prio;
1118d4204c85Sraf 		error = CL_DOPRIO(tp, CRED(), incr, &prio);
1119d4204c85Sraf 		schedctl_set_cidpri(tp);
1120d4204c85Sraf 	}
1121d4204c85Sraf 
1122d4204c85Sraf 	return (error);
1123d4204c85Sraf }
1124d4204c85Sraf 
1125d4204c85Sraf int
setprocprio(proc_t * pp,pcprio_t * pcprio)1126d4204c85Sraf setprocprio(proc_t *pp, pcprio_t *pcprio)
1127d4204c85Sraf {
1128d4204c85Sraf 	kthread_t *tp;
1129d4204c85Sraf 	int retval = 0;
1130d4204c85Sraf 	int error;
1131d4204c85Sraf 
1132d4204c85Sraf 	ASSERT(MUTEX_HELD(&pidlock));
1133d4204c85Sraf 	mutex_enter(&pp->p_lock);
1134d4204c85Sraf 
1135d4204c85Sraf 	if ((tp = pp->p_tlist) == NULL) {
1136d4204c85Sraf 		mutex_exit(&pp->p_lock);
1137d4204c85Sraf 		return (ESRCH);
1138d4204c85Sraf 	}
1139d4204c85Sraf 
1140d4204c85Sraf 	/*
1141d4204c85Sraf 	 * Check permissions before changing the prio value.
1142d4204c85Sraf 	 */
1143d4204c85Sraf 	if (pcprio->pc_op == PC_SETPRIO) {
1144d4204c85Sraf 		if (!prochasprocperm(pp, curproc, CRED())) {
1145d4204c85Sraf 			mutex_exit(&pp->p_lock);
1146d4204c85Sraf 			return (EPERM);
1147d4204c85Sraf 		}
1148d4204c85Sraf 	}
1149d4204c85Sraf 
1150d4204c85Sraf 	do {
1151d4204c85Sraf 		error = setthreadprio(pcprio, tp);
1152d4204c85Sraf 		if (error)
1153d4204c85Sraf 			retval = error;
1154d4204c85Sraf 	} while ((tp = tp->t_forw) != pp->p_tlist);
1155d4204c85Sraf 
1156d4204c85Sraf 	mutex_exit(&pp->p_lock);
1157d4204c85Sraf 	return (retval);
1158d4204c85Sraf }
1159d4204c85Sraf 
1160d4204c85Sraf /*
1161d4204c85Sraf  * Set the class and priority of the specified LWP or set of processes.
1162d4204c85Sraf  */
1163d4204c85Sraf static int
doprio(procset_t * procset,pcprio_t * pcprio)1164d4204c85Sraf doprio(procset_t *procset, pcprio_t *pcprio)
1165d4204c85Sraf {
1166d4204c85Sraf 	int err_proc = 0;
1167d4204c85Sraf 	int err_thread = 0;
1168d4204c85Sraf 	int err = 0;
1169d4204c85Sraf 
1170d4204c85Sraf 	/*
1171d4204c85Sraf 	 * Sanity check.
1172d4204c85Sraf 	 */
1173d4204c85Sraf 	if (pcprio->pc_op != PC_GETPRIO && pcprio->pc_op != PC_SETPRIO)
1174d4204c85Sraf 		return (EINVAL);
1175d4204c85Sraf 	if (pcprio->pc_op == PC_SETPRIO &&
1176d4204c85Sraf 	    (pcprio->pc_cid >= loaded_classes || pcprio->pc_cid < 1))
1177d4204c85Sraf 		return (EINVAL);
1178d4204c85Sraf 
1179d4204c85Sraf 	/*
1180d4204c85Sraf 	 * If it is a PC_GETPRIO operation then set pc_val to the smallest
1181d4204c85Sraf 	 * possible prio value to help us find the highest priority
1182d4204c85Sraf 	 * pertaining to any of the specified processes.
1183d4204c85Sraf 	 */
1184d4204c85Sraf 	if (pcprio->pc_op == PC_GETPRIO)
1185d4204c85Sraf 		pcprio->pc_val = SHRT_MIN;
1186d4204c85Sraf 
1187d4204c85Sraf 	if (procset->p_lidtype != P_LWPID ||
1188d4204c85Sraf 	    procset->p_ridtype != P_LWPID)
1189d4204c85Sraf 		err_proc = dotoprocs(procset, setprocprio, (char *)pcprio);
1190d4204c85Sraf 
1191d4204c85Sraf 	if (procset->p_lidtype == P_LWPID || procset->p_ridtype == P_LWPID) {
1192d4204c85Sraf 		err_thread = dotolwp(procset, setthreadprio, (char *)pcprio);
1193d4204c85Sraf 		/*
1194d4204c85Sraf 		 * dotolwp() can return with p_lock held.  This is required
1195d4204c85Sraf 		 * for the priocntl GETPARMS case.  So, here we just release
1196d4204c85Sraf 		 * the p_lock.
1197d4204c85Sraf 		 */
1198d4204c85Sraf 		if (MUTEX_HELD(&curproc->p_lock))
1199d4204c85Sraf 			mutex_exit(&curproc->p_lock);
1200d4204c85Sraf 
1201d4204c85Sraf 		/*
1202d4204c85Sraf 		 * If we were called for a single LWP, then ignore ESRCH
1203d4204c85Sraf 		 * returned by the previous dotoprocs() call.
1204d4204c85Sraf 		 */
1205d4204c85Sraf 		if (err_proc == ESRCH)
1206d4204c85Sraf 			err_proc = 0;
1207d4204c85Sraf 	}
1208d4204c85Sraf 
1209d4204c85Sraf 	/*
1210d4204c85Sraf 	 * dotoprocs() ignores the init process if it is in the set, unless
1211d4204c85Sraf 	 * it was the only process found. We want to make sure init is not
1212d4204c85Sraf 	 * excluded if we're going PC_GETPRIO operation.
1213d4204c85Sraf 	 */
1214d4204c85Sraf 	if (pcprio->pc_op == PC_GETPRIO) {
1215d4204c85Sraf 		proc_t *initpp;
1216d4204c85Sraf 
1217d4204c85Sraf 		mutex_enter(&pidlock);
1218def11082Srh87107 		if ((initpp = prfind(P_INITPID)) != NULL) {
1219def11082Srh87107 			mutex_enter(&initpp->p_lock);
1220def11082Srh87107 			if (procinset(initpp, procset)) {
1221def11082Srh87107 				mutex_exit(&initpp->p_lock);
1222d4204c85Sraf 				err = setprocprio(initpp, pcprio);
1223def11082Srh87107 			} else {
1224def11082Srh87107 				mutex_exit(&initpp->p_lock);
1225def11082Srh87107 			}
1226def11082Srh87107 		}
1227d4204c85Sraf 		mutex_exit(&pidlock);
1228d4204c85Sraf 	}
1229d4204c85Sraf 
1230d4204c85Sraf 	/*
1231d4204c85Sraf 	 * We're returning the latest error here that we've got back from
1232d4204c85Sraf 	 * the setthreadprio() or setprocprio(). That is, err_thread and/or
1233d4204c85Sraf 	 * err_proc can be replaced by err.
1234d4204c85Sraf 	 */
1235d4204c85Sraf 	if (!err)
1236d4204c85Sraf 		err = err_thread ? err_thread : err_proc;
1237d4204c85Sraf 
1238d4204c85Sraf 	return (err);
1239d4204c85Sraf }
1240