xref: /titanic_52/usr/src/uts/common/syscall/pset.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*7c478bd9Sstevel@tonic-gate 
29*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
30*7c478bd9Sstevel@tonic-gate #include <sys/systm.h>
31*7c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
32*7c478bd9Sstevel@tonic-gate #include <sys/cpuvar.h>
33*7c478bd9Sstevel@tonic-gate #include <sys/thread.h>
34*7c478bd9Sstevel@tonic-gate #include <sys/disp.h>
35*7c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
36*7c478bd9Sstevel@tonic-gate #include <sys/debug.h>
37*7c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
38*7c478bd9Sstevel@tonic-gate #include <sys/cpupart.h>
39*7c478bd9Sstevel@tonic-gate #include <sys/pset.h>
40*7c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
41*7c478bd9Sstevel@tonic-gate #include <sys/syscall.h>
42*7c478bd9Sstevel@tonic-gate #include <sys/task.h>
43*7c478bd9Sstevel@tonic-gate #include <sys/loadavg.h>
44*7c478bd9Sstevel@tonic-gate #include <sys/fss.h>
45*7c478bd9Sstevel@tonic-gate #include <sys/pool.h>
46*7c478bd9Sstevel@tonic-gate #include <sys/pool_pset.h>
47*7c478bd9Sstevel@tonic-gate #include <sys/policy.h>
48*7c478bd9Sstevel@tonic-gate #include <sys/zone.h>
49*7c478bd9Sstevel@tonic-gate #include <sys/contract/process_impl.h>
50*7c478bd9Sstevel@tonic-gate 
51*7c478bd9Sstevel@tonic-gate static int	pset(int, long, long, long, long);
52*7c478bd9Sstevel@tonic-gate 
53*7c478bd9Sstevel@tonic-gate static struct sysent pset_sysent = {
54*7c478bd9Sstevel@tonic-gate 	5,
55*7c478bd9Sstevel@tonic-gate 	SE_ARGC | SE_NOUNLOAD,
56*7c478bd9Sstevel@tonic-gate 	(int (*)())pset,
57*7c478bd9Sstevel@tonic-gate };
58*7c478bd9Sstevel@tonic-gate 
59*7c478bd9Sstevel@tonic-gate static struct modlsys modlsys = {
60*7c478bd9Sstevel@tonic-gate 	&mod_syscallops, "processor sets", &pset_sysent
61*7c478bd9Sstevel@tonic-gate };
62*7c478bd9Sstevel@tonic-gate 
63*7c478bd9Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
64*7c478bd9Sstevel@tonic-gate static struct modlsys modlsys32 = {
65*7c478bd9Sstevel@tonic-gate 	&mod_syscallops32, "32-bit pset(2) syscall", &pset_sysent
66*7c478bd9Sstevel@tonic-gate };
67*7c478bd9Sstevel@tonic-gate #endif
68*7c478bd9Sstevel@tonic-gate 
69*7c478bd9Sstevel@tonic-gate static struct modlinkage modlinkage = {
70*7c478bd9Sstevel@tonic-gate 	MODREV_1,
71*7c478bd9Sstevel@tonic-gate 	&modlsys,
72*7c478bd9Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
73*7c478bd9Sstevel@tonic-gate 	&modlsys32,
74*7c478bd9Sstevel@tonic-gate #endif
75*7c478bd9Sstevel@tonic-gate 	NULL
76*7c478bd9Sstevel@tonic-gate };
77*7c478bd9Sstevel@tonic-gate 
78*7c478bd9Sstevel@tonic-gate #define	PSET_BADATTR(attr)	((~PSET_NOESCAPE) & (attr))
79*7c478bd9Sstevel@tonic-gate 
80*7c478bd9Sstevel@tonic-gate int
81*7c478bd9Sstevel@tonic-gate _init(void)
82*7c478bd9Sstevel@tonic-gate {
83*7c478bd9Sstevel@tonic-gate 	return (mod_install(&modlinkage));
84*7c478bd9Sstevel@tonic-gate }
85*7c478bd9Sstevel@tonic-gate 
86*7c478bd9Sstevel@tonic-gate int
87*7c478bd9Sstevel@tonic-gate _info(struct modinfo *modinfop)
88*7c478bd9Sstevel@tonic-gate {
89*7c478bd9Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
90*7c478bd9Sstevel@tonic-gate }
91*7c478bd9Sstevel@tonic-gate 
92*7c478bd9Sstevel@tonic-gate static int
93*7c478bd9Sstevel@tonic-gate pset_create(psetid_t *psetp)
94*7c478bd9Sstevel@tonic-gate {
95*7c478bd9Sstevel@tonic-gate 	psetid_t newpset;
96*7c478bd9Sstevel@tonic-gate 	int error;
97*7c478bd9Sstevel@tonic-gate 
98*7c478bd9Sstevel@tonic-gate 	if (secpolicy_pset(CRED()) != 0)
99*7c478bd9Sstevel@tonic-gate 		return (set_errno(EPERM));
100*7c478bd9Sstevel@tonic-gate 
101*7c478bd9Sstevel@tonic-gate 	pool_lock();
102*7c478bd9Sstevel@tonic-gate 	if (pool_state == POOL_ENABLED) {
103*7c478bd9Sstevel@tonic-gate 		pool_unlock();
104*7c478bd9Sstevel@tonic-gate 		return (set_errno(ENOTSUP));
105*7c478bd9Sstevel@tonic-gate 	}
106*7c478bd9Sstevel@tonic-gate 	error = cpupart_create(&newpset);
107*7c478bd9Sstevel@tonic-gate 	if (error) {
108*7c478bd9Sstevel@tonic-gate 		pool_unlock();
109*7c478bd9Sstevel@tonic-gate 		return (set_errno(error));
110*7c478bd9Sstevel@tonic-gate 	}
111*7c478bd9Sstevel@tonic-gate 	if (copyout(&newpset, psetp, sizeof (psetid_t)) != 0) {
112*7c478bd9Sstevel@tonic-gate 		(void) cpupart_destroy(newpset);
113*7c478bd9Sstevel@tonic-gate 		pool_unlock();
114*7c478bd9Sstevel@tonic-gate 		return (set_errno(EFAULT));
115*7c478bd9Sstevel@tonic-gate 	}
116*7c478bd9Sstevel@tonic-gate 	pool_unlock();
117*7c478bd9Sstevel@tonic-gate 	return (error);
118*7c478bd9Sstevel@tonic-gate }
119*7c478bd9Sstevel@tonic-gate 
120*7c478bd9Sstevel@tonic-gate static int
121*7c478bd9Sstevel@tonic-gate pset_destroy(psetid_t pset)
122*7c478bd9Sstevel@tonic-gate {
123*7c478bd9Sstevel@tonic-gate 	int error;
124*7c478bd9Sstevel@tonic-gate 
125*7c478bd9Sstevel@tonic-gate 	if (secpolicy_pset(CRED()) != 0)
126*7c478bd9Sstevel@tonic-gate 		return (set_errno(EPERM));
127*7c478bd9Sstevel@tonic-gate 
128*7c478bd9Sstevel@tonic-gate 	pool_lock();
129*7c478bd9Sstevel@tonic-gate 	if (pool_state == POOL_ENABLED) {
130*7c478bd9Sstevel@tonic-gate 		pool_unlock();
131*7c478bd9Sstevel@tonic-gate 		return (set_errno(ENOTSUP));
132*7c478bd9Sstevel@tonic-gate 	}
133*7c478bd9Sstevel@tonic-gate 	error = cpupart_destroy(pset);
134*7c478bd9Sstevel@tonic-gate 	pool_unlock();
135*7c478bd9Sstevel@tonic-gate 	if (error)
136*7c478bd9Sstevel@tonic-gate 		return (set_errno(error));
137*7c478bd9Sstevel@tonic-gate 	else
138*7c478bd9Sstevel@tonic-gate 		return (0);
139*7c478bd9Sstevel@tonic-gate }
140*7c478bd9Sstevel@tonic-gate 
141*7c478bd9Sstevel@tonic-gate static int
142*7c478bd9Sstevel@tonic-gate pset_assign(psetid_t pset, processorid_t cpuid, psetid_t *opset, int forced)
143*7c478bd9Sstevel@tonic-gate {
144*7c478bd9Sstevel@tonic-gate 	psetid_t oldpset;
145*7c478bd9Sstevel@tonic-gate 	int	error = 0;
146*7c478bd9Sstevel@tonic-gate 	cpu_t	*cp;
147*7c478bd9Sstevel@tonic-gate 
148*7c478bd9Sstevel@tonic-gate 	if (pset != PS_QUERY && secpolicy_pset(CRED()) != 0)
149*7c478bd9Sstevel@tonic-gate 		return (set_errno(EPERM));
150*7c478bd9Sstevel@tonic-gate 
151*7c478bd9Sstevel@tonic-gate 	pool_lock();
152*7c478bd9Sstevel@tonic-gate 	if (pset != PS_QUERY && pool_state == POOL_ENABLED) {
153*7c478bd9Sstevel@tonic-gate 		pool_unlock();
154*7c478bd9Sstevel@tonic-gate 		return (set_errno(ENOTSUP));
155*7c478bd9Sstevel@tonic-gate 	}
156*7c478bd9Sstevel@tonic-gate 
157*7c478bd9Sstevel@tonic-gate 	mutex_enter(&cpu_lock);
158*7c478bd9Sstevel@tonic-gate 	if ((cp = cpu_get(cpuid)) == NULL) {
159*7c478bd9Sstevel@tonic-gate 		mutex_exit(&cpu_lock);
160*7c478bd9Sstevel@tonic-gate 		pool_unlock();
161*7c478bd9Sstevel@tonic-gate 		return (set_errno(EINVAL));
162*7c478bd9Sstevel@tonic-gate 	}
163*7c478bd9Sstevel@tonic-gate 
164*7c478bd9Sstevel@tonic-gate 	oldpset = cpupart_query_cpu(cp);
165*7c478bd9Sstevel@tonic-gate 
166*7c478bd9Sstevel@tonic-gate 	if (pset != PS_QUERY)
167*7c478bd9Sstevel@tonic-gate 		error = cpupart_attach_cpu(pset, cp, forced);
168*7c478bd9Sstevel@tonic-gate 	mutex_exit(&cpu_lock);
169*7c478bd9Sstevel@tonic-gate 	pool_unlock();
170*7c478bd9Sstevel@tonic-gate 
171*7c478bd9Sstevel@tonic-gate 	if (error)
172*7c478bd9Sstevel@tonic-gate 		return (set_errno(error));
173*7c478bd9Sstevel@tonic-gate 
174*7c478bd9Sstevel@tonic-gate 	if (opset != NULL)
175*7c478bd9Sstevel@tonic-gate 		if (copyout(&oldpset, opset, sizeof (psetid_t)) != 0)
176*7c478bd9Sstevel@tonic-gate 			return (set_errno(EFAULT));
177*7c478bd9Sstevel@tonic-gate 
178*7c478bd9Sstevel@tonic-gate 	return (0);
179*7c478bd9Sstevel@tonic-gate }
180*7c478bd9Sstevel@tonic-gate 
181*7c478bd9Sstevel@tonic-gate static int
182*7c478bd9Sstevel@tonic-gate pset_info(psetid_t pset, int *typep, uint_t *numcpusp,
183*7c478bd9Sstevel@tonic-gate     processorid_t *cpulistp)
184*7c478bd9Sstevel@tonic-gate {
185*7c478bd9Sstevel@tonic-gate 	int pset_type;
186*7c478bd9Sstevel@tonic-gate 	uint_t user_ncpus = 0, real_ncpus, copy_ncpus;
187*7c478bd9Sstevel@tonic-gate 	processorid_t *pset_cpus = NULL;
188*7c478bd9Sstevel@tonic-gate 	int error = 0;
189*7c478bd9Sstevel@tonic-gate 
190*7c478bd9Sstevel@tonic-gate 	if (numcpusp != NULL) {
191*7c478bd9Sstevel@tonic-gate 		if (copyin(numcpusp, &user_ncpus, sizeof (uint_t)) != 0)
192*7c478bd9Sstevel@tonic-gate 			return (set_errno(EFAULT));
193*7c478bd9Sstevel@tonic-gate 	}
194*7c478bd9Sstevel@tonic-gate 
195*7c478bd9Sstevel@tonic-gate 	if (user_ncpus > max_ncpus)	/* sanity check */
196*7c478bd9Sstevel@tonic-gate 		user_ncpus = max_ncpus;
197*7c478bd9Sstevel@tonic-gate 	if (user_ncpus != 0 && cpulistp != NULL)
198*7c478bd9Sstevel@tonic-gate 		pset_cpus = kmem_alloc(sizeof (processorid_t) * user_ncpus,
199*7c478bd9Sstevel@tonic-gate 		    KM_SLEEP);
200*7c478bd9Sstevel@tonic-gate 
201*7c478bd9Sstevel@tonic-gate 	real_ncpus = user_ncpus;
202*7c478bd9Sstevel@tonic-gate 	if ((error = cpupart_get_cpus(&pset, pset_cpus, &real_ncpus)) != 0)
203*7c478bd9Sstevel@tonic-gate 		goto out;
204*7c478bd9Sstevel@tonic-gate 
205*7c478bd9Sstevel@tonic-gate 	/*
206*7c478bd9Sstevel@tonic-gate 	 * Now copyout the information about this processor set.
207*7c478bd9Sstevel@tonic-gate 	 */
208*7c478bd9Sstevel@tonic-gate 
209*7c478bd9Sstevel@tonic-gate 	/*
210*7c478bd9Sstevel@tonic-gate 	 * Get number of cpus to copy back.  If the user didn't pass in
211*7c478bd9Sstevel@tonic-gate 	 * a big enough buffer, only copy back as many cpus as fits in
212*7c478bd9Sstevel@tonic-gate 	 * the buffer but copy back the real number of cpus.
213*7c478bd9Sstevel@tonic-gate 	 */
214*7c478bd9Sstevel@tonic-gate 
215*7c478bd9Sstevel@tonic-gate 	if (user_ncpus != 0 && cpulistp != NULL) {
216*7c478bd9Sstevel@tonic-gate 		copy_ncpus = MIN(real_ncpus, user_ncpus);
217*7c478bd9Sstevel@tonic-gate 		if (copyout(pset_cpus, cpulistp,
218*7c478bd9Sstevel@tonic-gate 		    sizeof (processorid_t) * copy_ncpus) != 0) {
219*7c478bd9Sstevel@tonic-gate 			error = EFAULT;
220*7c478bd9Sstevel@tonic-gate 			goto out;
221*7c478bd9Sstevel@tonic-gate 		}
222*7c478bd9Sstevel@tonic-gate 	}
223*7c478bd9Sstevel@tonic-gate 	if (pset_cpus != NULL)
224*7c478bd9Sstevel@tonic-gate 		kmem_free(pset_cpus, sizeof (processorid_t) * user_ncpus);
225*7c478bd9Sstevel@tonic-gate 	if (typep != NULL) {
226*7c478bd9Sstevel@tonic-gate 		if (pset == PS_NONE)
227*7c478bd9Sstevel@tonic-gate 			pset_type = PS_NONE;
228*7c478bd9Sstevel@tonic-gate 		else
229*7c478bd9Sstevel@tonic-gate 			pset_type = PS_PRIVATE;
230*7c478bd9Sstevel@tonic-gate 		if (copyout(&pset_type, typep, sizeof (int)) != 0)
231*7c478bd9Sstevel@tonic-gate 			return (set_errno(EFAULT));
232*7c478bd9Sstevel@tonic-gate 	}
233*7c478bd9Sstevel@tonic-gate 	if (numcpusp != NULL)
234*7c478bd9Sstevel@tonic-gate 		if (copyout(&real_ncpus, numcpusp, sizeof (uint_t)) != 0)
235*7c478bd9Sstevel@tonic-gate 			return (set_errno(EFAULT));
236*7c478bd9Sstevel@tonic-gate 	return (0);
237*7c478bd9Sstevel@tonic-gate 
238*7c478bd9Sstevel@tonic-gate out:
239*7c478bd9Sstevel@tonic-gate 	if (pset_cpus != NULL)
240*7c478bd9Sstevel@tonic-gate 		kmem_free(pset_cpus, sizeof (processorid_t) * user_ncpus);
241*7c478bd9Sstevel@tonic-gate 	return (set_errno(error));
242*7c478bd9Sstevel@tonic-gate }
243*7c478bd9Sstevel@tonic-gate 
244*7c478bd9Sstevel@tonic-gate static int
245*7c478bd9Sstevel@tonic-gate pset_bind_thread(kthread_t *tp, psetid_t pset, psetid_t *oldpset, void *projbuf,
246*7c478bd9Sstevel@tonic-gate     void *zonebuf)
247*7c478bd9Sstevel@tonic-gate {
248*7c478bd9Sstevel@tonic-gate 	int error = 0;
249*7c478bd9Sstevel@tonic-gate 
250*7c478bd9Sstevel@tonic-gate 	ASSERT(pool_lock_held());
251*7c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cpu_lock));
252*7c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&ttoproc(tp)->p_lock));
253*7c478bd9Sstevel@tonic-gate 
254*7c478bd9Sstevel@tonic-gate 	*oldpset = tp->t_bind_pset;
255*7c478bd9Sstevel@tonic-gate 	if (pset != PS_QUERY) {
256*7c478bd9Sstevel@tonic-gate 		/*
257*7c478bd9Sstevel@tonic-gate 		 * Must have the same UID as the target process or
258*7c478bd9Sstevel@tonic-gate 		 * have PRIV_PROC_OWNER privilege.
259*7c478bd9Sstevel@tonic-gate 		 */
260*7c478bd9Sstevel@tonic-gate 		if (!hasprocperm(tp->t_cred, CRED()))
261*7c478bd9Sstevel@tonic-gate 			return (EPERM);
262*7c478bd9Sstevel@tonic-gate 		/*
263*7c478bd9Sstevel@tonic-gate 		 * Unbinding of an unbound thread should always succeed.
264*7c478bd9Sstevel@tonic-gate 		 */
265*7c478bd9Sstevel@tonic-gate 		if (*oldpset == PS_NONE && pset == PS_NONE)
266*7c478bd9Sstevel@tonic-gate 			return (0);
267*7c478bd9Sstevel@tonic-gate 		/*
268*7c478bd9Sstevel@tonic-gate 		 * Only privileged processes can move threads from psets with
269*7c478bd9Sstevel@tonic-gate 		 * PSET_NOESCAPE attribute.
270*7c478bd9Sstevel@tonic-gate 		 */
271*7c478bd9Sstevel@tonic-gate 		if ((tp->t_cpupart->cp_attr & PSET_NOESCAPE) &&
272*7c478bd9Sstevel@tonic-gate 		    secpolicy_pset(CRED()) != 0)
273*7c478bd9Sstevel@tonic-gate 			return (EPERM);
274*7c478bd9Sstevel@tonic-gate 		if ((error = cpupart_bind_thread(tp, pset, 0,
275*7c478bd9Sstevel@tonic-gate 		    projbuf, zonebuf)) == 0)
276*7c478bd9Sstevel@tonic-gate 			tp->t_bind_pset = pset;
277*7c478bd9Sstevel@tonic-gate 	}
278*7c478bd9Sstevel@tonic-gate 	return (error);
279*7c478bd9Sstevel@tonic-gate }
280*7c478bd9Sstevel@tonic-gate 
281*7c478bd9Sstevel@tonic-gate static int
282*7c478bd9Sstevel@tonic-gate pset_bind_process(proc_t *pp, psetid_t pset, psetid_t *oldpset, void *projbuf,
283*7c478bd9Sstevel@tonic-gate     void *zonebuf)
284*7c478bd9Sstevel@tonic-gate {
285*7c478bd9Sstevel@tonic-gate 	int error = 0;
286*7c478bd9Sstevel@tonic-gate 	kthread_t *tp;
287*7c478bd9Sstevel@tonic-gate 
288*7c478bd9Sstevel@tonic-gate 	/* skip kernel processes */
289*7c478bd9Sstevel@tonic-gate 	if (pset != PS_QUERY && pp->p_flag & SSYS) {
290*7c478bd9Sstevel@tonic-gate 		*oldpset = PS_NONE;
291*7c478bd9Sstevel@tonic-gate 		return (0);
292*7c478bd9Sstevel@tonic-gate 	}
293*7c478bd9Sstevel@tonic-gate 
294*7c478bd9Sstevel@tonic-gate 	mutex_enter(&pp->p_lock);
295*7c478bd9Sstevel@tonic-gate 	tp = pp->p_tlist;
296*7c478bd9Sstevel@tonic-gate 	if (tp != NULL) {
297*7c478bd9Sstevel@tonic-gate 		do {
298*7c478bd9Sstevel@tonic-gate 			int rval;
299*7c478bd9Sstevel@tonic-gate 
300*7c478bd9Sstevel@tonic-gate 			rval = pset_bind_thread(tp, pset, oldpset, projbuf,
301*7c478bd9Sstevel@tonic-gate 			    zonebuf);
302*7c478bd9Sstevel@tonic-gate 			if (error == 0)
303*7c478bd9Sstevel@tonic-gate 				error = rval;
304*7c478bd9Sstevel@tonic-gate 		} while ((tp = tp->t_forw) != pp->p_tlist);
305*7c478bd9Sstevel@tonic-gate 	} else
306*7c478bd9Sstevel@tonic-gate 		error = ESRCH;
307*7c478bd9Sstevel@tonic-gate 	mutex_exit(&pp->p_lock);
308*7c478bd9Sstevel@tonic-gate 
309*7c478bd9Sstevel@tonic-gate 	return (error);
310*7c478bd9Sstevel@tonic-gate }
311*7c478bd9Sstevel@tonic-gate 
312*7c478bd9Sstevel@tonic-gate static int
313*7c478bd9Sstevel@tonic-gate pset_bind_task(task_t *tk, psetid_t pset, psetid_t *oldpset, void *projbuf,
314*7c478bd9Sstevel@tonic-gate     void *zonebuf)
315*7c478bd9Sstevel@tonic-gate {
316*7c478bd9Sstevel@tonic-gate 	int error = 0;
317*7c478bd9Sstevel@tonic-gate 	proc_t *pp;
318*7c478bd9Sstevel@tonic-gate 
319*7c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&pidlock));
320*7c478bd9Sstevel@tonic-gate 
321*7c478bd9Sstevel@tonic-gate 	if ((pp = tk->tk_memb_list) == NULL) {
322*7c478bd9Sstevel@tonic-gate 		return (ESRCH);
323*7c478bd9Sstevel@tonic-gate 	}
324*7c478bd9Sstevel@tonic-gate 
325*7c478bd9Sstevel@tonic-gate 	do {
326*7c478bd9Sstevel@tonic-gate 		int rval;
327*7c478bd9Sstevel@tonic-gate 
328*7c478bd9Sstevel@tonic-gate 		rval = pset_bind_process(pp, pset, oldpset, projbuf, zonebuf);
329*7c478bd9Sstevel@tonic-gate 		if (error == 0)
330*7c478bd9Sstevel@tonic-gate 			error = rval;
331*7c478bd9Sstevel@tonic-gate 	} while ((pp = pp->p_tasknext) != tk->tk_memb_list);
332*7c478bd9Sstevel@tonic-gate 
333*7c478bd9Sstevel@tonic-gate 	return (error);
334*7c478bd9Sstevel@tonic-gate }
335*7c478bd9Sstevel@tonic-gate 
336*7c478bd9Sstevel@tonic-gate static int
337*7c478bd9Sstevel@tonic-gate pset_bind_project(kproject_t *kpj, psetid_t pset, psetid_t *oldpset,
338*7c478bd9Sstevel@tonic-gate     void *projbuf, void *zonebuf)
339*7c478bd9Sstevel@tonic-gate {
340*7c478bd9Sstevel@tonic-gate 	int error = 0;
341*7c478bd9Sstevel@tonic-gate 	proc_t *pp;
342*7c478bd9Sstevel@tonic-gate 
343*7c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&pidlock));
344*7c478bd9Sstevel@tonic-gate 
345*7c478bd9Sstevel@tonic-gate 	for (pp = practive; pp != NULL; pp = pp->p_next) {
346*7c478bd9Sstevel@tonic-gate 		if (pp->p_tlist == NULL)
347*7c478bd9Sstevel@tonic-gate 			continue;
348*7c478bd9Sstevel@tonic-gate 		if (pp->p_task->tk_proj == kpj) {
349*7c478bd9Sstevel@tonic-gate 			int rval;
350*7c478bd9Sstevel@tonic-gate 
351*7c478bd9Sstevel@tonic-gate 			rval = pset_bind_process(pp, pset, oldpset, projbuf,
352*7c478bd9Sstevel@tonic-gate 			    zonebuf);
353*7c478bd9Sstevel@tonic-gate 			if (error == 0)
354*7c478bd9Sstevel@tonic-gate 				error = rval;
355*7c478bd9Sstevel@tonic-gate 		}
356*7c478bd9Sstevel@tonic-gate 	}
357*7c478bd9Sstevel@tonic-gate 
358*7c478bd9Sstevel@tonic-gate 	return (error);
359*7c478bd9Sstevel@tonic-gate }
360*7c478bd9Sstevel@tonic-gate 
361*7c478bd9Sstevel@tonic-gate static int
362*7c478bd9Sstevel@tonic-gate pset_bind_zone(zone_t *zptr, psetid_t pset, psetid_t *oldpset, void *projbuf,
363*7c478bd9Sstevel@tonic-gate     void *zonebuf)
364*7c478bd9Sstevel@tonic-gate {
365*7c478bd9Sstevel@tonic-gate 	int error = 0;
366*7c478bd9Sstevel@tonic-gate 	proc_t *pp;
367*7c478bd9Sstevel@tonic-gate 
368*7c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&pidlock));
369*7c478bd9Sstevel@tonic-gate 
370*7c478bd9Sstevel@tonic-gate 	for (pp = practive; pp != NULL; pp = pp->p_next) {
371*7c478bd9Sstevel@tonic-gate 		if (pp->p_zone == zptr) {
372*7c478bd9Sstevel@tonic-gate 			int rval;
373*7c478bd9Sstevel@tonic-gate 
374*7c478bd9Sstevel@tonic-gate 			rval = pset_bind_process(pp, pset, oldpset, projbuf,
375*7c478bd9Sstevel@tonic-gate 			    zonebuf);
376*7c478bd9Sstevel@tonic-gate 			if (error == 0)
377*7c478bd9Sstevel@tonic-gate 				error = rval;
378*7c478bd9Sstevel@tonic-gate 		}
379*7c478bd9Sstevel@tonic-gate 	}
380*7c478bd9Sstevel@tonic-gate 
381*7c478bd9Sstevel@tonic-gate 	return (error);
382*7c478bd9Sstevel@tonic-gate }
383*7c478bd9Sstevel@tonic-gate 
384*7c478bd9Sstevel@tonic-gate /*
385*7c478bd9Sstevel@tonic-gate  * Unbind all threads from the specified processor set, or from all
386*7c478bd9Sstevel@tonic-gate  * processor sets.
387*7c478bd9Sstevel@tonic-gate  */
388*7c478bd9Sstevel@tonic-gate static int
389*7c478bd9Sstevel@tonic-gate pset_unbind(psetid_t pset, void *projbuf, void *zonebuf, idtype_t idtype)
390*7c478bd9Sstevel@tonic-gate {
391*7c478bd9Sstevel@tonic-gate 	psetid_t olbind;
392*7c478bd9Sstevel@tonic-gate 	kthread_t *tp;
393*7c478bd9Sstevel@tonic-gate 	int error = 0;
394*7c478bd9Sstevel@tonic-gate 	int rval;
395*7c478bd9Sstevel@tonic-gate 	proc_t *pp;
396*7c478bd9Sstevel@tonic-gate 
397*7c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cpu_lock));
398*7c478bd9Sstevel@tonic-gate 
399*7c478bd9Sstevel@tonic-gate 	if (idtype == P_PSETID && cpupart_find(pset) == NULL)
400*7c478bd9Sstevel@tonic-gate 		return (EINVAL);
401*7c478bd9Sstevel@tonic-gate 
402*7c478bd9Sstevel@tonic-gate 	mutex_enter(&pidlock);
403*7c478bd9Sstevel@tonic-gate 	for (pp = practive; pp != NULL; pp = pp->p_next) {
404*7c478bd9Sstevel@tonic-gate 		mutex_enter(&pp->p_lock);
405*7c478bd9Sstevel@tonic-gate 		tp = pp->p_tlist;
406*7c478bd9Sstevel@tonic-gate 		/*
407*7c478bd9Sstevel@tonic-gate 		 * Skip zombies and kernel processes, and processes in
408*7c478bd9Sstevel@tonic-gate 		 * other zones, if called from a non-global zone.
409*7c478bd9Sstevel@tonic-gate 		 */
410*7c478bd9Sstevel@tonic-gate 		if (tp == NULL || (pp->p_flag & SSYS) ||
411*7c478bd9Sstevel@tonic-gate 		    !HASZONEACCESS(curproc, pp->p_zone->zone_id)) {
412*7c478bd9Sstevel@tonic-gate 			mutex_exit(&pp->p_lock);
413*7c478bd9Sstevel@tonic-gate 			continue;
414*7c478bd9Sstevel@tonic-gate 		}
415*7c478bd9Sstevel@tonic-gate 		do {
416*7c478bd9Sstevel@tonic-gate 			if ((idtype == P_PSETID && tp->t_bind_pset != pset) ||
417*7c478bd9Sstevel@tonic-gate 			    (idtype == P_ALL && tp->t_bind_pset == PS_NONE))
418*7c478bd9Sstevel@tonic-gate 				continue;
419*7c478bd9Sstevel@tonic-gate 			rval = pset_bind_thread(tp, PS_NONE, &olbind,
420*7c478bd9Sstevel@tonic-gate 			    projbuf, zonebuf);
421*7c478bd9Sstevel@tonic-gate 			if (error == 0)
422*7c478bd9Sstevel@tonic-gate 				error = rval;
423*7c478bd9Sstevel@tonic-gate 		} while ((tp = tp->t_forw) != pp->p_tlist);
424*7c478bd9Sstevel@tonic-gate 		mutex_exit(&pp->p_lock);
425*7c478bd9Sstevel@tonic-gate 	}
426*7c478bd9Sstevel@tonic-gate 	mutex_exit(&pidlock);
427*7c478bd9Sstevel@tonic-gate 	return (error);
428*7c478bd9Sstevel@tonic-gate }
429*7c478bd9Sstevel@tonic-gate 
430*7c478bd9Sstevel@tonic-gate static int
431*7c478bd9Sstevel@tonic-gate pset_bind_contract(cont_process_t *ctp, psetid_t pset, psetid_t *oldpset,
432*7c478bd9Sstevel@tonic-gate     void *projbuf, void *zonebuf)
433*7c478bd9Sstevel@tonic-gate {
434*7c478bd9Sstevel@tonic-gate 	int error = 0;
435*7c478bd9Sstevel@tonic-gate 	proc_t *pp;
436*7c478bd9Sstevel@tonic-gate 
437*7c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&pidlock));
438*7c478bd9Sstevel@tonic-gate 
439*7c478bd9Sstevel@tonic-gate 	for (pp = practive; pp != NULL; pp = pp->p_next) {
440*7c478bd9Sstevel@tonic-gate 		if (pp->p_ct_process == ctp) {
441*7c478bd9Sstevel@tonic-gate 			int rval;
442*7c478bd9Sstevel@tonic-gate 
443*7c478bd9Sstevel@tonic-gate 			rval = pset_bind_process(pp, pset, oldpset, projbuf,
444*7c478bd9Sstevel@tonic-gate 			    zonebuf);
445*7c478bd9Sstevel@tonic-gate 			if (error == 0)
446*7c478bd9Sstevel@tonic-gate 				error = rval;
447*7c478bd9Sstevel@tonic-gate 		}
448*7c478bd9Sstevel@tonic-gate 	}
449*7c478bd9Sstevel@tonic-gate 
450*7c478bd9Sstevel@tonic-gate 	return (error);
451*7c478bd9Sstevel@tonic-gate }
452*7c478bd9Sstevel@tonic-gate 
453*7c478bd9Sstevel@tonic-gate static int
454*7c478bd9Sstevel@tonic-gate pset_bind(psetid_t pset, idtype_t idtype, id_t id, psetid_t *opset)
455*7c478bd9Sstevel@tonic-gate {
456*7c478bd9Sstevel@tonic-gate 	kthread_t	*tp;
457*7c478bd9Sstevel@tonic-gate 	proc_t		*pp;
458*7c478bd9Sstevel@tonic-gate 	task_t		*tk;
459*7c478bd9Sstevel@tonic-gate 	kproject_t	*kpj;
460*7c478bd9Sstevel@tonic-gate 	contract_t	*ct;
461*7c478bd9Sstevel@tonic-gate 	zone_t		*zptr;
462*7c478bd9Sstevel@tonic-gate 	psetid_t	oldpset;
463*7c478bd9Sstevel@tonic-gate 	int		error = 0;
464*7c478bd9Sstevel@tonic-gate 	void		*projbuf, *zonebuf;
465*7c478bd9Sstevel@tonic-gate 
466*7c478bd9Sstevel@tonic-gate 	pool_lock();
467*7c478bd9Sstevel@tonic-gate 	if (pset != PS_QUERY) {
468*7c478bd9Sstevel@tonic-gate 		/*
469*7c478bd9Sstevel@tonic-gate 		 * Check if the set actually exists before checking
470*7c478bd9Sstevel@tonic-gate 		 * permissions.  This is the historical error
471*7c478bd9Sstevel@tonic-gate 		 * precedence.  Note that if pset was PS_MYID, the
472*7c478bd9Sstevel@tonic-gate 		 * cpupart_get_cpus call will change it to the
473*7c478bd9Sstevel@tonic-gate 		 * processor set id of the caller (or PS_NONE if the
474*7c478bd9Sstevel@tonic-gate 		 * caller is not bound to a processor set).
475*7c478bd9Sstevel@tonic-gate 		 */
476*7c478bd9Sstevel@tonic-gate 		if (pool_state == POOL_ENABLED) {
477*7c478bd9Sstevel@tonic-gate 			pool_unlock();
478*7c478bd9Sstevel@tonic-gate 			return (set_errno(ENOTSUP));
479*7c478bd9Sstevel@tonic-gate 		}
480*7c478bd9Sstevel@tonic-gate 		if (cpupart_get_cpus(&pset, NULL, NULL) != 0) {
481*7c478bd9Sstevel@tonic-gate 			pool_unlock();
482*7c478bd9Sstevel@tonic-gate 			return (set_errno(EINVAL));
483*7c478bd9Sstevel@tonic-gate 		} else if (pset != PS_NONE && secpolicy_pset(CRED()) != 0) {
484*7c478bd9Sstevel@tonic-gate 			pool_unlock();
485*7c478bd9Sstevel@tonic-gate 			return (set_errno(EPERM));
486*7c478bd9Sstevel@tonic-gate 		}
487*7c478bd9Sstevel@tonic-gate 	}
488*7c478bd9Sstevel@tonic-gate 
489*7c478bd9Sstevel@tonic-gate 	/*
490*7c478bd9Sstevel@tonic-gate 	 * Pre-allocate enough buffers for FSS for all active projects
491*7c478bd9Sstevel@tonic-gate 	 * and for all active zones on the system.  Unused buffers will
492*7c478bd9Sstevel@tonic-gate 	 * be freed later by fss_freebuf().
493*7c478bd9Sstevel@tonic-gate 	 */
494*7c478bd9Sstevel@tonic-gate 	mutex_enter(&cpu_lock);
495*7c478bd9Sstevel@tonic-gate 	projbuf = fss_allocbuf(FSS_NPROJ_BUF, FSS_ALLOC_PROJ);
496*7c478bd9Sstevel@tonic-gate 	zonebuf = fss_allocbuf(FSS_NPROJ_BUF, FSS_ALLOC_ZONE);
497*7c478bd9Sstevel@tonic-gate 
498*7c478bd9Sstevel@tonic-gate 	switch (idtype) {
499*7c478bd9Sstevel@tonic-gate 	case P_LWPID:
500*7c478bd9Sstevel@tonic-gate 		pp = curproc;
501*7c478bd9Sstevel@tonic-gate 		mutex_enter(&pidlock);
502*7c478bd9Sstevel@tonic-gate 		mutex_enter(&pp->p_lock);
503*7c478bd9Sstevel@tonic-gate 		if (id == P_MYID) {
504*7c478bd9Sstevel@tonic-gate 			tp = curthread;
505*7c478bd9Sstevel@tonic-gate 		} else {
506*7c478bd9Sstevel@tonic-gate 			if ((tp = idtot(pp, id)) == NULL) {
507*7c478bd9Sstevel@tonic-gate 				mutex_exit(&pp->p_lock);
508*7c478bd9Sstevel@tonic-gate 				mutex_exit(&pidlock);
509*7c478bd9Sstevel@tonic-gate 				error = ESRCH;
510*7c478bd9Sstevel@tonic-gate 				break;
511*7c478bd9Sstevel@tonic-gate 			}
512*7c478bd9Sstevel@tonic-gate 		}
513*7c478bd9Sstevel@tonic-gate 		error = pset_bind_thread(tp, pset, &oldpset, projbuf, zonebuf);
514*7c478bd9Sstevel@tonic-gate 		mutex_exit(&pp->p_lock);
515*7c478bd9Sstevel@tonic-gate 		mutex_exit(&pidlock);
516*7c478bd9Sstevel@tonic-gate 		break;
517*7c478bd9Sstevel@tonic-gate 
518*7c478bd9Sstevel@tonic-gate 	case P_PID:
519*7c478bd9Sstevel@tonic-gate 		mutex_enter(&pidlock);
520*7c478bd9Sstevel@tonic-gate 		if (id == P_MYID) {
521*7c478bd9Sstevel@tonic-gate 			pp = curproc;
522*7c478bd9Sstevel@tonic-gate 		} else if ((pp = prfind(id)) == NULL) {
523*7c478bd9Sstevel@tonic-gate 			mutex_exit(&pidlock);
524*7c478bd9Sstevel@tonic-gate 			error = ESRCH;
525*7c478bd9Sstevel@tonic-gate 			break;
526*7c478bd9Sstevel@tonic-gate 		}
527*7c478bd9Sstevel@tonic-gate 		error = pset_bind_process(pp, pset, &oldpset, projbuf, zonebuf);
528*7c478bd9Sstevel@tonic-gate 		mutex_exit(&pidlock);
529*7c478bd9Sstevel@tonic-gate 		break;
530*7c478bd9Sstevel@tonic-gate 
531*7c478bd9Sstevel@tonic-gate 	case P_TASKID:
532*7c478bd9Sstevel@tonic-gate 		mutex_enter(&pidlock);
533*7c478bd9Sstevel@tonic-gate 		if (id == P_MYID)
534*7c478bd9Sstevel@tonic-gate 			id = curproc->p_task->tk_tkid;
535*7c478bd9Sstevel@tonic-gate 		if ((tk = task_hold_by_id(id)) == NULL) {
536*7c478bd9Sstevel@tonic-gate 			mutex_exit(&pidlock);
537*7c478bd9Sstevel@tonic-gate 			error = ESRCH;
538*7c478bd9Sstevel@tonic-gate 			break;
539*7c478bd9Sstevel@tonic-gate 		}
540*7c478bd9Sstevel@tonic-gate 		error = pset_bind_task(tk, pset, &oldpset, projbuf, zonebuf);
541*7c478bd9Sstevel@tonic-gate 		mutex_exit(&pidlock);
542*7c478bd9Sstevel@tonic-gate 		task_rele(tk);
543*7c478bd9Sstevel@tonic-gate 		break;
544*7c478bd9Sstevel@tonic-gate 
545*7c478bd9Sstevel@tonic-gate 	case P_PROJID:
546*7c478bd9Sstevel@tonic-gate 		if (id == P_MYID)
547*7c478bd9Sstevel@tonic-gate 			id = curprojid();
548*7c478bd9Sstevel@tonic-gate 		if ((kpj = project_hold_by_id(id, getzoneid(),
549*7c478bd9Sstevel@tonic-gate 		    PROJECT_HOLD_FIND)) == NULL) {
550*7c478bd9Sstevel@tonic-gate 			error = ESRCH;
551*7c478bd9Sstevel@tonic-gate 			break;
552*7c478bd9Sstevel@tonic-gate 		}
553*7c478bd9Sstevel@tonic-gate 		mutex_enter(&pidlock);
554*7c478bd9Sstevel@tonic-gate 		error = pset_bind_project(kpj, pset, &oldpset, projbuf,
555*7c478bd9Sstevel@tonic-gate 		    zonebuf);
556*7c478bd9Sstevel@tonic-gate 		mutex_exit(&pidlock);
557*7c478bd9Sstevel@tonic-gate 		project_rele(kpj);
558*7c478bd9Sstevel@tonic-gate 		break;
559*7c478bd9Sstevel@tonic-gate 
560*7c478bd9Sstevel@tonic-gate 	case P_ZONEID:
561*7c478bd9Sstevel@tonic-gate 		if (id == P_MYID)
562*7c478bd9Sstevel@tonic-gate 			id = getzoneid();
563*7c478bd9Sstevel@tonic-gate 		if ((zptr = zone_find_by_id(id)) == NULL) {
564*7c478bd9Sstevel@tonic-gate 			error = ESRCH;
565*7c478bd9Sstevel@tonic-gate 			break;
566*7c478bd9Sstevel@tonic-gate 		}
567*7c478bd9Sstevel@tonic-gate 		mutex_enter(&pidlock);
568*7c478bd9Sstevel@tonic-gate 		error = pset_bind_zone(zptr, pset, &oldpset, projbuf, zonebuf);
569*7c478bd9Sstevel@tonic-gate 		mutex_exit(&pidlock);
570*7c478bd9Sstevel@tonic-gate 		zone_rele(zptr);
571*7c478bd9Sstevel@tonic-gate 		break;
572*7c478bd9Sstevel@tonic-gate 
573*7c478bd9Sstevel@tonic-gate 	case P_CTID:
574*7c478bd9Sstevel@tonic-gate 		if (id == P_MYID)
575*7c478bd9Sstevel@tonic-gate 			id = PRCTID(curproc);
576*7c478bd9Sstevel@tonic-gate 		if ((ct = contract_type_ptr(process_type, id,
577*7c478bd9Sstevel@tonic-gate 		    curproc->p_zone->zone_uniqid)) == NULL) {
578*7c478bd9Sstevel@tonic-gate 			error = ESRCH;
579*7c478bd9Sstevel@tonic-gate 			break;
580*7c478bd9Sstevel@tonic-gate 		}
581*7c478bd9Sstevel@tonic-gate 		mutex_enter(&pidlock);
582*7c478bd9Sstevel@tonic-gate 		error = pset_bind_contract(ct->ct_data, pset, &oldpset, projbuf,
583*7c478bd9Sstevel@tonic-gate 		    zonebuf);
584*7c478bd9Sstevel@tonic-gate 		mutex_exit(&pidlock);
585*7c478bd9Sstevel@tonic-gate 		contract_rele(ct);
586*7c478bd9Sstevel@tonic-gate 		break;
587*7c478bd9Sstevel@tonic-gate 
588*7c478bd9Sstevel@tonic-gate 	case P_PSETID:
589*7c478bd9Sstevel@tonic-gate 		if (id == P_MYID || pset != PS_NONE || !INGLOBALZONE(curproc)) {
590*7c478bd9Sstevel@tonic-gate 			error = EINVAL;
591*7c478bd9Sstevel@tonic-gate 			break;
592*7c478bd9Sstevel@tonic-gate 		}
593*7c478bd9Sstevel@tonic-gate 		error = pset_unbind(id, projbuf, zonebuf, idtype);
594*7c478bd9Sstevel@tonic-gate 		break;
595*7c478bd9Sstevel@tonic-gate 
596*7c478bd9Sstevel@tonic-gate 	case P_ALL:
597*7c478bd9Sstevel@tonic-gate 		if (id == P_MYID || pset != PS_NONE || !INGLOBALZONE(curproc)) {
598*7c478bd9Sstevel@tonic-gate 			error = EINVAL;
599*7c478bd9Sstevel@tonic-gate 			break;
600*7c478bd9Sstevel@tonic-gate 		}
601*7c478bd9Sstevel@tonic-gate 		error = pset_unbind(PS_NONE, projbuf, zonebuf, idtype);
602*7c478bd9Sstevel@tonic-gate 		break;
603*7c478bd9Sstevel@tonic-gate 
604*7c478bd9Sstevel@tonic-gate 	default:
605*7c478bd9Sstevel@tonic-gate 		error = EINVAL;
606*7c478bd9Sstevel@tonic-gate 		break;
607*7c478bd9Sstevel@tonic-gate 	}
608*7c478bd9Sstevel@tonic-gate 
609*7c478bd9Sstevel@tonic-gate 	fss_freebuf(projbuf, FSS_ALLOC_PROJ);
610*7c478bd9Sstevel@tonic-gate 	fss_freebuf(zonebuf, FSS_ALLOC_ZONE);
611*7c478bd9Sstevel@tonic-gate 	mutex_exit(&cpu_lock);
612*7c478bd9Sstevel@tonic-gate 	pool_unlock();
613*7c478bd9Sstevel@tonic-gate 
614*7c478bd9Sstevel@tonic-gate 	if (error != 0)
615*7c478bd9Sstevel@tonic-gate 		return (set_errno(error));
616*7c478bd9Sstevel@tonic-gate 	if (opset != NULL) {
617*7c478bd9Sstevel@tonic-gate 		if (copyout(&oldpset, opset, sizeof (psetid_t)) != 0)
618*7c478bd9Sstevel@tonic-gate 			return (set_errno(EFAULT));
619*7c478bd9Sstevel@tonic-gate 	}
620*7c478bd9Sstevel@tonic-gate 	return (0);
621*7c478bd9Sstevel@tonic-gate }
622*7c478bd9Sstevel@tonic-gate 
623*7c478bd9Sstevel@tonic-gate /*
624*7c478bd9Sstevel@tonic-gate  * Report load average statistics for the specified processor set.
625*7c478bd9Sstevel@tonic-gate  */
626*7c478bd9Sstevel@tonic-gate static int
627*7c478bd9Sstevel@tonic-gate pset_getloadavg(psetid_t pset, int *buf, int nelem)
628*7c478bd9Sstevel@tonic-gate {
629*7c478bd9Sstevel@tonic-gate 	int *loadbuf;
630*7c478bd9Sstevel@tonic-gate 	int error = 0;
631*7c478bd9Sstevel@tonic-gate 
632*7c478bd9Sstevel@tonic-gate 	if (nelem < 0)
633*7c478bd9Sstevel@tonic-gate 		return (set_errno(EINVAL));
634*7c478bd9Sstevel@tonic-gate 
635*7c478bd9Sstevel@tonic-gate 	/*
636*7c478bd9Sstevel@tonic-gate 	 * We keep the same number of load average statistics for processor
637*7c478bd9Sstevel@tonic-gate 	 * sets as we do for the system as a whole.
638*7c478bd9Sstevel@tonic-gate 	 */
639*7c478bd9Sstevel@tonic-gate 	if (nelem > LOADAVG_NSTATS)
640*7c478bd9Sstevel@tonic-gate 		nelem = LOADAVG_NSTATS;
641*7c478bd9Sstevel@tonic-gate 
642*7c478bd9Sstevel@tonic-gate 	loadbuf = kmem_alloc(nelem * sizeof (int), KM_SLEEP);
643*7c478bd9Sstevel@tonic-gate 
644*7c478bd9Sstevel@tonic-gate 	mutex_enter(&cpu_lock);
645*7c478bd9Sstevel@tonic-gate 	error = cpupart_get_loadavg(pset, loadbuf, nelem);
646*7c478bd9Sstevel@tonic-gate 	mutex_exit(&cpu_lock);
647*7c478bd9Sstevel@tonic-gate 	if (!error && nelem && copyout(loadbuf, buf, nelem * sizeof (int)) != 0)
648*7c478bd9Sstevel@tonic-gate 		error = EFAULT;
649*7c478bd9Sstevel@tonic-gate 
650*7c478bd9Sstevel@tonic-gate 	kmem_free(loadbuf, nelem * sizeof (int));
651*7c478bd9Sstevel@tonic-gate 
652*7c478bd9Sstevel@tonic-gate 	if (error)
653*7c478bd9Sstevel@tonic-gate 		return (set_errno(error));
654*7c478bd9Sstevel@tonic-gate 	else
655*7c478bd9Sstevel@tonic-gate 		return (0);
656*7c478bd9Sstevel@tonic-gate }
657*7c478bd9Sstevel@tonic-gate 
658*7c478bd9Sstevel@tonic-gate 
659*7c478bd9Sstevel@tonic-gate /*
660*7c478bd9Sstevel@tonic-gate  * Return list of active processor sets, up to a maximum indicated by
661*7c478bd9Sstevel@tonic-gate  * numpsets.  The total number of processor sets is stored in the
662*7c478bd9Sstevel@tonic-gate  * location pointed to by numpsets.
663*7c478bd9Sstevel@tonic-gate  */
664*7c478bd9Sstevel@tonic-gate static int
665*7c478bd9Sstevel@tonic-gate pset_list(psetid_t *psetlist, uint_t *numpsets)
666*7c478bd9Sstevel@tonic-gate {
667*7c478bd9Sstevel@tonic-gate 	uint_t user_npsets = 0;
668*7c478bd9Sstevel@tonic-gate 	uint_t real_npsets;
669*7c478bd9Sstevel@tonic-gate 	psetid_t *psets = NULL;
670*7c478bd9Sstevel@tonic-gate 	int error = 0;
671*7c478bd9Sstevel@tonic-gate 
672*7c478bd9Sstevel@tonic-gate 	if (numpsets != NULL) {
673*7c478bd9Sstevel@tonic-gate 		if (copyin(numpsets, &user_npsets, sizeof (uint_t)) != 0)
674*7c478bd9Sstevel@tonic-gate 			return (set_errno(EFAULT));
675*7c478bd9Sstevel@tonic-gate 	}
676*7c478bd9Sstevel@tonic-gate 
677*7c478bd9Sstevel@tonic-gate 	/*
678*7c478bd9Sstevel@tonic-gate 	 * Get the list of all processor sets.  First we need to find
679*7c478bd9Sstevel@tonic-gate 	 * out how many there are, so we can allocate a large enough
680*7c478bd9Sstevel@tonic-gate 	 * buffer.
681*7c478bd9Sstevel@tonic-gate 	 */
682*7c478bd9Sstevel@tonic-gate 	mutex_enter(&cpu_lock);
683*7c478bd9Sstevel@tonic-gate 	if (!INGLOBALZONE(curproc) && pool_pset_enabled()) {
684*7c478bd9Sstevel@tonic-gate 		psetid_t psetid = zone_pset_get(curproc->p_zone);
685*7c478bd9Sstevel@tonic-gate 
686*7c478bd9Sstevel@tonic-gate 		if (psetid == PS_NONE) {
687*7c478bd9Sstevel@tonic-gate 			real_npsets = 0;
688*7c478bd9Sstevel@tonic-gate 		} else {
689*7c478bd9Sstevel@tonic-gate 			real_npsets = 1;
690*7c478bd9Sstevel@tonic-gate 			psets = kmem_alloc(real_npsets * sizeof (psetid_t),
691*7c478bd9Sstevel@tonic-gate 			    KM_SLEEP);
692*7c478bd9Sstevel@tonic-gate 			psets[0] = psetid;
693*7c478bd9Sstevel@tonic-gate 		}
694*7c478bd9Sstevel@tonic-gate 	} else {
695*7c478bd9Sstevel@tonic-gate 		real_npsets = cpupart_list(0, NULL, CP_ALL);
696*7c478bd9Sstevel@tonic-gate 		if (real_npsets) {
697*7c478bd9Sstevel@tonic-gate 			psets = kmem_alloc(real_npsets * sizeof (psetid_t),
698*7c478bd9Sstevel@tonic-gate 			    KM_SLEEP);
699*7c478bd9Sstevel@tonic-gate 			(void) cpupart_list(psets, real_npsets, CP_ALL);
700*7c478bd9Sstevel@tonic-gate 		}
701*7c478bd9Sstevel@tonic-gate 	}
702*7c478bd9Sstevel@tonic-gate 	mutex_exit(&cpu_lock);
703*7c478bd9Sstevel@tonic-gate 
704*7c478bd9Sstevel@tonic-gate 	if (user_npsets > real_npsets)
705*7c478bd9Sstevel@tonic-gate 		user_npsets = real_npsets;
706*7c478bd9Sstevel@tonic-gate 
707*7c478bd9Sstevel@tonic-gate 	if (numpsets != NULL) {
708*7c478bd9Sstevel@tonic-gate 		if (copyout(&real_npsets, numpsets, sizeof (uint_t)) != 0)
709*7c478bd9Sstevel@tonic-gate 			error = EFAULT;
710*7c478bd9Sstevel@tonic-gate 		else if (psetlist != NULL && user_npsets != 0) {
711*7c478bd9Sstevel@tonic-gate 			if (copyout(psets, psetlist,
712*7c478bd9Sstevel@tonic-gate 			    user_npsets * sizeof (psetid_t)) != 0)
713*7c478bd9Sstevel@tonic-gate 				error = EFAULT;
714*7c478bd9Sstevel@tonic-gate 		}
715*7c478bd9Sstevel@tonic-gate 	}
716*7c478bd9Sstevel@tonic-gate 
717*7c478bd9Sstevel@tonic-gate 	if (real_npsets)
718*7c478bd9Sstevel@tonic-gate 		kmem_free(psets, real_npsets * sizeof (psetid_t));
719*7c478bd9Sstevel@tonic-gate 
720*7c478bd9Sstevel@tonic-gate 	if (error)
721*7c478bd9Sstevel@tonic-gate 		return (set_errno(error));
722*7c478bd9Sstevel@tonic-gate 	else
723*7c478bd9Sstevel@tonic-gate 		return (0);
724*7c478bd9Sstevel@tonic-gate }
725*7c478bd9Sstevel@tonic-gate 
726*7c478bd9Sstevel@tonic-gate static int
727*7c478bd9Sstevel@tonic-gate pset_setattr(psetid_t pset, uint_t attr)
728*7c478bd9Sstevel@tonic-gate {
729*7c478bd9Sstevel@tonic-gate 	int error;
730*7c478bd9Sstevel@tonic-gate 
731*7c478bd9Sstevel@tonic-gate 	if (secpolicy_pset(CRED()) != 0)
732*7c478bd9Sstevel@tonic-gate 		return (set_errno(EPERM));
733*7c478bd9Sstevel@tonic-gate 	pool_lock();
734*7c478bd9Sstevel@tonic-gate 	if (pool_state == POOL_ENABLED) {
735*7c478bd9Sstevel@tonic-gate 		pool_unlock();
736*7c478bd9Sstevel@tonic-gate 		return (set_errno(ENOTSUP));
737*7c478bd9Sstevel@tonic-gate 	}
738*7c478bd9Sstevel@tonic-gate 	if (pset == PS_QUERY || PSET_BADATTR(attr)) {
739*7c478bd9Sstevel@tonic-gate 		pool_unlock();
740*7c478bd9Sstevel@tonic-gate 		return (set_errno(EINVAL));
741*7c478bd9Sstevel@tonic-gate 	}
742*7c478bd9Sstevel@tonic-gate 	if ((error = cpupart_setattr(pset, attr)) != 0) {
743*7c478bd9Sstevel@tonic-gate 		pool_unlock();
744*7c478bd9Sstevel@tonic-gate 		return (set_errno(error));
745*7c478bd9Sstevel@tonic-gate 	}
746*7c478bd9Sstevel@tonic-gate 	pool_unlock();
747*7c478bd9Sstevel@tonic-gate 	return (0);
748*7c478bd9Sstevel@tonic-gate }
749*7c478bd9Sstevel@tonic-gate 
750*7c478bd9Sstevel@tonic-gate static int
751*7c478bd9Sstevel@tonic-gate pset_getattr(psetid_t pset, uint_t *attrp)
752*7c478bd9Sstevel@tonic-gate {
753*7c478bd9Sstevel@tonic-gate 	int error = 0;
754*7c478bd9Sstevel@tonic-gate 	uint_t attr;
755*7c478bd9Sstevel@tonic-gate 
756*7c478bd9Sstevel@tonic-gate 	if (pset == PS_QUERY)
757*7c478bd9Sstevel@tonic-gate 		return (set_errno(EINVAL));
758*7c478bd9Sstevel@tonic-gate 	if ((error = cpupart_getattr(pset, &attr)) != 0)
759*7c478bd9Sstevel@tonic-gate 		return (set_errno(error));
760*7c478bd9Sstevel@tonic-gate 	if (copyout(&attr, attrp, sizeof (uint_t)) != 0)
761*7c478bd9Sstevel@tonic-gate 		return (set_errno(EFAULT));
762*7c478bd9Sstevel@tonic-gate 	return (0);
763*7c478bd9Sstevel@tonic-gate }
764*7c478bd9Sstevel@tonic-gate 
765*7c478bd9Sstevel@tonic-gate static int
766*7c478bd9Sstevel@tonic-gate pset(int subcode, long arg1, long arg2, long arg3, long arg4)
767*7c478bd9Sstevel@tonic-gate {
768*7c478bd9Sstevel@tonic-gate 	switch (subcode) {
769*7c478bd9Sstevel@tonic-gate 	case PSET_CREATE:
770*7c478bd9Sstevel@tonic-gate 		return (pset_create((psetid_t *)arg1));
771*7c478bd9Sstevel@tonic-gate 	case PSET_DESTROY:
772*7c478bd9Sstevel@tonic-gate 		return (pset_destroy((psetid_t)arg1));
773*7c478bd9Sstevel@tonic-gate 	case PSET_ASSIGN:
774*7c478bd9Sstevel@tonic-gate 		return (pset_assign((psetid_t)arg1,
775*7c478bd9Sstevel@tonic-gate 		    (processorid_t)arg2, (psetid_t *)arg3, 0));
776*7c478bd9Sstevel@tonic-gate 	case PSET_INFO:
777*7c478bd9Sstevel@tonic-gate 		return (pset_info((psetid_t)arg1, (int *)arg2,
778*7c478bd9Sstevel@tonic-gate 		    (uint_t *)arg3, (processorid_t *)arg4));
779*7c478bd9Sstevel@tonic-gate 	case PSET_BIND:
780*7c478bd9Sstevel@tonic-gate 		return (pset_bind((psetid_t)arg1, (idtype_t)arg2,
781*7c478bd9Sstevel@tonic-gate 		    (id_t)arg3, (psetid_t *)arg4));
782*7c478bd9Sstevel@tonic-gate 	case PSET_GETLOADAVG:
783*7c478bd9Sstevel@tonic-gate 		return (pset_getloadavg((psetid_t)arg1, (int *)arg2,
784*7c478bd9Sstevel@tonic-gate 		    (int)arg3));
785*7c478bd9Sstevel@tonic-gate 	case PSET_LIST:
786*7c478bd9Sstevel@tonic-gate 		return (pset_list((psetid_t *)arg1, (uint_t *)arg2));
787*7c478bd9Sstevel@tonic-gate 	case PSET_SETATTR:
788*7c478bd9Sstevel@tonic-gate 		return (pset_setattr((psetid_t)arg1, (uint_t)arg2));
789*7c478bd9Sstevel@tonic-gate 	case PSET_GETATTR:
790*7c478bd9Sstevel@tonic-gate 		return (pset_getattr((psetid_t)arg1, (uint_t *)arg2));
791*7c478bd9Sstevel@tonic-gate 	case PSET_ASSIGN_FORCED:
792*7c478bd9Sstevel@tonic-gate 		return (pset_assign((psetid_t)arg1,
793*7c478bd9Sstevel@tonic-gate 		    (processorid_t)arg2, (psetid_t *)arg3, 1));
794*7c478bd9Sstevel@tonic-gate 	default:
795*7c478bd9Sstevel@tonic-gate 		return (set_errno(EINVAL));
796*7c478bd9Sstevel@tonic-gate 	}
797*7c478bd9Sstevel@tonic-gate }
798