xref: /titanic_53/usr/src/uts/common/syscall/pset.c (revision 0b70c4673a08649be68425e869e9d6357ea777a4)
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
5d5fbc238Sfr157268  * Common Development and Distribution License (the "License").
6d5fbc238Sfr157268  * 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*0b70c467Sakolb  * Copyright 2008 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;
254*0b70c467Sakolb 
255*0b70c467Sakolb 	switch (pset) {
256*0b70c467Sakolb 	case PS_SOFT:
257*0b70c467Sakolb 		TB_PSET_SOFT_SET(tp);
258*0b70c467Sakolb 		break;
259*0b70c467Sakolb 
260*0b70c467Sakolb 	case PS_HARD:
261*0b70c467Sakolb 		TB_PSET_HARD_SET(tp);
262*0b70c467Sakolb 		break;
263*0b70c467Sakolb 
264*0b70c467Sakolb 	case PS_QUERY:
265*0b70c467Sakolb 		break;
266*0b70c467Sakolb 
267*0b70c467Sakolb 	case PS_QUERY_TYPE:
268*0b70c467Sakolb 		*oldpset = TB_PSET_IS_SOFT(tp) ? PS_SOFT : PS_HARD;
269*0b70c467Sakolb 		break;
270*0b70c467Sakolb 
271*0b70c467Sakolb 	default:
2727c478bd9Sstevel@tonic-gate 		/*
2737c478bd9Sstevel@tonic-gate 		 * Must have the same UID as the target process or
2747c478bd9Sstevel@tonic-gate 		 * have PRIV_PROC_OWNER privilege.
2757c478bd9Sstevel@tonic-gate 		 */
2767c478bd9Sstevel@tonic-gate 		if (!hasprocperm(tp->t_cred, CRED()))
2777c478bd9Sstevel@tonic-gate 			return (EPERM);
2787c478bd9Sstevel@tonic-gate 		/*
2797c478bd9Sstevel@tonic-gate 		 * Unbinding of an unbound thread should always succeed.
2807c478bd9Sstevel@tonic-gate 		 */
2817c478bd9Sstevel@tonic-gate 		if (*oldpset == PS_NONE && pset == PS_NONE)
2827c478bd9Sstevel@tonic-gate 			return (0);
2837c478bd9Sstevel@tonic-gate 		/*
2847c478bd9Sstevel@tonic-gate 		 * Only privileged processes can move threads from psets with
2857c478bd9Sstevel@tonic-gate 		 * PSET_NOESCAPE attribute.
2867c478bd9Sstevel@tonic-gate 		 */
2877c478bd9Sstevel@tonic-gate 		if ((tp->t_cpupart->cp_attr & PSET_NOESCAPE) &&
2887c478bd9Sstevel@tonic-gate 		    secpolicy_pset(CRED()) != 0)
2897c478bd9Sstevel@tonic-gate 			return (EPERM);
2907c478bd9Sstevel@tonic-gate 		if ((error = cpupart_bind_thread(tp, pset, 0,
2917c478bd9Sstevel@tonic-gate 		    projbuf, zonebuf)) == 0)
2927c478bd9Sstevel@tonic-gate 			tp->t_bind_pset = pset;
293*0b70c467Sakolb 
294*0b70c467Sakolb 		break;
2957c478bd9Sstevel@tonic-gate 	}
296*0b70c467Sakolb 
2977c478bd9Sstevel@tonic-gate 	return (error);
2987c478bd9Sstevel@tonic-gate }
2997c478bd9Sstevel@tonic-gate 
3007c478bd9Sstevel@tonic-gate static int
3017c478bd9Sstevel@tonic-gate pset_bind_process(proc_t *pp, psetid_t pset, psetid_t *oldpset, void *projbuf,
3027c478bd9Sstevel@tonic-gate     void *zonebuf)
3037c478bd9Sstevel@tonic-gate {
3047c478bd9Sstevel@tonic-gate 	int error = 0;
3057c478bd9Sstevel@tonic-gate 	kthread_t *tp;
3067c478bd9Sstevel@tonic-gate 
3077c478bd9Sstevel@tonic-gate 	/* skip kernel processes */
308*0b70c467Sakolb 	if ((pset != PS_QUERY) && pp->p_flag & SSYS) {
3097c478bd9Sstevel@tonic-gate 		*oldpset = PS_NONE;
3107c478bd9Sstevel@tonic-gate 		return (0);
3117c478bd9Sstevel@tonic-gate 	}
3127c478bd9Sstevel@tonic-gate 
3137c478bd9Sstevel@tonic-gate 	mutex_enter(&pp->p_lock);
3147c478bd9Sstevel@tonic-gate 	tp = pp->p_tlist;
3157c478bd9Sstevel@tonic-gate 	if (tp != NULL) {
3167c478bd9Sstevel@tonic-gate 		do {
3177c478bd9Sstevel@tonic-gate 			int rval;
3187c478bd9Sstevel@tonic-gate 
3197c478bd9Sstevel@tonic-gate 			rval = pset_bind_thread(tp, pset, oldpset, projbuf,
3207c478bd9Sstevel@tonic-gate 			    zonebuf);
3217c478bd9Sstevel@tonic-gate 			if (error == 0)
3227c478bd9Sstevel@tonic-gate 				error = rval;
3237c478bd9Sstevel@tonic-gate 		} while ((tp = tp->t_forw) != pp->p_tlist);
3247c478bd9Sstevel@tonic-gate 	} else
3257c478bd9Sstevel@tonic-gate 		error = ESRCH;
3267c478bd9Sstevel@tonic-gate 	mutex_exit(&pp->p_lock);
3277c478bd9Sstevel@tonic-gate 
3287c478bd9Sstevel@tonic-gate 	return (error);
3297c478bd9Sstevel@tonic-gate }
3307c478bd9Sstevel@tonic-gate 
3317c478bd9Sstevel@tonic-gate static int
3327c478bd9Sstevel@tonic-gate pset_bind_task(task_t *tk, psetid_t pset, psetid_t *oldpset, void *projbuf,
3337c478bd9Sstevel@tonic-gate     void *zonebuf)
3347c478bd9Sstevel@tonic-gate {
3357c478bd9Sstevel@tonic-gate 	int error = 0;
3367c478bd9Sstevel@tonic-gate 	proc_t *pp;
3377c478bd9Sstevel@tonic-gate 
3387c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&pidlock));
3397c478bd9Sstevel@tonic-gate 
3407c478bd9Sstevel@tonic-gate 	if ((pp = tk->tk_memb_list) == NULL) {
3417c478bd9Sstevel@tonic-gate 		return (ESRCH);
3427c478bd9Sstevel@tonic-gate 	}
3437c478bd9Sstevel@tonic-gate 
3447c478bd9Sstevel@tonic-gate 	do {
3457c478bd9Sstevel@tonic-gate 		int rval;
3467c478bd9Sstevel@tonic-gate 
3477c478bd9Sstevel@tonic-gate 		rval = pset_bind_process(pp, pset, oldpset, projbuf, zonebuf);
3487c478bd9Sstevel@tonic-gate 		if (error == 0)
3497c478bd9Sstevel@tonic-gate 			error = rval;
3507c478bd9Sstevel@tonic-gate 	} while ((pp = pp->p_tasknext) != tk->tk_memb_list);
3517c478bd9Sstevel@tonic-gate 
3527c478bd9Sstevel@tonic-gate 	return (error);
3537c478bd9Sstevel@tonic-gate }
3547c478bd9Sstevel@tonic-gate 
3557c478bd9Sstevel@tonic-gate static int
3567c478bd9Sstevel@tonic-gate pset_bind_project(kproject_t *kpj, psetid_t pset, psetid_t *oldpset,
3577c478bd9Sstevel@tonic-gate     void *projbuf, void *zonebuf)
3587c478bd9Sstevel@tonic-gate {
3597c478bd9Sstevel@tonic-gate 	int error = 0;
3607c478bd9Sstevel@tonic-gate 	proc_t *pp;
3617c478bd9Sstevel@tonic-gate 
3627c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&pidlock));
3637c478bd9Sstevel@tonic-gate 
3647c478bd9Sstevel@tonic-gate 	for (pp = practive; pp != NULL; pp = pp->p_next) {
3657c478bd9Sstevel@tonic-gate 		if (pp->p_tlist == NULL)
3667c478bd9Sstevel@tonic-gate 			continue;
3677c478bd9Sstevel@tonic-gate 		if (pp->p_task->tk_proj == kpj) {
3687c478bd9Sstevel@tonic-gate 			int rval;
3697c478bd9Sstevel@tonic-gate 
3707c478bd9Sstevel@tonic-gate 			rval = pset_bind_process(pp, pset, oldpset, projbuf,
3717c478bd9Sstevel@tonic-gate 			    zonebuf);
3727c478bd9Sstevel@tonic-gate 			if (error == 0)
3737c478bd9Sstevel@tonic-gate 				error = rval;
3747c478bd9Sstevel@tonic-gate 		}
3757c478bd9Sstevel@tonic-gate 	}
3767c478bd9Sstevel@tonic-gate 
3777c478bd9Sstevel@tonic-gate 	return (error);
3787c478bd9Sstevel@tonic-gate }
3797c478bd9Sstevel@tonic-gate 
3807c478bd9Sstevel@tonic-gate static int
3817c478bd9Sstevel@tonic-gate pset_bind_zone(zone_t *zptr, psetid_t pset, psetid_t *oldpset, void *projbuf,
3827c478bd9Sstevel@tonic-gate     void *zonebuf)
3837c478bd9Sstevel@tonic-gate {
3847c478bd9Sstevel@tonic-gate 	int error = 0;
3857c478bd9Sstevel@tonic-gate 	proc_t *pp;
3867c478bd9Sstevel@tonic-gate 
3877c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&pidlock));
3887c478bd9Sstevel@tonic-gate 
3897c478bd9Sstevel@tonic-gate 	for (pp = practive; pp != NULL; pp = pp->p_next) {
3907c478bd9Sstevel@tonic-gate 		if (pp->p_zone == zptr) {
3917c478bd9Sstevel@tonic-gate 			int rval;
3927c478bd9Sstevel@tonic-gate 
3937c478bd9Sstevel@tonic-gate 			rval = pset_bind_process(pp, pset, oldpset, projbuf,
3947c478bd9Sstevel@tonic-gate 			    zonebuf);
3957c478bd9Sstevel@tonic-gate 			if (error == 0)
3967c478bd9Sstevel@tonic-gate 				error = rval;
3977c478bd9Sstevel@tonic-gate 		}
3987c478bd9Sstevel@tonic-gate 	}
3997c478bd9Sstevel@tonic-gate 
4007c478bd9Sstevel@tonic-gate 	return (error);
4017c478bd9Sstevel@tonic-gate }
4027c478bd9Sstevel@tonic-gate 
4037c478bd9Sstevel@tonic-gate /*
4047c478bd9Sstevel@tonic-gate  * Unbind all threads from the specified processor set, or from all
4057c478bd9Sstevel@tonic-gate  * processor sets.
4067c478bd9Sstevel@tonic-gate  */
4077c478bd9Sstevel@tonic-gate static int
4087c478bd9Sstevel@tonic-gate pset_unbind(psetid_t pset, void *projbuf, void *zonebuf, idtype_t idtype)
4097c478bd9Sstevel@tonic-gate {
4107c478bd9Sstevel@tonic-gate 	psetid_t olbind;
4117c478bd9Sstevel@tonic-gate 	kthread_t *tp;
4127c478bd9Sstevel@tonic-gate 	int error = 0;
4137c478bd9Sstevel@tonic-gate 	int rval;
4147c478bd9Sstevel@tonic-gate 	proc_t *pp;
4157c478bd9Sstevel@tonic-gate 
4167c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cpu_lock));
4177c478bd9Sstevel@tonic-gate 
4187c478bd9Sstevel@tonic-gate 	if (idtype == P_PSETID && cpupart_find(pset) == NULL)
4197c478bd9Sstevel@tonic-gate 		return (EINVAL);
4207c478bd9Sstevel@tonic-gate 
4217c478bd9Sstevel@tonic-gate 	mutex_enter(&pidlock);
4227c478bd9Sstevel@tonic-gate 	for (pp = practive; pp != NULL; pp = pp->p_next) {
4237c478bd9Sstevel@tonic-gate 		mutex_enter(&pp->p_lock);
4247c478bd9Sstevel@tonic-gate 		tp = pp->p_tlist;
4257c478bd9Sstevel@tonic-gate 		/*
4267c478bd9Sstevel@tonic-gate 		 * Skip zombies and kernel processes, and processes in
4277c478bd9Sstevel@tonic-gate 		 * other zones, if called from a non-global zone.
4287c478bd9Sstevel@tonic-gate 		 */
4297c478bd9Sstevel@tonic-gate 		if (tp == NULL || (pp->p_flag & SSYS) ||
4307c478bd9Sstevel@tonic-gate 		    !HASZONEACCESS(curproc, pp->p_zone->zone_id)) {
4317c478bd9Sstevel@tonic-gate 			mutex_exit(&pp->p_lock);
4327c478bd9Sstevel@tonic-gate 			continue;
4337c478bd9Sstevel@tonic-gate 		}
4347c478bd9Sstevel@tonic-gate 		do {
4357c478bd9Sstevel@tonic-gate 			if ((idtype == P_PSETID && tp->t_bind_pset != pset) ||
4367c478bd9Sstevel@tonic-gate 			    (idtype == P_ALL && tp->t_bind_pset == PS_NONE))
4377c478bd9Sstevel@tonic-gate 				continue;
4387c478bd9Sstevel@tonic-gate 			rval = pset_bind_thread(tp, PS_NONE, &olbind,
4397c478bd9Sstevel@tonic-gate 			    projbuf, zonebuf);
4407c478bd9Sstevel@tonic-gate 			if (error == 0)
4417c478bd9Sstevel@tonic-gate 				error = rval;
4427c478bd9Sstevel@tonic-gate 		} while ((tp = tp->t_forw) != pp->p_tlist);
4437c478bd9Sstevel@tonic-gate 		mutex_exit(&pp->p_lock);
4447c478bd9Sstevel@tonic-gate 	}
4457c478bd9Sstevel@tonic-gate 	mutex_exit(&pidlock);
4467c478bd9Sstevel@tonic-gate 	return (error);
4477c478bd9Sstevel@tonic-gate }
4487c478bd9Sstevel@tonic-gate 
4497c478bd9Sstevel@tonic-gate static int
4507c478bd9Sstevel@tonic-gate pset_bind_contract(cont_process_t *ctp, psetid_t pset, psetid_t *oldpset,
4517c478bd9Sstevel@tonic-gate     void *projbuf, void *zonebuf)
4527c478bd9Sstevel@tonic-gate {
4537c478bd9Sstevel@tonic-gate 	int error = 0;
4547c478bd9Sstevel@tonic-gate 	proc_t *pp;
4557c478bd9Sstevel@tonic-gate 
4567c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&pidlock));
4577c478bd9Sstevel@tonic-gate 
4587c478bd9Sstevel@tonic-gate 	for (pp = practive; pp != NULL; pp = pp->p_next) {
4597c478bd9Sstevel@tonic-gate 		if (pp->p_ct_process == ctp) {
4607c478bd9Sstevel@tonic-gate 			int rval;
4617c478bd9Sstevel@tonic-gate 
4627c478bd9Sstevel@tonic-gate 			rval = pset_bind_process(pp, pset, oldpset, projbuf,
4637c478bd9Sstevel@tonic-gate 			    zonebuf);
4647c478bd9Sstevel@tonic-gate 			if (error == 0)
4657c478bd9Sstevel@tonic-gate 				error = rval;
4667c478bd9Sstevel@tonic-gate 		}
4677c478bd9Sstevel@tonic-gate 	}
4687c478bd9Sstevel@tonic-gate 
4697c478bd9Sstevel@tonic-gate 	return (error);
4707c478bd9Sstevel@tonic-gate }
4717c478bd9Sstevel@tonic-gate 
4727c478bd9Sstevel@tonic-gate static int
4737c478bd9Sstevel@tonic-gate pset_bind(psetid_t pset, idtype_t idtype, id_t id, psetid_t *opset)
4747c478bd9Sstevel@tonic-gate {
4757c478bd9Sstevel@tonic-gate 	kthread_t	*tp;
4767c478bd9Sstevel@tonic-gate 	proc_t		*pp;
4777c478bd9Sstevel@tonic-gate 	task_t		*tk;
4787c478bd9Sstevel@tonic-gate 	kproject_t	*kpj;
4797c478bd9Sstevel@tonic-gate 	contract_t	*ct;
4807c478bd9Sstevel@tonic-gate 	zone_t		*zptr;
4817c478bd9Sstevel@tonic-gate 	psetid_t	oldpset;
4827c478bd9Sstevel@tonic-gate 	int		error = 0;
4837c478bd9Sstevel@tonic-gate 	void		*projbuf, *zonebuf;
4847c478bd9Sstevel@tonic-gate 
4857c478bd9Sstevel@tonic-gate 	pool_lock();
486*0b70c467Sakolb 	if ((pset != PS_QUERY) && (pset != PS_SOFT) &&
487*0b70c467Sakolb 	    (pset != PS_HARD) && (pset != PS_QUERY_TYPE)) {
4887c478bd9Sstevel@tonic-gate 		/*
4897c478bd9Sstevel@tonic-gate 		 * Check if the set actually exists before checking
4907c478bd9Sstevel@tonic-gate 		 * permissions.  This is the historical error
4917c478bd9Sstevel@tonic-gate 		 * precedence.  Note that if pset was PS_MYID, the
4927c478bd9Sstevel@tonic-gate 		 * cpupart_get_cpus call will change it to the
4937c478bd9Sstevel@tonic-gate 		 * processor set id of the caller (or PS_NONE if the
4947c478bd9Sstevel@tonic-gate 		 * caller is not bound to a processor set).
4957c478bd9Sstevel@tonic-gate 		 */
4967c478bd9Sstevel@tonic-gate 		if (pool_state == POOL_ENABLED) {
4977c478bd9Sstevel@tonic-gate 			pool_unlock();
4987c478bd9Sstevel@tonic-gate 			return (set_errno(ENOTSUP));
4997c478bd9Sstevel@tonic-gate 		}
5007c478bd9Sstevel@tonic-gate 		if (cpupart_get_cpus(&pset, NULL, NULL) != 0) {
5017c478bd9Sstevel@tonic-gate 			pool_unlock();
5027c478bd9Sstevel@tonic-gate 			return (set_errno(EINVAL));
5037c478bd9Sstevel@tonic-gate 		} else if (pset != PS_NONE && secpolicy_pset(CRED()) != 0) {
5047c478bd9Sstevel@tonic-gate 			pool_unlock();
5057c478bd9Sstevel@tonic-gate 			return (set_errno(EPERM));
5067c478bd9Sstevel@tonic-gate 		}
5077c478bd9Sstevel@tonic-gate 	}
5087c478bd9Sstevel@tonic-gate 
5097c478bd9Sstevel@tonic-gate 	/*
5107c478bd9Sstevel@tonic-gate 	 * Pre-allocate enough buffers for FSS for all active projects
5117c478bd9Sstevel@tonic-gate 	 * and for all active zones on the system.  Unused buffers will
5127c478bd9Sstevel@tonic-gate 	 * be freed later by fss_freebuf().
5137c478bd9Sstevel@tonic-gate 	 */
5147c478bd9Sstevel@tonic-gate 	mutex_enter(&cpu_lock);
5157c478bd9Sstevel@tonic-gate 	projbuf = fss_allocbuf(FSS_NPROJ_BUF, FSS_ALLOC_PROJ);
5167c478bd9Sstevel@tonic-gate 	zonebuf = fss_allocbuf(FSS_NPROJ_BUF, FSS_ALLOC_ZONE);
5177c478bd9Sstevel@tonic-gate 
5187c478bd9Sstevel@tonic-gate 	switch (idtype) {
5197c478bd9Sstevel@tonic-gate 	case P_LWPID:
5207c478bd9Sstevel@tonic-gate 		pp = curproc;
5217c478bd9Sstevel@tonic-gate 		mutex_enter(&pidlock);
5227c478bd9Sstevel@tonic-gate 		mutex_enter(&pp->p_lock);
5237c478bd9Sstevel@tonic-gate 		if (id == P_MYID) {
5247c478bd9Sstevel@tonic-gate 			tp = curthread;
5257c478bd9Sstevel@tonic-gate 		} else {
5267c478bd9Sstevel@tonic-gate 			if ((tp = idtot(pp, id)) == NULL) {
5277c478bd9Sstevel@tonic-gate 				mutex_exit(&pp->p_lock);
5287c478bd9Sstevel@tonic-gate 				mutex_exit(&pidlock);
5297c478bd9Sstevel@tonic-gate 				error = ESRCH;
5307c478bd9Sstevel@tonic-gate 				break;
5317c478bd9Sstevel@tonic-gate 			}
5327c478bd9Sstevel@tonic-gate 		}
5337c478bd9Sstevel@tonic-gate 		error = pset_bind_thread(tp, pset, &oldpset, projbuf, zonebuf);
5347c478bd9Sstevel@tonic-gate 		mutex_exit(&pp->p_lock);
5357c478bd9Sstevel@tonic-gate 		mutex_exit(&pidlock);
5367c478bd9Sstevel@tonic-gate 		break;
5377c478bd9Sstevel@tonic-gate 
5387c478bd9Sstevel@tonic-gate 	case P_PID:
5397c478bd9Sstevel@tonic-gate 		mutex_enter(&pidlock);
5407c478bd9Sstevel@tonic-gate 		if (id == P_MYID) {
5417c478bd9Sstevel@tonic-gate 			pp = curproc;
5427c478bd9Sstevel@tonic-gate 		} else if ((pp = prfind(id)) == NULL) {
5437c478bd9Sstevel@tonic-gate 			mutex_exit(&pidlock);
5447c478bd9Sstevel@tonic-gate 			error = ESRCH;
5457c478bd9Sstevel@tonic-gate 			break;
5467c478bd9Sstevel@tonic-gate 		}
5477c478bd9Sstevel@tonic-gate 		error = pset_bind_process(pp, pset, &oldpset, projbuf, zonebuf);
5487c478bd9Sstevel@tonic-gate 		mutex_exit(&pidlock);
5497c478bd9Sstevel@tonic-gate 		break;
5507c478bd9Sstevel@tonic-gate 
5517c478bd9Sstevel@tonic-gate 	case P_TASKID:
5527c478bd9Sstevel@tonic-gate 		mutex_enter(&pidlock);
5537c478bd9Sstevel@tonic-gate 		if (id == P_MYID)
5547c478bd9Sstevel@tonic-gate 			id = curproc->p_task->tk_tkid;
5557c478bd9Sstevel@tonic-gate 		if ((tk = task_hold_by_id(id)) == NULL) {
5567c478bd9Sstevel@tonic-gate 			mutex_exit(&pidlock);
5577c478bd9Sstevel@tonic-gate 			error = ESRCH;
5587c478bd9Sstevel@tonic-gate 			break;
5597c478bd9Sstevel@tonic-gate 		}
5607c478bd9Sstevel@tonic-gate 		error = pset_bind_task(tk, pset, &oldpset, projbuf, zonebuf);
5617c478bd9Sstevel@tonic-gate 		mutex_exit(&pidlock);
5627c478bd9Sstevel@tonic-gate 		task_rele(tk);
5637c478bd9Sstevel@tonic-gate 		break;
5647c478bd9Sstevel@tonic-gate 
5657c478bd9Sstevel@tonic-gate 	case P_PROJID:
5660209230bSgjelinek 		pp = curproc;
5677c478bd9Sstevel@tonic-gate 		if (id == P_MYID)
5687c478bd9Sstevel@tonic-gate 			id = curprojid();
5690209230bSgjelinek 		if ((kpj = project_hold_by_id(id, pp->p_zone,
5707c478bd9Sstevel@tonic-gate 		    PROJECT_HOLD_FIND)) == NULL) {
5717c478bd9Sstevel@tonic-gate 			error = ESRCH;
5727c478bd9Sstevel@tonic-gate 			break;
5737c478bd9Sstevel@tonic-gate 		}
5747c478bd9Sstevel@tonic-gate 		mutex_enter(&pidlock);
5757c478bd9Sstevel@tonic-gate 		error = pset_bind_project(kpj, pset, &oldpset, projbuf,
5767c478bd9Sstevel@tonic-gate 		    zonebuf);
5777c478bd9Sstevel@tonic-gate 		mutex_exit(&pidlock);
5787c478bd9Sstevel@tonic-gate 		project_rele(kpj);
5797c478bd9Sstevel@tonic-gate 		break;
5807c478bd9Sstevel@tonic-gate 
5817c478bd9Sstevel@tonic-gate 	case P_ZONEID:
5827c478bd9Sstevel@tonic-gate 		if (id == P_MYID)
5837c478bd9Sstevel@tonic-gate 			id = getzoneid();
5847c478bd9Sstevel@tonic-gate 		if ((zptr = zone_find_by_id(id)) == NULL) {
5857c478bd9Sstevel@tonic-gate 			error = ESRCH;
5867c478bd9Sstevel@tonic-gate 			break;
5877c478bd9Sstevel@tonic-gate 		}
5887c478bd9Sstevel@tonic-gate 		mutex_enter(&pidlock);
5897c478bd9Sstevel@tonic-gate 		error = pset_bind_zone(zptr, pset, &oldpset, projbuf, zonebuf);
5907c478bd9Sstevel@tonic-gate 		mutex_exit(&pidlock);
5917c478bd9Sstevel@tonic-gate 		zone_rele(zptr);
5927c478bd9Sstevel@tonic-gate 		break;
5937c478bd9Sstevel@tonic-gate 
5947c478bd9Sstevel@tonic-gate 	case P_CTID:
5957c478bd9Sstevel@tonic-gate 		if (id == P_MYID)
5967c478bd9Sstevel@tonic-gate 			id = PRCTID(curproc);
5977c478bd9Sstevel@tonic-gate 		if ((ct = contract_type_ptr(process_type, id,
5987c478bd9Sstevel@tonic-gate 		    curproc->p_zone->zone_uniqid)) == NULL) {
5997c478bd9Sstevel@tonic-gate 			error = ESRCH;
6007c478bd9Sstevel@tonic-gate 			break;
6017c478bd9Sstevel@tonic-gate 		}
6027c478bd9Sstevel@tonic-gate 		mutex_enter(&pidlock);
6037c478bd9Sstevel@tonic-gate 		error = pset_bind_contract(ct->ct_data, pset, &oldpset, projbuf,
6047c478bd9Sstevel@tonic-gate 		    zonebuf);
6057c478bd9Sstevel@tonic-gate 		mutex_exit(&pidlock);
6067c478bd9Sstevel@tonic-gate 		contract_rele(ct);
6077c478bd9Sstevel@tonic-gate 		break;
6087c478bd9Sstevel@tonic-gate 
6097c478bd9Sstevel@tonic-gate 	case P_PSETID:
6107c478bd9Sstevel@tonic-gate 		if (id == P_MYID || pset != PS_NONE || !INGLOBALZONE(curproc)) {
6117c478bd9Sstevel@tonic-gate 			error = EINVAL;
6127c478bd9Sstevel@tonic-gate 			break;
6137c478bd9Sstevel@tonic-gate 		}
6147c478bd9Sstevel@tonic-gate 		error = pset_unbind(id, projbuf, zonebuf, idtype);
6157c478bd9Sstevel@tonic-gate 		break;
6167c478bd9Sstevel@tonic-gate 
6177c478bd9Sstevel@tonic-gate 	case P_ALL:
6187c478bd9Sstevel@tonic-gate 		if (id == P_MYID || pset != PS_NONE || !INGLOBALZONE(curproc)) {
6197c478bd9Sstevel@tonic-gate 			error = EINVAL;
6207c478bd9Sstevel@tonic-gate 			break;
6217c478bd9Sstevel@tonic-gate 		}
6227c478bd9Sstevel@tonic-gate 		error = pset_unbind(PS_NONE, projbuf, zonebuf, idtype);
6237c478bd9Sstevel@tonic-gate 		break;
6247c478bd9Sstevel@tonic-gate 
6257c478bd9Sstevel@tonic-gate 	default:
6267c478bd9Sstevel@tonic-gate 		error = EINVAL;
6277c478bd9Sstevel@tonic-gate 		break;
6287c478bd9Sstevel@tonic-gate 	}
6297c478bd9Sstevel@tonic-gate 
6307c478bd9Sstevel@tonic-gate 	fss_freebuf(projbuf, FSS_ALLOC_PROJ);
6317c478bd9Sstevel@tonic-gate 	fss_freebuf(zonebuf, FSS_ALLOC_ZONE);
6327c478bd9Sstevel@tonic-gate 	mutex_exit(&cpu_lock);
6337c478bd9Sstevel@tonic-gate 	pool_unlock();
6347c478bd9Sstevel@tonic-gate 
6357c478bd9Sstevel@tonic-gate 	if (error != 0)
6367c478bd9Sstevel@tonic-gate 		return (set_errno(error));
6377c478bd9Sstevel@tonic-gate 	if (opset != NULL) {
6387c478bd9Sstevel@tonic-gate 		if (copyout(&oldpset, opset, sizeof (psetid_t)) != 0)
6397c478bd9Sstevel@tonic-gate 			return (set_errno(EFAULT));
6407c478bd9Sstevel@tonic-gate 	}
6417c478bd9Sstevel@tonic-gate 	return (0);
6427c478bd9Sstevel@tonic-gate }
6437c478bd9Sstevel@tonic-gate 
6447c478bd9Sstevel@tonic-gate /*
6457c478bd9Sstevel@tonic-gate  * Report load average statistics for the specified processor set.
6467c478bd9Sstevel@tonic-gate  */
6477c478bd9Sstevel@tonic-gate static int
6487c478bd9Sstevel@tonic-gate pset_getloadavg(psetid_t pset, int *buf, int nelem)
6497c478bd9Sstevel@tonic-gate {
650d5fbc238Sfr157268 	int loadbuf[LOADAVG_NSTATS];
6517c478bd9Sstevel@tonic-gate 	int error = 0;
6527c478bd9Sstevel@tonic-gate 
6537c478bd9Sstevel@tonic-gate 	if (nelem < 0)
6547c478bd9Sstevel@tonic-gate 		return (set_errno(EINVAL));
6557c478bd9Sstevel@tonic-gate 
6567c478bd9Sstevel@tonic-gate 	/*
6577c478bd9Sstevel@tonic-gate 	 * We keep the same number of load average statistics for processor
6587c478bd9Sstevel@tonic-gate 	 * sets as we do for the system as a whole.
6597c478bd9Sstevel@tonic-gate 	 */
6607c478bd9Sstevel@tonic-gate 	if (nelem > LOADAVG_NSTATS)
6617c478bd9Sstevel@tonic-gate 		nelem = LOADAVG_NSTATS;
6627c478bd9Sstevel@tonic-gate 
6637c478bd9Sstevel@tonic-gate 	mutex_enter(&cpu_lock);
6647c478bd9Sstevel@tonic-gate 	error = cpupart_get_loadavg(pset, loadbuf, nelem);
6657c478bd9Sstevel@tonic-gate 	mutex_exit(&cpu_lock);
6667c478bd9Sstevel@tonic-gate 	if (!error && nelem && copyout(loadbuf, buf, nelem * sizeof (int)) != 0)
6677c478bd9Sstevel@tonic-gate 		error = EFAULT;
6687c478bd9Sstevel@tonic-gate 
6697c478bd9Sstevel@tonic-gate 	if (error)
6707c478bd9Sstevel@tonic-gate 		return (set_errno(error));
6717c478bd9Sstevel@tonic-gate 	else
6727c478bd9Sstevel@tonic-gate 		return (0);
6737c478bd9Sstevel@tonic-gate }
6747c478bd9Sstevel@tonic-gate 
6757c478bd9Sstevel@tonic-gate 
6767c478bd9Sstevel@tonic-gate /*
6777c478bd9Sstevel@tonic-gate  * Return list of active processor sets, up to a maximum indicated by
6787c478bd9Sstevel@tonic-gate  * numpsets.  The total number of processor sets is stored in the
6797c478bd9Sstevel@tonic-gate  * location pointed to by numpsets.
6807c478bd9Sstevel@tonic-gate  */
6817c478bd9Sstevel@tonic-gate static int
6827c478bd9Sstevel@tonic-gate pset_list(psetid_t *psetlist, uint_t *numpsets)
6837c478bd9Sstevel@tonic-gate {
6847c478bd9Sstevel@tonic-gate 	uint_t user_npsets = 0;
6857c478bd9Sstevel@tonic-gate 	uint_t real_npsets;
6867c478bd9Sstevel@tonic-gate 	psetid_t *psets = NULL;
6877c478bd9Sstevel@tonic-gate 	int error = 0;
6887c478bd9Sstevel@tonic-gate 
6897c478bd9Sstevel@tonic-gate 	if (numpsets != NULL) {
6907c478bd9Sstevel@tonic-gate 		if (copyin(numpsets, &user_npsets, sizeof (uint_t)) != 0)
6917c478bd9Sstevel@tonic-gate 			return (set_errno(EFAULT));
6927c478bd9Sstevel@tonic-gate 	}
6937c478bd9Sstevel@tonic-gate 
6947c478bd9Sstevel@tonic-gate 	/*
6957c478bd9Sstevel@tonic-gate 	 * Get the list of all processor sets.  First we need to find
6967c478bd9Sstevel@tonic-gate 	 * out how many there are, so we can allocate a large enough
6977c478bd9Sstevel@tonic-gate 	 * buffer.
6987c478bd9Sstevel@tonic-gate 	 */
6997c478bd9Sstevel@tonic-gate 	mutex_enter(&cpu_lock);
7007c478bd9Sstevel@tonic-gate 	if (!INGLOBALZONE(curproc) && pool_pset_enabled()) {
7017c478bd9Sstevel@tonic-gate 		psetid_t psetid = zone_pset_get(curproc->p_zone);
7027c478bd9Sstevel@tonic-gate 
7037c478bd9Sstevel@tonic-gate 		if (psetid == PS_NONE) {
7047c478bd9Sstevel@tonic-gate 			real_npsets = 0;
7057c478bd9Sstevel@tonic-gate 		} else {
7067c478bd9Sstevel@tonic-gate 			real_npsets = 1;
7077c478bd9Sstevel@tonic-gate 			psets = kmem_alloc(real_npsets * sizeof (psetid_t),
7087c478bd9Sstevel@tonic-gate 			    KM_SLEEP);
7097c478bd9Sstevel@tonic-gate 			psets[0] = psetid;
7107c478bd9Sstevel@tonic-gate 		}
7117c478bd9Sstevel@tonic-gate 	} else {
7127c478bd9Sstevel@tonic-gate 		real_npsets = cpupart_list(0, NULL, CP_ALL);
7137c478bd9Sstevel@tonic-gate 		if (real_npsets) {
7147c478bd9Sstevel@tonic-gate 			psets = kmem_alloc(real_npsets * sizeof (psetid_t),
7157c478bd9Sstevel@tonic-gate 			    KM_SLEEP);
7167c478bd9Sstevel@tonic-gate 			(void) cpupart_list(psets, real_npsets, CP_ALL);
7177c478bd9Sstevel@tonic-gate 		}
7187c478bd9Sstevel@tonic-gate 	}
7197c478bd9Sstevel@tonic-gate 	mutex_exit(&cpu_lock);
7207c478bd9Sstevel@tonic-gate 
7217c478bd9Sstevel@tonic-gate 	if (user_npsets > real_npsets)
7227c478bd9Sstevel@tonic-gate 		user_npsets = real_npsets;
7237c478bd9Sstevel@tonic-gate 
7247c478bd9Sstevel@tonic-gate 	if (numpsets != NULL) {
7257c478bd9Sstevel@tonic-gate 		if (copyout(&real_npsets, numpsets, sizeof (uint_t)) != 0)
7267c478bd9Sstevel@tonic-gate 			error = EFAULT;
7277c478bd9Sstevel@tonic-gate 		else if (psetlist != NULL && user_npsets != 0) {
7287c478bd9Sstevel@tonic-gate 			if (copyout(psets, psetlist,
7297c478bd9Sstevel@tonic-gate 			    user_npsets * sizeof (psetid_t)) != 0)
7307c478bd9Sstevel@tonic-gate 				error = EFAULT;
7317c478bd9Sstevel@tonic-gate 		}
7327c478bd9Sstevel@tonic-gate 	}
7337c478bd9Sstevel@tonic-gate 
7347c478bd9Sstevel@tonic-gate 	if (real_npsets)
7357c478bd9Sstevel@tonic-gate 		kmem_free(psets, real_npsets * sizeof (psetid_t));
7367c478bd9Sstevel@tonic-gate 
7377c478bd9Sstevel@tonic-gate 	if (error)
7387c478bd9Sstevel@tonic-gate 		return (set_errno(error));
7397c478bd9Sstevel@tonic-gate 	else
7407c478bd9Sstevel@tonic-gate 		return (0);
7417c478bd9Sstevel@tonic-gate }
7427c478bd9Sstevel@tonic-gate 
7437c478bd9Sstevel@tonic-gate static int
7447c478bd9Sstevel@tonic-gate pset_setattr(psetid_t pset, uint_t attr)
7457c478bd9Sstevel@tonic-gate {
7467c478bd9Sstevel@tonic-gate 	int error;
7477c478bd9Sstevel@tonic-gate 
7487c478bd9Sstevel@tonic-gate 	if (secpolicy_pset(CRED()) != 0)
7497c478bd9Sstevel@tonic-gate 		return (set_errno(EPERM));
7507c478bd9Sstevel@tonic-gate 	pool_lock();
7517c478bd9Sstevel@tonic-gate 	if (pool_state == POOL_ENABLED) {
7527c478bd9Sstevel@tonic-gate 		pool_unlock();
7537c478bd9Sstevel@tonic-gate 		return (set_errno(ENOTSUP));
7547c478bd9Sstevel@tonic-gate 	}
7557c478bd9Sstevel@tonic-gate 	if (pset == PS_QUERY || PSET_BADATTR(attr)) {
7567c478bd9Sstevel@tonic-gate 		pool_unlock();
7577c478bd9Sstevel@tonic-gate 		return (set_errno(EINVAL));
7587c478bd9Sstevel@tonic-gate 	}
7597c478bd9Sstevel@tonic-gate 	if ((error = cpupart_setattr(pset, attr)) != 0) {
7607c478bd9Sstevel@tonic-gate 		pool_unlock();
7617c478bd9Sstevel@tonic-gate 		return (set_errno(error));
7627c478bd9Sstevel@tonic-gate 	}
7637c478bd9Sstevel@tonic-gate 	pool_unlock();
7647c478bd9Sstevel@tonic-gate 	return (0);
7657c478bd9Sstevel@tonic-gate }
7667c478bd9Sstevel@tonic-gate 
7677c478bd9Sstevel@tonic-gate static int
7687c478bd9Sstevel@tonic-gate pset_getattr(psetid_t pset, uint_t *attrp)
7697c478bd9Sstevel@tonic-gate {
7707c478bd9Sstevel@tonic-gate 	int error = 0;
7717c478bd9Sstevel@tonic-gate 	uint_t attr;
7727c478bd9Sstevel@tonic-gate 
7737c478bd9Sstevel@tonic-gate 	if (pset == PS_QUERY)
7747c478bd9Sstevel@tonic-gate 		return (set_errno(EINVAL));
7757c478bd9Sstevel@tonic-gate 	if ((error = cpupart_getattr(pset, &attr)) != 0)
7767c478bd9Sstevel@tonic-gate 		return (set_errno(error));
7777c478bd9Sstevel@tonic-gate 	if (copyout(&attr, attrp, sizeof (uint_t)) != 0)
7787c478bd9Sstevel@tonic-gate 		return (set_errno(EFAULT));
7797c478bd9Sstevel@tonic-gate 	return (0);
7807c478bd9Sstevel@tonic-gate }
7817c478bd9Sstevel@tonic-gate 
7827c478bd9Sstevel@tonic-gate static int
7837c478bd9Sstevel@tonic-gate pset(int subcode, long arg1, long arg2, long arg3, long arg4)
7847c478bd9Sstevel@tonic-gate {
7857c478bd9Sstevel@tonic-gate 	switch (subcode) {
7867c478bd9Sstevel@tonic-gate 	case PSET_CREATE:
7877c478bd9Sstevel@tonic-gate 		return (pset_create((psetid_t *)arg1));
7887c478bd9Sstevel@tonic-gate 	case PSET_DESTROY:
7897c478bd9Sstevel@tonic-gate 		return (pset_destroy((psetid_t)arg1));
7907c478bd9Sstevel@tonic-gate 	case PSET_ASSIGN:
7917c478bd9Sstevel@tonic-gate 		return (pset_assign((psetid_t)arg1,
7927c478bd9Sstevel@tonic-gate 		    (processorid_t)arg2, (psetid_t *)arg3, 0));
7937c478bd9Sstevel@tonic-gate 	case PSET_INFO:
7947c478bd9Sstevel@tonic-gate 		return (pset_info((psetid_t)arg1, (int *)arg2,
7957c478bd9Sstevel@tonic-gate 		    (uint_t *)arg3, (processorid_t *)arg4));
7967c478bd9Sstevel@tonic-gate 	case PSET_BIND:
7977c478bd9Sstevel@tonic-gate 		return (pset_bind((psetid_t)arg1, (idtype_t)arg2,
7987c478bd9Sstevel@tonic-gate 		    (id_t)arg3, (psetid_t *)arg4));
7997c478bd9Sstevel@tonic-gate 	case PSET_GETLOADAVG:
8007c478bd9Sstevel@tonic-gate 		return (pset_getloadavg((psetid_t)arg1, (int *)arg2,
8017c478bd9Sstevel@tonic-gate 		    (int)arg3));
8027c478bd9Sstevel@tonic-gate 	case PSET_LIST:
8037c478bd9Sstevel@tonic-gate 		return (pset_list((psetid_t *)arg1, (uint_t *)arg2));
8047c478bd9Sstevel@tonic-gate 	case PSET_SETATTR:
8057c478bd9Sstevel@tonic-gate 		return (pset_setattr((psetid_t)arg1, (uint_t)arg2));
8067c478bd9Sstevel@tonic-gate 	case PSET_GETATTR:
8077c478bd9Sstevel@tonic-gate 		return (pset_getattr((psetid_t)arg1, (uint_t *)arg2));
8087c478bd9Sstevel@tonic-gate 	case PSET_ASSIGN_FORCED:
8097c478bd9Sstevel@tonic-gate 		return (pset_assign((psetid_t)arg1,
8107c478bd9Sstevel@tonic-gate 		    (processorid_t)arg2, (psetid_t *)arg3, 1));
8117c478bd9Sstevel@tonic-gate 	default:
8127c478bd9Sstevel@tonic-gate 		return (set_errno(EINVAL));
8137c478bd9Sstevel@tonic-gate 	}
8147c478bd9Sstevel@tonic-gate }
815