1 0 stevel /* 2 0 stevel * CDDL HEADER START 3 0 stevel * 4 0 stevel * The contents of this file are subject to the terms of the 5 2712 nn35248 * Common Development and Distribution License (the "License"). 6 2712 nn35248 * You may not use this file except in compliance with the License. 7 0 stevel * 8 0 stevel * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 0 stevel * or http://www.opensolaris.org/os/licensing. 10 0 stevel * See the License for the specific language governing permissions 11 0 stevel * and limitations under the License. 12 0 stevel * 13 0 stevel * When distributing Covered Code, include this CDDL HEADER in each 14 0 stevel * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 0 stevel * If applicable, add the following below this CDDL HEADER, with the 16 0 stevel * fields enclosed by brackets "[]" replaced with your own identifying 17 0 stevel * information: Portions Copyright [yyyy] [name of copyright owner] 18 0 stevel * 19 0 stevel * CDDL HEADER END 20 0 stevel */ 21 390 raf 22 0 stevel /* 23 9393 Roger * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 24 0 stevel * Use is subject to license terms. 25 0 stevel */ 26 0 stevel 27 0 stevel #include <sys/param.h> 28 0 stevel #include <sys/types.h> 29 0 stevel #include <sys/sysmacros.h> 30 0 stevel #include <sys/systm.h> 31 0 stevel #include <sys/thread.h> 32 0 stevel #include <sys/proc.h> 33 0 stevel #include <sys/task.h> 34 0 stevel #include <sys/project.h> 35 0 stevel #include <sys/signal.h> 36 0 stevel #include <sys/errno.h> 37 0 stevel #include <sys/vmparam.h> 38 0 stevel #include <sys/stack.h> 39 0 stevel #include <sys/procfs.h> 40 0 stevel #include <sys/prsystm.h> 41 0 stevel #include <sys/cpuvar.h> 42 0 stevel #include <sys/kmem.h> 43 0 stevel #include <sys/vtrace.h> 44 0 stevel #include <sys/door.h> 45 0 stevel #include <vm/seg_kp.h> 46 0 stevel #include <sys/debug.h> 47 0 stevel #include <sys/tnf.h> 48 0 stevel #include <sys/schedctl.h> 49 0 stevel #include <sys/poll.h> 50 0 stevel #include <sys/copyops.h> 51 0 stevel #include <sys/lwp_upimutex_impl.h> 52 0 stevel #include <sys/cpupart.h> 53 0 stevel #include <sys/lgrp.h> 54 0 stevel #include <sys/rctl.h> 55 0 stevel #include <sys/contract_impl.h> 56 0 stevel #include <sys/cpc_impl.h> 57 0 stevel #include <sys/sdt.h> 58 0 stevel #include <sys/cmn_err.h> 59 2712 nn35248 #include <sys/brand.h> 60 9870 Roger #include <sys/cyclic.h> 61 10541 Gangadhar #include <sys/pool.h> 62 0 stevel 63 9393 Roger /* hash function for the lwpid hash table, p->p_tidhash[] */ 64 9393 Roger #define TIDHASH(tid, hash_sz) ((tid) & ((hash_sz) - 1)) 65 9393 Roger 66 0 stevel void *segkp_lwp; /* cookie for pool of segkp resources */ 67 5834 pt157919 extern void reapq_move_lq_to_tq(kthread_t *); 68 5834 pt157919 extern void freectx_ctx(struct ctxop *); 69 0 stevel 70 0 stevel /* 71 11173 Jonathan * Create a kernel thread associated with a particular system process. Give 72 11173 Jonathan * it an LWP so that microstate accounting will be available for it. 73 11173 Jonathan */ 74 11173 Jonathan kthread_t * 75 11173 Jonathan lwp_kernel_create(proc_t *p, void (*proc)(), void *arg, int state, pri_t pri) 76 11173 Jonathan { 77 11173 Jonathan klwp_t *lwp; 78 11173 Jonathan 79 11173 Jonathan VERIFY((p->p_flag & SSYS) != 0); 80 11173 Jonathan 81 11173 Jonathan lwp = lwp_create(proc, arg, 0, p, state, pri, &t0.t_hold, syscid, 0); 82 11173 Jonathan 83 11173 Jonathan VERIFY(lwp != NULL); 84 11173 Jonathan 85 11173 Jonathan return (lwptot(lwp)); 86 11173 Jonathan } 87 11173 Jonathan 88 11173 Jonathan /* 89 0 stevel * Create a thread that appears to be stopped at sys_rtt. 90 0 stevel */ 91 0 stevel klwp_t * 92 0 stevel lwp_create(void (*proc)(), caddr_t arg, size_t len, proc_t *p, 93 0 stevel int state, int pri, const k_sigset_t *smask, int cid, id_t lwpid) 94 0 stevel { 95 0 stevel klwp_t *lwp = NULL; 96 0 stevel kthread_t *t; 97 0 stevel kthread_t *tx; 98 0 stevel cpupart_t *oldpart = NULL; 99 0 stevel size_t stksize; 100 0 stevel caddr_t lwpdata = NULL; 101 0 stevel processorid_t binding; 102 0 stevel int err = 0; 103 0 stevel kproject_t *oldkpj, *newkpj; 104 0 stevel void *bufp = NULL; 105 11173 Jonathan klwp_t *curlwp; 106 0 stevel lwpent_t *lep; 107 0 stevel lwpdir_t *old_dir = NULL; 108 0 stevel uint_t old_dirsz = 0; 109 9393 Roger tidhash_t *old_hash = NULL; 110 0 stevel uint_t old_hashsz = 0; 111 9393 Roger ret_tidhash_t *ret_tidhash = NULL; 112 0 stevel int i; 113 0 stevel int rctlfail = 0; 114 2712 nn35248 boolean_t branded = 0; 115 5834 pt157919 struct ctxop *ctx = NULL; 116 0 stevel 117 11173 Jonathan ASSERT(cid != sysdccid); /* system threads must start in SYS */ 118 11173 Jonathan 119 11173 Jonathan ASSERT(p != &p0); /* No new LWPs in p0. */ 120 11173 Jonathan 121 0 stevel mutex_enter(&p->p_lock); 122 0 stevel mutex_enter(&p->p_zone->zone_nlwps_lock); 123 0 stevel /* 124 0 stevel * don't enforce rctl limits on system processes 125 0 stevel */ 126 11173 Jonathan if (!CLASS_KERNEL(cid)) { 127 0 stevel if (p->p_task->tk_nlwps >= p->p_task->tk_nlwps_ctl) 128 0 stevel if (rctl_test(rc_task_lwps, p->p_task->tk_rctls, p, 129 0 stevel 1, 0) & RCT_DENY) 130 0 stevel rctlfail = 1; 131 0 stevel if (p->p_task->tk_proj->kpj_nlwps >= 132 0 stevel p->p_task->tk_proj->kpj_nlwps_ctl) 133 0 stevel if (rctl_test(rc_project_nlwps, 134 0 stevel p->p_task->tk_proj->kpj_rctls, p, 1, 0) 135 0 stevel & RCT_DENY) 136 0 stevel rctlfail = 1; 137 0 stevel if (p->p_zone->zone_nlwps >= p->p_zone->zone_nlwps_ctl) 138 0 stevel if (rctl_test(rc_zone_nlwps, p->p_zone->zone_rctls, p, 139 0 stevel 1, 0) & RCT_DENY) 140 0 stevel rctlfail = 1; 141 0 stevel } 142 0 stevel if (rctlfail) { 143 0 stevel mutex_exit(&p->p_zone->zone_nlwps_lock); 144 0 stevel mutex_exit(&p->p_lock); 145 0 stevel return (NULL); 146 0 stevel } 147 0 stevel p->p_task->tk_nlwps++; 148 0 stevel p->p_task->tk_proj->kpj_nlwps++; 149 0 stevel p->p_zone->zone_nlwps++; 150 0 stevel mutex_exit(&p->p_zone->zone_nlwps_lock); 151 0 stevel mutex_exit(&p->p_lock); 152 0 stevel 153 11173 Jonathan if (CLASS_KERNEL(cid)) { 154 11173 Jonathan curlwp = NULL; /* don't inherit from curlwp */ 155 0 stevel stksize = lwp_default_stksize; 156 11173 Jonathan } else { 157 11173 Jonathan curlwp = ttolwp(curthread); 158 11173 Jonathan if (curlwp == NULL || (stksize = curlwp->lwp_childstksz) == 0) 159 11173 Jonathan stksize = lwp_default_stksize; 160 11173 Jonathan } 161 0 stevel 162 0 stevel /* 163 11173 Jonathan * For system threads, we sleep for our swap reservation, and the 164 11173 Jonathan * thread stack can't be swapped. 165 11173 Jonathan * 166 11173 Jonathan * Otherwise, try to reclaim a <lwp,stack> from 'deathrow' 167 0 stevel */ 168 11173 Jonathan if (CLASS_KERNEL(cid)) { 169 11173 Jonathan lwpdata = (caddr_t)segkp_get(segkp, stksize, 170 11173 Jonathan (KPD_NO_ANON | KPD_HASREDZONE | KPD_LOCKED)); 171 11173 Jonathan 172 11173 Jonathan } else if (stksize == lwp_default_stksize) { 173 0 stevel if (lwp_reapcnt > 0) { 174 0 stevel mutex_enter(&reaplock); 175 0 stevel if ((t = lwp_deathrow) != NULL) { 176 0 stevel ASSERT(t->t_swap); 177 0 stevel lwp_deathrow = t->t_forw; 178 0 stevel lwp_reapcnt--; 179 0 stevel lwpdata = t->t_swap; 180 0 stevel lwp = t->t_lwp; 181 5834 pt157919 ctx = t->t_ctx; 182 5834 pt157919 t->t_swap = NULL; 183 5834 pt157919 t->t_lwp = NULL; 184 5834 pt157919 t->t_ctx = NULL; 185 5834 pt157919 reapq_move_lq_to_tq(t); 186 0 stevel } 187 0 stevel mutex_exit(&reaplock); 188 5834 pt157919 if (lwp != NULL) { 189 5834 pt157919 lwp_stk_fini(lwp); 190 5834 pt157919 } 191 5834 pt157919 if (ctx != NULL) { 192 5834 pt157919 freectx_ctx(ctx); 193 0 stevel } 194 0 stevel } 195 0 stevel if (lwpdata == NULL && 196 0 stevel (lwpdata = (caddr_t)segkp_cache_get(segkp_lwp)) == NULL) { 197 0 stevel mutex_enter(&p->p_lock); 198 0 stevel mutex_enter(&p->p_zone->zone_nlwps_lock); 199 0 stevel p->p_task->tk_nlwps--; 200 0 stevel p->p_task->tk_proj->kpj_nlwps--; 201 0 stevel p->p_zone->zone_nlwps--; 202 0 stevel mutex_exit(&p->p_zone->zone_nlwps_lock); 203 0 stevel mutex_exit(&p->p_lock); 204 0 stevel return (NULL); 205 0 stevel } 206 0 stevel } else { 207 0 stevel stksize = roundup(stksize, PAGESIZE); 208 0 stevel if ((lwpdata = (caddr_t)segkp_get(segkp, stksize, 209 0 stevel (KPD_NOWAIT | KPD_HASREDZONE | KPD_LOCKED))) == NULL) { 210 0 stevel mutex_enter(&p->p_lock); 211 0 stevel mutex_enter(&p->p_zone->zone_nlwps_lock); 212 0 stevel p->p_task->tk_nlwps--; 213 0 stevel p->p_task->tk_proj->kpj_nlwps--; 214 0 stevel p->p_zone->zone_nlwps--; 215 0 stevel mutex_exit(&p->p_zone->zone_nlwps_lock); 216 0 stevel mutex_exit(&p->p_lock); 217 0 stevel return (NULL); 218 0 stevel } 219 0 stevel } 220 0 stevel 221 0 stevel /* 222 0 stevel * Create a thread, initializing the stack pointer 223 0 stevel */ 224 0 stevel t = thread_create(lwpdata, stksize, NULL, NULL, 0, p, TS_STOPPED, pri); 225 0 stevel 226 0 stevel t->t_swap = lwpdata; /* Start of page-able data */ 227 0 stevel if (lwp == NULL) 228 0 stevel lwp = kmem_cache_alloc(lwp_cache, KM_SLEEP); 229 0 stevel bzero(lwp, sizeof (*lwp)); 230 0 stevel t->t_lwp = lwp; 231 0 stevel 232 0 stevel t->t_hold = *smask; 233 0 stevel lwp->lwp_thread = t; 234 0 stevel lwp->lwp_procp = p; 235 0 stevel lwp->lwp_sigaltstack.ss_flags = SS_DISABLE; 236 0 stevel if (curlwp != NULL && curlwp->lwp_childstksz != 0) 237 0 stevel lwp->lwp_childstksz = curlwp->lwp_childstksz; 238 0 stevel 239 0 stevel t->t_stk = lwp_stk_init(lwp, t->t_stk); 240 0 stevel thread_load(t, proc, arg, len); 241 0 stevel 242 0 stevel /* 243 0 stevel * Allocate the SIGPROF buffer if ITIMER_REALPROF is in effect. 244 0 stevel */ 245 9870 Roger if (p->p_rprof_cyclic != CYCLIC_NONE) 246 0 stevel t->t_rprof = kmem_zalloc(sizeof (struct rprof), KM_SLEEP); 247 0 stevel 248 0 stevel if (cid != NOCLASS) 249 0 stevel (void) CL_ALLOC(&bufp, cid, KM_SLEEP); 250 0 stevel 251 0 stevel /* 252 0 stevel * Allocate an lwp directory entry for the new lwp. 253 0 stevel */ 254 0 stevel lep = kmem_zalloc(sizeof (*lep), KM_SLEEP); 255 0 stevel 256 0 stevel mutex_enter(&p->p_lock); 257 0 stevel grow: 258 0 stevel /* 259 0 stevel * Grow the lwp (thread) directory and lwpid hash table if necessary. 260 0 stevel * A note on the growth algorithm: 261 0 stevel * The new lwp directory size is computed as: 262 0 stevel * new = 2 * old + 2 263 0 stevel * Starting with an initial size of 2 (see exec_common()), 264 0 stevel * this yields numbers that are a power of two minus 2: 265 0 stevel * 2, 6, 14, 30, 62, 126, 254, 510, 1022, ... 266 0 stevel * The size of the lwpid hash table must be a power of two 267 0 stevel * and must be commensurate in size with the lwp directory 268 0 stevel * so that hash bucket chains remain short. Therefore, 269 0 stevel * the lwpid hash table size is computed as: 270 0 stevel * hashsz = (dirsz + 2) / 2 271 0 stevel * which leads to these hash table sizes corresponding to 272 0 stevel * the above directory sizes: 273 0 stevel * 2, 4, 8, 16, 32, 64, 128, 256, 512, ... 274 9393 Roger * A note on growing the hash table: 275 9393 Roger * For performance reasons, code in lwp_unpark() does not 276 9393 Roger * acquire curproc->p_lock when searching the hash table. 277 9393 Roger * Rather, it calls lwp_hash_lookup_and_lock() which 278 9393 Roger * acquires only the individual hash bucket lock, taking 279 9393 Roger * care to deal with reallocation of the hash table 280 9393 Roger * during the time it takes to acquire the lock. 281 9393 Roger * 282 9393 Roger * This is sufficient to protect the integrity of the 283 9393 Roger * hash table, but it requires us to acquire all of the 284 9393 Roger * old hash bucket locks before growing the hash table 285 9393 Roger * and to release them afterwards. It also requires us 286 9393 Roger * not to free the old hash table because some thread 287 9393 Roger * in lwp_hash_lookup_and_lock() might still be trying 288 9393 Roger * to acquire the old bucket lock. 289 9393 Roger * 290 9393 Roger * So we adopt the tactic of keeping all of the retired 291 9393 Roger * hash tables on a linked list, so they can be safely 292 9393 Roger * freed when the process exits or execs. 293 9393 Roger * 294 9393 Roger * Because the hash table grows in powers of two, the 295 9393 Roger * total size of all of the hash tables will be slightly 296 9393 Roger * less than twice the size of the largest hash table. 297 0 stevel */ 298 0 stevel while (p->p_lwpfree == NULL) { 299 0 stevel uint_t dirsz = p->p_lwpdir_sz; 300 9393 Roger lwpdir_t *new_dir; 301 0 stevel uint_t new_dirsz; 302 9393 Roger lwpdir_t *ldp; 303 9393 Roger tidhash_t *new_hash; 304 0 stevel uint_t new_hashsz; 305 0 stevel 306 0 stevel mutex_exit(&p->p_lock); 307 0 stevel 308 9393 Roger /* 309 9393 Roger * Prepare to remember the old p_tidhash for later 310 9393 Roger * kmem_free()ing when the process exits or execs. 311 9393 Roger */ 312 9393 Roger if (ret_tidhash == NULL) 313 9393 Roger ret_tidhash = kmem_zalloc(sizeof (ret_tidhash_t), 314 9393 Roger KM_SLEEP); 315 9393 Roger if (old_dir != NULL) 316 0 stevel kmem_free(old_dir, old_dirsz * sizeof (*old_dir)); 317 9393 Roger if (old_hash != NULL) 318 0 stevel kmem_free(old_hash, old_hashsz * sizeof (*old_hash)); 319 9393 Roger 320 0 stevel new_dirsz = 2 * dirsz + 2; 321 0 stevel new_dir = kmem_zalloc(new_dirsz * sizeof (lwpdir_t), KM_SLEEP); 322 0 stevel for (ldp = new_dir, i = 1; i < new_dirsz; i++, ldp++) 323 0 stevel ldp->ld_next = ldp + 1; 324 0 stevel new_hashsz = (new_dirsz + 2) / 2; 325 9393 Roger new_hash = kmem_zalloc(new_hashsz * sizeof (tidhash_t), 326 5834 pt157919 KM_SLEEP); 327 0 stevel 328 0 stevel mutex_enter(&p->p_lock); 329 0 stevel if (p == curproc) 330 0 stevel prbarrier(p); 331 0 stevel 332 0 stevel if (dirsz != p->p_lwpdir_sz || p->p_lwpfree != NULL) { 333 0 stevel /* 334 0 stevel * Someone else beat us to it or some lwp exited. 335 0 stevel * Set up to free our memory and take a lap. 336 0 stevel */ 337 0 stevel old_dir = new_dir; 338 0 stevel old_dirsz = new_dirsz; 339 0 stevel old_hash = new_hash; 340 0 stevel old_hashsz = new_hashsz; 341 0 stevel } else { 342 9393 Roger /* 343 9393 Roger * For the benefit of lwp_hash_lookup_and_lock(), 344 9393 Roger * called from lwp_unpark(), which searches the 345 9393 Roger * tid hash table without acquiring p->p_lock, 346 9393 Roger * we must acquire all of the tid hash table 347 9393 Roger * locks before replacing p->p_tidhash. 348 9393 Roger */ 349 0 stevel old_hash = p->p_tidhash; 350 0 stevel old_hashsz = p->p_tidhash_sz; 351 9393 Roger for (i = 0; i < old_hashsz; i++) { 352 9393 Roger mutex_enter(&old_hash[i].th_lock); 353 9393 Roger mutex_enter(&new_hash[i].th_lock); 354 9393 Roger } 355 9393 Roger 356 0 stevel /* 357 0 stevel * We simply hash in all of the old directory entries. 358 0 stevel * This works because the old directory has no empty 359 0 stevel * slots and the new hash table starts out empty. 360 0 stevel * This reproduces the original directory ordering 361 0 stevel * (required for /proc directory semantics). 362 0 stevel */ 363 9393 Roger old_dir = p->p_lwpdir; 364 9393 Roger old_dirsz = p->p_lwpdir_sz; 365 9393 Roger p->p_lwpdir = new_dir; 366 9393 Roger p->p_lwpfree = new_dir; 367 9393 Roger p->p_lwpdir_sz = new_dirsz; 368 9393 Roger for (ldp = old_dir, i = 0; i < old_dirsz; i++, ldp++) 369 9393 Roger lwp_hash_in(p, ldp->ld_entry, 370 9393 Roger new_hash, new_hashsz, 0); 371 9393 Roger 372 0 stevel /* 373 9393 Roger * Remember the old hash table along with all 374 9393 Roger * of the previously-remembered hash tables. 375 9393 Roger * We will free them at process exit or exec. 376 0 stevel */ 377 9393 Roger ret_tidhash->rth_tidhash = old_hash; 378 9393 Roger ret_tidhash->rth_tidhash_sz = old_hashsz; 379 9393 Roger ret_tidhash->rth_next = p->p_ret_tidhash; 380 9393 Roger p->p_ret_tidhash = ret_tidhash; 381 9393 Roger 382 9393 Roger /* 383 9393 Roger * Now establish the new tid hash table. 384 9393 Roger * As soon as we assign p->p_tidhash, 385 9393 Roger * code in lwp_unpark() can start using it. 386 9393 Roger */ 387 9393 Roger membar_producer(); 388 9393 Roger p->p_tidhash = new_hash; 389 9393 Roger 390 9393 Roger /* 391 9393 Roger * It is necessary that p_tidhash reach global 392 9393 Roger * visibility before p_tidhash_sz. Otherwise, 393 9393 Roger * code in lwp_hash_lookup_and_lock() could 394 9393 Roger * index into the old p_tidhash using the new 395 9393 Roger * p_tidhash_sz and thereby access invalid data. 396 9393 Roger */ 397 9393 Roger membar_producer(); 398 9393 Roger p->p_tidhash_sz = new_hashsz; 399 9393 Roger 400 9393 Roger /* 401 9393 Roger * Release the locks; allow lwp_unpark() to carry on. 402 9393 Roger */ 403 9393 Roger for (i = 0; i < old_hashsz; i++) { 404 9393 Roger mutex_exit(&old_hash[i].th_lock); 405 9393 Roger mutex_exit(&new_hash[i].th_lock); 406 9393 Roger } 407 9393 Roger 408 9393 Roger /* 409 9393 Roger * Avoid freeing these objects below. 410 9393 Roger */ 411 9393 Roger ret_tidhash = NULL; 412 9393 Roger old_hash = NULL; 413 9393 Roger old_hashsz = 0; 414 0 stevel } 415 0 stevel } 416 0 stevel 417 0 stevel /* 418 0 stevel * Block the process against /proc while we manipulate p->p_tlist, 419 0 stevel * unless lwp_create() was called by /proc for the PCAGENT operation. 420 0 stevel * We want to do this early enough so that we don't drop p->p_lock 421 0 stevel * until the thread is put on the p->p_tlist. 422 0 stevel */ 423 0 stevel if (p == curproc) { 424 0 stevel prbarrier(p); 425 0 stevel /* 426 0 stevel * If the current lwp has been requested to stop, do so now. 427 0 stevel * Otherwise we have a race condition between /proc attempting 428 0 stevel * to stop the process and this thread creating a new lwp 429 0 stevel * that was not seen when the /proc PCSTOP request was issued. 430 0 stevel * We rely on stop() to call prbarrier(p) before returning. 431 0 stevel */ 432 0 stevel while ((curthread->t_proc_flag & TP_PRSTOP) && 433 10541 Gangadhar !ttolwp(curthread)->lwp_nostop) { 434 10541 Gangadhar /* 435 10541 Gangadhar * We called pool_barrier_enter() before calling 436 10541 Gangadhar * here to lwp_create(). We have to call 437 10541 Gangadhar * pool_barrier_exit() before stopping. 438 10541 Gangadhar */ 439 10541 Gangadhar pool_barrier_exit(); 440 10541 Gangadhar prbarrier(p); 441 0 stevel stop(PR_REQUESTED, 0); 442 10541 Gangadhar /* 443 10541 Gangadhar * And we have to repeat the call to 444 10541 Gangadhar * pool_barrier_enter after stopping. 445 10541 Gangadhar */ 446 10541 Gangadhar pool_barrier_enter(); 447 10541 Gangadhar prbarrier(p); 448 10541 Gangadhar } 449 0 stevel 450 0 stevel /* 451 0 stevel * If process is exiting, there could be a race between 452 0 stevel * the agent lwp creation and the new lwp currently being 453 0 stevel * created. So to prevent this race lwp creation is failed 454 0 stevel * if the process is exiting. 455 0 stevel */ 456 0 stevel if (p->p_flag & (SEXITLWPS|SKILLED)) { 457 0 stevel err = 1; 458 0 stevel goto error; 459 0 stevel } 460 0 stevel 461 0 stevel /* 462 0 stevel * Since we might have dropped p->p_lock, the 463 0 stevel * lwp directory free list might have changed. 464 0 stevel */ 465 0 stevel if (p->p_lwpfree == NULL) 466 0 stevel goto grow; 467 0 stevel } 468 0 stevel 469 0 stevel kpreempt_disable(); /* can't grab cpu_lock here */ 470 0 stevel 471 0 stevel /* 472 11173 Jonathan * Inherit processor and processor set bindings from curthread. 473 11173 Jonathan * 474 11173 Jonathan * For kernel LWPs, we do not inherit processor set bindings at 475 11173 Jonathan * process creation time (i.e. when p != curproc). After the 476 11173 Jonathan * kernel process is created, any subsequent LWPs must be created 477 11173 Jonathan * by threads in the kernel process, at which point we *will* 478 11173 Jonathan * inherit processor set bindings. 479 0 stevel */ 480 11173 Jonathan if (CLASS_KERNEL(cid) && p != curproc) { 481 0 stevel t->t_bind_cpu = binding = PBIND_NONE; 482 0 stevel t->t_cpupart = oldpart = &cp_default; 483 0 stevel t->t_bind_pset = PS_NONE; 484 6298 akolb t->t_bindflag = (uchar_t)default_binding_mode; 485 0 stevel } else { 486 0 stevel binding = curthread->t_bind_cpu; 487 0 stevel t->t_bind_cpu = binding; 488 0 stevel oldpart = t->t_cpupart; 489 0 stevel t->t_cpupart = curthread->t_cpupart; 490 0 stevel t->t_bind_pset = curthread->t_bind_pset; 491 6298 akolb t->t_bindflag = curthread->t_bindflag | 492 6298 akolb (uchar_t)default_binding_mode; 493 0 stevel } 494 0 stevel 495 0 stevel /* 496 0 stevel * thread_create() initializes this thread's home lgroup to the root. 497 0 stevel * Choose a more suitable lgroup, since this thread is associated 498 0 stevel * with an lwp. 499 0 stevel */ 500 0 stevel ASSERT(oldpart != NULL); 501 0 stevel if (binding != PBIND_NONE && t->t_affinitycnt == 0) { 502 0 stevel t->t_bound_cpu = cpu[binding]; 503 0 stevel if (t->t_lpl != t->t_bound_cpu->cpu_lpl) 504 0 stevel lgrp_move_thread(t, t->t_bound_cpu->cpu_lpl, 1); 505 0 stevel } else { 506 0 stevel lgrp_move_thread(t, lgrp_choose(t, t->t_cpupart), 1); 507 0 stevel } 508 0 stevel 509 0 stevel kpreempt_enable(); 510 0 stevel 511 0 stevel /* 512 0 stevel * make sure lpl points to our own partition 513 0 stevel */ 514 0 stevel ASSERT(t->t_lpl >= t->t_cpupart->cp_lgrploads); 515 0 stevel ASSERT(t->t_lpl < t->t_cpupart->cp_lgrploads + 516 0 stevel t->t_cpupart->cp_nlgrploads); 517 0 stevel 518 0 stevel /* 519 0 stevel * If we're creating a new process, then inherit the project from our 520 0 stevel * parent. If we're only creating an additional lwp then use the 521 0 stevel * project pointer of the target process. 522 0 stevel */ 523 0 stevel if (p->p_task == NULL) 524 0 stevel newkpj = ttoproj(curthread); 525 0 stevel else 526 0 stevel newkpj = p->p_task->tk_proj; 527 0 stevel 528 0 stevel /* 529 0 stevel * It is safe to point the thread to the new project without holding it 530 0 stevel * since we're holding the target process' p_lock here and therefore 531 0 stevel * we're guaranteed that it will not move to another project. 532 0 stevel */ 533 0 stevel oldkpj = ttoproj(t); 534 0 stevel if (newkpj != oldkpj) { 535 0 stevel t->t_proj = newkpj; 536 0 stevel (void) project_hold(newkpj); 537 0 stevel project_rele(oldkpj); 538 0 stevel } 539 0 stevel 540 0 stevel if (cid != NOCLASS) { 541 0 stevel /* 542 0 stevel * If the lwp is being created in the current process 543 0 stevel * and matches the current thread's scheduling class, 544 0 stevel * we should propagate the current thread's scheduling 545 0 stevel * parameters by calling CL_FORK. Otherwise just use 546 0 stevel * the defaults by calling CL_ENTERCLASS. 547 0 stevel */ 548 0 stevel if (p != curproc || curthread->t_cid != cid) { 549 0 stevel err = CL_ENTERCLASS(t, cid, NULL, NULL, bufp); 550 0 stevel t->t_pri = pri; /* CL_ENTERCLASS may have changed it */ 551 6247 raf /* 552 6247 raf * We don't call schedctl_set_cidpri(t) here 553 6247 raf * because the schedctl data is not yet set 554 6247 raf * up for the newly-created lwp. 555 6247 raf */ 556 0 stevel } else { 557 0 stevel t->t_clfuncs = &(sclass[cid].cl_funcs->thread); 558 0 stevel err = CL_FORK(curthread, t, bufp); 559 0 stevel t->t_cid = cid; 560 0 stevel } 561 0 stevel if (err) 562 0 stevel goto error; 563 0 stevel else 564 0 stevel bufp = NULL; 565 0 stevel } 566 0 stevel 567 0 stevel /* 568 0 stevel * If we were given an lwpid then use it, else allocate one. 569 0 stevel */ 570 0 stevel if (lwpid != 0) 571 0 stevel t->t_tid = lwpid; 572 0 stevel else { 573 0 stevel /* 574 0 stevel * lwp/thread id 0 is never valid; reserved for special checks. 575 0 stevel * lwp/thread id 1 is reserved for the main thread. 576 0 stevel * Start again at 2 when INT_MAX has been reached 577 0 stevel * (id_t is a signed 32-bit integer). 578 0 stevel */ 579 0 stevel id_t prev_id = p->p_lwpid; /* last allocated tid */ 580 0 stevel 581 0 stevel do { /* avoid lwpid duplication */ 582 0 stevel if (p->p_lwpid == INT_MAX) { 583 0 stevel p->p_flag |= SLWPWRAP; 584 0 stevel p->p_lwpid = 1; 585 0 stevel } 586 0 stevel if ((t->t_tid = ++p->p_lwpid) == prev_id) { 587 0 stevel /* 588 0 stevel * All lwpids are allocated; fail the request. 589 0 stevel */ 590 0 stevel err = 1; 591 0 stevel goto error; 592 0 stevel } 593 0 stevel /* 594 0 stevel * We only need to worry about colliding with an id 595 0 stevel * that's already in use if this process has 596 0 stevel * cycled through all available lwp ids. 597 0 stevel */ 598 0 stevel if ((p->p_flag & SLWPWRAP) == 0) 599 0 stevel break; 600 0 stevel } while (lwp_hash_lookup(p, t->t_tid) != NULL); 601 0 stevel } 602 2712 nn35248 603 2712 nn35248 /* 604 2712 nn35248 * If this is a branded process, let the brand do any necessary lwp 605 2712 nn35248 * initialization. 606 2712 nn35248 */ 607 2712 nn35248 if (PROC_IS_BRANDED(p)) { 608 2712 nn35248 if (BROP(p)->b_initlwp(lwp)) { 609 2712 nn35248 err = 1; 610 2712 nn35248 goto error; 611 2712 nn35248 } 612 2712 nn35248 branded = 1; 613 2712 nn35248 } 614 2712 nn35248 615 4426 aguzovsk if (t->t_tid == 1) { 616 4426 aguzovsk kpreempt_disable(); 617 4426 aguzovsk ASSERT(t->t_lpl != NULL); 618 4426 aguzovsk p->p_t1_lgrpid = t->t_lpl->lpl_lgrpid; 619 4426 aguzovsk kpreempt_enable(); 620 4426 aguzovsk if (p->p_tr_lgrpid != LGRP_NONE && 621 4426 aguzovsk p->p_tr_lgrpid != p->p_t1_lgrpid) { 622 4426 aguzovsk lgrp_update_trthr_migrations(1); 623 4426 aguzovsk } 624 4426 aguzovsk } 625 4426 aguzovsk 626 0 stevel p->p_lwpcnt++; 627 0 stevel t->t_waitfor = -1; 628 0 stevel 629 0 stevel /* 630 0 stevel * Turn microstate accounting on for thread if on for process. 631 0 stevel */ 632 0 stevel if (p->p_flag & SMSACCT) 633 0 stevel t->t_proc_flag |= TP_MSACCT; 634 0 stevel 635 0 stevel /* 636 0 stevel * If the process has watchpoints, mark the new thread as such. 637 0 stevel */ 638 0 stevel if (pr_watch_active(p)) 639 0 stevel watch_enable(t); 640 0 stevel 641 0 stevel /* 642 0 stevel * The lwp is being created in the stopped state. 643 0 stevel * We set all the necessary flags to indicate that fact here. 644 0 stevel * We omit the TS_CREATE flag from t_schedflag so that the lwp 645 0 stevel * cannot be set running until the caller is finished with it, 646 0 stevel * even if lwp_continue() is called on it after we drop p->p_lock. 647 0 stevel * When the caller is finished with the newly-created lwp, 648 0 stevel * the caller must call lwp_create_done() to allow the lwp 649 0 stevel * to be set running. If the TP_HOLDLWP is left set, the 650 0 stevel * lwp will suspend itself after reaching system call exit. 651 0 stevel */ 652 0 stevel init_mstate(t, LMS_STOPPED); 653 0 stevel t->t_proc_flag |= TP_HOLDLWP; 654 0 stevel t->t_schedflag |= (TS_ALLSTART & ~(TS_CSTART | TS_CREATE)); 655 0 stevel t->t_whystop = PR_SUSPENDED; 656 0 stevel t->t_whatstop = SUSPEND_NORMAL; 657 0 stevel t->t_sig_check = 1; /* ensure that TP_HOLDLWP is honored */ 658 0 stevel 659 0 stevel /* 660 0 stevel * Set system call processing flags in case tracing or profiling 661 0 stevel * is set. The first system call will evaluate these and turn 662 0 stevel * them off if they aren't needed. 663 0 stevel */ 664 0 stevel t->t_pre_sys = 1; 665 0 stevel t->t_post_sys = 1; 666 0 stevel 667 0 stevel /* 668 0 stevel * Insert the new thread into the list of all threads. 669 0 stevel */ 670 0 stevel if ((tx = p->p_tlist) == NULL) { 671 0 stevel t->t_back = t; 672 0 stevel t->t_forw = t; 673 0 stevel p->p_tlist = t; 674 0 stevel } else { 675 0 stevel t->t_forw = tx; 676 0 stevel t->t_back = tx->t_back; 677 0 stevel tx->t_back->t_forw = t; 678 0 stevel tx->t_back = t; 679 0 stevel } 680 0 stevel 681 0 stevel /* 682 0 stevel * Insert the new lwp into an lwp directory slot position 683 0 stevel * and into the lwpid hash table. 684 0 stevel */ 685 0 stevel lep->le_thread = t; 686 0 stevel lep->le_lwpid = t->t_tid; 687 0 stevel lep->le_start = t->t_start; 688 9393 Roger lwp_hash_in(p, lep, p->p_tidhash, p->p_tidhash_sz, 1); 689 0 stevel 690 0 stevel if (state == TS_RUN) { 691 0 stevel /* 692 0 stevel * We set the new lwp running immediately. 693 0 stevel */ 694 0 stevel t->t_proc_flag &= ~TP_HOLDLWP; 695 0 stevel lwp_create_done(t); 696 0 stevel } 697 0 stevel 698 0 stevel error: 699 0 stevel if (err) { 700 11173 Jonathan if (CLASS_KERNEL(cid)) { 701 11173 Jonathan /* 702 11173 Jonathan * This should only happen if a system process runs 703 11173 Jonathan * out of lwpids, which shouldn't occur. 704 11173 Jonathan */ 705 11173 Jonathan panic("Failed to create a system LWP"); 706 11173 Jonathan } 707 0 stevel /* 708 0 stevel * We have failed to create an lwp, so decrement the number 709 0 stevel * of lwps in the task and let the lgroup load averages know 710 0 stevel * that this thread isn't going to show up. 711 0 stevel */ 712 0 stevel kpreempt_disable(); 713 0 stevel lgrp_move_thread(t, NULL, 1); 714 0 stevel kpreempt_enable(); 715 0 stevel 716 0 stevel ASSERT(MUTEX_HELD(&p->p_lock)); 717 0 stevel mutex_enter(&p->p_zone->zone_nlwps_lock); 718 0 stevel p->p_task->tk_nlwps--; 719 0 stevel p->p_task->tk_proj->kpj_nlwps--; 720 0 stevel p->p_zone->zone_nlwps--; 721 0 stevel mutex_exit(&p->p_zone->zone_nlwps_lock); 722 0 stevel if (cid != NOCLASS && bufp != NULL) 723 0 stevel CL_FREE(cid, bufp); 724 2712 nn35248 725 2712 nn35248 if (branded) 726 2712 nn35248 BROP(p)->b_freelwp(lwp); 727 0 stevel 728 0 stevel mutex_exit(&p->p_lock); 729 0 stevel t->t_state = TS_FREE; 730 0 stevel thread_rele(t); 731 0 stevel 732 0 stevel /* 733 0 stevel * We need to remove t from the list of all threads 734 0 stevel * because thread_exit()/lwp_exit() isn't called on t. 735 0 stevel */ 736 0 stevel mutex_enter(&pidlock); 737 0 stevel ASSERT(t != t->t_next); /* t0 never exits */ 738 0 stevel t->t_next->t_prev = t->t_prev; 739 0 stevel t->t_prev->t_next = t->t_next; 740 0 stevel mutex_exit(&pidlock); 741 0 stevel 742 0 stevel thread_free(t); 743 0 stevel kmem_free(lep, sizeof (*lep)); 744 0 stevel lwp = NULL; 745 0 stevel } else { 746 0 stevel mutex_exit(&p->p_lock); 747 0 stevel } 748 0 stevel 749 9393 Roger if (old_dir != NULL) 750 0 stevel kmem_free(old_dir, old_dirsz * sizeof (*old_dir)); 751 9393 Roger if (old_hash != NULL) 752 0 stevel kmem_free(old_hash, old_hashsz * sizeof (*old_hash)); 753 9393 Roger if (ret_tidhash != NULL) 754 9393 Roger kmem_free(ret_tidhash, sizeof (ret_tidhash_t)); 755 0 stevel 756 0 stevel DTRACE_PROC1(lwp__create, kthread_t *, t); 757 0 stevel return (lwp); 758 0 stevel } 759 0 stevel 760 0 stevel /* 761 0 stevel * lwp_create_done() is called by the caller of lwp_create() to set the 762 0 stevel * newly-created lwp running after the caller has finished manipulating it. 763 0 stevel */ 764 0 stevel void 765 0 stevel lwp_create_done(kthread_t *t) 766 0 stevel { 767 0 stevel proc_t *p = ttoproc(t); 768 0 stevel 769 0 stevel ASSERT(MUTEX_HELD(&p->p_lock)); 770 0 stevel 771 0 stevel /* 772 0 stevel * We set the TS_CREATE and TS_CSTART flags and call setrun_locked(). 773 0 stevel * (The absence of the TS_CREATE flag prevents the lwp from running 774 0 stevel * until we are finished with it, even if lwp_continue() is called on 775 0 stevel * it by some other lwp in the process or elsewhere in the kernel.) 776 0 stevel */ 777 0 stevel thread_lock(t); 778 0 stevel ASSERT(t->t_state == TS_STOPPED && !(t->t_schedflag & TS_CREATE)); 779 0 stevel /* 780 0 stevel * If TS_CSTART is set, lwp_continue(t) has been called and 781 0 stevel * has already incremented p_lwprcnt; avoid doing this twice. 782 0 stevel */ 783 0 stevel if (!(t->t_schedflag & TS_CSTART)) 784 0 stevel p->p_lwprcnt++; 785 0 stevel t->t_schedflag |= (TS_CSTART | TS_CREATE); 786 0 stevel setrun_locked(t); 787 0 stevel thread_unlock(t); 788 0 stevel } 789 0 stevel 790 0 stevel /* 791 0 stevel * Copy an LWP's active templates, and clear the latest contracts. 792 0 stevel */ 793 0 stevel void 794 0 stevel lwp_ctmpl_copy(klwp_t *dst, klwp_t *src) 795 0 stevel { 796 0 stevel int i; 797 0 stevel 798 0 stevel for (i = 0; i < ct_ntypes; i++) { 799 0 stevel dst->lwp_ct_active[i] = ctmpl_dup(src->lwp_ct_active[i]); 800 0 stevel dst->lwp_ct_latest[i] = NULL; 801 0 stevel } 802 0 stevel } 803 0 stevel 804 0 stevel /* 805 0 stevel * Clear an LWP's contract template state. 806 0 stevel */ 807 0 stevel void 808 0 stevel lwp_ctmpl_clear(klwp_t *lwp) 809 0 stevel { 810 0 stevel ct_template_t *tmpl; 811 0 stevel int i; 812 0 stevel 813 0 stevel for (i = 0; i < ct_ntypes; i++) { 814 0 stevel if ((tmpl = lwp->lwp_ct_active[i]) != NULL) { 815 0 stevel ctmpl_free(tmpl); 816 0 stevel lwp->lwp_ct_active[i] = NULL; 817 0 stevel } 818 0 stevel 819 0 stevel if (lwp->lwp_ct_latest[i] != NULL) { 820 0 stevel contract_rele(lwp->lwp_ct_latest[i]); 821 0 stevel lwp->lwp_ct_latest[i] = NULL; 822 0 stevel } 823 0 stevel } 824 0 stevel } 825 0 stevel 826 0 stevel /* 827 0 stevel * Individual lwp exit. 828 0 stevel * If this is the last lwp, exit the whole process. 829 0 stevel */ 830 0 stevel void 831 0 stevel lwp_exit(void) 832 0 stevel { 833 0 stevel kthread_t *t = curthread; 834 0 stevel klwp_t *lwp = ttolwp(t); 835 0 stevel proc_t *p = ttoproc(t); 836 0 stevel 837 0 stevel ASSERT(MUTEX_HELD(&p->p_lock)); 838 0 stevel 839 0 stevel mutex_exit(&p->p_lock); 840 0 stevel 841 0 stevel #if defined(__sparc) 842 0 stevel /* 843 0 stevel * Ensure that the user stack is fully abandoned.. 844 0 stevel */ 845 0 stevel trash_user_windows(); 846 0 stevel #endif 847 0 stevel 848 0 stevel tsd_exit(); /* free thread specific data */ 849 0 stevel 850 0 stevel kcpc_passivate(); /* Clean up performance counter state */ 851 0 stevel 852 0 stevel pollcleanup(); 853 0 stevel 854 0 stevel if (t->t_door) 855 0 stevel door_slam(); 856 0 stevel 857 0 stevel if (t->t_schedctl != NULL) 858 0 stevel schedctl_lwp_cleanup(t); 859 0 stevel 860 0 stevel if (t->t_upimutex != NULL) 861 0 stevel upimutex_cleanup(); 862 2712 nn35248 863 2712 nn35248 /* 864 2712 nn35248 * Perform any brand specific exit processing, then release any 865 2712 nn35248 * brand data associated with the lwp 866 2712 nn35248 */ 867 2712 nn35248 if (PROC_IS_BRANDED(p)) 868 2712 nn35248 BROP(p)->b_lwpexit(lwp); 869 0 stevel 870 0 stevel mutex_enter(&p->p_lock); 871 0 stevel lwp_cleanup(); 872 0 stevel 873 0 stevel /* 874 0 stevel * When this process is dumping core, its lwps are held here 875 0 stevel * until the core dump is finished. Then exitlwps() is called 876 0 stevel * again to release these lwps so that they can finish exiting. 877 0 stevel */ 878 0 stevel if (p->p_flag & SCOREDUMP) 879 0 stevel stop(PR_SUSPENDED, SUSPEND_NORMAL); 880 0 stevel 881 0 stevel /* 882 0 stevel * Block the process against /proc now that we have really acquired 883 0 stevel * p->p_lock (to decrement p_lwpcnt and manipulate p_tlist at least). 884 0 stevel */ 885 0 stevel prbarrier(p); 886 0 stevel 887 0 stevel /* 888 0 stevel * Call proc_exit() if this is the last non-daemon lwp in the process. 889 0 stevel */ 890 0 stevel if (!(t->t_proc_flag & TP_DAEMON) && 891 0 stevel p->p_lwpcnt == p->p_lwpdaemon + 1) { 892 0 stevel mutex_exit(&p->p_lock); 893 390 raf if (proc_exit(CLD_EXITED, 0) == 0) { 894 0 stevel /* Restarting init. */ 895 0 stevel return; 896 0 stevel } 897 0 stevel 898 0 stevel /* 899 0 stevel * proc_exit() returns a non-zero value when some other 900 0 stevel * lwp got there first. We just have to continue in 901 0 stevel * lwp_exit(). 902 0 stevel */ 903 0 stevel mutex_enter(&p->p_lock); 904 0 stevel ASSERT(curproc->p_flag & SEXITLWPS); 905 0 stevel prbarrier(p); 906 0 stevel } 907 0 stevel 908 0 stevel DTRACE_PROC(lwp__exit); 909 0 stevel 910 0 stevel /* 911 0 stevel * If the lwp is a detached lwp or if the process is exiting, 912 0 stevel * remove (lwp_hash_out()) the lwp from the lwp directory. 913 0 stevel * Otherwise null out the lwp's le_thread pointer in the lwp 914 0 stevel * directory so that other threads will see it as a zombie lwp. 915 0 stevel */ 916 0 stevel prlwpexit(t); /* notify /proc */ 917 0 stevel if (!(t->t_proc_flag & TP_TWAIT) || (p->p_flag & SEXITLWPS)) 918 0 stevel lwp_hash_out(p, t->t_tid); 919 0 stevel else { 920 0 stevel ASSERT(!(t->t_proc_flag & TP_DAEMON)); 921 0 stevel p->p_lwpdir[t->t_dslot].ld_entry->le_thread = NULL; 922 0 stevel p->p_zombcnt++; 923 0 stevel cv_broadcast(&p->p_lwpexit); 924 0 stevel } 925 0 stevel if (t->t_proc_flag & TP_DAEMON) { 926 0 stevel p->p_lwpdaemon--; 927 0 stevel t->t_proc_flag &= ~TP_DAEMON; 928 0 stevel } 929 0 stevel t->t_proc_flag &= ~TP_TWAIT; 930 0 stevel 931 0 stevel /* 932 0 stevel * Maintain accurate lwp count for task.max-lwps resource control. 933 0 stevel */ 934 0 stevel mutex_enter(&p->p_zone->zone_nlwps_lock); 935 0 stevel p->p_task->tk_nlwps--; 936 0 stevel p->p_task->tk_proj->kpj_nlwps--; 937 0 stevel p->p_zone->zone_nlwps--; 938 0 stevel mutex_exit(&p->p_zone->zone_nlwps_lock); 939 0 stevel 940 0 stevel CL_EXIT(t); /* tell the scheduler that t is exiting */ 941 0 stevel ASSERT(p->p_lwpcnt != 0); 942 0 stevel p->p_lwpcnt--; 943 0 stevel 944 0 stevel /* 945 0 stevel * If all remaining non-daemon lwps are waiting in lwp_wait(), 946 0 stevel * wake them up so someone can return EDEADLK. 947 0 stevel * (See the block comment preceeding lwp_wait().) 948 0 stevel */ 949 0 stevel if (p->p_lwpcnt == p->p_lwpdaemon + (p->p_lwpwait - p->p_lwpdwait)) 950 0 stevel cv_broadcast(&p->p_lwpexit); 951 0 stevel 952 0 stevel t->t_proc_flag |= TP_LWPEXIT; 953 0 stevel term_mstate(t); 954 3792 akolb 955 0 stevel #ifndef NPROBE 956 0 stevel /* Kernel probe */ 957 0 stevel if (t->t_tnf_tpdp) 958 0 stevel tnf_thread_exit(); 959 0 stevel #endif /* NPROBE */ 960 0 stevel 961 0 stevel t->t_forw->t_back = t->t_back; 962 0 stevel t->t_back->t_forw = t->t_forw; 963 0 stevel if (t == p->p_tlist) 964 0 stevel p->p_tlist = t->t_forw; 965 0 stevel 966 0 stevel /* 967 0 stevel * Clean up the signal state. 968 0 stevel */ 969 0 stevel if (t->t_sigqueue != NULL) 970 0 stevel sigdelq(p, t, 0); 971 0 stevel if (lwp->lwp_curinfo != NULL) { 972 0 stevel siginfofree(lwp->lwp_curinfo); 973 0 stevel lwp->lwp_curinfo = NULL; 974 0 stevel } 975 0 stevel 976 0 stevel thread_rele(t); 977 0 stevel 978 0 stevel /* 979 0 stevel * Terminated lwps are associated with process zero and are put onto 980 0 stevel * death-row by resume(). Avoid preemption after resetting t->t_procp. 981 0 stevel */ 982 0 stevel t->t_preempt++; 983 1217 rab 984 1217 rab if (t->t_ctx != NULL) 985 1217 rab exitctx(t); 986 1217 rab if (p->p_pctx != NULL) 987 1217 rab exitpctx(p); 988 1217 rab 989 0 stevel t->t_procp = &p0; 990 0 stevel 991 0 stevel /* 992 0 stevel * Notify the HAT about the change of address space 993 0 stevel */ 994 0 stevel hat_thread_exit(t); 995 0 stevel /* 996 0 stevel * When this is the last running lwp in this process and some lwp is 997 0 stevel * waiting for this condition to become true, or this thread was being 998 0 stevel * suspended, then the waiting lwp is awakened. 999 0 stevel * 1000 0 stevel * Also, if the process is exiting, we may have a thread waiting in 1001 0 stevel * exitlwps() that needs to be notified. 1002 0 stevel */ 1003 0 stevel if (--p->p_lwprcnt == 0 || (t->t_proc_flag & TP_HOLDLWP) || 1004 0 stevel (p->p_flag & SEXITLWPS)) 1005 0 stevel cv_broadcast(&p->p_holdlwps); 1006 0 stevel 1007 0 stevel /* 1008 0 stevel * Need to drop p_lock so we can reacquire pidlock. 1009 0 stevel */ 1010 0 stevel mutex_exit(&p->p_lock); 1011 0 stevel mutex_enter(&pidlock); 1012 0 stevel 1013 0 stevel ASSERT(t != t->t_next); /* t0 never exits */ 1014 0 stevel t->t_next->t_prev = t->t_prev; 1015 0 stevel t->t_prev->t_next = t->t_next; 1016 0 stevel cv_broadcast(&t->t_joincv); /* wake up anyone in thread_join */ 1017 0 stevel mutex_exit(&pidlock); 1018 0 stevel 1019 0 stevel lwp_pcb_exit(); 1020 0 stevel 1021 0 stevel t->t_state = TS_ZOMB; 1022 0 stevel swtch_from_zombie(); 1023 0 stevel /* never returns */ 1024 0 stevel } 1025 0 stevel 1026 0 stevel 1027 0 stevel /* 1028 0 stevel * Cleanup function for an exiting lwp. 1029 0 stevel * Called both from lwp_exit() and from proc_exit(). 1030 0 stevel * p->p_lock is repeatedly released and grabbed in this function. 1031 0 stevel */ 1032 0 stevel void 1033 0 stevel lwp_cleanup(void) 1034 0 stevel { 1035 0 stevel kthread_t *t = curthread; 1036 0 stevel proc_t *p = ttoproc(t); 1037 0 stevel 1038 0 stevel ASSERT(MUTEX_HELD(&p->p_lock)); 1039 0 stevel 1040 0 stevel /* untimeout any lwp-bound realtime timers */ 1041 0 stevel if (p->p_itimer != NULL) 1042 0 stevel timer_lwpexit(); 1043 0 stevel 1044 0 stevel /* 1045 0 stevel * If this is the /proc agent lwp that is exiting, readjust p_lwpid 1046 0 stevel * so it appears that the agent never existed, and clear p_agenttp. 1047 0 stevel */ 1048 0 stevel if (t == p->p_agenttp) { 1049 0 stevel ASSERT(t->t_tid == p->p_lwpid); 1050 0 stevel p->p_lwpid--; 1051 0 stevel p->p_agenttp = NULL; 1052 0 stevel } 1053 0 stevel 1054 0 stevel /* 1055 0 stevel * Do lgroup bookkeeping to account for thread exiting. 1056 0 stevel */ 1057 0 stevel kpreempt_disable(); 1058 0 stevel lgrp_move_thread(t, NULL, 1); 1059 4426 aguzovsk if (t->t_tid == 1) { 1060 4426 aguzovsk p->p_t1_lgrpid = LGRP_NONE; 1061 4426 aguzovsk } 1062 0 stevel kpreempt_enable(); 1063 0 stevel 1064 0 stevel lwp_ctmpl_clear(ttolwp(t)); 1065 0 stevel } 1066 0 stevel 1067 0 stevel int 1068 0 stevel lwp_suspend(kthread_t *t) 1069 0 stevel { 1070 0 stevel int tid; 1071 0 stevel proc_t *p = ttoproc(t); 1072 0 stevel 1073 0 stevel ASSERT(MUTEX_HELD(&p->p_lock)); 1074 0 stevel 1075 0 stevel /* 1076 0 stevel * Set the thread's TP_HOLDLWP flag so it will stop in holdlwp(). 1077 0 stevel * If an lwp is stopping itself, there is no need to wait. 1078 0 stevel */ 1079 875 raf top: 1080 0 stevel t->t_proc_flag |= TP_HOLDLWP; 1081 0 stevel if (t == curthread) { 1082 0 stevel t->t_sig_check = 1; 1083 0 stevel } else { 1084 0 stevel /* 1085 0 stevel * Make sure the lwp stops promptly. 1086 0 stevel */ 1087 0 stevel thread_lock(t); 1088 0 stevel t->t_sig_check = 1; 1089 0 stevel /* 1090 0 stevel * XXX Should use virtual stop like /proc does instead of 1091 0 stevel * XXX waking the thread to get it to stop. 1092 0 stevel */ 1093 3792 akolb if (ISWAKEABLE(t) || ISWAITING(t)) { 1094 0 stevel setrun_locked(t); 1095 3792 akolb } else if (t->t_state == TS_ONPROC && t->t_cpu != CPU) { 1096 0 stevel poke_cpu(t->t_cpu->cpu_id); 1097 3792 akolb } 1098 3792 akolb 1099 0 stevel tid = t->t_tid; /* remember thread ID */ 1100 0 stevel /* 1101 0 stevel * Wait for lwp to stop 1102 0 stevel */ 1103 0 stevel while (!SUSPENDED(t)) { 1104 0 stevel /* 1105 0 stevel * Drop the thread lock before waiting and reacquire it 1106 0 stevel * afterwards, so the thread can change its t_state 1107 0 stevel * field. 1108 0 stevel */ 1109 0 stevel thread_unlock(t); 1110 0 stevel 1111 0 stevel /* 1112 0 stevel * Check if aborted by exitlwps(). 1113 0 stevel */ 1114 0 stevel if (p->p_flag & SEXITLWPS) 1115 0 stevel lwp_exit(); 1116 0 stevel 1117 0 stevel /* 1118 0 stevel * Cooperate with jobcontrol signals and /proc stopping 1119 0 stevel * by calling cv_wait_sig() to wait for the target 1120 0 stevel * lwp to stop. Just using cv_wait() can lead to 1121 0 stevel * deadlock because, if some other lwp has stopped 1122 0 stevel * by either of these mechanisms, then p_lwprcnt will 1123 0 stevel * never become zero if we do a cv_wait(). 1124 0 stevel */ 1125 0 stevel if (!cv_wait_sig(&p->p_holdlwps, &p->p_lock)) 1126 0 stevel return (EINTR); 1127 0 stevel 1128 0 stevel /* 1129 0 stevel * Check to see if thread died while we were 1130 0 stevel * waiting for it to suspend. 1131 0 stevel */ 1132 0 stevel if (idtot(p, tid) == NULL) 1133 0 stevel return (ESRCH); 1134 0 stevel 1135 0 stevel thread_lock(t); 1136 0 stevel /* 1137 875 raf * If the TP_HOLDLWP flag went away, lwp_continue() 1138 875 raf * or vfork() must have been called while we were 1139 875 raf * waiting, so start over again. 1140 0 stevel */ 1141 0 stevel if ((t->t_proc_flag & TP_HOLDLWP) == 0) { 1142 0 stevel thread_unlock(t); 1143 875 raf goto top; 1144 0 stevel } 1145 0 stevel } 1146 0 stevel thread_unlock(t); 1147 0 stevel } 1148 0 stevel return (0); 1149 0 stevel } 1150 0 stevel 1151 0 stevel /* 1152 0 stevel * continue a lwp that's been stopped by lwp_suspend(). 1153 0 stevel */ 1154 0 stevel void 1155 0 stevel lwp_continue(kthread_t *t) 1156 0 stevel { 1157 0 stevel proc_t *p = ttoproc(t); 1158 0 stevel int was_suspended = t->t_proc_flag & TP_HOLDLWP; 1159 0 stevel 1160 0 stevel ASSERT(MUTEX_HELD(&p->p_lock)); 1161 0 stevel 1162 0 stevel t->t_proc_flag &= ~TP_HOLDLWP; 1163 0 stevel thread_lock(t); 1164 0 stevel if (SUSPENDED(t) && 1165 0 stevel !(p->p_flag & (SHOLDFORK | SHOLDFORK1 | SHOLDWATCH))) { 1166 0 stevel p->p_lwprcnt++; 1167 0 stevel t->t_schedflag |= TS_CSTART; 1168 0 stevel setrun_locked(t); 1169 0 stevel } 1170 0 stevel thread_unlock(t); 1171 0 stevel /* 1172 0 stevel * Wakeup anyone waiting for this thread to be suspended 1173 0 stevel */ 1174 0 stevel if (was_suspended) 1175 0 stevel cv_broadcast(&p->p_holdlwps); 1176 0 stevel } 1177 0 stevel 1178 0 stevel /* 1179 0 stevel * ******************************** 1180 0 stevel * Miscellaneous lwp routines * 1181 0 stevel * ******************************** 1182 0 stevel */ 1183 0 stevel /* 1184 0 stevel * When a process is undergoing a forkall(), its p_flag is set to SHOLDFORK. 1185 0 stevel * This will cause the process's lwps to stop at a hold point. A hold 1186 0 stevel * point is where a kernel thread has a flat stack. This is at the 1187 0 stevel * return from a system call and at the return from a user level trap. 1188 0 stevel * 1189 0 stevel * When a process is undergoing a fork1() or vfork(), its p_flag is set to 1190 0 stevel * SHOLDFORK1. This will cause the process's lwps to stop at a modified 1191 0 stevel * hold point. The lwps in the process are not being cloned, so they 1192 0 stevel * are held at the usual hold points and also within issig_forreal(). 1193 0 stevel * This has the side-effect that their system calls do not return 1194 0 stevel * showing EINTR. 1195 0 stevel * 1196 0 stevel * An lwp can also be held. This is identified by the TP_HOLDLWP flag on 1197 0 stevel * the thread. The TP_HOLDLWP flag is set in lwp_suspend(), where the active 1198 0 stevel * lwp is waiting for the target lwp to be stopped. 1199 0 stevel */ 1200 0 stevel void 1201 0 stevel holdlwp(void) 1202 0 stevel { 1203 0 stevel proc_t *p = curproc; 1204 0 stevel kthread_t *t = curthread; 1205 0 stevel 1206 0 stevel mutex_enter(&p->p_lock); 1207 0 stevel /* 1208 0 stevel * Don't terminate immediately if the process is dumping core. 1209 0 stevel * Once the process has dumped core, all lwps are terminated. 1210 0 stevel */ 1211 0 stevel if (!(p->p_flag & SCOREDUMP)) { 1212 0 stevel if ((p->p_flag & SEXITLWPS) || (t->t_proc_flag & TP_EXITLWP)) 1213 0 stevel lwp_exit(); 1214 0 stevel } 1215 0 stevel if (!(ISHOLD(p)) && !(p->p_flag & (SHOLDFORK1 | SHOLDWATCH))) { 1216 0 stevel mutex_exit(&p->p_lock); 1217 0 stevel return; 1218 0 stevel } 1219 0 stevel /* 1220 0 stevel * stop() decrements p->p_lwprcnt and cv_signal()s &p->p_holdlwps 1221 0 stevel * when p->p_lwprcnt becomes zero. 1222 0 stevel */ 1223 0 stevel stop(PR_SUSPENDED, SUSPEND_NORMAL); 1224 0 stevel if (p->p_flag & SEXITLWPS) 1225 0 stevel lwp_exit(); 1226 0 stevel mutex_exit(&p->p_lock); 1227 0 stevel } 1228 0 stevel 1229 0 stevel /* 1230 0 stevel * Have all lwps within the process hold at a point where they are 1231 0 stevel * cloneable (SHOLDFORK) or just safe w.r.t. fork1 (SHOLDFORK1). 1232 0 stevel */ 1233 0 stevel int 1234 0 stevel holdlwps(int holdflag) 1235 0 stevel { 1236 0 stevel proc_t *p = curproc; 1237 0 stevel 1238 0 stevel ASSERT(holdflag == SHOLDFORK || holdflag == SHOLDFORK1); 1239 0 stevel mutex_enter(&p->p_lock); 1240 0 stevel schedctl_finish_sigblock(curthread); 1241 0 stevel again: 1242 0 stevel while (p->p_flag & (SEXITLWPS | SHOLDFORK | SHOLDFORK1 | SHOLDWATCH)) { 1243 0 stevel /* 1244 0 stevel * If another lwp is doing a forkall() or proc_exit(), bail out. 1245 0 stevel */ 1246 0 stevel if (p->p_flag & (SEXITLWPS | SHOLDFORK)) { 1247 0 stevel mutex_exit(&p->p_lock); 1248 0 stevel return (0); 1249 0 stevel } 1250 0 stevel /* 1251 0 stevel * Another lwp is doing a fork1() or is undergoing 1252 0 stevel * watchpoint activity. We hold here for it to complete. 1253 0 stevel */ 1254 0 stevel stop(PR_SUSPENDED, SUSPEND_NORMAL); 1255 0 stevel } 1256 0 stevel p->p_flag |= holdflag; 1257 0 stevel pokelwps(p); 1258 0 stevel --p->p_lwprcnt; 1259 0 stevel /* 1260 0 stevel * Wait for the process to become quiescent (p->p_lwprcnt == 0). 1261 0 stevel */ 1262 0 stevel while (p->p_lwprcnt > 0) { 1263 0 stevel /* 1264 0 stevel * Check if aborted by exitlwps(). 1265 0 stevel * Also check if SHOLDWATCH is set; it takes precedence. 1266 0 stevel */ 1267 0 stevel if (p->p_flag & (SEXITLWPS | SHOLDWATCH)) { 1268 0 stevel p->p_lwprcnt++; 1269 0 stevel p->p_flag &= ~holdflag; 1270 0 stevel cv_broadcast(&p->p_holdlwps); 1271 0 stevel goto again; 1272 0 stevel } 1273 0 stevel /* 1274 0 stevel * Cooperate with jobcontrol signals and /proc stopping. 1275 0 stevel * If some other lwp has stopped by either of these 1276 0 stevel * mechanisms, then p_lwprcnt will never become zero 1277 0 stevel * and the process will appear deadlocked unless we 1278 0 stevel * stop here in sympathy with the other lwp before 1279 0 stevel * doing the cv_wait() below. 1280 0 stevel * 1281 0 stevel * If the other lwp stops after we do the cv_wait(), it 1282 0 stevel * will wake us up to loop around and do the sympathy stop. 1283 0 stevel * 1284 0 stevel * Since stop() drops p->p_lock, we must start from 1285 0 stevel * the top again on returning from stop(). 1286 0 stevel */ 1287 0 stevel if (p->p_stopsig | (curthread->t_proc_flag & TP_PRSTOP)) { 1288 0 stevel int whystop = p->p_stopsig? PR_JOBCONTROL : 1289 0 stevel PR_REQUESTED; 1290 0 stevel p->p_lwprcnt++; 1291 0 stevel p->p_flag &= ~holdflag; 1292 0 stevel stop(whystop, p->p_stopsig); 1293 0 stevel goto again; 1294 0 stevel } 1295 0 stevel cv_wait(&p->p_holdlwps, &p->p_lock); 1296 0 stevel } 1297 0 stevel p->p_lwprcnt++; 1298 0 stevel p->p_flag &= ~holdflag; 1299 0 stevel mutex_exit(&p->p_lock); 1300 0 stevel return (1); 1301 0 stevel } 1302 0 stevel 1303 0 stevel /* 1304 0 stevel * See comments for holdwatch(), below. 1305 0 stevel */ 1306 0 stevel static int 1307 0 stevel holdcheck(int clearflags) 1308 0 stevel { 1309 0 stevel proc_t *p = curproc; 1310 0 stevel 1311 0 stevel /* 1312 0 stevel * If we are trying to exit, that takes precedence over anything else. 1313 0 stevel */ 1314 0 stevel if (p->p_flag & SEXITLWPS) { 1315 0 stevel p->p_lwprcnt++; 1316 0 stevel p->p_flag &= ~clearflags; 1317 0 stevel lwp_exit(); 1318 0 stevel } 1319 0 stevel 1320 0 stevel /* 1321 0 stevel * If another thread is calling fork1(), stop the current thread so the 1322 0 stevel * other can complete. 1323 0 stevel */ 1324 0 stevel if (p->p_flag & SHOLDFORK1) { 1325 0 stevel p->p_lwprcnt++; 1326 0 stevel stop(PR_SUSPENDED, SUSPEND_NORMAL); 1327 0 stevel if (p->p_flag & SEXITLWPS) { 1328 0 stevel p->p_flag &= ~clearflags; 1329 0 stevel lwp_exit(); 1330 0 stevel } 1331 0 stevel return (-1); 1332 0 stevel } 1333 0 stevel 1334 0 stevel /* 1335 0 stevel * If another thread is calling fork(), then indicate we are doing 1336 0 stevel * watchpoint activity. This will cause holdlwps() above to stop the 1337 0 stevel * forking thread, at which point we can continue with watchpoint 1338 0 stevel * activity. 1339 0 stevel */ 1340 0 stevel if (p->p_flag & SHOLDFORK) { 1341 0 stevel p->p_lwprcnt++; 1342 0 stevel while (p->p_flag & SHOLDFORK) { 1343 0 stevel p->p_flag |= SHOLDWATCH; 1344 0 stevel cv_broadcast(&p->p_holdlwps); 1345 0 stevel cv_wait(&p->p_holdlwps, &p->p_lock); 1346 0 stevel p->p_flag &= ~SHOLDWATCH; 1347 0 stevel } 1348 0 stevel return (-1); 1349 0 stevel } 1350 0 stevel 1351 0 stevel return (0); 1352 0 stevel } 1353 0 stevel 1354 0 stevel /* 1355 0 stevel * Stop all lwps within the process, holding themselves in the kernel while the 1356 0 stevel * active lwp undergoes watchpoint activity. This is more complicated than 1357 0 stevel * expected because stop() relies on calling holdwatch() in order to copyin data 1358 0 stevel * from the user's address space. A double barrier is used to prevent an 1359 0 stevel * infinite loop. 1360 0 stevel * 1361 0 stevel * o The first thread into holdwatch() is the 'master' thread and does 1362 0 stevel * the following: 1363 0 stevel * 1364 0 stevel * - Sets SHOLDWATCH on the current process 1365 0 stevel * - Sets TP_WATCHSTOP on the current thread 1366 0 stevel * - Waits for all threads to be either stopped or have 1367 0 stevel * TP_WATCHSTOP set. 1368 0 stevel * - Sets the SWATCHOK flag on the process 1369 0 stevel * - Unsets TP_WATCHSTOP 1370 0 stevel * - Waits for the other threads to completely stop 1371 0 stevel * - Unsets SWATCHOK 1372 0 stevel * 1373 0 stevel * o If SHOLDWATCH is already set when we enter this function, then another 1374 0 stevel * thread is already trying to stop this thread. This 'slave' thread 1375 0 stevel * does the following: 1376 0 stevel * 1377 0 stevel * - Sets TP_WATCHSTOP on the current thread 1378 0 stevel * - Waits for SWATCHOK flag to be set 1379 0 stevel * - Calls stop() 1380 0 stevel * 1381 0 stevel * o If SWATCHOK is set on the process, then this function immediately 1382 0 stevel * returns, as we must have been called via stop(). 1383 0 stevel * 1384 0 stevel * In addition, there are other flags that take precedence over SHOLDWATCH: 1385 0 stevel * 1386 0 stevel * o If SEXITLWPS is set, exit immediately. 1387 0 stevel * 1388 0 stevel * o If SHOLDFORK1 is set, wait for fork1() to complete. 1389 0 stevel * 1390 0 stevel * o If SHOLDFORK is set, then watchpoint activity takes precedence In this 1391 0 stevel * case, set SHOLDWATCH, signalling the forking thread to stop first. 1392 0 stevel * 1393 0 stevel * o If the process is being stopped via /proc (TP_PRSTOP is set), then we 1394 0 stevel * stop the current thread. 1395 0 stevel * 1396 0 stevel * Returns 0 if all threads have been quiesced. Returns non-zero if not all 1397 0 stevel * threads were stopped, or the list of watched pages has changed. 1398 0 stevel */ 1399 0 stevel int 1400 0 stevel holdwatch(void) 1401 0 stevel { 1402 0 stevel proc_t *p = curproc; 1403 0 stevel kthread_t *t = curthread; 1404 0 stevel int ret = 0; 1405 0 stevel 1406 0 stevel mutex_enter(&p->p_lock); 1407 0 stevel 1408 0 stevel p->p_lwprcnt--; 1409 0 stevel 1410 0 stevel /* 1411 0 stevel * Check for bail-out conditions as outlined above. 1412 0 stevel */ 1413 0 stevel if (holdcheck(0) != 0) { 1414 0 stevel mutex_exit(&p->p_lock); 1415 0 stevel return (-1); 1416 0 stevel } 1417 0 stevel 1418 0 stevel if (!(p->p_flag & SHOLDWATCH)) { 1419 0 stevel /* 1420 0 stevel * We are the master watchpoint thread. Set SHOLDWATCH and poke 1421 0 stevel * the other threads. 1422 0 stevel */ 1423 0 stevel p->p_flag |= SHOLDWATCH; 1424 0 stevel pokelwps(p); 1425 0 stevel 1426 0 stevel /* 1427 0 stevel * Wait for all threads to be stopped or have TP_WATCHSTOP set. 1428 0 stevel */ 1429 0 stevel while (pr_allstopped(p, 1) > 0) { 1430 0 stevel if (holdcheck(SHOLDWATCH) != 0) { 1431 0 stevel p->p_flag &= ~SHOLDWATCH; 1432 0 stevel mutex_exit(&p->p_lock); 1433 0 stevel return (-1); 1434 0 stevel } 1435 0 stevel 1436 0 stevel cv_wait(&p->p_holdlwps, &p->p_lock); 1437 0 stevel } 1438 0 stevel 1439 0 stevel /* 1440 0 stevel * All threads are now stopped or in the process of stopping. 1441 0 stevel * Set SWATCHOK and let them stop completely. 1442 0 stevel */ 1443 0 stevel p->p_flag |= SWATCHOK; 1444 0 stevel t->t_proc_flag &= ~TP_WATCHSTOP; 1445 0 stevel cv_broadcast(&p->p_holdlwps); 1446 0 stevel 1447 0 stevel while (pr_allstopped(p, 0) > 0) { 1448 0 stevel /* 1449 0 stevel * At first glance, it may appear that we don't need a 1450 0 stevel * call to holdcheck() here. But if the process gets a 1451 0 stevel * SIGKILL signal, one of our stopped threads may have 1452 0 stevel * been awakened and is waiting in exitlwps(), which 1453 0 stevel * takes precedence over watchpoints. 1454 0 stevel */ 1455 0 stevel if (holdcheck(SHOLDWATCH | SWATCHOK) != 0) { 1456 0 stevel p->p_flag &= ~(SHOLDWATCH | SWATCHOK); 1457 0 stevel mutex_exit(&p->p_lock); 1458 0 stevel return (-1); 1459 0 stevel } 1460 0 stevel 1461 0 stevel cv_wait(&p->p_holdlwps, &p->p_lock); 1462 0 stevel } 1463 0 stevel 1464 0 stevel /* 1465 0 stevel * All threads are now completely stopped. 1466 0 stevel */ 1467 0 stevel p->p_flag &= ~SWATCHOK; 1468 0 stevel p->p_flag &= ~SHOLDWATCH; 1469 0 stevel p->p_lwprcnt++; 1470 0 stevel 1471 0 stevel } else if (!(p->p_flag & SWATCHOK)) { 1472 0 stevel 1473 0 stevel /* 1474 0 stevel * SHOLDWATCH is set, so another thread is trying to do 1475 0 stevel * watchpoint activity. Indicate this thread is stopping, and 1476 0 stevel * wait for the OK from the master thread. 1477 0 stevel */ 1478 0 stevel t->t_proc_flag |= TP_WATCHSTOP; 1479 0 stevel cv_broadcast(&p->p_holdlwps); 1480 0 stevel 1481 0 stevel while (!(p->p_flag & SWATCHOK)) { 1482 0 stevel if (holdcheck(0) != 0) { 1483 0 stevel t->t_proc_flag &= ~TP_WATCHSTOP; 1484 0 stevel mutex_exit(&p->p_lock); 1485 0 stevel return (-1); 1486 0 stevel } 1487 0 stevel 1488 0 stevel cv_wait(&p->p_holdlwps, &p->p_lock); 1489 0 stevel } 1490 0 stevel 1491 0 stevel /* 1492 0 stevel * Once the master thread has given the OK, this thread can 1493 0 stevel * actually call stop(). 1494 0 stevel */ 1495 0 stevel t->t_proc_flag &= ~TP_WATCHSTOP; 1496 0 stevel p->p_lwprcnt++; 1497 0 stevel 1498 0 stevel stop(PR_SUSPENDED, SUSPEND_NORMAL); 1499 0 stevel 1500 0 stevel /* 1501 0 stevel * It's not OK to do watchpoint activity, notify caller to 1502 0 stevel * retry. 1503 0 stevel */ 1504 0 stevel ret = -1; 1505 0 stevel 1506 0 stevel } else { 1507 0 stevel 1508 0 stevel /* 1509 0 stevel * The only way we can hit the case where SHOLDWATCH is set and 1510 0 stevel * SWATCHOK is set is if we are triggering this from within a 1511 0 stevel * stop() call. Assert that this is the case. 1512 0 stevel */ 1513 0 stevel 1514 0 stevel ASSERT(t->t_proc_flag & TP_STOPPING); 1515 0 stevel p->p_lwprcnt++; 1516 0 stevel } 1517 0 stevel 1518 0 stevel mutex_exit(&p->p_lock); 1519 0 stevel 1520 0 stevel return (ret); 1521 0 stevel } 1522 0 stevel 1523 0 stevel /* 1524 0 stevel * force all interruptible lwps to trap into the kernel. 1525 0 stevel */ 1526 0 stevel void 1527 0 stevel pokelwps(proc_t *p) 1528 0 stevel { 1529 0 stevel kthread_t *t; 1530 0 stevel 1531 0 stevel ASSERT(MUTEX_HELD(&p->p_lock)); 1532 0 stevel 1533 0 stevel t = p->p_tlist; 1534 0 stevel do { 1535 0 stevel if (t == curthread) 1536 0 stevel continue; 1537 0 stevel thread_lock(t); 1538 0 stevel aston(t); /* make thread trap or do post_syscall */ 1539 3792 akolb if (ISWAKEABLE(t) || ISWAITING(t)) { 1540 3792 akolb setrun_locked(t); 1541 0 stevel } else if (t->t_state == TS_STOPPED) { 1542 0 stevel /* 1543 0 stevel * Ensure that proc_exit() is not blocked by lwps 1544 0 stevel * that were stopped via jobcontrol or /proc. 1545 0 stevel */ 1546 0 stevel if (p->p_flag & SEXITLWPS) { 1547 0 stevel p->p_stopsig = 0; 1548 0 stevel t->t_schedflag |= (TS_XSTART | TS_PSTART); 1549 0 stevel setrun_locked(t); 1550 0 stevel } 1551 0 stevel /* 1552 0 stevel * If we are holding lwps for a forkall(), 1553 0 stevel * force lwps that have been suspended via 1554 0 stevel * lwp_suspend() and are suspended inside 1555 0 stevel * of a system call to proceed to their 1556 0 stevel * holdlwp() points where they are clonable. 1557 0 stevel */ 1558 0 stevel if ((p->p_flag & SHOLDFORK) && SUSPENDED(t)) { 1559 0 stevel if ((t->t_schedflag & TS_CSTART) == 0) { 1560 0 stevel p->p_lwprcnt++; 1561 0 stevel t->t_schedflag |= TS_CSTART; 1562 0 stevel setrun_locked(t); 1563 0 stevel } 1564 0 stevel } 1565 0 stevel } else if (t->t_state == TS_ONPROC) { 1566 0 stevel if (t->t_cpu != CPU) 1567 0 stevel poke_cpu(t->t_cpu->cpu_id); 1568 0 stevel } 1569 0 stevel thread_unlock(t); 1570 0 stevel } while ((t = t->t_forw) != p->p_tlist); 1571 0 stevel } 1572 0 stevel 1573 0 stevel /* 1574 0 stevel * undo the effects of holdlwps() or holdwatch(). 1575 0 stevel */ 1576 0 stevel void 1577 0 stevel continuelwps(proc_t *p) 1578 0 stevel { 1579 0 stevel kthread_t *t; 1580 0 stevel 1581 0 stevel /* 1582 0 stevel * If this flag is set, then the original holdwatch() didn't actually 1583 0 stevel * stop the process. See comments for holdwatch(). 1584 0 stevel */ 1585 0 stevel if (p->p_flag & SWATCHOK) { 1586 0 stevel ASSERT(curthread->t_proc_flag & TP_STOPPING); 1587 0 stevel return; 1588 0 stevel } 1589 0 stevel 1590 0 stevel ASSERT(MUTEX_HELD(&p->p_lock)); 1591 0 stevel ASSERT((p->p_flag & (SHOLDFORK | SHOLDFORK1 | SHOLDWATCH)) == 0); 1592 0 stevel 1593 0 stevel t = p->p_tlist; 1594 0 stevel do { 1595 0 stevel thread_lock(t); /* SUSPENDED looks at t_schedflag */ 1596 0 stevel if (SUSPENDED(t) && !(t->t_proc_flag & TP_HOLDLWP)) { 1597 0 stevel p->p_lwprcnt++; 1598 0 stevel t->t_schedflag |= TS_CSTART; 1599 0 stevel setrun_locked(t); 1600 0 stevel } 1601 0 stevel thread_unlock(t); 1602 0 stevel } while ((t = t->t_forw) != p->p_tlist); 1603 0 stevel } 1604 0 stevel 1605 0 stevel /* 1606 0 stevel * Force all other LWPs in the current process other than the caller to exit, 1607 0 stevel * and then cv_wait() on p_holdlwps for them to exit. The exitlwps() function 1608 0 stevel * is typically used in these situations: 1609 0 stevel * 1610 0 stevel * (a) prior to an exec() system call 1611 0 stevel * (b) prior to dumping a core file 1612 0 stevel * (c) prior to a uadmin() shutdown 1613 0 stevel * 1614 0 stevel * If the 'coredump' flag is set, other LWPs are quiesced but not destroyed. 1615 0 stevel * Multiple threads in the process can call this function at one time by 1616 0 stevel * triggering execs or core dumps simultaneously, so the SEXITLWPS bit is used 1617 0 stevel * to declare one particular thread the winner who gets to kill the others. 1618 0 stevel * If a thread wins the exitlwps() dance, zero is returned; otherwise an 1619 0 stevel * appropriate errno value is returned to caller for its system call to return. 1620 0 stevel */ 1621 0 stevel int 1622 0 stevel exitlwps(int coredump) 1623 0 stevel { 1624 0 stevel proc_t *p = curproc; 1625 0 stevel int heldcnt; 1626 0 stevel 1627 0 stevel if (curthread->t_door) 1628 0 stevel door_slam(); 1629 0 stevel if (p->p_door_list) 1630 0 stevel door_revoke_all(); 1631 0 stevel if (curthread->t_schedctl != NULL) 1632 0 stevel schedctl_lwp_cleanup(curthread); 1633 0 stevel 1634 0 stevel /* 1635 0 stevel * Ensure that before starting to wait for other lwps to exit, 1636 0 stevel * cleanup all upimutexes held by curthread. Otherwise, some other 1637 0 stevel * lwp could be waiting (uninterruptibly) for a upimutex held by 1638 0 stevel * curthread, and the call to pokelwps() below would deadlock. 1639 0 stevel * Even if a blocked upimutex_lock is made interruptible, 1640 0 stevel * curthread's upimutexes need to be unlocked: do it here. 1641 0 stevel */ 1642 0 stevel if (curthread->t_upimutex != NULL) 1643 0 stevel upimutex_cleanup(); 1644 0 stevel 1645 0 stevel /* 1646 0 stevel * Grab p_lock in order to check and set SEXITLWPS to declare a winner. 1647 0 stevel * We must also block any further /proc access from this point forward. 1648 0 stevel */ 1649 0 stevel mutex_enter(&p->p_lock); 1650 0 stevel prbarrier(p); 1651 0 stevel 1652 0 stevel if (p->p_flag & SEXITLWPS) { 1653 0 stevel mutex_exit(&p->p_lock); 1654 0 stevel aston(curthread); /* force a trip through post_syscall */ 1655 0 stevel return (set_errno(EINTR)); 1656 0 stevel } 1657 0 stevel 1658 0 stevel p->p_flag |= SEXITLWPS; 1659 0 stevel if (coredump) /* tell other lwps to stop, not exit */ 1660 0 stevel p->p_flag |= SCOREDUMP; 1661 0 stevel 1662 0 stevel /* 1663 0 stevel * Give precedence to exitlwps() if a holdlwps() is 1664 0 stevel * in progress. The lwp doing the holdlwps() operation 1665 0 stevel * is aborted when it is awakened. 1666 0 stevel */ 1667 0 stevel while (p->p_flag & (SHOLDFORK | SHOLDFORK1 | SHOLDWATCH)) { 1668 0 stevel cv_broadcast(&p->p_holdlwps); 1669 0 stevel cv_wait(&p->p_holdlwps, &p->p_lock); 1670 390 raf prbarrier(p); 1671 0 stevel } 1672 0 stevel p->p_flag |= SHOLDFORK; 1673 0 stevel pokelwps(p); 1674 0 stevel 1675 0 stevel /* 1676 0 stevel * Wait for process to become quiescent. 1677 0 stevel */ 1678 0 stevel --p->p_lwprcnt; 1679 390 raf while (p->p_lwprcnt > 0) { 1680 0 stevel cv_wait(&p->p_holdlwps, &p->p_lock); 1681 390 raf prbarrier(p); 1682 390 raf } 1683 0 stevel p->p_lwprcnt++; 1684 0 stevel ASSERT(p->p_lwprcnt == 1); 1685 0 stevel 1686 0 stevel /* 1687 0 stevel * The SCOREDUMP flag puts the process into a quiescent 1688 0 stevel * state. The process's lwps remain attached to this 1689 0 stevel * process until exitlwps() is called again without the 1690 0 stevel * 'coredump' flag set, then the lwps are terminated 1691 0 stevel * and the process can exit. 1692 0 stevel */ 1693 0 stevel if (coredump) { 1694 0 stevel p->p_flag &= ~(SCOREDUMP | SHOLDFORK | SEXITLWPS); 1695 0 stevel goto out; 1696 0 stevel } 1697 0 stevel 1698 0 stevel /* 1699 0 stevel * Determine if there are any lwps left dangling in 1700 0 stevel * the stopped state. This happens when exitlwps() 1701 0 stevel * aborts a holdlwps() operation. 1702 0 stevel */ 1703 0 stevel p->p_flag &= ~SHOLDFORK; 1704 0 stevel if ((heldcnt = p->p_lwpcnt) > 1) { 1705 0 stevel kthread_t *t; 1706 0 stevel for (t = curthread->t_forw; --heldcnt > 0; t = t->t_forw) { 1707 0 stevel t->t_proc_flag &= ~TP_TWAIT; 1708 0 stevel lwp_continue(t); 1709 0 stevel } 1710 0 stevel } 1711 0 stevel 1712 0 stevel /* 1713 0 stevel * Wait for all other lwps to exit. 1714 0 stevel */ 1715 0 stevel --p->p_lwprcnt; 1716 390 raf while (p->p_lwpcnt > 1) { 1717 0 stevel cv_wait(&p->p_holdlwps, &p->p_lock); 1718 390 raf prbarrier(p); 1719 390 raf } 1720 0 stevel ++p->p_lwprcnt; 1721 0 stevel ASSERT(p->p_lwpcnt == 1 && p->p_lwprcnt == 1); 1722 0 stevel 1723 0 stevel p->p_flag &= ~SEXITLWPS; 1724 0 stevel curthread->t_proc_flag &= ~TP_TWAIT; 1725 0 stevel 1726 0 stevel out: 1727 0 stevel if (!coredump && p->p_zombcnt) { /* cleanup the zombie lwps */ 1728 0 stevel lwpdir_t *ldp; 1729 0 stevel lwpent_t *lep; 1730 0 stevel int i; 1731 0 stevel 1732 0 stevel for (ldp = p->p_lwpdir, i = 0; i < p->p_lwpdir_sz; i++, ldp++) { 1733 0 stevel lep = ldp->ld_entry; 1734 0 stevel if (lep != NULL && lep->le_thread != curthread) { 1735 0 stevel ASSERT(lep->le_thread == NULL); 1736 0 stevel p->p_zombcnt--; 1737 0 stevel lwp_hash_out(p, lep->le_lwpid); 1738 0 stevel } 1739 0 stevel } 1740 0 stevel ASSERT(p->p_zombcnt == 0); 1741 0 stevel } 1742 0 stevel 1743 0 stevel /* 1744 0 stevel * If some other LWP in the process wanted us to suspend ourself, 1745 0 stevel * then we will not do it. The other LWP is now terminated and 1746 0 stevel * no one will ever continue us again if we suspend ourself. 1747 0 stevel */ 1748 0 stevel curthread->t_proc_flag &= ~TP_HOLDLWP; 1749 0 stevel p->p_flag &= ~(SHOLDFORK | SHOLDFORK1 | SHOLDWATCH | SLWPWRAP); 1750 0 stevel mutex_exit(&p->p_lock); 1751 0 stevel return (0); 1752 0 stevel } 1753 0 stevel 1754 0 stevel /* 1755 0 stevel * duplicate a lwp. 1756 0 stevel */ 1757 0 stevel klwp_t * 1758 0 stevel forklwp(klwp_t *lwp, proc_t *cp, id_t lwpid) 1759 0 stevel { 1760 0 stevel klwp_t *clwp; 1761 0 stevel void *tregs, *tfpu; 1762 0 stevel kthread_t *t = lwptot(lwp); 1763 0 stevel kthread_t *ct; 1764 0 stevel proc_t *p = lwptoproc(lwp); 1765 0 stevel int cid; 1766 0 stevel void *bufp; 1767 2712 nn35248 void *brand_data; 1768 0 stevel int val; 1769 0 stevel 1770 0 stevel ASSERT(p == curproc); 1771 0 stevel ASSERT(t == curthread || (SUSPENDED(t) && lwp->lwp_asleep == 0)); 1772 0 stevel 1773 0 stevel #if defined(__sparc) 1774 0 stevel if (t == curthread) 1775 0 stevel (void) flush_user_windows_to_stack(NULL); 1776 0 stevel #endif 1777 0 stevel 1778 0 stevel if (t == curthread) 1779 0 stevel /* copy args out of registers first */ 1780 0 stevel (void) save_syscall_args(); 1781 2712 nn35248 1782 0 stevel clwp = lwp_create(cp->p_lwpcnt == 0 ? lwp_rtt_initial : lwp_rtt, 1783 0 stevel NULL, 0, cp, TS_STOPPED, t->t_pri, &t->t_hold, NOCLASS, lwpid); 1784 0 stevel if (clwp == NULL) 1785 0 stevel return (NULL); 1786 0 stevel 1787 0 stevel /* 1788 0 stevel * most of the parent's lwp can be copied to its duplicate, 1789 0 stevel * except for the fields that are unique to each lwp, like 1790 0 stevel * lwp_thread, lwp_procp, lwp_regs, and lwp_ap. 1791 0 stevel */ 1792 0 stevel ct = clwp->lwp_thread; 1793 0 stevel tregs = clwp->lwp_regs; 1794 0 stevel tfpu = clwp->lwp_fpu; 1795 2712 nn35248 brand_data = clwp->lwp_brand; 1796 0 stevel 1797 3551 johansen /* 1798 3551 johansen * Copy parent lwp to child lwp. Hold child's p_lock to prevent 1799 3551 johansen * mstate_aggr_state() from reading stale mstate entries copied 1800 3551 johansen * from lwp to clwp. 1801 3551 johansen */ 1802 3551 johansen mutex_enter(&cp->p_lock); 1803 0 stevel *clwp = *lwp; 1804 3551 johansen 1805 3551 johansen /* clear microstate and resource usage data in new lwp */ 1806 3551 johansen init_mstate(ct, LMS_STOPPED); 1807 3551 johansen bzero(&clwp->lwp_ru, sizeof (clwp->lwp_ru)); 1808 3551 johansen mutex_exit(&cp->p_lock); 1809 0 stevel 1810 0 stevel /* fix up child's lwp */ 1811 0 stevel 1812 4503 sudheer clwp->lwp_pcb.pcb_flags = 0; 1813 4503 sudheer #if defined(__sparc) 1814 0 stevel clwp->lwp_pcb.pcb_step = STEP_NONE; 1815 0 stevel #endif 1816 0 stevel clwp->lwp_cursig = 0; 1817 0 stevel clwp->lwp_extsig = 0; 1818 0 stevel clwp->lwp_curinfo = (struct sigqueue *)0; 1819 0 stevel clwp->lwp_thread = ct; 1820 0 stevel ct->t_sysnum = t->t_sysnum; 1821 0 stevel clwp->lwp_regs = tregs; 1822 0 stevel clwp->lwp_fpu = tfpu; 1823 2712 nn35248 clwp->lwp_brand = brand_data; 1824 0 stevel clwp->lwp_ap = clwp->lwp_arg; 1825 0 stevel clwp->lwp_procp = cp; 1826 0 stevel bzero(clwp->lwp_timer, sizeof (clwp->lwp_timer)); 1827 0 stevel clwp->lwp_lastfault = 0; 1828 0 stevel clwp->lwp_lastfaddr = 0; 1829 0 stevel 1830 0 stevel /* copy parent's struct regs to child. */ 1831 0 stevel lwp_forkregs(lwp, clwp); 1832 0 stevel 1833 0 stevel /* 1834 1217 rab * Fork thread context ops, if any. 1835 0 stevel */ 1836 0 stevel if (t->t_ctx) 1837 0 stevel forkctx(t, ct); 1838 0 stevel 1839 0 stevel /* fix door state in the child */ 1840 0 stevel if (t->t_door) 1841 0 stevel door_fork(t, ct); 1842 0 stevel 1843 0 stevel /* copy current contract templates, clear latest contracts */ 1844 0 stevel lwp_ctmpl_copy(clwp, lwp); 1845 0 stevel 1846 0 stevel mutex_enter(&cp->p_lock); 1847 0 stevel /* lwp_create() set the TP_HOLDLWP flag */ 1848 0 stevel if (!(t->t_proc_flag & TP_HOLDLWP)) 1849 0 stevel ct->t_proc_flag &= ~TP_HOLDLWP; 1850 0 stevel if (cp->p_flag & SMSACCT) 1851 0 stevel ct->t_proc_flag |= TP_MSACCT; 1852 0 stevel mutex_exit(&cp->p_lock); 1853 2712 nn35248 1854 2712 nn35248 /* Allow brand to propagate brand-specific state */ 1855 2712 nn35248 if (PROC_IS_BRANDED(p)) 1856 2712 nn35248 BROP(p)->b_forklwp(lwp, clwp); 1857 0 stevel 1858 0 stevel retry: 1859 0 stevel cid = t->t_cid; 1860 0 stevel 1861 0 stevel val = CL_ALLOC(&bufp, cid, KM_SLEEP); 1862 0 stevel ASSERT(val == 0); 1863 0 stevel 1864 0 stevel mutex_enter(&p->p_lock); 1865 0 stevel if (cid != t->t_cid) { 1866 0 stevel /* 1867 0 stevel * Someone just changed this thread's scheduling class, 1868 0 stevel * so try pre-allocating the buffer again. Hopefully we 1869 0 stevel * don't hit this often. 1870 0 stevel */ 1871 0 stevel mutex_exit(&p->p_lock); 1872 0 stevel CL_FREE(cid, bufp); 1873 0 stevel goto retry; 1874 0 stevel } 1875 0 stevel 1876 0 stevel ct->t_unpark = t->t_unpark; 1877 0 stevel ct->t_clfuncs = t->t_clfuncs; 1878 0 stevel CL_FORK(t, ct, bufp); 1879 0 stevel ct->t_cid = t->t_cid; /* after data allocated so prgetpsinfo works */ 1880 0 stevel mutex_exit(&p->p_lock); 1881 0 stevel 1882 0 stevel return (clwp); 1883 0 stevel } 1884 0 stevel 1885 0 stevel /* 1886 0 stevel * Add a new lwp entry to the lwp directory and to the lwpid hash table. 1887 0 stevel */ 1888 0 stevel void 1889 9393 Roger lwp_hash_in(proc_t *p, lwpent_t *lep, tidhash_t *tidhash, uint_t tidhash_sz, 1890 9393 Roger int do_lock) 1891 0 stevel { 1892 9393 Roger tidhash_t *thp = &tidhash[TIDHASH(lep->le_lwpid, tidhash_sz)]; 1893 0 stevel lwpdir_t **ldpp; 1894 0 stevel lwpdir_t *ldp; 1895 0 stevel kthread_t *t; 1896 0 stevel 1897 0 stevel /* 1898 0 stevel * Allocate a directory element from the free list. 1899 0 stevel * Code elsewhere guarantees a free slot. 1900 0 stevel */ 1901 0 stevel ldp = p->p_lwpfree; 1902 0 stevel p->p_lwpfree = ldp->ld_next; 1903 0 stevel ASSERT(ldp->ld_entry == NULL); 1904 0 stevel ldp->ld_entry = lep; 1905 0 stevel 1906 9393 Roger if (do_lock) 1907 9393 Roger mutex_enter(&thp->th_lock); 1908 9393 Roger 1909 0 stevel /* 1910 0 stevel * Insert it into the lwpid hash table. 1911 0 stevel */ 1912 9393 Roger ldpp = &thp->th_list; 1913 0 stevel ldp->ld_next = *ldpp; 1914 0 stevel *ldpp = ldp; 1915 0 stevel 1916 0 stevel /* 1917 0 stevel * Set the active thread's directory slot entry. 1918 0 stevel */ 1919 0 stevel if ((t = lep->le_thread) != NULL) { 1920 0 stevel ASSERT(lep->le_lwpid == t->t_tid); 1921 0 stevel t->t_dslot = (int)(ldp - p->p_lwpdir); 1922 0 stevel } 1923 9393 Roger 1924 9393 Roger if (do_lock) 1925 9393 Roger mutex_exit(&thp->th_lock); 1926 0 stevel } 1927 0 stevel 1928 0 stevel /* 1929 0 stevel * Remove an lwp from the lwpid hash table and free its directory entry. 1930 0 stevel * This is done when a detached lwp exits in lwp_exit() or 1931 0 stevel * when a non-detached lwp is waited for in lwp_wait() or 1932 0 stevel * when a zombie lwp is detached in lwp_detach(). 1933 0 stevel */ 1934 0 stevel void 1935 0 stevel lwp_hash_out(proc_t *p, id_t lwpid) 1936 0 stevel { 1937 9393 Roger tidhash_t *thp = &p->p_tidhash[TIDHASH(lwpid, p->p_tidhash_sz)]; 1938 0 stevel lwpdir_t **ldpp; 1939 0 stevel lwpdir_t *ldp; 1940 0 stevel lwpent_t *lep; 1941 0 stevel 1942 9393 Roger mutex_enter(&thp->th_lock); 1943 9393 Roger for (ldpp = &thp->th_list; 1944 0 stevel (ldp = *ldpp) != NULL; ldpp = &ldp->ld_next) { 1945 0 stevel lep = ldp->ld_entry; 1946 0 stevel if (lep->le_lwpid == lwpid) { 1947 0 stevel prlwpfree(p, lep); /* /proc deals with le_trace */ 1948 0 stevel *ldpp = ldp->ld_next; 1949 0 stevel ldp->ld_entry = NULL; 1950 0 stevel ldp->ld_next = p->p_lwpfree; 1951 0 stevel p->p_lwpfree = ldp; 1952 0 stevel kmem_free(lep, sizeof (*lep)); 1953 0 stevel break; 1954 0 stevel } 1955 0 stevel } 1956 9393 Roger mutex_exit(&thp->th_lock); 1957 0 stevel } 1958 0 stevel 1959 0 stevel /* 1960 0 stevel * Lookup an lwp in the lwpid hash table by lwpid. 1961 0 stevel */ 1962 0 stevel lwpdir_t * 1963 0 stevel lwp_hash_lookup(proc_t *p, id_t lwpid) 1964 0 stevel { 1965 9393 Roger tidhash_t *thp; 1966 0 stevel lwpdir_t *ldp; 1967 0 stevel 1968 0 stevel /* 1969 0 stevel * The process may be exiting, after p_tidhash has been set to NULL in 1970 0 stevel * proc_exit() but before prfee() has been called. Return failure in 1971 0 stevel * this case. 1972 0 stevel */ 1973 0 stevel if (p->p_tidhash == NULL) 1974 0 stevel return (NULL); 1975 0 stevel 1976 9393 Roger thp = &p->p_tidhash[TIDHASH(lwpid, p->p_tidhash_sz)]; 1977 9393 Roger for (ldp = thp->th_list; ldp != NULL; ldp = ldp->ld_next) { 1978 0 stevel if (ldp->ld_entry->le_lwpid == lwpid) 1979 0 stevel return (ldp); 1980 0 stevel } 1981 0 stevel 1982 9393 Roger return (NULL); 1983 9393 Roger } 1984 9393 Roger 1985 9393 Roger /* 1986 9393 Roger * Same as lwp_hash_lookup(), but acquire and return 1987 9393 Roger * the tid hash table entry lock on success. 1988 9393 Roger */ 1989 9393 Roger lwpdir_t * 1990 9393 Roger lwp_hash_lookup_and_lock(proc_t *p, id_t lwpid, kmutex_t **mpp) 1991 9393 Roger { 1992 9393 Roger tidhash_t *tidhash; 1993 9393 Roger uint_t tidhash_sz; 1994 9393 Roger tidhash_t *thp; 1995 9393 Roger lwpdir_t *ldp; 1996 9393 Roger 1997 9393 Roger top: 1998 9393 Roger tidhash_sz = p->p_tidhash_sz; 1999 9393 Roger membar_consumer(); 2000 9393 Roger if ((tidhash = p->p_tidhash) == NULL) 2001 9393 Roger return (NULL); 2002 9393 Roger 2003 9393 Roger thp = &tidhash[TIDHASH(lwpid, tidhash_sz)]; 2004 9393 Roger mutex_enter(&thp->th_lock); 2005 9393 Roger 2006 9393 Roger /* 2007 9393 Roger * Since we are not holding p->p_lock, the tid hash table 2008 9393 Roger * may have changed. If so, start over. If not, then 2009 9393 Roger * it cannot change until after we drop &thp->th_lock; 2010 9393 Roger */ 2011 9393 Roger if (tidhash != p->p_tidhash || tidhash_sz != p->p_tidhash_sz) { 2012 9393 Roger mutex_exit(&thp->th_lock); 2013 9393 Roger goto top; 2014 9393 Roger } 2015 9393 Roger 2016 9393 Roger for (ldp = thp->th_list; ldp != NULL; ldp = ldp->ld_next) { 2017 9393 Roger if (ldp->ld_entry->le_lwpid == lwpid) { 2018 9393 Roger *mpp = &thp->th_lock; 2019 9393 Roger return (ldp); 2020 9393 Roger } 2021 9393 Roger } 2022 9393 Roger 2023 9393 Roger mutex_exit(&thp->th_lock); 2024 0 stevel return (NULL); 2025 0 stevel } 2026 0 stevel 2027 0 stevel /* 2028 0 stevel * Update the indicated LWP usage statistic for the current LWP. 2029 0 stevel */ 2030 0 stevel void 2031 0 stevel lwp_stat_update(lwp_stat_id_t lwp_stat_id, long inc) 2032 0 stevel { 2033 0 stevel klwp_t *lwp = ttolwp(curthread); 2034 0 stevel 2035 0 stevel if (lwp == NULL) 2036 0 stevel return; 2037 0 stevel 2038 0 stevel switch (lwp_stat_id) { 2039 0 stevel case LWP_STAT_INBLK: 2040 0 stevel lwp->lwp_ru.inblock += inc; 2041 0 stevel break; 2042 0 stevel case LWP_STAT_OUBLK: 2043 0 stevel lwp->lwp_ru.oublock += inc; 2044 0 stevel break; 2045 0 stevel case LWP_STAT_MSGRCV: 2046 0 stevel lwp->lwp_ru.msgrcv += inc; 2047 0 stevel break; 2048 0 stevel case LWP_STAT_MSGSND: 2049 0 stevel lwp->lwp_ru.msgsnd += inc; 2050 0 stevel break; 2051 0 stevel default: 2052 0 stevel panic("lwp_stat_update: invalid lwp_stat_id 0x%x", lwp_stat_id); 2053 0 stevel } 2054 0 stevel } 2055