10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
52712Snn35248 * Common Development and Distribution License (the "License").
62712Snn35248 * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate * See the License for the specific language governing permissions
110Sstevel@tonic-gate * and limitations under the License.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate *
190Sstevel@tonic-gate * CDDL HEADER END
200Sstevel@tonic-gate */
21390Sraf
220Sstevel@tonic-gate /*
2311595SJakub.Jermar@Sun.COM * Copyright 2010 Sun Microsystems, Inc. All rights reserved.
240Sstevel@tonic-gate * Use is subject to license terms.
250Sstevel@tonic-gate */
260Sstevel@tonic-gate
270Sstevel@tonic-gate #include <sys/param.h>
280Sstevel@tonic-gate #include <sys/types.h>
290Sstevel@tonic-gate #include <sys/sysmacros.h>
300Sstevel@tonic-gate #include <sys/systm.h>
310Sstevel@tonic-gate #include <sys/thread.h>
320Sstevel@tonic-gate #include <sys/proc.h>
330Sstevel@tonic-gate #include <sys/task.h>
340Sstevel@tonic-gate #include <sys/project.h>
350Sstevel@tonic-gate #include <sys/signal.h>
360Sstevel@tonic-gate #include <sys/errno.h>
370Sstevel@tonic-gate #include <sys/vmparam.h>
380Sstevel@tonic-gate #include <sys/stack.h>
390Sstevel@tonic-gate #include <sys/procfs.h>
400Sstevel@tonic-gate #include <sys/prsystm.h>
410Sstevel@tonic-gate #include <sys/cpuvar.h>
420Sstevel@tonic-gate #include <sys/kmem.h>
430Sstevel@tonic-gate #include <sys/vtrace.h>
440Sstevel@tonic-gate #include <sys/door.h>
450Sstevel@tonic-gate #include <vm/seg_kp.h>
460Sstevel@tonic-gate #include <sys/debug.h>
470Sstevel@tonic-gate #include <sys/tnf.h>
480Sstevel@tonic-gate #include <sys/schedctl.h>
490Sstevel@tonic-gate #include <sys/poll.h>
500Sstevel@tonic-gate #include <sys/copyops.h>
510Sstevel@tonic-gate #include <sys/lwp_upimutex_impl.h>
520Sstevel@tonic-gate #include <sys/cpupart.h>
530Sstevel@tonic-gate #include <sys/lgrp.h>
540Sstevel@tonic-gate #include <sys/rctl.h>
550Sstevel@tonic-gate #include <sys/contract_impl.h>
560Sstevel@tonic-gate #include <sys/cpc_impl.h>
570Sstevel@tonic-gate #include <sys/sdt.h>
580Sstevel@tonic-gate #include <sys/cmn_err.h>
592712Snn35248 #include <sys/brand.h>
609870SRoger.Faulkner@Sun.COM #include <sys/cyclic.h>
6110541SGangadhar.M@Sun.COM #include <sys/pool.h>
620Sstevel@tonic-gate
639393SRoger.Faulkner@Sun.COM /* hash function for the lwpid hash table, p->p_tidhash[] */
649393SRoger.Faulkner@Sun.COM #define TIDHASH(tid, hash_sz) ((tid) & ((hash_sz) - 1))
659393SRoger.Faulkner@Sun.COM
660Sstevel@tonic-gate void *segkp_lwp; /* cookie for pool of segkp resources */
675834Spt157919 extern void reapq_move_lq_to_tq(kthread_t *);
685834Spt157919 extern void freectx_ctx(struct ctxop *);
690Sstevel@tonic-gate
700Sstevel@tonic-gate /*
7111173SJonathan.Adams@Sun.COM * Create a kernel thread associated with a particular system process. Give
7211173SJonathan.Adams@Sun.COM * it an LWP so that microstate accounting will be available for it.
7311173SJonathan.Adams@Sun.COM */
7411173SJonathan.Adams@Sun.COM kthread_t *
lwp_kernel_create(proc_t * p,void (* proc)(),void * arg,int state,pri_t pri)7511173SJonathan.Adams@Sun.COM lwp_kernel_create(proc_t *p, void (*proc)(), void *arg, int state, pri_t pri)
7611173SJonathan.Adams@Sun.COM {
7711173SJonathan.Adams@Sun.COM klwp_t *lwp;
7811173SJonathan.Adams@Sun.COM
7911173SJonathan.Adams@Sun.COM VERIFY((p->p_flag & SSYS) != 0);
8011173SJonathan.Adams@Sun.COM
8111173SJonathan.Adams@Sun.COM lwp = lwp_create(proc, arg, 0, p, state, pri, &t0.t_hold, syscid, 0);
8211173SJonathan.Adams@Sun.COM
8311173SJonathan.Adams@Sun.COM VERIFY(lwp != NULL);
8411173SJonathan.Adams@Sun.COM
8511173SJonathan.Adams@Sun.COM return (lwptot(lwp));
8611173SJonathan.Adams@Sun.COM }
8711173SJonathan.Adams@Sun.COM
8811173SJonathan.Adams@Sun.COM /*
890Sstevel@tonic-gate * Create a thread that appears to be stopped at sys_rtt.
900Sstevel@tonic-gate */
910Sstevel@tonic-gate klwp_t *
lwp_create(void (* proc)(),caddr_t arg,size_t len,proc_t * p,int state,int pri,const k_sigset_t * smask,int cid,id_t lwpid)920Sstevel@tonic-gate lwp_create(void (*proc)(), caddr_t arg, size_t len, proc_t *p,
930Sstevel@tonic-gate int state, int pri, const k_sigset_t *smask, int cid, id_t lwpid)
940Sstevel@tonic-gate {
950Sstevel@tonic-gate klwp_t *lwp = NULL;
960Sstevel@tonic-gate kthread_t *t;
970Sstevel@tonic-gate kthread_t *tx;
980Sstevel@tonic-gate cpupart_t *oldpart = NULL;
990Sstevel@tonic-gate size_t stksize;
1000Sstevel@tonic-gate caddr_t lwpdata = NULL;
1010Sstevel@tonic-gate processorid_t binding;
1020Sstevel@tonic-gate int err = 0;
1030Sstevel@tonic-gate kproject_t *oldkpj, *newkpj;
1040Sstevel@tonic-gate void *bufp = NULL;
10511173SJonathan.Adams@Sun.COM klwp_t *curlwp;
1060Sstevel@tonic-gate lwpent_t *lep;
1070Sstevel@tonic-gate lwpdir_t *old_dir = NULL;
1080Sstevel@tonic-gate uint_t old_dirsz = 0;
1099393SRoger.Faulkner@Sun.COM tidhash_t *old_hash = NULL;
1100Sstevel@tonic-gate uint_t old_hashsz = 0;
1119393SRoger.Faulkner@Sun.COM ret_tidhash_t *ret_tidhash = NULL;
1120Sstevel@tonic-gate int i;
1130Sstevel@tonic-gate int rctlfail = 0;
1142712Snn35248 boolean_t branded = 0;
1155834Spt157919 struct ctxop *ctx = NULL;
1160Sstevel@tonic-gate
11711173SJonathan.Adams@Sun.COM ASSERT(cid != sysdccid); /* system threads must start in SYS */
11811173SJonathan.Adams@Sun.COM
11911173SJonathan.Adams@Sun.COM ASSERT(p != &p0); /* No new LWPs in p0. */
12011173SJonathan.Adams@Sun.COM
1210Sstevel@tonic-gate mutex_enter(&p->p_lock);
1220Sstevel@tonic-gate mutex_enter(&p->p_zone->zone_nlwps_lock);
1230Sstevel@tonic-gate /*
1240Sstevel@tonic-gate * don't enforce rctl limits on system processes
1250Sstevel@tonic-gate */
12611173SJonathan.Adams@Sun.COM if (!CLASS_KERNEL(cid)) {
1270Sstevel@tonic-gate if (p->p_task->tk_nlwps >= p->p_task->tk_nlwps_ctl)
1280Sstevel@tonic-gate if (rctl_test(rc_task_lwps, p->p_task->tk_rctls, p,
1290Sstevel@tonic-gate 1, 0) & RCT_DENY)
1300Sstevel@tonic-gate rctlfail = 1;
1310Sstevel@tonic-gate if (p->p_task->tk_proj->kpj_nlwps >=
1320Sstevel@tonic-gate p->p_task->tk_proj->kpj_nlwps_ctl)
1330Sstevel@tonic-gate if (rctl_test(rc_project_nlwps,
1340Sstevel@tonic-gate p->p_task->tk_proj->kpj_rctls, p, 1, 0)
1350Sstevel@tonic-gate & RCT_DENY)
1360Sstevel@tonic-gate rctlfail = 1;
1370Sstevel@tonic-gate if (p->p_zone->zone_nlwps >= p->p_zone->zone_nlwps_ctl)
1380Sstevel@tonic-gate if (rctl_test(rc_zone_nlwps, p->p_zone->zone_rctls, p,
1390Sstevel@tonic-gate 1, 0) & RCT_DENY)
1400Sstevel@tonic-gate rctlfail = 1;
1410Sstevel@tonic-gate }
1420Sstevel@tonic-gate if (rctlfail) {
1430Sstevel@tonic-gate mutex_exit(&p->p_zone->zone_nlwps_lock);
1440Sstevel@tonic-gate mutex_exit(&p->p_lock);
1450Sstevel@tonic-gate return (NULL);
1460Sstevel@tonic-gate }
1470Sstevel@tonic-gate p->p_task->tk_nlwps++;
1480Sstevel@tonic-gate p->p_task->tk_proj->kpj_nlwps++;
1490Sstevel@tonic-gate p->p_zone->zone_nlwps++;
1500Sstevel@tonic-gate mutex_exit(&p->p_zone->zone_nlwps_lock);
1510Sstevel@tonic-gate mutex_exit(&p->p_lock);
1520Sstevel@tonic-gate
15311292SJonathan.Adams@Sun.COM curlwp = ttolwp(curthread);
15411292SJonathan.Adams@Sun.COM if (curlwp == NULL || (stksize = curlwp->lwp_childstksz) == 0)
1550Sstevel@tonic-gate stksize = lwp_default_stksize;
1560Sstevel@tonic-gate
15711173SJonathan.Adams@Sun.COM if (CLASS_KERNEL(cid)) {
15811292SJonathan.Adams@Sun.COM /*
15911292SJonathan.Adams@Sun.COM * Since we are creating an LWP in an SSYS process, we do not
16011292SJonathan.Adams@Sun.COM * inherit anything from the current thread's LWP. We set
16111292SJonathan.Adams@Sun.COM * stksize and lwpdata to 0 in order to let thread_create()
16211292SJonathan.Adams@Sun.COM * allocate a regular kernel thread stack for this thread.
16311292SJonathan.Adams@Sun.COM */
16411292SJonathan.Adams@Sun.COM curlwp = NULL;
16511292SJonathan.Adams@Sun.COM stksize = 0;
16611292SJonathan.Adams@Sun.COM lwpdata = NULL;
16711173SJonathan.Adams@Sun.COM
16811173SJonathan.Adams@Sun.COM } else if (stksize == lwp_default_stksize) {
16911292SJonathan.Adams@Sun.COM /*
17011292SJonathan.Adams@Sun.COM * Try to reuse an <lwp,stack> from the LWP deathrow.
17111292SJonathan.Adams@Sun.COM */
1720Sstevel@tonic-gate if (lwp_reapcnt > 0) {
1730Sstevel@tonic-gate mutex_enter(&reaplock);
1740Sstevel@tonic-gate if ((t = lwp_deathrow) != NULL) {
1750Sstevel@tonic-gate ASSERT(t->t_swap);
1760Sstevel@tonic-gate lwp_deathrow = t->t_forw;
1770Sstevel@tonic-gate lwp_reapcnt--;
1780Sstevel@tonic-gate lwpdata = t->t_swap;
1790Sstevel@tonic-gate lwp = t->t_lwp;
1805834Spt157919 ctx = t->t_ctx;
1815834Spt157919 t->t_swap = NULL;
1825834Spt157919 t->t_lwp = NULL;
1835834Spt157919 t->t_ctx = NULL;
1845834Spt157919 reapq_move_lq_to_tq(t);
1850Sstevel@tonic-gate }
1860Sstevel@tonic-gate mutex_exit(&reaplock);
1875834Spt157919 if (lwp != NULL) {
1885834Spt157919 lwp_stk_fini(lwp);
1895834Spt157919 }
1905834Spt157919 if (ctx != NULL) {
1915834Spt157919 freectx_ctx(ctx);
1920Sstevel@tonic-gate }
1930Sstevel@tonic-gate }
1940Sstevel@tonic-gate if (lwpdata == NULL &&
1950Sstevel@tonic-gate (lwpdata = (caddr_t)segkp_cache_get(segkp_lwp)) == NULL) {
1960Sstevel@tonic-gate mutex_enter(&p->p_lock);
1970Sstevel@tonic-gate mutex_enter(&p->p_zone->zone_nlwps_lock);
1980Sstevel@tonic-gate p->p_task->tk_nlwps--;
1990Sstevel@tonic-gate p->p_task->tk_proj->kpj_nlwps--;
2000Sstevel@tonic-gate p->p_zone->zone_nlwps--;
2010Sstevel@tonic-gate mutex_exit(&p->p_zone->zone_nlwps_lock);
2020Sstevel@tonic-gate mutex_exit(&p->p_lock);
2030Sstevel@tonic-gate return (NULL);
2040Sstevel@tonic-gate }
2050Sstevel@tonic-gate } else {
2060Sstevel@tonic-gate stksize = roundup(stksize, PAGESIZE);
2070Sstevel@tonic-gate if ((lwpdata = (caddr_t)segkp_get(segkp, stksize,
2080Sstevel@tonic-gate (KPD_NOWAIT | KPD_HASREDZONE | KPD_LOCKED))) == NULL) {
2090Sstevel@tonic-gate mutex_enter(&p->p_lock);
2100Sstevel@tonic-gate mutex_enter(&p->p_zone->zone_nlwps_lock);
2110Sstevel@tonic-gate p->p_task->tk_nlwps--;
2120Sstevel@tonic-gate p->p_task->tk_proj->kpj_nlwps--;
2130Sstevel@tonic-gate p->p_zone->zone_nlwps--;
2140Sstevel@tonic-gate mutex_exit(&p->p_zone->zone_nlwps_lock);
2150Sstevel@tonic-gate mutex_exit(&p->p_lock);
2160Sstevel@tonic-gate return (NULL);
2170Sstevel@tonic-gate }
2180Sstevel@tonic-gate }
2190Sstevel@tonic-gate
2200Sstevel@tonic-gate /*
2210Sstevel@tonic-gate * Create a thread, initializing the stack pointer
2220Sstevel@tonic-gate */
2230Sstevel@tonic-gate t = thread_create(lwpdata, stksize, NULL, NULL, 0, p, TS_STOPPED, pri);
2240Sstevel@tonic-gate
22511292SJonathan.Adams@Sun.COM /*
22611292SJonathan.Adams@Sun.COM * If a non-NULL stack base is passed in, thread_create() assumes
22711292SJonathan.Adams@Sun.COM * that the stack might be statically allocated (as opposed to being
22811292SJonathan.Adams@Sun.COM * allocated from segkp), and so it does not set t_swap. Since
22911292SJonathan.Adams@Sun.COM * the lwpdata was allocated from segkp, we must set t_swap to point
23011292SJonathan.Adams@Sun.COM * to it ourselves.
23111292SJonathan.Adams@Sun.COM *
23211292SJonathan.Adams@Sun.COM * This would be less confusing if t_swap had a better name; it really
23311292SJonathan.Adams@Sun.COM * indicates that the stack is allocated from segkp, regardless of
23411292SJonathan.Adams@Sun.COM * whether or not it is swappable.
23511292SJonathan.Adams@Sun.COM */
23611292SJonathan.Adams@Sun.COM if (lwpdata != NULL) {
23711292SJonathan.Adams@Sun.COM ASSERT(!CLASS_KERNEL(cid));
23811292SJonathan.Adams@Sun.COM ASSERT(t->t_swap == NULL);
23911292SJonathan.Adams@Sun.COM t->t_swap = lwpdata; /* Start of page-able data */
24011292SJonathan.Adams@Sun.COM }
24111292SJonathan.Adams@Sun.COM
24211292SJonathan.Adams@Sun.COM /*
24311292SJonathan.Adams@Sun.COM * If the stack and lwp can be reused, mark the thread as such.
24411292SJonathan.Adams@Sun.COM * When we get to reapq_add() from resume_from_zombie(), these
24511292SJonathan.Adams@Sun.COM * threads will go onto lwp_deathrow instead of thread_deathrow.
24611292SJonathan.Adams@Sun.COM */
24711292SJonathan.Adams@Sun.COM if (!CLASS_KERNEL(cid) && stksize == lwp_default_stksize)
24811292SJonathan.Adams@Sun.COM t->t_flag |= T_LWPREUSE;
24911292SJonathan.Adams@Sun.COM
2500Sstevel@tonic-gate if (lwp == NULL)
2510Sstevel@tonic-gate lwp = kmem_cache_alloc(lwp_cache, KM_SLEEP);
2520Sstevel@tonic-gate bzero(lwp, sizeof (*lwp));
2530Sstevel@tonic-gate t->t_lwp = lwp;
2540Sstevel@tonic-gate
2550Sstevel@tonic-gate t->t_hold = *smask;
2560Sstevel@tonic-gate lwp->lwp_thread = t;
2570Sstevel@tonic-gate lwp->lwp_procp = p;
2580Sstevel@tonic-gate lwp->lwp_sigaltstack.ss_flags = SS_DISABLE;
2590Sstevel@tonic-gate if (curlwp != NULL && curlwp->lwp_childstksz != 0)
2600Sstevel@tonic-gate lwp->lwp_childstksz = curlwp->lwp_childstksz;
2610Sstevel@tonic-gate
2620Sstevel@tonic-gate t->t_stk = lwp_stk_init(lwp, t->t_stk);
2630Sstevel@tonic-gate thread_load(t, proc, arg, len);
2640Sstevel@tonic-gate
2650Sstevel@tonic-gate /*
2660Sstevel@tonic-gate * Allocate the SIGPROF buffer if ITIMER_REALPROF is in effect.
2670Sstevel@tonic-gate */
2689870SRoger.Faulkner@Sun.COM if (p->p_rprof_cyclic != CYCLIC_NONE)
2690Sstevel@tonic-gate t->t_rprof = kmem_zalloc(sizeof (struct rprof), KM_SLEEP);
2700Sstevel@tonic-gate
2710Sstevel@tonic-gate if (cid != NOCLASS)
2720Sstevel@tonic-gate (void) CL_ALLOC(&bufp, cid, KM_SLEEP);
2730Sstevel@tonic-gate
2740Sstevel@tonic-gate /*
2750Sstevel@tonic-gate * Allocate an lwp directory entry for the new lwp.
2760Sstevel@tonic-gate */
2770Sstevel@tonic-gate lep = kmem_zalloc(sizeof (*lep), KM_SLEEP);
2780Sstevel@tonic-gate
2790Sstevel@tonic-gate mutex_enter(&p->p_lock);
2800Sstevel@tonic-gate grow:
2810Sstevel@tonic-gate /*
2820Sstevel@tonic-gate * Grow the lwp (thread) directory and lwpid hash table if necessary.
2830Sstevel@tonic-gate * A note on the growth algorithm:
2840Sstevel@tonic-gate * The new lwp directory size is computed as:
2850Sstevel@tonic-gate * new = 2 * old + 2
2860Sstevel@tonic-gate * Starting with an initial size of 2 (see exec_common()),
2870Sstevel@tonic-gate * this yields numbers that are a power of two minus 2:
2880Sstevel@tonic-gate * 2, 6, 14, 30, 62, 126, 254, 510, 1022, ...
2890Sstevel@tonic-gate * The size of the lwpid hash table must be a power of two
2900Sstevel@tonic-gate * and must be commensurate in size with the lwp directory
2910Sstevel@tonic-gate * so that hash bucket chains remain short. Therefore,
2920Sstevel@tonic-gate * the lwpid hash table size is computed as:
2930Sstevel@tonic-gate * hashsz = (dirsz + 2) / 2
2940Sstevel@tonic-gate * which leads to these hash table sizes corresponding to
2950Sstevel@tonic-gate * the above directory sizes:
2960Sstevel@tonic-gate * 2, 4, 8, 16, 32, 64, 128, 256, 512, ...
2979393SRoger.Faulkner@Sun.COM * A note on growing the hash table:
2989393SRoger.Faulkner@Sun.COM * For performance reasons, code in lwp_unpark() does not
2999393SRoger.Faulkner@Sun.COM * acquire curproc->p_lock when searching the hash table.
3009393SRoger.Faulkner@Sun.COM * Rather, it calls lwp_hash_lookup_and_lock() which
3019393SRoger.Faulkner@Sun.COM * acquires only the individual hash bucket lock, taking
3029393SRoger.Faulkner@Sun.COM * care to deal with reallocation of the hash table
3039393SRoger.Faulkner@Sun.COM * during the time it takes to acquire the lock.
3049393SRoger.Faulkner@Sun.COM *
3059393SRoger.Faulkner@Sun.COM * This is sufficient to protect the integrity of the
3069393SRoger.Faulkner@Sun.COM * hash table, but it requires us to acquire all of the
3079393SRoger.Faulkner@Sun.COM * old hash bucket locks before growing the hash table
3089393SRoger.Faulkner@Sun.COM * and to release them afterwards. It also requires us
3099393SRoger.Faulkner@Sun.COM * not to free the old hash table because some thread
3109393SRoger.Faulkner@Sun.COM * in lwp_hash_lookup_and_lock() might still be trying
3119393SRoger.Faulkner@Sun.COM * to acquire the old bucket lock.
3129393SRoger.Faulkner@Sun.COM *
3139393SRoger.Faulkner@Sun.COM * So we adopt the tactic of keeping all of the retired
3149393SRoger.Faulkner@Sun.COM * hash tables on a linked list, so they can be safely
3159393SRoger.Faulkner@Sun.COM * freed when the process exits or execs.
3169393SRoger.Faulkner@Sun.COM *
3179393SRoger.Faulkner@Sun.COM * Because the hash table grows in powers of two, the
3189393SRoger.Faulkner@Sun.COM * total size of all of the hash tables will be slightly
3199393SRoger.Faulkner@Sun.COM * less than twice the size of the largest hash table.
3200Sstevel@tonic-gate */
3210Sstevel@tonic-gate while (p->p_lwpfree == NULL) {
3220Sstevel@tonic-gate uint_t dirsz = p->p_lwpdir_sz;
3239393SRoger.Faulkner@Sun.COM lwpdir_t *new_dir;
3240Sstevel@tonic-gate uint_t new_dirsz;
3259393SRoger.Faulkner@Sun.COM lwpdir_t *ldp;
3269393SRoger.Faulkner@Sun.COM tidhash_t *new_hash;
3270Sstevel@tonic-gate uint_t new_hashsz;
3280Sstevel@tonic-gate
3290Sstevel@tonic-gate mutex_exit(&p->p_lock);
3300Sstevel@tonic-gate
3319393SRoger.Faulkner@Sun.COM /*
3329393SRoger.Faulkner@Sun.COM * Prepare to remember the old p_tidhash for later
3339393SRoger.Faulkner@Sun.COM * kmem_free()ing when the process exits or execs.
3349393SRoger.Faulkner@Sun.COM */
3359393SRoger.Faulkner@Sun.COM if (ret_tidhash == NULL)
3369393SRoger.Faulkner@Sun.COM ret_tidhash = kmem_zalloc(sizeof (ret_tidhash_t),
3379393SRoger.Faulkner@Sun.COM KM_SLEEP);
3389393SRoger.Faulkner@Sun.COM if (old_dir != NULL)
3390Sstevel@tonic-gate kmem_free(old_dir, old_dirsz * sizeof (*old_dir));
3409393SRoger.Faulkner@Sun.COM if (old_hash != NULL)
3410Sstevel@tonic-gate kmem_free(old_hash, old_hashsz * sizeof (*old_hash));
3429393SRoger.Faulkner@Sun.COM
3430Sstevel@tonic-gate new_dirsz = 2 * dirsz + 2;
3440Sstevel@tonic-gate new_dir = kmem_zalloc(new_dirsz * sizeof (lwpdir_t), KM_SLEEP);
3450Sstevel@tonic-gate for (ldp = new_dir, i = 1; i < new_dirsz; i++, ldp++)
3460Sstevel@tonic-gate ldp->ld_next = ldp + 1;
3470Sstevel@tonic-gate new_hashsz = (new_dirsz + 2) / 2;
3489393SRoger.Faulkner@Sun.COM new_hash = kmem_zalloc(new_hashsz * sizeof (tidhash_t),
3495834Spt157919 KM_SLEEP);
3500Sstevel@tonic-gate
3510Sstevel@tonic-gate mutex_enter(&p->p_lock);
3520Sstevel@tonic-gate if (p == curproc)
3530Sstevel@tonic-gate prbarrier(p);
3540Sstevel@tonic-gate
3550Sstevel@tonic-gate if (dirsz != p->p_lwpdir_sz || p->p_lwpfree != NULL) {
3560Sstevel@tonic-gate /*
3570Sstevel@tonic-gate * Someone else beat us to it or some lwp exited.
3580Sstevel@tonic-gate * Set up to free our memory and take a lap.
3590Sstevel@tonic-gate */
3600Sstevel@tonic-gate old_dir = new_dir;
3610Sstevel@tonic-gate old_dirsz = new_dirsz;
3620Sstevel@tonic-gate old_hash = new_hash;
3630Sstevel@tonic-gate old_hashsz = new_hashsz;
3640Sstevel@tonic-gate } else {
3659393SRoger.Faulkner@Sun.COM /*
3669393SRoger.Faulkner@Sun.COM * For the benefit of lwp_hash_lookup_and_lock(),
3679393SRoger.Faulkner@Sun.COM * called from lwp_unpark(), which searches the
3689393SRoger.Faulkner@Sun.COM * tid hash table without acquiring p->p_lock,
3699393SRoger.Faulkner@Sun.COM * we must acquire all of the tid hash table
3709393SRoger.Faulkner@Sun.COM * locks before replacing p->p_tidhash.
3719393SRoger.Faulkner@Sun.COM */
3720Sstevel@tonic-gate old_hash = p->p_tidhash;
3730Sstevel@tonic-gate old_hashsz = p->p_tidhash_sz;
3749393SRoger.Faulkner@Sun.COM for (i = 0; i < old_hashsz; i++) {
3759393SRoger.Faulkner@Sun.COM mutex_enter(&old_hash[i].th_lock);
3769393SRoger.Faulkner@Sun.COM mutex_enter(&new_hash[i].th_lock);
3779393SRoger.Faulkner@Sun.COM }
3789393SRoger.Faulkner@Sun.COM
3790Sstevel@tonic-gate /*
3800Sstevel@tonic-gate * We simply hash in all of the old directory entries.
3810Sstevel@tonic-gate * This works because the old directory has no empty
3820Sstevel@tonic-gate * slots and the new hash table starts out empty.
3830Sstevel@tonic-gate * This reproduces the original directory ordering
3840Sstevel@tonic-gate * (required for /proc directory semantics).
3850Sstevel@tonic-gate */
3869393SRoger.Faulkner@Sun.COM old_dir = p->p_lwpdir;
3879393SRoger.Faulkner@Sun.COM old_dirsz = p->p_lwpdir_sz;
3889393SRoger.Faulkner@Sun.COM p->p_lwpdir = new_dir;
3899393SRoger.Faulkner@Sun.COM p->p_lwpfree = new_dir;
3909393SRoger.Faulkner@Sun.COM p->p_lwpdir_sz = new_dirsz;
3919393SRoger.Faulkner@Sun.COM for (ldp = old_dir, i = 0; i < old_dirsz; i++, ldp++)
3929393SRoger.Faulkner@Sun.COM lwp_hash_in(p, ldp->ld_entry,
3939393SRoger.Faulkner@Sun.COM new_hash, new_hashsz, 0);
3949393SRoger.Faulkner@Sun.COM
3959393SRoger.Faulkner@Sun.COM /*
3969393SRoger.Faulkner@Sun.COM * Remember the old hash table along with all
3979393SRoger.Faulkner@Sun.COM * of the previously-remembered hash tables.
3989393SRoger.Faulkner@Sun.COM * We will free them at process exit or exec.
3999393SRoger.Faulkner@Sun.COM */
4009393SRoger.Faulkner@Sun.COM ret_tidhash->rth_tidhash = old_hash;
4019393SRoger.Faulkner@Sun.COM ret_tidhash->rth_tidhash_sz = old_hashsz;
4029393SRoger.Faulkner@Sun.COM ret_tidhash->rth_next = p->p_ret_tidhash;
4039393SRoger.Faulkner@Sun.COM p->p_ret_tidhash = ret_tidhash;
4049393SRoger.Faulkner@Sun.COM
4050Sstevel@tonic-gate /*
4069393SRoger.Faulkner@Sun.COM * Now establish the new tid hash table.
4079393SRoger.Faulkner@Sun.COM * As soon as we assign p->p_tidhash,
4089393SRoger.Faulkner@Sun.COM * code in lwp_unpark() can start using it.
4099393SRoger.Faulkner@Sun.COM */
4109393SRoger.Faulkner@Sun.COM membar_producer();
4119393SRoger.Faulkner@Sun.COM p->p_tidhash = new_hash;
4129393SRoger.Faulkner@Sun.COM
4139393SRoger.Faulkner@Sun.COM /*
4149393SRoger.Faulkner@Sun.COM * It is necessary that p_tidhash reach global
4159393SRoger.Faulkner@Sun.COM * visibility before p_tidhash_sz. Otherwise,
4169393SRoger.Faulkner@Sun.COM * code in lwp_hash_lookup_and_lock() could
4179393SRoger.Faulkner@Sun.COM * index into the old p_tidhash using the new
4189393SRoger.Faulkner@Sun.COM * p_tidhash_sz and thereby access invalid data.
4190Sstevel@tonic-gate */
4209393SRoger.Faulkner@Sun.COM membar_producer();
4219393SRoger.Faulkner@Sun.COM p->p_tidhash_sz = new_hashsz;
4229393SRoger.Faulkner@Sun.COM
4239393SRoger.Faulkner@Sun.COM /*
4249393SRoger.Faulkner@Sun.COM * Release the locks; allow lwp_unpark() to carry on.
4259393SRoger.Faulkner@Sun.COM */
4269393SRoger.Faulkner@Sun.COM for (i = 0; i < old_hashsz; i++) {
4279393SRoger.Faulkner@Sun.COM mutex_exit(&old_hash[i].th_lock);
4289393SRoger.Faulkner@Sun.COM mutex_exit(&new_hash[i].th_lock);
4299393SRoger.Faulkner@Sun.COM }
4309393SRoger.Faulkner@Sun.COM
4319393SRoger.Faulkner@Sun.COM /*
4329393SRoger.Faulkner@Sun.COM * Avoid freeing these objects below.
4339393SRoger.Faulkner@Sun.COM */
4349393SRoger.Faulkner@Sun.COM ret_tidhash = NULL;
4359393SRoger.Faulkner@Sun.COM old_hash = NULL;
4369393SRoger.Faulkner@Sun.COM old_hashsz = 0;
4370Sstevel@tonic-gate }
4380Sstevel@tonic-gate }
4390Sstevel@tonic-gate
4400Sstevel@tonic-gate /*
4410Sstevel@tonic-gate * Block the process against /proc while we manipulate p->p_tlist,
4420Sstevel@tonic-gate * unless lwp_create() was called by /proc for the PCAGENT operation.
4430Sstevel@tonic-gate * We want to do this early enough so that we don't drop p->p_lock
4440Sstevel@tonic-gate * until the thread is put on the p->p_tlist.
4450Sstevel@tonic-gate */
4460Sstevel@tonic-gate if (p == curproc) {
4470Sstevel@tonic-gate prbarrier(p);
4480Sstevel@tonic-gate /*
4490Sstevel@tonic-gate * If the current lwp has been requested to stop, do so now.
4500Sstevel@tonic-gate * Otherwise we have a race condition between /proc attempting
4510Sstevel@tonic-gate * to stop the process and this thread creating a new lwp
4520Sstevel@tonic-gate * that was not seen when the /proc PCSTOP request was issued.
4530Sstevel@tonic-gate * We rely on stop() to call prbarrier(p) before returning.
4540Sstevel@tonic-gate */
4550Sstevel@tonic-gate while ((curthread->t_proc_flag & TP_PRSTOP) &&
45610541SGangadhar.M@Sun.COM !ttolwp(curthread)->lwp_nostop) {
45710541SGangadhar.M@Sun.COM /*
45810541SGangadhar.M@Sun.COM * We called pool_barrier_enter() before calling
45910541SGangadhar.M@Sun.COM * here to lwp_create(). We have to call
46010541SGangadhar.M@Sun.COM * pool_barrier_exit() before stopping.
46110541SGangadhar.M@Sun.COM */
46210541SGangadhar.M@Sun.COM pool_barrier_exit();
46310541SGangadhar.M@Sun.COM prbarrier(p);
4640Sstevel@tonic-gate stop(PR_REQUESTED, 0);
46510541SGangadhar.M@Sun.COM /*
46610541SGangadhar.M@Sun.COM * And we have to repeat the call to
46710541SGangadhar.M@Sun.COM * pool_barrier_enter after stopping.
46810541SGangadhar.M@Sun.COM */
46910541SGangadhar.M@Sun.COM pool_barrier_enter();
47010541SGangadhar.M@Sun.COM prbarrier(p);
47110541SGangadhar.M@Sun.COM }
4720Sstevel@tonic-gate
4730Sstevel@tonic-gate /*
4740Sstevel@tonic-gate * If process is exiting, there could be a race between
4750Sstevel@tonic-gate * the agent lwp creation and the new lwp currently being
4760Sstevel@tonic-gate * created. So to prevent this race lwp creation is failed
4770Sstevel@tonic-gate * if the process is exiting.
4780Sstevel@tonic-gate */
4790Sstevel@tonic-gate if (p->p_flag & (SEXITLWPS|SKILLED)) {
4800Sstevel@tonic-gate err = 1;
4810Sstevel@tonic-gate goto error;
4820Sstevel@tonic-gate }
4830Sstevel@tonic-gate
4840Sstevel@tonic-gate /*
4850Sstevel@tonic-gate * Since we might have dropped p->p_lock, the
4860Sstevel@tonic-gate * lwp directory free list might have changed.
4870Sstevel@tonic-gate */
4880Sstevel@tonic-gate if (p->p_lwpfree == NULL)
4890Sstevel@tonic-gate goto grow;
4900Sstevel@tonic-gate }
4910Sstevel@tonic-gate
4920Sstevel@tonic-gate kpreempt_disable(); /* can't grab cpu_lock here */
4930Sstevel@tonic-gate
4940Sstevel@tonic-gate /*
49511173SJonathan.Adams@Sun.COM * Inherit processor and processor set bindings from curthread.
49611173SJonathan.Adams@Sun.COM *
49711173SJonathan.Adams@Sun.COM * For kernel LWPs, we do not inherit processor set bindings at
49811173SJonathan.Adams@Sun.COM * process creation time (i.e. when p != curproc). After the
49911173SJonathan.Adams@Sun.COM * kernel process is created, any subsequent LWPs must be created
50011173SJonathan.Adams@Sun.COM * by threads in the kernel process, at which point we *will*
50111173SJonathan.Adams@Sun.COM * inherit processor set bindings.
5020Sstevel@tonic-gate */
50311173SJonathan.Adams@Sun.COM if (CLASS_KERNEL(cid) && p != curproc) {
5040Sstevel@tonic-gate t->t_bind_cpu = binding = PBIND_NONE;
5050Sstevel@tonic-gate t->t_cpupart = oldpart = &cp_default;
5060Sstevel@tonic-gate t->t_bind_pset = PS_NONE;
5076298Sakolb t->t_bindflag = (uchar_t)default_binding_mode;
5080Sstevel@tonic-gate } else {
5090Sstevel@tonic-gate binding = curthread->t_bind_cpu;
5100Sstevel@tonic-gate t->t_bind_cpu = binding;
5110Sstevel@tonic-gate oldpart = t->t_cpupart;
5120Sstevel@tonic-gate t->t_cpupart = curthread->t_cpupart;
5130Sstevel@tonic-gate t->t_bind_pset = curthread->t_bind_pset;
5146298Sakolb t->t_bindflag = curthread->t_bindflag |
5156298Sakolb (uchar_t)default_binding_mode;
5160Sstevel@tonic-gate }
5170Sstevel@tonic-gate
5180Sstevel@tonic-gate /*
5190Sstevel@tonic-gate * thread_create() initializes this thread's home lgroup to the root.
5200Sstevel@tonic-gate * Choose a more suitable lgroup, since this thread is associated
5210Sstevel@tonic-gate * with an lwp.
5220Sstevel@tonic-gate */
5230Sstevel@tonic-gate ASSERT(oldpart != NULL);
5240Sstevel@tonic-gate if (binding != PBIND_NONE && t->t_affinitycnt == 0) {
5250Sstevel@tonic-gate t->t_bound_cpu = cpu[binding];
5260Sstevel@tonic-gate if (t->t_lpl != t->t_bound_cpu->cpu_lpl)
5270Sstevel@tonic-gate lgrp_move_thread(t, t->t_bound_cpu->cpu_lpl, 1);
52811259SJonathan.Adams@Sun.COM } else if (CLASS_KERNEL(cid)) {
52911259SJonathan.Adams@Sun.COM /*
53011331SJonathan.Adams@Sun.COM * Kernel threads are always in the root lgrp.
53111259SJonathan.Adams@Sun.COM */
53211259SJonathan.Adams@Sun.COM lgrp_move_thread(t,
53311331SJonathan.Adams@Sun.COM &t->t_cpupart->cp_lgrploads[LGRP_ROOTID], 1);
5340Sstevel@tonic-gate } else {
5350Sstevel@tonic-gate lgrp_move_thread(t, lgrp_choose(t, t->t_cpupart), 1);
5360Sstevel@tonic-gate }
5370Sstevel@tonic-gate
5380Sstevel@tonic-gate kpreempt_enable();
5390Sstevel@tonic-gate
5400Sstevel@tonic-gate /*
5410Sstevel@tonic-gate * make sure lpl points to our own partition
5420Sstevel@tonic-gate */
5430Sstevel@tonic-gate ASSERT(t->t_lpl >= t->t_cpupart->cp_lgrploads);
5440Sstevel@tonic-gate ASSERT(t->t_lpl < t->t_cpupart->cp_lgrploads +
5450Sstevel@tonic-gate t->t_cpupart->cp_nlgrploads);
5460Sstevel@tonic-gate
5470Sstevel@tonic-gate /*
5480Sstevel@tonic-gate * It is safe to point the thread to the new project without holding it
5490Sstevel@tonic-gate * since we're holding the target process' p_lock here and therefore
5500Sstevel@tonic-gate * we're guaranteed that it will not move to another project.
5510Sstevel@tonic-gate */
55211331SJonathan.Adams@Sun.COM newkpj = p->p_task->tk_proj;
5530Sstevel@tonic-gate oldkpj = ttoproj(t);
5540Sstevel@tonic-gate if (newkpj != oldkpj) {
5550Sstevel@tonic-gate t->t_proj = newkpj;
5560Sstevel@tonic-gate (void) project_hold(newkpj);
5570Sstevel@tonic-gate project_rele(oldkpj);
5580Sstevel@tonic-gate }
5590Sstevel@tonic-gate
5600Sstevel@tonic-gate if (cid != NOCLASS) {
5610Sstevel@tonic-gate /*
5620Sstevel@tonic-gate * If the lwp is being created in the current process
5630Sstevel@tonic-gate * and matches the current thread's scheduling class,
5640Sstevel@tonic-gate * we should propagate the current thread's scheduling
5650Sstevel@tonic-gate * parameters by calling CL_FORK. Otherwise just use
5660Sstevel@tonic-gate * the defaults by calling CL_ENTERCLASS.
5670Sstevel@tonic-gate */
5680Sstevel@tonic-gate if (p != curproc || curthread->t_cid != cid) {
5690Sstevel@tonic-gate err = CL_ENTERCLASS(t, cid, NULL, NULL, bufp);
5700Sstevel@tonic-gate t->t_pri = pri; /* CL_ENTERCLASS may have changed it */
5716247Sraf /*
5726247Sraf * We don't call schedctl_set_cidpri(t) here
5736247Sraf * because the schedctl data is not yet set
5746247Sraf * up for the newly-created lwp.
5756247Sraf */
5760Sstevel@tonic-gate } else {
5770Sstevel@tonic-gate t->t_clfuncs = &(sclass[cid].cl_funcs->thread);
5780Sstevel@tonic-gate err = CL_FORK(curthread, t, bufp);
5790Sstevel@tonic-gate t->t_cid = cid;
5800Sstevel@tonic-gate }
5810Sstevel@tonic-gate if (err)
5820Sstevel@tonic-gate goto error;
5830Sstevel@tonic-gate else
5840Sstevel@tonic-gate bufp = NULL;
5850Sstevel@tonic-gate }
5860Sstevel@tonic-gate
5870Sstevel@tonic-gate /*
5880Sstevel@tonic-gate * If we were given an lwpid then use it, else allocate one.
5890Sstevel@tonic-gate */
5900Sstevel@tonic-gate if (lwpid != 0)
5910Sstevel@tonic-gate t->t_tid = lwpid;
5920Sstevel@tonic-gate else {
5930Sstevel@tonic-gate /*
5940Sstevel@tonic-gate * lwp/thread id 0 is never valid; reserved for special checks.
5950Sstevel@tonic-gate * lwp/thread id 1 is reserved for the main thread.
5960Sstevel@tonic-gate * Start again at 2 when INT_MAX has been reached
5970Sstevel@tonic-gate * (id_t is a signed 32-bit integer).
5980Sstevel@tonic-gate */
5990Sstevel@tonic-gate id_t prev_id = p->p_lwpid; /* last allocated tid */
6000Sstevel@tonic-gate
6010Sstevel@tonic-gate do { /* avoid lwpid duplication */
6020Sstevel@tonic-gate if (p->p_lwpid == INT_MAX) {
6030Sstevel@tonic-gate p->p_flag |= SLWPWRAP;
6040Sstevel@tonic-gate p->p_lwpid = 1;
6050Sstevel@tonic-gate }
6060Sstevel@tonic-gate if ((t->t_tid = ++p->p_lwpid) == prev_id) {
6070Sstevel@tonic-gate /*
6080Sstevel@tonic-gate * All lwpids are allocated; fail the request.
6090Sstevel@tonic-gate */
6100Sstevel@tonic-gate err = 1;
6110Sstevel@tonic-gate goto error;
6120Sstevel@tonic-gate }
6130Sstevel@tonic-gate /*
6140Sstevel@tonic-gate * We only need to worry about colliding with an id
6150Sstevel@tonic-gate * that's already in use if this process has
6160Sstevel@tonic-gate * cycled through all available lwp ids.
6170Sstevel@tonic-gate */
6180Sstevel@tonic-gate if ((p->p_flag & SLWPWRAP) == 0)
6190Sstevel@tonic-gate break;
6200Sstevel@tonic-gate } while (lwp_hash_lookup(p, t->t_tid) != NULL);
6210Sstevel@tonic-gate }
6222712Snn35248
6232712Snn35248 /*
6242712Snn35248 * If this is a branded process, let the brand do any necessary lwp
6252712Snn35248 * initialization.
6262712Snn35248 */
6272712Snn35248 if (PROC_IS_BRANDED(p)) {
6282712Snn35248 if (BROP(p)->b_initlwp(lwp)) {
6292712Snn35248 err = 1;
6302712Snn35248 goto error;
6312712Snn35248 }
6322712Snn35248 branded = 1;
6332712Snn35248 }
6342712Snn35248
6354426Saguzovsk if (t->t_tid == 1) {
6364426Saguzovsk kpreempt_disable();
6374426Saguzovsk ASSERT(t->t_lpl != NULL);
6384426Saguzovsk p->p_t1_lgrpid = t->t_lpl->lpl_lgrpid;
6394426Saguzovsk kpreempt_enable();
6404426Saguzovsk if (p->p_tr_lgrpid != LGRP_NONE &&
6414426Saguzovsk p->p_tr_lgrpid != p->p_t1_lgrpid) {
6424426Saguzovsk lgrp_update_trthr_migrations(1);
6434426Saguzovsk }
6444426Saguzovsk }
6454426Saguzovsk
6460Sstevel@tonic-gate p->p_lwpcnt++;
6470Sstevel@tonic-gate t->t_waitfor = -1;
6480Sstevel@tonic-gate
6490Sstevel@tonic-gate /*
6500Sstevel@tonic-gate * Turn microstate accounting on for thread if on for process.
6510Sstevel@tonic-gate */
6520Sstevel@tonic-gate if (p->p_flag & SMSACCT)
6530Sstevel@tonic-gate t->t_proc_flag |= TP_MSACCT;
6540Sstevel@tonic-gate
6550Sstevel@tonic-gate /*
6560Sstevel@tonic-gate * If the process has watchpoints, mark the new thread as such.
6570Sstevel@tonic-gate */
6580Sstevel@tonic-gate if (pr_watch_active(p))
6590Sstevel@tonic-gate watch_enable(t);
6600Sstevel@tonic-gate
6610Sstevel@tonic-gate /*
6620Sstevel@tonic-gate * The lwp is being created in the stopped state.
6630Sstevel@tonic-gate * We set all the necessary flags to indicate that fact here.
6640Sstevel@tonic-gate * We omit the TS_CREATE flag from t_schedflag so that the lwp
6650Sstevel@tonic-gate * cannot be set running until the caller is finished with it,
6660Sstevel@tonic-gate * even if lwp_continue() is called on it after we drop p->p_lock.
6670Sstevel@tonic-gate * When the caller is finished with the newly-created lwp,
6680Sstevel@tonic-gate * the caller must call lwp_create_done() to allow the lwp
6690Sstevel@tonic-gate * to be set running. If the TP_HOLDLWP is left set, the
6700Sstevel@tonic-gate * lwp will suspend itself after reaching system call exit.
6710Sstevel@tonic-gate */
6720Sstevel@tonic-gate init_mstate(t, LMS_STOPPED);
6730Sstevel@tonic-gate t->t_proc_flag |= TP_HOLDLWP;
6740Sstevel@tonic-gate t->t_schedflag |= (TS_ALLSTART & ~(TS_CSTART | TS_CREATE));
6750Sstevel@tonic-gate t->t_whystop = PR_SUSPENDED;
6760Sstevel@tonic-gate t->t_whatstop = SUSPEND_NORMAL;
6770Sstevel@tonic-gate t->t_sig_check = 1; /* ensure that TP_HOLDLWP is honored */
6780Sstevel@tonic-gate
6790Sstevel@tonic-gate /*
6800Sstevel@tonic-gate * Set system call processing flags in case tracing or profiling
6810Sstevel@tonic-gate * is set. The first system call will evaluate these and turn
6820Sstevel@tonic-gate * them off if they aren't needed.
6830Sstevel@tonic-gate */
6840Sstevel@tonic-gate t->t_pre_sys = 1;
6850Sstevel@tonic-gate t->t_post_sys = 1;
6860Sstevel@tonic-gate
6870Sstevel@tonic-gate /*
6880Sstevel@tonic-gate * Insert the new thread into the list of all threads.
6890Sstevel@tonic-gate */
6900Sstevel@tonic-gate if ((tx = p->p_tlist) == NULL) {
6910Sstevel@tonic-gate t->t_back = t;
6920Sstevel@tonic-gate t->t_forw = t;
6930Sstevel@tonic-gate p->p_tlist = t;
6940Sstevel@tonic-gate } else {
6950Sstevel@tonic-gate t->t_forw = tx;
6960Sstevel@tonic-gate t->t_back = tx->t_back;
6970Sstevel@tonic-gate tx->t_back->t_forw = t;
6980Sstevel@tonic-gate tx->t_back = t;
6990Sstevel@tonic-gate }
7000Sstevel@tonic-gate
7010Sstevel@tonic-gate /*
7020Sstevel@tonic-gate * Insert the new lwp into an lwp directory slot position
7030Sstevel@tonic-gate * and into the lwpid hash table.
7040Sstevel@tonic-gate */
7050Sstevel@tonic-gate lep->le_thread = t;
7060Sstevel@tonic-gate lep->le_lwpid = t->t_tid;
7070Sstevel@tonic-gate lep->le_start = t->t_start;
7089393SRoger.Faulkner@Sun.COM lwp_hash_in(p, lep, p->p_tidhash, p->p_tidhash_sz, 1);
7090Sstevel@tonic-gate
7100Sstevel@tonic-gate if (state == TS_RUN) {
7110Sstevel@tonic-gate /*
7120Sstevel@tonic-gate * We set the new lwp running immediately.
7130Sstevel@tonic-gate */
7140Sstevel@tonic-gate t->t_proc_flag &= ~TP_HOLDLWP;
7150Sstevel@tonic-gate lwp_create_done(t);
7160Sstevel@tonic-gate }
7170Sstevel@tonic-gate
7180Sstevel@tonic-gate error:
7190Sstevel@tonic-gate if (err) {
72011173SJonathan.Adams@Sun.COM if (CLASS_KERNEL(cid)) {
72111173SJonathan.Adams@Sun.COM /*
72211173SJonathan.Adams@Sun.COM * This should only happen if a system process runs
72311173SJonathan.Adams@Sun.COM * out of lwpids, which shouldn't occur.
72411173SJonathan.Adams@Sun.COM */
72511173SJonathan.Adams@Sun.COM panic("Failed to create a system LWP");
72611173SJonathan.Adams@Sun.COM }
7270Sstevel@tonic-gate /*
7280Sstevel@tonic-gate * We have failed to create an lwp, so decrement the number
7290Sstevel@tonic-gate * of lwps in the task and let the lgroup load averages know
7300Sstevel@tonic-gate * that this thread isn't going to show up.
7310Sstevel@tonic-gate */
7320Sstevel@tonic-gate kpreempt_disable();
7330Sstevel@tonic-gate lgrp_move_thread(t, NULL, 1);
7340Sstevel@tonic-gate kpreempt_enable();
7350Sstevel@tonic-gate
7360Sstevel@tonic-gate ASSERT(MUTEX_HELD(&p->p_lock));
7370Sstevel@tonic-gate mutex_enter(&p->p_zone->zone_nlwps_lock);
7380Sstevel@tonic-gate p->p_task->tk_nlwps--;
7390Sstevel@tonic-gate p->p_task->tk_proj->kpj_nlwps--;
7400Sstevel@tonic-gate p->p_zone->zone_nlwps--;
7410Sstevel@tonic-gate mutex_exit(&p->p_zone->zone_nlwps_lock);
7420Sstevel@tonic-gate if (cid != NOCLASS && bufp != NULL)
7430Sstevel@tonic-gate CL_FREE(cid, bufp);
7440Sstevel@tonic-gate
7452712Snn35248 if (branded)
7462712Snn35248 BROP(p)->b_freelwp(lwp);
7472712Snn35248
7480Sstevel@tonic-gate mutex_exit(&p->p_lock);
7490Sstevel@tonic-gate t->t_state = TS_FREE;
7500Sstevel@tonic-gate thread_rele(t);
7510Sstevel@tonic-gate
7520Sstevel@tonic-gate /*
7530Sstevel@tonic-gate * We need to remove t from the list of all threads
7540Sstevel@tonic-gate * because thread_exit()/lwp_exit() isn't called on t.
7550Sstevel@tonic-gate */
7560Sstevel@tonic-gate mutex_enter(&pidlock);
7570Sstevel@tonic-gate ASSERT(t != t->t_next); /* t0 never exits */
7580Sstevel@tonic-gate t->t_next->t_prev = t->t_prev;
7590Sstevel@tonic-gate t->t_prev->t_next = t->t_next;
7600Sstevel@tonic-gate mutex_exit(&pidlock);
7610Sstevel@tonic-gate
7620Sstevel@tonic-gate thread_free(t);
7630Sstevel@tonic-gate kmem_free(lep, sizeof (*lep));
7640Sstevel@tonic-gate lwp = NULL;
7650Sstevel@tonic-gate } else {
7660Sstevel@tonic-gate mutex_exit(&p->p_lock);
7670Sstevel@tonic-gate }
7680Sstevel@tonic-gate
7699393SRoger.Faulkner@Sun.COM if (old_dir != NULL)
7700Sstevel@tonic-gate kmem_free(old_dir, old_dirsz * sizeof (*old_dir));
7719393SRoger.Faulkner@Sun.COM if (old_hash != NULL)
7720Sstevel@tonic-gate kmem_free(old_hash, old_hashsz * sizeof (*old_hash));
7739393SRoger.Faulkner@Sun.COM if (ret_tidhash != NULL)
7749393SRoger.Faulkner@Sun.COM kmem_free(ret_tidhash, sizeof (ret_tidhash_t));
7750Sstevel@tonic-gate
7760Sstevel@tonic-gate DTRACE_PROC1(lwp__create, kthread_t *, t);
7770Sstevel@tonic-gate return (lwp);
7780Sstevel@tonic-gate }
7790Sstevel@tonic-gate
7800Sstevel@tonic-gate /*
7810Sstevel@tonic-gate * lwp_create_done() is called by the caller of lwp_create() to set the
7820Sstevel@tonic-gate * newly-created lwp running after the caller has finished manipulating it.
7830Sstevel@tonic-gate */
7840Sstevel@tonic-gate void
lwp_create_done(kthread_t * t)7850Sstevel@tonic-gate lwp_create_done(kthread_t *t)
7860Sstevel@tonic-gate {
7870Sstevel@tonic-gate proc_t *p = ttoproc(t);
7880Sstevel@tonic-gate
7890Sstevel@tonic-gate ASSERT(MUTEX_HELD(&p->p_lock));
7900Sstevel@tonic-gate
7910Sstevel@tonic-gate /*
7920Sstevel@tonic-gate * We set the TS_CREATE and TS_CSTART flags and call setrun_locked().
7930Sstevel@tonic-gate * (The absence of the TS_CREATE flag prevents the lwp from running
7940Sstevel@tonic-gate * until we are finished with it, even if lwp_continue() is called on
7950Sstevel@tonic-gate * it by some other lwp in the process or elsewhere in the kernel.)
7960Sstevel@tonic-gate */
7970Sstevel@tonic-gate thread_lock(t);
7980Sstevel@tonic-gate ASSERT(t->t_state == TS_STOPPED && !(t->t_schedflag & TS_CREATE));
7990Sstevel@tonic-gate /*
8000Sstevel@tonic-gate * If TS_CSTART is set, lwp_continue(t) has been called and
8010Sstevel@tonic-gate * has already incremented p_lwprcnt; avoid doing this twice.
8020Sstevel@tonic-gate */
8030Sstevel@tonic-gate if (!(t->t_schedflag & TS_CSTART))
8040Sstevel@tonic-gate p->p_lwprcnt++;
8050Sstevel@tonic-gate t->t_schedflag |= (TS_CSTART | TS_CREATE);
8060Sstevel@tonic-gate setrun_locked(t);
8070Sstevel@tonic-gate thread_unlock(t);
8080Sstevel@tonic-gate }
8090Sstevel@tonic-gate
8100Sstevel@tonic-gate /*
8110Sstevel@tonic-gate * Copy an LWP's active templates, and clear the latest contracts.
8120Sstevel@tonic-gate */
8130Sstevel@tonic-gate void
lwp_ctmpl_copy(klwp_t * dst,klwp_t * src)8140Sstevel@tonic-gate lwp_ctmpl_copy(klwp_t *dst, klwp_t *src)
8150Sstevel@tonic-gate {
8160Sstevel@tonic-gate int i;
8170Sstevel@tonic-gate
8180Sstevel@tonic-gate for (i = 0; i < ct_ntypes; i++) {
8190Sstevel@tonic-gate dst->lwp_ct_active[i] = ctmpl_dup(src->lwp_ct_active[i]);
8200Sstevel@tonic-gate dst->lwp_ct_latest[i] = NULL;
8210Sstevel@tonic-gate }
8220Sstevel@tonic-gate }
8230Sstevel@tonic-gate
8240Sstevel@tonic-gate /*
8250Sstevel@tonic-gate * Clear an LWP's contract template state.
8260Sstevel@tonic-gate */
8270Sstevel@tonic-gate void
lwp_ctmpl_clear(klwp_t * lwp)8280Sstevel@tonic-gate lwp_ctmpl_clear(klwp_t *lwp)
8290Sstevel@tonic-gate {
8300Sstevel@tonic-gate ct_template_t *tmpl;
8310Sstevel@tonic-gate int i;
8320Sstevel@tonic-gate
8330Sstevel@tonic-gate for (i = 0; i < ct_ntypes; i++) {
8340Sstevel@tonic-gate if ((tmpl = lwp->lwp_ct_active[i]) != NULL) {
8350Sstevel@tonic-gate ctmpl_free(tmpl);
8360Sstevel@tonic-gate lwp->lwp_ct_active[i] = NULL;
8370Sstevel@tonic-gate }
8380Sstevel@tonic-gate
8390Sstevel@tonic-gate if (lwp->lwp_ct_latest[i] != NULL) {
8400Sstevel@tonic-gate contract_rele(lwp->lwp_ct_latest[i]);
8410Sstevel@tonic-gate lwp->lwp_ct_latest[i] = NULL;
8420Sstevel@tonic-gate }
8430Sstevel@tonic-gate }
8440Sstevel@tonic-gate }
8450Sstevel@tonic-gate
8460Sstevel@tonic-gate /*
8470Sstevel@tonic-gate * Individual lwp exit.
8480Sstevel@tonic-gate * If this is the last lwp, exit the whole process.
8490Sstevel@tonic-gate */
8500Sstevel@tonic-gate void
lwp_exit(void)8510Sstevel@tonic-gate lwp_exit(void)
8520Sstevel@tonic-gate {
8530Sstevel@tonic-gate kthread_t *t = curthread;
8540Sstevel@tonic-gate klwp_t *lwp = ttolwp(t);
8550Sstevel@tonic-gate proc_t *p = ttoproc(t);
8560Sstevel@tonic-gate
8570Sstevel@tonic-gate ASSERT(MUTEX_HELD(&p->p_lock));
8580Sstevel@tonic-gate
8590Sstevel@tonic-gate mutex_exit(&p->p_lock);
8600Sstevel@tonic-gate
8610Sstevel@tonic-gate #if defined(__sparc)
8620Sstevel@tonic-gate /*
8630Sstevel@tonic-gate * Ensure that the user stack is fully abandoned..
8640Sstevel@tonic-gate */
8650Sstevel@tonic-gate trash_user_windows();
8660Sstevel@tonic-gate #endif
8670Sstevel@tonic-gate
8680Sstevel@tonic-gate tsd_exit(); /* free thread specific data */
8690Sstevel@tonic-gate
8700Sstevel@tonic-gate kcpc_passivate(); /* Clean up performance counter state */
8710Sstevel@tonic-gate
8720Sstevel@tonic-gate pollcleanup();
8730Sstevel@tonic-gate
8740Sstevel@tonic-gate if (t->t_door)
8750Sstevel@tonic-gate door_slam();
8760Sstevel@tonic-gate
8770Sstevel@tonic-gate if (t->t_schedctl != NULL)
8780Sstevel@tonic-gate schedctl_lwp_cleanup(t);
8790Sstevel@tonic-gate
8800Sstevel@tonic-gate if (t->t_upimutex != NULL)
8810Sstevel@tonic-gate upimutex_cleanup();
8820Sstevel@tonic-gate
8832712Snn35248 /*
8842712Snn35248 * Perform any brand specific exit processing, then release any
8852712Snn35248 * brand data associated with the lwp
8862712Snn35248 */
8872712Snn35248 if (PROC_IS_BRANDED(p))
8882712Snn35248 BROP(p)->b_lwpexit(lwp);
8892712Snn35248
890*12007SJakub.Jermar@Sun.COM lwp_pcb_exit();
891*12007SJakub.Jermar@Sun.COM
8920Sstevel@tonic-gate mutex_enter(&p->p_lock);
8930Sstevel@tonic-gate lwp_cleanup();
8940Sstevel@tonic-gate
8950Sstevel@tonic-gate /*
8960Sstevel@tonic-gate * When this process is dumping core, its lwps are held here
8970Sstevel@tonic-gate * until the core dump is finished. Then exitlwps() is called
8980Sstevel@tonic-gate * again to release these lwps so that they can finish exiting.
8990Sstevel@tonic-gate */
9000Sstevel@tonic-gate if (p->p_flag & SCOREDUMP)
9010Sstevel@tonic-gate stop(PR_SUSPENDED, SUSPEND_NORMAL);
9020Sstevel@tonic-gate
9030Sstevel@tonic-gate /*
904*12007SJakub.Jermar@Sun.COM * Block the process against /proc now that we have really acquired
905*12007SJakub.Jermar@Sun.COM * p->p_lock (to decrement p_lwpcnt and manipulate p_tlist at least).
906*12007SJakub.Jermar@Sun.COM */
907*12007SJakub.Jermar@Sun.COM prbarrier(p);
908*12007SJakub.Jermar@Sun.COM
909*12007SJakub.Jermar@Sun.COM /*
9100Sstevel@tonic-gate * Call proc_exit() if this is the last non-daemon lwp in the process.
9110Sstevel@tonic-gate */
9120Sstevel@tonic-gate if (!(t->t_proc_flag & TP_DAEMON) &&
9130Sstevel@tonic-gate p->p_lwpcnt == p->p_lwpdaemon + 1) {
9140Sstevel@tonic-gate mutex_exit(&p->p_lock);
915390Sraf if (proc_exit(CLD_EXITED, 0) == 0) {
9160Sstevel@tonic-gate /* Restarting init. */
9170Sstevel@tonic-gate return;
9180Sstevel@tonic-gate }
9190Sstevel@tonic-gate
9200Sstevel@tonic-gate /*
9210Sstevel@tonic-gate * proc_exit() returns a non-zero value when some other
9220Sstevel@tonic-gate * lwp got there first. We just have to continue in
9230Sstevel@tonic-gate * lwp_exit().
9240Sstevel@tonic-gate */
9250Sstevel@tonic-gate mutex_enter(&p->p_lock);
9260Sstevel@tonic-gate ASSERT(curproc->p_flag & SEXITLWPS);
927*12007SJakub.Jermar@Sun.COM prbarrier(p);
9280Sstevel@tonic-gate }
9290Sstevel@tonic-gate
9300Sstevel@tonic-gate DTRACE_PROC(lwp__exit);
9310Sstevel@tonic-gate
9320Sstevel@tonic-gate /*
9330Sstevel@tonic-gate * If the lwp is a detached lwp or if the process is exiting,
9340Sstevel@tonic-gate * remove (lwp_hash_out()) the lwp from the lwp directory.
9350Sstevel@tonic-gate * Otherwise null out the lwp's le_thread pointer in the lwp
9360Sstevel@tonic-gate * directory so that other threads will see it as a zombie lwp.
9370Sstevel@tonic-gate */
9380Sstevel@tonic-gate prlwpexit(t); /* notify /proc */
9390Sstevel@tonic-gate if (!(t->t_proc_flag & TP_TWAIT) || (p->p_flag & SEXITLWPS))
9400Sstevel@tonic-gate lwp_hash_out(p, t->t_tid);
9410Sstevel@tonic-gate else {
9420Sstevel@tonic-gate ASSERT(!(t->t_proc_flag & TP_DAEMON));
9430Sstevel@tonic-gate p->p_lwpdir[t->t_dslot].ld_entry->le_thread = NULL;
9440Sstevel@tonic-gate p->p_zombcnt++;
9450Sstevel@tonic-gate cv_broadcast(&p->p_lwpexit);
9460Sstevel@tonic-gate }
9470Sstevel@tonic-gate if (t->t_proc_flag & TP_DAEMON) {
9480Sstevel@tonic-gate p->p_lwpdaemon--;
9490Sstevel@tonic-gate t->t_proc_flag &= ~TP_DAEMON;
9500Sstevel@tonic-gate }
9510Sstevel@tonic-gate t->t_proc_flag &= ~TP_TWAIT;
9520Sstevel@tonic-gate
9530Sstevel@tonic-gate /*
9540Sstevel@tonic-gate * Maintain accurate lwp count for task.max-lwps resource control.
9550Sstevel@tonic-gate */
9560Sstevel@tonic-gate mutex_enter(&p->p_zone->zone_nlwps_lock);
9570Sstevel@tonic-gate p->p_task->tk_nlwps--;
9580Sstevel@tonic-gate p->p_task->tk_proj->kpj_nlwps--;
9590Sstevel@tonic-gate p->p_zone->zone_nlwps--;
9600Sstevel@tonic-gate mutex_exit(&p->p_zone->zone_nlwps_lock);
9610Sstevel@tonic-gate
9620Sstevel@tonic-gate CL_EXIT(t); /* tell the scheduler that t is exiting */
9630Sstevel@tonic-gate ASSERT(p->p_lwpcnt != 0);
9640Sstevel@tonic-gate p->p_lwpcnt--;
9650Sstevel@tonic-gate
9660Sstevel@tonic-gate /*
9670Sstevel@tonic-gate * If all remaining non-daemon lwps are waiting in lwp_wait(),
9680Sstevel@tonic-gate * wake them up so someone can return EDEADLK.
9690Sstevel@tonic-gate * (See the block comment preceeding lwp_wait().)
9700Sstevel@tonic-gate */
9710Sstevel@tonic-gate if (p->p_lwpcnt == p->p_lwpdaemon + (p->p_lwpwait - p->p_lwpdwait))
9720Sstevel@tonic-gate cv_broadcast(&p->p_lwpexit);
9730Sstevel@tonic-gate
9740Sstevel@tonic-gate t->t_proc_flag |= TP_LWPEXIT;
9750Sstevel@tonic-gate term_mstate(t);
9763792Sakolb
9770Sstevel@tonic-gate #ifndef NPROBE
9780Sstevel@tonic-gate /* Kernel probe */
9790Sstevel@tonic-gate if (t->t_tnf_tpdp)
9800Sstevel@tonic-gate tnf_thread_exit();
9810Sstevel@tonic-gate #endif /* NPROBE */
9820Sstevel@tonic-gate
9830Sstevel@tonic-gate t->t_forw->t_back = t->t_back;
9840Sstevel@tonic-gate t->t_back->t_forw = t->t_forw;
9850Sstevel@tonic-gate if (t == p->p_tlist)
9860Sstevel@tonic-gate p->p_tlist = t->t_forw;
9870Sstevel@tonic-gate
9880Sstevel@tonic-gate /*
9890Sstevel@tonic-gate * Clean up the signal state.
9900Sstevel@tonic-gate */
9910Sstevel@tonic-gate if (t->t_sigqueue != NULL)
9920Sstevel@tonic-gate sigdelq(p, t, 0);
9930Sstevel@tonic-gate if (lwp->lwp_curinfo != NULL) {
9940Sstevel@tonic-gate siginfofree(lwp->lwp_curinfo);
9950Sstevel@tonic-gate lwp->lwp_curinfo = NULL;
9960Sstevel@tonic-gate }
9970Sstevel@tonic-gate
9980Sstevel@tonic-gate thread_rele(t);
9990Sstevel@tonic-gate
10000Sstevel@tonic-gate /*
10010Sstevel@tonic-gate * Terminated lwps are associated with process zero and are put onto
10020Sstevel@tonic-gate * death-row by resume(). Avoid preemption after resetting t->t_procp.
10030Sstevel@tonic-gate */
10040Sstevel@tonic-gate t->t_preempt++;
10051217Srab
10061217Srab if (t->t_ctx != NULL)
10071217Srab exitctx(t);
10081217Srab if (p->p_pctx != NULL)
10091217Srab exitpctx(p);
10101217Srab
10110Sstevel@tonic-gate t->t_procp = &p0;
10120Sstevel@tonic-gate
10130Sstevel@tonic-gate /*
10140Sstevel@tonic-gate * Notify the HAT about the change of address space
10150Sstevel@tonic-gate */
10160Sstevel@tonic-gate hat_thread_exit(t);
10170Sstevel@tonic-gate /*
10180Sstevel@tonic-gate * When this is the last running lwp in this process and some lwp is
10190Sstevel@tonic-gate * waiting for this condition to become true, or this thread was being
10200Sstevel@tonic-gate * suspended, then the waiting lwp is awakened.
10210Sstevel@tonic-gate *
10220Sstevel@tonic-gate * Also, if the process is exiting, we may have a thread waiting in
10230Sstevel@tonic-gate * exitlwps() that needs to be notified.
10240Sstevel@tonic-gate */
10250Sstevel@tonic-gate if (--p->p_lwprcnt == 0 || (t->t_proc_flag & TP_HOLDLWP) ||
10260Sstevel@tonic-gate (p->p_flag & SEXITLWPS))
10270Sstevel@tonic-gate cv_broadcast(&p->p_holdlwps);
10280Sstevel@tonic-gate
10290Sstevel@tonic-gate /*
10300Sstevel@tonic-gate * Need to drop p_lock so we can reacquire pidlock.
10310Sstevel@tonic-gate */
10320Sstevel@tonic-gate mutex_exit(&p->p_lock);
10330Sstevel@tonic-gate mutex_enter(&pidlock);
10340Sstevel@tonic-gate
10350Sstevel@tonic-gate ASSERT(t != t->t_next); /* t0 never exits */
10360Sstevel@tonic-gate t->t_next->t_prev = t->t_prev;
10370Sstevel@tonic-gate t->t_prev->t_next = t->t_next;
10380Sstevel@tonic-gate cv_broadcast(&t->t_joincv); /* wake up anyone in thread_join */
10390Sstevel@tonic-gate mutex_exit(&pidlock);
10400Sstevel@tonic-gate
10410Sstevel@tonic-gate t->t_state = TS_ZOMB;
10420Sstevel@tonic-gate swtch_from_zombie();
10430Sstevel@tonic-gate /* never returns */
10440Sstevel@tonic-gate }
10450Sstevel@tonic-gate
10460Sstevel@tonic-gate
10470Sstevel@tonic-gate /*
10480Sstevel@tonic-gate * Cleanup function for an exiting lwp.
10490Sstevel@tonic-gate * Called both from lwp_exit() and from proc_exit().
10500Sstevel@tonic-gate * p->p_lock is repeatedly released and grabbed in this function.
10510Sstevel@tonic-gate */
10520Sstevel@tonic-gate void
lwp_cleanup(void)10530Sstevel@tonic-gate lwp_cleanup(void)
10540Sstevel@tonic-gate {
10550Sstevel@tonic-gate kthread_t *t = curthread;
10560Sstevel@tonic-gate proc_t *p = ttoproc(t);
10570Sstevel@tonic-gate
10580Sstevel@tonic-gate ASSERT(MUTEX_HELD(&p->p_lock));
10590Sstevel@tonic-gate
10600Sstevel@tonic-gate /* untimeout any lwp-bound realtime timers */
10610Sstevel@tonic-gate if (p->p_itimer != NULL)
10620Sstevel@tonic-gate timer_lwpexit();
10630Sstevel@tonic-gate
10640Sstevel@tonic-gate /*
10650Sstevel@tonic-gate * If this is the /proc agent lwp that is exiting, readjust p_lwpid
10660Sstevel@tonic-gate * so it appears that the agent never existed, and clear p_agenttp.
10670Sstevel@tonic-gate */
10680Sstevel@tonic-gate if (t == p->p_agenttp) {
10690Sstevel@tonic-gate ASSERT(t->t_tid == p->p_lwpid);
10700Sstevel@tonic-gate p->p_lwpid--;
10710Sstevel@tonic-gate p->p_agenttp = NULL;
10720Sstevel@tonic-gate }
10730Sstevel@tonic-gate
10740Sstevel@tonic-gate /*
10750Sstevel@tonic-gate * Do lgroup bookkeeping to account for thread exiting.
10760Sstevel@tonic-gate */
10770Sstevel@tonic-gate kpreempt_disable();
10780Sstevel@tonic-gate lgrp_move_thread(t, NULL, 1);
10794426Saguzovsk if (t->t_tid == 1) {
10804426Saguzovsk p->p_t1_lgrpid = LGRP_NONE;
10814426Saguzovsk }
10820Sstevel@tonic-gate kpreempt_enable();
10830Sstevel@tonic-gate
10840Sstevel@tonic-gate lwp_ctmpl_clear(ttolwp(t));
10850Sstevel@tonic-gate }
10860Sstevel@tonic-gate
10870Sstevel@tonic-gate int
lwp_suspend(kthread_t * t)10880Sstevel@tonic-gate lwp_suspend(kthread_t *t)
10890Sstevel@tonic-gate {
10900Sstevel@tonic-gate int tid;
10910Sstevel@tonic-gate proc_t *p = ttoproc(t);
10920Sstevel@tonic-gate
10930Sstevel@tonic-gate ASSERT(MUTEX_HELD(&p->p_lock));
10940Sstevel@tonic-gate
10950Sstevel@tonic-gate /*
10960Sstevel@tonic-gate * Set the thread's TP_HOLDLWP flag so it will stop in holdlwp().
10970Sstevel@tonic-gate * If an lwp is stopping itself, there is no need to wait.
10980Sstevel@tonic-gate */
1099875Sraf top:
11000Sstevel@tonic-gate t->t_proc_flag |= TP_HOLDLWP;
11010Sstevel@tonic-gate if (t == curthread) {
11020Sstevel@tonic-gate t->t_sig_check = 1;
11030Sstevel@tonic-gate } else {
11040Sstevel@tonic-gate /*
11050Sstevel@tonic-gate * Make sure the lwp stops promptly.
11060Sstevel@tonic-gate */
11070Sstevel@tonic-gate thread_lock(t);
11080Sstevel@tonic-gate t->t_sig_check = 1;
11090Sstevel@tonic-gate /*
11100Sstevel@tonic-gate * XXX Should use virtual stop like /proc does instead of
11110Sstevel@tonic-gate * XXX waking the thread to get it to stop.
11120Sstevel@tonic-gate */
11133792Sakolb if (ISWAKEABLE(t) || ISWAITING(t)) {
11140Sstevel@tonic-gate setrun_locked(t);
11153792Sakolb } else if (t->t_state == TS_ONPROC && t->t_cpu != CPU) {
11160Sstevel@tonic-gate poke_cpu(t->t_cpu->cpu_id);
11173792Sakolb }
11183792Sakolb
11190Sstevel@tonic-gate tid = t->t_tid; /* remember thread ID */
11200Sstevel@tonic-gate /*
11210Sstevel@tonic-gate * Wait for lwp to stop
11220Sstevel@tonic-gate */
11230Sstevel@tonic-gate while (!SUSPENDED(t)) {
11240Sstevel@tonic-gate /*
11250Sstevel@tonic-gate * Drop the thread lock before waiting and reacquire it
11260Sstevel@tonic-gate * afterwards, so the thread can change its t_state
11270Sstevel@tonic-gate * field.
11280Sstevel@tonic-gate */
11290Sstevel@tonic-gate thread_unlock(t);
11300Sstevel@tonic-gate
11310Sstevel@tonic-gate /*
11320Sstevel@tonic-gate * Check if aborted by exitlwps().
11330Sstevel@tonic-gate */
11340Sstevel@tonic-gate if (p->p_flag & SEXITLWPS)
11350Sstevel@tonic-gate lwp_exit();
11360Sstevel@tonic-gate
11370Sstevel@tonic-gate /*
11380Sstevel@tonic-gate * Cooperate with jobcontrol signals and /proc stopping
11390Sstevel@tonic-gate * by calling cv_wait_sig() to wait for the target
11400Sstevel@tonic-gate * lwp to stop. Just using cv_wait() can lead to
11410Sstevel@tonic-gate * deadlock because, if some other lwp has stopped
11420Sstevel@tonic-gate * by either of these mechanisms, then p_lwprcnt will
11430Sstevel@tonic-gate * never become zero if we do a cv_wait().
11440Sstevel@tonic-gate */
11450Sstevel@tonic-gate if (!cv_wait_sig(&p->p_holdlwps, &p->p_lock))
11460Sstevel@tonic-gate return (EINTR);
11470Sstevel@tonic-gate
11480Sstevel@tonic-gate /*
11490Sstevel@tonic-gate * Check to see if thread died while we were
11500Sstevel@tonic-gate * waiting for it to suspend.
11510Sstevel@tonic-gate */
11520Sstevel@tonic-gate if (idtot(p, tid) == NULL)
11530Sstevel@tonic-gate return (ESRCH);
11540Sstevel@tonic-gate
11550Sstevel@tonic-gate thread_lock(t);
11560Sstevel@tonic-gate /*
1157875Sraf * If the TP_HOLDLWP flag went away, lwp_continue()
1158875Sraf * or vfork() must have been called while we were
1159875Sraf * waiting, so start over again.
11600Sstevel@tonic-gate */
11610Sstevel@tonic-gate if ((t->t_proc_flag & TP_HOLDLWP) == 0) {
11620Sstevel@tonic-gate thread_unlock(t);
1163875Sraf goto top;
11640Sstevel@tonic-gate }
11650Sstevel@tonic-gate }
11660Sstevel@tonic-gate thread_unlock(t);
11670Sstevel@tonic-gate }
11680Sstevel@tonic-gate return (0);
11690Sstevel@tonic-gate }
11700Sstevel@tonic-gate
11710Sstevel@tonic-gate /*
11720Sstevel@tonic-gate * continue a lwp that's been stopped by lwp_suspend().
11730Sstevel@tonic-gate */
11740Sstevel@tonic-gate void
lwp_continue(kthread_t * t)11750Sstevel@tonic-gate lwp_continue(kthread_t *t)
11760Sstevel@tonic-gate {
11770Sstevel@tonic-gate proc_t *p = ttoproc(t);
11780Sstevel@tonic-gate int was_suspended = t->t_proc_flag & TP_HOLDLWP;
11790Sstevel@tonic-gate
11800Sstevel@tonic-gate ASSERT(MUTEX_HELD(&p->p_lock));
11810Sstevel@tonic-gate
11820Sstevel@tonic-gate t->t_proc_flag &= ~TP_HOLDLWP;
11830Sstevel@tonic-gate thread_lock(t);
11840Sstevel@tonic-gate if (SUSPENDED(t) &&
11850Sstevel@tonic-gate !(p->p_flag & (SHOLDFORK | SHOLDFORK1 | SHOLDWATCH))) {
11860Sstevel@tonic-gate p->p_lwprcnt++;
11870Sstevel@tonic-gate t->t_schedflag |= TS_CSTART;
11880Sstevel@tonic-gate setrun_locked(t);
11890Sstevel@tonic-gate }
11900Sstevel@tonic-gate thread_unlock(t);
11910Sstevel@tonic-gate /*
11920Sstevel@tonic-gate * Wakeup anyone waiting for this thread to be suspended
11930Sstevel@tonic-gate */
11940Sstevel@tonic-gate if (was_suspended)
11950Sstevel@tonic-gate cv_broadcast(&p->p_holdlwps);
11960Sstevel@tonic-gate }
11970Sstevel@tonic-gate
11980Sstevel@tonic-gate /*
11990Sstevel@tonic-gate * ********************************
12000Sstevel@tonic-gate * Miscellaneous lwp routines *
12010Sstevel@tonic-gate * ********************************
12020Sstevel@tonic-gate */
12030Sstevel@tonic-gate /*
12040Sstevel@tonic-gate * When a process is undergoing a forkall(), its p_flag is set to SHOLDFORK.
12050Sstevel@tonic-gate * This will cause the process's lwps to stop at a hold point. A hold
12060Sstevel@tonic-gate * point is where a kernel thread has a flat stack. This is at the
12070Sstevel@tonic-gate * return from a system call and at the return from a user level trap.
12080Sstevel@tonic-gate *
12090Sstevel@tonic-gate * When a process is undergoing a fork1() or vfork(), its p_flag is set to
12100Sstevel@tonic-gate * SHOLDFORK1. This will cause the process's lwps to stop at a modified
12110Sstevel@tonic-gate * hold point. The lwps in the process are not being cloned, so they
12120Sstevel@tonic-gate * are held at the usual hold points and also within issig_forreal().
12130Sstevel@tonic-gate * This has the side-effect that their system calls do not return
12140Sstevel@tonic-gate * showing EINTR.
12150Sstevel@tonic-gate *
12160Sstevel@tonic-gate * An lwp can also be held. This is identified by the TP_HOLDLWP flag on
12170Sstevel@tonic-gate * the thread. The TP_HOLDLWP flag is set in lwp_suspend(), where the active
12180Sstevel@tonic-gate * lwp is waiting for the target lwp to be stopped.
12190Sstevel@tonic-gate */
12200Sstevel@tonic-gate void
holdlwp(void)12210Sstevel@tonic-gate holdlwp(void)
12220Sstevel@tonic-gate {
12230Sstevel@tonic-gate proc_t *p = curproc;
12240Sstevel@tonic-gate kthread_t *t = curthread;
12250Sstevel@tonic-gate
12260Sstevel@tonic-gate mutex_enter(&p->p_lock);
12270Sstevel@tonic-gate /*
12280Sstevel@tonic-gate * Don't terminate immediately if the process is dumping core.
12290Sstevel@tonic-gate * Once the process has dumped core, all lwps are terminated.
12300Sstevel@tonic-gate */
12310Sstevel@tonic-gate if (!(p->p_flag & SCOREDUMP)) {
12320Sstevel@tonic-gate if ((p->p_flag & SEXITLWPS) || (t->t_proc_flag & TP_EXITLWP))
12330Sstevel@tonic-gate lwp_exit();
12340Sstevel@tonic-gate }
12350Sstevel@tonic-gate if (!(ISHOLD(p)) && !(p->p_flag & (SHOLDFORK1 | SHOLDWATCH))) {
12360Sstevel@tonic-gate mutex_exit(&p->p_lock);
12370Sstevel@tonic-gate return;
12380Sstevel@tonic-gate }
12390Sstevel@tonic-gate /*
12400Sstevel@tonic-gate * stop() decrements p->p_lwprcnt and cv_signal()s &p->p_holdlwps
12410Sstevel@tonic-gate * when p->p_lwprcnt becomes zero.
12420Sstevel@tonic-gate */
12430Sstevel@tonic-gate stop(PR_SUSPENDED, SUSPEND_NORMAL);
12440Sstevel@tonic-gate if (p->p_flag & SEXITLWPS)
12450Sstevel@tonic-gate lwp_exit();
12460Sstevel@tonic-gate mutex_exit(&p->p_lock);
12470Sstevel@tonic-gate }
12480Sstevel@tonic-gate
12490Sstevel@tonic-gate /*
12500Sstevel@tonic-gate * Have all lwps within the process hold at a point where they are
12510Sstevel@tonic-gate * cloneable (SHOLDFORK) or just safe w.r.t. fork1 (SHOLDFORK1).
12520Sstevel@tonic-gate */
12530Sstevel@tonic-gate int
holdlwps(int holdflag)12540Sstevel@tonic-gate holdlwps(int holdflag)
12550Sstevel@tonic-gate {
12560Sstevel@tonic-gate proc_t *p = curproc;
12570Sstevel@tonic-gate
12580Sstevel@tonic-gate ASSERT(holdflag == SHOLDFORK || holdflag == SHOLDFORK1);
12590Sstevel@tonic-gate mutex_enter(&p->p_lock);
12600Sstevel@tonic-gate schedctl_finish_sigblock(curthread);
12610Sstevel@tonic-gate again:
12620Sstevel@tonic-gate while (p->p_flag & (SEXITLWPS | SHOLDFORK | SHOLDFORK1 | SHOLDWATCH)) {
12630Sstevel@tonic-gate /*
12640Sstevel@tonic-gate * If another lwp is doing a forkall() or proc_exit(), bail out.
12650Sstevel@tonic-gate */
12660Sstevel@tonic-gate if (p->p_flag & (SEXITLWPS | SHOLDFORK)) {
12670Sstevel@tonic-gate mutex_exit(&p->p_lock);
12680Sstevel@tonic-gate return (0);
12690Sstevel@tonic-gate }
12700Sstevel@tonic-gate /*
12710Sstevel@tonic-gate * Another lwp is doing a fork1() or is undergoing
12720Sstevel@tonic-gate * watchpoint activity. We hold here for it to complete.
12730Sstevel@tonic-gate */
12740Sstevel@tonic-gate stop(PR_SUSPENDED, SUSPEND_NORMAL);
12750Sstevel@tonic-gate }
12760Sstevel@tonic-gate p->p_flag |= holdflag;
12770Sstevel@tonic-gate pokelwps(p);
12780Sstevel@tonic-gate --p->p_lwprcnt;
12790Sstevel@tonic-gate /*
12800Sstevel@tonic-gate * Wait for the process to become quiescent (p->p_lwprcnt == 0).
12810Sstevel@tonic-gate */
12820Sstevel@tonic-gate while (p->p_lwprcnt > 0) {
12830Sstevel@tonic-gate /*
12840Sstevel@tonic-gate * Check if aborted by exitlwps().
12850Sstevel@tonic-gate * Also check if SHOLDWATCH is set; it takes precedence.
12860Sstevel@tonic-gate */
12870Sstevel@tonic-gate if (p->p_flag & (SEXITLWPS | SHOLDWATCH)) {
12880Sstevel@tonic-gate p->p_lwprcnt++;
12890Sstevel@tonic-gate p->p_flag &= ~holdflag;
12900Sstevel@tonic-gate cv_broadcast(&p->p_holdlwps);
12910Sstevel@tonic-gate goto again;
12920Sstevel@tonic-gate }
12930Sstevel@tonic-gate /*
12940Sstevel@tonic-gate * Cooperate with jobcontrol signals and /proc stopping.
12950Sstevel@tonic-gate * If some other lwp has stopped by either of these
12960Sstevel@tonic-gate * mechanisms, then p_lwprcnt will never become zero
12970Sstevel@tonic-gate * and the process will appear deadlocked unless we
12980Sstevel@tonic-gate * stop here in sympathy with the other lwp before
12990Sstevel@tonic-gate * doing the cv_wait() below.
13000Sstevel@tonic-gate *
13010Sstevel@tonic-gate * If the other lwp stops after we do the cv_wait(), it
13020Sstevel@tonic-gate * will wake us up to loop around and do the sympathy stop.
13030Sstevel@tonic-gate *
13040Sstevel@tonic-gate * Since stop() drops p->p_lock, we must start from
13050Sstevel@tonic-gate * the top again on returning from stop().
13060Sstevel@tonic-gate */
13070Sstevel@tonic-gate if (p->p_stopsig | (curthread->t_proc_flag & TP_PRSTOP)) {
13080Sstevel@tonic-gate int whystop = p->p_stopsig? PR_JOBCONTROL :
13090Sstevel@tonic-gate PR_REQUESTED;
13100Sstevel@tonic-gate p->p_lwprcnt++;
13110Sstevel@tonic-gate p->p_flag &= ~holdflag;
13120Sstevel@tonic-gate stop(whystop, p->p_stopsig);
13130Sstevel@tonic-gate goto again;
13140Sstevel@tonic-gate }
13150Sstevel@tonic-gate cv_wait(&p->p_holdlwps, &p->p_lock);
13160Sstevel@tonic-gate }
13170Sstevel@tonic-gate p->p_lwprcnt++;
13180Sstevel@tonic-gate p->p_flag &= ~holdflag;
13190Sstevel@tonic-gate mutex_exit(&p->p_lock);
13200Sstevel@tonic-gate return (1);
13210Sstevel@tonic-gate }
13220Sstevel@tonic-gate
13230Sstevel@tonic-gate /*
13240Sstevel@tonic-gate * See comments for holdwatch(), below.
13250Sstevel@tonic-gate */
13260Sstevel@tonic-gate static int
holdcheck(int clearflags)13270Sstevel@tonic-gate holdcheck(int clearflags)
13280Sstevel@tonic-gate {
13290Sstevel@tonic-gate proc_t *p = curproc;
13300Sstevel@tonic-gate
13310Sstevel@tonic-gate /*
13320Sstevel@tonic-gate * If we are trying to exit, that takes precedence over anything else.
13330Sstevel@tonic-gate */
13340Sstevel@tonic-gate if (p->p_flag & SEXITLWPS) {
13350Sstevel@tonic-gate p->p_lwprcnt++;
13360Sstevel@tonic-gate p->p_flag &= ~clearflags;
13370Sstevel@tonic-gate lwp_exit();
13380Sstevel@tonic-gate }
13390Sstevel@tonic-gate
13400Sstevel@tonic-gate /*
13410Sstevel@tonic-gate * If another thread is calling fork1(), stop the current thread so the
13420Sstevel@tonic-gate * other can complete.
13430Sstevel@tonic-gate */
13440Sstevel@tonic-gate if (p->p_flag & SHOLDFORK1) {
13450Sstevel@tonic-gate p->p_lwprcnt++;
13460Sstevel@tonic-gate stop(PR_SUSPENDED, SUSPEND_NORMAL);
13470Sstevel@tonic-gate if (p->p_flag & SEXITLWPS) {
13480Sstevel@tonic-gate p->p_flag &= ~clearflags;
13490Sstevel@tonic-gate lwp_exit();
13500Sstevel@tonic-gate }
13510Sstevel@tonic-gate return (-1);
13520Sstevel@tonic-gate }
13530Sstevel@tonic-gate
13540Sstevel@tonic-gate /*
13550Sstevel@tonic-gate * If another thread is calling fork(), then indicate we are doing
13560Sstevel@tonic-gate * watchpoint activity. This will cause holdlwps() above to stop the
13570Sstevel@tonic-gate * forking thread, at which point we can continue with watchpoint
13580Sstevel@tonic-gate * activity.
13590Sstevel@tonic-gate */
13600Sstevel@tonic-gate if (p->p_flag & SHOLDFORK) {
13610Sstevel@tonic-gate p->p_lwprcnt++;
13620Sstevel@tonic-gate while (p->p_flag & SHOLDFORK) {
13630Sstevel@tonic-gate p->p_flag |= SHOLDWATCH;
13640Sstevel@tonic-gate cv_broadcast(&p->p_holdlwps);
13650Sstevel@tonic-gate cv_wait(&p->p_holdlwps, &p->p_lock);
13660Sstevel@tonic-gate p->p_flag &= ~SHOLDWATCH;
13670Sstevel@tonic-gate }
13680Sstevel@tonic-gate return (-1);
13690Sstevel@tonic-gate }
13700Sstevel@tonic-gate
13710Sstevel@tonic-gate return (0);
13720Sstevel@tonic-gate }
13730Sstevel@tonic-gate
13740Sstevel@tonic-gate /*
13750Sstevel@tonic-gate * Stop all lwps within the process, holding themselves in the kernel while the
13760Sstevel@tonic-gate * active lwp undergoes watchpoint activity. This is more complicated than
13770Sstevel@tonic-gate * expected because stop() relies on calling holdwatch() in order to copyin data
13780Sstevel@tonic-gate * from the user's address space. A double barrier is used to prevent an
13790Sstevel@tonic-gate * infinite loop.
13800Sstevel@tonic-gate *
13810Sstevel@tonic-gate * o The first thread into holdwatch() is the 'master' thread and does
13820Sstevel@tonic-gate * the following:
13830Sstevel@tonic-gate *
13840Sstevel@tonic-gate * - Sets SHOLDWATCH on the current process
13850Sstevel@tonic-gate * - Sets TP_WATCHSTOP on the current thread
13860Sstevel@tonic-gate * - Waits for all threads to be either stopped or have
13870Sstevel@tonic-gate * TP_WATCHSTOP set.
13880Sstevel@tonic-gate * - Sets the SWATCHOK flag on the process
13890Sstevel@tonic-gate * - Unsets TP_WATCHSTOP
13900Sstevel@tonic-gate * - Waits for the other threads to completely stop
13910Sstevel@tonic-gate * - Unsets SWATCHOK
13920Sstevel@tonic-gate *
13930Sstevel@tonic-gate * o If SHOLDWATCH is already set when we enter this function, then another
13940Sstevel@tonic-gate * thread is already trying to stop this thread. This 'slave' thread
13950Sstevel@tonic-gate * does the following:
13960Sstevel@tonic-gate *
13970Sstevel@tonic-gate * - Sets TP_WATCHSTOP on the current thread
13980Sstevel@tonic-gate * - Waits for SWATCHOK flag to be set
13990Sstevel@tonic-gate * - Calls stop()
14000Sstevel@tonic-gate *
14010Sstevel@tonic-gate * o If SWATCHOK is set on the process, then this function immediately
14020Sstevel@tonic-gate * returns, as we must have been called via stop().
14030Sstevel@tonic-gate *
14040Sstevel@tonic-gate * In addition, there are other flags that take precedence over SHOLDWATCH:
14050Sstevel@tonic-gate *
14060Sstevel@tonic-gate * o If SEXITLWPS is set, exit immediately.
14070Sstevel@tonic-gate *
14080Sstevel@tonic-gate * o If SHOLDFORK1 is set, wait for fork1() to complete.
14090Sstevel@tonic-gate *
14100Sstevel@tonic-gate * o If SHOLDFORK is set, then watchpoint activity takes precedence In this
14110Sstevel@tonic-gate * case, set SHOLDWATCH, signalling the forking thread to stop first.
14120Sstevel@tonic-gate *
14130Sstevel@tonic-gate * o If the process is being stopped via /proc (TP_PRSTOP is set), then we
14140Sstevel@tonic-gate * stop the current thread.
14150Sstevel@tonic-gate *
14160Sstevel@tonic-gate * Returns 0 if all threads have been quiesced. Returns non-zero if not all
14170Sstevel@tonic-gate * threads were stopped, or the list of watched pages has changed.
14180Sstevel@tonic-gate */
14190Sstevel@tonic-gate int
holdwatch(void)14200Sstevel@tonic-gate holdwatch(void)
14210Sstevel@tonic-gate {
14220Sstevel@tonic-gate proc_t *p = curproc;
14230Sstevel@tonic-gate kthread_t *t = curthread;
14240Sstevel@tonic-gate int ret = 0;
14250Sstevel@tonic-gate
14260Sstevel@tonic-gate mutex_enter(&p->p_lock);
14270Sstevel@tonic-gate
14280Sstevel@tonic-gate p->p_lwprcnt--;
14290Sstevel@tonic-gate
14300Sstevel@tonic-gate /*
14310Sstevel@tonic-gate * Check for bail-out conditions as outlined above.
14320Sstevel@tonic-gate */
14330Sstevel@tonic-gate if (holdcheck(0) != 0) {
14340Sstevel@tonic-gate mutex_exit(&p->p_lock);
14350Sstevel@tonic-gate return (-1);
14360Sstevel@tonic-gate }
14370Sstevel@tonic-gate
14380Sstevel@tonic-gate if (!(p->p_flag & SHOLDWATCH)) {
14390Sstevel@tonic-gate /*
14400Sstevel@tonic-gate * We are the master watchpoint thread. Set SHOLDWATCH and poke
14410Sstevel@tonic-gate * the other threads.
14420Sstevel@tonic-gate */
14430Sstevel@tonic-gate p->p_flag |= SHOLDWATCH;
14440Sstevel@tonic-gate pokelwps(p);
14450Sstevel@tonic-gate
14460Sstevel@tonic-gate /*
14470Sstevel@tonic-gate * Wait for all threads to be stopped or have TP_WATCHSTOP set.
14480Sstevel@tonic-gate */
14490Sstevel@tonic-gate while (pr_allstopped(p, 1) > 0) {
14500Sstevel@tonic-gate if (holdcheck(SHOLDWATCH) != 0) {
14510Sstevel@tonic-gate p->p_flag &= ~SHOLDWATCH;
14520Sstevel@tonic-gate mutex_exit(&p->p_lock);
14530Sstevel@tonic-gate return (-1);
14540Sstevel@tonic-gate }
14550Sstevel@tonic-gate
14560Sstevel@tonic-gate cv_wait(&p->p_holdlwps, &p->p_lock);
14570Sstevel@tonic-gate }
14580Sstevel@tonic-gate
14590Sstevel@tonic-gate /*
14600Sstevel@tonic-gate * All threads are now stopped or in the process of stopping.
14610Sstevel@tonic-gate * Set SWATCHOK and let them stop completely.
14620Sstevel@tonic-gate */
14630Sstevel@tonic-gate p->p_flag |= SWATCHOK;
14640Sstevel@tonic-gate t->t_proc_flag &= ~TP_WATCHSTOP;
14650Sstevel@tonic-gate cv_broadcast(&p->p_holdlwps);
14660Sstevel@tonic-gate
14670Sstevel@tonic-gate while (pr_allstopped(p, 0) > 0) {
14680Sstevel@tonic-gate /*
14690Sstevel@tonic-gate * At first glance, it may appear that we don't need a
14700Sstevel@tonic-gate * call to holdcheck() here. But if the process gets a
14710Sstevel@tonic-gate * SIGKILL signal, one of our stopped threads may have
14720Sstevel@tonic-gate * been awakened and is waiting in exitlwps(), which
14730Sstevel@tonic-gate * takes precedence over watchpoints.
14740Sstevel@tonic-gate */
14750Sstevel@tonic-gate if (holdcheck(SHOLDWATCH | SWATCHOK) != 0) {
14760Sstevel@tonic-gate p->p_flag &= ~(SHOLDWATCH | SWATCHOK);
14770Sstevel@tonic-gate mutex_exit(&p->p_lock);
14780Sstevel@tonic-gate return (-1);
14790Sstevel@tonic-gate }
14800Sstevel@tonic-gate
14810Sstevel@tonic-gate cv_wait(&p->p_holdlwps, &p->p_lock);
14820Sstevel@tonic-gate }
14830Sstevel@tonic-gate
14840Sstevel@tonic-gate /*
14850Sstevel@tonic-gate * All threads are now completely stopped.
14860Sstevel@tonic-gate */
14870Sstevel@tonic-gate p->p_flag &= ~SWATCHOK;
14880Sstevel@tonic-gate p->p_flag &= ~SHOLDWATCH;
14890Sstevel@tonic-gate p->p_lwprcnt++;
14900Sstevel@tonic-gate
14910Sstevel@tonic-gate } else if (!(p->p_flag & SWATCHOK)) {
14920Sstevel@tonic-gate
14930Sstevel@tonic-gate /*
14940Sstevel@tonic-gate * SHOLDWATCH is set, so another thread is trying to do
14950Sstevel@tonic-gate * watchpoint activity. Indicate this thread is stopping, and
14960Sstevel@tonic-gate * wait for the OK from the master thread.
14970Sstevel@tonic-gate */
14980Sstevel@tonic-gate t->t_proc_flag |= TP_WATCHSTOP;
14990Sstevel@tonic-gate cv_broadcast(&p->p_holdlwps);
15000Sstevel@tonic-gate
15010Sstevel@tonic-gate while (!(p->p_flag & SWATCHOK)) {
15020Sstevel@tonic-gate if (holdcheck(0) != 0) {
15030Sstevel@tonic-gate t->t_proc_flag &= ~TP_WATCHSTOP;
15040Sstevel@tonic-gate mutex_exit(&p->p_lock);
15050Sstevel@tonic-gate return (-1);
15060Sstevel@tonic-gate }
15070Sstevel@tonic-gate
15080Sstevel@tonic-gate cv_wait(&p->p_holdlwps, &p->p_lock);
15090Sstevel@tonic-gate }
15100Sstevel@tonic-gate
15110Sstevel@tonic-gate /*
15120Sstevel@tonic-gate * Once the master thread has given the OK, this thread can
15130Sstevel@tonic-gate * actually call stop().
15140Sstevel@tonic-gate */
15150Sstevel@tonic-gate t->t_proc_flag &= ~TP_WATCHSTOP;
15160Sstevel@tonic-gate p->p_lwprcnt++;
15170Sstevel@tonic-gate
15180Sstevel@tonic-gate stop(PR_SUSPENDED, SUSPEND_NORMAL);
15190Sstevel@tonic-gate
15200Sstevel@tonic-gate /*
15210Sstevel@tonic-gate * It's not OK to do watchpoint activity, notify caller to
15220Sstevel@tonic-gate * retry.
15230Sstevel@tonic-gate */
15240Sstevel@tonic-gate ret = -1;
15250Sstevel@tonic-gate
15260Sstevel@tonic-gate } else {
15270Sstevel@tonic-gate
15280Sstevel@tonic-gate /*
15290Sstevel@tonic-gate * The only way we can hit the case where SHOLDWATCH is set and
15300Sstevel@tonic-gate * SWATCHOK is set is if we are triggering this from within a
15310Sstevel@tonic-gate * stop() call. Assert that this is the case.
15320Sstevel@tonic-gate */
15330Sstevel@tonic-gate
15340Sstevel@tonic-gate ASSERT(t->t_proc_flag & TP_STOPPING);
15350Sstevel@tonic-gate p->p_lwprcnt++;
15360Sstevel@tonic-gate }
15370Sstevel@tonic-gate
15380Sstevel@tonic-gate mutex_exit(&p->p_lock);
15390Sstevel@tonic-gate
15400Sstevel@tonic-gate return (ret);
15410Sstevel@tonic-gate }
15420Sstevel@tonic-gate
15430Sstevel@tonic-gate /*
15440Sstevel@tonic-gate * force all interruptible lwps to trap into the kernel.
15450Sstevel@tonic-gate */
15460Sstevel@tonic-gate void
pokelwps(proc_t * p)15470Sstevel@tonic-gate pokelwps(proc_t *p)
15480Sstevel@tonic-gate {
15490Sstevel@tonic-gate kthread_t *t;
15500Sstevel@tonic-gate
15510Sstevel@tonic-gate ASSERT(MUTEX_HELD(&p->p_lock));
15520Sstevel@tonic-gate
15530Sstevel@tonic-gate t = p->p_tlist;
15540Sstevel@tonic-gate do {
15550Sstevel@tonic-gate if (t == curthread)
15560Sstevel@tonic-gate continue;
15570Sstevel@tonic-gate thread_lock(t);
15580Sstevel@tonic-gate aston(t); /* make thread trap or do post_syscall */
15593792Sakolb if (ISWAKEABLE(t) || ISWAITING(t)) {
15603792Sakolb setrun_locked(t);
15610Sstevel@tonic-gate } else if (t->t_state == TS_STOPPED) {
15620Sstevel@tonic-gate /*
15630Sstevel@tonic-gate * Ensure that proc_exit() is not blocked by lwps
15640Sstevel@tonic-gate * that were stopped via jobcontrol or /proc.
15650Sstevel@tonic-gate */
15660Sstevel@tonic-gate if (p->p_flag & SEXITLWPS) {
15670Sstevel@tonic-gate p->p_stopsig = 0;
15680Sstevel@tonic-gate t->t_schedflag |= (TS_XSTART | TS_PSTART);
15690Sstevel@tonic-gate setrun_locked(t);
15700Sstevel@tonic-gate }
15710Sstevel@tonic-gate /*
15720Sstevel@tonic-gate * If we are holding lwps for a forkall(),
15730Sstevel@tonic-gate * force lwps that have been suspended via
15740Sstevel@tonic-gate * lwp_suspend() and are suspended inside
15750Sstevel@tonic-gate * of a system call to proceed to their
15760Sstevel@tonic-gate * holdlwp() points where they are clonable.
15770Sstevel@tonic-gate */
15780Sstevel@tonic-gate if ((p->p_flag & SHOLDFORK) && SUSPENDED(t)) {
15790Sstevel@tonic-gate if ((t->t_schedflag & TS_CSTART) == 0) {
15800Sstevel@tonic-gate p->p_lwprcnt++;
15810Sstevel@tonic-gate t->t_schedflag |= TS_CSTART;
15820Sstevel@tonic-gate setrun_locked(t);
15830Sstevel@tonic-gate }
15840Sstevel@tonic-gate }
15850Sstevel@tonic-gate } else if (t->t_state == TS_ONPROC) {
15860Sstevel@tonic-gate if (t->t_cpu != CPU)
15870Sstevel@tonic-gate poke_cpu(t->t_cpu->cpu_id);
15880Sstevel@tonic-gate }
15890Sstevel@tonic-gate thread_unlock(t);
15900Sstevel@tonic-gate } while ((t = t->t_forw) != p->p_tlist);
15910Sstevel@tonic-gate }
15920Sstevel@tonic-gate
15930Sstevel@tonic-gate /*
15940Sstevel@tonic-gate * undo the effects of holdlwps() or holdwatch().
15950Sstevel@tonic-gate */
15960Sstevel@tonic-gate void
continuelwps(proc_t * p)15970Sstevel@tonic-gate continuelwps(proc_t *p)
15980Sstevel@tonic-gate {
15990Sstevel@tonic-gate kthread_t *t;
16000Sstevel@tonic-gate
16010Sstevel@tonic-gate /*
16020Sstevel@tonic-gate * If this flag is set, then the original holdwatch() didn't actually
16030Sstevel@tonic-gate * stop the process. See comments for holdwatch().
16040Sstevel@tonic-gate */
16050Sstevel@tonic-gate if (p->p_flag & SWATCHOK) {
16060Sstevel@tonic-gate ASSERT(curthread->t_proc_flag & TP_STOPPING);
16070Sstevel@tonic-gate return;
16080Sstevel@tonic-gate }
16090Sstevel@tonic-gate
16100Sstevel@tonic-gate ASSERT(MUTEX_HELD(&p->p_lock));
16110Sstevel@tonic-gate ASSERT((p->p_flag & (SHOLDFORK | SHOLDFORK1 | SHOLDWATCH)) == 0);
16120Sstevel@tonic-gate
16130Sstevel@tonic-gate t = p->p_tlist;
16140Sstevel@tonic-gate do {
16150Sstevel@tonic-gate thread_lock(t); /* SUSPENDED looks at t_schedflag */
16160Sstevel@tonic-gate if (SUSPENDED(t) && !(t->t_proc_flag & TP_HOLDLWP)) {
16170Sstevel@tonic-gate p->p_lwprcnt++;
16180Sstevel@tonic-gate t->t_schedflag |= TS_CSTART;
16190Sstevel@tonic-gate setrun_locked(t);
16200Sstevel@tonic-gate }
16210Sstevel@tonic-gate thread_unlock(t);
16220Sstevel@tonic-gate } while ((t = t->t_forw) != p->p_tlist);
16230Sstevel@tonic-gate }
16240Sstevel@tonic-gate
16250Sstevel@tonic-gate /*
16260Sstevel@tonic-gate * Force all other LWPs in the current process other than the caller to exit,
16270Sstevel@tonic-gate * and then cv_wait() on p_holdlwps for them to exit. The exitlwps() function
16280Sstevel@tonic-gate * is typically used in these situations:
16290Sstevel@tonic-gate *
16300Sstevel@tonic-gate * (a) prior to an exec() system call
16310Sstevel@tonic-gate * (b) prior to dumping a core file
16320Sstevel@tonic-gate * (c) prior to a uadmin() shutdown
16330Sstevel@tonic-gate *
16340Sstevel@tonic-gate * If the 'coredump' flag is set, other LWPs are quiesced but not destroyed.
16350Sstevel@tonic-gate * Multiple threads in the process can call this function at one time by
16360Sstevel@tonic-gate * triggering execs or core dumps simultaneously, so the SEXITLWPS bit is used
16370Sstevel@tonic-gate * to declare one particular thread the winner who gets to kill the others.
16380Sstevel@tonic-gate * If a thread wins the exitlwps() dance, zero is returned; otherwise an
16390Sstevel@tonic-gate * appropriate errno value is returned to caller for its system call to return.
16400Sstevel@tonic-gate */
16410Sstevel@tonic-gate int
exitlwps(int coredump)16420Sstevel@tonic-gate exitlwps(int coredump)
16430Sstevel@tonic-gate {
16440Sstevel@tonic-gate proc_t *p = curproc;
16450Sstevel@tonic-gate int heldcnt;
16460Sstevel@tonic-gate
16470Sstevel@tonic-gate if (curthread->t_door)
16480Sstevel@tonic-gate door_slam();
16490Sstevel@tonic-gate if (p->p_door_list)
16500Sstevel@tonic-gate door_revoke_all();
16510Sstevel@tonic-gate if (curthread->t_schedctl != NULL)
16520Sstevel@tonic-gate schedctl_lwp_cleanup(curthread);
16530Sstevel@tonic-gate
16540Sstevel@tonic-gate /*
16550Sstevel@tonic-gate * Ensure that before starting to wait for other lwps to exit,
16560Sstevel@tonic-gate * cleanup all upimutexes held by curthread. Otherwise, some other
16570Sstevel@tonic-gate * lwp could be waiting (uninterruptibly) for a upimutex held by
16580Sstevel@tonic-gate * curthread, and the call to pokelwps() below would deadlock.
16590Sstevel@tonic-gate * Even if a blocked upimutex_lock is made interruptible,
16600Sstevel@tonic-gate * curthread's upimutexes need to be unlocked: do it here.
16610Sstevel@tonic-gate */
16620Sstevel@tonic-gate if (curthread->t_upimutex != NULL)
16630Sstevel@tonic-gate upimutex_cleanup();
16640Sstevel@tonic-gate
16650Sstevel@tonic-gate /*
16660Sstevel@tonic-gate * Grab p_lock in order to check and set SEXITLWPS to declare a winner.
16670Sstevel@tonic-gate * We must also block any further /proc access from this point forward.
16680Sstevel@tonic-gate */
16690Sstevel@tonic-gate mutex_enter(&p->p_lock);
16700Sstevel@tonic-gate prbarrier(p);
16710Sstevel@tonic-gate
16720Sstevel@tonic-gate if (p->p_flag & SEXITLWPS) {
16730Sstevel@tonic-gate mutex_exit(&p->p_lock);
16740Sstevel@tonic-gate aston(curthread); /* force a trip through post_syscall */
16750Sstevel@tonic-gate return (set_errno(EINTR));
16760Sstevel@tonic-gate }
16770Sstevel@tonic-gate
16780Sstevel@tonic-gate p->p_flag |= SEXITLWPS;
16790Sstevel@tonic-gate if (coredump) /* tell other lwps to stop, not exit */
16800Sstevel@tonic-gate p->p_flag |= SCOREDUMP;
16810Sstevel@tonic-gate
16820Sstevel@tonic-gate /*
16830Sstevel@tonic-gate * Give precedence to exitlwps() if a holdlwps() is
16840Sstevel@tonic-gate * in progress. The lwp doing the holdlwps() operation
16850Sstevel@tonic-gate * is aborted when it is awakened.
16860Sstevel@tonic-gate */
16870Sstevel@tonic-gate while (p->p_flag & (SHOLDFORK | SHOLDFORK1 | SHOLDWATCH)) {
16880Sstevel@tonic-gate cv_broadcast(&p->p_holdlwps);
16890Sstevel@tonic-gate cv_wait(&p->p_holdlwps, &p->p_lock);
1690390Sraf prbarrier(p);
16910Sstevel@tonic-gate }
16920Sstevel@tonic-gate p->p_flag |= SHOLDFORK;
16930Sstevel@tonic-gate pokelwps(p);
16940Sstevel@tonic-gate
16950Sstevel@tonic-gate /*
16960Sstevel@tonic-gate * Wait for process to become quiescent.
16970Sstevel@tonic-gate */
16980Sstevel@tonic-gate --p->p_lwprcnt;
1699390Sraf while (p->p_lwprcnt > 0) {
17000Sstevel@tonic-gate cv_wait(&p->p_holdlwps, &p->p_lock);
1701390Sraf prbarrier(p);
1702390Sraf }
17030Sstevel@tonic-gate p->p_lwprcnt++;
17040Sstevel@tonic-gate ASSERT(p->p_lwprcnt == 1);
17050Sstevel@tonic-gate
17060Sstevel@tonic-gate /*
17070Sstevel@tonic-gate * The SCOREDUMP flag puts the process into a quiescent
17080Sstevel@tonic-gate * state. The process's lwps remain attached to this
17090Sstevel@tonic-gate * process until exitlwps() is called again without the
17100Sstevel@tonic-gate * 'coredump' flag set, then the lwps are terminated
17110Sstevel@tonic-gate * and the process can exit.
17120Sstevel@tonic-gate */
17130Sstevel@tonic-gate if (coredump) {
17140Sstevel@tonic-gate p->p_flag &= ~(SCOREDUMP | SHOLDFORK | SEXITLWPS);
17150Sstevel@tonic-gate goto out;
17160Sstevel@tonic-gate }
17170Sstevel@tonic-gate
17180Sstevel@tonic-gate /*
17190Sstevel@tonic-gate * Determine if there are any lwps left dangling in
17200Sstevel@tonic-gate * the stopped state. This happens when exitlwps()
17210Sstevel@tonic-gate * aborts a holdlwps() operation.
17220Sstevel@tonic-gate */
17230Sstevel@tonic-gate p->p_flag &= ~SHOLDFORK;
17240Sstevel@tonic-gate if ((heldcnt = p->p_lwpcnt) > 1) {
17250Sstevel@tonic-gate kthread_t *t;
17260Sstevel@tonic-gate for (t = curthread->t_forw; --heldcnt > 0; t = t->t_forw) {
17270Sstevel@tonic-gate t->t_proc_flag &= ~TP_TWAIT;
17280Sstevel@tonic-gate lwp_continue(t);
17290Sstevel@tonic-gate }
17300Sstevel@tonic-gate }
17310Sstevel@tonic-gate
17320Sstevel@tonic-gate /*
17330Sstevel@tonic-gate * Wait for all other lwps to exit.
17340Sstevel@tonic-gate */
17350Sstevel@tonic-gate --p->p_lwprcnt;
1736390Sraf while (p->p_lwpcnt > 1) {
17370Sstevel@tonic-gate cv_wait(&p->p_holdlwps, &p->p_lock);
1738390Sraf prbarrier(p);
1739390Sraf }
17400Sstevel@tonic-gate ++p->p_lwprcnt;
17410Sstevel@tonic-gate ASSERT(p->p_lwpcnt == 1 && p->p_lwprcnt == 1);
17420Sstevel@tonic-gate
17430Sstevel@tonic-gate p->p_flag &= ~SEXITLWPS;
17440Sstevel@tonic-gate curthread->t_proc_flag &= ~TP_TWAIT;
17450Sstevel@tonic-gate
17460Sstevel@tonic-gate out:
17470Sstevel@tonic-gate if (!coredump && p->p_zombcnt) { /* cleanup the zombie lwps */
17480Sstevel@tonic-gate lwpdir_t *ldp;
17490Sstevel@tonic-gate lwpent_t *lep;
17500Sstevel@tonic-gate int i;
17510Sstevel@tonic-gate
17520Sstevel@tonic-gate for (ldp = p->p_lwpdir, i = 0; i < p->p_lwpdir_sz; i++, ldp++) {
17530Sstevel@tonic-gate lep = ldp->ld_entry;
17540Sstevel@tonic-gate if (lep != NULL && lep->le_thread != curthread) {
17550Sstevel@tonic-gate ASSERT(lep->le_thread == NULL);
17560Sstevel@tonic-gate p->p_zombcnt--;
17570Sstevel@tonic-gate lwp_hash_out(p, lep->le_lwpid);
17580Sstevel@tonic-gate }
17590Sstevel@tonic-gate }
17600Sstevel@tonic-gate ASSERT(p->p_zombcnt == 0);
17610Sstevel@tonic-gate }
17620Sstevel@tonic-gate
17630Sstevel@tonic-gate /*
17640Sstevel@tonic-gate * If some other LWP in the process wanted us to suspend ourself,
17650Sstevel@tonic-gate * then we will not do it. The other LWP is now terminated and
17660Sstevel@tonic-gate * no one will ever continue us again if we suspend ourself.
17670Sstevel@tonic-gate */
17680Sstevel@tonic-gate curthread->t_proc_flag &= ~TP_HOLDLWP;
17690Sstevel@tonic-gate p->p_flag &= ~(SHOLDFORK | SHOLDFORK1 | SHOLDWATCH | SLWPWRAP);
17700Sstevel@tonic-gate mutex_exit(&p->p_lock);
17710Sstevel@tonic-gate return (0);
17720Sstevel@tonic-gate }
17730Sstevel@tonic-gate
17740Sstevel@tonic-gate /*
17750Sstevel@tonic-gate * duplicate a lwp.
17760Sstevel@tonic-gate */
17770Sstevel@tonic-gate klwp_t *
forklwp(klwp_t * lwp,proc_t * cp,id_t lwpid)17780Sstevel@tonic-gate forklwp(klwp_t *lwp, proc_t *cp, id_t lwpid)
17790Sstevel@tonic-gate {
17800Sstevel@tonic-gate klwp_t *clwp;
17810Sstevel@tonic-gate void *tregs, *tfpu;
17820Sstevel@tonic-gate kthread_t *t = lwptot(lwp);
17830Sstevel@tonic-gate kthread_t *ct;
17840Sstevel@tonic-gate proc_t *p = lwptoproc(lwp);
17850Sstevel@tonic-gate int cid;
17860Sstevel@tonic-gate void *bufp;
17872712Snn35248 void *brand_data;
17880Sstevel@tonic-gate int val;
17890Sstevel@tonic-gate
17900Sstevel@tonic-gate ASSERT(p == curproc);
17910Sstevel@tonic-gate ASSERT(t == curthread || (SUSPENDED(t) && lwp->lwp_asleep == 0));
17920Sstevel@tonic-gate
17930Sstevel@tonic-gate #if defined(__sparc)
17940Sstevel@tonic-gate if (t == curthread)
17950Sstevel@tonic-gate (void) flush_user_windows_to_stack(NULL);
17960Sstevel@tonic-gate #endif
17970Sstevel@tonic-gate
17980Sstevel@tonic-gate if (t == curthread)
17990Sstevel@tonic-gate /* copy args out of registers first */
18000Sstevel@tonic-gate (void) save_syscall_args();
18012712Snn35248
18020Sstevel@tonic-gate clwp = lwp_create(cp->p_lwpcnt == 0 ? lwp_rtt_initial : lwp_rtt,
18030Sstevel@tonic-gate NULL, 0, cp, TS_STOPPED, t->t_pri, &t->t_hold, NOCLASS, lwpid);
18040Sstevel@tonic-gate if (clwp == NULL)
18050Sstevel@tonic-gate return (NULL);
18060Sstevel@tonic-gate
18070Sstevel@tonic-gate /*
18080Sstevel@tonic-gate * most of the parent's lwp can be copied to its duplicate,
18090Sstevel@tonic-gate * except for the fields that are unique to each lwp, like
18100Sstevel@tonic-gate * lwp_thread, lwp_procp, lwp_regs, and lwp_ap.
18110Sstevel@tonic-gate */
18120Sstevel@tonic-gate ct = clwp->lwp_thread;
18130Sstevel@tonic-gate tregs = clwp->lwp_regs;
18140Sstevel@tonic-gate tfpu = clwp->lwp_fpu;
18152712Snn35248 brand_data = clwp->lwp_brand;
18160Sstevel@tonic-gate
18173551Sjohansen /*
18183551Sjohansen * Copy parent lwp to child lwp. Hold child's p_lock to prevent
18193551Sjohansen * mstate_aggr_state() from reading stale mstate entries copied
18203551Sjohansen * from lwp to clwp.
18213551Sjohansen */
18223551Sjohansen mutex_enter(&cp->p_lock);
18230Sstevel@tonic-gate *clwp = *lwp;
18240Sstevel@tonic-gate
18253551Sjohansen /* clear microstate and resource usage data in new lwp */
18263551Sjohansen init_mstate(ct, LMS_STOPPED);
18273551Sjohansen bzero(&clwp->lwp_ru, sizeof (clwp->lwp_ru));
18283551Sjohansen mutex_exit(&cp->p_lock);
18293551Sjohansen
18300Sstevel@tonic-gate /* fix up child's lwp */
18310Sstevel@tonic-gate
18324503Ssudheer clwp->lwp_pcb.pcb_flags = 0;
18334503Ssudheer #if defined(__sparc)
18340Sstevel@tonic-gate clwp->lwp_pcb.pcb_step = STEP_NONE;
18350Sstevel@tonic-gate #endif
18360Sstevel@tonic-gate clwp->lwp_cursig = 0;
18370Sstevel@tonic-gate clwp->lwp_extsig = 0;
18380Sstevel@tonic-gate clwp->lwp_curinfo = (struct sigqueue *)0;
18390Sstevel@tonic-gate clwp->lwp_thread = ct;
18400Sstevel@tonic-gate ct->t_sysnum = t->t_sysnum;
18410Sstevel@tonic-gate clwp->lwp_regs = tregs;
18420Sstevel@tonic-gate clwp->lwp_fpu = tfpu;
18432712Snn35248 clwp->lwp_brand = brand_data;
18440Sstevel@tonic-gate clwp->lwp_ap = clwp->lwp_arg;
18450Sstevel@tonic-gate clwp->lwp_procp = cp;
18460Sstevel@tonic-gate bzero(clwp->lwp_timer, sizeof (clwp->lwp_timer));
18470Sstevel@tonic-gate clwp->lwp_lastfault = 0;
18480Sstevel@tonic-gate clwp->lwp_lastfaddr = 0;
18490Sstevel@tonic-gate
18500Sstevel@tonic-gate /* copy parent's struct regs to child. */
18510Sstevel@tonic-gate lwp_forkregs(lwp, clwp);
18520Sstevel@tonic-gate
18530Sstevel@tonic-gate /*
18541217Srab * Fork thread context ops, if any.
18550Sstevel@tonic-gate */
18560Sstevel@tonic-gate if (t->t_ctx)
18570Sstevel@tonic-gate forkctx(t, ct);
18580Sstevel@tonic-gate
18590Sstevel@tonic-gate /* fix door state in the child */
18600Sstevel@tonic-gate if (t->t_door)
18610Sstevel@tonic-gate door_fork(t, ct);
18620Sstevel@tonic-gate
18630Sstevel@tonic-gate /* copy current contract templates, clear latest contracts */
18640Sstevel@tonic-gate lwp_ctmpl_copy(clwp, lwp);
18650Sstevel@tonic-gate
18660Sstevel@tonic-gate mutex_enter(&cp->p_lock);
18670Sstevel@tonic-gate /* lwp_create() set the TP_HOLDLWP flag */
18680Sstevel@tonic-gate if (!(t->t_proc_flag & TP_HOLDLWP))
18690Sstevel@tonic-gate ct->t_proc_flag &= ~TP_HOLDLWP;
18700Sstevel@tonic-gate if (cp->p_flag & SMSACCT)
18710Sstevel@tonic-gate ct->t_proc_flag |= TP_MSACCT;
18720Sstevel@tonic-gate mutex_exit(&cp->p_lock);
18730Sstevel@tonic-gate
18742712Snn35248 /* Allow brand to propagate brand-specific state */
18752712Snn35248 if (PROC_IS_BRANDED(p))
18762712Snn35248 BROP(p)->b_forklwp(lwp, clwp);
18772712Snn35248
18780Sstevel@tonic-gate retry:
18790Sstevel@tonic-gate cid = t->t_cid;
18800Sstevel@tonic-gate
18810Sstevel@tonic-gate val = CL_ALLOC(&bufp, cid, KM_SLEEP);
18820Sstevel@tonic-gate ASSERT(val == 0);
18830Sstevel@tonic-gate
18840Sstevel@tonic-gate mutex_enter(&p->p_lock);
18850Sstevel@tonic-gate if (cid != t->t_cid) {
18860Sstevel@tonic-gate /*
18870Sstevel@tonic-gate * Someone just changed this thread's scheduling class,
18880Sstevel@tonic-gate * so try pre-allocating the buffer again. Hopefully we
18890Sstevel@tonic-gate * don't hit this often.
18900Sstevel@tonic-gate */
18910Sstevel@tonic-gate mutex_exit(&p->p_lock);
18920Sstevel@tonic-gate CL_FREE(cid, bufp);
18930Sstevel@tonic-gate goto retry;
18940Sstevel@tonic-gate }
18950Sstevel@tonic-gate
18960Sstevel@tonic-gate ct->t_unpark = t->t_unpark;
18970Sstevel@tonic-gate ct->t_clfuncs = t->t_clfuncs;
18980Sstevel@tonic-gate CL_FORK(t, ct, bufp);
18990Sstevel@tonic-gate ct->t_cid = t->t_cid; /* after data allocated so prgetpsinfo works */
19000Sstevel@tonic-gate mutex_exit(&p->p_lock);
19010Sstevel@tonic-gate
19020Sstevel@tonic-gate return (clwp);
19030Sstevel@tonic-gate }
19040Sstevel@tonic-gate
19050Sstevel@tonic-gate /*
19060Sstevel@tonic-gate * Add a new lwp entry to the lwp directory and to the lwpid hash table.
19070Sstevel@tonic-gate */
19080Sstevel@tonic-gate void
lwp_hash_in(proc_t * p,lwpent_t * lep,tidhash_t * tidhash,uint_t tidhash_sz,int do_lock)19099393SRoger.Faulkner@Sun.COM lwp_hash_in(proc_t *p, lwpent_t *lep, tidhash_t *tidhash, uint_t tidhash_sz,
19109393SRoger.Faulkner@Sun.COM int do_lock)
19110Sstevel@tonic-gate {
19129393SRoger.Faulkner@Sun.COM tidhash_t *thp = &tidhash[TIDHASH(lep->le_lwpid, tidhash_sz)];
19130Sstevel@tonic-gate lwpdir_t **ldpp;
19140Sstevel@tonic-gate lwpdir_t *ldp;
19150Sstevel@tonic-gate kthread_t *t;
19160Sstevel@tonic-gate
19170Sstevel@tonic-gate /*
19180Sstevel@tonic-gate * Allocate a directory element from the free list.
19190Sstevel@tonic-gate * Code elsewhere guarantees a free slot.
19200Sstevel@tonic-gate */
19210Sstevel@tonic-gate ldp = p->p_lwpfree;
19220Sstevel@tonic-gate p->p_lwpfree = ldp->ld_next;
19230Sstevel@tonic-gate ASSERT(ldp->ld_entry == NULL);
19240Sstevel@tonic-gate ldp->ld_entry = lep;
19250Sstevel@tonic-gate
19269393SRoger.Faulkner@Sun.COM if (do_lock)
19279393SRoger.Faulkner@Sun.COM mutex_enter(&thp->th_lock);
19289393SRoger.Faulkner@Sun.COM
19290Sstevel@tonic-gate /*
19300Sstevel@tonic-gate * Insert it into the lwpid hash table.
19310Sstevel@tonic-gate */
19329393SRoger.Faulkner@Sun.COM ldpp = &thp->th_list;
19330Sstevel@tonic-gate ldp->ld_next = *ldpp;
19340Sstevel@tonic-gate *ldpp = ldp;
19350Sstevel@tonic-gate
19360Sstevel@tonic-gate /*
19370Sstevel@tonic-gate * Set the active thread's directory slot entry.
19380Sstevel@tonic-gate */
19390Sstevel@tonic-gate if ((t = lep->le_thread) != NULL) {
19400Sstevel@tonic-gate ASSERT(lep->le_lwpid == t->t_tid);
19410Sstevel@tonic-gate t->t_dslot = (int)(ldp - p->p_lwpdir);
19420Sstevel@tonic-gate }
19439393SRoger.Faulkner@Sun.COM
19449393SRoger.Faulkner@Sun.COM if (do_lock)
19459393SRoger.Faulkner@Sun.COM mutex_exit(&thp->th_lock);
19460Sstevel@tonic-gate }
19470Sstevel@tonic-gate
19480Sstevel@tonic-gate /*
19490Sstevel@tonic-gate * Remove an lwp from the lwpid hash table and free its directory entry.
19500Sstevel@tonic-gate * This is done when a detached lwp exits in lwp_exit() or
19510Sstevel@tonic-gate * when a non-detached lwp is waited for in lwp_wait() or
19520Sstevel@tonic-gate * when a zombie lwp is detached in lwp_detach().
19530Sstevel@tonic-gate */
19540Sstevel@tonic-gate void
lwp_hash_out(proc_t * p,id_t lwpid)19550Sstevel@tonic-gate lwp_hash_out(proc_t *p, id_t lwpid)
19560Sstevel@tonic-gate {
19579393SRoger.Faulkner@Sun.COM tidhash_t *thp = &p->p_tidhash[TIDHASH(lwpid, p->p_tidhash_sz)];
19580Sstevel@tonic-gate lwpdir_t **ldpp;
19590Sstevel@tonic-gate lwpdir_t *ldp;
19600Sstevel@tonic-gate lwpent_t *lep;
19610Sstevel@tonic-gate
19629393SRoger.Faulkner@Sun.COM mutex_enter(&thp->th_lock);
19639393SRoger.Faulkner@Sun.COM for (ldpp = &thp->th_list;
19640Sstevel@tonic-gate (ldp = *ldpp) != NULL; ldpp = &ldp->ld_next) {
19650Sstevel@tonic-gate lep = ldp->ld_entry;
19660Sstevel@tonic-gate if (lep->le_lwpid == lwpid) {
19670Sstevel@tonic-gate prlwpfree(p, lep); /* /proc deals with le_trace */
19680Sstevel@tonic-gate *ldpp = ldp->ld_next;
19690Sstevel@tonic-gate ldp->ld_entry = NULL;
19700Sstevel@tonic-gate ldp->ld_next = p->p_lwpfree;
19710Sstevel@tonic-gate p->p_lwpfree = ldp;
19720Sstevel@tonic-gate kmem_free(lep, sizeof (*lep));
19730Sstevel@tonic-gate break;
19740Sstevel@tonic-gate }
19750Sstevel@tonic-gate }
19769393SRoger.Faulkner@Sun.COM mutex_exit(&thp->th_lock);
19770Sstevel@tonic-gate }
19780Sstevel@tonic-gate
19790Sstevel@tonic-gate /*
19800Sstevel@tonic-gate * Lookup an lwp in the lwpid hash table by lwpid.
19810Sstevel@tonic-gate */
19820Sstevel@tonic-gate lwpdir_t *
lwp_hash_lookup(proc_t * p,id_t lwpid)19830Sstevel@tonic-gate lwp_hash_lookup(proc_t *p, id_t lwpid)
19840Sstevel@tonic-gate {
19859393SRoger.Faulkner@Sun.COM tidhash_t *thp;
19860Sstevel@tonic-gate lwpdir_t *ldp;
19870Sstevel@tonic-gate
19880Sstevel@tonic-gate /*
19890Sstevel@tonic-gate * The process may be exiting, after p_tidhash has been set to NULL in
19900Sstevel@tonic-gate * proc_exit() but before prfee() has been called. Return failure in
19910Sstevel@tonic-gate * this case.
19920Sstevel@tonic-gate */
19930Sstevel@tonic-gate if (p->p_tidhash == NULL)
19940Sstevel@tonic-gate return (NULL);
19950Sstevel@tonic-gate
19969393SRoger.Faulkner@Sun.COM thp = &p->p_tidhash[TIDHASH(lwpid, p->p_tidhash_sz)];
19979393SRoger.Faulkner@Sun.COM for (ldp = thp->th_list; ldp != NULL; ldp = ldp->ld_next) {
19980Sstevel@tonic-gate if (ldp->ld_entry->le_lwpid == lwpid)
19990Sstevel@tonic-gate return (ldp);
20000Sstevel@tonic-gate }
20010Sstevel@tonic-gate
20020Sstevel@tonic-gate return (NULL);
20030Sstevel@tonic-gate }
20040Sstevel@tonic-gate
20050Sstevel@tonic-gate /*
20069393SRoger.Faulkner@Sun.COM * Same as lwp_hash_lookup(), but acquire and return
20079393SRoger.Faulkner@Sun.COM * the tid hash table entry lock on success.
20089393SRoger.Faulkner@Sun.COM */
20099393SRoger.Faulkner@Sun.COM lwpdir_t *
lwp_hash_lookup_and_lock(proc_t * p,id_t lwpid,kmutex_t ** mpp)20109393SRoger.Faulkner@Sun.COM lwp_hash_lookup_and_lock(proc_t *p, id_t lwpid, kmutex_t **mpp)
20119393SRoger.Faulkner@Sun.COM {
20129393SRoger.Faulkner@Sun.COM tidhash_t *tidhash;
20139393SRoger.Faulkner@Sun.COM uint_t tidhash_sz;
20149393SRoger.Faulkner@Sun.COM tidhash_t *thp;
20159393SRoger.Faulkner@Sun.COM lwpdir_t *ldp;
20169393SRoger.Faulkner@Sun.COM
20179393SRoger.Faulkner@Sun.COM top:
20189393SRoger.Faulkner@Sun.COM tidhash_sz = p->p_tidhash_sz;
20199393SRoger.Faulkner@Sun.COM membar_consumer();
20209393SRoger.Faulkner@Sun.COM if ((tidhash = p->p_tidhash) == NULL)
20219393SRoger.Faulkner@Sun.COM return (NULL);
20229393SRoger.Faulkner@Sun.COM
20239393SRoger.Faulkner@Sun.COM thp = &tidhash[TIDHASH(lwpid, tidhash_sz)];
20249393SRoger.Faulkner@Sun.COM mutex_enter(&thp->th_lock);
20259393SRoger.Faulkner@Sun.COM
20269393SRoger.Faulkner@Sun.COM /*
20279393SRoger.Faulkner@Sun.COM * Since we are not holding p->p_lock, the tid hash table
20289393SRoger.Faulkner@Sun.COM * may have changed. If so, start over. If not, then
20299393SRoger.Faulkner@Sun.COM * it cannot change until after we drop &thp->th_lock;
20309393SRoger.Faulkner@Sun.COM */
20319393SRoger.Faulkner@Sun.COM if (tidhash != p->p_tidhash || tidhash_sz != p->p_tidhash_sz) {
20329393SRoger.Faulkner@Sun.COM mutex_exit(&thp->th_lock);
20339393SRoger.Faulkner@Sun.COM goto top;
20349393SRoger.Faulkner@Sun.COM }
20359393SRoger.Faulkner@Sun.COM
20369393SRoger.Faulkner@Sun.COM for (ldp = thp->th_list; ldp != NULL; ldp = ldp->ld_next) {
20379393SRoger.Faulkner@Sun.COM if (ldp->ld_entry->le_lwpid == lwpid) {
20389393SRoger.Faulkner@Sun.COM *mpp = &thp->th_lock;
20399393SRoger.Faulkner@Sun.COM return (ldp);
20409393SRoger.Faulkner@Sun.COM }
20419393SRoger.Faulkner@Sun.COM }
20429393SRoger.Faulkner@Sun.COM
20439393SRoger.Faulkner@Sun.COM mutex_exit(&thp->th_lock);
20449393SRoger.Faulkner@Sun.COM return (NULL);
20459393SRoger.Faulkner@Sun.COM }
20469393SRoger.Faulkner@Sun.COM
20479393SRoger.Faulkner@Sun.COM /*
20480Sstevel@tonic-gate * Update the indicated LWP usage statistic for the current LWP.
20490Sstevel@tonic-gate */
20500Sstevel@tonic-gate void
lwp_stat_update(lwp_stat_id_t lwp_stat_id,long inc)20510Sstevel@tonic-gate lwp_stat_update(lwp_stat_id_t lwp_stat_id, long inc)
20520Sstevel@tonic-gate {
20530Sstevel@tonic-gate klwp_t *lwp = ttolwp(curthread);
20540Sstevel@tonic-gate
20550Sstevel@tonic-gate if (lwp == NULL)
20560Sstevel@tonic-gate return;
20570Sstevel@tonic-gate
20580Sstevel@tonic-gate switch (lwp_stat_id) {
20590Sstevel@tonic-gate case LWP_STAT_INBLK:
20600Sstevel@tonic-gate lwp->lwp_ru.inblock += inc;
20610Sstevel@tonic-gate break;
20620Sstevel@tonic-gate case LWP_STAT_OUBLK:
20630Sstevel@tonic-gate lwp->lwp_ru.oublock += inc;
20640Sstevel@tonic-gate break;
20650Sstevel@tonic-gate case LWP_STAT_MSGRCV:
20660Sstevel@tonic-gate lwp->lwp_ru.msgrcv += inc;
20670Sstevel@tonic-gate break;
20680Sstevel@tonic-gate case LWP_STAT_MSGSND:
20690Sstevel@tonic-gate lwp->lwp_ru.msgsnd += inc;
20700Sstevel@tonic-gate break;
20710Sstevel@tonic-gate default:
20720Sstevel@tonic-gate panic("lwp_stat_update: invalid lwp_stat_id 0x%x", lwp_stat_id);
20730Sstevel@tonic-gate }
20740Sstevel@tonic-gate }
2075