17c478bd9Sstevel@tonic-gate /* 27c478bd9Sstevel@tonic-gate * CDDL HEADER START 37c478bd9Sstevel@tonic-gate * 47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 59acbbeafSnn35248 * Common Development and Distribution License (the "License"). 69acbbeafSnn35248 * You may not use this file except in compliance with the License. 77c478bd9Sstevel@tonic-gate * 87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 117c478bd9Sstevel@tonic-gate * and limitations under the License. 127c478bd9Sstevel@tonic-gate * 137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 187c478bd9Sstevel@tonic-gate * 197c478bd9Sstevel@tonic-gate * CDDL HEADER END 207c478bd9Sstevel@tonic-gate */ 2197eda132Sraf 227c478bd9Sstevel@tonic-gate /* 235d3ff519Sjohansen * Copyright 2007 Sun Microsystems, Inc. All rights reserved. 247c478bd9Sstevel@tonic-gate * Use is subject to license terms. 257c478bd9Sstevel@tonic-gate */ 267c478bd9Sstevel@tonic-gate 277c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 287c478bd9Sstevel@tonic-gate 297c478bd9Sstevel@tonic-gate #include <sys/param.h> 307c478bd9Sstevel@tonic-gate #include <sys/types.h> 317c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h> 327c478bd9Sstevel@tonic-gate #include <sys/systm.h> 337c478bd9Sstevel@tonic-gate #include <sys/thread.h> 347c478bd9Sstevel@tonic-gate #include <sys/proc.h> 357c478bd9Sstevel@tonic-gate #include <sys/task.h> 367c478bd9Sstevel@tonic-gate #include <sys/project.h> 377c478bd9Sstevel@tonic-gate #include <sys/signal.h> 387c478bd9Sstevel@tonic-gate #include <sys/errno.h> 397c478bd9Sstevel@tonic-gate #include <sys/vmparam.h> 407c478bd9Sstevel@tonic-gate #include <sys/stack.h> 417c478bd9Sstevel@tonic-gate #include <sys/procfs.h> 427c478bd9Sstevel@tonic-gate #include <sys/prsystm.h> 437c478bd9Sstevel@tonic-gate #include <sys/cpuvar.h> 447c478bd9Sstevel@tonic-gate #include <sys/kmem.h> 457c478bd9Sstevel@tonic-gate #include <sys/vtrace.h> 467c478bd9Sstevel@tonic-gate #include <sys/door.h> 477c478bd9Sstevel@tonic-gate #include <vm/seg_kp.h> 487c478bd9Sstevel@tonic-gate #include <sys/debug.h> 497c478bd9Sstevel@tonic-gate #include <sys/tnf.h> 507c478bd9Sstevel@tonic-gate #include <sys/schedctl.h> 517c478bd9Sstevel@tonic-gate #include <sys/poll.h> 527c478bd9Sstevel@tonic-gate #include <sys/copyops.h> 537c478bd9Sstevel@tonic-gate #include <sys/lwp_upimutex_impl.h> 547c478bd9Sstevel@tonic-gate #include <sys/cpupart.h> 557c478bd9Sstevel@tonic-gate #include <sys/lgrp.h> 567c478bd9Sstevel@tonic-gate #include <sys/rctl.h> 577c478bd9Sstevel@tonic-gate #include <sys/contract_impl.h> 587c478bd9Sstevel@tonic-gate #include <sys/cpc_impl.h> 597c478bd9Sstevel@tonic-gate #include <sys/sdt.h> 607c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h> 619acbbeafSnn35248 #include <sys/brand.h> 627c478bd9Sstevel@tonic-gate 637c478bd9Sstevel@tonic-gate void *segkp_lwp; /* cookie for pool of segkp resources */ 647c478bd9Sstevel@tonic-gate 657c478bd9Sstevel@tonic-gate /* 667c478bd9Sstevel@tonic-gate * Create a thread that appears to be stopped at sys_rtt. 677c478bd9Sstevel@tonic-gate */ 687c478bd9Sstevel@tonic-gate klwp_t * 697c478bd9Sstevel@tonic-gate lwp_create(void (*proc)(), caddr_t arg, size_t len, proc_t *p, 707c478bd9Sstevel@tonic-gate int state, int pri, const k_sigset_t *smask, int cid, id_t lwpid) 717c478bd9Sstevel@tonic-gate { 727c478bd9Sstevel@tonic-gate klwp_t *lwp = NULL; 737c478bd9Sstevel@tonic-gate kthread_t *t; 747c478bd9Sstevel@tonic-gate kthread_t *tx; 757c478bd9Sstevel@tonic-gate cpupart_t *oldpart = NULL; 767c478bd9Sstevel@tonic-gate size_t stksize; 777c478bd9Sstevel@tonic-gate caddr_t lwpdata = NULL; 787c478bd9Sstevel@tonic-gate processorid_t binding; 797c478bd9Sstevel@tonic-gate int err = 0; 807c478bd9Sstevel@tonic-gate kproject_t *oldkpj, *newkpj; 817c478bd9Sstevel@tonic-gate void *bufp = NULL; 827c478bd9Sstevel@tonic-gate klwp_t *curlwp = ttolwp(curthread); 837c478bd9Sstevel@tonic-gate lwpent_t *lep; 847c478bd9Sstevel@tonic-gate lwpdir_t *old_dir = NULL; 857c478bd9Sstevel@tonic-gate uint_t old_dirsz = 0; 867c478bd9Sstevel@tonic-gate lwpdir_t **old_hash = NULL; 877c478bd9Sstevel@tonic-gate uint_t old_hashsz = 0; 887c478bd9Sstevel@tonic-gate int i; 897c478bd9Sstevel@tonic-gate int rctlfail = 0; 909acbbeafSnn35248 boolean_t branded = 0; 917c478bd9Sstevel@tonic-gate 927c478bd9Sstevel@tonic-gate mutex_enter(&p->p_lock); 937c478bd9Sstevel@tonic-gate mutex_enter(&p->p_zone->zone_nlwps_lock); 947c478bd9Sstevel@tonic-gate /* 957c478bd9Sstevel@tonic-gate * don't enforce rctl limits on system processes 967c478bd9Sstevel@tonic-gate */ 977c478bd9Sstevel@tonic-gate if (cid != syscid) { 987c478bd9Sstevel@tonic-gate if (p->p_task->tk_nlwps >= p->p_task->tk_nlwps_ctl) 997c478bd9Sstevel@tonic-gate if (rctl_test(rc_task_lwps, p->p_task->tk_rctls, p, 1007c478bd9Sstevel@tonic-gate 1, 0) & RCT_DENY) 1017c478bd9Sstevel@tonic-gate rctlfail = 1; 1027c478bd9Sstevel@tonic-gate if (p->p_task->tk_proj->kpj_nlwps >= 1037c478bd9Sstevel@tonic-gate p->p_task->tk_proj->kpj_nlwps_ctl) 1047c478bd9Sstevel@tonic-gate if (rctl_test(rc_project_nlwps, 1057c478bd9Sstevel@tonic-gate p->p_task->tk_proj->kpj_rctls, p, 1, 0) 1067c478bd9Sstevel@tonic-gate & RCT_DENY) 1077c478bd9Sstevel@tonic-gate rctlfail = 1; 1087c478bd9Sstevel@tonic-gate if (p->p_zone->zone_nlwps >= p->p_zone->zone_nlwps_ctl) 1097c478bd9Sstevel@tonic-gate if (rctl_test(rc_zone_nlwps, p->p_zone->zone_rctls, p, 1107c478bd9Sstevel@tonic-gate 1, 0) & RCT_DENY) 1117c478bd9Sstevel@tonic-gate rctlfail = 1; 1127c478bd9Sstevel@tonic-gate } 1137c478bd9Sstevel@tonic-gate if (rctlfail) { 1147c478bd9Sstevel@tonic-gate mutex_exit(&p->p_zone->zone_nlwps_lock); 1157c478bd9Sstevel@tonic-gate mutex_exit(&p->p_lock); 1167c478bd9Sstevel@tonic-gate return (NULL); 1177c478bd9Sstevel@tonic-gate } 1187c478bd9Sstevel@tonic-gate p->p_task->tk_nlwps++; 1197c478bd9Sstevel@tonic-gate p->p_task->tk_proj->kpj_nlwps++; 1207c478bd9Sstevel@tonic-gate p->p_zone->zone_nlwps++; 1217c478bd9Sstevel@tonic-gate mutex_exit(&p->p_zone->zone_nlwps_lock); 1227c478bd9Sstevel@tonic-gate mutex_exit(&p->p_lock); 1237c478bd9Sstevel@tonic-gate 1247c478bd9Sstevel@tonic-gate if (curlwp == NULL || (stksize = curlwp->lwp_childstksz) == 0) 1257c478bd9Sstevel@tonic-gate stksize = lwp_default_stksize; 1267c478bd9Sstevel@tonic-gate 1277c478bd9Sstevel@tonic-gate /* 1287c478bd9Sstevel@tonic-gate * Try to reclaim a <lwp,stack> from 'deathrow' 1297c478bd9Sstevel@tonic-gate */ 1307c478bd9Sstevel@tonic-gate if (stksize == lwp_default_stksize) { 1317c478bd9Sstevel@tonic-gate if (lwp_reapcnt > 0) { 1327c478bd9Sstevel@tonic-gate mutex_enter(&reaplock); 1337c478bd9Sstevel@tonic-gate if ((t = lwp_deathrow) != NULL) { 1347c478bd9Sstevel@tonic-gate ASSERT(t->t_swap); 1357c478bd9Sstevel@tonic-gate lwp_deathrow = t->t_forw; 1367c478bd9Sstevel@tonic-gate lwp_reapcnt--; 1377c478bd9Sstevel@tonic-gate lwpdata = t->t_swap; 1387c478bd9Sstevel@tonic-gate lwp = t->t_lwp; 1397c478bd9Sstevel@tonic-gate } 1407c478bd9Sstevel@tonic-gate mutex_exit(&reaplock); 1417c478bd9Sstevel@tonic-gate if (t) { 1427c478bd9Sstevel@tonic-gate t->t_swap = NULL; 1437c478bd9Sstevel@tonic-gate lwp_stk_fini(t->t_lwp); 1447c478bd9Sstevel@tonic-gate t->t_lwp = NULL; 1457c478bd9Sstevel@tonic-gate t->t_forw = NULL; 1467c478bd9Sstevel@tonic-gate thread_free(t); 1477c478bd9Sstevel@tonic-gate } 1487c478bd9Sstevel@tonic-gate } 1497c478bd9Sstevel@tonic-gate if (lwpdata == NULL && 1507c478bd9Sstevel@tonic-gate (lwpdata = (caddr_t)segkp_cache_get(segkp_lwp)) == NULL) { 1517c478bd9Sstevel@tonic-gate mutex_enter(&p->p_lock); 1527c478bd9Sstevel@tonic-gate mutex_enter(&p->p_zone->zone_nlwps_lock); 1537c478bd9Sstevel@tonic-gate p->p_task->tk_nlwps--; 1547c478bd9Sstevel@tonic-gate p->p_task->tk_proj->kpj_nlwps--; 1557c478bd9Sstevel@tonic-gate p->p_zone->zone_nlwps--; 1567c478bd9Sstevel@tonic-gate mutex_exit(&p->p_zone->zone_nlwps_lock); 1577c478bd9Sstevel@tonic-gate mutex_exit(&p->p_lock); 1587c478bd9Sstevel@tonic-gate return (NULL); 1597c478bd9Sstevel@tonic-gate } 1607c478bd9Sstevel@tonic-gate } else { 1617c478bd9Sstevel@tonic-gate stksize = roundup(stksize, PAGESIZE); 1627c478bd9Sstevel@tonic-gate if ((lwpdata = (caddr_t)segkp_get(segkp, stksize, 1637c478bd9Sstevel@tonic-gate (KPD_NOWAIT | KPD_HASREDZONE | KPD_LOCKED))) == NULL) { 1647c478bd9Sstevel@tonic-gate mutex_enter(&p->p_lock); 1657c478bd9Sstevel@tonic-gate mutex_enter(&p->p_zone->zone_nlwps_lock); 1667c478bd9Sstevel@tonic-gate p->p_task->tk_nlwps--; 1677c478bd9Sstevel@tonic-gate p->p_task->tk_proj->kpj_nlwps--; 1687c478bd9Sstevel@tonic-gate p->p_zone->zone_nlwps--; 1697c478bd9Sstevel@tonic-gate mutex_exit(&p->p_zone->zone_nlwps_lock); 1707c478bd9Sstevel@tonic-gate mutex_exit(&p->p_lock); 1717c478bd9Sstevel@tonic-gate return (NULL); 1727c478bd9Sstevel@tonic-gate } 1737c478bd9Sstevel@tonic-gate } 1747c478bd9Sstevel@tonic-gate 1757c478bd9Sstevel@tonic-gate /* 1767c478bd9Sstevel@tonic-gate * Create a thread, initializing the stack pointer 1777c478bd9Sstevel@tonic-gate */ 1787c478bd9Sstevel@tonic-gate t = thread_create(lwpdata, stksize, NULL, NULL, 0, p, TS_STOPPED, pri); 1797c478bd9Sstevel@tonic-gate 1807c478bd9Sstevel@tonic-gate t->t_swap = lwpdata; /* Start of page-able data */ 1817c478bd9Sstevel@tonic-gate if (lwp == NULL) 1827c478bd9Sstevel@tonic-gate lwp = kmem_cache_alloc(lwp_cache, KM_SLEEP); 1837c478bd9Sstevel@tonic-gate bzero(lwp, sizeof (*lwp)); 1847c478bd9Sstevel@tonic-gate t->t_lwp = lwp; 1857c478bd9Sstevel@tonic-gate 1867c478bd9Sstevel@tonic-gate t->t_hold = *smask; 1877c478bd9Sstevel@tonic-gate lwp->lwp_thread = t; 1887c478bd9Sstevel@tonic-gate lwp->lwp_procp = p; 1897c478bd9Sstevel@tonic-gate lwp->lwp_sigaltstack.ss_flags = SS_DISABLE; 1907c478bd9Sstevel@tonic-gate if (curlwp != NULL && curlwp->lwp_childstksz != 0) 1917c478bd9Sstevel@tonic-gate lwp->lwp_childstksz = curlwp->lwp_childstksz; 1927c478bd9Sstevel@tonic-gate 1937c478bd9Sstevel@tonic-gate t->t_stk = lwp_stk_init(lwp, t->t_stk); 1947c478bd9Sstevel@tonic-gate thread_load(t, proc, arg, len); 1957c478bd9Sstevel@tonic-gate 1967c478bd9Sstevel@tonic-gate /* 1977c478bd9Sstevel@tonic-gate * Allocate the SIGPROF buffer if ITIMER_REALPROF is in effect. 1987c478bd9Sstevel@tonic-gate */ 1997c478bd9Sstevel@tonic-gate if (timerisset(&p->p_rprof_timer.it_value)) 2007c478bd9Sstevel@tonic-gate t->t_rprof = kmem_zalloc(sizeof (struct rprof), KM_SLEEP); 2017c478bd9Sstevel@tonic-gate 2027c478bd9Sstevel@tonic-gate if (cid != NOCLASS) 2037c478bd9Sstevel@tonic-gate (void) CL_ALLOC(&bufp, cid, KM_SLEEP); 2047c478bd9Sstevel@tonic-gate 2057c478bd9Sstevel@tonic-gate /* 2067c478bd9Sstevel@tonic-gate * Allocate an lwp directory entry for the new lwp. 2077c478bd9Sstevel@tonic-gate */ 2087c478bd9Sstevel@tonic-gate lep = kmem_zalloc(sizeof (*lep), KM_SLEEP); 2097c478bd9Sstevel@tonic-gate 2107c478bd9Sstevel@tonic-gate mutex_enter(&p->p_lock); 2117c478bd9Sstevel@tonic-gate grow: 2127c478bd9Sstevel@tonic-gate /* 2137c478bd9Sstevel@tonic-gate * Grow the lwp (thread) directory and lwpid hash table if necessary. 2147c478bd9Sstevel@tonic-gate * A note on the growth algorithm: 2157c478bd9Sstevel@tonic-gate * The new lwp directory size is computed as: 2167c478bd9Sstevel@tonic-gate * new = 2 * old + 2 2177c478bd9Sstevel@tonic-gate * Starting with an initial size of 2 (see exec_common()), 2187c478bd9Sstevel@tonic-gate * this yields numbers that are a power of two minus 2: 2197c478bd9Sstevel@tonic-gate * 2, 6, 14, 30, 62, 126, 254, 510, 1022, ... 2207c478bd9Sstevel@tonic-gate * The size of the lwpid hash table must be a power of two 2217c478bd9Sstevel@tonic-gate * and must be commensurate in size with the lwp directory 2227c478bd9Sstevel@tonic-gate * so that hash bucket chains remain short. Therefore, 2237c478bd9Sstevel@tonic-gate * the lwpid hash table size is computed as: 2247c478bd9Sstevel@tonic-gate * hashsz = (dirsz + 2) / 2 2257c478bd9Sstevel@tonic-gate * which leads to these hash table sizes corresponding to 2267c478bd9Sstevel@tonic-gate * the above directory sizes: 2277c478bd9Sstevel@tonic-gate * 2, 4, 8, 16, 32, 64, 128, 256, 512, ... 2287c478bd9Sstevel@tonic-gate */ 2297c478bd9Sstevel@tonic-gate while (p->p_lwpfree == NULL) { 2307c478bd9Sstevel@tonic-gate uint_t dirsz = p->p_lwpdir_sz; 2317c478bd9Sstevel@tonic-gate uint_t new_dirsz; 2327c478bd9Sstevel@tonic-gate uint_t new_hashsz; 2337c478bd9Sstevel@tonic-gate lwpdir_t *new_dir; 2347c478bd9Sstevel@tonic-gate lwpdir_t *ldp; 2357c478bd9Sstevel@tonic-gate lwpdir_t **new_hash; 2367c478bd9Sstevel@tonic-gate 2377c478bd9Sstevel@tonic-gate mutex_exit(&p->p_lock); 2387c478bd9Sstevel@tonic-gate 2397c478bd9Sstevel@tonic-gate if (old_dir != NULL) { 2407c478bd9Sstevel@tonic-gate kmem_free(old_dir, old_dirsz * sizeof (*old_dir)); 2417c478bd9Sstevel@tonic-gate kmem_free(old_hash, old_hashsz * sizeof (*old_hash)); 2427c478bd9Sstevel@tonic-gate old_dir = NULL; 2437c478bd9Sstevel@tonic-gate old_dirsz = 0; 2447c478bd9Sstevel@tonic-gate old_hash = NULL; 2457c478bd9Sstevel@tonic-gate old_hashsz = 0; 2467c478bd9Sstevel@tonic-gate } 2477c478bd9Sstevel@tonic-gate new_dirsz = 2 * dirsz + 2; 2487c478bd9Sstevel@tonic-gate new_dir = kmem_zalloc(new_dirsz * sizeof (lwpdir_t), KM_SLEEP); 2497c478bd9Sstevel@tonic-gate for (ldp = new_dir, i = 1; i < new_dirsz; i++, ldp++) 2507c478bd9Sstevel@tonic-gate ldp->ld_next = ldp + 1; 2517c478bd9Sstevel@tonic-gate new_hashsz = (new_dirsz + 2) / 2; 2527c478bd9Sstevel@tonic-gate new_hash = kmem_zalloc(new_hashsz * sizeof (lwpdir_t *), 2537c478bd9Sstevel@tonic-gate KM_SLEEP); 2547c478bd9Sstevel@tonic-gate 2557c478bd9Sstevel@tonic-gate mutex_enter(&p->p_lock); 2567c478bd9Sstevel@tonic-gate if (p == curproc) 2577c478bd9Sstevel@tonic-gate prbarrier(p); 2587c478bd9Sstevel@tonic-gate 2597c478bd9Sstevel@tonic-gate if (dirsz != p->p_lwpdir_sz || p->p_lwpfree != NULL) { 2607c478bd9Sstevel@tonic-gate /* 2617c478bd9Sstevel@tonic-gate * Someone else beat us to it or some lwp exited. 2627c478bd9Sstevel@tonic-gate * Set up to free our memory and take a lap. 2637c478bd9Sstevel@tonic-gate */ 2647c478bd9Sstevel@tonic-gate old_dir = new_dir; 2657c478bd9Sstevel@tonic-gate old_dirsz = new_dirsz; 2667c478bd9Sstevel@tonic-gate old_hash = new_hash; 2677c478bd9Sstevel@tonic-gate old_hashsz = new_hashsz; 2687c478bd9Sstevel@tonic-gate } else { 2697c478bd9Sstevel@tonic-gate old_dir = p->p_lwpdir; 2707c478bd9Sstevel@tonic-gate old_dirsz = p->p_lwpdir_sz; 2717c478bd9Sstevel@tonic-gate old_hash = p->p_tidhash; 2727c478bd9Sstevel@tonic-gate old_hashsz = p->p_tidhash_sz; 2737c478bd9Sstevel@tonic-gate p->p_lwpdir = new_dir; 2747c478bd9Sstevel@tonic-gate p->p_lwpfree = new_dir; 2757c478bd9Sstevel@tonic-gate p->p_lwpdir_sz = new_dirsz; 2767c478bd9Sstevel@tonic-gate p->p_tidhash = new_hash; 2777c478bd9Sstevel@tonic-gate p->p_tidhash_sz = new_hashsz; 2787c478bd9Sstevel@tonic-gate /* 2797c478bd9Sstevel@tonic-gate * We simply hash in all of the old directory entries. 2807c478bd9Sstevel@tonic-gate * This works because the old directory has no empty 2817c478bd9Sstevel@tonic-gate * slots and the new hash table starts out empty. 2827c478bd9Sstevel@tonic-gate * This reproduces the original directory ordering 2837c478bd9Sstevel@tonic-gate * (required for /proc directory semantics). 2847c478bd9Sstevel@tonic-gate */ 2857c478bd9Sstevel@tonic-gate for (ldp = old_dir, i = 0; i < dirsz; i++, ldp++) 2867c478bd9Sstevel@tonic-gate lwp_hash_in(p, ldp->ld_entry); 2877c478bd9Sstevel@tonic-gate /* 2887c478bd9Sstevel@tonic-gate * Defer freeing memory until we drop p->p_lock, 2897c478bd9Sstevel@tonic-gate */ 2907c478bd9Sstevel@tonic-gate } 2917c478bd9Sstevel@tonic-gate } 2927c478bd9Sstevel@tonic-gate 2937c478bd9Sstevel@tonic-gate /* 2947c478bd9Sstevel@tonic-gate * Block the process against /proc while we manipulate p->p_tlist, 2957c478bd9Sstevel@tonic-gate * unless lwp_create() was called by /proc for the PCAGENT operation. 2967c478bd9Sstevel@tonic-gate * We want to do this early enough so that we don't drop p->p_lock 2977c478bd9Sstevel@tonic-gate * until the thread is put on the p->p_tlist. 2987c478bd9Sstevel@tonic-gate */ 2997c478bd9Sstevel@tonic-gate if (p == curproc) { 3007c478bd9Sstevel@tonic-gate prbarrier(p); 3017c478bd9Sstevel@tonic-gate /* 3027c478bd9Sstevel@tonic-gate * If the current lwp has been requested to stop, do so now. 3037c478bd9Sstevel@tonic-gate * Otherwise we have a race condition between /proc attempting 3047c478bd9Sstevel@tonic-gate * to stop the process and this thread creating a new lwp 3057c478bd9Sstevel@tonic-gate * that was not seen when the /proc PCSTOP request was issued. 3067c478bd9Sstevel@tonic-gate * We rely on stop() to call prbarrier(p) before returning. 3077c478bd9Sstevel@tonic-gate */ 3087c478bd9Sstevel@tonic-gate while ((curthread->t_proc_flag & TP_PRSTOP) && 3097c478bd9Sstevel@tonic-gate !ttolwp(curthread)->lwp_nostop) 3107c478bd9Sstevel@tonic-gate stop(PR_REQUESTED, 0); 3117c478bd9Sstevel@tonic-gate 3127c478bd9Sstevel@tonic-gate /* 3137c478bd9Sstevel@tonic-gate * If process is exiting, there could be a race between 3147c478bd9Sstevel@tonic-gate * the agent lwp creation and the new lwp currently being 3157c478bd9Sstevel@tonic-gate * created. So to prevent this race lwp creation is failed 3167c478bd9Sstevel@tonic-gate * if the process is exiting. 3177c478bd9Sstevel@tonic-gate */ 3187c478bd9Sstevel@tonic-gate if (p->p_flag & (SEXITLWPS|SKILLED)) { 3197c478bd9Sstevel@tonic-gate err = 1; 3207c478bd9Sstevel@tonic-gate goto error; 3217c478bd9Sstevel@tonic-gate } 3227c478bd9Sstevel@tonic-gate 3237c478bd9Sstevel@tonic-gate /* 3247c478bd9Sstevel@tonic-gate * Since we might have dropped p->p_lock, the 3257c478bd9Sstevel@tonic-gate * lwp directory free list might have changed. 3267c478bd9Sstevel@tonic-gate */ 3277c478bd9Sstevel@tonic-gate if (p->p_lwpfree == NULL) 3287c478bd9Sstevel@tonic-gate goto grow; 3297c478bd9Sstevel@tonic-gate } 3307c478bd9Sstevel@tonic-gate 3317c478bd9Sstevel@tonic-gate kpreempt_disable(); /* can't grab cpu_lock here */ 3327c478bd9Sstevel@tonic-gate 3337c478bd9Sstevel@tonic-gate /* 3347c478bd9Sstevel@tonic-gate * Inherit processor and processor set bindings from curthread, 3357c478bd9Sstevel@tonic-gate * unless we're creating a new kernel process, in which case 3367c478bd9Sstevel@tonic-gate * clear all bindings. 3377c478bd9Sstevel@tonic-gate */ 3387c478bd9Sstevel@tonic-gate if (cid == syscid) { 3397c478bd9Sstevel@tonic-gate t->t_bind_cpu = binding = PBIND_NONE; 3407c478bd9Sstevel@tonic-gate t->t_cpupart = oldpart = &cp_default; 3417c478bd9Sstevel@tonic-gate t->t_bind_pset = PS_NONE; 3427c478bd9Sstevel@tonic-gate } else { 3437c478bd9Sstevel@tonic-gate binding = curthread->t_bind_cpu; 3447c478bd9Sstevel@tonic-gate t->t_bind_cpu = binding; 3457c478bd9Sstevel@tonic-gate oldpart = t->t_cpupart; 3467c478bd9Sstevel@tonic-gate t->t_cpupart = curthread->t_cpupart; 3477c478bd9Sstevel@tonic-gate t->t_bind_pset = curthread->t_bind_pset; 3487c478bd9Sstevel@tonic-gate } 3497c478bd9Sstevel@tonic-gate 3507c478bd9Sstevel@tonic-gate /* 3517c478bd9Sstevel@tonic-gate * thread_create() initializes this thread's home lgroup to the root. 3527c478bd9Sstevel@tonic-gate * Choose a more suitable lgroup, since this thread is associated 3537c478bd9Sstevel@tonic-gate * with an lwp. 3547c478bd9Sstevel@tonic-gate */ 3557c478bd9Sstevel@tonic-gate ASSERT(oldpart != NULL); 3567c478bd9Sstevel@tonic-gate if (binding != PBIND_NONE && t->t_affinitycnt == 0) { 3577c478bd9Sstevel@tonic-gate t->t_bound_cpu = cpu[binding]; 3587c478bd9Sstevel@tonic-gate if (t->t_lpl != t->t_bound_cpu->cpu_lpl) 3597c478bd9Sstevel@tonic-gate lgrp_move_thread(t, t->t_bound_cpu->cpu_lpl, 1); 3607c478bd9Sstevel@tonic-gate } else { 3617c478bd9Sstevel@tonic-gate lgrp_move_thread(t, lgrp_choose(t, t->t_cpupart), 1); 3627c478bd9Sstevel@tonic-gate } 3637c478bd9Sstevel@tonic-gate 3647c478bd9Sstevel@tonic-gate kpreempt_enable(); 3657c478bd9Sstevel@tonic-gate 3667c478bd9Sstevel@tonic-gate /* 3677c478bd9Sstevel@tonic-gate * make sure lpl points to our own partition 3687c478bd9Sstevel@tonic-gate */ 3697c478bd9Sstevel@tonic-gate ASSERT(t->t_lpl >= t->t_cpupart->cp_lgrploads); 3707c478bd9Sstevel@tonic-gate ASSERT(t->t_lpl < t->t_cpupart->cp_lgrploads + 3717c478bd9Sstevel@tonic-gate t->t_cpupart->cp_nlgrploads); 3727c478bd9Sstevel@tonic-gate 3737c478bd9Sstevel@tonic-gate /* 3747c478bd9Sstevel@tonic-gate * If we're creating a new process, then inherit the project from our 3757c478bd9Sstevel@tonic-gate * parent. If we're only creating an additional lwp then use the 3767c478bd9Sstevel@tonic-gate * project pointer of the target process. 3777c478bd9Sstevel@tonic-gate */ 3787c478bd9Sstevel@tonic-gate if (p->p_task == NULL) 3797c478bd9Sstevel@tonic-gate newkpj = ttoproj(curthread); 3807c478bd9Sstevel@tonic-gate else 3817c478bd9Sstevel@tonic-gate newkpj = p->p_task->tk_proj; 3827c478bd9Sstevel@tonic-gate 3837c478bd9Sstevel@tonic-gate /* 3847c478bd9Sstevel@tonic-gate * It is safe to point the thread to the new project without holding it 3857c478bd9Sstevel@tonic-gate * since we're holding the target process' p_lock here and therefore 3867c478bd9Sstevel@tonic-gate * we're guaranteed that it will not move to another project. 3877c478bd9Sstevel@tonic-gate */ 3887c478bd9Sstevel@tonic-gate oldkpj = ttoproj(t); 3897c478bd9Sstevel@tonic-gate if (newkpj != oldkpj) { 3907c478bd9Sstevel@tonic-gate t->t_proj = newkpj; 3917c478bd9Sstevel@tonic-gate (void) project_hold(newkpj); 3927c478bd9Sstevel@tonic-gate project_rele(oldkpj); 3937c478bd9Sstevel@tonic-gate } 3947c478bd9Sstevel@tonic-gate 3957c478bd9Sstevel@tonic-gate if (cid != NOCLASS) { 3967c478bd9Sstevel@tonic-gate /* 3977c478bd9Sstevel@tonic-gate * If the lwp is being created in the current process 3987c478bd9Sstevel@tonic-gate * and matches the current thread's scheduling class, 3997c478bd9Sstevel@tonic-gate * we should propagate the current thread's scheduling 4007c478bd9Sstevel@tonic-gate * parameters by calling CL_FORK. Otherwise just use 4017c478bd9Sstevel@tonic-gate * the defaults by calling CL_ENTERCLASS. 4027c478bd9Sstevel@tonic-gate */ 4037c478bd9Sstevel@tonic-gate if (p != curproc || curthread->t_cid != cid) { 4047c478bd9Sstevel@tonic-gate err = CL_ENTERCLASS(t, cid, NULL, NULL, bufp); 4057c478bd9Sstevel@tonic-gate t->t_pri = pri; /* CL_ENTERCLASS may have changed it */ 4067c478bd9Sstevel@tonic-gate } else { 4077c478bd9Sstevel@tonic-gate t->t_clfuncs = &(sclass[cid].cl_funcs->thread); 4087c478bd9Sstevel@tonic-gate err = CL_FORK(curthread, t, bufp); 4097c478bd9Sstevel@tonic-gate t->t_cid = cid; 4107c478bd9Sstevel@tonic-gate } 4117c478bd9Sstevel@tonic-gate if (err) 4127c478bd9Sstevel@tonic-gate goto error; 4137c478bd9Sstevel@tonic-gate else 4147c478bd9Sstevel@tonic-gate bufp = NULL; 4157c478bd9Sstevel@tonic-gate } 4167c478bd9Sstevel@tonic-gate 4177c478bd9Sstevel@tonic-gate /* 4187c478bd9Sstevel@tonic-gate * If we were given an lwpid then use it, else allocate one. 4197c478bd9Sstevel@tonic-gate */ 4207c478bd9Sstevel@tonic-gate if (lwpid != 0) 4217c478bd9Sstevel@tonic-gate t->t_tid = lwpid; 4227c478bd9Sstevel@tonic-gate else { 4237c478bd9Sstevel@tonic-gate /* 4247c478bd9Sstevel@tonic-gate * lwp/thread id 0 is never valid; reserved for special checks. 4257c478bd9Sstevel@tonic-gate * lwp/thread id 1 is reserved for the main thread. 4267c478bd9Sstevel@tonic-gate * Start again at 2 when INT_MAX has been reached 4277c478bd9Sstevel@tonic-gate * (id_t is a signed 32-bit integer). 4287c478bd9Sstevel@tonic-gate */ 4297c478bd9Sstevel@tonic-gate id_t prev_id = p->p_lwpid; /* last allocated tid */ 4307c478bd9Sstevel@tonic-gate 4317c478bd9Sstevel@tonic-gate do { /* avoid lwpid duplication */ 4327c478bd9Sstevel@tonic-gate if (p->p_lwpid == INT_MAX) { 4337c478bd9Sstevel@tonic-gate p->p_flag |= SLWPWRAP; 4347c478bd9Sstevel@tonic-gate p->p_lwpid = 1; 4357c478bd9Sstevel@tonic-gate } 4367c478bd9Sstevel@tonic-gate if ((t->t_tid = ++p->p_lwpid) == prev_id) { 4377c478bd9Sstevel@tonic-gate /* 4387c478bd9Sstevel@tonic-gate * All lwpids are allocated; fail the request. 4397c478bd9Sstevel@tonic-gate */ 4407c478bd9Sstevel@tonic-gate err = 1; 4417c478bd9Sstevel@tonic-gate goto error; 4427c478bd9Sstevel@tonic-gate } 4437c478bd9Sstevel@tonic-gate /* 4447c478bd9Sstevel@tonic-gate * We only need to worry about colliding with an id 4457c478bd9Sstevel@tonic-gate * that's already in use if this process has 4467c478bd9Sstevel@tonic-gate * cycled through all available lwp ids. 4477c478bd9Sstevel@tonic-gate */ 4487c478bd9Sstevel@tonic-gate if ((p->p_flag & SLWPWRAP) == 0) 4497c478bd9Sstevel@tonic-gate break; 4507c478bd9Sstevel@tonic-gate } while (lwp_hash_lookup(p, t->t_tid) != NULL); 4517c478bd9Sstevel@tonic-gate } 4529acbbeafSnn35248 4539acbbeafSnn35248 /* 4549acbbeafSnn35248 * If this is a branded process, let the brand do any necessary lwp 4559acbbeafSnn35248 * initialization. 4569acbbeafSnn35248 */ 4579acbbeafSnn35248 if (PROC_IS_BRANDED(p)) { 4589acbbeafSnn35248 if (BROP(p)->b_initlwp(lwp)) { 4599acbbeafSnn35248 err = 1; 4609acbbeafSnn35248 goto error; 4619acbbeafSnn35248 } 4629acbbeafSnn35248 branded = 1; 4639acbbeafSnn35248 } 4649acbbeafSnn35248 4652cb27123Saguzovsk if (t->t_tid == 1) { 4662cb27123Saguzovsk kpreempt_disable(); 4672cb27123Saguzovsk ASSERT(t->t_lpl != NULL); 4682cb27123Saguzovsk p->p_t1_lgrpid = t->t_lpl->lpl_lgrpid; 4692cb27123Saguzovsk kpreempt_enable(); 4702cb27123Saguzovsk if (p->p_tr_lgrpid != LGRP_NONE && 4712cb27123Saguzovsk p->p_tr_lgrpid != p->p_t1_lgrpid) { 4722cb27123Saguzovsk lgrp_update_trthr_migrations(1); 4732cb27123Saguzovsk } 4742cb27123Saguzovsk } 4752cb27123Saguzovsk 4767c478bd9Sstevel@tonic-gate p->p_lwpcnt++; 4777c478bd9Sstevel@tonic-gate t->t_waitfor = -1; 4787c478bd9Sstevel@tonic-gate 4797c478bd9Sstevel@tonic-gate /* 4807c478bd9Sstevel@tonic-gate * Turn microstate accounting on for thread if on for process. 4817c478bd9Sstevel@tonic-gate */ 4827c478bd9Sstevel@tonic-gate if (p->p_flag & SMSACCT) 4837c478bd9Sstevel@tonic-gate t->t_proc_flag |= TP_MSACCT; 4847c478bd9Sstevel@tonic-gate 4857c478bd9Sstevel@tonic-gate /* 4867c478bd9Sstevel@tonic-gate * If the process has watchpoints, mark the new thread as such. 4877c478bd9Sstevel@tonic-gate */ 4887c478bd9Sstevel@tonic-gate if (pr_watch_active(p)) 4897c478bd9Sstevel@tonic-gate watch_enable(t); 4907c478bd9Sstevel@tonic-gate 4917c478bd9Sstevel@tonic-gate /* 4927c478bd9Sstevel@tonic-gate * The lwp is being created in the stopped state. 4937c478bd9Sstevel@tonic-gate * We set all the necessary flags to indicate that fact here. 4947c478bd9Sstevel@tonic-gate * We omit the TS_CREATE flag from t_schedflag so that the lwp 4957c478bd9Sstevel@tonic-gate * cannot be set running until the caller is finished with it, 4967c478bd9Sstevel@tonic-gate * even if lwp_continue() is called on it after we drop p->p_lock. 4977c478bd9Sstevel@tonic-gate * When the caller is finished with the newly-created lwp, 4987c478bd9Sstevel@tonic-gate * the caller must call lwp_create_done() to allow the lwp 4997c478bd9Sstevel@tonic-gate * to be set running. If the TP_HOLDLWP is left set, the 5007c478bd9Sstevel@tonic-gate * lwp will suspend itself after reaching system call exit. 5017c478bd9Sstevel@tonic-gate */ 5027c478bd9Sstevel@tonic-gate init_mstate(t, LMS_STOPPED); 5037c478bd9Sstevel@tonic-gate t->t_proc_flag |= TP_HOLDLWP; 5047c478bd9Sstevel@tonic-gate t->t_schedflag |= (TS_ALLSTART & ~(TS_CSTART | TS_CREATE)); 5057c478bd9Sstevel@tonic-gate t->t_whystop = PR_SUSPENDED; 5067c478bd9Sstevel@tonic-gate t->t_whatstop = SUSPEND_NORMAL; 5077c478bd9Sstevel@tonic-gate t->t_sig_check = 1; /* ensure that TP_HOLDLWP is honored */ 5087c478bd9Sstevel@tonic-gate 5097c478bd9Sstevel@tonic-gate /* 5107c478bd9Sstevel@tonic-gate * Set system call processing flags in case tracing or profiling 5117c478bd9Sstevel@tonic-gate * is set. The first system call will evaluate these and turn 5127c478bd9Sstevel@tonic-gate * them off if they aren't needed. 5137c478bd9Sstevel@tonic-gate */ 5147c478bd9Sstevel@tonic-gate t->t_pre_sys = 1; 5157c478bd9Sstevel@tonic-gate t->t_post_sys = 1; 5167c478bd9Sstevel@tonic-gate 5177c478bd9Sstevel@tonic-gate /* 5187c478bd9Sstevel@tonic-gate * Insert the new thread into the list of all threads. 5197c478bd9Sstevel@tonic-gate */ 5207c478bd9Sstevel@tonic-gate if ((tx = p->p_tlist) == NULL) { 5217c478bd9Sstevel@tonic-gate t->t_back = t; 5227c478bd9Sstevel@tonic-gate t->t_forw = t; 5237c478bd9Sstevel@tonic-gate p->p_tlist = t; 5247c478bd9Sstevel@tonic-gate } else { 5257c478bd9Sstevel@tonic-gate t->t_forw = tx; 5267c478bd9Sstevel@tonic-gate t->t_back = tx->t_back; 5277c478bd9Sstevel@tonic-gate tx->t_back->t_forw = t; 5287c478bd9Sstevel@tonic-gate tx->t_back = t; 5297c478bd9Sstevel@tonic-gate } 5307c478bd9Sstevel@tonic-gate 5317c478bd9Sstevel@tonic-gate /* 5327c478bd9Sstevel@tonic-gate * Insert the new lwp into an lwp directory slot position 5337c478bd9Sstevel@tonic-gate * and into the lwpid hash table. 5347c478bd9Sstevel@tonic-gate */ 5357c478bd9Sstevel@tonic-gate lep->le_thread = t; 5367c478bd9Sstevel@tonic-gate lep->le_lwpid = t->t_tid; 5377c478bd9Sstevel@tonic-gate lep->le_start = t->t_start; 5387c478bd9Sstevel@tonic-gate lwp_hash_in(p, lep); 5397c478bd9Sstevel@tonic-gate 5407c478bd9Sstevel@tonic-gate if (state == TS_RUN) { 5417c478bd9Sstevel@tonic-gate /* 5427c478bd9Sstevel@tonic-gate * We set the new lwp running immediately. 5437c478bd9Sstevel@tonic-gate */ 5447c478bd9Sstevel@tonic-gate t->t_proc_flag &= ~TP_HOLDLWP; 5457c478bd9Sstevel@tonic-gate lwp_create_done(t); 5467c478bd9Sstevel@tonic-gate } 5477c478bd9Sstevel@tonic-gate 5487c478bd9Sstevel@tonic-gate error: 5497c478bd9Sstevel@tonic-gate if (err) { 5507c478bd9Sstevel@tonic-gate /* 5517c478bd9Sstevel@tonic-gate * We have failed to create an lwp, so decrement the number 5527c478bd9Sstevel@tonic-gate * of lwps in the task and let the lgroup load averages know 5537c478bd9Sstevel@tonic-gate * that this thread isn't going to show up. 5547c478bd9Sstevel@tonic-gate */ 5557c478bd9Sstevel@tonic-gate kpreempt_disable(); 5567c478bd9Sstevel@tonic-gate lgrp_move_thread(t, NULL, 1); 5577c478bd9Sstevel@tonic-gate kpreempt_enable(); 5587c478bd9Sstevel@tonic-gate 5597c478bd9Sstevel@tonic-gate ASSERT(MUTEX_HELD(&p->p_lock)); 5607c478bd9Sstevel@tonic-gate mutex_enter(&p->p_zone->zone_nlwps_lock); 5617c478bd9Sstevel@tonic-gate p->p_task->tk_nlwps--; 5627c478bd9Sstevel@tonic-gate p->p_task->tk_proj->kpj_nlwps--; 5637c478bd9Sstevel@tonic-gate p->p_zone->zone_nlwps--; 5647c478bd9Sstevel@tonic-gate mutex_exit(&p->p_zone->zone_nlwps_lock); 5657c478bd9Sstevel@tonic-gate if (cid != NOCLASS && bufp != NULL) 5667c478bd9Sstevel@tonic-gate CL_FREE(cid, bufp); 5677c478bd9Sstevel@tonic-gate 5689acbbeafSnn35248 if (branded) 5699acbbeafSnn35248 BROP(p)->b_freelwp(lwp); 5709acbbeafSnn35248 5717c478bd9Sstevel@tonic-gate mutex_exit(&p->p_lock); 5727c478bd9Sstevel@tonic-gate t->t_state = TS_FREE; 5737c478bd9Sstevel@tonic-gate thread_rele(t); 5747c478bd9Sstevel@tonic-gate 5757c478bd9Sstevel@tonic-gate /* 5767c478bd9Sstevel@tonic-gate * We need to remove t from the list of all threads 5777c478bd9Sstevel@tonic-gate * because thread_exit()/lwp_exit() isn't called on t. 5787c478bd9Sstevel@tonic-gate */ 5797c478bd9Sstevel@tonic-gate mutex_enter(&pidlock); 5807c478bd9Sstevel@tonic-gate ASSERT(t != t->t_next); /* t0 never exits */ 5817c478bd9Sstevel@tonic-gate t->t_next->t_prev = t->t_prev; 5827c478bd9Sstevel@tonic-gate t->t_prev->t_next = t->t_next; 5837c478bd9Sstevel@tonic-gate mutex_exit(&pidlock); 5847c478bd9Sstevel@tonic-gate 5857c478bd9Sstevel@tonic-gate thread_free(t); 5867c478bd9Sstevel@tonic-gate kmem_free(lep, sizeof (*lep)); 5877c478bd9Sstevel@tonic-gate lwp = NULL; 5887c478bd9Sstevel@tonic-gate } else { 5897c478bd9Sstevel@tonic-gate mutex_exit(&p->p_lock); 5907c478bd9Sstevel@tonic-gate } 5917c478bd9Sstevel@tonic-gate 5927c478bd9Sstevel@tonic-gate if (old_dir != NULL) { 5937c478bd9Sstevel@tonic-gate kmem_free(old_dir, old_dirsz * sizeof (*old_dir)); 5947c478bd9Sstevel@tonic-gate kmem_free(old_hash, old_hashsz * sizeof (*old_hash)); 5957c478bd9Sstevel@tonic-gate } 5967c478bd9Sstevel@tonic-gate 5977c478bd9Sstevel@tonic-gate DTRACE_PROC1(lwp__create, kthread_t *, t); 5987c478bd9Sstevel@tonic-gate return (lwp); 5997c478bd9Sstevel@tonic-gate } 6007c478bd9Sstevel@tonic-gate 6017c478bd9Sstevel@tonic-gate /* 6027c478bd9Sstevel@tonic-gate * lwp_create_done() is called by the caller of lwp_create() to set the 6037c478bd9Sstevel@tonic-gate * newly-created lwp running after the caller has finished manipulating it. 6047c478bd9Sstevel@tonic-gate */ 6057c478bd9Sstevel@tonic-gate void 6067c478bd9Sstevel@tonic-gate lwp_create_done(kthread_t *t) 6077c478bd9Sstevel@tonic-gate { 6087c478bd9Sstevel@tonic-gate proc_t *p = ttoproc(t); 6097c478bd9Sstevel@tonic-gate 6107c478bd9Sstevel@tonic-gate ASSERT(MUTEX_HELD(&p->p_lock)); 6117c478bd9Sstevel@tonic-gate 6127c478bd9Sstevel@tonic-gate /* 6137c478bd9Sstevel@tonic-gate * We set the TS_CREATE and TS_CSTART flags and call setrun_locked(). 6147c478bd9Sstevel@tonic-gate * (The absence of the TS_CREATE flag prevents the lwp from running 6157c478bd9Sstevel@tonic-gate * until we are finished with it, even if lwp_continue() is called on 6167c478bd9Sstevel@tonic-gate * it by some other lwp in the process or elsewhere in the kernel.) 6177c478bd9Sstevel@tonic-gate */ 6187c478bd9Sstevel@tonic-gate thread_lock(t); 6197c478bd9Sstevel@tonic-gate ASSERT(t->t_state == TS_STOPPED && !(t->t_schedflag & TS_CREATE)); 6207c478bd9Sstevel@tonic-gate /* 6217c478bd9Sstevel@tonic-gate * If TS_CSTART is set, lwp_continue(t) has been called and 6227c478bd9Sstevel@tonic-gate * has already incremented p_lwprcnt; avoid doing this twice. 6237c478bd9Sstevel@tonic-gate */ 6247c478bd9Sstevel@tonic-gate if (!(t->t_schedflag & TS_CSTART)) 6257c478bd9Sstevel@tonic-gate p->p_lwprcnt++; 6267c478bd9Sstevel@tonic-gate t->t_schedflag |= (TS_CSTART | TS_CREATE); 6277c478bd9Sstevel@tonic-gate setrun_locked(t); 6287c478bd9Sstevel@tonic-gate thread_unlock(t); 6297c478bd9Sstevel@tonic-gate } 6307c478bd9Sstevel@tonic-gate 6317c478bd9Sstevel@tonic-gate /* 6327c478bd9Sstevel@tonic-gate * Copy an LWP's active templates, and clear the latest contracts. 6337c478bd9Sstevel@tonic-gate */ 6347c478bd9Sstevel@tonic-gate void 6357c478bd9Sstevel@tonic-gate lwp_ctmpl_copy(klwp_t *dst, klwp_t *src) 6367c478bd9Sstevel@tonic-gate { 6377c478bd9Sstevel@tonic-gate int i; 6387c478bd9Sstevel@tonic-gate 6397c478bd9Sstevel@tonic-gate for (i = 0; i < ct_ntypes; i++) { 6407c478bd9Sstevel@tonic-gate dst->lwp_ct_active[i] = ctmpl_dup(src->lwp_ct_active[i]); 6417c478bd9Sstevel@tonic-gate dst->lwp_ct_latest[i] = NULL; 6427c478bd9Sstevel@tonic-gate } 6437c478bd9Sstevel@tonic-gate } 6447c478bd9Sstevel@tonic-gate 6457c478bd9Sstevel@tonic-gate /* 6467c478bd9Sstevel@tonic-gate * Clear an LWP's contract template state. 6477c478bd9Sstevel@tonic-gate */ 6487c478bd9Sstevel@tonic-gate void 6497c478bd9Sstevel@tonic-gate lwp_ctmpl_clear(klwp_t *lwp) 6507c478bd9Sstevel@tonic-gate { 6517c478bd9Sstevel@tonic-gate ct_template_t *tmpl; 6527c478bd9Sstevel@tonic-gate int i; 6537c478bd9Sstevel@tonic-gate 6547c478bd9Sstevel@tonic-gate for (i = 0; i < ct_ntypes; i++) { 6557c478bd9Sstevel@tonic-gate if ((tmpl = lwp->lwp_ct_active[i]) != NULL) { 6567c478bd9Sstevel@tonic-gate ctmpl_free(tmpl); 6577c478bd9Sstevel@tonic-gate lwp->lwp_ct_active[i] = NULL; 6587c478bd9Sstevel@tonic-gate } 6597c478bd9Sstevel@tonic-gate 6607c478bd9Sstevel@tonic-gate if (lwp->lwp_ct_latest[i] != NULL) { 6617c478bd9Sstevel@tonic-gate contract_rele(lwp->lwp_ct_latest[i]); 6627c478bd9Sstevel@tonic-gate lwp->lwp_ct_latest[i] = NULL; 6637c478bd9Sstevel@tonic-gate } 6647c478bd9Sstevel@tonic-gate } 6657c478bd9Sstevel@tonic-gate } 6667c478bd9Sstevel@tonic-gate 6677c478bd9Sstevel@tonic-gate /* 6687c478bd9Sstevel@tonic-gate * Individual lwp exit. 6697c478bd9Sstevel@tonic-gate * If this is the last lwp, exit the whole process. 6707c478bd9Sstevel@tonic-gate */ 6717c478bd9Sstevel@tonic-gate void 6727c478bd9Sstevel@tonic-gate lwp_exit(void) 6737c478bd9Sstevel@tonic-gate { 6747c478bd9Sstevel@tonic-gate kthread_t *t = curthread; 6757c478bd9Sstevel@tonic-gate klwp_t *lwp = ttolwp(t); 6767c478bd9Sstevel@tonic-gate proc_t *p = ttoproc(t); 6777c478bd9Sstevel@tonic-gate 6787c478bd9Sstevel@tonic-gate ASSERT(MUTEX_HELD(&p->p_lock)); 6797c478bd9Sstevel@tonic-gate 6807c478bd9Sstevel@tonic-gate mutex_exit(&p->p_lock); 6817c478bd9Sstevel@tonic-gate 6827c478bd9Sstevel@tonic-gate #if defined(__sparc) 6837c478bd9Sstevel@tonic-gate /* 6847c478bd9Sstevel@tonic-gate * Ensure that the user stack is fully abandoned.. 6857c478bd9Sstevel@tonic-gate */ 6867c478bd9Sstevel@tonic-gate trash_user_windows(); 6877c478bd9Sstevel@tonic-gate #endif 6887c478bd9Sstevel@tonic-gate 6897c478bd9Sstevel@tonic-gate tsd_exit(); /* free thread specific data */ 6907c478bd9Sstevel@tonic-gate 6917c478bd9Sstevel@tonic-gate kcpc_passivate(); /* Clean up performance counter state */ 6927c478bd9Sstevel@tonic-gate 6937c478bd9Sstevel@tonic-gate pollcleanup(); 6947c478bd9Sstevel@tonic-gate 6957c478bd9Sstevel@tonic-gate if (t->t_door) 6967c478bd9Sstevel@tonic-gate door_slam(); 6977c478bd9Sstevel@tonic-gate 6987c478bd9Sstevel@tonic-gate if (t->t_schedctl != NULL) 6997c478bd9Sstevel@tonic-gate schedctl_lwp_cleanup(t); 7007c478bd9Sstevel@tonic-gate 7017c478bd9Sstevel@tonic-gate if (t->t_upimutex != NULL) 7027c478bd9Sstevel@tonic-gate upimutex_cleanup(); 7037c478bd9Sstevel@tonic-gate 7049acbbeafSnn35248 /* 7059acbbeafSnn35248 * Perform any brand specific exit processing, then release any 7069acbbeafSnn35248 * brand data associated with the lwp 7079acbbeafSnn35248 */ 7089acbbeafSnn35248 if (PROC_IS_BRANDED(p)) 7099acbbeafSnn35248 BROP(p)->b_lwpexit(lwp); 7109acbbeafSnn35248 7117c478bd9Sstevel@tonic-gate mutex_enter(&p->p_lock); 7127c478bd9Sstevel@tonic-gate lwp_cleanup(); 7137c478bd9Sstevel@tonic-gate 7147c478bd9Sstevel@tonic-gate /* 7157c478bd9Sstevel@tonic-gate * When this process is dumping core, its lwps are held here 7167c478bd9Sstevel@tonic-gate * until the core dump is finished. Then exitlwps() is called 7177c478bd9Sstevel@tonic-gate * again to release these lwps so that they can finish exiting. 7187c478bd9Sstevel@tonic-gate */ 7197c478bd9Sstevel@tonic-gate if (p->p_flag & SCOREDUMP) 7207c478bd9Sstevel@tonic-gate stop(PR_SUSPENDED, SUSPEND_NORMAL); 7217c478bd9Sstevel@tonic-gate 7227c478bd9Sstevel@tonic-gate /* 7237c478bd9Sstevel@tonic-gate * Block the process against /proc now that we have really acquired 7247c478bd9Sstevel@tonic-gate * p->p_lock (to decrement p_lwpcnt and manipulate p_tlist at least). 7257c478bd9Sstevel@tonic-gate */ 7267c478bd9Sstevel@tonic-gate prbarrier(p); 7277c478bd9Sstevel@tonic-gate 7287c478bd9Sstevel@tonic-gate /* 7297c478bd9Sstevel@tonic-gate * Call proc_exit() if this is the last non-daemon lwp in the process. 7307c478bd9Sstevel@tonic-gate */ 7317c478bd9Sstevel@tonic-gate if (!(t->t_proc_flag & TP_DAEMON) && 7327c478bd9Sstevel@tonic-gate p->p_lwpcnt == p->p_lwpdaemon + 1) { 7337c478bd9Sstevel@tonic-gate mutex_exit(&p->p_lock); 73497eda132Sraf if (proc_exit(CLD_EXITED, 0) == 0) { 7357c478bd9Sstevel@tonic-gate /* Restarting init. */ 7367c478bd9Sstevel@tonic-gate return; 7377c478bd9Sstevel@tonic-gate } 7387c478bd9Sstevel@tonic-gate 7397c478bd9Sstevel@tonic-gate /* 7407c478bd9Sstevel@tonic-gate * proc_exit() returns a non-zero value when some other 7417c478bd9Sstevel@tonic-gate * lwp got there first. We just have to continue in 7427c478bd9Sstevel@tonic-gate * lwp_exit(). 7437c478bd9Sstevel@tonic-gate */ 7447c478bd9Sstevel@tonic-gate mutex_enter(&p->p_lock); 7457c478bd9Sstevel@tonic-gate ASSERT(curproc->p_flag & SEXITLWPS); 7467c478bd9Sstevel@tonic-gate prbarrier(p); 7477c478bd9Sstevel@tonic-gate } 7487c478bd9Sstevel@tonic-gate 7497c478bd9Sstevel@tonic-gate DTRACE_PROC(lwp__exit); 7507c478bd9Sstevel@tonic-gate 7517c478bd9Sstevel@tonic-gate /* 7527c478bd9Sstevel@tonic-gate * If the lwp is a detached lwp or if the process is exiting, 7537c478bd9Sstevel@tonic-gate * remove (lwp_hash_out()) the lwp from the lwp directory. 7547c478bd9Sstevel@tonic-gate * Otherwise null out the lwp's le_thread pointer in the lwp 7557c478bd9Sstevel@tonic-gate * directory so that other threads will see it as a zombie lwp. 7567c478bd9Sstevel@tonic-gate */ 7577c478bd9Sstevel@tonic-gate prlwpexit(t); /* notify /proc */ 7587c478bd9Sstevel@tonic-gate if (!(t->t_proc_flag & TP_TWAIT) || (p->p_flag & SEXITLWPS)) 7597c478bd9Sstevel@tonic-gate lwp_hash_out(p, t->t_tid); 7607c478bd9Sstevel@tonic-gate else { 7617c478bd9Sstevel@tonic-gate ASSERT(!(t->t_proc_flag & TP_DAEMON)); 7627c478bd9Sstevel@tonic-gate p->p_lwpdir[t->t_dslot].ld_entry->le_thread = NULL; 7637c478bd9Sstevel@tonic-gate p->p_zombcnt++; 7647c478bd9Sstevel@tonic-gate cv_broadcast(&p->p_lwpexit); 7657c478bd9Sstevel@tonic-gate } 7667c478bd9Sstevel@tonic-gate if (t->t_proc_flag & TP_DAEMON) { 7677c478bd9Sstevel@tonic-gate p->p_lwpdaemon--; 7687c478bd9Sstevel@tonic-gate t->t_proc_flag &= ~TP_DAEMON; 7697c478bd9Sstevel@tonic-gate } 7707c478bd9Sstevel@tonic-gate t->t_proc_flag &= ~TP_TWAIT; 7717c478bd9Sstevel@tonic-gate 7727c478bd9Sstevel@tonic-gate /* 7737c478bd9Sstevel@tonic-gate * Maintain accurate lwp count for task.max-lwps resource control. 7747c478bd9Sstevel@tonic-gate */ 7757c478bd9Sstevel@tonic-gate mutex_enter(&p->p_zone->zone_nlwps_lock); 7767c478bd9Sstevel@tonic-gate p->p_task->tk_nlwps--; 7777c478bd9Sstevel@tonic-gate p->p_task->tk_proj->kpj_nlwps--; 7787c478bd9Sstevel@tonic-gate p->p_zone->zone_nlwps--; 7797c478bd9Sstevel@tonic-gate mutex_exit(&p->p_zone->zone_nlwps_lock); 7807c478bd9Sstevel@tonic-gate 7817c478bd9Sstevel@tonic-gate CL_EXIT(t); /* tell the scheduler that t is exiting */ 7827c478bd9Sstevel@tonic-gate ASSERT(p->p_lwpcnt != 0); 7837c478bd9Sstevel@tonic-gate p->p_lwpcnt--; 7847c478bd9Sstevel@tonic-gate 7857c478bd9Sstevel@tonic-gate /* 7867c478bd9Sstevel@tonic-gate * If all remaining non-daemon lwps are waiting in lwp_wait(), 7877c478bd9Sstevel@tonic-gate * wake them up so someone can return EDEADLK. 7887c478bd9Sstevel@tonic-gate * (See the block comment preceeding lwp_wait().) 7897c478bd9Sstevel@tonic-gate */ 7907c478bd9Sstevel@tonic-gate if (p->p_lwpcnt == p->p_lwpdaemon + (p->p_lwpwait - p->p_lwpdwait)) 7917c478bd9Sstevel@tonic-gate cv_broadcast(&p->p_lwpexit); 7927c478bd9Sstevel@tonic-gate 7937c478bd9Sstevel@tonic-gate t->t_proc_flag |= TP_LWPEXIT; 7947c478bd9Sstevel@tonic-gate term_mstate(t); 795c97ad5cdSakolb 7967c478bd9Sstevel@tonic-gate #ifndef NPROBE 7977c478bd9Sstevel@tonic-gate /* Kernel probe */ 7987c478bd9Sstevel@tonic-gate if (t->t_tnf_tpdp) 7997c478bd9Sstevel@tonic-gate tnf_thread_exit(); 8007c478bd9Sstevel@tonic-gate #endif /* NPROBE */ 8017c478bd9Sstevel@tonic-gate 8027c478bd9Sstevel@tonic-gate t->t_forw->t_back = t->t_back; 8037c478bd9Sstevel@tonic-gate t->t_back->t_forw = t->t_forw; 8047c478bd9Sstevel@tonic-gate if (t == p->p_tlist) 8057c478bd9Sstevel@tonic-gate p->p_tlist = t->t_forw; 8067c478bd9Sstevel@tonic-gate 8077c478bd9Sstevel@tonic-gate /* 8087c478bd9Sstevel@tonic-gate * Clean up the signal state. 8097c478bd9Sstevel@tonic-gate */ 8107c478bd9Sstevel@tonic-gate if (t->t_sigqueue != NULL) 8117c478bd9Sstevel@tonic-gate sigdelq(p, t, 0); 8127c478bd9Sstevel@tonic-gate if (lwp->lwp_curinfo != NULL) { 8137c478bd9Sstevel@tonic-gate siginfofree(lwp->lwp_curinfo); 8147c478bd9Sstevel@tonic-gate lwp->lwp_curinfo = NULL; 8157c478bd9Sstevel@tonic-gate } 8167c478bd9Sstevel@tonic-gate 8177c478bd9Sstevel@tonic-gate thread_rele(t); 8187c478bd9Sstevel@tonic-gate 8197c478bd9Sstevel@tonic-gate /* 8207c478bd9Sstevel@tonic-gate * Terminated lwps are associated with process zero and are put onto 8217c478bd9Sstevel@tonic-gate * death-row by resume(). Avoid preemption after resetting t->t_procp. 8227c478bd9Sstevel@tonic-gate */ 8237c478bd9Sstevel@tonic-gate t->t_preempt++; 8240baeff3dSrab 8250baeff3dSrab if (t->t_ctx != NULL) 8260baeff3dSrab exitctx(t); 8270baeff3dSrab if (p->p_pctx != NULL) 8280baeff3dSrab exitpctx(p); 8290baeff3dSrab 8307c478bd9Sstevel@tonic-gate t->t_procp = &p0; 8317c478bd9Sstevel@tonic-gate 8327c478bd9Sstevel@tonic-gate /* 8337c478bd9Sstevel@tonic-gate * Notify the HAT about the change of address space 8347c478bd9Sstevel@tonic-gate */ 8357c478bd9Sstevel@tonic-gate hat_thread_exit(t); 8367c478bd9Sstevel@tonic-gate /* 8377c478bd9Sstevel@tonic-gate * When this is the last running lwp in this process and some lwp is 8387c478bd9Sstevel@tonic-gate * waiting for this condition to become true, or this thread was being 8397c478bd9Sstevel@tonic-gate * suspended, then the waiting lwp is awakened. 8407c478bd9Sstevel@tonic-gate * 8417c478bd9Sstevel@tonic-gate * Also, if the process is exiting, we may have a thread waiting in 8427c478bd9Sstevel@tonic-gate * exitlwps() that needs to be notified. 8437c478bd9Sstevel@tonic-gate */ 8447c478bd9Sstevel@tonic-gate if (--p->p_lwprcnt == 0 || (t->t_proc_flag & TP_HOLDLWP) || 8457c478bd9Sstevel@tonic-gate (p->p_flag & SEXITLWPS)) 8467c478bd9Sstevel@tonic-gate cv_broadcast(&p->p_holdlwps); 8477c478bd9Sstevel@tonic-gate 8487c478bd9Sstevel@tonic-gate /* 8497c478bd9Sstevel@tonic-gate * Need to drop p_lock so we can reacquire pidlock. 8507c478bd9Sstevel@tonic-gate */ 8517c478bd9Sstevel@tonic-gate mutex_exit(&p->p_lock); 8527c478bd9Sstevel@tonic-gate mutex_enter(&pidlock); 8537c478bd9Sstevel@tonic-gate 8547c478bd9Sstevel@tonic-gate ASSERT(t != t->t_next); /* t0 never exits */ 8557c478bd9Sstevel@tonic-gate t->t_next->t_prev = t->t_prev; 8567c478bd9Sstevel@tonic-gate t->t_prev->t_next = t->t_next; 8577c478bd9Sstevel@tonic-gate cv_broadcast(&t->t_joincv); /* wake up anyone in thread_join */ 8587c478bd9Sstevel@tonic-gate mutex_exit(&pidlock); 8597c478bd9Sstevel@tonic-gate 8607c478bd9Sstevel@tonic-gate lwp_pcb_exit(); 8617c478bd9Sstevel@tonic-gate 8627c478bd9Sstevel@tonic-gate t->t_state = TS_ZOMB; 8637c478bd9Sstevel@tonic-gate swtch_from_zombie(); 8647c478bd9Sstevel@tonic-gate /* never returns */ 8657c478bd9Sstevel@tonic-gate } 8667c478bd9Sstevel@tonic-gate 8677c478bd9Sstevel@tonic-gate 8687c478bd9Sstevel@tonic-gate /* 8697c478bd9Sstevel@tonic-gate * Cleanup function for an exiting lwp. 8707c478bd9Sstevel@tonic-gate * Called both from lwp_exit() and from proc_exit(). 8717c478bd9Sstevel@tonic-gate * p->p_lock is repeatedly released and grabbed in this function. 8727c478bd9Sstevel@tonic-gate */ 8737c478bd9Sstevel@tonic-gate void 8747c478bd9Sstevel@tonic-gate lwp_cleanup(void) 8757c478bd9Sstevel@tonic-gate { 8767c478bd9Sstevel@tonic-gate kthread_t *t = curthread; 8777c478bd9Sstevel@tonic-gate proc_t *p = ttoproc(t); 8787c478bd9Sstevel@tonic-gate 8797c478bd9Sstevel@tonic-gate ASSERT(MUTEX_HELD(&p->p_lock)); 8807c478bd9Sstevel@tonic-gate 8817c478bd9Sstevel@tonic-gate /* untimeout any lwp-bound realtime timers */ 8827c478bd9Sstevel@tonic-gate if (p->p_itimer != NULL) 8837c478bd9Sstevel@tonic-gate timer_lwpexit(); 8847c478bd9Sstevel@tonic-gate 8857c478bd9Sstevel@tonic-gate /* 8867c478bd9Sstevel@tonic-gate * If this is the /proc agent lwp that is exiting, readjust p_lwpid 8877c478bd9Sstevel@tonic-gate * so it appears that the agent never existed, and clear p_agenttp. 8887c478bd9Sstevel@tonic-gate */ 8897c478bd9Sstevel@tonic-gate if (t == p->p_agenttp) { 8907c478bd9Sstevel@tonic-gate ASSERT(t->t_tid == p->p_lwpid); 8917c478bd9Sstevel@tonic-gate p->p_lwpid--; 8927c478bd9Sstevel@tonic-gate p->p_agenttp = NULL; 8937c478bd9Sstevel@tonic-gate } 8947c478bd9Sstevel@tonic-gate 8957c478bd9Sstevel@tonic-gate /* 8967c478bd9Sstevel@tonic-gate * Do lgroup bookkeeping to account for thread exiting. 8977c478bd9Sstevel@tonic-gate */ 8987c478bd9Sstevel@tonic-gate kpreempt_disable(); 8997c478bd9Sstevel@tonic-gate lgrp_move_thread(t, NULL, 1); 9002cb27123Saguzovsk if (t->t_tid == 1) { 9012cb27123Saguzovsk p->p_t1_lgrpid = LGRP_NONE; 9022cb27123Saguzovsk } 9037c478bd9Sstevel@tonic-gate kpreempt_enable(); 9047c478bd9Sstevel@tonic-gate 9057c478bd9Sstevel@tonic-gate lwp_ctmpl_clear(ttolwp(t)); 9067c478bd9Sstevel@tonic-gate } 9077c478bd9Sstevel@tonic-gate 9087c478bd9Sstevel@tonic-gate int 9097c478bd9Sstevel@tonic-gate lwp_suspend(kthread_t *t) 9107c478bd9Sstevel@tonic-gate { 9117c478bd9Sstevel@tonic-gate int tid; 9127c478bd9Sstevel@tonic-gate proc_t *p = ttoproc(t); 9137c478bd9Sstevel@tonic-gate 9147c478bd9Sstevel@tonic-gate ASSERT(MUTEX_HELD(&p->p_lock)); 9157c478bd9Sstevel@tonic-gate 9167c478bd9Sstevel@tonic-gate /* 9177c478bd9Sstevel@tonic-gate * Set the thread's TP_HOLDLWP flag so it will stop in holdlwp(). 9187c478bd9Sstevel@tonic-gate * If an lwp is stopping itself, there is no need to wait. 9197c478bd9Sstevel@tonic-gate */ 9208132eb48Sraf top: 9217c478bd9Sstevel@tonic-gate t->t_proc_flag |= TP_HOLDLWP; 9227c478bd9Sstevel@tonic-gate if (t == curthread) { 9237c478bd9Sstevel@tonic-gate t->t_sig_check = 1; 9247c478bd9Sstevel@tonic-gate } else { 9257c478bd9Sstevel@tonic-gate /* 9267c478bd9Sstevel@tonic-gate * Make sure the lwp stops promptly. 9277c478bd9Sstevel@tonic-gate */ 9287c478bd9Sstevel@tonic-gate thread_lock(t); 9297c478bd9Sstevel@tonic-gate t->t_sig_check = 1; 9307c478bd9Sstevel@tonic-gate /* 9317c478bd9Sstevel@tonic-gate * XXX Should use virtual stop like /proc does instead of 9327c478bd9Sstevel@tonic-gate * XXX waking the thread to get it to stop. 9337c478bd9Sstevel@tonic-gate */ 934c97ad5cdSakolb if (ISWAKEABLE(t) || ISWAITING(t)) { 9357c478bd9Sstevel@tonic-gate setrun_locked(t); 936c97ad5cdSakolb } else if (t->t_state == TS_ONPROC && t->t_cpu != CPU) { 9377c478bd9Sstevel@tonic-gate poke_cpu(t->t_cpu->cpu_id); 938c97ad5cdSakolb } 939c97ad5cdSakolb 9407c478bd9Sstevel@tonic-gate tid = t->t_tid; /* remember thread ID */ 9417c478bd9Sstevel@tonic-gate /* 9427c478bd9Sstevel@tonic-gate * Wait for lwp to stop 9437c478bd9Sstevel@tonic-gate */ 9447c478bd9Sstevel@tonic-gate while (!SUSPENDED(t)) { 9457c478bd9Sstevel@tonic-gate /* 9467c478bd9Sstevel@tonic-gate * Drop the thread lock before waiting and reacquire it 9477c478bd9Sstevel@tonic-gate * afterwards, so the thread can change its t_state 9487c478bd9Sstevel@tonic-gate * field. 9497c478bd9Sstevel@tonic-gate */ 9507c478bd9Sstevel@tonic-gate thread_unlock(t); 9517c478bd9Sstevel@tonic-gate 9527c478bd9Sstevel@tonic-gate /* 9537c478bd9Sstevel@tonic-gate * Check if aborted by exitlwps(). 9547c478bd9Sstevel@tonic-gate */ 9557c478bd9Sstevel@tonic-gate if (p->p_flag & SEXITLWPS) 9567c478bd9Sstevel@tonic-gate lwp_exit(); 9577c478bd9Sstevel@tonic-gate 9587c478bd9Sstevel@tonic-gate /* 9597c478bd9Sstevel@tonic-gate * Cooperate with jobcontrol signals and /proc stopping 9607c478bd9Sstevel@tonic-gate * by calling cv_wait_sig() to wait for the target 9617c478bd9Sstevel@tonic-gate * lwp to stop. Just using cv_wait() can lead to 9627c478bd9Sstevel@tonic-gate * deadlock because, if some other lwp has stopped 9637c478bd9Sstevel@tonic-gate * by either of these mechanisms, then p_lwprcnt will 9647c478bd9Sstevel@tonic-gate * never become zero if we do a cv_wait(). 9657c478bd9Sstevel@tonic-gate */ 9667c478bd9Sstevel@tonic-gate if (!cv_wait_sig(&p->p_holdlwps, &p->p_lock)) 9677c478bd9Sstevel@tonic-gate return (EINTR); 9687c478bd9Sstevel@tonic-gate 9697c478bd9Sstevel@tonic-gate /* 9707c478bd9Sstevel@tonic-gate * Check to see if thread died while we were 9717c478bd9Sstevel@tonic-gate * waiting for it to suspend. 9727c478bd9Sstevel@tonic-gate */ 9737c478bd9Sstevel@tonic-gate if (idtot(p, tid) == NULL) 9747c478bd9Sstevel@tonic-gate return (ESRCH); 9757c478bd9Sstevel@tonic-gate 9767c478bd9Sstevel@tonic-gate thread_lock(t); 9777c478bd9Sstevel@tonic-gate /* 9788132eb48Sraf * If the TP_HOLDLWP flag went away, lwp_continue() 9798132eb48Sraf * or vfork() must have been called while we were 9808132eb48Sraf * waiting, so start over again. 9817c478bd9Sstevel@tonic-gate */ 9827c478bd9Sstevel@tonic-gate if ((t->t_proc_flag & TP_HOLDLWP) == 0) { 9837c478bd9Sstevel@tonic-gate thread_unlock(t); 9848132eb48Sraf goto top; 9857c478bd9Sstevel@tonic-gate } 9867c478bd9Sstevel@tonic-gate } 9877c478bd9Sstevel@tonic-gate thread_unlock(t); 9887c478bd9Sstevel@tonic-gate } 9897c478bd9Sstevel@tonic-gate return (0); 9907c478bd9Sstevel@tonic-gate } 9917c478bd9Sstevel@tonic-gate 9927c478bd9Sstevel@tonic-gate /* 9937c478bd9Sstevel@tonic-gate * continue a lwp that's been stopped by lwp_suspend(). 9947c478bd9Sstevel@tonic-gate */ 9957c478bd9Sstevel@tonic-gate void 9967c478bd9Sstevel@tonic-gate lwp_continue(kthread_t *t) 9977c478bd9Sstevel@tonic-gate { 9987c478bd9Sstevel@tonic-gate proc_t *p = ttoproc(t); 9997c478bd9Sstevel@tonic-gate int was_suspended = t->t_proc_flag & TP_HOLDLWP; 10007c478bd9Sstevel@tonic-gate 10017c478bd9Sstevel@tonic-gate ASSERT(MUTEX_HELD(&p->p_lock)); 10027c478bd9Sstevel@tonic-gate 10037c478bd9Sstevel@tonic-gate t->t_proc_flag &= ~TP_HOLDLWP; 10047c478bd9Sstevel@tonic-gate thread_lock(t); 10057c478bd9Sstevel@tonic-gate if (SUSPENDED(t) && 10067c478bd9Sstevel@tonic-gate !(p->p_flag & (SHOLDFORK | SHOLDFORK1 | SHOLDWATCH))) { 10077c478bd9Sstevel@tonic-gate p->p_lwprcnt++; 10087c478bd9Sstevel@tonic-gate t->t_schedflag |= TS_CSTART; 10097c478bd9Sstevel@tonic-gate setrun_locked(t); 10107c478bd9Sstevel@tonic-gate } 10117c478bd9Sstevel@tonic-gate thread_unlock(t); 10127c478bd9Sstevel@tonic-gate /* 10137c478bd9Sstevel@tonic-gate * Wakeup anyone waiting for this thread to be suspended 10147c478bd9Sstevel@tonic-gate */ 10157c478bd9Sstevel@tonic-gate if (was_suspended) 10167c478bd9Sstevel@tonic-gate cv_broadcast(&p->p_holdlwps); 10177c478bd9Sstevel@tonic-gate } 10187c478bd9Sstevel@tonic-gate 10197c478bd9Sstevel@tonic-gate /* 10207c478bd9Sstevel@tonic-gate * ******************************** 10217c478bd9Sstevel@tonic-gate * Miscellaneous lwp routines * 10227c478bd9Sstevel@tonic-gate * ******************************** 10237c478bd9Sstevel@tonic-gate */ 10247c478bd9Sstevel@tonic-gate /* 10257c478bd9Sstevel@tonic-gate * When a process is undergoing a forkall(), its p_flag is set to SHOLDFORK. 10267c478bd9Sstevel@tonic-gate * This will cause the process's lwps to stop at a hold point. A hold 10277c478bd9Sstevel@tonic-gate * point is where a kernel thread has a flat stack. This is at the 10287c478bd9Sstevel@tonic-gate * return from a system call and at the return from a user level trap. 10297c478bd9Sstevel@tonic-gate * 10307c478bd9Sstevel@tonic-gate * When a process is undergoing a fork1() or vfork(), its p_flag is set to 10317c478bd9Sstevel@tonic-gate * SHOLDFORK1. This will cause the process's lwps to stop at a modified 10327c478bd9Sstevel@tonic-gate * hold point. The lwps in the process are not being cloned, so they 10337c478bd9Sstevel@tonic-gate * are held at the usual hold points and also within issig_forreal(). 10347c478bd9Sstevel@tonic-gate * This has the side-effect that their system calls do not return 10357c478bd9Sstevel@tonic-gate * showing EINTR. 10367c478bd9Sstevel@tonic-gate * 10377c478bd9Sstevel@tonic-gate * An lwp can also be held. This is identified by the TP_HOLDLWP flag on 10387c478bd9Sstevel@tonic-gate * the thread. The TP_HOLDLWP flag is set in lwp_suspend(), where the active 10397c478bd9Sstevel@tonic-gate * lwp is waiting for the target lwp to be stopped. 10407c478bd9Sstevel@tonic-gate */ 10417c478bd9Sstevel@tonic-gate void 10427c478bd9Sstevel@tonic-gate holdlwp(void) 10437c478bd9Sstevel@tonic-gate { 10447c478bd9Sstevel@tonic-gate proc_t *p = curproc; 10457c478bd9Sstevel@tonic-gate kthread_t *t = curthread; 10467c478bd9Sstevel@tonic-gate 10477c478bd9Sstevel@tonic-gate mutex_enter(&p->p_lock); 10487c478bd9Sstevel@tonic-gate /* 10497c478bd9Sstevel@tonic-gate * Don't terminate immediately if the process is dumping core. 10507c478bd9Sstevel@tonic-gate * Once the process has dumped core, all lwps are terminated. 10517c478bd9Sstevel@tonic-gate */ 10527c478bd9Sstevel@tonic-gate if (!(p->p_flag & SCOREDUMP)) { 10537c478bd9Sstevel@tonic-gate if ((p->p_flag & SEXITLWPS) || (t->t_proc_flag & TP_EXITLWP)) 10547c478bd9Sstevel@tonic-gate lwp_exit(); 10557c478bd9Sstevel@tonic-gate } 10567c478bd9Sstevel@tonic-gate if (!(ISHOLD(p)) && !(p->p_flag & (SHOLDFORK1 | SHOLDWATCH))) { 10577c478bd9Sstevel@tonic-gate mutex_exit(&p->p_lock); 10587c478bd9Sstevel@tonic-gate return; 10597c478bd9Sstevel@tonic-gate } 10607c478bd9Sstevel@tonic-gate /* 10617c478bd9Sstevel@tonic-gate * stop() decrements p->p_lwprcnt and cv_signal()s &p->p_holdlwps 10627c478bd9Sstevel@tonic-gate * when p->p_lwprcnt becomes zero. 10637c478bd9Sstevel@tonic-gate */ 10647c478bd9Sstevel@tonic-gate stop(PR_SUSPENDED, SUSPEND_NORMAL); 10657c478bd9Sstevel@tonic-gate if (p->p_flag & SEXITLWPS) 10667c478bd9Sstevel@tonic-gate lwp_exit(); 10677c478bd9Sstevel@tonic-gate mutex_exit(&p->p_lock); 10687c478bd9Sstevel@tonic-gate } 10697c478bd9Sstevel@tonic-gate 10707c478bd9Sstevel@tonic-gate /* 10717c478bd9Sstevel@tonic-gate * Have all lwps within the process hold at a point where they are 10727c478bd9Sstevel@tonic-gate * cloneable (SHOLDFORK) or just safe w.r.t. fork1 (SHOLDFORK1). 10737c478bd9Sstevel@tonic-gate */ 10747c478bd9Sstevel@tonic-gate int 10757c478bd9Sstevel@tonic-gate holdlwps(int holdflag) 10767c478bd9Sstevel@tonic-gate { 10777c478bd9Sstevel@tonic-gate proc_t *p = curproc; 10787c478bd9Sstevel@tonic-gate 10797c478bd9Sstevel@tonic-gate ASSERT(holdflag == SHOLDFORK || holdflag == SHOLDFORK1); 10807c478bd9Sstevel@tonic-gate mutex_enter(&p->p_lock); 10817c478bd9Sstevel@tonic-gate schedctl_finish_sigblock(curthread); 10827c478bd9Sstevel@tonic-gate again: 10837c478bd9Sstevel@tonic-gate while (p->p_flag & (SEXITLWPS | SHOLDFORK | SHOLDFORK1 | SHOLDWATCH)) { 10847c478bd9Sstevel@tonic-gate /* 10857c478bd9Sstevel@tonic-gate * If another lwp is doing a forkall() or proc_exit(), bail out. 10867c478bd9Sstevel@tonic-gate */ 10877c478bd9Sstevel@tonic-gate if (p->p_flag & (SEXITLWPS | SHOLDFORK)) { 10887c478bd9Sstevel@tonic-gate mutex_exit(&p->p_lock); 10897c478bd9Sstevel@tonic-gate return (0); 10907c478bd9Sstevel@tonic-gate } 10917c478bd9Sstevel@tonic-gate /* 10927c478bd9Sstevel@tonic-gate * Another lwp is doing a fork1() or is undergoing 10937c478bd9Sstevel@tonic-gate * watchpoint activity. We hold here for it to complete. 10947c478bd9Sstevel@tonic-gate */ 10957c478bd9Sstevel@tonic-gate stop(PR_SUSPENDED, SUSPEND_NORMAL); 10967c478bd9Sstevel@tonic-gate } 10977c478bd9Sstevel@tonic-gate p->p_flag |= holdflag; 10987c478bd9Sstevel@tonic-gate pokelwps(p); 10997c478bd9Sstevel@tonic-gate --p->p_lwprcnt; 11007c478bd9Sstevel@tonic-gate /* 11017c478bd9Sstevel@tonic-gate * Wait for the process to become quiescent (p->p_lwprcnt == 0). 11027c478bd9Sstevel@tonic-gate */ 11037c478bd9Sstevel@tonic-gate while (p->p_lwprcnt > 0) { 11047c478bd9Sstevel@tonic-gate /* 11057c478bd9Sstevel@tonic-gate * Check if aborted by exitlwps(). 11067c478bd9Sstevel@tonic-gate * Also check if SHOLDWATCH is set; it takes precedence. 11077c478bd9Sstevel@tonic-gate */ 11087c478bd9Sstevel@tonic-gate if (p->p_flag & (SEXITLWPS | SHOLDWATCH)) { 11097c478bd9Sstevel@tonic-gate p->p_lwprcnt++; 11107c478bd9Sstevel@tonic-gate p->p_flag &= ~holdflag; 11117c478bd9Sstevel@tonic-gate cv_broadcast(&p->p_holdlwps); 11127c478bd9Sstevel@tonic-gate goto again; 11137c478bd9Sstevel@tonic-gate } 11147c478bd9Sstevel@tonic-gate /* 11157c478bd9Sstevel@tonic-gate * Cooperate with jobcontrol signals and /proc stopping. 11167c478bd9Sstevel@tonic-gate * If some other lwp has stopped by either of these 11177c478bd9Sstevel@tonic-gate * mechanisms, then p_lwprcnt will never become zero 11187c478bd9Sstevel@tonic-gate * and the process will appear deadlocked unless we 11197c478bd9Sstevel@tonic-gate * stop here in sympathy with the other lwp before 11207c478bd9Sstevel@tonic-gate * doing the cv_wait() below. 11217c478bd9Sstevel@tonic-gate * 11227c478bd9Sstevel@tonic-gate * If the other lwp stops after we do the cv_wait(), it 11237c478bd9Sstevel@tonic-gate * will wake us up to loop around and do the sympathy stop. 11247c478bd9Sstevel@tonic-gate * 11257c478bd9Sstevel@tonic-gate * Since stop() drops p->p_lock, we must start from 11267c478bd9Sstevel@tonic-gate * the top again on returning from stop(). 11277c478bd9Sstevel@tonic-gate */ 11287c478bd9Sstevel@tonic-gate if (p->p_stopsig | (curthread->t_proc_flag & TP_PRSTOP)) { 11297c478bd9Sstevel@tonic-gate int whystop = p->p_stopsig? PR_JOBCONTROL : 11307c478bd9Sstevel@tonic-gate PR_REQUESTED; 11317c478bd9Sstevel@tonic-gate p->p_lwprcnt++; 11327c478bd9Sstevel@tonic-gate p->p_flag &= ~holdflag; 11337c478bd9Sstevel@tonic-gate stop(whystop, p->p_stopsig); 11347c478bd9Sstevel@tonic-gate goto again; 11357c478bd9Sstevel@tonic-gate } 11367c478bd9Sstevel@tonic-gate cv_wait(&p->p_holdlwps, &p->p_lock); 11377c478bd9Sstevel@tonic-gate } 11387c478bd9Sstevel@tonic-gate p->p_lwprcnt++; 11397c478bd9Sstevel@tonic-gate p->p_flag &= ~holdflag; 11407c478bd9Sstevel@tonic-gate mutex_exit(&p->p_lock); 11417c478bd9Sstevel@tonic-gate return (1); 11427c478bd9Sstevel@tonic-gate } 11437c478bd9Sstevel@tonic-gate 11447c478bd9Sstevel@tonic-gate /* 11457c478bd9Sstevel@tonic-gate * See comments for holdwatch(), below. 11467c478bd9Sstevel@tonic-gate */ 11477c478bd9Sstevel@tonic-gate static int 11487c478bd9Sstevel@tonic-gate holdcheck(int clearflags) 11497c478bd9Sstevel@tonic-gate { 11507c478bd9Sstevel@tonic-gate proc_t *p = curproc; 11517c478bd9Sstevel@tonic-gate 11527c478bd9Sstevel@tonic-gate /* 11537c478bd9Sstevel@tonic-gate * If we are trying to exit, that takes precedence over anything else. 11547c478bd9Sstevel@tonic-gate */ 11557c478bd9Sstevel@tonic-gate if (p->p_flag & SEXITLWPS) { 11567c478bd9Sstevel@tonic-gate p->p_lwprcnt++; 11577c478bd9Sstevel@tonic-gate p->p_flag &= ~clearflags; 11587c478bd9Sstevel@tonic-gate lwp_exit(); 11597c478bd9Sstevel@tonic-gate } 11607c478bd9Sstevel@tonic-gate 11617c478bd9Sstevel@tonic-gate /* 11627c478bd9Sstevel@tonic-gate * If another thread is calling fork1(), stop the current thread so the 11637c478bd9Sstevel@tonic-gate * other can complete. 11647c478bd9Sstevel@tonic-gate */ 11657c478bd9Sstevel@tonic-gate if (p->p_flag & SHOLDFORK1) { 11667c478bd9Sstevel@tonic-gate p->p_lwprcnt++; 11677c478bd9Sstevel@tonic-gate stop(PR_SUSPENDED, SUSPEND_NORMAL); 11687c478bd9Sstevel@tonic-gate if (p->p_flag & SEXITLWPS) { 11697c478bd9Sstevel@tonic-gate p->p_flag &= ~clearflags; 11707c478bd9Sstevel@tonic-gate lwp_exit(); 11717c478bd9Sstevel@tonic-gate } 11727c478bd9Sstevel@tonic-gate return (-1); 11737c478bd9Sstevel@tonic-gate } 11747c478bd9Sstevel@tonic-gate 11757c478bd9Sstevel@tonic-gate /* 11767c478bd9Sstevel@tonic-gate * If another thread is calling fork(), then indicate we are doing 11777c478bd9Sstevel@tonic-gate * watchpoint activity. This will cause holdlwps() above to stop the 11787c478bd9Sstevel@tonic-gate * forking thread, at which point we can continue with watchpoint 11797c478bd9Sstevel@tonic-gate * activity. 11807c478bd9Sstevel@tonic-gate */ 11817c478bd9Sstevel@tonic-gate if (p->p_flag & SHOLDFORK) { 11827c478bd9Sstevel@tonic-gate p->p_lwprcnt++; 11837c478bd9Sstevel@tonic-gate while (p->p_flag & SHOLDFORK) { 11847c478bd9Sstevel@tonic-gate p->p_flag |= SHOLDWATCH; 11857c478bd9Sstevel@tonic-gate cv_broadcast(&p->p_holdlwps); 11867c478bd9Sstevel@tonic-gate cv_wait(&p->p_holdlwps, &p->p_lock); 11877c478bd9Sstevel@tonic-gate p->p_flag &= ~SHOLDWATCH; 11887c478bd9Sstevel@tonic-gate } 11897c478bd9Sstevel@tonic-gate return (-1); 11907c478bd9Sstevel@tonic-gate } 11917c478bd9Sstevel@tonic-gate 11927c478bd9Sstevel@tonic-gate return (0); 11937c478bd9Sstevel@tonic-gate } 11947c478bd9Sstevel@tonic-gate 11957c478bd9Sstevel@tonic-gate /* 11967c478bd9Sstevel@tonic-gate * Stop all lwps within the process, holding themselves in the kernel while the 11977c478bd9Sstevel@tonic-gate * active lwp undergoes watchpoint activity. This is more complicated than 11987c478bd9Sstevel@tonic-gate * expected because stop() relies on calling holdwatch() in order to copyin data 11997c478bd9Sstevel@tonic-gate * from the user's address space. A double barrier is used to prevent an 12007c478bd9Sstevel@tonic-gate * infinite loop. 12017c478bd9Sstevel@tonic-gate * 12027c478bd9Sstevel@tonic-gate * o The first thread into holdwatch() is the 'master' thread and does 12037c478bd9Sstevel@tonic-gate * the following: 12047c478bd9Sstevel@tonic-gate * 12057c478bd9Sstevel@tonic-gate * - Sets SHOLDWATCH on the current process 12067c478bd9Sstevel@tonic-gate * - Sets TP_WATCHSTOP on the current thread 12077c478bd9Sstevel@tonic-gate * - Waits for all threads to be either stopped or have 12087c478bd9Sstevel@tonic-gate * TP_WATCHSTOP set. 12097c478bd9Sstevel@tonic-gate * - Sets the SWATCHOK flag on the process 12107c478bd9Sstevel@tonic-gate * - Unsets TP_WATCHSTOP 12117c478bd9Sstevel@tonic-gate * - Waits for the other threads to completely stop 12127c478bd9Sstevel@tonic-gate * - Unsets SWATCHOK 12137c478bd9Sstevel@tonic-gate * 12147c478bd9Sstevel@tonic-gate * o If SHOLDWATCH is already set when we enter this function, then another 12157c478bd9Sstevel@tonic-gate * thread is already trying to stop this thread. This 'slave' thread 12167c478bd9Sstevel@tonic-gate * does the following: 12177c478bd9Sstevel@tonic-gate * 12187c478bd9Sstevel@tonic-gate * - Sets TP_WATCHSTOP on the current thread 12197c478bd9Sstevel@tonic-gate * - Waits for SWATCHOK flag to be set 12207c478bd9Sstevel@tonic-gate * - Calls stop() 12217c478bd9Sstevel@tonic-gate * 12227c478bd9Sstevel@tonic-gate * o If SWATCHOK is set on the process, then this function immediately 12237c478bd9Sstevel@tonic-gate * returns, as we must have been called via stop(). 12247c478bd9Sstevel@tonic-gate * 12257c478bd9Sstevel@tonic-gate * In addition, there are other flags that take precedence over SHOLDWATCH: 12267c478bd9Sstevel@tonic-gate * 12277c478bd9Sstevel@tonic-gate * o If SEXITLWPS is set, exit immediately. 12287c478bd9Sstevel@tonic-gate * 12297c478bd9Sstevel@tonic-gate * o If SHOLDFORK1 is set, wait for fork1() to complete. 12307c478bd9Sstevel@tonic-gate * 12317c478bd9Sstevel@tonic-gate * o If SHOLDFORK is set, then watchpoint activity takes precedence In this 12327c478bd9Sstevel@tonic-gate * case, set SHOLDWATCH, signalling the forking thread to stop first. 12337c478bd9Sstevel@tonic-gate * 12347c478bd9Sstevel@tonic-gate * o If the process is being stopped via /proc (TP_PRSTOP is set), then we 12357c478bd9Sstevel@tonic-gate * stop the current thread. 12367c478bd9Sstevel@tonic-gate * 12377c478bd9Sstevel@tonic-gate * Returns 0 if all threads have been quiesced. Returns non-zero if not all 12387c478bd9Sstevel@tonic-gate * threads were stopped, or the list of watched pages has changed. 12397c478bd9Sstevel@tonic-gate */ 12407c478bd9Sstevel@tonic-gate int 12417c478bd9Sstevel@tonic-gate holdwatch(void) 12427c478bd9Sstevel@tonic-gate { 12437c478bd9Sstevel@tonic-gate proc_t *p = curproc; 12447c478bd9Sstevel@tonic-gate kthread_t *t = curthread; 12457c478bd9Sstevel@tonic-gate int ret = 0; 12467c478bd9Sstevel@tonic-gate 12477c478bd9Sstevel@tonic-gate mutex_enter(&p->p_lock); 12487c478bd9Sstevel@tonic-gate 12497c478bd9Sstevel@tonic-gate p->p_lwprcnt--; 12507c478bd9Sstevel@tonic-gate 12517c478bd9Sstevel@tonic-gate /* 12527c478bd9Sstevel@tonic-gate * Check for bail-out conditions as outlined above. 12537c478bd9Sstevel@tonic-gate */ 12547c478bd9Sstevel@tonic-gate if (holdcheck(0) != 0) { 12557c478bd9Sstevel@tonic-gate mutex_exit(&p->p_lock); 12567c478bd9Sstevel@tonic-gate return (-1); 12577c478bd9Sstevel@tonic-gate } 12587c478bd9Sstevel@tonic-gate 12597c478bd9Sstevel@tonic-gate if (!(p->p_flag & SHOLDWATCH)) { 12607c478bd9Sstevel@tonic-gate /* 12617c478bd9Sstevel@tonic-gate * We are the master watchpoint thread. Set SHOLDWATCH and poke 12627c478bd9Sstevel@tonic-gate * the other threads. 12637c478bd9Sstevel@tonic-gate */ 12647c478bd9Sstevel@tonic-gate p->p_flag |= SHOLDWATCH; 12657c478bd9Sstevel@tonic-gate pokelwps(p); 12667c478bd9Sstevel@tonic-gate 12677c478bd9Sstevel@tonic-gate /* 12687c478bd9Sstevel@tonic-gate * Wait for all threads to be stopped or have TP_WATCHSTOP set. 12697c478bd9Sstevel@tonic-gate */ 12707c478bd9Sstevel@tonic-gate while (pr_allstopped(p, 1) > 0) { 12717c478bd9Sstevel@tonic-gate if (holdcheck(SHOLDWATCH) != 0) { 12727c478bd9Sstevel@tonic-gate p->p_flag &= ~SHOLDWATCH; 12737c478bd9Sstevel@tonic-gate mutex_exit(&p->p_lock); 12747c478bd9Sstevel@tonic-gate return (-1); 12757c478bd9Sstevel@tonic-gate } 12767c478bd9Sstevel@tonic-gate 12777c478bd9Sstevel@tonic-gate cv_wait(&p->p_holdlwps, &p->p_lock); 12787c478bd9Sstevel@tonic-gate } 12797c478bd9Sstevel@tonic-gate 12807c478bd9Sstevel@tonic-gate /* 12817c478bd9Sstevel@tonic-gate * All threads are now stopped or in the process of stopping. 12827c478bd9Sstevel@tonic-gate * Set SWATCHOK and let them stop completely. 12837c478bd9Sstevel@tonic-gate */ 12847c478bd9Sstevel@tonic-gate p->p_flag |= SWATCHOK; 12857c478bd9Sstevel@tonic-gate t->t_proc_flag &= ~TP_WATCHSTOP; 12867c478bd9Sstevel@tonic-gate cv_broadcast(&p->p_holdlwps); 12877c478bd9Sstevel@tonic-gate 12887c478bd9Sstevel@tonic-gate while (pr_allstopped(p, 0) > 0) { 12897c478bd9Sstevel@tonic-gate /* 12907c478bd9Sstevel@tonic-gate * At first glance, it may appear that we don't need a 12917c478bd9Sstevel@tonic-gate * call to holdcheck() here. But if the process gets a 12927c478bd9Sstevel@tonic-gate * SIGKILL signal, one of our stopped threads may have 12937c478bd9Sstevel@tonic-gate * been awakened and is waiting in exitlwps(), which 12947c478bd9Sstevel@tonic-gate * takes precedence over watchpoints. 12957c478bd9Sstevel@tonic-gate */ 12967c478bd9Sstevel@tonic-gate if (holdcheck(SHOLDWATCH | SWATCHOK) != 0) { 12977c478bd9Sstevel@tonic-gate p->p_flag &= ~(SHOLDWATCH | SWATCHOK); 12987c478bd9Sstevel@tonic-gate mutex_exit(&p->p_lock); 12997c478bd9Sstevel@tonic-gate return (-1); 13007c478bd9Sstevel@tonic-gate } 13017c478bd9Sstevel@tonic-gate 13027c478bd9Sstevel@tonic-gate cv_wait(&p->p_holdlwps, &p->p_lock); 13037c478bd9Sstevel@tonic-gate } 13047c478bd9Sstevel@tonic-gate 13057c478bd9Sstevel@tonic-gate /* 13067c478bd9Sstevel@tonic-gate * All threads are now completely stopped. 13077c478bd9Sstevel@tonic-gate */ 13087c478bd9Sstevel@tonic-gate p->p_flag &= ~SWATCHOK; 13097c478bd9Sstevel@tonic-gate p->p_flag &= ~SHOLDWATCH; 13107c478bd9Sstevel@tonic-gate p->p_lwprcnt++; 13117c478bd9Sstevel@tonic-gate 13127c478bd9Sstevel@tonic-gate } else if (!(p->p_flag & SWATCHOK)) { 13137c478bd9Sstevel@tonic-gate 13147c478bd9Sstevel@tonic-gate /* 13157c478bd9Sstevel@tonic-gate * SHOLDWATCH is set, so another thread is trying to do 13167c478bd9Sstevel@tonic-gate * watchpoint activity. Indicate this thread is stopping, and 13177c478bd9Sstevel@tonic-gate * wait for the OK from the master thread. 13187c478bd9Sstevel@tonic-gate */ 13197c478bd9Sstevel@tonic-gate t->t_proc_flag |= TP_WATCHSTOP; 13207c478bd9Sstevel@tonic-gate cv_broadcast(&p->p_holdlwps); 13217c478bd9Sstevel@tonic-gate 13227c478bd9Sstevel@tonic-gate while (!(p->p_flag & SWATCHOK)) { 13237c478bd9Sstevel@tonic-gate if (holdcheck(0) != 0) { 13247c478bd9Sstevel@tonic-gate t->t_proc_flag &= ~TP_WATCHSTOP; 13257c478bd9Sstevel@tonic-gate mutex_exit(&p->p_lock); 13267c478bd9Sstevel@tonic-gate return (-1); 13277c478bd9Sstevel@tonic-gate } 13287c478bd9Sstevel@tonic-gate 13297c478bd9Sstevel@tonic-gate cv_wait(&p->p_holdlwps, &p->p_lock); 13307c478bd9Sstevel@tonic-gate } 13317c478bd9Sstevel@tonic-gate 13327c478bd9Sstevel@tonic-gate /* 13337c478bd9Sstevel@tonic-gate * Once the master thread has given the OK, this thread can 13347c478bd9Sstevel@tonic-gate * actually call stop(). 13357c478bd9Sstevel@tonic-gate */ 13367c478bd9Sstevel@tonic-gate t->t_proc_flag &= ~TP_WATCHSTOP; 13377c478bd9Sstevel@tonic-gate p->p_lwprcnt++; 13387c478bd9Sstevel@tonic-gate 13397c478bd9Sstevel@tonic-gate stop(PR_SUSPENDED, SUSPEND_NORMAL); 13407c478bd9Sstevel@tonic-gate 13417c478bd9Sstevel@tonic-gate /* 13427c478bd9Sstevel@tonic-gate * It's not OK to do watchpoint activity, notify caller to 13437c478bd9Sstevel@tonic-gate * retry. 13447c478bd9Sstevel@tonic-gate */ 13457c478bd9Sstevel@tonic-gate ret = -1; 13467c478bd9Sstevel@tonic-gate 13477c478bd9Sstevel@tonic-gate } else { 13487c478bd9Sstevel@tonic-gate 13497c478bd9Sstevel@tonic-gate /* 13507c478bd9Sstevel@tonic-gate * The only way we can hit the case where SHOLDWATCH is set and 13517c478bd9Sstevel@tonic-gate * SWATCHOK is set is if we are triggering this from within a 13527c478bd9Sstevel@tonic-gate * stop() call. Assert that this is the case. 13537c478bd9Sstevel@tonic-gate */ 13547c478bd9Sstevel@tonic-gate 13557c478bd9Sstevel@tonic-gate ASSERT(t->t_proc_flag & TP_STOPPING); 13567c478bd9Sstevel@tonic-gate p->p_lwprcnt++; 13577c478bd9Sstevel@tonic-gate } 13587c478bd9Sstevel@tonic-gate 13597c478bd9Sstevel@tonic-gate mutex_exit(&p->p_lock); 13607c478bd9Sstevel@tonic-gate 13617c478bd9Sstevel@tonic-gate return (ret); 13627c478bd9Sstevel@tonic-gate } 13637c478bd9Sstevel@tonic-gate 13647c478bd9Sstevel@tonic-gate /* 13657c478bd9Sstevel@tonic-gate * force all interruptible lwps to trap into the kernel. 13667c478bd9Sstevel@tonic-gate */ 13677c478bd9Sstevel@tonic-gate void 13687c478bd9Sstevel@tonic-gate pokelwps(proc_t *p) 13697c478bd9Sstevel@tonic-gate { 13707c478bd9Sstevel@tonic-gate kthread_t *t; 13717c478bd9Sstevel@tonic-gate 13727c478bd9Sstevel@tonic-gate ASSERT(MUTEX_HELD(&p->p_lock)); 13737c478bd9Sstevel@tonic-gate 13747c478bd9Sstevel@tonic-gate t = p->p_tlist; 13757c478bd9Sstevel@tonic-gate do { 13767c478bd9Sstevel@tonic-gate if (t == curthread) 13777c478bd9Sstevel@tonic-gate continue; 13787c478bd9Sstevel@tonic-gate thread_lock(t); 13797c478bd9Sstevel@tonic-gate aston(t); /* make thread trap or do post_syscall */ 1380c97ad5cdSakolb if (ISWAKEABLE(t) || ISWAITING(t)) { 13817c478bd9Sstevel@tonic-gate setrun_locked(t); 13827c478bd9Sstevel@tonic-gate } else if (t->t_state == TS_STOPPED) { 13837c478bd9Sstevel@tonic-gate /* 13847c478bd9Sstevel@tonic-gate * Ensure that proc_exit() is not blocked by lwps 13857c478bd9Sstevel@tonic-gate * that were stopped via jobcontrol or /proc. 13867c478bd9Sstevel@tonic-gate */ 13877c478bd9Sstevel@tonic-gate if (p->p_flag & SEXITLWPS) { 13887c478bd9Sstevel@tonic-gate p->p_stopsig = 0; 13897c478bd9Sstevel@tonic-gate t->t_schedflag |= (TS_XSTART | TS_PSTART); 13907c478bd9Sstevel@tonic-gate setrun_locked(t); 13917c478bd9Sstevel@tonic-gate } 13927c478bd9Sstevel@tonic-gate /* 13937c478bd9Sstevel@tonic-gate * If we are holding lwps for a forkall(), 13947c478bd9Sstevel@tonic-gate * force lwps that have been suspended via 13957c478bd9Sstevel@tonic-gate * lwp_suspend() and are suspended inside 13967c478bd9Sstevel@tonic-gate * of a system call to proceed to their 13977c478bd9Sstevel@tonic-gate * holdlwp() points where they are clonable. 13987c478bd9Sstevel@tonic-gate */ 13997c478bd9Sstevel@tonic-gate if ((p->p_flag & SHOLDFORK) && SUSPENDED(t)) { 14007c478bd9Sstevel@tonic-gate if ((t->t_schedflag & TS_CSTART) == 0) { 14017c478bd9Sstevel@tonic-gate p->p_lwprcnt++; 14027c478bd9Sstevel@tonic-gate t->t_schedflag |= TS_CSTART; 14037c478bd9Sstevel@tonic-gate setrun_locked(t); 14047c478bd9Sstevel@tonic-gate } 14057c478bd9Sstevel@tonic-gate } 14067c478bd9Sstevel@tonic-gate } else if (t->t_state == TS_ONPROC) { 14077c478bd9Sstevel@tonic-gate if (t->t_cpu != CPU) 14087c478bd9Sstevel@tonic-gate poke_cpu(t->t_cpu->cpu_id); 14097c478bd9Sstevel@tonic-gate } 14107c478bd9Sstevel@tonic-gate thread_unlock(t); 14117c478bd9Sstevel@tonic-gate } while ((t = t->t_forw) != p->p_tlist); 14127c478bd9Sstevel@tonic-gate } 14137c478bd9Sstevel@tonic-gate 14147c478bd9Sstevel@tonic-gate /* 14157c478bd9Sstevel@tonic-gate * undo the effects of holdlwps() or holdwatch(). 14167c478bd9Sstevel@tonic-gate */ 14177c478bd9Sstevel@tonic-gate void 14187c478bd9Sstevel@tonic-gate continuelwps(proc_t *p) 14197c478bd9Sstevel@tonic-gate { 14207c478bd9Sstevel@tonic-gate kthread_t *t; 14217c478bd9Sstevel@tonic-gate 14227c478bd9Sstevel@tonic-gate /* 14237c478bd9Sstevel@tonic-gate * If this flag is set, then the original holdwatch() didn't actually 14247c478bd9Sstevel@tonic-gate * stop the process. See comments for holdwatch(). 14257c478bd9Sstevel@tonic-gate */ 14267c478bd9Sstevel@tonic-gate if (p->p_flag & SWATCHOK) { 14277c478bd9Sstevel@tonic-gate ASSERT(curthread->t_proc_flag & TP_STOPPING); 14287c478bd9Sstevel@tonic-gate return; 14297c478bd9Sstevel@tonic-gate } 14307c478bd9Sstevel@tonic-gate 14317c478bd9Sstevel@tonic-gate ASSERT(MUTEX_HELD(&p->p_lock)); 14327c478bd9Sstevel@tonic-gate ASSERT((p->p_flag & (SHOLDFORK | SHOLDFORK1 | SHOLDWATCH)) == 0); 14337c478bd9Sstevel@tonic-gate 14347c478bd9Sstevel@tonic-gate t = p->p_tlist; 14357c478bd9Sstevel@tonic-gate do { 14367c478bd9Sstevel@tonic-gate thread_lock(t); /* SUSPENDED looks at t_schedflag */ 14377c478bd9Sstevel@tonic-gate if (SUSPENDED(t) && !(t->t_proc_flag & TP_HOLDLWP)) { 14387c478bd9Sstevel@tonic-gate p->p_lwprcnt++; 14397c478bd9Sstevel@tonic-gate t->t_schedflag |= TS_CSTART; 14407c478bd9Sstevel@tonic-gate setrun_locked(t); 14417c478bd9Sstevel@tonic-gate } 14427c478bd9Sstevel@tonic-gate thread_unlock(t); 14437c478bd9Sstevel@tonic-gate } while ((t = t->t_forw) != p->p_tlist); 14447c478bd9Sstevel@tonic-gate } 14457c478bd9Sstevel@tonic-gate 14467c478bd9Sstevel@tonic-gate /* 14477c478bd9Sstevel@tonic-gate * Force all other LWPs in the current process other than the caller to exit, 14487c478bd9Sstevel@tonic-gate * and then cv_wait() on p_holdlwps for them to exit. The exitlwps() function 14497c478bd9Sstevel@tonic-gate * is typically used in these situations: 14507c478bd9Sstevel@tonic-gate * 14517c478bd9Sstevel@tonic-gate * (a) prior to an exec() system call 14527c478bd9Sstevel@tonic-gate * (b) prior to dumping a core file 14537c478bd9Sstevel@tonic-gate * (c) prior to a uadmin() shutdown 14547c478bd9Sstevel@tonic-gate * 14557c478bd9Sstevel@tonic-gate * If the 'coredump' flag is set, other LWPs are quiesced but not destroyed. 14567c478bd9Sstevel@tonic-gate * Multiple threads in the process can call this function at one time by 14577c478bd9Sstevel@tonic-gate * triggering execs or core dumps simultaneously, so the SEXITLWPS bit is used 14587c478bd9Sstevel@tonic-gate * to declare one particular thread the winner who gets to kill the others. 14597c478bd9Sstevel@tonic-gate * If a thread wins the exitlwps() dance, zero is returned; otherwise an 14607c478bd9Sstevel@tonic-gate * appropriate errno value is returned to caller for its system call to return. 14617c478bd9Sstevel@tonic-gate */ 14627c478bd9Sstevel@tonic-gate int 14637c478bd9Sstevel@tonic-gate exitlwps(int coredump) 14647c478bd9Sstevel@tonic-gate { 14657c478bd9Sstevel@tonic-gate proc_t *p = curproc; 14667c478bd9Sstevel@tonic-gate int heldcnt; 14677c478bd9Sstevel@tonic-gate 14687c478bd9Sstevel@tonic-gate if (curthread->t_door) 14697c478bd9Sstevel@tonic-gate door_slam(); 14707c478bd9Sstevel@tonic-gate if (p->p_door_list) 14717c478bd9Sstevel@tonic-gate door_revoke_all(); 14727c478bd9Sstevel@tonic-gate if (curthread->t_schedctl != NULL) 14737c478bd9Sstevel@tonic-gate schedctl_lwp_cleanup(curthread); 14747c478bd9Sstevel@tonic-gate 14757c478bd9Sstevel@tonic-gate /* 14767c478bd9Sstevel@tonic-gate * Ensure that before starting to wait for other lwps to exit, 14777c478bd9Sstevel@tonic-gate * cleanup all upimutexes held by curthread. Otherwise, some other 14787c478bd9Sstevel@tonic-gate * lwp could be waiting (uninterruptibly) for a upimutex held by 14797c478bd9Sstevel@tonic-gate * curthread, and the call to pokelwps() below would deadlock. 14807c478bd9Sstevel@tonic-gate * Even if a blocked upimutex_lock is made interruptible, 14817c478bd9Sstevel@tonic-gate * curthread's upimutexes need to be unlocked: do it here. 14827c478bd9Sstevel@tonic-gate */ 14837c478bd9Sstevel@tonic-gate if (curthread->t_upimutex != NULL) 14847c478bd9Sstevel@tonic-gate upimutex_cleanup(); 14857c478bd9Sstevel@tonic-gate 14867c478bd9Sstevel@tonic-gate /* 14877c478bd9Sstevel@tonic-gate * Grab p_lock in order to check and set SEXITLWPS to declare a winner. 14887c478bd9Sstevel@tonic-gate * We must also block any further /proc access from this point forward. 14897c478bd9Sstevel@tonic-gate */ 14907c478bd9Sstevel@tonic-gate mutex_enter(&p->p_lock); 14917c478bd9Sstevel@tonic-gate prbarrier(p); 14927c478bd9Sstevel@tonic-gate 14937c478bd9Sstevel@tonic-gate if (p->p_flag & SEXITLWPS) { 14947c478bd9Sstevel@tonic-gate mutex_exit(&p->p_lock); 14957c478bd9Sstevel@tonic-gate aston(curthread); /* force a trip through post_syscall */ 14967c478bd9Sstevel@tonic-gate return (set_errno(EINTR)); 14977c478bd9Sstevel@tonic-gate } 14987c478bd9Sstevel@tonic-gate 14997c478bd9Sstevel@tonic-gate p->p_flag |= SEXITLWPS; 15007c478bd9Sstevel@tonic-gate if (coredump) /* tell other lwps to stop, not exit */ 15017c478bd9Sstevel@tonic-gate p->p_flag |= SCOREDUMP; 15027c478bd9Sstevel@tonic-gate 15037c478bd9Sstevel@tonic-gate /* 15047c478bd9Sstevel@tonic-gate * Give precedence to exitlwps() if a holdlwps() is 15057c478bd9Sstevel@tonic-gate * in progress. The lwp doing the holdlwps() operation 15067c478bd9Sstevel@tonic-gate * is aborted when it is awakened. 15077c478bd9Sstevel@tonic-gate */ 15087c478bd9Sstevel@tonic-gate while (p->p_flag & (SHOLDFORK | SHOLDFORK1 | SHOLDWATCH)) { 15097c478bd9Sstevel@tonic-gate cv_broadcast(&p->p_holdlwps); 15107c478bd9Sstevel@tonic-gate cv_wait(&p->p_holdlwps, &p->p_lock); 151197eda132Sraf prbarrier(p); 15127c478bd9Sstevel@tonic-gate } 15137c478bd9Sstevel@tonic-gate p->p_flag |= SHOLDFORK; 15147c478bd9Sstevel@tonic-gate pokelwps(p); 15157c478bd9Sstevel@tonic-gate 15167c478bd9Sstevel@tonic-gate /* 15177c478bd9Sstevel@tonic-gate * Wait for process to become quiescent. 15187c478bd9Sstevel@tonic-gate */ 15197c478bd9Sstevel@tonic-gate --p->p_lwprcnt; 152097eda132Sraf while (p->p_lwprcnt > 0) { 15217c478bd9Sstevel@tonic-gate cv_wait(&p->p_holdlwps, &p->p_lock); 152297eda132Sraf prbarrier(p); 152397eda132Sraf } 15247c478bd9Sstevel@tonic-gate p->p_lwprcnt++; 15257c478bd9Sstevel@tonic-gate ASSERT(p->p_lwprcnt == 1); 15267c478bd9Sstevel@tonic-gate 15277c478bd9Sstevel@tonic-gate /* 15287c478bd9Sstevel@tonic-gate * The SCOREDUMP flag puts the process into a quiescent 15297c478bd9Sstevel@tonic-gate * state. The process's lwps remain attached to this 15307c478bd9Sstevel@tonic-gate * process until exitlwps() is called again without the 15317c478bd9Sstevel@tonic-gate * 'coredump' flag set, then the lwps are terminated 15327c478bd9Sstevel@tonic-gate * and the process can exit. 15337c478bd9Sstevel@tonic-gate */ 15347c478bd9Sstevel@tonic-gate if (coredump) { 15357c478bd9Sstevel@tonic-gate p->p_flag &= ~(SCOREDUMP | SHOLDFORK | SEXITLWPS); 15367c478bd9Sstevel@tonic-gate goto out; 15377c478bd9Sstevel@tonic-gate } 15387c478bd9Sstevel@tonic-gate 15397c478bd9Sstevel@tonic-gate /* 15407c478bd9Sstevel@tonic-gate * Determine if there are any lwps left dangling in 15417c478bd9Sstevel@tonic-gate * the stopped state. This happens when exitlwps() 15427c478bd9Sstevel@tonic-gate * aborts a holdlwps() operation. 15437c478bd9Sstevel@tonic-gate */ 15447c478bd9Sstevel@tonic-gate p->p_flag &= ~SHOLDFORK; 15457c478bd9Sstevel@tonic-gate if ((heldcnt = p->p_lwpcnt) > 1) { 15467c478bd9Sstevel@tonic-gate kthread_t *t; 15477c478bd9Sstevel@tonic-gate for (t = curthread->t_forw; --heldcnt > 0; t = t->t_forw) { 15487c478bd9Sstevel@tonic-gate t->t_proc_flag &= ~TP_TWAIT; 15497c478bd9Sstevel@tonic-gate lwp_continue(t); 15507c478bd9Sstevel@tonic-gate } 15517c478bd9Sstevel@tonic-gate } 15527c478bd9Sstevel@tonic-gate 15537c478bd9Sstevel@tonic-gate /* 15547c478bd9Sstevel@tonic-gate * Wait for all other lwps to exit. 15557c478bd9Sstevel@tonic-gate */ 15567c478bd9Sstevel@tonic-gate --p->p_lwprcnt; 155797eda132Sraf while (p->p_lwpcnt > 1) { 15587c478bd9Sstevel@tonic-gate cv_wait(&p->p_holdlwps, &p->p_lock); 155997eda132Sraf prbarrier(p); 156097eda132Sraf } 15617c478bd9Sstevel@tonic-gate ++p->p_lwprcnt; 15627c478bd9Sstevel@tonic-gate ASSERT(p->p_lwpcnt == 1 && p->p_lwprcnt == 1); 15637c478bd9Sstevel@tonic-gate 15647c478bd9Sstevel@tonic-gate p->p_flag &= ~SEXITLWPS; 15657c478bd9Sstevel@tonic-gate curthread->t_proc_flag &= ~TP_TWAIT; 15667c478bd9Sstevel@tonic-gate 15677c478bd9Sstevel@tonic-gate out: 15687c478bd9Sstevel@tonic-gate if (!coredump && p->p_zombcnt) { /* cleanup the zombie lwps */ 15697c478bd9Sstevel@tonic-gate lwpdir_t *ldp; 15707c478bd9Sstevel@tonic-gate lwpent_t *lep; 15717c478bd9Sstevel@tonic-gate int i; 15727c478bd9Sstevel@tonic-gate 15737c478bd9Sstevel@tonic-gate for (ldp = p->p_lwpdir, i = 0; i < p->p_lwpdir_sz; i++, ldp++) { 15747c478bd9Sstevel@tonic-gate lep = ldp->ld_entry; 15757c478bd9Sstevel@tonic-gate if (lep != NULL && lep->le_thread != curthread) { 15767c478bd9Sstevel@tonic-gate ASSERT(lep->le_thread == NULL); 15777c478bd9Sstevel@tonic-gate p->p_zombcnt--; 15787c478bd9Sstevel@tonic-gate lwp_hash_out(p, lep->le_lwpid); 15797c478bd9Sstevel@tonic-gate } 15807c478bd9Sstevel@tonic-gate } 15817c478bd9Sstevel@tonic-gate ASSERT(p->p_zombcnt == 0); 15827c478bd9Sstevel@tonic-gate } 15837c478bd9Sstevel@tonic-gate 15847c478bd9Sstevel@tonic-gate /* 15857c478bd9Sstevel@tonic-gate * If some other LWP in the process wanted us to suspend ourself, 15867c478bd9Sstevel@tonic-gate * then we will not do it. The other LWP is now terminated and 15877c478bd9Sstevel@tonic-gate * no one will ever continue us again if we suspend ourself. 15887c478bd9Sstevel@tonic-gate */ 15897c478bd9Sstevel@tonic-gate curthread->t_proc_flag &= ~TP_HOLDLWP; 15907c478bd9Sstevel@tonic-gate p->p_flag &= ~(SHOLDFORK | SHOLDFORK1 | SHOLDWATCH | SLWPWRAP); 15917c478bd9Sstevel@tonic-gate mutex_exit(&p->p_lock); 15927c478bd9Sstevel@tonic-gate return (0); 15937c478bd9Sstevel@tonic-gate } 15947c478bd9Sstevel@tonic-gate 15957c478bd9Sstevel@tonic-gate /* 15967c478bd9Sstevel@tonic-gate * duplicate a lwp. 15977c478bd9Sstevel@tonic-gate */ 15987c478bd9Sstevel@tonic-gate klwp_t * 15997c478bd9Sstevel@tonic-gate forklwp(klwp_t *lwp, proc_t *cp, id_t lwpid) 16007c478bd9Sstevel@tonic-gate { 16017c478bd9Sstevel@tonic-gate klwp_t *clwp; 16027c478bd9Sstevel@tonic-gate void *tregs, *tfpu; 16037c478bd9Sstevel@tonic-gate kthread_t *t = lwptot(lwp); 16047c478bd9Sstevel@tonic-gate kthread_t *ct; 16057c478bd9Sstevel@tonic-gate proc_t *p = lwptoproc(lwp); 16067c478bd9Sstevel@tonic-gate int cid; 16077c478bd9Sstevel@tonic-gate void *bufp; 16089acbbeafSnn35248 void *brand_data; 16097c478bd9Sstevel@tonic-gate int val; 16107c478bd9Sstevel@tonic-gate 16117c478bd9Sstevel@tonic-gate ASSERT(p == curproc); 16127c478bd9Sstevel@tonic-gate ASSERT(t == curthread || (SUSPENDED(t) && lwp->lwp_asleep == 0)); 16137c478bd9Sstevel@tonic-gate 16147c478bd9Sstevel@tonic-gate #if defined(__sparc) 16157c478bd9Sstevel@tonic-gate if (t == curthread) 16167c478bd9Sstevel@tonic-gate (void) flush_user_windows_to_stack(NULL); 16177c478bd9Sstevel@tonic-gate #endif 16187c478bd9Sstevel@tonic-gate 16197c478bd9Sstevel@tonic-gate if (t == curthread) 16207c478bd9Sstevel@tonic-gate /* copy args out of registers first */ 16217c478bd9Sstevel@tonic-gate (void) save_syscall_args(); 16229acbbeafSnn35248 16237c478bd9Sstevel@tonic-gate clwp = lwp_create(cp->p_lwpcnt == 0 ? lwp_rtt_initial : lwp_rtt, 16247c478bd9Sstevel@tonic-gate NULL, 0, cp, TS_STOPPED, t->t_pri, &t->t_hold, NOCLASS, lwpid); 16257c478bd9Sstevel@tonic-gate if (clwp == NULL) 16267c478bd9Sstevel@tonic-gate return (NULL); 16277c478bd9Sstevel@tonic-gate 16287c478bd9Sstevel@tonic-gate /* 16297c478bd9Sstevel@tonic-gate * most of the parent's lwp can be copied to its duplicate, 16307c478bd9Sstevel@tonic-gate * except for the fields that are unique to each lwp, like 16317c478bd9Sstevel@tonic-gate * lwp_thread, lwp_procp, lwp_regs, and lwp_ap. 16327c478bd9Sstevel@tonic-gate */ 16337c478bd9Sstevel@tonic-gate ct = clwp->lwp_thread; 16347c478bd9Sstevel@tonic-gate tregs = clwp->lwp_regs; 16357c478bd9Sstevel@tonic-gate tfpu = clwp->lwp_fpu; 16369acbbeafSnn35248 brand_data = clwp->lwp_brand; 16377c478bd9Sstevel@tonic-gate 16385d3ff519Sjohansen /* 16395d3ff519Sjohansen * Copy parent lwp to child lwp. Hold child's p_lock to prevent 16405d3ff519Sjohansen * mstate_aggr_state() from reading stale mstate entries copied 16415d3ff519Sjohansen * from lwp to clwp. 16425d3ff519Sjohansen */ 16435d3ff519Sjohansen mutex_enter(&cp->p_lock); 16447c478bd9Sstevel@tonic-gate *clwp = *lwp; 16457c478bd9Sstevel@tonic-gate 16465d3ff519Sjohansen /* clear microstate and resource usage data in new lwp */ 16475d3ff519Sjohansen init_mstate(ct, LMS_STOPPED); 16485d3ff519Sjohansen bzero(&clwp->lwp_ru, sizeof (clwp->lwp_ru)); 16495d3ff519Sjohansen mutex_exit(&cp->p_lock); 16505d3ff519Sjohansen 16517c478bd9Sstevel@tonic-gate /* fix up child's lwp */ 16527c478bd9Sstevel@tonic-gate 1653*7712e92cSsudheer clwp->lwp_pcb.pcb_flags = 0; 1654*7712e92cSsudheer #if defined(__sparc) 16557c478bd9Sstevel@tonic-gate clwp->lwp_pcb.pcb_step = STEP_NONE; 16567c478bd9Sstevel@tonic-gate #endif 16577c478bd9Sstevel@tonic-gate clwp->lwp_cursig = 0; 16587c478bd9Sstevel@tonic-gate clwp->lwp_extsig = 0; 16597c478bd9Sstevel@tonic-gate clwp->lwp_curinfo = (struct sigqueue *)0; 16607c478bd9Sstevel@tonic-gate clwp->lwp_thread = ct; 16617c478bd9Sstevel@tonic-gate ct->t_sysnum = t->t_sysnum; 16627c478bd9Sstevel@tonic-gate clwp->lwp_regs = tregs; 16637c478bd9Sstevel@tonic-gate clwp->lwp_fpu = tfpu; 16649acbbeafSnn35248 clwp->lwp_brand = brand_data; 16657c478bd9Sstevel@tonic-gate clwp->lwp_ap = clwp->lwp_arg; 16667c478bd9Sstevel@tonic-gate clwp->lwp_procp = cp; 16677c478bd9Sstevel@tonic-gate bzero(clwp->lwp_timer, sizeof (clwp->lwp_timer)); 16687c478bd9Sstevel@tonic-gate clwp->lwp_lastfault = 0; 16697c478bd9Sstevel@tonic-gate clwp->lwp_lastfaddr = 0; 16707c478bd9Sstevel@tonic-gate 16717c478bd9Sstevel@tonic-gate /* copy parent's struct regs to child. */ 16727c478bd9Sstevel@tonic-gate lwp_forkregs(lwp, clwp); 16737c478bd9Sstevel@tonic-gate 16747c478bd9Sstevel@tonic-gate /* 16750baeff3dSrab * Fork thread context ops, if any. 16767c478bd9Sstevel@tonic-gate */ 16777c478bd9Sstevel@tonic-gate if (t->t_ctx) 16787c478bd9Sstevel@tonic-gate forkctx(t, ct); 16797c478bd9Sstevel@tonic-gate 16807c478bd9Sstevel@tonic-gate /* fix door state in the child */ 16817c478bd9Sstevel@tonic-gate if (t->t_door) 16827c478bd9Sstevel@tonic-gate door_fork(t, ct); 16837c478bd9Sstevel@tonic-gate 16847c478bd9Sstevel@tonic-gate /* copy current contract templates, clear latest contracts */ 16857c478bd9Sstevel@tonic-gate lwp_ctmpl_copy(clwp, lwp); 16867c478bd9Sstevel@tonic-gate 16877c478bd9Sstevel@tonic-gate mutex_enter(&cp->p_lock); 16887c478bd9Sstevel@tonic-gate /* lwp_create() set the TP_HOLDLWP flag */ 16897c478bd9Sstevel@tonic-gate if (!(t->t_proc_flag & TP_HOLDLWP)) 16907c478bd9Sstevel@tonic-gate ct->t_proc_flag &= ~TP_HOLDLWP; 16917c478bd9Sstevel@tonic-gate if (cp->p_flag & SMSACCT) 16927c478bd9Sstevel@tonic-gate ct->t_proc_flag |= TP_MSACCT; 16937c478bd9Sstevel@tonic-gate mutex_exit(&cp->p_lock); 16947c478bd9Sstevel@tonic-gate 16959acbbeafSnn35248 /* Allow brand to propagate brand-specific state */ 16969acbbeafSnn35248 if (PROC_IS_BRANDED(p)) 16979acbbeafSnn35248 BROP(p)->b_forklwp(lwp, clwp); 16989acbbeafSnn35248 16997c478bd9Sstevel@tonic-gate retry: 17007c478bd9Sstevel@tonic-gate cid = t->t_cid; 17017c478bd9Sstevel@tonic-gate 17027c478bd9Sstevel@tonic-gate val = CL_ALLOC(&bufp, cid, KM_SLEEP); 17037c478bd9Sstevel@tonic-gate ASSERT(val == 0); 17047c478bd9Sstevel@tonic-gate 17057c478bd9Sstevel@tonic-gate mutex_enter(&p->p_lock); 17067c478bd9Sstevel@tonic-gate if (cid != t->t_cid) { 17077c478bd9Sstevel@tonic-gate /* 17087c478bd9Sstevel@tonic-gate * Someone just changed this thread's scheduling class, 17097c478bd9Sstevel@tonic-gate * so try pre-allocating the buffer again. Hopefully we 17107c478bd9Sstevel@tonic-gate * don't hit this often. 17117c478bd9Sstevel@tonic-gate */ 17127c478bd9Sstevel@tonic-gate mutex_exit(&p->p_lock); 17137c478bd9Sstevel@tonic-gate CL_FREE(cid, bufp); 17147c478bd9Sstevel@tonic-gate goto retry; 17157c478bd9Sstevel@tonic-gate } 17167c478bd9Sstevel@tonic-gate 17177c478bd9Sstevel@tonic-gate ct->t_unpark = t->t_unpark; 17187c478bd9Sstevel@tonic-gate ct->t_clfuncs = t->t_clfuncs; 17197c478bd9Sstevel@tonic-gate CL_FORK(t, ct, bufp); 17207c478bd9Sstevel@tonic-gate ct->t_cid = t->t_cid; /* after data allocated so prgetpsinfo works */ 17217c478bd9Sstevel@tonic-gate mutex_exit(&p->p_lock); 17227c478bd9Sstevel@tonic-gate 17237c478bd9Sstevel@tonic-gate return (clwp); 17247c478bd9Sstevel@tonic-gate } 17257c478bd9Sstevel@tonic-gate 17267c478bd9Sstevel@tonic-gate /* 17277c478bd9Sstevel@tonic-gate * Add a new lwp entry to the lwp directory and to the lwpid hash table. 17287c478bd9Sstevel@tonic-gate */ 17297c478bd9Sstevel@tonic-gate void 17307c478bd9Sstevel@tonic-gate lwp_hash_in(proc_t *p, lwpent_t *lep) 17317c478bd9Sstevel@tonic-gate { 17327c478bd9Sstevel@tonic-gate lwpdir_t **ldpp; 17337c478bd9Sstevel@tonic-gate lwpdir_t *ldp; 17347c478bd9Sstevel@tonic-gate kthread_t *t; 17357c478bd9Sstevel@tonic-gate 17367c478bd9Sstevel@tonic-gate /* 17377c478bd9Sstevel@tonic-gate * Allocate a directory element from the free list. 17387c478bd9Sstevel@tonic-gate * Code elsewhere guarantees a free slot. 17397c478bd9Sstevel@tonic-gate */ 17407c478bd9Sstevel@tonic-gate ldp = p->p_lwpfree; 17417c478bd9Sstevel@tonic-gate p->p_lwpfree = ldp->ld_next; 17427c478bd9Sstevel@tonic-gate ASSERT(ldp->ld_entry == NULL); 17437c478bd9Sstevel@tonic-gate ldp->ld_entry = lep; 17447c478bd9Sstevel@tonic-gate 17457c478bd9Sstevel@tonic-gate /* 17467c478bd9Sstevel@tonic-gate * Insert it into the lwpid hash table. 17477c478bd9Sstevel@tonic-gate */ 17487c478bd9Sstevel@tonic-gate ldpp = &p->p_tidhash[TIDHASH(p, lep->le_lwpid)]; 17497c478bd9Sstevel@tonic-gate ldp->ld_next = *ldpp; 17507c478bd9Sstevel@tonic-gate *ldpp = ldp; 17517c478bd9Sstevel@tonic-gate 17527c478bd9Sstevel@tonic-gate /* 17537c478bd9Sstevel@tonic-gate * Set the active thread's directory slot entry. 17547c478bd9Sstevel@tonic-gate */ 17557c478bd9Sstevel@tonic-gate if ((t = lep->le_thread) != NULL) { 17567c478bd9Sstevel@tonic-gate ASSERT(lep->le_lwpid == t->t_tid); 17577c478bd9Sstevel@tonic-gate t->t_dslot = (int)(ldp - p->p_lwpdir); 17587c478bd9Sstevel@tonic-gate } 17597c478bd9Sstevel@tonic-gate } 17607c478bd9Sstevel@tonic-gate 17617c478bd9Sstevel@tonic-gate /* 17627c478bd9Sstevel@tonic-gate * Remove an lwp from the lwpid hash table and free its directory entry. 17637c478bd9Sstevel@tonic-gate * This is done when a detached lwp exits in lwp_exit() or 17647c478bd9Sstevel@tonic-gate * when a non-detached lwp is waited for in lwp_wait() or 17657c478bd9Sstevel@tonic-gate * when a zombie lwp is detached in lwp_detach(). 17667c478bd9Sstevel@tonic-gate */ 17677c478bd9Sstevel@tonic-gate void 17687c478bd9Sstevel@tonic-gate lwp_hash_out(proc_t *p, id_t lwpid) 17697c478bd9Sstevel@tonic-gate { 17707c478bd9Sstevel@tonic-gate lwpdir_t **ldpp; 17717c478bd9Sstevel@tonic-gate lwpdir_t *ldp; 17727c478bd9Sstevel@tonic-gate lwpent_t *lep; 17737c478bd9Sstevel@tonic-gate 17747c478bd9Sstevel@tonic-gate for (ldpp = &p->p_tidhash[TIDHASH(p, lwpid)]; 17757c478bd9Sstevel@tonic-gate (ldp = *ldpp) != NULL; ldpp = &ldp->ld_next) { 17767c478bd9Sstevel@tonic-gate lep = ldp->ld_entry; 17777c478bd9Sstevel@tonic-gate if (lep->le_lwpid == lwpid) { 17787c478bd9Sstevel@tonic-gate prlwpfree(p, lep); /* /proc deals with le_trace */ 17797c478bd9Sstevel@tonic-gate *ldpp = ldp->ld_next; 17807c478bd9Sstevel@tonic-gate ldp->ld_entry = NULL; 17817c478bd9Sstevel@tonic-gate ldp->ld_next = p->p_lwpfree; 17827c478bd9Sstevel@tonic-gate p->p_lwpfree = ldp; 17837c478bd9Sstevel@tonic-gate kmem_free(lep, sizeof (*lep)); 17847c478bd9Sstevel@tonic-gate break; 17857c478bd9Sstevel@tonic-gate } 17867c478bd9Sstevel@tonic-gate } 17877c478bd9Sstevel@tonic-gate } 17887c478bd9Sstevel@tonic-gate 17897c478bd9Sstevel@tonic-gate /* 17907c478bd9Sstevel@tonic-gate * Lookup an lwp in the lwpid hash table by lwpid. 17917c478bd9Sstevel@tonic-gate */ 17927c478bd9Sstevel@tonic-gate lwpdir_t * 17937c478bd9Sstevel@tonic-gate lwp_hash_lookup(proc_t *p, id_t lwpid) 17947c478bd9Sstevel@tonic-gate { 17957c478bd9Sstevel@tonic-gate lwpdir_t *ldp; 17967c478bd9Sstevel@tonic-gate 17977c478bd9Sstevel@tonic-gate /* 17987c478bd9Sstevel@tonic-gate * The process may be exiting, after p_tidhash has been set to NULL in 17997c478bd9Sstevel@tonic-gate * proc_exit() but before prfee() has been called. Return failure in 18007c478bd9Sstevel@tonic-gate * this case. 18017c478bd9Sstevel@tonic-gate */ 18027c478bd9Sstevel@tonic-gate if (p->p_tidhash == NULL) 18037c478bd9Sstevel@tonic-gate return (NULL); 18047c478bd9Sstevel@tonic-gate 18057c478bd9Sstevel@tonic-gate for (ldp = p->p_tidhash[TIDHASH(p, lwpid)]; 18067c478bd9Sstevel@tonic-gate ldp != NULL; ldp = ldp->ld_next) { 18077c478bd9Sstevel@tonic-gate if (ldp->ld_entry->le_lwpid == lwpid) 18087c478bd9Sstevel@tonic-gate return (ldp); 18097c478bd9Sstevel@tonic-gate } 18107c478bd9Sstevel@tonic-gate 18117c478bd9Sstevel@tonic-gate return (NULL); 18127c478bd9Sstevel@tonic-gate } 18137c478bd9Sstevel@tonic-gate 18147c478bd9Sstevel@tonic-gate /* 18157c478bd9Sstevel@tonic-gate * Update the indicated LWP usage statistic for the current LWP. 18167c478bd9Sstevel@tonic-gate */ 18177c478bd9Sstevel@tonic-gate void 18187c478bd9Sstevel@tonic-gate lwp_stat_update(lwp_stat_id_t lwp_stat_id, long inc) 18197c478bd9Sstevel@tonic-gate { 18207c478bd9Sstevel@tonic-gate klwp_t *lwp = ttolwp(curthread); 18217c478bd9Sstevel@tonic-gate 18227c478bd9Sstevel@tonic-gate if (lwp == NULL) 18237c478bd9Sstevel@tonic-gate return; 18247c478bd9Sstevel@tonic-gate 18257c478bd9Sstevel@tonic-gate switch (lwp_stat_id) { 18267c478bd9Sstevel@tonic-gate case LWP_STAT_INBLK: 18277c478bd9Sstevel@tonic-gate lwp->lwp_ru.inblock += inc; 18287c478bd9Sstevel@tonic-gate break; 18297c478bd9Sstevel@tonic-gate case LWP_STAT_OUBLK: 18307c478bd9Sstevel@tonic-gate lwp->lwp_ru.oublock += inc; 18317c478bd9Sstevel@tonic-gate break; 18327c478bd9Sstevel@tonic-gate case LWP_STAT_MSGRCV: 18337c478bd9Sstevel@tonic-gate lwp->lwp_ru.msgrcv += inc; 18347c478bd9Sstevel@tonic-gate break; 18357c478bd9Sstevel@tonic-gate case LWP_STAT_MSGSND: 18367c478bd9Sstevel@tonic-gate lwp->lwp_ru.msgsnd += inc; 18377c478bd9Sstevel@tonic-gate break; 18387c478bd9Sstevel@tonic-gate default: 18397c478bd9Sstevel@tonic-gate panic("lwp_stat_update: invalid lwp_stat_id 0x%x", lwp_stat_id); 18407c478bd9Sstevel@tonic-gate } 18417c478bd9Sstevel@tonic-gate } 1842