xref: /onnv-gate/usr/src/uts/common/syscall/pset.c (revision 12494:15439b11d535)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
52308Sfr157268  * Common Development and Distribution License (the "License").
62308Sfr157268  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
22*12494Sgerald.jelinek@sun.com  * Copyright (c) 1996, 2010, Oracle and/or its affiliates. All rights reserved.
230Sstevel@tonic-gate  */
240Sstevel@tonic-gate 
250Sstevel@tonic-gate #include <sys/types.h>
260Sstevel@tonic-gate #include <sys/systm.h>
270Sstevel@tonic-gate #include <sys/cmn_err.h>
280Sstevel@tonic-gate #include <sys/cpuvar.h>
290Sstevel@tonic-gate #include <sys/thread.h>
300Sstevel@tonic-gate #include <sys/disp.h>
310Sstevel@tonic-gate #include <sys/kmem.h>
320Sstevel@tonic-gate #include <sys/debug.h>
330Sstevel@tonic-gate #include <sys/sysmacros.h>
340Sstevel@tonic-gate #include <sys/cpupart.h>
350Sstevel@tonic-gate #include <sys/pset.h>
360Sstevel@tonic-gate #include <sys/modctl.h>
370Sstevel@tonic-gate #include <sys/syscall.h>
380Sstevel@tonic-gate #include <sys/task.h>
390Sstevel@tonic-gate #include <sys/loadavg.h>
400Sstevel@tonic-gate #include <sys/fss.h>
410Sstevel@tonic-gate #include <sys/pool.h>
420Sstevel@tonic-gate #include <sys/pool_pset.h>
430Sstevel@tonic-gate #include <sys/policy.h>
440Sstevel@tonic-gate #include <sys/zone.h>
450Sstevel@tonic-gate #include <sys/contract/process_impl.h>
460Sstevel@tonic-gate 
470Sstevel@tonic-gate static int	pset(int, long, long, long, long);
480Sstevel@tonic-gate 
490Sstevel@tonic-gate static struct sysent pset_sysent = {
500Sstevel@tonic-gate 	5,
510Sstevel@tonic-gate 	SE_ARGC | SE_NOUNLOAD,
520Sstevel@tonic-gate 	(int (*)())pset,
530Sstevel@tonic-gate };
540Sstevel@tonic-gate 
550Sstevel@tonic-gate static struct modlsys modlsys = {
560Sstevel@tonic-gate 	&mod_syscallops, "processor sets", &pset_sysent
570Sstevel@tonic-gate };
580Sstevel@tonic-gate 
590Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
600Sstevel@tonic-gate static struct modlsys modlsys32 = {
610Sstevel@tonic-gate 	&mod_syscallops32, "32-bit pset(2) syscall", &pset_sysent
620Sstevel@tonic-gate };
630Sstevel@tonic-gate #endif
640Sstevel@tonic-gate 
650Sstevel@tonic-gate static struct modlinkage modlinkage = {
660Sstevel@tonic-gate 	MODREV_1,
670Sstevel@tonic-gate 	&modlsys,
680Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
690Sstevel@tonic-gate 	&modlsys32,
700Sstevel@tonic-gate #endif
710Sstevel@tonic-gate 	NULL
720Sstevel@tonic-gate };
730Sstevel@tonic-gate 
740Sstevel@tonic-gate #define	PSET_BADATTR(attr)	((~PSET_NOESCAPE) & (attr))
750Sstevel@tonic-gate 
760Sstevel@tonic-gate int
_init(void)770Sstevel@tonic-gate _init(void)
780Sstevel@tonic-gate {
790Sstevel@tonic-gate 	return (mod_install(&modlinkage));
800Sstevel@tonic-gate }
810Sstevel@tonic-gate 
820Sstevel@tonic-gate int
_info(struct modinfo * modinfop)830Sstevel@tonic-gate _info(struct modinfo *modinfop)
840Sstevel@tonic-gate {
850Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
860Sstevel@tonic-gate }
870Sstevel@tonic-gate 
880Sstevel@tonic-gate static int
pset_create(psetid_t * psetp)890Sstevel@tonic-gate pset_create(psetid_t *psetp)
900Sstevel@tonic-gate {
910Sstevel@tonic-gate 	psetid_t newpset;
920Sstevel@tonic-gate 	int error;
930Sstevel@tonic-gate 
940Sstevel@tonic-gate 	if (secpolicy_pset(CRED()) != 0)
950Sstevel@tonic-gate 		return (set_errno(EPERM));
960Sstevel@tonic-gate 
970Sstevel@tonic-gate 	pool_lock();
980Sstevel@tonic-gate 	if (pool_state == POOL_ENABLED) {
990Sstevel@tonic-gate 		pool_unlock();
1000Sstevel@tonic-gate 		return (set_errno(ENOTSUP));
1010Sstevel@tonic-gate 	}
1020Sstevel@tonic-gate 	error = cpupart_create(&newpset);
1030Sstevel@tonic-gate 	if (error) {
1040Sstevel@tonic-gate 		pool_unlock();
1050Sstevel@tonic-gate 		return (set_errno(error));
1060Sstevel@tonic-gate 	}
1070Sstevel@tonic-gate 	if (copyout(&newpset, psetp, sizeof (psetid_t)) != 0) {
1080Sstevel@tonic-gate 		(void) cpupart_destroy(newpset);
1090Sstevel@tonic-gate 		pool_unlock();
1100Sstevel@tonic-gate 		return (set_errno(EFAULT));
1110Sstevel@tonic-gate 	}
1120Sstevel@tonic-gate 	pool_unlock();
1130Sstevel@tonic-gate 	return (error);
1140Sstevel@tonic-gate }
1150Sstevel@tonic-gate 
1160Sstevel@tonic-gate static int
pset_destroy(psetid_t pset)1170Sstevel@tonic-gate pset_destroy(psetid_t pset)
1180Sstevel@tonic-gate {
1190Sstevel@tonic-gate 	int error;
1200Sstevel@tonic-gate 
1210Sstevel@tonic-gate 	if (secpolicy_pset(CRED()) != 0)
1220Sstevel@tonic-gate 		return (set_errno(EPERM));
1230Sstevel@tonic-gate 
1240Sstevel@tonic-gate 	pool_lock();
1250Sstevel@tonic-gate 	if (pool_state == POOL_ENABLED) {
1260Sstevel@tonic-gate 		pool_unlock();
1270Sstevel@tonic-gate 		return (set_errno(ENOTSUP));
1280Sstevel@tonic-gate 	}
1290Sstevel@tonic-gate 	error = cpupart_destroy(pset);
1300Sstevel@tonic-gate 	pool_unlock();
1310Sstevel@tonic-gate 	if (error)
1320Sstevel@tonic-gate 		return (set_errno(error));
1330Sstevel@tonic-gate 	else
1340Sstevel@tonic-gate 		return (0);
1350Sstevel@tonic-gate }
1360Sstevel@tonic-gate 
1370Sstevel@tonic-gate static int
pset_assign(psetid_t pset,processorid_t cpuid,psetid_t * opset,int forced)1380Sstevel@tonic-gate pset_assign(psetid_t pset, processorid_t cpuid, psetid_t *opset, int forced)
1390Sstevel@tonic-gate {
1400Sstevel@tonic-gate 	psetid_t oldpset;
1410Sstevel@tonic-gate 	int	error = 0;
1420Sstevel@tonic-gate 	cpu_t	*cp;
1430Sstevel@tonic-gate 
1440Sstevel@tonic-gate 	if (pset != PS_QUERY && secpolicy_pset(CRED()) != 0)
1450Sstevel@tonic-gate 		return (set_errno(EPERM));
1460Sstevel@tonic-gate 
1470Sstevel@tonic-gate 	pool_lock();
1480Sstevel@tonic-gate 	if (pset != PS_QUERY && pool_state == POOL_ENABLED) {
1490Sstevel@tonic-gate 		pool_unlock();
1500Sstevel@tonic-gate 		return (set_errno(ENOTSUP));
1510Sstevel@tonic-gate 	}
1520Sstevel@tonic-gate 
1530Sstevel@tonic-gate 	mutex_enter(&cpu_lock);
1540Sstevel@tonic-gate 	if ((cp = cpu_get(cpuid)) == NULL) {
1550Sstevel@tonic-gate 		mutex_exit(&cpu_lock);
1560Sstevel@tonic-gate 		pool_unlock();
1570Sstevel@tonic-gate 		return (set_errno(EINVAL));
1580Sstevel@tonic-gate 	}
1590Sstevel@tonic-gate 
1600Sstevel@tonic-gate 	oldpset = cpupart_query_cpu(cp);
1610Sstevel@tonic-gate 
1620Sstevel@tonic-gate 	if (pset != PS_QUERY)
1630Sstevel@tonic-gate 		error = cpupart_attach_cpu(pset, cp, forced);
1640Sstevel@tonic-gate 	mutex_exit(&cpu_lock);
1650Sstevel@tonic-gate 	pool_unlock();
1660Sstevel@tonic-gate 
1670Sstevel@tonic-gate 	if (error)
1680Sstevel@tonic-gate 		return (set_errno(error));
1690Sstevel@tonic-gate 
1700Sstevel@tonic-gate 	if (opset != NULL)
1710Sstevel@tonic-gate 		if (copyout(&oldpset, opset, sizeof (psetid_t)) != 0)
1720Sstevel@tonic-gate 			return (set_errno(EFAULT));
1730Sstevel@tonic-gate 
1740Sstevel@tonic-gate 	return (0);
1750Sstevel@tonic-gate }
1760Sstevel@tonic-gate 
1770Sstevel@tonic-gate static int
pset_info(psetid_t pset,int * typep,uint_t * numcpusp,processorid_t * cpulistp)1780Sstevel@tonic-gate pset_info(psetid_t pset, int *typep, uint_t *numcpusp,
1790Sstevel@tonic-gate     processorid_t *cpulistp)
1800Sstevel@tonic-gate {
1810Sstevel@tonic-gate 	int pset_type;
1820Sstevel@tonic-gate 	uint_t user_ncpus = 0, real_ncpus, copy_ncpus;
1830Sstevel@tonic-gate 	processorid_t *pset_cpus = NULL;
1840Sstevel@tonic-gate 	int error = 0;
1850Sstevel@tonic-gate 
1860Sstevel@tonic-gate 	if (numcpusp != NULL) {
1870Sstevel@tonic-gate 		if (copyin(numcpusp, &user_ncpus, sizeof (uint_t)) != 0)
1880Sstevel@tonic-gate 			return (set_errno(EFAULT));
1890Sstevel@tonic-gate 	}
1900Sstevel@tonic-gate 
1910Sstevel@tonic-gate 	if (user_ncpus > max_ncpus)	/* sanity check */
1920Sstevel@tonic-gate 		user_ncpus = max_ncpus;
1930Sstevel@tonic-gate 	if (user_ncpus != 0 && cpulistp != NULL)
1940Sstevel@tonic-gate 		pset_cpus = kmem_alloc(sizeof (processorid_t) * user_ncpus,
1950Sstevel@tonic-gate 		    KM_SLEEP);
1960Sstevel@tonic-gate 
1970Sstevel@tonic-gate 	real_ncpus = user_ncpus;
1980Sstevel@tonic-gate 	if ((error = cpupart_get_cpus(&pset, pset_cpus, &real_ncpus)) != 0)
1990Sstevel@tonic-gate 		goto out;
2000Sstevel@tonic-gate 
2010Sstevel@tonic-gate 	/*
2020Sstevel@tonic-gate 	 * Now copyout the information about this processor set.
2030Sstevel@tonic-gate 	 */
2040Sstevel@tonic-gate 
2050Sstevel@tonic-gate 	/*
2060Sstevel@tonic-gate 	 * Get number of cpus to copy back.  If the user didn't pass in
2070Sstevel@tonic-gate 	 * a big enough buffer, only copy back as many cpus as fits in
2080Sstevel@tonic-gate 	 * the buffer but copy back the real number of cpus.
2090Sstevel@tonic-gate 	 */
2100Sstevel@tonic-gate 
2110Sstevel@tonic-gate 	if (user_ncpus != 0 && cpulistp != NULL) {
2120Sstevel@tonic-gate 		copy_ncpus = MIN(real_ncpus, user_ncpus);
2130Sstevel@tonic-gate 		if (copyout(pset_cpus, cpulistp,
2140Sstevel@tonic-gate 		    sizeof (processorid_t) * copy_ncpus) != 0) {
2150Sstevel@tonic-gate 			error = EFAULT;
2160Sstevel@tonic-gate 			goto out;
2170Sstevel@tonic-gate 		}
2180Sstevel@tonic-gate 	}
2190Sstevel@tonic-gate 	if (pset_cpus != NULL)
2200Sstevel@tonic-gate 		kmem_free(pset_cpus, sizeof (processorid_t) * user_ncpus);
2210Sstevel@tonic-gate 	if (typep != NULL) {
2220Sstevel@tonic-gate 		if (pset == PS_NONE)
2230Sstevel@tonic-gate 			pset_type = PS_NONE;
2240Sstevel@tonic-gate 		else
2250Sstevel@tonic-gate 			pset_type = PS_PRIVATE;
2260Sstevel@tonic-gate 		if (copyout(&pset_type, typep, sizeof (int)) != 0)
2270Sstevel@tonic-gate 			return (set_errno(EFAULT));
2280Sstevel@tonic-gate 	}
2290Sstevel@tonic-gate 	if (numcpusp != NULL)
2300Sstevel@tonic-gate 		if (copyout(&real_ncpus, numcpusp, sizeof (uint_t)) != 0)
2310Sstevel@tonic-gate 			return (set_errno(EFAULT));
2320Sstevel@tonic-gate 	return (0);
2330Sstevel@tonic-gate 
2340Sstevel@tonic-gate out:
2350Sstevel@tonic-gate 	if (pset_cpus != NULL)
2360Sstevel@tonic-gate 		kmem_free(pset_cpus, sizeof (processorid_t) * user_ncpus);
2370Sstevel@tonic-gate 	return (set_errno(error));
2380Sstevel@tonic-gate }
2390Sstevel@tonic-gate 
2400Sstevel@tonic-gate static int
pset_bind_thread(kthread_t * tp,psetid_t pset,psetid_t * oldpset,void * projbuf,void * zonebuf)2410Sstevel@tonic-gate pset_bind_thread(kthread_t *tp, psetid_t pset, psetid_t *oldpset, void *projbuf,
2420Sstevel@tonic-gate     void *zonebuf)
2430Sstevel@tonic-gate {
2440Sstevel@tonic-gate 	int error = 0;
2450Sstevel@tonic-gate 
2460Sstevel@tonic-gate 	ASSERT(pool_lock_held());
2470Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cpu_lock));
2480Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&ttoproc(tp)->p_lock));
2490Sstevel@tonic-gate 
2500Sstevel@tonic-gate 	*oldpset = tp->t_bind_pset;
2516298Sakolb 
2526298Sakolb 	switch (pset) {
2536298Sakolb 	case PS_SOFT:
2546298Sakolb 		TB_PSET_SOFT_SET(tp);
2556298Sakolb 		break;
2566298Sakolb 
2576298Sakolb 	case PS_HARD:
2586298Sakolb 		TB_PSET_HARD_SET(tp);
2596298Sakolb 		break;
2606298Sakolb 
2616298Sakolb 	case PS_QUERY:
2626298Sakolb 		break;
2636298Sakolb 
2646298Sakolb 	case PS_QUERY_TYPE:
2656298Sakolb 		*oldpset = TB_PSET_IS_SOFT(tp) ? PS_SOFT : PS_HARD;
2666298Sakolb 		break;
2676298Sakolb 
2686298Sakolb 	default:
2690Sstevel@tonic-gate 		/*
2700Sstevel@tonic-gate 		 * Must have the same UID as the target process or
2710Sstevel@tonic-gate 		 * have PRIV_PROC_OWNER privilege.
2720Sstevel@tonic-gate 		 */
2730Sstevel@tonic-gate 		if (!hasprocperm(tp->t_cred, CRED()))
2740Sstevel@tonic-gate 			return (EPERM);
2750Sstevel@tonic-gate 		/*
2760Sstevel@tonic-gate 		 * Unbinding of an unbound thread should always succeed.
2770Sstevel@tonic-gate 		 */
2780Sstevel@tonic-gate 		if (*oldpset == PS_NONE && pset == PS_NONE)
2790Sstevel@tonic-gate 			return (0);
2800Sstevel@tonic-gate 		/*
2810Sstevel@tonic-gate 		 * Only privileged processes can move threads from psets with
2820Sstevel@tonic-gate 		 * PSET_NOESCAPE attribute.
2830Sstevel@tonic-gate 		 */
2840Sstevel@tonic-gate 		if ((tp->t_cpupart->cp_attr & PSET_NOESCAPE) &&
285*12494Sgerald.jelinek@sun.com 		    secpolicy_pbind(CRED()) != 0)
2860Sstevel@tonic-gate 			return (EPERM);
2870Sstevel@tonic-gate 		if ((error = cpupart_bind_thread(tp, pset, 0,
2880Sstevel@tonic-gate 		    projbuf, zonebuf)) == 0)
2890Sstevel@tonic-gate 			tp->t_bind_pset = pset;
2906298Sakolb 
2916298Sakolb 		break;
2920Sstevel@tonic-gate 	}
2936298Sakolb 
2940Sstevel@tonic-gate 	return (error);
2950Sstevel@tonic-gate }
2960Sstevel@tonic-gate 
2970Sstevel@tonic-gate static int
pset_bind_process(proc_t * pp,psetid_t pset,psetid_t * oldpset,void * projbuf,void * zonebuf)2980Sstevel@tonic-gate pset_bind_process(proc_t *pp, psetid_t pset, psetid_t *oldpset, void *projbuf,
2990Sstevel@tonic-gate     void *zonebuf)
3000Sstevel@tonic-gate {
3010Sstevel@tonic-gate 	int error = 0;
3020Sstevel@tonic-gate 	kthread_t *tp;
3030Sstevel@tonic-gate 
3040Sstevel@tonic-gate 	/* skip kernel processes */
3056298Sakolb 	if ((pset != PS_QUERY) && pp->p_flag & SSYS) {
3060Sstevel@tonic-gate 		*oldpset = PS_NONE;
3079150SGangadhar.M@Sun.COM 		return (ENOTSUP);
3080Sstevel@tonic-gate 	}
3090Sstevel@tonic-gate 
3100Sstevel@tonic-gate 	mutex_enter(&pp->p_lock);
3110Sstevel@tonic-gate 	tp = pp->p_tlist;
3120Sstevel@tonic-gate 	if (tp != NULL) {
3130Sstevel@tonic-gate 		do {
3140Sstevel@tonic-gate 			int rval;
3150Sstevel@tonic-gate 
3160Sstevel@tonic-gate 			rval = pset_bind_thread(tp, pset, oldpset, projbuf,
3170Sstevel@tonic-gate 			    zonebuf);
3180Sstevel@tonic-gate 			if (error == 0)
3190Sstevel@tonic-gate 				error = rval;
3200Sstevel@tonic-gate 		} while ((tp = tp->t_forw) != pp->p_tlist);
3210Sstevel@tonic-gate 	} else
3220Sstevel@tonic-gate 		error = ESRCH;
3230Sstevel@tonic-gate 	mutex_exit(&pp->p_lock);
3240Sstevel@tonic-gate 
3250Sstevel@tonic-gate 	return (error);
3260Sstevel@tonic-gate }
3270Sstevel@tonic-gate 
3280Sstevel@tonic-gate static int
pset_bind_task(task_t * tk,psetid_t pset,psetid_t * oldpset,void * projbuf,void * zonebuf)3290Sstevel@tonic-gate pset_bind_task(task_t *tk, psetid_t pset, psetid_t *oldpset, void *projbuf,
3300Sstevel@tonic-gate     void *zonebuf)
3310Sstevel@tonic-gate {
3320Sstevel@tonic-gate 	int error = 0;
3330Sstevel@tonic-gate 	proc_t *pp;
3340Sstevel@tonic-gate 
3350Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&pidlock));
3360Sstevel@tonic-gate 
3370Sstevel@tonic-gate 	if ((pp = tk->tk_memb_list) == NULL) {
3380Sstevel@tonic-gate 		return (ESRCH);
3390Sstevel@tonic-gate 	}
3400Sstevel@tonic-gate 
3410Sstevel@tonic-gate 	do {
3420Sstevel@tonic-gate 		int rval;
3430Sstevel@tonic-gate 
3449150SGangadhar.M@Sun.COM 		if (!(pp->p_flag & SSYS)) {
3459150SGangadhar.M@Sun.COM 			rval = pset_bind_process(pp, pset, oldpset, projbuf,
3469150SGangadhar.M@Sun.COM 			    zonebuf);
3479150SGangadhar.M@Sun.COM 			if (error == 0)
3489150SGangadhar.M@Sun.COM 				error = rval;
3499150SGangadhar.M@Sun.COM 		}
3500Sstevel@tonic-gate 	} while ((pp = pp->p_tasknext) != tk->tk_memb_list);
3510Sstevel@tonic-gate 
3520Sstevel@tonic-gate 	return (error);
3530Sstevel@tonic-gate }
3540Sstevel@tonic-gate 
3550Sstevel@tonic-gate static int
pset_bind_project(kproject_t * kpj,psetid_t pset,psetid_t * oldpset,void * projbuf,void * zonebuf)3560Sstevel@tonic-gate pset_bind_project(kproject_t *kpj, psetid_t pset, psetid_t *oldpset,
3570Sstevel@tonic-gate     void *projbuf, void *zonebuf)
3580Sstevel@tonic-gate {
3590Sstevel@tonic-gate 	int error = 0;
3600Sstevel@tonic-gate 	proc_t *pp;
3610Sstevel@tonic-gate 
3620Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&pidlock));
3630Sstevel@tonic-gate 
3640Sstevel@tonic-gate 	for (pp = practive; pp != NULL; pp = pp->p_next) {
3650Sstevel@tonic-gate 		if (pp->p_tlist == NULL)
3660Sstevel@tonic-gate 			continue;
3679150SGangadhar.M@Sun.COM 		if (pp->p_task->tk_proj == kpj && !(pp->p_flag & SSYS)) {
3680Sstevel@tonic-gate 			int rval;
3690Sstevel@tonic-gate 
3700Sstevel@tonic-gate 			rval = pset_bind_process(pp, pset, oldpset, projbuf,
3710Sstevel@tonic-gate 			    zonebuf);
3720Sstevel@tonic-gate 			if (error == 0)
3730Sstevel@tonic-gate 				error = rval;
3740Sstevel@tonic-gate 		}
3750Sstevel@tonic-gate 	}
3760Sstevel@tonic-gate 
3770Sstevel@tonic-gate 	return (error);
3780Sstevel@tonic-gate }
3790Sstevel@tonic-gate 
3800Sstevel@tonic-gate static int
pset_bind_zone(zone_t * zptr,psetid_t pset,psetid_t * oldpset,void * projbuf,void * zonebuf)3810Sstevel@tonic-gate pset_bind_zone(zone_t *zptr, psetid_t pset, psetid_t *oldpset, void *projbuf,
3820Sstevel@tonic-gate     void *zonebuf)
3830Sstevel@tonic-gate {
3840Sstevel@tonic-gate 	int error = 0;
3850Sstevel@tonic-gate 	proc_t *pp;
3860Sstevel@tonic-gate 
3870Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&pidlock));
3880Sstevel@tonic-gate 
3890Sstevel@tonic-gate 	for (pp = practive; pp != NULL; pp = pp->p_next) {
3909150SGangadhar.M@Sun.COM 		if (pp->p_zone == zptr && !(pp->p_flag & SSYS)) {
3910Sstevel@tonic-gate 			int rval;
3920Sstevel@tonic-gate 
3930Sstevel@tonic-gate 			rval = pset_bind_process(pp, pset, oldpset, projbuf,
3940Sstevel@tonic-gate 			    zonebuf);
3950Sstevel@tonic-gate 			if (error == 0)
3960Sstevel@tonic-gate 				error = rval;
3970Sstevel@tonic-gate 		}
3980Sstevel@tonic-gate 	}
3990Sstevel@tonic-gate 
4000Sstevel@tonic-gate 	return (error);
4010Sstevel@tonic-gate }
4020Sstevel@tonic-gate 
4030Sstevel@tonic-gate /*
4040Sstevel@tonic-gate  * Unbind all threads from the specified processor set, or from all
4050Sstevel@tonic-gate  * processor sets.
4060Sstevel@tonic-gate  */
4070Sstevel@tonic-gate static int
pset_unbind(psetid_t pset,void * projbuf,void * zonebuf,idtype_t idtype)4080Sstevel@tonic-gate pset_unbind(psetid_t pset, void *projbuf, void *zonebuf, idtype_t idtype)
4090Sstevel@tonic-gate {
4100Sstevel@tonic-gate 	psetid_t olbind;
4110Sstevel@tonic-gate 	kthread_t *tp;
4120Sstevel@tonic-gate 	int error = 0;
4130Sstevel@tonic-gate 	int rval;
4140Sstevel@tonic-gate 	proc_t *pp;
4150Sstevel@tonic-gate 
4160Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cpu_lock));
4170Sstevel@tonic-gate 
4180Sstevel@tonic-gate 	if (idtype == P_PSETID && cpupart_find(pset) == NULL)
4190Sstevel@tonic-gate 		return (EINVAL);
4200Sstevel@tonic-gate 
4210Sstevel@tonic-gate 	mutex_enter(&pidlock);
4220Sstevel@tonic-gate 	for (pp = practive; pp != NULL; pp = pp->p_next) {
4230Sstevel@tonic-gate 		mutex_enter(&pp->p_lock);
4240Sstevel@tonic-gate 		tp = pp->p_tlist;
4250Sstevel@tonic-gate 		/*
4260Sstevel@tonic-gate 		 * Skip zombies and kernel processes, and processes in
4270Sstevel@tonic-gate 		 * other zones, if called from a non-global zone.
4280Sstevel@tonic-gate 		 */
4290Sstevel@tonic-gate 		if (tp == NULL || (pp->p_flag & SSYS) ||
4300Sstevel@tonic-gate 		    !HASZONEACCESS(curproc, pp->p_zone->zone_id)) {
4310Sstevel@tonic-gate 			mutex_exit(&pp->p_lock);
4320Sstevel@tonic-gate 			continue;
4330Sstevel@tonic-gate 		}
4340Sstevel@tonic-gate 		do {
4350Sstevel@tonic-gate 			if ((idtype == P_PSETID && tp->t_bind_pset != pset) ||
4360Sstevel@tonic-gate 			    (idtype == P_ALL && tp->t_bind_pset == PS_NONE))
4370Sstevel@tonic-gate 				continue;
4380Sstevel@tonic-gate 			rval = pset_bind_thread(tp, PS_NONE, &olbind,
4390Sstevel@tonic-gate 			    projbuf, zonebuf);
4400Sstevel@tonic-gate 			if (error == 0)
4410Sstevel@tonic-gate 				error = rval;
4420Sstevel@tonic-gate 		} while ((tp = tp->t_forw) != pp->p_tlist);
4430Sstevel@tonic-gate 		mutex_exit(&pp->p_lock);
4440Sstevel@tonic-gate 	}
4450Sstevel@tonic-gate 	mutex_exit(&pidlock);
4460Sstevel@tonic-gate 	return (error);
4470Sstevel@tonic-gate }
4480Sstevel@tonic-gate 
4490Sstevel@tonic-gate static int
pset_bind_contract(cont_process_t * ctp,psetid_t pset,psetid_t * oldpset,void * projbuf,void * zonebuf)4500Sstevel@tonic-gate pset_bind_contract(cont_process_t *ctp, psetid_t pset, psetid_t *oldpset,
4510Sstevel@tonic-gate     void *projbuf, void *zonebuf)
4520Sstevel@tonic-gate {
4530Sstevel@tonic-gate 	int error = 0;
4540Sstevel@tonic-gate 	proc_t *pp;
4550Sstevel@tonic-gate 
4560Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&pidlock));
4570Sstevel@tonic-gate 
4580Sstevel@tonic-gate 	for (pp = practive; pp != NULL; pp = pp->p_next) {
4590Sstevel@tonic-gate 		if (pp->p_ct_process == ctp) {
4600Sstevel@tonic-gate 			int rval;
4610Sstevel@tonic-gate 
4620Sstevel@tonic-gate 			rval = pset_bind_process(pp, pset, oldpset, projbuf,
4630Sstevel@tonic-gate 			    zonebuf);
4640Sstevel@tonic-gate 			if (error == 0)
4650Sstevel@tonic-gate 				error = rval;
4660Sstevel@tonic-gate 		}
4670Sstevel@tonic-gate 	}
4680Sstevel@tonic-gate 
4690Sstevel@tonic-gate 	return (error);
4700Sstevel@tonic-gate }
4710Sstevel@tonic-gate 
47210089SSurya.Prakki@Sun.COM /*
47310089SSurya.Prakki@Sun.COM  * Bind the lwp:id of process:pid to processor set: pset
47410089SSurya.Prakki@Sun.COM  */
47510089SSurya.Prakki@Sun.COM static int
pset_bind_lwp(psetid_t pset,id_t id,pid_t pid,psetid_t * opset)47610089SSurya.Prakki@Sun.COM pset_bind_lwp(psetid_t pset, id_t id, pid_t pid, psetid_t *opset)
47710089SSurya.Prakki@Sun.COM {
47810089SSurya.Prakki@Sun.COM 	kthread_t	*tp;
47910089SSurya.Prakki@Sun.COM 	proc_t		*pp;
48010089SSurya.Prakki@Sun.COM 	psetid_t	oldpset;
48110089SSurya.Prakki@Sun.COM 	void		*projbuf, *zonebuf;
48210089SSurya.Prakki@Sun.COM 	int		error = 0;
48310089SSurya.Prakki@Sun.COM 
48410089SSurya.Prakki@Sun.COM 	pool_lock();
48510089SSurya.Prakki@Sun.COM 	mutex_enter(&cpu_lock);
48610089SSurya.Prakki@Sun.COM 	projbuf = fss_allocbuf(FSS_NPROJ_BUF, FSS_ALLOC_PROJ);
48710089SSurya.Prakki@Sun.COM 	zonebuf = fss_allocbuf(FSS_NPROJ_BUF, FSS_ALLOC_ZONE);
48810089SSurya.Prakki@Sun.COM 
48910089SSurya.Prakki@Sun.COM 	mutex_enter(&pidlock);
49010089SSurya.Prakki@Sun.COM 	if ((pid == P_MYID && id == P_MYID) ||
49110089SSurya.Prakki@Sun.COM 	    (pid == curproc->p_pid && id == P_MYID)) {
49210089SSurya.Prakki@Sun.COM 		pp = curproc;
49310089SSurya.Prakki@Sun.COM 		tp = curthread;
49410089SSurya.Prakki@Sun.COM 		mutex_enter(&pp->p_lock);
49510089SSurya.Prakki@Sun.COM 	} else {
49610089SSurya.Prakki@Sun.COM 		if (pid == P_MYID) {
49710089SSurya.Prakki@Sun.COM 			pp = curproc;
49810089SSurya.Prakki@Sun.COM 		} else if ((pp = prfind(pid)) == NULL) {
49910089SSurya.Prakki@Sun.COM 			error = ESRCH;
50010089SSurya.Prakki@Sun.COM 			goto err;
50110089SSurya.Prakki@Sun.COM 		}
50210089SSurya.Prakki@Sun.COM 		if (pp != curproc && id == P_MYID) {
50310089SSurya.Prakki@Sun.COM 			error = EINVAL;
50410089SSurya.Prakki@Sun.COM 			goto err;
50510089SSurya.Prakki@Sun.COM 		}
50610089SSurya.Prakki@Sun.COM 		mutex_enter(&pp->p_lock);
50710089SSurya.Prakki@Sun.COM 		if ((tp = idtot(pp, id)) == NULL) {
50810089SSurya.Prakki@Sun.COM 			mutex_exit(&pp->p_lock);
50910089SSurya.Prakki@Sun.COM 			error = ESRCH;
51010089SSurya.Prakki@Sun.COM 			goto err;
51110089SSurya.Prakki@Sun.COM 		}
51210089SSurya.Prakki@Sun.COM 	}
51310089SSurya.Prakki@Sun.COM 
51410089SSurya.Prakki@Sun.COM 	error = pset_bind_thread(tp, pset, &oldpset, projbuf, zonebuf);
51510089SSurya.Prakki@Sun.COM 	mutex_exit(&pp->p_lock);
51610089SSurya.Prakki@Sun.COM err:
51710089SSurya.Prakki@Sun.COM 	mutex_exit(&pidlock);
51810089SSurya.Prakki@Sun.COM 
51910089SSurya.Prakki@Sun.COM 	fss_freebuf(projbuf, FSS_ALLOC_PROJ);
52010089SSurya.Prakki@Sun.COM 	fss_freebuf(zonebuf, FSS_ALLOC_ZONE);
52110089SSurya.Prakki@Sun.COM 	mutex_exit(&cpu_lock);
52210089SSurya.Prakki@Sun.COM 	pool_unlock();
52310089SSurya.Prakki@Sun.COM 	if (opset != NULL) {
52410089SSurya.Prakki@Sun.COM 		if (copyout(&oldpset, opset, sizeof (psetid_t)) != 0)
52510089SSurya.Prakki@Sun.COM 			return (set_errno(EFAULT));
52610089SSurya.Prakki@Sun.COM 	}
52710089SSurya.Prakki@Sun.COM 	if (error != 0)
52810089SSurya.Prakki@Sun.COM 		return (set_errno(error));
52910089SSurya.Prakki@Sun.COM 	return (0);
53010089SSurya.Prakki@Sun.COM }
53110089SSurya.Prakki@Sun.COM 
5320Sstevel@tonic-gate static int
pset_bind(psetid_t pset,idtype_t idtype,id_t id,psetid_t * opset)5330Sstevel@tonic-gate pset_bind(psetid_t pset, idtype_t idtype, id_t id, psetid_t *opset)
5340Sstevel@tonic-gate {
5350Sstevel@tonic-gate 	kthread_t	*tp;
5360Sstevel@tonic-gate 	proc_t		*pp;
5370Sstevel@tonic-gate 	task_t		*tk;
5380Sstevel@tonic-gate 	kproject_t	*kpj;
5390Sstevel@tonic-gate 	contract_t	*ct;
5400Sstevel@tonic-gate 	zone_t		*zptr;
5410Sstevel@tonic-gate 	psetid_t	oldpset;
5420Sstevel@tonic-gate 	int		error = 0;
5430Sstevel@tonic-gate 	void		*projbuf, *zonebuf;
5440Sstevel@tonic-gate 
5450Sstevel@tonic-gate 	pool_lock();
5466298Sakolb 	if ((pset != PS_QUERY) && (pset != PS_SOFT) &&
5476298Sakolb 	    (pset != PS_HARD) && (pset != PS_QUERY_TYPE)) {
5480Sstevel@tonic-gate 		/*
5490Sstevel@tonic-gate 		 * Check if the set actually exists before checking
5500Sstevel@tonic-gate 		 * permissions.  This is the historical error
5510Sstevel@tonic-gate 		 * precedence.  Note that if pset was PS_MYID, the
5520Sstevel@tonic-gate 		 * cpupart_get_cpus call will change it to the
5530Sstevel@tonic-gate 		 * processor set id of the caller (or PS_NONE if the
5540Sstevel@tonic-gate 		 * caller is not bound to a processor set).
5550Sstevel@tonic-gate 		 */
5560Sstevel@tonic-gate 		if (pool_state == POOL_ENABLED) {
5570Sstevel@tonic-gate 			pool_unlock();
5580Sstevel@tonic-gate 			return (set_errno(ENOTSUP));
5590Sstevel@tonic-gate 		}
5600Sstevel@tonic-gate 		if (cpupart_get_cpus(&pset, NULL, NULL) != 0) {
5610Sstevel@tonic-gate 			pool_unlock();
5620Sstevel@tonic-gate 			return (set_errno(EINVAL));
563*12494Sgerald.jelinek@sun.com 		} else if (pset != PS_NONE && secpolicy_pbind(CRED()) != 0) {
5640Sstevel@tonic-gate 			pool_unlock();
5650Sstevel@tonic-gate 			return (set_errno(EPERM));
5660Sstevel@tonic-gate 		}
5670Sstevel@tonic-gate 	}
5680Sstevel@tonic-gate 
5690Sstevel@tonic-gate 	/*
5700Sstevel@tonic-gate 	 * Pre-allocate enough buffers for FSS for all active projects
5710Sstevel@tonic-gate 	 * and for all active zones on the system.  Unused buffers will
5720Sstevel@tonic-gate 	 * be freed later by fss_freebuf().
5730Sstevel@tonic-gate 	 */
5740Sstevel@tonic-gate 	mutex_enter(&cpu_lock);
5750Sstevel@tonic-gate 	projbuf = fss_allocbuf(FSS_NPROJ_BUF, FSS_ALLOC_PROJ);
5760Sstevel@tonic-gate 	zonebuf = fss_allocbuf(FSS_NPROJ_BUF, FSS_ALLOC_ZONE);
5770Sstevel@tonic-gate 
5780Sstevel@tonic-gate 	switch (idtype) {
5790Sstevel@tonic-gate 	case P_LWPID:
5800Sstevel@tonic-gate 		pp = curproc;
5810Sstevel@tonic-gate 		mutex_enter(&pidlock);
5820Sstevel@tonic-gate 		mutex_enter(&pp->p_lock);
5830Sstevel@tonic-gate 		if (id == P_MYID) {
5840Sstevel@tonic-gate 			tp = curthread;
5850Sstevel@tonic-gate 		} else {
5860Sstevel@tonic-gate 			if ((tp = idtot(pp, id)) == NULL) {
5870Sstevel@tonic-gate 				mutex_exit(&pp->p_lock);
5880Sstevel@tonic-gate 				mutex_exit(&pidlock);
5890Sstevel@tonic-gate 				error = ESRCH;
5900Sstevel@tonic-gate 				break;
5910Sstevel@tonic-gate 			}
5920Sstevel@tonic-gate 		}
5930Sstevel@tonic-gate 		error = pset_bind_thread(tp, pset, &oldpset, projbuf, zonebuf);
5940Sstevel@tonic-gate 		mutex_exit(&pp->p_lock);
5950Sstevel@tonic-gate 		mutex_exit(&pidlock);
5960Sstevel@tonic-gate 		break;
5970Sstevel@tonic-gate 
5980Sstevel@tonic-gate 	case P_PID:
5990Sstevel@tonic-gate 		mutex_enter(&pidlock);
6000Sstevel@tonic-gate 		if (id == P_MYID) {
6010Sstevel@tonic-gate 			pp = curproc;
6020Sstevel@tonic-gate 		} else if ((pp = prfind(id)) == NULL) {
6030Sstevel@tonic-gate 			mutex_exit(&pidlock);
6040Sstevel@tonic-gate 			error = ESRCH;
6050Sstevel@tonic-gate 			break;
6060Sstevel@tonic-gate 		}
6070Sstevel@tonic-gate 		error = pset_bind_process(pp, pset, &oldpset, projbuf, zonebuf);
6080Sstevel@tonic-gate 		mutex_exit(&pidlock);
6090Sstevel@tonic-gate 		break;
6100Sstevel@tonic-gate 
6110Sstevel@tonic-gate 	case P_TASKID:
6120Sstevel@tonic-gate 		mutex_enter(&pidlock);
6130Sstevel@tonic-gate 		if (id == P_MYID)
6140Sstevel@tonic-gate 			id = curproc->p_task->tk_tkid;
6150Sstevel@tonic-gate 		if ((tk = task_hold_by_id(id)) == NULL) {
6160Sstevel@tonic-gate 			mutex_exit(&pidlock);
6170Sstevel@tonic-gate 			error = ESRCH;
6180Sstevel@tonic-gate 			break;
6190Sstevel@tonic-gate 		}
6200Sstevel@tonic-gate 		error = pset_bind_task(tk, pset, &oldpset, projbuf, zonebuf);
6210Sstevel@tonic-gate 		mutex_exit(&pidlock);
6220Sstevel@tonic-gate 		task_rele(tk);
6230Sstevel@tonic-gate 		break;
6240Sstevel@tonic-gate 
6250Sstevel@tonic-gate 	case P_PROJID:
6263247Sgjelinek 		pp = curproc;
6270Sstevel@tonic-gate 		if (id == P_MYID)
6280Sstevel@tonic-gate 			id = curprojid();
6293247Sgjelinek 		if ((kpj = project_hold_by_id(id, pp->p_zone,
6300Sstevel@tonic-gate 		    PROJECT_HOLD_FIND)) == NULL) {
6310Sstevel@tonic-gate 			error = ESRCH;
6320Sstevel@tonic-gate 			break;
6330Sstevel@tonic-gate 		}
6340Sstevel@tonic-gate 		mutex_enter(&pidlock);
6350Sstevel@tonic-gate 		error = pset_bind_project(kpj, pset, &oldpset, projbuf,
6360Sstevel@tonic-gate 		    zonebuf);
6370Sstevel@tonic-gate 		mutex_exit(&pidlock);
6380Sstevel@tonic-gate 		project_rele(kpj);
6390Sstevel@tonic-gate 		break;
6400Sstevel@tonic-gate 
6410Sstevel@tonic-gate 	case P_ZONEID:
6420Sstevel@tonic-gate 		if (id == P_MYID)
6430Sstevel@tonic-gate 			id = getzoneid();
6440Sstevel@tonic-gate 		if ((zptr = zone_find_by_id(id)) == NULL) {
6450Sstevel@tonic-gate 			error = ESRCH;
6460Sstevel@tonic-gate 			break;
6470Sstevel@tonic-gate 		}
6480Sstevel@tonic-gate 		mutex_enter(&pidlock);
6490Sstevel@tonic-gate 		error = pset_bind_zone(zptr, pset, &oldpset, projbuf, zonebuf);
6500Sstevel@tonic-gate 		mutex_exit(&pidlock);
6510Sstevel@tonic-gate 		zone_rele(zptr);
6520Sstevel@tonic-gate 		break;
6530Sstevel@tonic-gate 
6540Sstevel@tonic-gate 	case P_CTID:
6550Sstevel@tonic-gate 		if (id == P_MYID)
6560Sstevel@tonic-gate 			id = PRCTID(curproc);
6570Sstevel@tonic-gate 		if ((ct = contract_type_ptr(process_type, id,
6580Sstevel@tonic-gate 		    curproc->p_zone->zone_uniqid)) == NULL) {
6590Sstevel@tonic-gate 			error = ESRCH;
6600Sstevel@tonic-gate 			break;
6610Sstevel@tonic-gate 		}
6620Sstevel@tonic-gate 		mutex_enter(&pidlock);
6630Sstevel@tonic-gate 		error = pset_bind_contract(ct->ct_data, pset, &oldpset, projbuf,
6640Sstevel@tonic-gate 		    zonebuf);
6650Sstevel@tonic-gate 		mutex_exit(&pidlock);
6660Sstevel@tonic-gate 		contract_rele(ct);
6670Sstevel@tonic-gate 		break;
6680Sstevel@tonic-gate 
6690Sstevel@tonic-gate 	case P_PSETID:
6700Sstevel@tonic-gate 		if (id == P_MYID || pset != PS_NONE || !INGLOBALZONE(curproc)) {
6710Sstevel@tonic-gate 			error = EINVAL;
6720Sstevel@tonic-gate 			break;
6730Sstevel@tonic-gate 		}
6740Sstevel@tonic-gate 		error = pset_unbind(id, projbuf, zonebuf, idtype);
6750Sstevel@tonic-gate 		break;
6760Sstevel@tonic-gate 
6770Sstevel@tonic-gate 	case P_ALL:
6780Sstevel@tonic-gate 		if (id == P_MYID || pset != PS_NONE || !INGLOBALZONE(curproc)) {
6790Sstevel@tonic-gate 			error = EINVAL;
6800Sstevel@tonic-gate 			break;
6810Sstevel@tonic-gate 		}
6820Sstevel@tonic-gate 		error = pset_unbind(PS_NONE, projbuf, zonebuf, idtype);
6830Sstevel@tonic-gate 		break;
6840Sstevel@tonic-gate 
6850Sstevel@tonic-gate 	default:
6860Sstevel@tonic-gate 		error = EINVAL;
6870Sstevel@tonic-gate 		break;
6880Sstevel@tonic-gate 	}
6890Sstevel@tonic-gate 
6900Sstevel@tonic-gate 	fss_freebuf(projbuf, FSS_ALLOC_PROJ);
6910Sstevel@tonic-gate 	fss_freebuf(zonebuf, FSS_ALLOC_ZONE);
6920Sstevel@tonic-gate 	mutex_exit(&cpu_lock);
6930Sstevel@tonic-gate 	pool_unlock();
6940Sstevel@tonic-gate 
6950Sstevel@tonic-gate 	if (error != 0)
6960Sstevel@tonic-gate 		return (set_errno(error));
6970Sstevel@tonic-gate 	if (opset != NULL) {
6980Sstevel@tonic-gate 		if (copyout(&oldpset, opset, sizeof (psetid_t)) != 0)
6990Sstevel@tonic-gate 			return (set_errno(EFAULT));
7000Sstevel@tonic-gate 	}
7010Sstevel@tonic-gate 	return (0);
7020Sstevel@tonic-gate }
7030Sstevel@tonic-gate 
7040Sstevel@tonic-gate /*
7050Sstevel@tonic-gate  * Report load average statistics for the specified processor set.
7060Sstevel@tonic-gate  */
7070Sstevel@tonic-gate static int
pset_getloadavg(psetid_t pset,int * buf,int nelem)7080Sstevel@tonic-gate pset_getloadavg(psetid_t pset, int *buf, int nelem)
7090Sstevel@tonic-gate {
7102308Sfr157268 	int loadbuf[LOADAVG_NSTATS];
7110Sstevel@tonic-gate 	int error = 0;
7120Sstevel@tonic-gate 
7130Sstevel@tonic-gate 	if (nelem < 0)
7140Sstevel@tonic-gate 		return (set_errno(EINVAL));
7150Sstevel@tonic-gate 
7160Sstevel@tonic-gate 	/*
7170Sstevel@tonic-gate 	 * We keep the same number of load average statistics for processor
7180Sstevel@tonic-gate 	 * sets as we do for the system as a whole.
7190Sstevel@tonic-gate 	 */
7200Sstevel@tonic-gate 	if (nelem > LOADAVG_NSTATS)
7210Sstevel@tonic-gate 		nelem = LOADAVG_NSTATS;
7220Sstevel@tonic-gate 
7230Sstevel@tonic-gate 	mutex_enter(&cpu_lock);
7240Sstevel@tonic-gate 	error = cpupart_get_loadavg(pset, loadbuf, nelem);
7250Sstevel@tonic-gate 	mutex_exit(&cpu_lock);
7260Sstevel@tonic-gate 	if (!error && nelem && copyout(loadbuf, buf, nelem * sizeof (int)) != 0)
7270Sstevel@tonic-gate 		error = EFAULT;
7280Sstevel@tonic-gate 
7290Sstevel@tonic-gate 	if (error)
7300Sstevel@tonic-gate 		return (set_errno(error));
7310Sstevel@tonic-gate 	else
7320Sstevel@tonic-gate 		return (0);
7330Sstevel@tonic-gate }
7340Sstevel@tonic-gate 
7350Sstevel@tonic-gate 
7360Sstevel@tonic-gate /*
7370Sstevel@tonic-gate  * Return list of active processor sets, up to a maximum indicated by
7380Sstevel@tonic-gate  * numpsets.  The total number of processor sets is stored in the
7390Sstevel@tonic-gate  * location pointed to by numpsets.
7400Sstevel@tonic-gate  */
7410Sstevel@tonic-gate static int
pset_list(psetid_t * psetlist,uint_t * numpsets)7420Sstevel@tonic-gate pset_list(psetid_t *psetlist, uint_t *numpsets)
7430Sstevel@tonic-gate {
7440Sstevel@tonic-gate 	uint_t user_npsets = 0;
7450Sstevel@tonic-gate 	uint_t real_npsets;
7460Sstevel@tonic-gate 	psetid_t *psets = NULL;
7470Sstevel@tonic-gate 	int error = 0;
7480Sstevel@tonic-gate 
7490Sstevel@tonic-gate 	if (numpsets != NULL) {
7500Sstevel@tonic-gate 		if (copyin(numpsets, &user_npsets, sizeof (uint_t)) != 0)
7510Sstevel@tonic-gate 			return (set_errno(EFAULT));
7520Sstevel@tonic-gate 	}
7530Sstevel@tonic-gate 
7540Sstevel@tonic-gate 	/*
7550Sstevel@tonic-gate 	 * Get the list of all processor sets.  First we need to find
7560Sstevel@tonic-gate 	 * out how many there are, so we can allocate a large enough
7570Sstevel@tonic-gate 	 * buffer.
7580Sstevel@tonic-gate 	 */
7590Sstevel@tonic-gate 	mutex_enter(&cpu_lock);
7600Sstevel@tonic-gate 	if (!INGLOBALZONE(curproc) && pool_pset_enabled()) {
7610Sstevel@tonic-gate 		psetid_t psetid = zone_pset_get(curproc->p_zone);
7620Sstevel@tonic-gate 
7630Sstevel@tonic-gate 		if (psetid == PS_NONE) {
7640Sstevel@tonic-gate 			real_npsets = 0;
7650Sstevel@tonic-gate 		} else {
7660Sstevel@tonic-gate 			real_npsets = 1;
7670Sstevel@tonic-gate 			psets = kmem_alloc(real_npsets * sizeof (psetid_t),
7680Sstevel@tonic-gate 			    KM_SLEEP);
7690Sstevel@tonic-gate 			psets[0] = psetid;
7700Sstevel@tonic-gate 		}
7710Sstevel@tonic-gate 	} else {
7720Sstevel@tonic-gate 		real_npsets = cpupart_list(0, NULL, CP_ALL);
7730Sstevel@tonic-gate 		if (real_npsets) {
7740Sstevel@tonic-gate 			psets = kmem_alloc(real_npsets * sizeof (psetid_t),
7750Sstevel@tonic-gate 			    KM_SLEEP);
7760Sstevel@tonic-gate 			(void) cpupart_list(psets, real_npsets, CP_ALL);
7770Sstevel@tonic-gate 		}
7780Sstevel@tonic-gate 	}
7790Sstevel@tonic-gate 	mutex_exit(&cpu_lock);
7800Sstevel@tonic-gate 
7810Sstevel@tonic-gate 	if (user_npsets > real_npsets)
7820Sstevel@tonic-gate 		user_npsets = real_npsets;
7830Sstevel@tonic-gate 
7840Sstevel@tonic-gate 	if (numpsets != NULL) {
7850Sstevel@tonic-gate 		if (copyout(&real_npsets, numpsets, sizeof (uint_t)) != 0)
7860Sstevel@tonic-gate 			error = EFAULT;
7870Sstevel@tonic-gate 		else if (psetlist != NULL && user_npsets != 0) {
7880Sstevel@tonic-gate 			if (copyout(psets, psetlist,
7890Sstevel@tonic-gate 			    user_npsets * sizeof (psetid_t)) != 0)
7900Sstevel@tonic-gate 				error = EFAULT;
7910Sstevel@tonic-gate 		}
7920Sstevel@tonic-gate 	}
7930Sstevel@tonic-gate 
7940Sstevel@tonic-gate 	if (real_npsets)
7950Sstevel@tonic-gate 		kmem_free(psets, real_npsets * sizeof (psetid_t));
7960Sstevel@tonic-gate 
7970Sstevel@tonic-gate 	if (error)
7980Sstevel@tonic-gate 		return (set_errno(error));
7990Sstevel@tonic-gate 	else
8000Sstevel@tonic-gate 		return (0);
8010Sstevel@tonic-gate }
8020Sstevel@tonic-gate 
8030Sstevel@tonic-gate static int
pset_setattr(psetid_t pset,uint_t attr)8040Sstevel@tonic-gate pset_setattr(psetid_t pset, uint_t attr)
8050Sstevel@tonic-gate {
8060Sstevel@tonic-gate 	int error;
8070Sstevel@tonic-gate 
8080Sstevel@tonic-gate 	if (secpolicy_pset(CRED()) != 0)
8090Sstevel@tonic-gate 		return (set_errno(EPERM));
8100Sstevel@tonic-gate 	pool_lock();
8110Sstevel@tonic-gate 	if (pool_state == POOL_ENABLED) {
8120Sstevel@tonic-gate 		pool_unlock();
8130Sstevel@tonic-gate 		return (set_errno(ENOTSUP));
8140Sstevel@tonic-gate 	}
8150Sstevel@tonic-gate 	if (pset == PS_QUERY || PSET_BADATTR(attr)) {
8160Sstevel@tonic-gate 		pool_unlock();
8170Sstevel@tonic-gate 		return (set_errno(EINVAL));
8180Sstevel@tonic-gate 	}
8190Sstevel@tonic-gate 	if ((error = cpupart_setattr(pset, attr)) != 0) {
8200Sstevel@tonic-gate 		pool_unlock();
8210Sstevel@tonic-gate 		return (set_errno(error));
8220Sstevel@tonic-gate 	}
8230Sstevel@tonic-gate 	pool_unlock();
8240Sstevel@tonic-gate 	return (0);
8250Sstevel@tonic-gate }
8260Sstevel@tonic-gate 
8270Sstevel@tonic-gate static int
pset_getattr(psetid_t pset,uint_t * attrp)8280Sstevel@tonic-gate pset_getattr(psetid_t pset, uint_t *attrp)
8290Sstevel@tonic-gate {
8300Sstevel@tonic-gate 	int error = 0;
8310Sstevel@tonic-gate 	uint_t attr;
8320Sstevel@tonic-gate 
8330Sstevel@tonic-gate 	if (pset == PS_QUERY)
8340Sstevel@tonic-gate 		return (set_errno(EINVAL));
8350Sstevel@tonic-gate 	if ((error = cpupart_getattr(pset, &attr)) != 0)
8360Sstevel@tonic-gate 		return (set_errno(error));
8370Sstevel@tonic-gate 	if (copyout(&attr, attrp, sizeof (uint_t)) != 0)
8380Sstevel@tonic-gate 		return (set_errno(EFAULT));
8390Sstevel@tonic-gate 	return (0);
8400Sstevel@tonic-gate }
8410Sstevel@tonic-gate 
8420Sstevel@tonic-gate static int
pset(int subcode,long arg1,long arg2,long arg3,long arg4)8430Sstevel@tonic-gate pset(int subcode, long arg1, long arg2, long arg3, long arg4)
8440Sstevel@tonic-gate {
8450Sstevel@tonic-gate 	switch (subcode) {
8460Sstevel@tonic-gate 	case PSET_CREATE:
8470Sstevel@tonic-gate 		return (pset_create((psetid_t *)arg1));
8480Sstevel@tonic-gate 	case PSET_DESTROY:
8490Sstevel@tonic-gate 		return (pset_destroy((psetid_t)arg1));
8500Sstevel@tonic-gate 	case PSET_ASSIGN:
8510Sstevel@tonic-gate 		return (pset_assign((psetid_t)arg1,
8520Sstevel@tonic-gate 		    (processorid_t)arg2, (psetid_t *)arg3, 0));
8530Sstevel@tonic-gate 	case PSET_INFO:
8540Sstevel@tonic-gate 		return (pset_info((psetid_t)arg1, (int *)arg2,
8550Sstevel@tonic-gate 		    (uint_t *)arg3, (processorid_t *)arg4));
8560Sstevel@tonic-gate 	case PSET_BIND:
8570Sstevel@tonic-gate 		return (pset_bind((psetid_t)arg1, (idtype_t)arg2,
8580Sstevel@tonic-gate 		    (id_t)arg3, (psetid_t *)arg4));
85910089SSurya.Prakki@Sun.COM 	case PSET_BIND_LWP:
86010089SSurya.Prakki@Sun.COM 		return (pset_bind_lwp((psetid_t)arg1, (id_t)arg2,
86110089SSurya.Prakki@Sun.COM 		    (pid_t)arg3, (psetid_t *)arg4));
8620Sstevel@tonic-gate 	case PSET_GETLOADAVG:
8630Sstevel@tonic-gate 		return (pset_getloadavg((psetid_t)arg1, (int *)arg2,
8640Sstevel@tonic-gate 		    (int)arg3));
8650Sstevel@tonic-gate 	case PSET_LIST:
8660Sstevel@tonic-gate 		return (pset_list((psetid_t *)arg1, (uint_t *)arg2));
8670Sstevel@tonic-gate 	case PSET_SETATTR:
8680Sstevel@tonic-gate 		return (pset_setattr((psetid_t)arg1, (uint_t)arg2));
8690Sstevel@tonic-gate 	case PSET_GETATTR:
8700Sstevel@tonic-gate 		return (pset_getattr((psetid_t)arg1, (uint_t *)arg2));
8710Sstevel@tonic-gate 	case PSET_ASSIGN_FORCED:
8720Sstevel@tonic-gate 		return (pset_assign((psetid_t)arg1,
8730Sstevel@tonic-gate 		    (processorid_t)arg2, (psetid_t *)arg3, 1));
8740Sstevel@tonic-gate 	default:
8750Sstevel@tonic-gate 		return (set_errno(EINVAL));
8760Sstevel@tonic-gate 	}
8770Sstevel@tonic-gate }
878