xref: /illumos-gate/usr/src/uts/common/syscall/pset.c (revision 0e9010a0)
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
5d5fbc238Sfr  * Common Development and Distribution License (the "License").
6d5fbc238Sfr  * 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 /*
22bbf58fc5S  * Copyright (c) 1996, 2010, Oracle and/or its affiliates. All rights reserved.
237c478bd9Sstevel@tonic-gate  */
247c478bd9Sstevel@tonic-gate 
257c478bd9Sstevel@tonic-gate #include <sys/types.h>
267c478bd9Sstevel@tonic-gate #include <sys/systm.h>
277c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
287c478bd9Sstevel@tonic-gate #include <sys/cpuvar.h>
297c478bd9Sstevel@tonic-gate #include <sys/thread.h>
307c478bd9Sstevel@tonic-gate #include <sys/disp.h>
317c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
327c478bd9Sstevel@tonic-gate #include <sys/debug.h>
337c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
347c478bd9Sstevel@tonic-gate #include <sys/cpupart.h>
357c478bd9Sstevel@tonic-gate #include <sys/pset.h>
367c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
377c478bd9Sstevel@tonic-gate #include <sys/syscall.h>
387c478bd9Sstevel@tonic-gate #include <sys/task.h>
397c478bd9Sstevel@tonic-gate #include <sys/loadavg.h>
407c478bd9Sstevel@tonic-gate #include <sys/fss.h>
417c478bd9Sstevel@tonic-gate #include <sys/pool.h>
427c478bd9Sstevel@tonic-gate #include <sys/pool_pset.h>
437c478bd9Sstevel@tonic-gate #include <sys/policy.h>
447c478bd9Sstevel@tonic-gate #include <sys/zone.h>
457c478bd9Sstevel@tonic-gate #include <sys/contract/process_impl.h>
467c478bd9Sstevel@tonic-gate 
477c478bd9Sstevel@tonic-gate static int	pset(int, long, long, long, long);
487c478bd9Sstevel@tonic-gate 
497c478bd9Sstevel@tonic-gate static struct sysent pset_sysent = {
507c478bd9Sstevel@tonic-gate 	5,
517c478bd9Sstevel@tonic-gate 	SE_ARGC | SE_NOUNLOAD,
527c478bd9Sstevel@tonic-gate 	(int (*)())pset,
537c478bd9Sstevel@tonic-gate };
547c478bd9Sstevel@tonic-gate 
557c478bd9Sstevel@tonic-gate static struct modlsys modlsys = {
567c478bd9Sstevel@tonic-gate 	&mod_syscallops, "processor sets", &pset_sysent
577c478bd9Sstevel@tonic-gate };
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
607c478bd9Sstevel@tonic-gate static struct modlsys modlsys32 = {
617c478bd9Sstevel@tonic-gate 	&mod_syscallops32, "32-bit pset(2) syscall", &pset_sysent
627c478bd9Sstevel@tonic-gate };
637c478bd9Sstevel@tonic-gate #endif
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate static struct modlinkage modlinkage = {
667c478bd9Sstevel@tonic-gate 	MODREV_1,
677c478bd9Sstevel@tonic-gate 	&modlsys,
687c478bd9Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
697c478bd9Sstevel@tonic-gate 	&modlsys32,
707c478bd9Sstevel@tonic-gate #endif
717c478bd9Sstevel@tonic-gate 	NULL
727c478bd9Sstevel@tonic-gate };
737c478bd9Sstevel@tonic-gate 
747c478bd9Sstevel@tonic-gate #define	PSET_BADATTR(attr)	((~PSET_NOESCAPE) & (attr))
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate int
_init(void)777c478bd9Sstevel@tonic-gate _init(void)
787c478bd9Sstevel@tonic-gate {
797c478bd9Sstevel@tonic-gate 	return (mod_install(&modlinkage));
807c478bd9Sstevel@tonic-gate }
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate int
_info(struct modinfo * modinfop)837c478bd9Sstevel@tonic-gate _info(struct modinfo *modinfop)
847c478bd9Sstevel@tonic-gate {
857c478bd9Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
867c478bd9Sstevel@tonic-gate }
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate static int
pset_create(psetid_t * psetp)897c478bd9Sstevel@tonic-gate pset_create(psetid_t *psetp)
907c478bd9Sstevel@tonic-gate {
917c478bd9Sstevel@tonic-gate 	psetid_t newpset;
927c478bd9Sstevel@tonic-gate 	int error;
937c478bd9Sstevel@tonic-gate 
947c478bd9Sstevel@tonic-gate 	if (secpolicy_pset(CRED()) != 0)
957c478bd9Sstevel@tonic-gate 		return (set_errno(EPERM));
967c478bd9Sstevel@tonic-gate 
977c478bd9Sstevel@tonic-gate 	pool_lock();
987c478bd9Sstevel@tonic-gate 	if (pool_state == POOL_ENABLED) {
997c478bd9Sstevel@tonic-gate 		pool_unlock();
1007c478bd9Sstevel@tonic-gate 		return (set_errno(ENOTSUP));
1017c478bd9Sstevel@tonic-gate 	}
1027c478bd9Sstevel@tonic-gate 	error = cpupart_create(&newpset);
1037c478bd9Sstevel@tonic-gate 	if (error) {
1047c478bd9Sstevel@tonic-gate 		pool_unlock();
1057c478bd9Sstevel@tonic-gate 		return (set_errno(error));
1067c478bd9Sstevel@tonic-gate 	}
1077c478bd9Sstevel@tonic-gate 	if (copyout(&newpset, psetp, sizeof (psetid_t)) != 0) {
1087c478bd9Sstevel@tonic-gate 		(void) cpupart_destroy(newpset);
1097c478bd9Sstevel@tonic-gate 		pool_unlock();
1107c478bd9Sstevel@tonic-gate 		return (set_errno(EFAULT));
1117c478bd9Sstevel@tonic-gate 	}
1127c478bd9Sstevel@tonic-gate 	pool_unlock();
1137c478bd9Sstevel@tonic-gate 	return (error);
1147c478bd9Sstevel@tonic-gate }
1157c478bd9Sstevel@tonic-gate 
1167c478bd9Sstevel@tonic-gate static int
pset_destroy(psetid_t pset)1177c478bd9Sstevel@tonic-gate pset_destroy(psetid_t pset)
1187c478bd9Sstevel@tonic-gate {
1197c478bd9Sstevel@tonic-gate 	int error;
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate 	if (secpolicy_pset(CRED()) != 0)
1227c478bd9Sstevel@tonic-gate 		return (set_errno(EPERM));
1237c478bd9Sstevel@tonic-gate 
1247c478bd9Sstevel@tonic-gate 	pool_lock();
1257c478bd9Sstevel@tonic-gate 	if (pool_state == POOL_ENABLED) {
1267c478bd9Sstevel@tonic-gate 		pool_unlock();
1277c478bd9Sstevel@tonic-gate 		return (set_errno(ENOTSUP));
1287c478bd9Sstevel@tonic-gate 	}
1297c478bd9Sstevel@tonic-gate 	error = cpupart_destroy(pset);
1307c478bd9Sstevel@tonic-gate 	pool_unlock();
1317c478bd9Sstevel@tonic-gate 	if (error)
1327c478bd9Sstevel@tonic-gate 		return (set_errno(error));
1337c478bd9Sstevel@tonic-gate 	else
1347c478bd9Sstevel@tonic-gate 		return (0);
1357c478bd9Sstevel@tonic-gate }
1367c478bd9Sstevel@tonic-gate 
1377c478bd9Sstevel@tonic-gate static int
pset_assign(psetid_t pset,processorid_t cpuid,psetid_t * opset,int forced)1387c478bd9Sstevel@tonic-gate pset_assign(psetid_t pset, processorid_t cpuid, psetid_t *opset, int forced)
1397c478bd9Sstevel@tonic-gate {
1407c478bd9Sstevel@tonic-gate 	psetid_t oldpset;
1417c478bd9Sstevel@tonic-gate 	int	error = 0;
1427c478bd9Sstevel@tonic-gate 	cpu_t	*cp;
1437c478bd9Sstevel@tonic-gate 
1447c478bd9Sstevel@tonic-gate 	if (pset != PS_QUERY && secpolicy_pset(CRED()) != 0)
1457c478bd9Sstevel@tonic-gate 		return (set_errno(EPERM));
1467c478bd9Sstevel@tonic-gate 
1477c478bd9Sstevel@tonic-gate 	pool_lock();
1487c478bd9Sstevel@tonic-gate 	if (pset != PS_QUERY && pool_state == POOL_ENABLED) {
1497c478bd9Sstevel@tonic-gate 		pool_unlock();
1507c478bd9Sstevel@tonic-gate 		return (set_errno(ENOTSUP));
1517c478bd9Sstevel@tonic-gate 	}
1527c478bd9Sstevel@tonic-gate 
1537c478bd9Sstevel@tonic-gate 	mutex_enter(&cpu_lock);
1547c478bd9Sstevel@tonic-gate 	if ((cp = cpu_get(cpuid)) == NULL) {
1557c478bd9Sstevel@tonic-gate 		mutex_exit(&cpu_lock);
1567c478bd9Sstevel@tonic-gate 		pool_unlock();
1577c478bd9Sstevel@tonic-gate 		return (set_errno(EINVAL));
1587c478bd9Sstevel@tonic-gate 	}
1597c478bd9Sstevel@tonic-gate 
1607c478bd9Sstevel@tonic-gate 	oldpset = cpupart_query_cpu(cp);
1617c478bd9Sstevel@tonic-gate 
1627c478bd9Sstevel@tonic-gate 	if (pset != PS_QUERY)
1637c478bd9Sstevel@tonic-gate 		error = cpupart_attach_cpu(pset, cp, forced);
1647c478bd9Sstevel@tonic-gate 	mutex_exit(&cpu_lock);
1657c478bd9Sstevel@tonic-gate 	pool_unlock();
1667c478bd9Sstevel@tonic-gate 
1677c478bd9Sstevel@tonic-gate 	if (error)
1687c478bd9Sstevel@tonic-gate 		return (set_errno(error));
1697c478bd9Sstevel@tonic-gate 
1707c478bd9Sstevel@tonic-gate 	if (opset != NULL)
1717c478bd9Sstevel@tonic-gate 		if (copyout(&oldpset, opset, sizeof (psetid_t)) != 0)
1727c478bd9Sstevel@tonic-gate 			return (set_errno(EFAULT));
1737c478bd9Sstevel@tonic-gate 
1747c478bd9Sstevel@tonic-gate 	return (0);
1757c478bd9Sstevel@tonic-gate }
1767c478bd9Sstevel@tonic-gate 
1777c478bd9Sstevel@tonic-gate static int
pset_info(psetid_t pset,int * typep,uint_t * numcpusp,processorid_t * cpulistp)1787c478bd9Sstevel@tonic-gate pset_info(psetid_t pset, int *typep, uint_t *numcpusp,
1797c478bd9Sstevel@tonic-gate     processorid_t *cpulistp)
1807c478bd9Sstevel@tonic-gate {
1817c478bd9Sstevel@tonic-gate 	int pset_type;
1827c478bd9Sstevel@tonic-gate 	uint_t user_ncpus = 0, real_ncpus, copy_ncpus;
1837c478bd9Sstevel@tonic-gate 	processorid_t *pset_cpus = NULL;
1847c478bd9Sstevel@tonic-gate 	int error = 0;
1857c478bd9Sstevel@tonic-gate 
1867c478bd9Sstevel@tonic-gate 	if (numcpusp != NULL) {
1877c478bd9Sstevel@tonic-gate 		if (copyin(numcpusp, &user_ncpus, sizeof (uint_t)) != 0)
1887c478bd9Sstevel@tonic-gate 			return (set_errno(EFAULT));
1897c478bd9Sstevel@tonic-gate 	}
1907c478bd9Sstevel@tonic-gate 
1917c478bd9Sstevel@tonic-gate 	if (user_ncpus > max_ncpus)	/* sanity check */
1927c478bd9Sstevel@tonic-gate 		user_ncpus = max_ncpus;
1937c478bd9Sstevel@tonic-gate 	if (user_ncpus != 0 && cpulistp != NULL)
1947c478bd9Sstevel@tonic-gate 		pset_cpus = kmem_alloc(sizeof (processorid_t) * user_ncpus,
1957c478bd9Sstevel@tonic-gate 		    KM_SLEEP);
1967c478bd9Sstevel@tonic-gate 
1977c478bd9Sstevel@tonic-gate 	real_ncpus = user_ncpus;
1987c478bd9Sstevel@tonic-gate 	if ((error = cpupart_get_cpus(&pset, pset_cpus, &real_ncpus)) != 0)
1997c478bd9Sstevel@tonic-gate 		goto out;
2007c478bd9Sstevel@tonic-gate 
2017c478bd9Sstevel@tonic-gate 	/*
2027c478bd9Sstevel@tonic-gate 	 * Now copyout the information about this processor set.
2037c478bd9Sstevel@tonic-gate 	 */
2047c478bd9Sstevel@tonic-gate 
2057c478bd9Sstevel@tonic-gate 	/*
2067c478bd9Sstevel@tonic-gate 	 * Get number of cpus to copy back.  If the user didn't pass in
2077c478bd9Sstevel@tonic-gate 	 * a big enough buffer, only copy back as many cpus as fits in
2087c478bd9Sstevel@tonic-gate 	 * the buffer but copy back the real number of cpus.
2097c478bd9Sstevel@tonic-gate 	 */
2107c478bd9Sstevel@tonic-gate 
2117c478bd9Sstevel@tonic-gate 	if (user_ncpus != 0 && cpulistp != NULL) {
2127c478bd9Sstevel@tonic-gate 		copy_ncpus = MIN(real_ncpus, user_ncpus);
2137c478bd9Sstevel@tonic-gate 		if (copyout(pset_cpus, cpulistp,
2147c478bd9Sstevel@tonic-gate 		    sizeof (processorid_t) * copy_ncpus) != 0) {
2157c478bd9Sstevel@tonic-gate 			error = EFAULT;
2167c478bd9Sstevel@tonic-gate 			goto out;
2177c478bd9Sstevel@tonic-gate 		}
2187c478bd9Sstevel@tonic-gate 	}
2197c478bd9Sstevel@tonic-gate 	if (pset_cpus != NULL)
2207c478bd9Sstevel@tonic-gate 		kmem_free(pset_cpus, sizeof (processorid_t) * user_ncpus);
2217c478bd9Sstevel@tonic-gate 	if (typep != NULL) {
2227c478bd9Sstevel@tonic-gate 		if (pset == PS_NONE)
2237c478bd9Sstevel@tonic-gate 			pset_type = PS_NONE;
2247c478bd9Sstevel@tonic-gate 		else
2257c478bd9Sstevel@tonic-gate 			pset_type = PS_PRIVATE;
2267c478bd9Sstevel@tonic-gate 		if (copyout(&pset_type, typep, sizeof (int)) != 0)
2277c478bd9Sstevel@tonic-gate 			return (set_errno(EFAULT));
2287c478bd9Sstevel@tonic-gate 	}
2297c478bd9Sstevel@tonic-gate 	if (numcpusp != NULL)
2307c478bd9Sstevel@tonic-gate 		if (copyout(&real_ncpus, numcpusp, sizeof (uint_t)) != 0)
2317c478bd9Sstevel@tonic-gate 			return (set_errno(EFAULT));
2327c478bd9Sstevel@tonic-gate 	return (0);
2337c478bd9Sstevel@tonic-gate 
2347c478bd9Sstevel@tonic-gate out:
2357c478bd9Sstevel@tonic-gate 	if (pset_cpus != NULL)
2367c478bd9Sstevel@tonic-gate 		kmem_free(pset_cpus, sizeof (processorid_t) * user_ncpus);
2377c478bd9Sstevel@tonic-gate 	return (set_errno(error));
2387c478bd9Sstevel@tonic-gate }
2397c478bd9Sstevel@tonic-gate 
2407c478bd9Sstevel@tonic-gate static int
pset_bind_thread(kthread_t * tp,psetid_t pset,psetid_t * oldpset,void * projbuf,void * zonebuf)2417c478bd9Sstevel@tonic-gate pset_bind_thread(kthread_t *tp, psetid_t pset, psetid_t *oldpset, void *projbuf,
2427c478bd9Sstevel@tonic-gate     void *zonebuf)
2437c478bd9Sstevel@tonic-gate {
2447c478bd9Sstevel@tonic-gate 	int error = 0;
2457c478bd9Sstevel@tonic-gate 
2467c478bd9Sstevel@tonic-gate 	ASSERT(pool_lock_held());
2477c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cpu_lock));
2487c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&ttoproc(tp)->p_lock));
2497c478bd9Sstevel@tonic-gate 
2507c478bd9Sstevel@tonic-gate 	*oldpset = tp->t_bind_pset;
2510b70c467Sakolb 
2520b70c467Sakolb 	switch (pset) {
2530b70c467Sakolb 	case PS_SOFT:
2540b70c467Sakolb 		TB_PSET_SOFT_SET(tp);
2550b70c467Sakolb 		break;
2560b70c467Sakolb 
2570b70c467Sakolb 	case PS_HARD:
2580b70c467Sakolb 		TB_PSET_HARD_SET(tp);
2590b70c467Sakolb 		break;
2600b70c467Sakolb 
2610b70c467Sakolb 	case PS_QUERY:
2620b70c467Sakolb 		break;
2630b70c467Sakolb 
2640b70c467Sakolb 	case PS_QUERY_TYPE:
2650b70c467Sakolb 		*oldpset = TB_PSET_IS_SOFT(tp) ? PS_SOFT : PS_HARD;
2660b70c467Sakolb 		break;
2670b70c467Sakolb 
2680b70c467Sakolb 	default:
2697c478bd9Sstevel@tonic-gate 		/*
2707c478bd9Sstevel@tonic-gate 		 * Must have the same UID as the target process or
2717c478bd9Sstevel@tonic-gate 		 * have PRIV_PROC_OWNER privilege.
2727c478bd9Sstevel@tonic-gate 		 */
2737c478bd9Sstevel@tonic-gate 		if (!hasprocperm(tp->t_cred, CRED()))
2747c478bd9Sstevel@tonic-gate 			return (EPERM);
2757c478bd9Sstevel@tonic-gate 		/*
2767c478bd9Sstevel@tonic-gate 		 * Unbinding of an unbound thread should always succeed.
2777c478bd9Sstevel@tonic-gate 		 */
2787c478bd9Sstevel@tonic-gate 		if (*oldpset == PS_NONE && pset == PS_NONE)
2797c478bd9Sstevel@tonic-gate 			return (0);
2807c478bd9Sstevel@tonic-gate 		/*
2817c478bd9Sstevel@tonic-gate 		 * Only privileged processes can move threads from psets with
2827c478bd9Sstevel@tonic-gate 		 * PSET_NOESCAPE attribute.
2837c478bd9Sstevel@tonic-gate 		 */
2847c478bd9Sstevel@tonic-gate 		if ((tp->t_cpupart->cp_attr & PSET_NOESCAPE) &&
285bbf58fc5S 		    secpolicy_pbind(CRED()) != 0)
2867c478bd9Sstevel@tonic-gate 			return (EPERM);
2877c478bd9Sstevel@tonic-gate 		if ((error = cpupart_bind_thread(tp, pset, 0,
2887c478bd9Sstevel@tonic-gate 		    projbuf, zonebuf)) == 0)
2897c478bd9Sstevel@tonic-gate 			tp->t_bind_pset = pset;
2900b70c467Sakolb 
2910b70c467Sakolb 		break;
2927c478bd9Sstevel@tonic-gate 	}
2930b70c467Sakolb 
2947c478bd9Sstevel@tonic-gate 	return (error);
2957c478bd9Sstevel@tonic-gate }
2967c478bd9Sstevel@tonic-gate 
2977c478bd9Sstevel@tonic-gate static int
pset_bind_process(proc_t * pp,psetid_t pset,psetid_t * oldpset,void * projbuf,void * zonebuf)2987c478bd9Sstevel@tonic-gate pset_bind_process(proc_t *pp, psetid_t pset, psetid_t *oldpset, void *projbuf,
2997c478bd9Sstevel@tonic-gate     void *zonebuf)
3007c478bd9Sstevel@tonic-gate {
3017c478bd9Sstevel@tonic-gate 	int error = 0;
3027c478bd9Sstevel@tonic-gate 	kthread_t *tp;
3037c478bd9Sstevel@tonic-gate 
3047c478bd9Sstevel@tonic-gate 	/* skip kernel processes */
3050b70c467Sakolb 	if ((pset != PS_QUERY) && pp->p_flag & SSYS) {
3067c478bd9Sstevel@tonic-gate 		*oldpset = PS_NONE;
307438076ebSGangadhar Mylapuram 		return (ENOTSUP);
3087c478bd9Sstevel@tonic-gate 	}
3097c478bd9Sstevel@tonic-gate 
3107c478bd9Sstevel@tonic-gate 	mutex_enter(&pp->p_lock);
3117c478bd9Sstevel@tonic-gate 	tp = pp->p_tlist;
3127c478bd9Sstevel@tonic-gate 	if (tp != NULL) {
3137c478bd9Sstevel@tonic-gate 		do {
3147c478bd9Sstevel@tonic-gate 			int rval;
3157c478bd9Sstevel@tonic-gate 
3167c478bd9Sstevel@tonic-gate 			rval = pset_bind_thread(tp, pset, oldpset, projbuf,
3177c478bd9Sstevel@tonic-gate 			    zonebuf);
3187c478bd9Sstevel@tonic-gate 			if (error == 0)
3197c478bd9Sstevel@tonic-gate 				error = rval;
3207c478bd9Sstevel@tonic-gate 		} while ((tp = tp->t_forw) != pp->p_tlist);
3217c478bd9Sstevel@tonic-gate 	} else
3227c478bd9Sstevel@tonic-gate 		error = ESRCH;
3237c478bd9Sstevel@tonic-gate 	mutex_exit(&pp->p_lock);
3247c478bd9Sstevel@tonic-gate 
3257c478bd9Sstevel@tonic-gate 	return (error);
3267c478bd9Sstevel@tonic-gate }
3277c478bd9Sstevel@tonic-gate 
3287c478bd9Sstevel@tonic-gate static int
pset_bind_task(task_t * tk,psetid_t pset,psetid_t * oldpset,void * projbuf,void * zonebuf)3297c478bd9Sstevel@tonic-gate pset_bind_task(task_t *tk, psetid_t pset, psetid_t *oldpset, void *projbuf,
3307c478bd9Sstevel@tonic-gate     void *zonebuf)
3317c478bd9Sstevel@tonic-gate {
3327c478bd9Sstevel@tonic-gate 	int error = 0;
3337c478bd9Sstevel@tonic-gate 	proc_t *pp;
3347c478bd9Sstevel@tonic-gate 
3357c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&pidlock));
3367c478bd9Sstevel@tonic-gate 
3377c478bd9Sstevel@tonic-gate 	if ((pp = tk->tk_memb_list) == NULL) {
3387c478bd9Sstevel@tonic-gate 		return (ESRCH);
3397c478bd9Sstevel@tonic-gate 	}
3407c478bd9Sstevel@tonic-gate 
3417c478bd9Sstevel@tonic-gate 	do {
3427c478bd9Sstevel@tonic-gate 		int rval;
3437c478bd9Sstevel@tonic-gate 
344438076ebSGangadhar Mylapuram 		if (!(pp->p_flag & SSYS)) {
345438076ebSGangadhar Mylapuram 			rval = pset_bind_process(pp, pset, oldpset, projbuf,
346438076ebSGangadhar Mylapuram 			    zonebuf);
347438076ebSGangadhar Mylapuram 			if (error == 0)
348438076ebSGangadhar Mylapuram 				error = rval;
349438076ebSGangadhar Mylapuram 		}
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
pset_bind_project(kproject_t * kpj,psetid_t pset,psetid_t * oldpset,void * projbuf,void * zonebuf)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;
367438076ebSGangadhar Mylapuram 		if (pp->p_task->tk_proj == kpj && !(pp->p_flag & SSYS)) {
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
pset_bind_zone(zone_t * zptr,psetid_t pset,psetid_t * oldpset,void * projbuf,void * zonebuf)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) {
390438076ebSGangadhar Mylapuram 		if (pp->p_zone == zptr && !(pp->p_flag & SSYS)) {
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
pset_unbind(psetid_t pset,void * projbuf,void * zonebuf,idtype_t idtype)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
pset_bind_contract(cont_process_t * ctp,psetid_t pset,psetid_t * oldpset,void * projbuf,void * zonebuf)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 
472fb9b0aa8SSurya Prakki /*
473fb9b0aa8SSurya Prakki  * Bind the lwp:id of process:pid to processor set: pset
474fb9b0aa8SSurya Prakki  */
475fb9b0aa8SSurya Prakki static int
pset_bind_lwp(psetid_t pset,id_t id,pid_t pid,psetid_t * opset)476fb9b0aa8SSurya Prakki pset_bind_lwp(psetid_t pset, id_t id, pid_t pid, psetid_t *opset)
477fb9b0aa8SSurya Prakki {
478fb9b0aa8SSurya Prakki 	kthread_t	*tp;
479fb9b0aa8SSurya Prakki 	proc_t		*pp;
480fb9b0aa8SSurya Prakki 	psetid_t	oldpset;
481fb9b0aa8SSurya Prakki 	void		*projbuf, *zonebuf;
482fb9b0aa8SSurya Prakki 	int		error = 0;
483fb9b0aa8SSurya Prakki 
484fb9b0aa8SSurya Prakki 	pool_lock();
485fb9b0aa8SSurya Prakki 	mutex_enter(&cpu_lock);
486fb9b0aa8SSurya Prakki 	projbuf = fss_allocbuf(FSS_NPROJ_BUF, FSS_ALLOC_PROJ);
487fb9b0aa8SSurya Prakki 	zonebuf = fss_allocbuf(FSS_NPROJ_BUF, FSS_ALLOC_ZONE);
488fb9b0aa8SSurya Prakki 
489fb9b0aa8SSurya Prakki 	mutex_enter(&pidlock);
490fb9b0aa8SSurya Prakki 	if ((pid == P_MYID && id == P_MYID) ||
491fb9b0aa8SSurya Prakki 	    (pid == curproc->p_pid && id == P_MYID)) {
492fb9b0aa8SSurya Prakki 		pp = curproc;
493fb9b0aa8SSurya Prakki 		tp = curthread;
494fb9b0aa8SSurya Prakki 		mutex_enter(&pp->p_lock);
495fb9b0aa8SSurya Prakki 	} else {
496fb9b0aa8SSurya Prakki 		if (pid == P_MYID) {
497fb9b0aa8SSurya Prakki 			pp = curproc;
498fb9b0aa8SSurya Prakki 		} else if ((pp = prfind(pid)) == NULL) {
499fb9b0aa8SSurya Prakki 			error = ESRCH;
500fb9b0aa8SSurya Prakki 			goto err;
501fb9b0aa8SSurya Prakki 		}
502fb9b0aa8SSurya Prakki 		if (pp != curproc && id == P_MYID) {
503fb9b0aa8SSurya Prakki 			error = EINVAL;
504fb9b0aa8SSurya Prakki 			goto err;
505fb9b0aa8SSurya Prakki 		}
506fb9b0aa8SSurya Prakki 		mutex_enter(&pp->p_lock);
507fb9b0aa8SSurya Prakki 		if ((tp = idtot(pp, id)) == NULL) {
508fb9b0aa8SSurya Prakki 			mutex_exit(&pp->p_lock);
509fb9b0aa8SSurya Prakki 			error = ESRCH;
510fb9b0aa8SSurya Prakki 			goto err;
511fb9b0aa8SSurya Prakki 		}
512fb9b0aa8SSurya Prakki 	}
513fb9b0aa8SSurya Prakki 
514fb9b0aa8SSurya Prakki 	error = pset_bind_thread(tp, pset, &oldpset, projbuf, zonebuf);
515fb9b0aa8SSurya Prakki 	mutex_exit(&pp->p_lock);
516fb9b0aa8SSurya Prakki err:
517fb9b0aa8SSurya Prakki 	mutex_exit(&pidlock);
518fb9b0aa8SSurya Prakki 
519fb9b0aa8SSurya Prakki 	fss_freebuf(projbuf, FSS_ALLOC_PROJ);
520fb9b0aa8SSurya Prakki 	fss_freebuf(zonebuf, FSS_ALLOC_ZONE);
521fb9b0aa8SSurya Prakki 	mutex_exit(&cpu_lock);
522fb9b0aa8SSurya Prakki 	pool_unlock();
523fb9b0aa8SSurya Prakki 	if (opset != NULL) {
524fb9b0aa8SSurya Prakki 		if (copyout(&oldpset, opset, sizeof (psetid_t)) != 0)
525fb9b0aa8SSurya Prakki 			return (set_errno(EFAULT));
526fb9b0aa8SSurya Prakki 	}
527fb9b0aa8SSurya Prakki 	if (error != 0)
528fb9b0aa8SSurya Prakki 		return (set_errno(error));
529fb9b0aa8SSurya Prakki 	return (0);
530fb9b0aa8SSurya Prakki }
531fb9b0aa8SSurya Prakki 
5327c478bd9Sstevel@tonic-gate static int
pset_bind(psetid_t pset,idtype_t idtype,id_t id,psetid_t * opset)5337c478bd9Sstevel@tonic-gate pset_bind(psetid_t pset, idtype_t idtype, id_t id, psetid_t *opset)
5347c478bd9Sstevel@tonic-gate {
5357c478bd9Sstevel@tonic-gate 	kthread_t	*tp;
5367c478bd9Sstevel@tonic-gate 	proc_t		*pp;
5377c478bd9Sstevel@tonic-gate 	task_t		*tk;
5387c478bd9Sstevel@tonic-gate 	kproject_t	*kpj;
5397c478bd9Sstevel@tonic-gate 	contract_t	*ct;
5407c478bd9Sstevel@tonic-gate 	zone_t		*zptr;
5417c478bd9Sstevel@tonic-gate 	psetid_t	oldpset;
5427c478bd9Sstevel@tonic-gate 	int		error = 0;
5437c478bd9Sstevel@tonic-gate 	void		*projbuf, *zonebuf;
5447c478bd9Sstevel@tonic-gate 
5457c478bd9Sstevel@tonic-gate 	pool_lock();
5460b70c467Sakolb 	if ((pset != PS_QUERY) && (pset != PS_SOFT) &&
5470b70c467Sakolb 	    (pset != PS_HARD) && (pset != PS_QUERY_TYPE)) {
5487c478bd9Sstevel@tonic-gate 		/*
5497c478bd9Sstevel@tonic-gate 		 * Check if the set actually exists before checking
5507c478bd9Sstevel@tonic-gate 		 * permissions.  This is the historical error
5517c478bd9Sstevel@tonic-gate 		 * precedence.  Note that if pset was PS_MYID, the
5527c478bd9Sstevel@tonic-gate 		 * cpupart_get_cpus call will change it to the
5537c478bd9Sstevel@tonic-gate 		 * processor set id of the caller (or PS_NONE if the
5547c478bd9Sstevel@tonic-gate 		 * caller is not bound to a processor set).
5557c478bd9Sstevel@tonic-gate 		 */
5567c478bd9Sstevel@tonic-gate 		if (pool_state == POOL_ENABLED) {
5577c478bd9Sstevel@tonic-gate 			pool_unlock();
5587c478bd9Sstevel@tonic-gate 			return (set_errno(ENOTSUP));
5597c478bd9Sstevel@tonic-gate 		}
5607c478bd9Sstevel@tonic-gate 		if (cpupart_get_cpus(&pset, NULL, NULL) != 0) {
5617c478bd9Sstevel@tonic-gate 			pool_unlock();
5627c478bd9Sstevel@tonic-gate 			return (set_errno(EINVAL));
563bbf58fc5S 		} else if (pset != PS_NONE && secpolicy_pbind(CRED()) != 0) {
5647c478bd9Sstevel@tonic-gate 			pool_unlock();
5657c478bd9Sstevel@tonic-gate 			return (set_errno(EPERM));
5667c478bd9Sstevel@tonic-gate 		}
5677c478bd9Sstevel@tonic-gate 	}
5687c478bd9Sstevel@tonic-gate 
5697c478bd9Sstevel@tonic-gate 	/*
5707c478bd9Sstevel@tonic-gate 	 * Pre-allocate enough buffers for FSS for all active projects
5717c478bd9Sstevel@tonic-gate 	 * and for all active zones on the system.  Unused buffers will
5727c478bd9Sstevel@tonic-gate 	 * be freed later by fss_freebuf().
5737c478bd9Sstevel@tonic-gate 	 */
5747c478bd9Sstevel@tonic-gate 	mutex_enter(&cpu_lock);
5757c478bd9Sstevel@tonic-gate 	projbuf = fss_allocbuf(FSS_NPROJ_BUF, FSS_ALLOC_PROJ);
5767c478bd9Sstevel@tonic-gate 	zonebuf = fss_allocbuf(FSS_NPROJ_BUF, FSS_ALLOC_ZONE);
5777c478bd9Sstevel@tonic-gate 
5787c478bd9Sstevel@tonic-gate 	switch (idtype) {
5797c478bd9Sstevel@tonic-gate 	case P_LWPID:
5807c478bd9Sstevel@tonic-gate 		pp = curproc;
5817c478bd9Sstevel@tonic-gate 		mutex_enter(&pidlock);
5827c478bd9Sstevel@tonic-gate 		mutex_enter(&pp->p_lock);
5837c478bd9Sstevel@tonic-gate 		if (id == P_MYID) {
5847c478bd9Sstevel@tonic-gate 			tp = curthread;
5857c478bd9Sstevel@tonic-gate 		} else {
5867c478bd9Sstevel@tonic-gate 			if ((tp = idtot(pp, id)) == NULL) {
5877c478bd9Sstevel@tonic-gate 				mutex_exit(&pp->p_lock);
5887c478bd9Sstevel@tonic-gate 				mutex_exit(&pidlock);
5897c478bd9Sstevel@tonic-gate 				error = ESRCH;
5907c478bd9Sstevel@tonic-gate 				break;
5917c478bd9Sstevel@tonic-gate 			}
5927c478bd9Sstevel@tonic-gate 		}
5937c478bd9Sstevel@tonic-gate 		error = pset_bind_thread(tp, pset, &oldpset, projbuf, zonebuf);
5947c478bd9Sstevel@tonic-gate 		mutex_exit(&pp->p_lock);
5957c478bd9Sstevel@tonic-gate 		mutex_exit(&pidlock);
5967c478bd9Sstevel@tonic-gate 		break;
5977c478bd9Sstevel@tonic-gate 
5987c478bd9Sstevel@tonic-gate 	case P_PID:
5997c478bd9Sstevel@tonic-gate 		mutex_enter(&pidlock);
6007c478bd9Sstevel@tonic-gate 		if (id == P_MYID) {
6017c478bd9Sstevel@tonic-gate 			pp = curproc;
6027c478bd9Sstevel@tonic-gate 		} else if ((pp = prfind(id)) == NULL) {
6037c478bd9Sstevel@tonic-gate 			mutex_exit(&pidlock);
6047c478bd9Sstevel@tonic-gate 			error = ESRCH;
6057c478bd9Sstevel@tonic-gate 			break;
6067c478bd9Sstevel@tonic-gate 		}
6077c478bd9Sstevel@tonic-gate 		error = pset_bind_process(pp, pset, &oldpset, projbuf, zonebuf);
6087c478bd9Sstevel@tonic-gate 		mutex_exit(&pidlock);
6097c478bd9Sstevel@tonic-gate 		break;
6107c478bd9Sstevel@tonic-gate 
6117c478bd9Sstevel@tonic-gate 	case P_TASKID:
6127c478bd9Sstevel@tonic-gate 		mutex_enter(&pidlock);
6137c478bd9Sstevel@tonic-gate 		if (id == P_MYID)
6147c478bd9Sstevel@tonic-gate 			id = curproc->p_task->tk_tkid;
6157c478bd9Sstevel@tonic-gate 		if ((tk = task_hold_by_id(id)) == NULL) {
6167c478bd9Sstevel@tonic-gate 			mutex_exit(&pidlock);
6177c478bd9Sstevel@tonic-gate 			error = ESRCH;
6187c478bd9Sstevel@tonic-gate 			break;
6197c478bd9Sstevel@tonic-gate 		}
6207c478bd9Sstevel@tonic-gate 		error = pset_bind_task(tk, pset, &oldpset, projbuf, zonebuf);
6217c478bd9Sstevel@tonic-gate 		mutex_exit(&pidlock);
6227c478bd9Sstevel@tonic-gate 		task_rele(tk);
6237c478bd9Sstevel@tonic-gate 		break;
6247c478bd9Sstevel@tonic-gate 
6257c478bd9Sstevel@tonic-gate 	case P_PROJID:
6260209230bSgjelinek 		pp = curproc;
6277c478bd9Sstevel@tonic-gate 		if (id == P_MYID)
6287c478bd9Sstevel@tonic-gate 			id = curprojid();
6290209230bSgjelinek 		if ((kpj = project_hold_by_id(id, pp->p_zone,
6307c478bd9Sstevel@tonic-gate 		    PROJECT_HOLD_FIND)) == NULL) {
6317c478bd9Sstevel@tonic-gate 			error = ESRCH;
6327c478bd9Sstevel@tonic-gate 			break;
6337c478bd9Sstevel@tonic-gate 		}
6347c478bd9Sstevel@tonic-gate 		mutex_enter(&pidlock);
6357c478bd9Sstevel@tonic-gate 		error = pset_bind_project(kpj, pset, &oldpset, projbuf,
6367c478bd9Sstevel@tonic-gate 		    zonebuf);
6377c478bd9Sstevel@tonic-gate 		mutex_exit(&pidlock);
6387c478bd9Sstevel@tonic-gate 		project_rele(kpj);
6397c478bd9Sstevel@tonic-gate 		break;
6407c478bd9Sstevel@tonic-gate 
6417c478bd9Sstevel@tonic-gate 	case P_ZONEID:
6427c478bd9Sstevel@tonic-gate 		if (id == P_MYID)
6437c478bd9Sstevel@tonic-gate 			id = getzoneid();
6447c478bd9Sstevel@tonic-gate 		if ((zptr = zone_find_by_id(id)) == NULL) {
6457c478bd9Sstevel@tonic-gate 			error = ESRCH;
6467c478bd9Sstevel@tonic-gate 			break;
6477c478bd9Sstevel@tonic-gate 		}
6487c478bd9Sstevel@tonic-gate 		mutex_enter(&pidlock);
6497c478bd9Sstevel@tonic-gate 		error = pset_bind_zone(zptr, pset, &oldpset, projbuf, zonebuf);
6507c478bd9Sstevel@tonic-gate 		mutex_exit(&pidlock);
6517c478bd9Sstevel@tonic-gate 		zone_rele(zptr);
6527c478bd9Sstevel@tonic-gate 		break;
6537c478bd9Sstevel@tonic-gate 
6547c478bd9Sstevel@tonic-gate 	case P_CTID:
6557c478bd9Sstevel@tonic-gate 		if (id == P_MYID)
6567c478bd9Sstevel@tonic-gate 			id = PRCTID(curproc);
6577c478bd9Sstevel@tonic-gate 		if ((ct = contract_type_ptr(process_type, id,
6587c478bd9Sstevel@tonic-gate 		    curproc->p_zone->zone_uniqid)) == NULL) {
6597c478bd9Sstevel@tonic-gate 			error = ESRCH;
6607c478bd9Sstevel@tonic-gate 			break;
6617c478bd9Sstevel@tonic-gate 		}
6627c478bd9Sstevel@tonic-gate 		mutex_enter(&pidlock);
6637c478bd9Sstevel@tonic-gate 		error = pset_bind_contract(ct->ct_data, pset, &oldpset, projbuf,
6647c478bd9Sstevel@tonic-gate 		    zonebuf);
6657c478bd9Sstevel@tonic-gate 		mutex_exit(&pidlock);
6667c478bd9Sstevel@tonic-gate 		contract_rele(ct);
6677c478bd9Sstevel@tonic-gate 		break;
6687c478bd9Sstevel@tonic-gate 
6697c478bd9Sstevel@tonic-gate 	case P_PSETID:
6707c478bd9Sstevel@tonic-gate 		if (id == P_MYID || pset != PS_NONE || !INGLOBALZONE(curproc)) {
6717c478bd9Sstevel@tonic-gate 			error = EINVAL;
6727c478bd9Sstevel@tonic-gate 			break;
6737c478bd9Sstevel@tonic-gate 		}
6747c478bd9Sstevel@tonic-gate 		error = pset_unbind(id, projbuf, zonebuf, idtype);
6757c478bd9Sstevel@tonic-gate 		break;
6767c478bd9Sstevel@tonic-gate 
6777c478bd9Sstevel@tonic-gate 	case P_ALL:
6787c478bd9Sstevel@tonic-gate 		if (id == P_MYID || pset != PS_NONE || !INGLOBALZONE(curproc)) {
6797c478bd9Sstevel@tonic-gate 			error = EINVAL;
6807c478bd9Sstevel@tonic-gate 			break;
6817c478bd9Sstevel@tonic-gate 		}
6827c478bd9Sstevel@tonic-gate 		error = pset_unbind(PS_NONE, projbuf, zonebuf, idtype);
6837c478bd9Sstevel@tonic-gate 		break;
6847c478bd9Sstevel@tonic-gate 
6857c478bd9Sstevel@tonic-gate 	default:
6867c478bd9Sstevel@tonic-gate 		error = EINVAL;
6877c478bd9Sstevel@tonic-gate 		break;
6887c478bd9Sstevel@tonic-gate 	}
6897c478bd9Sstevel@tonic-gate 
6907c478bd9Sstevel@tonic-gate 	fss_freebuf(projbuf, FSS_ALLOC_PROJ);
6917c478bd9Sstevel@tonic-gate 	fss_freebuf(zonebuf, FSS_ALLOC_ZONE);
6927c478bd9Sstevel@tonic-gate 	mutex_exit(&cpu_lock);
6937c478bd9Sstevel@tonic-gate 	pool_unlock();
6947c478bd9Sstevel@tonic-gate 
6957c478bd9Sstevel@tonic-gate 	if (error != 0)
6967c478bd9Sstevel@tonic-gate 		return (set_errno(error));
6977c478bd9Sstevel@tonic-gate 	if (opset != NULL) {
6987c478bd9Sstevel@tonic-gate 		if (copyout(&oldpset, opset, sizeof (psetid_t)) != 0)
6997c478bd9Sstevel@tonic-gate 			return (set_errno(EFAULT));
7007c478bd9Sstevel@tonic-gate 	}
7017c478bd9Sstevel@tonic-gate 	return (0);
7027c478bd9Sstevel@tonic-gate }
7037c478bd9Sstevel@tonic-gate 
7047c478bd9Sstevel@tonic-gate /*
7057c478bd9Sstevel@tonic-gate  * Report load average statistics for the specified processor set.
7067c478bd9Sstevel@tonic-gate  */
7077c478bd9Sstevel@tonic-gate static int
pset_getloadavg(psetid_t pset,int * buf,int nelem)7087c478bd9Sstevel@tonic-gate pset_getloadavg(psetid_t pset, int *buf, int nelem)
7097c478bd9Sstevel@tonic-gate {
710d5fbc238Sfr 	int loadbuf[LOADAVG_NSTATS];
7117c478bd9Sstevel@tonic-gate 	int error = 0;
7127c478bd9Sstevel@tonic-gate 
7137c478bd9Sstevel@tonic-gate 	if (nelem < 0)
7147c478bd9Sstevel@tonic-gate 		return (set_errno(EINVAL));
7157c478bd9Sstevel@tonic-gate 
7167c478bd9Sstevel@tonic-gate 	/*
7177c478bd9Sstevel@tonic-gate 	 * We keep the same number of load average statistics for processor
7187c478bd9Sstevel@tonic-gate 	 * sets as we do for the system as a whole.
7197c478bd9Sstevel@tonic-gate 	 */
7207c478bd9Sstevel@tonic-gate 	if (nelem > LOADAVG_NSTATS)
7217c478bd9Sstevel@tonic-gate 		nelem = LOADAVG_NSTATS;
7227c478bd9Sstevel@tonic-gate 
7237c478bd9Sstevel@tonic-gate 	mutex_enter(&cpu_lock);
7247c478bd9Sstevel@tonic-gate 	error = cpupart_get_loadavg(pset, loadbuf, nelem);
7257c478bd9Sstevel@tonic-gate 	mutex_exit(&cpu_lock);
7267c478bd9Sstevel@tonic-gate 	if (!error && nelem && copyout(loadbuf, buf, nelem * sizeof (int)) != 0)
7277c478bd9Sstevel@tonic-gate 		error = EFAULT;
7287c478bd9Sstevel@tonic-gate 
7297c478bd9Sstevel@tonic-gate 	if (error)
7307c478bd9Sstevel@tonic-gate 		return (set_errno(error));
7317c478bd9Sstevel@tonic-gate 	else
7327c478bd9Sstevel@tonic-gate 		return (0);
7337c478bd9Sstevel@tonic-gate }
7347c478bd9Sstevel@tonic-gate 
7357c478bd9Sstevel@tonic-gate 
7367c478bd9Sstevel@tonic-gate /*
7377c478bd9Sstevel@tonic-gate  * Return list of active processor sets, up to a maximum indicated by
7387c478bd9Sstevel@tonic-gate  * numpsets.  The total number of processor sets is stored in the
7397c478bd9Sstevel@tonic-gate  * location pointed to by numpsets.
7407c478bd9Sstevel@tonic-gate  */
7417c478bd9Sstevel@tonic-gate static int
pset_list(psetid_t * psetlist,uint_t * numpsets)7427c478bd9Sstevel@tonic-gate pset_list(psetid_t *psetlist, uint_t *numpsets)
7437c478bd9Sstevel@tonic-gate {
7447c478bd9Sstevel@tonic-gate 	uint_t user_npsets = 0;
7457c478bd9Sstevel@tonic-gate 	uint_t real_npsets;
7467c478bd9Sstevel@tonic-gate 	psetid_t *psets = NULL;
7477c478bd9Sstevel@tonic-gate 	int error = 0;
7487c478bd9Sstevel@tonic-gate 
7497c478bd9Sstevel@tonic-gate 	if (numpsets != NULL) {
7507c478bd9Sstevel@tonic-gate 		if (copyin(numpsets, &user_npsets, sizeof (uint_t)) != 0)
7517c478bd9Sstevel@tonic-gate 			return (set_errno(EFAULT));
7527c478bd9Sstevel@tonic-gate 	}
7537c478bd9Sstevel@tonic-gate 
7547c478bd9Sstevel@tonic-gate 	/*
7557c478bd9Sstevel@tonic-gate 	 * Get the list of all processor sets.  First we need to find
7567c478bd9Sstevel@tonic-gate 	 * out how many there are, so we can allocate a large enough
7577c478bd9Sstevel@tonic-gate 	 * buffer.
7587c478bd9Sstevel@tonic-gate 	 */
7597c478bd9Sstevel@tonic-gate 	mutex_enter(&cpu_lock);
7607c478bd9Sstevel@tonic-gate 	if (!INGLOBALZONE(curproc) && pool_pset_enabled()) {
7617c478bd9Sstevel@tonic-gate 		psetid_t psetid = zone_pset_get(curproc->p_zone);
7627c478bd9Sstevel@tonic-gate 
7637c478bd9Sstevel@tonic-gate 		if (psetid == PS_NONE) {
7647c478bd9Sstevel@tonic-gate 			real_npsets = 0;
7657c478bd9Sstevel@tonic-gate 		} else {
7667c478bd9Sstevel@tonic-gate 			real_npsets = 1;
7677c478bd9Sstevel@tonic-gate 			psets = kmem_alloc(real_npsets * sizeof (psetid_t),
7687c478bd9Sstevel@tonic-gate 			    KM_SLEEP);
7697c478bd9Sstevel@tonic-gate 			psets[0] = psetid;
7707c478bd9Sstevel@tonic-gate 		}
7717c478bd9Sstevel@tonic-gate 	} else {
772*0e9010a0SToomas Soome 		real_npsets = cpupart_list(NULL, 0, CP_ALL);
7737c478bd9Sstevel@tonic-gate 		if (real_npsets) {
7747c478bd9Sstevel@tonic-gate 			psets = kmem_alloc(real_npsets * sizeof (psetid_t),
7757c478bd9Sstevel@tonic-gate 			    KM_SLEEP);
7767c478bd9Sstevel@tonic-gate 			(void) cpupart_list(psets, real_npsets, CP_ALL);
7777c478bd9Sstevel@tonic-gate 		}
7787c478bd9Sstevel@tonic-gate 	}
7797c478bd9Sstevel@tonic-gate 	mutex_exit(&cpu_lock);
7807c478bd9Sstevel@tonic-gate 
7817c478bd9Sstevel@tonic-gate 	if (user_npsets > real_npsets)
7827c478bd9Sstevel@tonic-gate 		user_npsets = real_npsets;
7837c478bd9Sstevel@tonic-gate 
7847c478bd9Sstevel@tonic-gate 	if (numpsets != NULL) {
7857c478bd9Sstevel@tonic-gate 		if (copyout(&real_npsets, numpsets, sizeof (uint_t)) != 0)
7867c478bd9Sstevel@tonic-gate 			error = EFAULT;
7877c478bd9Sstevel@tonic-gate 		else if (psetlist != NULL && user_npsets != 0) {
7887c478bd9Sstevel@tonic-gate 			if (copyout(psets, psetlist,
7897c478bd9Sstevel@tonic-gate 			    user_npsets * sizeof (psetid_t)) != 0)
7907c478bd9Sstevel@tonic-gate 				error = EFAULT;
7917c478bd9Sstevel@tonic-gate 		}
7927c478bd9Sstevel@tonic-gate 	}
7937c478bd9Sstevel@tonic-gate 
7947c478bd9Sstevel@tonic-gate 	if (real_npsets)
7957c478bd9Sstevel@tonic-gate 		kmem_free(psets, real_npsets * sizeof (psetid_t));
7967c478bd9Sstevel@tonic-gate 
7977c478bd9Sstevel@tonic-gate 	if (error)
7987c478bd9Sstevel@tonic-gate 		return (set_errno(error));
7997c478bd9Sstevel@tonic-gate 	else
8007c478bd9Sstevel@tonic-gate 		return (0);
8017c478bd9Sstevel@tonic-gate }
8027c478bd9Sstevel@tonic-gate 
8037c478bd9Sstevel@tonic-gate static int
pset_setattr(psetid_t pset,uint_t attr)8047c478bd9Sstevel@tonic-gate pset_setattr(psetid_t pset, uint_t attr)
8057c478bd9Sstevel@tonic-gate {
8067c478bd9Sstevel@tonic-gate 	int error;
8077c478bd9Sstevel@tonic-gate 
8087c478bd9Sstevel@tonic-gate 	if (secpolicy_pset(CRED()) != 0)
8097c478bd9Sstevel@tonic-gate 		return (set_errno(EPERM));
8107c478bd9Sstevel@tonic-gate 	pool_lock();
8117c478bd9Sstevel@tonic-gate 	if (pool_state == POOL_ENABLED) {
8127c478bd9Sstevel@tonic-gate 		pool_unlock();
8137c478bd9Sstevel@tonic-gate 		return (set_errno(ENOTSUP));
8147c478bd9Sstevel@tonic-gate 	}
8157c478bd9Sstevel@tonic-gate 	if (pset == PS_QUERY || PSET_BADATTR(attr)) {
8167c478bd9Sstevel@tonic-gate 		pool_unlock();
8177c478bd9Sstevel@tonic-gate 		return (set_errno(EINVAL));
8187c478bd9Sstevel@tonic-gate 	}
8197c478bd9Sstevel@tonic-gate 	if ((error = cpupart_setattr(pset, attr)) != 0) {
8207c478bd9Sstevel@tonic-gate 		pool_unlock();
8217c478bd9Sstevel@tonic-gate 		return (set_errno(error));
8227c478bd9Sstevel@tonic-gate 	}
8237c478bd9Sstevel@tonic-gate 	pool_unlock();
8247c478bd9Sstevel@tonic-gate 	return (0);
8257c478bd9Sstevel@tonic-gate }
8267c478bd9Sstevel@tonic-gate 
8277c478bd9Sstevel@tonic-gate static int
pset_getattr(psetid_t pset,uint_t * attrp)8287c478bd9Sstevel@tonic-gate pset_getattr(psetid_t pset, uint_t *attrp)
8297c478bd9Sstevel@tonic-gate {
8307c478bd9Sstevel@tonic-gate 	int error = 0;
8317c478bd9Sstevel@tonic-gate 	uint_t attr;
8327c478bd9Sstevel@tonic-gate 
8337c478bd9Sstevel@tonic-gate 	if (pset == PS_QUERY)
8347c478bd9Sstevel@tonic-gate 		return (set_errno(EINVAL));
8357c478bd9Sstevel@tonic-gate 	if ((error = cpupart_getattr(pset, &attr)) != 0)
8367c478bd9Sstevel@tonic-gate 		return (set_errno(error));
8377c478bd9Sstevel@tonic-gate 	if (copyout(&attr, attrp, sizeof (uint_t)) != 0)
8387c478bd9Sstevel@tonic-gate 		return (set_errno(EFAULT));
8397c478bd9Sstevel@tonic-gate 	return (0);
8407c478bd9Sstevel@tonic-gate }
8417c478bd9Sstevel@tonic-gate 
8427c478bd9Sstevel@tonic-gate static int
pset(int subcode,long arg1,long arg2,long arg3,long arg4)8437c478bd9Sstevel@tonic-gate pset(int subcode, long arg1, long arg2, long arg3, long arg4)
8447c478bd9Sstevel@tonic-gate {
8457c478bd9Sstevel@tonic-gate 	switch (subcode) {
8467c478bd9Sstevel@tonic-gate 	case PSET_CREATE:
8477c478bd9Sstevel@tonic-gate 		return (pset_create((psetid_t *)arg1));
8487c478bd9Sstevel@tonic-gate 	case PSET_DESTROY:
8497c478bd9Sstevel@tonic-gate 		return (pset_destroy((psetid_t)arg1));
8507c478bd9Sstevel@tonic-gate 	case PSET_ASSIGN:
8517c478bd9Sstevel@tonic-gate 		return (pset_assign((psetid_t)arg1,
8527c478bd9Sstevel@tonic-gate 		    (processorid_t)arg2, (psetid_t *)arg3, 0));
8537c478bd9Sstevel@tonic-gate 	case PSET_INFO:
8547c478bd9Sstevel@tonic-gate 		return (pset_info((psetid_t)arg1, (int *)arg2,
8557c478bd9Sstevel@tonic-gate 		    (uint_t *)arg3, (processorid_t *)arg4));
8567c478bd9Sstevel@tonic-gate 	case PSET_BIND:
8577c478bd9Sstevel@tonic-gate 		return (pset_bind((psetid_t)arg1, (idtype_t)arg2,
8587c478bd9Sstevel@tonic-gate 		    (id_t)arg3, (psetid_t *)arg4));
859fb9b0aa8SSurya Prakki 	case PSET_BIND_LWP:
860fb9b0aa8SSurya Prakki 		return (pset_bind_lwp((psetid_t)arg1, (id_t)arg2,
861fb9b0aa8SSurya Prakki 		    (pid_t)arg3, (psetid_t *)arg4));
8627c478bd9Sstevel@tonic-gate 	case PSET_GETLOADAVG:
8637c478bd9Sstevel@tonic-gate 		return (pset_getloadavg((psetid_t)arg1, (int *)arg2,
8647c478bd9Sstevel@tonic-gate 		    (int)arg3));
8657c478bd9Sstevel@tonic-gate 	case PSET_LIST:
8667c478bd9Sstevel@tonic-gate 		return (pset_list((psetid_t *)arg1, (uint_t *)arg2));
8677c478bd9Sstevel@tonic-gate 	case PSET_SETATTR:
8687c478bd9Sstevel@tonic-gate 		return (pset_setattr((psetid_t)arg1, (uint_t)arg2));
8697c478bd9Sstevel@tonic-gate 	case PSET_GETATTR:
8707c478bd9Sstevel@tonic-gate 		return (pset_getattr((psetid_t)arg1, (uint_t *)arg2));
8717c478bd9Sstevel@tonic-gate 	case PSET_ASSIGN_FORCED:
8727c478bd9Sstevel@tonic-gate 		return (pset_assign((psetid_t)arg1,
8737c478bd9Sstevel@tonic-gate 		    (processorid_t)arg2, (psetid_t *)arg3, 1));
8747c478bd9Sstevel@tonic-gate 	default:
8757c478bd9Sstevel@tonic-gate 		return (set_errno(EINVAL));
8767c478bd9Sstevel@tonic-gate 	}
8777c478bd9Sstevel@tonic-gate }
878