17c478bd9Sstevel@tonic-gate /* 27c478bd9Sstevel@tonic-gate * CDDL HEADER START 37c478bd9Sstevel@tonic-gate * 47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5d5fbc238Sfr157268 * Common Development and Distribution License (the "License"). 6d5fbc238Sfr157268 * You may not use this file except in compliance with the License. 77c478bd9Sstevel@tonic-gate * 87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 117c478bd9Sstevel@tonic-gate * and limitations under the License. 127c478bd9Sstevel@tonic-gate * 137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 187c478bd9Sstevel@tonic-gate * 197c478bd9Sstevel@tonic-gate * CDDL HEADER END 207c478bd9Sstevel@tonic-gate */ 217c478bd9Sstevel@tonic-gate /* 22438076ebSGangadhar Mylapuram * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 237c478bd9Sstevel@tonic-gate * Use is subject to license terms. 247c478bd9Sstevel@tonic-gate */ 257c478bd9Sstevel@tonic-gate 267c478bd9Sstevel@tonic-gate #include <sys/types.h> 277c478bd9Sstevel@tonic-gate #include <sys/systm.h> 287c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h> 297c478bd9Sstevel@tonic-gate #include <sys/cpuvar.h> 307c478bd9Sstevel@tonic-gate #include <sys/thread.h> 317c478bd9Sstevel@tonic-gate #include <sys/disp.h> 327c478bd9Sstevel@tonic-gate #include <sys/kmem.h> 337c478bd9Sstevel@tonic-gate #include <sys/debug.h> 347c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h> 357c478bd9Sstevel@tonic-gate #include <sys/cpupart.h> 367c478bd9Sstevel@tonic-gate #include <sys/pset.h> 377c478bd9Sstevel@tonic-gate #include <sys/modctl.h> 387c478bd9Sstevel@tonic-gate #include <sys/syscall.h> 397c478bd9Sstevel@tonic-gate #include <sys/task.h> 407c478bd9Sstevel@tonic-gate #include <sys/loadavg.h> 417c478bd9Sstevel@tonic-gate #include <sys/fss.h> 427c478bd9Sstevel@tonic-gate #include <sys/pool.h> 437c478bd9Sstevel@tonic-gate #include <sys/pool_pset.h> 447c478bd9Sstevel@tonic-gate #include <sys/policy.h> 457c478bd9Sstevel@tonic-gate #include <sys/zone.h> 467c478bd9Sstevel@tonic-gate #include <sys/contract/process_impl.h> 477c478bd9Sstevel@tonic-gate 487c478bd9Sstevel@tonic-gate static int pset(int, long, long, long, long); 497c478bd9Sstevel@tonic-gate 507c478bd9Sstevel@tonic-gate static struct sysent pset_sysent = { 517c478bd9Sstevel@tonic-gate 5, 527c478bd9Sstevel@tonic-gate SE_ARGC | SE_NOUNLOAD, 537c478bd9Sstevel@tonic-gate (int (*)())pset, 547c478bd9Sstevel@tonic-gate }; 557c478bd9Sstevel@tonic-gate 567c478bd9Sstevel@tonic-gate static struct modlsys modlsys = { 577c478bd9Sstevel@tonic-gate &mod_syscallops, "processor sets", &pset_sysent 587c478bd9Sstevel@tonic-gate }; 597c478bd9Sstevel@tonic-gate 607c478bd9Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL 617c478bd9Sstevel@tonic-gate static struct modlsys modlsys32 = { 627c478bd9Sstevel@tonic-gate &mod_syscallops32, "32-bit pset(2) syscall", &pset_sysent 637c478bd9Sstevel@tonic-gate }; 647c478bd9Sstevel@tonic-gate #endif 657c478bd9Sstevel@tonic-gate 667c478bd9Sstevel@tonic-gate static struct modlinkage modlinkage = { 677c478bd9Sstevel@tonic-gate MODREV_1, 687c478bd9Sstevel@tonic-gate &modlsys, 697c478bd9Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL 707c478bd9Sstevel@tonic-gate &modlsys32, 717c478bd9Sstevel@tonic-gate #endif 727c478bd9Sstevel@tonic-gate NULL 737c478bd9Sstevel@tonic-gate }; 747c478bd9Sstevel@tonic-gate 757c478bd9Sstevel@tonic-gate #define PSET_BADATTR(attr) ((~PSET_NOESCAPE) & (attr)) 767c478bd9Sstevel@tonic-gate 777c478bd9Sstevel@tonic-gate int 787c478bd9Sstevel@tonic-gate _init(void) 797c478bd9Sstevel@tonic-gate { 807c478bd9Sstevel@tonic-gate return (mod_install(&modlinkage)); 817c478bd9Sstevel@tonic-gate } 827c478bd9Sstevel@tonic-gate 837c478bd9Sstevel@tonic-gate int 847c478bd9Sstevel@tonic-gate _info(struct modinfo *modinfop) 857c478bd9Sstevel@tonic-gate { 867c478bd9Sstevel@tonic-gate return (mod_info(&modlinkage, modinfop)); 877c478bd9Sstevel@tonic-gate } 887c478bd9Sstevel@tonic-gate 897c478bd9Sstevel@tonic-gate static int 907c478bd9Sstevel@tonic-gate pset_create(psetid_t *psetp) 917c478bd9Sstevel@tonic-gate { 927c478bd9Sstevel@tonic-gate psetid_t newpset; 937c478bd9Sstevel@tonic-gate int error; 947c478bd9Sstevel@tonic-gate 957c478bd9Sstevel@tonic-gate if (secpolicy_pset(CRED()) != 0) 967c478bd9Sstevel@tonic-gate return (set_errno(EPERM)); 977c478bd9Sstevel@tonic-gate 987c478bd9Sstevel@tonic-gate pool_lock(); 997c478bd9Sstevel@tonic-gate if (pool_state == POOL_ENABLED) { 1007c478bd9Sstevel@tonic-gate pool_unlock(); 1017c478bd9Sstevel@tonic-gate return (set_errno(ENOTSUP)); 1027c478bd9Sstevel@tonic-gate } 1037c478bd9Sstevel@tonic-gate error = cpupart_create(&newpset); 1047c478bd9Sstevel@tonic-gate if (error) { 1057c478bd9Sstevel@tonic-gate pool_unlock(); 1067c478bd9Sstevel@tonic-gate return (set_errno(error)); 1077c478bd9Sstevel@tonic-gate } 1087c478bd9Sstevel@tonic-gate if (copyout(&newpset, psetp, sizeof (psetid_t)) != 0) { 1097c478bd9Sstevel@tonic-gate (void) cpupart_destroy(newpset); 1107c478bd9Sstevel@tonic-gate pool_unlock(); 1117c478bd9Sstevel@tonic-gate return (set_errno(EFAULT)); 1127c478bd9Sstevel@tonic-gate } 1137c478bd9Sstevel@tonic-gate pool_unlock(); 1147c478bd9Sstevel@tonic-gate return (error); 1157c478bd9Sstevel@tonic-gate } 1167c478bd9Sstevel@tonic-gate 1177c478bd9Sstevel@tonic-gate static int 1187c478bd9Sstevel@tonic-gate pset_destroy(psetid_t pset) 1197c478bd9Sstevel@tonic-gate { 1207c478bd9Sstevel@tonic-gate int error; 1217c478bd9Sstevel@tonic-gate 1227c478bd9Sstevel@tonic-gate if (secpolicy_pset(CRED()) != 0) 1237c478bd9Sstevel@tonic-gate return (set_errno(EPERM)); 1247c478bd9Sstevel@tonic-gate 1257c478bd9Sstevel@tonic-gate pool_lock(); 1267c478bd9Sstevel@tonic-gate if (pool_state == POOL_ENABLED) { 1277c478bd9Sstevel@tonic-gate pool_unlock(); 1287c478bd9Sstevel@tonic-gate return (set_errno(ENOTSUP)); 1297c478bd9Sstevel@tonic-gate } 1307c478bd9Sstevel@tonic-gate error = cpupart_destroy(pset); 1317c478bd9Sstevel@tonic-gate pool_unlock(); 1327c478bd9Sstevel@tonic-gate if (error) 1337c478bd9Sstevel@tonic-gate return (set_errno(error)); 1347c478bd9Sstevel@tonic-gate else 1357c478bd9Sstevel@tonic-gate return (0); 1367c478bd9Sstevel@tonic-gate } 1377c478bd9Sstevel@tonic-gate 1387c478bd9Sstevel@tonic-gate static int 1397c478bd9Sstevel@tonic-gate pset_assign(psetid_t pset, processorid_t cpuid, psetid_t *opset, int forced) 1407c478bd9Sstevel@tonic-gate { 1417c478bd9Sstevel@tonic-gate psetid_t oldpset; 1427c478bd9Sstevel@tonic-gate int error = 0; 1437c478bd9Sstevel@tonic-gate cpu_t *cp; 1447c478bd9Sstevel@tonic-gate 1457c478bd9Sstevel@tonic-gate if (pset != PS_QUERY && secpolicy_pset(CRED()) != 0) 1467c478bd9Sstevel@tonic-gate return (set_errno(EPERM)); 1477c478bd9Sstevel@tonic-gate 1487c478bd9Sstevel@tonic-gate pool_lock(); 1497c478bd9Sstevel@tonic-gate if (pset != PS_QUERY && pool_state == POOL_ENABLED) { 1507c478bd9Sstevel@tonic-gate pool_unlock(); 1517c478bd9Sstevel@tonic-gate return (set_errno(ENOTSUP)); 1527c478bd9Sstevel@tonic-gate } 1537c478bd9Sstevel@tonic-gate 1547c478bd9Sstevel@tonic-gate mutex_enter(&cpu_lock); 1557c478bd9Sstevel@tonic-gate if ((cp = cpu_get(cpuid)) == NULL) { 1567c478bd9Sstevel@tonic-gate mutex_exit(&cpu_lock); 1577c478bd9Sstevel@tonic-gate pool_unlock(); 1587c478bd9Sstevel@tonic-gate return (set_errno(EINVAL)); 1597c478bd9Sstevel@tonic-gate } 1607c478bd9Sstevel@tonic-gate 1617c478bd9Sstevel@tonic-gate oldpset = cpupart_query_cpu(cp); 1627c478bd9Sstevel@tonic-gate 1637c478bd9Sstevel@tonic-gate if (pset != PS_QUERY) 1647c478bd9Sstevel@tonic-gate error = cpupart_attach_cpu(pset, cp, forced); 1657c478bd9Sstevel@tonic-gate mutex_exit(&cpu_lock); 1667c478bd9Sstevel@tonic-gate pool_unlock(); 1677c478bd9Sstevel@tonic-gate 1687c478bd9Sstevel@tonic-gate if (error) 1697c478bd9Sstevel@tonic-gate return (set_errno(error)); 1707c478bd9Sstevel@tonic-gate 1717c478bd9Sstevel@tonic-gate if (opset != NULL) 1727c478bd9Sstevel@tonic-gate if (copyout(&oldpset, opset, sizeof (psetid_t)) != 0) 1737c478bd9Sstevel@tonic-gate return (set_errno(EFAULT)); 1747c478bd9Sstevel@tonic-gate 1757c478bd9Sstevel@tonic-gate return (0); 1767c478bd9Sstevel@tonic-gate } 1777c478bd9Sstevel@tonic-gate 1787c478bd9Sstevel@tonic-gate static int 1797c478bd9Sstevel@tonic-gate pset_info(psetid_t pset, int *typep, uint_t *numcpusp, 1807c478bd9Sstevel@tonic-gate processorid_t *cpulistp) 1817c478bd9Sstevel@tonic-gate { 1827c478bd9Sstevel@tonic-gate int pset_type; 1837c478bd9Sstevel@tonic-gate uint_t user_ncpus = 0, real_ncpus, copy_ncpus; 1847c478bd9Sstevel@tonic-gate processorid_t *pset_cpus = NULL; 1857c478bd9Sstevel@tonic-gate int error = 0; 1867c478bd9Sstevel@tonic-gate 1877c478bd9Sstevel@tonic-gate if (numcpusp != NULL) { 1887c478bd9Sstevel@tonic-gate if (copyin(numcpusp, &user_ncpus, sizeof (uint_t)) != 0) 1897c478bd9Sstevel@tonic-gate return (set_errno(EFAULT)); 1907c478bd9Sstevel@tonic-gate } 1917c478bd9Sstevel@tonic-gate 1927c478bd9Sstevel@tonic-gate if (user_ncpus > max_ncpus) /* sanity check */ 1937c478bd9Sstevel@tonic-gate user_ncpus = max_ncpus; 1947c478bd9Sstevel@tonic-gate if (user_ncpus != 0 && cpulistp != NULL) 1957c478bd9Sstevel@tonic-gate pset_cpus = kmem_alloc(sizeof (processorid_t) * user_ncpus, 1967c478bd9Sstevel@tonic-gate KM_SLEEP); 1977c478bd9Sstevel@tonic-gate 1987c478bd9Sstevel@tonic-gate real_ncpus = user_ncpus; 1997c478bd9Sstevel@tonic-gate if ((error = cpupart_get_cpus(&pset, pset_cpus, &real_ncpus)) != 0) 2007c478bd9Sstevel@tonic-gate goto out; 2017c478bd9Sstevel@tonic-gate 2027c478bd9Sstevel@tonic-gate /* 2037c478bd9Sstevel@tonic-gate * Now copyout the information about this processor set. 2047c478bd9Sstevel@tonic-gate */ 2057c478bd9Sstevel@tonic-gate 2067c478bd9Sstevel@tonic-gate /* 2077c478bd9Sstevel@tonic-gate * Get number of cpus to copy back. If the user didn't pass in 2087c478bd9Sstevel@tonic-gate * a big enough buffer, only copy back as many cpus as fits in 2097c478bd9Sstevel@tonic-gate * the buffer but copy back the real number of cpus. 2107c478bd9Sstevel@tonic-gate */ 2117c478bd9Sstevel@tonic-gate 2127c478bd9Sstevel@tonic-gate if (user_ncpus != 0 && cpulistp != NULL) { 2137c478bd9Sstevel@tonic-gate copy_ncpus = MIN(real_ncpus, user_ncpus); 2147c478bd9Sstevel@tonic-gate if (copyout(pset_cpus, cpulistp, 2157c478bd9Sstevel@tonic-gate sizeof (processorid_t) * copy_ncpus) != 0) { 2167c478bd9Sstevel@tonic-gate error = EFAULT; 2177c478bd9Sstevel@tonic-gate goto out; 2187c478bd9Sstevel@tonic-gate } 2197c478bd9Sstevel@tonic-gate } 2207c478bd9Sstevel@tonic-gate if (pset_cpus != NULL) 2217c478bd9Sstevel@tonic-gate kmem_free(pset_cpus, sizeof (processorid_t) * user_ncpus); 2227c478bd9Sstevel@tonic-gate if (typep != NULL) { 2237c478bd9Sstevel@tonic-gate if (pset == PS_NONE) 2247c478bd9Sstevel@tonic-gate pset_type = PS_NONE; 2257c478bd9Sstevel@tonic-gate else 2267c478bd9Sstevel@tonic-gate pset_type = PS_PRIVATE; 2277c478bd9Sstevel@tonic-gate if (copyout(&pset_type, typep, sizeof (int)) != 0) 2287c478bd9Sstevel@tonic-gate return (set_errno(EFAULT)); 2297c478bd9Sstevel@tonic-gate } 2307c478bd9Sstevel@tonic-gate if (numcpusp != NULL) 2317c478bd9Sstevel@tonic-gate if (copyout(&real_ncpus, numcpusp, sizeof (uint_t)) != 0) 2327c478bd9Sstevel@tonic-gate return (set_errno(EFAULT)); 2337c478bd9Sstevel@tonic-gate return (0); 2347c478bd9Sstevel@tonic-gate 2357c478bd9Sstevel@tonic-gate out: 2367c478bd9Sstevel@tonic-gate if (pset_cpus != NULL) 2377c478bd9Sstevel@tonic-gate kmem_free(pset_cpus, sizeof (processorid_t) * user_ncpus); 2387c478bd9Sstevel@tonic-gate return (set_errno(error)); 2397c478bd9Sstevel@tonic-gate } 2407c478bd9Sstevel@tonic-gate 2417c478bd9Sstevel@tonic-gate static int 2427c478bd9Sstevel@tonic-gate pset_bind_thread(kthread_t *tp, psetid_t pset, psetid_t *oldpset, void *projbuf, 2437c478bd9Sstevel@tonic-gate void *zonebuf) 2447c478bd9Sstevel@tonic-gate { 2457c478bd9Sstevel@tonic-gate int error = 0; 2467c478bd9Sstevel@tonic-gate 2477c478bd9Sstevel@tonic-gate ASSERT(pool_lock_held()); 2487c478bd9Sstevel@tonic-gate ASSERT(MUTEX_HELD(&cpu_lock)); 2497c478bd9Sstevel@tonic-gate ASSERT(MUTEX_HELD(&ttoproc(tp)->p_lock)); 2507c478bd9Sstevel@tonic-gate 2517c478bd9Sstevel@tonic-gate *oldpset = tp->t_bind_pset; 2520b70c467Sakolb 2530b70c467Sakolb switch (pset) { 2540b70c467Sakolb case PS_SOFT: 2550b70c467Sakolb TB_PSET_SOFT_SET(tp); 2560b70c467Sakolb break; 2570b70c467Sakolb 2580b70c467Sakolb case PS_HARD: 2590b70c467Sakolb TB_PSET_HARD_SET(tp); 2600b70c467Sakolb break; 2610b70c467Sakolb 2620b70c467Sakolb case PS_QUERY: 2630b70c467Sakolb break; 2640b70c467Sakolb 2650b70c467Sakolb case PS_QUERY_TYPE: 2660b70c467Sakolb *oldpset = TB_PSET_IS_SOFT(tp) ? PS_SOFT : PS_HARD; 2670b70c467Sakolb break; 2680b70c467Sakolb 2690b70c467Sakolb default: 2707c478bd9Sstevel@tonic-gate /* 2717c478bd9Sstevel@tonic-gate * Must have the same UID as the target process or 2727c478bd9Sstevel@tonic-gate * have PRIV_PROC_OWNER privilege. 2737c478bd9Sstevel@tonic-gate */ 2747c478bd9Sstevel@tonic-gate if (!hasprocperm(tp->t_cred, CRED())) 2757c478bd9Sstevel@tonic-gate return (EPERM); 2767c478bd9Sstevel@tonic-gate /* 2777c478bd9Sstevel@tonic-gate * Unbinding of an unbound thread should always succeed. 2787c478bd9Sstevel@tonic-gate */ 2797c478bd9Sstevel@tonic-gate if (*oldpset == PS_NONE && pset == PS_NONE) 2807c478bd9Sstevel@tonic-gate return (0); 2817c478bd9Sstevel@tonic-gate /* 2827c478bd9Sstevel@tonic-gate * Only privileged processes can move threads from psets with 2837c478bd9Sstevel@tonic-gate * PSET_NOESCAPE attribute. 2847c478bd9Sstevel@tonic-gate */ 2857c478bd9Sstevel@tonic-gate if ((tp->t_cpupart->cp_attr & PSET_NOESCAPE) && 2867c478bd9Sstevel@tonic-gate secpolicy_pset(CRED()) != 0) 2877c478bd9Sstevel@tonic-gate return (EPERM); 2887c478bd9Sstevel@tonic-gate if ((error = cpupart_bind_thread(tp, pset, 0, 2897c478bd9Sstevel@tonic-gate projbuf, zonebuf)) == 0) 2907c478bd9Sstevel@tonic-gate tp->t_bind_pset = pset; 2910b70c467Sakolb 2920b70c467Sakolb break; 2937c478bd9Sstevel@tonic-gate } 2940b70c467Sakolb 2957c478bd9Sstevel@tonic-gate return (error); 2967c478bd9Sstevel@tonic-gate } 2977c478bd9Sstevel@tonic-gate 2987c478bd9Sstevel@tonic-gate static int 2997c478bd9Sstevel@tonic-gate pset_bind_process(proc_t *pp, psetid_t pset, psetid_t *oldpset, void *projbuf, 3007c478bd9Sstevel@tonic-gate void *zonebuf) 3017c478bd9Sstevel@tonic-gate { 3027c478bd9Sstevel@tonic-gate int error = 0; 3037c478bd9Sstevel@tonic-gate kthread_t *tp; 3047c478bd9Sstevel@tonic-gate 3057c478bd9Sstevel@tonic-gate /* skip kernel processes */ 3060b70c467Sakolb if ((pset != PS_QUERY) && pp->p_flag & SSYS) { 3077c478bd9Sstevel@tonic-gate *oldpset = PS_NONE; 308438076ebSGangadhar Mylapuram return (ENOTSUP); 3097c478bd9Sstevel@tonic-gate } 3107c478bd9Sstevel@tonic-gate 3117c478bd9Sstevel@tonic-gate mutex_enter(&pp->p_lock); 3127c478bd9Sstevel@tonic-gate tp = pp->p_tlist; 3137c478bd9Sstevel@tonic-gate if (tp != NULL) { 3147c478bd9Sstevel@tonic-gate do { 3157c478bd9Sstevel@tonic-gate int rval; 3167c478bd9Sstevel@tonic-gate 3177c478bd9Sstevel@tonic-gate rval = pset_bind_thread(tp, pset, oldpset, projbuf, 3187c478bd9Sstevel@tonic-gate zonebuf); 3197c478bd9Sstevel@tonic-gate if (error == 0) 3207c478bd9Sstevel@tonic-gate error = rval; 3217c478bd9Sstevel@tonic-gate } while ((tp = tp->t_forw) != pp->p_tlist); 3227c478bd9Sstevel@tonic-gate } else 3237c478bd9Sstevel@tonic-gate error = ESRCH; 3247c478bd9Sstevel@tonic-gate mutex_exit(&pp->p_lock); 3257c478bd9Sstevel@tonic-gate 3267c478bd9Sstevel@tonic-gate return (error); 3277c478bd9Sstevel@tonic-gate } 3287c478bd9Sstevel@tonic-gate 3297c478bd9Sstevel@tonic-gate static int 3307c478bd9Sstevel@tonic-gate pset_bind_task(task_t *tk, psetid_t pset, psetid_t *oldpset, void *projbuf, 3317c478bd9Sstevel@tonic-gate void *zonebuf) 3327c478bd9Sstevel@tonic-gate { 3337c478bd9Sstevel@tonic-gate int error = 0; 3347c478bd9Sstevel@tonic-gate proc_t *pp; 3357c478bd9Sstevel@tonic-gate 3367c478bd9Sstevel@tonic-gate ASSERT(MUTEX_HELD(&pidlock)); 3377c478bd9Sstevel@tonic-gate 3387c478bd9Sstevel@tonic-gate if ((pp = tk->tk_memb_list) == NULL) { 3397c478bd9Sstevel@tonic-gate return (ESRCH); 3407c478bd9Sstevel@tonic-gate } 3417c478bd9Sstevel@tonic-gate 3427c478bd9Sstevel@tonic-gate do { 3437c478bd9Sstevel@tonic-gate int rval; 3447c478bd9Sstevel@tonic-gate 345438076ebSGangadhar Mylapuram if (!(pp->p_flag & SSYS)) { 346438076ebSGangadhar Mylapuram rval = pset_bind_process(pp, pset, oldpset, projbuf, 347438076ebSGangadhar Mylapuram zonebuf); 3487c478bd9Sstevel@tonic-gate if (error == 0) 3497c478bd9Sstevel@tonic-gate error = rval; 350438076ebSGangadhar Mylapuram } 3517c478bd9Sstevel@tonic-gate } while ((pp = pp->p_tasknext) != tk->tk_memb_list); 3527c478bd9Sstevel@tonic-gate 3537c478bd9Sstevel@tonic-gate return (error); 3547c478bd9Sstevel@tonic-gate } 3557c478bd9Sstevel@tonic-gate 3567c478bd9Sstevel@tonic-gate static int 3577c478bd9Sstevel@tonic-gate pset_bind_project(kproject_t *kpj, psetid_t pset, psetid_t *oldpset, 3587c478bd9Sstevel@tonic-gate void *projbuf, void *zonebuf) 3597c478bd9Sstevel@tonic-gate { 3607c478bd9Sstevel@tonic-gate int error = 0; 3617c478bd9Sstevel@tonic-gate proc_t *pp; 3627c478bd9Sstevel@tonic-gate 3637c478bd9Sstevel@tonic-gate ASSERT(MUTEX_HELD(&pidlock)); 3647c478bd9Sstevel@tonic-gate 3657c478bd9Sstevel@tonic-gate for (pp = practive; pp != NULL; pp = pp->p_next) { 3667c478bd9Sstevel@tonic-gate if (pp->p_tlist == NULL) 3677c478bd9Sstevel@tonic-gate continue; 368438076ebSGangadhar Mylapuram if (pp->p_task->tk_proj == kpj && !(pp->p_flag & SSYS)) { 3697c478bd9Sstevel@tonic-gate int rval; 3707c478bd9Sstevel@tonic-gate 3717c478bd9Sstevel@tonic-gate rval = pset_bind_process(pp, pset, oldpset, projbuf, 3727c478bd9Sstevel@tonic-gate zonebuf); 3737c478bd9Sstevel@tonic-gate if (error == 0) 3747c478bd9Sstevel@tonic-gate error = rval; 3757c478bd9Sstevel@tonic-gate } 3767c478bd9Sstevel@tonic-gate } 3777c478bd9Sstevel@tonic-gate 3787c478bd9Sstevel@tonic-gate return (error); 3797c478bd9Sstevel@tonic-gate } 3807c478bd9Sstevel@tonic-gate 3817c478bd9Sstevel@tonic-gate static int 3827c478bd9Sstevel@tonic-gate pset_bind_zone(zone_t *zptr, psetid_t pset, psetid_t *oldpset, void *projbuf, 3837c478bd9Sstevel@tonic-gate void *zonebuf) 3847c478bd9Sstevel@tonic-gate { 3857c478bd9Sstevel@tonic-gate int error = 0; 3867c478bd9Sstevel@tonic-gate proc_t *pp; 3877c478bd9Sstevel@tonic-gate 3887c478bd9Sstevel@tonic-gate ASSERT(MUTEX_HELD(&pidlock)); 3897c478bd9Sstevel@tonic-gate 3907c478bd9Sstevel@tonic-gate for (pp = practive; pp != NULL; pp = pp->p_next) { 391438076ebSGangadhar Mylapuram if (pp->p_zone == zptr && !(pp->p_flag & SSYS)) { 3927c478bd9Sstevel@tonic-gate int rval; 3937c478bd9Sstevel@tonic-gate 3947c478bd9Sstevel@tonic-gate rval = pset_bind_process(pp, pset, oldpset, projbuf, 3957c478bd9Sstevel@tonic-gate zonebuf); 3967c478bd9Sstevel@tonic-gate if (error == 0) 3977c478bd9Sstevel@tonic-gate error = rval; 3987c478bd9Sstevel@tonic-gate } 3997c478bd9Sstevel@tonic-gate } 4007c478bd9Sstevel@tonic-gate 4017c478bd9Sstevel@tonic-gate return (error); 4027c478bd9Sstevel@tonic-gate } 4037c478bd9Sstevel@tonic-gate 4047c478bd9Sstevel@tonic-gate /* 4057c478bd9Sstevel@tonic-gate * Unbind all threads from the specified processor set, or from all 4067c478bd9Sstevel@tonic-gate * processor sets. 4077c478bd9Sstevel@tonic-gate */ 4087c478bd9Sstevel@tonic-gate static int 4097c478bd9Sstevel@tonic-gate pset_unbind(psetid_t pset, void *projbuf, void *zonebuf, idtype_t idtype) 4107c478bd9Sstevel@tonic-gate { 4117c478bd9Sstevel@tonic-gate psetid_t olbind; 4127c478bd9Sstevel@tonic-gate kthread_t *tp; 4137c478bd9Sstevel@tonic-gate int error = 0; 4147c478bd9Sstevel@tonic-gate int rval; 4157c478bd9Sstevel@tonic-gate proc_t *pp; 4167c478bd9Sstevel@tonic-gate 4177c478bd9Sstevel@tonic-gate ASSERT(MUTEX_HELD(&cpu_lock)); 4187c478bd9Sstevel@tonic-gate 4197c478bd9Sstevel@tonic-gate if (idtype == P_PSETID && cpupart_find(pset) == NULL) 4207c478bd9Sstevel@tonic-gate return (EINVAL); 4217c478bd9Sstevel@tonic-gate 4227c478bd9Sstevel@tonic-gate mutex_enter(&pidlock); 4237c478bd9Sstevel@tonic-gate for (pp = practive; pp != NULL; pp = pp->p_next) { 4247c478bd9Sstevel@tonic-gate mutex_enter(&pp->p_lock); 4257c478bd9Sstevel@tonic-gate tp = pp->p_tlist; 4267c478bd9Sstevel@tonic-gate /* 4277c478bd9Sstevel@tonic-gate * Skip zombies and kernel processes, and processes in 4287c478bd9Sstevel@tonic-gate * other zones, if called from a non-global zone. 4297c478bd9Sstevel@tonic-gate */ 4307c478bd9Sstevel@tonic-gate if (tp == NULL || (pp->p_flag & SSYS) || 4317c478bd9Sstevel@tonic-gate !HASZONEACCESS(curproc, pp->p_zone->zone_id)) { 4327c478bd9Sstevel@tonic-gate mutex_exit(&pp->p_lock); 4337c478bd9Sstevel@tonic-gate continue; 4347c478bd9Sstevel@tonic-gate } 4357c478bd9Sstevel@tonic-gate do { 4367c478bd9Sstevel@tonic-gate if ((idtype == P_PSETID && tp->t_bind_pset != pset) || 4377c478bd9Sstevel@tonic-gate (idtype == P_ALL && tp->t_bind_pset == PS_NONE)) 4387c478bd9Sstevel@tonic-gate continue; 4397c478bd9Sstevel@tonic-gate rval = pset_bind_thread(tp, PS_NONE, &olbind, 4407c478bd9Sstevel@tonic-gate projbuf, zonebuf); 4417c478bd9Sstevel@tonic-gate if (error == 0) 4427c478bd9Sstevel@tonic-gate error = rval; 4437c478bd9Sstevel@tonic-gate } while ((tp = tp->t_forw) != pp->p_tlist); 4447c478bd9Sstevel@tonic-gate mutex_exit(&pp->p_lock); 4457c478bd9Sstevel@tonic-gate } 4467c478bd9Sstevel@tonic-gate mutex_exit(&pidlock); 4477c478bd9Sstevel@tonic-gate return (error); 4487c478bd9Sstevel@tonic-gate } 4497c478bd9Sstevel@tonic-gate 4507c478bd9Sstevel@tonic-gate static int 4517c478bd9Sstevel@tonic-gate pset_bind_contract(cont_process_t *ctp, psetid_t pset, psetid_t *oldpset, 4527c478bd9Sstevel@tonic-gate void *projbuf, void *zonebuf) 4537c478bd9Sstevel@tonic-gate { 4547c478bd9Sstevel@tonic-gate int error = 0; 4557c478bd9Sstevel@tonic-gate proc_t *pp; 4567c478bd9Sstevel@tonic-gate 4577c478bd9Sstevel@tonic-gate ASSERT(MUTEX_HELD(&pidlock)); 4587c478bd9Sstevel@tonic-gate 4597c478bd9Sstevel@tonic-gate for (pp = practive; pp != NULL; pp = pp->p_next) { 4607c478bd9Sstevel@tonic-gate if (pp->p_ct_process == ctp) { 4617c478bd9Sstevel@tonic-gate int rval; 4627c478bd9Sstevel@tonic-gate 4637c478bd9Sstevel@tonic-gate rval = pset_bind_process(pp, pset, oldpset, projbuf, 4647c478bd9Sstevel@tonic-gate zonebuf); 4657c478bd9Sstevel@tonic-gate if (error == 0) 4667c478bd9Sstevel@tonic-gate error = rval; 4677c478bd9Sstevel@tonic-gate } 4687c478bd9Sstevel@tonic-gate } 4697c478bd9Sstevel@tonic-gate 4707c478bd9Sstevel@tonic-gate return (error); 4717c478bd9Sstevel@tonic-gate } 4727c478bd9Sstevel@tonic-gate 473*fb9b0aa8SSurya Prakki /* 474*fb9b0aa8SSurya Prakki * Bind the lwp:id of process:pid to processor set: pset 475*fb9b0aa8SSurya Prakki */ 476*fb9b0aa8SSurya Prakki static int 477*fb9b0aa8SSurya Prakki pset_bind_lwp(psetid_t pset, id_t id, pid_t pid, psetid_t *opset) 478*fb9b0aa8SSurya Prakki { 479*fb9b0aa8SSurya Prakki kthread_t *tp; 480*fb9b0aa8SSurya Prakki proc_t *pp; 481*fb9b0aa8SSurya Prakki psetid_t oldpset; 482*fb9b0aa8SSurya Prakki void *projbuf, *zonebuf; 483*fb9b0aa8SSurya Prakki int error = 0; 484*fb9b0aa8SSurya Prakki 485*fb9b0aa8SSurya Prakki pool_lock(); 486*fb9b0aa8SSurya Prakki mutex_enter(&cpu_lock); 487*fb9b0aa8SSurya Prakki projbuf = fss_allocbuf(FSS_NPROJ_BUF, FSS_ALLOC_PROJ); 488*fb9b0aa8SSurya Prakki zonebuf = fss_allocbuf(FSS_NPROJ_BUF, FSS_ALLOC_ZONE); 489*fb9b0aa8SSurya Prakki 490*fb9b0aa8SSurya Prakki mutex_enter(&pidlock); 491*fb9b0aa8SSurya Prakki if ((pid == P_MYID && id == P_MYID) || 492*fb9b0aa8SSurya Prakki (pid == curproc->p_pid && id == P_MYID)) { 493*fb9b0aa8SSurya Prakki pp = curproc; 494*fb9b0aa8SSurya Prakki tp = curthread; 495*fb9b0aa8SSurya Prakki mutex_enter(&pp->p_lock); 496*fb9b0aa8SSurya Prakki } else { 497*fb9b0aa8SSurya Prakki if (pid == P_MYID) { 498*fb9b0aa8SSurya Prakki pp = curproc; 499*fb9b0aa8SSurya Prakki } else if ((pp = prfind(pid)) == NULL) { 500*fb9b0aa8SSurya Prakki error = ESRCH; 501*fb9b0aa8SSurya Prakki goto err; 502*fb9b0aa8SSurya Prakki } 503*fb9b0aa8SSurya Prakki if (pp != curproc && id == P_MYID) { 504*fb9b0aa8SSurya Prakki error = EINVAL; 505*fb9b0aa8SSurya Prakki goto err; 506*fb9b0aa8SSurya Prakki } 507*fb9b0aa8SSurya Prakki mutex_enter(&pp->p_lock); 508*fb9b0aa8SSurya Prakki if ((tp = idtot(pp, id)) == NULL) { 509*fb9b0aa8SSurya Prakki mutex_exit(&pp->p_lock); 510*fb9b0aa8SSurya Prakki error = ESRCH; 511*fb9b0aa8SSurya Prakki goto err; 512*fb9b0aa8SSurya Prakki } 513*fb9b0aa8SSurya Prakki } 514*fb9b0aa8SSurya Prakki 515*fb9b0aa8SSurya Prakki error = pset_bind_thread(tp, pset, &oldpset, projbuf, zonebuf); 516*fb9b0aa8SSurya Prakki mutex_exit(&pp->p_lock); 517*fb9b0aa8SSurya Prakki err: 518*fb9b0aa8SSurya Prakki mutex_exit(&pidlock); 519*fb9b0aa8SSurya Prakki 520*fb9b0aa8SSurya Prakki fss_freebuf(projbuf, FSS_ALLOC_PROJ); 521*fb9b0aa8SSurya Prakki fss_freebuf(zonebuf, FSS_ALLOC_ZONE); 522*fb9b0aa8SSurya Prakki mutex_exit(&cpu_lock); 523*fb9b0aa8SSurya Prakki pool_unlock(); 524*fb9b0aa8SSurya Prakki if (opset != NULL) { 525*fb9b0aa8SSurya Prakki if (copyout(&oldpset, opset, sizeof (psetid_t)) != 0) 526*fb9b0aa8SSurya Prakki return (set_errno(EFAULT)); 527*fb9b0aa8SSurya Prakki } 528*fb9b0aa8SSurya Prakki if (error != 0) 529*fb9b0aa8SSurya Prakki return (set_errno(error)); 530*fb9b0aa8SSurya Prakki return (0); 531*fb9b0aa8SSurya Prakki } 532*fb9b0aa8SSurya Prakki 5337c478bd9Sstevel@tonic-gate static int 5347c478bd9Sstevel@tonic-gate pset_bind(psetid_t pset, idtype_t idtype, id_t id, psetid_t *opset) 5357c478bd9Sstevel@tonic-gate { 5367c478bd9Sstevel@tonic-gate kthread_t *tp; 5377c478bd9Sstevel@tonic-gate proc_t *pp; 5387c478bd9Sstevel@tonic-gate task_t *tk; 5397c478bd9Sstevel@tonic-gate kproject_t *kpj; 5407c478bd9Sstevel@tonic-gate contract_t *ct; 5417c478bd9Sstevel@tonic-gate zone_t *zptr; 5427c478bd9Sstevel@tonic-gate psetid_t oldpset; 5437c478bd9Sstevel@tonic-gate int error = 0; 5447c478bd9Sstevel@tonic-gate void *projbuf, *zonebuf; 5457c478bd9Sstevel@tonic-gate 5467c478bd9Sstevel@tonic-gate pool_lock(); 5470b70c467Sakolb if ((pset != PS_QUERY) && (pset != PS_SOFT) && 5480b70c467Sakolb (pset != PS_HARD) && (pset != PS_QUERY_TYPE)) { 5497c478bd9Sstevel@tonic-gate /* 5507c478bd9Sstevel@tonic-gate * Check if the set actually exists before checking 5517c478bd9Sstevel@tonic-gate * permissions. This is the historical error 5527c478bd9Sstevel@tonic-gate * precedence. Note that if pset was PS_MYID, the 5537c478bd9Sstevel@tonic-gate * cpupart_get_cpus call will change it to the 5547c478bd9Sstevel@tonic-gate * processor set id of the caller (or PS_NONE if the 5557c478bd9Sstevel@tonic-gate * caller is not bound to a processor set). 5567c478bd9Sstevel@tonic-gate */ 5577c478bd9Sstevel@tonic-gate if (pool_state == POOL_ENABLED) { 5587c478bd9Sstevel@tonic-gate pool_unlock(); 5597c478bd9Sstevel@tonic-gate return (set_errno(ENOTSUP)); 5607c478bd9Sstevel@tonic-gate } 5617c478bd9Sstevel@tonic-gate if (cpupart_get_cpus(&pset, NULL, NULL) != 0) { 5627c478bd9Sstevel@tonic-gate pool_unlock(); 5637c478bd9Sstevel@tonic-gate return (set_errno(EINVAL)); 5647c478bd9Sstevel@tonic-gate } else if (pset != PS_NONE && secpolicy_pset(CRED()) != 0) { 5657c478bd9Sstevel@tonic-gate pool_unlock(); 5667c478bd9Sstevel@tonic-gate return (set_errno(EPERM)); 5677c478bd9Sstevel@tonic-gate } 5687c478bd9Sstevel@tonic-gate } 5697c478bd9Sstevel@tonic-gate 5707c478bd9Sstevel@tonic-gate /* 5717c478bd9Sstevel@tonic-gate * Pre-allocate enough buffers for FSS for all active projects 5727c478bd9Sstevel@tonic-gate * and for all active zones on the system. Unused buffers will 5737c478bd9Sstevel@tonic-gate * be freed later by fss_freebuf(). 5747c478bd9Sstevel@tonic-gate */ 5757c478bd9Sstevel@tonic-gate mutex_enter(&cpu_lock); 5767c478bd9Sstevel@tonic-gate projbuf = fss_allocbuf(FSS_NPROJ_BUF, FSS_ALLOC_PROJ); 5777c478bd9Sstevel@tonic-gate zonebuf = fss_allocbuf(FSS_NPROJ_BUF, FSS_ALLOC_ZONE); 5787c478bd9Sstevel@tonic-gate 5797c478bd9Sstevel@tonic-gate switch (idtype) { 5807c478bd9Sstevel@tonic-gate case P_LWPID: 5817c478bd9Sstevel@tonic-gate pp = curproc; 5827c478bd9Sstevel@tonic-gate mutex_enter(&pidlock); 5837c478bd9Sstevel@tonic-gate mutex_enter(&pp->p_lock); 5847c478bd9Sstevel@tonic-gate if (id == P_MYID) { 5857c478bd9Sstevel@tonic-gate tp = curthread; 5867c478bd9Sstevel@tonic-gate } else { 5877c478bd9Sstevel@tonic-gate if ((tp = idtot(pp, id)) == NULL) { 5887c478bd9Sstevel@tonic-gate mutex_exit(&pp->p_lock); 5897c478bd9Sstevel@tonic-gate mutex_exit(&pidlock); 5907c478bd9Sstevel@tonic-gate error = ESRCH; 5917c478bd9Sstevel@tonic-gate break; 5927c478bd9Sstevel@tonic-gate } 5937c478bd9Sstevel@tonic-gate } 5947c478bd9Sstevel@tonic-gate error = pset_bind_thread(tp, pset, &oldpset, projbuf, zonebuf); 5957c478bd9Sstevel@tonic-gate mutex_exit(&pp->p_lock); 5967c478bd9Sstevel@tonic-gate mutex_exit(&pidlock); 5977c478bd9Sstevel@tonic-gate break; 5987c478bd9Sstevel@tonic-gate 5997c478bd9Sstevel@tonic-gate case P_PID: 6007c478bd9Sstevel@tonic-gate mutex_enter(&pidlock); 6017c478bd9Sstevel@tonic-gate if (id == P_MYID) { 6027c478bd9Sstevel@tonic-gate pp = curproc; 6037c478bd9Sstevel@tonic-gate } else if ((pp = prfind(id)) == NULL) { 6047c478bd9Sstevel@tonic-gate mutex_exit(&pidlock); 6057c478bd9Sstevel@tonic-gate error = ESRCH; 6067c478bd9Sstevel@tonic-gate break; 6077c478bd9Sstevel@tonic-gate } 6087c478bd9Sstevel@tonic-gate error = pset_bind_process(pp, pset, &oldpset, projbuf, zonebuf); 6097c478bd9Sstevel@tonic-gate mutex_exit(&pidlock); 6107c478bd9Sstevel@tonic-gate break; 6117c478bd9Sstevel@tonic-gate 6127c478bd9Sstevel@tonic-gate case P_TASKID: 6137c478bd9Sstevel@tonic-gate mutex_enter(&pidlock); 6147c478bd9Sstevel@tonic-gate if (id == P_MYID) 6157c478bd9Sstevel@tonic-gate id = curproc->p_task->tk_tkid; 6167c478bd9Sstevel@tonic-gate if ((tk = task_hold_by_id(id)) == NULL) { 6177c478bd9Sstevel@tonic-gate mutex_exit(&pidlock); 6187c478bd9Sstevel@tonic-gate error = ESRCH; 6197c478bd9Sstevel@tonic-gate break; 6207c478bd9Sstevel@tonic-gate } 6217c478bd9Sstevel@tonic-gate error = pset_bind_task(tk, pset, &oldpset, projbuf, zonebuf); 6227c478bd9Sstevel@tonic-gate mutex_exit(&pidlock); 6237c478bd9Sstevel@tonic-gate task_rele(tk); 6247c478bd9Sstevel@tonic-gate break; 6257c478bd9Sstevel@tonic-gate 6267c478bd9Sstevel@tonic-gate case P_PROJID: 6270209230bSgjelinek pp = curproc; 6287c478bd9Sstevel@tonic-gate if (id == P_MYID) 6297c478bd9Sstevel@tonic-gate id = curprojid(); 6300209230bSgjelinek if ((kpj = project_hold_by_id(id, pp->p_zone, 6317c478bd9Sstevel@tonic-gate PROJECT_HOLD_FIND)) == NULL) { 6327c478bd9Sstevel@tonic-gate error = ESRCH; 6337c478bd9Sstevel@tonic-gate break; 6347c478bd9Sstevel@tonic-gate } 6357c478bd9Sstevel@tonic-gate mutex_enter(&pidlock); 6367c478bd9Sstevel@tonic-gate error = pset_bind_project(kpj, pset, &oldpset, projbuf, 6377c478bd9Sstevel@tonic-gate zonebuf); 6387c478bd9Sstevel@tonic-gate mutex_exit(&pidlock); 6397c478bd9Sstevel@tonic-gate project_rele(kpj); 6407c478bd9Sstevel@tonic-gate break; 6417c478bd9Sstevel@tonic-gate 6427c478bd9Sstevel@tonic-gate case P_ZONEID: 6437c478bd9Sstevel@tonic-gate if (id == P_MYID) 6447c478bd9Sstevel@tonic-gate id = getzoneid(); 6457c478bd9Sstevel@tonic-gate if ((zptr = zone_find_by_id(id)) == NULL) { 6467c478bd9Sstevel@tonic-gate error = ESRCH; 6477c478bd9Sstevel@tonic-gate break; 6487c478bd9Sstevel@tonic-gate } 6497c478bd9Sstevel@tonic-gate mutex_enter(&pidlock); 6507c478bd9Sstevel@tonic-gate error = pset_bind_zone(zptr, pset, &oldpset, projbuf, zonebuf); 6517c478bd9Sstevel@tonic-gate mutex_exit(&pidlock); 6527c478bd9Sstevel@tonic-gate zone_rele(zptr); 6537c478bd9Sstevel@tonic-gate break; 6547c478bd9Sstevel@tonic-gate 6557c478bd9Sstevel@tonic-gate case P_CTID: 6567c478bd9Sstevel@tonic-gate if (id == P_MYID) 6577c478bd9Sstevel@tonic-gate id = PRCTID(curproc); 6587c478bd9Sstevel@tonic-gate if ((ct = contract_type_ptr(process_type, id, 6597c478bd9Sstevel@tonic-gate curproc->p_zone->zone_uniqid)) == NULL) { 6607c478bd9Sstevel@tonic-gate error = ESRCH; 6617c478bd9Sstevel@tonic-gate break; 6627c478bd9Sstevel@tonic-gate } 6637c478bd9Sstevel@tonic-gate mutex_enter(&pidlock); 6647c478bd9Sstevel@tonic-gate error = pset_bind_contract(ct->ct_data, pset, &oldpset, projbuf, 6657c478bd9Sstevel@tonic-gate zonebuf); 6667c478bd9Sstevel@tonic-gate mutex_exit(&pidlock); 6677c478bd9Sstevel@tonic-gate contract_rele(ct); 6687c478bd9Sstevel@tonic-gate break; 6697c478bd9Sstevel@tonic-gate 6707c478bd9Sstevel@tonic-gate case P_PSETID: 6717c478bd9Sstevel@tonic-gate if (id == P_MYID || pset != PS_NONE || !INGLOBALZONE(curproc)) { 6727c478bd9Sstevel@tonic-gate error = EINVAL; 6737c478bd9Sstevel@tonic-gate break; 6747c478bd9Sstevel@tonic-gate } 6757c478bd9Sstevel@tonic-gate error = pset_unbind(id, projbuf, zonebuf, idtype); 6767c478bd9Sstevel@tonic-gate break; 6777c478bd9Sstevel@tonic-gate 6787c478bd9Sstevel@tonic-gate case P_ALL: 6797c478bd9Sstevel@tonic-gate if (id == P_MYID || pset != PS_NONE || !INGLOBALZONE(curproc)) { 6807c478bd9Sstevel@tonic-gate error = EINVAL; 6817c478bd9Sstevel@tonic-gate break; 6827c478bd9Sstevel@tonic-gate } 6837c478bd9Sstevel@tonic-gate error = pset_unbind(PS_NONE, projbuf, zonebuf, idtype); 6847c478bd9Sstevel@tonic-gate break; 6857c478bd9Sstevel@tonic-gate 6867c478bd9Sstevel@tonic-gate default: 6877c478bd9Sstevel@tonic-gate error = EINVAL; 6887c478bd9Sstevel@tonic-gate break; 6897c478bd9Sstevel@tonic-gate } 6907c478bd9Sstevel@tonic-gate 6917c478bd9Sstevel@tonic-gate fss_freebuf(projbuf, FSS_ALLOC_PROJ); 6927c478bd9Sstevel@tonic-gate fss_freebuf(zonebuf, FSS_ALLOC_ZONE); 6937c478bd9Sstevel@tonic-gate mutex_exit(&cpu_lock); 6947c478bd9Sstevel@tonic-gate pool_unlock(); 6957c478bd9Sstevel@tonic-gate 6967c478bd9Sstevel@tonic-gate if (error != 0) 6977c478bd9Sstevel@tonic-gate return (set_errno(error)); 6987c478bd9Sstevel@tonic-gate if (opset != NULL) { 6997c478bd9Sstevel@tonic-gate if (copyout(&oldpset, opset, sizeof (psetid_t)) != 0) 7007c478bd9Sstevel@tonic-gate return (set_errno(EFAULT)); 7017c478bd9Sstevel@tonic-gate } 7027c478bd9Sstevel@tonic-gate return (0); 7037c478bd9Sstevel@tonic-gate } 7047c478bd9Sstevel@tonic-gate 7057c478bd9Sstevel@tonic-gate /* 7067c478bd9Sstevel@tonic-gate * Report load average statistics for the specified processor set. 7077c478bd9Sstevel@tonic-gate */ 7087c478bd9Sstevel@tonic-gate static int 7097c478bd9Sstevel@tonic-gate pset_getloadavg(psetid_t pset, int *buf, int nelem) 7107c478bd9Sstevel@tonic-gate { 711d5fbc238Sfr157268 int loadbuf[LOADAVG_NSTATS]; 7127c478bd9Sstevel@tonic-gate int error = 0; 7137c478bd9Sstevel@tonic-gate 7147c478bd9Sstevel@tonic-gate if (nelem < 0) 7157c478bd9Sstevel@tonic-gate return (set_errno(EINVAL)); 7167c478bd9Sstevel@tonic-gate 7177c478bd9Sstevel@tonic-gate /* 7187c478bd9Sstevel@tonic-gate * We keep the same number of load average statistics for processor 7197c478bd9Sstevel@tonic-gate * sets as we do for the system as a whole. 7207c478bd9Sstevel@tonic-gate */ 7217c478bd9Sstevel@tonic-gate if (nelem > LOADAVG_NSTATS) 7227c478bd9Sstevel@tonic-gate nelem = LOADAVG_NSTATS; 7237c478bd9Sstevel@tonic-gate 7247c478bd9Sstevel@tonic-gate mutex_enter(&cpu_lock); 7257c478bd9Sstevel@tonic-gate error = cpupart_get_loadavg(pset, loadbuf, nelem); 7267c478bd9Sstevel@tonic-gate mutex_exit(&cpu_lock); 7277c478bd9Sstevel@tonic-gate if (!error && nelem && copyout(loadbuf, buf, nelem * sizeof (int)) != 0) 7287c478bd9Sstevel@tonic-gate error = EFAULT; 7297c478bd9Sstevel@tonic-gate 7307c478bd9Sstevel@tonic-gate if (error) 7317c478bd9Sstevel@tonic-gate return (set_errno(error)); 7327c478bd9Sstevel@tonic-gate else 7337c478bd9Sstevel@tonic-gate return (0); 7347c478bd9Sstevel@tonic-gate } 7357c478bd9Sstevel@tonic-gate 7367c478bd9Sstevel@tonic-gate 7377c478bd9Sstevel@tonic-gate /* 7387c478bd9Sstevel@tonic-gate * Return list of active processor sets, up to a maximum indicated by 7397c478bd9Sstevel@tonic-gate * numpsets. The total number of processor sets is stored in the 7407c478bd9Sstevel@tonic-gate * location pointed to by numpsets. 7417c478bd9Sstevel@tonic-gate */ 7427c478bd9Sstevel@tonic-gate static int 7437c478bd9Sstevel@tonic-gate pset_list(psetid_t *psetlist, uint_t *numpsets) 7447c478bd9Sstevel@tonic-gate { 7457c478bd9Sstevel@tonic-gate uint_t user_npsets = 0; 7467c478bd9Sstevel@tonic-gate uint_t real_npsets; 7477c478bd9Sstevel@tonic-gate psetid_t *psets = NULL; 7487c478bd9Sstevel@tonic-gate int error = 0; 7497c478bd9Sstevel@tonic-gate 7507c478bd9Sstevel@tonic-gate if (numpsets != NULL) { 7517c478bd9Sstevel@tonic-gate if (copyin(numpsets, &user_npsets, sizeof (uint_t)) != 0) 7527c478bd9Sstevel@tonic-gate return (set_errno(EFAULT)); 7537c478bd9Sstevel@tonic-gate } 7547c478bd9Sstevel@tonic-gate 7557c478bd9Sstevel@tonic-gate /* 7567c478bd9Sstevel@tonic-gate * Get the list of all processor sets. First we need to find 7577c478bd9Sstevel@tonic-gate * out how many there are, so we can allocate a large enough 7587c478bd9Sstevel@tonic-gate * buffer. 7597c478bd9Sstevel@tonic-gate */ 7607c478bd9Sstevel@tonic-gate mutex_enter(&cpu_lock); 7617c478bd9Sstevel@tonic-gate if (!INGLOBALZONE(curproc) && pool_pset_enabled()) { 7627c478bd9Sstevel@tonic-gate psetid_t psetid = zone_pset_get(curproc->p_zone); 7637c478bd9Sstevel@tonic-gate 7647c478bd9Sstevel@tonic-gate if (psetid == PS_NONE) { 7657c478bd9Sstevel@tonic-gate real_npsets = 0; 7667c478bd9Sstevel@tonic-gate } else { 7677c478bd9Sstevel@tonic-gate real_npsets = 1; 7687c478bd9Sstevel@tonic-gate psets = kmem_alloc(real_npsets * sizeof (psetid_t), 7697c478bd9Sstevel@tonic-gate KM_SLEEP); 7707c478bd9Sstevel@tonic-gate psets[0] = psetid; 7717c478bd9Sstevel@tonic-gate } 7727c478bd9Sstevel@tonic-gate } else { 7737c478bd9Sstevel@tonic-gate real_npsets = cpupart_list(0, NULL, CP_ALL); 7747c478bd9Sstevel@tonic-gate if (real_npsets) { 7757c478bd9Sstevel@tonic-gate psets = kmem_alloc(real_npsets * sizeof (psetid_t), 7767c478bd9Sstevel@tonic-gate KM_SLEEP); 7777c478bd9Sstevel@tonic-gate (void) cpupart_list(psets, real_npsets, CP_ALL); 7787c478bd9Sstevel@tonic-gate } 7797c478bd9Sstevel@tonic-gate } 7807c478bd9Sstevel@tonic-gate mutex_exit(&cpu_lock); 7817c478bd9Sstevel@tonic-gate 7827c478bd9Sstevel@tonic-gate if (user_npsets > real_npsets) 7837c478bd9Sstevel@tonic-gate user_npsets = real_npsets; 7847c478bd9Sstevel@tonic-gate 7857c478bd9Sstevel@tonic-gate if (numpsets != NULL) { 7867c478bd9Sstevel@tonic-gate if (copyout(&real_npsets, numpsets, sizeof (uint_t)) != 0) 7877c478bd9Sstevel@tonic-gate error = EFAULT; 7887c478bd9Sstevel@tonic-gate else if (psetlist != NULL && user_npsets != 0) { 7897c478bd9Sstevel@tonic-gate if (copyout(psets, psetlist, 7907c478bd9Sstevel@tonic-gate user_npsets * sizeof (psetid_t)) != 0) 7917c478bd9Sstevel@tonic-gate error = EFAULT; 7927c478bd9Sstevel@tonic-gate } 7937c478bd9Sstevel@tonic-gate } 7947c478bd9Sstevel@tonic-gate 7957c478bd9Sstevel@tonic-gate if (real_npsets) 7967c478bd9Sstevel@tonic-gate kmem_free(psets, real_npsets * sizeof (psetid_t)); 7977c478bd9Sstevel@tonic-gate 7987c478bd9Sstevel@tonic-gate if (error) 7997c478bd9Sstevel@tonic-gate return (set_errno(error)); 8007c478bd9Sstevel@tonic-gate else 8017c478bd9Sstevel@tonic-gate return (0); 8027c478bd9Sstevel@tonic-gate } 8037c478bd9Sstevel@tonic-gate 8047c478bd9Sstevel@tonic-gate static int 8057c478bd9Sstevel@tonic-gate pset_setattr(psetid_t pset, uint_t attr) 8067c478bd9Sstevel@tonic-gate { 8077c478bd9Sstevel@tonic-gate int error; 8087c478bd9Sstevel@tonic-gate 8097c478bd9Sstevel@tonic-gate if (secpolicy_pset(CRED()) != 0) 8107c478bd9Sstevel@tonic-gate return (set_errno(EPERM)); 8117c478bd9Sstevel@tonic-gate pool_lock(); 8127c478bd9Sstevel@tonic-gate if (pool_state == POOL_ENABLED) { 8137c478bd9Sstevel@tonic-gate pool_unlock(); 8147c478bd9Sstevel@tonic-gate return (set_errno(ENOTSUP)); 8157c478bd9Sstevel@tonic-gate } 8167c478bd9Sstevel@tonic-gate if (pset == PS_QUERY || PSET_BADATTR(attr)) { 8177c478bd9Sstevel@tonic-gate pool_unlock(); 8187c478bd9Sstevel@tonic-gate return (set_errno(EINVAL)); 8197c478bd9Sstevel@tonic-gate } 8207c478bd9Sstevel@tonic-gate if ((error = cpupart_setattr(pset, attr)) != 0) { 8217c478bd9Sstevel@tonic-gate pool_unlock(); 8227c478bd9Sstevel@tonic-gate return (set_errno(error)); 8237c478bd9Sstevel@tonic-gate } 8247c478bd9Sstevel@tonic-gate pool_unlock(); 8257c478bd9Sstevel@tonic-gate return (0); 8267c478bd9Sstevel@tonic-gate } 8277c478bd9Sstevel@tonic-gate 8287c478bd9Sstevel@tonic-gate static int 8297c478bd9Sstevel@tonic-gate pset_getattr(psetid_t pset, uint_t *attrp) 8307c478bd9Sstevel@tonic-gate { 8317c478bd9Sstevel@tonic-gate int error = 0; 8327c478bd9Sstevel@tonic-gate uint_t attr; 8337c478bd9Sstevel@tonic-gate 8347c478bd9Sstevel@tonic-gate if (pset == PS_QUERY) 8357c478bd9Sstevel@tonic-gate return (set_errno(EINVAL)); 8367c478bd9Sstevel@tonic-gate if ((error = cpupart_getattr(pset, &attr)) != 0) 8377c478bd9Sstevel@tonic-gate return (set_errno(error)); 8387c478bd9Sstevel@tonic-gate if (copyout(&attr, attrp, sizeof (uint_t)) != 0) 8397c478bd9Sstevel@tonic-gate return (set_errno(EFAULT)); 8407c478bd9Sstevel@tonic-gate return (0); 8417c478bd9Sstevel@tonic-gate } 8427c478bd9Sstevel@tonic-gate 8437c478bd9Sstevel@tonic-gate static int 8447c478bd9Sstevel@tonic-gate pset(int subcode, long arg1, long arg2, long arg3, long arg4) 8457c478bd9Sstevel@tonic-gate { 8467c478bd9Sstevel@tonic-gate switch (subcode) { 8477c478bd9Sstevel@tonic-gate case PSET_CREATE: 8487c478bd9Sstevel@tonic-gate return (pset_create((psetid_t *)arg1)); 8497c478bd9Sstevel@tonic-gate case PSET_DESTROY: 8507c478bd9Sstevel@tonic-gate return (pset_destroy((psetid_t)arg1)); 8517c478bd9Sstevel@tonic-gate case PSET_ASSIGN: 8527c478bd9Sstevel@tonic-gate return (pset_assign((psetid_t)arg1, 8537c478bd9Sstevel@tonic-gate (processorid_t)arg2, (psetid_t *)arg3, 0)); 8547c478bd9Sstevel@tonic-gate case PSET_INFO: 8557c478bd9Sstevel@tonic-gate return (pset_info((psetid_t)arg1, (int *)arg2, 8567c478bd9Sstevel@tonic-gate (uint_t *)arg3, (processorid_t *)arg4)); 8577c478bd9Sstevel@tonic-gate case PSET_BIND: 8587c478bd9Sstevel@tonic-gate return (pset_bind((psetid_t)arg1, (idtype_t)arg2, 8597c478bd9Sstevel@tonic-gate (id_t)arg3, (psetid_t *)arg4)); 860*fb9b0aa8SSurya Prakki case PSET_BIND_LWP: 861*fb9b0aa8SSurya Prakki return (pset_bind_lwp((psetid_t)arg1, (id_t)arg2, 862*fb9b0aa8SSurya Prakki (pid_t)arg3, (psetid_t *)arg4)); 8637c478bd9Sstevel@tonic-gate case PSET_GETLOADAVG: 8647c478bd9Sstevel@tonic-gate return (pset_getloadavg((psetid_t)arg1, (int *)arg2, 8657c478bd9Sstevel@tonic-gate (int)arg3)); 8667c478bd9Sstevel@tonic-gate case PSET_LIST: 8677c478bd9Sstevel@tonic-gate return (pset_list((psetid_t *)arg1, (uint_t *)arg2)); 8687c478bd9Sstevel@tonic-gate case PSET_SETATTR: 8697c478bd9Sstevel@tonic-gate return (pset_setattr((psetid_t)arg1, (uint_t)arg2)); 8707c478bd9Sstevel@tonic-gate case PSET_GETATTR: 8717c478bd9Sstevel@tonic-gate return (pset_getattr((psetid_t)arg1, (uint_t *)arg2)); 8727c478bd9Sstevel@tonic-gate case PSET_ASSIGN_FORCED: 8737c478bd9Sstevel@tonic-gate return (pset_assign((psetid_t)arg1, 8747c478bd9Sstevel@tonic-gate (processorid_t)arg2, (psetid_t *)arg3, 1)); 8757c478bd9Sstevel@tonic-gate default: 8767c478bd9Sstevel@tonic-gate return (set_errno(EINVAL)); 8777c478bd9Sstevel@tonic-gate } 8787c478bd9Sstevel@tonic-gate } 879