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