1*0Sstevel@tonic-gate /* 2*0Sstevel@tonic-gate * CDDL HEADER START 3*0Sstevel@tonic-gate * 4*0Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*0Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 6*0Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 7*0Sstevel@tonic-gate * with the License. 8*0Sstevel@tonic-gate * 9*0Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10*0Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 11*0Sstevel@tonic-gate * See the License for the specific language governing permissions 12*0Sstevel@tonic-gate * and limitations under the License. 13*0Sstevel@tonic-gate * 14*0Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 15*0Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16*0Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 17*0Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 18*0Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 19*0Sstevel@tonic-gate * 20*0Sstevel@tonic-gate * CDDL HEADER END 21*0Sstevel@tonic-gate */ 22*0Sstevel@tonic-gate /* 23*0Sstevel@tonic-gate * Copyright 2004 Sun Microsystems, Inc. All rights reserved. 24*0Sstevel@tonic-gate * Use is subject to license terms. 25*0Sstevel@tonic-gate */ 26*0Sstevel@tonic-gate 27*0Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 28*0Sstevel@tonic-gate 29*0Sstevel@tonic-gate /* 30*0Sstevel@tonic-gate * System calls for creating and inquiring about tasks and projects 31*0Sstevel@tonic-gate */ 32*0Sstevel@tonic-gate 33*0Sstevel@tonic-gate #include <sys/param.h> 34*0Sstevel@tonic-gate #include <sys/types.h> 35*0Sstevel@tonic-gate #include <sys/errno.h> 36*0Sstevel@tonic-gate #include <sys/thread.h> 37*0Sstevel@tonic-gate #include <sys/proc.h> 38*0Sstevel@tonic-gate #include <sys/task.h> 39*0Sstevel@tonic-gate #include <sys/systm.h> 40*0Sstevel@tonic-gate #include <sys/project.h> 41*0Sstevel@tonic-gate #include <sys/cpuvar.h> 42*0Sstevel@tonic-gate #include <sys/policy.h> 43*0Sstevel@tonic-gate #include <sys/zone.h> 44*0Sstevel@tonic-gate 45*0Sstevel@tonic-gate /* 46*0Sstevel@tonic-gate * Limit projlist to 256k projects. 47*0Sstevel@tonic-gate */ 48*0Sstevel@tonic-gate #define MAX_PROJLIST_BUFSIZE 1048576 49*0Sstevel@tonic-gate 50*0Sstevel@tonic-gate typedef struct projlist_walk { 51*0Sstevel@tonic-gate projid_t *pw_buf; 52*0Sstevel@tonic-gate size_t pw_bufsz; 53*0Sstevel@tonic-gate } projlist_walk_t; 54*0Sstevel@tonic-gate 55*0Sstevel@tonic-gate 56*0Sstevel@tonic-gate /* 57*0Sstevel@tonic-gate * taskid_t tasksys_settaskid(projid_t projid, uint_t flags); 58*0Sstevel@tonic-gate * 59*0Sstevel@tonic-gate * Overview 60*0Sstevel@tonic-gate * Place the calling process in a new task if sufficiently privileged. If the 61*0Sstevel@tonic-gate * present task is finalized, the process may not create a new task. 62*0Sstevel@tonic-gate * 63*0Sstevel@tonic-gate * Return values 64*0Sstevel@tonic-gate * 0 on success, errno on failure. 65*0Sstevel@tonic-gate */ 66*0Sstevel@tonic-gate static long 67*0Sstevel@tonic-gate tasksys_settaskid(projid_t projid, uint_t flags) 68*0Sstevel@tonic-gate { 69*0Sstevel@tonic-gate proc_t *p = ttoproc(curthread); 70*0Sstevel@tonic-gate kproject_t *oldpj; 71*0Sstevel@tonic-gate kproject_t *kpj; 72*0Sstevel@tonic-gate task_t *tk, *oldtk; 73*0Sstevel@tonic-gate rctl_entity_p_t e; 74*0Sstevel@tonic-gate zone_t *zone; 75*0Sstevel@tonic-gate int rctlfail = 0; 76*0Sstevel@tonic-gate 77*0Sstevel@tonic-gate if (secpolicy_tasksys(CRED()) != 0) 78*0Sstevel@tonic-gate return (set_errno(EPERM)); 79*0Sstevel@tonic-gate 80*0Sstevel@tonic-gate if (projid < 0 || projid > MAXPROJID) 81*0Sstevel@tonic-gate return (set_errno(EINVAL)); 82*0Sstevel@tonic-gate 83*0Sstevel@tonic-gate if (flags & ~TASK_FINAL) 84*0Sstevel@tonic-gate return (set_errno(EINVAL)); 85*0Sstevel@tonic-gate 86*0Sstevel@tonic-gate mutex_enter(&pidlock); 87*0Sstevel@tonic-gate if (p->p_task->tk_flags & TASK_FINAL) { 88*0Sstevel@tonic-gate mutex_exit(&pidlock); 89*0Sstevel@tonic-gate return (set_errno(EACCES)); 90*0Sstevel@tonic-gate } 91*0Sstevel@tonic-gate mutex_exit(&pidlock); 92*0Sstevel@tonic-gate 93*0Sstevel@tonic-gate /* 94*0Sstevel@tonic-gate * Try to stop all other lwps in the process while we're changing 95*0Sstevel@tonic-gate * our project. This way, curthread doesn't need to grab its own 96*0Sstevel@tonic-gate * thread_lock to find its project ID (see curprojid()). If this 97*0Sstevel@tonic-gate * is the /proc agent lwp, we know that the other lwps are already 98*0Sstevel@tonic-gate * held. If we failed to hold all lwps, bail out and return EINTR. 99*0Sstevel@tonic-gate */ 100*0Sstevel@tonic-gate if (curthread != p->p_agenttp && !holdlwps(SHOLDFORK1)) 101*0Sstevel@tonic-gate return (set_errno(EINTR)); 102*0Sstevel@tonic-gate /* 103*0Sstevel@tonic-gate * Put a hold on our new project and make sure that nobody is 104*0Sstevel@tonic-gate * trying to bind it to a pool while we're joining. 105*0Sstevel@tonic-gate */ 106*0Sstevel@tonic-gate kpj = project_hold_by_id(projid, getzoneid(), PROJECT_HOLD_INSERT); 107*0Sstevel@tonic-gate e.rcep_p.proj = kpj; 108*0Sstevel@tonic-gate e.rcep_t = RCENTITY_PROJECT; 109*0Sstevel@tonic-gate 110*0Sstevel@tonic-gate mutex_enter(&p->p_lock); 111*0Sstevel@tonic-gate oldpj = p->p_task->tk_proj; 112*0Sstevel@tonic-gate zone = p->p_zone; 113*0Sstevel@tonic-gate 114*0Sstevel@tonic-gate mutex_enter(&zone->zone_nlwps_lock); 115*0Sstevel@tonic-gate 116*0Sstevel@tonic-gate if (kpj->kpj_nlwps + p->p_lwpcnt > kpj->kpj_nlwps_ctl) 117*0Sstevel@tonic-gate if (rctl_test_entity(rc_project_nlwps, kpj->kpj_rctls, p, &e, 118*0Sstevel@tonic-gate p->p_lwpcnt, 0) & RCT_DENY) 119*0Sstevel@tonic-gate rctlfail = 1; 120*0Sstevel@tonic-gate 121*0Sstevel@tonic-gate if (kpj->kpj_ntasks + 1 > kpj->kpj_ntasks_ctl) 122*0Sstevel@tonic-gate if (rctl_test_entity(rc_project_ntasks, kpj->kpj_rctls, p, &e, 123*0Sstevel@tonic-gate 1, 0) & RCT_DENY) 124*0Sstevel@tonic-gate rctlfail = 1; 125*0Sstevel@tonic-gate 126*0Sstevel@tonic-gate if (rctlfail) { 127*0Sstevel@tonic-gate mutex_exit(&zone->zone_nlwps_lock); 128*0Sstevel@tonic-gate if (curthread != p->p_agenttp) 129*0Sstevel@tonic-gate continuelwps(p); 130*0Sstevel@tonic-gate mutex_exit(&p->p_lock); 131*0Sstevel@tonic-gate return (set_errno(EAGAIN)); 132*0Sstevel@tonic-gate } 133*0Sstevel@tonic-gate kpj->kpj_nlwps += p->p_lwpcnt; 134*0Sstevel@tonic-gate kpj->kpj_ntasks++; 135*0Sstevel@tonic-gate 136*0Sstevel@tonic-gate oldpj->kpj_nlwps -= p->p_lwpcnt; 137*0Sstevel@tonic-gate 138*0Sstevel@tonic-gate mutex_exit(&zone->zone_nlwps_lock); 139*0Sstevel@tonic-gate mutex_exit(&p->p_lock); 140*0Sstevel@tonic-gate 141*0Sstevel@tonic-gate mutex_enter(&kpj->kpj_poolbind); 142*0Sstevel@tonic-gate tk = task_create(projid, curproc->p_zone); 143*0Sstevel@tonic-gate mutex_enter(&cpu_lock); 144*0Sstevel@tonic-gate /* 145*0Sstevel@tonic-gate * Returns with p_lock held. 146*0Sstevel@tonic-gate */ 147*0Sstevel@tonic-gate oldtk = task_join(tk, flags); 148*0Sstevel@tonic-gate if (curthread != p->p_agenttp) 149*0Sstevel@tonic-gate continuelwps(p); 150*0Sstevel@tonic-gate mutex_exit(&p->p_lock); 151*0Sstevel@tonic-gate mutex_exit(&cpu_lock); 152*0Sstevel@tonic-gate mutex_exit(&kpj->kpj_poolbind); 153*0Sstevel@tonic-gate task_rele(oldtk); 154*0Sstevel@tonic-gate project_rele(kpj); 155*0Sstevel@tonic-gate return (tk->tk_tkid); 156*0Sstevel@tonic-gate } 157*0Sstevel@tonic-gate 158*0Sstevel@tonic-gate /* 159*0Sstevel@tonic-gate * taskid_t tasksys_gettaskid(void); 160*0Sstevel@tonic-gate * 161*0Sstevel@tonic-gate * Overview 162*0Sstevel@tonic-gate * Return the current task ID for this process. 163*0Sstevel@tonic-gate * 164*0Sstevel@tonic-gate * Return value 165*0Sstevel@tonic-gate * The ID for the task to which the current process belongs. 166*0Sstevel@tonic-gate */ 167*0Sstevel@tonic-gate static long 168*0Sstevel@tonic-gate tasksys_gettaskid() 169*0Sstevel@tonic-gate { 170*0Sstevel@tonic-gate long ret; 171*0Sstevel@tonic-gate proc_t *p = ttoproc(curthread); 172*0Sstevel@tonic-gate 173*0Sstevel@tonic-gate mutex_enter(&pidlock); 174*0Sstevel@tonic-gate ret = p->p_task->tk_tkid; 175*0Sstevel@tonic-gate mutex_exit(&pidlock); 176*0Sstevel@tonic-gate return (ret); 177*0Sstevel@tonic-gate } 178*0Sstevel@tonic-gate 179*0Sstevel@tonic-gate /* 180*0Sstevel@tonic-gate * projid_t tasksys_getprojid(void); 181*0Sstevel@tonic-gate * 182*0Sstevel@tonic-gate * Overview 183*0Sstevel@tonic-gate * Return the current project ID for this process. 184*0Sstevel@tonic-gate * 185*0Sstevel@tonic-gate * Return value 186*0Sstevel@tonic-gate * The ID for the project to which the current process belongs. 187*0Sstevel@tonic-gate */ 188*0Sstevel@tonic-gate static long 189*0Sstevel@tonic-gate tasksys_getprojid() 190*0Sstevel@tonic-gate { 191*0Sstevel@tonic-gate long ret; 192*0Sstevel@tonic-gate proc_t *p = ttoproc(curthread); 193*0Sstevel@tonic-gate 194*0Sstevel@tonic-gate mutex_enter(&pidlock); 195*0Sstevel@tonic-gate ret = p->p_task->tk_proj->kpj_id; 196*0Sstevel@tonic-gate mutex_exit(&pidlock); 197*0Sstevel@tonic-gate return (ret); 198*0Sstevel@tonic-gate } 199*0Sstevel@tonic-gate 200*0Sstevel@tonic-gate static int 201*0Sstevel@tonic-gate tasksys_projlist_cb(kproject_t *kp, void *buf) 202*0Sstevel@tonic-gate { 203*0Sstevel@tonic-gate projlist_walk_t *pw = (projlist_walk_t *)buf; 204*0Sstevel@tonic-gate 205*0Sstevel@tonic-gate if (pw && pw->pw_bufsz >= sizeof (projid_t)) { 206*0Sstevel@tonic-gate *pw->pw_buf = kp->kpj_id; 207*0Sstevel@tonic-gate pw->pw_buf++; 208*0Sstevel@tonic-gate pw->pw_bufsz -= sizeof (projid_t); 209*0Sstevel@tonic-gate } 210*0Sstevel@tonic-gate 211*0Sstevel@tonic-gate return (0); 212*0Sstevel@tonic-gate } 213*0Sstevel@tonic-gate 214*0Sstevel@tonic-gate /* 215*0Sstevel@tonic-gate * long tasksys_projlist(void *buf, size_t bufsz) 216*0Sstevel@tonic-gate * 217*0Sstevel@tonic-gate * Overview 218*0Sstevel@tonic-gate * Return a buffer containing the project IDs of all currently active projects 219*0Sstevel@tonic-gate * in the current zone. 220*0Sstevel@tonic-gate * 221*0Sstevel@tonic-gate * Return values 222*0Sstevel@tonic-gate * The minimum size of a buffer sufficiently large to contain all of the 223*0Sstevel@tonic-gate * active project IDs, or -1 if an error occurs during copyout. 224*0Sstevel@tonic-gate */ 225*0Sstevel@tonic-gate static long 226*0Sstevel@tonic-gate tasksys_projlist(void *buf, size_t bufsz) 227*0Sstevel@tonic-gate { 228*0Sstevel@tonic-gate long ret = 0; 229*0Sstevel@tonic-gate projlist_walk_t pw; 230*0Sstevel@tonic-gate void *kbuf; 231*0Sstevel@tonic-gate 232*0Sstevel@tonic-gate if (buf == NULL || bufsz == 0) 233*0Sstevel@tonic-gate return (project_walk_all(getzoneid(), tasksys_projlist_cb, 234*0Sstevel@tonic-gate NULL)); 235*0Sstevel@tonic-gate 236*0Sstevel@tonic-gate if (bufsz > MAX_PROJLIST_BUFSIZE) 237*0Sstevel@tonic-gate return (set_errno(ENOMEM)); 238*0Sstevel@tonic-gate 239*0Sstevel@tonic-gate kbuf = pw.pw_buf = kmem_zalloc(bufsz, KM_SLEEP); 240*0Sstevel@tonic-gate pw.pw_bufsz = bufsz; 241*0Sstevel@tonic-gate 242*0Sstevel@tonic-gate ret = project_walk_all(getzoneid(), tasksys_projlist_cb, &pw); 243*0Sstevel@tonic-gate 244*0Sstevel@tonic-gate if (copyout(kbuf, buf, bufsz) == -1) 245*0Sstevel@tonic-gate ret = set_errno(EFAULT); 246*0Sstevel@tonic-gate 247*0Sstevel@tonic-gate kmem_free(kbuf, bufsz); 248*0Sstevel@tonic-gate return (ret); 249*0Sstevel@tonic-gate } 250*0Sstevel@tonic-gate 251*0Sstevel@tonic-gate long 252*0Sstevel@tonic-gate tasksys(int code, projid_t projid, uint_t flags, void *projidbuf, size_t pbufsz) 253*0Sstevel@tonic-gate { 254*0Sstevel@tonic-gate switch (code) { 255*0Sstevel@tonic-gate case 0: 256*0Sstevel@tonic-gate return (tasksys_settaskid(projid, flags)); 257*0Sstevel@tonic-gate case 1: 258*0Sstevel@tonic-gate return (tasksys_gettaskid()); 259*0Sstevel@tonic-gate case 2: 260*0Sstevel@tonic-gate return (tasksys_getprojid()); 261*0Sstevel@tonic-gate case 3: 262*0Sstevel@tonic-gate return (tasksys_projlist(projidbuf, pbufsz)); 263*0Sstevel@tonic-gate default: 264*0Sstevel@tonic-gate return (set_errno(EINVAL)); 265*0Sstevel@tonic-gate } 266*0Sstevel@tonic-gate } 267