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