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
53247Sgjelinek * Common Development and Distribution License (the "License").
63247Sgjelinek * 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*9150SGangadhar.M@Sun.COM * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
230Sstevel@tonic-gate * Use is subject to license terms.
240Sstevel@tonic-gate */
250Sstevel@tonic-gate
260Sstevel@tonic-gate #include <sys/types.h>
270Sstevel@tonic-gate #include <sys/param.h>
280Sstevel@tonic-gate #include <sys/var.h>
290Sstevel@tonic-gate #include <sys/thread.h>
300Sstevel@tonic-gate #include <sys/cpuvar.h>
310Sstevel@tonic-gate #include <sys/kstat.h>
320Sstevel@tonic-gate #include <sys/uadmin.h>
330Sstevel@tonic-gate #include <sys/systm.h>
340Sstevel@tonic-gate #include <sys/errno.h>
350Sstevel@tonic-gate #include <sys/cmn_err.h>
360Sstevel@tonic-gate #include <sys/procset.h>
370Sstevel@tonic-gate #include <sys/processor.h>
380Sstevel@tonic-gate #include <sys/debug.h>
390Sstevel@tonic-gate #include <sys/task.h>
400Sstevel@tonic-gate #include <sys/project.h>
410Sstevel@tonic-gate #include <sys/zone.h>
420Sstevel@tonic-gate #include <sys/contract_impl.h>
430Sstevel@tonic-gate #include <sys/contract/process_impl.h>
440Sstevel@tonic-gate
450Sstevel@tonic-gate /*
460Sstevel@tonic-gate * Bind all the threads of a process to a CPU.
470Sstevel@tonic-gate */
480Sstevel@tonic-gate static int
cpu_bind_process(proc_t * pp,processorid_t bind,processorid_t * obind,int * error)490Sstevel@tonic-gate cpu_bind_process(proc_t *pp, processorid_t bind, processorid_t *obind,
500Sstevel@tonic-gate int *error)
510Sstevel@tonic-gate {
520Sstevel@tonic-gate kthread_t *tp;
530Sstevel@tonic-gate kthread_t *fp;
540Sstevel@tonic-gate int err = 0;
550Sstevel@tonic-gate int i;
560Sstevel@tonic-gate
570Sstevel@tonic-gate ASSERT(MUTEX_HELD(&pidlock));
580Sstevel@tonic-gate
590Sstevel@tonic-gate /* skip kernel processes */
600Sstevel@tonic-gate if (pp->p_flag & SSYS) {
610Sstevel@tonic-gate *obind = PBIND_NONE;
62*9150SGangadhar.M@Sun.COM *error = ENOTSUP;
630Sstevel@tonic-gate return (0);
640Sstevel@tonic-gate }
650Sstevel@tonic-gate
660Sstevel@tonic-gate mutex_enter(&pp->p_lock);
670Sstevel@tonic-gate tp = pp->p_tlist;
680Sstevel@tonic-gate if (tp != NULL) {
690Sstevel@tonic-gate fp = tp;
700Sstevel@tonic-gate do {
710Sstevel@tonic-gate i = cpu_bind_thread(tp, bind, obind, error);
720Sstevel@tonic-gate if (err == 0)
730Sstevel@tonic-gate err = i;
740Sstevel@tonic-gate } while ((tp = tp->t_forw) != fp);
750Sstevel@tonic-gate }
760Sstevel@tonic-gate
770Sstevel@tonic-gate mutex_exit(&pp->p_lock);
780Sstevel@tonic-gate return (err);
790Sstevel@tonic-gate }
800Sstevel@tonic-gate
810Sstevel@tonic-gate /*
820Sstevel@tonic-gate * Bind all the processes of a task to a CPU.
830Sstevel@tonic-gate */
840Sstevel@tonic-gate static int
cpu_bind_task(task_t * tk,processorid_t bind,processorid_t * obind,int * error)850Sstevel@tonic-gate cpu_bind_task(task_t *tk, processorid_t bind, processorid_t *obind,
860Sstevel@tonic-gate int *error)
870Sstevel@tonic-gate {
880Sstevel@tonic-gate proc_t *p;
890Sstevel@tonic-gate int err = 0;
900Sstevel@tonic-gate int i;
910Sstevel@tonic-gate
920Sstevel@tonic-gate ASSERT(MUTEX_HELD(&pidlock));
930Sstevel@tonic-gate
940Sstevel@tonic-gate if ((p = tk->tk_memb_list) == NULL)
950Sstevel@tonic-gate return (ESRCH);
960Sstevel@tonic-gate
970Sstevel@tonic-gate do {
98*9150SGangadhar.M@Sun.COM if (!(p->p_flag & SSYS)) {
99*9150SGangadhar.M@Sun.COM i = cpu_bind_process(p, bind, obind, error);
100*9150SGangadhar.M@Sun.COM if (err == 0)
101*9150SGangadhar.M@Sun.COM err = i;
102*9150SGangadhar.M@Sun.COM }
1030Sstevel@tonic-gate } while ((p = p->p_tasknext) != tk->tk_memb_list);
1040Sstevel@tonic-gate
1050Sstevel@tonic-gate return (err);
1060Sstevel@tonic-gate }
1070Sstevel@tonic-gate
1080Sstevel@tonic-gate /*
1090Sstevel@tonic-gate * Bind all the processes in a project to a CPU.
1100Sstevel@tonic-gate */
1110Sstevel@tonic-gate static int
cpu_bind_project(kproject_t * kpj,processorid_t bind,processorid_t * obind,int * error)1120Sstevel@tonic-gate cpu_bind_project(kproject_t *kpj, processorid_t bind, processorid_t *obind,
1130Sstevel@tonic-gate int *error)
1140Sstevel@tonic-gate {
1150Sstevel@tonic-gate proc_t *p;
1160Sstevel@tonic-gate int err = 0;
1170Sstevel@tonic-gate int i;
1180Sstevel@tonic-gate
1190Sstevel@tonic-gate ASSERT(MUTEX_HELD(&pidlock));
1200Sstevel@tonic-gate
1210Sstevel@tonic-gate for (p = practive; p != NULL; p = p->p_next) {
1220Sstevel@tonic-gate if (p->p_tlist == NULL)
1230Sstevel@tonic-gate continue;
124*9150SGangadhar.M@Sun.COM if (p->p_task->tk_proj == kpj && !(p->p_flag & SSYS)) {
1250Sstevel@tonic-gate i = cpu_bind_process(p, bind, obind, error);
1260Sstevel@tonic-gate if (err == 0)
1270Sstevel@tonic-gate err = i;
1280Sstevel@tonic-gate }
1290Sstevel@tonic-gate }
1300Sstevel@tonic-gate return (err);
1310Sstevel@tonic-gate }
1320Sstevel@tonic-gate
1330Sstevel@tonic-gate /*
1340Sstevel@tonic-gate * Bind all the processes in a zone to a CPU.
1350Sstevel@tonic-gate */
1360Sstevel@tonic-gate int
cpu_bind_zone(zone_t * zptr,processorid_t bind,processorid_t * obind,int * error)1370Sstevel@tonic-gate cpu_bind_zone(zone_t *zptr, processorid_t bind, processorid_t *obind,
1380Sstevel@tonic-gate int *error)
1390Sstevel@tonic-gate {
1400Sstevel@tonic-gate proc_t *p;
1410Sstevel@tonic-gate int err = 0;
1420Sstevel@tonic-gate int i;
1430Sstevel@tonic-gate
1440Sstevel@tonic-gate ASSERT(MUTEX_HELD(&pidlock));
1450Sstevel@tonic-gate
1460Sstevel@tonic-gate for (p = practive; p != NULL; p = p->p_next) {
1470Sstevel@tonic-gate if (p->p_tlist == NULL)
1480Sstevel@tonic-gate continue;
149*9150SGangadhar.M@Sun.COM if (p->p_zone == zptr && !(p->p_flag & SSYS)) {
1500Sstevel@tonic-gate i = cpu_bind_process(p, bind, obind, error);
1510Sstevel@tonic-gate if (err == 0)
1520Sstevel@tonic-gate err = i;
1530Sstevel@tonic-gate }
1540Sstevel@tonic-gate }
1550Sstevel@tonic-gate return (err);
1560Sstevel@tonic-gate }
1570Sstevel@tonic-gate
1580Sstevel@tonic-gate /*
1590Sstevel@tonic-gate * Bind all the processes in a process contract to a CPU.
1600Sstevel@tonic-gate */
1610Sstevel@tonic-gate int
cpu_bind_contract(cont_process_t * ctp,processorid_t bind,processorid_t * obind,int * error)1620Sstevel@tonic-gate cpu_bind_contract(cont_process_t *ctp, processorid_t bind, processorid_t *obind,
1630Sstevel@tonic-gate int *error)
1640Sstevel@tonic-gate {
1650Sstevel@tonic-gate proc_t *p;
1660Sstevel@tonic-gate int err = 0;
1670Sstevel@tonic-gate int i;
1680Sstevel@tonic-gate
1690Sstevel@tonic-gate ASSERT(MUTEX_HELD(&pidlock));
1700Sstevel@tonic-gate
1710Sstevel@tonic-gate for (p = practive; p != NULL; p = p->p_next) {
1720Sstevel@tonic-gate if (p->p_tlist == NULL)
1730Sstevel@tonic-gate continue;
1740Sstevel@tonic-gate if (p->p_ct_process == ctp) {
1750Sstevel@tonic-gate i = cpu_bind_process(p, bind, obind, error);
1760Sstevel@tonic-gate if (err == 0)
1770Sstevel@tonic-gate err = i;
1780Sstevel@tonic-gate }
1790Sstevel@tonic-gate }
1800Sstevel@tonic-gate return (err);
1810Sstevel@tonic-gate }
1820Sstevel@tonic-gate
1830Sstevel@tonic-gate /*
1840Sstevel@tonic-gate * processor_bind(2) - Processor binding interfaces.
1850Sstevel@tonic-gate */
1860Sstevel@tonic-gate int
processor_bind(idtype_t idtype,id_t id,processorid_t bind,processorid_t * obindp)1870Sstevel@tonic-gate processor_bind(idtype_t idtype, id_t id, processorid_t bind,
1880Sstevel@tonic-gate processorid_t *obindp)
1890Sstevel@tonic-gate {
1900Sstevel@tonic-gate processorid_t obind = PBIND_NONE;
1910Sstevel@tonic-gate int ret = 0;
1920Sstevel@tonic-gate int err = 0;
1930Sstevel@tonic-gate cpu_t *cp;
1940Sstevel@tonic-gate kthread_id_t tp;
1950Sstevel@tonic-gate proc_t *pp;
1960Sstevel@tonic-gate task_t *tk;
1970Sstevel@tonic-gate kproject_t *kpj;
1980Sstevel@tonic-gate zone_t *zptr;
1990Sstevel@tonic-gate contract_t *ct;
2000Sstevel@tonic-gate
2010Sstevel@tonic-gate /*
2020Sstevel@tonic-gate * Since we might be making a binding to a processor, hold the
2030Sstevel@tonic-gate * cpu_lock so that the processor cannot be taken offline while
2040Sstevel@tonic-gate * we do this.
2050Sstevel@tonic-gate */
2060Sstevel@tonic-gate mutex_enter(&cpu_lock);
2070Sstevel@tonic-gate
2080Sstevel@tonic-gate /*
2090Sstevel@tonic-gate * Check to be sure binding processor ID is valid.
2100Sstevel@tonic-gate */
2110Sstevel@tonic-gate switch (bind) {
2120Sstevel@tonic-gate default:
2130Sstevel@tonic-gate if ((cp = cpu_get(bind)) == NULL ||
2140Sstevel@tonic-gate (cp->cpu_flags & (CPU_QUIESCED | CPU_OFFLINE)))
2150Sstevel@tonic-gate ret = EINVAL;
2160Sstevel@tonic-gate else if ((cp->cpu_flags & CPU_READY) == 0)
2170Sstevel@tonic-gate ret = EIO;
2180Sstevel@tonic-gate break;
2190Sstevel@tonic-gate
2200Sstevel@tonic-gate case PBIND_NONE:
2210Sstevel@tonic-gate case PBIND_QUERY:
2226298Sakolb case PBIND_HARD:
2236298Sakolb case PBIND_SOFT:
2246298Sakolb case PBIND_QUERY_TYPE:
2250Sstevel@tonic-gate break;
2260Sstevel@tonic-gate }
2270Sstevel@tonic-gate
2280Sstevel@tonic-gate if (ret) {
2290Sstevel@tonic-gate mutex_exit(&cpu_lock);
2300Sstevel@tonic-gate return (set_errno(ret));
2310Sstevel@tonic-gate }
2320Sstevel@tonic-gate
2330Sstevel@tonic-gate switch (idtype) {
2340Sstevel@tonic-gate case P_LWPID:
2350Sstevel@tonic-gate pp = curproc;
2360Sstevel@tonic-gate mutex_enter(&pp->p_lock);
2370Sstevel@tonic-gate if (id == P_MYID) {
2380Sstevel@tonic-gate ret = cpu_bind_thread(curthread, bind, &obind, &err);
2390Sstevel@tonic-gate } else {
2400Sstevel@tonic-gate int found = 0;
2410Sstevel@tonic-gate
2420Sstevel@tonic-gate tp = pp->p_tlist;
2430Sstevel@tonic-gate do {
2440Sstevel@tonic-gate if (tp->t_tid == id) {
2450Sstevel@tonic-gate ret = cpu_bind_thread(tp,
2460Sstevel@tonic-gate bind, &obind, &err);
2470Sstevel@tonic-gate found = 1;
2480Sstevel@tonic-gate break;
2490Sstevel@tonic-gate }
2500Sstevel@tonic-gate } while ((tp = tp->t_forw) != pp->p_tlist);
2510Sstevel@tonic-gate if (!found)
2520Sstevel@tonic-gate ret = ESRCH;
2530Sstevel@tonic-gate }
2540Sstevel@tonic-gate mutex_exit(&pp->p_lock);
2550Sstevel@tonic-gate break;
2560Sstevel@tonic-gate
2570Sstevel@tonic-gate case P_PID:
2580Sstevel@tonic-gate /*
2590Sstevel@tonic-gate * Note. Cannot use dotoprocs here because it doesn't find
2600Sstevel@tonic-gate * system class processes, which are legal to query.
2610Sstevel@tonic-gate */
2620Sstevel@tonic-gate mutex_enter(&pidlock);
2630Sstevel@tonic-gate if (id == P_MYID) {
2640Sstevel@tonic-gate ret = cpu_bind_process(curproc, bind, &obind, &err);
2650Sstevel@tonic-gate } else if ((pp = prfind(id)) != NULL) {
2660Sstevel@tonic-gate ret = cpu_bind_process(pp, bind, &obind, &err);
2670Sstevel@tonic-gate } else {
2680Sstevel@tonic-gate ret = ESRCH;
2690Sstevel@tonic-gate }
2700Sstevel@tonic-gate mutex_exit(&pidlock);
2710Sstevel@tonic-gate break;
2720Sstevel@tonic-gate
2730Sstevel@tonic-gate case P_TASKID:
2740Sstevel@tonic-gate mutex_enter(&pidlock);
2750Sstevel@tonic-gate if (id == P_MYID) {
2760Sstevel@tonic-gate proc_t *p = curproc;
2770Sstevel@tonic-gate id = p->p_task->tk_tkid;
2780Sstevel@tonic-gate }
2790Sstevel@tonic-gate
2800Sstevel@tonic-gate if ((tk = task_hold_by_id(id)) != NULL) {
2810Sstevel@tonic-gate ret = cpu_bind_task(tk, bind, &obind, &err);
2820Sstevel@tonic-gate mutex_exit(&pidlock);
2830Sstevel@tonic-gate task_rele(tk);
2840Sstevel@tonic-gate } else {
2850Sstevel@tonic-gate mutex_exit(&pidlock);
2860Sstevel@tonic-gate ret = ESRCH;
2870Sstevel@tonic-gate }
2880Sstevel@tonic-gate break;
2890Sstevel@tonic-gate
2900Sstevel@tonic-gate case P_PROJID:
2913247Sgjelinek pp = curproc;
2920Sstevel@tonic-gate if (id == P_MYID)
2930Sstevel@tonic-gate id = curprojid();
2943247Sgjelinek if ((kpj = project_hold_by_id(id, pp->p_zone,
2950Sstevel@tonic-gate PROJECT_HOLD_FIND)) == NULL) {
2960Sstevel@tonic-gate ret = ESRCH;
2970Sstevel@tonic-gate } else {
2980Sstevel@tonic-gate mutex_enter(&pidlock);
2990Sstevel@tonic-gate ret = cpu_bind_project(kpj, bind, &obind, &err);
3000Sstevel@tonic-gate mutex_exit(&pidlock);
3010Sstevel@tonic-gate project_rele(kpj);
3020Sstevel@tonic-gate }
3030Sstevel@tonic-gate break;
3040Sstevel@tonic-gate
3050Sstevel@tonic-gate case P_ZONEID:
3060Sstevel@tonic-gate if (id == P_MYID)
3070Sstevel@tonic-gate id = getzoneid();
3080Sstevel@tonic-gate
3090Sstevel@tonic-gate if ((zptr = zone_find_by_id(id)) == NULL) {
3100Sstevel@tonic-gate ret = ESRCH;
3110Sstevel@tonic-gate } else {
3120Sstevel@tonic-gate mutex_enter(&pidlock);
3130Sstevel@tonic-gate ret = cpu_bind_zone(zptr, bind, &obind, &err);
3140Sstevel@tonic-gate mutex_exit(&pidlock);
3150Sstevel@tonic-gate zone_rele(zptr);
3160Sstevel@tonic-gate }
3170Sstevel@tonic-gate break;
3180Sstevel@tonic-gate
3190Sstevel@tonic-gate case P_CTID:
3200Sstevel@tonic-gate if (id == P_MYID)
3210Sstevel@tonic-gate id = PRCTID(curproc);
3220Sstevel@tonic-gate
3230Sstevel@tonic-gate if ((ct = contract_type_ptr(process_type, id,
3240Sstevel@tonic-gate curproc->p_zone->zone_uniqid)) == NULL) {
3250Sstevel@tonic-gate ret = ESRCH;
3260Sstevel@tonic-gate } else {
3270Sstevel@tonic-gate mutex_enter(&pidlock);
3280Sstevel@tonic-gate ret = cpu_bind_contract(ct->ct_data,
3290Sstevel@tonic-gate bind, &obind, &err);
3300Sstevel@tonic-gate mutex_exit(&pidlock);
3310Sstevel@tonic-gate contract_rele(ct);
3320Sstevel@tonic-gate }
3330Sstevel@tonic-gate break;
3340Sstevel@tonic-gate
3350Sstevel@tonic-gate case P_CPUID:
3360Sstevel@tonic-gate if (id == P_MYID || bind != PBIND_NONE || cpu_get(id) == NULL)
3370Sstevel@tonic-gate ret = EINVAL;
3380Sstevel@tonic-gate else
3396298Sakolb ret = cpu_unbind(id, B_TRUE);
3400Sstevel@tonic-gate break;
3410Sstevel@tonic-gate
3420Sstevel@tonic-gate case P_ALL:
3430Sstevel@tonic-gate if (id == P_MYID || bind != PBIND_NONE) {
3440Sstevel@tonic-gate ret = EINVAL;
3450Sstevel@tonic-gate } else {
3460Sstevel@tonic-gate int i;
3470Sstevel@tonic-gate cpu_t *cp = cpu_list;
3480Sstevel@tonic-gate do {
3490Sstevel@tonic-gate if ((cp->cpu_flags & CPU_EXISTS) == 0)
3500Sstevel@tonic-gate continue;
3516298Sakolb i = cpu_unbind(cp->cpu_id, B_TRUE);
3520Sstevel@tonic-gate if (ret == 0)
3530Sstevel@tonic-gate ret = i;
3540Sstevel@tonic-gate } while ((cp = cp->cpu_next) != cpu_list);
3550Sstevel@tonic-gate }
3560Sstevel@tonic-gate break;
3570Sstevel@tonic-gate
3580Sstevel@tonic-gate default:
3590Sstevel@tonic-gate /*
3600Sstevel@tonic-gate * Spec says this is invalid, even though we could
3610Sstevel@tonic-gate * handle other idtypes.
3620Sstevel@tonic-gate */
3630Sstevel@tonic-gate ret = EINVAL;
3640Sstevel@tonic-gate break;
3650Sstevel@tonic-gate }
3660Sstevel@tonic-gate mutex_exit(&cpu_lock);
3670Sstevel@tonic-gate
3680Sstevel@tonic-gate /*
3690Sstevel@tonic-gate * If no search error occurred, see if any permissions errors did.
3700Sstevel@tonic-gate */
3710Sstevel@tonic-gate if (ret == 0)
3720Sstevel@tonic-gate ret = err;
3730Sstevel@tonic-gate
3740Sstevel@tonic-gate if (ret == 0 && obindp != NULL)
3750Sstevel@tonic-gate if (copyout((caddr_t)&obind, (caddr_t)obindp,
3760Sstevel@tonic-gate sizeof (obind)) == -1)
3770Sstevel@tonic-gate ret = EFAULT;
3780Sstevel@tonic-gate return (ret ? set_errno(ret) : 0); /* return success or failure */
3790Sstevel@tonic-gate }
380