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.
24f422aba8SYouzhong Yang * Copyright (c) 2017 by The MathWorks, Inc. All rights reserved.
257c478bd9Sstevel@tonic-gate */
263e3d658fSRobert Mustacchi /*
273e3d658fSRobert Mustacchi * Copyright (c) 2012, Joyent, Inc. All rights reserved.
283e3d658fSRobert 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 */
1097c478bd9Sstevel@tonic-gate { DEFAULTMUTEX, 0, 0, NULL }, /* tsd_metadata */
1107c478bd9Sstevel@tonic-gate { DEFAULTMUTEX, {0, 0}, {0, 0} }, /* tls_metadata */
1117c478bd9Sstevel@tonic-gate 0, /* primary_map */
1127c478bd9Sstevel@tonic-gate 0, /* bucket_init */
1137c478bd9Sstevel@tonic-gate 0, /* pad[0] */
1147c478bd9Sstevel@tonic-gate 0, /* pad[1] */
1157c478bd9Sstevel@tonic-gate { 0 }, /* uberflags */
1167c478bd9Sstevel@tonic-gate NULL, /* queue_head */
1177c478bd9Sstevel@tonic-gate init_hash_table, /* thr_hash_table */
1187c478bd9Sstevel@tonic-gate 1, /* hash_size: size of the hash table */
1197c478bd9Sstevel@tonic-gate 0, /* hash_mask: hash_size - 1 */
1207c478bd9Sstevel@tonic-gate NULL, /* ulwp_one */
1217c478bd9Sstevel@tonic-gate NULL, /* all_lwps */
1227c478bd9Sstevel@tonic-gate NULL, /* all_zombies */
1237c478bd9Sstevel@tonic-gate 0, /* nthreads */
1247c478bd9Sstevel@tonic-gate 0, /* nzombies */
1257c478bd9Sstevel@tonic-gate 0, /* ndaemons */
1267c478bd9Sstevel@tonic-gate 0, /* pid */
1277c478bd9Sstevel@tonic-gate sigacthandler, /* sigacthandler */
1287c478bd9Sstevel@tonic-gate NULL, /* lwp_stacks */
1297c478bd9Sstevel@tonic-gate NULL, /* lwp_laststack */
1307c478bd9Sstevel@tonic-gate 0, /* nfreestack */
1317c478bd9Sstevel@tonic-gate 10, /* thread_stack_cache */
1327c478bd9Sstevel@tonic-gate NULL, /* ulwp_freelist */
1337c478bd9Sstevel@tonic-gate NULL, /* ulwp_lastfree */
1347c478bd9Sstevel@tonic-gate NULL, /* ulwp_replace_free */
1357c478bd9Sstevel@tonic-gate NULL, /* ulwp_replace_last */
1367c478bd9Sstevel@tonic-gate NULL, /* atforklist */
137883492d5Sraf NULL, /* robustlocks */
13809ce0d4aSRoger A. Faulkner NULL, /* robustlist */
13923a1cceaSRoger A. Faulkner NULL, /* progname */
140*dd7afb26SPatrick Mooney NULL, /* ub_comm_page */
1417c478bd9Sstevel@tonic-gate NULL, /* __tdb_bootstrap */
1427c478bd9Sstevel@tonic-gate { /* tdb */
1437c478bd9Sstevel@tonic-gate NULL, /* tdb_sync_addr_hash */
1447c478bd9Sstevel@tonic-gate 0, /* tdb_register_count */
1457c478bd9Sstevel@tonic-gate 0, /* tdb_hash_alloc_failed */
1467c478bd9Sstevel@tonic-gate NULL, /* tdb_sync_addr_free */
1477c478bd9Sstevel@tonic-gate NULL, /* tdb_sync_addr_last */
1487c478bd9Sstevel@tonic-gate 0, /* tdb_sync_alloc */
1497c478bd9Sstevel@tonic-gate { 0, 0 }, /* tdb_ev_global_mask */
1507c478bd9Sstevel@tonic-gate tdb_events, /* tdb_events array */
1517c478bd9Sstevel@tonic-gate },
1527c478bd9Sstevel@tonic-gate };
1537c478bd9Sstevel@tonic-gate
1547c478bd9Sstevel@tonic-gate /*
1557c478bd9Sstevel@tonic-gate * The weak version is known to libc_db and mdb.
1567c478bd9Sstevel@tonic-gate */
1577c478bd9Sstevel@tonic-gate #pragma weak _tdb_bootstrap = __tdb_bootstrap
1587c478bd9Sstevel@tonic-gate uberdata_t **__tdb_bootstrap = NULL;
1597c478bd9Sstevel@tonic-gate
1607c478bd9Sstevel@tonic-gate int thread_queue_fifo = 4;
1617c478bd9Sstevel@tonic-gate int thread_queue_dump = 0;
1627c478bd9Sstevel@tonic-gate int thread_cond_wait_defer = 0;
1637c478bd9Sstevel@tonic-gate int thread_error_detection = 0;
1647c478bd9Sstevel@tonic-gate int thread_async_safe = 0;
1657c478bd9Sstevel@tonic-gate int thread_stack_cache = 10;
1667c478bd9Sstevel@tonic-gate int thread_door_noreserve = 0;
1677c5714f6Sraf int thread_locks_misaligned = 0;
1687c478bd9Sstevel@tonic-gate
1697c478bd9Sstevel@tonic-gate static ulwp_t *ulwp_alloc(void);
1707c478bd9Sstevel@tonic-gate static void ulwp_free(ulwp_t *);
1717c478bd9Sstevel@tonic-gate
1727c478bd9Sstevel@tonic-gate /*
1737c478bd9Sstevel@tonic-gate * Insert the lwp into the hash table.
1747c478bd9Sstevel@tonic-gate */
1757c478bd9Sstevel@tonic-gate void
hash_in_unlocked(ulwp_t * ulwp,int ix,uberdata_t * udp)1767c478bd9Sstevel@tonic-gate hash_in_unlocked(ulwp_t *ulwp, int ix, uberdata_t *udp)
1777c478bd9Sstevel@tonic-gate {
1787c478bd9Sstevel@tonic-gate ulwp->ul_hash = udp->thr_hash_table[ix].hash_bucket;
1797c478bd9Sstevel@tonic-gate udp->thr_hash_table[ix].hash_bucket = ulwp;
1807c478bd9Sstevel@tonic-gate ulwp->ul_ix = ix;
1817c478bd9Sstevel@tonic-gate }
1827c478bd9Sstevel@tonic-gate
1837c478bd9Sstevel@tonic-gate void
hash_in(ulwp_t * ulwp,uberdata_t * udp)1847c478bd9Sstevel@tonic-gate hash_in(ulwp_t *ulwp, uberdata_t *udp)
1857c478bd9Sstevel@tonic-gate {
1867c478bd9Sstevel@tonic-gate int ix = TIDHASH(ulwp->ul_lwpid, udp);
1877c478bd9Sstevel@tonic-gate mutex_t *mp = &udp->thr_hash_table[ix].hash_lock;
1887c478bd9Sstevel@tonic-gate
1897c478bd9Sstevel@tonic-gate lmutex_lock(mp);
1907c478bd9Sstevel@tonic-gate hash_in_unlocked(ulwp, ix, udp);
1917c478bd9Sstevel@tonic-gate lmutex_unlock(mp);
1927c478bd9Sstevel@tonic-gate }
1937c478bd9Sstevel@tonic-gate
1947c478bd9Sstevel@tonic-gate /*
1957c478bd9Sstevel@tonic-gate * Delete the lwp from the hash table.
1967c478bd9Sstevel@tonic-gate */
1977c478bd9Sstevel@tonic-gate void
hash_out_unlocked(ulwp_t * ulwp,int ix,uberdata_t * udp)1987c478bd9Sstevel@tonic-gate hash_out_unlocked(ulwp_t *ulwp, int ix, uberdata_t *udp)
1997c478bd9Sstevel@tonic-gate {
2007c478bd9Sstevel@tonic-gate ulwp_t **ulwpp;
2017c478bd9Sstevel@tonic-gate
2027c478bd9Sstevel@tonic-gate for (ulwpp = &udp->thr_hash_table[ix].hash_bucket;
2037c478bd9Sstevel@tonic-gate ulwp != *ulwpp;
2047c478bd9Sstevel@tonic-gate ulwpp = &(*ulwpp)->ul_hash)
2057c478bd9Sstevel@tonic-gate ;
2067c478bd9Sstevel@tonic-gate *ulwpp = ulwp->ul_hash;
2077c478bd9Sstevel@tonic-gate ulwp->ul_hash = NULL;
2087c478bd9Sstevel@tonic-gate ulwp->ul_ix = -1;
2097c478bd9Sstevel@tonic-gate }
2107c478bd9Sstevel@tonic-gate
2117c478bd9Sstevel@tonic-gate void
hash_out(ulwp_t * ulwp,uberdata_t * udp)2127c478bd9Sstevel@tonic-gate hash_out(ulwp_t *ulwp, uberdata_t *udp)
2137c478bd9Sstevel@tonic-gate {
2147c478bd9Sstevel@tonic-gate int ix;
2157c478bd9Sstevel@tonic-gate
2167c478bd9Sstevel@tonic-gate if ((ix = ulwp->ul_ix) >= 0) {
2177c478bd9Sstevel@tonic-gate mutex_t *mp = &udp->thr_hash_table[ix].hash_lock;
2187c478bd9Sstevel@tonic-gate
2197c478bd9Sstevel@tonic-gate lmutex_lock(mp);
2207c478bd9Sstevel@tonic-gate hash_out_unlocked(ulwp, ix, udp);
2217c478bd9Sstevel@tonic-gate lmutex_unlock(mp);
2227c478bd9Sstevel@tonic-gate }
2237c478bd9Sstevel@tonic-gate }
2247c478bd9Sstevel@tonic-gate
225a574db85Sraf /*
226a574db85Sraf * Retain stack information for thread structures that are being recycled for
227a574db85Sraf * new threads. All other members of the thread structure should be zeroed.
228a574db85Sraf */
2297c478bd9Sstevel@tonic-gate static void
ulwp_clean(ulwp_t * ulwp)2307c478bd9Sstevel@tonic-gate ulwp_clean(ulwp_t *ulwp)
2317c478bd9Sstevel@tonic-gate {
232a574db85Sraf caddr_t stk = ulwp->ul_stk;
233a574db85Sraf size_t mapsiz = ulwp->ul_mapsiz;
234a574db85Sraf size_t guardsize = ulwp->ul_guardsize;
235a574db85Sraf uintptr_t stktop = ulwp->ul_stktop;
236a574db85Sraf size_t stksiz = ulwp->ul_stksiz;
237a574db85Sraf
2388cd45542Sraf (void) memset(ulwp, 0, sizeof (*ulwp));
239a574db85Sraf
240a574db85Sraf ulwp->ul_stk = stk;
241a574db85Sraf ulwp->ul_mapsiz = mapsiz;
242a574db85Sraf ulwp->ul_guardsize = guardsize;
243a574db85Sraf ulwp->ul_stktop = stktop;
244a574db85Sraf ulwp->ul_stksiz = stksiz;
2457c478bd9Sstevel@tonic-gate }
2467c478bd9Sstevel@tonic-gate
2477c478bd9Sstevel@tonic-gate static int stackprot;
2487c478bd9Sstevel@tonic-gate
2497c478bd9Sstevel@tonic-gate /*
2507c478bd9Sstevel@tonic-gate * Answer the question, "Is the lwp in question really dead?"
2517c478bd9Sstevel@tonic-gate * We must inquire of the operating system to be really sure
2527c478bd9Sstevel@tonic-gate * because the lwp may have called lwp_exit() but it has not
2537c478bd9Sstevel@tonic-gate * yet completed the exit.
2547c478bd9Sstevel@tonic-gate */
2557c478bd9Sstevel@tonic-gate static int
dead_and_buried(ulwp_t * ulwp)2567c478bd9Sstevel@tonic-gate dead_and_buried(ulwp_t *ulwp)
2577c478bd9Sstevel@tonic-gate {
2587c478bd9Sstevel@tonic-gate if (ulwp->ul_lwpid == (lwpid_t)(-1))
2597c478bd9Sstevel@tonic-gate return (1);
2607c478bd9Sstevel@tonic-gate if (ulwp->ul_dead && ulwp->ul_detached &&
2617257d1b4Sraf _lwp_kill(ulwp->ul_lwpid, 0) == ESRCH) {
2627c478bd9Sstevel@tonic-gate ulwp->ul_lwpid = (lwpid_t)(-1);
2637c478bd9Sstevel@tonic-gate return (1);
2647c478bd9Sstevel@tonic-gate }
2657c478bd9Sstevel@tonic-gate return (0);
2667c478bd9Sstevel@tonic-gate }
2677c478bd9Sstevel@tonic-gate
2687c478bd9Sstevel@tonic-gate /*
2697c478bd9Sstevel@tonic-gate * Attempt to keep the stack cache within the specified cache limit.
2707c478bd9Sstevel@tonic-gate */
2717c478bd9Sstevel@tonic-gate static void
trim_stack_cache(int cache_limit)2727c478bd9Sstevel@tonic-gate trim_stack_cache(int cache_limit)
2737c478bd9Sstevel@tonic-gate {
2747c478bd9Sstevel@tonic-gate ulwp_t *self = curthread;
2757c478bd9Sstevel@tonic-gate uberdata_t *udp = self->ul_uberdata;
2767c478bd9Sstevel@tonic-gate ulwp_t *prev = NULL;
2777c478bd9Sstevel@tonic-gate ulwp_t **ulwpp = &udp->lwp_stacks;
2787c478bd9Sstevel@tonic-gate ulwp_t *ulwp;
2797c478bd9Sstevel@tonic-gate
2807c478bd9Sstevel@tonic-gate ASSERT(udp->nthreads <= 1 || MUTEX_OWNED(&udp->link_lock, self));
2817c478bd9Sstevel@tonic-gate
2827c478bd9Sstevel@tonic-gate while (udp->nfreestack > cache_limit && (ulwp = *ulwpp) != NULL) {
2837c478bd9Sstevel@tonic-gate if (dead_and_buried(ulwp)) {
2847c478bd9Sstevel@tonic-gate *ulwpp = ulwp->ul_next;
2857c478bd9Sstevel@tonic-gate if (ulwp == udp->lwp_laststack)
2867c478bd9Sstevel@tonic-gate udp->lwp_laststack = prev;
2877c478bd9Sstevel@tonic-gate hash_out(ulwp, udp);
2887c478bd9Sstevel@tonic-gate udp->nfreestack--;
2898cd45542Sraf (void) munmap(ulwp->ul_stk, ulwp->ul_mapsiz);
2907c478bd9Sstevel@tonic-gate /*
2917c478bd9Sstevel@tonic-gate * Now put the free ulwp on the ulwp freelist.
2927c478bd9Sstevel@tonic-gate */
2937c478bd9Sstevel@tonic-gate ulwp->ul_mapsiz = 0;
2947c478bd9Sstevel@tonic-gate ulwp->ul_next = NULL;
2957c478bd9Sstevel@tonic-gate if (udp->ulwp_freelist == NULL)
2967c478bd9Sstevel@tonic-gate udp->ulwp_freelist = udp->ulwp_lastfree = ulwp;
2977c478bd9Sstevel@tonic-gate else {
2987c478bd9Sstevel@tonic-gate udp->ulwp_lastfree->ul_next = ulwp;
2997c478bd9Sstevel@tonic-gate udp->ulwp_lastfree = ulwp;
3007c478bd9Sstevel@tonic-gate }
3017c478bd9Sstevel@tonic-gate } else {
3027c478bd9Sstevel@tonic-gate prev = ulwp;
3037c478bd9Sstevel@tonic-gate ulwpp = &ulwp->ul_next;
3047c478bd9Sstevel@tonic-gate }
3057c478bd9Sstevel@tonic-gate }
3067c478bd9Sstevel@tonic-gate }
3077c478bd9Sstevel@tonic-gate
3087c478bd9Sstevel@tonic-gate /*
3097c478bd9Sstevel@tonic-gate * Find an unused stack of the requested size
3107c478bd9Sstevel@tonic-gate * or create a new stack of the requested size.
3117c478bd9Sstevel@tonic-gate * Return a pointer to the ulwp_t structure referring to the stack, or NULL.
3127c478bd9Sstevel@tonic-gate * thr_exit() stores 1 in the ul_dead member.
3137c478bd9Sstevel@tonic-gate * thr_join() stores -1 in the ul_lwpid member.
3147c478bd9Sstevel@tonic-gate */
3153db34912SRoger A. Faulkner static ulwp_t *
find_stack(size_t stksize,size_t guardsize)3167c478bd9Sstevel@tonic-gate find_stack(size_t stksize, size_t guardsize)
3177c478bd9Sstevel@tonic-gate {
31834709573Sraf static size_t pagesize = 0;
31934709573Sraf
3207c478bd9Sstevel@tonic-gate uberdata_t *udp = curthread->ul_uberdata;
3217c478bd9Sstevel@tonic-gate size_t mapsize;
3227c478bd9Sstevel@tonic-gate ulwp_t *prev;
3237c478bd9Sstevel@tonic-gate ulwp_t *ulwp;
3247c478bd9Sstevel@tonic-gate ulwp_t **ulwpp;
3257c478bd9Sstevel@tonic-gate void *stk;
3267c478bd9Sstevel@tonic-gate
3277c478bd9Sstevel@tonic-gate /*
3287c478bd9Sstevel@tonic-gate * The stack is allocated PROT_READ|PROT_WRITE|PROT_EXEC
3297c478bd9Sstevel@tonic-gate * unless overridden by the system's configuration.
3307c478bd9Sstevel@tonic-gate */
3317c478bd9Sstevel@tonic-gate if (stackprot == 0) { /* do this once */
3327c478bd9Sstevel@tonic-gate long lprot = _sysconf(_SC_STACK_PROT);
3337c478bd9Sstevel@tonic-gate if (lprot <= 0)
3347c478bd9Sstevel@tonic-gate lprot = (PROT_READ|PROT_WRITE|PROT_EXEC);
3357c478bd9Sstevel@tonic-gate stackprot = (int)lprot;
3367c478bd9Sstevel@tonic-gate }
33734709573Sraf if (pagesize == 0) /* do this once */
33834709573Sraf pagesize = _sysconf(_SC_PAGESIZE);
33934709573Sraf
3407c478bd9Sstevel@tonic-gate /*
3417c478bd9Sstevel@tonic-gate * One megabyte stacks by default, but subtract off
3427c478bd9Sstevel@tonic-gate * two pages for the system-created red zones.
3437c478bd9Sstevel@tonic-gate * Round up a non-zero stack size to a pagesize multiple.
3447c478bd9Sstevel@tonic-gate */
3457c478bd9Sstevel@tonic-gate if (stksize == 0)
34634709573Sraf stksize = DEFAULTSTACK - 2 * pagesize;
3477c478bd9Sstevel@tonic-gate else
34834709573Sraf stksize = ((stksize + pagesize - 1) & -pagesize);
3497c478bd9Sstevel@tonic-gate
3507c478bd9Sstevel@tonic-gate /*
3517c478bd9Sstevel@tonic-gate * Round up the mapping size to a multiple of pagesize.
3527c478bd9Sstevel@tonic-gate * Note: mmap() provides at least one page of red zone
3537c478bd9Sstevel@tonic-gate * so we deduct that from the value of guardsize.
3547c478bd9Sstevel@tonic-gate */
3557c478bd9Sstevel@tonic-gate if (guardsize != 0)
35634709573Sraf guardsize = ((guardsize + pagesize - 1) & -pagesize) - pagesize;
3577c478bd9Sstevel@tonic-gate mapsize = stksize + guardsize;
3587c478bd9Sstevel@tonic-gate
3597c478bd9Sstevel@tonic-gate lmutex_lock(&udp->link_lock);
3607c478bd9Sstevel@tonic-gate for (prev = NULL, ulwpp = &udp->lwp_stacks;
3617c478bd9Sstevel@tonic-gate (ulwp = *ulwpp) != NULL;
3627c478bd9Sstevel@tonic-gate prev = ulwp, ulwpp = &ulwp->ul_next) {
3637c478bd9Sstevel@tonic-gate if (ulwp->ul_mapsiz == mapsize &&
3647c478bd9Sstevel@tonic-gate ulwp->ul_guardsize == guardsize &&
3657c478bd9Sstevel@tonic-gate dead_and_buried(ulwp)) {
3667c478bd9Sstevel@tonic-gate /*
3677c478bd9Sstevel@tonic-gate * The previous lwp is gone; reuse the stack.
3687c478bd9Sstevel@tonic-gate * Remove the ulwp from the stack list.
3697c478bd9Sstevel@tonic-gate */
3707c478bd9Sstevel@tonic-gate *ulwpp = ulwp->ul_next;
3717c478bd9Sstevel@tonic-gate ulwp->ul_next = NULL;
3727c478bd9Sstevel@tonic-gate if (ulwp == udp->lwp_laststack)
3737c478bd9Sstevel@tonic-gate udp->lwp_laststack = prev;
3747c478bd9Sstevel@tonic-gate hash_out(ulwp, udp);
3757c478bd9Sstevel@tonic-gate udp->nfreestack--;
3767c478bd9Sstevel@tonic-gate lmutex_unlock(&udp->link_lock);
3777c478bd9Sstevel@tonic-gate ulwp_clean(ulwp);
3787c478bd9Sstevel@tonic-gate return (ulwp);
3797c478bd9Sstevel@tonic-gate }
3807c478bd9Sstevel@tonic-gate }
3817c478bd9Sstevel@tonic-gate
3827c478bd9Sstevel@tonic-gate /*
3837c478bd9Sstevel@tonic-gate * None of the cached stacks matched our mapping size.
3847c478bd9Sstevel@tonic-gate * Reduce the stack cache to get rid of possibly
3857c478bd9Sstevel@tonic-gate * very old stacks that will never be reused.
3867c478bd9Sstevel@tonic-gate */
3877c478bd9Sstevel@tonic-gate if (udp->nfreestack > udp->thread_stack_cache)
3887c478bd9Sstevel@tonic-gate trim_stack_cache(udp->thread_stack_cache);
3897c478bd9Sstevel@tonic-gate else if (udp->nfreestack > 0)
3907c478bd9Sstevel@tonic-gate trim_stack_cache(udp->nfreestack - 1);
3917c478bd9Sstevel@tonic-gate lmutex_unlock(&udp->link_lock);
3927c478bd9Sstevel@tonic-gate
3937c478bd9Sstevel@tonic-gate /*
3947c478bd9Sstevel@tonic-gate * Create a new stack.
3957c478bd9Sstevel@tonic-gate */
3968cd45542Sraf if ((stk = mmap(NULL, mapsize, stackprot,
3977c478bd9Sstevel@tonic-gate MAP_PRIVATE|MAP_NORESERVE|MAP_ANON, -1, (off_t)0)) != MAP_FAILED) {
3987c478bd9Sstevel@tonic-gate /*
3997c478bd9Sstevel@tonic-gate * We have allocated our stack. Now allocate the ulwp.
4007c478bd9Sstevel@tonic-gate */
4017c478bd9Sstevel@tonic-gate ulwp = ulwp_alloc();
4027c478bd9Sstevel@tonic-gate if (ulwp == NULL)
4038cd45542Sraf (void) munmap(stk, mapsize);
4047c478bd9Sstevel@tonic-gate else {
4057c478bd9Sstevel@tonic-gate ulwp->ul_stk = stk;
4067c478bd9Sstevel@tonic-gate ulwp->ul_mapsiz = mapsize;
4077c478bd9Sstevel@tonic-gate ulwp->ul_guardsize = guardsize;
4087c478bd9Sstevel@tonic-gate ulwp->ul_stktop = (uintptr_t)stk + mapsize;
4097c478bd9Sstevel@tonic-gate ulwp->ul_stksiz = stksize;
4107c478bd9Sstevel@tonic-gate if (guardsize) /* protect the extra red zone */
4118cd45542Sraf (void) mprotect(stk, guardsize, PROT_NONE);
4127c478bd9Sstevel@tonic-gate }
4137c478bd9Sstevel@tonic-gate }
4147c478bd9Sstevel@tonic-gate return (ulwp);
4157c478bd9Sstevel@tonic-gate }
4167c478bd9Sstevel@tonic-gate
4177c478bd9Sstevel@tonic-gate /*
4187c478bd9Sstevel@tonic-gate * Get a ulwp_t structure from the free list or allocate a new one.
4197c478bd9Sstevel@tonic-gate * Such ulwp_t's do not have a stack allocated by the library.
4207c478bd9Sstevel@tonic-gate */
4217c478bd9Sstevel@tonic-gate static ulwp_t *
ulwp_alloc(void)4227c478bd9Sstevel@tonic-gate ulwp_alloc(void)
4237c478bd9Sstevel@tonic-gate {
4247c478bd9Sstevel@tonic-gate ulwp_t *self = curthread;
4257c478bd9Sstevel@tonic-gate uberdata_t *udp = self->ul_uberdata;
4267c478bd9Sstevel@tonic-gate size_t tls_size;
4277c478bd9Sstevel@tonic-gate ulwp_t *prev;
4287c478bd9Sstevel@tonic-gate ulwp_t *ulwp;
4297c478bd9Sstevel@tonic-gate ulwp_t **ulwpp;
4307c478bd9Sstevel@tonic-gate caddr_t data;
4317c478bd9Sstevel@tonic-gate
4327c478bd9Sstevel@tonic-gate lmutex_lock(&udp->link_lock);
4337c478bd9Sstevel@tonic-gate for (prev = NULL, ulwpp = &udp->ulwp_freelist;
4347c478bd9Sstevel@tonic-gate (ulwp = *ulwpp) != NULL;
4357c478bd9Sstevel@tonic-gate prev = ulwp, ulwpp = &ulwp->ul_next) {
4367c478bd9Sstevel@tonic-gate if (dead_and_buried(ulwp)) {
4377c478bd9Sstevel@tonic-gate *ulwpp = ulwp->ul_next;
4387c478bd9Sstevel@tonic-gate ulwp->ul_next = NULL;
4397c478bd9Sstevel@tonic-gate if (ulwp == udp->ulwp_lastfree)
4407c478bd9Sstevel@tonic-gate udp->ulwp_lastfree = prev;
4417c478bd9Sstevel@tonic-gate hash_out(ulwp, udp);
4427c478bd9Sstevel@tonic-gate lmutex_unlock(&udp->link_lock);
4437c478bd9Sstevel@tonic-gate ulwp_clean(ulwp);
4447c478bd9Sstevel@tonic-gate return (ulwp);
4457c478bd9Sstevel@tonic-gate }
4467c478bd9Sstevel@tonic-gate }
4477c478bd9Sstevel@tonic-gate lmutex_unlock(&udp->link_lock);
4487c478bd9Sstevel@tonic-gate
4497c478bd9Sstevel@tonic-gate tls_size = roundup64(udp->tls_metadata.static_tls.tls_size);
4507c478bd9Sstevel@tonic-gate data = lmalloc(sizeof (*ulwp) + tls_size);
4517c478bd9Sstevel@tonic-gate if (data != NULL) {
4527c478bd9Sstevel@tonic-gate /* LINTED pointer cast may result in improper alignment */
4537c478bd9Sstevel@tonic-gate ulwp = (ulwp_t *)(data + tls_size);
4547c478bd9Sstevel@tonic-gate }
4557c478bd9Sstevel@tonic-gate return (ulwp);
4567c478bd9Sstevel@tonic-gate }
4577c478bd9Sstevel@tonic-gate
4587c478bd9Sstevel@tonic-gate /*
4597c478bd9Sstevel@tonic-gate * Free a ulwp structure.
4607c478bd9Sstevel@tonic-gate * If there is an associated stack, put it on the stack list and
4617c478bd9Sstevel@tonic-gate * munmap() previously freed stacks up to the residual cache limit.
4627c478bd9Sstevel@tonic-gate * Else put it on the ulwp free list and never call lfree() on it.
4637c478bd9Sstevel@tonic-gate */
4647c478bd9Sstevel@tonic-gate static void
ulwp_free(ulwp_t * ulwp)4657c478bd9Sstevel@tonic-gate ulwp_free(ulwp_t *ulwp)
4667c478bd9Sstevel@tonic-gate {
4677c478bd9Sstevel@tonic-gate uberdata_t *udp = curthread->ul_uberdata;
4687c478bd9Sstevel@tonic-gate
4697c478bd9Sstevel@tonic-gate ASSERT(udp->nthreads <= 1 || MUTEX_OWNED(&udp->link_lock, curthread));
4707c478bd9Sstevel@tonic-gate ulwp->ul_next = NULL;
4717c478bd9Sstevel@tonic-gate if (ulwp == udp->ulwp_one) /* don't reuse the primoridal stack */
4727c478bd9Sstevel@tonic-gate /*EMPTY*/;
4737c478bd9Sstevel@tonic-gate else if (ulwp->ul_mapsiz != 0) {
4747c478bd9Sstevel@tonic-gate if (udp->lwp_stacks == NULL)
4757c478bd9Sstevel@tonic-gate udp->lwp_stacks = udp->lwp_laststack = ulwp;
4767c478bd9Sstevel@tonic-gate else {
4777c478bd9Sstevel@tonic-gate udp->lwp_laststack->ul_next = ulwp;
4787c478bd9Sstevel@tonic-gate udp->lwp_laststack = ulwp;
4797c478bd9Sstevel@tonic-gate }
4807c478bd9Sstevel@tonic-gate if (++udp->nfreestack > udp->thread_stack_cache)
4817c478bd9Sstevel@tonic-gate trim_stack_cache(udp->thread_stack_cache);
4827c478bd9Sstevel@tonic-gate } else {
4837c478bd9Sstevel@tonic-gate if (udp->ulwp_freelist == NULL)
4847c478bd9Sstevel@tonic-gate udp->ulwp_freelist = udp->ulwp_lastfree = ulwp;
4857c478bd9Sstevel@tonic-gate else {
4867c478bd9Sstevel@tonic-gate udp->ulwp_lastfree->ul_next = ulwp;
4877c478bd9Sstevel@tonic-gate udp->ulwp_lastfree = ulwp;
4887c478bd9Sstevel@tonic-gate }
4897c478bd9Sstevel@tonic-gate }
4907c478bd9Sstevel@tonic-gate }
4917c478bd9Sstevel@tonic-gate
4927c478bd9Sstevel@tonic-gate /*
4937c478bd9Sstevel@tonic-gate * Find a named lwp and return a pointer to its hash list location.
4947c478bd9Sstevel@tonic-gate * On success, returns with the hash lock held.
4957c478bd9Sstevel@tonic-gate */
4967c478bd9Sstevel@tonic-gate ulwp_t **
find_lwpp(thread_t tid)4977c478bd9Sstevel@tonic-gate find_lwpp(thread_t tid)
4987c478bd9Sstevel@tonic-gate {
4997c478bd9Sstevel@tonic-gate uberdata_t *udp = curthread->ul_uberdata;
5007c478bd9Sstevel@tonic-gate int ix = TIDHASH(tid, udp);
5017c478bd9Sstevel@tonic-gate mutex_t *mp = &udp->thr_hash_table[ix].hash_lock;
5027c478bd9Sstevel@tonic-gate ulwp_t *ulwp;
5037c478bd9Sstevel@tonic-gate ulwp_t **ulwpp;
5047c478bd9Sstevel@tonic-gate
5057c478bd9Sstevel@tonic-gate if (tid == 0)
5067c478bd9Sstevel@tonic-gate return (NULL);
5077c478bd9Sstevel@tonic-gate
5087c478bd9Sstevel@tonic-gate lmutex_lock(mp);
5097c478bd9Sstevel@tonic-gate for (ulwpp = &udp->thr_hash_table[ix].hash_bucket;
5107c478bd9Sstevel@tonic-gate (ulwp = *ulwpp) != NULL;
5117c478bd9Sstevel@tonic-gate ulwpp = &ulwp->ul_hash) {
5127c478bd9Sstevel@tonic-gate if (ulwp->ul_lwpid == tid)
5137c478bd9Sstevel@tonic-gate return (ulwpp);
5147c478bd9Sstevel@tonic-gate }
5157c478bd9Sstevel@tonic-gate lmutex_unlock(mp);
5167c478bd9Sstevel@tonic-gate return (NULL);
5177c478bd9Sstevel@tonic-gate }
5187c478bd9Sstevel@tonic-gate
5197c478bd9Sstevel@tonic-gate /*
5207c478bd9Sstevel@tonic-gate * Wake up all lwps waiting on this lwp for some reason.
5217c478bd9Sstevel@tonic-gate */
5227c478bd9Sstevel@tonic-gate void
ulwp_broadcast(ulwp_t * ulwp)5237c478bd9Sstevel@tonic-gate ulwp_broadcast(ulwp_t *ulwp)
5247c478bd9Sstevel@tonic-gate {
5257c478bd9Sstevel@tonic-gate ulwp_t *self = curthread;
5267c478bd9Sstevel@tonic-gate uberdata_t *udp = self->ul_uberdata;
5277c478bd9Sstevel@tonic-gate
5287c478bd9Sstevel@tonic-gate ASSERT(MUTEX_OWNED(ulwp_mutex(ulwp, udp), self));
5297257d1b4Sraf (void) cond_broadcast(ulwp_condvar(ulwp, udp));
5307c478bd9Sstevel@tonic-gate }
5317c478bd9Sstevel@tonic-gate
5327c478bd9Sstevel@tonic-gate /*
5337c478bd9Sstevel@tonic-gate * Find a named lwp and return a pointer to it.
5347c478bd9Sstevel@tonic-gate * Returns with the hash lock held.
5357c478bd9Sstevel@tonic-gate */
5367c478bd9Sstevel@tonic-gate ulwp_t *
find_lwp(thread_t tid)5377c478bd9Sstevel@tonic-gate find_lwp(thread_t tid)
5387c478bd9Sstevel@tonic-gate {
5397c478bd9Sstevel@tonic-gate ulwp_t *self = curthread;
5407c478bd9Sstevel@tonic-gate uberdata_t *udp = self->ul_uberdata;
5417c478bd9Sstevel@tonic-gate ulwp_t *ulwp = NULL;
5427c478bd9Sstevel@tonic-gate ulwp_t **ulwpp;
5437c478bd9Sstevel@tonic-gate
5447c478bd9Sstevel@tonic-gate if (self->ul_lwpid == tid) {
5457c478bd9Sstevel@tonic-gate ulwp = self;
5467c478bd9Sstevel@tonic-gate ulwp_lock(ulwp, udp);
5477c478bd9Sstevel@tonic-gate } else if ((ulwpp = find_lwpp(tid)) != NULL) {
5487c478bd9Sstevel@tonic-gate ulwp = *ulwpp;
5497c478bd9Sstevel@tonic-gate }
5507c478bd9Sstevel@tonic-gate
5517c478bd9Sstevel@tonic-gate if (ulwp && ulwp->ul_dead) {
5527c478bd9Sstevel@tonic-gate ulwp_unlock(ulwp, udp);
5537c478bd9Sstevel@tonic-gate ulwp = NULL;
5547c478bd9Sstevel@tonic-gate }
5557c478bd9Sstevel@tonic-gate
5567c478bd9Sstevel@tonic-gate return (ulwp);
5577c478bd9Sstevel@tonic-gate }
5587c478bd9Sstevel@tonic-gate
5597c478bd9Sstevel@tonic-gate int
_thrp_create(void * stk,size_t stksize,void * (* func)(void *),void * arg,long flags,thread_t * new_thread,size_t guardsize)5607c478bd9Sstevel@tonic-gate _thrp_create(void *stk, size_t stksize, void *(*func)(void *), void *arg,
561d4204c85Sraf long flags, thread_t *new_thread, size_t guardsize)
5627c478bd9Sstevel@tonic-gate {
5637c478bd9Sstevel@tonic-gate ulwp_t *self = curthread;
5647c478bd9Sstevel@tonic-gate uberdata_t *udp = self->ul_uberdata;
5657c478bd9Sstevel@tonic-gate ucontext_t uc;
5667c478bd9Sstevel@tonic-gate uint_t lwp_flags;
5677c478bd9Sstevel@tonic-gate thread_t tid;
568373d25a2SRoger A. Faulkner int error;
5697c478bd9Sstevel@tonic-gate ulwp_t *ulwp;
5707c478bd9Sstevel@tonic-gate
5717c478bd9Sstevel@tonic-gate /*
5727c478bd9Sstevel@tonic-gate * Enforce the restriction of not creating any threads
5737c478bd9Sstevel@tonic-gate * until the primary link map has been initialized.
5747c478bd9Sstevel@tonic-gate * Also, disallow thread creation to a child of vfork().
5757c478bd9Sstevel@tonic-gate */
5767c478bd9Sstevel@tonic-gate if (!self->ul_primarymap || self->ul_vfork)
5777c478bd9Sstevel@tonic-gate return (ENOTSUP);
5787c478bd9Sstevel@tonic-gate
5797c478bd9Sstevel@tonic-gate if (udp->hash_size == 1)
5807c478bd9Sstevel@tonic-gate finish_init();
5817c478bd9Sstevel@tonic-gate
582d4204c85Sraf if ((stk || stksize) && stksize < MINSTACK)
5837c478bd9Sstevel@tonic-gate return (EINVAL);
5847c478bd9Sstevel@tonic-gate
5857c478bd9Sstevel@tonic-gate if (stk == NULL) {
5867c478bd9Sstevel@tonic-gate if ((ulwp = find_stack(stksize, guardsize)) == NULL)
5877c478bd9Sstevel@tonic-gate return (ENOMEM);
5887c478bd9Sstevel@tonic-gate stksize = ulwp->ul_mapsiz - ulwp->ul_guardsize;
5897c478bd9Sstevel@tonic-gate } else {
5907c478bd9Sstevel@tonic-gate /* initialize the private stack */
5917c478bd9Sstevel@tonic-gate if ((ulwp = ulwp_alloc()) == NULL)
5927c478bd9Sstevel@tonic-gate return (ENOMEM);
5937c478bd9Sstevel@tonic-gate ulwp->ul_stk = stk;
5947c478bd9Sstevel@tonic-gate ulwp->ul_stktop = (uintptr_t)stk + stksize;
5957c478bd9Sstevel@tonic-gate ulwp->ul_stksiz = stksize;
5967c478bd9Sstevel@tonic-gate }
5973db34912SRoger A. Faulkner /* ulwp is not in the hash table; make sure hash_out() doesn't fail */
5983db34912SRoger A. Faulkner ulwp->ul_ix = -1;
5997c478bd9Sstevel@tonic-gate ulwp->ul_errnop = &ulwp->ul_errno;
6007c478bd9Sstevel@tonic-gate
6017c478bd9Sstevel@tonic-gate lwp_flags = LWP_SUSPENDED;
6027c478bd9Sstevel@tonic-gate if (flags & (THR_DETACHED|THR_DAEMON)) {
6037c478bd9Sstevel@tonic-gate flags |= THR_DETACHED;
6047c478bd9Sstevel@tonic-gate lwp_flags |= LWP_DETACHED;
6057c478bd9Sstevel@tonic-gate }
6067c478bd9Sstevel@tonic-gate if (flags & THR_DAEMON)
6077c478bd9Sstevel@tonic-gate lwp_flags |= LWP_DAEMON;
6087c478bd9Sstevel@tonic-gate
6097257d1b4Sraf /* creating a thread: enforce mt-correctness in mutex_lock() */
6107c478bd9Sstevel@tonic-gate self->ul_async_safe = 1;
6117c478bd9Sstevel@tonic-gate
6127c478bd9Sstevel@tonic-gate /* per-thread copies of global variables, for speed */
6137c478bd9Sstevel@tonic-gate ulwp->ul_queue_fifo = self->ul_queue_fifo;
6147c478bd9Sstevel@tonic-gate ulwp->ul_cond_wait_defer = self->ul_cond_wait_defer;
6157c478bd9Sstevel@tonic-gate ulwp->ul_error_detection = self->ul_error_detection;
6167c478bd9Sstevel@tonic-gate ulwp->ul_async_safe = self->ul_async_safe;
6177c478bd9Sstevel@tonic-gate ulwp->ul_max_spinners = self->ul_max_spinners;
6187c478bd9Sstevel@tonic-gate ulwp->ul_adaptive_spin = self->ul_adaptive_spin;
6197c478bd9Sstevel@tonic-gate ulwp->ul_queue_spin = self->ul_queue_spin;
6207c478bd9Sstevel@tonic-gate ulwp->ul_door_noreserve = self->ul_door_noreserve;
6217c5714f6Sraf ulwp->ul_misaligned = self->ul_misaligned;
6227c478bd9Sstevel@tonic-gate
623d4204c85Sraf /* new thread inherits creating thread's scheduling parameters */
624d4204c85Sraf ulwp->ul_policy = self->ul_policy;
625d4204c85Sraf ulwp->ul_pri = (self->ul_epri? self->ul_epri : self->ul_pri);
626d4204c85Sraf ulwp->ul_cid = self->ul_cid;
627d4204c85Sraf ulwp->ul_rtclassid = self->ul_rtclassid;
628d4204c85Sraf
6297c478bd9Sstevel@tonic-gate ulwp->ul_primarymap = self->ul_primarymap;
6307c478bd9Sstevel@tonic-gate ulwp->ul_self = ulwp;
6317c478bd9Sstevel@tonic-gate ulwp->ul_uberdata = udp;
6327c478bd9Sstevel@tonic-gate
6337c478bd9Sstevel@tonic-gate /* debugger support */
6347c478bd9Sstevel@tonic-gate ulwp->ul_usropts = flags;
6357c478bd9Sstevel@tonic-gate
6367c478bd9Sstevel@tonic-gate #ifdef __sparc
6377c478bd9Sstevel@tonic-gate /*
6387c478bd9Sstevel@tonic-gate * We cache several instructions in the thread structure for use
6397c478bd9Sstevel@tonic-gate * by the fasttrap DTrace provider. When changing this, read the
6407c478bd9Sstevel@tonic-gate * comment in fasttrap.h for the all the other places that must
6417c478bd9Sstevel@tonic-gate * be changed.
6427c478bd9Sstevel@tonic-gate */
6437c478bd9Sstevel@tonic-gate ulwp->ul_dsave = 0x9de04000; /* save %g1, %g0, %sp */
6447c478bd9Sstevel@tonic-gate ulwp->ul_drestore = 0x81e80000; /* restore %g0, %g0, %g0 */
6457c478bd9Sstevel@tonic-gate ulwp->ul_dftret = 0x91d0203a; /* ta 0x3a */
6467c478bd9Sstevel@tonic-gate ulwp->ul_dreturn = 0x81ca0000; /* return %o0 */
6477c478bd9Sstevel@tonic-gate #endif
6487c478bd9Sstevel@tonic-gate
6497c478bd9Sstevel@tonic-gate ulwp->ul_startpc = func;
6507c478bd9Sstevel@tonic-gate ulwp->ul_startarg = arg;
6517c478bd9Sstevel@tonic-gate _fpinherit(ulwp);
6527c478bd9Sstevel@tonic-gate /*
6537c478bd9Sstevel@tonic-gate * Defer signals on the new thread until its TLS constructors
6547257d1b4Sraf * have been called. _thrp_setup() will call sigon() after
6557c478bd9Sstevel@tonic-gate * it has called tls_setup().
6567c478bd9Sstevel@tonic-gate */
6577c478bd9Sstevel@tonic-gate ulwp->ul_sigdefer = 1;
6587c478bd9Sstevel@tonic-gate
659373d25a2SRoger A. Faulkner error = setup_context(&uc, _thrp_setup, ulwp,
660373d25a2SRoger A. Faulkner (caddr_t)ulwp->ul_stk + ulwp->ul_guardsize, stksize);
661373d25a2SRoger A. Faulkner if (error != 0 && stk != NULL) /* inaccessible stack */
662373d25a2SRoger A. Faulkner error = EFAULT;
6637c478bd9Sstevel@tonic-gate
6647c478bd9Sstevel@tonic-gate /*
6657c478bd9Sstevel@tonic-gate * Call enter_critical() to avoid being suspended until we
6667c478bd9Sstevel@tonic-gate * have linked the new thread into the proper lists.
6677c478bd9Sstevel@tonic-gate * This is necessary because forkall() and fork1() must
6687c478bd9Sstevel@tonic-gate * suspend all threads and they must see a complete list.
6697c478bd9Sstevel@tonic-gate */
6707c478bd9Sstevel@tonic-gate enter_critical(self);
6717c478bd9Sstevel@tonic-gate uc.uc_sigmask = ulwp->ul_sigmask = self->ul_sigmask;
6727c478bd9Sstevel@tonic-gate if (error != 0 ||
6737c478bd9Sstevel@tonic-gate (error = __lwp_create(&uc, lwp_flags, &tid)) != 0) {
6747c478bd9Sstevel@tonic-gate exit_critical(self);
6757c478bd9Sstevel@tonic-gate ulwp->ul_lwpid = (lwpid_t)(-1);
6767c478bd9Sstevel@tonic-gate ulwp->ul_dead = 1;
6777c478bd9Sstevel@tonic-gate ulwp->ul_detached = 1;
6787c478bd9Sstevel@tonic-gate lmutex_lock(&udp->link_lock);
6797c478bd9Sstevel@tonic-gate ulwp_free(ulwp);
6807c478bd9Sstevel@tonic-gate lmutex_unlock(&udp->link_lock);
6817c478bd9Sstevel@tonic-gate return (error);
6827c478bd9Sstevel@tonic-gate }
6837c478bd9Sstevel@tonic-gate self->ul_nocancel = 0; /* cancellation is now possible */
6847c478bd9Sstevel@tonic-gate udp->uberflags.uf_mt = 1;
6857c478bd9Sstevel@tonic-gate if (new_thread)
6867c478bd9Sstevel@tonic-gate *new_thread = tid;
6877c478bd9Sstevel@tonic-gate if (flags & THR_DETACHED)
6887c478bd9Sstevel@tonic-gate ulwp->ul_detached = 1;
6897c478bd9Sstevel@tonic-gate ulwp->ul_lwpid = tid;
6907c478bd9Sstevel@tonic-gate ulwp->ul_stop = TSTP_REGULAR;
69136319254Sraf if (flags & THR_SUSPENDED)
6927c478bd9Sstevel@tonic-gate ulwp->ul_created = 1;
6937c478bd9Sstevel@tonic-gate
6947c478bd9Sstevel@tonic-gate lmutex_lock(&udp->link_lock);
6957c478bd9Sstevel@tonic-gate ulwp->ul_forw = udp->all_lwps;
6967c478bd9Sstevel@tonic-gate ulwp->ul_back = udp->all_lwps->ul_back;
6977c478bd9Sstevel@tonic-gate ulwp->ul_back->ul_forw = ulwp;
6987c478bd9Sstevel@tonic-gate ulwp->ul_forw->ul_back = ulwp;
6997c478bd9Sstevel@tonic-gate hash_in(ulwp, udp);
7007c478bd9Sstevel@tonic-gate udp->nthreads++;
7017c478bd9Sstevel@tonic-gate if (flags & THR_DAEMON)
7027c478bd9Sstevel@tonic-gate udp->ndaemons++;
7037c478bd9Sstevel@tonic-gate if (flags & THR_NEW_LWP)
7047c478bd9Sstevel@tonic-gate thr_concurrency++;
7051d2738a5Sraf __libc_threaded = 1; /* inform stdio */
7067c478bd9Sstevel@tonic-gate lmutex_unlock(&udp->link_lock);
7077c478bd9Sstevel@tonic-gate
7087c478bd9Sstevel@tonic-gate if (__td_event_report(self, TD_CREATE, udp)) {
7097c478bd9Sstevel@tonic-gate self->ul_td_evbuf.eventnum = TD_CREATE;
7107c478bd9Sstevel@tonic-gate self->ul_td_evbuf.eventdata = (void *)(uintptr_t)tid;
7117c478bd9Sstevel@tonic-gate tdb_event(TD_CREATE, udp);
7127c478bd9Sstevel@tonic-gate }
7137c478bd9Sstevel@tonic-gate
7147c478bd9Sstevel@tonic-gate exit_critical(self);
71536319254Sraf
71636319254Sraf if (!(flags & THR_SUSPENDED))
71736319254Sraf (void) _thrp_continue(tid, TSTP_REGULAR);
71836319254Sraf
7197c478bd9Sstevel@tonic-gate return (0);
7207c478bd9Sstevel@tonic-gate }
7217c478bd9Sstevel@tonic-gate
7227c478bd9Sstevel@tonic-gate int
thr_create(void * stk,size_t stksize,void * (* func)(void *),void * arg,long flags,thread_t * new_thread)7237257d1b4Sraf thr_create(void *stk, size_t stksize, void *(*func)(void *), void *arg,
7247c478bd9Sstevel@tonic-gate long flags, thread_t *new_thread)
7257c478bd9Sstevel@tonic-gate {
726d4204c85Sraf return (_thrp_create(stk, stksize, func, arg, flags, new_thread, 0));
7277c478bd9Sstevel@tonic-gate }
7287c478bd9Sstevel@tonic-gate
7297c478bd9Sstevel@tonic-gate /*
7307c478bd9Sstevel@tonic-gate * A special cancellation cleanup hook for DCE.
7317c478bd9Sstevel@tonic-gate * cleanuphndlr, when it is not NULL, will contain a callback
7327c478bd9Sstevel@tonic-gate * function to be called before a thread is terminated in
7337257d1b4Sraf * thr_exit() as a result of being cancelled.
7347c478bd9Sstevel@tonic-gate */
7357c478bd9Sstevel@tonic-gate static void (*cleanuphndlr)(void) = NULL;
7367c478bd9Sstevel@tonic-gate
7377c478bd9Sstevel@tonic-gate /*
7387c478bd9Sstevel@tonic-gate * _pthread_setcleanupinit: sets the cleanup hook.
7397c478bd9Sstevel@tonic-gate */
7407c478bd9Sstevel@tonic-gate int
_pthread_setcleanupinit(void (* func)(void))7417c478bd9Sstevel@tonic-gate _pthread_setcleanupinit(void (*func)(void))
7427c478bd9Sstevel@tonic-gate {
7437c478bd9Sstevel@tonic-gate cleanuphndlr = func;
7447c478bd9Sstevel@tonic-gate return (0);
7457c478bd9Sstevel@tonic-gate }
7467c478bd9Sstevel@tonic-gate
7477c478bd9Sstevel@tonic-gate void
_thrp_exit()7487c478bd9Sstevel@tonic-gate _thrp_exit()
7497c478bd9Sstevel@tonic-gate {
7507c478bd9Sstevel@tonic-gate ulwp_t *self = curthread;
7517c478bd9Sstevel@tonic-gate uberdata_t *udp = self->ul_uberdata;
7527c478bd9Sstevel@tonic-gate ulwp_t *replace = NULL;
7537c478bd9Sstevel@tonic-gate
7547c478bd9Sstevel@tonic-gate if (__td_event_report(self, TD_DEATH, udp)) {
7557c478bd9Sstevel@tonic-gate self->ul_td_evbuf.eventnum = TD_DEATH;
7567c478bd9Sstevel@tonic-gate tdb_event(TD_DEATH, udp);
7577c478bd9Sstevel@tonic-gate }
7587c478bd9Sstevel@tonic-gate
7597c478bd9Sstevel@tonic-gate ASSERT(self->ul_sigdefer != 0);
7607c478bd9Sstevel@tonic-gate
7617c478bd9Sstevel@tonic-gate lmutex_lock(&udp->link_lock);
7627c478bd9Sstevel@tonic-gate udp->nthreads--;
7637c478bd9Sstevel@tonic-gate if (self->ul_usropts & THR_NEW_LWP)
7647c478bd9Sstevel@tonic-gate thr_concurrency--;
7657c478bd9Sstevel@tonic-gate if (self->ul_usropts & THR_DAEMON)
7667c478bd9Sstevel@tonic-gate udp->ndaemons--;
7677c478bd9Sstevel@tonic-gate else if (udp->nthreads == udp->ndaemons) {
7687c478bd9Sstevel@tonic-gate /*
7697c478bd9Sstevel@tonic-gate * We are the last non-daemon thread exiting.
7707c478bd9Sstevel@tonic-gate * Exit the process. We retain our TSD and TLS so
7717c478bd9Sstevel@tonic-gate * that atexit() application functions can use them.
7727c478bd9Sstevel@tonic-gate */
7737c478bd9Sstevel@tonic-gate lmutex_unlock(&udp->link_lock);
7747c478bd9Sstevel@tonic-gate exit(0);
7757c478bd9Sstevel@tonic-gate thr_panic("_thrp_exit(): exit(0) returned");
7767c478bd9Sstevel@tonic-gate }
7777c478bd9Sstevel@tonic-gate lmutex_unlock(&udp->link_lock);
7787c478bd9Sstevel@tonic-gate
779f422aba8SYouzhong Yang /*
780f422aba8SYouzhong Yang * tsd_exit() may call its destructor free(), thus depending on
781f422aba8SYouzhong Yang * tmem, therefore tmem_exit() needs to be called after tsd_exit()
782f422aba8SYouzhong Yang * and tls_exit().
783f422aba8SYouzhong Yang */
7847c478bd9Sstevel@tonic-gate tsd_exit(); /* deallocate thread-specific data */
7857c478bd9Sstevel@tonic-gate tls_exit(); /* deallocate thread-local storage */
786f422aba8SYouzhong Yang tmem_exit(); /* deallocate tmem allocations */
787883492d5Sraf heldlock_exit(); /* deal with left-over held locks */
7887c478bd9Sstevel@tonic-gate
7897c478bd9Sstevel@tonic-gate /* block all signals to finish exiting */
7907c478bd9Sstevel@tonic-gate block_all_signals(self);
7917c478bd9Sstevel@tonic-gate /* also prevent ourself from being suspended */
7927c478bd9Sstevel@tonic-gate enter_critical(self);
7937c478bd9Sstevel@tonic-gate rwl_free(self);
7947c478bd9Sstevel@tonic-gate lmutex_lock(&udp->link_lock);
7957c478bd9Sstevel@tonic-gate ulwp_free(self);
7967c478bd9Sstevel@tonic-gate (void) ulwp_lock(self, udp);
7977c478bd9Sstevel@tonic-gate
7987c478bd9Sstevel@tonic-gate if (self->ul_mapsiz && !self->ul_detached) {
7997c478bd9Sstevel@tonic-gate /*
8007c478bd9Sstevel@tonic-gate * We want to free the stack for reuse but must keep
8017c478bd9Sstevel@tonic-gate * the ulwp_t struct for the benefit of thr_join().
8027c478bd9Sstevel@tonic-gate * For this purpose we allocate a replacement ulwp_t.
8037c478bd9Sstevel@tonic-gate */
8047c478bd9Sstevel@tonic-gate if ((replace = udp->ulwp_replace_free) == NULL)
8057c478bd9Sstevel@tonic-gate replace = lmalloc(REPLACEMENT_SIZE);
8067c478bd9Sstevel@tonic-gate else if ((udp->ulwp_replace_free = replace->ul_next) == NULL)
8077c478bd9Sstevel@tonic-gate udp->ulwp_replace_last = NULL;
8087c478bd9Sstevel@tonic-gate }
8097c478bd9Sstevel@tonic-gate
8107c478bd9Sstevel@tonic-gate if (udp->all_lwps == self)
8117c478bd9Sstevel@tonic-gate udp->all_lwps = self->ul_forw;
8127c478bd9Sstevel@tonic-gate if (udp->all_lwps == self)
8137c478bd9Sstevel@tonic-gate udp->all_lwps = NULL;
8147c478bd9Sstevel@tonic-gate else {
8157c478bd9Sstevel@tonic-gate self->ul_forw->ul_back = self->ul_back;
8167c478bd9Sstevel@tonic-gate self->ul_back->ul_forw = self->ul_forw;
8177c478bd9Sstevel@tonic-gate }
8187c478bd9Sstevel@tonic-gate self->ul_forw = self->ul_back = NULL;
819d4204c85Sraf #if defined(THREAD_DEBUG)
8207c478bd9Sstevel@tonic-gate /* collect queue lock statistics before marking ourself dead */
8217c478bd9Sstevel@tonic-gate record_spin_locks(self);
822d4204c85Sraf #endif
8237c478bd9Sstevel@tonic-gate self->ul_dead = 1;
8247c478bd9Sstevel@tonic-gate self->ul_pleasestop = 0;
8257c478bd9Sstevel@tonic-gate if (replace != NULL) {
8267c478bd9Sstevel@tonic-gate int ix = self->ul_ix; /* the hash index */
8278cd45542Sraf (void) memcpy(replace, self, REPLACEMENT_SIZE);
8287c478bd9Sstevel@tonic-gate replace->ul_self = replace;
8297c478bd9Sstevel@tonic-gate replace->ul_next = NULL; /* clone not on stack list */
8307c478bd9Sstevel@tonic-gate replace->ul_mapsiz = 0; /* allows clone to be freed */
8317c478bd9Sstevel@tonic-gate replace->ul_replace = 1; /* requires clone to be freed */
8327c478bd9Sstevel@tonic-gate hash_out_unlocked(self, ix, udp);
8337c478bd9Sstevel@tonic-gate hash_in_unlocked(replace, ix, udp);
8347c478bd9Sstevel@tonic-gate ASSERT(!(self->ul_detached));
8357c478bd9Sstevel@tonic-gate self->ul_detached = 1; /* this frees the stack */
8367c478bd9Sstevel@tonic-gate self->ul_schedctl = NULL;
8377c478bd9Sstevel@tonic-gate self->ul_schedctl_called = &udp->uberflags;
8387c478bd9Sstevel@tonic-gate set_curthread(self = replace);
8397c478bd9Sstevel@tonic-gate /*
8407c478bd9Sstevel@tonic-gate * Having just changed the address of curthread, we
8417c478bd9Sstevel@tonic-gate * must reset the ownership of the locks we hold so
8427c478bd9Sstevel@tonic-gate * that assertions will not fire when we release them.
8437c478bd9Sstevel@tonic-gate */
8447c478bd9Sstevel@tonic-gate udp->link_lock.mutex_owner = (uintptr_t)self;
8457c478bd9Sstevel@tonic-gate ulwp_mutex(self, udp)->mutex_owner = (uintptr_t)self;
8467c478bd9Sstevel@tonic-gate /*
8477c478bd9Sstevel@tonic-gate * NOTE:
8487c478bd9Sstevel@tonic-gate * On i386, %gs still references the original, not the
8497c478bd9Sstevel@tonic-gate * replacement, ulwp structure. Fetching the replacement
8507c478bd9Sstevel@tonic-gate * curthread pointer via %gs:0 works correctly since the
8517c478bd9Sstevel@tonic-gate * original ulwp structure will not be reallocated until
8527c478bd9Sstevel@tonic-gate * this lwp has completed its lwp_exit() system call (see
8537c478bd9Sstevel@tonic-gate * dead_and_buried()), but from here on out, we must make
8547c478bd9Sstevel@tonic-gate * no references to %gs:<offset> other than %gs:0.
8557c478bd9Sstevel@tonic-gate */
8567c478bd9Sstevel@tonic-gate }
8577c478bd9Sstevel@tonic-gate /*
8587c478bd9Sstevel@tonic-gate * Put non-detached terminated threads in the all_zombies list.
8597c478bd9Sstevel@tonic-gate */
8607c478bd9Sstevel@tonic-gate if (!self->ul_detached) {
8617c478bd9Sstevel@tonic-gate udp->nzombies++;
8627c478bd9Sstevel@tonic-gate if (udp->all_zombies == NULL) {
8637c478bd9Sstevel@tonic-gate ASSERT(udp->nzombies == 1);
8647c478bd9Sstevel@tonic-gate udp->all_zombies = self->ul_forw = self->ul_back = self;
8657c478bd9Sstevel@tonic-gate } else {
8667c478bd9Sstevel@tonic-gate self->ul_forw = udp->all_zombies;
8677c478bd9Sstevel@tonic-gate self->ul_back = udp->all_zombies->ul_back;
8687c478bd9Sstevel@tonic-gate self->ul_back->ul_forw = self;
8697c478bd9Sstevel@tonic-gate self->ul_forw->ul_back = self;
8707c478bd9Sstevel@tonic-gate }
8717c478bd9Sstevel@tonic-gate }
8727c478bd9Sstevel@tonic-gate /*
8737c478bd9Sstevel@tonic-gate * Notify everyone waiting for this thread.
8747c478bd9Sstevel@tonic-gate */
8757c478bd9Sstevel@tonic-gate ulwp_broadcast(self);
8767c478bd9Sstevel@tonic-gate (void) ulwp_unlock(self, udp);
8777c478bd9Sstevel@tonic-gate /*
8787c478bd9Sstevel@tonic-gate * Prevent any more references to the schedctl data.
8797c478bd9Sstevel@tonic-gate * We are exiting and continue_fork() may not find us.
8807c478bd9Sstevel@tonic-gate * Do this just before dropping link_lock, since fork
8817c478bd9Sstevel@tonic-gate * serializes on link_lock.
8827c478bd9Sstevel@tonic-gate */
8837c478bd9Sstevel@tonic-gate self->ul_schedctl = NULL;
8847c478bd9Sstevel@tonic-gate self->ul_schedctl_called = &udp->uberflags;
8857c478bd9Sstevel@tonic-gate lmutex_unlock(&udp->link_lock);
8867c478bd9Sstevel@tonic-gate
8877c478bd9Sstevel@tonic-gate ASSERT(self->ul_critical == 1);
8887c478bd9Sstevel@tonic-gate ASSERT(self->ul_preempt == 0);
8897c478bd9Sstevel@tonic-gate _lwp_terminate(); /* never returns */
8907c478bd9Sstevel@tonic-gate thr_panic("_thrp_exit(): _lwp_terminate() returned");
8917c478bd9Sstevel@tonic-gate }
8927c478bd9Sstevel@tonic-gate
893d4204c85Sraf #if defined(THREAD_DEBUG)
8947c478bd9Sstevel@tonic-gate void
collect_queue_statistics()8957c478bd9Sstevel@tonic-gate collect_queue_statistics()
8967c478bd9Sstevel@tonic-gate {
8977c478bd9Sstevel@tonic-gate uberdata_t *udp = curthread->ul_uberdata;
8987c478bd9Sstevel@tonic-gate ulwp_t *ulwp;
8997c478bd9Sstevel@tonic-gate
9007c478bd9Sstevel@tonic-gate if (thread_queue_dump) {
9017c478bd9Sstevel@tonic-gate lmutex_lock(&udp->link_lock);
9027c478bd9Sstevel@tonic-gate if ((ulwp = udp->all_lwps) != NULL) {
9037c478bd9Sstevel@tonic-gate do {
9047c478bd9Sstevel@tonic-gate record_spin_locks(ulwp);
9057c478bd9Sstevel@tonic-gate } while ((ulwp = ulwp->ul_forw) != udp->all_lwps);
9067c478bd9Sstevel@tonic-gate }
9077c478bd9Sstevel@tonic-gate lmutex_unlock(&udp->link_lock);
9087c478bd9Sstevel@tonic-gate }
9097c478bd9Sstevel@tonic-gate }
910d4204c85Sraf #endif
9117c478bd9Sstevel@tonic-gate
9127257d1b4Sraf static void __NORETURN
_thrp_exit_common(void * status,int unwind)9137257d1b4Sraf _thrp_exit_common(void *status, int unwind)
9147c478bd9Sstevel@tonic-gate {
9157c478bd9Sstevel@tonic-gate ulwp_t *self = curthread;
9167c478bd9Sstevel@tonic-gate int cancelled = (self->ul_cancel_pending && status == PTHREAD_CANCELED);
9177c478bd9Sstevel@tonic-gate
9187c478bd9Sstevel@tonic-gate ASSERT(self->ul_critical == 0 && self->ul_preempt == 0);
9197c478bd9Sstevel@tonic-gate
9207c478bd9Sstevel@tonic-gate /*
9217c478bd9Sstevel@tonic-gate * Disable cancellation and call the special DCE cancellation
9227c478bd9Sstevel@tonic-gate * cleanup hook if it is enabled. Do nothing else before calling
9237c478bd9Sstevel@tonic-gate * the DCE cancellation cleanup hook; it may call longjmp() and
9247c478bd9Sstevel@tonic-gate * never return here.
9257c478bd9Sstevel@tonic-gate */
9267c478bd9Sstevel@tonic-gate self->ul_cancel_disabled = 1;
9277c478bd9Sstevel@tonic-gate self->ul_cancel_async = 0;
9287c478bd9Sstevel@tonic-gate self->ul_save_async = 0;
9297c478bd9Sstevel@tonic-gate self->ul_cancelable = 0;
9307c478bd9Sstevel@tonic-gate self->ul_cancel_pending = 0;
931a574db85Sraf set_cancel_pending_flag(self, 1);
9327c478bd9Sstevel@tonic-gate if (cancelled && cleanuphndlr != NULL)
9337c478bd9Sstevel@tonic-gate (*cleanuphndlr)();
9347c478bd9Sstevel@tonic-gate
9357c478bd9Sstevel@tonic-gate /*
9367c478bd9Sstevel@tonic-gate * Block application signals while we are exiting.
9377c478bd9Sstevel@tonic-gate * We call out to C++, TSD, and TLS destructors while exiting
9387c478bd9Sstevel@tonic-gate * and these are application-defined, so we cannot be assured
9397c478bd9Sstevel@tonic-gate * that they won't reset the signal mask. We use sigoff() to
9407c478bd9Sstevel@tonic-gate * defer any signals that may be received as a result of this
9417c478bd9Sstevel@tonic-gate * bad behavior. Such signals will be lost to the process
9427c478bd9Sstevel@tonic-gate * when the thread finishes exiting.
9437c478bd9Sstevel@tonic-gate */
9447257d1b4Sraf (void) thr_sigsetmask(SIG_SETMASK, &maskset, NULL);
9457c478bd9Sstevel@tonic-gate sigoff(self);
9467c478bd9Sstevel@tonic-gate
9477c478bd9Sstevel@tonic-gate self->ul_rval = status;
9487c478bd9Sstevel@tonic-gate
9497c478bd9Sstevel@tonic-gate /*
9507c478bd9Sstevel@tonic-gate * If thr_exit is being called from the places where
9517c478bd9Sstevel@tonic-gate * C++ destructors are to be called such as cancellation
9527c478bd9Sstevel@tonic-gate * points, then set this flag. It is checked in _t_cancel()
9537c478bd9Sstevel@tonic-gate * to decide whether _ex_unwind() is to be called or not.
9547c478bd9Sstevel@tonic-gate */
9557c478bd9Sstevel@tonic-gate if (unwind)
9567c478bd9Sstevel@tonic-gate self->ul_unwind = 1;
9577c478bd9Sstevel@tonic-gate
9587c478bd9Sstevel@tonic-gate /*
9597c478bd9Sstevel@tonic-gate * _thrp_unwind() will eventually call _thrp_exit().
9607c478bd9Sstevel@tonic-gate * It never returns.
9617c478bd9Sstevel@tonic-gate */
9627c478bd9Sstevel@tonic-gate _thrp_unwind(NULL);
9637257d1b4Sraf thr_panic("_thrp_exit_common(): _thrp_unwind() returned");
9647257d1b4Sraf
9657257d1b4Sraf for (;;) /* to shut the compiler up about __NORETURN */
9667257d1b4Sraf continue;
9677c478bd9Sstevel@tonic-gate }
9687c478bd9Sstevel@tonic-gate
9697c478bd9Sstevel@tonic-gate /*
9707c478bd9Sstevel@tonic-gate * Called when a thread returns from its start function.
9717c478bd9Sstevel@tonic-gate * We are at the top of the stack; no unwinding is necessary.
9727c478bd9Sstevel@tonic-gate */
9737c478bd9Sstevel@tonic-gate void
_thrp_terminate(void * status)9747257d1b4Sraf _thrp_terminate(void *status)
9757c478bd9Sstevel@tonic-gate {
9767257d1b4Sraf _thrp_exit_common(status, 0);
9777c478bd9Sstevel@tonic-gate }
9787c478bd9Sstevel@tonic-gate
9797257d1b4Sraf #pragma weak pthread_exit = thr_exit
9807257d1b4Sraf #pragma weak _thr_exit = thr_exit
9817c478bd9Sstevel@tonic-gate void
thr_exit(void * status)9827257d1b4Sraf thr_exit(void *status)
9837c478bd9Sstevel@tonic-gate {
9847257d1b4Sraf _thrp_exit_common(status, 1);
9857c478bd9Sstevel@tonic-gate }
9867c478bd9Sstevel@tonic-gate
9877c478bd9Sstevel@tonic-gate int
_thrp_join(thread_t tid,thread_t * departed,void ** status,int do_cancel)9887c478bd9Sstevel@tonic-gate _thrp_join(thread_t tid, thread_t *departed, void **status, int do_cancel)
9897c478bd9Sstevel@tonic-gate {
9907c478bd9Sstevel@tonic-gate uberdata_t *udp = curthread->ul_uberdata;
9917c478bd9Sstevel@tonic-gate mutex_t *mp;
9927c478bd9Sstevel@tonic-gate void *rval;
9937c478bd9Sstevel@tonic-gate thread_t found;
9947c478bd9Sstevel@tonic-gate ulwp_t *ulwp;
9957c478bd9Sstevel@tonic-gate ulwp_t **ulwpp;
9967c478bd9Sstevel@tonic-gate int replace;
9977c478bd9Sstevel@tonic-gate int error;
9987c478bd9Sstevel@tonic-gate
9997c478bd9Sstevel@tonic-gate if (do_cancel)
10007c478bd9Sstevel@tonic-gate error = lwp_wait(tid, &found);
10017c478bd9Sstevel@tonic-gate else {
10027c478bd9Sstevel@tonic-gate while ((error = __lwp_wait(tid, &found)) == EINTR)
10037c478bd9Sstevel@tonic-gate ;
10047c478bd9Sstevel@tonic-gate }
10057c478bd9Sstevel@tonic-gate if (error)
10067c478bd9Sstevel@tonic-gate return (error);
10077c478bd9Sstevel@tonic-gate
10087c478bd9Sstevel@tonic-gate /*
10097c478bd9Sstevel@tonic-gate * We must hold link_lock to avoid a race condition with find_stack().
10107c478bd9Sstevel@tonic-gate */
10117c478bd9Sstevel@tonic-gate lmutex_lock(&udp->link_lock);
10127c478bd9Sstevel@tonic-gate if ((ulwpp = find_lwpp(found)) == NULL) {
10137c478bd9Sstevel@tonic-gate /*
10147c478bd9Sstevel@tonic-gate * lwp_wait() found an lwp that the library doesn't know
10157c478bd9Sstevel@tonic-gate * about. It must have been created with _lwp_create().
10167c478bd9Sstevel@tonic-gate * Just return its lwpid; we can't know its status.
10177c478bd9Sstevel@tonic-gate */
10187c478bd9Sstevel@tonic-gate lmutex_unlock(&udp->link_lock);
10197c478bd9Sstevel@tonic-gate rval = NULL;
10207c478bd9Sstevel@tonic-gate } else {
10217c478bd9Sstevel@tonic-gate /*
10227c478bd9Sstevel@tonic-gate * Remove ulwp from the hash table.
10237c478bd9Sstevel@tonic-gate */
10247c478bd9Sstevel@tonic-gate ulwp = *ulwpp;
10257c478bd9Sstevel@tonic-gate *ulwpp = ulwp->ul_hash;
10267c478bd9Sstevel@tonic-gate ulwp->ul_hash = NULL;
10277c478bd9Sstevel@tonic-gate /*
10287c478bd9Sstevel@tonic-gate * Remove ulwp from all_zombies list.
10297c478bd9Sstevel@tonic-gate */
10307c478bd9Sstevel@tonic-gate ASSERT(udp->nzombies >= 1);
10317c478bd9Sstevel@tonic-gate if (udp->all_zombies == ulwp)
10327c478bd9Sstevel@tonic-gate udp->all_zombies = ulwp->ul_forw;
10337c478bd9Sstevel@tonic-gate if (udp->all_zombies == ulwp)
10347c478bd9Sstevel@tonic-gate udp->all_zombies = NULL;
10357c478bd9Sstevel@tonic-gate else {
10367c478bd9Sstevel@tonic-gate ulwp->ul_forw->ul_back = ulwp->ul_back;
10377c478bd9Sstevel@tonic-gate ulwp->ul_back->ul_forw = ulwp->ul_forw;
10387c478bd9Sstevel@tonic-gate }
10397c478bd9Sstevel@tonic-gate ulwp->ul_forw = ulwp->ul_back = NULL;
10407c478bd9Sstevel@tonic-gate udp->nzombies--;
10417c478bd9Sstevel@tonic-gate ASSERT(ulwp->ul_dead && !ulwp->ul_detached &&
10427c478bd9Sstevel@tonic-gate !(ulwp->ul_usropts & (THR_DETACHED|THR_DAEMON)));
10437c478bd9Sstevel@tonic-gate /*
10447c478bd9Sstevel@tonic-gate * We can't call ulwp_unlock(ulwp) after we set
10457c478bd9Sstevel@tonic-gate * ulwp->ul_ix = -1 so we have to get a pointer to the
10467c478bd9Sstevel@tonic-gate * ulwp's hash table mutex now in order to unlock it below.
10477c478bd9Sstevel@tonic-gate */
10487c478bd9Sstevel@tonic-gate mp = ulwp_mutex(ulwp, udp);
10497c478bd9Sstevel@tonic-gate ulwp->ul_lwpid = (lwpid_t)(-1);
10507c478bd9Sstevel@tonic-gate ulwp->ul_ix = -1;
10517c478bd9Sstevel@tonic-gate rval = ulwp->ul_rval;
10527c478bd9Sstevel@tonic-gate replace = ulwp->ul_replace;
10537c478bd9Sstevel@tonic-gate lmutex_unlock(mp);
10547c478bd9Sstevel@tonic-gate if (replace) {
10557c478bd9Sstevel@tonic-gate ulwp->ul_next = NULL;
10567c478bd9Sstevel@tonic-gate if (udp->ulwp_replace_free == NULL)
10577c478bd9Sstevel@tonic-gate udp->ulwp_replace_free =
10587c478bd9Sstevel@tonic-gate udp->ulwp_replace_last = ulwp;
10597c478bd9Sstevel@tonic-gate else {
10607c478bd9Sstevel@tonic-gate udp->ulwp_replace_last->ul_next = ulwp;
10617c478bd9Sstevel@tonic-gate udp->ulwp_replace_last = ulwp;
10627c478bd9Sstevel@tonic-gate }
10637c478bd9Sstevel@tonic-gate }
10647c478bd9Sstevel@tonic-gate lmutex_unlock(&udp->link_lock);
10657c478bd9Sstevel@tonic-gate }
10667c478bd9Sstevel@tonic-gate
10677c478bd9Sstevel@tonic-gate if (departed != NULL)
10687c478bd9Sstevel@tonic-gate *departed = found;
10697c478bd9Sstevel@tonic-gate if (status != NULL)
10707c478bd9Sstevel@tonic-gate *status = rval;
10717c478bd9Sstevel@tonic-gate return (0);
10727c478bd9Sstevel@tonic-gate }
10737c478bd9Sstevel@tonic-gate
10747c478bd9Sstevel@tonic-gate int
thr_join(thread_t tid,thread_t * departed,void ** status)10757257d1b4Sraf thr_join(thread_t tid, thread_t *departed, void **status)
10767c478bd9Sstevel@tonic-gate {
10777c478bd9Sstevel@tonic-gate int error = _thrp_join(tid, departed, status, 1);
10787c478bd9Sstevel@tonic-gate return ((error == EINVAL)? ESRCH : error);
10797c478bd9Sstevel@tonic-gate }
10807c478bd9Sstevel@tonic-gate
10817c478bd9Sstevel@tonic-gate /*
10827c478bd9Sstevel@tonic-gate * pthread_join() differs from Solaris thr_join():
10837c478bd9Sstevel@tonic-gate * It does not return the departed thread's id
10847c478bd9Sstevel@tonic-gate * and hence does not have a "departed" argument.
10857c478bd9Sstevel@tonic-gate * It returns EINVAL if tid refers to a detached thread.
10867c478bd9Sstevel@tonic-gate */
10877257d1b4Sraf #pragma weak _pthread_join = pthread_join
10887c478bd9Sstevel@tonic-gate int
pthread_join(pthread_t tid,void ** status)10897257d1b4Sraf pthread_join(pthread_t tid, void **status)
10907c478bd9Sstevel@tonic-gate {
10917c478bd9Sstevel@tonic-gate return ((tid == 0)? ESRCH : _thrp_join(tid, NULL, status, 1));
10927c478bd9Sstevel@tonic-gate }
10937c478bd9Sstevel@tonic-gate
10947c478bd9Sstevel@tonic-gate int
pthread_detach(pthread_t tid)10957257d1b4Sraf pthread_detach(pthread_t tid)
10967c478bd9Sstevel@tonic-gate {
10977c478bd9Sstevel@tonic-gate uberdata_t *udp = curthread->ul_uberdata;
10987c478bd9Sstevel@tonic-gate ulwp_t *ulwp;
10997c478bd9Sstevel@tonic-gate ulwp_t **ulwpp;
11007c478bd9Sstevel@tonic-gate int error = 0;
11017c478bd9Sstevel@tonic-gate
11027c478bd9Sstevel@tonic-gate if ((ulwpp = find_lwpp(tid)) == NULL)
11037c478bd9Sstevel@tonic-gate return (ESRCH);
11047c478bd9Sstevel@tonic-gate ulwp = *ulwpp;
11057c478bd9Sstevel@tonic-gate
11067c478bd9Sstevel@tonic-gate if (ulwp->ul_dead) {
11077c478bd9Sstevel@tonic-gate ulwp_unlock(ulwp, udp);
11087c478bd9Sstevel@tonic-gate error = _thrp_join(tid, NULL, NULL, 0);
11097c478bd9Sstevel@tonic-gate } else {
11107c478bd9Sstevel@tonic-gate error = __lwp_detach(tid);
11117c478bd9Sstevel@tonic-gate ulwp->ul_detached = 1;
11127c478bd9Sstevel@tonic-gate ulwp->ul_usropts |= THR_DETACHED;
11137c478bd9Sstevel@tonic-gate ulwp_unlock(ulwp, udp);
11147c478bd9Sstevel@tonic-gate }
11157c478bd9Sstevel@tonic-gate return (error);
11167c478bd9Sstevel@tonic-gate }
11177c478bd9Sstevel@tonic-gate
11187c478bd9Sstevel@tonic-gate static const char *
ematch(const char * ev,const char * match)11197c478bd9Sstevel@tonic-gate ematch(const char *ev, const char *match)
11207c478bd9Sstevel@tonic-gate {
11217c478bd9Sstevel@tonic-gate int c;
11227c478bd9Sstevel@tonic-gate
11237c478bd9Sstevel@tonic-gate while ((c = *match++) != '\0') {
11247c478bd9Sstevel@tonic-gate if (*ev++ != c)
11257c478bd9Sstevel@tonic-gate return (NULL);
11267c478bd9Sstevel@tonic-gate }
11277c478bd9Sstevel@tonic-gate if (*ev++ != '=')
11287c478bd9Sstevel@tonic-gate return (NULL);
11297c478bd9Sstevel@tonic-gate return (ev);
11307c478bd9Sstevel@tonic-gate }
11317c478bd9Sstevel@tonic-gate
11327c478bd9Sstevel@tonic-gate static int
envvar(const char * ev,const char * match,int limit)11337c478bd9Sstevel@tonic-gate envvar(const char *ev, const char *match, int limit)
11347c478bd9Sstevel@tonic-gate {
11357c478bd9Sstevel@tonic-gate int val = -1;
11367c478bd9Sstevel@tonic-gate const char *ename;
11377c478bd9Sstevel@tonic-gate
11387c478bd9Sstevel@tonic-gate if ((ename = ematch(ev, match)) != NULL) {
11397c478bd9Sstevel@tonic-gate int c;
11407c478bd9Sstevel@tonic-gate for (val = 0; (c = *ename) != '\0'; ename++) {
11417c478bd9Sstevel@tonic-gate if (!isdigit(c)) {
11427c478bd9Sstevel@tonic-gate val = -1;
11437c478bd9Sstevel@tonic-gate break;
11447c478bd9Sstevel@tonic-gate }
11457c478bd9Sstevel@tonic-gate val = val * 10 + (c - '0');
11467c478bd9Sstevel@tonic-gate if (val > limit) {
11477c478bd9Sstevel@tonic-gate val = limit;
11487c478bd9Sstevel@tonic-gate break;
11497c478bd9Sstevel@tonic-gate }
11507c478bd9Sstevel@tonic-gate }
11517c478bd9Sstevel@tonic-gate }
11527c478bd9Sstevel@tonic-gate return (val);
11537c478bd9Sstevel@tonic-gate }
11547c478bd9Sstevel@tonic-gate
11557c478bd9Sstevel@tonic-gate static void
etest(const char * ev)11567c478bd9Sstevel@tonic-gate etest(const char *ev)
11577c478bd9Sstevel@tonic-gate {
11587c478bd9Sstevel@tonic-gate int value;
11597c478bd9Sstevel@tonic-gate
11607c478bd9Sstevel@tonic-gate if ((value = envvar(ev, "QUEUE_SPIN", 1000000)) >= 0)
11617c478bd9Sstevel@tonic-gate thread_queue_spin = value;
11625d1dd9a9Sraf if ((value = envvar(ev, "ADAPTIVE_SPIN", 1000000)) >= 0)
11637c478bd9Sstevel@tonic-gate thread_adaptive_spin = value;
11645d1dd9a9Sraf if ((value = envvar(ev, "MAX_SPINNERS", 255)) >= 0)
11657c478bd9Sstevel@tonic-gate thread_max_spinners = value;
11667c478bd9Sstevel@tonic-gate if ((value = envvar(ev, "QUEUE_FIFO", 8)) >= 0)
11677c478bd9Sstevel@tonic-gate thread_queue_fifo = value;
11687c478bd9Sstevel@tonic-gate #if defined(THREAD_DEBUG)
11697c478bd9Sstevel@tonic-gate if ((value = envvar(ev, "QUEUE_VERIFY", 1)) >= 0)
11707c478bd9Sstevel@tonic-gate thread_queue_verify = value;
11717c478bd9Sstevel@tonic-gate if ((value = envvar(ev, "QUEUE_DUMP", 1)) >= 0)
11727c478bd9Sstevel@tonic-gate thread_queue_dump = value;
1173d4204c85Sraf #endif
11747c478bd9Sstevel@tonic-gate if ((value = envvar(ev, "STACK_CACHE", 10000)) >= 0)
11757c478bd9Sstevel@tonic-gate thread_stack_cache = value;
11767c478bd9Sstevel@tonic-gate if ((value = envvar(ev, "COND_WAIT_DEFER", 1)) >= 0)
11777c478bd9Sstevel@tonic-gate thread_cond_wait_defer = value;
11787c478bd9Sstevel@tonic-gate if ((value = envvar(ev, "ERROR_DETECTION", 2)) >= 0)
11797c478bd9Sstevel@tonic-gate thread_error_detection = value;
11807c478bd9Sstevel@tonic-gate if ((value = envvar(ev, "ASYNC_SAFE", 1)) >= 0)
11817c478bd9Sstevel@tonic-gate thread_async_safe = value;
11827c478bd9Sstevel@tonic-gate if ((value = envvar(ev, "DOOR_NORESERVE", 1)) >= 0)
11837c478bd9Sstevel@tonic-gate thread_door_noreserve = value;
11847c5714f6Sraf if ((value = envvar(ev, "LOCKS_MISALIGNED", 1)) >= 0)
11857c5714f6Sraf thread_locks_misaligned = value;
11867c478bd9Sstevel@tonic-gate }
11877c478bd9Sstevel@tonic-gate
11887c478bd9Sstevel@tonic-gate /*
11897c478bd9Sstevel@tonic-gate * Look for and evaluate environment variables of the form "_THREAD_*".
11907c478bd9Sstevel@tonic-gate * For compatibility with the past, we also look for environment
11917c478bd9Sstevel@tonic-gate * names of the form "LIBTHREAD_*".
11927c478bd9Sstevel@tonic-gate */
11937c478bd9Sstevel@tonic-gate static void
set_thread_vars()11947c478bd9Sstevel@tonic-gate set_thread_vars()
11957c478bd9Sstevel@tonic-gate {
119659f081edSraf extern const char **_environ;
11977c478bd9Sstevel@tonic-gate const char **pev;
11987c478bd9Sstevel@tonic-gate const char *ev;
11997c478bd9Sstevel@tonic-gate char c;
12007c478bd9Sstevel@tonic-gate
120159f081edSraf if ((pev = _environ) == NULL)
12027c478bd9Sstevel@tonic-gate return;
12037c478bd9Sstevel@tonic-gate while ((ev = *pev++) != NULL) {
12047c478bd9Sstevel@tonic-gate c = *ev;
12057257d1b4Sraf if (c == '_' && strncmp(ev, "_THREAD_", 8) == 0)
12067c478bd9Sstevel@tonic-gate etest(ev + 8);
12077257d1b4Sraf if (c == 'L' && strncmp(ev, "LIBTHREAD_", 10) == 0)
12087c478bd9Sstevel@tonic-gate etest(ev + 10);
12097c478bd9Sstevel@tonic-gate }
12107c478bd9Sstevel@tonic-gate }
12117c478bd9Sstevel@tonic-gate
12127c478bd9Sstevel@tonic-gate /* PROBE_SUPPORT begin */
12137c478bd9Sstevel@tonic-gate #pragma weak __tnf_probe_notify
12147c478bd9Sstevel@tonic-gate extern void __tnf_probe_notify(void);
12157c478bd9Sstevel@tonic-gate /* PROBE_SUPPORT end */
12167c478bd9Sstevel@tonic-gate
12177c478bd9Sstevel@tonic-gate /* same as atexit() but private to the library */
12187c478bd9Sstevel@tonic-gate extern int _atexit(void (*)(void));
12197c478bd9Sstevel@tonic-gate
12207c478bd9Sstevel@tonic-gate /* same as _cleanup() but private to the library */
12217c478bd9Sstevel@tonic-gate extern void __cleanup(void);
12227c478bd9Sstevel@tonic-gate
12237c478bd9Sstevel@tonic-gate extern void atfork_init(void);
12247c478bd9Sstevel@tonic-gate
12257c478bd9Sstevel@tonic-gate #ifdef __amd64
1226d0b3732eSbholler extern void __proc64id(void);
12277c478bd9Sstevel@tonic-gate #endif
12287c478bd9Sstevel@tonic-gate
1229*dd7afb26SPatrick Mooney static void
init_auxv_data(uberdata_t * udp)1230*dd7afb26SPatrick Mooney init_auxv_data(uberdata_t *udp)
1231*dd7afb26SPatrick Mooney {
1232*dd7afb26SPatrick Mooney Dl_argsinfo_t args;
1233*dd7afb26SPatrick Mooney
1234*dd7afb26SPatrick Mooney udp->ub_comm_page = NULL;
1235*dd7afb26SPatrick Mooney if (dlinfo(RTLD_SELF, RTLD_DI_ARGSINFO, &args) < 0)
1236*dd7afb26SPatrick Mooney return;
1237*dd7afb26SPatrick Mooney
1238*dd7afb26SPatrick Mooney while (args.dla_auxv->a_type != AT_NULL) {
1239*dd7afb26SPatrick Mooney if (args.dla_auxv->a_type == AT_SUN_COMMPAGE) {
1240*dd7afb26SPatrick Mooney udp->ub_comm_page = args.dla_auxv->a_un.a_ptr;
1241*dd7afb26SPatrick Mooney }
1242*dd7afb26SPatrick Mooney args.dla_auxv++;
1243*dd7afb26SPatrick Mooney }
1244*dd7afb26SPatrick Mooney }
1245*dd7afb26SPatrick Mooney
12467c478bd9Sstevel@tonic-gate /*
12477c478bd9Sstevel@tonic-gate * libc_init() is called by ld.so.1 for library initialization.
12487c478bd9Sstevel@tonic-gate * We perform minimal initialization; enough to work with the main thread.
12497c478bd9Sstevel@tonic-gate */
12507c478bd9Sstevel@tonic-gate void
libc_init(void)12517c478bd9Sstevel@tonic-gate libc_init(void)
12527c478bd9Sstevel@tonic-gate {
12537c478bd9Sstevel@tonic-gate uberdata_t *udp = &__uberdata;
12547c478bd9Sstevel@tonic-gate ulwp_t *oldself = __curthread();
12557c478bd9Sstevel@tonic-gate ucontext_t uc;
12567c478bd9Sstevel@tonic-gate ulwp_t *self;
12577c478bd9Sstevel@tonic-gate struct rlimit rl;
12587c478bd9Sstevel@tonic-gate caddr_t data;
12597c478bd9Sstevel@tonic-gate size_t tls_size;
12607c478bd9Sstevel@tonic-gate int setmask;
12617c478bd9Sstevel@tonic-gate
12627c478bd9Sstevel@tonic-gate /*
12637c478bd9Sstevel@tonic-gate * For the initial stage of initialization, we must be careful
12647c478bd9Sstevel@tonic-gate * not to call any function that could possibly call _cerror().
12657c478bd9Sstevel@tonic-gate * For this purpose, we call only the raw system call wrappers.
12667c478bd9Sstevel@tonic-gate */
12677c478bd9Sstevel@tonic-gate
12687c478bd9Sstevel@tonic-gate #ifdef __amd64
12697c478bd9Sstevel@tonic-gate /*
12707c478bd9Sstevel@tonic-gate * Gather information about cache layouts for optimized
1271d0b3732eSbholler * AMD and Intel assembler strfoo() and memfoo() functions.
12727c478bd9Sstevel@tonic-gate */
1273d0b3732eSbholler __proc64id();
12747c478bd9Sstevel@tonic-gate #endif
12757c478bd9Sstevel@tonic-gate
12767c478bd9Sstevel@tonic-gate /*
12777c478bd9Sstevel@tonic-gate * Every libc, regardless of which link map, must register __cleanup().
12787c478bd9Sstevel@tonic-gate */
12797c478bd9Sstevel@tonic-gate (void) _atexit(__cleanup);
12807c478bd9Sstevel@tonic-gate
12817c478bd9Sstevel@tonic-gate /*
1282*dd7afb26SPatrick Mooney * Every libc, regardless of link map, needs to go through and check
1283*dd7afb26SPatrick Mooney * its aux vectors. Doing so will indicate whether or not this has
1284*dd7afb26SPatrick Mooney * been given a comm page (to optimize certain system actions).
1285*dd7afb26SPatrick Mooney */
1286*dd7afb26SPatrick Mooney init_auxv_data(udp);
1287*dd7afb26SPatrick Mooney
1288*dd7afb26SPatrick Mooney /*
12897c478bd9Sstevel@tonic-gate * We keep our uberdata on one of (a) the first alternate link map
12907c478bd9Sstevel@tonic-gate * or (b) the primary link map. We switch to the primary link map
12917c478bd9Sstevel@tonic-gate * and stay there once we see it. All intermediate link maps are
12927c478bd9Sstevel@tonic-gate * subject to being unloaded at any time.
12937c478bd9Sstevel@tonic-gate */
12947c478bd9Sstevel@tonic-gate if (oldself != NULL && (oldself->ul_primarymap || !primary_link_map)) {
12957c478bd9Sstevel@tonic-gate __tdb_bootstrap = oldself->ul_uberdata->tdb_bootstrap;
12967c478bd9Sstevel@tonic-gate mutex_setup();
12977c478bd9Sstevel@tonic-gate atfork_init(); /* every link map needs atfork() processing */
129823a1cceaSRoger A. Faulkner init_progname();
12997c478bd9Sstevel@tonic-gate return;
13007c478bd9Sstevel@tonic-gate }
13017c478bd9Sstevel@tonic-gate
13027c478bd9Sstevel@tonic-gate /*
13037c478bd9Sstevel@tonic-gate * To establish the main stack information, we have to get our context.
13047c478bd9Sstevel@tonic-gate * This is also convenient to use for getting our signal mask.
13057c478bd9Sstevel@tonic-gate */
13067c478bd9Sstevel@tonic-gate uc.uc_flags = UC_ALL;
13078cd45542Sraf (void) __getcontext(&uc);
13087c478bd9Sstevel@tonic-gate ASSERT(uc.uc_link == NULL);
13097c478bd9Sstevel@tonic-gate
13107c478bd9Sstevel@tonic-gate tls_size = roundup64(udp->tls_metadata.static_tls.tls_size);
13117c478bd9Sstevel@tonic-gate ASSERT(primary_link_map || tls_size == 0);
13127c478bd9Sstevel@tonic-gate data = lmalloc(sizeof (ulwp_t) + tls_size);
13137c478bd9Sstevel@tonic-gate if (data == NULL)
13147c478bd9Sstevel@tonic-gate thr_panic("cannot allocate thread structure for main thread");
13157c478bd9Sstevel@tonic-gate /* LINTED pointer cast may result in improper alignment */
13167c478bd9Sstevel@tonic-gate self = (ulwp_t *)(data + tls_size);
13177c478bd9Sstevel@tonic-gate init_hash_table[0].hash_bucket = self;
13187c478bd9Sstevel@tonic-gate
13197c478bd9Sstevel@tonic-gate self->ul_sigmask = uc.uc_sigmask;
13207c478bd9Sstevel@tonic-gate delete_reserved_signals(&self->ul_sigmask);
13217c478bd9Sstevel@tonic-gate /*
13227c478bd9Sstevel@tonic-gate * Are the old and new sets different?
13237c478bd9Sstevel@tonic-gate * (This can happen if we are currently blocking SIGCANCEL.)
13247c478bd9Sstevel@tonic-gate * If so, we must explicitly set our signal mask, below.
13257c478bd9Sstevel@tonic-gate */
13267c478bd9Sstevel@tonic-gate setmask =
13277c478bd9Sstevel@tonic-gate ((self->ul_sigmask.__sigbits[0] ^ uc.uc_sigmask.__sigbits[0]) |
1328bdf0047cSRoger A. Faulkner (self->ul_sigmask.__sigbits[1] ^ uc.uc_sigmask.__sigbits[1]) |
1329bdf0047cSRoger A. Faulkner (self->ul_sigmask.__sigbits[2] ^ uc.uc_sigmask.__sigbits[2]) |
1330bdf0047cSRoger A. Faulkner (self->ul_sigmask.__sigbits[3] ^ uc.uc_sigmask.__sigbits[3]));
13317c478bd9Sstevel@tonic-gate
13327c478bd9Sstevel@tonic-gate #ifdef __sparc
13337c478bd9Sstevel@tonic-gate /*
13347c478bd9Sstevel@tonic-gate * We cache several instructions in the thread structure for use
13357c478bd9Sstevel@tonic-gate * by the fasttrap DTrace provider. When changing this, read the
13367c478bd9Sstevel@tonic-gate * comment in fasttrap.h for the all the other places that must
13377c478bd9Sstevel@tonic-gate * be changed.
13387c478bd9Sstevel@tonic-gate */
13397c478bd9Sstevel@tonic-gate self->ul_dsave = 0x9de04000; /* save %g1, %g0, %sp */
13407c478bd9Sstevel@tonic-gate self->ul_drestore = 0x81e80000; /* restore %g0, %g0, %g0 */
13417c478bd9Sstevel@tonic-gate self->ul_dftret = 0x91d0203a; /* ta 0x3a */
13427c478bd9Sstevel@tonic-gate self->ul_dreturn = 0x81ca0000; /* return %o0 */
13437c478bd9Sstevel@tonic-gate #endif
13447c478bd9Sstevel@tonic-gate
1345a574db85Sraf self->ul_stktop = (uintptr_t)uc.uc_stack.ss_sp + uc.uc_stack.ss_size;
13468cd45542Sraf (void) getrlimit(RLIMIT_STACK, &rl);
13477c478bd9Sstevel@tonic-gate self->ul_stksiz = rl.rlim_cur;
13487c478bd9Sstevel@tonic-gate self->ul_stk = (caddr_t)(self->ul_stktop - self->ul_stksiz);
13497c478bd9Sstevel@tonic-gate
13507c478bd9Sstevel@tonic-gate self->ul_forw = self->ul_back = self;
13517c478bd9Sstevel@tonic-gate self->ul_hash = NULL;
13527c478bd9Sstevel@tonic-gate self->ul_ix = 0;
13537257d1b4Sraf self->ul_lwpid = 1; /* _lwp_self() */
13547c478bd9Sstevel@tonic-gate self->ul_main = 1;
13557c478bd9Sstevel@tonic-gate self->ul_self = self;
1356d4204c85Sraf self->ul_policy = -1; /* initialize only when needed */
1357d4204c85Sraf self->ul_pri = 0;
1358d4204c85Sraf self->ul_cid = 0;
1359e84487aeSraf self->ul_rtclassid = -1;
13607c478bd9Sstevel@tonic-gate self->ul_uberdata = udp;
13617c478bd9Sstevel@tonic-gate if (oldself != NULL) {
13627c478bd9Sstevel@tonic-gate int i;
13637c478bd9Sstevel@tonic-gate
13647c478bd9Sstevel@tonic-gate ASSERT(primary_link_map);
13657c478bd9Sstevel@tonic-gate ASSERT(oldself->ul_main == 1);
13667c478bd9Sstevel@tonic-gate self->ul_stsd = oldself->ul_stsd;
13677c478bd9Sstevel@tonic-gate for (i = 0; i < TSD_NFAST; i++)
13687c478bd9Sstevel@tonic-gate self->ul_ftsd[i] = oldself->ul_ftsd[i];
13697c478bd9Sstevel@tonic-gate self->ul_tls = oldself->ul_tls;
13707c478bd9Sstevel@tonic-gate /*
13717c478bd9Sstevel@tonic-gate * Retrieve all pointers to uberdata allocated
13727c478bd9Sstevel@tonic-gate * while running on previous link maps.
13730293487cSraf * We would like to do a structure assignment here, but
13740293487cSraf * gcc turns structure assignments into calls to memcpy(),
13750293487cSraf * a function exported from libc. We can't call any such
13760293487cSraf * external functions until we establish curthread, below,
13770293487cSraf * so we just call our private version of memcpy().
13787c478bd9Sstevel@tonic-gate */
13798cd45542Sraf (void) memcpy(udp, oldself->ul_uberdata, sizeof (*udp));
13807c478bd9Sstevel@tonic-gate /*
13817c478bd9Sstevel@tonic-gate * These items point to global data on the primary link map.
13827c478bd9Sstevel@tonic-gate */
13837c478bd9Sstevel@tonic-gate udp->thr_hash_table = init_hash_table;
13847c478bd9Sstevel@tonic-gate udp->sigacthandler = sigacthandler;
13857c478bd9Sstevel@tonic-gate udp->tdb.tdb_events = tdb_events;
13867c478bd9Sstevel@tonic-gate ASSERT(udp->nthreads == 1 && !udp->uberflags.uf_mt);
13877c478bd9Sstevel@tonic-gate ASSERT(udp->lwp_stacks == NULL);
13887c478bd9Sstevel@tonic-gate ASSERT(udp->ulwp_freelist == NULL);
13897c478bd9Sstevel@tonic-gate ASSERT(udp->ulwp_replace_free == NULL);
13907c478bd9Sstevel@tonic-gate ASSERT(udp->hash_size == 1);
13917c478bd9Sstevel@tonic-gate }
13927c478bd9Sstevel@tonic-gate udp->all_lwps = self;
13937c478bd9Sstevel@tonic-gate udp->ulwp_one = self;
13948cd45542Sraf udp->pid = getpid();
13957c478bd9Sstevel@tonic-gate udp->nthreads = 1;
13967c478bd9Sstevel@tonic-gate /*
13977c478bd9Sstevel@tonic-gate * In every link map, tdb_bootstrap points to the same piece of
13987c478bd9Sstevel@tonic-gate * allocated memory. When the primary link map is initialized,
13997c478bd9Sstevel@tonic-gate * the allocated memory is assigned a pointer to the one true
14007c478bd9Sstevel@tonic-gate * uberdata. This allows libc_db to initialize itself regardless
14017c478bd9Sstevel@tonic-gate * of which instance of libc it finds in the address space.
14027c478bd9Sstevel@tonic-gate */
14037c478bd9Sstevel@tonic-gate if (udp->tdb_bootstrap == NULL)
14047c478bd9Sstevel@tonic-gate udp->tdb_bootstrap = lmalloc(sizeof (uberdata_t *));
14057c478bd9Sstevel@tonic-gate __tdb_bootstrap = udp->tdb_bootstrap;
14067c478bd9Sstevel@tonic-gate if (primary_link_map) {
14077c478bd9Sstevel@tonic-gate self->ul_primarymap = 1;
14087c478bd9Sstevel@tonic-gate udp->primary_map = 1;
14097c478bd9Sstevel@tonic-gate *udp->tdb_bootstrap = udp;
14107c478bd9Sstevel@tonic-gate }
14117c478bd9Sstevel@tonic-gate /*
14127c478bd9Sstevel@tonic-gate * Cancellation can't happen until:
14137c478bd9Sstevel@tonic-gate * pthread_cancel() is called
14147c478bd9Sstevel@tonic-gate * or:
14157c478bd9Sstevel@tonic-gate * another thread is created
14167c478bd9Sstevel@tonic-gate * For now, as a single-threaded process, set the flag that tells
14177c478bd9Sstevel@tonic-gate * PROLOGUE/EPILOGUE (in scalls.c) that cancellation can't happen.
14187c478bd9Sstevel@tonic-gate */
14197c478bd9Sstevel@tonic-gate self->ul_nocancel = 1;
14207c478bd9Sstevel@tonic-gate
14217c478bd9Sstevel@tonic-gate #if defined(__amd64)
1422ae115bc7Smrj (void) ___lwp_private(_LWP_SETPRIVATE, _LWP_FSBASE, self);
14237c478bd9Sstevel@tonic-gate #elif defined(__i386)
1424ae115bc7Smrj (void) ___lwp_private(_LWP_SETPRIVATE, _LWP_GSBASE, self);
14257c478bd9Sstevel@tonic-gate #endif /* __i386 || __amd64 */
14267c478bd9Sstevel@tonic-gate set_curthread(self); /* redundant on i386 */
14277c478bd9Sstevel@tonic-gate /*
14287c478bd9Sstevel@tonic-gate * Now curthread is established and it is safe to call any
14297c478bd9Sstevel@tonic-gate * function in libc except one that uses thread-local storage.
14307c478bd9Sstevel@tonic-gate */
14317c478bd9Sstevel@tonic-gate self->ul_errnop = &errno;
14327c478bd9Sstevel@tonic-gate if (oldself != NULL) {
14337c478bd9Sstevel@tonic-gate /* tls_size was zero when oldself was allocated */
14347c478bd9Sstevel@tonic-gate lfree(oldself, sizeof (ulwp_t));
14357c478bd9Sstevel@tonic-gate }
14367c478bd9Sstevel@tonic-gate mutex_setup();
14377c478bd9Sstevel@tonic-gate atfork_init();
14387c478bd9Sstevel@tonic-gate signal_init();
14397c478bd9Sstevel@tonic-gate
14407c478bd9Sstevel@tonic-gate /*
14417c478bd9Sstevel@tonic-gate * If the stack is unlimited, we set the size to zero to disable
14427c478bd9Sstevel@tonic-gate * stack checking.
14437c478bd9Sstevel@tonic-gate * XXX: Work harder here. Get the stack size from /proc/self/rmap
14447c478bd9Sstevel@tonic-gate */
14457c478bd9Sstevel@tonic-gate if (self->ul_stksiz == RLIM_INFINITY) {
14467c478bd9Sstevel@tonic-gate self->ul_ustack.ss_sp = (void *)self->ul_stktop;
14477c478bd9Sstevel@tonic-gate self->ul_ustack.ss_size = 0;
14487c478bd9Sstevel@tonic-gate } else {
14497c478bd9Sstevel@tonic-gate self->ul_ustack.ss_sp = self->ul_stk;
14507c478bd9Sstevel@tonic-gate self->ul_ustack.ss_size = self->ul_stksiz;
14517c478bd9Sstevel@tonic-gate }
14527c478bd9Sstevel@tonic-gate self->ul_ustack.ss_flags = 0;
14538cd45542Sraf (void) setustack(&self->ul_ustack);
14547c478bd9Sstevel@tonic-gate
14557c478bd9Sstevel@tonic-gate /*
14567c478bd9Sstevel@tonic-gate * Get the variables that affect thread behavior from the environment.
14577c478bd9Sstevel@tonic-gate */
14587c478bd9Sstevel@tonic-gate set_thread_vars();
14597c478bd9Sstevel@tonic-gate udp->uberflags.uf_thread_error_detection = (char)thread_error_detection;
14607c478bd9Sstevel@tonic-gate udp->thread_stack_cache = thread_stack_cache;
14617c478bd9Sstevel@tonic-gate
14627c478bd9Sstevel@tonic-gate /*
14637c478bd9Sstevel@tonic-gate * Make per-thread copies of global variables, for speed.
14647c478bd9Sstevel@tonic-gate */
14657c478bd9Sstevel@tonic-gate self->ul_queue_fifo = (char)thread_queue_fifo;
14667c478bd9Sstevel@tonic-gate self->ul_cond_wait_defer = (char)thread_cond_wait_defer;
14677c478bd9Sstevel@tonic-gate self->ul_error_detection = (char)thread_error_detection;
14687c478bd9Sstevel@tonic-gate self->ul_async_safe = (char)thread_async_safe;
14697c478bd9Sstevel@tonic-gate self->ul_door_noreserve = (char)thread_door_noreserve;
14707c5714f6Sraf self->ul_misaligned = (char)thread_locks_misaligned;
14715d1dd9a9Sraf self->ul_max_spinners = (uint8_t)thread_max_spinners;
14727c478bd9Sstevel@tonic-gate self->ul_adaptive_spin = thread_adaptive_spin;
14737c478bd9Sstevel@tonic-gate self->ul_queue_spin = thread_queue_spin;
14747c478bd9Sstevel@tonic-gate
147535e6f27aSRoger A. Faulkner #if defined(__sparc) && !defined(_LP64)
147635e6f27aSRoger A. Faulkner if (self->ul_misaligned) {
147735e6f27aSRoger A. Faulkner /*
147835e6f27aSRoger A. Faulkner * Tell the kernel to fix up ldx/stx instructions that
147935e6f27aSRoger A. Faulkner * refer to non-8-byte aligned data instead of giving
148035e6f27aSRoger A. Faulkner * the process an alignment trap and generating SIGBUS.
148135e6f27aSRoger A. Faulkner *
148235e6f27aSRoger A. Faulkner * Programs compiled for 32-bit sparc with the Studio SS12
148335e6f27aSRoger A. Faulkner * compiler get this done for them automatically (in _init()).
148435e6f27aSRoger A. Faulkner * We do it here for the benefit of programs compiled with
148535e6f27aSRoger A. Faulkner * other compilers, like gcc.
148635e6f27aSRoger A. Faulkner *
148735e6f27aSRoger A. Faulkner * This is necessary for the _THREAD_LOCKS_MISALIGNED=1
148835e6f27aSRoger A. Faulkner * environment variable horrible hack to work.
148935e6f27aSRoger A. Faulkner */
149035e6f27aSRoger A. Faulkner extern void _do_fix_align(void);
149135e6f27aSRoger A. Faulkner _do_fix_align();
149235e6f27aSRoger A. Faulkner }
149335e6f27aSRoger A. Faulkner #endif
149435e6f27aSRoger A. Faulkner
14957c478bd9Sstevel@tonic-gate /*
14967c478bd9Sstevel@tonic-gate * When we have initialized the primary link map, inform
14977c478bd9Sstevel@tonic-gate * the dynamic linker about our interface functions.
149823a1cceaSRoger A. Faulkner * Set up our pointer to the program name.
14997c478bd9Sstevel@tonic-gate */
15007c478bd9Sstevel@tonic-gate if (self->ul_primarymap)
15017c478bd9Sstevel@tonic-gate _ld_libc((void *)rtld_funcs);
150223a1cceaSRoger A. Faulkner init_progname();
15037c478bd9Sstevel@tonic-gate
15047c478bd9Sstevel@tonic-gate /*
15057c478bd9Sstevel@tonic-gate * Defer signals until TLS constructors have been called.
15067c478bd9Sstevel@tonic-gate */
15077c478bd9Sstevel@tonic-gate sigoff(self);
15087c478bd9Sstevel@tonic-gate tls_setup();
15097c478bd9Sstevel@tonic-gate sigon(self);
15107c478bd9Sstevel@tonic-gate if (setmask)
15117c478bd9Sstevel@tonic-gate (void) restore_signals(self);
15127c478bd9Sstevel@tonic-gate
1513a574db85Sraf /*
1514a574db85Sraf * Make private copies of __xpg4 and __xpg6 so libc can test
1515a574db85Sraf * them after this point without invoking the dynamic linker.
1516a574db85Sraf */
1517a574db85Sraf libc__xpg4 = __xpg4;
1518a574db85Sraf libc__xpg6 = __xpg6;
1519a574db85Sraf
15207c478bd9Sstevel@tonic-gate /* PROBE_SUPPORT begin */
15217c478bd9Sstevel@tonic-gate if (self->ul_primarymap && __tnf_probe_notify != NULL)
15227c478bd9Sstevel@tonic-gate __tnf_probe_notify();
15237c478bd9Sstevel@tonic-gate /* PROBE_SUPPORT end */
1524f841f6adSraf
1525f841f6adSraf init_sigev_thread();
1526f841f6adSraf init_aio();
152780598c2fSnakanon
152880598c2fSnakanon /*
152980598c2fSnakanon * We need to reset __threaded dynamically at runtime, so that
153080598c2fSnakanon * __threaded can be bound to __threaded outside libc which may not
153180598c2fSnakanon * have initial value of 1 (without a copy relocation in a.out).
153280598c2fSnakanon */
153380598c2fSnakanon __threaded = 1;
15347c478bd9Sstevel@tonic-gate }
15357c478bd9Sstevel@tonic-gate
15367c478bd9Sstevel@tonic-gate #pragma fini(libc_fini)
15377c478bd9Sstevel@tonic-gate void
libc_fini()15387c478bd9Sstevel@tonic-gate libc_fini()
15397c478bd9Sstevel@tonic-gate {
15407c478bd9Sstevel@tonic-gate /*
15417c478bd9Sstevel@tonic-gate * If we are doing fini processing for the instance of libc
15427c478bd9Sstevel@tonic-gate * on the first alternate link map (this happens only when
15437c478bd9Sstevel@tonic-gate * the dynamic linker rejects a bad audit library), then clear
15447c478bd9Sstevel@tonic-gate * __curthread(). We abandon whatever memory was allocated by
15457c478bd9Sstevel@tonic-gate * lmalloc() while running on this alternate link-map but we
15467c478bd9Sstevel@tonic-gate * don't care (and can't find the memory in any case); we just
15477c478bd9Sstevel@tonic-gate * want to protect the application from this bad audit library.
15487c478bd9Sstevel@tonic-gate * No fini processing is done by libc in the normal case.
15497c478bd9Sstevel@tonic-gate */
15507c478bd9Sstevel@tonic-gate
15517c478bd9Sstevel@tonic-gate uberdata_t *udp = curthread->ul_uberdata;
15527c478bd9Sstevel@tonic-gate
15537c478bd9Sstevel@tonic-gate if (udp->primary_map == 0 && udp == &__uberdata)
15547c478bd9Sstevel@tonic-gate set_curthread(NULL);
15557c478bd9Sstevel@tonic-gate }
15567c478bd9Sstevel@tonic-gate
15577c478bd9Sstevel@tonic-gate /*
15587c478bd9Sstevel@tonic-gate * finish_init is called when we are about to become multi-threaded,
15597c478bd9Sstevel@tonic-gate * that is, on the first call to thr_create().
15607c478bd9Sstevel@tonic-gate */
15617c478bd9Sstevel@tonic-gate void
finish_init()15627c478bd9Sstevel@tonic-gate finish_init()
15637c478bd9Sstevel@tonic-gate {
15647c478bd9Sstevel@tonic-gate ulwp_t *self = curthread;
15657c478bd9Sstevel@tonic-gate uberdata_t *udp = self->ul_uberdata;
15667c478bd9Sstevel@tonic-gate thr_hash_table_t *htp;
15677c478bd9Sstevel@tonic-gate void *data;
15687c478bd9Sstevel@tonic-gate int i;
15697c478bd9Sstevel@tonic-gate
15707c478bd9Sstevel@tonic-gate /*
15717c478bd9Sstevel@tonic-gate * No locks needed here; we are single-threaded on the first call.
15727c478bd9Sstevel@tonic-gate * We can be called only after the primary link map has been set up.
15737c478bd9Sstevel@tonic-gate */
15747c478bd9Sstevel@tonic-gate ASSERT(self->ul_primarymap);
15757c478bd9Sstevel@tonic-gate ASSERT(self == udp->ulwp_one);
15767c478bd9Sstevel@tonic-gate ASSERT(!udp->uberflags.uf_mt);
15777c478bd9Sstevel@tonic-gate ASSERT(udp->hash_size == 1);
15787c478bd9Sstevel@tonic-gate
15797c478bd9Sstevel@tonic-gate /*
1580d4204c85Sraf * Initialize self->ul_policy, self->ul_cid, and self->ul_pri.
1581d4204c85Sraf */
1582d4204c85Sraf update_sched(self);
1583d4204c85Sraf
1584d4204c85Sraf /*
1585d4204c85Sraf * Allocate the queue_head array if not already allocated.
15867c478bd9Sstevel@tonic-gate */
15877c478bd9Sstevel@tonic-gate if (udp->queue_head == NULL)
15887c478bd9Sstevel@tonic-gate queue_alloc();
15897c478bd9Sstevel@tonic-gate
15907c478bd9Sstevel@tonic-gate /*
15917c478bd9Sstevel@tonic-gate * Now allocate the thread hash table.
15927c478bd9Sstevel@tonic-gate */
15938cd45542Sraf if ((data = mmap(NULL, HASHTBLSZ * sizeof (thr_hash_table_t),
15947c478bd9Sstevel@tonic-gate PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, -1, (off_t)0))
15957c478bd9Sstevel@tonic-gate == MAP_FAILED)
15967c478bd9Sstevel@tonic-gate thr_panic("cannot allocate thread hash table");
15977c478bd9Sstevel@tonic-gate
15987c478bd9Sstevel@tonic-gate udp->thr_hash_table = htp = (thr_hash_table_t *)data;
15997c478bd9Sstevel@tonic-gate udp->hash_size = HASHTBLSZ;
16007c478bd9Sstevel@tonic-gate udp->hash_mask = HASHTBLSZ - 1;
16017c478bd9Sstevel@tonic-gate
16027c478bd9Sstevel@tonic-gate for (i = 0; i < HASHTBLSZ; i++, htp++) {
1603883492d5Sraf htp->hash_lock.mutex_flag = LOCK_INITED;
16047c478bd9Sstevel@tonic-gate htp->hash_lock.mutex_magic = MUTEX_MAGIC;
16057c478bd9Sstevel@tonic-gate htp->hash_cond.cond_magic = COND_MAGIC;
16067c478bd9Sstevel@tonic-gate }
16077c478bd9Sstevel@tonic-gate hash_in_unlocked(self, TIDHASH(self->ul_lwpid, udp), udp);
16087c478bd9Sstevel@tonic-gate
16097c478bd9Sstevel@tonic-gate /*
16107c478bd9Sstevel@tonic-gate * Set up the SIGCANCEL handler for threads cancellation.
16117c478bd9Sstevel@tonic-gate */
1612f841f6adSraf setup_cancelsig(SIGCANCEL);
16137c478bd9Sstevel@tonic-gate
16147c478bd9Sstevel@tonic-gate /*
16157c478bd9Sstevel@tonic-gate * Arrange to do special things on exit --
16167c478bd9Sstevel@tonic-gate * - collect queue statistics from all remaining active threads.
1617d4204c85Sraf * - dump queue statistics to stderr if _THREAD_QUEUE_DUMP is set.
16187c478bd9Sstevel@tonic-gate * - grab assert_lock to ensure that assertion failures
16197c478bd9Sstevel@tonic-gate * and a core dump take precedence over _exit().
16207c478bd9Sstevel@tonic-gate * (Functions are called in the reverse order of their registration.)
16217c478bd9Sstevel@tonic-gate */
16227c478bd9Sstevel@tonic-gate (void) _atexit(grab_assert_lock);
1623d4204c85Sraf #if defined(THREAD_DEBUG)
1624d4204c85Sraf (void) _atexit(dump_queue_statistics);
16257c478bd9Sstevel@tonic-gate (void) _atexit(collect_queue_statistics);
1626d4204c85Sraf #endif
16277c478bd9Sstevel@tonic-gate }
16287c478bd9Sstevel@tonic-gate
16297c478bd9Sstevel@tonic-gate /*
1630657b1f3dSraf * Used only by postfork1_child(), below.
16317c478bd9Sstevel@tonic-gate */
16327c478bd9Sstevel@tonic-gate static void
mark_dead_and_buried(ulwp_t * ulwp)16337c478bd9Sstevel@tonic-gate mark_dead_and_buried(ulwp_t *ulwp)
16347c478bd9Sstevel@tonic-gate {
16357c478bd9Sstevel@tonic-gate ulwp->ul_dead = 1;
16367c478bd9Sstevel@tonic-gate ulwp->ul_lwpid = (lwpid_t)(-1);
16377c478bd9Sstevel@tonic-gate ulwp->ul_hash = NULL;
16387c478bd9Sstevel@tonic-gate ulwp->ul_ix = -1;
16397c478bd9Sstevel@tonic-gate ulwp->ul_schedctl = NULL;
16407c478bd9Sstevel@tonic-gate ulwp->ul_schedctl_called = NULL;
16417c478bd9Sstevel@tonic-gate }
16427c478bd9Sstevel@tonic-gate
16437c478bd9Sstevel@tonic-gate /*
16447c478bd9Sstevel@tonic-gate * This is called from fork1() in the child.
16457c478bd9Sstevel@tonic-gate * Reset our data structures to reflect one lwp.
16467c478bd9Sstevel@tonic-gate */
16477c478bd9Sstevel@tonic-gate void
postfork1_child()1648f841f6adSraf postfork1_child()
16497c478bd9Sstevel@tonic-gate {
16507c478bd9Sstevel@tonic-gate ulwp_t *self = curthread;
16517c478bd9Sstevel@tonic-gate uberdata_t *udp = self->ul_uberdata;
1652d4204c85Sraf queue_head_t *qp;
16537c478bd9Sstevel@tonic-gate ulwp_t *next;
16547c478bd9Sstevel@tonic-gate ulwp_t *ulwp;
16557c478bd9Sstevel@tonic-gate int i;
16567c478bd9Sstevel@tonic-gate
16577c478bd9Sstevel@tonic-gate /* daemon threads shouldn't call fork1(), but oh well... */
16587c478bd9Sstevel@tonic-gate self->ul_usropts &= ~THR_DAEMON;
16597c478bd9Sstevel@tonic-gate udp->nthreads = 1;
16607c478bd9Sstevel@tonic-gate udp->ndaemons = 0;
16617c478bd9Sstevel@tonic-gate udp->uberflags.uf_mt = 0;
16621d2738a5Sraf __libc_threaded = 0;
16637c478bd9Sstevel@tonic-gate for (i = 0; i < udp->hash_size; i++)
16647c478bd9Sstevel@tonic-gate udp->thr_hash_table[i].hash_bucket = NULL;
16657257d1b4Sraf self->ul_lwpid = _lwp_self();
16667c478bd9Sstevel@tonic-gate hash_in_unlocked(self, TIDHASH(self->ul_lwpid, udp), udp);
16677c478bd9Sstevel@tonic-gate
166898c1a6b4Sraf /*
16698cd45542Sraf * Some thread in the parent might have been suspended
16708cd45542Sraf * while holding udp->callout_lock or udp->ld_lock.
16718cd45542Sraf * Reinitialize the child's copies.
167298c1a6b4Sraf */
16737257d1b4Sraf (void) mutex_init(&udp->callout_lock,
16747257d1b4Sraf USYNC_THREAD | LOCK_RECURSIVE, NULL);
16757257d1b4Sraf (void) mutex_init(&udp->ld_lock,
16767257d1b4Sraf USYNC_THREAD | LOCK_RECURSIVE, NULL);
167798c1a6b4Sraf
16787c478bd9Sstevel@tonic-gate /* no one in the child is on a sleep queue; reinitialize */
1679d4204c85Sraf if ((qp = udp->queue_head) != NULL) {
16808cd45542Sraf (void) memset(qp, 0, 2 * QHASHSIZE * sizeof (queue_head_t));
1681d4204c85Sraf for (i = 0; i < 2 * QHASHSIZE; qp++, i++) {
1682d4204c85Sraf qp->qh_type = (i < QHASHSIZE)? MX : CV;
1683d4204c85Sraf qp->qh_lock.mutex_flag = LOCK_INITED;
1684d4204c85Sraf qp->qh_lock.mutex_magic = MUTEX_MAGIC;
1685d4204c85Sraf qp->qh_hlist = &qp->qh_def_root;
1686d4204c85Sraf #if defined(THREAD_DEBUG)
1687d4204c85Sraf qp->qh_hlen = 1;
1688d4204c85Sraf qp->qh_hmax = 1;
1689d4204c85Sraf #endif
1690883492d5Sraf }
16917c478bd9Sstevel@tonic-gate }
16927c478bd9Sstevel@tonic-gate
16937c478bd9Sstevel@tonic-gate /*
1694c4a8d66cSRoger A. Faulkner * Do post-fork1 processing for subsystems that need it.
1695c4a8d66cSRoger A. Faulkner * We need to do this before unmapping all of the abandoned
1696c4a8d66cSRoger A. Faulkner * threads' stacks, below(), because the post-fork1 actions
1697c4a8d66cSRoger A. Faulkner * might require access to those stacks.
1698c4a8d66cSRoger A. Faulkner */
1699c4a8d66cSRoger A. Faulkner postfork1_child_sigev_aio();
1700c4a8d66cSRoger A. Faulkner postfork1_child_sigev_mq();
1701c4a8d66cSRoger A. Faulkner postfork1_child_sigev_timer();
1702c4a8d66cSRoger A. Faulkner postfork1_child_aio();
1703c4a8d66cSRoger A. Faulkner /*
1704c4a8d66cSRoger A. Faulkner * The above subsystems use thread pools, so this action
1705c4a8d66cSRoger A. Faulkner * must be performed after those actions.
1706c4a8d66cSRoger A. Faulkner */
1707c4a8d66cSRoger A. Faulkner postfork1_child_tpool();
1708c4a8d66cSRoger A. Faulkner
1709c4a8d66cSRoger A. Faulkner /*
17107c478bd9Sstevel@tonic-gate * All lwps except ourself are gone. Mark them so.
17117c478bd9Sstevel@tonic-gate * First mark all of the lwps that have already been freed.
17127c478bd9Sstevel@tonic-gate * Then mark and free all of the active lwps except ourself.
17137c478bd9Sstevel@tonic-gate * Since we are single-threaded, no locks are required here.
17147c478bd9Sstevel@tonic-gate */
17157c478bd9Sstevel@tonic-gate for (ulwp = udp->lwp_stacks; ulwp != NULL; ulwp = ulwp->ul_next)
17167c478bd9Sstevel@tonic-gate mark_dead_and_buried(ulwp);
17177c478bd9Sstevel@tonic-gate for (ulwp = udp->ulwp_freelist; ulwp != NULL; ulwp = ulwp->ul_next)
17187c478bd9Sstevel@tonic-gate mark_dead_and_buried(ulwp);
17197c478bd9Sstevel@tonic-gate for (ulwp = self->ul_forw; ulwp != self; ulwp = next) {
17207c478bd9Sstevel@tonic-gate next = ulwp->ul_forw;
17217c478bd9Sstevel@tonic-gate ulwp->ul_forw = ulwp->ul_back = NULL;
17227c478bd9Sstevel@tonic-gate mark_dead_and_buried(ulwp);
17237c478bd9Sstevel@tonic-gate tsd_free(ulwp);
17247c478bd9Sstevel@tonic-gate tls_free(ulwp);
17257c478bd9Sstevel@tonic-gate rwl_free(ulwp);
1726883492d5Sraf heldlock_free(ulwp);
17277c478bd9Sstevel@tonic-gate ulwp_free(ulwp);
17287c478bd9Sstevel@tonic-gate }
17297c478bd9Sstevel@tonic-gate self->ul_forw = self->ul_back = udp->all_lwps = self;
17307c478bd9Sstevel@tonic-gate if (self != udp->ulwp_one)
17317c478bd9Sstevel@tonic-gate mark_dead_and_buried(udp->ulwp_one);
17327c478bd9Sstevel@tonic-gate if ((ulwp = udp->all_zombies) != NULL) {
17337c478bd9Sstevel@tonic-gate ASSERT(udp->nzombies != 0);
17347c478bd9Sstevel@tonic-gate do {
17357c478bd9Sstevel@tonic-gate next = ulwp->ul_forw;
17367c478bd9Sstevel@tonic-gate ulwp->ul_forw = ulwp->ul_back = NULL;
17377c478bd9Sstevel@tonic-gate mark_dead_and_buried(ulwp);
17387c478bd9Sstevel@tonic-gate udp->nzombies--;
17397c478bd9Sstevel@tonic-gate if (ulwp->ul_replace) {
17407c478bd9Sstevel@tonic-gate ulwp->ul_next = NULL;
17417c478bd9Sstevel@tonic-gate if (udp->ulwp_replace_free == NULL) {
17427c478bd9Sstevel@tonic-gate udp->ulwp_replace_free =
17437c478bd9Sstevel@tonic-gate udp->ulwp_replace_last = ulwp;
17447c478bd9Sstevel@tonic-gate } else {
17457c478bd9Sstevel@tonic-gate udp->ulwp_replace_last->ul_next = ulwp;
17467c478bd9Sstevel@tonic-gate udp->ulwp_replace_last = ulwp;
17477c478bd9Sstevel@tonic-gate }
17487c478bd9Sstevel@tonic-gate }
17497c478bd9Sstevel@tonic-gate } while ((ulwp = next) != udp->all_zombies);
17507c478bd9Sstevel@tonic-gate ASSERT(udp->nzombies == 0);
17517c478bd9Sstevel@tonic-gate udp->all_zombies = NULL;
17527c478bd9Sstevel@tonic-gate udp->nzombies = 0;
17537c478bd9Sstevel@tonic-gate }
17547c478bd9Sstevel@tonic-gate trim_stack_cache(0);
17557c478bd9Sstevel@tonic-gate }
17567c478bd9Sstevel@tonic-gate
17577c478bd9Sstevel@tonic-gate lwpid_t
lwp_self(void)17587c478bd9Sstevel@tonic-gate lwp_self(void)
17597c478bd9Sstevel@tonic-gate {
17607c478bd9Sstevel@tonic-gate return (curthread->ul_lwpid);
17617c478bd9Sstevel@tonic-gate }
17627c478bd9Sstevel@tonic-gate
17637257d1b4Sraf #pragma weak _ti_thr_self = thr_self
17647257d1b4Sraf #pragma weak pthread_self = thr_self
17657c478bd9Sstevel@tonic-gate thread_t
thr_self()17667257d1b4Sraf thr_self()
17677c478bd9Sstevel@tonic-gate {
17687c478bd9Sstevel@tonic-gate return (curthread->ul_lwpid);
17697c478bd9Sstevel@tonic-gate }
17707c478bd9Sstevel@tonic-gate
17717c478bd9Sstevel@tonic-gate int
thr_main()17727257d1b4Sraf thr_main()
17737c478bd9Sstevel@tonic-gate {
17747c478bd9Sstevel@tonic-gate ulwp_t *self = __curthread();
17757c478bd9Sstevel@tonic-gate
17767c478bd9Sstevel@tonic-gate return ((self == NULL)? -1 : self->ul_main);
17777c478bd9Sstevel@tonic-gate }
17787c478bd9Sstevel@tonic-gate
17797c478bd9Sstevel@tonic-gate int
_thrp_cancelled(void)1780657b1f3dSraf _thrp_cancelled(void)
1781657b1f3dSraf {
1782657b1f3dSraf return (curthread->ul_rval == PTHREAD_CANCELED);
1783657b1f3dSraf }
1784657b1f3dSraf
1785657b1f3dSraf int
_thrp_stksegment(ulwp_t * ulwp,stack_t * stk)17867c478bd9Sstevel@tonic-gate _thrp_stksegment(ulwp_t *ulwp, stack_t *stk)
17877c478bd9Sstevel@tonic-gate {
17887c478bd9Sstevel@tonic-gate stk->ss_sp = (void *)ulwp->ul_stktop;
17897c478bd9Sstevel@tonic-gate stk->ss_size = ulwp->ul_stksiz;
17907c478bd9Sstevel@tonic-gate stk->ss_flags = 0;
17917c478bd9Sstevel@tonic-gate return (0);
17927c478bd9Sstevel@tonic-gate }
17937c478bd9Sstevel@tonic-gate
17947257d1b4Sraf #pragma weak _thr_stksegment = thr_stksegment
17957c478bd9Sstevel@tonic-gate int
thr_stksegment(stack_t * stk)17967257d1b4Sraf thr_stksegment(stack_t *stk)
17977c478bd9Sstevel@tonic-gate {
17987c478bd9Sstevel@tonic-gate return (_thrp_stksegment(curthread, stk));
17997c478bd9Sstevel@tonic-gate }
18007c478bd9Sstevel@tonic-gate
18017c478bd9Sstevel@tonic-gate void
force_continue(ulwp_t * ulwp)18027c478bd9Sstevel@tonic-gate force_continue(ulwp_t *ulwp)
18037c478bd9Sstevel@tonic-gate {
18047c478bd9Sstevel@tonic-gate #if defined(THREAD_DEBUG)
18057c478bd9Sstevel@tonic-gate ulwp_t *self = curthread;
18067c478bd9Sstevel@tonic-gate uberdata_t *udp = self->ul_uberdata;
18077c478bd9Sstevel@tonic-gate #endif
18087c478bd9Sstevel@tonic-gate int error;
18097c478bd9Sstevel@tonic-gate timespec_t ts;
18107c478bd9Sstevel@tonic-gate
181136319254Sraf ASSERT(MUTEX_OWNED(&udp->fork_lock, self));
18127c478bd9Sstevel@tonic-gate ASSERT(MUTEX_OWNED(ulwp_mutex(ulwp, udp), self));
18137c478bd9Sstevel@tonic-gate
18147c478bd9Sstevel@tonic-gate for (;;) {
18157257d1b4Sraf error = _lwp_continue(ulwp->ul_lwpid);
18167c478bd9Sstevel@tonic-gate if (error != 0 && error != EINTR)
18177c478bd9Sstevel@tonic-gate break;
18187c478bd9Sstevel@tonic-gate error = 0;
18197c478bd9Sstevel@tonic-gate if (ulwp->ul_stopping) { /* he is stopping himself */
18207c478bd9Sstevel@tonic-gate ts.tv_sec = 0; /* give him a chance to run */
18217c478bd9Sstevel@tonic-gate ts.tv_nsec = 100000; /* 100 usecs or clock tick */
1822f841f6adSraf (void) __nanosleep(&ts, NULL);
18237c478bd9Sstevel@tonic-gate }
18247c478bd9Sstevel@tonic-gate if (!ulwp->ul_stopping) /* he is running now */
18257c478bd9Sstevel@tonic-gate break; /* so we are done */
18267c478bd9Sstevel@tonic-gate /*
18277c478bd9Sstevel@tonic-gate * He is marked as being in the process of stopping
18287c478bd9Sstevel@tonic-gate * himself. Loop around and continue him again.
18297c478bd9Sstevel@tonic-gate * He may not have been stopped the first time.
18307c478bd9Sstevel@tonic-gate */
18317c478bd9Sstevel@tonic-gate }
18327c478bd9Sstevel@tonic-gate }
18337c478bd9Sstevel@tonic-gate
18347c478bd9Sstevel@tonic-gate /*
1835e54ab87fSRoger A. Faulkner * Suspend an lwp with lwp_suspend(), then move it to a safe point,
1836e54ab87fSRoger A. Faulkner * that is, to a point where ul_critical and ul_rtld are both zero.
18377c478bd9Sstevel@tonic-gate * On return, the ulwp_lock() is dropped as with ulwp_unlock().
18387c478bd9Sstevel@tonic-gate * If 'link_dropped' is non-NULL, then 'link_lock' is held on entry.
18397c478bd9Sstevel@tonic-gate * If we have to drop link_lock, we store 1 through link_dropped.
18407c478bd9Sstevel@tonic-gate * If the lwp exits before it can be suspended, we return ESRCH.
18417c478bd9Sstevel@tonic-gate */
18427c478bd9Sstevel@tonic-gate int
safe_suspend(ulwp_t * ulwp,uchar_t whystopped,int * link_dropped)18437c478bd9Sstevel@tonic-gate safe_suspend(ulwp_t *ulwp, uchar_t whystopped, int *link_dropped)
18447c478bd9Sstevel@tonic-gate {
18457c478bd9Sstevel@tonic-gate ulwp_t *self = curthread;
18467c478bd9Sstevel@tonic-gate uberdata_t *udp = self->ul_uberdata;
18477c478bd9Sstevel@tonic-gate cond_t *cvp = ulwp_condvar(ulwp, udp);
18487c478bd9Sstevel@tonic-gate mutex_t *mp = ulwp_mutex(ulwp, udp);
18497c478bd9Sstevel@tonic-gate thread_t tid = ulwp->ul_lwpid;
18507c478bd9Sstevel@tonic-gate int ix = ulwp->ul_ix;
18517c478bd9Sstevel@tonic-gate int error = 0;
18527c478bd9Sstevel@tonic-gate
18537c478bd9Sstevel@tonic-gate ASSERT(whystopped == TSTP_REGULAR ||
18547c478bd9Sstevel@tonic-gate whystopped == TSTP_MUTATOR ||
18557c478bd9Sstevel@tonic-gate whystopped == TSTP_FORK);
18567c478bd9Sstevel@tonic-gate ASSERT(ulwp != self);
18577c478bd9Sstevel@tonic-gate ASSERT(!ulwp->ul_stop);
185836319254Sraf ASSERT(MUTEX_OWNED(&udp->fork_lock, self));
18597c478bd9Sstevel@tonic-gate ASSERT(MUTEX_OWNED(mp, self));
18607c478bd9Sstevel@tonic-gate
18617c478bd9Sstevel@tonic-gate if (link_dropped != NULL)
18627c478bd9Sstevel@tonic-gate *link_dropped = 0;
18637c478bd9Sstevel@tonic-gate
18647c478bd9Sstevel@tonic-gate /*
18657c478bd9Sstevel@tonic-gate * We must grab the target's spin lock before suspending it.
18667c478bd9Sstevel@tonic-gate * See the comments below and in _thrp_suspend() for why.
18677c478bd9Sstevel@tonic-gate */
18687c478bd9Sstevel@tonic-gate spin_lock_set(&ulwp->ul_spinlock);
18697c478bd9Sstevel@tonic-gate (void) ___lwp_suspend(tid);
18707c478bd9Sstevel@tonic-gate spin_lock_clear(&ulwp->ul_spinlock);
18717c478bd9Sstevel@tonic-gate
18727c478bd9Sstevel@tonic-gate top:
1873e54ab87fSRoger A. Faulkner if ((ulwp->ul_critical == 0 && ulwp->ul_rtld == 0) ||
1874e54ab87fSRoger A. Faulkner ulwp->ul_stopping) {
18757c478bd9Sstevel@tonic-gate /* thread is already safe */
18767c478bd9Sstevel@tonic-gate ulwp->ul_stop |= whystopped;
18777c478bd9Sstevel@tonic-gate } else {
18787c478bd9Sstevel@tonic-gate /*
18797c478bd9Sstevel@tonic-gate * Setting ul_pleasestop causes the target thread to stop
18807c478bd9Sstevel@tonic-gate * itself in _thrp_suspend(), below, after we drop its lock.
18817c478bd9Sstevel@tonic-gate * We must continue the critical thread before dropping
18827c478bd9Sstevel@tonic-gate * link_lock because the critical thread may be holding
18837c478bd9Sstevel@tonic-gate * the queue lock for link_lock. This is delicate.
18847c478bd9Sstevel@tonic-gate */
18857c478bd9Sstevel@tonic-gate ulwp->ul_pleasestop |= whystopped;
18867c478bd9Sstevel@tonic-gate force_continue(ulwp);
18877c478bd9Sstevel@tonic-gate if (link_dropped != NULL) {
18887c478bd9Sstevel@tonic-gate *link_dropped = 1;
18897c478bd9Sstevel@tonic-gate lmutex_unlock(&udp->link_lock);
18907c478bd9Sstevel@tonic-gate /* be sure to drop link_lock only once */
18917c478bd9Sstevel@tonic-gate link_dropped = NULL;
18927c478bd9Sstevel@tonic-gate }
18937c478bd9Sstevel@tonic-gate
18947c478bd9Sstevel@tonic-gate /*
18957c478bd9Sstevel@tonic-gate * The thread may disappear by calling thr_exit() so we
18967c478bd9Sstevel@tonic-gate * cannot rely on the ulwp pointer after dropping the lock.
18977c478bd9Sstevel@tonic-gate * Instead, we search the hash table to find it again.
18987c478bd9Sstevel@tonic-gate * When we return, we may find that the thread has been
18997c478bd9Sstevel@tonic-gate * continued by some other thread. The suspend/continue
19007c478bd9Sstevel@tonic-gate * interfaces are prone to such race conditions by design.
19017c478bd9Sstevel@tonic-gate */
19027c478bd9Sstevel@tonic-gate while (ulwp && !ulwp->ul_dead && !ulwp->ul_stop &&
19037c478bd9Sstevel@tonic-gate (ulwp->ul_pleasestop & whystopped)) {
1904a574db85Sraf (void) __cond_wait(cvp, mp);
19057c478bd9Sstevel@tonic-gate for (ulwp = udp->thr_hash_table[ix].hash_bucket;
19067c478bd9Sstevel@tonic-gate ulwp != NULL; ulwp = ulwp->ul_hash) {
19077c478bd9Sstevel@tonic-gate if (ulwp->ul_lwpid == tid)
19087c478bd9Sstevel@tonic-gate break;
19097c478bd9Sstevel@tonic-gate }
19107c478bd9Sstevel@tonic-gate }
19117c478bd9Sstevel@tonic-gate
19127c478bd9Sstevel@tonic-gate if (ulwp == NULL || ulwp->ul_dead)
19137c478bd9Sstevel@tonic-gate error = ESRCH;
19147c478bd9Sstevel@tonic-gate else {
19157c478bd9Sstevel@tonic-gate /*
19167c478bd9Sstevel@tonic-gate * Do another lwp_suspend() to make sure we don't
19177c478bd9Sstevel@tonic-gate * return until the target thread is fully stopped
19187c478bd9Sstevel@tonic-gate * in the kernel. Don't apply lwp_suspend() until
19197c478bd9Sstevel@tonic-gate * we know that the target is not holding any
19207c478bd9Sstevel@tonic-gate * queue locks, that is, that it has completed
19217c478bd9Sstevel@tonic-gate * ulwp_unlock(self) and has, or at least is
19227c478bd9Sstevel@tonic-gate * about to, call lwp_suspend() on itself. We do
19237c478bd9Sstevel@tonic-gate * this by grabbing the target's spin lock.
19247c478bd9Sstevel@tonic-gate */
19257c478bd9Sstevel@tonic-gate ASSERT(ulwp->ul_lwpid == tid);
19267c478bd9Sstevel@tonic-gate spin_lock_set(&ulwp->ul_spinlock);
19277c478bd9Sstevel@tonic-gate (void) ___lwp_suspend(tid);
19287c478bd9Sstevel@tonic-gate spin_lock_clear(&ulwp->ul_spinlock);
19297c478bd9Sstevel@tonic-gate /*
19307c478bd9Sstevel@tonic-gate * If some other thread did a thr_continue()
19317c478bd9Sstevel@tonic-gate * on the target thread we have to start over.
19327c478bd9Sstevel@tonic-gate */
19337c478bd9Sstevel@tonic-gate if (!ulwp->ul_stopping || !(ulwp->ul_stop & whystopped))
19347c478bd9Sstevel@tonic-gate goto top;
19357c478bd9Sstevel@tonic-gate }
19367c478bd9Sstevel@tonic-gate }
19377c478bd9Sstevel@tonic-gate
19387257d1b4Sraf (void) cond_broadcast(cvp);
19397c478bd9Sstevel@tonic-gate lmutex_unlock(mp);
19407c478bd9Sstevel@tonic-gate return (error);
19417c478bd9Sstevel@tonic-gate }
19427c478bd9Sstevel@tonic-gate
19437c478bd9Sstevel@tonic-gate int
_thrp_suspend(thread_t tid,uchar_t whystopped)19447c478bd9Sstevel@tonic-gate _thrp_suspend(thread_t tid, uchar_t whystopped)
19457c478bd9Sstevel@tonic-gate {
19467c478bd9Sstevel@tonic-gate ulwp_t *self = curthread;
19477c478bd9Sstevel@tonic-gate uberdata_t *udp = self->ul_uberdata;
19487c478bd9Sstevel@tonic-gate ulwp_t *ulwp;
19497c478bd9Sstevel@tonic-gate int error = 0;
19507c478bd9Sstevel@tonic-gate
19517c478bd9Sstevel@tonic-gate ASSERT((whystopped & (TSTP_REGULAR|TSTP_MUTATOR|TSTP_FORK)) != 0);
19527c478bd9Sstevel@tonic-gate ASSERT((whystopped & ~(TSTP_REGULAR|TSTP_MUTATOR|TSTP_FORK)) == 0);
19537c478bd9Sstevel@tonic-gate
19547c478bd9Sstevel@tonic-gate /*
19557f6e74f8Sraf * We can't suspend anyone except ourself while
19567f6e74f8Sraf * some other thread is performing a fork.
19577f6e74f8Sraf * This also allows only one suspension at a time.
19587c478bd9Sstevel@tonic-gate */
19597f6e74f8Sraf if (tid != self->ul_lwpid)
19602e145884Sraf fork_lock_enter();
19617c478bd9Sstevel@tonic-gate
19627c478bd9Sstevel@tonic-gate if ((ulwp = find_lwp(tid)) == NULL)
19637c478bd9Sstevel@tonic-gate error = ESRCH;
19647c478bd9Sstevel@tonic-gate else if (whystopped == TSTP_MUTATOR && !ulwp->ul_mutator) {
19657c478bd9Sstevel@tonic-gate ulwp_unlock(ulwp, udp);
19667c478bd9Sstevel@tonic-gate error = EINVAL;
19677c478bd9Sstevel@tonic-gate } else if (ulwp->ul_stop) { /* already stopped */
19687c478bd9Sstevel@tonic-gate ulwp->ul_stop |= whystopped;
19697c478bd9Sstevel@tonic-gate ulwp_broadcast(ulwp);
19707c478bd9Sstevel@tonic-gate ulwp_unlock(ulwp, udp);
19717c478bd9Sstevel@tonic-gate } else if (ulwp != self) {
19727c478bd9Sstevel@tonic-gate /*
19737c478bd9Sstevel@tonic-gate * After suspending the other thread, move it out of a
19747c478bd9Sstevel@tonic-gate * critical section and deal with the schedctl mappings.
19757c478bd9Sstevel@tonic-gate * safe_suspend() suspends the other thread, calls
19767c478bd9Sstevel@tonic-gate * ulwp_broadcast(ulwp) and drops the ulwp lock.
19777c478bd9Sstevel@tonic-gate */
19787c478bd9Sstevel@tonic-gate error = safe_suspend(ulwp, whystopped, NULL);
19797c478bd9Sstevel@tonic-gate } else {
19807c478bd9Sstevel@tonic-gate int schedctl_after_fork = 0;
19817c478bd9Sstevel@tonic-gate
19827c478bd9Sstevel@tonic-gate /*
19837c478bd9Sstevel@tonic-gate * We are suspending ourself. We must not take a signal
19847c478bd9Sstevel@tonic-gate * until we return from lwp_suspend() and clear ul_stopping.
19857c478bd9Sstevel@tonic-gate * This is to guard against siglongjmp().
19867c478bd9Sstevel@tonic-gate */
19877c478bd9Sstevel@tonic-gate enter_critical(self);
19887c478bd9Sstevel@tonic-gate self->ul_sp = stkptr();
19897c478bd9Sstevel@tonic-gate _flush_windows(); /* sparc */
19907c478bd9Sstevel@tonic-gate self->ul_pleasestop = 0;
19917c478bd9Sstevel@tonic-gate self->ul_stop |= whystopped;
19927c478bd9Sstevel@tonic-gate /*
19937c478bd9Sstevel@tonic-gate * Grab our spin lock before dropping ulwp_mutex(self).
19947c478bd9Sstevel@tonic-gate * This prevents the suspending thread from applying
19957c478bd9Sstevel@tonic-gate * lwp_suspend() to us before we emerge from
19967c478bd9Sstevel@tonic-gate * lmutex_unlock(mp) and have dropped mp's queue lock.
19977c478bd9Sstevel@tonic-gate */
19987c478bd9Sstevel@tonic-gate spin_lock_set(&self->ul_spinlock);
19997c478bd9Sstevel@tonic-gate self->ul_stopping = 1;
20007c478bd9Sstevel@tonic-gate ulwp_broadcast(self);
20017c478bd9Sstevel@tonic-gate ulwp_unlock(self, udp);
20027c478bd9Sstevel@tonic-gate /*
20037c478bd9Sstevel@tonic-gate * From this point until we return from lwp_suspend(),
20047c478bd9Sstevel@tonic-gate * we must not call any function that might invoke the
20057c478bd9Sstevel@tonic-gate * dynamic linker, that is, we can only call functions
20067c478bd9Sstevel@tonic-gate * private to the library.
20077c478bd9Sstevel@tonic-gate *
20087c478bd9Sstevel@tonic-gate * Also, this is a nasty race condition for a process
20097c478bd9Sstevel@tonic-gate * that is undergoing a forkall() operation:
20107c478bd9Sstevel@tonic-gate * Once we clear our spinlock (below), we are vulnerable
20117c478bd9Sstevel@tonic-gate * to being suspended by the forkall() thread before
20127c478bd9Sstevel@tonic-gate * we manage to suspend ourself in ___lwp_suspend().
20137c478bd9Sstevel@tonic-gate * See safe_suspend() and force_continue().
20147c478bd9Sstevel@tonic-gate *
20157c478bd9Sstevel@tonic-gate * To avoid a SIGSEGV due to the disappearance
20167c478bd9Sstevel@tonic-gate * of the schedctl mappings in the child process,
20177c478bd9Sstevel@tonic-gate * which can happen in spin_lock_clear() if we
20187c478bd9Sstevel@tonic-gate * are suspended while we are in the middle of
20197c478bd9Sstevel@tonic-gate * its call to preempt(), we preemptively clear
20207c478bd9Sstevel@tonic-gate * our own schedctl pointer before dropping our
20217c478bd9Sstevel@tonic-gate * spinlock. We reinstate it, in both the parent
20227c478bd9Sstevel@tonic-gate * and (if this really is a forkall()) the child.
20237c478bd9Sstevel@tonic-gate */
20247c478bd9Sstevel@tonic-gate if (whystopped & TSTP_FORK) {
20257c478bd9Sstevel@tonic-gate schedctl_after_fork = 1;
20267c478bd9Sstevel@tonic-gate self->ul_schedctl = NULL;
20277c478bd9Sstevel@tonic-gate self->ul_schedctl_called = &udp->uberflags;
20287c478bd9Sstevel@tonic-gate }
20297c478bd9Sstevel@tonic-gate spin_lock_clear(&self->ul_spinlock);
20307c478bd9Sstevel@tonic-gate (void) ___lwp_suspend(tid);
20317c478bd9Sstevel@tonic-gate /*
20327c478bd9Sstevel@tonic-gate * Somebody else continued us.
20337c478bd9Sstevel@tonic-gate * We can't grab ulwp_lock(self)
20347c478bd9Sstevel@tonic-gate * until after clearing ul_stopping.
20357c478bd9Sstevel@tonic-gate * force_continue() relies on this.
20367c478bd9Sstevel@tonic-gate */
20377c478bd9Sstevel@tonic-gate self->ul_stopping = 0;
20387c478bd9Sstevel@tonic-gate self->ul_sp = 0;
20397c478bd9Sstevel@tonic-gate if (schedctl_after_fork) {
20407c478bd9Sstevel@tonic-gate self->ul_schedctl_called = NULL;
20417c478bd9Sstevel@tonic-gate self->ul_schedctl = NULL;
20427c478bd9Sstevel@tonic-gate (void) setup_schedctl();
20437c478bd9Sstevel@tonic-gate }
20447c478bd9Sstevel@tonic-gate ulwp_lock(self, udp);
20457c478bd9Sstevel@tonic-gate ulwp_broadcast(self);
20467c478bd9Sstevel@tonic-gate ulwp_unlock(self, udp);
20477c478bd9Sstevel@tonic-gate exit_critical(self);
20487c478bd9Sstevel@tonic-gate }
20497c478bd9Sstevel@tonic-gate
20507c478bd9Sstevel@tonic-gate if (tid != self->ul_lwpid)
20517c478bd9Sstevel@tonic-gate fork_lock_exit();
20527c478bd9Sstevel@tonic-gate
20537c478bd9Sstevel@tonic-gate return (error);
20547c478bd9Sstevel@tonic-gate }
20557c478bd9Sstevel@tonic-gate
20567c478bd9Sstevel@tonic-gate /*
20577c478bd9Sstevel@tonic-gate * Suspend all lwps other than ourself in preparation for fork.
20587c478bd9Sstevel@tonic-gate */
20597c478bd9Sstevel@tonic-gate void
suspend_fork()20607c478bd9Sstevel@tonic-gate suspend_fork()
20617c478bd9Sstevel@tonic-gate {
20627c478bd9Sstevel@tonic-gate ulwp_t *self = curthread;
20637c478bd9Sstevel@tonic-gate uberdata_t *udp = self->ul_uberdata;
20647c478bd9Sstevel@tonic-gate ulwp_t *ulwp;
20657c478bd9Sstevel@tonic-gate int link_dropped;
20667c478bd9Sstevel@tonic-gate
206736319254Sraf ASSERT(MUTEX_OWNED(&udp->fork_lock, self));
20687c478bd9Sstevel@tonic-gate top:
20697c478bd9Sstevel@tonic-gate lmutex_lock(&udp->link_lock);
20707c478bd9Sstevel@tonic-gate
20717c478bd9Sstevel@tonic-gate for (ulwp = self->ul_forw; ulwp != self; ulwp = ulwp->ul_forw) {
20727c478bd9Sstevel@tonic-gate ulwp_lock(ulwp, udp);
20737c478bd9Sstevel@tonic-gate if (ulwp->ul_stop) { /* already stopped */
20747c478bd9Sstevel@tonic-gate ulwp->ul_stop |= TSTP_FORK;
20757c478bd9Sstevel@tonic-gate ulwp_broadcast(ulwp);
20767c478bd9Sstevel@tonic-gate ulwp_unlock(ulwp, udp);
20777c478bd9Sstevel@tonic-gate } else {
20787c478bd9Sstevel@tonic-gate /*
20797c478bd9Sstevel@tonic-gate * Move the stopped lwp out of a critical section.
20807c478bd9Sstevel@tonic-gate */
20817c478bd9Sstevel@tonic-gate if (safe_suspend(ulwp, TSTP_FORK, &link_dropped) ||
20827c478bd9Sstevel@tonic-gate link_dropped)
20837c478bd9Sstevel@tonic-gate goto top;
20847c478bd9Sstevel@tonic-gate }
20857c478bd9Sstevel@tonic-gate }
20867c478bd9Sstevel@tonic-gate
20877c478bd9Sstevel@tonic-gate lmutex_unlock(&udp->link_lock);
20887c478bd9Sstevel@tonic-gate }
20897c478bd9Sstevel@tonic-gate
20907c478bd9Sstevel@tonic-gate void
continue_fork(int child)20917c478bd9Sstevel@tonic-gate continue_fork(int child)
20927c478bd9Sstevel@tonic-gate {
20937c478bd9Sstevel@tonic-gate ulwp_t *self = curthread;
20947c478bd9Sstevel@tonic-gate uberdata_t *udp = self->ul_uberdata;
20957c478bd9Sstevel@tonic-gate ulwp_t *ulwp;
20967c478bd9Sstevel@tonic-gate
209736319254Sraf ASSERT(MUTEX_OWNED(&udp->fork_lock, self));
209836319254Sraf
20997c478bd9Sstevel@tonic-gate /*
21007c478bd9Sstevel@tonic-gate * Clear the schedctl pointers in the child of forkall().
21017c478bd9Sstevel@tonic-gate */
21027c478bd9Sstevel@tonic-gate if (child) {
21037c478bd9Sstevel@tonic-gate for (ulwp = self->ul_forw; ulwp != self; ulwp = ulwp->ul_forw) {
21047c478bd9Sstevel@tonic-gate ulwp->ul_schedctl_called =
21057c478bd9Sstevel@tonic-gate ulwp->ul_dead? &udp->uberflags : NULL;
21067c478bd9Sstevel@tonic-gate ulwp->ul_schedctl = NULL;
21077c478bd9Sstevel@tonic-gate }
21087c478bd9Sstevel@tonic-gate }
21097c478bd9Sstevel@tonic-gate
21107c478bd9Sstevel@tonic-gate /*
21117c478bd9Sstevel@tonic-gate * Set all lwps that were stopped for fork() running again.
21127c478bd9Sstevel@tonic-gate */
21137c478bd9Sstevel@tonic-gate lmutex_lock(&udp->link_lock);
21147c478bd9Sstevel@tonic-gate for (ulwp = self->ul_forw; ulwp != self; ulwp = ulwp->ul_forw) {
21157c478bd9Sstevel@tonic-gate mutex_t *mp = ulwp_mutex(ulwp, udp);
21167c478bd9Sstevel@tonic-gate lmutex_lock(mp);
21177c478bd9Sstevel@tonic-gate ASSERT(ulwp->ul_stop & TSTP_FORK);
21187c478bd9Sstevel@tonic-gate ulwp->ul_stop &= ~TSTP_FORK;
21197c478bd9Sstevel@tonic-gate ulwp_broadcast(ulwp);
21207c478bd9Sstevel@tonic-gate if (!ulwp->ul_stop)
21217c478bd9Sstevel@tonic-gate force_continue(ulwp);
21227c478bd9Sstevel@tonic-gate lmutex_unlock(mp);
21237c478bd9Sstevel@tonic-gate }
21247c478bd9Sstevel@tonic-gate lmutex_unlock(&udp->link_lock);
21257c478bd9Sstevel@tonic-gate }
21267c478bd9Sstevel@tonic-gate
21277c478bd9Sstevel@tonic-gate int
_thrp_continue(thread_t tid,uchar_t whystopped)21287c478bd9Sstevel@tonic-gate _thrp_continue(thread_t tid, uchar_t whystopped)
21297c478bd9Sstevel@tonic-gate {
21307c478bd9Sstevel@tonic-gate uberdata_t *udp = curthread->ul_uberdata;
21317c478bd9Sstevel@tonic-gate ulwp_t *ulwp;
21327c478bd9Sstevel@tonic-gate mutex_t *mp;
21337c478bd9Sstevel@tonic-gate int error = 0;
21347c478bd9Sstevel@tonic-gate
21357c478bd9Sstevel@tonic-gate ASSERT(whystopped == TSTP_REGULAR ||
21367c478bd9Sstevel@tonic-gate whystopped == TSTP_MUTATOR);
21377c478bd9Sstevel@tonic-gate
213836319254Sraf /*
213936319254Sraf * We single-thread the entire thread suspend/continue mechanism.
214036319254Sraf */
21412e145884Sraf fork_lock_enter();
214236319254Sraf
214336319254Sraf if ((ulwp = find_lwp(tid)) == NULL) {
214436319254Sraf fork_lock_exit();
21457c478bd9Sstevel@tonic-gate return (ESRCH);
214636319254Sraf }
21477c478bd9Sstevel@tonic-gate
21487c478bd9Sstevel@tonic-gate mp = ulwp_mutex(ulwp, udp);
21497c478bd9Sstevel@tonic-gate if ((whystopped == TSTP_MUTATOR && !ulwp->ul_mutator)) {
21507c478bd9Sstevel@tonic-gate error = EINVAL;
21517c478bd9Sstevel@tonic-gate } else if (ulwp->ul_stop & whystopped) {
21527c478bd9Sstevel@tonic-gate ulwp->ul_stop &= ~whystopped;
21537c478bd9Sstevel@tonic-gate ulwp_broadcast(ulwp);
21547c478bd9Sstevel@tonic-gate if (!ulwp->ul_stop) {
21557c478bd9Sstevel@tonic-gate if (whystopped == TSTP_REGULAR && ulwp->ul_created) {
21567c478bd9Sstevel@tonic-gate ulwp->ul_sp = 0;
21577c478bd9Sstevel@tonic-gate ulwp->ul_created = 0;
21587c478bd9Sstevel@tonic-gate }
21597c478bd9Sstevel@tonic-gate force_continue(ulwp);
21607c478bd9Sstevel@tonic-gate }
21617c478bd9Sstevel@tonic-gate }
21627c478bd9Sstevel@tonic-gate lmutex_unlock(mp);
216336319254Sraf
216436319254Sraf fork_lock_exit();
21657c478bd9Sstevel@tonic-gate return (error);
21667c478bd9Sstevel@tonic-gate }
21677c478bd9Sstevel@tonic-gate
21687c478bd9Sstevel@tonic-gate int
thr_suspend(thread_t tid)21697257d1b4Sraf thr_suspend(thread_t tid)
21707c478bd9Sstevel@tonic-gate {
21717c478bd9Sstevel@tonic-gate return (_thrp_suspend(tid, TSTP_REGULAR));
21727c478bd9Sstevel@tonic-gate }
21737c478bd9Sstevel@tonic-gate
21747c478bd9Sstevel@tonic-gate int
thr_continue(thread_t tid)21757257d1b4Sraf thr_continue(thread_t tid)
21767c478bd9Sstevel@tonic-gate {
21777c478bd9Sstevel@tonic-gate return (_thrp_continue(tid, TSTP_REGULAR));
21787c478bd9Sstevel@tonic-gate }
21797c478bd9Sstevel@tonic-gate
21807c478bd9Sstevel@tonic-gate void
thr_yield()21817257d1b4Sraf thr_yield()
21827c478bd9Sstevel@tonic-gate {
21838cd45542Sraf yield();
21847c478bd9Sstevel@tonic-gate }
21857c478bd9Sstevel@tonic-gate
21867257d1b4Sraf #pragma weak pthread_kill = thr_kill
21877257d1b4Sraf #pragma weak _thr_kill = thr_kill
21887c478bd9Sstevel@tonic-gate int
thr_kill(thread_t tid,int sig)21897257d1b4Sraf thr_kill(thread_t tid, int sig)
21907c478bd9Sstevel@tonic-gate {
21917c478bd9Sstevel@tonic-gate if (sig == SIGCANCEL)
21927c478bd9Sstevel@tonic-gate return (EINVAL);
21937257d1b4Sraf return (_lwp_kill(tid, sig));
21947c478bd9Sstevel@tonic-gate }
21957c478bd9Sstevel@tonic-gate
21967c478bd9Sstevel@tonic-gate /*
21977c478bd9Sstevel@tonic-gate * Exit a critical section, take deferred actions if necessary.
2198e54ab87fSRoger A. Faulkner * Called from exit_critical() and from sigon().
21997c478bd9Sstevel@tonic-gate */
22007c478bd9Sstevel@tonic-gate void
do_exit_critical()22017c478bd9Sstevel@tonic-gate do_exit_critical()
22027c478bd9Sstevel@tonic-gate {
22037c478bd9Sstevel@tonic-gate ulwp_t *self = curthread;
22047c478bd9Sstevel@tonic-gate int sig;
22057c478bd9Sstevel@tonic-gate
22067c478bd9Sstevel@tonic-gate ASSERT(self->ul_critical == 0);
2207e54ab87fSRoger A. Faulkner
2208e54ab87fSRoger A. Faulkner /*
2209e54ab87fSRoger A. Faulkner * Don't suspend ourself or take a deferred signal while dying
2210e54ab87fSRoger A. Faulkner * or while executing inside the dynamic linker (ld.so.1).
2211e54ab87fSRoger A. Faulkner */
2212e54ab87fSRoger A. Faulkner if (self->ul_dead || self->ul_rtld)
22137c478bd9Sstevel@tonic-gate return;
22147c478bd9Sstevel@tonic-gate
22157c478bd9Sstevel@tonic-gate while (self->ul_pleasestop ||
22167c478bd9Sstevel@tonic-gate (self->ul_cursig != 0 && self->ul_sigdefer == 0)) {
22177c478bd9Sstevel@tonic-gate /*
22187c478bd9Sstevel@tonic-gate * Avoid a recursive call to exit_critical() in _thrp_suspend()
22197c478bd9Sstevel@tonic-gate * by keeping self->ul_critical == 1 here.
22207c478bd9Sstevel@tonic-gate */
22217c478bd9Sstevel@tonic-gate self->ul_critical++;
22227c478bd9Sstevel@tonic-gate while (self->ul_pleasestop) {
22237c478bd9Sstevel@tonic-gate /*
22247c478bd9Sstevel@tonic-gate * Guard against suspending ourself while on a sleep
22257c478bd9Sstevel@tonic-gate * queue. See the comments in call_user_handler().
22267c478bd9Sstevel@tonic-gate */
22277c478bd9Sstevel@tonic-gate unsleep_self();
22287c478bd9Sstevel@tonic-gate set_parking_flag(self, 0);
22297c478bd9Sstevel@tonic-gate (void) _thrp_suspend(self->ul_lwpid,
22307c478bd9Sstevel@tonic-gate self->ul_pleasestop);
22317c478bd9Sstevel@tonic-gate }
22327c478bd9Sstevel@tonic-gate self->ul_critical--;
22337c478bd9Sstevel@tonic-gate
22347c478bd9Sstevel@tonic-gate if ((sig = self->ul_cursig) != 0 && self->ul_sigdefer == 0) {
22357c478bd9Sstevel@tonic-gate /*
22367c478bd9Sstevel@tonic-gate * Clear ul_cursig before proceeding.
22377c478bd9Sstevel@tonic-gate * This protects us from the dynamic linker's
22387c478bd9Sstevel@tonic-gate * calls to bind_guard()/bind_clear() in the
22397c478bd9Sstevel@tonic-gate * event that it is invoked to resolve a symbol
22407c478bd9Sstevel@tonic-gate * like take_deferred_signal() below.
22417c478bd9Sstevel@tonic-gate */
22427c478bd9Sstevel@tonic-gate self->ul_cursig = 0;
22437c478bd9Sstevel@tonic-gate take_deferred_signal(sig);
22447c478bd9Sstevel@tonic-gate ASSERT(self->ul_cursig == 0);
22457c478bd9Sstevel@tonic-gate }
22467c478bd9Sstevel@tonic-gate }
22477c478bd9Sstevel@tonic-gate ASSERT(self->ul_critical == 0);
22487c478bd9Sstevel@tonic-gate }
22497c478bd9Sstevel@tonic-gate
2250a574db85Sraf /*
2251a574db85Sraf * _ti_bind_guard() and _ti_bind_clear() are called by the dynamic linker
2252a574db85Sraf * (ld.so.1) when it has do do something, like resolve a symbol to be called
2253a574db85Sraf * by the application or one of its libraries. _ti_bind_guard() is called
2254a574db85Sraf * on entry to ld.so.1, _ti_bind_clear() on exit from ld.so.1 back to the
2255a574db85Sraf * application. The dynamic linker gets special dispensation from libc to
2256a574db85Sraf * run in a critical region (all signals deferred and no thread suspension
2257a574db85Sraf * or forking allowed), and to be immune from cancellation for the duration.
2258a574db85Sraf */
22597c478bd9Sstevel@tonic-gate int
_ti_bind_guard(int flags)22608cd45542Sraf _ti_bind_guard(int flags)
22617c478bd9Sstevel@tonic-gate {
22627c478bd9Sstevel@tonic-gate ulwp_t *self = curthread;
22638cd45542Sraf uberdata_t *udp = self->ul_uberdata;
22648cd45542Sraf int bindflag = (flags & THR_FLG_RTLD);
22657c478bd9Sstevel@tonic-gate
22667c478bd9Sstevel@tonic-gate if ((self->ul_bindflags & bindflag) == bindflag)
22677c478bd9Sstevel@tonic-gate return (0);
22682a8d6ebaSRod Evans self->ul_bindflags |= bindflag;
22698cd45542Sraf if ((flags & (THR_FLG_NOLOCK | THR_FLG_REENTER)) == THR_FLG_NOLOCK) {
22708cd45542Sraf sigoff(self); /* see no signals while holding ld_lock */
2271e54ab87fSRoger A. Faulkner self->ul_rtld++; /* don't suspend while in ld.so.1 */
22727257d1b4Sraf (void) mutex_lock(&udp->ld_lock);
22738cd45542Sraf }
22747c478bd9Sstevel@tonic-gate enter_critical(self);
2275a574db85Sraf self->ul_save_state = self->ul_cancel_disabled;
2276a574db85Sraf self->ul_cancel_disabled = 1;
2277a574db85Sraf set_cancel_pending_flag(self, 0);
22787c478bd9Sstevel@tonic-gate return (1);
22797c478bd9Sstevel@tonic-gate }
22807c478bd9Sstevel@tonic-gate
22817c478bd9Sstevel@tonic-gate int
_ti_bind_clear(int flags)22828cd45542Sraf _ti_bind_clear(int flags)
22837c478bd9Sstevel@tonic-gate {
22847c478bd9Sstevel@tonic-gate ulwp_t *self = curthread;
22858cd45542Sraf uberdata_t *udp = self->ul_uberdata;
22868cd45542Sraf int bindflag = (flags & THR_FLG_RTLD);
22877c478bd9Sstevel@tonic-gate
22887c478bd9Sstevel@tonic-gate if ((self->ul_bindflags & bindflag) == 0)
22897c478bd9Sstevel@tonic-gate return (self->ul_bindflags);
22907c478bd9Sstevel@tonic-gate self->ul_bindflags &= ~bindflag;
2291a574db85Sraf self->ul_cancel_disabled = self->ul_save_state;
2292a574db85Sraf set_cancel_pending_flag(self, 0);
22937c478bd9Sstevel@tonic-gate exit_critical(self);
22948cd45542Sraf if ((flags & (THR_FLG_NOLOCK | THR_FLG_REENTER)) == THR_FLG_NOLOCK) {
22958cd45542Sraf if (MUTEX_OWNED(&udp->ld_lock, self)) {
22967257d1b4Sraf (void) mutex_unlock(&udp->ld_lock);
2297e54ab87fSRoger A. Faulkner self->ul_rtld--;
22988cd45542Sraf sigon(self); /* reenable signals */
22998cd45542Sraf }
23008cd45542Sraf }
23017c478bd9Sstevel@tonic-gate return (self->ul_bindflags);
23027c478bd9Sstevel@tonic-gate }
23037c478bd9Sstevel@tonic-gate
23047c478bd9Sstevel@tonic-gate /*
23052a8d6ebaSRod Evans * Tell the dynamic linker (ld.so.1) whether or not it was entered from
23062a8d6ebaSRod Evans * a critical region in libc. Return zero if not, else return non-zero.
23072a8d6ebaSRod Evans */
23082a8d6ebaSRod Evans int
_ti_critical(void)23092a8d6ebaSRod Evans _ti_critical(void)
23102a8d6ebaSRod Evans {
23112a8d6ebaSRod Evans ulwp_t *self = curthread;
23122a8d6ebaSRod Evans int level = self->ul_critical;
23132a8d6ebaSRod Evans
23142a8d6ebaSRod Evans if ((self->ul_bindflags & THR_FLG_RTLD) == 0 || level == 0)
23152a8d6ebaSRod Evans return (level); /* ld.so.1 hasn't (yet) called enter() */
23162a8d6ebaSRod Evans return (level - 1);
23172a8d6ebaSRod Evans }
23182a8d6ebaSRod Evans
23192a8d6ebaSRod Evans /*
23207c478bd9Sstevel@tonic-gate * sigoff() and sigon() enable cond_wait() to behave (optionally) like
23217c478bd9Sstevel@tonic-gate * it does in the old libthread (see the comments in cond_wait_queue()).
23227c478bd9Sstevel@tonic-gate * Also, signals are deferred at thread startup until TLS constructors
23237257d1b4Sraf * have all been called, at which time _thrp_setup() calls sigon().
232434709573Sraf *
2325f841f6adSraf * _sigoff() and _sigon() are external consolidation-private interfaces to
2326f841f6adSraf * sigoff() and sigon(), respectively, in libc. These are used in libnsl.
23277c478bd9Sstevel@tonic-gate * Also, _sigoff() and _sigon() are called from dbx's run-time checking
23287c478bd9Sstevel@tonic-gate * (librtc.so) to defer signals during its critical sections (not to be
23297c478bd9Sstevel@tonic-gate * confused with libc critical sections [see exit_critical() above]).
23307c478bd9Sstevel@tonic-gate */
23317c478bd9Sstevel@tonic-gate void
_sigoff(void)23327c478bd9Sstevel@tonic-gate _sigoff(void)
23337c478bd9Sstevel@tonic-gate {
2334e54ab87fSRoger A. Faulkner ulwp_t *self = curthread;
2335e54ab87fSRoger A. Faulkner
2336e54ab87fSRoger A. Faulkner sigoff(self);
23377c478bd9Sstevel@tonic-gate }
23387c478bd9Sstevel@tonic-gate
23397c478bd9Sstevel@tonic-gate void
_sigon(void)23407c478bd9Sstevel@tonic-gate _sigon(void)
23417c478bd9Sstevel@tonic-gate {
2342e54ab87fSRoger A. Faulkner ulwp_t *self = curthread;
23437c478bd9Sstevel@tonic-gate
23447c478bd9Sstevel@tonic-gate ASSERT(self->ul_sigdefer > 0);
2345e54ab87fSRoger A. Faulkner sigon(self);
23467c478bd9Sstevel@tonic-gate }
23477c478bd9Sstevel@tonic-gate
23487c478bd9Sstevel@tonic-gate int
thr_getconcurrency()23497257d1b4Sraf thr_getconcurrency()
23507c478bd9Sstevel@tonic-gate {
23517c478bd9Sstevel@tonic-gate return (thr_concurrency);
23527c478bd9Sstevel@tonic-gate }
23537c478bd9Sstevel@tonic-gate
23547c478bd9Sstevel@tonic-gate int
pthread_getconcurrency()23557257d1b4Sraf pthread_getconcurrency()
23567c478bd9Sstevel@tonic-gate {
23577c478bd9Sstevel@tonic-gate return (pthread_concurrency);
23587c478bd9Sstevel@tonic-gate }
23597c478bd9Sstevel@tonic-gate
23607c478bd9Sstevel@tonic-gate int
thr_setconcurrency(int new_level)23617257d1b4Sraf thr_setconcurrency(int new_level)
23627c478bd9Sstevel@tonic-gate {
23637c478bd9Sstevel@tonic-gate uberdata_t *udp = curthread->ul_uberdata;
23647c478bd9Sstevel@tonic-gate
23657c478bd9Sstevel@tonic-gate if (new_level < 0)
23667c478bd9Sstevel@tonic-gate return (EINVAL);
23677c478bd9Sstevel@tonic-gate if (new_level > 65536) /* 65536 is totally arbitrary */
23687c478bd9Sstevel@tonic-gate return (EAGAIN);
23697c478bd9Sstevel@tonic-gate lmutex_lock(&udp->link_lock);
23707c478bd9Sstevel@tonic-gate if (new_level > thr_concurrency)
23717c478bd9Sstevel@tonic-gate thr_concurrency = new_level;
23727c478bd9Sstevel@tonic-gate lmutex_unlock(&udp->link_lock);
23737c478bd9Sstevel@tonic-gate return (0);
23747c478bd9Sstevel@tonic-gate }
23757c478bd9Sstevel@tonic-gate
23767c478bd9Sstevel@tonic-gate int
pthread_setconcurrency(int new_level)23777257d1b4Sraf pthread_setconcurrency(int new_level)
23787c478bd9Sstevel@tonic-gate {
23797c478bd9Sstevel@tonic-gate if (new_level < 0)
23807c478bd9Sstevel@tonic-gate return (EINVAL);
23817c478bd9Sstevel@tonic-gate if (new_level > 65536) /* 65536 is totally arbitrary */
23827c478bd9Sstevel@tonic-gate return (EAGAIN);
23837c478bd9Sstevel@tonic-gate pthread_concurrency = new_level;
23847c478bd9Sstevel@tonic-gate return (0);
23857c478bd9Sstevel@tonic-gate }
23867c478bd9Sstevel@tonic-gate
23877c478bd9Sstevel@tonic-gate size_t
thr_min_stack(void)23887257d1b4Sraf thr_min_stack(void)
23897c478bd9Sstevel@tonic-gate {
23907c478bd9Sstevel@tonic-gate return (MINSTACK);
23917c478bd9Sstevel@tonic-gate }
23927c478bd9Sstevel@tonic-gate
23937c478bd9Sstevel@tonic-gate int
__nthreads(void)23947c478bd9Sstevel@tonic-gate __nthreads(void)
23957c478bd9Sstevel@tonic-gate {
23967c478bd9Sstevel@tonic-gate return (curthread->ul_uberdata->nthreads);
23977c478bd9Sstevel@tonic-gate }
23987c478bd9Sstevel@tonic-gate
23997c478bd9Sstevel@tonic-gate /*
24007c478bd9Sstevel@tonic-gate * XXX
24017c478bd9Sstevel@tonic-gate * The remainder of this file implements the private interfaces to java for
24027c478bd9Sstevel@tonic-gate * garbage collection. It is no longer used, at least by java 1.2.
24037c478bd9Sstevel@tonic-gate * It can all go away once all old JVMs have disappeared.
24047c478bd9Sstevel@tonic-gate */
24057c478bd9Sstevel@tonic-gate
24067c478bd9Sstevel@tonic-gate int suspendingallmutators; /* when non-zero, suspending all mutators. */
24077c478bd9Sstevel@tonic-gate int suspendedallmutators; /* when non-zero, all mutators suspended. */
24087c478bd9Sstevel@tonic-gate int mutatorsbarrier; /* when non-zero, mutators barrier imposed. */
24097c478bd9Sstevel@tonic-gate mutex_t mutatorslock = DEFAULTMUTEX; /* used to enforce mutators barrier. */
24107c478bd9Sstevel@tonic-gate cond_t mutatorscv = DEFAULTCV; /* where non-mutators sleep. */
24117c478bd9Sstevel@tonic-gate
24127c478bd9Sstevel@tonic-gate /*
24137c478bd9Sstevel@tonic-gate * Get the available register state for the target thread.
24147c478bd9Sstevel@tonic-gate * Return non-volatile registers: TRS_NONVOLATILE
24157c478bd9Sstevel@tonic-gate */
24167257d1b4Sraf #pragma weak _thr_getstate = thr_getstate
24177c478bd9Sstevel@tonic-gate int
thr_getstate(thread_t tid,int * flag,lwpid_t * lwp,stack_t * ss,gregset_t rs)24187257d1b4Sraf thr_getstate(thread_t tid, int *flag, lwpid_t *lwp, stack_t *ss, gregset_t rs)
24197c478bd9Sstevel@tonic-gate {
24207c478bd9Sstevel@tonic-gate ulwp_t *self = curthread;
24217c478bd9Sstevel@tonic-gate uberdata_t *udp = self->ul_uberdata;
24227c478bd9Sstevel@tonic-gate ulwp_t **ulwpp;
24237c478bd9Sstevel@tonic-gate ulwp_t *ulwp;
24247c478bd9Sstevel@tonic-gate int error = 0;
24257c478bd9Sstevel@tonic-gate int trs_flag = TRS_LWPID;
24267c478bd9Sstevel@tonic-gate
24277c478bd9Sstevel@tonic-gate if (tid == 0 || self->ul_lwpid == tid) {
24287c478bd9Sstevel@tonic-gate ulwp = self;
24297c478bd9Sstevel@tonic-gate ulwp_lock(ulwp, udp);
24307c478bd9Sstevel@tonic-gate } else if ((ulwpp = find_lwpp(tid)) != NULL) {
24317c478bd9Sstevel@tonic-gate ulwp = *ulwpp;
24327c478bd9Sstevel@tonic-gate } else {
24337c478bd9Sstevel@tonic-gate if (flag)
24347c478bd9Sstevel@tonic-gate *flag = TRS_INVALID;
24357c478bd9Sstevel@tonic-gate return (ESRCH);
24367c478bd9Sstevel@tonic-gate }
24377c478bd9Sstevel@tonic-gate
24387c478bd9Sstevel@tonic-gate if (ulwp->ul_dead) {
24397c478bd9Sstevel@tonic-gate trs_flag = TRS_INVALID;
24407c478bd9Sstevel@tonic-gate } else if (!ulwp->ul_stop && !suspendedallmutators) {
24417c478bd9Sstevel@tonic-gate error = EINVAL;
24427c478bd9Sstevel@tonic-gate trs_flag = TRS_INVALID;
24437c478bd9Sstevel@tonic-gate } else if (ulwp->ul_stop) {
24447c478bd9Sstevel@tonic-gate trs_flag = TRS_NONVOLATILE;
24457c478bd9Sstevel@tonic-gate getgregs(ulwp, rs);
24467c478bd9Sstevel@tonic-gate }
24477c478bd9Sstevel@tonic-gate
24487c478bd9Sstevel@tonic-gate if (flag)
24497c478bd9Sstevel@tonic-gate *flag = trs_flag;
24507c478bd9Sstevel@tonic-gate if (lwp)
24517c478bd9Sstevel@tonic-gate *lwp = tid;
24527c478bd9Sstevel@tonic-gate if (ss != NULL)
24537c478bd9Sstevel@tonic-gate (void) _thrp_stksegment(ulwp, ss);
24547c478bd9Sstevel@tonic-gate
24557c478bd9Sstevel@tonic-gate ulwp_unlock(ulwp, udp);
24567c478bd9Sstevel@tonic-gate return (error);
24577c478bd9Sstevel@tonic-gate }
24587c478bd9Sstevel@tonic-gate
24597c478bd9Sstevel@tonic-gate /*
24607c478bd9Sstevel@tonic-gate * Set the appropriate register state for the target thread.
24617c478bd9Sstevel@tonic-gate * This is not used by java. It exists solely for the MSTC test suite.
24627c478bd9Sstevel@tonic-gate */
24637257d1b4Sraf #pragma weak _thr_setstate = thr_setstate
24647c478bd9Sstevel@tonic-gate int
thr_setstate(thread_t tid,int flag,gregset_t rs)24657257d1b4Sraf thr_setstate(thread_t tid, int flag, gregset_t rs)
24667c478bd9Sstevel@tonic-gate {
24677c478bd9Sstevel@tonic-gate uberdata_t *udp = curthread->ul_uberdata;
24687c478bd9Sstevel@tonic-gate ulwp_t *ulwp;
24697c478bd9Sstevel@tonic-gate int error = 0;
24707c478bd9Sstevel@tonic-gate
24717c478bd9Sstevel@tonic-gate if ((ulwp = find_lwp(tid)) == NULL)
24727c478bd9Sstevel@tonic-gate return (ESRCH);
24737c478bd9Sstevel@tonic-gate
24747c478bd9Sstevel@tonic-gate if (!ulwp->ul_stop && !suspendedallmutators)
24757c478bd9Sstevel@tonic-gate error = EINVAL;
24767c478bd9Sstevel@tonic-gate else if (rs != NULL) {
24777c478bd9Sstevel@tonic-gate switch (flag) {
24787c478bd9Sstevel@tonic-gate case TRS_NONVOLATILE:
24797c478bd9Sstevel@tonic-gate /* do /proc stuff here? */
24807c478bd9Sstevel@tonic-gate if (ulwp->ul_stop)
24817c478bd9Sstevel@tonic-gate setgregs(ulwp, rs);
24827c478bd9Sstevel@tonic-gate else
24837c478bd9Sstevel@tonic-gate error = EINVAL;
24847c478bd9Sstevel@tonic-gate break;
24857c478bd9Sstevel@tonic-gate case TRS_LWPID: /* do /proc stuff here? */
24867c478bd9Sstevel@tonic-gate default:
24877c478bd9Sstevel@tonic-gate error = EINVAL;
24887c478bd9Sstevel@tonic-gate break;
24897c478bd9Sstevel@tonic-gate }
24907c478bd9Sstevel@tonic-gate }
24917c478bd9Sstevel@tonic-gate
24927c478bd9Sstevel@tonic-gate ulwp_unlock(ulwp, udp);
24937c478bd9Sstevel@tonic-gate return (error);
24947c478bd9Sstevel@tonic-gate }
24957c478bd9Sstevel@tonic-gate
24967c478bd9Sstevel@tonic-gate int
getlwpstatus(thread_t tid,struct lwpstatus * sp)24977c478bd9Sstevel@tonic-gate getlwpstatus(thread_t tid, struct lwpstatus *sp)
24987c478bd9Sstevel@tonic-gate {
2499a574db85Sraf extern ssize_t __pread(int, void *, size_t, off_t);
25007c478bd9Sstevel@tonic-gate char buf[100];
25017c478bd9Sstevel@tonic-gate int fd;
25027c478bd9Sstevel@tonic-gate
25037c478bd9Sstevel@tonic-gate /* "/proc/self/lwp/%u/lwpstatus" w/o stdio */
25047c478bd9Sstevel@tonic-gate (void) strcpy(buf, "/proc/self/lwp/");
25057c478bd9Sstevel@tonic-gate ultos((uint64_t)tid, 10, buf + strlen(buf));
25067c478bd9Sstevel@tonic-gate (void) strcat(buf, "/lwpstatus");
25078cd45542Sraf if ((fd = __open(buf, O_RDONLY, 0)) >= 0) {
2508a574db85Sraf while (__pread(fd, sp, sizeof (*sp), 0) == sizeof (*sp)) {
25097c478bd9Sstevel@tonic-gate if (sp->pr_flags & PR_STOPPED) {
25108cd45542Sraf (void) __close(fd);
25117c478bd9Sstevel@tonic-gate return (0);
25127c478bd9Sstevel@tonic-gate }
25138cd45542Sraf yield(); /* give him a chance to stop */
25147c478bd9Sstevel@tonic-gate }
25158cd45542Sraf (void) __close(fd);
25167c478bd9Sstevel@tonic-gate }
25177c478bd9Sstevel@tonic-gate return (-1);
25187c478bd9Sstevel@tonic-gate }
25197c478bd9Sstevel@tonic-gate
25207c478bd9Sstevel@tonic-gate int
putlwpregs(thread_t tid,prgregset_t prp)25217c478bd9Sstevel@tonic-gate putlwpregs(thread_t tid, prgregset_t prp)
25227c478bd9Sstevel@tonic-gate {
2523a574db85Sraf extern ssize_t __writev(int, const struct iovec *, int);
25247c478bd9Sstevel@tonic-gate char buf[100];
25257c478bd9Sstevel@tonic-gate int fd;
25267c478bd9Sstevel@tonic-gate long dstop_sreg[2];
25277c478bd9Sstevel@tonic-gate long run_null[2];
25287c478bd9Sstevel@tonic-gate iovec_t iov[3];
25297c478bd9Sstevel@tonic-gate
25307c478bd9Sstevel@tonic-gate /* "/proc/self/lwp/%u/lwpctl" w/o stdio */
25317c478bd9Sstevel@tonic-gate (void) strcpy(buf, "/proc/self/lwp/");
25327c478bd9Sstevel@tonic-gate ultos((uint64_t)tid, 10, buf + strlen(buf));
25337c478bd9Sstevel@tonic-gate (void) strcat(buf, "/lwpctl");
25348cd45542Sraf if ((fd = __open(buf, O_WRONLY, 0)) >= 0) {
25357c478bd9Sstevel@tonic-gate dstop_sreg[0] = PCDSTOP; /* direct it to stop */
25367c478bd9Sstevel@tonic-gate dstop_sreg[1] = PCSREG; /* set the registers */
25377c478bd9Sstevel@tonic-gate iov[0].iov_base = (caddr_t)dstop_sreg;
25387c478bd9Sstevel@tonic-gate iov[0].iov_len = sizeof (dstop_sreg);
25397c478bd9Sstevel@tonic-gate iov[1].iov_base = (caddr_t)prp; /* from the register set */
25407c478bd9Sstevel@tonic-gate iov[1].iov_len = sizeof (prgregset_t);
25417c478bd9Sstevel@tonic-gate run_null[0] = PCRUN; /* make it runnable again */
25427c478bd9Sstevel@tonic-gate run_null[1] = 0;
25437c478bd9Sstevel@tonic-gate iov[2].iov_base = (caddr_t)run_null;
25447c478bd9Sstevel@tonic-gate iov[2].iov_len = sizeof (run_null);
2545a574db85Sraf if (__writev(fd, iov, 3) >= 0) {
25468cd45542Sraf (void) __close(fd);
25477c478bd9Sstevel@tonic-gate return (0);
25487c478bd9Sstevel@tonic-gate }
25498cd45542Sraf (void) __close(fd);
25507c478bd9Sstevel@tonic-gate }
25517c478bd9Sstevel@tonic-gate return (-1);
25527c478bd9Sstevel@tonic-gate }
25537c478bd9Sstevel@tonic-gate
25547c478bd9Sstevel@tonic-gate static ulong_t
gettsp_slow(thread_t tid)25557c478bd9Sstevel@tonic-gate gettsp_slow(thread_t tid)
25567c478bd9Sstevel@tonic-gate {
25577c478bd9Sstevel@tonic-gate char buf[100];
25587c478bd9Sstevel@tonic-gate struct lwpstatus status;
25597c478bd9Sstevel@tonic-gate
25607c478bd9Sstevel@tonic-gate if (getlwpstatus(tid, &status) != 0) {
25617c478bd9Sstevel@tonic-gate /* "__gettsp(%u): can't read lwpstatus" w/o stdio */
25627c478bd9Sstevel@tonic-gate (void) strcpy(buf, "__gettsp(");
25637c478bd9Sstevel@tonic-gate ultos((uint64_t)tid, 10, buf + strlen(buf));
25647c478bd9Sstevel@tonic-gate (void) strcat(buf, "): can't read lwpstatus");
25657c478bd9Sstevel@tonic-gate thr_panic(buf);
25667c478bd9Sstevel@tonic-gate }
25677c478bd9Sstevel@tonic-gate return (status.pr_reg[R_SP]);
25687c478bd9Sstevel@tonic-gate }
25697c478bd9Sstevel@tonic-gate
25707c478bd9Sstevel@tonic-gate ulong_t
__gettsp(thread_t tid)25717c478bd9Sstevel@tonic-gate __gettsp(thread_t tid)
25727c478bd9Sstevel@tonic-gate {
25737c478bd9Sstevel@tonic-gate uberdata_t *udp = curthread->ul_uberdata;
25747c478bd9Sstevel@tonic-gate ulwp_t *ulwp;
25757c478bd9Sstevel@tonic-gate ulong_t result;
25767c478bd9Sstevel@tonic-gate
25777c478bd9Sstevel@tonic-gate if ((ulwp = find_lwp(tid)) == NULL)
25787c478bd9Sstevel@tonic-gate return (0);
25797c478bd9Sstevel@tonic-gate
25807c478bd9Sstevel@tonic-gate if (ulwp->ul_stop && (result = ulwp->ul_sp) != 0) {
25817c478bd9Sstevel@tonic-gate ulwp_unlock(ulwp, udp);
25827c478bd9Sstevel@tonic-gate return (result);
25837c478bd9Sstevel@tonic-gate }
25847c478bd9Sstevel@tonic-gate
25857c478bd9Sstevel@tonic-gate result = gettsp_slow(tid);
25867c478bd9Sstevel@tonic-gate ulwp_unlock(ulwp, udp);
25877c478bd9Sstevel@tonic-gate return (result);
25887c478bd9Sstevel@tonic-gate }
25897c478bd9Sstevel@tonic-gate
25907c478bd9Sstevel@tonic-gate /*
25917c478bd9Sstevel@tonic-gate * This tells java stack walkers how to find the ucontext
25927c478bd9Sstevel@tonic-gate * structure passed to signal handlers.
25937c478bd9Sstevel@tonic-gate */
25947257d1b4Sraf #pragma weak _thr_sighndlrinfo = thr_sighndlrinfo
25957c478bd9Sstevel@tonic-gate void
thr_sighndlrinfo(void (** func)(),int * funcsize)25967257d1b4Sraf thr_sighndlrinfo(void (**func)(), int *funcsize)
25977c478bd9Sstevel@tonic-gate {
25987c478bd9Sstevel@tonic-gate *func = &__sighndlr;
25997c478bd9Sstevel@tonic-gate *funcsize = (char *)&__sighndlrend - (char *)&__sighndlr;
26007c478bd9Sstevel@tonic-gate }
26017c478bd9Sstevel@tonic-gate
26027c478bd9Sstevel@tonic-gate /*
26037c478bd9Sstevel@tonic-gate * Mark a thread a mutator or reset a mutator to being a default,
26047c478bd9Sstevel@tonic-gate * non-mutator thread.
26057c478bd9Sstevel@tonic-gate */
26067257d1b4Sraf #pragma weak _thr_setmutator = thr_setmutator
26077c478bd9Sstevel@tonic-gate int
thr_setmutator(thread_t tid,int enabled)26087257d1b4Sraf thr_setmutator(thread_t tid, int enabled)
26097c478bd9Sstevel@tonic-gate {
26107c478bd9Sstevel@tonic-gate ulwp_t *self = curthread;
26117c478bd9Sstevel@tonic-gate uberdata_t *udp = self->ul_uberdata;
26127c478bd9Sstevel@tonic-gate ulwp_t *ulwp;
26137c478bd9Sstevel@tonic-gate int error;
2614a574db85Sraf int cancel_state;
26157c478bd9Sstevel@tonic-gate
26167c478bd9Sstevel@tonic-gate enabled = enabled? 1 : 0;
26177c478bd9Sstevel@tonic-gate top:
26187c478bd9Sstevel@tonic-gate if (tid == 0) {
26197c478bd9Sstevel@tonic-gate ulwp = self;
26207c478bd9Sstevel@tonic-gate ulwp_lock(ulwp, udp);
26217c478bd9Sstevel@tonic-gate } else if ((ulwp = find_lwp(tid)) == NULL) {
26227c478bd9Sstevel@tonic-gate return (ESRCH);
26237c478bd9Sstevel@tonic-gate }
26247c478bd9Sstevel@tonic-gate
26257c478bd9Sstevel@tonic-gate /*
26267c478bd9Sstevel@tonic-gate * The target thread should be the caller itself or a suspended thread.
26277c478bd9Sstevel@tonic-gate * This prevents the target from also changing its ul_mutator field.
26287c478bd9Sstevel@tonic-gate */
26297c478bd9Sstevel@tonic-gate error = 0;
26307c478bd9Sstevel@tonic-gate if (ulwp != self && !ulwp->ul_stop && enabled)
26317c478bd9Sstevel@tonic-gate error = EINVAL;
26327c478bd9Sstevel@tonic-gate else if (ulwp->ul_mutator != enabled) {
26337c478bd9Sstevel@tonic-gate lmutex_lock(&mutatorslock);
26347c478bd9Sstevel@tonic-gate if (mutatorsbarrier) {
26357c478bd9Sstevel@tonic-gate ulwp_unlock(ulwp, udp);
26367257d1b4Sraf (void) pthread_setcancelstate(PTHREAD_CANCEL_DISABLE,
2637a574db85Sraf &cancel_state);
26387c478bd9Sstevel@tonic-gate while (mutatorsbarrier)
26397257d1b4Sraf (void) cond_wait(&mutatorscv, &mutatorslock);
26407257d1b4Sraf (void) pthread_setcancelstate(cancel_state, NULL);
26417c478bd9Sstevel@tonic-gate lmutex_unlock(&mutatorslock);
26427c478bd9Sstevel@tonic-gate goto top;
26437c478bd9Sstevel@tonic-gate }
26447c478bd9Sstevel@tonic-gate ulwp->ul_mutator = enabled;
26457c478bd9Sstevel@tonic-gate lmutex_unlock(&mutatorslock);
26467c478bd9Sstevel@tonic-gate }
26477c478bd9Sstevel@tonic-gate
26487c478bd9Sstevel@tonic-gate ulwp_unlock(ulwp, udp);
26497c478bd9Sstevel@tonic-gate return (error);
26507c478bd9Sstevel@tonic-gate }
26517c478bd9Sstevel@tonic-gate
26527c478bd9Sstevel@tonic-gate /*
26537c478bd9Sstevel@tonic-gate * Establish a barrier against new mutators. Any non-mutator trying
26547c478bd9Sstevel@tonic-gate * to become a mutator is suspended until the barrier is removed.
26557c478bd9Sstevel@tonic-gate */
26567257d1b4Sraf #pragma weak _thr_mutators_barrier = thr_mutators_barrier
26577c478bd9Sstevel@tonic-gate void
thr_mutators_barrier(int enabled)26587257d1b4Sraf thr_mutators_barrier(int enabled)
26597c478bd9Sstevel@tonic-gate {
26607c478bd9Sstevel@tonic-gate int oldvalue;
2661a574db85Sraf int cancel_state;
26627c478bd9Sstevel@tonic-gate
26637c478bd9Sstevel@tonic-gate lmutex_lock(&mutatorslock);
26647c478bd9Sstevel@tonic-gate
26657c478bd9Sstevel@tonic-gate /*
26667c478bd9Sstevel@tonic-gate * Wait if trying to set the barrier while it is already set.
26677c478bd9Sstevel@tonic-gate */
26687257d1b4Sraf (void) pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cancel_state);
26697c478bd9Sstevel@tonic-gate while (mutatorsbarrier && enabled)
26707257d1b4Sraf (void) cond_wait(&mutatorscv, &mutatorslock);
26717257d1b4Sraf (void) pthread_setcancelstate(cancel_state, NULL);
26727c478bd9Sstevel@tonic-gate
26737c478bd9Sstevel@tonic-gate oldvalue = mutatorsbarrier;
26747c478bd9Sstevel@tonic-gate mutatorsbarrier = enabled;
26757c478bd9Sstevel@tonic-gate /*
26767c478bd9Sstevel@tonic-gate * Wakeup any blocked non-mutators when barrier is removed.
26777c478bd9Sstevel@tonic-gate */
26787c478bd9Sstevel@tonic-gate if (oldvalue && !enabled)
26797257d1b4Sraf (void) cond_broadcast(&mutatorscv);
26807c478bd9Sstevel@tonic-gate lmutex_unlock(&mutatorslock);
26817c478bd9Sstevel@tonic-gate }
26827c478bd9Sstevel@tonic-gate
26837c478bd9Sstevel@tonic-gate /*
26847c478bd9Sstevel@tonic-gate * Suspend the set of all mutators except for the caller. The list
26857c478bd9Sstevel@tonic-gate * of actively running threads is searched and only the mutators
26867c478bd9Sstevel@tonic-gate * in this list are suspended. Actively running non-mutators remain
26877c478bd9Sstevel@tonic-gate * running. Any other thread is suspended.
26887c478bd9Sstevel@tonic-gate */
26897257d1b4Sraf #pragma weak _thr_suspend_allmutators = thr_suspend_allmutators
26907c478bd9Sstevel@tonic-gate int
thr_suspend_allmutators(void)26917257d1b4Sraf thr_suspend_allmutators(void)
26927c478bd9Sstevel@tonic-gate {
26937c478bd9Sstevel@tonic-gate ulwp_t *self = curthread;
26947c478bd9Sstevel@tonic-gate uberdata_t *udp = self->ul_uberdata;
26957c478bd9Sstevel@tonic-gate ulwp_t *ulwp;
26967c478bd9Sstevel@tonic-gate int link_dropped;
26977c478bd9Sstevel@tonic-gate
26987c478bd9Sstevel@tonic-gate /*
269936319254Sraf * We single-thread the entire thread suspend/continue mechanism.
27007c478bd9Sstevel@tonic-gate */
27012e145884Sraf fork_lock_enter();
270236319254Sraf
27037c478bd9Sstevel@tonic-gate top:
27047c478bd9Sstevel@tonic-gate lmutex_lock(&udp->link_lock);
27057c478bd9Sstevel@tonic-gate
27067c478bd9Sstevel@tonic-gate if (suspendingallmutators || suspendedallmutators) {
27077c478bd9Sstevel@tonic-gate lmutex_unlock(&udp->link_lock);
27087c478bd9Sstevel@tonic-gate fork_lock_exit();
27097c478bd9Sstevel@tonic-gate return (EINVAL);
27107c478bd9Sstevel@tonic-gate }
27117c478bd9Sstevel@tonic-gate suspendingallmutators = 1;
27127c478bd9Sstevel@tonic-gate
27137c478bd9Sstevel@tonic-gate for (ulwp = self->ul_forw; ulwp != self; ulwp = ulwp->ul_forw) {
27147c478bd9Sstevel@tonic-gate ulwp_lock(ulwp, udp);
27157c478bd9Sstevel@tonic-gate if (!ulwp->ul_mutator) {
27167c478bd9Sstevel@tonic-gate ulwp_unlock(ulwp, udp);
27177c478bd9Sstevel@tonic-gate } else if (ulwp->ul_stop) { /* already stopped */
27187c478bd9Sstevel@tonic-gate ulwp->ul_stop |= TSTP_MUTATOR;
27197c478bd9Sstevel@tonic-gate ulwp_broadcast(ulwp);
27207c478bd9Sstevel@tonic-gate ulwp_unlock(ulwp, udp);
27217c478bd9Sstevel@tonic-gate } else {
27227c478bd9Sstevel@tonic-gate /*
27237c478bd9Sstevel@tonic-gate * Move the stopped lwp out of a critical section.
27247c478bd9Sstevel@tonic-gate */
27257c478bd9Sstevel@tonic-gate if (safe_suspend(ulwp, TSTP_MUTATOR, &link_dropped) ||
27267c478bd9Sstevel@tonic-gate link_dropped) {
27277c478bd9Sstevel@tonic-gate suspendingallmutators = 0;
27287c478bd9Sstevel@tonic-gate goto top;
27297c478bd9Sstevel@tonic-gate }
27307c478bd9Sstevel@tonic-gate }
27317c478bd9Sstevel@tonic-gate }
27327c478bd9Sstevel@tonic-gate
27337c478bd9Sstevel@tonic-gate suspendedallmutators = 1;
27347c478bd9Sstevel@tonic-gate suspendingallmutators = 0;
27357c478bd9Sstevel@tonic-gate lmutex_unlock(&udp->link_lock);
27367c478bd9Sstevel@tonic-gate fork_lock_exit();
27377c478bd9Sstevel@tonic-gate return (0);
27387c478bd9Sstevel@tonic-gate }
27397c478bd9Sstevel@tonic-gate
27407c478bd9Sstevel@tonic-gate /*
27417c478bd9Sstevel@tonic-gate * Suspend the target mutator. The caller is permitted to suspend
27427c478bd9Sstevel@tonic-gate * itself. If a mutator barrier is enabled, the caller will suspend
27437c478bd9Sstevel@tonic-gate * itself as though it had been suspended by thr_suspend_allmutators().
27447c478bd9Sstevel@tonic-gate * When the barrier is removed, this thread will be resumed. Any
27457c478bd9Sstevel@tonic-gate * suspended mutator, whether suspended by thr_suspend_mutator(), or by
27467c478bd9Sstevel@tonic-gate * thr_suspend_allmutators(), can be resumed by thr_continue_mutator().
27477c478bd9Sstevel@tonic-gate */
27487257d1b4Sraf #pragma weak _thr_suspend_mutator = thr_suspend_mutator
27497c478bd9Sstevel@tonic-gate int
thr_suspend_mutator(thread_t tid)27507257d1b4Sraf thr_suspend_mutator(thread_t tid)
27517c478bd9Sstevel@tonic-gate {
27527c478bd9Sstevel@tonic-gate if (tid == 0)
27537c478bd9Sstevel@tonic-gate tid = curthread->ul_lwpid;
27547c478bd9Sstevel@tonic-gate return (_thrp_suspend(tid, TSTP_MUTATOR));
27557c478bd9Sstevel@tonic-gate }
27567c478bd9Sstevel@tonic-gate
27577c478bd9Sstevel@tonic-gate /*
27587c478bd9Sstevel@tonic-gate * Resume the set of all suspended mutators.
27597c478bd9Sstevel@tonic-gate */
27607257d1b4Sraf #pragma weak _thr_continue_allmutators = thr_continue_allmutators
27617c478bd9Sstevel@tonic-gate int
thr_continue_allmutators()27627257d1b4Sraf thr_continue_allmutators()
27637c478bd9Sstevel@tonic-gate {
27647c478bd9Sstevel@tonic-gate ulwp_t *self = curthread;
27657c478bd9Sstevel@tonic-gate uberdata_t *udp = self->ul_uberdata;
27667c478bd9Sstevel@tonic-gate ulwp_t *ulwp;
276736319254Sraf
276836319254Sraf /*
276936319254Sraf * We single-thread the entire thread suspend/continue mechanism.
277036319254Sraf */
27712e145884Sraf fork_lock_enter();
27727c478bd9Sstevel@tonic-gate
27737c478bd9Sstevel@tonic-gate lmutex_lock(&udp->link_lock);
27747c478bd9Sstevel@tonic-gate if (!suspendedallmutators) {
27757c478bd9Sstevel@tonic-gate lmutex_unlock(&udp->link_lock);
277636319254Sraf fork_lock_exit();
27777c478bd9Sstevel@tonic-gate return (EINVAL);
27787c478bd9Sstevel@tonic-gate }
27797c478bd9Sstevel@tonic-gate suspendedallmutators = 0;
27807c478bd9Sstevel@tonic-gate
27817c478bd9Sstevel@tonic-gate for (ulwp = self->ul_forw; ulwp != self; ulwp = ulwp->ul_forw) {
27827c478bd9Sstevel@tonic-gate mutex_t *mp = ulwp_mutex(ulwp, udp);
27837c478bd9Sstevel@tonic-gate lmutex_lock(mp);
27847c478bd9Sstevel@tonic-gate if (ulwp->ul_stop & TSTP_MUTATOR) {
27857c478bd9Sstevel@tonic-gate ulwp->ul_stop &= ~TSTP_MUTATOR;
27867c478bd9Sstevel@tonic-gate ulwp_broadcast(ulwp);
27877c478bd9Sstevel@tonic-gate if (!ulwp->ul_stop)
27887c478bd9Sstevel@tonic-gate force_continue(ulwp);
27897c478bd9Sstevel@tonic-gate }
27907c478bd9Sstevel@tonic-gate lmutex_unlock(mp);
27917c478bd9Sstevel@tonic-gate }
27927c478bd9Sstevel@tonic-gate
27937c478bd9Sstevel@tonic-gate lmutex_unlock(&udp->link_lock);
279436319254Sraf fork_lock_exit();
27957c478bd9Sstevel@tonic-gate return (0);
27967c478bd9Sstevel@tonic-gate }
27977c478bd9Sstevel@tonic-gate
27987c478bd9Sstevel@tonic-gate /*
27997c478bd9Sstevel@tonic-gate * Resume a suspended mutator.
28007c478bd9Sstevel@tonic-gate */
28017257d1b4Sraf #pragma weak _thr_continue_mutator = thr_continue_mutator
28027c478bd9Sstevel@tonic-gate int
thr_continue_mutator(thread_t tid)28037257d1b4Sraf thr_continue_mutator(thread_t tid)
28047c478bd9Sstevel@tonic-gate {
28057c478bd9Sstevel@tonic-gate return (_thrp_continue(tid, TSTP_MUTATOR));
28067c478bd9Sstevel@tonic-gate }
28077c478bd9Sstevel@tonic-gate
28087257d1b4Sraf #pragma weak _thr_wait_mutator = thr_wait_mutator
28097c478bd9Sstevel@tonic-gate int
thr_wait_mutator(thread_t tid,int dontwait)28107257d1b4Sraf thr_wait_mutator(thread_t tid, int dontwait)
28117c478bd9Sstevel@tonic-gate {
28127c478bd9Sstevel@tonic-gate uberdata_t *udp = curthread->ul_uberdata;
28137c478bd9Sstevel@tonic-gate ulwp_t *ulwp;
2814a574db85Sraf int cancel_state;
28157c478bd9Sstevel@tonic-gate int error = 0;
28167c478bd9Sstevel@tonic-gate
28177257d1b4Sraf (void) pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cancel_state);
28187c478bd9Sstevel@tonic-gate top:
2819a574db85Sraf if ((ulwp = find_lwp(tid)) == NULL) {
28207257d1b4Sraf (void) pthread_setcancelstate(cancel_state, NULL);
28217c478bd9Sstevel@tonic-gate return (ESRCH);
2822a574db85Sraf }
28237c478bd9Sstevel@tonic-gate
28247c478bd9Sstevel@tonic-gate if (!ulwp->ul_mutator)
28257c478bd9Sstevel@tonic-gate error = EINVAL;
28267c478bd9Sstevel@tonic-gate else if (dontwait) {
28277c478bd9Sstevel@tonic-gate if (!(ulwp->ul_stop & TSTP_MUTATOR))
28287c478bd9Sstevel@tonic-gate error = EWOULDBLOCK;
28297c478bd9Sstevel@tonic-gate } else if (!(ulwp->ul_stop & TSTP_MUTATOR)) {
28307c478bd9Sstevel@tonic-gate cond_t *cvp = ulwp_condvar(ulwp, udp);
28317c478bd9Sstevel@tonic-gate mutex_t *mp = ulwp_mutex(ulwp, udp);
28327c478bd9Sstevel@tonic-gate
28337257d1b4Sraf (void) cond_wait(cvp, mp);
28347c478bd9Sstevel@tonic-gate (void) lmutex_unlock(mp);
28357c478bd9Sstevel@tonic-gate goto top;
28367c478bd9Sstevel@tonic-gate }
28377c478bd9Sstevel@tonic-gate
28387c478bd9Sstevel@tonic-gate ulwp_unlock(ulwp, udp);
28397257d1b4Sraf (void) pthread_setcancelstate(cancel_state, NULL);
28407c478bd9Sstevel@tonic-gate return (error);
28417c478bd9Sstevel@tonic-gate }
28427c478bd9Sstevel@tonic-gate
28437c478bd9Sstevel@tonic-gate /* PROBE_SUPPORT begin */
28447c478bd9Sstevel@tonic-gate
28457c478bd9Sstevel@tonic-gate void
thr_probe_setup(void * data)28467c478bd9Sstevel@tonic-gate thr_probe_setup(void *data)
28477c478bd9Sstevel@tonic-gate {
28487c478bd9Sstevel@tonic-gate curthread->ul_tpdp = data;
28497c478bd9Sstevel@tonic-gate }
28507c478bd9Sstevel@tonic-gate
28517c478bd9Sstevel@tonic-gate static void *
_thread_probe_getfunc()28527c478bd9Sstevel@tonic-gate _thread_probe_getfunc()
28537c478bd9Sstevel@tonic-gate {
28547c478bd9Sstevel@tonic-gate return (curthread->ul_tpdp);
28557c478bd9Sstevel@tonic-gate }
28567c478bd9Sstevel@tonic-gate
28577c478bd9Sstevel@tonic-gate void * (*thr_probe_getfunc_addr)(void) = _thread_probe_getfunc;
28587c478bd9Sstevel@tonic-gate
28597c478bd9Sstevel@tonic-gate /* ARGSUSED */
28607c478bd9Sstevel@tonic-gate void
_resume(ulwp_t * ulwp,caddr_t sp,int dontsave)28617c478bd9Sstevel@tonic-gate _resume(ulwp_t *ulwp, caddr_t sp, int dontsave)
28627c478bd9Sstevel@tonic-gate {
28637c478bd9Sstevel@tonic-gate /* never called */
28647c478bd9Sstevel@tonic-gate }
28657c478bd9Sstevel@tonic-gate
28667c478bd9Sstevel@tonic-gate /* ARGSUSED */
28677c478bd9Sstevel@tonic-gate void
_resume_ret(ulwp_t * oldlwp)28687c478bd9Sstevel@tonic-gate _resume_ret(ulwp_t *oldlwp)
28697c478bd9Sstevel@tonic-gate {
28707c478bd9Sstevel@tonic-gate /* never called */
28717c478bd9Sstevel@tonic-gate }
28727c478bd9Sstevel@tonic-gate
28737c478bd9Sstevel@tonic-gate /* PROBE_SUPPORT end */
2874