xref: /titanic_44/usr/src/uts/common/syscall/pset.c (revision d5fbc2382ba3b6e27ed2481fcfd8f091b5850cd7)
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*d5fbc238Sfr157268  * Common Development and Distribution License (the "License").
6*d5fbc238Sfr157268  * 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  */
217c478bd9Sstevel@tonic-gate /*
22*d5fbc238Sfr157268  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate #include <sys/types.h>
297c478bd9Sstevel@tonic-gate #include <sys/systm.h>
307c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
317c478bd9Sstevel@tonic-gate #include <sys/cpuvar.h>
327c478bd9Sstevel@tonic-gate #include <sys/thread.h>
337c478bd9Sstevel@tonic-gate #include <sys/disp.h>
347c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
357c478bd9Sstevel@tonic-gate #include <sys/debug.h>
367c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
377c478bd9Sstevel@tonic-gate #include <sys/cpupart.h>
387c478bd9Sstevel@tonic-gate #include <sys/pset.h>
397c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
407c478bd9Sstevel@tonic-gate #include <sys/syscall.h>
417c478bd9Sstevel@tonic-gate #include <sys/task.h>
427c478bd9Sstevel@tonic-gate #include <sys/loadavg.h>
437c478bd9Sstevel@tonic-gate #include <sys/fss.h>
447c478bd9Sstevel@tonic-gate #include <sys/pool.h>
457c478bd9Sstevel@tonic-gate #include <sys/pool_pset.h>
467c478bd9Sstevel@tonic-gate #include <sys/policy.h>
477c478bd9Sstevel@tonic-gate #include <sys/zone.h>
487c478bd9Sstevel@tonic-gate #include <sys/contract/process_impl.h>
497c478bd9Sstevel@tonic-gate 
507c478bd9Sstevel@tonic-gate static int	pset(int, long, long, long, long);
517c478bd9Sstevel@tonic-gate 
527c478bd9Sstevel@tonic-gate static struct sysent pset_sysent = {
537c478bd9Sstevel@tonic-gate 	5,
547c478bd9Sstevel@tonic-gate 	SE_ARGC | SE_NOUNLOAD,
557c478bd9Sstevel@tonic-gate 	(int (*)())pset,
567c478bd9Sstevel@tonic-gate };
577c478bd9Sstevel@tonic-gate 
587c478bd9Sstevel@tonic-gate static struct modlsys modlsys = {
597c478bd9Sstevel@tonic-gate 	&mod_syscallops, "processor sets", &pset_sysent
607c478bd9Sstevel@tonic-gate };
617c478bd9Sstevel@tonic-gate 
627c478bd9Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
637c478bd9Sstevel@tonic-gate static struct modlsys modlsys32 = {
647c478bd9Sstevel@tonic-gate 	&mod_syscallops32, "32-bit pset(2) syscall", &pset_sysent
657c478bd9Sstevel@tonic-gate };
667c478bd9Sstevel@tonic-gate #endif
677c478bd9Sstevel@tonic-gate 
687c478bd9Sstevel@tonic-gate static struct modlinkage modlinkage = {
697c478bd9Sstevel@tonic-gate 	MODREV_1,
707c478bd9Sstevel@tonic-gate 	&modlsys,
717c478bd9Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
727c478bd9Sstevel@tonic-gate 	&modlsys32,
737c478bd9Sstevel@tonic-gate #endif
747c478bd9Sstevel@tonic-gate 	NULL
757c478bd9Sstevel@tonic-gate };
767c478bd9Sstevel@tonic-gate 
777c478bd9Sstevel@tonic-gate #define	PSET_BADATTR(attr)	((~PSET_NOESCAPE) & (attr))
787c478bd9Sstevel@tonic-gate 
797c478bd9Sstevel@tonic-gate int
807c478bd9Sstevel@tonic-gate _init(void)
817c478bd9Sstevel@tonic-gate {
827c478bd9Sstevel@tonic-gate 	return (mod_install(&modlinkage));
837c478bd9Sstevel@tonic-gate }
847c478bd9Sstevel@tonic-gate 
857c478bd9Sstevel@tonic-gate int
867c478bd9Sstevel@tonic-gate _info(struct modinfo *modinfop)
877c478bd9Sstevel@tonic-gate {
887c478bd9Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
897c478bd9Sstevel@tonic-gate }
907c478bd9Sstevel@tonic-gate 
917c478bd9Sstevel@tonic-gate static int
927c478bd9Sstevel@tonic-gate pset_create(psetid_t *psetp)
937c478bd9Sstevel@tonic-gate {
947c478bd9Sstevel@tonic-gate 	psetid_t newpset;
957c478bd9Sstevel@tonic-gate 	int error;
967c478bd9Sstevel@tonic-gate 
977c478bd9Sstevel@tonic-gate 	if (secpolicy_pset(CRED()) != 0)
987c478bd9Sstevel@tonic-gate 		return (set_errno(EPERM));
997c478bd9Sstevel@tonic-gate 
1007c478bd9Sstevel@tonic-gate 	pool_lock();
1017c478bd9Sstevel@tonic-gate 	if (pool_state == POOL_ENABLED) {
1027c478bd9Sstevel@tonic-gate 		pool_unlock();
1037c478bd9Sstevel@tonic-gate 		return (set_errno(ENOTSUP));
1047c478bd9Sstevel@tonic-gate 	}
1057c478bd9Sstevel@tonic-gate 	error = cpupart_create(&newpset);
1067c478bd9Sstevel@tonic-gate 	if (error) {
1077c478bd9Sstevel@tonic-gate 		pool_unlock();
1087c478bd9Sstevel@tonic-gate 		return (set_errno(error));
1097c478bd9Sstevel@tonic-gate 	}
1107c478bd9Sstevel@tonic-gate 	if (copyout(&newpset, psetp, sizeof (psetid_t)) != 0) {
1117c478bd9Sstevel@tonic-gate 		(void) cpupart_destroy(newpset);
1127c478bd9Sstevel@tonic-gate 		pool_unlock();
1137c478bd9Sstevel@tonic-gate 		return (set_errno(EFAULT));
1147c478bd9Sstevel@tonic-gate 	}
1157c478bd9Sstevel@tonic-gate 	pool_unlock();
1167c478bd9Sstevel@tonic-gate 	return (error);
1177c478bd9Sstevel@tonic-gate }
1187c478bd9Sstevel@tonic-gate 
1197c478bd9Sstevel@tonic-gate static int
1207c478bd9Sstevel@tonic-gate pset_destroy(psetid_t pset)
1217c478bd9Sstevel@tonic-gate {
1227c478bd9Sstevel@tonic-gate 	int error;
1237c478bd9Sstevel@tonic-gate 
1247c478bd9Sstevel@tonic-gate 	if (secpolicy_pset(CRED()) != 0)
1257c478bd9Sstevel@tonic-gate 		return (set_errno(EPERM));
1267c478bd9Sstevel@tonic-gate 
1277c478bd9Sstevel@tonic-gate 	pool_lock();
1287c478bd9Sstevel@tonic-gate 	if (pool_state == POOL_ENABLED) {
1297c478bd9Sstevel@tonic-gate 		pool_unlock();
1307c478bd9Sstevel@tonic-gate 		return (set_errno(ENOTSUP));
1317c478bd9Sstevel@tonic-gate 	}
1327c478bd9Sstevel@tonic-gate 	error = cpupart_destroy(pset);
1337c478bd9Sstevel@tonic-gate 	pool_unlock();
1347c478bd9Sstevel@tonic-gate 	if (error)
1357c478bd9Sstevel@tonic-gate 		return (set_errno(error));
1367c478bd9Sstevel@tonic-gate 	else
1377c478bd9Sstevel@tonic-gate 		return (0);
1387c478bd9Sstevel@tonic-gate }
1397c478bd9Sstevel@tonic-gate 
1407c478bd9Sstevel@tonic-gate static int
1417c478bd9Sstevel@tonic-gate pset_assign(psetid_t pset, processorid_t cpuid, psetid_t *opset, int forced)
1427c478bd9Sstevel@tonic-gate {
1437c478bd9Sstevel@tonic-gate 	psetid_t oldpset;
1447c478bd9Sstevel@tonic-gate 	int	error = 0;
1457c478bd9Sstevel@tonic-gate 	cpu_t	*cp;
1467c478bd9Sstevel@tonic-gate 
1477c478bd9Sstevel@tonic-gate 	if (pset != PS_QUERY && secpolicy_pset(CRED()) != 0)
1487c478bd9Sstevel@tonic-gate 		return (set_errno(EPERM));
1497c478bd9Sstevel@tonic-gate 
1507c478bd9Sstevel@tonic-gate 	pool_lock();
1517c478bd9Sstevel@tonic-gate 	if (pset != PS_QUERY && pool_state == POOL_ENABLED) {
1527c478bd9Sstevel@tonic-gate 		pool_unlock();
1537c478bd9Sstevel@tonic-gate 		return (set_errno(ENOTSUP));
1547c478bd9Sstevel@tonic-gate 	}
1557c478bd9Sstevel@tonic-gate 
1567c478bd9Sstevel@tonic-gate 	mutex_enter(&cpu_lock);
1577c478bd9Sstevel@tonic-gate 	if ((cp = cpu_get(cpuid)) == NULL) {
1587c478bd9Sstevel@tonic-gate 		mutex_exit(&cpu_lock);
1597c478bd9Sstevel@tonic-gate 		pool_unlock();
1607c478bd9Sstevel@tonic-gate 		return (set_errno(EINVAL));
1617c478bd9Sstevel@tonic-gate 	}
1627c478bd9Sstevel@tonic-gate 
1637c478bd9Sstevel@tonic-gate 	oldpset = cpupart_query_cpu(cp);
1647c478bd9Sstevel@tonic-gate 
1657c478bd9Sstevel@tonic-gate 	if (pset != PS_QUERY)
1667c478bd9Sstevel@tonic-gate 		error = cpupart_attach_cpu(pset, cp, forced);
1677c478bd9Sstevel@tonic-gate 	mutex_exit(&cpu_lock);
1687c478bd9Sstevel@tonic-gate 	pool_unlock();
1697c478bd9Sstevel@tonic-gate 
1707c478bd9Sstevel@tonic-gate 	if (error)
1717c478bd9Sstevel@tonic-gate 		return (set_errno(error));
1727c478bd9Sstevel@tonic-gate 
1737c478bd9Sstevel@tonic-gate 	if (opset != NULL)
1747c478bd9Sstevel@tonic-gate 		if (copyout(&oldpset, opset, sizeof (psetid_t)) != 0)
1757c478bd9Sstevel@tonic-gate 			return (set_errno(EFAULT));
1767c478bd9Sstevel@tonic-gate 
1777c478bd9Sstevel@tonic-gate 	return (0);
1787c478bd9Sstevel@tonic-gate }
1797c478bd9Sstevel@tonic-gate 
1807c478bd9Sstevel@tonic-gate static int
1817c478bd9Sstevel@tonic-gate pset_info(psetid_t pset, int *typep, uint_t *numcpusp,
1827c478bd9Sstevel@tonic-gate     processorid_t *cpulistp)
1837c478bd9Sstevel@tonic-gate {
1847c478bd9Sstevel@tonic-gate 	int pset_type;
1857c478bd9Sstevel@tonic-gate 	uint_t user_ncpus = 0, real_ncpus, copy_ncpus;
1867c478bd9Sstevel@tonic-gate 	processorid_t *pset_cpus = NULL;
1877c478bd9Sstevel@tonic-gate 	int error = 0;
1887c478bd9Sstevel@tonic-gate 
1897c478bd9Sstevel@tonic-gate 	if (numcpusp != NULL) {
1907c478bd9Sstevel@tonic-gate 		if (copyin(numcpusp, &user_ncpus, sizeof (uint_t)) != 0)
1917c478bd9Sstevel@tonic-gate 			return (set_errno(EFAULT));
1927c478bd9Sstevel@tonic-gate 	}
1937c478bd9Sstevel@tonic-gate 
1947c478bd9Sstevel@tonic-gate 	if (user_ncpus > max_ncpus)	/* sanity check */
1957c478bd9Sstevel@tonic-gate 		user_ncpus = max_ncpus;
1967c478bd9Sstevel@tonic-gate 	if (user_ncpus != 0 && cpulistp != NULL)
1977c478bd9Sstevel@tonic-gate 		pset_cpus = kmem_alloc(sizeof (processorid_t) * user_ncpus,
1987c478bd9Sstevel@tonic-gate 		    KM_SLEEP);
1997c478bd9Sstevel@tonic-gate 
2007c478bd9Sstevel@tonic-gate 	real_ncpus = user_ncpus;
2017c478bd9Sstevel@tonic-gate 	if ((error = cpupart_get_cpus(&pset, pset_cpus, &real_ncpus)) != 0)
2027c478bd9Sstevel@tonic-gate 		goto out;
2037c478bd9Sstevel@tonic-gate 
2047c478bd9Sstevel@tonic-gate 	/*
2057c478bd9Sstevel@tonic-gate 	 * Now copyout the information about this processor set.
2067c478bd9Sstevel@tonic-gate 	 */
2077c478bd9Sstevel@tonic-gate 
2087c478bd9Sstevel@tonic-gate 	/*
2097c478bd9Sstevel@tonic-gate 	 * Get number of cpus to copy back.  If the user didn't pass in
2107c478bd9Sstevel@tonic-gate 	 * a big enough buffer, only copy back as many cpus as fits in
2117c478bd9Sstevel@tonic-gate 	 * the buffer but copy back the real number of cpus.
2127c478bd9Sstevel@tonic-gate 	 */
2137c478bd9Sstevel@tonic-gate 
2147c478bd9Sstevel@tonic-gate 	if (user_ncpus != 0 && cpulistp != NULL) {
2157c478bd9Sstevel@tonic-gate 		copy_ncpus = MIN(real_ncpus, user_ncpus);
2167c478bd9Sstevel@tonic-gate 		if (copyout(pset_cpus, cpulistp,
2177c478bd9Sstevel@tonic-gate 		    sizeof (processorid_t) * copy_ncpus) != 0) {
2187c478bd9Sstevel@tonic-gate 			error = EFAULT;
2197c478bd9Sstevel@tonic-gate 			goto out;
2207c478bd9Sstevel@tonic-gate 		}
2217c478bd9Sstevel@tonic-gate 	}
2227c478bd9Sstevel@tonic-gate 	if (pset_cpus != NULL)
2237c478bd9Sstevel@tonic-gate 		kmem_free(pset_cpus, sizeof (processorid_t) * user_ncpus);
2247c478bd9Sstevel@tonic-gate 	if (typep != NULL) {
2257c478bd9Sstevel@tonic-gate 		if (pset == PS_NONE)
2267c478bd9Sstevel@tonic-gate 			pset_type = PS_NONE;
2277c478bd9Sstevel@tonic-gate 		else
2287c478bd9Sstevel@tonic-gate 			pset_type = PS_PRIVATE;
2297c478bd9Sstevel@tonic-gate 		if (copyout(&pset_type, typep, sizeof (int)) != 0)
2307c478bd9Sstevel@tonic-gate 			return (set_errno(EFAULT));
2317c478bd9Sstevel@tonic-gate 	}
2327c478bd9Sstevel@tonic-gate 	if (numcpusp != NULL)
2337c478bd9Sstevel@tonic-gate 		if (copyout(&real_ncpus, numcpusp, sizeof (uint_t)) != 0)
2347c478bd9Sstevel@tonic-gate 			return (set_errno(EFAULT));
2357c478bd9Sstevel@tonic-gate 	return (0);
2367c478bd9Sstevel@tonic-gate 
2377c478bd9Sstevel@tonic-gate out:
2387c478bd9Sstevel@tonic-gate 	if (pset_cpus != NULL)
2397c478bd9Sstevel@tonic-gate 		kmem_free(pset_cpus, sizeof (processorid_t) * user_ncpus);
2407c478bd9Sstevel@tonic-gate 	return (set_errno(error));
2417c478bd9Sstevel@tonic-gate }
2427c478bd9Sstevel@tonic-gate 
2437c478bd9Sstevel@tonic-gate static int
2447c478bd9Sstevel@tonic-gate pset_bind_thread(kthread_t *tp, psetid_t pset, psetid_t *oldpset, void *projbuf,
2457c478bd9Sstevel@tonic-gate     void *zonebuf)
2467c478bd9Sstevel@tonic-gate {
2477c478bd9Sstevel@tonic-gate 	int error = 0;
2487c478bd9Sstevel@tonic-gate 
2497c478bd9Sstevel@tonic-gate 	ASSERT(pool_lock_held());
2507c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cpu_lock));
2517c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&ttoproc(tp)->p_lock));
2527c478bd9Sstevel@tonic-gate 
2537c478bd9Sstevel@tonic-gate 	*oldpset = tp->t_bind_pset;
2547c478bd9Sstevel@tonic-gate 	if (pset != PS_QUERY) {
2557c478bd9Sstevel@tonic-gate 		/*
2567c478bd9Sstevel@tonic-gate 		 * Must have the same UID as the target process or
2577c478bd9Sstevel@tonic-gate 		 * have PRIV_PROC_OWNER privilege.
2587c478bd9Sstevel@tonic-gate 		 */
2597c478bd9Sstevel@tonic-gate 		if (!hasprocperm(tp->t_cred, CRED()))
2607c478bd9Sstevel@tonic-gate 			return (EPERM);
2617c478bd9Sstevel@tonic-gate 		/*
2627c478bd9Sstevel@tonic-gate 		 * Unbinding of an unbound thread should always succeed.
2637c478bd9Sstevel@tonic-gate 		 */
2647c478bd9Sstevel@tonic-gate 		if (*oldpset == PS_NONE && pset == PS_NONE)
2657c478bd9Sstevel@tonic-gate 			return (0);
2667c478bd9Sstevel@tonic-gate 		/*
2677c478bd9Sstevel@tonic-gate 		 * Only privileged processes can move threads from psets with
2687c478bd9Sstevel@tonic-gate 		 * PSET_NOESCAPE attribute.
2697c478bd9Sstevel@tonic-gate 		 */
2707c478bd9Sstevel@tonic-gate 		if ((tp->t_cpupart->cp_attr & PSET_NOESCAPE) &&
2717c478bd9Sstevel@tonic-gate 		    secpolicy_pset(CRED()) != 0)
2727c478bd9Sstevel@tonic-gate 			return (EPERM);
2737c478bd9Sstevel@tonic-gate 		if ((error = cpupart_bind_thread(tp, pset, 0,
2747c478bd9Sstevel@tonic-gate 		    projbuf, zonebuf)) == 0)
2757c478bd9Sstevel@tonic-gate 			tp->t_bind_pset = pset;
2767c478bd9Sstevel@tonic-gate 	}
2777c478bd9Sstevel@tonic-gate 	return (error);
2787c478bd9Sstevel@tonic-gate }
2797c478bd9Sstevel@tonic-gate 
2807c478bd9Sstevel@tonic-gate static int
2817c478bd9Sstevel@tonic-gate pset_bind_process(proc_t *pp, psetid_t pset, psetid_t *oldpset, void *projbuf,
2827c478bd9Sstevel@tonic-gate     void *zonebuf)
2837c478bd9Sstevel@tonic-gate {
2847c478bd9Sstevel@tonic-gate 	int error = 0;
2857c478bd9Sstevel@tonic-gate 	kthread_t *tp;
2867c478bd9Sstevel@tonic-gate 
2877c478bd9Sstevel@tonic-gate 	/* skip kernel processes */
2887c478bd9Sstevel@tonic-gate 	if (pset != PS_QUERY && pp->p_flag & SSYS) {
2897c478bd9Sstevel@tonic-gate 		*oldpset = PS_NONE;
2907c478bd9Sstevel@tonic-gate 		return (0);
2917c478bd9Sstevel@tonic-gate 	}
2927c478bd9Sstevel@tonic-gate 
2937c478bd9Sstevel@tonic-gate 	mutex_enter(&pp->p_lock);
2947c478bd9Sstevel@tonic-gate 	tp = pp->p_tlist;
2957c478bd9Sstevel@tonic-gate 	if (tp != NULL) {
2967c478bd9Sstevel@tonic-gate 		do {
2977c478bd9Sstevel@tonic-gate 			int rval;
2987c478bd9Sstevel@tonic-gate 
2997c478bd9Sstevel@tonic-gate 			rval = pset_bind_thread(tp, pset, oldpset, projbuf,
3007c478bd9Sstevel@tonic-gate 			    zonebuf);
3017c478bd9Sstevel@tonic-gate 			if (error == 0)
3027c478bd9Sstevel@tonic-gate 				error = rval;
3037c478bd9Sstevel@tonic-gate 		} while ((tp = tp->t_forw) != pp->p_tlist);
3047c478bd9Sstevel@tonic-gate 	} else
3057c478bd9Sstevel@tonic-gate 		error = ESRCH;
3067c478bd9Sstevel@tonic-gate 	mutex_exit(&pp->p_lock);
3077c478bd9Sstevel@tonic-gate 
3087c478bd9Sstevel@tonic-gate 	return (error);
3097c478bd9Sstevel@tonic-gate }
3107c478bd9Sstevel@tonic-gate 
3117c478bd9Sstevel@tonic-gate static int
3127c478bd9Sstevel@tonic-gate pset_bind_task(task_t *tk, psetid_t pset, psetid_t *oldpset, void *projbuf,
3137c478bd9Sstevel@tonic-gate     void *zonebuf)
3147c478bd9Sstevel@tonic-gate {
3157c478bd9Sstevel@tonic-gate 	int error = 0;
3167c478bd9Sstevel@tonic-gate 	proc_t *pp;
3177c478bd9Sstevel@tonic-gate 
3187c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&pidlock));
3197c478bd9Sstevel@tonic-gate 
3207c478bd9Sstevel@tonic-gate 	if ((pp = tk->tk_memb_list) == NULL) {
3217c478bd9Sstevel@tonic-gate 		return (ESRCH);
3227c478bd9Sstevel@tonic-gate 	}
3237c478bd9Sstevel@tonic-gate 
3247c478bd9Sstevel@tonic-gate 	do {
3257c478bd9Sstevel@tonic-gate 		int rval;
3267c478bd9Sstevel@tonic-gate 
3277c478bd9Sstevel@tonic-gate 		rval = pset_bind_process(pp, pset, oldpset, projbuf, zonebuf);
3287c478bd9Sstevel@tonic-gate 		if (error == 0)
3297c478bd9Sstevel@tonic-gate 			error = rval;
3307c478bd9Sstevel@tonic-gate 	} while ((pp = pp->p_tasknext) != tk->tk_memb_list);
3317c478bd9Sstevel@tonic-gate 
3327c478bd9Sstevel@tonic-gate 	return (error);
3337c478bd9Sstevel@tonic-gate }
3347c478bd9Sstevel@tonic-gate 
3357c478bd9Sstevel@tonic-gate static int
3367c478bd9Sstevel@tonic-gate pset_bind_project(kproject_t *kpj, psetid_t pset, psetid_t *oldpset,
3377c478bd9Sstevel@tonic-gate     void *projbuf, void *zonebuf)
3387c478bd9Sstevel@tonic-gate {
3397c478bd9Sstevel@tonic-gate 	int error = 0;
3407c478bd9Sstevel@tonic-gate 	proc_t *pp;
3417c478bd9Sstevel@tonic-gate 
3427c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&pidlock));
3437c478bd9Sstevel@tonic-gate 
3447c478bd9Sstevel@tonic-gate 	for (pp = practive; pp != NULL; pp = pp->p_next) {
3457c478bd9Sstevel@tonic-gate 		if (pp->p_tlist == NULL)
3467c478bd9Sstevel@tonic-gate 			continue;
3477c478bd9Sstevel@tonic-gate 		if (pp->p_task->tk_proj == kpj) {
3487c478bd9Sstevel@tonic-gate 			int rval;
3497c478bd9Sstevel@tonic-gate 
3507c478bd9Sstevel@tonic-gate 			rval = pset_bind_process(pp, pset, oldpset, projbuf,
3517c478bd9Sstevel@tonic-gate 			    zonebuf);
3527c478bd9Sstevel@tonic-gate 			if (error == 0)
3537c478bd9Sstevel@tonic-gate 				error = rval;
3547c478bd9Sstevel@tonic-gate 		}
3557c478bd9Sstevel@tonic-gate 	}
3567c478bd9Sstevel@tonic-gate 
3577c478bd9Sstevel@tonic-gate 	return (error);
3587c478bd9Sstevel@tonic-gate }
3597c478bd9Sstevel@tonic-gate 
3607c478bd9Sstevel@tonic-gate static int
3617c478bd9Sstevel@tonic-gate pset_bind_zone(zone_t *zptr, psetid_t pset, psetid_t *oldpset, void *projbuf,
3627c478bd9Sstevel@tonic-gate     void *zonebuf)
3637c478bd9Sstevel@tonic-gate {
3647c478bd9Sstevel@tonic-gate 	int error = 0;
3657c478bd9Sstevel@tonic-gate 	proc_t *pp;
3667c478bd9Sstevel@tonic-gate 
3677c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&pidlock));
3687c478bd9Sstevel@tonic-gate 
3697c478bd9Sstevel@tonic-gate 	for (pp = practive; pp != NULL; pp = pp->p_next) {
3707c478bd9Sstevel@tonic-gate 		if (pp->p_zone == zptr) {
3717c478bd9Sstevel@tonic-gate 			int rval;
3727c478bd9Sstevel@tonic-gate 
3737c478bd9Sstevel@tonic-gate 			rval = pset_bind_process(pp, pset, oldpset, projbuf,
3747c478bd9Sstevel@tonic-gate 			    zonebuf);
3757c478bd9Sstevel@tonic-gate 			if (error == 0)
3767c478bd9Sstevel@tonic-gate 				error = rval;
3777c478bd9Sstevel@tonic-gate 		}
3787c478bd9Sstevel@tonic-gate 	}
3797c478bd9Sstevel@tonic-gate 
3807c478bd9Sstevel@tonic-gate 	return (error);
3817c478bd9Sstevel@tonic-gate }
3827c478bd9Sstevel@tonic-gate 
3837c478bd9Sstevel@tonic-gate /*
3847c478bd9Sstevel@tonic-gate  * Unbind all threads from the specified processor set, or from all
3857c478bd9Sstevel@tonic-gate  * processor sets.
3867c478bd9Sstevel@tonic-gate  */
3877c478bd9Sstevel@tonic-gate static int
3887c478bd9Sstevel@tonic-gate pset_unbind(psetid_t pset, void *projbuf, void *zonebuf, idtype_t idtype)
3897c478bd9Sstevel@tonic-gate {
3907c478bd9Sstevel@tonic-gate 	psetid_t olbind;
3917c478bd9Sstevel@tonic-gate 	kthread_t *tp;
3927c478bd9Sstevel@tonic-gate 	int error = 0;
3937c478bd9Sstevel@tonic-gate 	int rval;
3947c478bd9Sstevel@tonic-gate 	proc_t *pp;
3957c478bd9Sstevel@tonic-gate 
3967c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cpu_lock));
3977c478bd9Sstevel@tonic-gate 
3987c478bd9Sstevel@tonic-gate 	if (idtype == P_PSETID && cpupart_find(pset) == NULL)
3997c478bd9Sstevel@tonic-gate 		return (EINVAL);
4007c478bd9Sstevel@tonic-gate 
4017c478bd9Sstevel@tonic-gate 	mutex_enter(&pidlock);
4027c478bd9Sstevel@tonic-gate 	for (pp = practive; pp != NULL; pp = pp->p_next) {
4037c478bd9Sstevel@tonic-gate 		mutex_enter(&pp->p_lock);
4047c478bd9Sstevel@tonic-gate 		tp = pp->p_tlist;
4057c478bd9Sstevel@tonic-gate 		/*
4067c478bd9Sstevel@tonic-gate 		 * Skip zombies and kernel processes, and processes in
4077c478bd9Sstevel@tonic-gate 		 * other zones, if called from a non-global zone.
4087c478bd9Sstevel@tonic-gate 		 */
4097c478bd9Sstevel@tonic-gate 		if (tp == NULL || (pp->p_flag & SSYS) ||
4107c478bd9Sstevel@tonic-gate 		    !HASZONEACCESS(curproc, pp->p_zone->zone_id)) {
4117c478bd9Sstevel@tonic-gate 			mutex_exit(&pp->p_lock);
4127c478bd9Sstevel@tonic-gate 			continue;
4137c478bd9Sstevel@tonic-gate 		}
4147c478bd9Sstevel@tonic-gate 		do {
4157c478bd9Sstevel@tonic-gate 			if ((idtype == P_PSETID && tp->t_bind_pset != pset) ||
4167c478bd9Sstevel@tonic-gate 			    (idtype == P_ALL && tp->t_bind_pset == PS_NONE))
4177c478bd9Sstevel@tonic-gate 				continue;
4187c478bd9Sstevel@tonic-gate 			rval = pset_bind_thread(tp, PS_NONE, &olbind,
4197c478bd9Sstevel@tonic-gate 			    projbuf, zonebuf);
4207c478bd9Sstevel@tonic-gate 			if (error == 0)
4217c478bd9Sstevel@tonic-gate 				error = rval;
4227c478bd9Sstevel@tonic-gate 		} while ((tp = tp->t_forw) != pp->p_tlist);
4237c478bd9Sstevel@tonic-gate 		mutex_exit(&pp->p_lock);
4247c478bd9Sstevel@tonic-gate 	}
4257c478bd9Sstevel@tonic-gate 	mutex_exit(&pidlock);
4267c478bd9Sstevel@tonic-gate 	return (error);
4277c478bd9Sstevel@tonic-gate }
4287c478bd9Sstevel@tonic-gate 
4297c478bd9Sstevel@tonic-gate static int
4307c478bd9Sstevel@tonic-gate pset_bind_contract(cont_process_t *ctp, psetid_t pset, psetid_t *oldpset,
4317c478bd9Sstevel@tonic-gate     void *projbuf, void *zonebuf)
4327c478bd9Sstevel@tonic-gate {
4337c478bd9Sstevel@tonic-gate 	int error = 0;
4347c478bd9Sstevel@tonic-gate 	proc_t *pp;
4357c478bd9Sstevel@tonic-gate 
4367c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&pidlock));
4377c478bd9Sstevel@tonic-gate 
4387c478bd9Sstevel@tonic-gate 	for (pp = practive; pp != NULL; pp = pp->p_next) {
4397c478bd9Sstevel@tonic-gate 		if (pp->p_ct_process == ctp) {
4407c478bd9Sstevel@tonic-gate 			int rval;
4417c478bd9Sstevel@tonic-gate 
4427c478bd9Sstevel@tonic-gate 			rval = pset_bind_process(pp, pset, oldpset, projbuf,
4437c478bd9Sstevel@tonic-gate 			    zonebuf);
4447c478bd9Sstevel@tonic-gate 			if (error == 0)
4457c478bd9Sstevel@tonic-gate 				error = rval;
4467c478bd9Sstevel@tonic-gate 		}
4477c478bd9Sstevel@tonic-gate 	}
4487c478bd9Sstevel@tonic-gate 
4497c478bd9Sstevel@tonic-gate 	return (error);
4507c478bd9Sstevel@tonic-gate }
4517c478bd9Sstevel@tonic-gate 
4527c478bd9Sstevel@tonic-gate static int
4537c478bd9Sstevel@tonic-gate pset_bind(psetid_t pset, idtype_t idtype, id_t id, psetid_t *opset)
4547c478bd9Sstevel@tonic-gate {
4557c478bd9Sstevel@tonic-gate 	kthread_t	*tp;
4567c478bd9Sstevel@tonic-gate 	proc_t		*pp;
4577c478bd9Sstevel@tonic-gate 	task_t		*tk;
4587c478bd9Sstevel@tonic-gate 	kproject_t	*kpj;
4597c478bd9Sstevel@tonic-gate 	contract_t	*ct;
4607c478bd9Sstevel@tonic-gate 	zone_t		*zptr;
4617c478bd9Sstevel@tonic-gate 	psetid_t	oldpset;
4627c478bd9Sstevel@tonic-gate 	int		error = 0;
4637c478bd9Sstevel@tonic-gate 	void		*projbuf, *zonebuf;
4647c478bd9Sstevel@tonic-gate 
4657c478bd9Sstevel@tonic-gate 	pool_lock();
4667c478bd9Sstevel@tonic-gate 	if (pset != PS_QUERY) {
4677c478bd9Sstevel@tonic-gate 		/*
4687c478bd9Sstevel@tonic-gate 		 * Check if the set actually exists before checking
4697c478bd9Sstevel@tonic-gate 		 * permissions.  This is the historical error
4707c478bd9Sstevel@tonic-gate 		 * precedence.  Note that if pset was PS_MYID, the
4717c478bd9Sstevel@tonic-gate 		 * cpupart_get_cpus call will change it to the
4727c478bd9Sstevel@tonic-gate 		 * processor set id of the caller (or PS_NONE if the
4737c478bd9Sstevel@tonic-gate 		 * caller is not bound to a processor set).
4747c478bd9Sstevel@tonic-gate 		 */
4757c478bd9Sstevel@tonic-gate 		if (pool_state == POOL_ENABLED) {
4767c478bd9Sstevel@tonic-gate 			pool_unlock();
4777c478bd9Sstevel@tonic-gate 			return (set_errno(ENOTSUP));
4787c478bd9Sstevel@tonic-gate 		}
4797c478bd9Sstevel@tonic-gate 		if (cpupart_get_cpus(&pset, NULL, NULL) != 0) {
4807c478bd9Sstevel@tonic-gate 			pool_unlock();
4817c478bd9Sstevel@tonic-gate 			return (set_errno(EINVAL));
4827c478bd9Sstevel@tonic-gate 		} else if (pset != PS_NONE && secpolicy_pset(CRED()) != 0) {
4837c478bd9Sstevel@tonic-gate 			pool_unlock();
4847c478bd9Sstevel@tonic-gate 			return (set_errno(EPERM));
4857c478bd9Sstevel@tonic-gate 		}
4867c478bd9Sstevel@tonic-gate 	}
4877c478bd9Sstevel@tonic-gate 
4887c478bd9Sstevel@tonic-gate 	/*
4897c478bd9Sstevel@tonic-gate 	 * Pre-allocate enough buffers for FSS for all active projects
4907c478bd9Sstevel@tonic-gate 	 * and for all active zones on the system.  Unused buffers will
4917c478bd9Sstevel@tonic-gate 	 * be freed later by fss_freebuf().
4927c478bd9Sstevel@tonic-gate 	 */
4937c478bd9Sstevel@tonic-gate 	mutex_enter(&cpu_lock);
4947c478bd9Sstevel@tonic-gate 	projbuf = fss_allocbuf(FSS_NPROJ_BUF, FSS_ALLOC_PROJ);
4957c478bd9Sstevel@tonic-gate 	zonebuf = fss_allocbuf(FSS_NPROJ_BUF, FSS_ALLOC_ZONE);
4967c478bd9Sstevel@tonic-gate 
4977c478bd9Sstevel@tonic-gate 	switch (idtype) {
4987c478bd9Sstevel@tonic-gate 	case P_LWPID:
4997c478bd9Sstevel@tonic-gate 		pp = curproc;
5007c478bd9Sstevel@tonic-gate 		mutex_enter(&pidlock);
5017c478bd9Sstevel@tonic-gate 		mutex_enter(&pp->p_lock);
5027c478bd9Sstevel@tonic-gate 		if (id == P_MYID) {
5037c478bd9Sstevel@tonic-gate 			tp = curthread;
5047c478bd9Sstevel@tonic-gate 		} else {
5057c478bd9Sstevel@tonic-gate 			if ((tp = idtot(pp, id)) == NULL) {
5067c478bd9Sstevel@tonic-gate 				mutex_exit(&pp->p_lock);
5077c478bd9Sstevel@tonic-gate 				mutex_exit(&pidlock);
5087c478bd9Sstevel@tonic-gate 				error = ESRCH;
5097c478bd9Sstevel@tonic-gate 				break;
5107c478bd9Sstevel@tonic-gate 			}
5117c478bd9Sstevel@tonic-gate 		}
5127c478bd9Sstevel@tonic-gate 		error = pset_bind_thread(tp, pset, &oldpset, projbuf, zonebuf);
5137c478bd9Sstevel@tonic-gate 		mutex_exit(&pp->p_lock);
5147c478bd9Sstevel@tonic-gate 		mutex_exit(&pidlock);
5157c478bd9Sstevel@tonic-gate 		break;
5167c478bd9Sstevel@tonic-gate 
5177c478bd9Sstevel@tonic-gate 	case P_PID:
5187c478bd9Sstevel@tonic-gate 		mutex_enter(&pidlock);
5197c478bd9Sstevel@tonic-gate 		if (id == P_MYID) {
5207c478bd9Sstevel@tonic-gate 			pp = curproc;
5217c478bd9Sstevel@tonic-gate 		} else if ((pp = prfind(id)) == NULL) {
5227c478bd9Sstevel@tonic-gate 			mutex_exit(&pidlock);
5237c478bd9Sstevel@tonic-gate 			error = ESRCH;
5247c478bd9Sstevel@tonic-gate 			break;
5257c478bd9Sstevel@tonic-gate 		}
5267c478bd9Sstevel@tonic-gate 		error = pset_bind_process(pp, pset, &oldpset, projbuf, zonebuf);
5277c478bd9Sstevel@tonic-gate 		mutex_exit(&pidlock);
5287c478bd9Sstevel@tonic-gate 		break;
5297c478bd9Sstevel@tonic-gate 
5307c478bd9Sstevel@tonic-gate 	case P_TASKID:
5317c478bd9Sstevel@tonic-gate 		mutex_enter(&pidlock);
5327c478bd9Sstevel@tonic-gate 		if (id == P_MYID)
5337c478bd9Sstevel@tonic-gate 			id = curproc->p_task->tk_tkid;
5347c478bd9Sstevel@tonic-gate 		if ((tk = task_hold_by_id(id)) == NULL) {
5357c478bd9Sstevel@tonic-gate 			mutex_exit(&pidlock);
5367c478bd9Sstevel@tonic-gate 			error = ESRCH;
5377c478bd9Sstevel@tonic-gate 			break;
5387c478bd9Sstevel@tonic-gate 		}
5397c478bd9Sstevel@tonic-gate 		error = pset_bind_task(tk, pset, &oldpset, projbuf, zonebuf);
5407c478bd9Sstevel@tonic-gate 		mutex_exit(&pidlock);
5417c478bd9Sstevel@tonic-gate 		task_rele(tk);
5427c478bd9Sstevel@tonic-gate 		break;
5437c478bd9Sstevel@tonic-gate 
5447c478bd9Sstevel@tonic-gate 	case P_PROJID:
5457c478bd9Sstevel@tonic-gate 		if (id == P_MYID)
5467c478bd9Sstevel@tonic-gate 			id = curprojid();
5477c478bd9Sstevel@tonic-gate 		if ((kpj = project_hold_by_id(id, getzoneid(),
5487c478bd9Sstevel@tonic-gate 		    PROJECT_HOLD_FIND)) == NULL) {
5497c478bd9Sstevel@tonic-gate 			error = ESRCH;
5507c478bd9Sstevel@tonic-gate 			break;
5517c478bd9Sstevel@tonic-gate 		}
5527c478bd9Sstevel@tonic-gate 		mutex_enter(&pidlock);
5537c478bd9Sstevel@tonic-gate 		error = pset_bind_project(kpj, pset, &oldpset, projbuf,
5547c478bd9Sstevel@tonic-gate 		    zonebuf);
5557c478bd9Sstevel@tonic-gate 		mutex_exit(&pidlock);
5567c478bd9Sstevel@tonic-gate 		project_rele(kpj);
5577c478bd9Sstevel@tonic-gate 		break;
5587c478bd9Sstevel@tonic-gate 
5597c478bd9Sstevel@tonic-gate 	case P_ZONEID:
5607c478bd9Sstevel@tonic-gate 		if (id == P_MYID)
5617c478bd9Sstevel@tonic-gate 			id = getzoneid();
5627c478bd9Sstevel@tonic-gate 		if ((zptr = zone_find_by_id(id)) == NULL) {
5637c478bd9Sstevel@tonic-gate 			error = ESRCH;
5647c478bd9Sstevel@tonic-gate 			break;
5657c478bd9Sstevel@tonic-gate 		}
5667c478bd9Sstevel@tonic-gate 		mutex_enter(&pidlock);
5677c478bd9Sstevel@tonic-gate 		error = pset_bind_zone(zptr, pset, &oldpset, projbuf, zonebuf);
5687c478bd9Sstevel@tonic-gate 		mutex_exit(&pidlock);
5697c478bd9Sstevel@tonic-gate 		zone_rele(zptr);
5707c478bd9Sstevel@tonic-gate 		break;
5717c478bd9Sstevel@tonic-gate 
5727c478bd9Sstevel@tonic-gate 	case P_CTID:
5737c478bd9Sstevel@tonic-gate 		if (id == P_MYID)
5747c478bd9Sstevel@tonic-gate 			id = PRCTID(curproc);
5757c478bd9Sstevel@tonic-gate 		if ((ct = contract_type_ptr(process_type, id,
5767c478bd9Sstevel@tonic-gate 		    curproc->p_zone->zone_uniqid)) == NULL) {
5777c478bd9Sstevel@tonic-gate 			error = ESRCH;
5787c478bd9Sstevel@tonic-gate 			break;
5797c478bd9Sstevel@tonic-gate 		}
5807c478bd9Sstevel@tonic-gate 		mutex_enter(&pidlock);
5817c478bd9Sstevel@tonic-gate 		error = pset_bind_contract(ct->ct_data, pset, &oldpset, projbuf,
5827c478bd9Sstevel@tonic-gate 		    zonebuf);
5837c478bd9Sstevel@tonic-gate 		mutex_exit(&pidlock);
5847c478bd9Sstevel@tonic-gate 		contract_rele(ct);
5857c478bd9Sstevel@tonic-gate 		break;
5867c478bd9Sstevel@tonic-gate 
5877c478bd9Sstevel@tonic-gate 	case P_PSETID:
5887c478bd9Sstevel@tonic-gate 		if (id == P_MYID || pset != PS_NONE || !INGLOBALZONE(curproc)) {
5897c478bd9Sstevel@tonic-gate 			error = EINVAL;
5907c478bd9Sstevel@tonic-gate 			break;
5917c478bd9Sstevel@tonic-gate 		}
5927c478bd9Sstevel@tonic-gate 		error = pset_unbind(id, projbuf, zonebuf, idtype);
5937c478bd9Sstevel@tonic-gate 		break;
5947c478bd9Sstevel@tonic-gate 
5957c478bd9Sstevel@tonic-gate 	case P_ALL:
5967c478bd9Sstevel@tonic-gate 		if (id == P_MYID || pset != PS_NONE || !INGLOBALZONE(curproc)) {
5977c478bd9Sstevel@tonic-gate 			error = EINVAL;
5987c478bd9Sstevel@tonic-gate 			break;
5997c478bd9Sstevel@tonic-gate 		}
6007c478bd9Sstevel@tonic-gate 		error = pset_unbind(PS_NONE, projbuf, zonebuf, idtype);
6017c478bd9Sstevel@tonic-gate 		break;
6027c478bd9Sstevel@tonic-gate 
6037c478bd9Sstevel@tonic-gate 	default:
6047c478bd9Sstevel@tonic-gate 		error = EINVAL;
6057c478bd9Sstevel@tonic-gate 		break;
6067c478bd9Sstevel@tonic-gate 	}
6077c478bd9Sstevel@tonic-gate 
6087c478bd9Sstevel@tonic-gate 	fss_freebuf(projbuf, FSS_ALLOC_PROJ);
6097c478bd9Sstevel@tonic-gate 	fss_freebuf(zonebuf, FSS_ALLOC_ZONE);
6107c478bd9Sstevel@tonic-gate 	mutex_exit(&cpu_lock);
6117c478bd9Sstevel@tonic-gate 	pool_unlock();
6127c478bd9Sstevel@tonic-gate 
6137c478bd9Sstevel@tonic-gate 	if (error != 0)
6147c478bd9Sstevel@tonic-gate 		return (set_errno(error));
6157c478bd9Sstevel@tonic-gate 	if (opset != NULL) {
6167c478bd9Sstevel@tonic-gate 		if (copyout(&oldpset, opset, sizeof (psetid_t)) != 0)
6177c478bd9Sstevel@tonic-gate 			return (set_errno(EFAULT));
6187c478bd9Sstevel@tonic-gate 	}
6197c478bd9Sstevel@tonic-gate 	return (0);
6207c478bd9Sstevel@tonic-gate }
6217c478bd9Sstevel@tonic-gate 
6227c478bd9Sstevel@tonic-gate /*
6237c478bd9Sstevel@tonic-gate  * Report load average statistics for the specified processor set.
6247c478bd9Sstevel@tonic-gate  */
6257c478bd9Sstevel@tonic-gate static int
6267c478bd9Sstevel@tonic-gate pset_getloadavg(psetid_t pset, int *buf, int nelem)
6277c478bd9Sstevel@tonic-gate {
628*d5fbc238Sfr157268 	int loadbuf[LOADAVG_NSTATS];
6297c478bd9Sstevel@tonic-gate 	int error = 0;
6307c478bd9Sstevel@tonic-gate 
6317c478bd9Sstevel@tonic-gate 	if (nelem < 0)
6327c478bd9Sstevel@tonic-gate 		return (set_errno(EINVAL));
6337c478bd9Sstevel@tonic-gate 
6347c478bd9Sstevel@tonic-gate 	/*
6357c478bd9Sstevel@tonic-gate 	 * We keep the same number of load average statistics for processor
6367c478bd9Sstevel@tonic-gate 	 * sets as we do for the system as a whole.
6377c478bd9Sstevel@tonic-gate 	 */
6387c478bd9Sstevel@tonic-gate 	if (nelem > LOADAVG_NSTATS)
6397c478bd9Sstevel@tonic-gate 		nelem = LOADAVG_NSTATS;
6407c478bd9Sstevel@tonic-gate 
6417c478bd9Sstevel@tonic-gate 	mutex_enter(&cpu_lock);
6427c478bd9Sstevel@tonic-gate 	error = cpupart_get_loadavg(pset, loadbuf, nelem);
6437c478bd9Sstevel@tonic-gate 	mutex_exit(&cpu_lock);
6447c478bd9Sstevel@tonic-gate 	if (!error && nelem && copyout(loadbuf, buf, nelem * sizeof (int)) != 0)
6457c478bd9Sstevel@tonic-gate 		error = EFAULT;
6467c478bd9Sstevel@tonic-gate 
6477c478bd9Sstevel@tonic-gate 	if (error)
6487c478bd9Sstevel@tonic-gate 		return (set_errno(error));
6497c478bd9Sstevel@tonic-gate 	else
6507c478bd9Sstevel@tonic-gate 		return (0);
6517c478bd9Sstevel@tonic-gate }
6527c478bd9Sstevel@tonic-gate 
6537c478bd9Sstevel@tonic-gate 
6547c478bd9Sstevel@tonic-gate /*
6557c478bd9Sstevel@tonic-gate  * Return list of active processor sets, up to a maximum indicated by
6567c478bd9Sstevel@tonic-gate  * numpsets.  The total number of processor sets is stored in the
6577c478bd9Sstevel@tonic-gate  * location pointed to by numpsets.
6587c478bd9Sstevel@tonic-gate  */
6597c478bd9Sstevel@tonic-gate static int
6607c478bd9Sstevel@tonic-gate pset_list(psetid_t *psetlist, uint_t *numpsets)
6617c478bd9Sstevel@tonic-gate {
6627c478bd9Sstevel@tonic-gate 	uint_t user_npsets = 0;
6637c478bd9Sstevel@tonic-gate 	uint_t real_npsets;
6647c478bd9Sstevel@tonic-gate 	psetid_t *psets = NULL;
6657c478bd9Sstevel@tonic-gate 	int error = 0;
6667c478bd9Sstevel@tonic-gate 
6677c478bd9Sstevel@tonic-gate 	if (numpsets != NULL) {
6687c478bd9Sstevel@tonic-gate 		if (copyin(numpsets, &user_npsets, sizeof (uint_t)) != 0)
6697c478bd9Sstevel@tonic-gate 			return (set_errno(EFAULT));
6707c478bd9Sstevel@tonic-gate 	}
6717c478bd9Sstevel@tonic-gate 
6727c478bd9Sstevel@tonic-gate 	/*
6737c478bd9Sstevel@tonic-gate 	 * Get the list of all processor sets.  First we need to find
6747c478bd9Sstevel@tonic-gate 	 * out how many there are, so we can allocate a large enough
6757c478bd9Sstevel@tonic-gate 	 * buffer.
6767c478bd9Sstevel@tonic-gate 	 */
6777c478bd9Sstevel@tonic-gate 	mutex_enter(&cpu_lock);
6787c478bd9Sstevel@tonic-gate 	if (!INGLOBALZONE(curproc) && pool_pset_enabled()) {
6797c478bd9Sstevel@tonic-gate 		psetid_t psetid = zone_pset_get(curproc->p_zone);
6807c478bd9Sstevel@tonic-gate 
6817c478bd9Sstevel@tonic-gate 		if (psetid == PS_NONE) {
6827c478bd9Sstevel@tonic-gate 			real_npsets = 0;
6837c478bd9Sstevel@tonic-gate 		} else {
6847c478bd9Sstevel@tonic-gate 			real_npsets = 1;
6857c478bd9Sstevel@tonic-gate 			psets = kmem_alloc(real_npsets * sizeof (psetid_t),
6867c478bd9Sstevel@tonic-gate 			    KM_SLEEP);
6877c478bd9Sstevel@tonic-gate 			psets[0] = psetid;
6887c478bd9Sstevel@tonic-gate 		}
6897c478bd9Sstevel@tonic-gate 	} else {
6907c478bd9Sstevel@tonic-gate 		real_npsets = cpupart_list(0, NULL, CP_ALL);
6917c478bd9Sstevel@tonic-gate 		if (real_npsets) {
6927c478bd9Sstevel@tonic-gate 			psets = kmem_alloc(real_npsets * sizeof (psetid_t),
6937c478bd9Sstevel@tonic-gate 			    KM_SLEEP);
6947c478bd9Sstevel@tonic-gate 			(void) cpupart_list(psets, real_npsets, CP_ALL);
6957c478bd9Sstevel@tonic-gate 		}
6967c478bd9Sstevel@tonic-gate 	}
6977c478bd9Sstevel@tonic-gate 	mutex_exit(&cpu_lock);
6987c478bd9Sstevel@tonic-gate 
6997c478bd9Sstevel@tonic-gate 	if (user_npsets > real_npsets)
7007c478bd9Sstevel@tonic-gate 		user_npsets = real_npsets;
7017c478bd9Sstevel@tonic-gate 
7027c478bd9Sstevel@tonic-gate 	if (numpsets != NULL) {
7037c478bd9Sstevel@tonic-gate 		if (copyout(&real_npsets, numpsets, sizeof (uint_t)) != 0)
7047c478bd9Sstevel@tonic-gate 			error = EFAULT;
7057c478bd9Sstevel@tonic-gate 		else if (psetlist != NULL && user_npsets != 0) {
7067c478bd9Sstevel@tonic-gate 			if (copyout(psets, psetlist,
7077c478bd9Sstevel@tonic-gate 			    user_npsets * sizeof (psetid_t)) != 0)
7087c478bd9Sstevel@tonic-gate 				error = EFAULT;
7097c478bd9Sstevel@tonic-gate 		}
7107c478bd9Sstevel@tonic-gate 	}
7117c478bd9Sstevel@tonic-gate 
7127c478bd9Sstevel@tonic-gate 	if (real_npsets)
7137c478bd9Sstevel@tonic-gate 		kmem_free(psets, real_npsets * sizeof (psetid_t));
7147c478bd9Sstevel@tonic-gate 
7157c478bd9Sstevel@tonic-gate 	if (error)
7167c478bd9Sstevel@tonic-gate 		return (set_errno(error));
7177c478bd9Sstevel@tonic-gate 	else
7187c478bd9Sstevel@tonic-gate 		return (0);
7197c478bd9Sstevel@tonic-gate }
7207c478bd9Sstevel@tonic-gate 
7217c478bd9Sstevel@tonic-gate static int
7227c478bd9Sstevel@tonic-gate pset_setattr(psetid_t pset, uint_t attr)
7237c478bd9Sstevel@tonic-gate {
7247c478bd9Sstevel@tonic-gate 	int error;
7257c478bd9Sstevel@tonic-gate 
7267c478bd9Sstevel@tonic-gate 	if (secpolicy_pset(CRED()) != 0)
7277c478bd9Sstevel@tonic-gate 		return (set_errno(EPERM));
7287c478bd9Sstevel@tonic-gate 	pool_lock();
7297c478bd9Sstevel@tonic-gate 	if (pool_state == POOL_ENABLED) {
7307c478bd9Sstevel@tonic-gate 		pool_unlock();
7317c478bd9Sstevel@tonic-gate 		return (set_errno(ENOTSUP));
7327c478bd9Sstevel@tonic-gate 	}
7337c478bd9Sstevel@tonic-gate 	if (pset == PS_QUERY || PSET_BADATTR(attr)) {
7347c478bd9Sstevel@tonic-gate 		pool_unlock();
7357c478bd9Sstevel@tonic-gate 		return (set_errno(EINVAL));
7367c478bd9Sstevel@tonic-gate 	}
7377c478bd9Sstevel@tonic-gate 	if ((error = cpupart_setattr(pset, attr)) != 0) {
7387c478bd9Sstevel@tonic-gate 		pool_unlock();
7397c478bd9Sstevel@tonic-gate 		return (set_errno(error));
7407c478bd9Sstevel@tonic-gate 	}
7417c478bd9Sstevel@tonic-gate 	pool_unlock();
7427c478bd9Sstevel@tonic-gate 	return (0);
7437c478bd9Sstevel@tonic-gate }
7447c478bd9Sstevel@tonic-gate 
7457c478bd9Sstevel@tonic-gate static int
7467c478bd9Sstevel@tonic-gate pset_getattr(psetid_t pset, uint_t *attrp)
7477c478bd9Sstevel@tonic-gate {
7487c478bd9Sstevel@tonic-gate 	int error = 0;
7497c478bd9Sstevel@tonic-gate 	uint_t attr;
7507c478bd9Sstevel@tonic-gate 
7517c478bd9Sstevel@tonic-gate 	if (pset == PS_QUERY)
7527c478bd9Sstevel@tonic-gate 		return (set_errno(EINVAL));
7537c478bd9Sstevel@tonic-gate 	if ((error = cpupart_getattr(pset, &attr)) != 0)
7547c478bd9Sstevel@tonic-gate 		return (set_errno(error));
7557c478bd9Sstevel@tonic-gate 	if (copyout(&attr, attrp, sizeof (uint_t)) != 0)
7567c478bd9Sstevel@tonic-gate 		return (set_errno(EFAULT));
7577c478bd9Sstevel@tonic-gate 	return (0);
7587c478bd9Sstevel@tonic-gate }
7597c478bd9Sstevel@tonic-gate 
7607c478bd9Sstevel@tonic-gate static int
7617c478bd9Sstevel@tonic-gate pset(int subcode, long arg1, long arg2, long arg3, long arg4)
7627c478bd9Sstevel@tonic-gate {
7637c478bd9Sstevel@tonic-gate 	switch (subcode) {
7647c478bd9Sstevel@tonic-gate 	case PSET_CREATE:
7657c478bd9Sstevel@tonic-gate 		return (pset_create((psetid_t *)arg1));
7667c478bd9Sstevel@tonic-gate 	case PSET_DESTROY:
7677c478bd9Sstevel@tonic-gate 		return (pset_destroy((psetid_t)arg1));
7687c478bd9Sstevel@tonic-gate 	case PSET_ASSIGN:
7697c478bd9Sstevel@tonic-gate 		return (pset_assign((psetid_t)arg1,
7707c478bd9Sstevel@tonic-gate 		    (processorid_t)arg2, (psetid_t *)arg3, 0));
7717c478bd9Sstevel@tonic-gate 	case PSET_INFO:
7727c478bd9Sstevel@tonic-gate 		return (pset_info((psetid_t)arg1, (int *)arg2,
7737c478bd9Sstevel@tonic-gate 		    (uint_t *)arg3, (processorid_t *)arg4));
7747c478bd9Sstevel@tonic-gate 	case PSET_BIND:
7757c478bd9Sstevel@tonic-gate 		return (pset_bind((psetid_t)arg1, (idtype_t)arg2,
7767c478bd9Sstevel@tonic-gate 		    (id_t)arg3, (psetid_t *)arg4));
7777c478bd9Sstevel@tonic-gate 	case PSET_GETLOADAVG:
7787c478bd9Sstevel@tonic-gate 		return (pset_getloadavg((psetid_t)arg1, (int *)arg2,
7797c478bd9Sstevel@tonic-gate 		    (int)arg3));
7807c478bd9Sstevel@tonic-gate 	case PSET_LIST:
7817c478bd9Sstevel@tonic-gate 		return (pset_list((psetid_t *)arg1, (uint_t *)arg2));
7827c478bd9Sstevel@tonic-gate 	case PSET_SETATTR:
7837c478bd9Sstevel@tonic-gate 		return (pset_setattr((psetid_t)arg1, (uint_t)arg2));
7847c478bd9Sstevel@tonic-gate 	case PSET_GETATTR:
7857c478bd9Sstevel@tonic-gate 		return (pset_getattr((psetid_t)arg1, (uint_t *)arg2));
7867c478bd9Sstevel@tonic-gate 	case PSET_ASSIGN_FORCED:
7877c478bd9Sstevel@tonic-gate 		return (pset_assign((psetid_t)arg1,
7887c478bd9Sstevel@tonic-gate 		    (processorid_t)arg2, (psetid_t *)arg3, 1));
7897c478bd9Sstevel@tonic-gate 	default:
7907c478bd9Sstevel@tonic-gate 		return (set_errno(EINVAL));
7917c478bd9Sstevel@tonic-gate 	}
7927c478bd9Sstevel@tonic-gate }
793