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 51d2738a5Sraf * Common Development and Distribution License (the "License"). 61d2738a5Sraf * 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 */ 210293487cSraf 227c478bd9Sstevel@tonic-gate /* 23c4a8d66cSRoger A. Faulkner * Copyright (c) 1999, 2010, Oracle and/or its affiliates. All rights reserved. 2486dd6e44SYouzhong Yang * Copyright (c) 2017 by The MathWorks, Inc. All rights reserved. 257c478bd9Sstevel@tonic-gate */ 264f364e7cSRobert Mustacchi /* 27*dcdfe824SRobert Mustacchi * Copyright 2016 Joyent, Inc. 284f364e7cSRobert Mustacchi */ 297c478bd9Sstevel@tonic-gate 307c478bd9Sstevel@tonic-gate #include "lint.h" 317c478bd9Sstevel@tonic-gate #include "thr_uberdata.h" 32a574db85Sraf #include <pthread.h> 337c478bd9Sstevel@tonic-gate #include <procfs.h> 347c478bd9Sstevel@tonic-gate #include <sys/uio.h> 357c478bd9Sstevel@tonic-gate #include <ctype.h> 36a574db85Sraf #include "libc.h" 377c478bd9Sstevel@tonic-gate 387257d1b4Sraf /* 397257d1b4Sraf * These symbols should not be exported from libc, but 407257d1b4Sraf * /lib/libm.so.2 references _thr_main. libm needs to be fixed. 417257d1b4Sraf * Also, some older versions of the Studio compiler/debugger 427257d1b4Sraf * components reference them. These need to be fixed, too. 437257d1b4Sraf */ 447257d1b4Sraf #pragma weak _thr_main = thr_main 457257d1b4Sraf #pragma weak _thr_create = thr_create 467257d1b4Sraf #pragma weak _thr_join = thr_join 477257d1b4Sraf #pragma weak _thr_self = thr_self 487257d1b4Sraf 497c478bd9Sstevel@tonic-gate #undef errno 507c478bd9Sstevel@tonic-gate extern int errno; 517c478bd9Sstevel@tonic-gate 521d2738a5Sraf /* 531d2738a5Sraf * Between Solaris 2.5 and Solaris 9, __threaded was used to indicate 541d2738a5Sraf * "we are linked with libthread". The Sun Workshop 6 update 1 compilation 551d2738a5Sraf * system used it illegally (it is a consolidation private symbol). 561d2738a5Sraf * To accommodate this and possibly other abusers of the symbol, 571d2738a5Sraf * we make it always equal to 1 now that libthread has been folded 581d2738a5Sraf * into libc. The new __libc_threaded symbol is used to indicate 591d2738a5Sraf * the new meaning, "more than one thread exists". 601d2738a5Sraf */ 611d2738a5Sraf int __threaded = 1; /* always equal to 1 */ 621d2738a5Sraf int __libc_threaded = 0; /* zero until first thr_create() */ 637c478bd9Sstevel@tonic-gate 647c478bd9Sstevel@tonic-gate /* 657c478bd9Sstevel@tonic-gate * thr_concurrency and pthread_concurrency are not used by the library. 667c478bd9Sstevel@tonic-gate * They exist solely to hold and return the values set by calls to 677c478bd9Sstevel@tonic-gate * thr_setconcurrency() and pthread_setconcurrency(). 687c478bd9Sstevel@tonic-gate * Because thr_concurrency is affected by the THR_NEW_LWP flag 697c478bd9Sstevel@tonic-gate * to thr_create(), thr_concurrency is protected by link_lock. 707c478bd9Sstevel@tonic-gate */ 717c478bd9Sstevel@tonic-gate static int thr_concurrency = 1; 727c478bd9Sstevel@tonic-gate static int pthread_concurrency; 737c478bd9Sstevel@tonic-gate 747c478bd9Sstevel@tonic-gate #define HASHTBLSZ 1024 /* must be a power of two */ 757c478bd9Sstevel@tonic-gate #define TIDHASH(tid, udp) (tid & (udp)->hash_mask) 767c478bd9Sstevel@tonic-gate 777c478bd9Sstevel@tonic-gate /* initial allocation, just enough for one lwp */ 787c478bd9Sstevel@tonic-gate #pragma align 64(init_hash_table) 797c478bd9Sstevel@tonic-gate thr_hash_table_t init_hash_table[1] = { 807c478bd9Sstevel@tonic-gate { DEFAULTMUTEX, DEFAULTCV, NULL }, 817c478bd9Sstevel@tonic-gate }; 827c478bd9Sstevel@tonic-gate 837c478bd9Sstevel@tonic-gate extern const Lc_interface rtld_funcs[]; 847c478bd9Sstevel@tonic-gate 857c478bd9Sstevel@tonic-gate /* 867c478bd9Sstevel@tonic-gate * The weak version is known to libc_db and mdb. 877c478bd9Sstevel@tonic-gate */ 887c478bd9Sstevel@tonic-gate #pragma weak _uberdata = __uberdata 897c478bd9Sstevel@tonic-gate uberdata_t __uberdata = { 9036319254Sraf { DEFAULTMUTEX, NULL, 0 }, /* link_lock */ 918cd45542Sraf { RECURSIVEMUTEX, NULL, 0 }, /* ld_lock */ 9236319254Sraf { RECURSIVEMUTEX, NULL, 0 }, /* fork_lock */ 932e145884Sraf { RECURSIVEMUTEX, NULL, 0 }, /* atfork_lock */ 9498c1a6b4Sraf { RECURSIVEMUTEX, NULL, 0 }, /* callout_lock */ 9536319254Sraf { DEFAULTMUTEX, NULL, 0 }, /* tdb_hash_lock */ 967c478bd9Sstevel@tonic-gate { 0, }, /* tdb_hash_lock_stats */ 977c478bd9Sstevel@tonic-gate { { 0 }, }, /* siguaction[NSIG] */ 987c478bd9Sstevel@tonic-gate {{ DEFAULTMUTEX, NULL, 0 }, /* bucket[NBUCKETS] */ 997c478bd9Sstevel@tonic-gate { DEFAULTMUTEX, NULL, 0 }, 1007c478bd9Sstevel@tonic-gate { DEFAULTMUTEX, NULL, 0 }, 1017c478bd9Sstevel@tonic-gate { DEFAULTMUTEX, NULL, 0 }, 1027c478bd9Sstevel@tonic-gate { DEFAULTMUTEX, NULL, 0 }, 1037c478bd9Sstevel@tonic-gate { DEFAULTMUTEX, NULL, 0 }, 1047c478bd9Sstevel@tonic-gate { DEFAULTMUTEX, NULL, 0 }, 1057c478bd9Sstevel@tonic-gate { DEFAULTMUTEX, NULL, 0 }, 1067c478bd9Sstevel@tonic-gate { DEFAULTMUTEX, NULL, 0 }, 1077c478bd9Sstevel@tonic-gate { DEFAULTMUTEX, NULL, 0 }}, 1087c478bd9Sstevel@tonic-gate { RECURSIVEMUTEX, NULL, NULL }, /* atexit_root */ 109*dcdfe824SRobert Mustacchi { RECURSIVEMUTEX, NULL }, /* quickexit_root */ 1107c478bd9Sstevel@tonic-gate { DEFAULTMUTEX, 0, 0, NULL }, /* tsd_metadata */ 1117c478bd9Sstevel@tonic-gate { DEFAULTMUTEX, {0, 0}, {0, 0} }, /* tls_metadata */ 1127c478bd9Sstevel@tonic-gate 0, /* primary_map */ 1137c478bd9Sstevel@tonic-gate 0, /* bucket_init */ 1147c478bd9Sstevel@tonic-gate 0, /* pad[0] */ 1157c478bd9Sstevel@tonic-gate 0, /* pad[1] */ 1167c478bd9Sstevel@tonic-gate { 0 }, /* uberflags */ 1177c478bd9Sstevel@tonic-gate NULL, /* queue_head */ 1187c478bd9Sstevel@tonic-gate init_hash_table, /* thr_hash_table */ 1197c478bd9Sstevel@tonic-gate 1, /* hash_size: size of the hash table */ 1207c478bd9Sstevel@tonic-gate 0, /* hash_mask: hash_size - 1 */ 1217c478bd9Sstevel@tonic-gate NULL, /* ulwp_one */ 1227c478bd9Sstevel@tonic-gate NULL, /* all_lwps */ 1237c478bd9Sstevel@tonic-gate NULL, /* all_zombies */ 1247c478bd9Sstevel@tonic-gate 0, /* nthreads */ 1257c478bd9Sstevel@tonic-gate 0, /* nzombies */ 1267c478bd9Sstevel@tonic-gate 0, /* ndaemons */ 1277c478bd9Sstevel@tonic-gate 0, /* pid */ 1287c478bd9Sstevel@tonic-gate sigacthandler, /* sigacthandler */ 1297c478bd9Sstevel@tonic-gate NULL, /* lwp_stacks */ 1307c478bd9Sstevel@tonic-gate NULL, /* lwp_laststack */ 1317c478bd9Sstevel@tonic-gate 0, /* nfreestack */ 1327c478bd9Sstevel@tonic-gate 10, /* thread_stack_cache */ 1337c478bd9Sstevel@tonic-gate NULL, /* ulwp_freelist */ 1347c478bd9Sstevel@tonic-gate NULL, /* ulwp_lastfree */ 1357c478bd9Sstevel@tonic-gate NULL, /* ulwp_replace_free */ 1367c478bd9Sstevel@tonic-gate NULL, /* ulwp_replace_last */ 1377c478bd9Sstevel@tonic-gate NULL, /* atforklist */ 138883492d5Sraf NULL, /* robustlocks */ 13909ce0d4aSRoger A. Faulkner NULL, /* robustlist */ 14023a1cceaSRoger A. Faulkner NULL, /* progname */ 141263f549eSPatrick Mooney NULL, /* ub_comm_page */ 1427c478bd9Sstevel@tonic-gate NULL, /* __tdb_bootstrap */ 1437c478bd9Sstevel@tonic-gate { /* tdb */ 1447c478bd9Sstevel@tonic-gate NULL, /* tdb_sync_addr_hash */ 1457c478bd9Sstevel@tonic-gate 0, /* tdb_register_count */ 1467c478bd9Sstevel@tonic-gate 0, /* tdb_hash_alloc_failed */ 1477c478bd9Sstevel@tonic-gate NULL, /* tdb_sync_addr_free */ 1487c478bd9Sstevel@tonic-gate NULL, /* tdb_sync_addr_last */ 1497c478bd9Sstevel@tonic-gate 0, /* tdb_sync_alloc */ 1507c478bd9Sstevel@tonic-gate { 0, 0 }, /* tdb_ev_global_mask */ 1517c478bd9Sstevel@tonic-gate tdb_events, /* tdb_events array */ 1527c478bd9Sstevel@tonic-gate }, 1537c478bd9Sstevel@tonic-gate }; 1547c478bd9Sstevel@tonic-gate 1557c478bd9Sstevel@tonic-gate /* 1567c478bd9Sstevel@tonic-gate * The weak version is known to libc_db and mdb. 1577c478bd9Sstevel@tonic-gate */ 1587c478bd9Sstevel@tonic-gate #pragma weak _tdb_bootstrap = __tdb_bootstrap 1597c478bd9Sstevel@tonic-gate uberdata_t **__tdb_bootstrap = NULL; 1607c478bd9Sstevel@tonic-gate 1617c478bd9Sstevel@tonic-gate int thread_queue_fifo = 4; 1627c478bd9Sstevel@tonic-gate int thread_queue_dump = 0; 1637c478bd9Sstevel@tonic-gate int thread_cond_wait_defer = 0; 1647c478bd9Sstevel@tonic-gate int thread_error_detection = 0; 1657c478bd9Sstevel@tonic-gate int thread_async_safe = 0; 1667c478bd9Sstevel@tonic-gate int thread_stack_cache = 10; 1677c478bd9Sstevel@tonic-gate int thread_door_noreserve = 0; 1687c5714f6Sraf int thread_locks_misaligned = 0; 1697c478bd9Sstevel@tonic-gate 1707c478bd9Sstevel@tonic-gate static ulwp_t *ulwp_alloc(void); 1717c478bd9Sstevel@tonic-gate static void ulwp_free(ulwp_t *); 1727c478bd9Sstevel@tonic-gate 1737c478bd9Sstevel@tonic-gate /* 1747c478bd9Sstevel@tonic-gate * Insert the lwp into the hash table. 1757c478bd9Sstevel@tonic-gate */ 1767c478bd9Sstevel@tonic-gate void 1777c478bd9Sstevel@tonic-gate hash_in_unlocked(ulwp_t *ulwp, int ix, uberdata_t *udp) 1787c478bd9Sstevel@tonic-gate { 1797c478bd9Sstevel@tonic-gate ulwp->ul_hash = udp->thr_hash_table[ix].hash_bucket; 1807c478bd9Sstevel@tonic-gate udp->thr_hash_table[ix].hash_bucket = ulwp; 1817c478bd9Sstevel@tonic-gate ulwp->ul_ix = ix; 1827c478bd9Sstevel@tonic-gate } 1837c478bd9Sstevel@tonic-gate 1847c478bd9Sstevel@tonic-gate void 1857c478bd9Sstevel@tonic-gate hash_in(ulwp_t *ulwp, uberdata_t *udp) 1867c478bd9Sstevel@tonic-gate { 1877c478bd9Sstevel@tonic-gate int ix = TIDHASH(ulwp->ul_lwpid, udp); 1887c478bd9Sstevel@tonic-gate mutex_t *mp = &udp->thr_hash_table[ix].hash_lock; 1897c478bd9Sstevel@tonic-gate 1907c478bd9Sstevel@tonic-gate lmutex_lock(mp); 1917c478bd9Sstevel@tonic-gate hash_in_unlocked(ulwp, ix, udp); 1927c478bd9Sstevel@tonic-gate lmutex_unlock(mp); 1937c478bd9Sstevel@tonic-gate } 1947c478bd9Sstevel@tonic-gate 1957c478bd9Sstevel@tonic-gate /* 1967c478bd9Sstevel@tonic-gate * Delete the lwp from the hash table. 1977c478bd9Sstevel@tonic-gate */ 1987c478bd9Sstevel@tonic-gate void 1997c478bd9Sstevel@tonic-gate hash_out_unlocked(ulwp_t *ulwp, int ix, uberdata_t *udp) 2007c478bd9Sstevel@tonic-gate { 2017c478bd9Sstevel@tonic-gate ulwp_t **ulwpp; 2027c478bd9Sstevel@tonic-gate 2037c478bd9Sstevel@tonic-gate for (ulwpp = &udp->thr_hash_table[ix].hash_bucket; 2047c478bd9Sstevel@tonic-gate ulwp != *ulwpp; 2057c478bd9Sstevel@tonic-gate ulwpp = &(*ulwpp)->ul_hash) 2067c478bd9Sstevel@tonic-gate ; 2077c478bd9Sstevel@tonic-gate *ulwpp = ulwp->ul_hash; 2087c478bd9Sstevel@tonic-gate ulwp->ul_hash = NULL; 2097c478bd9Sstevel@tonic-gate ulwp->ul_ix = -1; 2107c478bd9Sstevel@tonic-gate } 2117c478bd9Sstevel@tonic-gate 2127c478bd9Sstevel@tonic-gate void 2137c478bd9Sstevel@tonic-gate hash_out(ulwp_t *ulwp, uberdata_t *udp) 2147c478bd9Sstevel@tonic-gate { 2157c478bd9Sstevel@tonic-gate int ix; 2167c478bd9Sstevel@tonic-gate 2177c478bd9Sstevel@tonic-gate if ((ix = ulwp->ul_ix) >= 0) { 2187c478bd9Sstevel@tonic-gate mutex_t *mp = &udp->thr_hash_table[ix].hash_lock; 2197c478bd9Sstevel@tonic-gate 2207c478bd9Sstevel@tonic-gate lmutex_lock(mp); 2217c478bd9Sstevel@tonic-gate hash_out_unlocked(ulwp, ix, udp); 2227c478bd9Sstevel@tonic-gate lmutex_unlock(mp); 2237c478bd9Sstevel@tonic-gate } 2247c478bd9Sstevel@tonic-gate } 2257c478bd9Sstevel@tonic-gate 226a574db85Sraf /* 227a574db85Sraf * Retain stack information for thread structures that are being recycled for 228a574db85Sraf * new threads. All other members of the thread structure should be zeroed. 229a574db85Sraf */ 2307c478bd9Sstevel@tonic-gate static void 2317c478bd9Sstevel@tonic-gate ulwp_clean(ulwp_t *ulwp) 2327c478bd9Sstevel@tonic-gate { 233a574db85Sraf caddr_t stk = ulwp->ul_stk; 234a574db85Sraf size_t mapsiz = ulwp->ul_mapsiz; 235a574db85Sraf size_t guardsize = ulwp->ul_guardsize; 236a574db85Sraf uintptr_t stktop = ulwp->ul_stktop; 237a574db85Sraf size_t stksiz = ulwp->ul_stksiz; 238a574db85Sraf 2398cd45542Sraf (void) memset(ulwp, 0, sizeof (*ulwp)); 240a574db85Sraf 241a574db85Sraf ulwp->ul_stk = stk; 242a574db85Sraf ulwp->ul_mapsiz = mapsiz; 243a574db85Sraf ulwp->ul_guardsize = guardsize; 244a574db85Sraf ulwp->ul_stktop = stktop; 245a574db85Sraf ulwp->ul_stksiz = stksiz; 2467c478bd9Sstevel@tonic-gate } 2477c478bd9Sstevel@tonic-gate 2487c478bd9Sstevel@tonic-gate static int stackprot; 2497c478bd9Sstevel@tonic-gate 2507c478bd9Sstevel@tonic-gate /* 2517c478bd9Sstevel@tonic-gate * Answer the question, "Is the lwp in question really dead?" 2527c478bd9Sstevel@tonic-gate * We must inquire of the operating system to be really sure 2537c478bd9Sstevel@tonic-gate * because the lwp may have called lwp_exit() but it has not 2547c478bd9Sstevel@tonic-gate * yet completed the exit. 2557c478bd9Sstevel@tonic-gate */ 2567c478bd9Sstevel@tonic-gate static int 2577c478bd9Sstevel@tonic-gate dead_and_buried(ulwp_t *ulwp) 2587c478bd9Sstevel@tonic-gate { 2597c478bd9Sstevel@tonic-gate if (ulwp->ul_lwpid == (lwpid_t)(-1)) 2607c478bd9Sstevel@tonic-gate return (1); 2617c478bd9Sstevel@tonic-gate if (ulwp->ul_dead && ulwp->ul_detached && 2627257d1b4Sraf _lwp_kill(ulwp->ul_lwpid, 0) == ESRCH) { 2637c478bd9Sstevel@tonic-gate ulwp->ul_lwpid = (lwpid_t)(-1); 2647c478bd9Sstevel@tonic-gate return (1); 2657c478bd9Sstevel@tonic-gate } 2667c478bd9Sstevel@tonic-gate return (0); 2677c478bd9Sstevel@tonic-gate } 2687c478bd9Sstevel@tonic-gate 2697c478bd9Sstevel@tonic-gate /* 2707c478bd9Sstevel@tonic-gate * Attempt to keep the stack cache within the specified cache limit. 2717c478bd9Sstevel@tonic-gate */ 2727c478bd9Sstevel@tonic-gate static void 2737c478bd9Sstevel@tonic-gate trim_stack_cache(int cache_limit) 2747c478bd9Sstevel@tonic-gate { 2757c478bd9Sstevel@tonic-gate ulwp_t *self = curthread; 2767c478bd9Sstevel@tonic-gate uberdata_t *udp = self->ul_uberdata; 2777c478bd9Sstevel@tonic-gate ulwp_t *prev = NULL; 2787c478bd9Sstevel@tonic-gate ulwp_t **ulwpp = &udp->lwp_stacks; 2797c478bd9Sstevel@tonic-gate ulwp_t *ulwp; 2807c478bd9Sstevel@tonic-gate 2817c478bd9Sstevel@tonic-gate ASSERT(udp->nthreads <= 1 || MUTEX_OWNED(&udp->link_lock, self)); 2827c478bd9Sstevel@tonic-gate 2837c478bd9Sstevel@tonic-gate while (udp->nfreestack > cache_limit && (ulwp = *ulwpp) != NULL) { 2847c478bd9Sstevel@tonic-gate if (dead_and_buried(ulwp)) { 2857c478bd9Sstevel@tonic-gate *ulwpp = ulwp->ul_next; 2867c478bd9Sstevel@tonic-gate if (ulwp == udp->lwp_laststack) 2877c478bd9Sstevel@tonic-gate udp->lwp_laststack = prev; 2887c478bd9Sstevel@tonic-gate hash_out(ulwp, udp); 2897c478bd9Sstevel@tonic-gate udp->nfreestack--; 2908cd45542Sraf (void) munmap(ulwp->ul_stk, ulwp->ul_mapsiz); 2917c478bd9Sstevel@tonic-gate /* 2927c478bd9Sstevel@tonic-gate * Now put the free ulwp on the ulwp freelist. 2937c478bd9Sstevel@tonic-gate */ 2947c478bd9Sstevel@tonic-gate ulwp->ul_mapsiz = 0; 2957c478bd9Sstevel@tonic-gate ulwp->ul_next = NULL; 2967c478bd9Sstevel@tonic-gate if (udp->ulwp_freelist == NULL) 2977c478bd9Sstevel@tonic-gate udp->ulwp_freelist = udp->ulwp_lastfree = ulwp; 2987c478bd9Sstevel@tonic-gate else { 2997c478bd9Sstevel@tonic-gate udp->ulwp_lastfree->ul_next = ulwp; 3007c478bd9Sstevel@tonic-gate udp->ulwp_lastfree = ulwp; 3017c478bd9Sstevel@tonic-gate } 3027c478bd9Sstevel@tonic-gate } else { 3037c478bd9Sstevel@tonic-gate prev = ulwp; 3047c478bd9Sstevel@tonic-gate ulwpp = &ulwp->ul_next; 3057c478bd9Sstevel@tonic-gate } 3067c478bd9Sstevel@tonic-gate } 3077c478bd9Sstevel@tonic-gate } 3087c478bd9Sstevel@tonic-gate 3097c478bd9Sstevel@tonic-gate /* 3107c478bd9Sstevel@tonic-gate * Find an unused stack of the requested size 3117c478bd9Sstevel@tonic-gate * or create a new stack of the requested size. 3127c478bd9Sstevel@tonic-gate * Return a pointer to the ulwp_t structure referring to the stack, or NULL. 3137c478bd9Sstevel@tonic-gate * thr_exit() stores 1 in the ul_dead member. 3147c478bd9Sstevel@tonic-gate * thr_join() stores -1 in the ul_lwpid member. 3157c478bd9Sstevel@tonic-gate */ 3163db34912SRoger A. Faulkner static ulwp_t * 3177c478bd9Sstevel@tonic-gate find_stack(size_t stksize, size_t guardsize) 3187c478bd9Sstevel@tonic-gate { 31934709573Sraf static size_t pagesize = 0; 32034709573Sraf 3217c478bd9Sstevel@tonic-gate uberdata_t *udp = curthread->ul_uberdata; 3227c478bd9Sstevel@tonic-gate size_t mapsize; 3237c478bd9Sstevel@tonic-gate ulwp_t *prev; 3247c478bd9Sstevel@tonic-gate ulwp_t *ulwp; 3257c478bd9Sstevel@tonic-gate ulwp_t **ulwpp; 3267c478bd9Sstevel@tonic-gate void *stk; 3277c478bd9Sstevel@tonic-gate 3287c478bd9Sstevel@tonic-gate /* 3297c478bd9Sstevel@tonic-gate * The stack is allocated PROT_READ|PROT_WRITE|PROT_EXEC 3307c478bd9Sstevel@tonic-gate * unless overridden by the system's configuration. 3317c478bd9Sstevel@tonic-gate */ 3327c478bd9Sstevel@tonic-gate if (stackprot == 0) { /* do this once */ 3337c478bd9Sstevel@tonic-gate long lprot = _sysconf(_SC_STACK_PROT); 3347c478bd9Sstevel@tonic-gate if (lprot <= 0) 3357c478bd9Sstevel@tonic-gate lprot = (PROT_READ|PROT_WRITE|PROT_EXEC); 3367c478bd9Sstevel@tonic-gate stackprot = (int)lprot; 3377c478bd9Sstevel@tonic-gate } 33834709573Sraf if (pagesize == 0) /* do this once */ 33934709573Sraf pagesize = _sysconf(_SC_PAGESIZE); 34034709573Sraf 3417c478bd9Sstevel@tonic-gate /* 3427c478bd9Sstevel@tonic-gate * One megabyte stacks by default, but subtract off 3437c478bd9Sstevel@tonic-gate * two pages for the system-created red zones. 3447c478bd9Sstevel@tonic-gate * Round up a non-zero stack size to a pagesize multiple. 3457c478bd9Sstevel@tonic-gate */ 3467c478bd9Sstevel@tonic-gate if (stksize == 0) 34734709573Sraf stksize = DEFAULTSTACK - 2 * pagesize; 3487c478bd9Sstevel@tonic-gate else 34934709573Sraf stksize = ((stksize + pagesize - 1) & -pagesize); 3507c478bd9Sstevel@tonic-gate 3517c478bd9Sstevel@tonic-gate /* 3527c478bd9Sstevel@tonic-gate * Round up the mapping size to a multiple of pagesize. 3537c478bd9Sstevel@tonic-gate * Note: mmap() provides at least one page of red zone 3547c478bd9Sstevel@tonic-gate * so we deduct that from the value of guardsize. 3557c478bd9Sstevel@tonic-gate */ 3567c478bd9Sstevel@tonic-gate if (guardsize != 0) 35734709573Sraf guardsize = ((guardsize + pagesize - 1) & -pagesize) - pagesize; 3587c478bd9Sstevel@tonic-gate mapsize = stksize + guardsize; 3597c478bd9Sstevel@tonic-gate 3607c478bd9Sstevel@tonic-gate lmutex_lock(&udp->link_lock); 3617c478bd9Sstevel@tonic-gate for (prev = NULL, ulwpp = &udp->lwp_stacks; 3627c478bd9Sstevel@tonic-gate (ulwp = *ulwpp) != NULL; 3637c478bd9Sstevel@tonic-gate prev = ulwp, ulwpp = &ulwp->ul_next) { 3647c478bd9Sstevel@tonic-gate if (ulwp->ul_mapsiz == mapsize && 3657c478bd9Sstevel@tonic-gate ulwp->ul_guardsize == guardsize && 3667c478bd9Sstevel@tonic-gate dead_and_buried(ulwp)) { 3677c478bd9Sstevel@tonic-gate /* 3687c478bd9Sstevel@tonic-gate * The previous lwp is gone; reuse the stack. 3697c478bd9Sstevel@tonic-gate * Remove the ulwp from the stack list. 3707c478bd9Sstevel@tonic-gate */ 3717c478bd9Sstevel@tonic-gate *ulwpp = ulwp->ul_next; 3727c478bd9Sstevel@tonic-gate ulwp->ul_next = NULL; 3737c478bd9Sstevel@tonic-gate if (ulwp == udp->lwp_laststack) 3747c478bd9Sstevel@tonic-gate udp->lwp_laststack = prev; 3757c478bd9Sstevel@tonic-gate hash_out(ulwp, udp); 3767c478bd9Sstevel@tonic-gate udp->nfreestack--; 3777c478bd9Sstevel@tonic-gate lmutex_unlock(&udp->link_lock); 3787c478bd9Sstevel@tonic-gate ulwp_clean(ulwp); 3797c478bd9Sstevel@tonic-gate return (ulwp); 3807c478bd9Sstevel@tonic-gate } 3817c478bd9Sstevel@tonic-gate } 3827c478bd9Sstevel@tonic-gate 3837c478bd9Sstevel@tonic-gate /* 3847c478bd9Sstevel@tonic-gate * None of the cached stacks matched our mapping size. 3857c478bd9Sstevel@tonic-gate * Reduce the stack cache to get rid of possibly 3867c478bd9Sstevel@tonic-gate * very old stacks that will never be reused. 3877c478bd9Sstevel@tonic-gate */ 3887c478bd9Sstevel@tonic-gate if (udp->nfreestack > udp->thread_stack_cache) 3897c478bd9Sstevel@tonic-gate trim_stack_cache(udp->thread_stack_cache); 3907c478bd9Sstevel@tonic-gate else if (udp->nfreestack > 0) 3917c478bd9Sstevel@tonic-gate trim_stack_cache(udp->nfreestack - 1); 3927c478bd9Sstevel@tonic-gate lmutex_unlock(&udp->link_lock); 3937c478bd9Sstevel@tonic-gate 3947c478bd9Sstevel@tonic-gate /* 3957c478bd9Sstevel@tonic-gate * Create a new stack. 3967c478bd9Sstevel@tonic-gate */ 3978cd45542Sraf if ((stk = mmap(NULL, mapsize, stackprot, 3987c478bd9Sstevel@tonic-gate MAP_PRIVATE|MAP_NORESERVE|MAP_ANON, -1, (off_t)0)) != MAP_FAILED) { 3997c478bd9Sstevel@tonic-gate /* 4007c478bd9Sstevel@tonic-gate * We have allocated our stack. Now allocate the ulwp. 4017c478bd9Sstevel@tonic-gate */ 4027c478bd9Sstevel@tonic-gate ulwp = ulwp_alloc(); 4037c478bd9Sstevel@tonic-gate if (ulwp == NULL) 4048cd45542Sraf (void) munmap(stk, mapsize); 4057c478bd9Sstevel@tonic-gate else { 4067c478bd9Sstevel@tonic-gate ulwp->ul_stk = stk; 4077c478bd9Sstevel@tonic-gate ulwp->ul_mapsiz = mapsize; 4087c478bd9Sstevel@tonic-gate ulwp->ul_guardsize = guardsize; 4097c478bd9Sstevel@tonic-gate ulwp->ul_stktop = (uintptr_t)stk + mapsize; 4107c478bd9Sstevel@tonic-gate ulwp->ul_stksiz = stksize; 4117c478bd9Sstevel@tonic-gate if (guardsize) /* protect the extra red zone */ 4128cd45542Sraf (void) mprotect(stk, guardsize, PROT_NONE); 4137c478bd9Sstevel@tonic-gate } 4147c478bd9Sstevel@tonic-gate } 4157c478bd9Sstevel@tonic-gate return (ulwp); 4167c478bd9Sstevel@tonic-gate } 4177c478bd9Sstevel@tonic-gate 4187c478bd9Sstevel@tonic-gate /* 4197c478bd9Sstevel@tonic-gate * Get a ulwp_t structure from the free list or allocate a new one. 4207c478bd9Sstevel@tonic-gate * Such ulwp_t's do not have a stack allocated by the library. 4217c478bd9Sstevel@tonic-gate */ 4227c478bd9Sstevel@tonic-gate static ulwp_t * 4237c478bd9Sstevel@tonic-gate ulwp_alloc(void) 4247c478bd9Sstevel@tonic-gate { 4257c478bd9Sstevel@tonic-gate ulwp_t *self = curthread; 4267c478bd9Sstevel@tonic-gate uberdata_t *udp = self->ul_uberdata; 4277c478bd9Sstevel@tonic-gate size_t tls_size; 4287c478bd9Sstevel@tonic-gate ulwp_t *prev; 4297c478bd9Sstevel@tonic-gate ulwp_t *ulwp; 4307c478bd9Sstevel@tonic-gate ulwp_t **ulwpp; 4317c478bd9Sstevel@tonic-gate caddr_t data; 4327c478bd9Sstevel@tonic-gate 4337c478bd9Sstevel@tonic-gate lmutex_lock(&udp->link_lock); 4347c478bd9Sstevel@tonic-gate for (prev = NULL, ulwpp = &udp->ulwp_freelist; 4357c478bd9Sstevel@tonic-gate (ulwp = *ulwpp) != NULL; 4367c478bd9Sstevel@tonic-gate prev = ulwp, ulwpp = &ulwp->ul_next) { 4377c478bd9Sstevel@tonic-gate if (dead_and_buried(ulwp)) { 4387c478bd9Sstevel@tonic-gate *ulwpp = ulwp->ul_next; 4397c478bd9Sstevel@tonic-gate ulwp->ul_next = NULL; 4407c478bd9Sstevel@tonic-gate if (ulwp == udp->ulwp_lastfree) 4417c478bd9Sstevel@tonic-gate udp->ulwp_lastfree = prev; 4427c478bd9Sstevel@tonic-gate hash_out(ulwp, udp); 4437c478bd9Sstevel@tonic-gate lmutex_unlock(&udp->link_lock); 4447c478bd9Sstevel@tonic-gate ulwp_clean(ulwp); 4457c478bd9Sstevel@tonic-gate return (ulwp); 4467c478bd9Sstevel@tonic-gate } 4477c478bd9Sstevel@tonic-gate } 4487c478bd9Sstevel@tonic-gate lmutex_unlock(&udp->link_lock); 4497c478bd9Sstevel@tonic-gate 4507c478bd9Sstevel@tonic-gate tls_size = roundup64(udp->tls_metadata.static_tls.tls_size); 4517c478bd9Sstevel@tonic-gate data = lmalloc(sizeof (*ulwp) + tls_size); 4527c478bd9Sstevel@tonic-gate if (data != NULL) { 4537c478bd9Sstevel@tonic-gate /* LINTED pointer cast may result in improper alignment */ 4547c478bd9Sstevel@tonic-gate ulwp = (ulwp_t *)(data + tls_size); 4557c478bd9Sstevel@tonic-gate } 4567c478bd9Sstevel@tonic-gate return (ulwp); 4577c478bd9Sstevel@tonic-gate } 4587c478bd9Sstevel@tonic-gate 4597c478bd9Sstevel@tonic-gate /* 4607c478bd9Sstevel@tonic-gate * Free a ulwp structure. 4617c478bd9Sstevel@tonic-gate * If there is an associated stack, put it on the stack list and 4627c478bd9Sstevel@tonic-gate * munmap() previously freed stacks up to the residual cache limit. 4637c478bd9Sstevel@tonic-gate * Else put it on the ulwp free list and never call lfree() on it. 4647c478bd9Sstevel@tonic-gate */ 4657c478bd9Sstevel@tonic-gate static void 4667c478bd9Sstevel@tonic-gate ulwp_free(ulwp_t *ulwp) 4677c478bd9Sstevel@tonic-gate { 4687c478bd9Sstevel@tonic-gate uberdata_t *udp = curthread->ul_uberdata; 4697c478bd9Sstevel@tonic-gate 4707c478bd9Sstevel@tonic-gate ASSERT(udp->nthreads <= 1 || MUTEX_OWNED(&udp->link_lock, curthread)); 4717c478bd9Sstevel@tonic-gate ulwp->ul_next = NULL; 4727c478bd9Sstevel@tonic-gate if (ulwp == udp->ulwp_one) /* don't reuse the primoridal stack */ 4737c478bd9Sstevel@tonic-gate /*EMPTY*/; 4747c478bd9Sstevel@tonic-gate else if (ulwp->ul_mapsiz != 0) { 4757c478bd9Sstevel@tonic-gate if (udp->lwp_stacks == NULL) 4767c478bd9Sstevel@tonic-gate udp->lwp_stacks = udp->lwp_laststack = ulwp; 4777c478bd9Sstevel@tonic-gate else { 4787c478bd9Sstevel@tonic-gate udp->lwp_laststack->ul_next = ulwp; 4797c478bd9Sstevel@tonic-gate udp->lwp_laststack = ulwp; 4807c478bd9Sstevel@tonic-gate } 4817c478bd9Sstevel@tonic-gate if (++udp->nfreestack > udp->thread_stack_cache) 4827c478bd9Sstevel@tonic-gate trim_stack_cache(udp->thread_stack_cache); 4837c478bd9Sstevel@tonic-gate } else { 4847c478bd9Sstevel@tonic-gate if (udp->ulwp_freelist == NULL) 4857c478bd9Sstevel@tonic-gate udp->ulwp_freelist = udp->ulwp_lastfree = ulwp; 4867c478bd9Sstevel@tonic-gate else { 4877c478bd9Sstevel@tonic-gate udp->ulwp_lastfree->ul_next = ulwp; 4887c478bd9Sstevel@tonic-gate udp->ulwp_lastfree = ulwp; 4897c478bd9Sstevel@tonic-gate } 4907c478bd9Sstevel@tonic-gate } 4917c478bd9Sstevel@tonic-gate } 4927c478bd9Sstevel@tonic-gate 4937c478bd9Sstevel@tonic-gate /* 4947c478bd9Sstevel@tonic-gate * Find a named lwp and return a pointer to its hash list location. 4957c478bd9Sstevel@tonic-gate * On success, returns with the hash lock held. 4967c478bd9Sstevel@tonic-gate */ 4977c478bd9Sstevel@tonic-gate ulwp_t ** 4987c478bd9Sstevel@tonic-gate find_lwpp(thread_t tid) 4997c478bd9Sstevel@tonic-gate { 5007c478bd9Sstevel@tonic-gate uberdata_t *udp = curthread->ul_uberdata; 5017c478bd9Sstevel@tonic-gate int ix = TIDHASH(tid, udp); 5027c478bd9Sstevel@tonic-gate mutex_t *mp = &udp->thr_hash_table[ix].hash_lock; 5037c478bd9Sstevel@tonic-gate ulwp_t *ulwp; 5047c478bd9Sstevel@tonic-gate ulwp_t **ulwpp; 5057c478bd9Sstevel@tonic-gate 5067c478bd9Sstevel@tonic-gate if (tid == 0) 5077c478bd9Sstevel@tonic-gate return (NULL); 5087c478bd9Sstevel@tonic-gate 5097c478bd9Sstevel@tonic-gate lmutex_lock(mp); 5107c478bd9Sstevel@tonic-gate for (ulwpp = &udp->thr_hash_table[ix].hash_bucket; 5117c478bd9Sstevel@tonic-gate (ulwp = *ulwpp) != NULL; 5127c478bd9Sstevel@tonic-gate ulwpp = &ulwp->ul_hash) { 5137c478bd9Sstevel@tonic-gate if (ulwp->ul_lwpid == tid) 5147c478bd9Sstevel@tonic-gate return (ulwpp); 5157c478bd9Sstevel@tonic-gate } 5167c478bd9Sstevel@tonic-gate lmutex_unlock(mp); 5177c478bd9Sstevel@tonic-gate return (NULL); 5187c478bd9Sstevel@tonic-gate } 5197c478bd9Sstevel@tonic-gate 5207c478bd9Sstevel@tonic-gate /* 5217c478bd9Sstevel@tonic-gate * Wake up all lwps waiting on this lwp for some reason. 5227c478bd9Sstevel@tonic-gate */ 5237c478bd9Sstevel@tonic-gate void 5247c478bd9Sstevel@tonic-gate ulwp_broadcast(ulwp_t *ulwp) 5257c478bd9Sstevel@tonic-gate { 5267c478bd9Sstevel@tonic-gate ulwp_t *self = curthread; 5277c478bd9Sstevel@tonic-gate uberdata_t *udp = self->ul_uberdata; 5287c478bd9Sstevel@tonic-gate 5297c478bd9Sstevel@tonic-gate ASSERT(MUTEX_OWNED(ulwp_mutex(ulwp, udp), self)); 5307257d1b4Sraf (void) cond_broadcast(ulwp_condvar(ulwp, udp)); 5317c478bd9Sstevel@tonic-gate } 5327c478bd9Sstevel@tonic-gate 5337c478bd9Sstevel@tonic-gate /* 5347c478bd9Sstevel@tonic-gate * Find a named lwp and return a pointer to it. 5357c478bd9Sstevel@tonic-gate * Returns with the hash lock held. 5367c478bd9Sstevel@tonic-gate */ 5377c478bd9Sstevel@tonic-gate ulwp_t * 5387c478bd9Sstevel@tonic-gate find_lwp(thread_t tid) 5397c478bd9Sstevel@tonic-gate { 5407c478bd9Sstevel@tonic-gate ulwp_t *self = curthread; 5417c478bd9Sstevel@tonic-gate uberdata_t *udp = self->ul_uberdata; 5427c478bd9Sstevel@tonic-gate ulwp_t *ulwp = NULL; 5437c478bd9Sstevel@tonic-gate ulwp_t **ulwpp; 5447c478bd9Sstevel@tonic-gate 5457c478bd9Sstevel@tonic-gate if (self->ul_lwpid == tid) { 5467c478bd9Sstevel@tonic-gate ulwp = self; 5477c478bd9Sstevel@tonic-gate ulwp_lock(ulwp, udp); 5487c478bd9Sstevel@tonic-gate } else if ((ulwpp = find_lwpp(tid)) != NULL) { 5497c478bd9Sstevel@tonic-gate ulwp = *ulwpp; 5507c478bd9Sstevel@tonic-gate } 5517c478bd9Sstevel@tonic-gate 5527c478bd9Sstevel@tonic-gate if (ulwp && ulwp->ul_dead) { 5537c478bd9Sstevel@tonic-gate ulwp_unlock(ulwp, udp); 5547c478bd9Sstevel@tonic-gate ulwp = NULL; 5557c478bd9Sstevel@tonic-gate } 5567c478bd9Sstevel@tonic-gate 5577c478bd9Sstevel@tonic-gate return (ulwp); 5587c478bd9Sstevel@tonic-gate } 5597c478bd9Sstevel@tonic-gate 5607c478bd9Sstevel@tonic-gate int 5617c478bd9Sstevel@tonic-gate _thrp_create(void *stk, size_t stksize, void *(*func)(void *), void *arg, 562d4204c85Sraf long flags, thread_t *new_thread, size_t guardsize) 5637c478bd9Sstevel@tonic-gate { 5647c478bd9Sstevel@tonic-gate ulwp_t *self = curthread; 5657c478bd9Sstevel@tonic-gate uberdata_t *udp = self->ul_uberdata; 5667c478bd9Sstevel@tonic-gate ucontext_t uc; 5677c478bd9Sstevel@tonic-gate uint_t lwp_flags; 5687c478bd9Sstevel@tonic-gate thread_t tid; 569373d25a2SRoger A. Faulkner int error; 5707c478bd9Sstevel@tonic-gate ulwp_t *ulwp; 5717c478bd9Sstevel@tonic-gate 5727c478bd9Sstevel@tonic-gate /* 5737c478bd9Sstevel@tonic-gate * Enforce the restriction of not creating any threads 5747c478bd9Sstevel@tonic-gate * until the primary link map has been initialized. 5757c478bd9Sstevel@tonic-gate * Also, disallow thread creation to a child of vfork(). 5767c478bd9Sstevel@tonic-gate */ 5777c478bd9Sstevel@tonic-gate if (!self->ul_primarymap || self->ul_vfork) 5787c478bd9Sstevel@tonic-gate return (ENOTSUP); 5797c478bd9Sstevel@tonic-gate 5807c478bd9Sstevel@tonic-gate if (udp->hash_size == 1) 5817c478bd9Sstevel@tonic-gate finish_init(); 5827c478bd9Sstevel@tonic-gate 583d4204c85Sraf if ((stk || stksize) && stksize < MINSTACK) 5847c478bd9Sstevel@tonic-gate return (EINVAL); 5857c478bd9Sstevel@tonic-gate 5867c478bd9Sstevel@tonic-gate if (stk == NULL) { 5877c478bd9Sstevel@tonic-gate if ((ulwp = find_stack(stksize, guardsize)) == NULL) 5887c478bd9Sstevel@tonic-gate return (ENOMEM); 5897c478bd9Sstevel@tonic-gate stksize = ulwp->ul_mapsiz - ulwp->ul_guardsize; 5907c478bd9Sstevel@tonic-gate } else { 5917c478bd9Sstevel@tonic-gate /* initialize the private stack */ 5927c478bd9Sstevel@tonic-gate if ((ulwp = ulwp_alloc()) == NULL) 5937c478bd9Sstevel@tonic-gate return (ENOMEM); 5947c478bd9Sstevel@tonic-gate ulwp->ul_stk = stk; 5957c478bd9Sstevel@tonic-gate ulwp->ul_stktop = (uintptr_t)stk + stksize; 5967c478bd9Sstevel@tonic-gate ulwp->ul_stksiz = stksize; 5977c478bd9Sstevel@tonic-gate } 5983db34912SRoger A. Faulkner /* ulwp is not in the hash table; make sure hash_out() doesn't fail */ 5993db34912SRoger A. Faulkner ulwp->ul_ix = -1; 6007c478bd9Sstevel@tonic-gate ulwp->ul_errnop = &ulwp->ul_errno; 6017c478bd9Sstevel@tonic-gate 6027c478bd9Sstevel@tonic-gate lwp_flags = LWP_SUSPENDED; 6037c478bd9Sstevel@tonic-gate if (flags & (THR_DETACHED|THR_DAEMON)) { 6047c478bd9Sstevel@tonic-gate flags |= THR_DETACHED; 6057c478bd9Sstevel@tonic-gate lwp_flags |= LWP_DETACHED; 6067c478bd9Sstevel@tonic-gate } 6077c478bd9Sstevel@tonic-gate if (flags & THR_DAEMON) 6087c478bd9Sstevel@tonic-gate lwp_flags |= LWP_DAEMON; 6097c478bd9Sstevel@tonic-gate 6107257d1b4Sraf /* creating a thread: enforce mt-correctness in mutex_lock() */ 6117c478bd9Sstevel@tonic-gate self->ul_async_safe = 1; 6127c478bd9Sstevel@tonic-gate 6137c478bd9Sstevel@tonic-gate /* per-thread copies of global variables, for speed */ 6147c478bd9Sstevel@tonic-gate ulwp->ul_queue_fifo = self->ul_queue_fifo; 6157c478bd9Sstevel@tonic-gate ulwp->ul_cond_wait_defer = self->ul_cond_wait_defer; 6167c478bd9Sstevel@tonic-gate ulwp->ul_error_detection = self->ul_error_detection; 6177c478bd9Sstevel@tonic-gate ulwp->ul_async_safe = self->ul_async_safe; 6187c478bd9Sstevel@tonic-gate ulwp->ul_max_spinners = self->ul_max_spinners; 6197c478bd9Sstevel@tonic-gate ulwp->ul_adaptive_spin = self->ul_adaptive_spin; 6207c478bd9Sstevel@tonic-gate ulwp->ul_queue_spin = self->ul_queue_spin; 6217c478bd9Sstevel@tonic-gate ulwp->ul_door_noreserve = self->ul_door_noreserve; 6227c5714f6Sraf ulwp->ul_misaligned = self->ul_misaligned; 6237c478bd9Sstevel@tonic-gate 624d4204c85Sraf /* new thread inherits creating thread's scheduling parameters */ 625d4204c85Sraf ulwp->ul_policy = self->ul_policy; 626d4204c85Sraf ulwp->ul_pri = (self->ul_epri? self->ul_epri : self->ul_pri); 627d4204c85Sraf ulwp->ul_cid = self->ul_cid; 628d4204c85Sraf ulwp->ul_rtclassid = self->ul_rtclassid; 629d4204c85Sraf 6307c478bd9Sstevel@tonic-gate ulwp->ul_primarymap = self->ul_primarymap; 6317c478bd9Sstevel@tonic-gate ulwp->ul_self = ulwp; 6327c478bd9Sstevel@tonic-gate ulwp->ul_uberdata = udp; 6337c478bd9Sstevel@tonic-gate 6347c478bd9Sstevel@tonic-gate /* debugger support */ 6357c478bd9Sstevel@tonic-gate ulwp->ul_usropts = flags; 6367c478bd9Sstevel@tonic-gate 6377c478bd9Sstevel@tonic-gate #ifdef __sparc 6387c478bd9Sstevel@tonic-gate /* 6397c478bd9Sstevel@tonic-gate * We cache several instructions in the thread structure for use 6407c478bd9Sstevel@tonic-gate * by the fasttrap DTrace provider. When changing this, read the 6417c478bd9Sstevel@tonic-gate * comment in fasttrap.h for the all the other places that must 6427c478bd9Sstevel@tonic-gate * be changed. 6437c478bd9Sstevel@tonic-gate */ 6447c478bd9Sstevel@tonic-gate ulwp->ul_dsave = 0x9de04000; /* save %g1, %g0, %sp */ 6457c478bd9Sstevel@tonic-gate ulwp->ul_drestore = 0x81e80000; /* restore %g0, %g0, %g0 */ 6467c478bd9Sstevel@tonic-gate ulwp->ul_dftret = 0x91d0203a; /* ta 0x3a */ 6477c478bd9Sstevel@tonic-gate ulwp->ul_dreturn = 0x81ca0000; /* return %o0 */ 6487c478bd9Sstevel@tonic-gate #endif 6497c478bd9Sstevel@tonic-gate 6507c478bd9Sstevel@tonic-gate ulwp->ul_startpc = func; 6517c478bd9Sstevel@tonic-gate ulwp->ul_startarg = arg; 6527c478bd9Sstevel@tonic-gate _fpinherit(ulwp); 6537c478bd9Sstevel@tonic-gate /* 6547c478bd9Sstevel@tonic-gate * Defer signals on the new thread until its TLS constructors 6557257d1b4Sraf * have been called. _thrp_setup() will call sigon() after 6567c478bd9Sstevel@tonic-gate * it has called tls_setup(). 6577c478bd9Sstevel@tonic-gate */ 6587c478bd9Sstevel@tonic-gate ulwp->ul_sigdefer = 1; 6597c478bd9Sstevel@tonic-gate 660373d25a2SRoger A. Faulkner error = setup_context(&uc, _thrp_setup, ulwp, 661373d25a2SRoger A. Faulkner (caddr_t)ulwp->ul_stk + ulwp->ul_guardsize, stksize); 662373d25a2SRoger A. Faulkner if (error != 0 && stk != NULL) /* inaccessible stack */ 663373d25a2SRoger A. Faulkner error = EFAULT; 6647c478bd9Sstevel@tonic-gate 6657c478bd9Sstevel@tonic-gate /* 6667c478bd9Sstevel@tonic-gate * Call enter_critical() to avoid being suspended until we 6677c478bd9Sstevel@tonic-gate * have linked the new thread into the proper lists. 6687c478bd9Sstevel@tonic-gate * This is necessary because forkall() and fork1() must 6697c478bd9Sstevel@tonic-gate * suspend all threads and they must see a complete list. 6707c478bd9Sstevel@tonic-gate */ 6717c478bd9Sstevel@tonic-gate enter_critical(self); 6727c478bd9Sstevel@tonic-gate uc.uc_sigmask = ulwp->ul_sigmask = self->ul_sigmask; 6737c478bd9Sstevel@tonic-gate if (error != 0 || 6747c478bd9Sstevel@tonic-gate (error = __lwp_create(&uc, lwp_flags, &tid)) != 0) { 6757c478bd9Sstevel@tonic-gate exit_critical(self); 6767c478bd9Sstevel@tonic-gate ulwp->ul_lwpid = (lwpid_t)(-1); 6777c478bd9Sstevel@tonic-gate ulwp->ul_dead = 1; 6787c478bd9Sstevel@tonic-gate ulwp->ul_detached = 1; 6797c478bd9Sstevel@tonic-gate lmutex_lock(&udp->link_lock); 6807c478bd9Sstevel@tonic-gate ulwp_free(ulwp); 6817c478bd9Sstevel@tonic-gate lmutex_unlock(&udp->link_lock); 6827c478bd9Sstevel@tonic-gate return (error); 6837c478bd9Sstevel@tonic-gate } 6847c478bd9Sstevel@tonic-gate self->ul_nocancel = 0; /* cancellation is now possible */ 6857c478bd9Sstevel@tonic-gate udp->uberflags.uf_mt = 1; 6867c478bd9Sstevel@tonic-gate if (new_thread) 6877c478bd9Sstevel@tonic-gate *new_thread = tid; 6887c478bd9Sstevel@tonic-gate if (flags & THR_DETACHED) 6897c478bd9Sstevel@tonic-gate ulwp->ul_detached = 1; 6907c478bd9Sstevel@tonic-gate ulwp->ul_lwpid = tid; 6917c478bd9Sstevel@tonic-gate ulwp->ul_stop = TSTP_REGULAR; 69236319254Sraf if (flags & THR_SUSPENDED) 6937c478bd9Sstevel@tonic-gate ulwp->ul_created = 1; 6947c478bd9Sstevel@tonic-gate 6957c478bd9Sstevel@tonic-gate lmutex_lock(&udp->link_lock); 6967c478bd9Sstevel@tonic-gate ulwp->ul_forw = udp->all_lwps; 6977c478bd9Sstevel@tonic-gate ulwp->ul_back = udp->all_lwps->ul_back; 6987c478bd9Sstevel@tonic-gate ulwp->ul_back->ul_forw = ulwp; 6997c478bd9Sstevel@tonic-gate ulwp->ul_forw->ul_back = ulwp; 7007c478bd9Sstevel@tonic-gate hash_in(ulwp, udp); 7017c478bd9Sstevel@tonic-gate udp->nthreads++; 7027c478bd9Sstevel@tonic-gate if (flags & THR_DAEMON) 7037c478bd9Sstevel@tonic-gate udp->ndaemons++; 7047c478bd9Sstevel@tonic-gate if (flags & THR_NEW_LWP) 7057c478bd9Sstevel@tonic-gate thr_concurrency++; 7061d2738a5Sraf __libc_threaded = 1; /* inform stdio */ 7077c478bd9Sstevel@tonic-gate lmutex_unlock(&udp->link_lock); 7087c478bd9Sstevel@tonic-gate 7097c478bd9Sstevel@tonic-gate if (__td_event_report(self, TD_CREATE, udp)) { 7107c478bd9Sstevel@tonic-gate self->ul_td_evbuf.eventnum = TD_CREATE; 7117c478bd9Sstevel@tonic-gate self->ul_td_evbuf.eventdata = (void *)(uintptr_t)tid; 7127c478bd9Sstevel@tonic-gate tdb_event(TD_CREATE, udp); 7137c478bd9Sstevel@tonic-gate } 7147c478bd9Sstevel@tonic-gate 7157c478bd9Sstevel@tonic-gate exit_critical(self); 71636319254Sraf 71736319254Sraf if (!(flags & THR_SUSPENDED)) 71836319254Sraf (void) _thrp_continue(tid, TSTP_REGULAR); 71936319254Sraf 7207c478bd9Sstevel@tonic-gate return (0); 7217c478bd9Sstevel@tonic-gate } 7227c478bd9Sstevel@tonic-gate 7237c478bd9Sstevel@tonic-gate int 7247257d1b4Sraf thr_create(void *stk, size_t stksize, void *(*func)(void *), void *arg, 7257c478bd9Sstevel@tonic-gate long flags, thread_t *new_thread) 7267c478bd9Sstevel@tonic-gate { 727d4204c85Sraf return (_thrp_create(stk, stksize, func, arg, flags, new_thread, 0)); 7287c478bd9Sstevel@tonic-gate } 7297c478bd9Sstevel@tonic-gate 7307c478bd9Sstevel@tonic-gate /* 7317c478bd9Sstevel@tonic-gate * A special cancellation cleanup hook for DCE. 7327c478bd9Sstevel@tonic-gate * cleanuphndlr, when it is not NULL, will contain a callback 7337c478bd9Sstevel@tonic-gate * function to be called before a thread is terminated in 7347257d1b4Sraf * thr_exit() as a result of being cancelled. 7357c478bd9Sstevel@tonic-gate */ 7367c478bd9Sstevel@tonic-gate static void (*cleanuphndlr)(void) = NULL; 7377c478bd9Sstevel@tonic-gate 7387c478bd9Sstevel@tonic-gate /* 7397c478bd9Sstevel@tonic-gate * _pthread_setcleanupinit: sets the cleanup hook. 7407c478bd9Sstevel@tonic-gate */ 7417c478bd9Sstevel@tonic-gate int 7427c478bd9Sstevel@tonic-gate _pthread_setcleanupinit(void (*func)(void)) 7437c478bd9Sstevel@tonic-gate { 7447c478bd9Sstevel@tonic-gate cleanuphndlr = func; 7457c478bd9Sstevel@tonic-gate return (0); 7467c478bd9Sstevel@tonic-gate } 7477c478bd9Sstevel@tonic-gate 7487c478bd9Sstevel@tonic-gate void 7497c478bd9Sstevel@tonic-gate _thrp_exit() 7507c478bd9Sstevel@tonic-gate { 7517c478bd9Sstevel@tonic-gate ulwp_t *self = curthread; 7527c478bd9Sstevel@tonic-gate uberdata_t *udp = self->ul_uberdata; 7537c478bd9Sstevel@tonic-gate ulwp_t *replace = NULL; 7547c478bd9Sstevel@tonic-gate 7557c478bd9Sstevel@tonic-gate if (__td_event_report(self, TD_DEATH, udp)) { 7567c478bd9Sstevel@tonic-gate self->ul_td_evbuf.eventnum = TD_DEATH; 7577c478bd9Sstevel@tonic-gate tdb_event(TD_DEATH, udp); 7587c478bd9Sstevel@tonic-gate } 7597c478bd9Sstevel@tonic-gate 7607c478bd9Sstevel@tonic-gate ASSERT(self->ul_sigdefer != 0); 7617c478bd9Sstevel@tonic-gate 7627c478bd9Sstevel@tonic-gate lmutex_lock(&udp->link_lock); 7637c478bd9Sstevel@tonic-gate udp->nthreads--; 7647c478bd9Sstevel@tonic-gate if (self->ul_usropts & THR_NEW_LWP) 7657c478bd9Sstevel@tonic-gate thr_concurrency--; 7667c478bd9Sstevel@tonic-gate if (self->ul_usropts & THR_DAEMON) 7677c478bd9Sstevel@tonic-gate udp->ndaemons--; 7687c478bd9Sstevel@tonic-gate else if (udp->nthreads == udp->ndaemons) { 7697c478bd9Sstevel@tonic-gate /* 7707c478bd9Sstevel@tonic-gate * We are the last non-daemon thread exiting. 7717c478bd9Sstevel@tonic-gate * Exit the process. We retain our TSD and TLS so 7727c478bd9Sstevel@tonic-gate * that atexit() application functions can use them. 7737c478bd9Sstevel@tonic-gate */ 7747c478bd9Sstevel@tonic-gate lmutex_unlock(&udp->link_lock); 7757c478bd9Sstevel@tonic-gate exit(0); 7767c478bd9Sstevel@tonic-gate thr_panic("_thrp_exit(): exit(0) returned"); 7777c478bd9Sstevel@tonic-gate } 7787c478bd9Sstevel@tonic-gate lmutex_unlock(&udp->link_lock); 7797c478bd9Sstevel@tonic-gate 78086dd6e44SYouzhong Yang /* 78186dd6e44SYouzhong Yang * tsd_exit() may call its destructor free(), thus depending on 78286dd6e44SYouzhong Yang * tmem, therefore tmem_exit() needs to be called after tsd_exit() 78386dd6e44SYouzhong Yang * and tls_exit(). 78486dd6e44SYouzhong Yang */ 7857c478bd9Sstevel@tonic-gate tsd_exit(); /* deallocate thread-specific data */ 7867c478bd9Sstevel@tonic-gate tls_exit(); /* deallocate thread-local storage */ 78786dd6e44SYouzhong Yang tmem_exit(); /* deallocate tmem allocations */ 788883492d5Sraf heldlock_exit(); /* deal with left-over held locks */ 7897c478bd9Sstevel@tonic-gate 7907c478bd9Sstevel@tonic-gate /* block all signals to finish exiting */ 7917c478bd9Sstevel@tonic-gate block_all_signals(self); 7927c478bd9Sstevel@tonic-gate /* also prevent ourself from being suspended */ 7937c478bd9Sstevel@tonic-gate enter_critical(self); 7947c478bd9Sstevel@tonic-gate rwl_free(self); 7957c478bd9Sstevel@tonic-gate lmutex_lock(&udp->link_lock); 7967c478bd9Sstevel@tonic-gate ulwp_free(self); 7977c478bd9Sstevel@tonic-gate (void) ulwp_lock(self, udp); 7987c478bd9Sstevel@tonic-gate 7997c478bd9Sstevel@tonic-gate if (self->ul_mapsiz && !self->ul_detached) { 8007c478bd9Sstevel@tonic-gate /* 8017c478bd9Sstevel@tonic-gate * We want to free the stack for reuse but must keep 8027c478bd9Sstevel@tonic-gate * the ulwp_t struct for the benefit of thr_join(). 8037c478bd9Sstevel@tonic-gate * For this purpose we allocate a replacement ulwp_t. 8047c478bd9Sstevel@tonic-gate */ 8057c478bd9Sstevel@tonic-gate if ((replace = udp->ulwp_replace_free) == NULL) 8067c478bd9Sstevel@tonic-gate replace = lmalloc(REPLACEMENT_SIZE); 8077c478bd9Sstevel@tonic-gate else if ((udp->ulwp_replace_free = replace->ul_next) == NULL) 8087c478bd9Sstevel@tonic-gate udp->ulwp_replace_last = NULL; 8097c478bd9Sstevel@tonic-gate } 8107c478bd9Sstevel@tonic-gate 8117c478bd9Sstevel@tonic-gate if (udp->all_lwps == self) 8127c478bd9Sstevel@tonic-gate udp->all_lwps = self->ul_forw; 8137c478bd9Sstevel@tonic-gate if (udp->all_lwps == self) 8147c478bd9Sstevel@tonic-gate udp->all_lwps = NULL; 8157c478bd9Sstevel@tonic-gate else { 8167c478bd9Sstevel@tonic-gate self->ul_forw->ul_back = self->ul_back; 8177c478bd9Sstevel@tonic-gate self->ul_back->ul_forw = self->ul_forw; 8187c478bd9Sstevel@tonic-gate } 8197c478bd9Sstevel@tonic-gate self->ul_forw = self->ul_back = NULL; 820d4204c85Sraf #if defined(THREAD_DEBUG) 8217c478bd9Sstevel@tonic-gate /* collect queue lock statistics before marking ourself dead */ 8227c478bd9Sstevel@tonic-gate record_spin_locks(self); 823d4204c85Sraf #endif 8247c478bd9Sstevel@tonic-gate self->ul_dead = 1; 8257c478bd9Sstevel@tonic-gate self->ul_pleasestop = 0; 8267c478bd9Sstevel@tonic-gate if (replace != NULL) { 8277c478bd9Sstevel@tonic-gate int ix = self->ul_ix; /* the hash index */ 8288cd45542Sraf (void) memcpy(replace, self, REPLACEMENT_SIZE); 8297c478bd9Sstevel@tonic-gate replace->ul_self = replace; 8307c478bd9Sstevel@tonic-gate replace->ul_next = NULL; /* clone not on stack list */ 8317c478bd9Sstevel@tonic-gate replace->ul_mapsiz = 0; /* allows clone to be freed */ 8327c478bd9Sstevel@tonic-gate replace->ul_replace = 1; /* requires clone to be freed */ 8337c478bd9Sstevel@tonic-gate hash_out_unlocked(self, ix, udp); 8347c478bd9Sstevel@tonic-gate hash_in_unlocked(replace, ix, udp); 8357c478bd9Sstevel@tonic-gate ASSERT(!(self->ul_detached)); 8367c478bd9Sstevel@tonic-gate self->ul_detached = 1; /* this frees the stack */ 8377c478bd9Sstevel@tonic-gate self->ul_schedctl = NULL; 8387c478bd9Sstevel@tonic-gate self->ul_schedctl_called = &udp->uberflags; 8397c478bd9Sstevel@tonic-gate set_curthread(self = replace); 8407c478bd9Sstevel@tonic-gate /* 8417c478bd9Sstevel@tonic-gate * Having just changed the address of curthread, we 8427c478bd9Sstevel@tonic-gate * must reset the ownership of the locks we hold so 8437c478bd9Sstevel@tonic-gate * that assertions will not fire when we release them. 8447c478bd9Sstevel@tonic-gate */ 8457c478bd9Sstevel@tonic-gate udp->link_lock.mutex_owner = (uintptr_t)self; 8467c478bd9Sstevel@tonic-gate ulwp_mutex(self, udp)->mutex_owner = (uintptr_t)self; 8477c478bd9Sstevel@tonic-gate /* 8487c478bd9Sstevel@tonic-gate * NOTE: 8497c478bd9Sstevel@tonic-gate * On i386, %gs still references the original, not the 8507c478bd9Sstevel@tonic-gate * replacement, ulwp structure. Fetching the replacement 8517c478bd9Sstevel@tonic-gate * curthread pointer via %gs:0 works correctly since the 8527c478bd9Sstevel@tonic-gate * original ulwp structure will not be reallocated until 8537c478bd9Sstevel@tonic-gate * this lwp has completed its lwp_exit() system call (see 8547c478bd9Sstevel@tonic-gate * dead_and_buried()), but from here on out, we must make 8557c478bd9Sstevel@tonic-gate * no references to %gs:<offset> other than %gs:0. 8567c478bd9Sstevel@tonic-gate */ 8577c478bd9Sstevel@tonic-gate } 8587c478bd9Sstevel@tonic-gate /* 8597c478bd9Sstevel@tonic-gate * Put non-detached terminated threads in the all_zombies list. 8607c478bd9Sstevel@tonic-gate */ 8617c478bd9Sstevel@tonic-gate if (!self->ul_detached) { 8627c478bd9Sstevel@tonic-gate udp->nzombies++; 8637c478bd9Sstevel@tonic-gate if (udp->all_zombies == NULL) { 8647c478bd9Sstevel@tonic-gate ASSERT(udp->nzombies == 1); 8657c478bd9Sstevel@tonic-gate udp->all_zombies = self->ul_forw = self->ul_back = self; 8667c478bd9Sstevel@tonic-gate } else { 8677c478bd9Sstevel@tonic-gate self->ul_forw = udp->all_zombies; 8687c478bd9Sstevel@tonic-gate self->ul_back = udp->all_zombies->ul_back; 8697c478bd9Sstevel@tonic-gate self->ul_back->ul_forw = self; 8707c478bd9Sstevel@tonic-gate self->ul_forw->ul_back = self; 8717c478bd9Sstevel@tonic-gate } 8727c478bd9Sstevel@tonic-gate } 8737c478bd9Sstevel@tonic-gate /* 8747c478bd9Sstevel@tonic-gate * Notify everyone waiting for this thread. 8757c478bd9Sstevel@tonic-gate */ 8767c478bd9Sstevel@tonic-gate ulwp_broadcast(self); 8777c478bd9Sstevel@tonic-gate (void) ulwp_unlock(self, udp); 8787c478bd9Sstevel@tonic-gate /* 8797c478bd9Sstevel@tonic-gate * Prevent any more references to the schedctl data. 8807c478bd9Sstevel@tonic-gate * We are exiting and continue_fork() may not find us. 8817c478bd9Sstevel@tonic-gate * Do this just before dropping link_lock, since fork 8827c478bd9Sstevel@tonic-gate * serializes on link_lock. 8837c478bd9Sstevel@tonic-gate */ 8847c478bd9Sstevel@tonic-gate self->ul_schedctl = NULL; 8857c478bd9Sstevel@tonic-gate self->ul_schedctl_called = &udp->uberflags; 8867c478bd9Sstevel@tonic-gate lmutex_unlock(&udp->link_lock); 8877c478bd9Sstevel@tonic-gate 8887c478bd9Sstevel@tonic-gate ASSERT(self->ul_critical == 1); 8897c478bd9Sstevel@tonic-gate ASSERT(self->ul_preempt == 0); 8907c478bd9Sstevel@tonic-gate _lwp_terminate(); /* never returns */ 8917c478bd9Sstevel@tonic-gate thr_panic("_thrp_exit(): _lwp_terminate() returned"); 8927c478bd9Sstevel@tonic-gate } 8937c478bd9Sstevel@tonic-gate 894d4204c85Sraf #if defined(THREAD_DEBUG) 8957c478bd9Sstevel@tonic-gate void 8967c478bd9Sstevel@tonic-gate collect_queue_statistics() 8977c478bd9Sstevel@tonic-gate { 8987c478bd9Sstevel@tonic-gate uberdata_t *udp = curthread->ul_uberdata; 8997c478bd9Sstevel@tonic-gate ulwp_t *ulwp; 9007c478bd9Sstevel@tonic-gate 9017c478bd9Sstevel@tonic-gate if (thread_queue_dump) { 9027c478bd9Sstevel@tonic-gate lmutex_lock(&udp->link_lock); 9037c478bd9Sstevel@tonic-gate if ((ulwp = udp->all_lwps) != NULL) { 9047c478bd9Sstevel@tonic-gate do { 9057c478bd9Sstevel@tonic-gate record_spin_locks(ulwp); 9067c478bd9Sstevel@tonic-gate } while ((ulwp = ulwp->ul_forw) != udp->all_lwps); 9077c478bd9Sstevel@tonic-gate } 9087c478bd9Sstevel@tonic-gate lmutex_unlock(&udp->link_lock); 9097c478bd9Sstevel@tonic-gate } 9107c478bd9Sstevel@tonic-gate } 911d4204c85Sraf #endif 9127c478bd9Sstevel@tonic-gate 9137257d1b4Sraf static void __NORETURN 9147257d1b4Sraf _thrp_exit_common(void *status, int unwind) 9157c478bd9Sstevel@tonic-gate { 9167c478bd9Sstevel@tonic-gate ulwp_t *self = curthread; 9177c478bd9Sstevel@tonic-gate int cancelled = (self->ul_cancel_pending && status == PTHREAD_CANCELED); 9187c478bd9Sstevel@tonic-gate 9197c478bd9Sstevel@tonic-gate ASSERT(self->ul_critical == 0 && self->ul_preempt == 0); 9207c478bd9Sstevel@tonic-gate 9217c478bd9Sstevel@tonic-gate /* 9227c478bd9Sstevel@tonic-gate * Disable cancellation and call the special DCE cancellation 9237c478bd9Sstevel@tonic-gate * cleanup hook if it is enabled. Do nothing else before calling 9247c478bd9Sstevel@tonic-gate * the DCE cancellation cleanup hook; it may call longjmp() and 9257c478bd9Sstevel@tonic-gate * never return here. 9267c478bd9Sstevel@tonic-gate */ 9277c478bd9Sstevel@tonic-gate self->ul_cancel_disabled = 1; 9287c478bd9Sstevel@tonic-gate self->ul_cancel_async = 0; 9297c478bd9Sstevel@tonic-gate self->ul_save_async = 0; 9307c478bd9Sstevel@tonic-gate self->ul_cancelable = 0; 9317c478bd9Sstevel@tonic-gate self->ul_cancel_pending = 0; 932a574db85Sraf set_cancel_pending_flag(self, 1); 9337c478bd9Sstevel@tonic-gate if (cancelled && cleanuphndlr != NULL) 9347c478bd9Sstevel@tonic-gate (*cleanuphndlr)(); 9357c478bd9Sstevel@tonic-gate 9367c478bd9Sstevel@tonic-gate /* 9377c478bd9Sstevel@tonic-gate * Block application signals while we are exiting. 9387c478bd9Sstevel@tonic-gate * We call out to C++, TSD, and TLS destructors while exiting 9397c478bd9Sstevel@tonic-gate * and these are application-defined, so we cannot be assured 9407c478bd9Sstevel@tonic-gate * that they won't reset the signal mask. We use sigoff() to 9417c478bd9Sstevel@tonic-gate * defer any signals that may be received as a result of this 9427c478bd9Sstevel@tonic-gate * bad behavior. Such signals will be lost to the process 9437c478bd9Sstevel@tonic-gate * when the thread finishes exiting. 9447c478bd9Sstevel@tonic-gate */ 9457257d1b4Sraf (void) thr_sigsetmask(SIG_SETMASK, &maskset, NULL); 9467c478bd9Sstevel@tonic-gate sigoff(self); 9477c478bd9Sstevel@tonic-gate 9487c478bd9Sstevel@tonic-gate self->ul_rval = status; 9497c478bd9Sstevel@tonic-gate 9507c478bd9Sstevel@tonic-gate /* 9517c478bd9Sstevel@tonic-gate * If thr_exit is being called from the places where 9527c478bd9Sstevel@tonic-gate * C++ destructors are to be called such as cancellation 9537c478bd9Sstevel@tonic-gate * points, then set this flag. It is checked in _t_cancel() 9547c478bd9Sstevel@tonic-gate * to decide whether _ex_unwind() is to be called or not. 9557c478bd9Sstevel@tonic-gate */ 9567c478bd9Sstevel@tonic-gate if (unwind) 9577c478bd9Sstevel@tonic-gate self->ul_unwind = 1; 9587c478bd9Sstevel@tonic-gate 9597c478bd9Sstevel@tonic-gate /* 9607c478bd9Sstevel@tonic-gate * _thrp_unwind() will eventually call _thrp_exit(). 9617c478bd9Sstevel@tonic-gate * It never returns. 9627c478bd9Sstevel@tonic-gate */ 9637c478bd9Sstevel@tonic-gate _thrp_unwind(NULL); 9647257d1b4Sraf thr_panic("_thrp_exit_common(): _thrp_unwind() returned"); 9657257d1b4Sraf 9667257d1b4Sraf for (;;) /* to shut the compiler up about __NORETURN */ 9677257d1b4Sraf continue; 9687c478bd9Sstevel@tonic-gate } 9697c478bd9Sstevel@tonic-gate 9707c478bd9Sstevel@tonic-gate /* 9717c478bd9Sstevel@tonic-gate * Called when a thread returns from its start function. 9727c478bd9Sstevel@tonic-gate * We are at the top of the stack; no unwinding is necessary. 9737c478bd9Sstevel@tonic-gate */ 9747c478bd9Sstevel@tonic-gate void 9757257d1b4Sraf _thrp_terminate(void *status) 9767c478bd9Sstevel@tonic-gate { 9777257d1b4Sraf _thrp_exit_common(status, 0); 9787c478bd9Sstevel@tonic-gate } 9797c478bd9Sstevel@tonic-gate 9807257d1b4Sraf #pragma weak pthread_exit = thr_exit 9817257d1b4Sraf #pragma weak _thr_exit = thr_exit 9827c478bd9Sstevel@tonic-gate void 9837257d1b4Sraf thr_exit(void *status) 9847c478bd9Sstevel@tonic-gate { 9857257d1b4Sraf _thrp_exit_common(status, 1); 9867c478bd9Sstevel@tonic-gate } 9877c478bd9Sstevel@tonic-gate 9887c478bd9Sstevel@tonic-gate int 9897c478bd9Sstevel@tonic-gate _thrp_join(thread_t tid, thread_t *departed, void **status, int do_cancel) 9907c478bd9Sstevel@tonic-gate { 9917c478bd9Sstevel@tonic-gate uberdata_t *udp = curthread->ul_uberdata; 9927c478bd9Sstevel@tonic-gate mutex_t *mp; 9937c478bd9Sstevel@tonic-gate void *rval; 9947c478bd9Sstevel@tonic-gate thread_t found; 9957c478bd9Sstevel@tonic-gate ulwp_t *ulwp; 9967c478bd9Sstevel@tonic-gate ulwp_t **ulwpp; 9977c478bd9Sstevel@tonic-gate int replace; 9987c478bd9Sstevel@tonic-gate int error; 9997c478bd9Sstevel@tonic-gate 10007c478bd9Sstevel@tonic-gate if (do_cancel) 10017c478bd9Sstevel@tonic-gate error = lwp_wait(tid, &found); 10027c478bd9Sstevel@tonic-gate else { 10037c478bd9Sstevel@tonic-gate while ((error = __lwp_wait(tid, &found)) == EINTR) 10047c478bd9Sstevel@tonic-gate ; 10057c478bd9Sstevel@tonic-gate } 10067c478bd9Sstevel@tonic-gate if (error) 10077c478bd9Sstevel@tonic-gate return (error); 10087c478bd9Sstevel@tonic-gate 10097c478bd9Sstevel@tonic-gate /* 10107c478bd9Sstevel@tonic-gate * We must hold link_lock to avoid a race condition with find_stack(). 10117c478bd9Sstevel@tonic-gate */ 10127c478bd9Sstevel@tonic-gate lmutex_lock(&udp->link_lock); 10137c478bd9Sstevel@tonic-gate if ((ulwpp = find_lwpp(found)) == NULL) { 10147c478bd9Sstevel@tonic-gate /* 10157c478bd9Sstevel@tonic-gate * lwp_wait() found an lwp that the library doesn't know 10167c478bd9Sstevel@tonic-gate * about. It must have been created with _lwp_create(). 10177c478bd9Sstevel@tonic-gate * Just return its lwpid; we can't know its status. 10187c478bd9Sstevel@tonic-gate */ 10197c478bd9Sstevel@tonic-gate lmutex_unlock(&udp->link_lock); 10207c478bd9Sstevel@tonic-gate rval = NULL; 10217c478bd9Sstevel@tonic-gate } else { 10227c478bd9Sstevel@tonic-gate /* 10237c478bd9Sstevel@tonic-gate * Remove ulwp from the hash table. 10247c478bd9Sstevel@tonic-gate */ 10257c478bd9Sstevel@tonic-gate ulwp = *ulwpp; 10267c478bd9Sstevel@tonic-gate *ulwpp = ulwp->ul_hash; 10277c478bd9Sstevel@tonic-gate ulwp->ul_hash = NULL; 10287c478bd9Sstevel@tonic-gate /* 10297c478bd9Sstevel@tonic-gate * Remove ulwp from all_zombies list. 10307c478bd9Sstevel@tonic-gate */ 10317c478bd9Sstevel@tonic-gate ASSERT(udp->nzombies >= 1); 10327c478bd9Sstevel@tonic-gate if (udp->all_zombies == ulwp) 10337c478bd9Sstevel@tonic-gate udp->all_zombies = ulwp->ul_forw; 10347c478bd9Sstevel@tonic-gate if (udp->all_zombies == ulwp) 10357c478bd9Sstevel@tonic-gate udp->all_zombies = NULL; 10367c478bd9Sstevel@tonic-gate else { 10377c478bd9Sstevel@tonic-gate ulwp->ul_forw->ul_back = ulwp->ul_back; 10387c478bd9Sstevel@tonic-gate ulwp->ul_back->ul_forw = ulwp->ul_forw; 10397c478bd9Sstevel@tonic-gate } 10407c478bd9Sstevel@tonic-gate ulwp->ul_forw = ulwp->ul_back = NULL; 10417c478bd9Sstevel@tonic-gate udp->nzombies--; 10427c478bd9Sstevel@tonic-gate ASSERT(ulwp->ul_dead && !ulwp->ul_detached && 10437c478bd9Sstevel@tonic-gate !(ulwp->ul_usropts & (THR_DETACHED|THR_DAEMON))); 10447c478bd9Sstevel@tonic-gate /* 10457c478bd9Sstevel@tonic-gate * We can't call ulwp_unlock(ulwp) after we set 10467c478bd9Sstevel@tonic-gate * ulwp->ul_ix = -1 so we have to get a pointer to the 10477c478bd9Sstevel@tonic-gate * ulwp's hash table mutex now in order to unlock it below. 10487c478bd9Sstevel@tonic-gate */ 10497c478bd9Sstevel@tonic-gate mp = ulwp_mutex(ulwp, udp); 10507c478bd9Sstevel@tonic-gate ulwp->ul_lwpid = (lwpid_t)(-1); 10517c478bd9Sstevel@tonic-gate ulwp->ul_ix = -1; 10527c478bd9Sstevel@tonic-gate rval = ulwp->ul_rval; 10537c478bd9Sstevel@tonic-gate replace = ulwp->ul_replace; 10547c478bd9Sstevel@tonic-gate lmutex_unlock(mp); 10557c478bd9Sstevel@tonic-gate if (replace) { 10567c478bd9Sstevel@tonic-gate ulwp->ul_next = NULL; 10577c478bd9Sstevel@tonic-gate if (udp->ulwp_replace_free == NULL) 10587c478bd9Sstevel@tonic-gate udp->ulwp_replace_free = 10597c478bd9Sstevel@tonic-gate udp->ulwp_replace_last = ulwp; 10607c478bd9Sstevel@tonic-gate else { 10617c478bd9Sstevel@tonic-gate udp->ulwp_replace_last->ul_next = ulwp; 10627c478bd9Sstevel@tonic-gate udp->ulwp_replace_last = ulwp; 10637c478bd9Sstevel@tonic-gate } 10647c478bd9Sstevel@tonic-gate } 10657c478bd9Sstevel@tonic-gate lmutex_unlock(&udp->link_lock); 10667c478bd9Sstevel@tonic-gate } 10677c478bd9Sstevel@tonic-gate 10687c478bd9Sstevel@tonic-gate if (departed != NULL) 10697c478bd9Sstevel@tonic-gate *departed = found; 10707c478bd9Sstevel@tonic-gate if (status != NULL) 10717c478bd9Sstevel@tonic-gate *status = rval; 10727c478bd9Sstevel@tonic-gate return (0); 10737c478bd9Sstevel@tonic-gate } 10747c478bd9Sstevel@tonic-gate 10757c478bd9Sstevel@tonic-gate int 10767257d1b4Sraf thr_join(thread_t tid, thread_t *departed, void **status) 10777c478bd9Sstevel@tonic-gate { 10787c478bd9Sstevel@tonic-gate int error = _thrp_join(tid, departed, status, 1); 10797c478bd9Sstevel@tonic-gate return ((error == EINVAL)? ESRCH : error); 10807c478bd9Sstevel@tonic-gate } 10817c478bd9Sstevel@tonic-gate 10827c478bd9Sstevel@tonic-gate /* 10837c478bd9Sstevel@tonic-gate * pthread_join() differs from Solaris thr_join(): 10847c478bd9Sstevel@tonic-gate * It does not return the departed thread's id 10857c478bd9Sstevel@tonic-gate * and hence does not have a "departed" argument. 10867c478bd9Sstevel@tonic-gate * It returns EINVAL if tid refers to a detached thread. 10877c478bd9Sstevel@tonic-gate */ 10887257d1b4Sraf #pragma weak _pthread_join = pthread_join 10897c478bd9Sstevel@tonic-gate int 10907257d1b4Sraf pthread_join(pthread_t tid, void **status) 10917c478bd9Sstevel@tonic-gate { 10927c478bd9Sstevel@tonic-gate return ((tid == 0)? ESRCH : _thrp_join(tid, NULL, status, 1)); 10937c478bd9Sstevel@tonic-gate } 10947c478bd9Sstevel@tonic-gate 10957c478bd9Sstevel@tonic-gate int 10967257d1b4Sraf pthread_detach(pthread_t tid) 10977c478bd9Sstevel@tonic-gate { 10987c478bd9Sstevel@tonic-gate uberdata_t *udp = curthread->ul_uberdata; 10997c478bd9Sstevel@tonic-gate ulwp_t *ulwp; 11007c478bd9Sstevel@tonic-gate ulwp_t **ulwpp; 11017c478bd9Sstevel@tonic-gate int error = 0; 11027c478bd9Sstevel@tonic-gate 11037c478bd9Sstevel@tonic-gate if ((ulwpp = find_lwpp(tid)) == NULL) 11047c478bd9Sstevel@tonic-gate return (ESRCH); 11057c478bd9Sstevel@tonic-gate ulwp = *ulwpp; 11067c478bd9Sstevel@tonic-gate 11077c478bd9Sstevel@tonic-gate if (ulwp->ul_dead) { 11087c478bd9Sstevel@tonic-gate ulwp_unlock(ulwp, udp); 11097c478bd9Sstevel@tonic-gate error = _thrp_join(tid, NULL, NULL, 0); 11107c478bd9Sstevel@tonic-gate } else { 11117c478bd9Sstevel@tonic-gate error = __lwp_detach(tid); 11127c478bd9Sstevel@tonic-gate ulwp->ul_detached = 1; 11137c478bd9Sstevel@tonic-gate ulwp->ul_usropts |= THR_DETACHED; 11147c478bd9Sstevel@tonic-gate ulwp_unlock(ulwp, udp); 11157c478bd9Sstevel@tonic-gate } 11167c478bd9Sstevel@tonic-gate return (error); 11177c478bd9Sstevel@tonic-gate } 11187c478bd9Sstevel@tonic-gate 11197c478bd9Sstevel@tonic-gate static const char * 11207c478bd9Sstevel@tonic-gate ematch(const char *ev, const char *match) 11217c478bd9Sstevel@tonic-gate { 11227c478bd9Sstevel@tonic-gate int c; 11237c478bd9Sstevel@tonic-gate 11247c478bd9Sstevel@tonic-gate while ((c = *match++) != '\0') { 11257c478bd9Sstevel@tonic-gate if (*ev++ != c) 11267c478bd9Sstevel@tonic-gate return (NULL); 11277c478bd9Sstevel@tonic-gate } 11287c478bd9Sstevel@tonic-gate if (*ev++ != '=') 11297c478bd9Sstevel@tonic-gate return (NULL); 11307c478bd9Sstevel@tonic-gate return (ev); 11317c478bd9Sstevel@tonic-gate } 11327c478bd9Sstevel@tonic-gate 11337c478bd9Sstevel@tonic-gate static int 11347c478bd9Sstevel@tonic-gate envvar(const char *ev, const char *match, int limit) 11357c478bd9Sstevel@tonic-gate { 11367c478bd9Sstevel@tonic-gate int val = -1; 11377c478bd9Sstevel@tonic-gate const char *ename; 11387c478bd9Sstevel@tonic-gate 11397c478bd9Sstevel@tonic-gate if ((ename = ematch(ev, match)) != NULL) { 11407c478bd9Sstevel@tonic-gate int c; 11417c478bd9Sstevel@tonic-gate for (val = 0; (c = *ename) != '\0'; ename++) { 11427c478bd9Sstevel@tonic-gate if (!isdigit(c)) { 11437c478bd9Sstevel@tonic-gate val = -1; 11447c478bd9Sstevel@tonic-gate break; 11457c478bd9Sstevel@tonic-gate } 11467c478bd9Sstevel@tonic-gate val = val * 10 + (c - '0'); 11477c478bd9Sstevel@tonic-gate if (val > limit) { 11487c478bd9Sstevel@tonic-gate val = limit; 11497c478bd9Sstevel@tonic-gate break; 11507c478bd9Sstevel@tonic-gate } 11517c478bd9Sstevel@tonic-gate } 11527c478bd9Sstevel@tonic-gate } 11537c478bd9Sstevel@tonic-gate return (val); 11547c478bd9Sstevel@tonic-gate } 11557c478bd9Sstevel@tonic-gate 11567c478bd9Sstevel@tonic-gate static void 11577c478bd9Sstevel@tonic-gate etest(const char *ev) 11587c478bd9Sstevel@tonic-gate { 11597c478bd9Sstevel@tonic-gate int value; 11607c478bd9Sstevel@tonic-gate 11617c478bd9Sstevel@tonic-gate if ((value = envvar(ev, "QUEUE_SPIN", 1000000)) >= 0) 11627c478bd9Sstevel@tonic-gate thread_queue_spin = value; 11635d1dd9a9Sraf if ((value = envvar(ev, "ADAPTIVE_SPIN", 1000000)) >= 0) 11647c478bd9Sstevel@tonic-gate thread_adaptive_spin = value; 11655d1dd9a9Sraf if ((value = envvar(ev, "MAX_SPINNERS", 255)) >= 0) 11667c478bd9Sstevel@tonic-gate thread_max_spinners = value; 11677c478bd9Sstevel@tonic-gate if ((value = envvar(ev, "QUEUE_FIFO", 8)) >= 0) 11687c478bd9Sstevel@tonic-gate thread_queue_fifo = value; 11697c478bd9Sstevel@tonic-gate #if defined(THREAD_DEBUG) 11707c478bd9Sstevel@tonic-gate if ((value = envvar(ev, "QUEUE_VERIFY", 1)) >= 0) 11717c478bd9Sstevel@tonic-gate thread_queue_verify = value; 11727c478bd9Sstevel@tonic-gate if ((value = envvar(ev, "QUEUE_DUMP", 1)) >= 0) 11737c478bd9Sstevel@tonic-gate thread_queue_dump = value; 1174d4204c85Sraf #endif 11757c478bd9Sstevel@tonic-gate if ((value = envvar(ev, "STACK_CACHE", 10000)) >= 0) 11767c478bd9Sstevel@tonic-gate thread_stack_cache = value; 11777c478bd9Sstevel@tonic-gate if ((value = envvar(ev, "COND_WAIT_DEFER", 1)) >= 0) 11787c478bd9Sstevel@tonic-gate thread_cond_wait_defer = value; 11797c478bd9Sstevel@tonic-gate if ((value = envvar(ev, "ERROR_DETECTION", 2)) >= 0) 11807c478bd9Sstevel@tonic-gate thread_error_detection = value; 11817c478bd9Sstevel@tonic-gate if ((value = envvar(ev, "ASYNC_SAFE", 1)) >= 0) 11827c478bd9Sstevel@tonic-gate thread_async_safe = value; 11837c478bd9Sstevel@tonic-gate if ((value = envvar(ev, "DOOR_NORESERVE", 1)) >= 0) 11847c478bd9Sstevel@tonic-gate thread_door_noreserve = value; 11857c5714f6Sraf if ((value = envvar(ev, "LOCKS_MISALIGNED", 1)) >= 0) 11867c5714f6Sraf thread_locks_misaligned = value; 11877c478bd9Sstevel@tonic-gate } 11887c478bd9Sstevel@tonic-gate 11897c478bd9Sstevel@tonic-gate /* 11907c478bd9Sstevel@tonic-gate * Look for and evaluate environment variables of the form "_THREAD_*". 11917c478bd9Sstevel@tonic-gate * For compatibility with the past, we also look for environment 11927c478bd9Sstevel@tonic-gate * names of the form "LIBTHREAD_*". 11937c478bd9Sstevel@tonic-gate */ 11947c478bd9Sstevel@tonic-gate static void 11957c478bd9Sstevel@tonic-gate set_thread_vars() 11967c478bd9Sstevel@tonic-gate { 119759f081edSraf extern const char **_environ; 11987c478bd9Sstevel@tonic-gate const char **pev; 11997c478bd9Sstevel@tonic-gate const char *ev; 12007c478bd9Sstevel@tonic-gate char c; 12017c478bd9Sstevel@tonic-gate 120259f081edSraf if ((pev = _environ) == NULL) 12037c478bd9Sstevel@tonic-gate return; 12047c478bd9Sstevel@tonic-gate while ((ev = *pev++) != NULL) { 12057c478bd9Sstevel@tonic-gate c = *ev; 12067257d1b4Sraf if (c == '_' && strncmp(ev, "_THREAD_", 8) == 0) 12077c478bd9Sstevel@tonic-gate etest(ev + 8); 12087257d1b4Sraf if (c == 'L' && strncmp(ev, "LIBTHREAD_", 10) == 0) 12097c478bd9Sstevel@tonic-gate etest(ev + 10); 12107c478bd9Sstevel@tonic-gate } 12117c478bd9Sstevel@tonic-gate } 12127c478bd9Sstevel@tonic-gate 12137c478bd9Sstevel@tonic-gate /* PROBE_SUPPORT begin */ 12147c478bd9Sstevel@tonic-gate #pragma weak __tnf_probe_notify 12157c478bd9Sstevel@tonic-gate extern void __tnf_probe_notify(void); 12167c478bd9Sstevel@tonic-gate /* PROBE_SUPPORT end */ 12177c478bd9Sstevel@tonic-gate 12187c478bd9Sstevel@tonic-gate /* same as atexit() but private to the library */ 12197c478bd9Sstevel@tonic-gate extern int _atexit(void (*)(void)); 12207c478bd9Sstevel@tonic-gate 12217c478bd9Sstevel@tonic-gate /* same as _cleanup() but private to the library */ 12227c478bd9Sstevel@tonic-gate extern void __cleanup(void); 12237c478bd9Sstevel@tonic-gate 12247c478bd9Sstevel@tonic-gate extern void atfork_init(void); 12257c478bd9Sstevel@tonic-gate 12267c478bd9Sstevel@tonic-gate #ifdef __amd64 1227d0b3732eSbholler extern void __proc64id(void); 12287c478bd9Sstevel@tonic-gate #endif 12297c478bd9Sstevel@tonic-gate 1230263f549eSPatrick Mooney static void 1231263f549eSPatrick Mooney init_auxv_data(uberdata_t *udp) 1232263f549eSPatrick Mooney { 1233263f549eSPatrick Mooney Dl_argsinfo_t args; 1234263f549eSPatrick Mooney 1235263f549eSPatrick Mooney udp->ub_comm_page = NULL; 1236263f549eSPatrick Mooney if (dlinfo(RTLD_SELF, RTLD_DI_ARGSINFO, &args) < 0) 1237263f549eSPatrick Mooney return; 1238263f549eSPatrick Mooney 1239263f549eSPatrick Mooney while (args.dla_auxv->a_type != AT_NULL) { 1240263f549eSPatrick Mooney if (args.dla_auxv->a_type == AT_SUN_COMMPAGE) { 1241263f549eSPatrick Mooney udp->ub_comm_page = args.dla_auxv->a_un.a_ptr; 1242263f549eSPatrick Mooney } 1243263f549eSPatrick Mooney args.dla_auxv++; 1244263f549eSPatrick Mooney } 1245263f549eSPatrick Mooney } 1246263f549eSPatrick Mooney 12477c478bd9Sstevel@tonic-gate /* 12487c478bd9Sstevel@tonic-gate * libc_init() is called by ld.so.1 for library initialization. 12497c478bd9Sstevel@tonic-gate * We perform minimal initialization; enough to work with the main thread. 12507c478bd9Sstevel@tonic-gate */ 12517c478bd9Sstevel@tonic-gate void 12527c478bd9Sstevel@tonic-gate libc_init(void) 12537c478bd9Sstevel@tonic-gate { 12547c478bd9Sstevel@tonic-gate uberdata_t *udp = &__uberdata; 12557c478bd9Sstevel@tonic-gate ulwp_t *oldself = __curthread(); 12567c478bd9Sstevel@tonic-gate ucontext_t uc; 12577c478bd9Sstevel@tonic-gate ulwp_t *self; 12587c478bd9Sstevel@tonic-gate struct rlimit rl; 12597c478bd9Sstevel@tonic-gate caddr_t data; 12607c478bd9Sstevel@tonic-gate size_t tls_size; 12617c478bd9Sstevel@tonic-gate int setmask; 12627c478bd9Sstevel@tonic-gate 12637c478bd9Sstevel@tonic-gate /* 12647c478bd9Sstevel@tonic-gate * For the initial stage of initialization, we must be careful 12657c478bd9Sstevel@tonic-gate * not to call any function that could possibly call _cerror(). 12667c478bd9Sstevel@tonic-gate * For this purpose, we call only the raw system call wrappers. 12677c478bd9Sstevel@tonic-gate */ 12687c478bd9Sstevel@tonic-gate 12697c478bd9Sstevel@tonic-gate #ifdef __amd64 12707c478bd9Sstevel@tonic-gate /* 12717c478bd9Sstevel@tonic-gate * Gather information about cache layouts for optimized 1272d0b3732eSbholler * AMD and Intel assembler strfoo() and memfoo() functions. 12737c478bd9Sstevel@tonic-gate */ 1274d0b3732eSbholler __proc64id(); 12757c478bd9Sstevel@tonic-gate #endif 12767c478bd9Sstevel@tonic-gate 12777c478bd9Sstevel@tonic-gate /* 12787c478bd9Sstevel@tonic-gate * Every libc, regardless of which link map, must register __cleanup(). 12797c478bd9Sstevel@tonic-gate */ 12807c478bd9Sstevel@tonic-gate (void) _atexit(__cleanup); 12817c478bd9Sstevel@tonic-gate 12827c478bd9Sstevel@tonic-gate /* 1283263f549eSPatrick Mooney * Every libc, regardless of link map, needs to go through and check 1284263f549eSPatrick Mooney * its aux vectors. Doing so will indicate whether or not this has 1285263f549eSPatrick Mooney * been given a comm page (to optimize certain system actions). 1286263f549eSPatrick Mooney */ 1287263f549eSPatrick Mooney init_auxv_data(udp); 1288263f549eSPatrick Mooney 1289263f549eSPatrick Mooney /* 12907c478bd9Sstevel@tonic-gate * We keep our uberdata on one of (a) the first alternate link map 12917c478bd9Sstevel@tonic-gate * or (b) the primary link map. We switch to the primary link map 12927c478bd9Sstevel@tonic-gate * and stay there once we see it. All intermediate link maps are 12937c478bd9Sstevel@tonic-gate * subject to being unloaded at any time. 12947c478bd9Sstevel@tonic-gate */ 12957c478bd9Sstevel@tonic-gate if (oldself != NULL && (oldself->ul_primarymap || !primary_link_map)) { 12967c478bd9Sstevel@tonic-gate __tdb_bootstrap = oldself->ul_uberdata->tdb_bootstrap; 12977c478bd9Sstevel@tonic-gate mutex_setup(); 12987c478bd9Sstevel@tonic-gate atfork_init(); /* every link map needs atfork() processing */ 129923a1cceaSRoger A. Faulkner init_progname(); 13007c478bd9Sstevel@tonic-gate return; 13017c478bd9Sstevel@tonic-gate } 13027c478bd9Sstevel@tonic-gate 13037c478bd9Sstevel@tonic-gate /* 13047c478bd9Sstevel@tonic-gate * To establish the main stack information, we have to get our context. 13057c478bd9Sstevel@tonic-gate * This is also convenient to use for getting our signal mask. 13067c478bd9Sstevel@tonic-gate */ 13077c478bd9Sstevel@tonic-gate uc.uc_flags = UC_ALL; 13088cd45542Sraf (void) __getcontext(&uc); 13097c478bd9Sstevel@tonic-gate ASSERT(uc.uc_link == NULL); 13107c478bd9Sstevel@tonic-gate 13117c478bd9Sstevel@tonic-gate tls_size = roundup64(udp->tls_metadata.static_tls.tls_size); 13127c478bd9Sstevel@tonic-gate ASSERT(primary_link_map || tls_size == 0); 13137c478bd9Sstevel@tonic-gate data = lmalloc(sizeof (ulwp_t) + tls_size); 13147c478bd9Sstevel@tonic-gate if (data == NULL) 13157c478bd9Sstevel@tonic-gate thr_panic("cannot allocate thread structure for main thread"); 13167c478bd9Sstevel@tonic-gate /* LINTED pointer cast may result in improper alignment */ 13177c478bd9Sstevel@tonic-gate self = (ulwp_t *)(data + tls_size); 13187c478bd9Sstevel@tonic-gate init_hash_table[0].hash_bucket = self; 13197c478bd9Sstevel@tonic-gate 13207c478bd9Sstevel@tonic-gate self->ul_sigmask = uc.uc_sigmask; 13217c478bd9Sstevel@tonic-gate delete_reserved_signals(&self->ul_sigmask); 13227c478bd9Sstevel@tonic-gate /* 13237c478bd9Sstevel@tonic-gate * Are the old and new sets different? 13247c478bd9Sstevel@tonic-gate * (This can happen if we are currently blocking SIGCANCEL.) 13257c478bd9Sstevel@tonic-gate * If so, we must explicitly set our signal mask, below. 13267c478bd9Sstevel@tonic-gate */ 13277c478bd9Sstevel@tonic-gate setmask = 13287c478bd9Sstevel@tonic-gate ((self->ul_sigmask.__sigbits[0] ^ uc.uc_sigmask.__sigbits[0]) | 1329bdf0047cSRoger A. Faulkner (self->ul_sigmask.__sigbits[1] ^ uc.uc_sigmask.__sigbits[1]) | 1330bdf0047cSRoger A. Faulkner (self->ul_sigmask.__sigbits[2] ^ uc.uc_sigmask.__sigbits[2]) | 1331bdf0047cSRoger A. Faulkner (self->ul_sigmask.__sigbits[3] ^ uc.uc_sigmask.__sigbits[3])); 13327c478bd9Sstevel@tonic-gate 13337c478bd9Sstevel@tonic-gate #ifdef __sparc 13347c478bd9Sstevel@tonic-gate /* 13357c478bd9Sstevel@tonic-gate * We cache several instructions in the thread structure for use 13367c478bd9Sstevel@tonic-gate * by the fasttrap DTrace provider. When changing this, read the 13377c478bd9Sstevel@tonic-gate * comment in fasttrap.h for the all the other places that must 13387c478bd9Sstevel@tonic-gate * be changed. 13397c478bd9Sstevel@tonic-gate */ 13407c478bd9Sstevel@tonic-gate self->ul_dsave = 0x9de04000; /* save %g1, %g0, %sp */ 13417c478bd9Sstevel@tonic-gate self->ul_drestore = 0x81e80000; /* restore %g0, %g0, %g0 */ 13427c478bd9Sstevel@tonic-gate self->ul_dftret = 0x91d0203a; /* ta 0x3a */ 13437c478bd9Sstevel@tonic-gate self->ul_dreturn = 0x81ca0000; /* return %o0 */ 13447c478bd9Sstevel@tonic-gate #endif 13457c478bd9Sstevel@tonic-gate 1346a574db85Sraf self->ul_stktop = (uintptr_t)uc.uc_stack.ss_sp + uc.uc_stack.ss_size; 13478cd45542Sraf (void) getrlimit(RLIMIT_STACK, &rl); 13487c478bd9Sstevel@tonic-gate self->ul_stksiz = rl.rlim_cur; 13497c478bd9Sstevel@tonic-gate self->ul_stk = (caddr_t)(self->ul_stktop - self->ul_stksiz); 13507c478bd9Sstevel@tonic-gate 13517c478bd9Sstevel@tonic-gate self->ul_forw = self->ul_back = self; 13527c478bd9Sstevel@tonic-gate self->ul_hash = NULL; 13537c478bd9Sstevel@tonic-gate self->ul_ix = 0; 13547257d1b4Sraf self->ul_lwpid = 1; /* _lwp_self() */ 13557c478bd9Sstevel@tonic-gate self->ul_main = 1; 13567c478bd9Sstevel@tonic-gate self->ul_self = self; 1357d4204c85Sraf self->ul_policy = -1; /* initialize only when needed */ 1358d4204c85Sraf self->ul_pri = 0; 1359d4204c85Sraf self->ul_cid = 0; 1360e84487aeSraf self->ul_rtclassid = -1; 13617c478bd9Sstevel@tonic-gate self->ul_uberdata = udp; 13627c478bd9Sstevel@tonic-gate if (oldself != NULL) { 13637c478bd9Sstevel@tonic-gate int i; 13647c478bd9Sstevel@tonic-gate 13657c478bd9Sstevel@tonic-gate ASSERT(primary_link_map); 13667c478bd9Sstevel@tonic-gate ASSERT(oldself->ul_main == 1); 13677c478bd9Sstevel@tonic-gate self->ul_stsd = oldself->ul_stsd; 13687c478bd9Sstevel@tonic-gate for (i = 0; i < TSD_NFAST; i++) 13697c478bd9Sstevel@tonic-gate self->ul_ftsd[i] = oldself->ul_ftsd[i]; 13707c478bd9Sstevel@tonic-gate self->ul_tls = oldself->ul_tls; 13717c478bd9Sstevel@tonic-gate /* 13727c478bd9Sstevel@tonic-gate * Retrieve all pointers to uberdata allocated 13737c478bd9Sstevel@tonic-gate * while running on previous link maps. 13740293487cSraf * We would like to do a structure assignment here, but 13750293487cSraf * gcc turns structure assignments into calls to memcpy(), 13760293487cSraf * a function exported from libc. We can't call any such 13770293487cSraf * external functions until we establish curthread, below, 13780293487cSraf * so we just call our private version of memcpy(). 13797c478bd9Sstevel@tonic-gate */ 13808cd45542Sraf (void) memcpy(udp, oldself->ul_uberdata, sizeof (*udp)); 13817c478bd9Sstevel@tonic-gate /* 13827c478bd9Sstevel@tonic-gate * These items point to global data on the primary link map. 13837c478bd9Sstevel@tonic-gate */ 13847c478bd9Sstevel@tonic-gate udp->thr_hash_table = init_hash_table; 13857c478bd9Sstevel@tonic-gate udp->sigacthandler = sigacthandler; 13867c478bd9Sstevel@tonic-gate udp->tdb.tdb_events = tdb_events; 13877c478bd9Sstevel@tonic-gate ASSERT(udp->nthreads == 1 && !udp->uberflags.uf_mt); 13887c478bd9Sstevel@tonic-gate ASSERT(udp->lwp_stacks == NULL); 13897c478bd9Sstevel@tonic-gate ASSERT(udp->ulwp_freelist == NULL); 13907c478bd9Sstevel@tonic-gate ASSERT(udp->ulwp_replace_free == NULL); 13917c478bd9Sstevel@tonic-gate ASSERT(udp->hash_size == 1); 13927c478bd9Sstevel@tonic-gate } 13937c478bd9Sstevel@tonic-gate udp->all_lwps = self; 13947c478bd9Sstevel@tonic-gate udp->ulwp_one = self; 13958cd45542Sraf udp->pid = getpid(); 13967c478bd9Sstevel@tonic-gate udp->nthreads = 1; 13977c478bd9Sstevel@tonic-gate /* 13987c478bd9Sstevel@tonic-gate * In every link map, tdb_bootstrap points to the same piece of 13997c478bd9Sstevel@tonic-gate * allocated memory. When the primary link map is initialized, 14007c478bd9Sstevel@tonic-gate * the allocated memory is assigned a pointer to the one true 14017c478bd9Sstevel@tonic-gate * uberdata. This allows libc_db to initialize itself regardless 14027c478bd9Sstevel@tonic-gate * of which instance of libc it finds in the address space. 14037c478bd9Sstevel@tonic-gate */ 14047c478bd9Sstevel@tonic-gate if (udp->tdb_bootstrap == NULL) 14057c478bd9Sstevel@tonic-gate udp->tdb_bootstrap = lmalloc(sizeof (uberdata_t *)); 14067c478bd9Sstevel@tonic-gate __tdb_bootstrap = udp->tdb_bootstrap; 14077c478bd9Sstevel@tonic-gate if (primary_link_map) { 14087c478bd9Sstevel@tonic-gate self->ul_primarymap = 1; 14097c478bd9Sstevel@tonic-gate udp->primary_map = 1; 14107c478bd9Sstevel@tonic-gate *udp->tdb_bootstrap = udp; 14117c478bd9Sstevel@tonic-gate } 14127c478bd9Sstevel@tonic-gate /* 14137c478bd9Sstevel@tonic-gate * Cancellation can't happen until: 14147c478bd9Sstevel@tonic-gate * pthread_cancel() is called 14157c478bd9Sstevel@tonic-gate * or: 14167c478bd9Sstevel@tonic-gate * another thread is created 14177c478bd9Sstevel@tonic-gate * For now, as a single-threaded process, set the flag that tells 14187c478bd9Sstevel@tonic-gate * PROLOGUE/EPILOGUE (in scalls.c) that cancellation can't happen. 14197c478bd9Sstevel@tonic-gate */ 14207c478bd9Sstevel@tonic-gate self->ul_nocancel = 1; 14217c478bd9Sstevel@tonic-gate 14227c478bd9Sstevel@tonic-gate #if defined(__amd64) 1423ae115bc7Smrj (void) ___lwp_private(_LWP_SETPRIVATE, _LWP_FSBASE, self); 14247c478bd9Sstevel@tonic-gate #elif defined(__i386) 1425ae115bc7Smrj (void) ___lwp_private(_LWP_SETPRIVATE, _LWP_GSBASE, self); 14267c478bd9Sstevel@tonic-gate #endif /* __i386 || __amd64 */ 14277c478bd9Sstevel@tonic-gate set_curthread(self); /* redundant on i386 */ 14287c478bd9Sstevel@tonic-gate /* 14297c478bd9Sstevel@tonic-gate * Now curthread is established and it is safe to call any 14307c478bd9Sstevel@tonic-gate * function in libc except one that uses thread-local storage. 14317c478bd9Sstevel@tonic-gate */ 14327c478bd9Sstevel@tonic-gate self->ul_errnop = &errno; 14337c478bd9Sstevel@tonic-gate if (oldself != NULL) { 14347c478bd9Sstevel@tonic-gate /* tls_size was zero when oldself was allocated */ 14357c478bd9Sstevel@tonic-gate lfree(oldself, sizeof (ulwp_t)); 14367c478bd9Sstevel@tonic-gate } 14377c478bd9Sstevel@tonic-gate mutex_setup(); 14387c478bd9Sstevel@tonic-gate atfork_init(); 14397c478bd9Sstevel@tonic-gate signal_init(); 14407c478bd9Sstevel@tonic-gate 14417c478bd9Sstevel@tonic-gate /* 14427c478bd9Sstevel@tonic-gate * If the stack is unlimited, we set the size to zero to disable 14437c478bd9Sstevel@tonic-gate * stack checking. 14447c478bd9Sstevel@tonic-gate * XXX: Work harder here. Get the stack size from /proc/self/rmap 14457c478bd9Sstevel@tonic-gate */ 14467c478bd9Sstevel@tonic-gate if (self->ul_stksiz == RLIM_INFINITY) { 14477c478bd9Sstevel@tonic-gate self->ul_ustack.ss_sp = (void *)self->ul_stktop; 14487c478bd9Sstevel@tonic-gate self->ul_ustack.ss_size = 0; 14497c478bd9Sstevel@tonic-gate } else { 14507c478bd9Sstevel@tonic-gate self->ul_ustack.ss_sp = self->ul_stk; 14517c478bd9Sstevel@tonic-gate self->ul_ustack.ss_size = self->ul_stksiz; 14527c478bd9Sstevel@tonic-gate } 14537c478bd9Sstevel@tonic-gate self->ul_ustack.ss_flags = 0; 14548cd45542Sraf (void) setustack(&self->ul_ustack); 14557c478bd9Sstevel@tonic-gate 14567c478bd9Sstevel@tonic-gate /* 14577c478bd9Sstevel@tonic-gate * Get the variables that affect thread behavior from the environment. 14587c478bd9Sstevel@tonic-gate */ 14597c478bd9Sstevel@tonic-gate set_thread_vars(); 14607c478bd9Sstevel@tonic-gate udp->uberflags.uf_thread_error_detection = (char)thread_error_detection; 14617c478bd9Sstevel@tonic-gate udp->thread_stack_cache = thread_stack_cache; 14627c478bd9Sstevel@tonic-gate 14637c478bd9Sstevel@tonic-gate /* 14647c478bd9Sstevel@tonic-gate * Make per-thread copies of global variables, for speed. 14657c478bd9Sstevel@tonic-gate */ 14667c478bd9Sstevel@tonic-gate self->ul_queue_fifo = (char)thread_queue_fifo; 14677c478bd9Sstevel@tonic-gate self->ul_cond_wait_defer = (char)thread_cond_wait_defer; 14687c478bd9Sstevel@tonic-gate self->ul_error_detection = (char)thread_error_detection; 14697c478bd9Sstevel@tonic-gate self->ul_async_safe = (char)thread_async_safe; 14707c478bd9Sstevel@tonic-gate self->ul_door_noreserve = (char)thread_door_noreserve; 14717c5714f6Sraf self->ul_misaligned = (char)thread_locks_misaligned; 14725d1dd9a9Sraf self->ul_max_spinners = (uint8_t)thread_max_spinners; 14737c478bd9Sstevel@tonic-gate self->ul_adaptive_spin = thread_adaptive_spin; 14747c478bd9Sstevel@tonic-gate self->ul_queue_spin = thread_queue_spin; 14757c478bd9Sstevel@tonic-gate 147635e6f27aSRoger A. Faulkner #if defined(__sparc) && !defined(_LP64) 147735e6f27aSRoger A. Faulkner if (self->ul_misaligned) { 147835e6f27aSRoger A. Faulkner /* 147935e6f27aSRoger A. Faulkner * Tell the kernel to fix up ldx/stx instructions that 148035e6f27aSRoger A. Faulkner * refer to non-8-byte aligned data instead of giving 148135e6f27aSRoger A. Faulkner * the process an alignment trap and generating SIGBUS. 148235e6f27aSRoger A. Faulkner * 148335e6f27aSRoger A. Faulkner * Programs compiled for 32-bit sparc with the Studio SS12 148435e6f27aSRoger A. Faulkner * compiler get this done for them automatically (in _init()). 148535e6f27aSRoger A. Faulkner * We do it here for the benefit of programs compiled with 148635e6f27aSRoger A. Faulkner * other compilers, like gcc. 148735e6f27aSRoger A. Faulkner * 148835e6f27aSRoger A. Faulkner * This is necessary for the _THREAD_LOCKS_MISALIGNED=1 148935e6f27aSRoger A. Faulkner * environment variable horrible hack to work. 149035e6f27aSRoger A. Faulkner */ 149135e6f27aSRoger A. Faulkner extern void _do_fix_align(void); 149235e6f27aSRoger A. Faulkner _do_fix_align(); 149335e6f27aSRoger A. Faulkner } 149435e6f27aSRoger A. Faulkner #endif 149535e6f27aSRoger A. Faulkner 14967c478bd9Sstevel@tonic-gate /* 14977c478bd9Sstevel@tonic-gate * When we have initialized the primary link map, inform 14987c478bd9Sstevel@tonic-gate * the dynamic linker about our interface functions. 149923a1cceaSRoger A. Faulkner * Set up our pointer to the program name. 15007c478bd9Sstevel@tonic-gate */ 15017c478bd9Sstevel@tonic-gate if (self->ul_primarymap) 15027c478bd9Sstevel@tonic-gate _ld_libc((void *)rtld_funcs); 150323a1cceaSRoger A. Faulkner init_progname(); 15047c478bd9Sstevel@tonic-gate 15057c478bd9Sstevel@tonic-gate /* 15067c478bd9Sstevel@tonic-gate * Defer signals until TLS constructors have been called. 15077c478bd9Sstevel@tonic-gate */ 15087c478bd9Sstevel@tonic-gate sigoff(self); 15097c478bd9Sstevel@tonic-gate tls_setup(); 15107c478bd9Sstevel@tonic-gate sigon(self); 15117c478bd9Sstevel@tonic-gate if (setmask) 15127c478bd9Sstevel@tonic-gate (void) restore_signals(self); 15137c478bd9Sstevel@tonic-gate 1514a574db85Sraf /* 1515a574db85Sraf * Make private copies of __xpg4 and __xpg6 so libc can test 1516a574db85Sraf * them after this point without invoking the dynamic linker. 1517a574db85Sraf */ 1518a574db85Sraf libc__xpg4 = __xpg4; 1519a574db85Sraf libc__xpg6 = __xpg6; 1520a574db85Sraf 15217c478bd9Sstevel@tonic-gate /* PROBE_SUPPORT begin */ 15227c478bd9Sstevel@tonic-gate if (self->ul_primarymap && __tnf_probe_notify != NULL) 15237c478bd9Sstevel@tonic-gate __tnf_probe_notify(); 15247c478bd9Sstevel@tonic-gate /* PROBE_SUPPORT end */ 1525f841f6adSraf 1526f841f6adSraf init_sigev_thread(); 1527f841f6adSraf init_aio(); 152880598c2fSnakanon 152980598c2fSnakanon /* 153080598c2fSnakanon * We need to reset __threaded dynamically at runtime, so that 153180598c2fSnakanon * __threaded can be bound to __threaded outside libc which may not 153280598c2fSnakanon * have initial value of 1 (without a copy relocation in a.out). 153380598c2fSnakanon */ 153480598c2fSnakanon __threaded = 1; 15357c478bd9Sstevel@tonic-gate } 15367c478bd9Sstevel@tonic-gate 15377c478bd9Sstevel@tonic-gate #pragma fini(libc_fini) 15387c478bd9Sstevel@tonic-gate void 15397c478bd9Sstevel@tonic-gate libc_fini() 15407c478bd9Sstevel@tonic-gate { 15417c478bd9Sstevel@tonic-gate /* 15427c478bd9Sstevel@tonic-gate * If we are doing fini processing for the instance of libc 15437c478bd9Sstevel@tonic-gate * on the first alternate link map (this happens only when 15447c478bd9Sstevel@tonic-gate * the dynamic linker rejects a bad audit library), then clear 15457c478bd9Sstevel@tonic-gate * __curthread(). We abandon whatever memory was allocated by 15467c478bd9Sstevel@tonic-gate * lmalloc() while running on this alternate link-map but we 15477c478bd9Sstevel@tonic-gate * don't care (and can't find the memory in any case); we just 15487c478bd9Sstevel@tonic-gate * want to protect the application from this bad audit library. 15497c478bd9Sstevel@tonic-gate * No fini processing is done by libc in the normal case. 15507c478bd9Sstevel@tonic-gate */ 15517c478bd9Sstevel@tonic-gate 15527c478bd9Sstevel@tonic-gate uberdata_t *udp = curthread->ul_uberdata; 15537c478bd9Sstevel@tonic-gate 15547c478bd9Sstevel@tonic-gate if (udp->primary_map == 0 && udp == &__uberdata) 15557c478bd9Sstevel@tonic-gate set_curthread(NULL); 15567c478bd9Sstevel@tonic-gate } 15577c478bd9Sstevel@tonic-gate 15587c478bd9Sstevel@tonic-gate /* 15597c478bd9Sstevel@tonic-gate * finish_init is called when we are about to become multi-threaded, 15607c478bd9Sstevel@tonic-gate * that is, on the first call to thr_create(). 15617c478bd9Sstevel@tonic-gate */ 15627c478bd9Sstevel@tonic-gate void 15637c478bd9Sstevel@tonic-gate finish_init() 15647c478bd9Sstevel@tonic-gate { 15657c478bd9Sstevel@tonic-gate ulwp_t *self = curthread; 15667c478bd9Sstevel@tonic-gate uberdata_t *udp = self->ul_uberdata; 15677c478bd9Sstevel@tonic-gate thr_hash_table_t *htp; 15687c478bd9Sstevel@tonic-gate void *data; 15697c478bd9Sstevel@tonic-gate int i; 15707c478bd9Sstevel@tonic-gate 15717c478bd9Sstevel@tonic-gate /* 15727c478bd9Sstevel@tonic-gate * No locks needed here; we are single-threaded on the first call. 15737c478bd9Sstevel@tonic-gate * We can be called only after the primary link map has been set up. 15747c478bd9Sstevel@tonic-gate */ 15757c478bd9Sstevel@tonic-gate ASSERT(self->ul_primarymap); 15767c478bd9Sstevel@tonic-gate ASSERT(self == udp->ulwp_one); 15777c478bd9Sstevel@tonic-gate ASSERT(!udp->uberflags.uf_mt); 15787c478bd9Sstevel@tonic-gate ASSERT(udp->hash_size == 1); 15797c478bd9Sstevel@tonic-gate 15807c478bd9Sstevel@tonic-gate /* 1581d4204c85Sraf * Initialize self->ul_policy, self->ul_cid, and self->ul_pri. 1582d4204c85Sraf */ 1583d4204c85Sraf update_sched(self); 1584d4204c85Sraf 1585d4204c85Sraf /* 1586d4204c85Sraf * Allocate the queue_head array if not already allocated. 15877c478bd9Sstevel@tonic-gate */ 15887c478bd9Sstevel@tonic-gate if (udp->queue_head == NULL) 15897c478bd9Sstevel@tonic-gate queue_alloc(); 15907c478bd9Sstevel@tonic-gate 15917c478bd9Sstevel@tonic-gate /* 15927c478bd9Sstevel@tonic-gate * Now allocate the thread hash table. 15937c478bd9Sstevel@tonic-gate */ 15948cd45542Sraf if ((data = mmap(NULL, HASHTBLSZ * sizeof (thr_hash_table_t), 15957c478bd9Sstevel@tonic-gate PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, -1, (off_t)0)) 15967c478bd9Sstevel@tonic-gate == MAP_FAILED) 15977c478bd9Sstevel@tonic-gate thr_panic("cannot allocate thread hash table"); 15987c478bd9Sstevel@tonic-gate 15997c478bd9Sstevel@tonic-gate udp->thr_hash_table = htp = (thr_hash_table_t *)data; 16007c478bd9Sstevel@tonic-gate udp->hash_size = HASHTBLSZ; 16017c478bd9Sstevel@tonic-gate udp->hash_mask = HASHTBLSZ - 1; 16027c478bd9Sstevel@tonic-gate 16037c478bd9Sstevel@tonic-gate for (i = 0; i < HASHTBLSZ; i++, htp++) { 1604883492d5Sraf htp->hash_lock.mutex_flag = LOCK_INITED; 16057c478bd9Sstevel@tonic-gate htp->hash_lock.mutex_magic = MUTEX_MAGIC; 16067c478bd9Sstevel@tonic-gate htp->hash_cond.cond_magic = COND_MAGIC; 16077c478bd9Sstevel@tonic-gate } 16087c478bd9Sstevel@tonic-gate hash_in_unlocked(self, TIDHASH(self->ul_lwpid, udp), udp); 16097c478bd9Sstevel@tonic-gate 16107c478bd9Sstevel@tonic-gate /* 16117c478bd9Sstevel@tonic-gate * Set up the SIGCANCEL handler for threads cancellation. 16127c478bd9Sstevel@tonic-gate */ 1613f841f6adSraf setup_cancelsig(SIGCANCEL); 16147c478bd9Sstevel@tonic-gate 16157c478bd9Sstevel@tonic-gate /* 16167c478bd9Sstevel@tonic-gate * Arrange to do special things on exit -- 16177c478bd9Sstevel@tonic-gate * - collect queue statistics from all remaining active threads. 1618d4204c85Sraf * - dump queue statistics to stderr if _THREAD_QUEUE_DUMP is set. 16197c478bd9Sstevel@tonic-gate * - grab assert_lock to ensure that assertion failures 16207c478bd9Sstevel@tonic-gate * and a core dump take precedence over _exit(). 16217c478bd9Sstevel@tonic-gate * (Functions are called in the reverse order of their registration.) 16227c478bd9Sstevel@tonic-gate */ 16237c478bd9Sstevel@tonic-gate (void) _atexit(grab_assert_lock); 1624d4204c85Sraf #if defined(THREAD_DEBUG) 1625d4204c85Sraf (void) _atexit(dump_queue_statistics); 16267c478bd9Sstevel@tonic-gate (void) _atexit(collect_queue_statistics); 1627d4204c85Sraf #endif 16287c478bd9Sstevel@tonic-gate } 16297c478bd9Sstevel@tonic-gate 16307c478bd9Sstevel@tonic-gate /* 1631657b1f3dSraf * Used only by postfork1_child(), below. 16327c478bd9Sstevel@tonic-gate */ 16337c478bd9Sstevel@tonic-gate static void 16347c478bd9Sstevel@tonic-gate mark_dead_and_buried(ulwp_t *ulwp) 16357c478bd9Sstevel@tonic-gate { 16367c478bd9Sstevel@tonic-gate ulwp->ul_dead = 1; 16377c478bd9Sstevel@tonic-gate ulwp->ul_lwpid = (lwpid_t)(-1); 16387c478bd9Sstevel@tonic-gate ulwp->ul_hash = NULL; 16397c478bd9Sstevel@tonic-gate ulwp->ul_ix = -1; 16407c478bd9Sstevel@tonic-gate ulwp->ul_schedctl = NULL; 16417c478bd9Sstevel@tonic-gate ulwp->ul_schedctl_called = NULL; 16427c478bd9Sstevel@tonic-gate } 16437c478bd9Sstevel@tonic-gate 16447c478bd9Sstevel@tonic-gate /* 16457c478bd9Sstevel@tonic-gate * This is called from fork1() in the child. 16467c478bd9Sstevel@tonic-gate * Reset our data structures to reflect one lwp. 16477c478bd9Sstevel@tonic-gate */ 16487c478bd9Sstevel@tonic-gate void 1649f841f6adSraf postfork1_child() 16507c478bd9Sstevel@tonic-gate { 16517c478bd9Sstevel@tonic-gate ulwp_t *self = curthread; 16527c478bd9Sstevel@tonic-gate uberdata_t *udp = self->ul_uberdata; 1653d4204c85Sraf queue_head_t *qp; 16547c478bd9Sstevel@tonic-gate ulwp_t *next; 16557c478bd9Sstevel@tonic-gate ulwp_t *ulwp; 16567c478bd9Sstevel@tonic-gate int i; 16577c478bd9Sstevel@tonic-gate 16587c478bd9Sstevel@tonic-gate /* daemon threads shouldn't call fork1(), but oh well... */ 16597c478bd9Sstevel@tonic-gate self->ul_usropts &= ~THR_DAEMON; 16607c478bd9Sstevel@tonic-gate udp->nthreads = 1; 16617c478bd9Sstevel@tonic-gate udp->ndaemons = 0; 16627c478bd9Sstevel@tonic-gate udp->uberflags.uf_mt = 0; 16631d2738a5Sraf __libc_threaded = 0; 16647c478bd9Sstevel@tonic-gate for (i = 0; i < udp->hash_size; i++) 16657c478bd9Sstevel@tonic-gate udp->thr_hash_table[i].hash_bucket = NULL; 16667257d1b4Sraf self->ul_lwpid = _lwp_self(); 16677c478bd9Sstevel@tonic-gate hash_in_unlocked(self, TIDHASH(self->ul_lwpid, udp), udp); 16687c478bd9Sstevel@tonic-gate 166998c1a6b4Sraf /* 16708cd45542Sraf * Some thread in the parent might have been suspended 16718cd45542Sraf * while holding udp->callout_lock or udp->ld_lock. 16728cd45542Sraf * Reinitialize the child's copies. 167398c1a6b4Sraf */ 16747257d1b4Sraf (void) mutex_init(&udp->callout_lock, 16757257d1b4Sraf USYNC_THREAD | LOCK_RECURSIVE, NULL); 16767257d1b4Sraf (void) mutex_init(&udp->ld_lock, 16777257d1b4Sraf USYNC_THREAD | LOCK_RECURSIVE, NULL); 167898c1a6b4Sraf 16797c478bd9Sstevel@tonic-gate /* no one in the child is on a sleep queue; reinitialize */ 1680d4204c85Sraf if ((qp = udp->queue_head) != NULL) { 16818cd45542Sraf (void) memset(qp, 0, 2 * QHASHSIZE * sizeof (queue_head_t)); 1682d4204c85Sraf for (i = 0; i < 2 * QHASHSIZE; qp++, i++) { 1683d4204c85Sraf qp->qh_type = (i < QHASHSIZE)? MX : CV; 1684d4204c85Sraf qp->qh_lock.mutex_flag = LOCK_INITED; 1685d4204c85Sraf qp->qh_lock.mutex_magic = MUTEX_MAGIC; 1686d4204c85Sraf qp->qh_hlist = &qp->qh_def_root; 1687d4204c85Sraf #if defined(THREAD_DEBUG) 1688d4204c85Sraf qp->qh_hlen = 1; 1689d4204c85Sraf qp->qh_hmax = 1; 1690d4204c85Sraf #endif 1691883492d5Sraf } 16927c478bd9Sstevel@tonic-gate } 16937c478bd9Sstevel@tonic-gate 16947c478bd9Sstevel@tonic-gate /* 1695c4a8d66cSRoger A. Faulkner * Do post-fork1 processing for subsystems that need it. 1696c4a8d66cSRoger A. Faulkner * We need to do this before unmapping all of the abandoned 1697c4a8d66cSRoger A. Faulkner * threads' stacks, below(), because the post-fork1 actions 1698c4a8d66cSRoger A. Faulkner * might require access to those stacks. 1699c4a8d66cSRoger A. Faulkner */ 1700c4a8d66cSRoger A. Faulkner postfork1_child_sigev_aio(); 1701c4a8d66cSRoger A. Faulkner postfork1_child_sigev_mq(); 1702c4a8d66cSRoger A. Faulkner postfork1_child_sigev_timer(); 1703c4a8d66cSRoger A. Faulkner postfork1_child_aio(); 1704c4a8d66cSRoger A. Faulkner /* 1705c4a8d66cSRoger A. Faulkner * The above subsystems use thread pools, so this action 1706c4a8d66cSRoger A. Faulkner * must be performed after those actions. 1707c4a8d66cSRoger A. Faulkner */ 1708c4a8d66cSRoger A. Faulkner postfork1_child_tpool(); 1709c4a8d66cSRoger A. Faulkner 1710c4a8d66cSRoger A. Faulkner /* 17117c478bd9Sstevel@tonic-gate * All lwps except ourself are gone. Mark them so. 17127c478bd9Sstevel@tonic-gate * First mark all of the lwps that have already been freed. 17137c478bd9Sstevel@tonic-gate * Then mark and free all of the active lwps except ourself. 17147c478bd9Sstevel@tonic-gate * Since we are single-threaded, no locks are required here. 17157c478bd9Sstevel@tonic-gate */ 17167c478bd9Sstevel@tonic-gate for (ulwp = udp->lwp_stacks; ulwp != NULL; ulwp = ulwp->ul_next) 17177c478bd9Sstevel@tonic-gate mark_dead_and_buried(ulwp); 17187c478bd9Sstevel@tonic-gate for (ulwp = udp->ulwp_freelist; ulwp != NULL; ulwp = ulwp->ul_next) 17197c478bd9Sstevel@tonic-gate mark_dead_and_buried(ulwp); 17207c478bd9Sstevel@tonic-gate for (ulwp = self->ul_forw; ulwp != self; ulwp = next) { 17217c478bd9Sstevel@tonic-gate next = ulwp->ul_forw; 17227c478bd9Sstevel@tonic-gate ulwp->ul_forw = ulwp->ul_back = NULL; 17237c478bd9Sstevel@tonic-gate mark_dead_and_buried(ulwp); 17247c478bd9Sstevel@tonic-gate tsd_free(ulwp); 17257c478bd9Sstevel@tonic-gate tls_free(ulwp); 17267c478bd9Sstevel@tonic-gate rwl_free(ulwp); 1727883492d5Sraf heldlock_free(ulwp); 17287c478bd9Sstevel@tonic-gate ulwp_free(ulwp); 17297c478bd9Sstevel@tonic-gate } 17307c478bd9Sstevel@tonic-gate self->ul_forw = self->ul_back = udp->all_lwps = self; 17317c478bd9Sstevel@tonic-gate if (self != udp->ulwp_one) 17327c478bd9Sstevel@tonic-gate mark_dead_and_buried(udp->ulwp_one); 17337c478bd9Sstevel@tonic-gate if ((ulwp = udp->all_zombies) != NULL) { 17347c478bd9Sstevel@tonic-gate ASSERT(udp->nzombies != 0); 17357c478bd9Sstevel@tonic-gate do { 17367c478bd9Sstevel@tonic-gate next = ulwp->ul_forw; 17377c478bd9Sstevel@tonic-gate ulwp->ul_forw = ulwp->ul_back = NULL; 17387c478bd9Sstevel@tonic-gate mark_dead_and_buried(ulwp); 17397c478bd9Sstevel@tonic-gate udp->nzombies--; 17407c478bd9Sstevel@tonic-gate if (ulwp->ul_replace) { 17417c478bd9Sstevel@tonic-gate ulwp->ul_next = NULL; 17427c478bd9Sstevel@tonic-gate if (udp->ulwp_replace_free == NULL) { 17437c478bd9Sstevel@tonic-gate udp->ulwp_replace_free = 17447c478bd9Sstevel@tonic-gate udp->ulwp_replace_last = ulwp; 17457c478bd9Sstevel@tonic-gate } else { 17467c478bd9Sstevel@tonic-gate udp->ulwp_replace_last->ul_next = ulwp; 17477c478bd9Sstevel@tonic-gate udp->ulwp_replace_last = ulwp; 17487c478bd9Sstevel@tonic-gate } 17497c478bd9Sstevel@tonic-gate } 17507c478bd9Sstevel@tonic-gate } while ((ulwp = next) != udp->all_zombies); 17517c478bd9Sstevel@tonic-gate ASSERT(udp->nzombies == 0); 17527c478bd9Sstevel@tonic-gate udp->all_zombies = NULL; 17537c478bd9Sstevel@tonic-gate udp->nzombies = 0; 17547c478bd9Sstevel@tonic-gate } 17557c478bd9Sstevel@tonic-gate trim_stack_cache(0); 17567c478bd9Sstevel@tonic-gate } 17577c478bd9Sstevel@tonic-gate 17587c478bd9Sstevel@tonic-gate lwpid_t 17597c478bd9Sstevel@tonic-gate lwp_self(void) 17607c478bd9Sstevel@tonic-gate { 17617c478bd9Sstevel@tonic-gate return (curthread->ul_lwpid); 17627c478bd9Sstevel@tonic-gate } 17637c478bd9Sstevel@tonic-gate 17647257d1b4Sraf #pragma weak _ti_thr_self = thr_self 17657257d1b4Sraf #pragma weak pthread_self = thr_self 17667c478bd9Sstevel@tonic-gate thread_t 17677257d1b4Sraf thr_self() 17687c478bd9Sstevel@tonic-gate { 17697c478bd9Sstevel@tonic-gate return (curthread->ul_lwpid); 17707c478bd9Sstevel@tonic-gate } 17717c478bd9Sstevel@tonic-gate 17727c478bd9Sstevel@tonic-gate int 17737257d1b4Sraf thr_main() 17747c478bd9Sstevel@tonic-gate { 17757c478bd9Sstevel@tonic-gate ulwp_t *self = __curthread(); 17767c478bd9Sstevel@tonic-gate 17777c478bd9Sstevel@tonic-gate return ((self == NULL)? -1 : self->ul_main); 17787c478bd9Sstevel@tonic-gate } 17797c478bd9Sstevel@tonic-gate 17807c478bd9Sstevel@tonic-gate int 1781657b1f3dSraf _thrp_cancelled(void) 1782657b1f3dSraf { 1783657b1f3dSraf return (curthread->ul_rval == PTHREAD_CANCELED); 1784657b1f3dSraf } 1785657b1f3dSraf 1786657b1f3dSraf int 17877c478bd9Sstevel@tonic-gate _thrp_stksegment(ulwp_t *ulwp, stack_t *stk) 17887c478bd9Sstevel@tonic-gate { 17897c478bd9Sstevel@tonic-gate stk->ss_sp = (void *)ulwp->ul_stktop; 17907c478bd9Sstevel@tonic-gate stk->ss_size = ulwp->ul_stksiz; 17917c478bd9Sstevel@tonic-gate stk->ss_flags = 0; 17927c478bd9Sstevel@tonic-gate return (0); 17937c478bd9Sstevel@tonic-gate } 17947c478bd9Sstevel@tonic-gate 17957257d1b4Sraf #pragma weak _thr_stksegment = thr_stksegment 17967c478bd9Sstevel@tonic-gate int 17977257d1b4Sraf thr_stksegment(stack_t *stk) 17987c478bd9Sstevel@tonic-gate { 17997c478bd9Sstevel@tonic-gate return (_thrp_stksegment(curthread, stk)); 18007c478bd9Sstevel@tonic-gate } 18017c478bd9Sstevel@tonic-gate 18027c478bd9Sstevel@tonic-gate void 18037c478bd9Sstevel@tonic-gate force_continue(ulwp_t *ulwp) 18047c478bd9Sstevel@tonic-gate { 18057c478bd9Sstevel@tonic-gate #if defined(THREAD_DEBUG) 18067c478bd9Sstevel@tonic-gate ulwp_t *self = curthread; 18077c478bd9Sstevel@tonic-gate uberdata_t *udp = self->ul_uberdata; 18087c478bd9Sstevel@tonic-gate #endif 18097c478bd9Sstevel@tonic-gate int error; 18107c478bd9Sstevel@tonic-gate timespec_t ts; 18117c478bd9Sstevel@tonic-gate 181236319254Sraf ASSERT(MUTEX_OWNED(&udp->fork_lock, self)); 18137c478bd9Sstevel@tonic-gate ASSERT(MUTEX_OWNED(ulwp_mutex(ulwp, udp), self)); 18147c478bd9Sstevel@tonic-gate 18157c478bd9Sstevel@tonic-gate for (;;) { 18167257d1b4Sraf error = _lwp_continue(ulwp->ul_lwpid); 18177c478bd9Sstevel@tonic-gate if (error != 0 && error != EINTR) 18187c478bd9Sstevel@tonic-gate break; 18197c478bd9Sstevel@tonic-gate error = 0; 18207c478bd9Sstevel@tonic-gate if (ulwp->ul_stopping) { /* he is stopping himself */ 18217c478bd9Sstevel@tonic-gate ts.tv_sec = 0; /* give him a chance to run */ 18227c478bd9Sstevel@tonic-gate ts.tv_nsec = 100000; /* 100 usecs or clock tick */ 1823f841f6adSraf (void) __nanosleep(&ts, NULL); 18247c478bd9Sstevel@tonic-gate } 18257c478bd9Sstevel@tonic-gate if (!ulwp->ul_stopping) /* he is running now */ 18267c478bd9Sstevel@tonic-gate break; /* so we are done */ 18277c478bd9Sstevel@tonic-gate /* 18287c478bd9Sstevel@tonic-gate * He is marked as being in the process of stopping 18297c478bd9Sstevel@tonic-gate * himself. Loop around and continue him again. 18307c478bd9Sstevel@tonic-gate * He may not have been stopped the first time. 18317c478bd9Sstevel@tonic-gate */ 18327c478bd9Sstevel@tonic-gate } 18337c478bd9Sstevel@tonic-gate } 18347c478bd9Sstevel@tonic-gate 18357c478bd9Sstevel@tonic-gate /* 1836e54ab87fSRoger A. Faulkner * Suspend an lwp with lwp_suspend(), then move it to a safe point, 1837e54ab87fSRoger A. Faulkner * that is, to a point where ul_critical and ul_rtld are both zero. 18387c478bd9Sstevel@tonic-gate * On return, the ulwp_lock() is dropped as with ulwp_unlock(). 18397c478bd9Sstevel@tonic-gate * If 'link_dropped' is non-NULL, then 'link_lock' is held on entry. 18407c478bd9Sstevel@tonic-gate * If we have to drop link_lock, we store 1 through link_dropped. 18417c478bd9Sstevel@tonic-gate * If the lwp exits before it can be suspended, we return ESRCH. 18427c478bd9Sstevel@tonic-gate */ 18437c478bd9Sstevel@tonic-gate int 18447c478bd9Sstevel@tonic-gate safe_suspend(ulwp_t *ulwp, uchar_t whystopped, int *link_dropped) 18457c478bd9Sstevel@tonic-gate { 18467c478bd9Sstevel@tonic-gate ulwp_t *self = curthread; 18477c478bd9Sstevel@tonic-gate uberdata_t *udp = self->ul_uberdata; 18487c478bd9Sstevel@tonic-gate cond_t *cvp = ulwp_condvar(ulwp, udp); 18497c478bd9Sstevel@tonic-gate mutex_t *mp = ulwp_mutex(ulwp, udp); 18507c478bd9Sstevel@tonic-gate thread_t tid = ulwp->ul_lwpid; 18517c478bd9Sstevel@tonic-gate int ix = ulwp->ul_ix; 18527c478bd9Sstevel@tonic-gate int error = 0; 18537c478bd9Sstevel@tonic-gate 18547c478bd9Sstevel@tonic-gate ASSERT(whystopped == TSTP_REGULAR || 18557c478bd9Sstevel@tonic-gate whystopped == TSTP_MUTATOR || 18567c478bd9Sstevel@tonic-gate whystopped == TSTP_FORK); 18577c478bd9Sstevel@tonic-gate ASSERT(ulwp != self); 18587c478bd9Sstevel@tonic-gate ASSERT(!ulwp->ul_stop); 185936319254Sraf ASSERT(MUTEX_OWNED(&udp->fork_lock, self)); 18607c478bd9Sstevel@tonic-gate ASSERT(MUTEX_OWNED(mp, self)); 18617c478bd9Sstevel@tonic-gate 18627c478bd9Sstevel@tonic-gate if (link_dropped != NULL) 18637c478bd9Sstevel@tonic-gate *link_dropped = 0; 18647c478bd9Sstevel@tonic-gate 18657c478bd9Sstevel@tonic-gate /* 18667c478bd9Sstevel@tonic-gate * We must grab the target's spin lock before suspending it. 18677c478bd9Sstevel@tonic-gate * See the comments below and in _thrp_suspend() for why. 18687c478bd9Sstevel@tonic-gate */ 18697c478bd9Sstevel@tonic-gate spin_lock_set(&ulwp->ul_spinlock); 18707c478bd9Sstevel@tonic-gate (void) ___lwp_suspend(tid); 18717c478bd9Sstevel@tonic-gate spin_lock_clear(&ulwp->ul_spinlock); 18727c478bd9Sstevel@tonic-gate 18737c478bd9Sstevel@tonic-gate top: 1874e54ab87fSRoger A. Faulkner if ((ulwp->ul_critical == 0 && ulwp->ul_rtld == 0) || 1875e54ab87fSRoger A. Faulkner ulwp->ul_stopping) { 18767c478bd9Sstevel@tonic-gate /* thread is already safe */ 18777c478bd9Sstevel@tonic-gate ulwp->ul_stop |= whystopped; 18787c478bd9Sstevel@tonic-gate } else { 18797c478bd9Sstevel@tonic-gate /* 18807c478bd9Sstevel@tonic-gate * Setting ul_pleasestop causes the target thread to stop 18817c478bd9Sstevel@tonic-gate * itself in _thrp_suspend(), below, after we drop its lock. 18827c478bd9Sstevel@tonic-gate * We must continue the critical thread before dropping 18837c478bd9Sstevel@tonic-gate * link_lock because the critical thread may be holding 18847c478bd9Sstevel@tonic-gate * the queue lock for link_lock. This is delicate. 18857c478bd9Sstevel@tonic-gate */ 18867c478bd9Sstevel@tonic-gate ulwp->ul_pleasestop |= whystopped; 18877c478bd9Sstevel@tonic-gate force_continue(ulwp); 18887c478bd9Sstevel@tonic-gate if (link_dropped != NULL) { 18897c478bd9Sstevel@tonic-gate *link_dropped = 1; 18907c478bd9Sstevel@tonic-gate lmutex_unlock(&udp->link_lock); 18917c478bd9Sstevel@tonic-gate /* be sure to drop link_lock only once */ 18927c478bd9Sstevel@tonic-gate link_dropped = NULL; 18937c478bd9Sstevel@tonic-gate } 18947c478bd9Sstevel@tonic-gate 18957c478bd9Sstevel@tonic-gate /* 18967c478bd9Sstevel@tonic-gate * The thread may disappear by calling thr_exit() so we 18977c478bd9Sstevel@tonic-gate * cannot rely on the ulwp pointer after dropping the lock. 18987c478bd9Sstevel@tonic-gate * Instead, we search the hash table to find it again. 18997c478bd9Sstevel@tonic-gate * When we return, we may find that the thread has been 19007c478bd9Sstevel@tonic-gate * continued by some other thread. The suspend/continue 19017c478bd9Sstevel@tonic-gate * interfaces are prone to such race conditions by design. 19027c478bd9Sstevel@tonic-gate */ 19037c478bd9Sstevel@tonic-gate while (ulwp && !ulwp->ul_dead && !ulwp->ul_stop && 19047c478bd9Sstevel@tonic-gate (ulwp->ul_pleasestop & whystopped)) { 1905a574db85Sraf (void) __cond_wait(cvp, mp); 19067c478bd9Sstevel@tonic-gate for (ulwp = udp->thr_hash_table[ix].hash_bucket; 19077c478bd9Sstevel@tonic-gate ulwp != NULL; ulwp = ulwp->ul_hash) { 19087c478bd9Sstevel@tonic-gate if (ulwp->ul_lwpid == tid) 19097c478bd9Sstevel@tonic-gate break; 19107c478bd9Sstevel@tonic-gate } 19117c478bd9Sstevel@tonic-gate } 19127c478bd9Sstevel@tonic-gate 19137c478bd9Sstevel@tonic-gate if (ulwp == NULL || ulwp->ul_dead) 19147c478bd9Sstevel@tonic-gate error = ESRCH; 19157c478bd9Sstevel@tonic-gate else { 19167c478bd9Sstevel@tonic-gate /* 19177c478bd9Sstevel@tonic-gate * Do another lwp_suspend() to make sure we don't 19187c478bd9Sstevel@tonic-gate * return until the target thread is fully stopped 19197c478bd9Sstevel@tonic-gate * in the kernel. Don't apply lwp_suspend() until 19207c478bd9Sstevel@tonic-gate * we know that the target is not holding any 19217c478bd9Sstevel@tonic-gate * queue locks, that is, that it has completed 19227c478bd9Sstevel@tonic-gate * ulwp_unlock(self) and has, or at least is 19237c478bd9Sstevel@tonic-gate * about to, call lwp_suspend() on itself. We do 19247c478bd9Sstevel@tonic-gate * this by grabbing the target's spin lock. 19257c478bd9Sstevel@tonic-gate */ 19267c478bd9Sstevel@tonic-gate ASSERT(ulwp->ul_lwpid == tid); 19277c478bd9Sstevel@tonic-gate spin_lock_set(&ulwp->ul_spinlock); 19287c478bd9Sstevel@tonic-gate (void) ___lwp_suspend(tid); 19297c478bd9Sstevel@tonic-gate spin_lock_clear(&ulwp->ul_spinlock); 19307c478bd9Sstevel@tonic-gate /* 19317c478bd9Sstevel@tonic-gate * If some other thread did a thr_continue() 19327c478bd9Sstevel@tonic-gate * on the target thread we have to start over. 19337c478bd9Sstevel@tonic-gate */ 19347c478bd9Sstevel@tonic-gate if (!ulwp->ul_stopping || !(ulwp->ul_stop & whystopped)) 19357c478bd9Sstevel@tonic-gate goto top; 19367c478bd9Sstevel@tonic-gate } 19377c478bd9Sstevel@tonic-gate } 19387c478bd9Sstevel@tonic-gate 19397257d1b4Sraf (void) cond_broadcast(cvp); 19407c478bd9Sstevel@tonic-gate lmutex_unlock(mp); 19417c478bd9Sstevel@tonic-gate return (error); 19427c478bd9Sstevel@tonic-gate } 19437c478bd9Sstevel@tonic-gate 19447c478bd9Sstevel@tonic-gate int 19457c478bd9Sstevel@tonic-gate _thrp_suspend(thread_t tid, uchar_t whystopped) 19467c478bd9Sstevel@tonic-gate { 19477c478bd9Sstevel@tonic-gate ulwp_t *self = curthread; 19487c478bd9Sstevel@tonic-gate uberdata_t *udp = self->ul_uberdata; 19497c478bd9Sstevel@tonic-gate ulwp_t *ulwp; 19507c478bd9Sstevel@tonic-gate int error = 0; 19517c478bd9Sstevel@tonic-gate 19527c478bd9Sstevel@tonic-gate ASSERT((whystopped & (TSTP_REGULAR|TSTP_MUTATOR|TSTP_FORK)) != 0); 19537c478bd9Sstevel@tonic-gate ASSERT((whystopped & ~(TSTP_REGULAR|TSTP_MUTATOR|TSTP_FORK)) == 0); 19547c478bd9Sstevel@tonic-gate 19557c478bd9Sstevel@tonic-gate /* 19567f6e74f8Sraf * We can't suspend anyone except ourself while 19577f6e74f8Sraf * some other thread is performing a fork. 19587f6e74f8Sraf * This also allows only one suspension at a time. 19597c478bd9Sstevel@tonic-gate */ 19607f6e74f8Sraf if (tid != self->ul_lwpid) 19612e145884Sraf fork_lock_enter(); 19627c478bd9Sstevel@tonic-gate 19637c478bd9Sstevel@tonic-gate if ((ulwp = find_lwp(tid)) == NULL) 19647c478bd9Sstevel@tonic-gate error = ESRCH; 19657c478bd9Sstevel@tonic-gate else if (whystopped == TSTP_MUTATOR && !ulwp->ul_mutator) { 19667c478bd9Sstevel@tonic-gate ulwp_unlock(ulwp, udp); 19677c478bd9Sstevel@tonic-gate error = EINVAL; 19687c478bd9Sstevel@tonic-gate } else if (ulwp->ul_stop) { /* already stopped */ 19697c478bd9Sstevel@tonic-gate ulwp->ul_stop |= whystopped; 19707c478bd9Sstevel@tonic-gate ulwp_broadcast(ulwp); 19717c478bd9Sstevel@tonic-gate ulwp_unlock(ulwp, udp); 19727c478bd9Sstevel@tonic-gate } else if (ulwp != self) { 19737c478bd9Sstevel@tonic-gate /* 19747c478bd9Sstevel@tonic-gate * After suspending the other thread, move it out of a 19757c478bd9Sstevel@tonic-gate * critical section and deal with the schedctl mappings. 19767c478bd9Sstevel@tonic-gate * safe_suspend() suspends the other thread, calls 19777c478bd9Sstevel@tonic-gate * ulwp_broadcast(ulwp) and drops the ulwp lock. 19787c478bd9Sstevel@tonic-gate */ 19797c478bd9Sstevel@tonic-gate error = safe_suspend(ulwp, whystopped, NULL); 19807c478bd9Sstevel@tonic-gate } else { 19817c478bd9Sstevel@tonic-gate int schedctl_after_fork = 0; 19827c478bd9Sstevel@tonic-gate 19837c478bd9Sstevel@tonic-gate /* 19847c478bd9Sstevel@tonic-gate * We are suspending ourself. We must not take a signal 19857c478bd9Sstevel@tonic-gate * until we return from lwp_suspend() and clear ul_stopping. 19867c478bd9Sstevel@tonic-gate * This is to guard against siglongjmp(). 19877c478bd9Sstevel@tonic-gate */ 19887c478bd9Sstevel@tonic-gate enter_critical(self); 19897c478bd9Sstevel@tonic-gate self->ul_sp = stkptr(); 19907c478bd9Sstevel@tonic-gate _flush_windows(); /* sparc */ 19917c478bd9Sstevel@tonic-gate self->ul_pleasestop = 0; 19927c478bd9Sstevel@tonic-gate self->ul_stop |= whystopped; 19937c478bd9Sstevel@tonic-gate /* 19947c478bd9Sstevel@tonic-gate * Grab our spin lock before dropping ulwp_mutex(self). 19957c478bd9Sstevel@tonic-gate * This prevents the suspending thread from applying 19967c478bd9Sstevel@tonic-gate * lwp_suspend() to us before we emerge from 19977c478bd9Sstevel@tonic-gate * lmutex_unlock(mp) and have dropped mp's queue lock. 19987c478bd9Sstevel@tonic-gate */ 19997c478bd9Sstevel@tonic-gate spin_lock_set(&self->ul_spinlock); 20007c478bd9Sstevel@tonic-gate self->ul_stopping = 1; 20017c478bd9Sstevel@tonic-gate ulwp_broadcast(self); 20027c478bd9Sstevel@tonic-gate ulwp_unlock(self, udp); 20037c478bd9Sstevel@tonic-gate /* 20047c478bd9Sstevel@tonic-gate * From this point until we return from lwp_suspend(), 20057c478bd9Sstevel@tonic-gate * we must not call any function that might invoke the 20067c478bd9Sstevel@tonic-gate * dynamic linker, that is, we can only call functions 20077c478bd9Sstevel@tonic-gate * private to the library. 20087c478bd9Sstevel@tonic-gate * 20097c478bd9Sstevel@tonic-gate * Also, this is a nasty race condition for a process 20107c478bd9Sstevel@tonic-gate * that is undergoing a forkall() operation: 20117c478bd9Sstevel@tonic-gate * Once we clear our spinlock (below), we are vulnerable 20127c478bd9Sstevel@tonic-gate * to being suspended by the forkall() thread before 20137c478bd9Sstevel@tonic-gate * we manage to suspend ourself in ___lwp_suspend(). 20147c478bd9Sstevel@tonic-gate * See safe_suspend() and force_continue(). 20157c478bd9Sstevel@tonic-gate * 20167c478bd9Sstevel@tonic-gate * To avoid a SIGSEGV due to the disappearance 20177c478bd9Sstevel@tonic-gate * of the schedctl mappings in the child process, 20187c478bd9Sstevel@tonic-gate * which can happen in spin_lock_clear() if we 20197c478bd9Sstevel@tonic-gate * are suspended while we are in the middle of 20207c478bd9Sstevel@tonic-gate * its call to preempt(), we preemptively clear 20217c478bd9Sstevel@tonic-gate * our own schedctl pointer before dropping our 20227c478bd9Sstevel@tonic-gate * spinlock. We reinstate it, in both the parent 20237c478bd9Sstevel@tonic-gate * and (if this really is a forkall()) the child. 20247c478bd9Sstevel@tonic-gate */ 20257c478bd9Sstevel@tonic-gate if (whystopped & TSTP_FORK) { 20267c478bd9Sstevel@tonic-gate schedctl_after_fork = 1; 20277c478bd9Sstevel@tonic-gate self->ul_schedctl = NULL; 20287c478bd9Sstevel@tonic-gate self->ul_schedctl_called = &udp->uberflags; 20297c478bd9Sstevel@tonic-gate } 20307c478bd9Sstevel@tonic-gate spin_lock_clear(&self->ul_spinlock); 20317c478bd9Sstevel@tonic-gate (void) ___lwp_suspend(tid); 20327c478bd9Sstevel@tonic-gate /* 20337c478bd9Sstevel@tonic-gate * Somebody else continued us. 20347c478bd9Sstevel@tonic-gate * We can't grab ulwp_lock(self) 20357c478bd9Sstevel@tonic-gate * until after clearing ul_stopping. 20367c478bd9Sstevel@tonic-gate * force_continue() relies on this. 20377c478bd9Sstevel@tonic-gate */ 20387c478bd9Sstevel@tonic-gate self->ul_stopping = 0; 20397c478bd9Sstevel@tonic-gate self->ul_sp = 0; 20407c478bd9Sstevel@tonic-gate if (schedctl_after_fork) { 20417c478bd9Sstevel@tonic-gate self->ul_schedctl_called = NULL; 20427c478bd9Sstevel@tonic-gate self->ul_schedctl = NULL; 20437c478bd9Sstevel@tonic-gate (void) setup_schedctl(); 20447c478bd9Sstevel@tonic-gate } 20457c478bd9Sstevel@tonic-gate ulwp_lock(self, udp); 20467c478bd9Sstevel@tonic-gate ulwp_broadcast(self); 20477c478bd9Sstevel@tonic-gate ulwp_unlock(self, udp); 20487c478bd9Sstevel@tonic-gate exit_critical(self); 20497c478bd9Sstevel@tonic-gate } 20507c478bd9Sstevel@tonic-gate 20517c478bd9Sstevel@tonic-gate if (tid != self->ul_lwpid) 20527c478bd9Sstevel@tonic-gate fork_lock_exit(); 20537c478bd9Sstevel@tonic-gate 20547c478bd9Sstevel@tonic-gate return (error); 20557c478bd9Sstevel@tonic-gate } 20567c478bd9Sstevel@tonic-gate 20577c478bd9Sstevel@tonic-gate /* 20587c478bd9Sstevel@tonic-gate * Suspend all lwps other than ourself in preparation for fork. 20597c478bd9Sstevel@tonic-gate */ 20607c478bd9Sstevel@tonic-gate void 20617c478bd9Sstevel@tonic-gate suspend_fork() 20627c478bd9Sstevel@tonic-gate { 20637c478bd9Sstevel@tonic-gate ulwp_t *self = curthread; 20647c478bd9Sstevel@tonic-gate uberdata_t *udp = self->ul_uberdata; 20657c478bd9Sstevel@tonic-gate ulwp_t *ulwp; 20667c478bd9Sstevel@tonic-gate int link_dropped; 20677c478bd9Sstevel@tonic-gate 206836319254Sraf ASSERT(MUTEX_OWNED(&udp->fork_lock, self)); 20697c478bd9Sstevel@tonic-gate top: 20707c478bd9Sstevel@tonic-gate lmutex_lock(&udp->link_lock); 20717c478bd9Sstevel@tonic-gate 20727c478bd9Sstevel@tonic-gate for (ulwp = self->ul_forw; ulwp != self; ulwp = ulwp->ul_forw) { 20737c478bd9Sstevel@tonic-gate ulwp_lock(ulwp, udp); 20747c478bd9Sstevel@tonic-gate if (ulwp->ul_stop) { /* already stopped */ 20757c478bd9Sstevel@tonic-gate ulwp->ul_stop |= TSTP_FORK; 20767c478bd9Sstevel@tonic-gate ulwp_broadcast(ulwp); 20777c478bd9Sstevel@tonic-gate ulwp_unlock(ulwp, udp); 20787c478bd9Sstevel@tonic-gate } else { 20797c478bd9Sstevel@tonic-gate /* 20807c478bd9Sstevel@tonic-gate * Move the stopped lwp out of a critical section. 20817c478bd9Sstevel@tonic-gate */ 20827c478bd9Sstevel@tonic-gate if (safe_suspend(ulwp, TSTP_FORK, &link_dropped) || 20837c478bd9Sstevel@tonic-gate link_dropped) 20847c478bd9Sstevel@tonic-gate goto top; 20857c478bd9Sstevel@tonic-gate } 20867c478bd9Sstevel@tonic-gate } 20877c478bd9Sstevel@tonic-gate 20887c478bd9Sstevel@tonic-gate lmutex_unlock(&udp->link_lock); 20897c478bd9Sstevel@tonic-gate } 20907c478bd9Sstevel@tonic-gate 20917c478bd9Sstevel@tonic-gate void 20927c478bd9Sstevel@tonic-gate continue_fork(int child) 20937c478bd9Sstevel@tonic-gate { 20947c478bd9Sstevel@tonic-gate ulwp_t *self = curthread; 20957c478bd9Sstevel@tonic-gate uberdata_t *udp = self->ul_uberdata; 20967c478bd9Sstevel@tonic-gate ulwp_t *ulwp; 20977c478bd9Sstevel@tonic-gate 209836319254Sraf ASSERT(MUTEX_OWNED(&udp->fork_lock, self)); 209936319254Sraf 21007c478bd9Sstevel@tonic-gate /* 21017c478bd9Sstevel@tonic-gate * Clear the schedctl pointers in the child of forkall(). 21027c478bd9Sstevel@tonic-gate */ 21037c478bd9Sstevel@tonic-gate if (child) { 21047c478bd9Sstevel@tonic-gate for (ulwp = self->ul_forw; ulwp != self; ulwp = ulwp->ul_forw) { 21057c478bd9Sstevel@tonic-gate ulwp->ul_schedctl_called = 21067c478bd9Sstevel@tonic-gate ulwp->ul_dead? &udp->uberflags : NULL; 21077c478bd9Sstevel@tonic-gate ulwp->ul_schedctl = NULL; 21087c478bd9Sstevel@tonic-gate } 21097c478bd9Sstevel@tonic-gate } 21107c478bd9Sstevel@tonic-gate 21117c478bd9Sstevel@tonic-gate /* 21127c478bd9Sstevel@tonic-gate * Set all lwps that were stopped for fork() running again. 21137c478bd9Sstevel@tonic-gate */ 21147c478bd9Sstevel@tonic-gate lmutex_lock(&udp->link_lock); 21157c478bd9Sstevel@tonic-gate for (ulwp = self->ul_forw; ulwp != self; ulwp = ulwp->ul_forw) { 21167c478bd9Sstevel@tonic-gate mutex_t *mp = ulwp_mutex(ulwp, udp); 21177c478bd9Sstevel@tonic-gate lmutex_lock(mp); 21187c478bd9Sstevel@tonic-gate ASSERT(ulwp->ul_stop & TSTP_FORK); 21197c478bd9Sstevel@tonic-gate ulwp->ul_stop &= ~TSTP_FORK; 21207c478bd9Sstevel@tonic-gate ulwp_broadcast(ulwp); 21217c478bd9Sstevel@tonic-gate if (!ulwp->ul_stop) 21227c478bd9Sstevel@tonic-gate force_continue(ulwp); 21237c478bd9Sstevel@tonic-gate lmutex_unlock(mp); 21247c478bd9Sstevel@tonic-gate } 21257c478bd9Sstevel@tonic-gate lmutex_unlock(&udp->link_lock); 21267c478bd9Sstevel@tonic-gate } 21277c478bd9Sstevel@tonic-gate 21287c478bd9Sstevel@tonic-gate int 21297c478bd9Sstevel@tonic-gate _thrp_continue(thread_t tid, uchar_t whystopped) 21307c478bd9Sstevel@tonic-gate { 21317c478bd9Sstevel@tonic-gate uberdata_t *udp = curthread->ul_uberdata; 21327c478bd9Sstevel@tonic-gate ulwp_t *ulwp; 21337c478bd9Sstevel@tonic-gate mutex_t *mp; 21347c478bd9Sstevel@tonic-gate int error = 0; 21357c478bd9Sstevel@tonic-gate 21367c478bd9Sstevel@tonic-gate ASSERT(whystopped == TSTP_REGULAR || 21377c478bd9Sstevel@tonic-gate whystopped == TSTP_MUTATOR); 21387c478bd9Sstevel@tonic-gate 213936319254Sraf /* 214036319254Sraf * We single-thread the entire thread suspend/continue mechanism. 214136319254Sraf */ 21422e145884Sraf fork_lock_enter(); 214336319254Sraf 214436319254Sraf if ((ulwp = find_lwp(tid)) == NULL) { 214536319254Sraf fork_lock_exit(); 21467c478bd9Sstevel@tonic-gate return (ESRCH); 214736319254Sraf } 21487c478bd9Sstevel@tonic-gate 21497c478bd9Sstevel@tonic-gate mp = ulwp_mutex(ulwp, udp); 21507c478bd9Sstevel@tonic-gate if ((whystopped == TSTP_MUTATOR && !ulwp->ul_mutator)) { 21517c478bd9Sstevel@tonic-gate error = EINVAL; 21527c478bd9Sstevel@tonic-gate } else if (ulwp->ul_stop & whystopped) { 21537c478bd9Sstevel@tonic-gate ulwp->ul_stop &= ~whystopped; 21547c478bd9Sstevel@tonic-gate ulwp_broadcast(ulwp); 21557c478bd9Sstevel@tonic-gate if (!ulwp->ul_stop) { 21567c478bd9Sstevel@tonic-gate if (whystopped == TSTP_REGULAR && ulwp->ul_created) { 21577c478bd9Sstevel@tonic-gate ulwp->ul_sp = 0; 21587c478bd9Sstevel@tonic-gate ulwp->ul_created = 0; 21597c478bd9Sstevel@tonic-gate } 21607c478bd9Sstevel@tonic-gate force_continue(ulwp); 21617c478bd9Sstevel@tonic-gate } 21627c478bd9Sstevel@tonic-gate } 21637c478bd9Sstevel@tonic-gate lmutex_unlock(mp); 216436319254Sraf 216536319254Sraf fork_lock_exit(); 21667c478bd9Sstevel@tonic-gate return (error); 21677c478bd9Sstevel@tonic-gate } 21687c478bd9Sstevel@tonic-gate 21697c478bd9Sstevel@tonic-gate int 21707257d1b4Sraf thr_suspend(thread_t tid) 21717c478bd9Sstevel@tonic-gate { 21727c478bd9Sstevel@tonic-gate return (_thrp_suspend(tid, TSTP_REGULAR)); 21737c478bd9Sstevel@tonic-gate } 21747c478bd9Sstevel@tonic-gate 21757c478bd9Sstevel@tonic-gate int 21767257d1b4Sraf thr_continue(thread_t tid) 21777c478bd9Sstevel@tonic-gate { 21787c478bd9Sstevel@tonic-gate return (_thrp_continue(tid, TSTP_REGULAR)); 21797c478bd9Sstevel@tonic-gate } 21807c478bd9Sstevel@tonic-gate 21817c478bd9Sstevel@tonic-gate void 21827257d1b4Sraf thr_yield() 21837c478bd9Sstevel@tonic-gate { 21848cd45542Sraf yield(); 21857c478bd9Sstevel@tonic-gate } 21867c478bd9Sstevel@tonic-gate 21877257d1b4Sraf #pragma weak pthread_kill = thr_kill 21887257d1b4Sraf #pragma weak _thr_kill = thr_kill 21897c478bd9Sstevel@tonic-gate int 21907257d1b4Sraf thr_kill(thread_t tid, int sig) 21917c478bd9Sstevel@tonic-gate { 21927c478bd9Sstevel@tonic-gate if (sig == SIGCANCEL) 21937c478bd9Sstevel@tonic-gate return (EINVAL); 21947257d1b4Sraf return (_lwp_kill(tid, sig)); 21957c478bd9Sstevel@tonic-gate } 21967c478bd9Sstevel@tonic-gate 21977c478bd9Sstevel@tonic-gate /* 21987c478bd9Sstevel@tonic-gate * Exit a critical section, take deferred actions if necessary. 2199e54ab87fSRoger A. Faulkner * Called from exit_critical() and from sigon(). 22007c478bd9Sstevel@tonic-gate */ 22017c478bd9Sstevel@tonic-gate void 22027c478bd9Sstevel@tonic-gate do_exit_critical() 22037c478bd9Sstevel@tonic-gate { 22047c478bd9Sstevel@tonic-gate ulwp_t *self = curthread; 22057c478bd9Sstevel@tonic-gate int sig; 22067c478bd9Sstevel@tonic-gate 22077c478bd9Sstevel@tonic-gate ASSERT(self->ul_critical == 0); 2208e54ab87fSRoger A. Faulkner 2209e54ab87fSRoger A. Faulkner /* 2210e54ab87fSRoger A. Faulkner * Don't suspend ourself or take a deferred signal while dying 2211e54ab87fSRoger A. Faulkner * or while executing inside the dynamic linker (ld.so.1). 2212e54ab87fSRoger A. Faulkner */ 2213e54ab87fSRoger A. Faulkner if (self->ul_dead || self->ul_rtld) 22147c478bd9Sstevel@tonic-gate return; 22157c478bd9Sstevel@tonic-gate 22167c478bd9Sstevel@tonic-gate while (self->ul_pleasestop || 22177c478bd9Sstevel@tonic-gate (self->ul_cursig != 0 && self->ul_sigdefer == 0)) { 22187c478bd9Sstevel@tonic-gate /* 22197c478bd9Sstevel@tonic-gate * Avoid a recursive call to exit_critical() in _thrp_suspend() 22207c478bd9Sstevel@tonic-gate * by keeping self->ul_critical == 1 here. 22217c478bd9Sstevel@tonic-gate */ 22227c478bd9Sstevel@tonic-gate self->ul_critical++; 22237c478bd9Sstevel@tonic-gate while (self->ul_pleasestop) { 22247c478bd9Sstevel@tonic-gate /* 22257c478bd9Sstevel@tonic-gate * Guard against suspending ourself while on a sleep 22267c478bd9Sstevel@tonic-gate * queue. See the comments in call_user_handler(). 22277c478bd9Sstevel@tonic-gate */ 22287c478bd9Sstevel@tonic-gate unsleep_self(); 22297c478bd9Sstevel@tonic-gate set_parking_flag(self, 0); 22307c478bd9Sstevel@tonic-gate (void) _thrp_suspend(self->ul_lwpid, 22317c478bd9Sstevel@tonic-gate self->ul_pleasestop); 22327c478bd9Sstevel@tonic-gate } 22337c478bd9Sstevel@tonic-gate self->ul_critical--; 22347c478bd9Sstevel@tonic-gate 22357c478bd9Sstevel@tonic-gate if ((sig = self->ul_cursig) != 0 && self->ul_sigdefer == 0) { 22367c478bd9Sstevel@tonic-gate /* 22377c478bd9Sstevel@tonic-gate * Clear ul_cursig before proceeding. 22387c478bd9Sstevel@tonic-gate * This protects us from the dynamic linker's 22397c478bd9Sstevel@tonic-gate * calls to bind_guard()/bind_clear() in the 22407c478bd9Sstevel@tonic-gate * event that it is invoked to resolve a symbol 22417c478bd9Sstevel@tonic-gate * like take_deferred_signal() below. 22427c478bd9Sstevel@tonic-gate */ 22437c478bd9Sstevel@tonic-gate self->ul_cursig = 0; 22447c478bd9Sstevel@tonic-gate take_deferred_signal(sig); 22457c478bd9Sstevel@tonic-gate ASSERT(self->ul_cursig == 0); 22467c478bd9Sstevel@tonic-gate } 22477c478bd9Sstevel@tonic-gate } 22487c478bd9Sstevel@tonic-gate ASSERT(self->ul_critical == 0); 22497c478bd9Sstevel@tonic-gate } 22507c478bd9Sstevel@tonic-gate 2251a574db85Sraf /* 2252a574db85Sraf * _ti_bind_guard() and _ti_bind_clear() are called by the dynamic linker 2253a574db85Sraf * (ld.so.1) when it has do do something, like resolve a symbol to be called 2254a574db85Sraf * by the application or one of its libraries. _ti_bind_guard() is called 2255a574db85Sraf * on entry to ld.so.1, _ti_bind_clear() on exit from ld.so.1 back to the 2256a574db85Sraf * application. The dynamic linker gets special dispensation from libc to 2257a574db85Sraf * run in a critical region (all signals deferred and no thread suspension 2258a574db85Sraf * or forking allowed), and to be immune from cancellation for the duration. 2259a574db85Sraf */ 22607c478bd9Sstevel@tonic-gate int 22618cd45542Sraf _ti_bind_guard(int flags) 22627c478bd9Sstevel@tonic-gate { 22637c478bd9Sstevel@tonic-gate ulwp_t *self = curthread; 22648cd45542Sraf uberdata_t *udp = self->ul_uberdata; 22658cd45542Sraf int bindflag = (flags & THR_FLG_RTLD); 22667c478bd9Sstevel@tonic-gate 22677c478bd9Sstevel@tonic-gate if ((self->ul_bindflags & bindflag) == bindflag) 22687c478bd9Sstevel@tonic-gate return (0); 22692a8d6ebaSRod Evans self->ul_bindflags |= bindflag; 22708cd45542Sraf if ((flags & (THR_FLG_NOLOCK | THR_FLG_REENTER)) == THR_FLG_NOLOCK) { 22718cd45542Sraf sigoff(self); /* see no signals while holding ld_lock */ 2272e54ab87fSRoger A. Faulkner self->ul_rtld++; /* don't suspend while in ld.so.1 */ 22737257d1b4Sraf (void) mutex_lock(&udp->ld_lock); 22748cd45542Sraf } 22757c478bd9Sstevel@tonic-gate enter_critical(self); 2276a574db85Sraf self->ul_save_state = self->ul_cancel_disabled; 2277a574db85Sraf self->ul_cancel_disabled = 1; 2278a574db85Sraf set_cancel_pending_flag(self, 0); 22797c478bd9Sstevel@tonic-gate return (1); 22807c478bd9Sstevel@tonic-gate } 22817c478bd9Sstevel@tonic-gate 22827c478bd9Sstevel@tonic-gate int 22838cd45542Sraf _ti_bind_clear(int flags) 22847c478bd9Sstevel@tonic-gate { 22857c478bd9Sstevel@tonic-gate ulwp_t *self = curthread; 22868cd45542Sraf uberdata_t *udp = self->ul_uberdata; 22878cd45542Sraf int bindflag = (flags & THR_FLG_RTLD); 22887c478bd9Sstevel@tonic-gate 22897c478bd9Sstevel@tonic-gate if ((self->ul_bindflags & bindflag) == 0) 22907c478bd9Sstevel@tonic-gate return (self->ul_bindflags); 22917c478bd9Sstevel@tonic-gate self->ul_bindflags &= ~bindflag; 2292a574db85Sraf self->ul_cancel_disabled = self->ul_save_state; 2293a574db85Sraf set_cancel_pending_flag(self, 0); 22947c478bd9Sstevel@tonic-gate exit_critical(self); 22958cd45542Sraf if ((flags & (THR_FLG_NOLOCK | THR_FLG_REENTER)) == THR_FLG_NOLOCK) { 22968cd45542Sraf if (MUTEX_OWNED(&udp->ld_lock, self)) { 22977257d1b4Sraf (void) mutex_unlock(&udp->ld_lock); 2298e54ab87fSRoger A. Faulkner self->ul_rtld--; 22998cd45542Sraf sigon(self); /* reenable signals */ 23008cd45542Sraf } 23018cd45542Sraf } 23027c478bd9Sstevel@tonic-gate return (self->ul_bindflags); 23037c478bd9Sstevel@tonic-gate } 23047c478bd9Sstevel@tonic-gate 23057c478bd9Sstevel@tonic-gate /* 23062a8d6ebaSRod Evans * Tell the dynamic linker (ld.so.1) whether or not it was entered from 23072a8d6ebaSRod Evans * a critical region in libc. Return zero if not, else return non-zero. 23082a8d6ebaSRod Evans */ 23092a8d6ebaSRod Evans int 23102a8d6ebaSRod Evans _ti_critical(void) 23112a8d6ebaSRod Evans { 23122a8d6ebaSRod Evans ulwp_t *self = curthread; 23132a8d6ebaSRod Evans int level = self->ul_critical; 23142a8d6ebaSRod Evans 23152a8d6ebaSRod Evans if ((self->ul_bindflags & THR_FLG_RTLD) == 0 || level == 0) 23162a8d6ebaSRod Evans return (level); /* ld.so.1 hasn't (yet) called enter() */ 23172a8d6ebaSRod Evans return (level - 1); 23182a8d6ebaSRod Evans } 23192a8d6ebaSRod Evans 23202a8d6ebaSRod Evans /* 23217c478bd9Sstevel@tonic-gate * sigoff() and sigon() enable cond_wait() to behave (optionally) like 23227c478bd9Sstevel@tonic-gate * it does in the old libthread (see the comments in cond_wait_queue()). 23237c478bd9Sstevel@tonic-gate * Also, signals are deferred at thread startup until TLS constructors 23247257d1b4Sraf * have all been called, at which time _thrp_setup() calls sigon(). 232534709573Sraf * 2326f841f6adSraf * _sigoff() and _sigon() are external consolidation-private interfaces to 2327f841f6adSraf * sigoff() and sigon(), respectively, in libc. These are used in libnsl. 23287c478bd9Sstevel@tonic-gate * Also, _sigoff() and _sigon() are called from dbx's run-time checking 23297c478bd9Sstevel@tonic-gate * (librtc.so) to defer signals during its critical sections (not to be 23307c478bd9Sstevel@tonic-gate * confused with libc critical sections [see exit_critical() above]). 23317c478bd9Sstevel@tonic-gate */ 23327c478bd9Sstevel@tonic-gate void 23337c478bd9Sstevel@tonic-gate _sigoff(void) 23347c478bd9Sstevel@tonic-gate { 2335e54ab87fSRoger A. Faulkner ulwp_t *self = curthread; 2336e54ab87fSRoger A. Faulkner 2337e54ab87fSRoger A. Faulkner sigoff(self); 23387c478bd9Sstevel@tonic-gate } 23397c478bd9Sstevel@tonic-gate 23407c478bd9Sstevel@tonic-gate void 23417c478bd9Sstevel@tonic-gate _sigon(void) 23427c478bd9Sstevel@tonic-gate { 2343e54ab87fSRoger A. Faulkner ulwp_t *self = curthread; 23447c478bd9Sstevel@tonic-gate 23457c478bd9Sstevel@tonic-gate ASSERT(self->ul_sigdefer > 0); 2346e54ab87fSRoger A. Faulkner sigon(self); 23477c478bd9Sstevel@tonic-gate } 23487c478bd9Sstevel@tonic-gate 23497c478bd9Sstevel@tonic-gate int 23507257d1b4Sraf thr_getconcurrency() 23517c478bd9Sstevel@tonic-gate { 23527c478bd9Sstevel@tonic-gate return (thr_concurrency); 23537c478bd9Sstevel@tonic-gate } 23547c478bd9Sstevel@tonic-gate 23557c478bd9Sstevel@tonic-gate int 23567257d1b4Sraf pthread_getconcurrency() 23577c478bd9Sstevel@tonic-gate { 23587c478bd9Sstevel@tonic-gate return (pthread_concurrency); 23597c478bd9Sstevel@tonic-gate } 23607c478bd9Sstevel@tonic-gate 23617c478bd9Sstevel@tonic-gate int 23627257d1b4Sraf thr_setconcurrency(int new_level) 23637c478bd9Sstevel@tonic-gate { 23647c478bd9Sstevel@tonic-gate uberdata_t *udp = curthread->ul_uberdata; 23657c478bd9Sstevel@tonic-gate 23667c478bd9Sstevel@tonic-gate if (new_level < 0) 23677c478bd9Sstevel@tonic-gate return (EINVAL); 23687c478bd9Sstevel@tonic-gate if (new_level > 65536) /* 65536 is totally arbitrary */ 23697c478bd9Sstevel@tonic-gate return (EAGAIN); 23707c478bd9Sstevel@tonic-gate lmutex_lock(&udp->link_lock); 23717c478bd9Sstevel@tonic-gate if (new_level > thr_concurrency) 23727c478bd9Sstevel@tonic-gate thr_concurrency = new_level; 23737c478bd9Sstevel@tonic-gate lmutex_unlock(&udp->link_lock); 23747c478bd9Sstevel@tonic-gate return (0); 23757c478bd9Sstevel@tonic-gate } 23767c478bd9Sstevel@tonic-gate 23777c478bd9Sstevel@tonic-gate int 23787257d1b4Sraf pthread_setconcurrency(int new_level) 23797c478bd9Sstevel@tonic-gate { 23807c478bd9Sstevel@tonic-gate if (new_level < 0) 23817c478bd9Sstevel@tonic-gate return (EINVAL); 23827c478bd9Sstevel@tonic-gate if (new_level > 65536) /* 65536 is totally arbitrary */ 23837c478bd9Sstevel@tonic-gate return (EAGAIN); 23847c478bd9Sstevel@tonic-gate pthread_concurrency = new_level; 23857c478bd9Sstevel@tonic-gate return (0); 23867c478bd9Sstevel@tonic-gate } 23877c478bd9Sstevel@tonic-gate 23887c478bd9Sstevel@tonic-gate size_t 23897257d1b4Sraf thr_min_stack(void) 23907c478bd9Sstevel@tonic-gate { 23917c478bd9Sstevel@tonic-gate return (MINSTACK); 23927c478bd9Sstevel@tonic-gate } 23937c478bd9Sstevel@tonic-gate 23947c478bd9Sstevel@tonic-gate int 23957c478bd9Sstevel@tonic-gate __nthreads(void) 23967c478bd9Sstevel@tonic-gate { 23977c478bd9Sstevel@tonic-gate return (curthread->ul_uberdata->nthreads); 23987c478bd9Sstevel@tonic-gate } 23997c478bd9Sstevel@tonic-gate 24007c478bd9Sstevel@tonic-gate /* 24017c478bd9Sstevel@tonic-gate * XXX 24027c478bd9Sstevel@tonic-gate * The remainder of this file implements the private interfaces to java for 24037c478bd9Sstevel@tonic-gate * garbage collection. It is no longer used, at least by java 1.2. 24047c478bd9Sstevel@tonic-gate * It can all go away once all old JVMs have disappeared. 24057c478bd9Sstevel@tonic-gate */ 24067c478bd9Sstevel@tonic-gate 24077c478bd9Sstevel@tonic-gate int suspendingallmutators; /* when non-zero, suspending all mutators. */ 24087c478bd9Sstevel@tonic-gate int suspendedallmutators; /* when non-zero, all mutators suspended. */ 24097c478bd9Sstevel@tonic-gate int mutatorsbarrier; /* when non-zero, mutators barrier imposed. */ 24107c478bd9Sstevel@tonic-gate mutex_t mutatorslock = DEFAULTMUTEX; /* used to enforce mutators barrier. */ 24117c478bd9Sstevel@tonic-gate cond_t mutatorscv = DEFAULTCV; /* where non-mutators sleep. */ 24127c478bd9Sstevel@tonic-gate 24137c478bd9Sstevel@tonic-gate /* 24147c478bd9Sstevel@tonic-gate * Get the available register state for the target thread. 24157c478bd9Sstevel@tonic-gate * Return non-volatile registers: TRS_NONVOLATILE 24167c478bd9Sstevel@tonic-gate */ 24177257d1b4Sraf #pragma weak _thr_getstate = thr_getstate 24187c478bd9Sstevel@tonic-gate int 24197257d1b4Sraf thr_getstate(thread_t tid, int *flag, lwpid_t *lwp, stack_t *ss, gregset_t rs) 24207c478bd9Sstevel@tonic-gate { 24217c478bd9Sstevel@tonic-gate ulwp_t *self = curthread; 24227c478bd9Sstevel@tonic-gate uberdata_t *udp = self->ul_uberdata; 24237c478bd9Sstevel@tonic-gate ulwp_t **ulwpp; 24247c478bd9Sstevel@tonic-gate ulwp_t *ulwp; 24257c478bd9Sstevel@tonic-gate int error = 0; 24267c478bd9Sstevel@tonic-gate int trs_flag = TRS_LWPID; 24277c478bd9Sstevel@tonic-gate 24287c478bd9Sstevel@tonic-gate if (tid == 0 || self->ul_lwpid == tid) { 24297c478bd9Sstevel@tonic-gate ulwp = self; 24307c478bd9Sstevel@tonic-gate ulwp_lock(ulwp, udp); 24317c478bd9Sstevel@tonic-gate } else if ((ulwpp = find_lwpp(tid)) != NULL) { 24327c478bd9Sstevel@tonic-gate ulwp = *ulwpp; 24337c478bd9Sstevel@tonic-gate } else { 24347c478bd9Sstevel@tonic-gate if (flag) 24357c478bd9Sstevel@tonic-gate *flag = TRS_INVALID; 24367c478bd9Sstevel@tonic-gate return (ESRCH); 24377c478bd9Sstevel@tonic-gate } 24387c478bd9Sstevel@tonic-gate 24397c478bd9Sstevel@tonic-gate if (ulwp->ul_dead) { 24407c478bd9Sstevel@tonic-gate trs_flag = TRS_INVALID; 24417c478bd9Sstevel@tonic-gate } else if (!ulwp->ul_stop && !suspendedallmutators) { 24427c478bd9Sstevel@tonic-gate error = EINVAL; 24437c478bd9Sstevel@tonic-gate trs_flag = TRS_INVALID; 24447c478bd9Sstevel@tonic-gate } else if (ulwp->ul_stop) { 24457c478bd9Sstevel@tonic-gate trs_flag = TRS_NONVOLATILE; 24467c478bd9Sstevel@tonic-gate getgregs(ulwp, rs); 24477c478bd9Sstevel@tonic-gate } 24487c478bd9Sstevel@tonic-gate 24497c478bd9Sstevel@tonic-gate if (flag) 24507c478bd9Sstevel@tonic-gate *flag = trs_flag; 24517c478bd9Sstevel@tonic-gate if (lwp) 24527c478bd9Sstevel@tonic-gate *lwp = tid; 24537c478bd9Sstevel@tonic-gate if (ss != NULL) 24547c478bd9Sstevel@tonic-gate (void) _thrp_stksegment(ulwp, ss); 24557c478bd9Sstevel@tonic-gate 24567c478bd9Sstevel@tonic-gate ulwp_unlock(ulwp, udp); 24577c478bd9Sstevel@tonic-gate return (error); 24587c478bd9Sstevel@tonic-gate } 24597c478bd9Sstevel@tonic-gate 24607c478bd9Sstevel@tonic-gate /* 24617c478bd9Sstevel@tonic-gate * Set the appropriate register state for the target thread. 24627c478bd9Sstevel@tonic-gate * This is not used by java. It exists solely for the MSTC test suite. 24637c478bd9Sstevel@tonic-gate */ 24647257d1b4Sraf #pragma weak _thr_setstate = thr_setstate 24657c478bd9Sstevel@tonic-gate int 24667257d1b4Sraf thr_setstate(thread_t tid, int flag, gregset_t rs) 24677c478bd9Sstevel@tonic-gate { 24687c478bd9Sstevel@tonic-gate uberdata_t *udp = curthread->ul_uberdata; 24697c478bd9Sstevel@tonic-gate ulwp_t *ulwp; 24707c478bd9Sstevel@tonic-gate int error = 0; 24717c478bd9Sstevel@tonic-gate 24727c478bd9Sstevel@tonic-gate if ((ulwp = find_lwp(tid)) == NULL) 24737c478bd9Sstevel@tonic-gate return (ESRCH); 24747c478bd9Sstevel@tonic-gate 24757c478bd9Sstevel@tonic-gate if (!ulwp->ul_stop && !suspendedallmutators) 24767c478bd9Sstevel@tonic-gate error = EINVAL; 24777c478bd9Sstevel@tonic-gate else if (rs != NULL) { 24787c478bd9Sstevel@tonic-gate switch (flag) { 24797c478bd9Sstevel@tonic-gate case TRS_NONVOLATILE: 24807c478bd9Sstevel@tonic-gate /* do /proc stuff here? */ 24817c478bd9Sstevel@tonic-gate if (ulwp->ul_stop) 24827c478bd9Sstevel@tonic-gate setgregs(ulwp, rs); 24837c478bd9Sstevel@tonic-gate else 24847c478bd9Sstevel@tonic-gate error = EINVAL; 24857c478bd9Sstevel@tonic-gate break; 24867c478bd9Sstevel@tonic-gate case TRS_LWPID: /* do /proc stuff here? */ 24877c478bd9Sstevel@tonic-gate default: 24887c478bd9Sstevel@tonic-gate error = EINVAL; 24897c478bd9Sstevel@tonic-gate break; 24907c478bd9Sstevel@tonic-gate } 24917c478bd9Sstevel@tonic-gate } 24927c478bd9Sstevel@tonic-gate 24937c478bd9Sstevel@tonic-gate ulwp_unlock(ulwp, udp); 24947c478bd9Sstevel@tonic-gate return (error); 24957c478bd9Sstevel@tonic-gate } 24967c478bd9Sstevel@tonic-gate 24977c478bd9Sstevel@tonic-gate int 24987c478bd9Sstevel@tonic-gate getlwpstatus(thread_t tid, struct lwpstatus *sp) 24997c478bd9Sstevel@tonic-gate { 2500a574db85Sraf extern ssize_t __pread(int, void *, size_t, off_t); 25017c478bd9Sstevel@tonic-gate char buf[100]; 25027c478bd9Sstevel@tonic-gate int fd; 25037c478bd9Sstevel@tonic-gate 25047c478bd9Sstevel@tonic-gate /* "/proc/self/lwp/%u/lwpstatus" w/o stdio */ 25057c478bd9Sstevel@tonic-gate (void) strcpy(buf, "/proc/self/lwp/"); 25067c478bd9Sstevel@tonic-gate ultos((uint64_t)tid, 10, buf + strlen(buf)); 25077c478bd9Sstevel@tonic-gate (void) strcat(buf, "/lwpstatus"); 25088cd45542Sraf if ((fd = __open(buf, O_RDONLY, 0)) >= 0) { 2509a574db85Sraf while (__pread(fd, sp, sizeof (*sp), 0) == sizeof (*sp)) { 25107c478bd9Sstevel@tonic-gate if (sp->pr_flags & PR_STOPPED) { 25118cd45542Sraf (void) __close(fd); 25127c478bd9Sstevel@tonic-gate return (0); 25137c478bd9Sstevel@tonic-gate } 25148cd45542Sraf yield(); /* give him a chance to stop */ 25157c478bd9Sstevel@tonic-gate } 25168cd45542Sraf (void) __close(fd); 25177c478bd9Sstevel@tonic-gate } 25187c478bd9Sstevel@tonic-gate return (-1); 25197c478bd9Sstevel@tonic-gate } 25207c478bd9Sstevel@tonic-gate 25217c478bd9Sstevel@tonic-gate int 25227c478bd9Sstevel@tonic-gate putlwpregs(thread_t tid, prgregset_t prp) 25237c478bd9Sstevel@tonic-gate { 2524a574db85Sraf extern ssize_t __writev(int, const struct iovec *, int); 25257c478bd9Sstevel@tonic-gate char buf[100]; 25267c478bd9Sstevel@tonic-gate int fd; 25277c478bd9Sstevel@tonic-gate long dstop_sreg[2]; 25287c478bd9Sstevel@tonic-gate long run_null[2]; 25297c478bd9Sstevel@tonic-gate iovec_t iov[3]; 25307c478bd9Sstevel@tonic-gate 25317c478bd9Sstevel@tonic-gate /* "/proc/self/lwp/%u/lwpctl" w/o stdio */ 25327c478bd9Sstevel@tonic-gate (void) strcpy(buf, "/proc/self/lwp/"); 25337c478bd9Sstevel@tonic-gate ultos((uint64_t)tid, 10, buf + strlen(buf)); 25347c478bd9Sstevel@tonic-gate (void) strcat(buf, "/lwpctl"); 25358cd45542Sraf if ((fd = __open(buf, O_WRONLY, 0)) >= 0) { 25367c478bd9Sstevel@tonic-gate dstop_sreg[0] = PCDSTOP; /* direct it to stop */ 25377c478bd9Sstevel@tonic-gate dstop_sreg[1] = PCSREG; /* set the registers */ 25387c478bd9Sstevel@tonic-gate iov[0].iov_base = (caddr_t)dstop_sreg; 25397c478bd9Sstevel@tonic-gate iov[0].iov_len = sizeof (dstop_sreg); 25407c478bd9Sstevel@tonic-gate iov[1].iov_base = (caddr_t)prp; /* from the register set */ 25417c478bd9Sstevel@tonic-gate iov[1].iov_len = sizeof (prgregset_t); 25427c478bd9Sstevel@tonic-gate run_null[0] = PCRUN; /* make it runnable again */ 25437c478bd9Sstevel@tonic-gate run_null[1] = 0; 25447c478bd9Sstevel@tonic-gate iov[2].iov_base = (caddr_t)run_null; 25457c478bd9Sstevel@tonic-gate iov[2].iov_len = sizeof (run_null); 2546a574db85Sraf if (__writev(fd, iov, 3) >= 0) { 25478cd45542Sraf (void) __close(fd); 25487c478bd9Sstevel@tonic-gate return (0); 25497c478bd9Sstevel@tonic-gate } 25508cd45542Sraf (void) __close(fd); 25517c478bd9Sstevel@tonic-gate } 25527c478bd9Sstevel@tonic-gate return (-1); 25537c478bd9Sstevel@tonic-gate } 25547c478bd9Sstevel@tonic-gate 25557c478bd9Sstevel@tonic-gate static ulong_t 25567c478bd9Sstevel@tonic-gate gettsp_slow(thread_t tid) 25577c478bd9Sstevel@tonic-gate { 25587c478bd9Sstevel@tonic-gate char buf[100]; 25597c478bd9Sstevel@tonic-gate struct lwpstatus status; 25607c478bd9Sstevel@tonic-gate 25617c478bd9Sstevel@tonic-gate if (getlwpstatus(tid, &status) != 0) { 25627c478bd9Sstevel@tonic-gate /* "__gettsp(%u): can't read lwpstatus" w/o stdio */ 25637c478bd9Sstevel@tonic-gate (void) strcpy(buf, "__gettsp("); 25647c478bd9Sstevel@tonic-gate ultos((uint64_t)tid, 10, buf + strlen(buf)); 25657c478bd9Sstevel@tonic-gate (void) strcat(buf, "): can't read lwpstatus"); 25667c478bd9Sstevel@tonic-gate thr_panic(buf); 25677c478bd9Sstevel@tonic-gate } 25687c478bd9Sstevel@tonic-gate return (status.pr_reg[R_SP]); 25697c478bd9Sstevel@tonic-gate } 25707c478bd9Sstevel@tonic-gate 25717c478bd9Sstevel@tonic-gate ulong_t 25727c478bd9Sstevel@tonic-gate __gettsp(thread_t tid) 25737c478bd9Sstevel@tonic-gate { 25747c478bd9Sstevel@tonic-gate uberdata_t *udp = curthread->ul_uberdata; 25757c478bd9Sstevel@tonic-gate ulwp_t *ulwp; 25767c478bd9Sstevel@tonic-gate ulong_t result; 25777c478bd9Sstevel@tonic-gate 25787c478bd9Sstevel@tonic-gate if ((ulwp = find_lwp(tid)) == NULL) 25797c478bd9Sstevel@tonic-gate return (0); 25807c478bd9Sstevel@tonic-gate 25817c478bd9Sstevel@tonic-gate if (ulwp->ul_stop && (result = ulwp->ul_sp) != 0) { 25827c478bd9Sstevel@tonic-gate ulwp_unlock(ulwp, udp); 25837c478bd9Sstevel@tonic-gate return (result); 25847c478bd9Sstevel@tonic-gate } 25857c478bd9Sstevel@tonic-gate 25867c478bd9Sstevel@tonic-gate result = gettsp_slow(tid); 25877c478bd9Sstevel@tonic-gate ulwp_unlock(ulwp, udp); 25887c478bd9Sstevel@tonic-gate return (result); 25897c478bd9Sstevel@tonic-gate } 25907c478bd9Sstevel@tonic-gate 25917c478bd9Sstevel@tonic-gate /* 25927c478bd9Sstevel@tonic-gate * This tells java stack walkers how to find the ucontext 25937c478bd9Sstevel@tonic-gate * structure passed to signal handlers. 25947c478bd9Sstevel@tonic-gate */ 25957257d1b4Sraf #pragma weak _thr_sighndlrinfo = thr_sighndlrinfo 25967c478bd9Sstevel@tonic-gate void 25977257d1b4Sraf thr_sighndlrinfo(void (**func)(), int *funcsize) 25987c478bd9Sstevel@tonic-gate { 25997c478bd9Sstevel@tonic-gate *func = &__sighndlr; 26007c478bd9Sstevel@tonic-gate *funcsize = (char *)&__sighndlrend - (char *)&__sighndlr; 26017c478bd9Sstevel@tonic-gate } 26027c478bd9Sstevel@tonic-gate 26037c478bd9Sstevel@tonic-gate /* 26047c478bd9Sstevel@tonic-gate * Mark a thread a mutator or reset a mutator to being a default, 26057c478bd9Sstevel@tonic-gate * non-mutator thread. 26067c478bd9Sstevel@tonic-gate */ 26077257d1b4Sraf #pragma weak _thr_setmutator = thr_setmutator 26087c478bd9Sstevel@tonic-gate int 26097257d1b4Sraf thr_setmutator(thread_t tid, int enabled) 26107c478bd9Sstevel@tonic-gate { 26117c478bd9Sstevel@tonic-gate ulwp_t *self = curthread; 26127c478bd9Sstevel@tonic-gate uberdata_t *udp = self->ul_uberdata; 26137c478bd9Sstevel@tonic-gate ulwp_t *ulwp; 26147c478bd9Sstevel@tonic-gate int error; 2615a574db85Sraf int cancel_state; 26167c478bd9Sstevel@tonic-gate 26177c478bd9Sstevel@tonic-gate enabled = enabled? 1 : 0; 26187c478bd9Sstevel@tonic-gate top: 26197c478bd9Sstevel@tonic-gate if (tid == 0) { 26207c478bd9Sstevel@tonic-gate ulwp = self; 26217c478bd9Sstevel@tonic-gate ulwp_lock(ulwp, udp); 26227c478bd9Sstevel@tonic-gate } else if ((ulwp = find_lwp(tid)) == NULL) { 26237c478bd9Sstevel@tonic-gate return (ESRCH); 26247c478bd9Sstevel@tonic-gate } 26257c478bd9Sstevel@tonic-gate 26267c478bd9Sstevel@tonic-gate /* 26277c478bd9Sstevel@tonic-gate * The target thread should be the caller itself or a suspended thread. 26287c478bd9Sstevel@tonic-gate * This prevents the target from also changing its ul_mutator field. 26297c478bd9Sstevel@tonic-gate */ 26307c478bd9Sstevel@tonic-gate error = 0; 26317c478bd9Sstevel@tonic-gate if (ulwp != self && !ulwp->ul_stop && enabled) 26327c478bd9Sstevel@tonic-gate error = EINVAL; 26337c478bd9Sstevel@tonic-gate else if (ulwp->ul_mutator != enabled) { 26347c478bd9Sstevel@tonic-gate lmutex_lock(&mutatorslock); 26357c478bd9Sstevel@tonic-gate if (mutatorsbarrier) { 26367c478bd9Sstevel@tonic-gate ulwp_unlock(ulwp, udp); 26377257d1b4Sraf (void) pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, 2638a574db85Sraf &cancel_state); 26397c478bd9Sstevel@tonic-gate while (mutatorsbarrier) 26407257d1b4Sraf (void) cond_wait(&mutatorscv, &mutatorslock); 26417257d1b4Sraf (void) pthread_setcancelstate(cancel_state, NULL); 26427c478bd9Sstevel@tonic-gate lmutex_unlock(&mutatorslock); 26437c478bd9Sstevel@tonic-gate goto top; 26447c478bd9Sstevel@tonic-gate } 26457c478bd9Sstevel@tonic-gate ulwp->ul_mutator = enabled; 26467c478bd9Sstevel@tonic-gate lmutex_unlock(&mutatorslock); 26477c478bd9Sstevel@tonic-gate } 26487c478bd9Sstevel@tonic-gate 26497c478bd9Sstevel@tonic-gate ulwp_unlock(ulwp, udp); 26507c478bd9Sstevel@tonic-gate return (error); 26517c478bd9Sstevel@tonic-gate } 26527c478bd9Sstevel@tonic-gate 26537c478bd9Sstevel@tonic-gate /* 26547c478bd9Sstevel@tonic-gate * Establish a barrier against new mutators. Any non-mutator trying 26557c478bd9Sstevel@tonic-gate * to become a mutator is suspended until the barrier is removed. 26567c478bd9Sstevel@tonic-gate */ 26577257d1b4Sraf #pragma weak _thr_mutators_barrier = thr_mutators_barrier 26587c478bd9Sstevel@tonic-gate void 26597257d1b4Sraf thr_mutators_barrier(int enabled) 26607c478bd9Sstevel@tonic-gate { 26617c478bd9Sstevel@tonic-gate int oldvalue; 2662a574db85Sraf int cancel_state; 26637c478bd9Sstevel@tonic-gate 26647c478bd9Sstevel@tonic-gate lmutex_lock(&mutatorslock); 26657c478bd9Sstevel@tonic-gate 26667c478bd9Sstevel@tonic-gate /* 26677c478bd9Sstevel@tonic-gate * Wait if trying to set the barrier while it is already set. 26687c478bd9Sstevel@tonic-gate */ 26697257d1b4Sraf (void) pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cancel_state); 26707c478bd9Sstevel@tonic-gate while (mutatorsbarrier && enabled) 26717257d1b4Sraf (void) cond_wait(&mutatorscv, &mutatorslock); 26727257d1b4Sraf (void) pthread_setcancelstate(cancel_state, NULL); 26737c478bd9Sstevel@tonic-gate 26747c478bd9Sstevel@tonic-gate oldvalue = mutatorsbarrier; 26757c478bd9Sstevel@tonic-gate mutatorsbarrier = enabled; 26767c478bd9Sstevel@tonic-gate /* 26777c478bd9Sstevel@tonic-gate * Wakeup any blocked non-mutators when barrier is removed. 26787c478bd9Sstevel@tonic-gate */ 26797c478bd9Sstevel@tonic-gate if (oldvalue && !enabled) 26807257d1b4Sraf (void) cond_broadcast(&mutatorscv); 26817c478bd9Sstevel@tonic-gate lmutex_unlock(&mutatorslock); 26827c478bd9Sstevel@tonic-gate } 26837c478bd9Sstevel@tonic-gate 26847c478bd9Sstevel@tonic-gate /* 26857c478bd9Sstevel@tonic-gate * Suspend the set of all mutators except for the caller. The list 26867c478bd9Sstevel@tonic-gate * of actively running threads is searched and only the mutators 26877c478bd9Sstevel@tonic-gate * in this list are suspended. Actively running non-mutators remain 26887c478bd9Sstevel@tonic-gate * running. Any other thread is suspended. 26897c478bd9Sstevel@tonic-gate */ 26907257d1b4Sraf #pragma weak _thr_suspend_allmutators = thr_suspend_allmutators 26917c478bd9Sstevel@tonic-gate int 26927257d1b4Sraf thr_suspend_allmutators(void) 26937c478bd9Sstevel@tonic-gate { 26947c478bd9Sstevel@tonic-gate ulwp_t *self = curthread; 26957c478bd9Sstevel@tonic-gate uberdata_t *udp = self->ul_uberdata; 26967c478bd9Sstevel@tonic-gate ulwp_t *ulwp; 26977c478bd9Sstevel@tonic-gate int link_dropped; 26987c478bd9Sstevel@tonic-gate 26997c478bd9Sstevel@tonic-gate /* 270036319254Sraf * We single-thread the entire thread suspend/continue mechanism. 27017c478bd9Sstevel@tonic-gate */ 27022e145884Sraf fork_lock_enter(); 270336319254Sraf 27047c478bd9Sstevel@tonic-gate top: 27057c478bd9Sstevel@tonic-gate lmutex_lock(&udp->link_lock); 27067c478bd9Sstevel@tonic-gate 27077c478bd9Sstevel@tonic-gate if (suspendingallmutators || suspendedallmutators) { 27087c478bd9Sstevel@tonic-gate lmutex_unlock(&udp->link_lock); 27097c478bd9Sstevel@tonic-gate fork_lock_exit(); 27107c478bd9Sstevel@tonic-gate return (EINVAL); 27117c478bd9Sstevel@tonic-gate } 27127c478bd9Sstevel@tonic-gate suspendingallmutators = 1; 27137c478bd9Sstevel@tonic-gate 27147c478bd9Sstevel@tonic-gate for (ulwp = self->ul_forw; ulwp != self; ulwp = ulwp->ul_forw) { 27157c478bd9Sstevel@tonic-gate ulwp_lock(ulwp, udp); 27167c478bd9Sstevel@tonic-gate if (!ulwp->ul_mutator) { 27177c478bd9Sstevel@tonic-gate ulwp_unlock(ulwp, udp); 27187c478bd9Sstevel@tonic-gate } else if (ulwp->ul_stop) { /* already stopped */ 27197c478bd9Sstevel@tonic-gate ulwp->ul_stop |= TSTP_MUTATOR; 27207c478bd9Sstevel@tonic-gate ulwp_broadcast(ulwp); 27217c478bd9Sstevel@tonic-gate ulwp_unlock(ulwp, udp); 27227c478bd9Sstevel@tonic-gate } else { 27237c478bd9Sstevel@tonic-gate /* 27247c478bd9Sstevel@tonic-gate * Move the stopped lwp out of a critical section. 27257c478bd9Sstevel@tonic-gate */ 27267c478bd9Sstevel@tonic-gate if (safe_suspend(ulwp, TSTP_MUTATOR, &link_dropped) || 27277c478bd9Sstevel@tonic-gate link_dropped) { 27287c478bd9Sstevel@tonic-gate suspendingallmutators = 0; 27297c478bd9Sstevel@tonic-gate goto top; 27307c478bd9Sstevel@tonic-gate } 27317c478bd9Sstevel@tonic-gate } 27327c478bd9Sstevel@tonic-gate } 27337c478bd9Sstevel@tonic-gate 27347c478bd9Sstevel@tonic-gate suspendedallmutators = 1; 27357c478bd9Sstevel@tonic-gate suspendingallmutators = 0; 27367c478bd9Sstevel@tonic-gate lmutex_unlock(&udp->link_lock); 27377c478bd9Sstevel@tonic-gate fork_lock_exit(); 27387c478bd9Sstevel@tonic-gate return (0); 27397c478bd9Sstevel@tonic-gate } 27407c478bd9Sstevel@tonic-gate 27417c478bd9Sstevel@tonic-gate /* 27427c478bd9Sstevel@tonic-gate * Suspend the target mutator. The caller is permitted to suspend 27437c478bd9Sstevel@tonic-gate * itself. If a mutator barrier is enabled, the caller will suspend 27447c478bd9Sstevel@tonic-gate * itself as though it had been suspended by thr_suspend_allmutators(). 27457c478bd9Sstevel@tonic-gate * When the barrier is removed, this thread will be resumed. Any 27467c478bd9Sstevel@tonic-gate * suspended mutator, whether suspended by thr_suspend_mutator(), or by 27477c478bd9Sstevel@tonic-gate * thr_suspend_allmutators(), can be resumed by thr_continue_mutator(). 27487c478bd9Sstevel@tonic-gate */ 27497257d1b4Sraf #pragma weak _thr_suspend_mutator = thr_suspend_mutator 27507c478bd9Sstevel@tonic-gate int 27517257d1b4Sraf thr_suspend_mutator(thread_t tid) 27527c478bd9Sstevel@tonic-gate { 27537c478bd9Sstevel@tonic-gate if (tid == 0) 27547c478bd9Sstevel@tonic-gate tid = curthread->ul_lwpid; 27557c478bd9Sstevel@tonic-gate return (_thrp_suspend(tid, TSTP_MUTATOR)); 27567c478bd9Sstevel@tonic-gate } 27577c478bd9Sstevel@tonic-gate 27587c478bd9Sstevel@tonic-gate /* 27597c478bd9Sstevel@tonic-gate * Resume the set of all suspended mutators. 27607c478bd9Sstevel@tonic-gate */ 27617257d1b4Sraf #pragma weak _thr_continue_allmutators = thr_continue_allmutators 27627c478bd9Sstevel@tonic-gate int 27637257d1b4Sraf thr_continue_allmutators() 27647c478bd9Sstevel@tonic-gate { 27657c478bd9Sstevel@tonic-gate ulwp_t *self = curthread; 27667c478bd9Sstevel@tonic-gate uberdata_t *udp = self->ul_uberdata; 27677c478bd9Sstevel@tonic-gate ulwp_t *ulwp; 276836319254Sraf 276936319254Sraf /* 277036319254Sraf * We single-thread the entire thread suspend/continue mechanism. 277136319254Sraf */ 27722e145884Sraf fork_lock_enter(); 27737c478bd9Sstevel@tonic-gate 27747c478bd9Sstevel@tonic-gate lmutex_lock(&udp->link_lock); 27757c478bd9Sstevel@tonic-gate if (!suspendedallmutators) { 27767c478bd9Sstevel@tonic-gate lmutex_unlock(&udp->link_lock); 277736319254Sraf fork_lock_exit(); 27787c478bd9Sstevel@tonic-gate return (EINVAL); 27797c478bd9Sstevel@tonic-gate } 27807c478bd9Sstevel@tonic-gate suspendedallmutators = 0; 27817c478bd9Sstevel@tonic-gate 27827c478bd9Sstevel@tonic-gate for (ulwp = self->ul_forw; ulwp != self; ulwp = ulwp->ul_forw) { 27837c478bd9Sstevel@tonic-gate mutex_t *mp = ulwp_mutex(ulwp, udp); 27847c478bd9Sstevel@tonic-gate lmutex_lock(mp); 27857c478bd9Sstevel@tonic-gate if (ulwp->ul_stop & TSTP_MUTATOR) { 27867c478bd9Sstevel@tonic-gate ulwp->ul_stop &= ~TSTP_MUTATOR; 27877c478bd9Sstevel@tonic-gate ulwp_broadcast(ulwp); 27887c478bd9Sstevel@tonic-gate if (!ulwp->ul_stop) 27897c478bd9Sstevel@tonic-gate force_continue(ulwp); 27907c478bd9Sstevel@tonic-gate } 27917c478bd9Sstevel@tonic-gate lmutex_unlock(mp); 27927c478bd9Sstevel@tonic-gate } 27937c478bd9Sstevel@tonic-gate 27947c478bd9Sstevel@tonic-gate lmutex_unlock(&udp->link_lock); 279536319254Sraf fork_lock_exit(); 27967c478bd9Sstevel@tonic-gate return (0); 27977c478bd9Sstevel@tonic-gate } 27987c478bd9Sstevel@tonic-gate 27997c478bd9Sstevel@tonic-gate /* 28007c478bd9Sstevel@tonic-gate * Resume a suspended mutator. 28017c478bd9Sstevel@tonic-gate */ 28027257d1b4Sraf #pragma weak _thr_continue_mutator = thr_continue_mutator 28037c478bd9Sstevel@tonic-gate int 28047257d1b4Sraf thr_continue_mutator(thread_t tid) 28057c478bd9Sstevel@tonic-gate { 28067c478bd9Sstevel@tonic-gate return (_thrp_continue(tid, TSTP_MUTATOR)); 28077c478bd9Sstevel@tonic-gate } 28087c478bd9Sstevel@tonic-gate 28097257d1b4Sraf #pragma weak _thr_wait_mutator = thr_wait_mutator 28107c478bd9Sstevel@tonic-gate int 28117257d1b4Sraf thr_wait_mutator(thread_t tid, int dontwait) 28127c478bd9Sstevel@tonic-gate { 28137c478bd9Sstevel@tonic-gate uberdata_t *udp = curthread->ul_uberdata; 28147c478bd9Sstevel@tonic-gate ulwp_t *ulwp; 2815a574db85Sraf int cancel_state; 28167c478bd9Sstevel@tonic-gate int error = 0; 28177c478bd9Sstevel@tonic-gate 28187257d1b4Sraf (void) pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cancel_state); 28197c478bd9Sstevel@tonic-gate top: 2820a574db85Sraf if ((ulwp = find_lwp(tid)) == NULL) { 28217257d1b4Sraf (void) pthread_setcancelstate(cancel_state, NULL); 28227c478bd9Sstevel@tonic-gate return (ESRCH); 2823a574db85Sraf } 28247c478bd9Sstevel@tonic-gate 28257c478bd9Sstevel@tonic-gate if (!ulwp->ul_mutator) 28267c478bd9Sstevel@tonic-gate error = EINVAL; 28277c478bd9Sstevel@tonic-gate else if (dontwait) { 28287c478bd9Sstevel@tonic-gate if (!(ulwp->ul_stop & TSTP_MUTATOR)) 28297c478bd9Sstevel@tonic-gate error = EWOULDBLOCK; 28307c478bd9Sstevel@tonic-gate } else if (!(ulwp->ul_stop & TSTP_MUTATOR)) { 28317c478bd9Sstevel@tonic-gate cond_t *cvp = ulwp_condvar(ulwp, udp); 28327c478bd9Sstevel@tonic-gate mutex_t *mp = ulwp_mutex(ulwp, udp); 28337c478bd9Sstevel@tonic-gate 28347257d1b4Sraf (void) cond_wait(cvp, mp); 28357c478bd9Sstevel@tonic-gate (void) lmutex_unlock(mp); 28367c478bd9Sstevel@tonic-gate goto top; 28377c478bd9Sstevel@tonic-gate } 28387c478bd9Sstevel@tonic-gate 28397c478bd9Sstevel@tonic-gate ulwp_unlock(ulwp, udp); 28407257d1b4Sraf (void) pthread_setcancelstate(cancel_state, NULL); 28417c478bd9Sstevel@tonic-gate return (error); 28427c478bd9Sstevel@tonic-gate } 28437c478bd9Sstevel@tonic-gate 28447c478bd9Sstevel@tonic-gate /* PROBE_SUPPORT begin */ 28457c478bd9Sstevel@tonic-gate 28467c478bd9Sstevel@tonic-gate void 28477c478bd9Sstevel@tonic-gate thr_probe_setup(void *data) 28487c478bd9Sstevel@tonic-gate { 28497c478bd9Sstevel@tonic-gate curthread->ul_tpdp = data; 28507c478bd9Sstevel@tonic-gate } 28517c478bd9Sstevel@tonic-gate 28527c478bd9Sstevel@tonic-gate static void * 28537c478bd9Sstevel@tonic-gate _thread_probe_getfunc() 28547c478bd9Sstevel@tonic-gate { 28557c478bd9Sstevel@tonic-gate return (curthread->ul_tpdp); 28567c478bd9Sstevel@tonic-gate } 28577c478bd9Sstevel@tonic-gate 28587c478bd9Sstevel@tonic-gate void * (*thr_probe_getfunc_addr)(void) = _thread_probe_getfunc; 28597c478bd9Sstevel@tonic-gate 28607c478bd9Sstevel@tonic-gate /* ARGSUSED */ 28617c478bd9Sstevel@tonic-gate void 28627c478bd9Sstevel@tonic-gate _resume(ulwp_t *ulwp, caddr_t sp, int dontsave) 28637c478bd9Sstevel@tonic-gate { 28647c478bd9Sstevel@tonic-gate /* never called */ 28657c478bd9Sstevel@tonic-gate } 28667c478bd9Sstevel@tonic-gate 28677c478bd9Sstevel@tonic-gate /* ARGSUSED */ 28687c478bd9Sstevel@tonic-gate void 28697c478bd9Sstevel@tonic-gate _resume_ret(ulwp_t *oldlwp) 28707c478bd9Sstevel@tonic-gate { 28717c478bd9Sstevel@tonic-gate /* never called */ 28727c478bd9Sstevel@tonic-gate } 28737c478bd9Sstevel@tonic-gate 28747c478bd9Sstevel@tonic-gate /* PROBE_SUPPORT end */ 2875