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 */ 21*6247Sraf 220Sstevel@tonic-gate /* 23*6247Sraf * 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> 40*6247Sraf #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 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 850Sstevel@tonic-gate scheduler_load(char *clname, sclass_t *clp) 860Sstevel@tonic-gate { 870Sstevel@tonic-gate int rv = 0; 880Sstevel@tonic-gate 890Sstevel@tonic-gate if (LOADABLE_SCHED(clp)) { 900Sstevel@tonic-gate rw_enter(clp->cl_lock, RW_READER); 910Sstevel@tonic-gate if (!SCHED_INSTALLED(clp)) { 920Sstevel@tonic-gate rw_exit(clp->cl_lock); 930Sstevel@tonic-gate if (modload("sched", clname) == -1) 940Sstevel@tonic-gate return (EINVAL); 950Sstevel@tonic-gate rw_enter(clp->cl_lock, RW_READER); 960Sstevel@tonic-gate /* did we really load a scheduling class? */ 970Sstevel@tonic-gate if (!SCHED_INSTALLED(clp)) 980Sstevel@tonic-gate rv = EINVAL; 990Sstevel@tonic-gate } 1000Sstevel@tonic-gate rw_exit(clp->cl_lock); 1010Sstevel@tonic-gate } 1020Sstevel@tonic-gate return (rv); 1030Sstevel@tonic-gate } 1040Sstevel@tonic-gate 1050Sstevel@tonic-gate /* 1060Sstevel@tonic-gate * Get class ID given class name. 1070Sstevel@tonic-gate */ 1080Sstevel@tonic-gate int 1090Sstevel@tonic-gate getcid(char *clname, id_t *cidp) 1100Sstevel@tonic-gate { 1110Sstevel@tonic-gate sclass_t *clp; 1120Sstevel@tonic-gate int retval; 1130Sstevel@tonic-gate 1140Sstevel@tonic-gate mutex_enter(&class_lock); 1150Sstevel@tonic-gate if ((retval = alloc_cid(clname, cidp)) == 0) { 1160Sstevel@tonic-gate clp = &sclass[*cidp]; 1170Sstevel@tonic-gate clp->cl_count++; 1180Sstevel@tonic-gate 1190Sstevel@tonic-gate /* 1200Sstevel@tonic-gate * If it returns zero, it's loaded & locked 1210Sstevel@tonic-gate * or we found a statically installed scheduler 1220Sstevel@tonic-gate * module. 1230Sstevel@tonic-gate * If it returns EINVAL, modload() failed when 1240Sstevel@tonic-gate * it tried to load the module. 1250Sstevel@tonic-gate */ 1260Sstevel@tonic-gate mutex_exit(&class_lock); 1270Sstevel@tonic-gate retval = scheduler_load(clname, clp); 1280Sstevel@tonic-gate mutex_enter(&class_lock); 1290Sstevel@tonic-gate 1300Sstevel@tonic-gate clp->cl_count--; 1310Sstevel@tonic-gate if (retval != 0 && clp->cl_count == 0) { 1320Sstevel@tonic-gate /* last guy out of scheduler_load frees the storage */ 1330Sstevel@tonic-gate kmem_free(clp->cl_name, strlen(clname) + 1); 1340Sstevel@tonic-gate kmem_free(clp->cl_lock, sizeof (krwlock_t)); 1350Sstevel@tonic-gate clp->cl_name = ""; 1360Sstevel@tonic-gate clp->cl_lock = (krwlock_t *)NULL; 1370Sstevel@tonic-gate } 1380Sstevel@tonic-gate } 1390Sstevel@tonic-gate mutex_exit(&class_lock); 1400Sstevel@tonic-gate return (retval); 1410Sstevel@tonic-gate 1420Sstevel@tonic-gate } 1430Sstevel@tonic-gate 1440Sstevel@tonic-gate static int 1450Sstevel@tonic-gate getcidbyname_locked(char *clname, id_t *cidp) 1460Sstevel@tonic-gate { 1470Sstevel@tonic-gate sclass_t *clp; 1480Sstevel@tonic-gate 1490Sstevel@tonic-gate ASSERT(MUTEX_HELD(&class_lock)); 1500Sstevel@tonic-gate 1510Sstevel@tonic-gate if (*clname == NULL) 1520Sstevel@tonic-gate return (EINVAL); 1530Sstevel@tonic-gate 1540Sstevel@tonic-gate for (clp = &sclass[0]; clp < &sclass[nclass]; clp++) { 1550Sstevel@tonic-gate if (strcmp(clp->cl_name, clname) == 0) { 1560Sstevel@tonic-gate *cidp = clp - &sclass[0]; 1570Sstevel@tonic-gate return (0); 1580Sstevel@tonic-gate } 1590Sstevel@tonic-gate } 1600Sstevel@tonic-gate return (EINVAL); 1610Sstevel@tonic-gate } 1620Sstevel@tonic-gate 1630Sstevel@tonic-gate /* 1640Sstevel@tonic-gate * Lookup a module by name. 1650Sstevel@tonic-gate */ 1660Sstevel@tonic-gate int 1670Sstevel@tonic-gate getcidbyname(char *clname, id_t *cidp) 1680Sstevel@tonic-gate { 1690Sstevel@tonic-gate int retval; 1700Sstevel@tonic-gate 1710Sstevel@tonic-gate mutex_enter(&class_lock); 1720Sstevel@tonic-gate retval = getcidbyname_locked(clname, cidp); 1730Sstevel@tonic-gate mutex_exit(&class_lock); 1740Sstevel@tonic-gate 1750Sstevel@tonic-gate return (retval); 1760Sstevel@tonic-gate } 1770Sstevel@tonic-gate 1780Sstevel@tonic-gate /* 1790Sstevel@tonic-gate * Get the scheduling parameters of the thread pointed to by 1800Sstevel@tonic-gate * tp into the buffer pointed to by parmsp. 1810Sstevel@tonic-gate */ 1820Sstevel@tonic-gate void 183*6247Sraf parmsget(kthread_t *tp, pcparms_t *parmsp) 1840Sstevel@tonic-gate { 1850Sstevel@tonic-gate parmsp->pc_cid = tp->t_cid; 1860Sstevel@tonic-gate CL_PARMSGET(tp, parmsp->pc_clparms); 1870Sstevel@tonic-gate } 1880Sstevel@tonic-gate 1890Sstevel@tonic-gate 1900Sstevel@tonic-gate /* 1910Sstevel@tonic-gate * Check the validity of the scheduling parameters in the buffer 1920Sstevel@tonic-gate * pointed to by parmsp. 1930Sstevel@tonic-gate * Note that the format of the parameters may be changed by class 1940Sstevel@tonic-gate * specific code which we call. 1950Sstevel@tonic-gate */ 1960Sstevel@tonic-gate int 1970Sstevel@tonic-gate parmsin(pcparms_t *parmsp, pc_vaparms_t *vaparmsp) 1980Sstevel@tonic-gate { 1990Sstevel@tonic-gate if (parmsp->pc_cid >= loaded_classes || parmsp->pc_cid < 1) 2000Sstevel@tonic-gate return (EINVAL); 2010Sstevel@tonic-gate 2020Sstevel@tonic-gate /* 2030Sstevel@tonic-gate * Call the class specific routine to validate class 2040Sstevel@tonic-gate * specific parameters. 2050Sstevel@tonic-gate * The input parameters are either in a pcparms structure (PC_SETPARMS) 2060Sstevel@tonic-gate * or in a variable parameter structure (PC_SETXPARMS). In the 2070Sstevel@tonic-gate * 'PC_SETPARMS' case vaparmsp is a NULL pointer and a CL_PARMSIN() 2080Sstevel@tonic-gate * routine gets the parameter. Otherwise vaparmsp points to a variable 2090Sstevel@tonic-gate * parameter structure and a CL_VAPARMSIN() routine gets the parameter. 2100Sstevel@tonic-gate */ 2110Sstevel@tonic-gate if (vaparmsp != NULL) 2120Sstevel@tonic-gate return (CL_VAPARMSIN(&sclass[parmsp->pc_cid], 2130Sstevel@tonic-gate parmsp->pc_clparms, vaparmsp)); 2140Sstevel@tonic-gate else 2150Sstevel@tonic-gate return (CL_PARMSIN(&sclass[parmsp->pc_cid], 2160Sstevel@tonic-gate parmsp->pc_clparms)); 2170Sstevel@tonic-gate } 2180Sstevel@tonic-gate 2190Sstevel@tonic-gate 2200Sstevel@tonic-gate /* 2210Sstevel@tonic-gate * Call the class specific code to do the required processing 2220Sstevel@tonic-gate * before the scheduling parameters are copied out to the user. 2230Sstevel@tonic-gate * Note that the format of the parameters may be changed by the 2240Sstevel@tonic-gate * class specific code. 2250Sstevel@tonic-gate */ 2260Sstevel@tonic-gate int 2270Sstevel@tonic-gate parmsout(pcparms_t *parmsp, pc_vaparms_t *vaparmsp) 2280Sstevel@tonic-gate { 2290Sstevel@tonic-gate return (CL_PARMSOUT(&sclass[parmsp->pc_cid], parmsp->pc_clparms, 230*6247Sraf vaparmsp)); 2310Sstevel@tonic-gate } 2320Sstevel@tonic-gate 2330Sstevel@tonic-gate 2340Sstevel@tonic-gate /* 2350Sstevel@tonic-gate * Set the scheduling parameters of the thread pointed to by 2360Sstevel@tonic-gate * targtp to those specified in the pcparms structure pointed 2370Sstevel@tonic-gate * to by parmsp. If reqtp is non-NULL it points to the thread 2380Sstevel@tonic-gate * that initiated the request for the parameter change and indicates 2390Sstevel@tonic-gate * that our caller wants us to verify that the requesting thread 2400Sstevel@tonic-gate * has the appropriate permissions. 2410Sstevel@tonic-gate */ 2420Sstevel@tonic-gate int 243*6247Sraf parmsset(pcparms_t *parmsp, kthread_t *targtp) 2440Sstevel@tonic-gate { 2450Sstevel@tonic-gate caddr_t clprocp; 2460Sstevel@tonic-gate int error; 2470Sstevel@tonic-gate cred_t *reqpcredp; 2480Sstevel@tonic-gate proc_t *reqpp = ttoproc(curthread); 2490Sstevel@tonic-gate proc_t *targpp = ttoproc(targtp); 2500Sstevel@tonic-gate id_t oldcid; 2510Sstevel@tonic-gate 2520Sstevel@tonic-gate ASSERT(MUTEX_HELD(&pidlock)); 2530Sstevel@tonic-gate ASSERT(MUTEX_HELD(&targpp->p_lock)); 2540Sstevel@tonic-gate if (reqpp != NULL) { 2550Sstevel@tonic-gate mutex_enter(&reqpp->p_crlock); 2560Sstevel@tonic-gate crhold(reqpcredp = reqpp->p_cred); 2570Sstevel@tonic-gate mutex_exit(&reqpp->p_crlock); 2580Sstevel@tonic-gate 2590Sstevel@tonic-gate /* 2600Sstevel@tonic-gate * Check basic permissions. 2610Sstevel@tonic-gate */ 2620Sstevel@tonic-gate if (!prochasprocperm(targpp, reqpp, reqpcredp)) { 2630Sstevel@tonic-gate crfree(reqpcredp); 2640Sstevel@tonic-gate return (EPERM); 2650Sstevel@tonic-gate } 2660Sstevel@tonic-gate } else { 2670Sstevel@tonic-gate reqpcredp = NULL; 2680Sstevel@tonic-gate } 2690Sstevel@tonic-gate 2700Sstevel@tonic-gate if (parmsp->pc_cid != targtp->t_cid) { 2710Sstevel@tonic-gate void *bufp = NULL; 2720Sstevel@tonic-gate /* 2730Sstevel@tonic-gate * Target thread must change to new class. 2740Sstevel@tonic-gate */ 2750Sstevel@tonic-gate clprocp = (caddr_t)targtp->t_cldata; 2760Sstevel@tonic-gate oldcid = targtp->t_cid; 2770Sstevel@tonic-gate 2780Sstevel@tonic-gate /* 2790Sstevel@tonic-gate * Purpose: allow scheduling class to veto moves 2800Sstevel@tonic-gate * to other classes. All the classes, except FSS, 2810Sstevel@tonic-gate * do nothing except returning 0. 2820Sstevel@tonic-gate */ 2830Sstevel@tonic-gate error = CL_CANEXIT(targtp, reqpcredp); 2840Sstevel@tonic-gate if (error) { 2850Sstevel@tonic-gate /* 2860Sstevel@tonic-gate * Not allowed to leave the class, so return error. 2870Sstevel@tonic-gate */ 2880Sstevel@tonic-gate crfree(reqpcredp); 2890Sstevel@tonic-gate return (error); 2900Sstevel@tonic-gate } else { 2910Sstevel@tonic-gate /* 2920Sstevel@tonic-gate * Pre-allocate scheduling class data. 2930Sstevel@tonic-gate */ 2940Sstevel@tonic-gate if (CL_ALLOC(&bufp, parmsp->pc_cid, KM_NOSLEEP) != 0) { 2950Sstevel@tonic-gate error = ENOMEM; /* no memory available */ 2960Sstevel@tonic-gate crfree(reqpcredp); 2970Sstevel@tonic-gate return (error); 2980Sstevel@tonic-gate } else { 2990Sstevel@tonic-gate error = CL_ENTERCLASS(targtp, parmsp->pc_cid, 3000Sstevel@tonic-gate parmsp->pc_clparms, reqpcredp, bufp); 3010Sstevel@tonic-gate crfree(reqpcredp); 3020Sstevel@tonic-gate if (error) { 3030Sstevel@tonic-gate CL_FREE(parmsp->pc_cid, bufp); 3040Sstevel@tonic-gate return (error); 3050Sstevel@tonic-gate } 3060Sstevel@tonic-gate } 3070Sstevel@tonic-gate } 3080Sstevel@tonic-gate CL_EXITCLASS(oldcid, clprocp); 3090Sstevel@tonic-gate } else { 3100Sstevel@tonic-gate 3110Sstevel@tonic-gate /* 3120Sstevel@tonic-gate * Not changing class 3130Sstevel@tonic-gate */ 3140Sstevel@tonic-gate error = CL_PARMSSET(targtp, parmsp->pc_clparms, 315*6247Sraf curthread->t_cid, reqpcredp); 3160Sstevel@tonic-gate crfree(reqpcredp); 3170Sstevel@tonic-gate if (error) 3180Sstevel@tonic-gate return (error); 3190Sstevel@tonic-gate } 320*6247Sraf schedctl_set_cidpri(targtp); 3210Sstevel@tonic-gate return (0); 3220Sstevel@tonic-gate } 3230Sstevel@tonic-gate 3240Sstevel@tonic-gate 3250Sstevel@tonic-gate /* 3260Sstevel@tonic-gate * Copy all selected class parameters to the user. 3270Sstevel@tonic-gate * The parameters are specified by a key. 3280Sstevel@tonic-gate */ 3290Sstevel@tonic-gate int 3302712Snn35248 vaparmsout(char *classp, pcparms_t *prmsp, pc_vaparms_t *vaparmsp, 3312712Snn35248 uio_seg_t seg) 3320Sstevel@tonic-gate { 3330Sstevel@tonic-gate char *clname; 3340Sstevel@tonic-gate 3350Sstevel@tonic-gate ASSERT(MUTEX_NOT_HELD(&curproc->p_lock)); 3360Sstevel@tonic-gate 3370Sstevel@tonic-gate if (classp != NULL) 3380Sstevel@tonic-gate return (CL_VAPARMSOUT(&sclass[prmsp->pc_cid], 3390Sstevel@tonic-gate prmsp->pc_clparms, vaparmsp)); 3400Sstevel@tonic-gate 3410Sstevel@tonic-gate switch (vaparmsp->pc_vaparmscnt) { 3420Sstevel@tonic-gate case 0: 3430Sstevel@tonic-gate return (0); 3440Sstevel@tonic-gate case 1: 3450Sstevel@tonic-gate break; 3460Sstevel@tonic-gate default: 3470Sstevel@tonic-gate return (EINVAL); 3480Sstevel@tonic-gate } 3490Sstevel@tonic-gate 3500Sstevel@tonic-gate if (vaparmsp->pc_parms[0].pc_key != PC_KY_CLNAME) 3510Sstevel@tonic-gate return (EINVAL); 3520Sstevel@tonic-gate 3530Sstevel@tonic-gate clname = sclass[prmsp->pc_cid].cl_name; 3542712Snn35248 if ((seg == UIO_USERSPACE ? copyout : kcopy)(clname, 3552712Snn35248 (void *)(uintptr_t)vaparmsp->pc_parms[0].pc_parm, 3560Sstevel@tonic-gate MIN(strlen(clname) + 1, PC_CLNMSZ))) 3570Sstevel@tonic-gate return (EFAULT); 3580Sstevel@tonic-gate 3590Sstevel@tonic-gate return (0); 3600Sstevel@tonic-gate } 361