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
52712Snn35248 * Common Development and Distribution License (the "License").
62712Snn35248 * 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 */
216247Sraf
220Sstevel@tonic-gate /*
236247Sraf * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
240Sstevel@tonic-gate * Use is subject to license terms.
250Sstevel@tonic-gate */
260Sstevel@tonic-gate
270Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
280Sstevel@tonic-gate
290Sstevel@tonic-gate #include <sys/types.h>
300Sstevel@tonic-gate #include <sys/systm.h>
310Sstevel@tonic-gate #include <sys/cmn_err.h>
320Sstevel@tonic-gate #include <sys/class.h>
330Sstevel@tonic-gate #include <sys/kmem.h>
340Sstevel@tonic-gate #include <sys/cred.h>
350Sstevel@tonic-gate #include <sys/proc.h>
360Sstevel@tonic-gate #include <sys/procset.h>
370Sstevel@tonic-gate #include <sys/modctl.h>
380Sstevel@tonic-gate #include <sys/disp.h>
390Sstevel@tonic-gate #include <sys/sysmacros.h>
406247Sraf #include <sys/schedctl.h>
410Sstevel@tonic-gate
420Sstevel@tonic-gate static int getcidbyname_locked(char *, id_t *);
430Sstevel@tonic-gate
440Sstevel@tonic-gate /*
450Sstevel@tonic-gate * Allocate a cid given a class name if one is not already allocated.
460Sstevel@tonic-gate * Returns 0 if the cid was already exists or if the allocation of a new
470Sstevel@tonic-gate * cid was successful. Nonzero return indicates error.
480Sstevel@tonic-gate */
490Sstevel@tonic-gate int
alloc_cid(char * clname,id_t * cidp)500Sstevel@tonic-gate alloc_cid(char *clname, id_t *cidp)
510Sstevel@tonic-gate {
520Sstevel@tonic-gate sclass_t *clp;
530Sstevel@tonic-gate
540Sstevel@tonic-gate ASSERT(MUTEX_HELD(&class_lock));
550Sstevel@tonic-gate
560Sstevel@tonic-gate /*
570Sstevel@tonic-gate * If the clname doesn't already have a cid, allocate one.
580Sstevel@tonic-gate */
590Sstevel@tonic-gate if (getcidbyname_locked(clname, cidp) != 0) {
600Sstevel@tonic-gate /*
610Sstevel@tonic-gate * Allocate a class entry and a lock for it.
620Sstevel@tonic-gate */
630Sstevel@tonic-gate for (clp = sclass; clp < &sclass[nclass]; clp++)
640Sstevel@tonic-gate if (clp->cl_name[0] == '\0' && clp->cl_lock == NULL)
650Sstevel@tonic-gate break;
660Sstevel@tonic-gate
670Sstevel@tonic-gate if (clp == &sclass[nclass]) {
680Sstevel@tonic-gate return (ENOSPC);
690Sstevel@tonic-gate }
700Sstevel@tonic-gate *cidp = clp - &sclass[0];
710Sstevel@tonic-gate clp->cl_lock = kmem_alloc(sizeof (krwlock_t), KM_SLEEP);
720Sstevel@tonic-gate clp->cl_name = kmem_alloc(strlen(clname) + 1, KM_SLEEP);
730Sstevel@tonic-gate (void) strcpy(clp->cl_name, clname);
740Sstevel@tonic-gate rw_init(clp->cl_lock, NULL, RW_DEFAULT, NULL);
750Sstevel@tonic-gate }
760Sstevel@tonic-gate
770Sstevel@tonic-gate /*
780Sstevel@tonic-gate * At this point, *cidp will contain the index into the class
790Sstevel@tonic-gate * array for the given class name.
800Sstevel@tonic-gate */
810Sstevel@tonic-gate return (0);
820Sstevel@tonic-gate }
830Sstevel@tonic-gate
840Sstevel@tonic-gate int
scheduler_load(char * clname,sclass_t * clp)850Sstevel@tonic-gate scheduler_load(char *clname, sclass_t *clp)
860Sstevel@tonic-gate {
870Sstevel@tonic-gate int rv = 0;
88*9180SKrishnendu.Sadhukhan@Sun.COM char *tmp = clname + 1;
89*9180SKrishnendu.Sadhukhan@Sun.COM
90*9180SKrishnendu.Sadhukhan@Sun.COM /* Check if class name is "", ".", ".." or "`" */
91*9180SKrishnendu.Sadhukhan@Sun.COM if (*clname == '\0' || *clname == '`' || (*clname == '.' && *tmp == '\0') ||
92*9180SKrishnendu.Sadhukhan@Sun.COM (*clname == '.' && *tmp == '.' && *(++tmp) == '\0'))
93*9180SKrishnendu.Sadhukhan@Sun.COM return (EINVAL);
940Sstevel@tonic-gate
950Sstevel@tonic-gate if (LOADABLE_SCHED(clp)) {
960Sstevel@tonic-gate rw_enter(clp->cl_lock, RW_READER);
970Sstevel@tonic-gate if (!SCHED_INSTALLED(clp)) {
980Sstevel@tonic-gate rw_exit(clp->cl_lock);
990Sstevel@tonic-gate if (modload("sched", clname) == -1)
1000Sstevel@tonic-gate return (EINVAL);
1010Sstevel@tonic-gate rw_enter(clp->cl_lock, RW_READER);
1020Sstevel@tonic-gate /* did we really load a scheduling class? */
1030Sstevel@tonic-gate if (!SCHED_INSTALLED(clp))
1040Sstevel@tonic-gate rv = EINVAL;
1050Sstevel@tonic-gate }
1060Sstevel@tonic-gate rw_exit(clp->cl_lock);
1070Sstevel@tonic-gate }
1080Sstevel@tonic-gate return (rv);
1090Sstevel@tonic-gate }
1100Sstevel@tonic-gate
1110Sstevel@tonic-gate /*
1120Sstevel@tonic-gate * Get class ID given class name.
1130Sstevel@tonic-gate */
1140Sstevel@tonic-gate int
getcid(char * clname,id_t * cidp)1150Sstevel@tonic-gate getcid(char *clname, id_t *cidp)
1160Sstevel@tonic-gate {
1170Sstevel@tonic-gate sclass_t *clp;
1180Sstevel@tonic-gate int retval;
1190Sstevel@tonic-gate
1200Sstevel@tonic-gate mutex_enter(&class_lock);
1210Sstevel@tonic-gate if ((retval = alloc_cid(clname, cidp)) == 0) {
1220Sstevel@tonic-gate clp = &sclass[*cidp];
1230Sstevel@tonic-gate clp->cl_count++;
1240Sstevel@tonic-gate
1250Sstevel@tonic-gate /*
1260Sstevel@tonic-gate * If it returns zero, it's loaded & locked
1270Sstevel@tonic-gate * or we found a statically installed scheduler
1280Sstevel@tonic-gate * module.
1290Sstevel@tonic-gate * If it returns EINVAL, modload() failed when
1300Sstevel@tonic-gate * it tried to load the module.
1310Sstevel@tonic-gate */
1320Sstevel@tonic-gate mutex_exit(&class_lock);
1330Sstevel@tonic-gate retval = scheduler_load(clname, clp);
1340Sstevel@tonic-gate mutex_enter(&class_lock);
1350Sstevel@tonic-gate
1360Sstevel@tonic-gate clp->cl_count--;
1370Sstevel@tonic-gate if (retval != 0 && clp->cl_count == 0) {
1380Sstevel@tonic-gate /* last guy out of scheduler_load frees the storage */
1390Sstevel@tonic-gate kmem_free(clp->cl_name, strlen(clname) + 1);
1400Sstevel@tonic-gate kmem_free(clp->cl_lock, sizeof (krwlock_t));
1410Sstevel@tonic-gate clp->cl_name = "";
1420Sstevel@tonic-gate clp->cl_lock = (krwlock_t *)NULL;
1430Sstevel@tonic-gate }
1440Sstevel@tonic-gate }
1450Sstevel@tonic-gate mutex_exit(&class_lock);
1460Sstevel@tonic-gate return (retval);
1470Sstevel@tonic-gate
1480Sstevel@tonic-gate }
1490Sstevel@tonic-gate
1500Sstevel@tonic-gate static int
getcidbyname_locked(char * clname,id_t * cidp)1510Sstevel@tonic-gate getcidbyname_locked(char *clname, id_t *cidp)
1520Sstevel@tonic-gate {
1530Sstevel@tonic-gate sclass_t *clp;
1540Sstevel@tonic-gate
1550Sstevel@tonic-gate ASSERT(MUTEX_HELD(&class_lock));
1560Sstevel@tonic-gate
1570Sstevel@tonic-gate if (*clname == NULL)
1580Sstevel@tonic-gate return (EINVAL);
1590Sstevel@tonic-gate
1600Sstevel@tonic-gate for (clp = &sclass[0]; clp < &sclass[nclass]; clp++) {
1610Sstevel@tonic-gate if (strcmp(clp->cl_name, clname) == 0) {
1620Sstevel@tonic-gate *cidp = clp - &sclass[0];
1630Sstevel@tonic-gate return (0);
1640Sstevel@tonic-gate }
1650Sstevel@tonic-gate }
1660Sstevel@tonic-gate return (EINVAL);
1670Sstevel@tonic-gate }
1680Sstevel@tonic-gate
1690Sstevel@tonic-gate /*
1700Sstevel@tonic-gate * Lookup a module by name.
1710Sstevel@tonic-gate */
1720Sstevel@tonic-gate int
getcidbyname(char * clname,id_t * cidp)1730Sstevel@tonic-gate getcidbyname(char *clname, id_t *cidp)
1740Sstevel@tonic-gate {
1750Sstevel@tonic-gate int retval;
1760Sstevel@tonic-gate
1770Sstevel@tonic-gate mutex_enter(&class_lock);
1780Sstevel@tonic-gate retval = getcidbyname_locked(clname, cidp);
1790Sstevel@tonic-gate mutex_exit(&class_lock);
1800Sstevel@tonic-gate
1810Sstevel@tonic-gate return (retval);
1820Sstevel@tonic-gate }
1830Sstevel@tonic-gate
1840Sstevel@tonic-gate /*
1850Sstevel@tonic-gate * Get the scheduling parameters of the thread pointed to by
1860Sstevel@tonic-gate * tp into the buffer pointed to by parmsp.
1870Sstevel@tonic-gate */
1880Sstevel@tonic-gate void
parmsget(kthread_t * tp,pcparms_t * parmsp)1896247Sraf parmsget(kthread_t *tp, pcparms_t *parmsp)
1900Sstevel@tonic-gate {
1910Sstevel@tonic-gate parmsp->pc_cid = tp->t_cid;
1920Sstevel@tonic-gate CL_PARMSGET(tp, parmsp->pc_clparms);
1930Sstevel@tonic-gate }
1940Sstevel@tonic-gate
1950Sstevel@tonic-gate
1960Sstevel@tonic-gate /*
1970Sstevel@tonic-gate * Check the validity of the scheduling parameters in the buffer
1980Sstevel@tonic-gate * pointed to by parmsp.
1990Sstevel@tonic-gate * Note that the format of the parameters may be changed by class
2000Sstevel@tonic-gate * specific code which we call.
2010Sstevel@tonic-gate */
2020Sstevel@tonic-gate int
parmsin(pcparms_t * parmsp,pc_vaparms_t * vaparmsp)2030Sstevel@tonic-gate parmsin(pcparms_t *parmsp, pc_vaparms_t *vaparmsp)
2040Sstevel@tonic-gate {
2050Sstevel@tonic-gate if (parmsp->pc_cid >= loaded_classes || parmsp->pc_cid < 1)
2060Sstevel@tonic-gate return (EINVAL);
2070Sstevel@tonic-gate
2080Sstevel@tonic-gate /*
2090Sstevel@tonic-gate * Call the class specific routine to validate class
2100Sstevel@tonic-gate * specific parameters.
2110Sstevel@tonic-gate * The input parameters are either in a pcparms structure (PC_SETPARMS)
2120Sstevel@tonic-gate * or in a variable parameter structure (PC_SETXPARMS). In the
2130Sstevel@tonic-gate * 'PC_SETPARMS' case vaparmsp is a NULL pointer and a CL_PARMSIN()
2140Sstevel@tonic-gate * routine gets the parameter. Otherwise vaparmsp points to a variable
2150Sstevel@tonic-gate * parameter structure and a CL_VAPARMSIN() routine gets the parameter.
2160Sstevel@tonic-gate */
2170Sstevel@tonic-gate if (vaparmsp != NULL)
2180Sstevel@tonic-gate return (CL_VAPARMSIN(&sclass[parmsp->pc_cid],
2190Sstevel@tonic-gate parmsp->pc_clparms, vaparmsp));
2200Sstevel@tonic-gate else
2210Sstevel@tonic-gate return (CL_PARMSIN(&sclass[parmsp->pc_cid],
2220Sstevel@tonic-gate parmsp->pc_clparms));
2230Sstevel@tonic-gate }
2240Sstevel@tonic-gate
2250Sstevel@tonic-gate
2260Sstevel@tonic-gate /*
2270Sstevel@tonic-gate * Call the class specific code to do the required processing
2280Sstevel@tonic-gate * before the scheduling parameters are copied out to the user.
2290Sstevel@tonic-gate * Note that the format of the parameters may be changed by the
2300Sstevel@tonic-gate * class specific code.
2310Sstevel@tonic-gate */
2320Sstevel@tonic-gate int
parmsout(pcparms_t * parmsp,pc_vaparms_t * vaparmsp)2330Sstevel@tonic-gate parmsout(pcparms_t *parmsp, pc_vaparms_t *vaparmsp)
2340Sstevel@tonic-gate {
2350Sstevel@tonic-gate return (CL_PARMSOUT(&sclass[parmsp->pc_cid], parmsp->pc_clparms,
2366247Sraf vaparmsp));
2370Sstevel@tonic-gate }
2380Sstevel@tonic-gate
2390Sstevel@tonic-gate
2400Sstevel@tonic-gate /*
2410Sstevel@tonic-gate * Set the scheduling parameters of the thread pointed to by
2420Sstevel@tonic-gate * targtp to those specified in the pcparms structure pointed
2430Sstevel@tonic-gate * to by parmsp. If reqtp is non-NULL it points to the thread
2440Sstevel@tonic-gate * that initiated the request for the parameter change and indicates
2450Sstevel@tonic-gate * that our caller wants us to verify that the requesting thread
2460Sstevel@tonic-gate * has the appropriate permissions.
2470Sstevel@tonic-gate */
2480Sstevel@tonic-gate int
parmsset(pcparms_t * parmsp,kthread_t * targtp)2496247Sraf parmsset(pcparms_t *parmsp, kthread_t *targtp)
2500Sstevel@tonic-gate {
2510Sstevel@tonic-gate caddr_t clprocp;
2520Sstevel@tonic-gate int error;
2530Sstevel@tonic-gate cred_t *reqpcredp;
2540Sstevel@tonic-gate proc_t *reqpp = ttoproc(curthread);
2550Sstevel@tonic-gate proc_t *targpp = ttoproc(targtp);
2560Sstevel@tonic-gate id_t oldcid;
2570Sstevel@tonic-gate
2580Sstevel@tonic-gate ASSERT(MUTEX_HELD(&pidlock));
2590Sstevel@tonic-gate ASSERT(MUTEX_HELD(&targpp->p_lock));
2600Sstevel@tonic-gate if (reqpp != NULL) {
2610Sstevel@tonic-gate mutex_enter(&reqpp->p_crlock);
2620Sstevel@tonic-gate crhold(reqpcredp = reqpp->p_cred);
2630Sstevel@tonic-gate mutex_exit(&reqpp->p_crlock);
2640Sstevel@tonic-gate
2650Sstevel@tonic-gate /*
2660Sstevel@tonic-gate * Check basic permissions.
2670Sstevel@tonic-gate */
2680Sstevel@tonic-gate if (!prochasprocperm(targpp, reqpp, reqpcredp)) {
2690Sstevel@tonic-gate crfree(reqpcredp);
2700Sstevel@tonic-gate return (EPERM);
2710Sstevel@tonic-gate }
2720Sstevel@tonic-gate } else {
2730Sstevel@tonic-gate reqpcredp = NULL;
2740Sstevel@tonic-gate }
2750Sstevel@tonic-gate
2760Sstevel@tonic-gate if (parmsp->pc_cid != targtp->t_cid) {
2770Sstevel@tonic-gate void *bufp = NULL;
2780Sstevel@tonic-gate /*
2790Sstevel@tonic-gate * Target thread must change to new class.
2800Sstevel@tonic-gate */
2810Sstevel@tonic-gate clprocp = (caddr_t)targtp->t_cldata;
2820Sstevel@tonic-gate oldcid = targtp->t_cid;
2830Sstevel@tonic-gate
2840Sstevel@tonic-gate /*
2850Sstevel@tonic-gate * Purpose: allow scheduling class to veto moves
2860Sstevel@tonic-gate * to other classes. All the classes, except FSS,
2870Sstevel@tonic-gate * do nothing except returning 0.
2880Sstevel@tonic-gate */
2890Sstevel@tonic-gate error = CL_CANEXIT(targtp, reqpcredp);
2900Sstevel@tonic-gate if (error) {
2910Sstevel@tonic-gate /*
2920Sstevel@tonic-gate * Not allowed to leave the class, so return error.
2930Sstevel@tonic-gate */
2940Sstevel@tonic-gate crfree(reqpcredp);
2950Sstevel@tonic-gate return (error);
2960Sstevel@tonic-gate } else {
2970Sstevel@tonic-gate /*
2980Sstevel@tonic-gate * Pre-allocate scheduling class data.
2990Sstevel@tonic-gate */
3000Sstevel@tonic-gate if (CL_ALLOC(&bufp, parmsp->pc_cid, KM_NOSLEEP) != 0) {
3010Sstevel@tonic-gate error = ENOMEM; /* no memory available */
3020Sstevel@tonic-gate crfree(reqpcredp);
3030Sstevel@tonic-gate return (error);
3040Sstevel@tonic-gate } else {
3050Sstevel@tonic-gate error = CL_ENTERCLASS(targtp, parmsp->pc_cid,
3060Sstevel@tonic-gate parmsp->pc_clparms, reqpcredp, bufp);
3070Sstevel@tonic-gate crfree(reqpcredp);
3080Sstevel@tonic-gate if (error) {
3090Sstevel@tonic-gate CL_FREE(parmsp->pc_cid, bufp);
3100Sstevel@tonic-gate return (error);
3110Sstevel@tonic-gate }
3120Sstevel@tonic-gate }
3130Sstevel@tonic-gate }
3140Sstevel@tonic-gate CL_EXITCLASS(oldcid, clprocp);
3150Sstevel@tonic-gate } else {
3160Sstevel@tonic-gate
3170Sstevel@tonic-gate /*
3180Sstevel@tonic-gate * Not changing class
3190Sstevel@tonic-gate */
3200Sstevel@tonic-gate error = CL_PARMSSET(targtp, parmsp->pc_clparms,
3216247Sraf curthread->t_cid, reqpcredp);
3220Sstevel@tonic-gate crfree(reqpcredp);
3230Sstevel@tonic-gate if (error)
3240Sstevel@tonic-gate return (error);
3250Sstevel@tonic-gate }
3266247Sraf schedctl_set_cidpri(targtp);
3270Sstevel@tonic-gate return (0);
3280Sstevel@tonic-gate }
3290Sstevel@tonic-gate
3300Sstevel@tonic-gate
3310Sstevel@tonic-gate /*
3320Sstevel@tonic-gate * Copy all selected class parameters to the user.
3330Sstevel@tonic-gate * The parameters are specified by a key.
3340Sstevel@tonic-gate */
3350Sstevel@tonic-gate int
vaparmsout(char * classp,pcparms_t * prmsp,pc_vaparms_t * vaparmsp,uio_seg_t seg)3362712Snn35248 vaparmsout(char *classp, pcparms_t *prmsp, pc_vaparms_t *vaparmsp,
3372712Snn35248 uio_seg_t seg)
3380Sstevel@tonic-gate {
3390Sstevel@tonic-gate char *clname;
3400Sstevel@tonic-gate
3410Sstevel@tonic-gate ASSERT(MUTEX_NOT_HELD(&curproc->p_lock));
3420Sstevel@tonic-gate
3430Sstevel@tonic-gate if (classp != NULL)
3440Sstevel@tonic-gate return (CL_VAPARMSOUT(&sclass[prmsp->pc_cid],
3450Sstevel@tonic-gate prmsp->pc_clparms, vaparmsp));
3460Sstevel@tonic-gate
3470Sstevel@tonic-gate switch (vaparmsp->pc_vaparmscnt) {
3480Sstevel@tonic-gate case 0:
3490Sstevel@tonic-gate return (0);
3500Sstevel@tonic-gate case 1:
3510Sstevel@tonic-gate break;
3520Sstevel@tonic-gate default:
3530Sstevel@tonic-gate return (EINVAL);
3540Sstevel@tonic-gate }
3550Sstevel@tonic-gate
3560Sstevel@tonic-gate if (vaparmsp->pc_parms[0].pc_key != PC_KY_CLNAME)
3570Sstevel@tonic-gate return (EINVAL);
3580Sstevel@tonic-gate
3590Sstevel@tonic-gate clname = sclass[prmsp->pc_cid].cl_name;
3602712Snn35248 if ((seg == UIO_USERSPACE ? copyout : kcopy)(clname,
3612712Snn35248 (void *)(uintptr_t)vaparmsp->pc_parms[0].pc_parm,
3620Sstevel@tonic-gate MIN(strlen(clname) + 1, PC_CLNMSZ)))
3630Sstevel@tonic-gate return (EFAULT);
3640Sstevel@tonic-gate
3650Sstevel@tonic-gate return (0);
3660Sstevel@tonic-gate }
367